[PATCH] update Changes for NFS changes
[linux-2.6/history.git] / kernel / module.c
blob58d73701bbdeeeed56076f68ec1083b112db9c73
1 /* Rewritten by Rusty Russell, on the backs of many others...
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/moduleloader.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/elf.h>
26 #include <linux/seq_file.h>
27 #include <linux/fcntl.h>
28 #include <linux/rcupdate.h>
29 #include <linux/cpu.h>
30 #include <linux/moduleparam.h>
31 #include <linux/errno.h>
32 #include <linux/err.h>
33 #include <linux/vermagic.h>
34 #include <linux/notifier.h>
35 #include <asm/uaccess.h>
36 #include <asm/semaphore.h>
37 #include <asm/pgalloc.h>
38 #include <asm/cacheflush.h>
40 #if 0
41 #define DEBUGP printk
42 #else
43 #define DEBUGP(fmt , a...)
44 #endif
46 #ifndef ARCH_SHF_SMALL
47 #define ARCH_SHF_SMALL 0
48 #endif
50 /* If this is set, the section belongs in the init part of the module */
51 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
53 #define symbol_is(literal, string) \
54 (strcmp(MODULE_SYMBOL_PREFIX literal, (string)) == 0)
56 /* Protects module list */
57 static spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
59 /* List of modules, protected by module_mutex AND modlist_lock */
60 static DECLARE_MUTEX(module_mutex);
61 static LIST_HEAD(modules);
63 static DECLARE_MUTEX(notify_mutex);
64 static struct notifier_block * module_notify_list;
66 int register_module_notifier(struct notifier_block * nb)
68 int err;
69 down(&notify_mutex);
70 err = notifier_chain_register(&module_notify_list, nb);
71 up(&notify_mutex);
72 return err;
74 EXPORT_SYMBOL(register_module_notifier);
76 int unregister_module_notifier(struct notifier_block * nb)
78 int err;
79 down(&notify_mutex);
80 err = notifier_chain_unregister(&module_notify_list, nb);
81 up(&notify_mutex);
82 return err;
84 EXPORT_SYMBOL(unregister_module_notifier);
86 /* We require a truly strong try_module_get() */
87 static inline int strong_try_module_get(struct module *mod)
89 if (mod && mod->state == MODULE_STATE_COMING)
90 return 0;
91 return try_module_get(mod);
94 /* Stub function for modules which don't have an initfn */
95 int init_module(void)
97 return 0;
99 EXPORT_SYMBOL(init_module);
101 /* A thread that wants to hold a reference to a module only while it
102 * is running can call ths to safely exit.
103 * nfsd and lockd use this.
105 void __module_put_and_exit(struct module *mod, long code)
107 module_put(mod);
108 do_exit(code);
110 EXPORT_SYMBOL(__module_put_and_exit);
112 /* Find a module section: 0 means not found. */
113 static unsigned int find_sec(Elf_Ehdr *hdr,
114 Elf_Shdr *sechdrs,
115 const char *secstrings,
116 const char *name)
118 unsigned int i;
120 for (i = 1; i < hdr->e_shnum; i++)
121 /* Alloc bit cleared means "ignore it." */
122 if ((sechdrs[i].sh_flags & SHF_ALLOC)
123 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
124 return i;
125 return 0;
128 /* Provided by the linker */
129 extern const struct kernel_symbol __start___ksymtab[];
130 extern const struct kernel_symbol __stop___ksymtab[];
131 extern const struct kernel_symbol __start___ksymtab_gpl[];
132 extern const struct kernel_symbol __stop___ksymtab_gpl[];
133 extern const unsigned long __start___kcrctab[];
134 extern const unsigned long __start___kcrctab_gpl[];
136 #ifndef CONFIG_MODVERSIONS
137 #define symversion(base, idx) NULL
138 #else
139 #define symversion(base, idx) ((base) ? ((base) + (idx)) : NULL)
140 #endif
142 /* Find a symbol, return value, crc and module which owns it */
143 static unsigned long __find_symbol(const char *name,
144 struct module **owner,
145 const unsigned long **crc,
146 int gplok)
148 struct module *mod;
149 unsigned int i;
151 /* Core kernel first. */
152 *owner = NULL;
153 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++) {
154 if (strcmp(__start___ksymtab[i].name, name) == 0) {
155 *crc = symversion(__start___kcrctab, i);
156 return __start___ksymtab[i].value;
159 if (gplok) {
160 for (i = 0; __start___ksymtab_gpl+i<__stop___ksymtab_gpl; i++)
161 if (strcmp(__start___ksymtab_gpl[i].name, name) == 0) {
162 *crc = symversion(__start___kcrctab_gpl, i);
163 return __start___ksymtab_gpl[i].value;
167 /* Now try modules. */
168 list_for_each_entry(mod, &modules, list) {
169 *owner = mod;
170 for (i = 0; i < mod->num_syms; i++)
171 if (strcmp(mod->syms[i].name, name) == 0) {
172 *crc = symversion(mod->crcs, i);
173 return mod->syms[i].value;
176 if (gplok) {
177 for (i = 0; i < mod->num_gpl_syms; i++) {
178 if (strcmp(mod->gpl_syms[i].name, name) == 0) {
179 *crc = symversion(mod->gpl_crcs, i);
180 return mod->gpl_syms[i].value;
185 DEBUGP("Failed to find symbol %s\n", name);
186 return 0;
189 /* Find a symbol in this elf symbol table */
190 static unsigned long find_local_symbol(Elf_Shdr *sechdrs,
191 unsigned int symindex,
192 const char *strtab,
193 const char *name)
195 unsigned int i;
196 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
198 /* Search (defined) internal symbols first. */
199 for (i = 1; i < sechdrs[symindex].sh_size/sizeof(*sym); i++) {
200 if (sym[i].st_shndx != SHN_UNDEF
201 && strcmp(name, strtab + sym[i].st_name) == 0)
202 return sym[i].st_value;
204 return 0;
207 /* Search for module by name: must hold module_mutex. */
208 static struct module *find_module(const char *name)
210 struct module *mod;
212 list_for_each_entry(mod, &modules, list) {
213 if (strcmp(mod->name, name) == 0)
214 return mod;
216 return NULL;
219 #ifdef CONFIG_SMP
220 /* Number of blocks used and allocated. */
221 static unsigned int pcpu_num_used, pcpu_num_allocated;
222 /* Size of each block. -ve means used. */
223 static int *pcpu_size;
225 static int split_block(unsigned int i, unsigned short size)
227 /* Reallocation required? */
228 if (pcpu_num_used + 1 > pcpu_num_allocated) {
229 int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
230 GFP_KERNEL);
231 if (!new)
232 return 0;
234 memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
235 pcpu_num_allocated *= 2;
236 kfree(pcpu_size);
237 pcpu_size = new;
240 /* Insert a new subblock */
241 memmove(&pcpu_size[i+1], &pcpu_size[i],
242 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
243 pcpu_num_used++;
245 pcpu_size[i+1] -= size;
246 pcpu_size[i] = size;
247 return 1;
250 static inline unsigned int block_size(int val)
252 if (val < 0)
253 return -val;
254 return val;
257 /* Created by linker magic */
258 extern char __per_cpu_start[], __per_cpu_end[];
260 static void *percpu_modalloc(unsigned long size, unsigned long align)
262 unsigned long extra;
263 unsigned int i;
264 void *ptr;
266 BUG_ON(align > SMP_CACHE_BYTES);
268 ptr = __per_cpu_start;
269 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
270 /* Extra for alignment requirement. */
271 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
272 BUG_ON(i == 0 && extra != 0);
274 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
275 continue;
277 /* Transfer extra to previous block. */
278 if (pcpu_size[i-1] < 0)
279 pcpu_size[i-1] -= extra;
280 else
281 pcpu_size[i-1] += extra;
282 pcpu_size[i] -= extra;
283 ptr += extra;
285 /* Split block if warranted */
286 if (pcpu_size[i] - size > sizeof(unsigned long))
287 if (!split_block(i, size))
288 return NULL;
290 /* Mark allocated */
291 pcpu_size[i] = -pcpu_size[i];
292 return ptr;
295 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
296 size);
297 return NULL;
300 static void percpu_modfree(void *freeme)
302 unsigned int i;
303 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
305 /* First entry is core kernel percpu data. */
306 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
307 if (ptr == freeme) {
308 pcpu_size[i] = -pcpu_size[i];
309 goto free;
312 BUG();
314 free:
315 /* Merge with previous? */
316 if (pcpu_size[i-1] >= 0) {
317 pcpu_size[i-1] += pcpu_size[i];
318 pcpu_num_used--;
319 memmove(&pcpu_size[i], &pcpu_size[i+1],
320 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
321 i--;
323 /* Merge with next? */
324 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
325 pcpu_size[i] += pcpu_size[i+1];
326 pcpu_num_used--;
327 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
328 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
332 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
333 Elf_Shdr *sechdrs,
334 const char *secstrings)
336 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
339 static int percpu_modinit(void)
341 pcpu_num_used = 2;
342 pcpu_num_allocated = 2;
343 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
344 GFP_KERNEL);
345 /* Static in-kernel percpu data (used). */
346 pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
347 /* Free room. */
348 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
349 if (pcpu_size[1] < 0) {
350 printk(KERN_ERR "No per-cpu room for modules.\n");
351 pcpu_num_used = 1;
354 return 0;
356 __initcall(percpu_modinit);
357 #else /* ... !CONFIG_SMP */
358 static inline void *percpu_modalloc(unsigned long size, unsigned long align)
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 /* Init the unload section of the module. */
382 static void module_unload_init(struct module *mod)
384 unsigned int i;
386 INIT_LIST_HEAD(&mod->modules_which_use_me);
387 for (i = 0; i < NR_CPUS; i++)
388 local_set(&mod->ref[i].count, 0);
389 /* Hold reference count during initialization. */
390 local_set(&mod->ref[smp_processor_id()].count, 1);
391 /* Backwards compatibility macros put refcount during init. */
392 mod->waiter = current;
395 /* modules using other modules */
396 struct module_use
398 struct list_head list;
399 struct module *module_which_uses;
402 /* Does a already use b? */
403 static int already_uses(struct module *a, struct module *b)
405 struct module_use *use;
407 list_for_each_entry(use, &b->modules_which_use_me, list) {
408 if (use->module_which_uses == a) {
409 DEBUGP("%s uses %s!\n", a->name, b->name);
410 return 1;
413 DEBUGP("%s does not use %s!\n", a->name, b->name);
414 return 0;
417 /* Module a uses b */
418 static int use_module(struct module *a, struct module *b)
420 struct module_use *use;
421 if (b == NULL || already_uses(a, b)) return 1;
423 if (!strong_try_module_get(b))
424 return 0;
426 DEBUGP("Allocating new usage for %s.\n", a->name);
427 use = kmalloc(sizeof(*use), GFP_ATOMIC);
428 if (!use) {
429 printk("%s: out of memory loading\n", a->name);
430 module_put(b);
431 return 0;
434 use->module_which_uses = a;
435 list_add(&use->list, &b->modules_which_use_me);
436 return 1;
439 /* Clear the unload stuff of the module. */
440 static void module_unload_free(struct module *mod)
442 struct module *i;
444 list_for_each_entry(i, &modules, list) {
445 struct module_use *use;
447 list_for_each_entry(use, &i->modules_which_use_me, list) {
448 if (use->module_which_uses == mod) {
449 DEBUGP("%s unusing %s\n", mod->name, i->name);
450 module_put(i);
451 list_del(&use->list);
452 kfree(use);
453 /* There can be at most one match. */
454 break;
460 #ifdef CONFIG_SMP
461 /* Thread to stop each CPU in user context. */
462 enum stopref_state {
463 STOPREF_WAIT,
464 STOPREF_PREPARE,
465 STOPREF_DISABLE_IRQ,
466 STOPREF_EXIT,
469 static enum stopref_state stopref_state;
470 static unsigned int stopref_num_threads;
471 static atomic_t stopref_thread_ack;
473 static int stopref(void *cpu)
475 int irqs_disabled = 0;
476 int prepared = 0;
478 sprintf(current->comm, "kmodule%lu\n", (unsigned long)cpu);
480 /* Highest priority we can manage, and move to right CPU. */
481 #if 0 /* FIXME */
482 struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 };
483 setscheduler(current->pid, SCHED_FIFO, &param);
484 #endif
485 set_cpus_allowed(current, 1UL << (unsigned long)cpu);
487 /* Ack: we are alive */
488 atomic_inc(&stopref_thread_ack);
490 /* Simple state machine */
491 while (stopref_state != STOPREF_EXIT) {
492 if (stopref_state == STOPREF_DISABLE_IRQ && !irqs_disabled) {
493 local_irq_disable();
494 irqs_disabled = 1;
495 /* Ack: irqs disabled. */
496 atomic_inc(&stopref_thread_ack);
497 } else if (stopref_state == STOPREF_PREPARE && !prepared) {
498 /* Everyone is in place, hold CPU. */
499 preempt_disable();
500 prepared = 1;
501 atomic_inc(&stopref_thread_ack);
503 if (irqs_disabled || prepared)
504 cpu_relax();
505 else
506 yield();
509 /* Ack: we are exiting. */
510 atomic_inc(&stopref_thread_ack);
512 if (irqs_disabled)
513 local_irq_enable();
514 if (prepared)
515 preempt_enable();
517 return 0;
520 /* Change the thread state */
521 static void stopref_set_state(enum stopref_state state, int sleep)
523 atomic_set(&stopref_thread_ack, 0);
524 wmb();
525 stopref_state = state;
526 while (atomic_read(&stopref_thread_ack) != stopref_num_threads) {
527 if (sleep)
528 yield();
529 else
530 cpu_relax();
534 /* Stop the machine. Disables irqs. */
535 static int stop_refcounts(void)
537 unsigned int i, cpu;
538 unsigned long old_allowed;
539 int ret = 0;
541 /* One thread per cpu. We'll do our own. */
542 cpu = smp_processor_id();
544 /* FIXME: racy with set_cpus_allowed. */
545 old_allowed = current->cpus_allowed;
546 set_cpus_allowed(current, 1UL << (unsigned long)cpu);
548 atomic_set(&stopref_thread_ack, 0);
549 stopref_num_threads = 0;
550 stopref_state = STOPREF_WAIT;
552 /* No CPUs can come up or down during this. */
553 down(&cpucontrol);
555 for (i = 0; i < NR_CPUS; i++) {
556 if (i == cpu || !cpu_online(i))
557 continue;
558 ret = kernel_thread(stopref, (void *)(long)i, CLONE_KERNEL);
559 if (ret < 0)
560 break;
561 stopref_num_threads++;
564 /* Wait for them all to come to life. */
565 while (atomic_read(&stopref_thread_ack) != stopref_num_threads)
566 yield();
568 /* If some failed, kill them all. */
569 if (ret < 0) {
570 stopref_set_state(STOPREF_EXIT, 1);
571 up(&cpucontrol);
572 return ret;
575 /* Don't schedule us away at this point, please. */
576 preempt_disable();
578 /* Now they are all scheduled, make them hold the CPUs, ready. */
579 stopref_set_state(STOPREF_PREPARE, 0);
581 /* Make them disable irqs. */
582 stopref_set_state(STOPREF_DISABLE_IRQ, 0);
584 local_irq_disable();
585 return 0;
588 /* Restart the machine. Re-enables irqs. */
589 static void restart_refcounts(void)
591 stopref_set_state(STOPREF_EXIT, 0);
592 local_irq_enable();
593 preempt_enable();
594 up(&cpucontrol);
596 #else /* ...!SMP */
597 static inline int stop_refcounts(void)
599 local_irq_disable();
600 return 0;
602 static inline void restart_refcounts(void)
604 local_irq_enable();
606 #endif
608 unsigned int module_refcount(struct module *mod)
610 unsigned int i, total = 0;
612 for (i = 0; i < NR_CPUS; i++)
613 total += local_read(&mod->ref[i].count);
614 return total;
616 EXPORT_SYMBOL(module_refcount);
618 /* This exists whether we can unload or not */
619 static void free_module(struct module *mod);
621 #ifdef CONFIG_MODULE_FORCE_UNLOAD
622 static inline int try_force(unsigned int flags)
624 int ret = (flags & O_TRUNC);
625 if (ret)
626 tainted |= TAINT_FORCED_MODULE;
627 return ret;
629 #else
630 static inline int try_force(unsigned int flags)
632 return 0;
634 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
636 /* Stub function for modules which don't have an exitfn */
637 void cleanup_module(void)
640 EXPORT_SYMBOL(cleanup_module);
642 static void wait_for_zero_refcount(struct module *mod)
644 /* Since we might sleep for some time, drop the semaphore first */
645 up(&module_mutex);
646 for (;;) {
647 DEBUGP("Looking at refcount...\n");
648 set_current_state(TASK_UNINTERRUPTIBLE);
649 if (module_refcount(mod) == 0)
650 break;
651 schedule();
653 current->state = TASK_RUNNING;
654 down(&module_mutex);
657 asmlinkage long
658 sys_delete_module(const char __user *name_user, unsigned int flags)
660 struct module *mod;
661 char name[MODULE_NAME_LEN];
662 int ret, forced = 0;
664 if (!capable(CAP_SYS_MODULE))
665 return -EPERM;
667 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
668 return -EFAULT;
669 name[MODULE_NAME_LEN-1] = '\0';
671 if (down_interruptible(&module_mutex) != 0)
672 return -EINTR;
674 mod = find_module(name);
675 if (!mod) {
676 ret = -ENOENT;
677 goto out;
680 if (!list_empty(&mod->modules_which_use_me)) {
681 /* Other modules depend on us: get rid of them first. */
682 ret = -EWOULDBLOCK;
683 goto out;
686 /* Already dying? */
687 if (mod->state == MODULE_STATE_GOING) {
688 /* FIXME: if (force), slam module count and wake up
689 waiter --RR */
690 DEBUGP("%s already dying\n", mod->name);
691 ret = -EBUSY;
692 goto out;
695 /* If it has an init func, it must have an exit func to unload */
696 if ((mod->init != init_module && mod->exit == cleanup_module)
697 || mod->unsafe) {
698 forced = try_force(flags);
699 if (!forced) {
700 /* This module can't be removed */
701 ret = -EBUSY;
702 goto out;
705 /* Stop the machine so refcounts can't move: irqs disabled. */
706 DEBUGP("Stopping refcounts...\n");
707 ret = stop_refcounts();
708 if (ret != 0)
709 goto out;
711 /* If it's not unused, quit unless we are told to block. */
712 if ((flags & O_NONBLOCK) && module_refcount(mod) != 0) {
713 forced = try_force(flags);
714 if (!forced) {
715 ret = -EWOULDBLOCK;
716 restart_refcounts();
717 goto out;
721 /* Mark it as dying. */
722 mod->waiter = current;
723 mod->state = MODULE_STATE_GOING;
724 restart_refcounts();
726 /* Never wait if forced. */
727 if (!forced && module_refcount(mod) != 0)
728 wait_for_zero_refcount(mod);
730 /* Final destruction now noone is using it. */
731 mod->exit();
732 free_module(mod);
734 out:
735 up(&module_mutex);
736 return ret;
739 static void print_unload_info(struct seq_file *m, struct module *mod)
741 struct module_use *use;
742 int printed_something = 0;
744 seq_printf(m, " %u ", module_refcount(mod));
746 /* Always include a trailing , so userspace can differentiate
747 between this and the old multi-field proc format. */
748 list_for_each_entry(use, &mod->modules_which_use_me, list) {
749 printed_something = 1;
750 seq_printf(m, "%s,", use->module_which_uses->name);
753 if (mod->unsafe) {
754 printed_something = 1;
755 seq_printf(m, "[unsafe],");
758 if (mod->init != init_module && mod->exit == cleanup_module) {
759 printed_something = 1;
760 seq_printf(m, "[permanent],");
763 if (!printed_something)
764 seq_printf(m, "-");
767 void __symbol_put(const char *symbol)
769 struct module *owner;
770 unsigned long flags;
771 const unsigned long *crc;
773 spin_lock_irqsave(&modlist_lock, flags);
774 if (!__find_symbol(symbol, &owner, &crc, 1))
775 BUG();
776 module_put(owner);
777 spin_unlock_irqrestore(&modlist_lock, flags);
779 EXPORT_SYMBOL(__symbol_put);
781 void symbol_put_addr(void *addr)
783 unsigned long flags;
785 spin_lock_irqsave(&modlist_lock, flags);
786 if (!kernel_text_address((unsigned long)addr))
787 BUG();
789 module_put(module_text_address((unsigned long)addr));
790 spin_unlock_irqrestore(&modlist_lock, flags);
792 EXPORT_SYMBOL_GPL(symbol_put_addr);
794 #else /* !CONFIG_MODULE_UNLOAD */
795 static void print_unload_info(struct seq_file *m, struct module *mod)
797 /* We don't know the usage count, or what modules are using. */
798 seq_printf(m, " - -");
801 static inline void module_unload_free(struct module *mod)
805 static inline int use_module(struct module *a, struct module *b)
807 return strong_try_module_get(b);
810 static inline void module_unload_init(struct module *mod)
814 asmlinkage long
815 sys_delete_module(const char *name_user, unsigned int flags)
817 return -ENOSYS;
820 #endif /* CONFIG_MODULE_UNLOAD */
822 #ifdef CONFIG_OBSOLETE_MODPARM
823 static int param_set_byte(const char *val, struct kernel_param *kp)
825 char *endp;
826 long l;
828 if (!val) return -EINVAL;
829 l = simple_strtol(val, &endp, 0);
830 if (endp == val || *endp || ((char)l != l))
831 return -EINVAL;
832 *((char *)kp->arg) = l;
833 return 0;
836 /* Bounds checking done below */
837 static int obsparm_copy_string(const char *val, struct kernel_param *kp)
839 strcpy(kp->arg, val);
840 return 0;
843 int set_obsolete(const char *val, struct kernel_param *kp)
845 unsigned int min, max;
846 unsigned int size, maxsize;
847 char *endp;
848 const char *p;
849 struct obsolete_modparm *obsparm = kp->arg;
851 if (!val) {
852 printk(KERN_ERR "Parameter %s needs an argument\n", kp->name);
853 return -EINVAL;
856 /* type is: [min[-max]]{b,h,i,l,s} */
857 p = obsparm->type;
858 min = simple_strtol(p, &endp, 10);
859 if (endp == obsparm->type)
860 min = max = 1;
861 else if (*endp == '-') {
862 p = endp+1;
863 max = simple_strtol(p, &endp, 10);
864 } else
865 max = min;
866 switch (*endp) {
867 case 'b':
868 return param_array(kp->name, val, min, max, obsparm->addr,
869 1, param_set_byte);
870 case 'h':
871 return param_array(kp->name, val, min, max, obsparm->addr,
872 sizeof(short), param_set_short);
873 case 'i':
874 return param_array(kp->name, val, min, max, obsparm->addr,
875 sizeof(int), param_set_int);
876 case 'l':
877 return param_array(kp->name, val, min, max, obsparm->addr,
878 sizeof(long), param_set_long);
879 case 's':
880 return param_array(kp->name, val, min, max, obsparm->addr,
881 sizeof(char *), param_set_charp);
883 case 'c':
884 /* Undocumented: 1-5c50 means 1-5 strings of up to 49 chars,
885 and the decl is "char xxx[5][50];" */
886 p = endp+1;
887 maxsize = simple_strtol(p, &endp, 10);
888 /* We check lengths here (yes, this is a hack). */
889 p = val;
890 while (p[size = strcspn(p, ",")]) {
891 if (size >= maxsize)
892 goto oversize;
893 p += size+1;
895 if (size >= maxsize)
896 goto oversize;
897 return param_array(kp->name, val, min, max, obsparm->addr,
898 maxsize, obsparm_copy_string);
900 printk(KERN_ERR "Unknown obsolete parameter type %s\n", obsparm->type);
901 return -EINVAL;
902 oversize:
903 printk(KERN_ERR
904 "Parameter %s doesn't fit in %u chars.\n", kp->name, maxsize);
905 return -EINVAL;
908 static int obsolete_params(const char *name,
909 char *args,
910 struct obsolete_modparm obsparm[],
911 unsigned int num,
912 Elf_Shdr *sechdrs,
913 unsigned int symindex,
914 const char *strtab)
916 struct kernel_param *kp;
917 unsigned int i;
918 int ret;
920 kp = kmalloc(sizeof(kp[0]) * num, GFP_KERNEL);
921 if (!kp)
922 return -ENOMEM;
924 for (i = 0; i < num; i++) {
925 char sym_name[128 + sizeof(MODULE_SYMBOL_PREFIX)];
927 snprintf(sym_name, sizeof(sym_name), "%s%s",
928 MODULE_SYMBOL_PREFIX, obsparm[i].name);
930 kp[i].name = obsparm[i].name;
931 kp[i].perm = 000;
932 kp[i].set = set_obsolete;
933 kp[i].get = NULL;
934 obsparm[i].addr
935 = (void *)find_local_symbol(sechdrs, symindex, strtab,
936 sym_name);
937 if (!obsparm[i].addr) {
938 printk("%s: falsely claims to have parameter %s\n",
939 name, obsparm[i].name);
940 ret = -EINVAL;
941 goto out;
943 kp[i].arg = &obsparm[i];
946 ret = parse_args(name, args, kp, num, NULL);
947 out:
948 kfree(kp);
949 return ret;
951 #else
952 static int obsolete_params(const char *name,
953 char *args,
954 struct obsolete_modparm obsparm[],
955 unsigned int num,
956 Elf_Shdr *sechdrs,
957 unsigned int symindex,
958 const char *strtab)
960 if (num != 0)
961 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
962 name);
963 return 0;
965 #endif /* CONFIG_OBSOLETE_MODPARM */
967 static const char vermagic[] = VERMAGIC_STRING;
969 #ifdef CONFIG_MODVERSIONS
970 static int check_version(Elf_Shdr *sechdrs,
971 unsigned int versindex,
972 const char *symname,
973 struct module *mod,
974 const unsigned long *crc)
976 unsigned int i, num_versions;
977 struct modversion_info *versions;
979 /* Exporting module didn't supply crcs? OK, we're already tainted. */
980 if (!crc)
981 return 1;
983 versions = (void *) sechdrs[versindex].sh_addr;
984 num_versions = sechdrs[versindex].sh_size
985 / sizeof(struct modversion_info);
987 for (i = 0; i < num_versions; i++) {
988 if (strcmp(versions[i].name, symname) != 0)
989 continue;
991 if (versions[i].crc == *crc)
992 return 1;
993 printk("%s: disagrees about version of symbol %s\n",
994 mod->name, symname);
995 DEBUGP("Found checksum %lX vs module %lX\n",
996 *crc, versions[i].crc);
997 return 0;
999 /* Not in module's version table. OK, but that taints the kernel. */
1000 if (!(tainted & TAINT_FORCED_MODULE)) {
1001 printk("%s: no version for \"%s\" found: kernel tainted.\n",
1002 mod->name, symname);
1003 tainted |= TAINT_FORCED_MODULE;
1005 return 1;
1008 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1009 unsigned int versindex,
1010 struct module *mod)
1012 const unsigned long *crc;
1013 struct module *owner;
1015 if (!__find_symbol("struct_module", &owner, &crc, 1))
1016 BUG();
1017 return check_version(sechdrs, versindex, "struct_module", mod,
1018 crc);
1021 /* First part is kernel version, which we ignore. */
1022 static inline int same_magic(const char *amagic, const char *bmagic)
1024 amagic += strcspn(amagic, " ");
1025 bmagic += strcspn(bmagic, " ");
1026 return strcmp(amagic, bmagic) == 0;
1028 #else
1029 static inline int check_version(Elf_Shdr *sechdrs,
1030 unsigned int versindex,
1031 const char *symname,
1032 struct module *mod,
1033 const unsigned long *crc)
1035 return 1;
1038 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1039 unsigned int versindex,
1040 struct module *mod)
1042 return 1;
1045 static inline int same_magic(const char *amagic, const char *bmagic)
1047 return strcmp(amagic, bmagic) == 0;
1049 #endif /* CONFIG_MODVERSIONS */
1051 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1052 Must be holding module_mutex. */
1053 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1054 unsigned int versindex,
1055 const char *name,
1056 struct module *mod)
1058 struct module *owner;
1059 unsigned long ret;
1060 const unsigned long *crc;
1062 spin_lock_irq(&modlist_lock);
1063 ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
1064 if (ret) {
1065 /* use_module can fail due to OOM, or module unloading */
1066 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1067 !use_module(mod, owner))
1068 ret = 0;
1070 spin_unlock_irq(&modlist_lock);
1071 return ret;
1074 /* Free a module, remove from lists, etc (must hold module mutex). */
1075 static void free_module(struct module *mod)
1077 /* Delete from various lists */
1078 spin_lock_irq(&modlist_lock);
1079 list_del(&mod->list);
1080 spin_unlock_irq(&modlist_lock);
1082 /* Arch-specific cleanup. */
1083 module_arch_cleanup(mod);
1085 /* Module unload stuff */
1086 module_unload_free(mod);
1088 /* This may be NULL, but that's OK */
1089 module_free(mod, mod->module_init);
1090 kfree(mod->args);
1091 if (mod->percpu)
1092 percpu_modfree(mod->percpu);
1094 /* Finally, free the core (containing the module structure) */
1095 module_free(mod, mod->module_core);
1098 void *__symbol_get(const char *symbol)
1100 struct module *owner;
1101 unsigned long value, flags;
1102 const unsigned long *crc;
1104 spin_lock_irqsave(&modlist_lock, flags);
1105 value = __find_symbol(symbol, &owner, &crc, 1);
1106 if (value && !strong_try_module_get(owner))
1107 value = 0;
1108 spin_unlock_irqrestore(&modlist_lock, flags);
1110 return (void *)value;
1112 EXPORT_SYMBOL_GPL(__symbol_get);
1114 /* Change all symbols so that sh_value encodes the pointer directly. */
1115 static int simplify_symbols(Elf_Shdr *sechdrs,
1116 unsigned int symindex,
1117 const char *strtab,
1118 unsigned int versindex,
1119 unsigned int pcpuindex,
1120 struct module *mod)
1122 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1123 unsigned long secbase;
1124 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1125 int ret = 0;
1127 for (i = 1; i < n; i++) {
1128 switch (sym[i].st_shndx) {
1129 case SHN_COMMON:
1130 /* We compiled with -fno-common. These are not
1131 supposed to happen. */
1132 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1133 ret = -ENOEXEC;
1134 break;
1136 case SHN_ABS:
1137 /* Don't need to do anything */
1138 DEBUGP("Absolute symbol: 0x%08lx\n",
1139 (long)sym[i].st_value);
1140 break;
1142 case SHN_UNDEF:
1143 sym[i].st_value
1144 = resolve_symbol(sechdrs, versindex,
1145 strtab + sym[i].st_name, mod);
1147 /* Ok if resolved. */
1148 if (sym[i].st_value != 0)
1149 break;
1150 /* Ok if weak. */
1151 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1152 break;
1154 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1155 mod->name, strtab + sym[i].st_name);
1156 ret = -ENOENT;
1157 break;
1159 default:
1160 /* Divert to percpu allocation if a percpu var. */
1161 if (sym[i].st_shndx == pcpuindex)
1162 secbase = (unsigned long)mod->percpu;
1163 else
1164 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1165 sym[i].st_value += secbase;
1166 break;
1170 return ret;
1173 /* Update size with this section: return offset. */
1174 static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
1176 long ret;
1178 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1179 *size = ret + sechdr->sh_size;
1180 return ret;
1183 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1184 might -- code, read-only data, read-write data, small data. Tally
1185 sizes, and place the offsets into sh_entsize fields: high bit means it
1186 belongs in init. */
1187 static void layout_sections(struct module *mod,
1188 const Elf_Ehdr *hdr,
1189 Elf_Shdr *sechdrs,
1190 const char *secstrings)
1192 static unsigned long const masks[][2] = {
1193 /* NOTE: all executable code must be the first section
1194 * in this array; otherwise modify the text_size
1195 * finder in the two loops below */
1196 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1197 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1198 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1199 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1201 unsigned int m, i;
1203 for (i = 0; i < hdr->e_shnum; i++)
1204 sechdrs[i].sh_entsize = ~0UL;
1206 DEBUGP("Core section allocation order:\n");
1207 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1208 for (i = 0; i < hdr->e_shnum; ++i) {
1209 Elf_Shdr *s = &sechdrs[i];
1211 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1212 || (s->sh_flags & masks[m][1])
1213 || s->sh_entsize != ~0UL
1214 || strstr(secstrings + s->sh_name, ".init"))
1215 continue;
1216 s->sh_entsize = get_offset(&mod->core_size, s);
1217 DEBUGP("\t%s\n", secstrings + s->sh_name);
1219 if (m == 0)
1220 mod->core_text_size = mod->core_size;
1223 DEBUGP("Init section allocation order:\n");
1224 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1225 for (i = 0; i < hdr->e_shnum; ++i) {
1226 Elf_Shdr *s = &sechdrs[i];
1228 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1229 || (s->sh_flags & masks[m][1])
1230 || s->sh_entsize != ~0UL
1231 || !strstr(secstrings + s->sh_name, ".init"))
1232 continue;
1233 s->sh_entsize = (get_offset(&mod->init_size, s)
1234 | INIT_OFFSET_MASK);
1235 DEBUGP("\t%s\n", secstrings + s->sh_name);
1237 if (m == 0)
1238 mod->init_text_size = mod->init_size;
1242 static inline int license_is_gpl_compatible(const char *license)
1244 return (strcmp(license, "GPL") == 0
1245 || strcmp(license, "GPL v2") == 0
1246 || strcmp(license, "GPL and additional rights") == 0
1247 || strcmp(license, "Dual BSD/GPL") == 0
1248 || strcmp(license, "Dual MPL/GPL") == 0);
1251 static void set_license(struct module *mod, const char *license)
1253 if (!license)
1254 license = "unspecified";
1256 mod->license_gplok = license_is_gpl_compatible(license);
1257 if (!mod->license_gplok) {
1258 printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
1259 mod->name, license);
1260 tainted |= TAINT_PROPRIETARY_MODULE;
1264 /* Parse tag=value strings from .modinfo section */
1265 static char *next_string(char *string, unsigned long *secsize)
1267 /* Skip non-zero chars */
1268 while (string[0]) {
1269 string++;
1270 if ((*secsize)-- <= 1)
1271 return NULL;
1274 /* Skip any zero padding. */
1275 while (!string[0]) {
1276 string++;
1277 if ((*secsize)-- <= 1)
1278 return NULL;
1280 return string;
1283 static char *get_modinfo(Elf_Shdr *sechdrs,
1284 unsigned int info,
1285 const char *tag)
1287 char *p;
1288 unsigned int taglen = strlen(tag);
1289 unsigned long size = sechdrs[info].sh_size;
1291 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1292 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1293 return p + taglen + 1;
1295 return NULL;
1298 #ifdef CONFIG_KALLSYMS
1299 int is_exported(const char *name, const struct module *mod)
1301 unsigned int i;
1303 if (!mod) {
1304 for (i = 0; __start___ksymtab+i < __stop___ksymtab; i++)
1305 if (strcmp(__start___ksymtab[i].name, name) == 0)
1306 return 1;
1307 return 0;
1309 for (i = 0; i < mod->num_syms; i++)
1310 if (strcmp(mod->syms[i].name, name) == 0)
1311 return 1;
1312 return 0;
1315 /* As per nm */
1316 static char elf_type(const Elf_Sym *sym,
1317 Elf_Shdr *sechdrs,
1318 const char *secstrings,
1319 struct module *mod)
1321 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1322 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1323 return 'v';
1324 else
1325 return 'w';
1327 if (sym->st_shndx == SHN_UNDEF)
1328 return 'U';
1329 if (sym->st_shndx == SHN_ABS)
1330 return 'a';
1331 if (sym->st_shndx >= SHN_LORESERVE)
1332 return '?';
1333 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1334 return 't';
1335 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1336 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1337 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1338 return 'r';
1339 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1340 return 'g';
1341 else
1342 return 'd';
1344 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1345 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1346 return 's';
1347 else
1348 return 'b';
1350 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1351 ".debug", strlen(".debug")) == 0)
1352 return 'n';
1353 return '?';
1356 static void add_kallsyms(struct module *mod,
1357 Elf_Shdr *sechdrs,
1358 unsigned int symindex,
1359 unsigned int strindex,
1360 const char *secstrings)
1362 unsigned int i;
1364 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1365 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1366 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1368 /* Set types up while we still have access to sections. */
1369 for (i = 0; i < mod->num_symtab; i++)
1370 mod->symtab[i].st_info
1371 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1373 #endif
1375 /* Allocate and load the module: note that size of section 0 is always
1376 zero, and we rely on this for optional sections. */
1377 static struct module *load_module(void __user *umod,
1378 unsigned long len,
1379 const char __user *uargs)
1381 Elf_Ehdr *hdr;
1382 Elf_Shdr *sechdrs;
1383 char *secstrings, *args, *modmagic, *strtab = NULL;
1384 unsigned int i, symindex = 0, strindex = 0, setupindex, exindex,
1385 exportindex, modindex, obsparmindex, infoindex, gplindex,
1386 crcindex, gplcrcindex, versindex, pcpuindex;
1387 long arglen;
1388 struct module *mod;
1389 long err = 0;
1390 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1392 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1393 umod, len, uargs);
1394 if (len < sizeof(*hdr))
1395 return ERR_PTR(-ENOEXEC);
1397 /* Suck in entire file: we'll want most of it. */
1398 /* vmalloc barfs on "unusual" numbers. Check here */
1399 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1400 return ERR_PTR(-ENOMEM);
1401 if (copy_from_user(hdr, umod, len) != 0) {
1402 err = -EFAULT;
1403 goto free_hdr;
1406 /* Sanity checks against insmoding binaries or wrong arch,
1407 weird elf version */
1408 if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
1409 || hdr->e_type != ET_REL
1410 || !elf_check_arch(hdr)
1411 || hdr->e_shentsize != sizeof(*sechdrs)) {
1412 err = -ENOEXEC;
1413 goto free_hdr;
1416 /* Convenience variables */
1417 sechdrs = (void *)hdr + hdr->e_shoff;
1418 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1419 sechdrs[0].sh_addr = 0;
1421 /* And these should exist, but gcc whinges if we don't init them */
1422 symindex = strindex = 0;
1424 for (i = 1; i < hdr->e_shnum; i++) {
1425 /* Mark all sections sh_addr with their address in the
1426 temporary image. */
1427 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1429 /* Internal symbols and strings. */
1430 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1431 symindex = i;
1432 strindex = sechdrs[i].sh_link;
1433 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1435 #ifndef CONFIG_MODULE_UNLOAD
1436 /* Don't load .exit sections */
1437 if (strstr(secstrings+sechdrs[i].sh_name, ".exit"))
1438 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1439 #endif
1442 modindex = find_sec(hdr, sechdrs, secstrings,
1443 ".gnu.linkonce.this_module");
1444 if (!modindex) {
1445 printk(KERN_WARNING "No module found in object\n");
1446 err = -ENOEXEC;
1447 goto free_hdr;
1449 mod = (void *)sechdrs[modindex].sh_addr;
1451 /* Optional sections */
1452 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1453 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1454 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1455 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1456 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1457 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1458 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1459 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1460 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1461 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1463 /* Don't keep modinfo section */
1464 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1465 #ifdef CONFIG_KALLSYMS
1466 /* Keep symbol and string tables for decoding later. */
1467 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1468 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1469 #endif
1471 /* Check module struct version now, before we try to use module. */
1472 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1473 err = -ENOEXEC;
1474 goto free_hdr;
1477 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1478 /* This is allowed: modprobe --force will invalidate it. */
1479 if (!modmagic) {
1480 tainted |= TAINT_FORCED_MODULE;
1481 printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
1482 mod->name);
1483 } else if (!same_magic(modmagic, vermagic)) {
1484 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1485 mod->name, modmagic, vermagic);
1486 err = -ENOEXEC;
1487 goto free_hdr;
1490 /* Now copy in args */
1491 arglen = strlen_user(uargs);
1492 if (!arglen) {
1493 err = -EFAULT;
1494 goto free_hdr;
1496 args = kmalloc(arglen, GFP_KERNEL);
1497 if (!args) {
1498 err = -ENOMEM;
1499 goto free_hdr;
1501 if (copy_from_user(args, uargs, arglen) != 0) {
1502 err = -EFAULT;
1503 goto free_mod;
1506 if (find_module(mod->name)) {
1507 err = -EEXIST;
1508 goto free_mod;
1511 mod->state = MODULE_STATE_COMING;
1513 /* Allow arches to frob section contents and sizes. */
1514 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
1515 if (err < 0)
1516 goto free_mod;
1518 if (pcpuindex) {
1519 /* We have a special allocation for this section. */
1520 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
1521 sechdrs[pcpuindex].sh_addralign);
1522 if (!percpu) {
1523 err = -ENOMEM;
1524 goto free_mod;
1526 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1527 mod->percpu = percpu;
1530 /* Determine total sizes, and put offsets in sh_entsize. For now
1531 this is done generically; there doesn't appear to be any
1532 special cases for the architectures. */
1533 layout_sections(mod, hdr, sechdrs, secstrings);
1535 /* Do the allocs. */
1536 ptr = module_alloc(mod->core_size);
1537 if (!ptr) {
1538 err = -ENOMEM;
1539 goto free_percpu;
1541 memset(ptr, 0, mod->core_size);
1542 mod->module_core = ptr;
1544 ptr = module_alloc(mod->init_size);
1545 if (!ptr && mod->init_size) {
1546 err = -ENOMEM;
1547 goto free_core;
1549 memset(ptr, 0, mod->init_size);
1550 mod->module_init = ptr;
1552 /* Transfer each section which specifies SHF_ALLOC */
1553 DEBUGP("final section addresses:\n");
1554 for (i = 0; i < hdr->e_shnum; i++) {
1555 void *dest;
1557 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1558 continue;
1560 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
1561 dest = mod->module_init
1562 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
1563 else
1564 dest = mod->module_core + sechdrs[i].sh_entsize;
1566 if (sechdrs[i].sh_type != SHT_NOBITS)
1567 memcpy(dest, (void *)sechdrs[i].sh_addr,
1568 sechdrs[i].sh_size);
1569 /* Update sh_addr to point to copy in image. */
1570 sechdrs[i].sh_addr = (unsigned long)dest;
1571 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
1573 /* Module has been moved. */
1574 mod = (void *)sechdrs[modindex].sh_addr;
1576 /* Now we've moved module, initialize linked lists, etc. */
1577 module_unload_init(mod);
1579 /* Set up license info based on the info section */
1580 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
1582 /* Fix up syms, so that st_value is a pointer to location. */
1583 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
1584 mod);
1585 if (err < 0)
1586 goto cleanup;
1588 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
1589 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
1590 mod->syms = (void *)sechdrs[exportindex].sh_addr;
1591 if (crcindex)
1592 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
1593 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
1594 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
1595 if (gplcrcindex)
1596 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
1598 #ifdef CONFIG_MODVERSIONS
1599 if ((mod->num_syms && !crcindex) ||
1600 (mod->num_gpl_syms && !gplcrcindex)) {
1601 printk(KERN_WARNING "%s: No versions for exported symbols."
1602 " Tainting kernel.\n", mod->name);
1603 tainted |= TAINT_FORCED_MODULE;
1605 #endif
1607 /* Set up exception table */
1608 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
1609 mod->extable = (void *)sechdrs[exindex].sh_addr;
1611 /* Now do relocations. */
1612 for (i = 1; i < hdr->e_shnum; i++) {
1613 const char *strtab = (char *)sechdrs[strindex].sh_addr;
1614 if (sechdrs[i].sh_type == SHT_REL)
1615 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
1616 else if (sechdrs[i].sh_type == SHT_RELA)
1617 err = apply_relocate_add(sechdrs, strtab, symindex, i,
1618 mod);
1619 if (err < 0)
1620 goto cleanup;
1623 /* Finally, copy percpu area over. */
1624 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
1625 sechdrs[pcpuindex].sh_size);
1627 err = module_finalize(hdr, sechdrs, mod);
1628 if (err < 0)
1629 goto cleanup;
1631 #ifdef CONFIG_KALLSYMS
1632 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
1633 #endif
1635 mod->args = args;
1636 if (obsparmindex) {
1637 err = obsolete_params(mod->name, mod->args,
1638 (struct obsolete_modparm *)
1639 sechdrs[obsparmindex].sh_addr,
1640 sechdrs[obsparmindex].sh_size
1641 / sizeof(struct obsolete_modparm),
1642 sechdrs, symindex,
1643 (char *)sechdrs[strindex].sh_addr);
1644 } else {
1645 /* Size of section 0 is 0, so this works well if no params */
1646 err = parse_args(mod->name, mod->args,
1647 (struct kernel_param *)
1648 sechdrs[setupindex].sh_addr,
1649 sechdrs[setupindex].sh_size
1650 / sizeof(struct kernel_param),
1651 NULL);
1653 if (err < 0)
1654 goto cleanup;
1656 /* Get rid of temporary copy */
1657 vfree(hdr);
1659 /* Done! */
1660 return mod;
1662 cleanup:
1663 module_unload_free(mod);
1664 module_free(mod, mod->module_init);
1665 free_core:
1666 module_free(mod, mod->module_core);
1667 free_percpu:
1668 if (percpu)
1669 percpu_modfree(percpu);
1670 free_mod:
1671 kfree(args);
1672 free_hdr:
1673 vfree(hdr);
1674 if (err < 0) return ERR_PTR(err);
1675 else return ptr;
1678 /* This is where the real work happens */
1679 asmlinkage long
1680 sys_init_module(void __user *umod,
1681 unsigned long len,
1682 const char __user *uargs)
1684 struct module *mod;
1685 int ret;
1687 /* Must have permission */
1688 if (!capable(CAP_SYS_MODULE))
1689 return -EPERM;
1691 /* Only one module load at a time, please */
1692 if (down_interruptible(&module_mutex) != 0)
1693 return -EINTR;
1695 /* Do all the hard work */
1696 mod = load_module(umod, len, uargs);
1697 if (IS_ERR(mod)) {
1698 up(&module_mutex);
1699 return PTR_ERR(mod);
1702 /* Flush the instruction cache, since we've played with text */
1703 if (mod->module_init)
1704 flush_icache_range((unsigned long)mod->module_init,
1705 (unsigned long)mod->module_init
1706 + mod->init_size);
1707 flush_icache_range((unsigned long)mod->module_core,
1708 (unsigned long)mod->module_core + mod->core_size);
1710 /* Now sew it into the lists. They won't access us, since
1711 strong_try_module_get() will fail. */
1712 spin_lock_irq(&modlist_lock);
1713 list_add(&mod->list, &modules);
1714 spin_unlock_irq(&modlist_lock);
1716 /* Drop lock so they can recurse */
1717 up(&module_mutex);
1719 down(&notify_mutex);
1720 notifier_call_chain(&module_notify_list, MODULE_STATE_COMING, mod);
1721 up(&notify_mutex);
1723 /* Start the module */
1724 ret = mod->init();
1725 if (ret < 0) {
1726 /* Init routine failed: abort. Try to protect us from
1727 buggy refcounters. */
1728 mod->state = MODULE_STATE_GOING;
1729 synchronize_kernel();
1730 if (mod->unsafe)
1731 printk(KERN_ERR "%s: module is now stuck!\n",
1732 mod->name);
1733 else {
1734 module_put(mod);
1735 down(&module_mutex);
1736 free_module(mod);
1737 up(&module_mutex);
1739 return ret;
1742 /* Now it's a first class citizen! */
1743 down(&module_mutex);
1744 mod->state = MODULE_STATE_LIVE;
1745 /* Drop initial reference. */
1746 module_put(mod);
1747 module_free(mod, mod->module_init);
1748 mod->module_init = NULL;
1749 mod->init_size = 0;
1750 mod->init_text_size = 0;
1751 up(&module_mutex);
1753 return 0;
1756 static inline int within(unsigned long addr, void *start, unsigned long size)
1758 return ((void *)addr >= start && (void *)addr < start + size);
1761 #ifdef CONFIG_KALLSYMS
1762 static const char *get_ksymbol(struct module *mod,
1763 unsigned long addr,
1764 unsigned long *size,
1765 unsigned long *offset)
1767 unsigned int i, best = 0;
1768 unsigned long nextval;
1770 /* At worse, next value is at end of module */
1771 if (within(addr, mod->module_init, mod->init_size))
1772 nextval = (unsigned long)mod->module_init+mod->init_text_size;
1773 else
1774 nextval = (unsigned long)mod->module_core+mod->core_text_size;
1776 /* Scan for closest preceeding symbol, and next symbol. (ELF
1777 starts real symbols at 1). */
1778 for (i = 1; i < mod->num_symtab; i++) {
1779 if (mod->symtab[i].st_shndx == SHN_UNDEF)
1780 continue;
1782 /* We ignore unnamed symbols: they're uninformative
1783 * and inserted at a whim. */
1784 if (mod->symtab[i].st_value <= addr
1785 && mod->symtab[i].st_value > mod->symtab[best].st_value
1786 && *(mod->strtab + mod->symtab[i].st_name) != '\0' )
1787 best = i;
1788 if (mod->symtab[i].st_value > addr
1789 && mod->symtab[i].st_value < nextval
1790 && *(mod->strtab + mod->symtab[i].st_name) != '\0')
1791 nextval = mod->symtab[i].st_value;
1794 if (!best)
1795 return NULL;
1797 *size = nextval - mod->symtab[best].st_value;
1798 *offset = addr - mod->symtab[best].st_value;
1799 return mod->strtab + mod->symtab[best].st_name;
1802 /* For kallsyms to ask for address resolution. NULL means not found.
1803 We don't lock, as this is used for oops resolution and races are a
1804 lesser concern. */
1805 const char *module_address_lookup(unsigned long addr,
1806 unsigned long *size,
1807 unsigned long *offset,
1808 char **modname)
1810 struct module *mod;
1812 list_for_each_entry(mod, &modules, list) {
1813 if (within(addr, mod->module_init, mod->init_size)
1814 || within(addr, mod->module_core, mod->core_size)) {
1815 *modname = mod->name;
1816 return get_ksymbol(mod, addr, size, offset);
1819 return NULL;
1822 struct module *module_get_kallsym(unsigned int symnum,
1823 unsigned long *value,
1824 char *type,
1825 char namebuf[128])
1827 struct module *mod;
1829 down(&module_mutex);
1830 list_for_each_entry(mod, &modules, list) {
1831 if (symnum < mod->num_symtab) {
1832 *value = mod->symtab[symnum].st_value;
1833 *type = mod->symtab[symnum].st_info;
1834 strncpy(namebuf,
1835 mod->strtab + mod->symtab[symnum].st_name,
1836 127);
1837 up(&module_mutex);
1838 return mod;
1840 symnum -= mod->num_symtab;
1842 up(&module_mutex);
1843 return NULL;
1845 #endif /* CONFIG_KALLSYMS */
1847 /* Called by the /proc file system to return a list of modules. */
1848 static void *m_start(struct seq_file *m, loff_t *pos)
1850 struct list_head *i;
1851 loff_t n = 0;
1853 down(&module_mutex);
1854 list_for_each(i, &modules) {
1855 if (n++ == *pos)
1856 break;
1858 if (i == &modules)
1859 return NULL;
1860 return i;
1863 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
1865 struct list_head *i = p;
1866 (*pos)++;
1867 if (i->next == &modules)
1868 return NULL;
1869 return i->next;
1872 static void m_stop(struct seq_file *m, void *p)
1874 up(&module_mutex);
1877 static int m_show(struct seq_file *m, void *p)
1879 struct module *mod = list_entry(p, struct module, list);
1880 seq_printf(m, "%s %lu",
1881 mod->name, mod->init_size + mod->core_size);
1882 print_unload_info(m, mod);
1884 /* Informative for users. */
1885 seq_printf(m, " %s",
1886 mod->state == MODULE_STATE_GOING ? "Unloading":
1887 mod->state == MODULE_STATE_COMING ? "Loading":
1888 "Live");
1889 /* Used by oprofile and other similar tools. */
1890 seq_printf(m, " 0x%p", mod->module_core);
1892 seq_printf(m, "\n");
1893 return 0;
1896 /* Format: modulename size refcount deps address
1898 Where refcount is a number or -, and deps is a comma-separated list
1899 of depends or -.
1901 struct seq_operations modules_op = {
1902 .start = m_start,
1903 .next = m_next,
1904 .stop = m_stop,
1905 .show = m_show
1908 /* Given an address, look for it in the module exception tables. */
1909 const struct exception_table_entry *search_module_extables(unsigned long addr)
1911 unsigned long flags;
1912 const struct exception_table_entry *e = NULL;
1913 struct module *mod;
1915 spin_lock_irqsave(&modlist_lock, flags);
1916 list_for_each_entry(mod, &modules, list) {
1917 if (mod->num_exentries == 0)
1918 continue;
1920 e = search_extable(mod->extable,
1921 mod->extable + mod->num_exentries - 1,
1922 addr);
1923 if (e)
1924 break;
1926 spin_unlock_irqrestore(&modlist_lock, flags);
1928 /* Now, if we found one, we are running inside it now, hence
1929 we cannot unload the module, hence no refcnt needed. */
1930 return e;
1933 /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
1934 struct module *module_text_address(unsigned long addr)
1936 struct module *mod;
1938 list_for_each_entry(mod, &modules, list)
1939 if (within(addr, mod->module_init, mod->init_text_size)
1940 || within(addr, mod->module_core, mod->core_text_size))
1941 return mod;
1942 return NULL;
1945 #ifdef CONFIG_MODVERSIONS
1946 /* Generate the signature for struct module here, too, for modversions. */
1947 void struct_module(struct module *mod) { return; }
1948 EXPORT_SYMBOL(struct_module);
1949 #endif