1 /* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */
3 Copyright (C) 2002 Richard Henderson
4 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/ftrace_event.h>
23 #include <linux/init.h>
24 #include <linux/kallsyms.h>
26 #include <linux/sysfs.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30 #include <linux/elf.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/syscalls.h>
34 #include <linux/fcntl.h>
35 #include <linux/rcupdate.h>
36 #include <linux/capability.h>
37 #include <linux/cpu.h>
38 #include <linux/moduleparam.h>
39 #include <linux/errno.h>
40 #include <linux/err.h>
41 #include <linux/vermagic.h>
42 #include <linux/notifier.h>
43 #include <linux/sched.h>
44 #include <linux/stop_machine.h>
45 #include <linux/device.h>
46 #include <linux/string.h>
47 #include <linux/mutex.h>
48 #include <linux/rculist.h>
49 #include <asm/uaccess.h>
50 #include <asm/cacheflush.h>
51 #include <asm/mmu_context.h>
52 #include <linux/license.h>
53 #include <asm/sections.h>
54 #include <linux/tracepoint.h>
55 #include <linux/ftrace.h>
56 #include <linux/async.h>
57 #include <linux/percpu.h>
58 #include <linux/kmemleak.h>
60 #define CREATE_TRACE_POINTS
61 #include <trace/events/module.h>
63 #define DEBUGP(fmt , a...)
65 #ifndef ARCH_SHF_SMALL
66 #define ARCH_SHF_SMALL 0
69 /* If this is set, the section belongs in the init part of the module */
70 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
74 * 1) List of modules (also safely readable with preempt_disable),
75 * 2) module_use links,
76 * 3) module_addr_min/module_addr_max.
77 * (delete uses stop_machine/add uses RCU list operations). */
78 DEFINE_MUTEX(module_mutex
);
79 EXPORT_SYMBOL_GPL(module_mutex
);
80 static LIST_HEAD(modules
);
81 #ifdef CONFIG_KGDB_KDB
82 struct list_head
*kdb_modules
= &modules
; /* kdb needs the list of modules */
83 #endif /* CONFIG_KGDB_KDB */
86 /* Block module loading/unloading? */
87 int modules_disabled
= 0;
89 /* Waiting for a module to finish initializing? */
90 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
92 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
94 /* Bounds of module allocation, for speeding __module_address.
95 * Protected by module_mutex. */
96 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
98 int register_module_notifier(struct notifier_block
* nb
)
100 return blocking_notifier_chain_register(&module_notify_list
, nb
);
102 EXPORT_SYMBOL(register_module_notifier
);
104 int unregister_module_notifier(struct notifier_block
* nb
)
106 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
108 EXPORT_SYMBOL(unregister_module_notifier
);
114 char *secstrings
, *strtab
;
115 unsigned long *strmap
;
116 unsigned long symoffs
, stroffs
;
117 struct _ddebug
*debug
;
118 unsigned int num_debug
;
120 unsigned int sym
, str
, mod
, vers
, info
, pcpu
;
124 /* We require a truly strong try_module_get(): 0 means failure due to
125 ongoing or failed initialization etc. */
126 static inline int strong_try_module_get(struct module
*mod
)
128 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
130 if (try_module_get(mod
))
136 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
139 mod
->taints
|= (1U << flag
);
143 * A thread that wants to hold a reference to a module only while it
144 * is running can call this to safely exit. nfsd and lockd use this.
146 void __module_put_and_exit(struct module
*mod
, long code
)
151 EXPORT_SYMBOL(__module_put_and_exit
);
153 /* Find a module section: 0 means not found. */
154 static unsigned int find_sec(const struct load_info
*info
, const char *name
)
158 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
159 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
160 /* Alloc bit cleared means "ignore it." */
161 if ((shdr
->sh_flags
& SHF_ALLOC
)
162 && strcmp(info
->secstrings
+ shdr
->sh_name
, name
) == 0)
168 /* Find a module section, or NULL. */
169 static void *section_addr(const struct load_info
*info
, const char *name
)
171 /* Section 0 has sh_addr 0. */
172 return (void *)info
->sechdrs
[find_sec(info
, name
)].sh_addr
;
175 /* Find a module section, or NULL. Fill in number of "objects" in section. */
176 static void *section_objs(const struct load_info
*info
,
181 unsigned int sec
= find_sec(info
, name
);
183 /* Section 0 has sh_addr 0 and sh_size 0. */
184 *num
= info
->sechdrs
[sec
].sh_size
/ object_size
;
185 return (void *)info
->sechdrs
[sec
].sh_addr
;
188 /* Provided by the linker */
189 extern const struct kernel_symbol __start___ksymtab
[];
190 extern const struct kernel_symbol __stop___ksymtab
[];
191 extern const struct kernel_symbol __start___ksymtab_gpl
[];
192 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
193 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
194 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
195 extern const unsigned long __start___kcrctab
[];
196 extern const unsigned long __start___kcrctab_gpl
[];
197 extern const unsigned long __start___kcrctab_gpl_future
[];
198 #ifdef CONFIG_UNUSED_SYMBOLS
199 extern const struct kernel_symbol __start___ksymtab_unused
[];
200 extern const struct kernel_symbol __stop___ksymtab_unused
[];
201 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
202 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
203 extern const unsigned long __start___kcrctab_unused
[];
204 extern const unsigned long __start___kcrctab_unused_gpl
[];
207 #ifndef CONFIG_MODVERSIONS
208 #define symversion(base, idx) NULL
210 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
213 static bool each_symbol_in_section(const struct symsearch
*arr
,
214 unsigned int arrsize
,
215 struct module
*owner
,
216 bool (*fn
)(const struct symsearch
*syms
,
217 struct module
*owner
,
218 unsigned int symnum
, void *data
),
223 for (j
= 0; j
< arrsize
; j
++) {
224 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
225 if (fn(&arr
[j
], owner
, i
, data
))
232 /* Returns true as soon as fn returns true, otherwise false. */
233 bool each_symbol(bool (*fn
)(const struct symsearch
*arr
, struct module
*owner
,
234 unsigned int symnum
, void *data
), void *data
)
237 static const struct symsearch arr
[] = {
238 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
239 NOT_GPL_ONLY
, false },
240 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
241 __start___kcrctab_gpl
,
243 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
244 __start___kcrctab_gpl_future
,
245 WILL_BE_GPL_ONLY
, false },
246 #ifdef CONFIG_UNUSED_SYMBOLS
247 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
248 __start___kcrctab_unused
,
249 NOT_GPL_ONLY
, true },
250 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
251 __start___kcrctab_unused_gpl
,
256 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
259 list_for_each_entry_rcu(mod
, &modules
, list
) {
260 struct symsearch arr
[] = {
261 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
262 NOT_GPL_ONLY
, false },
263 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
266 { mod
->gpl_future_syms
,
267 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
268 mod
->gpl_future_crcs
,
269 WILL_BE_GPL_ONLY
, false },
270 #ifdef CONFIG_UNUSED_SYMBOLS
272 mod
->unused_syms
+ mod
->num_unused_syms
,
274 NOT_GPL_ONLY
, true },
275 { mod
->unused_gpl_syms
,
276 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
277 mod
->unused_gpl_crcs
,
282 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
287 EXPORT_SYMBOL_GPL(each_symbol
);
289 struct find_symbol_arg
{
296 struct module
*owner
;
297 const unsigned long *crc
;
298 const struct kernel_symbol
*sym
;
301 static bool find_symbol_in_section(const struct symsearch
*syms
,
302 struct module
*owner
,
303 unsigned int symnum
, void *data
)
305 struct find_symbol_arg
*fsa
= data
;
307 if (strcmp(syms
->start
[symnum
].name
, fsa
->name
) != 0)
311 if (syms
->licence
== GPL_ONLY
)
313 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
314 printk(KERN_WARNING
"Symbol %s is being used "
315 "by a non-GPL module, which will not "
316 "be allowed in the future\n", fsa
->name
);
317 printk(KERN_WARNING
"Please see the file "
318 "Documentation/feature-removal-schedule.txt "
319 "in the kernel source tree for more details.\n");
323 #ifdef CONFIG_UNUSED_SYMBOLS
324 if (syms
->unused
&& fsa
->warn
) {
325 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
326 "however this module is using it.\n", fsa
->name
);
328 "This symbol will go away in the future.\n");
330 "Please evalute if this is the right api to use and if "
331 "it really is, submit a report the linux kernel "
332 "mailinglist together with submitting your code for "
338 fsa
->crc
= symversion(syms
->crcs
, symnum
);
339 fsa
->sym
= &syms
->start
[symnum
];
343 /* Find a symbol and return it, along with, (optional) crc and
344 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
345 const struct kernel_symbol
*find_symbol(const char *name
,
346 struct module
**owner
,
347 const unsigned long **crc
,
351 struct find_symbol_arg fsa
;
357 if (each_symbol(find_symbol_in_section
, &fsa
)) {
365 DEBUGP("Failed to find symbol %s\n", name
);
368 EXPORT_SYMBOL_GPL(find_symbol
);
370 /* Search for module by name: must hold module_mutex. */
371 struct module
*find_module(const char *name
)
375 list_for_each_entry(mod
, &modules
, list
) {
376 if (strcmp(mod
->name
, name
) == 0)
381 EXPORT_SYMBOL_GPL(find_module
);
385 static inline void __percpu
*mod_percpu(struct module
*mod
)
390 static int percpu_modalloc(struct module
*mod
,
391 unsigned long size
, unsigned long align
)
393 if (align
> PAGE_SIZE
) {
394 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
395 mod
->name
, align
, PAGE_SIZE
);
399 mod
->percpu
= __alloc_reserved_percpu(size
, align
);
402 "%s: Could not allocate %lu bytes percpu data\n",
406 mod
->percpu_size
= size
;
410 static void percpu_modfree(struct module
*mod
)
412 free_percpu(mod
->percpu
);
415 static unsigned int find_pcpusec(struct load_info
*info
)
417 return find_sec(info
, ".data..percpu");
420 static void percpu_modcopy(struct module
*mod
,
421 const void *from
, unsigned long size
)
425 for_each_possible_cpu(cpu
)
426 memcpy(per_cpu_ptr(mod
->percpu
, cpu
), from
, size
);
430 * is_module_percpu_address - test whether address is from module static percpu
431 * @addr: address to test
433 * Test whether @addr belongs to module static percpu area.
436 * %true if @addr is from module static percpu area
438 bool is_module_percpu_address(unsigned long addr
)
445 list_for_each_entry_rcu(mod
, &modules
, list
) {
446 if (!mod
->percpu_size
)
448 for_each_possible_cpu(cpu
) {
449 void *start
= per_cpu_ptr(mod
->percpu
, cpu
);
451 if ((void *)addr
>= start
&&
452 (void *)addr
< start
+ mod
->percpu_size
) {
463 #else /* ... !CONFIG_SMP */
465 static inline void __percpu
*mod_percpu(struct module
*mod
)
469 static inline int percpu_modalloc(struct module
*mod
,
470 unsigned long size
, unsigned long align
)
474 static inline void percpu_modfree(struct module
*mod
)
477 static unsigned int find_pcpusec(struct load_info
*info
)
481 static inline void percpu_modcopy(struct module
*mod
,
482 const void *from
, unsigned long size
)
484 /* pcpusec should be 0, and size of that section should be 0. */
487 bool is_module_percpu_address(unsigned long addr
)
492 #endif /* CONFIG_SMP */
494 #define MODINFO_ATTR(field) \
495 static void setup_modinfo_##field(struct module *mod, const char *s) \
497 mod->field = kstrdup(s, GFP_KERNEL); \
499 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
500 struct module *mod, char *buffer) \
502 return sprintf(buffer, "%s\n", mod->field); \
504 static int modinfo_##field##_exists(struct module *mod) \
506 return mod->field != NULL; \
508 static void free_modinfo_##field(struct module *mod) \
513 static struct module_attribute modinfo_##field = { \
514 .attr = { .name = __stringify(field), .mode = 0444 }, \
515 .show = show_modinfo_##field, \
516 .setup = setup_modinfo_##field, \
517 .test = modinfo_##field##_exists, \
518 .free = free_modinfo_##field, \
521 MODINFO_ATTR(version
);
522 MODINFO_ATTR(srcversion
);
524 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
526 #ifdef CONFIG_MODULE_UNLOAD
528 EXPORT_TRACEPOINT_SYMBOL(module_get
);
530 /* Init the unload section of the module. */
531 static int module_unload_init(struct module
*mod
)
533 mod
->refptr
= alloc_percpu(struct module_ref
);
537 INIT_LIST_HEAD(&mod
->source_list
);
538 INIT_LIST_HEAD(&mod
->target_list
);
540 /* Hold reference count during initialization. */
541 __this_cpu_write(mod
->refptr
->incs
, 1);
542 /* Backwards compatibility macros put refcount during init. */
543 mod
->waiter
= current
;
548 /* Does a already use b? */
549 static int already_uses(struct module
*a
, struct module
*b
)
551 struct module_use
*use
;
553 list_for_each_entry(use
, &b
->source_list
, source_list
) {
554 if (use
->source
== a
) {
555 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
559 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
565 * - we add 'a' as a "source", 'b' as a "target" of module use
566 * - the module_use is added to the list of 'b' sources (so
567 * 'b' can walk the list to see who sourced them), and of 'a'
568 * targets (so 'a' can see what modules it targets).
570 static int add_module_usage(struct module
*a
, struct module
*b
)
572 struct module_use
*use
;
574 DEBUGP("Allocating new usage for %s.\n", a
->name
);
575 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
577 printk(KERN_WARNING
"%s: out of memory loading\n", a
->name
);
583 list_add(&use
->source_list
, &b
->source_list
);
584 list_add(&use
->target_list
, &a
->target_list
);
588 /* Module a uses b: caller needs module_mutex() */
589 int ref_module(struct module
*a
, struct module
*b
)
593 if (b
== NULL
|| already_uses(a
, b
))
596 /* If module isn't available, we fail. */
597 err
= strong_try_module_get(b
);
601 err
= add_module_usage(a
, b
);
608 EXPORT_SYMBOL_GPL(ref_module
);
610 /* Clear the unload stuff of the module. */
611 static void module_unload_free(struct module
*mod
)
613 struct module_use
*use
, *tmp
;
615 mutex_lock(&module_mutex
);
616 list_for_each_entry_safe(use
, tmp
, &mod
->target_list
, target_list
) {
617 struct module
*i
= use
->target
;
618 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
620 list_del(&use
->source_list
);
621 list_del(&use
->target_list
);
624 mutex_unlock(&module_mutex
);
626 free_percpu(mod
->refptr
);
629 #ifdef CONFIG_MODULE_FORCE_UNLOAD
630 static inline int try_force_unload(unsigned int flags
)
632 int ret
= (flags
& O_TRUNC
);
634 add_taint(TAINT_FORCED_RMMOD
);
638 static inline int try_force_unload(unsigned int flags
)
642 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
651 /* Whole machine is stopped with interrupts off when this runs. */
652 static int __try_stop_module(void *_sref
)
654 struct stopref
*sref
= _sref
;
656 /* If it's not unused, quit unless we're forcing. */
657 if (module_refcount(sref
->mod
) != 0) {
658 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
662 /* Mark it as dying. */
663 sref
->mod
->state
= MODULE_STATE_GOING
;
667 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
669 if (flags
& O_NONBLOCK
) {
670 struct stopref sref
= { mod
, flags
, forced
};
672 return stop_machine(__try_stop_module
, &sref
, NULL
);
674 /* We don't need to stop the machine for this. */
675 mod
->state
= MODULE_STATE_GOING
;
681 unsigned int module_refcount(struct module
*mod
)
683 unsigned int incs
= 0, decs
= 0;
686 for_each_possible_cpu(cpu
)
687 decs
+= per_cpu_ptr(mod
->refptr
, cpu
)->decs
;
689 * ensure the incs are added up after the decs.
690 * module_put ensures incs are visible before decs with smp_wmb.
692 * This 2-count scheme avoids the situation where the refcount
693 * for CPU0 is read, then CPU0 increments the module refcount,
694 * then CPU1 drops that refcount, then the refcount for CPU1 is
695 * read. We would record a decrement but not its corresponding
696 * increment so we would see a low count (disaster).
698 * Rare situation? But module_refcount can be preempted, and we
699 * might be tallying up 4096+ CPUs. So it is not impossible.
702 for_each_possible_cpu(cpu
)
703 incs
+= per_cpu_ptr(mod
->refptr
, cpu
)->incs
;
706 EXPORT_SYMBOL(module_refcount
);
708 /* This exists whether we can unload or not */
709 static void free_module(struct module
*mod
);
711 static void wait_for_zero_refcount(struct module
*mod
)
713 /* Since we might sleep for some time, release the mutex first */
714 mutex_unlock(&module_mutex
);
716 DEBUGP("Looking at refcount...\n");
717 set_current_state(TASK_UNINTERRUPTIBLE
);
718 if (module_refcount(mod
) == 0)
722 current
->state
= TASK_RUNNING
;
723 mutex_lock(&module_mutex
);
726 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
730 char name
[MODULE_NAME_LEN
];
733 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
736 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
738 name
[MODULE_NAME_LEN
-1] = '\0';
740 if (mutex_lock_interruptible(&module_mutex
) != 0)
743 mod
= find_module(name
);
749 if (!list_empty(&mod
->source_list
)) {
750 /* Other modules depend on us: get rid of them first. */
755 /* Doing init or already dying? */
756 if (mod
->state
!= MODULE_STATE_LIVE
) {
757 DEBUGP("%s already dying\n", mod
->name
);
762 /* If it has an init func, it must have an exit func to unload */
763 if (mod
->init
&& !mod
->exit
) {
764 forced
= try_force_unload(flags
);
766 /* This module can't be removed */
772 /* Set this up before setting mod->state */
773 mod
->waiter
= current
;
775 /* Stop the machine so refcounts can't move and disable module. */
776 ret
= try_stop_module(mod
, flags
, &forced
);
780 /* Never wait if forced. */
781 if (!forced
&& module_refcount(mod
) != 0)
782 wait_for_zero_refcount(mod
);
784 mutex_unlock(&module_mutex
);
785 /* Final destruction now noone is using it. */
786 if (mod
->exit
!= NULL
)
788 blocking_notifier_call_chain(&module_notify_list
,
789 MODULE_STATE_GOING
, mod
);
790 async_synchronize_full();
792 /* Store the name of the last unloaded module for diagnostic purposes */
793 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
798 mutex_unlock(&module_mutex
);
802 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
804 struct module_use
*use
;
805 int printed_something
= 0;
807 seq_printf(m
, " %u ", module_refcount(mod
));
809 /* Always include a trailing , so userspace can differentiate
810 between this and the old multi-field proc format. */
811 list_for_each_entry(use
, &mod
->source_list
, source_list
) {
812 printed_something
= 1;
813 seq_printf(m
, "%s,", use
->source
->name
);
816 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
817 printed_something
= 1;
818 seq_printf(m
, "[permanent],");
821 if (!printed_something
)
825 void __symbol_put(const char *symbol
)
827 struct module
*owner
;
830 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
835 EXPORT_SYMBOL(__symbol_put
);
837 /* Note this assumes addr is a function, which it currently always is. */
838 void symbol_put_addr(void *addr
)
840 struct module
*modaddr
;
841 unsigned long a
= (unsigned long)dereference_function_descriptor(addr
);
843 if (core_kernel_text(a
))
846 /* module_text_address is safe here: we're supposed to have reference
847 * to module from symbol_get, so it can't go away. */
848 modaddr
= __module_text_address(a
);
852 EXPORT_SYMBOL_GPL(symbol_put_addr
);
854 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
855 struct module
*mod
, char *buffer
)
857 return sprintf(buffer
, "%u\n", module_refcount(mod
));
860 static struct module_attribute refcnt
= {
861 .attr
= { .name
= "refcnt", .mode
= 0444 },
865 void module_put(struct module
*module
)
869 smp_wmb(); /* see comment in module_refcount */
870 __this_cpu_inc(module
->refptr
->decs
);
872 trace_module_put(module
, _RET_IP_
);
873 /* Maybe they're waiting for us to drop reference? */
874 if (unlikely(!module_is_live(module
)))
875 wake_up_process(module
->waiter
);
879 EXPORT_SYMBOL(module_put
);
881 #else /* !CONFIG_MODULE_UNLOAD */
882 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
884 /* We don't know the usage count, or what modules are using. */
885 seq_printf(m
, " - -");
888 static inline void module_unload_free(struct module
*mod
)
892 int ref_module(struct module
*a
, struct module
*b
)
894 return strong_try_module_get(b
);
896 EXPORT_SYMBOL_GPL(ref_module
);
898 static inline int module_unload_init(struct module
*mod
)
902 #endif /* CONFIG_MODULE_UNLOAD */
904 static ssize_t
show_initstate(struct module_attribute
*mattr
,
905 struct module
*mod
, char *buffer
)
907 const char *state
= "unknown";
909 switch (mod
->state
) {
910 case MODULE_STATE_LIVE
:
913 case MODULE_STATE_COMING
:
916 case MODULE_STATE_GOING
:
920 return sprintf(buffer
, "%s\n", state
);
923 static struct module_attribute initstate
= {
924 .attr
= { .name
= "initstate", .mode
= 0444 },
925 .show
= show_initstate
,
928 static struct module_attribute
*modinfo_attrs
[] = {
932 #ifdef CONFIG_MODULE_UNLOAD
938 static const char vermagic
[] = VERMAGIC_STRING
;
940 static int try_to_force_load(struct module
*mod
, const char *reason
)
942 #ifdef CONFIG_MODULE_FORCE_LOAD
943 if (!test_taint(TAINT_FORCED_MODULE
))
944 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
946 add_taint_module(mod
, TAINT_FORCED_MODULE
);
953 #ifdef CONFIG_MODVERSIONS
954 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
955 static unsigned long maybe_relocated(unsigned long crc
,
956 const struct module
*crc_owner
)
958 #ifdef ARCH_RELOCATES_KCRCTAB
959 if (crc_owner
== NULL
)
960 return crc
- (unsigned long)reloc_start
;
965 static int check_version(Elf_Shdr
*sechdrs
,
966 unsigned int versindex
,
969 const unsigned long *crc
,
970 const struct module
*crc_owner
)
972 unsigned int i
, num_versions
;
973 struct modversion_info
*versions
;
975 /* Exporting module didn't supply crcs? OK, we're already tainted. */
979 /* No versions at all? modprobe --force does this. */
981 return try_to_force_load(mod
, symname
) == 0;
983 versions
= (void *) sechdrs
[versindex
].sh_addr
;
984 num_versions
= sechdrs
[versindex
].sh_size
985 / sizeof(struct modversion_info
);
987 for (i
= 0; i
< num_versions
; i
++) {
988 if (strcmp(versions
[i
].name
, symname
) != 0)
991 if (versions
[i
].crc
== maybe_relocated(*crc
, crc_owner
))
993 DEBUGP("Found checksum %lX vs module %lX\n",
994 maybe_relocated(*crc
, crc_owner
), versions
[i
].crc
);
998 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1003 printk("%s: disagrees about version of symbol %s\n",
1004 mod
->name
, symname
);
1008 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1009 unsigned int versindex
,
1012 const unsigned long *crc
;
1014 /* Since this should be found in kernel (which can't be removed),
1015 * no locking is necessary. */
1016 if (!find_symbol(MODULE_SYMBOL_PREFIX
"module_layout", NULL
,
1019 return check_version(sechdrs
, versindex
, "module_layout", mod
, crc
,
1023 /* First part is kernel version, which we ignore if module has crcs. */
1024 static inline int same_magic(const char *amagic
, const char *bmagic
,
1028 amagic
+= strcspn(amagic
, " ");
1029 bmagic
+= strcspn(bmagic
, " ");
1031 return strcmp(amagic
, bmagic
) == 0;
1034 static inline int check_version(Elf_Shdr
*sechdrs
,
1035 unsigned int versindex
,
1036 const char *symname
,
1038 const unsigned long *crc
,
1039 const struct module
*crc_owner
)
1044 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1045 unsigned int versindex
,
1051 static inline int same_magic(const char *amagic
, const char *bmagic
,
1054 return strcmp(amagic
, bmagic
) == 0;
1056 #endif /* CONFIG_MODVERSIONS */
1058 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1059 static const struct kernel_symbol
*resolve_symbol(struct module
*mod
,
1060 const struct load_info
*info
,
1064 struct module
*owner
;
1065 const struct kernel_symbol
*sym
;
1066 const unsigned long *crc
;
1069 mutex_lock(&module_mutex
);
1070 sym
= find_symbol(name
, &owner
, &crc
,
1071 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1075 if (!check_version(info
->sechdrs
, info
->index
.vers
, name
, mod
, crc
,
1077 sym
= ERR_PTR(-EINVAL
);
1081 err
= ref_module(mod
, owner
);
1088 /* We must make copy under the lock if we failed to get ref. */
1089 strncpy(ownername
, module_name(owner
), MODULE_NAME_LEN
);
1091 mutex_unlock(&module_mutex
);
1095 static const struct kernel_symbol
*
1096 resolve_symbol_wait(struct module
*mod
,
1097 const struct load_info
*info
,
1100 const struct kernel_symbol
*ksym
;
1101 char owner
[MODULE_NAME_LEN
];
1103 if (wait_event_interruptible_timeout(module_wq
,
1104 !IS_ERR(ksym
= resolve_symbol(mod
, info
, name
, owner
))
1105 || PTR_ERR(ksym
) != -EBUSY
,
1107 printk(KERN_WARNING
"%s: gave up waiting for init of module %s.\n",
1114 * /sys/module/foo/sections stuff
1115 * J. Corbet <corbet@lwn.net>
1119 #ifdef CONFIG_KALLSYMS
1120 static inline bool sect_empty(const Elf_Shdr
*sect
)
1122 return !(sect
->sh_flags
& SHF_ALLOC
) || sect
->sh_size
== 0;
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
, const struct load_info
*info
)
1158 unsigned int nloaded
= 0, i
, size
[2];
1159 struct module_sect_attrs
*sect_attrs
;
1160 struct module_sect_attr
*sattr
;
1161 struct attribute
**gattr
;
1163 /* Count loaded sections and allocate structures */
1164 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1165 if (!sect_empty(&info
->sechdrs
[i
]))
1167 size
[0] = ALIGN(sizeof(*sect_attrs
)
1168 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1169 sizeof(sect_attrs
->grp
.attrs
[0]));
1170 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1171 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1172 if (sect_attrs
== NULL
)
1175 /* Setup section attributes. */
1176 sect_attrs
->grp
.name
= "sections";
1177 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1179 sect_attrs
->nsections
= 0;
1180 sattr
= §_attrs
->attrs
[0];
1181 gattr
= §_attrs
->grp
.attrs
[0];
1182 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
1183 Elf_Shdr
*sec
= &info
->sechdrs
[i
];
1184 if (sect_empty(sec
))
1186 sattr
->address
= sec
->sh_addr
;
1187 sattr
->name
= kstrdup(info
->secstrings
+ sec
->sh_name
,
1189 if (sattr
->name
== NULL
)
1191 sect_attrs
->nsections
++;
1192 sysfs_attr_init(&sattr
->mattr
.attr
);
1193 sattr
->mattr
.show
= module_sect_show
;
1194 sattr
->mattr
.store
= NULL
;
1195 sattr
->mattr
.attr
.name
= sattr
->name
;
1196 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1197 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1201 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1204 mod
->sect_attrs
= sect_attrs
;
1207 free_sect_attrs(sect_attrs
);
1210 static void remove_sect_attrs(struct module
*mod
)
1212 if (mod
->sect_attrs
) {
1213 sysfs_remove_group(&mod
->mkobj
.kobj
,
1214 &mod
->sect_attrs
->grp
);
1215 /* We are positive that no one is using any sect attrs
1216 * at this point. Deallocate immediately. */
1217 free_sect_attrs(mod
->sect_attrs
);
1218 mod
->sect_attrs
= NULL
;
1223 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1226 struct module_notes_attrs
{
1227 struct kobject
*dir
;
1229 struct bin_attribute attrs
[0];
1232 static ssize_t
module_notes_read(struct file
*filp
, struct kobject
*kobj
,
1233 struct bin_attribute
*bin_attr
,
1234 char *buf
, loff_t pos
, size_t count
)
1237 * The caller checked the pos and count against our size.
1239 memcpy(buf
, bin_attr
->private + pos
, count
);
1243 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1246 if (notes_attrs
->dir
) {
1248 sysfs_remove_bin_file(notes_attrs
->dir
,
1249 ¬es_attrs
->attrs
[i
]);
1250 kobject_put(notes_attrs
->dir
);
1255 static void add_notes_attrs(struct module
*mod
, const struct load_info
*info
)
1257 unsigned int notes
, loaded
, i
;
1258 struct module_notes_attrs
*notes_attrs
;
1259 struct bin_attribute
*nattr
;
1261 /* failed to create section attributes, so can't create notes */
1262 if (!mod
->sect_attrs
)
1265 /* Count notes sections and allocate structures. */
1267 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1268 if (!sect_empty(&info
->sechdrs
[i
]) &&
1269 (info
->sechdrs
[i
].sh_type
== SHT_NOTE
))
1275 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1276 + notes
* sizeof(notes_attrs
->attrs
[0]),
1278 if (notes_attrs
== NULL
)
1281 notes_attrs
->notes
= notes
;
1282 nattr
= ¬es_attrs
->attrs
[0];
1283 for (loaded
= i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1284 if (sect_empty(&info
->sechdrs
[i
]))
1286 if (info
->sechdrs
[i
].sh_type
== SHT_NOTE
) {
1287 sysfs_bin_attr_init(nattr
);
1288 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1289 nattr
->attr
.mode
= S_IRUGO
;
1290 nattr
->size
= info
->sechdrs
[i
].sh_size
;
1291 nattr
->private = (void *) info
->sechdrs
[i
].sh_addr
;
1292 nattr
->read
= module_notes_read
;
1298 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1299 if (!notes_attrs
->dir
)
1302 for (i
= 0; i
< notes
; ++i
)
1303 if (sysfs_create_bin_file(notes_attrs
->dir
,
1304 ¬es_attrs
->attrs
[i
]))
1307 mod
->notes_attrs
= notes_attrs
;
1311 free_notes_attrs(notes_attrs
, i
);
1314 static void remove_notes_attrs(struct module
*mod
)
1316 if (mod
->notes_attrs
)
1317 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1322 static inline void add_sect_attrs(struct module
*mod
,
1323 const struct load_info
*info
)
1327 static inline void remove_sect_attrs(struct module
*mod
)
1331 static inline void add_notes_attrs(struct module
*mod
,
1332 const struct load_info
*info
)
1336 static inline void remove_notes_attrs(struct module
*mod
)
1339 #endif /* CONFIG_KALLSYMS */
1341 static void add_usage_links(struct module
*mod
)
1343 #ifdef CONFIG_MODULE_UNLOAD
1344 struct module_use
*use
;
1347 mutex_lock(&module_mutex
);
1348 list_for_each_entry(use
, &mod
->target_list
, target_list
) {
1349 nowarn
= sysfs_create_link(use
->target
->holders_dir
,
1350 &mod
->mkobj
.kobj
, mod
->name
);
1352 mutex_unlock(&module_mutex
);
1356 static void del_usage_links(struct module
*mod
)
1358 #ifdef CONFIG_MODULE_UNLOAD
1359 struct module_use
*use
;
1361 mutex_lock(&module_mutex
);
1362 list_for_each_entry(use
, &mod
->target_list
, target_list
)
1363 sysfs_remove_link(use
->target
->holders_dir
, mod
->name
);
1364 mutex_unlock(&module_mutex
);
1368 static int module_add_modinfo_attrs(struct module
*mod
)
1370 struct module_attribute
*attr
;
1371 struct module_attribute
*temp_attr
;
1375 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1376 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1378 if (!mod
->modinfo_attrs
)
1381 temp_attr
= mod
->modinfo_attrs
;
1382 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1384 (attr
->test
&& attr
->test(mod
))) {
1385 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1386 sysfs_attr_init(&temp_attr
->attr
);
1387 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1394 static void module_remove_modinfo_attrs(struct module
*mod
)
1396 struct module_attribute
*attr
;
1399 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1400 /* pick a field to test for end of list */
1401 if (!attr
->attr
.name
)
1403 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1407 kfree(mod
->modinfo_attrs
);
1410 static int mod_sysfs_init(struct module
*mod
)
1413 struct kobject
*kobj
;
1415 if (!module_sysfs_initialized
) {
1416 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1422 kobj
= kset_find_obj(module_kset
, mod
->name
);
1424 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1430 mod
->mkobj
.mod
= mod
;
1432 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1433 mod
->mkobj
.kobj
.kset
= module_kset
;
1434 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1437 kobject_put(&mod
->mkobj
.kobj
);
1439 /* delay uevent until full sysfs population */
1444 static int mod_sysfs_setup(struct module
*mod
,
1445 const struct load_info
*info
,
1446 struct kernel_param
*kparam
,
1447 unsigned int num_params
)
1451 err
= mod_sysfs_init(mod
);
1455 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1456 if (!mod
->holders_dir
) {
1461 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1463 goto out_unreg_holders
;
1465 err
= module_add_modinfo_attrs(mod
);
1467 goto out_unreg_param
;
1469 add_usage_links(mod
);
1470 add_sect_attrs(mod
, info
);
1471 add_notes_attrs(mod
, info
);
1473 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1477 module_param_sysfs_remove(mod
);
1479 kobject_put(mod
->holders_dir
);
1481 kobject_put(&mod
->mkobj
.kobj
);
1486 static void mod_sysfs_fini(struct module
*mod
)
1488 remove_notes_attrs(mod
);
1489 remove_sect_attrs(mod
);
1490 kobject_put(&mod
->mkobj
.kobj
);
1493 #else /* !CONFIG_SYSFS */
1495 static int mod_sysfs_setup(struct module
*mod
,
1496 const struct load_info
*info
,
1497 struct kernel_param
*kparam
,
1498 unsigned int num_params
)
1503 static void mod_sysfs_fini(struct module
*mod
)
1507 static void module_remove_modinfo_attrs(struct module
*mod
)
1511 static void del_usage_links(struct module
*mod
)
1515 #endif /* CONFIG_SYSFS */
1517 static void mod_sysfs_teardown(struct module
*mod
)
1519 del_usage_links(mod
);
1520 module_remove_modinfo_attrs(mod
);
1521 module_param_sysfs_remove(mod
);
1522 kobject_put(mod
->mkobj
.drivers_dir
);
1523 kobject_put(mod
->holders_dir
);
1524 mod_sysfs_fini(mod
);
1528 * unlink the module with the whole machine is stopped with interrupts off
1529 * - this defends against kallsyms not taking locks
1531 static int __unlink_module(void *_mod
)
1533 struct module
*mod
= _mod
;
1534 list_del(&mod
->list
);
1535 module_bug_cleanup(mod
);
1539 /* Free a module, remove from lists, etc. */
1540 static void free_module(struct module
*mod
)
1542 trace_module_free(mod
);
1544 /* Delete from various lists */
1545 mutex_lock(&module_mutex
);
1546 stop_machine(__unlink_module
, mod
, NULL
);
1547 mutex_unlock(&module_mutex
);
1548 mod_sysfs_teardown(mod
);
1550 /* Remove dynamic debug info */
1551 ddebug_remove_module(mod
->name
);
1553 /* Arch-specific cleanup. */
1554 module_arch_cleanup(mod
);
1556 /* Module unload stuff */
1557 module_unload_free(mod
);
1559 /* Free any allocated parameters. */
1560 destroy_params(mod
->kp
, mod
->num_kp
);
1562 /* This may be NULL, but that's OK */
1563 module_free(mod
, mod
->module_init
);
1565 percpu_modfree(mod
);
1567 /* Free lock-classes: */
1568 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1570 /* Finally, free the core (containing the module structure) */
1571 module_free(mod
, mod
->module_core
);
1574 update_protections(current
->mm
);
1578 void *__symbol_get(const char *symbol
)
1580 struct module
*owner
;
1581 const struct kernel_symbol
*sym
;
1584 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1585 if (sym
&& strong_try_module_get(owner
))
1589 return sym
? (void *)sym
->value
: NULL
;
1591 EXPORT_SYMBOL_GPL(__symbol_get
);
1594 * Ensure that an exported symbol [global namespace] does not already exist
1595 * in the kernel or in some other module's exported symbol table.
1597 * You must hold the module_mutex.
1599 static int verify_export_symbols(struct module
*mod
)
1602 struct module
*owner
;
1603 const struct kernel_symbol
*s
;
1605 const struct kernel_symbol
*sym
;
1608 { mod
->syms
, mod
->num_syms
},
1609 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1610 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1611 #ifdef CONFIG_UNUSED_SYMBOLS
1612 { mod
->unused_syms
, mod
->num_unused_syms
},
1613 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1617 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1618 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1619 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1621 "%s: exports duplicate symbol %s"
1623 mod
->name
, s
->name
, module_name(owner
));
1631 /* Change all symbols so that st_value encodes the pointer directly. */
1632 static int simplify_symbols(struct module
*mod
, const struct load_info
*info
)
1634 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
1635 Elf_Sym
*sym
= (void *)symsec
->sh_addr
;
1636 unsigned long secbase
;
1639 const struct kernel_symbol
*ksym
;
1641 for (i
= 1; i
< symsec
->sh_size
/ sizeof(Elf_Sym
); i
++) {
1642 const char *name
= info
->strtab
+ sym
[i
].st_name
;
1644 switch (sym
[i
].st_shndx
) {
1646 /* We compiled with -fno-common. These are not
1647 supposed to happen. */
1648 DEBUGP("Common symbol: %s\n", name
);
1649 printk("%s: please compile with -fno-common\n",
1655 /* Don't need to do anything */
1656 DEBUGP("Absolute symbol: 0x%08lx\n",
1657 (long)sym
[i
].st_value
);
1661 ksym
= resolve_symbol_wait(mod
, info
, name
);
1662 /* Ok if resolved. */
1663 if (ksym
&& !IS_ERR(ksym
)) {
1664 sym
[i
].st_value
= ksym
->value
;
1669 if (!ksym
&& ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1672 printk(KERN_WARNING
"%s: Unknown symbol %s (err %li)\n",
1673 mod
->name
, name
, PTR_ERR(ksym
));
1674 ret
= PTR_ERR(ksym
) ?: -ENOENT
;
1678 /* Divert to percpu allocation if a percpu var. */
1679 if (sym
[i
].st_shndx
== info
->index
.pcpu
)
1680 secbase
= (unsigned long)mod_percpu(mod
);
1682 secbase
= info
->sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1683 sym
[i
].st_value
+= secbase
;
1691 static int apply_relocations(struct module
*mod
, const struct load_info
*info
)
1696 /* Now do relocations. */
1697 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
1698 unsigned int infosec
= info
->sechdrs
[i
].sh_info
;
1700 /* Not a valid relocation section? */
1701 if (infosec
>= info
->hdr
->e_shnum
)
1704 /* Don't bother with non-allocated sections */
1705 if (!(info
->sechdrs
[infosec
].sh_flags
& SHF_ALLOC
))
1708 if (info
->sechdrs
[i
].sh_type
== SHT_REL
)
1709 err
= apply_relocate(info
->sechdrs
, info
->strtab
,
1710 info
->index
.sym
, i
, mod
);
1711 else if (info
->sechdrs
[i
].sh_type
== SHT_RELA
)
1712 err
= apply_relocate_add(info
->sechdrs
, info
->strtab
,
1713 info
->index
.sym
, i
, mod
);
1720 /* Additional bytes needed by arch in front of individual sections */
1721 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1722 unsigned int section
)
1724 /* default implementation just returns zero */
1728 /* Update size with this section: return offset. */
1729 static long get_offset(struct module
*mod
, unsigned int *size
,
1730 Elf_Shdr
*sechdr
, unsigned int section
)
1734 *size
+= arch_mod_section_prepend(mod
, section
);
1735 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1736 *size
= ret
+ sechdr
->sh_size
;
1740 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1741 might -- code, read-only data, read-write data, small data. Tally
1742 sizes, and place the offsets into sh_entsize fields: high bit means it
1744 static void layout_sections(struct module
*mod
, struct load_info
*info
)
1746 static unsigned long const masks
[][2] = {
1747 /* NOTE: all executable code must be the first section
1748 * in this array; otherwise modify the text_size
1749 * finder in the two loops below */
1750 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1751 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1752 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1753 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1757 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1758 info
->sechdrs
[i
].sh_entsize
= ~0UL;
1760 DEBUGP("Core section allocation order:\n");
1761 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1762 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1763 Elf_Shdr
*s
= &info
->sechdrs
[i
];
1764 const char *sname
= info
->secstrings
+ s
->sh_name
;
1766 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1767 || (s
->sh_flags
& masks
[m
][1])
1768 || s
->sh_entsize
!= ~0UL
1769 || strstarts(sname
, ".init"))
1771 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1772 DEBUGP("\t%s\n", name
);
1775 mod
->core_text_size
= mod
->core_size
;
1778 DEBUGP("Init section allocation order:\n");
1779 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1780 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1781 Elf_Shdr
*s
= &info
->sechdrs
[i
];
1782 const char *sname
= info
->secstrings
+ s
->sh_name
;
1784 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1785 || (s
->sh_flags
& masks
[m
][1])
1786 || s
->sh_entsize
!= ~0UL
1787 || !strstarts(sname
, ".init"))
1789 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
1790 | INIT_OFFSET_MASK
);
1791 DEBUGP("\t%s\n", sname
);
1794 mod
->init_text_size
= mod
->init_size
;
1798 static void set_license(struct module
*mod
, const char *license
)
1801 license
= "unspecified";
1803 #ifndef CONFIG_LOCKDEP
1804 if (!license_is_gpl_compatible(license
)) {
1805 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1806 printk(KERN_WARNING
"%s: module license '%s' taints "
1807 "kernel.\n", mod
->name
, license
);
1808 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1813 /* Parse tag=value strings from .modinfo section */
1814 static char *next_string(char *string
, unsigned long *secsize
)
1816 /* Skip non-zero chars */
1819 if ((*secsize
)-- <= 1)
1823 /* Skip any zero padding. */
1824 while (!string
[0]) {
1826 if ((*secsize
)-- <= 1)
1832 static char *get_modinfo(struct load_info
*info
, const char *tag
)
1835 unsigned int taglen
= strlen(tag
);
1836 Elf_Shdr
*infosec
= &info
->sechdrs
[info
->index
.info
];
1837 unsigned long size
= infosec
->sh_size
;
1839 for (p
= (char *)infosec
->sh_addr
; p
; p
= next_string(p
, &size
)) {
1840 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1841 return p
+ taglen
+ 1;
1846 static void setup_modinfo(struct module
*mod
, struct load_info
*info
)
1848 struct module_attribute
*attr
;
1851 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1853 attr
->setup(mod
, get_modinfo(info
, attr
->attr
.name
));
1857 static void free_modinfo(struct module
*mod
)
1859 struct module_attribute
*attr
;
1862 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
1868 #ifdef CONFIG_KALLSYMS
1870 /* lookup symbol in given range of kernel_symbols */
1871 static const struct kernel_symbol
*lookup_symbol(const char *name
,
1872 const struct kernel_symbol
*start
,
1873 const struct kernel_symbol
*stop
)
1875 const struct kernel_symbol
*ks
= start
;
1876 for (; ks
< stop
; ks
++)
1877 if (strcmp(ks
->name
, name
) == 0)
1882 static int is_exported(const char *name
, unsigned long value
,
1883 const struct module
*mod
)
1885 const struct kernel_symbol
*ks
;
1887 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
1889 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
1890 return ks
!= NULL
&& ks
->value
== value
;
1894 static char elf_type(const Elf_Sym
*sym
, const struct load_info
*info
)
1896 const Elf_Shdr
*sechdrs
= info
->sechdrs
;
1898 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
1899 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
1904 if (sym
->st_shndx
== SHN_UNDEF
)
1906 if (sym
->st_shndx
== SHN_ABS
)
1908 if (sym
->st_shndx
>= SHN_LORESERVE
)
1910 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
1912 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
1913 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
1914 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
1916 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1921 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
1922 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
1927 if (strstarts(info
->secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
1934 static bool is_core_symbol(const Elf_Sym
*src
, const Elf_Shdr
*sechdrs
,
1937 const Elf_Shdr
*sec
;
1939 if (src
->st_shndx
== SHN_UNDEF
1940 || src
->st_shndx
>= shnum
1944 sec
= sechdrs
+ src
->st_shndx
;
1945 if (!(sec
->sh_flags
& SHF_ALLOC
)
1946 #ifndef CONFIG_KALLSYMS_ALL
1947 || !(sec
->sh_flags
& SHF_EXECINSTR
)
1949 || (sec
->sh_entsize
& INIT_OFFSET_MASK
))
1955 static void layout_symtab(struct module
*mod
, struct load_info
*info
)
1957 Elf_Shdr
*symsect
= info
->sechdrs
+ info
->index
.sym
;
1958 Elf_Shdr
*strsect
= info
->sechdrs
+ info
->index
.str
;
1960 unsigned int i
, nsrc
, ndst
;
1962 /* Put symbol section at end of init part of module. */
1963 symsect
->sh_flags
|= SHF_ALLOC
;
1964 symsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, symsect
,
1965 info
->index
.sym
) | INIT_OFFSET_MASK
;
1966 DEBUGP("\t%s\n", info
->secstrings
+ symsect
->sh_name
);
1968 src
= (void *)info
->hdr
+ symsect
->sh_offset
;
1969 nsrc
= symsect
->sh_size
/ sizeof(*src
);
1970 for (ndst
= i
= 1; i
< nsrc
; ++i
, ++src
)
1971 if (is_core_symbol(src
, info
->sechdrs
, info
->hdr
->e_shnum
)) {
1972 unsigned int j
= src
->st_name
;
1974 while (!__test_and_set_bit(j
, info
->strmap
)
1980 /* Append room for core symbols at end of core part. */
1981 info
->symoffs
= ALIGN(mod
->core_size
, symsect
->sh_addralign
?: 1);
1982 mod
->core_size
= info
->symoffs
+ ndst
* sizeof(Elf_Sym
);
1984 /* Put string table section at end of init part of module. */
1985 strsect
->sh_flags
|= SHF_ALLOC
;
1986 strsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, strsect
,
1987 info
->index
.str
) | INIT_OFFSET_MASK
;
1988 DEBUGP("\t%s\n", info
->secstrings
+ strsect
->sh_name
);
1990 /* Append room for core symbols' strings at end of core part. */
1991 info
->stroffs
= mod
->core_size
;
1992 __set_bit(0, info
->strmap
);
1993 mod
->core_size
+= bitmap_weight(info
->strmap
, strsect
->sh_size
);
1996 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
1998 unsigned int i
, ndst
;
2002 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
2004 mod
->symtab
= (void *)symsec
->sh_addr
;
2005 mod
->num_symtab
= symsec
->sh_size
/ sizeof(Elf_Sym
);
2006 /* Make sure we get permanent strtab: don't use info->strtab. */
2007 mod
->strtab
= (void *)info
->sechdrs
[info
->index
.str
].sh_addr
;
2009 /* Set types up while we still have access to sections. */
2010 for (i
= 0; i
< mod
->num_symtab
; i
++)
2011 mod
->symtab
[i
].st_info
= elf_type(&mod
->symtab
[i
], info
);
2013 mod
->core_symtab
= dst
= mod
->module_core
+ info
->symoffs
;
2016 for (ndst
= i
= 1; i
< mod
->num_symtab
; ++i
, ++src
) {
2017 if (!is_core_symbol(src
, info
->sechdrs
, info
->hdr
->e_shnum
))
2020 dst
[ndst
].st_name
= bitmap_weight(info
->strmap
,
2024 mod
->core_num_syms
= ndst
;
2026 mod
->core_strtab
= s
= mod
->module_core
+ info
->stroffs
;
2027 for (*s
= 0, i
= 1; i
< info
->sechdrs
[info
->index
.str
].sh_size
; ++i
)
2028 if (test_bit(i
, info
->strmap
))
2029 *++s
= mod
->strtab
[i
];
2032 static inline void layout_symtab(struct module
*mod
, struct load_info
*info
)
2036 static void add_kallsyms(struct module
*mod
, struct load_info
*info
)
2039 #endif /* CONFIG_KALLSYMS */
2041 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
2045 #ifdef CONFIG_DYNAMIC_DEBUG
2046 if (ddebug_add_module(debug
, num
, debug
->modname
))
2047 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
2052 static void dynamic_debug_remove(struct _ddebug
*debug
)
2055 ddebug_remove_module(debug
->modname
);
2058 static void *module_alloc_update_bounds(unsigned long size
)
2060 void *ret
= module_alloc(size
);
2063 mutex_lock(&module_mutex
);
2064 /* Update module bounds. */
2065 if ((unsigned long)ret
< module_addr_min
)
2066 module_addr_min
= (unsigned long)ret
;
2067 if ((unsigned long)ret
+ size
> module_addr_max
)
2068 module_addr_max
= (unsigned long)ret
+ size
;
2069 mutex_unlock(&module_mutex
);
2074 #ifdef CONFIG_DEBUG_KMEMLEAK
2075 static void kmemleak_load_module(const struct module
*mod
,
2076 const struct load_info
*info
)
2080 /* only scan the sections containing data */
2081 kmemleak_scan_area(mod
, sizeof(struct module
), GFP_KERNEL
);
2083 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2084 const char *name
= info
->secstrings
+ info
->sechdrs
[i
].sh_name
;
2085 if (!(info
->sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2087 if (!strstarts(name
, ".data") && !strstarts(name
, ".bss"))
2090 kmemleak_scan_area((void *)info
->sechdrs
[i
].sh_addr
,
2091 info
->sechdrs
[i
].sh_size
, GFP_KERNEL
);
2095 static inline void kmemleak_load_module(const struct module
*mod
,
2096 const struct load_info
*info
)
2101 /* Sets info->hdr and info->len. */
2102 static int copy_and_check(struct load_info
*info
,
2103 const void __user
*umod
, unsigned long len
,
2104 const char __user
*uargs
)
2109 if (len
< sizeof(*hdr
))
2112 /* Suck in entire file: we'll want most of it. */
2113 /* vmalloc barfs on "unusual" numbers. Check here */
2114 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
2117 if (copy_from_user(hdr
, umod
, len
) != 0) {
2122 /* Sanity checks against insmoding binaries or wrong arch,
2123 weird elf version */
2124 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
2125 || hdr
->e_type
!= ET_REL
2126 || !elf_check_arch(hdr
)
2127 || hdr
->e_shentsize
!= sizeof(Elf_Shdr
)) {
2132 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
)) {
2146 static void free_copy(struct load_info
*info
)
2151 static int rewrite_section_headers(struct load_info
*info
)
2155 /* This should always be true, but let's be sure. */
2156 info
->sechdrs
[0].sh_addr
= 0;
2158 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2159 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2160 if (shdr
->sh_type
!= SHT_NOBITS
2161 && info
->len
< shdr
->sh_offset
+ shdr
->sh_size
) {
2162 printk(KERN_ERR
"Module len %lu truncated\n",
2167 /* Mark all sections sh_addr with their address in the
2169 shdr
->sh_addr
= (size_t)info
->hdr
+ shdr
->sh_offset
;
2171 #ifndef CONFIG_MODULE_UNLOAD
2172 /* Don't load .exit sections */
2173 if (strstarts(info
->secstrings
+shdr
->sh_name
, ".exit"))
2174 shdr
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2178 /* Track but don't keep modinfo and version sections. */
2179 info
->index
.vers
= find_sec(info
, "__versions");
2180 info
->index
.info
= find_sec(info
, ".modinfo");
2181 info
->sechdrs
[info
->index
.info
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2182 info
->sechdrs
[info
->index
.vers
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2187 * Set up our basic convenience variables (pointers to section headers,
2188 * search for module section index etc), and do some basic section
2191 * Return the temporary module pointer (we'll replace it with the final
2192 * one when we move the module sections around).
2194 static struct module
*setup_load_info(struct load_info
*info
)
2200 /* Set up the convenience variables */
2201 info
->sechdrs
= (void *)info
->hdr
+ info
->hdr
->e_shoff
;
2202 info
->secstrings
= (void *)info
->hdr
2203 + info
->sechdrs
[info
->hdr
->e_shstrndx
].sh_offset
;
2205 err
= rewrite_section_headers(info
);
2207 return ERR_PTR(err
);
2209 /* Find internal symbols and strings. */
2210 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2211 if (info
->sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
2212 info
->index
.sym
= i
;
2213 info
->index
.str
= info
->sechdrs
[i
].sh_link
;
2214 info
->strtab
= (char *)info
->hdr
2215 + info
->sechdrs
[info
->index
.str
].sh_offset
;
2220 info
->index
.mod
= find_sec(info
, ".gnu.linkonce.this_module");
2221 if (!info
->index
.mod
) {
2222 printk(KERN_WARNING
"No module found in object\n");
2223 return ERR_PTR(-ENOEXEC
);
2225 /* This is temporary: point mod into copy of data. */
2226 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2228 if (info
->index
.sym
== 0) {
2229 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
2231 return ERR_PTR(-ENOEXEC
);
2234 info
->index
.pcpu
= find_pcpusec(info
);
2236 /* Check module struct version now, before we try to use module. */
2237 if (!check_modstruct_version(info
->sechdrs
, info
->index
.vers
, mod
))
2238 return ERR_PTR(-ENOEXEC
);
2243 static int check_modinfo(struct module
*mod
, struct load_info
*info
)
2245 const char *modmagic
= get_modinfo(info
, "vermagic");
2248 /* This is allowed: modprobe --force will invalidate it. */
2250 err
= try_to_force_load(mod
, "bad vermagic");
2253 } else if (!same_magic(modmagic
, vermagic
, info
->index
.vers
)) {
2254 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2255 mod
->name
, modmagic
, vermagic
);
2259 if (get_modinfo(info
, "staging")) {
2260 add_taint_module(mod
, TAINT_CRAP
);
2261 printk(KERN_WARNING
"%s: module is from the staging directory,"
2262 " the quality is unknown, you have been warned.\n",
2266 /* Set up license info based on the info section */
2267 set_license(mod
, get_modinfo(info
, "license"));
2272 static void find_module_sections(struct module
*mod
, struct load_info
*info
)
2274 mod
->kp
= section_objs(info
, "__param",
2275 sizeof(*mod
->kp
), &mod
->num_kp
);
2276 mod
->syms
= section_objs(info
, "__ksymtab",
2277 sizeof(*mod
->syms
), &mod
->num_syms
);
2278 mod
->crcs
= section_addr(info
, "__kcrctab");
2279 mod
->gpl_syms
= section_objs(info
, "__ksymtab_gpl",
2280 sizeof(*mod
->gpl_syms
),
2281 &mod
->num_gpl_syms
);
2282 mod
->gpl_crcs
= section_addr(info
, "__kcrctab_gpl");
2283 mod
->gpl_future_syms
= section_objs(info
,
2284 "__ksymtab_gpl_future",
2285 sizeof(*mod
->gpl_future_syms
),
2286 &mod
->num_gpl_future_syms
);
2287 mod
->gpl_future_crcs
= section_addr(info
, "__kcrctab_gpl_future");
2289 #ifdef CONFIG_UNUSED_SYMBOLS
2290 mod
->unused_syms
= section_objs(info
, "__ksymtab_unused",
2291 sizeof(*mod
->unused_syms
),
2292 &mod
->num_unused_syms
);
2293 mod
->unused_crcs
= section_addr(info
, "__kcrctab_unused");
2294 mod
->unused_gpl_syms
= section_objs(info
, "__ksymtab_unused_gpl",
2295 sizeof(*mod
->unused_gpl_syms
),
2296 &mod
->num_unused_gpl_syms
);
2297 mod
->unused_gpl_crcs
= section_addr(info
, "__kcrctab_unused_gpl");
2299 #ifdef CONFIG_CONSTRUCTORS
2300 mod
->ctors
= section_objs(info
, ".ctors",
2301 sizeof(*mod
->ctors
), &mod
->num_ctors
);
2304 #ifdef CONFIG_TRACEPOINTS
2305 mod
->tracepoints
= section_objs(info
, "__tracepoints",
2306 sizeof(*mod
->tracepoints
),
2307 &mod
->num_tracepoints
);
2309 #ifdef CONFIG_EVENT_TRACING
2310 mod
->trace_events
= section_objs(info
, "_ftrace_events",
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(info
, "__mcount_loc",
2323 sizeof(*mod
->ftrace_callsites
),
2324 &mod
->num_ftrace_callsites
);
2327 mod
->extable
= section_objs(info
, "__ex_table",
2328 sizeof(*mod
->extable
), &mod
->num_exentries
);
2330 if (section_addr(info
, "__obsparm"))
2331 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2334 info
->debug
= section_objs(info
, "__verbose",
2335 sizeof(*info
->debug
), &info
->num_debug
);
2338 static int move_module(struct module
*mod
, struct load_info
*info
)
2343 /* Do the allocs. */
2344 ptr
= module_alloc_update_bounds(mod
->core_size
);
2346 * The pointer to this block is stored in the module structure
2347 * which is inside the block. Just mark it as not being a
2350 kmemleak_not_leak(ptr
);
2354 memset(ptr
, 0, mod
->core_size
);
2355 mod
->module_core
= ptr
;
2357 ptr
= module_alloc_update_bounds(mod
->init_size
);
2359 * The pointer to this block is stored in the module structure
2360 * which is inside the block. This block doesn't need to be
2361 * scanned as it contains data and code that will be freed
2362 * after the module is initialized.
2364 kmemleak_ignore(ptr
);
2365 if (!ptr
&& mod
->init_size
) {
2366 module_free(mod
, mod
->module_core
);
2369 memset(ptr
, 0, mod
->init_size
);
2370 mod
->module_init
= ptr
;
2372 /* Transfer each section which specifies SHF_ALLOC */
2373 DEBUGP("final section addresses:\n");
2374 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
2376 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2378 if (!(shdr
->sh_flags
& SHF_ALLOC
))
2381 if (shdr
->sh_entsize
& INIT_OFFSET_MASK
)
2382 dest
= mod
->module_init
2383 + (shdr
->sh_entsize
& ~INIT_OFFSET_MASK
);
2385 dest
= mod
->module_core
+ shdr
->sh_entsize
;
2387 if (shdr
->sh_type
!= SHT_NOBITS
)
2388 memcpy(dest
, (void *)shdr
->sh_addr
, shdr
->sh_size
);
2389 /* Update sh_addr to point to copy in image. */
2390 shdr
->sh_addr
= (unsigned long)dest
;
2391 DEBUGP("\t0x%lx %s\n",
2392 shdr
->sh_addr
, info
->secstrings
+ shdr
->sh_name
);
2398 static int check_module_license_and_versions(struct module
*mod
)
2401 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2402 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2403 * using GPL-only symbols it needs.
2405 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2406 add_taint(TAINT_PROPRIETARY_MODULE
);
2408 /* driverloader was caught wrongly pretending to be under GPL */
2409 if (strcmp(mod
->name
, "driverloader") == 0)
2410 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2412 #ifdef CONFIG_MODVERSIONS
2413 if ((mod
->num_syms
&& !mod
->crcs
)
2414 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2415 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2416 #ifdef CONFIG_UNUSED_SYMBOLS
2417 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2418 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2421 return try_to_force_load(mod
,
2422 "no versions for exported symbols");
2428 static void flush_module_icache(const struct module
*mod
)
2430 mm_segment_t old_fs
;
2432 /* flush the icache in correct context */
2437 * Flush the instruction cache, since we've played with text.
2438 * Do it before processing of module parameters, so the module
2439 * can provide parameter accessor functions of its own.
2441 if (mod
->module_init
)
2442 flush_icache_range((unsigned long)mod
->module_init
,
2443 (unsigned long)mod
->module_init
2445 flush_icache_range((unsigned long)mod
->module_core
,
2446 (unsigned long)mod
->module_core
+ mod
->core_size
);
2451 static struct module
*layout_and_allocate(struct load_info
*info
)
2453 /* Module within temporary copy. */
2458 mod
= setup_load_info(info
);
2462 err
= check_modinfo(mod
, info
);
2464 return ERR_PTR(err
);
2466 /* Allow arches to frob section contents and sizes. */
2467 err
= module_frob_arch_sections(info
->hdr
, info
->sechdrs
,
2468 info
->secstrings
, mod
);
2472 pcpusec
= &info
->sechdrs
[info
->index
.pcpu
];
2473 if (pcpusec
->sh_size
) {
2474 /* We have a special allocation for this section. */
2475 err
= percpu_modalloc(mod
,
2476 pcpusec
->sh_size
, pcpusec
->sh_addralign
);
2479 pcpusec
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2482 /* Determine total sizes, and put offsets in sh_entsize. For now
2483 this is done generically; there doesn't appear to be any
2484 special cases for the architectures. */
2485 layout_sections(mod
, info
);
2487 info
->strmap
= kzalloc(BITS_TO_LONGS(info
->sechdrs
[info
->index
.str
].sh_size
)
2488 * sizeof(long), GFP_KERNEL
);
2489 if (!info
->strmap
) {
2493 layout_symtab(mod
, info
);
2495 /* Allocate and move to the final place */
2496 err
= move_module(mod
, info
);
2500 /* Module has been copied to its final place now: return it. */
2501 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2502 kmemleak_load_module(mod
, info
);
2506 kfree(info
->strmap
);
2508 percpu_modfree(mod
);
2510 return ERR_PTR(err
);
2513 /* mod is no longer valid after this! */
2514 static void module_deallocate(struct module
*mod
, struct load_info
*info
)
2516 kfree(info
->strmap
);
2517 percpu_modfree(mod
);
2518 module_free(mod
, mod
->module_init
);
2519 module_free(mod
, mod
->module_core
);
2522 static int post_relocation(struct module
*mod
, const struct load_info
*info
)
2524 /* Sort exception table now relocations are done. */
2525 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2527 /* Copy relocated percpu area over. */
2528 percpu_modcopy(mod
, (void *)info
->sechdrs
[info
->index
.pcpu
].sh_addr
,
2529 info
->sechdrs
[info
->index
.pcpu
].sh_size
);
2531 /* Setup kallsyms-specific fields. */
2532 add_kallsyms(mod
, info
);
2534 /* Arch-specific module finalizing. */
2535 return module_finalize(info
->hdr
, info
->sechdrs
, mod
);
2538 /* Allocate and load the module: note that size of section 0 is always
2539 zero, and we rely on this for optional sections. */
2540 static struct module
*load_module(void __user
*umod
,
2542 const char __user
*uargs
)
2544 struct load_info info
= { NULL
, };
2548 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2551 /* Copy in the blobs from userspace, check they are vaguely sane. */
2552 err
= copy_and_check(&info
, umod
, len
, uargs
);
2554 return ERR_PTR(err
);
2556 /* Figure out module layout, and allocate all the memory. */
2557 mod
= layout_and_allocate(&info
);
2563 /* Now module is in final location, initialize linked lists, etc. */
2564 err
= module_unload_init(mod
);
2568 /* Now we've got everything in the final locations, we can
2569 * find optional sections. */
2570 find_module_sections(mod
, &info
);
2572 err
= check_module_license_and_versions(mod
);
2576 /* Set up MODINFO_ATTR fields */
2577 setup_modinfo(mod
, &info
);
2579 /* Fix up syms, so that st_value is a pointer to location. */
2580 err
= simplify_symbols(mod
, &info
);
2584 err
= apply_relocations(mod
, &info
);
2588 err
= post_relocation(mod
, &info
);
2592 flush_module_icache(mod
);
2594 /* Now copy in args */
2595 mod
->args
= strndup_user(uargs
, ~0UL >> 1);
2596 if (IS_ERR(mod
->args
)) {
2597 err
= PTR_ERR(mod
->args
);
2598 goto free_arch_cleanup
;
2601 /* Mark state as coming so strong_try_module_get() ignores us. */
2602 mod
->state
= MODULE_STATE_COMING
;
2604 /* Now sew it into the lists so we can get lockdep and oops
2605 * info during argument parsing. Noone should access us, since
2606 * strong_try_module_get() will fail.
2607 * lockdep/oops can run asynchronous, so use the RCU list insertion
2608 * function to insert in a way safe to concurrent readers.
2609 * The mutex protects against concurrent writers.
2611 mutex_lock(&module_mutex
);
2612 if (find_module(mod
->name
)) {
2617 /* This has to be done once we're sure module name is unique. */
2619 dynamic_debug_setup(info
.debug
, info
.num_debug
);
2621 /* Find duplicate symbols */
2622 err
= verify_export_symbols(mod
);
2626 module_bug_finalize(info
.hdr
, info
.sechdrs
, mod
);
2627 list_add_rcu(&mod
->list
, &modules
);
2628 mutex_unlock(&module_mutex
);
2630 /* Module is ready to execute: parsing args may do that. */
2631 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
, NULL
);
2635 /* Link in to syfs. */
2636 err
= mod_sysfs_setup(mod
, &info
, mod
->kp
, mod
->num_kp
);
2640 /* Get rid of temporary copy and strmap. */
2645 trace_module_load(mod
);
2649 mutex_lock(&module_mutex
);
2650 /* Unlink carefully: kallsyms could be walking list. */
2651 list_del_rcu(&mod
->list
);
2652 module_bug_cleanup(mod
);
2656 dynamic_debug_remove(info
.debug
);
2658 mutex_unlock(&module_mutex
);
2659 synchronize_sched();
2662 module_arch_cleanup(mod
);
2666 module_unload_free(mod
);
2668 module_deallocate(mod
, &info
);
2671 return ERR_PTR(err
);
2674 /* Call module constructors. */
2675 static void do_mod_ctors(struct module
*mod
)
2677 #ifdef CONFIG_CONSTRUCTORS
2680 for (i
= 0; i
< mod
->num_ctors
; i
++)
2685 /* This is where the real work happens */
2686 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
2687 unsigned long, len
, const char __user
*, uargs
)
2692 /* Must have permission */
2693 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
2696 /* Do all the hard work */
2697 mod
= load_module(umod
, len
, uargs
);
2699 return PTR_ERR(mod
);
2701 blocking_notifier_call_chain(&module_notify_list
,
2702 MODULE_STATE_COMING
, mod
);
2705 /* Start the module */
2706 if (mod
->init
!= NULL
)
2707 ret
= do_one_initcall(mod
->init
);
2709 /* Init routine failed: abort. Try to protect us from
2710 buggy refcounters. */
2711 mod
->state
= MODULE_STATE_GOING
;
2712 synchronize_sched();
2714 blocking_notifier_call_chain(&module_notify_list
,
2715 MODULE_STATE_GOING
, mod
);
2717 wake_up(&module_wq
);
2722 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2723 "%s: loading module anyway...\n",
2724 __func__
, mod
->name
, ret
,
2729 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2730 mod
->state
= MODULE_STATE_LIVE
;
2731 wake_up(&module_wq
);
2732 blocking_notifier_call_chain(&module_notify_list
,
2733 MODULE_STATE_LIVE
, mod
);
2735 /* We need to finish all async code before the module init sequence is done */
2736 async_synchronize_full();
2738 mutex_lock(&module_mutex
);
2739 /* Drop initial reference. */
2741 trim_init_extable(mod
);
2742 #ifdef CONFIG_KALLSYMS
2743 mod
->num_symtab
= mod
->core_num_syms
;
2744 mod
->symtab
= mod
->core_symtab
;
2745 mod
->strtab
= mod
->core_strtab
;
2747 module_free(mod
, mod
->module_init
);
2748 mod
->module_init
= NULL
;
2750 mod
->init_text_size
= 0;
2751 mutex_unlock(&module_mutex
);
2756 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2758 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2761 #ifdef CONFIG_KALLSYMS
2763 * This ignores the intensely annoying "mapping symbols" found
2764 * in ARM ELF files: $a, $t and $d.
2766 static inline int is_arm_mapping_symbol(const char *str
)
2768 return str
[0] == '$' && strchr("atd", str
[1])
2769 && (str
[2] == '\0' || str
[2] == '.');
2772 static const char *get_ksymbol(struct module
*mod
,
2774 unsigned long *size
,
2775 unsigned long *offset
)
2777 unsigned int i
, best
= 0;
2778 unsigned long nextval
;
2780 /* At worse, next value is at end of module */
2781 if (within_module_init(addr
, mod
))
2782 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2784 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2786 /* Scan for closest preceeding symbol, and next symbol. (ELF
2787 starts real symbols at 1). */
2788 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2789 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2792 /* We ignore unnamed symbols: they're uninformative
2793 * and inserted at a whim. */
2794 if (mod
->symtab
[i
].st_value
<= addr
2795 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2796 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2797 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2799 if (mod
->symtab
[i
].st_value
> addr
2800 && mod
->symtab
[i
].st_value
< nextval
2801 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2802 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2803 nextval
= mod
->symtab
[i
].st_value
;
2810 *size
= nextval
- mod
->symtab
[best
].st_value
;
2812 *offset
= addr
- mod
->symtab
[best
].st_value
;
2813 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
2816 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2817 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2818 const char *module_address_lookup(unsigned long addr
,
2819 unsigned long *size
,
2820 unsigned long *offset
,
2825 const char *ret
= NULL
;
2828 list_for_each_entry_rcu(mod
, &modules
, list
) {
2829 if (within_module_init(addr
, mod
) ||
2830 within_module_core(addr
, mod
)) {
2832 *modname
= mod
->name
;
2833 ret
= get_ksymbol(mod
, addr
, size
, offset
);
2837 /* Make a copy in here where it's safe */
2839 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
2846 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
2851 list_for_each_entry_rcu(mod
, &modules
, list
) {
2852 if (within_module_init(addr
, mod
) ||
2853 within_module_core(addr
, mod
)) {
2856 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
2859 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
2869 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
2870 unsigned long *offset
, char *modname
, char *name
)
2875 list_for_each_entry_rcu(mod
, &modules
, list
) {
2876 if (within_module_init(addr
, mod
) ||
2877 within_module_core(addr
, mod
)) {
2880 sym
= get_ksymbol(mod
, addr
, size
, offset
);
2884 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
2886 strlcpy(name
, sym
, KSYM_NAME_LEN
);
2896 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
2897 char *name
, char *module_name
, int *exported
)
2902 list_for_each_entry_rcu(mod
, &modules
, list
) {
2903 if (symnum
< mod
->num_symtab
) {
2904 *value
= mod
->symtab
[symnum
].st_value
;
2905 *type
= mod
->symtab
[symnum
].st_info
;
2906 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
2908 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
2909 *exported
= is_exported(name
, *value
, mod
);
2913 symnum
-= mod
->num_symtab
;
2919 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
2923 for (i
= 0; i
< mod
->num_symtab
; i
++)
2924 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
2925 mod
->symtab
[i
].st_info
!= 'U')
2926 return mod
->symtab
[i
].st_value
;
2930 /* Look for this name: can be of form module:name. */
2931 unsigned long module_kallsyms_lookup_name(const char *name
)
2935 unsigned long ret
= 0;
2937 /* Don't lock: we're in enough trouble already. */
2939 if ((colon
= strchr(name
, ':')) != NULL
) {
2941 if ((mod
= find_module(name
)) != NULL
)
2942 ret
= mod_find_symname(mod
, colon
+1);
2945 list_for_each_entry_rcu(mod
, &modules
, list
)
2946 if ((ret
= mod_find_symname(mod
, name
)) != 0)
2953 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
2954 struct module
*, unsigned long),
2961 list_for_each_entry(mod
, &modules
, list
) {
2962 for (i
= 0; i
< mod
->num_symtab
; i
++) {
2963 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
2964 mod
, mod
->symtab
[i
].st_value
);
2971 #endif /* CONFIG_KALLSYMS */
2973 static char *module_flags(struct module
*mod
, char *buf
)
2978 mod
->state
== MODULE_STATE_GOING
||
2979 mod
->state
== MODULE_STATE_COMING
) {
2981 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
2983 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
2985 if (mod
->taints
& (1 << TAINT_CRAP
))
2988 * TAINT_FORCED_RMMOD: could be added.
2989 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2993 /* Show a - for module-is-being-unloaded */
2994 if (mod
->state
== MODULE_STATE_GOING
)
2996 /* Show a + for module-is-being-loaded */
2997 if (mod
->state
== MODULE_STATE_COMING
)
3006 #ifdef CONFIG_PROC_FS
3007 /* Called by the /proc file system to return a list of modules. */
3008 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
3010 mutex_lock(&module_mutex
);
3011 return seq_list_start(&modules
, *pos
);
3014 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
3016 return seq_list_next(p
, &modules
, pos
);
3019 static void m_stop(struct seq_file
*m
, void *p
)
3021 mutex_unlock(&module_mutex
);
3024 static int m_show(struct seq_file
*m
, void *p
)
3026 struct module
*mod
= list_entry(p
, struct module
, list
);
3029 seq_printf(m
, "%s %u",
3030 mod
->name
, mod
->init_size
+ mod
->core_size
);
3031 print_unload_info(m
, mod
);
3033 /* Informative for users. */
3034 seq_printf(m
, " %s",
3035 mod
->state
== MODULE_STATE_GOING
? "Unloading":
3036 mod
->state
== MODULE_STATE_COMING
? "Loading":
3038 /* Used by oprofile and other similar tools. */
3039 seq_printf(m
, " 0x%p", mod
->module_core
);
3043 seq_printf(m
, " %s", module_flags(mod
, buf
));
3045 seq_printf(m
, "\n");
3049 /* Format: modulename size refcount deps address
3051 Where refcount is a number or -, and deps is a comma-separated list
3054 static const struct seq_operations modules_op
= {
3061 static int modules_open(struct inode
*inode
, struct file
*file
)
3063 return seq_open(file
, &modules_op
);
3066 static const struct file_operations proc_modules_operations
= {
3067 .open
= modules_open
,
3069 .llseek
= seq_lseek
,
3070 .release
= seq_release
,
3073 static int __init
proc_modules_init(void)
3075 proc_create("modules", 0, NULL
, &proc_modules_operations
);
3078 module_init(proc_modules_init
);
3081 /* Given an address, look for it in the module exception tables. */
3082 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
3084 const struct exception_table_entry
*e
= NULL
;
3088 list_for_each_entry_rcu(mod
, &modules
, list
) {
3089 if (mod
->num_exentries
== 0)
3092 e
= search_extable(mod
->extable
,
3093 mod
->extable
+ mod
->num_exentries
- 1,
3100 /* Now, if we found one, we are running inside it now, hence
3101 we cannot unload the module, hence no refcnt needed. */
3106 * is_module_address - is this address inside a module?
3107 * @addr: the address to check.
3109 * See is_module_text_address() if you simply want to see if the address
3110 * is code (not data).
3112 bool is_module_address(unsigned long addr
)
3117 ret
= __module_address(addr
) != NULL
;
3124 * __module_address - get the module which contains an address.
3125 * @addr: the address.
3127 * Must be called with preempt disabled or module mutex held so that
3128 * module doesn't get freed during this.
3130 struct module
*__module_address(unsigned long addr
)
3134 if (addr
< module_addr_min
|| addr
> module_addr_max
)
3137 list_for_each_entry_rcu(mod
, &modules
, list
)
3138 if (within_module_core(addr
, mod
)
3139 || within_module_init(addr
, mod
))
3143 EXPORT_SYMBOL_GPL(__module_address
);
3146 * is_module_text_address - is this address inside module code?
3147 * @addr: the address to check.
3149 * See is_module_address() if you simply want to see if the address is
3150 * anywhere in a module. See kernel_text_address() for testing if an
3151 * address corresponds to kernel or module code.
3153 bool is_module_text_address(unsigned long addr
)
3158 ret
= __module_text_address(addr
) != NULL
;
3165 * __module_text_address - get the module whose code contains an address.
3166 * @addr: the address.
3168 * Must be called with preempt disabled or module mutex held so that
3169 * module doesn't get freed during this.
3171 struct module
*__module_text_address(unsigned long addr
)
3173 struct module
*mod
= __module_address(addr
);
3175 /* Make sure it's within the text section. */
3176 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
3177 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
3182 EXPORT_SYMBOL_GPL(__module_text_address
);
3184 /* Don't grab lock, we're oopsing. */
3185 void print_modules(void)
3190 printk(KERN_DEFAULT
"Modules linked in:");
3191 /* Most callers should already have preempt disabled, but make sure */
3193 list_for_each_entry_rcu(mod
, &modules
, list
)
3194 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
3196 if (last_unloaded_module
[0])
3197 printk(" [last unloaded: %s]", last_unloaded_module
);
3201 #ifdef CONFIG_MODVERSIONS
3202 /* Generate the signature for all relevant module structures here.
3203 * If these change, we don't want to try to parse the module. */
3204 void module_layout(struct module
*mod
,
3205 struct modversion_info
*ver
,
3206 struct kernel_param
*kp
,
3207 struct kernel_symbol
*ks
,
3208 struct tracepoint
*tp
)
3211 EXPORT_SYMBOL(module_layout
);
3214 #ifdef CONFIG_TRACEPOINTS
3215 void module_update_tracepoints(void)
3219 mutex_lock(&module_mutex
);
3220 list_for_each_entry(mod
, &modules
, list
)
3222 tracepoint_update_probe_range(mod
->tracepoints
,
3223 mod
->tracepoints
+ mod
->num_tracepoints
);
3224 mutex_unlock(&module_mutex
);
3228 * Returns 0 if current not found.
3229 * Returns 1 if current found.
3231 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
3233 struct module
*iter_mod
;
3236 mutex_lock(&module_mutex
);
3237 list_for_each_entry(iter_mod
, &modules
, list
) {
3238 if (!iter_mod
->taints
) {
3240 * Sorted module list
3242 if (iter_mod
< iter
->module
)
3244 else if (iter_mod
> iter
->module
)
3245 iter
->tracepoint
= NULL
;
3246 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
3247 iter_mod
->tracepoints
,
3248 iter_mod
->tracepoints
3249 + iter_mod
->num_tracepoints
);
3251 iter
->module
= iter_mod
;
3256 mutex_unlock(&module_mutex
);