Merge branch 'docs-next' of git://git.lwn.net/linux-2.6
[deliverable/linux.git] / kernel / module.c
1 /*
2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
4
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.
9
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.
14
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
18 */
19 #include <linux/module.h>
20 #include <linux/moduleloader.h>
21 #include <linux/ftrace_event.h>
22 #include <linux/init.h>
23 #include <linux/kallsyms.h>
24 #include <linux/fs.h>
25 #include <linux/sysfs.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/elf.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/syscalls.h>
33 #include <linux/fcntl.h>
34 #include <linux/rcupdate.h>
35 #include <linux/capability.h>
36 #include <linux/cpu.h>
37 #include <linux/moduleparam.h>
38 #include <linux/errno.h>
39 #include <linux/err.h>
40 #include <linux/vermagic.h>
41 #include <linux/notifier.h>
42 #include <linux/sched.h>
43 #include <linux/stop_machine.h>
44 #include <linux/device.h>
45 #include <linux/string.h>
46 #include <linux/mutex.h>
47 #include <linux/rculist.h>
48 #include <asm/uaccess.h>
49 #include <asm/cacheflush.h>
50 #include <asm/mmu_context.h>
51 #include <linux/license.h>
52 #include <asm/sections.h>
53 #include <linux/tracepoint.h>
54 #include <linux/ftrace.h>
55 #include <linux/async.h>
56 #include <linux/percpu.h>
57 #include <linux/kmemleak.h>
58
59 #define CREATE_TRACE_POINTS
60 #include <trace/events/module.h>
61
62 #if 0
63 #define DEBUGP printk
64 #else
65 #define DEBUGP(fmt , a...)
66 #endif
67
68 #ifndef ARCH_SHF_SMALL
69 #define ARCH_SHF_SMALL 0
70 #endif
71
72 /* If this is set, the section belongs in the init part of the module */
73 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
74
75 /* List of modules, protected by module_mutex or preempt_disable
76 * (delete uses stop_machine/add uses RCU list operations). */
77 DEFINE_MUTEX(module_mutex);
78 EXPORT_SYMBOL_GPL(module_mutex);
79 static LIST_HEAD(modules);
80
81 /* Block module loading/unloading? */
82 int modules_disabled = 0;
83
84 /* Waiting for a module to finish initializing? */
85 static DECLARE_WAIT_QUEUE_HEAD(module_wq);
86
87 static BLOCKING_NOTIFIER_HEAD(module_notify_list);
88
89 /* Bounds of module allocation, for speeding __module_address */
90 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
91
92 int register_module_notifier(struct notifier_block * nb)
93 {
94 return blocking_notifier_chain_register(&module_notify_list, nb);
95 }
96 EXPORT_SYMBOL(register_module_notifier);
97
98 int unregister_module_notifier(struct notifier_block * nb)
99 {
100 return blocking_notifier_chain_unregister(&module_notify_list, nb);
101 }
102 EXPORT_SYMBOL(unregister_module_notifier);
103
104 /* We require a truly strong try_module_get(): 0 means failure due to
105 ongoing or failed initialization etc. */
106 static inline int strong_try_module_get(struct module *mod)
107 {
108 if (mod && mod->state == MODULE_STATE_COMING)
109 return -EBUSY;
110 if (try_module_get(mod))
111 return 0;
112 else
113 return -ENOENT;
114 }
115
116 static inline void add_taint_module(struct module *mod, unsigned flag)
117 {
118 add_taint(flag);
119 mod->taints |= (1U << flag);
120 }
121
122 /*
123 * A thread that wants to hold a reference to a module only while it
124 * is running can call this to safely exit. nfsd and lockd use this.
125 */
126 void __module_put_and_exit(struct module *mod, long code)
127 {
128 module_put(mod);
129 do_exit(code);
130 }
131 EXPORT_SYMBOL(__module_put_and_exit);
132
133 /* Find a module section: 0 means not found. */
134 static unsigned int find_sec(Elf_Ehdr *hdr,
135 Elf_Shdr *sechdrs,
136 const char *secstrings,
137 const char *name)
138 {
139 unsigned int i;
140
141 for (i = 1; i < hdr->e_shnum; i++)
142 /* Alloc bit cleared means "ignore it." */
143 if ((sechdrs[i].sh_flags & SHF_ALLOC)
144 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
145 return i;
146 return 0;
147 }
148
149 /* Find a module section, or NULL. */
150 static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs,
151 const char *secstrings, const char *name)
152 {
153 /* Section 0 has sh_addr 0. */
154 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr;
155 }
156
157 /* Find a module section, or NULL. Fill in number of "objects" in section. */
158 static void *section_objs(Elf_Ehdr *hdr,
159 Elf_Shdr *sechdrs,
160 const char *secstrings,
161 const char *name,
162 size_t object_size,
163 unsigned int *num)
164 {
165 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name);
166
167 /* Section 0 has sh_addr 0 and sh_size 0. */
168 *num = sechdrs[sec].sh_size / object_size;
169 return (void *)sechdrs[sec].sh_addr;
170 }
171
172 /* Provided by the linker */
173 extern const struct kernel_symbol __start___ksymtab[];
174 extern const struct kernel_symbol __stop___ksymtab[];
175 extern const struct kernel_symbol __start___ksymtab_gpl[];
176 extern const struct kernel_symbol __stop___ksymtab_gpl[];
177 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
178 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
179 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
180 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
181 extern const unsigned long __start___kcrctab[];
182 extern const unsigned long __start___kcrctab_gpl[];
183 extern const unsigned long __start___kcrctab_gpl_future[];
184 #ifdef CONFIG_UNUSED_SYMBOLS
185 extern const struct kernel_symbol __start___ksymtab_unused[];
186 extern const struct kernel_symbol __stop___ksymtab_unused[];
187 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
188 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
189 extern const unsigned long __start___kcrctab_unused[];
190 extern const unsigned long __start___kcrctab_unused_gpl[];
191 #endif
192
193 #ifndef CONFIG_MODVERSIONS
194 #define symversion(base, idx) NULL
195 #else
196 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
197 #endif
198
199 static bool each_symbol_in_section(const struct symsearch *arr,
200 unsigned int arrsize,
201 struct module *owner,
202 bool (*fn)(const struct symsearch *syms,
203 struct module *owner,
204 unsigned int symnum, void *data),
205 void *data)
206 {
207 unsigned int i, j;
208
209 for (j = 0; j < arrsize; j++) {
210 for (i = 0; i < arr[j].stop - arr[j].start; i++)
211 if (fn(&arr[j], owner, i, data))
212 return true;
213 }
214
215 return false;
216 }
217
218 /* Returns true as soon as fn returns true, otherwise false. */
219 bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
220 unsigned int symnum, void *data), void *data)
221 {
222 struct module *mod;
223 const struct symsearch arr[] = {
224 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
225 NOT_GPL_ONLY, false },
226 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
227 __start___kcrctab_gpl,
228 GPL_ONLY, false },
229 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
230 __start___kcrctab_gpl_future,
231 WILL_BE_GPL_ONLY, false },
232 #ifdef CONFIG_UNUSED_SYMBOLS
233 { __start___ksymtab_unused, __stop___ksymtab_unused,
234 __start___kcrctab_unused,
235 NOT_GPL_ONLY, true },
236 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
237 __start___kcrctab_unused_gpl,
238 GPL_ONLY, true },
239 #endif
240 };
241
242 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
243 return true;
244
245 list_for_each_entry_rcu(mod, &modules, list) {
246 struct symsearch arr[] = {
247 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
248 NOT_GPL_ONLY, false },
249 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
250 mod->gpl_crcs,
251 GPL_ONLY, false },
252 { mod->gpl_future_syms,
253 mod->gpl_future_syms + mod->num_gpl_future_syms,
254 mod->gpl_future_crcs,
255 WILL_BE_GPL_ONLY, false },
256 #ifdef CONFIG_UNUSED_SYMBOLS
257 { mod->unused_syms,
258 mod->unused_syms + mod->num_unused_syms,
259 mod->unused_crcs,
260 NOT_GPL_ONLY, true },
261 { mod->unused_gpl_syms,
262 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
263 mod->unused_gpl_crcs,
264 GPL_ONLY, true },
265 #endif
266 };
267
268 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), mod, fn, data))
269 return true;
270 }
271 return false;
272 }
273 EXPORT_SYMBOL_GPL(each_symbol);
274
275 struct find_symbol_arg {
276 /* Input */
277 const char *name;
278 bool gplok;
279 bool warn;
280
281 /* Output */
282 struct module *owner;
283 const unsigned long *crc;
284 const struct kernel_symbol *sym;
285 };
286
287 static bool find_symbol_in_section(const struct symsearch *syms,
288 struct module *owner,
289 unsigned int symnum, void *data)
290 {
291 struct find_symbol_arg *fsa = data;
292
293 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
294 return false;
295
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");
306 }
307 }
308
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");
320 }
321 #endif
322
323 fsa->owner = owner;
324 fsa->crc = symversion(syms->crcs, symnum);
325 fsa->sym = &syms->start[symnum];
326 return true;
327 }
328
329 /* Find a symbol and return it, along with, (optional) crc and
330 * (optional) module which owns it */
331 const struct kernel_symbol *find_symbol(const char *name,
332 struct module **owner,
333 const unsigned long **crc,
334 bool gplok,
335 bool warn)
336 {
337 struct find_symbol_arg fsa;
338
339 fsa.name = name;
340 fsa.gplok = gplok;
341 fsa.warn = warn;
342
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.sym;
349 }
350
351 DEBUGP("Failed to find symbol %s\n", name);
352 return NULL;
353 }
354 EXPORT_SYMBOL_GPL(find_symbol);
355
356 /* Search for module by name: must hold module_mutex. */
357 struct module *find_module(const char *name)
358 {
359 struct module *mod;
360
361 list_for_each_entry(mod, &modules, list) {
362 if (strcmp(mod->name, name) == 0)
363 return mod;
364 }
365 return NULL;
366 }
367 EXPORT_SYMBOL_GPL(find_module);
368
369 #ifdef CONFIG_SMP
370
371 static inline void __percpu *mod_percpu(struct module *mod)
372 {
373 return mod->percpu;
374 }
375
376 static int percpu_modalloc(struct module *mod,
377 unsigned long size, unsigned long align)
378 {
379 if (align > PAGE_SIZE) {
380 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
381 mod->name, align, PAGE_SIZE);
382 align = PAGE_SIZE;
383 }
384
385 mod->percpu = __alloc_reserved_percpu(size, align);
386 if (!mod->percpu) {
387 printk(KERN_WARNING
388 "Could not allocate %lu bytes percpu data\n", size);
389 return -ENOMEM;
390 }
391 mod->percpu_size = size;
392 return 0;
393 }
394
395 static void percpu_modfree(struct module *mod)
396 {
397 free_percpu(mod->percpu);
398 }
399
400 static unsigned int find_pcpusec(Elf_Ehdr *hdr,
401 Elf_Shdr *sechdrs,
402 const char *secstrings)
403 {
404 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
405 }
406
407 static void percpu_modcopy(struct module *mod,
408 const void *from, unsigned long size)
409 {
410 int cpu;
411
412 for_each_possible_cpu(cpu)
413 memcpy(per_cpu_ptr(mod->percpu, cpu), from, size);
414 }
415
416 /**
417 * is_module_percpu_address - test whether address is from module static percpu
418 * @addr: address to test
419 *
420 * Test whether @addr belongs to module static percpu area.
421 *
422 * RETURNS:
423 * %true if @addr is from module static percpu area
424 */
425 bool is_module_percpu_address(unsigned long addr)
426 {
427 struct module *mod;
428 unsigned int cpu;
429
430 preempt_disable();
431
432 list_for_each_entry_rcu(mod, &modules, list) {
433 if (!mod->percpu_size)
434 continue;
435 for_each_possible_cpu(cpu) {
436 void *start = per_cpu_ptr(mod->percpu, cpu);
437
438 if ((void *)addr >= start &&
439 (void *)addr < start + mod->percpu_size) {
440 preempt_enable();
441 return true;
442 }
443 }
444 }
445
446 preempt_enable();
447 return false;
448 }
449
450 #else /* ... !CONFIG_SMP */
451
452 static inline void __percpu *mod_percpu(struct module *mod)
453 {
454 return NULL;
455 }
456 static inline int percpu_modalloc(struct module *mod,
457 unsigned long size, unsigned long align)
458 {
459 return -ENOMEM;
460 }
461 static inline void percpu_modfree(struct module *mod)
462 {
463 }
464 static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
465 Elf_Shdr *sechdrs,
466 const char *secstrings)
467 {
468 return 0;
469 }
470 static inline void percpu_modcopy(struct module *mod,
471 const void *from, unsigned long size)
472 {
473 /* pcpusec should be 0, and size of that section should be 0. */
474 BUG_ON(size != 0);
475 }
476 bool is_module_percpu_address(unsigned long addr)
477 {
478 return false;
479 }
480
481 #endif /* CONFIG_SMP */
482
483 #define MODINFO_ATTR(field) \
484 static void setup_modinfo_##field(struct module *mod, const char *s) \
485 { \
486 mod->field = kstrdup(s, GFP_KERNEL); \
487 } \
488 static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
489 struct module *mod, char *buffer) \
490 { \
491 return sprintf(buffer, "%s\n", mod->field); \
492 } \
493 static int modinfo_##field##_exists(struct module *mod) \
494 { \
495 return mod->field != NULL; \
496 } \
497 static void free_modinfo_##field(struct module *mod) \
498 { \
499 kfree(mod->field); \
500 mod->field = NULL; \
501 } \
502 static struct module_attribute modinfo_##field = { \
503 .attr = { .name = __stringify(field), .mode = 0444 }, \
504 .show = show_modinfo_##field, \
505 .setup = setup_modinfo_##field, \
506 .test = modinfo_##field##_exists, \
507 .free = free_modinfo_##field, \
508 };
509
510 MODINFO_ATTR(version);
511 MODINFO_ATTR(srcversion);
512
513 static char last_unloaded_module[MODULE_NAME_LEN+1];
514
515 #ifdef CONFIG_MODULE_UNLOAD
516
517 EXPORT_TRACEPOINT_SYMBOL(module_get);
518
519 /* Init the unload section of the module. */
520 static void module_unload_init(struct module *mod)
521 {
522 int cpu;
523
524 INIT_LIST_HEAD(&mod->modules_which_use_me);
525 for_each_possible_cpu(cpu) {
526 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
527 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
528 }
529
530 /* Hold reference count during initialization. */
531 __this_cpu_write(mod->refptr->incs, 1);
532 /* Backwards compatibility macros put refcount during init. */
533 mod->waiter = current;
534 }
535
536 /* modules using other modules */
537 struct module_use
538 {
539 struct list_head list;
540 struct module *module_which_uses;
541 };
542
543 /* Does a already use b? */
544 static int already_uses(struct module *a, struct module *b)
545 {
546 struct module_use *use;
547
548 list_for_each_entry(use, &b->modules_which_use_me, list) {
549 if (use->module_which_uses == a) {
550 DEBUGP("%s uses %s!\n", a->name, b->name);
551 return 1;
552 }
553 }
554 DEBUGP("%s does not use %s!\n", a->name, b->name);
555 return 0;
556 }
557
558 /* Module a uses b */
559 int use_module(struct module *a, struct module *b)
560 {
561 struct module_use *use;
562 int no_warn, err;
563
564 if (b == NULL || already_uses(a, b)) return 1;
565
566 /* If we're interrupted or time out, we fail. */
567 if (wait_event_interruptible_timeout(
568 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
569 30 * HZ) <= 0) {
570 printk("%s: gave up waiting for init of module %s.\n",
571 a->name, b->name);
572 return 0;
573 }
574
575 /* If strong_try_module_get() returned a different error, we fail. */
576 if (err)
577 return 0;
578
579 DEBUGP("Allocating new usage for %s.\n", a->name);
580 use = kmalloc(sizeof(*use), GFP_ATOMIC);
581 if (!use) {
582 printk("%s: out of memory loading\n", a->name);
583 module_put(b);
584 return 0;
585 }
586
587 use->module_which_uses = a;
588 list_add(&use->list, &b->modules_which_use_me);
589 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
590 return 1;
591 }
592 EXPORT_SYMBOL_GPL(use_module);
593
594 /* Clear the unload stuff of the module. */
595 static void module_unload_free(struct module *mod)
596 {
597 struct module *i;
598
599 list_for_each_entry(i, &modules, list) {
600 struct module_use *use;
601
602 list_for_each_entry(use, &i->modules_which_use_me, list) {
603 if (use->module_which_uses == mod) {
604 DEBUGP("%s unusing %s\n", mod->name, i->name);
605 module_put(i);
606 list_del(&use->list);
607 kfree(use);
608 sysfs_remove_link(i->holders_dir, mod->name);
609 /* There can be at most one match. */
610 break;
611 }
612 }
613 }
614 }
615
616 #ifdef CONFIG_MODULE_FORCE_UNLOAD
617 static inline int try_force_unload(unsigned int flags)
618 {
619 int ret = (flags & O_TRUNC);
620 if (ret)
621 add_taint(TAINT_FORCED_RMMOD);
622 return ret;
623 }
624 #else
625 static inline int try_force_unload(unsigned int flags)
626 {
627 return 0;
628 }
629 #endif /* CONFIG_MODULE_FORCE_UNLOAD */
630
631 struct stopref
632 {
633 struct module *mod;
634 int flags;
635 int *forced;
636 };
637
638 /* Whole machine is stopped with interrupts off when this runs. */
639 static int __try_stop_module(void *_sref)
640 {
641 struct stopref *sref = _sref;
642
643 /* If it's not unused, quit unless we're forcing. */
644 if (module_refcount(sref->mod) != 0) {
645 if (!(*sref->forced = try_force_unload(sref->flags)))
646 return -EWOULDBLOCK;
647 }
648
649 /* Mark it as dying. */
650 sref->mod->state = MODULE_STATE_GOING;
651 return 0;
652 }
653
654 static int try_stop_module(struct module *mod, int flags, int *forced)
655 {
656 if (flags & O_NONBLOCK) {
657 struct stopref sref = { mod, flags, forced };
658
659 return stop_machine(__try_stop_module, &sref, NULL);
660 } else {
661 /* We don't need to stop the machine for this. */
662 mod->state = MODULE_STATE_GOING;
663 synchronize_sched();
664 return 0;
665 }
666 }
667
668 unsigned int module_refcount(struct module *mod)
669 {
670 unsigned int incs = 0, decs = 0;
671 int cpu;
672
673 for_each_possible_cpu(cpu)
674 decs += per_cpu_ptr(mod->refptr, cpu)->decs;
675 /*
676 * ensure the incs are added up after the decs.
677 * module_put ensures incs are visible before decs with smp_wmb.
678 *
679 * This 2-count scheme avoids the situation where the refcount
680 * for CPU0 is read, then CPU0 increments the module refcount,
681 * then CPU1 drops that refcount, then the refcount for CPU1 is
682 * read. We would record a decrement but not its corresponding
683 * increment so we would see a low count (disaster).
684 *
685 * Rare situation? But module_refcount can be preempted, and we
686 * might be tallying up 4096+ CPUs. So it is not impossible.
687 */
688 smp_rmb();
689 for_each_possible_cpu(cpu)
690 incs += per_cpu_ptr(mod->refptr, cpu)->incs;
691 return incs - decs;
692 }
693 EXPORT_SYMBOL(module_refcount);
694
695 /* This exists whether we can unload or not */
696 static void free_module(struct module *mod);
697
698 static void wait_for_zero_refcount(struct module *mod)
699 {
700 /* Since we might sleep for some time, release the mutex first */
701 mutex_unlock(&module_mutex);
702 for (;;) {
703 DEBUGP("Looking at refcount...\n");
704 set_current_state(TASK_UNINTERRUPTIBLE);
705 if (module_refcount(mod) == 0)
706 break;
707 schedule();
708 }
709 current->state = TASK_RUNNING;
710 mutex_lock(&module_mutex);
711 }
712
713 SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
714 unsigned int, flags)
715 {
716 struct module *mod;
717 char name[MODULE_NAME_LEN];
718 int ret, forced = 0;
719
720 if (!capable(CAP_SYS_MODULE) || modules_disabled)
721 return -EPERM;
722
723 if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
724 return -EFAULT;
725 name[MODULE_NAME_LEN-1] = '\0';
726
727 if (mutex_lock_interruptible(&module_mutex) != 0)
728 return -EINTR;
729
730 mod = find_module(name);
731 if (!mod) {
732 ret = -ENOENT;
733 goto out;
734 }
735
736 if (!list_empty(&mod->modules_which_use_me)) {
737 /* Other modules depend on us: get rid of them first. */
738 ret = -EWOULDBLOCK;
739 goto out;
740 }
741
742 /* Doing init or already dying? */
743 if (mod->state != MODULE_STATE_LIVE) {
744 /* FIXME: if (force), slam module count and wake up
745 waiter --RR */
746 DEBUGP("%s already dying\n", mod->name);
747 ret = -EBUSY;
748 goto out;
749 }
750
751 /* If it has an init func, it must have an exit func to unload */
752 if (mod->init && !mod->exit) {
753 forced = try_force_unload(flags);
754 if (!forced) {
755 /* This module can't be removed */
756 ret = -EBUSY;
757 goto out;
758 }
759 }
760
761 /* Set this up before setting mod->state */
762 mod->waiter = current;
763
764 /* Stop the machine so refcounts can't move and disable module. */
765 ret = try_stop_module(mod, flags, &forced);
766 if (ret != 0)
767 goto out;
768
769 /* Never wait if forced. */
770 if (!forced && module_refcount(mod) != 0)
771 wait_for_zero_refcount(mod);
772
773 mutex_unlock(&module_mutex);
774 /* Final destruction now noone is using it. */
775 if (mod->exit != NULL)
776 mod->exit();
777 blocking_notifier_call_chain(&module_notify_list,
778 MODULE_STATE_GOING, mod);
779 async_synchronize_full();
780 mutex_lock(&module_mutex);
781 /* Store the name of the last unloaded module for diagnostic purposes */
782 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
783 ddebug_remove_module(mod->name);
784 free_module(mod);
785
786 out:
787 mutex_unlock(&module_mutex);
788 return ret;
789 }
790
791 static inline void print_unload_info(struct seq_file *m, struct module *mod)
792 {
793 struct module_use *use;
794 int printed_something = 0;
795
796 seq_printf(m, " %u ", module_refcount(mod));
797
798 /* Always include a trailing , so userspace can differentiate
799 between this and the old multi-field proc format. */
800 list_for_each_entry(use, &mod->modules_which_use_me, list) {
801 printed_something = 1;
802 seq_printf(m, "%s,", use->module_which_uses->name);
803 }
804
805 if (mod->init != NULL && mod->exit == NULL) {
806 printed_something = 1;
807 seq_printf(m, "[permanent],");
808 }
809
810 if (!printed_something)
811 seq_printf(m, "-");
812 }
813
814 void __symbol_put(const char *symbol)
815 {
816 struct module *owner;
817
818 preempt_disable();
819 if (!find_symbol(symbol, &owner, NULL, true, false))
820 BUG();
821 module_put(owner);
822 preempt_enable();
823 }
824 EXPORT_SYMBOL(__symbol_put);
825
826 /* Note this assumes addr is a function, which it currently always is. */
827 void symbol_put_addr(void *addr)
828 {
829 struct module *modaddr;
830 unsigned long a = (unsigned long)dereference_function_descriptor(addr);
831
832 if (core_kernel_text(a))
833 return;
834
835 /* module_text_address is safe here: we're supposed to have reference
836 * to module from symbol_get, so it can't go away. */
837 modaddr = __module_text_address(a);
838 BUG_ON(!modaddr);
839 module_put(modaddr);
840 }
841 EXPORT_SYMBOL_GPL(symbol_put_addr);
842
843 static ssize_t show_refcnt(struct module_attribute *mattr,
844 struct module *mod, char *buffer)
845 {
846 return sprintf(buffer, "%u\n", module_refcount(mod));
847 }
848
849 static struct module_attribute refcnt = {
850 .attr = { .name = "refcnt", .mode = 0444 },
851 .show = show_refcnt,
852 };
853
854 void module_put(struct module *module)
855 {
856 if (module) {
857 preempt_disable();
858 smp_wmb(); /* see comment in module_refcount */
859 __this_cpu_inc(module->refptr->decs);
860
861 trace_module_put(module, _RET_IP_);
862 /* Maybe they're waiting for us to drop reference? */
863 if (unlikely(!module_is_live(module)))
864 wake_up_process(module->waiter);
865 preempt_enable();
866 }
867 }
868 EXPORT_SYMBOL(module_put);
869
870 #else /* !CONFIG_MODULE_UNLOAD */
871 static inline void print_unload_info(struct seq_file *m, struct module *mod)
872 {
873 /* We don't know the usage count, or what modules are using. */
874 seq_printf(m, " - -");
875 }
876
877 static inline void module_unload_free(struct module *mod)
878 {
879 }
880
881 int use_module(struct module *a, struct module *b)
882 {
883 return strong_try_module_get(b) == 0;
884 }
885 EXPORT_SYMBOL_GPL(use_module);
886
887 static inline void module_unload_init(struct module *mod)
888 {
889 }
890 #endif /* CONFIG_MODULE_UNLOAD */
891
892 static ssize_t show_initstate(struct module_attribute *mattr,
893 struct module *mod, char *buffer)
894 {
895 const char *state = "unknown";
896
897 switch (mod->state) {
898 case MODULE_STATE_LIVE:
899 state = "live";
900 break;
901 case MODULE_STATE_COMING:
902 state = "coming";
903 break;
904 case MODULE_STATE_GOING:
905 state = "going";
906 break;
907 }
908 return sprintf(buffer, "%s\n", state);
909 }
910
911 static struct module_attribute initstate = {
912 .attr = { .name = "initstate", .mode = 0444 },
913 .show = show_initstate,
914 };
915
916 static struct module_attribute *modinfo_attrs[] = {
917 &modinfo_version,
918 &modinfo_srcversion,
919 &initstate,
920 #ifdef CONFIG_MODULE_UNLOAD
921 &refcnt,
922 #endif
923 NULL,
924 };
925
926 static const char vermagic[] = VERMAGIC_STRING;
927
928 static int try_to_force_load(struct module *mod, const char *reason)
929 {
930 #ifdef CONFIG_MODULE_FORCE_LOAD
931 if (!test_taint(TAINT_FORCED_MODULE))
932 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
933 mod->name, reason);
934 add_taint_module(mod, TAINT_FORCED_MODULE);
935 return 0;
936 #else
937 return -ENOEXEC;
938 #endif
939 }
940
941 #ifdef CONFIG_MODVERSIONS
942 /* If the arch applies (non-zero) relocations to kernel kcrctab, unapply it. */
943 static unsigned long maybe_relocated(unsigned long crc,
944 const struct module *crc_owner)
945 {
946 #ifdef ARCH_RELOCATES_KCRCTAB
947 if (crc_owner == NULL)
948 return crc - (unsigned long)reloc_start;
949 #endif
950 return crc;
951 }
952
953 static int check_version(Elf_Shdr *sechdrs,
954 unsigned int versindex,
955 const char *symname,
956 struct module *mod,
957 const unsigned long *crc,
958 const struct module *crc_owner)
959 {
960 unsigned int i, num_versions;
961 struct modversion_info *versions;
962
963 /* Exporting module didn't supply crcs? OK, we're already tainted. */
964 if (!crc)
965 return 1;
966
967 /* No versions at all? modprobe --force does this. */
968 if (versindex == 0)
969 return try_to_force_load(mod, symname) == 0;
970
971 versions = (void *) sechdrs[versindex].sh_addr;
972 num_versions = sechdrs[versindex].sh_size
973 / sizeof(struct modversion_info);
974
975 for (i = 0; i < num_versions; i++) {
976 if (strcmp(versions[i].name, symname) != 0)
977 continue;
978
979 if (versions[i].crc == maybe_relocated(*crc, crc_owner))
980 return 1;
981 DEBUGP("Found checksum %lX vs module %lX\n",
982 maybe_relocated(*crc, crc_owner), versions[i].crc);
983 goto bad_version;
984 }
985
986 printk(KERN_WARNING "%s: no symbol version for %s\n",
987 mod->name, symname);
988 return 0;
989
990 bad_version:
991 printk("%s: disagrees about version of symbol %s\n",
992 mod->name, symname);
993 return 0;
994 }
995
996 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
997 unsigned int versindex,
998 struct module *mod)
999 {
1000 const unsigned long *crc;
1001
1002 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1003 &crc, true, false))
1004 BUG();
1005 return check_version(sechdrs, versindex, "module_layout", mod, crc,
1006 NULL);
1007 }
1008
1009 /* First part is kernel version, which we ignore if module has crcs. */
1010 static inline int same_magic(const char *amagic, const char *bmagic,
1011 bool has_crcs)
1012 {
1013 if (has_crcs) {
1014 amagic += strcspn(amagic, " ");
1015 bmagic += strcspn(bmagic, " ");
1016 }
1017 return strcmp(amagic, bmagic) == 0;
1018 }
1019 #else
1020 static inline int check_version(Elf_Shdr *sechdrs,
1021 unsigned int versindex,
1022 const char *symname,
1023 struct module *mod,
1024 const unsigned long *crc,
1025 const struct module *crc_owner)
1026 {
1027 return 1;
1028 }
1029
1030 static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1031 unsigned int versindex,
1032 struct module *mod)
1033 {
1034 return 1;
1035 }
1036
1037 static inline int same_magic(const char *amagic, const char *bmagic,
1038 bool has_crcs)
1039 {
1040 return strcmp(amagic, bmagic) == 0;
1041 }
1042 #endif /* CONFIG_MODVERSIONS */
1043
1044 /* Resolve a symbol for this module. I.e. if we find one, record usage.
1045 Must be holding module_mutex. */
1046 static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1047 unsigned int versindex,
1048 const char *name,
1049 struct module *mod)
1050 {
1051 struct module *owner;
1052 const struct kernel_symbol *sym;
1053 const unsigned long *crc;
1054
1055 sym = find_symbol(name, &owner, &crc,
1056 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1057 /* use_module can fail due to OOM,
1058 or module initialization or unloading */
1059 if (sym) {
1060 if (!check_version(sechdrs, versindex, name, mod, crc, owner)
1061 || !use_module(mod, owner))
1062 sym = NULL;
1063 }
1064 return sym;
1065 }
1066
1067 /*
1068 * /sys/module/foo/sections stuff
1069 * J. Corbet <corbet@lwn.net>
1070 */
1071 #if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS)
1072
1073 static inline bool sect_empty(const Elf_Shdr *sect)
1074 {
1075 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
1076 }
1077
1078 struct module_sect_attr
1079 {
1080 struct module_attribute mattr;
1081 char *name;
1082 unsigned long address;
1083 };
1084
1085 struct module_sect_attrs
1086 {
1087 struct attribute_group grp;
1088 unsigned int nsections;
1089 struct module_sect_attr attrs[0];
1090 };
1091
1092 static ssize_t module_sect_show(struct module_attribute *mattr,
1093 struct module *mod, char *buf)
1094 {
1095 struct module_sect_attr *sattr =
1096 container_of(mattr, struct module_sect_attr, mattr);
1097 return sprintf(buf, "0x%lx\n", sattr->address);
1098 }
1099
1100 static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1101 {
1102 unsigned int section;
1103
1104 for (section = 0; section < sect_attrs->nsections; section++)
1105 kfree(sect_attrs->attrs[section].name);
1106 kfree(sect_attrs);
1107 }
1108
1109 static void add_sect_attrs(struct module *mod, unsigned int nsect,
1110 char *secstrings, Elf_Shdr *sechdrs)
1111 {
1112 unsigned int nloaded = 0, i, size[2];
1113 struct module_sect_attrs *sect_attrs;
1114 struct module_sect_attr *sattr;
1115 struct attribute **gattr;
1116
1117 /* Count loaded sections and allocate structures */
1118 for (i = 0; i < nsect; i++)
1119 if (!sect_empty(&sechdrs[i]))
1120 nloaded++;
1121 size[0] = ALIGN(sizeof(*sect_attrs)
1122 + nloaded * sizeof(sect_attrs->attrs[0]),
1123 sizeof(sect_attrs->grp.attrs[0]));
1124 size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
1125 sect_attrs = kzalloc(size[0] + size[1], GFP_KERNEL);
1126 if (sect_attrs == NULL)
1127 return;
1128
1129 /* Setup section attributes. */
1130 sect_attrs->grp.name = "sections";
1131 sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
1132
1133 sect_attrs->nsections = 0;
1134 sattr = &sect_attrs->attrs[0];
1135 gattr = &sect_attrs->grp.attrs[0];
1136 for (i = 0; i < nsect; i++) {
1137 if (sect_empty(&sechdrs[i]))
1138 continue;
1139 sattr->address = sechdrs[i].sh_addr;
1140 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name,
1141 GFP_KERNEL);
1142 if (sattr->name == NULL)
1143 goto out;
1144 sect_attrs->nsections++;
1145 sysfs_attr_init(&sattr->mattr.attr);
1146 sattr->mattr.show = module_sect_show;
1147 sattr->mattr.store = NULL;
1148 sattr->mattr.attr.name = sattr->name;
1149 sattr->mattr.attr.mode = S_IRUGO;
1150 *(gattr++) = &(sattr++)->mattr.attr;
1151 }
1152 *gattr = NULL;
1153
1154 if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
1155 goto out;
1156
1157 mod->sect_attrs = sect_attrs;
1158 return;
1159 out:
1160 free_sect_attrs(sect_attrs);
1161 }
1162
1163 static void remove_sect_attrs(struct module *mod)
1164 {
1165 if (mod->sect_attrs) {
1166 sysfs_remove_group(&mod->mkobj.kobj,
1167 &mod->sect_attrs->grp);
1168 /* We are positive that no one is using any sect attrs
1169 * at this point. Deallocate immediately. */
1170 free_sect_attrs(mod->sect_attrs);
1171 mod->sect_attrs = NULL;
1172 }
1173 }
1174
1175 /*
1176 * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections.
1177 */
1178
1179 struct module_notes_attrs {
1180 struct kobject *dir;
1181 unsigned int notes;
1182 struct bin_attribute attrs[0];
1183 };
1184
1185 static ssize_t module_notes_read(struct kobject *kobj,
1186 struct bin_attribute *bin_attr,
1187 char *buf, loff_t pos, size_t count)
1188 {
1189 /*
1190 * The caller checked the pos and count against our size.
1191 */
1192 memcpy(buf, bin_attr->private + pos, count);
1193 return count;
1194 }
1195
1196 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1197 unsigned int i)
1198 {
1199 if (notes_attrs->dir) {
1200 while (i-- > 0)
1201 sysfs_remove_bin_file(notes_attrs->dir,
1202 &notes_attrs->attrs[i]);
1203 kobject_put(notes_attrs->dir);
1204 }
1205 kfree(notes_attrs);
1206 }
1207
1208 static void add_notes_attrs(struct module *mod, unsigned int nsect,
1209 char *secstrings, Elf_Shdr *sechdrs)
1210 {
1211 unsigned int notes, loaded, i;
1212 struct module_notes_attrs *notes_attrs;
1213 struct bin_attribute *nattr;
1214
1215 /* failed to create section attributes, so can't create notes */
1216 if (!mod->sect_attrs)
1217 return;
1218
1219 /* Count notes sections and allocate structures. */
1220 notes = 0;
1221 for (i = 0; i < nsect; i++)
1222 if (!sect_empty(&sechdrs[i]) &&
1223 (sechdrs[i].sh_type == SHT_NOTE))
1224 ++notes;
1225
1226 if (notes == 0)
1227 return;
1228
1229 notes_attrs = kzalloc(sizeof(*notes_attrs)
1230 + notes * sizeof(notes_attrs->attrs[0]),
1231 GFP_KERNEL);
1232 if (notes_attrs == NULL)
1233 return;
1234
1235 notes_attrs->notes = notes;
1236 nattr = &notes_attrs->attrs[0];
1237 for (loaded = i = 0; i < nsect; ++i) {
1238 if (sect_empty(&sechdrs[i]))
1239 continue;
1240 if (sechdrs[i].sh_type == SHT_NOTE) {
1241 sysfs_bin_attr_init(nattr);
1242 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1243 nattr->attr.mode = S_IRUGO;
1244 nattr->size = sechdrs[i].sh_size;
1245 nattr->private = (void *) sechdrs[i].sh_addr;
1246 nattr->read = module_notes_read;
1247 ++nattr;
1248 }
1249 ++loaded;
1250 }
1251
1252 notes_attrs->dir = kobject_create_and_add("notes", &mod->mkobj.kobj);
1253 if (!notes_attrs->dir)
1254 goto out;
1255
1256 for (i = 0; i < notes; ++i)
1257 if (sysfs_create_bin_file(notes_attrs->dir,
1258 &notes_attrs->attrs[i]))
1259 goto out;
1260
1261 mod->notes_attrs = notes_attrs;
1262 return;
1263
1264 out:
1265 free_notes_attrs(notes_attrs, i);
1266 }
1267
1268 static void remove_notes_attrs(struct module *mod)
1269 {
1270 if (mod->notes_attrs)
1271 free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes);
1272 }
1273
1274 #else
1275
1276 static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
1277 char *sectstrings, Elf_Shdr *sechdrs)
1278 {
1279 }
1280
1281 static inline void remove_sect_attrs(struct module *mod)
1282 {
1283 }
1284
1285 static inline void add_notes_attrs(struct module *mod, unsigned int nsect,
1286 char *sectstrings, Elf_Shdr *sechdrs)
1287 {
1288 }
1289
1290 static inline void remove_notes_attrs(struct module *mod)
1291 {
1292 }
1293 #endif
1294
1295 #ifdef CONFIG_SYSFS
1296 int module_add_modinfo_attrs(struct module *mod)
1297 {
1298 struct module_attribute *attr;
1299 struct module_attribute *temp_attr;
1300 int error = 0;
1301 int i;
1302
1303 mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
1304 (ARRAY_SIZE(modinfo_attrs) + 1)),
1305 GFP_KERNEL);
1306 if (!mod->modinfo_attrs)
1307 return -ENOMEM;
1308
1309 temp_attr = mod->modinfo_attrs;
1310 for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
1311 if (!attr->test ||
1312 (attr->test && attr->test(mod))) {
1313 memcpy(temp_attr, attr, sizeof(*temp_attr));
1314 sysfs_attr_init(&temp_attr->attr);
1315 error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
1316 ++temp_attr;
1317 }
1318 }
1319 return error;
1320 }
1321
1322 void module_remove_modinfo_attrs(struct module *mod)
1323 {
1324 struct module_attribute *attr;
1325 int i;
1326
1327 for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
1328 /* pick a field to test for end of list */
1329 if (!attr->attr.name)
1330 break;
1331 sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
1332 if (attr->free)
1333 attr->free(mod);
1334 }
1335 kfree(mod->modinfo_attrs);
1336 }
1337
1338 int mod_sysfs_init(struct module *mod)
1339 {
1340 int err;
1341 struct kobject *kobj;
1342
1343 if (!module_sysfs_initialized) {
1344 printk(KERN_ERR "%s: module sysfs not initialized\n",
1345 mod->name);
1346 err = -EINVAL;
1347 goto out;
1348 }
1349
1350 kobj = kset_find_obj(module_kset, mod->name);
1351 if (kobj) {
1352 printk(KERN_ERR "%s: module is already loaded\n", mod->name);
1353 kobject_put(kobj);
1354 err = -EINVAL;
1355 goto out;
1356 }
1357
1358 mod->mkobj.mod = mod;
1359
1360 memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
1361 mod->mkobj.kobj.kset = module_kset;
1362 err = kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL,
1363 "%s", mod->name);
1364 if (err)
1365 kobject_put(&mod->mkobj.kobj);
1366
1367 /* delay uevent until full sysfs population */
1368 out:
1369 return err;
1370 }
1371
1372 int mod_sysfs_setup(struct module *mod,
1373 struct kernel_param *kparam,
1374 unsigned int num_params)
1375 {
1376 int err;
1377
1378 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1379 if (!mod->holders_dir) {
1380 err = -ENOMEM;
1381 goto out_unreg;
1382 }
1383
1384 err = module_param_sysfs_setup(mod, kparam, num_params);
1385 if (err)
1386 goto out_unreg_holders;
1387
1388 err = module_add_modinfo_attrs(mod);
1389 if (err)
1390 goto out_unreg_param;
1391
1392 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1393 return 0;
1394
1395 out_unreg_param:
1396 module_param_sysfs_remove(mod);
1397 out_unreg_holders:
1398 kobject_put(mod->holders_dir);
1399 out_unreg:
1400 kobject_put(&mod->mkobj.kobj);
1401 return err;
1402 }
1403
1404 static void mod_sysfs_fini(struct module *mod)
1405 {
1406 kobject_put(&mod->mkobj.kobj);
1407 }
1408
1409 #else /* CONFIG_SYSFS */
1410
1411 static void mod_sysfs_fini(struct module *mod)
1412 {
1413 }
1414
1415 #endif /* CONFIG_SYSFS */
1416
1417 static void mod_kobject_remove(struct module *mod)
1418 {
1419 module_remove_modinfo_attrs(mod);
1420 module_param_sysfs_remove(mod);
1421 kobject_put(mod->mkobj.drivers_dir);
1422 kobject_put(mod->holders_dir);
1423 mod_sysfs_fini(mod);
1424 }
1425
1426 /*
1427 * unlink the module with the whole machine is stopped with interrupts off
1428 * - this defends against kallsyms not taking locks
1429 */
1430 static int __unlink_module(void *_mod)
1431 {
1432 struct module *mod = _mod;
1433 list_del(&mod->list);
1434 return 0;
1435 }
1436
1437 /* Free a module, remove from lists, etc (must hold module_mutex). */
1438 static void free_module(struct module *mod)
1439 {
1440 trace_module_free(mod);
1441
1442 /* Delete from various lists */
1443 stop_machine(__unlink_module, mod, NULL);
1444 remove_notes_attrs(mod);
1445 remove_sect_attrs(mod);
1446 mod_kobject_remove(mod);
1447
1448 /* Arch-specific cleanup. */
1449 module_arch_cleanup(mod);
1450
1451 /* Module unload stuff */
1452 module_unload_free(mod);
1453
1454 /* Free any allocated parameters. */
1455 destroy_params(mod->kp, mod->num_kp);
1456
1457 /* This may be NULL, but that's OK */
1458 module_free(mod, mod->module_init);
1459 kfree(mod->args);
1460 percpu_modfree(mod);
1461 #if defined(CONFIG_MODULE_UNLOAD)
1462 if (mod->refptr)
1463 free_percpu(mod->refptr);
1464 #endif
1465 /* Free lock-classes: */
1466 lockdep_free_key_range(mod->module_core, mod->core_size);
1467
1468 /* Finally, free the core (containing the module structure) */
1469 module_free(mod, mod->module_core);
1470
1471 #ifdef CONFIG_MPU
1472 update_protections(current->mm);
1473 #endif
1474 }
1475
1476 void *__symbol_get(const char *symbol)
1477 {
1478 struct module *owner;
1479 const struct kernel_symbol *sym;
1480
1481 preempt_disable();
1482 sym = find_symbol(symbol, &owner, NULL, true, true);
1483 if (sym && strong_try_module_get(owner))
1484 sym = NULL;
1485 preempt_enable();
1486
1487 return sym ? (void *)sym->value : NULL;
1488 }
1489 EXPORT_SYMBOL_GPL(__symbol_get);
1490
1491 /*
1492 * Ensure that an exported symbol [global namespace] does not already exist
1493 * in the kernel or in some other module's exported symbol table.
1494 */
1495 static int verify_export_symbols(struct module *mod)
1496 {
1497 unsigned int i;
1498 struct module *owner;
1499 const struct kernel_symbol *s;
1500 struct {
1501 const struct kernel_symbol *sym;
1502 unsigned int num;
1503 } arr[] = {
1504 { mod->syms, mod->num_syms },
1505 { mod->gpl_syms, mod->num_gpl_syms },
1506 { mod->gpl_future_syms, mod->num_gpl_future_syms },
1507 #ifdef CONFIG_UNUSED_SYMBOLS
1508 { mod->unused_syms, mod->num_unused_syms },
1509 { mod->unused_gpl_syms, mod->num_unused_gpl_syms },
1510 #endif
1511 };
1512
1513 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1514 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1515 if (find_symbol(s->name, &owner, 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;
1521 }
1522 }
1523 }
1524 return 0;
1525 }
1526
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)
1534 {
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;
1539 const struct kernel_symbol *ksym;
1540
1541 for (i = 1; i < n; i++) {
1542 switch (sym[i].st_shndx) {
1543 case SHN_COMMON:
1544 /* We compiled with -fno-common. These are not
1545 supposed to happen. */
1546 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
1547 printk("%s: please compile with -fno-common\n",
1548 mod->name);
1549 ret = -ENOEXEC;
1550 break;
1551
1552 case SHN_ABS:
1553 /* Don't need to do anything */
1554 DEBUGP("Absolute symbol: 0x%08lx\n",
1555 (long)sym[i].st_value);
1556 break;
1557
1558 case SHN_UNDEF:
1559 ksym = resolve_symbol(sechdrs, versindex,
1560 strtab + sym[i].st_name, mod);
1561 /* Ok if resolved. */
1562 if (ksym) {
1563 sym[i].st_value = ksym->value;
1564 break;
1565 }
1566
1567 /* Ok if weak. */
1568 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1569 break;
1570
1571 printk(KERN_WARNING "%s: Unknown symbol %s\n",
1572 mod->name, strtab + sym[i].st_name);
1573 ret = -ENOENT;
1574 break;
1575
1576 default:
1577 /* Divert to percpu allocation if a percpu var. */
1578 if (sym[i].st_shndx == pcpuindex)
1579 secbase = (unsigned long)mod_percpu(mod);
1580 else
1581 secbase = sechdrs[sym[i].st_shndx].sh_addr;
1582 sym[i].st_value += secbase;
1583 break;
1584 }
1585 }
1586
1587 return ret;
1588 }
1589
1590 /* Additional bytes needed by arch in front of individual sections */
1591 unsigned int __weak arch_mod_section_prepend(struct module *mod,
1592 unsigned int section)
1593 {
1594 /* default implementation just returns zero */
1595 return 0;
1596 }
1597
1598 /* Update size with this section: return offset. */
1599 static long get_offset(struct module *mod, unsigned int *size,
1600 Elf_Shdr *sechdr, unsigned int section)
1601 {
1602 long ret;
1603
1604 *size += arch_mod_section_prepend(mod, section);
1605 ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
1606 *size = ret + sechdr->sh_size;
1607 return ret;
1608 }
1609
1610 /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
1611 might -- code, read-only data, read-write data, small data. Tally
1612 sizes, and place the offsets into sh_entsize fields: high bit means it
1613 belongs in init. */
1614 static void layout_sections(struct module *mod,
1615 const Elf_Ehdr *hdr,
1616 Elf_Shdr *sechdrs,
1617 const char *secstrings)
1618 {
1619 static unsigned long const masks[][2] = {
1620 /* NOTE: all executable code must be the first section
1621 * in this array; otherwise modify the text_size
1622 * finder in the two loops below */
1623 { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
1624 { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
1625 { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
1626 { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
1627 };
1628 unsigned int m, i;
1629
1630 for (i = 0; i < hdr->e_shnum; i++)
1631 sechdrs[i].sh_entsize = ~0UL;
1632
1633 DEBUGP("Core section allocation order:\n");
1634 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1635 for (i = 0; i < hdr->e_shnum; ++i) {
1636 Elf_Shdr *s = &sechdrs[i];
1637
1638 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1639 || (s->sh_flags & masks[m][1])
1640 || s->sh_entsize != ~0UL
1641 || strstarts(secstrings + s->sh_name, ".init"))
1642 continue;
1643 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1644 DEBUGP("\t%s\n", secstrings + s->sh_name);
1645 }
1646 if (m == 0)
1647 mod->core_text_size = mod->core_size;
1648 }
1649
1650 DEBUGP("Init section allocation order:\n");
1651 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1652 for (i = 0; i < hdr->e_shnum; ++i) {
1653 Elf_Shdr *s = &sechdrs[i];
1654
1655 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1656 || (s->sh_flags & masks[m][1])
1657 || s->sh_entsize != ~0UL
1658 || !strstarts(secstrings + s->sh_name, ".init"))
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);
1663 }
1664 if (m == 0)
1665 mod->init_text_size = mod->init_size;
1666 }
1667 }
1668
1669 static void set_license(struct module *mod, const char *license)
1670 {
1671 if (!license)
1672 license = "unspecified";
1673
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);
1679 }
1680 }
1681
1682 /* Parse tag=value strings from .modinfo section */
1683 static char *next_string(char *string, unsigned long *secsize)
1684 {
1685 /* Skip non-zero chars */
1686 while (string[0]) {
1687 string++;
1688 if ((*secsize)-- <= 1)
1689 return NULL;
1690 }
1691
1692 /* Skip any zero padding. */
1693 while (!string[0]) {
1694 string++;
1695 if ((*secsize)-- <= 1)
1696 return NULL;
1697 }
1698 return string;
1699 }
1700
1701 static char *get_modinfo(Elf_Shdr *sechdrs,
1702 unsigned int info,
1703 const char *tag)
1704 {
1705 char *p;
1706 unsigned int taglen = strlen(tag);
1707 unsigned long size = sechdrs[info].sh_size;
1708
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;
1712 }
1713 return NULL;
1714 }
1715
1716 static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
1717 unsigned int infoindex)
1718 {
1719 struct module_attribute *attr;
1720 int i;
1721
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));
1728 }
1729 }
1730
1731 static void free_modinfo(struct module *mod)
1732 {
1733 struct module_attribute *attr;
1734 int i;
1735
1736 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1737 if (attr->free)
1738 attr->free(mod);
1739 }
1740 }
1741
1742 #ifdef CONFIG_KALLSYMS
1743
1744 /* lookup symbol in given range of kernel_symbols */
1745 static const struct kernel_symbol *lookup_symbol(const char *name,
1746 const struct kernel_symbol *start,
1747 const struct kernel_symbol *stop)
1748 {
1749 const struct kernel_symbol *ks = start;
1750 for (; ks < stop; ks++)
1751 if (strcmp(ks->name, name) == 0)
1752 return ks;
1753 return NULL;
1754 }
1755
1756 static int is_exported(const char *name, unsigned long value,
1757 const struct module *mod)
1758 {
1759 const struct kernel_symbol *ks;
1760 if (!mod)
1761 ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
1762 else
1763 ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
1764 return ks != NULL && ks->value == value;
1765 }
1766
1767 /* As per nm */
1768 static char elf_type(const Elf_Sym *sym,
1769 Elf_Shdr *sechdrs,
1770 const char *secstrings,
1771 struct module *mod)
1772 {
1773 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1774 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1775 return 'v';
1776 else
1777 return 'w';
1778 }
1779 if (sym->st_shndx == SHN_UNDEF)
1780 return 'U';
1781 if (sym->st_shndx == SHN_ABS)
1782 return 'a';
1783 if (sym->st_shndx >= SHN_LORESERVE)
1784 return '?';
1785 if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
1786 return 't';
1787 if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
1788 && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
1789 if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
1790 return 'r';
1791 else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1792 return 'g';
1793 else
1794 return 'd';
1795 }
1796 if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
1797 if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
1798 return 's';
1799 else
1800 return 'b';
1801 }
1802 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
1803 return 'n';
1804 return '?';
1805 }
1806
1807 static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1808 unsigned int shnum)
1809 {
1810 const Elf_Shdr *sec;
1811
1812 if (src->st_shndx == SHN_UNDEF
1813 || src->st_shndx >= shnum
1814 || !src->st_name)
1815 return false;
1816
1817 sec = sechdrs + src->st_shndx;
1818 if (!(sec->sh_flags & SHF_ALLOC)
1819 #ifndef CONFIG_KALLSYMS_ALL
1820 || !(sec->sh_flags & SHF_EXECINSTR)
1821 #endif
1822 || (sec->sh_entsize & INIT_OFFSET_MASK))
1823 return false;
1824
1825 return true;
1826 }
1827
1828 static unsigned long layout_symtab(struct module *mod,
1829 Elf_Shdr *sechdrs,
1830 unsigned int symindex,
1831 unsigned int strindex,
1832 const Elf_Ehdr *hdr,
1833 const char *secstrings,
1834 unsigned long *pstroffs,
1835 unsigned long *strmap)
1836 {
1837 unsigned long symoffs;
1838 Elf_Shdr *symsect = sechdrs + symindex;
1839 Elf_Shdr *strsect = sechdrs + strindex;
1840 const Elf_Sym *src;
1841 const char *strtab;
1842 unsigned int i, nsrc, ndst;
1843
1844 /* Put symbol section at end of init part of module. */
1845 symsect->sh_flags |= SHF_ALLOC;
1846 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
1847 symindex) | INIT_OFFSET_MASK;
1848 DEBUGP("\t%s\n", secstrings + symsect->sh_name);
1849
1850 src = (void *)hdr + symsect->sh_offset;
1851 nsrc = symsect->sh_size / sizeof(*src);
1852 strtab = (void *)hdr + strsect->sh_offset;
1853 for (ndst = i = 1; i < nsrc; ++i, ++src)
1854 if (is_core_symbol(src, sechdrs, hdr->e_shnum)) {
1855 unsigned int j = src->st_name;
1856
1857 while(!__test_and_set_bit(j, strmap) && strtab[j])
1858 ++j;
1859 ++ndst;
1860 }
1861
1862 /* Append room for core symbols at end of core part. */
1863 symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1864 mod->core_size = symoffs + ndst * sizeof(Elf_Sym);
1865
1866 /* Put string table section at end of init part of module. */
1867 strsect->sh_flags |= SHF_ALLOC;
1868 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
1869 strindex) | INIT_OFFSET_MASK;
1870 DEBUGP("\t%s\n", secstrings + strsect->sh_name);
1871
1872 /* Append room for core symbols' strings at end of core part. */
1873 *pstroffs = mod->core_size;
1874 __set_bit(0, strmap);
1875 mod->core_size += bitmap_weight(strmap, strsect->sh_size);
1876
1877 return symoffs;
1878 }
1879
1880 static void add_kallsyms(struct module *mod,
1881 Elf_Shdr *sechdrs,
1882 unsigned int shnum,
1883 unsigned int symindex,
1884 unsigned int strindex,
1885 unsigned long symoffs,
1886 unsigned long stroffs,
1887 const char *secstrings,
1888 unsigned long *strmap)
1889 {
1890 unsigned int i, ndst;
1891 const Elf_Sym *src;
1892 Elf_Sym *dst;
1893 char *s;
1894
1895 mod->symtab = (void *)sechdrs[symindex].sh_addr;
1896 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1897 mod->strtab = (void *)sechdrs[strindex].sh_addr;
1898
1899 /* Set types up while we still have access to sections. */
1900 for (i = 0; i < mod->num_symtab; i++)
1901 mod->symtab[i].st_info
1902 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
1903
1904 mod->core_symtab = dst = mod->module_core + symoffs;
1905 src = mod->symtab;
1906 *dst = *src;
1907 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
1908 if (!is_core_symbol(src, sechdrs, shnum))
1909 continue;
1910 dst[ndst] = *src;
1911 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name);
1912 ++ndst;
1913 }
1914 mod->core_num_syms = ndst;
1915
1916 mod->core_strtab = s = mod->module_core + stroffs;
1917 for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i)
1918 if (test_bit(i, strmap))
1919 *++s = mod->strtab[i];
1920 }
1921 #else
1922 static inline unsigned long layout_symtab(struct module *mod,
1923 Elf_Shdr *sechdrs,
1924 unsigned int symindex,
1925 unsigned int strindex,
1926 const Elf_Ehdr *hdr,
1927 const char *secstrings,
1928 unsigned long *pstroffs,
1929 unsigned long *strmap)
1930 {
1931 return 0;
1932 }
1933
1934 static inline void add_kallsyms(struct module *mod,
1935 Elf_Shdr *sechdrs,
1936 unsigned int shnum,
1937 unsigned int symindex,
1938 unsigned int strindex,
1939 unsigned long symoffs,
1940 unsigned long stroffs,
1941 const char *secstrings,
1942 const unsigned long *strmap)
1943 {
1944 }
1945 #endif /* CONFIG_KALLSYMS */
1946
1947 static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1948 {
1949 #ifdef CONFIG_DYNAMIC_DEBUG
1950 if (ddebug_add_module(debug, num, debug->modname))
1951 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1952 debug->modname);
1953 #endif
1954 }
1955
1956 static void *module_alloc_update_bounds(unsigned long size)
1957 {
1958 void *ret = module_alloc(size);
1959
1960 if (ret) {
1961 /* Update module bounds. */
1962 if ((unsigned long)ret < module_addr_min)
1963 module_addr_min = (unsigned long)ret;
1964 if ((unsigned long)ret + size > module_addr_max)
1965 module_addr_max = (unsigned long)ret + size;
1966 }
1967 return ret;
1968 }
1969
1970 #ifdef CONFIG_DEBUG_KMEMLEAK
1971 static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1972 Elf_Shdr *sechdrs, char *secstrings)
1973 {
1974 unsigned int i;
1975
1976 /* only scan the sections containing data */
1977 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
1978
1979 for (i = 1; i < hdr->e_shnum; i++) {
1980 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
1981 continue;
1982 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0
1983 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
1984 continue;
1985
1986 kmemleak_scan_area((void *)sechdrs[i].sh_addr,
1987 sechdrs[i].sh_size, GFP_KERNEL);
1988 }
1989 }
1990 #else
1991 static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr,
1992 Elf_Shdr *sechdrs, char *secstrings)
1993 {
1994 }
1995 #endif
1996
1997 /* Allocate and load the module: note that size of section 0 is always
1998 zero, and we rely on this for optional sections. */
1999 static noinline struct module *load_module(void __user *umod,
2000 unsigned long len,
2001 const char __user *uargs)
2002 {
2003 Elf_Ehdr *hdr;
2004 Elf_Shdr *sechdrs;
2005 char *secstrings, *args, *modmagic, *strtab = NULL;
2006 char *staging;
2007 unsigned int i;
2008 unsigned int symindex = 0;
2009 unsigned int strindex = 0;
2010 unsigned int modindex, versindex, infoindex, pcpuindex;
2011 struct module *mod;
2012 long err = 0;
2013 void *ptr = NULL; /* Stops spurious gcc warning */
2014 unsigned long symoffs, stroffs, *strmap;
2015
2016 mm_segment_t old_fs;
2017
2018 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2019 umod, len, uargs);
2020 if (len < sizeof(*hdr))
2021 return ERR_PTR(-ENOEXEC);
2022
2023 /* Suck in entire file: we'll want most of it. */
2024 /* vmalloc barfs on "unusual" numbers. Check here */
2025 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
2026 return ERR_PTR(-ENOMEM);
2027
2028 if (copy_from_user(hdr, umod, len) != 0) {
2029 err = -EFAULT;
2030 goto free_hdr;
2031 }
2032
2033 /* Sanity checks against insmoding binaries or wrong arch,
2034 weird elf version */
2035 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2036 || hdr->e_type != ET_REL
2037 || !elf_check_arch(hdr)
2038 || hdr->e_shentsize != sizeof(*sechdrs)) {
2039 err = -ENOEXEC;
2040 goto free_hdr;
2041 }
2042
2043 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
2044 goto truncated;
2045
2046 /* Convenience variables */
2047 sechdrs = (void *)hdr + hdr->e_shoff;
2048 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
2049 sechdrs[0].sh_addr = 0;
2050
2051 for (i = 1; i < hdr->e_shnum; i++) {
2052 if (sechdrs[i].sh_type != SHT_NOBITS
2053 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
2054 goto truncated;
2055
2056 /* Mark all sections sh_addr with their address in the
2057 temporary image. */
2058 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
2059
2060 /* Internal symbols and strings. */
2061 if (sechdrs[i].sh_type == SHT_SYMTAB) {
2062 symindex = i;
2063 strindex = sechdrs[i].sh_link;
2064 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
2065 }
2066 #ifndef CONFIG_MODULE_UNLOAD
2067 /* Don't load .exit sections */
2068 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
2069 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
2070 #endif
2071 }
2072
2073 modindex = find_sec(hdr, sechdrs, secstrings,
2074 ".gnu.linkonce.this_module");
2075 if (!modindex) {
2076 printk(KERN_WARNING "No module found in object\n");
2077 err = -ENOEXEC;
2078 goto free_hdr;
2079 }
2080 /* This is temporary: point mod into copy of data. */
2081 mod = (void *)sechdrs[modindex].sh_addr;
2082
2083 if (symindex == 0) {
2084 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2085 mod->name);
2086 err = -ENOEXEC;
2087 goto free_hdr;
2088 }
2089
2090 versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
2091 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2092 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2093
2094 /* Don't keep modinfo and version sections. */
2095 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2096 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2097
2098 /* Check module struct version now, before we try to use module. */
2099 if (!check_modstruct_version(sechdrs, versindex, mod)) {
2100 err = -ENOEXEC;
2101 goto free_hdr;
2102 }
2103
2104 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2105 /* This is allowed: modprobe --force will invalidate it. */
2106 if (!modmagic) {
2107 err = try_to_force_load(mod, "bad vermagic");
2108 if (err)
2109 goto free_hdr;
2110 } else if (!same_magic(modmagic, vermagic, versindex)) {
2111 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2112 mod->name, modmagic, vermagic);
2113 err = -ENOEXEC;
2114 goto free_hdr;
2115 }
2116
2117 staging = get_modinfo(sechdrs, infoindex, "staging");
2118 if (staging) {
2119 add_taint_module(mod, TAINT_CRAP);
2120 printk(KERN_WARNING "%s: module is from the staging directory,"
2121 " the quality is unknown, you have been warned.\n",
2122 mod->name);
2123 }
2124
2125 /* Now copy in args */
2126 args = strndup_user(uargs, ~0UL >> 1);
2127 if (IS_ERR(args)) {
2128 err = PTR_ERR(args);
2129 goto free_hdr;
2130 }
2131
2132 strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size)
2133 * sizeof(long), GFP_KERNEL);
2134 if (!strmap) {
2135 err = -ENOMEM;
2136 goto free_mod;
2137 }
2138
2139 if (find_module(mod->name)) {
2140 err = -EEXIST;
2141 goto free_mod;
2142 }
2143
2144 mod->state = MODULE_STATE_COMING;
2145
2146 /* Allow arches to frob section contents and sizes. */
2147 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
2148 if (err < 0)
2149 goto free_mod;
2150
2151 if (pcpuindex) {
2152 /* We have a special allocation for this section. */
2153 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size,
2154 sechdrs[pcpuindex].sh_addralign);
2155 if (err)
2156 goto free_mod;
2157 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2158 }
2159
2160 /* Determine total sizes, and put offsets in sh_entsize. For now
2161 this is done generically; there doesn't appear to be any
2162 special cases for the architectures. */
2163 layout_sections(mod, hdr, sechdrs, secstrings);
2164 symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
2165 secstrings, &stroffs, strmap);
2166
2167 /* Do the allocs. */
2168 ptr = module_alloc_update_bounds(mod->core_size);
2169 /*
2170 * The pointer to this block is stored in the module structure
2171 * which is inside the block. Just mark it as not being a
2172 * leak.
2173 */
2174 kmemleak_not_leak(ptr);
2175 if (!ptr) {
2176 err = -ENOMEM;
2177 goto free_percpu;
2178 }
2179 memset(ptr, 0, mod->core_size);
2180 mod->module_core = ptr;
2181
2182 ptr = module_alloc_update_bounds(mod->init_size);
2183 /*
2184 * The pointer to this block is stored in the module structure
2185 * which is inside the block. This block doesn't need to be
2186 * scanned as it contains data and code that will be freed
2187 * after the module is initialized.
2188 */
2189 kmemleak_ignore(ptr);
2190 if (!ptr && mod->init_size) {
2191 err = -ENOMEM;
2192 goto free_core;
2193 }
2194 memset(ptr, 0, mod->init_size);
2195 mod->module_init = ptr;
2196
2197 /* Transfer each section which specifies SHF_ALLOC */
2198 DEBUGP("final section addresses:\n");
2199 for (i = 0; i < hdr->e_shnum; i++) {
2200 void *dest;
2201
2202 if (!(sechdrs[i].sh_flags & SHF_ALLOC))
2203 continue;
2204
2205 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
2206 dest = mod->module_init
2207 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
2208 else
2209 dest = mod->module_core + sechdrs[i].sh_entsize;
2210
2211 if (sechdrs[i].sh_type != SHT_NOBITS)
2212 memcpy(dest, (void *)sechdrs[i].sh_addr,
2213 sechdrs[i].sh_size);
2214 /* Update sh_addr to point to copy in image. */
2215 sechdrs[i].sh_addr = (unsigned long)dest;
2216 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
2217 }
2218 /* Module has been moved. */
2219 mod = (void *)sechdrs[modindex].sh_addr;
2220 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2221
2222 #if defined(CONFIG_MODULE_UNLOAD)
2223 mod->refptr = alloc_percpu(struct module_ref);
2224 if (!mod->refptr) {
2225 err = -ENOMEM;
2226 goto free_init;
2227 }
2228 #endif
2229 /* Now we've moved module, initialize linked lists, etc. */
2230 module_unload_init(mod);
2231
2232 /* add kobject, so we can reference it. */
2233 err = mod_sysfs_init(mod);
2234 if (err)
2235 goto free_unload;
2236
2237 /* Set up license info based on the info section */
2238 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2239
2240 /*
2241 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2242 * Don't use add_taint_module(), as it would prevent ndiswrapper from
2243 * using GPL-only symbols it needs.
2244 */
2245 if (strcmp(mod->name, "ndiswrapper") == 0)
2246 add_taint(TAINT_PROPRIETARY_MODULE);
2247
2248 /* driverloader was caught wrongly pretending to be under GPL */
2249 if (strcmp(mod->name, "driverloader") == 0)
2250 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2251
2252 /* Set up MODINFO_ATTR fields */
2253 setup_modinfo(mod, sechdrs, infoindex);
2254
2255 /* Fix up syms, so that st_value is a pointer to location. */
2256 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2257 mod);
2258 if (err < 0)
2259 goto cleanup;
2260
2261 /* Now we've got everything in the final locations, we can
2262 * find optional sections. */
2263 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2264 sizeof(*mod->kp), &mod->num_kp);
2265 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2266 sizeof(*mod->syms), &mod->num_syms);
2267 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2268 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2269 sizeof(*mod->gpl_syms),
2270 &mod->num_gpl_syms);
2271 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2272 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2273 "__ksymtab_gpl_future",
2274 sizeof(*mod->gpl_future_syms),
2275 &mod->num_gpl_future_syms);
2276 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2277 "__kcrctab_gpl_future");
2278
2279 #ifdef CONFIG_UNUSED_SYMBOLS
2280 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2281 "__ksymtab_unused",
2282 sizeof(*mod->unused_syms),
2283 &mod->num_unused_syms);
2284 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2285 "__kcrctab_unused");
2286 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2287 "__ksymtab_unused_gpl",
2288 sizeof(*mod->unused_gpl_syms),
2289 &mod->num_unused_gpl_syms);
2290 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2291 "__kcrctab_unused_gpl");
2292 #endif
2293 #ifdef CONFIG_CONSTRUCTORS
2294 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2295 sizeof(*mod->ctors), &mod->num_ctors);
2296 #endif
2297
2298 #ifdef CONFIG_TRACEPOINTS
2299 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2300 "__tracepoints",
2301 sizeof(*mod->tracepoints),
2302 &mod->num_tracepoints);
2303 #endif
2304 #ifdef CONFIG_EVENT_TRACING
2305 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2306 "_ftrace_events",
2307 sizeof(*mod->trace_events),
2308 &mod->num_trace_events);
2309 /*
2310 * This section contains pointers to allocated objects in the trace
2311 * code and not scanning it leads to false positives.
2312 */
2313 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2314 mod->num_trace_events, GFP_KERNEL);
2315 #endif
2316 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
2317 /* sechdrs[0].sh_size is always zero */
2318 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2319 "__mcount_loc",
2320 sizeof(*mod->ftrace_callsites),
2321 &mod->num_ftrace_callsites);
2322 #endif
2323 #ifdef CONFIG_MODVERSIONS
2324 if ((mod->num_syms && !mod->crcs)
2325 || (mod->num_gpl_syms && !mod->gpl_crcs)
2326 || (mod->num_gpl_future_syms && !mod->gpl_future_crcs)
2327 #ifdef CONFIG_UNUSED_SYMBOLS
2328 || (mod->num_unused_syms && !mod->unused_crcs)
2329 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2330 #endif
2331 ) {
2332 err = try_to_force_load(mod,
2333 "no versions for exported symbols");
2334 if (err)
2335 goto cleanup;
2336 }
2337 #endif
2338
2339 /* Now do relocations. */
2340 for (i = 1; i < hdr->e_shnum; i++) {
2341 const char *strtab = (char *)sechdrs[strindex].sh_addr;
2342 unsigned int info = sechdrs[i].sh_info;
2343
2344 /* Not a valid relocation section? */
2345 if (info >= hdr->e_shnum)
2346 continue;
2347
2348 /* Don't bother with non-allocated sections */
2349 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2350 continue;
2351
2352 if (sechdrs[i].sh_type == SHT_REL)
2353 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2354 else if (sechdrs[i].sh_type == SHT_RELA)
2355 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2356 mod);
2357 if (err < 0)
2358 goto cleanup;
2359 }
2360
2361 /* Find duplicate symbols */
2362 err = verify_export_symbols(mod);
2363 if (err < 0)
2364 goto cleanup;
2365
2366 /* Set up and sort exception table */
2367 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2368 sizeof(*mod->extable), &mod->num_exentries);
2369 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2370
2371 /* Finally, copy percpu area over. */
2372 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
2373 sechdrs[pcpuindex].sh_size);
2374
2375 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
2376 symoffs, stroffs, secstrings, strmap);
2377 kfree(strmap);
2378 strmap = NULL;
2379
2380 if (!mod->taints) {
2381 struct _ddebug *debug;
2382 unsigned int num_debug;
2383
2384 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2385 sizeof(*debug), &num_debug);
2386 if (debug)
2387 dynamic_debug_setup(debug, num_debug);
2388 }
2389
2390 err = module_finalize(hdr, sechdrs, mod);
2391 if (err < 0)
2392 goto cleanup;
2393
2394 /* flush the icache in correct context */
2395 old_fs = get_fs();
2396 set_fs(KERNEL_DS);
2397
2398 /*
2399 * Flush the instruction cache, since we've played with text.
2400 * Do it before processing of module parameters, so the module
2401 * can provide parameter accessor functions of its own.
2402 */
2403 if (mod->module_init)
2404 flush_icache_range((unsigned long)mod->module_init,
2405 (unsigned long)mod->module_init
2406 + mod->init_size);
2407 flush_icache_range((unsigned long)mod->module_core,
2408 (unsigned long)mod->module_core + mod->core_size);
2409
2410 set_fs(old_fs);
2411
2412 mod->args = args;
2413 if (section_addr(hdr, sechdrs, secstrings, "__obsparm"))
2414 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2415 mod->name);
2416
2417 /* Now sew it into the lists so we can get lockdep and oops
2418 * info during argument parsing. Noone should access us, since
2419 * strong_try_module_get() will fail.
2420 * lockdep/oops can run asynchronous, so use the RCU list insertion
2421 * function to insert in a way safe to concurrent readers.
2422 * The mutex protects against concurrent writers.
2423 */
2424 list_add_rcu(&mod->list, &modules);
2425
2426 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2427 if (err < 0)
2428 goto unlink;
2429
2430 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2431 if (err < 0)
2432 goto unlink;
2433 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2434 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2435
2436 /* Get rid of temporary copy */
2437 vfree(hdr);
2438
2439 trace_module_load(mod);
2440
2441 /* Done! */
2442 return mod;
2443
2444 unlink:
2445 /* Unlink carefully: kallsyms could be walking list. */
2446 list_del_rcu(&mod->list);
2447 synchronize_sched();
2448 module_arch_cleanup(mod);
2449 cleanup:
2450 free_modinfo(mod);
2451 kobject_del(&mod->mkobj.kobj);
2452 kobject_put(&mod->mkobj.kobj);
2453 free_unload:
2454 module_unload_free(mod);
2455 #if defined(CONFIG_MODULE_UNLOAD)
2456 free_percpu(mod->refptr);
2457 free_init:
2458 #endif
2459 module_free(mod, mod->module_init);
2460 free_core:
2461 module_free(mod, mod->module_core);
2462 /* mod will be freed with core. Don't access it beyond this line! */
2463 free_percpu:
2464 percpu_modfree(mod);
2465 free_mod:
2466 kfree(args);
2467 kfree(strmap);
2468 free_hdr:
2469 vfree(hdr);
2470 return ERR_PTR(err);
2471
2472 truncated:
2473 printk(KERN_ERR "Module len %lu truncated\n", len);
2474 err = -ENOEXEC;
2475 goto free_hdr;
2476 }
2477
2478 /* Call module constructors. */
2479 static void do_mod_ctors(struct module *mod)
2480 {
2481 #ifdef CONFIG_CONSTRUCTORS
2482 unsigned long i;
2483
2484 for (i = 0; i < mod->num_ctors; i++)
2485 mod->ctors[i]();
2486 #endif
2487 }
2488
2489 /* This is where the real work happens */
2490 SYSCALL_DEFINE3(init_module, void __user *, umod,
2491 unsigned long, len, const char __user *, uargs)
2492 {
2493 struct module *mod;
2494 int ret = 0;
2495
2496 /* Must have permission */
2497 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2498 return -EPERM;
2499
2500 /* Only one module load at a time, please */
2501 if (mutex_lock_interruptible(&module_mutex) != 0)
2502 return -EINTR;
2503
2504 /* Do all the hard work */
2505 mod = load_module(umod, len, uargs);
2506 if (IS_ERR(mod)) {
2507 mutex_unlock(&module_mutex);
2508 return PTR_ERR(mod);
2509 }
2510
2511 /* Drop lock so they can recurse */
2512 mutex_unlock(&module_mutex);
2513
2514 blocking_notifier_call_chain(&module_notify_list,
2515 MODULE_STATE_COMING, mod);
2516
2517 do_mod_ctors(mod);
2518 /* Start the module */
2519 if (mod->init != NULL)
2520 ret = do_one_initcall(mod->init);
2521 if (ret < 0) {
2522 /* Init routine failed: abort. Try to protect us from
2523 buggy refcounters. */
2524 mod->state = MODULE_STATE_GOING;
2525 synchronize_sched();
2526 module_put(mod);
2527 blocking_notifier_call_chain(&module_notify_list,
2528 MODULE_STATE_GOING, mod);
2529 mutex_lock(&module_mutex);
2530 free_module(mod);
2531 mutex_unlock(&module_mutex);
2532 wake_up(&module_wq);
2533 return ret;
2534 }
2535 if (ret > 0) {
2536 printk(KERN_WARNING
2537 "%s: '%s'->init suspiciously returned %d, it should follow 0/-E convention\n"
2538 "%s: loading module anyway...\n",
2539 __func__, mod->name, ret,
2540 __func__);
2541 dump_stack();
2542 }
2543
2544 /* Now it's a first class citizen! Wake up anyone waiting for it. */
2545 mod->state = MODULE_STATE_LIVE;
2546 wake_up(&module_wq);
2547 blocking_notifier_call_chain(&module_notify_list,
2548 MODULE_STATE_LIVE, mod);
2549
2550 /* We need to finish all async code before the module init sequence is done */
2551 async_synchronize_full();
2552
2553 mutex_lock(&module_mutex);
2554 /* Drop initial reference. */
2555 module_put(mod);
2556 trim_init_extable(mod);
2557 #ifdef CONFIG_KALLSYMS
2558 mod->num_symtab = mod->core_num_syms;
2559 mod->symtab = mod->core_symtab;
2560 mod->strtab = mod->core_strtab;
2561 #endif
2562 module_free(mod, mod->module_init);
2563 mod->module_init = NULL;
2564 mod->init_size = 0;
2565 mod->init_text_size = 0;
2566 mutex_unlock(&module_mutex);
2567
2568 return 0;
2569 }
2570
2571 static inline int within(unsigned long addr, void *start, unsigned long size)
2572 {
2573 return ((void *)addr >= start && (void *)addr < start + size);
2574 }
2575
2576 #ifdef CONFIG_KALLSYMS
2577 /*
2578 * This ignores the intensely annoying "mapping symbols" found
2579 * in ARM ELF files: $a, $t and $d.
2580 */
2581 static inline int is_arm_mapping_symbol(const char *str)
2582 {
2583 return str[0] == '$' && strchr("atd", str[1])
2584 && (str[2] == '\0' || str[2] == '.');
2585 }
2586
2587 static const char *get_ksymbol(struct module *mod,
2588 unsigned long addr,
2589 unsigned long *size,
2590 unsigned long *offset)
2591 {
2592 unsigned int i, best = 0;
2593 unsigned long nextval;
2594
2595 /* At worse, next value is at end of module */
2596 if (within_module_init(addr, mod))
2597 nextval = (unsigned long)mod->module_init+mod->init_text_size;
2598 else
2599 nextval = (unsigned long)mod->module_core+mod->core_text_size;
2600
2601 /* Scan for closest preceeding symbol, and next symbol. (ELF
2602 starts real symbols at 1). */
2603 for (i = 1; i < mod->num_symtab; i++) {
2604 if (mod->symtab[i].st_shndx == SHN_UNDEF)
2605 continue;
2606
2607 /* We ignore unnamed symbols: they're uninformative
2608 * and inserted at a whim. */
2609 if (mod->symtab[i].st_value <= addr
2610 && mod->symtab[i].st_value > mod->symtab[best].st_value
2611 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2612 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2613 best = i;
2614 if (mod->symtab[i].st_value > addr
2615 && mod->symtab[i].st_value < nextval
2616 && *(mod->strtab + mod->symtab[i].st_name) != '\0'
2617 && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
2618 nextval = mod->symtab[i].st_value;
2619 }
2620
2621 if (!best)
2622 return NULL;
2623
2624 if (size)
2625 *size = nextval - mod->symtab[best].st_value;
2626 if (offset)
2627 *offset = addr - mod->symtab[best].st_value;
2628 return mod->strtab + mod->symtab[best].st_name;
2629 }
2630
2631 /* For kallsyms to ask for address resolution. NULL means not found. Careful
2632 * not to lock to avoid deadlock on oopses, simply disable preemption. */
2633 const char *module_address_lookup(unsigned long addr,
2634 unsigned long *size,
2635 unsigned long *offset,
2636 char **modname,
2637 char *namebuf)
2638 {
2639 struct module *mod;
2640 const char *ret = NULL;
2641
2642 preempt_disable();
2643 list_for_each_entry_rcu(mod, &modules, list) {
2644 if (within_module_init(addr, mod) ||
2645 within_module_core(addr, mod)) {
2646 if (modname)
2647 *modname = mod->name;
2648 ret = get_ksymbol(mod, addr, size, offset);
2649 break;
2650 }
2651 }
2652 /* Make a copy in here where it's safe */
2653 if (ret) {
2654 strncpy(namebuf, ret, KSYM_NAME_LEN - 1);
2655 ret = namebuf;
2656 }
2657 preempt_enable();
2658 return ret;
2659 }
2660
2661 int lookup_module_symbol_name(unsigned long addr, char *symname)
2662 {
2663 struct module *mod;
2664
2665 preempt_disable();
2666 list_for_each_entry_rcu(mod, &modules, list) {
2667 if (within_module_init(addr, mod) ||
2668 within_module_core(addr, mod)) {
2669 const char *sym;
2670
2671 sym = get_ksymbol(mod, addr, NULL, NULL);
2672 if (!sym)
2673 goto out;
2674 strlcpy(symname, sym, KSYM_NAME_LEN);
2675 preempt_enable();
2676 return 0;
2677 }
2678 }
2679 out:
2680 preempt_enable();
2681 return -ERANGE;
2682 }
2683
2684 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size,
2685 unsigned long *offset, char *modname, char *name)
2686 {
2687 struct module *mod;
2688
2689 preempt_disable();
2690 list_for_each_entry_rcu(mod, &modules, list) {
2691 if (within_module_init(addr, mod) ||
2692 within_module_core(addr, mod)) {
2693 const char *sym;
2694
2695 sym = get_ksymbol(mod, addr, size, offset);
2696 if (!sym)
2697 goto out;
2698 if (modname)
2699 strlcpy(modname, mod->name, MODULE_NAME_LEN);
2700 if (name)
2701 strlcpy(name, sym, KSYM_NAME_LEN);
2702 preempt_enable();
2703 return 0;
2704 }
2705 }
2706 out:
2707 preempt_enable();
2708 return -ERANGE;
2709 }
2710
2711 int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2712 char *name, char *module_name, int *exported)
2713 {
2714 struct module *mod;
2715
2716 preempt_disable();
2717 list_for_each_entry_rcu(mod, &modules, list) {
2718 if (symnum < mod->num_symtab) {
2719 *value = mod->symtab[symnum].st_value;
2720 *type = mod->symtab[symnum].st_info;
2721 strlcpy(name, mod->strtab + mod->symtab[symnum].st_name,
2722 KSYM_NAME_LEN);
2723 strlcpy(module_name, mod->name, MODULE_NAME_LEN);
2724 *exported = is_exported(name, *value, mod);
2725 preempt_enable();
2726 return 0;
2727 }
2728 symnum -= mod->num_symtab;
2729 }
2730 preempt_enable();
2731 return -ERANGE;
2732 }
2733
2734 static unsigned long mod_find_symname(struct module *mod, const char *name)
2735 {
2736 unsigned int i;
2737
2738 for (i = 0; i < mod->num_symtab; i++)
2739 if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
2740 mod->symtab[i].st_info != 'U')
2741 return mod->symtab[i].st_value;
2742 return 0;
2743 }
2744
2745 /* Look for this name: can be of form module:name. */
2746 unsigned long module_kallsyms_lookup_name(const char *name)
2747 {
2748 struct module *mod;
2749 char *colon;
2750 unsigned long ret = 0;
2751
2752 /* Don't lock: we're in enough trouble already. */
2753 preempt_disable();
2754 if ((colon = strchr(name, ':')) != NULL) {
2755 *colon = '\0';
2756 if ((mod = find_module(name)) != NULL)
2757 ret = mod_find_symname(mod, colon+1);
2758 *colon = ':';
2759 } else {
2760 list_for_each_entry_rcu(mod, &modules, list)
2761 if ((ret = mod_find_symname(mod, name)) != 0)
2762 break;
2763 }
2764 preempt_enable();
2765 return ret;
2766 }
2767
2768 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2769 struct module *, unsigned long),
2770 void *data)
2771 {
2772 struct module *mod;
2773 unsigned int i;
2774 int ret;
2775
2776 list_for_each_entry(mod, &modules, list) {
2777 for (i = 0; i < mod->num_symtab; i++) {
2778 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2779 mod, mod->symtab[i].st_value);
2780 if (ret != 0)
2781 return ret;
2782 }
2783 }
2784 return 0;
2785 }
2786 #endif /* CONFIG_KALLSYMS */
2787
2788 static char *module_flags(struct module *mod, char *buf)
2789 {
2790 int bx = 0;
2791
2792 if (mod->taints ||
2793 mod->state == MODULE_STATE_GOING ||
2794 mod->state == MODULE_STATE_COMING) {
2795 buf[bx++] = '(';
2796 if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
2797 buf[bx++] = 'P';
2798 if (mod->taints & (1 << TAINT_FORCED_MODULE))
2799 buf[bx++] = 'F';
2800 if (mod->taints & (1 << TAINT_CRAP))
2801 buf[bx++] = 'C';
2802 /*
2803 * TAINT_FORCED_RMMOD: could be added.
2804 * TAINT_UNSAFE_SMP, TAINT_MACHINE_CHECK, TAINT_BAD_PAGE don't
2805 * apply to modules.
2806 */
2807
2808 /* Show a - for module-is-being-unloaded */
2809 if (mod->state == MODULE_STATE_GOING)
2810 buf[bx++] = '-';
2811 /* Show a + for module-is-being-loaded */
2812 if (mod->state == MODULE_STATE_COMING)
2813 buf[bx++] = '+';
2814 buf[bx++] = ')';
2815 }
2816 buf[bx] = '\0';
2817
2818 return buf;
2819 }
2820
2821 #ifdef CONFIG_PROC_FS
2822 /* Called by the /proc file system to return a list of modules. */
2823 static void *m_start(struct seq_file *m, loff_t *pos)
2824 {
2825 mutex_lock(&module_mutex);
2826 return seq_list_start(&modules, *pos);
2827 }
2828
2829 static void *m_next(struct seq_file *m, void *p, loff_t *pos)
2830 {
2831 return seq_list_next(p, &modules, pos);
2832 }
2833
2834 static void m_stop(struct seq_file *m, void *p)
2835 {
2836 mutex_unlock(&module_mutex);
2837 }
2838
2839 static int m_show(struct seq_file *m, void *p)
2840 {
2841 struct module *mod = list_entry(p, struct module, list);
2842 char buf[8];
2843
2844 seq_printf(m, "%s %u",
2845 mod->name, mod->init_size + mod->core_size);
2846 print_unload_info(m, mod);
2847
2848 /* Informative for users. */
2849 seq_printf(m, " %s",
2850 mod->state == MODULE_STATE_GOING ? "Unloading":
2851 mod->state == MODULE_STATE_COMING ? "Loading":
2852 "Live");
2853 /* Used by oprofile and other similar tools. */
2854 seq_printf(m, " 0x%p", mod->module_core);
2855
2856 /* Taints info */
2857 if (mod->taints)
2858 seq_printf(m, " %s", module_flags(mod, buf));
2859
2860 seq_printf(m, "\n");
2861 return 0;
2862 }
2863
2864 /* Format: modulename size refcount deps address
2865
2866 Where refcount is a number or -, and deps is a comma-separated list
2867 of depends or -.
2868 */
2869 static const struct seq_operations modules_op = {
2870 .start = m_start,
2871 .next = m_next,
2872 .stop = m_stop,
2873 .show = m_show
2874 };
2875
2876 static int modules_open(struct inode *inode, struct file *file)
2877 {
2878 return seq_open(file, &modules_op);
2879 }
2880
2881 static const struct file_operations proc_modules_operations = {
2882 .open = modules_open,
2883 .read = seq_read,
2884 .llseek = seq_lseek,
2885 .release = seq_release,
2886 };
2887
2888 static int __init proc_modules_init(void)
2889 {
2890 proc_create("modules", 0, NULL, &proc_modules_operations);
2891 return 0;
2892 }
2893 module_init(proc_modules_init);
2894 #endif
2895
2896 /* Given an address, look for it in the module exception tables. */
2897 const struct exception_table_entry *search_module_extables(unsigned long addr)
2898 {
2899 const struct exception_table_entry *e = NULL;
2900 struct module *mod;
2901
2902 preempt_disable();
2903 list_for_each_entry_rcu(mod, &modules, list) {
2904 if (mod->num_exentries == 0)
2905 continue;
2906
2907 e = search_extable(mod->extable,
2908 mod->extable + mod->num_exentries - 1,
2909 addr);
2910 if (e)
2911 break;
2912 }
2913 preempt_enable();
2914
2915 /* Now, if we found one, we are running inside it now, hence
2916 we cannot unload the module, hence no refcnt needed. */
2917 return e;
2918 }
2919
2920 /*
2921 * is_module_address - is this address inside a module?
2922 * @addr: the address to check.
2923 *
2924 * See is_module_text_address() if you simply want to see if the address
2925 * is code (not data).
2926 */
2927 bool is_module_address(unsigned long addr)
2928 {
2929 bool ret;
2930
2931 preempt_disable();
2932 ret = __module_address(addr) != NULL;
2933 preempt_enable();
2934
2935 return ret;
2936 }
2937
2938 /*
2939 * __module_address - get the module which contains an address.
2940 * @addr: the address.
2941 *
2942 * Must be called with preempt disabled or module mutex held so that
2943 * module doesn't get freed during this.
2944 */
2945 struct module *__module_address(unsigned long addr)
2946 {
2947 struct module *mod;
2948
2949 if (addr < module_addr_min || addr > module_addr_max)
2950 return NULL;
2951
2952 list_for_each_entry_rcu(mod, &modules, list)
2953 if (within_module_core(addr, mod)
2954 || within_module_init(addr, mod))
2955 return mod;
2956 return NULL;
2957 }
2958 EXPORT_SYMBOL_GPL(__module_address);
2959
2960 /*
2961 * is_module_text_address - is this address inside module code?
2962 * @addr: the address to check.
2963 *
2964 * See is_module_address() if you simply want to see if the address is
2965 * anywhere in a module. See kernel_text_address() for testing if an
2966 * address corresponds to kernel or module code.
2967 */
2968 bool is_module_text_address(unsigned long addr)
2969 {
2970 bool ret;
2971
2972 preempt_disable();
2973 ret = __module_text_address(addr) != NULL;
2974 preempt_enable();
2975
2976 return ret;
2977 }
2978
2979 /*
2980 * __module_text_address - get the module whose code contains an address.
2981 * @addr: the address.
2982 *
2983 * Must be called with preempt disabled or module mutex held so that
2984 * module doesn't get freed during this.
2985 */
2986 struct module *__module_text_address(unsigned long addr)
2987 {
2988 struct module *mod = __module_address(addr);
2989 if (mod) {
2990 /* Make sure it's within the text section. */
2991 if (!within(addr, mod->module_init, mod->init_text_size)
2992 && !within(addr, mod->module_core, mod->core_text_size))
2993 mod = NULL;
2994 }
2995 return mod;
2996 }
2997 EXPORT_SYMBOL_GPL(__module_text_address);
2998
2999 /* Don't grab lock, we're oopsing. */
3000 void print_modules(void)
3001 {
3002 struct module *mod;
3003 char buf[8];
3004
3005 printk(KERN_DEFAULT "Modules linked in:");
3006 /* Most callers should already have preempt disabled, but make sure */
3007 preempt_disable();
3008 list_for_each_entry_rcu(mod, &modules, list)
3009 printk(" %s%s", mod->name, module_flags(mod, buf));
3010 preempt_enable();
3011 if (last_unloaded_module[0])
3012 printk(" [last unloaded: %s]", last_unloaded_module);
3013 printk("\n");
3014 }
3015
3016 #ifdef CONFIG_MODVERSIONS
3017 /* Generate the signature for all relevant module structures here.
3018 * If these change, we don't want to try to parse the module. */
3019 void module_layout(struct module *mod,
3020 struct modversion_info *ver,
3021 struct kernel_param *kp,
3022 struct kernel_symbol *ks,
3023 struct tracepoint *tp)
3024 {
3025 }
3026 EXPORT_SYMBOL(module_layout);
3027 #endif
3028
3029 #ifdef CONFIG_TRACEPOINTS
3030 void module_update_tracepoints(void)
3031 {
3032 struct module *mod;
3033
3034 mutex_lock(&module_mutex);
3035 list_for_each_entry(mod, &modules, list)
3036 if (!mod->taints)
3037 tracepoint_update_probe_range(mod->tracepoints,
3038 mod->tracepoints + mod->num_tracepoints);
3039 mutex_unlock(&module_mutex);
3040 }
3041
3042 /*
3043 * Returns 0 if current not found.
3044 * Returns 1 if current found.
3045 */
3046 int module_get_iter_tracepoints(struct tracepoint_iter *iter)
3047 {
3048 struct module *iter_mod;
3049 int found = 0;
3050
3051 mutex_lock(&module_mutex);
3052 list_for_each_entry(iter_mod, &modules, list) {
3053 if (!iter_mod->taints) {
3054 /*
3055 * Sorted module list
3056 */
3057 if (iter_mod < iter->module)
3058 continue;
3059 else if (iter_mod > iter->module)
3060 iter->tracepoint = NULL;
3061 found = tracepoint_get_iter_range(&iter->tracepoint,
3062 iter_mod->tracepoints,
3063 iter_mod->tracepoints
3064 + iter_mod->num_tracepoints);
3065 if (found) {
3066 iter->module = iter_mod;
3067 break;
3068 }
3069 }
3070 }
3071 mutex_unlock(&module_mutex);
3072 return found;
3073 }
3074 #endif
This page took 0.094624 seconds and 5 git commands to generate.