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>
23 #include <linux/sysfs.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/elf.h>
28 #include <linux/seq_file.h>
29 #include <linux/syscalls.h>
30 #include <linux/fcntl.h>
31 #include <linux/rcupdate.h>
32 #include <linux/capability.h>
33 #include <linux/cpu.h>
34 #include <linux/moduleparam.h>
35 #include <linux/errno.h>
36 #include <linux/err.h>
37 #include <linux/vermagic.h>
38 #include <linux/notifier.h>
39 #include <linux/sched.h>
40 #include <linux/stop_machine.h>
41 #include <linux/device.h>
42 #include <linux/string.h>
43 #include <linux/mutex.h>
44 #include <linux/unwind.h>
45 #include <linux/rculist.h>
46 #include <asm/uaccess.h>
47 #include <asm/cacheflush.h>
48 #include <linux/license.h>
49 #include <asm/sections.h>
50 #include <linux/tracepoint.h>
51 #include <linux/ftrace.h>
56 #define DEBUGP(fmt , a...)
59 #ifndef ARCH_SHF_SMALL
60 #define ARCH_SHF_SMALL 0
63 /* If this is set, the section belongs in the init part of the module */
64 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
66 /* List of modules, protected by module_mutex or preempt_disable
67 * (delete uses stop_machine/add uses RCU list operations). */
68 static DEFINE_MUTEX(module_mutex);
69 static LIST_HEAD(modules);
71 /* Waiting for a module to finish initializing? */
72 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
74 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
76 /* Bounds of module allocation, for speeding __module_text_address */
77 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
79 int register_module_notifier(struct notifier_block * nb)
81 return blocking_notifier_chain_register(&module_notify_list, nb);
83 EXPORT_SYMBOL(register_module_notifier);
85 int unregister_module_notifier(struct notifier_block * nb)
87 return blocking_notifier_chain_unregister(&module_notify_list, nb);
89 EXPORT_SYMBOL(unregister_module_notifier);
91 /* We require a truly strong try_module_get(): 0 means failure due to
92 ongoing or failed initialization etc. */
93 static inline int strong_try_module_get(struct module *mod)
95 if (mod && mod->state == MODULE_STATE_COMING)
97 if (try_module_get(mod))
103 static inline void add_taint_module(struct module *mod, unsigned flag)
106 mod->taints |= (1U << flag);
110 * A thread that wants to hold a reference to a module only while it
111 * is running can call this to safely exit. nfsd and lockd use this.
113 void __module_put_and_exit(struct module *mod, long code)
118 EXPORT_SYMBOL(__module_put_and_exit);
120 /* Find a module section: 0 means not found. */
121 static unsigned int find_sec(Elf_Ehdr *hdr,
123 const char *secstrings,
128 for (i = 1; i < hdr->e_shnum; i++)
129 /* Alloc bit cleared means "ignore it." */
130 if ((sechdrs[i].sh_flags & SHF_ALLOC)
131 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
136 /* Find a module section, or NULL. */
137 static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
138 const char *secstrings, const char *name)
140 /* Section 0 has sh_addr 0. */
141 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
144 /* Find a module section, or NULL. Fill in number of "objects" in section. */
145 static void *section_objs(Elf_Ehdr *hdr,
147 const char *secstrings,
152 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
154 /* Section 0 has sh_addr 0 and sh_size 0. */
155 *num = sechdrs[sec].sh_size / object_size;
156 return (void *)sechdrs[sec].sh_addr;
159 /* Provided by the linker */
160 extern const struct kernel_symbol __start___ksymtab[];
161 extern const struct kernel_symbol __stop___ksymtab[];
162 extern const struct kernel_symbol __start___ksymtab_gpl[];
163 extern const struct kernel_symbol __stop___ksymtab_gpl[];
164 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
165 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
166 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
167 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
168 extern const unsigned long __start___kcrctab[];
169 extern const unsigned long __start___kcrctab_gpl[];
170 extern const unsigned long __start___kcrctab_gpl_future[];
171 #ifdef CONFIG_UNUSED_SYMBOLS
172 extern const struct kernel_symbol __start___ksymtab_unused[];
173 extern const struct kernel_symbol __stop___ksymtab_unused[];
174 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
175 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
176 extern const unsigned long __start___kcrctab_unused[];
177 extern const unsigned long __start___kcrctab_unused_gpl[];
180 #ifndef CONFIG_MODVERSIONS
181 #define symversion(base, idx) NULL
183 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
187 const struct kernel_symbol *start, *stop;
188 const unsigned long *crcs;
197 static bool each_symbol_in_section(const struct symsearch *arr,
198 unsigned int arrsize,
199 struct module *owner,
200 bool (*fn)(const struct symsearch *syms,
201 struct module *owner,
202 unsigned int symnum, void *data),
207 for (j = 0; j < arrsize; j++) {
208 for (i = 0; i < arr[j].stop - arr[j].start; i++)
209 if (fn(&arr[j], owner, i, data))
216 /* Returns true as soon as fn returns true, otherwise false. */
217 static bool each_symbol(bool (*fn)(const struct symsearch *arr,
218 struct module *owner,
219 unsigned int symnum, void *data),
223 const struct symsearch arr[] = {
224 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
225 NOT_GPL_ONLY, false },
226 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
227 __start___kcrctab_gpl,
229 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
230 __start___kcrctab_gpl_future,
231 WILL_BE_GPL_ONLY, false },
232 #ifdef CONFIG_UNUSED_SYMBOLS
233 { __start___ksymtab_unused, __stop___ksymtab_unused,
234 __start___kcrctab_unused,
235 NOT_GPL_ONLY, true },
236 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
237 __start___kcrctab_unused_gpl,
242 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
245 list_for_each_entry_rcu(mod, &modules, list) {
246 struct symsearch arr[] = {
247 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
248 NOT_GPL_ONLY, false },
249 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
252 { mod->gpl_future_syms,
253 mod->gpl_future_syms + mod->num_gpl_future_syms,
254 mod->gpl_future_crcs,
255 WILL_BE_GPL_ONLY, false },
256 #ifdef CONFIG_UNUSED_SYMBOLS
258 mod->unused_syms + mod->num_unused_syms,
260 NOT_GPL_ONLY, true },
261 { mod->unused_gpl_syms,
262 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
263 mod->unused_gpl_crcs,
268 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
274 struct find_symbol_arg {
281 struct module *owner;
282 const unsigned long *crc;
286 static bool find_symbol_in_section(const struct symsearch *syms,
287 struct module *owner,
288 unsigned int symnum, void *data)
290 struct find_symbol_arg *fsa = data;
292 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
296 if (syms->licence == GPL_ONLY)
298 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
299 printk(KERN_WARNING "Symbol %s is being used "
300 "by a non-GPL module, which will not "
301 "be allowed in the future\n", fsa->name);
302 printk(KERN_WARNING "Please see the file "
303 "Documentation/feature-removal-schedule.txt "
304 "in the kernel source tree for more details.\n");
308 #ifdef CONFIG_UNUSED_SYMBOLS
309 if (syms->unused && fsa->warn) {
310 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
311 "however this module is using it.\n", fsa->name);
313 "This symbol will go away in the future.\n");
315 "Please evalute if this is the right api to use and if "
316 "it really is, submit a report the linux kernel "
317 "mailinglist together with submitting your code for "
323 fsa->crc = symversion(syms->crcs, symnum);
324 fsa->value = syms->start[symnum].value;
328 /* Find a symbol, return value, (optional) crc and (optional) module
330 static unsigned long find_symbol(const char *name,
331 struct module **owner,
332 const unsigned long **crc,
336 struct find_symbol_arg fsa;
342 if (each_symbol(find_symbol_in_section, &fsa)) {
350 DEBUGP("Failed to find symbol %s\n", name);
354 /* Search for module by name: must hold module_mutex. */
355 static struct module *find_module(const char *name)
359 list_for_each_entry(mod, &modules, list) {
360 if (strcmp(mod->name, name) == 0)
367 /* Number of blocks used and allocated. */
368 static unsigned int pcpu_num_used, pcpu_num_allocated;
369 /* Size of each block. -ve means used. */
370 static int *pcpu_size;
372 static int split_block(unsigned int i, unsigned short size)
374 /* Reallocation required? */
375 if (pcpu_num_used + 1 > pcpu_num_allocated) {
378 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
383 pcpu_num_allocated *= 2;
387 /* Insert a new subblock */
388 memmove(&pcpu_size[i+1], &pcpu_size[i],
389 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
392 pcpu_size[i+1] -= size;
397 static inline unsigned int block_size(int val)
404 static void *percpu_modalloc(unsigned long size, unsigned long align,
411 if (align > PAGE_SIZE) {
412 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
413 name, align, PAGE_SIZE);
417 ptr = __per_cpu_start;
418 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
419 /* Extra for alignment requirement. */
420 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
421 BUG_ON(i == 0 && extra != 0);
423 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
426 /* Transfer extra to previous block. */
427 if (pcpu_size[i-1] < 0)
428 pcpu_size[i-1] -= extra;
430 pcpu_size[i-1] += extra;
431 pcpu_size[i] -= extra;
434 /* Split block if warranted */
435 if (pcpu_size[i] - size > sizeof(unsigned long))
436 if (!split_block(i, size))
440 pcpu_size[i] = -pcpu_size[i];
444 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
449 static void percpu_modfree(void *freeme)
452 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
454 /* First entry is core kernel percpu data. */
455 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
457 pcpu_size[i] = -pcpu_size[i];
464 /* Merge with previous? */
465 if (pcpu_size[i-1] >= 0) {
466 pcpu_size[i-1] += pcpu_size[i];
468 memmove(&pcpu_size[i], &pcpu_size[i+1],
469 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
472 /* Merge with next? */
473 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
474 pcpu_size[i] += pcpu_size[i+1];
476 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
477 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
481 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
483 const char *secstrings)
485 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
488 static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
492 for_each_possible_cpu(cpu)
493 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
496 static int percpu_modinit(void)
499 pcpu_num_allocated = 2;
500 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
502 /* Static in-kernel percpu data (used). */
503 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
505 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
506 if (pcpu_size[1] < 0) {
507 printk(KERN_ERR "No per-cpu room for modules.\n");
513 __initcall(percpu_modinit);
514 #else /* ... !CONFIG_SMP */
515 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
520 static inline void percpu_modfree(void *pcpuptr)
524 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
526 const char *secstrings)
530 static inline void percpu_modcopy(void *pcpudst, const void *src,
533 /* pcpusec should be 0, and size of that section should be 0. */
536 #endif /* CONFIG_SMP */
538 #define MODINFO_ATTR(field) \
539 static void setup_modinfo_##field(struct module *mod, const char *s) \
541 mod->field = kstrdup(s, GFP_KERNEL); \
543 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
544 struct module *mod, char *buffer) \
546 return sprintf(buffer, "%s\n", mod->field); \
548 static int modinfo_##field##_exists(struct module *mod) \
550 return mod->field != NULL; \
552 static void free_modinfo_##field(struct module *mod) \
557 static struct module_attribute modinfo_##field = { \
558 .attr = { .name = __stringify(field), .mode = 0444 }, \
559 .show = show_modinfo_##field, \
560 .setup = setup_modinfo_##field, \
561 .test = modinfo_##field##_exists, \
562 .free = free_modinfo_##field, \
565 MODINFO_ATTR(version);
566 MODINFO_ATTR(srcversion);
568 static char last_unloaded_module[MODULE_NAME_LEN+1];
570 #ifdef CONFIG_MODULE_UNLOAD
571 /* Init the unload section of the module. */
572 static void module_unload_init(struct module *mod)
576 INIT_LIST_HEAD(&mod->modules_which_use_me);
577 for (i = 0; i < NR_CPUS; i++)
578 local_set(&mod->ref[i].count, 0);
579 /* Hold reference count during initialization. */
580 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
581 /* Backwards compatibility macros put refcount during init. */
582 mod->waiter = current;
585 /* modules using other modules */
588 struct list_head list;
589 struct module *module_which_uses;
592 /* Does a already use b? */
593 static int already_uses(struct module *a, struct module *b)
595 struct module_use *use;
597 list_for_each_entry(use, &b->modules_which_use_me, list) {
598 if (use->module_which_uses == a) {
599 DEBUGP("%s uses %s!\n", a->name, b->name);
603 DEBUGP("%s does not use %s!\n", a->name, b->name);
607 /* Module a uses b */
608 static int use_module(struct module *a, struct module *b)
610 struct module_use *use;
613 if (b == NULL || already_uses(a, b)) return 1;
615 /* If we're interrupted or time out, we fail. */
616 if (wait_event_interruptible_timeout(
617 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
619 printk("%s: gave up waiting for init of module %s.\n",
624 /* If strong_try_module_get() returned a different error, we fail. */
628 DEBUGP("Allocating new usage for %s.\n", a->name);
629 use = kmalloc(sizeof(*use), GFP_ATOMIC);
631 printk("%s: out of memory loading\n", a->name);
636 use->module_which_uses = a;
637 list_add(&use->list, &b->modules_which_use_me);
638 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
642 /* Clear the unload stuff of the module. */
643 static void module_unload_free(struct module *mod)
647 list_for_each_entry(i, &modules, list) {
648 struct module_use *use;
650 list_for_each_entry(use, &i->modules_which_use_me, list) {
651 if (use->module_which_uses == mod) {
652 DEBUGP("%s unusing %s\n", mod->name, i->name);
654 list_del(&use->list);
656 sysfs_remove_link(i->holders_dir, mod->name);
657 /* There can be at most one match. */
664 #ifdef CONFIG_MODULE_FORCE_UNLOAD
665 static inline int try_force_unload(unsigned int flags)
667 int ret = (flags & O_TRUNC);
669 add_taint(TAINT_FORCED_RMMOD);
673 static inline int try_force_unload(unsigned int flags)
677 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
686 /* Whole machine is stopped with interrupts off when this runs. */
687 static int __try_stop_module(void *_sref)
689 struct stopref *sref = _sref;
691 /* If it's not unused, quit unless we're forcing. */
692 if (module_refcount(sref->mod) != 0) {
693 if (!(*sref->forced = try_force_unload(sref->flags)))
697 /* Mark it as dying. */
698 sref->mod->state = MODULE_STATE_GOING;
702 static int try_stop_module(struct module *mod, int flags, int *forced)
704 if (flags & O_NONBLOCK) {
705 struct stopref sref = { mod, flags, forced };
707 return stop_machine(__try_stop_module, &sref, NULL);
709 /* We don't need to stop the machine for this. */
710 mod->state = MODULE_STATE_GOING;
716 unsigned int module_refcount(struct module *mod)
718 unsigned int i, total = 0;
720 for (i = 0; i < NR_CPUS; i++)
721 total += local_read(&mod->ref[i].count);
724 EXPORT_SYMBOL(module_refcount);
726 /* This exists whether we can unload or not */
727 static void free_module(struct module *mod);
729 static void wait_for_zero_refcount(struct module *mod)
731 /* Since we might sleep for some time, release the mutex first */
732 mutex_unlock(&module_mutex);
734 DEBUGP("Looking at refcount...\n");
735 set_current_state(TASK_UNINTERRUPTIBLE);
736 if (module_refcount(mod) == 0)
740 current->state = TASK_RUNNING;
741 mutex_lock(&module_mutex);
745 sys_delete_module(const char __user *name_user, unsigned int flags)
748 char name[MODULE_NAME_LEN];
751 if (!capable(CAP_SYS_MODULE))
754 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
756 name[MODULE_NAME_LEN-1] = '\0';
758 if (mutex_lock_interruptible(&module_mutex) != 0)
761 mod = find_module(name);
767 if (!list_empty(&mod->modules_which_use_me)) {
768 /* Other modules depend on us: get rid of them first. */
773 /* Doing init or already dying? */
774 if (mod->state != MODULE_STATE_LIVE) {
775 /* FIXME: if (force), slam module count and wake up
777 DEBUGP("%s already dying\n", mod->name);
782 /* If it has an init func, it must have an exit func to unload */
783 if (mod->init && !mod->exit) {
784 forced = try_force_unload(flags);
786 /* This module can't be removed */
792 /* Set this up before setting mod->state */
793 mod->waiter = current;
795 /* Stop the machine so refcounts can't move and disable module. */
796 ret = try_stop_module(mod, flags, &forced);
800 /* Never wait if forced. */
801 if (!forced && module_refcount(mod) != 0)
802 wait_for_zero_refcount(mod);
804 mutex_unlock(&module_mutex);
805 /* Final destruction now noone is using it. */
806 if (mod->exit != NULL)
808 blocking_notifier_call_chain(&module_notify_list,
809 MODULE_STATE_GOING, mod);
810 mutex_lock(&module_mutex);
811 /* Store the name of the last unloaded module for diagnostic purposes */
812 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
813 unregister_dynamic_debug_module(mod->name);
817 mutex_unlock(&module_mutex);
821 static void print_unload_info(struct seq_file *m, struct module *mod)
823 struct module_use *use;
824 int printed_something = 0;
826 seq_printf(m, " %u ", module_refcount(mod));
828 /* Always include a trailing , so userspace can differentiate
829 between this and the old multi-field proc format. */
830 list_for_each_entry(use, &mod->modules_which_use_me, list) {
831 printed_something = 1;
832 seq_printf(m, "%s,", use->module_which_uses->name);
835 if (mod->init != NULL && mod->exit == NULL) {
836 printed_something = 1;
837 seq_printf(m, "[permanent],");
840 if (!printed_something)
844 void __symbol_put(const char *symbol)
846 struct module *owner;
849 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
854 EXPORT_SYMBOL(__symbol_put);
856 void symbol_put_addr(void *addr)
858 struct module *modaddr;
860 if (core_kernel_text((unsigned long)addr))
863 if (!(modaddr = module_text_address((unsigned long)addr)))
867 EXPORT_SYMBOL_GPL(symbol_put_addr);
869 static ssize_t show_refcnt(struct module_attribute *mattr,
870 struct module *mod, char *buffer)
872 return sprintf(buffer, "%u\n", module_refcount(mod));
875 static struct module_attribute refcnt = {
876 .attr = { .name = "refcnt", .mode = 0444 },
880 void module_put(struct module *module)
883 unsigned int cpu = get_cpu();
884 local_dec(&module->ref[cpu].count);
885 /* Maybe they're waiting for us to drop reference? */
886 if (unlikely(!module_is_live(module)))
887 wake_up_process(module->waiter);
891 EXPORT_SYMBOL(module_put);
893 #else /* !CONFIG_MODULE_UNLOAD */
894 static void print_unload_info(struct seq_file *m, struct module *mod)
896 /* We don't know the usage count, or what modules are using. */
897 seq_printf(m, " - -");
900 static inline void module_unload_free(struct module *mod)
904 static inline int use_module(struct module *a, struct module *b)
906 return strong_try_module_get(b) == 0;
909 static inline void module_unload_init(struct module *mod)
912 #endif /* CONFIG_MODULE_UNLOAD */
914 static ssize_t show_initstate(struct module_attribute *mattr,
915 struct module *mod, char *buffer)
917 const char *state = "unknown";
919 switch (mod->state) {
920 case MODULE_STATE_LIVE:
923 case MODULE_STATE_COMING:
926 case MODULE_STATE_GOING:
930 return sprintf(buffer, "%s\n", state);
933 static struct module_attribute initstate = {
934 .attr = { .name = "initstate", .mode = 0444 },
935 .show = show_initstate,
938 static struct module_attribute *modinfo_attrs[] = {
942 #ifdef CONFIG_MODULE_UNLOAD
948 static const char vermagic[] = VERMAGIC_STRING;
950 static int try_to_force_load(struct module *mod, const char *symname)
952 #ifdef CONFIG_MODULE_FORCE_LOAD
953 if (!test_taint(TAINT_FORCED_MODULE))
954 printk("%s: no version for \"%s\" found: kernel tainted.\n",
956 add_taint_module(mod, TAINT_FORCED_MODULE);
963 #ifdef CONFIG_MODVERSIONS
964 static int check_version(Elf_Shdr *sechdrs,
965 unsigned int versindex,
968 const unsigned long *crc)
970 unsigned int i, num_versions;
971 struct modversion_info *versions;
973 /* Exporting module didn't supply crcs? OK, we're already tainted. */
977 /* No versions at all? modprobe --force does this. */
979 return try_to_force_load(mod, symname) == 0;
981 versions = (void *) sechdrs[versindex].sh_addr;
982 num_versions = sechdrs[versindex].sh_size
983 / sizeof(struct modversion_info);
985 for (i = 0; i < num_versions; i++) {
986 if (strcmp(versions[i].name, symname) != 0)
989 if (versions[i].crc == *crc)
991 DEBUGP("Found checksum %lX vs module %lX\n",
992 *crc, versions[i].crc);
996 printk(KERN_WARNING "%s: no symbol version for %s\n",
1001 printk("%s: disagrees about version of symbol %s\n",
1002 mod->name, symname);
1006 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1007 unsigned int versindex,
1010 const unsigned long *crc;
1012 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
1014 return check_version(sechdrs, versindex, "struct_module", mod, crc);
1017 /* First part is kernel version, which we ignore if module has crcs. */
1018 static inline int same_magic(const char *amagic, const char *bmagic,
1022 amagic += strcspn(amagic, " ");
1023 bmagic += strcspn(bmagic, " ");
1025 return strcmp(amagic, bmagic) == 0;
1028 static inline int check_version(Elf_Shdr *sechdrs,
1029 unsigned int versindex,
1030 const char *symname,
1032 const unsigned long *crc)
1037 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1038 unsigned int versindex,
1044 static inline int same_magic(const char *amagic, const char *bmagic,
1047 return strcmp(amagic, bmagic) == 0;
1049 #endif /* CONFIG_MODVERSIONS */
1051 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1052 Must be holding module_mutex. */
1053 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1054 unsigned int versindex,
1058 struct module *owner;
1060 const unsigned long *crc;
1062 ret = find_symbol(name, &owner, &crc,
1063 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1064 if (!IS_ERR_VALUE(ret)) {
1065 /* use_module can fail due to OOM,
1066 or module initialization or unloading */
1067 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1068 !use_module(mod, owner))
1075 * /sys/module/foo/sections stuff
1076 * J. Corbet <corbet@lwn.net>
1078 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1079 struct module_sect_attr
1081 struct module_attribute mattr;
1083 unsigned long address;
1086 struct module_sect_attrs
1088 struct attribute_group grp;
1089 unsigned int nsections;
1090 struct module_sect_attr attrs[0];
1093 static ssize_t module_sect_show(struct module_attribute *mattr,
1094 struct module *mod, char *buf)
1096 struct module_sect_attr *sattr =
1097 container_of(mattr, struct module_sect_attr, mattr);
1098 return sprintf(buf, "0x%lx\n", sattr->address);
1101 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1103 unsigned int section;
1105 for (section = 0; section < sect_attrs->nsections; section++)
1106 kfree(sect_attrs->attrs[section].name);
1110 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1111 char *secstrings, Elf_Shdr *sechdrs)
1113 unsigned int nloaded = 0, i, size[2];
1114 struct module_sect_attrs *sect_attrs;
1115 struct module_sect_attr *sattr;
1116 struct attribute **gattr;
1118 /* Count loaded sections and allocate structures */
1119 for (i = 0; i < nsect; i++)
1120 if (sechdrs[i].sh_flags & SHF_ALLOC)
1122 size[0] = ALIGN(sizeof(*sect_attrs)
1123 + nloaded * sizeof(sect_attrs->attrs[0]),
1124 sizeof(sect_attrs->grp.attrs[0]));
1125 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1126 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1127 if (sect_attrs == NULL)
1130 /* Setup section attributes. */
1131 sect_attrs->grp.name = "sections";
1132 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1134 sect_attrs->nsections = 0;
1135 sattr = §_attrs->attrs[0];
1136 gattr = §_attrs->grp.attrs[0];
1137 for (i = 0; i < nsect; i++) {
1138 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1140 sattr->address = sechdrs[i].sh_addr;
1141 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1143 if (sattr->name == NULL)
1145 sect_attrs->nsections++;
1146 sattr->mattr.show = module_sect_show;
1147 sattr->mattr.store = NULL;
1148 sattr->mattr.attr.name = sattr->name;
1149 sattr->mattr.attr.mode = S_IRUGO;
1150 *(gattr++) = &(sattr++)->mattr.attr;
1154 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1157 mod->sect_attrs = sect_attrs;
1160 free_sect_attrs(sect_attrs);
1163 static void remove_sect_attrs(struct module *mod)
1165 if (mod->sect_attrs) {
1166 sysfs_remove_group(&mod->mkobj.kobj,
1167 &mod->sect_attrs->grp);
1168 /* We are positive that no one is using any sect attrs
1169 * at this point. Deallocate immediately. */
1170 free_sect_attrs(mod->sect_attrs);
1171 mod->sect_attrs = NULL;
1176 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1179 struct module_notes_attrs {
1180 struct kobject *dir;
1182 struct bin_attribute attrs[0];
1185 static ssize_t module_notes_read(struct kobject *kobj,
1186 struct bin_attribute *bin_attr,
1187 char *buf, loff_t pos, size_t count)
1190 * The caller checked the pos and count against our size.
1192 memcpy(buf, bin_attr->private + pos, count);
1196 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1199 if (notes_attrs->dir) {
1201 sysfs_remove_bin_file(notes_attrs->dir,
1202 ¬es_attrs->attrs[i]);
1203 kobject_put(notes_attrs->dir);
1208 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1209 char *secstrings, Elf_Shdr *sechdrs)
1211 unsigned int notes, loaded, i;
1212 struct module_notes_attrs *notes_attrs;
1213 struct bin_attribute *nattr;
1215 /* Count notes sections and allocate structures. */
1217 for (i = 0; i < nsect; i++)
1218 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1219 (sechdrs[i].sh_type == SHT_NOTE))
1225 notes_attrs = kzalloc(sizeof(*notes_attrs)
1226 + notes * sizeof(notes_attrs->attrs[0]),
1228 if (notes_attrs == NULL)
1231 notes_attrs->notes = notes;
1232 nattr = ¬es_attrs->attrs[0];
1233 for (loaded = i = 0; i < nsect; ++i) {
1234 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1236 if (sechdrs[i].sh_type == SHT_NOTE) {
1237 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1238 nattr->attr.mode = S_IRUGO;
1239 nattr->size = sechdrs[i].sh_size;
1240 nattr->private = (void *) sechdrs[i].sh_addr;
1241 nattr->read = module_notes_read;
1247 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1248 if (!notes_attrs->dir)
1251 for (i = 0; i < notes; ++i)
1252 if (sysfs_create_bin_file(notes_attrs->dir,
1253 ¬es_attrs->attrs[i]))
1256 mod->notes_attrs = notes_attrs;
1260 free_notes_attrs(notes_attrs, i);
1263 static void remove_notes_attrs(struct module *mod)
1265 if (mod->notes_attrs)
1266 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1271 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1272 char *sectstrings, Elf_Shdr *sechdrs)
1276 static inline void remove_sect_attrs(struct module *mod)
1280 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1281 char *sectstrings, Elf_Shdr *sechdrs)
1285 static inline void remove_notes_attrs(struct module *mod)
1291 int module_add_modinfo_attrs(struct module *mod)
1293 struct module_attribute *attr;
1294 struct module_attribute *temp_attr;
1298 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1299 (ARRAY_SIZE(modinfo_attrs) + 1)),
1301 if (!mod->modinfo_attrs)
1304 temp_attr = mod->modinfo_attrs;
1305 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1307 (attr->test && attr->test(mod))) {
1308 memcpy(temp_attr, attr, sizeof(*temp_attr));
1309 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1316 void module_remove_modinfo_attrs(struct module *mod)
1318 struct module_attribute *attr;
1321 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1322 /* pick a field to test for end of list */
1323 if (!attr->attr.name)
1325 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1329 kfree(mod->modinfo_attrs);
1332 int mod_sysfs_init(struct module *mod)
1335 struct kobject *kobj;
1337 if (!module_sysfs_initialized) {
1338 printk(KERN_ERR "%s: module sysfs not initialized\n",
1344 kobj = kset_find_obj(module_kset, mod->name);
1346 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1352 mod->mkobj.mod = mod;
1354 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1355 mod->mkobj.kobj.kset = module_kset;
1356 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1359 kobject_put(&mod->mkobj.kobj);
1361 /* delay uevent until full sysfs population */
1366 int mod_sysfs_setup(struct module *mod,
1367 struct kernel_param *kparam,
1368 unsigned int num_params)
1372 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1373 if (!mod->holders_dir) {
1378 err = module_param_sysfs_setup(mod, kparam, num_params);
1380 goto out_unreg_holders;
1382 err = module_add_modinfo_attrs(mod);
1384 goto out_unreg_param;
1386 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1390 module_param_sysfs_remove(mod);
1392 kobject_put(mod->holders_dir);
1394 kobject_put(&mod->mkobj.kobj);
1398 static void mod_sysfs_fini(struct module *mod)
1400 kobject_put(&mod->mkobj.kobj);
1403 #else /* CONFIG_SYSFS */
1405 static void mod_sysfs_fini(struct module *mod)
1409 #endif /* CONFIG_SYSFS */
1411 static void mod_kobject_remove(struct module *mod)
1413 module_remove_modinfo_attrs(mod);
1414 module_param_sysfs_remove(mod);
1415 kobject_put(mod->mkobj.drivers_dir);
1416 kobject_put(mod->holders_dir);
1417 mod_sysfs_fini(mod);
1421 * unlink the module with the whole machine is stopped with interrupts off
1422 * - this defends against kallsyms not taking locks
1424 static int __unlink_module(void *_mod)
1426 struct module *mod = _mod;
1427 list_del(&mod->list);
1431 /* Free a module, remove from lists, etc (must hold module_mutex). */
1432 static void free_module(struct module *mod)
1434 /* Delete from various lists */
1435 stop_machine(__unlink_module, mod, NULL);
1436 remove_notes_attrs(mod);
1437 remove_sect_attrs(mod);
1438 mod_kobject_remove(mod);
1440 unwind_remove_table(mod->unwind_info, 0);
1442 /* Arch-specific cleanup. */
1443 module_arch_cleanup(mod);
1445 /* Module unload stuff */
1446 module_unload_free(mod);
1448 /* release any pointers to mcount in this module */
1449 ftrace_release(mod->module_core, mod->core_size);
1451 /* This may be NULL, but that's OK */
1452 module_free(mod, mod->module_init);
1455 percpu_modfree(mod->percpu);
1457 /* Free lock-classes: */
1458 lockdep_free_key_range(mod->module_core, mod->core_size);
1460 /* Finally, free the core (containing the module structure) */
1461 module_free(mod, mod->module_core);
1464 void *__symbol_get(const char *symbol)
1466 struct module *owner;
1467 unsigned long value;
1470 value = find_symbol(symbol, &owner, NULL, true, true);
1471 if (IS_ERR_VALUE(value))
1473 else if (strong_try_module_get(owner))
1477 return (void *)value;
1479 EXPORT_SYMBOL_GPL(__symbol_get);
1482 * Ensure that an exported symbol [global namespace] does not already exist
1483 * in the kernel or in some other module's exported symbol table.
1485 static int verify_export_symbols(struct module *mod)
1488 struct module *owner;
1489 const struct kernel_symbol *s;
1491 const struct kernel_symbol *sym;
1494 { mod->syms, mod->num_syms },
1495 { mod->gpl_syms, mod->num_gpl_syms },
1496 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1497 #ifdef CONFIG_UNUSED_SYMBOLS
1498 { mod->unused_syms, mod->num_unused_syms },
1499 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1503 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1504 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1505 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1506 NULL, true, false))) {
1508 "%s: exports duplicate symbol %s"
1510 mod->name, s->name, module_name(owner));
1518 /* Change all symbols so that st_value encodes the pointer directly. */
1519 static int simplify_symbols(Elf_Shdr *sechdrs,
1520 unsigned int symindex,
1522 unsigned int versindex,
1523 unsigned int pcpuindex,
1526 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1527 unsigned long secbase;
1528 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1531 for (i = 1; i < n; i++) {
1532 switch (sym[i].st_shndx) {
1534 /* We compiled with -fno-common. These are not
1535 supposed to happen. */
1536 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1537 printk("%s: please compile with -fno-common\n",
1543 /* Don't need to do anything */
1544 DEBUGP("Absolute symbol: 0x%08lx\n",
1545 (long)sym[i].st_value);
1550 = resolve_symbol(sechdrs, versindex,
1551 strtab + sym[i].st_name, mod);
1553 /* Ok if resolved. */
1554 if (!IS_ERR_VALUE(sym[i].st_value))
1557 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1560 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1561 mod->name, strtab + sym[i].st_name);
1566 /* Divert to percpu allocation if a percpu var. */
1567 if (sym[i].st_shndx == pcpuindex)
1568 secbase = (unsigned long)mod->percpu;
1570 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1571 sym[i].st_value += secbase;
1579 /* Update size with this section: return offset. */
1580 static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
1584 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1585 *size = ret + sechdr->sh_size;
1589 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1590 might -- code, read-only data, read-write data, small data. Tally
1591 sizes, and place the offsets into sh_entsize fields: high bit means it
1593 static void layout_sections(struct module *mod,
1594 const Elf_Ehdr *hdr,
1596 const char *secstrings)
1598 static unsigned long const masks[][2] = {
1599 /* NOTE: all executable code must be the first section
1600 * in this array; otherwise modify the text_size
1601 * finder in the two loops below */
1602 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1603 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1604 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1605 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1609 for (i = 0; i < hdr->e_shnum; i++)
1610 sechdrs[i].sh_entsize = ~0UL;
1612 DEBUGP("Core section allocation order:\n");
1613 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1614 for (i = 0; i < hdr->e_shnum; ++i) {
1615 Elf_Shdr *s = &sechdrs[i];
1617 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1618 || (s->sh_flags & masks[m][1])
1619 || s->sh_entsize != ~0UL
1620 || strncmp(secstrings + s->sh_name,
1623 s->sh_entsize = get_offset(&mod->core_size, s);
1624 DEBUGP("\t%s\n", secstrings + s->sh_name);
1627 mod->core_text_size = mod->core_size;
1630 DEBUGP("Init section allocation order:\n");
1631 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1632 for (i = 0; i < hdr->e_shnum; ++i) {
1633 Elf_Shdr *s = &sechdrs[i];
1635 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1636 || (s->sh_flags & masks[m][1])
1637 || s->sh_entsize != ~0UL
1638 || strncmp(secstrings + s->sh_name,
1641 s->sh_entsize = (get_offset(&mod->init_size, s)
1642 | INIT_OFFSET_MASK);
1643 DEBUGP("\t%s\n", secstrings + s->sh_name);
1646 mod->init_text_size = mod->init_size;
1650 static void set_license(struct module *mod, const char *license)
1653 license = "unspecified";
1655 if (!license_is_gpl_compatible(license)) {
1656 if (!test_taint(TAINT_PROPRIETARY_MODULE))
1657 printk(KERN_WARNING "%s: module license '%s' taints "
1658 "kernel.\n", mod->name, license);
1659 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1663 /* Parse tag=value strings from .modinfo section */
1664 static char *next_string(char *string, unsigned long *secsize)
1666 /* Skip non-zero chars */
1669 if ((*secsize)-- <= 1)
1673 /* Skip any zero padding. */
1674 while (!string[0]) {
1676 if ((*secsize)-- <= 1)
1682 static char *get_modinfo(Elf_Shdr *sechdrs,
1687 unsigned int taglen = strlen(tag);
1688 unsigned long size = sechdrs[info].sh_size;
1690 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1691 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1692 return p + taglen + 1;
1697 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1698 unsigned int infoindex)
1700 struct module_attribute *attr;
1703 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1706 get_modinfo(sechdrs,
1712 #ifdef CONFIG_KALLSYMS
1714 /* lookup symbol in given range of kernel_symbols */
1715 static const struct kernel_symbol *lookup_symbol(const char *name,
1716 const struct kernel_symbol *start,
1717 const struct kernel_symbol *stop)
1719 const struct kernel_symbol *ks = start;
1720 for (; ks < stop; ks++)
1721 if (strcmp(ks->name, name) == 0)
1726 static int is_exported(const char *name, const struct module *mod)
1728 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1731 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1738 static char elf_type(const Elf_Sym *sym,
1740 const char *secstrings,
1743 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1744 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1749 if (sym->st_shndx == SHN_UNDEF)
1751 if (sym->st_shndx == SHN_ABS)
1753 if (sym->st_shndx >= SHN_LORESERVE)
1755 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1757 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1758 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1759 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1761 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1766 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1767 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1772 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1773 ".debug", strlen(".debug")) == 0)
1778 static void add_kallsyms(struct module *mod,
1780 unsigned int symindex,
1781 unsigned int strindex,
1782 const char *secstrings)
1786 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1787 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1788 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1790 /* Set types up while we still have access to sections. */
1791 for (i = 0; i < mod->num_symtab; i++)
1792 mod->symtab[i].st_info
1793 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1796 static inline void add_kallsyms(struct module *mod,
1798 unsigned int symindex,
1799 unsigned int strindex,
1800 const char *secstrings)
1803 #endif /* CONFIG_KALLSYMS */
1805 static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
1807 #ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
1810 for (i = 0; i < num; i++) {
1811 register_dynamic_debug_module(debug[i].modname,
1813 debug[i].logical_modname,
1814 debug[i].flag_names,
1815 debug[i].hash, debug[i].hash2);
1817 #endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1820 static void *module_alloc_update_bounds(unsigned long size)
1822 void *ret = module_alloc(size);
1825 /* Update module bounds. */
1826 if ((unsigned long)ret < module_addr_min)
1827 module_addr_min = (unsigned long)ret;
1828 if ((unsigned long)ret + size > module_addr_max)
1829 module_addr_max = (unsigned long)ret + size;
1834 /* Allocate and load the module: note that size of section 0 is always
1835 zero, and we rely on this for optional sections. */
1836 static noinline struct module *load_module(void __user *umod,
1838 const char __user *uargs)
1842 char *secstrings, *args, *modmagic, *strtab = NULL;
1845 unsigned int symindex = 0;
1846 unsigned int strindex = 0;
1847 unsigned int modindex, versindex, infoindex, pcpuindex;
1848 unsigned int unwindex = 0;
1849 unsigned int num_kp, num_mcount;
1850 struct kernel_param *kp;
1853 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1854 unsigned long *mseg;
1855 mm_segment_t old_fs;
1857 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1859 if (len < sizeof(*hdr))
1860 return ERR_PTR(-ENOEXEC);
1862 /* Suck in entire file: we'll want most of it. */
1863 /* vmalloc barfs on "unusual" numbers. Check here */
1864 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1865 return ERR_PTR(-ENOMEM);
1866 if (copy_from_user(hdr, umod, len) != 0) {
1871 /* Sanity checks against insmoding binaries or wrong arch,
1872 weird elf version */
1873 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
1874 || hdr->e_type != ET_REL
1875 || !elf_check_arch(hdr)
1876 || hdr->e_shentsize != sizeof(*sechdrs)) {
1881 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1884 /* Convenience variables */
1885 sechdrs = (void *)hdr + hdr->e_shoff;
1886 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1887 sechdrs[0].sh_addr = 0;
1889 for (i = 1; i < hdr->e_shnum; i++) {
1890 if (sechdrs[i].sh_type != SHT_NOBITS
1891 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1894 /* Mark all sections sh_addr with their address in the
1896 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1898 /* Internal symbols and strings. */
1899 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1901 strindex = sechdrs[i].sh_link;
1902 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1904 #ifndef CONFIG_MODULE_UNLOAD
1905 /* Don't load .exit sections */
1906 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1907 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1911 modindex = find_sec(hdr, sechdrs, secstrings,
1912 ".gnu.linkonce.this_module");
1914 printk(KERN_WARNING "No module found in object\n");
1918 /* This is temporary: point mod into copy of data. */
1919 mod = (void *)sechdrs[modindex].sh_addr;
1921 if (symindex == 0) {
1922 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1928 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1929 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1930 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1931 #ifdef ARCH_UNWIND_SECTION_NAME
1932 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1935 /* Don't keep modinfo and version sections. */
1936 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1937 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1938 #ifdef CONFIG_KALLSYMS
1939 /* Keep symbol and string tables for decoding later. */
1940 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1941 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1944 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
1946 /* Check module struct version now, before we try to use module. */
1947 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1952 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1953 /* This is allowed: modprobe --force will invalidate it. */
1955 err = try_to_force_load(mod, "magic");
1958 } else if (!same_magic(modmagic, vermagic, versindex)) {
1959 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1960 mod->name, modmagic, vermagic);
1965 staging = get_modinfo(sechdrs, infoindex, "staging");
1967 add_taint_module(mod, TAINT_CRAP);
1968 printk(KERN_WARNING "%s: module is from the staging directory,"
1969 " the quality is unknown, you have been warned.\n",
1973 /* Now copy in args */
1974 args = strndup_user(uargs, ~0UL >> 1);
1976 err = PTR_ERR(args);
1980 if (find_module(mod->name)) {
1985 mod->state = MODULE_STATE_COMING;
1987 /* Allow arches to frob section contents and sizes. */
1988 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1993 /* We have a special allocation for this section. */
1994 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1995 sechdrs[pcpuindex].sh_addralign,
2001 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2002 mod->percpu = percpu;
2005 /* Determine total sizes, and put offsets in sh_entsize. For now
2006 this is done generically; there doesn't appear to be any
2007 special cases for the architectures. */
2008 layout_sections(mod, hdr, sechdrs, secstrings);
2010 /* Do the allocs. */
2011 ptr = module_alloc_update_bounds(mod->core_size);
2016 memset(ptr, 0, mod->core_size);
2017 mod->module_core = ptr;
2019 ptr = module_alloc_update_bounds(mod->init_size);
2020 if (!ptr && mod->init_size) {
2024 memset(ptr, 0, mod->init_size);
2025 mod->module_init = ptr;
2027 /* Transfer each section which specifies SHF_ALLOC */
2028 DEBUGP("final section addresses:\n");
2029 for (i = 0; i < hdr->e_shnum; i++) {
2032 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2035 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2036 dest = mod->module_init
2037 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2039 dest = mod->module_core + sechdrs[i].sh_entsize;
2041 if (sechdrs[i].sh_type != SHT_NOBITS)
2042 memcpy(dest, (void *)sechdrs[i].sh_addr,
2043 sechdrs[i].sh_size);
2044 /* Update sh_addr to point to copy in image. */
2045 sechdrs[i].sh_addr = (unsigned long)dest;
2046 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2048 /* Module has been moved. */
2049 mod = (void *)sechdrs[modindex].sh_addr;
2051 /* Now we've moved module, initialize linked lists, etc. */
2052 module_unload_init(mod);
2054 /* add kobject, so we can reference it. */
2055 err = mod_sysfs_init(mod);
2059 /* Set up license info based on the info section */
2060 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2063 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2064 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2065 * using GPL-only symbols it needs.
2067 if (strcmp(mod->name, "ndiswrapper") == 0)
2068 add_taint(TAINT_PROPRIETARY_MODULE);
2070 /* driverloader was caught wrongly pretending to be under GPL */
2071 if (strcmp(mod->name, "driverloader") == 0)
2072 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2074 /* Set up MODINFO_ATTR fields */
2075 setup_modinfo(mod, sechdrs, infoindex);
2077 /* Fix up syms, so that st_value is a pointer to location. */
2078 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2083 /* Now we've got everything in the final locations, we can
2084 * find optional sections. */
2085 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2087 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2088 sizeof(*mod->syms), &mod->num_syms);
2089 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2090 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2091 sizeof(*mod->gpl_syms),
2092 &mod->num_gpl_syms);
2093 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2094 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2095 "__ksymtab_gpl_future",
2096 sizeof(*mod->gpl_future_syms),
2097 &mod->num_gpl_future_syms);
2098 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2099 "__kcrctab_gpl_future");
2101 #ifdef CONFIG_UNUSED_SYMBOLS
2102 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2104 sizeof(*mod->unused_syms),
2105 &mod->num_unused_syms);
2106 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2107 "__kcrctab_unused");
2108 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2109 "__ksymtab_unused_gpl",
2110 sizeof(*mod->unused_gpl_syms),
2111 &mod->num_unused_gpl_syms);
2112 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2113 "__kcrctab_unused_gpl");
2116 #ifdef CONFIG_MARKERS
2117 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2118 sizeof(*mod->markers), &mod->num_markers);
2120 #ifdef CONFIG_TRACEPOINTS
2121 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2123 sizeof(*mod->tracepoints),
2124 &mod->num_tracepoints);
2127 #ifdef CONFIG_MODVERSIONS
2128 if ((mod->num_syms && !mod->crcs)
2129 || (mod->num_gpl_syms && !mod->gpl_crcs)
2130 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2131 #ifdef CONFIG_UNUSED_SYMBOLS
2132 || (mod->num_unused_syms && !mod->unused_crcs)
2133 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2136 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2137 err = try_to_force_load(mod, "nocrc");
2143 /* Now do relocations. */
2144 for (i = 1; i < hdr->e_shnum; i++) {
2145 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2146 unsigned int info = sechdrs[i].sh_info;
2148 /* Not a valid relocation section? */
2149 if (info >= hdr->e_shnum)
2152 /* Don't bother with non-allocated sections */
2153 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2156 if (sechdrs[i].sh_type == SHT_REL)
2157 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2158 else if (sechdrs[i].sh_type == SHT_RELA)
2159 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2165 /* Find duplicate symbols */
2166 err = verify_export_symbols(mod);
2170 /* Set up and sort exception table */
2171 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2172 sizeof(*mod->extable), &mod->num_exentries);
2173 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2175 /* Finally, copy percpu area over. */
2176 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2177 sechdrs[pcpuindex].sh_size);
2179 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2182 struct mod_debug *debug;
2183 unsigned int num_debug;
2185 #ifdef CONFIG_MARKERS
2186 marker_update_probe_range(mod->markers,
2187 mod->markers + mod->num_markers);
2189 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2190 sizeof(*debug), &num_debug);
2191 dynamic_printk_setup(debug, num_debug);
2193 #ifdef CONFIG_TRACEPOINTS
2194 tracepoint_update_probe_range(mod->tracepoints,
2195 mod->tracepoints + mod->num_tracepoints);
2199 /* sechdrs[0].sh_size is always zero */
2200 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2201 sizeof(*mseg), &num_mcount);
2202 ftrace_init_module(mseg, mseg + num_mcount);
2204 err = module_finalize(hdr, sechdrs, mod);
2208 /* flush the icache in correct context */
2213 * Flush the instruction cache, since we've played with text.
2214 * Do it before processing of module parameters, so the module
2215 * can provide parameter accessor functions of its own.
2217 if (mod->module_init)
2218 flush_icache_range((unsigned long)mod->module_init,
2219 (unsigned long)mod->module_init
2221 flush_icache_range((unsigned long)mod->module_core,
2222 (unsigned long)mod->module_core + mod->core_size);
2227 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2228 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2231 /* Now sew it into the lists so we can get lockdep and oops
2232 * info during argument parsing. Noone should access us, since
2233 * strong_try_module_get() will fail.
2234 * lockdep/oops can run asynchronous, so use the RCU list insertion
2235 * function to insert in a way safe to concurrent readers.
2236 * The mutex protects against concurrent writers.
2238 list_add_rcu(&mod->list, &modules);
2240 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
2244 err = mod_sysfs_setup(mod, kp, num_kp);
2247 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2248 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2250 /* Size of section 0 is 0, so this works well if no unwind info. */
2251 mod->unwind_info = unwind_add_table(mod,
2252 (void *)sechdrs[unwindex].sh_addr,
2253 sechdrs[unwindex].sh_size);
2255 /* Get rid of temporary copy */
2262 stop_machine(__unlink_module, mod, NULL);
2263 module_arch_cleanup(mod);
2265 kobject_del(&mod->mkobj.kobj);
2266 kobject_put(&mod->mkobj.kobj);
2267 ftrace_release(mod->module_core, mod->core_size);
2269 module_unload_free(mod);
2270 module_free(mod, mod->module_init);
2272 module_free(mod, mod->module_core);
2275 percpu_modfree(percpu);
2280 return ERR_PTR(err);
2283 printk(KERN_ERR "Module len %lu truncated\n", len);
2288 /* This is where the real work happens */
2290 sys_init_module(void __user *umod,
2292 const char __user *uargs)
2297 /* Must have permission */
2298 if (!capable(CAP_SYS_MODULE))
2301 /* Only one module load at a time, please */
2302 if (mutex_lock_interruptible(&module_mutex) != 0)
2305 /* Do all the hard work */
2306 mod = load_module(umod, len, uargs);
2308 mutex_unlock(&module_mutex);
2309 return PTR_ERR(mod);
2312 /* Drop lock so they can recurse */
2313 mutex_unlock(&module_mutex);
2315 blocking_notifier_call_chain(&module_notify_list,
2316 MODULE_STATE_COMING, mod);
2318 /* Start the module */
2319 if (mod->init != NULL)
2320 ret = do_one_initcall(mod->init);
2322 /* Init routine failed: abort. Try to protect us from
2323 buggy refcounters. */
2324 mod->state = MODULE_STATE_GOING;
2325 synchronize_sched();
2327 blocking_notifier_call_chain(&module_notify_list,
2328 MODULE_STATE_GOING, mod);
2329 mutex_lock(&module_mutex);
2331 mutex_unlock(&module_mutex);
2332 wake_up(&module_wq);
2336 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2337 "it should follow 0/-E convention\n"
2338 KERN_WARNING "%s: loading module anyway...\n",
2339 __func__, mod->name, ret,
2344 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2345 mod->state = MODULE_STATE_LIVE;
2346 wake_up(&module_wq);
2348 mutex_lock(&module_mutex);
2349 /* Drop initial reference. */
2351 unwind_remove_table(mod->unwind_info, 1);
2352 module_free(mod, mod->module_init);
2353 mod->module_init = NULL;
2355 mod->init_text_size = 0;
2356 mutex_unlock(&module_mutex);
2361 static inline int within(unsigned long addr, void *start, unsigned long size)
2363 return ((void *)addr >= start && (void *)addr < start + size);
2366 #ifdef CONFIG_KALLSYMS
2368 * This ignores the intensely annoying "mapping symbols" found
2369 * in ARM ELF files: $a, $t and $d.
2371 static inline int is_arm_mapping_symbol(const char *str)
2373 return str[0] == '$' && strchr("atd", str[1])
2374 && (str[2] == '\0' || str[2] == '.');
2377 static const char *get_ksymbol(struct module *mod,
2379 unsigned long *size,
2380 unsigned long *offset)
2382 unsigned int i, best = 0;
2383 unsigned long nextval;
2385 /* At worse, next value is at end of module */
2386 if (within(addr, mod->module_init, mod->init_size))
2387 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2389 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2391 /* Scan for closest preceeding symbol, and next symbol. (ELF
2392 starts real symbols at 1). */
2393 for (i = 1; i < mod->num_symtab; i++) {
2394 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2397 /* We ignore unnamed symbols: they're uninformative
2398 * and inserted at a whim. */
2399 if (mod->symtab[i].st_value <= addr
2400 && mod->symtab[i].st_value > mod->symtab[best].st_value
2401 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2402 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2404 if (mod->symtab[i].st_value > addr
2405 && mod->symtab[i].st_value < nextval
2406 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2407 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2408 nextval = mod->symtab[i].st_value;
2415 *size = nextval - mod->symtab[best].st_value;
2417 *offset = addr - mod->symtab[best].st_value;
2418 return mod->strtab + mod->symtab[best].st_name;
2421 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2422 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2423 const char *module_address_lookup(unsigned long addr,
2424 unsigned long *size,
2425 unsigned long *offset,
2430 const char *ret = NULL;
2433 list_for_each_entry_rcu(mod, &modules, list) {
2434 if (within(addr, mod->module_init, mod->init_size)
2435 || within(addr, mod->module_core, mod->core_size)) {
2437 *modname = mod->name;
2438 ret = get_ksymbol(mod, addr, size, offset);
2442 /* Make a copy in here where it's safe */
2444 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2451 int lookup_module_symbol_name(unsigned long addr, char *symname)
2456 list_for_each_entry_rcu(mod, &modules, list) {
2457 if (within(addr, mod->module_init, mod->init_size) ||
2458 within(addr, mod->module_core, mod->core_size)) {
2461 sym = get_ksymbol(mod, addr, NULL, NULL);
2464 strlcpy(symname, sym, KSYM_NAME_LEN);
2474 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2475 unsigned long *offset, char *modname, char *name)
2480 list_for_each_entry_rcu(mod, &modules, list) {
2481 if (within(addr, mod->module_init, mod->init_size) ||
2482 within(addr, mod->module_core, mod->core_size)) {
2485 sym = get_ksymbol(mod, addr, size, offset);
2489 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2491 strlcpy(name, sym, KSYM_NAME_LEN);
2501 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2502 char *name, char *module_name, int *exported)
2507 list_for_each_entry_rcu(mod, &modules, list) {
2508 if (symnum < mod->num_symtab) {
2509 *value = mod->symtab[symnum].st_value;
2510 *type = mod->symtab[symnum].st_info;
2511 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2513 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2514 *exported = is_exported(name, mod);
2518 symnum -= mod->num_symtab;
2524 static unsigned long mod_find_symname(struct module *mod, const char *name)
2528 for (i = 0; i < mod->num_symtab; i++)
2529 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2530 mod->symtab[i].st_info != 'U')
2531 return mod->symtab[i].st_value;
2535 /* Look for this name: can be of form module:name. */
2536 unsigned long module_kallsyms_lookup_name(const char *name)
2540 unsigned long ret = 0;
2542 /* Don't lock: we're in enough trouble already. */
2544 if ((colon = strchr(name, ':')) != NULL) {
2546 if ((mod = find_module(name)) != NULL)
2547 ret = mod_find_symname(mod, colon+1);
2550 list_for_each_entry_rcu(mod, &modules, list)
2551 if ((ret = mod_find_symname(mod, name)) != 0)
2557 #endif /* CONFIG_KALLSYMS */
2559 /* Called by the /proc file system to return a list of modules. */
2560 static void *m_start(struct seq_file *m, loff_t *pos)
2562 mutex_lock(&module_mutex);
2563 return seq_list_start(&modules, *pos);
2566 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2568 return seq_list_next(p, &modules, pos);
2571 static void m_stop(struct seq_file *m, void *p)
2573 mutex_unlock(&module_mutex);
2576 static char *module_flags(struct module *mod, char *buf)
2581 mod->state == MODULE_STATE_GOING ||
2582 mod->state == MODULE_STATE_COMING) {
2584 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
2586 if (mod->taints & (1 << TAINT_FORCED_MODULE))
2588 if (mod->taints & (1 << TAINT_CRAP))
2591 * TAINT_FORCED_RMMOD: could be added.
2592 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2596 /* Show a - for module-is-being-unloaded */
2597 if (mod->state == MODULE_STATE_GOING)
2599 /* Show a + for module-is-being-loaded */
2600 if (mod->state == MODULE_STATE_COMING)
2609 static int m_show(struct seq_file *m, void *p)
2611 struct module *mod = list_entry(p, struct module, list);
2614 seq_printf(m, "%s %u",
2615 mod->name, mod->init_size + mod->core_size);
2616 print_unload_info(m, mod);
2618 /* Informative for users. */
2619 seq_printf(m, " %s",
2620 mod->state == MODULE_STATE_GOING ? "Unloading":
2621 mod->state == MODULE_STATE_COMING ? "Loading":
2623 /* Used by oprofile and other similar tools. */
2624 seq_printf(m, " 0x%p", mod->module_core);
2628 seq_printf(m, " %s", module_flags(mod, buf));
2630 seq_printf(m, "\n");
2634 /* Format: modulename size refcount deps address
2636 Where refcount is a number or -, and deps is a comma-separated list
2639 const struct seq_operations modules_op = {
2646 /* Given an address, look for it in the module exception tables. */
2647 const struct exception_table_entry *search_module_extables(unsigned long addr)
2649 const struct exception_table_entry *e = NULL;
2653 list_for_each_entry_rcu(mod, &modules, list) {
2654 if (mod->num_exentries == 0)
2657 e = search_extable(mod->extable,
2658 mod->extable + mod->num_exentries - 1,
2665 /* Now, if we found one, we are running inside it now, hence
2666 we cannot unload the module, hence no refcnt needed. */
2671 * Is this a valid module address?
2673 int is_module_address(unsigned long addr)
2679 list_for_each_entry_rcu(mod, &modules, list) {
2680 if (within(addr, mod->module_core, mod->core_size)) {
2692 /* Is this a valid kernel address? */
2693 struct module *__module_text_address(unsigned long addr)
2697 if (addr < module_addr_min || addr > module_addr_max)
2700 list_for_each_entry_rcu(mod, &modules, list)
2701 if (within(addr, mod->module_init, mod->init_text_size)
2702 || within(addr, mod->module_core, mod->core_text_size))
2707 struct module *module_text_address(unsigned long addr)
2712 mod = __module_text_address(addr);
2718 /* Don't grab lock, we're oopsing. */
2719 void print_modules(void)
2724 printk("Modules linked in:");
2725 /* Most callers should already have preempt disabled, but make sure */
2727 list_for_each_entry_rcu(mod, &modules, list)
2728 printk(" %s%s", mod->name, module_flags(mod, buf));
2730 if (last_unloaded_module[0])
2731 printk(" [last unloaded: %s]", last_unloaded_module);
2735 #ifdef CONFIG_MODVERSIONS
2736 /* Generate the signature for struct module here, too, for modversions. */
2737 void struct_module(struct module *mod) { return; }
2738 EXPORT_SYMBOL(struct_module);
2741 #ifdef CONFIG_MARKERS
2742 void module_update_markers(void)
2746 mutex_lock(&module_mutex);
2747 list_for_each_entry(mod, &modules, list)
2749 marker_update_probe_range(mod->markers,
2750 mod->markers + mod->num_markers);
2751 mutex_unlock(&module_mutex);
2755 #ifdef CONFIG_TRACEPOINTS
2756 void module_update_tracepoints(void)
2760 mutex_lock(&module_mutex);
2761 list_for_each_entry(mod, &modules, list)
2763 tracepoint_update_probe_range(mod->tracepoints,
2764 mod->tracepoints + mod->num_tracepoints);
2765 mutex_unlock(&module_mutex);
2769 * Returns 0 if current not found.
2770 * Returns 1 if current found.
2772 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2774 struct module *iter_mod;
2777 mutex_lock(&module_mutex);
2778 list_for_each_entry(iter_mod, &modules, list) {
2779 if (!iter_mod->taints) {
2781 * Sorted module list
2783 if (iter_mod < iter->module)
2785 else if (iter_mod > iter->module)
2786 iter->tracepoint = NULL;
2787 found = tracepoint_get_iter_range(&iter->tracepoint,
2788 iter_mod->tracepoints,
2789 iter_mod->tracepoints
2790 + iter_mod->num_tracepoints);
2792 iter->module = iter_mod;
2797 mutex_unlock(&module_mutex);