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