2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/module.h>
20 #include <linux/moduleloader.h>
21 #include <linux/init.h>
22 #include <linux/kallsyms.h>
24 #include <linux/sysfs.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/elf.h>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <linux/syscalls.h>
32 #include <linux/fcntl.h>
33 #include <linux/rcupdate.h>
34 #include <linux/capability.h>
35 #include <linux/cpu.h>
36 #include <linux/moduleparam.h>
37 #include <linux/errno.h>
38 #include <linux/err.h>
39 #include <linux/vermagic.h>
40 #include <linux/notifier.h>
41 #include <linux/sched.h>
42 #include <linux/stop_machine.h>
43 #include <linux/device.h>
44 #include <linux/string.h>
45 #include <linux/mutex.h>
46 #include <linux/rculist.h>
47 #include <asm/uaccess.h>
48 #include <asm/cacheflush.h>
49 #include <linux/license.h>
50 #include <asm/sections.h>
51 #include <linux/tracepoint.h>
52 #include <linux/ftrace.h>
53 #include <linux/async.h>
54 #include <linux/percpu.h>
59 #define DEBUGP(fmt , a...)
62 #ifndef ARCH_SHF_SMALL
63 #define ARCH_SHF_SMALL 0
66 /* If this is set, the section belongs in the init part of the module */
67 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
69 /* List of modules, protected by module_mutex or preempt_disable
70 * (delete uses stop_machine/add uses RCU list operations). */
71 DEFINE_MUTEX(module_mutex
);
72 EXPORT_SYMBOL_GPL(module_mutex
);
73 static LIST_HEAD(modules
);
75 /* Block module loading/unloading? */
76 int modules_disabled
= 0;
78 /* Waiting for a module to finish initializing? */
79 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
81 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
83 /* Bounds of module allocation, for speeding __module_address */
84 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
86 int register_module_notifier(struct notifier_block
* nb
)
88 return blocking_notifier_chain_register(&module_notify_list
, nb
);
90 EXPORT_SYMBOL(register_module_notifier
);
92 int unregister_module_notifier(struct notifier_block
* nb
)
94 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
96 EXPORT_SYMBOL(unregister_module_notifier
);
98 /* We require a truly strong try_module_get(): 0 means failure due to
99 ongoing or failed initialization etc. */
100 static inline int strong_try_module_get(struct module
*mod
)
102 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
104 if (try_module_get(mod
))
110 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
113 mod
->taints
|= (1U << flag
);
117 * A thread that wants to hold a reference to a module only while it
118 * is running can call this to safely exit. nfsd and lockd use this.
120 void __module_put_and_exit(struct module
*mod
, long code
)
125 EXPORT_SYMBOL(__module_put_and_exit
);
127 /* Find a module section: 0 means not found. */
128 static unsigned int find_sec(Elf_Ehdr
*hdr
,
130 const char *secstrings
,
135 for (i
= 1; i
< hdr
->e_shnum
; i
++)
136 /* Alloc bit cleared means "ignore it." */
137 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
138 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
143 /* Find a module section, or NULL. */
144 static void *section_addr(Elf_Ehdr
*hdr
, Elf_Shdr
*shdrs
,
145 const char *secstrings
, const char *name
)
147 /* Section 0 has sh_addr 0. */
148 return (void *)shdrs
[find_sec(hdr
, shdrs
, secstrings
, name
)].sh_addr
;
151 /* Find a module section, or NULL. Fill in number of "objects" in section. */
152 static void *section_objs(Elf_Ehdr
*hdr
,
154 const char *secstrings
,
159 unsigned int sec
= find_sec(hdr
, sechdrs
, secstrings
, name
);
161 /* Section 0 has sh_addr 0 and sh_size 0. */
162 *num
= sechdrs
[sec
].sh_size
/ object_size
;
163 return (void *)sechdrs
[sec
].sh_addr
;
166 /* Provided by the linker */
167 extern const struct kernel_symbol __start___ksymtab
[];
168 extern const struct kernel_symbol __stop___ksymtab
[];
169 extern const struct kernel_symbol __start___ksymtab_gpl
[];
170 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
171 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
172 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
173 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
174 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
175 extern const unsigned long __start___kcrctab
[];
176 extern const unsigned long __start___kcrctab_gpl
[];
177 extern const unsigned long __start___kcrctab_gpl_future
[];
178 #ifdef CONFIG_UNUSED_SYMBOLS
179 extern const struct kernel_symbol __start___ksymtab_unused
[];
180 extern const struct kernel_symbol __stop___ksymtab_unused
[];
181 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
182 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
183 extern const unsigned long __start___kcrctab_unused
[];
184 extern const unsigned long __start___kcrctab_unused_gpl
[];
187 #ifndef CONFIG_MODVERSIONS
188 #define symversion(base, idx) NULL
190 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
193 static bool each_symbol_in_section(const struct symsearch
*arr
,
194 unsigned int arrsize
,
195 struct module
*owner
,
196 bool (*fn
)(const struct symsearch
*syms
,
197 struct module
*owner
,
198 unsigned int symnum
, void *data
),
203 for (j
= 0; j
< arrsize
; j
++) {
204 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
205 if (fn(&arr
[j
], owner
, i
, data
))
212 /* Returns true as soon as fn returns true, otherwise false. */
213 bool each_symbol(bool (*fn
)(const struct symsearch
*arr
, struct module
*owner
,
214 unsigned int symnum
, void *data
), void *data
)
217 const struct symsearch arr
[] = {
218 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
219 NOT_GPL_ONLY
, false },
220 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
221 __start___kcrctab_gpl
,
223 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
224 __start___kcrctab_gpl_future
,
225 WILL_BE_GPL_ONLY
, false },
226 #ifdef CONFIG_UNUSED_SYMBOLS
227 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
228 __start___kcrctab_unused
,
229 NOT_GPL_ONLY
, true },
230 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
231 __start___kcrctab_unused_gpl
,
236 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
239 list_for_each_entry_rcu(mod
, &modules
, list
) {
240 struct symsearch arr
[] = {
241 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
242 NOT_GPL_ONLY
, false },
243 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
246 { mod
->gpl_future_syms
,
247 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
248 mod
->gpl_future_crcs
,
249 WILL_BE_GPL_ONLY
, false },
250 #ifdef CONFIG_UNUSED_SYMBOLS
252 mod
->unused_syms
+ mod
->num_unused_syms
,
254 NOT_GPL_ONLY
, true },
255 { mod
->unused_gpl_syms
,
256 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
257 mod
->unused_gpl_crcs
,
262 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
267 EXPORT_SYMBOL_GPL(each_symbol
);
269 struct find_symbol_arg
{
276 struct module
*owner
;
277 const unsigned long *crc
;
278 const struct kernel_symbol
*sym
;
281 static bool find_symbol_in_section(const struct symsearch
*syms
,
282 struct module
*owner
,
283 unsigned int symnum
, void *data
)
285 struct find_symbol_arg
*fsa
= data
;
287 if (strcmp(syms
->start
[symnum
].name
, fsa
->name
) != 0)
291 if (syms
->licence
== GPL_ONLY
)
293 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
294 printk(KERN_WARNING
"Symbol %s is being used "
295 "by a non-GPL module, which will not "
296 "be allowed in the future\n", fsa
->name
);
297 printk(KERN_WARNING
"Please see the file "
298 "Documentation/feature-removal-schedule.txt "
299 "in the kernel source tree for more details.\n");
303 #ifdef CONFIG_UNUSED_SYMBOLS
304 if (syms
->unused
&& fsa
->warn
) {
305 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
306 "however this module is using it.\n", fsa
->name
);
308 "This symbol will go away in the future.\n");
310 "Please evalute if this is the right api to use and if "
311 "it really is, submit a report the linux kernel "
312 "mailinglist together with submitting your code for "
318 fsa
->crc
= symversion(syms
->crcs
, symnum
);
319 fsa
->sym
= &syms
->start
[symnum
];
323 /* Find a symbol and return it, along with, (optional) crc and
324 * (optional) module which owns it */
325 const struct kernel_symbol
*find_symbol(const char *name
,
326 struct module
**owner
,
327 const unsigned long **crc
,
331 struct find_symbol_arg fsa
;
337 if (each_symbol(find_symbol_in_section
, &fsa
)) {
345 DEBUGP("Failed to find symbol %s\n", name
);
348 EXPORT_SYMBOL_GPL(find_symbol
);
350 /* Search for module by name: must hold module_mutex. */
351 struct module
*find_module(const char *name
)
355 list_for_each_entry(mod
, &modules
, list
) {
356 if (strcmp(mod
->name
, name
) == 0)
361 EXPORT_SYMBOL_GPL(find_module
);
365 #ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
367 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
372 if (align
> PAGE_SIZE
) {
373 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
374 name
, align
, PAGE_SIZE
);
378 ptr
= __alloc_reserved_percpu(size
, align
);
381 "Could not allocate %lu bytes percpu data\n", size
);
385 static void percpu_modfree(void *freeme
)
390 #else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
392 /* Number of blocks used and allocated. */
393 static unsigned int pcpu_num_used
, pcpu_num_allocated
;
394 /* Size of each block. -ve means used. */
395 static int *pcpu_size
;
397 static int split_block(unsigned int i
, unsigned short size
)
399 /* Reallocation required? */
400 if (pcpu_num_used
+ 1 > pcpu_num_allocated
) {
403 new = krealloc(pcpu_size
, sizeof(new[0])*pcpu_num_allocated
*2,
408 pcpu_num_allocated
*= 2;
412 /* Insert a new subblock */
413 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
],
414 sizeof(pcpu_size
[0]) * (pcpu_num_used
- i
));
417 pcpu_size
[i
+1] -= size
;
422 static inline unsigned int block_size(int val
)
429 static void *percpu_modalloc(unsigned long size
, unsigned long align
,
436 if (align
> PAGE_SIZE
) {
437 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
438 name
, align
, PAGE_SIZE
);
442 ptr
= __per_cpu_start
;
443 for (i
= 0; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
444 /* Extra for alignment requirement. */
445 extra
= ALIGN((unsigned long)ptr
, align
) - (unsigned long)ptr
;
446 BUG_ON(i
== 0 && extra
!= 0);
448 if (pcpu_size
[i
] < 0 || pcpu_size
[i
] < extra
+ size
)
451 /* Transfer extra to previous block. */
452 if (pcpu_size
[i
-1] < 0)
453 pcpu_size
[i
-1] -= extra
;
455 pcpu_size
[i
-1] += extra
;
456 pcpu_size
[i
] -= extra
;
459 /* Split block if warranted */
460 if (pcpu_size
[i
] - size
> sizeof(unsigned long))
461 if (!split_block(i
, size
))
465 pcpu_size
[i
] = -pcpu_size
[i
];
469 printk(KERN_WARNING
"Could not allocate %lu bytes percpu data\n",
474 static void percpu_modfree(void *freeme
)
477 void *ptr
= __per_cpu_start
+ block_size(pcpu_size
[0]);
479 /* First entry is core kernel percpu data. */
480 for (i
= 1; i
< pcpu_num_used
; ptr
+= block_size(pcpu_size
[i
]), i
++) {
482 pcpu_size
[i
] = -pcpu_size
[i
];
489 /* Merge with previous? */
490 if (pcpu_size
[i
-1] >= 0) {
491 pcpu_size
[i
-1] += pcpu_size
[i
];
493 memmove(&pcpu_size
[i
], &pcpu_size
[i
+1],
494 (pcpu_num_used
- i
) * sizeof(pcpu_size
[0]));
497 /* Merge with next? */
498 if (i
+1 < pcpu_num_used
&& pcpu_size
[i
+1] >= 0) {
499 pcpu_size
[i
] += pcpu_size
[i
+1];
501 memmove(&pcpu_size
[i
+1], &pcpu_size
[i
+2],
502 (pcpu_num_used
- (i
+1)) * sizeof(pcpu_size
[0]));
506 static int percpu_modinit(void)
509 pcpu_num_allocated
= 2;
510 pcpu_size
= kmalloc(sizeof(pcpu_size
[0]) * pcpu_num_allocated
,
512 /* Static in-kernel percpu data (used). */
513 pcpu_size
[0] = -(__per_cpu_end
-__per_cpu_start
);
515 pcpu_size
[1] = PERCPU_ENOUGH_ROOM
+ pcpu_size
[0];
516 if (pcpu_size
[1] < 0) {
517 printk(KERN_ERR
"No per-cpu room for modules.\n");
523 __initcall(percpu_modinit
);
525 #endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
527 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
529 const char *secstrings
)
531 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
534 static void percpu_modcopy(void *pcpudest
, const void *from
, unsigned long size
)
538 for_each_possible_cpu(cpu
)
539 memcpy(pcpudest
+ per_cpu_offset(cpu
), from
, size
);
542 #else /* ... !CONFIG_SMP */
544 static inline void *percpu_modalloc(unsigned long size
, unsigned long align
,
549 static inline void percpu_modfree(void *pcpuptr
)
553 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
555 const char *secstrings
)
559 static inline void percpu_modcopy(void *pcpudst
, const void *src
,
562 /* pcpusec should be 0, and size of that section should be 0. */
566 #endif /* CONFIG_SMP */
568 #define MODINFO_ATTR(field) \
569 static void setup_modinfo_##field(struct module *mod, const char *s) \
571 mod->field = kstrdup(s, GFP_KERNEL); \
573 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
574 struct module *mod, char *buffer) \
576 return sprintf(buffer, "%s\n", mod->field); \
578 static int modinfo_##field##_exists(struct module *mod) \
580 return mod->field != NULL; \
582 static void free_modinfo_##field(struct module *mod) \
587 static struct module_attribute modinfo_##field = { \
588 .attr = { .name = __stringify(field), .mode = 0444 }, \
589 .show = show_modinfo_##field, \
590 .setup = setup_modinfo_##field, \
591 .test = modinfo_##field##_exists, \
592 .free = free_modinfo_##field, \
595 MODINFO_ATTR(version
);
596 MODINFO_ATTR(srcversion
);
598 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
600 #ifdef CONFIG_MODULE_UNLOAD
601 /* Init the unload section of the module. */
602 static void module_unload_init(struct module
*mod
)
606 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
607 for_each_possible_cpu(cpu
)
608 local_set(__module_ref_addr(mod
, cpu
), 0);
609 /* Hold reference count during initialization. */
610 local_set(__module_ref_addr(mod
, raw_smp_processor_id()), 1);
611 /* Backwards compatibility macros put refcount during init. */
612 mod
->waiter
= current
;
615 /* modules using other modules */
618 struct list_head list
;
619 struct module
*module_which_uses
;
622 /* Does a already use b? */
623 static int already_uses(struct module
*a
, struct module
*b
)
625 struct module_use
*use
;
627 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
628 if (use
->module_which_uses
== a
) {
629 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
633 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
637 /* Module a uses b */
638 int use_module(struct module
*a
, struct module
*b
)
640 struct module_use
*use
;
643 if (b
== NULL
|| already_uses(a
, b
)) return 1;
645 /* If we're interrupted or time out, we fail. */
646 if (wait_event_interruptible_timeout(
647 module_wq
, (err
= strong_try_module_get(b
)) != -EBUSY
,
649 printk("%s: gave up waiting for init of module %s.\n",
654 /* If strong_try_module_get() returned a different error, we fail. */
658 DEBUGP("Allocating new usage for %s.\n", a
->name
);
659 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
661 printk("%s: out of memory loading\n", a
->name
);
666 use
->module_which_uses
= a
;
667 list_add(&use
->list
, &b
->modules_which_use_me
);
668 no_warn
= sysfs_create_link(b
->holders_dir
, &a
->mkobj
.kobj
, a
->name
);
671 EXPORT_SYMBOL_GPL(use_module
);
673 /* Clear the unload stuff of the module. */
674 static void module_unload_free(struct module
*mod
)
678 list_for_each_entry(i
, &modules
, list
) {
679 struct module_use
*use
;
681 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
682 if (use
->module_which_uses
== mod
) {
683 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
685 list_del(&use
->list
);
687 sysfs_remove_link(i
->holders_dir
, mod
->name
);
688 /* There can be at most one match. */
695 #ifdef CONFIG_MODULE_FORCE_UNLOAD
696 static inline int try_force_unload(unsigned int flags
)
698 int ret
= (flags
& O_TRUNC
);
700 add_taint(TAINT_FORCED_RMMOD
);
704 static inline int try_force_unload(unsigned int flags
)
708 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
717 /* Whole machine is stopped with interrupts off when this runs. */
718 static int __try_stop_module(void *_sref
)
720 struct stopref
*sref
= _sref
;
722 /* If it's not unused, quit unless we're forcing. */
723 if (module_refcount(sref
->mod
) != 0) {
724 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
728 /* Mark it as dying. */
729 sref
->mod
->state
= MODULE_STATE_GOING
;
733 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
735 if (flags
& O_NONBLOCK
) {
736 struct stopref sref
= { mod
, flags
, forced
};
738 return stop_machine(__try_stop_module
, &sref
, NULL
);
740 /* We don't need to stop the machine for this. */
741 mod
->state
= MODULE_STATE_GOING
;
747 unsigned int module_refcount(struct module
*mod
)
749 unsigned int total
= 0;
752 for_each_possible_cpu(cpu
)
753 total
+= local_read(__module_ref_addr(mod
, cpu
));
756 EXPORT_SYMBOL(module_refcount
);
758 /* This exists whether we can unload or not */
759 static void free_module(struct module
*mod
);
761 static void wait_for_zero_refcount(struct module
*mod
)
763 /* Since we might sleep for some time, release the mutex first */
764 mutex_unlock(&module_mutex
);
766 DEBUGP("Looking at refcount...\n");
767 set_current_state(TASK_UNINTERRUPTIBLE
);
768 if (module_refcount(mod
) == 0)
772 current
->state
= TASK_RUNNING
;
773 mutex_lock(&module_mutex
);
776 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
780 char name
[MODULE_NAME_LEN
];
783 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
786 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
788 name
[MODULE_NAME_LEN
-1] = '\0';
790 /* Create stop_machine threads since free_module relies on
791 * a non-failing stop_machine call. */
792 ret
= stop_machine_create();
796 if (mutex_lock_interruptible(&module_mutex
) != 0) {
801 mod
= find_module(name
);
807 if (!list_empty(&mod
->modules_which_use_me
)) {
808 /* Other modules depend on us: get rid of them first. */
813 /* Doing init or already dying? */
814 if (mod
->state
!= MODULE_STATE_LIVE
) {
815 /* FIXME: if (force), slam module count and wake up
817 DEBUGP("%s already dying\n", mod
->name
);
822 /* If it has an init func, it must have an exit func to unload */
823 if (mod
->init
&& !mod
->exit
) {
824 forced
= try_force_unload(flags
);
826 /* This module can't be removed */
832 /* Set this up before setting mod->state */
833 mod
->waiter
= current
;
835 /* Stop the machine so refcounts can't move and disable module. */
836 ret
= try_stop_module(mod
, flags
, &forced
);
840 /* Never wait if forced. */
841 if (!forced
&& module_refcount(mod
) != 0)
842 wait_for_zero_refcount(mod
);
844 mutex_unlock(&module_mutex
);
845 /* Final destruction now noone is using it. */
846 if (mod
->exit
!= NULL
)
848 blocking_notifier_call_chain(&module_notify_list
,
849 MODULE_STATE_GOING
, mod
);
850 async_synchronize_full();
851 mutex_lock(&module_mutex
);
852 /* Store the name of the last unloaded module for diagnostic purposes */
853 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
854 ddebug_remove_module(mod
->name
);
858 mutex_unlock(&module_mutex
);
860 stop_machine_destroy();
864 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
866 struct module_use
*use
;
867 int printed_something
= 0;
869 seq_printf(m
, " %u ", module_refcount(mod
));
871 /* Always include a trailing , so userspace can differentiate
872 between this and the old multi-field proc format. */
873 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
874 printed_something
= 1;
875 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
878 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
879 printed_something
= 1;
880 seq_printf(m
, "[permanent],");
883 if (!printed_something
)
887 void __symbol_put(const char *symbol
)
889 struct module
*owner
;
892 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
897 EXPORT_SYMBOL(__symbol_put
);
899 void symbol_put_addr(void *addr
)
901 struct module
*modaddr
;
903 if (core_kernel_text((unsigned long)addr
))
906 /* module_text_address is safe here: we're supposed to have reference
907 * to module from symbol_get, so it can't go away. */
908 modaddr
= __module_text_address((unsigned long)addr
);
912 EXPORT_SYMBOL_GPL(symbol_put_addr
);
914 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
915 struct module
*mod
, char *buffer
)
917 return sprintf(buffer
, "%u\n", module_refcount(mod
));
920 static struct module_attribute refcnt
= {
921 .attr
= { .name
= "refcnt", .mode
= 0444 },
925 void module_put(struct module
*module
)
928 unsigned int cpu
= get_cpu();
929 local_dec(__module_ref_addr(module
, cpu
));
930 /* Maybe they're waiting for us to drop reference? */
931 if (unlikely(!module_is_live(module
)))
932 wake_up_process(module
->waiter
);
936 EXPORT_SYMBOL(module_put
);
938 #else /* !CONFIG_MODULE_UNLOAD */
939 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
941 /* We don't know the usage count, or what modules are using. */
942 seq_printf(m
, " - -");
945 static inline void module_unload_free(struct module
*mod
)
949 int use_module(struct module
*a
, struct module
*b
)
951 return strong_try_module_get(b
) == 0;
953 EXPORT_SYMBOL_GPL(use_module
);
955 static inline void module_unload_init(struct module
*mod
)
958 #endif /* CONFIG_MODULE_UNLOAD */
960 static ssize_t
show_initstate(struct module_attribute
*mattr
,
961 struct module
*mod
, char *buffer
)
963 const char *state
= "unknown";
965 switch (mod
->state
) {
966 case MODULE_STATE_LIVE
:
969 case MODULE_STATE_COMING
:
972 case MODULE_STATE_GOING
:
976 return sprintf(buffer
, "%s\n", state
);
979 static struct module_attribute initstate
= {
980 .attr
= { .name
= "initstate", .mode
= 0444 },
981 .show
= show_initstate
,
984 static struct module_attribute
*modinfo_attrs
[] = {
988 #ifdef CONFIG_MODULE_UNLOAD
994 static const char vermagic
[] = VERMAGIC_STRING
;
996 static int try_to_force_load(struct module
*mod
, const char *reason
)
998 #ifdef CONFIG_MODULE_FORCE_LOAD
999 if (!test_taint(TAINT_FORCED_MODULE
))
1000 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
1002 add_taint_module(mod
, TAINT_FORCED_MODULE
);
1009 #ifdef CONFIG_MODVERSIONS
1010 static int check_version(Elf_Shdr
*sechdrs
,
1011 unsigned int versindex
,
1012 const char *symname
,
1014 const unsigned long *crc
)
1016 unsigned int i
, num_versions
;
1017 struct modversion_info
*versions
;
1019 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1023 /* No versions at all? modprobe --force does this. */
1025 return try_to_force_load(mod
, symname
) == 0;
1027 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1028 num_versions
= sechdrs
[versindex
].sh_size
1029 / sizeof(struct modversion_info
);
1031 for (i
= 0; i
< num_versions
; i
++) {
1032 if (strcmp(versions
[i
].name
, symname
) != 0)
1035 if (versions
[i
].crc
== *crc
)
1037 DEBUGP("Found checksum %lX vs module %lX\n",
1038 *crc
, versions
[i
].crc
);
1042 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1043 mod
->name
, symname
);
1047 printk("%s: disagrees about version of symbol %s\n",
1048 mod
->name
, symname
);
1052 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1053 unsigned int versindex
,
1056 const unsigned long *crc
;
1058 if (!find_symbol("module_layout", NULL
, &crc
, true, false))
1060 return check_version(sechdrs
, versindex
, "module_layout", mod
, crc
);
1063 /* First part is kernel version, which we ignore if module has crcs. */
1064 static inline int same_magic(const char *amagic
, const char *bmagic
,
1068 amagic
+= strcspn(amagic
, " ");
1069 bmagic
+= strcspn(bmagic
, " ");
1071 return strcmp(amagic
, bmagic
) == 0;
1074 static inline int check_version(Elf_Shdr
*sechdrs
,
1075 unsigned int versindex
,
1076 const char *symname
,
1078 const unsigned long *crc
)
1083 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1084 unsigned int versindex
,
1090 static inline int same_magic(const char *amagic
, const char *bmagic
,
1093 return strcmp(amagic
, bmagic
) == 0;
1095 #endif /* CONFIG_MODVERSIONS */
1097 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1098 Must be holding module_mutex. */
1099 static const struct kernel_symbol
*resolve_symbol(Elf_Shdr
*sechdrs
,
1100 unsigned int versindex
,
1104 struct module
*owner
;
1105 const struct kernel_symbol
*sym
;
1106 const unsigned long *crc
;
1108 sym
= find_symbol(name
, &owner
, &crc
,
1109 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1110 /* use_module can fail due to OOM,
1111 or module initialization or unloading */
1113 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
) ||
1114 !use_module(mod
, owner
))
1121 * /sys/module/foo/sections stuff
1122 * J. Corbet <corbet@lwn.net>
1124 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1125 struct module_sect_attr
1127 struct module_attribute mattr
;
1129 unsigned long address
;
1132 struct module_sect_attrs
1134 struct attribute_group grp
;
1135 unsigned int nsections
;
1136 struct module_sect_attr attrs
[0];
1139 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1140 struct module
*mod
, char *buf
)
1142 struct module_sect_attr
*sattr
=
1143 container_of(mattr
, struct module_sect_attr
, mattr
);
1144 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1147 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1149 unsigned int section
;
1151 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1152 kfree(sect_attrs
->attrs
[section
].name
);
1156 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1157 char *secstrings
, Elf_Shdr
*sechdrs
)
1159 unsigned int nloaded
= 0, i
, size
[2];
1160 struct module_sect_attrs
*sect_attrs
;
1161 struct module_sect_attr
*sattr
;
1162 struct attribute
**gattr
;
1164 /* Count loaded sections and allocate structures */
1165 for (i
= 0; i
< nsect
; i
++)
1166 if (sechdrs
[i
].sh_flags
& SHF_ALLOC
)
1168 size
[0] = ALIGN(sizeof(*sect_attrs
)
1169 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1170 sizeof(sect_attrs
->grp
.attrs
[0]));
1171 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1172 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1173 if (sect_attrs
== NULL
)
1176 /* Setup section attributes. */
1177 sect_attrs
->grp
.name
= "sections";
1178 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1180 sect_attrs
->nsections
= 0;
1181 sattr
= §_attrs
->attrs
[0];
1182 gattr
= §_attrs
->grp
.attrs
[0];
1183 for (i
= 0; i
< nsect
; i
++) {
1184 if (! (sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1186 sattr
->address
= sechdrs
[i
].sh_addr
;
1187 sattr
->name
= kstrdup(secstrings
+ sechdrs
[i
].sh_name
,
1189 if (sattr
->name
== NULL
)
1191 sect_attrs
->nsections
++;
1192 sattr
->mattr
.show
= module_sect_show
;
1193 sattr
->mattr
.store
= NULL
;
1194 sattr
->mattr
.attr
.name
= sattr
->name
;
1195 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1196 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1200 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1203 mod
->sect_attrs
= sect_attrs
;
1206 free_sect_attrs(sect_attrs
);
1209 static void remove_sect_attrs(struct module
*mod
)
1211 if (mod
->sect_attrs
) {
1212 sysfs_remove_group(&mod
->mkobj
.kobj
,
1213 &mod
->sect_attrs
->grp
);
1214 /* We are positive that no one is using any sect attrs
1215 * at this point. Deallocate immediately. */
1216 free_sect_attrs(mod
->sect_attrs
);
1217 mod
->sect_attrs
= NULL
;
1222 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1225 struct module_notes_attrs
{
1226 struct kobject
*dir
;
1228 struct bin_attribute attrs
[0];
1231 static ssize_t
module_notes_read(struct kobject
*kobj
,
1232 struct bin_attribute
*bin_attr
,
1233 char *buf
, loff_t pos
, size_t count
)
1236 * The caller checked the pos and count against our size.
1238 memcpy(buf
, bin_attr
->private + pos
, count
);
1242 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1245 if (notes_attrs
->dir
) {
1247 sysfs_remove_bin_file(notes_attrs
->dir
,
1248 ¬es_attrs
->attrs
[i
]);
1249 kobject_put(notes_attrs
->dir
);
1254 static void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1255 char *secstrings
, Elf_Shdr
*sechdrs
)
1257 unsigned int notes
, loaded
, i
;
1258 struct module_notes_attrs
*notes_attrs
;
1259 struct bin_attribute
*nattr
;
1261 /* Count notes sections and allocate structures. */
1263 for (i
= 0; i
< nsect
; i
++)
1264 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
) &&
1265 (sechdrs
[i
].sh_type
== SHT_NOTE
))
1271 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1272 + notes
* sizeof(notes_attrs
->attrs
[0]),
1274 if (notes_attrs
== NULL
)
1277 notes_attrs
->notes
= notes
;
1278 nattr
= ¬es_attrs
->attrs
[0];
1279 for (loaded
= i
= 0; i
< nsect
; ++i
) {
1280 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1282 if (sechdrs
[i
].sh_type
== SHT_NOTE
) {
1283 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1284 nattr
->attr
.mode
= S_IRUGO
;
1285 nattr
->size
= sechdrs
[i
].sh_size
;
1286 nattr
->private = (void *) sechdrs
[i
].sh_addr
;
1287 nattr
->read
= module_notes_read
;
1293 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1294 if (!notes_attrs
->dir
)
1297 for (i
= 0; i
< notes
; ++i
)
1298 if (sysfs_create_bin_file(notes_attrs
->dir
,
1299 ¬es_attrs
->attrs
[i
]))
1302 mod
->notes_attrs
= notes_attrs
;
1306 free_notes_attrs(notes_attrs
, i
);
1309 static void remove_notes_attrs(struct module
*mod
)
1311 if (mod
->notes_attrs
)
1312 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1317 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1318 char *sectstrings
, Elf_Shdr
*sechdrs
)
1322 static inline void remove_sect_attrs(struct module
*mod
)
1326 static inline void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1327 char *sectstrings
, Elf_Shdr
*sechdrs
)
1331 static inline void remove_notes_attrs(struct module
*mod
)
1337 int module_add_modinfo_attrs(struct module
*mod
)
1339 struct module_attribute
*attr
;
1340 struct module_attribute
*temp_attr
;
1344 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1345 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1347 if (!mod
->modinfo_attrs
)
1350 temp_attr
= mod
->modinfo_attrs
;
1351 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1353 (attr
->test
&& attr
->test(mod
))) {
1354 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1355 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1362 void module_remove_modinfo_attrs(struct module
*mod
)
1364 struct module_attribute
*attr
;
1367 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1368 /* pick a field to test for end of list */
1369 if (!attr
->attr
.name
)
1371 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1375 kfree(mod
->modinfo_attrs
);
1378 int mod_sysfs_init(struct module
*mod
)
1381 struct kobject
*kobj
;
1383 if (!module_sysfs_initialized
) {
1384 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1390 kobj
= kset_find_obj(module_kset
, mod
->name
);
1392 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1398 mod
->mkobj
.mod
= mod
;
1400 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1401 mod
->mkobj
.kobj
.kset
= module_kset
;
1402 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1405 kobject_put(&mod
->mkobj
.kobj
);
1407 /* delay uevent until full sysfs population */
1412 int mod_sysfs_setup(struct module
*mod
,
1413 struct kernel_param
*kparam
,
1414 unsigned int num_params
)
1418 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1419 if (!mod
->holders_dir
) {
1424 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1426 goto out_unreg_holders
;
1428 err
= module_add_modinfo_attrs(mod
);
1430 goto out_unreg_param
;
1432 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1436 module_param_sysfs_remove(mod
);
1438 kobject_put(mod
->holders_dir
);
1440 kobject_put(&mod
->mkobj
.kobj
);
1444 static void mod_sysfs_fini(struct module
*mod
)
1446 kobject_put(&mod
->mkobj
.kobj
);
1449 #else /* CONFIG_SYSFS */
1451 static void mod_sysfs_fini(struct module
*mod
)
1455 #endif /* CONFIG_SYSFS */
1457 static void mod_kobject_remove(struct module
*mod
)
1459 module_remove_modinfo_attrs(mod
);
1460 module_param_sysfs_remove(mod
);
1461 kobject_put(mod
->mkobj
.drivers_dir
);
1462 kobject_put(mod
->holders_dir
);
1463 mod_sysfs_fini(mod
);
1467 * unlink the module with the whole machine is stopped with interrupts off
1468 * - this defends against kallsyms not taking locks
1470 static int __unlink_module(void *_mod
)
1472 struct module
*mod
= _mod
;
1473 list_del(&mod
->list
);
1477 /* Free a module, remove from lists, etc (must hold module_mutex). */
1478 static void free_module(struct module
*mod
)
1480 /* Delete from various lists */
1481 stop_machine(__unlink_module
, mod
, NULL
);
1482 remove_notes_attrs(mod
);
1483 remove_sect_attrs(mod
);
1484 mod_kobject_remove(mod
);
1486 /* Arch-specific cleanup. */
1487 module_arch_cleanup(mod
);
1489 /* Module unload stuff */
1490 module_unload_free(mod
);
1492 /* Free any allocated parameters. */
1493 destroy_params(mod
->kp
, mod
->num_kp
);
1495 /* release any pointers to mcount in this module */
1496 ftrace_release(mod
->module_core
, mod
->core_size
);
1498 /* This may be NULL, but that's OK */
1499 module_free(mod
, mod
->module_init
);
1502 percpu_modfree(mod
->percpu
);
1503 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1505 percpu_modfree(mod
->refptr
);
1507 /* Free lock-classes: */
1508 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1510 /* Finally, free the core (containing the module structure) */
1511 module_free(mod
, mod
->module_core
);
1514 void *__symbol_get(const char *symbol
)
1516 struct module
*owner
;
1517 const struct kernel_symbol
*sym
;
1520 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1521 if (sym
&& strong_try_module_get(owner
))
1525 return sym
? (void *)sym
->value
: NULL
;
1527 EXPORT_SYMBOL_GPL(__symbol_get
);
1530 * Ensure that an exported symbol [global namespace] does not already exist
1531 * in the kernel or in some other module's exported symbol table.
1533 static int verify_export_symbols(struct module
*mod
)
1536 struct module
*owner
;
1537 const struct kernel_symbol
*s
;
1539 const struct kernel_symbol
*sym
;
1542 { mod
->syms
, mod
->num_syms
},
1543 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1544 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1545 #ifdef CONFIG_UNUSED_SYMBOLS
1546 { mod
->unused_syms
, mod
->num_unused_syms
},
1547 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1551 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1552 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1553 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1555 "%s: exports duplicate symbol %s"
1557 mod
->name
, s
->name
, module_name(owner
));
1565 /* Change all symbols so that st_value encodes the pointer directly. */
1566 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1567 unsigned int symindex
,
1569 unsigned int versindex
,
1570 unsigned int pcpuindex
,
1573 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1574 unsigned long secbase
;
1575 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1577 const struct kernel_symbol
*ksym
;
1579 for (i
= 1; i
< n
; i
++) {
1580 switch (sym
[i
].st_shndx
) {
1582 /* We compiled with -fno-common. These are not
1583 supposed to happen. */
1584 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1585 printk("%s: please compile with -fno-common\n",
1591 /* Don't need to do anything */
1592 DEBUGP("Absolute symbol: 0x%08lx\n",
1593 (long)sym
[i
].st_value
);
1597 ksym
= resolve_symbol(sechdrs
, versindex
,
1598 strtab
+ sym
[i
].st_name
, mod
);
1599 /* Ok if resolved. */
1601 sym
[i
].st_value
= ksym
->value
;
1606 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1609 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1610 mod
->name
, strtab
+ sym
[i
].st_name
);
1615 /* Divert to percpu allocation if a percpu var. */
1616 if (sym
[i
].st_shndx
== pcpuindex
)
1617 secbase
= (unsigned long)mod
->percpu
;
1619 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1620 sym
[i
].st_value
+= secbase
;
1628 /* Additional bytes needed by arch in front of individual sections */
1629 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1630 unsigned int section
)
1632 /* default implementation just returns zero */
1636 /* Update size with this section: return offset. */
1637 static long get_offset(struct module
*mod
, unsigned int *size
,
1638 Elf_Shdr
*sechdr
, unsigned int section
)
1642 *size
+= arch_mod_section_prepend(mod
, section
);
1643 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1644 *size
= ret
+ sechdr
->sh_size
;
1648 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1649 might -- code, read-only data, read-write data, small data. Tally
1650 sizes, and place the offsets into sh_entsize fields: high bit means it
1652 static void layout_sections(struct module
*mod
,
1653 const Elf_Ehdr
*hdr
,
1655 const char *secstrings
)
1657 static unsigned long const masks
[][2] = {
1658 /* NOTE: all executable code must be the first section
1659 * in this array; otherwise modify the text_size
1660 * finder in the two loops below */
1661 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1662 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1663 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1664 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1668 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1669 sechdrs
[i
].sh_entsize
= ~0UL;
1671 DEBUGP("Core section allocation order:\n");
1672 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1673 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1674 Elf_Shdr
*s
= &sechdrs
[i
];
1676 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1677 || (s
->sh_flags
& masks
[m
][1])
1678 || s
->sh_entsize
!= ~0UL
1679 || strstarts(secstrings
+ s
->sh_name
, ".init"))
1681 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1682 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1685 mod
->core_text_size
= mod
->core_size
;
1688 DEBUGP("Init section allocation order:\n");
1689 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1690 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1691 Elf_Shdr
*s
= &sechdrs
[i
];
1693 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1694 || (s
->sh_flags
& masks
[m
][1])
1695 || s
->sh_entsize
!= ~0UL
1696 || !strstarts(secstrings
+ s
->sh_name
, ".init"))
1698 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
1699 | INIT_OFFSET_MASK
);
1700 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1703 mod
->init_text_size
= mod
->init_size
;
1707 static void set_license(struct module
*mod
, const char *license
)
1710 license
= "unspecified";
1712 if (!license_is_gpl_compatible(license
)) {
1713 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1714 printk(KERN_WARNING
"%s: module license '%s' taints "
1715 "kernel.\n", mod
->name
, license
);
1716 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1720 /* Parse tag=value strings from .modinfo section */
1721 static char *next_string(char *string
, unsigned long *secsize
)
1723 /* Skip non-zero chars */
1726 if ((*secsize
)-- <= 1)
1730 /* Skip any zero padding. */
1731 while (!string
[0]) {
1733 if ((*secsize
)-- <= 1)
1739 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1744 unsigned int taglen
= strlen(tag
);
1745 unsigned long size
= sechdrs
[info
].sh_size
;
1747 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1748 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1749 return p
+ taglen
+ 1;
1754 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1755 unsigned int infoindex
)
1757 struct module_attribute
*attr
;
1760 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1763 get_modinfo(sechdrs
,
1769 #ifdef CONFIG_KALLSYMS
1771 /* lookup symbol in given range of kernel_symbols */
1772 static const struct kernel_symbol
*lookup_symbol(const char *name
,
1773 const struct kernel_symbol
*start
,
1774 const struct kernel_symbol
*stop
)
1776 const struct kernel_symbol
*ks
= start
;
1777 for (; ks
< stop
; ks
++)
1778 if (strcmp(ks
->name
, name
) == 0)
1783 static int is_exported(const char *name
, unsigned long value
,
1784 const struct module
*mod
)
1786 const struct kernel_symbol
*ks
;
1788 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
1790 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
1791 return ks
!= NULL
&& ks
->value
== value
;
1795 static char elf_type(const Elf_Sym
*sym
,
1797 const char *secstrings
,
1800 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1801 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1806 if (sym
->st_shndx
== SHN_UNDEF
)
1808 if (sym
->st_shndx
== SHN_ABS
)
1810 if (sym
->st_shndx
>= SHN_LORESERVE
)
1812 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1814 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1815 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1816 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1818 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1823 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1824 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1829 if (strstarts(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
, ".debug"))
1834 static void add_kallsyms(struct module
*mod
,
1836 unsigned int symindex
,
1837 unsigned int strindex
,
1838 const char *secstrings
)
1842 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1843 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1844 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1846 /* Set types up while we still have access to sections. */
1847 for (i
= 0; i
< mod
->num_symtab
; i
++)
1848 mod
->symtab
[i
].st_info
1849 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1852 static inline void add_kallsyms(struct module
*mod
,
1854 unsigned int symindex
,
1855 unsigned int strindex
,
1856 const char *secstrings
)
1859 #endif /* CONFIG_KALLSYMS */
1861 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
1863 #ifdef CONFIG_DYNAMIC_DEBUG
1864 if (ddebug_add_module(debug
, num
, debug
->modname
))
1865 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
1870 static void *module_alloc_update_bounds(unsigned long size
)
1872 void *ret
= module_alloc(size
);
1875 /* Update module bounds. */
1876 if ((unsigned long)ret
< module_addr_min
)
1877 module_addr_min
= (unsigned long)ret
;
1878 if ((unsigned long)ret
+ size
> module_addr_max
)
1879 module_addr_max
= (unsigned long)ret
+ size
;
1884 /* Allocate and load the module: note that size of section 0 is always
1885 zero, and we rely on this for optional sections. */
1886 static noinline
struct module
*load_module(void __user
*umod
,
1888 const char __user
*uargs
)
1892 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
1895 unsigned int symindex
= 0;
1896 unsigned int strindex
= 0;
1897 unsigned int modindex
, versindex
, infoindex
, pcpuindex
;
1898 unsigned int num_mcount
;
1901 void *percpu
= NULL
, *ptr
= NULL
; /* Stops spurious gcc warning */
1902 unsigned long *mseg
;
1903 mm_segment_t old_fs
;
1905 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1907 if (len
< sizeof(*hdr
))
1908 return ERR_PTR(-ENOEXEC
);
1910 /* Suck in entire file: we'll want most of it. */
1911 /* vmalloc barfs on "unusual" numbers. Check here */
1912 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
1913 return ERR_PTR(-ENOMEM
);
1915 if (copy_from_user(hdr
, umod
, len
) != 0) {
1920 /* Sanity checks against insmoding binaries or wrong arch,
1921 weird elf version */
1922 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
1923 || hdr
->e_type
!= ET_REL
1924 || !elf_check_arch(hdr
)
1925 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
1930 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
1933 /* Convenience variables */
1934 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
1935 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
1936 sechdrs
[0].sh_addr
= 0;
1938 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1939 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
1940 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
1943 /* Mark all sections sh_addr with their address in the
1945 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
1947 /* Internal symbols and strings. */
1948 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
1950 strindex
= sechdrs
[i
].sh_link
;
1951 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
1953 #ifndef CONFIG_MODULE_UNLOAD
1954 /* Don't load .exit sections */
1955 if (strstarts(secstrings
+sechdrs
[i
].sh_name
, ".exit"))
1956 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1960 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
1961 ".gnu.linkonce.this_module");
1963 printk(KERN_WARNING
"No module found in object\n");
1967 /* This is temporary: point mod into copy of data. */
1968 mod
= (void *)sechdrs
[modindex
].sh_addr
;
1970 if (symindex
== 0) {
1971 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
1977 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
1978 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
1979 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
1981 /* Don't keep modinfo and version sections. */
1982 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1983 sechdrs
[versindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
1984 #ifdef CONFIG_KALLSYMS
1985 /* Keep symbol and string tables for decoding later. */
1986 sechdrs
[symindex
].sh_flags
|= SHF_ALLOC
;
1987 sechdrs
[strindex
].sh_flags
|= SHF_ALLOC
;
1990 /* Check module struct version now, before we try to use module. */
1991 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
1996 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
1997 /* This is allowed: modprobe --force will invalidate it. */
1999 err
= try_to_force_load(mod
, "bad vermagic");
2002 } else if (!same_magic(modmagic
, vermagic
, versindex
)) {
2003 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2004 mod
->name
, modmagic
, vermagic
);
2009 staging
= get_modinfo(sechdrs
, infoindex
, "staging");
2011 add_taint_module(mod
, TAINT_CRAP
);
2012 printk(KERN_WARNING
"%s: module is from the staging directory,"
2013 " the quality is unknown, you have been warned.\n",
2017 /* Now copy in args */
2018 args
= strndup_user(uargs
, ~0UL >> 1);
2020 err
= PTR_ERR(args
);
2024 if (find_module(mod
->name
)) {
2029 mod
->state
= MODULE_STATE_COMING
;
2031 /* Allow arches to frob section contents and sizes. */
2032 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
2037 /* We have a special allocation for this section. */
2038 percpu
= percpu_modalloc(sechdrs
[pcpuindex
].sh_size
,
2039 sechdrs
[pcpuindex
].sh_addralign
,
2045 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2046 mod
->percpu
= percpu
;
2049 /* Determine total sizes, and put offsets in sh_entsize. For now
2050 this is done generically; there doesn't appear to be any
2051 special cases for the architectures. */
2052 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
2054 /* Do the allocs. */
2055 ptr
= module_alloc_update_bounds(mod
->core_size
);
2060 memset(ptr
, 0, mod
->core_size
);
2061 mod
->module_core
= ptr
;
2063 ptr
= module_alloc_update_bounds(mod
->init_size
);
2064 if (!ptr
&& mod
->init_size
) {
2068 memset(ptr
, 0, mod
->init_size
);
2069 mod
->module_init
= ptr
;
2071 /* Transfer each section which specifies SHF_ALLOC */
2072 DEBUGP("final section addresses:\n");
2073 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
2076 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2079 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
2080 dest
= mod
->module_init
2081 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
2083 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
2085 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
2086 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
2087 sechdrs
[i
].sh_size
);
2088 /* Update sh_addr to point to copy in image. */
2089 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
2090 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
2092 /* Module has been moved. */
2093 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2095 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2096 mod
->refptr
= percpu_modalloc(sizeof(local_t
), __alignof__(local_t
),
2103 /* Now we've moved module, initialize linked lists, etc. */
2104 module_unload_init(mod
);
2106 /* add kobject, so we can reference it. */
2107 err
= mod_sysfs_init(mod
);
2111 /* Set up license info based on the info section */
2112 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
2115 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2116 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2117 * using GPL-only symbols it needs.
2119 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2120 add_taint(TAINT_PROPRIETARY_MODULE
);
2122 /* driverloader was caught wrongly pretending to be under GPL */
2123 if (strcmp(mod
->name
, "driverloader") == 0)
2124 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2126 /* Set up MODINFO_ATTR fields */
2127 setup_modinfo(mod
, sechdrs
, infoindex
);
2129 /* Fix up syms, so that st_value is a pointer to location. */
2130 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
2135 /* Now we've got everything in the final locations, we can
2136 * find optional sections. */
2137 mod
->kp
= section_objs(hdr
, sechdrs
, secstrings
, "__param",
2138 sizeof(*mod
->kp
), &mod
->num_kp
);
2139 mod
->syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab",
2140 sizeof(*mod
->syms
), &mod
->num_syms
);
2141 mod
->crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab");
2142 mod
->gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl",
2143 sizeof(*mod
->gpl_syms
),
2144 &mod
->num_gpl_syms
);
2145 mod
->gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
2146 mod
->gpl_future_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2147 "__ksymtab_gpl_future",
2148 sizeof(*mod
->gpl_future_syms
),
2149 &mod
->num_gpl_future_syms
);
2150 mod
->gpl_future_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2151 "__kcrctab_gpl_future");
2153 #ifdef CONFIG_UNUSED_SYMBOLS
2154 mod
->unused_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2156 sizeof(*mod
->unused_syms
),
2157 &mod
->num_unused_syms
);
2158 mod
->unused_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2159 "__kcrctab_unused");
2160 mod
->unused_gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2161 "__ksymtab_unused_gpl",
2162 sizeof(*mod
->unused_gpl_syms
),
2163 &mod
->num_unused_gpl_syms
);
2164 mod
->unused_gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2165 "__kcrctab_unused_gpl");
2168 #ifdef CONFIG_MARKERS
2169 mod
->markers
= section_objs(hdr
, sechdrs
, secstrings
, "__markers",
2170 sizeof(*mod
->markers
), &mod
->num_markers
);
2172 #ifdef CONFIG_TRACEPOINTS
2173 mod
->tracepoints
= section_objs(hdr
, sechdrs
, secstrings
,
2175 sizeof(*mod
->tracepoints
),
2176 &mod
->num_tracepoints
);
2179 #ifdef CONFIG_MODVERSIONS
2180 if ((mod
->num_syms
&& !mod
->crcs
)
2181 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2182 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2183 #ifdef CONFIG_UNUSED_SYMBOLS
2184 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2185 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2188 err
= try_to_force_load(mod
,
2189 "no versions for exported symbols");
2195 /* Now do relocations. */
2196 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2197 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
2198 unsigned int info
= sechdrs
[i
].sh_info
;
2200 /* Not a valid relocation section? */
2201 if (info
>= hdr
->e_shnum
)
2204 /* Don't bother with non-allocated sections */
2205 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
2208 if (sechdrs
[i
].sh_type
== SHT_REL
)
2209 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
2210 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
2211 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
2217 /* Find duplicate symbols */
2218 err
= verify_export_symbols(mod
);
2222 /* Set up and sort exception table */
2223 mod
->extable
= section_objs(hdr
, sechdrs
, secstrings
, "__ex_table",
2224 sizeof(*mod
->extable
), &mod
->num_exentries
);
2225 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2227 /* Finally, copy percpu area over. */
2228 percpu_modcopy(mod
->percpu
, (void *)sechdrs
[pcpuindex
].sh_addr
,
2229 sechdrs
[pcpuindex
].sh_size
);
2231 add_kallsyms(mod
, sechdrs
, symindex
, strindex
, secstrings
);
2234 struct _ddebug
*debug
;
2235 unsigned int num_debug
;
2237 debug
= section_objs(hdr
, sechdrs
, secstrings
, "__verbose",
2238 sizeof(*debug
), &num_debug
);
2240 dynamic_debug_setup(debug
, num_debug
);
2243 /* sechdrs[0].sh_size is always zero */
2244 mseg
= section_objs(hdr
, sechdrs
, secstrings
, "__mcount_loc",
2245 sizeof(*mseg
), &num_mcount
);
2246 ftrace_init_module(mod
, mseg
, mseg
+ num_mcount
);
2248 err
= module_finalize(hdr
, sechdrs
, mod
);
2252 /* flush the icache in correct context */
2257 * Flush the instruction cache, since we've played with text.
2258 * Do it before processing of module parameters, so the module
2259 * can provide parameter accessor functions of its own.
2261 if (mod
->module_init
)
2262 flush_icache_range((unsigned long)mod
->module_init
,
2263 (unsigned long)mod
->module_init
2265 flush_icache_range((unsigned long)mod
->module_core
,
2266 (unsigned long)mod
->module_core
+ mod
->core_size
);
2271 if (section_addr(hdr
, sechdrs
, secstrings
, "__obsparm"))
2272 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2275 /* Now sew it into the lists so we can get lockdep and oops
2276 * info during argument parsing. Noone should access us, since
2277 * strong_try_module_get() will fail.
2278 * lockdep/oops can run asynchronous, so use the RCU list insertion
2279 * function to insert in a way safe to concurrent readers.
2280 * The mutex protects against concurrent writers.
2282 list_add_rcu(&mod
->list
, &modules
);
2284 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
, NULL
);
2288 err
= mod_sysfs_setup(mod
, mod
->kp
, mod
->num_kp
);
2291 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2292 add_notes_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2294 /* Get rid of temporary copy */
2301 /* Unlink carefully: kallsyms could be walking list. */
2302 list_del_rcu(&mod
->list
);
2303 synchronize_sched();
2304 module_arch_cleanup(mod
);
2306 kobject_del(&mod
->mkobj
.kobj
);
2307 kobject_put(&mod
->mkobj
.kobj
);
2308 ftrace_release(mod
->module_core
, mod
->core_size
);
2310 module_unload_free(mod
);
2311 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2313 percpu_modfree(mod
->refptr
);
2315 module_free(mod
, mod
->module_init
);
2317 module_free(mod
, mod
->module_core
);
2318 /* mod will be freed with core. Don't access it beyond this line! */
2321 percpu_modfree(percpu
);
2326 return ERR_PTR(err
);
2329 printk(KERN_ERR
"Module len %lu truncated\n", len
);
2334 /* This is where the real work happens */
2335 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
2336 unsigned long, len
, const char __user
*, uargs
)
2341 /* Must have permission */
2342 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
2345 /* Only one module load at a time, please */
2346 if (mutex_lock_interruptible(&module_mutex
) != 0)
2349 /* Do all the hard work */
2350 mod
= load_module(umod
, len
, uargs
);
2352 mutex_unlock(&module_mutex
);
2353 return PTR_ERR(mod
);
2356 /* Drop lock so they can recurse */
2357 mutex_unlock(&module_mutex
);
2359 blocking_notifier_call_chain(&module_notify_list
,
2360 MODULE_STATE_COMING
, mod
);
2362 /* Start the module */
2363 if (mod
->init
!= NULL
)
2364 ret
= do_one_initcall(mod
->init
);
2366 /* Init routine failed: abort. Try to protect us from
2367 buggy refcounters. */
2368 mod
->state
= MODULE_STATE_GOING
;
2369 synchronize_sched();
2371 blocking_notifier_call_chain(&module_notify_list
,
2372 MODULE_STATE_GOING
, mod
);
2373 mutex_lock(&module_mutex
);
2375 mutex_unlock(&module_mutex
);
2376 wake_up(&module_wq
);
2380 printk(KERN_WARNING
"%s: '%s'->init suspiciously returned %d, "
2381 "it should follow 0/-E convention\n"
2382 KERN_WARNING
"%s: loading module anyway...\n",
2383 __func__
, mod
->name
, ret
,
2388 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2389 mod
->state
= MODULE_STATE_LIVE
;
2390 wake_up(&module_wq
);
2391 blocking_notifier_call_chain(&module_notify_list
,
2392 MODULE_STATE_LIVE
, mod
);
2394 /* We need to finish all async code before the module init sequence is done */
2395 async_synchronize_full();
2397 mutex_lock(&module_mutex
);
2398 /* Drop initial reference. */
2400 module_free(mod
, mod
->module_init
);
2401 mod
->module_init
= NULL
;
2403 mod
->init_text_size
= 0;
2404 mutex_unlock(&module_mutex
);
2409 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2411 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2414 #ifdef CONFIG_KALLSYMS
2416 * This ignores the intensely annoying "mapping symbols" found
2417 * in ARM ELF files: $a, $t and $d.
2419 static inline int is_arm_mapping_symbol(const char *str
)
2421 return str
[0] == '$' && strchr("atd", str
[1])
2422 && (str
[2] == '\0' || str
[2] == '.');
2425 static const char *get_ksymbol(struct module
*mod
,
2427 unsigned long *size
,
2428 unsigned long *offset
)
2430 unsigned int i
, best
= 0;
2431 unsigned long nextval
;
2433 /* At worse, next value is at end of module */
2434 if (within_module_init(addr
, mod
))
2435 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2437 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2439 /* Scan for closest preceeding symbol, and next symbol. (ELF
2440 starts real symbols at 1). */
2441 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2442 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2445 /* We ignore unnamed symbols: they're uninformative
2446 * and inserted at a whim. */
2447 if (mod
->symtab
[i
].st_value
<= addr
2448 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2449 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2450 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2452 if (mod
->symtab
[i
].st_value
> addr
2453 && mod
->symtab
[i
].st_value
< nextval
2454 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2455 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2456 nextval
= mod
->symtab
[i
].st_value
;
2463 *size
= nextval
- mod
->symtab
[best
].st_value
;
2465 *offset
= addr
- mod
->symtab
[best
].st_value
;
2466 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2469 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2470 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2471 const char *module_address_lookup(unsigned long addr
,
2472 unsigned long *size
,
2473 unsigned long *offset
,
2478 const char *ret
= NULL
;
2481 list_for_each_entry_rcu(mod
, &modules
, list
) {
2482 if (within_module_init(addr
, mod
) ||
2483 within_module_core(addr
, mod
)) {
2485 *modname
= mod
->name
;
2486 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2490 /* Make a copy in here where it's safe */
2492 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2499 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2504 list_for_each_entry_rcu(mod
, &modules
, list
) {
2505 if (within_module_init(addr
, mod
) ||
2506 within_module_core(addr
, mod
)) {
2509 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2512 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2522 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2523 unsigned long *offset
, char *modname
, char *name
)
2528 list_for_each_entry_rcu(mod
, &modules
, list
) {
2529 if (within_module_init(addr
, mod
) ||
2530 within_module_core(addr
, mod
)) {
2533 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2537 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2539 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2549 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2550 char *name
, char *module_name
, int *exported
)
2555 list_for_each_entry_rcu(mod
, &modules
, list
) {
2556 if (symnum
< mod
->num_symtab
) {
2557 *value
= mod
->symtab
[symnum
].st_value
;
2558 *type
= mod
->symtab
[symnum
].st_info
;
2559 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2561 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2562 *exported
= is_exported(name
, *value
, mod
);
2566 symnum
-= mod
->num_symtab
;
2572 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2576 for (i
= 0; i
< mod
->num_symtab
; i
++)
2577 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2578 mod
->symtab
[i
].st_info
!= 'U')
2579 return mod
->symtab
[i
].st_value
;
2583 /* Look for this name: can be of form module:name. */
2584 unsigned long module_kallsyms_lookup_name(const char *name
)
2588 unsigned long ret
= 0;
2590 /* Don't lock: we're in enough trouble already. */
2592 if ((colon
= strchr(name
, ':')) != NULL
) {
2594 if ((mod
= find_module(name
)) != NULL
)
2595 ret
= mod_find_symname(mod
, colon
+1);
2598 list_for_each_entry_rcu(mod
, &modules
, list
)
2599 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2606 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
2607 struct module
*, unsigned long),
2614 list_for_each_entry(mod
, &modules
, list
) {
2615 for (i
= 0; i
< mod
->num_symtab
; i
++) {
2616 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
2617 mod
, mod
->symtab
[i
].st_value
);
2624 #endif /* CONFIG_KALLSYMS */
2626 static char *module_flags(struct module
*mod
, char *buf
)
2631 mod
->state
== MODULE_STATE_GOING
||
2632 mod
->state
== MODULE_STATE_COMING
) {
2634 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
2636 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
2638 if (mod
->taints
& (1 << TAINT_CRAP
))
2641 * TAINT_FORCED_RMMOD: could be added.
2642 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2646 /* Show a - for module-is-being-unloaded */
2647 if (mod
->state
== MODULE_STATE_GOING
)
2649 /* Show a + for module-is-being-loaded */
2650 if (mod
->state
== MODULE_STATE_COMING
)
2659 #ifdef CONFIG_PROC_FS
2660 /* Called by the /proc file system to return a list of modules. */
2661 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2663 mutex_lock(&module_mutex
);
2664 return seq_list_start(&modules
, *pos
);
2667 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2669 return seq_list_next(p
, &modules
, pos
);
2672 static void m_stop(struct seq_file
*m
, void *p
)
2674 mutex_unlock(&module_mutex
);
2677 static int m_show(struct seq_file
*m
, void *p
)
2679 struct module
*mod
= list_entry(p
, struct module
, list
);
2682 seq_printf(m
, "%s %u",
2683 mod
->name
, mod
->init_size
+ mod
->core_size
);
2684 print_unload_info(m
, mod
);
2686 /* Informative for users. */
2687 seq_printf(m
, " %s",
2688 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2689 mod
->state
== MODULE_STATE_COMING
? "Loading":
2691 /* Used by oprofile and other similar tools. */
2692 seq_printf(m
, " 0x%p", mod
->module_core
);
2696 seq_printf(m
, " %s", module_flags(mod
, buf
));
2698 seq_printf(m
, "\n");
2702 /* Format: modulename size refcount deps address
2704 Where refcount is a number or -, and deps is a comma-separated list
2707 static const struct seq_operations modules_op
= {
2714 static int modules_open(struct inode
*inode
, struct file
*file
)
2716 return seq_open(file
, &modules_op
);
2719 static const struct file_operations proc_modules_operations
= {
2720 .open
= modules_open
,
2722 .llseek
= seq_lseek
,
2723 .release
= seq_release
,
2726 static int __init
proc_modules_init(void)
2728 proc_create("modules", 0, NULL
, &proc_modules_operations
);
2731 module_init(proc_modules_init
);
2734 /* Given an address, look for it in the module exception tables. */
2735 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2737 const struct exception_table_entry
*e
= NULL
;
2741 list_for_each_entry_rcu(mod
, &modules
, list
) {
2742 if (mod
->num_exentries
== 0)
2745 e
= search_extable(mod
->extable
,
2746 mod
->extable
+ mod
->num_exentries
- 1,
2753 /* Now, if we found one, we are running inside it now, hence
2754 we cannot unload the module, hence no refcnt needed. */
2759 * is_module_address - is this address inside a module?
2760 * @addr: the address to check.
2762 * See is_module_text_address() if you simply want to see if the address
2763 * is code (not data).
2765 bool is_module_address(unsigned long addr
)
2770 ret
= __module_address(addr
) != NULL
;
2777 * __module_address - get the module which contains an address.
2778 * @addr: the address.
2780 * Must be called with preempt disabled or module mutex held so that
2781 * module doesn't get freed during this.
2783 struct module
*__module_address(unsigned long addr
)
2787 if (addr
< module_addr_min
|| addr
> module_addr_max
)
2790 list_for_each_entry_rcu(mod
, &modules
, list
)
2791 if (within_module_core(addr
, mod
)
2792 || within_module_init(addr
, mod
))
2796 EXPORT_SYMBOL_GPL(__module_address
);
2799 * is_module_text_address - is this address inside module code?
2800 * @addr: the address to check.
2802 * See is_module_address() if you simply want to see if the address is
2803 * anywhere in a module. See kernel_text_address() for testing if an
2804 * address corresponds to kernel or module code.
2806 bool is_module_text_address(unsigned long addr
)
2811 ret
= __module_text_address(addr
) != NULL
;
2818 * __module_text_address - get the module whose code contains an address.
2819 * @addr: the address.
2821 * Must be called with preempt disabled or module mutex held so that
2822 * module doesn't get freed during this.
2824 struct module
*__module_text_address(unsigned long addr
)
2826 struct module
*mod
= __module_address(addr
);
2828 /* Make sure it's within the text section. */
2829 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
2830 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
2835 EXPORT_SYMBOL_GPL(__module_text_address
);
2837 /* Don't grab lock, we're oopsing. */
2838 void print_modules(void)
2843 printk("Modules linked in:");
2844 /* Most callers should already have preempt disabled, but make sure */
2846 list_for_each_entry_rcu(mod
, &modules
, list
)
2847 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
2849 if (last_unloaded_module
[0])
2850 printk(" [last unloaded: %s]", last_unloaded_module
);
2854 #ifdef CONFIG_MODVERSIONS
2855 /* Generate the signature for all relevant module structures here.
2856 * If these change, we don't want to try to parse the module. */
2857 void module_layout(struct module
*mod
,
2858 struct modversion_info
*ver
,
2859 struct kernel_param
*kp
,
2860 struct kernel_symbol
*ks
,
2861 struct marker
*marker
,
2862 struct tracepoint
*tp
)
2865 EXPORT_SYMBOL(module_layout
);
2868 #ifdef CONFIG_MARKERS
2869 void module_update_markers(void)
2873 mutex_lock(&module_mutex
);
2874 list_for_each_entry(mod
, &modules
, list
)
2876 marker_update_probe_range(mod
->markers
,
2877 mod
->markers
+ mod
->num_markers
);
2878 mutex_unlock(&module_mutex
);
2882 #ifdef CONFIG_TRACEPOINTS
2883 void module_update_tracepoints(void)
2887 mutex_lock(&module_mutex
);
2888 list_for_each_entry(mod
, &modules
, list
)
2890 tracepoint_update_probe_range(mod
->tracepoints
,
2891 mod
->tracepoints
+ mod
->num_tracepoints
);
2892 mutex_unlock(&module_mutex
);
2896 * Returns 0 if current not found.
2897 * Returns 1 if current found.
2899 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
2901 struct module
*iter_mod
;
2904 mutex_lock(&module_mutex
);
2905 list_for_each_entry(iter_mod
, &modules
, list
) {
2906 if (!iter_mod
->taints
) {
2908 * Sorted module list
2910 if (iter_mod
< iter
->module
)
2912 else if (iter_mod
> iter
->module
)
2913 iter
->tracepoint
= NULL
;
2914 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
2915 iter_mod
->tracepoints
,
2916 iter_mod
->tracepoints
2917 + iter_mod
->num_tracepoints
);
2919 iter
->module
= iter_mod
;
2924 mutex_unlock(&module_mutex
);