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 <asm/uaccess.h>
38 #include <asm/semaphore.h>
39 #include <asm/cacheflush.h>
44 #define DEBUGP(fmt , a...)
47 #ifndef ARCH_SHF_SMALL
48 #define ARCH_SHF_SMALL 0
51 /* If this is set, the section belongs in the init part of the module */
52 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
54 /* Protects module list */
55 static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
57 /* List of modules, protected by module_mutex AND modlist_lock */
58 static DECLARE_MUTEX(module_mutex);
59 static LIST_HEAD(modules);
61 static DECLARE_MUTEX(notify_mutex);
62 static struct notifier_block * module_notify_list;
64 int register_module_notifier(struct notifier_block * nb)
68 err = notifier_chain_register(&module_notify_list, nb);
72 EXPORT_SYMBOL(register_module_notifier);
74 int unregister_module_notifier(struct notifier_block * nb)
78 err = 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 /* Stub function for modules which don't have an initfn */
97 EXPORT_SYMBOL(init_module);
99 /* A thread that wants to hold a reference to a module only while it
100 * is running can call ths to safely exit.
101 * nfsd and lockd use this.
103 void __module_put_and_exit(struct module *mod, long code)
108 EXPORT_SYMBOL(__module_put_and_exit);
110 /* Find a module section: 0 means not found. */
111 static unsigned int find_sec(Elf_Ehdr *hdr,
113 const char *secstrings,
118 for (i = 1; i < hdr->e_shnum; i++)
119 /* Alloc bit cleared means "ignore it." */
120 if ((sechdrs[i].sh_flags & SHF_ALLOC)
121 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
126 /* Provided by the linker */
127 extern const struct kernel_symbol __start___ksymtab[];
128 extern const struct kernel_symbol __stop___ksymtab[];
129 extern const struct kernel_symbol __start___ksymtab_gpl[];
130 extern const struct kernel_symbol __stop___ksymtab_gpl[];
131 extern const unsigned long __start___kcrctab[];
132 extern const unsigned long __start___kcrctab_gpl[];
134 #ifndef CONFIG_MODVERSIONS
135 #define symversion(base, idx) NULL
137 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
140 /* Find a symbol, return value, crc and module which owns it */
141 static unsigned long __find_symbol(const char *name,
142 struct module **owner,
143 const unsigned long **crc,
149 /* Core kernel first. */
151 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
152 if (strcmp(__start___ksymtab[i].name, name) == 0) {
153 *crc = symversion(__start___kcrctab, i);
154 return __start___ksymtab[i].value;
158 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
159 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
160 *crc = symversion(__start___kcrctab_gpl, i);
161 return __start___ksymtab_gpl[i].value;
165 /* Now try modules. */
166 list_for_each_entry(mod, &modules, list) {
168 for (i = 0; i < mod->num_syms; i++)
169 if (strcmp(mod->syms[i].name, name) == 0) {
170 *crc = symversion(mod->crcs, i);
171 return mod->syms[i].value;
175 for (i = 0; i < mod->num_gpl_syms; i++) {
176 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
177 *crc = symversion(mod->gpl_crcs, i);
178 return mod->gpl_syms[i].value;
183 DEBUGP("Failed to find symbol %s\n", name);
187 /* Find a symbol in this elf symbol table */
188 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
189 unsigned int symindex,
194 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
196 /* Search (defined) internal symbols first. */
197 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
198 if (sym[i].st_shndx != SHN_UNDEF
199 && strcmp(name, strtab + sym[i].st_name) == 0)
200 return sym[i].st_value;
205 /* Search for module by name: must hold module_mutex. */
206 static struct module *find_module(const char *name)
210 list_for_each_entry(mod, &modules, list) {
211 if (strcmp(mod->name, name) == 0)
218 /* Number of blocks used and allocated. */
219 static unsigned int pcpu_num_used, pcpu_num_allocated;
220 /* Size of each block. -ve means used. */
221 static int *pcpu_size;
223 static int split_block(unsigned int i, unsigned short size)
225 /* Reallocation required? */
226 if (pcpu_num_used + 1 > pcpu_num_allocated) {
227 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
232 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
233 pcpu_num_allocated *= 2;
238 /* Insert a new subblock */
239 memmove(&pcpu_size[i+1], &pcpu_size[i],
240 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
243 pcpu_size[i+1] -= size;
248 static inline unsigned int block_size(int val)
255 /* Created by linker magic */
256 extern char __per_cpu_start[], __per_cpu_end[];
258 static void *percpu_modalloc(unsigned long size, unsigned long align)
264 BUG_ON(align > SMP_CACHE_BYTES);
266 ptr = __per_cpu_start;
267 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
268 /* Extra for alignment requirement. */
269 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
270 BUG_ON(i == 0 && extra != 0);
272 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
275 /* Transfer extra to previous block. */
276 if (pcpu_size[i-1] < 0)
277 pcpu_size[i-1] -= extra;
279 pcpu_size[i-1] += extra;
280 pcpu_size[i] -= extra;
283 /* Split block if warranted */
284 if (pcpu_size[i] - size > sizeof(unsigned long))
285 if (!split_block(i, size))
289 pcpu_size[i] = -pcpu_size[i];
293 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
298 static void percpu_modfree(void *freeme)
301 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
303 /* First entry is core kernel percpu data. */
304 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
306 pcpu_size[i] = -pcpu_size[i];
313 /* Merge with previous? */
314 if (pcpu_size[i-1] >= 0) {
315 pcpu_size[i-1] += pcpu_size[i];
317 memmove(&pcpu_size[i], &pcpu_size[i+1],
318 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
321 /* Merge with next? */
322 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
323 pcpu_size[i] += pcpu_size[i+1];
325 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
326 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
330 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
332 const char *secstrings)
334 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
337 static int percpu_modinit(void)
340 pcpu_num_allocated = 2;
341 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
343 /* Static in-kernel percpu data (used). */
344 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
346 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
347 if (pcpu_size[1] < 0) {
348 printk(KERN_ERR "No per-cpu room for modules.\n");
354 __initcall(percpu_modinit);
355 #else /* ... !CONFIG_SMP */
356 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
360 static inline void percpu_modfree(void *pcpuptr)
364 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
366 const char *secstrings)
370 static inline void percpu_modcopy(void *pcpudst, const void *src,
373 /* pcpusec should be 0, and size of that section should be 0. */
376 #endif /* CONFIG_SMP */
378 static int add_attribute(struct module *mod, struct kernel_param *kp)
380 struct module_attribute *a;
383 a = &mod->mkobj->attr[mod->mkobj->num_attributes];
384 a->attr.name = (char *)kp->name;
386 a->attr.mode = kp->perm;
388 retval = sysfs_create_file(&mod->mkobj->kobj, &a->attr);
390 mod->mkobj->num_attributes++;
394 #ifdef CONFIG_MODULE_UNLOAD
395 /* Init the unload section of the module. */
396 static void module_unload_init(struct module *mod)
400 INIT_LIST_HEAD(&mod->modules_which_use_me);
401 for (i = 0; i < NR_CPUS; i++)
402 local_set(&mod->ref[i].count, 0);
403 /* Hold reference count during initialization. */
404 local_set(&mod->ref[smp_processor_id()].count, 1);
405 /* Backwards compatibility macros put refcount during init. */
406 mod->waiter = current;
409 /* modules using other modules */
412 struct list_head list;
413 struct module *module_which_uses;
416 /* Does a already use b? */
417 static int already_uses(struct module *a, struct module *b)
419 struct module_use *use;
421 list_for_each_entry(use, &b->modules_which_use_me, list) {
422 if (use->module_which_uses == a) {
423 DEBUGP("%s uses %s!\n", a->name, b->name);
427 DEBUGP("%s does not use %s!\n", a->name, b->name);
431 /* Module a uses b */
432 static int use_module(struct module *a, struct module *b)
434 struct module_use *use;
435 if (b == NULL || already_uses(a, b)) return 1;
437 if (!strong_try_module_get(b))
440 DEBUGP("Allocating new usage for %s.\n", a->name);
441 use = kmalloc(sizeof(*use), GFP_ATOMIC);
443 printk("%s: out of memory loading\n", a->name);
448 use->module_which_uses = a;
449 list_add(&use->list, &b->modules_which_use_me);
453 /* Clear the unload stuff of the module. */
454 static void module_unload_free(struct module *mod)
458 list_for_each_entry(i, &modules, list) {
459 struct module_use *use;
461 list_for_each_entry(use, &i->modules_which_use_me, list) {
462 if (use->module_which_uses == mod) {
463 DEBUGP("%s unusing %s\n", mod->name, i->name);
465 list_del(&use->list);
467 /* There can be at most one match. */
474 #ifdef CONFIG_MODULE_FORCE_UNLOAD
475 static inline int try_force(unsigned int flags)
477 int ret = (flags & O_TRUNC);
479 tainted |= TAINT_FORCED_MODULE;
483 static inline int try_force(unsigned int flags)
487 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
496 /* Whole machine is stopped with interrupts off when this runs. */
497 static inline int __try_stop_module(void *_sref)
499 struct stopref *sref = _sref;
501 /* If it's not unused, quit unless we are told to block. */
502 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
503 if (!(*sref->forced = try_force(sref->flags)))
507 /* Mark it as dying. */
508 sref->mod->state = MODULE_STATE_GOING;
512 static int try_stop_module(struct module *mod, int flags, int *forced)
514 struct stopref sref = { mod, flags, forced };
516 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
519 unsigned int module_refcount(struct module *mod)
521 unsigned int i, total = 0;
523 for (i = 0; i < NR_CPUS; i++)
524 total += local_read(&mod->ref[i].count);
527 EXPORT_SYMBOL(module_refcount);
529 /* This exists whether we can unload or not */
530 static void free_module(struct module *mod);
532 /* Stub function for modules which don't have an exitfn */
533 void cleanup_module(void)
536 EXPORT_SYMBOL(cleanup_module);
538 static void wait_for_zero_refcount(struct module *mod)
540 /* Since we might sleep for some time, drop the semaphore first */
543 DEBUGP("Looking at refcount...\n");
544 set_current_state(TASK_UNINTERRUPTIBLE);
545 if (module_refcount(mod) == 0)
549 current->state = TASK_RUNNING;
554 sys_delete_module(const char __user *name_user, unsigned int flags)
557 char name[MODULE_NAME_LEN];
560 if (!capable(CAP_SYS_MODULE))
563 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
565 name[MODULE_NAME_LEN-1] = '\0';
567 if (down_interruptible(&module_mutex) != 0)
570 mod = find_module(name);
576 if (!list_empty(&mod->modules_which_use_me)) {
577 /* Other modules depend on us: get rid of them first. */
582 /* Doing init or already dying? */
583 if (mod->state != MODULE_STATE_LIVE) {
584 /* FIXME: if (force), slam module count and wake up
586 DEBUGP("%s already dying\n", mod->name);
591 /* If it has an init func, it must have an exit func to unload */
592 if ((mod->init != init_module && mod->exit == cleanup_module)
594 forced = try_force(flags);
596 /* This module can't be removed */
602 /* Set this up before setting mod->state */
603 mod->waiter = current;
605 /* Stop the machine so refcounts can't move and disable module. */
606 ret = try_stop_module(mod, flags, &forced);
608 /* Never wait if forced. */
609 if (!forced && module_refcount(mod) != 0)
610 wait_for_zero_refcount(mod);
612 /* Final destruction now noone is using it. */
623 static void print_unload_info(struct seq_file *m, struct module *mod)
625 struct module_use *use;
626 int printed_something = 0;
628 seq_printf(m, " %u ", module_refcount(mod));
630 /* Always include a trailing , so userspace can differentiate
631 between this and the old multi-field proc format. */
632 list_for_each_entry(use, &mod->modules_which_use_me, list) {
633 printed_something = 1;
634 seq_printf(m, "%s,", use->module_which_uses->name);
638 printed_something = 1;
639 seq_printf(m, "[unsafe],");
642 if (mod->init != init_module && mod->exit == cleanup_module) {
643 printed_something = 1;
644 seq_printf(m, "[permanent],");
647 if (!printed_something)
651 void __symbol_put(const char *symbol)
653 struct module *owner;
655 const unsigned long *crc;
657 spin_lock_irqsave(&modlist_lock, flags);
658 if (!__find_symbol(symbol, &owner, &crc, 1))
661 spin_unlock_irqrestore(&modlist_lock, flags);
663 EXPORT_SYMBOL(__symbol_put);
665 void symbol_put_addr(void *addr)
669 spin_lock_irqsave(&modlist_lock, flags);
670 if (!kernel_text_address((unsigned long)addr))
673 module_put(module_text_address((unsigned long)addr));
674 spin_unlock_irqrestore(&modlist_lock, flags);
676 EXPORT_SYMBOL_GPL(symbol_put_addr);
678 static int refcnt_get_fn(char *buffer, struct kernel_param *kp)
680 struct module *mod = container_of(kp, struct module, refcnt_param);
682 /* sysfs holds one reference. */
683 return sprintf(buffer, "%u", module_refcount(mod)-1);
686 static inline int sysfs_unload_setup(struct module *mod)
688 mod->refcnt_param.name = "refcnt";
689 mod->refcnt_param.perm = 0444;
690 mod->refcnt_param.get = refcnt_get_fn;
692 return add_attribute(mod, &mod->refcnt_param);
695 #else /* !CONFIG_MODULE_UNLOAD */
696 static void print_unload_info(struct seq_file *m, struct module *mod)
698 /* We don't know the usage count, or what modules are using. */
699 seq_printf(m, " - -");
702 static inline void module_unload_free(struct module *mod)
706 static inline int use_module(struct module *a, struct module *b)
708 return strong_try_module_get(b);
711 static inline void module_unload_init(struct module *mod)
716 sys_delete_module(const char __user *name_user, unsigned int flags)
721 static inline int sysfs_unload_setup(struct module *mod)
725 #endif /* CONFIG_MODULE_UNLOAD */
727 #ifdef CONFIG_OBSOLETE_MODPARM
728 static int param_set_byte(const char *val, struct kernel_param *kp)
733 if (!val) return -EINVAL;
734 l = simple_strtol(val, &endp, 0);
735 if (endp == val || *endp || ((char)l != l))
737 *((char *)kp->arg) = l;
741 /* Bounds checking done below */
742 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
744 strcpy(kp->arg, val);
748 int set_obsolete(const char *val, struct kernel_param *kp)
750 unsigned int min, max;
751 unsigned int size, maxsize;
755 struct obsolete_modparm *obsparm = kp->arg;
758 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
762 /* type is: [min[-max]]{b,h,i,l,s} */
764 min = simple_strtol(p, &endp, 10);
765 if (endp == obsparm->type)
767 else if (*endp == '-') {
769 max = simple_strtol(p, &endp, 10);
774 return param_array(kp->name, val, min, max, obsparm->addr,
775 1, param_set_byte, &dummy);
777 return param_array(kp->name, val, min, max, obsparm->addr,
778 sizeof(short), param_set_short, &dummy);
780 return param_array(kp->name, val, min, max, obsparm->addr,
781 sizeof(int), param_set_int, &dummy);
783 return param_array(kp->name, val, min, max, obsparm->addr,
784 sizeof(long), param_set_long, &dummy);
786 return param_array(kp->name, val, min, max, obsparm->addr,
787 sizeof(char *), param_set_charp, &dummy);
790 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
791 and the decl is "char xxx[5][50];" */
793 maxsize = simple_strtol(p, &endp, 10);
794 /* We check lengths here (yes, this is a hack). */
796 while (p[size = strcspn(p, ",")]) {
803 return param_array(kp->name, val, min, max, obsparm->addr,
804 maxsize, obsparm_copy_string, &dummy);
806 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
810 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
814 static int obsolete_params(const char *name,
816 struct obsolete_modparm obsparm[],
819 unsigned int symindex,
822 struct kernel_param *kp;
826 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
830 for (i = 0; i < num; i++) {
831 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
833 snprintf(sym_name, sizeof(sym_name), "%s%s",
834 MODULE_SYMBOL_PREFIX, obsparm[i].name);
836 kp[i].name = obsparm[i].name;
838 kp[i].set = set_obsolete;
841 = (void *)find_local_symbol(sechdrs, symindex, strtab,
843 if (!obsparm[i].addr) {
844 printk("%s: falsely claims to have parameter %s\n",
845 name, obsparm[i].name);
849 kp[i].arg = &obsparm[i];
852 ret = parse_args(name, args, kp, num, NULL);
858 static int obsolete_params(const char *name,
860 struct obsolete_modparm obsparm[],
863 unsigned int symindex,
867 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
871 #endif /* CONFIG_OBSOLETE_MODPARM */
873 static const char vermagic[] = VERMAGIC_STRING;
875 #ifdef CONFIG_MODVERSIONS
876 static int check_version(Elf_Shdr *sechdrs,
877 unsigned int versindex,
880 const unsigned long *crc)
882 unsigned int i, num_versions;
883 struct modversion_info *versions;
885 /* Exporting module didn't supply crcs? OK, we're already tainted. */
889 versions = (void *) sechdrs[versindex].sh_addr;
890 num_versions = sechdrs[versindex].sh_size
891 / sizeof(struct modversion_info);
893 for (i = 0; i < num_versions; i++) {
894 if (strcmp(versions[i].name, symname) != 0)
897 if (versions[i].crc == *crc)
899 printk("%s: disagrees about version of symbol %s\n",
901 DEBUGP("Found checksum %lX vs module %lX\n",
902 *crc, versions[i].crc);
905 /* Not in module's version table. OK, but that taints the kernel. */
906 if (!(tainted & TAINT_FORCED_MODULE)) {
907 printk("%s: no version for \"%s\" found: kernel tainted.\n",
909 tainted |= TAINT_FORCED_MODULE;
914 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
915 unsigned int versindex,
918 const unsigned long *crc;
919 struct module *owner;
921 if (!__find_symbol("struct_module", &owner, &crc, 1))
923 return check_version(sechdrs, versindex, "struct_module", mod,
927 /* First part is kernel version, which we ignore. */
928 static inline int same_magic(const char *amagic, const char *bmagic)
930 amagic += strcspn(amagic, " ");
931 bmagic += strcspn(bmagic, " ");
932 return strcmp(amagic, bmagic) == 0;
935 static inline int check_version(Elf_Shdr *sechdrs,
936 unsigned int versindex,
939 const unsigned long *crc)
944 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
945 unsigned int versindex,
951 static inline int same_magic(const char *amagic, const char *bmagic)
953 return strcmp(amagic, bmagic) == 0;
955 #endif /* CONFIG_MODVERSIONS */
957 /* Resolve a symbol for this module. I.e. if we find one, record usage.
958 Must be holding module_mutex. */
959 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
960 unsigned int versindex,
964 struct module *owner;
966 const unsigned long *crc;
968 spin_lock_irq(&modlist_lock);
969 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
971 /* use_module can fail due to OOM, or module unloading */
972 if (!check_version(sechdrs, versindex, name, mod, crc) ||
973 !use_module(mod, owner))
976 spin_unlock_irq(&modlist_lock);
982 * /sys/module/foo/sections stuff
983 * J. Corbet <corbet@lwn.net>
985 #ifdef CONFIG_KALLSYMS
986 static void module_sect_attrs_release(struct kobject *kobj)
988 kfree(container_of(kobj, struct module_sections, kobj));
991 static ssize_t module_sect_show(struct kobject *kobj, struct attribute *attr,
994 struct module_sect_attr *sattr =
995 container_of(attr, struct module_sect_attr, attr);
996 return sprintf(buf, "0x%lx\n", sattr->address);
999 static struct sysfs_ops module_sect_ops = {
1000 .show = module_sect_show,
1003 static struct kobj_type module_sect_ktype = {
1004 .sysfs_ops = &module_sect_ops,
1005 .release = module_sect_attrs_release,
1008 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1009 char *secstrings, Elf_Shdr *sechdrs)
1011 unsigned int nloaded = 0, i;
1012 struct module_sect_attr *sattr;
1017 /* Count loaded sections and allocate structures */
1018 for (i = 0; i < nsect; i++)
1019 if (sechdrs[i].sh_flags & SHF_ALLOC)
1021 mod->sect_attrs = kmalloc(sizeof(struct module_sections) +
1022 nloaded*sizeof(mod->sect_attrs->attrs[0]), GFP_KERNEL);
1023 if (! mod->sect_attrs)
1026 /* sections entry setup */
1027 memset(mod->sect_attrs, 0, sizeof(struct module_sections));
1028 if (kobject_set_name(&mod->sect_attrs->kobj, "sections"))
1030 mod->sect_attrs->kobj.parent = &mod->mkobj->kobj;
1031 mod->sect_attrs->kobj.ktype = &module_sect_ktype;
1032 if (kobject_register(&mod->sect_attrs->kobj))
1035 /* And the section attributes. */
1036 sattr = &mod->sect_attrs->attrs[0];
1037 for (i = 0; i < nsect; i++) {
1038 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1040 sattr->address = sechdrs[i].sh_addr;
1041 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1042 MODULE_SECT_NAME_LEN);
1043 sattr->attr.name = sattr->name;
1044 sattr->attr.owner = mod;
1045 sattr->attr.mode = S_IRUGO;
1046 (void) sysfs_create_file(&mod->sect_attrs->kobj, &sattr->attr);
1051 kfree(mod->sect_attrs);
1052 mod->sect_attrs = NULL;
1055 static void remove_sect_attrs(struct module *mod)
1057 if (mod->sect_attrs) {
1058 kobject_unregister(&mod->sect_attrs->kobj);
1059 mod->sect_attrs = NULL;
1065 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1066 char *sectstrings, Elf_Shdr *sechdrs)
1070 static inline void remove_sect_attrs(struct module *mod)
1073 #endif /* CONFIG_KALLSYMS */
1078 #define to_module_attr(n) container_of(n, struct module_attribute, attr);
1080 static ssize_t module_attr_show(struct kobject *kobj,
1081 struct attribute *attr,
1085 struct module_attribute *attribute = to_module_attr(attr);
1087 if (!attribute->param->get)
1090 count = attribute->param->get(buf, attribute->param);
1098 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
1099 static ssize_t module_attr_store(struct kobject *kobj,
1100 struct attribute *attr,
1101 const char *buf, size_t len)
1104 struct module_attribute *attribute = to_module_attr(attr);
1106 if (!attribute->param->set)
1109 err = attribute->param->set(buf, attribute->param);
1115 static struct sysfs_ops module_sysfs_ops = {
1116 .show = module_attr_show,
1117 .store = module_attr_store,
1120 static void module_kobj_release(struct kobject *kobj)
1122 kfree(container_of(kobj, struct module_kobject, kobj));
1125 static struct kobj_type module_ktype = {
1126 .sysfs_ops = &module_sysfs_ops,
1127 .release = &module_kobj_release,
1129 static decl_subsys(module, &module_ktype, NULL);
1131 static int mod_sysfs_setup(struct module *mod,
1132 struct kernel_param *kparam,
1133 unsigned int num_params)
1138 /* We overallocate: not every param is in sysfs, and maybe no refcnt */
1139 mod->mkobj = kmalloc(sizeof(*mod->mkobj)
1140 + sizeof(mod->mkobj->attr[0]) * (num_params+1),
1145 memset(&mod->mkobj->kobj, 0, sizeof(mod->mkobj->kobj));
1146 err = kobject_set_name(&mod->mkobj->kobj, mod->name);
1149 kobj_set_kset_s(mod->mkobj, module_subsys);
1150 err = kobject_register(&mod->mkobj->kobj);
1154 mod->mkobj->num_attributes = 0;
1156 for (i = 0; i < num_params; i++) {
1157 if (kparam[i].perm) {
1158 err = add_attribute(mod, &kparam[i]);
1163 err = sysfs_unload_setup(mod);
1169 for (i = 0; i < mod->mkobj->num_attributes; i++)
1170 sysfs_remove_file(&mod->mkobj->kobj,&mod->mkobj->attr[i].attr);
1171 /* Calls module_kobj_release */
1172 kobject_unregister(&mod->mkobj->kobj);
1179 static void mod_kobject_remove(struct module *mod)
1182 for (i = 0; i < mod->mkobj->num_attributes; i++)
1183 sysfs_remove_file(&mod->mkobj->kobj,&mod->mkobj->attr[i].attr);
1184 /* Calls module_kobj_release */
1185 kobject_unregister(&mod->mkobj->kobj);
1188 /* Free a module, remove from lists, etc (must hold module mutex). */
1189 static void free_module(struct module *mod)
1191 /* Delete from various lists */
1192 spin_lock_irq(&modlist_lock);
1193 list_del(&mod->list);
1194 spin_unlock_irq(&modlist_lock);
1196 remove_sect_attrs(mod);
1197 mod_kobject_remove(mod);
1199 /* Arch-specific cleanup. */
1200 module_arch_cleanup(mod);
1202 /* Module unload stuff */
1203 module_unload_free(mod);
1205 /* This may be NULL, but that's OK */
1206 module_free(mod, mod->module_init);
1209 percpu_modfree(mod->percpu);
1211 /* Finally, free the core (containing the module structure) */
1212 module_free(mod, mod->module_core);
1215 void *__symbol_get(const char *symbol)
1217 struct module *owner;
1218 unsigned long value, flags;
1219 const unsigned long *crc;
1221 spin_lock_irqsave(&modlist_lock, flags);
1222 value = __find_symbol(symbol, &owner, &crc, 1);
1223 if (value && !strong_try_module_get(owner))
1225 spin_unlock_irqrestore(&modlist_lock, flags);
1227 return (void *)value;
1229 EXPORT_SYMBOL_GPL(__symbol_get);
1231 /* Change all symbols so that sh_value encodes the pointer directly. */
1232 static int simplify_symbols(Elf_Shdr *sechdrs,
1233 unsigned int symindex,
1235 unsigned int versindex,
1236 unsigned int pcpuindex,
1239 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1240 unsigned long secbase;
1241 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1244 for (i = 1; i < n; i++) {
1245 switch (sym[i].st_shndx) {
1247 /* We compiled with -fno-common. These are not
1248 supposed to happen. */
1249 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1250 printk("%s: please compile with -fno-common\n",
1256 /* Don't need to do anything */
1257 DEBUGP("Absolute symbol: 0x%08lx\n",
1258 (long)sym[i].st_value);
1263 = resolve_symbol(sechdrs, versindex,
1264 strtab + sym[i].st_name, mod);
1266 /* Ok if resolved. */
1267 if (sym[i].st_value != 0)
1270 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1273 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1274 mod->name, strtab + sym[i].st_name);
1279 /* Divert to percpu allocation if a percpu var. */
1280 if (sym[i].st_shndx == pcpuindex)
1281 secbase = (unsigned long)mod->percpu;
1283 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1284 sym[i].st_value += secbase;
1292 /* Update size with this section: return offset. */
1293 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1297 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1298 *size = ret + sechdr->sh_size;
1302 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1303 might -- code, read-only data, read-write data, small data. Tally
1304 sizes, and place the offsets into sh_entsize fields: high bit means it
1306 static void layout_sections(struct module *mod,
1307 const Elf_Ehdr *hdr,
1309 const char *secstrings)
1311 static unsigned long const masks[][2] = {
1312 /* NOTE: all executable code must be the first section
1313 * in this array; otherwise modify the text_size
1314 * finder in the two loops below */
1315 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1316 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1317 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1318 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1322 for (i = 0; i < hdr->e_shnum; i++)
1323 sechdrs[i].sh_entsize = ~0UL;
1325 DEBUGP("Core section allocation order:\n");
1326 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1327 for (i = 0; i < hdr->e_shnum; ++i) {
1328 Elf_Shdr *s = &sechdrs[i];
1330 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1331 || (s->sh_flags & masks[m][1])
1332 || s->sh_entsize != ~0UL
1333 || strncmp(secstrings + s->sh_name,
1336 s->sh_entsize = get_offset(&mod->core_size, s);
1337 DEBUGP("\t%s\n", secstrings + s->sh_name);
1340 mod->core_text_size = mod->core_size;
1343 DEBUGP("Init section allocation order:\n");
1344 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1345 for (i = 0; i < hdr->e_shnum; ++i) {
1346 Elf_Shdr *s = &sechdrs[i];
1348 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1349 || (s->sh_flags & masks[m][1])
1350 || s->sh_entsize != ~0UL
1351 || strncmp(secstrings + s->sh_name,
1354 s->sh_entsize = (get_offset(&mod->init_size, s)
1355 | INIT_OFFSET_MASK);
1356 DEBUGP("\t%s\n", secstrings + s->sh_name);
1359 mod->init_text_size = mod->init_size;
1363 static inline int license_is_gpl_compatible(const char *license)
1365 return (strcmp(license, "GPL") == 0
1366 || strcmp(license, "GPL v2") == 0
1367 || strcmp(license, "GPL and additional rights") == 0
1368 || strcmp(license, "Dual BSD/GPL") == 0
1369 || strcmp(license, "Dual MPL/GPL") == 0);
1372 static void set_license(struct module *mod, const char *license)
1375 license = "unspecified";
1377 mod->license_gplok = license_is_gpl_compatible(license);
1378 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1379 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1380 mod->name, license);
1381 tainted |= TAINT_PROPRIETARY_MODULE;
1385 /* Parse tag=value strings from .modinfo section */
1386 static char *next_string(char *string, unsigned long *secsize)
1388 /* Skip non-zero chars */
1391 if ((*secsize)-- <= 1)
1395 /* Skip any zero padding. */
1396 while (!string[0]) {
1398 if ((*secsize)-- <= 1)
1404 static char *get_modinfo(Elf_Shdr *sechdrs,
1409 unsigned int taglen = strlen(tag);
1410 unsigned long size = sechdrs[info].sh_size;
1412 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1413 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1414 return p + taglen + 1;
1419 #ifdef CONFIG_KALLSYMS
1420 int is_exported(const char *name, const struct module *mod)
1425 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1426 if (strcmp(__start___ksymtab[i].name, name) == 0)
1430 for (i = 0; i < mod->num_syms; i++)
1431 if (strcmp(mod->syms[i].name, name) == 0)
1437 static char elf_type(const Elf_Sym *sym,
1439 const char *secstrings,
1442 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1443 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1448 if (sym->st_shndx == SHN_UNDEF)
1450 if (sym->st_shndx == SHN_ABS)
1452 if (sym->st_shndx >= SHN_LORESERVE)
1454 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1456 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1457 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1458 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1460 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1465 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1466 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1471 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1472 ".debug", strlen(".debug")) == 0)
1477 static void add_kallsyms(struct module *mod,
1479 unsigned int symindex,
1480 unsigned int strindex,
1481 const char *secstrings)
1485 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1486 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1487 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1489 /* Set types up while we still have access to sections. */
1490 for (i = 0; i < mod->num_symtab; i++)
1491 mod->symtab[i].st_info
1492 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1495 static inline void add_kallsyms(struct module *mod,
1497 unsigned int symindex,
1498 unsigned int strindex,
1499 const char *secstrings)
1502 #endif /* CONFIG_KALLSYMS */
1504 /* Allocate and load the module: note that size of section 0 is always
1505 zero, and we rely on this for optional sections. */
1506 static struct module *load_module(void __user *umod,
1508 const char __user *uargs)
1512 char *secstrings, *args, *modmagic, *strtab = NULL;
1513 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1514 exportindex, modindex, obsparmindex, infoindex, gplindex,
1515 crcindex, gplcrcindex, versindex, pcpuindex;
1519 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1520 struct exception_table_entry *extable;
1522 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1524 if (len < sizeof(*hdr))
1525 return ERR_PTR(-ENOEXEC);
1527 /* Suck in entire file: we'll want most of it. */
1528 /* vmalloc barfs on "unusual" numbers. Check here */
1529 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1530 return ERR_PTR(-ENOMEM);
1531 if (copy_from_user(hdr, umod, len) != 0) {
1536 /* Sanity checks against insmoding binaries or wrong arch,
1537 weird elf version */
1538 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1539 || hdr->e_type != ET_REL
1540 || !elf_check_arch(hdr)
1541 || hdr->e_shentsize != sizeof(*sechdrs)) {
1546 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1549 /* Convenience variables */
1550 sechdrs = (void *)hdr + hdr->e_shoff;
1551 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1552 sechdrs[0].sh_addr = 0;
1554 /* And these should exist, but gcc whinges if we don't init them */
1555 symindex = strindex = 0;
1557 for (i = 1; i < hdr->e_shnum; i++) {
1558 if (sechdrs[i].sh_type != SHT_NOBITS
1559 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1562 /* Mark all sections sh_addr with their address in the
1564 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1566 /* Internal symbols and strings. */
1567 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1569 strindex = sechdrs[i].sh_link;
1570 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1572 #ifndef CONFIG_MODULE_UNLOAD
1573 /* Don't load .exit sections */
1574 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1575 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1579 modindex = find_sec(hdr, sechdrs, secstrings,
1580 ".gnu.linkonce.this_module");
1582 printk(KERN_WARNING "No module found in object\n");
1586 mod = (void *)sechdrs[modindex].sh_addr;
1588 /* Optional sections */
1589 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1590 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1591 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1592 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1593 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1594 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1595 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1596 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1597 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1598 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1600 /* Don't keep modinfo section */
1601 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1602 #ifdef CONFIG_KALLSYMS
1603 /* Keep symbol and string tables for decoding later. */
1604 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1605 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1608 /* Check module struct version now, before we try to use module. */
1609 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1614 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1615 /* This is allowed: modprobe --force will invalidate it. */
1617 tainted |= TAINT_FORCED_MODULE;
1618 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1620 } else if (!same_magic(modmagic, vermagic)) {
1621 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1622 mod->name, modmagic, vermagic);
1627 /* Now copy in args */
1628 arglen = strlen_user(uargs);
1633 args = kmalloc(arglen, GFP_KERNEL);
1638 if (copy_from_user(args, uargs, arglen) != 0) {
1643 if (find_module(mod->name)) {
1648 mod->state = MODULE_STATE_COMING;
1650 /* Allow arches to frob section contents and sizes. */
1651 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1656 /* We have a special allocation for this section. */
1657 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1658 sechdrs[pcpuindex].sh_addralign);
1663 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1664 mod->percpu = percpu;
1667 /* Determine total sizes, and put offsets in sh_entsize. For now
1668 this is done generically; there doesn't appear to be any
1669 special cases for the architectures. */
1670 layout_sections(mod, hdr, sechdrs, secstrings);
1672 /* Do the allocs. */
1673 ptr = module_alloc(mod->core_size);
1678 memset(ptr, 0, mod->core_size);
1679 mod->module_core = ptr;
1681 ptr = module_alloc(mod->init_size);
1682 if (!ptr && mod->init_size) {
1686 memset(ptr, 0, mod->init_size);
1687 mod->module_init = ptr;
1689 /* Transfer each section which specifies SHF_ALLOC */
1690 DEBUGP("final section addresses:\n");
1691 for (i = 0; i < hdr->e_shnum; i++) {
1694 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1697 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1698 dest = mod->module_init
1699 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1701 dest = mod->module_core + sechdrs[i].sh_entsize;
1703 if (sechdrs[i].sh_type != SHT_NOBITS)
1704 memcpy(dest, (void *)sechdrs[i].sh_addr,
1705 sechdrs[i].sh_size);
1706 /* Update sh_addr to point to copy in image. */
1707 sechdrs[i].sh_addr = (unsigned long)dest;
1708 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1710 /* Module has been moved. */
1711 mod = (void *)sechdrs[modindex].sh_addr;
1713 /* Now we've moved module, initialize linked lists, etc. */
1714 module_unload_init(mod);
1716 /* Set up license info based on the info section */
1717 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1719 /* Fix up syms, so that st_value is a pointer to location. */
1720 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1725 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1726 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1727 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1729 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1730 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1731 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1733 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1735 #ifdef CONFIG_MODVERSIONS
1736 if ((mod->num_syms && !crcindex) ||
1737 (mod->num_gpl_syms && !gplcrcindex)) {
1738 printk(KERN_WARNING "%s: No versions for exported symbols."
1739 " Tainting kernel.\n", mod->name);
1740 tainted |= TAINT_FORCED_MODULE;
1744 /* Now do relocations. */
1745 for (i = 1; i < hdr->e_shnum; i++) {
1746 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1747 unsigned int info = sechdrs[i].sh_info;
1749 /* Not a valid relocation section? */
1750 if (info >= hdr->e_shnum)
1753 /* Don't bother with non-allocated sections */
1754 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1757 if (sechdrs[i].sh_type == SHT_REL)
1758 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1759 else if (sechdrs[i].sh_type == SHT_RELA)
1760 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1766 /* Set up and sort exception table */
1767 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1768 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1769 sort_extable(extable, extable + mod->num_exentries);
1771 /* Finally, copy percpu area over. */
1772 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1773 sechdrs[pcpuindex].sh_size);
1775 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1777 err = module_finalize(hdr, sechdrs, mod);
1783 err = obsolete_params(mod->name, mod->args,
1784 (struct obsolete_modparm *)
1785 sechdrs[obsparmindex].sh_addr,
1786 sechdrs[obsparmindex].sh_size
1787 / sizeof(struct obsolete_modparm),
1789 (char *)sechdrs[strindex].sh_addr);
1791 printk(KERN_WARNING "%s: Ignoring new-style "
1792 "parameters in presence of obsolete ones\n",
1795 /* Size of section 0 is 0, so this works well if no params */
1796 err = parse_args(mod->name, mod->args,
1797 (struct kernel_param *)
1798 sechdrs[setupindex].sh_addr,
1799 sechdrs[setupindex].sh_size
1800 / sizeof(struct kernel_param),
1803 err = mod_sysfs_setup(mod,
1804 (struct kernel_param *)
1805 sechdrs[setupindex].sh_addr,
1806 sechdrs[setupindex].sh_size
1807 / sizeof(struct kernel_param));
1810 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1812 /* Get rid of temporary copy */
1819 module_arch_cleanup(mod);
1821 module_unload_free(mod);
1822 module_free(mod, mod->module_init);
1824 module_free(mod, mod->module_core);
1827 percpu_modfree(percpu);
1832 if (err < 0) return ERR_PTR(err);
1836 printk(KERN_ERR "Module len %lu truncated\n", len);
1841 /* This is where the real work happens */
1843 sys_init_module(void __user *umod,
1845 const char __user *uargs)
1850 /* Must have permission */
1851 if (!capable(CAP_SYS_MODULE))
1854 /* Only one module load at a time, please */
1855 if (down_interruptible(&module_mutex) != 0)
1858 /* Do all the hard work */
1859 mod = load_module(umod, len, uargs);
1862 return PTR_ERR(mod);
1865 /* Flush the instruction cache, since we've played with text */
1866 if (mod->module_init)
1867 flush_icache_range((unsigned long)mod->module_init,
1868 (unsigned long)mod->module_init
1870 flush_icache_range((unsigned long)mod->module_core,
1871 (unsigned long)mod->module_core + mod->core_size);
1873 /* Now sew it into the lists. They won't access us, since
1874 strong_try_module_get() will fail. */
1875 spin_lock_irq(&modlist_lock);
1876 list_add(&mod->list, &modules);
1877 spin_unlock_irq(&modlist_lock);
1879 /* Drop lock so they can recurse */
1882 down(¬ify_mutex);
1883 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1886 /* Start the module */
1889 /* Init routine failed: abort. Try to protect us from
1890 buggy refcounters. */
1891 mod->state = MODULE_STATE_GOING;
1892 synchronize_kernel();
1894 printk(KERN_ERR "%s: module is now stuck!\n",
1898 down(&module_mutex);
1905 /* Now it's a first class citizen! */
1906 down(&module_mutex);
1907 mod->state = MODULE_STATE_LIVE;
1908 /* Drop initial reference. */
1910 module_free(mod, mod->module_init);
1911 mod->module_init = NULL;
1913 mod->init_text_size = 0;
1919 static inline int within(unsigned long addr, void *start, unsigned long size)
1921 return ((void *)addr >= start && (void *)addr < start + size);
1924 #ifdef CONFIG_KALLSYMS
1925 static const char *get_ksymbol(struct module *mod,
1927 unsigned long *size,
1928 unsigned long *offset)
1930 unsigned int i, best = 0;
1931 unsigned long nextval;
1933 /* At worse, next value is at end of module */
1934 if (within(addr, mod->module_init, mod->init_size))
1935 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1937 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1939 /* Scan for closest preceeding symbol, and next symbol. (ELF
1940 starts real symbols at 1). */
1941 for (i = 1; i < mod->num_symtab; i++) {
1942 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1945 /* We ignore unnamed symbols: they're uninformative
1946 * and inserted at a whim. */
1947 if (mod->symtab[i].st_value <= addr
1948 && mod->symtab[i].st_value > mod->symtab[best].st_value
1949 && *(mod->strtab + mod->symtab[i].st_name) != '\0' )
1951 if (mod->symtab[i].st_value > addr
1952 && mod->symtab[i].st_value < nextval
1953 && *(mod->strtab + mod->symtab[i].st_name) != '\0')
1954 nextval = mod->symtab[i].st_value;
1960 *size = nextval - mod->symtab[best].st_value;
1961 *offset = addr - mod->symtab[best].st_value;
1962 return mod->strtab + mod->symtab[best].st_name;
1965 /* For kallsyms to ask for address resolution. NULL means not found.
1966 We don't lock, as this is used for oops resolution and races are a
1968 const char *module_address_lookup(unsigned long addr,
1969 unsigned long *size,
1970 unsigned long *offset,
1975 list_for_each_entry(mod, &modules, list) {
1976 if (within(addr, mod->module_init, mod->init_size)
1977 || within(addr, mod->module_core, mod->core_size)) {
1978 *modname = mod->name;
1979 return get_ksymbol(mod, addr, size, offset);
1985 struct module *module_get_kallsym(unsigned int symnum,
1986 unsigned long *value,
1992 down(&module_mutex);
1993 list_for_each_entry(mod, &modules, list) {
1994 if (symnum < mod->num_symtab) {
1995 *value = mod->symtab[symnum].st_value;
1996 *type = mod->symtab[symnum].st_info;
1998 mod->strtab + mod->symtab[symnum].st_name,
2003 symnum -= mod->num_symtab;
2009 static unsigned long mod_find_symname(struct module *mod, const char *name)
2013 for (i = 0; i < mod->num_symtab; i++)
2014 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
2015 return mod->symtab[i].st_value;
2019 /* Look for this name: can be of form module:name. */
2020 unsigned long module_kallsyms_lookup_name(const char *name)
2024 unsigned long ret = 0;
2026 /* Don't lock: we're in enough trouble already. */
2027 if ((colon = strchr(name, ':')) != NULL) {
2029 if ((mod = find_module(name)) != NULL)
2030 ret = mod_find_symname(mod, colon+1);
2033 list_for_each_entry(mod, &modules, list)
2034 if ((ret = mod_find_symname(mod, name)) != 0)
2039 #endif /* CONFIG_KALLSYMS */
2041 /* Called by the /proc file system to return a list of modules. */
2042 static void *m_start(struct seq_file *m, loff_t *pos)
2044 struct list_head *i;
2047 down(&module_mutex);
2048 list_for_each(i, &modules) {
2057 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2059 struct list_head *i = p;
2061 if (i->next == &modules)
2066 static void m_stop(struct seq_file *m, void *p)
2071 static int m_show(struct seq_file *m, void *p)
2073 struct module *mod = list_entry(p, struct module, list);
2074 seq_printf(m, "%s %lu",
2075 mod->name, mod->init_size + mod->core_size);
2076 print_unload_info(m, mod);
2078 /* Informative for users. */
2079 seq_printf(m, " %s",
2080 mod->state == MODULE_STATE_GOING ? "Unloading":
2081 mod->state == MODULE_STATE_COMING ? "Loading":
2083 /* Used by oprofile and other similar tools. */
2084 seq_printf(m, " 0x%p", mod->module_core);
2086 seq_printf(m, "\n");
2090 /* Format: modulename size refcount deps address
2092 Where refcount is a number or -, and deps is a comma-separated list
2095 struct seq_operations modules_op = {
2102 /* Given an address, look for it in the module exception tables. */
2103 const struct exception_table_entry *search_module_extables(unsigned long addr)
2105 unsigned long flags;
2106 const struct exception_table_entry *e = NULL;
2109 spin_lock_irqsave(&modlist_lock, flags);
2110 list_for_each_entry(mod, &modules, list) {
2111 if (mod->num_exentries == 0)
2114 e = search_extable(mod->extable,
2115 mod->extable + mod->num_exentries - 1,
2120 spin_unlock_irqrestore(&modlist_lock, flags);
2122 /* Now, if we found one, we are running inside it now, hence
2123 we cannot unload the module, hence no refcnt needed. */
2127 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2128 struct module *__module_text_address(unsigned long addr)
2132 list_for_each_entry(mod, &modules, list)
2133 if (within(addr, mod->module_init, mod->init_text_size)
2134 || within(addr, mod->module_core, mod->core_text_size))
2139 struct module *module_text_address(unsigned long addr)
2142 unsigned long flags;
2144 spin_lock_irqsave(&modlist_lock, flags);
2145 mod = __module_text_address(addr);
2146 spin_unlock_irqrestore(&modlist_lock, flags);
2151 /* Don't grab lock, we're oopsing. */
2152 void print_modules(void)
2156 printk("Modules linked in:");
2157 list_for_each_entry(mod, &modules, list)
2158 printk(" %s", mod->name);
2162 #ifdef CONFIG_MODVERSIONS
2163 /* Generate the signature for struct module here, too, for modversions. */
2164 void struct_module(struct module *mod) { return; }
2165 EXPORT_SYMBOL(struct_module);
2168 static int __init modules_init(void)
2170 return subsystem_register(&module_subsys);
2172 __initcall(modules_init);