[PATCH vfs-2.6 5/6] vfs: remove LOOKUP_PARENT from non LOOKUP_PARENT lookup
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / module.c
blob0d8d21ee792c4e5812694162d241b7975ffd99bf
1 /*
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/module.h>
20 #include <linux/moduleloader.h>
21 #include <linux/init.h>
22 #include <linux/kallsyms.h>
23 #include <linux/sysfs.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/elf.h>
28 #include <linux/seq_file.h>
29 #include <linux/syscalls.h>
30 #include <linux/fcntl.h>
31 #include <linux/rcupdate.h>
32 #include <linux/capability.h>
33 #include <linux/cpu.h>
34 #include <linux/moduleparam.h>
35 #include <linux/errno.h>
36 #include <linux/err.h>
37 #include <linux/vermagic.h>
38 #include <linux/notifier.h>
39 #include <linux/sched.h>
40 #include <linux/stop_machine.h>
41 #include <linux/device.h>
42 #include <linux/string.h>
43 #include <linux/mutex.h>
44 #include <linux/unwind.h>
45 #include <asm/uaccess.h>
46 #include <asm/cacheflush.h>
47 #include <linux/license.h>
48 #include <asm/sections.h>
49 #include <linux/tracepoint.h>
50 #include <linux/ftrace.h>
52 #if 0
53 #define DEBUGP printk
54 #else
55 #define DEBUGP(fmt , a...)
56 #endif
58 #ifndef ARCH_SHF_SMALL
59 #define ARCH_SHF_SMALL 0
60 #endif
62 /* If this is set, the section belongs in the init part of the module */
63 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
65 /* List of modules, protected by module_mutex or preempt_disable
66 * (add/delete uses stop_machine). */
67 static DEFINE_MUTEX(module_mutex);
68 static LIST_HEAD(modules);
70 /* Waiting for a module to finish initializing? */
71 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
73 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
75 /* Bounds of module allocation, for speeding __module_text_address */
76 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
78 int register_module_notifier(struct notifier_block * nb)
80 return blocking_notifier_chain_register(&module_notify_list, nb);
82 EXPORT_SYMBOL(register_module_notifier);
84 int unregister_module_notifier(struct notifier_block * nb)
86 return blocking_notifier_chain_unregister(&module_notify_list, nb);
88 EXPORT_SYMBOL(unregister_module_notifier);
90 /* We require a truly strong try_module_get(): 0 means failure due to
91 ongoing or failed initialization etc. */
92 static inline int strong_try_module_get(struct module *mod)
94 if (mod && mod->state == MODULE_STATE_COMING)
95 return -EBUSY;
96 if (try_module_get(mod))
97 return 0;
98 else
99 return -ENOENT;
102 static inline void add_taint_module(struct module *mod, unsigned flag)
104 add_taint(flag);
105 mod->taints |= (1U << flag);
109 * A thread that wants to hold a reference to a module only while it
110 * is running can call this to safely exit. nfsd and lockd use this.
112 void __module_put_and_exit(struct module *mod, long code)
114 module_put(mod);
115 do_exit(code);
117 EXPORT_SYMBOL(__module_put_and_exit);
119 /* Find a module section: 0 means not found. */
120 static unsigned int find_sec(Elf_Ehdr *hdr,
121 Elf_Shdr *sechdrs,
122 const char *secstrings,
123 const char *name)
125 unsigned int i;
127 for (i = 1; i < hdr->e_shnum; i++)
128 /* Alloc bit cleared means "ignore it." */
129 if ((sechdrs[i].sh_flags & SHF_ALLOC)
130 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
131 return i;
132 return 0;
135 /* Provided by the linker */
136 extern const struct kernel_symbol __start___ksymtab[];
137 extern const struct kernel_symbol __stop___ksymtab[];
138 extern const struct kernel_symbol __start___ksymtab_gpl[];
139 extern const struct kernel_symbol __stop___ksymtab_gpl[];
140 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
141 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
142 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
143 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
144 extern const unsigned long __start___kcrctab[];
145 extern const unsigned long __start___kcrctab_gpl[];
146 extern const unsigned long __start___kcrctab_gpl_future[];
147 #ifdef CONFIG_UNUSED_SYMBOLS
148 extern const struct kernel_symbol __start___ksymtab_unused[];
149 extern const struct kernel_symbol __stop___ksymtab_unused[];
150 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
151 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
152 extern const unsigned long __start___kcrctab_unused[];
153 extern const unsigned long __start___kcrctab_unused_gpl[];
154 #endif
156 #ifndef CONFIG_MODVERSIONS
157 #define symversion(base, idx) NULL
158 #else
159 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
160 #endif
162 struct symsearch {
163 const struct kernel_symbol *start, *stop;
164 const unsigned long *crcs;
165 enum {
166 NOT_GPL_ONLY,
167 GPL_ONLY,
168 WILL_BE_GPL_ONLY,
169 } licence;
170 bool unused;
173 static bool each_symbol_in_section(const struct symsearch *arr,
174 unsigned int arrsize,
175 struct module *owner,
176 bool (*fn)(const struct symsearch *syms,
177 struct module *owner,
178 unsigned int symnum, void *data),
179 void *data)
181 unsigned int i, j;
183 for (j = 0; j < arrsize; j++) {
184 for (i = 0; i < arr[j].stop - arr[j].start; i++)
185 if (fn(&arr[j], owner, i, data))
186 return true;
189 return false;
192 /* Returns true as soon as fn returns true, otherwise false. */
193 static bool each_symbol(bool (*fn)(const struct symsearch *arr,
194 struct module *owner,
195 unsigned int symnum, void *data),
196 void *data)
198 struct module *mod;
199 const struct symsearch arr[] = {
200 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
201 NOT_GPL_ONLY, false },
202 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
203 __start___kcrctab_gpl,
204 GPL_ONLY, false },
205 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
206 __start___kcrctab_gpl_future,
207 WILL_BE_GPL_ONLY, false },
208 #ifdef CONFIG_UNUSED_SYMBOLS
209 { __start___ksymtab_unused, __stop___ksymtab_unused,
210 __start___kcrctab_unused,
211 NOT_GPL_ONLY, true },
212 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
213 __start___kcrctab_unused_gpl,
214 GPL_ONLY, true },
215 #endif
218 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
219 return true;
221 list_for_each_entry(mod, &modules, list) {
222 struct symsearch arr[] = {
223 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
224 NOT_GPL_ONLY, false },
225 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
226 mod->gpl_crcs,
227 GPL_ONLY, false },
228 { mod->gpl_future_syms,
229 mod->gpl_future_syms + mod->num_gpl_future_syms,
230 mod->gpl_future_crcs,
231 WILL_BE_GPL_ONLY, false },
232 #ifdef CONFIG_UNUSED_SYMBOLS
233 { mod->unused_syms,
234 mod->unused_syms + mod->num_unused_syms,
235 mod->unused_crcs,
236 NOT_GPL_ONLY, true },
237 { mod->unused_gpl_syms,
238 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
239 mod->unused_gpl_crcs,
240 GPL_ONLY, true },
241 #endif
244 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
245 return true;
247 return false;
250 struct find_symbol_arg {
251 /* Input */
252 const char *name;
253 bool gplok;
254 bool warn;
256 /* Output */
257 struct module *owner;
258 const unsigned long *crc;
259 unsigned long value;
262 static bool find_symbol_in_section(const struct symsearch *syms,
263 struct module *owner,
264 unsigned int symnum, void *data)
266 struct find_symbol_arg *fsa = data;
268 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
269 return false;
271 if (!fsa->gplok) {
272 if (syms->licence == GPL_ONLY)
273 return false;
274 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
275 printk(KERN_WARNING "Symbol %s is being used "
276 "by a non-GPL module, which will not "
277 "be allowed in the future\n", fsa->name);
278 printk(KERN_WARNING "Please see the file "
279 "Documentation/feature-removal-schedule.txt "
280 "in the kernel source tree for more details.\n");
284 #ifdef CONFIG_UNUSED_SYMBOLS
285 if (syms->unused && fsa->warn) {
286 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
287 "however this module is using it.\n", fsa->name);
288 printk(KERN_WARNING
289 "This symbol will go away in the future.\n");
290 printk(KERN_WARNING
291 "Please evalute if this is the right api to use and if "
292 "it really is, submit a report the linux kernel "
293 "mailinglist together with submitting your code for "
294 "inclusion.\n");
296 #endif
298 fsa->owner = owner;
299 fsa->crc = symversion(syms->crcs, symnum);
300 fsa->value = syms->start[symnum].value;
301 return true;
304 /* Find a symbol, return value, (optional) crc and (optional) module
305 * which owns it */
306 static unsigned long find_symbol(const char *name,
307 struct module **owner,
308 const unsigned long **crc,
309 bool gplok,
310 bool warn)
312 struct find_symbol_arg fsa;
314 fsa.name = name;
315 fsa.gplok = gplok;
316 fsa.warn = warn;
318 if (each_symbol(find_symbol_in_section, &fsa)) {
319 if (owner)
320 *owner = fsa.owner;
321 if (crc)
322 *crc = fsa.crc;
323 return fsa.value;
326 DEBUGP("Failed to find symbol %s\n", name);
327 return -ENOENT;
330 /* Search for module by name: must hold module_mutex. */
331 static struct module *find_module(const char *name)
333 struct module *mod;
335 list_for_each_entry(mod, &modules, list) {
336 if (strcmp(mod->name, name) == 0)
337 return mod;
339 return NULL;
342 #ifdef CONFIG_SMP
343 /* Number of blocks used and allocated. */
344 static unsigned int pcpu_num_used, pcpu_num_allocated;
345 /* Size of each block. -ve means used. */
346 static int *pcpu_size;
348 static int split_block(unsigned int i, unsigned short size)
350 /* Reallocation required? */
351 if (pcpu_num_used + 1 > pcpu_num_allocated) {
352 int *new;
354 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
355 GFP_KERNEL);
356 if (!new)
357 return 0;
359 pcpu_num_allocated *= 2;
360 pcpu_size = new;
363 /* Insert a new subblock */
364 memmove(&pcpu_size[i+1], &pcpu_size[i],
365 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
366 pcpu_num_used++;
368 pcpu_size[i+1] -= size;
369 pcpu_size[i] = size;
370 return 1;
373 static inline unsigned int block_size(int val)
375 if (val < 0)
376 return -val;
377 return val;
380 static void *percpu_modalloc(unsigned long size, unsigned long align,
381 const char *name)
383 unsigned long extra;
384 unsigned int i;
385 void *ptr;
387 if (align > PAGE_SIZE) {
388 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
389 name, align, PAGE_SIZE);
390 align = PAGE_SIZE;
393 ptr = __per_cpu_start;
394 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
395 /* Extra for alignment requirement. */
396 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
397 BUG_ON(i == 0 && extra != 0);
399 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
400 continue;
402 /* Transfer extra to previous block. */
403 if (pcpu_size[i-1] < 0)
404 pcpu_size[i-1] -= extra;
405 else
406 pcpu_size[i-1] += extra;
407 pcpu_size[i] -= extra;
408 ptr += extra;
410 /* Split block if warranted */
411 if (pcpu_size[i] - size > sizeof(unsigned long))
412 if (!split_block(i, size))
413 return NULL;
415 /* Mark allocated */
416 pcpu_size[i] = -pcpu_size[i];
417 return ptr;
420 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
421 size);
422 return NULL;
425 static void percpu_modfree(void *freeme)
427 unsigned int i;
428 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
430 /* First entry is core kernel percpu data. */
431 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
432 if (ptr == freeme) {
433 pcpu_size[i] = -pcpu_size[i];
434 goto free;
437 BUG();
439 free:
440 /* Merge with previous? */
441 if (pcpu_size[i-1] >= 0) {
442 pcpu_size[i-1] += pcpu_size[i];
443 pcpu_num_used--;
444 memmove(&pcpu_size[i], &pcpu_size[i+1],
445 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
446 i--;
448 /* Merge with next? */
449 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
450 pcpu_size[i] += pcpu_size[i+1];
451 pcpu_num_used--;
452 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
453 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
457 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
458 Elf_Shdr *sechdrs,
459 const char *secstrings)
461 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
464 static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
466 int cpu;
468 for_each_possible_cpu(cpu)
469 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
472 static int percpu_modinit(void)
474 pcpu_num_used = 2;
475 pcpu_num_allocated = 2;
476 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
477 GFP_KERNEL);
478 /* Static in-kernel percpu data (used). */
479 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
480 /* Free room. */
481 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
482 if (pcpu_size[1] < 0) {
483 printk(KERN_ERR "No per-cpu room for modules.\n");
484 pcpu_num_used = 1;
487 return 0;
489 __initcall(percpu_modinit);
490 #else /* ... !CONFIG_SMP */
491 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
492 const char *name)
494 return NULL;
496 static inline void percpu_modfree(void *pcpuptr)
498 BUG();
500 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
501 Elf_Shdr *sechdrs,
502 const char *secstrings)
504 return 0;
506 static inline void percpu_modcopy(void *pcpudst, const void *src,
507 unsigned long size)
509 /* pcpusec should be 0, and size of that section should be 0. */
510 BUG_ON(size != 0);
512 #endif /* CONFIG_SMP */
514 #define MODINFO_ATTR(field) \
515 static void setup_modinfo_##field(struct module *mod, const char *s) \
517 mod->field = kstrdup(s, GFP_KERNEL); \
519 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
520 struct module *mod, char *buffer) \
522 return sprintf(buffer, "%s\n", mod->field); \
524 static int modinfo_##field##_exists(struct module *mod) \
526 return mod->field != NULL; \
528 static void free_modinfo_##field(struct module *mod) \
530 kfree(mod->field); \
531 mod->field = NULL; \
533 static struct module_attribute modinfo_##field = { \
534 .attr = { .name = __stringify(field), .mode = 0444 }, \
535 .show = show_modinfo_##field, \
536 .setup = setup_modinfo_##field, \
537 .test = modinfo_##field##_exists, \
538 .free = free_modinfo_##field, \
541 MODINFO_ATTR(version);
542 MODINFO_ATTR(srcversion);
544 static char last_unloaded_module[MODULE_NAME_LEN+1];
546 #ifdef CONFIG_MODULE_UNLOAD
547 /* Init the unload section of the module. */
548 static void module_unload_init(struct module *mod)
550 unsigned int i;
552 INIT_LIST_HEAD(&mod->modules_which_use_me);
553 for (i = 0; i < NR_CPUS; i++)
554 local_set(&mod->ref[i].count, 0);
555 /* Hold reference count during initialization. */
556 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
557 /* Backwards compatibility macros put refcount during init. */
558 mod->waiter = current;
561 /* modules using other modules */
562 struct module_use
564 struct list_head list;
565 struct module *module_which_uses;
568 /* Does a already use b? */
569 static int already_uses(struct module *a, struct module *b)
571 struct module_use *use;
573 list_for_each_entry(use, &b->modules_which_use_me, list) {
574 if (use->module_which_uses == a) {
575 DEBUGP("%s uses %s!\n", a->name, b->name);
576 return 1;
579 DEBUGP("%s does not use %s!\n", a->name, b->name);
580 return 0;
583 /* Module a uses b */
584 static int use_module(struct module *a, struct module *b)
586 struct module_use *use;
587 int no_warn, err;
589 if (b == NULL || already_uses(a, b)) return 1;
591 /* If we're interrupted or time out, we fail. */
592 if (wait_event_interruptible_timeout(
593 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
594 30 * HZ) <= 0) {
595 printk("%s: gave up waiting for init of module %s.\n",
596 a->name, b->name);
597 return 0;
600 /* If strong_try_module_get() returned a different error, we fail. */
601 if (err)
602 return 0;
604 DEBUGP("Allocating new usage for %s.\n", a->name);
605 use = kmalloc(sizeof(*use), GFP_ATOMIC);
606 if (!use) {
607 printk("%s: out of memory loading\n", a->name);
608 module_put(b);
609 return 0;
612 use->module_which_uses = a;
613 list_add(&use->list, &b->modules_which_use_me);
614 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
615 return 1;
618 /* Clear the unload stuff of the module. */
619 static void module_unload_free(struct module *mod)
621 struct module *i;
623 list_for_each_entry(i, &modules, list) {
624 struct module_use *use;
626 list_for_each_entry(use, &i->modules_which_use_me, list) {
627 if (use->module_which_uses == mod) {
628 DEBUGP("%s unusing %s\n", mod->name, i->name);
629 module_put(i);
630 list_del(&use->list);
631 kfree(use);
632 sysfs_remove_link(i->holders_dir, mod->name);
633 /* There can be at most one match. */
634 break;
640 #ifdef CONFIG_MODULE_FORCE_UNLOAD
641 static inline int try_force_unload(unsigned int flags)
643 int ret = (flags & O_TRUNC);
644 if (ret)
645 add_taint(TAINT_FORCED_RMMOD);
646 return ret;
648 #else
649 static inline int try_force_unload(unsigned int flags)
651 return 0;
653 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
655 struct stopref
657 struct module *mod;
658 int flags;
659 int *forced;
662 /* Whole machine is stopped with interrupts off when this runs. */
663 static int __try_stop_module(void *_sref)
665 struct stopref *sref = _sref;
667 /* If it's not unused, quit unless we're forcing. */
668 if (module_refcount(sref->mod) != 0) {
669 if (!(*sref->forced = try_force_unload(sref->flags)))
670 return -EWOULDBLOCK;
673 /* Mark it as dying. */
674 sref->mod->state = MODULE_STATE_GOING;
675 return 0;
678 static int try_stop_module(struct module *mod, int flags, int *forced)
680 if (flags & O_NONBLOCK) {
681 struct stopref sref = { mod, flags, forced };
683 return stop_machine(__try_stop_module, &sref, NULL);
684 } else {
685 /* We don't need to stop the machine for this. */
686 mod->state = MODULE_STATE_GOING;
687 synchronize_sched();
688 return 0;
692 unsigned int module_refcount(struct module *mod)
694 unsigned int i, total = 0;
696 for (i = 0; i < NR_CPUS; i++)
697 total += local_read(&mod->ref[i].count);
698 return total;
700 EXPORT_SYMBOL(module_refcount);
702 /* This exists whether we can unload or not */
703 static void free_module(struct module *mod);
705 static void wait_for_zero_refcount(struct module *mod)
707 /* Since we might sleep for some time, release the mutex first */
708 mutex_unlock(&module_mutex);
709 for (;;) {
710 DEBUGP("Looking at refcount...\n");
711 set_current_state(TASK_UNINTERRUPTIBLE);
712 if (module_refcount(mod) == 0)
713 break;
714 schedule();
716 current->state = TASK_RUNNING;
717 mutex_lock(&module_mutex);
720 asmlinkage long
721 sys_delete_module(const char __user *name_user, unsigned int flags)
723 struct module *mod;
724 char name[MODULE_NAME_LEN];
725 int ret, forced = 0;
727 if (!capable(CAP_SYS_MODULE))
728 return -EPERM;
730 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
731 return -EFAULT;
732 name[MODULE_NAME_LEN-1] = '\0';
734 if (mutex_lock_interruptible(&module_mutex) != 0)
735 return -EINTR;
737 mod = find_module(name);
738 if (!mod) {
739 ret = -ENOENT;
740 goto out;
743 if (!list_empty(&mod->modules_which_use_me)) {
744 /* Other modules depend on us: get rid of them first. */
745 ret = -EWOULDBLOCK;
746 goto out;
749 /* Doing init or already dying? */
750 if (mod->state != MODULE_STATE_LIVE) {
751 /* FIXME: if (force), slam module count and wake up
752 waiter --RR */
753 DEBUGP("%s already dying\n", mod->name);
754 ret = -EBUSY;
755 goto out;
758 /* If it has an init func, it must have an exit func to unload */
759 if (mod->init && !mod->exit) {
760 forced = try_force_unload(flags);
761 if (!forced) {
762 /* This module can't be removed */
763 ret = -EBUSY;
764 goto out;
768 /* Set this up before setting mod->state */
769 mod->waiter = current;
771 /* Stop the machine so refcounts can't move and disable module. */
772 ret = try_stop_module(mod, flags, &forced);
773 if (ret != 0)
774 goto out;
776 /* Never wait if forced. */
777 if (!forced && module_refcount(mod) != 0)
778 wait_for_zero_refcount(mod);
780 mutex_unlock(&module_mutex);
781 /* Final destruction now noone is using it. */
782 if (mod->exit != NULL)
783 mod->exit();
784 blocking_notifier_call_chain(&module_notify_list,
785 MODULE_STATE_GOING, mod);
786 mutex_lock(&module_mutex);
787 /* Store the name of the last unloaded module for diagnostic purposes */
788 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
789 unregister_dynamic_debug_module(mod->name);
790 free_module(mod);
792 out:
793 mutex_unlock(&module_mutex);
794 return ret;
797 static void print_unload_info(struct seq_file *m, struct module *mod)
799 struct module_use *use;
800 int printed_something = 0;
802 seq_printf(m, " %u ", module_refcount(mod));
804 /* Always include a trailing , so userspace can differentiate
805 between this and the old multi-field proc format. */
806 list_for_each_entry(use, &mod->modules_which_use_me, list) {
807 printed_something = 1;
808 seq_printf(m, "%s,", use->module_which_uses->name);
811 if (mod->init != NULL && mod->exit == NULL) {
812 printed_something = 1;
813 seq_printf(m, "[permanent],");
816 if (!printed_something)
817 seq_printf(m, "-");
820 void __symbol_put(const char *symbol)
822 struct module *owner;
824 preempt_disable();
825 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
826 BUG();
827 module_put(owner);
828 preempt_enable();
830 EXPORT_SYMBOL(__symbol_put);
832 void symbol_put_addr(void *addr)
834 struct module *modaddr;
836 if (core_kernel_text((unsigned long)addr))
837 return;
839 if (!(modaddr = module_text_address((unsigned long)addr)))
840 BUG();
841 module_put(modaddr);
843 EXPORT_SYMBOL_GPL(symbol_put_addr);
845 static ssize_t show_refcnt(struct module_attribute *mattr,
846 struct module *mod, char *buffer)
848 return sprintf(buffer, "%u\n", module_refcount(mod));
851 static struct module_attribute refcnt = {
852 .attr = { .name = "refcnt", .mode = 0444 },
853 .show = show_refcnt,
856 void module_put(struct module *module)
858 if (module) {
859 unsigned int cpu = get_cpu();
860 local_dec(&module->ref[cpu].count);
861 /* Maybe they're waiting for us to drop reference? */
862 if (unlikely(!module_is_live(module)))
863 wake_up_process(module->waiter);
864 put_cpu();
867 EXPORT_SYMBOL(module_put);
869 #else /* !CONFIG_MODULE_UNLOAD */
870 static void print_unload_info(struct seq_file *m, struct module *mod)
872 /* We don't know the usage count, or what modules are using. */
873 seq_printf(m, " - -");
876 static inline void module_unload_free(struct module *mod)
880 static inline int use_module(struct module *a, struct module *b)
882 return strong_try_module_get(b) == 0;
885 static inline void module_unload_init(struct module *mod)
888 #endif /* CONFIG_MODULE_UNLOAD */
890 static ssize_t show_initstate(struct module_attribute *mattr,
891 struct module *mod, char *buffer)
893 const char *state = "unknown";
895 switch (mod->state) {
896 case MODULE_STATE_LIVE:
897 state = "live";
898 break;
899 case MODULE_STATE_COMING:
900 state = "coming";
901 break;
902 case MODULE_STATE_GOING:
903 state = "going";
904 break;
906 return sprintf(buffer, "%s\n", state);
909 static struct module_attribute initstate = {
910 .attr = { .name = "initstate", .mode = 0444 },
911 .show = show_initstate,
914 static struct module_attribute *modinfo_attrs[] = {
915 &modinfo_version,
916 &modinfo_srcversion,
917 &initstate,
918 #ifdef CONFIG_MODULE_UNLOAD
919 &refcnt,
920 #endif
921 NULL,
924 static const char vermagic[] = VERMAGIC_STRING;
926 static int try_to_force_load(struct module *mod, const char *symname)
928 #ifdef CONFIG_MODULE_FORCE_LOAD
929 if (!test_taint(TAINT_FORCED_MODULE))
930 printk("%s: no version for \"%s\" found: kernel tainted.\n",
931 mod->name, symname);
932 add_taint_module(mod, TAINT_FORCED_MODULE);
933 return 0;
934 #else
935 return -ENOEXEC;
936 #endif
939 #ifdef CONFIG_MODVERSIONS
940 static int check_version(Elf_Shdr *sechdrs,
941 unsigned int versindex,
942 const char *symname,
943 struct module *mod,
944 const unsigned long *crc)
946 unsigned int i, num_versions;
947 struct modversion_info *versions;
949 /* Exporting module didn't supply crcs? OK, we're already tainted. */
950 if (!crc)
951 return 1;
953 /* No versions at all? modprobe --force does this. */
954 if (versindex == 0)
955 return try_to_force_load(mod, symname) == 0;
957 versions = (void *) sechdrs[versindex].sh_addr;
958 num_versions = sechdrs[versindex].sh_size
959 / sizeof(struct modversion_info);
961 for (i = 0; i < num_versions; i++) {
962 if (strcmp(versions[i].name, symname) != 0)
963 continue;
965 if (versions[i].crc == *crc)
966 return 1;
967 DEBUGP("Found checksum %lX vs module %lX\n",
968 *crc, versions[i].crc);
969 goto bad_version;
972 printk(KERN_WARNING "%s: no symbol version for %s\n",
973 mod->name, symname);
974 return 0;
976 bad_version:
977 printk("%s: disagrees about version of symbol %s\n",
978 mod->name, symname);
979 return 0;
982 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
983 unsigned int versindex,
984 struct module *mod)
986 const unsigned long *crc;
988 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
989 BUG();
990 return check_version(sechdrs, versindex, "struct_module", mod, crc);
993 /* First part is kernel version, which we ignore if module has crcs. */
994 static inline int same_magic(const char *amagic, const char *bmagic,
995 bool has_crcs)
997 if (has_crcs) {
998 amagic += strcspn(amagic, " ");
999 bmagic += strcspn(bmagic, " ");
1001 return strcmp(amagic, bmagic) == 0;
1003 #else
1004 static inline int check_version(Elf_Shdr *sechdrs,
1005 unsigned int versindex,
1006 const char *symname,
1007 struct module *mod,
1008 const unsigned long *crc)
1010 return 1;
1013 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1014 unsigned int versindex,
1015 struct module *mod)
1017 return 1;
1020 static inline int same_magic(const char *amagic, const char *bmagic,
1021 bool has_crcs)
1023 return strcmp(amagic, bmagic) == 0;
1025 #endif /* CONFIG_MODVERSIONS */
1027 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1028 Must be holding module_mutex. */
1029 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1030 unsigned int versindex,
1031 const char *name,
1032 struct module *mod)
1034 struct module *owner;
1035 unsigned long ret;
1036 const unsigned long *crc;
1038 ret = find_symbol(name, &owner, &crc,
1039 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1040 if (!IS_ERR_VALUE(ret)) {
1041 /* use_module can fail due to OOM,
1042 or module initialization or unloading */
1043 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1044 !use_module(mod, owner))
1045 ret = -EINVAL;
1047 return ret;
1051 * /sys/module/foo/sections stuff
1052 * J. Corbet <corbet@lwn.net>
1054 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1055 struct module_sect_attr
1057 struct module_attribute mattr;
1058 char *name;
1059 unsigned long address;
1062 struct module_sect_attrs
1064 struct attribute_group grp;
1065 unsigned int nsections;
1066 struct module_sect_attr attrs[0];
1069 static ssize_t module_sect_show(struct module_attribute *mattr,
1070 struct module *mod, char *buf)
1072 struct module_sect_attr *sattr =
1073 container_of(mattr, struct module_sect_attr, mattr);
1074 return sprintf(buf, "0x%lx\n", sattr->address);
1077 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1079 unsigned int section;
1081 for (section = 0; section < sect_attrs->nsections; section++)
1082 kfree(sect_attrs->attrs[section].name);
1083 kfree(sect_attrs);
1086 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1087 char *secstrings, Elf_Shdr *sechdrs)
1089 unsigned int nloaded = 0, i, size[2];
1090 struct module_sect_attrs *sect_attrs;
1091 struct module_sect_attr *sattr;
1092 struct attribute **gattr;
1094 /* Count loaded sections and allocate structures */
1095 for (i = 0; i < nsect; i++)
1096 if (sechdrs[i].sh_flags & SHF_ALLOC)
1097 nloaded++;
1098 size[0] = ALIGN(sizeof(*sect_attrs)
1099 + nloaded * sizeof(sect_attrs->attrs[0]),
1100 sizeof(sect_attrs->grp.attrs[0]));
1101 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1102 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1103 if (sect_attrs == NULL)
1104 return;
1106 /* Setup section attributes. */
1107 sect_attrs->grp.name = "sections";
1108 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1110 sect_attrs->nsections = 0;
1111 sattr = &sect_attrs->attrs[0];
1112 gattr = &sect_attrs->grp.attrs[0];
1113 for (i = 0; i < nsect; i++) {
1114 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1115 continue;
1116 sattr->address = sechdrs[i].sh_addr;
1117 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1118 GFP_KERNEL);
1119 if (sattr->name == NULL)
1120 goto out;
1121 sect_attrs->nsections++;
1122 sattr->mattr.show = module_sect_show;
1123 sattr->mattr.store = NULL;
1124 sattr->mattr.attr.name = sattr->name;
1125 sattr->mattr.attr.mode = S_IRUGO;
1126 *(gattr++) = &(sattr++)->mattr.attr;
1128 *gattr = NULL;
1130 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1131 goto out;
1133 mod->sect_attrs = sect_attrs;
1134 return;
1135 out:
1136 free_sect_attrs(sect_attrs);
1139 static void remove_sect_attrs(struct module *mod)
1141 if (mod->sect_attrs) {
1142 sysfs_remove_group(&mod->mkobj.kobj,
1143 &mod->sect_attrs->grp);
1144 /* We are positive that no one is using any sect attrs
1145 * at this point. Deallocate immediately. */
1146 free_sect_attrs(mod->sect_attrs);
1147 mod->sect_attrs = NULL;
1152 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1155 struct module_notes_attrs {
1156 struct kobject *dir;
1157 unsigned int notes;
1158 struct bin_attribute attrs[0];
1161 static ssize_t module_notes_read(struct kobject *kobj,
1162 struct bin_attribute *bin_attr,
1163 char *buf, loff_t pos, size_t count)
1166 * The caller checked the pos and count against our size.
1168 memcpy(buf, bin_attr->private + pos, count);
1169 return count;
1172 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1173 unsigned int i)
1175 if (notes_attrs->dir) {
1176 while (i-- > 0)
1177 sysfs_remove_bin_file(notes_attrs->dir,
1178 &notes_attrs->attrs[i]);
1179 kobject_put(notes_attrs->dir);
1181 kfree(notes_attrs);
1184 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1185 char *secstrings, Elf_Shdr *sechdrs)
1187 unsigned int notes, loaded, i;
1188 struct module_notes_attrs *notes_attrs;
1189 struct bin_attribute *nattr;
1191 /* Count notes sections and allocate structures. */
1192 notes = 0;
1193 for (i = 0; i < nsect; i++)
1194 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1195 (sechdrs[i].sh_type == SHT_NOTE))
1196 ++notes;
1198 if (notes == 0)
1199 return;
1201 notes_attrs = kzalloc(sizeof(*notes_attrs)
1202 + notes * sizeof(notes_attrs->attrs[0]),
1203 GFP_KERNEL);
1204 if (notes_attrs == NULL)
1205 return;
1207 notes_attrs->notes = notes;
1208 nattr = &notes_attrs->attrs[0];
1209 for (loaded = i = 0; i < nsect; ++i) {
1210 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1211 continue;
1212 if (sechdrs[i].sh_type == SHT_NOTE) {
1213 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1214 nattr->attr.mode = S_IRUGO;
1215 nattr->size = sechdrs[i].sh_size;
1216 nattr->private = (void *) sechdrs[i].sh_addr;
1217 nattr->read = module_notes_read;
1218 ++nattr;
1220 ++loaded;
1223 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1224 if (!notes_attrs->dir)
1225 goto out;
1227 for (i = 0; i < notes; ++i)
1228 if (sysfs_create_bin_file(notes_attrs->dir,
1229 &notes_attrs->attrs[i]))
1230 goto out;
1232 mod->notes_attrs = notes_attrs;
1233 return;
1235 out:
1236 free_notes_attrs(notes_attrs, i);
1239 static void remove_notes_attrs(struct module *mod)
1241 if (mod->notes_attrs)
1242 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1245 #else
1247 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1248 char *sectstrings, Elf_Shdr *sechdrs)
1252 static inline void remove_sect_attrs(struct module *mod)
1256 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1257 char *sectstrings, Elf_Shdr *sechdrs)
1261 static inline void remove_notes_attrs(struct module *mod)
1264 #endif
1266 #ifdef CONFIG_SYSFS
1267 int module_add_modinfo_attrs(struct module *mod)
1269 struct module_attribute *attr;
1270 struct module_attribute *temp_attr;
1271 int error = 0;
1272 int i;
1274 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1275 (ARRAY_SIZE(modinfo_attrs) + 1)),
1276 GFP_KERNEL);
1277 if (!mod->modinfo_attrs)
1278 return -ENOMEM;
1280 temp_attr = mod->modinfo_attrs;
1281 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1282 if (!attr->test ||
1283 (attr->test && attr->test(mod))) {
1284 memcpy(temp_attr, attr, sizeof(*temp_attr));
1285 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1286 ++temp_attr;
1289 return error;
1292 void module_remove_modinfo_attrs(struct module *mod)
1294 struct module_attribute *attr;
1295 int i;
1297 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1298 /* pick a field to test for end of list */
1299 if (!attr->attr.name)
1300 break;
1301 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1302 if (attr->free)
1303 attr->free(mod);
1305 kfree(mod->modinfo_attrs);
1308 int mod_sysfs_init(struct module *mod)
1310 int err;
1311 struct kobject *kobj;
1313 if (!module_sysfs_initialized) {
1314 printk(KERN_ERR "%s: module sysfs not initialized\n",
1315 mod->name);
1316 err = -EINVAL;
1317 goto out;
1320 kobj = kset_find_obj(module_kset, mod->name);
1321 if (kobj) {
1322 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1323 kobject_put(kobj);
1324 err = -EINVAL;
1325 goto out;
1328 mod->mkobj.mod = mod;
1330 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1331 mod->mkobj.kobj.kset = module_kset;
1332 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1333 "%s", mod->name);
1334 if (err)
1335 kobject_put(&mod->mkobj.kobj);
1337 /* delay uevent until full sysfs population */
1338 out:
1339 return err;
1342 int mod_sysfs_setup(struct module *mod,
1343 struct kernel_param *kparam,
1344 unsigned int num_params)
1346 int err;
1348 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1349 if (!mod->holders_dir) {
1350 err = -ENOMEM;
1351 goto out_unreg;
1354 err = module_param_sysfs_setup(mod, kparam, num_params);
1355 if (err)
1356 goto out_unreg_holders;
1358 err = module_add_modinfo_attrs(mod);
1359 if (err)
1360 goto out_unreg_param;
1362 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1363 return 0;
1365 out_unreg_param:
1366 module_param_sysfs_remove(mod);
1367 out_unreg_holders:
1368 kobject_put(mod->holders_dir);
1369 out_unreg:
1370 kobject_put(&mod->mkobj.kobj);
1371 return err;
1374 static void mod_sysfs_fini(struct module *mod)
1376 kobject_put(&mod->mkobj.kobj);
1379 #else /* CONFIG_SYSFS */
1381 static void mod_sysfs_fini(struct module *mod)
1385 #endif /* CONFIG_SYSFS */
1387 static void mod_kobject_remove(struct module *mod)
1389 module_remove_modinfo_attrs(mod);
1390 module_param_sysfs_remove(mod);
1391 kobject_put(mod->mkobj.drivers_dir);
1392 kobject_put(mod->holders_dir);
1393 mod_sysfs_fini(mod);
1397 * link the module with the whole machine is stopped with interrupts off
1398 * - this defends against kallsyms not taking locks
1400 static int __link_module(void *_mod)
1402 struct module *mod = _mod;
1403 list_add(&mod->list, &modules);
1404 return 0;
1408 * unlink the module with the whole machine is stopped with interrupts off
1409 * - this defends against kallsyms not taking locks
1411 static int __unlink_module(void *_mod)
1413 struct module *mod = _mod;
1414 list_del(&mod->list);
1415 return 0;
1418 /* Free a module, remove from lists, etc (must hold module_mutex). */
1419 static void free_module(struct module *mod)
1421 /* Delete from various lists */
1422 stop_machine(__unlink_module, mod, NULL);
1423 remove_notes_attrs(mod);
1424 remove_sect_attrs(mod);
1425 mod_kobject_remove(mod);
1427 unwind_remove_table(mod->unwind_info, 0);
1429 /* Arch-specific cleanup. */
1430 module_arch_cleanup(mod);
1432 /* Module unload stuff */
1433 module_unload_free(mod);
1435 /* release any pointers to mcount in this module */
1436 ftrace_release(mod->module_core, mod->core_size);
1438 /* This may be NULL, but that's OK */
1439 module_free(mod, mod->module_init);
1440 kfree(mod->args);
1441 if (mod->percpu)
1442 percpu_modfree(mod->percpu);
1444 /* Free lock-classes: */
1445 lockdep_free_key_range(mod->module_core, mod->core_size);
1447 /* Finally, free the core (containing the module structure) */
1448 module_free(mod, mod->module_core);
1451 void *__symbol_get(const char *symbol)
1453 struct module *owner;
1454 unsigned long value;
1456 preempt_disable();
1457 value = find_symbol(symbol, &owner, NULL, true, true);
1458 if (IS_ERR_VALUE(value))
1459 value = 0;
1460 else if (strong_try_module_get(owner))
1461 value = 0;
1462 preempt_enable();
1464 return (void *)value;
1466 EXPORT_SYMBOL_GPL(__symbol_get);
1469 * Ensure that an exported symbol [global namespace] does not already exist
1470 * in the kernel or in some other module's exported symbol table.
1472 static int verify_export_symbols(struct module *mod)
1474 unsigned int i;
1475 struct module *owner;
1476 const struct kernel_symbol *s;
1477 struct {
1478 const struct kernel_symbol *sym;
1479 unsigned int num;
1480 } arr[] = {
1481 { mod->syms, mod->num_syms },
1482 { mod->gpl_syms, mod->num_gpl_syms },
1483 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1484 #ifdef CONFIG_UNUSED_SYMBOLS
1485 { mod->unused_syms, mod->num_unused_syms },
1486 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1487 #endif
1490 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1491 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1492 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1493 NULL, true, false))) {
1494 printk(KERN_ERR
1495 "%s: exports duplicate symbol %s"
1496 " (owned by %s)\n",
1497 mod->name, s->name, module_name(owner));
1498 return -ENOEXEC;
1502 return 0;
1505 /* Change all symbols so that st_value encodes the pointer directly. */
1506 static int simplify_symbols(Elf_Shdr *sechdrs,
1507 unsigned int symindex,
1508 const char *strtab,
1509 unsigned int versindex,
1510 unsigned int pcpuindex,
1511 struct module *mod)
1513 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1514 unsigned long secbase;
1515 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1516 int ret = 0;
1518 for (i = 1; i < n; i++) {
1519 switch (sym[i].st_shndx) {
1520 case SHN_COMMON:
1521 /* We compiled with -fno-common. These are not
1522 supposed to happen. */
1523 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1524 printk("%s: please compile with -fno-common\n",
1525 mod->name);
1526 ret = -ENOEXEC;
1527 break;
1529 case SHN_ABS:
1530 /* Don't need to do anything */
1531 DEBUGP("Absolute symbol: 0x%08lx\n",
1532 (long)sym[i].st_value);
1533 break;
1535 case SHN_UNDEF:
1536 sym[i].st_value
1537 = resolve_symbol(sechdrs, versindex,
1538 strtab + sym[i].st_name, mod);
1540 /* Ok if resolved. */
1541 if (!IS_ERR_VALUE(sym[i].st_value))
1542 break;
1543 /* Ok if weak. */
1544 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1545 break;
1547 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1548 mod->name, strtab + sym[i].st_name);
1549 ret = -ENOENT;
1550 break;
1552 default:
1553 /* Divert to percpu allocation if a percpu var. */
1554 if (sym[i].st_shndx == pcpuindex)
1555 secbase = (unsigned long)mod->percpu;
1556 else
1557 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1558 sym[i].st_value += secbase;
1559 break;
1563 return ret;
1566 /* Update size with this section: return offset. */
1567 static long get_offset(unsigned int *size, Elf_Shdr *sechdr)
1569 long ret;
1571 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1572 *size = ret + sechdr->sh_size;
1573 return ret;
1576 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1577 might -- code, read-only data, read-write data, small data. Tally
1578 sizes, and place the offsets into sh_entsize fields: high bit means it
1579 belongs in init. */
1580 static void layout_sections(struct module *mod,
1581 const Elf_Ehdr *hdr,
1582 Elf_Shdr *sechdrs,
1583 const char *secstrings)
1585 static unsigned long const masks[][2] = {
1586 /* NOTE: all executable code must be the first section
1587 * in this array; otherwise modify the text_size
1588 * finder in the two loops below */
1589 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1590 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1591 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1592 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1594 unsigned int m, i;
1596 for (i = 0; i < hdr->e_shnum; i++)
1597 sechdrs[i].sh_entsize = ~0UL;
1599 DEBUGP("Core section allocation order:\n");
1600 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1601 for (i = 0; i < hdr->e_shnum; ++i) {
1602 Elf_Shdr *s = &sechdrs[i];
1604 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1605 || (s->sh_flags & masks[m][1])
1606 || s->sh_entsize != ~0UL
1607 || strncmp(secstrings + s->sh_name,
1608 ".init", 5) == 0)
1609 continue;
1610 s->sh_entsize = get_offset(&mod->core_size, s);
1611 DEBUGP("\t%s\n", secstrings + s->sh_name);
1613 if (m == 0)
1614 mod->core_text_size = mod->core_size;
1617 DEBUGP("Init section allocation order:\n");
1618 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1619 for (i = 0; i < hdr->e_shnum; ++i) {
1620 Elf_Shdr *s = &sechdrs[i];
1622 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1623 || (s->sh_flags & masks[m][1])
1624 || s->sh_entsize != ~0UL
1625 || strncmp(secstrings + s->sh_name,
1626 ".init", 5) != 0)
1627 continue;
1628 s->sh_entsize = (get_offset(&mod->init_size, s)
1629 | INIT_OFFSET_MASK);
1630 DEBUGP("\t%s\n", secstrings + s->sh_name);
1632 if (m == 0)
1633 mod->init_text_size = mod->init_size;
1637 static void set_license(struct module *mod, const char *license)
1639 if (!license)
1640 license = "unspecified";
1642 if (!license_is_gpl_compatible(license)) {
1643 if (!test_taint(TAINT_PROPRIETARY_MODULE))
1644 printk(KERN_WARNING "%s: module license '%s' taints "
1645 "kernel.\n", mod->name, license);
1646 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1650 /* Parse tag=value strings from .modinfo section */
1651 static char *next_string(char *string, unsigned long *secsize)
1653 /* Skip non-zero chars */
1654 while (string[0]) {
1655 string++;
1656 if ((*secsize)-- <= 1)
1657 return NULL;
1660 /* Skip any zero padding. */
1661 while (!string[0]) {
1662 string++;
1663 if ((*secsize)-- <= 1)
1664 return NULL;
1666 return string;
1669 static char *get_modinfo(Elf_Shdr *sechdrs,
1670 unsigned int info,
1671 const char *tag)
1673 char *p;
1674 unsigned int taglen = strlen(tag);
1675 unsigned long size = sechdrs[info].sh_size;
1677 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1678 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1679 return p + taglen + 1;
1681 return NULL;
1684 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1685 unsigned int infoindex)
1687 struct module_attribute *attr;
1688 int i;
1690 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1691 if (attr->setup)
1692 attr->setup(mod,
1693 get_modinfo(sechdrs,
1694 infoindex,
1695 attr->attr.name));
1699 #ifdef CONFIG_KALLSYMS
1701 /* lookup symbol in given range of kernel_symbols */
1702 static const struct kernel_symbol *lookup_symbol(const char *name,
1703 const struct kernel_symbol *start,
1704 const struct kernel_symbol *stop)
1706 const struct kernel_symbol *ks = start;
1707 for (; ks < stop; ks++)
1708 if (strcmp(ks->name, name) == 0)
1709 return ks;
1710 return NULL;
1713 static int is_exported(const char *name, const struct module *mod)
1715 if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
1716 return 1;
1717 else
1718 if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
1719 return 1;
1720 else
1721 return 0;
1724 /* As per nm */
1725 static char elf_type(const Elf_Sym *sym,
1726 Elf_Shdr *sechdrs,
1727 const char *secstrings,
1728 struct module *mod)
1730 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1731 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1732 return 'v';
1733 else
1734 return 'w';
1736 if (sym->st_shndx == SHN_UNDEF)
1737 return 'U';
1738 if (sym->st_shndx == SHN_ABS)
1739 return 'a';
1740 if (sym->st_shndx >= SHN_LORESERVE)
1741 return '?';
1742 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1743 return 't';
1744 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1745 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1746 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1747 return 'r';
1748 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1749 return 'g';
1750 else
1751 return 'd';
1753 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1754 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1755 return 's';
1756 else
1757 return 'b';
1759 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1760 ".debug", strlen(".debug")) == 0)
1761 return 'n';
1762 return '?';
1765 static void add_kallsyms(struct module *mod,
1766 Elf_Shdr *sechdrs,
1767 unsigned int symindex,
1768 unsigned int strindex,
1769 const char *secstrings)
1771 unsigned int i;
1773 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1774 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1775 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1777 /* Set types up while we still have access to sections. */
1778 for (i = 0; i < mod->num_symtab; i++)
1779 mod->symtab[i].st_info
1780 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1782 #else
1783 static inline void add_kallsyms(struct module *mod,
1784 Elf_Shdr *sechdrs,
1785 unsigned int symindex,
1786 unsigned int strindex,
1787 const char *secstrings)
1790 #endif /* CONFIG_KALLSYMS */
1792 #ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
1793 static void dynamic_printk_setup(Elf_Shdr *sechdrs, unsigned int verboseindex)
1795 struct mod_debug *debug_info;
1796 unsigned long pos, end;
1797 unsigned int num_verbose;
1799 pos = sechdrs[verboseindex].sh_addr;
1800 num_verbose = sechdrs[verboseindex].sh_size /
1801 sizeof(struct mod_debug);
1802 end = pos + (num_verbose * sizeof(struct mod_debug));
1804 for (; pos < end; pos += sizeof(struct mod_debug)) {
1805 debug_info = (struct mod_debug *)pos;
1806 register_dynamic_debug_module(debug_info->modname,
1807 debug_info->type, debug_info->logical_modname,
1808 debug_info->flag_names, debug_info->hash,
1809 debug_info->hash2);
1812 #else
1813 static inline void dynamic_printk_setup(Elf_Shdr *sechdrs,
1814 unsigned int verboseindex)
1817 #endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1819 static void *module_alloc_update_bounds(unsigned long size)
1821 void *ret = module_alloc(size);
1823 if (ret) {
1824 /* Update module bounds. */
1825 if ((unsigned long)ret < module_addr_min)
1826 module_addr_min = (unsigned long)ret;
1827 if ((unsigned long)ret + size > module_addr_max)
1828 module_addr_max = (unsigned long)ret + size;
1830 return ret;
1833 /* Allocate and load the module: note that size of section 0 is always
1834 zero, and we rely on this for optional sections. */
1835 static noinline struct module *load_module(void __user *umod,
1836 unsigned long len,
1837 const char __user *uargs)
1839 Elf_Ehdr *hdr;
1840 Elf_Shdr *sechdrs;
1841 char *secstrings, *args, *modmagic, *strtab = NULL;
1842 char *staging;
1843 unsigned int i;
1844 unsigned int symindex = 0;
1845 unsigned int strindex = 0;
1846 unsigned int setupindex;
1847 unsigned int exindex;
1848 unsigned int exportindex;
1849 unsigned int modindex;
1850 unsigned int obsparmindex;
1851 unsigned int infoindex;
1852 unsigned int gplindex;
1853 unsigned int crcindex;
1854 unsigned int gplcrcindex;
1855 unsigned int versindex;
1856 unsigned int pcpuindex;
1857 unsigned int gplfutureindex;
1858 unsigned int gplfuturecrcindex;
1859 unsigned int unwindex = 0;
1860 #ifdef CONFIG_UNUSED_SYMBOLS
1861 unsigned int unusedindex;
1862 unsigned int unusedcrcindex;
1863 unsigned int unusedgplindex;
1864 unsigned int unusedgplcrcindex;
1865 #endif
1866 unsigned int markersindex;
1867 unsigned int markersstringsindex;
1868 unsigned int verboseindex;
1869 unsigned int tracepointsindex;
1870 unsigned int tracepointsstringsindex;
1871 unsigned int mcountindex;
1872 struct module *mod;
1873 long err = 0;
1874 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1875 void *mseg;
1876 struct exception_table_entry *extable;
1877 mm_segment_t old_fs;
1879 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1880 umod, len, uargs);
1881 if (len < sizeof(*hdr))
1882 return ERR_PTR(-ENOEXEC);
1884 /* Suck in entire file: we'll want most of it. */
1885 /* vmalloc barfs on "unusual" numbers. Check here */
1886 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1887 return ERR_PTR(-ENOMEM);
1888 if (copy_from_user(hdr, umod, len) != 0) {
1889 err = -EFAULT;
1890 goto free_hdr;
1893 /* Sanity checks against insmoding binaries or wrong arch,
1894 weird elf version */
1895 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
1896 || hdr->e_type != ET_REL
1897 || !elf_check_arch(hdr)
1898 || hdr->e_shentsize != sizeof(*sechdrs)) {
1899 err = -ENOEXEC;
1900 goto free_hdr;
1903 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1904 goto truncated;
1906 /* Convenience variables */
1907 sechdrs = (void *)hdr + hdr->e_shoff;
1908 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1909 sechdrs[0].sh_addr = 0;
1911 for (i = 1; i < hdr->e_shnum; i++) {
1912 if (sechdrs[i].sh_type != SHT_NOBITS
1913 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1914 goto truncated;
1916 /* Mark all sections sh_addr with their address in the
1917 temporary image. */
1918 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1920 /* Internal symbols and strings. */
1921 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1922 symindex = i;
1923 strindex = sechdrs[i].sh_link;
1924 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1926 #ifndef CONFIG_MODULE_UNLOAD
1927 /* Don't load .exit sections */
1928 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1929 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1930 #endif
1933 modindex = find_sec(hdr, sechdrs, secstrings,
1934 ".gnu.linkonce.this_module");
1935 if (!modindex) {
1936 printk(KERN_WARNING "No module found in object\n");
1937 err = -ENOEXEC;
1938 goto free_hdr;
1940 mod = (void *)sechdrs[modindex].sh_addr;
1942 if (symindex == 0) {
1943 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1944 mod->name);
1945 err = -ENOEXEC;
1946 goto free_hdr;
1949 /* Optional sections */
1950 exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
1951 gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
1952 gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
1953 crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
1954 gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
1955 gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
1956 #ifdef CONFIG_UNUSED_SYMBOLS
1957 unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
1958 unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
1959 unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
1960 unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
1961 #endif
1962 setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
1963 exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
1964 obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
1965 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1966 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1967 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1968 #ifdef ARCH_UNWIND_SECTION_NAME
1969 unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
1970 #endif
1972 /* Don't keep modinfo and version sections. */
1973 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1974 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1975 #ifdef CONFIG_KALLSYMS
1976 /* Keep symbol and string tables for decoding later. */
1977 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1978 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1979 #endif
1980 if (unwindex)
1981 sechdrs[unwindex].sh_flags |= SHF_ALLOC;
1983 /* Check module struct version now, before we try to use module. */
1984 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1985 err = -ENOEXEC;
1986 goto free_hdr;
1989 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1990 /* This is allowed: modprobe --force will invalidate it. */
1991 if (!modmagic) {
1992 err = try_to_force_load(mod, "magic");
1993 if (err)
1994 goto free_hdr;
1995 } else if (!same_magic(modmagic, vermagic, versindex)) {
1996 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1997 mod->name, modmagic, vermagic);
1998 err = -ENOEXEC;
1999 goto free_hdr;
2002 staging = get_modinfo(sechdrs, infoindex, "staging");
2003 if (staging) {
2004 add_taint_module(mod, TAINT_CRAP);
2005 printk(KERN_WARNING "%s: module is from the staging directory,"
2006 " the quality is unknown, you have been warned.\n",
2007 mod->name);
2010 /* Now copy in args */
2011 args = strndup_user(uargs, ~0UL >> 1);
2012 if (IS_ERR(args)) {
2013 err = PTR_ERR(args);
2014 goto free_hdr;
2017 if (find_module(mod->name)) {
2018 err = -EEXIST;
2019 goto free_mod;
2022 mod->state = MODULE_STATE_COMING;
2024 /* Allow arches to frob section contents and sizes. */
2025 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2026 if (err < 0)
2027 goto free_mod;
2029 if (pcpuindex) {
2030 /* We have a special allocation for this section. */
2031 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
2032 sechdrs[pcpuindex].sh_addralign,
2033 mod->name);
2034 if (!percpu) {
2035 err = -ENOMEM;
2036 goto free_mod;
2038 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2039 mod->percpu = percpu;
2042 /* Determine total sizes, and put offsets in sh_entsize. For now
2043 this is done generically; there doesn't appear to be any
2044 special cases for the architectures. */
2045 layout_sections(mod, hdr, sechdrs, secstrings);
2047 /* Do the allocs. */
2048 ptr = module_alloc_update_bounds(mod->core_size);
2049 if (!ptr) {
2050 err = -ENOMEM;
2051 goto free_percpu;
2053 memset(ptr, 0, mod->core_size);
2054 mod->module_core = ptr;
2056 ptr = module_alloc_update_bounds(mod->init_size);
2057 if (!ptr && mod->init_size) {
2058 err = -ENOMEM;
2059 goto free_core;
2061 memset(ptr, 0, mod->init_size);
2062 mod->module_init = ptr;
2064 /* Transfer each section which specifies SHF_ALLOC */
2065 DEBUGP("final section addresses:\n");
2066 for (i = 0; i < hdr->e_shnum; i++) {
2067 void *dest;
2069 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2070 continue;
2072 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2073 dest = mod->module_init
2074 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2075 else
2076 dest = mod->module_core + sechdrs[i].sh_entsize;
2078 if (sechdrs[i].sh_type != SHT_NOBITS)
2079 memcpy(dest, (void *)sechdrs[i].sh_addr,
2080 sechdrs[i].sh_size);
2081 /* Update sh_addr to point to copy in image. */
2082 sechdrs[i].sh_addr = (unsigned long)dest;
2083 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2085 /* Module has been moved. */
2086 mod = (void *)sechdrs[modindex].sh_addr;
2088 /* Now we've moved module, initialize linked lists, etc. */
2089 module_unload_init(mod);
2091 /* add kobject, so we can reference it. */
2092 err = mod_sysfs_init(mod);
2093 if (err)
2094 goto free_unload;
2096 /* Set up license info based on the info section */
2097 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2100 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2101 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2102 * using GPL-only symbols it needs.
2104 if (strcmp(mod->name, "ndiswrapper") == 0)
2105 add_taint(TAINT_PROPRIETARY_MODULE);
2107 /* driverloader was caught wrongly pretending to be under GPL */
2108 if (strcmp(mod->name, "driverloader") == 0)
2109 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2111 /* Set up MODINFO_ATTR fields */
2112 setup_modinfo(mod, sechdrs, infoindex);
2114 /* Fix up syms, so that st_value is a pointer to location. */
2115 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2116 mod);
2117 if (err < 0)
2118 goto cleanup;
2120 /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
2121 mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
2122 mod->syms = (void *)sechdrs[exportindex].sh_addr;
2123 if (crcindex)
2124 mod->crcs = (void *)sechdrs[crcindex].sh_addr;
2125 mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
2126 mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
2127 if (gplcrcindex)
2128 mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
2129 mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
2130 sizeof(*mod->gpl_future_syms);
2131 mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
2132 if (gplfuturecrcindex)
2133 mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
2135 #ifdef CONFIG_UNUSED_SYMBOLS
2136 mod->num_unused_syms = sechdrs[unusedindex].sh_size /
2137 sizeof(*mod->unused_syms);
2138 mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
2139 sizeof(*mod->unused_gpl_syms);
2140 mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
2141 if (unusedcrcindex)
2142 mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
2143 mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
2144 if (unusedgplcrcindex)
2145 mod->unused_gpl_crcs
2146 = (void *)sechdrs[unusedgplcrcindex].sh_addr;
2147 #endif
2149 #ifdef CONFIG_MODVERSIONS
2150 if ((mod->num_syms && !crcindex)
2151 || (mod->num_gpl_syms && !gplcrcindex)
2152 || (mod->num_gpl_future_syms && !gplfuturecrcindex)
2153 #ifdef CONFIG_UNUSED_SYMBOLS
2154 || (mod->num_unused_syms && !unusedcrcindex)
2155 || (mod->num_unused_gpl_syms && !unusedgplcrcindex)
2156 #endif
2158 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2159 err = try_to_force_load(mod, "nocrc");
2160 if (err)
2161 goto cleanup;
2163 #endif
2164 markersindex = find_sec(hdr, sechdrs, secstrings, "__markers");
2165 markersstringsindex = find_sec(hdr, sechdrs, secstrings,
2166 "__markers_strings");
2167 verboseindex = find_sec(hdr, sechdrs, secstrings, "__verbose");
2168 tracepointsindex = find_sec(hdr, sechdrs, secstrings, "__tracepoints");
2169 tracepointsstringsindex = find_sec(hdr, sechdrs, secstrings,
2170 "__tracepoints_strings");
2172 mcountindex = find_sec(hdr, sechdrs, secstrings,
2173 "__mcount_loc");
2175 /* Now do relocations. */
2176 for (i = 1; i < hdr->e_shnum; i++) {
2177 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2178 unsigned int info = sechdrs[i].sh_info;
2180 /* Not a valid relocation section? */
2181 if (info >= hdr->e_shnum)
2182 continue;
2184 /* Don't bother with non-allocated sections */
2185 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2186 continue;
2188 if (sechdrs[i].sh_type == SHT_REL)
2189 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2190 else if (sechdrs[i].sh_type == SHT_RELA)
2191 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2192 mod);
2193 if (err < 0)
2194 goto cleanup;
2196 #ifdef CONFIG_MARKERS
2197 mod->markers = (void *)sechdrs[markersindex].sh_addr;
2198 mod->num_markers =
2199 sechdrs[markersindex].sh_size / sizeof(*mod->markers);
2200 #endif
2201 #ifdef CONFIG_TRACEPOINTS
2202 mod->tracepoints = (void *)sechdrs[tracepointsindex].sh_addr;
2203 mod->num_tracepoints =
2204 sechdrs[tracepointsindex].sh_size / sizeof(*mod->tracepoints);
2205 #endif
2208 /* Find duplicate symbols */
2209 err = verify_export_symbols(mod);
2211 if (err < 0)
2212 goto cleanup;
2214 /* Set up and sort exception table */
2215 mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
2216 mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
2217 sort_extable(extable, extable + mod->num_exentries);
2219 /* Finally, copy percpu area over. */
2220 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2221 sechdrs[pcpuindex].sh_size);
2223 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2225 if (!mod->taints) {
2226 #ifdef CONFIG_MARKERS
2227 marker_update_probe_range(mod->markers,
2228 mod->markers + mod->num_markers);
2229 #endif
2230 dynamic_printk_setup(sechdrs, verboseindex);
2231 #ifdef CONFIG_TRACEPOINTS
2232 tracepoint_update_probe_range(mod->tracepoints,
2233 mod->tracepoints + mod->num_tracepoints);
2234 #endif
2237 /* sechdrs[0].sh_size is always zero */
2238 mseg = (void *)sechdrs[mcountindex].sh_addr;
2239 ftrace_init_module(mseg, mseg + sechdrs[mcountindex].sh_size);
2241 err = module_finalize(hdr, sechdrs, mod);
2242 if (err < 0)
2243 goto cleanup;
2245 /* flush the icache in correct context */
2246 old_fs = get_fs();
2247 set_fs(KERNEL_DS);
2250 * Flush the instruction cache, since we've played with text.
2251 * Do it before processing of module parameters, so the module
2252 * can provide parameter accessor functions of its own.
2254 if (mod->module_init)
2255 flush_icache_range((unsigned long)mod->module_init,
2256 (unsigned long)mod->module_init
2257 + mod->init_size);
2258 flush_icache_range((unsigned long)mod->module_core,
2259 (unsigned long)mod->module_core + mod->core_size);
2261 set_fs(old_fs);
2263 mod->args = args;
2264 if (obsparmindex)
2265 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2266 mod->name);
2268 /* Now sew it into the lists so we can get lockdep and oops
2269 * info during argument parsing. Noone should access us, since
2270 * strong_try_module_get() will fail. */
2271 stop_machine(__link_module, mod, NULL);
2273 /* Size of section 0 is 0, so this works well if no params */
2274 err = parse_args(mod->name, mod->args,
2275 (struct kernel_param *)
2276 sechdrs[setupindex].sh_addr,
2277 sechdrs[setupindex].sh_size
2278 / sizeof(struct kernel_param),
2279 NULL);
2280 if (err < 0)
2281 goto unlink;
2283 err = mod_sysfs_setup(mod,
2284 (struct kernel_param *)
2285 sechdrs[setupindex].sh_addr,
2286 sechdrs[setupindex].sh_size
2287 / sizeof(struct kernel_param));
2288 if (err < 0)
2289 goto unlink;
2290 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2291 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2293 /* Size of section 0 is 0, so this works well if no unwind info. */
2294 mod->unwind_info = unwind_add_table(mod,
2295 (void *)sechdrs[unwindex].sh_addr,
2296 sechdrs[unwindex].sh_size);
2298 /* Get rid of temporary copy */
2299 vfree(hdr);
2301 /* Done! */
2302 return mod;
2304 unlink:
2305 stop_machine(__unlink_module, mod, NULL);
2306 module_arch_cleanup(mod);
2307 cleanup:
2308 kobject_del(&mod->mkobj.kobj);
2309 kobject_put(&mod->mkobj.kobj);
2310 ftrace_release(mod->module_core, mod->core_size);
2311 free_unload:
2312 module_unload_free(mod);
2313 module_free(mod, mod->module_init);
2314 free_core:
2315 module_free(mod, mod->module_core);
2316 free_percpu:
2317 if (percpu)
2318 percpu_modfree(percpu);
2319 free_mod:
2320 kfree(args);
2321 free_hdr:
2322 vfree(hdr);
2323 return ERR_PTR(err);
2325 truncated:
2326 printk(KERN_ERR "Module len %lu truncated\n", len);
2327 err = -ENOEXEC;
2328 goto free_hdr;
2331 /* This is where the real work happens */
2332 asmlinkage long
2333 sys_init_module(void __user *umod,
2334 unsigned long len,
2335 const char __user *uargs)
2337 struct module *mod;
2338 int ret = 0;
2340 /* Must have permission */
2341 if (!capable(CAP_SYS_MODULE))
2342 return -EPERM;
2344 /* Only one module load at a time, please */
2345 if (mutex_lock_interruptible(&module_mutex) != 0)
2346 return -EINTR;
2348 /* Do all the hard work */
2349 mod = load_module(umod, len, uargs);
2350 if (IS_ERR(mod)) {
2351 mutex_unlock(&module_mutex);
2352 return PTR_ERR(mod);
2355 /* Drop lock so they can recurse */
2356 mutex_unlock(&module_mutex);
2358 blocking_notifier_call_chain(&module_notify_list,
2359 MODULE_STATE_COMING, mod);
2361 /* Start the module */
2362 if (mod->init != NULL)
2363 ret = do_one_initcall(mod->init);
2364 if (ret < 0) {
2365 /* Init routine failed: abort. Try to protect us from
2366 buggy refcounters. */
2367 mod->state = MODULE_STATE_GOING;
2368 synchronize_sched();
2369 module_put(mod);
2370 blocking_notifier_call_chain(&module_notify_list,
2371 MODULE_STATE_GOING, mod);
2372 mutex_lock(&module_mutex);
2373 free_module(mod);
2374 mutex_unlock(&module_mutex);
2375 wake_up(&module_wq);
2376 return ret;
2378 if (ret > 0) {
2379 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2380 "it should follow 0/-E convention\n"
2381 KERN_WARNING "%s: loading module anyway...\n",
2382 __func__, mod->name, ret,
2383 __func__);
2384 dump_stack();
2387 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2388 mod->state = MODULE_STATE_LIVE;
2389 wake_up(&module_wq);
2391 mutex_lock(&module_mutex);
2392 /* Drop initial reference. */
2393 module_put(mod);
2394 unwind_remove_table(mod->unwind_info, 1);
2395 module_free(mod, mod->module_init);
2396 mod->module_init = NULL;
2397 mod->init_size = 0;
2398 mod->init_text_size = 0;
2399 mutex_unlock(&module_mutex);
2401 return 0;
2404 static inline int within(unsigned long addr, void *start, unsigned long size)
2406 return ((void *)addr >= start && (void *)addr < start + size);
2409 #ifdef CONFIG_KALLSYMS
2411 * This ignores the intensely annoying "mapping symbols" found
2412 * in ARM ELF files: $a, $t and $d.
2414 static inline int is_arm_mapping_symbol(const char *str)
2416 return str[0] == '$' && strchr("atd", str[1])
2417 && (str[2] == '\0' || str[2] == '.');
2420 static const char *get_ksymbol(struct module *mod,
2421 unsigned long addr,
2422 unsigned long *size,
2423 unsigned long *offset)
2425 unsigned int i, best = 0;
2426 unsigned long nextval;
2428 /* At worse, next value is at end of module */
2429 if (within(addr, mod->module_init, mod->init_size))
2430 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2431 else
2432 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2434 /* Scan for closest preceeding symbol, and next symbol. (ELF
2435 starts real symbols at 1). */
2436 for (i = 1; i < mod->num_symtab; i++) {
2437 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2438 continue;
2440 /* We ignore unnamed symbols: they're uninformative
2441 * and inserted at a whim. */
2442 if (mod->symtab[i].st_value <= addr
2443 && mod->symtab[i].st_value > mod->symtab[best].st_value
2444 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2445 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2446 best = i;
2447 if (mod->symtab[i].st_value > addr
2448 && mod->symtab[i].st_value < nextval
2449 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2450 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2451 nextval = mod->symtab[i].st_value;
2454 if (!best)
2455 return NULL;
2457 if (size)
2458 *size = nextval - mod->symtab[best].st_value;
2459 if (offset)
2460 *offset = addr - mod->symtab[best].st_value;
2461 return mod->strtab + mod->symtab[best].st_name;
2464 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2465 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2466 const char *module_address_lookup(unsigned long addr,
2467 unsigned long *size,
2468 unsigned long *offset,
2469 char **modname,
2470 char *namebuf)
2472 struct module *mod;
2473 const char *ret = NULL;
2475 preempt_disable();
2476 list_for_each_entry(mod, &modules, list) {
2477 if (within(addr, mod->module_init, mod->init_size)
2478 || within(addr, mod->module_core, mod->core_size)) {
2479 if (modname)
2480 *modname = mod->name;
2481 ret = get_ksymbol(mod, addr, size, offset);
2482 break;
2485 /* Make a copy in here where it's safe */
2486 if (ret) {
2487 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2488 ret = namebuf;
2490 preempt_enable();
2491 return ret;
2494 int lookup_module_symbol_name(unsigned long addr, char *symname)
2496 struct module *mod;
2498 preempt_disable();
2499 list_for_each_entry(mod, &modules, list) {
2500 if (within(addr, mod->module_init, mod->init_size) ||
2501 within(addr, mod->module_core, mod->core_size)) {
2502 const char *sym;
2504 sym = get_ksymbol(mod, addr, NULL, NULL);
2505 if (!sym)
2506 goto out;
2507 strlcpy(symname, sym, KSYM_NAME_LEN);
2508 preempt_enable();
2509 return 0;
2512 out:
2513 preempt_enable();
2514 return -ERANGE;
2517 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2518 unsigned long *offset, char *modname, char *name)
2520 struct module *mod;
2522 preempt_disable();
2523 list_for_each_entry(mod, &modules, list) {
2524 if (within(addr, mod->module_init, mod->init_size) ||
2525 within(addr, mod->module_core, mod->core_size)) {
2526 const char *sym;
2528 sym = get_ksymbol(mod, addr, size, offset);
2529 if (!sym)
2530 goto out;
2531 if (modname)
2532 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2533 if (name)
2534 strlcpy(name, sym, KSYM_NAME_LEN);
2535 preempt_enable();
2536 return 0;
2539 out:
2540 preempt_enable();
2541 return -ERANGE;
2544 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2545 char *name, char *module_name, int *exported)
2547 struct module *mod;
2549 preempt_disable();
2550 list_for_each_entry(mod, &modules, list) {
2551 if (symnum < mod->num_symtab) {
2552 *value = mod->symtab[symnum].st_value;
2553 *type = mod->symtab[symnum].st_info;
2554 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2555 KSYM_NAME_LEN);
2556 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2557 *exported = is_exported(name, mod);
2558 preempt_enable();
2559 return 0;
2561 symnum -= mod->num_symtab;
2563 preempt_enable();
2564 return -ERANGE;
2567 static unsigned long mod_find_symname(struct module *mod, const char *name)
2569 unsigned int i;
2571 for (i = 0; i < mod->num_symtab; i++)
2572 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2573 mod->symtab[i].st_info != 'U')
2574 return mod->symtab[i].st_value;
2575 return 0;
2578 /* Look for this name: can be of form module:name. */
2579 unsigned long module_kallsyms_lookup_name(const char *name)
2581 struct module *mod;
2582 char *colon;
2583 unsigned long ret = 0;
2585 /* Don't lock: we're in enough trouble already. */
2586 preempt_disable();
2587 if ((colon = strchr(name, ':')) != NULL) {
2588 *colon = '\0';
2589 if ((mod = find_module(name)) != NULL)
2590 ret = mod_find_symname(mod, colon+1);
2591 *colon = ':';
2592 } else {
2593 list_for_each_entry(mod, &modules, list)
2594 if ((ret = mod_find_symname(mod, name)) != 0)
2595 break;
2597 preempt_enable();
2598 return ret;
2600 #endif /* CONFIG_KALLSYMS */
2602 /* Called by the /proc file system to return a list of modules. */
2603 static void *m_start(struct seq_file *m, loff_t *pos)
2605 mutex_lock(&module_mutex);
2606 return seq_list_start(&modules, *pos);
2609 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2611 return seq_list_next(p, &modules, pos);
2614 static void m_stop(struct seq_file *m, void *p)
2616 mutex_unlock(&module_mutex);
2619 static char *module_flags(struct module *mod, char *buf)
2621 int bx = 0;
2623 if (mod->taints ||
2624 mod->state == MODULE_STATE_GOING ||
2625 mod->state == MODULE_STATE_COMING) {
2626 buf[bx++] = '(';
2627 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
2628 buf[bx++] = 'P';
2629 if (mod->taints & (1 << TAINT_FORCED_MODULE))
2630 buf[bx++] = 'F';
2631 if (mod->taints & (1 << TAINT_CRAP))
2632 buf[bx++] = 'C';
2634 * TAINT_FORCED_RMMOD: could be added.
2635 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2636 * apply to modules.
2639 /* Show a - for module-is-being-unloaded */
2640 if (mod->state == MODULE_STATE_GOING)
2641 buf[bx++] = '-';
2642 /* Show a + for module-is-being-loaded */
2643 if (mod->state == MODULE_STATE_COMING)
2644 buf[bx++] = '+';
2645 buf[bx++] = ')';
2647 buf[bx] = '\0';
2649 return buf;
2652 static int m_show(struct seq_file *m, void *p)
2654 struct module *mod = list_entry(p, struct module, list);
2655 char buf[8];
2657 seq_printf(m, "%s %u",
2658 mod->name, mod->init_size + mod->core_size);
2659 print_unload_info(m, mod);
2661 /* Informative for users. */
2662 seq_printf(m, " %s",
2663 mod->state == MODULE_STATE_GOING ? "Unloading":
2664 mod->state == MODULE_STATE_COMING ? "Loading":
2665 "Live");
2666 /* Used by oprofile and other similar tools. */
2667 seq_printf(m, " 0x%p", mod->module_core);
2669 /* Taints info */
2670 if (mod->taints)
2671 seq_printf(m, " %s", module_flags(mod, buf));
2673 seq_printf(m, "\n");
2674 return 0;
2677 /* Format: modulename size refcount deps address
2679 Where refcount is a number or -, and deps is a comma-separated list
2680 of depends or -.
2682 const struct seq_operations modules_op = {
2683 .start = m_start,
2684 .next = m_next,
2685 .stop = m_stop,
2686 .show = m_show
2689 /* Given an address, look for it in the module exception tables. */
2690 const struct exception_table_entry *search_module_extables(unsigned long addr)
2692 const struct exception_table_entry *e = NULL;
2693 struct module *mod;
2695 preempt_disable();
2696 list_for_each_entry(mod, &modules, list) {
2697 if (mod->num_exentries == 0)
2698 continue;
2700 e = search_extable(mod->extable,
2701 mod->extable + mod->num_exentries - 1,
2702 addr);
2703 if (e)
2704 break;
2706 preempt_enable();
2708 /* Now, if we found one, we are running inside it now, hence
2709 we cannot unload the module, hence no refcnt needed. */
2710 return e;
2714 * Is this a valid module address?
2716 int is_module_address(unsigned long addr)
2718 struct module *mod;
2720 preempt_disable();
2722 list_for_each_entry(mod, &modules, list) {
2723 if (within(addr, mod->module_core, mod->core_size)) {
2724 preempt_enable();
2725 return 1;
2729 preempt_enable();
2731 return 0;
2735 /* Is this a valid kernel address? */
2736 struct module *__module_text_address(unsigned long addr)
2738 struct module *mod;
2740 if (addr < module_addr_min || addr > module_addr_max)
2741 return NULL;
2743 list_for_each_entry(mod, &modules, list)
2744 if (within(addr, mod->module_init, mod->init_text_size)
2745 || within(addr, mod->module_core, mod->core_text_size))
2746 return mod;
2747 return NULL;
2750 struct module *module_text_address(unsigned long addr)
2752 struct module *mod;
2754 preempt_disable();
2755 mod = __module_text_address(addr);
2756 preempt_enable();
2758 return mod;
2761 /* Don't grab lock, we're oopsing. */
2762 void print_modules(void)
2764 struct module *mod;
2765 char buf[8];
2767 printk("Modules linked in:");
2768 list_for_each_entry(mod, &modules, list)
2769 printk(" %s%s", mod->name, module_flags(mod, buf));
2770 if (last_unloaded_module[0])
2771 printk(" [last unloaded: %s]", last_unloaded_module);
2772 printk("\n");
2775 #ifdef CONFIG_MODVERSIONS
2776 /* Generate the signature for struct module here, too, for modversions. */
2777 void struct_module(struct module *mod) { return; }
2778 EXPORT_SYMBOL(struct_module);
2779 #endif
2781 #ifdef CONFIG_MARKERS
2782 void module_update_markers(void)
2784 struct module *mod;
2786 mutex_lock(&module_mutex);
2787 list_for_each_entry(mod, &modules, list)
2788 if (!mod->taints)
2789 marker_update_probe_range(mod->markers,
2790 mod->markers + mod->num_markers);
2791 mutex_unlock(&module_mutex);
2793 #endif
2795 #ifdef CONFIG_TRACEPOINTS
2796 void module_update_tracepoints(void)
2798 struct module *mod;
2800 mutex_lock(&module_mutex);
2801 list_for_each_entry(mod, &modules, list)
2802 if (!mod->taints)
2803 tracepoint_update_probe_range(mod->tracepoints,
2804 mod->tracepoints + mod->num_tracepoints);
2805 mutex_unlock(&module_mutex);
2809 * Returns 0 if current not found.
2810 * Returns 1 if current found.
2812 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2814 struct module *iter_mod;
2815 int found = 0;
2817 mutex_lock(&module_mutex);
2818 list_for_each_entry(iter_mod, &modules, list) {
2819 if (!iter_mod->taints) {
2821 * Sorted module list
2823 if (iter_mod < iter->module)
2824 continue;
2825 else if (iter_mod > iter->module)
2826 iter->tracepoint = NULL;
2827 found = tracepoint_get_iter_range(&iter->tracepoint,
2828 iter_mod->tracepoints,
2829 iter_mod->tracepoints
2830 + iter_mod->num_tracepoints);
2831 if (found) {
2832 iter->module = iter_mod;
2833 break;
2837 mutex_unlock(&module_mutex);
2838 return found;
2840 #endif