2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002, 2010 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>
58 #include <linux/jump_label.h>
59 #include <linux/pfn.h>
61 #define CREATE_TRACE_POINTS
62 #include <trace/events/module.h>
67 #define DEBUGP(fmt , a...)
70 #ifndef ARCH_SHF_SMALL
71 #define ARCH_SHF_SMALL 0
75 * Modules' sections will be aligned on page boundaries
76 * to ensure complete separation of code and data, but
77 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
79 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
80 # define debug_align(X) ALIGN(X, PAGE_SIZE)
82 # define debug_align(X) (X)
86 * Given BASE and SIZE this macro calculates the number of pages the
87 * memory regions occupies
89 #define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
90 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
91 PFN_DOWN((unsigned long)BASE) + 1) \
94 /* If this is set, the section belongs in the init part of the module */
95 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
99 * 1) List of modules (also safely readable with preempt_disable),
100 * 2) module_use links,
101 * 3) module_addr_min/module_addr_max.
102 * (delete uses stop_machine/add uses RCU list operations). */
103 DEFINE_MUTEX(module_mutex
);
104 EXPORT_SYMBOL_GPL(module_mutex
);
105 static LIST_HEAD(modules
);
106 #ifdef CONFIG_KGDB_KDB
107 struct list_head
*kdb_modules
= &modules
; /* kdb needs the list of modules */
108 #endif /* CONFIG_KGDB_KDB */
111 /* Block module loading/unloading? */
112 int modules_disabled
= 0;
114 /* Waiting for a module to finish initializing? */
115 static DECLARE_WAIT_QUEUE_HEAD(module_wq
);
117 static BLOCKING_NOTIFIER_HEAD(module_notify_list
);
119 /* Bounds of module allocation, for speeding __module_address.
120 * Protected by module_mutex. */
121 static unsigned long module_addr_min
= -1UL, module_addr_max
= 0;
123 int register_module_notifier(struct notifier_block
* nb
)
125 return blocking_notifier_chain_register(&module_notify_list
, nb
);
127 EXPORT_SYMBOL(register_module_notifier
);
129 int unregister_module_notifier(struct notifier_block
* nb
)
131 return blocking_notifier_chain_unregister(&module_notify_list
, nb
);
133 EXPORT_SYMBOL(unregister_module_notifier
);
139 char *secstrings
, *strtab
;
140 unsigned long *strmap
;
141 unsigned long symoffs
, stroffs
;
142 struct _ddebug
*debug
;
143 unsigned int num_debug
;
145 unsigned int sym
, str
, mod
, vers
, info
, pcpu
;
149 /* We require a truly strong try_module_get(): 0 means failure due to
150 ongoing or failed initialization etc. */
151 static inline int strong_try_module_get(struct module
*mod
)
153 if (mod
&& mod
->state
== MODULE_STATE_COMING
)
155 if (try_module_get(mod
))
161 static inline void add_taint_module(struct module
*mod
, unsigned flag
)
164 mod
->taints
|= (1U << flag
);
168 * A thread that wants to hold a reference to a module only while it
169 * is running can call this to safely exit. nfsd and lockd use this.
171 void __module_put_and_exit(struct module
*mod
, long code
)
176 EXPORT_SYMBOL(__module_put_and_exit
);
178 /* Find a module section: 0 means not found. */
179 static unsigned int find_sec(const struct load_info
*info
, const char *name
)
183 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
184 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
185 /* Alloc bit cleared means "ignore it." */
186 if ((shdr
->sh_flags
& SHF_ALLOC
)
187 && strcmp(info
->secstrings
+ shdr
->sh_name
, name
) == 0)
193 /* Find a module section, or NULL. */
194 static void *section_addr(const struct load_info
*info
, const char *name
)
196 /* Section 0 has sh_addr 0. */
197 return (void *)info
->sechdrs
[find_sec(info
, name
)].sh_addr
;
200 /* Find a module section, or NULL. Fill in number of "objects" in section. */
201 static void *section_objs(const struct load_info
*info
,
206 unsigned int sec
= find_sec(info
, name
);
208 /* Section 0 has sh_addr 0 and sh_size 0. */
209 *num
= info
->sechdrs
[sec
].sh_size
/ object_size
;
210 return (void *)info
->sechdrs
[sec
].sh_addr
;
213 /* Provided by the linker */
214 extern const struct kernel_symbol __start___ksymtab
[];
215 extern const struct kernel_symbol __stop___ksymtab
[];
216 extern const struct kernel_symbol __start___ksymtab_gpl
[];
217 extern const struct kernel_symbol __stop___ksymtab_gpl
[];
218 extern const struct kernel_symbol __start___ksymtab_gpl_future
[];
219 extern const struct kernel_symbol __stop___ksymtab_gpl_future
[];
220 extern const unsigned long __start___kcrctab
[];
221 extern const unsigned long __start___kcrctab_gpl
[];
222 extern const unsigned long __start___kcrctab_gpl_future
[];
223 #ifdef CONFIG_UNUSED_SYMBOLS
224 extern const struct kernel_symbol __start___ksymtab_unused
[];
225 extern const struct kernel_symbol __stop___ksymtab_unused
[];
226 extern const struct kernel_symbol __start___ksymtab_unused_gpl
[];
227 extern const struct kernel_symbol __stop___ksymtab_unused_gpl
[];
228 extern const unsigned long __start___kcrctab_unused
[];
229 extern const unsigned long __start___kcrctab_unused_gpl
[];
232 #ifndef CONFIG_MODVERSIONS
233 #define symversion(base, idx) NULL
235 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
238 static bool each_symbol_in_section(const struct symsearch
*arr
,
239 unsigned int arrsize
,
240 struct module
*owner
,
241 bool (*fn
)(const struct symsearch
*syms
,
242 struct module
*owner
,
248 for (j
= 0; j
< arrsize
; j
++) {
249 if (fn(&arr
[j
], owner
, data
))
256 /* Returns true as soon as fn returns true, otherwise false. */
257 bool each_symbol_section(bool (*fn
)(const struct symsearch
*arr
,
258 struct module
*owner
,
263 static const struct symsearch arr
[] = {
264 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
265 NOT_GPL_ONLY
, false },
266 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
267 __start___kcrctab_gpl
,
269 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
270 __start___kcrctab_gpl_future
,
271 WILL_BE_GPL_ONLY
, false },
272 #ifdef CONFIG_UNUSED_SYMBOLS
273 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
274 __start___kcrctab_unused
,
275 NOT_GPL_ONLY
, true },
276 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
277 __start___kcrctab_unused_gpl
,
282 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
285 list_for_each_entry_rcu(mod
, &modules
, list
) {
286 struct symsearch arr
[] = {
287 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
288 NOT_GPL_ONLY
, false },
289 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
292 { mod
->gpl_future_syms
,
293 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
294 mod
->gpl_future_crcs
,
295 WILL_BE_GPL_ONLY
, false },
296 #ifdef CONFIG_UNUSED_SYMBOLS
298 mod
->unused_syms
+ mod
->num_unused_syms
,
300 NOT_GPL_ONLY
, true },
301 { mod
->unused_gpl_syms
,
302 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
303 mod
->unused_gpl_crcs
,
308 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
313 EXPORT_SYMBOL_GPL(each_symbol_section
);
315 struct find_symbol_arg
{
322 struct module
*owner
;
323 const unsigned long *crc
;
324 const struct kernel_symbol
*sym
;
327 static bool check_symbol(const struct symsearch
*syms
,
328 struct module
*owner
,
329 unsigned int symnum
, void *data
)
331 struct find_symbol_arg
*fsa
= data
;
334 if (syms
->licence
== GPL_ONLY
)
336 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
337 printk(KERN_WARNING
"Symbol %s is being used "
338 "by a non-GPL module, which will not "
339 "be allowed in the future\n", fsa
->name
);
340 printk(KERN_WARNING
"Please see the file "
341 "Documentation/feature-removal-schedule.txt "
342 "in the kernel source tree for more details.\n");
346 #ifdef CONFIG_UNUSED_SYMBOLS
347 if (syms
->unused
&& fsa
->warn
) {
348 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
349 "however this module is using it.\n", fsa
->name
);
351 "This symbol will go away in the future.\n");
353 "Please evalute if this is the right api to use and if "
354 "it really is, submit a report the linux kernel "
355 "mailinglist together with submitting your code for "
361 fsa
->crc
= symversion(syms
->crcs
, symnum
);
362 fsa
->sym
= &syms
->start
[symnum
];
366 static bool find_symbol_in_section(const struct symsearch
*syms
,
367 struct module
*owner
,
370 struct find_symbol_arg
*fsa
= data
;
373 for (i
= 0; i
< syms
->stop
- syms
->start
; i
++) {
374 if (strcmp(syms
->start
[i
].name
, fsa
->name
) == 0)
375 return check_symbol(syms
, owner
, i
, data
);
380 /* Find a symbol and return it, along with, (optional) crc and
381 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
382 const struct kernel_symbol
*find_symbol(const char *name
,
383 struct module
**owner
,
384 const unsigned long **crc
,
388 struct find_symbol_arg fsa
;
394 if (each_symbol_section(find_symbol_in_section
, &fsa
)) {
402 DEBUGP("Failed to find symbol %s\n", name
);
405 EXPORT_SYMBOL_GPL(find_symbol
);
407 /* Search for module by name: must hold module_mutex. */
408 struct module
*find_module(const char *name
)
412 list_for_each_entry(mod
, &modules
, list
) {
413 if (strcmp(mod
->name
, name
) == 0)
418 EXPORT_SYMBOL_GPL(find_module
);
422 static inline void __percpu
*mod_percpu(struct module
*mod
)
427 static int percpu_modalloc(struct module
*mod
,
428 unsigned long size
, unsigned long align
)
430 if (align
> PAGE_SIZE
) {
431 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
432 mod
->name
, align
, PAGE_SIZE
);
436 mod
->percpu
= __alloc_reserved_percpu(size
, align
);
439 "%s: Could not allocate %lu bytes percpu data\n",
443 mod
->percpu_size
= size
;
447 static void percpu_modfree(struct module
*mod
)
449 free_percpu(mod
->percpu
);
452 static unsigned int find_pcpusec(struct load_info
*info
)
454 return find_sec(info
, ".data..percpu");
457 static void percpu_modcopy(struct module
*mod
,
458 const void *from
, unsigned long size
)
462 for_each_possible_cpu(cpu
)
463 memcpy(per_cpu_ptr(mod
->percpu
, cpu
), from
, size
);
467 * is_module_percpu_address - test whether address is from module static percpu
468 * @addr: address to test
470 * Test whether @addr belongs to module static percpu area.
473 * %true if @addr is from module static percpu area
475 bool is_module_percpu_address(unsigned long addr
)
482 list_for_each_entry_rcu(mod
, &modules
, list
) {
483 if (!mod
->percpu_size
)
485 for_each_possible_cpu(cpu
) {
486 void *start
= per_cpu_ptr(mod
->percpu
, cpu
);
488 if ((void *)addr
>= start
&&
489 (void *)addr
< start
+ mod
->percpu_size
) {
500 #else /* ... !CONFIG_SMP */
502 static inline void __percpu
*mod_percpu(struct module
*mod
)
506 static inline int percpu_modalloc(struct module
*mod
,
507 unsigned long size
, unsigned long align
)
511 static inline void percpu_modfree(struct module
*mod
)
514 static unsigned int find_pcpusec(struct load_info
*info
)
518 static inline void percpu_modcopy(struct module
*mod
,
519 const void *from
, unsigned long size
)
521 /* pcpusec should be 0, and size of that section should be 0. */
524 bool is_module_percpu_address(unsigned long addr
)
529 #endif /* CONFIG_SMP */
531 #define MODINFO_ATTR(field) \
532 static void setup_modinfo_##field(struct module *mod, const char *s) \
534 mod->field = kstrdup(s, GFP_KERNEL); \
536 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
537 struct module *mod, char *buffer) \
539 return sprintf(buffer, "%s\n", mod->field); \
541 static int modinfo_##field##_exists(struct module *mod) \
543 return mod->field != NULL; \
545 static void free_modinfo_##field(struct module *mod) \
550 static struct module_attribute modinfo_##field = { \
551 .attr = { .name = __stringify(field), .mode = 0444 }, \
552 .show = show_modinfo_##field, \
553 .setup = setup_modinfo_##field, \
554 .test = modinfo_##field##_exists, \
555 .free = free_modinfo_##field, \
558 MODINFO_ATTR(version
);
559 MODINFO_ATTR(srcversion
);
561 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
563 #ifdef CONFIG_MODULE_UNLOAD
565 EXPORT_TRACEPOINT_SYMBOL(module_get
);
567 /* Init the unload section of the module. */
568 static int module_unload_init(struct module
*mod
)
570 mod
->refptr
= alloc_percpu(struct module_ref
);
574 INIT_LIST_HEAD(&mod
->source_list
);
575 INIT_LIST_HEAD(&mod
->target_list
);
577 /* Hold reference count during initialization. */
578 __this_cpu_write(mod
->refptr
->incs
, 1);
579 /* Backwards compatibility macros put refcount during init. */
580 mod
->waiter
= current
;
585 /* Does a already use b? */
586 static int already_uses(struct module
*a
, struct module
*b
)
588 struct module_use
*use
;
590 list_for_each_entry(use
, &b
->source_list
, source_list
) {
591 if (use
->source
== a
) {
592 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
596 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
602 * - we add 'a' as a "source", 'b' as a "target" of module use
603 * - the module_use is added to the list of 'b' sources (so
604 * 'b' can walk the list to see who sourced them), and of 'a'
605 * targets (so 'a' can see what modules it targets).
607 static int add_module_usage(struct module
*a
, struct module
*b
)
609 struct module_use
*use
;
611 DEBUGP("Allocating new usage for %s.\n", a
->name
);
612 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
614 printk(KERN_WARNING
"%s: out of memory loading\n", a
->name
);
620 list_add(&use
->source_list
, &b
->source_list
);
621 list_add(&use
->target_list
, &a
->target_list
);
625 /* Module a uses b: caller needs module_mutex() */
626 int ref_module(struct module
*a
, struct module
*b
)
630 if (b
== NULL
|| already_uses(a
, b
))
633 /* If module isn't available, we fail. */
634 err
= strong_try_module_get(b
);
638 err
= add_module_usage(a
, b
);
645 EXPORT_SYMBOL_GPL(ref_module
);
647 /* Clear the unload stuff of the module. */
648 static void module_unload_free(struct module
*mod
)
650 struct module_use
*use
, *tmp
;
652 mutex_lock(&module_mutex
);
653 list_for_each_entry_safe(use
, tmp
, &mod
->target_list
, target_list
) {
654 struct module
*i
= use
->target
;
655 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
657 list_del(&use
->source_list
);
658 list_del(&use
->target_list
);
661 mutex_unlock(&module_mutex
);
663 free_percpu(mod
->refptr
);
666 #ifdef CONFIG_MODULE_FORCE_UNLOAD
667 static inline int try_force_unload(unsigned int flags
)
669 int ret
= (flags
& O_TRUNC
);
671 add_taint(TAINT_FORCED_RMMOD
);
675 static inline int try_force_unload(unsigned int flags
)
679 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
688 /* Whole machine is stopped with interrupts off when this runs. */
689 static int __try_stop_module(void *_sref
)
691 struct stopref
*sref
= _sref
;
693 /* If it's not unused, quit unless we're forcing. */
694 if (module_refcount(sref
->mod
) != 0) {
695 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
699 /* Mark it as dying. */
700 sref
->mod
->state
= MODULE_STATE_GOING
;
704 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
706 if (flags
& O_NONBLOCK
) {
707 struct stopref sref
= { mod
, flags
, forced
};
709 return stop_machine(__try_stop_module
, &sref
, NULL
);
711 /* We don't need to stop the machine for this. */
712 mod
->state
= MODULE_STATE_GOING
;
718 unsigned int module_refcount(struct module
*mod
)
720 unsigned int incs
= 0, decs
= 0;
723 for_each_possible_cpu(cpu
)
724 decs
+= per_cpu_ptr(mod
->refptr
, cpu
)->decs
;
726 * ensure the incs are added up after the decs.
727 * module_put ensures incs are visible before decs with smp_wmb.
729 * This 2-count scheme avoids the situation where the refcount
730 * for CPU0 is read, then CPU0 increments the module refcount,
731 * then CPU1 drops that refcount, then the refcount for CPU1 is
732 * read. We would record a decrement but not its corresponding
733 * increment so we would see a low count (disaster).
735 * Rare situation? But module_refcount can be preempted, and we
736 * might be tallying up 4096+ CPUs. So it is not impossible.
739 for_each_possible_cpu(cpu
)
740 incs
+= per_cpu_ptr(mod
->refptr
, cpu
)->incs
;
743 EXPORT_SYMBOL(module_refcount
);
745 /* This exists whether we can unload or not */
746 static void free_module(struct module
*mod
);
748 static void wait_for_zero_refcount(struct module
*mod
)
750 /* Since we might sleep for some time, release the mutex first */
751 mutex_unlock(&module_mutex
);
753 DEBUGP("Looking at refcount...\n");
754 set_current_state(TASK_UNINTERRUPTIBLE
);
755 if (module_refcount(mod
) == 0)
759 current
->state
= TASK_RUNNING
;
760 mutex_lock(&module_mutex
);
763 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
767 char name
[MODULE_NAME_LEN
];
770 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
773 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
775 name
[MODULE_NAME_LEN
-1] = '\0';
777 if (mutex_lock_interruptible(&module_mutex
) != 0)
780 mod
= find_module(name
);
786 if (!list_empty(&mod
->source_list
)) {
787 /* Other modules depend on us: get rid of them first. */
792 /* Doing init or already dying? */
793 if (mod
->state
!= MODULE_STATE_LIVE
) {
794 /* FIXME: if (force), slam module count and wake up
796 DEBUGP("%s already dying\n", mod
->name
);
801 /* If it has an init func, it must have an exit func to unload */
802 if (mod
->init
&& !mod
->exit
) {
803 forced
= try_force_unload(flags
);
805 /* This module can't be removed */
811 /* Set this up before setting mod->state */
812 mod
->waiter
= current
;
814 /* Stop the machine so refcounts can't move and disable module. */
815 ret
= try_stop_module(mod
, flags
, &forced
);
819 /* Never wait if forced. */
820 if (!forced
&& module_refcount(mod
) != 0)
821 wait_for_zero_refcount(mod
);
823 mutex_unlock(&module_mutex
);
824 /* Final destruction now no one is using it. */
825 if (mod
->exit
!= NULL
)
827 blocking_notifier_call_chain(&module_notify_list
,
828 MODULE_STATE_GOING
, mod
);
829 async_synchronize_full();
831 /* Store the name of the last unloaded module for diagnostic purposes */
832 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
837 mutex_unlock(&module_mutex
);
841 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
843 struct module_use
*use
;
844 int printed_something
= 0;
846 seq_printf(m
, " %u ", module_refcount(mod
));
848 /* Always include a trailing , so userspace can differentiate
849 between this and the old multi-field proc format. */
850 list_for_each_entry(use
, &mod
->source_list
, source_list
) {
851 printed_something
= 1;
852 seq_printf(m
, "%s,", use
->source
->name
);
855 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
856 printed_something
= 1;
857 seq_printf(m
, "[permanent],");
860 if (!printed_something
)
864 void __symbol_put(const char *symbol
)
866 struct module
*owner
;
869 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
874 EXPORT_SYMBOL(__symbol_put
);
876 /* Note this assumes addr is a function, which it currently always is. */
877 void symbol_put_addr(void *addr
)
879 struct module
*modaddr
;
880 unsigned long a
= (unsigned long)dereference_function_descriptor(addr
);
882 if (core_kernel_text(a
))
885 /* module_text_address is safe here: we're supposed to have reference
886 * to module from symbol_get, so it can't go away. */
887 modaddr
= __module_text_address(a
);
891 EXPORT_SYMBOL_GPL(symbol_put_addr
);
893 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
894 struct module
*mod
, char *buffer
)
896 return sprintf(buffer
, "%u\n", module_refcount(mod
));
899 static struct module_attribute refcnt
= {
900 .attr
= { .name
= "refcnt", .mode
= 0444 },
904 void module_put(struct module
*module
)
908 smp_wmb(); /* see comment in module_refcount */
909 __this_cpu_inc(module
->refptr
->decs
);
911 trace_module_put(module
, _RET_IP_
);
912 /* Maybe they're waiting for us to drop reference? */
913 if (unlikely(!module_is_live(module
)))
914 wake_up_process(module
->waiter
);
918 EXPORT_SYMBOL(module_put
);
920 #else /* !CONFIG_MODULE_UNLOAD */
921 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
923 /* We don't know the usage count, or what modules are using. */
924 seq_printf(m
, " - -");
927 static inline void module_unload_free(struct module
*mod
)
931 int ref_module(struct module
*a
, struct module
*b
)
933 return strong_try_module_get(b
);
935 EXPORT_SYMBOL_GPL(ref_module
);
937 static inline int module_unload_init(struct module
*mod
)
941 #endif /* CONFIG_MODULE_UNLOAD */
943 static ssize_t
show_initstate(struct module_attribute
*mattr
,
944 struct module
*mod
, char *buffer
)
946 const char *state
= "unknown";
948 switch (mod
->state
) {
949 case MODULE_STATE_LIVE
:
952 case MODULE_STATE_COMING
:
955 case MODULE_STATE_GOING
:
959 return sprintf(buffer
, "%s\n", state
);
962 static struct module_attribute initstate
= {
963 .attr
= { .name
= "initstate", .mode
= 0444 },
964 .show
= show_initstate
,
967 static struct module_attribute
*modinfo_attrs
[] = {
971 #ifdef CONFIG_MODULE_UNLOAD
977 static const char vermagic
[] = VERMAGIC_STRING
;
979 static int try_to_force_load(struct module
*mod
, const char *reason
)
981 #ifdef CONFIG_MODULE_FORCE_LOAD
982 if (!test_taint(TAINT_FORCED_MODULE
))
983 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
985 add_taint_module(mod
, TAINT_FORCED_MODULE
);
992 #ifdef CONFIG_MODVERSIONS
993 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
994 static unsigned long maybe_relocated(unsigned long crc
,
995 const struct module
*crc_owner
)
997 #ifdef ARCH_RELOCATES_KCRCTAB
998 if (crc_owner
== NULL
)
999 return crc
- (unsigned long)reloc_start
;
1004 static int check_version(Elf_Shdr
*sechdrs
,
1005 unsigned int versindex
,
1006 const char *symname
,
1008 const unsigned long *crc
,
1009 const struct module
*crc_owner
)
1011 unsigned int i
, num_versions
;
1012 struct modversion_info
*versions
;
1014 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1018 /* No versions at all? modprobe --force does this. */
1020 return try_to_force_load(mod
, symname
) == 0;
1022 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1023 num_versions
= sechdrs
[versindex
].sh_size
1024 / sizeof(struct modversion_info
);
1026 for (i
= 0; i
< num_versions
; i
++) {
1027 if (strcmp(versions
[i
].name
, symname
) != 0)
1030 if (versions
[i
].crc
== maybe_relocated(*crc
, crc_owner
))
1032 DEBUGP("Found checksum %lX vs module %lX\n",
1033 maybe_relocated(*crc
, crc_owner
), versions
[i
].crc
);
1037 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1038 mod
->name
, symname
);
1042 printk("%s: disagrees about version of symbol %s\n",
1043 mod
->name
, symname
);
1047 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1048 unsigned int versindex
,
1051 const unsigned long *crc
;
1053 /* Since this should be found in kernel (which can't be removed),
1054 * no locking is necessary. */
1055 if (!find_symbol(MODULE_SYMBOL_PREFIX
"module_layout", NULL
,
1058 return check_version(sechdrs
, versindex
, "module_layout", mod
, crc
,
1062 /* First part is kernel version, which we ignore if module has crcs. */
1063 static inline int same_magic(const char *amagic
, const char *bmagic
,
1067 amagic
+= strcspn(amagic
, " ");
1068 bmagic
+= strcspn(bmagic
, " ");
1070 return strcmp(amagic
, bmagic
) == 0;
1073 static inline int check_version(Elf_Shdr
*sechdrs
,
1074 unsigned int versindex
,
1075 const char *symname
,
1077 const unsigned long *crc
,
1078 const struct module
*crc_owner
)
1083 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1084 unsigned int versindex
,
1090 static inline int same_magic(const char *amagic
, const char *bmagic
,
1093 return strcmp(amagic
, bmagic
) == 0;
1095 #endif /* CONFIG_MODVERSIONS */
1097 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1098 static const struct kernel_symbol
*resolve_symbol(struct module
*mod
,
1099 const struct load_info
*info
,
1103 struct module
*owner
;
1104 const struct kernel_symbol
*sym
;
1105 const unsigned long *crc
;
1108 mutex_lock(&module_mutex
);
1109 sym
= find_symbol(name
, &owner
, &crc
,
1110 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1114 if (!check_version(info
->sechdrs
, info
->index
.vers
, name
, mod
, crc
,
1116 sym
= ERR_PTR(-EINVAL
);
1120 err
= ref_module(mod
, owner
);
1127 /* We must make copy under the lock if we failed to get ref. */
1128 strncpy(ownername
, module_name(owner
), MODULE_NAME_LEN
);
1130 mutex_unlock(&module_mutex
);
1134 static const struct kernel_symbol
*
1135 resolve_symbol_wait(struct module
*mod
,
1136 const struct load_info
*info
,
1139 const struct kernel_symbol
*ksym
;
1140 char owner
[MODULE_NAME_LEN
];
1142 if (wait_event_interruptible_timeout(module_wq
,
1143 !IS_ERR(ksym
= resolve_symbol(mod
, info
, name
, owner
))
1144 || PTR_ERR(ksym
) != -EBUSY
,
1146 printk(KERN_WARNING
"%s: gave up waiting for init of module %s.\n",
1153 * /sys/module/foo/sections stuff
1154 * J. Corbet <corbet@lwn.net>
1158 #ifdef CONFIG_KALLSYMS
1159 static inline bool sect_empty(const Elf_Shdr
*sect
)
1161 return !(sect
->sh_flags
& SHF_ALLOC
) || sect
->sh_size
== 0;
1164 struct module_sect_attr
1166 struct module_attribute mattr
;
1168 unsigned long address
;
1171 struct module_sect_attrs
1173 struct attribute_group grp
;
1174 unsigned int nsections
;
1175 struct module_sect_attr attrs
[0];
1178 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1179 struct module
*mod
, char *buf
)
1181 struct module_sect_attr
*sattr
=
1182 container_of(mattr
, struct module_sect_attr
, mattr
);
1183 return sprintf(buf
, "0x%pK\n", (void *)sattr
->address
);
1186 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1188 unsigned int section
;
1190 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1191 kfree(sect_attrs
->attrs
[section
].name
);
1195 static void add_sect_attrs(struct module
*mod
, const struct load_info
*info
)
1197 unsigned int nloaded
= 0, i
, size
[2];
1198 struct module_sect_attrs
*sect_attrs
;
1199 struct module_sect_attr
*sattr
;
1200 struct attribute
**gattr
;
1202 /* Count loaded sections and allocate structures */
1203 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1204 if (!sect_empty(&info
->sechdrs
[i
]))
1206 size
[0] = ALIGN(sizeof(*sect_attrs
)
1207 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1208 sizeof(sect_attrs
->grp
.attrs
[0]));
1209 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1210 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1211 if (sect_attrs
== NULL
)
1214 /* Setup section attributes. */
1215 sect_attrs
->grp
.name
= "sections";
1216 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1218 sect_attrs
->nsections
= 0;
1219 sattr
= §_attrs
->attrs
[0];
1220 gattr
= §_attrs
->grp
.attrs
[0];
1221 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
1222 Elf_Shdr
*sec
= &info
->sechdrs
[i
];
1223 if (sect_empty(sec
))
1225 sattr
->address
= sec
->sh_addr
;
1226 sattr
->name
= kstrdup(info
->secstrings
+ sec
->sh_name
,
1228 if (sattr
->name
== NULL
)
1230 sect_attrs
->nsections
++;
1231 sysfs_attr_init(&sattr
->mattr
.attr
);
1232 sattr
->mattr
.show
= module_sect_show
;
1233 sattr
->mattr
.store
= NULL
;
1234 sattr
->mattr
.attr
.name
= sattr
->name
;
1235 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1236 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1240 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1243 mod
->sect_attrs
= sect_attrs
;
1246 free_sect_attrs(sect_attrs
);
1249 static void remove_sect_attrs(struct module
*mod
)
1251 if (mod
->sect_attrs
) {
1252 sysfs_remove_group(&mod
->mkobj
.kobj
,
1253 &mod
->sect_attrs
->grp
);
1254 /* We are positive that no one is using any sect attrs
1255 * at this point. Deallocate immediately. */
1256 free_sect_attrs(mod
->sect_attrs
);
1257 mod
->sect_attrs
= NULL
;
1262 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1265 struct module_notes_attrs
{
1266 struct kobject
*dir
;
1268 struct bin_attribute attrs
[0];
1271 static ssize_t
module_notes_read(struct file
*filp
, struct kobject
*kobj
,
1272 struct bin_attribute
*bin_attr
,
1273 char *buf
, loff_t pos
, size_t count
)
1276 * The caller checked the pos and count against our size.
1278 memcpy(buf
, bin_attr
->private + pos
, count
);
1282 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1285 if (notes_attrs
->dir
) {
1287 sysfs_remove_bin_file(notes_attrs
->dir
,
1288 ¬es_attrs
->attrs
[i
]);
1289 kobject_put(notes_attrs
->dir
);
1294 static void add_notes_attrs(struct module
*mod
, const struct load_info
*info
)
1296 unsigned int notes
, loaded
, i
;
1297 struct module_notes_attrs
*notes_attrs
;
1298 struct bin_attribute
*nattr
;
1300 /* failed to create section attributes, so can't create notes */
1301 if (!mod
->sect_attrs
)
1304 /* Count notes sections and allocate structures. */
1306 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1307 if (!sect_empty(&info
->sechdrs
[i
]) &&
1308 (info
->sechdrs
[i
].sh_type
== SHT_NOTE
))
1314 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1315 + notes
* sizeof(notes_attrs
->attrs
[0]),
1317 if (notes_attrs
== NULL
)
1320 notes_attrs
->notes
= notes
;
1321 nattr
= ¬es_attrs
->attrs
[0];
1322 for (loaded
= i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1323 if (sect_empty(&info
->sechdrs
[i
]))
1325 if (info
->sechdrs
[i
].sh_type
== SHT_NOTE
) {
1326 sysfs_bin_attr_init(nattr
);
1327 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1328 nattr
->attr
.mode
= S_IRUGO
;
1329 nattr
->size
= info
->sechdrs
[i
].sh_size
;
1330 nattr
->private = (void *) info
->sechdrs
[i
].sh_addr
;
1331 nattr
->read
= module_notes_read
;
1337 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1338 if (!notes_attrs
->dir
)
1341 for (i
= 0; i
< notes
; ++i
)
1342 if (sysfs_create_bin_file(notes_attrs
->dir
,
1343 ¬es_attrs
->attrs
[i
]))
1346 mod
->notes_attrs
= notes_attrs
;
1350 free_notes_attrs(notes_attrs
, i
);
1353 static void remove_notes_attrs(struct module
*mod
)
1355 if (mod
->notes_attrs
)
1356 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1361 static inline void add_sect_attrs(struct module
*mod
,
1362 const struct load_info
*info
)
1366 static inline void remove_sect_attrs(struct module
*mod
)
1370 static inline void add_notes_attrs(struct module
*mod
,
1371 const struct load_info
*info
)
1375 static inline void remove_notes_attrs(struct module
*mod
)
1378 #endif /* CONFIG_KALLSYMS */
1380 static void add_usage_links(struct module
*mod
)
1382 #ifdef CONFIG_MODULE_UNLOAD
1383 struct module_use
*use
;
1386 mutex_lock(&module_mutex
);
1387 list_for_each_entry(use
, &mod
->target_list
, target_list
) {
1388 nowarn
= sysfs_create_link(use
->target
->holders_dir
,
1389 &mod
->mkobj
.kobj
, mod
->name
);
1391 mutex_unlock(&module_mutex
);
1395 static void del_usage_links(struct module
*mod
)
1397 #ifdef CONFIG_MODULE_UNLOAD
1398 struct module_use
*use
;
1400 mutex_lock(&module_mutex
);
1401 list_for_each_entry(use
, &mod
->target_list
, target_list
)
1402 sysfs_remove_link(use
->target
->holders_dir
, mod
->name
);
1403 mutex_unlock(&module_mutex
);
1407 static int module_add_modinfo_attrs(struct module
*mod
)
1409 struct module_attribute
*attr
;
1410 struct module_attribute
*temp_attr
;
1414 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1415 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1417 if (!mod
->modinfo_attrs
)
1420 temp_attr
= mod
->modinfo_attrs
;
1421 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1423 (attr
->test
&& attr
->test(mod
))) {
1424 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1425 sysfs_attr_init(&temp_attr
->attr
);
1426 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1433 static void module_remove_modinfo_attrs(struct module
*mod
)
1435 struct module_attribute
*attr
;
1438 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1439 /* pick a field to test for end of list */
1440 if (!attr
->attr
.name
)
1442 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1446 kfree(mod
->modinfo_attrs
);
1449 static int mod_sysfs_init(struct module
*mod
)
1452 struct kobject
*kobj
;
1454 if (!module_sysfs_initialized
) {
1455 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1461 kobj
= kset_find_obj(module_kset
, mod
->name
);
1463 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1469 mod
->mkobj
.mod
= mod
;
1471 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1472 mod
->mkobj
.kobj
.kset
= module_kset
;
1473 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1476 kobject_put(&mod
->mkobj
.kobj
);
1478 /* delay uevent until full sysfs population */
1483 static int mod_sysfs_setup(struct module
*mod
,
1484 const struct load_info
*info
,
1485 struct kernel_param
*kparam
,
1486 unsigned int num_params
)
1490 err
= mod_sysfs_init(mod
);
1494 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1495 if (!mod
->holders_dir
) {
1500 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1502 goto out_unreg_holders
;
1504 err
= module_add_modinfo_attrs(mod
);
1506 goto out_unreg_param
;
1508 add_usage_links(mod
);
1509 add_sect_attrs(mod
, info
);
1510 add_notes_attrs(mod
, info
);
1512 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1516 module_param_sysfs_remove(mod
);
1518 kobject_put(mod
->holders_dir
);
1520 kobject_put(&mod
->mkobj
.kobj
);
1525 static void mod_sysfs_fini(struct module
*mod
)
1527 remove_notes_attrs(mod
);
1528 remove_sect_attrs(mod
);
1529 kobject_put(&mod
->mkobj
.kobj
);
1532 #else /* !CONFIG_SYSFS */
1534 static int mod_sysfs_setup(struct module
*mod
,
1535 const struct load_info
*info
,
1536 struct kernel_param
*kparam
,
1537 unsigned int num_params
)
1542 static void mod_sysfs_fini(struct module
*mod
)
1546 static void module_remove_modinfo_attrs(struct module
*mod
)
1550 static void del_usage_links(struct module
*mod
)
1554 #endif /* CONFIG_SYSFS */
1556 static void mod_sysfs_teardown(struct module
*mod
)
1558 del_usage_links(mod
);
1559 module_remove_modinfo_attrs(mod
);
1560 module_param_sysfs_remove(mod
);
1561 kobject_put(mod
->mkobj
.drivers_dir
);
1562 kobject_put(mod
->holders_dir
);
1563 mod_sysfs_fini(mod
);
1567 * unlink the module with the whole machine is stopped with interrupts off
1568 * - this defends against kallsyms not taking locks
1570 static int __unlink_module(void *_mod
)
1572 struct module
*mod
= _mod
;
1573 list_del(&mod
->list
);
1574 module_bug_cleanup(mod
);
1578 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
1580 * LKM RO/NX protection: protect module's text/ro-data
1581 * from modification and any data from execution.
1583 void set_page_attributes(void *start
, void *end
, int (*set
)(unsigned long start
, int num_pages
))
1585 unsigned long begin_pfn
= PFN_DOWN((unsigned long)start
);
1586 unsigned long end_pfn
= PFN_DOWN((unsigned long)end
);
1588 if (end_pfn
> begin_pfn
)
1589 set(begin_pfn
<< PAGE_SHIFT
, end_pfn
- begin_pfn
);
1592 static void set_section_ro_nx(void *base
,
1593 unsigned long text_size
,
1594 unsigned long ro_size
,
1595 unsigned long total_size
)
1597 /* begin and end PFNs of the current subsection */
1598 unsigned long begin_pfn
;
1599 unsigned long end_pfn
;
1602 * Set RO for module text and RO-data:
1603 * - Always protect first page.
1604 * - Do not protect last partial page.
1607 set_page_attributes(base
, base
+ ro_size
, set_memory_ro
);
1610 * Set NX permissions for module data:
1611 * - Do not protect first partial page.
1612 * - Always protect last page.
1614 if (total_size
> text_size
) {
1615 begin_pfn
= PFN_UP((unsigned long)base
+ text_size
);
1616 end_pfn
= PFN_UP((unsigned long)base
+ total_size
);
1617 if (end_pfn
> begin_pfn
)
1618 set_memory_nx(begin_pfn
<< PAGE_SHIFT
, end_pfn
- begin_pfn
);
1622 static void unset_module_core_ro_nx(struct module
*mod
)
1624 set_page_attributes(mod
->module_core
+ mod
->core_text_size
,
1625 mod
->module_core
+ mod
->core_size
,
1627 set_page_attributes(mod
->module_core
,
1628 mod
->module_core
+ mod
->core_ro_size
,
1632 static void unset_module_init_ro_nx(struct module
*mod
)
1634 set_page_attributes(mod
->module_init
+ mod
->init_text_size
,
1635 mod
->module_init
+ mod
->init_size
,
1637 set_page_attributes(mod
->module_init
,
1638 mod
->module_init
+ mod
->init_ro_size
,
1642 /* Iterate through all modules and set each module's text as RW */
1643 void set_all_modules_text_rw(void)
1647 mutex_lock(&module_mutex
);
1648 list_for_each_entry_rcu(mod
, &modules
, list
) {
1649 if ((mod
->module_core
) && (mod
->core_text_size
)) {
1650 set_page_attributes(mod
->module_core
,
1651 mod
->module_core
+ mod
->core_text_size
,
1654 if ((mod
->module_init
) && (mod
->init_text_size
)) {
1655 set_page_attributes(mod
->module_init
,
1656 mod
->module_init
+ mod
->init_text_size
,
1660 mutex_unlock(&module_mutex
);
1663 /* Iterate through all modules and set each module's text as RO */
1664 void set_all_modules_text_ro(void)
1668 mutex_lock(&module_mutex
);
1669 list_for_each_entry_rcu(mod
, &modules
, list
) {
1670 if ((mod
->module_core
) && (mod
->core_text_size
)) {
1671 set_page_attributes(mod
->module_core
,
1672 mod
->module_core
+ mod
->core_text_size
,
1675 if ((mod
->module_init
) && (mod
->init_text_size
)) {
1676 set_page_attributes(mod
->module_init
,
1677 mod
->module_init
+ mod
->init_text_size
,
1681 mutex_unlock(&module_mutex
);
1684 static inline void set_section_ro_nx(void *base
, unsigned long text_size
, unsigned long ro_size
, unsigned long total_size
) { }
1685 static void unset_module_core_ro_nx(struct module
*mod
) { }
1686 static void unset_module_init_ro_nx(struct module
*mod
) { }
1689 /* Free a module, remove from lists, etc. */
1690 static void free_module(struct module
*mod
)
1692 trace_module_free(mod
);
1694 /* Delete from various lists */
1695 mutex_lock(&module_mutex
);
1696 stop_machine(__unlink_module
, mod
, NULL
);
1697 mutex_unlock(&module_mutex
);
1698 mod_sysfs_teardown(mod
);
1700 /* Remove dynamic debug info */
1701 ddebug_remove_module(mod
->name
);
1703 /* Arch-specific cleanup. */
1704 module_arch_cleanup(mod
);
1706 /* Module unload stuff */
1707 module_unload_free(mod
);
1709 /* Free any allocated parameters. */
1710 destroy_params(mod
->kp
, mod
->num_kp
);
1712 /* This may be NULL, but that's OK */
1713 unset_module_init_ro_nx(mod
);
1714 module_free(mod
, mod
->module_init
);
1716 percpu_modfree(mod
);
1718 /* Free lock-classes: */
1719 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1721 /* Finally, free the core (containing the module structure) */
1722 unset_module_core_ro_nx(mod
);
1723 module_free(mod
, mod
->module_core
);
1726 update_protections(current
->mm
);
1730 void *__symbol_get(const char *symbol
)
1732 struct module
*owner
;
1733 const struct kernel_symbol
*sym
;
1736 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1737 if (sym
&& strong_try_module_get(owner
))
1741 return sym
? (void *)sym
->value
: NULL
;
1743 EXPORT_SYMBOL_GPL(__symbol_get
);
1746 * Ensure that an exported symbol [global namespace] does not already exist
1747 * in the kernel or in some other module's exported symbol table.
1749 * You must hold the module_mutex.
1751 static int verify_export_symbols(struct module
*mod
)
1754 struct module
*owner
;
1755 const struct kernel_symbol
*s
;
1757 const struct kernel_symbol
*sym
;
1760 { mod
->syms
, mod
->num_syms
},
1761 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1762 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1763 #ifdef CONFIG_UNUSED_SYMBOLS
1764 { mod
->unused_syms
, mod
->num_unused_syms
},
1765 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1769 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1770 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1771 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1773 "%s: exports duplicate symbol %s"
1775 mod
->name
, s
->name
, module_name(owner
));
1783 /* Change all symbols so that st_value encodes the pointer directly. */
1784 static int simplify_symbols(struct module
*mod
, const struct load_info
*info
)
1786 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
1787 Elf_Sym
*sym
= (void *)symsec
->sh_addr
;
1788 unsigned long secbase
;
1791 const struct kernel_symbol
*ksym
;
1793 for (i
= 1; i
< symsec
->sh_size
/ sizeof(Elf_Sym
); i
++) {
1794 const char *name
= info
->strtab
+ sym
[i
].st_name
;
1796 switch (sym
[i
].st_shndx
) {
1798 /* We compiled with -fno-common. These are not
1799 supposed to happen. */
1800 DEBUGP("Common symbol: %s\n", name
);
1801 printk("%s: please compile with -fno-common\n",
1807 /* Don't need to do anything */
1808 DEBUGP("Absolute symbol: 0x%08lx\n",
1809 (long)sym
[i
].st_value
);
1813 ksym
= resolve_symbol_wait(mod
, info
, name
);
1814 /* Ok if resolved. */
1815 if (ksym
&& !IS_ERR(ksym
)) {
1816 sym
[i
].st_value
= ksym
->value
;
1821 if (!ksym
&& ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1824 printk(KERN_WARNING
"%s: Unknown symbol %s (err %li)\n",
1825 mod
->name
, name
, PTR_ERR(ksym
));
1826 ret
= PTR_ERR(ksym
) ?: -ENOENT
;
1830 /* Divert to percpu allocation if a percpu var. */
1831 if (sym
[i
].st_shndx
== info
->index
.pcpu
)
1832 secbase
= (unsigned long)mod_percpu(mod
);
1834 secbase
= info
->sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1835 sym
[i
].st_value
+= secbase
;
1843 static int apply_relocations(struct module
*mod
, const struct load_info
*info
)
1848 /* Now do relocations. */
1849 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
1850 unsigned int infosec
= info
->sechdrs
[i
].sh_info
;
1852 /* Not a valid relocation section? */
1853 if (infosec
>= info
->hdr
->e_shnum
)
1856 /* Don't bother with non-allocated sections */
1857 if (!(info
->sechdrs
[infosec
].sh_flags
& SHF_ALLOC
))
1860 if (info
->sechdrs
[i
].sh_type
== SHT_REL
)
1861 err
= apply_relocate(info
->sechdrs
, info
->strtab
,
1862 info
->index
.sym
, i
, mod
);
1863 else if (info
->sechdrs
[i
].sh_type
== SHT_RELA
)
1864 err
= apply_relocate_add(info
->sechdrs
, info
->strtab
,
1865 info
->index
.sym
, i
, mod
);
1872 /* Additional bytes needed by arch in front of individual sections */
1873 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1874 unsigned int section
)
1876 /* default implementation just returns zero */
1880 /* Update size with this section: return offset. */
1881 static long get_offset(struct module
*mod
, unsigned int *size
,
1882 Elf_Shdr
*sechdr
, unsigned int section
)
1886 *size
+= arch_mod_section_prepend(mod
, section
);
1887 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1888 *size
= ret
+ sechdr
->sh_size
;
1892 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1893 might -- code, read-only data, read-write data, small data. Tally
1894 sizes, and place the offsets into sh_entsize fields: high bit means it
1896 static void layout_sections(struct module
*mod
, struct load_info
*info
)
1898 static unsigned long const masks
[][2] = {
1899 /* NOTE: all executable code must be the first section
1900 * in this array; otherwise modify the text_size
1901 * finder in the two loops below */
1902 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1903 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1904 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1905 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1909 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1910 info
->sechdrs
[i
].sh_entsize
= ~0UL;
1912 DEBUGP("Core section allocation order:\n");
1913 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1914 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1915 Elf_Shdr
*s
= &info
->sechdrs
[i
];
1916 const char *sname
= info
->secstrings
+ s
->sh_name
;
1918 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1919 || (s
->sh_flags
& masks
[m
][1])
1920 || s
->sh_entsize
!= ~0UL
1921 || strstarts(sname
, ".init"))
1923 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1924 DEBUGP("\t%s\n", name
);
1927 case 0: /* executable */
1928 mod
->core_size
= debug_align(mod
->core_size
);
1929 mod
->core_text_size
= mod
->core_size
;
1931 case 1: /* RO: text and ro-data */
1932 mod
->core_size
= debug_align(mod
->core_size
);
1933 mod
->core_ro_size
= mod
->core_size
;
1935 case 3: /* whole core */
1936 mod
->core_size
= debug_align(mod
->core_size
);
1941 DEBUGP("Init section allocation order:\n");
1942 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1943 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1944 Elf_Shdr
*s
= &info
->sechdrs
[i
];
1945 const char *sname
= info
->secstrings
+ s
->sh_name
;
1947 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1948 || (s
->sh_flags
& masks
[m
][1])
1949 || s
->sh_entsize
!= ~0UL
1950 || !strstarts(sname
, ".init"))
1952 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
1953 | INIT_OFFSET_MASK
);
1954 DEBUGP("\t%s\n", sname
);
1957 case 0: /* executable */
1958 mod
->init_size
= debug_align(mod
->init_size
);
1959 mod
->init_text_size
= mod
->init_size
;
1961 case 1: /* RO: text and ro-data */
1962 mod
->init_size
= debug_align(mod
->init_size
);
1963 mod
->init_ro_size
= mod
->init_size
;
1965 case 3: /* whole init */
1966 mod
->init_size
= debug_align(mod
->init_size
);
1972 static void set_license(struct module
*mod
, const char *license
)
1975 license
= "unspecified";
1977 if (!license_is_gpl_compatible(license
)) {
1978 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1979 printk(KERN_WARNING
"%s: module license '%s' taints "
1980 "kernel.\n", mod
->name
, license
);
1981 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1985 /* Parse tag=value strings from .modinfo section */
1986 static char *next_string(char *string
, unsigned long *secsize
)
1988 /* Skip non-zero chars */
1991 if ((*secsize
)-- <= 1)
1995 /* Skip any zero padding. */
1996 while (!string
[0]) {
1998 if ((*secsize
)-- <= 1)
2004 static char *get_modinfo(struct load_info
*info
, const char *tag
)
2007 unsigned int taglen
= strlen(tag
);
2008 Elf_Shdr
*infosec
= &info
->sechdrs
[info
->index
.info
];
2009 unsigned long size
= infosec
->sh_size
;
2011 for (p
= (char *)infosec
->sh_addr
; p
; p
= next_string(p
, &size
)) {
2012 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
2013 return p
+ taglen
+ 1;
2018 static void setup_modinfo(struct module
*mod
, struct load_info
*info
)
2020 struct module_attribute
*attr
;
2023 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2025 attr
->setup(mod
, get_modinfo(info
, attr
->attr
.name
));
2029 static void free_modinfo(struct module
*mod
)
2031 struct module_attribute
*attr
;
2034 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2040 #ifdef CONFIG_KALLSYMS
2042 /* lookup symbol in given range of kernel_symbols */
2043 static const struct kernel_symbol
*lookup_symbol(const char *name
,
2044 const struct kernel_symbol
*start
,
2045 const struct kernel_symbol
*stop
)
2047 const struct kernel_symbol
*ks
= start
;
2048 for (; ks
< stop
; ks
++)
2049 if (strcmp(ks
->name
, name
) == 0)
2054 static int is_exported(const char *name
, unsigned long value
,
2055 const struct module
*mod
)
2057 const struct kernel_symbol
*ks
;
2059 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
2061 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
2062 return ks
!= NULL
&& ks
->value
== value
;
2066 static char elf_type(const Elf_Sym
*sym
, const struct load_info
*info
)
2068 const Elf_Shdr
*sechdrs
= info
->sechdrs
;
2070 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
2071 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
2076 if (sym
->st_shndx
== SHN_UNDEF
)
2078 if (sym
->st_shndx
== SHN_ABS
)
2080 if (sym
->st_shndx
>= SHN_LORESERVE
)
2082 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
2084 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
2085 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
2086 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
2088 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2093 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
2094 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2099 if (strstarts(info
->secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
2106 static bool is_core_symbol(const Elf_Sym
*src
, const Elf_Shdr
*sechdrs
,
2109 const Elf_Shdr
*sec
;
2111 if (src
->st_shndx
== SHN_UNDEF
2112 || src
->st_shndx
>= shnum
2116 sec
= sechdrs
+ src
->st_shndx
;
2117 if (!(sec
->sh_flags
& SHF_ALLOC
)
2118 #ifndef CONFIG_KALLSYMS_ALL
2119 || !(sec
->sh_flags
& SHF_EXECINSTR
)
2121 || (sec
->sh_entsize
& INIT_OFFSET_MASK
))
2127 static void layout_symtab(struct module
*mod
, struct load_info
*info
)
2129 Elf_Shdr
*symsect
= info
->sechdrs
+ info
->index
.sym
;
2130 Elf_Shdr
*strsect
= info
->sechdrs
+ info
->index
.str
;
2132 unsigned int i
, nsrc
, ndst
;
2134 /* Put symbol section at end of init part of module. */
2135 symsect
->sh_flags
|= SHF_ALLOC
;
2136 symsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, symsect
,
2137 info
->index
.sym
) | INIT_OFFSET_MASK
;
2138 DEBUGP("\t%s\n", info
->secstrings
+ symsect
->sh_name
);
2140 src
= (void *)info
->hdr
+ symsect
->sh_offset
;
2141 nsrc
= symsect
->sh_size
/ sizeof(*src
);
2142 for (ndst
= i
= 1; i
< nsrc
; ++i
, ++src
)
2143 if (is_core_symbol(src
, info
->sechdrs
, info
->hdr
->e_shnum
)) {
2144 unsigned int j
= src
->st_name
;
2146 while (!__test_and_set_bit(j
, info
->strmap
)
2152 /* Append room for core symbols at end of core part. */
2153 info
->symoffs
= ALIGN(mod
->core_size
, symsect
->sh_addralign
?: 1);
2154 mod
->core_size
= info
->symoffs
+ ndst
* sizeof(Elf_Sym
);
2156 /* Put string table section at end of init part of module. */
2157 strsect
->sh_flags
|= SHF_ALLOC
;
2158 strsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, strsect
,
2159 info
->index
.str
) | INIT_OFFSET_MASK
;
2160 DEBUGP("\t%s\n", info
->secstrings
+ strsect
->sh_name
);
2162 /* Append room for core symbols' strings at end of core part. */
2163 info
->stroffs
= mod
->core_size
;
2164 __set_bit(0, info
->strmap
);
2165 mod
->core_size
+= bitmap_weight(info
->strmap
, strsect
->sh_size
);
2168 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2170 unsigned int i
, ndst
;
2174 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
2176 mod
->symtab
= (void *)symsec
->sh_addr
;
2177 mod
->num_symtab
= symsec
->sh_size
/ sizeof(Elf_Sym
);
2178 /* Make sure we get permanent strtab: don't use info->strtab. */
2179 mod
->strtab
= (void *)info
->sechdrs
[info
->index
.str
].sh_addr
;
2181 /* Set types up while we still have access to sections. */
2182 for (i
= 0; i
< mod
->num_symtab
; i
++)
2183 mod
->symtab
[i
].st_info
= elf_type(&mod
->symtab
[i
], info
);
2185 mod
->core_symtab
= dst
= mod
->module_core
+ info
->symoffs
;
2188 for (ndst
= i
= 1; i
< mod
->num_symtab
; ++i
, ++src
) {
2189 if (!is_core_symbol(src
, info
->sechdrs
, info
->hdr
->e_shnum
))
2192 dst
[ndst
].st_name
= bitmap_weight(info
->strmap
,
2196 mod
->core_num_syms
= ndst
;
2198 mod
->core_strtab
= s
= mod
->module_core
+ info
->stroffs
;
2199 for (*s
= 0, i
= 1; i
< info
->sechdrs
[info
->index
.str
].sh_size
; ++i
)
2200 if (test_bit(i
, info
->strmap
))
2201 *++s
= mod
->strtab
[i
];
2204 static inline void layout_symtab(struct module
*mod
, struct load_info
*info
)
2208 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2211 #endif /* CONFIG_KALLSYMS */
2213 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
2217 #ifdef CONFIG_DYNAMIC_DEBUG
2218 if (ddebug_add_module(debug
, num
, debug
->modname
))
2219 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
2224 static void dynamic_debug_remove(struct _ddebug
*debug
)
2227 ddebug_remove_module(debug
->modname
);
2230 static void *module_alloc_update_bounds(unsigned long size
)
2232 void *ret
= module_alloc(size
);
2235 mutex_lock(&module_mutex
);
2236 /* Update module bounds. */
2237 if ((unsigned long)ret
< module_addr_min
)
2238 module_addr_min
= (unsigned long)ret
;
2239 if ((unsigned long)ret
+ size
> module_addr_max
)
2240 module_addr_max
= (unsigned long)ret
+ size
;
2241 mutex_unlock(&module_mutex
);
2246 #ifdef CONFIG_DEBUG_KMEMLEAK
2247 static void kmemleak_load_module(const struct module
*mod
,
2248 const struct load_info
*info
)
2252 /* only scan the sections containing data */
2253 kmemleak_scan_area(mod
, sizeof(struct module
), GFP_KERNEL
);
2255 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2256 const char *name
= info
->secstrings
+ info
->sechdrs
[i
].sh_name
;
2257 if (!(info
->sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2259 if (!strstarts(name
, ".data") && !strstarts(name
, ".bss"))
2262 kmemleak_scan_area((void *)info
->sechdrs
[i
].sh_addr
,
2263 info
->sechdrs
[i
].sh_size
, GFP_KERNEL
);
2267 static inline void kmemleak_load_module(const struct module
*mod
,
2268 const struct load_info
*info
)
2273 /* Sets info->hdr and info->len. */
2274 static int copy_and_check(struct load_info
*info
,
2275 const void __user
*umod
, unsigned long len
,
2276 const char __user
*uargs
)
2281 if (len
< sizeof(*hdr
))
2284 /* Suck in entire file: we'll want most of it. */
2285 /* vmalloc barfs on "unusual" numbers. Check here */
2286 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
2289 if (copy_from_user(hdr
, umod
, len
) != 0) {
2294 /* Sanity checks against insmoding binaries or wrong arch,
2295 weird elf version */
2296 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
2297 || hdr
->e_type
!= ET_REL
2298 || !elf_check_arch(hdr
)
2299 || hdr
->e_shentsize
!= sizeof(Elf_Shdr
)) {
2304 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
)) {
2318 static void free_copy(struct load_info
*info
)
2323 static int rewrite_section_headers(struct load_info
*info
)
2327 /* This should always be true, but let's be sure. */
2328 info
->sechdrs
[0].sh_addr
= 0;
2330 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2331 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2332 if (shdr
->sh_type
!= SHT_NOBITS
2333 && info
->len
< shdr
->sh_offset
+ shdr
->sh_size
) {
2334 printk(KERN_ERR
"Module len %lu truncated\n",
2339 /* Mark all sections sh_addr with their address in the
2341 shdr
->sh_addr
= (size_t)info
->hdr
+ shdr
->sh_offset
;
2343 #ifndef CONFIG_MODULE_UNLOAD
2344 /* Don't load .exit sections */
2345 if (strstarts(info
->secstrings
+shdr
->sh_name
, ".exit"))
2346 shdr
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2350 /* Track but don't keep modinfo and version sections. */
2351 info
->index
.vers
= find_sec(info
, "__versions");
2352 info
->index
.info
= find_sec(info
, ".modinfo");
2353 info
->sechdrs
[info
->index
.info
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2354 info
->sechdrs
[info
->index
.vers
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2359 * Set up our basic convenience variables (pointers to section headers,
2360 * search for module section index etc), and do some basic section
2363 * Return the temporary module pointer (we'll replace it with the final
2364 * one when we move the module sections around).
2366 static struct module
*setup_load_info(struct load_info
*info
)
2372 /* Set up the convenience variables */
2373 info
->sechdrs
= (void *)info
->hdr
+ info
->hdr
->e_shoff
;
2374 info
->secstrings
= (void *)info
->hdr
2375 + info
->sechdrs
[info
->hdr
->e_shstrndx
].sh_offset
;
2377 err
= rewrite_section_headers(info
);
2379 return ERR_PTR(err
);
2381 /* Find internal symbols and strings. */
2382 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2383 if (info
->sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
2384 info
->index
.sym
= i
;
2385 info
->index
.str
= info
->sechdrs
[i
].sh_link
;
2386 info
->strtab
= (char *)info
->hdr
2387 + info
->sechdrs
[info
->index
.str
].sh_offset
;
2392 info
->index
.mod
= find_sec(info
, ".gnu.linkonce.this_module");
2393 if (!info
->index
.mod
) {
2394 printk(KERN_WARNING
"No module found in object\n");
2395 return ERR_PTR(-ENOEXEC
);
2397 /* This is temporary: point mod into copy of data. */
2398 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2400 if (info
->index
.sym
== 0) {
2401 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
2403 return ERR_PTR(-ENOEXEC
);
2406 info
->index
.pcpu
= find_pcpusec(info
);
2408 /* Check module struct version now, before we try to use module. */
2409 if (!check_modstruct_version(info
->sechdrs
, info
->index
.vers
, mod
))
2410 return ERR_PTR(-ENOEXEC
);
2415 static int check_modinfo(struct module
*mod
, struct load_info
*info
)
2417 const char *modmagic
= get_modinfo(info
, "vermagic");
2420 /* This is allowed: modprobe --force will invalidate it. */
2422 err
= try_to_force_load(mod
, "bad vermagic");
2425 } else if (!same_magic(modmagic
, vermagic
, info
->index
.vers
)) {
2426 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2427 mod
->name
, modmagic
, vermagic
);
2431 if (get_modinfo(info
, "staging")) {
2432 add_taint_module(mod
, TAINT_CRAP
);
2433 printk(KERN_WARNING
"%s: module is from the staging directory,"
2434 " the quality is unknown, you have been warned.\n",
2438 /* Set up license info based on the info section */
2439 set_license(mod
, get_modinfo(info
, "license"));
2444 static void find_module_sections(struct module
*mod
, struct load_info
*info
)
2446 mod
->kp
= section_objs(info
, "__param",
2447 sizeof(*mod
->kp
), &mod
->num_kp
);
2448 mod
->syms
= section_objs(info
, "__ksymtab",
2449 sizeof(*mod
->syms
), &mod
->num_syms
);
2450 mod
->crcs
= section_addr(info
, "__kcrctab");
2451 mod
->gpl_syms
= section_objs(info
, "__ksymtab_gpl",
2452 sizeof(*mod
->gpl_syms
),
2453 &mod
->num_gpl_syms
);
2454 mod
->gpl_crcs
= section_addr(info
, "__kcrctab_gpl");
2455 mod
->gpl_future_syms
= section_objs(info
,
2456 "__ksymtab_gpl_future",
2457 sizeof(*mod
->gpl_future_syms
),
2458 &mod
->num_gpl_future_syms
);
2459 mod
->gpl_future_crcs
= section_addr(info
, "__kcrctab_gpl_future");
2461 #ifdef CONFIG_UNUSED_SYMBOLS
2462 mod
->unused_syms
= section_objs(info
, "__ksymtab_unused",
2463 sizeof(*mod
->unused_syms
),
2464 &mod
->num_unused_syms
);
2465 mod
->unused_crcs
= section_addr(info
, "__kcrctab_unused");
2466 mod
->unused_gpl_syms
= section_objs(info
, "__ksymtab_unused_gpl",
2467 sizeof(*mod
->unused_gpl_syms
),
2468 &mod
->num_unused_gpl_syms
);
2469 mod
->unused_gpl_crcs
= section_addr(info
, "__kcrctab_unused_gpl");
2471 #ifdef CONFIG_CONSTRUCTORS
2472 mod
->ctors
= section_objs(info
, ".ctors",
2473 sizeof(*mod
->ctors
), &mod
->num_ctors
);
2476 #ifdef CONFIG_TRACEPOINTS
2477 mod
->tracepoints_ptrs
= section_objs(info
, "__tracepoints_ptrs",
2478 sizeof(*mod
->tracepoints_ptrs
),
2479 &mod
->num_tracepoints
);
2481 #ifdef HAVE_JUMP_LABEL
2482 mod
->jump_entries
= section_objs(info
, "__jump_table",
2483 sizeof(*mod
->jump_entries
),
2484 &mod
->num_jump_entries
);
2486 #ifdef CONFIG_EVENT_TRACING
2487 mod
->trace_events
= section_objs(info
, "_ftrace_events",
2488 sizeof(*mod
->trace_events
),
2489 &mod
->num_trace_events
);
2491 * This section contains pointers to allocated objects in the trace
2492 * code and not scanning it leads to false positives.
2494 kmemleak_scan_area(mod
->trace_events
, sizeof(*mod
->trace_events
) *
2495 mod
->num_trace_events
, GFP_KERNEL
);
2497 #ifdef CONFIG_TRACING
2498 mod
->trace_bprintk_fmt_start
= section_objs(info
, "__trace_printk_fmt",
2499 sizeof(*mod
->trace_bprintk_fmt_start
),
2500 &mod
->num_trace_bprintk_fmt
);
2502 * This section contains pointers to allocated objects in the trace
2503 * code and not scanning it leads to false positives.
2505 kmemleak_scan_area(mod
->trace_bprintk_fmt_start
,
2506 sizeof(*mod
->trace_bprintk_fmt_start
) *
2507 mod
->num_trace_bprintk_fmt
, GFP_KERNEL
);
2509 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2510 /* sechdrs[0].sh_size is always zero */
2511 mod
->ftrace_callsites
= section_objs(info
, "__mcount_loc",
2512 sizeof(*mod
->ftrace_callsites
),
2513 &mod
->num_ftrace_callsites
);
2516 mod
->extable
= section_objs(info
, "__ex_table",
2517 sizeof(*mod
->extable
), &mod
->num_exentries
);
2519 if (section_addr(info
, "__obsparm"))
2520 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2523 info
->debug
= section_objs(info
, "__verbose",
2524 sizeof(*info
->debug
), &info
->num_debug
);
2527 static int move_module(struct module
*mod
, struct load_info
*info
)
2532 /* Do the allocs. */
2533 ptr
= module_alloc_update_bounds(mod
->core_size
);
2535 * The pointer to this block is stored in the module structure
2536 * which is inside the block. Just mark it as not being a
2539 kmemleak_not_leak(ptr
);
2543 memset(ptr
, 0, mod
->core_size
);
2544 mod
->module_core
= ptr
;
2546 ptr
= module_alloc_update_bounds(mod
->init_size
);
2548 * The pointer to this block is stored in the module structure
2549 * which is inside the block. This block doesn't need to be
2550 * scanned as it contains data and code that will be freed
2551 * after the module is initialized.
2553 kmemleak_ignore(ptr
);
2554 if (!ptr
&& mod
->init_size
) {
2555 module_free(mod
, mod
->module_core
);
2558 memset(ptr
, 0, mod
->init_size
);
2559 mod
->module_init
= ptr
;
2561 /* Transfer each section which specifies SHF_ALLOC */
2562 DEBUGP("final section addresses:\n");
2563 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
2565 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2567 if (!(shdr
->sh_flags
& SHF_ALLOC
))
2570 if (shdr
->sh_entsize
& INIT_OFFSET_MASK
)
2571 dest
= mod
->module_init
2572 + (shdr
->sh_entsize
& ~INIT_OFFSET_MASK
);
2574 dest
= mod
->module_core
+ shdr
->sh_entsize
;
2576 if (shdr
->sh_type
!= SHT_NOBITS
)
2577 memcpy(dest
, (void *)shdr
->sh_addr
, shdr
->sh_size
);
2578 /* Update sh_addr to point to copy in image. */
2579 shdr
->sh_addr
= (unsigned long)dest
;
2580 DEBUGP("\t0x%lx %s\n",
2581 shdr
->sh_addr
, info
->secstrings
+ shdr
->sh_name
);
2587 static int check_module_license_and_versions(struct module
*mod
)
2590 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2591 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2592 * using GPL-only symbols it needs.
2594 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2595 add_taint(TAINT_PROPRIETARY_MODULE
);
2597 /* driverloader was caught wrongly pretending to be under GPL */
2598 if (strcmp(mod
->name
, "driverloader") == 0)
2599 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2601 #ifdef CONFIG_MODVERSIONS
2602 if ((mod
->num_syms
&& !mod
->crcs
)
2603 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2604 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2605 #ifdef CONFIG_UNUSED_SYMBOLS
2606 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2607 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2610 return try_to_force_load(mod
,
2611 "no versions for exported symbols");
2617 static void flush_module_icache(const struct module
*mod
)
2619 mm_segment_t old_fs
;
2621 /* flush the icache in correct context */
2626 * Flush the instruction cache, since we've played with text.
2627 * Do it before processing of module parameters, so the module
2628 * can provide parameter accessor functions of its own.
2630 if (mod
->module_init
)
2631 flush_icache_range((unsigned long)mod
->module_init
,
2632 (unsigned long)mod
->module_init
2634 flush_icache_range((unsigned long)mod
->module_core
,
2635 (unsigned long)mod
->module_core
+ mod
->core_size
);
2640 static struct module
*layout_and_allocate(struct load_info
*info
)
2642 /* Module within temporary copy. */
2647 mod
= setup_load_info(info
);
2651 err
= check_modinfo(mod
, info
);
2653 return ERR_PTR(err
);
2655 /* Allow arches to frob section contents and sizes. */
2656 err
= module_frob_arch_sections(info
->hdr
, info
->sechdrs
,
2657 info
->secstrings
, mod
);
2661 pcpusec
= &info
->sechdrs
[info
->index
.pcpu
];
2662 if (pcpusec
->sh_size
) {
2663 /* We have a special allocation for this section. */
2664 err
= percpu_modalloc(mod
,
2665 pcpusec
->sh_size
, pcpusec
->sh_addralign
);
2668 pcpusec
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2671 /* Determine total sizes, and put offsets in sh_entsize. For now
2672 this is done generically; there doesn't appear to be any
2673 special cases for the architectures. */
2674 layout_sections(mod
, info
);
2676 info
->strmap
= kzalloc(BITS_TO_LONGS(info
->sechdrs
[info
->index
.str
].sh_size
)
2677 * sizeof(long), GFP_KERNEL
);
2678 if (!info
->strmap
) {
2682 layout_symtab(mod
, info
);
2684 /* Allocate and move to the final place */
2685 err
= move_module(mod
, info
);
2689 /* Module has been copied to its final place now: return it. */
2690 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2691 kmemleak_load_module(mod
, info
);
2695 kfree(info
->strmap
);
2697 percpu_modfree(mod
);
2699 return ERR_PTR(err
);
2702 /* mod is no longer valid after this! */
2703 static void module_deallocate(struct module
*mod
, struct load_info
*info
)
2705 kfree(info
->strmap
);
2706 percpu_modfree(mod
);
2707 module_free(mod
, mod
->module_init
);
2708 module_free(mod
, mod
->module_core
);
2711 static int post_relocation(struct module
*mod
, const struct load_info
*info
)
2713 /* Sort exception table now relocations are done. */
2714 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2716 /* Copy relocated percpu area over. */
2717 percpu_modcopy(mod
, (void *)info
->sechdrs
[info
->index
.pcpu
].sh_addr
,
2718 info
->sechdrs
[info
->index
.pcpu
].sh_size
);
2720 /* Setup kallsyms-specific fields. */
2721 add_kallsyms(mod
, info
);
2723 /* Arch-specific module finalizing. */
2724 return module_finalize(info
->hdr
, info
->sechdrs
, mod
);
2727 /* Allocate and load the module: note that size of section 0 is always
2728 zero, and we rely on this for optional sections. */
2729 static struct module
*load_module(void __user
*umod
,
2731 const char __user
*uargs
)
2733 struct load_info info
= { NULL
, };
2737 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2740 /* Copy in the blobs from userspace, check they are vaguely sane. */
2741 err
= copy_and_check(&info
, umod
, len
, uargs
);
2743 return ERR_PTR(err
);
2745 /* Figure out module layout, and allocate all the memory. */
2746 mod
= layout_and_allocate(&info
);
2752 /* Now module is in final location, initialize linked lists, etc. */
2753 err
= module_unload_init(mod
);
2757 /* Now we've got everything in the final locations, we can
2758 * find optional sections. */
2759 find_module_sections(mod
, &info
);
2761 err
= check_module_license_and_versions(mod
);
2765 /* Set up MODINFO_ATTR fields */
2766 setup_modinfo(mod
, &info
);
2768 /* Fix up syms, so that st_value is a pointer to location. */
2769 err
= simplify_symbols(mod
, &info
);
2773 err
= apply_relocations(mod
, &info
);
2777 err
= post_relocation(mod
, &info
);
2781 flush_module_icache(mod
);
2783 /* Now copy in args */
2784 mod
->args
= strndup_user(uargs
, ~0UL >> 1);
2785 if (IS_ERR(mod
->args
)) {
2786 err
= PTR_ERR(mod
->args
);
2787 goto free_arch_cleanup
;
2790 /* Mark state as coming so strong_try_module_get() ignores us. */
2791 mod
->state
= MODULE_STATE_COMING
;
2793 /* Now sew it into the lists so we can get lockdep and oops
2794 * info during argument parsing. No one should access us, since
2795 * strong_try_module_get() will fail.
2796 * lockdep/oops can run asynchronous, so use the RCU list insertion
2797 * function to insert in a way safe to concurrent readers.
2798 * The mutex protects against concurrent writers.
2800 mutex_lock(&module_mutex
);
2801 if (find_module(mod
->name
)) {
2806 /* This has to be done once we're sure module name is unique. */
2808 dynamic_debug_setup(info
.debug
, info
.num_debug
);
2810 /* Find duplicate symbols */
2811 err
= verify_export_symbols(mod
);
2815 module_bug_finalize(info
.hdr
, info
.sechdrs
, mod
);
2816 list_add_rcu(&mod
->list
, &modules
);
2817 mutex_unlock(&module_mutex
);
2819 /* Module is ready to execute: parsing args may do that. */
2820 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
, NULL
);
2824 /* Link in to syfs. */
2825 err
= mod_sysfs_setup(mod
, &info
, mod
->kp
, mod
->num_kp
);
2829 /* Get rid of temporary copy and strmap. */
2834 trace_module_load(mod
);
2838 mutex_lock(&module_mutex
);
2839 /* Unlink carefully: kallsyms could be walking list. */
2840 list_del_rcu(&mod
->list
);
2841 module_bug_cleanup(mod
);
2845 dynamic_debug_remove(info
.debug
);
2847 mutex_unlock(&module_mutex
);
2848 synchronize_sched();
2851 module_arch_cleanup(mod
);
2855 module_unload_free(mod
);
2857 module_deallocate(mod
, &info
);
2860 return ERR_PTR(err
);
2863 /* Call module constructors. */
2864 static void do_mod_ctors(struct module
*mod
)
2866 #ifdef CONFIG_CONSTRUCTORS
2869 for (i
= 0; i
< mod
->num_ctors
; i
++)
2874 /* This is where the real work happens */
2875 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
2876 unsigned long, len
, const char __user
*, uargs
)
2881 /* Must have permission */
2882 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
2885 /* Do all the hard work */
2886 mod
= load_module(umod
, len
, uargs
);
2888 return PTR_ERR(mod
);
2890 blocking_notifier_call_chain(&module_notify_list
,
2891 MODULE_STATE_COMING
, mod
);
2893 /* Set RO and NX regions for core */
2894 set_section_ro_nx(mod
->module_core
,
2895 mod
->core_text_size
,
2899 /* Set RO and NX regions for init */
2900 set_section_ro_nx(mod
->module_init
,
2901 mod
->init_text_size
,
2906 /* Start the module */
2907 if (mod
->init
!= NULL
)
2908 ret
= do_one_initcall(mod
->init
);
2910 /* Init routine failed: abort. Try to protect us from
2911 buggy refcounters. */
2912 mod
->state
= MODULE_STATE_GOING
;
2913 synchronize_sched();
2915 blocking_notifier_call_chain(&module_notify_list
,
2916 MODULE_STATE_GOING
, mod
);
2918 wake_up(&module_wq
);
2923 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2924 "%s: loading module anyway...\n",
2925 __func__
, mod
->name
, ret
,
2930 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2931 mod
->state
= MODULE_STATE_LIVE
;
2932 wake_up(&module_wq
);
2933 blocking_notifier_call_chain(&module_notify_list
,
2934 MODULE_STATE_LIVE
, mod
);
2936 /* We need to finish all async code before the module init sequence is done */
2937 async_synchronize_full();
2939 mutex_lock(&module_mutex
);
2940 /* Drop initial reference. */
2942 trim_init_extable(mod
);
2943 #ifdef CONFIG_KALLSYMS
2944 mod
->num_symtab
= mod
->core_num_syms
;
2945 mod
->symtab
= mod
->core_symtab
;
2946 mod
->strtab
= mod
->core_strtab
;
2948 unset_module_init_ro_nx(mod
);
2949 module_free(mod
, mod
->module_init
);
2950 mod
->module_init
= NULL
;
2952 mod
->init_ro_size
= 0;
2953 mod
->init_text_size
= 0;
2954 mutex_unlock(&module_mutex
);
2959 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2961 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2964 #ifdef CONFIG_KALLSYMS
2966 * This ignores the intensely annoying "mapping symbols" found
2967 * in ARM ELF files: $a, $t and $d.
2969 static inline int is_arm_mapping_symbol(const char *str
)
2971 return str
[0] == '$' && strchr("atd", str
[1])
2972 && (str
[2] == '\0' || str
[2] == '.');
2975 static const char *get_ksymbol(struct module
*mod
,
2977 unsigned long *size
,
2978 unsigned long *offset
)
2980 unsigned int i
, best
= 0;
2981 unsigned long nextval
;
2983 /* At worse, next value is at end of module */
2984 if (within_module_init(addr
, mod
))
2985 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2987 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2989 /* Scan for closest preceding symbol, and next symbol. (ELF
2990 starts real symbols at 1). */
2991 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2992 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2995 /* We ignore unnamed symbols: they're uninformative
2996 * and inserted at a whim. */
2997 if (mod
->symtab
[i
].st_value
<= addr
2998 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2999 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
3000 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
3002 if (mod
->symtab
[i
].st_value
> addr
3003 && mod
->symtab
[i
].st_value
< nextval
3004 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
3005 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
3006 nextval
= mod
->symtab
[i
].st_value
;
3013 *size
= nextval
- mod
->symtab
[best
].st_value
;
3015 *offset
= addr
- mod
->symtab
[best
].st_value
;
3016 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
3019 /* For kallsyms to ask for address resolution. NULL means not found. Careful
3020 * not to lock to avoid deadlock on oopses, simply disable preemption. */
3021 const char *module_address_lookup(unsigned long addr
,
3022 unsigned long *size
,
3023 unsigned long *offset
,
3028 const char *ret
= NULL
;
3031 list_for_each_entry_rcu(mod
, &modules
, list
) {
3032 if (within_module_init(addr
, mod
) ||
3033 within_module_core(addr
, mod
)) {
3035 *modname
= mod
->name
;
3036 ret
= get_ksymbol(mod
, addr
, size
, offset
);
3040 /* Make a copy in here where it's safe */
3042 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
3049 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
3054 list_for_each_entry_rcu(mod
, &modules
, list
) {
3055 if (within_module_init(addr
, mod
) ||
3056 within_module_core(addr
, mod
)) {
3059 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
3062 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
3072 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
3073 unsigned long *offset
, char *modname
, char *name
)
3078 list_for_each_entry_rcu(mod
, &modules
, list
) {
3079 if (within_module_init(addr
, mod
) ||
3080 within_module_core(addr
, mod
)) {
3083 sym
= get_ksymbol(mod
, addr
, size
, offset
);
3087 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
3089 strlcpy(name
, sym
, KSYM_NAME_LEN
);
3099 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
3100 char *name
, char *module_name
, int *exported
)
3105 list_for_each_entry_rcu(mod
, &modules
, list
) {
3106 if (symnum
< mod
->num_symtab
) {
3107 *value
= mod
->symtab
[symnum
].st_value
;
3108 *type
= mod
->symtab
[symnum
].st_info
;
3109 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
3111 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
3112 *exported
= is_exported(name
, *value
, mod
);
3116 symnum
-= mod
->num_symtab
;
3122 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
3126 for (i
= 0; i
< mod
->num_symtab
; i
++)
3127 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
3128 mod
->symtab
[i
].st_info
!= 'U')
3129 return mod
->symtab
[i
].st_value
;
3133 /* Look for this name: can be of form module:name. */
3134 unsigned long module_kallsyms_lookup_name(const char *name
)
3138 unsigned long ret
= 0;
3140 /* Don't lock: we're in enough trouble already. */
3142 if ((colon
= strchr(name
, ':')) != NULL
) {
3144 if ((mod
= find_module(name
)) != NULL
)
3145 ret
= mod_find_symname(mod
, colon
+1);
3148 list_for_each_entry_rcu(mod
, &modules
, list
)
3149 if ((ret
= mod_find_symname(mod
, name
)) != 0)
3156 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
3157 struct module
*, unsigned long),
3164 list_for_each_entry(mod
, &modules
, list
) {
3165 for (i
= 0; i
< mod
->num_symtab
; i
++) {
3166 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
3167 mod
, mod
->symtab
[i
].st_value
);
3174 #endif /* CONFIG_KALLSYMS */
3176 static char *module_flags(struct module
*mod
, char *buf
)
3181 mod
->state
== MODULE_STATE_GOING
||
3182 mod
->state
== MODULE_STATE_COMING
) {
3184 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
3186 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
3188 if (mod
->taints
& (1 << TAINT_CRAP
))
3191 * TAINT_FORCED_RMMOD: could be added.
3192 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3196 /* Show a - for module-is-being-unloaded */
3197 if (mod
->state
== MODULE_STATE_GOING
)
3199 /* Show a + for module-is-being-loaded */
3200 if (mod
->state
== MODULE_STATE_COMING
)
3209 #ifdef CONFIG_PROC_FS
3210 /* Called by the /proc file system to return a list of modules. */
3211 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
3213 mutex_lock(&module_mutex
);
3214 return seq_list_start(&modules
, *pos
);
3217 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
3219 return seq_list_next(p
, &modules
, pos
);
3222 static void m_stop(struct seq_file
*m
, void *p
)
3224 mutex_unlock(&module_mutex
);
3227 static int m_show(struct seq_file
*m
, void *p
)
3229 struct module
*mod
= list_entry(p
, struct module
, list
);
3232 seq_printf(m
, "%s %u",
3233 mod
->name
, mod
->init_size
+ mod
->core_size
);
3234 print_unload_info(m
, mod
);
3236 /* Informative for users. */
3237 seq_printf(m
, " %s",
3238 mod
->state
== MODULE_STATE_GOING
? "Unloading":
3239 mod
->state
== MODULE_STATE_COMING
? "Loading":
3241 /* Used by oprofile and other similar tools. */
3242 seq_printf(m
, " 0x%pK", mod
->module_core
);
3246 seq_printf(m
, " %s", module_flags(mod
, buf
));
3248 seq_printf(m
, "\n");
3252 /* Format: modulename size refcount deps address
3254 Where refcount is a number or -, and deps is a comma-separated list
3257 static const struct seq_operations modules_op
= {
3264 static int modules_open(struct inode
*inode
, struct file
*file
)
3266 return seq_open(file
, &modules_op
);
3269 static const struct file_operations proc_modules_operations
= {
3270 .open
= modules_open
,
3272 .llseek
= seq_lseek
,
3273 .release
= seq_release
,
3276 static int __init
proc_modules_init(void)
3278 proc_create("modules", 0, NULL
, &proc_modules_operations
);
3281 module_init(proc_modules_init
);
3284 /* Given an address, look for it in the module exception tables. */
3285 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
3287 const struct exception_table_entry
*e
= NULL
;
3291 list_for_each_entry_rcu(mod
, &modules
, list
) {
3292 if (mod
->num_exentries
== 0)
3295 e
= search_extable(mod
->extable
,
3296 mod
->extable
+ mod
->num_exentries
- 1,
3303 /* Now, if we found one, we are running inside it now, hence
3304 we cannot unload the module, hence no refcnt needed. */
3309 * is_module_address - is this address inside a module?
3310 * @addr: the address to check.
3312 * See is_module_text_address() if you simply want to see if the address
3313 * is code (not data).
3315 bool is_module_address(unsigned long addr
)
3320 ret
= __module_address(addr
) != NULL
;
3327 * __module_address - get the module which contains an address.
3328 * @addr: the address.
3330 * Must be called with preempt disabled or module mutex held so that
3331 * module doesn't get freed during this.
3333 struct module
*__module_address(unsigned long addr
)
3337 if (addr
< module_addr_min
|| addr
> module_addr_max
)
3340 list_for_each_entry_rcu(mod
, &modules
, list
)
3341 if (within_module_core(addr
, mod
)
3342 || within_module_init(addr
, mod
))
3346 EXPORT_SYMBOL_GPL(__module_address
);
3349 * is_module_text_address - is this address inside module code?
3350 * @addr: the address to check.
3352 * See is_module_address() if you simply want to see if the address is
3353 * anywhere in a module. See kernel_text_address() for testing if an
3354 * address corresponds to kernel or module code.
3356 bool is_module_text_address(unsigned long addr
)
3361 ret
= __module_text_address(addr
) != NULL
;
3368 * __module_text_address - get the module whose code contains an address.
3369 * @addr: the address.
3371 * Must be called with preempt disabled or module mutex held so that
3372 * module doesn't get freed during this.
3374 struct module
*__module_text_address(unsigned long addr
)
3376 struct module
*mod
= __module_address(addr
);
3378 /* Make sure it's within the text section. */
3379 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
3380 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
3385 EXPORT_SYMBOL_GPL(__module_text_address
);
3387 /* Don't grab lock, we're oopsing. */
3388 void print_modules(void)
3393 printk(KERN_DEFAULT
"Modules linked in:");
3394 /* Most callers should already have preempt disabled, but make sure */
3396 list_for_each_entry_rcu(mod
, &modules
, list
)
3397 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
3399 if (last_unloaded_module
[0])
3400 printk(" [last unloaded: %s]", last_unloaded_module
);
3404 #ifdef CONFIG_MODVERSIONS
3405 /* Generate the signature for all relevant module structures here.
3406 * If these change, we don't want to try to parse the module. */
3407 void module_layout(struct module
*mod
,
3408 struct modversion_info
*ver
,
3409 struct kernel_param
*kp
,
3410 struct kernel_symbol
*ks
,
3411 struct tracepoint
* const *tp
)
3414 EXPORT_SYMBOL(module_layout
);
3417 #ifdef CONFIG_TRACEPOINTS
3418 void module_update_tracepoints(void)
3422 mutex_lock(&module_mutex
);
3423 list_for_each_entry(mod
, &modules
, list
)
3425 tracepoint_update_probe_range(mod
->tracepoints_ptrs
,
3426 mod
->tracepoints_ptrs
+ mod
->num_tracepoints
);
3427 mutex_unlock(&module_mutex
);
3431 * Returns 0 if current not found.
3432 * Returns 1 if current found.
3434 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
3436 struct module
*iter_mod
;
3439 mutex_lock(&module_mutex
);
3440 list_for_each_entry(iter_mod
, &modules
, list
) {
3441 if (!iter_mod
->taints
) {
3443 * Sorted module list
3445 if (iter_mod
< iter
->module
)
3447 else if (iter_mod
> iter
->module
)
3448 iter
->tracepoint
= NULL
;
3449 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
3450 iter_mod
->tracepoints_ptrs
,
3451 iter_mod
->tracepoints_ptrs
3452 + iter_mod
->num_tracepoints
);
3454 iter
->module
= iter_mod
;
3459 mutex_unlock(&module_mutex
);