aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
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 f77ac320d0b5..f6e08b7cff7c 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/* Waiting for a module to finish initializing? */ 75/* Waiting for a module to finish initializing? */
@@ -76,7 +77,7 @@ static DECLARE_WAIT_QUEUE_HEAD(module_wq);
76 77
77static BLOCKING_NOTIFIER_HEAD(module_notify_list); 78static BLOCKING_NOTIFIER_HEAD(module_notify_list);
78 79
79/* Bounds of module allocation, for speeding __module_text_address */ 80/* Bounds of module allocation, for speeding __module_address */
80static unsigned long module_addr_min = -1UL, module_addr_max = 0; 81static unsigned long module_addr_min = -1UL, module_addr_max = 0;
81 82
82int register_module_notifier(struct notifier_block * nb) 83int register_module_notifier(struct notifier_block * nb)
@@ -186,17 +187,6 @@ extern const unsigned long __start___kcrctab_unused_gpl[];
186#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL) 187#define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
187#endif 188#endif
188 189
189struct symsearch {
190 const struct kernel_symbol *start, *stop;
191 const unsigned long *crcs;
192 enum {
193 NOT_GPL_ONLY,
194 GPL_ONLY,
195 WILL_BE_GPL_ONLY,
196 } licence;
197 bool unused;
198};
199
200static bool each_symbol_in_section(const struct symsearch *arr, 190static bool each_symbol_in_section(const struct symsearch *arr,
201 unsigned int arrsize, 191 unsigned int arrsize,
202 struct module *owner, 192 struct module *owner,
@@ -217,10 +207,8 @@ static bool each_symbol_in_section(const struct symsearch *arr,
217} 207}
218 208
219/* Returns true as soon as fn returns true, otherwise false. */ 209/* Returns true as soon as fn returns true, otherwise false. */
220static bool each_symbol(bool (*fn)(const struct symsearch *arr, 210bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
221 struct module *owner, 211 unsigned int symnum, void *data), void *data)
222 unsigned int symnum, void *data),
223 void *data)
224{ 212{
225 struct module *mod; 213 struct module *mod;
226 const struct symsearch arr[] = { 214 const struct symsearch arr[] = {
@@ -273,6 +261,7 @@ static bool each_symbol(bool (*fn)(const struct symsearch *arr,
273 } 261 }
274 return false; 262 return false;
275} 263}
264EXPORT_SYMBOL_GPL(each_symbol);
276 265
277struct find_symbol_arg { 266struct find_symbol_arg {
278 /* Input */ 267 /* Input */
@@ -283,7 +272,7 @@ struct find_symbol_arg {
283 /* Output */ 272 /* Output */
284 struct module *owner; 273 struct module *owner;
285 const unsigned long *crc; 274 const unsigned long *crc;
286 unsigned long value; 275 const struct kernel_symbol *sym;
287}; 276};
288 277
289static bool find_symbol_in_section(const struct symsearch *syms, 278static bool find_symbol_in_section(const struct symsearch *syms,
@@ -324,17 +313,17 @@ static bool find_symbol_in_section(const struct symsearch *syms,
324 313
325 fsa->owner = owner; 314 fsa->owner = owner;
326 fsa->crc = symversion(syms->crcs, symnum); 315 fsa->crc = symversion(syms->crcs, symnum);
327 fsa->value = syms->start[symnum].value; 316 fsa->sym = &syms->start[symnum];
328 return true; 317 return true;
329} 318}
330 319
331/* Find a symbol, return value, (optional) crc and (optional) module 320/* Find a symbol and return it, along with, (optional) crc and
332 * which owns it */ 321 * (optional) module which owns it */
333static unsigned long find_symbol(const char *name, 322const struct kernel_symbol *find_symbol(const char *name,
334 struct module **owner, 323 struct module **owner,
335 const unsigned long **crc, 324 const unsigned long **crc,
336 bool gplok, 325 bool gplok,
337 bool warn) 326 bool warn)
338{ 327{
339 struct find_symbol_arg fsa; 328 struct find_symbol_arg fsa;
340 329
@@ -347,15 +336,16 @@ static unsigned long find_symbol(const char *name,
347 *owner = fsa.owner; 336 *owner = fsa.owner;
348 if (crc) 337 if (crc)
349 *crc = fsa.crc; 338 *crc = fsa.crc;
350 return fsa.value; 339 return fsa.sym;
351 } 340 }
352 341
353 DEBUGP("Failed to find symbol %s\n", name); 342 DEBUGP("Failed to find symbol %s\n", name);
354 return -ENOENT; 343 return NULL;
355} 344}
345EXPORT_SYMBOL_GPL(find_symbol);
356 346
357/* Search for module by name: must hold module_mutex. */ 347/* Search for module by name: must hold module_mutex. */
358static struct module *find_module(const char *name) 348struct module *find_module(const char *name)
359{ 349{
360 struct module *mod; 350 struct module *mod;
361 351
@@ -365,6 +355,7 @@ static struct module *find_module(const char *name)
365 } 355 }
366 return NULL; 356 return NULL;
367} 357}
358EXPORT_SYMBOL_GPL(find_module);
368 359
369#ifdef CONFIG_SMP 360#ifdef CONFIG_SMP
370 361
@@ -641,7 +632,7 @@ static int already_uses(struct module *a, struct module *b)
641} 632}
642 633
643/* Module a uses b */ 634/* Module a uses b */
644static int use_module(struct module *a, struct module *b) 635int use_module(struct module *a, struct module *b)
645{ 636{
646 struct module_use *use; 637 struct module_use *use;
647 int no_warn, err; 638 int no_warn, err;
@@ -674,6 +665,7 @@ static int use_module(struct module *a, struct module *b)
674 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);
675 return 1; 666 return 1;
676} 667}
668EXPORT_SYMBOL_GPL(use_module);
677 669
678/* Clear the unload stuff of the module. */ 670/* Clear the unload stuff of the module. */
679static void module_unload_free(struct module *mod) 671static void module_unload_free(struct module *mod)
@@ -894,7 +886,7 @@ void __symbol_put(const char *symbol)
894 struct module *owner; 886 struct module *owner;
895 887
896 preempt_disable(); 888 preempt_disable();
897 if (IS_ERR_VALUE(find_symbol(symbol, &owner, NULL, true, false))) 889 if (!find_symbol(symbol, &owner, NULL, true, false))
898 BUG(); 890 BUG();
899 module_put(owner); 891 module_put(owner);
900 preempt_enable(); 892 preempt_enable();
@@ -908,8 +900,10 @@ void symbol_put_addr(void *addr)
908 if (core_kernel_text((unsigned long)addr)) 900 if (core_kernel_text((unsigned long)addr))
909 return; 901 return;
910 902
911 if (!(modaddr = module_text_address((unsigned long)addr))) 903 /* module_text_address is safe here: we're supposed to have reference
912 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);
913 module_put(modaddr); 907 module_put(modaddr);
914} 908}
915EXPORT_SYMBOL_GPL(symbol_put_addr); 909EXPORT_SYMBOL_GPL(symbol_put_addr);
@@ -949,10 +943,11 @@ static inline void module_unload_free(struct module *mod)
949{ 943{
950} 944}
951 945
952static inline int use_module(struct module *a, struct module *b) 946int use_module(struct module *a, struct module *b)
953{ 947{
954 return strong_try_module_get(b) == 0; 948 return strong_try_module_get(b) == 0;
955} 949}
950EXPORT_SYMBOL_GPL(use_module);
956 951
957static inline void module_unload_init(struct module *mod) 952static inline void module_unload_init(struct module *mod)
958{ 953{
@@ -995,12 +990,12 @@ static struct module_attribute *modinfo_attrs[] = {
995 990
996static const char vermagic[] = VERMAGIC_STRING; 991static const char vermagic[] = VERMAGIC_STRING;
997 992
998static int try_to_force_load(struct module *mod, const char *symname) 993static int try_to_force_load(struct module *mod, const char *reason)
999{ 994{
1000#ifdef CONFIG_MODULE_FORCE_LOAD 995#ifdef CONFIG_MODULE_FORCE_LOAD
1001 if (!test_taint(TAINT_FORCED_MODULE)) 996 if (!test_taint(TAINT_FORCED_MODULE))
1002 printk("%s: no version for \"%s\" found: kernel tainted.\n", 997 printk(KERN_WARNING "%s: %s: kernel tainted.\n",
1003 mod->name, symname); 998 mod->name, reason);
1004 add_taint_module(mod, TAINT_FORCED_MODULE); 999 add_taint_module(mod, TAINT_FORCED_MODULE);
1005 return 0; 1000 return 0;
1006#else 1001#else
@@ -1057,9 +1052,9 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1057{ 1052{
1058 const unsigned long *crc; 1053 const unsigned long *crc;
1059 1054
1060 if (IS_ERR_VALUE(find_symbol("struct_module", NULL, &crc, true, false))) 1055 if (!find_symbol("module_layout", NULL, &crc, true, false))
1061 BUG(); 1056 BUG();
1062 return check_version(sechdrs, versindex, "struct_module", mod, crc); 1057 return check_version(sechdrs, versindex, "module_layout", mod, crc);
1063} 1058}
1064 1059
1065/* 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. */
@@ -1098,25 +1093,25 @@ static inline int same_magic(const char *amagic, const char *bmagic,
1098 1093
1099/* 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.
1100 Must be holding module_mutex. */ 1095 Must be holding module_mutex. */
1101static unsigned long resolve_symbol(Elf_Shdr *sechdrs, 1096static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1102 unsigned int versindex, 1097 unsigned int versindex,
1103 const char *name, 1098 const char *name,
1104 struct module *mod) 1099 struct module *mod)
1105{ 1100{
1106 struct module *owner; 1101 struct module *owner;
1107 unsigned long ret; 1102 const struct kernel_symbol *sym;
1108 const unsigned long *crc; 1103 const unsigned long *crc;
1109 1104
1110 ret = find_symbol(name, &owner, &crc, 1105 sym = find_symbol(name, &owner, &crc,
1111 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1106 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1112 if (!IS_ERR_VALUE(ret)) { 1107 /* use_module can fail due to OOM,
1113 /* use_module can fail due to OOM, 1108 or module initialization or unloading */
1114 or module initialization or unloading */ 1109 if (sym) {
1115 if (!check_version(sechdrs, versindex, name, mod, crc) || 1110 if (!check_version(sechdrs, versindex, name, mod, crc) ||
1116 !use_module(mod, owner)) 1111 !use_module(mod, owner))
1117 ret = -EINVAL; 1112 sym = NULL;
1118 } 1113 }
1119 return ret; 1114 return sym;
1120} 1115}
1121 1116
1122/* 1117/*
@@ -1491,6 +1486,9 @@ static void free_module(struct module *mod)
1491 /* Module unload stuff */ 1486 /* Module unload stuff */
1492 module_unload_free(mod); 1487 module_unload_free(mod);
1493 1488
1489 /* Free any allocated parameters. */
1490 destroy_params(mod->kp, mod->num_kp);
1491
1494 /* release any pointers to mcount in this module */ 1492 /* release any pointers to mcount in this module */
1495 ftrace_release(mod->module_core, mod->core_size); 1493 ftrace_release(mod->module_core, mod->core_size);
1496 1494
@@ -1513,17 +1511,15 @@ static void free_module(struct module *mod)
1513void *__symbol_get(const char *symbol) 1511void *__symbol_get(const char *symbol)
1514{ 1512{
1515 struct module *owner; 1513 struct module *owner;
1516 unsigned long value; 1514 const struct kernel_symbol *sym;
1517 1515
1518 preempt_disable(); 1516 preempt_disable();
1519 value = find_symbol(symbol, &owner, NULL, true, true); 1517 sym = find_symbol(symbol, &owner, NULL, true, true);
1520 if (IS_ERR_VALUE(value)) 1518 if (sym && strong_try_module_get(owner))
1521 value = 0; 1519 sym = NULL;
1522 else if (strong_try_module_get(owner))
1523 value = 0;
1524 preempt_enable(); 1520 preempt_enable();
1525 1521
1526 return (void *)value; 1522 return sym ? (void *)sym->value : NULL;
1527} 1523}
1528EXPORT_SYMBOL_GPL(__symbol_get); 1524EXPORT_SYMBOL_GPL(__symbol_get);
1529 1525
@@ -1551,8 +1547,7 @@ static int verify_export_symbols(struct module *mod)
1551 1547
1552 for (i = 0; i < ARRAY_SIZE(arr); i++) { 1548 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1553 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++) {
1554 if (!IS_ERR_VALUE(find_symbol(s->name, &owner, 1550 if (find_symbol(s->name, &owner, NULL, true, false)) {
1555 NULL, true, false))) {
1556 printk(KERN_ERR 1551 printk(KERN_ERR
1557 "%s: exports duplicate symbol %s" 1552 "%s: exports duplicate symbol %s"
1558 " (owned by %s)\n", 1553 " (owned by %s)\n",
@@ -1576,6 +1571,7 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1576 unsigned long secbase; 1571 unsigned long secbase;
1577 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1572 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
1578 int ret = 0; 1573 int ret = 0;
1574 const struct kernel_symbol *ksym;
1579 1575
1580 for (i = 1; i < n; i++) { 1576 for (i = 1; i < n; i++) {
1581 switch (sym[i].st_shndx) { 1577 switch (sym[i].st_shndx) {
@@ -1595,13 +1591,14 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1595 break; 1591 break;
1596 1592
1597 case SHN_UNDEF: 1593 case SHN_UNDEF:
1598 sym[i].st_value 1594 ksym = resolve_symbol(sechdrs, versindex,
1599 = resolve_symbol(sechdrs, versindex, 1595 strtab + sym[i].st_name, mod);
1600 strtab + sym[i].st_name, mod);
1601
1602 /* Ok if resolved. */ 1596 /* Ok if resolved. */
1603 if (!IS_ERR_VALUE(sym[i].st_value)) 1597 if (ksym) {
1598 sym[i].st_value = ksym->value;
1604 break; 1599 break;
1600 }
1601
1605 /* Ok if weak. */ 1602 /* Ok if weak. */
1606 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1603 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1607 break; 1604 break;
@@ -1676,8 +1673,7 @@ static void layout_sections(struct module *mod,
1676 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1673 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1677 || (s->sh_flags & masks[m][1]) 1674 || (s->sh_flags & masks[m][1])
1678 || s->sh_entsize != ~0UL 1675 || s->sh_entsize != ~0UL
1679 || strncmp(secstrings + s->sh_name, 1676 || strstarts(secstrings + s->sh_name, ".init"))
1680 ".init", 5) == 0)
1681 continue; 1677 continue;
1682 s->sh_entsize = get_offset(mod, &mod->core_size, s, i); 1678 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1683 DEBUGP("\t%s\n", secstrings + s->sh_name); 1679 DEBUGP("\t%s\n", secstrings + s->sh_name);
@@ -1694,8 +1690,7 @@ static void layout_sections(struct module *mod,
1694 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1690 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1695 || (s->sh_flags & masks[m][1]) 1691 || (s->sh_flags & masks[m][1])
1696 || s->sh_entsize != ~0UL 1692 || s->sh_entsize != ~0UL
1697 || strncmp(secstrings + s->sh_name, 1693 || !strstarts(secstrings + s->sh_name, ".init"))
1698 ".init", 5) != 0)
1699 continue; 1694 continue;
1700 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i) 1695 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1701 | INIT_OFFSET_MASK); 1696 | INIT_OFFSET_MASK);
@@ -1828,8 +1823,7 @@ static char elf_type(const Elf_Sym *sym,
1828 else 1823 else
1829 return 'b'; 1824 return 'b';
1830 } 1825 }
1831 if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name, 1826 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug"))
1832 ".debug", strlen(".debug")) == 0)
1833 return 'n'; 1827 return 'n';
1834 return '?'; 1828 return '?';
1835} 1829}
@@ -1898,8 +1892,7 @@ static noinline struct module *load_module(void __user *umod,
1898 unsigned int symindex = 0; 1892 unsigned int symindex = 0;
1899 unsigned int strindex = 0; 1893 unsigned int strindex = 0;
1900 unsigned int modindex, versindex, infoindex, pcpuindex; 1894 unsigned int modindex, versindex, infoindex, pcpuindex;
1901 unsigned int num_kp, num_mcount; 1895 unsigned int num_mcount;
1902 struct kernel_param *kp;
1903 struct module *mod; 1896 struct module *mod;
1904 long err = 0; 1897 long err = 0;
1905 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */ 1898 void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
@@ -1916,12 +1909,6 @@ static noinline struct module *load_module(void __user *umod,
1916 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) 1909 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
1917 return ERR_PTR(-ENOMEM); 1910 return ERR_PTR(-ENOMEM);
1918 1911
1919 /* Create stop_machine threads since the error path relies on
1920 * a non-failing stop_machine call. */
1921 err = stop_machine_create();
1922 if (err)
1923 goto free_hdr;
1924
1925 if (copy_from_user(hdr, umod, len) != 0) { 1912 if (copy_from_user(hdr, umod, len) != 0) {
1926 err = -EFAULT; 1913 err = -EFAULT;
1927 goto free_hdr; 1914 goto free_hdr;
@@ -1962,9 +1949,12 @@ static noinline struct module *load_module(void __user *umod,
1962 } 1949 }
1963#ifndef CONFIG_MODULE_UNLOAD 1950#ifndef CONFIG_MODULE_UNLOAD
1964 /* Don't load .exit sections */ 1951 /* Don't load .exit sections */
1965 if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0) 1952 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit"))
1966 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC; 1953 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1967#endif 1954#endif
1955 /* Don't keep __versions around; it's just for loading. */
1956 if (strcmp(secstrings + sechdrs[i].sh_name, "__versions") == 0)
1957 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
1968 } 1958 }
1969 1959
1970 modindex = find_sec(hdr, sechdrs, secstrings, 1960 modindex = find_sec(hdr, sechdrs, secstrings,
@@ -2006,7 +1996,7 @@ static noinline struct module *load_module(void __user *umod,
2006 modmagic = get_modinfo(sechdrs, infoindex, "vermagic"); 1996 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2007 /* This is allowed: modprobe --force will invalidate it. */ 1997 /* This is allowed: modprobe --force will invalidate it. */
2008 if (!modmagic) { 1998 if (!modmagic) {
2009 err = try_to_force_load(mod, "magic"); 1999 err = try_to_force_load(mod, "bad vermagic");
2010 if (err) 2000 if (err)
2011 goto free_hdr; 2001 goto free_hdr;
2012 } else if (!same_magic(modmagic, vermagic, versindex)) { 2002 } else if (!same_magic(modmagic, vermagic, versindex)) {
@@ -2144,8 +2134,8 @@ static noinline struct module *load_module(void __user *umod,
2144 2134
2145 /* Now we've got everything in the final locations, we can 2135 /* Now we've got everything in the final locations, we can
2146 * find optional sections. */ 2136 * find optional sections. */
2147 kp = section_objs(hdr, sechdrs, secstrings, "__param", sizeof(*kp), 2137 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2148 &num_kp); 2138 sizeof(*mod->kp), &mod->num_kp);
2149 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab", 2139 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2150 sizeof(*mod->syms), &mod->num_syms); 2140 sizeof(*mod->syms), &mod->num_syms);
2151 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab"); 2141 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
@@ -2195,8 +2185,8 @@ static noinline struct module *load_module(void __user *umod,
2195 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs) 2185 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2196#endif 2186#endif
2197 ) { 2187 ) {
2198 printk(KERN_WARNING "%s: No versions for exported symbols.\n", mod->name); 2188 err = try_to_force_load(mod,
2199 err = try_to_force_load(mod, "nocrc"); 2189 "no versions for exported symbols");
2200 if (err) 2190 if (err)
2201 goto cleanup; 2191 goto cleanup;
2202 } 2192 }
@@ -2291,11 +2281,11 @@ static noinline struct module *load_module(void __user *umod,
2291 */ 2281 */
2292 list_add_rcu(&mod->list, &modules); 2282 list_add_rcu(&mod->list, &modules);
2293 2283
2294 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);
2295 if (err < 0) 2285 if (err < 0)
2296 goto unlink; 2286 goto unlink;
2297 2287
2298 err = mod_sysfs_setup(mod, kp, num_kp); 2288 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2299 if (err < 0) 2289 if (err < 0)
2300 goto unlink; 2290 goto unlink;
2301 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2291 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
@@ -2304,12 +2294,13 @@ static noinline struct module *load_module(void __user *umod,
2304 /* Get rid of temporary copy */ 2294 /* Get rid of temporary copy */
2305 vfree(hdr); 2295 vfree(hdr);
2306 2296
2307 stop_machine_destroy();
2308 /* Done! */ 2297 /* Done! */
2309 return mod; 2298 return mod;
2310 2299
2311 unlink: 2300 unlink:
2312 stop_machine(__unlink_module, mod, NULL); 2301 /* Unlink carefully: kallsyms could be walking list. */
2302 list_del_rcu(&mod->list);
2303 synchronize_sched();
2313 module_arch_cleanup(mod); 2304 module_arch_cleanup(mod);
2314 cleanup: 2305 cleanup:
2315 kobject_del(&mod->mkobj.kobj); 2306 kobject_del(&mod->mkobj.kobj);
@@ -2317,8 +2308,8 @@ static noinline struct module *load_module(void __user *umod,
2317 ftrace_release(mod->module_core, mod->core_size); 2308 ftrace_release(mod->module_core, mod->core_size);
2318 free_unload: 2309 free_unload:
2319 module_unload_free(mod); 2310 module_unload_free(mod);
2320 free_init:
2321#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP) 2311#if defined(CONFIG_MODULE_UNLOAD) && defined(CONFIG_SMP)
2312 free_init:
2322 percpu_modfree(mod->refptr); 2313 percpu_modfree(mod->refptr);
2323#endif 2314#endif
2324 module_free(mod, mod->module_init); 2315 module_free(mod, mod->module_init);
@@ -2332,7 +2323,6 @@ static noinline struct module *load_module(void __user *umod,
2332 kfree(args); 2323 kfree(args);
2333 free_hdr: 2324 free_hdr:
2334 vfree(hdr); 2325 vfree(hdr);
2335 stop_machine_destroy();
2336 return ERR_PTR(err); 2326 return ERR_PTR(err);
2337 2327
2338 truncated: 2328 truncated:
@@ -2609,6 +2599,25 @@ unsigned long module_kallsyms_lookup_name(const char *name)
2609 preempt_enable(); 2599 preempt_enable();
2610 return ret; 2600 return ret;
2611} 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}
2612#endif /* CONFIG_KALLSYMS */ 2621#endif /* CONFIG_KALLSYMS */
2613 2622
2614static char *module_flags(struct module *mod, char *buf) 2623static char *module_flags(struct module *mod, char *buf)
@@ -2744,29 +2753,31 @@ const struct exception_table_entry *search_module_extables(unsigned long addr)
2744} 2753}
2745 2754
2746/* 2755/*
2747 * 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).
2748 */ 2761 */
2749int is_module_address(unsigned long addr) 2762bool is_module_address(unsigned long addr)
2750{ 2763{
2751 struct module *mod; 2764 bool ret;
2752 2765
2753 preempt_disable(); 2766 preempt_disable();
2754 2767 ret = __module_address(addr) != NULL;
2755 list_for_each_entry_rcu(mod, &modules, list) {
2756 if (within_module_core(addr, mod)) {
2757 preempt_enable();
2758 return 1;
2759 }
2760 }
2761
2762 preempt_enable(); 2768 preempt_enable();
2763 2769
2764 return 0; 2770 return ret;
2765} 2771}
2766 2772
2767 2773/*
2768/* Is this a valid kernel address? */ 2774 * __module_address - get the module which contains an address.
2769__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 */
2780__notrace_funcgraph struct module *__module_address(unsigned long addr)
2770{ 2781{
2771 struct module *mod; 2782 struct module *mod;
2772 2783
@@ -2774,22 +2785,51 @@ __notrace_funcgraph struct module *__module_text_address(unsigned long addr)
2774 return NULL; 2785 return NULL;
2775 2786
2776 list_for_each_entry_rcu(mod, &modules, list) 2787 list_for_each_entry_rcu(mod, &modules, list)
2777 if (within(addr, mod->module_init, mod->init_text_size) 2788 if (within_module_core(addr, mod)
2778 || within(addr, mod->module_core, mod->core_text_size)) 2789 || within_module_init(addr, mod))
2779 return mod; 2790 return mod;
2780 return NULL; 2791 return NULL;
2781} 2792}
2793EXPORT_SYMBOL_GPL(__module_address);
2782 2794
2783struct 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)
2784{ 2804{
2785 struct module *mod; 2805 bool ret;
2786 2806
2787 preempt_disable(); 2807 preempt_disable();
2788 mod = __module_text_address(addr); 2808 ret = __module_text_address(addr) != NULL;
2789 preempt_enable(); 2809 preempt_enable();
2790 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 }
2791 return mod; 2830 return mod;
2792} 2831}
2832EXPORT_SYMBOL_GPL(__module_text_address);
2793 2833
2794/* Don't grab lock, we're oopsing. */ 2834/* Don't grab lock, we're oopsing. */
2795void print_modules(void) 2835void print_modules(void)
@@ -2809,9 +2849,17 @@ void print_modules(void)
2809} 2849}
2810 2850
2811#ifdef CONFIG_MODVERSIONS 2851#ifdef CONFIG_MODVERSIONS
2812/* Generate the signature for struct module here, too, for modversions. */ 2852/* Generate the signature for all relevant module structures here.
2813void struct_module(struct module *mod) { return; } 2853 * If these change, we don't want to try to parse the module. */
2814EXPORT_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);
2815#endif 2863#endif
2816 2864
2817#ifdef CONFIG_MARKERS 2865#ifdef CONFIG_MARKERS