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/semaphore.h>
47 #include <asm/cacheflush.h>
48 #include <linux/license.h>
50 extern int module_sysfs_initialized;
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 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
72 int register_module_notifier(struct notifier_block * nb)
74 return blocking_notifier_chain_register(&module_notify_list, nb);
76 EXPORT_SYMBOL(register_module_notifier);
78 int unregister_module_notifier(struct notifier_block * nb)
80 return blocking_notifier_chain_unregister(&module_notify_list, nb);
82 EXPORT_SYMBOL(unregister_module_notifier);
84 /* We require a truly strong try_module_get() */
85 static inline int strong_try_module_get(struct module *mod)
87 if (mod && mod->state == MODULE_STATE_COMING)
89 return try_module_get(mod);
92 static inline void add_taint_module(struct module *mod, unsigned flag)
99 * A thread that wants to hold a reference to a module only while it
100 * is running can call this to safely exit. nfsd and lockd use this.
102 void __module_put_and_exit(struct module *mod, long code)
107 EXPORT_SYMBOL(__module_put_and_exit);
109 /* Find a module section: 0 means not found. */
110 static unsigned int find_sec(Elf_Ehdr *hdr,
112 const char *secstrings,
117 for (i = 1; i < hdr->e_shnum; i++)
118 /* Alloc bit cleared means "ignore it." */
119 if ((sechdrs[i].sh_flags & SHF_ALLOC)
120 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
125 /* Provided by the linker */
126 extern const struct kernel_symbol __start___ksymtab[];
127 extern const struct kernel_symbol __stop___ksymtab[];
128 extern const struct kernel_symbol __start___ksymtab_gpl[];
129 extern const struct kernel_symbol __stop___ksymtab_gpl[];
130 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
131 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
132 extern const struct kernel_symbol __start___ksymtab_unused[];
133 extern const struct kernel_symbol __stop___ksymtab_unused[];
134 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
135 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
136 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
137 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
138 extern const unsigned long __start___kcrctab[];
139 extern const unsigned long __start___kcrctab_gpl[];
140 extern const unsigned long __start___kcrctab_gpl_future[];
141 extern const unsigned long __start___kcrctab_unused[];
142 extern const unsigned long __start___kcrctab_unused_gpl[];
144 #ifndef CONFIG_MODVERSIONS
145 #define symversion(base, idx) NULL
147 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
150 /* lookup symbol in given range of kernel_symbols */
151 static const struct kernel_symbol *lookup_symbol(const char *name,
152 const struct kernel_symbol *start,
153 const struct kernel_symbol *stop)
155 const struct kernel_symbol *ks = start;
156 for (; ks < stop; ks++)
157 if (strcmp(ks->name, name) == 0)
162 static void printk_unused_warning(const char *name)
164 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
165 "however this module is using it.\n", name);
166 printk(KERN_WARNING "This symbol will go away in the future.\n");
167 printk(KERN_WARNING "Please evalute if this is the right api to use, "
168 "and if it really is, submit a report the linux kernel "
169 "mailinglist together with submitting your code for "
173 /* Find a symbol, return value, crc and module which owns it */
174 static unsigned long __find_symbol(const char *name,
175 struct module **owner,
176 const unsigned long **crc,
180 const struct kernel_symbol *ks;
182 /* Core kernel first. */
184 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
186 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
190 ks = lookup_symbol(name, __start___ksymtab_gpl,
191 __stop___ksymtab_gpl);
193 *crc = symversion(__start___kcrctab_gpl,
194 (ks - __start___ksymtab_gpl));
198 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
199 __stop___ksymtab_gpl_future);
202 printk(KERN_WARNING "Symbol %s is being used "
203 "by a non-GPL module, which will not "
204 "be allowed in the future\n", name);
205 printk(KERN_WARNING "Please see the file "
206 "Documentation/feature-removal-schedule.txt "
207 "in the kernel source tree for more "
210 *crc = symversion(__start___kcrctab_gpl_future,
211 (ks - __start___ksymtab_gpl_future));
215 ks = lookup_symbol(name, __start___ksymtab_unused,
216 __stop___ksymtab_unused);
218 printk_unused_warning(name);
219 *crc = symversion(__start___kcrctab_unused,
220 (ks - __start___ksymtab_unused));
225 ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
226 __stop___ksymtab_unused_gpl);
228 printk_unused_warning(name);
229 *crc = symversion(__start___kcrctab_unused_gpl,
230 (ks - __start___ksymtab_unused_gpl));
234 /* Now try modules. */
235 list_for_each_entry(mod, &modules, list) {
237 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
239 *crc = symversion(mod->crcs, (ks - mod->syms));
244 ks = lookup_symbol(name, mod->gpl_syms,
245 mod->gpl_syms + mod->num_gpl_syms);
247 *crc = symversion(mod->gpl_crcs,
248 (ks - mod->gpl_syms));
252 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
254 printk_unused_warning(name);
255 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
260 ks = lookup_symbol(name, mod->unused_gpl_syms,
261 mod->unused_gpl_syms + mod->num_unused_gpl_syms);
263 printk_unused_warning(name);
264 *crc = symversion(mod->unused_gpl_crcs,
265 (ks - mod->unused_gpl_syms));
269 ks = lookup_symbol(name, mod->gpl_future_syms,
270 (mod->gpl_future_syms +
271 mod->num_gpl_future_syms));
274 printk(KERN_WARNING "Symbol %s is being used "
275 "by a non-GPL module, which will not "
276 "be allowed in the future\n", name);
277 printk(KERN_WARNING "Please see the file "
278 "Documentation/feature-removal-schedule.txt "
279 "in the kernel source tree for more "
282 *crc = symversion(mod->gpl_future_crcs,
283 (ks - mod->gpl_future_syms));
287 DEBUGP("Failed to find symbol %s\n", name);
291 /* Search for module by name: must hold module_mutex. */
292 static struct module *find_module(const char *name)
296 list_for_each_entry(mod, &modules, list) {
297 if (strcmp(mod->name, name) == 0)
304 /* Number of blocks used and allocated. */
305 static unsigned int pcpu_num_used, pcpu_num_allocated;
306 /* Size of each block. -ve means used. */
307 static int *pcpu_size;
309 static int split_block(unsigned int i, unsigned short size)
311 /* Reallocation required? */
312 if (pcpu_num_used + 1 > pcpu_num_allocated) {
315 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
320 pcpu_num_allocated *= 2;
324 /* Insert a new subblock */
325 memmove(&pcpu_size[i+1], &pcpu_size[i],
326 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
329 pcpu_size[i+1] -= size;
334 static inline unsigned int block_size(int val)
341 /* Created by linker magic */
342 extern char __per_cpu_start[], __per_cpu_end[];
344 static void *percpu_modalloc(unsigned long size, unsigned long align,
351 if (align > PAGE_SIZE) {
352 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
353 name, align, PAGE_SIZE);
357 ptr = __per_cpu_start;
358 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
359 /* Extra for alignment requirement. */
360 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
361 BUG_ON(i == 0 && extra != 0);
363 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
366 /* Transfer extra to previous block. */
367 if (pcpu_size[i-1] < 0)
368 pcpu_size[i-1] -= extra;
370 pcpu_size[i-1] += extra;
371 pcpu_size[i] -= extra;
374 /* Split block if warranted */
375 if (pcpu_size[i] - size > sizeof(unsigned long))
376 if (!split_block(i, size))
380 pcpu_size[i] = -pcpu_size[i];
384 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
389 static void percpu_modfree(void *freeme)
392 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
394 /* First entry is core kernel percpu data. */
395 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
397 pcpu_size[i] = -pcpu_size[i];
404 /* Merge with previous? */
405 if (pcpu_size[i-1] >= 0) {
406 pcpu_size[i-1] += pcpu_size[i];
408 memmove(&pcpu_size[i], &pcpu_size[i+1],
409 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
412 /* Merge with next? */
413 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
414 pcpu_size[i] += pcpu_size[i+1];
416 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
417 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
421 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
423 const char *secstrings)
425 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
428 static int percpu_modinit(void)
431 pcpu_num_allocated = 2;
432 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
434 /* Static in-kernel percpu data (used). */
435 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
437 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
438 if (pcpu_size[1] < 0) {
439 printk(KERN_ERR "No per-cpu room for modules.\n");
445 __initcall(percpu_modinit);
446 #else /* ... !CONFIG_SMP */
447 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
452 static inline void percpu_modfree(void *pcpuptr)
456 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
458 const char *secstrings)
462 static inline void percpu_modcopy(void *pcpudst, const void *src,
465 /* pcpusec should be 0, and size of that section should be 0. */
468 #endif /* CONFIG_SMP */
470 #define MODINFO_ATTR(field) \
471 static void setup_modinfo_##field(struct module *mod, const char *s) \
473 mod->field = kstrdup(s, GFP_KERNEL); \
475 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
476 struct module *mod, char *buffer) \
478 return sprintf(buffer, "%s\n", mod->field); \
480 static int modinfo_##field##_exists(struct module *mod) \
482 return mod->field != NULL; \
484 static void free_modinfo_##field(struct module *mod) \
489 static struct module_attribute modinfo_##field = { \
490 .attr = { .name = __stringify(field), .mode = 0444 }, \
491 .show = show_modinfo_##field, \
492 .setup = setup_modinfo_##field, \
493 .test = modinfo_##field##_exists, \
494 .free = free_modinfo_##field, \
497 MODINFO_ATTR(version);
498 MODINFO_ATTR(srcversion);
500 #ifdef CONFIG_MODULE_UNLOAD
501 /* Init the unload section of the module. */
502 static void module_unload_init(struct module *mod)
506 INIT_LIST_HEAD(&mod->modules_which_use_me);
507 for (i = 0; i < NR_CPUS; i++)
508 local_set(&mod->ref[i].count, 0);
509 /* Hold reference count during initialization. */
510 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
511 /* Backwards compatibility macros put refcount during init. */
512 mod->waiter = current;
515 /* modules using other modules */
518 struct list_head list;
519 struct module *module_which_uses;
522 /* Does a already use b? */
523 static int already_uses(struct module *a, struct module *b)
525 struct module_use *use;
527 list_for_each_entry(use, &b->modules_which_use_me, list) {
528 if (use->module_which_uses == a) {
529 DEBUGP("%s uses %s!\n", a->name, b->name);
533 DEBUGP("%s does not use %s!\n", a->name, b->name);
537 /* Module a uses b */
538 static int use_module(struct module *a, struct module *b)
540 struct module_use *use;
543 if (b == NULL || already_uses(a, b)) return 1;
545 if (!strong_try_module_get(b))
548 DEBUGP("Allocating new usage for %s.\n", a->name);
549 use = kmalloc(sizeof(*use), GFP_ATOMIC);
551 printk("%s: out of memory loading\n", a->name);
556 use->module_which_uses = a;
557 list_add(&use->list, &b->modules_which_use_me);
558 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
562 /* Clear the unload stuff of the module. */
563 static void module_unload_free(struct module *mod)
567 list_for_each_entry(i, &modules, list) {
568 struct module_use *use;
570 list_for_each_entry(use, &i->modules_which_use_me, list) {
571 if (use->module_which_uses == mod) {
572 DEBUGP("%s unusing %s\n", mod->name, i->name);
574 list_del(&use->list);
576 sysfs_remove_link(i->holders_dir, mod->name);
577 /* There can be at most one match. */
584 #ifdef CONFIG_MODULE_FORCE_UNLOAD
585 static inline int try_force_unload(unsigned int flags)
587 int ret = (flags & O_TRUNC);
589 add_taint(TAINT_FORCED_RMMOD);
593 static inline int try_force_unload(unsigned int flags)
597 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
606 /* Whole machine is stopped with interrupts off when this runs. */
607 static int __try_stop_module(void *_sref)
609 struct stopref *sref = _sref;
611 /* If it's not unused, quit unless we are told to block. */
612 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
613 if (!(*sref->forced = try_force_unload(sref->flags)))
617 /* Mark it as dying. */
618 sref->mod->state = MODULE_STATE_GOING;
622 static int try_stop_module(struct module *mod, int flags, int *forced)
624 struct stopref sref = { mod, flags, forced };
626 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
629 unsigned int module_refcount(struct module *mod)
631 unsigned int i, total = 0;
633 for (i = 0; i < NR_CPUS; i++)
634 total += local_read(&mod->ref[i].count);
637 EXPORT_SYMBOL(module_refcount);
639 /* This exists whether we can unload or not */
640 static void free_module(struct module *mod);
642 static void wait_for_zero_refcount(struct module *mod)
644 /* Since we might sleep for some time, drop the semaphore first */
645 mutex_unlock(&module_mutex);
647 DEBUGP("Looking at refcount...\n");
648 set_current_state(TASK_UNINTERRUPTIBLE);
649 if (module_refcount(mod) == 0)
653 current->state = TASK_RUNNING;
654 mutex_lock(&module_mutex);
658 sys_delete_module(const char __user *name_user, unsigned int flags)
661 char name[MODULE_NAME_LEN];
664 if (!capable(CAP_SYS_MODULE))
667 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
669 name[MODULE_NAME_LEN-1] = '\0';
671 if (mutex_lock_interruptible(&module_mutex) != 0)
674 mod = find_module(name);
680 if (!list_empty(&mod->modules_which_use_me)) {
681 /* Other modules depend on us: get rid of them first. */
686 /* Doing init or already dying? */
687 if (mod->state != MODULE_STATE_LIVE) {
688 /* FIXME: if (force), slam module count and wake up
690 DEBUGP("%s already dying\n", mod->name);
695 /* If it has an init func, it must have an exit func to unload */
696 if (mod->init && !mod->exit) {
697 forced = try_force_unload(flags);
699 /* This module can't be removed */
705 /* Set this up before setting mod->state */
706 mod->waiter = current;
708 /* Stop the machine so refcounts can't move and disable module. */
709 ret = try_stop_module(mod, flags, &forced);
713 /* Never wait if forced. */
714 if (!forced && module_refcount(mod) != 0)
715 wait_for_zero_refcount(mod);
717 /* Final destruction now noone is using it. */
718 if (mod->exit != NULL) {
719 mutex_unlock(&module_mutex);
721 mutex_lock(&module_mutex);
726 mutex_unlock(&module_mutex);
730 static void print_unload_info(struct seq_file *m, struct module *mod)
732 struct module_use *use;
733 int printed_something = 0;
735 seq_printf(m, " %u ", module_refcount(mod));
737 /* Always include a trailing , so userspace can differentiate
738 between this and the old multi-field proc format. */
739 list_for_each_entry(use, &mod->modules_which_use_me, list) {
740 printed_something = 1;
741 seq_printf(m, "%s,", use->module_which_uses->name);
744 if (mod->init != NULL && mod->exit == NULL) {
745 printed_something = 1;
746 seq_printf(m, "[permanent],");
749 if (!printed_something)
753 void __symbol_put(const char *symbol)
755 struct module *owner;
756 const unsigned long *crc;
759 if (!__find_symbol(symbol, &owner, &crc, 1))
764 EXPORT_SYMBOL(__symbol_put);
766 void symbol_put_addr(void *addr)
768 struct module *modaddr;
770 if (core_kernel_text((unsigned long)addr))
773 if (!(modaddr = module_text_address((unsigned long)addr)))
777 EXPORT_SYMBOL_GPL(symbol_put_addr);
779 static ssize_t show_refcnt(struct module_attribute *mattr,
780 struct module *mod, char *buffer)
782 return sprintf(buffer, "%u\n", module_refcount(mod));
785 static struct module_attribute refcnt = {
786 .attr = { .name = "refcnt", .mode = 0444 },
790 void module_put(struct module *module)
793 unsigned int cpu = get_cpu();
794 local_dec(&module->ref[cpu].count);
795 /* Maybe they're waiting for us to drop reference? */
796 if (unlikely(!module_is_live(module)))
797 wake_up_process(module->waiter);
801 EXPORT_SYMBOL(module_put);
803 #else /* !CONFIG_MODULE_UNLOAD */
804 static void print_unload_info(struct seq_file *m, struct module *mod)
806 /* We don't know the usage count, or what modules are using. */
807 seq_printf(m, " - -");
810 static inline void module_unload_free(struct module *mod)
814 static inline int use_module(struct module *a, struct module *b)
816 return strong_try_module_get(b);
819 static inline void module_unload_init(struct module *mod)
822 #endif /* CONFIG_MODULE_UNLOAD */
824 static ssize_t show_initstate(struct module_attribute *mattr,
825 struct module *mod, char *buffer)
827 const char *state = "unknown";
829 switch (mod->state) {
830 case MODULE_STATE_LIVE:
833 case MODULE_STATE_COMING:
836 case MODULE_STATE_GOING:
840 return sprintf(buffer, "%s\n", state);
843 static struct module_attribute initstate = {
844 .attr = { .name = "initstate", .mode = 0444 },
845 .show = show_initstate,
848 static struct module_attribute *modinfo_attrs[] = {
852 #ifdef CONFIG_MODULE_UNLOAD
858 static const char vermagic[] = VERMAGIC_STRING;
860 #ifdef CONFIG_MODVERSIONS
861 static int check_version(Elf_Shdr *sechdrs,
862 unsigned int versindex,
865 const unsigned long *crc)
867 unsigned int i, num_versions;
868 struct modversion_info *versions;
870 /* Exporting module didn't supply crcs? OK, we're already tainted. */
874 versions = (void *) sechdrs[versindex].sh_addr;
875 num_versions = sechdrs[versindex].sh_size
876 / sizeof(struct modversion_info);
878 for (i = 0; i < num_versions; i++) {
879 if (strcmp(versions[i].name, symname) != 0)
882 if (versions[i].crc == *crc)
884 printk("%s: disagrees about version of symbol %s\n",
886 DEBUGP("Found checksum %lX vs module %lX\n",
887 *crc, versions[i].crc);
890 /* Not in module's version table. OK, but that taints the kernel. */
891 if (!(tainted & TAINT_FORCED_MODULE))
892 printk("%s: no version for \"%s\" found: kernel tainted.\n",
894 add_taint_module(mod, TAINT_FORCED_MODULE);
898 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
899 unsigned int versindex,
902 const unsigned long *crc;
903 struct module *owner;
905 if (!__find_symbol("struct_module", &owner, &crc, 1))
907 return check_version(sechdrs, versindex, "struct_module", mod,
911 /* First part is kernel version, which we ignore. */
912 static inline int same_magic(const char *amagic, const char *bmagic)
914 amagic += strcspn(amagic, " ");
915 bmagic += strcspn(bmagic, " ");
916 return strcmp(amagic, bmagic) == 0;
919 static inline int check_version(Elf_Shdr *sechdrs,
920 unsigned int versindex,
923 const unsigned long *crc)
928 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
929 unsigned int versindex,
935 static inline int same_magic(const char *amagic, const char *bmagic)
937 return strcmp(amagic, bmagic) == 0;
939 #endif /* CONFIG_MODVERSIONS */
941 /* Resolve a symbol for this module. I.e. if we find one, record usage.
942 Must be holding module_mutex. */
943 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
944 unsigned int versindex,
948 struct module *owner;
950 const unsigned long *crc;
952 ret = __find_symbol(name, &owner, &crc,
953 !(mod->taints & TAINT_PROPRIETARY_MODULE));
955 /* use_module can fail due to OOM, or module unloading */
956 if (!check_version(sechdrs, versindex, name, mod, crc) ||
957 !use_module(mod, owner))
965 * /sys/module/foo/sections stuff
966 * J. Corbet <corbet@lwn.net>
968 #ifdef CONFIG_KALLSYMS
969 static ssize_t module_sect_show(struct module_attribute *mattr,
970 struct module *mod, char *buf)
972 struct module_sect_attr *sattr =
973 container_of(mattr, struct module_sect_attr, mattr);
974 return sprintf(buf, "0x%lx\n", sattr->address);
977 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
981 for (section = 0; section < sect_attrs->nsections; section++)
982 kfree(sect_attrs->attrs[section].name);
986 static void add_sect_attrs(struct module *mod, unsigned int nsect,
987 char *secstrings, Elf_Shdr *sechdrs)
989 unsigned int nloaded = 0, i, size[2];
990 struct module_sect_attrs *sect_attrs;
991 struct module_sect_attr *sattr;
992 struct attribute **gattr;
994 /* Count loaded sections and allocate structures */
995 for (i = 0; i < nsect; i++)
996 if (sechdrs[i].sh_flags & SHF_ALLOC)
998 size[0] = ALIGN(sizeof(*sect_attrs)
999 + nloaded * sizeof(sect_attrs->attrs[0]),
1000 sizeof(sect_attrs->grp.attrs[0]));
1001 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1002 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1003 if (sect_attrs == NULL)
1006 /* Setup section attributes. */
1007 sect_attrs->grp.name = "sections";
1008 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1010 sect_attrs->nsections = 0;
1011 sattr = §_attrs->attrs[0];
1012 gattr = §_attrs->grp.attrs[0];
1013 for (i = 0; i < nsect; i++) {
1014 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1016 sattr->address = sechdrs[i].sh_addr;
1017 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1019 if (sattr->name == NULL)
1021 sect_attrs->nsections++;
1022 sattr->mattr.show = module_sect_show;
1023 sattr->mattr.store = NULL;
1024 sattr->mattr.attr.name = sattr->name;
1025 sattr->mattr.attr.mode = S_IRUGO;
1026 *(gattr++) = &(sattr++)->mattr.attr;
1030 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1033 mod->sect_attrs = sect_attrs;
1036 free_sect_attrs(sect_attrs);
1039 static void remove_sect_attrs(struct module *mod)
1041 if (mod->sect_attrs) {
1042 sysfs_remove_group(&mod->mkobj.kobj,
1043 &mod->sect_attrs->grp);
1044 /* We are positive that no one is using any sect attrs
1045 * at this point. Deallocate immediately. */
1046 free_sect_attrs(mod->sect_attrs);
1047 mod->sect_attrs = NULL;
1052 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1055 struct module_notes_attrs {
1056 struct kobject *dir;
1058 struct bin_attribute attrs[0];
1061 static ssize_t module_notes_read(struct kobject *kobj,
1062 struct bin_attribute *bin_attr,
1063 char *buf, loff_t pos, size_t count)
1066 * The caller checked the pos and count against our size.
1068 memcpy(buf, bin_attr->private + pos, count);
1072 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1075 if (notes_attrs->dir) {
1077 sysfs_remove_bin_file(notes_attrs->dir,
1078 ¬es_attrs->attrs[i]);
1079 kobject_del(notes_attrs->dir);
1084 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1085 char *secstrings, Elf_Shdr *sechdrs)
1087 unsigned int notes, loaded, i;
1088 struct module_notes_attrs *notes_attrs;
1089 struct bin_attribute *nattr;
1091 /* Count notes sections and allocate structures. */
1093 for (i = 0; i < nsect; i++)
1094 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1095 (sechdrs[i].sh_type == SHT_NOTE))
1101 notes_attrs = kzalloc(sizeof(*notes_attrs)
1102 + notes * sizeof(notes_attrs->attrs[0]),
1104 if (notes_attrs == NULL)
1107 notes_attrs->notes = notes;
1108 nattr = ¬es_attrs->attrs[0];
1109 for (loaded = i = 0; i < nsect; ++i) {
1110 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1112 if (sechdrs[i].sh_type == SHT_NOTE) {
1113 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1114 nattr->attr.mode = S_IRUGO;
1115 nattr->size = sechdrs[i].sh_size;
1116 nattr->private = (void *) sechdrs[i].sh_addr;
1117 nattr->read = module_notes_read;
1123 notes_attrs->dir = kobject_add_dir(&mod->mkobj.kobj, "notes");
1124 if (!notes_attrs->dir)
1127 for (i = 0; i < notes; ++i)
1128 if (sysfs_create_bin_file(notes_attrs->dir,
1129 ¬es_attrs->attrs[i]))
1132 mod->notes_attrs = notes_attrs;
1136 free_notes_attrs(notes_attrs, i);
1139 static void remove_notes_attrs(struct module *mod)
1141 if (mod->notes_attrs)
1142 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1147 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1148 char *sectstrings, Elf_Shdr *sechdrs)
1152 static inline void remove_sect_attrs(struct module *mod)
1156 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1157 char *sectstrings, Elf_Shdr *sechdrs)
1161 static inline void remove_notes_attrs(struct module *mod)
1164 #endif /* CONFIG_KALLSYMS */
1167 int module_add_modinfo_attrs(struct module *mod)
1169 struct module_attribute *attr;
1170 struct module_attribute *temp_attr;
1174 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1175 (ARRAY_SIZE(modinfo_attrs) + 1)),
1177 if (!mod->modinfo_attrs)
1180 temp_attr = mod->modinfo_attrs;
1181 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1183 (attr->test && attr->test(mod))) {
1184 memcpy(temp_attr, attr, sizeof(*temp_attr));
1185 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1192 void module_remove_modinfo_attrs(struct module *mod)
1194 struct module_attribute *attr;
1197 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1198 /* pick a field to test for end of list */
1199 if (!attr->attr.name)
1201 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1205 kfree(mod->modinfo_attrs);
1210 int mod_sysfs_init(struct module *mod)
1214 if (!module_sysfs_initialized) {
1215 printk(KERN_ERR "%s: module sysfs not initialized\n",
1220 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1221 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1224 kobj_set_kset_s(&mod->mkobj, module_subsys);
1225 mod->mkobj.mod = mod;
1227 kobject_init(&mod->mkobj.kobj);
1233 int mod_sysfs_setup(struct module *mod,
1234 struct kernel_param *kparam,
1235 unsigned int num_params)
1239 /* delay uevent until full sysfs population */
1240 err = kobject_add(&mod->mkobj.kobj);
1244 mod->holders_dir = kobject_add_dir(&mod->mkobj.kobj, "holders");
1245 if (!mod->holders_dir) {
1250 err = module_param_sysfs_setup(mod, kparam, num_params);
1252 goto out_unreg_holders;
1254 err = module_add_modinfo_attrs(mod);
1256 goto out_unreg_param;
1258 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1262 module_param_sysfs_remove(mod);
1264 kobject_unregister(mod->holders_dir);
1266 kobject_del(&mod->mkobj.kobj);
1267 kobject_put(&mod->mkobj.kobj);
1273 static void mod_kobject_remove(struct module *mod)
1275 module_remove_modinfo_attrs(mod);
1276 module_param_sysfs_remove(mod);
1277 kobject_unregister(mod->mkobj.drivers_dir);
1278 kobject_unregister(mod->holders_dir);
1279 kobject_unregister(&mod->mkobj.kobj);
1283 * unlink the module with the whole machine is stopped with interrupts off
1284 * - this defends against kallsyms not taking locks
1286 static int __unlink_module(void *_mod)
1288 struct module *mod = _mod;
1289 list_del(&mod->list);
1293 /* Free a module, remove from lists, etc (must hold module_mutex). */
1294 static void free_module(struct module *mod)
1296 /* Delete from various lists */
1297 stop_machine_run(__unlink_module, mod, NR_CPUS);
1298 remove_notes_attrs(mod);
1299 remove_sect_attrs(mod);
1300 mod_kobject_remove(mod);
1302 unwind_remove_table(mod->unwind_info, 0);
1304 /* Arch-specific cleanup. */
1305 module_arch_cleanup(mod);
1307 /* Module unload stuff */
1308 module_unload_free(mod);
1310 /* This may be NULL, but that's OK */
1311 module_free(mod, mod->module_init);
1314 percpu_modfree(mod->percpu);
1316 /* Free lock-classes: */
1317 lockdep_free_key_range(mod->module_core, mod->core_size);
1319 /* Finally, free the core (containing the module structure) */
1320 module_free(mod, mod->module_core);
1323 void *__symbol_get(const char *symbol)
1325 struct module *owner;
1326 unsigned long value;
1327 const unsigned long *crc;
1330 value = __find_symbol(symbol, &owner, &crc, 1);
1331 if (value && !strong_try_module_get(owner))
1335 return (void *)value;
1337 EXPORT_SYMBOL_GPL(__symbol_get);
1340 * Ensure that an exported symbol [global namespace] does not already exist
1341 * in the kernel or in some other module's exported symbol table.
1343 static int verify_export_symbols(struct module *mod)
1345 const char *name = NULL;
1346 unsigned long i, ret = 0;
1347 struct module *owner;
1348 const unsigned long *crc;
1350 for (i = 0; i < mod->num_syms; i++)
1351 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
1352 name = mod->syms[i].name;
1357 for (i = 0; i < mod->num_gpl_syms; i++)
1358 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
1359 name = mod->gpl_syms[i].name;
1366 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1367 mod->name, name, module_name(owner));
1372 /* Change all symbols so that sh_value encodes the pointer directly. */
1373 static int simplify_symbols(Elf_Shdr *sechdrs,
1374 unsigned int symindex,
1376 unsigned int versindex,
1377 unsigned int pcpuindex,
1380 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1381 unsigned long secbase;
1382 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1385 for (i = 1; i < n; i++) {
1386 switch (sym[i].st_shndx) {
1388 /* We compiled with -fno-common. These are not
1389 supposed to happen. */
1390 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1391 printk("%s: please compile with -fno-common\n",
1397 /* Don't need to do anything */
1398 DEBUGP("Absolute symbol: 0x%08lx\n",
1399 (long)sym[i].st_value);
1404 = resolve_symbol(sechdrs, versindex,
1405 strtab + sym[i].st_name, mod);
1407 /* Ok if resolved. */
1408 if (sym[i].st_value != 0)
1411 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1414 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1415 mod->name, strtab + sym[i].st_name);
1420 /* Divert to percpu allocation if a percpu var. */
1421 if (sym[i].st_shndx == pcpuindex)
1422 secbase = (unsigned long)mod->percpu;
1424 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1425 sym[i].st_value += secbase;
1433 /* Update size with this section: return offset. */
1434 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1438 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1439 *size = ret + sechdr->sh_size;
1443 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1444 might -- code, read-only data, read-write data, small data. Tally
1445 sizes, and place the offsets into sh_entsize fields: high bit means it
1447 static void layout_sections(struct module *mod,
1448 const Elf_Ehdr *hdr,
1450 const char *secstrings)
1452 static unsigned long const masks[][2] = {
1453 /* NOTE: all executable code must be the first section
1454 * in this array; otherwise modify the text_size
1455 * finder in the two loops below */
1456 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1457 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1458 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1459 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1463 for (i = 0; i < hdr->e_shnum; i++)
1464 sechdrs[i].sh_entsize = ~0UL;
1466 DEBUGP("Core section allocation order:\n");
1467 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1468 for (i = 0; i < hdr->e_shnum; ++i) {
1469 Elf_Shdr *s = &sechdrs[i];
1471 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1472 || (s->sh_flags & masks[m][1])
1473 || s->sh_entsize != ~0UL
1474 || strncmp(secstrings + s->sh_name,
1477 s->sh_entsize = get_offset(&mod->core_size, s);
1478 DEBUGP("\t%s\n", secstrings + s->sh_name);
1481 mod->core_text_size = mod->core_size;
1484 DEBUGP("Init section allocation order:\n");
1485 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1486 for (i = 0; i < hdr->e_shnum; ++i) {
1487 Elf_Shdr *s = &sechdrs[i];
1489 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1490 || (s->sh_flags & masks[m][1])
1491 || s->sh_entsize != ~0UL
1492 || strncmp(secstrings + s->sh_name,
1495 s->sh_entsize = (get_offset(&mod->init_size, s)
1496 | INIT_OFFSET_MASK);
1497 DEBUGP("\t%s\n", secstrings + s->sh_name);
1500 mod->init_text_size = mod->init_size;
1504 static void set_license(struct module *mod, const char *license)
1507 license = "unspecified";
1509 if (!license_is_gpl_compatible(license)) {
1510 if (!(tainted & TAINT_PROPRIETARY_MODULE))
1511 printk(KERN_WARNING "%s: module license '%s' taints "
1512 "kernel.\n", mod->name, license);
1513 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1517 /* Parse tag=value strings from .modinfo section */
1518 static char *next_string(char *string, unsigned long *secsize)
1520 /* Skip non-zero chars */
1523 if ((*secsize)-- <= 1)
1527 /* Skip any zero padding. */
1528 while (!string[0]) {
1530 if ((*secsize)-- <= 1)
1536 static char *get_modinfo(Elf_Shdr *sechdrs,
1541 unsigned int taglen = strlen(tag);
1542 unsigned long size = sechdrs[info].sh_size;
1544 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1545 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1546 return p + taglen + 1;
1551 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1552 unsigned int infoindex)
1554 struct module_attribute *attr;
1557 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1560 get_modinfo(sechdrs,
1566 #ifdef CONFIG_KALLSYMS
1567 static int is_exported(const char *name, const struct module *mod)
1569 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1572 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1579 static char elf_type(const Elf_Sym *sym,
1581 const char *secstrings,
1584 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1585 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1590 if (sym->st_shndx == SHN_UNDEF)
1592 if (sym->st_shndx == SHN_ABS)
1594 if (sym->st_shndx >= SHN_LORESERVE)
1596 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1598 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1599 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1600 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1602 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1607 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1608 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1613 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1614 ".debug", strlen(".debug")) == 0)
1619 static void add_kallsyms(struct module *mod,
1621 unsigned int symindex,
1622 unsigned int strindex,
1623 const char *secstrings)
1627 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1628 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1629 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1631 /* Set types up while we still have access to sections. */
1632 for (i = 0; i < mod->num_symtab; i++)
1633 mod->symtab[i].st_info
1634 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1637 static inline void add_kallsyms(struct module *mod,
1639 unsigned int symindex,
1640 unsigned int strindex,
1641 const char *secstrings)
1644 #endif /* CONFIG_KALLSYMS */
1646 /* Allocate and load the module: note that size of section 0 is always
1647 zero, and we rely on this for optional sections. */
1648 static struct module *load_module(void __user *umod,
1650 const char __user *uargs)
1654 char *secstrings, *args, *modmagic, *strtab = NULL;
1656 unsigned int symindex = 0;
1657 unsigned int strindex = 0;
1658 unsigned int setupindex;
1659 unsigned int exindex;
1660 unsigned int exportindex;
1661 unsigned int modindex;
1662 unsigned int obsparmindex;
1663 unsigned int infoindex;
1664 unsigned int gplindex;
1665 unsigned int crcindex;
1666 unsigned int gplcrcindex;
1667 unsigned int versindex;
1668 unsigned int pcpuindex;
1669 unsigned int gplfutureindex;
1670 unsigned int gplfuturecrcindex;
1671 unsigned int unwindex = 0;
1672 unsigned int unusedindex;
1673 unsigned int unusedcrcindex;
1674 unsigned int unusedgplindex;
1675 unsigned int unusedgplcrcindex;
1678 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1679 struct exception_table_entry *extable;
1680 mm_segment_t old_fs;
1682 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1684 if (len < sizeof(*hdr))
1685 return ERR_PTR(-ENOEXEC);
1687 /* Suck in entire file: we'll want most of it. */
1688 /* vmalloc barfs on "unusual" numbers. Check here */
1689 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1690 return ERR_PTR(-ENOMEM);
1691 if (copy_from_user(hdr, umod, len) != 0) {
1696 /* Sanity checks against insmoding binaries or wrong arch,
1697 weird elf version */
1698 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1699 || hdr->e_type != ET_REL
1700 || !elf_check_arch(hdr)
1701 || hdr->e_shentsize != sizeof(*sechdrs)) {
1706 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1709 /* Convenience variables */
1710 sechdrs = (void *)hdr + hdr->e_shoff;
1711 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1712 sechdrs[0].sh_addr = 0;
1714 for (i = 1; i < hdr->e_shnum; i++) {
1715 if (sechdrs[i].sh_type != SHT_NOBITS
1716 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1719 /* Mark all sections sh_addr with their address in the
1721 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1723 /* Internal symbols and strings. */
1724 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1726 strindex = sechdrs[i].sh_link;
1727 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1729 #ifndef CONFIG_MODULE_UNLOAD
1730 /* Don't load .exit sections */
1731 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1732 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1736 modindex = find_sec(hdr, sechdrs, secstrings,
1737 ".gnu.linkonce.this_module");
1739 printk(KERN_WARNING "No module found in object\n");
1743 mod = (void *)sechdrs[modindex].sh_addr;
1745 if (symindex == 0) {
1746 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1752 /* Optional sections */
1753 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1754 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1755 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
1756 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1757 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
1758 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1759 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1760 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
1761 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1762 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
1763 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1764 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1765 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1766 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1767 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1768 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1769 #ifdef ARCH_UNWIND_SECTION_NAME
1770 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1773 /* Don't keep modinfo section */
1774 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1775 #ifdef CONFIG_KALLSYMS
1776 /* Keep symbol and string tables for decoding later. */
1777 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1778 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1781 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
1783 /* Check module struct version now, before we try to use module. */
1784 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1789 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1790 /* This is allowed: modprobe --force will invalidate it. */
1792 add_taint_module(mod, TAINT_FORCED_MODULE);
1793 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1795 } else if (!same_magic(modmagic, vermagic)) {
1796 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1797 mod->name, modmagic, vermagic);
1802 /* Now copy in args */
1803 args = strndup_user(uargs, ~0UL >> 1);
1805 err = PTR_ERR(args);
1809 if (find_module(mod->name)) {
1814 mod->state = MODULE_STATE_COMING;
1816 /* Allow arches to frob section contents and sizes. */
1817 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1822 /* We have a special allocation for this section. */
1823 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1824 sechdrs[pcpuindex].sh_addralign,
1830 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1831 mod->percpu = percpu;
1834 /* Determine total sizes, and put offsets in sh_entsize. For now
1835 this is done generically; there doesn't appear to be any
1836 special cases for the architectures. */
1837 layout_sections(mod, hdr, sechdrs, secstrings);
1839 /* Do the allocs. */
1840 ptr = module_alloc(mod->core_size);
1845 memset(ptr, 0, mod->core_size);
1846 mod->module_core = ptr;
1848 ptr = module_alloc(mod->init_size);
1849 if (!ptr && mod->init_size) {
1853 memset(ptr, 0, mod->init_size);
1854 mod->module_init = ptr;
1856 /* Transfer each section which specifies SHF_ALLOC */
1857 DEBUGP("final section addresses:\n");
1858 for (i = 0; i < hdr->e_shnum; i++) {
1861 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1864 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1865 dest = mod->module_init
1866 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1868 dest = mod->module_core + sechdrs[i].sh_entsize;
1870 if (sechdrs[i].sh_type != SHT_NOBITS)
1871 memcpy(dest, (void *)sechdrs[i].sh_addr,
1872 sechdrs[i].sh_size);
1873 /* Update sh_addr to point to copy in image. */
1874 sechdrs[i].sh_addr = (unsigned long)dest;
1875 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1877 /* Module has been moved. */
1878 mod = (void *)sechdrs[modindex].sh_addr;
1880 /* Now we've moved module, initialize linked lists, etc. */
1881 module_unload_init(mod);
1883 /* Initialize kobject, so we can reference it. */
1884 err = mod_sysfs_init(mod);
1888 /* Set up license info based on the info section */
1889 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1891 if (strcmp(mod->name, "ndiswrapper") == 0)
1892 add_taint(TAINT_PROPRIETARY_MODULE);
1893 if (strcmp(mod->name, "driverloader") == 0)
1894 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1896 /* Set up MODINFO_ATTR fields */
1897 setup_modinfo(mod, sechdrs, infoindex);
1899 /* Fix up syms, so that st_value is a pointer to location. */
1900 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1905 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1906 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1907 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1909 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1910 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1911 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1913 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1914 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
1915 sizeof(*mod->gpl_future_syms);
1916 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
1917 sizeof(*mod->unused_syms);
1918 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
1919 sizeof(*mod->unused_gpl_syms);
1920 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
1921 if (gplfuturecrcindex)
1922 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
1924 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
1926 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
1927 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
1928 if (unusedgplcrcindex)
1929 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
1931 #ifdef CONFIG_MODVERSIONS
1932 if ((mod->num_syms && !crcindex) ||
1933 (mod->num_gpl_syms && !gplcrcindex) ||
1934 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
1935 (mod->num_unused_syms && !unusedcrcindex) ||
1936 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
1937 printk(KERN_WARNING "%s: No versions for exported symbols."
1938 " Tainting kernel.\n", mod->name);
1939 add_taint_module(mod, TAINT_FORCED_MODULE);
1943 /* Now do relocations. */
1944 for (i = 1; i < hdr->e_shnum; i++) {
1945 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1946 unsigned int info = sechdrs[i].sh_info;
1948 /* Not a valid relocation section? */
1949 if (info >= hdr->e_shnum)
1952 /* Don't bother with non-allocated sections */
1953 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1956 if (sechdrs[i].sh_type == SHT_REL)
1957 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1958 else if (sechdrs[i].sh_type == SHT_RELA)
1959 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1965 /* Find duplicate symbols */
1966 err = verify_export_symbols(mod);
1971 /* Set up and sort exception table */
1972 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1973 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1974 sort_extable(extable, extable + mod->num_exentries);
1976 /* Finally, copy percpu area over. */
1977 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1978 sechdrs[pcpuindex].sh_size);
1980 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1982 err = module_finalize(hdr, sechdrs, mod);
1986 /* flush the icache in correct context */
1991 * Flush the instruction cache, since we've played with text.
1992 * Do it before processing of module parameters, so the module
1993 * can provide parameter accessor functions of its own.
1995 if (mod->module_init)
1996 flush_icache_range((unsigned long)mod->module_init,
1997 (unsigned long)mod->module_init
1999 flush_icache_range((unsigned long)mod->module_core,
2000 (unsigned long)mod->module_core + mod->core_size);
2006 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2009 /* Size of section 0 is 0, so this works well if no params */
2010 err = parse_args(mod->name, mod->args,
2011 (struct kernel_param *)
2012 sechdrs[setupindex].sh_addr,
2013 sechdrs[setupindex].sh_size
2014 / sizeof(struct kernel_param),
2019 err = mod_sysfs_setup(mod,
2020 (struct kernel_param *)
2021 sechdrs[setupindex].sh_addr,
2022 sechdrs[setupindex].sh_size
2023 / sizeof(struct kernel_param));
2026 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2027 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2029 /* Size of section 0 is 0, so this works well if no unwind info. */
2030 mod->unwind_info = unwind_add_table(mod,
2031 (void *)sechdrs[unwindex].sh_addr,
2032 sechdrs[unwindex].sh_size);
2034 /* Get rid of temporary copy */
2041 module_arch_cleanup(mod);
2043 module_unload_free(mod);
2044 module_free(mod, mod->module_init);
2046 module_free(mod, mod->module_core);
2049 percpu_modfree(percpu);
2054 return ERR_PTR(err);
2057 printk(KERN_ERR "Module len %lu truncated\n", len);
2063 * link the module with the whole machine is stopped with interrupts off
2064 * - this defends against kallsyms not taking locks
2066 static int __link_module(void *_mod)
2068 struct module *mod = _mod;
2069 list_add(&mod->list, &modules);
2073 /* This is where the real work happens */
2075 sys_init_module(void __user *umod,
2077 const char __user *uargs)
2082 /* Must have permission */
2083 if (!capable(CAP_SYS_MODULE))
2086 /* Only one module load at a time, please */
2087 if (mutex_lock_interruptible(&module_mutex) != 0)
2090 /* Do all the hard work */
2091 mod = load_module(umod, len, uargs);
2093 mutex_unlock(&module_mutex);
2094 return PTR_ERR(mod);
2097 /* Now sew it into the lists. They won't access us, since
2098 strong_try_module_get() will fail. */
2099 stop_machine_run(__link_module, mod, NR_CPUS);
2101 /* Drop lock so they can recurse */
2102 mutex_unlock(&module_mutex);
2104 blocking_notifier_call_chain(&module_notify_list,
2105 MODULE_STATE_COMING, mod);
2107 /* Start the module */
2108 if (mod->init != NULL)
2111 /* Init routine failed: abort. Try to protect us from
2112 buggy refcounters. */
2113 mod->state = MODULE_STATE_GOING;
2114 synchronize_sched();
2116 mutex_lock(&module_mutex);
2118 mutex_unlock(&module_mutex);
2122 /* Now it's a first class citizen! */
2123 mutex_lock(&module_mutex);
2124 mod->state = MODULE_STATE_LIVE;
2125 /* Drop initial reference. */
2127 unwind_remove_table(mod->unwind_info, 1);
2128 module_free(mod, mod->module_init);
2129 mod->module_init = NULL;
2131 mod->init_text_size = 0;
2132 mutex_unlock(&module_mutex);
2137 static inline int within(unsigned long addr, void *start, unsigned long size)
2139 return ((void *)addr >= start && (void *)addr < start + size);
2142 #ifdef CONFIG_KALLSYMS
2144 * This ignores the intensely annoying "mapping symbols" found
2145 * in ARM ELF files: $a, $t and $d.
2147 static inline int is_arm_mapping_symbol(const char *str)
2149 return str[0] == '$' && strchr("atd", str[1])
2150 && (str[2] == '\0' || str[2] == '.');
2153 static const char *get_ksymbol(struct module *mod,
2155 unsigned long *size,
2156 unsigned long *offset)
2158 unsigned int i, best = 0;
2159 unsigned long nextval;
2161 /* At worse, next value is at end of module */
2162 if (within(addr, mod->module_init, mod->init_size))
2163 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2165 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2167 /* Scan for closest preceeding symbol, and next symbol. (ELF
2168 starts real symbols at 1). */
2169 for (i = 1; i < mod->num_symtab; i++) {
2170 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2173 /* We ignore unnamed symbols: they're uninformative
2174 * and inserted at a whim. */
2175 if (mod->symtab[i].st_value <= addr
2176 && mod->symtab[i].st_value > mod->symtab[best].st_value
2177 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2178 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2180 if (mod->symtab[i].st_value > addr
2181 && mod->symtab[i].st_value < nextval
2182 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2183 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2184 nextval = mod->symtab[i].st_value;
2191 *size = nextval - mod->symtab[best].st_value;
2193 *offset = addr - mod->symtab[best].st_value;
2194 return mod->strtab + mod->symtab[best].st_name;
2197 /* For kallsyms to ask for address resolution. NULL means not found.
2198 We don't lock, as this is used for oops resolution and races are a
2200 const char *module_address_lookup(unsigned long addr,
2201 unsigned long *size,
2202 unsigned long *offset,
2207 list_for_each_entry(mod, &modules, list) {
2208 if (within(addr, mod->module_init, mod->init_size)
2209 || within(addr, mod->module_core, mod->core_size)) {
2211 *modname = mod->name;
2212 return get_ksymbol(mod, addr, size, offset);
2218 int lookup_module_symbol_name(unsigned long addr, char *symname)
2222 mutex_lock(&module_mutex);
2223 list_for_each_entry(mod, &modules, list) {
2224 if (within(addr, mod->module_init, mod->init_size) ||
2225 within(addr, mod->module_core, mod->core_size)) {
2228 sym = get_ksymbol(mod, addr, NULL, NULL);
2231 strlcpy(symname, sym, KSYM_NAME_LEN);
2232 mutex_unlock(&module_mutex);
2237 mutex_unlock(&module_mutex);
2241 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2242 unsigned long *offset, char *modname, char *name)
2246 mutex_lock(&module_mutex);
2247 list_for_each_entry(mod, &modules, list) {
2248 if (within(addr, mod->module_init, mod->init_size) ||
2249 within(addr, mod->module_core, mod->core_size)) {
2252 sym = get_ksymbol(mod, addr, size, offset);
2256 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2258 strlcpy(name, sym, KSYM_NAME_LEN);
2259 mutex_unlock(&module_mutex);
2264 mutex_unlock(&module_mutex);
2268 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2269 char *name, char *module_name, int *exported)
2273 mutex_lock(&module_mutex);
2274 list_for_each_entry(mod, &modules, list) {
2275 if (symnum < mod->num_symtab) {
2276 *value = mod->symtab[symnum].st_value;
2277 *type = mod->symtab[symnum].st_info;
2278 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2280 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2281 *exported = is_exported(name, mod);
2282 mutex_unlock(&module_mutex);
2285 symnum -= mod->num_symtab;
2287 mutex_unlock(&module_mutex);
2291 static unsigned long mod_find_symname(struct module *mod, const char *name)
2295 for (i = 0; i < mod->num_symtab; i++)
2296 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2297 mod->symtab[i].st_info != 'U')
2298 return mod->symtab[i].st_value;
2302 /* Look for this name: can be of form module:name. */
2303 unsigned long module_kallsyms_lookup_name(const char *name)
2307 unsigned long ret = 0;
2309 /* Don't lock: we're in enough trouble already. */
2310 if ((colon = strchr(name, ':')) != NULL) {
2312 if ((mod = find_module(name)) != NULL)
2313 ret = mod_find_symname(mod, colon+1);
2316 list_for_each_entry(mod, &modules, list)
2317 if ((ret = mod_find_symname(mod, name)) != 0)
2322 #endif /* CONFIG_KALLSYMS */
2324 /* Called by the /proc file system to return a list of modules. */
2325 static void *m_start(struct seq_file *m, loff_t *pos)
2327 mutex_lock(&module_mutex);
2328 return seq_list_start(&modules, *pos);
2331 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2333 return seq_list_next(p, &modules, pos);
2336 static void m_stop(struct seq_file *m, void *p)
2338 mutex_unlock(&module_mutex);
2341 static char *taint_flags(unsigned int taints, char *buf)
2347 if (taints & TAINT_PROPRIETARY_MODULE)
2349 if (taints & TAINT_FORCED_MODULE)
2352 * TAINT_FORCED_RMMOD: could be added.
2353 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2363 static int m_show(struct seq_file *m, void *p)
2365 struct module *mod = list_entry(p, struct module, list);
2368 seq_printf(m, "%s %lu",
2369 mod->name, mod->init_size + mod->core_size);
2370 print_unload_info(m, mod);
2372 /* Informative for users. */
2373 seq_printf(m, " %s",
2374 mod->state == MODULE_STATE_GOING ? "Unloading":
2375 mod->state == MODULE_STATE_COMING ? "Loading":
2377 /* Used by oprofile and other similar tools. */
2378 seq_printf(m, " 0x%p", mod->module_core);
2382 seq_printf(m, " %s", taint_flags(mod->taints, buf));
2384 seq_printf(m, "\n");
2388 /* Format: modulename size refcount deps address
2390 Where refcount is a number or -, and deps is a comma-separated list
2393 const struct seq_operations modules_op = {
2400 /* Given an address, look for it in the module exception tables. */
2401 const struct exception_table_entry *search_module_extables(unsigned long addr)
2403 const struct exception_table_entry *e = NULL;
2407 list_for_each_entry(mod, &modules, list) {
2408 if (mod->num_exentries == 0)
2411 e = search_extable(mod->extable,
2412 mod->extable + mod->num_exentries - 1,
2419 /* Now, if we found one, we are running inside it now, hence
2420 we cannot unload the module, hence no refcnt needed. */
2425 * Is this a valid module address?
2427 int is_module_address(unsigned long addr)
2433 list_for_each_entry(mod, &modules, list) {
2434 if (within(addr, mod->module_core, mod->core_size)) {
2446 /* Is this a valid kernel address? */
2447 struct module *__module_text_address(unsigned long addr)
2451 list_for_each_entry(mod, &modules, list)
2452 if (within(addr, mod->module_init, mod->init_text_size)
2453 || within(addr, mod->module_core, mod->core_text_size))
2458 struct module *module_text_address(unsigned long addr)
2463 mod = __module_text_address(addr);
2469 /* Don't grab lock, we're oopsing. */
2470 void print_modules(void)
2475 printk("Modules linked in:");
2476 list_for_each_entry(mod, &modules, list)
2477 printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
2482 static char *make_driver_name(struct device_driver *drv)
2486 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
2491 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
2495 static void module_create_drivers_dir(struct module_kobject *mk)
2497 if (!mk || mk->drivers_dir)
2500 mk->drivers_dir = kobject_add_dir(&mk->kobj, "drivers");
2503 void module_add_driver(struct module *mod, struct device_driver *drv)
2507 struct module_kobject *mk = NULL;
2514 else if (drv->mod_name) {
2515 struct kobject *mkobj;
2517 /* Lookup built-in module entry in /sys/modules */
2518 mkobj = kset_find_obj(&module_subsys, drv->mod_name);
2520 mk = container_of(mkobj, struct module_kobject, kobj);
2521 /* remember our module structure */
2523 /* kset_find_obj took a reference */
2531 /* Don't check return codes; these calls are idempotent */
2532 no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
2533 driver_name = make_driver_name(drv);
2535 module_create_drivers_dir(mk);
2536 no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
2541 EXPORT_SYMBOL(module_add_driver);
2543 void module_remove_driver(struct device_driver *drv)
2545 struct module_kobject *mk = NULL;
2551 sysfs_remove_link(&drv->kobj, "module");
2554 mk = &drv->owner->mkobj;
2555 else if (drv->mkobj)
2557 if (mk && mk->drivers_dir) {
2558 driver_name = make_driver_name(drv);
2560 sysfs_remove_link(mk->drivers_dir, driver_name);
2565 EXPORT_SYMBOL(module_remove_driver);
2568 #ifdef CONFIG_MODVERSIONS
2569 /* Generate the signature for struct module here, too, for modversions. */
2570 void struct_module(struct module *mod) { return; }
2571 EXPORT_SYMBOL(struct_module);