Blackfin arch: show_cpuinfo - consolidate ugly casts
[linux-2.6/mini2440.git] / kernel / module.c
blob496dcb57b608e0e6c60ebc5a1be002761bdf66a2
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/fs.h>
24 #include <linux/sysfs.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/elf.h>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <linux/syscalls.h>
32 #include <linux/fcntl.h>
33 #include <linux/rcupdate.h>
34 #include <linux/capability.h>
35 #include <linux/cpu.h>
36 #include <linux/moduleparam.h>
37 #include <linux/errno.h>
38 #include <linux/err.h>
39 #include <linux/vermagic.h>
40 #include <linux/notifier.h>
41 #include <linux/sched.h>
42 #include <linux/stop_machine.h>
43 #include <linux/device.h>
44 #include <linux/string.h>
45 #include <linux/mutex.h>
46 #include <linux/rculist.h>
47 #include <asm/uaccess.h>
48 #include <asm/cacheflush.h>
49 #include <linux/license.h>
50 #include <asm/sections.h>
51 #include <linux/tracepoint.h>
52 #include <linux/ftrace.h>
54 #if 0
55 #define DEBUGP printk
56 #else
57 #define DEBUGP(fmt , a...)
58 #endif
60 #ifndef ARCH_SHF_SMALL
61 #define ARCH_SHF_SMALL 0
62 #endif
64 /* If this is set, the section belongs in the init part of the module */
65 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
67 /* List of modules, protected by module_mutex or preempt_disable
68 * (delete uses stop_machine/add uses RCU list operations). */
69 static DEFINE_MUTEX(module_mutex);
70 static LIST_HEAD(modules);
72 /* Waiting for a module to finish initializing? */
73 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
75 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
77 /* Bounds of module allocation, for speeding __module_text_address */
78 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
80 int register_module_notifier(struct notifier_block * nb)
82 return blocking_notifier_chain_register(&module_notify_list, nb);
84 EXPORT_SYMBOL(register_module_notifier);
86 int unregister_module_notifier(struct notifier_block * nb)
88 return blocking_notifier_chain_unregister(&module_notify_list, nb);
90 EXPORT_SYMBOL(unregister_module_notifier);
92 /* We require a truly strong try_module_get(): 0 means failure due to
93 ongoing or failed initialization etc. */
94 static inline int strong_try_module_get(struct module *mod)
96 if (mod && mod->state == MODULE_STATE_COMING)
97 return -EBUSY;
98 if (try_module_get(mod))
99 return 0;
100 else
101 return -ENOENT;
104 static inline void add_taint_module(struct module *mod, unsigned flag)
106 add_taint(flag);
107 mod->taints |= (1U << flag);
111 * A thread that wants to hold a reference to a module only while it
112 * is running can call this to safely exit. nfsd and lockd use this.
114 void __module_put_and_exit(struct module *mod, long code)
116 module_put(mod);
117 do_exit(code);
119 EXPORT_SYMBOL(__module_put_and_exit);
121 /* Find a module section: 0 means not found. */
122 static unsigned int find_sec(Elf_Ehdr *hdr,
123 Elf_Shdr *sechdrs,
124 const char *secstrings,
125 const char *name)
127 unsigned int i;
129 for (i = 1; i < hdr->e_shnum; i++)
130 /* Alloc bit cleared means "ignore it." */
131 if ((sechdrs[i].sh_flags & SHF_ALLOC)
132 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
133 return i;
134 return 0;
137 /* Find a module section, or NULL. */
138 static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
139 const char *secstrings, const char *name)
141 /* Section 0 has sh_addr 0. */
142 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
145 /* Find a module section, or NULL. Fill in number of "objects" in section. */
146 static void *section_objs(Elf_Ehdr *hdr,
147 Elf_Shdr *sechdrs,
148 const char *secstrings,
149 const char *name,
150 size_t object_size,
151 unsigned int *num)
153 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
155 /* Section 0 has sh_addr 0 and sh_size 0. */
156 *num = sechdrs[sec].sh_size / object_size;
157 return (void *)sechdrs[sec].sh_addr;
160 /* Provided by the linker */
161 extern const struct kernel_symbol __start___ksymtab[];
162 extern const struct kernel_symbol __stop___ksymtab[];
163 extern const struct kernel_symbol __start___ksymtab_gpl[];
164 extern const struct kernel_symbol __stop___ksymtab_gpl[];
165 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
166 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
167 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
168 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
169 extern const unsigned long __start___kcrctab[];
170 extern const unsigned long __start___kcrctab_gpl[];
171 extern const unsigned long __start___kcrctab_gpl_future[];
172 #ifdef CONFIG_UNUSED_SYMBOLS
173 extern const struct kernel_symbol __start___ksymtab_unused[];
174 extern const struct kernel_symbol __stop___ksymtab_unused[];
175 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
176 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
177 extern const unsigned long __start___kcrctab_unused[];
178 extern const unsigned long __start___kcrctab_unused_gpl[];
179 #endif
181 #ifndef CONFIG_MODVERSIONS
182 #define symversion(base, idx) NULL
183 #else
184 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
185 #endif
187 struct symsearch {
188 const struct kernel_symbol *start, *stop;
189 const unsigned long *crcs;
190 enum {
191 NOT_GPL_ONLY,
192 GPL_ONLY,
193 WILL_BE_GPL_ONLY,
194 } licence;
195 bool unused;
198 static bool each_symbol_in_section(const struct symsearch *arr,
199 unsigned int arrsize,
200 struct module *owner,
201 bool (*fn)(const struct symsearch *syms,
202 struct module *owner,
203 unsigned int symnum, void *data),
204 void *data)
206 unsigned int i, j;
208 for (j = 0; j < arrsize; j++) {
209 for (i = 0; i < arr[j].stop - arr[j].start; i++)
210 if (fn(&arr[j], owner, i, data))
211 return true;
214 return false;
217 /* Returns true as soon as fn returns true, otherwise false. */
218 static bool each_symbol(bool (*fn)(const struct symsearch *arr,
219 struct module *owner,
220 unsigned int symnum, void *data),
221 void *data)
223 struct module *mod;
224 const struct symsearch arr[] = {
225 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
226 NOT_GPL_ONLY, false },
227 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
228 __start___kcrctab_gpl,
229 GPL_ONLY, false },
230 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
231 __start___kcrctab_gpl_future,
232 WILL_BE_GPL_ONLY, false },
233 #ifdef CONFIG_UNUSED_SYMBOLS
234 { __start___ksymtab_unused, __stop___ksymtab_unused,
235 __start___kcrctab_unused,
236 NOT_GPL_ONLY, true },
237 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
238 __start___kcrctab_unused_gpl,
239 GPL_ONLY, true },
240 #endif
243 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
244 return true;
246 list_for_each_entry_rcu(mod, &modules, list) {
247 struct symsearch arr[] = {
248 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
249 NOT_GPL_ONLY, false },
250 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
251 mod->gpl_crcs,
252 GPL_ONLY, false },
253 { mod->gpl_future_syms,
254 mod->gpl_future_syms + mod->num_gpl_future_syms,
255 mod->gpl_future_crcs,
256 WILL_BE_GPL_ONLY, false },
257 #ifdef CONFIG_UNUSED_SYMBOLS
258 { mod->unused_syms,
259 mod->unused_syms + mod->num_unused_syms,
260 mod->unused_crcs,
261 NOT_GPL_ONLY, true },
262 { mod->unused_gpl_syms,
263 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
264 mod->unused_gpl_crcs,
265 GPL_ONLY, true },
266 #endif
269 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
270 return true;
272 return false;
275 struct find_symbol_arg {
276 /* Input */
277 const char *name;
278 bool gplok;
279 bool warn;
281 /* Output */
282 struct module *owner;
283 const unsigned long *crc;
284 unsigned long value;
287 static bool find_symbol_in_section(const struct symsearch *syms,
288 struct module *owner,
289 unsigned int symnum, void *data)
291 struct find_symbol_arg *fsa = data;
293 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
294 return false;
296 if (!fsa->gplok) {
297 if (syms->licence == GPL_ONLY)
298 return false;
299 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
300 printk(KERN_WARNING "Symbol %s is being used "
301 "by a non-GPL module, which will not "
302 "be allowed in the future\n", fsa->name);
303 printk(KERN_WARNING "Please see the file "
304 "Documentation/feature-removal-schedule.txt "
305 "in the kernel source tree for more details.\n");
309 #ifdef CONFIG_UNUSED_SYMBOLS
310 if (syms->unused && fsa->warn) {
311 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
312 "however this module is using it.\n", fsa->name);
313 printk(KERN_WARNING
314 "This symbol will go away in the future.\n");
315 printk(KERN_WARNING
316 "Please evalute if this is the right api to use and if "
317 "it really is, submit a report the linux kernel "
318 "mailinglist together with submitting your code for "
319 "inclusion.\n");
321 #endif
323 fsa->owner = owner;
324 fsa->crc = symversion(syms->crcs, symnum);
325 fsa->value = syms->start[symnum].value;
326 return true;
329 /* Find a symbol, return value, (optional) crc and (optional) module
330 * which owns it */
331 static unsigned long find_symbol(const char *name,
332 struct module **owner,
333 const unsigned long **crc,
334 bool gplok,
335 bool warn)
337 struct find_symbol_arg fsa;
339 fsa.name = name;
340 fsa.gplok = gplok;
341 fsa.warn = warn;
343 if (each_symbol(find_symbol_in_section, &fsa)) {
344 if (owner)
345 *owner = fsa.owner;
346 if (crc)
347 *crc = fsa.crc;
348 return fsa.value;
351 DEBUGP("Failed to find symbol %s\n", name);
352 return -ENOENT;
355 /* Search for module by name: must hold module_mutex. */
356 static struct module *find_module(const char *name)
358 struct module *mod;
360 list_for_each_entry(mod, &modules, list) {
361 if (strcmp(mod->name, name) == 0)
362 return mod;
364 return NULL;
367 #ifdef CONFIG_SMP
368 /* Number of blocks used and allocated. */
369 static unsigned int pcpu_num_used, pcpu_num_allocated;
370 /* Size of each block. -ve means used. */
371 static int *pcpu_size;
373 static int split_block(unsigned int i, unsigned short size)
375 /* Reallocation required? */
376 if (pcpu_num_used + 1 > pcpu_num_allocated) {
377 int *new;
379 new = krealloc(pcpu_size, sizeof(new[0])*pcpu_num_allocated*2,
380 GFP_KERNEL);
381 if (!new)
382 return 0;
384 pcpu_num_allocated *= 2;
385 pcpu_size = new;
388 /* Insert a new subblock */
389 memmove(&pcpu_size[i+1], &pcpu_size[i],
390 sizeof(pcpu_size[0]) * (pcpu_num_used - i));
391 pcpu_num_used++;
393 pcpu_size[i+1] -= size;
394 pcpu_size[i] = size;
395 return 1;
398 static inline unsigned int block_size(int val)
400 if (val < 0)
401 return -val;
402 return val;
405 static void *percpu_modalloc(unsigned long size, unsigned long align,
406 const char *name)
408 unsigned long extra;
409 unsigned int i;
410 void *ptr;
412 if (align > PAGE_SIZE) {
413 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
414 name, align, PAGE_SIZE);
415 align = PAGE_SIZE;
418 ptr = __per_cpu_start;
419 for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
420 /* Extra for alignment requirement. */
421 extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
422 BUG_ON(i == 0 && extra != 0);
424 if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
425 continue;
427 /* Transfer extra to previous block. */
428 if (pcpu_size[i-1] < 0)
429 pcpu_size[i-1] -= extra;
430 else
431 pcpu_size[i-1] += extra;
432 pcpu_size[i] -= extra;
433 ptr += extra;
435 /* Split block if warranted */
436 if (pcpu_size[i] - size > sizeof(unsigned long))
437 if (!split_block(i, size))
438 return NULL;
440 /* Mark allocated */
441 pcpu_size[i] = -pcpu_size[i];
442 return ptr;
445 printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
446 size);
447 return NULL;
450 static void percpu_modfree(void *freeme)
452 unsigned int i;
453 void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
455 /* First entry is core kernel percpu data. */
456 for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
457 if (ptr == freeme) {
458 pcpu_size[i] = -pcpu_size[i];
459 goto free;
462 BUG();
464 free:
465 /* Merge with previous? */
466 if (pcpu_size[i-1] >= 0) {
467 pcpu_size[i-1] += pcpu_size[i];
468 pcpu_num_used--;
469 memmove(&pcpu_size[i], &pcpu_size[i+1],
470 (pcpu_num_used - i) * sizeof(pcpu_size[0]));
471 i--;
473 /* Merge with next? */
474 if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
475 pcpu_size[i] += pcpu_size[i+1];
476 pcpu_num_used--;
477 memmove(&pcpu_size[i+1], &pcpu_size[i+2],
478 (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
482 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
483 Elf_Shdr *sechdrs,
484 const char *secstrings)
486 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
489 static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
491 int cpu;
493 for_each_possible_cpu(cpu)
494 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
497 static int percpu_modinit(void)
499 pcpu_num_used = 2;
500 pcpu_num_allocated = 2;
501 pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
502 GFP_KERNEL);
503 /* Static in-kernel percpu data (used). */
504 pcpu_size[0] = -(__per_cpu_end-__per_cpu_start);
505 /* Free room. */
506 pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
507 if (pcpu_size[1] < 0) {
508 printk(KERN_ERR "No per-cpu room for modules.\n");
509 pcpu_num_used = 1;
512 return 0;
514 __initcall(percpu_modinit);
515 #else /* ... !CONFIG_SMP */
516 static inline void *percpu_modalloc(unsigned long size, unsigned long align,
517 const char *name)
519 return NULL;
521 static inline void percpu_modfree(void *pcpuptr)
523 BUG();
525 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
526 Elf_Shdr *sechdrs,
527 const char *secstrings)
529 return 0;
531 static inline void percpu_modcopy(void *pcpudst, const void *src,
532 unsigned long size)
534 /* pcpusec should be 0, and size of that section should be 0. */
535 BUG_ON(size != 0);
537 #endif /* CONFIG_SMP */
539 #define MODINFO_ATTR(field) \
540 static void setup_modinfo_##field(struct module *mod, const char *s) \
542 mod->field = kstrdup(s, GFP_KERNEL); \
544 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
545 struct module *mod, char *buffer) \
547 return sprintf(buffer, "%s\n", mod->field); \
549 static int modinfo_##field##_exists(struct module *mod) \
551 return mod->field != NULL; \
553 static void free_modinfo_##field(struct module *mod) \
555 kfree(mod->field); \
556 mod->field = NULL; \
558 static struct module_attribute modinfo_##field = { \
559 .attr = { .name = __stringify(field), .mode = 0444 }, \
560 .show = show_modinfo_##field, \
561 .setup = setup_modinfo_##field, \
562 .test = modinfo_##field##_exists, \
563 .free = free_modinfo_##field, \
566 MODINFO_ATTR(version);
567 MODINFO_ATTR(srcversion);
569 static char last_unloaded_module[MODULE_NAME_LEN+1];
571 #ifdef CONFIG_MODULE_UNLOAD
572 /* Init the unload section of the module. */
573 static void module_unload_init(struct module *mod)
575 unsigned int i;
577 INIT_LIST_HEAD(&mod->modules_which_use_me);
578 for (i = 0; i < NR_CPUS; i++)
579 local_set(&mod->ref[i].count, 0);
580 /* Hold reference count during initialization. */
581 local_set(&mod->ref[raw_smp_processor_id()].count, 1);
582 /* Backwards compatibility macros put refcount during init. */
583 mod->waiter = current;
586 /* modules using other modules */
587 struct module_use
589 struct list_head list;
590 struct module *module_which_uses;
593 /* Does a already use b? */
594 static int already_uses(struct module *a, struct module *b)
596 struct module_use *use;
598 list_for_each_entry(use, &b->modules_which_use_me, list) {
599 if (use->module_which_uses == a) {
600 DEBUGP("%s uses %s!\n", a->name, b->name);
601 return 1;
604 DEBUGP("%s does not use %s!\n", a->name, b->name);
605 return 0;
608 /* Module a uses b */
609 static int use_module(struct module *a, struct module *b)
611 struct module_use *use;
612 int no_warn, err;
614 if (b == NULL || already_uses(a, b)) return 1;
616 /* If we're interrupted or time out, we fail. */
617 if (wait_event_interruptible_timeout(
618 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
619 30 * HZ) <= 0) {
620 printk("%s: gave up waiting for init of module %s.\n",
621 a->name, b->name);
622 return 0;
625 /* If strong_try_module_get() returned a different error, we fail. */
626 if (err)
627 return 0;
629 DEBUGP("Allocating new usage for %s.\n", a->name);
630 use = kmalloc(sizeof(*use), GFP_ATOMIC);
631 if (!use) {
632 printk("%s: out of memory loading\n", a->name);
633 module_put(b);
634 return 0;
637 use->module_which_uses = a;
638 list_add(&use->list, &b->modules_which_use_me);
639 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
640 return 1;
643 /* Clear the unload stuff of the module. */
644 static void module_unload_free(struct module *mod)
646 struct module *i;
648 list_for_each_entry(i, &modules, list) {
649 struct module_use *use;
651 list_for_each_entry(use, &i->modules_which_use_me, list) {
652 if (use->module_which_uses == mod) {
653 DEBUGP("%s unusing %s\n", mod->name, i->name);
654 module_put(i);
655 list_del(&use->list);
656 kfree(use);
657 sysfs_remove_link(i->holders_dir, mod->name);
658 /* There can be at most one match. */
659 break;
665 #ifdef CONFIG_MODULE_FORCE_UNLOAD
666 static inline int try_force_unload(unsigned int flags)
668 int ret = (flags & O_TRUNC);
669 if (ret)
670 add_taint(TAINT_FORCED_RMMOD);
671 return ret;
673 #else
674 static inline int try_force_unload(unsigned int flags)
676 return 0;
678 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
680 struct stopref
682 struct module *mod;
683 int flags;
684 int *forced;
687 /* Whole machine is stopped with interrupts off when this runs. */
688 static int __try_stop_module(void *_sref)
690 struct stopref *sref = _sref;
692 /* If it's not unused, quit unless we're forcing. */
693 if (module_refcount(sref->mod) != 0) {
694 if (!(*sref->forced = try_force_unload(sref->flags)))
695 return -EWOULDBLOCK;
698 /* Mark it as dying. */
699 sref->mod->state = MODULE_STATE_GOING;
700 return 0;
703 static int try_stop_module(struct module *mod, int flags, int *forced)
705 if (flags & O_NONBLOCK) {
706 struct stopref sref = { mod, flags, forced };
708 return stop_machine(__try_stop_module, &sref, NULL);
709 } else {
710 /* We don't need to stop the machine for this. */
711 mod->state = MODULE_STATE_GOING;
712 synchronize_sched();
713 return 0;
717 unsigned int module_refcount(struct module *mod)
719 unsigned int i, total = 0;
721 for (i = 0; i < NR_CPUS; i++)
722 total += local_read(&mod->ref[i].count);
723 return total;
725 EXPORT_SYMBOL(module_refcount);
727 /* This exists whether we can unload or not */
728 static void free_module(struct module *mod);
730 static void wait_for_zero_refcount(struct module *mod)
732 /* Since we might sleep for some time, release the mutex first */
733 mutex_unlock(&module_mutex);
734 for (;;) {
735 DEBUGP("Looking at refcount...\n");
736 set_current_state(TASK_UNINTERRUPTIBLE);
737 if (module_refcount(mod) == 0)
738 break;
739 schedule();
741 current->state = TASK_RUNNING;
742 mutex_lock(&module_mutex);
745 asmlinkage long
746 sys_delete_module(const char __user *name_user, unsigned int flags)
748 struct module *mod;
749 char name[MODULE_NAME_LEN];
750 int ret, forced = 0;
752 if (!capable(CAP_SYS_MODULE))
753 return -EPERM;
755 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
756 return -EFAULT;
757 name[MODULE_NAME_LEN-1] = '\0';
759 /* Create stop_machine threads since free_module relies on
760 * a non-failing stop_machine call. */
761 ret = stop_machine_create();
762 if (ret)
763 return ret;
765 if (mutex_lock_interruptible(&module_mutex) != 0) {
766 ret = -EINTR;
767 goto out_stop;
770 mod = find_module(name);
771 if (!mod) {
772 ret = -ENOENT;
773 goto out;
776 if (!list_empty(&mod->modules_which_use_me)) {
777 /* Other modules depend on us: get rid of them first. */
778 ret = -EWOULDBLOCK;
779 goto out;
782 /* Doing init or already dying? */
783 if (mod->state != MODULE_STATE_LIVE) {
784 /* FIXME: if (force), slam module count and wake up
785 waiter --RR */
786 DEBUGP("%s already dying\n", mod->name);
787 ret = -EBUSY;
788 goto out;
791 /* If it has an init func, it must have an exit func to unload */
792 if (mod->init && !mod->exit) {
793 forced = try_force_unload(flags);
794 if (!forced) {
795 /* This module can't be removed */
796 ret = -EBUSY;
797 goto out;
801 /* Set this up before setting mod->state */
802 mod->waiter = current;
804 /* Stop the machine so refcounts can't move and disable module. */
805 ret = try_stop_module(mod, flags, &forced);
806 if (ret != 0)
807 goto out;
809 /* Never wait if forced. */
810 if (!forced && module_refcount(mod) != 0)
811 wait_for_zero_refcount(mod);
813 mutex_unlock(&module_mutex);
814 /* Final destruction now noone is using it. */
815 if (mod->exit != NULL)
816 mod->exit();
817 blocking_notifier_call_chain(&module_notify_list,
818 MODULE_STATE_GOING, mod);
819 mutex_lock(&module_mutex);
820 /* Store the name of the last unloaded module for diagnostic purposes */
821 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
822 unregister_dynamic_debug_module(mod->name);
823 free_module(mod);
825 out:
826 mutex_unlock(&module_mutex);
827 out_stop:
828 stop_machine_destroy();
829 return ret;
832 static inline void print_unload_info(struct seq_file *m, struct module *mod)
834 struct module_use *use;
835 int printed_something = 0;
837 seq_printf(m, " %u ", module_refcount(mod));
839 /* Always include a trailing , so userspace can differentiate
840 between this and the old multi-field proc format. */
841 list_for_each_entry(use, &mod->modules_which_use_me, list) {
842 printed_something = 1;
843 seq_printf(m, "%s,", use->module_which_uses->name);
846 if (mod->init != NULL && mod->exit == NULL) {
847 printed_something = 1;
848 seq_printf(m, "[permanent],");
851 if (!printed_something)
852 seq_printf(m, "-");
855 void __symbol_put(const char *symbol)
857 struct module *owner;
859 preempt_disable();
860 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false)))
861 BUG();
862 module_put(owner);
863 preempt_enable();
865 EXPORT_SYMBOL(__symbol_put);
867 void symbol_put_addr(void *addr)
869 struct module *modaddr;
871 if (core_kernel_text((unsigned long)addr))
872 return;
874 if (!(modaddr = module_text_address((unsigned long)addr)))
875 BUG();
876 module_put(modaddr);
878 EXPORT_SYMBOL_GPL(symbol_put_addr);
880 static ssize_t show_refcnt(struct module_attribute *mattr,
881 struct module *mod, char *buffer)
883 return sprintf(buffer, "%u\n", module_refcount(mod));
886 static struct module_attribute refcnt = {
887 .attr = { .name = "refcnt", .mode = 0444 },
888 .show = show_refcnt,
891 void module_put(struct module *module)
893 if (module) {
894 unsigned int cpu = get_cpu();
895 local_dec(&module->ref[cpu].count);
896 /* Maybe they're waiting for us to drop reference? */
897 if (unlikely(!module_is_live(module)))
898 wake_up_process(module->waiter);
899 put_cpu();
902 EXPORT_SYMBOL(module_put);
904 #else /* !CONFIG_MODULE_UNLOAD */
905 static inline void print_unload_info(struct seq_file *m, struct module *mod)
907 /* We don't know the usage count, or what modules are using. */
908 seq_printf(m, " - -");
911 static inline void module_unload_free(struct module *mod)
915 static inline int use_module(struct module *a, struct module *b)
917 return strong_try_module_get(b) == 0;
920 static inline void module_unload_init(struct module *mod)
923 #endif /* CONFIG_MODULE_UNLOAD */
925 static ssize_t show_initstate(struct module_attribute *mattr,
926 struct module *mod, char *buffer)
928 const char *state = "unknown";
930 switch (mod->state) {
931 case MODULE_STATE_LIVE:
932 state = "live";
933 break;
934 case MODULE_STATE_COMING:
935 state = "coming";
936 break;
937 case MODULE_STATE_GOING:
938 state = "going";
939 break;
941 return sprintf(buffer, "%s\n", state);
944 static struct module_attribute initstate = {
945 .attr = { .name = "initstate", .mode = 0444 },
946 .show = show_initstate,
949 static struct module_attribute *modinfo_attrs[] = {
950 &modinfo_version,
951 &modinfo_srcversion,
952 &initstate,
953 #ifdef CONFIG_MODULE_UNLOAD
954 &refcnt,
955 #endif
956 NULL,
959 static const char vermagic[] = VERMAGIC_STRING;
961 static int try_to_force_load(struct module *mod, const char *symname)
963 #ifdef CONFIG_MODULE_FORCE_LOAD
964 if (!test_taint(TAINT_FORCED_MODULE))
965 printk("%s: no version for \"%s\" found: kernel tainted.\n",
966 mod->name, symname);
967 add_taint_module(mod, TAINT_FORCED_MODULE);
968 return 0;
969 #else
970 return -ENOEXEC;
971 #endif
974 #ifdef CONFIG_MODVERSIONS
975 static int check_version(Elf_Shdr *sechdrs,
976 unsigned int versindex,
977 const char *symname,
978 struct module *mod,
979 const unsigned long *crc)
981 unsigned int i, num_versions;
982 struct modversion_info *versions;
984 /* Exporting module didn't supply crcs? OK, we're already tainted. */
985 if (!crc)
986 return 1;
988 /* No versions at all? modprobe --force does this. */
989 if (versindex == 0)
990 return try_to_force_load(mod, symname) == 0;
992 versions = (void *) sechdrs[versindex].sh_addr;
993 num_versions = sechdrs[versindex].sh_size
994 / sizeof(struct modversion_info);
996 for (i = 0; i < num_versions; i++) {
997 if (strcmp(versions[i].name, symname) != 0)
998 continue;
1000 if (versions[i].crc == *crc)
1001 return 1;
1002 DEBUGP("Found checksum %lX vs module %lX\n",
1003 *crc, versions[i].crc);
1004 goto bad_version;
1007 printk(KERN_WARNING "%s: no symbol version for %s\n",
1008 mod->name, symname);
1009 return 0;
1011 bad_version:
1012 printk("%s: disagrees about version of symbol %s\n",
1013 mod->name, symname);
1014 return 0;
1017 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1018 unsigned int versindex,
1019 struct module *mod)
1021 const unsigned long *crc;
1023 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false)))
1024 BUG();
1025 return check_version(sechdrs, versindex, "struct_module", mod, crc);
1028 /* First part is kernel version, which we ignore if module has crcs. */
1029 static inline int same_magic(const char *amagic, const char *bmagic,
1030 bool has_crcs)
1032 if (has_crcs) {
1033 amagic += strcspn(amagic, " ");
1034 bmagic += strcspn(bmagic, " ");
1036 return strcmp(amagic, bmagic) == 0;
1038 #else
1039 static inline int check_version(Elf_Shdr *sechdrs,
1040 unsigned int versindex,
1041 const char *symname,
1042 struct module *mod,
1043 const unsigned long *crc)
1045 return 1;
1048 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1049 unsigned int versindex,
1050 struct module *mod)
1052 return 1;
1055 static inline int same_magic(const char *amagic, const char *bmagic,
1056 bool has_crcs)
1058 return strcmp(amagic, bmagic) == 0;
1060 #endif /* CONFIG_MODVERSIONS */
1062 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1063 Must be holding module_mutex. */
1064 static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
1065 unsigned int versindex,
1066 const char *name,
1067 struct module *mod)
1069 struct module *owner;
1070 unsigned long ret;
1071 const unsigned long *crc;
1073 ret = find_symbol(name, &owner, &crc,
1074 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1075 if (!IS_ERR_VALUE(ret)) {
1076 /* use_module can fail due to OOM,
1077 or module initialization or unloading */
1078 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1079 !use_module(mod, owner))
1080 ret = -EINVAL;
1082 return ret;
1086 * /sys/module/foo/sections stuff
1087 * J. Corbet <corbet@lwn.net>
1089 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1090 struct module_sect_attr
1092 struct module_attribute mattr;
1093 char *name;
1094 unsigned long address;
1097 struct module_sect_attrs
1099 struct attribute_group grp;
1100 unsigned int nsections;
1101 struct module_sect_attr attrs[0];
1104 static ssize_t module_sect_show(struct module_attribute *mattr,
1105 struct module *mod, char *buf)
1107 struct module_sect_attr *sattr =
1108 container_of(mattr, struct module_sect_attr, mattr);
1109 return sprintf(buf, "0x%lx\n", sattr->address);
1112 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1114 unsigned int section;
1116 for (section = 0; section < sect_attrs->nsections; section++)
1117 kfree(sect_attrs->attrs[section].name);
1118 kfree(sect_attrs);
1121 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1122 char *secstrings, Elf_Shdr *sechdrs)
1124 unsigned int nloaded = 0, i, size[2];
1125 struct module_sect_attrs *sect_attrs;
1126 struct module_sect_attr *sattr;
1127 struct attribute **gattr;
1129 /* Count loaded sections and allocate structures */
1130 for (i = 0; i < nsect; i++)
1131 if (sechdrs[i].sh_flags & SHF_ALLOC)
1132 nloaded++;
1133 size[0] = ALIGN(sizeof(*sect_attrs)
1134 + nloaded * sizeof(sect_attrs->attrs[0]),
1135 sizeof(sect_attrs->grp.attrs[0]));
1136 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1137 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1138 if (sect_attrs == NULL)
1139 return;
1141 /* Setup section attributes. */
1142 sect_attrs->grp.name = "sections";
1143 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1145 sect_attrs->nsections = 0;
1146 sattr = &sect_attrs->attrs[0];
1147 gattr = &sect_attrs->grp.attrs[0];
1148 for (i = 0; i < nsect; i++) {
1149 if (! (sechdrs[i].sh_flags & SHF_ALLOC))
1150 continue;
1151 sattr->address = sechdrs[i].sh_addr;
1152 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1153 GFP_KERNEL);
1154 if (sattr->name == NULL)
1155 goto out;
1156 sect_attrs->nsections++;
1157 sattr->mattr.show = module_sect_show;
1158 sattr->mattr.store = NULL;
1159 sattr->mattr.attr.name = sattr->name;
1160 sattr->mattr.attr.mode = S_IRUGO;
1161 *(gattr++) = &(sattr++)->mattr.attr;
1163 *gattr = NULL;
1165 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1166 goto out;
1168 mod->sect_attrs = sect_attrs;
1169 return;
1170 out:
1171 free_sect_attrs(sect_attrs);
1174 static void remove_sect_attrs(struct module *mod)
1176 if (mod->sect_attrs) {
1177 sysfs_remove_group(&mod->mkobj.kobj,
1178 &mod->sect_attrs->grp);
1179 /* We are positive that no one is using any sect attrs
1180 * at this point. Deallocate immediately. */
1181 free_sect_attrs(mod->sect_attrs);
1182 mod->sect_attrs = NULL;
1187 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1190 struct module_notes_attrs {
1191 struct kobject *dir;
1192 unsigned int notes;
1193 struct bin_attribute attrs[0];
1196 static ssize_t module_notes_read(struct kobject *kobj,
1197 struct bin_attribute *bin_attr,
1198 char *buf, loff_t pos, size_t count)
1201 * The caller checked the pos and count against our size.
1203 memcpy(buf, bin_attr->private + pos, count);
1204 return count;
1207 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1208 unsigned int i)
1210 if (notes_attrs->dir) {
1211 while (i-- > 0)
1212 sysfs_remove_bin_file(notes_attrs->dir,
1213 &notes_attrs->attrs[i]);
1214 kobject_put(notes_attrs->dir);
1216 kfree(notes_attrs);
1219 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1220 char *secstrings, Elf_Shdr *sechdrs)
1222 unsigned int notes, loaded, i;
1223 struct module_notes_attrs *notes_attrs;
1224 struct bin_attribute *nattr;
1226 /* Count notes sections and allocate structures. */
1227 notes = 0;
1228 for (i = 0; i < nsect; i++)
1229 if ((sechdrs[i].sh_flags & SHF_ALLOC) &&
1230 (sechdrs[i].sh_type == SHT_NOTE))
1231 ++notes;
1233 if (notes == 0)
1234 return;
1236 notes_attrs = kzalloc(sizeof(*notes_attrs)
1237 + notes * sizeof(notes_attrs->attrs[0]),
1238 GFP_KERNEL);
1239 if (notes_attrs == NULL)
1240 return;
1242 notes_attrs->notes = notes;
1243 nattr = &notes_attrs->attrs[0];
1244 for (loaded = i = 0; i < nsect; ++i) {
1245 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1246 continue;
1247 if (sechdrs[i].sh_type == SHT_NOTE) {
1248 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1249 nattr->attr.mode = S_IRUGO;
1250 nattr->size = sechdrs[i].sh_size;
1251 nattr->private = (void *) sechdrs[i].sh_addr;
1252 nattr->read = module_notes_read;
1253 ++nattr;
1255 ++loaded;
1258 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1259 if (!notes_attrs->dir)
1260 goto out;
1262 for (i = 0; i < notes; ++i)
1263 if (sysfs_create_bin_file(notes_attrs->dir,
1264 &notes_attrs->attrs[i]))
1265 goto out;
1267 mod->notes_attrs = notes_attrs;
1268 return;
1270 out:
1271 free_notes_attrs(notes_attrs, i);
1274 static void remove_notes_attrs(struct module *mod)
1276 if (mod->notes_attrs)
1277 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1280 #else
1282 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1283 char *sectstrings, Elf_Shdr *sechdrs)
1287 static inline void remove_sect_attrs(struct module *mod)
1291 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1292 char *sectstrings, Elf_Shdr *sechdrs)
1296 static inline void remove_notes_attrs(struct module *mod)
1299 #endif
1301 #ifdef CONFIG_SYSFS
1302 int module_add_modinfo_attrs(struct module *mod)
1304 struct module_attribute *attr;
1305 struct module_attribute *temp_attr;
1306 int error = 0;
1307 int i;
1309 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1310 (ARRAY_SIZE(modinfo_attrs) + 1)),
1311 GFP_KERNEL);
1312 if (!mod->modinfo_attrs)
1313 return -ENOMEM;
1315 temp_attr = mod->modinfo_attrs;
1316 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1317 if (!attr->test ||
1318 (attr->test && attr->test(mod))) {
1319 memcpy(temp_attr, attr, sizeof(*temp_attr));
1320 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1321 ++temp_attr;
1324 return error;
1327 void module_remove_modinfo_attrs(struct module *mod)
1329 struct module_attribute *attr;
1330 int i;
1332 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1333 /* pick a field to test for end of list */
1334 if (!attr->attr.name)
1335 break;
1336 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1337 if (attr->free)
1338 attr->free(mod);
1340 kfree(mod->modinfo_attrs);
1343 int mod_sysfs_init(struct module *mod)
1345 int err;
1346 struct kobject *kobj;
1348 if (!module_sysfs_initialized) {
1349 printk(KERN_ERR "%s: module sysfs not initialized\n",
1350 mod->name);
1351 err = -EINVAL;
1352 goto out;
1355 kobj = kset_find_obj(module_kset, mod->name);
1356 if (kobj) {
1357 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1358 kobject_put(kobj);
1359 err = -EINVAL;
1360 goto out;
1363 mod->mkobj.mod = mod;
1365 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1366 mod->mkobj.kobj.kset = module_kset;
1367 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1368 "%s", mod->name);
1369 if (err)
1370 kobject_put(&mod->mkobj.kobj);
1372 /* delay uevent until full sysfs population */
1373 out:
1374 return err;
1377 int mod_sysfs_setup(struct module *mod,
1378 struct kernel_param *kparam,
1379 unsigned int num_params)
1381 int err;
1383 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1384 if (!mod->holders_dir) {
1385 err = -ENOMEM;
1386 goto out_unreg;
1389 err = module_param_sysfs_setup(mod, kparam, num_params);
1390 if (err)
1391 goto out_unreg_holders;
1393 err = module_add_modinfo_attrs(mod);
1394 if (err)
1395 goto out_unreg_param;
1397 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1398 return 0;
1400 out_unreg_param:
1401 module_param_sysfs_remove(mod);
1402 out_unreg_holders:
1403 kobject_put(mod->holders_dir);
1404 out_unreg:
1405 kobject_put(&mod->mkobj.kobj);
1406 return err;
1409 static void mod_sysfs_fini(struct module *mod)
1411 kobject_put(&mod->mkobj.kobj);
1414 #else /* CONFIG_SYSFS */
1416 static void mod_sysfs_fini(struct module *mod)
1420 #endif /* CONFIG_SYSFS */
1422 static void mod_kobject_remove(struct module *mod)
1424 module_remove_modinfo_attrs(mod);
1425 module_param_sysfs_remove(mod);
1426 kobject_put(mod->mkobj.drivers_dir);
1427 kobject_put(mod->holders_dir);
1428 mod_sysfs_fini(mod);
1432 * unlink the module with the whole machine is stopped with interrupts off
1433 * - this defends against kallsyms not taking locks
1435 static int __unlink_module(void *_mod)
1437 struct module *mod = _mod;
1438 list_del(&mod->list);
1439 return 0;
1442 /* Free a module, remove from lists, etc (must hold module_mutex). */
1443 static void free_module(struct module *mod)
1445 /* Delete from various lists */
1446 stop_machine(__unlink_module, mod, NULL);
1447 remove_notes_attrs(mod);
1448 remove_sect_attrs(mod);
1449 mod_kobject_remove(mod);
1451 /* Arch-specific cleanup. */
1452 module_arch_cleanup(mod);
1454 /* Module unload stuff */
1455 module_unload_free(mod);
1457 /* release any pointers to mcount in this module */
1458 ftrace_release(mod->module_core, mod->core_size);
1460 /* This may be NULL, but that's OK */
1461 module_free(mod, mod->module_init);
1462 kfree(mod->args);
1463 if (mod->percpu)
1464 percpu_modfree(mod->percpu);
1466 /* Free lock-classes: */
1467 lockdep_free_key_range(mod->module_core, mod->core_size);
1469 /* Finally, free the core (containing the module structure) */
1470 module_free(mod, mod->module_core);
1473 void *__symbol_get(const char *symbol)
1475 struct module *owner;
1476 unsigned long value;
1478 preempt_disable();
1479 value = find_symbol(symbol, &owner, NULL, true, true);
1480 if (IS_ERR_VALUE(value))
1481 value = 0;
1482 else if (strong_try_module_get(owner))
1483 value = 0;
1484 preempt_enable();
1486 return (void *)value;
1488 EXPORT_SYMBOL_GPL(__symbol_get);
1491 * Ensure that an exported symbol [global namespace] does not already exist
1492 * in the kernel or in some other module's exported symbol table.
1494 static int verify_export_symbols(struct module *mod)
1496 unsigned int i;
1497 struct module *owner;
1498 const struct kernel_symbol *s;
1499 struct {
1500 const struct kernel_symbol *sym;
1501 unsigned int num;
1502 } arr[] = {
1503 { mod->syms, mod->num_syms },
1504 { mod->gpl_syms, mod->num_gpl_syms },
1505 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1506 #ifdef CONFIG_UNUSED_SYMBOLS
1507 { mod->unused_syms, mod->num_unused_syms },
1508 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1509 #endif
1512 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1513 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1514 if (!IS_ERR_VALUE(find_symbol(s->name, &owner,
1515 NULL, true, false))) {
1516 printk(KERN_ERR
1517 "%s: exports duplicate symbol %s"
1518 " (owned by %s)\n",
1519 mod->name, s->name, module_name(owner));
1520 return -ENOEXEC;
1524 return 0;
1527 /* Change all symbols so that st_value encodes the pointer directly. */
1528 static int simplify_symbols(Elf_Shdr *sechdrs,
1529 unsigned int symindex,
1530 const char *strtab,
1531 unsigned int versindex,
1532 unsigned int pcpuindex,
1533 struct module *mod)
1535 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1536 unsigned long secbase;
1537 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1538 int ret = 0;
1540 for (i = 1; i < n; i++) {
1541 switch (sym[i].st_shndx) {
1542 case SHN_COMMON:
1543 /* We compiled with -fno-common. These are not
1544 supposed to happen. */
1545 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1546 printk("%s: please compile with -fno-common\n",
1547 mod->name);
1548 ret = -ENOEXEC;
1549 break;
1551 case SHN_ABS:
1552 /* Don't need to do anything */
1553 DEBUGP("Absolute symbol: 0x%08lx\n",
1554 (long)sym[i].st_value);
1555 break;
1557 case SHN_UNDEF:
1558 sym[i].st_value
1559 = resolve_symbol(sechdrs, versindex,
1560 strtab + sym[i].st_name, mod);
1562 /* Ok if resolved. */
1563 if (!IS_ERR_VALUE(sym[i].st_value))
1564 break;
1565 /* Ok if weak. */
1566 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1567 break;
1569 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1570 mod->name, strtab + sym[i].st_name);
1571 ret = -ENOENT;
1572 break;
1574 default:
1575 /* Divert to percpu allocation if a percpu var. */
1576 if (sym[i].st_shndx == pcpuindex)
1577 secbase = (unsigned long)mod->percpu;
1578 else
1579 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1580 sym[i].st_value += secbase;
1581 break;
1585 return ret;
1588 /* Additional bytes needed by arch in front of individual sections */
1589 unsigned int __weak arch_mod_section_prepend(struct module *mod,
1590 unsigned int section)
1592 /* default implementation just returns zero */
1593 return 0;
1596 /* Update size with this section: return offset. */
1597 static long get_offset(struct module *mod, unsigned int *size,
1598 Elf_Shdr *sechdr, unsigned int section)
1600 long ret;
1602 *size += arch_mod_section_prepend(mod, section);
1603 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1604 *size = ret + sechdr->sh_size;
1605 return ret;
1608 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1609 might -- code, read-only data, read-write data, small data. Tally
1610 sizes, and place the offsets into sh_entsize fields: high bit means it
1611 belongs in init. */
1612 static void layout_sections(struct module *mod,
1613 const Elf_Ehdr *hdr,
1614 Elf_Shdr *sechdrs,
1615 const char *secstrings)
1617 static unsigned long const masks[][2] = {
1618 /* NOTE: all executable code must be the first section
1619 * in this array; otherwise modify the text_size
1620 * finder in the two loops below */
1621 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1622 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1623 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1624 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1626 unsigned int m, i;
1628 for (i = 0; i < hdr->e_shnum; i++)
1629 sechdrs[i].sh_entsize = ~0UL;
1631 DEBUGP("Core section allocation order:\n");
1632 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1633 for (i = 0; i < hdr->e_shnum; ++i) {
1634 Elf_Shdr *s = &sechdrs[i];
1636 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1637 || (s->sh_flags & masks[m][1])
1638 || s->sh_entsize != ~0UL
1639 || strncmp(secstrings + s->sh_name,
1640 ".init", 5) == 0)
1641 continue;
1642 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1643 DEBUGP("\t%s\n", secstrings + s->sh_name);
1645 if (m == 0)
1646 mod->core_text_size = mod->core_size;
1649 DEBUGP("Init section allocation order:\n");
1650 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1651 for (i = 0; i < hdr->e_shnum; ++i) {
1652 Elf_Shdr *s = &sechdrs[i];
1654 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1655 || (s->sh_flags & masks[m][1])
1656 || s->sh_entsize != ~0UL
1657 || strncmp(secstrings + s->sh_name,
1658 ".init", 5) != 0)
1659 continue;
1660 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1661 | INIT_OFFSET_MASK);
1662 DEBUGP("\t%s\n", secstrings + s->sh_name);
1664 if (m == 0)
1665 mod->init_text_size = mod->init_size;
1669 static void set_license(struct module *mod, const char *license)
1671 if (!license)
1672 license = "unspecified";
1674 if (!license_is_gpl_compatible(license)) {
1675 if (!test_taint(TAINT_PROPRIETARY_MODULE))
1676 printk(KERN_WARNING "%s: module license '%s' taints "
1677 "kernel.\n", mod->name, license);
1678 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
1682 /* Parse tag=value strings from .modinfo section */
1683 static char *next_string(char *string, unsigned long *secsize)
1685 /* Skip non-zero chars */
1686 while (string[0]) {
1687 string++;
1688 if ((*secsize)-- <= 1)
1689 return NULL;
1692 /* Skip any zero padding. */
1693 while (!string[0]) {
1694 string++;
1695 if ((*secsize)-- <= 1)
1696 return NULL;
1698 return string;
1701 static char *get_modinfo(Elf_Shdr *sechdrs,
1702 unsigned int info,
1703 const char *tag)
1705 char *p;
1706 unsigned int taglen = strlen(tag);
1707 unsigned long size = sechdrs[info].sh_size;
1709 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
1710 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1711 return p + taglen + 1;
1713 return NULL;
1716 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1717 unsigned int infoindex)
1719 struct module_attribute *attr;
1720 int i;
1722 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1723 if (attr->setup)
1724 attr->setup(mod,
1725 get_modinfo(sechdrs,
1726 infoindex,
1727 attr->attr.name));
1731 #ifdef CONFIG_KALLSYMS
1733 /* lookup symbol in given range of kernel_symbols */
1734 static const struct kernel_symbol *lookup_symbol(const char *name,
1735 const struct kernel_symbol *start,
1736 const struct kernel_symbol *stop)
1738 const struct kernel_symbol *ks = start;
1739 for (; ks < stop; ks++)
1740 if (strcmp(ks->name, name) == 0)
1741 return ks;
1742 return NULL;
1745 static int is_exported(const char *name, unsigned long value,
1746 const struct module *mod)
1748 const struct kernel_symbol *ks;
1749 if (!mod)
1750 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
1751 else
1752 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1753 return ks != NULL && ks->value == value;
1756 /* As per nm */
1757 static char elf_type(const Elf_Sym *sym,
1758 Elf_Shdr *sechdrs,
1759 const char *secstrings,
1760 struct module *mod)
1762 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1763 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1764 return 'v';
1765 else
1766 return 'w';
1768 if (sym->st_shndx == SHN_UNDEF)
1769 return 'U';
1770 if (sym->st_shndx == SHN_ABS)
1771 return 'a';
1772 if (sym->st_shndx >= SHN_LORESERVE)
1773 return '?';
1774 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1775 return 't';
1776 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1777 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1778 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1779 return 'r';
1780 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1781 return 'g';
1782 else
1783 return 'd';
1785 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1786 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1787 return 's';
1788 else
1789 return 'b';
1791 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
1792 ".debug", strlen(".debug")) == 0)
1793 return 'n';
1794 return '?';
1797 static void add_kallsyms(struct module *mod,
1798 Elf_Shdr *sechdrs,
1799 unsigned int symindex,
1800 unsigned int strindex,
1801 const char *secstrings)
1803 unsigned int i;
1805 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1806 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1807 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1809 /* Set types up while we still have access to sections. */
1810 for (i = 0; i < mod->num_symtab; i++)
1811 mod->symtab[i].st_info
1812 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1814 #else
1815 static inline void add_kallsyms(struct module *mod,
1816 Elf_Shdr *sechdrs,
1817 unsigned int symindex,
1818 unsigned int strindex,
1819 const char *secstrings)
1822 #endif /* CONFIG_KALLSYMS */
1824 static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num)
1826 #ifdef CONFIG_DYNAMIC_PRINTK_DEBUG
1827 unsigned int i;
1829 for (i = 0; i < num; i++) {
1830 register_dynamic_debug_module(debug[i].modname,
1831 debug[i].type,
1832 debug[i].logical_modname,
1833 debug[i].flag_names,
1834 debug[i].hash, debug[i].hash2);
1836 #endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1839 static void *module_alloc_update_bounds(unsigned long size)
1841 void *ret = module_alloc(size);
1843 if (ret) {
1844 /* Update module bounds. */
1845 if ((unsigned long)ret < module_addr_min)
1846 module_addr_min = (unsigned long)ret;
1847 if ((unsigned long)ret + size > module_addr_max)
1848 module_addr_max = (unsigned long)ret + size;
1850 return ret;
1853 /* Allocate and load the module: note that size of section 0 is always
1854 zero, and we rely on this for optional sections. */
1855 static noinline struct module *load_module(void __user *umod,
1856 unsigned long len,
1857 const char __user *uargs)
1859 Elf_Ehdr *hdr;
1860 Elf_Shdr *sechdrs;
1861 char *secstrings, *args, *modmagic, *strtab = NULL;
1862 char *staging;
1863 unsigned int i;
1864 unsigned int symindex = 0;
1865 unsigned int strindex = 0;
1866 unsigned int modindex, versindex, infoindex, pcpuindex;
1867 unsigned int num_kp, num_mcount;
1868 struct kernel_param *kp;
1869 struct module *mod;
1870 long err = 0;
1871 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
1872 unsigned long *mseg;
1873 mm_segment_t old_fs;
1875 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
1876 umod, len, uargs);
1877 if (len < sizeof(*hdr))
1878 return ERR_PTR(-ENOEXEC);
1880 /* Suck in entire file: we'll want most of it. */
1881 /* vmalloc barfs on "unusual" numbers. Check here */
1882 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1883 return ERR_PTR(-ENOMEM);
1885 /* Create stop_machine threads since the error path relies on
1886 * a non-failing stop_machine call. */
1887 err = stop_machine_create();
1888 if (err)
1889 goto free_hdr;
1891 if (copy_from_user(hdr, umod, len) != 0) {
1892 err = -EFAULT;
1893 goto free_hdr;
1896 /* Sanity checks against insmoding binaries or wrong arch,
1897 weird elf version */
1898 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
1899 || hdr->e_type != ET_REL
1900 || !elf_check_arch(hdr)
1901 || hdr->e_shentsize != sizeof(*sechdrs)) {
1902 err = -ENOEXEC;
1903 goto free_hdr;
1906 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
1907 goto truncated;
1909 /* Convenience variables */
1910 sechdrs = (void *)hdr + hdr->e_shoff;
1911 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1912 sechdrs[0].sh_addr = 0;
1914 for (i = 1; i < hdr->e_shnum; i++) {
1915 if (sechdrs[i].sh_type != SHT_NOBITS
1916 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
1917 goto truncated;
1919 /* Mark all sections sh_addr with their address in the
1920 temporary image. */
1921 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
1923 /* Internal symbols and strings. */
1924 if (sechdrs[i].sh_type == SHT_SYMTAB) {
1925 symindex = i;
1926 strindex = sechdrs[i].sh_link;
1927 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
1929 #ifndef CONFIG_MODULE_UNLOAD
1930 /* Don't load .exit sections */
1931 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
1932 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1933 #endif
1936 modindex = find_sec(hdr, sechdrs, secstrings,
1937 ".gnu.linkonce.this_module");
1938 if (!modindex) {
1939 printk(KERN_WARNING "No module found in object\n");
1940 err = -ENOEXEC;
1941 goto free_hdr;
1943 /* This is temporary: point mod into copy of data. */
1944 mod = (void *)sechdrs[modindex].sh_addr;
1946 if (symindex == 0) {
1947 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
1948 mod->name);
1949 err = -ENOEXEC;
1950 goto free_hdr;
1953 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
1954 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
1955 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
1957 /* Don't keep modinfo and version sections. */
1958 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1959 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
1960 #ifdef CONFIG_KALLSYMS
1961 /* Keep symbol and string tables for decoding later. */
1962 sechdrs[symindex].sh_flags |= SHF_ALLOC;
1963 sechdrs[strindex].sh_flags |= SHF_ALLOC;
1964 #endif
1966 /* Check module struct version now, before we try to use module. */
1967 if (!check_modstruct_version(sechdrs, versindex, mod)) {
1968 err = -ENOEXEC;
1969 goto free_hdr;
1972 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1973 /* This is allowed: modprobe --force will invalidate it. */
1974 if (!modmagic) {
1975 err = try_to_force_load(mod, "magic");
1976 if (err)
1977 goto free_hdr;
1978 } else if (!same_magic(modmagic, vermagic, versindex)) {
1979 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
1980 mod->name, modmagic, vermagic);
1981 err = -ENOEXEC;
1982 goto free_hdr;
1985 staging = get_modinfo(sechdrs, infoindex, "staging");
1986 if (staging) {
1987 add_taint_module(mod, TAINT_CRAP);
1988 printk(KERN_WARNING "%s: module is from the staging directory,"
1989 " the quality is unknown, you have been warned.\n",
1990 mod->name);
1993 /* Now copy in args */
1994 args = strndup_user(uargs, ~0UL >> 1);
1995 if (IS_ERR(args)) {
1996 err = PTR_ERR(args);
1997 goto free_hdr;
2000 if (find_module(mod->name)) {
2001 err = -EEXIST;
2002 goto free_mod;
2005 mod->state = MODULE_STATE_COMING;
2007 /* Allow arches to frob section contents and sizes. */
2008 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2009 if (err < 0)
2010 goto free_mod;
2012 if (pcpuindex) {
2013 /* We have a special allocation for this section. */
2014 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
2015 sechdrs[pcpuindex].sh_addralign,
2016 mod->name);
2017 if (!percpu) {
2018 err = -ENOMEM;
2019 goto free_mod;
2021 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2022 mod->percpu = percpu;
2025 /* Determine total sizes, and put offsets in sh_entsize. For now
2026 this is done generically; there doesn't appear to be any
2027 special cases for the architectures. */
2028 layout_sections(mod, hdr, sechdrs, secstrings);
2030 /* Do the allocs. */
2031 ptr = module_alloc_update_bounds(mod->core_size);
2032 if (!ptr) {
2033 err = -ENOMEM;
2034 goto free_percpu;
2036 memset(ptr, 0, mod->core_size);
2037 mod->module_core = ptr;
2039 ptr = module_alloc_update_bounds(mod->init_size);
2040 if (!ptr && mod->init_size) {
2041 err = -ENOMEM;
2042 goto free_core;
2044 memset(ptr, 0, mod->init_size);
2045 mod->module_init = ptr;
2047 /* Transfer each section which specifies SHF_ALLOC */
2048 DEBUGP("final section addresses:\n");
2049 for (i = 0; i < hdr->e_shnum; i++) {
2050 void *dest;
2052 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2053 continue;
2055 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2056 dest = mod->module_init
2057 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2058 else
2059 dest = mod->module_core + sechdrs[i].sh_entsize;
2061 if (sechdrs[i].sh_type != SHT_NOBITS)
2062 memcpy(dest, (void *)sechdrs[i].sh_addr,
2063 sechdrs[i].sh_size);
2064 /* Update sh_addr to point to copy in image. */
2065 sechdrs[i].sh_addr = (unsigned long)dest;
2066 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2068 /* Module has been moved. */
2069 mod = (void *)sechdrs[modindex].sh_addr;
2071 /* Now we've moved module, initialize linked lists, etc. */
2072 module_unload_init(mod);
2074 /* add kobject, so we can reference it. */
2075 err = mod_sysfs_init(mod);
2076 if (err)
2077 goto free_unload;
2079 /* Set up license info based on the info section */
2080 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2083 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2084 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2085 * using GPL-only symbols it needs.
2087 if (strcmp(mod->name, "ndiswrapper") == 0)
2088 add_taint(TAINT_PROPRIETARY_MODULE);
2090 /* driverloader was caught wrongly pretending to be under GPL */
2091 if (strcmp(mod->name, "driverloader") == 0)
2092 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2094 /* Set up MODINFO_ATTR fields */
2095 setup_modinfo(mod, sechdrs, infoindex);
2097 /* Fix up syms, so that st_value is a pointer to location. */
2098 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2099 mod);
2100 if (err < 0)
2101 goto cleanup;
2103 /* Now we've got everything in the final locations, we can
2104 * find optional sections. */
2105 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp),
2106 &num_kp);
2107 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2108 sizeof(*mod->syms), &mod->num_syms);
2109 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2110 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2111 sizeof(*mod->gpl_syms),
2112 &mod->num_gpl_syms);
2113 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2114 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2115 "__ksymtab_gpl_future",
2116 sizeof(*mod->gpl_future_syms),
2117 &mod->num_gpl_future_syms);
2118 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2119 "__kcrctab_gpl_future");
2121 #ifdef CONFIG_UNUSED_SYMBOLS
2122 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2123 "__ksymtab_unused",
2124 sizeof(*mod->unused_syms),
2125 &mod->num_unused_syms);
2126 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2127 "__kcrctab_unused");
2128 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2129 "__ksymtab_unused_gpl",
2130 sizeof(*mod->unused_gpl_syms),
2131 &mod->num_unused_gpl_syms);
2132 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2133 "__kcrctab_unused_gpl");
2134 #endif
2136 #ifdef CONFIG_MARKERS
2137 mod->markers = section_objs(hdr, sechdrs, secstrings, "__markers",
2138 sizeof(*mod->markers), &mod->num_markers);
2139 #endif
2140 #ifdef CONFIG_TRACEPOINTS
2141 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2142 "__tracepoints",
2143 sizeof(*mod->tracepoints),
2144 &mod->num_tracepoints);
2145 #endif
2147 #ifdef CONFIG_MODVERSIONS
2148 if ((mod->num_syms && !mod->crcs)
2149 || (mod->num_gpl_syms && !mod->gpl_crcs)
2150 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2151 #ifdef CONFIG_UNUSED_SYMBOLS
2152 || (mod->num_unused_syms && !mod->unused_crcs)
2153 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2154 #endif
2156 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name);
2157 err = try_to_force_load(mod, "nocrc");
2158 if (err)
2159 goto cleanup;
2161 #endif
2163 /* Now do relocations. */
2164 for (i = 1; i < hdr->e_shnum; i++) {
2165 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2166 unsigned int info = sechdrs[i].sh_info;
2168 /* Not a valid relocation section? */
2169 if (info >= hdr->e_shnum)
2170 continue;
2172 /* Don't bother with non-allocated sections */
2173 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2174 continue;
2176 if (sechdrs[i].sh_type == SHT_REL)
2177 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2178 else if (sechdrs[i].sh_type == SHT_RELA)
2179 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2180 mod);
2181 if (err < 0)
2182 goto cleanup;
2185 /* Find duplicate symbols */
2186 err = verify_export_symbols(mod);
2187 if (err < 0)
2188 goto cleanup;
2190 /* Set up and sort exception table */
2191 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2192 sizeof(*mod->extable), &mod->num_exentries);
2193 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2195 /* Finally, copy percpu area over. */
2196 percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
2197 sechdrs[pcpuindex].sh_size);
2199 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2201 if (!mod->taints) {
2202 struct mod_debug *debug;
2203 unsigned int num_debug;
2205 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2206 sizeof(*debug), &num_debug);
2207 dynamic_printk_setup(debug, num_debug);
2210 /* sechdrs[0].sh_size is always zero */
2211 mseg = section_objs(hdr, sechdrs, secstrings, "__mcount_loc",
2212 sizeof(*mseg), &num_mcount);
2213 ftrace_init_module(mod, mseg, mseg + num_mcount);
2215 err = module_finalize(hdr, sechdrs, mod);
2216 if (err < 0)
2217 goto cleanup;
2219 /* flush the icache in correct context */
2220 old_fs = get_fs();
2221 set_fs(KERNEL_DS);
2224 * Flush the instruction cache, since we've played with text.
2225 * Do it before processing of module parameters, so the module
2226 * can provide parameter accessor functions of its own.
2228 if (mod->module_init)
2229 flush_icache_range((unsigned long)mod->module_init,
2230 (unsigned long)mod->module_init
2231 + mod->init_size);
2232 flush_icache_range((unsigned long)mod->module_core,
2233 (unsigned long)mod->module_core + mod->core_size);
2235 set_fs(old_fs);
2237 mod->args = args;
2238 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2239 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2240 mod->name);
2242 /* Now sew it into the lists so we can get lockdep and oops
2243 * info during argument parsing. Noone should access us, since
2244 * strong_try_module_get() will fail.
2245 * lockdep/oops can run asynchronous, so use the RCU list insertion
2246 * function to insert in a way safe to concurrent readers.
2247 * The mutex protects against concurrent writers.
2249 list_add_rcu(&mod->list, &modules);
2251 err = parse_args(mod->name, mod->args, kp, num_kp, NULL);
2252 if (err < 0)
2253 goto unlink;
2255 err = mod_sysfs_setup(mod, kp, num_kp);
2256 if (err < 0)
2257 goto unlink;
2258 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2259 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2261 /* Get rid of temporary copy */
2262 vfree(hdr);
2264 stop_machine_destroy();
2265 /* Done! */
2266 return mod;
2268 unlink:
2269 stop_machine(__unlink_module, mod, NULL);
2270 module_arch_cleanup(mod);
2271 cleanup:
2272 kobject_del(&mod->mkobj.kobj);
2273 kobject_put(&mod->mkobj.kobj);
2274 ftrace_release(mod->module_core, mod->core_size);
2275 free_unload:
2276 module_unload_free(mod);
2277 module_free(mod, mod->module_init);
2278 free_core:
2279 module_free(mod, mod->module_core);
2280 free_percpu:
2281 if (percpu)
2282 percpu_modfree(percpu);
2283 free_mod:
2284 kfree(args);
2285 free_hdr:
2286 vfree(hdr);
2287 stop_machine_destroy();
2288 return ERR_PTR(err);
2290 truncated:
2291 printk(KERN_ERR "Module len %lu truncated\n", len);
2292 err = -ENOEXEC;
2293 goto free_hdr;
2296 /* This is where the real work happens */
2297 asmlinkage long
2298 sys_init_module(void __user *umod,
2299 unsigned long len,
2300 const char __user *uargs)
2302 struct module *mod;
2303 int ret = 0;
2305 /* Must have permission */
2306 if (!capable(CAP_SYS_MODULE))
2307 return -EPERM;
2309 /* Only one module load at a time, please */
2310 if (mutex_lock_interruptible(&module_mutex) != 0)
2311 return -EINTR;
2313 /* Do all the hard work */
2314 mod = load_module(umod, len, uargs);
2315 if (IS_ERR(mod)) {
2316 mutex_unlock(&module_mutex);
2317 return PTR_ERR(mod);
2320 /* Drop lock so they can recurse */
2321 mutex_unlock(&module_mutex);
2323 blocking_notifier_call_chain(&module_notify_list,
2324 MODULE_STATE_COMING, mod);
2326 /* Start the module */
2327 if (mod->init != NULL)
2328 ret = do_one_initcall(mod->init);
2329 if (ret < 0) {
2330 /* Init routine failed: abort. Try to protect us from
2331 buggy refcounters. */
2332 mod->state = MODULE_STATE_GOING;
2333 synchronize_sched();
2334 module_put(mod);
2335 blocking_notifier_call_chain(&module_notify_list,
2336 MODULE_STATE_GOING, mod);
2337 mutex_lock(&module_mutex);
2338 free_module(mod);
2339 mutex_unlock(&module_mutex);
2340 wake_up(&module_wq);
2341 return ret;
2343 if (ret > 0) {
2344 printk(KERN_WARNING "%s: '%s'->init suspiciously returned %d, "
2345 "it should follow 0/-E convention\n"
2346 KERN_WARNING "%s: loading module anyway...\n",
2347 __func__, mod->name, ret,
2348 __func__);
2349 dump_stack();
2352 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2353 mod->state = MODULE_STATE_LIVE;
2354 wake_up(&module_wq);
2355 blocking_notifier_call_chain(&module_notify_list,
2356 MODULE_STATE_LIVE, mod);
2358 mutex_lock(&module_mutex);
2359 /* Drop initial reference. */
2360 module_put(mod);
2361 module_free(mod, mod->module_init);
2362 mod->module_init = NULL;
2363 mod->init_size = 0;
2364 mod->init_text_size = 0;
2365 mutex_unlock(&module_mutex);
2367 return 0;
2370 static inline int within(unsigned long addr, void *start, unsigned long size)
2372 return ((void *)addr >= start && (void *)addr < start + size);
2375 #ifdef CONFIG_KALLSYMS
2377 * This ignores the intensely annoying "mapping symbols" found
2378 * in ARM ELF files: $a, $t and $d.
2380 static inline int is_arm_mapping_symbol(const char *str)
2382 return str[0] == '$' && strchr("atd", str[1])
2383 && (str[2] == '\0' || str[2] == '.');
2386 static const char *get_ksymbol(struct module *mod,
2387 unsigned long addr,
2388 unsigned long *size,
2389 unsigned long *offset)
2391 unsigned int i, best = 0;
2392 unsigned long nextval;
2394 /* At worse, next value is at end of module */
2395 if (within_module_init(addr, mod))
2396 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2397 else
2398 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2400 /* Scan for closest preceeding symbol, and next symbol. (ELF
2401 starts real symbols at 1). */
2402 for (i = 1; i < mod->num_symtab; i++) {
2403 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2404 continue;
2406 /* We ignore unnamed symbols: they're uninformative
2407 * and inserted at a whim. */
2408 if (mod->symtab[i].st_value <= addr
2409 && mod->symtab[i].st_value > mod->symtab[best].st_value
2410 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2411 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2412 best = i;
2413 if (mod->symtab[i].st_value > addr
2414 && mod->symtab[i].st_value < nextval
2415 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2416 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2417 nextval = mod->symtab[i].st_value;
2420 if (!best)
2421 return NULL;
2423 if (size)
2424 *size = nextval - mod->symtab[best].st_value;
2425 if (offset)
2426 *offset = addr - mod->symtab[best].st_value;
2427 return mod->strtab + mod->symtab[best].st_name;
2430 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2431 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2432 const char *module_address_lookup(unsigned long addr,
2433 unsigned long *size,
2434 unsigned long *offset,
2435 char **modname,
2436 char *namebuf)
2438 struct module *mod;
2439 const char *ret = NULL;
2441 preempt_disable();
2442 list_for_each_entry_rcu(mod, &modules, list) {
2443 if (within_module_init(addr, mod) ||
2444 within_module_core(addr, mod)) {
2445 if (modname)
2446 *modname = mod->name;
2447 ret = get_ksymbol(mod, addr, size, offset);
2448 break;
2451 /* Make a copy in here where it's safe */
2452 if (ret) {
2453 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2454 ret = namebuf;
2456 preempt_enable();
2457 return ret;
2460 int lookup_module_symbol_name(unsigned long addr, char *symname)
2462 struct module *mod;
2464 preempt_disable();
2465 list_for_each_entry_rcu(mod, &modules, list) {
2466 if (within_module_init(addr, mod) ||
2467 within_module_core(addr, mod)) {
2468 const char *sym;
2470 sym = get_ksymbol(mod, addr, NULL, NULL);
2471 if (!sym)
2472 goto out;
2473 strlcpy(symname, sym, KSYM_NAME_LEN);
2474 preempt_enable();
2475 return 0;
2478 out:
2479 preempt_enable();
2480 return -ERANGE;
2483 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2484 unsigned long *offset, char *modname, char *name)
2486 struct module *mod;
2488 preempt_disable();
2489 list_for_each_entry_rcu(mod, &modules, list) {
2490 if (within_module_init(addr, mod) ||
2491 within_module_core(addr, mod)) {
2492 const char *sym;
2494 sym = get_ksymbol(mod, addr, size, offset);
2495 if (!sym)
2496 goto out;
2497 if (modname)
2498 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2499 if (name)
2500 strlcpy(name, sym, KSYM_NAME_LEN);
2501 preempt_enable();
2502 return 0;
2505 out:
2506 preempt_enable();
2507 return -ERANGE;
2510 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2511 char *name, char *module_name, int *exported)
2513 struct module *mod;
2515 preempt_disable();
2516 list_for_each_entry_rcu(mod, &modules, list) {
2517 if (symnum < mod->num_symtab) {
2518 *value = mod->symtab[symnum].st_value;
2519 *type = mod->symtab[symnum].st_info;
2520 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2521 KSYM_NAME_LEN);
2522 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2523 *exported = is_exported(name, *value, mod);
2524 preempt_enable();
2525 return 0;
2527 symnum -= mod->num_symtab;
2529 preempt_enable();
2530 return -ERANGE;
2533 static unsigned long mod_find_symname(struct module *mod, const char *name)
2535 unsigned int i;
2537 for (i = 0; i < mod->num_symtab; i++)
2538 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2539 mod->symtab[i].st_info != 'U')
2540 return mod->symtab[i].st_value;
2541 return 0;
2544 /* Look for this name: can be of form module:name. */
2545 unsigned long module_kallsyms_lookup_name(const char *name)
2547 struct module *mod;
2548 char *colon;
2549 unsigned long ret = 0;
2551 /* Don't lock: we're in enough trouble already. */
2552 preempt_disable();
2553 if ((colon = strchr(name, ':')) != NULL) {
2554 *colon = '\0';
2555 if ((mod = find_module(name)) != NULL)
2556 ret = mod_find_symname(mod, colon+1);
2557 *colon = ':';
2558 } else {
2559 list_for_each_entry_rcu(mod, &modules, list)
2560 if ((ret = mod_find_symname(mod, name)) != 0)
2561 break;
2563 preempt_enable();
2564 return ret;
2566 #endif /* CONFIG_KALLSYMS */
2568 static char *module_flags(struct module *mod, char *buf)
2570 int bx = 0;
2572 if (mod->taints ||
2573 mod->state == MODULE_STATE_GOING ||
2574 mod->state == MODULE_STATE_COMING) {
2575 buf[bx++] = '(';
2576 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
2577 buf[bx++] = 'P';
2578 if (mod->taints & (1 << TAINT_FORCED_MODULE))
2579 buf[bx++] = 'F';
2580 if (mod->taints & (1 << TAINT_CRAP))
2581 buf[bx++] = 'C';
2583 * TAINT_FORCED_RMMOD: could be added.
2584 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2585 * apply to modules.
2588 /* Show a - for module-is-being-unloaded */
2589 if (mod->state == MODULE_STATE_GOING)
2590 buf[bx++] = '-';
2591 /* Show a + for module-is-being-loaded */
2592 if (mod->state == MODULE_STATE_COMING)
2593 buf[bx++] = '+';
2594 buf[bx++] = ')';
2596 buf[bx] = '\0';
2598 return buf;
2601 #ifdef CONFIG_PROC_FS
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 int m_show(struct seq_file *m, void *p)
2621 struct module *mod = list_entry(p, struct module, list);
2622 char buf[8];
2624 seq_printf(m, "%s %u",
2625 mod->name, mod->init_size + mod->core_size);
2626 print_unload_info(m, mod);
2628 /* Informative for users. */
2629 seq_printf(m, " %s",
2630 mod->state == MODULE_STATE_GOING ? "Unloading":
2631 mod->state == MODULE_STATE_COMING ? "Loading":
2632 "Live");
2633 /* Used by oprofile and other similar tools. */
2634 seq_printf(m, " 0x%p", mod->module_core);
2636 /* Taints info */
2637 if (mod->taints)
2638 seq_printf(m, " %s", module_flags(mod, buf));
2640 seq_printf(m, "\n");
2641 return 0;
2644 /* Format: modulename size refcount deps address
2646 Where refcount is a number or -, and deps is a comma-separated list
2647 of depends or -.
2649 static const struct seq_operations modules_op = {
2650 .start = m_start,
2651 .next = m_next,
2652 .stop = m_stop,
2653 .show = m_show
2656 static int modules_open(struct inode *inode, struct file *file)
2658 return seq_open(file, &modules_op);
2661 static const struct file_operations proc_modules_operations = {
2662 .open = modules_open,
2663 .read = seq_read,
2664 .llseek = seq_lseek,
2665 .release = seq_release,
2668 static int __init proc_modules_init(void)
2670 proc_create("modules", 0, NULL, &proc_modules_operations);
2671 return 0;
2673 module_init(proc_modules_init);
2674 #endif
2676 /* Given an address, look for it in the module exception tables. */
2677 const struct exception_table_entry *search_module_extables(unsigned long addr)
2679 const struct exception_table_entry *e = NULL;
2680 struct module *mod;
2682 preempt_disable();
2683 list_for_each_entry_rcu(mod, &modules, list) {
2684 if (mod->num_exentries == 0)
2685 continue;
2687 e = search_extable(mod->extable,
2688 mod->extable + mod->num_exentries - 1,
2689 addr);
2690 if (e)
2691 break;
2693 preempt_enable();
2695 /* Now, if we found one, we are running inside it now, hence
2696 we cannot unload the module, hence no refcnt needed. */
2697 return e;
2701 * Is this a valid module address?
2703 int is_module_address(unsigned long addr)
2705 struct module *mod;
2707 preempt_disable();
2709 list_for_each_entry_rcu(mod, &modules, list) {
2710 if (within_module_core(addr, mod)) {
2711 preempt_enable();
2712 return 1;
2716 preempt_enable();
2718 return 0;
2722 /* Is this a valid kernel address? */
2723 __notrace_funcgraph struct module *__module_text_address(unsigned long addr)
2725 struct module *mod;
2727 if (addr < module_addr_min || addr > module_addr_max)
2728 return NULL;
2730 list_for_each_entry_rcu(mod, &modules, list)
2731 if (within(addr, mod->module_init, mod->init_text_size)
2732 || within(addr, mod->module_core, mod->core_text_size))
2733 return mod;
2734 return NULL;
2737 struct module *module_text_address(unsigned long addr)
2739 struct module *mod;
2741 preempt_disable();
2742 mod = __module_text_address(addr);
2743 preempt_enable();
2745 return mod;
2748 /* Don't grab lock, we're oopsing. */
2749 void print_modules(void)
2751 struct module *mod;
2752 char buf[8];
2754 printk("Modules linked in:");
2755 /* Most callers should already have preempt disabled, but make sure */
2756 preempt_disable();
2757 list_for_each_entry_rcu(mod, &modules, list)
2758 printk(" %s%s", mod->name, module_flags(mod, buf));
2759 preempt_enable();
2760 if (last_unloaded_module[0])
2761 printk(" [last unloaded: %s]", last_unloaded_module);
2762 printk("\n");
2765 #ifdef CONFIG_MODVERSIONS
2766 /* Generate the signature for struct module here, too, for modversions. */
2767 void struct_module(struct module *mod) { return; }
2768 EXPORT_SYMBOL(struct_module);
2769 #endif
2771 #ifdef CONFIG_MARKERS
2772 void module_update_markers(void)
2774 struct module *mod;
2776 mutex_lock(&module_mutex);
2777 list_for_each_entry(mod, &modules, list)
2778 if (!mod->taints)
2779 marker_update_probe_range(mod->markers,
2780 mod->markers + mod->num_markers);
2781 mutex_unlock(&module_mutex);
2783 #endif
2785 #ifdef CONFIG_TRACEPOINTS
2786 void module_update_tracepoints(void)
2788 struct module *mod;
2790 mutex_lock(&module_mutex);
2791 list_for_each_entry(mod, &modules, list)
2792 if (!mod->taints)
2793 tracepoint_update_probe_range(mod->tracepoints,
2794 mod->tracepoints + mod->num_tracepoints);
2795 mutex_unlock(&module_mutex);
2799 * Returns 0 if current not found.
2800 * Returns 1 if current found.
2802 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
2804 struct module *iter_mod;
2805 int found = 0;
2807 mutex_lock(&module_mutex);
2808 list_for_each_entry(iter_mod, &modules, list) {
2809 if (!iter_mod->taints) {
2811 * Sorted module list
2813 if (iter_mod < iter->module)
2814 continue;
2815 else if (iter_mod > iter->module)
2816 iter->tracepoint = NULL;
2817 found = tracepoint_get_iter_range(&iter->tracepoint,
2818 iter_mod->tracepoints,
2819 iter_mod->tracepoints
2820 + iter_mod->num_tracepoints);
2821 if (found) {
2822 iter->module = iter_mod;
2823 break;
2827 mutex_unlock(&module_mutex);
2828 return found;
2830 #endif