1 /* Rewritten by Rusty Russell, on the backs of many others...
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/elf.h>
26 #include <linux/seq_file.h>
27 #include <linux/syscalls.h>
28 #include <linux/fcntl.h>
29 #include <linux/rcupdate.h>
30 #include <linux/cpu.h>
31 #include <linux/moduleparam.h>
32 #include <linux/errno.h>
33 #include <linux/err.h>
34 #include <linux/vermagic.h>
35 #include <linux/notifier.h>
36 #include <linux/stop_machine.h>
37 #include <linux/device.h>
38 #include <linux/string.h>
39 #include <asm/uaccess.h>
40 #include <asm/semaphore.h>
41 #include <asm/cacheflush.h>
46 #define DEBUGP(fmt , a...)
49 #ifndef ARCH_SHF_SMALL
50 #define ARCH_SHF_SMALL 0
53 /* If this is set, the section belongs in the init part of the module */
54 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
56 /* Protects module list */
57 static DEFINE_SPINLOCK(modlist_lock);
59 /* List of modules, protected by module_mutex AND modlist_lock */
60 static DECLARE_MUTEX(module_mutex);
61 static LIST_HEAD(modules);
63 static DECLARE_MUTEX(notify_mutex);
64 static struct notifier_block * module_notify_list;
66 int register_module_notifier(struct notifier_block * nb)
70 err = notifier_chain_register(&module_notify_list, nb);
74 EXPORT_SYMBOL(register_module_notifier);
76 int unregister_module_notifier(struct notifier_block * nb)
80 err = notifier_chain_unregister(&module_notify_list, nb);
84 EXPORT_SYMBOL(unregister_module_notifier);
86 /* We require a truly strong try_module_get() */
87 static inline int strong_try_module_get(struct module *mod)
89 if (mod && mod->state == MODULE_STATE_COMING)
91 return try_module_get(mod);
94 /* A thread that wants to hold a reference to a module only while it
95 * is running can call ths to safely exit.
96 * nfsd and lockd use this.
98 void __module_put_and_exit(struct module *mod, long code)
103 EXPORT_SYMBOL(__module_put_and_exit);
105 /* Find a module section: 0 means not found. */
106 static unsigned int find_sec(Elf_Ehdr *hdr,
108 const char *secstrings,
113 for (i = 1; i < hdr->e_shnum; i++)
114 /* Alloc bit cleared means "ignore it." */
115 if ((sechdrs[i].sh_flags & SHF_ALLOC)
116 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
121 /* Provided by the linker */
122 extern const struct kernel_symbol __start___ksymtab[];
123 extern const struct kernel_symbol __stop___ksymtab[];
124 extern const struct kernel_symbol __start___ksymtab_gpl[];
125 extern const struct kernel_symbol __stop___ksymtab_gpl[];
126 extern const unsigned long __start___kcrctab[];
127 extern const unsigned long __start___kcrctab_gpl[];
129 #ifndef CONFIG_MODVERSIONS
130 #define symversion(base, idx) NULL
132 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
135 /* Find a symbol, return value, crc and module which owns it */
136 static unsigned long __find_symbol(const char *name,
137 struct module **owner,
138 const unsigned long **crc,
144 /* Core kernel first. */
146 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
147 if (strcmp(__start___ksymtab[i].name, name) == 0) {
148 *crc = symversion(__start___kcrctab, i);
149 return __start___ksymtab[i].value;
153 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
154 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
155 *crc = symversion(__start___kcrctab_gpl, i);
156 return __start___ksymtab_gpl[i].value;
160 /* Now try modules. */
161 list_for_each_entry(mod, &modules, list) {
163 for (i = 0; i < mod->num_syms; i++)
164 if (strcmp(mod->syms[i].name, name) == 0) {
165 *crc = symversion(mod->crcs, i);
166 return mod->syms[i].value;
170 for (i = 0; i < mod->num_gpl_syms; i++) {
171 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
172 *crc = symversion(mod->gpl_crcs, i);
173 return mod->gpl_syms[i].value;
178 DEBUGP("Failed to find symbol %s\n", name);
182 /* Find a symbol in this elf symbol table */
183 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
184 unsigned int symindex,
189 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
191 /* Search (defined) internal symbols first. */
192 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
193 if (sym[i].st_shndx != SHN_UNDEF
194 && strcmp(name, strtab + sym[i].st_name) == 0)
195 return sym[i].st_value;
200 /* Search for module by name: must hold module_mutex. */
201 static struct module *find_module(const char *name)
205 list_for_each_entry(mod, &modules, list) {
206 if (strcmp(mod->name, name) == 0)
213 /* Number of blocks used and allocated. */
214 static unsigned int pcpu_num_used, pcpu_num_allocated;
215 /* Size of each block. -ve means used. */
216 static int *pcpu_size;
218 static int split_block(unsigned int i, unsigned short size)
220 /* Reallocation required? */
221 if (pcpu_num_used + 1 > pcpu_num_allocated) {
222 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
227 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
228 pcpu_num_allocated *= 2;
233 /* Insert a new subblock */
234 memmove(&pcpu_size[i+1], &pcpu_size[i],
235 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
238 pcpu_size[i+1] -= size;
243 static inline unsigned int block_size(int val)
250 /* Created by linker magic */
251 extern char __per_cpu_start[], __per_cpu_end[];
253 static void *percpu_modalloc(unsigned long size, unsigned long align)
259 BUG_ON(align > SMP_CACHE_BYTES);
261 ptr = __per_cpu_start;
262 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
263 /* Extra for alignment requirement. */
264 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
265 BUG_ON(i == 0 && extra != 0);
267 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
270 /* Transfer extra to previous block. */
271 if (pcpu_size[i-1] < 0)
272 pcpu_size[i-1] -= extra;
274 pcpu_size[i-1] += extra;
275 pcpu_size[i] -= extra;
278 /* Split block if warranted */
279 if (pcpu_size[i] - size > sizeof(unsigned long))
280 if (!split_block(i, size))
284 pcpu_size[i] = -pcpu_size[i];
288 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
293 static void percpu_modfree(void *freeme)
296 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
298 /* First entry is core kernel percpu data. */
299 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
301 pcpu_size[i] = -pcpu_size[i];
308 /* Merge with previous? */
309 if (pcpu_size[i-1] >= 0) {
310 pcpu_size[i-1] += pcpu_size[i];
312 memmove(&pcpu_size[i], &pcpu_size[i+1],
313 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
316 /* Merge with next? */
317 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
318 pcpu_size[i] += pcpu_size[i+1];
320 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
321 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
325 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
327 const char *secstrings)
329 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
332 static int percpu_modinit(void)
335 pcpu_num_allocated = 2;
336 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
338 /* Static in-kernel percpu data (used). */
339 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
341 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
342 if (pcpu_size[1] < 0) {
343 printk(KERN_ERR "No per-cpu room for modules.\n");
349 __initcall(percpu_modinit);
350 #else /* ... !CONFIG_SMP */
351 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
355 static inline void percpu_modfree(void *pcpuptr)
359 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
361 const char *secstrings)
365 static inline void percpu_modcopy(void *pcpudst, const void *src,
368 /* pcpusec should be 0, and size of that section should be 0. */
371 #endif /* CONFIG_SMP */
373 #ifdef CONFIG_MODULE_UNLOAD
374 #define MODINFO_ATTR(field) \
375 static void setup_modinfo_##field(struct module *mod, const char *s) \
377 mod->field = kstrdup(s, GFP_KERNEL); \
379 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
380 struct module *mod, char *buffer) \
382 return sprintf(buffer, "%s\n", mod->field); \
384 static int modinfo_##field##_exists(struct module *mod) \
386 return mod->field != NULL; \
388 static void free_modinfo_##field(struct module *mod) \
393 static struct module_attribute modinfo_##field = { \
394 .attr = { .name = __stringify(field), .mode = 0444, \
395 .owner = THIS_MODULE }, \
396 .show = show_modinfo_##field, \
397 .setup = setup_modinfo_##field, \
398 .test = modinfo_##field##_exists, \
399 .free = free_modinfo_##field, \
402 MODINFO_ATTR(version);
403 MODINFO_ATTR(srcversion);
405 static struct module_attribute *modinfo_attrs[] = {
411 /* Init the unload section of the module. */
412 static void module_unload_init(struct module *mod)
416 INIT_LIST_HEAD(&mod->modules_which_use_me);
417 for (i = 0; i < NR_CPUS; i++)
418 local_set(&mod->ref[i].count, 0);
419 /* Hold reference count during initialization. */
420 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
421 /* Backwards compatibility macros put refcount during init. */
422 mod->waiter = current;
425 /* modules using other modules */
428 struct list_head list;
429 struct module *module_which_uses;
432 /* Does a already use b? */
433 static int already_uses(struct module *a, struct module *b)
435 struct module_use *use;
437 list_for_each_entry(use, &b->modules_which_use_me, list) {
438 if (use->module_which_uses == a) {
439 DEBUGP("%s uses %s!\n", a->name, b->name);
443 DEBUGP("%s does not use %s!\n", a->name, b->name);
447 /* Module a uses b */
448 static int use_module(struct module *a, struct module *b)
450 struct module_use *use;
451 if (b == NULL || already_uses(a, b)) return 1;
453 if (!strong_try_module_get(b))
456 DEBUGP("Allocating new usage for %s.\n", a->name);
457 use = kmalloc(sizeof(*use), GFP_ATOMIC);
459 printk("%s: out of memory loading\n", a->name);
464 use->module_which_uses = a;
465 list_add(&use->list, &b->modules_which_use_me);
469 /* Clear the unload stuff of the module. */
470 static void module_unload_free(struct module *mod)
474 list_for_each_entry(i, &modules, list) {
475 struct module_use *use;
477 list_for_each_entry(use, &i->modules_which_use_me, list) {
478 if (use->module_which_uses == mod) {
479 DEBUGP("%s unusing %s\n", mod->name, i->name);
481 list_del(&use->list);
483 /* There can be at most one match. */
490 #ifdef CONFIG_MODULE_FORCE_UNLOAD
491 static inline int try_force(unsigned int flags)
493 int ret = (flags & O_TRUNC);
495 tainted |= TAINT_FORCED_MODULE;
499 static inline int try_force(unsigned int flags)
503 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
512 /* Whole machine is stopped with interrupts off when this runs. */
513 static int __try_stop_module(void *_sref)
515 struct stopref *sref = _sref;
517 /* If it's not unused, quit unless we are told to block. */
518 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
519 if (!(*sref->forced = try_force(sref->flags)))
523 /* Mark it as dying. */
524 sref->mod->state = MODULE_STATE_GOING;
528 static int try_stop_module(struct module *mod, int flags, int *forced)
530 struct stopref sref = { mod, flags, forced };
532 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
535 unsigned int module_refcount(struct module *mod)
537 unsigned int i, total = 0;
539 for (i = 0; i < NR_CPUS; i++)
540 total += local_read(&mod->ref[i].count);
543 EXPORT_SYMBOL(module_refcount);
545 /* This exists whether we can unload or not */
546 static void free_module(struct module *mod);
548 static void wait_for_zero_refcount(struct module *mod)
550 /* Since we might sleep for some time, drop the semaphore first */
553 DEBUGP("Looking at refcount...\n");
554 set_current_state(TASK_UNINTERRUPTIBLE);
555 if (module_refcount(mod) == 0)
559 current->state = TASK_RUNNING;
564 sys_delete_module(const char __user *name_user, unsigned int flags)
567 char name[MODULE_NAME_LEN];
570 if (!capable(CAP_SYS_MODULE))
573 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
575 name[MODULE_NAME_LEN-1] = '\0';
577 if (down_interruptible(&module_mutex) != 0)
580 mod = find_module(name);
586 if (!list_empty(&mod->modules_which_use_me)) {
587 /* Other modules depend on us: get rid of them first. */
592 /* Doing init or already dying? */
593 if (mod->state != MODULE_STATE_LIVE) {
594 /* FIXME: if (force), slam module count and wake up
596 DEBUGP("%s already dying\n", mod->name);
601 /* If it has an init func, it must have an exit func to unload */
602 if ((mod->init != NULL && mod->exit == NULL)
604 forced = try_force(flags);
606 /* This module can't be removed */
612 /* Set this up before setting mod->state */
613 mod->waiter = current;
615 /* Stop the machine so refcounts can't move and disable module. */
616 ret = try_stop_module(mod, flags, &forced);
620 /* Never wait if forced. */
621 if (!forced && module_refcount(mod) != 0)
622 wait_for_zero_refcount(mod);
624 /* Final destruction now noone is using it. */
625 if (mod->exit != NULL) {
637 static void print_unload_info(struct seq_file *m, struct module *mod)
639 struct module_use *use;
640 int printed_something = 0;
642 seq_printf(m, " %u ", module_refcount(mod));
644 /* Always include a trailing , so userspace can differentiate
645 between this and the old multi-field proc format. */
646 list_for_each_entry(use, &mod->modules_which_use_me, list) {
647 printed_something = 1;
648 seq_printf(m, "%s,", use->module_which_uses->name);
652 printed_something = 1;
653 seq_printf(m, "[unsafe],");
656 if (mod->init != NULL && mod->exit == NULL) {
657 printed_something = 1;
658 seq_printf(m, "[permanent],");
661 if (!printed_something)
665 void __symbol_put(const char *symbol)
667 struct module *owner;
669 const unsigned long *crc;
671 spin_lock_irqsave(&modlist_lock, flags);
672 if (!__find_symbol(symbol, &owner, &crc, 1))
675 spin_unlock_irqrestore(&modlist_lock, flags);
677 EXPORT_SYMBOL(__symbol_put);
679 void symbol_put_addr(void *addr)
683 spin_lock_irqsave(&modlist_lock, flags);
684 if (!kernel_text_address((unsigned long)addr))
687 module_put(module_text_address((unsigned long)addr));
688 spin_unlock_irqrestore(&modlist_lock, flags);
690 EXPORT_SYMBOL_GPL(symbol_put_addr);
692 static ssize_t show_refcnt(struct module_attribute *mattr,
693 struct module *mod, char *buffer)
695 /* sysfs holds a reference */
696 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
699 static struct module_attribute refcnt = {
700 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
704 #else /* !CONFIG_MODULE_UNLOAD */
705 static void print_unload_info(struct seq_file *m, struct module *mod)
707 /* We don't know the usage count, or what modules are using. */
708 seq_printf(m, " - -");
711 static inline void module_unload_free(struct module *mod)
715 static inline int use_module(struct module *a, struct module *b)
717 return strong_try_module_get(b);
720 static inline void module_unload_init(struct module *mod)
723 #endif /* CONFIG_MODULE_UNLOAD */
725 #ifdef CONFIG_OBSOLETE_MODPARM
726 /* Bounds checking done below */
727 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
729 strcpy(kp->arg, val);
733 static int set_obsolete(const char *val, struct kernel_param *kp)
735 unsigned int min, max;
736 unsigned int size, maxsize;
740 struct obsolete_modparm *obsparm = kp->arg;
743 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
747 /* type is: [min[-max]]{b,h,i,l,s} */
749 min = simple_strtol(p, &endp, 10);
750 if (endp == obsparm->type)
752 else if (*endp == '-') {
754 max = simple_strtol(p, &endp, 10);
759 return param_array(kp->name, val, min, max, obsparm->addr,
760 1, param_set_byte, &dummy);
762 return param_array(kp->name, val, min, max, obsparm->addr,
763 sizeof(short), param_set_short, &dummy);
765 return param_array(kp->name, val, min, max, obsparm->addr,
766 sizeof(int), param_set_int, &dummy);
768 return param_array(kp->name, val, min, max, obsparm->addr,
769 sizeof(long), param_set_long, &dummy);
771 return param_array(kp->name, val, min, max, obsparm->addr,
772 sizeof(char *), param_set_charp, &dummy);
775 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
776 and the decl is "char xxx[5][50];" */
778 maxsize = simple_strtol(p, &endp, 10);
779 /* We check lengths here (yes, this is a hack). */
781 while (p[size = strcspn(p, ",")]) {
788 return param_array(kp->name, val, min, max, obsparm->addr,
789 maxsize, obsparm_copy_string, &dummy);
791 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
795 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
799 static int obsolete_params(const char *name,
801 struct obsolete_modparm obsparm[],
804 unsigned int symindex,
807 struct kernel_param *kp;
811 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
815 for (i = 0; i < num; i++) {
816 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
818 snprintf(sym_name, sizeof(sym_name), "%s%s",
819 MODULE_SYMBOL_PREFIX, obsparm[i].name);
821 kp[i].name = obsparm[i].name;
823 kp[i].set = set_obsolete;
826 = (void *)find_local_symbol(sechdrs, symindex, strtab,
828 if (!obsparm[i].addr) {
829 printk("%s: falsely claims to have parameter %s\n",
830 name, obsparm[i].name);
834 kp[i].arg = &obsparm[i];
837 ret = parse_args(name, args, kp, num, NULL);
843 static int obsolete_params(const char *name,
845 struct obsolete_modparm obsparm[],
848 unsigned int symindex,
852 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
856 #endif /* CONFIG_OBSOLETE_MODPARM */
858 static const char vermagic[] = VERMAGIC_STRING;
860 #ifdef CONFIG_MODVERSIONS
861 static int check_version(Elf_Shdr *sechdrs,
862 unsigned int versindex,
865 const unsigned long *crc)
867 unsigned int i, num_versions;
868 struct modversion_info *versions;
870 /* Exporting module didn't supply crcs? OK, we're already tainted. */
874 versions = (void *) sechdrs[versindex].sh_addr;
875 num_versions = sechdrs[versindex].sh_size
876 / sizeof(struct modversion_info);
878 for (i = 0; i < num_versions; i++) {
879 if (strcmp(versions[i].name, symname) != 0)
882 if (versions[i].crc == *crc)
884 printk("%s: disagrees about version of symbol %s\n",
886 DEBUGP("Found checksum %lX vs module %lX\n",
887 *crc, versions[i].crc);
890 /* Not in module's version table. OK, but that taints the kernel. */
891 if (!(tainted & TAINT_FORCED_MODULE)) {
892 printk("%s: no version for \"%s\" found: kernel tainted.\n",
894 tainted |= TAINT_FORCED_MODULE;
899 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
900 unsigned int versindex,
903 const unsigned long *crc;
904 struct module *owner;
906 if (!__find_symbol("struct_module", &owner, &crc, 1))
908 return check_version(sechdrs, versindex, "struct_module", mod,
912 /* First part is kernel version, which we ignore. */
913 static inline int same_magic(const char *amagic, const char *bmagic)
915 amagic += strcspn(amagic, " ");
916 bmagic += strcspn(bmagic, " ");
917 return strcmp(amagic, bmagic) == 0;
920 static inline int check_version(Elf_Shdr *sechdrs,
921 unsigned int versindex,
924 const unsigned long *crc)
929 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
930 unsigned int versindex,
936 static inline int same_magic(const char *amagic, const char *bmagic)
938 return strcmp(amagic, bmagic) == 0;
940 #endif /* CONFIG_MODVERSIONS */
942 /* Resolve a symbol for this module. I.e. if we find one, record usage.
943 Must be holding module_mutex. */
944 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
945 unsigned int versindex,
949 struct module *owner;
951 const unsigned long *crc;
953 spin_lock_irq(&modlist_lock);
954 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
956 /* use_module can fail due to OOM, or module unloading */
957 if (!check_version(sechdrs, versindex, name, mod, crc) ||
958 !use_module(mod, owner))
961 spin_unlock_irq(&modlist_lock);
967 * /sys/module/foo/sections stuff
968 * J. Corbet <corbet@lwn.net>
970 #ifdef CONFIG_KALLSYMS
971 static ssize_t module_sect_show(struct module_attribute *mattr,
972 struct module *mod, char *buf)
974 struct module_sect_attr *sattr =
975 container_of(mattr, struct module_sect_attr, mattr);
976 return sprintf(buf, "0x%lx\n", sattr->address);
979 static void add_sect_attrs(struct module *mod, unsigned int nsect,
980 char *secstrings, Elf_Shdr *sechdrs)
982 unsigned int nloaded = 0, i, size[2];
983 struct module_sect_attrs *sect_attrs;
984 struct module_sect_attr *sattr;
985 struct attribute **gattr;
987 /* Count loaded sections and allocate structures */
988 for (i = 0; i < nsect; i++)
989 if (sechdrs[i].sh_flags & SHF_ALLOC)
991 size[0] = ALIGN(sizeof(*sect_attrs)
992 + nloaded * sizeof(sect_attrs->attrs[0]),
993 sizeof(sect_attrs->grp.attrs[0]));
994 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
995 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
998 /* Setup section attributes. */
999 sect_attrs->grp.name = "sections";
1000 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1002 sattr = §_attrs->attrs[0];
1003 gattr = §_attrs->grp.attrs[0];
1004 for (i = 0; i < nsect; i++) {
1005 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1007 sattr->address = sechdrs[i].sh_addr;
1008 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1009 MODULE_SECT_NAME_LEN);
1010 sattr->mattr.show = module_sect_show;
1011 sattr->mattr.store = NULL;
1012 sattr->mattr.attr.name = sattr->name;
1013 sattr->mattr.attr.owner = mod;
1014 sattr->mattr.attr.mode = S_IRUGO;
1015 *(gattr++) = &(sattr++)->mattr.attr;
1019 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
1022 mod->sect_attrs = sect_attrs;
1028 static void remove_sect_attrs(struct module *mod)
1030 if (mod->sect_attrs) {
1031 sysfs_remove_group(&mod->mkobj.kobj,
1032 &mod->sect_attrs->grp);
1033 /* We are positive that no one is using any sect attrs
1034 * at this point. Deallocate immediately. */
1035 kfree(mod->sect_attrs);
1036 mod->sect_attrs = NULL;
1042 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1043 char *sectstrings, Elf_Shdr *sechdrs)
1047 static inline void remove_sect_attrs(struct module *mod)
1050 #endif /* CONFIG_KALLSYMS */
1053 #ifdef CONFIG_MODULE_UNLOAD
1054 static inline int module_add_refcnt_attr(struct module *mod)
1056 return sysfs_create_file(&mod->mkobj.kobj, &refcnt.attr);
1058 static void module_remove_refcnt_attr(struct module *mod)
1060 return sysfs_remove_file(&mod->mkobj.kobj, &refcnt.attr);
1063 static inline int module_add_refcnt_attr(struct module *mod)
1067 static void module_remove_refcnt_attr(struct module *mod)
1072 #ifdef CONFIG_MODULE_UNLOAD
1073 static int module_add_modinfo_attrs(struct module *mod)
1075 struct module_attribute *attr;
1079 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1081 (attr->test && attr->test(mod)))
1082 error = sysfs_create_file(&mod->mkobj.kobj,&attr->attr);
1087 static void module_remove_modinfo_attrs(struct module *mod)
1089 struct module_attribute *attr;
1092 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1093 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1099 static int mod_sysfs_setup(struct module *mod,
1100 struct kernel_param *kparam,
1101 unsigned int num_params)
1105 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1106 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1109 kobj_set_kset_s(&mod->mkobj, module_subsys);
1110 mod->mkobj.mod = mod;
1111 err = kobject_register(&mod->mkobj.kobj);
1115 err = module_add_refcnt_attr(mod);
1119 err = module_param_sysfs_setup(mod, kparam, num_params);
1123 #ifdef CONFIG_MODULE_UNLOAD
1124 err = module_add_modinfo_attrs(mod);
1132 kobject_unregister(&mod->mkobj.kobj);
1137 static void mod_kobject_remove(struct module *mod)
1139 #ifdef CONFIG_MODULE_UNLOAD
1140 module_remove_modinfo_attrs(mod);
1142 module_remove_refcnt_attr(mod);
1143 module_param_sysfs_remove(mod);
1145 kobject_unregister(&mod->mkobj.kobj);
1149 * unlink the module with the whole machine is stopped with interrupts off
1150 * - this defends against kallsyms not taking locks
1152 static int __unlink_module(void *_mod)
1154 struct module *mod = _mod;
1155 list_del(&mod->list);
1159 /* Free a module, remove from lists, etc (must hold module mutex). */
1160 static void free_module(struct module *mod)
1162 /* Delete from various lists */
1163 stop_machine_run(__unlink_module, mod, NR_CPUS);
1164 remove_sect_attrs(mod);
1165 mod_kobject_remove(mod);
1167 /* Arch-specific cleanup. */
1168 module_arch_cleanup(mod);
1170 /* Module unload stuff */
1171 module_unload_free(mod);
1173 /* This may be NULL, but that's OK */
1174 module_free(mod, mod->module_init);
1177 percpu_modfree(mod->percpu);
1179 /* Finally, free the core (containing the module structure) */
1180 module_free(mod, mod->module_core);
1183 void *__symbol_get(const char *symbol)
1185 struct module *owner;
1186 unsigned long value, flags;
1187 const unsigned long *crc;
1189 spin_lock_irqsave(&modlist_lock, flags);
1190 value = __find_symbol(symbol, &owner, &crc, 1);
1191 if (value && !strong_try_module_get(owner))
1193 spin_unlock_irqrestore(&modlist_lock, flags);
1195 return (void *)value;
1197 EXPORT_SYMBOL_GPL(__symbol_get);
1199 /* Change all symbols so that sh_value encodes the pointer directly. */
1200 static int simplify_symbols(Elf_Shdr *sechdrs,
1201 unsigned int symindex,
1203 unsigned int versindex,
1204 unsigned int pcpuindex,
1207 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1208 unsigned long secbase;
1209 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1212 for (i = 1; i < n; i++) {
1213 switch (sym[i].st_shndx) {
1215 /* We compiled with -fno-common. These are not
1216 supposed to happen. */
1217 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1218 printk("%s: please compile with -fno-common\n",
1224 /* Don't need to do anything */
1225 DEBUGP("Absolute symbol: 0x%08lx\n",
1226 (long)sym[i].st_value);
1231 = resolve_symbol(sechdrs, versindex,
1232 strtab + sym[i].st_name, mod);
1234 /* Ok if resolved. */
1235 if (sym[i].st_value != 0)
1238 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1241 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1242 mod->name, strtab + sym[i].st_name);
1247 /* Divert to percpu allocation if a percpu var. */
1248 if (sym[i].st_shndx == pcpuindex)
1249 secbase = (unsigned long)mod->percpu;
1251 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1252 sym[i].st_value += secbase;
1260 /* Update size with this section: return offset. */
1261 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1265 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1266 *size = ret + sechdr->sh_size;
1270 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1271 might -- code, read-only data, read-write data, small data. Tally
1272 sizes, and place the offsets into sh_entsize fields: high bit means it
1274 static void layout_sections(struct module *mod,
1275 const Elf_Ehdr *hdr,
1277 const char *secstrings)
1279 static unsigned long const masks[][2] = {
1280 /* NOTE: all executable code must be the first section
1281 * in this array; otherwise modify the text_size
1282 * finder in the two loops below */
1283 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1284 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1285 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1286 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1290 for (i = 0; i < hdr->e_shnum; i++)
1291 sechdrs[i].sh_entsize = ~0UL;
1293 DEBUGP("Core section allocation order:\n");
1294 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1295 for (i = 0; i < hdr->e_shnum; ++i) {
1296 Elf_Shdr *s = &sechdrs[i];
1298 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1299 || (s->sh_flags & masks[m][1])
1300 || s->sh_entsize != ~0UL
1301 || strncmp(secstrings + s->sh_name,
1304 s->sh_entsize = get_offset(&mod->core_size, s);
1305 DEBUGP("\t%s\n", secstrings + s->sh_name);
1308 mod->core_text_size = mod->core_size;
1311 DEBUGP("Init section allocation order:\n");
1312 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1313 for (i = 0; i < hdr->e_shnum; ++i) {
1314 Elf_Shdr *s = &sechdrs[i];
1316 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1317 || (s->sh_flags & masks[m][1])
1318 || s->sh_entsize != ~0UL
1319 || strncmp(secstrings + s->sh_name,
1322 s->sh_entsize = (get_offset(&mod->init_size, s)
1323 | INIT_OFFSET_MASK);
1324 DEBUGP("\t%s\n", secstrings + s->sh_name);
1327 mod->init_text_size = mod->init_size;
1331 static inline int license_is_gpl_compatible(const char *license)
1333 return (strcmp(license, "GPL") == 0
1334 || strcmp(license, "GPL v2") == 0
1335 || strcmp(license, "GPL and additional rights") == 0
1336 || strcmp(license, "Dual BSD/GPL") == 0
1337 || strcmp(license, "Dual MPL/GPL") == 0);
1340 static void set_license(struct module *mod, const char *license)
1343 license = "unspecified";
1345 mod->license_gplok = license_is_gpl_compatible(license);
1346 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1347 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1348 mod->name, license);
1349 tainted |= TAINT_PROPRIETARY_MODULE;
1353 /* Parse tag=value strings from .modinfo section */
1354 static char *next_string(char *string, unsigned long *secsize)
1356 /* Skip non-zero chars */
1359 if ((*secsize)-- <= 1)
1363 /* Skip any zero padding. */
1364 while (!string[0]) {
1366 if ((*secsize)-- <= 1)
1372 static char *get_modinfo(Elf_Shdr *sechdrs,
1377 unsigned int taglen = strlen(tag);
1378 unsigned long size = sechdrs[info].sh_size;
1380 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1381 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1382 return p + taglen + 1;
1387 #ifdef CONFIG_MODULE_UNLOAD
1388 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1389 unsigned int infoindex)
1391 struct module_attribute *attr;
1394 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1397 get_modinfo(sechdrs,
1404 #ifdef CONFIG_KALLSYMS
1405 int is_exported(const char *name, const struct module *mod)
1410 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1411 if (strcmp(__start___ksymtab[i].name, name) == 0)
1415 for (i = 0; i < mod->num_syms; i++)
1416 if (strcmp(mod->syms[i].name, name) == 0)
1422 static char elf_type(const Elf_Sym *sym,
1424 const char *secstrings,
1427 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1428 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1433 if (sym->st_shndx == SHN_UNDEF)
1435 if (sym->st_shndx == SHN_ABS)
1437 if (sym->st_shndx >= SHN_LORESERVE)
1439 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1441 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1442 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1443 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1445 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1450 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1451 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1456 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1457 ".debug", strlen(".debug")) == 0)
1462 static void add_kallsyms(struct module *mod,
1464 unsigned int symindex,
1465 unsigned int strindex,
1466 const char *secstrings)
1470 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1471 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1472 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1474 /* Set types up while we still have access to sections. */
1475 for (i = 0; i < mod->num_symtab; i++)
1476 mod->symtab[i].st_info
1477 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1480 static inline void add_kallsyms(struct module *mod,
1482 unsigned int symindex,
1483 unsigned int strindex,
1484 const char *secstrings)
1487 #endif /* CONFIG_KALLSYMS */
1489 /* Allocate and load the module: note that size of section 0 is always
1490 zero, and we rely on this for optional sections. */
1491 static struct module *load_module(void __user *umod,
1493 const char __user *uargs)
1497 char *secstrings, *args, *modmagic, *strtab = NULL;
1498 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1499 exportindex, modindex, obsparmindex, infoindex, gplindex,
1500 crcindex, gplcrcindex, versindex, pcpuindex;
1504 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1505 struct exception_table_entry *extable;
1507 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1509 if (len < sizeof(*hdr))
1510 return ERR_PTR(-ENOEXEC);
1512 /* Suck in entire file: we'll want most of it. */
1513 /* vmalloc barfs on "unusual" numbers. Check here */
1514 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1515 return ERR_PTR(-ENOMEM);
1516 if (copy_from_user(hdr, umod, len) != 0) {
1521 /* Sanity checks against insmoding binaries or wrong arch,
1522 weird elf version */
1523 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1524 || hdr->e_type != ET_REL
1525 || !elf_check_arch(hdr)
1526 || hdr->e_shentsize != sizeof(*sechdrs)) {
1531 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1534 /* Convenience variables */
1535 sechdrs = (void *)hdr + hdr->e_shoff;
1536 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1537 sechdrs[0].sh_addr = 0;
1539 for (i = 1; i < hdr->e_shnum; i++) {
1540 if (sechdrs[i].sh_type != SHT_NOBITS
1541 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1544 /* Mark all sections sh_addr with their address in the
1546 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1548 /* Internal symbols and strings. */
1549 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1551 strindex = sechdrs[i].sh_link;
1552 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1554 #ifndef CONFIG_MODULE_UNLOAD
1555 /* Don't load .exit sections */
1556 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1557 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1561 modindex = find_sec(hdr, sechdrs, secstrings,
1562 ".gnu.linkonce.this_module");
1564 printk(KERN_WARNING "No module found in object\n");
1568 mod = (void *)sechdrs[modindex].sh_addr;
1570 if (symindex == 0) {
1571 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1577 /* Optional sections */
1578 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1579 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1580 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1581 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1582 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1583 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1584 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1585 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1586 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1587 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1589 /* Don't keep modinfo section */
1590 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1591 #ifdef CONFIG_KALLSYMS
1592 /* Keep symbol and string tables for decoding later. */
1593 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1594 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1597 /* Check module struct version now, before we try to use module. */
1598 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1603 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1604 /* This is allowed: modprobe --force will invalidate it. */
1606 tainted |= TAINT_FORCED_MODULE;
1607 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1609 } else if (!same_magic(modmagic, vermagic)) {
1610 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1611 mod->name, modmagic, vermagic);
1616 /* Now copy in args */
1617 arglen = strlen_user(uargs);
1622 args = kmalloc(arglen, GFP_KERNEL);
1627 if (copy_from_user(args, uargs, arglen) != 0) {
1632 if (find_module(mod->name)) {
1637 mod->state = MODULE_STATE_COMING;
1639 /* Allow arches to frob section contents and sizes. */
1640 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1645 /* We have a special allocation for this section. */
1646 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1647 sechdrs[pcpuindex].sh_addralign);
1652 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1653 mod->percpu = percpu;
1656 /* Determine total sizes, and put offsets in sh_entsize. For now
1657 this is done generically; there doesn't appear to be any
1658 special cases for the architectures. */
1659 layout_sections(mod, hdr, sechdrs, secstrings);
1661 /* Do the allocs. */
1662 ptr = module_alloc(mod->core_size);
1667 memset(ptr, 0, mod->core_size);
1668 mod->module_core = ptr;
1670 ptr = module_alloc(mod->init_size);
1671 if (!ptr && mod->init_size) {
1675 memset(ptr, 0, mod->init_size);
1676 mod->module_init = ptr;
1678 /* Transfer each section which specifies SHF_ALLOC */
1679 DEBUGP("final section addresses:\n");
1680 for (i = 0; i < hdr->e_shnum; i++) {
1683 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1686 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1687 dest = mod->module_init
1688 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1690 dest = mod->module_core + sechdrs[i].sh_entsize;
1692 if (sechdrs[i].sh_type != SHT_NOBITS)
1693 memcpy(dest, (void *)sechdrs[i].sh_addr,
1694 sechdrs[i].sh_size);
1695 /* Update sh_addr to point to copy in image. */
1696 sechdrs[i].sh_addr = (unsigned long)dest;
1697 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1699 /* Module has been moved. */
1700 mod = (void *)sechdrs[modindex].sh_addr;
1702 /* Now we've moved module, initialize linked lists, etc. */
1703 module_unload_init(mod);
1705 /* Set up license info based on the info section */
1706 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1708 #ifdef CONFIG_MODULE_UNLOAD
1709 /* Set up MODINFO_ATTR fields */
1710 setup_modinfo(mod, sechdrs, infoindex);
1713 /* Fix up syms, so that st_value is a pointer to location. */
1714 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1719 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1720 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1721 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1723 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1724 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1725 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1727 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1729 #ifdef CONFIG_MODVERSIONS
1730 if ((mod->num_syms && !crcindex) ||
1731 (mod->num_gpl_syms && !gplcrcindex)) {
1732 printk(KERN_WARNING "%s: No versions for exported symbols."
1733 " Tainting kernel.\n", mod->name);
1734 tainted |= TAINT_FORCED_MODULE;
1738 /* Now do relocations. */
1739 for (i = 1; i < hdr->e_shnum; i++) {
1740 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1741 unsigned int info = sechdrs[i].sh_info;
1743 /* Not a valid relocation section? */
1744 if (info >= hdr->e_shnum)
1747 /* Don't bother with non-allocated sections */
1748 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1751 if (sechdrs[i].sh_type == SHT_REL)
1752 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1753 else if (sechdrs[i].sh_type == SHT_RELA)
1754 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1760 /* Set up and sort exception table */
1761 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1762 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1763 sort_extable(extable, extable + mod->num_exentries);
1765 /* Finally, copy percpu area over. */
1766 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1767 sechdrs[pcpuindex].sh_size);
1769 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1771 err = module_finalize(hdr, sechdrs, mod);
1777 err = obsolete_params(mod->name, mod->args,
1778 (struct obsolete_modparm *)
1779 sechdrs[obsparmindex].sh_addr,
1780 sechdrs[obsparmindex].sh_size
1781 / sizeof(struct obsolete_modparm),
1783 (char *)sechdrs[strindex].sh_addr);
1785 printk(KERN_WARNING "%s: Ignoring new-style "
1786 "parameters in presence of obsolete ones\n",
1789 /* Size of section 0 is 0, so this works well if no params */
1790 err = parse_args(mod->name, mod->args,
1791 (struct kernel_param *)
1792 sechdrs[setupindex].sh_addr,
1793 sechdrs[setupindex].sh_size
1794 / sizeof(struct kernel_param),
1800 err = mod_sysfs_setup(mod,
1801 (struct kernel_param *)
1802 sechdrs[setupindex].sh_addr,
1803 sechdrs[setupindex].sh_size
1804 / sizeof(struct kernel_param));
1807 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1809 /* Get rid of temporary copy */
1816 module_arch_cleanup(mod);
1818 module_unload_free(mod);
1819 module_free(mod, mod->module_init);
1821 module_free(mod, mod->module_core);
1824 percpu_modfree(percpu);
1829 if (err < 0) return ERR_PTR(err);
1833 printk(KERN_ERR "Module len %lu truncated\n", len);
1839 * link the module with the whole machine is stopped with interrupts off
1840 * - this defends against kallsyms not taking locks
1842 static int __link_module(void *_mod)
1844 struct module *mod = _mod;
1845 list_add(&mod->list, &modules);
1849 /* This is where the real work happens */
1851 sys_init_module(void __user *umod,
1853 const char __user *uargs)
1856 mm_segment_t old_fs = get_fs();
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 icache in correct context */
1877 /* Flush the instruction cache, since we've played with text */
1878 if (mod->module_init)
1879 flush_icache_range((unsigned long)mod->module_init,
1880 (unsigned long)mod->module_init
1882 flush_icache_range((unsigned long)mod->module_core,
1883 (unsigned long)mod->module_core + mod->core_size);
1887 /* Now sew it into the lists. They won't access us, since
1888 strong_try_module_get() will fail. */
1889 stop_machine_run(__link_module, mod, NR_CPUS);
1891 /* Drop lock so they can recurse */
1894 down(¬ify_mutex);
1895 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1898 /* Start the module */
1899 if (mod->init != NULL)
1902 /* Init routine failed: abort. Try to protect us from
1903 buggy refcounters. */
1904 mod->state = MODULE_STATE_GOING;
1905 synchronize_sched();
1907 printk(KERN_ERR "%s: module is now stuck!\n",
1911 down(&module_mutex);
1918 /* Now it's a first class citizen! */
1919 down(&module_mutex);
1920 mod->state = MODULE_STATE_LIVE;
1921 /* Drop initial reference. */
1923 module_free(mod, mod->module_init);
1924 mod->module_init = NULL;
1926 mod->init_text_size = 0;
1932 static inline int within(unsigned long addr, void *start, unsigned long size)
1934 return ((void *)addr >= start && (void *)addr < start + size);
1937 #ifdef CONFIG_KALLSYMS
1939 * This ignores the intensely annoying "mapping symbols" found
1940 * in ARM ELF files: $a, $t and $d.
1942 static inline int is_arm_mapping_symbol(const char *str)
1944 return str[0] == '$' && strchr("atd", str[1])
1945 && (str[2] == '\0' || str[2] == '.');
1948 static const char *get_ksymbol(struct module *mod,
1950 unsigned long *size,
1951 unsigned long *offset)
1953 unsigned int i, best = 0;
1954 unsigned long nextval;
1956 /* At worse, next value is at end of module */
1957 if (within(addr, mod->module_init, mod->init_size))
1958 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1960 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1962 /* Scan for closest preceeding symbol, and next symbol. (ELF
1963 starts real symbols at 1). */
1964 for (i = 1; i < mod->num_symtab; i++) {
1965 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1968 /* We ignore unnamed symbols: they're uninformative
1969 * and inserted at a whim. */
1970 if (mod->symtab[i].st_value <= addr
1971 && mod->symtab[i].st_value > mod->symtab[best].st_value
1972 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1973 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1975 if (mod->symtab[i].st_value > addr
1976 && mod->symtab[i].st_value < nextval
1977 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1978 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1979 nextval = mod->symtab[i].st_value;
1985 *size = nextval - mod->symtab[best].st_value;
1986 *offset = addr - mod->symtab[best].st_value;
1987 return mod->strtab + mod->symtab[best].st_name;
1990 /* For kallsyms to ask for address resolution. NULL means not found.
1991 We don't lock, as this is used for oops resolution and races are a
1993 const char *module_address_lookup(unsigned long addr,
1994 unsigned long *size,
1995 unsigned long *offset,
2000 list_for_each_entry(mod, &modules, list) {
2001 if (within(addr, mod->module_init, mod->init_size)
2002 || within(addr, mod->module_core, mod->core_size)) {
2003 *modname = mod->name;
2004 return get_ksymbol(mod, addr, size, offset);
2010 struct module *module_get_kallsym(unsigned int symnum,
2011 unsigned long *value,
2017 down(&module_mutex);
2018 list_for_each_entry(mod, &modules, list) {
2019 if (symnum < mod->num_symtab) {
2020 *value = mod->symtab[symnum].st_value;
2021 *type = mod->symtab[symnum].st_info;
2023 mod->strtab + mod->symtab[symnum].st_name,
2028 symnum -= mod->num_symtab;
2034 static unsigned long mod_find_symname(struct module *mod, const char *name)
2038 for (i = 0; i < mod->num_symtab; i++)
2039 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
2040 return mod->symtab[i].st_value;
2044 /* Look for this name: can be of form module:name. */
2045 unsigned long module_kallsyms_lookup_name(const char *name)
2049 unsigned long ret = 0;
2051 /* Don't lock: we're in enough trouble already. */
2052 if ((colon = strchr(name, ':')) != NULL) {
2054 if ((mod = find_module(name)) != NULL)
2055 ret = mod_find_symname(mod, colon+1);
2058 list_for_each_entry(mod, &modules, list)
2059 if ((ret = mod_find_symname(mod, name)) != 0)
2064 #endif /* CONFIG_KALLSYMS */
2066 /* Called by the /proc file system to return a list of modules. */
2067 static void *m_start(struct seq_file *m, loff_t *pos)
2069 struct list_head *i;
2072 down(&module_mutex);
2073 list_for_each(i, &modules) {
2082 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2084 struct list_head *i = p;
2086 if (i->next == &modules)
2091 static void m_stop(struct seq_file *m, void *p)
2096 static int m_show(struct seq_file *m, void *p)
2098 struct module *mod = list_entry(p, struct module, list);
2099 seq_printf(m, "%s %lu",
2100 mod->name, mod->init_size + mod->core_size);
2101 print_unload_info(m, mod);
2103 /* Informative for users. */
2104 seq_printf(m, " %s",
2105 mod->state == MODULE_STATE_GOING ? "Unloading":
2106 mod->state == MODULE_STATE_COMING ? "Loading":
2108 /* Used by oprofile and other similar tools. */
2109 seq_printf(m, " 0x%p", mod->module_core);
2111 seq_printf(m, "\n");
2115 /* Format: modulename size refcount deps address
2117 Where refcount is a number or -, and deps is a comma-separated list
2120 struct seq_operations modules_op = {
2127 /* Given an address, look for it in the module exception tables. */
2128 const struct exception_table_entry *search_module_extables(unsigned long addr)
2130 unsigned long flags;
2131 const struct exception_table_entry *e = NULL;
2134 spin_lock_irqsave(&modlist_lock, flags);
2135 list_for_each_entry(mod, &modules, list) {
2136 if (mod->num_exentries == 0)
2139 e = search_extable(mod->extable,
2140 mod->extable + mod->num_exentries - 1,
2145 spin_unlock_irqrestore(&modlist_lock, flags);
2147 /* Now, if we found one, we are running inside it now, hence
2148 we cannot unload the module, hence no refcnt needed. */
2152 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2153 struct module *__module_text_address(unsigned long addr)
2157 list_for_each_entry(mod, &modules, list)
2158 if (within(addr, mod->module_init, mod->init_text_size)
2159 || within(addr, mod->module_core, mod->core_text_size))
2164 struct module *module_text_address(unsigned long addr)
2167 unsigned long flags;
2169 spin_lock_irqsave(&modlist_lock, flags);
2170 mod = __module_text_address(addr);
2171 spin_unlock_irqrestore(&modlist_lock, flags);
2176 /* Don't grab lock, we're oopsing. */
2177 void print_modules(void)
2181 printk("Modules linked in:");
2182 list_for_each_entry(mod, &modules, list)
2183 printk(" %s", mod->name);
2187 void module_add_driver(struct module *mod, struct device_driver *drv)
2192 /* Don't check return code; this call is idempotent */
2193 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2195 EXPORT_SYMBOL(module_add_driver);
2197 void module_remove_driver(struct device_driver *drv)
2201 sysfs_remove_link(&drv->kobj, "module");
2203 EXPORT_SYMBOL(module_remove_driver);
2205 #ifdef CONFIG_MODVERSIONS
2206 /* Generate the signature for struct module here, too, for modversions. */
2207 void struct_module(struct module *mod) { return; }
2208 EXPORT_SYMBOL(struct_module);