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/ftrace_event.h>
22 #include <linux/init.h>
23 #include <linux/kallsyms.h>
25 #include <linux/sysfs.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/elf.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/syscalls.h>
33 #include <linux/fcntl.h>
34 #include <linux/rcupdate.h>
35 #include <linux/capability.h>
36 #include <linux/cpu.h>
37 #include <linux/moduleparam.h>
38 #include <linux/errno.h>
39 #include <linux/err.h>
40 #include <linux/vermagic.h>
41 #include <linux/notifier.h>
42 #include <linux/sched.h>
43 #include <linux/stop_machine.h>
44 #include <linux/device.h>
45 #include <linux/string.h>
46 #include <linux/mutex.h>
47 #include <linux/rculist.h>
48 #include <asm/uaccess.h>
49 #include <asm/cacheflush.h>
50 #include <asm/mmu_context.h>
51 #include <linux/license.h>
52 #include <asm/sections.h>
53 #include <linux/tracepoint.h>
54 #include <linux/ftrace.h>
55 #include <linux/async.h>
56 #include <linux/percpu.h>
57 #include <linux/kmemleak.h>
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/module.h>
65 #define DEBUGP(fmt , a...)
68 #ifndef ARCH_SHF_SMALL
69 #define ARCH_SHF_SMALL 0
72 /* If this is set, the section belongs in the init part of the module */
73 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
75 /* List of modules, protected by module_mutex or preempt_disable
76 * (delete uses stop_machine/add uses RCU list operations). */
77 DEFINE_MUTEX(module_mutex
);
78 EXPORT_SYMBOL_GPL(module_mutex
);
79 static LIST_HEAD(modules
);
80 #ifdef CONFIG_KGDB_KDB
81 struct list_head
*kdb_modules
= &modules
; /* kdb needs the list of modules */
82 #endif /* CONFIG_KGDB_KDB */
85 /* Block module loading/unloading? */
86 int modules_disabled
= 0;
88 /* Waiting for a module to finish initializing? */
89 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
91 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
93 /* Bounds of module allocation, for speeding __module_address */
94 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
96 int register_module_notifier(struct notifier_block
* nb
)
98 return blocking_notifier_chain_register(&module_notify_list
, nb
);
100 EXPORT_SYMBOL(register_module_notifier
);
102 int unregister_module_notifier(struct notifier_block
* nb
)
104 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
106 EXPORT_SYMBOL(unregister_module_notifier
);
108 /* We require a truly strong try_module_get(): 0 means failure due to
109 ongoing or failed initialization etc. */
110 static inline int strong_try_module_get(struct module
*mod
)
112 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
114 if (try_module_get(mod
))
120 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
123 mod
->taints
|= (1U << flag
);
127 * A thread that wants to hold a reference to a module only while it
128 * is running can call this to safely exit. nfsd and lockd use this.
130 void __module_put_and_exit(struct module
*mod
, long code
)
135 EXPORT_SYMBOL(__module_put_and_exit
);
137 /* Find a module section: 0 means not found. */
138 static unsigned int find_sec(Elf_Ehdr
*hdr
,
140 const char *secstrings
,
145 for (i
= 1; i
< hdr
->e_shnum
; i
++)
146 /* Alloc bit cleared means "ignore it." */
147 if ((sechdrs
[i
].sh_flags
& SHF_ALLOC
)
148 && strcmp(secstrings
+sechdrs
[i
].sh_name
, name
) == 0)
153 /* Find a module section, or NULL. */
154 static void *section_addr(Elf_Ehdr
*hdr
, Elf_Shdr
*shdrs
,
155 const char *secstrings
, const char *name
)
157 /* Section 0 has sh_addr 0. */
158 return (void *)shdrs
[find_sec(hdr
, shdrs
, secstrings
, name
)].sh_addr
;
161 /* Find a module section, or NULL. Fill in number of "objects" in section. */
162 static void *section_objs(Elf_Ehdr
*hdr
,
164 const char *secstrings
,
169 unsigned int sec
= find_sec(hdr
, sechdrs
, secstrings
, name
);
171 /* Section 0 has sh_addr 0 and sh_size 0. */
172 *num
= sechdrs
[sec
].sh_size
/ object_size
;
173 return (void *)sechdrs
[sec
].sh_addr
;
176 /* Provided by the linker */
177 extern const struct kernel_symbol __start___ksymtab
[];
178 extern const struct kernel_symbol __stop___ksymtab
[];
179 extern const struct kernel_symbol __start___ksymtab_gpl
[];
180 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
181 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
182 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
183 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
184 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
185 extern const unsigned long __start___kcrctab
[];
186 extern const unsigned long __start___kcrctab_gpl
[];
187 extern const unsigned long __start___kcrctab_gpl_future
[];
188 #ifdef CONFIG_UNUSED_SYMBOLS
189 extern const struct kernel_symbol __start___ksymtab_unused
[];
190 extern const struct kernel_symbol __stop___ksymtab_unused
[];
191 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
192 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
193 extern const unsigned long __start___kcrctab_unused
[];
194 extern const unsigned long __start___kcrctab_unused_gpl
[];
197 #ifndef CONFIG_MODVERSIONS
198 #define symversion(base, idx) NULL
200 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
203 static bool each_symbol_in_section(const struct symsearch
*arr
,
204 unsigned int arrsize
,
205 struct module
*owner
,
206 bool (*fn
)(const struct symsearch
*syms
,
207 struct module
*owner
,
208 unsigned int symnum
, void *data
),
213 for (j
= 0; j
< arrsize
; j
++) {
214 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
215 if (fn(&arr
[j
], owner
, i
, data
))
222 /* Returns true as soon as fn returns true, otherwise false. */
223 bool each_symbol(bool (*fn
)(const struct symsearch
*arr
, struct module
*owner
,
224 unsigned int symnum
, void *data
), void *data
)
227 const struct symsearch arr
[] = {
228 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
229 NOT_GPL_ONLY
, false },
230 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
231 __start___kcrctab_gpl
,
233 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
234 __start___kcrctab_gpl_future
,
235 WILL_BE_GPL_ONLY
, false },
236 #ifdef CONFIG_UNUSED_SYMBOLS
237 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
238 __start___kcrctab_unused
,
239 NOT_GPL_ONLY
, true },
240 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
241 __start___kcrctab_unused_gpl
,
246 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
249 list_for_each_entry_rcu(mod
, &modules
, list
) {
250 struct symsearch arr
[] = {
251 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
252 NOT_GPL_ONLY
, false },
253 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
256 { mod
->gpl_future_syms
,
257 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
258 mod
->gpl_future_crcs
,
259 WILL_BE_GPL_ONLY
, false },
260 #ifdef CONFIG_UNUSED_SYMBOLS
262 mod
->unused_syms
+ mod
->num_unused_syms
,
264 NOT_GPL_ONLY
, true },
265 { mod
->unused_gpl_syms
,
266 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
267 mod
->unused_gpl_crcs
,
272 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
277 EXPORT_SYMBOL_GPL(each_symbol
);
279 struct find_symbol_arg
{
286 struct module
*owner
;
287 const unsigned long *crc
;
288 const struct kernel_symbol
*sym
;
291 static bool find_symbol_in_section(const struct symsearch
*syms
,
292 struct module
*owner
,
293 unsigned int symnum
, void *data
)
295 struct find_symbol_arg
*fsa
= data
;
297 if (strcmp(syms
->start
[symnum
].name
, fsa
->name
) != 0)
301 if (syms
->licence
== GPL_ONLY
)
303 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
304 printk(KERN_WARNING
"Symbol %s is being used "
305 "by a non-GPL module, which will not "
306 "be allowed in the future\n", fsa
->name
);
307 printk(KERN_WARNING
"Please see the file "
308 "Documentation/feature-removal-schedule.txt "
309 "in the kernel source tree for more details.\n");
313 #ifdef CONFIG_UNUSED_SYMBOLS
314 if (syms
->unused
&& fsa
->warn
) {
315 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
316 "however this module is using it.\n", fsa
->name
);
318 "This symbol will go away in the future.\n");
320 "Please evalute if this is the right api to use and if "
321 "it really is, submit a report the linux kernel "
322 "mailinglist together with submitting your code for "
328 fsa
->crc
= symversion(syms
->crcs
, symnum
);
329 fsa
->sym
= &syms
->start
[symnum
];
333 /* Find a symbol and return it, along with, (optional) crc and
334 * (optional) module which owns it */
335 const struct kernel_symbol
*find_symbol(const char *name
,
336 struct module
**owner
,
337 const unsigned long **crc
,
341 struct find_symbol_arg fsa
;
347 if (each_symbol(find_symbol_in_section
, &fsa
)) {
355 DEBUGP("Failed to find symbol %s\n", name
);
358 EXPORT_SYMBOL_GPL(find_symbol
);
360 /* Search for module by name: must hold module_mutex. */
361 struct module
*find_module(const char *name
)
365 list_for_each_entry(mod
, &modules
, list
) {
366 if (strcmp(mod
->name
, name
) == 0)
371 EXPORT_SYMBOL_GPL(find_module
);
375 static inline void __percpu
*mod_percpu(struct module
*mod
)
380 static int percpu_modalloc(struct module
*mod
,
381 unsigned long size
, unsigned long align
)
383 if (align
> PAGE_SIZE
) {
384 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
385 mod
->name
, align
, PAGE_SIZE
);
389 mod
->percpu
= __alloc_reserved_percpu(size
, align
);
392 "Could not allocate %lu bytes percpu data\n", size
);
395 mod
->percpu_size
= size
;
399 static void percpu_modfree(struct module
*mod
)
401 free_percpu(mod
->percpu
);
404 static unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
406 const char *secstrings
)
408 return find_sec(hdr
, sechdrs
, secstrings
, ".data.percpu");
411 static void percpu_modcopy(struct module
*mod
,
412 const void *from
, unsigned long size
)
416 for_each_possible_cpu(cpu
)
417 memcpy(per_cpu_ptr(mod
->percpu
, cpu
), from
, size
);
421 * is_module_percpu_address - test whether address is from module static percpu
422 * @addr: address to test
424 * Test whether @addr belongs to module static percpu area.
427 * %true if @addr is from module static percpu area
429 bool is_module_percpu_address(unsigned long addr
)
436 list_for_each_entry_rcu(mod
, &modules
, list
) {
437 if (!mod
->percpu_size
)
439 for_each_possible_cpu(cpu
) {
440 void *start
= per_cpu_ptr(mod
->percpu
, cpu
);
442 if ((void *)addr
>= start
&&
443 (void *)addr
< start
+ mod
->percpu_size
) {
454 #else /* ... !CONFIG_SMP */
456 static inline void __percpu
*mod_percpu(struct module
*mod
)
460 static inline int percpu_modalloc(struct module
*mod
,
461 unsigned long size
, unsigned long align
)
465 static inline void percpu_modfree(struct module
*mod
)
468 static inline unsigned int find_pcpusec(Elf_Ehdr
*hdr
,
470 const char *secstrings
)
474 static inline void percpu_modcopy(struct module
*mod
,
475 const void *from
, unsigned long size
)
477 /* pcpusec should be 0, and size of that section should be 0. */
480 bool is_module_percpu_address(unsigned long addr
)
485 #endif /* CONFIG_SMP */
487 #define MODINFO_ATTR(field) \
488 static void setup_modinfo_##field(struct module *mod, const char *s) \
490 mod->field = kstrdup(s, GFP_KERNEL); \
492 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
493 struct module *mod, char *buffer) \
495 return sprintf(buffer, "%s\n", mod->field); \
497 static int modinfo_##field##_exists(struct module *mod) \
499 return mod->field != NULL; \
501 static void free_modinfo_##field(struct module *mod) \
506 static struct module_attribute modinfo_##field = { \
507 .attr = { .name = __stringify(field), .mode = 0444 }, \
508 .show = show_modinfo_##field, \
509 .setup = setup_modinfo_##field, \
510 .test = modinfo_##field##_exists, \
511 .free = free_modinfo_##field, \
514 MODINFO_ATTR(version
);
515 MODINFO_ATTR(srcversion
);
517 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
519 #ifdef CONFIG_MODULE_UNLOAD
521 EXPORT_TRACEPOINT_SYMBOL(module_get
);
523 /* Init the unload section of the module. */
524 static void module_unload_init(struct module
*mod
)
528 INIT_LIST_HEAD(&mod
->modules_which_use_me
);
529 for_each_possible_cpu(cpu
) {
530 per_cpu_ptr(mod
->refptr
, cpu
)->incs
= 0;
531 per_cpu_ptr(mod
->refptr
, cpu
)->decs
= 0;
534 /* Hold reference count during initialization. */
535 __this_cpu_write(mod
->refptr
->incs
, 1);
536 /* Backwards compatibility macros put refcount during init. */
537 mod
->waiter
= current
;
540 /* modules using other modules */
543 struct list_head list
;
544 struct module
*module_which_uses
;
547 /* Does a already use b? */
548 static int already_uses(struct module
*a
, struct module
*b
)
550 struct module_use
*use
;
552 list_for_each_entry(use
, &b
->modules_which_use_me
, list
) {
553 if (use
->module_which_uses
== a
) {
554 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
558 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
562 /* Module a uses b */
563 int use_module(struct module
*a
, struct module
*b
)
565 struct module_use
*use
;
568 if (b
== NULL
|| already_uses(a
, b
)) return 1;
570 /* If we're interrupted or time out, we fail. */
571 if (wait_event_interruptible_timeout(
572 module_wq
, (err
= strong_try_module_get(b
)) != -EBUSY
,
574 printk("%s: gave up waiting for init of module %s.\n",
579 /* If strong_try_module_get() returned a different error, we fail. */
583 DEBUGP("Allocating new usage for %s.\n", a
->name
);
584 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
586 printk("%s: out of memory loading\n", a
->name
);
591 use
->module_which_uses
= a
;
592 list_add(&use
->list
, &b
->modules_which_use_me
);
593 no_warn
= sysfs_create_link(b
->holders_dir
, &a
->mkobj
.kobj
, a
->name
);
596 EXPORT_SYMBOL_GPL(use_module
);
598 /* Clear the unload stuff of the module. */
599 static void module_unload_free(struct module
*mod
)
603 list_for_each_entry(i
, &modules
, list
) {
604 struct module_use
*use
;
606 list_for_each_entry(use
, &i
->modules_which_use_me
, list
) {
607 if (use
->module_which_uses
== mod
) {
608 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
610 list_del(&use
->list
);
612 sysfs_remove_link(i
->holders_dir
, mod
->name
);
613 /* There can be at most one match. */
620 #ifdef CONFIG_MODULE_FORCE_UNLOAD
621 static inline int try_force_unload(unsigned int flags
)
623 int ret
= (flags
& O_TRUNC
);
625 add_taint(TAINT_FORCED_RMMOD
);
629 static inline int try_force_unload(unsigned int flags
)
633 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
642 /* Whole machine is stopped with interrupts off when this runs. */
643 static int __try_stop_module(void *_sref
)
645 struct stopref
*sref
= _sref
;
647 /* If it's not unused, quit unless we're forcing. */
648 if (module_refcount(sref
->mod
) != 0) {
649 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
653 /* Mark it as dying. */
654 sref
->mod
->state
= MODULE_STATE_GOING
;
658 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
660 if (flags
& O_NONBLOCK
) {
661 struct stopref sref
= { mod
, flags
, forced
};
663 return stop_machine(__try_stop_module
, &sref
, NULL
);
665 /* We don't need to stop the machine for this. */
666 mod
->state
= MODULE_STATE_GOING
;
672 unsigned int module_refcount(struct module
*mod
)
674 unsigned int incs
= 0, decs
= 0;
677 for_each_possible_cpu(cpu
)
678 decs
+= per_cpu_ptr(mod
->refptr
, cpu
)->decs
;
680 * ensure the incs are added up after the decs.
681 * module_put ensures incs are visible before decs with smp_wmb.
683 * This 2-count scheme avoids the situation where the refcount
684 * for CPU0 is read, then CPU0 increments the module refcount,
685 * then CPU1 drops that refcount, then the refcount for CPU1 is
686 * read. We would record a decrement but not its corresponding
687 * increment so we would see a low count (disaster).
689 * Rare situation? But module_refcount can be preempted, and we
690 * might be tallying up 4096+ CPUs. So it is not impossible.
693 for_each_possible_cpu(cpu
)
694 incs
+= per_cpu_ptr(mod
->refptr
, cpu
)->incs
;
697 EXPORT_SYMBOL(module_refcount
);
699 /* This exists whether we can unload or not */
700 static void free_module(struct module
*mod
);
702 static void wait_for_zero_refcount(struct module
*mod
)
704 /* Since we might sleep for some time, release the mutex first */
705 mutex_unlock(&module_mutex
);
707 DEBUGP("Looking at refcount...\n");
708 set_current_state(TASK_UNINTERRUPTIBLE
);
709 if (module_refcount(mod
) == 0)
713 current
->state
= TASK_RUNNING
;
714 mutex_lock(&module_mutex
);
717 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
721 char name
[MODULE_NAME_LEN
];
724 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
727 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
729 name
[MODULE_NAME_LEN
-1] = '\0';
731 if (mutex_lock_interruptible(&module_mutex
) != 0)
734 mod
= find_module(name
);
740 if (!list_empty(&mod
->modules_which_use_me
)) {
741 /* Other modules depend on us: get rid of them first. */
746 /* Doing init or already dying? */
747 if (mod
->state
!= MODULE_STATE_LIVE
) {
748 /* FIXME: if (force), slam module count and wake up
750 DEBUGP("%s already dying\n", mod
->name
);
755 /* If it has an init func, it must have an exit func to unload */
756 if (mod
->init
&& !mod
->exit
) {
757 forced
= try_force_unload(flags
);
759 /* This module can't be removed */
765 /* Set this up before setting mod->state */
766 mod
->waiter
= current
;
768 /* Stop the machine so refcounts can't move and disable module. */
769 ret
= try_stop_module(mod
, flags
, &forced
);
773 /* Never wait if forced. */
774 if (!forced
&& module_refcount(mod
) != 0)
775 wait_for_zero_refcount(mod
);
777 mutex_unlock(&module_mutex
);
778 /* Final destruction now noone is using it. */
779 if (mod
->exit
!= NULL
)
781 blocking_notifier_call_chain(&module_notify_list
,
782 MODULE_STATE_GOING
, mod
);
783 async_synchronize_full();
784 mutex_lock(&module_mutex
);
785 /* Store the name of the last unloaded module for diagnostic purposes */
786 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
787 ddebug_remove_module(mod
->name
);
791 mutex_unlock(&module_mutex
);
795 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
797 struct module_use
*use
;
798 int printed_something
= 0;
800 seq_printf(m
, " %u ", module_refcount(mod
));
802 /* Always include a trailing , so userspace can differentiate
803 between this and the old multi-field proc format. */
804 list_for_each_entry(use
, &mod
->modules_which_use_me
, list
) {
805 printed_something
= 1;
806 seq_printf(m
, "%s,", use
->module_which_uses
->name
);
809 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
810 printed_something
= 1;
811 seq_printf(m
, "[permanent],");
814 if (!printed_something
)
818 void __symbol_put(const char *symbol
)
820 struct module
*owner
;
823 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
828 EXPORT_SYMBOL(__symbol_put
);
830 /* Note this assumes addr is a function, which it currently always is. */
831 void symbol_put_addr(void *addr
)
833 struct module
*modaddr
;
834 unsigned long a
= (unsigned long)dereference_function_descriptor(addr
);
836 if (core_kernel_text(a
))
839 /* module_text_address is safe here: we're supposed to have reference
840 * to module from symbol_get, so it can't go away. */
841 modaddr
= __module_text_address(a
);
845 EXPORT_SYMBOL_GPL(symbol_put_addr
);
847 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
848 struct module
*mod
, char *buffer
)
850 return sprintf(buffer
, "%u\n", module_refcount(mod
));
853 static struct module_attribute refcnt
= {
854 .attr
= { .name
= "refcnt", .mode
= 0444 },
858 void module_put(struct module
*module
)
862 smp_wmb(); /* see comment in module_refcount */
863 __this_cpu_inc(module
->refptr
->decs
);
865 trace_module_put(module
, _RET_IP_
);
866 /* Maybe they're waiting for us to drop reference? */
867 if (unlikely(!module_is_live(module
)))
868 wake_up_process(module
->waiter
);
872 EXPORT_SYMBOL(module_put
);
874 #else /* !CONFIG_MODULE_UNLOAD */
875 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
877 /* We don't know the usage count, or what modules are using. */
878 seq_printf(m
, " - -");
881 static inline void module_unload_free(struct module
*mod
)
885 int use_module(struct module
*a
, struct module
*b
)
887 return strong_try_module_get(b
) == 0;
889 EXPORT_SYMBOL_GPL(use_module
);
891 static inline void module_unload_init(struct module
*mod
)
894 #endif /* CONFIG_MODULE_UNLOAD */
896 static ssize_t
show_initstate(struct module_attribute
*mattr
,
897 struct module
*mod
, char *buffer
)
899 const char *state
= "unknown";
901 switch (mod
->state
) {
902 case MODULE_STATE_LIVE
:
905 case MODULE_STATE_COMING
:
908 case MODULE_STATE_GOING
:
912 return sprintf(buffer
, "%s\n", state
);
915 static struct module_attribute initstate
= {
916 .attr
= { .name
= "initstate", .mode
= 0444 },
917 .show
= show_initstate
,
920 static struct module_attribute
*modinfo_attrs
[] = {
924 #ifdef CONFIG_MODULE_UNLOAD
930 static const char vermagic
[] = VERMAGIC_STRING
;
932 static int try_to_force_load(struct module
*mod
, const char *reason
)
934 #ifdef CONFIG_MODULE_FORCE_LOAD
935 if (!test_taint(TAINT_FORCED_MODULE
))
936 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
938 add_taint_module(mod
, TAINT_FORCED_MODULE
);
945 #ifdef CONFIG_MODVERSIONS
946 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
947 static unsigned long maybe_relocated(unsigned long crc
,
948 const struct module
*crc_owner
)
950 #ifdef ARCH_RELOCATES_KCRCTAB
951 if (crc_owner
== NULL
)
952 return crc
- (unsigned long)reloc_start
;
957 static int check_version(Elf_Shdr
*sechdrs
,
958 unsigned int versindex
,
961 const unsigned long *crc
,
962 const struct module
*crc_owner
)
964 unsigned int i
, num_versions
;
965 struct modversion_info
*versions
;
967 /* Exporting module didn't supply crcs? OK, we're already tainted. */
971 /* No versions at all? modprobe --force does this. */
973 return try_to_force_load(mod
, symname
) == 0;
975 versions
= (void *) sechdrs
[versindex
].sh_addr
;
976 num_versions
= sechdrs
[versindex
].sh_size
977 / sizeof(struct modversion_info
);
979 for (i
= 0; i
< num_versions
; i
++) {
980 if (strcmp(versions
[i
].name
, symname
) != 0)
983 if (versions
[i
].crc
== maybe_relocated(*crc
, crc_owner
))
985 DEBUGP("Found checksum %lX vs module %lX\n",
986 maybe_relocated(*crc
, crc_owner
), versions
[i
].crc
);
990 printk(KERN_WARNING
"%s: no symbol version for %s\n",
995 printk("%s: disagrees about version of symbol %s\n",
1000 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1001 unsigned int versindex
,
1004 const unsigned long *crc
;
1006 if (!find_symbol(MODULE_SYMBOL_PREFIX
"module_layout", NULL
,
1009 return check_version(sechdrs
, versindex
, "module_layout", mod
, crc
,
1013 /* First part is kernel version, which we ignore if module has crcs. */
1014 static inline int same_magic(const char *amagic
, const char *bmagic
,
1018 amagic
+= strcspn(amagic
, " ");
1019 bmagic
+= strcspn(bmagic
, " ");
1021 return strcmp(amagic
, bmagic
) == 0;
1024 static inline int check_version(Elf_Shdr
*sechdrs
,
1025 unsigned int versindex
,
1026 const char *symname
,
1028 const unsigned long *crc
,
1029 const struct module
*crc_owner
)
1034 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1035 unsigned int versindex
,
1041 static inline int same_magic(const char *amagic
, const char *bmagic
,
1044 return strcmp(amagic
, bmagic
) == 0;
1046 #endif /* CONFIG_MODVERSIONS */
1048 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1049 Must be holding module_mutex. */
1050 static const struct kernel_symbol
*resolve_symbol(Elf_Shdr
*sechdrs
,
1051 unsigned int versindex
,
1055 struct module
*owner
;
1056 const struct kernel_symbol
*sym
;
1057 const unsigned long *crc
;
1059 sym
= find_symbol(name
, &owner
, &crc
,
1060 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1061 /* use_module can fail due to OOM,
1062 or module initialization or unloading */
1064 if (!check_version(sechdrs
, versindex
, name
, mod
, crc
, owner
)
1065 || !use_module(mod
, owner
))
1072 * /sys/module/foo/sections stuff
1073 * J. Corbet <corbet@lwn.net>
1075 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1077 static inline bool sect_empty(const Elf_Shdr
*sect
)
1079 return !(sect
->sh_flags
& SHF_ALLOC
) || sect
->sh_size
== 0;
1082 struct module_sect_attr
1084 struct module_attribute mattr
;
1086 unsigned long address
;
1089 struct module_sect_attrs
1091 struct attribute_group grp
;
1092 unsigned int nsections
;
1093 struct module_sect_attr attrs
[0];
1096 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1097 struct module
*mod
, char *buf
)
1099 struct module_sect_attr
*sattr
=
1100 container_of(mattr
, struct module_sect_attr
, mattr
);
1101 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1104 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1106 unsigned int section
;
1108 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1109 kfree(sect_attrs
->attrs
[section
].name
);
1113 static void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1114 char *secstrings
, Elf_Shdr
*sechdrs
)
1116 unsigned int nloaded
= 0, i
, size
[2];
1117 struct module_sect_attrs
*sect_attrs
;
1118 struct module_sect_attr
*sattr
;
1119 struct attribute
**gattr
;
1121 /* Count loaded sections and allocate structures */
1122 for (i
= 0; i
< nsect
; i
++)
1123 if (!sect_empty(&sechdrs
[i
]))
1125 size
[0] = ALIGN(sizeof(*sect_attrs
)
1126 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1127 sizeof(sect_attrs
->grp
.attrs
[0]));
1128 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1129 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1130 if (sect_attrs
== NULL
)
1133 /* Setup section attributes. */
1134 sect_attrs
->grp
.name
= "sections";
1135 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1137 sect_attrs
->nsections
= 0;
1138 sattr
= §_attrs
->attrs
[0];
1139 gattr
= §_attrs
->grp
.attrs
[0];
1140 for (i
= 0; i
< nsect
; i
++) {
1141 if (sect_empty(&sechdrs
[i
]))
1143 sattr
->address
= sechdrs
[i
].sh_addr
;
1144 sattr
->name
= kstrdup(secstrings
+ sechdrs
[i
].sh_name
,
1146 if (sattr
->name
== NULL
)
1148 sect_attrs
->nsections
++;
1149 sysfs_attr_init(&sattr
->mattr
.attr
);
1150 sattr
->mattr
.show
= module_sect_show
;
1151 sattr
->mattr
.store
= NULL
;
1152 sattr
->mattr
.attr
.name
= sattr
->name
;
1153 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1154 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1158 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1161 mod
->sect_attrs
= sect_attrs
;
1164 free_sect_attrs(sect_attrs
);
1167 static void remove_sect_attrs(struct module
*mod
)
1169 if (mod
->sect_attrs
) {
1170 sysfs_remove_group(&mod
->mkobj
.kobj
,
1171 &mod
->sect_attrs
->grp
);
1172 /* We are positive that no one is using any sect attrs
1173 * at this point. Deallocate immediately. */
1174 free_sect_attrs(mod
->sect_attrs
);
1175 mod
->sect_attrs
= NULL
;
1180 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1183 struct module_notes_attrs
{
1184 struct kobject
*dir
;
1186 struct bin_attribute attrs
[0];
1189 static ssize_t
module_notes_read(struct file
*filp
, struct kobject
*kobj
,
1190 struct bin_attribute
*bin_attr
,
1191 char *buf
, loff_t pos
, size_t count
)
1194 * The caller checked the pos and count against our size.
1196 memcpy(buf
, bin_attr
->private + pos
, count
);
1200 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1203 if (notes_attrs
->dir
) {
1205 sysfs_remove_bin_file(notes_attrs
->dir
,
1206 ¬es_attrs
->attrs
[i
]);
1207 kobject_put(notes_attrs
->dir
);
1212 static void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1213 char *secstrings
, Elf_Shdr
*sechdrs
)
1215 unsigned int notes
, loaded
, i
;
1216 struct module_notes_attrs
*notes_attrs
;
1217 struct bin_attribute
*nattr
;
1219 /* failed to create section attributes, so can't create notes */
1220 if (!mod
->sect_attrs
)
1223 /* Count notes sections and allocate structures. */
1225 for (i
= 0; i
< nsect
; i
++)
1226 if (!sect_empty(&sechdrs
[i
]) &&
1227 (sechdrs
[i
].sh_type
== SHT_NOTE
))
1233 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1234 + notes
* sizeof(notes_attrs
->attrs
[0]),
1236 if (notes_attrs
== NULL
)
1239 notes_attrs
->notes
= notes
;
1240 nattr
= ¬es_attrs
->attrs
[0];
1241 for (loaded
= i
= 0; i
< nsect
; ++i
) {
1242 if (sect_empty(&sechdrs
[i
]))
1244 if (sechdrs
[i
].sh_type
== SHT_NOTE
) {
1245 sysfs_bin_attr_init(nattr
);
1246 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1247 nattr
->attr
.mode
= S_IRUGO
;
1248 nattr
->size
= sechdrs
[i
].sh_size
;
1249 nattr
->private = (void *) sechdrs
[i
].sh_addr
;
1250 nattr
->read
= module_notes_read
;
1256 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1257 if (!notes_attrs
->dir
)
1260 for (i
= 0; i
< notes
; ++i
)
1261 if (sysfs_create_bin_file(notes_attrs
->dir
,
1262 ¬es_attrs
->attrs
[i
]))
1265 mod
->notes_attrs
= notes_attrs
;
1269 free_notes_attrs(notes_attrs
, i
);
1272 static void remove_notes_attrs(struct module
*mod
)
1274 if (mod
->notes_attrs
)
1275 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1280 static inline void add_sect_attrs(struct module
*mod
, unsigned int nsect
,
1281 char *sectstrings
, Elf_Shdr
*sechdrs
)
1285 static inline void remove_sect_attrs(struct module
*mod
)
1289 static inline void add_notes_attrs(struct module
*mod
, unsigned int nsect
,
1290 char *sectstrings
, Elf_Shdr
*sechdrs
)
1294 static inline void remove_notes_attrs(struct module
*mod
)
1300 int module_add_modinfo_attrs(struct module
*mod
)
1302 struct module_attribute
*attr
;
1303 struct module_attribute
*temp_attr
;
1307 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1308 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1310 if (!mod
->modinfo_attrs
)
1313 temp_attr
= mod
->modinfo_attrs
;
1314 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1316 (attr
->test
&& attr
->test(mod
))) {
1317 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1318 sysfs_attr_init(&temp_attr
->attr
);
1319 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1326 void module_remove_modinfo_attrs(struct module
*mod
)
1328 struct module_attribute
*attr
;
1331 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1332 /* pick a field to test for end of list */
1333 if (!attr
->attr
.name
)
1335 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1339 kfree(mod
->modinfo_attrs
);
1342 int mod_sysfs_init(struct module
*mod
)
1345 struct kobject
*kobj
;
1347 if (!module_sysfs_initialized
) {
1348 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1354 kobj
= kset_find_obj(module_kset
, mod
->name
);
1356 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1362 mod
->mkobj
.mod
= mod
;
1364 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1365 mod
->mkobj
.kobj
.kset
= module_kset
;
1366 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1369 kobject_put(&mod
->mkobj
.kobj
);
1371 /* delay uevent until full sysfs population */
1376 int mod_sysfs_setup(struct module
*mod
,
1377 struct kernel_param
*kparam
,
1378 unsigned int num_params
)
1382 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1383 if (!mod
->holders_dir
) {
1388 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1390 goto out_unreg_holders
;
1392 err
= module_add_modinfo_attrs(mod
);
1394 goto out_unreg_param
;
1396 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1400 module_param_sysfs_remove(mod
);
1402 kobject_put(mod
->holders_dir
);
1404 kobject_put(&mod
->mkobj
.kobj
);
1408 static void mod_sysfs_fini(struct module
*mod
)
1410 kobject_put(&mod
->mkobj
.kobj
);
1413 #else /* CONFIG_SYSFS */
1415 static void mod_sysfs_fini(struct module
*mod
)
1419 #endif /* CONFIG_SYSFS */
1421 static void mod_kobject_remove(struct module
*mod
)
1423 module_remove_modinfo_attrs(mod
);
1424 module_param_sysfs_remove(mod
);
1425 kobject_put(mod
->mkobj
.drivers_dir
);
1426 kobject_put(mod
->holders_dir
);
1427 mod_sysfs_fini(mod
);
1431 * unlink the module with the whole machine is stopped with interrupts off
1432 * - this defends against kallsyms not taking locks
1434 static int __unlink_module(void *_mod
)
1436 struct module
*mod
= _mod
;
1437 list_del(&mod
->list
);
1441 /* Free a module, remove from lists, etc (must hold module_mutex). */
1442 static void free_module(struct module
*mod
)
1444 trace_module_free(mod
);
1446 /* Delete from various lists */
1447 stop_machine(__unlink_module
, mod
, NULL
);
1448 remove_notes_attrs(mod
);
1449 remove_sect_attrs(mod
);
1450 mod_kobject_remove(mod
);
1452 /* Arch-specific cleanup. */
1453 module_arch_cleanup(mod
);
1455 /* Module unload stuff */
1456 module_unload_free(mod
);
1458 /* Free any allocated parameters. */
1459 destroy_params(mod
->kp
, mod
->num_kp
);
1461 /* This may be NULL, but that's OK */
1462 module_free(mod
, mod
->module_init
);
1464 percpu_modfree(mod
);
1465 #if defined(CONFIG_MODULE_UNLOAD)
1467 free_percpu(mod
->refptr
);
1469 /* Free lock-classes: */
1470 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1472 /* Finally, free the core (containing the module structure) */
1473 module_free(mod
, mod
->module_core
);
1476 update_protections(current
->mm
);
1480 void *__symbol_get(const char *symbol
)
1482 struct module
*owner
;
1483 const struct kernel_symbol
*sym
;
1486 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1487 if (sym
&& strong_try_module_get(owner
))
1491 return sym
? (void *)sym
->value
: NULL
;
1493 EXPORT_SYMBOL_GPL(__symbol_get
);
1496 * Ensure that an exported symbol [global namespace] does not already exist
1497 * in the kernel or in some other module's exported symbol table.
1499 static int verify_export_symbols(struct module
*mod
)
1502 struct module
*owner
;
1503 const struct kernel_symbol
*s
;
1505 const struct kernel_symbol
*sym
;
1508 { mod
->syms
, mod
->num_syms
},
1509 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1510 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1511 #ifdef CONFIG_UNUSED_SYMBOLS
1512 { mod
->unused_syms
, mod
->num_unused_syms
},
1513 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1517 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1518 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1519 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1521 "%s: exports duplicate symbol %s"
1523 mod
->name
, s
->name
, module_name(owner
));
1531 /* Change all symbols so that st_value encodes the pointer directly. */
1532 static int simplify_symbols(Elf_Shdr
*sechdrs
,
1533 unsigned int symindex
,
1535 unsigned int versindex
,
1536 unsigned int pcpuindex
,
1539 Elf_Sym
*sym
= (void *)sechdrs
[symindex
].sh_addr
;
1540 unsigned long secbase
;
1541 unsigned int i
, n
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1543 const struct kernel_symbol
*ksym
;
1545 for (i
= 1; i
< n
; i
++) {
1546 switch (sym
[i
].st_shndx
) {
1548 /* We compiled with -fno-common. These are not
1549 supposed to happen. */
1550 DEBUGP("Common symbol: %s\n", strtab
+ sym
[i
].st_name
);
1551 printk("%s: please compile with -fno-common\n",
1557 /* Don't need to do anything */
1558 DEBUGP("Absolute symbol: 0x%08lx\n",
1559 (long)sym
[i
].st_value
);
1563 ksym
= resolve_symbol(sechdrs
, versindex
,
1564 strtab
+ sym
[i
].st_name
, mod
);
1565 /* Ok if resolved. */
1567 sym
[i
].st_value
= ksym
->value
;
1572 if (ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1575 printk(KERN_WARNING
"%s: Unknown symbol %s\n",
1576 mod
->name
, strtab
+ sym
[i
].st_name
);
1581 /* Divert to percpu allocation if a percpu var. */
1582 if (sym
[i
].st_shndx
== pcpuindex
)
1583 secbase
= (unsigned long)mod_percpu(mod
);
1585 secbase
= sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1586 sym
[i
].st_value
+= secbase
;
1594 /* Additional bytes needed by arch in front of individual sections */
1595 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1596 unsigned int section
)
1598 /* default implementation just returns zero */
1602 /* Update size with this section: return offset. */
1603 static long get_offset(struct module
*mod
, unsigned int *size
,
1604 Elf_Shdr
*sechdr
, unsigned int section
)
1608 *size
+= arch_mod_section_prepend(mod
, section
);
1609 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1610 *size
= ret
+ sechdr
->sh_size
;
1614 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1615 might -- code, read-only data, read-write data, small data. Tally
1616 sizes, and place the offsets into sh_entsize fields: high bit means it
1618 static void layout_sections(struct module
*mod
,
1619 const Elf_Ehdr
*hdr
,
1621 const char *secstrings
)
1623 static unsigned long const masks
[][2] = {
1624 /* NOTE: all executable code must be the first section
1625 * in this array; otherwise modify the text_size
1626 * finder in the two loops below */
1627 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1628 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1629 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1630 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1634 for (i
= 0; i
< hdr
->e_shnum
; i
++)
1635 sechdrs
[i
].sh_entsize
= ~0UL;
1637 DEBUGP("Core section allocation order:\n");
1638 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1639 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1640 Elf_Shdr
*s
= &sechdrs
[i
];
1642 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1643 || (s
->sh_flags
& masks
[m
][1])
1644 || s
->sh_entsize
!= ~0UL
1645 || strstarts(secstrings
+ s
->sh_name
, ".init"))
1647 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1648 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1651 mod
->core_text_size
= mod
->core_size
;
1654 DEBUGP("Init section allocation order:\n");
1655 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1656 for (i
= 0; i
< hdr
->e_shnum
; ++i
) {
1657 Elf_Shdr
*s
= &sechdrs
[i
];
1659 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1660 || (s
->sh_flags
& masks
[m
][1])
1661 || s
->sh_entsize
!= ~0UL
1662 || !strstarts(secstrings
+ s
->sh_name
, ".init"))
1664 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
1665 | INIT_OFFSET_MASK
);
1666 DEBUGP("\t%s\n", secstrings
+ s
->sh_name
);
1669 mod
->init_text_size
= mod
->init_size
;
1673 static void set_license(struct module
*mod
, const char *license
)
1676 license
= "unspecified";
1678 if (!license_is_gpl_compatible(license
)) {
1679 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1680 printk(KERN_WARNING
"%s: module license '%s' taints "
1681 "kernel.\n", mod
->name
, license
);
1682 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1686 /* Parse tag=value strings from .modinfo section */
1687 static char *next_string(char *string
, unsigned long *secsize
)
1689 /* Skip non-zero chars */
1692 if ((*secsize
)-- <= 1)
1696 /* Skip any zero padding. */
1697 while (!string
[0]) {
1699 if ((*secsize
)-- <= 1)
1705 static char *get_modinfo(Elf_Shdr
*sechdrs
,
1710 unsigned int taglen
= strlen(tag
);
1711 unsigned long size
= sechdrs
[info
].sh_size
;
1713 for (p
= (char *)sechdrs
[info
].sh_addr
; p
; p
= next_string(p
, &size
)) {
1714 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1715 return p
+ taglen
+ 1;
1720 static void setup_modinfo(struct module
*mod
, Elf_Shdr
*sechdrs
,
1721 unsigned int infoindex
)
1723 struct module_attribute
*attr
;
1726 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1729 get_modinfo(sechdrs
,
1735 static void free_modinfo(struct module
*mod
)
1737 struct module_attribute
*attr
;
1740 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1746 #ifdef CONFIG_KALLSYMS
1748 /* lookup symbol in given range of kernel_symbols */
1749 static const struct kernel_symbol
*lookup_symbol(const char *name
,
1750 const struct kernel_symbol
*start
,
1751 const struct kernel_symbol
*stop
)
1753 const struct kernel_symbol
*ks
= start
;
1754 for (; ks
< stop
; ks
++)
1755 if (strcmp(ks
->name
, name
) == 0)
1760 static int is_exported(const char *name
, unsigned long value
,
1761 const struct module
*mod
)
1763 const struct kernel_symbol
*ks
;
1765 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
1767 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
1768 return ks
!= NULL
&& ks
->value
== value
;
1772 static char elf_type(const Elf_Sym
*sym
,
1774 const char *secstrings
,
1777 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1778 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1783 if (sym
->st_shndx
== SHN_UNDEF
)
1785 if (sym
->st_shndx
== SHN_ABS
)
1787 if (sym
->st_shndx
>= SHN_LORESERVE
)
1789 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1791 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1792 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1793 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1795 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1800 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1801 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1806 if (strstarts(secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
, ".debug"))
1811 static bool is_core_symbol(const Elf_Sym
*src
, const Elf_Shdr
*sechdrs
,
1814 const Elf_Shdr
*sec
;
1816 if (src
->st_shndx
== SHN_UNDEF
1817 || src
->st_shndx
>= shnum
1821 sec
= sechdrs
+ src
->st_shndx
;
1822 if (!(sec
->sh_flags
& SHF_ALLOC
)
1823 #ifndef CONFIG_KALLSYMS_ALL
1824 || !(sec
->sh_flags
& SHF_EXECINSTR
)
1826 || (sec
->sh_entsize
& INIT_OFFSET_MASK
))
1832 static unsigned long layout_symtab(struct module
*mod
,
1834 unsigned int symindex
,
1835 unsigned int strindex
,
1836 const Elf_Ehdr
*hdr
,
1837 const char *secstrings
,
1838 unsigned long *pstroffs
,
1839 unsigned long *strmap
)
1841 unsigned long symoffs
;
1842 Elf_Shdr
*symsect
= sechdrs
+ symindex
;
1843 Elf_Shdr
*strsect
= sechdrs
+ strindex
;
1846 unsigned int i
, nsrc
, ndst
;
1848 /* Put symbol section at end of init part of module. */
1849 symsect
->sh_flags
|= SHF_ALLOC
;
1850 symsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, symsect
,
1851 symindex
) | INIT_OFFSET_MASK
;
1852 DEBUGP("\t%s\n", secstrings
+ symsect
->sh_name
);
1854 src
= (void *)hdr
+ symsect
->sh_offset
;
1855 nsrc
= symsect
->sh_size
/ sizeof(*src
);
1856 strtab
= (void *)hdr
+ strsect
->sh_offset
;
1857 for (ndst
= i
= 1; i
< nsrc
; ++i
, ++src
)
1858 if (is_core_symbol(src
, sechdrs
, hdr
->e_shnum
)) {
1859 unsigned int j
= src
->st_name
;
1861 while(!__test_and_set_bit(j
, strmap
) && strtab
[j
])
1866 /* Append room for core symbols at end of core part. */
1867 symoffs
= ALIGN(mod
->core_size
, symsect
->sh_addralign
?: 1);
1868 mod
->core_size
= symoffs
+ ndst
* sizeof(Elf_Sym
);
1870 /* Put string table section at end of init part of module. */
1871 strsect
->sh_flags
|= SHF_ALLOC
;
1872 strsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, strsect
,
1873 strindex
) | INIT_OFFSET_MASK
;
1874 DEBUGP("\t%s\n", secstrings
+ strsect
->sh_name
);
1876 /* Append room for core symbols' strings at end of core part. */
1877 *pstroffs
= mod
->core_size
;
1878 __set_bit(0, strmap
);
1879 mod
->core_size
+= bitmap_weight(strmap
, strsect
->sh_size
);
1884 static void add_kallsyms(struct module
*mod
,
1887 unsigned int symindex
,
1888 unsigned int strindex
,
1889 unsigned long symoffs
,
1890 unsigned long stroffs
,
1891 const char *secstrings
,
1892 unsigned long *strmap
)
1894 unsigned int i
, ndst
;
1899 mod
->symtab
= (void *)sechdrs
[symindex
].sh_addr
;
1900 mod
->num_symtab
= sechdrs
[symindex
].sh_size
/ sizeof(Elf_Sym
);
1901 mod
->strtab
= (void *)sechdrs
[strindex
].sh_addr
;
1903 /* Set types up while we still have access to sections. */
1904 for (i
= 0; i
< mod
->num_symtab
; i
++)
1905 mod
->symtab
[i
].st_info
1906 = elf_type(&mod
->symtab
[i
], sechdrs
, secstrings
, mod
);
1908 mod
->core_symtab
= dst
= mod
->module_core
+ symoffs
;
1911 for (ndst
= i
= 1; i
< mod
->num_symtab
; ++i
, ++src
) {
1912 if (!is_core_symbol(src
, sechdrs
, shnum
))
1915 dst
[ndst
].st_name
= bitmap_weight(strmap
, dst
[ndst
].st_name
);
1918 mod
->core_num_syms
= ndst
;
1920 mod
->core_strtab
= s
= mod
->module_core
+ stroffs
;
1921 for (*s
= 0, i
= 1; i
< sechdrs
[strindex
].sh_size
; ++i
)
1922 if (test_bit(i
, strmap
))
1923 *++s
= mod
->strtab
[i
];
1926 static inline unsigned long layout_symtab(struct module
*mod
,
1928 unsigned int symindex
,
1929 unsigned int strindex
,
1930 const Elf_Ehdr
*hdr
,
1931 const char *secstrings
,
1932 unsigned long *pstroffs
,
1933 unsigned long *strmap
)
1938 static inline void add_kallsyms(struct module
*mod
,
1941 unsigned int symindex
,
1942 unsigned int strindex
,
1943 unsigned long symoffs
,
1944 unsigned long stroffs
,
1945 const char *secstrings
,
1946 const unsigned long *strmap
)
1949 #endif /* CONFIG_KALLSYMS */
1951 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
1953 #ifdef CONFIG_DYNAMIC_DEBUG
1954 if (ddebug_add_module(debug
, num
, debug
->modname
))
1955 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
1960 static void *module_alloc_update_bounds(unsigned long size
)
1962 void *ret
= module_alloc(size
);
1965 /* Update module bounds. */
1966 if ((unsigned long)ret
< module_addr_min
)
1967 module_addr_min
= (unsigned long)ret
;
1968 if ((unsigned long)ret
+ size
> module_addr_max
)
1969 module_addr_max
= (unsigned long)ret
+ size
;
1974 #ifdef CONFIG_DEBUG_KMEMLEAK
1975 static void kmemleak_load_module(struct module
*mod
, Elf_Ehdr
*hdr
,
1976 Elf_Shdr
*sechdrs
, char *secstrings
)
1980 /* only scan the sections containing data */
1981 kmemleak_scan_area(mod
, sizeof(struct module
), GFP_KERNEL
);
1983 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
1984 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
1986 if (strncmp(secstrings
+ sechdrs
[i
].sh_name
, ".data", 5) != 0
1987 && strncmp(secstrings
+ sechdrs
[i
].sh_name
, ".bss", 4) != 0)
1990 kmemleak_scan_area((void *)sechdrs
[i
].sh_addr
,
1991 sechdrs
[i
].sh_size
, GFP_KERNEL
);
1995 static inline void kmemleak_load_module(struct module
*mod
, Elf_Ehdr
*hdr
,
1996 Elf_Shdr
*sechdrs
, char *secstrings
)
2001 /* Allocate and load the module: note that size of section 0 is always
2002 zero, and we rely on this for optional sections. */
2003 static noinline
struct module
*load_module(void __user
*umod
,
2005 const char __user
*uargs
)
2009 char *secstrings
, *args
, *modmagic
, *strtab
= NULL
;
2012 unsigned int symindex
= 0;
2013 unsigned int strindex
= 0;
2014 unsigned int modindex
, versindex
, infoindex
, pcpuindex
;
2017 void *ptr
= NULL
; /* Stops spurious gcc warning */
2018 unsigned long symoffs
, stroffs
, *strmap
;
2020 mm_segment_t old_fs
;
2022 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2024 if (len
< sizeof(*hdr
))
2025 return ERR_PTR(-ENOEXEC
);
2027 /* Suck in entire file: we'll want most of it. */
2028 /* vmalloc barfs on "unusual" numbers. Check here */
2029 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
2030 return ERR_PTR(-ENOMEM
);
2032 if (copy_from_user(hdr
, umod
, len
) != 0) {
2037 /* Sanity checks against insmoding binaries or wrong arch,
2038 weird elf version */
2039 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
2040 || hdr
->e_type
!= ET_REL
2041 || !elf_check_arch(hdr
)
2042 || hdr
->e_shentsize
!= sizeof(*sechdrs
)) {
2047 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
))
2050 /* Convenience variables */
2051 sechdrs
= (void *)hdr
+ hdr
->e_shoff
;
2052 secstrings
= (void *)hdr
+ sechdrs
[hdr
->e_shstrndx
].sh_offset
;
2053 sechdrs
[0].sh_addr
= 0;
2055 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2056 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
2057 && len
< sechdrs
[i
].sh_offset
+ sechdrs
[i
].sh_size
)
2060 /* Mark all sections sh_addr with their address in the
2062 sechdrs
[i
].sh_addr
= (size_t)hdr
+ sechdrs
[i
].sh_offset
;
2064 /* Internal symbols and strings. */
2065 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
2067 strindex
= sechdrs
[i
].sh_link
;
2068 strtab
= (char *)hdr
+ sechdrs
[strindex
].sh_offset
;
2070 #ifndef CONFIG_MODULE_UNLOAD
2071 /* Don't load .exit sections */
2072 if (strstarts(secstrings
+sechdrs
[i
].sh_name
, ".exit"))
2073 sechdrs
[i
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2077 modindex
= find_sec(hdr
, sechdrs
, secstrings
,
2078 ".gnu.linkonce.this_module");
2080 printk(KERN_WARNING
"No module found in object\n");
2084 /* This is temporary: point mod into copy of data. */
2085 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2087 if (symindex
== 0) {
2088 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
2094 versindex
= find_sec(hdr
, sechdrs
, secstrings
, "__versions");
2095 infoindex
= find_sec(hdr
, sechdrs
, secstrings
, ".modinfo");
2096 pcpuindex
= find_pcpusec(hdr
, sechdrs
, secstrings
);
2098 /* Don't keep modinfo and version sections. */
2099 sechdrs
[infoindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2100 sechdrs
[versindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2102 /* Check module struct version now, before we try to use module. */
2103 if (!check_modstruct_version(sechdrs
, versindex
, mod
)) {
2108 modmagic
= get_modinfo(sechdrs
, infoindex
, "vermagic");
2109 /* This is allowed: modprobe --force will invalidate it. */
2111 err
= try_to_force_load(mod
, "bad vermagic");
2114 } else if (!same_magic(modmagic
, vermagic
, versindex
)) {
2115 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2116 mod
->name
, modmagic
, vermagic
);
2121 staging
= get_modinfo(sechdrs
, infoindex
, "staging");
2123 add_taint_module(mod
, TAINT_CRAP
);
2124 printk(KERN_WARNING
"%s: module is from the staging directory,"
2125 " the quality is unknown, you have been warned.\n",
2129 /* Now copy in args */
2130 args
= strndup_user(uargs
, ~0UL >> 1);
2132 err
= PTR_ERR(args
);
2136 strmap
= kzalloc(BITS_TO_LONGS(sechdrs
[strindex
].sh_size
)
2137 * sizeof(long), GFP_KERNEL
);
2143 if (find_module(mod
->name
)) {
2148 mod
->state
= MODULE_STATE_COMING
;
2150 /* Allow arches to frob section contents and sizes. */
2151 err
= module_frob_arch_sections(hdr
, sechdrs
, secstrings
, mod
);
2156 /* We have a special allocation for this section. */
2157 err
= percpu_modalloc(mod
, sechdrs
[pcpuindex
].sh_size
,
2158 sechdrs
[pcpuindex
].sh_addralign
);
2161 sechdrs
[pcpuindex
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2164 /* Determine total sizes, and put offsets in sh_entsize. For now
2165 this is done generically; there doesn't appear to be any
2166 special cases for the architectures. */
2167 layout_sections(mod
, hdr
, sechdrs
, secstrings
);
2168 symoffs
= layout_symtab(mod
, sechdrs
, symindex
, strindex
, hdr
,
2169 secstrings
, &stroffs
, strmap
);
2171 /* Do the allocs. */
2172 ptr
= module_alloc_update_bounds(mod
->core_size
);
2174 * The pointer to this block is stored in the module structure
2175 * which is inside the block. Just mark it as not being a
2178 kmemleak_not_leak(ptr
);
2183 memset(ptr
, 0, mod
->core_size
);
2184 mod
->module_core
= ptr
;
2186 ptr
= module_alloc_update_bounds(mod
->init_size
);
2188 * The pointer to this block is stored in the module structure
2189 * which is inside the block. This block doesn't need to be
2190 * scanned as it contains data and code that will be freed
2191 * after the module is initialized.
2193 kmemleak_ignore(ptr
);
2194 if (!ptr
&& mod
->init_size
) {
2198 memset(ptr
, 0, mod
->init_size
);
2199 mod
->module_init
= ptr
;
2201 /* Transfer each section which specifies SHF_ALLOC */
2202 DEBUGP("final section addresses:\n");
2203 for (i
= 0; i
< hdr
->e_shnum
; i
++) {
2206 if (!(sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2209 if (sechdrs
[i
].sh_entsize
& INIT_OFFSET_MASK
)
2210 dest
= mod
->module_init
2211 + (sechdrs
[i
].sh_entsize
& ~INIT_OFFSET_MASK
);
2213 dest
= mod
->module_core
+ sechdrs
[i
].sh_entsize
;
2215 if (sechdrs
[i
].sh_type
!= SHT_NOBITS
)
2216 memcpy(dest
, (void *)sechdrs
[i
].sh_addr
,
2217 sechdrs
[i
].sh_size
);
2218 /* Update sh_addr to point to copy in image. */
2219 sechdrs
[i
].sh_addr
= (unsigned long)dest
;
2220 DEBUGP("\t0x%lx %s\n", sechdrs
[i
].sh_addr
, secstrings
+ sechdrs
[i
].sh_name
);
2222 /* Module has been moved. */
2223 mod
= (void *)sechdrs
[modindex
].sh_addr
;
2224 kmemleak_load_module(mod
, hdr
, sechdrs
, secstrings
);
2226 #if defined(CONFIG_MODULE_UNLOAD)
2227 mod
->refptr
= alloc_percpu(struct module_ref
);
2233 /* Now we've moved module, initialize linked lists, etc. */
2234 module_unload_init(mod
);
2236 /* add kobject, so we can reference it. */
2237 err
= mod_sysfs_init(mod
);
2241 /* Set up license info based on the info section */
2242 set_license(mod
, get_modinfo(sechdrs
, infoindex
, "license"));
2245 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2246 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2247 * using GPL-only symbols it needs.
2249 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2250 add_taint(TAINT_PROPRIETARY_MODULE
);
2252 /* driverloader was caught wrongly pretending to be under GPL */
2253 if (strcmp(mod
->name
, "driverloader") == 0)
2254 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2256 /* Set up MODINFO_ATTR fields */
2257 setup_modinfo(mod
, sechdrs
, infoindex
);
2259 /* Fix up syms, so that st_value is a pointer to location. */
2260 err
= simplify_symbols(sechdrs
, symindex
, strtab
, versindex
, pcpuindex
,
2265 /* Now we've got everything in the final locations, we can
2266 * find optional sections. */
2267 mod
->kp
= section_objs(hdr
, sechdrs
, secstrings
, "__param",
2268 sizeof(*mod
->kp
), &mod
->num_kp
);
2269 mod
->syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab",
2270 sizeof(*mod
->syms
), &mod
->num_syms
);
2271 mod
->crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab");
2272 mod
->gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
, "__ksymtab_gpl",
2273 sizeof(*mod
->gpl_syms
),
2274 &mod
->num_gpl_syms
);
2275 mod
->gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
, "__kcrctab_gpl");
2276 mod
->gpl_future_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2277 "__ksymtab_gpl_future",
2278 sizeof(*mod
->gpl_future_syms
),
2279 &mod
->num_gpl_future_syms
);
2280 mod
->gpl_future_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2281 "__kcrctab_gpl_future");
2283 #ifdef CONFIG_UNUSED_SYMBOLS
2284 mod
->unused_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2286 sizeof(*mod
->unused_syms
),
2287 &mod
->num_unused_syms
);
2288 mod
->unused_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2289 "__kcrctab_unused");
2290 mod
->unused_gpl_syms
= section_objs(hdr
, sechdrs
, secstrings
,
2291 "__ksymtab_unused_gpl",
2292 sizeof(*mod
->unused_gpl_syms
),
2293 &mod
->num_unused_gpl_syms
);
2294 mod
->unused_gpl_crcs
= section_addr(hdr
, sechdrs
, secstrings
,
2295 "__kcrctab_unused_gpl");
2297 #ifdef CONFIG_CONSTRUCTORS
2298 mod
->ctors
= section_objs(hdr
, sechdrs
, secstrings
, ".ctors",
2299 sizeof(*mod
->ctors
), &mod
->num_ctors
);
2302 #ifdef CONFIG_TRACEPOINTS
2303 mod
->tracepoints
= section_objs(hdr
, sechdrs
, secstrings
,
2305 sizeof(*mod
->tracepoints
),
2306 &mod
->num_tracepoints
);
2308 #ifdef CONFIG_EVENT_TRACING
2309 mod
->trace_events
= section_objs(hdr
, sechdrs
, secstrings
,
2311 sizeof(*mod
->trace_events
),
2312 &mod
->num_trace_events
);
2314 * This section contains pointers to allocated objects in the trace
2315 * code and not scanning it leads to false positives.
2317 kmemleak_scan_area(mod
->trace_events
, sizeof(*mod
->trace_events
) *
2318 mod
->num_trace_events
, GFP_KERNEL
);
2320 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2321 /* sechdrs[0].sh_size is always zero */
2322 mod
->ftrace_callsites
= section_objs(hdr
, sechdrs
, secstrings
,
2324 sizeof(*mod
->ftrace_callsites
),
2325 &mod
->num_ftrace_callsites
);
2327 #ifdef CONFIG_MODVERSIONS
2328 if ((mod
->num_syms
&& !mod
->crcs
)
2329 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2330 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2331 #ifdef CONFIG_UNUSED_SYMBOLS
2332 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2333 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2336 err
= try_to_force_load(mod
,
2337 "no versions for exported symbols");
2343 /* Now do relocations. */
2344 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
2345 const char *strtab
= (char *)sechdrs
[strindex
].sh_addr
;
2346 unsigned int info
= sechdrs
[i
].sh_info
;
2348 /* Not a valid relocation section? */
2349 if (info
>= hdr
->e_shnum
)
2352 /* Don't bother with non-allocated sections */
2353 if (!(sechdrs
[info
].sh_flags
& SHF_ALLOC
))
2356 if (sechdrs
[i
].sh_type
== SHT_REL
)
2357 err
= apply_relocate(sechdrs
, strtab
, symindex
, i
,mod
);
2358 else if (sechdrs
[i
].sh_type
== SHT_RELA
)
2359 err
= apply_relocate_add(sechdrs
, strtab
, symindex
, i
,
2365 /* Find duplicate symbols */
2366 err
= verify_export_symbols(mod
);
2370 /* Set up and sort exception table */
2371 mod
->extable
= section_objs(hdr
, sechdrs
, secstrings
, "__ex_table",
2372 sizeof(*mod
->extable
), &mod
->num_exentries
);
2373 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2375 /* Finally, copy percpu area over. */
2376 percpu_modcopy(mod
, (void *)sechdrs
[pcpuindex
].sh_addr
,
2377 sechdrs
[pcpuindex
].sh_size
);
2379 add_kallsyms(mod
, sechdrs
, hdr
->e_shnum
, symindex
, strindex
,
2380 symoffs
, stroffs
, secstrings
, strmap
);
2385 struct _ddebug
*debug
;
2386 unsigned int num_debug
;
2388 debug
= section_objs(hdr
, sechdrs
, secstrings
, "__verbose",
2389 sizeof(*debug
), &num_debug
);
2391 dynamic_debug_setup(debug
, num_debug
);
2394 err
= module_finalize(hdr
, sechdrs
, mod
);
2398 /* flush the icache in correct context */
2403 * Flush the instruction cache, since we've played with text.
2404 * Do it before processing of module parameters, so the module
2405 * can provide parameter accessor functions of its own.
2407 if (mod
->module_init
)
2408 flush_icache_range((unsigned long)mod
->module_init
,
2409 (unsigned long)mod
->module_init
2411 flush_icache_range((unsigned long)mod
->module_core
,
2412 (unsigned long)mod
->module_core
+ mod
->core_size
);
2417 if (section_addr(hdr
, sechdrs
, secstrings
, "__obsparm"))
2418 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2421 /* Now sew it into the lists so we can get lockdep and oops
2422 * info during argument parsing. Noone should access us, since
2423 * strong_try_module_get() will fail.
2424 * lockdep/oops can run asynchronous, so use the RCU list insertion
2425 * function to insert in a way safe to concurrent readers.
2426 * The mutex protects against concurrent writers.
2428 list_add_rcu(&mod
->list
, &modules
);
2430 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
, NULL
);
2434 err
= mod_sysfs_setup(mod
, mod
->kp
, mod
->num_kp
);
2437 add_sect_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2438 add_notes_attrs(mod
, hdr
->e_shnum
, secstrings
, sechdrs
);
2440 /* Get rid of temporary copy */
2443 trace_module_load(mod
);
2449 /* Unlink carefully: kallsyms could be walking list. */
2450 list_del_rcu(&mod
->list
);
2451 synchronize_sched();
2452 module_arch_cleanup(mod
);
2455 kobject_del(&mod
->mkobj
.kobj
);
2456 kobject_put(&mod
->mkobj
.kobj
);
2458 module_unload_free(mod
);
2459 #if defined(CONFIG_MODULE_UNLOAD)
2460 free_percpu(mod
->refptr
);
2463 module_free(mod
, mod
->module_init
);
2465 module_free(mod
, mod
->module_core
);
2466 /* mod will be freed with core. Don't access it beyond this line! */
2468 percpu_modfree(mod
);
2474 return ERR_PTR(err
);
2477 printk(KERN_ERR
"Module len %lu truncated\n", len
);
2482 /* Call module constructors. */
2483 static void do_mod_ctors(struct module
*mod
)
2485 #ifdef CONFIG_CONSTRUCTORS
2488 for (i
= 0; i
< mod
->num_ctors
; i
++)
2493 /* This is where the real work happens */
2494 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
2495 unsigned long, len
, const char __user
*, uargs
)
2500 /* Must have permission */
2501 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
2504 /* Only one module load at a time, please */
2505 if (mutex_lock_interruptible(&module_mutex
) != 0)
2508 /* Do all the hard work */
2509 mod
= load_module(umod
, len
, uargs
);
2511 mutex_unlock(&module_mutex
);
2512 return PTR_ERR(mod
);
2515 /* Drop lock so they can recurse */
2516 mutex_unlock(&module_mutex
);
2518 blocking_notifier_call_chain(&module_notify_list
,
2519 MODULE_STATE_COMING
, mod
);
2522 /* Start the module */
2523 if (mod
->init
!= NULL
)
2524 ret
= do_one_initcall(mod
->init
);
2526 /* Init routine failed: abort. Try to protect us from
2527 buggy refcounters. */
2528 mod
->state
= MODULE_STATE_GOING
;
2529 synchronize_sched();
2531 blocking_notifier_call_chain(&module_notify_list
,
2532 MODULE_STATE_GOING
, mod
);
2533 mutex_lock(&module_mutex
);
2535 mutex_unlock(&module_mutex
);
2536 wake_up(&module_wq
);
2541 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2542 "%s: loading module anyway...\n",
2543 __func__
, mod
->name
, ret
,
2548 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2549 mod
->state
= MODULE_STATE_LIVE
;
2550 wake_up(&module_wq
);
2551 blocking_notifier_call_chain(&module_notify_list
,
2552 MODULE_STATE_LIVE
, mod
);
2554 /* We need to finish all async code before the module init sequence is done */
2555 async_synchronize_full();
2557 mutex_lock(&module_mutex
);
2558 /* Drop initial reference. */
2560 trim_init_extable(mod
);
2561 #ifdef CONFIG_KALLSYMS
2562 mod
->num_symtab
= mod
->core_num_syms
;
2563 mod
->symtab
= mod
->core_symtab
;
2564 mod
->strtab
= mod
->core_strtab
;
2566 module_free(mod
, mod
->module_init
);
2567 mod
->module_init
= NULL
;
2569 mod
->init_text_size
= 0;
2570 mutex_unlock(&module_mutex
);
2575 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2577 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2580 #ifdef CONFIG_KALLSYMS
2582 * This ignores the intensely annoying "mapping symbols" found
2583 * in ARM ELF files: $a, $t and $d.
2585 static inline int is_arm_mapping_symbol(const char *str
)
2587 return str
[0] == '$' && strchr("atd", str
[1])
2588 && (str
[2] == '\0' || str
[2] == '.');
2591 static const char *get_ksymbol(struct module
*mod
,
2593 unsigned long *size
,
2594 unsigned long *offset
)
2596 unsigned int i
, best
= 0;
2597 unsigned long nextval
;
2599 /* At worse, next value is at end of module */
2600 if (within_module_init(addr
, mod
))
2601 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2603 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2605 /* Scan for closest preceeding symbol, and next symbol. (ELF
2606 starts real symbols at 1). */
2607 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2608 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2611 /* We ignore unnamed symbols: they're uninformative
2612 * and inserted at a whim. */
2613 if (mod
->symtab
[i
].st_value
<= addr
2614 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2615 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2616 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2618 if (mod
->symtab
[i
].st_value
> addr
2619 && mod
->symtab
[i
].st_value
< nextval
2620 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2621 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2622 nextval
= mod
->symtab
[i
].st_value
;
2629 *size
= nextval
- mod
->symtab
[best
].st_value
;
2631 *offset
= addr
- mod
->symtab
[best
].st_value
;
2632 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2635 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2636 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2637 const char *module_address_lookup(unsigned long addr
,
2638 unsigned long *size
,
2639 unsigned long *offset
,
2644 const char *ret
= NULL
;
2647 list_for_each_entry_rcu(mod
, &modules
, list
) {
2648 if (within_module_init(addr
, mod
) ||
2649 within_module_core(addr
, mod
)) {
2651 *modname
= mod
->name
;
2652 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2656 /* Make a copy in here where it's safe */
2658 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2665 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2670 list_for_each_entry_rcu(mod
, &modules
, list
) {
2671 if (within_module_init(addr
, mod
) ||
2672 within_module_core(addr
, mod
)) {
2675 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2678 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2688 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2689 unsigned long *offset
, char *modname
, char *name
)
2694 list_for_each_entry_rcu(mod
, &modules
, list
) {
2695 if (within_module_init(addr
, mod
) ||
2696 within_module_core(addr
, mod
)) {
2699 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2703 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2705 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2715 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2716 char *name
, char *module_name
, int *exported
)
2721 list_for_each_entry_rcu(mod
, &modules
, list
) {
2722 if (symnum
< mod
->num_symtab
) {
2723 *value
= mod
->symtab
[symnum
].st_value
;
2724 *type
= mod
->symtab
[symnum
].st_info
;
2725 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2727 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2728 *exported
= is_exported(name
, *value
, mod
);
2732 symnum
-= mod
->num_symtab
;
2738 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2742 for (i
= 0; i
< mod
->num_symtab
; i
++)
2743 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2744 mod
->symtab
[i
].st_info
!= 'U')
2745 return mod
->symtab
[i
].st_value
;
2749 /* Look for this name: can be of form module:name. */
2750 unsigned long module_kallsyms_lookup_name(const char *name
)
2754 unsigned long ret
= 0;
2756 /* Don't lock: we're in enough trouble already. */
2758 if ((colon
= strchr(name
, ':')) != NULL
) {
2760 if ((mod
= find_module(name
)) != NULL
)
2761 ret
= mod_find_symname(mod
, colon
+1);
2764 list_for_each_entry_rcu(mod
, &modules
, list
)
2765 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2772 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
2773 struct module
*, unsigned long),
2780 list_for_each_entry(mod
, &modules
, list
) {
2781 for (i
= 0; i
< mod
->num_symtab
; i
++) {
2782 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
2783 mod
, mod
->symtab
[i
].st_value
);
2790 #endif /* CONFIG_KALLSYMS */
2792 static char *module_flags(struct module
*mod
, char *buf
)
2797 mod
->state
== MODULE_STATE_GOING
||
2798 mod
->state
== MODULE_STATE_COMING
) {
2800 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
2802 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
2804 if (mod
->taints
& (1 << TAINT_CRAP
))
2807 * TAINT_FORCED_RMMOD: could be added.
2808 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2812 /* Show a - for module-is-being-unloaded */
2813 if (mod
->state
== MODULE_STATE_GOING
)
2815 /* Show a + for module-is-being-loaded */
2816 if (mod
->state
== MODULE_STATE_COMING
)
2825 #ifdef CONFIG_PROC_FS
2826 /* Called by the /proc file system to return a list of modules. */
2827 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
2829 mutex_lock(&module_mutex
);
2830 return seq_list_start(&modules
, *pos
);
2833 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2835 return seq_list_next(p
, &modules
, pos
);
2838 static void m_stop(struct seq_file
*m
, void *p
)
2840 mutex_unlock(&module_mutex
);
2843 static int m_show(struct seq_file
*m
, void *p
)
2845 struct module
*mod
= list_entry(p
, struct module
, list
);
2848 seq_printf(m
, "%s %u",
2849 mod
->name
, mod
->init_size
+ mod
->core_size
);
2850 print_unload_info(m
, mod
);
2852 /* Informative for users. */
2853 seq_printf(m
, " %s",
2854 mod
->state
== MODULE_STATE_GOING
? "Unloading":
2855 mod
->state
== MODULE_STATE_COMING
? "Loading":
2857 /* Used by oprofile and other similar tools. */
2858 seq_printf(m
, " 0x%p", mod
->module_core
);
2862 seq_printf(m
, " %s", module_flags(mod
, buf
));
2864 seq_printf(m
, "\n");
2868 /* Format: modulename size refcount deps address
2870 Where refcount is a number or -, and deps is a comma-separated list
2873 static const struct seq_operations modules_op
= {
2880 static int modules_open(struct inode
*inode
, struct file
*file
)
2882 return seq_open(file
, &modules_op
);
2885 static const struct file_operations proc_modules_operations
= {
2886 .open
= modules_open
,
2888 .llseek
= seq_lseek
,
2889 .release
= seq_release
,
2892 static int __init
proc_modules_init(void)
2894 proc_create("modules", 0, NULL
, &proc_modules_operations
);
2897 module_init(proc_modules_init
);
2900 /* Given an address, look for it in the module exception tables. */
2901 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
2903 const struct exception_table_entry
*e
= NULL
;
2907 list_for_each_entry_rcu(mod
, &modules
, list
) {
2908 if (mod
->num_exentries
== 0)
2911 e
= search_extable(mod
->extable
,
2912 mod
->extable
+ mod
->num_exentries
- 1,
2919 /* Now, if we found one, we are running inside it now, hence
2920 we cannot unload the module, hence no refcnt needed. */
2925 * is_module_address - is this address inside a module?
2926 * @addr: the address to check.
2928 * See is_module_text_address() if you simply want to see if the address
2929 * is code (not data).
2931 bool is_module_address(unsigned long addr
)
2936 ret
= __module_address(addr
) != NULL
;
2943 * __module_address - get the module which contains an address.
2944 * @addr: the address.
2946 * Must be called with preempt disabled or module mutex held so that
2947 * module doesn't get freed during this.
2949 struct module
*__module_address(unsigned long addr
)
2953 if (addr
< module_addr_min
|| addr
> module_addr_max
)
2956 list_for_each_entry_rcu(mod
, &modules
, list
)
2957 if (within_module_core(addr
, mod
)
2958 || within_module_init(addr
, mod
))
2962 EXPORT_SYMBOL_GPL(__module_address
);
2965 * is_module_text_address - is this address inside module code?
2966 * @addr: the address to check.
2968 * See is_module_address() if you simply want to see if the address is
2969 * anywhere in a module. See kernel_text_address() for testing if an
2970 * address corresponds to kernel or module code.
2972 bool is_module_text_address(unsigned long addr
)
2977 ret
= __module_text_address(addr
) != NULL
;
2984 * __module_text_address - get the module whose code contains an address.
2985 * @addr: the address.
2987 * Must be called with preempt disabled or module mutex held so that
2988 * module doesn't get freed during this.
2990 struct module
*__module_text_address(unsigned long addr
)
2992 struct module
*mod
= __module_address(addr
);
2994 /* Make sure it's within the text section. */
2995 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
2996 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
3001 EXPORT_SYMBOL_GPL(__module_text_address
);
3003 /* Don't grab lock, we're oopsing. */
3004 void print_modules(void)
3009 printk(KERN_DEFAULT
"Modules linked in:");
3010 /* Most callers should already have preempt disabled, but make sure */
3012 list_for_each_entry_rcu(mod
, &modules
, list
)
3013 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
3015 if (last_unloaded_module
[0])
3016 printk(" [last unloaded: %s]", last_unloaded_module
);
3020 #ifdef CONFIG_MODVERSIONS
3021 /* Generate the signature for all relevant module structures here.
3022 * If these change, we don't want to try to parse the module. */
3023 void module_layout(struct module
*mod
,
3024 struct modversion_info
*ver
,
3025 struct kernel_param
*kp
,
3026 struct kernel_symbol
*ks
,
3027 struct tracepoint
*tp
)
3030 EXPORT_SYMBOL(module_layout
);
3033 #ifdef CONFIG_TRACEPOINTS
3034 void module_update_tracepoints(void)
3038 mutex_lock(&module_mutex
);
3039 list_for_each_entry(mod
, &modules
, list
)
3041 tracepoint_update_probe_range(mod
->tracepoints
,
3042 mod
->tracepoints
+ mod
->num_tracepoints
);
3043 mutex_unlock(&module_mutex
);
3047 * Returns 0 if current not found.
3048 * Returns 1 if current found.
3050 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
3052 struct module
*iter_mod
;
3055 mutex_lock(&module_mutex
);
3056 list_for_each_entry(iter_mod
, &modules
, list
) {
3057 if (!iter_mod
->taints
) {
3059 * Sorted module list
3061 if (iter_mod
< iter
->module
)
3063 else if (iter_mod
> iter
->module
)
3064 iter
->tracepoint
= NULL
;
3065 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
3066 iter_mod
->tracepoints
,
3067 iter_mod
->tracepoints
3068 + iter_mod
->num_tracepoints
);
3070 iter
->module
= iter_mod
;
3075 mutex_unlock(&module_mutex
);