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/export.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/unwind.h>
48 #include <linux/rculist.h>
49 #include <asm/uaccess.h>
50 #include <asm/cacheflush.h>
51 #include <asm/mmu_context.h>
52 #include <linux/license.h>
53 #include <asm/sections.h>
54 #include <linux/tracepoint.h>
55 #include <linux/ftrace.h>
56 #include <linux/async.h>
57 #include <linux/percpu.h>
58 #include <linux/kmemleak.h>
59 #include <linux/jump_label.h>
60 #include <linux/pfn.h>
61 #include <linux/bsearch.h>
63 #define CREATE_TRACE_POINTS
64 #include <trace/events/module.h>
66 #ifndef ARCH_SHF_SMALL
67 #define ARCH_SHF_SMALL 0
71 * Modules' sections will be aligned on page boundaries
72 * to ensure complete separation of code and data, but
73 * only when CONFIG_DEBUG_SET_MODULE_RONX=y
75 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
76 # define debug_align(X) ALIGN(X, PAGE_SIZE)
78 # define debug_align(X) (X)
82 * Given BASE and SIZE this macro calculates the number of pages the
83 * memory regions occupies
85 #define MOD_NUMBER_OF_PAGES(BASE, SIZE) (((SIZE) > 0) ? \
86 (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
87 PFN_DOWN((unsigned long)BASE) + 1) \
90 /* If this is set, the section belongs in the init part of the module */
91 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
93 #ifdef CONFIG_ENTERPRISE_SUPPORT
94 /* Allow unsupported modules switch. */
95 #ifdef UNSUPPORTED_MODULES
96 int unsupported = UNSUPPORTED_MODULES;
98 int unsupported = 2; /* don't warn when loading unsupported modules. */
101 static int __init unsupported_setup(char *str)
103 get_option(&str, &unsupported);
106 __setup("unsupported=", unsupported_setup);
111 * 1) List of modules (also safely readable with preempt_disable),
112 * 2) module_use links,
113 * 3) module_addr_min/module_addr_max.
114 * (delete uses stop_machine/add uses RCU list operations). */
115 DEFINE_MUTEX(module_mutex);
116 EXPORT_SYMBOL_GPL(module_mutex);
117 static LIST_HEAD(modules);
118 #ifdef CONFIG_KGDB_KDB
119 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
120 #endif /* CONFIG_KGDB_KDB */
123 /* Block module loading/unloading? */
124 int modules_disabled = 0;
125 core_param(nomodule, modules_disabled, bint, 0);
127 /* Waiting for a module to finish initializing? */
128 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
130 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
132 /* Bounds of module allocation, for speeding __module_address.
133 * Protected by module_mutex. */
134 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
136 int register_module_notifier(struct notifier_block * nb)
138 return blocking_notifier_chain_register(&module_notify_list, nb);
140 EXPORT_SYMBOL(register_module_notifier);
142 int unregister_module_notifier(struct notifier_block * nb)
144 return blocking_notifier_chain_unregister(&module_notify_list, nb);
146 EXPORT_SYMBOL(unregister_module_notifier);
152 char *secstrings, *strtab;
153 unsigned long symoffs, stroffs;
154 struct _ddebug *debug;
155 unsigned int num_debug;
157 unsigned int sym, str, mod, vers, info, pcpu, unwind;
161 /* We require a truly strong try_module_get(): 0 means failure due to
162 ongoing or failed initialization etc. */
163 static inline int strong_try_module_get(struct module *mod)
165 if (mod && mod->state == MODULE_STATE_COMING)
167 if (try_module_get(mod))
173 static inline void add_taint_module(struct module *mod, unsigned flag)
176 mod->taints |= (1U << flag);
180 * A thread that wants to hold a reference to a module only while it
181 * is running can call this to safely exit. nfsd and lockd use this.
183 void __module_put_and_exit(struct module *mod, long code)
188 EXPORT_SYMBOL(__module_put_and_exit);
190 /* Find a module section: 0 means not found. */
191 static unsigned int find_sec(const struct load_info *info, const char *name)
195 for (i = 1; i < info->hdr->e_shnum; i++) {
196 Elf_Shdr *shdr = &info->sechdrs[i];
197 /* Alloc bit cleared means "ignore it." */
198 if ((shdr->sh_flags & SHF_ALLOC)
199 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
205 /* Find a module section, or NULL. */
206 static void *section_addr(const struct load_info *info, const char *name)
208 /* Section 0 has sh_addr 0. */
209 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
212 /* Find a module section, or NULL. Fill in number of "objects" in section. */
213 static void *section_objs(const struct load_info *info,
218 unsigned int sec = find_sec(info, name);
220 /* Section 0 has sh_addr 0 and sh_size 0. */
221 *num = info->sechdrs[sec].sh_size / object_size;
222 return (void *)info->sechdrs[sec].sh_addr;
225 /* Provided by the linker */
226 extern const struct kernel_symbol __start___ksymtab[];
227 extern const struct kernel_symbol __stop___ksymtab[];
228 extern const struct kernel_symbol __start___ksymtab_gpl[];
229 extern const struct kernel_symbol __stop___ksymtab_gpl[];
230 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
231 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
232 extern const unsigned long __start___kcrctab[];
233 extern const unsigned long __start___kcrctab_gpl[];
234 extern const unsigned long __start___kcrctab_gpl_future[];
235 #ifdef CONFIG_UNUSED_SYMBOLS
236 extern const struct kernel_symbol __start___ksymtab_unused[];
237 extern const struct kernel_symbol __stop___ksymtab_unused[];
238 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
239 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
240 extern const unsigned long __start___kcrctab_unused[];
241 extern const unsigned long __start___kcrctab_unused_gpl[];
244 #ifndef CONFIG_MODVERSIONS
245 #define symversion(base, idx) NULL
247 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
250 static bool each_symbol_in_section(const struct symsearch *arr,
251 unsigned int arrsize,
252 struct module *owner,
253 bool (*fn)(const struct symsearch *syms,
254 struct module *owner,
260 for (j = 0; j < arrsize; j++) {
261 if (fn(&arr[j], owner, data))
268 /* Returns true as soon as fn returns true, otherwise false. */
269 bool each_symbol_section(bool (*fn)(const struct symsearch *arr,
270 struct module *owner,
275 static const struct symsearch arr[] = {
276 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
277 NOT_GPL_ONLY, false },
278 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
279 __start___kcrctab_gpl,
281 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
282 __start___kcrctab_gpl_future,
283 WILL_BE_GPL_ONLY, false },
284 #ifdef CONFIG_UNUSED_SYMBOLS
285 { __start___ksymtab_unused, __stop___ksymtab_unused,
286 __start___kcrctab_unused,
287 NOT_GPL_ONLY, true },
288 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
289 __start___kcrctab_unused_gpl,
294 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
297 list_for_each_entry_rcu(mod, &modules, list) {
298 struct symsearch arr[] = {
299 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
300 NOT_GPL_ONLY, false },
301 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
304 { mod->gpl_future_syms,
305 mod->gpl_future_syms + mod->num_gpl_future_syms,
306 mod->gpl_future_crcs,
307 WILL_BE_GPL_ONLY, false },
308 #ifdef CONFIG_UNUSED_SYMBOLS
310 mod->unused_syms + mod->num_unused_syms,
312 NOT_GPL_ONLY, true },
313 { mod->unused_gpl_syms,
314 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
315 mod->unused_gpl_crcs,
320 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
325 EXPORT_SYMBOL_GPL(each_symbol_section);
327 struct find_symbol_arg {
334 struct module *owner;
335 const unsigned long *crc;
336 const struct kernel_symbol *sym;
339 static bool check_symbol(const struct symsearch *syms,
340 struct module *owner,
341 unsigned int symnum, void *data)
343 struct find_symbol_arg *fsa = data;
346 if (syms->licence == GPL_ONLY)
348 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
349 printk(KERN_WARNING "Symbol %s is being used "
350 "by a non-GPL module, which will not "
351 "be allowed in the future\n", fsa->name);
352 printk(KERN_WARNING "Please see the file "
353 "Documentation/feature-removal-schedule.txt "
354 "in the kernel source tree for more details.\n");
358 #ifdef CONFIG_UNUSED_SYMBOLS
359 if (syms->unused && fsa->warn) {
360 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
361 "however this module is using it.\n", fsa->name);
363 "This symbol will go away in the future.\n");
365 "Please evalute if this is the right api to use and if "
366 "it really is, submit a report the linux kernel "
367 "mailinglist together with submitting your code for "
373 fsa->crc = symversion(syms->crcs, symnum);
374 fsa->sym = &syms->start[symnum];
378 static int cmp_name(const void *va, const void *vb)
381 const struct kernel_symbol *b;
383 return strcmp(a, b->name);
386 static bool find_symbol_in_section(const struct symsearch *syms,
387 struct module *owner,
390 struct find_symbol_arg *fsa = data;
391 struct kernel_symbol *sym;
393 sym = bsearch(fsa->name, syms->start, syms->stop - syms->start,
394 sizeof(struct kernel_symbol), cmp_name);
396 if (sym != NULL && check_symbol(syms, owner, sym - syms->start, data))
402 /* Find a symbol and return it, along with, (optional) crc and
403 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
404 const struct kernel_symbol *find_symbol(const char *name,
405 struct module **owner,
406 const unsigned long **crc,
410 struct find_symbol_arg fsa;
416 if (each_symbol_section(find_symbol_in_section, &fsa)) {
424 pr_debug("Failed to find symbol %s\n", name);
427 EXPORT_SYMBOL_GPL(find_symbol);
429 /* Search for module by name: must hold module_mutex. */
430 struct module *find_module(const char *name)
434 list_for_each_entry(mod, &modules, list) {
435 if (strcmp(mod->name, name) == 0)
440 EXPORT_SYMBOL_GPL(find_module);
444 static inline void __percpu *mod_percpu(struct module *mod)
449 static int percpu_modalloc(struct module *mod,
450 unsigned long size, unsigned long align)
452 if (align > PAGE_SIZE) {
453 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
454 mod->name, align, PAGE_SIZE);
458 mod->percpu = __alloc_reserved_percpu(size, align);
461 "%s: Could not allocate %lu bytes percpu data\n",
465 mod->percpu_size = size;
469 static void percpu_modfree(struct module *mod)
471 free_percpu(mod->percpu);
474 static unsigned int find_pcpusec(struct load_info *info)
476 return find_sec(info, ".data..percpu");
479 static void percpu_modcopy(struct module *mod,
480 const void *from, unsigned long size)
484 for_each_possible_cpu(cpu)
485 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
489 * is_module_percpu_address - test whether address is from module static percpu
490 * @addr: address to test
492 * Test whether @addr belongs to module static percpu area.
495 * %true if @addr is from module static percpu area
497 bool is_module_percpu_address(unsigned long addr)
504 list_for_each_entry_rcu(mod, &modules, list) {
505 if (!mod->percpu_size)
507 for_each_possible_cpu(cpu) {
508 void *start = per_cpu_ptr(mod->percpu, cpu);
510 if ((void *)addr >= start &&
511 (void *)addr < start + mod->percpu_size) {
522 #else /* ... !CONFIG_SMP */
524 static inline void __percpu *mod_percpu(struct module *mod)
528 static inline int percpu_modalloc(struct module *mod,
529 unsigned long size, unsigned long align)
533 static inline void percpu_modfree(struct module *mod)
536 static unsigned int find_pcpusec(struct load_info *info)
540 static inline void percpu_modcopy(struct module *mod,
541 const void *from, unsigned long size)
543 /* pcpusec should be 0, and size of that section should be 0. */
546 bool is_module_percpu_address(unsigned long addr)
551 #endif /* CONFIG_SMP */
553 static unsigned int find_unwind(struct load_info *info)
556 #ifdef ARCH_UNWIND_SECTION_NAME
557 section = find_sec(info, ARCH_UNWIND_SECTION_NAME);
559 info->sechdrs[section].sh_flags |= SHF_ALLOC;
564 static void add_unwind_table(struct module *mod, struct load_info *info)
566 int index = info->index.unwind;
568 /* Size of section 0 is 0, so this is ok if there is no unwind info. */
569 mod->unwind_info = unwind_add_table(mod,
570 (void *)info->sechdrs[index].sh_addr,
571 info->sechdrs[index].sh_size);
574 #define MODINFO_ATTR(field) \
575 static void setup_modinfo_##field(struct module *mod, const char *s) \
577 mod->field = kstrdup(s, GFP_KERNEL); \
579 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
580 struct module_kobject *mk, char *buffer) \
582 return sprintf(buffer, "%s\n", mk->mod->field); \
584 static int modinfo_##field##_exists(struct module *mod) \
586 return mod->field != NULL; \
588 static void free_modinfo_##field(struct module *mod) \
593 static struct module_attribute modinfo_##field = { \
594 .attr = { .name = __stringify(field), .mode = 0444 }, \
595 .show = show_modinfo_##field, \
596 .setup = setup_modinfo_##field, \
597 .test = modinfo_##field##_exists, \
598 .free = free_modinfo_##field, \
601 MODINFO_ATTR(version);
602 MODINFO_ATTR(srcversion);
604 static char last_unloaded_module[MODULE_NAME_LEN+1];
606 #ifdef CONFIG_MODULE_UNLOAD
608 EXPORT_TRACEPOINT_SYMBOL(module_get);
610 /* Init the unload section of the module. */
611 static int module_unload_init(struct module *mod)
613 mod->refptr = alloc_percpu(struct module_ref);
617 INIT_LIST_HEAD(&mod->source_list);
618 INIT_LIST_HEAD(&mod->target_list);
620 /* Hold reference count during initialization. */
621 __this_cpu_write(mod->refptr->incs, 1);
622 /* Backwards compatibility macros put refcount during init. */
623 mod->waiter = current;
628 /* Does a already use b? */
629 static int already_uses(struct module *a, struct module *b)
631 struct module_use *use;
633 list_for_each_entry(use, &b->source_list, source_list) {
634 if (use->source == a) {
635 pr_debug("%s uses %s!\n", a->name, b->name);
639 pr_debug("%s does not use %s!\n", a->name, b->name);
645 * - we add 'a' as a "source", 'b' as a "target" of module use
646 * - the module_use is added to the list of 'b' sources (so
647 * 'b' can walk the list to see who sourced them), and of 'a'
648 * targets (so 'a' can see what modules it targets).
650 static int add_module_usage(struct module *a, struct module *b)
652 struct module_use *use;
654 pr_debug("Allocating new usage for %s.\n", a->name);
655 use = kmalloc(sizeof(*use), GFP_ATOMIC);
657 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
663 list_add(&use->source_list, &b->source_list);
664 list_add(&use->target_list, &a->target_list);
668 /* Module a uses b: caller needs module_mutex() */
669 int ref_module(struct module *a, struct module *b)
673 if (b == NULL || already_uses(a, b))
676 /* If module isn't available, we fail. */
677 err = strong_try_module_get(b);
681 err = add_module_usage(a, b);
688 EXPORT_SYMBOL_GPL(ref_module);
690 /* Clear the unload stuff of the module. */
691 static void module_unload_free(struct module *mod)
693 struct module_use *use, *tmp;
695 mutex_lock(&module_mutex);
696 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
697 struct module *i = use->target;
698 pr_debug("%s unusing %s\n", mod->name, i->name);
700 list_del(&use->source_list);
701 list_del(&use->target_list);
704 mutex_unlock(&module_mutex);
706 free_percpu(mod->refptr);
709 #ifdef CONFIG_MODULE_FORCE_UNLOAD
710 static inline int try_force_unload(unsigned int flags)
712 int ret = (flags & O_TRUNC);
714 add_taint(TAINT_FORCED_RMMOD);
718 static inline int try_force_unload(unsigned int flags)
722 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
731 /* Whole machine is stopped with interrupts off when this runs. */
732 static int __try_stop_module(void *_sref)
734 struct stopref *sref = _sref;
736 /* If it's not unused, quit unless we're forcing. */
737 if (module_refcount(sref->mod) != 0) {
738 if (!(*sref->forced = try_force_unload(sref->flags)))
742 /* Mark it as dying. */
743 sref->mod->state = MODULE_STATE_GOING;
747 static int try_stop_module(struct module *mod, int flags, int *forced)
749 if (flags & O_NONBLOCK) {
750 struct stopref sref = { mod, flags, forced };
752 return stop_machine(__try_stop_module, &sref, NULL);
754 /* We don't need to stop the machine for this. */
755 mod->state = MODULE_STATE_GOING;
761 unsigned long module_refcount(struct module *mod)
763 unsigned long incs = 0, decs = 0;
766 for_each_possible_cpu(cpu)
767 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
769 * ensure the incs are added up after the decs.
770 * module_put ensures incs are visible before decs with smp_wmb.
772 * This 2-count scheme avoids the situation where the refcount
773 * for CPU0 is read, then CPU0 increments the module refcount,
774 * then CPU1 drops that refcount, then the refcount for CPU1 is
775 * read. We would record a decrement but not its corresponding
776 * increment so we would see a low count (disaster).
778 * Rare situation? But module_refcount can be preempted, and we
779 * might be tallying up 4096+ CPUs. So it is not impossible.
782 for_each_possible_cpu(cpu)
783 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
786 EXPORT_SYMBOL(module_refcount);
788 /* This exists whether we can unload or not */
789 static void free_module(struct module *mod);
791 static void wait_for_zero_refcount(struct module *mod)
793 /* Since we might sleep for some time, release the mutex first */
794 mutex_unlock(&module_mutex);
796 pr_debug("Looking at refcount...\n");
797 set_current_state(TASK_UNINTERRUPTIBLE);
798 if (module_refcount(mod) == 0)
802 current->state = TASK_RUNNING;
803 mutex_lock(&module_mutex);
806 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
810 char name[MODULE_NAME_LEN];
813 if (!capable(CAP_SYS_MODULE) || modules_disabled)
816 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
818 name[MODULE_NAME_LEN-1] = '\0';
820 if (mutex_lock_interruptible(&module_mutex) != 0)
823 mod = find_module(name);
829 if (!list_empty(&mod->source_list)) {
830 /* Other modules depend on us: get rid of them first. */
835 /* Doing init or already dying? */
836 if (mod->state != MODULE_STATE_LIVE) {
837 /* FIXME: if (force), slam module count and wake up
839 pr_debug("%s already dying\n", mod->name);
844 /* If it has an init func, it must have an exit func to unload */
845 if (mod->init && !mod->exit) {
846 forced = try_force_unload(flags);
848 /* This module can't be removed */
854 /* Set this up before setting mod->state */
855 mod->waiter = current;
857 /* Stop the machine so refcounts can't move and disable module. */
858 ret = try_stop_module(mod, flags, &forced);
862 /* Never wait if forced. */
863 if (!forced && module_refcount(mod) != 0)
864 wait_for_zero_refcount(mod);
866 mutex_unlock(&module_mutex);
867 /* Final destruction now no one is using it. */
868 if (mod->exit != NULL)
870 blocking_notifier_call_chain(&module_notify_list,
871 MODULE_STATE_GOING, mod);
872 async_synchronize_full();
874 /* Store the name of the last unloaded module for diagnostic purposes */
875 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
880 mutex_unlock(&module_mutex);
884 static inline void print_unload_info(struct seq_file *m, struct module *mod)
886 struct module_use *use;
887 int printed_something = 0;
889 seq_printf(m, " %lu ", module_refcount(mod));
891 /* Always include a trailing , so userspace can differentiate
892 between this and the old multi-field proc format. */
893 list_for_each_entry(use, &mod->source_list, source_list) {
894 printed_something = 1;
895 seq_printf(m, "%s,", use->source->name);
898 if (mod->init != NULL && mod->exit == NULL) {
899 printed_something = 1;
900 seq_printf(m, "[permanent],");
903 if (!printed_something)
907 void __symbol_put(const char *symbol)
909 struct module *owner;
912 if (!find_symbol(symbol, &owner, NULL, true, false))
917 EXPORT_SYMBOL(__symbol_put);
919 /* Note this assumes addr is a function, which it currently always is. */
920 void symbol_put_addr(void *addr)
922 struct module *modaddr;
923 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
925 if (core_kernel_text(a))
928 /* module_text_address is safe here: we're supposed to have reference
929 * to module from symbol_get, so it can't go away. */
930 modaddr = __module_text_address(a);
934 EXPORT_SYMBOL_GPL(symbol_put_addr);
936 static ssize_t show_refcnt(struct module_attribute *mattr,
937 struct module_kobject *mk, char *buffer)
939 return sprintf(buffer, "%lu\n", module_refcount(mk->mod));
942 static struct module_attribute modinfo_refcnt =
943 __ATTR(refcnt, 0444, show_refcnt, NULL);
945 void __module_get(struct module *module)
949 __this_cpu_inc(module->refptr->incs);
950 trace_module_get(module, _RET_IP_);
954 EXPORT_SYMBOL(__module_get);
956 bool try_module_get(struct module *module)
963 if (likely(module_is_live(module))) {
964 __this_cpu_inc(module->refptr->incs);
965 trace_module_get(module, _RET_IP_);
973 EXPORT_SYMBOL(try_module_get);
975 void module_put(struct module *module)
979 smp_wmb(); /* see comment in module_refcount */
980 __this_cpu_inc(module->refptr->decs);
982 trace_module_put(module, _RET_IP_);
983 /* Maybe they're waiting for us to drop reference? */
984 if (unlikely(!module_is_live(module)))
985 wake_up_process(module->waiter);
989 EXPORT_SYMBOL(module_put);
991 #else /* !CONFIG_MODULE_UNLOAD */
992 static inline void print_unload_info(struct seq_file *m, struct module *mod)
994 /* We don't know the usage count, or what modules are using. */
995 seq_printf(m, " - -");
998 static inline void module_unload_free(struct module *mod)
1002 int ref_module(struct module *a, struct module *b)
1004 return strong_try_module_get(b);
1006 EXPORT_SYMBOL_GPL(ref_module);
1008 static inline int module_unload_init(struct module *mod)
1012 #endif /* CONFIG_MODULE_UNLOAD */
1014 static size_t module_flags_taint(struct module *mod, char *buf)
1018 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
1020 if (mod->taints & (1 << TAINT_OOT_MODULE))
1022 if (mod->taints & (1 << TAINT_FORCED_MODULE))
1024 if (mod->taints & (1 << TAINT_CRAP))
1026 #ifdef CONFIG_ENTERPRISE_SUPPORT
1027 if (mod->taints & (1 << TAINT_NO_SUPPORT))
1029 if (mod->taints & (1 << TAINT_EXTERNAL_SUPPORT))
1033 * TAINT_FORCED_RMMOD: could be added.
1034 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
1040 static ssize_t show_initstate(struct module_attribute *mattr,
1041 struct module_kobject *mk, char *buffer)
1043 const char *state = "unknown";
1045 switch (mk->mod->state) {
1046 case MODULE_STATE_LIVE:
1049 case MODULE_STATE_COMING:
1052 case MODULE_STATE_GOING:
1056 return sprintf(buffer, "%s\n", state);
1059 static struct module_attribute modinfo_initstate =
1060 __ATTR(initstate, 0444, show_initstate, NULL);
1062 static ssize_t store_uevent(struct module_attribute *mattr,
1063 struct module_kobject *mk,
1064 const char *buffer, size_t count)
1066 enum kobject_action action;
1068 if (kobject_action_type(buffer, count, &action) == 0)
1069 kobject_uevent(&mk->kobj, action);
1073 struct module_attribute module_uevent =
1074 __ATTR(uevent, 0200, NULL, store_uevent);
1076 static ssize_t show_coresize(struct module_attribute *mattr,
1077 struct module_kobject *mk, char *buffer)
1079 return sprintf(buffer, "%u\n", mk->mod->core_size);
1082 static struct module_attribute modinfo_coresize =
1083 __ATTR(coresize, 0444, show_coresize, NULL);
1085 static ssize_t show_initsize(struct module_attribute *mattr,
1086 struct module_kobject *mk, char *buffer)
1088 return sprintf(buffer, "%u\n", mk->mod->init_size);
1091 static struct module_attribute modinfo_initsize =
1092 __ATTR(initsize, 0444, show_initsize, NULL);
1094 static ssize_t show_taint(struct module_attribute *mattr,
1095 struct module_kobject *mk, char *buffer)
1099 l = module_flags_taint(mk->mod, buffer);
1104 static struct module_attribute modinfo_taint =
1105 __ATTR(taint, 0444, show_taint, NULL);
1107 #ifdef CONFIG_ENTERPRISE_SUPPORT
1108 static void setup_modinfo_supported(struct module *mod, const char *s)
1111 mod->taints |= (1 << TAINT_NO_SUPPORT);
1115 if (strcmp(s, "external") == 0)
1116 mod->taints |= (1 << TAINT_EXTERNAL_SUPPORT);
1117 else if (strcmp(s, "yes"))
1118 mod->taints |= (1 << TAINT_NO_SUPPORT);
1121 static ssize_t show_modinfo_supported(struct module_attribute *mattr,
1122 struct module_kobject *mk, char *buffer)
1124 return sprintf(buffer, "%s\n", supported_printable(mk->mod->taints));
1127 static struct module_attribute modinfo_supported = {
1128 .attr = { .name = "supported", .mode = 0444 },
1129 .show = show_modinfo_supported,
1130 .setup = setup_modinfo_supported,
1134 static struct module_attribute *modinfo_attrs[] = {
1137 &modinfo_srcversion,
1142 #ifdef CONFIG_ENTERPRISE_SUPPORT
1145 #ifdef CONFIG_MODULE_UNLOAD
1151 static const char vermagic[] = VERMAGIC_STRING;
1153 static int try_to_force_load(struct module *mod, const char *reason)
1155 #ifdef CONFIG_MODULE_FORCE_LOAD
1156 if (!test_taint(TAINT_FORCED_MODULE))
1157 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1159 add_taint_module(mod, TAINT_FORCED_MODULE);
1166 #ifdef CONFIG_MODVERSIONS
1167 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
1168 static unsigned long maybe_relocated(unsigned long crc,
1169 const struct module *crc_owner)
1171 #ifdef ARCH_RELOCATES_KCRCTAB
1172 if (crc_owner == NULL)
1173 return crc - (unsigned long)reloc_start;
1178 static int check_version(Elf_Shdr *sechdrs,
1179 unsigned int versindex,
1180 const char *symname,
1182 const unsigned long *crc,
1183 const struct module *crc_owner)
1185 unsigned int i, num_versions;
1186 struct modversion_info *versions;
1188 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1192 /* No versions at all? modprobe --force does this. */
1194 return try_to_force_load(mod, symname) == 0;
1196 versions = (void *) sechdrs[versindex].sh_addr;
1197 num_versions = sechdrs[versindex].sh_size
1198 / sizeof(struct modversion_info);
1200 for (i = 0; i < num_versions; i++) {
1201 if (strcmp(versions[i].name, symname) != 0)
1204 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
1206 pr_debug("Found checksum %lX vs module %lX\n",
1207 maybe_relocated(*crc, crc_owner), versions[i].crc);
1211 printk(KERN_WARNING "%s: no symbol version for %s\n",
1212 mod->name, symname);
1216 printk("%s: disagrees about version of symbol %s\n",
1217 mod->name, symname);
1221 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1222 unsigned int versindex,
1225 const unsigned long *crc;
1227 /* Since this should be found in kernel (which can't be removed),
1228 * no locking is necessary. */
1229 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1232 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1236 /* First part is kernel version, which we ignore if module has crcs. */
1237 static inline int same_magic(const char *amagic, const char *bmagic,
1241 amagic += strcspn(amagic, " ");
1242 bmagic += strcspn(bmagic, " ");
1244 return strcmp(amagic, bmagic) == 0;
1247 static inline int check_version(Elf_Shdr *sechdrs,
1248 unsigned int versindex,
1249 const char *symname,
1251 const unsigned long *crc,
1252 const struct module *crc_owner)
1257 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1258 unsigned int versindex,
1264 static inline int same_magic(const char *amagic, const char *bmagic,
1267 return strcmp(amagic, bmagic) == 0;
1269 #endif /* CONFIG_MODVERSIONS */
1271 /* Resolve a symbol for this module. I.e. if we find one, record usage. */
1272 static const struct kernel_symbol *resolve_symbol(struct module *mod,
1273 const struct load_info *info,
1277 struct module *owner;
1278 const struct kernel_symbol *sym;
1279 const unsigned long *crc;
1282 mutex_lock(&module_mutex);
1283 sym = find_symbol(name, &owner, &crc,
1284 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1288 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1290 sym = ERR_PTR(-EINVAL);
1294 err = ref_module(mod, owner);
1301 /* We must make copy under the lock if we failed to get ref. */
1302 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1304 mutex_unlock(&module_mutex);
1308 static const struct kernel_symbol *
1309 resolve_symbol_wait(struct module *mod,
1310 const struct load_info *info,
1313 const struct kernel_symbol *ksym;
1314 char owner[MODULE_NAME_LEN];
1316 if (wait_event_interruptible_timeout(module_wq,
1317 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1318 || PTR_ERR(ksym) != -EBUSY,
1320 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1327 * /sys/module/foo/sections stuff
1328 * J. Corbet <corbet@lwn.net>
1332 #ifdef CONFIG_KALLSYMS
1333 static inline bool sect_empty(const Elf_Shdr *sect)
1335 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1338 struct module_sect_attr
1340 struct module_attribute mattr;
1342 unsigned long address;
1345 struct module_sect_attrs
1347 struct attribute_group grp;
1348 unsigned int nsections;
1349 struct module_sect_attr attrs[0];
1352 static ssize_t module_sect_show(struct module_attribute *mattr,
1353 struct module_kobject *mk, char *buf)
1355 struct module_sect_attr *sattr =
1356 container_of(mattr, struct module_sect_attr, mattr);
1357 return sprintf(buf, "0x%pK\n", (void *)sattr->address);
1360 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1362 unsigned int section;
1364 for (section = 0; section < sect_attrs->nsections; section++)
1365 kfree(sect_attrs->attrs[section].name);
1369 static void add_sect_attrs(struct module *mod, const struct load_info *info)
1371 unsigned int nloaded = 0, i, size[2];
1372 struct module_sect_attrs *sect_attrs;
1373 struct module_sect_attr *sattr;
1374 struct attribute **gattr;
1376 /* Count loaded sections and allocate structures */
1377 for (i = 0; i < info->hdr->e_shnum; i++)
1378 if (!sect_empty(&info->sechdrs[i]))
1380 size[0] = ALIGN(sizeof(*sect_attrs)
1381 + nloaded * sizeof(sect_attrs->attrs[0]),
1382 sizeof(sect_attrs->grp.attrs[0]));
1383 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1384 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1385 if (sect_attrs == NULL)
1388 /* Setup section attributes. */
1389 sect_attrs->grp.name = "sections";
1390 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1392 sect_attrs->nsections = 0;
1393 sattr = §_attrs->attrs[0];
1394 gattr = §_attrs->grp.attrs[0];
1395 for (i = 0; i < info->hdr->e_shnum; i++) {
1396 Elf_Shdr *sec = &info->sechdrs[i];
1397 if (sect_empty(sec))
1399 sattr->address = sec->sh_addr;
1400 sattr->name = kstrdup(info->secstrings + sec->sh_name,
1402 if (sattr->name == NULL)
1404 sect_attrs->nsections++;
1405 sysfs_attr_init(&sattr->mattr.attr);
1406 sattr->mattr.show = module_sect_show;
1407 sattr->mattr.store = NULL;
1408 sattr->mattr.attr.name = sattr->name;
1409 sattr->mattr.attr.mode = S_IRUGO;
1410 *(gattr++) = &(sattr++)->mattr.attr;
1414 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1417 mod->sect_attrs = sect_attrs;
1420 free_sect_attrs(sect_attrs);
1423 static void remove_sect_attrs(struct module *mod)
1425 if (mod->sect_attrs) {
1426 sysfs_remove_group(&mod->mkobj.kobj,
1427 &mod->sect_attrs->grp);
1428 /* We are positive that no one is using any sect attrs
1429 * at this point. Deallocate immediately. */
1430 free_sect_attrs(mod->sect_attrs);
1431 mod->sect_attrs = NULL;
1436 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1439 struct module_notes_attrs {
1440 struct kobject *dir;
1442 struct bin_attribute attrs[0];
1445 static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1446 struct bin_attribute *bin_attr,
1447 char *buf, loff_t pos, size_t count)
1450 * The caller checked the pos and count against our size.
1452 memcpy(buf, bin_attr->private + pos, count);
1456 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1459 if (notes_attrs->dir) {
1461 sysfs_remove_bin_file(notes_attrs->dir,
1462 ¬es_attrs->attrs[i]);
1463 kobject_put(notes_attrs->dir);
1468 static void add_notes_attrs(struct module *mod, const struct load_info *info)
1470 unsigned int notes, loaded, i;
1471 struct module_notes_attrs *notes_attrs;
1472 struct bin_attribute *nattr;
1474 /* failed to create section attributes, so can't create notes */
1475 if (!mod->sect_attrs)
1478 /* Count notes sections and allocate structures. */
1480 for (i = 0; i < info->hdr->e_shnum; i++)
1481 if (!sect_empty(&info->sechdrs[i]) &&
1482 (info->sechdrs[i].sh_type == SHT_NOTE))
1488 notes_attrs = kzalloc(sizeof(*notes_attrs)
1489 + notes * sizeof(notes_attrs->attrs[0]),
1491 if (notes_attrs == NULL)
1494 notes_attrs->notes = notes;
1495 nattr = ¬es_attrs->attrs[0];
1496 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1497 if (sect_empty(&info->sechdrs[i]))
1499 if (info->sechdrs[i].sh_type == SHT_NOTE) {
1500 sysfs_bin_attr_init(nattr);
1501 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1502 nattr->attr.mode = S_IRUGO;
1503 nattr->size = info->sechdrs[i].sh_size;
1504 nattr->private = (void *) info->sechdrs[i].sh_addr;
1505 nattr->read = module_notes_read;
1511 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1512 if (!notes_attrs->dir)
1515 for (i = 0; i < notes; ++i)
1516 if (sysfs_create_bin_file(notes_attrs->dir,
1517 ¬es_attrs->attrs[i]))
1520 mod->notes_attrs = notes_attrs;
1524 free_notes_attrs(notes_attrs, i);
1527 static void remove_notes_attrs(struct module *mod)
1529 if (mod->notes_attrs)
1530 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1535 static inline void add_sect_attrs(struct module *mod,
1536 const struct load_info *info)
1540 static inline void remove_sect_attrs(struct module *mod)
1544 static inline void add_notes_attrs(struct module *mod,
1545 const struct load_info *info)
1549 static inline void remove_notes_attrs(struct module *mod)
1552 #endif /* CONFIG_KALLSYMS */
1554 static void add_usage_links(struct module *mod)
1556 #ifdef CONFIG_MODULE_UNLOAD
1557 struct module_use *use;
1560 mutex_lock(&module_mutex);
1561 list_for_each_entry(use, &mod->target_list, target_list) {
1562 nowarn = sysfs_create_link(use->target->holders_dir,
1563 &mod->mkobj.kobj, mod->name);
1565 mutex_unlock(&module_mutex);
1569 static void del_usage_links(struct module *mod)
1571 #ifdef CONFIG_MODULE_UNLOAD
1572 struct module_use *use;
1574 mutex_lock(&module_mutex);
1575 list_for_each_entry(use, &mod->target_list, target_list)
1576 sysfs_remove_link(use->target->holders_dir, mod->name);
1577 mutex_unlock(&module_mutex);
1581 static int module_add_modinfo_attrs(struct module *mod)
1583 struct module_attribute *attr;
1584 struct module_attribute *temp_attr;
1588 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1589 (ARRAY_SIZE(modinfo_attrs) + 1)),
1591 if (!mod->modinfo_attrs)
1594 temp_attr = mod->modinfo_attrs;
1595 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1597 (attr->test && attr->test(mod))) {
1598 memcpy(temp_attr, attr, sizeof(*temp_attr));
1599 sysfs_attr_init(&temp_attr->attr);
1600 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1607 static void module_remove_modinfo_attrs(struct module *mod)
1609 struct module_attribute *attr;
1612 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1613 /* pick a field to test for end of list */
1614 if (!attr->attr.name)
1616 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1620 kfree(mod->modinfo_attrs);
1623 static int mod_sysfs_init(struct module *mod)
1626 struct kobject *kobj;
1628 if (!module_sysfs_initialized) {
1629 printk(KERN_ERR "%s: module sysfs not initialized\n",
1635 kobj = kset_find_obj(module_kset, mod->name);
1637 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1643 mod->mkobj.mod = mod;
1645 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1646 mod->mkobj.kobj.kset = module_kset;
1647 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1650 kobject_put(&mod->mkobj.kobj);
1652 /* delay uevent until full sysfs population */
1657 static int mod_sysfs_setup(struct module *mod,
1658 const struct load_info *info,
1659 struct kernel_param *kparam,
1660 unsigned int num_params)
1664 err = mod_sysfs_init(mod);
1668 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1669 if (!mod->holders_dir) {
1674 err = module_param_sysfs_setup(mod, kparam, num_params);
1676 goto out_unreg_holders;
1678 err = module_add_modinfo_attrs(mod);
1680 goto out_unreg_param;
1682 add_usage_links(mod);
1683 add_sect_attrs(mod, info);
1684 add_notes_attrs(mod, info);
1686 #ifdef CONFIG_ENTERPRISE_SUPPORT
1687 /* We don't use add_taint() here because it also disables lockdep. */
1688 if (mod->taints & (1 << TAINT_EXTERNAL_SUPPORT))
1689 add_nonfatal_taint(TAINT_EXTERNAL_SUPPORT);
1690 else if (mod->taints == (1 << TAINT_NO_SUPPORT)) {
1691 if (unsupported == 0) {
1692 printk(KERN_WARNING "%s: module not supported by "
1693 "Novell, refusing to load. To override, echo "
1694 "1 > /proc/sys/kernel/unsupported\n", mod->name);
1696 goto out_remove_attrs;
1698 add_nonfatal_taint(TAINT_NO_SUPPORT);
1699 if (unsupported == 1) {
1700 printk(KERN_WARNING "%s: module is not supported by "
1701 "Novell. Novell Technical Services may decline "
1702 "your support request if it involves a kernel "
1703 "fault.\n", mod->name);
1708 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1712 remove_notes_attrs(mod);
1713 remove_sect_attrs(mod);
1714 del_usage_links(mod);
1715 module_remove_modinfo_attrs(mod);
1717 module_param_sysfs_remove(mod);
1719 kobject_put(mod->holders_dir);
1721 kobject_put(&mod->mkobj.kobj);
1726 static void mod_sysfs_fini(struct module *mod)
1728 remove_notes_attrs(mod);
1729 remove_sect_attrs(mod);
1730 kobject_put(&mod->mkobj.kobj);
1733 #else /* !CONFIG_SYSFS */
1735 static int mod_sysfs_setup(struct module *mod,
1736 const struct load_info *info,
1737 struct kernel_param *kparam,
1738 unsigned int num_params)
1743 static void mod_sysfs_fini(struct module *mod)
1747 static void module_remove_modinfo_attrs(struct module *mod)
1751 static void del_usage_links(struct module *mod)
1755 #endif /* CONFIG_SYSFS */
1757 static void mod_sysfs_teardown(struct module *mod)
1759 del_usage_links(mod);
1760 module_remove_modinfo_attrs(mod);
1761 module_param_sysfs_remove(mod);
1762 kobject_put(mod->mkobj.drivers_dir);
1763 kobject_put(mod->holders_dir);
1764 mod_sysfs_fini(mod);
1768 * unlink the module with the whole machine is stopped with interrupts off
1769 * - this defends against kallsyms not taking locks
1771 static int __unlink_module(void *_mod)
1773 struct module *mod = _mod;
1774 list_del(&mod->list);
1775 module_bug_cleanup(mod);
1779 #ifdef CONFIG_DEBUG_SET_MODULE_RONX
1781 * LKM RO/NX protection: protect module's text/ro-data
1782 * from modification and any data from execution.
1784 void set_page_attributes(void *start, void *end, int (*set)(unsigned long start, int num_pages))
1786 unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
1787 unsigned long end_pfn = PFN_DOWN((unsigned long)end);
1789 if (end_pfn > begin_pfn)
1790 set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1793 static void set_section_ro_nx(void *base,
1794 unsigned long text_size,
1795 unsigned long ro_size,
1796 unsigned long total_size)
1798 /* begin and end PFNs of the current subsection */
1799 unsigned long begin_pfn;
1800 unsigned long end_pfn;
1803 * Set RO for module text and RO-data:
1804 * - Always protect first page.
1805 * - Do not protect last partial page.
1808 set_page_attributes(base, base + ro_size, set_memory_ro);
1811 * Set NX permissions for module data:
1812 * - Do not protect first partial page.
1813 * - Always protect last page.
1815 if (total_size > text_size) {
1816 begin_pfn = PFN_UP((unsigned long)base + text_size);
1817 end_pfn = PFN_UP((unsigned long)base + total_size);
1818 if (end_pfn > begin_pfn)
1819 set_memory_nx(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
1823 static void unset_module_core_ro_nx(struct module *mod)
1825 set_page_attributes(mod->module_core + mod->core_text_size,
1826 mod->module_core + mod->core_size,
1828 set_page_attributes(mod->module_core,
1829 mod->module_core + mod->core_ro_size,
1833 static void unset_module_init_ro_nx(struct module *mod)
1835 set_page_attributes(mod->module_init + mod->init_text_size,
1836 mod->module_init + mod->init_size,
1838 set_page_attributes(mod->module_init,
1839 mod->module_init + mod->init_ro_size,
1843 /* Iterate through all modules and set each module's text as RW */
1844 void set_all_modules_text_rw(void)
1848 mutex_lock(&module_mutex);
1849 list_for_each_entry_rcu(mod, &modules, list) {
1850 if ((mod->module_core) && (mod->core_text_size)) {
1851 set_page_attributes(mod->module_core,
1852 mod->module_core + mod->core_text_size,
1855 if ((mod->module_init) && (mod->init_text_size)) {
1856 set_page_attributes(mod->module_init,
1857 mod->module_init + mod->init_text_size,
1861 mutex_unlock(&module_mutex);
1864 /* Iterate through all modules and set each module's text as RO */
1865 void set_all_modules_text_ro(void)
1869 mutex_lock(&module_mutex);
1870 list_for_each_entry_rcu(mod, &modules, list) {
1871 if ((mod->module_core) && (mod->core_text_size)) {
1872 set_page_attributes(mod->module_core,
1873 mod->module_core + mod->core_text_size,
1876 if ((mod->module_init) && (mod->init_text_size)) {
1877 set_page_attributes(mod->module_init,
1878 mod->module_init + mod->init_text_size,
1882 mutex_unlock(&module_mutex);
1885 static inline void set_section_ro_nx(void *base, unsigned long text_size, unsigned long ro_size, unsigned long total_size) { }
1886 static void unset_module_core_ro_nx(struct module *mod) { }
1887 static void unset_module_init_ro_nx(struct module *mod) { }
1890 void __weak module_free(struct module *mod, void *module_region)
1892 vfree(module_region);
1895 void __weak module_arch_cleanup(struct module *mod)
1899 /* Free a module, remove from lists, etc. */
1900 static void free_module(struct module *mod)
1902 trace_module_free(mod);
1904 /* Delete from various lists */
1905 mutex_lock(&module_mutex);
1906 stop_machine(__unlink_module, mod, NULL);
1907 mutex_unlock(&module_mutex);
1908 mod_sysfs_teardown(mod);
1910 /* Remove dynamic debug info */
1911 ddebug_remove_module(mod->name);
1913 unwind_remove_table(mod->unwind_info, 0);
1915 /* Arch-specific cleanup. */
1916 module_arch_cleanup(mod);
1918 /* Module unload stuff */
1919 module_unload_free(mod);
1921 /* Free any allocated parameters. */
1922 destroy_params(mod->kp, mod->num_kp);
1924 /* This may be NULL, but that's OK */
1925 unset_module_init_ro_nx(mod);
1926 module_free(mod, mod->module_init);
1928 percpu_modfree(mod);
1930 /* Free lock-classes: */
1931 lockdep_free_key_range(mod->module_core, mod->core_size);
1933 /* Finally, free the core (containing the module structure) */
1934 unset_module_core_ro_nx(mod);
1935 module_free(mod, mod->module_core);
1938 update_protections(current->mm);
1942 void *__symbol_get(const char *symbol)
1944 struct module *owner;
1945 const struct kernel_symbol *sym;
1948 sym = find_symbol(symbol, &owner, NULL, true, true);
1949 if (sym && strong_try_module_get(owner))
1953 return sym ? (void *)sym->value : NULL;
1955 EXPORT_SYMBOL_GPL(__symbol_get);
1958 * Ensure that an exported symbol [global namespace] does not already exist
1959 * in the kernel or in some other module's exported symbol table.
1961 * You must hold the module_mutex.
1963 static int verify_export_symbols(struct module *mod)
1966 struct module *owner;
1967 const struct kernel_symbol *s;
1969 const struct kernel_symbol *sym;
1972 { mod->syms, mod->num_syms },
1973 { mod->gpl_syms, mod->num_gpl_syms },
1974 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1975 #ifdef CONFIG_UNUSED_SYMBOLS
1976 { mod->unused_syms, mod->num_unused_syms },
1977 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1981 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1982 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1983 if (find_symbol(s->name, &owner, NULL, true, false)) {
1985 "%s: exports duplicate symbol %s"
1987 mod->name, s->name, module_name(owner));
1995 /* Change all symbols so that st_value encodes the pointer directly. */
1996 static int simplify_symbols(struct module *mod, const struct load_info *info)
1998 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1999 Elf_Sym *sym = (void *)symsec->sh_addr;
2000 unsigned long secbase;
2003 const struct kernel_symbol *ksym;
2005 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
2006 const char *name = info->strtab + sym[i].st_name;
2008 switch (sym[i].st_shndx) {
2010 /* We compiled with -fno-common. These are not
2011 supposed to happen. */
2012 pr_debug("Common symbol: %s\n", name);
2013 printk("%s: please compile with -fno-common\n",
2019 /* Don't need to do anything */
2020 pr_debug("Absolute symbol: 0x%08lx\n",
2021 (long)sym[i].st_value);
2025 ksym = resolve_symbol_wait(mod, info, name);
2026 /* Ok if resolved. */
2027 if (ksym && !IS_ERR(ksym)) {
2028 sym[i].st_value = ksym->value;
2033 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
2036 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
2037 mod->name, name, PTR_ERR(ksym));
2038 ret = PTR_ERR(ksym) ?: -ENOENT;
2042 /* Divert to percpu allocation if a percpu var. */
2043 if (sym[i].st_shndx == info->index.pcpu)
2044 secbase = (unsigned long)mod_percpu(mod);
2046 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
2047 sym[i].st_value += secbase;
2055 int __weak apply_relocate(Elf_Shdr *sechdrs,
2057 unsigned int symindex,
2058 unsigned int relsec,
2061 pr_err("module %s: REL relocation unsupported\n", me->name);
2065 int __weak apply_relocate_add(Elf_Shdr *sechdrs,
2067 unsigned int symindex,
2068 unsigned int relsec,
2071 pr_err("module %s: RELA relocation unsupported\n", me->name);
2075 static int apply_relocations(struct module *mod, const struct load_info *info)
2080 /* Now do relocations. */
2081 for (i = 1; i < info->hdr->e_shnum; i++) {
2082 unsigned int infosec = info->sechdrs[i].sh_info;
2084 /* Not a valid relocation section? */
2085 if (infosec >= info->hdr->e_shnum)
2088 /* Don't bother with non-allocated sections */
2089 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
2092 if (info->sechdrs[i].sh_type == SHT_REL)
2093 err = apply_relocate(info->sechdrs, info->strtab,
2094 info->index.sym, i, mod);
2095 else if (info->sechdrs[i].sh_type == SHT_RELA)
2096 err = apply_relocate_add(info->sechdrs, info->strtab,
2097 info->index.sym, i, mod);
2104 /* Additional bytes needed by arch in front of individual sections */
2105 unsigned int __weak arch_mod_section_prepend(struct module *mod,
2106 unsigned int section)
2108 /* default implementation just returns zero */
2112 /* Update size with this section: return offset. */
2113 static long get_offset(struct module *mod, unsigned int *size,
2114 Elf_Shdr *sechdr, unsigned int section)
2118 *size += arch_mod_section_prepend(mod, section);
2119 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
2120 *size = ret + sechdr->sh_size;
2124 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
2125 might -- code, read-only data, read-write data, small data. Tally
2126 sizes, and place the offsets into sh_entsize fields: high bit means it
2128 static void layout_sections(struct module *mod, struct load_info *info)
2130 static unsigned long const masks[][2] = {
2131 /* NOTE: all executable code must be the first section
2132 * in this array; otherwise modify the text_size
2133 * finder in the two loops below */
2134 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
2135 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
2136 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
2137 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
2141 for (i = 0; i < info->hdr->e_shnum; i++)
2142 info->sechdrs[i].sh_entsize = ~0UL;
2144 pr_debug("Core section allocation order:\n");
2145 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2146 for (i = 0; i < info->hdr->e_shnum; ++i) {
2147 Elf_Shdr *s = &info->sechdrs[i];
2148 const char *sname = info->secstrings + s->sh_name;
2150 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2151 || (s->sh_flags & masks[m][1])
2152 || s->sh_entsize != ~0UL
2153 || strstarts(sname, ".init"))
2155 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
2156 pr_debug("\t%s\n", sname);
2159 case 0: /* executable */
2160 mod->core_size = debug_align(mod->core_size);
2161 mod->core_text_size = mod->core_size;
2163 case 1: /* RO: text and ro-data */
2164 mod->core_size = debug_align(mod->core_size);
2165 mod->core_ro_size = mod->core_size;
2167 case 3: /* whole core */
2168 mod->core_size = debug_align(mod->core_size);
2173 pr_debug("Init section allocation order:\n");
2174 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
2175 for (i = 0; i < info->hdr->e_shnum; ++i) {
2176 Elf_Shdr *s = &info->sechdrs[i];
2177 const char *sname = info->secstrings + s->sh_name;
2179 if ((s->sh_flags & masks[m][0]) != masks[m][0]
2180 || (s->sh_flags & masks[m][1])
2181 || s->sh_entsize != ~0UL
2182 || !strstarts(sname, ".init"))
2184 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
2185 | INIT_OFFSET_MASK);
2186 pr_debug("\t%s\n", sname);
2189 case 0: /* executable */
2190 mod->init_size = debug_align(mod->init_size);
2191 mod->init_text_size = mod->init_size;
2193 case 1: /* RO: text and ro-data */
2194 mod->init_size = debug_align(mod->init_size);
2195 mod->init_ro_size = mod->init_size;
2197 case 3: /* whole init */
2198 mod->init_size = debug_align(mod->init_size);
2204 static void set_license(struct module *mod, const char *license)
2207 license = "unspecified";
2209 if (!license_is_gpl_compatible(license)) {
2210 if (!test_taint(TAINT_PROPRIETARY_MODULE))
2211 printk(KERN_WARNING "%s: module license '%s' taints "
2212 "kernel.\n", mod->name, license);
2213 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2217 /* Parse tag=value strings from .modinfo section */
2218 static char *next_string(char *string, unsigned long *secsize)
2220 /* Skip non-zero chars */
2223 if ((*secsize)-- <= 1)
2227 /* Skip any zero padding. */
2228 while (!string[0]) {
2230 if ((*secsize)-- <= 1)
2236 static char *get_modinfo(struct load_info *info, const char *tag)
2239 unsigned int taglen = strlen(tag);
2240 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
2241 unsigned long size = infosec->sh_size;
2243 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
2244 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
2245 return p + taglen + 1;
2250 static void setup_modinfo(struct module *mod, struct load_info *info)
2252 struct module_attribute *attr;
2255 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2257 attr->setup(mod, get_modinfo(info, attr->attr.name));
2261 static void free_modinfo(struct module *mod)
2263 struct module_attribute *attr;
2266 for (i = 0; (attr = modinfo_attrs[i]); i++) {
2272 #ifdef CONFIG_KALLSYMS
2274 /* lookup symbol in given range of kernel_symbols */
2275 static const struct kernel_symbol *lookup_symbol(const char *name,
2276 const struct kernel_symbol *start,
2277 const struct kernel_symbol *stop)
2279 return bsearch(name, start, stop - start,
2280 sizeof(struct kernel_symbol), cmp_name);
2283 static int is_exported(const char *name, unsigned long value,
2284 const struct module *mod)
2286 const struct kernel_symbol *ks;
2288 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
2290 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
2291 return ks != NULL && ks->value == value;
2295 static char elf_type(const Elf_Sym *sym, const struct load_info *info)
2297 const Elf_Shdr *sechdrs = info->sechdrs;
2299 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
2300 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
2305 if (sym->st_shndx == SHN_UNDEF)
2307 if (sym->st_shndx == SHN_ABS)
2309 if (sym->st_shndx >= SHN_LORESERVE)
2311 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
2313 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
2314 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
2315 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
2317 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2322 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
2323 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
2328 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
2335 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
2338 const Elf_Shdr *sec;
2340 if (src->st_shndx == SHN_UNDEF
2341 || src->st_shndx >= shnum
2345 sec = sechdrs + src->st_shndx;
2346 if (!(sec->sh_flags & SHF_ALLOC)
2347 #ifndef CONFIG_KALLSYMS_ALL
2348 || !(sec->sh_flags & SHF_EXECINSTR)
2350 || (sec->sh_entsize & INIT_OFFSET_MASK))
2357 * We only allocate and copy the strings needed by the parts of symtab
2358 * we keep. This is simple, but has the effect of making multiple
2359 * copies of duplicates. We could be more sophisticated, see
2360 * linux-kernel thread starting with
2361 * <73defb5e4bca04a6431392cc341112b1@localhost>.
2363 static void layout_symtab(struct module *mod, struct load_info *info)
2365 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
2366 Elf_Shdr *strsect = info->sechdrs + info->index.str;
2368 unsigned int i, nsrc, ndst, strtab_size;
2370 /* Put symbol section at end of init part of module. */
2371 symsect->sh_flags |= SHF_ALLOC;
2372 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
2373 info->index.sym) | INIT_OFFSET_MASK;
2374 pr_debug("\t%s\n", info->secstrings + symsect->sh_name);
2376 src = (void *)info->hdr + symsect->sh_offset;
2377 nsrc = symsect->sh_size / sizeof(*src);
2379 /* Compute total space required for the core symbols' strtab. */
2380 for (ndst = i = strtab_size = 1; i < nsrc; ++i, ++src)
2381 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
2382 strtab_size += strlen(&info->strtab[src->st_name]) + 1;
2386 /* Append room for core symbols at end of core part. */
2387 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
2388 info->stroffs = mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
2389 mod->core_size += strtab_size;
2391 /* Put string table section at end of init part of module. */
2392 strsect->sh_flags |= SHF_ALLOC;
2393 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
2394 info->index.str) | INIT_OFFSET_MASK;
2395 pr_debug("\t%s\n", info->secstrings + strsect->sh_name);
2398 static void add_kallsyms(struct module *mod, const struct load_info *info)
2400 unsigned int i, ndst;
2404 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2406 mod->symtab = (void *)symsec->sh_addr;
2407 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
2408 /* Make sure we get permanent strtab: don't use info->strtab. */
2409 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
2411 /* Set types up while we still have access to sections. */
2412 for (i = 0; i < mod->num_symtab; i++)
2413 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
2415 mod->core_symtab = dst = mod->module_core + info->symoffs;
2416 mod->core_strtab = s = mod->module_core + info->stroffs;
2420 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
2421 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
2425 dst[ndst++].st_name = s - mod->core_strtab;
2426 s += strlcpy(s, &mod->strtab[src->st_name], KSYM_NAME_LEN) + 1;
2428 mod->core_num_syms = ndst;
2431 static inline void layout_symtab(struct module *mod, struct load_info *info)
2435 static void add_kallsyms(struct module *mod, const struct load_info *info)
2438 #endif /* CONFIG_KALLSYMS */
2440 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
2444 #ifdef CONFIG_DYNAMIC_DEBUG
2445 if (ddebug_add_module(debug, num, debug->modname))
2446 printk(KERN_ERR "dynamic debug error adding module: %s\n",
2451 static void dynamic_debug_remove(struct _ddebug *debug)
2454 ddebug_remove_module(debug->modname);
2457 void * __weak module_alloc(unsigned long size)
2459 return size == 0 ? NULL : vmalloc_exec(size);
2462 static void *module_alloc_update_bounds(unsigned long size)
2464 void *ret = module_alloc(size);
2467 mutex_lock(&module_mutex);
2468 /* Update module bounds. */
2469 if ((unsigned long)ret < module_addr_min)
2470 module_addr_min = (unsigned long)ret;
2471 if ((unsigned long)ret + size > module_addr_max)
2472 module_addr_max = (unsigned long)ret + size;
2473 mutex_unlock(&module_mutex);
2478 #ifdef CONFIG_DEBUG_KMEMLEAK
2479 static void kmemleak_load_module(const struct module *mod,
2480 const struct load_info *info)
2484 /* only scan the sections containing data */
2485 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2487 for (i = 1; i < info->hdr->e_shnum; i++) {
2488 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2489 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
2491 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
2494 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2495 info->sechdrs[i].sh_size, GFP_KERNEL);
2499 static inline void kmemleak_load_module(const struct module *mod,
2500 const struct load_info *info)
2505 /* Sets info->hdr and info->len. */
2506 static int copy_and_check(struct load_info *info,
2507 const void __user *umod, unsigned long len,
2508 const char __user *uargs)
2513 if (len < sizeof(*hdr))
2516 /* Suck in entire file: we'll want most of it. */
2517 if ((hdr = vmalloc(len)) == NULL)
2520 if (copy_from_user(hdr, umod, len) != 0) {
2525 /* Sanity checks against insmoding binaries or wrong arch,
2526 weird elf version */
2527 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2528 || hdr->e_type != ET_REL
2529 || !elf_check_arch(hdr)
2530 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2535 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2549 static void free_copy(struct load_info *info)
2554 static int rewrite_section_headers(struct load_info *info)
2558 /* This should always be true, but let's be sure. */
2559 info->sechdrs[0].sh_addr = 0;
2561 for (i = 1; i < info->hdr->e_shnum; i++) {
2562 Elf_Shdr *shdr = &info->sechdrs[i];
2563 if (shdr->sh_type != SHT_NOBITS
2564 && info->len < shdr->sh_offset + shdr->sh_size) {
2565 printk(KERN_ERR "Module len %lu truncated\n",
2570 /* Mark all sections sh_addr with their address in the
2572 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2574 #ifndef CONFIG_MODULE_UNLOAD
2575 /* Don't load .exit sections */
2576 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2577 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2581 /* Track but don't keep modinfo and version sections. */
2582 info->index.vers = find_sec(info, "__versions");
2583 info->index.info = find_sec(info, ".modinfo");
2584 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2585 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
2590 * Set up our basic convenience variables (pointers to section headers,
2591 * search for module section index etc), and do some basic section
2594 * Return the temporary module pointer (we'll replace it with the final
2595 * one when we move the module sections around).
2597 static struct module *setup_load_info(struct load_info *info)
2603 /* Set up the convenience variables */
2604 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
2605 info->secstrings = (void *)info->hdr
2606 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
2608 err = rewrite_section_headers(info);
2610 return ERR_PTR(err);
2612 /* Find internal symbols and strings. */
2613 for (i = 1; i < info->hdr->e_shnum; i++) {
2614 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2615 info->index.sym = i;
2616 info->index.str = info->sechdrs[i].sh_link;
2617 info->strtab = (char *)info->hdr
2618 + info->sechdrs[info->index.str].sh_offset;
2623 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
2624 if (!info->index.mod) {
2625 printk(KERN_WARNING "No module found in object\n");
2626 return ERR_PTR(-ENOEXEC);
2628 /* This is temporary: point mod into copy of data. */
2629 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2631 if (info->index.sym == 0) {
2632 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2634 return ERR_PTR(-ENOEXEC);
2637 info->index.pcpu = find_pcpusec(info);
2639 info->index.unwind = find_unwind(info);
2641 /* Check module struct version now, before we try to use module. */
2642 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2643 return ERR_PTR(-ENOEXEC);
2648 static int check_modinfo(struct module *mod, struct load_info *info)
2650 const char *modmagic = get_modinfo(info, "vermagic");
2653 /* This is allowed: modprobe --force will invalidate it. */
2655 err = try_to_force_load(mod, "bad vermagic");
2658 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
2659 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2660 mod->name, modmagic, vermagic);
2664 if (!get_modinfo(info, "intree"))
2665 add_taint_module(mod, TAINT_OOT_MODULE);
2667 if (get_modinfo(info, "staging")) {
2668 add_taint_module(mod, TAINT_CRAP);
2669 printk(KERN_WARNING "%s: module is from the staging directory,"
2670 " the quality is unknown, you have been warned.\n",
2674 /* Set up license info based on the info section */
2675 set_license(mod, get_modinfo(info, "license"));
2680 static void find_module_sections(struct module *mod, struct load_info *info)
2682 mod->kp = section_objs(info, "__param",
2683 sizeof(*mod->kp), &mod->num_kp);
2684 mod->syms = section_objs(info, "__ksymtab",
2685 sizeof(*mod->syms), &mod->num_syms);
2686 mod->crcs = section_addr(info, "__kcrctab");
2687 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
2688 sizeof(*mod->gpl_syms),
2689 &mod->num_gpl_syms);
2690 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2691 mod->gpl_future_syms = section_objs(info,
2692 "__ksymtab_gpl_future",
2693 sizeof(*mod->gpl_future_syms),
2694 &mod->num_gpl_future_syms);
2695 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
2697 #ifdef CONFIG_UNUSED_SYMBOLS
2698 mod->unused_syms = section_objs(info, "__ksymtab_unused",
2699 sizeof(*mod->unused_syms),
2700 &mod->num_unused_syms);
2701 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2702 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
2703 sizeof(*mod->unused_gpl_syms),
2704 &mod->num_unused_gpl_syms);
2705 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
2707 #ifdef CONFIG_CONSTRUCTORS
2708 mod->ctors = section_objs(info, ".ctors",
2709 sizeof(*mod->ctors), &mod->num_ctors);
2712 #ifdef CONFIG_TRACEPOINTS
2713 mod->tracepoints_ptrs = section_objs(info, "__tracepoints_ptrs",
2714 sizeof(*mod->tracepoints_ptrs),
2715 &mod->num_tracepoints);
2717 #ifdef HAVE_JUMP_LABEL
2718 mod->jump_entries = section_objs(info, "__jump_table",
2719 sizeof(*mod->jump_entries),
2720 &mod->num_jump_entries);
2722 #ifdef CONFIG_EVENT_TRACING
2723 mod->trace_events = section_objs(info, "_ftrace_events",
2724 sizeof(*mod->trace_events),
2725 &mod->num_trace_events);
2727 * This section contains pointers to allocated objects in the trace
2728 * code and not scanning it leads to false positives.
2730 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2731 mod->num_trace_events, GFP_KERNEL);
2733 #ifdef CONFIG_TRACING
2734 mod->trace_bprintk_fmt_start = section_objs(info, "__trace_printk_fmt",
2735 sizeof(*mod->trace_bprintk_fmt_start),
2736 &mod->num_trace_bprintk_fmt);
2738 * This section contains pointers to allocated objects in the trace
2739 * code and not scanning it leads to false positives.
2741 kmemleak_scan_area(mod->trace_bprintk_fmt_start,
2742 sizeof(*mod->trace_bprintk_fmt_start) *
2743 mod->num_trace_bprintk_fmt, GFP_KERNEL);
2745 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2746 /* sechdrs[0].sh_size is always zero */
2747 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
2748 sizeof(*mod->ftrace_callsites),
2749 &mod->num_ftrace_callsites);
2752 mod->extable = section_objs(info, "__ex_table",
2753 sizeof(*mod->extable), &mod->num_exentries);
2755 if (section_addr(info, "__obsparm"))
2756 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2759 info->debug = section_objs(info, "__verbose",
2760 sizeof(*info->debug), &info->num_debug);
2763 static int move_module(struct module *mod, struct load_info *info)
2768 /* Do the allocs. */
2769 ptr = module_alloc_update_bounds(mod->core_size);
2771 * The pointer to this block is stored in the module structure
2772 * which is inside the block. Just mark it as not being a
2775 kmemleak_not_leak(ptr);
2779 memset(ptr, 0, mod->core_size);
2780 mod->module_core = ptr;
2782 ptr = module_alloc_update_bounds(mod->init_size);
2784 * The pointer to this block is stored in the module structure
2785 * which is inside the block. This block doesn't need to be
2786 * scanned as it contains data and code that will be freed
2787 * after the module is initialized.
2789 kmemleak_ignore(ptr);
2790 if (!ptr && mod->init_size) {
2791 module_free(mod, mod->module_core);
2794 memset(ptr, 0, mod->init_size);
2795 mod->module_init = ptr;
2797 /* Transfer each section which specifies SHF_ALLOC */
2798 pr_debug("final section addresses:\n");
2799 for (i = 0; i < info->hdr->e_shnum; i++) {
2801 Elf_Shdr *shdr = &info->sechdrs[i];
2803 if (!(shdr->sh_flags & SHF_ALLOC))
2806 if (shdr->sh_entsize & INIT_OFFSET_MASK)
2807 dest = mod->module_init
2808 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
2810 dest = mod->module_core + shdr->sh_entsize;
2812 if (shdr->sh_type != SHT_NOBITS)
2813 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
2814 /* Update sh_addr to point to copy in image. */
2815 shdr->sh_addr = (unsigned long)dest;
2816 pr_debug("\t0x%lx %s\n",
2817 (long)shdr->sh_addr, info->secstrings + shdr->sh_name);
2823 static int check_module_license_and_versions(struct module *mod)
2826 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2827 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2828 * using GPL-only symbols it needs.
2830 if (strcmp(mod->name, "ndiswrapper") == 0)
2831 add_taint(TAINT_PROPRIETARY_MODULE);
2833 /* driverloader was caught wrongly pretending to be under GPL */
2834 if (strcmp(mod->name, "driverloader") == 0)
2835 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2837 #ifdef CONFIG_MODVERSIONS
2838 if ((mod->num_syms && !mod->crcs)
2839 || (mod->num_gpl_syms && !mod->gpl_crcs)
2840 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2841 #ifdef CONFIG_UNUSED_SYMBOLS
2842 || (mod->num_unused_syms && !mod->unused_crcs)
2843 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2846 return try_to_force_load(mod,
2847 "no versions for exported symbols");
2853 static void flush_module_icache(const struct module *mod)
2855 mm_segment_t old_fs;
2857 /* flush the icache in correct context */
2862 * Flush the instruction cache, since we've played with text.
2863 * Do it before processing of module parameters, so the module
2864 * can provide parameter accessor functions of its own.
2866 if (mod->module_init)
2867 flush_icache_range((unsigned long)mod->module_init,
2868 (unsigned long)mod->module_init
2870 flush_icache_range((unsigned long)mod->module_core,
2871 (unsigned long)mod->module_core + mod->core_size);
2876 int __weak module_frob_arch_sections(Elf_Ehdr *hdr,
2884 static struct module *layout_and_allocate(struct load_info *info)
2886 /* Module within temporary copy. */
2891 mod = setup_load_info(info);
2895 err = check_modinfo(mod, info);
2897 return ERR_PTR(err);
2899 /* Allow arches to frob section contents and sizes. */
2900 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2901 info->secstrings, mod);
2905 pcpusec = &info->sechdrs[info->index.pcpu];
2906 if (pcpusec->sh_size) {
2907 /* We have a special allocation for this section. */
2908 err = percpu_modalloc(mod,
2909 pcpusec->sh_size, pcpusec->sh_addralign);
2912 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
2915 /* Determine total sizes, and put offsets in sh_entsize. For now
2916 this is done generically; there doesn't appear to be any
2917 special cases for the architectures. */
2918 layout_sections(mod, info);
2919 layout_symtab(mod, info);
2921 /* Allocate and move to the final place */
2922 err = move_module(mod, info);
2926 /* Module has been copied to its final place now: return it. */
2927 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2928 kmemleak_load_module(mod, info);
2932 percpu_modfree(mod);
2934 return ERR_PTR(err);
2937 /* mod is no longer valid after this! */
2938 static void module_deallocate(struct module *mod, struct load_info *info)
2940 percpu_modfree(mod);
2941 module_free(mod, mod->module_init);
2942 module_free(mod, mod->module_core);
2945 int __weak module_finalize(const Elf_Ehdr *hdr,
2946 const Elf_Shdr *sechdrs,
2952 static int post_relocation(struct module *mod, const struct load_info *info)
2954 /* Sort exception table now relocations are done. */
2955 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2957 /* Copy relocated percpu area over. */
2958 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2959 info->sechdrs[info->index.pcpu].sh_size);
2961 /* Setup kallsyms-specific fields. */
2962 add_kallsyms(mod, info);
2964 /* Arch-specific module finalizing. */
2965 return module_finalize(info->hdr, info->sechdrs, mod);
2968 /* Allocate and load the module: note that size of section 0 is always
2969 zero, and we rely on this for optional sections. */
2970 static struct module *load_module(void __user *umod,
2972 const char __user *uargs)
2974 struct load_info info = { NULL, };
2978 pr_debug("load_module: umod=%p, len=%lu, uargs=%p\n",
2981 /* Copy in the blobs from userspace, check they are vaguely sane. */
2982 err = copy_and_check(&info, umod, len, uargs);
2984 return ERR_PTR(err);
2986 /* Figure out module layout, and allocate all the memory. */
2987 mod = layout_and_allocate(&info);
2993 /* Now module is in final location, initialize linked lists, etc. */
2994 err = module_unload_init(mod);
2998 /* Now we've got everything in the final locations, we can
2999 * find optional sections. */
3000 find_module_sections(mod, &info);
3002 err = check_module_license_and_versions(mod);
3006 /* Set up MODINFO_ATTR fields */
3007 setup_modinfo(mod, &info);
3009 /* Fix up syms, so that st_value is a pointer to location. */
3010 err = simplify_symbols(mod, &info);
3014 err = apply_relocations(mod, &info);
3018 err = post_relocation(mod, &info);
3022 flush_module_icache(mod);
3024 /* Now copy in args */
3025 mod->args = strndup_user(uargs, ~0UL >> 1);
3026 if (IS_ERR(mod->args)) {
3027 err = PTR_ERR(mod->args);
3028 goto free_arch_cleanup;
3031 /* Mark state as coming so strong_try_module_get() ignores us. */
3032 mod->state = MODULE_STATE_COMING;
3034 /* Now sew it into the lists so we can get lockdep and oops
3035 * info during argument parsing. No one should access us, since
3036 * strong_try_module_get() will fail.
3037 * lockdep/oops can run asynchronous, so use the RCU list insertion
3038 * function to insert in a way safe to concurrent readers.
3039 * The mutex protects against concurrent writers.
3041 mutex_lock(&module_mutex);
3042 if (find_module(mod->name)) {
3047 /* This has to be done once we're sure module name is unique. */
3048 dynamic_debug_setup(info.debug, info.num_debug);
3050 /* Find duplicate symbols */
3051 err = verify_export_symbols(mod);
3055 module_bug_finalize(info.hdr, info.sechdrs, mod);
3056 list_add_rcu(&mod->list, &modules);
3057 mutex_unlock(&module_mutex);
3059 /* Module is ready to execute: parsing args may do that. */
3060 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
3061 -32768, 32767, NULL);
3065 /* Link in to syfs. */
3066 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
3070 /* Initialize unwind table */
3071 add_unwind_table(mod, &info);
3073 /* Get rid of temporary copy. */
3077 trace_module_load(mod);
3081 mutex_lock(&module_mutex);
3082 /* Unlink carefully: kallsyms could be walking list. */
3083 list_del_rcu(&mod->list);
3084 module_bug_cleanup(mod);
3087 dynamic_debug_remove(info.debug);
3089 mutex_unlock(&module_mutex);
3090 synchronize_sched();
3093 module_arch_cleanup(mod);
3097 module_unload_free(mod);
3099 module_deallocate(mod, &info);
3102 return ERR_PTR(err);
3105 /* Call module constructors. */
3106 static void do_mod_ctors(struct module *mod)
3108 #ifdef CONFIG_CONSTRUCTORS
3111 for (i = 0; i < mod->num_ctors; i++)
3116 /* This is where the real work happens */
3117 SYSCALL_DEFINE3(init_module, void __user *, umod,
3118 unsigned long, len, const char __user *, uargs)
3123 /* Must have permission */
3124 if (!capable(CAP_SYS_MODULE) || modules_disabled)
3127 /* Do all the hard work */
3128 mod = load_module(umod, len, uargs);
3130 return PTR_ERR(mod);
3132 blocking_notifier_call_chain(&module_notify_list,
3133 MODULE_STATE_COMING, mod);
3135 /* Set RO and NX regions for core */
3136 set_section_ro_nx(mod->module_core,
3137 mod->core_text_size,
3141 /* Set RO and NX regions for init */
3142 set_section_ro_nx(mod->module_init,
3143 mod->init_text_size,
3148 /* Start the module */
3149 if (mod->init != NULL)
3150 ret = do_one_initcall(mod->init);
3152 /* Init routine failed: abort. Try to protect us from
3153 buggy refcounters. */
3154 mod->state = MODULE_STATE_GOING;
3155 synchronize_sched();
3157 blocking_notifier_call_chain(&module_notify_list,
3158 MODULE_STATE_GOING, mod);
3160 wake_up(&module_wq);
3165 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
3166 "%s: loading module anyway...\n",
3167 __func__, mod->name, ret,
3172 /* Now it's a first class citizen! Wake up anyone waiting for it. */
3173 mod->state = MODULE_STATE_LIVE;
3174 wake_up(&module_wq);
3175 blocking_notifier_call_chain(&module_notify_list,
3176 MODULE_STATE_LIVE, mod);
3178 /* We need to finish all async code before the module init sequence is done */
3179 async_synchronize_full();
3181 mutex_lock(&module_mutex);
3182 /* Drop initial reference. */
3184 trim_init_extable(mod);
3185 unwind_remove_table(mod->unwind_info, 1);
3186 #ifdef CONFIG_KALLSYMS
3187 mod->num_symtab = mod->core_num_syms;
3188 mod->symtab = mod->core_symtab;
3189 mod->strtab = mod->core_strtab;
3191 unset_module_init_ro_nx(mod);
3192 module_free(mod, mod->module_init);
3193 mod->module_init = NULL;
3195 mod->init_ro_size = 0;
3196 mod->init_text_size = 0;
3197 mutex_unlock(&module_mutex);
3202 static inline int within(unsigned long addr, void *start, unsigned long size)
3204 return ((void *)addr >= start && (void *)addr < start + size);
3207 #ifdef CONFIG_KALLSYMS
3209 * This ignores the intensely annoying "mapping symbols" found
3210 * in ARM ELF files: $a, $t and $d.
3212 static inline int is_arm_mapping_symbol(const char *str)
3214 return str[0] == '$' && strchr("atd", str[1])
3215 && (str[2] == '\0' || str[2] == '.');
3218 static const char *get_ksymbol(struct module *mod,
3220 unsigned long *size,
3221 unsigned long *offset)
3223 unsigned int i, best = 0;
3224 unsigned long nextval;
3226 /* At worse, next value is at end of module */
3227 if (within_module_init(addr, mod))
3228 nextval = (unsigned long)mod->module_init+mod->init_text_size;
3230 nextval = (unsigned long)mod->module_core+mod->core_text_size;
3232 /* Scan for closest preceding symbol, and next symbol. (ELF
3233 starts real symbols at 1). */
3234 for (i = 1; i < mod->num_symtab; i++) {
3235 if (mod->symtab[i].st_shndx == SHN_UNDEF)
3238 /* We ignore unnamed symbols: they're uninformative
3239 * and inserted at a whim. */
3240 if (mod->symtab[i].st_value <= addr
3241 && mod->symtab[i].st_value > mod->symtab[best].st_value
3242 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3243 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3245 if (mod->symtab[i].st_value > addr
3246 && mod->symtab[i].st_value < nextval
3247 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
3248 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
3249 nextval = mod->symtab[i].st_value;
3256 *size = nextval - mod->symtab[best].st_value;
3258 *offset = addr - mod->symtab[best].st_value;
3259 return mod->strtab + mod->symtab[best].st_name;
3262 /* For kallsyms to ask for address resolution. NULL means not found. Careful
3263 * not to lock to avoid deadlock on oopses, simply disable preemption. */
3264 const char *module_address_lookup(unsigned long addr,
3265 unsigned long *size,
3266 unsigned long *offset,
3271 const char *ret = NULL;
3274 list_for_each_entry_rcu(mod, &modules, list) {
3275 if (within_module_init(addr, mod) ||
3276 within_module_core(addr, mod)) {
3278 *modname = mod->name;
3279 ret = get_ksymbol(mod, addr, size, offset);
3283 /* Make a copy in here where it's safe */
3285 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
3292 int lookup_module_symbol_name(unsigned long addr, char *symname)
3297 list_for_each_entry_rcu(mod, &modules, list) {
3298 if (within_module_init(addr, mod) ||
3299 within_module_core(addr, mod)) {
3302 sym = get_ksymbol(mod, addr, NULL, NULL);
3305 strlcpy(symname, sym, KSYM_NAME_LEN);
3315 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
3316 unsigned long *offset, char *modname, char *name)
3321 list_for_each_entry_rcu(mod, &modules, list) {
3322 if (within_module_init(addr, mod) ||
3323 within_module_core(addr, mod)) {
3326 sym = get_ksymbol(mod, addr, size, offset);
3330 strlcpy(modname, mod->name, MODULE_NAME_LEN);
3332 strlcpy(name, sym, KSYM_NAME_LEN);
3342 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
3343 char *name, char *module_name, int *exported)
3348 list_for_each_entry_rcu(mod, &modules, list) {
3349 if (symnum < mod->num_symtab) {
3350 *value = mod->symtab[symnum].st_value;
3351 *type = mod->symtab[symnum].st_info;
3352 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
3354 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
3355 *exported = is_exported(name, *value, mod);
3359 symnum -= mod->num_symtab;
3365 static unsigned long mod_find_symname(struct module *mod, const char *name)
3369 for (i = 0; i < mod->num_symtab; i++)
3370 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
3371 mod->symtab[i].st_info != 'U')
3372 return mod->symtab[i].st_value;
3376 /* Look for this name: can be of form module:name. */
3377 unsigned long module_kallsyms_lookup_name(const char *name)
3381 unsigned long ret = 0;
3383 /* Don't lock: we're in enough trouble already. */
3385 if ((colon = strchr(name, ':')) != NULL) {
3387 if ((mod = find_module(name)) != NULL)
3388 ret = mod_find_symname(mod, colon+1);
3391 list_for_each_entry_rcu(mod, &modules, list)
3392 if ((ret = mod_find_symname(mod, name)) != 0)
3399 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3400 struct module *, unsigned long),
3407 list_for_each_entry(mod, &modules, list) {
3408 for (i = 0; i < mod->num_symtab; i++) {
3409 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3410 mod, mod->symtab[i].st_value);
3417 #endif /* CONFIG_KALLSYMS */
3419 static char *module_flags(struct module *mod, char *buf)
3424 mod->state == MODULE_STATE_GOING ||
3425 mod->state == MODULE_STATE_COMING) {
3427 bx += module_flags_taint(mod, buf + bx);
3428 /* Show a - for module-is-being-unloaded */
3429 if (mod->state == MODULE_STATE_GOING)
3431 /* Show a + for module-is-being-loaded */
3432 if (mod->state == MODULE_STATE_COMING)
3441 #ifdef CONFIG_PROC_FS
3442 /* Called by the /proc file system to return a list of modules. */
3443 static void *m_start(struct seq_file *m, loff_t *pos)
3445 mutex_lock(&module_mutex);
3446 return seq_list_start(&modules, *pos);
3449 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
3451 return seq_list_next(p, &modules, pos);
3454 static void m_stop(struct seq_file *m, void *p)
3456 mutex_unlock(&module_mutex);
3459 static int m_show(struct seq_file *m, void *p)
3461 struct module *mod = list_entry(p, struct module, list);
3464 seq_printf(m, "%s %u",
3465 mod->name, mod->init_size + mod->core_size);
3466 print_unload_info(m, mod);
3468 /* Informative for users. */
3469 seq_printf(m, " %s",
3470 mod->state == MODULE_STATE_GOING ? "Unloading":
3471 mod->state == MODULE_STATE_COMING ? "Loading":
3473 /* Used by oprofile and other similar tools. */
3474 seq_printf(m, " 0x%pK", mod->module_core);
3478 seq_printf(m, " %s", module_flags(mod, buf));
3480 seq_printf(m, "\n");
3484 /* Format: modulename size refcount deps address
3486 Where refcount is a number or -, and deps is a comma-separated list
3489 static const struct seq_operations modules_op = {
3496 static int modules_open(struct inode *inode, struct file *file)
3498 return seq_open(file, &modules_op);
3501 static const struct file_operations proc_modules_operations = {
3502 .open = modules_open,
3504 .llseek = seq_lseek,
3505 .release = seq_release,
3508 static int __init proc_modules_init(void)
3510 proc_create("modules", 0, NULL, &proc_modules_operations);
3513 module_init(proc_modules_init);
3516 /* Given an address, look for it in the module exception tables. */
3517 const struct exception_table_entry *search_module_extables(unsigned long addr)
3519 const struct exception_table_entry *e = NULL;
3523 list_for_each_entry_rcu(mod, &modules, list) {
3524 if (mod->num_exentries == 0)
3527 e = search_extable(mod->extable,
3528 mod->extable + mod->num_exentries - 1,
3535 /* Now, if we found one, we are running inside it now, hence
3536 we cannot unload the module, hence no refcnt needed. */
3541 * is_module_address - is this address inside a module?
3542 * @addr: the address to check.
3544 * See is_module_text_address() if you simply want to see if the address
3545 * is code (not data).
3547 bool is_module_address(unsigned long addr)
3552 ret = __module_address(addr) != NULL;
3559 * __module_address - get the module which contains an address.
3560 * @addr: the address.
3562 * Must be called with preempt disabled or module mutex held so that
3563 * module doesn't get freed during this.
3565 struct module *__module_address(unsigned long addr)
3569 if (addr < module_addr_min || addr > module_addr_max)
3572 list_for_each_entry_rcu(mod, &modules, list)
3573 if (within_module_core(addr, mod)
3574 || within_module_init(addr, mod))
3578 EXPORT_SYMBOL_GPL(__module_address);
3581 * is_module_text_address - is this address inside module code?
3582 * @addr: the address to check.
3584 * See is_module_address() if you simply want to see if the address is
3585 * anywhere in a module. See kernel_text_address() for testing if an
3586 * address corresponds to kernel or module code.
3588 bool is_module_text_address(unsigned long addr)
3593 ret = __module_text_address(addr) != NULL;
3600 * __module_text_address - get the module whose code contains an address.
3601 * @addr: the address.
3603 * Must be called with preempt disabled or module mutex held so that
3604 * module doesn't get freed during this.
3606 struct module *__module_text_address(unsigned long addr)
3608 struct module *mod = __module_address(addr);
3610 /* Make sure it's within the text section. */
3611 if (!within(addr, mod->module_init, mod->init_text_size)
3612 && !within(addr, mod->module_core, mod->core_text_size))
3617 EXPORT_SYMBOL_GPL(__module_text_address);
3619 /* Don't grab lock, we're oopsing. */
3620 void print_modules(void)
3625 printk(KERN_DEFAULT "Modules linked in:");
3626 /* Most callers should already have preempt disabled, but make sure */
3628 list_for_each_entry_rcu(mod, &modules, list)
3629 printk(" %s%s", mod->name, module_flags(mod, buf));
3631 if (last_unloaded_module[0])
3632 printk(" [last unloaded: %s]", last_unloaded_module);
3634 #ifdef CONFIG_ENTERPRISE_SUPPORT
3635 printk("Supported: %s\n", supported_printable(get_taint()));
3639 #ifdef CONFIG_MODVERSIONS
3640 /* Generate the signature for all relevant module structures here.
3641 * If these change, we don't want to try to parse the module. */
3642 void module_layout(struct module *mod,
3643 struct modversion_info *ver,
3644 struct kernel_param *kp,
3645 struct kernel_symbol *ks,
3646 struct tracepoint * const *tp)
3649 EXPORT_SYMBOL(module_layout);