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/init.h>
22 #include <linux/kallsyms.h>
24 #include <linux/sysfs.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/elf.h>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <linux/syscalls.h>
32 #include <linux/fcntl.h>
33 #include <linux/rcupdate.h>
34 #include <linux/capability.h>
35 #include <linux/cpu.h>
36 #include <linux/moduleparam.h>
37 #include <linux/errno.h>
38 #include <linux/err.h>
39 #include <linux/vermagic.h>
40 #include <linux/notifier.h>
41 #include <linux/sched.h>
42 #include <linux/stop_machine.h>
43 #include <linux/device.h>
44 #include <linux/string.h>
45 #include <linux/mutex.h>
46 #include <linux/rculist.h>
47 #include <asm/uaccess.h>
48 #include <asm/cacheflush.h>
49 #include <linux/license.h>
50 #include <asm/sections.h>
51 #include <linux/tracepoint.h>
52 #include <linux/ftrace.h>
53 #include <linux/async.h>
54 #include <linux/percpu.h>
59 #define DEBUGP(fmt , a...)
62 #ifndef ARCH_SHF_SMALL
63 #define ARCH_SHF_SMALL 0
66 /* If this is set, the section belongs in the init part of the module */
67 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
69 /* List of modules, protected by module_mutex or preempt_disable
70 * (delete uses stop_machine/add uses RCU list operations). */
71 static DEFINE_MUTEX(module_mutex);
72 static LIST_HEAD(modules);
74 /* Waiting for a module to finish initializing? */
75 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
77 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
79 /* Bounds of module allocation, for speeding __module_address */
80 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
82 int register_module_notifier(struct notifier_block * nb)
84 return blocking_notifier_chain_register(&module_notify_list, nb);
86 EXPORT_SYMBOL(register_module_notifier);
88 int unregister_module_notifier(struct notifier_block * nb)
90 return blocking_notifier_chain_unregister(&module_notify_list, nb);
92 EXPORT_SYMBOL(unregister_module_notifier);
94 /* We require a truly strong try_module_get(): 0 means failure due to
95 ongoing or failed initialization etc. */
96 static inline int strong_try_module_get(struct module *mod)
98 if (mod && mod->state == MODULE_STATE_COMING)
100 if (try_module_get(mod))
106 static inline void add_taint_module(struct module *mod, unsigned flag)
109 mod->taints |= (1U << flag);
113 * A thread that wants to hold a reference to a module only while it
114 * is running can call this to safely exit. nfsd and lockd use this.
116 void __module_put_and_exit(struct module *mod, long code)
121 EXPORT_SYMBOL(__module_put_and_exit);
123 /* Find a module section: 0 means not found. */
124 static unsigned int find_sec(Elf_Ehdr *hdr,
126 const char *secstrings,
131 for (i = 1; i < hdr->e_shnum; i++)
132 /* Alloc bit cleared means "ignore it." */
133 if ((sechdrs[i].sh_flags & SHF_ALLOC)
134 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
139 /* Find a module section, or NULL. */
140 static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
141 const char *secstrings, const char *name)
143 /* Section 0 has sh_addr 0. */
144 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
147 /* Find a module section, or NULL. Fill in number of "objects" in section. */
148 static void *section_objs(Elf_Ehdr *hdr,
150 const char *secstrings,
155 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
157 /* Section 0 has sh_addr 0 and sh_size 0. */
158 *num = sechdrs[sec].sh_size / object_size;
159 return (void *)sechdrs[sec].sh_addr;
162 /* Provided by the linker */
163 extern const struct kernel_symbol __start___ksymtab[];
164 extern const struct kernel_symbol __stop___ksymtab[];
165 extern const struct kernel_symbol __start___ksymtab_gpl[];
166 extern const struct kernel_symbol __stop___ksymtab_gpl[];
167 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
168 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
169 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
170 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
171 extern const unsigned long __start___kcrctab[];
172 extern const unsigned long __start___kcrctab_gpl[];
173 extern const unsigned long __start___kcrctab_gpl_future[];
174 #ifdef CONFIG_UNUSED_SYMBOLS
175 extern const struct kernel_symbol __start___ksymtab_unused[];
176 extern const struct kernel_symbol __stop___ksymtab_unused[];
177 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
178 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
179 extern const unsigned long __start___kcrctab_unused[];
180 extern const unsigned long __start___kcrctab_unused_gpl[];
183 #ifndef CONFIG_MODVERSIONS
184 #define symversion(base, idx) NULL
186 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
190 const struct kernel_symbol *start, *stop;
191 const unsigned long *crcs;
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 static bool each_symbol(bool (*fn)(const struct symsearch *arr,
221 struct module *owner,
222 unsigned int symnum, void *data),
226 const struct symsearch arr[] = {
227 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
228 NOT_GPL_ONLY, false },
229 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
230 __start___kcrctab_gpl,
232 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
233 __start___kcrctab_gpl_future,
234 WILL_BE_GPL_ONLY, false },
235 #ifdef CONFIG_UNUSED_SYMBOLS
236 { __start___ksymtab_unused, __stop___ksymtab_unused,
237 __start___kcrctab_unused,
238 NOT_GPL_ONLY, true },
239 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
240 __start___kcrctab_unused_gpl,
245 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
248 list_for_each_entry_rcu(mod, &modules, list) {
249 struct symsearch arr[] = {
250 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
251 NOT_GPL_ONLY, false },
252 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
255 { mod->gpl_future_syms,
256 mod->gpl_future_syms + mod->num_gpl_future_syms,
257 mod->gpl_future_crcs,
258 WILL_BE_GPL_ONLY, false },
259 #ifdef CONFIG_UNUSED_SYMBOLS
261 mod->unused_syms + mod->num_unused_syms,
263 NOT_GPL_ONLY, true },
264 { mod->unused_gpl_syms,
265 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
266 mod->unused_gpl_crcs,
271 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
277 struct find_symbol_arg {
284 struct module *owner;
285 const unsigned long *crc;
286 const struct kernel_symbol *sym;
289 static bool find_symbol_in_section(const struct symsearch *syms,
290 struct module *owner,
291 unsigned int symnum, void *data)
293 struct find_symbol_arg *fsa = data;
295 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
299 if (syms->licence == GPL_ONLY)
301 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
302 printk(KERN_WARNING "Symbol %s is being used "
303 "by a non-GPL module, which will not "
304 "be allowed in the future\n", fsa->name);
305 printk(KERN_WARNING "Please see the file "
306 "Documentation/feature-removal-schedule.txt "
307 "in the kernel source tree for more details.\n");
311 #ifdef CONFIG_UNUSED_SYMBOLS
312 if (syms->unused && fsa->warn) {
313 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
314 "however this module is using it.\n", fsa->name);
316 "This symbol will go away in the future.\n");
318 "Please evalute if this is the right api to use and if "
319 "it really is, submit a report the linux kernel "
320 "mailinglist together with submitting your code for "
326 fsa->crc = symversion(syms->crcs, symnum);
327 fsa->sym = &syms->start[symnum];
331 /* Find a symbol and return it, along with, (optional) crc and
332 * (optional) module which owns it */
333 static const struct kernel_symbol *find_symbol(const char *name,
334 struct module **owner,
335 const unsigned long **crc,
339 struct find_symbol_arg fsa;
345 if (each_symbol(find_symbol_in_section, &fsa)) {
353 DEBUGP("Failed to find symbol %s\n", name);
357 /* Search for module by name: must hold module_mutex. */
358 static struct module *find_module(const char *name)
362 list_for_each_entry(mod, &modules, list) {
363 if (strcmp(mod->name, name) == 0)
371 #ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
373 static void *percpu_modalloc(unsigned long size, unsigned long align,
378 if (align > PAGE_SIZE) {
379 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
380 name, align, PAGE_SIZE);
384 ptr = __alloc_reserved_percpu(size, align);
387 "Could not allocate %lu bytes percpu data\n", size);
391 static void percpu_modfree(void *freeme)
396 #else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
398 /* Number of blocks used and allocated. */
399 static unsigned int pcpu_num_used, pcpu_num_allocated;
400 /* Size of each block. -ve means used. */
401 static int *pcpu_size;
403 static int split_block(unsigned int i, unsigned short size)
405 /* Reallocation required? */
406 if (pcpu_num_used + 1 > pcpu_num_allocated) {
409 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
414 pcpu_num_allocated *= 2;
418 /* Insert a new subblock */
419 memmove(&pcpu_size[i+1], &pcpu_size[i],
420 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
423 pcpu_size[i+1] -= size;
428 static inline unsigned int block_size(int val)
435 static void *percpu_modalloc(unsigned long size, unsigned long align,
442 if (align > PAGE_SIZE) {
443 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
444 name, align, PAGE_SIZE);
448 ptr = __per_cpu_start;
449 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
450 /* Extra for alignment requirement. */
451 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
452 BUG_ON(i == 0 && extra != 0);
454 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
457 /* Transfer extra to previous block. */
458 if (pcpu_size[i-1] < 0)
459 pcpu_size[i-1] -= extra;
461 pcpu_size[i-1] += extra;
462 pcpu_size[i] -= extra;
465 /* Split block if warranted */
466 if (pcpu_size[i] - size > sizeof(unsigned long))
467 if (!split_block(i, size))
471 pcpu_size[i] = -pcpu_size[i];
475 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
480 static void percpu_modfree(void *freeme)
483 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
485 /* First entry is core kernel percpu data. */
486 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
488 pcpu_size[i] = -pcpu_size[i];
495 /* Merge with previous? */
496 if (pcpu_size[i-1] >= 0) {
497 pcpu_size[i-1] += pcpu_size[i];
499 memmove(&pcpu_size[i], &pcpu_size[i+1],
500 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
503 /* Merge with next? */
504 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
505 pcpu_size[i] += pcpu_size[i+1];
507 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
508 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
512 static int percpu_modinit(void)
515 pcpu_num_allocated = 2;
516 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
518 /* Static in-kernel percpu data (used). */
519 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
521 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
522 if (pcpu_size[1] < 0) {
523 printk(KERN_ERR "No per-cpu room for modules.\n");
529 __initcall(percpu_modinit);
531 #endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
533 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
535 const char *secstrings)
537 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
540 static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
544 for_each_possible_cpu(cpu)
545 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
548 #else /* ... !CONFIG_SMP */
550 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
555 static inline void percpu_modfree(void *pcpuptr)
559 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
561 const char *secstrings)
565 static inline void percpu_modcopy(void *pcpudst, const void *src,
568 /* pcpusec should be 0, and size of that section should be 0. */
572 #endif /* CONFIG_SMP */
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 *mod, char *buffer) \
582 return sprintf(buffer, "%s\n", 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
607 /* Init the unload section of the module. */
608 static void module_unload_init(struct module *mod)
612 INIT_LIST_HEAD(&mod->modules_which_use_me);
613 for_each_possible_cpu(cpu)
614 local_set(__module_ref_addr(mod, cpu), 0);
615 /* Hold reference count during initialization. */
616 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
617 /* Backwards compatibility macros put refcount during init. */
618 mod->waiter = current;
621 /* modules using other modules */
624 struct list_head list;
625 struct module *module_which_uses;
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->modules_which_use_me, list) {
634 if (use->module_which_uses == a) {
635 DEBUGP("%s uses %s!\n", a->name, b->name);
639 DEBUGP("%s does not use %s!\n", a->name, b->name);
643 /* Module a uses b */
644 static int use_module(struct module *a, struct module *b)
646 struct module_use *use;
649 if (b == NULL || already_uses(a, b)) return 1;
651 /* If we're interrupted or time out, we fail. */
652 if (wait_event_interruptible_timeout(
653 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
655 printk("%s: gave up waiting for init of module %s.\n",
660 /* If strong_try_module_get() returned a different error, we fail. */
664 DEBUGP("Allocating new usage for %s.\n", a->name);
665 use = kmalloc(sizeof(*use), GFP_ATOMIC);
667 printk("%s: out of memory loading\n", a->name);
672 use->module_which_uses = a;
673 list_add(&use->list, &b->modules_which_use_me);
674 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
678 /* Clear the unload stuff of the module. */
679 static void module_unload_free(struct module *mod)
683 list_for_each_entry(i, &modules, list) {
684 struct module_use *use;
686 list_for_each_entry(use, &i->modules_which_use_me, list) {
687 if (use->module_which_uses == mod) {
688 DEBUGP("%s unusing %s\n", mod->name, i->name);
690 list_del(&use->list);
692 sysfs_remove_link(i->holders_dir, mod->name);
693 /* There can be at most one match. */
700 #ifdef CONFIG_MODULE_FORCE_UNLOAD
701 static inline int try_force_unload(unsigned int flags)
703 int ret = (flags & O_TRUNC);
705 add_taint(TAINT_FORCED_RMMOD);
709 static inline int try_force_unload(unsigned int flags)
713 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
722 /* Whole machine is stopped with interrupts off when this runs. */
723 static int __try_stop_module(void *_sref)
725 struct stopref *sref = _sref;
727 /* If it's not unused, quit unless we're forcing. */
728 if (module_refcount(sref->mod) != 0) {
729 if (!(*sref->forced = try_force_unload(sref->flags)))
733 /* Mark it as dying. */
734 sref->mod->state = MODULE_STATE_GOING;
738 static int try_stop_module(struct module *mod, int flags, int *forced)
740 if (flags & O_NONBLOCK) {
741 struct stopref sref = { mod, flags, forced };
743 return stop_machine(__try_stop_module, &sref, NULL);
745 /* We don't need to stop the machine for this. */
746 mod->state = MODULE_STATE_GOING;
752 unsigned int module_refcount(struct module *mod)
754 unsigned int total = 0;
757 for_each_possible_cpu(cpu)
758 total += local_read(__module_ref_addr(mod, cpu));
761 EXPORT_SYMBOL(module_refcount);
763 /* This exists whether we can unload or not */
764 static void free_module(struct module *mod);
766 static void wait_for_zero_refcount(struct module *mod)
768 /* Since we might sleep for some time, release the mutex first */
769 mutex_unlock(&module_mutex);
771 DEBUGP("Looking at refcount...\n");
772 set_current_state(TASK_UNINTERRUPTIBLE);
773 if (module_refcount(mod) == 0)
777 current->state = TASK_RUNNING;
778 mutex_lock(&module_mutex);
781 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
785 char name[MODULE_NAME_LEN];
788 if (!capable(CAP_SYS_MODULE))
791 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
793 name[MODULE_NAME_LEN-1] = '\0';
795 /* Create stop_machine threads since free_module relies on
796 * a non-failing stop_machine call. */
797 ret = stop_machine_create();
801 if (mutex_lock_interruptible(&module_mutex) != 0) {
806 mod = find_module(name);
812 if (!list_empty(&mod->modules_which_use_me)) {
813 /* Other modules depend on us: get rid of them first. */
818 /* Doing init or already dying? */
819 if (mod->state != MODULE_STATE_LIVE) {
820 /* FIXME: if (force), slam module count and wake up
822 DEBUGP("%s already dying\n", mod->name);
827 /* If it has an init func, it must have an exit func to unload */
828 if (mod->init && !mod->exit) {
829 forced = try_force_unload(flags);
831 /* This module can't be removed */
837 /* Set this up before setting mod->state */
838 mod->waiter = current;
840 /* Stop the machine so refcounts can't move and disable module. */
841 ret = try_stop_module(mod, flags, &forced);
845 /* Never wait if forced. */
846 if (!forced && module_refcount(mod) != 0)
847 wait_for_zero_refcount(mod);
849 mutex_unlock(&module_mutex);
850 /* Final destruction now noone is using it. */
851 if (mod->exit != NULL)
853 blocking_notifier_call_chain(&module_notify_list,
854 MODULE_STATE_GOING, mod);
855 async_synchronize_full();
856 mutex_lock(&module_mutex);
857 /* Store the name of the last unloaded module for diagnostic purposes */
858 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
859 ddebug_remove_module(mod->name);
863 mutex_unlock(&module_mutex);
865 stop_machine_destroy();
869 static inline void print_unload_info(struct seq_file *m, struct module *mod)
871 struct module_use *use;
872 int printed_something = 0;
874 seq_printf(m, " %u ", module_refcount(mod));
876 /* Always include a trailing , so userspace can differentiate
877 between this and the old multi-field proc format. */
878 list_for_each_entry(use, &mod->modules_which_use_me, list) {
879 printed_something = 1;
880 seq_printf(m, "%s,", use->module_which_uses->name);
883 if (mod->init != NULL && mod->exit == NULL) {
884 printed_something = 1;
885 seq_printf(m, "[permanent],");
888 if (!printed_something)
892 void __symbol_put(const char *symbol)
894 struct module *owner;
897 if (!find_symbol(symbol, &owner, NULL, true, false))
902 EXPORT_SYMBOL(__symbol_put);
904 void symbol_put_addr(void *addr)
906 struct module *modaddr;
908 if (core_kernel_text((unsigned long)addr))
911 if (!(modaddr = module_text_address((unsigned long)addr)))
915 EXPORT_SYMBOL_GPL(symbol_put_addr);
917 static ssize_t show_refcnt(struct module_attribute *mattr,
918 struct module *mod, char *buffer)
920 return sprintf(buffer, "%u\n", module_refcount(mod));
923 static struct module_attribute refcnt = {
924 .attr = { .name = "refcnt", .mode = 0444 },
928 void module_put(struct module *module)
931 unsigned int cpu = get_cpu();
932 local_dec(__module_ref_addr(module, cpu));
933 /* Maybe they're waiting for us to drop reference? */
934 if (unlikely(!module_is_live(module)))
935 wake_up_process(module->waiter);
939 EXPORT_SYMBOL(module_put);
941 #else /* !CONFIG_MODULE_UNLOAD */
942 static inline void print_unload_info(struct seq_file *m, struct module *mod)
944 /* We don't know the usage count, or what modules are using. */
945 seq_printf(m, " - -");
948 static inline void module_unload_free(struct module *mod)
952 static inline int use_module(struct module *a, struct module *b)
954 return strong_try_module_get(b) == 0;
957 static inline void module_unload_init(struct module *mod)
960 #endif /* CONFIG_MODULE_UNLOAD */
962 static ssize_t show_initstate(struct module_attribute *mattr,
963 struct module *mod, char *buffer)
965 const char *state = "unknown";
967 switch (mod->state) {
968 case MODULE_STATE_LIVE:
971 case MODULE_STATE_COMING:
974 case MODULE_STATE_GOING:
978 return sprintf(buffer, "%s\n", state);
981 static struct module_attribute initstate = {
982 .attr = { .name = "initstate", .mode = 0444 },
983 .show = show_initstate,
986 static struct module_attribute *modinfo_attrs[] = {
990 #ifdef CONFIG_MODULE_UNLOAD
996 static const char vermagic[] = VERMAGIC_STRING;
998 static int try_to_force_load(struct module *mod, const char *symname)
1000 #ifdef CONFIG_MODULE_FORCE_LOAD
1001 if (!test_taint(TAINT_FORCED_MODULE))
1002 printk("%s: no version for \"%s\" found: kernel tainted.\n",
1003 mod->name, symname);
1004 add_taint_module(mod, TAINT_FORCED_MODULE);
1011 #ifdef CONFIG_MODVERSIONS
1012 static int check_version(Elf_Shdr *sechdrs,
1013 unsigned int versindex,
1014 const char *symname,
1016 const unsigned long *crc)
1018 unsigned int i, num_versions;
1019 struct modversion_info *versions;
1021 /* Exporting module didn't supply crcs? OK, we're already tainted. */
1025 /* No versions at all? modprobe --force does this. */
1027 return try_to_force_load(mod, symname) == 0;
1029 versions = (void *) sechdrs[versindex].sh_addr;
1030 num_versions = sechdrs[versindex].sh_size
1031 / sizeof(struct modversion_info);
1033 for (i = 0; i < num_versions; i++) {
1034 if (strcmp(versions[i].name, symname) != 0)
1037 if (versions[i].crc == *crc)
1039 DEBUGP("Found checksum %lX vs module %lX\n",
1040 *crc, versions[i].crc);
1044 printk(KERN_WARNING "%s: no symbol version for %s\n",
1045 mod->name, symname);
1049 printk("%s: disagrees about version of symbol %s\n",
1050 mod->name, symname);
1054 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1055 unsigned int versindex,
1058 const unsigned long *crc;
1060 if (!find_symbol("struct_module", NULL, &crc, true, false))
1062 return check_version(sechdrs, versindex, "struct_module", mod, crc);
1065 /* First part is kernel version, which we ignore if module has crcs. */
1066 static inline int same_magic(const char *amagic, const char *bmagic,
1070 amagic += strcspn(amagic, " ");
1071 bmagic += strcspn(bmagic, " ");
1073 return strcmp(amagic, bmagic) == 0;
1076 static inline int check_version(Elf_Shdr *sechdrs,
1077 unsigned int versindex,
1078 const char *symname,
1080 const unsigned long *crc)
1085 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1086 unsigned int versindex,
1092 static inline int same_magic(const char *amagic, const char *bmagic,
1095 return strcmp(amagic, bmagic) == 0;
1097 #endif /* CONFIG_MODVERSIONS */
1099 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1100 Must be holding module_mutex. */
1101 static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1102 unsigned int versindex,
1106 struct module *owner;
1107 const struct kernel_symbol *sym;
1108 const unsigned long *crc;
1110 sym = find_symbol(name, &owner, &crc,
1111 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1112 /* use_module can fail due to OOM,
1113 or module initialization or unloading */
1115 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1116 !use_module(mod, owner))
1123 * /sys/module/foo/sections stuff
1124 * J. Corbet <corbet@lwn.net>
1126 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1127 struct module_sect_attr
1129 struct module_attribute mattr;
1131 unsigned long address;
1134 struct module_sect_attrs
1136 struct attribute_group grp;
1137 unsigned int nsections;
1138 struct module_sect_attr attrs[0];
1141 static ssize_t module_sect_show(struct module_attribute *mattr,
1142 struct module *mod, char *buf)
1144 struct module_sect_attr *sattr =
1145 container_of(mattr, struct module_sect_attr, mattr);
1146 return sprintf(buf, "0x%lx\n", sattr->address);
1149 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1151 unsigned int section;
1153 for (section = 0; section < sect_attrs->nsections; section++)
1154 kfree(sect_attrs->attrs[section].name);
1158 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1159 char *secstrings, Elf_Shdr *sechdrs)
1161 unsigned int nloaded = 0, i, size[2];
1162 struct module_sect_attrs *sect_attrs;
1163 struct module_sect_attr *sattr;
1164 struct attribute **gattr;
1166 /* Count loaded sections and allocate structures */
1167 for (i = 0; i < nsect; i++)
1168 if (sechdrs[i].sh_flags & SHF_ALLOC)
1170 size[0] = ALIGN(sizeof(*sect_attrs)
1171 + nloaded * sizeof(sect_attrs->attrs[0]),
1172 sizeof(sect_attrs->grp.attrs[0]));
1173 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1174 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1175 if (sect_attrs == NULL)
1178 /* Setup section attributes. */
1179 sect_attrs->grp.name = "sections";
1180 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1182 sect_attrs->nsections = 0;
1183 sattr = §_attrs->attrs[0];
1184 gattr = §_attrs->grp.attrs[0];
1185 for (i = 0; i < nsect; i++) {
1186 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1188 sattr->address = sechdrs[i].sh_addr;
1189 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1191 if (sattr->name == NULL)
1193 sect_attrs->nsections++;
1194 sattr->mattr.show = module_sect_show;
1195 sattr->mattr.store = NULL;
1196 sattr->mattr.attr.name = sattr->name;
1197 sattr->mattr.attr.mode = S_IRUGO;
1198 *(gattr++) = &(sattr++)->mattr.attr;
1202 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1205 mod->sect_attrs = sect_attrs;
1208 free_sect_attrs(sect_attrs);
1211 static void remove_sect_attrs(struct module *mod)
1213 if (mod->sect_attrs) {
1214 sysfs_remove_group(&mod->mkobj.kobj,
1215 &mod->sect_attrs->grp);
1216 /* We are positive that no one is using any sect attrs
1217 * at this point. Deallocate immediately. */
1218 free_sect_attrs(mod->sect_attrs);
1219 mod->sect_attrs = NULL;
1224 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1227 struct module_notes_attrs {
1228 struct kobject *dir;
1230 struct bin_attribute attrs[0];
1233 static ssize_t module_notes_read(struct kobject *kobj,
1234 struct bin_attribute *bin_attr,
1235 char *buf, loff_t pos, size_t count)
1238 * The caller checked the pos and count against our size.
1240 memcpy(buf, bin_attr->private + pos, count);
1244 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1247 if (notes_attrs->dir) {
1249 sysfs_remove_bin_file(notes_attrs->dir,
1250 ¬es_attrs->attrs[i]);
1251 kobject_put(notes_attrs->dir);
1256 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1257 char *secstrings, Elf_Shdr *sechdrs)
1259 unsigned int notes, loaded, i;
1260 struct module_notes_attrs *notes_attrs;
1261 struct bin_attribute *nattr;
1263 /* Count notes sections and allocate structures. */
1265 for (i = 0; i < nsect; i++)
1266 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1267 (sechdrs[i].sh_type == SHT_NOTE))
1273 notes_attrs = kzalloc(sizeof(*notes_attrs)
1274 + notes * sizeof(notes_attrs->attrs[0]),
1276 if (notes_attrs == NULL)
1279 notes_attrs->notes = notes;
1280 nattr = ¬es_attrs->attrs[0];
1281 for (loaded = i = 0; i < nsect; ++i) {
1282 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1284 if (sechdrs[i].sh_type == SHT_NOTE) {
1285 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1286 nattr->attr.mode = S_IRUGO;
1287 nattr->size = sechdrs[i].sh_size;
1288 nattr->private = (void *) sechdrs[i].sh_addr;
1289 nattr->read = module_notes_read;
1295 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1296 if (!notes_attrs->dir)
1299 for (i = 0; i < notes; ++i)
1300 if (sysfs_create_bin_file(notes_attrs->dir,
1301 ¬es_attrs->attrs[i]))
1304 mod->notes_attrs = notes_attrs;
1308 free_notes_attrs(notes_attrs, i);
1311 static void remove_notes_attrs(struct module *mod)
1313 if (mod->notes_attrs)
1314 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1319 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1320 char *sectstrings, Elf_Shdr *sechdrs)
1324 static inline void remove_sect_attrs(struct module *mod)
1328 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1329 char *sectstrings, Elf_Shdr *sechdrs)
1333 static inline void remove_notes_attrs(struct module *mod)
1339 int module_add_modinfo_attrs(struct module *mod)
1341 struct module_attribute *attr;
1342 struct module_attribute *temp_attr;
1346 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1347 (ARRAY_SIZE(modinfo_attrs) + 1)),
1349 if (!mod->modinfo_attrs)
1352 temp_attr = mod->modinfo_attrs;
1353 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1355 (attr->test && attr->test(mod))) {
1356 memcpy(temp_attr, attr, sizeof(*temp_attr));
1357 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1364 void module_remove_modinfo_attrs(struct module *mod)
1366 struct module_attribute *attr;
1369 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1370 /* pick a field to test for end of list */
1371 if (!attr->attr.name)
1373 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1377 kfree(mod->modinfo_attrs);
1380 int mod_sysfs_init(struct module *mod)
1383 struct kobject *kobj;
1385 if (!module_sysfs_initialized) {
1386 printk(KERN_ERR "%s: module sysfs not initialized\n",
1392 kobj = kset_find_obj(module_kset, mod->name);
1394 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1400 mod->mkobj.mod = mod;
1402 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1403 mod->mkobj.kobj.kset = module_kset;
1404 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1407 kobject_put(&mod->mkobj.kobj);
1409 /* delay uevent until full sysfs population */
1414 int mod_sysfs_setup(struct module *mod,
1415 struct kernel_param *kparam,
1416 unsigned int num_params)
1420 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1421 if (!mod->holders_dir) {
1426 err = module_param_sysfs_setup(mod, kparam, num_params);
1428 goto out_unreg_holders;
1430 err = module_add_modinfo_attrs(mod);
1432 goto out_unreg_param;
1434 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1438 module_param_sysfs_remove(mod);
1440 kobject_put(mod->holders_dir);
1442 kobject_put(&mod->mkobj.kobj);
1446 static void mod_sysfs_fini(struct module *mod)
1448 kobject_put(&mod->mkobj.kobj);
1451 #else /* CONFIG_SYSFS */
1453 static void mod_sysfs_fini(struct module *mod)
1457 #endif /* CONFIG_SYSFS */
1459 static void mod_kobject_remove(struct module *mod)
1461 module_remove_modinfo_attrs(mod);
1462 module_param_sysfs_remove(mod);
1463 kobject_put(mod->mkobj.drivers_dir);
1464 kobject_put(mod->holders_dir);
1465 mod_sysfs_fini(mod);
1469 * unlink the module with the whole machine is stopped with interrupts off
1470 * - this defends against kallsyms not taking locks
1472 static int __unlink_module(void *_mod)
1474 struct module *mod = _mod;
1475 list_del(&mod->list);
1479 /* Free a module, remove from lists, etc (must hold module_mutex). */
1480 static void free_module(struct module *mod)
1482 /* Delete from various lists */
1483 stop_machine(__unlink_module, mod, NULL);
1484 remove_notes_attrs(mod);
1485 remove_sect_attrs(mod);
1486 mod_kobject_remove(mod);
1488 /* Arch-specific cleanup. */
1489 module_arch_cleanup(mod);
1491 /* Module unload stuff */
1492 module_unload_free(mod);
1494 /* Free any allocated parameters. */
1495 destroy_params(mod->kp, mod->num_kp);
1497 /* release any pointers to mcount in this module */
1498 ftrace_release(mod->module_core, mod->core_size);
1500 /* This may be NULL, but that's OK */
1501 module_free(mod, mod->module_init);
1504 percpu_modfree(mod->percpu);
1505 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1507 percpu_modfree(mod->refptr);
1509 /* Free lock-classes: */
1510 lockdep_free_key_range(mod->module_core, mod->core_size);
1512 /* Finally, free the core (containing the module structure) */
1513 module_free(mod, mod->module_core);
1516 void *__symbol_get(const char *symbol)
1518 struct module *owner;
1519 const struct kernel_symbol *sym;
1522 sym = find_symbol(symbol, &owner, NULL, true, true);
1523 if (sym && strong_try_module_get(owner))
1527 return sym ? (void *)sym->value : NULL;
1529 EXPORT_SYMBOL_GPL(__symbol_get);
1532 * Ensure that an exported symbol [global namespace] does not already exist
1533 * in the kernel or in some other module's exported symbol table.
1535 static int verify_export_symbols(struct module *mod)
1538 struct module *owner;
1539 const struct kernel_symbol *s;
1541 const struct kernel_symbol *sym;
1544 { mod->syms, mod->num_syms },
1545 { mod->gpl_syms, mod->num_gpl_syms },
1546 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1547 #ifdef CONFIG_UNUSED_SYMBOLS
1548 { mod->unused_syms, mod->num_unused_syms },
1549 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1553 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1554 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1555 if (find_symbol(s->name, &owner, NULL, true, false)) {
1557 "%s: exports duplicate symbol %s"
1559 mod->name, s->name, module_name(owner));
1567 /* Change all symbols so that st_value encodes the pointer directly. */
1568 static int simplify_symbols(Elf_Shdr *sechdrs,
1569 unsigned int symindex,
1571 unsigned int versindex,
1572 unsigned int pcpuindex,
1575 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1576 unsigned long secbase;
1577 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1579 const struct kernel_symbol *ksym;
1581 for (i = 1; i < n; i++) {
1582 switch (sym[i].st_shndx) {
1584 /* We compiled with -fno-common. These are not
1585 supposed to happen. */
1586 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1587 printk("%s: please compile with -fno-common\n",
1593 /* Don't need to do anything */
1594 DEBUGP("Absolute symbol: 0x%08lx\n",
1595 (long)sym[i].st_value);
1599 ksym = resolve_symbol(sechdrs, versindex,
1600 strtab + sym[i].st_name, mod);
1601 /* Ok if resolved. */
1603 sym[i].st_value = ksym->value;
1608 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1611 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1612 mod->name, strtab + sym[i].st_name);
1617 /* Divert to percpu allocation if a percpu var. */
1618 if (sym[i].st_shndx == pcpuindex)
1619 secbase = (unsigned long)mod->percpu;
1621 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1622 sym[i].st_value += secbase;
1630 /* Additional bytes needed by arch in front of individual sections */
1631 unsigned int __weak arch_mod_section_prepend(struct module *mod,
1632 unsigned int section)
1634 /* default implementation just returns zero */
1638 /* Update size with this section: return offset. */
1639 static long get_offset(struct module *mod, unsigned int *size,
1640 Elf_Shdr *sechdr, unsigned int section)
1644 *size += arch_mod_section_prepend(mod, section);
1645 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1646 *size = ret + sechdr->sh_size;
1650 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1651 might -- code, read-only data, read-write data, small data. Tally
1652 sizes, and place the offsets into sh_entsize fields: high bit means it
1654 static void layout_sections(struct module *mod,
1655 const Elf_Ehdr *hdr,
1657 const char *secstrings)
1659 static unsigned long const masks[][2] = {
1660 /* NOTE: all executable code must be the first section
1661 * in this array; otherwise modify the text_size
1662 * finder in the two loops below */
1663 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1664 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1665 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1666 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1670 for (i = 0; i < hdr->e_shnum; i++)
1671 sechdrs[i].sh_entsize = ~0UL;
1673 DEBUGP("Core section allocation order:\n");
1674 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1675 for (i = 0; i < hdr->e_shnum; ++i) {
1676 Elf_Shdr *s = &sechdrs[i];
1678 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1679 || (s->sh_flags & masks[m][1])
1680 || s->sh_entsize != ~0UL
1681 || strncmp(secstrings + s->sh_name,
1684 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1685 DEBUGP("\t%s\n", secstrings + s->sh_name);
1688 mod->core_text_size = mod->core_size;
1691 DEBUGP("Init section allocation order:\n");
1692 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1693 for (i = 0; i < hdr->e_shnum; ++i) {
1694 Elf_Shdr *s = &sechdrs[i];
1696 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1697 || (s->sh_flags & masks[m][1])
1698 || s->sh_entsize != ~0UL
1699 || strncmp(secstrings + s->sh_name,
1702 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1703 | INIT_OFFSET_MASK);
1704 DEBUGP("\t%s\n", secstrings + s->sh_name);
1707 mod->init_text_size = mod->init_size;
1711 static void set_license(struct module *mod, const char *license)
1714 license = "unspecified";
1716 if (!license_is_gpl_compatible(license)) {
1717 if (!test_taint(TAINT_PROPRIETARY_MODULE))
1718 printk(KERN_WARNING "%s: module license '%s' taints "
1719 "kernel.\n", mod->name, license);
1720 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1724 /* Parse tag=value strings from .modinfo section */
1725 static char *next_string(char *string, unsigned long *secsize)
1727 /* Skip non-zero chars */
1730 if ((*secsize)-- <= 1)
1734 /* Skip any zero padding. */
1735 while (!string[0]) {
1737 if ((*secsize)-- <= 1)
1743 static char *get_modinfo(Elf_Shdr *sechdrs,
1748 unsigned int taglen = strlen(tag);
1749 unsigned long size = sechdrs[info].sh_size;
1751 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1752 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1753 return p + taglen + 1;
1758 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1759 unsigned int infoindex)
1761 struct module_attribute *attr;
1764 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1767 get_modinfo(sechdrs,
1773 #ifdef CONFIG_KALLSYMS
1775 /* lookup symbol in given range of kernel_symbols */
1776 static const struct kernel_symbol *lookup_symbol(const char *name,
1777 const struct kernel_symbol *start,
1778 const struct kernel_symbol *stop)
1780 const struct kernel_symbol *ks = start;
1781 for (; ks < stop; ks++)
1782 if (strcmp(ks->name, name) == 0)
1787 static int is_exported(const char *name, unsigned long value,
1788 const struct module *mod)
1790 const struct kernel_symbol *ks;
1792 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
1794 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1795 return ks != NULL && ks->value == value;
1799 static char elf_type(const Elf_Sym *sym,
1801 const char *secstrings,
1804 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1805 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1810 if (sym->st_shndx == SHN_UNDEF)
1812 if (sym->st_shndx == SHN_ABS)
1814 if (sym->st_shndx >= SHN_LORESERVE)
1816 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1818 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1819 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1820 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1822 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1827 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1828 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1833 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1834 ".debug", strlen(".debug")) == 0)
1839 static void add_kallsyms(struct module *mod,
1841 unsigned int symindex,
1842 unsigned int strindex,
1843 const char *secstrings)
1847 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1848 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1849 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1851 /* Set types up while we still have access to sections. */
1852 for (i = 0; i < mod->num_symtab; i++)
1853 mod->symtab[i].st_info
1854 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1857 static inline void add_kallsyms(struct module *mod,
1859 unsigned int symindex,
1860 unsigned int strindex,
1861 const char *secstrings)
1864 #endif /* CONFIG_KALLSYMS */
1866 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1868 #ifdef CONFIG_DYNAMIC_DEBUG
1869 if (ddebug_add_module(debug, num, debug->modname))
1870 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1875 static void *module_alloc_update_bounds(unsigned long size)
1877 void *ret = module_alloc(size);
1880 /* Update module bounds. */
1881 if ((unsigned long)ret < module_addr_min)
1882 module_addr_min = (unsigned long)ret;
1883 if ((unsigned long)ret + size > module_addr_max)
1884 module_addr_max = (unsigned long)ret + size;
1889 /* Allocate and load the module: note that size of section 0 is always
1890 zero, and we rely on this for optional sections. */
1891 static noinline struct module *load_module(void __user *umod,
1893 const char __user *uargs)
1897 char *secstrings, *args, *modmagic, *strtab = NULL;
1900 unsigned int symindex = 0;
1901 unsigned int strindex = 0;
1902 unsigned int modindex, versindex, infoindex, pcpuindex;
1903 unsigned int num_mcount;
1906 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1907 unsigned long *mseg;
1908 mm_segment_t old_fs;
1910 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1912 if (len < sizeof(*hdr))
1913 return ERR_PTR(-ENOEXEC);
1915 /* Suck in entire file: we'll want most of it. */
1916 /* vmalloc barfs on "unusual" numbers. Check here */
1917 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1918 return ERR_PTR(-ENOMEM);
1920 /* Create stop_machine threads since the error path relies on
1921 * a non-failing stop_machine call. */
1922 err = stop_machine_create();
1926 if (copy_from_user(hdr, umod, len) != 0) {
1931 /* Sanity checks against insmoding binaries or wrong arch,
1932 weird elf version */
1933 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
1934 || hdr->e_type != ET_REL
1935 || !elf_check_arch(hdr)
1936 || hdr->e_shentsize != sizeof(*sechdrs)) {
1941 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1944 /* Convenience variables */
1945 sechdrs = (void *)hdr + hdr->e_shoff;
1946 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1947 sechdrs[0].sh_addr = 0;
1949 for (i = 1; i < hdr->e_shnum; i++) {
1950 if (sechdrs[i].sh_type != SHT_NOBITS
1951 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1954 /* Mark all sections sh_addr with their address in the
1956 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1958 /* Internal symbols and strings. */
1959 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1961 strindex = sechdrs[i].sh_link;
1962 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1964 #ifndef CONFIG_MODULE_UNLOAD
1965 /* Don't load .exit sections */
1966 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1967 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1971 modindex = find_sec(hdr, sechdrs, secstrings,
1972 ".gnu.linkonce.this_module");
1974 printk(KERN_WARNING "No module found in object\n");
1978 /* This is temporary: point mod into copy of data. */
1979 mod = (void *)sechdrs[modindex].sh_addr;
1981 if (symindex == 0) {
1982 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1988 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1989 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1990 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1992 /* Don't keep modinfo and version sections. */
1993 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1994 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1995 #ifdef CONFIG_KALLSYMS
1996 /* Keep symbol and string tables for decoding later. */
1997 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1998 sechdrs[strindex].sh_flags |= SHF_ALLOC;
2001 /* Check module struct version now, before we try to use module. */
2002 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2007 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2008 /* This is allowed: modprobe --force will invalidate it. */
2010 err = try_to_force_load(mod, "magic");
2013 } else if (!same_magic(modmagic, vermagic, versindex)) {
2014 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2015 mod->name, modmagic, vermagic);
2020 staging = get_modinfo(sechdrs, infoindex, "staging");
2022 add_taint_module(mod, TAINT_CRAP);
2023 printk(KERN_WARNING "%s: module is from the staging directory,"
2024 " the quality is unknown, you have been warned.\n",
2028 /* Now copy in args */
2029 args = strndup_user(uargs, ~0UL >> 1);
2031 err = PTR_ERR(args);
2035 if (find_module(mod->name)) {
2040 mod->state = MODULE_STATE_COMING;
2042 /* Allow arches to frob section contents and sizes. */
2043 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2048 /* We have a special allocation for this section. */
2049 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
2050 sechdrs[pcpuindex].sh_addralign,
2056 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2057 mod->percpu = percpu;
2060 /* Determine total sizes, and put offsets in sh_entsize. For now
2061 this is done generically; there doesn't appear to be any
2062 special cases for the architectures. */
2063 layout_sections(mod, hdr, sechdrs, secstrings);
2065 /* Do the allocs. */
2066 ptr = module_alloc_update_bounds(mod->core_size);
2071 memset(ptr, 0, mod->core_size);
2072 mod->module_core = ptr;
2074 ptr = module_alloc_update_bounds(mod->init_size);
2075 if (!ptr && mod->init_size) {
2079 memset(ptr, 0, mod->init_size);
2080 mod->module_init = ptr;
2082 /* Transfer each section which specifies SHF_ALLOC */
2083 DEBUGP("final section addresses:\n");
2084 for (i = 0; i < hdr->e_shnum; i++) {
2087 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2090 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2091 dest = mod->module_init
2092 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2094 dest = mod->module_core + sechdrs[i].sh_entsize;
2096 if (sechdrs[i].sh_type != SHT_NOBITS)
2097 memcpy(dest, (void *)sechdrs[i].sh_addr,
2098 sechdrs[i].sh_size);
2099 /* Update sh_addr to point to copy in image. */
2100 sechdrs[i].sh_addr = (unsigned long)dest;
2101 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2103 /* Module has been moved. */
2104 mod = (void *)sechdrs[modindex].sh_addr;
2106 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2107 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2114 /* Now we've moved module, initialize linked lists, etc. */
2115 module_unload_init(mod);
2117 /* add kobject, so we can reference it. */
2118 err = mod_sysfs_init(mod);
2122 /* Set up license info based on the info section */
2123 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2126 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2127 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2128 * using GPL-only symbols it needs.
2130 if (strcmp(mod->name, "ndiswrapper") == 0)
2131 add_taint(TAINT_PROPRIETARY_MODULE);
2133 /* driverloader was caught wrongly pretending to be under GPL */
2134 if (strcmp(mod->name, "driverloader") == 0)
2135 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2137 /* Set up MODINFO_ATTR fields */
2138 setup_modinfo(mod, sechdrs, infoindex);
2140 /* Fix up syms, so that st_value is a pointer to location. */
2141 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2146 /* Now we've got everything in the final locations, we can
2147 * find optional sections. */
2148 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2149 sizeof(*mod->kp), &mod->num_kp);
2150 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2151 sizeof(*mod->syms), &mod->num_syms);
2152 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2153 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2154 sizeof(*mod->gpl_syms),
2155 &mod->num_gpl_syms);
2156 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2157 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2158 "__ksymtab_gpl_future",
2159 sizeof(*mod->gpl_future_syms),
2160 &mod->num_gpl_future_syms);
2161 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2162 "__kcrctab_gpl_future");
2164 #ifdef CONFIG_UNUSED_SYMBOLS
2165 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2167 sizeof(*mod->unused_syms),
2168 &mod->num_unused_syms);
2169 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2170 "__kcrctab_unused");
2171 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2172 "__ksymtab_unused_gpl",
2173 sizeof(*mod->unused_gpl_syms),
2174 &mod->num_unused_gpl_syms);
2175 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2176 "__kcrctab_unused_gpl");
2179 #ifdef CONFIG_MARKERS
2180 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2181 sizeof(*mod->markers), &mod->num_markers);
2183 #ifdef CONFIG_TRACEPOINTS
2184 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2186 sizeof(*mod->tracepoints),
2187 &mod->num_tracepoints);
2190 #ifdef CONFIG_MODVERSIONS
2191 if ((mod->num_syms && !mod->crcs)
2192 || (mod->num_gpl_syms && !mod->gpl_crcs)
2193 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2194 #ifdef CONFIG_UNUSED_SYMBOLS
2195 || (mod->num_unused_syms && !mod->unused_crcs)
2196 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2199 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2200 err = try_to_force_load(mod, "nocrc");
2206 /* Now do relocations. */
2207 for (i = 1; i < hdr->e_shnum; i++) {
2208 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2209 unsigned int info = sechdrs[i].sh_info;
2211 /* Not a valid relocation section? */
2212 if (info >= hdr->e_shnum)
2215 /* Don't bother with non-allocated sections */
2216 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2219 if (sechdrs[i].sh_type == SHT_REL)
2220 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2221 else if (sechdrs[i].sh_type == SHT_RELA)
2222 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2228 /* Find duplicate symbols */
2229 err = verify_export_symbols(mod);
2233 /* Set up and sort exception table */
2234 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2235 sizeof(*mod->extable), &mod->num_exentries);
2236 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2238 /* Finally, copy percpu area over. */
2239 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2240 sechdrs[pcpuindex].sh_size);
2242 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2245 struct _ddebug *debug;
2246 unsigned int num_debug;
2248 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2249 sizeof(*debug), &num_debug);
2251 dynamic_debug_setup(debug, num_debug);
2254 /* sechdrs[0].sh_size is always zero */
2255 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2256 sizeof(*mseg), &num_mcount);
2257 ftrace_init_module(mod, mseg, mseg + num_mcount);
2259 err = module_finalize(hdr, sechdrs, mod);
2263 /* flush the icache in correct context */
2268 * Flush the instruction cache, since we've played with text.
2269 * Do it before processing of module parameters, so the module
2270 * can provide parameter accessor functions of its own.
2272 if (mod->module_init)
2273 flush_icache_range((unsigned long)mod->module_init,
2274 (unsigned long)mod->module_init
2276 flush_icache_range((unsigned long)mod->module_core,
2277 (unsigned long)mod->module_core + mod->core_size);
2282 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2283 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2286 /* Now sew it into the lists so we can get lockdep and oops
2287 * info during argument parsing. Noone should access us, since
2288 * strong_try_module_get() will fail.
2289 * lockdep/oops can run asynchronous, so use the RCU list insertion
2290 * function to insert in a way safe to concurrent readers.
2291 * The mutex protects against concurrent writers.
2293 list_add_rcu(&mod->list, &modules);
2295 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2299 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2302 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2303 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2305 /* Get rid of temporary copy */
2308 stop_machine_destroy();
2313 stop_machine(__unlink_module, mod, NULL);
2314 module_arch_cleanup(mod);
2316 kobject_del(&mod->mkobj.kobj);
2317 kobject_put(&mod->mkobj.kobj);
2318 ftrace_release(mod->module_core, mod->core_size);
2320 module_unload_free(mod);
2321 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2323 percpu_modfree(mod->refptr);
2325 module_free(mod, mod->module_init);
2327 module_free(mod, mod->module_core);
2328 /* mod will be freed with core. Don't access it beyond this line! */
2331 percpu_modfree(percpu);
2336 stop_machine_destroy();
2337 return ERR_PTR(err);
2340 printk(KERN_ERR "Module len %lu truncated\n", len);
2345 /* This is where the real work happens */
2346 SYSCALL_DEFINE3(init_module, void __user *, umod,
2347 unsigned long, len, const char __user *, uargs)
2352 /* Must have permission */
2353 if (!capable(CAP_SYS_MODULE))
2356 /* Only one module load at a time, please */
2357 if (mutex_lock_interruptible(&module_mutex) != 0)
2360 /* Do all the hard work */
2361 mod = load_module(umod, len, uargs);
2363 mutex_unlock(&module_mutex);
2364 return PTR_ERR(mod);
2367 /* Drop lock so they can recurse */
2368 mutex_unlock(&module_mutex);
2370 blocking_notifier_call_chain(&module_notify_list,
2371 MODULE_STATE_COMING, mod);
2373 /* Start the module */
2374 if (mod->init != NULL)
2375 ret = do_one_initcall(mod->init);
2377 /* Init routine failed: abort. Try to protect us from
2378 buggy refcounters. */
2379 mod->state = MODULE_STATE_GOING;
2380 synchronize_sched();
2382 blocking_notifier_call_chain(&module_notify_list,
2383 MODULE_STATE_GOING, mod);
2384 mutex_lock(&module_mutex);
2386 mutex_unlock(&module_mutex);
2387 wake_up(&module_wq);
2391 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2392 "it should follow 0/-E convention\n"
2393 KERN_WARNING "%s: loading module anyway...\n",
2394 __func__, mod->name, ret,
2399 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2400 mod->state = MODULE_STATE_LIVE;
2401 wake_up(&module_wq);
2402 blocking_notifier_call_chain(&module_notify_list,
2403 MODULE_STATE_LIVE, mod);
2405 mutex_lock(&module_mutex);
2406 /* Drop initial reference. */
2408 module_free(mod, mod->module_init);
2409 mod->module_init = NULL;
2411 mod->init_text_size = 0;
2412 mutex_unlock(&module_mutex);
2417 static inline int within(unsigned long addr, void *start, unsigned long size)
2419 return ((void *)addr >= start && (void *)addr < start + size);
2422 #ifdef CONFIG_KALLSYMS
2424 * This ignores the intensely annoying "mapping symbols" found
2425 * in ARM ELF files: $a, $t and $d.
2427 static inline int is_arm_mapping_symbol(const char *str)
2429 return str[0] == '$' && strchr("atd", str[1])
2430 && (str[2] == '\0' || str[2] == '.');
2433 static const char *get_ksymbol(struct module *mod,
2435 unsigned long *size,
2436 unsigned long *offset)
2438 unsigned int i, best = 0;
2439 unsigned long nextval;
2441 /* At worse, next value is at end of module */
2442 if (within_module_init(addr, mod))
2443 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2445 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2447 /* Scan for closest preceeding symbol, and next symbol. (ELF
2448 starts real symbols at 1). */
2449 for (i = 1; i < mod->num_symtab; i++) {
2450 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2453 /* We ignore unnamed symbols: they're uninformative
2454 * and inserted at a whim. */
2455 if (mod->symtab[i].st_value <= addr
2456 && mod->symtab[i].st_value > mod->symtab[best].st_value
2457 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2458 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2460 if (mod->symtab[i].st_value > addr
2461 && mod->symtab[i].st_value < nextval
2462 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2463 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2464 nextval = mod->symtab[i].st_value;
2471 *size = nextval - mod->symtab[best].st_value;
2473 *offset = addr - mod->symtab[best].st_value;
2474 return mod->strtab + mod->symtab[best].st_name;
2477 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2478 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2479 const char *module_address_lookup(unsigned long addr,
2480 unsigned long *size,
2481 unsigned long *offset,
2486 const char *ret = NULL;
2489 list_for_each_entry_rcu(mod, &modules, list) {
2490 if (within_module_init(addr, mod) ||
2491 within_module_core(addr, mod)) {
2493 *modname = mod->name;
2494 ret = get_ksymbol(mod, addr, size, offset);
2498 /* Make a copy in here where it's safe */
2500 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2507 int lookup_module_symbol_name(unsigned long addr, char *symname)
2512 list_for_each_entry_rcu(mod, &modules, list) {
2513 if (within_module_init(addr, mod) ||
2514 within_module_core(addr, mod)) {
2517 sym = get_ksymbol(mod, addr, NULL, NULL);
2520 strlcpy(symname, sym, KSYM_NAME_LEN);
2530 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2531 unsigned long *offset, char *modname, char *name)
2536 list_for_each_entry_rcu(mod, &modules, list) {
2537 if (within_module_init(addr, mod) ||
2538 within_module_core(addr, mod)) {
2541 sym = get_ksymbol(mod, addr, size, offset);
2545 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2547 strlcpy(name, sym, KSYM_NAME_LEN);
2557 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2558 char *name, char *module_name, int *exported)
2563 list_for_each_entry_rcu(mod, &modules, list) {
2564 if (symnum < mod->num_symtab) {
2565 *value = mod->symtab[symnum].st_value;
2566 *type = mod->symtab[symnum].st_info;
2567 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2569 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2570 *exported = is_exported(name, *value, mod);
2574 symnum -= mod->num_symtab;
2580 static unsigned long mod_find_symname(struct module *mod, const char *name)
2584 for (i = 0; i < mod->num_symtab; i++)
2585 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2586 mod->symtab[i].st_info != 'U')
2587 return mod->symtab[i].st_value;
2591 /* Look for this name: can be of form module:name. */
2592 unsigned long module_kallsyms_lookup_name(const char *name)
2596 unsigned long ret = 0;
2598 /* Don't lock: we're in enough trouble already. */
2600 if ((colon = strchr(name, ':')) != NULL) {
2602 if ((mod = find_module(name)) != NULL)
2603 ret = mod_find_symname(mod, colon+1);
2606 list_for_each_entry_rcu(mod, &modules, list)
2607 if ((ret = mod_find_symname(mod, name)) != 0)
2613 #endif /* CONFIG_KALLSYMS */
2615 static char *module_flags(struct module *mod, char *buf)
2620 mod->state == MODULE_STATE_GOING ||
2621 mod->state == MODULE_STATE_COMING) {
2623 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
2625 if (mod->taints & (1 << TAINT_FORCED_MODULE))
2627 if (mod->taints & (1 << TAINT_CRAP))
2630 * TAINT_FORCED_RMMOD: could be added.
2631 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2635 /* Show a - for module-is-being-unloaded */
2636 if (mod->state == MODULE_STATE_GOING)
2638 /* Show a + for module-is-being-loaded */
2639 if (mod->state == MODULE_STATE_COMING)
2648 #ifdef CONFIG_PROC_FS
2649 /* Called by the /proc file system to return a list of modules. */
2650 static void *m_start(struct seq_file *m, loff_t *pos)
2652 mutex_lock(&module_mutex);
2653 return seq_list_start(&modules, *pos);
2656 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2658 return seq_list_next(p, &modules, pos);
2661 static void m_stop(struct seq_file *m, void *p)
2663 mutex_unlock(&module_mutex);
2666 static int m_show(struct seq_file *m, void *p)
2668 struct module *mod = list_entry(p, struct module, list);
2671 seq_printf(m, "%s %u",
2672 mod->name, mod->init_size + mod->core_size);
2673 print_unload_info(m, mod);
2675 /* Informative for users. */
2676 seq_printf(m, " %s",
2677 mod->state == MODULE_STATE_GOING ? "Unloading":
2678 mod->state == MODULE_STATE_COMING ? "Loading":
2680 /* Used by oprofile and other similar tools. */
2681 seq_printf(m, " 0x%p", mod->module_core);
2685 seq_printf(m, " %s", module_flags(mod, buf));
2687 seq_printf(m, "\n");
2691 /* Format: modulename size refcount deps address
2693 Where refcount is a number or -, and deps is a comma-separated list
2696 static const struct seq_operations modules_op = {
2703 static int modules_open(struct inode *inode, struct file *file)
2705 return seq_open(file, &modules_op);
2708 static const struct file_operations proc_modules_operations = {
2709 .open = modules_open,
2711 .llseek = seq_lseek,
2712 .release = seq_release,
2715 static int __init proc_modules_init(void)
2717 proc_create("modules", 0, NULL, &proc_modules_operations);
2720 module_init(proc_modules_init);
2723 /* Given an address, look for it in the module exception tables. */
2724 const struct exception_table_entry *search_module_extables(unsigned long addr)
2726 const struct exception_table_entry *e = NULL;
2730 list_for_each_entry_rcu(mod, &modules, list) {
2731 if (mod->num_exentries == 0)
2734 e = search_extable(mod->extable,
2735 mod->extable + mod->num_exentries - 1,
2742 /* Now, if we found one, we are running inside it now, hence
2743 we cannot unload the module, hence no refcnt needed. */
2748 * is_module_address - is this address inside a module?
2749 * @addr: the address to check.
2751 * See is_module_text_address() if you simply want to see if the address
2752 * is code (not data).
2754 bool is_module_address(unsigned long addr)
2759 ret = __module_address(addr) != NULL;
2766 * __module_address - get the module which contains an address.
2767 * @addr: the address.
2769 * Must be called with preempt disabled or module mutex held so that
2770 * module doesn't get freed during this.
2772 __notrace_funcgraph struct module *__module_address(unsigned long addr)
2776 if (addr < module_addr_min || addr > module_addr_max)
2779 list_for_each_entry_rcu(mod, &modules, list)
2780 if (within_module_core(addr, mod)
2781 || within_module_init(addr, mod))
2787 * is_module_text_address - is this address inside module code?
2788 * @addr: the address to check.
2790 * See is_module_address() if you simply want to see if the address is
2791 * anywhere in a module. See kernel_text_address() for testing if an
2792 * address corresponds to kernel or module code.
2794 bool is_module_text_address(unsigned long addr)
2799 ret = __module_text_address(addr) != NULL;
2806 * __module_text_address - get the module whose code contains an address.
2807 * @addr: the address.
2809 * Must be called with preempt disabled or module mutex held so that
2810 * module doesn't get freed during this.
2812 struct module *__module_text_address(unsigned long addr)
2814 struct module *mod = __module_address(addr);
2816 /* Make sure it's within the text section. */
2817 if (!within(addr, mod->module_init, mod->init_text_size)
2818 && !within(addr, mod->module_core, mod->core_text_size))
2824 struct module *module_text_address(unsigned long addr)
2829 mod = __module_text_address(addr);
2835 /* Don't grab lock, we're oopsing. */
2836 void print_modules(void)
2841 printk("Modules linked in:");
2842 /* Most callers should already have preempt disabled, but make sure */
2844 list_for_each_entry_rcu(mod, &modules, list)
2845 printk(" %s%s", mod->name, module_flags(mod, buf));
2847 if (last_unloaded_module[0])
2848 printk(" [last unloaded: %s]", last_unloaded_module);
2852 #ifdef CONFIG_MODVERSIONS
2853 /* Generate the signature for struct module here, too, for modversions. */
2854 void struct_module(struct module *mod) { return; }
2855 EXPORT_SYMBOL(struct_module);
2858 #ifdef CONFIG_MARKERS
2859 void module_update_markers(void)
2863 mutex_lock(&module_mutex);
2864 list_for_each_entry(mod, &modules, list)
2866 marker_update_probe_range(mod->markers,
2867 mod->markers + mod->num_markers);
2868 mutex_unlock(&module_mutex);
2872 #ifdef CONFIG_TRACEPOINTS
2873 void module_update_tracepoints(void)
2877 mutex_lock(&module_mutex);
2878 list_for_each_entry(mod, &modules, list)
2880 tracepoint_update_probe_range(mod->tracepoints,
2881 mod->tracepoints + mod->num_tracepoints);
2882 mutex_unlock(&module_mutex);
2886 * Returns 0 if current not found.
2887 * Returns 1 if current found.
2889 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2891 struct module *iter_mod;
2894 mutex_lock(&module_mutex);
2895 list_for_each_entry(iter_mod, &modules, list) {
2896 if (!iter_mod->taints) {
2898 * Sorted module list
2900 if (iter_mod < iter->module)
2902 else if (iter_mod > iter->module)
2903 iter->tracepoint = NULL;
2904 found = tracepoint_get_iter_range(&iter->tracepoint,
2905 iter_mod->tracepoints,
2906 iter_mod->tracepoints
2907 + iter_mod->num_tracepoints);
2909 iter->module = iter_mod;
2914 mutex_unlock(&module_mutex);