qtronix.c: Handle kmalloc failure.
[linux-2.6/linux-mips.git] / kernel / module.c
blobff5c500ab625202ef8401998bc0a71cac53424aa
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/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/cpu.h>
32 #include <linux/moduleparam.h>
33 #include <linux/errno.h>
34 #include <linux/err.h>
35 #include <linux/vermagic.h>
36 #include <linux/notifier.h>
37 #include <linux/stop_machine.h>
38 #include <linux/device.h>
39 #include <linux/string.h>
40 #include <asm/uaccess.h>
41 #include <asm/semaphore.h>
42 #include <asm/cacheflush.h>
44 #if 0
45 #define DEBUGP printk
46 #else
47 #define DEBUGP(fmt , a...)
48 #endif
50 #ifndef ARCH_SHF_SMALL
51 #define ARCH_SHF_SMALL 0
52 #endif
54 /* If this is set, the section belongs in the init part of the module */
55 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
57 /* Protects module list */
58 static DEFINE_SPINLOCK(modlist_lock);
60 /* List of modules, protected by module_mutex AND modlist_lock */
61 static DECLARE_MUTEX(module_mutex);
62 static LIST_HEAD(modules);
64 static DECLARE_MUTEX(notify_mutex);
65 static struct notifier_block * module_notify_list;
67 int register_module_notifier(struct notifier_block * nb)
69 int err;
70 down(&notify_mutex);
71 err = notifier_chain_register(&module_notify_list, nb);
72 up(&notify_mutex);
73 return err;
75 EXPORT_SYMBOL(register_module_notifier);
77 int unregister_module_notifier(struct notifier_block * nb)
79 int err;
80 down(&notify_mutex);
81 err = notifier_chain_unregister(&module_notify_list, nb);
82 up(&notify_mutex);
83 return err;
85 EXPORT_SYMBOL(unregister_module_notifier);
87 /* We require a truly strong try_module_get() */
88 static inline int strong_try_module_get(struct module *mod)
90 if (mod && mod->state == MODULE_STATE_COMING)
91 return 0;
92 return try_module_get(mod);
95 /* A thread that wants to hold a reference to a module only while it
96 * is running can call ths to safely exit.
97 * nfsd and lockd use this.
99 void __module_put_and_exit(struct module *mod, long code)
101 module_put(mod);
102 do_exit(code);
104 EXPORT_SYMBOL(__module_put_and_exit);
106 /* Find a module section: 0 means not found. */
107 static unsigned int find_sec(Elf_Ehdr *hdr,
108 Elf_Shdr *sechdrs,
109 const char *secstrings,
110 const char *name)
112 unsigned int i;
114 for (i = 1; i < hdr->e_shnum; i++)
115 /* Alloc bit cleared means "ignore it." */
116 if ((sechdrs[i].sh_flags & SHF_ALLOC)
117 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
118 return i;
119 return 0;
122 /* Provided by the linker */
123 extern const struct kernel_symbol __start___ksymtab[];
124 extern const struct kernel_symbol __stop___ksymtab[];
125 extern const struct kernel_symbol __start___ksymtab_gpl[];
126 extern const struct kernel_symbol __stop___ksymtab_gpl[];
127 extern const unsigned long __start___kcrctab[];
128 extern const unsigned long __start___kcrctab_gpl[];
130 #ifndef CONFIG_MODVERSIONS
131 #define symversion(base, idx) NULL
132 #else
133 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
134 #endif
136 /* Find a symbol, return value, crc and module which owns it */
137 static unsigned long __find_symbol(const char *name,
138 struct module **owner,
139 const unsigned long **crc,
140 int gplok)
142 struct module *mod;
143 unsigned int i;
145 /* Core kernel first. */
146 *owner = NULL;
147 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
148 if (strcmp(__start___ksymtab[i].name, name) == 0) {
149 *crc = symversion(__start___kcrctab, i);
150 return __start___ksymtab[i].value;
153 if (gplok) {
154 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
155 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
156 *crc = symversion(__start___kcrctab_gpl, i);
157 return __start___ksymtab_gpl[i].value;
161 /* Now try modules. */
162 list_for_each_entry(mod, &modules, list) {
163 *owner = mod;
164 for (i = 0; i < mod->num_syms; i++)
165 if (strcmp(mod->syms[i].name, name) == 0) {
166 *crc = symversion(mod->crcs, i);
167 return mod->syms[i].value;
170 if (gplok) {
171 for (i = 0; i < mod->num_gpl_syms; i++) {
172 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
173 *crc = symversion(mod->gpl_crcs, i);
174 return mod->gpl_syms[i].value;
179 DEBUGP("Failed to find symbol %s\n", name);
180 return 0;
183 /* Find a symbol in this elf symbol table */
184 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
185 unsigned int symindex,
186 const char *strtab,
187 const char *name)
189 unsigned int i;
190 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
192 /* Search (defined) internal symbols first. */
193 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
194 if (sym[i].st_shndx != SHN_UNDEF
195 && strcmp(name, strtab + sym[i].st_name) == 0)
196 return sym[i].st_value;
198 return 0;
201 /* Search for module by name: must hold module_mutex. */
202 static struct module *find_module(const char *name)
204 struct module *mod;
206 list_for_each_entry(mod, &modules, list) {
207 if (strcmp(mod->name, name) == 0)
208 return mod;
210 return NULL;
213 #ifdef CONFIG_SMP
214 /* Number of blocks used and allocated. */
215 static unsigned int pcpu_num_used, pcpu_num_allocated;
216 /* Size of each block. -ve means used. */
217 static int *pcpu_size;
219 static int split_block(unsigned int i, unsigned short size)
221 /* Reallocation required? */
222 if (pcpu_num_used + 1 > pcpu_num_allocated) {
223 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
224 GFP_KERNEL);
225 if (!new)
226 return 0;
228 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
229 pcpu_num_allocated *= 2;
230 kfree(pcpu_size);
231 pcpu_size = new;
234 /* Insert a new subblock */
235 memmove(&pcpu_size[i+1], &pcpu_size[i],
236 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
237 pcpu_num_used++;
239 pcpu_size[i+1] -= size;
240 pcpu_size[i] = size;
241 return 1;
244 static inline unsigned int block_size(int val)
246 if (val < 0)
247 return -val;
248 return val;
251 /* Created by linker magic */
252 extern char __per_cpu_start[], __per_cpu_end[];
254 static void *percpu_modalloc(unsigned long size, unsigned long align,
255 const char *name)
257 unsigned long extra;
258 unsigned int i;
259 void *ptr;
261 if (align > SMP_CACHE_BYTES) {
262 printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
263 name, align, SMP_CACHE_BYTES);
264 align = SMP_CACHE_BYTES;
267 ptr = __per_cpu_start;
268 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
269 /* Extra for alignment requirement. */
270 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
271 BUG_ON(i == 0 && extra != 0);
273 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
274 continue;
276 /* Transfer extra to previous block. */
277 if (pcpu_size[i-1] < 0)
278 pcpu_size[i-1] -= extra;
279 else
280 pcpu_size[i-1] += extra;
281 pcpu_size[i] -= extra;
282 ptr += extra;
284 /* Split block if warranted */
285 if (pcpu_size[i] - size > sizeof(unsigned long))
286 if (!split_block(i, size))
287 return NULL;
289 /* Mark allocated */
290 pcpu_size[i] = -pcpu_size[i];
291 return ptr;
294 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
295 size);
296 return NULL;
299 static void percpu_modfree(void *freeme)
301 unsigned int i;
302 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
304 /* First entry is core kernel percpu data. */
305 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
306 if (ptr == freeme) {
307 pcpu_size[i] = -pcpu_size[i];
308 goto free;
311 BUG();
313 free:
314 /* Merge with previous? */
315 if (pcpu_size[i-1] >= 0) {
316 pcpu_size[i-1] += pcpu_size[i];
317 pcpu_num_used--;
318 memmove(&pcpu_size[i], &pcpu_size[i+1],
319 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
320 i--;
322 /* Merge with next? */
323 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
324 pcpu_size[i] += pcpu_size[i+1];
325 pcpu_num_used--;
326 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
327 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
331 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
332 Elf_Shdr *sechdrs,
333 const char *secstrings)
335 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
338 static int percpu_modinit(void)
340 pcpu_num_used = 2;
341 pcpu_num_allocated = 2;
342 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
343 GFP_KERNEL);
344 /* Static in-kernel percpu data (used). */
345 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
346 /* Free room. */
347 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
348 if (pcpu_size[1] < 0) {
349 printk(KERN_ERR "No per-cpu room for modules.\n");
350 pcpu_num_used = 1;
353 return 0;
355 __initcall(percpu_modinit);
356 #else /* ... !CONFIG_SMP */
357 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
358 const char *name)
360 return NULL;
362 static inline void percpu_modfree(void *pcpuptr)
364 BUG();
366 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
367 Elf_Shdr *sechdrs,
368 const char *secstrings)
370 return 0;
372 static inline void percpu_modcopy(void *pcpudst, const void *src,
373 unsigned long size)
375 /* pcpusec should be 0, and size of that section should be 0. */
376 BUG_ON(size != 0);
378 #endif /* CONFIG_SMP */
380 #ifdef CONFIG_MODULE_UNLOAD
381 #define MODINFO_ATTR(field) \
382 static void setup_modinfo_##field(struct module *mod, const char *s) \
384 mod->field = kstrdup(s, GFP_KERNEL); \
386 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
387 struct module *mod, char *buffer) \
389 return sprintf(buffer, "%s\n", mod->field); \
391 static int modinfo_##field##_exists(struct module *mod) \
393 return mod->field != NULL; \
395 static void free_modinfo_##field(struct module *mod) \
397 kfree(mod->field); \
398 mod->field = NULL; \
400 static struct module_attribute modinfo_##field = { \
401 .attr = { .name = __stringify(field), .mode = 0444, \
402 .owner = THIS_MODULE }, \
403 .show = show_modinfo_##field, \
404 .setup = setup_modinfo_##field, \
405 .test = modinfo_##field##_exists, \
406 .free = free_modinfo_##field, \
409 MODINFO_ATTR(version);
410 MODINFO_ATTR(srcversion);
412 static struct module_attribute *modinfo_attrs[] = {
413 &modinfo_version,
414 &modinfo_srcversion,
415 NULL,
418 /* Init the unload section of the module. */
419 static void module_unload_init(struct module *mod)
421 unsigned int i;
423 INIT_LIST_HEAD(&mod->modules_which_use_me);
424 for (i = 0; i < NR_CPUS; i++)
425 local_set(&mod->ref[i].count, 0);
426 /* Hold reference count during initialization. */
427 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
428 /* Backwards compatibility macros put refcount during init. */
429 mod->waiter = current;
432 /* modules using other modules */
433 struct module_use
435 struct list_head list;
436 struct module *module_which_uses;
439 /* Does a already use b? */
440 static int already_uses(struct module *a, struct module *b)
442 struct module_use *use;
444 list_for_each_entry(use, &b->modules_which_use_me, list) {
445 if (use->module_which_uses == a) {
446 DEBUGP("%s uses %s!\n", a->name, b->name);
447 return 1;
450 DEBUGP("%s does not use %s!\n", a->name, b->name);
451 return 0;
454 /* Module a uses b */
455 static int use_module(struct module *a, struct module *b)
457 struct module_use *use;
458 if (b == NULL || already_uses(a, b)) return 1;
460 if (!strong_try_module_get(b))
461 return 0;
463 DEBUGP("Allocating new usage for %s.\n", a->name);
464 use = kmalloc(sizeof(*use), GFP_ATOMIC);
465 if (!use) {
466 printk("%s: out of memory loading\n", a->name);
467 module_put(b);
468 return 0;
471 use->module_which_uses = a;
472 list_add(&use->list, &b->modules_which_use_me);
473 return 1;
476 /* Clear the unload stuff of the module. */
477 static void module_unload_free(struct module *mod)
479 struct module *i;
481 list_for_each_entry(i, &modules, list) {
482 struct module_use *use;
484 list_for_each_entry(use, &i->modules_which_use_me, list) {
485 if (use->module_which_uses == mod) {
486 DEBUGP("%s unusing %s\n", mod->name, i->name);
487 module_put(i);
488 list_del(&use->list);
489 kfree(use);
490 /* There can be at most one match. */
491 break;
497 #ifdef CONFIG_MODULE_FORCE_UNLOAD
498 static inline int try_force(unsigned int flags)
500 int ret = (flags & O_TRUNC);
501 if (ret)
502 add_taint(TAINT_FORCED_MODULE);
503 return ret;
505 #else
506 static inline int try_force(unsigned int flags)
508 return 0;
510 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
512 struct stopref
514 struct module *mod;
515 int flags;
516 int *forced;
519 /* Whole machine is stopped with interrupts off when this runs. */
520 static int __try_stop_module(void *_sref)
522 struct stopref *sref = _sref;
524 /* If it's not unused, quit unless we are told to block. */
525 if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
526 if (!(*sref->forced = try_force(sref->flags)))
527 return -EWOULDBLOCK;
530 /* Mark it as dying. */
531 sref->mod->state = MODULE_STATE_GOING;
532 return 0;
535 static int try_stop_module(struct module *mod, int flags, int *forced)
537 struct stopref sref = { mod, flags, forced };
539 return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
542 unsigned int module_refcount(struct module *mod)
544 unsigned int i, total = 0;
546 for (i = 0; i < NR_CPUS; i++)
547 total += local_read(&mod->ref[i].count);
548 return total;
550 EXPORT_SYMBOL(module_refcount);
552 /* This exists whether we can unload or not */
553 static void free_module(struct module *mod);
555 static void wait_for_zero_refcount(struct module *mod)
557 /* Since we might sleep for some time, drop the semaphore first */
558 up(&module_mutex);
559 for (;;) {
560 DEBUGP("Looking at refcount...\n");
561 set_current_state(TASK_UNINTERRUPTIBLE);
562 if (module_refcount(mod) == 0)
563 break;
564 schedule();
566 current->state = TASK_RUNNING;
567 down(&module_mutex);
570 asmlinkage long
571 sys_delete_module(const char __user *name_user, unsigned int flags)
573 struct module *mod;
574 char name[MODULE_NAME_LEN];
575 int ret, forced = 0;
577 if (!capable(CAP_SYS_MODULE))
578 return -EPERM;
580 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
581 return -EFAULT;
582 name[MODULE_NAME_LEN-1] = '\0';
584 if (down_interruptible(&module_mutex) != 0)
585 return -EINTR;
587 mod = find_module(name);
588 if (!mod) {
589 ret = -ENOENT;
590 goto out;
593 if (!list_empty(&mod->modules_which_use_me)) {
594 /* Other modules depend on us: get rid of them first. */
595 ret = -EWOULDBLOCK;
596 goto out;
599 /* Doing init or already dying? */
600 if (mod->state != MODULE_STATE_LIVE) {
601 /* FIXME: if (force), slam module count and wake up
602 waiter --RR */
603 DEBUGP("%s already dying\n", mod->name);
604 ret = -EBUSY;
605 goto out;
608 /* If it has an init func, it must have an exit func to unload */
609 if ((mod->init != NULL && mod->exit == NULL)
610 || mod->unsafe) {
611 forced = try_force(flags);
612 if (!forced) {
613 /* This module can't be removed */
614 ret = -EBUSY;
615 goto out;
619 /* Set this up before setting mod->state */
620 mod->waiter = current;
622 /* Stop the machine so refcounts can't move and disable module. */
623 ret = try_stop_module(mod, flags, &forced);
624 if (ret != 0)
625 goto out;
627 /* Never wait if forced. */
628 if (!forced && module_refcount(mod) != 0)
629 wait_for_zero_refcount(mod);
631 /* Final destruction now noone is using it. */
632 if (mod->exit != NULL) {
633 up(&module_mutex);
634 mod->exit();
635 down(&module_mutex);
637 free_module(mod);
639 out:
640 up(&module_mutex);
641 return ret;
644 static void print_unload_info(struct seq_file *m, struct module *mod)
646 struct module_use *use;
647 int printed_something = 0;
649 seq_printf(m, " %u ", module_refcount(mod));
651 /* Always include a trailing , so userspace can differentiate
652 between this and the old multi-field proc format. */
653 list_for_each_entry(use, &mod->modules_which_use_me, list) {
654 printed_something = 1;
655 seq_printf(m, "%s,", use->module_which_uses->name);
658 if (mod->unsafe) {
659 printed_something = 1;
660 seq_printf(m, "[unsafe],");
663 if (mod->init != NULL && mod->exit == NULL) {
664 printed_something = 1;
665 seq_printf(m, "[permanent],");
668 if (!printed_something)
669 seq_printf(m, "-");
672 void __symbol_put(const char *symbol)
674 struct module *owner;
675 unsigned long flags;
676 const unsigned long *crc;
678 spin_lock_irqsave(&modlist_lock, flags);
679 if (!__find_symbol(symbol, &owner, &crc, 1))
680 BUG();
681 module_put(owner);
682 spin_unlock_irqrestore(&modlist_lock, flags);
684 EXPORT_SYMBOL(__symbol_put);
686 void symbol_put_addr(void *addr)
688 unsigned long flags;
690 spin_lock_irqsave(&modlist_lock, flags);
691 if (!kernel_text_address((unsigned long)addr))
692 BUG();
694 module_put(module_text_address((unsigned long)addr));
695 spin_unlock_irqrestore(&modlist_lock, flags);
697 EXPORT_SYMBOL_GPL(symbol_put_addr);
699 static ssize_t show_refcnt(struct module_attribute *mattr,
700 struct module *mod, char *buffer)
702 /* sysfs holds a reference */
703 return sprintf(buffer, "%u\n", module_refcount(mod)-1);
706 static struct module_attribute refcnt = {
707 .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
708 .show = show_refcnt,
711 #else /* !CONFIG_MODULE_UNLOAD */
712 static void print_unload_info(struct seq_file *m, struct module *mod)
714 /* We don't know the usage count, or what modules are using. */
715 seq_printf(m, " - -");
718 static inline void module_unload_free(struct module *mod)
722 static inline int use_module(struct module *a, struct module *b)
724 return strong_try_module_get(b);
727 static inline void module_unload_init(struct module *mod)
730 #endif /* CONFIG_MODULE_UNLOAD */
732 #ifdef CONFIG_OBSOLETE_MODPARM
733 /* Bounds checking done below */
734 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
736 strcpy(kp->arg, val);
737 return 0;
740 static int set_obsolete(const char *val, struct kernel_param *kp)
742 unsigned int min, max;
743 unsigned int size, maxsize;
744 int dummy;
745 char *endp;
746 const char *p;
747 struct obsolete_modparm *obsparm = kp->arg;
749 if (!val) {
750 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
751 return -EINVAL;
754 /* type is: [min[-max]]{b,h,i,l,s} */
755 p = obsparm->type;
756 min = simple_strtol(p, &endp, 10);
757 if (endp == obsparm->type)
758 min = max = 1;
759 else if (*endp == '-') {
760 p = endp+1;
761 max = simple_strtol(p, &endp, 10);
762 } else
763 max = min;
764 switch (*endp) {
765 case 'b':
766 return param_array(kp->name, val, min, max, obsparm->addr,
767 1, param_set_byte, &dummy);
768 case 'h':
769 return param_array(kp->name, val, min, max, obsparm->addr,
770 sizeof(short), param_set_short, &dummy);
771 case 'i':
772 return param_array(kp->name, val, min, max, obsparm->addr,
773 sizeof(int), param_set_int, &dummy);
774 case 'l':
775 return param_array(kp->name, val, min, max, obsparm->addr,
776 sizeof(long), param_set_long, &dummy);
777 case 's':
778 return param_array(kp->name, val, min, max, obsparm->addr,
779 sizeof(char *), param_set_charp, &dummy);
781 case 'c':
782 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
783 and the decl is "char xxx[5][50];" */
784 p = endp+1;
785 maxsize = simple_strtol(p, &endp, 10);
786 /* We check lengths here (yes, this is a hack). */
787 p = val;
788 while (p[size = strcspn(p, ",")]) {
789 if (size >= maxsize)
790 goto oversize;
791 p += size+1;
793 if (size >= maxsize)
794 goto oversize;
795 return param_array(kp->name, val, min, max, obsparm->addr,
796 maxsize, obsparm_copy_string, &dummy);
798 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
799 return -EINVAL;
800 oversize:
801 printk(KERN_ERR
802 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
803 return -EINVAL;
806 static int obsolete_params(const char *name,
807 char *args,
808 struct obsolete_modparm obsparm[],
809 unsigned int num,
810 Elf_Shdr *sechdrs,
811 unsigned int symindex,
812 const char *strtab)
814 struct kernel_param *kp;
815 unsigned int i;
816 int ret;
818 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
819 if (!kp)
820 return -ENOMEM;
822 for (i = 0; i < num; i++) {
823 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
825 snprintf(sym_name, sizeof(sym_name), "%s%s",
826 MODULE_SYMBOL_PREFIX, obsparm[i].name);
828 kp[i].name = obsparm[i].name;
829 kp[i].perm = 000;
830 kp[i].set = set_obsolete;
831 kp[i].get = NULL;
832 obsparm[i].addr
833 = (void *)find_local_symbol(sechdrs, symindex, strtab,
834 sym_name);
835 if (!obsparm[i].addr) {
836 printk("%s: falsely claims to have parameter %s\n",
837 name, obsparm[i].name);
838 ret = -EINVAL;
839 goto out;
841 kp[i].arg = &obsparm[i];
844 ret = parse_args(name, args, kp, num, NULL);
845 out:
846 kfree(kp);
847 return ret;
849 #else
850 static int obsolete_params(const char *name,
851 char *args,
852 struct obsolete_modparm obsparm[],
853 unsigned int num,
854 Elf_Shdr *sechdrs,
855 unsigned int symindex,
856 const char *strtab)
858 if (num != 0)
859 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
860 name);
861 return 0;
863 #endif /* CONFIG_OBSOLETE_MODPARM */
865 static const char vermagic[] = VERMAGIC_STRING;
867 #ifdef CONFIG_MODVERSIONS
868 static int check_version(Elf_Shdr *sechdrs,
869 unsigned int versindex,
870 const char *symname,
871 struct module *mod,
872 const unsigned long *crc)
874 unsigned int i, num_versions;
875 struct modversion_info *versions;
877 /* Exporting module didn't supply crcs? OK, we're already tainted. */
878 if (!crc)
879 return 1;
881 versions = (void *) sechdrs[versindex].sh_addr;
882 num_versions = sechdrs[versindex].sh_size
883 / sizeof(struct modversion_info);
885 for (i = 0; i < num_versions; i++) {
886 if (strcmp(versions[i].name, symname) != 0)
887 continue;
889 if (versions[i].crc == *crc)
890 return 1;
891 printk("%s: disagrees about version of symbol %s\n",
892 mod->name, symname);
893 DEBUGP("Found checksum %lX vs module %lX\n",
894 *crc, versions[i].crc);
895 return 0;
897 /* Not in module's version table. OK, but that taints the kernel. */
898 if (!(tainted & TAINT_FORCED_MODULE)) {
899 printk("%s: no version for \"%s\" found: kernel tainted.\n",
900 mod->name, symname);
901 add_taint(TAINT_FORCED_MODULE);
903 return 1;
906 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
907 unsigned int versindex,
908 struct module *mod)
910 const unsigned long *crc;
911 struct module *owner;
913 if (!__find_symbol("struct_module", &owner, &crc, 1))
914 BUG();
915 return check_version(sechdrs, versindex, "struct_module", mod,
916 crc);
919 /* First part is kernel version, which we ignore. */
920 static inline int same_magic(const char *amagic, const char *bmagic)
922 amagic += strcspn(amagic, " ");
923 bmagic += strcspn(bmagic, " ");
924 return strcmp(amagic, bmagic) == 0;
926 #else
927 static inline int check_version(Elf_Shdr *sechdrs,
928 unsigned int versindex,
929 const char *symname,
930 struct module *mod,
931 const unsigned long *crc)
933 return 1;
936 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
937 unsigned int versindex,
938 struct module *mod)
940 return 1;
943 static inline int same_magic(const char *amagic, const char *bmagic)
945 return strcmp(amagic, bmagic) == 0;
947 #endif /* CONFIG_MODVERSIONS */
949 /* Resolve a symbol for this module. I.e. if we find one, record usage.
950 Must be holding module_mutex. */
951 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
952 unsigned int versindex,
953 const char *name,
954 struct module *mod)
956 struct module *owner;
957 unsigned long ret;
958 const unsigned long *crc;
960 spin_lock_irq(&modlist_lock);
961 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
962 if (ret) {
963 /* use_module can fail due to OOM, or module unloading */
964 if (!check_version(sechdrs, versindex, name, mod, crc) ||
965 !use_module(mod, owner))
966 ret = 0;
968 spin_unlock_irq(&modlist_lock);
969 return ret;
974 * /sys/module/foo/sections stuff
975 * J. Corbet <corbet@lwn.net>
977 #ifdef CONFIG_KALLSYMS
978 static ssize_t module_sect_show(struct module_attribute *mattr,
979 struct module *mod, char *buf)
981 struct module_sect_attr *sattr =
982 container_of(mattr, struct module_sect_attr, mattr);
983 return sprintf(buf, "0x%lx\n", sattr->address);
986 static void add_sect_attrs(struct module *mod, unsigned int nsect,
987 char *secstrings, Elf_Shdr *sechdrs)
989 unsigned int nloaded = 0, i, size[2];
990 struct module_sect_attrs *sect_attrs;
991 struct module_sect_attr *sattr;
992 struct attribute **gattr;
994 /* Count loaded sections and allocate structures */
995 for (i = 0; i < nsect; i++)
996 if (sechdrs[i].sh_flags & SHF_ALLOC)
997 nloaded++;
998 size[0] = ALIGN(sizeof(*sect_attrs)
999 + nloaded * sizeof(sect_attrs->attrs[0]),
1000 sizeof(sect_attrs->grp.attrs[0]));
1001 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1002 if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
1003 return;
1005 /* Setup section attributes. */
1006 sect_attrs->grp.name = "sections";
1007 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1009 sattr = &sect_attrs->attrs[0];
1010 gattr = &sect_attrs->grp.attrs[0];
1011 for (i = 0; i < nsect; i++) {
1012 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1013 continue;
1014 sattr->address = sechdrs[i].sh_addr;
1015 strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
1016 MODULE_SECT_NAME_LEN);
1017 sattr->mattr.show = module_sect_show;
1018 sattr->mattr.store = NULL;
1019 sattr->mattr.attr.name = sattr->name;
1020 sattr->mattr.attr.owner = mod;
1021 sattr->mattr.attr.mode = S_IRUGO;
1022 *(gattr++) = &(sattr++)->mattr.attr;
1024 *gattr = NULL;
1026 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1027 goto out;
1029 mod->sect_attrs = sect_attrs;
1030 return;
1031 out:
1032 kfree(sect_attrs);
1035 static void remove_sect_attrs(struct module *mod)
1037 if (mod->sect_attrs) {
1038 sysfs_remove_group(&mod->mkobj.kobj,
1039 &mod->sect_attrs->grp);
1040 /* We are positive that no one is using any sect attrs
1041 * at this point. Deallocate immediately. */
1042 kfree(mod->sect_attrs);
1043 mod->sect_attrs = NULL;
1048 #else
1049 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1050 char *sectstrings, Elf_Shdr *sechdrs)
1054 static inline void remove_sect_attrs(struct module *mod)
1057 #endif /* CONFIG_KALLSYMS */
1060 #ifdef CONFIG_MODULE_UNLOAD
1061 static inline int module_add_refcnt_attr(struct module *mod)
1063 return sysfs_create_file(&mod->mkobj.kobj, &refcnt.attr);
1065 static void module_remove_refcnt_attr(struct module *mod)
1067 return sysfs_remove_file(&mod->mkobj.kobj, &refcnt.attr);
1069 #else
1070 static inline int module_add_refcnt_attr(struct module *mod)
1072 return 0;
1074 static void module_remove_refcnt_attr(struct module *mod)
1077 #endif
1079 #ifdef CONFIG_MODULE_UNLOAD
1080 static int module_add_modinfo_attrs(struct module *mod)
1082 struct module_attribute *attr;
1083 int error = 0;
1084 int i;
1086 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1087 if (!attr->test ||
1088 (attr->test && attr->test(mod)))
1089 error = sysfs_create_file(&mod->mkobj.kobj,&attr->attr);
1091 return error;
1094 static void module_remove_modinfo_attrs(struct module *mod)
1096 struct module_attribute *attr;
1097 int i;
1099 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1100 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1101 attr->free(mod);
1104 #endif
1106 static int mod_sysfs_setup(struct module *mod,
1107 struct kernel_param *kparam,
1108 unsigned int num_params)
1110 int err;
1112 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1113 err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
1114 if (err)
1115 goto out;
1116 kobj_set_kset_s(&mod->mkobj, module_subsys);
1117 mod->mkobj.mod = mod;
1118 err = kobject_register(&mod->mkobj.kobj);
1119 if (err)
1120 goto out;
1122 err = module_add_refcnt_attr(mod);
1123 if (err)
1124 goto out_unreg;
1126 err = module_param_sysfs_setup(mod, kparam, num_params);
1127 if (err)
1128 goto out_unreg;
1130 #ifdef CONFIG_MODULE_UNLOAD
1131 err = module_add_modinfo_attrs(mod);
1132 if (err)
1133 goto out_unreg;
1134 #endif
1136 return 0;
1138 out_unreg:
1139 kobject_unregister(&mod->mkobj.kobj);
1140 out:
1141 return err;
1144 static void mod_kobject_remove(struct module *mod)
1146 #ifdef CONFIG_MODULE_UNLOAD
1147 module_remove_modinfo_attrs(mod);
1148 #endif
1149 module_remove_refcnt_attr(mod);
1150 module_param_sysfs_remove(mod);
1152 kobject_unregister(&mod->mkobj.kobj);
1156 * unlink the module with the whole machine is stopped with interrupts off
1157 * - this defends against kallsyms not taking locks
1159 static int __unlink_module(void *_mod)
1161 struct module *mod = _mod;
1162 list_del(&mod->list);
1163 return 0;
1166 /* Free a module, remove from lists, etc (must hold module mutex). */
1167 static void free_module(struct module *mod)
1169 /* Delete from various lists */
1170 stop_machine_run(__unlink_module, mod, NR_CPUS);
1171 remove_sect_attrs(mod);
1172 mod_kobject_remove(mod);
1174 /* Arch-specific cleanup. */
1175 module_arch_cleanup(mod);
1177 /* Module unload stuff */
1178 module_unload_free(mod);
1180 /* This may be NULL, but that's OK */
1181 module_free(mod, mod->module_init);
1182 kfree(mod->args);
1183 if (mod->percpu)
1184 percpu_modfree(mod->percpu);
1186 /* Finally, free the core (containing the module structure) */
1187 module_free(mod, mod->module_core);
1190 void *__symbol_get(const char *symbol)
1192 struct module *owner;
1193 unsigned long value, flags;
1194 const unsigned long *crc;
1196 spin_lock_irqsave(&modlist_lock, flags);
1197 value = __find_symbol(symbol, &owner, &crc, 1);
1198 if (value && !strong_try_module_get(owner))
1199 value = 0;
1200 spin_unlock_irqrestore(&modlist_lock, flags);
1202 return (void *)value;
1204 EXPORT_SYMBOL_GPL(__symbol_get);
1206 /* Change all symbols so that sh_value encodes the pointer directly. */
1207 static int simplify_symbols(Elf_Shdr *sechdrs,
1208 unsigned int symindex,
1209 const char *strtab,
1210 unsigned int versindex,
1211 unsigned int pcpuindex,
1212 struct module *mod)
1214 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1215 unsigned long secbase;
1216 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1217 int ret = 0;
1219 for (i = 1; i < n; i++) {
1220 switch (sym[i].st_shndx) {
1221 case SHN_COMMON:
1222 /* We compiled with -fno-common. These are not
1223 supposed to happen. */
1224 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1225 printk("%s: please compile with -fno-common\n",
1226 mod->name);
1227 ret = -ENOEXEC;
1228 break;
1230 case SHN_ABS:
1231 /* Don't need to do anything */
1232 DEBUGP("Absolute symbol: 0x%08lx\n",
1233 (long)sym[i].st_value);
1234 break;
1236 case SHN_UNDEF:
1237 sym[i].st_value
1238 = resolve_symbol(sechdrs, versindex,
1239 strtab + sym[i].st_name, mod);
1241 /* Ok if resolved. */
1242 if (sym[i].st_value != 0)
1243 break;
1244 /* Ok if weak. */
1245 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1246 break;
1248 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1249 mod->name, strtab + sym[i].st_name);
1250 ret = -ENOENT;
1251 break;
1253 default:
1254 /* Divert to percpu allocation if a percpu var. */
1255 if (sym[i].st_shndx == pcpuindex)
1256 secbase = (unsigned long)mod->percpu;
1257 else
1258 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1259 sym[i].st_value += secbase;
1260 break;
1264 return ret;
1267 /* Update size with this section: return offset. */
1268 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1270 long ret;
1272 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1273 *size = ret + sechdr->sh_size;
1274 return ret;
1277 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1278 might -- code, read-only data, read-write data, small data. Tally
1279 sizes, and place the offsets into sh_entsize fields: high bit means it
1280 belongs in init. */
1281 static void layout_sections(struct module *mod,
1282 const Elf_Ehdr *hdr,
1283 Elf_Shdr *sechdrs,
1284 const char *secstrings)
1286 static unsigned long const masks[][2] = {
1287 /* NOTE: all executable code must be the first section
1288 * in this array; otherwise modify the text_size
1289 * finder in the two loops below */
1290 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1291 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1292 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1293 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1295 unsigned int m, i;
1297 for (i = 0; i < hdr->e_shnum; i++)
1298 sechdrs[i].sh_entsize = ~0UL;
1300 DEBUGP("Core section allocation order:\n");
1301 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1302 for (i = 0; i < hdr->e_shnum; ++i) {
1303 Elf_Shdr *s = &sechdrs[i];
1305 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1306 || (s->sh_flags & masks[m][1])
1307 || s->sh_entsize != ~0UL
1308 || strncmp(secstrings + s->sh_name,
1309 ".init", 5) == 0)
1310 continue;
1311 s->sh_entsize = get_offset(&mod->core_size, s);
1312 DEBUGP("\t%s\n", secstrings + s->sh_name);
1314 if (m == 0)
1315 mod->core_text_size = mod->core_size;
1318 DEBUGP("Init section allocation order:\n");
1319 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1320 for (i = 0; i < hdr->e_shnum; ++i) {
1321 Elf_Shdr *s = &sechdrs[i];
1323 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1324 || (s->sh_flags & masks[m][1])
1325 || s->sh_entsize != ~0UL
1326 || strncmp(secstrings + s->sh_name,
1327 ".init", 5) != 0)
1328 continue;
1329 s->sh_entsize = (get_offset(&mod->init_size, s)
1330 | INIT_OFFSET_MASK);
1331 DEBUGP("\t%s\n", secstrings + s->sh_name);
1333 if (m == 0)
1334 mod->init_text_size = mod->init_size;
1338 static inline int license_is_gpl_compatible(const char *license)
1340 return (strcmp(license, "GPL") == 0
1341 || strcmp(license, "GPL v2") == 0
1342 || strcmp(license, "GPL and additional rights") == 0
1343 || strcmp(license, "Dual BSD/GPL") == 0
1344 || strcmp(license, "Dual MPL/GPL") == 0);
1347 static void set_license(struct module *mod, const char *license)
1349 if (!license)
1350 license = "unspecified";
1352 mod->license_gplok = license_is_gpl_compatible(license);
1353 if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
1354 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1355 mod->name, license);
1356 add_taint(TAINT_PROPRIETARY_MODULE);
1360 /* Parse tag=value strings from .modinfo section */
1361 static char *next_string(char *string, unsigned long *secsize)
1363 /* Skip non-zero chars */
1364 while (string[0]) {
1365 string++;
1366 if ((*secsize)-- <= 1)
1367 return NULL;
1370 /* Skip any zero padding. */
1371 while (!string[0]) {
1372 string++;
1373 if ((*secsize)-- <= 1)
1374 return NULL;
1376 return string;
1379 static char *get_modinfo(Elf_Shdr *sechdrs,
1380 unsigned int info,
1381 const char *tag)
1383 char *p;
1384 unsigned int taglen = strlen(tag);
1385 unsigned long size = sechdrs[info].sh_size;
1387 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1388 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1389 return p + taglen + 1;
1391 return NULL;
1394 #ifdef CONFIG_MODULE_UNLOAD
1395 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1396 unsigned int infoindex)
1398 struct module_attribute *attr;
1399 int i;
1401 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1402 if (attr->setup)
1403 attr->setup(mod,
1404 get_modinfo(sechdrs,
1405 infoindex,
1406 attr->attr.name));
1409 #endif
1411 #ifdef CONFIG_KALLSYMS
1412 int is_exported(const char *name, const struct module *mod)
1414 unsigned int i;
1416 if (!mod) {
1417 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1418 if (strcmp(__start___ksymtab[i].name, name) == 0)
1419 return 1;
1420 return 0;
1422 for (i = 0; i < mod->num_syms; i++)
1423 if (strcmp(mod->syms[i].name, name) == 0)
1424 return 1;
1425 return 0;
1428 /* As per nm */
1429 static char elf_type(const Elf_Sym *sym,
1430 Elf_Shdr *sechdrs,
1431 const char *secstrings,
1432 struct module *mod)
1434 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1435 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1436 return 'v';
1437 else
1438 return 'w';
1440 if (sym->st_shndx == SHN_UNDEF)
1441 return 'U';
1442 if (sym->st_shndx == SHN_ABS)
1443 return 'a';
1444 if (sym->st_shndx >= SHN_LORESERVE)
1445 return '?';
1446 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1447 return 't';
1448 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1449 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1450 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1451 return 'r';
1452 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1453 return 'g';
1454 else
1455 return 'd';
1457 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1458 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1459 return 's';
1460 else
1461 return 'b';
1463 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1464 ".debug", strlen(".debug")) == 0)
1465 return 'n';
1466 return '?';
1469 static void add_kallsyms(struct module *mod,
1470 Elf_Shdr *sechdrs,
1471 unsigned int symindex,
1472 unsigned int strindex,
1473 const char *secstrings)
1475 unsigned int i;
1477 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1478 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1479 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1481 /* Set types up while we still have access to sections. */
1482 for (i = 0; i < mod->num_symtab; i++)
1483 mod->symtab[i].st_info
1484 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1486 #else
1487 static inline void add_kallsyms(struct module *mod,
1488 Elf_Shdr *sechdrs,
1489 unsigned int symindex,
1490 unsigned int strindex,
1491 const char *secstrings)
1494 #endif /* CONFIG_KALLSYMS */
1496 /* Allocate and load the module: note that size of section 0 is always
1497 zero, and we rely on this for optional sections. */
1498 static struct module *load_module(void __user *umod,
1499 unsigned long len,
1500 const char __user *uargs)
1502 Elf_Ehdr *hdr;
1503 Elf_Shdr *sechdrs;
1504 char *secstrings, *args, *modmagic, *strtab = NULL;
1505 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1506 exportindex, modindex, obsparmindex, infoindex, gplindex,
1507 crcindex, gplcrcindex, versindex, pcpuindex;
1508 long arglen;
1509 struct module *mod;
1510 long err = 0;
1511 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1512 struct exception_table_entry *extable;
1513 mm_segment_t old_fs;
1515 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1516 umod, len, uargs);
1517 if (len < sizeof(*hdr))
1518 return ERR_PTR(-ENOEXEC);
1520 /* Suck in entire file: we'll want most of it. */
1521 /* vmalloc barfs on "unusual" numbers. Check here */
1522 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1523 return ERR_PTR(-ENOMEM);
1524 if (copy_from_user(hdr, umod, len) != 0) {
1525 err = -EFAULT;
1526 goto free_hdr;
1529 /* Sanity checks against insmoding binaries or wrong arch,
1530 weird elf version */
1531 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1532 || hdr->e_type != ET_REL
1533 || !elf_check_arch(hdr)
1534 || hdr->e_shentsize != sizeof(*sechdrs)) {
1535 err = -ENOEXEC;
1536 goto free_hdr;
1539 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1540 goto truncated;
1542 /* Convenience variables */
1543 sechdrs = (void *)hdr + hdr->e_shoff;
1544 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1545 sechdrs[0].sh_addr = 0;
1547 for (i = 1; i < hdr->e_shnum; i++) {
1548 if (sechdrs[i].sh_type != SHT_NOBITS
1549 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1550 goto truncated;
1552 /* Mark all sections sh_addr with their address in the
1553 temporary image. */
1554 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1556 /* Internal symbols and strings. */
1557 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1558 symindex = i;
1559 strindex = sechdrs[i].sh_link;
1560 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1562 #ifndef CONFIG_MODULE_UNLOAD
1563 /* Don't load .exit sections */
1564 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1565 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1566 #endif
1569 modindex = find_sec(hdr, sechdrs, secstrings,
1570 ".gnu.linkonce.this_module");
1571 if (!modindex) {
1572 printk(KERN_WARNING "No module found in object\n");
1573 err = -ENOEXEC;
1574 goto free_hdr;
1576 mod = (void *)sechdrs[modindex].sh_addr;
1578 if (symindex == 0) {
1579 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1580 mod->name);
1581 err = -ENOEXEC;
1582 goto free_hdr;
1585 /* Optional sections */
1586 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1587 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1588 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1589 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1590 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1591 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1592 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1593 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1594 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1595 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1597 /* Don't keep modinfo section */
1598 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1599 #ifdef CONFIG_KALLSYMS
1600 /* Keep symbol and string tables for decoding later. */
1601 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1602 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1603 #endif
1605 /* Check module struct version now, before we try to use module. */
1606 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1607 err = -ENOEXEC;
1608 goto free_hdr;
1611 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1612 /* This is allowed: modprobe --force will invalidate it. */
1613 if (!modmagic) {
1614 add_taint(TAINT_FORCED_MODULE);
1615 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1616 mod->name);
1617 } else if (!same_magic(modmagic, vermagic)) {
1618 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1619 mod->name, modmagic, vermagic);
1620 err = -ENOEXEC;
1621 goto free_hdr;
1624 /* Now copy in args */
1625 arglen = strlen_user(uargs);
1626 if (!arglen) {
1627 err = -EFAULT;
1628 goto free_hdr;
1630 args = kmalloc(arglen, GFP_KERNEL);
1631 if (!args) {
1632 err = -ENOMEM;
1633 goto free_hdr;
1635 if (copy_from_user(args, uargs, arglen) != 0) {
1636 err = -EFAULT;
1637 goto free_mod;
1640 if (find_module(mod->name)) {
1641 err = -EEXIST;
1642 goto free_mod;
1645 mod->state = MODULE_STATE_COMING;
1647 /* Allow arches to frob section contents and sizes. */
1648 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1649 if (err < 0)
1650 goto free_mod;
1652 if (pcpuindex) {
1653 /* We have a special allocation for this section. */
1654 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1655 sechdrs[pcpuindex].sh_addralign,
1656 mod->name);
1657 if (!percpu) {
1658 err = -ENOMEM;
1659 goto free_mod;
1661 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1662 mod->percpu = percpu;
1665 /* Determine total sizes, and put offsets in sh_entsize. For now
1666 this is done generically; there doesn't appear to be any
1667 special cases for the architectures. */
1668 layout_sections(mod, hdr, sechdrs, secstrings);
1670 /* Do the allocs. */
1671 ptr = module_alloc(mod->core_size);
1672 if (!ptr) {
1673 err = -ENOMEM;
1674 goto free_percpu;
1676 memset(ptr, 0, mod->core_size);
1677 mod->module_core = ptr;
1679 ptr = module_alloc(mod->init_size);
1680 if (!ptr && mod->init_size) {
1681 err = -ENOMEM;
1682 goto free_core;
1684 memset(ptr, 0, mod->init_size);
1685 mod->module_init = ptr;
1687 /* Transfer each section which specifies SHF_ALLOC */
1688 DEBUGP("final section addresses:\n");
1689 for (i = 0; i < hdr->e_shnum; i++) {
1690 void *dest;
1692 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1693 continue;
1695 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1696 dest = mod->module_init
1697 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1698 else
1699 dest = mod->module_core + sechdrs[i].sh_entsize;
1701 if (sechdrs[i].sh_type != SHT_NOBITS)
1702 memcpy(dest, (void *)sechdrs[i].sh_addr,
1703 sechdrs[i].sh_size);
1704 /* Update sh_addr to point to copy in image. */
1705 sechdrs[i].sh_addr = (unsigned long)dest;
1706 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1708 /* Module has been moved. */
1709 mod = (void *)sechdrs[modindex].sh_addr;
1711 /* Now we've moved module, initialize linked lists, etc. */
1712 module_unload_init(mod);
1714 /* Set up license info based on the info section */
1715 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1717 #ifdef CONFIG_MODULE_UNLOAD
1718 /* Set up MODINFO_ATTR fields */
1719 setup_modinfo(mod, sechdrs, infoindex);
1720 #endif
1722 /* Fix up syms, so that st_value is a pointer to location. */
1723 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1724 mod);
1725 if (err < 0)
1726 goto cleanup;
1728 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1729 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1730 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1731 if (crcindex)
1732 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1733 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1734 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1735 if (gplcrcindex)
1736 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1738 #ifdef CONFIG_MODVERSIONS
1739 if ((mod->num_syms && !crcindex) ||
1740 (mod->num_gpl_syms && !gplcrcindex)) {
1741 printk(KERN_WARNING "%s: No versions for exported symbols."
1742 " Tainting kernel.\n", mod->name);
1743 add_taint(TAINT_FORCED_MODULE);
1745 #endif
1747 /* Now do relocations. */
1748 for (i = 1; i < hdr->e_shnum; i++) {
1749 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1750 unsigned int info = sechdrs[i].sh_info;
1752 /* Not a valid relocation section? */
1753 if (info >= hdr->e_shnum)
1754 continue;
1756 /* Don't bother with non-allocated sections */
1757 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
1758 continue;
1760 if (sechdrs[i].sh_type == SHT_REL)
1761 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1762 else if (sechdrs[i].sh_type == SHT_RELA)
1763 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1764 mod);
1765 if (err < 0)
1766 goto cleanup;
1769 /* Set up and sort exception table */
1770 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1771 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
1772 sort_extable(extable, extable + mod->num_exentries);
1774 /* Finally, copy percpu area over. */
1775 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1776 sechdrs[pcpuindex].sh_size);
1778 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1780 err = module_finalize(hdr, sechdrs, mod);
1781 if (err < 0)
1782 goto cleanup;
1784 /* flush the icache in correct context */
1785 old_fs = get_fs();
1786 set_fs(KERNEL_DS);
1789 * Flush the instruction cache, since we've played with text.
1790 * Do it before processing of module parameters, so the module
1791 * can provide parameter accessor functions of its own.
1793 if (mod->module_init)
1794 flush_icache_range((unsigned long)mod->module_init,
1795 (unsigned long)mod->module_init
1796 + mod->init_size);
1797 flush_icache_range((unsigned long)mod->module_core,
1798 (unsigned long)mod->module_core + mod->core_size);
1800 set_fs(old_fs);
1802 mod->args = args;
1803 if (obsparmindex) {
1804 err = obsolete_params(mod->name, mod->args,
1805 (struct obsolete_modparm *)
1806 sechdrs[obsparmindex].sh_addr,
1807 sechdrs[obsparmindex].sh_size
1808 / sizeof(struct obsolete_modparm),
1809 sechdrs, symindex,
1810 (char *)sechdrs[strindex].sh_addr);
1811 if (setupindex)
1812 printk(KERN_WARNING "%s: Ignoring new-style "
1813 "parameters in presence of obsolete ones\n",
1814 mod->name);
1815 } else {
1816 /* Size of section 0 is 0, so this works well if no params */
1817 err = parse_args(mod->name, mod->args,
1818 (struct kernel_param *)
1819 sechdrs[setupindex].sh_addr,
1820 sechdrs[setupindex].sh_size
1821 / sizeof(struct kernel_param),
1822 NULL);
1824 if (err < 0)
1825 goto arch_cleanup;
1827 err = mod_sysfs_setup(mod,
1828 (struct kernel_param *)
1829 sechdrs[setupindex].sh_addr,
1830 sechdrs[setupindex].sh_size
1831 / sizeof(struct kernel_param));
1832 if (err < 0)
1833 goto arch_cleanup;
1834 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
1836 /* Get rid of temporary copy */
1837 vfree(hdr);
1839 /* Done! */
1840 return mod;
1842 arch_cleanup:
1843 module_arch_cleanup(mod);
1844 cleanup:
1845 module_unload_free(mod);
1846 module_free(mod, mod->module_init);
1847 free_core:
1848 module_free(mod, mod->module_core);
1849 free_percpu:
1850 if (percpu)
1851 percpu_modfree(percpu);
1852 free_mod:
1853 kfree(args);
1854 free_hdr:
1855 vfree(hdr);
1856 if (err < 0) return ERR_PTR(err);
1857 else return ptr;
1859 truncated:
1860 printk(KERN_ERR "Module len %lu truncated\n", len);
1861 err = -ENOEXEC;
1862 goto free_hdr;
1866 * link the module with the whole machine is stopped with interrupts off
1867 * - this defends against kallsyms not taking locks
1869 static int __link_module(void *_mod)
1871 struct module *mod = _mod;
1872 list_add(&mod->list, &modules);
1873 return 0;
1876 /* This is where the real work happens */
1877 asmlinkage long
1878 sys_init_module(void __user *umod,
1879 unsigned long len,
1880 const char __user *uargs)
1882 struct module *mod;
1883 int ret = 0;
1885 /* Must have permission */
1886 if (!capable(CAP_SYS_MODULE))
1887 return -EPERM;
1889 /* Only one module load at a time, please */
1890 if (down_interruptible(&module_mutex) != 0)
1891 return -EINTR;
1893 /* Do all the hard work */
1894 mod = load_module(umod, len, uargs);
1895 if (IS_ERR(mod)) {
1896 up(&module_mutex);
1897 return PTR_ERR(mod);
1900 /* Now sew it into the lists. They won't access us, since
1901 strong_try_module_get() will fail. */
1902 stop_machine_run(__link_module, mod, NR_CPUS);
1904 /* Drop lock so they can recurse */
1905 up(&module_mutex);
1907 down(&notify_mutex);
1908 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1909 up(&notify_mutex);
1911 /* Start the module */
1912 if (mod->init != NULL)
1913 ret = mod->init();
1914 if (ret < 0) {
1915 /* Init routine failed: abort. Try to protect us from
1916 buggy refcounters. */
1917 mod->state = MODULE_STATE_GOING;
1918 synchronize_sched();
1919 if (mod->unsafe)
1920 printk(KERN_ERR "%s: module is now stuck!\n",
1921 mod->name);
1922 else {
1923 module_put(mod);
1924 down(&module_mutex);
1925 free_module(mod);
1926 up(&module_mutex);
1928 return ret;
1931 /* Now it's a first class citizen! */
1932 down(&module_mutex);
1933 mod->state = MODULE_STATE_LIVE;
1934 /* Drop initial reference. */
1935 module_put(mod);
1936 module_free(mod, mod->module_init);
1937 mod->module_init = NULL;
1938 mod->init_size = 0;
1939 mod->init_text_size = 0;
1940 up(&module_mutex);
1942 return 0;
1945 static inline int within(unsigned long addr, void *start, unsigned long size)
1947 return ((void *)addr >= start && (void *)addr < start + size);
1950 #ifdef CONFIG_KALLSYMS
1952 * This ignores the intensely annoying "mapping symbols" found
1953 * in ARM ELF files: $a, $t and $d.
1955 static inline int is_arm_mapping_symbol(const char *str)
1957 return str[0] == '$' && strchr("atd", str[1])
1958 && (str[2] == '\0' || str[2] == '.');
1961 static const char *get_ksymbol(struct module *mod,
1962 unsigned long addr,
1963 unsigned long *size,
1964 unsigned long *offset)
1966 unsigned int i, best = 0;
1967 unsigned long nextval;
1969 /* At worse, next value is at end of module */
1970 if (within(addr, mod->module_init, mod->init_size))
1971 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1972 else
1973 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1975 /* Scan for closest preceeding symbol, and next symbol. (ELF
1976 starts real symbols at 1). */
1977 for (i = 1; i < mod->num_symtab; i++) {
1978 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1979 continue;
1981 /* We ignore unnamed symbols: they're uninformative
1982 * and inserted at a whim. */
1983 if (mod->symtab[i].st_value <= addr
1984 && mod->symtab[i].st_value > mod->symtab[best].st_value
1985 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1986 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1987 best = i;
1988 if (mod->symtab[i].st_value > addr
1989 && mod->symtab[i].st_value < nextval
1990 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
1991 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
1992 nextval = mod->symtab[i].st_value;
1995 if (!best)
1996 return NULL;
1998 *size = nextval - mod->symtab[best].st_value;
1999 *offset = addr - mod->symtab[best].st_value;
2000 return mod->strtab + mod->symtab[best].st_name;
2003 /* For kallsyms to ask for address resolution. NULL means not found.
2004 We don't lock, as this is used for oops resolution and races are a
2005 lesser concern. */
2006 const char *module_address_lookup(unsigned long addr,
2007 unsigned long *size,
2008 unsigned long *offset,
2009 char **modname)
2011 struct module *mod;
2013 list_for_each_entry(mod, &modules, list) {
2014 if (within(addr, mod->module_init, mod->init_size)
2015 || within(addr, mod->module_core, mod->core_size)) {
2016 *modname = mod->name;
2017 return get_ksymbol(mod, addr, size, offset);
2020 return NULL;
2023 struct module *module_get_kallsym(unsigned int symnum,
2024 unsigned long *value,
2025 char *type,
2026 char namebuf[128])
2028 struct module *mod;
2030 down(&module_mutex);
2031 list_for_each_entry(mod, &modules, list) {
2032 if (symnum < mod->num_symtab) {
2033 *value = mod->symtab[symnum].st_value;
2034 *type = mod->symtab[symnum].st_info;
2035 strncpy(namebuf,
2036 mod->strtab + mod->symtab[symnum].st_name,
2037 127);
2038 up(&module_mutex);
2039 return mod;
2041 symnum -= mod->num_symtab;
2043 up(&module_mutex);
2044 return NULL;
2047 static unsigned long mod_find_symname(struct module *mod, const char *name)
2049 unsigned int i;
2051 for (i = 0; i < mod->num_symtab; i++)
2052 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0)
2053 return mod->symtab[i].st_value;
2054 return 0;
2057 /* Look for this name: can be of form module:name. */
2058 unsigned long module_kallsyms_lookup_name(const char *name)
2060 struct module *mod;
2061 char *colon;
2062 unsigned long ret = 0;
2064 /* Don't lock: we're in enough trouble already. */
2065 if ((colon = strchr(name, ':')) != NULL) {
2066 *colon = '\0';
2067 if ((mod = find_module(name)) != NULL)
2068 ret = mod_find_symname(mod, colon+1);
2069 *colon = ':';
2070 } else {
2071 list_for_each_entry(mod, &modules, list)
2072 if ((ret = mod_find_symname(mod, name)) != 0)
2073 break;
2075 return ret;
2077 #endif /* CONFIG_KALLSYMS */
2079 /* Called by the /proc file system to return a list of modules. */
2080 static void *m_start(struct seq_file *m, loff_t *pos)
2082 struct list_head *i;
2083 loff_t n = 0;
2085 down(&module_mutex);
2086 list_for_each(i, &modules) {
2087 if (n++ == *pos)
2088 break;
2090 if (i == &modules)
2091 return NULL;
2092 return i;
2095 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2097 struct list_head *i = p;
2098 (*pos)++;
2099 if (i->next == &modules)
2100 return NULL;
2101 return i->next;
2104 static void m_stop(struct seq_file *m, void *p)
2106 up(&module_mutex);
2109 static int m_show(struct seq_file *m, void *p)
2111 struct module *mod = list_entry(p, struct module, list);
2112 seq_printf(m, "%s %lu",
2113 mod->name, mod->init_size + mod->core_size);
2114 print_unload_info(m, mod);
2116 /* Informative for users. */
2117 seq_printf(m, " %s",
2118 mod->state == MODULE_STATE_GOING ? "Unloading":
2119 mod->state == MODULE_STATE_COMING ? "Loading":
2120 "Live");
2121 /* Used by oprofile and other similar tools. */
2122 seq_printf(m, " 0x%p", mod->module_core);
2124 seq_printf(m, "\n");
2125 return 0;
2128 /* Format: modulename size refcount deps address
2130 Where refcount is a number or -, and deps is a comma-separated list
2131 of depends or -.
2133 struct seq_operations modules_op = {
2134 .start = m_start,
2135 .next = m_next,
2136 .stop = m_stop,
2137 .show = m_show
2140 /* Given an address, look for it in the module exception tables. */
2141 const struct exception_table_entry *search_module_extables(unsigned long addr)
2143 unsigned long flags;
2144 const struct exception_table_entry *e = NULL;
2145 struct module *mod;
2147 spin_lock_irqsave(&modlist_lock, flags);
2148 list_for_each_entry(mod, &modules, list) {
2149 if (mod->num_exentries == 0)
2150 continue;
2152 e = search_extable(mod->extable,
2153 mod->extable + mod->num_exentries - 1,
2154 addr);
2155 if (e)
2156 break;
2158 spin_unlock_irqrestore(&modlist_lock, flags);
2160 /* Now, if we found one, we are running inside it now, hence
2161 we cannot unload the module, hence no refcnt needed. */
2162 return e;
2165 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
2166 struct module *__module_text_address(unsigned long addr)
2168 struct module *mod;
2170 list_for_each_entry(mod, &modules, list)
2171 if (within(addr, mod->module_init, mod->init_text_size)
2172 || within(addr, mod->module_core, mod->core_text_size))
2173 return mod;
2174 return NULL;
2177 struct module *module_text_address(unsigned long addr)
2179 struct module *mod;
2180 unsigned long flags;
2182 spin_lock_irqsave(&modlist_lock, flags);
2183 mod = __module_text_address(addr);
2184 spin_unlock_irqrestore(&modlist_lock, flags);
2186 return mod;
2189 /* Don't grab lock, we're oopsing. */
2190 void print_modules(void)
2192 struct module *mod;
2194 printk("Modules linked in:");
2195 list_for_each_entry(mod, &modules, list)
2196 printk(" %s", mod->name);
2197 printk("\n");
2200 void module_add_driver(struct module *mod, struct device_driver *drv)
2202 if (!mod || !drv)
2203 return;
2205 /* Don't check return code; this call is idempotent */
2206 sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
2208 EXPORT_SYMBOL(module_add_driver);
2210 void module_remove_driver(struct device_driver *drv)
2212 if (!drv)
2213 return;
2214 sysfs_remove_link(&drv->kobj, "module");
2216 EXPORT_SYMBOL(module_remove_driver);
2218 #ifdef CONFIG_MODVERSIONS
2219 /* Generate the signature for struct module here, too, for modversions. */
2220 void struct_module(struct module *mod) { return; }
2221 EXPORT_SYMBOL(struct_module);
2222 #endif