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