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>
40 #include "module-verify.h"
45 #define DEBUGP(fmt , a...)
48 #ifndef ARCH_SHF_SMALL
49 #define ARCH_SHF_SMALL 0
52 /* If this is set, the section belongs in the init part of the module */
53 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
55 /* Protects module list */
56 static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
58 /* List of modules, protected by module_mutex AND modlist_lock */
59 static DECLARE_MUTEX(module_mutex);
60 static LIST_HEAD(modules);
62 static DECLARE_MUTEX(notify_mutex);
63 static struct notifier_block * module_notify_list;
65 int register_module_notifier(struct notifier_block * nb)
69 err = notifier_chain_register(&module_notify_list, nb);
73 EXPORT_SYMBOL(register_module_notifier);
75 int unregister_module_notifier(struct notifier_block * nb)
79 err = notifier_chain_unregister(&module_notify_list, nb);
83 EXPORT_SYMBOL(unregister_module_notifier);
85 /* We require a truly strong try_module_get() */
86 static inline int strong_try_module_get(struct module *mod)
88 if (mod && mod->state == MODULE_STATE_COMING)
90 return try_module_get(mod);
93 /* Stub function for modules which don't have an initfn */
98 EXPORT_SYMBOL(init_module);
100 /* A thread that wants to hold a reference to a module only while it
101 * is running can call ths to safely exit.
102 * nfsd and lockd use this.
104 void __module_put_and_exit(struct module *mod, long code)
109 EXPORT_SYMBOL(__module_put_and_exit);
111 /* Find a module section: 0 means not found. */
112 static unsigned int find_sec(Elf_Ehdr *hdr,
114 const char *secstrings,
119 for (i = 1; i < hdr->e_shnum; i++)
120 /* Alloc bit cleared means "ignore it." */
121 if ((sechdrs[i].sh_flags & SHF_ALLOC)
122 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
127 /* Provided by the linker */
128 extern const struct kernel_symbol __start___ksymtab[];
129 extern const struct kernel_symbol __stop___ksymtab[];
130 extern const struct kernel_symbol __start___ksymtab_gpl[];
131 extern const struct kernel_symbol __stop___ksymtab_gpl[];
132 extern const unsigned long __start___kcrctab[];
133 extern const unsigned long __start___kcrctab_gpl[];
135 #ifndef CONFIG_MODVERSIONS
136 #define symversion(base, idx) NULL
138 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
141 /* Find a symbol, return value, crc and module which owns it */
142 static unsigned long __find_symbol(const char *name,
143 struct module **owner,
144 const unsigned long **crc,
150 /* Core kernel first. */
152 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
153 if (strcmp(__start___ksymtab[i].name, name) == 0) {
154 *crc = symversion(__start___kcrctab, i);
155 return __start___ksymtab[i].value;
159 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
160 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
161 *crc = symversion(__start___kcrctab_gpl, i);
162 return __start___ksymtab_gpl[i].value;
166 /* Now try modules. */
167 list_for_each_entry(mod, &modules, list) {
169 for (i = 0; i < mod->num_syms; i++)
170 if (strcmp(mod->syms[i].name, name) == 0) {
171 *crc = symversion(mod->crcs, i);
172 return mod->syms[i].value;
176 for (i = 0; i < mod->num_gpl_syms; i++) {
177 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
178 *crc = symversion(mod->gpl_crcs, i);
179 return mod->gpl_syms[i].value;
184 DEBUGP("Failed to find symbol %s\n", name);
188 /* Find a symbol in this elf symbol table */
189 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
190 unsigned int symindex,
195 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
197 /* Search (defined) internal symbols first. */
198 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
199 if (sym[i].st_shndx != SHN_UNDEF
200 && strcmp(name, strtab + sym[i].st_name) == 0)
201 return sym[i].st_value;
206 /* Search for module by name: must hold module_mutex. */
207 static struct module *find_module(const char *name)
211 list_for_each_entry(mod, &modules, list) {
212 if (strcmp(mod->name, name) == 0)
219 /* Number of blocks used and allocated. */
220 static unsigned int pcpu_num_used, pcpu_num_allocated;
221 /* Size of each block. -ve means used. */
222 static int *pcpu_size;
224 static int split_block(unsigned int i, unsigned short size)
226 /* Reallocation required? */
227 if (pcpu_num_used + 1 > pcpu_num_allocated) {
228 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
233 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
234 pcpu_num_allocated *= 2;
239 /* Insert a new subblock */
240 memmove(&pcpu_size[i+1], &pcpu_size[i],
241 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
244 pcpu_size[i+1] -= size;
249 static inline unsigned int block_size(int val)
256 /* Created by linker magic */
257 extern char __per_cpu_start[], __per_cpu_end[];
259 static void *percpu_modalloc(unsigned long size, unsigned long align)
265 BUG_ON(align > SMP_CACHE_BYTES);
267 ptr = __per_cpu_start;
268 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
269 /* Extra for alignment requirement. */
270 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
271 BUG_ON(i == 0 && extra != 0);
273 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
276 /* Transfer extra to previous block. */
277 if (pcpu_size[i-1] < 0)
278 pcpu_size[i-1] -= extra;
280 pcpu_size[i-1] += extra;
281 pcpu_size[i] -= extra;
284 /* Split block if warranted */
285 if (pcpu_size[i] - size > sizeof(unsigned long))
286 if (!split_block(i, size))
290 pcpu_size[i] = -pcpu_size[i];
294 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
299 static void percpu_modfree(void *freeme)
302 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
304 /* First entry is core kernel percpu data. */
305 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
307 pcpu_size[i] = -pcpu_size[i];
314 /* Merge with previous? */
315 if (pcpu_size[i-1] >= 0) {
316 pcpu_size[i-1] += pcpu_size[i];
318 memmove(&pcpu_size[i], &pcpu_size[i+1],
319 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
322 /* Merge with next? */
323 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
324 pcpu_size[i] += pcpu_size[i+1];
326 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
327 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
331 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
333 const char *secstrings)
335 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
338 static int percpu_modinit(void)
341 pcpu_num_allocated = 2;
342 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
344 /* Static in-kernel percpu data (used). */
345 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
347 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
348 if (pcpu_size[1] < 0) {
349 printk(KERN_ERR "No per-cpu room for modules.\n");
355 __initcall(percpu_modinit);
356 #else /* ... !CONFIG_SMP */
357 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
361 static inline void percpu_modfree(void *pcpuptr)
365 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
367 const char *secstrings)
371 static inline void percpu_modcopy(void *pcpudst, const void *src,
374 /* pcpusec should be 0, and size of that section should be 0. */
377 #endif /* CONFIG_SMP */
379 static int add_attribute(struct module *mod, struct kernel_param *kp)
381 struct module_attribute *a;
384 a = &mod->mkobj->attr[mod->mkobj->num_attributes];
385 a->attr.name = (char *)kp->name;
387 a->attr.mode = kp->perm;
389 retval = sysfs_create_file(&mod->mkobj->kobj, &a->attr);
391 mod->mkobj->num_attributes++;
395 #ifdef CONFIG_MODULE_UNLOAD
396 /* Init the unload section of the module. */
397 static void module_unload_init(struct module *mod)
401 INIT_LIST_HEAD(&mod->modules_which_use_me);
402 for (i = 0; i < NR_CPUS; i++)
403 local_set(&mod->ref[i].count, 0);
404 /* Hold reference count during initialization. */
405 local_set(&mod->ref[smp_processor_id()].count, 1);
406 /* Backwards compatibility macros put refcount during init. */
407 mod->waiter = current;
410 /* modules using other modules */
413 struct list_head list;
414 struct module *module_which_uses;
417 /* Does a already use b? */
418 static int already_uses(struct module *a, struct module *b)
420 struct module_use *use;
422 list_for_each_entry(use, &b->modules_which_use_me, list) {
423 if (use->module_which_uses == a) {
424 DEBUGP("%s uses %s!\n", a->name, b->name);
428 DEBUGP("%s does not use %s!\n", a->name, b->name);
432 /* Module a uses b */
433 static int use_module(struct module *a, struct module *b)
435 struct module_use *use;
436 if (b == NULL || already_uses(a, b)) return 1;
438 if (!strong_try_module_get(b))
441 DEBUGP("Allocating new usage for %s.\n", a->name);
442 use = kmalloc(sizeof(*use), GFP_ATOMIC);
444 printk("%s: out of memory loading\n", a->name);
449 use->module_which_uses = a;
450 list_add(&use->list, &b->modules_which_use_me);
454 /* Clear the unload stuff of the module. */
455 static void module_unload_free(struct module *mod)
459 list_for_each_entry(i, &modules, list) {
460 struct module_use *use;
462 list_for_each_entry(use, &i->modules_which_use_me, list) {
463 if (use->module_which_uses == mod) {
464 DEBUGP("%s unusing %s\n", mod->name, i->name);
466 list_del(&use->list);
468 /* There can be at most one match. */
475 #ifdef CONFIG_MODULE_FORCE_UNLOAD
476 static inline int try_force(unsigned int flags)
478 int ret = (flags & O_TRUNC);
480 tainted |= TAINT_FORCED_MODULE;
484 static inline int try_force(unsigned int flags)
488 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
497 /* Whole machine is stopped with interrupts off when this runs. */
498 static inline int __try_stop_module(void *_sref)
500 struct stopref *sref = _sref;
502 /* If it's not unused, quit unless we are told to block. */
503 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
504 if (!(*sref->forced = try_force(sref->flags)))
508 /* Mark it as dying. */
509 sref->mod->state = MODULE_STATE_GOING;
513 static int try_stop_module(struct module *mod, int flags, int *forced)
515 struct stopref sref = { mod, flags, forced };
517 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
520 unsigned int module_refcount(struct module *mod)
522 unsigned int i, total = 0;
524 for (i = 0; i < NR_CPUS; i++)
525 total += local_read(&mod->ref[i].count);
528 EXPORT_SYMBOL(module_refcount);
530 /* This exists whether we can unload or not */
531 static void free_module(struct module *mod);
533 /* Stub function for modules which don't have an exitfn */
534 void cleanup_module(void)
537 EXPORT_SYMBOL(cleanup_module);
539 static void wait_for_zero_refcount(struct module *mod)
541 /* Since we might sleep for some time, drop the semaphore first */
544 DEBUGP("Looking at refcount...\n");
545 set_current_state(TASK_UNINTERRUPTIBLE);
546 if (module_refcount(mod) == 0)
550 current->state = TASK_RUNNING;
555 sys_delete_module(const char __user *name_user, unsigned int flags)
558 char name[MODULE_NAME_LEN];
561 if (!capable(CAP_SYS_MODULE))
564 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
566 name[MODULE_NAME_LEN-1] = '\0';
568 if (down_interruptible(&module_mutex) != 0)
571 mod = find_module(name);
577 if (!list_empty(&mod->modules_which_use_me)) {
578 /* Other modules depend on us: get rid of them first. */
583 /* Doing init or already dying? */
584 if (mod->state != MODULE_STATE_LIVE) {
585 /* FIXME: if (force), slam module count and wake up
587 DEBUGP("%s already dying\n", mod->name);
592 /* If it has an init func, it must have an exit func to unload */
593 if ((mod->init != init_module && mod->exit == cleanup_module)
595 forced = try_force(flags);
597 /* This module can't be removed */
603 /* Set this up before setting mod->state */
604 mod->waiter = current;
606 /* Stop the machine so refcounts can't move and disable module. */
607 ret = try_stop_module(mod, flags, &forced);
609 /* Never wait if forced. */
610 if (!forced && module_refcount(mod) != 0)
611 wait_for_zero_refcount(mod);
613 /* Final destruction now noone is using it. */
624 static void print_unload_info(struct seq_file *m, struct module *mod)
626 struct module_use *use;
627 int printed_something = 0;
629 seq_printf(m, " %u ", module_refcount(mod));
631 /* Always include a trailing , so userspace can differentiate
632 between this and the old multi-field proc format. */
633 list_for_each_entry(use, &mod->modules_which_use_me, list) {
634 printed_something = 1;
635 seq_printf(m, "%s,", use->module_which_uses->name);
639 printed_something = 1;
640 seq_printf(m, "[unsafe],");
643 if (mod->init != init_module && mod->exit == cleanup_module) {
644 printed_something = 1;
645 seq_printf(m, "[permanent],");
648 if (!printed_something)
652 void __symbol_put(const char *symbol)
654 struct module *owner;
656 const unsigned long *crc;
658 spin_lock_irqsave(&modlist_lock, flags);
659 if (!__find_symbol(symbol, &owner, &crc, 1))
662 spin_unlock_irqrestore(&modlist_lock, flags);
664 EXPORT_SYMBOL(__symbol_put);
666 void symbol_put_addr(void *addr)
670 spin_lock_irqsave(&modlist_lock, flags);
671 if (!kernel_text_address((unsigned long)addr))
674 module_put(module_text_address((unsigned long)addr));
675 spin_unlock_irqrestore(&modlist_lock, flags);
677 EXPORT_SYMBOL_GPL(symbol_put_addr);
679 static int refcnt_get_fn(char *buffer, struct kernel_param *kp)
681 struct module *mod = container_of(kp, struct module, refcnt_param);
683 /* sysfs holds one reference. */
684 return sprintf(buffer, "%u", module_refcount(mod)-1);
687 static inline int sysfs_unload_setup(struct module *mod)
689 mod->refcnt_param.name = "refcnt";
690 mod->refcnt_param.perm = 0444;
691 mod->refcnt_param.get = refcnt_get_fn;
693 return add_attribute(mod, &mod->refcnt_param);
696 #else /* !CONFIG_MODULE_UNLOAD */
697 static void print_unload_info(struct seq_file *m, struct module *mod)
699 /* We don't know the usage count, or what modules are using. */
700 seq_printf(m, " - -");
703 static inline void module_unload_free(struct module *mod)
707 static inline int use_module(struct module *a, struct module *b)
709 return strong_try_module_get(b);
712 static inline void module_unload_init(struct module *mod)
717 sys_delete_module(const char __user *name_user, unsigned int flags)
722 static inline int sysfs_unload_setup(struct module *mod)
726 #endif /* CONFIG_MODULE_UNLOAD */
728 #ifdef CONFIG_OBSOLETE_MODPARM
729 static int param_set_byte(const char *val, struct kernel_param *kp)
734 if (!val) return -EINVAL;
735 l = simple_strtol(val, &endp, 0);
736 if (endp == val || *endp || ((char)l != l))
738 *((char *)kp->arg) = l;
742 /* Bounds checking done below */
743 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
745 strcpy(kp->arg, val);
749 int set_obsolete(const char *val, struct kernel_param *kp)
751 unsigned int min, max;
752 unsigned int size, maxsize;
756 struct obsolete_modparm *obsparm = kp->arg;
759 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
763 /* type is: [min[-max]]{b,h,i,l,s} */
765 min = simple_strtol(p, &endp, 10);
766 if (endp == obsparm->type)
768 else if (*endp == '-') {
770 max = simple_strtol(p, &endp, 10);
775 return param_array(kp->name, val, min, max, obsparm->addr,
776 1, param_set_byte, &dummy);
778 return param_array(kp->name, val, min, max, obsparm->addr,
779 sizeof(short), param_set_short, &dummy);
781 return param_array(kp->name, val, min, max, obsparm->addr,
782 sizeof(int), param_set_int, &dummy);
784 return param_array(kp->name, val, min, max, obsparm->addr,
785 sizeof(long), param_set_long, &dummy);
787 return param_array(kp->name, val, min, max, obsparm->addr,
788 sizeof(char *), param_set_charp, &dummy);
791 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
792 and the decl is "char xxx[5][50];" */
794 maxsize = simple_strtol(p, &endp, 10);
795 /* We check lengths here (yes, this is a hack). */
797 while (p[size = strcspn(p, ",")]) {
804 return param_array(kp->name, val, min, max, obsparm->addr,
805 maxsize, obsparm_copy_string, &dummy);
807 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
811 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
815 static int obsolete_params(const char *name,
817 struct obsolete_modparm obsparm[],
820 unsigned int symindex,
823 struct kernel_param *kp;
827 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
831 for (i = 0; i < num; i++) {
832 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
834 snprintf(sym_name, sizeof(sym_name), "%s%s",
835 MODULE_SYMBOL_PREFIX, obsparm[i].name);
837 kp[i].name = obsparm[i].name;
839 kp[i].set = set_obsolete;
842 = (void *)find_local_symbol(sechdrs, symindex, strtab,
844 if (!obsparm[i].addr) {
845 printk("%s: falsely claims to have parameter %s\n",
846 name, obsparm[i].name);
850 kp[i].arg = &obsparm[i];
853 ret = parse_args(name, args, kp, num, NULL);
859 static int obsolete_params(const char *name,
861 struct obsolete_modparm obsparm[],
864 unsigned int symindex,
868 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
872 #endif /* CONFIG_OBSOLETE_MODPARM */
874 static const char vermagic[] = VERMAGIC_STRING;
876 #ifdef CONFIG_MODVERSIONS
877 static int check_version(Elf_Shdr *sechdrs,
878 unsigned int versindex,
881 const unsigned long *crc)
883 unsigned int i, num_versions;
884 struct modversion_info *versions;
886 /* Exporting module didn't supply crcs? OK, we're already tainted. */
890 versions = (void *) sechdrs[versindex].sh_addr;
891 num_versions = sechdrs[versindex].sh_size
892 / sizeof(struct modversion_info);
894 for (i = 0; i < num_versions; i++) {
895 if (strcmp(versions[i].name, symname) != 0)
898 if (versions[i].crc == *crc)
900 printk("%s: disagrees about version of symbol %s\n",
902 DEBUGP("Found checksum %lX vs module %lX\n",
903 *crc, versions[i].crc);
906 /* Not in module's version table. OK, but that taints the kernel. */
907 if (!(tainted & TAINT_FORCED_MODULE)) {
908 printk("%s: no version for \"%s\" found: kernel tainted.\n",
910 tainted |= TAINT_FORCED_MODULE;
915 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
916 unsigned int versindex,
919 const unsigned long *crc;
920 struct module *owner;
922 if (!__find_symbol("struct_module", &owner, &crc, 1))
924 return check_version(sechdrs, versindex, "struct_module", mod,
928 /* First part is kernel version, which we ignore. */
929 static inline int same_magic(const char *amagic, const char *bmagic)
931 amagic += strcspn(amagic, " ");
932 bmagic += strcspn(bmagic, " ");
933 return strcmp(amagic, bmagic) == 0;
936 static inline int check_version(Elf_Shdr *sechdrs,
937 unsigned int versindex,
940 const unsigned long *crc)
945 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
946 unsigned int versindex,
952 static inline int same_magic(const char *amagic, const char *bmagic)
954 return strcmp(amagic, bmagic) == 0;
956 #endif /* CONFIG_MODVERSIONS */
958 /* Resolve a symbol for this module. I.e. if we find one, record usage.
959 Must be holding module_mutex. */
960 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
961 unsigned int versindex,
965 struct module *owner;
967 const unsigned long *crc;
969 spin_lock_irq(&modlist_lock);
970 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
972 /* use_module can fail due to OOM, or module unloading */
973 if (!check_version(sechdrs, versindex, name, mod, crc) ||
974 !use_module(mod, owner))
977 spin_unlock_irq(&modlist_lock);
983 * /sys/module/foo/sections stuff
984 * J. Corbet <corbet@lwn.net>
986 #ifdef CONFIG_KALLSYMS
987 static void module_sect_attrs_release(struct kobject *kobj)
989 kfree(container_of(kobj, struct module_sections, kobj));
992 static ssize_t module_sect_show(struct kobject *kobj, struct attribute *attr,
995 struct module_sect_attr *sattr =
996 container_of(attr, struct module_sect_attr, attr);
997 return sprintf(buf, "0x%lx\n", sattr->address);
1000 static struct sysfs_ops module_sect_ops = {
1001 .show = module_sect_show,
1004 static struct kobj_type module_sect_ktype = {
1005 .sysfs_ops = &module_sect_ops,
1006 .release = module_sect_attrs_release,
1009 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1010 char *secstrings, Elf_Shdr *sechdrs)
1012 unsigned int nloaded = 0, i;
1013 struct module_sect_attr *sattr;
1018 /* Count loaded sections and allocate structures */
1019 for (i = 0; i < nsect; i++)
1020 if (sechdrs[i].sh_flags & SHF_ALLOC)
1022 mod->sect_attrs = kmalloc(sizeof(struct module_sections) +
1023 nloaded*sizeof(mod->sect_attrs->attrs[0]), GFP_KERNEL);
1024 if (! mod->sect_attrs)
1027 /* sections entry setup */
1028 memset(mod->sect_attrs, 0, sizeof(struct module_sections));
1029 if (kobject_set_name(&mod->sect_attrs->kobj, "sections"))
1031 mod->sect_attrs->kobj.parent = &mod->mkobj->kobj;
1032 mod->sect_attrs->kobj.ktype = &module_sect_ktype;
1033 if (kobject_register(&mod->sect_attrs->kobj))
1036 /* And the section attributes. */
1037 sattr = &mod->sect_attrs->attrs[0];
1038 for (i = 0; i < nsect; i++) {
1039 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1041 sattr->address = sechdrs[i].sh_addr;
1042 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1043 MODULE_SECT_NAME_LEN);
1044 sattr->attr.name = sattr->name;
1045 sattr->attr.owner = mod;
1046 sattr->attr.mode = S_IRUGO;
1047 (void) sysfs_create_file(&mod->sect_attrs->kobj, &sattr->attr);
1052 kfree(mod->sect_attrs);
1053 mod->sect_attrs = NULL;
1056 static void remove_sect_attrs(struct module *mod)
1058 if (mod->sect_attrs) {
1059 kobject_unregister(&mod->sect_attrs->kobj);
1060 mod->sect_attrs = NULL;
1066 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1067 char *sectstrings, Elf_Shdr *sechdrs)
1071 static inline void remove_sect_attrs(struct module *mod)
1074 #endif /* CONFIG_KALLSYMS */
1079 #define to_module_attr(n) container_of(n, struct module_attribute, attr);
1081 static ssize_t module_attr_show(struct kobject *kobj,
1082 struct attribute *attr,
1086 struct module_attribute *attribute = to_module_attr(attr);
1088 if (!attribute->param->get)
1091 count = attribute->param->get(buf, attribute->param);
1099 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
1100 static ssize_t module_attr_store(struct kobject *kobj,
1101 struct attribute *attr,
1102 const char *buf, size_t len)
1105 struct module_attribute *attribute = to_module_attr(attr);
1107 if (!attribute->param->set)
1110 err = attribute->param->set(buf, attribute->param);
1116 static struct sysfs_ops module_sysfs_ops = {
1117 .show = module_attr_show,
1118 .store = module_attr_store,
1121 static void module_kobj_release(struct kobject *kobj)
1123 kfree(container_of(kobj, struct module_kobject, kobj));
1126 static struct kobj_type module_ktype = {
1127 .sysfs_ops = &module_sysfs_ops,
1128 .release = &module_kobj_release,
1130 static decl_subsys(module, &module_ktype, NULL);
1132 static int mod_sysfs_setup(struct module *mod,
1133 struct kernel_param *kparam,
1134 unsigned int num_params)
1139 /* We overallocate: not every param is in sysfs, and maybe no refcnt */
1140 mod->mkobj = kmalloc(sizeof(*mod->mkobj)
1141 + sizeof(mod->mkobj->attr[0]) * (num_params+1),
1146 memset(&mod->mkobj->kobj, 0, sizeof(mod->mkobj->kobj));
1147 err = kobject_set_name(&mod->mkobj->kobj, mod->name);
1150 kobj_set_kset_s(mod->mkobj, module_subsys);
1151 err = kobject_register(&mod->mkobj->kobj);
1155 mod->mkobj->num_attributes = 0;
1157 for (i = 0; i < num_params; i++) {
1158 if (kparam[i].perm) {
1159 err = add_attribute(mod, &kparam[i]);
1164 err = sysfs_unload_setup(mod);
1170 for (i = 0; i < mod->mkobj->num_attributes; i++)
1171 sysfs_remove_file(&mod->mkobj->kobj,&mod->mkobj->attr[i].attr);
1172 /* Calls module_kobj_release */
1173 kobject_unregister(&mod->mkobj->kobj);
1180 static void mod_kobject_remove(struct module *mod)
1183 for (i = 0; i < mod->mkobj->num_attributes; i++)
1184 sysfs_remove_file(&mod->mkobj->kobj,&mod->mkobj->attr[i].attr);
1185 /* Calls module_kobj_release */
1186 kobject_unregister(&mod->mkobj->kobj);
1189 /* Free a module, remove from lists, etc (must hold module mutex). */
1190 static void free_module(struct module *mod)
1192 /* Delete from various lists */
1193 spin_lock_irq(&modlist_lock);
1194 list_del(&mod->list);
1195 spin_unlock_irq(&modlist_lock);
1197 remove_sect_attrs(mod);
1198 mod_kobject_remove(mod);
1200 /* Arch-specific cleanup. */
1201 module_arch_cleanup(mod);
1203 /* Module unload stuff */
1204 module_unload_free(mod);
1206 /* This may be NULL, but that's OK */
1207 module_free(mod, mod->module_init);
1210 percpu_modfree(mod->percpu);
1212 /* Finally, free the core (containing the module structure) */
1213 module_free(mod, mod->module_core);
1216 void *__symbol_get(const char *symbol)
1218 struct module *owner;
1219 unsigned long value, flags;
1220 const unsigned long *crc;
1222 spin_lock_irqsave(&modlist_lock, flags);
1223 value = __find_symbol(symbol, &owner, &crc, 1);
1224 if (value && !strong_try_module_get(owner))
1226 spin_unlock_irqrestore(&modlist_lock, flags);
1228 return (void *)value;
1230 EXPORT_SYMBOL_GPL(__symbol_get);
1232 /* Change all symbols so that sh_value encodes the pointer directly. */
1233 static int simplify_symbols(Elf_Shdr *sechdrs,
1234 unsigned int symindex,
1236 unsigned int versindex,
1237 unsigned int pcpuindex,
1240 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1241 unsigned long secbase;
1242 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1245 for (i = 1; i < n; i++) {
1246 switch (sym[i].st_shndx) {
1248 /* We compiled with -fno-common. These are not
1249 supposed to happen. */
1250 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1251 printk("%s: please compile with -fno-common\n",
1257 /* Don't need to do anything */
1258 DEBUGP("Absolute symbol: 0x%08lx\n",
1259 (long)sym[i].st_value);
1264 = resolve_symbol(sechdrs, versindex,
1265 strtab + sym[i].st_name, mod);
1267 /* Ok if resolved. */
1268 if (sym[i].st_value != 0)
1271 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1274 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1275 mod->name, strtab + sym[i].st_name);
1280 /* Divert to percpu allocation if a percpu var. */
1281 if (sym[i].st_shndx == pcpuindex)
1282 secbase = (unsigned long)mod->percpu;
1284 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1285 sym[i].st_value += secbase;
1293 /* Update size with this section: return offset. */
1294 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1298 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1299 *size = ret + sechdr->sh_size;
1303 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1304 might -- code, read-only data, read-write data, small data. Tally
1305 sizes, and place the offsets into sh_entsize fields: high bit means it
1307 static void layout_sections(struct module *mod,
1308 const Elf_Ehdr *hdr,
1310 const char *secstrings)
1312 static unsigned long const masks[][2] = {
1313 /* NOTE: all executable code must be the first section
1314 * in this array; otherwise modify the text_size
1315 * finder in the two loops below */
1316 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1317 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1318 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1319 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1323 for (i = 0; i < hdr->e_shnum; i++)
1324 sechdrs[i].sh_entsize = ~0UL;
1326 DEBUGP("Core section allocation order:\n");
1327 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1328 for (i = 0; i < hdr->e_shnum; ++i) {
1329 Elf_Shdr *s = &sechdrs[i];
1331 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1332 || (s->sh_flags & masks[m][1])
1333 || s->sh_entsize != ~0UL
1334 || strncmp(secstrings + s->sh_name,
1337 s->sh_entsize = get_offset(&mod->core_size, s);
1338 DEBUGP("\t%s\n", secstrings + s->sh_name);
1341 mod->core_text_size = mod->core_size;
1344 DEBUGP("Init section allocation order:\n");
1345 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1346 for (i = 0; i < hdr->e_shnum; ++i) {
1347 Elf_Shdr *s = &sechdrs[i];
1349 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1350 || (s->sh_flags & masks[m][1])
1351 || s->sh_entsize != ~0UL
1352 || strncmp(secstrings + s->sh_name,
1355 s->sh_entsize = (get_offset(&mod->init_size, s)
1356 | INIT_OFFSET_MASK);
1357 DEBUGP("\t%s\n", secstrings + s->sh_name);
1360 mod->init_text_size = mod->init_size;
1364 static inline int license_is_gpl_compatible(const char *license)
1366 return (strcmp(license, "GPL") == 0
1367 || strcmp(license, "GPL v2") == 0
1368 || strcmp(license, "GPL and additional rights") == 0
1369 || strcmp(license, "Dual BSD/GPL") == 0
1370 || strcmp(license, "Dual MPL/GPL") == 0);
1373 static void set_license(struct module *mod, const char *license)
1376 license = "unspecified";
1378 mod->license_gplok = license_is_gpl_compatible(license);
1379 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1380 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1381 mod->name, license);
1382 tainted |= TAINT_PROPRIETARY_MODULE;
1386 /* Parse tag=value strings from .modinfo section */
1387 static char *next_string(char *string, unsigned long *secsize)
1389 /* Skip non-zero chars */
1392 if ((*secsize)-- <= 1)
1396 /* Skip any zero padding. */
1397 while (!string[0]) {
1399 if ((*secsize)-- <= 1)
1405 static char *get_modinfo(Elf_Shdr *sechdrs,
1410 unsigned int taglen = strlen(tag);
1411 unsigned long size = sechdrs[info].sh_size;
1413 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1414 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1415 return p + taglen + 1;
1420 #ifdef CONFIG_KALLSYMS
1421 int is_exported(const char *name, const struct module *mod)
1426 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1427 if (strcmp(__start___ksymtab[i].name, name) == 0)
1431 for (i = 0; i < mod->num_syms; i++)
1432 if (strcmp(mod->syms[i].name, name) == 0)
1438 static char elf_type(const Elf_Sym *sym,
1440 const char *secstrings,
1443 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1444 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1449 if (sym->st_shndx == SHN_UNDEF)
1451 if (sym->st_shndx == SHN_ABS)
1453 if (sym->st_shndx >= SHN_LORESERVE)
1455 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1457 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1458 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1459 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1461 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1466 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1467 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1472 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1473 ".debug", strlen(".debug")) == 0)
1478 static void add_kallsyms(struct module *mod,
1480 unsigned int symindex,
1481 unsigned int strindex,
1482 const char *secstrings)
1486 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1487 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1488 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1490 /* Set types up while we still have access to sections. */
1491 for (i = 0; i < mod->num_symtab; i++)
1492 mod->symtab[i].st_info
1493 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1496 static inline void add_kallsyms(struct module *mod,
1498 unsigned int symindex,
1499 unsigned int strindex,
1500 const char *secstrings)
1503 #endif /* CONFIG_KALLSYMS */
1505 /* Allocate and load the module: note that size of section 0 is always
1506 zero, and we rely on this for optional sections. */
1507 static struct module *load_module(void __user *umod,
1509 const char __user *uargs)
1513 char *secstrings, *args, *modmagic, *strtab = NULL;
1514 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1515 exportindex, modindex, obsparmindex, infoindex, gplindex,
1516 crcindex, gplcrcindex, versindex, pcpuindex;
1520 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1521 struct exception_table_entry *extable;
1523 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1525 if (len < sizeof(*hdr))
1526 return ERR_PTR(-ENOEXEC);
1528 /* Suck in entire file: we'll want most of it. */
1529 /* vmalloc barfs on "unusual" numbers. Check here */
1530 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1531 return ERR_PTR(-ENOMEM);
1532 if (copy_from_user(hdr, umod, len) != 0) {
1537 /* Sanity checks against insmoding binaries or wrong arch,
1538 weird elf version */
1539 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1540 || hdr->e_type != ET_REL
1541 || !elf_check_arch(hdr)
1542 || hdr->e_shentsize != sizeof(*sechdrs)) {
1547 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1550 /* Convenience variables */
1551 sechdrs = (void *)hdr + hdr->e_shoff;
1552 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1553 sechdrs[0].sh_addr = 0;
1555 /* And these should exist, but gcc whinges if we don't init them */
1556 symindex = strindex = 0;
1558 for (i = 1; i < hdr->e_shnum; i++) {
1559 if (sechdrs[i].sh_type != SHT_NOBITS
1560 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1563 /* Mark all sections sh_addr with their address in the
1565 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1567 /* Internal symbols and strings. */
1568 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1570 strindex = sechdrs[i].sh_link;
1571 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1573 #ifndef CONFIG_MODULE_UNLOAD
1574 /* Don't load .exit sections */
1575 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1576 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1580 modindex = find_sec(hdr, sechdrs, secstrings,
1581 ".gnu.linkonce.this_module");
1583 printk(KERN_WARNING "No module found in object\n");
1587 mod = (void *)sechdrs[modindex].sh_addr;
1589 /* Optional sections */
1590 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1591 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1592 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1593 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1594 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1595 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1596 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1597 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1598 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1599 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1601 /* Don't keep modinfo section */
1602 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1603 #ifdef CONFIG_KALLSYMS
1604 /* Keep symbol and string tables for decoding later. */
1605 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1606 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1609 /* Check module struct version now, before we try to use module. */
1610 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1615 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1616 /* This is allowed: modprobe --force will invalidate it. */
1618 tainted |= TAINT_FORCED_MODULE;
1619 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1621 } else if (!same_magic(modmagic, vermagic)) {
1622 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1623 mod->name, modmagic, vermagic);
1628 /* verify the signature on the module */
1629 #ifdef CONFIG_MODULE_SIG
1630 if (module_verify_sig(hdr, sechdrs, secstrings, mod)) {
1636 /* Now copy in args */
1637 arglen = strlen_user(uargs);
1642 args = kmalloc(arglen, GFP_KERNEL);
1647 if (copy_from_user(args, uargs, arglen) != 0) {
1652 if (find_module(mod->name)) {
1657 mod->state = MODULE_STATE_COMING;
1659 /* Allow arches to frob section contents and sizes. */
1660 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1665 /* We have a special allocation for this section. */
1666 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1667 sechdrs[pcpuindex].sh_addralign);
1672 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1673 mod->percpu = percpu;
1676 /* Determine total sizes, and put offsets in sh_entsize. For now
1677 this is done generically; there doesn't appear to be any
1678 special cases for the architectures. */
1679 layout_sections(mod, hdr, sechdrs, secstrings);
1681 /* Do the allocs. */
1682 ptr = module_alloc(mod->core_size);
1687 memset(ptr, 0, mod->core_size);
1688 mod->module_core = ptr;
1690 ptr = module_alloc(mod->init_size);
1691 if (!ptr && mod->init_size) {
1695 memset(ptr, 0, mod->init_size);
1696 mod->module_init = ptr;
1698 /* Transfer each section which specifies SHF_ALLOC */
1699 DEBUGP("final section addresses:\n");
1700 for (i = 0; i < hdr->e_shnum; i++) {
1703 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1706 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1707 dest = mod->module_init
1708 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1710 dest = mod->module_core + sechdrs[i].sh_entsize;
1712 if (sechdrs[i].sh_type != SHT_NOBITS)
1713 memcpy(dest, (void *)sechdrs[i].sh_addr,
1714 sechdrs[i].sh_size);
1715 /* Update sh_addr to point to copy in image. */
1716 sechdrs[i].sh_addr = (unsigned long)dest;
1717 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1719 /* Module has been moved. */
1720 mod = (void *)sechdrs[modindex].sh_addr;
1722 /* Now we've moved module, initialize linked lists, etc. */
1723 module_unload_init(mod);
1725 /* Set up license info based on the info section */
1726 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1728 /* Fix up syms, so that st_value is a pointer to location. */
1729 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1734 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1735 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1736 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1738 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1739 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1740 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1742 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1744 #ifdef CONFIG_MODVERSIONS
1745 if ((mod->num_syms && !crcindex) ||
1746 (mod->num_gpl_syms && !gplcrcindex)) {
1747 printk(KERN_WARNING "%s: No versions for exported symbols."
1748 " Tainting kernel.\n", mod->name);
1749 tainted |= TAINT_FORCED_MODULE;
1753 /* Now do relocations. */
1754 for (i = 1; i < hdr->e_shnum; i++) {
1755 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1756 unsigned int info = sechdrs[i].sh_info;
1758 /* Not a valid relocation section? */
1759 if (info >= hdr->e_shnum)
1762 /* Don't bother with non-allocated sections */
1763 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1766 if (sechdrs[i].sh_type == SHT_REL)
1767 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1768 else if (sechdrs[i].sh_type == SHT_RELA)
1769 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1775 /* Set up and sort exception table */
1776 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1777 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1778 sort_extable(extable, extable + mod->num_exentries);
1780 /* Finally, copy percpu area over. */
1781 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1782 sechdrs[pcpuindex].sh_size);
1784 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1786 err = module_finalize(hdr, sechdrs, mod);
1792 err = obsolete_params(mod->name, mod->args,
1793 (struct obsolete_modparm *)
1794 sechdrs[obsparmindex].sh_addr,
1795 sechdrs[obsparmindex].sh_size
1796 / sizeof(struct obsolete_modparm),
1798 (char *)sechdrs[strindex].sh_addr);
1800 printk(KERN_WARNING "%s: Ignoring new-style "
1801 "parameters in presence of obsolete ones\n",
1804 /* Size of section 0 is 0, so this works well if no params */
1805 err = parse_args(mod->name, mod->args,
1806 (struct kernel_param *)
1807 sechdrs[setupindex].sh_addr,
1808 sechdrs[setupindex].sh_size
1809 / sizeof(struct kernel_param),
1812 err = mod_sysfs_setup(mod,
1813 (struct kernel_param *)
1814 sechdrs[setupindex].sh_addr,
1815 sechdrs[setupindex].sh_size
1816 / sizeof(struct kernel_param));
1819 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1821 /* Get rid of temporary copy */
1828 module_arch_cleanup(mod);
1830 module_unload_free(mod);
1831 module_free(mod, mod->module_init);
1833 module_free(mod, mod->module_core);
1836 percpu_modfree(percpu);
1841 if (err < 0) return ERR_PTR(err);
1845 printk(KERN_ERR "Module len %lu truncated\n", len);
1850 /* This is where the real work happens */
1852 sys_init_module(void __user *umod,
1854 const char __user *uargs)
1859 /* Must have permission */
1860 if (!capable(CAP_SYS_MODULE))
1863 /* Only one module load at a time, please */
1864 if (down_interruptible(&module_mutex) != 0)
1867 /* Do all the hard work */
1868 mod = load_module(umod, len, uargs);
1871 return PTR_ERR(mod);
1874 /* Flush the instruction cache, since we've played with text */
1875 if (mod->module_init)
1876 flush_icache_range((unsigned long)mod->module_init,
1877 (unsigned long)mod->module_init
1879 flush_icache_range((unsigned long)mod->module_core,
1880 (unsigned long)mod->module_core + mod->core_size);
1882 /* Now sew it into the lists. They won't access us, since
1883 strong_try_module_get() will fail. */
1884 spin_lock_irq(&modlist_lock);
1885 list_add(&mod->list, &modules);
1886 spin_unlock_irq(&modlist_lock);
1888 /* Drop lock so they can recurse */
1891 down(¬ify_mutex);
1892 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1895 /* Start the module */
1898 /* Init routine failed: abort. Try to protect us from
1899 buggy refcounters. */
1900 mod->state = MODULE_STATE_GOING;
1901 synchronize_kernel();
1903 printk(KERN_ERR "%s: module is now stuck!\n",
1907 down(&module_mutex);
1914 /* Now it's a first class citizen! */
1915 down(&module_mutex);
1916 mod->state = MODULE_STATE_LIVE;
1917 /* Drop initial reference. */
1919 module_free(mod, mod->module_init);
1920 mod->module_init = NULL;
1922 mod->init_text_size = 0;
1928 static inline int within(unsigned long addr, void *start, unsigned long size)
1930 return ((void *)addr >= start && (void *)addr < start + size);
1933 #ifdef CONFIG_KALLSYMS
1934 static const char *get_ksymbol(struct module *mod,
1936 unsigned long *size,
1937 unsigned long *offset)
1939 unsigned int i, best = 0;
1940 unsigned long nextval;
1942 /* At worse, next value is at end of module */
1943 if (within(addr, mod->module_init, mod->init_size))
1944 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1946 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1948 /* Scan for closest preceeding symbol, and next symbol. (ELF
1949 starts real symbols at 1). */
1950 for (i = 1; i < mod->num_symtab; i++) {
1951 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1954 /* We ignore unnamed symbols: they're uninformative
1955 * and inserted at a whim. */
1956 if (mod->symtab[i].st_value <= addr
1957 && mod->symtab[i].st_value > mod->symtab[best].st_value
1958 && *(mod->strtab + mod->symtab[i].st_name) != '\0' )
1960 if (mod->symtab[i].st_value > addr
1961 && mod->symtab[i].st_value < nextval
1962 && *(mod->strtab + mod->symtab[i].st_name) != '\0')
1963 nextval = mod->symtab[i].st_value;
1969 *size = nextval - mod->symtab[best].st_value;
1970 *offset = addr - mod->symtab[best].st_value;
1971 return mod->strtab + mod->symtab[best].st_name;
1974 /* For kallsyms to ask for address resolution. NULL means not found.
1975 We don't lock, as this is used for oops resolution and races are a
1977 const char *module_address_lookup(unsigned long addr,
1978 unsigned long *size,
1979 unsigned long *offset,
1984 list_for_each_entry(mod, &modules, list) {
1985 if (within(addr, mod->module_init, mod->init_size)
1986 || within(addr, mod->module_core, mod->core_size)) {
1987 *modname = mod->name;
1988 return get_ksymbol(mod, addr, size, offset);
1994 struct module *module_get_kallsym(unsigned int symnum,
1995 unsigned long *value,
2001 down(&module_mutex);
2002 list_for_each_entry(mod, &modules, list) {
2003 if (symnum < mod->num_symtab) {
2004 *value = mod->symtab[symnum].st_value;
2005 *type = mod->symtab[symnum].st_info;
2007 mod->strtab + mod->symtab[symnum].st_name,
2012 symnum -= mod->num_symtab;
2018 static unsigned long mod_find_symname(struct module *mod, const char *name)
2022 for (i = 0; i < mod->num_symtab; i++)
2023 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
2024 return mod->symtab[i].st_value;
2028 /* Look for this name: can be of form module:name. */
2029 unsigned long module_kallsyms_lookup_name(const char *name)
2033 unsigned long ret = 0;
2035 /* Don't lock: we're in enough trouble already. */
2036 if ((colon = strchr(name, ':')) != NULL) {
2038 if ((mod = find_module(name)) != NULL)
2039 ret = mod_find_symname(mod, colon+1);
2042 list_for_each_entry(mod, &modules, list)
2043 if ((ret = mod_find_symname(mod, name)) != 0)
2048 #endif /* CONFIG_KALLSYMS */
2050 /* Called by the /proc file system to return a list of modules. */
2051 static void *m_start(struct seq_file *m, loff_t *pos)
2053 struct list_head *i;
2056 down(&module_mutex);
2057 list_for_each(i, &modules) {
2066 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2068 struct list_head *i = p;
2070 if (i->next == &modules)
2075 static void m_stop(struct seq_file *m, void *p)
2080 static int m_show(struct seq_file *m, void *p)
2082 struct module *mod = list_entry(p, struct module, list);
2083 seq_printf(m, "%s %lu",
2084 mod->name, mod->init_size + mod->core_size);
2085 print_unload_info(m, mod);
2087 /* Informative for users. */
2088 seq_printf(m, " %s",
2089 mod->state == MODULE_STATE_GOING ? "Unloading":
2090 mod->state == MODULE_STATE_COMING ? "Loading":
2092 /* Used by oprofile and other similar tools. */
2093 seq_printf(m, " 0x%p", mod->module_core);
2095 seq_printf(m, "\n");
2099 /* Format: modulename size refcount deps address
2101 Where refcount is a number or -, and deps is a comma-separated list
2104 struct seq_operations modules_op = {
2111 /* Given an address, look for it in the module exception tables. */
2112 const struct exception_table_entry *search_module_extables(unsigned long addr)
2114 unsigned long flags;
2115 const struct exception_table_entry *e = NULL;
2118 spin_lock_irqsave(&modlist_lock, flags);
2119 list_for_each_entry(mod, &modules, list) {
2120 if (mod->num_exentries == 0)
2123 e = search_extable(mod->extable,
2124 mod->extable + mod->num_exentries - 1,
2129 spin_unlock_irqrestore(&modlist_lock, flags);
2131 /* Now, if we found one, we are running inside it now, hence
2132 we cannot unload the module, hence no refcnt needed. */
2136 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2137 struct module *__module_text_address(unsigned long addr)
2141 list_for_each_entry(mod, &modules, list)
2142 if (within(addr, mod->module_init, mod->init_text_size)
2143 || within(addr, mod->module_core, mod->core_text_size))
2148 struct module *module_text_address(unsigned long addr)
2151 unsigned long flags;
2153 spin_lock_irqsave(&modlist_lock, flags);
2154 mod = __module_text_address(addr);
2155 spin_unlock_irqrestore(&modlist_lock, flags);
2160 /* Don't grab lock, we're oopsing. */
2161 void print_modules(void)
2165 printk("Modules linked in:");
2166 list_for_each_entry(mod, &modules, list) {
2167 printk(" %s", mod->name);
2168 #if CONFIG_MODULE_SIG
2169 if (!mod->gpgsig_ok)
2176 #ifdef CONFIG_MODVERSIONS
2177 /* Generate the signature for struct module here, too, for modversions. */
2178 void struct_module(struct module *mod) { return; }
2179 EXPORT_SYMBOL(struct_module);
2182 static int __init modules_init(void)
2184 return subsystem_register(&module_subsys);
2186 __initcall(modules_init);