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 <asm/uaccess.h>
46 #include <asm/cacheflush.h>
47 #include <linux/license.h>
48 #include <asm/sections.h>
49 #include <linux/tracepoint.h>
50 #include <linux/ftrace.h>
55 #define DEBUGP(fmt , a...)
58 #ifndef ARCH_SHF_SMALL
59 #define ARCH_SHF_SMALL 0
62 /* If this is set, the section belongs in the init part of the module */
63 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
65 /* List of modules, protected by module_mutex or preempt_disable
66 * (add/delete uses stop_machine). */
67 static DEFINE_MUTEX(module_mutex);
68 static LIST_HEAD(modules);
70 /* Waiting for a module to finish initializing? */
71 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
73 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
75 /* Bounds of module allocation, for speeding __module_text_address */
76 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
78 int register_module_notifier(struct notifier_block * nb)
80 return blocking_notifier_chain_register(&module_notify_list, nb);
82 EXPORT_SYMBOL(register_module_notifier);
84 int unregister_module_notifier(struct notifier_block * nb)
86 return blocking_notifier_chain_unregister(&module_notify_list, nb);
88 EXPORT_SYMBOL(unregister_module_notifier);
90 /* We require a truly strong try_module_get(): 0 means failure due to
91 ongoing or failed initialization etc. */
92 static inline int strong_try_module_get(struct module *mod)
94 if (mod && mod->state == MODULE_STATE_COMING)
96 if (try_module_get(mod))
102 static inline void add_taint_module(struct module *mod, unsigned flag)
105 mod->taints |= (1U << flag);
109 * A thread that wants to hold a reference to a module only while it
110 * is running can call this to safely exit. nfsd and lockd use this.
112 void __module_put_and_exit(struct module *mod, long code)
117 EXPORT_SYMBOL(__module_put_and_exit);
119 /* Find a module section: 0 means not found. */
120 static unsigned int find_sec(Elf_Ehdr *hdr,
122 const char *secstrings,
127 for (i = 1; i < hdr->e_shnum; i++)
128 /* Alloc bit cleared means "ignore it." */
129 if ((sechdrs[i].sh_flags & SHF_ALLOC)
130 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
135 /* Find a module section, or NULL. */
136 static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
137 const char *secstrings, const char *name)
139 /* Section 0 has sh_addr 0. */
140 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
143 /* Find a module section, or NULL. Fill in number of "objects" in section. */
144 static void *section_objs(Elf_Ehdr *hdr,
146 const char *secstrings,
151 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
153 /* Section 0 has sh_addr 0 and sh_size 0. */
154 *num = sechdrs[sec].sh_size / object_size;
155 return (void *)sechdrs[sec].sh_addr;
158 /* Provided by the linker */
159 extern const struct kernel_symbol __start___ksymtab[];
160 extern const struct kernel_symbol __stop___ksymtab[];
161 extern const struct kernel_symbol __start___ksymtab_gpl[];
162 extern const struct kernel_symbol __stop___ksymtab_gpl[];
163 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
164 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
165 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
166 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
167 extern const unsigned long __start___kcrctab[];
168 extern const unsigned long __start___kcrctab_gpl[];
169 extern const unsigned long __start___kcrctab_gpl_future[];
170 #ifdef CONFIG_UNUSED_SYMBOLS
171 extern const struct kernel_symbol __start___ksymtab_unused[];
172 extern const struct kernel_symbol __stop___ksymtab_unused[];
173 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
174 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
175 extern const unsigned long __start___kcrctab_unused[];
176 extern const unsigned long __start___kcrctab_unused_gpl[];
179 #ifndef CONFIG_MODVERSIONS
180 #define symversion(base, idx) NULL
182 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
186 const struct kernel_symbol *start, *stop;
187 const unsigned long *crcs;
196 static bool each_symbol_in_section(const struct symsearch *arr,
197 unsigned int arrsize,
198 struct module *owner,
199 bool (*fn)(const struct symsearch *syms,
200 struct module *owner,
201 unsigned int symnum, void *data),
206 for (j = 0; j < arrsize; j++) {
207 for (i = 0; i < arr[j].stop - arr[j].start; i++)
208 if (fn(&arr[j], owner, i, data))
215 /* Returns true as soon as fn returns true, otherwise false. */
216 static bool each_symbol(bool (*fn)(const struct symsearch *arr,
217 struct module *owner,
218 unsigned int symnum, void *data),
222 const struct symsearch arr[] = {
223 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
224 NOT_GPL_ONLY, false },
225 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
226 __start___kcrctab_gpl,
228 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
229 __start___kcrctab_gpl_future,
230 WILL_BE_GPL_ONLY, false },
231 #ifdef CONFIG_UNUSED_SYMBOLS
232 { __start___ksymtab_unused, __stop___ksymtab_unused,
233 __start___kcrctab_unused,
234 NOT_GPL_ONLY, true },
235 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
236 __start___kcrctab_unused_gpl,
241 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
244 list_for_each_entry(mod, &modules, list) {
245 struct symsearch arr[] = {
246 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
247 NOT_GPL_ONLY, false },
248 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
251 { mod->gpl_future_syms,
252 mod->gpl_future_syms + mod->num_gpl_future_syms,
253 mod->gpl_future_crcs,
254 WILL_BE_GPL_ONLY, false },
255 #ifdef CONFIG_UNUSED_SYMBOLS
257 mod->unused_syms + mod->num_unused_syms,
259 NOT_GPL_ONLY, true },
260 { mod->unused_gpl_syms,
261 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
262 mod->unused_gpl_crcs,
267 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
273 struct find_symbol_arg {
280 struct module *owner;
281 const unsigned long *crc;
285 static bool find_symbol_in_section(const struct symsearch *syms,
286 struct module *owner,
287 unsigned int symnum, void *data)
289 struct find_symbol_arg *fsa = data;
291 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
295 if (syms->licence == GPL_ONLY)
297 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
298 printk(KERN_WARNING "Symbol %s is being used "
299 "by a non-GPL module, which will not "
300 "be allowed in the future\n", fsa->name);
301 printk(KERN_WARNING "Please see the file "
302 "Documentation/feature-removal-schedule.txt "
303 "in the kernel source tree for more details.\n");
307 #ifdef CONFIG_UNUSED_SYMBOLS
308 if (syms->unused && fsa->warn) {
309 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
310 "however this module is using it.\n", fsa->name);
312 "This symbol will go away in the future.\n");
314 "Please evalute if this is the right api to use and if "
315 "it really is, submit a report the linux kernel "
316 "mailinglist together with submitting your code for "
322 fsa->crc = symversion(syms->crcs, symnum);
323 fsa->value = syms->start[symnum].value;
327 /* Find a symbol, return value, (optional) crc and (optional) module
329 static unsigned long find_symbol(const char *name,
330 struct module **owner,
331 const unsigned long **crc,
335 struct find_symbol_arg fsa;
341 if (each_symbol(find_symbol_in_section, &fsa)) {
349 DEBUGP("Failed to find symbol %s\n", name);
353 /* Search for module by name: must hold module_mutex. */
354 static struct module *find_module(const char *name)
358 list_for_each_entry(mod, &modules, list) {
359 if (strcmp(mod->name, name) == 0)
366 /* Number of blocks used and allocated. */
367 static unsigned int pcpu_num_used, pcpu_num_allocated;
368 /* Size of each block. -ve means used. */
369 static int *pcpu_size;
371 static int split_block(unsigned int i, unsigned short size)
373 /* Reallocation required? */
374 if (pcpu_num_used + 1 > pcpu_num_allocated) {
377 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
382 pcpu_num_allocated *= 2;
386 /* Insert a new subblock */
387 memmove(&pcpu_size[i+1], &pcpu_size[i],
388 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
391 pcpu_size[i+1] -= size;
396 static inline unsigned int block_size(int val)
403 static void *percpu_modalloc(unsigned long size, unsigned long align,
410 if (align > PAGE_SIZE) {
411 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
412 name, align, PAGE_SIZE);
416 ptr = __per_cpu_start;
417 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
418 /* Extra for alignment requirement. */
419 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
420 BUG_ON(i == 0 && extra != 0);
422 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
425 /* Transfer extra to previous block. */
426 if (pcpu_size[i-1] < 0)
427 pcpu_size[i-1] -= extra;
429 pcpu_size[i-1] += extra;
430 pcpu_size[i] -= extra;
433 /* Split block if warranted */
434 if (pcpu_size[i] - size > sizeof(unsigned long))
435 if (!split_block(i, size))
439 pcpu_size[i] = -pcpu_size[i];
443 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
448 static void percpu_modfree(void *freeme)
451 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
453 /* First entry is core kernel percpu data. */
454 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
456 pcpu_size[i] = -pcpu_size[i];
463 /* Merge with previous? */
464 if (pcpu_size[i-1] >= 0) {
465 pcpu_size[i-1] += pcpu_size[i];
467 memmove(&pcpu_size[i], &pcpu_size[i+1],
468 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
471 /* Merge with next? */
472 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
473 pcpu_size[i] += pcpu_size[i+1];
475 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
476 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
480 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
482 const char *secstrings)
484 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
487 static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
491 for_each_possible_cpu(cpu)
492 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
495 static int percpu_modinit(void)
498 pcpu_num_allocated = 2;
499 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
501 /* Static in-kernel percpu data (used). */
502 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
504 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
505 if (pcpu_size[1] < 0) {
506 printk(KERN_ERR "No per-cpu room for modules.\n");
512 __initcall(percpu_modinit);
513 #else /* ... !CONFIG_SMP */
514 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
519 static inline void percpu_modfree(void *pcpuptr)
523 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
525 const char *secstrings)
529 static inline void percpu_modcopy(void *pcpudst, const void *src,
532 /* pcpusec should be 0, and size of that section should be 0. */
535 #endif /* CONFIG_SMP */
537 #define MODINFO_ATTR(field) \
538 static void setup_modinfo_##field(struct module *mod, const char *s) \
540 mod->field = kstrdup(s, GFP_KERNEL); \
542 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
543 struct module *mod, char *buffer) \
545 return sprintf(buffer, "%s\n", mod->field); \
547 static int modinfo_##field##_exists(struct module *mod) \
549 return mod->field != NULL; \
551 static void free_modinfo_##field(struct module *mod) \
556 static struct module_attribute modinfo_##field = { \
557 .attr = { .name = __stringify(field), .mode = 0444 }, \
558 .show = show_modinfo_##field, \
559 .setup = setup_modinfo_##field, \
560 .test = modinfo_##field##_exists, \
561 .free = free_modinfo_##field, \
564 MODINFO_ATTR(version);
565 MODINFO_ATTR(srcversion);
567 static char last_unloaded_module[MODULE_NAME_LEN+1];
569 #ifdef CONFIG_MODULE_UNLOAD
570 /* Init the unload section of the module. */
571 static void module_unload_init(struct module *mod)
575 INIT_LIST_HEAD(&mod->modules_which_use_me);
576 for (i = 0; i < NR_CPUS; i++)
577 local_set(&mod->ref[i].count, 0);
578 /* Hold reference count during initialization. */
579 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
580 /* Backwards compatibility macros put refcount during init. */
581 mod->waiter = current;
584 /* modules using other modules */
587 struct list_head list;
588 struct module *module_which_uses;
591 /* Does a already use b? */
592 static int already_uses(struct module *a, struct module *b)
594 struct module_use *use;
596 list_for_each_entry(use, &b->modules_which_use_me, list) {
597 if (use->module_which_uses == a) {
598 DEBUGP("%s uses %s!\n", a->name, b->name);
602 DEBUGP("%s does not use %s!\n", a->name, b->name);
606 /* Module a uses b */
607 static int use_module(struct module *a, struct module *b)
609 struct module_use *use;
612 if (b == NULL || already_uses(a, b)) return 1;
614 /* If we're interrupted or time out, we fail. */
615 if (wait_event_interruptible_timeout(
616 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
618 printk("%s: gave up waiting for init of module %s.\n",
623 /* If strong_try_module_get() returned a different error, we fail. */
627 DEBUGP("Allocating new usage for %s.\n", a->name);
628 use = kmalloc(sizeof(*use), GFP_ATOMIC);
630 printk("%s: out of memory loading\n", a->name);
635 use->module_which_uses = a;
636 list_add(&use->list, &b->modules_which_use_me);
637 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
641 /* Clear the unload stuff of the module. */
642 static void module_unload_free(struct module *mod)
646 list_for_each_entry(i, &modules, list) {
647 struct module_use *use;
649 list_for_each_entry(use, &i->modules_which_use_me, list) {
650 if (use->module_which_uses == mod) {
651 DEBUGP("%s unusing %s\n", mod->name, i->name);
653 list_del(&use->list);
655 sysfs_remove_link(i->holders_dir, mod->name);
656 /* There can be at most one match. */
663 #ifdef CONFIG_MODULE_FORCE_UNLOAD
664 static inline int try_force_unload(unsigned int flags)
666 int ret = (flags & O_TRUNC);
668 add_taint(TAINT_FORCED_RMMOD);
672 static inline int try_force_unload(unsigned int flags)
676 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
685 /* Whole machine is stopped with interrupts off when this runs. */
686 static int __try_stop_module(void *_sref)
688 struct stopref *sref = _sref;
690 /* If it's not unused, quit unless we're forcing. */
691 if (module_refcount(sref->mod) != 0) {
692 if (!(*sref->forced = try_force_unload(sref->flags)))
696 /* Mark it as dying. */
697 sref->mod->state = MODULE_STATE_GOING;
701 static int try_stop_module(struct module *mod, int flags, int *forced)
703 if (flags & O_NONBLOCK) {
704 struct stopref sref = { mod, flags, forced };
706 return stop_machine(__try_stop_module, &sref, NULL);
708 /* We don't need to stop the machine for this. */
709 mod->state = MODULE_STATE_GOING;
715 unsigned int module_refcount(struct module *mod)
717 unsigned int i, total = 0;
719 for (i = 0; i < NR_CPUS; i++)
720 total += local_read(&mod->ref[i].count);
723 EXPORT_SYMBOL(module_refcount);
725 /* This exists whether we can unload or not */
726 static void free_module(struct module *mod);
728 static void wait_for_zero_refcount(struct module *mod)
730 /* Since we might sleep for some time, release the mutex first */
731 mutex_unlock(&module_mutex);
733 DEBUGP("Looking at refcount...\n");
734 set_current_state(TASK_UNINTERRUPTIBLE);
735 if (module_refcount(mod) == 0)
739 current->state = TASK_RUNNING;
740 mutex_lock(&module_mutex);
744 sys_delete_module(const char __user *name_user, unsigned int flags)
747 char name[MODULE_NAME_LEN];
750 if (!capable(CAP_SYS_MODULE))
753 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
755 name[MODULE_NAME_LEN-1] = '\0';
757 if (mutex_lock_interruptible(&module_mutex) != 0)
760 mod = find_module(name);
766 if (!list_empty(&mod->modules_which_use_me)) {
767 /* Other modules depend on us: get rid of them first. */
772 /* Doing init or already dying? */
773 if (mod->state != MODULE_STATE_LIVE) {
774 /* FIXME: if (force), slam module count and wake up
776 DEBUGP("%s already dying\n", mod->name);
781 /* If it has an init func, it must have an exit func to unload */
782 if (mod->init && !mod->exit) {
783 forced = try_force_unload(flags);
785 /* This module can't be removed */
791 /* Set this up before setting mod->state */
792 mod->waiter = current;
794 /* Stop the machine so refcounts can't move and disable module. */
795 ret = try_stop_module(mod, flags, &forced);
799 /* Never wait if forced. */
800 if (!forced && module_refcount(mod) != 0)
801 wait_for_zero_refcount(mod);
803 mutex_unlock(&module_mutex);
804 /* Final destruction now noone is using it. */
805 if (mod->exit != NULL)
807 blocking_notifier_call_chain(&module_notify_list,
808 MODULE_STATE_GOING, mod);
809 mutex_lock(&module_mutex);
810 /* Store the name of the last unloaded module for diagnostic purposes */
811 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
812 unregister_dynamic_debug_module(mod->name);
816 mutex_unlock(&module_mutex);
820 static void print_unload_info(struct seq_file *m, struct module *mod)
822 struct module_use *use;
823 int printed_something = 0;
825 seq_printf(m, " %u ", module_refcount(mod));
827 /* Always include a trailing , so userspace can differentiate
828 between this and the old multi-field proc format. */
829 list_for_each_entry(use, &mod->modules_which_use_me, list) {
830 printed_something = 1;
831 seq_printf(m, "%s,", use->module_which_uses->name);
834 if (mod->init != NULL && mod->exit == NULL) {
835 printed_something = 1;
836 seq_printf(m, "[permanent],");
839 if (!printed_something)
843 void __symbol_put(const char *symbol)
845 struct module *owner;
848 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
853 EXPORT_SYMBOL(__symbol_put);
855 void symbol_put_addr(void *addr)
857 struct module *modaddr;
859 if (core_kernel_text((unsigned long)addr))
862 if (!(modaddr = module_text_address((unsigned long)addr)))
866 EXPORT_SYMBOL_GPL(symbol_put_addr);
868 static ssize_t show_refcnt(struct module_attribute *mattr,
869 struct module *mod, char *buffer)
871 return sprintf(buffer, "%u\n", module_refcount(mod));
874 static struct module_attribute refcnt = {
875 .attr = { .name = "refcnt", .mode = 0444 },
879 void module_put(struct module *module)
882 unsigned int cpu = get_cpu();
883 local_dec(&module->ref[cpu].count);
884 /* Maybe they're waiting for us to drop reference? */
885 if (unlikely(!module_is_live(module)))
886 wake_up_process(module->waiter);
890 EXPORT_SYMBOL(module_put);
892 #else /* !CONFIG_MODULE_UNLOAD */
893 static void print_unload_info(struct seq_file *m, struct module *mod)
895 /* We don't know the usage count, or what modules are using. */
896 seq_printf(m, " - -");
899 static inline void module_unload_free(struct module *mod)
903 static inline int use_module(struct module *a, struct module *b)
905 return strong_try_module_get(b) == 0;
908 static inline void module_unload_init(struct module *mod)
911 #endif /* CONFIG_MODULE_UNLOAD */
913 static ssize_t show_initstate(struct module_attribute *mattr,
914 struct module *mod, char *buffer)
916 const char *state = "unknown";
918 switch (mod->state) {
919 case MODULE_STATE_LIVE:
922 case MODULE_STATE_COMING:
925 case MODULE_STATE_GOING:
929 return sprintf(buffer, "%s\n", state);
932 static struct module_attribute initstate = {
933 .attr = { .name = "initstate", .mode = 0444 },
934 .show = show_initstate,
937 static struct module_attribute *modinfo_attrs[] = {
941 #ifdef CONFIG_MODULE_UNLOAD
947 static const char vermagic[] = VERMAGIC_STRING;
949 static int try_to_force_load(struct module *mod, const char *symname)
951 #ifdef CONFIG_MODULE_FORCE_LOAD
952 if (!test_taint(TAINT_FORCED_MODULE))
953 printk("%s: no version for \"%s\" found: kernel tainted.\n",
955 add_taint_module(mod, TAINT_FORCED_MODULE);
962 #ifdef CONFIG_MODVERSIONS
963 static int check_version(Elf_Shdr *sechdrs,
964 unsigned int versindex,
967 const unsigned long *crc)
969 unsigned int i, num_versions;
970 struct modversion_info *versions;
972 /* Exporting module didn't supply crcs? OK, we're already tainted. */
976 /* No versions at all? modprobe --force does this. */
978 return try_to_force_load(mod, symname) == 0;
980 versions = (void *) sechdrs[versindex].sh_addr;
981 num_versions = sechdrs[versindex].sh_size
982 / sizeof(struct modversion_info);
984 for (i = 0; i < num_versions; i++) {
985 if (strcmp(versions[i].name, symname) != 0)
988 if (versions[i].crc == *crc)
990 DEBUGP("Found checksum %lX vs module %lX\n",
991 *crc, versions[i].crc);
995 printk(KERN_WARNING "%s: no symbol version for %s\n",
1000 printk("%s: disagrees about version of symbol %s\n",
1001 mod->name, symname);
1005 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1006 unsigned int versindex,
1009 const unsigned long *crc;
1011 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
1013 return check_version(sechdrs, versindex, "struct_module", mod, crc);
1016 /* First part is kernel version, which we ignore if module has crcs. */
1017 static inline int same_magic(const char *amagic, const char *bmagic,
1021 amagic += strcspn(amagic, " ");
1022 bmagic += strcspn(bmagic, " ");
1024 return strcmp(amagic, bmagic) == 0;
1027 static inline int check_version(Elf_Shdr *sechdrs,
1028 unsigned int versindex,
1029 const char *symname,
1031 const unsigned long *crc)
1036 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1037 unsigned int versindex,
1043 static inline int same_magic(const char *amagic, const char *bmagic,
1046 return strcmp(amagic, bmagic) == 0;
1048 #endif /* CONFIG_MODVERSIONS */
1050 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1051 Must be holding module_mutex. */
1052 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1053 unsigned int versindex,
1057 struct module *owner;
1059 const unsigned long *crc;
1061 ret = find_symbol(name, &owner, &crc,
1062 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1063 if (!IS_ERR_VALUE(ret)) {
1064 /* use_module can fail due to OOM,
1065 or module initialization or unloading */
1066 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1067 !use_module(mod, owner))
1074 * /sys/module/foo/sections stuff
1075 * J. Corbet <corbet@lwn.net>
1077 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1078 struct module_sect_attr
1080 struct module_attribute mattr;
1082 unsigned long address;
1085 struct module_sect_attrs
1087 struct attribute_group grp;
1088 unsigned int nsections;
1089 struct module_sect_attr attrs[0];
1092 static ssize_t module_sect_show(struct module_attribute *mattr,
1093 struct module *mod, char *buf)
1095 struct module_sect_attr *sattr =
1096 container_of(mattr, struct module_sect_attr, mattr);
1097 return sprintf(buf, "0x%lx\n", sattr->address);
1100 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1102 unsigned int section;
1104 for (section = 0; section < sect_attrs->nsections; section++)
1105 kfree(sect_attrs->attrs[section].name);
1109 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1110 char *secstrings, Elf_Shdr *sechdrs)
1112 unsigned int nloaded = 0, i, size[2];
1113 struct module_sect_attrs *sect_attrs;
1114 struct module_sect_attr *sattr;
1115 struct attribute **gattr;
1117 /* Count loaded sections and allocate structures */
1118 for (i = 0; i < nsect; i++)
1119 if (sechdrs[i].sh_flags & SHF_ALLOC)
1121 size[0] = ALIGN(sizeof(*sect_attrs)
1122 + nloaded * sizeof(sect_attrs->attrs[0]),
1123 sizeof(sect_attrs->grp.attrs[0]));
1124 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1125 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1126 if (sect_attrs == NULL)
1129 /* Setup section attributes. */
1130 sect_attrs->grp.name = "sections";
1131 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1133 sect_attrs->nsections = 0;
1134 sattr = §_attrs->attrs[0];
1135 gattr = §_attrs->grp.attrs[0];
1136 for (i = 0; i < nsect; i++) {
1137 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1139 sattr->address = sechdrs[i].sh_addr;
1140 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1142 if (sattr->name == NULL)
1144 sect_attrs->nsections++;
1145 sattr->mattr.show = module_sect_show;
1146 sattr->mattr.store = NULL;
1147 sattr->mattr.attr.name = sattr->name;
1148 sattr->mattr.attr.mode = S_IRUGO;
1149 *(gattr++) = &(sattr++)->mattr.attr;
1153 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1156 mod->sect_attrs = sect_attrs;
1159 free_sect_attrs(sect_attrs);
1162 static void remove_sect_attrs(struct module *mod)
1164 if (mod->sect_attrs) {
1165 sysfs_remove_group(&mod->mkobj.kobj,
1166 &mod->sect_attrs->grp);
1167 /* We are positive that no one is using any sect attrs
1168 * at this point. Deallocate immediately. */
1169 free_sect_attrs(mod->sect_attrs);
1170 mod->sect_attrs = NULL;
1175 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1178 struct module_notes_attrs {
1179 struct kobject *dir;
1181 struct bin_attribute attrs[0];
1184 static ssize_t module_notes_read(struct kobject *kobj,
1185 struct bin_attribute *bin_attr,
1186 char *buf, loff_t pos, size_t count)
1189 * The caller checked the pos and count against our size.
1191 memcpy(buf, bin_attr->private + pos, count);
1195 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1198 if (notes_attrs->dir) {
1200 sysfs_remove_bin_file(notes_attrs->dir,
1201 ¬es_attrs->attrs[i]);
1202 kobject_put(notes_attrs->dir);
1207 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1208 char *secstrings, Elf_Shdr *sechdrs)
1210 unsigned int notes, loaded, i;
1211 struct module_notes_attrs *notes_attrs;
1212 struct bin_attribute *nattr;
1214 /* Count notes sections and allocate structures. */
1216 for (i = 0; i < nsect; i++)
1217 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1218 (sechdrs[i].sh_type == SHT_NOTE))
1224 notes_attrs = kzalloc(sizeof(*notes_attrs)
1225 + notes * sizeof(notes_attrs->attrs[0]),
1227 if (notes_attrs == NULL)
1230 notes_attrs->notes = notes;
1231 nattr = ¬es_attrs->attrs[0];
1232 for (loaded = i = 0; i < nsect; ++i) {
1233 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1235 if (sechdrs[i].sh_type == SHT_NOTE) {
1236 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1237 nattr->attr.mode = S_IRUGO;
1238 nattr->size = sechdrs[i].sh_size;
1239 nattr->private = (void *) sechdrs[i].sh_addr;
1240 nattr->read = module_notes_read;
1246 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1247 if (!notes_attrs->dir)
1250 for (i = 0; i < notes; ++i)
1251 if (sysfs_create_bin_file(notes_attrs->dir,
1252 ¬es_attrs->attrs[i]))
1255 mod->notes_attrs = notes_attrs;
1259 free_notes_attrs(notes_attrs, i);
1262 static void remove_notes_attrs(struct module *mod)
1264 if (mod->notes_attrs)
1265 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1270 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1271 char *sectstrings, Elf_Shdr *sechdrs)
1275 static inline void remove_sect_attrs(struct module *mod)
1279 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1280 char *sectstrings, Elf_Shdr *sechdrs)
1284 static inline void remove_notes_attrs(struct module *mod)
1290 int module_add_modinfo_attrs(struct module *mod)
1292 struct module_attribute *attr;
1293 struct module_attribute *temp_attr;
1297 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1298 (ARRAY_SIZE(modinfo_attrs) + 1)),
1300 if (!mod->modinfo_attrs)
1303 temp_attr = mod->modinfo_attrs;
1304 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1306 (attr->test && attr->test(mod))) {
1307 memcpy(temp_attr, attr, sizeof(*temp_attr));
1308 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1315 void module_remove_modinfo_attrs(struct module *mod)
1317 struct module_attribute *attr;
1320 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1321 /* pick a field to test for end of list */
1322 if (!attr->attr.name)
1324 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1328 kfree(mod->modinfo_attrs);
1331 int mod_sysfs_init(struct module *mod)
1334 struct kobject *kobj;
1336 if (!module_sysfs_initialized) {
1337 printk(KERN_ERR "%s: module sysfs not initialized\n",
1343 kobj = kset_find_obj(module_kset, mod->name);
1345 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1351 mod->mkobj.mod = mod;
1353 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1354 mod->mkobj.kobj.kset = module_kset;
1355 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1358 kobject_put(&mod->mkobj.kobj);
1360 /* delay uevent until full sysfs population */
1365 int mod_sysfs_setup(struct module *mod,
1366 struct kernel_param *kparam,
1367 unsigned int num_params)
1371 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1372 if (!mod->holders_dir) {
1377 err = module_param_sysfs_setup(mod, kparam, num_params);
1379 goto out_unreg_holders;
1381 err = module_add_modinfo_attrs(mod);
1383 goto out_unreg_param;
1385 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1389 module_param_sysfs_remove(mod);
1391 kobject_put(mod->holders_dir);
1393 kobject_put(&mod->mkobj.kobj);
1397 static void mod_sysfs_fini(struct module *mod)
1399 kobject_put(&mod->mkobj.kobj);
1402 #else /* CONFIG_SYSFS */
1404 static void mod_sysfs_fini(struct module *mod)
1408 #endif /* CONFIG_SYSFS */
1410 static void mod_kobject_remove(struct module *mod)
1412 module_remove_modinfo_attrs(mod);
1413 module_param_sysfs_remove(mod);
1414 kobject_put(mod->mkobj.drivers_dir);
1415 kobject_put(mod->holders_dir);
1416 mod_sysfs_fini(mod);
1420 * link the module with the whole machine is stopped with interrupts off
1421 * - this defends against kallsyms not taking locks
1423 static int __link_module(void *_mod)
1425 struct module *mod = _mod;
1426 list_add(&mod->list, &modules);
1431 * unlink the module with the whole machine is stopped with interrupts off
1432 * - this defends against kallsyms not taking locks
1434 static int __unlink_module(void *_mod)
1436 struct module *mod = _mod;
1437 list_del(&mod->list);
1441 /* Free a module, remove from lists, etc (must hold module_mutex). */
1442 static void free_module(struct module *mod)
1444 /* Delete from various lists */
1445 stop_machine(__unlink_module, mod, NULL);
1446 remove_notes_attrs(mod);
1447 remove_sect_attrs(mod);
1448 mod_kobject_remove(mod);
1450 unwind_remove_table(mod->unwind_info, 0);
1452 /* Arch-specific cleanup. */
1453 module_arch_cleanup(mod);
1455 /* Module unload stuff */
1456 module_unload_free(mod);
1458 /* release any pointers to mcount in this module */
1459 ftrace_release(mod->module_core, mod->core_size);
1461 /* This may be NULL, but that's OK */
1462 module_free(mod, mod->module_init);
1465 percpu_modfree(mod->percpu);
1467 /* Free lock-classes: */
1468 lockdep_free_key_range(mod->module_core, mod->core_size);
1470 /* Finally, free the core (containing the module structure) */
1471 module_free(mod, mod->module_core);
1474 void *__symbol_get(const char *symbol)
1476 struct module *owner;
1477 unsigned long value;
1480 value = find_symbol(symbol, &owner, NULL, true, true);
1481 if (IS_ERR_VALUE(value))
1483 else if (strong_try_module_get(owner))
1487 return (void *)value;
1489 EXPORT_SYMBOL_GPL(__symbol_get);
1492 * Ensure that an exported symbol [global namespace] does not already exist
1493 * in the kernel or in some other module's exported symbol table.
1495 static int verify_export_symbols(struct module *mod)
1498 struct module *owner;
1499 const struct kernel_symbol *s;
1501 const struct kernel_symbol *sym;
1504 { mod->syms, mod->num_syms },
1505 { mod->gpl_syms, mod->num_gpl_syms },
1506 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1507 #ifdef CONFIG_UNUSED_SYMBOLS
1508 { mod->unused_syms, mod->num_unused_syms },
1509 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1513 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1514 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1515 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1516 NULL, true, false))) {
1518 "%s: exports duplicate symbol %s"
1520 mod->name, s->name, module_name(owner));
1528 /* Change all symbols so that st_value encodes the pointer directly. */
1529 static int simplify_symbols(Elf_Shdr *sechdrs,
1530 unsigned int symindex,
1532 unsigned int versindex,
1533 unsigned int pcpuindex,
1536 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1537 unsigned long secbase;
1538 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1541 for (i = 1; i < n; i++) {
1542 switch (sym[i].st_shndx) {
1544 /* We compiled with -fno-common. These are not
1545 supposed to happen. */
1546 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1547 printk("%s: please compile with -fno-common\n",
1553 /* Don't need to do anything */
1554 DEBUGP("Absolute symbol: 0x%08lx\n",
1555 (long)sym[i].st_value);
1560 = resolve_symbol(sechdrs, versindex,
1561 strtab + sym[i].st_name, mod);
1563 /* Ok if resolved. */
1564 if (!IS_ERR_VALUE(sym[i].st_value))
1567 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1570 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1571 mod->name, strtab + sym[i].st_name);
1576 /* Divert to percpu allocation if a percpu var. */
1577 if (sym[i].st_shndx == pcpuindex)
1578 secbase = (unsigned long)mod->percpu;
1580 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1581 sym[i].st_value += secbase;
1589 /* Update size with this section: return offset. */
1590 static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
1594 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1595 *size = ret + sechdr->sh_size;
1599 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1600 might -- code, read-only data, read-write data, small data. Tally
1601 sizes, and place the offsets into sh_entsize fields: high bit means it
1603 static void layout_sections(struct module *mod,
1604 const Elf_Ehdr *hdr,
1606 const char *secstrings)
1608 static unsigned long const masks[][2] = {
1609 /* NOTE: all executable code must be the first section
1610 * in this array; otherwise modify the text_size
1611 * finder in the two loops below */
1612 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1613 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1614 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1615 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1619 for (i = 0; i < hdr->e_shnum; i++)
1620 sechdrs[i].sh_entsize = ~0UL;
1622 DEBUGP("Core section allocation order:\n");
1623 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1624 for (i = 0; i < hdr->e_shnum; ++i) {
1625 Elf_Shdr *s = &sechdrs[i];
1627 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1628 || (s->sh_flags & masks[m][1])
1629 || s->sh_entsize != ~0UL
1630 || strncmp(secstrings + s->sh_name,
1633 s->sh_entsize = get_offset(&mod->core_size, s);
1634 DEBUGP("\t%s\n", secstrings + s->sh_name);
1637 mod->core_text_size = mod->core_size;
1640 DEBUGP("Init 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->init_size, s)
1652 | INIT_OFFSET_MASK);
1653 DEBUGP("\t%s\n", secstrings + s->sh_name);
1656 mod->init_text_size = mod->init_size;
1660 static void set_license(struct module *mod, const char *license)
1663 license = "unspecified";
1665 if (!license_is_gpl_compatible(license)) {
1666 if (!test_taint(TAINT_PROPRIETARY_MODULE))
1667 printk(KERN_WARNING "%s: module license '%s' taints "
1668 "kernel.\n", mod->name, license);
1669 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1673 /* Parse tag=value strings from .modinfo section */
1674 static char *next_string(char *string, unsigned long *secsize)
1676 /* Skip non-zero chars */
1679 if ((*secsize)-- <= 1)
1683 /* Skip any zero padding. */
1684 while (!string[0]) {
1686 if ((*secsize)-- <= 1)
1692 static char *get_modinfo(Elf_Shdr *sechdrs,
1697 unsigned int taglen = strlen(tag);
1698 unsigned long size = sechdrs[info].sh_size;
1700 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1701 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1702 return p + taglen + 1;
1707 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1708 unsigned int infoindex)
1710 struct module_attribute *attr;
1713 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1716 get_modinfo(sechdrs,
1722 #ifdef CONFIG_KALLSYMS
1724 /* lookup symbol in given range of kernel_symbols */
1725 static const struct kernel_symbol *lookup_symbol(const char *name,
1726 const struct kernel_symbol *start,
1727 const struct kernel_symbol *stop)
1729 const struct kernel_symbol *ks = start;
1730 for (; ks < stop; ks++)
1731 if (strcmp(ks->name, name) == 0)
1736 static int is_exported(const char *name, const struct module *mod)
1738 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1741 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1748 static char elf_type(const Elf_Sym *sym,
1750 const char *secstrings,
1753 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1754 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1759 if (sym->st_shndx == SHN_UNDEF)
1761 if (sym->st_shndx == SHN_ABS)
1763 if (sym->st_shndx >= SHN_LORESERVE)
1765 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1767 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1768 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1769 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1771 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1776 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1777 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1782 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1783 ".debug", strlen(".debug")) == 0)
1788 static void add_kallsyms(struct module *mod,
1790 unsigned int symindex,
1791 unsigned int strindex,
1792 const char *secstrings)
1796 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1797 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1798 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1800 /* Set types up while we still have access to sections. */
1801 for (i = 0; i < mod->num_symtab; i++)
1802 mod->symtab[i].st_info
1803 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1806 static inline void add_kallsyms(struct module *mod,
1808 unsigned int symindex,
1809 unsigned int strindex,
1810 const char *secstrings)
1813 #endif /* CONFIG_KALLSYMS */
1815 static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
1817 #ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
1820 for (i = 0; i < num; i++) {
1821 register_dynamic_debug_module(debug[i].modname,
1823 debug[i].logical_modname,
1824 debug[i].flag_names,
1825 debug[i].hash, debug[i].hash2);
1827 #endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1830 static void *module_alloc_update_bounds(unsigned long size)
1832 void *ret = module_alloc(size);
1835 /* Update module bounds. */
1836 if ((unsigned long)ret < module_addr_min)
1837 module_addr_min = (unsigned long)ret;
1838 if ((unsigned long)ret + size > module_addr_max)
1839 module_addr_max = (unsigned long)ret + size;
1844 /* Allocate and load the module: note that size of section 0 is always
1845 zero, and we rely on this for optional sections. */
1846 static noinline struct module *load_module(void __user *umod,
1848 const char __user *uargs)
1852 char *secstrings, *args, *modmagic, *strtab = NULL;
1855 unsigned int symindex = 0;
1856 unsigned int strindex = 0;
1857 unsigned int modindex, versindex, infoindex, pcpuindex;
1858 unsigned int unwindex = 0;
1859 unsigned int num_kp, num_mcount;
1860 struct kernel_param *kp;
1863 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1864 unsigned long *mseg;
1865 mm_segment_t old_fs;
1867 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1869 if (len < sizeof(*hdr))
1870 return ERR_PTR(-ENOEXEC);
1872 /* Suck in entire file: we'll want most of it. */
1873 /* vmalloc barfs on "unusual" numbers. Check here */
1874 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1875 return ERR_PTR(-ENOMEM);
1876 if (copy_from_user(hdr, umod, len) != 0) {
1881 /* Sanity checks against insmoding binaries or wrong arch,
1882 weird elf version */
1883 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
1884 || hdr->e_type != ET_REL
1885 || !elf_check_arch(hdr)
1886 || hdr->e_shentsize != sizeof(*sechdrs)) {
1891 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1894 /* Convenience variables */
1895 sechdrs = (void *)hdr + hdr->e_shoff;
1896 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1897 sechdrs[0].sh_addr = 0;
1899 for (i = 1; i < hdr->e_shnum; i++) {
1900 if (sechdrs[i].sh_type != SHT_NOBITS
1901 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1904 /* Mark all sections sh_addr with their address in the
1906 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1908 /* Internal symbols and strings. */
1909 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1911 strindex = sechdrs[i].sh_link;
1912 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1914 #ifndef CONFIG_MODULE_UNLOAD
1915 /* Don't load .exit sections */
1916 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1917 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1921 modindex = find_sec(hdr, sechdrs, secstrings,
1922 ".gnu.linkonce.this_module");
1924 printk(KERN_WARNING "No module found in object\n");
1928 /* This is temporary: point mod into copy of data. */
1929 mod = (void *)sechdrs[modindex].sh_addr;
1931 if (symindex == 0) {
1932 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1938 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1939 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1940 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1941 #ifdef ARCH_UNWIND_SECTION_NAME
1942 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1945 /* Don't keep modinfo and version sections. */
1946 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1947 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1948 #ifdef CONFIG_KALLSYMS
1949 /* Keep symbol and string tables for decoding later. */
1950 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1951 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1954 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
1956 /* Check module struct version now, before we try to use module. */
1957 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1962 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1963 /* This is allowed: modprobe --force will invalidate it. */
1965 err = try_to_force_load(mod, "magic");
1968 } else if (!same_magic(modmagic, vermagic, versindex)) {
1969 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1970 mod->name, modmagic, vermagic);
1975 staging = get_modinfo(sechdrs, infoindex, "staging");
1977 add_taint_module(mod, TAINT_CRAP);
1978 printk(KERN_WARNING "%s: module is from the staging directory,"
1979 " the quality is unknown, you have been warned.\n",
1983 /* Now copy in args */
1984 args = strndup_user(uargs, ~0UL >> 1);
1986 err = PTR_ERR(args);
1990 if (find_module(mod->name)) {
1995 mod->state = MODULE_STATE_COMING;
1997 /* Allow arches to frob section contents and sizes. */
1998 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2003 /* We have a special allocation for this section. */
2004 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
2005 sechdrs[pcpuindex].sh_addralign,
2011 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2012 mod->percpu = percpu;
2015 /* Determine total sizes, and put offsets in sh_entsize. For now
2016 this is done generically; there doesn't appear to be any
2017 special cases for the architectures. */
2018 layout_sections(mod, hdr, sechdrs, secstrings);
2020 /* Do the allocs. */
2021 ptr = module_alloc_update_bounds(mod->core_size);
2026 memset(ptr, 0, mod->core_size);
2027 mod->module_core = ptr;
2029 ptr = module_alloc_update_bounds(mod->init_size);
2030 if (!ptr && mod->init_size) {
2034 memset(ptr, 0, mod->init_size);
2035 mod->module_init = ptr;
2037 /* Transfer each section which specifies SHF_ALLOC */
2038 DEBUGP("final section addresses:\n");
2039 for (i = 0; i < hdr->e_shnum; i++) {
2042 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2045 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2046 dest = mod->module_init
2047 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2049 dest = mod->module_core + sechdrs[i].sh_entsize;
2051 if (sechdrs[i].sh_type != SHT_NOBITS)
2052 memcpy(dest, (void *)sechdrs[i].sh_addr,
2053 sechdrs[i].sh_size);
2054 /* Update sh_addr to point to copy in image. */
2055 sechdrs[i].sh_addr = (unsigned long)dest;
2056 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2058 /* Module has been moved. */
2059 mod = (void *)sechdrs[modindex].sh_addr;
2061 /* Now we've moved module, initialize linked lists, etc. */
2062 module_unload_init(mod);
2064 /* add kobject, so we can reference it. */
2065 err = mod_sysfs_init(mod);
2069 /* Set up license info based on the info section */
2070 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2073 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2074 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2075 * using GPL-only symbols it needs.
2077 if (strcmp(mod->name, "ndiswrapper") == 0)
2078 add_taint(TAINT_PROPRIETARY_MODULE);
2080 /* driverloader was caught wrongly pretending to be under GPL */
2081 if (strcmp(mod->name, "driverloader") == 0)
2082 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2084 /* Set up MODINFO_ATTR fields */
2085 setup_modinfo(mod, sechdrs, infoindex);
2087 /* Fix up syms, so that st_value is a pointer to location. */
2088 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2093 /* Now we've got everything in the final locations, we can
2094 * find optional sections. */
2095 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2097 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2098 sizeof(*mod->syms), &mod->num_syms);
2099 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2100 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2101 sizeof(*mod->gpl_syms),
2102 &mod->num_gpl_syms);
2103 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2104 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2105 "__ksymtab_gpl_future",
2106 sizeof(*mod->gpl_future_syms),
2107 &mod->num_gpl_future_syms);
2108 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2109 "__kcrctab_gpl_future");
2111 #ifdef CONFIG_UNUSED_SYMBOLS
2112 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2114 sizeof(*mod->unused_syms),
2115 &mod->num_unused_syms);
2116 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2117 "__kcrctab_unused");
2118 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2119 "__ksymtab_unused_gpl",
2120 sizeof(*mod->unused_gpl_syms),
2121 &mod->num_unused_gpl_syms);
2122 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2123 "__kcrctab_unused_gpl");
2126 #ifdef CONFIG_MARKERS
2127 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2128 sizeof(*mod->markers), &mod->num_markers);
2130 #ifdef CONFIG_TRACEPOINTS
2131 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2133 sizeof(*mod->tracepoints),
2134 &mod->num_tracepoints);
2137 #ifdef CONFIG_MODVERSIONS
2138 if ((mod->num_syms && !mod->crcs)
2139 || (mod->num_gpl_syms && !mod->gpl_crcs)
2140 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2141 #ifdef CONFIG_UNUSED_SYMBOLS
2142 || (mod->num_unused_syms && !mod->unused_crcs)
2143 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2146 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2147 err = try_to_force_load(mod, "nocrc");
2153 /* Now do relocations. */
2154 for (i = 1; i < hdr->e_shnum; i++) {
2155 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2156 unsigned int info = sechdrs[i].sh_info;
2158 /* Not a valid relocation section? */
2159 if (info >= hdr->e_shnum)
2162 /* Don't bother with non-allocated sections */
2163 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2166 if (sechdrs[i].sh_type == SHT_REL)
2167 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2168 else if (sechdrs[i].sh_type == SHT_RELA)
2169 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2175 /* Find duplicate symbols */
2176 err = verify_export_symbols(mod);
2180 /* Set up and sort exception table */
2181 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2182 sizeof(*mod->extable), &mod->num_exentries);
2183 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2185 /* Finally, copy percpu area over. */
2186 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2187 sechdrs[pcpuindex].sh_size);
2189 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2192 struct mod_debug *debug;
2193 unsigned int num_debug;
2195 #ifdef CONFIG_MARKERS
2196 marker_update_probe_range(mod->markers,
2197 mod->markers + mod->num_markers);
2199 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2200 sizeof(*debug), &num_debug);
2201 dynamic_printk_setup(debug, num_debug);
2203 #ifdef CONFIG_TRACEPOINTS
2204 tracepoint_update_probe_range(mod->tracepoints,
2205 mod->tracepoints + mod->num_tracepoints);
2209 /* sechdrs[0].sh_size is always zero */
2210 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2211 sizeof(*mseg), &num_mcount);
2212 ftrace_init_module(mseg, mseg + num_mcount);
2214 err = module_finalize(hdr, sechdrs, mod);
2218 /* flush the icache in correct context */
2223 * Flush the instruction cache, since we've played with text.
2224 * Do it before processing of module parameters, so the module
2225 * can provide parameter accessor functions of its own.
2227 if (mod->module_init)
2228 flush_icache_range((unsigned long)mod->module_init,
2229 (unsigned long)mod->module_init
2231 flush_icache_range((unsigned long)mod->module_core,
2232 (unsigned long)mod->module_core + mod->core_size);
2237 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2238 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2241 /* Now sew it into the lists so we can get lockdep and oops
2242 * info during argument parsing. Noone should access us, since
2243 * strong_try_module_get() will fail. */
2244 stop_machine(__link_module, mod, NULL);
2246 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
2250 err = mod_sysfs_setup(mod, kp, num_kp);
2253 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2254 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2256 /* Size of section 0 is 0, so this works well if no unwind info. */
2257 mod->unwind_info = unwind_add_table(mod,
2258 (void *)sechdrs[unwindex].sh_addr,
2259 sechdrs[unwindex].sh_size);
2261 /* Get rid of temporary copy */
2268 stop_machine(__unlink_module, mod, NULL);
2269 module_arch_cleanup(mod);
2271 kobject_del(&mod->mkobj.kobj);
2272 kobject_put(&mod->mkobj.kobj);
2273 ftrace_release(mod->module_core, mod->core_size);
2275 module_unload_free(mod);
2276 module_free(mod, mod->module_init);
2278 module_free(mod, mod->module_core);
2281 percpu_modfree(percpu);
2286 return ERR_PTR(err);
2289 printk(KERN_ERR "Module len %lu truncated\n", len);
2294 /* This is where the real work happens */
2296 sys_init_module(void __user *umod,
2298 const char __user *uargs)
2303 /* Must have permission */
2304 if (!capable(CAP_SYS_MODULE))
2307 /* Only one module load at a time, please */
2308 if (mutex_lock_interruptible(&module_mutex) != 0)
2311 /* Do all the hard work */
2312 mod = load_module(umod, len, uargs);
2314 mutex_unlock(&module_mutex);
2315 return PTR_ERR(mod);
2318 /* Drop lock so they can recurse */
2319 mutex_unlock(&module_mutex);
2321 blocking_notifier_call_chain(&module_notify_list,
2322 MODULE_STATE_COMING, mod);
2324 /* Start the module */
2325 if (mod->init != NULL)
2326 ret = do_one_initcall(mod->init);
2328 /* Init routine failed: abort. Try to protect us from
2329 buggy refcounters. */
2330 mod->state = MODULE_STATE_GOING;
2331 synchronize_sched();
2333 blocking_notifier_call_chain(&module_notify_list,
2334 MODULE_STATE_GOING, mod);
2335 mutex_lock(&module_mutex);
2337 mutex_unlock(&module_mutex);
2338 wake_up(&module_wq);
2342 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2343 "it should follow 0/-E convention\n"
2344 KERN_WARNING "%s: loading module anyway...\n",
2345 __func__, mod->name, ret,
2350 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2351 mod->state = MODULE_STATE_LIVE;
2352 wake_up(&module_wq);
2354 mutex_lock(&module_mutex);
2355 /* Drop initial reference. */
2357 unwind_remove_table(mod->unwind_info, 1);
2358 module_free(mod, mod->module_init);
2359 mod->module_init = NULL;
2361 mod->init_text_size = 0;
2362 mutex_unlock(&module_mutex);
2367 static inline int within(unsigned long addr, void *start, unsigned long size)
2369 return ((void *)addr >= start && (void *)addr < start + size);
2372 #ifdef CONFIG_KALLSYMS
2374 * This ignores the intensely annoying "mapping symbols" found
2375 * in ARM ELF files: $a, $t and $d.
2377 static inline int is_arm_mapping_symbol(const char *str)
2379 return str[0] == '$' && strchr("atd", str[1])
2380 && (str[2] == '\0' || str[2] == '.');
2383 static const char *get_ksymbol(struct module *mod,
2385 unsigned long *size,
2386 unsigned long *offset)
2388 unsigned int i, best = 0;
2389 unsigned long nextval;
2391 /* At worse, next value is at end of module */
2392 if (within(addr, mod->module_init, mod->init_size))
2393 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2395 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2397 /* Scan for closest preceeding symbol, and next symbol. (ELF
2398 starts real symbols at 1). */
2399 for (i = 1; i < mod->num_symtab; i++) {
2400 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2403 /* We ignore unnamed symbols: they're uninformative
2404 * and inserted at a whim. */
2405 if (mod->symtab[i].st_value <= addr
2406 && mod->symtab[i].st_value > mod->symtab[best].st_value
2407 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2408 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2410 if (mod->symtab[i].st_value > addr
2411 && mod->symtab[i].st_value < nextval
2412 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2413 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2414 nextval = mod->symtab[i].st_value;
2421 *size = nextval - mod->symtab[best].st_value;
2423 *offset = addr - mod->symtab[best].st_value;
2424 return mod->strtab + mod->symtab[best].st_name;
2427 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2428 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2429 const char *module_address_lookup(unsigned long addr,
2430 unsigned long *size,
2431 unsigned long *offset,
2436 const char *ret = NULL;
2439 list_for_each_entry(mod, &modules, list) {
2440 if (within(addr, mod->module_init, mod->init_size)
2441 || within(addr, mod->module_core, mod->core_size)) {
2443 *modname = mod->name;
2444 ret = get_ksymbol(mod, addr, size, offset);
2448 /* Make a copy in here where it's safe */
2450 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2457 int lookup_module_symbol_name(unsigned long addr, char *symname)
2462 list_for_each_entry(mod, &modules, list) {
2463 if (within(addr, mod->module_init, mod->init_size) ||
2464 within(addr, mod->module_core, mod->core_size)) {
2467 sym = get_ksymbol(mod, addr, NULL, NULL);
2470 strlcpy(symname, sym, KSYM_NAME_LEN);
2480 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2481 unsigned long *offset, char *modname, char *name)
2486 list_for_each_entry(mod, &modules, list) {
2487 if (within(addr, mod->module_init, mod->init_size) ||
2488 within(addr, mod->module_core, mod->core_size)) {
2491 sym = get_ksymbol(mod, addr, size, offset);
2495 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2497 strlcpy(name, sym, KSYM_NAME_LEN);
2507 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2508 char *name, char *module_name, int *exported)
2513 list_for_each_entry(mod, &modules, list) {
2514 if (symnum < mod->num_symtab) {
2515 *value = mod->symtab[symnum].st_value;
2516 *type = mod->symtab[symnum].st_info;
2517 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2519 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2520 *exported = is_exported(name, mod);
2524 symnum -= mod->num_symtab;
2530 static unsigned long mod_find_symname(struct module *mod, const char *name)
2534 for (i = 0; i < mod->num_symtab; i++)
2535 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2536 mod->symtab[i].st_info != 'U')
2537 return mod->symtab[i].st_value;
2541 /* Look for this name: can be of form module:name. */
2542 unsigned long module_kallsyms_lookup_name(const char *name)
2546 unsigned long ret = 0;
2548 /* Don't lock: we're in enough trouble already. */
2550 if ((colon = strchr(name, ':')) != NULL) {
2552 if ((mod = find_module(name)) != NULL)
2553 ret = mod_find_symname(mod, colon+1);
2556 list_for_each_entry(mod, &modules, list)
2557 if ((ret = mod_find_symname(mod, name)) != 0)
2563 #endif /* CONFIG_KALLSYMS */
2565 /* Called by the /proc file system to return a list of modules. */
2566 static void *m_start(struct seq_file *m, loff_t *pos)
2568 mutex_lock(&module_mutex);
2569 return seq_list_start(&modules, *pos);
2572 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2574 return seq_list_next(p, &modules, pos);
2577 static void m_stop(struct seq_file *m, void *p)
2579 mutex_unlock(&module_mutex);
2582 static char *module_flags(struct module *mod, char *buf)
2587 mod->state == MODULE_STATE_GOING ||
2588 mod->state == MODULE_STATE_COMING) {
2590 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
2592 if (mod->taints & (1 << TAINT_FORCED_MODULE))
2594 if (mod->taints & (1 << TAINT_CRAP))
2597 * TAINT_FORCED_RMMOD: could be added.
2598 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2602 /* Show a - for module-is-being-unloaded */
2603 if (mod->state == MODULE_STATE_GOING)
2605 /* Show a + for module-is-being-loaded */
2606 if (mod->state == MODULE_STATE_COMING)
2615 static int m_show(struct seq_file *m, void *p)
2617 struct module *mod = list_entry(p, struct module, list);
2620 seq_printf(m, "%s %u",
2621 mod->name, mod->init_size + mod->core_size);
2622 print_unload_info(m, mod);
2624 /* Informative for users. */
2625 seq_printf(m, " %s",
2626 mod->state == MODULE_STATE_GOING ? "Unloading":
2627 mod->state == MODULE_STATE_COMING ? "Loading":
2629 /* Used by oprofile and other similar tools. */
2630 seq_printf(m, " 0x%p", mod->module_core);
2634 seq_printf(m, " %s", module_flags(mod, buf));
2636 seq_printf(m, "\n");
2640 /* Format: modulename size refcount deps address
2642 Where refcount is a number or -, and deps is a comma-separated list
2645 const struct seq_operations modules_op = {
2652 /* Given an address, look for it in the module exception tables. */
2653 const struct exception_table_entry *search_module_extables(unsigned long addr)
2655 const struct exception_table_entry *e = NULL;
2659 list_for_each_entry(mod, &modules, list) {
2660 if (mod->num_exentries == 0)
2663 e = search_extable(mod->extable,
2664 mod->extable + mod->num_exentries - 1,
2671 /* Now, if we found one, we are running inside it now, hence
2672 we cannot unload the module, hence no refcnt needed. */
2677 * Is this a valid module address?
2679 int is_module_address(unsigned long addr)
2685 list_for_each_entry(mod, &modules, list) {
2686 if (within(addr, mod->module_core, mod->core_size)) {
2698 /* Is this a valid kernel address? */
2699 struct module *__module_text_address(unsigned long addr)
2703 if (addr < module_addr_min || addr > module_addr_max)
2706 list_for_each_entry(mod, &modules, list)
2707 if (within(addr, mod->module_init, mod->init_text_size)
2708 || within(addr, mod->module_core, mod->core_text_size))
2713 struct module *module_text_address(unsigned long addr)
2718 mod = __module_text_address(addr);
2724 /* Don't grab lock, we're oopsing. */
2725 void print_modules(void)
2730 printk("Modules linked in:");
2731 list_for_each_entry(mod, &modules, list)
2732 printk(" %s%s", mod->name, module_flags(mod, buf));
2733 if (last_unloaded_module[0])
2734 printk(" [last unloaded: %s]", last_unloaded_module);
2738 #ifdef CONFIG_MODVERSIONS
2739 /* Generate the signature for struct module here, too, for modversions. */
2740 void struct_module(struct module *mod) { return; }
2741 EXPORT_SYMBOL(struct_module);
2744 #ifdef CONFIG_MARKERS
2745 void module_update_markers(void)
2749 mutex_lock(&module_mutex);
2750 list_for_each_entry(mod, &modules, list)
2752 marker_update_probe_range(mod->markers,
2753 mod->markers + mod->num_markers);
2754 mutex_unlock(&module_mutex);
2758 #ifdef CONFIG_TRACEPOINTS
2759 void module_update_tracepoints(void)
2763 mutex_lock(&module_mutex);
2764 list_for_each_entry(mod, &modules, list)
2766 tracepoint_update_probe_range(mod->tracepoints,
2767 mod->tracepoints + mod->num_tracepoints);
2768 mutex_unlock(&module_mutex);
2772 * Returns 0 if current not found.
2773 * Returns 1 if current found.
2775 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2777 struct module *iter_mod;
2780 mutex_lock(&module_mutex);
2781 list_for_each_entry(iter_mod, &modules, list) {
2782 if (!iter_mod->taints) {
2784 * Sorted module list
2786 if (iter_mod < iter->module)
2788 else if (iter_mod > iter->module)
2789 iter->tracepoint = NULL;
2790 found = tracepoint_get_iter_range(&iter->tracepoint,
2791 iter_mod->tracepoints,
2792 iter_mod->tracepoints
2793 + iter_mod->num_tracepoints);
2795 iter->module = iter_mod;
2800 mutex_unlock(&module_mutex);