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/pgalloc.h>
40 #include <asm/cacheflush.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 #define symbol_is(literal, string) \
56 (strcmp(MODULE_SYMBOL_PREFIX literal, (string)) == 0)
58 /* Protects module list */
59 static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
61 /* List of modules, protected by module_mutex AND modlist_lock */
62 static DECLARE_MUTEX(module_mutex);
63 static LIST_HEAD(modules);
65 static DECLARE_MUTEX(notify_mutex);
66 static struct notifier_block * module_notify_list;
68 int register_module_notifier(struct notifier_block * nb)
72 err = notifier_chain_register(&module_notify_list, nb);
76 EXPORT_SYMBOL(register_module_notifier);
78 int unregister_module_notifier(struct notifier_block * nb)
82 err = notifier_chain_unregister(&module_notify_list, nb);
86 EXPORT_SYMBOL(unregister_module_notifier);
88 /* We require a truly strong try_module_get() */
89 static inline int strong_try_module_get(struct module *mod)
91 if (mod && mod->state == MODULE_STATE_COMING)
93 return try_module_get(mod);
96 /* Stub function for modules which don't have an initfn */
101 EXPORT_SYMBOL(init_module);
103 /* A thread that wants to hold a reference to a module only while it
104 * is running can call ths to safely exit.
105 * nfsd and lockd use this.
107 void __module_put_and_exit(struct module *mod, long code)
112 EXPORT_SYMBOL(__module_put_and_exit);
114 /* Find a module section: 0 means not found. */
115 static unsigned int find_sec(Elf_Ehdr *hdr,
117 const char *secstrings,
122 for (i = 1; i < hdr->e_shnum; i++)
123 /* Alloc bit cleared means "ignore it." */
124 if ((sechdrs[i].sh_flags & SHF_ALLOC)
125 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
130 /* Provided by the linker */
131 extern const struct kernel_symbol __start___ksymtab[];
132 extern const struct kernel_symbol __stop___ksymtab[];
133 extern const struct kernel_symbol __start___ksymtab_gpl[];
134 extern const struct kernel_symbol __stop___ksymtab_gpl[];
135 extern const unsigned long __start___kcrctab[];
136 extern const unsigned long __start___kcrctab_gpl[];
138 #ifndef CONFIG_MODVERSIONS
139 #define symversion(base, idx) NULL
141 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
144 /* Find a symbol, return value, crc and module which owns it */
145 static unsigned long __find_symbol(const char *name,
146 struct module **owner,
147 const unsigned long **crc,
153 /* Core kernel first. */
155 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
156 if (strcmp(__start___ksymtab[i].name, name) == 0) {
157 *crc = symversion(__start___kcrctab, i);
158 return __start___ksymtab[i].value;
162 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
163 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
164 *crc = symversion(__start___kcrctab_gpl, i);
165 return __start___ksymtab_gpl[i].value;
169 /* Now try modules. */
170 list_for_each_entry(mod, &modules, list) {
172 for (i = 0; i < mod->num_syms; i++)
173 if (strcmp(mod->syms[i].name, name) == 0) {
174 *crc = symversion(mod->crcs, i);
175 return mod->syms[i].value;
179 for (i = 0; i < mod->num_gpl_syms; i++) {
180 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
181 *crc = symversion(mod->gpl_crcs, i);
182 return mod->gpl_syms[i].value;
187 DEBUGP("Failed to find symbol %s\n", name);
191 /* Find a symbol in this elf symbol table */
192 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
193 unsigned int symindex,
198 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
200 /* Search (defined) internal symbols first. */
201 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
202 if (sym[i].st_shndx != SHN_UNDEF
203 && strcmp(name, strtab + sym[i].st_name) == 0)
204 return sym[i].st_value;
209 /* Search for module by name: must hold module_mutex. */
210 static struct module *find_module(const char *name)
214 list_for_each_entry(mod, &modules, list) {
215 if (strcmp(mod->name, name) == 0)
222 /* Number of blocks used and allocated. */
223 static unsigned int pcpu_num_used, pcpu_num_allocated;
224 /* Size of each block. -ve means used. */
225 static int *pcpu_size;
227 static int split_block(unsigned int i, unsigned short size)
229 /* Reallocation required? */
230 if (pcpu_num_used + 1 > pcpu_num_allocated) {
231 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
236 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
237 pcpu_num_allocated *= 2;
242 /* Insert a new subblock */
243 memmove(&pcpu_size[i+1], &pcpu_size[i],
244 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
247 pcpu_size[i+1] -= size;
252 static inline unsigned int block_size(int val)
259 /* Created by linker magic */
260 extern char __per_cpu_start[], __per_cpu_end[];
262 static void *percpu_modalloc(unsigned long size, unsigned long align)
268 BUG_ON(align > SMP_CACHE_BYTES);
270 ptr = __per_cpu_start;
271 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
272 /* Extra for alignment requirement. */
273 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
274 BUG_ON(i == 0 && extra != 0);
276 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
279 /* Transfer extra to previous block. */
280 if (pcpu_size[i-1] < 0)
281 pcpu_size[i-1] -= extra;
283 pcpu_size[i-1] += extra;
284 pcpu_size[i] -= extra;
287 /* Split block if warranted */
288 if (pcpu_size[i] - size > sizeof(unsigned long))
289 if (!split_block(i, size))
293 pcpu_size[i] = -pcpu_size[i];
297 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
302 static void percpu_modfree(void *freeme)
305 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
307 /* First entry is core kernel percpu data. */
308 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
310 pcpu_size[i] = -pcpu_size[i];
317 /* Merge with previous? */
318 if (pcpu_size[i-1] >= 0) {
319 pcpu_size[i-1] += pcpu_size[i];
321 memmove(&pcpu_size[i], &pcpu_size[i+1],
322 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
325 /* Merge with next? */
326 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
327 pcpu_size[i] += pcpu_size[i+1];
329 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
330 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
334 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
336 const char *secstrings)
338 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
341 static int percpu_modinit(void)
344 pcpu_num_allocated = 2;
345 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
347 /* Static in-kernel percpu data (used). */
348 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
350 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
351 if (pcpu_size[1] < 0) {
352 printk(KERN_ERR "No per-cpu room for modules.\n");
358 __initcall(percpu_modinit);
359 #else /* ... !CONFIG_SMP */
360 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
364 static inline void percpu_modfree(void *pcpuptr)
368 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
370 const char *secstrings)
374 static inline void percpu_modcopy(void *pcpudst, const void *src,
377 /* pcpusec should be 0, and size of that section should be 0. */
380 #endif /* CONFIG_SMP */
382 static int add_attribute(struct module *mod, struct kernel_param *kp)
384 struct module_attribute *a;
387 a = &mod->mkobj->attr[mod->mkobj->num_attributes];
388 a->attr.name = (char *)kp->name;
390 a->attr.mode = kp->perm;
392 retval = sysfs_create_file(&mod->mkobj->kobj, &a->attr);
394 mod->mkobj->num_attributes++;
398 #ifdef CONFIG_MODULE_UNLOAD
399 /* Init the unload section of the module. */
400 static void module_unload_init(struct module *mod)
404 INIT_LIST_HEAD(&mod->modules_which_use_me);
405 for (i = 0; i < NR_CPUS; i++)
406 local_set(&mod->ref[i].count, 0);
407 /* Hold reference count during initialization. */
408 local_set(&mod->ref[smp_processor_id()].count, 1);
409 /* Backwards compatibility macros put refcount during init. */
410 mod->waiter = current;
413 /* modules using other modules */
416 struct list_head list;
417 struct module *module_which_uses;
420 /* Does a already use b? */
421 static int already_uses(struct module *a, struct module *b)
423 struct module_use *use;
425 list_for_each_entry(use, &b->modules_which_use_me, list) {
426 if (use->module_which_uses == a) {
427 DEBUGP("%s uses %s!\n", a->name, b->name);
431 DEBUGP("%s does not use %s!\n", a->name, b->name);
435 /* Module a uses b */
436 static int use_module(struct module *a, struct module *b)
438 struct module_use *use;
439 if (b == NULL || already_uses(a, b)) return 1;
441 if (!strong_try_module_get(b))
444 DEBUGP("Allocating new usage for %s.\n", a->name);
445 use = kmalloc(sizeof(*use), GFP_ATOMIC);
447 printk("%s: out of memory loading\n", a->name);
452 use->module_which_uses = a;
453 list_add(&use->list, &b->modules_which_use_me);
457 /* Clear the unload stuff of the module. */
458 static void module_unload_free(struct module *mod)
462 list_for_each_entry(i, &modules, list) {
463 struct module_use *use;
465 list_for_each_entry(use, &i->modules_which_use_me, list) {
466 if (use->module_which_uses == mod) {
467 DEBUGP("%s unusing %s\n", mod->name, i->name);
469 list_del(&use->list);
471 /* There can be at most one match. */
478 #ifdef CONFIG_MODULE_FORCE_UNLOAD
479 static inline int try_force(unsigned int flags)
481 int ret = (flags & O_TRUNC);
483 tainted |= TAINT_FORCED_MODULE;
487 static inline int try_force(unsigned int flags)
491 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
500 /* Whole machine is stopped with interrupts off when this runs. */
501 static inline int __try_stop_module(void *_sref)
503 struct stopref *sref = _sref;
505 /* If it's not unused, quit unless we are told to block. */
506 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
507 if (!(*sref->forced = try_force(sref->flags)))
511 /* Mark it as dying. */
512 sref->mod->state = MODULE_STATE_GOING;
516 static int try_stop_module(struct module *mod, int flags, int *forced)
518 struct stopref sref = { mod, flags, forced };
520 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
523 unsigned int module_refcount(struct module *mod)
525 unsigned int i, total = 0;
527 for (i = 0; i < NR_CPUS; i++)
528 total += local_read(&mod->ref[i].count);
531 EXPORT_SYMBOL(module_refcount);
533 /* This exists whether we can unload or not */
534 static void free_module(struct module *mod);
536 /* Stub function for modules which don't have an exitfn */
537 void cleanup_module(void)
540 EXPORT_SYMBOL(cleanup_module);
542 static void wait_for_zero_refcount(struct module *mod)
544 /* Since we might sleep for some time, drop the semaphore first */
547 DEBUGP("Looking at refcount...\n");
548 set_current_state(TASK_UNINTERRUPTIBLE);
549 if (module_refcount(mod) == 0)
553 current->state = TASK_RUNNING;
558 sys_delete_module(const char __user *name_user, unsigned int flags)
561 char name[MODULE_NAME_LEN];
564 if (!capable(CAP_SYS_MODULE))
567 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
569 name[MODULE_NAME_LEN-1] = '\0';
571 if (down_interruptible(&module_mutex) != 0)
574 mod = find_module(name);
580 if (!list_empty(&mod->modules_which_use_me)) {
581 /* Other modules depend on us: get rid of them first. */
586 /* Doing init or already dying? */
587 if (mod->state != MODULE_STATE_LIVE) {
588 /* FIXME: if (force), slam module count and wake up
590 DEBUGP("%s already dying\n", mod->name);
595 /* If it has an init func, it must have an exit func to unload */
596 if ((mod->init != init_module && mod->exit == cleanup_module)
598 forced = try_force(flags);
600 /* This module can't be removed */
606 /* Set this up before setting mod->state */
607 mod->waiter = current;
609 /* Stop the machine so refcounts can't move and disable module. */
610 ret = try_stop_module(mod, flags, &forced);
612 /* Never wait if forced. */
613 if (!forced && module_refcount(mod) != 0)
614 wait_for_zero_refcount(mod);
616 /* Final destruction now noone is using it. */
627 static void print_unload_info(struct seq_file *m, struct module *mod)
629 struct module_use *use;
630 int printed_something = 0;
632 seq_printf(m, " %u ", module_refcount(mod));
634 /* Always include a trailing , so userspace can differentiate
635 between this and the old multi-field proc format. */
636 list_for_each_entry(use, &mod->modules_which_use_me, list) {
637 printed_something = 1;
638 seq_printf(m, "%s,", use->module_which_uses->name);
642 printed_something = 1;
643 seq_printf(m, "[unsafe],");
646 if (mod->init != init_module && mod->exit == cleanup_module) {
647 printed_something = 1;
648 seq_printf(m, "[permanent],");
651 if (!printed_something)
655 void __symbol_put(const char *symbol)
657 struct module *owner;
659 const unsigned long *crc;
661 spin_lock_irqsave(&modlist_lock, flags);
662 if (!__find_symbol(symbol, &owner, &crc, 1))
665 spin_unlock_irqrestore(&modlist_lock, flags);
667 EXPORT_SYMBOL(__symbol_put);
669 void symbol_put_addr(void *addr)
673 spin_lock_irqsave(&modlist_lock, flags);
674 if (!kernel_text_address((unsigned long)addr))
677 module_put(module_text_address((unsigned long)addr));
678 spin_unlock_irqrestore(&modlist_lock, flags);
680 EXPORT_SYMBOL_GPL(symbol_put_addr);
682 static int refcnt_get_fn(char *buffer, struct kernel_param *kp)
684 struct module *mod = container_of(kp, struct module, refcnt_param);
686 /* sysfs holds one reference. */
687 return sprintf(buffer, "%u", module_refcount(mod)-1);
690 static inline int sysfs_unload_setup(struct module *mod)
692 mod->refcnt_param.name = "refcnt";
693 mod->refcnt_param.perm = 0444;
694 mod->refcnt_param.get = refcnt_get_fn;
696 return add_attribute(mod, &mod->refcnt_param);
699 #else /* !CONFIG_MODULE_UNLOAD */
700 static void print_unload_info(struct seq_file *m, struct module *mod)
702 /* We don't know the usage count, or what modules are using. */
703 seq_printf(m, " - -");
706 static inline void module_unload_free(struct module *mod)
710 static inline int use_module(struct module *a, struct module *b)
712 return strong_try_module_get(b);
715 static inline void module_unload_init(struct module *mod)
720 sys_delete_module(const char *name_user, unsigned int flags)
725 static inline int sysfs_unload_setup(struct module *mod)
729 #endif /* CONFIG_MODULE_UNLOAD */
731 #ifdef CONFIG_OBSOLETE_MODPARM
732 static int param_set_byte(const char *val, struct kernel_param *kp)
737 if (!val) return -EINVAL;
738 l = simple_strtol(val, &endp, 0);
739 if (endp == val || *endp || ((char)l != l))
741 *((char *)kp->arg) = l;
745 /* Bounds checking done below */
746 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
748 strcpy(kp->arg, val);
752 int set_obsolete(const char *val, struct kernel_param *kp)
754 unsigned int min, max;
755 unsigned int size, maxsize;
759 struct obsolete_modparm *obsparm = kp->arg;
762 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
766 /* type is: [min[-max]]{b,h,i,l,s} */
768 min = simple_strtol(p, &endp, 10);
769 if (endp == obsparm->type)
771 else if (*endp == '-') {
773 max = simple_strtol(p, &endp, 10);
778 return param_array(kp->name, val, min, max, obsparm->addr,
779 1, param_set_byte, &dummy);
781 return param_array(kp->name, val, min, max, obsparm->addr,
782 sizeof(short), param_set_short, &dummy);
784 return param_array(kp->name, val, min, max, obsparm->addr,
785 sizeof(int), param_set_int, &dummy);
787 return param_array(kp->name, val, min, max, obsparm->addr,
788 sizeof(long), param_set_long, &dummy);
790 return param_array(kp->name, val, min, max, obsparm->addr,
791 sizeof(char *), param_set_charp, &dummy);
794 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
795 and the decl is "char xxx[5][50];" */
797 maxsize = simple_strtol(p, &endp, 10);
798 /* We check lengths here (yes, this is a hack). */
800 while (p[size = strcspn(p, ",")]) {
807 return param_array(kp->name, val, min, max, obsparm->addr,
808 maxsize, obsparm_copy_string, &dummy);
810 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
814 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
818 static int obsolete_params(const char *name,
820 struct obsolete_modparm obsparm[],
823 unsigned int symindex,
826 struct kernel_param *kp;
830 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
834 for (i = 0; i < num; i++) {
835 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
837 snprintf(sym_name, sizeof(sym_name), "%s%s",
838 MODULE_SYMBOL_PREFIX, obsparm[i].name);
840 kp[i].name = obsparm[i].name;
842 kp[i].set = set_obsolete;
845 = (void *)find_local_symbol(sechdrs, symindex, strtab,
847 if (!obsparm[i].addr) {
848 printk("%s: falsely claims to have parameter %s\n",
849 name, obsparm[i].name);
853 kp[i].arg = &obsparm[i];
856 ret = parse_args(name, args, kp, num, NULL);
862 static int obsolete_params(const char *name,
864 struct obsolete_modparm obsparm[],
867 unsigned int symindex,
871 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
875 #endif /* CONFIG_OBSOLETE_MODPARM */
877 static const char vermagic[] = VERMAGIC_STRING;
879 #ifdef CONFIG_MODVERSIONS
880 static int check_version(Elf_Shdr *sechdrs,
881 unsigned int versindex,
884 const unsigned long *crc)
886 unsigned int i, num_versions;
887 struct modversion_info *versions;
889 /* Exporting module didn't supply crcs? OK, we're already tainted. */
893 versions = (void *) sechdrs[versindex].sh_addr;
894 num_versions = sechdrs[versindex].sh_size
895 / sizeof(struct modversion_info);
897 for (i = 0; i < num_versions; i++) {
898 if (strcmp(versions[i].name, symname) != 0)
901 if (versions[i].crc == *crc)
903 printk("%s: disagrees about version of symbol %s\n",
905 DEBUGP("Found checksum %lX vs module %lX\n",
906 *crc, versions[i].crc);
909 /* Not in module's version table. OK, but that taints the kernel. */
910 if (!(tainted & TAINT_FORCED_MODULE)) {
911 printk("%s: no version for \"%s\" found: kernel tainted.\n",
913 tainted |= TAINT_FORCED_MODULE;
918 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
919 unsigned int versindex,
922 const unsigned long *crc;
923 struct module *owner;
925 if (!__find_symbol("struct_module", &owner, &crc, 1))
927 return check_version(sechdrs, versindex, "struct_module", mod,
931 /* First part is kernel version, which we ignore. */
932 static inline int same_magic(const char *amagic, const char *bmagic)
934 amagic += strcspn(amagic, " ");
935 bmagic += strcspn(bmagic, " ");
936 return strcmp(amagic, bmagic) == 0;
939 static inline int check_version(Elf_Shdr *sechdrs,
940 unsigned int versindex,
943 const unsigned long *crc)
948 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
949 unsigned int versindex,
955 static inline int same_magic(const char *amagic, const char *bmagic)
957 return strcmp(amagic, bmagic) == 0;
959 #endif /* CONFIG_MODVERSIONS */
961 /* Resolve a symbol for this module. I.e. if we find one, record usage.
962 Must be holding module_mutex. */
963 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
964 unsigned int versindex,
968 struct module *owner;
970 const unsigned long *crc;
972 spin_lock_irq(&modlist_lock);
973 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
975 /* use_module can fail due to OOM, or module unloading */
976 if (!check_version(sechdrs, versindex, name, mod, crc) ||
977 !use_module(mod, owner))
980 spin_unlock_irq(&modlist_lock);
984 #define to_module_attr(n) container_of(n, struct module_attribute, attr);
986 static ssize_t module_attr_show(struct kobject *kobj,
987 struct attribute *attr,
991 struct module_attribute *attribute = to_module_attr(attr);
993 if (!attribute->param->get)
996 count = attribute->param->get(buf, attribute->param);
1004 /* sysfs always hands a nul-terminated string in buf. We rely on that. */
1005 static ssize_t module_attr_store(struct kobject *kobj,
1006 struct attribute *attr,
1007 const char *buf, size_t len)
1010 struct module_attribute *attribute = to_module_attr(attr);
1012 if (!attribute->param->set)
1015 err = attribute->param->set(buf, attribute->param);
1021 static struct sysfs_ops module_sysfs_ops = {
1022 .show = module_attr_show,
1023 .store = module_attr_store,
1026 static void module_kobj_release(struct kobject *kobj)
1028 kfree(container_of(kobj, struct module_kobject, kobj));
1031 static struct kobj_type module_ktype = {
1032 .sysfs_ops = &module_sysfs_ops,
1033 .release = &module_kobj_release,
1035 static decl_subsys(module, &module_ktype, NULL);
1037 static int mod_sysfs_setup(struct module *mod,
1038 struct kernel_param *kparam,
1039 unsigned int num_params)
1044 /* We overallocate: not every param is in sysfs, and maybe no refcnt */
1045 mod->mkobj = kmalloc(sizeof(*mod->mkobj)
1046 + sizeof(mod->mkobj->attr[0]) * (num_params+1),
1051 memset(&mod->mkobj->kobj, 0, sizeof(mod->mkobj->kobj));
1052 err = kobject_set_name(&mod->mkobj->kobj, mod->name);
1055 kobj_set_kset_s(mod->mkobj, module_subsys);
1056 err = kobject_register(&mod->mkobj->kobj);
1060 mod->mkobj->num_attributes = 0;
1062 for (i = 0; i < num_params; i++) {
1063 if (kparam[i].perm) {
1064 err = add_attribute(mod, &kparam[i]);
1069 err = sysfs_unload_setup(mod);
1075 for (i = 0; i < mod->mkobj->num_attributes; i++)
1076 sysfs_remove_file(&mod->mkobj->kobj,&mod->mkobj->attr[i].attr);
1077 /* Calls module_kobj_release */
1078 kobject_unregister(&mod->mkobj->kobj);
1085 static void mod_kobject_remove(struct module *mod)
1088 for (i = 0; i < mod->mkobj->num_attributes; i++)
1089 sysfs_remove_file(&mod->mkobj->kobj,&mod->mkobj->attr[i].attr);
1090 /* Calls module_kobj_release */
1091 kobject_unregister(&mod->mkobj->kobj);
1094 /* Free a module, remove from lists, etc (must hold module mutex). */
1095 static void free_module(struct module *mod)
1097 /* Delete from various lists */
1098 spin_lock_irq(&modlist_lock);
1099 list_del(&mod->list);
1100 spin_unlock_irq(&modlist_lock);
1102 mod_kobject_remove(mod);
1104 /* Arch-specific cleanup. */
1105 module_arch_cleanup(mod);
1107 /* Module unload stuff */
1108 module_unload_free(mod);
1110 /* This may be NULL, but that's OK */
1111 module_free(mod, mod->module_init);
1114 percpu_modfree(mod->percpu);
1116 /* Finally, free the core (containing the module structure) */
1117 module_free(mod, mod->module_core);
1120 void *__symbol_get(const char *symbol)
1122 struct module *owner;
1123 unsigned long value, flags;
1124 const unsigned long *crc;
1126 spin_lock_irqsave(&modlist_lock, flags);
1127 value = __find_symbol(symbol, &owner, &crc, 1);
1128 if (value && !strong_try_module_get(owner))
1130 spin_unlock_irqrestore(&modlist_lock, flags);
1132 return (void *)value;
1134 EXPORT_SYMBOL_GPL(__symbol_get);
1136 /* Change all symbols so that sh_value encodes the pointer directly. */
1137 static int simplify_symbols(Elf_Shdr *sechdrs,
1138 unsigned int symindex,
1140 unsigned int versindex,
1141 unsigned int pcpuindex,
1144 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1145 unsigned long secbase;
1146 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1149 for (i = 1; i < n; i++) {
1150 switch (sym[i].st_shndx) {
1152 /* We compiled with -fno-common. These are not
1153 supposed to happen. */
1154 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1155 printk("%s: please compile with -fno-common\n",
1161 /* Don't need to do anything */
1162 DEBUGP("Absolute symbol: 0x%08lx\n",
1163 (long)sym[i].st_value);
1168 = resolve_symbol(sechdrs, versindex,
1169 strtab + sym[i].st_name, mod);
1171 /* Ok if resolved. */
1172 if (sym[i].st_value != 0)
1175 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1178 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1179 mod->name, strtab + sym[i].st_name);
1184 /* Divert to percpu allocation if a percpu var. */
1185 if (sym[i].st_shndx == pcpuindex)
1186 secbase = (unsigned long)mod->percpu;
1188 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1189 sym[i].st_value += secbase;
1197 /* Update size with this section: return offset. */
1198 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1202 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1203 *size = ret + sechdr->sh_size;
1207 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1208 might -- code, read-only data, read-write data, small data. Tally
1209 sizes, and place the offsets into sh_entsize fields: high bit means it
1211 static void layout_sections(struct module *mod,
1212 const Elf_Ehdr *hdr,
1214 const char *secstrings)
1216 static unsigned long const masks[][2] = {
1217 /* NOTE: all executable code must be the first section
1218 * in this array; otherwise modify the text_size
1219 * finder in the two loops below */
1220 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1221 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1222 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1223 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1227 for (i = 0; i < hdr->e_shnum; i++)
1228 sechdrs[i].sh_entsize = ~0UL;
1230 DEBUGP("Core section allocation order:\n");
1231 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1232 for (i = 0; i < hdr->e_shnum; ++i) {
1233 Elf_Shdr *s = &sechdrs[i];
1235 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1236 || (s->sh_flags & masks[m][1])
1237 || s->sh_entsize != ~0UL
1238 || strncmp(secstrings + s->sh_name,
1241 s->sh_entsize = get_offset(&mod->core_size, s);
1242 DEBUGP("\t%s\n", secstrings + s->sh_name);
1245 mod->core_text_size = mod->core_size;
1248 DEBUGP("Init section allocation order:\n");
1249 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1250 for (i = 0; i < hdr->e_shnum; ++i) {
1251 Elf_Shdr *s = &sechdrs[i];
1253 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1254 || (s->sh_flags & masks[m][1])
1255 || s->sh_entsize != ~0UL
1256 || strncmp(secstrings + s->sh_name,
1259 s->sh_entsize = (get_offset(&mod->init_size, s)
1260 | INIT_OFFSET_MASK);
1261 DEBUGP("\t%s\n", secstrings + s->sh_name);
1264 mod->init_text_size = mod->init_size;
1268 static inline int license_is_gpl_compatible(const char *license)
1270 return (strcmp(license, "GPL") == 0
1271 || strcmp(license, "GPL v2") == 0
1272 || strcmp(license, "GPL and additional rights") == 0
1273 || strcmp(license, "Dual BSD/GPL") == 0
1274 || strcmp(license, "Dual MPL/GPL") == 0);
1277 static void set_license(struct module *mod, const char *license)
1280 license = "unspecified";
1282 mod->license_gplok = license_is_gpl_compatible(license);
1283 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1284 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1285 mod->name, license);
1286 tainted |= TAINT_PROPRIETARY_MODULE;
1290 /* Parse tag=value strings from .modinfo section */
1291 static char *next_string(char *string, unsigned long *secsize)
1293 /* Skip non-zero chars */
1296 if ((*secsize)-- <= 1)
1300 /* Skip any zero padding. */
1301 while (!string[0]) {
1303 if ((*secsize)-- <= 1)
1309 static char *get_modinfo(Elf_Shdr *sechdrs,
1314 unsigned int taglen = strlen(tag);
1315 unsigned long size = sechdrs[info].sh_size;
1317 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1318 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1319 return p + taglen + 1;
1324 #ifdef CONFIG_KALLSYMS
1325 int is_exported(const char *name, const struct module *mod)
1330 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1331 if (strcmp(__start___ksymtab[i].name, name) == 0)
1335 for (i = 0; i < mod->num_syms; i++)
1336 if (strcmp(mod->syms[i].name, name) == 0)
1342 static char elf_type(const Elf_Sym *sym,
1344 const char *secstrings,
1347 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1348 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1353 if (sym->st_shndx == SHN_UNDEF)
1355 if (sym->st_shndx == SHN_ABS)
1357 if (sym->st_shndx >= SHN_LORESERVE)
1359 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1361 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1362 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1363 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1365 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1370 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1371 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1376 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1377 ".debug", strlen(".debug")) == 0)
1382 static void add_kallsyms(struct module *mod,
1384 unsigned int symindex,
1385 unsigned int strindex,
1386 const char *secstrings)
1390 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1391 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1392 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1394 /* Set types up while we still have access to sections. */
1395 for (i = 0; i < mod->num_symtab; i++)
1396 mod->symtab[i].st_info
1397 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1400 static inline void add_kallsyms(struct module *mod,
1402 unsigned int symindex,
1403 unsigned int strindex,
1404 const char *secstrings)
1407 #endif /* CONFIG_KALLSYMS */
1409 /* Allocate and load the module: note that size of section 0 is always
1410 zero, and we rely on this for optional sections. */
1411 static struct module *load_module(void __user *umod,
1413 const char __user *uargs)
1417 char *secstrings, *args, *modmagic, *strtab = NULL;
1418 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1419 exportindex, modindex, obsparmindex, infoindex, gplindex,
1420 crcindex, gplcrcindex, versindex, pcpuindex;
1424 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1425 struct exception_table_entry *extable;
1427 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1429 if (len < sizeof(*hdr))
1430 return ERR_PTR(-ENOEXEC);
1432 /* Suck in entire file: we'll want most of it. */
1433 /* vmalloc barfs on "unusual" numbers. Check here */
1434 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1435 return ERR_PTR(-ENOMEM);
1436 if (copy_from_user(hdr, umod, len) != 0) {
1441 /* Sanity checks against insmoding binaries or wrong arch,
1442 weird elf version */
1443 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1444 || hdr->e_type != ET_REL
1445 || !elf_check_arch(hdr)
1446 || hdr->e_shentsize != sizeof(*sechdrs)) {
1451 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1454 /* Convenience variables */
1455 sechdrs = (void *)hdr + hdr->e_shoff;
1456 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1457 sechdrs[0].sh_addr = 0;
1459 /* And these should exist, but gcc whinges if we don't init them */
1460 symindex = strindex = 0;
1462 for (i = 1; i < hdr->e_shnum; i++) {
1463 if (sechdrs[i].sh_type != SHT_NOBITS
1464 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1467 /* Mark all sections sh_addr with their address in the
1469 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1471 /* Internal symbols and strings. */
1472 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1474 strindex = sechdrs[i].sh_link;
1475 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1477 #ifndef CONFIG_MODULE_UNLOAD
1478 /* Don't load .exit sections */
1479 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1480 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1484 modindex = find_sec(hdr, sechdrs, secstrings,
1485 ".gnu.linkonce.this_module");
1487 printk(KERN_WARNING "No module found in object\n");
1491 mod = (void *)sechdrs[modindex].sh_addr;
1493 /* Optional sections */
1494 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1495 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1496 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1497 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1498 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1499 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1500 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1501 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1502 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1503 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1505 /* Don't keep modinfo section */
1506 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1507 #ifdef CONFIG_KALLSYMS
1508 /* Keep symbol and string tables for decoding later. */
1509 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1510 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1513 /* Check module struct version now, before we try to use module. */
1514 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1519 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1520 /* This is allowed: modprobe --force will invalidate it. */
1522 tainted |= TAINT_FORCED_MODULE;
1523 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1525 } else if (!same_magic(modmagic, vermagic)) {
1526 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1527 mod->name, modmagic, vermagic);
1532 /* Now copy in args */
1533 arglen = strlen_user(uargs);
1538 args = kmalloc(arglen, GFP_KERNEL);
1543 if (copy_from_user(args, uargs, arglen) != 0) {
1548 if (find_module(mod->name)) {
1553 mod->state = MODULE_STATE_COMING;
1555 /* Allow arches to frob section contents and sizes. */
1556 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1561 /* We have a special allocation for this section. */
1562 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1563 sechdrs[pcpuindex].sh_addralign);
1568 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1569 mod->percpu = percpu;
1572 /* Determine total sizes, and put offsets in sh_entsize. For now
1573 this is done generically; there doesn't appear to be any
1574 special cases for the architectures. */
1575 layout_sections(mod, hdr, sechdrs, secstrings);
1577 /* Do the allocs. */
1578 ptr = module_alloc(mod->core_size);
1583 memset(ptr, 0, mod->core_size);
1584 mod->module_core = ptr;
1586 ptr = module_alloc(mod->init_size);
1587 if (!ptr && mod->init_size) {
1591 memset(ptr, 0, mod->init_size);
1592 mod->module_init = ptr;
1594 /* Transfer each section which specifies SHF_ALLOC */
1595 DEBUGP("final section addresses:\n");
1596 for (i = 0; i < hdr->e_shnum; i++) {
1599 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1602 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1603 dest = mod->module_init
1604 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1606 dest = mod->module_core + sechdrs[i].sh_entsize;
1608 if (sechdrs[i].sh_type != SHT_NOBITS)
1609 memcpy(dest, (void *)sechdrs[i].sh_addr,
1610 sechdrs[i].sh_size);
1611 /* Update sh_addr to point to copy in image. */
1612 sechdrs[i].sh_addr = (unsigned long)dest;
1613 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1615 /* Module has been moved. */
1616 mod = (void *)sechdrs[modindex].sh_addr;
1618 /* Now we've moved module, initialize linked lists, etc. */
1619 module_unload_init(mod);
1621 /* Set up license info based on the info section */
1622 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1624 /* Fix up syms, so that st_value is a pointer to location. */
1625 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1630 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1631 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1632 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1634 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1635 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1636 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1638 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1640 #ifdef CONFIG_MODVERSIONS
1641 if ((mod->num_syms && !crcindex) ||
1642 (mod->num_gpl_syms && !gplcrcindex)) {
1643 printk(KERN_WARNING "%s: No versions for exported symbols."
1644 " Tainting kernel.\n", mod->name);
1645 tainted |= TAINT_FORCED_MODULE;
1649 /* Now do relocations. */
1650 for (i = 1; i < hdr->e_shnum; i++) {
1651 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1652 unsigned int info = sechdrs[i].sh_info;
1654 /* Not a valid relocation section? */
1655 if (info >= hdr->e_shnum)
1658 /* Don't bother with non-allocated sections */
1659 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1662 if (sechdrs[i].sh_type == SHT_REL)
1663 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1664 else if (sechdrs[i].sh_type == SHT_RELA)
1665 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1671 /* Set up and sort exception table */
1672 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1673 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1674 sort_extable(extable, extable + mod->num_exentries);
1676 /* Finally, copy percpu area over. */
1677 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1678 sechdrs[pcpuindex].sh_size);
1680 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1682 err = module_finalize(hdr, sechdrs, mod);
1688 err = obsolete_params(mod->name, mod->args,
1689 (struct obsolete_modparm *)
1690 sechdrs[obsparmindex].sh_addr,
1691 sechdrs[obsparmindex].sh_size
1692 / sizeof(struct obsolete_modparm),
1694 (char *)sechdrs[strindex].sh_addr);
1696 printk(KERN_WARNING "%s: Ignoring new-style "
1697 "parameters in presence of obsolete ones\n",
1700 /* Size of section 0 is 0, so this works well if no params */
1701 err = parse_args(mod->name, mod->args,
1702 (struct kernel_param *)
1703 sechdrs[setupindex].sh_addr,
1704 sechdrs[setupindex].sh_size
1705 / sizeof(struct kernel_param),
1708 err = mod_sysfs_setup(mod,
1709 (struct kernel_param *)
1710 sechdrs[setupindex].sh_addr,
1711 sechdrs[setupindex].sh_size
1712 / sizeof(struct kernel_param));
1716 /* Get rid of temporary copy */
1723 module_arch_cleanup(mod);
1725 module_unload_free(mod);
1726 module_free(mod, mod->module_init);
1728 module_free(mod, mod->module_core);
1731 percpu_modfree(percpu);
1736 if (err < 0) return ERR_PTR(err);
1740 printk(KERN_ERR "Module len %lu truncated\n", len);
1745 /* This is where the real work happens */
1747 sys_init_module(void __user *umod,
1749 const char __user *uargs)
1754 /* Must have permission */
1755 if (!capable(CAP_SYS_MODULE))
1758 /* Only one module load at a time, please */
1759 if (down_interruptible(&module_mutex) != 0)
1762 /* Do all the hard work */
1763 mod = load_module(umod, len, uargs);
1766 return PTR_ERR(mod);
1769 /* Flush the instruction cache, since we've played with text */
1770 if (mod->module_init)
1771 flush_icache_range((unsigned long)mod->module_init,
1772 (unsigned long)mod->module_init
1774 flush_icache_range((unsigned long)mod->module_core,
1775 (unsigned long)mod->module_core + mod->core_size);
1777 /* Now sew it into the lists. They won't access us, since
1778 strong_try_module_get() will fail. */
1779 spin_lock_irq(&modlist_lock);
1780 list_add(&mod->list, &modules);
1781 spin_unlock_irq(&modlist_lock);
1783 /* Drop lock so they can recurse */
1786 down(¬ify_mutex);
1787 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1790 /* Start the module */
1793 /* Init routine failed: abort. Try to protect us from
1794 buggy refcounters. */
1795 mod->state = MODULE_STATE_GOING;
1796 synchronize_kernel();
1798 printk(KERN_ERR "%s: module is now stuck!\n",
1802 down(&module_mutex);
1809 /* Now it's a first class citizen! */
1810 down(&module_mutex);
1811 mod->state = MODULE_STATE_LIVE;
1812 /* Drop initial reference. */
1814 module_free(mod, mod->module_init);
1815 mod->module_init = NULL;
1817 mod->init_text_size = 0;
1823 static inline int within(unsigned long addr, void *start, unsigned long size)
1825 return ((void *)addr >= start && (void *)addr < start + size);
1828 #ifdef CONFIG_KALLSYMS
1829 static const char *get_ksymbol(struct module *mod,
1831 unsigned long *size,
1832 unsigned long *offset)
1834 unsigned int i, best = 0;
1835 unsigned long nextval;
1837 /* At worse, next value is at end of module */
1838 if (within(addr, mod->module_init, mod->init_size))
1839 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1841 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1843 /* Scan for closest preceeding symbol, and next symbol. (ELF
1844 starts real symbols at 1). */
1845 for (i = 1; i < mod->num_symtab; i++) {
1846 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1849 /* We ignore unnamed symbols: they're uninformative
1850 * and inserted at a whim. */
1851 if (mod->symtab[i].st_value <= addr
1852 && mod->symtab[i].st_value > mod->symtab[best].st_value
1853 && *(mod->strtab + mod->symtab[i].st_name) != '\0' )
1855 if (mod->symtab[i].st_value > addr
1856 && mod->symtab[i].st_value < nextval
1857 && *(mod->strtab + mod->symtab[i].st_name) != '\0')
1858 nextval = mod->symtab[i].st_value;
1864 *size = nextval - mod->symtab[best].st_value;
1865 *offset = addr - mod->symtab[best].st_value;
1866 return mod->strtab + mod->symtab[best].st_name;
1869 /* For kallsyms to ask for address resolution. NULL means not found.
1870 We don't lock, as this is used for oops resolution and races are a
1872 const char *module_address_lookup(unsigned long addr,
1873 unsigned long *size,
1874 unsigned long *offset,
1879 list_for_each_entry(mod, &modules, list) {
1880 if (within(addr, mod->module_init, mod->init_size)
1881 || within(addr, mod->module_core, mod->core_size)) {
1882 *modname = mod->name;
1883 return get_ksymbol(mod, addr, size, offset);
1889 struct module *module_get_kallsym(unsigned int symnum,
1890 unsigned long *value,
1896 down(&module_mutex);
1897 list_for_each_entry(mod, &modules, list) {
1898 if (symnum < mod->num_symtab) {
1899 *value = mod->symtab[symnum].st_value;
1900 *type = mod->symtab[symnum].st_info;
1902 mod->strtab + mod->symtab[symnum].st_name,
1907 symnum -= mod->num_symtab;
1913 static unsigned long mod_find_symname(struct module *mod, const char *name)
1917 for (i = 0; i < mod->num_symtab; i++)
1918 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
1919 return mod->symtab[i].st_value;
1923 /* Look for this name: can be of form module:name. */
1924 unsigned long module_kallsyms_lookup_name(const char *name)
1928 unsigned long ret = 0;
1930 /* Don't lock: we're in enough trouble already. */
1931 if ((colon = strchr(name, ':')) != NULL) {
1933 if ((mod = find_module(name)) != NULL)
1934 ret = mod_find_symname(mod, colon+1);
1937 list_for_each_entry(mod, &modules, list)
1938 if ((ret = mod_find_symname(mod, name)) != 0)
1943 #endif /* CONFIG_KALLSYMS */
1945 /* Called by the /proc file system to return a list of modules. */
1946 static void *m_start(struct seq_file *m, loff_t *pos)
1948 struct list_head *i;
1951 down(&module_mutex);
1952 list_for_each(i, &modules) {
1961 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
1963 struct list_head *i = p;
1965 if (i->next == &modules)
1970 static void m_stop(struct seq_file *m, void *p)
1975 static int m_show(struct seq_file *m, void *p)
1977 struct module *mod = list_entry(p, struct module, list);
1978 seq_printf(m, "%s %lu",
1979 mod->name, mod->init_size + mod->core_size);
1980 print_unload_info(m, mod);
1982 /* Informative for users. */
1983 seq_printf(m, " %s",
1984 mod->state == MODULE_STATE_GOING ? "Unloading":
1985 mod->state == MODULE_STATE_COMING ? "Loading":
1987 /* Used by oprofile and other similar tools. */
1988 seq_printf(m, " 0x%p", mod->module_core);
1990 seq_printf(m, "\n");
1994 /* Format: modulename size refcount deps address
1996 Where refcount is a number or -, and deps is a comma-separated list
1999 struct seq_operations modules_op = {
2006 /* Given an address, look for it in the module exception tables. */
2007 const struct exception_table_entry *search_module_extables(unsigned long addr)
2009 unsigned long flags;
2010 const struct exception_table_entry *e = NULL;
2013 spin_lock_irqsave(&modlist_lock, flags);
2014 list_for_each_entry(mod, &modules, list) {
2015 if (mod->num_exentries == 0)
2018 e = search_extable(mod->extable,
2019 mod->extable + mod->num_exentries - 1,
2024 spin_unlock_irqrestore(&modlist_lock, flags);
2026 /* Now, if we found one, we are running inside it now, hence
2027 we cannot unload the module, hence no refcnt needed. */
2031 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2032 struct module *module_text_address(unsigned long addr)
2036 list_for_each_entry(mod, &modules, list)
2037 if (within(addr, mod->module_init, mod->init_text_size)
2038 || within(addr, mod->module_core, mod->core_text_size))
2043 /* Don't grab lock, we're oopsing. */
2044 void print_modules(void)
2048 printk("Modules linked in:");
2049 list_for_each_entry(mod, &modules, list)
2050 printk(" %s", mod->name);
2054 #ifdef CONFIG_MODVERSIONS
2055 /* Generate the signature for struct module here, too, for modversions. */
2056 void struct_module(struct module *mod) { return; }
2057 EXPORT_SYMBOL(struct_module);
2060 static int __init modules_init(void)
2062 return subsystem_register(&module_subsys);
2064 __initcall(modules_init);