aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c1092
1 files changed, 585 insertions, 507 deletions
diff --git a/kernel/module.c b/kernel/module.c
index 6c562828c85c..ccd641991842 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -1,6 +1,6 @@
1/* 1/*
2 Copyright (C) 2002 Richard Henderson 2 Copyright (C) 2002 Richard Henderson
3 Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM. 3 Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
@@ -110,6 +110,20 @@ int unregister_module_notifier(struct notifier_block * nb)
110} 110}
111EXPORT_SYMBOL(unregister_module_notifier); 111EXPORT_SYMBOL(unregister_module_notifier);
112 112
113struct load_info {
114 Elf_Ehdr *hdr;
115 unsigned long len;
116 Elf_Shdr *sechdrs;
117 char *secstrings, *strtab;
118 unsigned long *strmap;
119 unsigned long symoffs, stroffs;
120 struct _ddebug *debug;
121 unsigned int num_debug;
122 struct {
123 unsigned int sym, str, mod, vers, info, pcpu;
124 } index;
125};
126
113/* We require a truly strong try_module_get(): 0 means failure due to 127/* We require a truly strong try_module_get(): 0 means failure due to
114 ongoing or failed initialization etc. */ 128 ongoing or failed initialization etc. */
115static inline int strong_try_module_get(struct module *mod) 129static inline int strong_try_module_get(struct module *mod)
@@ -140,42 +154,38 @@ void __module_put_and_exit(struct module *mod, long code)
140EXPORT_SYMBOL(__module_put_and_exit); 154EXPORT_SYMBOL(__module_put_and_exit);
141 155
142/* Find a module section: 0 means not found. */ 156/* Find a module section: 0 means not found. */
143static unsigned int find_sec(Elf_Ehdr *hdr, 157static unsigned int find_sec(const struct load_info *info, const char *name)
144 Elf_Shdr *sechdrs,
145 const char *secstrings,
146 const char *name)
147{ 158{
148 unsigned int i; 159 unsigned int i;
149 160
150 for (i = 1; i < hdr->e_shnum; i++) 161 for (i = 1; i < info->hdr->e_shnum; i++) {
162 Elf_Shdr *shdr = &info->sechdrs[i];
151 /* Alloc bit cleared means "ignore it." */ 163 /* Alloc bit cleared means "ignore it." */
152 if ((sechdrs[i].sh_flags & SHF_ALLOC) 164 if ((shdr->sh_flags & SHF_ALLOC)
153 && strcmp(secstrings+sechdrs[i].sh_name, name) == 0) 165 && strcmp(info->secstrings + shdr->sh_name, name) == 0)
154 return i; 166 return i;
167 }
155 return 0; 168 return 0;
156} 169}
157 170
158/* Find a module section, or NULL. */ 171/* Find a module section, or NULL. */
159static void *section_addr(Elf_Ehdr *hdr, Elf_Shdr *shdrs, 172static void *section_addr(const struct load_info *info, const char *name)
160 const char *secstrings, const char *name)
161{ 173{
162 /* Section 0 has sh_addr 0. */ 174 /* Section 0 has sh_addr 0. */
163 return (void *)shdrs[find_sec(hdr, shdrs, secstrings, name)].sh_addr; 175 return (void *)info->sechdrs[find_sec(info, name)].sh_addr;
164} 176}
165 177
166/* Find a module section, or NULL. Fill in number of "objects" in section. */ 178/* Find a module section, or NULL. Fill in number of "objects" in section. */
167static void *section_objs(Elf_Ehdr *hdr, 179static void *section_objs(const struct load_info *info,
168 Elf_Shdr *sechdrs,
169 const char *secstrings,
170 const char *name, 180 const char *name,
171 size_t object_size, 181 size_t object_size,
172 unsigned int *num) 182 unsigned int *num)
173{ 183{
174 unsigned int sec = find_sec(hdr, sechdrs, secstrings, name); 184 unsigned int sec = find_sec(info, name);
175 185
176 /* Section 0 has sh_addr 0 and sh_size 0. */ 186 /* Section 0 has sh_addr 0 and sh_size 0. */
177 *num = sechdrs[sec].sh_size / object_size; 187 *num = info->sechdrs[sec].sh_size / object_size;
178 return (void *)sechdrs[sec].sh_addr; 188 return (void *)info->sechdrs[sec].sh_addr;
179} 189}
180 190
181/* Provided by the linker */ 191/* Provided by the linker */
@@ -227,7 +237,7 @@ bool each_symbol(bool (*fn)(const struct symsearch *arr, struct module *owner,
227 unsigned int symnum, void *data), void *data) 237 unsigned int symnum, void *data), void *data)
228{ 238{
229 struct module *mod; 239 struct module *mod;
230 const struct symsearch arr[] = { 240 static const struct symsearch arr[] = {
231 { __start___ksymtab, __stop___ksymtab, __start___kcrctab, 241 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
232 NOT_GPL_ONLY, false }, 242 NOT_GPL_ONLY, false },
233 { __start___ksymtab_gpl, __stop___ksymtab_gpl, 243 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
@@ -392,7 +402,8 @@ static int percpu_modalloc(struct module *mod,
392 mod->percpu = __alloc_reserved_percpu(size, align); 402 mod->percpu = __alloc_reserved_percpu(size, align);
393 if (!mod->percpu) { 403 if (!mod->percpu) {
394 printk(KERN_WARNING 404 printk(KERN_WARNING
395 "Could not allocate %lu bytes percpu data\n", size); 405 "%s: Could not allocate %lu bytes percpu data\n",
406 mod->name, size);
396 return -ENOMEM; 407 return -ENOMEM;
397 } 408 }
398 mod->percpu_size = size; 409 mod->percpu_size = size;
@@ -404,11 +415,9 @@ static void percpu_modfree(struct module *mod)
404 free_percpu(mod->percpu); 415 free_percpu(mod->percpu);
405} 416}
406 417
407static unsigned int find_pcpusec(Elf_Ehdr *hdr, 418static unsigned int find_pcpusec(struct load_info *info)
408 Elf_Shdr *sechdrs,
409 const char *secstrings)
410{ 419{
411 return find_sec(hdr, sechdrs, secstrings, ".data..percpu"); 420 return find_sec(info, ".data..percpu");
412} 421}
413 422
414static void percpu_modcopy(struct module *mod, 423static void percpu_modcopy(struct module *mod,
@@ -468,9 +477,7 @@ static inline int percpu_modalloc(struct module *mod,
468static inline void percpu_modfree(struct module *mod) 477static inline void percpu_modfree(struct module *mod)
469{ 478{
470} 479}
471static inline unsigned int find_pcpusec(Elf_Ehdr *hdr, 480static unsigned int find_pcpusec(struct load_info *info)
472 Elf_Shdr *sechdrs,
473 const char *secstrings)
474{ 481{
475 return 0; 482 return 0;
476} 483}
@@ -524,21 +531,21 @@ static char last_unloaded_module[MODULE_NAME_LEN+1];
524EXPORT_TRACEPOINT_SYMBOL(module_get); 531EXPORT_TRACEPOINT_SYMBOL(module_get);
525 532
526/* Init the unload section of the module. */ 533/* Init the unload section of the module. */
527static void module_unload_init(struct module *mod) 534static int module_unload_init(struct module *mod)
528{ 535{
529 int cpu; 536 mod->refptr = alloc_percpu(struct module_ref);
537 if (!mod->refptr)
538 return -ENOMEM;
530 539
531 INIT_LIST_HEAD(&mod->source_list); 540 INIT_LIST_HEAD(&mod->source_list);
532 INIT_LIST_HEAD(&mod->target_list); 541 INIT_LIST_HEAD(&mod->target_list);
533 for_each_possible_cpu(cpu) {
534 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
535 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
536 }
537 542
538 /* Hold reference count during initialization. */ 543 /* Hold reference count during initialization. */
539 __this_cpu_write(mod->refptr->incs, 1); 544 __this_cpu_write(mod->refptr->incs, 1);
540 /* Backwards compatibility macros put refcount during init. */ 545 /* Backwards compatibility macros put refcount during init. */
541 mod->waiter = current; 546 mod->waiter = current;
547
548 return 0;
542} 549}
543 550
544/* Does a already use b? */ 551/* Does a already use b? */
@@ -618,6 +625,8 @@ static void module_unload_free(struct module *mod)
618 kfree(use); 625 kfree(use);
619 } 626 }
620 mutex_unlock(&module_mutex); 627 mutex_unlock(&module_mutex);
628
629 free_percpu(mod->refptr);
621} 630}
622 631
623#ifdef CONFIG_MODULE_FORCE_UNLOAD 632#ifdef CONFIG_MODULE_FORCE_UNLOAD
@@ -891,8 +900,9 @@ int ref_module(struct module *a, struct module *b)
891} 900}
892EXPORT_SYMBOL_GPL(ref_module); 901EXPORT_SYMBOL_GPL(ref_module);
893 902
894static inline void module_unload_init(struct module *mod) 903static inline int module_unload_init(struct module *mod)
895{ 904{
905 return 0;
896} 906}
897#endif /* CONFIG_MODULE_UNLOAD */ 907#endif /* CONFIG_MODULE_UNLOAD */
898 908
@@ -1051,10 +1061,9 @@ static inline int same_magic(const char *amagic, const char *bmagic,
1051#endif /* CONFIG_MODVERSIONS */ 1061#endif /* CONFIG_MODVERSIONS */
1052 1062
1053/* Resolve a symbol for this module. I.e. if we find one, record usage. */ 1063/* Resolve a symbol for this module. I.e. if we find one, record usage. */
1054static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, 1064static const struct kernel_symbol *resolve_symbol(struct module *mod,
1055 unsigned int versindex, 1065 const struct load_info *info,
1056 const char *name, 1066 const char *name,
1057 struct module *mod,
1058 char ownername[]) 1067 char ownername[])
1059{ 1068{
1060 struct module *owner; 1069 struct module *owner;
@@ -1068,7 +1077,8 @@ static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1068 if (!sym) 1077 if (!sym)
1069 goto unlock; 1078 goto unlock;
1070 1079
1071 if (!check_version(sechdrs, versindex, name, mod, crc, owner)) { 1080 if (!check_version(info->sechdrs, info->index.vers, name, mod, crc,
1081 owner)) {
1072 sym = ERR_PTR(-EINVAL); 1082 sym = ERR_PTR(-EINVAL);
1073 goto getname; 1083 goto getname;
1074 } 1084 }
@@ -1087,21 +1097,20 @@ unlock:
1087 return sym; 1097 return sym;
1088} 1098}
1089 1099
1090static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs, 1100static const struct kernel_symbol *
1091 unsigned int versindex, 1101resolve_symbol_wait(struct module *mod,
1092 const char *name, 1102 const struct load_info *info,
1093 struct module *mod) 1103 const char *name)
1094{ 1104{
1095 const struct kernel_symbol *ksym; 1105 const struct kernel_symbol *ksym;
1096 char ownername[MODULE_NAME_LEN]; 1106 char owner[MODULE_NAME_LEN];
1097 1107
1098 if (wait_event_interruptible_timeout(module_wq, 1108 if (wait_event_interruptible_timeout(module_wq,
1099 !IS_ERR(ksym = resolve_symbol(sechdrs, versindex, name, 1109 !IS_ERR(ksym = resolve_symbol(mod, info, name, owner))
1100 mod, ownername)) || 1110 || PTR_ERR(ksym) != -EBUSY,
1101 PTR_ERR(ksym) != -EBUSY,
1102 30 * HZ) <= 0) { 1111 30 * HZ) <= 0) {
1103 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n", 1112 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1104 mod->name, ownername); 1113 mod->name, owner);
1105 } 1114 }
1106 return ksym; 1115 return ksym;
1107} 1116}
@@ -1110,8 +1119,9 @@ static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs,
1110 * /sys/module/foo/sections stuff 1119 * /sys/module/foo/sections stuff
1111 * J. Corbet <corbet@lwn.net> 1120 * J. Corbet <corbet@lwn.net>
1112 */ 1121 */
1113#if defined(CONFIG_KALLSYMS) && defined(CONFIG_SYSFS) 1122#ifdef CONFIG_SYSFS
1114 1123
1124#ifdef CONFIG_KALLSYMS
1115static inline bool sect_empty(const Elf_Shdr *sect) 1125static inline bool sect_empty(const Elf_Shdr *sect)
1116{ 1126{
1117 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0; 1127 return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size == 0;
@@ -1148,8 +1158,7 @@ static void free_sect_attrs(struct module_sect_attrs *sect_attrs)
1148 kfree(sect_attrs); 1158 kfree(sect_attrs);
1149} 1159}
1150 1160
1151static void add_sect_attrs(struct module *mod, unsigned int nsect, 1161static void add_sect_attrs(struct module *mod, const struct load_info *info)
1152 char *secstrings, Elf_Shdr *sechdrs)
1153{ 1162{
1154 unsigned int nloaded = 0, i, size[2]; 1163 unsigned int nloaded = 0, i, size[2];
1155 struct module_sect_attrs *sect_attrs; 1164 struct module_sect_attrs *sect_attrs;
@@ -1157,8 +1166,8 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
1157 struct attribute **gattr; 1166 struct attribute **gattr;
1158 1167
1159 /* Count loaded sections and allocate structures */ 1168 /* Count loaded sections and allocate structures */
1160 for (i = 0; i < nsect; i++) 1169 for (i = 0; i < info->hdr->e_shnum; i++)
1161 if (!sect_empty(&sechdrs[i])) 1170 if (!sect_empty(&info->sechdrs[i]))
1162 nloaded++; 1171 nloaded++;
1163 size[0] = ALIGN(sizeof(*sect_attrs) 1172 size[0] = ALIGN(sizeof(*sect_attrs)
1164 + nloaded * sizeof(sect_attrs->attrs[0]), 1173 + nloaded * sizeof(sect_attrs->attrs[0]),
@@ -1175,11 +1184,12 @@ static void add_sect_attrs(struct module *mod, unsigned int nsect,
1175 sect_attrs->nsections = 0; 1184 sect_attrs->nsections = 0;
1176 sattr = &sect_attrs->attrs[0]; 1185 sattr = &sect_attrs->attrs[0];
1177 gattr = &sect_attrs->grp.attrs[0]; 1186 gattr = &sect_attrs->grp.attrs[0];
1178 for (i = 0; i < nsect; i++) { 1187 for (i = 0; i < info->hdr->e_shnum; i++) {
1179 if (sect_empty(&sechdrs[i])) 1188 Elf_Shdr *sec = &info->sechdrs[i];
1189 if (sect_empty(sec))
1180 continue; 1190 continue;
1181 sattr->address = sechdrs[i].sh_addr; 1191 sattr->address = sec->sh_addr;
1182 sattr->name = kstrdup(secstrings + sechdrs[i].sh_name, 1192 sattr->name = kstrdup(info->secstrings + sec->sh_name,
1183 GFP_KERNEL); 1193 GFP_KERNEL);
1184 if (sattr->name == NULL) 1194 if (sattr->name == NULL)
1185 goto out; 1195 goto out;
@@ -1247,8 +1257,7 @@ static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
1247 kfree(notes_attrs); 1257 kfree(notes_attrs);
1248} 1258}
1249 1259
1250static void add_notes_attrs(struct module *mod, unsigned int nsect, 1260static void add_notes_attrs(struct module *mod, const struct load_info *info)
1251 char *secstrings, Elf_Shdr *sechdrs)
1252{ 1261{
1253 unsigned int notes, loaded, i; 1262 unsigned int notes, loaded, i;
1254 struct module_notes_attrs *notes_attrs; 1263 struct module_notes_attrs *notes_attrs;
@@ -1260,9 +1269,9 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1260 1269
1261 /* Count notes sections and allocate structures. */ 1270 /* Count notes sections and allocate structures. */
1262 notes = 0; 1271 notes = 0;
1263 for (i = 0; i < nsect; i++) 1272 for (i = 0; i < info->hdr->e_shnum; i++)
1264 if (!sect_empty(&sechdrs[i]) && 1273 if (!sect_empty(&info->sechdrs[i]) &&
1265 (sechdrs[i].sh_type == SHT_NOTE)) 1274 (info->sechdrs[i].sh_type == SHT_NOTE))
1266 ++notes; 1275 ++notes;
1267 1276
1268 if (notes == 0) 1277 if (notes == 0)
@@ -1276,15 +1285,15 @@ static void add_notes_attrs(struct module *mod, unsigned int nsect,
1276 1285
1277 notes_attrs->notes = notes; 1286 notes_attrs->notes = notes;
1278 nattr = &notes_attrs->attrs[0]; 1287 nattr = &notes_attrs->attrs[0];
1279 for (loaded = i = 0; i < nsect; ++i) { 1288 for (loaded = i = 0; i < info->hdr->e_shnum; ++i) {
1280 if (sect_empty(&sechdrs[i])) 1289 if (sect_empty(&info->sechdrs[i]))
1281 continue; 1290 continue;
1282 if (sechdrs[i].sh_type == SHT_NOTE) { 1291 if (info->sechdrs[i].sh_type == SHT_NOTE) {
1283 sysfs_bin_attr_init(nattr); 1292 sysfs_bin_attr_init(nattr);
1284 nattr->attr.name = mod->sect_attrs->attrs[loaded].name; 1293 nattr->attr.name = mod->sect_attrs->attrs[loaded].name;
1285 nattr->attr.mode = S_IRUGO; 1294 nattr->attr.mode = S_IRUGO;
1286 nattr->size = sechdrs[i].sh_size; 1295 nattr->size = info->sechdrs[i].sh_size;
1287 nattr->private = (void *) sechdrs[i].sh_addr; 1296 nattr->private = (void *) info->sechdrs[i].sh_addr;
1288 nattr->read = module_notes_read; 1297 nattr->read = module_notes_read;
1289 ++nattr; 1298 ++nattr;
1290 } 1299 }
@@ -1315,8 +1324,8 @@ static void remove_notes_attrs(struct module *mod)
1315 1324
1316#else 1325#else
1317 1326
1318static inline void add_sect_attrs(struct module *mod, unsigned int nsect, 1327static inline void add_sect_attrs(struct module *mod,
1319 char *sectstrings, Elf_Shdr *sechdrs) 1328 const struct load_info *info)
1320{ 1329{
1321} 1330}
1322 1331
@@ -1324,17 +1333,16 @@ static inline void remove_sect_attrs(struct module *mod)
1324{ 1333{
1325} 1334}
1326 1335
1327static inline void add_notes_attrs(struct module *mod, unsigned int nsect, 1336static inline void add_notes_attrs(struct module *mod,
1328 char *sectstrings, Elf_Shdr *sechdrs) 1337 const struct load_info *info)
1329{ 1338{
1330} 1339}
1331 1340
1332static inline void remove_notes_attrs(struct module *mod) 1341static inline void remove_notes_attrs(struct module *mod)
1333{ 1342{
1334} 1343}
1335#endif 1344#endif /* CONFIG_KALLSYMS */
1336 1345
1337#ifdef CONFIG_SYSFS
1338static void add_usage_links(struct module *mod) 1346static void add_usage_links(struct module *mod)
1339{ 1347{
1340#ifdef CONFIG_MODULE_UNLOAD 1348#ifdef CONFIG_MODULE_UNLOAD
@@ -1439,6 +1447,7 @@ out:
1439} 1447}
1440 1448
1441static int mod_sysfs_setup(struct module *mod, 1449static int mod_sysfs_setup(struct module *mod,
1450 const struct load_info *info,
1442 struct kernel_param *kparam, 1451 struct kernel_param *kparam,
1443 unsigned int num_params) 1452 unsigned int num_params)
1444{ 1453{
@@ -1463,6 +1472,8 @@ static int mod_sysfs_setup(struct module *mod,
1463 goto out_unreg_param; 1472 goto out_unreg_param;
1464 1473
1465 add_usage_links(mod); 1474 add_usage_links(mod);
1475 add_sect_attrs(mod, info);
1476 add_notes_attrs(mod, info);
1466 1477
1467 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); 1478 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1468 return 0; 1479 return 0;
@@ -1479,33 +1490,26 @@ out:
1479 1490
1480static void mod_sysfs_fini(struct module *mod) 1491static void mod_sysfs_fini(struct module *mod)
1481{ 1492{
1493 remove_notes_attrs(mod);
1494 remove_sect_attrs(mod);
1482 kobject_put(&mod->mkobj.kobj); 1495 kobject_put(&mod->mkobj.kobj);
1483} 1496}
1484 1497
1485#else /* CONFIG_SYSFS */ 1498#else /* !CONFIG_SYSFS */
1486
1487static inline int mod_sysfs_init(struct module *mod)
1488{
1489 return 0;
1490}
1491 1499
1492static inline int mod_sysfs_setup(struct module *mod, 1500static int mod_sysfs_setup(struct module *mod,
1501 const struct load_info *info,
1493 struct kernel_param *kparam, 1502 struct kernel_param *kparam,
1494 unsigned int num_params) 1503 unsigned int num_params)
1495{ 1504{
1496 return 0; 1505 return 0;
1497} 1506}
1498 1507
1499static inline int module_add_modinfo_attrs(struct module *mod) 1508static void mod_sysfs_fini(struct module *mod)
1500{
1501 return 0;
1502}
1503
1504static inline void module_remove_modinfo_attrs(struct module *mod)
1505{ 1509{
1506} 1510}
1507 1511
1508static void mod_sysfs_fini(struct module *mod) 1512static void module_remove_modinfo_attrs(struct module *mod)
1509{ 1513{
1510} 1514}
1511 1515
@@ -1515,7 +1519,7 @@ static void del_usage_links(struct module *mod)
1515 1519
1516#endif /* CONFIG_SYSFS */ 1520#endif /* CONFIG_SYSFS */
1517 1521
1518static void mod_kobject_remove(struct module *mod) 1522static void mod_sysfs_teardown(struct module *mod)
1519{ 1523{
1520 del_usage_links(mod); 1524 del_usage_links(mod);
1521 module_remove_modinfo_attrs(mod); 1525 module_remove_modinfo_attrs(mod);
@@ -1533,6 +1537,7 @@ static int __unlink_module(void *_mod)
1533{ 1537{
1534 struct module *mod = _mod; 1538 struct module *mod = _mod;
1535 list_del(&mod->list); 1539 list_del(&mod->list);
1540 module_bug_cleanup(mod);
1536 return 0; 1541 return 0;
1537} 1542}
1538 1543
@@ -1545,9 +1550,7 @@ static void free_module(struct module *mod)
1545 mutex_lock(&module_mutex); 1550 mutex_lock(&module_mutex);
1546 stop_machine(__unlink_module, mod, NULL); 1551 stop_machine(__unlink_module, mod, NULL);
1547 mutex_unlock(&module_mutex); 1552 mutex_unlock(&module_mutex);
1548 remove_notes_attrs(mod); 1553 mod_sysfs_teardown(mod);
1549 remove_sect_attrs(mod);
1550 mod_kobject_remove(mod);
1551 1554
1552 /* Remove dynamic debug info */ 1555 /* Remove dynamic debug info */
1553 ddebug_remove_module(mod->name); 1556 ddebug_remove_module(mod->name);
@@ -1565,10 +1568,7 @@ static void free_module(struct module *mod)
1565 module_free(mod, mod->module_init); 1568 module_free(mod, mod->module_init);
1566 kfree(mod->args); 1569 kfree(mod->args);
1567 percpu_modfree(mod); 1570 percpu_modfree(mod);
1568#if defined(CONFIG_MODULE_UNLOAD) 1571
1569 if (mod->refptr)
1570 free_percpu(mod->refptr);
1571#endif
1572 /* Free lock-classes: */ 1572 /* Free lock-classes: */
1573 lockdep_free_key_range(mod->module_core, mod->core_size); 1573 lockdep_free_key_range(mod->module_core, mod->core_size);
1574 1574
@@ -1634,25 +1634,23 @@ static int verify_export_symbols(struct module *mod)
1634} 1634}
1635 1635
1636/* Change all symbols so that st_value encodes the pointer directly. */ 1636/* Change all symbols so that st_value encodes the pointer directly. */
1637static int simplify_symbols(Elf_Shdr *sechdrs, 1637static int simplify_symbols(struct module *mod, const struct load_info *info)
1638 unsigned int symindex, 1638{
1639 const char *strtab, 1639 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
1640 unsigned int versindex, 1640 Elf_Sym *sym = (void *)symsec->sh_addr;
1641 unsigned int pcpuindex,
1642 struct module *mod)
1643{
1644 Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
1645 unsigned long secbase; 1641 unsigned long secbase;
1646 unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 1642 unsigned int i;
1647 int ret = 0; 1643 int ret = 0;
1648 const struct kernel_symbol *ksym; 1644 const struct kernel_symbol *ksym;
1649 1645
1650 for (i = 1; i < n; i++) { 1646 for (i = 1; i < symsec->sh_size / sizeof(Elf_Sym); i++) {
1647 const char *name = info->strtab + sym[i].st_name;
1648
1651 switch (sym[i].st_shndx) { 1649 switch (sym[i].st_shndx) {
1652 case SHN_COMMON: 1650 case SHN_COMMON:
1653 /* We compiled with -fno-common. These are not 1651 /* We compiled with -fno-common. These are not
1654 supposed to happen. */ 1652 supposed to happen. */
1655 DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name); 1653 DEBUGP("Common symbol: %s\n", name);
1656 printk("%s: please compile with -fno-common\n", 1654 printk("%s: please compile with -fno-common\n",
1657 mod->name); 1655 mod->name);
1658 ret = -ENOEXEC; 1656 ret = -ENOEXEC;
@@ -1665,9 +1663,7 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1665 break; 1663 break;
1666 1664
1667 case SHN_UNDEF: 1665 case SHN_UNDEF:
1668 ksym = resolve_symbol_wait(sechdrs, versindex, 1666 ksym = resolve_symbol_wait(mod, info, name);
1669 strtab + sym[i].st_name,
1670 mod);
1671 /* Ok if resolved. */ 1667 /* Ok if resolved. */
1672 if (ksym && !IS_ERR(ksym)) { 1668 if (ksym && !IS_ERR(ksym)) {
1673 sym[i].st_value = ksym->value; 1669 sym[i].st_value = ksym->value;
@@ -1679,17 +1675,16 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1679 break; 1675 break;
1680 1676
1681 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n", 1677 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1682 mod->name, strtab + sym[i].st_name, 1678 mod->name, name, PTR_ERR(ksym));
1683 PTR_ERR(ksym));
1684 ret = PTR_ERR(ksym) ?: -ENOENT; 1679 ret = PTR_ERR(ksym) ?: -ENOENT;
1685 break; 1680 break;
1686 1681
1687 default: 1682 default:
1688 /* Divert to percpu allocation if a percpu var. */ 1683 /* Divert to percpu allocation if a percpu var. */
1689 if (sym[i].st_shndx == pcpuindex) 1684 if (sym[i].st_shndx == info->index.pcpu)
1690 secbase = (unsigned long)mod_percpu(mod); 1685 secbase = (unsigned long)mod_percpu(mod);
1691 else 1686 else
1692 secbase = sechdrs[sym[i].st_shndx].sh_addr; 1687 secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
1693 sym[i].st_value += secbase; 1688 sym[i].st_value += secbase;
1694 break; 1689 break;
1695 } 1690 }
@@ -1698,6 +1693,35 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1698 return ret; 1693 return ret;
1699} 1694}
1700 1695
1696static int apply_relocations(struct module *mod, const struct load_info *info)
1697{
1698 unsigned int i;
1699 int err = 0;
1700
1701 /* Now do relocations. */
1702 for (i = 1; i < info->hdr->e_shnum; i++) {
1703 unsigned int infosec = info->sechdrs[i].sh_info;
1704
1705 /* Not a valid relocation section? */
1706 if (infosec >= info->hdr->e_shnum)
1707 continue;
1708
1709 /* Don't bother with non-allocated sections */
1710 if (!(info->sechdrs[infosec].sh_flags & SHF_ALLOC))
1711 continue;
1712
1713 if (info->sechdrs[i].sh_type == SHT_REL)
1714 err = apply_relocate(info->sechdrs, info->strtab,
1715 info->index.sym, i, mod);
1716 else if (info->sechdrs[i].sh_type == SHT_RELA)
1717 err = apply_relocate_add(info->sechdrs, info->strtab,
1718 info->index.sym, i, mod);
1719 if (err < 0)
1720 break;
1721 }
1722 return err;
1723}
1724
1701/* Additional bytes needed by arch in front of individual sections */ 1725/* Additional bytes needed by arch in front of individual sections */
1702unsigned int __weak arch_mod_section_prepend(struct module *mod, 1726unsigned int __weak arch_mod_section_prepend(struct module *mod,
1703 unsigned int section) 1727 unsigned int section)
@@ -1722,10 +1746,7 @@ static long get_offset(struct module *mod, unsigned int *size,
1722 might -- code, read-only data, read-write data, small data. Tally 1746 might -- code, read-only data, read-write data, small data. Tally
1723 sizes, and place the offsets into sh_entsize fields: high bit means it 1747 sizes, and place the offsets into sh_entsize fields: high bit means it
1724 belongs in init. */ 1748 belongs in init. */
1725static void layout_sections(struct module *mod, 1749static void layout_sections(struct module *mod, struct load_info *info)
1726 const Elf_Ehdr *hdr,
1727 Elf_Shdr *sechdrs,
1728 const char *secstrings)
1729{ 1750{
1730 static unsigned long const masks[][2] = { 1751 static unsigned long const masks[][2] = {
1731 /* NOTE: all executable code must be the first section 1752 /* NOTE: all executable code must be the first section
@@ -1738,21 +1759,22 @@ static void layout_sections(struct module *mod,
1738 }; 1759 };
1739 unsigned int m, i; 1760 unsigned int m, i;
1740 1761
1741 for (i = 0; i < hdr->e_shnum; i++) 1762 for (i = 0; i < info->hdr->e_shnum; i++)
1742 sechdrs[i].sh_entsize = ~0UL; 1763 info->sechdrs[i].sh_entsize = ~0UL;
1743 1764
1744 DEBUGP("Core section allocation order:\n"); 1765 DEBUGP("Core section allocation order:\n");
1745 for (m = 0; m < ARRAY_SIZE(masks); ++m) { 1766 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1746 for (i = 0; i < hdr->e_shnum; ++i) { 1767 for (i = 0; i < info->hdr->e_shnum; ++i) {
1747 Elf_Shdr *s = &sechdrs[i]; 1768 Elf_Shdr *s = &info->sechdrs[i];
1769 const char *sname = info->secstrings + s->sh_name;
1748 1770
1749 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1771 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1750 || (s->sh_flags & masks[m][1]) 1772 || (s->sh_flags & masks[m][1])
1751 || s->sh_entsize != ~0UL 1773 || s->sh_entsize != ~0UL
1752 || strstarts(secstrings + s->sh_name, ".init")) 1774 || strstarts(sname, ".init"))
1753 continue; 1775 continue;
1754 s->sh_entsize = get_offset(mod, &mod->core_size, s, i); 1776 s->sh_entsize = get_offset(mod, &mod->core_size, s, i);
1755 DEBUGP("\t%s\n", secstrings + s->sh_name); 1777 DEBUGP("\t%s\n", name);
1756 } 1778 }
1757 if (m == 0) 1779 if (m == 0)
1758 mod->core_text_size = mod->core_size; 1780 mod->core_text_size = mod->core_size;
@@ -1760,17 +1782,18 @@ static void layout_sections(struct module *mod,
1760 1782
1761 DEBUGP("Init section allocation order:\n"); 1783 DEBUGP("Init section allocation order:\n");
1762 for (m = 0; m < ARRAY_SIZE(masks); ++m) { 1784 for (m = 0; m < ARRAY_SIZE(masks); ++m) {
1763 for (i = 0; i < hdr->e_shnum; ++i) { 1785 for (i = 0; i < info->hdr->e_shnum; ++i) {
1764 Elf_Shdr *s = &sechdrs[i]; 1786 Elf_Shdr *s = &info->sechdrs[i];
1787 const char *sname = info->secstrings + s->sh_name;
1765 1788
1766 if ((s->sh_flags & masks[m][0]) != masks[m][0] 1789 if ((s->sh_flags & masks[m][0]) != masks[m][0]
1767 || (s->sh_flags & masks[m][1]) 1790 || (s->sh_flags & masks[m][1])
1768 || s->sh_entsize != ~0UL 1791 || s->sh_entsize != ~0UL
1769 || !strstarts(secstrings + s->sh_name, ".init")) 1792 || !strstarts(sname, ".init"))
1770 continue; 1793 continue;
1771 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i) 1794 s->sh_entsize = (get_offset(mod, &mod->init_size, s, i)
1772 | INIT_OFFSET_MASK); 1795 | INIT_OFFSET_MASK);
1773 DEBUGP("\t%s\n", secstrings + s->sh_name); 1796 DEBUGP("\t%s\n", sname);
1774 } 1797 }
1775 if (m == 0) 1798 if (m == 0)
1776 mod->init_text_size = mod->init_size; 1799 mod->init_text_size = mod->init_size;
@@ -1809,33 +1832,28 @@ static char *next_string(char *string, unsigned long *secsize)
1809 return string; 1832 return string;
1810} 1833}
1811 1834
1812static char *get_modinfo(Elf_Shdr *sechdrs, 1835static char *get_modinfo(struct load_info *info, const char *tag)
1813 unsigned int info,
1814 const char *tag)
1815{ 1836{
1816 char *p; 1837 char *p;
1817 unsigned int taglen = strlen(tag); 1838 unsigned int taglen = strlen(tag);
1818 unsigned long size = sechdrs[info].sh_size; 1839 Elf_Shdr *infosec = &info->sechdrs[info->index.info];
1840 unsigned long size = infosec->sh_size;
1819 1841
1820 for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) { 1842 for (p = (char *)infosec->sh_addr; p; p = next_string(p, &size)) {
1821 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') 1843 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
1822 return p + taglen + 1; 1844 return p + taglen + 1;
1823 } 1845 }
1824 return NULL; 1846 return NULL;
1825} 1847}
1826 1848
1827static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs, 1849static void setup_modinfo(struct module *mod, struct load_info *info)
1828 unsigned int infoindex)
1829{ 1850{
1830 struct module_attribute *attr; 1851 struct module_attribute *attr;
1831 int i; 1852 int i;
1832 1853
1833 for (i = 0; (attr = modinfo_attrs[i]); i++) { 1854 for (i = 0; (attr = modinfo_attrs[i]); i++) {
1834 if (attr->setup) 1855 if (attr->setup)
1835 attr->setup(mod, 1856 attr->setup(mod, get_modinfo(info, attr->attr.name));
1836 get_modinfo(sechdrs,
1837 infoindex,
1838 attr->attr.name));
1839 } 1857 }
1840} 1858}
1841 1859
@@ -1876,11 +1894,10 @@ static int is_exported(const char *name, unsigned long value,
1876} 1894}
1877 1895
1878/* As per nm */ 1896/* As per nm */
1879static char elf_type(const Elf_Sym *sym, 1897static char elf_type(const Elf_Sym *sym, const struct load_info *info)
1880 Elf_Shdr *sechdrs,
1881 const char *secstrings,
1882 struct module *mod)
1883{ 1898{
1899 const Elf_Shdr *sechdrs = info->sechdrs;
1900
1884 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) { 1901 if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
1885 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) 1902 if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
1886 return 'v'; 1903 return 'v';
@@ -1910,8 +1927,10 @@ static char elf_type(const Elf_Sym *sym,
1910 else 1927 else
1911 return 'b'; 1928 return 'b';
1912 } 1929 }
1913 if (strstarts(secstrings + sechdrs[sym->st_shndx].sh_name, ".debug")) 1930 if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name,
1931 ".debug")) {
1914 return 'n'; 1932 return 'n';
1933 }
1915 return '?'; 1934 return '?';
1916} 1935}
1917 1936
@@ -1936,127 +1955,96 @@ static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
1936 return true; 1955 return true;
1937} 1956}
1938 1957
1939static unsigned long layout_symtab(struct module *mod, 1958static void layout_symtab(struct module *mod, struct load_info *info)
1940 Elf_Shdr *sechdrs,
1941 unsigned int symindex,
1942 unsigned int strindex,
1943 const Elf_Ehdr *hdr,
1944 const char *secstrings,
1945 unsigned long *pstroffs,
1946 unsigned long *strmap)
1947{ 1959{
1948 unsigned long symoffs; 1960 Elf_Shdr *symsect = info->sechdrs + info->index.sym;
1949 Elf_Shdr *symsect = sechdrs + symindex; 1961 Elf_Shdr *strsect = info->sechdrs + info->index.str;
1950 Elf_Shdr *strsect = sechdrs + strindex;
1951 const Elf_Sym *src; 1962 const Elf_Sym *src;
1952 const char *strtab;
1953 unsigned int i, nsrc, ndst; 1963 unsigned int i, nsrc, ndst;
1954 1964
1955 /* Put symbol section at end of init part of module. */ 1965 /* Put symbol section at end of init part of module. */
1956 symsect->sh_flags |= SHF_ALLOC; 1966 symsect->sh_flags |= SHF_ALLOC;
1957 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect, 1967 symsect->sh_entsize = get_offset(mod, &mod->init_size, symsect,
1958 symindex) | INIT_OFFSET_MASK; 1968 info->index.sym) | INIT_OFFSET_MASK;
1959 DEBUGP("\t%s\n", secstrings + symsect->sh_name); 1969 DEBUGP("\t%s\n", info->secstrings + symsect->sh_name);
1960 1970
1961 src = (void *)hdr + symsect->sh_offset; 1971 src = (void *)info->hdr + symsect->sh_offset;
1962 nsrc = symsect->sh_size / sizeof(*src); 1972 nsrc = symsect->sh_size / sizeof(*src);
1963 strtab = (void *)hdr + strsect->sh_offset;
1964 for (ndst = i = 1; i < nsrc; ++i, ++src) 1973 for (ndst = i = 1; i < nsrc; ++i, ++src)
1965 if (is_core_symbol(src, sechdrs, hdr->e_shnum)) { 1974 if (is_core_symbol(src, info->sechdrs, info->hdr->e_shnum)) {
1966 unsigned int j = src->st_name; 1975 unsigned int j = src->st_name;
1967 1976
1968 while(!__test_and_set_bit(j, strmap) && strtab[j]) 1977 while (!__test_and_set_bit(j, info->strmap)
1978 && info->strtab[j])
1969 ++j; 1979 ++j;
1970 ++ndst; 1980 ++ndst;
1971 } 1981 }
1972 1982
1973 /* Append room for core symbols at end of core part. */ 1983 /* Append room for core symbols at end of core part. */
1974 symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1); 1984 info->symoffs = ALIGN(mod->core_size, symsect->sh_addralign ?: 1);
1975 mod->core_size = symoffs + ndst * sizeof(Elf_Sym); 1985 mod->core_size = info->symoffs + ndst * sizeof(Elf_Sym);
1976 1986
1977 /* Put string table section at end of init part of module. */ 1987 /* Put string table section at end of init part of module. */
1978 strsect->sh_flags |= SHF_ALLOC; 1988 strsect->sh_flags |= SHF_ALLOC;
1979 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect, 1989 strsect->sh_entsize = get_offset(mod, &mod->init_size, strsect,
1980 strindex) | INIT_OFFSET_MASK; 1990 info->index.str) | INIT_OFFSET_MASK;
1981 DEBUGP("\t%s\n", secstrings + strsect->sh_name); 1991 DEBUGP("\t%s\n", info->secstrings + strsect->sh_name);
1982 1992
1983 /* Append room for core symbols' strings at end of core part. */ 1993 /* Append room for core symbols' strings at end of core part. */
1984 *pstroffs = mod->core_size; 1994 info->stroffs = mod->core_size;
1985 __set_bit(0, strmap); 1995 __set_bit(0, info->strmap);
1986 mod->core_size += bitmap_weight(strmap, strsect->sh_size); 1996 mod->core_size += bitmap_weight(info->strmap, strsect->sh_size);
1987
1988 return symoffs;
1989} 1997}
1990 1998
1991static void add_kallsyms(struct module *mod, 1999static void add_kallsyms(struct module *mod, const struct load_info *info)
1992 Elf_Shdr *sechdrs,
1993 unsigned int shnum,
1994 unsigned int symindex,
1995 unsigned int strindex,
1996 unsigned long symoffs,
1997 unsigned long stroffs,
1998 const char *secstrings,
1999 unsigned long *strmap)
2000{ 2000{
2001 unsigned int i, ndst; 2001 unsigned int i, ndst;
2002 const Elf_Sym *src; 2002 const Elf_Sym *src;
2003 Elf_Sym *dst; 2003 Elf_Sym *dst;
2004 char *s; 2004 char *s;
2005 Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
2005 2006
2006 mod->symtab = (void *)sechdrs[symindex].sh_addr; 2007 mod->symtab = (void *)symsec->sh_addr;
2007 mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym); 2008 mod->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
2008 mod->strtab = (void *)sechdrs[strindex].sh_addr; 2009 /* Make sure we get permanent strtab: don't use info->strtab. */
2010 mod->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
2009 2011
2010 /* Set types up while we still have access to sections. */ 2012 /* Set types up while we still have access to sections. */
2011 for (i = 0; i < mod->num_symtab; i++) 2013 for (i = 0; i < mod->num_symtab; i++)
2012 mod->symtab[i].st_info 2014 mod->symtab[i].st_info = elf_type(&mod->symtab[i], info);
2013 = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
2014 2015
2015 mod->core_symtab = dst = mod->module_core + symoffs; 2016 mod->core_symtab = dst = mod->module_core + info->symoffs;
2016 src = mod->symtab; 2017 src = mod->symtab;
2017 *dst = *src; 2018 *dst = *src;
2018 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) { 2019 for (ndst = i = 1; i < mod->num_symtab; ++i, ++src) {
2019 if (!is_core_symbol(src, sechdrs, shnum)) 2020 if (!is_core_symbol(src, info->sechdrs, info->hdr->e_shnum))
2020 continue; 2021 continue;
2021 dst[ndst] = *src; 2022 dst[ndst] = *src;
2022 dst[ndst].st_name = bitmap_weight(strmap, dst[ndst].st_name); 2023 dst[ndst].st_name = bitmap_weight(info->strmap,
2024 dst[ndst].st_name);
2023 ++ndst; 2025 ++ndst;
2024 } 2026 }
2025 mod->core_num_syms = ndst; 2027 mod->core_num_syms = ndst;
2026 2028
2027 mod->core_strtab = s = mod->module_core + stroffs; 2029 mod->core_strtab = s = mod->module_core + info->stroffs;
2028 for (*s = 0, i = 1; i < sechdrs[strindex].sh_size; ++i) 2030 for (*s = 0, i = 1; i < info->sechdrs[info->index.str].sh_size; ++i)
2029 if (test_bit(i, strmap)) 2031 if (test_bit(i, info->strmap))
2030 *++s = mod->strtab[i]; 2032 *++s = mod->strtab[i];
2031} 2033}
2032#else 2034#else
2033static inline unsigned long layout_symtab(struct module *mod, 2035static inline void layout_symtab(struct module *mod, struct load_info *info)
2034 Elf_Shdr *sechdrs,
2035 unsigned int symindex,
2036 unsigned int strindex,
2037 const Elf_Ehdr *hdr,
2038 const char *secstrings,
2039 unsigned long *pstroffs,
2040 unsigned long *strmap)
2041{ 2036{
2042 return 0;
2043} 2037}
2044 2038
2045static inline void add_kallsyms(struct module *mod, 2039static void add_kallsyms(struct module *mod, struct load_info *info)
2046 Elf_Shdr *sechdrs,
2047 unsigned int shnum,
2048 unsigned int symindex,
2049 unsigned int strindex,
2050 unsigned long symoffs,
2051 unsigned long stroffs,
2052 const char *secstrings,
2053 const unsigned long *strmap)
2054{ 2040{
2055} 2041}
2056#endif /* CONFIG_KALLSYMS */ 2042#endif /* CONFIG_KALLSYMS */
2057 2043
2058static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num) 2044static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
2059{ 2045{
2046 if (!debug)
2047 return;
2060#ifdef CONFIG_DYNAMIC_DEBUG 2048#ifdef CONFIG_DYNAMIC_DEBUG
2061 if (ddebug_add_module(debug, num, debug->modname)) 2049 if (ddebug_add_module(debug, num, debug->modname))
2062 printk(KERN_ERR "dynamic debug error adding module: %s\n", 2050 printk(KERN_ERR "dynamic debug error adding module: %s\n",
@@ -2087,65 +2075,47 @@ static void *module_alloc_update_bounds(unsigned long size)
2087} 2075}
2088 2076
2089#ifdef CONFIG_DEBUG_KMEMLEAK 2077#ifdef CONFIG_DEBUG_KMEMLEAK
2090static void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr, 2078static void kmemleak_load_module(const struct module *mod,
2091 Elf_Shdr *sechdrs, char *secstrings) 2079 const struct load_info *info)
2092{ 2080{
2093 unsigned int i; 2081 unsigned int i;
2094 2082
2095 /* only scan the sections containing data */ 2083 /* only scan the sections containing data */
2096 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL); 2084 kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL);
2097 2085
2098 for (i = 1; i < hdr->e_shnum; i++) { 2086 for (i = 1; i < info->hdr->e_shnum; i++) {
2099 if (!(sechdrs[i].sh_flags & SHF_ALLOC)) 2087 const char *name = info->secstrings + info->sechdrs[i].sh_name;
2088 if (!(info->sechdrs[i].sh_flags & SHF_ALLOC))
2100 continue; 2089 continue;
2101 if (strncmp(secstrings + sechdrs[i].sh_name, ".data", 5) != 0 2090 if (!strstarts(name, ".data") && !strstarts(name, ".bss"))
2102 && strncmp(secstrings + sechdrs[i].sh_name, ".bss", 4) != 0)
2103 continue; 2091 continue;
2104 2092
2105 kmemleak_scan_area((void *)sechdrs[i].sh_addr, 2093 kmemleak_scan_area((void *)info->sechdrs[i].sh_addr,
2106 sechdrs[i].sh_size, GFP_KERNEL); 2094 info->sechdrs[i].sh_size, GFP_KERNEL);
2107 } 2095 }
2108} 2096}
2109#else 2097#else
2110static inline void kmemleak_load_module(struct module *mod, Elf_Ehdr *hdr, 2098static inline void kmemleak_load_module(const struct module *mod,
2111 Elf_Shdr *sechdrs, char *secstrings) 2099 const struct load_info *info)
2112{ 2100{
2113} 2101}
2114#endif 2102#endif
2115 2103
2116/* Allocate and load the module: note that size of section 0 is always 2104/* Sets info->hdr and info->len. */
2117 zero, and we rely on this for optional sections. */ 2105static int copy_and_check(struct load_info *info,
2118static noinline struct module *load_module(void __user *umod, 2106 const void __user *umod, unsigned long len,
2119 unsigned long len, 2107 const char __user *uargs)
2120 const char __user *uargs)
2121{ 2108{
2109 int err;
2122 Elf_Ehdr *hdr; 2110 Elf_Ehdr *hdr;
2123 Elf_Shdr *sechdrs;
2124 char *secstrings, *args, *modmagic, *strtab = NULL;
2125 char *staging;
2126 unsigned int i;
2127 unsigned int symindex = 0;
2128 unsigned int strindex = 0;
2129 unsigned int modindex, versindex, infoindex, pcpuindex;
2130 struct module *mod;
2131 long err = 0;
2132 void *ptr = NULL; /* Stops spurious gcc warning */
2133 unsigned long symoffs, stroffs, *strmap;
2134 void __percpu *percpu;
2135 struct _ddebug *debug = NULL;
2136 unsigned int num_debug = 0;
2137 2111
2138 mm_segment_t old_fs;
2139
2140 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2141 umod, len, uargs);
2142 if (len < sizeof(*hdr)) 2112 if (len < sizeof(*hdr))
2143 return ERR_PTR(-ENOEXEC); 2113 return -ENOEXEC;
2144 2114
2145 /* Suck in entire file: we'll want most of it. */ 2115 /* Suck in entire file: we'll want most of it. */
2146 /* vmalloc barfs on "unusual" numbers. Check here */ 2116 /* vmalloc barfs on "unusual" numbers. Check here */
2147 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL) 2117 if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
2148 return ERR_PTR(-ENOMEM); 2118 return -ENOMEM;
2149 2119
2150 if (copy_from_user(hdr, umod, len) != 0) { 2120 if (copy_from_user(hdr, umod, len) != 0) {
2151 err = -EFAULT; 2121 err = -EFAULT;
@@ -2153,135 +2123,225 @@ static noinline struct module *load_module(void __user *umod,
2153 } 2123 }
2154 2124
2155 /* Sanity checks against insmoding binaries or wrong arch, 2125 /* Sanity checks against insmoding binaries or wrong arch,
2156 weird elf version */ 2126 weird elf version */
2157 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0 2127 if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0
2158 || hdr->e_type != ET_REL 2128 || hdr->e_type != ET_REL
2159 || !elf_check_arch(hdr) 2129 || !elf_check_arch(hdr)
2160 || hdr->e_shentsize != sizeof(*sechdrs)) { 2130 || hdr->e_shentsize != sizeof(Elf_Shdr)) {
2161 err = -ENOEXEC; 2131 err = -ENOEXEC;
2162 goto free_hdr; 2132 goto free_hdr;
2163 } 2133 }
2164 2134
2165 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) 2135 if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr)) {
2166 goto truncated; 2136 err = -ENOEXEC;
2137 goto free_hdr;
2138 }
2167 2139
2168 /* Convenience variables */ 2140 info->hdr = hdr;
2169 sechdrs = (void *)hdr + hdr->e_shoff; 2141 info->len = len;
2170 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 2142 return 0;
2171 sechdrs[0].sh_addr = 0;
2172 2143
2173 for (i = 1; i < hdr->e_shnum; i++) { 2144free_hdr:
2174 if (sechdrs[i].sh_type != SHT_NOBITS 2145 vfree(hdr);
2175 && len < sechdrs[i].sh_offset + sechdrs[i].sh_size) 2146 return err;
2176 goto truncated; 2147}
2148
2149static void free_copy(struct load_info *info)
2150{
2151 vfree(info->hdr);
2152}
2153
2154static int rewrite_section_headers(struct load_info *info)
2155{
2156 unsigned int i;
2157
2158 /* This should always be true, but let's be sure. */
2159 info->sechdrs[0].sh_addr = 0;
2160
2161 for (i = 1; i < info->hdr->e_shnum; i++) {
2162 Elf_Shdr *shdr = &info->sechdrs[i];
2163 if (shdr->sh_type != SHT_NOBITS
2164 && info->len < shdr->sh_offset + shdr->sh_size) {
2165 printk(KERN_ERR "Module len %lu truncated\n",
2166 info->len);
2167 return -ENOEXEC;
2168 }
2177 2169
2178 /* Mark all sections sh_addr with their address in the 2170 /* Mark all sections sh_addr with their address in the
2179 temporary image. */ 2171 temporary image. */
2180 sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset; 2172 shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
2181 2173
2182 /* Internal symbols and strings. */
2183 if (sechdrs[i].sh_type == SHT_SYMTAB) {
2184 symindex = i;
2185 strindex = sechdrs[i].sh_link;
2186 strtab = (char *)hdr + sechdrs[strindex].sh_offset;
2187 }
2188#ifndef CONFIG_MODULE_UNLOAD 2174#ifndef CONFIG_MODULE_UNLOAD
2189 /* Don't load .exit sections */ 2175 /* Don't load .exit sections */
2190 if (strstarts(secstrings+sechdrs[i].sh_name, ".exit")) 2176 if (strstarts(info->secstrings+shdr->sh_name, ".exit"))
2191 sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC; 2177 shdr->sh_flags &= ~(unsigned long)SHF_ALLOC;
2192#endif 2178#endif
2193 } 2179 }
2194 2180
2195 modindex = find_sec(hdr, sechdrs, secstrings, 2181 /* Track but don't keep modinfo and version sections. */
2196 ".gnu.linkonce.this_module"); 2182 info->index.vers = find_sec(info, "__versions");
2197 if (!modindex) { 2183 info->index.info = find_sec(info, ".modinfo");
2184 info->sechdrs[info->index.info].sh_flags &= ~(unsigned long)SHF_ALLOC;
2185 info->sechdrs[info->index.vers].sh_flags &= ~(unsigned long)SHF_ALLOC;
2186 return 0;
2187}
2188
2189/*
2190 * Set up our basic convenience variables (pointers to section headers,
2191 * search for module section index etc), and do some basic section
2192 * verification.
2193 *
2194 * Return the temporary module pointer (we'll replace it with the final
2195 * one when we move the module sections around).
2196 */
2197static struct module *setup_load_info(struct load_info *info)
2198{
2199 unsigned int i;
2200 int err;
2201 struct module *mod;
2202
2203 /* Set up the convenience variables */
2204 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
2205 info->secstrings = (void *)info->hdr
2206 + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
2207
2208 err = rewrite_section_headers(info);
2209 if (err)
2210 return ERR_PTR(err);
2211
2212 /* Find internal symbols and strings. */
2213 for (i = 1; i < info->hdr->e_shnum; i++) {
2214 if (info->sechdrs[i].sh_type == SHT_SYMTAB) {
2215 info->index.sym = i;
2216 info->index.str = info->sechdrs[i].sh_link;
2217 info->strtab = (char *)info->hdr
2218 + info->sechdrs[info->index.str].sh_offset;
2219 break;
2220 }
2221 }
2222
2223 info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
2224 if (!info->index.mod) {
2198 printk(KERN_WARNING "No module found in object\n"); 2225 printk(KERN_WARNING "No module found in object\n");
2199 err = -ENOEXEC; 2226 return ERR_PTR(-ENOEXEC);
2200 goto free_hdr;
2201 } 2227 }
2202 /* This is temporary: point mod into copy of data. */ 2228 /* This is temporary: point mod into copy of data. */
2203 mod = (void *)sechdrs[modindex].sh_addr; 2229 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2204 2230
2205 if (symindex == 0) { 2231 if (info->index.sym == 0) {
2206 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n", 2232 printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
2207 mod->name); 2233 mod->name);
2208 err = -ENOEXEC; 2234 return ERR_PTR(-ENOEXEC);
2209 goto free_hdr;
2210 } 2235 }
2211 2236
2212 versindex = find_sec(hdr, sechdrs, secstrings, "__versions"); 2237 info->index.pcpu = find_pcpusec(info);
2213 infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
2214 pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
2215
2216 /* Don't keep modinfo and version sections. */
2217 sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2218 sechdrs[versindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2219 2238
2220 /* Check module struct version now, before we try to use module. */ 2239 /* Check module struct version now, before we try to use module. */
2221 if (!check_modstruct_version(sechdrs, versindex, mod)) { 2240 if (!check_modstruct_version(info->sechdrs, info->index.vers, mod))
2222 err = -ENOEXEC; 2241 return ERR_PTR(-ENOEXEC);
2223 goto free_hdr; 2242
2224 } 2243 return mod;
2244}
2245
2246static int check_modinfo(struct module *mod, struct load_info *info)
2247{
2248 const char *modmagic = get_modinfo(info, "vermagic");
2249 int err;
2225 2250
2226 modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
2227 /* This is allowed: modprobe --force will invalidate it. */ 2251 /* This is allowed: modprobe --force will invalidate it. */
2228 if (!modmagic) { 2252 if (!modmagic) {
2229 err = try_to_force_load(mod, "bad vermagic"); 2253 err = try_to_force_load(mod, "bad vermagic");
2230 if (err) 2254 if (err)
2231 goto free_hdr; 2255 return err;
2232 } else if (!same_magic(modmagic, vermagic, versindex)) { 2256 } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
2233 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n", 2257 printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
2234 mod->name, modmagic, vermagic); 2258 mod->name, modmagic, vermagic);
2235 err = -ENOEXEC; 2259 return -ENOEXEC;
2236 goto free_hdr;
2237 } 2260 }
2238 2261
2239 staging = get_modinfo(sechdrs, infoindex, "staging"); 2262 if (get_modinfo(info, "staging")) {
2240 if (staging) {
2241 add_taint_module(mod, TAINT_CRAP); 2263 add_taint_module(mod, TAINT_CRAP);
2242 printk(KERN_WARNING "%s: module is from the staging directory," 2264 printk(KERN_WARNING "%s: module is from the staging directory,"
2243 " the quality is unknown, you have been warned.\n", 2265 " the quality is unknown, you have been warned.\n",
2244 mod->name); 2266 mod->name);
2245 } 2267 }
2246 2268
2247 /* Now copy in args */ 2269 /* Set up license info based on the info section */
2248 args = strndup_user(uargs, ~0UL >> 1); 2270 set_license(mod, get_modinfo(info, "license"));
2249 if (IS_ERR(args)) {
2250 err = PTR_ERR(args);
2251 goto free_hdr;
2252 }
2253 2271
2254 strmap = kzalloc(BITS_TO_LONGS(sechdrs[strindex].sh_size) 2272 return 0;
2255 * sizeof(long), GFP_KERNEL); 2273}
2256 if (!strmap) {
2257 err = -ENOMEM;
2258 goto free_mod;
2259 }
2260 2274
2261 mod->state = MODULE_STATE_COMING; 2275static void find_module_sections(struct module *mod, struct load_info *info)
2276{
2277 mod->kp = section_objs(info, "__param",
2278 sizeof(*mod->kp), &mod->num_kp);
2279 mod->syms = section_objs(info, "__ksymtab",
2280 sizeof(*mod->syms), &mod->num_syms);
2281 mod->crcs = section_addr(info, "__kcrctab");
2282 mod->gpl_syms = section_objs(info, "__ksymtab_gpl",
2283 sizeof(*mod->gpl_syms),
2284 &mod->num_gpl_syms);
2285 mod->gpl_crcs = section_addr(info, "__kcrctab_gpl");
2286 mod->gpl_future_syms = section_objs(info,
2287 "__ksymtab_gpl_future",
2288 sizeof(*mod->gpl_future_syms),
2289 &mod->num_gpl_future_syms);
2290 mod->gpl_future_crcs = section_addr(info, "__kcrctab_gpl_future");
2262 2291
2263 /* Allow arches to frob section contents and sizes. */ 2292#ifdef CONFIG_UNUSED_SYMBOLS
2264 err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod); 2293 mod->unused_syms = section_objs(info, "__ksymtab_unused",
2265 if (err < 0) 2294 sizeof(*mod->unused_syms),
2266 goto free_mod; 2295 &mod->num_unused_syms);
2296 mod->unused_crcs = section_addr(info, "__kcrctab_unused");
2297 mod->unused_gpl_syms = section_objs(info, "__ksymtab_unused_gpl",
2298 sizeof(*mod->unused_gpl_syms),
2299 &mod->num_unused_gpl_syms);
2300 mod->unused_gpl_crcs = section_addr(info, "__kcrctab_unused_gpl");
2301#endif
2302#ifdef CONFIG_CONSTRUCTORS
2303 mod->ctors = section_objs(info, ".ctors",
2304 sizeof(*mod->ctors), &mod->num_ctors);
2305#endif
2267 2306
2268 if (pcpuindex) { 2307#ifdef CONFIG_TRACEPOINTS
2269 /* We have a special allocation for this section. */ 2308 mod->tracepoints = section_objs(info, "__tracepoints",
2270 err = percpu_modalloc(mod, sechdrs[pcpuindex].sh_size, 2309 sizeof(*mod->tracepoints),
2271 sechdrs[pcpuindex].sh_addralign); 2310 &mod->num_tracepoints);
2272 if (err) 2311#endif
2273 goto free_mod; 2312#ifdef CONFIG_EVENT_TRACING
2274 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2313 mod->trace_events = section_objs(info, "_ftrace_events",
2275 } 2314 sizeof(*mod->trace_events),
2276 /* Keep this around for failure path. */ 2315 &mod->num_trace_events);
2277 percpu = mod_percpu(mod); 2316 /*
2317 * This section contains pointers to allocated objects in the trace
2318 * code and not scanning it leads to false positives.
2319 */
2320 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2321 mod->num_trace_events, GFP_KERNEL);
2322#endif
2323#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2324 /* sechdrs[0].sh_size is always zero */
2325 mod->ftrace_callsites = section_objs(info, "__mcount_loc",
2326 sizeof(*mod->ftrace_callsites),
2327 &mod->num_ftrace_callsites);
2328#endif
2278 2329
2279 /* Determine total sizes, and put offsets in sh_entsize. For now 2330 mod->extable = section_objs(info, "__ex_table",
2280 this is done generically; there doesn't appear to be any 2331 sizeof(*mod->extable), &mod->num_exentries);
2281 special cases for the architectures. */ 2332
2282 layout_sections(mod, hdr, sechdrs, secstrings); 2333 if (section_addr(info, "__obsparm"))
2283 symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr, 2334 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
2284 secstrings, &stroffs, strmap); 2335 mod->name);
2336
2337 info->debug = section_objs(info, "__verbose",
2338 sizeof(*info->debug), &info->num_debug);
2339}
2340
2341static int move_module(struct module *mod, struct load_info *info)
2342{
2343 int i;
2344 void *ptr;
2285 2345
2286 /* Do the allocs. */ 2346 /* Do the allocs. */
2287 ptr = module_alloc_update_bounds(mod->core_size); 2347 ptr = module_alloc_update_bounds(mod->core_size);
@@ -2291,10 +2351,9 @@ static noinline struct module *load_module(void __user *umod,
2291 * leak. 2351 * leak.
2292 */ 2352 */
2293 kmemleak_not_leak(ptr); 2353 kmemleak_not_leak(ptr);
2294 if (!ptr) { 2354 if (!ptr)
2295 err = -ENOMEM; 2355 return -ENOMEM;
2296 goto free_percpu; 2356
2297 }
2298 memset(ptr, 0, mod->core_size); 2357 memset(ptr, 0, mod->core_size);
2299 mod->module_core = ptr; 2358 mod->module_core = ptr;
2300 2359
@@ -2307,50 +2366,40 @@ static noinline struct module *load_module(void __user *umod,
2307 */ 2366 */
2308 kmemleak_ignore(ptr); 2367 kmemleak_ignore(ptr);
2309 if (!ptr && mod->init_size) { 2368 if (!ptr && mod->init_size) {
2310 err = -ENOMEM; 2369 module_free(mod, mod->module_core);
2311 goto free_core; 2370 return -ENOMEM;
2312 } 2371 }
2313 memset(ptr, 0, mod->init_size); 2372 memset(ptr, 0, mod->init_size);
2314 mod->module_init = ptr; 2373 mod->module_init = ptr;
2315 2374
2316 /* Transfer each section which specifies SHF_ALLOC */ 2375 /* Transfer each section which specifies SHF_ALLOC */
2317 DEBUGP("final section addresses:\n"); 2376 DEBUGP("final section addresses:\n");
2318 for (i = 0; i < hdr->e_shnum; i++) { 2377 for (i = 0; i < info->hdr->e_shnum; i++) {
2319 void *dest; 2378 void *dest;
2379 Elf_Shdr *shdr = &info->sechdrs[i];
2320 2380
2321 if (!(sechdrs[i].sh_flags & SHF_ALLOC)) 2381 if (!(shdr->sh_flags & SHF_ALLOC))
2322 continue; 2382 continue;
2323 2383
2324 if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK) 2384 if (shdr->sh_entsize & INIT_OFFSET_MASK)
2325 dest = mod->module_init 2385 dest = mod->module_init
2326 + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK); 2386 + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
2327 else 2387 else
2328 dest = mod->module_core + sechdrs[i].sh_entsize; 2388 dest = mod->module_core + shdr->sh_entsize;
2329 2389
2330 if (sechdrs[i].sh_type != SHT_NOBITS) 2390 if (shdr->sh_type != SHT_NOBITS)
2331 memcpy(dest, (void *)sechdrs[i].sh_addr, 2391 memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
2332 sechdrs[i].sh_size);
2333 /* Update sh_addr to point to copy in image. */ 2392 /* Update sh_addr to point to copy in image. */
2334 sechdrs[i].sh_addr = (unsigned long)dest; 2393 shdr->sh_addr = (unsigned long)dest;
2335 DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name); 2394 DEBUGP("\t0x%lx %s\n",
2336 } 2395 shdr->sh_addr, info->secstrings + shdr->sh_name);
2337 /* Module has been moved. */
2338 mod = (void *)sechdrs[modindex].sh_addr;
2339 kmemleak_load_module(mod, hdr, sechdrs, secstrings);
2340
2341#if defined(CONFIG_MODULE_UNLOAD)
2342 mod->refptr = alloc_percpu(struct module_ref);
2343 if (!mod->refptr) {
2344 err = -ENOMEM;
2345 goto free_init;
2346 } 2396 }
2347#endif
2348 /* Now we've moved module, initialize linked lists, etc. */
2349 module_unload_init(mod);
2350 2397
2351 /* Set up license info based on the info section */ 2398 return 0;
2352 set_license(mod, get_modinfo(sechdrs, infoindex, "license")); 2399}
2353 2400
2401static int check_module_license_and_versions(struct module *mod)
2402{
2354 /* 2403 /*
2355 * ndiswrapper is under GPL by itself, but loads proprietary modules. 2404 * ndiswrapper is under GPL by itself, but loads proprietary modules.
2356 * Don't use add_taint_module(), as it would prevent ndiswrapper from 2405 * Don't use add_taint_module(), as it would prevent ndiswrapper from
@@ -2363,77 +2412,6 @@ static noinline struct module *load_module(void __user *umod,
2363 if (strcmp(mod->name, "driverloader") == 0) 2412 if (strcmp(mod->name, "driverloader") == 0)
2364 add_taint_module(mod, TAINT_PROPRIETARY_MODULE); 2413 add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
2365 2414
2366 /* Set up MODINFO_ATTR fields */
2367 setup_modinfo(mod, sechdrs, infoindex);
2368
2369 /* Fix up syms, so that st_value is a pointer to location. */
2370 err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
2371 mod);
2372 if (err < 0)
2373 goto cleanup;
2374
2375 /* Now we've got everything in the final locations, we can
2376 * find optional sections. */
2377 mod->kp = section_objs(hdr, sechdrs, secstrings, "__param",
2378 sizeof(*mod->kp), &mod->num_kp);
2379 mod->syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab",
2380 sizeof(*mod->syms), &mod->num_syms);
2381 mod->crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab");
2382 mod->gpl_syms = section_objs(hdr, sechdrs, secstrings, "__ksymtab_gpl",
2383 sizeof(*mod->gpl_syms),
2384 &mod->num_gpl_syms);
2385 mod->gpl_crcs = section_addr(hdr, sechdrs, secstrings, "__kcrctab_gpl");
2386 mod->gpl_future_syms = section_objs(hdr, sechdrs, secstrings,
2387 "__ksymtab_gpl_future",
2388 sizeof(*mod->gpl_future_syms),
2389 &mod->num_gpl_future_syms);
2390 mod->gpl_future_crcs = section_addr(hdr, sechdrs, secstrings,
2391 "__kcrctab_gpl_future");
2392
2393#ifdef CONFIG_UNUSED_SYMBOLS
2394 mod->unused_syms = section_objs(hdr, sechdrs, secstrings,
2395 "__ksymtab_unused",
2396 sizeof(*mod->unused_syms),
2397 &mod->num_unused_syms);
2398 mod->unused_crcs = section_addr(hdr, sechdrs, secstrings,
2399 "__kcrctab_unused");
2400 mod->unused_gpl_syms = section_objs(hdr, sechdrs, secstrings,
2401 "__ksymtab_unused_gpl",
2402 sizeof(*mod->unused_gpl_syms),
2403 &mod->num_unused_gpl_syms);
2404 mod->unused_gpl_crcs = section_addr(hdr, sechdrs, secstrings,
2405 "__kcrctab_unused_gpl");
2406#endif
2407#ifdef CONFIG_CONSTRUCTORS
2408 mod->ctors = section_objs(hdr, sechdrs, secstrings, ".ctors",
2409 sizeof(*mod->ctors), &mod->num_ctors);
2410#endif
2411
2412#ifdef CONFIG_TRACEPOINTS
2413 mod->tracepoints = section_objs(hdr, sechdrs, secstrings,
2414 "__tracepoints",
2415 sizeof(*mod->tracepoints),
2416 &mod->num_tracepoints);
2417#endif
2418#ifdef CONFIG_EVENT_TRACING
2419 mod->trace_events = section_objs(hdr, sechdrs, secstrings,
2420 "_ftrace_events",
2421 sizeof(*mod->trace_events),
2422 &mod->num_trace_events);
2423 /*
2424 * This section contains pointers to allocated objects in the trace
2425 * code and not scanning it leads to false positives.
2426 */
2427 kmemleak_scan_area(mod->trace_events, sizeof(*mod->trace_events) *
2428 mod->num_trace_events, GFP_KERNEL);
2429#endif
2430#ifdef CONFIG_FTRACE_MCOUNT_RECORD
2431 /* sechdrs[0].sh_size is always zero */
2432 mod->ftrace_callsites = section_objs(hdr, sechdrs, secstrings,
2433 "__mcount_loc",
2434 sizeof(*mod->ftrace_callsites),
2435 &mod->num_ftrace_callsites);
2436#endif
2437#ifdef CONFIG_MODVERSIONS 2415#ifdef CONFIG_MODVERSIONS
2438 if ((mod->num_syms && !mod->crcs) 2416 if ((mod->num_syms && !mod->crcs)
2439 || (mod->num_gpl_syms && !mod->gpl_crcs) 2417 || (mod->num_gpl_syms && !mod->gpl_crcs)
@@ -2443,56 +2421,16 @@ static noinline struct module *load_module(void __user *umod,
2443 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs) 2421 || (mod->num_unused_gpl_syms && !mod->unused_gpl_crcs)
2444#endif 2422#endif
2445 ) { 2423 ) {
2446 err = try_to_force_load(mod, 2424 return try_to_force_load(mod,
2447 "no versions for exported symbols"); 2425 "no versions for exported symbols");
2448 if (err)
2449 goto cleanup;
2450 } 2426 }
2451#endif 2427#endif
2428 return 0;
2429}
2452 2430
2453 /* Now do relocations. */ 2431static void flush_module_icache(const struct module *mod)
2454 for (i = 1; i < hdr->e_shnum; i++) { 2432{
2455 const char *strtab = (char *)sechdrs[strindex].sh_addr; 2433 mm_segment_t old_fs;
2456 unsigned int info = sechdrs[i].sh_info;
2457
2458 /* Not a valid relocation section? */
2459 if (info >= hdr->e_shnum)
2460 continue;
2461
2462 /* Don't bother with non-allocated sections */
2463 if (!(sechdrs[info].sh_flags & SHF_ALLOC))
2464 continue;
2465
2466 if (sechdrs[i].sh_type == SHT_REL)
2467 err = apply_relocate(sechdrs, strtab, symindex, i,mod);
2468 else if (sechdrs[i].sh_type == SHT_RELA)
2469 err = apply_relocate_add(sechdrs, strtab, symindex, i,
2470 mod);
2471 if (err < 0)
2472 goto cleanup;
2473 }
2474
2475 /* Set up and sort exception table */
2476 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2477 sizeof(*mod->extable), &mod->num_exentries);
2478 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2479
2480 /* Finally, copy percpu area over. */
2481 percpu_modcopy(mod, (void *)sechdrs[pcpuindex].sh_addr,
2482 sechdrs[pcpuindex].sh_size);
2483
2484 add_kallsyms(mod, sechdrs, hdr->e_shnum, symindex, strindex,
2485 symoffs, stroffs, secstrings, strmap);
2486 kfree(strmap);
2487 strmap = NULL;
2488
2489 if (!mod->taints)
2490 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2491 sizeof(*debug), &num_debug);
2492
2493 err = module_finalize(hdr, sechdrs, mod);
2494 if (err < 0)
2495 goto cleanup;
2496 2434
2497 /* flush the icache in correct context */ 2435 /* flush the icache in correct context */
2498 old_fs = get_fs(); 2436 old_fs = get_fs();
@@ -2511,11 +2449,160 @@ static noinline struct module *load_module(void __user *umod,
2511 (unsigned long)mod->module_core + mod->core_size); 2449 (unsigned long)mod->module_core + mod->core_size);
2512 2450
2513 set_fs(old_fs); 2451 set_fs(old_fs);
2452}
2514 2453
2515 mod->args = args; 2454static struct module *layout_and_allocate(struct load_info *info)
2516 if (section_addr(hdr, sechdrs, secstrings, "__obsparm")) 2455{
2517 printk(KERN_WARNING "%s: Ignoring obsolete parameters\n", 2456 /* Module within temporary copy. */
2518 mod->name); 2457 struct module *mod;
2458 Elf_Shdr *pcpusec;
2459 int err;
2460
2461 mod = setup_load_info(info);
2462 if (IS_ERR(mod))
2463 return mod;
2464
2465 err = check_modinfo(mod, info);
2466 if (err)
2467 return ERR_PTR(err);
2468
2469 /* Allow arches to frob section contents and sizes. */
2470 err = module_frob_arch_sections(info->hdr, info->sechdrs,
2471 info->secstrings, mod);
2472 if (err < 0)
2473 goto out;
2474
2475 pcpusec = &info->sechdrs[info->index.pcpu];
2476 if (pcpusec->sh_size) {
2477 /* We have a special allocation for this section. */
2478 err = percpu_modalloc(mod,
2479 pcpusec->sh_size, pcpusec->sh_addralign);
2480 if (err)
2481 goto out;
2482 pcpusec->sh_flags &= ~(unsigned long)SHF_ALLOC;
2483 }
2484
2485 /* Determine total sizes, and put offsets in sh_entsize. For now
2486 this is done generically; there doesn't appear to be any
2487 special cases for the architectures. */
2488 layout_sections(mod, info);
2489
2490 info->strmap = kzalloc(BITS_TO_LONGS(info->sechdrs[info->index.str].sh_size)
2491 * sizeof(long), GFP_KERNEL);
2492 if (!info->strmap) {
2493 err = -ENOMEM;
2494 goto free_percpu;
2495 }
2496 layout_symtab(mod, info);
2497
2498 /* Allocate and move to the final place */
2499 err = move_module(mod, info);
2500 if (err)
2501 goto free_strmap;
2502
2503 /* Module has been copied to its final place now: return it. */
2504 mod = (void *)info->sechdrs[info->index.mod].sh_addr;
2505 kmemleak_load_module(mod, info);
2506 return mod;
2507
2508free_strmap:
2509 kfree(info->strmap);
2510free_percpu:
2511 percpu_modfree(mod);
2512out:
2513 return ERR_PTR(err);
2514}
2515
2516/* mod is no longer valid after this! */
2517static void module_deallocate(struct module *mod, struct load_info *info)
2518{
2519 kfree(info->strmap);
2520 percpu_modfree(mod);
2521 module_free(mod, mod->module_init);
2522 module_free(mod, mod->module_core);
2523}
2524
2525static int post_relocation(struct module *mod, const struct load_info *info)
2526{
2527 /* Sort exception table now relocations are done. */
2528 sort_extable(mod->extable, mod->extable + mod->num_exentries);
2529
2530 /* Copy relocated percpu area over. */
2531 percpu_modcopy(mod, (void *)info->sechdrs[info->index.pcpu].sh_addr,
2532 info->sechdrs[info->index.pcpu].sh_size);
2533
2534 /* Setup kallsyms-specific fields. */
2535 add_kallsyms(mod, info);
2536
2537 /* Arch-specific module finalizing. */
2538 return module_finalize(info->hdr, info->sechdrs, mod);
2539}
2540
2541/* Allocate and load the module: note that size of section 0 is always
2542 zero, and we rely on this for optional sections. */
2543static struct module *load_module(void __user *umod,
2544 unsigned long len,
2545 const char __user *uargs)
2546{
2547 struct load_info info = { NULL, };
2548 struct module *mod;
2549 long err;
2550
2551 DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
2552 umod, len, uargs);
2553
2554 /* Copy in the blobs from userspace, check they are vaguely sane. */
2555 err = copy_and_check(&info, umod, len, uargs);
2556 if (err)
2557 return ERR_PTR(err);
2558
2559 /* Figure out module layout, and allocate all the memory. */
2560 mod = layout_and_allocate(&info);
2561 if (IS_ERR(mod)) {
2562 err = PTR_ERR(mod);
2563 goto free_copy;
2564 }
2565
2566 /* Now module is in final location, initialize linked lists, etc. */
2567 err = module_unload_init(mod);
2568 if (err)
2569 goto free_module;
2570
2571 /* Now we've got everything in the final locations, we can
2572 * find optional sections. */
2573 find_module_sections(mod, &info);
2574
2575 err = check_module_license_and_versions(mod);
2576 if (err)
2577 goto free_unload;
2578
2579 /* Set up MODINFO_ATTR fields */
2580 setup_modinfo(mod, &info);
2581
2582 /* Fix up syms, so that st_value is a pointer to location. */
2583 err = simplify_symbols(mod, &info);
2584 if (err < 0)
2585 goto free_modinfo;
2586
2587 err = apply_relocations(mod, &info);
2588 if (err < 0)
2589 goto free_modinfo;
2590
2591 err = post_relocation(mod, &info);
2592 if (err < 0)
2593 goto free_modinfo;
2594
2595 flush_module_icache(mod);
2596
2597 /* Now copy in args */
2598 mod->args = strndup_user(uargs, ~0UL >> 1);
2599 if (IS_ERR(mod->args)) {
2600 err = PTR_ERR(mod->args);
2601 goto free_arch_cleanup;
2602 }
2603
2604 /* Mark state as coming so strong_try_module_get() ignores us. */
2605 mod->state = MODULE_STATE_COMING;
2519 2606
2520 /* Now sew it into the lists so we can get lockdep and oops 2607 /* Now sew it into the lists so we can get lockdep and oops
2521 * info during argument parsing. Noone should access us, since 2608 * info during argument parsing. Noone should access us, since
@@ -2530,70 +2617,61 @@ static noinline struct module *load_module(void __user *umod,
2530 goto unlock; 2617 goto unlock;
2531 } 2618 }
2532 2619
2533 if (debug) 2620 /* This has to be done once we're sure module name is unique. */
2534 dynamic_debug_setup(debug, num_debug); 2621 if (!mod->taints)
2622 dynamic_debug_setup(info.debug, info.num_debug);
2535 2623
2536 /* Find duplicate symbols */ 2624 /* Find duplicate symbols */
2537 err = verify_export_symbols(mod); 2625 err = verify_export_symbols(mod);
2538 if (err < 0) 2626 if (err < 0)
2539 goto ddebug; 2627 goto ddebug;
2540 2628
2629 module_bug_finalize(info.hdr, info.sechdrs, mod);
2541 list_add_rcu(&mod->list, &modules); 2630 list_add_rcu(&mod->list, &modules);
2542 mutex_unlock(&module_mutex); 2631 mutex_unlock(&module_mutex);
2543 2632
2633 /* Module is ready to execute: parsing args may do that. */
2544 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL); 2634 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2545 if (err < 0) 2635 if (err < 0)
2546 goto unlink; 2636 goto unlink;
2547 2637
2548 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp); 2638 /* Link in to syfs. */
2639 err = mod_sysfs_setup(mod, &info, mod->kp, mod->num_kp);
2549 if (err < 0) 2640 if (err < 0)
2550 goto unlink; 2641 goto unlink;
2551 2642
2552 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2643 /* Get rid of temporary copy and strmap. */
2553 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2644 kfree(info.strmap);
2554 2645 free_copy(&info);
2555 /* Get rid of temporary copy */
2556 vfree(hdr);
2557
2558 trace_module_load(mod);
2559 2646
2560 /* Done! */ 2647 /* Done! */
2648 trace_module_load(mod);
2561 return mod; 2649 return mod;
2562 2650
2563 unlink: 2651 unlink:
2564 mutex_lock(&module_mutex); 2652 mutex_lock(&module_mutex);
2565 /* Unlink carefully: kallsyms could be walking list. */ 2653 /* Unlink carefully: kallsyms could be walking list. */
2566 list_del_rcu(&mod->list); 2654 list_del_rcu(&mod->list);
2655 module_bug_cleanup(mod);
2656
2567 ddebug: 2657 ddebug:
2568 dynamic_debug_remove(debug); 2658 if (!mod->taints)
2659 dynamic_debug_remove(info.debug);
2569 unlock: 2660 unlock:
2570 mutex_unlock(&module_mutex); 2661 mutex_unlock(&module_mutex);
2571 synchronize_sched(); 2662 synchronize_sched();
2663 kfree(mod->args);
2664 free_arch_cleanup:
2572 module_arch_cleanup(mod); 2665 module_arch_cleanup(mod);
2573 cleanup: 2666 free_modinfo:
2574 free_modinfo(mod); 2667 free_modinfo(mod);
2668 free_unload:
2575 module_unload_free(mod); 2669 module_unload_free(mod);
2576#if defined(CONFIG_MODULE_UNLOAD) 2670 free_module:
2577 free_percpu(mod->refptr); 2671 module_deallocate(mod, &info);
2578 free_init: 2672 free_copy:
2579#endif 2673 free_copy(&info);
2580 module_free(mod, mod->module_init);
2581 free_core:
2582 module_free(mod, mod->module_core);
2583 /* mod will be freed with core. Don't access it beyond this line! */
2584 free_percpu:
2585 free_percpu(percpu);
2586 free_mod:
2587 kfree(args);
2588 kfree(strmap);
2589 free_hdr:
2590 vfree(hdr);
2591 return ERR_PTR(err); 2674 return ERR_PTR(err);
2592
2593 truncated:
2594 printk(KERN_ERR "Module len %lu truncated\n", len);
2595 err = -ENOEXEC;
2596 goto free_hdr;
2597} 2675}
2598 2676
2599/* Call module constructors. */ 2677/* Call module constructors. */