aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c384
1 files changed, 230 insertions, 154 deletions
diff --git a/kernel/module.c b/kernel/module.c
index ba22484a987e..05f014efa32c 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) \
@@ -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)
@@ -822,7 +848,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
822 mutex_lock(&module_mutex); 848 mutex_lock(&module_mutex);
823 /* Store the name of the last unloaded module for diagnostic purposes */ 849 /* Store the name of the last unloaded module for diagnostic purposes */
824 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); 850 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
825 unregister_dynamic_debug_module(mod->name); 851 ddebug_remove_module(mod->name);
826 free_module(mod); 852 free_module(mod);
827 853
828 out: 854 out:
@@ -860,7 +886,7 @@ void __symbol_put(const char *symbol)
860 struct module *owner; 886 struct module *owner;
861 887
862 preempt_disable(); 888 preempt_disable();
863 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false))) 889 if (!find_symbol(symbol, &owner, NULL, true, false))
864 BUG(); 890 BUG();
865 module_put(owner); 891 module_put(owner);
866 preempt_enable(); 892 preempt_enable();
@@ -874,8 +900,10 @@ void symbol_put_addr(void *addr)
874 if (core_kernel_text((unsigned long)addr)) 900 if (core_kernel_text((unsigned long)addr))
875 return; 901 return;
876 902
877 if (!(modaddr = module_text_address((unsigned long)addr))) 903 /* module_text_address is safe here: we're supposed to have reference
878 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);
879 module_put(modaddr); 907 module_put(modaddr);
880} 908}
881EXPORT_SYMBOL_GPL(symbol_put_addr); 909EXPORT_SYMBOL_GPL(symbol_put_addr);
@@ -915,10 +943,11 @@ static inline void module_unload_free(struct module *mod)
915{ 943{
916} 944}
917 945
918static inline int use_module(struct module *a, struct module *b) 946int use_module(struct module *a, struct module *b)
919{ 947{
920 return strong_try_module_get(b) == 0; 948 return strong_try_module_get(b) == 0;
921} 949}
950EXPORT_SYMBOL_GPL(use_module);
922 951
923static inline void module_unload_init(struct module *mod) 952static inline void module_unload_init(struct module *mod)
924{ 953{
@@ -961,12 +990,12 @@ static struct module_attribute *modinfo_attrs[] = {
961 990
962static const char vermagic[] = VERMAGIC_STRING; 991static const char vermagic[] = VERMAGIC_STRING;
963 992
964static int try_to_force_load(struct module *mod, const char *symname) 993static int try_to_force_load(struct module *mod, const char *reason)
965{ 994{
966#ifdef CONFIG_MODULE_FORCE_LOAD 995#ifdef CONFIG_MODULE_FORCE_LOAD
967 if (!test_taint(TAINT_FORCED_MODULE)) 996 if (!test_taint(TAINT_FORCED_MODULE))
968 printk("%s: no version for \"%s\" found: kernel tainted.\n", 997 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
969 mod->name, symname); 998 mod->name, reason);
970 add_taint_module(mod, TAINT_FORCED_MODULE); 999 add_taint_module(mod, TAINT_FORCED_MODULE);
971 return 0; 1000 return 0;
972#else 1001#else
@@ -1023,9 +1052,9 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1023{ 1052{
1024 const unsigned long *crc; 1053 const unsigned long *crc;
1025 1054
1026 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false))) 1055 if (!find_symbol("module_layout", NULL, &crc, true, false))
1027 BUG(); 1056 BUG();
1028 return check_version(sechdrs, versindex, "struct_module", mod, crc); 1057 return check_version(sechdrs, versindex, "module_layout", mod, crc);
1029} 1058}
1030 1059
1031/* 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. */
@@ -1064,25 +1093,25 @@ static inline int same_magic(const char *amagic, const char *bmagic,
1064 1093
1065/* 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.
1066 Must be holding module_mutex. */ 1095 Must be holding module_mutex. */
1067static unsigned long resolve_symbol(Elf_Shdr *sechdrs, 1096static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1068 unsigned int versindex, 1097 unsigned int versindex,
1069 const char *name, 1098 const char *name,
1070 struct module *mod) 1099 struct module *mod)
1071{ 1100{
1072 struct module *owner; 1101 struct module *owner;
1073 unsigned long ret; 1102 const struct kernel_symbol *sym;
1074 const unsigned long *crc; 1103 const unsigned long *crc;
1075 1104
1076 ret = find_symbol(name, &owner, &crc, 1105 sym = find_symbol(name, &owner, &crc,
1077 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1106 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1078 if (!IS_ERR_VALUE(ret)) { 1107 /* use_module can fail due to OOM,
1079 /* use_module can fail due to OOM, 1108 or module initialization or unloading */
1080 or module initialization or unloading */ 1109 if (sym) {
1081 if (!check_version(sechdrs, versindex, name, mod, crc) || 1110 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1082 !use_module(mod, owner)) 1111 !use_module(mod, owner))
1083 ret = -EINVAL; 1112 sym = NULL;
1084 } 1113 }
1085 return ret; 1114 return sym;
1086} 1115}
1087 1116
1088/* 1117/*
@@ -1457,6 +1486,9 @@ static void free_module(struct module *mod)
1457 /* Module unload stuff */ 1486 /* Module unload stuff */
1458 module_unload_free(mod); 1487 module_unload_free(mod);
1459 1488
1489 /* Free any allocated parameters. */
1490 destroy_params(mod->kp, mod->num_kp);
1491
1460 /* release any pointers to mcount in this module */ 1492 /* release any pointers to mcount in this module */
1461 ftrace_release(mod->module_core, mod->core_size); 1493 ftrace_release(mod->module_core, mod->core_size);
1462 1494
@@ -1479,17 +1511,15 @@ static void free_module(struct module *mod)
1479void *__symbol_get(const char *symbol) 1511void *__symbol_get(const char *symbol)
1480{ 1512{
1481 struct module *owner; 1513 struct module *owner;
1482 unsigned long value; 1514 const struct kernel_symbol *sym;
1483 1515
1484 preempt_disable(); 1516 preempt_disable();
1485 value = find_symbol(symbol, &owner, NULL, true, true); 1517 sym = find_symbol(symbol, &owner, NULL, true, true);
1486 if (IS_ERR_VALUE(value)) 1518 if (sym && strong_try_module_get(owner))
1487 value = 0; 1519 sym = NULL;
1488 else if (strong_try_module_get(owner))
1489 value = 0;
1490 preempt_enable(); 1520 preempt_enable();
1491 1521
1492 return (void *)value; 1522 return sym ? (void *)sym->value : NULL;
1493} 1523}
1494EXPORT_SYMBOL_GPL(__symbol_get); 1524EXPORT_SYMBOL_GPL(__symbol_get);
1495 1525
@@ -1517,8 +1547,7 @@ static int verify_export_symbols(struct module *mod)
1517 1547
1518 for (i = 0; i < ARRAY_SIZE(arr); i++) { 1548 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1519 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++) {
1520 if (!IS_ERR_VALUE(find_symbol(s->name, &owner, 1550 if (find_symbol(s->name, &owner, NULL, true, false)) {
1521 NULL, true, false))) {
1522 printk(KERN_ERR 1551 printk(KERN_ERR
1523 "%s: exports duplicate symbol %s" 1552 "%s: exports duplicate symbol %s"
1524 " (owned by %s)\n", 1553 " (owned by %s)\n",
@@ -1542,6 +1571,7 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1542 unsigned long secbase; 1571 unsigned long secbase;
1543 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1572 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1544 int ret = 0; 1573 int ret = 0;
1574 const struct kernel_symbol *ksym;
1545 1575
1546 for (i = 1; i < n; i++) { 1576 for (i = 1; i < n; i++) {
1547 switch (sym[i].st_shndx) { 1577 switch (sym[i].st_shndx) {
@@ -1561,13 +1591,14 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1561 break; 1591 break;
1562 1592
1563 case SHN_UNDEF: 1593 case SHN_UNDEF:
1564 sym[i].st_value 1594 ksym = resolve_symbol(sechdrs, versindex,
1565 = resolve_symbol(sechdrs, versindex, 1595 strtab + sym[i].st_name, mod);
1566 strtab + sym[i].st_name, mod);
1567
1568 /* Ok if resolved. */ 1596 /* Ok if resolved. */
1569 if (!IS_ERR_VALUE(sym[i].st_value)) 1597 if (ksym) {
1598 sym[i].st_value = ksym->value;
1570 break; 1599 break;
1600 }
1601
1571 /* Ok if weak. */ 1602 /* Ok if weak. */
1572 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1603 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1573 break; 1604 break;
@@ -1642,8 +1673,7 @@ static void layout_sections(struct module *mod,
1642 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1673 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1643 || (s->sh_flags & masks[m][1]) 1674 || (s->sh_flags & masks[m][1])
1644 || s->sh_entsize != ~0UL 1675 || s->sh_entsize != ~0UL
1645 || strncmp(secstrings + s->sh_name, 1676 || strstarts(secstrings + s->sh_name, ".init"))
1646 ".init", 5) == 0)
1647 continue; 1677 continue;
1648 s->sh_entsize = get_offset(mod, &mod->core_size, s, i); 1678 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1649 DEBUGP("\t%s\n", secstrings + s->sh_name); 1679 DEBUGP("\t%s\n", secstrings + s->sh_name);
@@ -1660,8 +1690,7 @@ static void layout_sections(struct module *mod,
1660 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1690 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1661 || (s->sh_flags & masks[m][1]) 1691 || (s->sh_flags & masks[m][1])
1662 || s->sh_entsize != ~0UL 1692 || s->sh_entsize != ~0UL
1663 || strncmp(secstrings + s->sh_name, 1693 || !strstarts(secstrings + s->sh_name, ".init"))
1664 ".init", 5) != 0)
1665 continue; 1694 continue;
1666 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i) 1695 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1667 | INIT_OFFSET_MASK); 1696 | INIT_OFFSET_MASK);
@@ -1794,8 +1823,7 @@ static char elf_type(const Elf_Sym *sym,
1794 else 1823 else
1795 return 'b'; 1824 return 'b';
1796 } 1825 }
1797 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name, 1826 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
1798 ".debug", strlen(".debug")) == 0)
1799 return 'n'; 1827 return 'n';
1800 return '?'; 1828 return '?';
1801} 1829}
@@ -1827,19 +1855,13 @@ static inline void add_kallsyms(struct module *mod,
1827} 1855}
1828#endif /* CONFIG_KALLSYMS */ 1856#endif /* CONFIG_KALLSYMS */
1829 1857
1830static void dynamic_printk_setup(struct mod_debug *debug, unsigned int num) 1858static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1831{ 1859{
1832#ifdef CONFIG_DYNAMIC_PRINTK_DEBUG 1860#ifdef CONFIG_DYNAMIC_DEBUG
1833 unsigned int i; 1861 if (ddebug_add_module(debug, num, debug->modname))
1834 1862 printk(KERN_ERR "dynamic debug error adding module: %s\n",
1835 for (i = 0; i < num; i++) { 1863 debug->modname);
1836 register_dynamic_debug_module(debug[i].modname, 1864#endif
1837 debug[i].type,
1838 debug[i].logical_modname,
1839 debug[i].flag_names,
1840 debug[i].hash, debug[i].hash2);
1841 }
1842#endif /* CONFIG_DYNAMIC_PRINTK_DEBUG */
1843} 1865}
1844 1866
1845static void *module_alloc_update_bounds(unsigned long size) 1867static void *module_alloc_update_bounds(unsigned long size)
@@ -1870,8 +1892,7 @@ static noinline struct module *load_module(void __user *umod,
1870 unsigned int symindex = 0; 1892 unsigned int symindex = 0;
1871 unsigned int strindex = 0; 1893 unsigned int strindex = 0;
1872 unsigned int modindex, versindex, infoindex, pcpuindex; 1894 unsigned int modindex, versindex, infoindex, pcpuindex;
1873 unsigned int num_kp, num_mcount; 1895 unsigned int num_mcount;
1874 struct kernel_param *kp;
1875 struct module *mod; 1896 struct module *mod;
1876 long err = 0; 1897 long err = 0;
1877 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 1898 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
@@ -1888,12 +1909,6 @@ static noinline struct module *load_module(void __user *umod,
1888 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) 1909 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1889 return ERR_PTR(-ENOMEM); 1910 return ERR_PTR(-ENOMEM);
1890 1911
1891 /* Create stop_machine threads since the error path relies on
1892 * a non-failing stop_machine call. */
1893 err = stop_machine_create();
1894 if (err)
1895 goto free_hdr;
1896
1897 if (copy_from_user(hdr, umod, len) != 0) { 1912 if (copy_from_user(hdr, umod, len) != 0) {
1898 err = -EFAULT; 1913 err = -EFAULT;
1899 goto free_hdr; 1914 goto free_hdr;
@@ -1934,7 +1949,7 @@ static noinline struct module *load_module(void __user *umod,
1934 } 1949 }
1935#ifndef CONFIG_MODULE_UNLOAD 1950#ifndef CONFIG_MODULE_UNLOAD
1936 /* Don't load .exit sections */ 1951 /* Don't load .exit sections */
1937 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0) 1952 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
1938 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC; 1953 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1939#endif 1954#endif
1940 } 1955 }
@@ -1978,7 +1993,7 @@ static noinline struct module *load_module(void __user *umod,
1978 modmagic = get_modinfo(sechdrs, infoindex, "vermagic"); 1993 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
1979 /* This is allowed: modprobe --force will invalidate it. */ 1994 /* This is allowed: modprobe --force will invalidate it. */
1980 if (!modmagic) { 1995 if (!modmagic) {
1981 err = try_to_force_load(mod, "magic"); 1996 err = try_to_force_load(mod, "bad vermagic");
1982 if (err) 1997 if (err)
1983 goto free_hdr; 1998 goto free_hdr;
1984 } else if (!same_magic(modmagic, vermagic, versindex)) { 1999 } else if (!same_magic(modmagic, vermagic, versindex)) {
@@ -2015,14 +2030,6 @@ static noinline struct module *load_module(void __user *umod,
2015 if (err < 0) 2030 if (err < 0)
2016 goto free_mod; 2031 goto free_mod;
2017 2032
2018#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2019 mod->refptr = percpu_modalloc(sizeof(local_t), __alignof__(local_t),
2020 mod->name);
2021 if (!mod->refptr) {
2022 err = -ENOMEM;
2023 goto free_mod;
2024 }
2025#endif
2026 if (pcpuindex) { 2033 if (pcpuindex) {
2027 /* We have a special allocation for this section. */ 2034 /* We have a special allocation for this section. */
2028 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size, 2035 percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
@@ -2030,7 +2037,7 @@ static noinline struct module *load_module(void __user *umod,
2030 mod->name); 2037 mod->name);
2031 if (!percpu) { 2038 if (!percpu) {
2032 err = -ENOMEM; 2039 err = -ENOMEM;
2033 goto free_percpu; 2040 goto free_mod;
2034 } 2041 }
2035 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2042 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2036 mod->percpu = percpu; 2043 mod->percpu = percpu;
@@ -2082,6 +2089,14 @@ static noinline struct module *load_module(void __user *umod,
2082 /* Module has been moved. */ 2089 /* Module has been moved. */
2083 mod = (void *)sechdrs[modindex].sh_addr; 2090 mod = (void *)sechdrs[modindex].sh_addr;
2084 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
2085 /* Now we've moved module, initialize linked lists, etc. */ 2100 /* Now we've moved module, initialize linked lists, etc. */
2086 module_unload_init(mod); 2101 module_unload_init(mod);
2087 2102
@@ -2116,8 +2131,8 @@ static noinline struct module *load_module(void __user *umod,
2116 2131
2117 /* Now we've got everything in the final locations, we can 2132 /* Now we've got everything in the final locations, we can
2118 * find optional sections. */ 2133 * find optional sections. */
2119 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp), 2134 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2120 &num_kp); 2135 sizeof(*mod->kp), &mod->num_kp);
2121 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab", 2136 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2122 sizeof(*mod->syms), &mod->num_syms); 2137 sizeof(*mod->syms), &mod->num_syms);
2123 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab"); 2138 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
@@ -2167,8 +2182,8 @@ static noinline struct module *load_module(void __user *umod,
2167 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs) 2182 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2168#endif 2183#endif
2169 ) { 2184 ) {
2170 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name); 2185 err = try_to_force_load(mod,
2171 err = try_to_force_load(mod, "nocrc"); 2186 "no versions for exported symbols");
2172 if (err) 2187 if (err)
2173 goto cleanup; 2188 goto cleanup;
2174 } 2189 }
@@ -2213,12 +2228,13 @@ static noinline struct module *load_module(void __user *umod,
2213 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings); 2228 add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
2214 2229
2215 if (!mod->taints) { 2230 if (!mod->taints) {
2216 struct mod_debug *debug; 2231 struct _ddebug *debug;
2217 unsigned int num_debug; 2232 unsigned int num_debug;
2218 2233
2219 debug = section_objs(hdr, sechdrs, secstrings, "__verbose", 2234 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2220 sizeof(*debug), &num_debug); 2235 sizeof(*debug), &num_debug);
2221 dynamic_printk_setup(debug, num_debug); 2236 if (debug)
2237 dynamic_debug_setup(debug, num_debug);
2222 } 2238 }
2223 2239
2224 /* sechdrs[0].sh_size is always zero */ 2240 /* sechdrs[0].sh_size is always zero */
@@ -2262,11 +2278,11 @@ static noinline struct module *load_module(void __user *umod,
2262 */ 2278 */
2263 list_add_rcu(&mod->list, &modules); 2279 list_add_rcu(&mod->list, &modules);
2264 2280
2265 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);
2266 if (err < 0) 2282 if (err < 0)
2267 goto unlink; 2283 goto unlink;
2268 2284
2269 err = mod_sysfs_setup(mod, kp, num_kp); 2285 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2270 if (err < 0) 2286 if (err < 0)
2271 goto unlink; 2287 goto unlink;
2272 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2288 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
@@ -2275,12 +2291,13 @@ static noinline struct module *load_module(void __user *umod,
2275 /* Get rid of temporary copy */ 2291 /* Get rid of temporary copy */
2276 vfree(hdr); 2292 vfree(hdr);
2277 2293
2278 stop_machine_destroy();
2279 /* Done! */ 2294 /* Done! */
2280 return mod; 2295 return mod;
2281 2296
2282 unlink: 2297 unlink:
2283 stop_machine(__unlink_module, mod, NULL); 2298 /* Unlink carefully: kallsyms could be walking list. */
2299 list_del_rcu(&mod->list);
2300 synchronize_sched();
2284 module_arch_cleanup(mod); 2301 module_arch_cleanup(mod);
2285 cleanup: 2302 cleanup:
2286 kobject_del(&mod->mkobj.kobj); 2303 kobject_del(&mod->mkobj.kobj);
@@ -2288,20 +2305,21 @@ static noinline struct module *load_module(void __user *umod,
2288 ftrace_release(mod->module_core, mod->core_size); 2305 ftrace_release(mod->module_core, mod->core_size);
2289 free_unload: 2306 free_unload:
2290 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
2291 module_free(mod, mod->module_init); 2312 module_free(mod, mod->module_init);
2292 free_core: 2313 free_core:
2293 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! */
2294 free_percpu: 2316 free_percpu:
2295 if (percpu) 2317 if (percpu)
2296 percpu_modfree(percpu); 2318 percpu_modfree(percpu);
2297#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2298 percpu_modfree(mod->refptr);
2299#endif
2300 free_mod: 2319 free_mod:
2301 kfree(args); 2320 kfree(args);
2302 free_hdr: 2321 free_hdr:
2303 vfree(hdr); 2322 vfree(hdr);
2304 stop_machine_destroy();
2305 return ERR_PTR(err); 2323 return ERR_PTR(err);
2306 2324
2307 truncated: 2325 truncated:
@@ -2578,6 +2596,25 @@ unsigned long module_kallsyms_lookup_name(const char *name)
2578 preempt_enable(); 2596 preempt_enable();
2579 return ret; 2597 return ret;
2580} 2598}
2599
2600int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2601 struct module *, unsigned long),
2602 void *data)
2603{
2604 struct module *mod;
2605 unsigned int i;
2606 int ret;
2607
2608 list_for_each_entry(mod, &modules, list) {
2609 for (i = 0; i < mod->num_symtab; i++) {
2610 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2611 mod, mod->symtab[i].st_value);
2612 if (ret != 0)
2613 return ret;
2614 }
2615 }
2616 return 0;
2617}
2581#endif /* CONFIG_KALLSYMS */ 2618#endif /* CONFIG_KALLSYMS */
2582 2619
2583static char *module_flags(struct module *mod, char *buf) 2620static char *module_flags(struct module *mod, char *buf)
@@ -2713,29 +2750,31 @@ const struct exception_table_entry *search_module_extables(unsigned long addr)
2713} 2750}
2714 2751
2715/* 2752/*
2716 * Is this a valid module address? 2753 * is_module_address - is this address inside a module?
2754 * @addr: the address to check.
2755 *
2756 * See is_module_text_address() if you simply want to see if the address
2757 * is code (not data).
2717 */ 2758 */
2718int is_module_address(unsigned long addr) 2759bool is_module_address(unsigned long addr)
2719{ 2760{
2720 struct module *mod; 2761 bool ret;
2721 2762
2722 preempt_disable(); 2763 preempt_disable();
2723 2764 ret = __module_address(addr) != NULL;
2724 list_for_each_entry_rcu(mod, &modules, list) {
2725 if (within_module_core(addr, mod)) {
2726 preempt_enable();
2727 return 1;
2728 }
2729 }
2730
2731 preempt_enable(); 2765 preempt_enable();
2732 2766
2733 return 0; 2767 return ret;
2734} 2768}
2735 2769
2736 2770/*
2737/* Is this a valid kernel address? */ 2771 * __module_address - get the module which contains an address.
2738__notrace_funcgraph struct module *__module_text_address(unsigned long addr) 2772 * @addr: the address.
2773 *
2774 * Must be called with preempt disabled or module mutex held so that
2775 * module doesn't get freed during this.
2776 */
2777struct module *__module_address(unsigned long addr)
2739{ 2778{
2740 struct module *mod; 2779 struct module *mod;
2741 2780
@@ -2743,22 +2782,51 @@ __notrace_funcgraph struct module *__module_text_address(unsigned long addr)
2743 return NULL; 2782 return NULL;
2744 2783
2745 list_for_each_entry_rcu(mod, &modules, list) 2784 list_for_each_entry_rcu(mod, &modules, list)
2746 if (within(addr, mod->module_init, mod->init_text_size) 2785 if (within_module_core(addr, mod)
2747 || within(addr, mod->module_core, mod->core_text_size)) 2786 || within_module_init(addr, mod))
2748 return mod; 2787 return mod;
2749 return NULL; 2788 return NULL;
2750} 2789}
2790EXPORT_SYMBOL_GPL(__module_address);
2751 2791
2752struct module *module_text_address(unsigned long addr) 2792/*
2793 * is_module_text_address - is this address inside module code?
2794 * @addr: the address to check.
2795 *
2796 * See is_module_address() if you simply want to see if the address is
2797 * anywhere in a module. See kernel_text_address() for testing if an
2798 * address corresponds to kernel or module code.
2799 */
2800bool is_module_text_address(unsigned long addr)
2753{ 2801{
2754 struct module *mod; 2802 bool ret;
2755 2803
2756 preempt_disable(); 2804 preempt_disable();
2757 mod = __module_text_address(addr); 2805 ret = __module_text_address(addr) != NULL;
2758 preempt_enable(); 2806 preempt_enable();
2759 2807
2808 return ret;
2809}
2810
2811/*
2812 * __module_text_address - get the module whose code contains an address.
2813 * @addr: the address.
2814 *
2815 * Must be called with preempt disabled or module mutex held so that
2816 * module doesn't get freed during this.
2817 */
2818struct module *__module_text_address(unsigned long addr)
2819{
2820 struct module *mod = __module_address(addr);
2821 if (mod) {
2822 /* Make sure it's within the text section. */
2823 if (!within(addr, mod->module_init, mod->init_text_size)
2824 && !within(addr, mod->module_core, mod->core_text_size))
2825 mod = NULL;
2826 }
2760 return mod; 2827 return mod;
2761} 2828}
2829EXPORT_SYMBOL_GPL(__module_text_address);
2762 2830
2763/* Don't grab lock, we're oopsing. */ 2831/* Don't grab lock, we're oopsing. */
2764void print_modules(void) 2832void print_modules(void)
@@ -2778,9 +2846,17 @@ void print_modules(void)
2778} 2846}
2779 2847
2780#ifdef CONFIG_MODVERSIONS 2848#ifdef CONFIG_MODVERSIONS
2781/* Generate the signature for struct module here, too, for modversions. */ 2849/* Generate the signature for all relevant module structures here.
2782void struct_module(struct module *mod) { return; } 2850 * If these change, we don't want to try to parse the module. */
2783EXPORT_SYMBOL(struct_module); 2851void module_layout(struct module *mod,
2852 struct modversion_info *ver,
2853 struct kernel_param *kp,
2854 struct kernel_symbol *ks,
2855 struct marker *marker,
2856 struct tracepoint *tp)
2857{
2858}
2859EXPORT_SYMBOL(module_layout);
2784#endif 2860#endif
2785 2861
2786#ifdef CONFIG_MARKERS 2862#ifdef CONFIG_MARKERS