MAINTAINERS: Add cxacru website/mailing list
[linux-2.6/mini2440.git] / kernel / module.c
blob9bd93de01f4a95ab6abb0729401bf424cf5fab21
1 /*
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/module.h>
20 #include <linux/moduleloader.h>
21 #include <linux/init.h>
22 #include <linux/kallsyms.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/elf.h>
27 #include <linux/seq_file.h>
28 #include <linux/syscalls.h>
29 #include <linux/fcntl.h>
30 #include <linux/rcupdate.h>
31 #include <linux/capability.h>
32 #include <linux/cpu.h>
33 #include <linux/moduleparam.h>
34 #include <linux/errno.h>
35 #include <linux/err.h>
36 #include <linux/vermagic.h>
37 #include <linux/notifier.h>
38 #include <linux/sched.h>
39 #include <linux/stop_machine.h>
40 #include <linux/device.h>
41 #include <linux/string.h>
42 #include <linux/mutex.h>
43 #include <linux/unwind.h>
44 #include <asm/uaccess.h>
45 #include <asm/semaphore.h>
46 #include <asm/cacheflush.h>
47 #include <linux/license.h>
49 extern int module_sysfs_initialized;
51 #if 0
52 #define DEBUGP printk
53 #else
54 #define DEBUGP(fmt , a...)
55 #endif
57 #ifndef ARCH_SHF_SMALL
58 #define ARCH_SHF_SMALL 0
59 #endif
61 /* If this is set, the section belongs in the init part of the module */
62 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
64 /* Protects module list */
65 static DEFINE_SPINLOCK(modlist_lock);
67 /* List of modules, protected by module_mutex AND modlist_lock */
68 static DEFINE_MUTEX(module_mutex);
69 static LIST_HEAD(modules);
71 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
73 int register_module_notifier(struct notifier_block * nb)
75 return blocking_notifier_chain_register(&module_notify_list, nb);
77 EXPORT_SYMBOL(register_module_notifier);
79 int unregister_module_notifier(struct notifier_block * nb)
81 return blocking_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)
89 return 0;
90 return try_module_get(mod);
93 static inline void add_taint_module(struct module *mod, unsigned flag)
95 add_taint(flag);
96 mod->taints |= flag;
100 * A thread that wants to hold a reference to a module only while it
101 * is running can call this to safely exit. nfsd and lockd use this.
103 void __module_put_and_exit(struct module *mod, long code)
105 module_put(mod);
106 do_exit(code);
108 EXPORT_SYMBOL(__module_put_and_exit);
110 /* Find a module section: 0 means not found. */
111 static unsigned int find_sec(Elf_Ehdr *hdr,
112 Elf_Shdr *sechdrs,
113 const char *secstrings,
114 const char *name)
116 unsigned int i;
118 for (i = 1; i < hdr->e_shnum; i++)
119 /* Alloc bit cleared means "ignore it." */
120 if ((sechdrs[i].sh_flags & SHF_ALLOC)
121 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
122 return i;
123 return 0;
126 /* Provided by the linker */
127 extern const struct kernel_symbol __start___ksymtab[];
128 extern const struct kernel_symbol __stop___ksymtab[];
129 extern const struct kernel_symbol __start___ksymtab_gpl[];
130 extern const struct kernel_symbol __stop___ksymtab_gpl[];
131 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
132 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
133 extern const struct kernel_symbol __start___ksymtab_unused[];
134 extern const struct kernel_symbol __stop___ksymtab_unused[];
135 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
136 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
137 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
138 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
139 extern const unsigned long __start___kcrctab[];
140 extern const unsigned long __start___kcrctab_gpl[];
141 extern const unsigned long __start___kcrctab_gpl_future[];
142 extern const unsigned long __start___kcrctab_unused[];
143 extern const unsigned long __start___kcrctab_unused_gpl[];
145 #ifndef CONFIG_MODVERSIONS
146 #define symversion(base, idx) NULL
147 #else
148 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
149 #endif
151 /* lookup symbol in given range of kernel_symbols */
152 static const struct kernel_symbol *lookup_symbol(const char *name,
153 const struct kernel_symbol *start,
154 const struct kernel_symbol *stop)
156 const struct kernel_symbol *ks = start;
157 for (; ks < stop; ks++)
158 if (strcmp(ks->name, name) == 0)
159 return ks;
160 return NULL;
163 static void printk_unused_warning(const char *name)
165 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
166 "however this module is using it.\n", name);
167 printk(KERN_WARNING "This symbol will go away in the future.\n");
168 printk(KERN_WARNING "Please evalute if this is the right api to use, "
169 "and if it really is, submit a report the linux kernel "
170 "mailinglist together with submitting your code for "
171 "inclusion.\n");
174 /* Find a symbol, return value, crc and module which owns it */
175 static unsigned long __find_symbol(const char *name,
176 struct module **owner,
177 const unsigned long **crc,
178 int gplok)
180 struct module *mod;
181 const struct kernel_symbol *ks;
183 /* Core kernel first. */
184 *owner = NULL;
185 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
186 if (ks) {
187 *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
188 return ks->value;
190 if (gplok) {
191 ks = lookup_symbol(name, __start___ksymtab_gpl,
192 __stop___ksymtab_gpl);
193 if (ks) {
194 *crc = symversion(__start___kcrctab_gpl,
195 (ks - __start___ksymtab_gpl));
196 return ks->value;
199 ks = lookup_symbol(name, __start___ksymtab_gpl_future,
200 __stop___ksymtab_gpl_future);
201 if (ks) {
202 if (!gplok) {
203 printk(KERN_WARNING "Symbol %s is being used "
204 "by a non-GPL module, which will not "
205 "be allowed in the future\n", name);
206 printk(KERN_WARNING "Please see the file "
207 "Documentation/feature-removal-schedule.txt "
208 "in the kernel source tree for more "
209 "details.\n");
211 *crc = symversion(__start___kcrctab_gpl_future,
212 (ks - __start___ksymtab_gpl_future));
213 return ks->value;
216 ks = lookup_symbol(name, __start___ksymtab_unused,
217 __stop___ksymtab_unused);
218 if (ks) {
219 printk_unused_warning(name);
220 *crc = symversion(__start___kcrctab_unused,
221 (ks - __start___ksymtab_unused));
222 return ks->value;
225 if (gplok)
226 ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
227 __stop___ksymtab_unused_gpl);
228 if (ks) {
229 printk_unused_warning(name);
230 *crc = symversion(__start___kcrctab_unused_gpl,
231 (ks - __start___ksymtab_unused_gpl));
232 return ks->value;
235 /* Now try modules. */
236 list_for_each_entry(mod, &modules, list) {
237 *owner = mod;
238 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
239 if (ks) {
240 *crc = symversion(mod->crcs, (ks - mod->syms));
241 return ks->value;
244 if (gplok) {
245 ks = lookup_symbol(name, mod->gpl_syms,
246 mod->gpl_syms + mod->num_gpl_syms);
247 if (ks) {
248 *crc = symversion(mod->gpl_crcs,
249 (ks - mod->gpl_syms));
250 return ks->value;
253 ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
254 if (ks) {
255 printk_unused_warning(name);
256 *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
257 return ks->value;
260 if (gplok) {
261 ks = lookup_symbol(name, mod->unused_gpl_syms,
262 mod->unused_gpl_syms + mod->num_unused_gpl_syms);
263 if (ks) {
264 printk_unused_warning(name);
265 *crc = symversion(mod->unused_gpl_crcs,
266 (ks - mod->unused_gpl_syms));
267 return ks->value;
270 ks = lookup_symbol(name, mod->gpl_future_syms,
271 (mod->gpl_future_syms +
272 mod->num_gpl_future_syms));
273 if (ks) {
274 if (!gplok) {
275 printk(KERN_WARNING "Symbol %s is being used "
276 "by a non-GPL module, which will not "
277 "be allowed in the future\n", name);
278 printk(KERN_WARNING "Please see the file "
279 "Documentation/feature-removal-schedule.txt "
280 "in the kernel source tree for more "
281 "details.\n");
283 *crc = symversion(mod->gpl_future_crcs,
284 (ks - mod->gpl_future_syms));
285 return ks->value;
288 DEBUGP("Failed to find symbol %s\n", name);
289 return 0;
292 /* Search for module by name: must hold module_mutex. */
293 static struct module *find_module(const char *name)
295 struct module *mod;
297 list_for_each_entry(mod, &modules, list) {
298 if (strcmp(mod->name, name) == 0)
299 return mod;
301 return NULL;
304 #ifdef CONFIG_SMP
305 /* Number of blocks used and allocated. */
306 static unsigned int pcpu_num_used, pcpu_num_allocated;
307 /* Size of each block. -ve means used. */
308 static int *pcpu_size;
310 static int split_block(unsigned int i, unsigned short size)
312 /* Reallocation required? */
313 if (pcpu_num_used + 1 > pcpu_num_allocated) {
314 int *new;
316 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
317 GFP_KERNEL);
318 if (!new)
319 return 0;
321 pcpu_num_allocated *= 2;
322 pcpu_size = new;
325 /* Insert a new subblock */
326 memmove(&pcpu_size[i+1], &pcpu_size[i],
327 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
328 pcpu_num_used++;
330 pcpu_size[i+1] -= size;
331 pcpu_size[i] = size;
332 return 1;
335 static inline unsigned int block_size(int val)
337 if (val < 0)
338 return -val;
339 return val;
342 /* Created by linker magic */
343 extern char __per_cpu_start[], __per_cpu_end[];
345 static void *percpu_modalloc(unsigned long size, unsigned long align,
346 const char *name)
348 unsigned long extra;
349 unsigned int i;
350 void *ptr;
352 if (align > PAGE_SIZE) {
353 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
354 name, align, PAGE_SIZE);
355 align = PAGE_SIZE;
358 ptr = __per_cpu_start;
359 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
360 /* Extra for alignment requirement. */
361 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
362 BUG_ON(i == 0 && extra != 0);
364 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
365 continue;
367 /* Transfer extra to previous block. */
368 if (pcpu_size[i-1] < 0)
369 pcpu_size[i-1] -= extra;
370 else
371 pcpu_size[i-1] += extra;
372 pcpu_size[i] -= extra;
373 ptr += extra;
375 /* Split block if warranted */
376 if (pcpu_size[i] - size > sizeof(unsigned long))
377 if (!split_block(i, size))
378 return NULL;
380 /* Mark allocated */
381 pcpu_size[i] = -pcpu_size[i];
382 return ptr;
385 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
386 size);
387 return NULL;
390 static void percpu_modfree(void *freeme)
392 unsigned int i;
393 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
395 /* First entry is core kernel percpu data. */
396 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
397 if (ptr == freeme) {
398 pcpu_size[i] = -pcpu_size[i];
399 goto free;
402 BUG();
404 free:
405 /* Merge with previous? */
406 if (pcpu_size[i-1] >= 0) {
407 pcpu_size[i-1] += pcpu_size[i];
408 pcpu_num_used--;
409 memmove(&pcpu_size[i], &pcpu_size[i+1],
410 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
411 i--;
413 /* Merge with next? */
414 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
415 pcpu_size[i] += pcpu_size[i+1];
416 pcpu_num_used--;
417 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
418 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
422 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
423 Elf_Shdr *sechdrs,
424 const char *secstrings)
426 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
429 static int percpu_modinit(void)
431 pcpu_num_used = 2;
432 pcpu_num_allocated = 2;
433 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
434 GFP_KERNEL);
435 /* Static in-kernel percpu data (used). */
436 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
437 /* Free room. */
438 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
439 if (pcpu_size[1] < 0) {
440 printk(KERN_ERR "No per-cpu room for modules.\n");
441 pcpu_num_used = 1;
444 return 0;
446 __initcall(percpu_modinit);
447 #else /* ... !CONFIG_SMP */
448 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
449 const char *name)
451 return NULL;
453 static inline void percpu_modfree(void *pcpuptr)
455 BUG();
457 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
458 Elf_Shdr *sechdrs,
459 const char *secstrings)
461 return 0;
463 static inline void percpu_modcopy(void *pcpudst, const void *src,
464 unsigned long size)
466 /* pcpusec should be 0, and size of that section should be 0. */
467 BUG_ON(size != 0);
469 #endif /* CONFIG_SMP */
471 #define MODINFO_ATTR(field) \
472 static void setup_modinfo_##field(struct module *mod, const char *s) \
474 mod->field = kstrdup(s, GFP_KERNEL); \
476 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
477 struct module *mod, char *buffer) \
479 return sprintf(buffer, "%s\n", mod->field); \
481 static int modinfo_##field##_exists(struct module *mod) \
483 return mod->field != NULL; \
485 static void free_modinfo_##field(struct module *mod) \
487 kfree(mod->field); \
488 mod->field = NULL; \
490 static struct module_attribute modinfo_##field = { \
491 .attr = { .name = __stringify(field), .mode = 0444, \
492 .owner = THIS_MODULE }, \
493 .show = show_modinfo_##field, \
494 .setup = setup_modinfo_##field, \
495 .test = modinfo_##field##_exists, \
496 .free = free_modinfo_##field, \
499 MODINFO_ATTR(version);
500 MODINFO_ATTR(srcversion);
502 #ifdef CONFIG_MODULE_UNLOAD
503 /* Init the unload section of the module. */
504 static void module_unload_init(struct module *mod)
506 unsigned int i;
508 INIT_LIST_HEAD(&mod->modules_which_use_me);
509 for (i = 0; i < NR_CPUS; i++)
510 local_set(&mod->ref[i].count, 0);
511 /* Hold reference count during initialization. */
512 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
513 /* Backwards compatibility macros put refcount during init. */
514 mod->waiter = current;
517 /* modules using other modules */
518 struct module_use
520 struct list_head list;
521 struct module *module_which_uses;
524 /* Does a already use b? */
525 static int already_uses(struct module *a, struct module *b)
527 struct module_use *use;
529 list_for_each_entry(use, &b->modules_which_use_me, list) {
530 if (use->module_which_uses == a) {
531 DEBUGP("%s uses %s!\n", a->name, b->name);
532 return 1;
535 DEBUGP("%s does not use %s!\n", a->name, b->name);
536 return 0;
539 /* Module a uses b */
540 static int use_module(struct module *a, struct module *b)
542 struct module_use *use;
543 int no_warn;
545 if (b == NULL || already_uses(a, b)) return 1;
547 if (!strong_try_module_get(b))
548 return 0;
550 DEBUGP("Allocating new usage for %s.\n", a->name);
551 use = kmalloc(sizeof(*use), GFP_ATOMIC);
552 if (!use) {
553 printk("%s: out of memory loading\n", a->name);
554 module_put(b);
555 return 0;
558 use->module_which_uses = a;
559 list_add(&use->list, &b->modules_which_use_me);
560 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
561 return 1;
564 /* Clear the unload stuff of the module. */
565 static void module_unload_free(struct module *mod)
567 struct module *i;
569 list_for_each_entry(i, &modules, list) {
570 struct module_use *use;
572 list_for_each_entry(use, &i->modules_which_use_me, list) {
573 if (use->module_which_uses == mod) {
574 DEBUGP("%s unusing %s\n", mod->name, i->name);
575 module_put(i);
576 list_del(&use->list);
577 kfree(use);
578 sysfs_remove_link(i->holders_dir, mod->name);
579 /* There can be at most one match. */
580 break;
586 #ifdef CONFIG_MODULE_FORCE_UNLOAD
587 static inline int try_force_unload(unsigned int flags)
589 int ret = (flags & O_TRUNC);
590 if (ret)
591 add_taint(TAINT_FORCED_RMMOD);
592 return ret;
594 #else
595 static inline int try_force_unload(unsigned int flags)
597 return 0;
599 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
601 struct stopref
603 struct module *mod;
604 int flags;
605 int *forced;
608 /* Whole machine is stopped with interrupts off when this runs. */
609 static int __try_stop_module(void *_sref)
611 struct stopref *sref = _sref;
613 /* If it's not unused, quit unless we are told to block. */
614 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
615 if (!(*sref->forced = try_force_unload(sref->flags)))
616 return -EWOULDBLOCK;
619 /* Mark it as dying. */
620 sref->mod->state = MODULE_STATE_GOING;
621 return 0;
624 static int try_stop_module(struct module *mod, int flags, int *forced)
626 struct stopref sref = { mod, flags, forced };
628 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
631 unsigned int module_refcount(struct module *mod)
633 unsigned int i, total = 0;
635 for (i = 0; i < NR_CPUS; i++)
636 total += local_read(&mod->ref[i].count);
637 return total;
639 EXPORT_SYMBOL(module_refcount);
641 /* This exists whether we can unload or not */
642 static void free_module(struct module *mod);
644 static void wait_for_zero_refcount(struct module *mod)
646 /* Since we might sleep for some time, drop the semaphore first */
647 mutex_unlock(&module_mutex);
648 for (;;) {
649 DEBUGP("Looking at refcount...\n");
650 set_current_state(TASK_UNINTERRUPTIBLE);
651 if (module_refcount(mod) == 0)
652 break;
653 schedule();
655 current->state = TASK_RUNNING;
656 mutex_lock(&module_mutex);
659 asmlinkage long
660 sys_delete_module(const char __user *name_user, unsigned int flags)
662 struct module *mod;
663 char name[MODULE_NAME_LEN];
664 int ret, forced = 0;
666 if (!capable(CAP_SYS_MODULE))
667 return -EPERM;
669 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
670 return -EFAULT;
671 name[MODULE_NAME_LEN-1] = '\0';
673 if (mutex_lock_interruptible(&module_mutex) != 0)
674 return -EINTR;
676 mod = find_module(name);
677 if (!mod) {
678 ret = -ENOENT;
679 goto out;
682 if (!list_empty(&mod->modules_which_use_me)) {
683 /* Other modules depend on us: get rid of them first. */
684 ret = -EWOULDBLOCK;
685 goto out;
688 /* Doing init or already dying? */
689 if (mod->state != MODULE_STATE_LIVE) {
690 /* FIXME: if (force), slam module count and wake up
691 waiter --RR */
692 DEBUGP("%s already dying\n", mod->name);
693 ret = -EBUSY;
694 goto out;
697 /* If it has an init func, it must have an exit func to unload */
698 if ((mod->init != NULL && mod->exit == NULL)
699 || mod->unsafe) {
700 forced = try_force_unload(flags);
701 if (!forced) {
702 /* This module can't be removed */
703 ret = -EBUSY;
704 goto out;
708 /* Set this up before setting mod->state */
709 mod->waiter = current;
711 /* Stop the machine so refcounts can't move and disable module. */
712 ret = try_stop_module(mod, flags, &forced);
713 if (ret != 0)
714 goto out;
716 /* Never wait if forced. */
717 if (!forced && module_refcount(mod) != 0)
718 wait_for_zero_refcount(mod);
720 /* Final destruction now noone is using it. */
721 if (mod->exit != NULL) {
722 mutex_unlock(&module_mutex);
723 mod->exit();
724 mutex_lock(&module_mutex);
726 free_module(mod);
728 out:
729 mutex_unlock(&module_mutex);
730 return ret;
733 static void print_unload_info(struct seq_file *m, struct module *mod)
735 struct module_use *use;
736 int printed_something = 0;
738 seq_printf(m, " %u ", module_refcount(mod));
740 /* Always include a trailing , so userspace can differentiate
741 between this and the old multi-field proc format. */
742 list_for_each_entry(use, &mod->modules_which_use_me, list) {
743 printed_something = 1;
744 seq_printf(m, "%s,", use->module_which_uses->name);
747 if (mod->unsafe) {
748 printed_something = 1;
749 seq_printf(m, "[unsafe],");
752 if (mod->init != NULL && mod->exit == NULL) {
753 printed_something = 1;
754 seq_printf(m, "[permanent],");
757 if (!printed_something)
758 seq_printf(m, "-");
761 void __symbol_put(const char *symbol)
763 struct module *owner;
764 unsigned long flags;
765 const unsigned long *crc;
767 spin_lock_irqsave(&modlist_lock, flags);
768 if (!__find_symbol(symbol, &owner, &crc, 1))
769 BUG();
770 module_put(owner);
771 spin_unlock_irqrestore(&modlist_lock, flags);
773 EXPORT_SYMBOL(__symbol_put);
775 void symbol_put_addr(void *addr)
777 struct module *modaddr;
779 if (core_kernel_text((unsigned long)addr))
780 return;
782 if (!(modaddr = module_text_address((unsigned long)addr)))
783 BUG();
784 module_put(modaddr);
786 EXPORT_SYMBOL_GPL(symbol_put_addr);
788 static ssize_t show_refcnt(struct module_attribute *mattr,
789 struct module *mod, char *buffer)
791 /* sysfs holds a reference */
792 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
795 static struct module_attribute refcnt = {
796 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
797 .show = show_refcnt,
800 void module_put(struct module *module)
802 if (module) {
803 unsigned int cpu = get_cpu();
804 local_dec(&module->ref[cpu].count);
805 /* Maybe they're waiting for us to drop reference? */
806 if (unlikely(!module_is_live(module)))
807 wake_up_process(module->waiter);
808 put_cpu();
811 EXPORT_SYMBOL(module_put);
813 #else /* !CONFIG_MODULE_UNLOAD */
814 static void print_unload_info(struct seq_file *m, struct module *mod)
816 /* We don't know the usage count, or what modules are using. */
817 seq_printf(m, " - -");
820 static inline void module_unload_free(struct module *mod)
824 static inline int use_module(struct module *a, struct module *b)
826 return strong_try_module_get(b);
829 static inline void module_unload_init(struct module *mod)
832 #endif /* CONFIG_MODULE_UNLOAD */
834 static ssize_t show_initstate(struct module_attribute *mattr,
835 struct module *mod, char *buffer)
837 const char *state = "unknown";
839 switch (mod->state) {
840 case MODULE_STATE_LIVE:
841 state = "live";
842 break;
843 case MODULE_STATE_COMING:
844 state = "coming";
845 break;
846 case MODULE_STATE_GOING:
847 state = "going";
848 break;
850 return sprintf(buffer, "%s\n", state);
853 static struct module_attribute initstate = {
854 .attr = { .name = "initstate", .mode = 0444, .owner = THIS_MODULE },
855 .show = show_initstate,
858 static struct module_attribute *modinfo_attrs[] = {
859 &modinfo_version,
860 &modinfo_srcversion,
861 &initstate,
862 #ifdef CONFIG_MODULE_UNLOAD
863 &refcnt,
864 #endif
865 NULL,
868 static const char vermagic[] = VERMAGIC_STRING;
870 #ifdef CONFIG_MODVERSIONS
871 static int check_version(Elf_Shdr *sechdrs,
872 unsigned int versindex,
873 const char *symname,
874 struct module *mod,
875 const unsigned long *crc)
877 unsigned int i, num_versions;
878 struct modversion_info *versions;
880 /* Exporting module didn't supply crcs? OK, we're already tainted. */
881 if (!crc)
882 return 1;
884 versions = (void *) sechdrs[versindex].sh_addr;
885 num_versions = sechdrs[versindex].sh_size
886 / sizeof(struct modversion_info);
888 for (i = 0; i < num_versions; i++) {
889 if (strcmp(versions[i].name, symname) != 0)
890 continue;
892 if (versions[i].crc == *crc)
893 return 1;
894 printk("%s: disagrees about version of symbol %s\n",
895 mod->name, symname);
896 DEBUGP("Found checksum %lX vs module %lX\n",
897 *crc, versions[i].crc);
898 return 0;
900 /* Not in module's version table. OK, but that taints the kernel. */
901 if (!(tainted & TAINT_FORCED_MODULE))
902 printk("%s: no version for \"%s\" found: kernel tainted.\n",
903 mod->name, symname);
904 add_taint_module(mod, TAINT_FORCED_MODULE);
905 return 1;
908 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
909 unsigned int versindex,
910 struct module *mod)
912 const unsigned long *crc;
913 struct module *owner;
915 if (!__find_symbol("struct_module", &owner, &crc, 1))
916 BUG();
917 return check_version(sechdrs, versindex, "struct_module", mod,
918 crc);
921 /* First part is kernel version, which we ignore. */
922 static inline int same_magic(const char *amagic, const char *bmagic)
924 amagic += strcspn(amagic, " ");
925 bmagic += strcspn(bmagic, " ");
926 return strcmp(amagic, bmagic) == 0;
928 #else
929 static inline int check_version(Elf_Shdr *sechdrs,
930 unsigned int versindex,
931 const char *symname,
932 struct module *mod,
933 const unsigned long *crc)
935 return 1;
938 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
939 unsigned int versindex,
940 struct module *mod)
942 return 1;
945 static inline int same_magic(const char *amagic, const char *bmagic)
947 return strcmp(amagic, bmagic) == 0;
949 #endif /* CONFIG_MODVERSIONS */
951 /* Resolve a symbol for this module. I.e. if we find one, record usage.
952 Must be holding module_mutex. */
953 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
954 unsigned int versindex,
955 const char *name,
956 struct module *mod)
958 struct module *owner;
959 unsigned long ret;
960 const unsigned long *crc;
962 ret = __find_symbol(name, &owner, &crc,
963 !(mod->taints & TAINT_PROPRIETARY_MODULE));
964 if (ret) {
965 /* use_module can fail due to OOM, or module unloading */
966 if (!check_version(sechdrs, versindex, name, mod, crc) ||
967 !use_module(mod, owner))
968 ret = 0;
970 return ret;
975 * /sys/module/foo/sections stuff
976 * J. Corbet <corbet@lwn.net>
978 #ifdef CONFIG_KALLSYMS
979 static ssize_t module_sect_show(struct module_attribute *mattr,
980 struct module *mod, char *buf)
982 struct module_sect_attr *sattr =
983 container_of(mattr, struct module_sect_attr, mattr);
984 return sprintf(buf, "0x%lx\n", sattr->address);
987 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
989 int section;
991 for (section = 0; section < sect_attrs->nsections; section++)
992 kfree(sect_attrs->attrs[section].name);
993 kfree(sect_attrs);
996 static void add_sect_attrs(struct module *mod, unsigned int nsect,
997 char *secstrings, Elf_Shdr *sechdrs)
999 unsigned int nloaded = 0, i, size[2];
1000 struct module_sect_attrs *sect_attrs;
1001 struct module_sect_attr *sattr;
1002 struct attribute **gattr;
1004 /* Count loaded sections and allocate structures */
1005 for (i = 0; i < nsect; i++)
1006 if (sechdrs[i].sh_flags & SHF_ALLOC)
1007 nloaded++;
1008 size[0] = ALIGN(sizeof(*sect_attrs)
1009 + nloaded * sizeof(sect_attrs->attrs[0]),
1010 sizeof(sect_attrs->grp.attrs[0]));
1011 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1012 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1013 if (sect_attrs == NULL)
1014 return;
1016 /* Setup section attributes. */
1017 sect_attrs->grp.name = "sections";
1018 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1020 sect_attrs->nsections = 0;
1021 sattr = &sect_attrs->attrs[0];
1022 gattr = &sect_attrs->grp.attrs[0];
1023 for (i = 0; i < nsect; i++) {
1024 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1025 continue;
1026 sattr->address = sechdrs[i].sh_addr;
1027 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1028 GFP_KERNEL);
1029 if (sattr->name == NULL)
1030 goto out;
1031 sect_attrs->nsections++;
1032 sattr->mattr.show = module_sect_show;
1033 sattr->mattr.store = NULL;
1034 sattr->mattr.attr.name = sattr->name;
1035 sattr->mattr.attr.owner = mod;
1036 sattr->mattr.attr.mode = S_IRUGO;
1037 *(gattr++) = &(sattr++)->mattr.attr;
1039 *gattr = NULL;
1041 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1042 goto out;
1044 mod->sect_attrs = sect_attrs;
1045 return;
1046 out:
1047 free_sect_attrs(sect_attrs);
1050 static void remove_sect_attrs(struct module *mod)
1052 if (mod->sect_attrs) {
1053 sysfs_remove_group(&mod->mkobj.kobj,
1054 &mod->sect_attrs->grp);
1055 /* We are positive that no one is using any sect attrs
1056 * at this point. Deallocate immediately. */
1057 free_sect_attrs(mod->sect_attrs);
1058 mod->sect_attrs = NULL;
1062 #else
1064 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1065 char *sectstrings, Elf_Shdr *sechdrs)
1069 static inline void remove_sect_attrs(struct module *mod)
1072 #endif /* CONFIG_KALLSYMS */
1074 #ifdef CONFIG_SYSFS
1075 int module_add_modinfo_attrs(struct module *mod)
1077 struct module_attribute *attr;
1078 struct module_attribute *temp_attr;
1079 int error = 0;
1080 int i;
1082 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1083 (ARRAY_SIZE(modinfo_attrs) + 1)),
1084 GFP_KERNEL);
1085 if (!mod->modinfo_attrs)
1086 return -ENOMEM;
1088 temp_attr = mod->modinfo_attrs;
1089 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1090 if (!attr->test ||
1091 (attr->test && attr->test(mod))) {
1092 memcpy(temp_attr, attr, sizeof(*temp_attr));
1093 temp_attr->attr.owner = mod;
1094 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1095 ++temp_attr;
1098 return error;
1101 void module_remove_modinfo_attrs(struct module *mod)
1103 struct module_attribute *attr;
1104 int i;
1106 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1107 /* pick a field to test for end of list */
1108 if (!attr->attr.name)
1109 break;
1110 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1111 if (attr->free)
1112 attr->free(mod);
1114 kfree(mod->modinfo_attrs);
1116 #endif
1118 #ifdef CONFIG_SYSFS
1119 int mod_sysfs_init(struct module *mod)
1121 int err;
1123 if (!module_sysfs_initialized) {
1124 printk(KERN_ERR "%s: module sysfs not initialized\n",
1125 mod->name);
1126 err = -EINVAL;
1127 goto out;
1129 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1130 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1131 if (err)
1132 goto out;
1133 kobj_set_kset_s(&mod->mkobj, module_subsys);
1134 mod->mkobj.mod = mod;
1136 kobject_init(&mod->mkobj.kobj);
1138 out:
1139 return err;
1142 int mod_sysfs_setup(struct module *mod,
1143 struct kernel_param *kparam,
1144 unsigned int num_params)
1146 int err;
1148 /* delay uevent until full sysfs population */
1149 err = kobject_add(&mod->mkobj.kobj);
1150 if (err)
1151 goto out;
1153 mod->holders_dir = kobject_add_dir(&mod->mkobj.kobj, "holders");
1154 if (!mod->holders_dir) {
1155 err = -ENOMEM;
1156 goto out_unreg;
1159 err = module_param_sysfs_setup(mod, kparam, num_params);
1160 if (err)
1161 goto out_unreg_holders;
1163 err = module_add_modinfo_attrs(mod);
1164 if (err)
1165 goto out_unreg_param;
1167 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1168 return 0;
1170 out_unreg_param:
1171 module_param_sysfs_remove(mod);
1172 out_unreg_holders:
1173 kobject_unregister(mod->holders_dir);
1174 out_unreg:
1175 kobject_del(&mod->mkobj.kobj);
1176 kobject_put(&mod->mkobj.kobj);
1177 out:
1178 return err;
1180 #endif
1182 static void mod_kobject_remove(struct module *mod)
1184 module_remove_modinfo_attrs(mod);
1185 module_param_sysfs_remove(mod);
1186 kobject_unregister(mod->mkobj.drivers_dir);
1187 kobject_unregister(mod->holders_dir);
1188 kobject_unregister(&mod->mkobj.kobj);
1192 * unlink the module with the whole machine is stopped with interrupts off
1193 * - this defends against kallsyms not taking locks
1195 static int __unlink_module(void *_mod)
1197 struct module *mod = _mod;
1198 list_del(&mod->list);
1199 return 0;
1202 /* Free a module, remove from lists, etc (must hold module_mutex). */
1203 static void free_module(struct module *mod)
1205 /* Delete from various lists */
1206 stop_machine_run(__unlink_module, mod, NR_CPUS);
1207 remove_sect_attrs(mod);
1208 mod_kobject_remove(mod);
1210 unwind_remove_table(mod->unwind_info, 0);
1212 /* Arch-specific cleanup. */
1213 module_arch_cleanup(mod);
1215 /* Module unload stuff */
1216 module_unload_free(mod);
1218 /* This may be NULL, but that's OK */
1219 module_free(mod, mod->module_init);
1220 kfree(mod->args);
1221 if (mod->percpu)
1222 percpu_modfree(mod->percpu);
1224 /* Free lock-classes: */
1225 lockdep_free_key_range(mod->module_core, mod->core_size);
1227 /* Finally, free the core (containing the module structure) */
1228 module_free(mod, mod->module_core);
1231 void *__symbol_get(const char *symbol)
1233 struct module *owner;
1234 unsigned long value, flags;
1235 const unsigned long *crc;
1237 spin_lock_irqsave(&modlist_lock, flags);
1238 value = __find_symbol(symbol, &owner, &crc, 1);
1239 if (value && !strong_try_module_get(owner))
1240 value = 0;
1241 spin_unlock_irqrestore(&modlist_lock, flags);
1243 return (void *)value;
1245 EXPORT_SYMBOL_GPL(__symbol_get);
1248 * Ensure that an exported symbol [global namespace] does not already exist
1249 * in the kernel or in some other module's exported symbol table.
1251 static int verify_export_symbols(struct module *mod)
1253 const char *name = NULL;
1254 unsigned long i, ret = 0;
1255 struct module *owner;
1256 const unsigned long *crc;
1258 for (i = 0; i < mod->num_syms; i++)
1259 if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
1260 name = mod->syms[i].name;
1261 ret = -ENOEXEC;
1262 goto dup;
1265 for (i = 0; i < mod->num_gpl_syms; i++)
1266 if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
1267 name = mod->gpl_syms[i].name;
1268 ret = -ENOEXEC;
1269 goto dup;
1272 dup:
1273 if (ret)
1274 printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
1275 mod->name, name, module_name(owner));
1277 return ret;
1280 /* Change all symbols so that sh_value encodes the pointer directly. */
1281 static int simplify_symbols(Elf_Shdr *sechdrs,
1282 unsigned int symindex,
1283 const char *strtab,
1284 unsigned int versindex,
1285 unsigned int pcpuindex,
1286 struct module *mod)
1288 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1289 unsigned long secbase;
1290 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1291 int ret = 0;
1293 for (i = 1; i < n; i++) {
1294 switch (sym[i].st_shndx) {
1295 case SHN_COMMON:
1296 /* We compiled with -fno-common. These are not
1297 supposed to happen. */
1298 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1299 printk("%s: please compile with -fno-common\n",
1300 mod->name);
1301 ret = -ENOEXEC;
1302 break;
1304 case SHN_ABS:
1305 /* Don't need to do anything */
1306 DEBUGP("Absolute symbol: 0x%08lx\n",
1307 (long)sym[i].st_value);
1308 break;
1310 case SHN_UNDEF:
1311 sym[i].st_value
1312 = resolve_symbol(sechdrs, versindex,
1313 strtab + sym[i].st_name, mod);
1315 /* Ok if resolved. */
1316 if (sym[i].st_value != 0)
1317 break;
1318 /* Ok if weak. */
1319 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1320 break;
1322 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1323 mod->name, strtab + sym[i].st_name);
1324 ret = -ENOENT;
1325 break;
1327 default:
1328 /* Divert to percpu allocation if a percpu var. */
1329 if (sym[i].st_shndx == pcpuindex)
1330 secbase = (unsigned long)mod->percpu;
1331 else
1332 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1333 sym[i].st_value += secbase;
1334 break;
1338 return ret;
1341 /* Update size with this section: return offset. */
1342 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1344 long ret;
1346 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1347 *size = ret + sechdr->sh_size;
1348 return ret;
1351 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1352 might -- code, read-only data, read-write data, small data. Tally
1353 sizes, and place the offsets into sh_entsize fields: high bit means it
1354 belongs in init. */
1355 static void layout_sections(struct module *mod,
1356 const Elf_Ehdr *hdr,
1357 Elf_Shdr *sechdrs,
1358 const char *secstrings)
1360 static unsigned long const masks[][2] = {
1361 /* NOTE: all executable code must be the first section
1362 * in this array; otherwise modify the text_size
1363 * finder in the two loops below */
1364 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1365 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1366 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1367 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1369 unsigned int m, i;
1371 for (i = 0; i < hdr->e_shnum; i++)
1372 sechdrs[i].sh_entsize = ~0UL;
1374 DEBUGP("Core section allocation order:\n");
1375 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1376 for (i = 0; i < hdr->e_shnum; ++i) {
1377 Elf_Shdr *s = &sechdrs[i];
1379 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1380 || (s->sh_flags & masks[m][1])
1381 || s->sh_entsize != ~0UL
1382 || strncmp(secstrings + s->sh_name,
1383 ".init", 5) == 0)
1384 continue;
1385 s->sh_entsize = get_offset(&mod->core_size, s);
1386 DEBUGP("\t%s\n", secstrings + s->sh_name);
1388 if (m == 0)
1389 mod->core_text_size = mod->core_size;
1392 DEBUGP("Init section allocation order:\n");
1393 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1394 for (i = 0; i < hdr->e_shnum; ++i) {
1395 Elf_Shdr *s = &sechdrs[i];
1397 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1398 || (s->sh_flags & masks[m][1])
1399 || s->sh_entsize != ~0UL
1400 || strncmp(secstrings + s->sh_name,
1401 ".init", 5) != 0)
1402 continue;
1403 s->sh_entsize = (get_offset(&mod->init_size, s)
1404 | INIT_OFFSET_MASK);
1405 DEBUGP("\t%s\n", secstrings + s->sh_name);
1407 if (m == 0)
1408 mod->init_text_size = mod->init_size;
1412 static void set_license(struct module *mod, const char *license)
1414 if (!license)
1415 license = "unspecified";
1417 if (!license_is_gpl_compatible(license)) {
1418 if (!(tainted & TAINT_PROPRIETARY_MODULE))
1419 printk(KERN_WARNING "%s: module license '%s' taints "
1420 "kernel.\n", mod->name, license);
1421 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1425 /* Parse tag=value strings from .modinfo section */
1426 static char *next_string(char *string, unsigned long *secsize)
1428 /* Skip non-zero chars */
1429 while (string[0]) {
1430 string++;
1431 if ((*secsize)-- <= 1)
1432 return NULL;
1435 /* Skip any zero padding. */
1436 while (!string[0]) {
1437 string++;
1438 if ((*secsize)-- <= 1)
1439 return NULL;
1441 return string;
1444 static char *get_modinfo(Elf_Shdr *sechdrs,
1445 unsigned int info,
1446 const char *tag)
1448 char *p;
1449 unsigned int taglen = strlen(tag);
1450 unsigned long size = sechdrs[info].sh_size;
1452 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1453 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1454 return p + taglen + 1;
1456 return NULL;
1459 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1460 unsigned int infoindex)
1462 struct module_attribute *attr;
1463 int i;
1465 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1466 if (attr->setup)
1467 attr->setup(mod,
1468 get_modinfo(sechdrs,
1469 infoindex,
1470 attr->attr.name));
1474 #ifdef CONFIG_KALLSYMS
1475 static int is_exported(const char *name, const struct module *mod)
1477 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1478 return 1;
1479 else
1480 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1481 return 1;
1482 else
1483 return 0;
1486 /* As per nm */
1487 static char elf_type(const Elf_Sym *sym,
1488 Elf_Shdr *sechdrs,
1489 const char *secstrings,
1490 struct module *mod)
1492 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1493 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1494 return 'v';
1495 else
1496 return 'w';
1498 if (sym->st_shndx == SHN_UNDEF)
1499 return 'U';
1500 if (sym->st_shndx == SHN_ABS)
1501 return 'a';
1502 if (sym->st_shndx >= SHN_LORESERVE)
1503 return '?';
1504 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1505 return 't';
1506 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1507 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1508 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1509 return 'r';
1510 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1511 return 'g';
1512 else
1513 return 'd';
1515 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1516 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1517 return 's';
1518 else
1519 return 'b';
1521 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1522 ".debug", strlen(".debug")) == 0)
1523 return 'n';
1524 return '?';
1527 static void add_kallsyms(struct module *mod,
1528 Elf_Shdr *sechdrs,
1529 unsigned int symindex,
1530 unsigned int strindex,
1531 const char *secstrings)
1533 unsigned int i;
1535 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1536 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1537 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1539 /* Set types up while we still have access to sections. */
1540 for (i = 0; i < mod->num_symtab; i++)
1541 mod->symtab[i].st_info
1542 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1544 #else
1545 static inline void add_kallsyms(struct module *mod,
1546 Elf_Shdr *sechdrs,
1547 unsigned int symindex,
1548 unsigned int strindex,
1549 const char *secstrings)
1552 #endif /* CONFIG_KALLSYMS */
1554 /* Allocate and load the module: note that size of section 0 is always
1555 zero, and we rely on this for optional sections. */
1556 static struct module *load_module(void __user *umod,
1557 unsigned long len,
1558 const char __user *uargs)
1560 Elf_Ehdr *hdr;
1561 Elf_Shdr *sechdrs;
1562 char *secstrings, *args, *modmagic, *strtab = NULL;
1563 unsigned int i;
1564 unsigned int symindex = 0;
1565 unsigned int strindex = 0;
1566 unsigned int setupindex;
1567 unsigned int exindex;
1568 unsigned int exportindex;
1569 unsigned int modindex;
1570 unsigned int obsparmindex;
1571 unsigned int infoindex;
1572 unsigned int gplindex;
1573 unsigned int crcindex;
1574 unsigned int gplcrcindex;
1575 unsigned int versindex;
1576 unsigned int pcpuindex;
1577 unsigned int gplfutureindex;
1578 unsigned int gplfuturecrcindex;
1579 unsigned int unwindex = 0;
1580 unsigned int unusedindex;
1581 unsigned int unusedcrcindex;
1582 unsigned int unusedgplindex;
1583 unsigned int unusedgplcrcindex;
1584 struct module *mod;
1585 long err = 0;
1586 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1587 struct exception_table_entry *extable;
1588 mm_segment_t old_fs;
1590 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1591 umod, len, uargs);
1592 if (len < sizeof(*hdr))
1593 return ERR_PTR(-ENOEXEC);
1595 /* Suck in entire file: we'll want most of it. */
1596 /* vmalloc barfs on "unusual" numbers. Check here */
1597 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1598 return ERR_PTR(-ENOMEM);
1599 if (copy_from_user(hdr, umod, len) != 0) {
1600 err = -EFAULT;
1601 goto free_hdr;
1604 /* Sanity checks against insmoding binaries or wrong arch,
1605 weird elf version */
1606 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1607 || hdr->e_type != ET_REL
1608 || !elf_check_arch(hdr)
1609 || hdr->e_shentsize != sizeof(*sechdrs)) {
1610 err = -ENOEXEC;
1611 goto free_hdr;
1614 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1615 goto truncated;
1617 /* Convenience variables */
1618 sechdrs = (void *)hdr + hdr->e_shoff;
1619 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1620 sechdrs[0].sh_addr = 0;
1622 for (i = 1; i < hdr->e_shnum; i++) {
1623 if (sechdrs[i].sh_type != SHT_NOBITS
1624 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1625 goto truncated;
1627 /* Mark all sections sh_addr with their address in the
1628 temporary image. */
1629 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1631 /* Internal symbols and strings. */
1632 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1633 symindex = i;
1634 strindex = sechdrs[i].sh_link;
1635 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1637 #ifndef CONFIG_MODULE_UNLOAD
1638 /* Don't load .exit sections */
1639 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1640 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1641 #endif
1644 modindex = find_sec(hdr, sechdrs, secstrings,
1645 ".gnu.linkonce.this_module");
1646 if (!modindex) {
1647 printk(KERN_WARNING "No module found in object\n");
1648 err = -ENOEXEC;
1649 goto free_hdr;
1651 mod = (void *)sechdrs[modindex].sh_addr;
1653 if (symindex == 0) {
1654 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1655 mod->name);
1656 err = -ENOEXEC;
1657 goto free_hdr;
1660 /* Optional sections */
1661 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1662 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1663 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
1664 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1665 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
1666 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1667 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1668 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
1669 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1670 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
1671 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1672 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1673 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1674 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1675 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1676 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1677 #ifdef ARCH_UNWIND_SECTION_NAME
1678 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1679 #endif
1681 /* Don't keep modinfo section */
1682 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1683 #ifdef CONFIG_KALLSYMS
1684 /* Keep symbol and string tables for decoding later. */
1685 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1686 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1687 #endif
1688 if (unwindex)
1689 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
1691 /* Check module struct version now, before we try to use module. */
1692 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1693 err = -ENOEXEC;
1694 goto free_hdr;
1697 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1698 /* This is allowed: modprobe --force will invalidate it. */
1699 if (!modmagic) {
1700 add_taint_module(mod, TAINT_FORCED_MODULE);
1701 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1702 mod->name);
1703 } else if (!same_magic(modmagic, vermagic)) {
1704 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1705 mod->name, modmagic, vermagic);
1706 err = -ENOEXEC;
1707 goto free_hdr;
1710 /* Now copy in args */
1711 args = strndup_user(uargs, ~0UL >> 1);
1712 if (IS_ERR(args)) {
1713 err = PTR_ERR(args);
1714 goto free_hdr;
1717 if (find_module(mod->name)) {
1718 err = -EEXIST;
1719 goto free_mod;
1722 mod->state = MODULE_STATE_COMING;
1724 /* Allow arches to frob section contents and sizes. */
1725 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1726 if (err < 0)
1727 goto free_mod;
1729 if (pcpuindex) {
1730 /* We have a special allocation for this section. */
1731 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1732 sechdrs[pcpuindex].sh_addralign,
1733 mod->name);
1734 if (!percpu) {
1735 err = -ENOMEM;
1736 goto free_mod;
1738 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1739 mod->percpu = percpu;
1742 /* Determine total sizes, and put offsets in sh_entsize. For now
1743 this is done generically; there doesn't appear to be any
1744 special cases for the architectures. */
1745 layout_sections(mod, hdr, sechdrs, secstrings);
1747 /* Do the allocs. */
1748 ptr = module_alloc(mod->core_size);
1749 if (!ptr) {
1750 err = -ENOMEM;
1751 goto free_percpu;
1753 memset(ptr, 0, mod->core_size);
1754 mod->module_core = ptr;
1756 ptr = module_alloc(mod->init_size);
1757 if (!ptr && mod->init_size) {
1758 err = -ENOMEM;
1759 goto free_core;
1761 memset(ptr, 0, mod->init_size);
1762 mod->module_init = ptr;
1764 /* Transfer each section which specifies SHF_ALLOC */
1765 DEBUGP("final section addresses:\n");
1766 for (i = 0; i < hdr->e_shnum; i++) {
1767 void *dest;
1769 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1770 continue;
1772 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1773 dest = mod->module_init
1774 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1775 else
1776 dest = mod->module_core + sechdrs[i].sh_entsize;
1778 if (sechdrs[i].sh_type != SHT_NOBITS)
1779 memcpy(dest, (void *)sechdrs[i].sh_addr,
1780 sechdrs[i].sh_size);
1781 /* Update sh_addr to point to copy in image. */
1782 sechdrs[i].sh_addr = (unsigned long)dest;
1783 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1785 /* Module has been moved. */
1786 mod = (void *)sechdrs[modindex].sh_addr;
1788 /* Now we've moved module, initialize linked lists, etc. */
1789 module_unload_init(mod);
1791 /* Initialize kobject, so we can reference it. */
1792 if (mod_sysfs_init(mod) != 0)
1793 goto cleanup;
1795 /* Set up license info based on the info section */
1796 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1798 if (strcmp(mod->name, "ndiswrapper") == 0)
1799 add_taint(TAINT_PROPRIETARY_MODULE);
1800 if (strcmp(mod->name, "driverloader") == 0)
1801 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1803 /* Set up MODINFO_ATTR fields */
1804 setup_modinfo(mod, sechdrs, infoindex);
1806 /* Fix up syms, so that st_value is a pointer to location. */
1807 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1808 mod);
1809 if (err < 0)
1810 goto cleanup;
1812 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1813 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1814 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1815 if (crcindex)
1816 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1817 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1818 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1819 if (gplcrcindex)
1820 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1821 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
1822 sizeof(*mod->gpl_future_syms);
1823 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
1824 sizeof(*mod->unused_syms);
1825 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
1826 sizeof(*mod->unused_gpl_syms);
1827 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
1828 if (gplfuturecrcindex)
1829 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
1831 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
1832 if (unusedcrcindex)
1833 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
1834 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
1835 if (unusedgplcrcindex)
1836 mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
1838 #ifdef CONFIG_MODVERSIONS
1839 if ((mod->num_syms && !crcindex) ||
1840 (mod->num_gpl_syms && !gplcrcindex) ||
1841 (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
1842 (mod->num_unused_syms && !unusedcrcindex) ||
1843 (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
1844 printk(KERN_WARNING "%s: No versions for exported symbols."
1845 " Tainting kernel.\n", mod->name);
1846 add_taint_module(mod, TAINT_FORCED_MODULE);
1848 #endif
1850 /* Now do relocations. */
1851 for (i = 1; i < hdr->e_shnum; i++) {
1852 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1853 unsigned int info = sechdrs[i].sh_info;
1855 /* Not a valid relocation section? */
1856 if (info >= hdr->e_shnum)
1857 continue;
1859 /* Don't bother with non-allocated sections */
1860 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1861 continue;
1863 if (sechdrs[i].sh_type == SHT_REL)
1864 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1865 else if (sechdrs[i].sh_type == SHT_RELA)
1866 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1867 mod);
1868 if (err < 0)
1869 goto cleanup;
1872 /* Find duplicate symbols */
1873 err = verify_export_symbols(mod);
1875 if (err < 0)
1876 goto cleanup;
1878 /* Set up and sort exception table */
1879 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1880 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1881 sort_extable(extable, extable + mod->num_exentries);
1883 /* Finally, copy percpu area over. */
1884 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1885 sechdrs[pcpuindex].sh_size);
1887 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1889 err = module_finalize(hdr, sechdrs, mod);
1890 if (err < 0)
1891 goto cleanup;
1893 /* flush the icache in correct context */
1894 old_fs = get_fs();
1895 set_fs(KERNEL_DS);
1898 * Flush the instruction cache, since we've played with text.
1899 * Do it before processing of module parameters, so the module
1900 * can provide parameter accessor functions of its own.
1902 if (mod->module_init)
1903 flush_icache_range((unsigned long)mod->module_init,
1904 (unsigned long)mod->module_init
1905 + mod->init_size);
1906 flush_icache_range((unsigned long)mod->module_core,
1907 (unsigned long)mod->module_core + mod->core_size);
1909 set_fs(old_fs);
1911 mod->args = args;
1912 if (obsparmindex)
1913 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
1914 mod->name);
1916 /* Size of section 0 is 0, so this works well if no params */
1917 err = parse_args(mod->name, mod->args,
1918 (struct kernel_param *)
1919 sechdrs[setupindex].sh_addr,
1920 sechdrs[setupindex].sh_size
1921 / sizeof(struct kernel_param),
1922 NULL);
1923 if (err < 0)
1924 goto arch_cleanup;
1926 err = mod_sysfs_setup(mod,
1927 (struct kernel_param *)
1928 sechdrs[setupindex].sh_addr,
1929 sechdrs[setupindex].sh_size
1930 / sizeof(struct kernel_param));
1931 if (err < 0)
1932 goto arch_cleanup;
1933 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1935 /* Size of section 0 is 0, so this works well if no unwind info. */
1936 mod->unwind_info = unwind_add_table(mod,
1937 (void *)sechdrs[unwindex].sh_addr,
1938 sechdrs[unwindex].sh_size);
1940 /* Get rid of temporary copy */
1941 vfree(hdr);
1943 /* Done! */
1944 return mod;
1946 arch_cleanup:
1947 module_arch_cleanup(mod);
1948 cleanup:
1949 module_unload_free(mod);
1950 module_free(mod, mod->module_init);
1951 free_core:
1952 module_free(mod, mod->module_core);
1953 free_percpu:
1954 if (percpu)
1955 percpu_modfree(percpu);
1956 free_mod:
1957 kfree(args);
1958 free_hdr:
1959 vfree(hdr);
1960 return ERR_PTR(err);
1962 truncated:
1963 printk(KERN_ERR "Module len %lu truncated\n", len);
1964 err = -ENOEXEC;
1965 goto free_hdr;
1969 * link the module with the whole machine is stopped with interrupts off
1970 * - this defends against kallsyms not taking locks
1972 static int __link_module(void *_mod)
1974 struct module *mod = _mod;
1975 list_add(&mod->list, &modules);
1976 return 0;
1979 /* This is where the real work happens */
1980 asmlinkage long
1981 sys_init_module(void __user *umod,
1982 unsigned long len,
1983 const char __user *uargs)
1985 struct module *mod;
1986 int ret = 0;
1988 /* Must have permission */
1989 if (!capable(CAP_SYS_MODULE))
1990 return -EPERM;
1992 /* Only one module load at a time, please */
1993 if (mutex_lock_interruptible(&module_mutex) != 0)
1994 return -EINTR;
1996 /* Do all the hard work */
1997 mod = load_module(umod, len, uargs);
1998 if (IS_ERR(mod)) {
1999 mutex_unlock(&module_mutex);
2000 return PTR_ERR(mod);
2003 /* Now sew it into the lists. They won't access us, since
2004 strong_try_module_get() will fail. */
2005 stop_machine_run(__link_module, mod, NR_CPUS);
2007 /* Drop lock so they can recurse */
2008 mutex_unlock(&module_mutex);
2010 blocking_notifier_call_chain(&module_notify_list,
2011 MODULE_STATE_COMING, mod);
2013 /* Start the module */
2014 if (mod->init != NULL)
2015 ret = mod->init();
2016 if (ret < 0) {
2017 /* Init routine failed: abort. Try to protect us from
2018 buggy refcounters. */
2019 mod->state = MODULE_STATE_GOING;
2020 synchronize_sched();
2021 if (mod->unsafe)
2022 printk(KERN_ERR "%s: module is now stuck!\n",
2023 mod->name);
2024 else {
2025 module_put(mod);
2026 mutex_lock(&module_mutex);
2027 free_module(mod);
2028 mutex_unlock(&module_mutex);
2030 return ret;
2033 /* Now it's a first class citizen! */
2034 mutex_lock(&module_mutex);
2035 mod->state = MODULE_STATE_LIVE;
2036 /* Drop initial reference. */
2037 module_put(mod);
2038 unwind_remove_table(mod->unwind_info, 1);
2039 module_free(mod, mod->module_init);
2040 mod->module_init = NULL;
2041 mod->init_size = 0;
2042 mod->init_text_size = 0;
2043 mutex_unlock(&module_mutex);
2045 return 0;
2048 static inline int within(unsigned long addr, void *start, unsigned long size)
2050 return ((void *)addr >= start && (void *)addr < start + size);
2053 #ifdef CONFIG_KALLSYMS
2055 * This ignores the intensely annoying "mapping symbols" found
2056 * in ARM ELF files: $a, $t and $d.
2058 static inline int is_arm_mapping_symbol(const char *str)
2060 return str[0] == '$' && strchr("atd", str[1])
2061 && (str[2] == '\0' || str[2] == '.');
2064 static const char *get_ksymbol(struct module *mod,
2065 unsigned long addr,
2066 unsigned long *size,
2067 unsigned long *offset)
2069 unsigned int i, best = 0;
2070 unsigned long nextval;
2072 /* At worse, next value is at end of module */
2073 if (within(addr, mod->module_init, mod->init_size))
2074 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2075 else
2076 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2078 /* Scan for closest preceeding symbol, and next symbol. (ELF
2079 starts real symbols at 1). */
2080 for (i = 1; i < mod->num_symtab; i++) {
2081 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2082 continue;
2084 /* We ignore unnamed symbols: they're uninformative
2085 * and inserted at a whim. */
2086 if (mod->symtab[i].st_value <= addr
2087 && mod->symtab[i].st_value > mod->symtab[best].st_value
2088 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2089 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2090 best = i;
2091 if (mod->symtab[i].st_value > addr
2092 && mod->symtab[i].st_value < nextval
2093 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2094 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2095 nextval = mod->symtab[i].st_value;
2098 if (!best)
2099 return NULL;
2101 if (size)
2102 *size = nextval - mod->symtab[best].st_value;
2103 if (offset)
2104 *offset = addr - mod->symtab[best].st_value;
2105 return mod->strtab + mod->symtab[best].st_name;
2108 /* For kallsyms to ask for address resolution. NULL means not found.
2109 We don't lock, as this is used for oops resolution and races are a
2110 lesser concern. */
2111 const char *module_address_lookup(unsigned long addr,
2112 unsigned long *size,
2113 unsigned long *offset,
2114 char **modname)
2116 struct module *mod;
2118 list_for_each_entry(mod, &modules, list) {
2119 if (within(addr, mod->module_init, mod->init_size)
2120 || within(addr, mod->module_core, mod->core_size)) {
2121 if (modname)
2122 *modname = mod->name;
2123 return get_ksymbol(mod, addr, size, offset);
2126 return NULL;
2129 int lookup_module_symbol_name(unsigned long addr, char *symname)
2131 struct module *mod;
2133 mutex_lock(&module_mutex);
2134 list_for_each_entry(mod, &modules, list) {
2135 if (within(addr, mod->module_init, mod->init_size) ||
2136 within(addr, mod->module_core, mod->core_size)) {
2137 const char *sym;
2139 sym = get_ksymbol(mod, addr, NULL, NULL);
2140 if (!sym)
2141 goto out;
2142 strlcpy(symname, sym, KSYM_NAME_LEN + 1);
2143 mutex_unlock(&module_mutex);
2144 return 0;
2147 out:
2148 mutex_unlock(&module_mutex);
2149 return -ERANGE;
2152 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2153 unsigned long *offset, char *modname, char *name)
2155 struct module *mod;
2157 mutex_lock(&module_mutex);
2158 list_for_each_entry(mod, &modules, list) {
2159 if (within(addr, mod->module_init, mod->init_size) ||
2160 within(addr, mod->module_core, mod->core_size)) {
2161 const char *sym;
2163 sym = get_ksymbol(mod, addr, size, offset);
2164 if (!sym)
2165 goto out;
2166 if (modname)
2167 strlcpy(modname, mod->name, MODULE_NAME_LEN + 1);
2168 if (name)
2169 strlcpy(name, sym, KSYM_NAME_LEN + 1);
2170 mutex_unlock(&module_mutex);
2171 return 0;
2174 out:
2175 mutex_unlock(&module_mutex);
2176 return -ERANGE;
2179 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2180 char *name, char *module_name, int *exported)
2182 struct module *mod;
2184 mutex_lock(&module_mutex);
2185 list_for_each_entry(mod, &modules, list) {
2186 if (symnum < mod->num_symtab) {
2187 *value = mod->symtab[symnum].st_value;
2188 *type = mod->symtab[symnum].st_info;
2189 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2190 KSYM_NAME_LEN + 1);
2191 strlcpy(module_name, mod->name, MODULE_NAME_LEN + 1);
2192 *exported = is_exported(name, mod);
2193 mutex_unlock(&module_mutex);
2194 return 0;
2196 symnum -= mod->num_symtab;
2198 mutex_unlock(&module_mutex);
2199 return -ERANGE;
2202 static unsigned long mod_find_symname(struct module *mod, const char *name)
2204 unsigned int i;
2206 for (i = 0; i < mod->num_symtab; i++)
2207 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2208 mod->symtab[i].st_info != 'U')
2209 return mod->symtab[i].st_value;
2210 return 0;
2213 /* Look for this name: can be of form module:name. */
2214 unsigned long module_kallsyms_lookup_name(const char *name)
2216 struct module *mod;
2217 char *colon;
2218 unsigned long ret = 0;
2220 /* Don't lock: we're in enough trouble already. */
2221 if ((colon = strchr(name, ':')) != NULL) {
2222 *colon = '\0';
2223 if ((mod = find_module(name)) != NULL)
2224 ret = mod_find_symname(mod, colon+1);
2225 *colon = ':';
2226 } else {
2227 list_for_each_entry(mod, &modules, list)
2228 if ((ret = mod_find_symname(mod, name)) != 0)
2229 break;
2231 return ret;
2233 #endif /* CONFIG_KALLSYMS */
2235 /* Called by the /proc file system to return a list of modules. */
2236 static void *m_start(struct seq_file *m, loff_t *pos)
2238 struct list_head *i;
2239 loff_t n = 0;
2241 mutex_lock(&module_mutex);
2242 list_for_each(i, &modules) {
2243 if (n++ == *pos)
2244 break;
2246 if (i == &modules)
2247 return NULL;
2248 return i;
2251 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2253 struct list_head *i = p;
2254 (*pos)++;
2255 if (i->next == &modules)
2256 return NULL;
2257 return i->next;
2260 static void m_stop(struct seq_file *m, void *p)
2262 mutex_unlock(&module_mutex);
2265 static char *taint_flags(unsigned int taints, char *buf)
2267 int bx = 0;
2269 if (taints) {
2270 buf[bx++] = '(';
2271 if (taints & TAINT_PROPRIETARY_MODULE)
2272 buf[bx++] = 'P';
2273 if (taints & TAINT_FORCED_MODULE)
2274 buf[bx++] = 'F';
2276 * TAINT_FORCED_RMMOD: could be added.
2277 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2278 * apply to modules.
2280 buf[bx++] = ')';
2282 buf[bx] = '\0';
2284 return buf;
2287 static int m_show(struct seq_file *m, void *p)
2289 struct module *mod = list_entry(p, struct module, list);
2290 char buf[8];
2292 seq_printf(m, "%s %lu",
2293 mod->name, mod->init_size + mod->core_size);
2294 print_unload_info(m, mod);
2296 /* Informative for users. */
2297 seq_printf(m, " %s",
2298 mod->state == MODULE_STATE_GOING ? "Unloading":
2299 mod->state == MODULE_STATE_COMING ? "Loading":
2300 "Live");
2301 /* Used by oprofile and other similar tools. */
2302 seq_printf(m, " 0x%p", mod->module_core);
2304 /* Taints info */
2305 if (mod->taints)
2306 seq_printf(m, " %s", taint_flags(mod->taints, buf));
2308 seq_printf(m, "\n");
2309 return 0;
2312 /* Format: modulename size refcount deps address
2314 Where refcount is a number or -, and deps is a comma-separated list
2315 of depends or -.
2317 const struct seq_operations modules_op = {
2318 .start = m_start,
2319 .next = m_next,
2320 .stop = m_stop,
2321 .show = m_show
2324 /* Given an address, look for it in the module exception tables. */
2325 const struct exception_table_entry *search_module_extables(unsigned long addr)
2327 unsigned long flags;
2328 const struct exception_table_entry *e = NULL;
2329 struct module *mod;
2331 spin_lock_irqsave(&modlist_lock, flags);
2332 list_for_each_entry(mod, &modules, list) {
2333 if (mod->num_exentries == 0)
2334 continue;
2336 e = search_extable(mod->extable,
2337 mod->extable + mod->num_exentries - 1,
2338 addr);
2339 if (e)
2340 break;
2342 spin_unlock_irqrestore(&modlist_lock, flags);
2344 /* Now, if we found one, we are running inside it now, hence
2345 we cannot unload the module, hence no refcnt needed. */
2346 return e;
2350 * Is this a valid module address?
2352 int is_module_address(unsigned long addr)
2354 unsigned long flags;
2355 struct module *mod;
2357 spin_lock_irqsave(&modlist_lock, flags);
2359 list_for_each_entry(mod, &modules, list) {
2360 if (within(addr, mod->module_core, mod->core_size)) {
2361 spin_unlock_irqrestore(&modlist_lock, flags);
2362 return 1;
2366 spin_unlock_irqrestore(&modlist_lock, flags);
2368 return 0;
2372 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2373 struct module *__module_text_address(unsigned long addr)
2375 struct module *mod;
2377 list_for_each_entry(mod, &modules, list)
2378 if (within(addr, mod->module_init, mod->init_text_size)
2379 || within(addr, mod->module_core, mod->core_text_size))
2380 return mod;
2381 return NULL;
2384 struct module *module_text_address(unsigned long addr)
2386 struct module *mod;
2387 unsigned long flags;
2389 spin_lock_irqsave(&modlist_lock, flags);
2390 mod = __module_text_address(addr);
2391 spin_unlock_irqrestore(&modlist_lock, flags);
2393 return mod;
2396 /* Don't grab lock, we're oopsing. */
2397 void print_modules(void)
2399 struct module *mod;
2400 char buf[8];
2402 printk("Modules linked in:");
2403 list_for_each_entry(mod, &modules, list)
2404 printk(" %s%s", mod->name, taint_flags(mod->taints, buf));
2405 printk("\n");
2408 #ifdef CONFIG_SYSFS
2409 static char *make_driver_name(struct device_driver *drv)
2411 char *driver_name;
2413 driver_name = kmalloc(strlen(drv->name) + strlen(drv->bus->name) + 2,
2414 GFP_KERNEL);
2415 if (!driver_name)
2416 return NULL;
2418 sprintf(driver_name, "%s:%s", drv->bus->name, drv->name);
2419 return driver_name;
2422 static void module_create_drivers_dir(struct module_kobject *mk)
2424 if (!mk || mk->drivers_dir)
2425 return;
2427 mk->drivers_dir = kobject_add_dir(&mk->kobj, "drivers");
2430 void module_add_driver(struct module *mod, struct device_driver *drv)
2432 char *driver_name;
2433 int no_warn;
2434 struct module_kobject *mk = NULL;
2436 if (!drv)
2437 return;
2439 if (mod)
2440 mk = &mod->mkobj;
2441 else if (drv->mod_name) {
2442 struct kobject *mkobj;
2444 /* Lookup built-in module entry in /sys/modules */
2445 mkobj = kset_find_obj(&module_subsys, drv->mod_name);
2446 if (mkobj) {
2447 mk = container_of(mkobj, struct module_kobject, kobj);
2448 /* remember our module structure */
2449 drv->mkobj = mk;
2450 /* kset_find_obj took a reference */
2451 kobject_put(mkobj);
2455 if (!mk)
2456 return;
2458 /* Don't check return codes; these calls are idempotent */
2459 no_warn = sysfs_create_link(&drv->kobj, &mk->kobj, "module");
2460 driver_name = make_driver_name(drv);
2461 if (driver_name) {
2462 module_create_drivers_dir(mk);
2463 no_warn = sysfs_create_link(mk->drivers_dir, &drv->kobj,
2464 driver_name);
2465 kfree(driver_name);
2468 EXPORT_SYMBOL(module_add_driver);
2470 void module_remove_driver(struct device_driver *drv)
2472 struct module_kobject *mk = NULL;
2473 char *driver_name;
2475 if (!drv)
2476 return;
2478 sysfs_remove_link(&drv->kobj, "module");
2480 if (drv->owner)
2481 mk = &drv->owner->mkobj;
2482 else if (drv->mkobj)
2483 mk = drv->mkobj;
2484 if (mk && mk->drivers_dir) {
2485 driver_name = make_driver_name(drv);
2486 if (driver_name) {
2487 sysfs_remove_link(mk->drivers_dir, driver_name);
2488 kfree(driver_name);
2492 EXPORT_SYMBOL(module_remove_driver);
2493 #endif
2495 #ifdef CONFIG_MODVERSIONS
2496 /* Generate the signature for struct module here, too, for modversions. */
2497 void struct_module(struct module *mod) { return; }
2498 EXPORT_SYMBOL(struct_module);
2499 #endif