1 /* Rewritten by Rusty Russell, on the backs of many others...
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/elf.h>
26 #include <linux/seq_file.h>
27 #include <linux/syscalls.h>
28 #include <linux/fcntl.h>
29 #include <linux/rcupdate.h>
30 #include <linux/cpu.h>
31 #include <linux/moduleparam.h>
32 #include <linux/errno.h>
33 #include <linux/err.h>
34 #include <linux/vermagic.h>
35 #include <linux/notifier.h>
36 #include <linux/stop_machine.h>
37 #include <linux/device.h>
38 #include <asm/uaccess.h>
39 #include <asm/semaphore.h>
40 #include <asm/cacheflush.h>
45 #define DEBUGP(fmt , a...)
48 #ifndef ARCH_SHF_SMALL
49 #define ARCH_SHF_SMALL 0
52 /* If this is set, the section belongs in the init part of the module */
53 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
55 /* Protects module list */
56 static DEFINE_SPINLOCK(modlist_lock);
58 /* List of modules, protected by module_mutex AND modlist_lock */
59 static DECLARE_MUTEX(module_mutex);
60 static LIST_HEAD(modules);
62 static DECLARE_MUTEX(notify_mutex);
63 static struct notifier_block * module_notify_list;
65 int register_module_notifier(struct notifier_block * nb)
69 err = notifier_chain_register(&module_notify_list, nb);
73 EXPORT_SYMBOL(register_module_notifier);
75 int unregister_module_notifier(struct notifier_block * nb)
79 err = notifier_chain_unregister(&module_notify_list, nb);
83 EXPORT_SYMBOL(unregister_module_notifier);
85 /* We require a truly strong try_module_get() */
86 static inline int strong_try_module_get(struct module *mod)
88 if (mod && mod->state == MODULE_STATE_COMING)
90 return try_module_get(mod);
93 /* A thread that wants to hold a reference to a module only while it
94 * is running can call ths to safely exit.
95 * nfsd and lockd use this.
97 void __module_put_and_exit(struct module *mod, long code)
102 EXPORT_SYMBOL(__module_put_and_exit);
104 /* Find a module section: 0 means not found. */
105 static unsigned int find_sec(Elf_Ehdr *hdr,
107 const char *secstrings,
112 for (i = 1; i < hdr->e_shnum; i++)
113 /* Alloc bit cleared means "ignore it." */
114 if ((sechdrs[i].sh_flags & SHF_ALLOC)
115 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
120 /* Provided by the linker */
121 extern const struct kernel_symbol __start___ksymtab[];
122 extern const struct kernel_symbol __stop___ksymtab[];
123 extern const struct kernel_symbol __start___ksymtab_gpl[];
124 extern const struct kernel_symbol __stop___ksymtab_gpl[];
125 extern const unsigned long __start___kcrctab[];
126 extern const unsigned long __start___kcrctab_gpl[];
128 #ifndef CONFIG_MODVERSIONS
129 #define symversion(base, idx) NULL
131 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
134 /* Find a symbol, return value, crc and module which owns it */
135 static unsigned long __find_symbol(const char *name,
136 struct module **owner,
137 const unsigned long **crc,
143 /* Core kernel first. */
145 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
146 if (strcmp(__start___ksymtab[i].name, name) == 0) {
147 *crc = symversion(__start___kcrctab, i);
148 return __start___ksymtab[i].value;
152 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
153 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
154 *crc = symversion(__start___kcrctab_gpl, i);
155 return __start___ksymtab_gpl[i].value;
159 /* Now try modules. */
160 list_for_each_entry(mod, &modules, list) {
162 for (i = 0; i < mod->num_syms; i++)
163 if (strcmp(mod->syms[i].name, name) == 0) {
164 *crc = symversion(mod->crcs, i);
165 return mod->syms[i].value;
169 for (i = 0; i < mod->num_gpl_syms; i++) {
170 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
171 *crc = symversion(mod->gpl_crcs, i);
172 return mod->gpl_syms[i].value;
177 DEBUGP("Failed to find symbol %s\n", name);
181 /* Find a symbol in this elf symbol table */
182 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
183 unsigned int symindex,
188 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
190 /* Search (defined) internal symbols first. */
191 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
192 if (sym[i].st_shndx != SHN_UNDEF
193 && strcmp(name, strtab + sym[i].st_name) == 0)
194 return sym[i].st_value;
199 /* Search for module by name: must hold module_mutex. */
200 static struct module *find_module(const char *name)
204 list_for_each_entry(mod, &modules, list) {
205 if (strcmp(mod->name, name) == 0)
212 /* Number of blocks used and allocated. */
213 static unsigned int pcpu_num_used, pcpu_num_allocated;
214 /* Size of each block. -ve means used. */
215 static int *pcpu_size;
217 static int split_block(unsigned int i, unsigned short size)
219 /* Reallocation required? */
220 if (pcpu_num_used + 1 > pcpu_num_allocated) {
221 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
226 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
227 pcpu_num_allocated *= 2;
232 /* Insert a new subblock */
233 memmove(&pcpu_size[i+1], &pcpu_size[i],
234 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
237 pcpu_size[i+1] -= size;
242 static inline unsigned int block_size(int val)
249 /* Created by linker magic */
250 extern char __per_cpu_start[], __per_cpu_end[];
252 static void *percpu_modalloc(unsigned long size, unsigned long align)
258 BUG_ON(align > SMP_CACHE_BYTES);
260 ptr = __per_cpu_start;
261 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
262 /* Extra for alignment requirement. */
263 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
264 BUG_ON(i == 0 && extra != 0);
266 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
269 /* Transfer extra to previous block. */
270 if (pcpu_size[i-1] < 0)
271 pcpu_size[i-1] -= extra;
273 pcpu_size[i-1] += extra;
274 pcpu_size[i] -= extra;
277 /* Split block if warranted */
278 if (pcpu_size[i] - size > sizeof(unsigned long))
279 if (!split_block(i, size))
283 pcpu_size[i] = -pcpu_size[i];
287 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
292 static void percpu_modfree(void *freeme)
295 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
297 /* First entry is core kernel percpu data. */
298 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
300 pcpu_size[i] = -pcpu_size[i];
307 /* Merge with previous? */
308 if (pcpu_size[i-1] >= 0) {
309 pcpu_size[i-1] += pcpu_size[i];
311 memmove(&pcpu_size[i], &pcpu_size[i+1],
312 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
315 /* Merge with next? */
316 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
317 pcpu_size[i] += pcpu_size[i+1];
319 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
320 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
324 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
326 const char *secstrings)
328 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
331 static int percpu_modinit(void)
334 pcpu_num_allocated = 2;
335 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
337 /* Static in-kernel percpu data (used). */
338 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
340 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
341 if (pcpu_size[1] < 0) {
342 printk(KERN_ERR "No per-cpu room for modules.\n");
348 __initcall(percpu_modinit);
349 #else /* ... !CONFIG_SMP */
350 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
354 static inline void percpu_modfree(void *pcpuptr)
358 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
360 const char *secstrings)
364 static inline void percpu_modcopy(void *pcpudst, const void *src,
367 /* pcpusec should be 0, and size of that section should be 0. */
370 #endif /* CONFIG_SMP */
372 #ifdef CONFIG_MODULE_UNLOAD
373 /* Init the unload section of the module. */
374 static void module_unload_init(struct module *mod)
378 INIT_LIST_HEAD(&mod->modules_which_use_me);
379 for (i = 0; i < NR_CPUS; i++)
380 local_set(&mod->ref[i].count, 0);
381 /* Hold reference count during initialization. */
382 local_set(&mod->ref[_smp_processor_id()].count, 1);
383 /* Backwards compatibility macros put refcount during init. */
384 mod->waiter = current;
387 /* modules using other modules */
390 struct list_head list;
391 struct module *module_which_uses;
394 /* Does a already use b? */
395 static int already_uses(struct module *a, struct module *b)
397 struct module_use *use;
399 list_for_each_entry(use, &b->modules_which_use_me, list) {
400 if (use->module_which_uses == a) {
401 DEBUGP("%s uses %s!\n", a->name, b->name);
405 DEBUGP("%s does not use %s!\n", a->name, b->name);
409 /* Module a uses b */
410 static int use_module(struct module *a, struct module *b)
412 struct module_use *use;
413 if (b == NULL || already_uses(a, b)) return 1;
415 if (!strong_try_module_get(b))
418 DEBUGP("Allocating new usage for %s.\n", a->name);
419 use = kmalloc(sizeof(*use), GFP_ATOMIC);
421 printk("%s: out of memory loading\n", a->name);
426 use->module_which_uses = a;
427 list_add(&use->list, &b->modules_which_use_me);
431 /* Clear the unload stuff of the module. */
432 static void module_unload_free(struct module *mod)
436 list_for_each_entry(i, &modules, list) {
437 struct module_use *use;
439 list_for_each_entry(use, &i->modules_which_use_me, list) {
440 if (use->module_which_uses == mod) {
441 DEBUGP("%s unusing %s\n", mod->name, i->name);
443 list_del(&use->list);
445 /* There can be at most one match. */
452 #ifdef CONFIG_MODULE_FORCE_UNLOAD
453 static inline int try_force(unsigned int flags)
455 int ret = (flags & O_TRUNC);
457 tainted |= TAINT_FORCED_MODULE;
461 static inline int try_force(unsigned int flags)
465 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
474 /* Whole machine is stopped with interrupts off when this runs. */
475 static inline int __try_stop_module(void *_sref)
477 struct stopref *sref = _sref;
479 /* If it's not unused, quit unless we are told to block. */
480 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
481 if (!(*sref->forced = try_force(sref->flags)))
485 /* Mark it as dying. */
486 sref->mod->state = MODULE_STATE_GOING;
490 static int try_stop_module(struct module *mod, int flags, int *forced)
492 struct stopref sref = { mod, flags, forced };
494 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
497 unsigned int module_refcount(struct module *mod)
499 unsigned int i, total = 0;
501 for (i = 0; i < NR_CPUS; i++)
502 total += local_read(&mod->ref[i].count);
505 EXPORT_SYMBOL(module_refcount);
507 /* This exists whether we can unload or not */
508 static void free_module(struct module *mod);
510 static void wait_for_zero_refcount(struct module *mod)
512 /* Since we might sleep for some time, drop the semaphore first */
515 DEBUGP("Looking at refcount...\n");
516 set_current_state(TASK_UNINTERRUPTIBLE);
517 if (module_refcount(mod) == 0)
521 current->state = TASK_RUNNING;
526 sys_delete_module(const char __user *name_user, unsigned int flags)
529 char name[MODULE_NAME_LEN];
532 if (!capable(CAP_SYS_MODULE))
535 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
537 name[MODULE_NAME_LEN-1] = '\0';
539 if (down_interruptible(&module_mutex) != 0)
542 mod = find_module(name);
548 if (!list_empty(&mod->modules_which_use_me)) {
549 /* Other modules depend on us: get rid of them first. */
554 /* Doing init or already dying? */
555 if (mod->state != MODULE_STATE_LIVE) {
556 /* FIXME: if (force), slam module count and wake up
558 DEBUGP("%s already dying\n", mod->name);
563 /* If it has an init func, it must have an exit func to unload */
564 if ((mod->init != NULL && mod->exit == NULL)
566 forced = try_force(flags);
568 /* This module can't be removed */
574 /* Set this up before setting mod->state */
575 mod->waiter = current;
577 /* Stop the machine so refcounts can't move and disable module. */
578 ret = try_stop_module(mod, flags, &forced);
582 /* Never wait if forced. */
583 if (!forced && module_refcount(mod) != 0)
584 wait_for_zero_refcount(mod);
586 /* Final destruction now noone is using it. */
587 if (mod->exit != NULL) {
599 static void print_unload_info(struct seq_file *m, struct module *mod)
601 struct module_use *use;
602 int printed_something = 0;
604 seq_printf(m, " %u ", module_refcount(mod));
606 /* Always include a trailing , so userspace can differentiate
607 between this and the old multi-field proc format. */
608 list_for_each_entry(use, &mod->modules_which_use_me, list) {
609 printed_something = 1;
610 seq_printf(m, "%s,", use->module_which_uses->name);
614 printed_something = 1;
615 seq_printf(m, "[unsafe],");
618 if (mod->init != NULL && mod->exit == NULL) {
619 printed_something = 1;
620 seq_printf(m, "[permanent],");
623 if (!printed_something)
627 void __symbol_put(const char *symbol)
629 struct module *owner;
631 const unsigned long *crc;
633 spin_lock_irqsave(&modlist_lock, flags);
634 if (!__find_symbol(symbol, &owner, &crc, 1))
637 spin_unlock_irqrestore(&modlist_lock, flags);
639 EXPORT_SYMBOL(__symbol_put);
641 void symbol_put_addr(void *addr)
645 spin_lock_irqsave(&modlist_lock, flags);
646 if (!kernel_text_address((unsigned long)addr))
649 module_put(module_text_address((unsigned long)addr));
650 spin_unlock_irqrestore(&modlist_lock, flags);
652 EXPORT_SYMBOL_GPL(symbol_put_addr);
654 static ssize_t show_refcnt(struct module_attribute *mattr,
655 struct module *mod, char *buffer)
657 /* sysfs holds a reference */
658 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
661 static struct module_attribute refcnt = {
662 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
666 #else /* !CONFIG_MODULE_UNLOAD */
667 static void print_unload_info(struct seq_file *m, struct module *mod)
669 /* We don't know the usage count, or what modules are using. */
670 seq_printf(m, " - -");
673 static inline void module_unload_free(struct module *mod)
677 static inline int use_module(struct module *a, struct module *b)
679 return strong_try_module_get(b);
682 static inline void module_unload_init(struct module *mod)
685 #endif /* CONFIG_MODULE_UNLOAD */
687 #ifdef CONFIG_OBSOLETE_MODPARM
688 /* Bounds checking done below */
689 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
691 strcpy(kp->arg, val);
695 int set_obsolete(const char *val, struct kernel_param *kp)
697 unsigned int min, max;
698 unsigned int size, maxsize;
702 struct obsolete_modparm *obsparm = kp->arg;
705 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
709 /* type is: [min[-max]]{b,h,i,l,s} */
711 min = simple_strtol(p, &endp, 10);
712 if (endp == obsparm->type)
714 else if (*endp == '-') {
716 max = simple_strtol(p, &endp, 10);
721 return param_array(kp->name, val, min, max, obsparm->addr,
722 1, param_set_byte, &dummy);
724 return param_array(kp->name, val, min, max, obsparm->addr,
725 sizeof(short), param_set_short, &dummy);
727 return param_array(kp->name, val, min, max, obsparm->addr,
728 sizeof(int), param_set_int, &dummy);
730 return param_array(kp->name, val, min, max, obsparm->addr,
731 sizeof(long), param_set_long, &dummy);
733 return param_array(kp->name, val, min, max, obsparm->addr,
734 sizeof(char *), param_set_charp, &dummy);
737 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
738 and the decl is "char xxx[5][50];" */
740 maxsize = simple_strtol(p, &endp, 10);
741 /* We check lengths here (yes, this is a hack). */
743 while (p[size = strcspn(p, ",")]) {
750 return param_array(kp->name, val, min, max, obsparm->addr,
751 maxsize, obsparm_copy_string, &dummy);
753 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
757 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
761 static int obsolete_params(const char *name,
763 struct obsolete_modparm obsparm[],
766 unsigned int symindex,
769 struct kernel_param *kp;
773 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
777 for (i = 0; i < num; i++) {
778 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
780 snprintf(sym_name, sizeof(sym_name), "%s%s",
781 MODULE_SYMBOL_PREFIX, obsparm[i].name);
783 kp[i].name = obsparm[i].name;
785 kp[i].set = set_obsolete;
788 = (void *)find_local_symbol(sechdrs, symindex, strtab,
790 if (!obsparm[i].addr) {
791 printk("%s: falsely claims to have parameter %s\n",
792 name, obsparm[i].name);
796 kp[i].arg = &obsparm[i];
799 ret = parse_args(name, args, kp, num, NULL);
805 static int obsolete_params(const char *name,
807 struct obsolete_modparm obsparm[],
810 unsigned int symindex,
814 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
818 #endif /* CONFIG_OBSOLETE_MODPARM */
820 static const char vermagic[] = VERMAGIC_STRING;
822 #ifdef CONFIG_MODVERSIONS
823 static int check_version(Elf_Shdr *sechdrs,
824 unsigned int versindex,
827 const unsigned long *crc)
829 unsigned int i, num_versions;
830 struct modversion_info *versions;
832 /* Exporting module didn't supply crcs? OK, we're already tainted. */
836 versions = (void *) sechdrs[versindex].sh_addr;
837 num_versions = sechdrs[versindex].sh_size
838 / sizeof(struct modversion_info);
840 for (i = 0; i < num_versions; i++) {
841 if (strcmp(versions[i].name, symname) != 0)
844 if (versions[i].crc == *crc)
846 printk("%s: disagrees about version of symbol %s\n",
848 DEBUGP("Found checksum %lX vs module %lX\n",
849 *crc, versions[i].crc);
852 /* Not in module's version table. OK, but that taints the kernel. */
853 if (!(tainted & TAINT_FORCED_MODULE)) {
854 printk("%s: no version for \"%s\" found: kernel tainted.\n",
856 tainted |= TAINT_FORCED_MODULE;
861 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
862 unsigned int versindex,
865 const unsigned long *crc;
866 struct module *owner;
868 if (!__find_symbol("struct_module", &owner, &crc, 1))
870 return check_version(sechdrs, versindex, "struct_module", mod,
874 /* First part is kernel version, which we ignore. */
875 static inline int same_magic(const char *amagic, const char *bmagic)
877 amagic += strcspn(amagic, " ");
878 bmagic += strcspn(bmagic, " ");
879 return strcmp(amagic, bmagic) == 0;
882 static inline int check_version(Elf_Shdr *sechdrs,
883 unsigned int versindex,
886 const unsigned long *crc)
891 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
892 unsigned int versindex,
898 static inline int same_magic(const char *amagic, const char *bmagic)
900 return strcmp(amagic, bmagic) == 0;
902 #endif /* CONFIG_MODVERSIONS */
904 /* Resolve a symbol for this module. I.e. if we find one, record usage.
905 Must be holding module_mutex. */
906 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
907 unsigned int versindex,
911 struct module *owner;
913 const unsigned long *crc;
915 spin_lock_irq(&modlist_lock);
916 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
918 /* use_module can fail due to OOM, or module unloading */
919 if (!check_version(sechdrs, versindex, name, mod, crc) ||
920 !use_module(mod, owner))
923 spin_unlock_irq(&modlist_lock);
929 * /sys/module/foo/sections stuff
930 * J. Corbet <corbet@lwn.net>
932 #ifdef CONFIG_KALLSYMS
933 static ssize_t module_sect_show(struct module_attribute *mattr,
934 struct module *mod, char *buf)
936 struct module_sect_attr *sattr =
937 container_of(mattr, struct module_sect_attr, mattr);
938 return sprintf(buf, "0x%lx\n", sattr->address);
941 static void add_sect_attrs(struct module *mod, unsigned int nsect,
942 char *secstrings, Elf_Shdr *sechdrs)
944 unsigned int nloaded = 0, i, size[2];
945 struct module_sect_attrs *sect_attrs;
946 struct module_sect_attr *sattr;
947 struct attribute **gattr;
949 /* Count loaded sections and allocate structures */
950 for (i = 0; i < nsect; i++)
951 if (sechdrs[i].sh_flags & SHF_ALLOC)
953 size[0] = ALIGN(sizeof(*sect_attrs)
954 + nloaded * sizeof(sect_attrs->attrs[0]),
955 sizeof(sect_attrs->grp.attrs[0]));
956 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
957 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
960 /* Setup section attributes. */
961 sect_attrs->grp.name = "sections";
962 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
964 sattr = §_attrs->attrs[0];
965 gattr = §_attrs->grp.attrs[0];
966 for (i = 0; i < nsect; i++) {
967 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
969 sattr->address = sechdrs[i].sh_addr;
970 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
971 MODULE_SECT_NAME_LEN);
972 sattr->mattr.show = module_sect_show;
973 sattr->mattr.store = NULL;
974 sattr->mattr.attr.name = sattr->name;
975 sattr->mattr.attr.owner = mod;
976 sattr->mattr.attr.mode = S_IRUGO;
977 *(gattr++) = &(sattr++)->mattr.attr;
981 if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp))
984 mod->sect_attrs = sect_attrs;
990 static void remove_sect_attrs(struct module *mod)
992 if (mod->sect_attrs) {
993 sysfs_remove_group(&mod->mkobj.kobj,
994 &mod->sect_attrs->grp);
995 /* We are positive that no one is using any sect attrs
996 * at this point. Deallocate immediately. */
997 kfree(mod->sect_attrs);
998 mod->sect_attrs = NULL;
1004 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1005 char *sectstrings, Elf_Shdr *sechdrs)
1009 static inline void remove_sect_attrs(struct module *mod)
1012 #endif /* CONFIG_KALLSYMS */
1015 #ifdef CONFIG_MODULE_UNLOAD
1016 static inline int module_add_refcnt_attr(struct module *mod)
1018 return sysfs_create_file(&mod->mkobj.kobj, &refcnt.attr);
1020 static void module_remove_refcnt_attr(struct module *mod)
1022 return sysfs_remove_file(&mod->mkobj.kobj, &refcnt.attr);
1025 static inline int module_add_refcnt_attr(struct module *mod)
1029 static void module_remove_refcnt_attr(struct module *mod)
1035 static int mod_sysfs_setup(struct module *mod,
1036 struct kernel_param *kparam,
1037 unsigned int num_params)
1041 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1042 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1045 kobj_set_kset_s(&mod->mkobj, module_subsys);
1046 mod->mkobj.mod = mod;
1047 err = kobject_register(&mod->mkobj.kobj);
1051 err = module_add_refcnt_attr(mod);
1055 err = module_param_sysfs_setup(mod, kparam, num_params);
1062 kobject_unregister(&mod->mkobj.kobj);
1067 static void mod_kobject_remove(struct module *mod)
1069 module_remove_refcnt_attr(mod);
1070 module_param_sysfs_remove(mod);
1072 kobject_unregister(&mod->mkobj.kobj);
1075 /* Free a module, remove from lists, etc (must hold module mutex). */
1076 static void free_module(struct module *mod)
1078 /* Delete from various lists */
1079 spin_lock_irq(&modlist_lock);
1080 list_del(&mod->list);
1081 spin_unlock_irq(&modlist_lock);
1083 remove_sect_attrs(mod);
1084 mod_kobject_remove(mod);
1086 /* Arch-specific cleanup. */
1087 module_arch_cleanup(mod);
1089 /* Module unload stuff */
1090 module_unload_free(mod);
1092 /* This may be NULL, but that's OK */
1093 module_free(mod, mod->module_init);
1096 percpu_modfree(mod->percpu);
1098 /* Finally, free the core (containing the module structure) */
1099 module_free(mod, mod->module_core);
1102 void *__symbol_get(const char *symbol)
1104 struct module *owner;
1105 unsigned long value, flags;
1106 const unsigned long *crc;
1108 spin_lock_irqsave(&modlist_lock, flags);
1109 value = __find_symbol(symbol, &owner, &crc, 1);
1110 if (value && !strong_try_module_get(owner))
1112 spin_unlock_irqrestore(&modlist_lock, flags);
1114 return (void *)value;
1116 EXPORT_SYMBOL_GPL(__symbol_get);
1118 /* Change all symbols so that sh_value encodes the pointer directly. */
1119 static int simplify_symbols(Elf_Shdr *sechdrs,
1120 unsigned int symindex,
1122 unsigned int versindex,
1123 unsigned int pcpuindex,
1126 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1127 unsigned long secbase;
1128 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1131 for (i = 1; i < n; i++) {
1132 switch (sym[i].st_shndx) {
1134 /* We compiled with -fno-common. These are not
1135 supposed to happen. */
1136 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1137 printk("%s: please compile with -fno-common\n",
1143 /* Don't need to do anything */
1144 DEBUGP("Absolute symbol: 0x%08lx\n",
1145 (long)sym[i].st_value);
1150 = resolve_symbol(sechdrs, versindex,
1151 strtab + sym[i].st_name, mod);
1153 /* Ok if resolved. */
1154 if (sym[i].st_value != 0)
1157 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1160 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1161 mod->name, strtab + sym[i].st_name);
1166 /* Divert to percpu allocation if a percpu var. */
1167 if (sym[i].st_shndx == pcpuindex)
1168 secbase = (unsigned long)mod->percpu;
1170 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1171 sym[i].st_value += secbase;
1179 /* Update size with this section: return offset. */
1180 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1184 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1185 *size = ret + sechdr->sh_size;
1189 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1190 might -- code, read-only data, read-write data, small data. Tally
1191 sizes, and place the offsets into sh_entsize fields: high bit means it
1193 static void layout_sections(struct module *mod,
1194 const Elf_Ehdr *hdr,
1196 const char *secstrings)
1198 static unsigned long const masks[][2] = {
1199 /* NOTE: all executable code must be the first section
1200 * in this array; otherwise modify the text_size
1201 * finder in the two loops below */
1202 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1203 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1204 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1205 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1209 for (i = 0; i < hdr->e_shnum; i++)
1210 sechdrs[i].sh_entsize = ~0UL;
1212 DEBUGP("Core section allocation order:\n");
1213 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1214 for (i = 0; i < hdr->e_shnum; ++i) {
1215 Elf_Shdr *s = &sechdrs[i];
1217 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1218 || (s->sh_flags & masks[m][1])
1219 || s->sh_entsize != ~0UL
1220 || strncmp(secstrings + s->sh_name,
1223 s->sh_entsize = get_offset(&mod->core_size, s);
1224 DEBUGP("\t%s\n", secstrings + s->sh_name);
1227 mod->core_text_size = mod->core_size;
1230 DEBUGP("Init 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->init_size, s)
1242 | INIT_OFFSET_MASK);
1243 DEBUGP("\t%s\n", secstrings + s->sh_name);
1246 mod->init_text_size = mod->init_size;
1250 static inline int license_is_gpl_compatible(const char *license)
1252 return (strcmp(license, "GPL") == 0
1253 || strcmp(license, "GPL v2") == 0
1254 || strcmp(license, "GPL and additional rights") == 0
1255 || strcmp(license, "Dual BSD/GPL") == 0
1256 || strcmp(license, "Dual MPL/GPL") == 0);
1259 static void set_license(struct module *mod, const char *license)
1262 license = "unspecified";
1264 mod->license_gplok = license_is_gpl_compatible(license);
1265 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1266 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1267 mod->name, license);
1268 tainted |= TAINT_PROPRIETARY_MODULE;
1272 /* Parse tag=value strings from .modinfo section */
1273 static char *next_string(char *string, unsigned long *secsize)
1275 /* Skip non-zero chars */
1278 if ((*secsize)-- <= 1)
1282 /* Skip any zero padding. */
1283 while (!string[0]) {
1285 if ((*secsize)-- <= 1)
1291 static char *get_modinfo(Elf_Shdr *sechdrs,
1296 unsigned int taglen = strlen(tag);
1297 unsigned long size = sechdrs[info].sh_size;
1299 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1300 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1301 return p + taglen + 1;
1306 #ifdef CONFIG_KALLSYMS
1307 int is_exported(const char *name, const struct module *mod)
1312 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1313 if (strcmp(__start___ksymtab[i].name, name) == 0)
1317 for (i = 0; i < mod->num_syms; i++)
1318 if (strcmp(mod->syms[i].name, name) == 0)
1324 static char elf_type(const Elf_Sym *sym,
1326 const char *secstrings,
1329 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1330 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1335 if (sym->st_shndx == SHN_UNDEF)
1337 if (sym->st_shndx == SHN_ABS)
1339 if (sym->st_shndx >= SHN_LORESERVE)
1341 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1343 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1344 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1345 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1347 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1352 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1353 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1358 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1359 ".debug", strlen(".debug")) == 0)
1364 static void add_kallsyms(struct module *mod,
1366 unsigned int symindex,
1367 unsigned int strindex,
1368 const char *secstrings)
1372 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1373 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1374 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1376 /* Set types up while we still have access to sections. */
1377 for (i = 0; i < mod->num_symtab; i++)
1378 mod->symtab[i].st_info
1379 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1382 static inline void add_kallsyms(struct module *mod,
1384 unsigned int symindex,
1385 unsigned int strindex,
1386 const char *secstrings)
1389 #endif /* CONFIG_KALLSYMS */
1391 /* Allocate and load the module: note that size of section 0 is always
1392 zero, and we rely on this for optional sections. */
1393 static struct module *load_module(void __user *umod,
1395 const char __user *uargs)
1399 char *secstrings, *args, *modmagic, *strtab = NULL;
1400 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1401 exportindex, modindex, obsparmindex, infoindex, gplindex,
1402 crcindex, gplcrcindex, versindex, pcpuindex;
1406 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1407 struct exception_table_entry *extable;
1409 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1411 if (len < sizeof(*hdr))
1412 return ERR_PTR(-ENOEXEC);
1414 /* Suck in entire file: we'll want most of it. */
1415 /* vmalloc barfs on "unusual" numbers. Check here */
1416 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1417 return ERR_PTR(-ENOMEM);
1418 if (copy_from_user(hdr, umod, len) != 0) {
1423 /* Sanity checks against insmoding binaries or wrong arch,
1424 weird elf version */
1425 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1426 || hdr->e_type != ET_REL
1427 || !elf_check_arch(hdr)
1428 || hdr->e_shentsize != sizeof(*sechdrs)) {
1433 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1436 /* Convenience variables */
1437 sechdrs = (void *)hdr + hdr->e_shoff;
1438 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1439 sechdrs[0].sh_addr = 0;
1441 for (i = 1; i < hdr->e_shnum; i++) {
1442 if (sechdrs[i].sh_type != SHT_NOBITS
1443 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1446 /* Mark all sections sh_addr with their address in the
1448 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1450 /* Internal symbols and strings. */
1451 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1453 strindex = sechdrs[i].sh_link;
1454 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1456 #ifndef CONFIG_MODULE_UNLOAD
1457 /* Don't load .exit sections */
1458 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1459 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1463 modindex = find_sec(hdr, sechdrs, secstrings,
1464 ".gnu.linkonce.this_module");
1466 printk(KERN_WARNING "No module found in object\n");
1470 mod = (void *)sechdrs[modindex].sh_addr;
1472 if (symindex == 0) {
1473 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1479 /* Optional sections */
1480 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1481 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1482 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1483 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1484 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1485 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1486 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1487 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1488 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1489 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1491 /* Don't keep modinfo section */
1492 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1493 #ifdef CONFIG_KALLSYMS
1494 /* Keep symbol and string tables for decoding later. */
1495 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1496 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1499 /* Check module struct version now, before we try to use module. */
1500 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1505 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1506 /* This is allowed: modprobe --force will invalidate it. */
1508 tainted |= TAINT_FORCED_MODULE;
1509 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1511 } else if (!same_magic(modmagic, vermagic)) {
1512 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1513 mod->name, modmagic, vermagic);
1518 /* Now copy in args */
1519 arglen = strlen_user(uargs);
1524 args = kmalloc(arglen, GFP_KERNEL);
1529 if (copy_from_user(args, uargs, arglen) != 0) {
1534 if (find_module(mod->name)) {
1539 mod->state = MODULE_STATE_COMING;
1541 /* Allow arches to frob section contents and sizes. */
1542 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1547 /* We have a special allocation for this section. */
1548 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1549 sechdrs[pcpuindex].sh_addralign);
1554 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1555 mod->percpu = percpu;
1558 /* Determine total sizes, and put offsets in sh_entsize. For now
1559 this is done generically; there doesn't appear to be any
1560 special cases for the architectures. */
1561 layout_sections(mod, hdr, sechdrs, secstrings);
1563 /* Do the allocs. */
1564 ptr = module_alloc(mod->core_size);
1569 memset(ptr, 0, mod->core_size);
1570 mod->module_core = ptr;
1572 ptr = module_alloc(mod->init_size);
1573 if (!ptr && mod->init_size) {
1577 memset(ptr, 0, mod->init_size);
1578 mod->module_init = ptr;
1580 /* Transfer each section which specifies SHF_ALLOC */
1581 DEBUGP("final section addresses:\n");
1582 for (i = 0; i < hdr->e_shnum; i++) {
1585 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1588 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1589 dest = mod->module_init
1590 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1592 dest = mod->module_core + sechdrs[i].sh_entsize;
1594 if (sechdrs[i].sh_type != SHT_NOBITS)
1595 memcpy(dest, (void *)sechdrs[i].sh_addr,
1596 sechdrs[i].sh_size);
1597 /* Update sh_addr to point to copy in image. */
1598 sechdrs[i].sh_addr = (unsigned long)dest;
1599 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1601 /* Module has been moved. */
1602 mod = (void *)sechdrs[modindex].sh_addr;
1604 /* Now we've moved module, initialize linked lists, etc. */
1605 module_unload_init(mod);
1607 /* Set up license info based on the info section */
1608 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1610 /* Fix up syms, so that st_value is a pointer to location. */
1611 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1616 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1617 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1618 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1620 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1621 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1622 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1624 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1626 #ifdef CONFIG_MODVERSIONS
1627 if ((mod->num_syms && !crcindex) ||
1628 (mod->num_gpl_syms && !gplcrcindex)) {
1629 printk(KERN_WARNING "%s: No versions for exported symbols."
1630 " Tainting kernel.\n", mod->name);
1631 tainted |= TAINT_FORCED_MODULE;
1635 /* Now do relocations. */
1636 for (i = 1; i < hdr->e_shnum; i++) {
1637 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1638 unsigned int info = sechdrs[i].sh_info;
1640 /* Not a valid relocation section? */
1641 if (info >= hdr->e_shnum)
1644 /* Don't bother with non-allocated sections */
1645 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1648 if (sechdrs[i].sh_type == SHT_REL)
1649 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1650 else if (sechdrs[i].sh_type == SHT_RELA)
1651 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1657 /* Set up and sort exception table */
1658 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1659 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1660 sort_extable(extable, extable + mod->num_exentries);
1662 /* Finally, copy percpu area over. */
1663 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1664 sechdrs[pcpuindex].sh_size);
1666 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1668 err = module_finalize(hdr, sechdrs, mod);
1674 err = obsolete_params(mod->name, mod->args,
1675 (struct obsolete_modparm *)
1676 sechdrs[obsparmindex].sh_addr,
1677 sechdrs[obsparmindex].sh_size
1678 / sizeof(struct obsolete_modparm),
1680 (char *)sechdrs[strindex].sh_addr);
1682 printk(KERN_WARNING "%s: Ignoring new-style "
1683 "parameters in presence of obsolete ones\n",
1686 /* Size of section 0 is 0, so this works well if no params */
1687 err = parse_args(mod->name, mod->args,
1688 (struct kernel_param *)
1689 sechdrs[setupindex].sh_addr,
1690 sechdrs[setupindex].sh_size
1691 / sizeof(struct kernel_param),
1697 err = mod_sysfs_setup(mod,
1698 (struct kernel_param *)
1699 sechdrs[setupindex].sh_addr,
1700 sechdrs[setupindex].sh_size
1701 / sizeof(struct kernel_param));
1704 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1706 /* Get rid of temporary copy */
1713 module_arch_cleanup(mod);
1715 module_unload_free(mod);
1716 module_free(mod, mod->module_init);
1718 module_free(mod, mod->module_core);
1721 percpu_modfree(percpu);
1726 if (err < 0) return ERR_PTR(err);
1730 printk(KERN_ERR "Module len %lu truncated\n", len);
1735 /* This is where the real work happens */
1737 sys_init_module(void __user *umod,
1739 const char __user *uargs)
1744 /* Must have permission */
1745 if (!capable(CAP_SYS_MODULE))
1748 /* Only one module load at a time, please */
1749 if (down_interruptible(&module_mutex) != 0)
1752 /* Do all the hard work */
1753 mod = load_module(umod, len, uargs);
1756 return PTR_ERR(mod);
1759 /* Flush the instruction cache, since we've played with text */
1760 if (mod->module_init)
1761 flush_icache_range((unsigned long)mod->module_init,
1762 (unsigned long)mod->module_init
1764 flush_icache_range((unsigned long)mod->module_core,
1765 (unsigned long)mod->module_core + mod->core_size);
1767 /* Now sew it into the lists. They won't access us, since
1768 strong_try_module_get() will fail. */
1769 spin_lock_irq(&modlist_lock);
1770 list_add(&mod->list, &modules);
1771 spin_unlock_irq(&modlist_lock);
1773 /* Drop lock so they can recurse */
1776 down(¬ify_mutex);
1777 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1780 /* Start the module */
1781 if (mod->init != NULL)
1784 /* Init routine failed: abort. Try to protect us from
1785 buggy refcounters. */
1786 mod->state = MODULE_STATE_GOING;
1787 synchronize_kernel();
1789 printk(KERN_ERR "%s: module is now stuck!\n",
1793 down(&module_mutex);
1800 /* Now it's a first class citizen! */
1801 down(&module_mutex);
1802 mod->state = MODULE_STATE_LIVE;
1803 /* Drop initial reference. */
1805 module_free(mod, mod->module_init);
1806 mod->module_init = NULL;
1808 mod->init_text_size = 0;
1814 static inline int within(unsigned long addr, void *start, unsigned long size)
1816 return ((void *)addr >= start && (void *)addr < start + size);
1819 #ifdef CONFIG_KALLSYMS
1821 * This ignores the intensely annoying "mapping symbols" found
1822 * in ARM ELF files: $a, $t and $d.
1824 static inline int is_arm_mapping_symbol(const char *str)
1826 return str[0] == '$' && strchr("atd", str[1])
1827 && (str[2] == '\0' || str[2] == '.');
1830 static const char *get_ksymbol(struct module *mod,
1832 unsigned long *size,
1833 unsigned long *offset)
1835 unsigned int i, best = 0;
1836 unsigned long nextval;
1838 /* At worse, next value is at end of module */
1839 if (within(addr, mod->module_init, mod->init_size))
1840 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1842 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1844 /* Scan for closest preceeding symbol, and next symbol. (ELF
1845 starts real symbols at 1). */
1846 for (i = 1; i < mod->num_symtab; i++) {
1847 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1850 /* We ignore unnamed symbols: they're uninformative
1851 * and inserted at a whim. */
1852 if (mod->symtab[i].st_value <= addr
1853 && mod->symtab[i].st_value > mod->symtab[best].st_value
1854 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1855 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1857 if (mod->symtab[i].st_value > addr
1858 && mod->symtab[i].st_value < nextval
1859 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1860 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1861 nextval = mod->symtab[i].st_value;
1867 *size = nextval - mod->symtab[best].st_value;
1868 *offset = addr - mod->symtab[best].st_value;
1869 return mod->strtab + mod->symtab[best].st_name;
1872 /* For kallsyms to ask for address resolution. NULL means not found.
1873 We don't lock, as this is used for oops resolution and races are a
1875 const char *module_address_lookup(unsigned long addr,
1876 unsigned long *size,
1877 unsigned long *offset,
1882 list_for_each_entry(mod, &modules, list) {
1883 if (within(addr, mod->module_init, mod->init_size)
1884 || within(addr, mod->module_core, mod->core_size)) {
1885 *modname = mod->name;
1886 return get_ksymbol(mod, addr, size, offset);
1892 struct module *module_get_kallsym(unsigned int symnum,
1893 unsigned long *value,
1899 down(&module_mutex);
1900 list_for_each_entry(mod, &modules, list) {
1901 if (symnum < mod->num_symtab) {
1902 *value = mod->symtab[symnum].st_value;
1903 *type = mod->symtab[symnum].st_info;
1905 mod->strtab + mod->symtab[symnum].st_name,
1910 symnum -= mod->num_symtab;
1916 static unsigned long mod_find_symname(struct module *mod, const char *name)
1920 for (i = 0; i < mod->num_symtab; i++)
1921 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
1922 return mod->symtab[i].st_value;
1926 /* Look for this name: can be of form module:name. */
1927 unsigned long module_kallsyms_lookup_name(const char *name)
1931 unsigned long ret = 0;
1933 /* Don't lock: we're in enough trouble already. */
1934 if ((colon = strchr(name, ':')) != NULL) {
1936 if ((mod = find_module(name)) != NULL)
1937 ret = mod_find_symname(mod, colon+1);
1940 list_for_each_entry(mod, &modules, list)
1941 if ((ret = mod_find_symname(mod, name)) != 0)
1946 #endif /* CONFIG_KALLSYMS */
1948 /* Called by the /proc file system to return a list of modules. */
1949 static void *m_start(struct seq_file *m, loff_t *pos)
1951 struct list_head *i;
1954 down(&module_mutex);
1955 list_for_each(i, &modules) {
1964 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
1966 struct list_head *i = p;
1968 if (i->next == &modules)
1973 static void m_stop(struct seq_file *m, void *p)
1978 static int m_show(struct seq_file *m, void *p)
1980 struct module *mod = list_entry(p, struct module, list);
1981 seq_printf(m, "%s %lu",
1982 mod->name, mod->init_size + mod->core_size);
1983 print_unload_info(m, mod);
1985 /* Informative for users. */
1986 seq_printf(m, " %s",
1987 mod->state == MODULE_STATE_GOING ? "Unloading":
1988 mod->state == MODULE_STATE_COMING ? "Loading":
1990 /* Used by oprofile and other similar tools. */
1991 seq_printf(m, " 0x%p", mod->module_core);
1993 seq_printf(m, "\n");
1997 /* Format: modulename size refcount deps address
1999 Where refcount is a number or -, and deps is a comma-separated list
2002 struct seq_operations modules_op = {
2009 /* Given an address, look for it in the module exception tables. */
2010 const struct exception_table_entry *search_module_extables(unsigned long addr)
2012 unsigned long flags;
2013 const struct exception_table_entry *e = NULL;
2016 spin_lock_irqsave(&modlist_lock, flags);
2017 list_for_each_entry(mod, &modules, list) {
2018 if (mod->num_exentries == 0)
2021 e = search_extable(mod->extable,
2022 mod->extable + mod->num_exentries - 1,
2027 spin_unlock_irqrestore(&modlist_lock, flags);
2029 /* Now, if we found one, we are running inside it now, hence
2030 we cannot unload the module, hence no refcnt needed. */
2034 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2035 struct module *__module_text_address(unsigned long addr)
2039 list_for_each_entry(mod, &modules, list)
2040 if (within(addr, mod->module_init, mod->init_text_size)
2041 || within(addr, mod->module_core, mod->core_text_size))
2046 struct module *module_text_address(unsigned long addr)
2049 unsigned long flags;
2051 spin_lock_irqsave(&modlist_lock, flags);
2052 mod = __module_text_address(addr);
2053 spin_unlock_irqrestore(&modlist_lock, flags);
2058 /* Don't grab lock, we're oopsing. */
2059 void print_modules(void)
2063 printk("Modules linked in:");
2064 list_for_each_entry(mod, &modules, list)
2065 printk(" %s", mod->name);
2069 void module_add_driver(struct module *mod, struct device_driver *drv)
2074 /* Don't check return code; this call is idempotent */
2075 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2077 EXPORT_SYMBOL(module_add_driver);
2079 void module_remove_driver(struct device_driver *drv)
2083 sysfs_remove_link(&drv->kobj, "module");
2085 EXPORT_SYMBOL(module_remove_driver);
2087 #ifdef CONFIG_MODVERSIONS
2088 /* Generate the signature for struct module here, too, for modversions. */
2089 void struct_module(struct module *mod) { return; }
2090 EXPORT_SYMBOL(struct_module);