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
,
243 unsigned int symnum
, void *data
),
248 for (j
= 0; j
< arrsize
; j
++) {
249 for (i
= 0; i
< arr
[j
].stop
- arr
[j
].start
; i
++)
250 if (fn(&arr
[j
], owner
, i
, data
))
257 /* Returns true as soon as fn returns true, otherwise false. */
258 bool each_symbol(bool (*fn
)(const struct symsearch
*arr
, struct module
*owner
,
259 unsigned int symnum
, void *data
), void *data
)
262 static const struct symsearch arr
[] = {
263 { __start___ksymtab
, __stop___ksymtab
, __start___kcrctab
,
264 NOT_GPL_ONLY
, false },
265 { __start___ksymtab_gpl
, __stop___ksymtab_gpl
,
266 __start___kcrctab_gpl
,
268 { __start___ksymtab_gpl_future
, __stop___ksymtab_gpl_future
,
269 __start___kcrctab_gpl_future
,
270 WILL_BE_GPL_ONLY
, false },
271 #ifdef CONFIG_UNUSED_SYMBOLS
272 { __start___ksymtab_unused
, __stop___ksymtab_unused
,
273 __start___kcrctab_unused
,
274 NOT_GPL_ONLY
, true },
275 { __start___ksymtab_unused_gpl
, __stop___ksymtab_unused_gpl
,
276 __start___kcrctab_unused_gpl
,
281 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), NULL
, fn
, data
))
284 list_for_each_entry_rcu(mod
, &modules
, list
) {
285 struct symsearch arr
[] = {
286 { mod
->syms
, mod
->syms
+ mod
->num_syms
, mod
->crcs
,
287 NOT_GPL_ONLY
, false },
288 { mod
->gpl_syms
, mod
->gpl_syms
+ mod
->num_gpl_syms
,
291 { mod
->gpl_future_syms
,
292 mod
->gpl_future_syms
+ mod
->num_gpl_future_syms
,
293 mod
->gpl_future_crcs
,
294 WILL_BE_GPL_ONLY
, false },
295 #ifdef CONFIG_UNUSED_SYMBOLS
297 mod
->unused_syms
+ mod
->num_unused_syms
,
299 NOT_GPL_ONLY
, true },
300 { mod
->unused_gpl_syms
,
301 mod
->unused_gpl_syms
+ mod
->num_unused_gpl_syms
,
302 mod
->unused_gpl_crcs
,
307 if (each_symbol_in_section(arr
, ARRAY_SIZE(arr
), mod
, fn
, data
))
312 EXPORT_SYMBOL_GPL(each_symbol
);
314 struct find_symbol_arg
{
321 struct module
*owner
;
322 const unsigned long *crc
;
323 const struct kernel_symbol
*sym
;
326 static bool find_symbol_in_section(const struct symsearch
*syms
,
327 struct module
*owner
,
328 unsigned int symnum
, void *data
)
330 struct find_symbol_arg
*fsa
= data
;
332 if (strcmp(syms
->start
[symnum
].name
, fsa
->name
) != 0)
336 if (syms
->licence
== GPL_ONLY
)
338 if (syms
->licence
== WILL_BE_GPL_ONLY
&& fsa
->warn
) {
339 printk(KERN_WARNING
"Symbol %s is being used "
340 "by a non-GPL module, which will not "
341 "be allowed in the future\n", fsa
->name
);
342 printk(KERN_WARNING
"Please see the file "
343 "Documentation/feature-removal-schedule.txt "
344 "in the kernel source tree for more details.\n");
348 #ifdef CONFIG_UNUSED_SYMBOLS
349 if (syms
->unused
&& fsa
->warn
) {
350 printk(KERN_WARNING
"Symbol %s is marked as UNUSED, "
351 "however this module is using it.\n", fsa
->name
);
353 "This symbol will go away in the future.\n");
355 "Please evalute if this is the right api to use and if "
356 "it really is, submit a report the linux kernel "
357 "mailinglist together with submitting your code for "
363 fsa
->crc
= symversion(syms
->crcs
, symnum
);
364 fsa
->sym
= &syms
->start
[symnum
];
368 /* Find a symbol and return it, along with, (optional) crc and
369 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
370 const struct kernel_symbol
*find_symbol(const char *name
,
371 struct module
**owner
,
372 const unsigned long **crc
,
376 struct find_symbol_arg fsa
;
382 if (each_symbol(find_symbol_in_section
, &fsa
)) {
390 DEBUGP("Failed to find symbol %s\n", name
);
393 EXPORT_SYMBOL_GPL(find_symbol
);
395 /* Search for module by name: must hold module_mutex. */
396 struct module
*find_module(const char *name
)
400 list_for_each_entry(mod
, &modules
, list
) {
401 if (strcmp(mod
->name
, name
) == 0)
406 EXPORT_SYMBOL_GPL(find_module
);
410 static inline void __percpu
*mod_percpu(struct module
*mod
)
415 static int percpu_modalloc(struct module
*mod
,
416 unsigned long size
, unsigned long align
)
418 if (align
> PAGE_SIZE
) {
419 printk(KERN_WARNING
"%s: per-cpu alignment %li > %li\n",
420 mod
->name
, align
, PAGE_SIZE
);
424 mod
->percpu
= __alloc_reserved_percpu(size
, align
);
427 "%s: Could not allocate %lu bytes percpu data\n",
431 mod
->percpu_size
= size
;
435 static void percpu_modfree(struct module
*mod
)
437 free_percpu(mod
->percpu
);
440 static unsigned int find_pcpusec(struct load_info
*info
)
442 return find_sec(info
, ".data..percpu");
445 static void percpu_modcopy(struct module
*mod
,
446 const void *from
, unsigned long size
)
450 for_each_possible_cpu(cpu
)
451 memcpy(per_cpu_ptr(mod
->percpu
, cpu
), from
, size
);
455 * is_module_percpu_address - test whether address is from module static percpu
456 * @addr: address to test
458 * Test whether @addr belongs to module static percpu area.
461 * %true if @addr is from module static percpu area
463 bool is_module_percpu_address(unsigned long addr
)
470 list_for_each_entry_rcu(mod
, &modules
, list
) {
471 if (!mod
->percpu_size
)
473 for_each_possible_cpu(cpu
) {
474 void *start
= per_cpu_ptr(mod
->percpu
, cpu
);
476 if ((void *)addr
>= start
&&
477 (void *)addr
< start
+ mod
->percpu_size
) {
488 #else /* ... !CONFIG_SMP */
490 static inline void __percpu
*mod_percpu(struct module
*mod
)
494 static inline int percpu_modalloc(struct module
*mod
,
495 unsigned long size
, unsigned long align
)
499 static inline void percpu_modfree(struct module
*mod
)
502 static unsigned int find_pcpusec(struct load_info
*info
)
506 static inline void percpu_modcopy(struct module
*mod
,
507 const void *from
, unsigned long size
)
509 /* pcpusec should be 0, and size of that section should be 0. */
512 bool is_module_percpu_address(unsigned long addr
)
517 #endif /* CONFIG_SMP */
519 #define MODINFO_ATTR(field) \
520 static void setup_modinfo_##field(struct module *mod, const char *s) \
522 mod->field = kstrdup(s, GFP_KERNEL); \
524 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
525 struct module *mod, char *buffer) \
527 return sprintf(buffer, "%s\n", mod->field); \
529 static int modinfo_##field##_exists(struct module *mod) \
531 return mod->field != NULL; \
533 static void free_modinfo_##field(struct module *mod) \
538 static struct module_attribute modinfo_##field = { \
539 .attr = { .name = __stringify(field), .mode = 0444 }, \
540 .show = show_modinfo_##field, \
541 .setup = setup_modinfo_##field, \
542 .test = modinfo_##field##_exists, \
543 .free = free_modinfo_##field, \
546 MODINFO_ATTR(version
);
547 MODINFO_ATTR(srcversion
);
549 static char last_unloaded_module
[MODULE_NAME_LEN
+1];
551 #ifdef CONFIG_MODULE_UNLOAD
553 EXPORT_TRACEPOINT_SYMBOL(module_get
);
555 /* Init the unload section of the module. */
556 static int module_unload_init(struct module
*mod
)
558 mod
->refptr
= alloc_percpu(struct module_ref
);
562 INIT_LIST_HEAD(&mod
->source_list
);
563 INIT_LIST_HEAD(&mod
->target_list
);
565 /* Hold reference count during initialization. */
566 __this_cpu_write(mod
->refptr
->incs
, 1);
567 /* Backwards compatibility macros put refcount during init. */
568 mod
->waiter
= current
;
573 /* Does a already use b? */
574 static int already_uses(struct module
*a
, struct module
*b
)
576 struct module_use
*use
;
578 list_for_each_entry(use
, &b
->source_list
, source_list
) {
579 if (use
->source
== a
) {
580 DEBUGP("%s uses %s!\n", a
->name
, b
->name
);
584 DEBUGP("%s does not use %s!\n", a
->name
, b
->name
);
590 * - we add 'a' as a "source", 'b' as a "target" of module use
591 * - the module_use is added to the list of 'b' sources (so
592 * 'b' can walk the list to see who sourced them), and of 'a'
593 * targets (so 'a' can see what modules it targets).
595 static int add_module_usage(struct module
*a
, struct module
*b
)
597 struct module_use
*use
;
599 DEBUGP("Allocating new usage for %s.\n", a
->name
);
600 use
= kmalloc(sizeof(*use
), GFP_ATOMIC
);
602 printk(KERN_WARNING
"%s: out of memory loading\n", a
->name
);
608 list_add(&use
->source_list
, &b
->source_list
);
609 list_add(&use
->target_list
, &a
->target_list
);
613 /* Module a uses b: caller needs module_mutex() */
614 int ref_module(struct module
*a
, struct module
*b
)
618 if (b
== NULL
|| already_uses(a
, b
))
621 /* If module isn't available, we fail. */
622 err
= strong_try_module_get(b
);
626 err
= add_module_usage(a
, b
);
633 EXPORT_SYMBOL_GPL(ref_module
);
635 /* Clear the unload stuff of the module. */
636 static void module_unload_free(struct module
*mod
)
638 struct module_use
*use
, *tmp
;
640 mutex_lock(&module_mutex
);
641 list_for_each_entry_safe(use
, tmp
, &mod
->target_list
, target_list
) {
642 struct module
*i
= use
->target
;
643 DEBUGP("%s unusing %s\n", mod
->name
, i
->name
);
645 list_del(&use
->source_list
);
646 list_del(&use
->target_list
);
649 mutex_unlock(&module_mutex
);
651 free_percpu(mod
->refptr
);
654 #ifdef CONFIG_MODULE_FORCE_UNLOAD
655 static inline int try_force_unload(unsigned int flags
)
657 int ret
= (flags
& O_TRUNC
);
659 add_taint(TAINT_FORCED_RMMOD
);
663 static inline int try_force_unload(unsigned int flags
)
667 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
676 /* Whole machine is stopped with interrupts off when this runs. */
677 static int __try_stop_module(void *_sref
)
679 struct stopref
*sref
= _sref
;
681 /* If it's not unused, quit unless we're forcing. */
682 if (module_refcount(sref
->mod
) != 0) {
683 if (!(*sref
->forced
= try_force_unload(sref
->flags
)))
687 /* Mark it as dying. */
688 sref
->mod
->state
= MODULE_STATE_GOING
;
692 static int try_stop_module(struct module
*mod
, int flags
, int *forced
)
694 if (flags
& O_NONBLOCK
) {
695 struct stopref sref
= { mod
, flags
, forced
};
697 return stop_machine(__try_stop_module
, &sref
, NULL
);
699 /* We don't need to stop the machine for this. */
700 mod
->state
= MODULE_STATE_GOING
;
706 unsigned int module_refcount(struct module
*mod
)
708 unsigned int incs
= 0, decs
= 0;
711 for_each_possible_cpu(cpu
)
712 decs
+= per_cpu_ptr(mod
->refptr
, cpu
)->decs
;
714 * ensure the incs are added up after the decs.
715 * module_put ensures incs are visible before decs with smp_wmb.
717 * This 2-count scheme avoids the situation where the refcount
718 * for CPU0 is read, then CPU0 increments the module refcount,
719 * then CPU1 drops that refcount, then the refcount for CPU1 is
720 * read. We would record a decrement but not its corresponding
721 * increment so we would see a low count (disaster).
723 * Rare situation? But module_refcount can be preempted, and we
724 * might be tallying up 4096+ CPUs. So it is not impossible.
727 for_each_possible_cpu(cpu
)
728 incs
+= per_cpu_ptr(mod
->refptr
, cpu
)->incs
;
731 EXPORT_SYMBOL(module_refcount
);
733 /* This exists whether we can unload or not */
734 static void free_module(struct module
*mod
);
736 static void wait_for_zero_refcount(struct module
*mod
)
738 /* Since we might sleep for some time, release the mutex first */
739 mutex_unlock(&module_mutex
);
741 DEBUGP("Looking at refcount...\n");
742 set_current_state(TASK_UNINTERRUPTIBLE
);
743 if (module_refcount(mod
) == 0)
747 current
->state
= TASK_RUNNING
;
748 mutex_lock(&module_mutex
);
751 SYSCALL_DEFINE2(delete_module
, const char __user
*, name_user
,
755 char name
[MODULE_NAME_LEN
];
758 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
761 if (strncpy_from_user(name
, name_user
, MODULE_NAME_LEN
-1) < 0)
763 name
[MODULE_NAME_LEN
-1] = '\0';
765 if (mutex_lock_interruptible(&module_mutex
) != 0)
768 mod
= find_module(name
);
774 if (!list_empty(&mod
->source_list
)) {
775 /* Other modules depend on us: get rid of them first. */
780 /* Doing init or already dying? */
781 if (mod
->state
!= MODULE_STATE_LIVE
) {
782 /* FIXME: if (force), slam module count and wake up
784 DEBUGP("%s already dying\n", mod
->name
);
789 /* If it has an init func, it must have an exit func to unload */
790 if (mod
->init
&& !mod
->exit
) {
791 forced
= try_force_unload(flags
);
793 /* This module can't be removed */
799 /* Set this up before setting mod->state */
800 mod
->waiter
= current
;
802 /* Stop the machine so refcounts can't move and disable module. */
803 ret
= try_stop_module(mod
, flags
, &forced
);
807 /* Never wait if forced. */
808 if (!forced
&& module_refcount(mod
) != 0)
809 wait_for_zero_refcount(mod
);
811 mutex_unlock(&module_mutex
);
812 /* Final destruction now noone is using it. */
813 if (mod
->exit
!= NULL
)
815 blocking_notifier_call_chain(&module_notify_list
,
816 MODULE_STATE_GOING
, mod
);
817 async_synchronize_full();
819 /* Store the name of the last unloaded module for diagnostic purposes */
820 strlcpy(last_unloaded_module
, mod
->name
, sizeof(last_unloaded_module
));
825 mutex_unlock(&module_mutex
);
829 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
831 struct module_use
*use
;
832 int printed_something
= 0;
834 seq_printf(m
, " %u ", module_refcount(mod
));
836 /* Always include a trailing , so userspace can differentiate
837 between this and the old multi-field proc format. */
838 list_for_each_entry(use
, &mod
->source_list
, source_list
) {
839 printed_something
= 1;
840 seq_printf(m
, "%s,", use
->source
->name
);
843 if (mod
->init
!= NULL
&& mod
->exit
== NULL
) {
844 printed_something
= 1;
845 seq_printf(m
, "[permanent],");
848 if (!printed_something
)
852 void __symbol_put(const char *symbol
)
854 struct module
*owner
;
857 if (!find_symbol(symbol
, &owner
, NULL
, true, false))
862 EXPORT_SYMBOL(__symbol_put
);
864 /* Note this assumes addr is a function, which it currently always is. */
865 void symbol_put_addr(void *addr
)
867 struct module
*modaddr
;
868 unsigned long a
= (unsigned long)dereference_function_descriptor(addr
);
870 if (core_kernel_text(a
))
873 /* module_text_address is safe here: we're supposed to have reference
874 * to module from symbol_get, so it can't go away. */
875 modaddr
= __module_text_address(a
);
879 EXPORT_SYMBOL_GPL(symbol_put_addr
);
881 static ssize_t
show_refcnt(struct module_attribute
*mattr
,
882 struct module
*mod
, char *buffer
)
884 return sprintf(buffer
, "%u\n", module_refcount(mod
));
887 static struct module_attribute refcnt
= {
888 .attr
= { .name
= "refcnt", .mode
= 0444 },
892 void module_put(struct module
*module
)
896 smp_wmb(); /* see comment in module_refcount */
897 __this_cpu_inc(module
->refptr
->decs
);
899 trace_module_put(module
, _RET_IP_
);
900 /* Maybe they're waiting for us to drop reference? */
901 if (unlikely(!module_is_live(module
)))
902 wake_up_process(module
->waiter
);
906 EXPORT_SYMBOL(module_put
);
908 #else /* !CONFIG_MODULE_UNLOAD */
909 static inline void print_unload_info(struct seq_file
*m
, struct module
*mod
)
911 /* We don't know the usage count, or what modules are using. */
912 seq_printf(m
, " - -");
915 static inline void module_unload_free(struct module
*mod
)
919 int ref_module(struct module
*a
, struct module
*b
)
921 return strong_try_module_get(b
);
923 EXPORT_SYMBOL_GPL(ref_module
);
925 static inline int module_unload_init(struct module
*mod
)
929 #endif /* CONFIG_MODULE_UNLOAD */
931 static ssize_t
show_initstate(struct module_attribute
*mattr
,
932 struct module
*mod
, char *buffer
)
934 const char *state
= "unknown";
936 switch (mod
->state
) {
937 case MODULE_STATE_LIVE
:
940 case MODULE_STATE_COMING
:
943 case MODULE_STATE_GOING
:
947 return sprintf(buffer
, "%s\n", state
);
950 static struct module_attribute initstate
= {
951 .attr
= { .name
= "initstate", .mode
= 0444 },
952 .show
= show_initstate
,
955 static struct module_attribute
*modinfo_attrs
[] = {
959 #ifdef CONFIG_MODULE_UNLOAD
965 static const char vermagic
[] = VERMAGIC_STRING
;
967 static int try_to_force_load(struct module
*mod
, const char *reason
)
969 #ifdef CONFIG_MODULE_FORCE_LOAD
970 if (!test_taint(TAINT_FORCED_MODULE
))
971 printk(KERN_WARNING
"%s: %s: kernel tainted.\n",
973 add_taint_module(mod
, TAINT_FORCED_MODULE
);
980 #ifdef CONFIG_MODVERSIONS
981 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
982 static unsigned long maybe_relocated(unsigned long crc
,
983 const struct module
*crc_owner
)
985 #ifdef ARCH_RELOCATES_KCRCTAB
986 if (crc_owner
== NULL
)
987 return crc
- (unsigned long)reloc_start
;
992 static int check_version(Elf_Shdr
*sechdrs
,
993 unsigned int versindex
,
996 const unsigned long *crc
,
997 const struct module
*crc_owner
)
999 unsigned int i
, num_versions
;
1000 struct modversion_info
*versions
;
1002 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1006 /* No versions at all? modprobe --force does this. */
1008 return try_to_force_load(mod
, symname
) == 0;
1010 versions
= (void *) sechdrs
[versindex
].sh_addr
;
1011 num_versions
= sechdrs
[versindex
].sh_size
1012 / sizeof(struct modversion_info
);
1014 for (i
= 0; i
< num_versions
; i
++) {
1015 if (strcmp(versions
[i
].name
, symname
) != 0)
1018 if (versions
[i
].crc
== maybe_relocated(*crc
, crc_owner
))
1020 DEBUGP("Found checksum %lX vs module %lX\n",
1021 maybe_relocated(*crc
, crc_owner
), versions
[i
].crc
);
1025 printk(KERN_WARNING
"%s: no symbol version for %s\n",
1026 mod
->name
, symname
);
1030 printk("%s: disagrees about version of symbol %s\n",
1031 mod
->name
, symname
);
1035 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1036 unsigned int versindex
,
1039 const unsigned long *crc
;
1041 /* Since this should be found in kernel (which can't be removed),
1042 * no locking is necessary. */
1043 if (!find_symbol(MODULE_SYMBOL_PREFIX
"module_layout", NULL
,
1046 return check_version(sechdrs
, versindex
, "module_layout", mod
, crc
,
1050 /* First part is kernel version, which we ignore if module has crcs. */
1051 static inline int same_magic(const char *amagic
, const char *bmagic
,
1055 amagic
+= strcspn(amagic
, " ");
1056 bmagic
+= strcspn(bmagic
, " ");
1058 return strcmp(amagic
, bmagic
) == 0;
1061 static inline int check_version(Elf_Shdr
*sechdrs
,
1062 unsigned int versindex
,
1063 const char *symname
,
1065 const unsigned long *crc
,
1066 const struct module
*crc_owner
)
1071 static inline int check_modstruct_version(Elf_Shdr
*sechdrs
,
1072 unsigned int versindex
,
1078 static inline int same_magic(const char *amagic
, const char *bmagic
,
1081 return strcmp(amagic
, bmagic
) == 0;
1083 #endif /* CONFIG_MODVERSIONS */
1085 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1086 static const struct kernel_symbol
*resolve_symbol(struct module
*mod
,
1087 const struct load_info
*info
,
1091 struct module
*owner
;
1092 const struct kernel_symbol
*sym
;
1093 const unsigned long *crc
;
1096 mutex_lock(&module_mutex
);
1097 sym
= find_symbol(name
, &owner
, &crc
,
1098 !(mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
)), true);
1102 if (!check_version(info
->sechdrs
, info
->index
.vers
, name
, mod
, crc
,
1104 sym
= ERR_PTR(-EINVAL
);
1108 err
= ref_module(mod
, owner
);
1115 /* We must make copy under the lock if we failed to get ref. */
1116 strncpy(ownername
, module_name(owner
), MODULE_NAME_LEN
);
1118 mutex_unlock(&module_mutex
);
1122 static const struct kernel_symbol
*
1123 resolve_symbol_wait(struct module
*mod
,
1124 const struct load_info
*info
,
1127 const struct kernel_symbol
*ksym
;
1128 char owner
[MODULE_NAME_LEN
];
1130 if (wait_event_interruptible_timeout(module_wq
,
1131 !IS_ERR(ksym
= resolve_symbol(mod
, info
, name
, owner
))
1132 || PTR_ERR(ksym
) != -EBUSY
,
1134 printk(KERN_WARNING
"%s: gave up waiting for init of module %s.\n",
1141 * /sys/module/foo/sections stuff
1142 * J. Corbet <corbet@lwn.net>
1146 #ifdef CONFIG_KALLSYMS
1147 static inline bool sect_empty(const Elf_Shdr
*sect
)
1149 return !(sect
->sh_flags
& SHF_ALLOC
) || sect
->sh_size
== 0;
1152 struct module_sect_attr
1154 struct module_attribute mattr
;
1156 unsigned long address
;
1159 struct module_sect_attrs
1161 struct attribute_group grp
;
1162 unsigned int nsections
;
1163 struct module_sect_attr attrs
[0];
1166 static ssize_t
module_sect_show(struct module_attribute
*mattr
,
1167 struct module
*mod
, char *buf
)
1169 struct module_sect_attr
*sattr
=
1170 container_of(mattr
, struct module_sect_attr
, mattr
);
1171 return sprintf(buf
, "0x%lx\n", sattr
->address
);
1174 static void free_sect_attrs(struct module_sect_attrs
*sect_attrs
)
1176 unsigned int section
;
1178 for (section
= 0; section
< sect_attrs
->nsections
; section
++)
1179 kfree(sect_attrs
->attrs
[section
].name
);
1183 static void add_sect_attrs(struct module
*mod
, const struct load_info
*info
)
1185 unsigned int nloaded
= 0, i
, size
[2];
1186 struct module_sect_attrs
*sect_attrs
;
1187 struct module_sect_attr
*sattr
;
1188 struct attribute
**gattr
;
1190 /* Count loaded sections and allocate structures */
1191 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1192 if (!sect_empty(&info
->sechdrs
[i
]))
1194 size
[0] = ALIGN(sizeof(*sect_attrs
)
1195 + nloaded
* sizeof(sect_attrs
->attrs
[0]),
1196 sizeof(sect_attrs
->grp
.attrs
[0]));
1197 size
[1] = (nloaded
+ 1) * sizeof(sect_attrs
->grp
.attrs
[0]);
1198 sect_attrs
= kzalloc(size
[0] + size
[1], GFP_KERNEL
);
1199 if (sect_attrs
== NULL
)
1202 /* Setup section attributes. */
1203 sect_attrs
->grp
.name
= "sections";
1204 sect_attrs
->grp
.attrs
= (void *)sect_attrs
+ size
[0];
1206 sect_attrs
->nsections
= 0;
1207 sattr
= §_attrs
->attrs
[0];
1208 gattr
= §_attrs
->grp
.attrs
[0];
1209 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
1210 Elf_Shdr
*sec
= &info
->sechdrs
[i
];
1211 if (sect_empty(sec
))
1213 sattr
->address
= sec
->sh_addr
;
1214 sattr
->name
= kstrdup(info
->secstrings
+ sec
->sh_name
,
1216 if (sattr
->name
== NULL
)
1218 sect_attrs
->nsections
++;
1219 sysfs_attr_init(&sattr
->mattr
.attr
);
1220 sattr
->mattr
.show
= module_sect_show
;
1221 sattr
->mattr
.store
= NULL
;
1222 sattr
->mattr
.attr
.name
= sattr
->name
;
1223 sattr
->mattr
.attr
.mode
= S_IRUGO
;
1224 *(gattr
++) = &(sattr
++)->mattr
.attr
;
1228 if (sysfs_create_group(&mod
->mkobj
.kobj
, §_attrs
->grp
))
1231 mod
->sect_attrs
= sect_attrs
;
1234 free_sect_attrs(sect_attrs
);
1237 static void remove_sect_attrs(struct module
*mod
)
1239 if (mod
->sect_attrs
) {
1240 sysfs_remove_group(&mod
->mkobj
.kobj
,
1241 &mod
->sect_attrs
->grp
);
1242 /* We are positive that no one is using any sect attrs
1243 * at this point. Deallocate immediately. */
1244 free_sect_attrs(mod
->sect_attrs
);
1245 mod
->sect_attrs
= NULL
;
1250 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1253 struct module_notes_attrs
{
1254 struct kobject
*dir
;
1256 struct bin_attribute attrs
[0];
1259 static ssize_t
module_notes_read(struct file
*filp
, struct kobject
*kobj
,
1260 struct bin_attribute
*bin_attr
,
1261 char *buf
, loff_t pos
, size_t count
)
1264 * The caller checked the pos and count against our size.
1266 memcpy(buf
, bin_attr
->private + pos
, count
);
1270 static void free_notes_attrs(struct module_notes_attrs
*notes_attrs
,
1273 if (notes_attrs
->dir
) {
1275 sysfs_remove_bin_file(notes_attrs
->dir
,
1276 ¬es_attrs
->attrs
[i
]);
1277 kobject_put(notes_attrs
->dir
);
1282 static void add_notes_attrs(struct module
*mod
, const struct load_info
*info
)
1284 unsigned int notes
, loaded
, i
;
1285 struct module_notes_attrs
*notes_attrs
;
1286 struct bin_attribute
*nattr
;
1288 /* failed to create section attributes, so can't create notes */
1289 if (!mod
->sect_attrs
)
1292 /* Count notes sections and allocate structures. */
1294 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1295 if (!sect_empty(&info
->sechdrs
[i
]) &&
1296 (info
->sechdrs
[i
].sh_type
== SHT_NOTE
))
1302 notes_attrs
= kzalloc(sizeof(*notes_attrs
)
1303 + notes
* sizeof(notes_attrs
->attrs
[0]),
1305 if (notes_attrs
== NULL
)
1308 notes_attrs
->notes
= notes
;
1309 nattr
= ¬es_attrs
->attrs
[0];
1310 for (loaded
= i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1311 if (sect_empty(&info
->sechdrs
[i
]))
1313 if (info
->sechdrs
[i
].sh_type
== SHT_NOTE
) {
1314 sysfs_bin_attr_init(nattr
);
1315 nattr
->attr
.name
= mod
->sect_attrs
->attrs
[loaded
].name
;
1316 nattr
->attr
.mode
= S_IRUGO
;
1317 nattr
->size
= info
->sechdrs
[i
].sh_size
;
1318 nattr
->private = (void *) info
->sechdrs
[i
].sh_addr
;
1319 nattr
->read
= module_notes_read
;
1325 notes_attrs
->dir
= kobject_create_and_add("notes", &mod
->mkobj
.kobj
);
1326 if (!notes_attrs
->dir
)
1329 for (i
= 0; i
< notes
; ++i
)
1330 if (sysfs_create_bin_file(notes_attrs
->dir
,
1331 ¬es_attrs
->attrs
[i
]))
1334 mod
->notes_attrs
= notes_attrs
;
1338 free_notes_attrs(notes_attrs
, i
);
1341 static void remove_notes_attrs(struct module
*mod
)
1343 if (mod
->notes_attrs
)
1344 free_notes_attrs(mod
->notes_attrs
, mod
->notes_attrs
->notes
);
1349 static inline void add_sect_attrs(struct module
*mod
,
1350 const struct load_info
*info
)
1354 static inline void remove_sect_attrs(struct module
*mod
)
1358 static inline void add_notes_attrs(struct module
*mod
,
1359 const struct load_info
*info
)
1363 static inline void remove_notes_attrs(struct module
*mod
)
1366 #endif /* CONFIG_KALLSYMS */
1368 static void add_usage_links(struct module
*mod
)
1370 #ifdef CONFIG_MODULE_UNLOAD
1371 struct module_use
*use
;
1374 mutex_lock(&module_mutex
);
1375 list_for_each_entry(use
, &mod
->target_list
, target_list
) {
1376 nowarn
= sysfs_create_link(use
->target
->holders_dir
,
1377 &mod
->mkobj
.kobj
, mod
->name
);
1379 mutex_unlock(&module_mutex
);
1383 static void del_usage_links(struct module
*mod
)
1385 #ifdef CONFIG_MODULE_UNLOAD
1386 struct module_use
*use
;
1388 mutex_lock(&module_mutex
);
1389 list_for_each_entry(use
, &mod
->target_list
, target_list
)
1390 sysfs_remove_link(use
->target
->holders_dir
, mod
->name
);
1391 mutex_unlock(&module_mutex
);
1395 static int module_add_modinfo_attrs(struct module
*mod
)
1397 struct module_attribute
*attr
;
1398 struct module_attribute
*temp_attr
;
1402 mod
->modinfo_attrs
= kzalloc((sizeof(struct module_attribute
) *
1403 (ARRAY_SIZE(modinfo_attrs
) + 1)),
1405 if (!mod
->modinfo_attrs
)
1408 temp_attr
= mod
->modinfo_attrs
;
1409 for (i
= 0; (attr
= modinfo_attrs
[i
]) && !error
; i
++) {
1411 (attr
->test
&& attr
->test(mod
))) {
1412 memcpy(temp_attr
, attr
, sizeof(*temp_attr
));
1413 sysfs_attr_init(&temp_attr
->attr
);
1414 error
= sysfs_create_file(&mod
->mkobj
.kobj
,&temp_attr
->attr
);
1421 static void module_remove_modinfo_attrs(struct module
*mod
)
1423 struct module_attribute
*attr
;
1426 for (i
= 0; (attr
= &mod
->modinfo_attrs
[i
]); i
++) {
1427 /* pick a field to test for end of list */
1428 if (!attr
->attr
.name
)
1430 sysfs_remove_file(&mod
->mkobj
.kobj
,&attr
->attr
);
1434 kfree(mod
->modinfo_attrs
);
1437 static int mod_sysfs_init(struct module
*mod
)
1440 struct kobject
*kobj
;
1442 if (!module_sysfs_initialized
) {
1443 printk(KERN_ERR
"%s: module sysfs not initialized\n",
1449 kobj
= kset_find_obj(module_kset
, mod
->name
);
1451 printk(KERN_ERR
"%s: module is already loaded\n", mod
->name
);
1457 mod
->mkobj
.mod
= mod
;
1459 memset(&mod
->mkobj
.kobj
, 0, sizeof(mod
->mkobj
.kobj
));
1460 mod
->mkobj
.kobj
.kset
= module_kset
;
1461 err
= kobject_init_and_add(&mod
->mkobj
.kobj
, &module_ktype
, NULL
,
1464 kobject_put(&mod
->mkobj
.kobj
);
1466 /* delay uevent until full sysfs population */
1471 static int mod_sysfs_setup(struct module
*mod
,
1472 const struct load_info
*info
,
1473 struct kernel_param
*kparam
,
1474 unsigned int num_params
)
1478 err
= mod_sysfs_init(mod
);
1482 mod
->holders_dir
= kobject_create_and_add("holders", &mod
->mkobj
.kobj
);
1483 if (!mod
->holders_dir
) {
1488 err
= module_param_sysfs_setup(mod
, kparam
, num_params
);
1490 goto out_unreg_holders
;
1492 err
= module_add_modinfo_attrs(mod
);
1494 goto out_unreg_param
;
1496 add_usage_links(mod
);
1497 add_sect_attrs(mod
, info
);
1498 add_notes_attrs(mod
, info
);
1500 kobject_uevent(&mod
->mkobj
.kobj
, KOBJ_ADD
);
1504 module_param_sysfs_remove(mod
);
1506 kobject_put(mod
->holders_dir
);
1508 kobject_put(&mod
->mkobj
.kobj
);
1513 static void mod_sysfs_fini(struct module
*mod
)
1515 remove_notes_attrs(mod
);
1516 remove_sect_attrs(mod
);
1517 kobject_put(&mod
->mkobj
.kobj
);
1520 #else /* !CONFIG_SYSFS */
1522 static int mod_sysfs_setup(struct module
*mod
,
1523 const struct load_info
*info
,
1524 struct kernel_param
*kparam
,
1525 unsigned int num_params
)
1530 static void mod_sysfs_fini(struct module
*mod
)
1534 static void module_remove_modinfo_attrs(struct module
*mod
)
1538 static void del_usage_links(struct module
*mod
)
1542 #endif /* CONFIG_SYSFS */
1544 static void mod_sysfs_teardown(struct module
*mod
)
1546 del_usage_links(mod
);
1547 module_remove_modinfo_attrs(mod
);
1548 module_param_sysfs_remove(mod
);
1549 kobject_put(mod
->mkobj
.drivers_dir
);
1550 kobject_put(mod
->holders_dir
);
1551 mod_sysfs_fini(mod
);
1555 * unlink the module with the whole machine is stopped with interrupts off
1556 * - this defends against kallsyms not taking locks
1558 static int __unlink_module(void *_mod
)
1560 struct module
*mod
= _mod
;
1561 list_del(&mod
->list
);
1562 module_bug_cleanup(mod
);
1566 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
1568 * LKM RO/NX protection: protect module's text/ro-data
1569 * from modification and any data from execution.
1571 void set_page_attributes(void *start
, void *end
, int (*set
)(unsigned long start
, int num_pages
))
1573 unsigned long begin_pfn
= PFN_DOWN((unsigned long)start
);
1574 unsigned long end_pfn
= PFN_DOWN((unsigned long)end
);
1576 if (end_pfn
> begin_pfn
)
1577 set(begin_pfn
<< PAGE_SHIFT
, end_pfn
- begin_pfn
);
1580 static void set_section_ro_nx(void *base
,
1581 unsigned long text_size
,
1582 unsigned long ro_size
,
1583 unsigned long total_size
)
1585 /* begin and end PFNs of the current subsection */
1586 unsigned long begin_pfn
;
1587 unsigned long end_pfn
;
1590 * Set RO for module text and RO-data:
1591 * - Always protect first page.
1592 * - Do not protect last partial page.
1595 set_page_attributes(base
, base
+ ro_size
, set_memory_ro
);
1598 * Set NX permissions for module data:
1599 * - Do not protect first partial page.
1600 * - Always protect last page.
1602 if (total_size
> text_size
) {
1603 begin_pfn
= PFN_UP((unsigned long)base
+ text_size
);
1604 end_pfn
= PFN_UP((unsigned long)base
+ total_size
);
1605 if (end_pfn
> begin_pfn
)
1606 set_memory_nx(begin_pfn
<< PAGE_SHIFT
, end_pfn
- begin_pfn
);
1610 /* Setting memory back to RW+NX before releasing it */
1611 void unset_section_ro_nx(struct module
*mod
, void *module_region
)
1613 unsigned long total_pages
;
1615 if (mod
->module_core
== module_region
) {
1616 /* Set core as NX+RW */
1617 total_pages
= MOD_NUMBER_OF_PAGES(mod
->module_core
, mod
->core_size
);
1618 set_memory_nx((unsigned long)mod
->module_core
, total_pages
);
1619 set_memory_rw((unsigned long)mod
->module_core
, total_pages
);
1621 } else if (mod
->module_init
== module_region
) {
1622 /* Set init as NX+RW */
1623 total_pages
= MOD_NUMBER_OF_PAGES(mod
->module_init
, mod
->init_size
);
1624 set_memory_nx((unsigned long)mod
->module_init
, total_pages
);
1625 set_memory_rw((unsigned long)mod
->module_init
, total_pages
);
1629 /* Iterate through all modules and set each module's text as RW */
1630 void set_all_modules_text_rw()
1634 mutex_lock(&module_mutex
);
1635 list_for_each_entry_rcu(mod
, &modules
, list
) {
1636 if ((mod
->module_core
) && (mod
->core_text_size
)) {
1637 set_page_attributes(mod
->module_core
,
1638 mod
->module_core
+ mod
->core_text_size
,
1641 if ((mod
->module_init
) && (mod
->init_text_size
)) {
1642 set_page_attributes(mod
->module_init
,
1643 mod
->module_init
+ mod
->init_text_size
,
1647 mutex_unlock(&module_mutex
);
1650 /* Iterate through all modules and set each module's text as RO */
1651 void set_all_modules_text_ro()
1655 mutex_lock(&module_mutex
);
1656 list_for_each_entry_rcu(mod
, &modules
, list
) {
1657 if ((mod
->module_core
) && (mod
->core_text_size
)) {
1658 set_page_attributes(mod
->module_core
,
1659 mod
->module_core
+ mod
->core_text_size
,
1662 if ((mod
->module_init
) && (mod
->init_text_size
)) {
1663 set_page_attributes(mod
->module_init
,
1664 mod
->module_init
+ mod
->init_text_size
,
1668 mutex_unlock(&module_mutex
);
1671 static inline void set_section_ro_nx(void *base
, unsigned long text_size
, unsigned long ro_size
, unsigned long total_size
) { }
1672 static inline void unset_section_ro_nx(struct module
*mod
, void *module_region
) { }
1675 /* Free a module, remove from lists, etc. */
1676 static void free_module(struct module
*mod
)
1678 trace_module_free(mod
);
1680 /* Delete from various lists */
1681 mutex_lock(&module_mutex
);
1682 stop_machine(__unlink_module
, mod
, NULL
);
1683 mutex_unlock(&module_mutex
);
1684 mod_sysfs_teardown(mod
);
1686 /* Remove dynamic debug info */
1687 ddebug_remove_module(mod
->name
);
1689 /* Arch-specific cleanup. */
1690 module_arch_cleanup(mod
);
1692 /* Module unload stuff */
1693 module_unload_free(mod
);
1695 /* Free any allocated parameters. */
1696 destroy_params(mod
->kp
, mod
->num_kp
);
1698 /* This may be NULL, but that's OK */
1699 unset_section_ro_nx(mod
, mod
->module_init
);
1700 module_free(mod
, mod
->module_init
);
1702 percpu_modfree(mod
);
1704 /* Free lock-classes: */
1705 lockdep_free_key_range(mod
->module_core
, mod
->core_size
);
1707 /* Finally, free the core (containing the module structure) */
1708 unset_section_ro_nx(mod
, mod
->module_core
);
1709 module_free(mod
, mod
->module_core
);
1712 update_protections(current
->mm
);
1716 void *__symbol_get(const char *symbol
)
1718 struct module
*owner
;
1719 const struct kernel_symbol
*sym
;
1722 sym
= find_symbol(symbol
, &owner
, NULL
, true, true);
1723 if (sym
&& strong_try_module_get(owner
))
1727 return sym
? (void *)sym
->value
: NULL
;
1729 EXPORT_SYMBOL_GPL(__symbol_get
);
1732 * Ensure that an exported symbol [global namespace] does not already exist
1733 * in the kernel or in some other module's exported symbol table.
1735 * You must hold the module_mutex.
1737 static int verify_export_symbols(struct module
*mod
)
1740 struct module
*owner
;
1741 const struct kernel_symbol
*s
;
1743 const struct kernel_symbol
*sym
;
1746 { mod
->syms
, mod
->num_syms
},
1747 { mod
->gpl_syms
, mod
->num_gpl_syms
},
1748 { mod
->gpl_future_syms
, mod
->num_gpl_future_syms
},
1749 #ifdef CONFIG_UNUSED_SYMBOLS
1750 { mod
->unused_syms
, mod
->num_unused_syms
},
1751 { mod
->unused_gpl_syms
, mod
->num_unused_gpl_syms
},
1755 for (i
= 0; i
< ARRAY_SIZE(arr
); i
++) {
1756 for (s
= arr
[i
].sym
; s
< arr
[i
].sym
+ arr
[i
].num
; s
++) {
1757 if (find_symbol(s
->name
, &owner
, NULL
, true, false)) {
1759 "%s: exports duplicate symbol %s"
1761 mod
->name
, s
->name
, module_name(owner
));
1769 /* Change all symbols so that st_value encodes the pointer directly. */
1770 static int simplify_symbols(struct module
*mod
, const struct load_info
*info
)
1772 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
1773 Elf_Sym
*sym
= (void *)symsec
->sh_addr
;
1774 unsigned long secbase
;
1777 const struct kernel_symbol
*ksym
;
1779 for (i
= 1; i
< symsec
->sh_size
/ sizeof(Elf_Sym
); i
++) {
1780 const char *name
= info
->strtab
+ sym
[i
].st_name
;
1782 switch (sym
[i
].st_shndx
) {
1784 /* We compiled with -fno-common. These are not
1785 supposed to happen. */
1786 DEBUGP("Common symbol: %s\n", name
);
1787 printk("%s: please compile with -fno-common\n",
1793 /* Don't need to do anything */
1794 DEBUGP("Absolute symbol: 0x%08lx\n",
1795 (long)sym
[i
].st_value
);
1799 ksym
= resolve_symbol_wait(mod
, info
, name
);
1800 /* Ok if resolved. */
1801 if (ksym
&& !IS_ERR(ksym
)) {
1802 sym
[i
].st_value
= ksym
->value
;
1807 if (!ksym
&& ELF_ST_BIND(sym
[i
].st_info
) == STB_WEAK
)
1810 printk(KERN_WARNING
"%s: Unknown symbol %s (err %li)\n",
1811 mod
->name
, name
, PTR_ERR(ksym
));
1812 ret
= PTR_ERR(ksym
) ?: -ENOENT
;
1816 /* Divert to percpu allocation if a percpu var. */
1817 if (sym
[i
].st_shndx
== info
->index
.pcpu
)
1818 secbase
= (unsigned long)mod_percpu(mod
);
1820 secbase
= info
->sechdrs
[sym
[i
].st_shndx
].sh_addr
;
1821 sym
[i
].st_value
+= secbase
;
1829 static int apply_relocations(struct module
*mod
, const struct load_info
*info
)
1834 /* Now do relocations. */
1835 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
1836 unsigned int infosec
= info
->sechdrs
[i
].sh_info
;
1838 /* Not a valid relocation section? */
1839 if (infosec
>= info
->hdr
->e_shnum
)
1842 /* Don't bother with non-allocated sections */
1843 if (!(info
->sechdrs
[infosec
].sh_flags
& SHF_ALLOC
))
1846 if (info
->sechdrs
[i
].sh_type
== SHT_REL
)
1847 err
= apply_relocate(info
->sechdrs
, info
->strtab
,
1848 info
->index
.sym
, i
, mod
);
1849 else if (info
->sechdrs
[i
].sh_type
== SHT_RELA
)
1850 err
= apply_relocate_add(info
->sechdrs
, info
->strtab
,
1851 info
->index
.sym
, i
, mod
);
1858 /* Additional bytes needed by arch in front of individual sections */
1859 unsigned int __weak
arch_mod_section_prepend(struct module
*mod
,
1860 unsigned int section
)
1862 /* default implementation just returns zero */
1866 /* Update size with this section: return offset. */
1867 static long get_offset(struct module
*mod
, unsigned int *size
,
1868 Elf_Shdr
*sechdr
, unsigned int section
)
1872 *size
+= arch_mod_section_prepend(mod
, section
);
1873 ret
= ALIGN(*size
, sechdr
->sh_addralign
?: 1);
1874 *size
= ret
+ sechdr
->sh_size
;
1878 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1879 might -- code, read-only data, read-write data, small data. Tally
1880 sizes, and place the offsets into sh_entsize fields: high bit means it
1882 static void layout_sections(struct module
*mod
, struct load_info
*info
)
1884 static unsigned long const masks
[][2] = {
1885 /* NOTE: all executable code must be the first section
1886 * in this array; otherwise modify the text_size
1887 * finder in the two loops below */
1888 { SHF_EXECINSTR
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1889 { SHF_ALLOC
, SHF_WRITE
| ARCH_SHF_SMALL
},
1890 { SHF_WRITE
| SHF_ALLOC
, ARCH_SHF_SMALL
},
1891 { ARCH_SHF_SMALL
| SHF_ALLOC
, 0 }
1895 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++)
1896 info
->sechdrs
[i
].sh_entsize
= ~0UL;
1898 DEBUGP("Core section allocation order:\n");
1899 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1900 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1901 Elf_Shdr
*s
= &info
->sechdrs
[i
];
1902 const char *sname
= info
->secstrings
+ s
->sh_name
;
1904 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1905 || (s
->sh_flags
& masks
[m
][1])
1906 || s
->sh_entsize
!= ~0UL
1907 || strstarts(sname
, ".init"))
1909 s
->sh_entsize
= get_offset(mod
, &mod
->core_size
, s
, i
);
1910 DEBUGP("\t%s\n", name
);
1913 case 0: /* executable */
1914 mod
->core_size
= debug_align(mod
->core_size
);
1915 mod
->core_text_size
= mod
->core_size
;
1917 case 1: /* RO: text and ro-data */
1918 mod
->core_size
= debug_align(mod
->core_size
);
1919 mod
->core_ro_size
= mod
->core_size
;
1921 case 3: /* whole core */
1922 mod
->core_size
= debug_align(mod
->core_size
);
1927 DEBUGP("Init section allocation order:\n");
1928 for (m
= 0; m
< ARRAY_SIZE(masks
); ++m
) {
1929 for (i
= 0; i
< info
->hdr
->e_shnum
; ++i
) {
1930 Elf_Shdr
*s
= &info
->sechdrs
[i
];
1931 const char *sname
= info
->secstrings
+ s
->sh_name
;
1933 if ((s
->sh_flags
& masks
[m
][0]) != masks
[m
][0]
1934 || (s
->sh_flags
& masks
[m
][1])
1935 || s
->sh_entsize
!= ~0UL
1936 || !strstarts(sname
, ".init"))
1938 s
->sh_entsize
= (get_offset(mod
, &mod
->init_size
, s
, i
)
1939 | INIT_OFFSET_MASK
);
1940 DEBUGP("\t%s\n", sname
);
1943 case 0: /* executable */
1944 mod
->init_size
= debug_align(mod
->init_size
);
1945 mod
->init_text_size
= mod
->init_size
;
1947 case 1: /* RO: text and ro-data */
1948 mod
->init_size
= debug_align(mod
->init_size
);
1949 mod
->init_ro_size
= mod
->init_size
;
1951 case 3: /* whole init */
1952 mod
->init_size
= debug_align(mod
->init_size
);
1958 static void set_license(struct module
*mod
, const char *license
)
1961 license
= "unspecified";
1963 if (!license_is_gpl_compatible(license
)) {
1964 if (!test_taint(TAINT_PROPRIETARY_MODULE
))
1965 printk(KERN_WARNING
"%s: module license '%s' taints "
1966 "kernel.\n", mod
->name
, license
);
1967 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
1971 /* Parse tag=value strings from .modinfo section */
1972 static char *next_string(char *string
, unsigned long *secsize
)
1974 /* Skip non-zero chars */
1977 if ((*secsize
)-- <= 1)
1981 /* Skip any zero padding. */
1982 while (!string
[0]) {
1984 if ((*secsize
)-- <= 1)
1990 static char *get_modinfo(struct load_info
*info
, const char *tag
)
1993 unsigned int taglen
= strlen(tag
);
1994 Elf_Shdr
*infosec
= &info
->sechdrs
[info
->index
.info
];
1995 unsigned long size
= infosec
->sh_size
;
1997 for (p
= (char *)infosec
->sh_addr
; p
; p
= next_string(p
, &size
)) {
1998 if (strncmp(p
, tag
, taglen
) == 0 && p
[taglen
] == '=')
1999 return p
+ taglen
+ 1;
2004 static void setup_modinfo(struct module
*mod
, struct load_info
*info
)
2006 struct module_attribute
*attr
;
2009 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2011 attr
->setup(mod
, get_modinfo(info
, attr
->attr
.name
));
2015 static void free_modinfo(struct module
*mod
)
2017 struct module_attribute
*attr
;
2020 for (i
= 0; (attr
= modinfo_attrs
[i
]); i
++) {
2026 #ifdef CONFIG_KALLSYMS
2028 /* lookup symbol in given range of kernel_symbols */
2029 static const struct kernel_symbol
*lookup_symbol(const char *name
,
2030 const struct kernel_symbol
*start
,
2031 const struct kernel_symbol
*stop
)
2033 const struct kernel_symbol
*ks
= start
;
2034 for (; ks
< stop
; ks
++)
2035 if (strcmp(ks
->name
, name
) == 0)
2040 static int is_exported(const char *name
, unsigned long value
,
2041 const struct module
*mod
)
2043 const struct kernel_symbol
*ks
;
2045 ks
= lookup_symbol(name
, __start___ksymtab
, __stop___ksymtab
);
2047 ks
= lookup_symbol(name
, mod
->syms
, mod
->syms
+ mod
->num_syms
);
2048 return ks
!= NULL
&& ks
->value
== value
;
2052 static char elf_type(const Elf_Sym
*sym
, const struct load_info
*info
)
2054 const Elf_Shdr
*sechdrs
= info
->sechdrs
;
2056 if (ELF_ST_BIND(sym
->st_info
) == STB_WEAK
) {
2057 if (ELF_ST_TYPE(sym
->st_info
) == STT_OBJECT
)
2062 if (sym
->st_shndx
== SHN_UNDEF
)
2064 if (sym
->st_shndx
== SHN_ABS
)
2066 if (sym
->st_shndx
>= SHN_LORESERVE
)
2068 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_EXECINSTR
)
2070 if (sechdrs
[sym
->st_shndx
].sh_flags
& SHF_ALLOC
2071 && sechdrs
[sym
->st_shndx
].sh_type
!= SHT_NOBITS
) {
2072 if (!(sechdrs
[sym
->st_shndx
].sh_flags
& SHF_WRITE
))
2074 else if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2079 if (sechdrs
[sym
->st_shndx
].sh_type
== SHT_NOBITS
) {
2080 if (sechdrs
[sym
->st_shndx
].sh_flags
& ARCH_SHF_SMALL
)
2085 if (strstarts(info
->secstrings
+ sechdrs
[sym
->st_shndx
].sh_name
,
2092 static bool is_core_symbol(const Elf_Sym
*src
, const Elf_Shdr
*sechdrs
,
2095 const Elf_Shdr
*sec
;
2097 if (src
->st_shndx
== SHN_UNDEF
2098 || src
->st_shndx
>= shnum
2102 sec
= sechdrs
+ src
->st_shndx
;
2103 if (!(sec
->sh_flags
& SHF_ALLOC
)
2104 #ifndef CONFIG_KALLSYMS_ALL
2105 || !(sec
->sh_flags
& SHF_EXECINSTR
)
2107 || (sec
->sh_entsize
& INIT_OFFSET_MASK
))
2113 static void layout_symtab(struct module
*mod
, struct load_info
*info
)
2115 Elf_Shdr
*symsect
= info
->sechdrs
+ info
->index
.sym
;
2116 Elf_Shdr
*strsect
= info
->sechdrs
+ info
->index
.str
;
2118 unsigned int i
, nsrc
, ndst
;
2120 /* Put symbol section at end of init part of module. */
2121 symsect
->sh_flags
|= SHF_ALLOC
;
2122 symsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, symsect
,
2123 info
->index
.sym
) | INIT_OFFSET_MASK
;
2124 DEBUGP("\t%s\n", info
->secstrings
+ symsect
->sh_name
);
2126 src
= (void *)info
->hdr
+ symsect
->sh_offset
;
2127 nsrc
= symsect
->sh_size
/ sizeof(*src
);
2128 for (ndst
= i
= 1; i
< nsrc
; ++i
, ++src
)
2129 if (is_core_symbol(src
, info
->sechdrs
, info
->hdr
->e_shnum
)) {
2130 unsigned int j
= src
->st_name
;
2132 while (!__test_and_set_bit(j
, info
->strmap
)
2138 /* Append room for core symbols at end of core part. */
2139 info
->symoffs
= ALIGN(mod
->core_size
, symsect
->sh_addralign
?: 1);
2140 mod
->core_size
= info
->symoffs
+ ndst
* sizeof(Elf_Sym
);
2142 /* Put string table section at end of init part of module. */
2143 strsect
->sh_flags
|= SHF_ALLOC
;
2144 strsect
->sh_entsize
= get_offset(mod
, &mod
->init_size
, strsect
,
2145 info
->index
.str
) | INIT_OFFSET_MASK
;
2146 DEBUGP("\t%s\n", info
->secstrings
+ strsect
->sh_name
);
2148 /* Append room for core symbols' strings at end of core part. */
2149 info
->stroffs
= mod
->core_size
;
2150 __set_bit(0, info
->strmap
);
2151 mod
->core_size
+= bitmap_weight(info
->strmap
, strsect
->sh_size
);
2154 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2156 unsigned int i
, ndst
;
2160 Elf_Shdr
*symsec
= &info
->sechdrs
[info
->index
.sym
];
2162 mod
->symtab
= (void *)symsec
->sh_addr
;
2163 mod
->num_symtab
= symsec
->sh_size
/ sizeof(Elf_Sym
);
2164 /* Make sure we get permanent strtab: don't use info->strtab. */
2165 mod
->strtab
= (void *)info
->sechdrs
[info
->index
.str
].sh_addr
;
2167 /* Set types up while we still have access to sections. */
2168 for (i
= 0; i
< mod
->num_symtab
; i
++)
2169 mod
->symtab
[i
].st_info
= elf_type(&mod
->symtab
[i
], info
);
2171 mod
->core_symtab
= dst
= mod
->module_core
+ info
->symoffs
;
2174 for (ndst
= i
= 1; i
< mod
->num_symtab
; ++i
, ++src
) {
2175 if (!is_core_symbol(src
, info
->sechdrs
, info
->hdr
->e_shnum
))
2178 dst
[ndst
].st_name
= bitmap_weight(info
->strmap
,
2182 mod
->core_num_syms
= ndst
;
2184 mod
->core_strtab
= s
= mod
->module_core
+ info
->stroffs
;
2185 for (*s
= 0, i
= 1; i
< info
->sechdrs
[info
->index
.str
].sh_size
; ++i
)
2186 if (test_bit(i
, info
->strmap
))
2187 *++s
= mod
->strtab
[i
];
2190 static inline void layout_symtab(struct module
*mod
, struct load_info
*info
)
2194 static void add_kallsyms(struct module
*mod
, const struct load_info
*info
)
2197 #endif /* CONFIG_KALLSYMS */
2199 static void dynamic_debug_setup(struct _ddebug
*debug
, unsigned int num
)
2203 #ifdef CONFIG_DYNAMIC_DEBUG
2204 if (ddebug_add_module(debug
, num
, debug
->modname
))
2205 printk(KERN_ERR
"dynamic debug error adding module: %s\n",
2210 static void dynamic_debug_remove(struct _ddebug
*debug
)
2213 ddebug_remove_module(debug
->modname
);
2216 static void *module_alloc_update_bounds(unsigned long size
)
2218 void *ret
= module_alloc(size
);
2221 mutex_lock(&module_mutex
);
2222 /* Update module bounds. */
2223 if ((unsigned long)ret
< module_addr_min
)
2224 module_addr_min
= (unsigned long)ret
;
2225 if ((unsigned long)ret
+ size
> module_addr_max
)
2226 module_addr_max
= (unsigned long)ret
+ size
;
2227 mutex_unlock(&module_mutex
);
2232 #ifdef CONFIG_DEBUG_KMEMLEAK
2233 static void kmemleak_load_module(const struct module
*mod
,
2234 const struct load_info
*info
)
2238 /* only scan the sections containing data */
2239 kmemleak_scan_area(mod
, sizeof(struct module
), GFP_KERNEL
);
2241 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2242 const char *name
= info
->secstrings
+ info
->sechdrs
[i
].sh_name
;
2243 if (!(info
->sechdrs
[i
].sh_flags
& SHF_ALLOC
))
2245 if (!strstarts(name
, ".data") && !strstarts(name
, ".bss"))
2248 kmemleak_scan_area((void *)info
->sechdrs
[i
].sh_addr
,
2249 info
->sechdrs
[i
].sh_size
, GFP_KERNEL
);
2253 static inline void kmemleak_load_module(const struct module
*mod
,
2254 const struct load_info
*info
)
2259 /* Sets info->hdr and info->len. */
2260 static int copy_and_check(struct load_info
*info
,
2261 const void __user
*umod
, unsigned long len
,
2262 const char __user
*uargs
)
2267 if (len
< sizeof(*hdr
))
2270 /* Suck in entire file: we'll want most of it. */
2271 /* vmalloc barfs on "unusual" numbers. Check here */
2272 if (len
> 64 * 1024 * 1024 || (hdr
= vmalloc(len
)) == NULL
)
2275 if (copy_from_user(hdr
, umod
, len
) != 0) {
2280 /* Sanity checks against insmoding binaries or wrong arch,
2281 weird elf version */
2282 if (memcmp(hdr
->e_ident
, ELFMAG
, SELFMAG
) != 0
2283 || hdr
->e_type
!= ET_REL
2284 || !elf_check_arch(hdr
)
2285 || hdr
->e_shentsize
!= sizeof(Elf_Shdr
)) {
2290 if (len
< hdr
->e_shoff
+ hdr
->e_shnum
* sizeof(Elf_Shdr
)) {
2304 static void free_copy(struct load_info
*info
)
2309 static int rewrite_section_headers(struct load_info
*info
)
2313 /* This should always be true, but let's be sure. */
2314 info
->sechdrs
[0].sh_addr
= 0;
2316 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2317 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2318 if (shdr
->sh_type
!= SHT_NOBITS
2319 && info
->len
< shdr
->sh_offset
+ shdr
->sh_size
) {
2320 printk(KERN_ERR
"Module len %lu truncated\n",
2325 /* Mark all sections sh_addr with their address in the
2327 shdr
->sh_addr
= (size_t)info
->hdr
+ shdr
->sh_offset
;
2329 #ifndef CONFIG_MODULE_UNLOAD
2330 /* Don't load .exit sections */
2331 if (strstarts(info
->secstrings
+shdr
->sh_name
, ".exit"))
2332 shdr
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2336 /* Track but don't keep modinfo and version sections. */
2337 info
->index
.vers
= find_sec(info
, "__versions");
2338 info
->index
.info
= find_sec(info
, ".modinfo");
2339 info
->sechdrs
[info
->index
.info
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2340 info
->sechdrs
[info
->index
.vers
].sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2345 * Set up our basic convenience variables (pointers to section headers,
2346 * search for module section index etc), and do some basic section
2349 * Return the temporary module pointer (we'll replace it with the final
2350 * one when we move the module sections around).
2352 static struct module
*setup_load_info(struct load_info
*info
)
2358 /* Set up the convenience variables */
2359 info
->sechdrs
= (void *)info
->hdr
+ info
->hdr
->e_shoff
;
2360 info
->secstrings
= (void *)info
->hdr
2361 + info
->sechdrs
[info
->hdr
->e_shstrndx
].sh_offset
;
2363 err
= rewrite_section_headers(info
);
2365 return ERR_PTR(err
);
2367 /* Find internal symbols and strings. */
2368 for (i
= 1; i
< info
->hdr
->e_shnum
; i
++) {
2369 if (info
->sechdrs
[i
].sh_type
== SHT_SYMTAB
) {
2370 info
->index
.sym
= i
;
2371 info
->index
.str
= info
->sechdrs
[i
].sh_link
;
2372 info
->strtab
= (char *)info
->hdr
2373 + info
->sechdrs
[info
->index
.str
].sh_offset
;
2378 info
->index
.mod
= find_sec(info
, ".gnu.linkonce.this_module");
2379 if (!info
->index
.mod
) {
2380 printk(KERN_WARNING
"No module found in object\n");
2381 return ERR_PTR(-ENOEXEC
);
2383 /* This is temporary: point mod into copy of data. */
2384 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2386 if (info
->index
.sym
== 0) {
2387 printk(KERN_WARNING
"%s: module has no symbols (stripped?)\n",
2389 return ERR_PTR(-ENOEXEC
);
2392 info
->index
.pcpu
= find_pcpusec(info
);
2394 /* Check module struct version now, before we try to use module. */
2395 if (!check_modstruct_version(info
->sechdrs
, info
->index
.vers
, mod
))
2396 return ERR_PTR(-ENOEXEC
);
2401 static int check_modinfo(struct module
*mod
, struct load_info
*info
)
2403 const char *modmagic
= get_modinfo(info
, "vermagic");
2406 /* This is allowed: modprobe --force will invalidate it. */
2408 err
= try_to_force_load(mod
, "bad vermagic");
2411 } else if (!same_magic(modmagic
, vermagic
, info
->index
.vers
)) {
2412 printk(KERN_ERR
"%s: version magic '%s' should be '%s'\n",
2413 mod
->name
, modmagic
, vermagic
);
2417 if (get_modinfo(info
, "staging")) {
2418 add_taint_module(mod
, TAINT_CRAP
);
2419 printk(KERN_WARNING
"%s: module is from the staging directory,"
2420 " the quality is unknown, you have been warned.\n",
2424 /* Set up license info based on the info section */
2425 set_license(mod
, get_modinfo(info
, "license"));
2430 static void find_module_sections(struct module
*mod
, struct load_info
*info
)
2432 mod
->kp
= section_objs(info
, "__param",
2433 sizeof(*mod
->kp
), &mod
->num_kp
);
2434 mod
->syms
= section_objs(info
, "__ksymtab",
2435 sizeof(*mod
->syms
), &mod
->num_syms
);
2436 mod
->crcs
= section_addr(info
, "__kcrctab");
2437 mod
->gpl_syms
= section_objs(info
, "__ksymtab_gpl",
2438 sizeof(*mod
->gpl_syms
),
2439 &mod
->num_gpl_syms
);
2440 mod
->gpl_crcs
= section_addr(info
, "__kcrctab_gpl");
2441 mod
->gpl_future_syms
= section_objs(info
,
2442 "__ksymtab_gpl_future",
2443 sizeof(*mod
->gpl_future_syms
),
2444 &mod
->num_gpl_future_syms
);
2445 mod
->gpl_future_crcs
= section_addr(info
, "__kcrctab_gpl_future");
2447 #ifdef CONFIG_UNUSED_SYMBOLS
2448 mod
->unused_syms
= section_objs(info
, "__ksymtab_unused",
2449 sizeof(*mod
->unused_syms
),
2450 &mod
->num_unused_syms
);
2451 mod
->unused_crcs
= section_addr(info
, "__kcrctab_unused");
2452 mod
->unused_gpl_syms
= section_objs(info
, "__ksymtab_unused_gpl",
2453 sizeof(*mod
->unused_gpl_syms
),
2454 &mod
->num_unused_gpl_syms
);
2455 mod
->unused_gpl_crcs
= section_addr(info
, "__kcrctab_unused_gpl");
2457 #ifdef CONFIG_CONSTRUCTORS
2458 mod
->ctors
= section_objs(info
, ".ctors",
2459 sizeof(*mod
->ctors
), &mod
->num_ctors
);
2462 #ifdef CONFIG_TRACEPOINTS
2463 mod
->tracepoints
= section_objs(info
, "__tracepoints",
2464 sizeof(*mod
->tracepoints
),
2465 &mod
->num_tracepoints
);
2467 #ifdef HAVE_JUMP_LABEL
2468 mod
->jump_entries
= section_objs(info
, "__jump_table",
2469 sizeof(*mod
->jump_entries
),
2470 &mod
->num_jump_entries
);
2472 #ifdef CONFIG_EVENT_TRACING
2473 mod
->trace_events
= section_objs(info
, "_ftrace_events",
2474 sizeof(*mod
->trace_events
),
2475 &mod
->num_trace_events
);
2477 * This section contains pointers to allocated objects in the trace
2478 * code and not scanning it leads to false positives.
2480 kmemleak_scan_area(mod
->trace_events
, sizeof(*mod
->trace_events
) *
2481 mod
->num_trace_events
, GFP_KERNEL
);
2483 #ifdef CONFIG_TRACING
2484 mod
->trace_bprintk_fmt_start
= section_objs(info
, "__trace_printk_fmt",
2485 sizeof(*mod
->trace_bprintk_fmt_start
),
2486 &mod
->num_trace_bprintk_fmt
);
2488 * This section contains pointers to allocated objects in the trace
2489 * code and not scanning it leads to false positives.
2491 kmemleak_scan_area(mod
->trace_bprintk_fmt_start
,
2492 sizeof(*mod
->trace_bprintk_fmt_start
) *
2493 mod
->num_trace_bprintk_fmt
, GFP_KERNEL
);
2495 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2496 /* sechdrs[0].sh_size is always zero */
2497 mod
->ftrace_callsites
= section_objs(info
, "__mcount_loc",
2498 sizeof(*mod
->ftrace_callsites
),
2499 &mod
->num_ftrace_callsites
);
2502 mod
->extable
= section_objs(info
, "__ex_table",
2503 sizeof(*mod
->extable
), &mod
->num_exentries
);
2505 if (section_addr(info
, "__obsparm"))
2506 printk(KERN_WARNING
"%s: Ignoring obsolete parameters\n",
2509 info
->debug
= section_objs(info
, "__verbose",
2510 sizeof(*info
->debug
), &info
->num_debug
);
2513 static int move_module(struct module
*mod
, struct load_info
*info
)
2518 /* Do the allocs. */
2519 ptr
= module_alloc_update_bounds(mod
->core_size
);
2521 * The pointer to this block is stored in the module structure
2522 * which is inside the block. Just mark it as not being a
2525 kmemleak_not_leak(ptr
);
2529 memset(ptr
, 0, mod
->core_size
);
2530 mod
->module_core
= ptr
;
2532 ptr
= module_alloc_update_bounds(mod
->init_size
);
2534 * The pointer to this block is stored in the module structure
2535 * which is inside the block. This block doesn't need to be
2536 * scanned as it contains data and code that will be freed
2537 * after the module is initialized.
2539 kmemleak_ignore(ptr
);
2540 if (!ptr
&& mod
->init_size
) {
2541 module_free(mod
, mod
->module_core
);
2544 memset(ptr
, 0, mod
->init_size
);
2545 mod
->module_init
= ptr
;
2547 /* Transfer each section which specifies SHF_ALLOC */
2548 DEBUGP("final section addresses:\n");
2549 for (i
= 0; i
< info
->hdr
->e_shnum
; i
++) {
2551 Elf_Shdr
*shdr
= &info
->sechdrs
[i
];
2553 if (!(shdr
->sh_flags
& SHF_ALLOC
))
2556 if (shdr
->sh_entsize
& INIT_OFFSET_MASK
)
2557 dest
= mod
->module_init
2558 + (shdr
->sh_entsize
& ~INIT_OFFSET_MASK
);
2560 dest
= mod
->module_core
+ shdr
->sh_entsize
;
2562 if (shdr
->sh_type
!= SHT_NOBITS
)
2563 memcpy(dest
, (void *)shdr
->sh_addr
, shdr
->sh_size
);
2564 /* Update sh_addr to point to copy in image. */
2565 shdr
->sh_addr
= (unsigned long)dest
;
2566 DEBUGP("\t0x%lx %s\n",
2567 shdr
->sh_addr
, info
->secstrings
+ shdr
->sh_name
);
2573 static int check_module_license_and_versions(struct module
*mod
)
2576 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2577 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2578 * using GPL-only symbols it needs.
2580 if (strcmp(mod
->name
, "ndiswrapper") == 0)
2581 add_taint(TAINT_PROPRIETARY_MODULE
);
2583 /* driverloader was caught wrongly pretending to be under GPL */
2584 if (strcmp(mod
->name
, "driverloader") == 0)
2585 add_taint_module(mod
, TAINT_PROPRIETARY_MODULE
);
2587 #ifdef CONFIG_MODVERSIONS
2588 if ((mod
->num_syms
&& !mod
->crcs
)
2589 || (mod
->num_gpl_syms
&& !mod
->gpl_crcs
)
2590 || (mod
->num_gpl_future_syms
&& !mod
->gpl_future_crcs
)
2591 #ifdef CONFIG_UNUSED_SYMBOLS
2592 || (mod
->num_unused_syms
&& !mod
->unused_crcs
)
2593 || (mod
->num_unused_gpl_syms
&& !mod
->unused_gpl_crcs
)
2596 return try_to_force_load(mod
,
2597 "no versions for exported symbols");
2603 static void flush_module_icache(const struct module
*mod
)
2605 mm_segment_t old_fs
;
2607 /* flush the icache in correct context */
2612 * Flush the instruction cache, since we've played with text.
2613 * Do it before processing of module parameters, so the module
2614 * can provide parameter accessor functions of its own.
2616 if (mod
->module_init
)
2617 flush_icache_range((unsigned long)mod
->module_init
,
2618 (unsigned long)mod
->module_init
2620 flush_icache_range((unsigned long)mod
->module_core
,
2621 (unsigned long)mod
->module_core
+ mod
->core_size
);
2626 static struct module
*layout_and_allocate(struct load_info
*info
)
2628 /* Module within temporary copy. */
2633 mod
= setup_load_info(info
);
2637 err
= check_modinfo(mod
, info
);
2639 return ERR_PTR(err
);
2641 /* Allow arches to frob section contents and sizes. */
2642 err
= module_frob_arch_sections(info
->hdr
, info
->sechdrs
,
2643 info
->secstrings
, mod
);
2647 pcpusec
= &info
->sechdrs
[info
->index
.pcpu
];
2648 if (pcpusec
->sh_size
) {
2649 /* We have a special allocation for this section. */
2650 err
= percpu_modalloc(mod
,
2651 pcpusec
->sh_size
, pcpusec
->sh_addralign
);
2654 pcpusec
->sh_flags
&= ~(unsigned long)SHF_ALLOC
;
2657 /* Determine total sizes, and put offsets in sh_entsize. For now
2658 this is done generically; there doesn't appear to be any
2659 special cases for the architectures. */
2660 layout_sections(mod
, info
);
2662 info
->strmap
= kzalloc(BITS_TO_LONGS(info
->sechdrs
[info
->index
.str
].sh_size
)
2663 * sizeof(long), GFP_KERNEL
);
2664 if (!info
->strmap
) {
2668 layout_symtab(mod
, info
);
2670 /* Allocate and move to the final place */
2671 err
= move_module(mod
, info
);
2675 /* Module has been copied to its final place now: return it. */
2676 mod
= (void *)info
->sechdrs
[info
->index
.mod
].sh_addr
;
2677 kmemleak_load_module(mod
, info
);
2681 kfree(info
->strmap
);
2683 percpu_modfree(mod
);
2685 return ERR_PTR(err
);
2688 /* mod is no longer valid after this! */
2689 static void module_deallocate(struct module
*mod
, struct load_info
*info
)
2691 kfree(info
->strmap
);
2692 percpu_modfree(mod
);
2693 module_free(mod
, mod
->module_init
);
2694 module_free(mod
, mod
->module_core
);
2697 static int post_relocation(struct module
*mod
, const struct load_info
*info
)
2699 /* Sort exception table now relocations are done. */
2700 sort_extable(mod
->extable
, mod
->extable
+ mod
->num_exentries
);
2702 /* Copy relocated percpu area over. */
2703 percpu_modcopy(mod
, (void *)info
->sechdrs
[info
->index
.pcpu
].sh_addr
,
2704 info
->sechdrs
[info
->index
.pcpu
].sh_size
);
2706 /* Setup kallsyms-specific fields. */
2707 add_kallsyms(mod
, info
);
2709 /* Arch-specific module finalizing. */
2710 return module_finalize(info
->hdr
, info
->sechdrs
, mod
);
2713 /* Allocate and load the module: note that size of section 0 is always
2714 zero, and we rely on this for optional sections. */
2715 static struct module
*load_module(void __user
*umod
,
2717 const char __user
*uargs
)
2719 struct load_info info
= { NULL
, };
2723 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2726 /* Copy in the blobs from userspace, check they are vaguely sane. */
2727 err
= copy_and_check(&info
, umod
, len
, uargs
);
2729 return ERR_PTR(err
);
2731 /* Figure out module layout, and allocate all the memory. */
2732 mod
= layout_and_allocate(&info
);
2738 /* Now module is in final location, initialize linked lists, etc. */
2739 err
= module_unload_init(mod
);
2743 /* Now we've got everything in the final locations, we can
2744 * find optional sections. */
2745 find_module_sections(mod
, &info
);
2747 err
= check_module_license_and_versions(mod
);
2751 /* Set up MODINFO_ATTR fields */
2752 setup_modinfo(mod
, &info
);
2754 /* Fix up syms, so that st_value is a pointer to location. */
2755 err
= simplify_symbols(mod
, &info
);
2759 err
= apply_relocations(mod
, &info
);
2763 err
= post_relocation(mod
, &info
);
2767 flush_module_icache(mod
);
2769 /* Now copy in args */
2770 mod
->args
= strndup_user(uargs
, ~0UL >> 1);
2771 if (IS_ERR(mod
->args
)) {
2772 err
= PTR_ERR(mod
->args
);
2773 goto free_arch_cleanup
;
2776 /* Mark state as coming so strong_try_module_get() ignores us. */
2777 mod
->state
= MODULE_STATE_COMING
;
2779 /* Now sew it into the lists so we can get lockdep and oops
2780 * info during argument parsing. Noone should access us, since
2781 * strong_try_module_get() will fail.
2782 * lockdep/oops can run asynchronous, so use the RCU list insertion
2783 * function to insert in a way safe to concurrent readers.
2784 * The mutex protects against concurrent writers.
2786 mutex_lock(&module_mutex
);
2787 if (find_module(mod
->name
)) {
2792 /* This has to be done once we're sure module name is unique. */
2794 dynamic_debug_setup(info
.debug
, info
.num_debug
);
2796 /* Find duplicate symbols */
2797 err
= verify_export_symbols(mod
);
2801 module_bug_finalize(info
.hdr
, info
.sechdrs
, mod
);
2802 list_add_rcu(&mod
->list
, &modules
);
2803 mutex_unlock(&module_mutex
);
2805 /* Module is ready to execute: parsing args may do that. */
2806 err
= parse_args(mod
->name
, mod
->args
, mod
->kp
, mod
->num_kp
, NULL
);
2810 /* Link in to syfs. */
2811 err
= mod_sysfs_setup(mod
, &info
, mod
->kp
, mod
->num_kp
);
2815 /* Get rid of temporary copy and strmap. */
2820 trace_module_load(mod
);
2824 mutex_lock(&module_mutex
);
2825 /* Unlink carefully: kallsyms could be walking list. */
2826 list_del_rcu(&mod
->list
);
2827 module_bug_cleanup(mod
);
2831 dynamic_debug_remove(info
.debug
);
2833 mutex_unlock(&module_mutex
);
2834 synchronize_sched();
2837 module_arch_cleanup(mod
);
2841 module_unload_free(mod
);
2843 module_deallocate(mod
, &info
);
2846 return ERR_PTR(err
);
2849 /* Call module constructors. */
2850 static void do_mod_ctors(struct module
*mod
)
2852 #ifdef CONFIG_CONSTRUCTORS
2855 for (i
= 0; i
< mod
->num_ctors
; i
++)
2860 /* This is where the real work happens */
2861 SYSCALL_DEFINE3(init_module
, void __user
*, umod
,
2862 unsigned long, len
, const char __user
*, uargs
)
2867 /* Must have permission */
2868 if (!capable(CAP_SYS_MODULE
) || modules_disabled
)
2871 /* Do all the hard work */
2872 mod
= load_module(umod
, len
, uargs
);
2874 return PTR_ERR(mod
);
2876 blocking_notifier_call_chain(&module_notify_list
,
2877 MODULE_STATE_COMING
, mod
);
2879 /* Set RO and NX regions for core */
2880 set_section_ro_nx(mod
->module_core
,
2881 mod
->core_text_size
,
2885 /* Set RO and NX regions for init */
2886 set_section_ro_nx(mod
->module_init
,
2887 mod
->init_text_size
,
2892 /* Start the module */
2893 if (mod
->init
!= NULL
)
2894 ret
= do_one_initcall(mod
->init
);
2896 /* Init routine failed: abort. Try to protect us from
2897 buggy refcounters. */
2898 mod
->state
= MODULE_STATE_GOING
;
2899 synchronize_sched();
2901 blocking_notifier_call_chain(&module_notify_list
,
2902 MODULE_STATE_GOING
, mod
);
2904 wake_up(&module_wq
);
2909 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2910 "%s: loading module anyway...\n",
2911 __func__
, mod
->name
, ret
,
2916 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2917 mod
->state
= MODULE_STATE_LIVE
;
2918 wake_up(&module_wq
);
2919 blocking_notifier_call_chain(&module_notify_list
,
2920 MODULE_STATE_LIVE
, mod
);
2922 /* We need to finish all async code before the module init sequence is done */
2923 async_synchronize_full();
2925 mutex_lock(&module_mutex
);
2926 /* Drop initial reference. */
2928 trim_init_extable(mod
);
2929 #ifdef CONFIG_KALLSYMS
2930 mod
->num_symtab
= mod
->core_num_syms
;
2931 mod
->symtab
= mod
->core_symtab
;
2932 mod
->strtab
= mod
->core_strtab
;
2934 unset_section_ro_nx(mod
, mod
->module_init
);
2935 module_free(mod
, mod
->module_init
);
2936 mod
->module_init
= NULL
;
2938 mod
->init_text_size
= 0;
2939 mutex_unlock(&module_mutex
);
2944 static inline int within(unsigned long addr
, void *start
, unsigned long size
)
2946 return ((void *)addr
>= start
&& (void *)addr
< start
+ size
);
2949 #ifdef CONFIG_KALLSYMS
2951 * This ignores the intensely annoying "mapping symbols" found
2952 * in ARM ELF files: $a, $t and $d.
2954 static inline int is_arm_mapping_symbol(const char *str
)
2956 return str
[0] == '$' && strchr("atd", str
[1])
2957 && (str
[2] == '\0' || str
[2] == '.');
2960 static const char *get_ksymbol(struct module
*mod
,
2962 unsigned long *size
,
2963 unsigned long *offset
)
2965 unsigned int i
, best
= 0;
2966 unsigned long nextval
;
2968 /* At worse, next value is at end of module */
2969 if (within_module_init(addr
, mod
))
2970 nextval
= (unsigned long)mod
->module_init
+mod
->init_text_size
;
2972 nextval
= (unsigned long)mod
->module_core
+mod
->core_text_size
;
2974 /* Scan for closest preceeding symbol, and next symbol. (ELF
2975 starts real symbols at 1). */
2976 for (i
= 1; i
< mod
->num_symtab
; i
++) {
2977 if (mod
->symtab
[i
].st_shndx
== SHN_UNDEF
)
2980 /* We ignore unnamed symbols: they're uninformative
2981 * and inserted at a whim. */
2982 if (mod
->symtab
[i
].st_value
<= addr
2983 && mod
->symtab
[i
].st_value
> mod
->symtab
[best
].st_value
2984 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2985 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2987 if (mod
->symtab
[i
].st_value
> addr
2988 && mod
->symtab
[i
].st_value
< nextval
2989 && *(mod
->strtab
+ mod
->symtab
[i
].st_name
) != '\0'
2990 && !is_arm_mapping_symbol(mod
->strtab
+ mod
->symtab
[i
].st_name
))
2991 nextval
= mod
->symtab
[i
].st_value
;
2998 *size
= nextval
- mod
->symtab
[best
].st_value
;
3000 *offset
= addr
- mod
->symtab
[best
].st_value
;
3001 return mod
->strtab
+ mod
->symtab
[best
].st_name
;
3004 /* For kallsyms to ask for address resolution. NULL means not found. Careful
3005 * not to lock to avoid deadlock on oopses, simply disable preemption. */
3006 const char *module_address_lookup(unsigned long addr
,
3007 unsigned long *size
,
3008 unsigned long *offset
,
3013 const char *ret
= NULL
;
3016 list_for_each_entry_rcu(mod
, &modules
, list
) {
3017 if (within_module_init(addr
, mod
) ||
3018 within_module_core(addr
, mod
)) {
3020 *modname
= mod
->name
;
3021 ret
= get_ksymbol(mod
, addr
, size
, offset
);
3025 /* Make a copy in here where it's safe */
3027 strncpy(namebuf
, ret
, KSYM_NAME_LEN
- 1);
3034 int lookup_module_symbol_name(unsigned long addr
, char *symname
)
3039 list_for_each_entry_rcu(mod
, &modules
, list
) {
3040 if (within_module_init(addr
, mod
) ||
3041 within_module_core(addr
, mod
)) {
3044 sym
= get_ksymbol(mod
, addr
, NULL
, NULL
);
3047 strlcpy(symname
, sym
, KSYM_NAME_LEN
);
3057 int lookup_module_symbol_attrs(unsigned long addr
, unsigned long *size
,
3058 unsigned long *offset
, char *modname
, char *name
)
3063 list_for_each_entry_rcu(mod
, &modules
, list
) {
3064 if (within_module_init(addr
, mod
) ||
3065 within_module_core(addr
, mod
)) {
3068 sym
= get_ksymbol(mod
, addr
, size
, offset
);
3072 strlcpy(modname
, mod
->name
, MODULE_NAME_LEN
);
3074 strlcpy(name
, sym
, KSYM_NAME_LEN
);
3084 int module_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
3085 char *name
, char *module_name
, int *exported
)
3090 list_for_each_entry_rcu(mod
, &modules
, list
) {
3091 if (symnum
< mod
->num_symtab
) {
3092 *value
= mod
->symtab
[symnum
].st_value
;
3093 *type
= mod
->symtab
[symnum
].st_info
;
3094 strlcpy(name
, mod
->strtab
+ mod
->symtab
[symnum
].st_name
,
3096 strlcpy(module_name
, mod
->name
, MODULE_NAME_LEN
);
3097 *exported
= is_exported(name
, *value
, mod
);
3101 symnum
-= mod
->num_symtab
;
3107 static unsigned long mod_find_symname(struct module
*mod
, const char *name
)
3111 for (i
= 0; i
< mod
->num_symtab
; i
++)
3112 if (strcmp(name
, mod
->strtab
+mod
->symtab
[i
].st_name
) == 0 &&
3113 mod
->symtab
[i
].st_info
!= 'U')
3114 return mod
->symtab
[i
].st_value
;
3118 /* Look for this name: can be of form module:name. */
3119 unsigned long module_kallsyms_lookup_name(const char *name
)
3123 unsigned long ret
= 0;
3125 /* Don't lock: we're in enough trouble already. */
3127 if ((colon
= strchr(name
, ':')) != NULL
) {
3129 if ((mod
= find_module(name
)) != NULL
)
3130 ret
= mod_find_symname(mod
, colon
+1);
3133 list_for_each_entry_rcu(mod
, &modules
, list
)
3134 if ((ret
= mod_find_symname(mod
, name
)) != 0)
3141 int module_kallsyms_on_each_symbol(int (*fn
)(void *, const char *,
3142 struct module
*, unsigned long),
3149 list_for_each_entry(mod
, &modules
, list
) {
3150 for (i
= 0; i
< mod
->num_symtab
; i
++) {
3151 ret
= fn(data
, mod
->strtab
+ mod
->symtab
[i
].st_name
,
3152 mod
, mod
->symtab
[i
].st_value
);
3159 #endif /* CONFIG_KALLSYMS */
3161 static char *module_flags(struct module
*mod
, char *buf
)
3166 mod
->state
== MODULE_STATE_GOING
||
3167 mod
->state
== MODULE_STATE_COMING
) {
3169 if (mod
->taints
& (1 << TAINT_PROPRIETARY_MODULE
))
3171 if (mod
->taints
& (1 << TAINT_FORCED_MODULE
))
3173 if (mod
->taints
& (1 << TAINT_CRAP
))
3176 * TAINT_FORCED_RMMOD: could be added.
3177 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
3181 /* Show a - for module-is-being-unloaded */
3182 if (mod
->state
== MODULE_STATE_GOING
)
3184 /* Show a + for module-is-being-loaded */
3185 if (mod
->state
== MODULE_STATE_COMING
)
3194 #ifdef CONFIG_PROC_FS
3195 /* Called by the /proc file system to return a list of modules. */
3196 static void *m_start(struct seq_file
*m
, loff_t
*pos
)
3198 mutex_lock(&module_mutex
);
3199 return seq_list_start(&modules
, *pos
);
3202 static void *m_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
3204 return seq_list_next(p
, &modules
, pos
);
3207 static void m_stop(struct seq_file
*m
, void *p
)
3209 mutex_unlock(&module_mutex
);
3212 static int m_show(struct seq_file
*m
, void *p
)
3214 struct module
*mod
= list_entry(p
, struct module
, list
);
3217 seq_printf(m
, "%s %u",
3218 mod
->name
, mod
->init_size
+ mod
->core_size
);
3219 print_unload_info(m
, mod
);
3221 /* Informative for users. */
3222 seq_printf(m
, " %s",
3223 mod
->state
== MODULE_STATE_GOING
? "Unloading":
3224 mod
->state
== MODULE_STATE_COMING
? "Loading":
3226 /* Used by oprofile and other similar tools. */
3227 seq_printf(m
, " 0x%p", mod
->module_core
);
3231 seq_printf(m
, " %s", module_flags(mod
, buf
));
3233 seq_printf(m
, "\n");
3237 /* Format: modulename size refcount deps address
3239 Where refcount is a number or -, and deps is a comma-separated list
3242 static const struct seq_operations modules_op
= {
3249 static int modules_open(struct inode
*inode
, struct file
*file
)
3251 return seq_open(file
, &modules_op
);
3254 static const struct file_operations proc_modules_operations
= {
3255 .open
= modules_open
,
3257 .llseek
= seq_lseek
,
3258 .release
= seq_release
,
3261 static int __init
proc_modules_init(void)
3263 proc_create("modules", 0, NULL
, &proc_modules_operations
);
3266 module_init(proc_modules_init
);
3269 /* Given an address, look for it in the module exception tables. */
3270 const struct exception_table_entry
*search_module_extables(unsigned long addr
)
3272 const struct exception_table_entry
*e
= NULL
;
3276 list_for_each_entry_rcu(mod
, &modules
, list
) {
3277 if (mod
->num_exentries
== 0)
3280 e
= search_extable(mod
->extable
,
3281 mod
->extable
+ mod
->num_exentries
- 1,
3288 /* Now, if we found one, we are running inside it now, hence
3289 we cannot unload the module, hence no refcnt needed. */
3294 * is_module_address - is this address inside a module?
3295 * @addr: the address to check.
3297 * See is_module_text_address() if you simply want to see if the address
3298 * is code (not data).
3300 bool is_module_address(unsigned long addr
)
3305 ret
= __module_address(addr
) != NULL
;
3312 * __module_address - get the module which contains an address.
3313 * @addr: the address.
3315 * Must be called with preempt disabled or module mutex held so that
3316 * module doesn't get freed during this.
3318 struct module
*__module_address(unsigned long addr
)
3322 if (addr
< module_addr_min
|| addr
> module_addr_max
)
3325 list_for_each_entry_rcu(mod
, &modules
, list
)
3326 if (within_module_core(addr
, mod
)
3327 || within_module_init(addr
, mod
))
3331 EXPORT_SYMBOL_GPL(__module_address
);
3334 * is_module_text_address - is this address inside module code?
3335 * @addr: the address to check.
3337 * See is_module_address() if you simply want to see if the address is
3338 * anywhere in a module. See kernel_text_address() for testing if an
3339 * address corresponds to kernel or module code.
3341 bool is_module_text_address(unsigned long addr
)
3346 ret
= __module_text_address(addr
) != NULL
;
3353 * __module_text_address - get the module whose code contains an address.
3354 * @addr: the address.
3356 * Must be called with preempt disabled or module mutex held so that
3357 * module doesn't get freed during this.
3359 struct module
*__module_text_address(unsigned long addr
)
3361 struct module
*mod
= __module_address(addr
);
3363 /* Make sure it's within the text section. */
3364 if (!within(addr
, mod
->module_init
, mod
->init_text_size
)
3365 && !within(addr
, mod
->module_core
, mod
->core_text_size
))
3370 EXPORT_SYMBOL_GPL(__module_text_address
);
3372 /* Don't grab lock, we're oopsing. */
3373 void print_modules(void)
3378 printk(KERN_DEFAULT
"Modules linked in:");
3379 /* Most callers should already have preempt disabled, but make sure */
3381 list_for_each_entry_rcu(mod
, &modules
, list
)
3382 printk(" %s%s", mod
->name
, module_flags(mod
, buf
));
3384 if (last_unloaded_module
[0])
3385 printk(" [last unloaded: %s]", last_unloaded_module
);
3389 #ifdef CONFIG_MODVERSIONS
3390 /* Generate the signature for all relevant module structures here.
3391 * If these change, we don't want to try to parse the module. */
3392 void module_layout(struct module
*mod
,
3393 struct modversion_info
*ver
,
3394 struct kernel_param
*kp
,
3395 struct kernel_symbol
*ks
,
3396 struct tracepoint
*tp
)
3399 EXPORT_SYMBOL(module_layout
);
3402 #ifdef CONFIG_TRACEPOINTS
3403 void module_update_tracepoints(void)
3407 mutex_lock(&module_mutex
);
3408 list_for_each_entry(mod
, &modules
, list
)
3410 tracepoint_update_probe_range(mod
->tracepoints
,
3411 mod
->tracepoints
+ mod
->num_tracepoints
);
3412 mutex_unlock(&module_mutex
);
3416 * Returns 0 if current not found.
3417 * Returns 1 if current found.
3419 int module_get_iter_tracepoints(struct tracepoint_iter
*iter
)
3421 struct module
*iter_mod
;
3424 mutex_lock(&module_mutex
);
3425 list_for_each_entry(iter_mod
, &modules
, list
) {
3426 if (!iter_mod
->taints
) {
3428 * Sorted module list
3430 if (iter_mod
< iter
->module
)
3432 else if (iter_mod
> iter
->module
)
3433 iter
->tracepoint
= NULL
;
3434 found
= tracepoint_get_iter_range(&iter
->tracepoint
,
3435 iter_mod
->tracepoints
,
3436 iter_mod
->tracepoints
3437 + iter_mod
->num_tracepoints
);
3439 iter
->module
= iter_mod
;
3444 mutex_unlock(&module_mutex
);