aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c406
1 files changed, 249 insertions, 157 deletions
diff --git a/kernel/module.c b/kernel/module.c
index c9332c90d5a0..e797812a4d95 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -51,6 +51,7 @@
51#include <linux/tracepoint.h> 51#include <linux/tracepoint.h>
52#include <linux/ftrace.h> 52#include <linux/ftrace.h>
53#include <linux/async.h> 53#include <linux/async.h>
54#include <linux/percpu.h>
54 55
55#if 0 56#if 0
56#define DEBUGP printk 57#define DEBUGP printk
@@ -67,7 +68,8 @@
67 68
68/* List of modules, protected by module_mutex or preempt_disable 69/* List of modules, protected by module_mutex or preempt_disable
69 * (delete uses stop_machine/add uses RCU list operations). */ 70 * (delete uses stop_machine/add uses RCU list operations). */
70static DEFINE_MUTEX(module_mutex); 71DEFINE_MUTEX(module_mutex);
72EXPORT_SYMBOL_GPL(module_mutex);
71static LIST_HEAD(modules); 73static LIST_HEAD(modules);
72 74
73/* Waiting for a module to finish initializing? */ 75/* Waiting for a module to finish initializing? */
@@ -75,7 +77,7 @@ static DECLARE_WAIT_QUEUE_HEAD(module_wq);
75 77
76static BLOCKING_NOTIFIER_HEAD(module_notify_list); 78static BLOCKING_NOTIFIER_HEAD(module_notify_list);
77 79
78/* Bounds of module allocation, for speeding __module_text_address */ 80/* Bounds of module allocation, for speeding __module_address */
79static unsigned long module_addr_min = -1UL, module_addr_max = 0; 81static unsigned long module_addr_min = -1UL, module_addr_max = 0;
80 82
81int register_module_notifier(struct notifier_block * nb) 83int register_module_notifier(struct notifier_block * nb)
@@ -185,17 +187,6 @@ extern const unsigned long __start___kcrctab_unused_gpl[];
185#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) 187#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
186#endif 188#endif
187 189
188struct symsearch {
189 const struct kernel_symbol *start, *stop;
190 const unsigned long *crcs;
191 enum {
192 NOT_GPL_ONLY,
193 GPL_ONLY,
194 WILL_BE_GPL_ONLY,
195 } licence;
196 bool unused;
197};
198
199static bool each_symbol_in_section(const struct symsearch *arr, 190static bool each_symbol_in_section(const struct symsearch *arr,
200 unsigned int arrsize, 191 unsigned int arrsize,
201 struct module *owner, 192 struct module *owner,
@@ -216,10 +207,8 @@ static bool each_symbol_in_section(const struct symsearch *arr,
216} 207}
217 208
218/* Returns true as soon as fn returns true, otherwise false. */ 209/* Returns true as soon as fn returns true, otherwise false. */
219static bool each_symbol(bool (*fn)(const struct symsearch *arr, 210bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
220 struct module *owner, 211 unsigned int symnum, void *data), void *data)
221 unsigned int symnum, void *data),
222 void *data)
223{ 212{
224 struct module *mod; 213 struct module *mod;
225 const struct symsearch arr[] = { 214 const struct symsearch arr[] = {
@@ -272,6 +261,7 @@ static bool each_symbol(bool (*fn)(const struct symsearch *arr,
272 } 261 }
273 return false; 262 return false;
274} 263}
264EXPORT_SYMBOL_GPL(each_symbol);
275 265
276struct find_symbol_arg { 266struct find_symbol_arg {
277 /* Input */ 267 /* Input */
@@ -282,7 +272,7 @@ struct find_symbol_arg {
282 /* Output */ 272 /* Output */
283 struct module *owner; 273 struct module *owner;
284 const unsigned long *crc; 274 const unsigned long *crc;
285 unsigned long value; 275 const struct kernel_symbol *sym;
286}; 276};
287 277
288static bool find_symbol_in_section(const struct symsearch *syms, 278static bool find_symbol_in_section(const struct symsearch *syms,
@@ -323,17 +313,17 @@ static bool find_symbol_in_section(const struct symsearch *syms,
323 313
324 fsa->owner = owner; 314 fsa->owner = owner;
325 fsa->crc = symversion(syms->crcs, symnum); 315 fsa->crc = symversion(syms->crcs, symnum);
326 fsa->value = syms->start[symnum].value; 316 fsa->sym = &syms->start[symnum];
327 return true; 317 return true;
328} 318}
329 319
330/* Find a symbol, return value, (optional) crc and (optional) module 320/* Find a symbol and return it, along with, (optional) crc and
331 * which owns it */ 321 * (optional) module which owns it */
332static unsigned long find_symbol(const char *name, 322const struct kernel_symbol *find_symbol(const char *name,
333 struct module **owner, 323 struct module **owner,
334 const unsigned long **crc, 324 const unsigned long **crc,
335 bool gplok, 325 bool gplok,
336 bool warn) 326 bool warn)
337{ 327{
338 struct find_symbol_arg fsa; 328 struct find_symbol_arg fsa;
339 329
@@ -346,15 +336,16 @@ static unsigned long find_symbol(const char *name,
346 *owner = fsa.owner; 336 *owner = fsa.owner;
347 if (crc) 337 if (crc)
348 *crc = fsa.crc; 338 *crc = fsa.crc;
349 return fsa.value; 339 return fsa.sym;
350 } 340 }
351 341
352 DEBUGP("Failed to find symbol %s\n", name); 342 DEBUGP("Failed to find symbol %s\n", name);
353 return -ENOENT; 343 return NULL;
354} 344}
345EXPORT_SYMBOL_GPL(find_symbol);
355 346
356/* Search for module by name: must hold module_mutex. */ 347/* Search for module by name: must hold module_mutex. */
357static struct module *find_module(const char *name) 348struct module *find_module(const char *name)
358{ 349{
359 struct module *mod; 350 struct module *mod;
360 351
@@ -364,8 +355,37 @@ static struct module *find_module(const char *name)
364 } 355 }
365 return NULL; 356 return NULL;
366} 357}
358EXPORT_SYMBOL_GPL(find_module);
367 359
368#ifdef CONFIG_SMP 360#ifdef CONFIG_SMP
361
362#ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
363
364static void *percpu_modalloc(unsigned long size, unsigned long align,
365 const char *name)
366{
367 void *ptr;
368
369 if (align > PAGE_SIZE) {
370 printk(KERN_WARNING "%s: per-cpu alignment %li > %li\n",
371 name, align, PAGE_SIZE);
372 align = PAGE_SIZE;
373 }
374
375 ptr = __alloc_reserved_percpu(size, align);
376 if (!ptr)
377 printk(KERN_WARNING
378 "Could not allocate %lu bytes percpu data\n", size);
379 return ptr;
380}
381
382static void percpu_modfree(void *freeme)
383{
384 free_percpu(freeme);
385}
386
387#else /* ... !CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
388
369/* Number of blocks used and allocated. */ 389/* Number of blocks used and allocated. */
370static unsigned int pcpu_num_used, pcpu_num_allocated; 390static unsigned int pcpu_num_used, pcpu_num_allocated;
371/* Size of each block. -ve means used. */ 391/* Size of each block. -ve means used. */
@@ -480,21 +500,6 @@ static void percpu_modfree(void *freeme)
480 } 500 }
481} 501}
482 502
483static unsigned int find_pcpusec(Elf_Ehdr *hdr,
484 Elf_Shdr *sechdrs,
485 const char *secstrings)
486{
487 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
488}
489
490static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
491{
492 int cpu;
493
494 for_each_possible_cpu(cpu)
495 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
496}
497
498static int percpu_modinit(void) 503static int percpu_modinit(void)
499{ 504{
500 pcpu_num_used = 2; 505 pcpu_num_used = 2;
@@ -513,7 +518,26 @@ static int percpu_modinit(void)
513 return 0; 518 return 0;
514} 519}
515__initcall(percpu_modinit); 520__initcall(percpu_modinit);
521
522#endif /* CONFIG_HAVE_DYNAMIC_PER_CPU_AREA */
523
524static unsigned int find_pcpusec(Elf_Ehdr *hdr,
525 Elf_Shdr *sechdrs,
526 const char *secstrings)
527{
528 return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
529}
530
531static void percpu_modcopy(void *pcpudest, const void *from, unsigned long size)
532{
533 int cpu;
534
535 for_each_possible_cpu(cpu)
536 memcpy(pcpudest + per_cpu_offset(cpu), from, size);
537}
538
516#else /* ... !CONFIG_SMP */ 539#else /* ... !CONFIG_SMP */
540
517static inline void *percpu_modalloc(unsigned long size, unsigned long align, 541static inline void *percpu_modalloc(unsigned long size, unsigned long align,
518 const char *name) 542 const char *name)
519{ 543{
@@ -535,6 +559,7 @@ static inline void percpu_modcopy(void *pcpudst, const void *src,
535 /* pcpusec should be 0, and size of that section should be 0. */ 559 /* pcpusec should be 0, and size of that section should be 0. */
536 BUG_ON(size != 0); 560 BUG_ON(size != 0);
537} 561}
562
538#endif /* CONFIG_SMP */ 563#endif /* CONFIG_SMP */
539 564
540#define MODINFO_ATTR(field) \ 565#define MODINFO_ATTR(field) \
@@ -573,13 +598,13 @@ static char last_unloaded_module[MODULE_NAME_LEN+1];
573/* Init the unload section of the module. */ 598/* Init the unload section of the module. */
574static void module_unload_init(struct module *mod) 599static void module_unload_init(struct module *mod)
575{ 600{
576 unsigned int i; 601 int cpu;
577 602
578 INIT_LIST_HEAD(&mod->modules_which_use_me); 603 INIT_LIST_HEAD(&mod->modules_which_use_me);
579 for (i = 0; i < NR_CPUS; i++) 604 for_each_possible_cpu(cpu)
580 local_set(&mod->ref[i].count, 0); 605 local_set(__module_ref_addr(mod, cpu), 0);
581 /* Hold reference count during initialization. */ 606 /* Hold reference count during initialization. */
582 local_set(&mod->ref[raw_smp_processor_id()].count, 1); 607 local_set(__module_ref_addr(mod, raw_smp_processor_id()), 1);
583 /* Backwards compatibility macros put refcount during init. */ 608 /* Backwards compatibility macros put refcount during init. */
584 mod->waiter = current; 609 mod->waiter = current;
585} 610}
@@ -607,7 +632,7 @@ static int already_uses(struct module *a, struct module *b)
607} 632}
608 633
609/* Module a uses b */ 634/* Module a uses b */
610static int use_module(struct module *a, struct module *b) 635int use_module(struct module *a, struct module *b)
611{ 636{
612 struct module_use *use; 637 struct module_use *use;
613 int no_warn, err; 638 int no_warn, err;
@@ -640,6 +665,7 @@ static int use_module(struct module *a, struct module *b)
640 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name); 665 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
641 return 1; 666 return 1;
642} 667}
668EXPORT_SYMBOL_GPL(use_module);
643 669
644/* Clear the unload stuff of the module. */ 670/* Clear the unload stuff of the module. */
645static void module_unload_free(struct module *mod) 671static void module_unload_free(struct module *mod)
@@ -717,10 +743,11 @@ static int try_stop_module(struct module *mod, int flags, int *forced)
717 743
718unsigned int module_refcount(struct module *mod) 744unsigned int module_refcount(struct module *mod)
719{ 745{
720 unsigned int i, total = 0; 746 unsigned int total = 0;
747 int cpu;
721 748
722 for (i = 0; i < NR_CPUS; i++) 749 for_each_possible_cpu(cpu)
723 total += local_read(&mod->ref[i].count); 750 total += local_read(__module_ref_addr(mod, cpu));
724 return total; 751 return total;
725} 752}
726EXPORT_SYMBOL(module_refcount); 753EXPORT_SYMBOL(module_refcount);
@@ -743,8 +770,8 @@ static void wait_for_zero_refcount(struct module *mod)
743 mutex_lock(&module_mutex); 770 mutex_lock(&module_mutex);
744} 771}
745 772
746asmlinkage long 773SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
747sys_delete_module(const char __user *name_user, unsigned int flags) 774 unsigned int, flags)
748{ 775{
749 struct module *mod; 776 struct module *mod;
750 char name[MODULE_NAME_LEN]; 777 char name[MODULE_NAME_LEN];
@@ -821,7 +848,7 @@ sys_delete_module(const char __user *name_user, unsigned int flags)
821 mutex_lock(&module_mutex); 848 mutex_lock(&module_mutex);
822 /* Store the name of the last unloaded module for diagnostic purposes */ 849 /* Store the name of the last unloaded module for diagnostic purposes */
823 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); 850 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
824 unregister_dynamic_debug_module(mod->name); 851 ddebug_remove_module(mod->name);
825 free_module(mod); 852 free_module(mod);
826 853
827 out: 854 out:
@@ -859,7 +886,7 @@ void __symbol_put(const char *symbol)
859 struct module *owner; 886 struct module *owner;
860 887
861 preempt_disable(); 888 preempt_disable();
862 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false))) 889 if (!find_symbol(symbol, &owner, NULL, true, false))
863 BUG(); 890 BUG();
864 module_put(owner); 891 module_put(owner);
865 preempt_enable(); 892 preempt_enable();
@@ -873,8 +900,10 @@ void symbol_put_addr(void *addr)
873 if (core_kernel_text((unsigned long)addr)) 900 if (core_kernel_text((unsigned long)addr))
874 return; 901 return;
875 902
876 if (!(modaddr = module_text_address((unsigned long)addr))) 903 /* module_text_address is safe here: we're supposed to have reference
877 BUG(); 904 * to module from symbol_get, so it can't go away. */
905 modaddr = __module_text_address((unsigned long)addr);
906 BUG_ON(!modaddr);
878 module_put(modaddr); 907 module_put(modaddr);
879} 908}
880EXPORT_SYMBOL_GPL(symbol_put_addr); 909EXPORT_SYMBOL_GPL(symbol_put_addr);
@@ -894,7 +923,7 @@ void module_put(struct module *module)
894{ 923{
895 if (module) { 924 if (module) {
896 unsigned int cpu = get_cpu(); 925 unsigned int cpu = get_cpu();
897 local_dec(&module->ref[cpu].count); 926 local_dec(__module_ref_addr(module, cpu));
898 /* Maybe they're waiting for us to drop reference? */ 927 /* Maybe they're waiting for us to drop reference? */
899 if (unlikely(!module_is_live(module))) 928 if (unlikely(!module_is_live(module)))
900 wake_up_process(module->waiter); 929 wake_up_process(module->waiter);
@@ -914,10 +943,11 @@ static inline void module_unload_free(struct module *mod)
914{ 943{
915} 944}
916 945
917static inline int use_module(struct module *a, struct module *b) 946int use_module(struct module *a, struct module *b)
918{ 947{
919 return strong_try_module_get(b) == 0; 948 return strong_try_module_get(b) == 0;
920} 949}
950EXPORT_SYMBOL_GPL(use_module);
921 951
922static inline void module_unload_init(struct module *mod) 952static inline void module_unload_init(struct module *mod)
923{ 953{
@@ -960,12 +990,12 @@ static struct module_attribute *modinfo_attrs[] = {
960 990
961static const char vermagic[] = VERMAGIC_STRING; 991static const char vermagic[] = VERMAGIC_STRING;
962 992
963static int try_to_force_load(struct module *mod, const char *symname) 993static int try_to_force_load(struct module *mod, const char *reason)
964{ 994{
965#ifdef CONFIG_MODULE_FORCE_LOAD 995#ifdef CONFIG_MODULE_FORCE_LOAD
966 if (!test_taint(TAINT_FORCED_MODULE)) 996 if (!test_taint(TAINT_FORCED_MODULE))
967 printk("%s: no version for \"%s\" found: kernel tainted.\n", 997 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
968 mod->name, symname); 998 mod->name, reason);
969 add_taint_module(mod, TAINT_FORCED_MODULE); 999 add_taint_module(mod, TAINT_FORCED_MODULE);
970 return 0; 1000 return 0;
971#else 1001#else
@@ -1022,9 +1052,9 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1022{ 1052{
1023 const unsigned long *crc; 1053 const unsigned long *crc;
1024 1054
1025 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false))) 1055 if (!find_symbol("module_layout", NULL, &crc, true, false))
1026 BUG(); 1056 BUG();
1027 return check_version(sechdrs, versindex, "struct_module", mod, crc); 1057 return check_version(sechdrs, versindex, "module_layout", mod, crc);
1028} 1058}
1029 1059
1030/* First part is kernel version, which we ignore if module has crcs. */ 1060/* First part is kernel version, which we ignore if module has crcs. */
@@ -1063,25 +1093,25 @@ static inline int same_magic(const char *amagic, const char *bmagic,
1063 1093
1064/* Resolve a symbol for this module. I.e. if we find one, record usage. 1094/* Resolve a symbol for this module. I.e. if we find one, record usage.
1065 Must be holding module_mutex. */ 1095 Must be holding module_mutex. */
1066static unsigned long resolve_symbol(Elf_Shdr *sechdrs, 1096static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1067 unsigned int versindex, 1097 unsigned int versindex,
1068 const char *name, 1098 const char *name,
1069 struct module *mod) 1099 struct module *mod)
1070{ 1100{
1071 struct module *owner; 1101 struct module *owner;
1072 unsigned long ret; 1102 const struct kernel_symbol *sym;
1073 const unsigned long *crc; 1103 const unsigned long *crc;
1074 1104
1075 ret = find_symbol(name, &owner, &crc, 1105 sym = find_symbol(name, &owner, &crc,
1076 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1106 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1077 if (!IS_ERR_VALUE(ret)) { 1107 /* use_module can fail due to OOM,
1078 /* use_module can fail due to OOM, 1108 or module initialization or unloading */
1079 or module initialization or unloading */ 1109 if (sym) {
1080 if (!check_version(sechdrs, versindex, name, mod, crc) || 1110 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1081 !use_module(mod, owner)) 1111 !use_module(mod, owner))
1082 ret = -EINVAL; 1112 sym = NULL;
1083 } 1113 }
1084 return ret; 1114 return sym;
1085} 1115}
1086 1116
1087/* 1117/*
@@ -1456,6 +1486,9 @@ static void free_module(struct module *mod)
1456 /* Module unload stuff */ 1486 /* Module unload stuff */
1457 module_unload_free(mod); 1487 module_unload_free(mod);
1458 1488
1489 /* Free any allocated parameters. */
1490 destroy_params(mod->kp, mod->num_kp);
1491
1459 /* release any pointers to mcount in this module */ 1492 /* release any pointers to mcount in this module */
1460 ftrace_release(mod->module_core, mod->core_size); 1493 ftrace_release(mod->module_core, mod->core_size);
1461 1494
@@ -1464,7 +1497,10 @@ static void free_module(struct module *mod)
1464 kfree(mod->args); 1497 kfree(mod->args);
1465 if (mod->percpu) 1498 if (mod->percpu)
1466 percpu_modfree(mod->percpu); 1499 percpu_modfree(mod->percpu);
1467 1500#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
1501 if (mod->refptr)
1502 percpu_modfree(mod->refptr);
1503#endif
1468 /* Free lock-classes: */ 1504 /* Free lock-classes: */
1469 lockdep_free_key_range(mod->module_core, mod->core_size); 1505 lockdep_free_key_range(mod->module_core, mod->core_size);
1470 1506
@@ -1475,17 +1511,15 @@ static void free_module(struct module *mod)
1475void *__symbol_get(const char *symbol) 1511void *__symbol_get(const char *symbol)
1476{ 1512{
1477 struct module *owner; 1513 struct module *owner;
1478 unsigned long value; 1514 const struct kernel_symbol *sym;
1479 1515
1480 preempt_disable(); 1516 preempt_disable();
1481 value = find_symbol(symbol, &owner, NULL, true, true); 1517 sym = find_symbol(symbol, &owner, NULL, true, true);
1482 if (IS_ERR_VALUE(value)) 1518 if (sym && strong_try_module_get(owner))
1483 value = 0; 1519 sym = NULL;
1484 else if (strong_try_module_get(owner))
1485 value = 0;
1486 preempt_enable(); 1520 preempt_enable();
1487 1521
1488 return (void *)value; 1522 return sym ? (void *)sym->value : NULL;
1489} 1523}
1490EXPORT_SYMBOL_GPL(__symbol_get); 1524EXPORT_SYMBOL_GPL(__symbol_get);
1491 1525
@@ -1513,8 +1547,7 @@ static int verify_export_symbols(struct module *mod)
1513 1547
1514 for (i = 0; i < ARRAY_SIZE(arr); i++) { 1548 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1515 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) { 1549 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1516 if (!IS_ERR_VALUE(find_symbol(s->name, &owner, 1550 if (find_symbol(s->name, &owner, NULL, true, false)) {
1517 NULL, true, false))) {
1518 printk(KERN_ERR 1551 printk(KERN_ERR
1519 "%s: exports duplicate symbol %s" 1552 "%s: exports duplicate symbol %s"
1520 " (owned by %s)\n", 1553 " (owned by %s)\n",
@@ -1538,6 +1571,7 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1538 unsigned long secbase; 1571 unsigned long secbase;
1539 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1572 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1540 int ret = 0; 1573 int ret = 0;
1574 const struct kernel_symbol *ksym;
1541 1575
1542 for (i = 1; i < n; i++) { 1576 for (i = 1; i < n; i++) {
1543 switch (sym[i].st_shndx) { 1577 switch (sym[i].st_shndx) {
@@ -1557,13 +1591,14 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1557 break; 1591 break;
1558 1592
1559 case SHN_UNDEF: 1593 case SHN_UNDEF:
1560 sym[i].st_value 1594 ksym = resolve_symbol(sechdrs, versindex,
1561 = resolve_symbol(sechdrs, versindex, 1595 strtab + sym[i].st_name, mod);
1562 strtab + sym[i].st_name, mod);
1563
1564 /* Ok if resolved. */ 1596 /* Ok if resolved. */
1565 if (!IS_ERR_VALUE(sym[i].st_value)) 1597 if (ksym) {
1598 sym[i].st_value = ksym->value;
1566 break; 1599 break;
1600 }
1601
1567 /* Ok if weak. */ 1602 /* Ok if weak. */
1568 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1603 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1569 break; 1604 break;
@@ -1638,8 +1673,7 @@ static void layout_sections(struct module *mod,
1638 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1673 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1639 || (s->sh_flags & masks[m][1]) 1674 || (s->sh_flags & masks[m][1])
1640 || s->sh_entsize != ~0UL 1675 || s->sh_entsize != ~0UL
1641 || strncmp(secstrings + s->sh_name, 1676 || strstarts(secstrings + s->sh_name, ".init"))
1642 ".init", 5) == 0)
1643 continue; 1677 continue;
1644 s->sh_entsize = get_offset(mod, &mod->core_size, s, i); 1678 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1645 DEBUGP("\t%s\n", secstrings + s->sh_name); 1679 DEBUGP("\t%s\n", secstrings + s->sh_name);
@@ -1656,8 +1690,7 @@ static void layout_sections(struct module *mod,
1656 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1690 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1657 || (s->sh_flags & masks[m][1]) 1691 || (s->sh_flags & masks[m][1])
1658 || s->sh_entsize != ~0UL 1692 || s->sh_entsize != ~0UL
1659 || strncmp(secstrings + s->sh_name, 1693 || !strstarts(secstrings + s->sh_name, ".init"))
1660 ".init", 5) != 0)
1661 continue; 1694 continue;
1662 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i) 1695 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1663 | INIT_OFFSET_MASK); 1696 | INIT_OFFSET_MASK);
@@ -1790,8 +1823,7 @@ static char elf_type(const Elf_Sym *sym,
1790 else 1823 else
1791 return 'b'; 1824 return 'b';
1792 } 1825 }
1793 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name, 1826 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
1794 ".debug", strlen(".debug")) == 0)
1795 return 'n'; 1827 return 'n';
1796 return '?'; 1828 return '?';
1797} 1829}
@@ -1823,19 +1855,13 @@ static inline void add_kallsyms(struct module *mod,
1823} 1855}
1824#endif /* CONFIG_KALLSYMS */ 1856#endif /* CONFIG_KALLSYMS */
1825 1857
1826static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num) 1858static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1827{ 1859{
1828#ifdef CONFIG_DYNAMIC_PRINTK_DEBUG 1860#ifdef CONFIG_DYNAMIC_DEBUG
1829 unsigned int i; 1861 if (ddebug_add_module(debug, num, debug->modname))
1830 1862 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1831 for (i = 0; i < num; i++) { 1863 debug->modname);
1832 register_dynamic_debug_module(debug[i].modname, 1864#endif
1833 debug[i].type,
1834 debug[i].logical_modname,
1835 debug[i].flag_names,
1836 debug[i].hash, debug[i].hash2);
1837 }
1838#endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1839} 1865}
1840 1866
1841static void *module_alloc_update_bounds(unsigned long size) 1867static void *module_alloc_update_bounds(unsigned long size)
@@ -1866,8 +1892,7 @@ static noinline struct module *load_module(void __user *umod,
1866 unsigned int symindex = 0; 1892 unsigned int symindex = 0;
1867 unsigned int strindex = 0; 1893 unsigned int strindex = 0;
1868 unsigned int modindex, versindex, infoindex, pcpuindex; 1894 unsigned int modindex, versindex, infoindex, pcpuindex;
1869 unsigned int num_kp, num_mcount; 1895 unsigned int num_mcount;
1870 struct kernel_param *kp;
1871 struct module *mod; 1896 struct module *mod;
1872 long err = 0; 1897 long err = 0;
1873 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 1898 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
@@ -1884,12 +1909,6 @@ static noinline struct module *load_module(void __user *umod,
1884 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) 1909 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1885 return ERR_PTR(-ENOMEM); 1910 return ERR_PTR(-ENOMEM);
1886 1911
1887 /* Create stop_machine threads since the error path relies on
1888 * a non-failing stop_machine call. */
1889 err = stop_machine_create();
1890 if (err)
1891 goto free_hdr;
1892
1893 if (copy_from_user(hdr, umod, len) != 0) { 1912 if (copy_from_user(hdr, umod, len) != 0) {
1894 err = -EFAULT; 1913 err = -EFAULT;
1895 goto free_hdr; 1914 goto free_hdr;
@@ -1930,7 +1949,7 @@ static noinline struct module *load_module(void __user *umod,
1930 } 1949 }
1931#ifndef CONFIG_MODULE_UNLOAD 1950#ifndef CONFIG_MODULE_UNLOAD
1932 /* Don't load .exit sections */ 1951 /* Don't load .exit sections */
1933 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0) 1952 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
1934 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC; 1953 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1935#endif 1954#endif
1936 } 1955 }
@@ -1974,7 +1993,7 @@ static noinline struct module *load_module(void __user *umod,
1974 modmagic = get_modinfo(sechdrs, infoindex, "vermagic"); 1993 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1975 /* This is allowed: modprobe --force will invalidate it. */ 1994 /* This is allowed: modprobe --force will invalidate it. */
1976 if (!modmagic) { 1995 if (!modmagic) {
1977 err = try_to_force_load(mod, "magic"); 1996 err = try_to_force_load(mod, "bad vermagic");
1978 if (err) 1997 if (err)
1979 goto free_hdr; 1998 goto free_hdr;
1980 } else if (!same_magic(modmagic, vermagic, versindex)) { 1999 } else if (!same_magic(modmagic, vermagic, versindex)) {
@@ -2070,6 +2089,14 @@ static noinline struct module *load_module(void __user *umod,
2070 /* Module has been moved. */ 2089 /* Module has been moved. */
2071 mod = (void *)sechdrs[modindex].sh_addr; 2090 mod = (void *)sechdrs[modindex].sh_addr;
2072 2091
2092#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2093 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2094 mod->name);
2095 if (!mod->refptr) {
2096 err = -ENOMEM;
2097 goto free_init;
2098 }
2099#endif
2073 /* Now we've moved module, initialize linked lists, etc. */ 2100 /* Now we've moved module, initialize linked lists, etc. */
2074 module_unload_init(mod); 2101 module_unload_init(mod);
2075 2102
@@ -2104,8 +2131,8 @@ static noinline struct module *load_module(void __user *umod,
2104 2131
2105 /* Now we've got everything in the final locations, we can 2132 /* Now we've got everything in the final locations, we can
2106 * find optional sections. */ 2133 * find optional sections. */
2107 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp), 2134 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2108 &num_kp); 2135 sizeof(*mod->kp), &mod->num_kp);
2109 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab", 2136 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2110 sizeof(*mod->syms), &mod->num_syms); 2137 sizeof(*mod->syms), &mod->num_syms);
2111 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab"); 2138 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
@@ -2155,8 +2182,8 @@ static noinline struct module *load_module(void __user *umod,
2155 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs) 2182 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2156#endif 2183#endif
2157 ) { 2184 ) {
2158 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name); 2185 err = try_to_force_load(mod,
2159 err = try_to_force_load(mod, "nocrc"); 2186 "no versions for exported symbols");
2160 if (err) 2187 if (err)
2161 goto cleanup; 2188 goto cleanup;
2162 } 2189 }
@@ -2201,12 +2228,13 @@ static noinline struct module *load_module(void __user *umod,
2201 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings); 2228 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2202 2229
2203 if (!mod->taints) { 2230 if (!mod->taints) {
2204 struct mod_debug *debug; 2231 struct _ddebug *debug;
2205 unsigned int num_debug; 2232 unsigned int num_debug;
2206 2233
2207 debug = section_objs(hdr, sechdrs, secstrings, "__verbose", 2234 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2208 sizeof(*debug), &num_debug); 2235 sizeof(*debug), &num_debug);
2209 dynamic_printk_setup(debug, num_debug); 2236 if (debug)
2237 dynamic_debug_setup(debug, num_debug);
2210 } 2238 }
2211 2239
2212 /* sechdrs[0].sh_size is always zero */ 2240 /* sechdrs[0].sh_size is always zero */
@@ -2250,11 +2278,11 @@ static noinline struct module *load_module(void __user *umod,
2250 */ 2278 */
2251 list_add_rcu(&mod->list, &modules); 2279 list_add_rcu(&mod->list, &modules);
2252 2280
2253 err = parse_args(mod->name, mod->args, kp, num_kp, NULL); 2281 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2254 if (err < 0) 2282 if (err < 0)
2255 goto unlink; 2283 goto unlink;
2256 2284
2257 err = mod_sysfs_setup(mod, kp, num_kp); 2285 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2258 if (err < 0) 2286 if (err < 0)
2259 goto unlink; 2287 goto unlink;
2260 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2288 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
@@ -2263,12 +2291,13 @@ static noinline struct module *load_module(void __user *umod,
2263 /* Get rid of temporary copy */ 2291 /* Get rid of temporary copy */
2264 vfree(hdr); 2292 vfree(hdr);
2265 2293
2266 stop_machine_destroy();
2267 /* Done! */ 2294 /* Done! */
2268 return mod; 2295 return mod;
2269 2296
2270 unlink: 2297 unlink:
2271 stop_machine(__unlink_module, mod, NULL); 2298 /* Unlink carefully: kallsyms could be walking list. */
2299 list_del_rcu(&mod->list);
2300 synchronize_sched();
2272 module_arch_cleanup(mod); 2301 module_arch_cleanup(mod);
2273 cleanup: 2302 cleanup:
2274 kobject_del(&mod->mkobj.kobj); 2303 kobject_del(&mod->mkobj.kobj);
@@ -2276,9 +2305,14 @@ static noinline struct module *load_module(void __user *umod,
2276 ftrace_release(mod->module_core, mod->core_size); 2305 ftrace_release(mod->module_core, mod->core_size);
2277 free_unload: 2306 free_unload:
2278 module_unload_free(mod); 2307 module_unload_free(mod);
2308#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2309 free_init:
2310 percpu_modfree(mod->refptr);
2311#endif
2279 module_free(mod, mod->module_init); 2312 module_free(mod, mod->module_init);
2280 free_core: 2313 free_core:
2281 module_free(mod, mod->module_core); 2314 module_free(mod, mod->module_core);
2315 /* mod will be freed with core. Don't access it beyond this line! */
2282 free_percpu: 2316 free_percpu:
2283 if (percpu) 2317 if (percpu)
2284 percpu_modfree(percpu); 2318 percpu_modfree(percpu);
@@ -2286,7 +2320,6 @@ static noinline struct module *load_module(void __user *umod,
2286 kfree(args); 2320 kfree(args);
2287 free_hdr: 2321 free_hdr:
2288 vfree(hdr); 2322 vfree(hdr);
2289 stop_machine_destroy();
2290 return ERR_PTR(err); 2323 return ERR_PTR(err);
2291 2324
2292 truncated: 2325 truncated:
@@ -2296,10 +2329,8 @@ static noinline struct module *load_module(void __user *umod,
2296} 2329}
2297 2330
2298/* This is where the real work happens */ 2331/* This is where the real work happens */
2299asmlinkage long 2332SYSCALL_DEFINE3(init_module, void __user *, umod,
2300sys_init_module(void __user *umod, 2333 unsigned long, len, const char __user *, uargs)
2301 unsigned long len,
2302 const char __user *uargs)
2303{ 2334{
2304 struct module *mod; 2335 struct module *mod;
2305 int ret = 0; 2336 int ret = 0;
@@ -2357,6 +2388,9 @@ sys_init_module(void __user *umod,
2357 blocking_notifier_call_chain(&module_notify_list, 2388 blocking_notifier_call_chain(&module_notify_list,
2358 MODULE_STATE_LIVE, mod); 2389 MODULE_STATE_LIVE, mod);
2359 2390
2391 /* We need to finish all async code before the module init sequence is done */
2392 async_synchronize_full();
2393
2360 mutex_lock(&module_mutex); 2394 mutex_lock(&module_mutex);
2361 /* Drop initial reference. */ 2395 /* Drop initial reference. */
2362 module_put(mod); 2396 module_put(mod);
@@ -2565,6 +2599,25 @@ unsigned long module_kallsyms_lookup_name(const char *name)
2565 preempt_enable(); 2599 preempt_enable();
2566 return ret; 2600 return ret;
2567} 2601}
2602
2603int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2604 struct module *, unsigned long),
2605 void *data)
2606{
2607 struct module *mod;
2608 unsigned int i;
2609 int ret;
2610
2611 list_for_each_entry(mod, &modules, list) {
2612 for (i = 0; i < mod->num_symtab; i++) {
2613 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2614 mod, mod->symtab[i].st_value);
2615 if (ret != 0)
2616 return ret;
2617 }
2618 }
2619 return 0;
2620}
2568#endif /* CONFIG_KALLSYMS */ 2621#endif /* CONFIG_KALLSYMS */
2569 2622
2570static char *module_flags(struct module *mod, char *buf) 2623static char *module_flags(struct module *mod, char *buf)
@@ -2700,29 +2753,31 @@ const struct exception_table_entry *search_module_extables(unsigned long addr)
2700} 2753}
2701 2754
2702/* 2755/*
2703 * Is this a valid module address? 2756 * is_module_address - is this address inside a module?
2757 * @addr: the address to check.
2758 *
2759 * See is_module_text_address() if you simply want to see if the address
2760 * is code (not data).
2704 */ 2761 */
2705int is_module_address(unsigned long addr) 2762bool is_module_address(unsigned long addr)
2706{ 2763{
2707 struct module *mod; 2764 bool ret;
2708 2765
2709 preempt_disable(); 2766 preempt_disable();
2710 2767 ret = __module_address(addr) != NULL;
2711 list_for_each_entry_rcu(mod, &modules, list) {
2712 if (within_module_core(addr, mod)) {
2713 preempt_enable();
2714 return 1;
2715 }
2716 }
2717
2718 preempt_enable(); 2768 preempt_enable();
2719 2769
2720 return 0; 2770 return ret;
2721} 2771}
2722 2772
2723 2773/*
2724/* Is this a valid kernel address? */ 2774 * __module_address - get the module which contains an address.
2725__notrace_funcgraph struct module *__module_text_address(unsigned long addr) 2775 * @addr: the address.
2776 *
2777 * Must be called with preempt disabled or module mutex held so that
2778 * module doesn't get freed during this.
2779 */
2780struct module *__module_address(unsigned long addr)
2726{ 2781{
2727 struct module *mod; 2782 struct module *mod;
2728 2783
@@ -2730,22 +2785,51 @@ __notrace_funcgraph struct module *__module_text_address(unsigned long addr)
2730 return NULL; 2785 return NULL;
2731 2786
2732 list_for_each_entry_rcu(mod, &modules, list) 2787 list_for_each_entry_rcu(mod, &modules, list)
2733 if (within(addr, mod->module_init, mod->init_text_size) 2788 if (within_module_core(addr, mod)
2734 || within(addr, mod->module_core, mod->core_text_size)) 2789 || within_module_init(addr, mod))
2735 return mod; 2790 return mod;
2736 return NULL; 2791 return NULL;
2737} 2792}
2793EXPORT_SYMBOL_GPL(__module_address);
2738 2794
2739struct module *module_text_address(unsigned long addr) 2795/*
2796 * is_module_text_address - is this address inside module code?
2797 * @addr: the address to check.
2798 *
2799 * See is_module_address() if you simply want to see if the address is
2800 * anywhere in a module. See kernel_text_address() for testing if an
2801 * address corresponds to kernel or module code.
2802 */
2803bool is_module_text_address(unsigned long addr)
2740{ 2804{
2741 struct module *mod; 2805 bool ret;
2742 2806
2743 preempt_disable(); 2807 preempt_disable();
2744 mod = __module_text_address(addr); 2808 ret = __module_text_address(addr) != NULL;
2745 preempt_enable(); 2809 preempt_enable();
2746 2810
2811 return ret;
2812}
2813
2814/*
2815 * __module_text_address - get the module whose code contains an address.
2816 * @addr: the address.
2817 *
2818 * Must be called with preempt disabled or module mutex held so that
2819 * module doesn't get freed during this.
2820 */
2821struct module *__module_text_address(unsigned long addr)
2822{
2823 struct module *mod = __module_address(addr);
2824 if (mod) {
2825 /* Make sure it's within the text section. */
2826 if (!within(addr, mod->module_init, mod->init_text_size)
2827 && !within(addr, mod->module_core, mod->core_text_size))
2828 mod = NULL;
2829 }
2747 return mod; 2830 return mod;
2748} 2831}
2832EXPORT_SYMBOL_GPL(__module_text_address);
2749 2833
2750/* Don't grab lock, we're oopsing. */ 2834/* Don't grab lock, we're oopsing. */
2751void print_modules(void) 2835void print_modules(void)
@@ -2765,9 +2849,17 @@ void print_modules(void)
2765} 2849}
2766 2850
2767#ifdef CONFIG_MODVERSIONS 2851#ifdef CONFIG_MODVERSIONS
2768/* Generate the signature for struct module here, too, for modversions. */ 2852/* Generate the signature for all relevant module structures here.
2769void struct_module(struct module *mod) { return; } 2853 * If these change, we don't want to try to parse the module. */
2770EXPORT_SYMBOL(struct_module); 2854void module_layout(struct module *mod,
2855 struct modversion_info *ver,
2856 struct kernel_param *kp,
2857 struct kernel_symbol *ks,
2858 struct marker *marker,
2859 struct tracepoint *tp)
2860{
2861}
2862EXPORT_SYMBOL(module_layout);
2771#endif 2863#endif
2772 2864
2773#ifdef CONFIG_MARKERS 2865#ifdef CONFIG_MARKERS