1 /* Rewritten by Rusty Russell, on the backs of many others...
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/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/elf.h>
26 #include <linux/seq_file.h>
27 #include <linux/syscalls.h>
28 #include <linux/fcntl.h>
29 #include <linux/rcupdate.h>
30 #include <linux/cpu.h>
31 #include <linux/moduleparam.h>
32 #include <linux/errno.h>
33 #include <linux/err.h>
34 #include <linux/vermagic.h>
35 #include <linux/notifier.h>
36 #include <linux/stop_machine.h>
37 #include <linux/device.h>
38 #include <asm/uaccess.h>
39 #include <asm/semaphore.h>
40 #include <asm/cacheflush.h>
41 #include "module-verify.h"
46 #define DEBUGP(fmt , a...)
49 #ifndef ARCH_SHF_SMALL
50 #define ARCH_SHF_SMALL 0
53 /* If this is set, the section belongs in the init part of the module */
54 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
56 /* Protects module list */
57 static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
59 /* List of modules, protected by module_mutex AND modlist_lock */
60 static DECLARE_MUTEX(module_mutex);
61 static LIST_HEAD(modules);
63 static DECLARE_MUTEX(notify_mutex);
64 static struct notifier_block * module_notify_list;
66 int register_module_notifier(struct notifier_block * nb)
70 err = notifier_chain_register(&module_notify_list, nb);
74 EXPORT_SYMBOL(register_module_notifier);
76 int unregister_module_notifier(struct notifier_block * nb)
80 err = notifier_chain_unregister(&module_notify_list, nb);
84 EXPORT_SYMBOL(unregister_module_notifier);
86 /* We require a truly strong try_module_get() */
87 static inline int strong_try_module_get(struct module *mod)
89 if (mod && mod->state == MODULE_STATE_COMING)
91 return try_module_get(mod);
94 /* A thread that wants to hold a reference to a module only while it
95 * is running can call ths to safely exit.
96 * nfsd and lockd use this.
98 void __module_put_and_exit(struct module *mod, long code)
103 EXPORT_SYMBOL(__module_put_and_exit);
105 /* Find a module section: 0 means not found. */
106 static unsigned int find_sec(Elf_Ehdr *hdr,
108 const char *secstrings,
113 for (i = 1; i < hdr->e_shnum; i++)
114 /* Alloc bit cleared means "ignore it." */
115 if ((sechdrs[i].sh_flags & SHF_ALLOC)
116 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
121 /* Provided by the linker */
122 extern const struct kernel_symbol __start___ksymtab[];
123 extern const struct kernel_symbol __stop___ksymtab[];
124 extern const struct kernel_symbol __start___ksymtab_gpl[];
125 extern const struct kernel_symbol __stop___ksymtab_gpl[];
126 extern const unsigned long __start___kcrctab[];
127 extern const unsigned long __start___kcrctab_gpl[];
129 #ifndef CONFIG_MODVERSIONS
130 #define symversion(base, idx) NULL
132 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
135 /* Find a symbol, return value, crc and module which owns it */
136 static unsigned long __find_symbol(const char *name,
137 struct module **owner,
138 const unsigned long **crc,
144 /* Core kernel first. */
146 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
147 if (strcmp(__start___ksymtab[i].name, name) == 0) {
148 *crc = symversion(__start___kcrctab, i);
149 return __start___ksymtab[i].value;
153 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
154 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
155 *crc = symversion(__start___kcrctab_gpl, i);
156 return __start___ksymtab_gpl[i].value;
160 /* Now try modules. */
161 list_for_each_entry(mod, &modules, list) {
163 for (i = 0; i < mod->num_syms; i++)
164 if (strcmp(mod->syms[i].name, name) == 0) {
165 *crc = symversion(mod->crcs, i);
166 return mod->syms[i].value;
170 for (i = 0; i < mod->num_gpl_syms; i++) {
171 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
172 *crc = symversion(mod->gpl_crcs, i);
173 return mod->gpl_syms[i].value;
178 DEBUGP("Failed to find symbol %s\n", name);
182 /* Find a symbol in this elf symbol table */
183 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
184 unsigned int symindex,
189 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
191 /* Search (defined) internal symbols first. */
192 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
193 if (sym[i].st_shndx != SHN_UNDEF
194 && strcmp(name, strtab + sym[i].st_name) == 0)
195 return sym[i].st_value;
200 /* Search for module by name: must hold module_mutex. */
201 static struct module *find_module(const char *name)
205 list_for_each_entry(mod, &modules, list) {
206 if (strcmp(mod->name, name) == 0)
213 /* Number of blocks used and allocated. */
214 static unsigned int pcpu_num_used, pcpu_num_allocated;
215 /* Size of each block. -ve means used. */
216 static int *pcpu_size;
218 static int split_block(unsigned int i, unsigned short size)
220 /* Reallocation required? */
221 if (pcpu_num_used + 1 > pcpu_num_allocated) {
222 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
227 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
228 pcpu_num_allocated *= 2;
233 /* Insert a new subblock */
234 memmove(&pcpu_size[i+1], &pcpu_size[i],
235 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
238 pcpu_size[i+1] -= size;
243 static inline unsigned int block_size(int val)
250 /* Created by linker magic */
251 extern char __per_cpu_start[], __per_cpu_end[];
253 static void *percpu_modalloc(unsigned long size, unsigned long align)
259 BUG_ON(align > SMP_CACHE_BYTES);
261 ptr = __per_cpu_start;
262 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
263 /* Extra for alignment requirement. */
264 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
265 BUG_ON(i == 0 && extra != 0);
267 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
270 /* Transfer extra to previous block. */
271 if (pcpu_size[i-1] < 0)
272 pcpu_size[i-1] -= extra;
274 pcpu_size[i-1] += extra;
275 pcpu_size[i] -= extra;
278 /* Split block if warranted */
279 if (pcpu_size[i] - size > sizeof(unsigned long))
280 if (!split_block(i, size))
284 pcpu_size[i] = -pcpu_size[i];
288 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
293 static void percpu_modfree(void *freeme)
296 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
298 /* First entry is core kernel percpu data. */
299 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
301 pcpu_size[i] = -pcpu_size[i];
308 /* Merge with previous? */
309 if (pcpu_size[i-1] >= 0) {
310 pcpu_size[i-1] += pcpu_size[i];
312 memmove(&pcpu_size[i], &pcpu_size[i+1],
313 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
316 /* Merge with next? */
317 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
318 pcpu_size[i] += pcpu_size[i+1];
320 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
321 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
325 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
327 const char *secstrings)
329 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
332 static int percpu_modinit(void)
335 pcpu_num_allocated = 2;
336 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
338 /* Static in-kernel percpu data (used). */
339 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
341 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
342 if (pcpu_size[1] < 0) {
343 printk(KERN_ERR "No per-cpu room for modules.\n");
349 __initcall(percpu_modinit);
350 #else /* ... !CONFIG_SMP */
351 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
355 static inline void percpu_modfree(void *pcpuptr)
359 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
361 const char *secstrings)
365 static inline void percpu_modcopy(void *pcpudst, const void *src,
368 /* pcpusec should be 0, and size of that section should be 0. */
371 #endif /* CONFIG_SMP */
373 #ifdef CONFIG_MODULE_UNLOAD
374 /* Init the unload section of the module. */
375 static void module_unload_init(struct module *mod)
379 INIT_LIST_HEAD(&mod->modules_which_use_me);
380 for (i = 0; i < NR_CPUS; i++)
381 local_set(&mod->ref[i].count, 0);
382 /* Hold reference count during initialization. */
383 local_set(&mod->ref[smp_processor_id()].count, 1);
384 /* Backwards compatibility macros put refcount during init. */
385 mod->waiter = current;
388 /* modules using other modules */
391 struct list_head list;
392 struct module *module_which_uses;
395 /* Does a already use b? */
396 static int already_uses(struct module *a, struct module *b)
398 struct module_use *use;
400 list_for_each_entry(use, &b->modules_which_use_me, list) {
401 if (use->module_which_uses == a) {
402 DEBUGP("%s uses %s!\n", a->name, b->name);
406 DEBUGP("%s does not use %s!\n", a->name, b->name);
410 /* Module a uses b */
411 static int use_module(struct module *a, struct module *b)
413 struct module_use *use;
414 if (b == NULL || already_uses(a, b)) return 1;
416 if (!strong_try_module_get(b))
419 DEBUGP("Allocating new usage for %s.\n", a->name);
420 use = kmalloc(sizeof(*use), GFP_ATOMIC);
422 printk("%s: out of memory loading\n", a->name);
427 use->module_which_uses = a;
428 list_add(&use->list, &b->modules_which_use_me);
432 /* Clear the unload stuff of the module. */
433 static void module_unload_free(struct module *mod)
437 list_for_each_entry(i, &modules, list) {
438 struct module_use *use;
440 list_for_each_entry(use, &i->modules_which_use_me, list) {
441 if (use->module_which_uses == mod) {
442 DEBUGP("%s unusing %s\n", mod->name, i->name);
444 list_del(&use->list);
446 /* There can be at most one match. */
453 #ifdef CONFIG_MODULE_FORCE_UNLOAD
454 static inline int try_force(unsigned int flags)
456 int ret = (flags & O_TRUNC);
458 tainted |= TAINT_FORCED_MODULE;
462 static inline int try_force(unsigned int flags)
466 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
475 /* Whole machine is stopped with interrupts off when this runs. */
476 static inline int __try_stop_module(void *_sref)
478 struct stopref *sref = _sref;
480 /* If it's not unused, quit unless we are told to block. */
481 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
482 if (!(*sref->forced = try_force(sref->flags)))
486 /* Mark it as dying. */
487 sref->mod->state = MODULE_STATE_GOING;
491 static int try_stop_module(struct module *mod, int flags, int *forced)
493 struct stopref sref = { mod, flags, forced };
495 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
498 unsigned int module_refcount(struct module *mod)
500 unsigned int i, total = 0;
502 for (i = 0; i < NR_CPUS; i++)
503 total += local_read(&mod->ref[i].count);
506 EXPORT_SYMBOL(module_refcount);
508 /* This exists whether we can unload or not */
509 static void free_module(struct module *mod);
511 static void wait_for_zero_refcount(struct module *mod)
513 /* Since we might sleep for some time, drop the semaphore first */
516 DEBUGP("Looking at refcount...\n");
517 set_current_state(TASK_UNINTERRUPTIBLE);
518 if (module_refcount(mod) == 0)
522 current->state = TASK_RUNNING;
527 sys_delete_module(const char __user *name_user, unsigned int flags)
530 char name[MODULE_NAME_LEN];
533 if (!capable(CAP_SYS_MODULE))
536 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
538 name[MODULE_NAME_LEN-1] = '\0';
540 if (down_interruptible(&module_mutex) != 0)
543 mod = find_module(name);
549 if (!list_empty(&mod->modules_which_use_me)) {
550 /* Other modules depend on us: get rid of them first. */
555 /* Doing init or already dying? */
556 if (mod->state != MODULE_STATE_LIVE) {
557 /* FIXME: if (force), slam module count and wake up
559 DEBUGP("%s already dying\n", mod->name);
564 /* If it has an init func, it must have an exit func to unload */
565 if ((mod->init != NULL && mod->exit == NULL)
567 forced = try_force(flags);
569 /* This module can't be removed */
575 /* Set this up before setting mod->state */
576 mod->waiter = current;
578 /* Stop the machine so refcounts can't move and disable module. */
579 ret = try_stop_module(mod, flags, &forced);
583 /* Never wait if forced. */
584 if (!forced && module_refcount(mod) != 0)
585 wait_for_zero_refcount(mod);
587 /* Final destruction now noone is using it. */
588 if (mod->exit != NULL) {
600 static void print_unload_info(struct seq_file *m, struct module *mod)
602 struct module_use *use;
603 int printed_something = 0;
605 seq_printf(m, " %u ", module_refcount(mod));
607 /* Always include a trailing , so userspace can differentiate
608 between this and the old multi-field proc format. */
609 list_for_each_entry(use, &mod->modules_which_use_me, list) {
610 printed_something = 1;
611 seq_printf(m, "%s,", use->module_which_uses->name);
615 printed_something = 1;
616 seq_printf(m, "[unsafe],");
619 if (mod->init != NULL && mod->exit == NULL) {
620 printed_something = 1;
621 seq_printf(m, "[permanent],");
624 if (!printed_something)
628 void __symbol_put(const char *symbol)
630 struct module *owner;
632 const unsigned long *crc;
634 spin_lock_irqsave(&modlist_lock, flags);
635 if (!__find_symbol(symbol, &owner, &crc, 1))
638 spin_unlock_irqrestore(&modlist_lock, flags);
640 EXPORT_SYMBOL(__symbol_put);
642 void symbol_put_addr(void *addr)
646 spin_lock_irqsave(&modlist_lock, flags);
647 if (!kernel_text_address((unsigned long)addr))
650 module_put(module_text_address((unsigned long)addr));
651 spin_unlock_irqrestore(&modlist_lock, flags);
653 EXPORT_SYMBOL_GPL(symbol_put_addr);
655 static ssize_t show_refcnt(struct module *mod, char *buffer)
657 /* sysfs holds a reference */
658 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
661 static struct module_attribute refcnt = {
662 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
666 #else /* !CONFIG_MODULE_UNLOAD */
667 static void print_unload_info(struct seq_file *m, struct module *mod)
669 /* We don't know the usage count, or what modules are using. */
670 seq_printf(m, " - -");
673 static inline void module_unload_free(struct module *mod)
677 static inline int use_module(struct module *a, struct module *b)
679 return strong_try_module_get(b);
682 static inline void module_unload_init(struct module *mod)
687 sys_delete_module(const char __user *name_user, unsigned int flags)
692 #endif /* CONFIG_MODULE_UNLOAD */
694 #ifdef CONFIG_OBSOLETE_MODPARM
695 /* Bounds checking done below */
696 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
698 strcpy(kp->arg, val);
702 int set_obsolete(const char *val, struct kernel_param *kp)
704 unsigned int min, max;
705 unsigned int size, maxsize;
709 struct obsolete_modparm *obsparm = kp->arg;
712 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
716 /* type is: [min[-max]]{b,h,i,l,s} */
718 min = simple_strtol(p, &endp, 10);
719 if (endp == obsparm->type)
721 else if (*endp == '-') {
723 max = simple_strtol(p, &endp, 10);
728 return param_array(kp->name, val, min, max, obsparm->addr,
729 1, param_set_byte, &dummy);
731 return param_array(kp->name, val, min, max, obsparm->addr,
732 sizeof(short), param_set_short, &dummy);
734 return param_array(kp->name, val, min, max, obsparm->addr,
735 sizeof(int), param_set_int, &dummy);
737 return param_array(kp->name, val, min, max, obsparm->addr,
738 sizeof(long), param_set_long, &dummy);
740 return param_array(kp->name, val, min, max, obsparm->addr,
741 sizeof(char *), param_set_charp, &dummy);
744 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
745 and the decl is "char xxx[5][50];" */
747 maxsize = simple_strtol(p, &endp, 10);
748 /* We check lengths here (yes, this is a hack). */
750 while (p[size = strcspn(p, ",")]) {
757 return param_array(kp->name, val, min, max, obsparm->addr,
758 maxsize, obsparm_copy_string, &dummy);
760 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
764 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
768 static int obsolete_params(const char *name,
770 struct obsolete_modparm obsparm[],
773 unsigned int symindex,
776 struct kernel_param *kp;
780 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
784 for (i = 0; i < num; i++) {
785 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
787 snprintf(sym_name, sizeof(sym_name), "%s%s",
788 MODULE_SYMBOL_PREFIX, obsparm[i].name);
790 kp[i].name = obsparm[i].name;
792 kp[i].set = set_obsolete;
795 = (void *)find_local_symbol(sechdrs, symindex, strtab,
797 if (!obsparm[i].addr) {
798 printk("%s: falsely claims to have parameter %s\n",
799 name, obsparm[i].name);
803 kp[i].arg = &obsparm[i];
806 ret = parse_args(name, args, kp, num, NULL);
812 static int obsolete_params(const char *name,
814 struct obsolete_modparm obsparm[],
817 unsigned int symindex,
821 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
825 #endif /* CONFIG_OBSOLETE_MODPARM */
827 static const char vermagic[] = VERMAGIC_STRING;
829 #ifdef CONFIG_MODVERSIONS
830 static int check_version(Elf_Shdr *sechdrs,
831 unsigned int versindex,
834 const unsigned long *crc)
836 unsigned int i, num_versions;
837 struct modversion_info *versions;
839 /* Exporting module didn't supply crcs? OK, we're already tainted. */
843 versions = (void *) sechdrs[versindex].sh_addr;
844 num_versions = sechdrs[versindex].sh_size
845 / sizeof(struct modversion_info);
847 for (i = 0; i < num_versions; i++) {
848 if (strcmp(versions[i].name, symname) != 0)
851 if (versions[i].crc == *crc)
853 printk("%s: disagrees about version of symbol %s\n",
855 DEBUGP("Found checksum %lX vs module %lX\n",
856 *crc, versions[i].crc);
859 /* Not in module's version table. OK, but that taints the kernel. */
860 if (!(tainted & TAINT_FORCED_MODULE)) {
861 printk("%s: no version for \"%s\" found: kernel tainted.\n",
863 tainted |= TAINT_FORCED_MODULE;
868 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
869 unsigned int versindex,
872 const unsigned long *crc;
873 struct module *owner;
875 if (!__find_symbol("struct_module", &owner, &crc, 1))
877 return check_version(sechdrs, versindex, "struct_module", mod,
881 /* First part is kernel version, which we ignore. */
882 static inline int same_magic(const char *amagic, const char *bmagic)
884 amagic += strcspn(amagic, " ");
885 bmagic += strcspn(bmagic, " ");
886 return strcmp(amagic, bmagic) == 0;
889 static inline int check_version(Elf_Shdr *sechdrs,
890 unsigned int versindex,
893 const unsigned long *crc)
898 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
899 unsigned int versindex,
905 static inline int same_magic(const char *amagic, const char *bmagic)
907 return strcmp(amagic, bmagic) == 0;
909 #endif /* CONFIG_MODVERSIONS */
911 /* Resolve a symbol for this module. I.e. if we find one, record usage.
912 Must be holding module_mutex. */
913 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
914 unsigned int versindex,
918 struct module *owner;
920 const unsigned long *crc;
922 spin_lock_irq(&modlist_lock);
923 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
925 /* use_module can fail due to OOM, or module unloading */
926 if (!check_version(sechdrs, versindex, name, mod, crc) ||
927 !use_module(mod, owner))
930 spin_unlock_irq(&modlist_lock);
936 * /sys/module/foo/sections stuff
937 * J. Corbet <corbet@lwn.net>
939 #ifdef CONFIG_KALLSYMS
940 static void module_sect_attrs_release(struct kobject *kobj)
942 kfree(container_of(kobj, struct module_sections, kobj));
945 static ssize_t module_sect_show(struct kobject *kobj, struct attribute *attr,
948 struct module_sect_attr *sattr =
949 container_of(attr, struct module_sect_attr, attr);
950 return sprintf(buf, "0x%lx\n", sattr->address);
953 static struct sysfs_ops module_sect_ops = {
954 .show = module_sect_show,
957 static struct kobj_type module_sect_ktype = {
958 .sysfs_ops = &module_sect_ops,
959 .release = module_sect_attrs_release,
962 static void add_sect_attrs(struct module *mod, unsigned int nsect,
963 char *secstrings, Elf_Shdr *sechdrs)
965 unsigned int nloaded = 0, i;
966 struct module_sect_attr *sattr;
971 /* Count loaded sections and allocate structures */
972 for (i = 0; i < nsect; i++)
973 if (sechdrs[i].sh_flags & SHF_ALLOC)
975 mod->sect_attrs = kmalloc(sizeof(struct module_sections) +
976 nloaded*sizeof(mod->sect_attrs->attrs[0]), GFP_KERNEL);
977 if (! mod->sect_attrs)
980 /* sections entry setup */
981 memset(mod->sect_attrs, 0, sizeof(struct module_sections));
982 if (kobject_set_name(&mod->sect_attrs->kobj, "sections"))
984 mod->sect_attrs->kobj.parent = &mod->mkobj->kobj;
985 mod->sect_attrs->kobj.ktype = &module_sect_ktype;
986 if (kobject_register(&mod->sect_attrs->kobj))
989 /* And the section attributes. */
990 sattr = &mod->sect_attrs->attrs[0];
991 for (i = 0; i < nsect; i++) {
992 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
994 sattr->address = sechdrs[i].sh_addr;
995 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
996 MODULE_SECT_NAME_LEN);
997 sattr->attr.name = sattr->name;
998 sattr->attr.owner = mod;
999 sattr->attr.mode = S_IRUGO;
1000 (void) sysfs_create_file(&mod->sect_attrs->kobj, &sattr->attr);
1005 kfree(mod->sect_attrs);
1006 mod->sect_attrs = NULL;
1009 static void remove_sect_attrs(struct module *mod)
1011 if (mod->sect_attrs) {
1012 kobject_unregister(&mod->sect_attrs->kobj);
1013 mod->sect_attrs = NULL;
1019 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1020 char *sectstrings, Elf_Shdr *sechdrs)
1024 static inline void remove_sect_attrs(struct module *mod)
1027 #endif /* CONFIG_KALLSYMS */
1030 #ifdef CONFIG_MODULE_UNLOAD
1031 static inline int module_add_refcnt_attr(struct module *mod)
1033 return sysfs_create_file(&mod->mkobj->kobj, &refcnt.attr);
1035 static void module_remove_refcnt_attr(struct module *mod)
1037 return sysfs_remove_file(&mod->mkobj->kobj, &refcnt.attr);
1040 static inline int module_add_refcnt_attr(struct module *mod)
1044 static void module_remove_refcnt_attr(struct module *mod)
1050 static int mod_sysfs_setup(struct module *mod,
1051 struct kernel_param *kparam,
1052 unsigned int num_params)
1056 mod->mkobj = kmalloc(sizeof(struct module_kobject), GFP_KERNEL);
1060 memset(&mod->mkobj->kobj, 0, sizeof(mod->mkobj->kobj));
1061 err = kobject_set_name(&mod->mkobj->kobj, "%s", mod->name);
1064 kobj_set_kset_s(mod->mkobj, module_subsys);
1065 mod->mkobj->mod = mod;
1066 err = kobject_register(&mod->mkobj->kobj);
1070 err = module_add_refcnt_attr(mod);
1074 err = module_param_sysfs_setup(mod, kparam, num_params);
1081 /* Calls module_kobj_release */
1082 kobject_unregister(&mod->mkobj->kobj);
1089 static void mod_kobject_remove(struct module *mod)
1091 module_remove_refcnt_attr(mod);
1092 module_param_sysfs_remove(mod);
1094 /* Calls module_kobj_release */
1095 kobject_unregister(&mod->mkobj->kobj);
1098 /* Free a module, remove from lists, etc (must hold module mutex). */
1099 static void free_module(struct module *mod)
1101 /* Delete from various lists */
1102 spin_lock_irq(&modlist_lock);
1103 list_del(&mod->list);
1104 spin_unlock_irq(&modlist_lock);
1106 remove_sect_attrs(mod);
1107 mod_kobject_remove(mod);
1109 /* Arch-specific cleanup. */
1110 module_arch_cleanup(mod);
1112 /* Module unload stuff */
1113 module_unload_free(mod);
1115 /* This may be NULL, but that's OK */
1116 module_free(mod, mod->module_init);
1119 percpu_modfree(mod->percpu);
1121 /* Finally, free the core (containing the module structure) */
1122 module_free(mod, mod->module_core);
1125 void *__symbol_get(const char *symbol)
1127 struct module *owner;
1128 unsigned long value, flags;
1129 const unsigned long *crc;
1131 spin_lock_irqsave(&modlist_lock, flags);
1132 value = __find_symbol(symbol, &owner, &crc, 1);
1133 if (value && !strong_try_module_get(owner))
1135 spin_unlock_irqrestore(&modlist_lock, flags);
1137 return (void *)value;
1139 EXPORT_SYMBOL_GPL(__symbol_get);
1141 /* Change all symbols so that sh_value encodes the pointer directly. */
1142 static int simplify_symbols(Elf_Shdr *sechdrs,
1143 unsigned int symindex,
1145 unsigned int versindex,
1146 unsigned int pcpuindex,
1149 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1150 unsigned long secbase;
1151 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1154 for (i = 1; i < n; i++) {
1155 switch (sym[i].st_shndx) {
1157 /* We compiled with -fno-common. These are not
1158 supposed to happen. */
1159 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1160 printk("%s: please compile with -fno-common\n",
1166 /* Don't need to do anything */
1167 DEBUGP("Absolute symbol: 0x%08lx\n",
1168 (long)sym[i].st_value);
1173 = resolve_symbol(sechdrs, versindex,
1174 strtab + sym[i].st_name, mod);
1176 /* Ok if resolved. */
1177 if (sym[i].st_value != 0)
1180 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1183 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1184 mod->name, strtab + sym[i].st_name);
1189 /* Divert to percpu allocation if a percpu var. */
1190 if (sym[i].st_shndx == pcpuindex)
1191 secbase = (unsigned long)mod->percpu;
1193 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1194 sym[i].st_value += secbase;
1202 /* Update size with this section: return offset. */
1203 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1207 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1208 *size = ret + sechdr->sh_size;
1212 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1213 might -- code, read-only data, read-write data, small data. Tally
1214 sizes, and place the offsets into sh_entsize fields: high bit means it
1216 static void layout_sections(struct module *mod,
1217 const Elf_Ehdr *hdr,
1219 const char *secstrings)
1221 static unsigned long const masks[][2] = {
1222 /* NOTE: all executable code must be the first section
1223 * in this array; otherwise modify the text_size
1224 * finder in the two loops below */
1225 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1226 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1227 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1228 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1232 for (i = 0; i < hdr->e_shnum; i++)
1233 sechdrs[i].sh_entsize = ~0UL;
1235 DEBUGP("Core section allocation order:\n");
1236 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1237 for (i = 0; i < hdr->e_shnum; ++i) {
1238 Elf_Shdr *s = &sechdrs[i];
1240 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1241 || (s->sh_flags & masks[m][1])
1242 || s->sh_entsize != ~0UL
1243 || strncmp(secstrings + s->sh_name,
1246 s->sh_entsize = get_offset(&mod->core_size, s);
1247 DEBUGP("\t%s\n", secstrings + s->sh_name);
1250 mod->core_text_size = mod->core_size;
1253 DEBUGP("Init section allocation order:\n");
1254 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1255 for (i = 0; i < hdr->e_shnum; ++i) {
1256 Elf_Shdr *s = &sechdrs[i];
1258 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1259 || (s->sh_flags & masks[m][1])
1260 || s->sh_entsize != ~0UL
1261 || strncmp(secstrings + s->sh_name,
1264 s->sh_entsize = (get_offset(&mod->init_size, s)
1265 | INIT_OFFSET_MASK);
1266 DEBUGP("\t%s\n", secstrings + s->sh_name);
1269 mod->init_text_size = mod->init_size;
1273 static inline int license_is_gpl_compatible(const char *license)
1275 return (strcmp(license, "GPL") == 0
1276 || strcmp(license, "GPL v2") == 0
1277 || strcmp(license, "GPL and additional rights") == 0
1278 || strcmp(license, "Dual BSD/GPL") == 0
1279 || strcmp(license, "Dual MPL/GPL") == 0);
1282 static void set_license(struct module *mod, const char *license)
1285 license = "unspecified";
1287 mod->license_gplok = license_is_gpl_compatible(license);
1288 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1289 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1290 mod->name, license);
1291 tainted |= TAINT_PROPRIETARY_MODULE;
1295 /* Parse tag=value strings from .modinfo section */
1296 static char *next_string(char *string, unsigned long *secsize)
1298 /* Skip non-zero chars */
1301 if ((*secsize)-- <= 1)
1305 /* Skip any zero padding. */
1306 while (!string[0]) {
1308 if ((*secsize)-- <= 1)
1314 static char *get_modinfo(Elf_Shdr *sechdrs,
1319 unsigned int taglen = strlen(tag);
1320 unsigned long size = sechdrs[info].sh_size;
1322 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1323 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1324 return p + taglen + 1;
1329 #ifdef CONFIG_KALLSYMS
1330 int is_exported(const char *name, const struct module *mod)
1335 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1336 if (strcmp(__start___ksymtab[i].name, name) == 0)
1340 for (i = 0; i < mod->num_syms; i++)
1341 if (strcmp(mod->syms[i].name, name) == 0)
1347 static char elf_type(const Elf_Sym *sym,
1349 const char *secstrings,
1352 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1353 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1358 if (sym->st_shndx == SHN_UNDEF)
1360 if (sym->st_shndx == SHN_ABS)
1362 if (sym->st_shndx >= SHN_LORESERVE)
1364 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1366 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1367 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1368 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1370 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1375 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1376 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1381 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1382 ".debug", strlen(".debug")) == 0)
1387 static void add_kallsyms(struct module *mod,
1389 unsigned int symindex,
1390 unsigned int strindex,
1391 const char *secstrings)
1395 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1396 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1397 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1399 /* Set types up while we still have access to sections. */
1400 for (i = 0; i < mod->num_symtab; i++)
1401 mod->symtab[i].st_info
1402 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1405 static inline void add_kallsyms(struct module *mod,
1407 unsigned int symindex,
1408 unsigned int strindex,
1409 const char *secstrings)
1412 #endif /* CONFIG_KALLSYMS */
1414 /* Allocate and load the module: note that size of section 0 is always
1415 zero, and we rely on this for optional sections. */
1416 static struct module *load_module(void __user *umod,
1418 const char __user *uargs)
1422 char *secstrings, *args, *modmagic, *strtab = NULL;
1423 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1424 exportindex, modindex, obsparmindex, infoindex, gplindex,
1425 crcindex, gplcrcindex, versindex, pcpuindex;
1429 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1430 struct exception_table_entry *extable;
1433 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1435 if (len < sizeof(*hdr))
1436 return ERR_PTR(-ENOEXEC);
1438 /* Suck in entire file: we'll want most of it. */
1439 /* vmalloc barfs on "unusual" numbers. Check here */
1440 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1441 return ERR_PTR(-ENOMEM);
1442 if (copy_from_user(hdr, umod, len) != 0) {
1447 /* Sanity checks against insmoding binaries or wrong arch,
1448 weird elf version */
1449 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1450 || hdr->e_type != ET_REL
1451 || !elf_check_arch(hdr)
1452 || hdr->e_shentsize != sizeof(*sechdrs)) {
1457 /* verify the module (validates ELF and checks signature) */
1459 err = module_verify(hdr, len);
1465 /* Convenience variables */
1466 sechdrs = (void *)hdr + hdr->e_shoff;
1467 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1468 sechdrs[0].sh_addr = 0;
1470 for (i = 1; i < hdr->e_shnum; i++) {
1471 if (sechdrs[i].sh_type != SHT_NOBITS
1472 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1475 /* Mark all sections sh_addr with their address in the
1477 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1479 /* Internal symbols and strings. */
1480 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1482 strindex = sechdrs[i].sh_link;
1483 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1485 #ifndef CONFIG_MODULE_UNLOAD
1486 /* Don't load .exit sections */
1487 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1488 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1492 modindex = find_sec(hdr, sechdrs, secstrings,
1493 ".gnu.linkonce.this_module");
1495 printk(KERN_WARNING "No module found in object\n");
1499 mod = (void *)sechdrs[modindex].sh_addr;
1500 mod->gpgsig_ok = gpgsig_ok;
1502 if (symindex == 0) {
1503 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1509 /* Optional sections */
1510 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1511 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1512 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1513 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1514 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1515 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1516 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1517 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1518 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1519 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1521 /* Don't keep modinfo section */
1522 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1523 #ifdef CONFIG_KALLSYMS
1524 /* Keep symbol and string tables for decoding later. */
1525 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1526 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1529 /* Check module struct version now, before we try to use module. */
1530 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1535 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1536 /* This is allowed: modprobe --force will invalidate it. */
1538 tainted |= TAINT_FORCED_MODULE;
1539 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1541 } else if (!same_magic(modmagic, vermagic)) {
1542 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1543 mod->name, modmagic, vermagic);
1548 /* Now copy in args */
1549 arglen = strlen_user(uargs);
1554 args = kmalloc(arglen, GFP_KERNEL);
1559 if (copy_from_user(args, uargs, arglen) != 0) {
1564 if (find_module(mod->name)) {
1569 mod->state = MODULE_STATE_COMING;
1571 /* Allow arches to frob section contents and sizes. */
1572 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1577 /* We have a special allocation for this section. */
1578 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1579 sechdrs[pcpuindex].sh_addralign);
1584 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1585 mod->percpu = percpu;
1588 /* Determine total sizes, and put offsets in sh_entsize. For now
1589 this is done generically; there doesn't appear to be any
1590 special cases for the architectures. */
1591 layout_sections(mod, hdr, sechdrs, secstrings);
1593 /* Do the allocs. */
1594 ptr = module_alloc(mod->core_size);
1599 memset(ptr, 0, mod->core_size);
1600 mod->module_core = ptr;
1602 ptr = module_alloc(mod->init_size);
1603 if (!ptr && mod->init_size) {
1607 memset(ptr, 0, mod->init_size);
1608 mod->module_init = ptr;
1610 /* Transfer each section which specifies SHF_ALLOC */
1611 DEBUGP("final section addresses:\n");
1612 for (i = 0; i < hdr->e_shnum; i++) {
1615 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1618 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1619 dest = mod->module_init
1620 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1622 dest = mod->module_core + sechdrs[i].sh_entsize;
1624 if (sechdrs[i].sh_type != SHT_NOBITS)
1625 memcpy(dest, (void *)sechdrs[i].sh_addr,
1626 sechdrs[i].sh_size);
1627 /* Update sh_addr to point to copy in image. */
1628 sechdrs[i].sh_addr = (unsigned long)dest;
1629 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1631 /* Module has been moved. */
1632 mod = (void *)sechdrs[modindex].sh_addr;
1634 /* Now we've moved module, initialize linked lists, etc. */
1635 module_unload_init(mod);
1637 /* Set up license info based on the info section */
1638 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1640 /* Fix up syms, so that st_value is a pointer to location. */
1641 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1646 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1647 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1648 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1650 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1651 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1652 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1654 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1656 #ifdef CONFIG_MODVERSIONS
1657 if ((mod->num_syms && !crcindex) ||
1658 (mod->num_gpl_syms && !gplcrcindex)) {
1659 printk(KERN_WARNING "%s: No versions for exported symbols."
1660 " Tainting kernel.\n", mod->name);
1661 tainted |= TAINT_FORCED_MODULE;
1665 /* Now do relocations. */
1666 for (i = 1; i < hdr->e_shnum; i++) {
1667 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1668 unsigned int info = sechdrs[i].sh_info;
1670 /* Not a valid relocation section? */
1671 if (info >= hdr->e_shnum)
1674 /* Don't bother with non-allocated sections */
1675 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1678 if (sechdrs[i].sh_type == SHT_REL)
1679 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1680 else if (sechdrs[i].sh_type == SHT_RELA)
1681 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1687 /* Set up and sort exception table */
1688 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1689 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1690 sort_extable(extable, extable + mod->num_exentries);
1692 /* Finally, copy percpu area over. */
1693 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1694 sechdrs[pcpuindex].sh_size);
1696 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1698 err = module_finalize(hdr, sechdrs, mod);
1704 err = obsolete_params(mod->name, mod->args,
1705 (struct obsolete_modparm *)
1706 sechdrs[obsparmindex].sh_addr,
1707 sechdrs[obsparmindex].sh_size
1708 / sizeof(struct obsolete_modparm),
1710 (char *)sechdrs[strindex].sh_addr);
1712 printk(KERN_WARNING "%s: Ignoring new-style "
1713 "parameters in presence of obsolete ones\n",
1716 /* Size of section 0 is 0, so this works well if no params */
1717 err = parse_args(mod->name, mod->args,
1718 (struct kernel_param *)
1719 sechdrs[setupindex].sh_addr,
1720 sechdrs[setupindex].sh_size
1721 / sizeof(struct kernel_param),
1725 err = mod_sysfs_setup(mod,
1726 (struct kernel_param *)
1727 sechdrs[setupindex].sh_addr,
1728 sechdrs[setupindex].sh_size
1729 / sizeof(struct kernel_param));
1732 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1734 /* Get rid of temporary copy */
1741 module_arch_cleanup(mod);
1743 module_unload_free(mod);
1744 module_free(mod, mod->module_init);
1746 module_free(mod, mod->module_core);
1749 percpu_modfree(percpu);
1754 if (err < 0) return ERR_PTR(err);
1758 printk(KERN_ERR "Module len %lu truncated\n", len);
1763 /* This is where the real work happens */
1765 sys_init_module(void __user *umod,
1767 const char __user *uargs)
1772 /* Must have permission */
1773 if (!capable(CAP_SYS_MODULE))
1776 /* Only one module load at a time, please */
1777 if (down_interruptible(&module_mutex) != 0)
1780 /* Do all the hard work */
1781 mod = load_module(umod, len, uargs);
1784 return PTR_ERR(mod);
1787 /* Flush the instruction cache, since we've played with text */
1788 if (mod->module_init)
1789 flush_icache_range((unsigned long)mod->module_init,
1790 (unsigned long)mod->module_init
1792 flush_icache_range((unsigned long)mod->module_core,
1793 (unsigned long)mod->module_core + mod->core_size);
1795 /* Now sew it into the lists. They won't access us, since
1796 strong_try_module_get() will fail. */
1797 spin_lock_irq(&modlist_lock);
1798 list_add(&mod->list, &modules);
1799 spin_unlock_irq(&modlist_lock);
1801 /* Drop lock so they can recurse */
1804 down(¬ify_mutex);
1805 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1808 /* Start the module */
1809 if (mod->init != NULL)
1812 /* Init routine failed: abort. Try to protect us from
1813 buggy refcounters. */
1814 mod->state = MODULE_STATE_GOING;
1815 synchronize_kernel();
1817 printk(KERN_ERR "%s: module is now stuck!\n",
1821 down(&module_mutex);
1828 /* Now it's a first class citizen! */
1829 down(&module_mutex);
1830 mod->state = MODULE_STATE_LIVE;
1831 /* Drop initial reference. */
1833 module_free(mod, mod->module_init);
1834 mod->module_init = NULL;
1836 mod->init_text_size = 0;
1842 static inline int within(unsigned long addr, void *start, unsigned long size)
1844 return ((void *)addr >= start && (void *)addr < start + size);
1847 #ifdef CONFIG_KALLSYMS
1849 * This ignores the intensely annoying "mapping symbols" found
1850 * in ARM ELF files: $a, $t and $d.
1852 static inline int is_arm_mapping_symbol(const char *str)
1854 return str[0] == '$' && strchr("atd", str[1])
1855 && (str[2] == '\0' || str[2] == '.');
1858 static const char *get_ksymbol(struct module *mod,
1860 unsigned long *size,
1861 unsigned long *offset)
1863 unsigned int i, best = 0;
1864 unsigned long nextval;
1866 /* At worse, next value is at end of module */
1867 if (within(addr, mod->module_init, mod->init_size))
1868 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1870 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1872 /* Scan for closest preceeding symbol, and next symbol. (ELF
1873 starts real symbols at 1). */
1874 for (i = 1; i < mod->num_symtab; i++) {
1875 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1878 /* We ignore unnamed symbols: they're uninformative
1879 * and inserted at a whim. */
1880 if (mod->symtab[i].st_value <= addr
1881 && mod->symtab[i].st_value > mod->symtab[best].st_value
1882 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1883 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1885 if (mod->symtab[i].st_value > addr
1886 && mod->symtab[i].st_value < nextval
1887 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1888 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1889 nextval = mod->symtab[i].st_value;
1895 *size = nextval - mod->symtab[best].st_value;
1896 *offset = addr - mod->symtab[best].st_value;
1897 return mod->strtab + mod->symtab[best].st_name;
1900 /* For kallsyms to ask for address resolution. NULL means not found.
1901 We don't lock, as this is used for oops resolution and races are a
1903 const char *module_address_lookup(unsigned long addr,
1904 unsigned long *size,
1905 unsigned long *offset,
1910 list_for_each_entry(mod, &modules, list) {
1911 if (within(addr, mod->module_init, mod->init_size)
1912 || within(addr, mod->module_core, mod->core_size)) {
1913 *modname = mod->name;
1914 return get_ksymbol(mod, addr, size, offset);
1920 struct module *module_get_kallsym(unsigned int symnum,
1921 unsigned long *value,
1927 down(&module_mutex);
1928 list_for_each_entry(mod, &modules, list) {
1929 if (symnum < mod->num_symtab) {
1930 *value = mod->symtab[symnum].st_value;
1931 *type = mod->symtab[symnum].st_info;
1933 mod->strtab + mod->symtab[symnum].st_name,
1938 symnum -= mod->num_symtab;
1944 static unsigned long mod_find_symname(struct module *mod, const char *name)
1948 for (i = 0; i < mod->num_symtab; i++)
1949 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
1950 return mod->symtab[i].st_value;
1954 /* Look for this name: can be of form module:name. */
1955 unsigned long module_kallsyms_lookup_name(const char *name)
1959 unsigned long ret = 0;
1961 /* Don't lock: we're in enough trouble already. */
1962 if ((colon = strchr(name, ':')) != NULL) {
1964 if ((mod = find_module(name)) != NULL)
1965 ret = mod_find_symname(mod, colon+1);
1968 list_for_each_entry(mod, &modules, list)
1969 if ((ret = mod_find_symname(mod, name)) != 0)
1974 #endif /* CONFIG_KALLSYMS */
1976 /* Called by the /proc file system to return a list of modules. */
1977 static void *m_start(struct seq_file *m, loff_t *pos)
1979 struct list_head *i;
1982 down(&module_mutex);
1983 list_for_each(i, &modules) {
1992 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
1994 struct list_head *i = p;
1996 if (i->next == &modules)
2001 static void m_stop(struct seq_file *m, void *p)
2006 static int m_show(struct seq_file *m, void *p)
2008 struct module *mod = list_entry(p, struct module, list);
2009 seq_printf(m, "%s %lu",
2010 mod->name, mod->init_size + mod->core_size);
2011 print_unload_info(m, mod);
2013 /* Informative for users. */
2014 seq_printf(m, " %s",
2015 mod->state == MODULE_STATE_GOING ? "Unloading":
2016 mod->state == MODULE_STATE_COMING ? "Loading":
2018 /* Used by oprofile and other similar tools. */
2019 seq_printf(m, " 0x%p", mod->module_core);
2021 seq_printf(m, "\n");
2025 /* Format: modulename size refcount deps address
2027 Where refcount is a number or -, and deps is a comma-separated list
2030 struct seq_operations modules_op = {
2037 /* Given an address, look for it in the module exception tables. */
2038 const struct exception_table_entry *search_module_extables(unsigned long addr)
2040 unsigned long flags;
2041 const struct exception_table_entry *e = NULL;
2044 spin_lock_irqsave(&modlist_lock, flags);
2045 list_for_each_entry(mod, &modules, list) {
2046 if (mod->num_exentries == 0)
2049 e = search_extable(mod->extable,
2050 mod->extable + mod->num_exentries - 1,
2055 spin_unlock_irqrestore(&modlist_lock, flags);
2057 /* Now, if we found one, we are running inside it now, hence
2058 we cannot unload the module, hence no refcnt needed. */
2062 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2063 struct module *__module_text_address(unsigned long addr)
2067 list_for_each_entry(mod, &modules, list)
2068 if (within(addr, mod->module_init, mod->init_text_size)
2069 || within(addr, mod->module_core, mod->core_text_size))
2074 struct module *module_text_address(unsigned long addr)
2077 unsigned long flags;
2079 spin_lock_irqsave(&modlist_lock, flags);
2080 mod = __module_text_address(addr);
2081 spin_unlock_irqrestore(&modlist_lock, flags);
2086 /* Don't grab lock, we're oopsing. */
2087 void print_modules(void)
2091 printk("Modules linked in:");
2092 list_for_each_entry(mod, &modules, list) {
2093 printk(" %s", mod->name);
2094 #if CONFIG_MODULE_SIG
2095 if (!mod->gpgsig_ok)
2102 void module_add_driver(struct module *mod, struct device_driver *drv)
2109 /* Don't check return code; this call is idempotent */
2110 sysfs_create_link(&drv->kobj, &mod->mkobj->kobj, "module");
2112 EXPORT_SYMBOL(module_add_driver);
2114 void module_remove_driver(struct device_driver *drv)
2118 sysfs_remove_link(&drv->kobj, "module");
2120 EXPORT_SYMBOL(module_remove_driver);
2122 #ifdef CONFIG_MODVERSIONS
2123 /* Generate the signature for struct module here, too, for modversions. */
2124 void struct_module(struct module *mod) { return; }
2125 EXPORT_SYMBOL(struct_module);