aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
authorJames Morris <jmorris@namei.org>2009-05-08 03:56:47 -0400
committerJames Morris <jmorris@namei.org>2009-05-08 03:56:47 -0400
commitd254117099d711f215e62427f55dfb8ebd5ad011 (patch)
tree0848ff8dd74314fec14a86497f8d288c86ba7c65 /kernel/module.c
parent07ff7a0b187f3951788f64ae1f30e8109bc8e9eb (diff)
parent8c9ed899b44c19e81859fbb0e9d659fe2f8630fc (diff)
Merge branch 'master' into next
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c274
1 files changed, 161 insertions, 113 deletions
diff --git a/kernel/module.c b/kernel/module.c
index ee7ab612dafa..cb3887e770e2 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -68,7 +68,8 @@
68 68
69/* List of modules, protected by module_mutex or preempt_disable 69/* List of modules, protected by module_mutex or preempt_disable
70 * (delete uses stop_machine/add uses RCU list operations). */ 70 * (delete uses stop_machine/add uses RCU list operations). */
71static DEFINE_MUTEX(module_mutex); 71DEFINE_MUTEX(module_mutex);
72EXPORT_SYMBOL_GPL(module_mutex);
72static LIST_HEAD(modules); 73static LIST_HEAD(modules);
73 74
74/* Block module loading/unloading? */ 75/* Block module loading/unloading? */
@@ -79,7 +80,7 @@ static DECLARE_WAIT_QUEUE_HEAD(module_wq);
79 80
80static BLOCKING_NOTIFIER_HEAD(module_notify_list); 81static BLOCKING_NOTIFIER_HEAD(module_notify_list);
81 82
82/* Bounds of module allocation, for speeding __module_text_address */ 83/* Bounds of module allocation, for speeding __module_address */
83static unsigned long module_addr_min = -1UL, module_addr_max = 0; 84static unsigned long module_addr_min = -1UL, module_addr_max = 0;
84 85
85int register_module_notifier(struct notifier_block * nb) 86int register_module_notifier(struct notifier_block * nb)
@@ -189,17 +190,6 @@ extern const unsigned long __start___kcrctab_unused_gpl[];
189#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) 190#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
190#endif 191#endif
191 192
192struct symsearch {
193 const struct kernel_symbol *start, *stop;
194 const unsigned long *crcs;
195 enum {
196 NOT_GPL_ONLY,
197 GPL_ONLY,
198 WILL_BE_GPL_ONLY,
199 } licence;
200 bool unused;
201};
202
203static bool each_symbol_in_section(const struct symsearch *arr, 193static bool each_symbol_in_section(const struct symsearch *arr,
204 unsigned int arrsize, 194 unsigned int arrsize,
205 struct module *owner, 195 struct module *owner,
@@ -220,10 +210,8 @@ static bool each_symbol_in_section(const struct symsearch *arr,
220} 210}
221 211
222/* Returns true as soon as fn returns true, otherwise false. */ 212/* Returns true as soon as fn returns true, otherwise false. */
223static bool each_symbol(bool (*fn)(const struct symsearch *arr, 213bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
224 struct module *owner, 214 unsigned int symnum, void *data), void *data)
225 unsigned int symnum, void *data),
226 void *data)
227{ 215{
228 struct module *mod; 216 struct module *mod;
229 const struct symsearch arr[] = { 217 const struct symsearch arr[] = {
@@ -276,6 +264,7 @@ static bool each_symbol(bool (*fn)(const struct symsearch *arr,
276 } 264 }
277 return false; 265 return false;
278} 266}
267EXPORT_SYMBOL_GPL(each_symbol);
279 268
280struct find_symbol_arg { 269struct find_symbol_arg {
281 /* Input */ 270 /* Input */
@@ -286,7 +275,7 @@ struct find_symbol_arg {
286 /* Output */ 275 /* Output */
287 struct module *owner; 276 struct module *owner;
288 const unsigned long *crc; 277 const unsigned long *crc;
289 unsigned long value; 278 const struct kernel_symbol *sym;
290}; 279};
291 280
292static bool find_symbol_in_section(const struct symsearch *syms, 281static bool find_symbol_in_section(const struct symsearch *syms,
@@ -327,17 +316,17 @@ static bool find_symbol_in_section(const struct symsearch *syms,
327 316
328 fsa->owner = owner; 317 fsa->owner = owner;
329 fsa->crc = symversion(syms->crcs, symnum); 318 fsa->crc = symversion(syms->crcs, symnum);
330 fsa->value = syms->start[symnum].value; 319 fsa->sym = &syms->start[symnum];
331 return true; 320 return true;
332} 321}
333 322
334/* Find a symbol, return value, (optional) crc and (optional) module 323/* Find a symbol and return it, along with, (optional) crc and
335 * which owns it */ 324 * (optional) module which owns it */
336static unsigned long find_symbol(const char *name, 325const struct kernel_symbol *find_symbol(const char *name,
337 struct module **owner, 326 struct module **owner,
338 const unsigned long **crc, 327 const unsigned long **crc,
339 bool gplok, 328 bool gplok,
340 bool warn) 329 bool warn)
341{ 330{
342 struct find_symbol_arg fsa; 331 struct find_symbol_arg fsa;
343 332
@@ -350,15 +339,16 @@ static unsigned long find_symbol(const char *name,
350 *owner = fsa.owner; 339 *owner = fsa.owner;
351 if (crc) 340 if (crc)
352 *crc = fsa.crc; 341 *crc = fsa.crc;
353 return fsa.value; 342 return fsa.sym;
354 } 343 }
355 344
356 DEBUGP("Failed to find symbol %s\n", name); 345 DEBUGP("Failed to find symbol %s\n", name);
357 return -ENOENT; 346 return NULL;
358} 347}
348EXPORT_SYMBOL_GPL(find_symbol);
359 349
360/* Search for module by name: must hold module_mutex. */ 350/* Search for module by name: must hold module_mutex. */
361static struct module *find_module(const char *name) 351struct module *find_module(const char *name)
362{ 352{
363 struct module *mod; 353 struct module *mod;
364 354
@@ -368,6 +358,7 @@ static struct module *find_module(const char *name)
368 } 358 }
369 return NULL; 359 return NULL;
370} 360}
361EXPORT_SYMBOL_GPL(find_module);
371 362
372#ifdef CONFIG_SMP 363#ifdef CONFIG_SMP
373 364
@@ -644,7 +635,7 @@ static int already_uses(struct module *a, struct module *b)
644} 635}
645 636
646/* Module a uses b */ 637/* Module a uses b */
647static int use_module(struct module *a, struct module *b) 638int use_module(struct module *a, struct module *b)
648{ 639{
649 struct module_use *use; 640 struct module_use *use;
650 int no_warn, err; 641 int no_warn, err;
@@ -677,6 +668,7 @@ static int use_module(struct module *a, struct module *b)
677 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name); 668 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
678 return 1; 669 return 1;
679} 670}
671EXPORT_SYMBOL_GPL(use_module);
680 672
681/* Clear the unload stuff of the module. */ 673/* Clear the unload stuff of the module. */
682static void module_unload_free(struct module *mod) 674static void module_unload_free(struct module *mod)
@@ -897,7 +889,7 @@ void __symbol_put(const char *symbol)
897 struct module *owner; 889 struct module *owner;
898 890
899 preempt_disable(); 891 preempt_disable();
900 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false))) 892 if (!find_symbol(symbol, &owner, NULL, true, false))
901 BUG(); 893 BUG();
902 module_put(owner); 894 module_put(owner);
903 preempt_enable(); 895 preempt_enable();
@@ -911,8 +903,10 @@ void symbol_put_addr(void *addr)
911 if (core_kernel_text((unsigned long)addr)) 903 if (core_kernel_text((unsigned long)addr))
912 return; 904 return;
913 905
914 if (!(modaddr = module_text_address((unsigned long)addr))) 906 /* module_text_address is safe here: we're supposed to have reference
915 BUG(); 907 * to module from symbol_get, so it can't go away. */
908 modaddr = __module_text_address((unsigned long)addr);
909 BUG_ON(!modaddr);
916 module_put(modaddr); 910 module_put(modaddr);
917} 911}
918EXPORT_SYMBOL_GPL(symbol_put_addr); 912EXPORT_SYMBOL_GPL(symbol_put_addr);
@@ -952,10 +946,11 @@ static inline void module_unload_free(struct module *mod)
952{ 946{
953} 947}
954 948
955static inline int use_module(struct module *a, struct module *b) 949int use_module(struct module *a, struct module *b)
956{ 950{
957 return strong_try_module_get(b) == 0; 951 return strong_try_module_get(b) == 0;
958} 952}
953EXPORT_SYMBOL_GPL(use_module);
959 954
960static inline void module_unload_init(struct module *mod) 955static inline void module_unload_init(struct module *mod)
961{ 956{
@@ -998,12 +993,12 @@ static struct module_attribute *modinfo_attrs[] = {
998 993
999static const char vermagic[] = VERMAGIC_STRING; 994static const char vermagic[] = VERMAGIC_STRING;
1000 995
1001static int try_to_force_load(struct module *mod, const char *symname) 996static int try_to_force_load(struct module *mod, const char *reason)
1002{ 997{
1003#ifdef CONFIG_MODULE_FORCE_LOAD 998#ifdef CONFIG_MODULE_FORCE_LOAD
1004 if (!test_taint(TAINT_FORCED_MODULE)) 999 if (!test_taint(TAINT_FORCED_MODULE))
1005 printk("%s: no version for \"%s\" found: kernel tainted.\n", 1000 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1006 mod->name, symname); 1001 mod->name, reason);
1007 add_taint_module(mod, TAINT_FORCED_MODULE); 1002 add_taint_module(mod, TAINT_FORCED_MODULE);
1008 return 0; 1003 return 0;
1009#else 1004#else
@@ -1060,9 +1055,9 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1060{ 1055{
1061 const unsigned long *crc; 1056 const unsigned long *crc;
1062 1057
1063 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false))) 1058 if (!find_symbol("module_layout", NULL, &crc, true, false))
1064 BUG(); 1059 BUG();
1065 return check_version(sechdrs, versindex, "struct_module", mod, crc); 1060 return check_version(sechdrs, versindex, "module_layout", mod, crc);
1066} 1061}
1067 1062
1068/* First part is kernel version, which we ignore if module has crcs. */ 1063/* First part is kernel version, which we ignore if module has crcs. */
@@ -1101,25 +1096,25 @@ static inline int same_magic(const char *amagic, const char *bmagic,
1101 1096
1102/* Resolve a symbol for this module. I.e. if we find one, record usage. 1097/* Resolve a symbol for this module. I.e. if we find one, record usage.
1103 Must be holding module_mutex. */ 1098 Must be holding module_mutex. */
1104static unsigned long resolve_symbol(Elf_Shdr *sechdrs, 1099static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1105 unsigned int versindex, 1100 unsigned int versindex,
1106 const char *name, 1101 const char *name,
1107 struct module *mod) 1102 struct module *mod)
1108{ 1103{
1109 struct module *owner; 1104 struct module *owner;
1110 unsigned long ret; 1105 const struct kernel_symbol *sym;
1111 const unsigned long *crc; 1106 const unsigned long *crc;
1112 1107
1113 ret = find_symbol(name, &owner, &crc, 1108 sym = find_symbol(name, &owner, &crc,
1114 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1109 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1115 if (!IS_ERR_VALUE(ret)) { 1110 /* use_module can fail due to OOM,
1116 /* use_module can fail due to OOM, 1111 or module initialization or unloading */
1117 or module initialization or unloading */ 1112 if (sym) {
1118 if (!check_version(sechdrs, versindex, name, mod, crc) || 1113 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1119 !use_module(mod, owner)) 1114 !use_module(mod, owner))
1120 ret = -EINVAL; 1115 sym = NULL;
1121 } 1116 }
1122 return ret; 1117 return sym;
1123} 1118}
1124 1119
1125/* 1120/*
@@ -1494,6 +1489,9 @@ static void free_module(struct module *mod)
1494 /* Module unload stuff */ 1489 /* Module unload stuff */
1495 module_unload_free(mod); 1490 module_unload_free(mod);
1496 1491
1492 /* Free any allocated parameters. */
1493 destroy_params(mod->kp, mod->num_kp);
1494
1497 /* release any pointers to mcount in this module */ 1495 /* release any pointers to mcount in this module */
1498 ftrace_release(mod->module_core, mod->core_size); 1496 ftrace_release(mod->module_core, mod->core_size);
1499 1497
@@ -1516,17 +1514,15 @@ static void free_module(struct module *mod)
1516void *__symbol_get(const char *symbol) 1514void *__symbol_get(const char *symbol)
1517{ 1515{
1518 struct module *owner; 1516 struct module *owner;
1519 unsigned long value; 1517 const struct kernel_symbol *sym;
1520 1518
1521 preempt_disable(); 1519 preempt_disable();
1522 value = find_symbol(symbol, &owner, NULL, true, true); 1520 sym = find_symbol(symbol, &owner, NULL, true, true);
1523 if (IS_ERR_VALUE(value)) 1521 if (sym && strong_try_module_get(owner))
1524 value = 0; 1522 sym = NULL;
1525 else if (strong_try_module_get(owner))
1526 value = 0;
1527 preempt_enable(); 1523 preempt_enable();
1528 1524
1529 return (void *)value; 1525 return sym ? (void *)sym->value : NULL;
1530} 1526}
1531EXPORT_SYMBOL_GPL(__symbol_get); 1527EXPORT_SYMBOL_GPL(__symbol_get);
1532 1528
@@ -1554,8 +1550,7 @@ static int verify_export_symbols(struct module *mod)
1554 1550
1555 for (i = 0; i < ARRAY_SIZE(arr); i++) { 1551 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1556 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) { 1552 for (s = arr[i].sym; s < arr[i].sym + arr[i].num; s++) {
1557 if (!IS_ERR_VALUE(find_symbol(s->name, &owner, 1553 if (find_symbol(s->name, &owner, NULL, true, false)) {
1558 NULL, true, false))) {
1559 printk(KERN_ERR 1554 printk(KERN_ERR
1560 "%s: exports duplicate symbol %s" 1555 "%s: exports duplicate symbol %s"
1561 " (owned by %s)\n", 1556 " (owned by %s)\n",
@@ -1579,6 +1574,7 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1579 unsigned long secbase; 1574 unsigned long secbase;
1580 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1575 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1581 int ret = 0; 1576 int ret = 0;
1577 const struct kernel_symbol *ksym;
1582 1578
1583 for (i = 1; i < n; i++) { 1579 for (i = 1; i < n; i++) {
1584 switch (sym[i].st_shndx) { 1580 switch (sym[i].st_shndx) {
@@ -1598,13 +1594,14 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1598 break; 1594 break;
1599 1595
1600 case SHN_UNDEF: 1596 case SHN_UNDEF:
1601 sym[i].st_value 1597 ksym = resolve_symbol(sechdrs, versindex,
1602 = resolve_symbol(sechdrs, versindex, 1598 strtab + sym[i].st_name, mod);
1603 strtab + sym[i].st_name, mod);
1604
1605 /* Ok if resolved. */ 1599 /* Ok if resolved. */
1606 if (!IS_ERR_VALUE(sym[i].st_value)) 1600 if (ksym) {
1601 sym[i].st_value = ksym->value;
1607 break; 1602 break;
1603 }
1604
1608 /* Ok if weak. */ 1605 /* Ok if weak. */
1609 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1606 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1610 break; 1607 break;
@@ -1679,8 +1676,7 @@ static void layout_sections(struct module *mod,
1679 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1676 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1680 || (s->sh_flags & masks[m][1]) 1677 || (s->sh_flags & masks[m][1])
1681 || s->sh_entsize != ~0UL 1678 || s->sh_entsize != ~0UL
1682 || strncmp(secstrings + s->sh_name, 1679 || strstarts(secstrings + s->sh_name, ".init"))
1683 ".init", 5) == 0)
1684 continue; 1680 continue;
1685 s->sh_entsize = get_offset(mod, &mod->core_size, s, i); 1681 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1686 DEBUGP("\t%s\n", secstrings + s->sh_name); 1682 DEBUGP("\t%s\n", secstrings + s->sh_name);
@@ -1697,8 +1693,7 @@ static void layout_sections(struct module *mod,
1697 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1693 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1698 || (s->sh_flags & masks[m][1]) 1694 || (s->sh_flags & masks[m][1])
1699 || s->sh_entsize != ~0UL 1695 || s->sh_entsize != ~0UL
1700 || strncmp(secstrings + s->sh_name, 1696 || !strstarts(secstrings + s->sh_name, ".init"))
1701 ".init", 5) != 0)
1702 continue; 1697 continue;
1703 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i) 1698 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1704 | INIT_OFFSET_MASK); 1699 | INIT_OFFSET_MASK);
@@ -1831,8 +1826,7 @@ static char elf_type(const Elf_Sym *sym,
1831 else 1826 else
1832 return 'b'; 1827 return 'b';
1833 } 1828 }
1834 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name, 1829 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
1835 ".debug", strlen(".debug")) == 0)
1836 return 'n'; 1830 return 'n';
1837 return '?'; 1831 return '?';
1838} 1832}
@@ -1901,8 +1895,7 @@ static noinline struct module *load_module(void __user *umod,
1901 unsigned int symindex = 0; 1895 unsigned int symindex = 0;
1902 unsigned int strindex = 0; 1896 unsigned int strindex = 0;
1903 unsigned int modindex, versindex, infoindex, pcpuindex; 1897 unsigned int modindex, versindex, infoindex, pcpuindex;
1904 unsigned int num_kp, num_mcount; 1898 unsigned int num_mcount;
1905 struct kernel_param *kp;
1906 struct module *mod; 1899 struct module *mod;
1907 long err = 0; 1900 long err = 0;
1908 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 1901 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
@@ -1919,12 +1912,6 @@ static noinline struct module *load_module(void __user *umod,
1919 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) 1912 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1920 return ERR_PTR(-ENOMEM); 1913 return ERR_PTR(-ENOMEM);
1921 1914
1922 /* Create stop_machine threads since the error path relies on
1923 * a non-failing stop_machine call. */
1924 err = stop_machine_create();
1925 if (err)
1926 goto free_hdr;
1927
1928 if (copy_from_user(hdr, umod, len) != 0) { 1915 if (copy_from_user(hdr, umod, len) != 0) {
1929 err = -EFAULT; 1916 err = -EFAULT;
1930 goto free_hdr; 1917 goto free_hdr;
@@ -1965,7 +1952,7 @@ static noinline struct module *load_module(void __user *umod,
1965 } 1952 }
1966#ifndef CONFIG_MODULE_UNLOAD 1953#ifndef CONFIG_MODULE_UNLOAD
1967 /* Don't load .exit sections */ 1954 /* Don't load .exit sections */
1968 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0) 1955 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
1969 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC; 1956 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1970#endif 1957#endif
1971 } 1958 }
@@ -2009,7 +1996,7 @@ static noinline struct module *load_module(void __user *umod,
2009 modmagic = get_modinfo(sechdrs, infoindex, "vermagic"); 1996 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2010 /* This is allowed: modprobe --force will invalidate it. */ 1997 /* This is allowed: modprobe --force will invalidate it. */
2011 if (!modmagic) { 1998 if (!modmagic) {
2012 err = try_to_force_load(mod, "magic"); 1999 err = try_to_force_load(mod, "bad vermagic");
2013 if (err) 2000 if (err)
2014 goto free_hdr; 2001 goto free_hdr;
2015 } else if (!same_magic(modmagic, vermagic, versindex)) { 2002 } else if (!same_magic(modmagic, vermagic, versindex)) {
@@ -2147,8 +2134,8 @@ static noinline struct module *load_module(void __user *umod,
2147 2134
2148 /* Now we've got everything in the final locations, we can 2135 /* Now we've got everything in the final locations, we can
2149 * find optional sections. */ 2136 * find optional sections. */
2150 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp), 2137 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2151 &num_kp); 2138 sizeof(*mod->kp), &mod->num_kp);
2152 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab", 2139 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2153 sizeof(*mod->syms), &mod->num_syms); 2140 sizeof(*mod->syms), &mod->num_syms);
2154 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab"); 2141 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
@@ -2198,8 +2185,8 @@ static noinline struct module *load_module(void __user *umod,
2198 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs) 2185 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2199#endif 2186#endif
2200 ) { 2187 ) {
2201 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name); 2188 err = try_to_force_load(mod,
2202 err = try_to_force_load(mod, "nocrc"); 2189 "no versions for exported symbols");
2203 if (err) 2190 if (err)
2204 goto cleanup; 2191 goto cleanup;
2205 } 2192 }
@@ -2294,11 +2281,11 @@ static noinline struct module *load_module(void __user *umod,
2294 */ 2281 */
2295 list_add_rcu(&mod->list, &modules); 2282 list_add_rcu(&mod->list, &modules);
2296 2283
2297 err = parse_args(mod->name, mod->args, kp, num_kp, NULL); 2284 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2298 if (err < 0) 2285 if (err < 0)
2299 goto unlink; 2286 goto unlink;
2300 2287
2301 err = mod_sysfs_setup(mod, kp, num_kp); 2288 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2302 if (err < 0) 2289 if (err < 0)
2303 goto unlink; 2290 goto unlink;
2304 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2291 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
@@ -2307,12 +2294,13 @@ static noinline struct module *load_module(void __user *umod,
2307 /* Get rid of temporary copy */ 2294 /* Get rid of temporary copy */
2308 vfree(hdr); 2295 vfree(hdr);
2309 2296
2310 stop_machine_destroy();
2311 /* Done! */ 2297 /* Done! */
2312 return mod; 2298 return mod;
2313 2299
2314 unlink: 2300 unlink:
2315 stop_machine(__unlink_module, mod, NULL); 2301 /* Unlink carefully: kallsyms could be walking list. */
2302 list_del_rcu(&mod->list);
2303 synchronize_sched();
2316 module_arch_cleanup(mod); 2304 module_arch_cleanup(mod);
2317 cleanup: 2305 cleanup:
2318 kobject_del(&mod->mkobj.kobj); 2306 kobject_del(&mod->mkobj.kobj);
@@ -2320,8 +2308,8 @@ static noinline struct module *load_module(void __user *umod,
2320 ftrace_release(mod->module_core, mod->core_size); 2308 ftrace_release(mod->module_core, mod->core_size);
2321 free_unload: 2309 free_unload:
2322 module_unload_free(mod); 2310 module_unload_free(mod);
2323 free_init:
2324#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2311#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2312 free_init:
2325 percpu_modfree(mod->refptr); 2313 percpu_modfree(mod->refptr);
2326#endif 2314#endif
2327 module_free(mod, mod->module_init); 2315 module_free(mod, mod->module_init);
@@ -2335,7 +2323,6 @@ static noinline struct module *load_module(void __user *umod,
2335 kfree(args); 2323 kfree(args);
2336 free_hdr: 2324 free_hdr:
2337 vfree(hdr); 2325 vfree(hdr);
2338 stop_machine_destroy();
2339 return ERR_PTR(err); 2326 return ERR_PTR(err);
2340 2327
2341 truncated: 2328 truncated:
@@ -2404,6 +2391,9 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2404 blocking_notifier_call_chain(&module_notify_list, 2391 blocking_notifier_call_chain(&module_notify_list,
2405 MODULE_STATE_LIVE, mod); 2392 MODULE_STATE_LIVE, mod);
2406 2393
2394 /* We need to finish all async code before the module init sequence is done */
2395 async_synchronize_full();
2396
2407 mutex_lock(&module_mutex); 2397 mutex_lock(&module_mutex);
2408 /* Drop initial reference. */ 2398 /* Drop initial reference. */
2409 module_put(mod); 2399 module_put(mod);
@@ -2612,6 +2602,25 @@ unsigned long module_kallsyms_lookup_name(const char *name)
2612 preempt_enable(); 2602 preempt_enable();
2613 return ret; 2603 return ret;
2614} 2604}
2605
2606int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
2607 struct module *, unsigned long),
2608 void *data)
2609{
2610 struct module *mod;
2611 unsigned int i;
2612 int ret;
2613
2614 list_for_each_entry(mod, &modules, list) {
2615 for (i = 0; i < mod->num_symtab; i++) {
2616 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
2617 mod, mod->symtab[i].st_value);
2618 if (ret != 0)
2619 return ret;
2620 }
2621 }
2622 return 0;
2623}
2615#endif /* CONFIG_KALLSYMS */ 2624#endif /* CONFIG_KALLSYMS */
2616 2625
2617static char *module_flags(struct module *mod, char *buf) 2626static char *module_flags(struct module *mod, char *buf)
@@ -2747,29 +2756,31 @@ const struct exception_table_entry *search_module_extables(unsigned long addr)
2747} 2756}
2748 2757
2749/* 2758/*
2750 * Is this a valid module address? 2759 * is_module_address - is this address inside a module?
2760 * @addr: the address to check.
2761 *
2762 * See is_module_text_address() if you simply want to see if the address
2763 * is code (not data).
2751 */ 2764 */
2752int is_module_address(unsigned long addr) 2765bool is_module_address(unsigned long addr)
2753{ 2766{
2754 struct module *mod; 2767 bool ret;
2755 2768
2756 preempt_disable(); 2769 preempt_disable();
2757 2770 ret = __module_address(addr) != NULL;
2758 list_for_each_entry_rcu(mod, &modules, list) {
2759 if (within_module_core(addr, mod)) {
2760 preempt_enable();
2761 return 1;
2762 }
2763 }
2764
2765 preempt_enable(); 2771 preempt_enable();
2766 2772
2767 return 0; 2773 return ret;
2768} 2774}
2769 2775
2770 2776/*
2771/* Is this a valid kernel address? */ 2777 * __module_address - get the module which contains an address.
2772__notrace_funcgraph struct module *__module_text_address(unsigned long addr) 2778 * @addr: the address.
2779 *
2780 * Must be called with preempt disabled or module mutex held so that
2781 * module doesn't get freed during this.
2782 */
2783struct module *__module_address(unsigned long addr)
2773{ 2784{
2774 struct module *mod; 2785 struct module *mod;
2775 2786
@@ -2777,22 +2788,51 @@ __notrace_funcgraph struct module *__module_text_address(unsigned long addr)
2777 return NULL; 2788 return NULL;
2778 2789
2779 list_for_each_entry_rcu(mod, &modules, list) 2790 list_for_each_entry_rcu(mod, &modules, list)
2780 if (within(addr, mod->module_init, mod->init_text_size) 2791 if (within_module_core(addr, mod)
2781 || within(addr, mod->module_core, mod->core_text_size)) 2792 || within_module_init(addr, mod))
2782 return mod; 2793 return mod;
2783 return NULL; 2794 return NULL;
2784} 2795}
2796EXPORT_SYMBOL_GPL(__module_address);
2785 2797
2786struct module *module_text_address(unsigned long addr) 2798/*
2799 * is_module_text_address - is this address inside module code?
2800 * @addr: the address to check.
2801 *
2802 * See is_module_address() if you simply want to see if the address is
2803 * anywhere in a module. See kernel_text_address() for testing if an
2804 * address corresponds to kernel or module code.
2805 */
2806bool is_module_text_address(unsigned long addr)
2787{ 2807{
2788 struct module *mod; 2808 bool ret;
2789 2809
2790 preempt_disable(); 2810 preempt_disable();
2791 mod = __module_text_address(addr); 2811 ret = __module_text_address(addr) != NULL;
2792 preempt_enable(); 2812 preempt_enable();
2793 2813
2814 return ret;
2815}
2816
2817/*
2818 * __module_text_address - get the module whose code contains an address.
2819 * @addr: the address.
2820 *
2821 * Must be called with preempt disabled or module mutex held so that
2822 * module doesn't get freed during this.
2823 */
2824struct module *__module_text_address(unsigned long addr)
2825{
2826 struct module *mod = __module_address(addr);
2827 if (mod) {
2828 /* Make sure it's within the text section. */
2829 if (!within(addr, mod->module_init, mod->init_text_size)
2830 && !within(addr, mod->module_core, mod->core_text_size))
2831 mod = NULL;
2832 }
2794 return mod; 2833 return mod;
2795} 2834}
2835EXPORT_SYMBOL_GPL(__module_text_address);
2796 2836
2797/* Don't grab lock, we're oopsing. */ 2837/* Don't grab lock, we're oopsing. */
2798void print_modules(void) 2838void print_modules(void)
@@ -2812,9 +2852,17 @@ void print_modules(void)
2812} 2852}
2813 2853
2814#ifdef CONFIG_MODVERSIONS 2854#ifdef CONFIG_MODVERSIONS
2815/* Generate the signature for struct module here, too, for modversions. */ 2855/* Generate the signature for all relevant module structures here.
2816void struct_module(struct module *mod) { return; } 2856 * If these change, we don't want to try to parse the module. */
2817EXPORT_SYMBOL(struct_module); 2857void module_layout(struct module *mod,
2858 struct modversion_info *ver,
2859 struct kernel_param *kp,
2860 struct kernel_symbol *ks,
2861 struct marker *marker,
2862 struct tracepoint *tp)
2863{
2864}
2865EXPORT_SYMBOL(module_layout);
2818#endif 2866#endif
2819 2867
2820#ifdef CONFIG_MARKERS 2868#ifdef CONFIG_MARKERS