2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/module.h>
20 #include <linux/moduleloader.h>
21 #include <linux/ftrace_event.h>
22 #include <linux/init.h>
23 #include <linux/kallsyms.h>
25 #include <linux/sysfs.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/elf.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/syscalls.h>
33 #include <linux/fcntl.h>
34 #include <linux/rcupdate.h>
35 #include <linux/capability.h>
36 #include <linux/cpu.h>
37 #include <linux/moduleparam.h>
38 #include <linux/errno.h>
39 #include <linux/err.h>
40 #include <linux/vermagic.h>
41 #include <linux/notifier.h>
42 #include <linux/sched.h>
43 #include <linux/stop_machine.h>
44 #include <linux/device.h>
45 #include <linux/string.h>
46 #include <linux/mutex.h>
47 #include <linux/rculist.h>
48 #include <asm/uaccess.h>
49 #include <asm/cacheflush.h>
50 #include <linux/license.h>
51 #include <asm/sections.h>
52 #include <linux/tracepoint.h>
53 #include <linux/ftrace.h>
54 #include <linux/async.h>
55 #include <linux/percpu.h>
56 #include <linux/kmemleak.h>
58 #define CREATE_TRACE_POINTS
59 #include <trace/events/module.h>
61 EXPORT_TRACEPOINT_SYMBOL(module_get);
66 #define DEBUGP(fmt , a...)
69 #ifndef ARCH_SHF_SMALL
70 #define ARCH_SHF_SMALL 0
73 /* If this is set, the section belongs in the init part of the module */
74 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
76 /* List of modules, protected by module_mutex or preempt_disable
77 * (delete uses stop_machine/add uses RCU list operations). */
78 DEFINE_MUTEX(module_mutex);
79 EXPORT_SYMBOL_GPL(module_mutex);
80 static LIST_HEAD(modules);
82 /* Block module loading/unloading? */
83 int modules_disabled = 0;
85 /* Waiting for a module to finish initializing? */
86 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
88 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
90 /* Bounds of module allocation, for speeding __module_address */
91 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
93 int register_module_notifier(struct notifier_block * nb)
95 return blocking_notifier_chain_register(&module_notify_list, nb);
97 EXPORT_SYMBOL(register_module_notifier);
99 int unregister_module_notifier(struct notifier_block * nb)
101 return blocking_notifier_chain_unregister(&module_notify_list, nb);
103 EXPORT_SYMBOL(unregister_module_notifier);
105 /* We require a truly strong try_module_get(): 0 means failure due to
106 ongoing or failed initialization etc. */
107 static inline int strong_try_module_get(struct module *mod)
109 if (mod && mod->state == MODULE_STATE_COMING)
111 if (try_module_get(mod))
117 static inline void add_taint_module(struct module *mod, unsigned flag)
120 mod->taints |= (1U << flag);
124 * A thread that wants to hold a reference to a module only while it
125 * is running can call this to safely exit. nfsd and lockd use this.
127 void __module_put_and_exit(struct module *mod, long code)
132 EXPORT_SYMBOL(__module_put_and_exit);
134 /* Find a module section: 0 means not found. */
135 static unsigned int find_sec(Elf_Ehdr *hdr,
137 const char *secstrings,
142 for (i = 1; i < hdr->e_shnum; i++)
143 /* Alloc bit cleared means "ignore it." */
144 if ((sechdrs[i].sh_flags & SHF_ALLOC)
145 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
150 /* Find a module section, or NULL. */
151 static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
152 const char *secstrings, const char *name)
154 /* Section 0 has sh_addr 0. */
155 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
158 /* Find a module section, or NULL. Fill in number of "objects" in section. */
159 static void *section_objs(Elf_Ehdr *hdr,
161 const char *secstrings,
166 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
168 /* Section 0 has sh_addr 0 and sh_size 0. */
169 *num = sechdrs[sec].sh_size / object_size;
170 return (void *)sechdrs[sec].sh_addr;
173 /* Provided by the linker */
174 extern const struct kernel_symbol __start___ksymtab[];
175 extern const struct kernel_symbol __stop___ksymtab[];
176 extern const struct kernel_symbol __start___ksymtab_gpl[];
177 extern const struct kernel_symbol __stop___ksymtab_gpl[];
178 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
179 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
180 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
181 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
182 extern const unsigned long __start___kcrctab[];
183 extern const unsigned long __start___kcrctab_gpl[];
184 extern const unsigned long __start___kcrctab_gpl_future[];
185 #ifdef CONFIG_UNUSED_SYMBOLS
186 extern const struct kernel_symbol __start___ksymtab_unused[];
187 extern const struct kernel_symbol __stop___ksymtab_unused[];
188 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
189 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
190 extern const unsigned long __start___kcrctab_unused[];
191 extern const unsigned long __start___kcrctab_unused_gpl[];
194 #ifndef CONFIG_MODVERSIONS
195 #define symversion(base, idx) NULL
197 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
200 static bool each_symbol_in_section(const struct symsearch *arr,
201 unsigned int arrsize,
202 struct module *owner,
203 bool (*fn)(const struct symsearch *syms,
204 struct module *owner,
205 unsigned int symnum, void *data),
210 for (j = 0; j < arrsize; j++) {
211 for (i = 0; i < arr[j].stop - arr[j].start; i++)
212 if (fn(&arr[j], owner, i, data))
219 /* Returns true as soon as fn returns true, otherwise false. */
220 bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
221 unsigned int symnum, void *data), void *data)
224 const struct symsearch arr[] = {
225 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
226 NOT_GPL_ONLY, false },
227 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
228 __start___kcrctab_gpl,
230 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
231 __start___kcrctab_gpl_future,
232 WILL_BE_GPL_ONLY, false },
233 #ifdef CONFIG_UNUSED_SYMBOLS
234 { __start___ksymtab_unused, __stop___ksymtab_unused,
235 __start___kcrctab_unused,
236 NOT_GPL_ONLY, true },
237 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
238 __start___kcrctab_unused_gpl,
243 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
246 list_for_each_entry_rcu(mod, &modules, list) {
247 struct symsearch arr[] = {
248 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
249 NOT_GPL_ONLY, false },
250 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
253 { mod->gpl_future_syms,
254 mod->gpl_future_syms + mod->num_gpl_future_syms,
255 mod->gpl_future_crcs,
256 WILL_BE_GPL_ONLY, false },
257 #ifdef CONFIG_UNUSED_SYMBOLS
259 mod->unused_syms + mod->num_unused_syms,
261 NOT_GPL_ONLY, true },
262 { mod->unused_gpl_syms,
263 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
264 mod->unused_gpl_crcs,
269 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
274 EXPORT_SYMBOL_GPL(each_symbol);
276 struct find_symbol_arg {
283 struct module *owner;
284 const unsigned long *crc;
285 const struct kernel_symbol *sym;
288 static bool find_symbol_in_section(const struct symsearch *syms,
289 struct module *owner,
290 unsigned int symnum, void *data)
292 struct find_symbol_arg *fsa = data;
294 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
298 if (syms->licence == GPL_ONLY)
300 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
301 printk(KERN_WARNING "Symbol %s is being used "
302 "by a non-GPL module, which will not "
303 "be allowed in the future\n", fsa->name);
304 printk(KERN_WARNING "Please see the file "
305 "Documentation/feature-removal-schedule.txt "
306 "in the kernel source tree for more details.\n");
310 #ifdef CONFIG_UNUSED_SYMBOLS
311 if (syms->unused && fsa->warn) {
312 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
313 "however this module is using it.\n", fsa->name);
315 "This symbol will go away in the future.\n");
317 "Please evalute if this is the right api to use and if "
318 "it really is, submit a report the linux kernel "
319 "mailinglist together with submitting your code for "
325 fsa->crc = symversion(syms->crcs, symnum);
326 fsa->sym = &syms->start[symnum];
330 /* Find a symbol and return it, along with, (optional) crc and
331 * (optional) module which owns it */
332 const struct kernel_symbol *find_symbol(const char *name,
333 struct module **owner,
334 const unsigned long **crc,
338 struct find_symbol_arg fsa;
344 if (each_symbol(find_symbol_in_section, &fsa)) {
352 DEBUGP("Failed to find symbol %s\n", name);
355 EXPORT_SYMBOL_GPL(find_symbol);
357 /* Search for module by name: must hold module_mutex. */
358 struct module *find_module(const char *name)
362 list_for_each_entry(mod, &modules, list) {
363 if (strcmp(mod->name, name) == 0)
368 EXPORT_SYMBOL_GPL(find_module);
372 #ifndef CONFIG_HAVE_LEGACY_PER_CPU_AREA
374 static void *percpu_modalloc(unsigned long size, unsigned long align,
379 if (align > PAGE_SIZE) {
380 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
381 name, align, PAGE_SIZE);
385 ptr = __alloc_reserved_percpu(size, align);
388 "Could not allocate %lu bytes percpu data\n", size);
392 static void percpu_modfree(void *freeme)
397 #else /* ... CONFIG_HAVE_LEGACY_PER_CPU_AREA */
399 /* Number of blocks used and allocated. */
400 static unsigned int pcpu_num_used, pcpu_num_allocated;
401 /* Size of each block. -ve means used. */
402 static int *pcpu_size;
404 static int split_block(unsigned int i, unsigned short size)
406 /* Reallocation required? */
407 if (pcpu_num_used + 1 > pcpu_num_allocated) {
410 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
415 pcpu_num_allocated *= 2;
419 /* Insert a new subblock */
420 memmove(&pcpu_size[i+1], &pcpu_size[i],
421 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
424 pcpu_size[i+1] -= size;
429 static inline unsigned int block_size(int val)
436 static void *percpu_modalloc(unsigned long size, unsigned long align,
444 if (align > PAGE_SIZE) {
445 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
446 name, align, PAGE_SIZE);
450 ptr = __per_cpu_start;
451 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
452 /* Extra for alignment requirement. */
453 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
454 BUG_ON(i == 0 && extra != 0);
456 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
459 /* Transfer extra to previous block. */
460 if (pcpu_size[i-1] < 0)
461 pcpu_size[i-1] -= extra;
463 pcpu_size[i-1] += extra;
464 pcpu_size[i] -= extra;
467 /* Split block if warranted */
468 if (pcpu_size[i] - size > sizeof(unsigned long))
469 if (!split_block(i, size))
472 /* add the per-cpu scanning areas */
473 for_each_possible_cpu(cpu)
474 kmemleak_alloc(ptr + per_cpu_offset(cpu), size, 0,
478 pcpu_size[i] = -pcpu_size[i];
482 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
487 static void percpu_modfree(void *freeme)
490 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
493 /* First entry is core kernel percpu data. */
494 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
496 pcpu_size[i] = -pcpu_size[i];
503 /* remove the per-cpu scanning areas */
504 for_each_possible_cpu(cpu)
505 kmemleak_free(freeme + per_cpu_offset(cpu));
507 /* Merge with previous? */
508 if (pcpu_size[i-1] >= 0) {
509 pcpu_size[i-1] += pcpu_size[i];
511 memmove(&pcpu_size[i], &pcpu_size[i+1],
512 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
515 /* Merge with next? */
516 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
517 pcpu_size[i] += pcpu_size[i+1];
519 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
520 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
524 static int percpu_modinit(void)
527 pcpu_num_allocated = 2;
528 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
530 /* Static in-kernel percpu data (used). */
531 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
533 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
534 if (pcpu_size[1] < 0) {
535 printk(KERN_ERR "No per-cpu room for modules.\n");
541 __initcall(percpu_modinit);
543 #endif /* CONFIG_HAVE_LEGACY_PER_CPU_AREA */
545 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
547 const char *secstrings)
549 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
552 static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
556 for_each_possible_cpu(cpu)
557 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
560 #else /* ... !CONFIG_SMP */
562 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
567 static inline void percpu_modfree(void *pcpuptr)
571 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
573 const char *secstrings)
577 static inline void percpu_modcopy(void *pcpudst, const void *src,
580 /* pcpusec should be 0, and size of that section should be 0. */
584 #endif /* CONFIG_SMP */
586 #define MODINFO_ATTR(field) \
587 static void setup_modinfo_##field(struct module *mod, const char *s) \
589 mod->field = kstrdup(s, GFP_KERNEL); \
591 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
592 struct module *mod, char *buffer) \
594 return sprintf(buffer, "%s\n", mod->field); \
596 static int modinfo_##field##_exists(struct module *mod) \
598 return mod->field != NULL; \
600 static void free_modinfo_##field(struct module *mod) \
605 static struct module_attribute modinfo_##field = { \
606 .attr = { .name = __stringify(field), .mode = 0444 }, \
607 .show = show_modinfo_##field, \
608 .setup = setup_modinfo_##field, \
609 .test = modinfo_##field##_exists, \
610 .free = free_modinfo_##field, \
613 MODINFO_ATTR(version);
614 MODINFO_ATTR(srcversion);
616 static char last_unloaded_module[MODULE_NAME_LEN+1];
618 #ifdef CONFIG_MODULE_UNLOAD
619 /* Init the unload section of the module. */
620 static void module_unload_init(struct module *mod)
624 INIT_LIST_HEAD(&mod->modules_which_use_me);
625 for_each_possible_cpu(cpu)
626 local_set(__module_ref_addr(mod, cpu), 0);
627 /* Hold reference count during initialization. */
628 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
629 /* Backwards compatibility macros put refcount during init. */
630 mod->waiter = current;
633 /* modules using other modules */
636 struct list_head list;
637 struct module *module_which_uses;
640 /* Does a already use b? */
641 static int already_uses(struct module *a, struct module *b)
643 struct module_use *use;
645 list_for_each_entry(use, &b->modules_which_use_me, list) {
646 if (use->module_which_uses == a) {
647 DEBUGP("%s uses %s!\n", a->name, b->name);
651 DEBUGP("%s does not use %s!\n", a->name, b->name);
655 /* Module a uses b */
656 int use_module(struct module *a, struct module *b)
658 struct module_use *use;
661 if (b == NULL || already_uses(a, b)) return 1;
663 /* If we're interrupted or time out, we fail. */
664 if (wait_event_interruptible_timeout(
665 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
667 printk("%s: gave up waiting for init of module %s.\n",
672 /* If strong_try_module_get() returned a different error, we fail. */
676 DEBUGP("Allocating new usage for %s.\n", a->name);
677 use = kmalloc(sizeof(*use), GFP_ATOMIC);
679 printk("%s: out of memory loading\n", a->name);
684 use->module_which_uses = a;
685 list_add(&use->list, &b->modules_which_use_me);
686 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
689 EXPORT_SYMBOL_GPL(use_module);
691 /* Clear the unload stuff of the module. */
692 static void module_unload_free(struct module *mod)
696 list_for_each_entry(i, &modules, list) {
697 struct module_use *use;
699 list_for_each_entry(use, &i->modules_which_use_me, list) {
700 if (use->module_which_uses == mod) {
701 DEBUGP("%s unusing %s\n", mod->name, i->name);
703 list_del(&use->list);
705 sysfs_remove_link(i->holders_dir, mod->name);
706 /* There can be at most one match. */
713 #ifdef CONFIG_MODULE_FORCE_UNLOAD
714 static inline int try_force_unload(unsigned int flags)
716 int ret = (flags & O_TRUNC);
718 add_taint(TAINT_FORCED_RMMOD);
722 static inline int try_force_unload(unsigned int flags)
726 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
735 /* Whole machine is stopped with interrupts off when this runs. */
736 static int __try_stop_module(void *_sref)
738 struct stopref *sref = _sref;
740 /* If it's not unused, quit unless we're forcing. */
741 if (module_refcount(sref->mod) != 0) {
742 if (!(*sref->forced = try_force_unload(sref->flags)))
746 /* Mark it as dying. */
747 sref->mod->state = MODULE_STATE_GOING;
751 static int try_stop_module(struct module *mod, int flags, int *forced)
753 if (flags & O_NONBLOCK) {
754 struct stopref sref = { mod, flags, forced };
756 return stop_machine(__try_stop_module, &sref, NULL);
758 /* We don't need to stop the machine for this. */
759 mod->state = MODULE_STATE_GOING;
765 unsigned int module_refcount(struct module *mod)
767 unsigned int total = 0;
770 for_each_possible_cpu(cpu)
771 total += local_read(__module_ref_addr(mod, cpu));
774 EXPORT_SYMBOL(module_refcount);
776 /* This exists whether we can unload or not */
777 static void free_module(struct module *mod);
779 static void wait_for_zero_refcount(struct module *mod)
781 /* Since we might sleep for some time, release the mutex first */
782 mutex_unlock(&module_mutex);
784 DEBUGP("Looking at refcount...\n");
785 set_current_state(TASK_UNINTERRUPTIBLE);
786 if (module_refcount(mod) == 0)
790 current->state = TASK_RUNNING;
791 mutex_lock(&module_mutex);
794 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
798 char name[MODULE_NAME_LEN];
801 if (!capable(CAP_SYS_MODULE) || modules_disabled)
804 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
806 name[MODULE_NAME_LEN-1] = '\0';
808 /* Create stop_machine threads since free_module relies on
809 * a non-failing stop_machine call. */
810 ret = stop_machine_create();
814 if (mutex_lock_interruptible(&module_mutex) != 0) {
819 mod = find_module(name);
825 if (!list_empty(&mod->modules_which_use_me)) {
826 /* Other modules depend on us: get rid of them first. */
831 /* Doing init or already dying? */
832 if (mod->state != MODULE_STATE_LIVE) {
833 /* FIXME: if (force), slam module count and wake up
835 DEBUGP("%s already dying\n", mod->name);
840 /* If it has an init func, it must have an exit func to unload */
841 if (mod->init && !mod->exit) {
842 forced = try_force_unload(flags);
844 /* This module can't be removed */
850 /* Set this up before setting mod->state */
851 mod->waiter = current;
853 /* Stop the machine so refcounts can't move and disable module. */
854 ret = try_stop_module(mod, flags, &forced);
858 /* Never wait if forced. */
859 if (!forced && module_refcount(mod) != 0)
860 wait_for_zero_refcount(mod);
862 mutex_unlock(&module_mutex);
863 /* Final destruction now noone is using it. */
864 if (mod->exit != NULL)
866 blocking_notifier_call_chain(&module_notify_list,
867 MODULE_STATE_GOING, mod);
868 async_synchronize_full();
869 mutex_lock(&module_mutex);
870 /* Store the name of the last unloaded module for diagnostic purposes */
871 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
872 ddebug_remove_module(mod->name);
876 mutex_unlock(&module_mutex);
878 stop_machine_destroy();
882 static inline void print_unload_info(struct seq_file *m, struct module *mod)
884 struct module_use *use;
885 int printed_something = 0;
887 seq_printf(m, " %u ", module_refcount(mod));
889 /* Always include a trailing , so userspace can differentiate
890 between this and the old multi-field proc format. */
891 list_for_each_entry(use, &mod->modules_which_use_me, list) {
892 printed_something = 1;
893 seq_printf(m, "%s,", use->module_which_uses->name);
896 if (mod->init != NULL && mod->exit == NULL) {
897 printed_something = 1;
898 seq_printf(m, "[permanent],");
901 if (!printed_something)
905 void __symbol_put(const char *symbol)
907 struct module *owner;
910 if (!find_symbol(symbol, &owner, NULL, true, false))
915 EXPORT_SYMBOL(__symbol_put);
917 /* Note this assumes addr is a function, which it currently always is. */
918 void symbol_put_addr(void *addr)
920 struct module *modaddr;
921 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
923 if (core_kernel_text(a))
926 /* module_text_address is safe here: we're supposed to have reference
927 * to module from symbol_get, so it can't go away. */
928 modaddr = __module_text_address(a);
932 EXPORT_SYMBOL_GPL(symbol_put_addr);
934 static ssize_t show_refcnt(struct module_attribute *mattr,
935 struct module *mod, char *buffer)
937 return sprintf(buffer, "%u\n", module_refcount(mod));
940 static struct module_attribute refcnt = {
941 .attr = { .name = "refcnt", .mode = 0444 },
945 void module_put(struct module *module)
948 unsigned int cpu = get_cpu();
949 local_dec(__module_ref_addr(module, cpu));
950 trace_module_put(module, _RET_IP_,
951 local_read(__module_ref_addr(module, cpu)));
952 /* Maybe they're waiting for us to drop reference? */
953 if (unlikely(!module_is_live(module)))
954 wake_up_process(module->waiter);
958 EXPORT_SYMBOL(module_put);
960 #else /* !CONFIG_MODULE_UNLOAD */
961 static inline void print_unload_info(struct seq_file *m, struct module *mod)
963 /* We don't know the usage count, or what modules are using. */
964 seq_printf(m, " - -");
967 static inline void module_unload_free(struct module *mod)
971 int use_module(struct module *a, struct module *b)
973 return strong_try_module_get(b) == 0;
975 EXPORT_SYMBOL_GPL(use_module);
977 static inline void module_unload_init(struct module *mod)
980 #endif /* CONFIG_MODULE_UNLOAD */
982 static ssize_t show_initstate(struct module_attribute *mattr,
983 struct module *mod, char *buffer)
985 const char *state = "unknown";
987 switch (mod->state) {
988 case MODULE_STATE_LIVE:
991 case MODULE_STATE_COMING:
994 case MODULE_STATE_GOING:
998 return sprintf(buffer, "%s\n", state);
1001 static struct module_attribute initstate = {
1002 .attr = { .name = "initstate", .mode = 0444 },
1003 .show = show_initstate,
1006 static struct module_attribute *modinfo_attrs[] = {
1008 &modinfo_srcversion,
1010 #ifdef CONFIG_MODULE_UNLOAD
1016 static const char vermagic[] = VERMAGIC_STRING;
1018 static int try_to_force_load(struct module *mod, const char *reason)
1020 #ifdef CONFIG_MODULE_FORCE_LOAD
1021 if (!test_taint(TAINT_FORCED_MODULE))
1022 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1024 add_taint_module(mod, TAINT_FORCED_MODULE);
1031 #ifdef CONFIG_MODVERSIONS
1032 static int check_version(Elf_Shdr *sechdrs,
1033 unsigned int versindex,
1034 const char *symname,
1036 const unsigned long *crc)
1038 unsigned int i, num_versions;
1039 struct modversion_info *versions;
1041 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1045 /* No versions at all? modprobe --force does this. */
1047 return try_to_force_load(mod, symname) == 0;
1049 versions = (void *) sechdrs[versindex].sh_addr;
1050 num_versions = sechdrs[versindex].sh_size
1051 / sizeof(struct modversion_info);
1053 for (i = 0; i < num_versions; i++) {
1054 if (strcmp(versions[i].name, symname) != 0)
1057 if (versions[i].crc == *crc)
1059 DEBUGP("Found checksum %lX vs module %lX\n",
1060 *crc, versions[i].crc);
1064 printk(KERN_WARNING "%s: no symbol version for %s\n",
1065 mod->name, symname);
1069 printk("%s: disagrees about version of symbol %s\n",
1070 mod->name, symname);
1074 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1075 unsigned int versindex,
1078 const unsigned long *crc;
1080 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1083 return check_version(sechdrs, versindex, "module_layout", mod, crc);
1086 /* First part is kernel version, which we ignore if module has crcs. */
1087 static inline int same_magic(const char *amagic, const char *bmagic,
1091 amagic += strcspn(amagic, " ");
1092 bmagic += strcspn(bmagic, " ");
1094 return strcmp(amagic, bmagic) == 0;
1097 static inline int check_version(Elf_Shdr *sechdrs,
1098 unsigned int versindex,
1099 const char *symname,
1101 const unsigned long *crc)
1106 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1107 unsigned int versindex,
1113 static inline int same_magic(const char *amagic, const char *bmagic,
1116 return strcmp(amagic, bmagic) == 0;
1118 #endif /* CONFIG_MODVERSIONS */
1120 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1121 Must be holding module_mutex. */
1122 static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1123 unsigned int versindex,
1127 struct module *owner;
1128 const struct kernel_symbol *sym;
1129 const unsigned long *crc;
1131 sym = find_symbol(name, &owner, &crc,
1132 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1133 /* use_module can fail due to OOM,
1134 or module initialization or unloading */
1136 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1137 !use_module(mod, owner))
1144 * /sys/module/foo/sections stuff
1145 * J. Corbet <corbet@lwn.net>
1147 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1148 struct module_sect_attr
1150 struct module_attribute mattr;
1152 unsigned long address;
1155 struct module_sect_attrs
1157 struct attribute_group grp;
1158 unsigned int nsections;
1159 struct module_sect_attr attrs[0];
1162 static ssize_t module_sect_show(struct module_attribute *mattr,
1163 struct module *mod, char *buf)
1165 struct module_sect_attr *sattr =
1166 container_of(mattr, struct module_sect_attr, mattr);
1167 return sprintf(buf, "0x%lx\n", sattr->address);
1170 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1172 unsigned int section;
1174 for (section = 0; section < sect_attrs->nsections; section++)
1175 kfree(sect_attrs->attrs[section].name);
1179 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1180 char *secstrings, Elf_Shdr *sechdrs)
1182 unsigned int nloaded = 0, i, size[2];
1183 struct module_sect_attrs *sect_attrs;
1184 struct module_sect_attr *sattr;
1185 struct attribute **gattr;
1187 /* Count loaded sections and allocate structures */
1188 for (i = 0; i < nsect; i++)
1189 if (sechdrs[i].sh_flags & SHF_ALLOC)
1191 size[0] = ALIGN(sizeof(*sect_attrs)
1192 + nloaded * sizeof(sect_attrs->attrs[0]),
1193 sizeof(sect_attrs->grp.attrs[0]));
1194 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1195 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1196 if (sect_attrs == NULL)
1199 /* Setup section attributes. */
1200 sect_attrs->grp.name = "sections";
1201 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1203 sect_attrs->nsections = 0;
1204 sattr = §_attrs->attrs[0];
1205 gattr = §_attrs->grp.attrs[0];
1206 for (i = 0; i < nsect; i++) {
1207 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1209 sattr->address = sechdrs[i].sh_addr;
1210 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1212 if (sattr->name == NULL)
1214 sect_attrs->nsections++;
1215 sattr->mattr.show = module_sect_show;
1216 sattr->mattr.store = NULL;
1217 sattr->mattr.attr.name = sattr->name;
1218 sattr->mattr.attr.mode = S_IRUGO;
1219 *(gattr++) = &(sattr++)->mattr.attr;
1223 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1226 mod->sect_attrs = sect_attrs;
1229 free_sect_attrs(sect_attrs);
1232 static void remove_sect_attrs(struct module *mod)
1234 if (mod->sect_attrs) {
1235 sysfs_remove_group(&mod->mkobj.kobj,
1236 &mod->sect_attrs->grp);
1237 /* We are positive that no one is using any sect attrs
1238 * at this point. Deallocate immediately. */
1239 free_sect_attrs(mod->sect_attrs);
1240 mod->sect_attrs = NULL;
1245 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1248 struct module_notes_attrs {
1249 struct kobject *dir;
1251 struct bin_attribute attrs[0];
1254 static ssize_t module_notes_read(struct kobject *kobj,
1255 struct bin_attribute *bin_attr,
1256 char *buf, loff_t pos, size_t count)
1259 * The caller checked the pos and count against our size.
1261 memcpy(buf, bin_attr->private + pos, count);
1265 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1268 if (notes_attrs->dir) {
1270 sysfs_remove_bin_file(notes_attrs->dir,
1271 ¬es_attrs->attrs[i]);
1272 kobject_put(notes_attrs->dir);
1277 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1278 char *secstrings, Elf_Shdr *sechdrs)
1280 unsigned int notes, loaded, i;
1281 struct module_notes_attrs *notes_attrs;
1282 struct bin_attribute *nattr;
1284 /* failed to create section attributes, so can't create notes */
1285 if (!mod->sect_attrs)
1288 /* Count notes sections and allocate structures. */
1290 for (i = 0; i < nsect; i++)
1291 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1292 (sechdrs[i].sh_type == SHT_NOTE))
1298 notes_attrs = kzalloc(sizeof(*notes_attrs)
1299 + notes * sizeof(notes_attrs->attrs[0]),
1301 if (notes_attrs == NULL)
1304 notes_attrs->notes = notes;
1305 nattr = ¬es_attrs->attrs[0];
1306 for (loaded = i = 0; i < nsect; ++i) {
1307 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1309 if (sechdrs[i].sh_type == SHT_NOTE) {
1310 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1311 nattr->attr.mode = S_IRUGO;
1312 nattr->size = sechdrs[i].sh_size;
1313 nattr->private = (void *) sechdrs[i].sh_addr;
1314 nattr->read = module_notes_read;
1320 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1321 if (!notes_attrs->dir)
1324 for (i = 0; i < notes; ++i)
1325 if (sysfs_create_bin_file(notes_attrs->dir,
1326 ¬es_attrs->attrs[i]))
1329 mod->notes_attrs = notes_attrs;
1333 free_notes_attrs(notes_attrs, i);
1336 static void remove_notes_attrs(struct module *mod)
1338 if (mod->notes_attrs)
1339 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1344 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1345 char *sectstrings, Elf_Shdr *sechdrs)
1349 static inline void remove_sect_attrs(struct module *mod)
1353 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1354 char *sectstrings, Elf_Shdr *sechdrs)
1358 static inline void remove_notes_attrs(struct module *mod)
1364 int module_add_modinfo_attrs(struct module *mod)
1366 struct module_attribute *attr;
1367 struct module_attribute *temp_attr;
1371 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1372 (ARRAY_SIZE(modinfo_attrs) + 1)),
1374 if (!mod->modinfo_attrs)
1377 temp_attr = mod->modinfo_attrs;
1378 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1380 (attr->test && attr->test(mod))) {
1381 memcpy(temp_attr, attr, sizeof(*temp_attr));
1382 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1389 void module_remove_modinfo_attrs(struct module *mod)
1391 struct module_attribute *attr;
1394 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1395 /* pick a field to test for end of list */
1396 if (!attr->attr.name)
1398 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1402 kfree(mod->modinfo_attrs);
1405 int mod_sysfs_init(struct module *mod)
1408 struct kobject *kobj;
1410 if (!module_sysfs_initialized) {
1411 printk(KERN_ERR "%s: module sysfs not initialized\n",
1417 kobj = kset_find_obj(module_kset, mod->name);
1419 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1425 mod->mkobj.mod = mod;
1427 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1428 mod->mkobj.kobj.kset = module_kset;
1429 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1432 kobject_put(&mod->mkobj.kobj);
1434 /* delay uevent until full sysfs population */
1439 int mod_sysfs_setup(struct module *mod,
1440 struct kernel_param *kparam,
1441 unsigned int num_params)
1445 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1446 if (!mod->holders_dir) {
1451 err = module_param_sysfs_setup(mod, kparam, num_params);
1453 goto out_unreg_holders;
1455 err = module_add_modinfo_attrs(mod);
1457 goto out_unreg_param;
1459 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1463 module_param_sysfs_remove(mod);
1465 kobject_put(mod->holders_dir);
1467 kobject_put(&mod->mkobj.kobj);
1471 static void mod_sysfs_fini(struct module *mod)
1473 kobject_put(&mod->mkobj.kobj);
1476 #else /* CONFIG_SYSFS */
1478 static void mod_sysfs_fini(struct module *mod)
1482 #endif /* CONFIG_SYSFS */
1484 static void mod_kobject_remove(struct module *mod)
1486 module_remove_modinfo_attrs(mod);
1487 module_param_sysfs_remove(mod);
1488 kobject_put(mod->mkobj.drivers_dir);
1489 kobject_put(mod->holders_dir);
1490 mod_sysfs_fini(mod);
1494 * unlink the module with the whole machine is stopped with interrupts off
1495 * - this defends against kallsyms not taking locks
1497 static int __unlink_module(void *_mod)
1499 struct module *mod = _mod;
1500 list_del(&mod->list);
1504 /* Free a module, remove from lists, etc (must hold module_mutex). */
1505 static void free_module(struct module *mod)
1507 trace_module_free(mod);
1509 /* Delete from various lists */
1510 stop_machine(__unlink_module, mod, NULL);
1511 remove_notes_attrs(mod);
1512 remove_sect_attrs(mod);
1513 mod_kobject_remove(mod);
1515 /* Arch-specific cleanup. */
1516 module_arch_cleanup(mod);
1518 /* Module unload stuff */
1519 module_unload_free(mod);
1521 /* Free any allocated parameters. */
1522 destroy_params(mod->kp, mod->num_kp);
1524 /* This may be NULL, but that's OK */
1525 module_free(mod, mod->module_init);
1528 percpu_modfree(mod->percpu);
1529 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1531 percpu_modfree(mod->refptr);
1533 /* Free lock-classes: */
1534 lockdep_free_key_range(mod->module_core, mod->core_size);
1536 /* Finally, free the core (containing the module structure) */
1537 module_free(mod, mod->module_core);
1540 void *__symbol_get(const char *symbol)
1542 struct module *owner;
1543 const struct kernel_symbol *sym;
1546 sym = find_symbol(symbol, &owner, NULL, true, true);
1547 if (sym && strong_try_module_get(owner))
1551 return sym ? (void *)sym->value : NULL;
1553 EXPORT_SYMBOL_GPL(__symbol_get);
1556 * Ensure that an exported symbol [global namespace] does not already exist
1557 * in the kernel or in some other module's exported symbol table.
1559 static int verify_export_symbols(struct module *mod)
1562 struct module *owner;
1563 const struct kernel_symbol *s;
1565 const struct kernel_symbol *sym;
1568 { mod->syms, mod->num_syms },
1569 { mod->gpl_syms, mod->num_gpl_syms },
1570 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1571 #ifdef CONFIG_UNUSED_SYMBOLS
1572 { mod->unused_syms, mod->num_unused_syms },
1573 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1577 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1578 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1579 if (find_symbol(s->name, &owner, NULL, true, false)) {
1581 "%s: exports duplicate symbol %s"
1583 mod->name, s->name, module_name(owner));
1591 /* Change all symbols so that st_value encodes the pointer directly. */
1592 static int simplify_symbols(Elf_Shdr *sechdrs,
1593 unsigned int symindex,
1595 unsigned int versindex,
1596 unsigned int pcpuindex,
1599 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1600 unsigned long secbase;
1601 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1603 const struct kernel_symbol *ksym;
1605 for (i = 1; i < n; i++) {
1606 switch (sym[i].st_shndx) {
1608 /* We compiled with -fno-common. These are not
1609 supposed to happen. */
1610 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1611 printk("%s: please compile with -fno-common\n",
1617 /* Don't need to do anything */
1618 DEBUGP("Absolute symbol: 0x%08lx\n",
1619 (long)sym[i].st_value);
1623 ksym = resolve_symbol(sechdrs, versindex,
1624 strtab + sym[i].st_name, mod);
1625 /* Ok if resolved. */
1627 sym[i].st_value = ksym->value;
1632 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1635 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1636 mod->name, strtab + sym[i].st_name);
1641 /* Divert to percpu allocation if a percpu var. */
1642 if (sym[i].st_shndx == pcpuindex)
1643 secbase = (unsigned long)mod->percpu;
1645 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1646 sym[i].st_value += secbase;
1654 /* Additional bytes needed by arch in front of individual sections */
1655 unsigned int __weak arch_mod_section_prepend(struct module *mod,
1656 unsigned int section)
1658 /* default implementation just returns zero */
1662 /* Update size with this section: return offset. */
1663 static long get_offset(struct module *mod, unsigned int *size,
1664 Elf_Shdr *sechdr, unsigned int section)
1668 *size += arch_mod_section_prepend(mod, section);
1669 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1670 *size = ret + sechdr->sh_size;
1674 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1675 might -- code, read-only data, read-write data, small data. Tally
1676 sizes, and place the offsets into sh_entsize fields: high bit means it
1678 static void layout_sections(struct module *mod,
1679 const Elf_Ehdr *hdr,
1681 const char *secstrings)
1683 static unsigned long const masks[][2] = {
1684 /* NOTE: all executable code must be the first section
1685 * in this array; otherwise modify the text_size
1686 * finder in the two loops below */
1687 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1688 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1689 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1690 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1694 for (i = 0; i < hdr->e_shnum; i++)
1695 sechdrs[i].sh_entsize = ~0UL;
1697 DEBUGP("Core section allocation order:\n");
1698 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1699 for (i = 0; i < hdr->e_shnum; ++i) {
1700 Elf_Shdr *s = &sechdrs[i];
1702 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1703 || (s->sh_flags & masks[m][1])
1704 || s->sh_entsize != ~0UL
1705 || strstarts(secstrings + s->sh_name, ".init"))
1707 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1708 DEBUGP("\t%s\n", secstrings + s->sh_name);
1711 mod->core_text_size = mod->core_size;
1714 DEBUGP("Init section allocation order:\n");
1715 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1716 for (i = 0; i < hdr->e_shnum; ++i) {
1717 Elf_Shdr *s = &sechdrs[i];
1719 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1720 || (s->sh_flags & masks[m][1])
1721 || s->sh_entsize != ~0UL
1722 || !strstarts(secstrings + s->sh_name, ".init"))
1724 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1725 | INIT_OFFSET_MASK);
1726 DEBUGP("\t%s\n", secstrings + s->sh_name);
1729 mod->init_text_size = mod->init_size;
1733 static void set_license(struct module *mod, const char *license)
1736 license = "unspecified";
1738 if (!license_is_gpl_compatible(license)) {
1739 if (!test_taint(TAINT_PROPRIETARY_MODULE))
1740 printk(KERN_WARNING "%s: module license '%s' taints "
1741 "kernel.\n", mod->name, license);
1742 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1746 /* Parse tag=value strings from .modinfo section */
1747 static char *next_string(char *string, unsigned long *secsize)
1749 /* Skip non-zero chars */
1752 if ((*secsize)-- <= 1)
1756 /* Skip any zero padding. */
1757 while (!string[0]) {
1759 if ((*secsize)-- <= 1)
1765 static char *get_modinfo(Elf_Shdr *sechdrs,
1770 unsigned int taglen = strlen(tag);
1771 unsigned long size = sechdrs[info].sh_size;
1773 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1774 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1775 return p + taglen + 1;
1780 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1781 unsigned int infoindex)
1783 struct module_attribute *attr;
1786 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1789 get_modinfo(sechdrs,
1795 #ifdef CONFIG_KALLSYMS
1797 /* lookup symbol in given range of kernel_symbols */
1798 static const struct kernel_symbol *lookup_symbol(const char *name,
1799 const struct kernel_symbol *start,
1800 const struct kernel_symbol *stop)
1802 const struct kernel_symbol *ks = start;
1803 for (; ks < stop; ks++)
1804 if (strcmp(ks->name, name) == 0)
1809 static int is_exported(const char *name, unsigned long value,
1810 const struct module *mod)
1812 const struct kernel_symbol *ks;
1814 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
1816 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1817 return ks != NULL && ks->value == value;
1821 static char elf_type(const Elf_Sym *sym,
1823 const char *secstrings,
1826 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1827 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1832 if (sym->st_shndx == SHN_UNDEF)
1834 if (sym->st_shndx == SHN_ABS)
1836 if (sym->st_shndx >= SHN_LORESERVE)
1838 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1840 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1841 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1842 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1844 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1849 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1850 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1855 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
1860 static void add_kallsyms(struct module *mod,
1862 unsigned int symindex,
1863 unsigned int strindex,
1864 const char *secstrings)
1868 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1869 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1870 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1872 /* Set types up while we still have access to sections. */
1873 for (i = 0; i < mod->num_symtab; i++)
1874 mod->symtab[i].st_info
1875 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1878 static inline void add_kallsyms(struct module *mod,
1880 unsigned int symindex,
1881 unsigned int strindex,
1882 const char *secstrings)
1885 #endif /* CONFIG_KALLSYMS */
1887 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1889 #ifdef CONFIG_DYNAMIC_DEBUG
1890 if (ddebug_add_module(debug, num, debug->modname))
1891 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1896 static void *module_alloc_update_bounds(unsigned long size)
1898 void *ret = module_alloc(size);
1901 /* Update module bounds. */
1902 if ((unsigned long)ret < module_addr_min)
1903 module_addr_min = (unsigned long)ret;
1904 if ((unsigned long)ret + size > module_addr_max)
1905 module_addr_max = (unsigned long)ret + size;
1910 #ifdef CONFIG_DEBUG_KMEMLEAK
1911 static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1912 Elf_Shdr *sechdrs, char *secstrings)
1916 /* only scan the sections containing data */
1917 kmemleak_scan_area(mod->module_core, (unsigned long)mod -
1918 (unsigned long)mod->module_core,
1919 sizeof(struct module), GFP_KERNEL);
1921 for (i = 1; i < hdr->e_shnum; i++) {
1922 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1924 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1925 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1928 kmemleak_scan_area(mod->module_core, sechdrs[i].sh_addr -
1929 (unsigned long)mod->module_core,
1930 sechdrs[i].sh_size, GFP_KERNEL);
1934 static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1935 Elf_Shdr *sechdrs, char *secstrings)
1940 /* Allocate and load the module: note that size of section 0 is always
1941 zero, and we rely on this for optional sections. */
1942 static noinline struct module *load_module(void __user *umod,
1944 const char __user *uargs)
1948 char *secstrings, *args, *modmagic, *strtab = NULL;
1951 unsigned int symindex = 0;
1952 unsigned int strindex = 0;
1953 unsigned int modindex, versindex, infoindex, pcpuindex;
1956 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1957 mm_segment_t old_fs;
1959 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1961 if (len < sizeof(*hdr))
1962 return ERR_PTR(-ENOEXEC);
1964 /* Suck in entire file: we'll want most of it. */
1965 /* vmalloc barfs on "unusual" numbers. Check here */
1966 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1967 return ERR_PTR(-ENOMEM);
1969 if (copy_from_user(hdr, umod, len) != 0) {
1974 /* Sanity checks against insmoding binaries or wrong arch,
1975 weird elf version */
1976 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
1977 || hdr->e_type != ET_REL
1978 || !elf_check_arch(hdr)
1979 || hdr->e_shentsize != sizeof(*sechdrs)) {
1984 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1987 /* Convenience variables */
1988 sechdrs = (void *)hdr + hdr->e_shoff;
1989 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1990 sechdrs[0].sh_addr = 0;
1992 for (i = 1; i < hdr->e_shnum; i++) {
1993 if (sechdrs[i].sh_type != SHT_NOBITS
1994 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1997 /* Mark all sections sh_addr with their address in the
1999 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
2001 /* Internal symbols and strings. */
2002 if (sechdrs[i].sh_type == SHT_SYMTAB) {
2004 strindex = sechdrs[i].sh_link;
2005 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
2007 #ifndef CONFIG_MODULE_UNLOAD
2008 /* Don't load .exit sections */
2009 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
2010 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
2014 modindex = find_sec(hdr, sechdrs, secstrings,
2015 ".gnu.linkonce.this_module");
2017 printk(KERN_WARNING "No module found in object\n");
2021 /* This is temporary: point mod into copy of data. */
2022 mod = (void *)sechdrs[modindex].sh_addr;
2024 if (symindex == 0) {
2025 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2031 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
2032 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2033 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2035 /* Don't keep modinfo and version sections. */
2036 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2037 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2038 #ifdef CONFIG_KALLSYMS
2039 /* Keep symbol and string tables for decoding later. */
2040 sechdrs[symindex].sh_flags |= SHF_ALLOC;
2041 sechdrs[strindex].sh_flags |= SHF_ALLOC;
2044 /* Check module struct version now, before we try to use module. */
2045 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2050 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2051 /* This is allowed: modprobe --force will invalidate it. */
2053 err = try_to_force_load(mod, "bad vermagic");
2056 } else if (!same_magic(modmagic, vermagic, versindex)) {
2057 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2058 mod->name, modmagic, vermagic);
2063 staging = get_modinfo(sechdrs, infoindex, "staging");
2065 add_taint_module(mod, TAINT_CRAP);
2066 printk(KERN_WARNING "%s: module is from the staging directory,"
2067 " the quality is unknown, you have been warned.\n",
2071 /* Now copy in args */
2072 args = strndup_user(uargs, ~0UL >> 1);
2074 err = PTR_ERR(args);
2078 if (find_module(mod->name)) {
2083 mod->state = MODULE_STATE_COMING;
2085 /* Allow arches to frob section contents and sizes. */
2086 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2091 /* We have a special allocation for this section. */
2092 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
2093 sechdrs[pcpuindex].sh_addralign,
2099 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2100 mod->percpu = percpu;
2103 /* Determine total sizes, and put offsets in sh_entsize. For now
2104 this is done generically; there doesn't appear to be any
2105 special cases for the architectures. */
2106 layout_sections(mod, hdr, sechdrs, secstrings);
2108 /* Do the allocs. */
2109 ptr = module_alloc_update_bounds(mod->core_size);
2111 * The pointer to this block is stored in the module structure
2112 * which is inside the block. Just mark it as not being a
2115 kmemleak_not_leak(ptr);
2120 memset(ptr, 0, mod->core_size);
2121 mod->module_core = ptr;
2123 ptr = module_alloc_update_bounds(mod->init_size);
2125 * The pointer to this block is stored in the module structure
2126 * which is inside the block. This block doesn't need to be
2127 * scanned as it contains data and code that will be freed
2128 * after the module is initialized.
2130 kmemleak_ignore(ptr);
2131 if (!ptr && mod->init_size) {
2135 memset(ptr, 0, mod->init_size);
2136 mod->module_init = ptr;
2138 /* Transfer each section which specifies SHF_ALLOC */
2139 DEBUGP("final section addresses:\n");
2140 for (i = 0; i < hdr->e_shnum; i++) {
2143 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2146 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2147 dest = mod->module_init
2148 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2150 dest = mod->module_core + sechdrs[i].sh_entsize;
2152 if (sechdrs[i].sh_type != SHT_NOBITS)
2153 memcpy(dest, (void *)sechdrs[i].sh_addr,
2154 sechdrs[i].sh_size);
2155 /* Update sh_addr to point to copy in image. */
2156 sechdrs[i].sh_addr = (unsigned long)dest;
2157 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2159 /* Module has been moved. */
2160 mod = (void *)sechdrs[modindex].sh_addr;
2161 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2163 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2164 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2171 /* Now we've moved module, initialize linked lists, etc. */
2172 module_unload_init(mod);
2174 /* add kobject, so we can reference it. */
2175 err = mod_sysfs_init(mod);
2179 /* Set up license info based on the info section */
2180 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2183 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2184 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2185 * using GPL-only symbols it needs.
2187 if (strcmp(mod->name, "ndiswrapper") == 0)
2188 add_taint(TAINT_PROPRIETARY_MODULE);
2190 /* driverloader was caught wrongly pretending to be under GPL */
2191 if (strcmp(mod->name, "driverloader") == 0)
2192 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2194 /* Set up MODINFO_ATTR fields */
2195 setup_modinfo(mod, sechdrs, infoindex);
2197 /* Fix up syms, so that st_value is a pointer to location. */
2198 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2203 /* Now we've got everything in the final locations, we can
2204 * find optional sections. */
2205 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2206 sizeof(*mod->kp), &mod->num_kp);
2207 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2208 sizeof(*mod->syms), &mod->num_syms);
2209 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2210 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2211 sizeof(*mod->gpl_syms),
2212 &mod->num_gpl_syms);
2213 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2214 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2215 "__ksymtab_gpl_future",
2216 sizeof(*mod->gpl_future_syms),
2217 &mod->num_gpl_future_syms);
2218 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2219 "__kcrctab_gpl_future");
2221 #ifdef CONFIG_UNUSED_SYMBOLS
2222 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2224 sizeof(*mod->unused_syms),
2225 &mod->num_unused_syms);
2226 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2227 "__kcrctab_unused");
2228 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2229 "__ksymtab_unused_gpl",
2230 sizeof(*mod->unused_gpl_syms),
2231 &mod->num_unused_gpl_syms);
2232 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2233 "__kcrctab_unused_gpl");
2235 #ifdef CONFIG_CONSTRUCTORS
2236 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2237 sizeof(*mod->ctors), &mod->num_ctors);
2240 #ifdef CONFIG_TRACEPOINTS
2241 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2243 sizeof(*mod->tracepoints),
2244 &mod->num_tracepoints);
2246 #ifdef CONFIG_EVENT_TRACING
2247 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2249 sizeof(*mod->trace_events),
2250 &mod->num_trace_events);
2252 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2253 /* sechdrs[0].sh_size is always zero */
2254 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2256 sizeof(*mod->ftrace_callsites),
2257 &mod->num_ftrace_callsites);
2259 #ifdef CONFIG_MODVERSIONS
2260 if ((mod->num_syms && !mod->crcs)
2261 || (mod->num_gpl_syms && !mod->gpl_crcs)
2262 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2263 #ifdef CONFIG_UNUSED_SYMBOLS
2264 || (mod->num_unused_syms && !mod->unused_crcs)
2265 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2268 err = try_to_force_load(mod,
2269 "no versions for exported symbols");
2275 /* Now do relocations. */
2276 for (i = 1; i < hdr->e_shnum; i++) {
2277 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2278 unsigned int info = sechdrs[i].sh_info;
2280 /* Not a valid relocation section? */
2281 if (info >= hdr->e_shnum)
2284 /* Don't bother with non-allocated sections */
2285 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2288 if (sechdrs[i].sh_type == SHT_REL)
2289 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2290 else if (sechdrs[i].sh_type == SHT_RELA)
2291 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2297 /* Find duplicate symbols */
2298 err = verify_export_symbols(mod);
2302 /* Set up and sort exception table */
2303 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2304 sizeof(*mod->extable), &mod->num_exentries);
2305 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2307 /* Finally, copy percpu area over. */
2308 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2309 sechdrs[pcpuindex].sh_size);
2311 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2314 struct _ddebug *debug;
2315 unsigned int num_debug;
2317 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2318 sizeof(*debug), &num_debug);
2320 dynamic_debug_setup(debug, num_debug);
2323 err = module_finalize(hdr, sechdrs, mod);
2327 /* flush the icache in correct context */
2332 * Flush the instruction cache, since we've played with text.
2333 * Do it before processing of module parameters, so the module
2334 * can provide parameter accessor functions of its own.
2336 if (mod->module_init)
2337 flush_icache_range((unsigned long)mod->module_init,
2338 (unsigned long)mod->module_init
2340 flush_icache_range((unsigned long)mod->module_core,
2341 (unsigned long)mod->module_core + mod->core_size);
2346 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2347 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2350 /* Now sew it into the lists so we can get lockdep and oops
2351 * info during argument parsing. Noone should access us, since
2352 * strong_try_module_get() will fail.
2353 * lockdep/oops can run asynchronous, so use the RCU list insertion
2354 * function to insert in a way safe to concurrent readers.
2355 * The mutex protects against concurrent writers.
2357 list_add_rcu(&mod->list, &modules);
2359 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2363 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2366 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2367 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2369 /* Get rid of temporary copy */
2372 trace_module_load(mod);
2378 /* Unlink carefully: kallsyms could be walking list. */
2379 list_del_rcu(&mod->list);
2380 synchronize_sched();
2381 module_arch_cleanup(mod);
2383 kobject_del(&mod->mkobj.kobj);
2384 kobject_put(&mod->mkobj.kobj);
2386 module_unload_free(mod);
2387 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2389 percpu_modfree(mod->refptr);
2391 module_free(mod, mod->module_init);
2393 module_free(mod, mod->module_core);
2394 /* mod will be freed with core. Don't access it beyond this line! */
2397 percpu_modfree(percpu);
2402 return ERR_PTR(err);
2405 printk(KERN_ERR "Module len %lu truncated\n", len);
2410 /* Call module constructors. */
2411 static void do_mod_ctors(struct module *mod)
2413 #ifdef CONFIG_CONSTRUCTORS
2416 for (i = 0; i < mod->num_ctors; i++)
2421 /* This is where the real work happens */
2422 SYSCALL_DEFINE3(init_module, void __user *, umod,
2423 unsigned long, len, const char __user *, uargs)
2428 /* Must have permission */
2429 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2432 /* Only one module load at a time, please */
2433 if (mutex_lock_interruptible(&module_mutex) != 0)
2436 /* Do all the hard work */
2437 mod = load_module(umod, len, uargs);
2439 mutex_unlock(&module_mutex);
2440 return PTR_ERR(mod);
2443 /* Drop lock so they can recurse */
2444 mutex_unlock(&module_mutex);
2446 blocking_notifier_call_chain(&module_notify_list,
2447 MODULE_STATE_COMING, mod);
2450 /* Start the module */
2451 if (mod->init != NULL)
2452 ret = do_one_initcall(mod->init);
2454 /* Init routine failed: abort. Try to protect us from
2455 buggy refcounters. */
2456 mod->state = MODULE_STATE_GOING;
2457 synchronize_sched();
2459 blocking_notifier_call_chain(&module_notify_list,
2460 MODULE_STATE_GOING, mod);
2461 mutex_lock(&module_mutex);
2463 mutex_unlock(&module_mutex);
2464 wake_up(&module_wq);
2469 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2470 "%s: loading module anyway...\n",
2471 __func__, mod->name, ret,
2476 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2477 mod->state = MODULE_STATE_LIVE;
2478 wake_up(&module_wq);
2479 blocking_notifier_call_chain(&module_notify_list,
2480 MODULE_STATE_LIVE, mod);
2482 /* We need to finish all async code before the module init sequence is done */
2483 async_synchronize_full();
2485 mutex_lock(&module_mutex);
2486 /* Drop initial reference. */
2488 trim_init_extable(mod);
2489 module_free(mod, mod->module_init);
2490 mod->module_init = NULL;
2492 mod->init_text_size = 0;
2493 mutex_unlock(&module_mutex);
2498 static inline int within(unsigned long addr, void *start, unsigned long size)
2500 return ((void *)addr >= start && (void *)addr < start + size);
2503 #ifdef CONFIG_KALLSYMS
2505 * This ignores the intensely annoying "mapping symbols" found
2506 * in ARM ELF files: $a, $t and $d.
2508 static inline int is_arm_mapping_symbol(const char *str)
2510 return str[0] == '$' && strchr("atd", str[1])
2511 && (str[2] == '\0' || str[2] == '.');
2514 static const char *get_ksymbol(struct module *mod,
2516 unsigned long *size,
2517 unsigned long *offset)
2519 unsigned int i, best = 0;
2520 unsigned long nextval;
2522 /* At worse, next value is at end of module */
2523 if (within_module_init(addr, mod))
2524 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2526 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2528 /* Scan for closest preceeding symbol, and next symbol. (ELF
2529 starts real symbols at 1). */
2530 for (i = 1; i < mod->num_symtab; i++) {
2531 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2534 /* We ignore unnamed symbols: they're uninformative
2535 * and inserted at a whim. */
2536 if (mod->symtab[i].st_value <= addr
2537 && mod->symtab[i].st_value > mod->symtab[best].st_value
2538 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2539 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2541 if (mod->symtab[i].st_value > addr
2542 && mod->symtab[i].st_value < nextval
2543 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2544 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2545 nextval = mod->symtab[i].st_value;
2552 *size = nextval - mod->symtab[best].st_value;
2554 *offset = addr - mod->symtab[best].st_value;
2555 return mod->strtab + mod->symtab[best].st_name;
2558 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2559 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2560 const char *module_address_lookup(unsigned long addr,
2561 unsigned long *size,
2562 unsigned long *offset,
2567 const char *ret = NULL;
2570 list_for_each_entry_rcu(mod, &modules, list) {
2571 if (within_module_init(addr, mod) ||
2572 within_module_core(addr, mod)) {
2574 *modname = mod->name;
2575 ret = get_ksymbol(mod, addr, size, offset);
2579 /* Make a copy in here where it's safe */
2581 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2588 int lookup_module_symbol_name(unsigned long addr, char *symname)
2593 list_for_each_entry_rcu(mod, &modules, list) {
2594 if (within_module_init(addr, mod) ||
2595 within_module_core(addr, mod)) {
2598 sym = get_ksymbol(mod, addr, NULL, NULL);
2601 strlcpy(symname, sym, KSYM_NAME_LEN);
2611 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2612 unsigned long *offset, char *modname, char *name)
2617 list_for_each_entry_rcu(mod, &modules, list) {
2618 if (within_module_init(addr, mod) ||
2619 within_module_core(addr, mod)) {
2622 sym = get_ksymbol(mod, addr, size, offset);
2626 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2628 strlcpy(name, sym, KSYM_NAME_LEN);
2638 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2639 char *name, char *module_name, int *exported)
2644 list_for_each_entry_rcu(mod, &modules, list) {
2645 if (symnum < mod->num_symtab) {
2646 *value = mod->symtab[symnum].st_value;
2647 *type = mod->symtab[symnum].st_info;
2648 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2650 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2651 *exported = is_exported(name, *value, mod);
2655 symnum -= mod->num_symtab;
2661 static unsigned long mod_find_symname(struct module *mod, const char *name)
2665 for (i = 0; i < mod->num_symtab; i++)
2666 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2667 mod->symtab[i].st_info != 'U')
2668 return mod->symtab[i].st_value;
2672 /* Look for this name: can be of form module:name. */
2673 unsigned long module_kallsyms_lookup_name(const char *name)
2677 unsigned long ret = 0;
2679 /* Don't lock: we're in enough trouble already. */
2681 if ((colon = strchr(name, ':')) != NULL) {
2683 if ((mod = find_module(name)) != NULL)
2684 ret = mod_find_symname(mod, colon+1);
2687 list_for_each_entry_rcu(mod, &modules, list)
2688 if ((ret = mod_find_symname(mod, name)) != 0)
2695 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2696 struct module *, unsigned long),
2703 list_for_each_entry(mod, &modules, list) {
2704 for (i = 0; i < mod->num_symtab; i++) {
2705 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2706 mod, mod->symtab[i].st_value);
2713 #endif /* CONFIG_KALLSYMS */
2715 static char *module_flags(struct module *mod, char *buf)
2720 mod->state == MODULE_STATE_GOING ||
2721 mod->state == MODULE_STATE_COMING) {
2723 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
2725 if (mod->taints & (1 << TAINT_FORCED_MODULE))
2727 if (mod->taints & (1 << TAINT_CRAP))
2730 * TAINT_FORCED_RMMOD: could be added.
2731 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2735 /* Show a - for module-is-being-unloaded */
2736 if (mod->state == MODULE_STATE_GOING)
2738 /* Show a + for module-is-being-loaded */
2739 if (mod->state == MODULE_STATE_COMING)
2748 #ifdef CONFIG_PROC_FS
2749 /* Called by the /proc file system to return a list of modules. */
2750 static void *m_start(struct seq_file *m, loff_t *pos)
2752 mutex_lock(&module_mutex);
2753 return seq_list_start(&modules, *pos);
2756 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2758 return seq_list_next(p, &modules, pos);
2761 static void m_stop(struct seq_file *m, void *p)
2763 mutex_unlock(&module_mutex);
2766 static int m_show(struct seq_file *m, void *p)
2768 struct module *mod = list_entry(p, struct module, list);
2771 seq_printf(m, "%s %u",
2772 mod->name, mod->init_size + mod->core_size);
2773 print_unload_info(m, mod);
2775 /* Informative for users. */
2776 seq_printf(m, " %s",
2777 mod->state == MODULE_STATE_GOING ? "Unloading":
2778 mod->state == MODULE_STATE_COMING ? "Loading":
2780 /* Used by oprofile and other similar tools. */
2781 seq_printf(m, " 0x%p", mod->module_core);
2785 seq_printf(m, " %s", module_flags(mod, buf));
2787 seq_printf(m, "\n");
2791 /* Format: modulename size refcount deps address
2793 Where refcount is a number or -, and deps is a comma-separated list
2796 static const struct seq_operations modules_op = {
2803 static int modules_open(struct inode *inode, struct file *file)
2805 return seq_open(file, &modules_op);
2808 static const struct file_operations proc_modules_operations = {
2809 .open = modules_open,
2811 .llseek = seq_lseek,
2812 .release = seq_release,
2815 static int __init proc_modules_init(void)
2817 proc_create("modules", 0, NULL, &proc_modules_operations);
2820 module_init(proc_modules_init);
2823 /* Given an address, look for it in the module exception tables. */
2824 const struct exception_table_entry *search_module_extables(unsigned long addr)
2826 const struct exception_table_entry *e = NULL;
2830 list_for_each_entry_rcu(mod, &modules, list) {
2831 if (mod->num_exentries == 0)
2834 e = search_extable(mod->extable,
2835 mod->extable + mod->num_exentries - 1,
2842 /* Now, if we found one, we are running inside it now, hence
2843 we cannot unload the module, hence no refcnt needed. */
2848 * is_module_address - is this address inside a module?
2849 * @addr: the address to check.
2851 * See is_module_text_address() if you simply want to see if the address
2852 * is code (not data).
2854 bool is_module_address(unsigned long addr)
2859 ret = __module_address(addr) != NULL;
2866 * __module_address - get the module which contains an address.
2867 * @addr: the address.
2869 * Must be called with preempt disabled or module mutex held so that
2870 * module doesn't get freed during this.
2872 struct module *__module_address(unsigned long addr)
2876 if (addr < module_addr_min || addr > module_addr_max)
2879 list_for_each_entry_rcu(mod, &modules, list)
2880 if (within_module_core(addr, mod)
2881 || within_module_init(addr, mod))
2885 EXPORT_SYMBOL_GPL(__module_address);
2888 * is_module_text_address - is this address inside module code?
2889 * @addr: the address to check.
2891 * See is_module_address() if you simply want to see if the address is
2892 * anywhere in a module. See kernel_text_address() for testing if an
2893 * address corresponds to kernel or module code.
2895 bool is_module_text_address(unsigned long addr)
2900 ret = __module_text_address(addr) != NULL;
2907 * __module_text_address - get the module whose code contains an address.
2908 * @addr: the address.
2910 * Must be called with preempt disabled or module mutex held so that
2911 * module doesn't get freed during this.
2913 struct module *__module_text_address(unsigned long addr)
2915 struct module *mod = __module_address(addr);
2917 /* Make sure it's within the text section. */
2918 if (!within(addr, mod->module_init, mod->init_text_size)
2919 && !within(addr, mod->module_core, mod->core_text_size))
2924 EXPORT_SYMBOL_GPL(__module_text_address);
2926 /* Don't grab lock, we're oopsing. */
2927 void print_modules(void)
2932 printk(KERN_DEFAULT "Modules linked in:");
2933 /* Most callers should already have preempt disabled, but make sure */
2935 list_for_each_entry_rcu(mod, &modules, list)
2936 printk(" %s%s", mod->name, module_flags(mod, buf));
2938 if (last_unloaded_module[0])
2939 printk(" [last unloaded: %s]", last_unloaded_module);
2943 #ifdef CONFIG_MODVERSIONS
2944 /* Generate the signature for all relevant module structures here.
2945 * If these change, we don't want to try to parse the module. */
2946 void module_layout(struct module *mod,
2947 struct modversion_info *ver,
2948 struct kernel_param *kp,
2949 struct kernel_symbol *ks,
2950 struct marker *marker,
2951 struct tracepoint *tp)
2954 EXPORT_SYMBOL(module_layout);
2957 #ifdef CONFIG_TRACEPOINTS
2958 void module_update_tracepoints(void)
2962 mutex_lock(&module_mutex);
2963 list_for_each_entry(mod, &modules, list)
2965 tracepoint_update_probe_range(mod->tracepoints,
2966 mod->tracepoints + mod->num_tracepoints);
2967 mutex_unlock(&module_mutex);
2971 * Returns 0 if current not found.
2972 * Returns 1 if current found.
2974 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2976 struct module *iter_mod;
2979 mutex_lock(&module_mutex);
2980 list_for_each_entry(iter_mod, &modules, list) {
2981 if (!iter_mod->taints) {
2983 * Sorted module list
2985 if (iter_mod < iter->module)
2987 else if (iter_mod > iter->module)
2988 iter->tracepoint = NULL;
2989 found = tracepoint_get_iter_range(&iter->tracepoint,
2990 iter_mod->tracepoints,
2991 iter_mod->tracepoints
2992 + iter_mod->num_tracepoints);
2994 iter->module = iter_mod;
2999 mutex_unlock(&module_mutex);