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>
58 #define DEBUGP(fmt , a...)
61 #ifndef ARCH_SHF_SMALL
62 #define ARCH_SHF_SMALL 0
65 /* If this is set, the section belongs in the init part of the module */
66 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
68 /* List of modules, protected by module_mutex or preempt_disable
69 * (delete uses stop_machine/add uses RCU list operations). */
70 static DEFINE_MUTEX(module_mutex);
71 static LIST_HEAD(modules);
73 /* Waiting for a module to finish initializing? */
74 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
76 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
78 /* Bounds of module allocation, for speeding __module_text_address */
79 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
81 int register_module_notifier(struct notifier_block * nb)
83 return blocking_notifier_chain_register(&module_notify_list, nb);
85 EXPORT_SYMBOL(register_module_notifier);
87 int unregister_module_notifier(struct notifier_block * nb)
89 return blocking_notifier_chain_unregister(&module_notify_list, nb);
91 EXPORT_SYMBOL(unregister_module_notifier);
93 /* We require a truly strong try_module_get(): 0 means failure due to
94 ongoing or failed initialization etc. */
95 static inline int strong_try_module_get(struct module *mod)
97 if (mod && mod->state == MODULE_STATE_COMING)
99 if (try_module_get(mod))
105 static inline void add_taint_module(struct module *mod, unsigned flag)
108 mod->taints |= (1U << flag);
112 * A thread that wants to hold a reference to a module only while it
113 * is running can call this to safely exit. nfsd and lockd use this.
115 void __module_put_and_exit(struct module *mod, long code)
120 EXPORT_SYMBOL(__module_put_and_exit);
122 /* Find a module section: 0 means not found. */
123 static unsigned int find_sec(Elf_Ehdr *hdr,
125 const char *secstrings,
130 for (i = 1; i < hdr->e_shnum; i++)
131 /* Alloc bit cleared means "ignore it." */
132 if ((sechdrs[i].sh_flags & SHF_ALLOC)
133 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
138 /* Find a module section, or NULL. */
139 static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
140 const char *secstrings, const char *name)
142 /* Section 0 has sh_addr 0. */
143 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
146 /* Find a module section, or NULL. Fill in number of "objects" in section. */
147 static void *section_objs(Elf_Ehdr *hdr,
149 const char *secstrings,
154 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
156 /* Section 0 has sh_addr 0 and sh_size 0. */
157 *num = sechdrs[sec].sh_size / object_size;
158 return (void *)sechdrs[sec].sh_addr;
161 /* Provided by the linker */
162 extern const struct kernel_symbol __start___ksymtab[];
163 extern const struct kernel_symbol __stop___ksymtab[];
164 extern const struct kernel_symbol __start___ksymtab_gpl[];
165 extern const struct kernel_symbol __stop___ksymtab_gpl[];
166 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
167 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
168 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
169 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
170 extern const unsigned long __start___kcrctab[];
171 extern const unsigned long __start___kcrctab_gpl[];
172 extern const unsigned long __start___kcrctab_gpl_future[];
173 #ifdef CONFIG_UNUSED_SYMBOLS
174 extern const struct kernel_symbol __start___ksymtab_unused[];
175 extern const struct kernel_symbol __stop___ksymtab_unused[];
176 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
177 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
178 extern const unsigned long __start___kcrctab_unused[];
179 extern const unsigned long __start___kcrctab_unused_gpl[];
182 #ifndef CONFIG_MODVERSIONS
183 #define symversion(base, idx) NULL
185 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
189 const struct kernel_symbol *start, *stop;
190 const unsigned long *crcs;
199 static bool each_symbol_in_section(const struct symsearch *arr,
200 unsigned int arrsize,
201 struct module *owner,
202 bool (*fn)(const struct symsearch *syms,
203 struct module *owner,
204 unsigned int symnum, void *data),
209 for (j = 0; j < arrsize; j++) {
210 for (i = 0; i < arr[j].stop - arr[j].start; i++)
211 if (fn(&arr[j], owner, i, data))
218 /* Returns true as soon as fn returns true, otherwise false. */
219 static bool each_symbol(bool (*fn)(const struct symsearch *arr,
220 struct module *owner,
221 unsigned int symnum, void *data),
225 const struct symsearch arr[] = {
226 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
227 NOT_GPL_ONLY, false },
228 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
229 __start___kcrctab_gpl,
231 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
232 __start___kcrctab_gpl_future,
233 WILL_BE_GPL_ONLY, false },
234 #ifdef CONFIG_UNUSED_SYMBOLS
235 { __start___ksymtab_unused, __stop___ksymtab_unused,
236 __start___kcrctab_unused,
237 NOT_GPL_ONLY, true },
238 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
239 __start___kcrctab_unused_gpl,
244 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
247 list_for_each_entry_rcu(mod, &modules, list) {
248 struct symsearch arr[] = {
249 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
250 NOT_GPL_ONLY, false },
251 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
254 { mod->gpl_future_syms,
255 mod->gpl_future_syms + mod->num_gpl_future_syms,
256 mod->gpl_future_crcs,
257 WILL_BE_GPL_ONLY, false },
258 #ifdef CONFIG_UNUSED_SYMBOLS
260 mod->unused_syms + mod->num_unused_syms,
262 NOT_GPL_ONLY, true },
263 { mod->unused_gpl_syms,
264 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
265 mod->unused_gpl_crcs,
270 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
276 struct find_symbol_arg {
283 struct module *owner;
284 const unsigned long *crc;
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->value = syms->start[symnum].value;
330 /* Find a symbol, return value, (optional) crc and (optional) module
332 static unsigned long 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);
356 /* Search for module by name: must hold module_mutex. */
357 static struct module *find_module(const char *name)
361 list_for_each_entry(mod, &modules, list) {
362 if (strcmp(mod->name, name) == 0)
369 /* Number of blocks used and allocated. */
370 static unsigned int pcpu_num_used, pcpu_num_allocated;
371 /* Size of each block. -ve means used. */
372 static int *pcpu_size;
374 static int split_block(unsigned int i, unsigned short size)
376 /* Reallocation required? */
377 if (pcpu_num_used + 1 > pcpu_num_allocated) {
380 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
385 pcpu_num_allocated *= 2;
389 /* Insert a new subblock */
390 memmove(&pcpu_size[i+1], &pcpu_size[i],
391 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
394 pcpu_size[i+1] -= size;
399 static inline unsigned int block_size(int val)
406 static void *percpu_modalloc(unsigned long size, unsigned long align,
413 if (align > PAGE_SIZE) {
414 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
415 name, align, PAGE_SIZE);
419 ptr = __per_cpu_start;
420 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
421 /* Extra for alignment requirement. */
422 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
423 BUG_ON(i == 0 && extra != 0);
425 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
428 /* Transfer extra to previous block. */
429 if (pcpu_size[i-1] < 0)
430 pcpu_size[i-1] -= extra;
432 pcpu_size[i-1] += extra;
433 pcpu_size[i] -= extra;
436 /* Split block if warranted */
437 if (pcpu_size[i] - size > sizeof(unsigned long))
438 if (!split_block(i, size))
442 pcpu_size[i] = -pcpu_size[i];
446 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
451 static void percpu_modfree(void *freeme)
454 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
456 /* First entry is core kernel percpu data. */
457 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
459 pcpu_size[i] = -pcpu_size[i];
466 /* Merge with previous? */
467 if (pcpu_size[i-1] >= 0) {
468 pcpu_size[i-1] += pcpu_size[i];
470 memmove(&pcpu_size[i], &pcpu_size[i+1],
471 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
474 /* Merge with next? */
475 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
476 pcpu_size[i] += pcpu_size[i+1];
478 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
479 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
483 static int percpu_modinit(void)
486 pcpu_num_allocated = 2;
487 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
489 /* Static in-kernel percpu data (used). */
490 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
492 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
493 if (pcpu_size[1] < 0) {
494 printk(KERN_ERR "No per-cpu room for modules.\n");
500 __initcall(percpu_modinit);
502 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
504 const char *secstrings)
506 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
509 static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
513 for_each_possible_cpu(cpu)
514 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
517 #else /* ... !CONFIG_SMP */
519 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
524 static inline void percpu_modfree(void *pcpuptr)
528 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
530 const char *secstrings)
534 static inline void percpu_modcopy(void *pcpudst, const void *src,
537 /* pcpusec should be 0, and size of that section should be 0. */
541 #endif /* CONFIG_SMP */
543 #define MODINFO_ATTR(field) \
544 static void setup_modinfo_##field(struct module *mod, const char *s) \
546 mod->field = kstrdup(s, GFP_KERNEL); \
548 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
549 struct module *mod, char *buffer) \
551 return sprintf(buffer, "%s\n", mod->field); \
553 static int modinfo_##field##_exists(struct module *mod) \
555 return mod->field != NULL; \
557 static void free_modinfo_##field(struct module *mod) \
562 static struct module_attribute modinfo_##field = { \
563 .attr = { .name = __stringify(field), .mode = 0444 }, \
564 .show = show_modinfo_##field, \
565 .setup = setup_modinfo_##field, \
566 .test = modinfo_##field##_exists, \
567 .free = free_modinfo_##field, \
570 MODINFO_ATTR(version);
571 MODINFO_ATTR(srcversion);
573 static char last_unloaded_module[MODULE_NAME_LEN+1];
575 #ifdef CONFIG_MODULE_UNLOAD
576 /* Init the unload section of the module. */
577 static void module_unload_init(struct module *mod)
581 INIT_LIST_HEAD(&mod->modules_which_use_me);
582 for_each_possible_cpu(cpu)
583 local_set(__module_ref_addr(mod, cpu), 0);
584 /* Hold reference count during initialization. */
585 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
586 /* Backwards compatibility macros put refcount during init. */
587 mod->waiter = current;
590 /* modules using other modules */
593 struct list_head list;
594 struct module *module_which_uses;
597 /* Does a already use b? */
598 static int already_uses(struct module *a, struct module *b)
600 struct module_use *use;
602 list_for_each_entry(use, &b->modules_which_use_me, list) {
603 if (use->module_which_uses == a) {
604 DEBUGP("%s uses %s!\n", a->name, b->name);
608 DEBUGP("%s does not use %s!\n", a->name, b->name);
612 /* Module a uses b */
613 static int use_module(struct module *a, struct module *b)
615 struct module_use *use;
618 if (b == NULL || already_uses(a, b)) return 1;
620 /* If we're interrupted or time out, we fail. */
621 if (wait_event_interruptible_timeout(
622 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
624 printk("%s: gave up waiting for init of module %s.\n",
629 /* If strong_try_module_get() returned a different error, we fail. */
633 DEBUGP("Allocating new usage for %s.\n", a->name);
634 use = kmalloc(sizeof(*use), GFP_ATOMIC);
636 printk("%s: out of memory loading\n", a->name);
641 use->module_which_uses = a;
642 list_add(&use->list, &b->modules_which_use_me);
643 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
647 /* Clear the unload stuff of the module. */
648 static void module_unload_free(struct module *mod)
652 list_for_each_entry(i, &modules, list) {
653 struct module_use *use;
655 list_for_each_entry(use, &i->modules_which_use_me, list) {
656 if (use->module_which_uses == mod) {
657 DEBUGP("%s unusing %s\n", mod->name, i->name);
659 list_del(&use->list);
661 sysfs_remove_link(i->holders_dir, mod->name);
662 /* There can be at most one match. */
669 #ifdef CONFIG_MODULE_FORCE_UNLOAD
670 static inline int try_force_unload(unsigned int flags)
672 int ret = (flags & O_TRUNC);
674 add_taint(TAINT_FORCED_RMMOD);
678 static inline int try_force_unload(unsigned int flags)
682 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
691 /* Whole machine is stopped with interrupts off when this runs. */
692 static int __try_stop_module(void *_sref)
694 struct stopref *sref = _sref;
696 /* If it's not unused, quit unless we're forcing. */
697 if (module_refcount(sref->mod) != 0) {
698 if (!(*sref->forced = try_force_unload(sref->flags)))
702 /* Mark it as dying. */
703 sref->mod->state = MODULE_STATE_GOING;
707 static int try_stop_module(struct module *mod, int flags, int *forced)
709 if (flags & O_NONBLOCK) {
710 struct stopref sref = { mod, flags, forced };
712 return stop_machine(__try_stop_module, &sref, NULL);
714 /* We don't need to stop the machine for this. */
715 mod->state = MODULE_STATE_GOING;
721 unsigned int module_refcount(struct module *mod)
723 unsigned int total = 0;
726 for_each_possible_cpu(cpu)
727 total += local_read(__module_ref_addr(mod, cpu));
730 EXPORT_SYMBOL(module_refcount);
732 /* This exists whether we can unload or not */
733 static void free_module(struct module *mod);
735 static void wait_for_zero_refcount(struct module *mod)
737 /* Since we might sleep for some time, release the mutex first */
738 mutex_unlock(&module_mutex);
740 DEBUGP("Looking at refcount...\n");
741 set_current_state(TASK_UNINTERRUPTIBLE);
742 if (module_refcount(mod) == 0)
746 current->state = TASK_RUNNING;
747 mutex_lock(&module_mutex);
750 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
754 char name[MODULE_NAME_LEN];
757 if (!capable(CAP_SYS_MODULE))
760 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
762 name[MODULE_NAME_LEN-1] = '\0';
764 /* Create stop_machine threads since free_module relies on
765 * a non-failing stop_machine call. */
766 ret = stop_machine_create();
770 if (mutex_lock_interruptible(&module_mutex) != 0) {
775 mod = find_module(name);
781 if (!list_empty(&mod->modules_which_use_me)) {
782 /* Other modules depend on us: get rid of them first. */
787 /* Doing init or already dying? */
788 if (mod->state != MODULE_STATE_LIVE) {
789 /* FIXME: if (force), slam module count and wake up
791 DEBUGP("%s already dying\n", mod->name);
796 /* If it has an init func, it must have an exit func to unload */
797 if (mod->init && !mod->exit) {
798 forced = try_force_unload(flags);
800 /* This module can't be removed */
806 /* Set this up before setting mod->state */
807 mod->waiter = current;
809 /* Stop the machine so refcounts can't move and disable module. */
810 ret = try_stop_module(mod, flags, &forced);
814 /* Never wait if forced. */
815 if (!forced && module_refcount(mod) != 0)
816 wait_for_zero_refcount(mod);
818 mutex_unlock(&module_mutex);
819 /* Final destruction now noone is using it. */
820 if (mod->exit != NULL)
822 blocking_notifier_call_chain(&module_notify_list,
823 MODULE_STATE_GOING, mod);
824 async_synchronize_full();
825 mutex_lock(&module_mutex);
826 /* Store the name of the last unloaded module for diagnostic purposes */
827 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
828 unregister_dynamic_debug_module(mod->name);
832 mutex_unlock(&module_mutex);
834 stop_machine_destroy();
838 static inline void print_unload_info(struct seq_file *m, struct module *mod)
840 struct module_use *use;
841 int printed_something = 0;
843 seq_printf(m, " %u ", module_refcount(mod));
845 /* Always include a trailing , so userspace can differentiate
846 between this and the old multi-field proc format. */
847 list_for_each_entry(use, &mod->modules_which_use_me, list) {
848 printed_something = 1;
849 seq_printf(m, "%s,", use->module_which_uses->name);
852 if (mod->init != NULL && mod->exit == NULL) {
853 printed_something = 1;
854 seq_printf(m, "[permanent],");
857 if (!printed_something)
861 void __symbol_put(const char *symbol)
863 struct module *owner;
866 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
871 EXPORT_SYMBOL(__symbol_put);
873 void symbol_put_addr(void *addr)
875 struct module *modaddr;
877 if (core_kernel_text((unsigned long)addr))
880 if (!(modaddr = module_text_address((unsigned long)addr)))
884 EXPORT_SYMBOL_GPL(symbol_put_addr);
886 static ssize_t show_refcnt(struct module_attribute *mattr,
887 struct module *mod, char *buffer)
889 return sprintf(buffer, "%u\n", module_refcount(mod));
892 static struct module_attribute refcnt = {
893 .attr = { .name = "refcnt", .mode = 0444 },
897 void module_put(struct module *module)
900 unsigned int cpu = get_cpu();
901 local_dec(__module_ref_addr(module, cpu));
902 /* Maybe they're waiting for us to drop reference? */
903 if (unlikely(!module_is_live(module)))
904 wake_up_process(module->waiter);
908 EXPORT_SYMBOL(module_put);
910 #else /* !CONFIG_MODULE_UNLOAD */
911 static inline void print_unload_info(struct seq_file *m, struct module *mod)
913 /* We don't know the usage count, or what modules are using. */
914 seq_printf(m, " - -");
917 static inline void module_unload_free(struct module *mod)
921 static inline int use_module(struct module *a, struct module *b)
923 return strong_try_module_get(b) == 0;
926 static inline void module_unload_init(struct module *mod)
929 #endif /* CONFIG_MODULE_UNLOAD */
931 static ssize_t show_initstate(struct module_attribute *mattr,
932 struct module *mod, char *buffer)
934 const char *state = "unknown";
936 switch (mod->state) {
937 case MODULE_STATE_LIVE:
940 case MODULE_STATE_COMING:
943 case MODULE_STATE_GOING:
947 return sprintf(buffer, "%s\n", state);
950 static struct module_attribute initstate = {
951 .attr = { .name = "initstate", .mode = 0444 },
952 .show = show_initstate,
955 static struct module_attribute *modinfo_attrs[] = {
959 #ifdef CONFIG_MODULE_UNLOAD
965 static const char vermagic[] = VERMAGIC_STRING;
967 static int try_to_force_load(struct module *mod, const char *symname)
969 #ifdef CONFIG_MODULE_FORCE_LOAD
970 if (!test_taint(TAINT_FORCED_MODULE))
971 printk("%s: no version for \"%s\" found: kernel tainted.\n",
973 add_taint_module(mod, TAINT_FORCED_MODULE);
980 #ifdef CONFIG_MODVERSIONS
981 static int check_version(Elf_Shdr *sechdrs,
982 unsigned int versindex,
985 const unsigned long *crc)
987 unsigned int i, num_versions;
988 struct modversion_info *versions;
990 /* Exporting module didn't supply crcs? OK, we're already tainted. */
994 /* No versions at all? modprobe --force does this. */
996 return try_to_force_load(mod, symname) == 0;
998 versions = (void *) sechdrs[versindex].sh_addr;
999 num_versions = sechdrs[versindex].sh_size
1000 / sizeof(struct modversion_info);
1002 for (i = 0; i < num_versions; i++) {
1003 if (strcmp(versions[i].name, symname) != 0)
1006 if (versions[i].crc == *crc)
1008 DEBUGP("Found checksum %lX vs module %lX\n",
1009 *crc, versions[i].crc);
1013 printk(KERN_WARNING "%s: no symbol version for %s\n",
1014 mod->name, symname);
1018 printk("%s: disagrees about version of symbol %s\n",
1019 mod->name, symname);
1023 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1024 unsigned int versindex,
1027 const unsigned long *crc;
1029 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
1031 return check_version(sechdrs, versindex, "struct_module", mod, crc);
1034 /* First part is kernel version, which we ignore if module has crcs. */
1035 static inline int same_magic(const char *amagic, const char *bmagic,
1039 amagic += strcspn(amagic, " ");
1040 bmagic += strcspn(bmagic, " ");
1042 return strcmp(amagic, bmagic) == 0;
1045 static inline int check_version(Elf_Shdr *sechdrs,
1046 unsigned int versindex,
1047 const char *symname,
1049 const unsigned long *crc)
1054 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1055 unsigned int versindex,
1061 static inline int same_magic(const char *amagic, const char *bmagic,
1064 return strcmp(amagic, bmagic) == 0;
1066 #endif /* CONFIG_MODVERSIONS */
1068 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1069 Must be holding module_mutex. */
1070 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1071 unsigned int versindex,
1075 struct module *owner;
1077 const unsigned long *crc;
1079 ret = find_symbol(name, &owner, &crc,
1080 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1081 if (!IS_ERR_VALUE(ret)) {
1082 /* use_module can fail due to OOM,
1083 or module initialization or unloading */
1084 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1085 !use_module(mod, owner))
1092 * /sys/module/foo/sections stuff
1093 * J. Corbet <corbet@lwn.net>
1095 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1096 struct module_sect_attr
1098 struct module_attribute mattr;
1100 unsigned long address;
1103 struct module_sect_attrs
1105 struct attribute_group grp;
1106 unsigned int nsections;
1107 struct module_sect_attr attrs[0];
1110 static ssize_t module_sect_show(struct module_attribute *mattr,
1111 struct module *mod, char *buf)
1113 struct module_sect_attr *sattr =
1114 container_of(mattr, struct module_sect_attr, mattr);
1115 return sprintf(buf, "0x%lx\n", sattr->address);
1118 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1120 unsigned int section;
1122 for (section = 0; section < sect_attrs->nsections; section++)
1123 kfree(sect_attrs->attrs[section].name);
1127 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1128 char *secstrings, Elf_Shdr *sechdrs)
1130 unsigned int nloaded = 0, i, size[2];
1131 struct module_sect_attrs *sect_attrs;
1132 struct module_sect_attr *sattr;
1133 struct attribute **gattr;
1135 /* Count loaded sections and allocate structures */
1136 for (i = 0; i < nsect; i++)
1137 if (sechdrs[i].sh_flags & SHF_ALLOC)
1139 size[0] = ALIGN(sizeof(*sect_attrs)
1140 + nloaded * sizeof(sect_attrs->attrs[0]),
1141 sizeof(sect_attrs->grp.attrs[0]));
1142 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1143 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1144 if (sect_attrs == NULL)
1147 /* Setup section attributes. */
1148 sect_attrs->grp.name = "sections";
1149 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1151 sect_attrs->nsections = 0;
1152 sattr = §_attrs->attrs[0];
1153 gattr = §_attrs->grp.attrs[0];
1154 for (i = 0; i < nsect; i++) {
1155 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1157 sattr->address = sechdrs[i].sh_addr;
1158 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1160 if (sattr->name == NULL)
1162 sect_attrs->nsections++;
1163 sattr->mattr.show = module_sect_show;
1164 sattr->mattr.store = NULL;
1165 sattr->mattr.attr.name = sattr->name;
1166 sattr->mattr.attr.mode = S_IRUGO;
1167 *(gattr++) = &(sattr++)->mattr.attr;
1171 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1174 mod->sect_attrs = sect_attrs;
1177 free_sect_attrs(sect_attrs);
1180 static void remove_sect_attrs(struct module *mod)
1182 if (mod->sect_attrs) {
1183 sysfs_remove_group(&mod->mkobj.kobj,
1184 &mod->sect_attrs->grp);
1185 /* We are positive that no one is using any sect attrs
1186 * at this point. Deallocate immediately. */
1187 free_sect_attrs(mod->sect_attrs);
1188 mod->sect_attrs = NULL;
1193 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1196 struct module_notes_attrs {
1197 struct kobject *dir;
1199 struct bin_attribute attrs[0];
1202 static ssize_t module_notes_read(struct kobject *kobj,
1203 struct bin_attribute *bin_attr,
1204 char *buf, loff_t pos, size_t count)
1207 * The caller checked the pos and count against our size.
1209 memcpy(buf, bin_attr->private + pos, count);
1213 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1216 if (notes_attrs->dir) {
1218 sysfs_remove_bin_file(notes_attrs->dir,
1219 ¬es_attrs->attrs[i]);
1220 kobject_put(notes_attrs->dir);
1225 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1226 char *secstrings, Elf_Shdr *sechdrs)
1228 unsigned int notes, loaded, i;
1229 struct module_notes_attrs *notes_attrs;
1230 struct bin_attribute *nattr;
1232 /* Count notes sections and allocate structures. */
1234 for (i = 0; i < nsect; i++)
1235 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1236 (sechdrs[i].sh_type == SHT_NOTE))
1242 notes_attrs = kzalloc(sizeof(*notes_attrs)
1243 + notes * sizeof(notes_attrs->attrs[0]),
1245 if (notes_attrs == NULL)
1248 notes_attrs->notes = notes;
1249 nattr = ¬es_attrs->attrs[0];
1250 for (loaded = i = 0; i < nsect; ++i) {
1251 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1253 if (sechdrs[i].sh_type == SHT_NOTE) {
1254 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1255 nattr->attr.mode = S_IRUGO;
1256 nattr->size = sechdrs[i].sh_size;
1257 nattr->private = (void *) sechdrs[i].sh_addr;
1258 nattr->read = module_notes_read;
1264 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1265 if (!notes_attrs->dir)
1268 for (i = 0; i < notes; ++i)
1269 if (sysfs_create_bin_file(notes_attrs->dir,
1270 ¬es_attrs->attrs[i]))
1273 mod->notes_attrs = notes_attrs;
1277 free_notes_attrs(notes_attrs, i);
1280 static void remove_notes_attrs(struct module *mod)
1282 if (mod->notes_attrs)
1283 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1288 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1289 char *sectstrings, Elf_Shdr *sechdrs)
1293 static inline void remove_sect_attrs(struct module *mod)
1297 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1298 char *sectstrings, Elf_Shdr *sechdrs)
1302 static inline void remove_notes_attrs(struct module *mod)
1308 int module_add_modinfo_attrs(struct module *mod)
1310 struct module_attribute *attr;
1311 struct module_attribute *temp_attr;
1315 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1316 (ARRAY_SIZE(modinfo_attrs) + 1)),
1318 if (!mod->modinfo_attrs)
1321 temp_attr = mod->modinfo_attrs;
1322 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1324 (attr->test && attr->test(mod))) {
1325 memcpy(temp_attr, attr, sizeof(*temp_attr));
1326 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1333 void module_remove_modinfo_attrs(struct module *mod)
1335 struct module_attribute *attr;
1338 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1339 /* pick a field to test for end of list */
1340 if (!attr->attr.name)
1342 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1346 kfree(mod->modinfo_attrs);
1349 int mod_sysfs_init(struct module *mod)
1352 struct kobject *kobj;
1354 if (!module_sysfs_initialized) {
1355 printk(KERN_ERR "%s: module sysfs not initialized\n",
1361 kobj = kset_find_obj(module_kset, mod->name);
1363 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1369 mod->mkobj.mod = mod;
1371 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1372 mod->mkobj.kobj.kset = module_kset;
1373 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1376 kobject_put(&mod->mkobj.kobj);
1378 /* delay uevent until full sysfs population */
1383 int mod_sysfs_setup(struct module *mod,
1384 struct kernel_param *kparam,
1385 unsigned int num_params)
1389 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1390 if (!mod->holders_dir) {
1395 err = module_param_sysfs_setup(mod, kparam, num_params);
1397 goto out_unreg_holders;
1399 err = module_add_modinfo_attrs(mod);
1401 goto out_unreg_param;
1403 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1407 module_param_sysfs_remove(mod);
1409 kobject_put(mod->holders_dir);
1411 kobject_put(&mod->mkobj.kobj);
1415 static void mod_sysfs_fini(struct module *mod)
1417 kobject_put(&mod->mkobj.kobj);
1420 #else /* CONFIG_SYSFS */
1422 static void mod_sysfs_fini(struct module *mod)
1426 #endif /* CONFIG_SYSFS */
1428 static void mod_kobject_remove(struct module *mod)
1430 module_remove_modinfo_attrs(mod);
1431 module_param_sysfs_remove(mod);
1432 kobject_put(mod->mkobj.drivers_dir);
1433 kobject_put(mod->holders_dir);
1434 mod_sysfs_fini(mod);
1438 * unlink the module with the whole machine is stopped with interrupts off
1439 * - this defends against kallsyms not taking locks
1441 static int __unlink_module(void *_mod)
1443 struct module *mod = _mod;
1444 list_del(&mod->list);
1448 /* Free a module, remove from lists, etc (must hold module_mutex). */
1449 static void free_module(struct module *mod)
1451 /* Delete from various lists */
1452 stop_machine(__unlink_module, mod, NULL);
1453 remove_notes_attrs(mod);
1454 remove_sect_attrs(mod);
1455 mod_kobject_remove(mod);
1457 /* Arch-specific cleanup. */
1458 module_arch_cleanup(mod);
1460 /* Module unload stuff */
1461 module_unload_free(mod);
1463 /* release any pointers to mcount in this module */
1464 ftrace_release(mod->module_core, mod->core_size);
1466 /* This may be NULL, but that's OK */
1467 module_free(mod, mod->module_init);
1470 percpu_modfree(mod->percpu);
1471 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1473 percpu_modfree(mod->refptr);
1475 /* Free lock-classes: */
1476 lockdep_free_key_range(mod->module_core, mod->core_size);
1478 /* Finally, free the core (containing the module structure) */
1479 module_free(mod, mod->module_core);
1482 void *__symbol_get(const char *symbol)
1484 struct module *owner;
1485 unsigned long value;
1488 value = find_symbol(symbol, &owner, NULL, true, true);
1489 if (IS_ERR_VALUE(value))
1491 else if (strong_try_module_get(owner))
1495 return (void *)value;
1497 EXPORT_SYMBOL_GPL(__symbol_get);
1500 * Ensure that an exported symbol [global namespace] does not already exist
1501 * in the kernel or in some other module's exported symbol table.
1503 static int verify_export_symbols(struct module *mod)
1506 struct module *owner;
1507 const struct kernel_symbol *s;
1509 const struct kernel_symbol *sym;
1512 { mod->syms, mod->num_syms },
1513 { mod->gpl_syms, mod->num_gpl_syms },
1514 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1515 #ifdef CONFIG_UNUSED_SYMBOLS
1516 { mod->unused_syms, mod->num_unused_syms },
1517 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1521 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1522 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1523 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1524 NULL, true, false))) {
1526 "%s: exports duplicate symbol %s"
1528 mod->name, s->name, module_name(owner));
1536 /* Change all symbols so that st_value encodes the pointer directly. */
1537 static int simplify_symbols(Elf_Shdr *sechdrs,
1538 unsigned int symindex,
1540 unsigned int versindex,
1541 unsigned int pcpuindex,
1544 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1545 unsigned long secbase;
1546 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1549 for (i = 1; i < n; i++) {
1550 switch (sym[i].st_shndx) {
1552 /* We compiled with -fno-common. These are not
1553 supposed to happen. */
1554 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1555 printk("%s: please compile with -fno-common\n",
1561 /* Don't need to do anything */
1562 DEBUGP("Absolute symbol: 0x%08lx\n",
1563 (long)sym[i].st_value);
1568 = resolve_symbol(sechdrs, versindex,
1569 strtab + sym[i].st_name, mod);
1571 /* Ok if resolved. */
1572 if (!IS_ERR_VALUE(sym[i].st_value))
1575 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1578 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1579 mod->name, strtab + sym[i].st_name);
1584 /* Divert to percpu allocation if a percpu var. */
1585 if (sym[i].st_shndx == pcpuindex)
1586 secbase = (unsigned long)mod->percpu;
1588 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1589 sym[i].st_value += secbase;
1597 /* Additional bytes needed by arch in front of individual sections */
1598 unsigned int __weak arch_mod_section_prepend(struct module *mod,
1599 unsigned int section)
1601 /* default implementation just returns zero */
1605 /* Update size with this section: return offset. */
1606 static long get_offset(struct module *mod, unsigned int *size,
1607 Elf_Shdr *sechdr, unsigned int section)
1611 *size += arch_mod_section_prepend(mod, section);
1612 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1613 *size = ret + sechdr->sh_size;
1617 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1618 might -- code, read-only data, read-write data, small data. Tally
1619 sizes, and place the offsets into sh_entsize fields: high bit means it
1621 static void layout_sections(struct module *mod,
1622 const Elf_Ehdr *hdr,
1624 const char *secstrings)
1626 static unsigned long const masks[][2] = {
1627 /* NOTE: all executable code must be the first section
1628 * in this array; otherwise modify the text_size
1629 * finder in the two loops below */
1630 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1631 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1632 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1633 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1637 for (i = 0; i < hdr->e_shnum; i++)
1638 sechdrs[i].sh_entsize = ~0UL;
1640 DEBUGP("Core section allocation order:\n");
1641 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1642 for (i = 0; i < hdr->e_shnum; ++i) {
1643 Elf_Shdr *s = &sechdrs[i];
1645 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1646 || (s->sh_flags & masks[m][1])
1647 || s->sh_entsize != ~0UL
1648 || strncmp(secstrings + s->sh_name,
1651 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1652 DEBUGP("\t%s\n", secstrings + s->sh_name);
1655 mod->core_text_size = mod->core_size;
1658 DEBUGP("Init section allocation order:\n");
1659 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1660 for (i = 0; i < hdr->e_shnum; ++i) {
1661 Elf_Shdr *s = &sechdrs[i];
1663 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1664 || (s->sh_flags & masks[m][1])
1665 || s->sh_entsize != ~0UL
1666 || strncmp(secstrings + s->sh_name,
1669 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1670 | INIT_OFFSET_MASK);
1671 DEBUGP("\t%s\n", secstrings + s->sh_name);
1674 mod->init_text_size = mod->init_size;
1678 static void set_license(struct module *mod, const char *license)
1681 license = "unspecified";
1683 if (!license_is_gpl_compatible(license)) {
1684 if (!test_taint(TAINT_PROPRIETARY_MODULE))
1685 printk(KERN_WARNING "%s: module license '%s' taints "
1686 "kernel.\n", mod->name, license);
1687 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1691 /* Parse tag=value strings from .modinfo section */
1692 static char *next_string(char *string, unsigned long *secsize)
1694 /* Skip non-zero chars */
1697 if ((*secsize)-- <= 1)
1701 /* Skip any zero padding. */
1702 while (!string[0]) {
1704 if ((*secsize)-- <= 1)
1710 static char *get_modinfo(Elf_Shdr *sechdrs,
1715 unsigned int taglen = strlen(tag);
1716 unsigned long size = sechdrs[info].sh_size;
1718 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1719 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1720 return p + taglen + 1;
1725 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1726 unsigned int infoindex)
1728 struct module_attribute *attr;
1731 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1734 get_modinfo(sechdrs,
1740 #ifdef CONFIG_KALLSYMS
1742 /* lookup symbol in given range of kernel_symbols */
1743 static const struct kernel_symbol *lookup_symbol(const char *name,
1744 const struct kernel_symbol *start,
1745 const struct kernel_symbol *stop)
1747 const struct kernel_symbol *ks = start;
1748 for (; ks < stop; ks++)
1749 if (strcmp(ks->name, name) == 0)
1754 static int is_exported(const char *name, unsigned long value,
1755 const struct module *mod)
1757 const struct kernel_symbol *ks;
1759 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
1761 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1762 return ks != NULL && ks->value == value;
1766 static char elf_type(const Elf_Sym *sym,
1768 const char *secstrings,
1771 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1772 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1777 if (sym->st_shndx == SHN_UNDEF)
1779 if (sym->st_shndx == SHN_ABS)
1781 if (sym->st_shndx >= SHN_LORESERVE)
1783 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1785 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1786 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1787 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1789 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1794 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1795 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1800 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1801 ".debug", strlen(".debug")) == 0)
1806 static void add_kallsyms(struct module *mod,
1808 unsigned int symindex,
1809 unsigned int strindex,
1810 const char *secstrings)
1814 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1815 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1816 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1818 /* Set types up while we still have access to sections. */
1819 for (i = 0; i < mod->num_symtab; i++)
1820 mod->symtab[i].st_info
1821 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1824 static inline void add_kallsyms(struct module *mod,
1826 unsigned int symindex,
1827 unsigned int strindex,
1828 const char *secstrings)
1831 #endif /* CONFIG_KALLSYMS */
1833 static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
1835 #ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
1838 for (i = 0; i < num; i++) {
1839 register_dynamic_debug_module(debug[i].modname,
1841 debug[i].logical_modname,
1842 debug[i].flag_names,
1843 debug[i].hash, debug[i].hash2);
1845 #endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1848 static void *module_alloc_update_bounds(unsigned long size)
1850 void *ret = module_alloc(size);
1853 /* Update module bounds. */
1854 if ((unsigned long)ret < module_addr_min)
1855 module_addr_min = (unsigned long)ret;
1856 if ((unsigned long)ret + size > module_addr_max)
1857 module_addr_max = (unsigned long)ret + size;
1862 /* Allocate and load the module: note that size of section 0 is always
1863 zero, and we rely on this for optional sections. */
1864 static noinline struct module *load_module(void __user *umod,
1866 const char __user *uargs)
1870 char *secstrings, *args, *modmagic, *strtab = NULL;
1873 unsigned int symindex = 0;
1874 unsigned int strindex = 0;
1875 unsigned int modindex, versindex, infoindex, pcpuindex;
1876 unsigned int num_kp, num_mcount;
1877 struct kernel_param *kp;
1880 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1881 unsigned long *mseg;
1882 mm_segment_t old_fs;
1884 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1886 if (len < sizeof(*hdr))
1887 return ERR_PTR(-ENOEXEC);
1889 /* Suck in entire file: we'll want most of it. */
1890 /* vmalloc barfs on "unusual" numbers. Check here */
1891 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1892 return ERR_PTR(-ENOMEM);
1894 /* Create stop_machine threads since the error path relies on
1895 * a non-failing stop_machine call. */
1896 err = stop_machine_create();
1900 if (copy_from_user(hdr, umod, len) != 0) {
1905 /* Sanity checks against insmoding binaries or wrong arch,
1906 weird elf version */
1907 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
1908 || hdr->e_type != ET_REL
1909 || !elf_check_arch(hdr)
1910 || hdr->e_shentsize != sizeof(*sechdrs)) {
1915 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1918 /* Convenience variables */
1919 sechdrs = (void *)hdr + hdr->e_shoff;
1920 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1921 sechdrs[0].sh_addr = 0;
1923 for (i = 1; i < hdr->e_shnum; i++) {
1924 if (sechdrs[i].sh_type != SHT_NOBITS
1925 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1928 /* Mark all sections sh_addr with their address in the
1930 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1932 /* Internal symbols and strings. */
1933 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1935 strindex = sechdrs[i].sh_link;
1936 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1938 #ifndef CONFIG_MODULE_UNLOAD
1939 /* Don't load .exit sections */
1940 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1941 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1945 modindex = find_sec(hdr, sechdrs, secstrings,
1946 ".gnu.linkonce.this_module");
1948 printk(KERN_WARNING "No module found in object\n");
1952 /* This is temporary: point mod into copy of data. */
1953 mod = (void *)sechdrs[modindex].sh_addr;
1955 if (symindex == 0) {
1956 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1962 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1963 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1964 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1966 /* Don't keep modinfo and version sections. */
1967 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1968 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1969 #ifdef CONFIG_KALLSYMS
1970 /* Keep symbol and string tables for decoding later. */
1971 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1972 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1975 /* Check module struct version now, before we try to use module. */
1976 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1981 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1982 /* This is allowed: modprobe --force will invalidate it. */
1984 err = try_to_force_load(mod, "magic");
1987 } else if (!same_magic(modmagic, vermagic, versindex)) {
1988 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1989 mod->name, modmagic, vermagic);
1994 staging = get_modinfo(sechdrs, infoindex, "staging");
1996 add_taint_module(mod, TAINT_CRAP);
1997 printk(KERN_WARNING "%s: module is from the staging directory,"
1998 " the quality is unknown, you have been warned.\n",
2002 /* Now copy in args */
2003 args = strndup_user(uargs, ~0UL >> 1);
2005 err = PTR_ERR(args);
2009 if (find_module(mod->name)) {
2014 mod->state = MODULE_STATE_COMING;
2016 /* Allow arches to frob section contents and sizes. */
2017 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2021 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2022 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2030 /* We have a special allocation for this section. */
2031 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
2032 sechdrs[pcpuindex].sh_addralign,
2038 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2039 mod->percpu = percpu;
2042 /* Determine total sizes, and put offsets in sh_entsize. For now
2043 this is done generically; there doesn't appear to be any
2044 special cases for the architectures. */
2045 layout_sections(mod, hdr, sechdrs, secstrings);
2047 /* Do the allocs. */
2048 ptr = module_alloc_update_bounds(mod->core_size);
2053 memset(ptr, 0, mod->core_size);
2054 mod->module_core = ptr;
2056 ptr = module_alloc_update_bounds(mod->init_size);
2057 if (!ptr && mod->init_size) {
2061 memset(ptr, 0, mod->init_size);
2062 mod->module_init = ptr;
2064 /* Transfer each section which specifies SHF_ALLOC */
2065 DEBUGP("final section addresses:\n");
2066 for (i = 0; i < hdr->e_shnum; i++) {
2069 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2072 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2073 dest = mod->module_init
2074 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2076 dest = mod->module_core + sechdrs[i].sh_entsize;
2078 if (sechdrs[i].sh_type != SHT_NOBITS)
2079 memcpy(dest, (void *)sechdrs[i].sh_addr,
2080 sechdrs[i].sh_size);
2081 /* Update sh_addr to point to copy in image. */
2082 sechdrs[i].sh_addr = (unsigned long)dest;
2083 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2085 /* Module has been moved. */
2086 mod = (void *)sechdrs[modindex].sh_addr;
2088 /* Now we've moved module, initialize linked lists, etc. */
2089 module_unload_init(mod);
2091 /* add kobject, so we can reference it. */
2092 err = mod_sysfs_init(mod);
2096 /* Set up license info based on the info section */
2097 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2100 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2101 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2102 * using GPL-only symbols it needs.
2104 if (strcmp(mod->name, "ndiswrapper") == 0)
2105 add_taint(TAINT_PROPRIETARY_MODULE);
2107 /* driverloader was caught wrongly pretending to be under GPL */
2108 if (strcmp(mod->name, "driverloader") == 0)
2109 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2111 /* Set up MODINFO_ATTR fields */
2112 setup_modinfo(mod, sechdrs, infoindex);
2114 /* Fix up syms, so that st_value is a pointer to location. */
2115 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2120 /* Now we've got everything in the final locations, we can
2121 * find optional sections. */
2122 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2124 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2125 sizeof(*mod->syms), &mod->num_syms);
2126 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2127 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2128 sizeof(*mod->gpl_syms),
2129 &mod->num_gpl_syms);
2130 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2131 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2132 "__ksymtab_gpl_future",
2133 sizeof(*mod->gpl_future_syms),
2134 &mod->num_gpl_future_syms);
2135 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2136 "__kcrctab_gpl_future");
2138 #ifdef CONFIG_UNUSED_SYMBOLS
2139 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2141 sizeof(*mod->unused_syms),
2142 &mod->num_unused_syms);
2143 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2144 "__kcrctab_unused");
2145 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2146 "__ksymtab_unused_gpl",
2147 sizeof(*mod->unused_gpl_syms),
2148 &mod->num_unused_gpl_syms);
2149 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2150 "__kcrctab_unused_gpl");
2153 #ifdef CONFIG_MARKERS
2154 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2155 sizeof(*mod->markers), &mod->num_markers);
2157 #ifdef CONFIG_TRACEPOINTS
2158 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2160 sizeof(*mod->tracepoints),
2161 &mod->num_tracepoints);
2164 #ifdef CONFIG_MODVERSIONS
2165 if ((mod->num_syms && !mod->crcs)
2166 || (mod->num_gpl_syms && !mod->gpl_crcs)
2167 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2168 #ifdef CONFIG_UNUSED_SYMBOLS
2169 || (mod->num_unused_syms && !mod->unused_crcs)
2170 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2173 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2174 err = try_to_force_load(mod, "nocrc");
2180 /* Now do relocations. */
2181 for (i = 1; i < hdr->e_shnum; i++) {
2182 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2183 unsigned int info = sechdrs[i].sh_info;
2185 /* Not a valid relocation section? */
2186 if (info >= hdr->e_shnum)
2189 /* Don't bother with non-allocated sections */
2190 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2193 if (sechdrs[i].sh_type == SHT_REL)
2194 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2195 else if (sechdrs[i].sh_type == SHT_RELA)
2196 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2202 /* Find duplicate symbols */
2203 err = verify_export_symbols(mod);
2207 /* Set up and sort exception table */
2208 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2209 sizeof(*mod->extable), &mod->num_exentries);
2210 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2212 /* Finally, copy percpu area over. */
2213 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2214 sechdrs[pcpuindex].sh_size);
2216 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2219 struct mod_debug *debug;
2220 unsigned int num_debug;
2222 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2223 sizeof(*debug), &num_debug);
2224 dynamic_printk_setup(debug, num_debug);
2227 /* sechdrs[0].sh_size is always zero */
2228 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2229 sizeof(*mseg), &num_mcount);
2230 ftrace_init_module(mod, mseg, mseg + num_mcount);
2232 err = module_finalize(hdr, sechdrs, mod);
2236 /* flush the icache in correct context */
2241 * Flush the instruction cache, since we've played with text.
2242 * Do it before processing of module parameters, so the module
2243 * can provide parameter accessor functions of its own.
2245 if (mod->module_init)
2246 flush_icache_range((unsigned long)mod->module_init,
2247 (unsigned long)mod->module_init
2249 flush_icache_range((unsigned long)mod->module_core,
2250 (unsigned long)mod->module_core + mod->core_size);
2255 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2256 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2259 /* Now sew it into the lists so we can get lockdep and oops
2260 * info during argument parsing. Noone should access us, since
2261 * strong_try_module_get() will fail.
2262 * lockdep/oops can run asynchronous, so use the RCU list insertion
2263 * function to insert in a way safe to concurrent readers.
2264 * The mutex protects against concurrent writers.
2266 list_add_rcu(&mod->list, &modules);
2268 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
2272 err = mod_sysfs_setup(mod, kp, num_kp);
2275 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2276 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2278 /* Get rid of temporary copy */
2281 stop_machine_destroy();
2286 stop_machine(__unlink_module, mod, NULL);
2287 module_arch_cleanup(mod);
2289 kobject_del(&mod->mkobj.kobj);
2290 kobject_put(&mod->mkobj.kobj);
2291 ftrace_release(mod->module_core, mod->core_size);
2293 module_unload_free(mod);
2294 module_free(mod, mod->module_init);
2296 module_free(mod, mod->module_core);
2299 percpu_modfree(percpu);
2300 #if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2301 percpu_modfree(mod->refptr);
2307 stop_machine_destroy();
2308 return ERR_PTR(err);
2311 printk(KERN_ERR "Module len %lu truncated\n", len);
2316 /* This is where the real work happens */
2317 SYSCALL_DEFINE3(init_module, void __user *, umod,
2318 unsigned long, len, const char __user *, uargs)
2323 /* Must have permission */
2324 if (!capable(CAP_SYS_MODULE))
2327 /* Only one module load at a time, please */
2328 if (mutex_lock_interruptible(&module_mutex) != 0)
2331 /* Do all the hard work */
2332 mod = load_module(umod, len, uargs);
2334 mutex_unlock(&module_mutex);
2335 return PTR_ERR(mod);
2338 /* Drop lock so they can recurse */
2339 mutex_unlock(&module_mutex);
2341 blocking_notifier_call_chain(&module_notify_list,
2342 MODULE_STATE_COMING, mod);
2344 /* Start the module */
2345 if (mod->init != NULL)
2346 ret = do_one_initcall(mod->init);
2348 /* Init routine failed: abort. Try to protect us from
2349 buggy refcounters. */
2350 mod->state = MODULE_STATE_GOING;
2351 synchronize_sched();
2353 blocking_notifier_call_chain(&module_notify_list,
2354 MODULE_STATE_GOING, mod);
2355 mutex_lock(&module_mutex);
2357 mutex_unlock(&module_mutex);
2358 wake_up(&module_wq);
2362 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2363 "it should follow 0/-E convention\n"
2364 KERN_WARNING "%s: loading module anyway...\n",
2365 __func__, mod->name, ret,
2370 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2371 mod->state = MODULE_STATE_LIVE;
2372 wake_up(&module_wq);
2373 blocking_notifier_call_chain(&module_notify_list,
2374 MODULE_STATE_LIVE, mod);
2376 mutex_lock(&module_mutex);
2377 /* Drop initial reference. */
2379 module_free(mod, mod->module_init);
2380 mod->module_init = NULL;
2382 mod->init_text_size = 0;
2383 mutex_unlock(&module_mutex);
2388 static inline int within(unsigned long addr, void *start, unsigned long size)
2390 return ((void *)addr >= start && (void *)addr < start + size);
2393 #ifdef CONFIG_KALLSYMS
2395 * This ignores the intensely annoying "mapping symbols" found
2396 * in ARM ELF files: $a, $t and $d.
2398 static inline int is_arm_mapping_symbol(const char *str)
2400 return str[0] == '$' && strchr("atd", str[1])
2401 && (str[2] == '\0' || str[2] == '.');
2404 static const char *get_ksymbol(struct module *mod,
2406 unsigned long *size,
2407 unsigned long *offset)
2409 unsigned int i, best = 0;
2410 unsigned long nextval;
2412 /* At worse, next value is at end of module */
2413 if (within_module_init(addr, mod))
2414 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2416 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2418 /* Scan for closest preceeding symbol, and next symbol. (ELF
2419 starts real symbols at 1). */
2420 for (i = 1; i < mod->num_symtab; i++) {
2421 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2424 /* We ignore unnamed symbols: they're uninformative
2425 * and inserted at a whim. */
2426 if (mod->symtab[i].st_value <= addr
2427 && mod->symtab[i].st_value > mod->symtab[best].st_value
2428 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2429 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2431 if (mod->symtab[i].st_value > addr
2432 && mod->symtab[i].st_value < nextval
2433 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2434 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2435 nextval = mod->symtab[i].st_value;
2442 *size = nextval - mod->symtab[best].st_value;
2444 *offset = addr - mod->symtab[best].st_value;
2445 return mod->strtab + mod->symtab[best].st_name;
2448 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2449 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2450 const char *module_address_lookup(unsigned long addr,
2451 unsigned long *size,
2452 unsigned long *offset,
2457 const char *ret = NULL;
2460 list_for_each_entry_rcu(mod, &modules, list) {
2461 if (within_module_init(addr, mod) ||
2462 within_module_core(addr, mod)) {
2464 *modname = mod->name;
2465 ret = get_ksymbol(mod, addr, size, offset);
2469 /* Make a copy in here where it's safe */
2471 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2478 int lookup_module_symbol_name(unsigned long addr, char *symname)
2483 list_for_each_entry_rcu(mod, &modules, list) {
2484 if (within_module_init(addr, mod) ||
2485 within_module_core(addr, mod)) {
2488 sym = get_ksymbol(mod, addr, NULL, NULL);
2491 strlcpy(symname, sym, KSYM_NAME_LEN);
2501 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2502 unsigned long *offset, char *modname, char *name)
2507 list_for_each_entry_rcu(mod, &modules, list) {
2508 if (within_module_init(addr, mod) ||
2509 within_module_core(addr, mod)) {
2512 sym = get_ksymbol(mod, addr, size, offset);
2516 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2518 strlcpy(name, sym, KSYM_NAME_LEN);
2528 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2529 char *name, char *module_name, int *exported)
2534 list_for_each_entry_rcu(mod, &modules, list) {
2535 if (symnum < mod->num_symtab) {
2536 *value = mod->symtab[symnum].st_value;
2537 *type = mod->symtab[symnum].st_info;
2538 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2540 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2541 *exported = is_exported(name, *value, mod);
2545 symnum -= mod->num_symtab;
2551 static unsigned long mod_find_symname(struct module *mod, const char *name)
2555 for (i = 0; i < mod->num_symtab; i++)
2556 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2557 mod->symtab[i].st_info != 'U')
2558 return mod->symtab[i].st_value;
2562 /* Look for this name: can be of form module:name. */
2563 unsigned long module_kallsyms_lookup_name(const char *name)
2567 unsigned long ret = 0;
2569 /* Don't lock: we're in enough trouble already. */
2571 if ((colon = strchr(name, ':')) != NULL) {
2573 if ((mod = find_module(name)) != NULL)
2574 ret = mod_find_symname(mod, colon+1);
2577 list_for_each_entry_rcu(mod, &modules, list)
2578 if ((ret = mod_find_symname(mod, name)) != 0)
2584 #endif /* CONFIG_KALLSYMS */
2586 static char *module_flags(struct module *mod, char *buf)
2591 mod->state == MODULE_STATE_GOING ||
2592 mod->state == MODULE_STATE_COMING) {
2594 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
2596 if (mod->taints & (1 << TAINT_FORCED_MODULE))
2598 if (mod->taints & (1 << TAINT_CRAP))
2601 * TAINT_FORCED_RMMOD: could be added.
2602 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2606 /* Show a - for module-is-being-unloaded */
2607 if (mod->state == MODULE_STATE_GOING)
2609 /* Show a + for module-is-being-loaded */
2610 if (mod->state == MODULE_STATE_COMING)
2619 #ifdef CONFIG_PROC_FS
2620 /* Called by the /proc file system to return a list of modules. */
2621 static void *m_start(struct seq_file *m, loff_t *pos)
2623 mutex_lock(&module_mutex);
2624 return seq_list_start(&modules, *pos);
2627 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2629 return seq_list_next(p, &modules, pos);
2632 static void m_stop(struct seq_file *m, void *p)
2634 mutex_unlock(&module_mutex);
2637 static int m_show(struct seq_file *m, void *p)
2639 struct module *mod = list_entry(p, struct module, list);
2642 seq_printf(m, "%s %u",
2643 mod->name, mod->init_size + mod->core_size);
2644 print_unload_info(m, mod);
2646 /* Informative for users. */
2647 seq_printf(m, " %s",
2648 mod->state == MODULE_STATE_GOING ? "Unloading":
2649 mod->state == MODULE_STATE_COMING ? "Loading":
2651 /* Used by oprofile and other similar tools. */
2652 seq_printf(m, " 0x%p", mod->module_core);
2656 seq_printf(m, " %s", module_flags(mod, buf));
2658 seq_printf(m, "\n");
2662 /* Format: modulename size refcount deps address
2664 Where refcount is a number or -, and deps is a comma-separated list
2667 static const struct seq_operations modules_op = {
2674 static int modules_open(struct inode *inode, struct file *file)
2676 return seq_open(file, &modules_op);
2679 static const struct file_operations proc_modules_operations = {
2680 .open = modules_open,
2682 .llseek = seq_lseek,
2683 .release = seq_release,
2686 static int __init proc_modules_init(void)
2688 proc_create("modules", 0, NULL, &proc_modules_operations);
2691 module_init(proc_modules_init);
2694 /* Given an address, look for it in the module exception tables. */
2695 const struct exception_table_entry *search_module_extables(unsigned long addr)
2697 const struct exception_table_entry *e = NULL;
2701 list_for_each_entry_rcu(mod, &modules, list) {
2702 if (mod->num_exentries == 0)
2705 e = search_extable(mod->extable,
2706 mod->extable + mod->num_exentries - 1,
2713 /* Now, if we found one, we are running inside it now, hence
2714 we cannot unload the module, hence no refcnt needed. */
2719 * Is this a valid module address?
2721 int is_module_address(unsigned long addr)
2727 list_for_each_entry_rcu(mod, &modules, list) {
2728 if (within_module_core(addr, mod)) {
2740 /* Is this a valid kernel address? */
2741 __notrace_funcgraph struct module *__module_text_address(unsigned long addr)
2745 if (addr < module_addr_min || addr > module_addr_max)
2748 list_for_each_entry_rcu(mod, &modules, list)
2749 if (within(addr, mod->module_init, mod->init_text_size)
2750 || within(addr, mod->module_core, mod->core_text_size))
2755 struct module *module_text_address(unsigned long addr)
2760 mod = __module_text_address(addr);
2766 /* Don't grab lock, we're oopsing. */
2767 void print_modules(void)
2772 printk("Modules linked in:");
2773 /* Most callers should already have preempt disabled, but make sure */
2775 list_for_each_entry_rcu(mod, &modules, list)
2776 printk(" %s%s", mod->name, module_flags(mod, buf));
2778 if (last_unloaded_module[0])
2779 printk(" [last unloaded: %s]", last_unloaded_module);
2783 #ifdef CONFIG_MODVERSIONS
2784 /* Generate the signature for struct module here, too, for modversions. */
2785 void struct_module(struct module *mod) { return; }
2786 EXPORT_SYMBOL(struct_module);
2789 #ifdef CONFIG_MARKERS
2790 void module_update_markers(void)
2794 mutex_lock(&module_mutex);
2795 list_for_each_entry(mod, &modules, list)
2797 marker_update_probe_range(mod->markers,
2798 mod->markers + mod->num_markers);
2799 mutex_unlock(&module_mutex);
2803 #ifdef CONFIG_TRACEPOINTS
2804 void module_update_tracepoints(void)
2808 mutex_lock(&module_mutex);
2809 list_for_each_entry(mod, &modules, list)
2811 tracepoint_update_probe_range(mod->tracepoints,
2812 mod->tracepoints + mod->num_tracepoints);
2813 mutex_unlock(&module_mutex);
2817 * Returns 0 if current not found.
2818 * Returns 1 if current found.
2820 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2822 struct module *iter_mod;
2825 mutex_lock(&module_mutex);
2826 list_for_each_entry(iter_mod, &modules, list) {
2827 if (!iter_mod->taints) {
2829 * Sorted module list
2831 if (iter_mod < iter->module)
2833 else if (iter_mod > iter->module)
2834 iter->tracepoint = NULL;
2835 found = tracepoint_get_iter_range(&iter->tracepoint,
2836 iter_mod->tracepoints,
2837 iter_mod->tracepoints
2838 + iter_mod->num_tracepoints);
2840 iter->module = iter_mod;
2845 mutex_unlock(&module_mutex);