aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c350
1 files changed, 227 insertions, 123 deletions
diff --git a/kernel/module.c b/kernel/module.c
index 3c4fc4bb4b82..5d2d28197c82 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -72,7 +72,11 @@
72/* If this is set, the section belongs in the init part of the module */ 72/* If this is set, the section belongs in the init part of the module */
73#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) 73#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
74 74
75/* List of modules, protected by module_mutex or preempt_disable 75/*
76 * Mutex protects:
77 * 1) List of modules (also safely readable with preempt_disable),
78 * 2) module_use links,
79 * 3) module_addr_min/module_addr_max.
76 * (delete uses stop_machine/add uses RCU list operations). */ 80 * (delete uses stop_machine/add uses RCU list operations). */
77DEFINE_MUTEX(module_mutex); 81DEFINE_MUTEX(module_mutex);
78EXPORT_SYMBOL_GPL(module_mutex); 82EXPORT_SYMBOL_GPL(module_mutex);
@@ -90,7 +94,8 @@ static DECLARE_WAIT_QUEUE_HEAD(module_wq);
90 94
91static BLOCKING_NOTIFIER_HEAD(module_notify_list); 95static BLOCKING_NOTIFIER_HEAD(module_notify_list);
92 96
93/* Bounds of module allocation, for speeding __module_address */ 97/* Bounds of module allocation, for speeding __module_address.
98 * Protected by module_mutex. */
94static unsigned long module_addr_min = -1UL, module_addr_max = 0; 99static unsigned long module_addr_min = -1UL, module_addr_max = 0;
95 100
96int register_module_notifier(struct notifier_block * nb) 101int register_module_notifier(struct notifier_block * nb)
@@ -180,8 +185,6 @@ extern const struct kernel_symbol __start___ksymtab_gpl[];
180extern const struct kernel_symbol __stop___ksymtab_gpl[]; 185extern const struct kernel_symbol __stop___ksymtab_gpl[];
181extern const struct kernel_symbol __start___ksymtab_gpl_future[]; 186extern const struct kernel_symbol __start___ksymtab_gpl_future[];
182extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; 187extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
183extern const struct kernel_symbol __start___ksymtab_gpl_future[];
184extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
185extern const unsigned long __start___kcrctab[]; 188extern const unsigned long __start___kcrctab[];
186extern const unsigned long __start___kcrctab_gpl[]; 189extern const unsigned long __start___kcrctab_gpl[];
187extern const unsigned long __start___kcrctab_gpl_future[]; 190extern const unsigned long __start___kcrctab_gpl_future[];
@@ -331,7 +334,7 @@ static bool find_symbol_in_section(const struct symsearch *syms,
331} 334}
332 335
333/* Find a symbol and return it, along with, (optional) crc and 336/* Find a symbol and return it, along with, (optional) crc and
334 * (optional) module which owns it */ 337 * (optional) module which owns it. Needs preempt disabled or module_mutex. */
335const struct kernel_symbol *find_symbol(const char *name, 338const struct kernel_symbol *find_symbol(const char *name,
336 struct module **owner, 339 struct module **owner,
337 const unsigned long **crc, 340 const unsigned long **crc,
@@ -405,7 +408,7 @@ static unsigned int find_pcpusec(Elf_Ehdr *hdr,
405 Elf_Shdr *sechdrs, 408 Elf_Shdr *sechdrs,
406 const char *secstrings) 409 const char *secstrings)
407{ 410{
408 return find_sec(hdr, sechdrs, secstrings, ".data.percpu"); 411 return find_sec(hdr, sechdrs, secstrings, ".data..percpu");
409} 412}
410 413
411static void percpu_modcopy(struct module *mod, 414static void percpu_modcopy(struct module *mod,
@@ -525,7 +528,8 @@ static void module_unload_init(struct module *mod)
525{ 528{
526 int cpu; 529 int cpu;
527 530
528 INIT_LIST_HEAD(&mod->modules_which_use_me); 531 INIT_LIST_HEAD(&mod->source_list);
532 INIT_LIST_HEAD(&mod->target_list);
529 for_each_possible_cpu(cpu) { 533 for_each_possible_cpu(cpu) {
530 per_cpu_ptr(mod->refptr, cpu)->incs = 0; 534 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
531 per_cpu_ptr(mod->refptr, cpu)->decs = 0; 535 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
@@ -537,20 +541,13 @@ static void module_unload_init(struct module *mod)
537 mod->waiter = current; 541 mod->waiter = current;
538} 542}
539 543
540/* modules using other modules */
541struct module_use
542{
543 struct list_head list;
544 struct module *module_which_uses;
545};
546
547/* Does a already use b? */ 544/* Does a already use b? */
548static int already_uses(struct module *a, struct module *b) 545static int already_uses(struct module *a, struct module *b)
549{ 546{
550 struct module_use *use; 547 struct module_use *use;
551 548
552 list_for_each_entry(use, &b->modules_which_use_me, list) { 549 list_for_each_entry(use, &b->source_list, source_list) {
553 if (use->module_which_uses == a) { 550 if (use->source == a) {
554 DEBUGP("%s uses %s!\n", a->name, b->name); 551 DEBUGP("%s uses %s!\n", a->name, b->name);
555 return 1; 552 return 1;
556 } 553 }
@@ -559,62 +556,68 @@ static int already_uses(struct module *a, struct module *b)
559 return 0; 556 return 0;
560} 557}
561 558
562/* Module a uses b */ 559/*
563int use_module(struct module *a, struct module *b) 560 * Module a uses b
561 * - we add 'a' as a "source", 'b' as a "target" of module use
562 * - the module_use is added to the list of 'b' sources (so
563 * 'b' can walk the list to see who sourced them), and of 'a'
564 * targets (so 'a' can see what modules it targets).
565 */
566static int add_module_usage(struct module *a, struct module *b)
564{ 567{
565 struct module_use *use; 568 struct module_use *use;
566 int no_warn, err;
567 569
568 if (b == NULL || already_uses(a, b)) return 1; 570 DEBUGP("Allocating new usage for %s.\n", a->name);
571 use = kmalloc(sizeof(*use), GFP_ATOMIC);
572 if (!use) {
573 printk(KERN_WARNING "%s: out of memory loading\n", a->name);
574 return -ENOMEM;
575 }
576
577 use->source = a;
578 use->target = b;
579 list_add(&use->source_list, &b->source_list);
580 list_add(&use->target_list, &a->target_list);
581 return 0;
582}
583
584/* Module a uses b: caller needs module_mutex() */
585int ref_module(struct module *a, struct module *b)
586{
587 int err;
569 588
570 /* If we're interrupted or time out, we fail. */ 589 if (b == NULL || already_uses(a, b))
571 if (wait_event_interruptible_timeout(
572 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
573 30 * HZ) <= 0) {
574 printk("%s: gave up waiting for init of module %s.\n",
575 a->name, b->name);
576 return 0; 590 return 0;
577 }
578 591
579 /* If strong_try_module_get() returned a different error, we fail. */ 592 /* If module isn't available, we fail. */
593 err = strong_try_module_get(b);
580 if (err) 594 if (err)
581 return 0; 595 return err;
582 596
583 DEBUGP("Allocating new usage for %s.\n", a->name); 597 err = add_module_usage(a, b);
584 use = kmalloc(sizeof(*use), GFP_ATOMIC); 598 if (err) {
585 if (!use) {
586 printk("%s: out of memory loading\n", a->name);
587 module_put(b); 599 module_put(b);
588 return 0; 600 return err;
589 } 601 }
590 602 return 0;
591 use->module_which_uses = a;
592 list_add(&use->list, &b->modules_which_use_me);
593 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
594 return 1;
595} 603}
596EXPORT_SYMBOL_GPL(use_module); 604EXPORT_SYMBOL_GPL(ref_module);
597 605
598/* Clear the unload stuff of the module. */ 606/* Clear the unload stuff of the module. */
599static void module_unload_free(struct module *mod) 607static void module_unload_free(struct module *mod)
600{ 608{
601 struct module *i; 609 struct module_use *use, *tmp;
602
603 list_for_each_entry(i, &modules, list) {
604 struct module_use *use;
605 610
606 list_for_each_entry(use, &i->modules_which_use_me, list) { 611 mutex_lock(&module_mutex);
607 if (use->module_which_uses == mod) { 612 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
608 DEBUGP("%s unusing %s\n", mod->name, i->name); 613 struct module *i = use->target;
609 module_put(i); 614 DEBUGP("%s unusing %s\n", mod->name, i->name);
610 list_del(&use->list); 615 module_put(i);
611 kfree(use); 616 list_del(&use->source_list);
612 sysfs_remove_link(i->holders_dir, mod->name); 617 list_del(&use->target_list);
613 /* There can be at most one match. */ 618 kfree(use);
614 break;
615 }
616 }
617 } 619 }
620 mutex_unlock(&module_mutex);
618} 621}
619 622
620#ifdef CONFIG_MODULE_FORCE_UNLOAD 623#ifdef CONFIG_MODULE_FORCE_UNLOAD
@@ -737,7 +740,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
737 goto out; 740 goto out;
738 } 741 }
739 742
740 if (!list_empty(&mod->modules_which_use_me)) { 743 if (!list_empty(&mod->source_list)) {
741 /* Other modules depend on us: get rid of them first. */ 744 /* Other modules depend on us: get rid of them first. */
742 ret = -EWOULDBLOCK; 745 ret = -EWOULDBLOCK;
743 goto out; 746 goto out;
@@ -781,13 +784,14 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
781 blocking_notifier_call_chain(&module_notify_list, 784 blocking_notifier_call_chain(&module_notify_list,
782 MODULE_STATE_GOING, mod); 785 MODULE_STATE_GOING, mod);
783 async_synchronize_full(); 786 async_synchronize_full();
784 mutex_lock(&module_mutex); 787
785 /* Store the name of the last unloaded module for diagnostic purposes */ 788 /* Store the name of the last unloaded module for diagnostic purposes */
786 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); 789 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
787 ddebug_remove_module(mod->name); 790 ddebug_remove_module(mod->name);
788 free_module(mod);
789 791
790 out: 792 free_module(mod);
793 return 0;
794out:
791 mutex_unlock(&module_mutex); 795 mutex_unlock(&module_mutex);
792 return ret; 796 return ret;
793} 797}
@@ -801,9 +805,9 @@ static inline void print_unload_info(struct seq_file *m, struct module *mod)
801 805
802 /* Always include a trailing , so userspace can differentiate 806 /* Always include a trailing , so userspace can differentiate
803 between this and the old multi-field proc format. */ 807 between this and the old multi-field proc format. */
804 list_for_each_entry(use, &mod->modules_which_use_me, list) { 808 list_for_each_entry(use, &mod->source_list, source_list) {
805 printed_something = 1; 809 printed_something = 1;
806 seq_printf(m, "%s,", use->module_which_uses->name); 810 seq_printf(m, "%s,", use->source->name);
807 } 811 }
808 812
809 if (mod->init != NULL && mod->exit == NULL) { 813 if (mod->init != NULL && mod->exit == NULL) {
@@ -882,11 +886,11 @@ static inline void module_unload_free(struct module *mod)
882{ 886{
883} 887}
884 888
885int use_module(struct module *a, struct module *b) 889int ref_module(struct module *a, struct module *b)
886{ 890{
887 return strong_try_module_get(b) == 0; 891 return strong_try_module_get(b);
888} 892}
889EXPORT_SYMBOL_GPL(use_module); 893EXPORT_SYMBOL_GPL(ref_module);
890 894
891static inline void module_unload_init(struct module *mod) 895static inline void module_unload_init(struct module *mod)
892{ 896{
@@ -1003,6 +1007,8 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1003{ 1007{
1004 const unsigned long *crc; 1008 const unsigned long *crc;
1005 1009
1010 /* Since this should be found in kernel (which can't be removed),
1011 * no locking is necessary. */
1006 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL, 1012 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1007 &crc, true, false)) 1013 &crc, true, false))
1008 BUG(); 1014 BUG();
@@ -1045,29 +1051,62 @@ static inline int same_magic(const char *amagic, const char *bmagic,
1045} 1051}
1046#endif /* CONFIG_MODVERSIONS */ 1052#endif /* CONFIG_MODVERSIONS */
1047 1053
1048/* Resolve a symbol for this module. I.e. if we find one, record usage. 1054/* Resolve a symbol for this module. I.e. if we find one, record usage. */
1049 Must be holding module_mutex. */
1050static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, 1055static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1051 unsigned int versindex, 1056 unsigned int versindex,
1052 const char *name, 1057 const char *name,
1053 struct module *mod) 1058 struct module *mod,
1059 char ownername[])
1054{ 1060{
1055 struct module *owner; 1061 struct module *owner;
1056 const struct kernel_symbol *sym; 1062 const struct kernel_symbol *sym;
1057 const unsigned long *crc; 1063 const unsigned long *crc;
1064 int err;
1058 1065
1066 mutex_lock(&module_mutex);
1059 sym = find_symbol(name, &owner, &crc, 1067 sym = find_symbol(name, &owner, &crc,
1060 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1068 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1061 /* use_module can fail due to OOM, 1069 if (!sym)
1062 or module initialization or unloading */ 1070 goto unlock;
1063 if (sym) { 1071
1064 if (!check_version(sechdrs, versindex, name, mod, crc, owner) 1072 if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
1065 || !use_module(mod, owner)) 1073 sym = ERR_PTR(-EINVAL);
1066 sym = NULL; 1074 goto getname;
1075 }
1076
1077 err = ref_module(mod, owner);
1078 if (err) {
1079 sym = ERR_PTR(err);
1080 goto getname;
1067 } 1081 }
1082
1083getname:
1084 /* We must make copy under the lock if we failed to get ref. */
1085 strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
1086unlock:
1087 mutex_unlock(&module_mutex);
1068 return sym; 1088 return sym;
1069} 1089}
1070 1090
1091static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs,
1092 unsigned int versindex,
1093 const char *name,
1094 struct module *mod)
1095{
1096 const struct kernel_symbol *ksym;
1097 char ownername[MODULE_NAME_LEN];
1098
1099 if (wait_event_interruptible_timeout(module_wq,
1100 !IS_ERR(ksym = resolve_symbol(sechdrs, versindex, name,
1101 mod, ownername)) ||
1102 PTR_ERR(ksym) != -EBUSY,
1103 30 * HZ) <= 0) {
1104 printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
1105 mod->name, ownername);
1106 }
1107 return ksym;
1108}
1109
1071/* 1110/*
1072 * /sys/module/foo/sections stuff 1111 * /sys/module/foo/sections stuff
1073 * J. Corbet <corbet@lwn.net> 1112 * J. Corbet <corbet@lwn.net>
@@ -1297,7 +1336,34 @@ static inline void remove_notes_attrs(struct module *mod)
1297#endif 1336#endif
1298 1337
1299#ifdef CONFIG_SYSFS 1338#ifdef CONFIG_SYSFS
1300int module_add_modinfo_attrs(struct module *mod) 1339static void add_usage_links(struct module *mod)
1340{
1341#ifdef CONFIG_MODULE_UNLOAD
1342 struct module_use *use;
1343 int nowarn;
1344
1345 mutex_lock(&module_mutex);
1346 list_for_each_entry(use, &mod->target_list, target_list) {
1347 nowarn = sysfs_create_link(use->target->holders_dir,
1348 &mod->mkobj.kobj, mod->name);
1349 }
1350 mutex_unlock(&module_mutex);
1351#endif
1352}
1353
1354static void del_usage_links(struct module *mod)
1355{
1356#ifdef CONFIG_MODULE_UNLOAD
1357 struct module_use *use;
1358
1359 mutex_lock(&module_mutex);
1360 list_for_each_entry(use, &mod->target_list, target_list)
1361 sysfs_remove_link(use->target->holders_dir, mod->name);
1362 mutex_unlock(&module_mutex);
1363#endif
1364}
1365
1366static int module_add_modinfo_attrs(struct module *mod)
1301{ 1367{
1302 struct module_attribute *attr; 1368 struct module_attribute *attr;
1303 struct module_attribute *temp_attr; 1369 struct module_attribute *temp_attr;
@@ -1323,7 +1389,7 @@ int module_add_modinfo_attrs(struct module *mod)
1323 return error; 1389 return error;
1324} 1390}
1325 1391
1326void module_remove_modinfo_attrs(struct module *mod) 1392static void module_remove_modinfo_attrs(struct module *mod)
1327{ 1393{
1328 struct module_attribute *attr; 1394 struct module_attribute *attr;
1329 int i; 1395 int i;
@@ -1339,7 +1405,7 @@ void module_remove_modinfo_attrs(struct module *mod)
1339 kfree(mod->modinfo_attrs); 1405 kfree(mod->modinfo_attrs);
1340} 1406}
1341 1407
1342int mod_sysfs_init(struct module *mod) 1408static int mod_sysfs_init(struct module *mod)
1343{ 1409{
1344 int err; 1410 int err;
1345 struct kobject *kobj; 1411 struct kobject *kobj;
@@ -1373,12 +1439,16 @@ out:
1373 return err; 1439 return err;
1374} 1440}
1375 1441
1376int mod_sysfs_setup(struct module *mod, 1442static int mod_sysfs_setup(struct module *mod,
1377 struct kernel_param *kparam, 1443 struct kernel_param *kparam,
1378 unsigned int num_params) 1444 unsigned int num_params)
1379{ 1445{
1380 int err; 1446 int err;
1381 1447
1448 err = mod_sysfs_init(mod);
1449 if (err)
1450 goto out;
1451
1382 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); 1452 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1383 if (!mod->holders_dir) { 1453 if (!mod->holders_dir) {
1384 err = -ENOMEM; 1454 err = -ENOMEM;
@@ -1393,6 +1463,8 @@ int mod_sysfs_setup(struct module *mod,
1393 if (err) 1463 if (err)
1394 goto out_unreg_param; 1464 goto out_unreg_param;
1395 1465
1466 add_usage_links(mod);
1467
1396 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); 1468 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1397 return 0; 1469 return 0;
1398 1470
@@ -1402,6 +1474,7 @@ out_unreg_holders:
1402 kobject_put(mod->holders_dir); 1474 kobject_put(mod->holders_dir);
1403out_unreg: 1475out_unreg:
1404 kobject_put(&mod->mkobj.kobj); 1476 kobject_put(&mod->mkobj.kobj);
1477out:
1405 return err; 1478 return err;
1406} 1479}
1407 1480
@@ -1412,14 +1485,40 @@ static void mod_sysfs_fini(struct module *mod)
1412 1485
1413#else /* CONFIG_SYSFS */ 1486#else /* CONFIG_SYSFS */
1414 1487
1488static inline int mod_sysfs_init(struct module *mod)
1489{
1490 return 0;
1491}
1492
1493static inline int mod_sysfs_setup(struct module *mod,
1494 struct kernel_param *kparam,
1495 unsigned int num_params)
1496{
1497 return 0;
1498}
1499
1500static inline int module_add_modinfo_attrs(struct module *mod)
1501{
1502 return 0;
1503}
1504
1505static inline void module_remove_modinfo_attrs(struct module *mod)
1506{
1507}
1508
1415static void mod_sysfs_fini(struct module *mod) 1509static void mod_sysfs_fini(struct module *mod)
1416{ 1510{
1417} 1511}
1418 1512
1513static void del_usage_links(struct module *mod)
1514{
1515}
1516
1419#endif /* CONFIG_SYSFS */ 1517#endif /* CONFIG_SYSFS */
1420 1518
1421static void mod_kobject_remove(struct module *mod) 1519static void mod_kobject_remove(struct module *mod)
1422{ 1520{
1521 del_usage_links(mod);
1423 module_remove_modinfo_attrs(mod); 1522 module_remove_modinfo_attrs(mod);
1424 module_param_sysfs_remove(mod); 1523 module_param_sysfs_remove(mod);
1425 kobject_put(mod->mkobj.drivers_dir); 1524 kobject_put(mod->mkobj.drivers_dir);
@@ -1438,13 +1537,15 @@ static int __unlink_module(void *_mod)
1438 return 0; 1537 return 0;
1439} 1538}
1440 1539
1441/* Free a module, remove from lists, etc (must hold module_mutex). */ 1540/* Free a module, remove from lists, etc. */
1442static void free_module(struct module *mod) 1541static void free_module(struct module *mod)
1443{ 1542{
1444 trace_module_free(mod); 1543 trace_module_free(mod);
1445 1544
1446 /* Delete from various lists */ 1545 /* Delete from various lists */
1546 mutex_lock(&module_mutex);
1447 stop_machine(__unlink_module, mod, NULL); 1547 stop_machine(__unlink_module, mod, NULL);
1548 mutex_unlock(&module_mutex);
1448 remove_notes_attrs(mod); 1549 remove_notes_attrs(mod);
1449 remove_sect_attrs(mod); 1550 remove_sect_attrs(mod);
1450 mod_kobject_remove(mod); 1551 mod_kobject_remove(mod);
@@ -1495,6 +1596,8 @@ EXPORT_SYMBOL_GPL(__symbol_get);
1495/* 1596/*
1496 * Ensure that an exported symbol [global namespace] does not already exist 1597 * Ensure that an exported symbol [global namespace] does not already exist
1497 * in the kernel or in some other module's exported symbol table. 1598 * in the kernel or in some other module's exported symbol table.
1599 *
1600 * You must hold the module_mutex.
1498 */ 1601 */
1499static int verify_export_symbols(struct module *mod) 1602static int verify_export_symbols(struct module *mod)
1500{ 1603{
@@ -1560,21 +1663,23 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1560 break; 1663 break;
1561 1664
1562 case SHN_UNDEF: 1665 case SHN_UNDEF:
1563 ksym = resolve_symbol(sechdrs, versindex, 1666 ksym = resolve_symbol_wait(sechdrs, versindex,
1564 strtab + sym[i].st_name, mod); 1667 strtab + sym[i].st_name,
1668 mod);
1565 /* Ok if resolved. */ 1669 /* Ok if resolved. */
1566 if (ksym) { 1670 if (ksym && !IS_ERR(ksym)) {
1567 sym[i].st_value = ksym->value; 1671 sym[i].st_value = ksym->value;
1568 break; 1672 break;
1569 } 1673 }
1570 1674
1571 /* Ok if weak. */ 1675 /* Ok if weak. */
1572 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1676 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1573 break; 1677 break;
1574 1678
1575 printk(KERN_WARNING "%s: Unknown symbol %s\n", 1679 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1576 mod->name, strtab + sym[i].st_name); 1680 mod->name, strtab + sym[i].st_name,
1577 ret = -ENOENT; 1681 PTR_ERR(ksym));
1682 ret = PTR_ERR(ksym) ?: -ENOENT;
1578 break; 1683 break;
1579 1684
1580 default: 1685 default:
@@ -1957,16 +2062,24 @@ static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1957#endif 2062#endif
1958} 2063}
1959 2064
2065static void dynamic_debug_remove(struct _ddebug *debug)
2066{
2067 if (debug)
2068 ddebug_remove_module(debug->modname);
2069}
2070
1960static void *module_alloc_update_bounds(unsigned long size) 2071static void *module_alloc_update_bounds(unsigned long size)
1961{ 2072{
1962 void *ret = module_alloc(size); 2073 void *ret = module_alloc(size);
1963 2074
1964 if (ret) { 2075 if (ret) {
2076 mutex_lock(&module_mutex);
1965 /* Update module bounds. */ 2077 /* Update module bounds. */
1966 if ((unsigned long)ret < module_addr_min) 2078 if ((unsigned long)ret < module_addr_min)
1967 module_addr_min = (unsigned long)ret; 2079 module_addr_min = (unsigned long)ret;
1968 if ((unsigned long)ret + size > module_addr_max) 2080 if ((unsigned long)ret + size > module_addr_max)
1969 module_addr_max = (unsigned long)ret + size; 2081 module_addr_max = (unsigned long)ret + size;
2082 mutex_unlock(&module_mutex);
1970 } 2083 }
1971 return ret; 2084 return ret;
1972} 2085}
@@ -2016,6 +2129,9 @@ static noinline struct module *load_module(void __user *umod,
2016 long err = 0; 2129 long err = 0;
2017 void *ptr = NULL; /* Stops spurious gcc warning */ 2130 void *ptr = NULL; /* Stops spurious gcc warning */
2018 unsigned long symoffs, stroffs, *strmap; 2131 unsigned long symoffs, stroffs, *strmap;
2132 void __percpu *percpu;
2133 struct _ddebug *debug = NULL;
2134 unsigned int num_debug = 0;
2019 2135
2020 mm_segment_t old_fs; 2136 mm_segment_t old_fs;
2021 2137
@@ -2140,11 +2256,6 @@ static noinline struct module *load_module(void __user *umod,
2140 goto free_mod; 2256 goto free_mod;
2141 } 2257 }
2142 2258
2143 if (find_module(mod->name)) {
2144 err = -EEXIST;
2145 goto free_mod;
2146 }
2147
2148 mod->state = MODULE_STATE_COMING; 2259 mod->state = MODULE_STATE_COMING;
2149 2260
2150 /* Allow arches to frob section contents and sizes. */ 2261 /* Allow arches to frob section contents and sizes. */
@@ -2160,6 +2271,8 @@ static noinline struct module *load_module(void __user *umod,
2160 goto free_mod; 2271 goto free_mod;
2161 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2272 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2162 } 2273 }
2274 /* Keep this around for failure path. */
2275 percpu = mod_percpu(mod);
2163 2276
2164 /* Determine total sizes, and put offsets in sh_entsize. For now 2277 /* Determine total sizes, and put offsets in sh_entsize. For now
2165 this is done generically; there doesn't appear to be any 2278 this is done generically; there doesn't appear to be any
@@ -2233,11 +2346,6 @@ static noinline struct module *load_module(void __user *umod,
2233 /* Now we've moved module, initialize linked lists, etc. */ 2346 /* Now we've moved module, initialize linked lists, etc. */
2234 module_unload_init(mod); 2347 module_unload_init(mod);
2235 2348
2236 /* add kobject, so we can reference it. */
2237 err = mod_sysfs_init(mod);
2238 if (err)
2239 goto free_unload;
2240
2241 /* Set up license info based on the info section */ 2349 /* Set up license info based on the info section */
2242 set_license(mod, get_modinfo(sechdrs, infoindex, "license")); 2350 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2243 2351
@@ -2362,11 +2470,6 @@ static noinline struct module *load_module(void __user *umod,
2362 goto cleanup; 2470 goto cleanup;
2363 } 2471 }
2364 2472
2365 /* Find duplicate symbols */
2366 err = verify_export_symbols(mod);
2367 if (err < 0)
2368 goto cleanup;
2369
2370 /* Set up and sort exception table */ 2473 /* Set up and sort exception table */
2371 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table", 2474 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2372 sizeof(*mod->extable), &mod->num_exentries); 2475 sizeof(*mod->extable), &mod->num_exentries);
@@ -2381,15 +2484,9 @@ static noinline struct module *load_module(void __user *umod,
2381 kfree(strmap); 2484 kfree(strmap);
2382 strmap = NULL; 2485 strmap = NULL;
2383 2486
2384 if (!mod->taints) { 2487 if (!mod->taints)
2385 struct _ddebug *debug;
2386 unsigned int num_debug;
2387
2388 debug = section_objs(hdr, sechdrs, secstrings, "__verbose", 2488 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2389 sizeof(*debug), &num_debug); 2489 sizeof(*debug), &num_debug);
2390 if (debug)
2391 dynamic_debug_setup(debug, num_debug);
2392 }
2393 2490
2394 err = module_finalize(hdr, sechdrs, mod); 2491 err = module_finalize(hdr, sechdrs, mod);
2395 if (err < 0) 2492 if (err < 0)
@@ -2425,7 +2522,22 @@ static noinline struct module *load_module(void __user *umod,
2425 * function to insert in a way safe to concurrent readers. 2522 * function to insert in a way safe to concurrent readers.
2426 * The mutex protects against concurrent writers. 2523 * The mutex protects against concurrent writers.
2427 */ 2524 */
2525 mutex_lock(&module_mutex);
2526 if (find_module(mod->name)) {
2527 err = -EEXIST;
2528 goto unlock;
2529 }
2530
2531 if (debug)
2532 dynamic_debug_setup(debug, num_debug);
2533
2534 /* Find duplicate symbols */
2535 err = verify_export_symbols(mod);
2536 if (err < 0)
2537 goto ddebug;
2538
2428 list_add_rcu(&mod->list, &modules); 2539 list_add_rcu(&mod->list, &modules);
2540 mutex_unlock(&module_mutex);
2429 2541
2430 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL); 2542 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2431 if (err < 0) 2543 if (err < 0)
@@ -2434,6 +2546,7 @@ static noinline struct module *load_module(void __user *umod,
2434 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp); 2546 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2435 if (err < 0) 2547 if (err < 0)
2436 goto unlink; 2548 goto unlink;
2549
2437 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2550 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2438 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2551 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2439 2552
@@ -2446,15 +2559,17 @@ static noinline struct module *load_module(void __user *umod,
2446 return mod; 2559 return mod;
2447 2560
2448 unlink: 2561 unlink:
2562 mutex_lock(&module_mutex);
2449 /* Unlink carefully: kallsyms could be walking list. */ 2563 /* Unlink carefully: kallsyms could be walking list. */
2450 list_del_rcu(&mod->list); 2564 list_del_rcu(&mod->list);
2565 ddebug:
2566 dynamic_debug_remove(debug);
2567 unlock:
2568 mutex_unlock(&module_mutex);
2451 synchronize_sched(); 2569 synchronize_sched();
2452 module_arch_cleanup(mod); 2570 module_arch_cleanup(mod);
2453 cleanup: 2571 cleanup:
2454 free_modinfo(mod); 2572 free_modinfo(mod);
2455 kobject_del(&mod->mkobj.kobj);
2456 kobject_put(&mod->mkobj.kobj);
2457 free_unload:
2458 module_unload_free(mod); 2573 module_unload_free(mod);
2459#if defined(CONFIG_MODULE_UNLOAD) 2574#if defined(CONFIG_MODULE_UNLOAD)
2460 free_percpu(mod->refptr); 2575 free_percpu(mod->refptr);
@@ -2465,7 +2580,7 @@ static noinline struct module *load_module(void __user *umod,
2465 module_free(mod, mod->module_core); 2580 module_free(mod, mod->module_core);
2466 /* mod will be freed with core. Don't access it beyond this line! */ 2581 /* mod will be freed with core. Don't access it beyond this line! */
2467 free_percpu: 2582 free_percpu:
2468 percpu_modfree(mod); 2583 free_percpu(percpu);
2469 free_mod: 2584 free_mod:
2470 kfree(args); 2585 kfree(args);
2471 kfree(strmap); 2586 kfree(strmap);
@@ -2501,19 +2616,10 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2501 if (!capable(CAP_SYS_MODULE) || modules_disabled) 2616 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2502 return -EPERM; 2617 return -EPERM;
2503 2618
2504 /* Only one module load at a time, please */
2505 if (mutex_lock_interruptible(&module_mutex) != 0)
2506 return -EINTR;
2507
2508 /* Do all the hard work */ 2619 /* Do all the hard work */
2509 mod = load_module(umod, len, uargs); 2620 mod = load_module(umod, len, uargs);
2510 if (IS_ERR(mod)) { 2621 if (IS_ERR(mod))
2511 mutex_unlock(&module_mutex);
2512 return PTR_ERR(mod); 2622 return PTR_ERR(mod);
2513 }
2514
2515 /* Drop lock so they can recurse */
2516 mutex_unlock(&module_mutex);
2517 2623
2518 blocking_notifier_call_chain(&module_notify_list, 2624 blocking_notifier_call_chain(&module_notify_list,
2519 MODULE_STATE_COMING, mod); 2625 MODULE_STATE_COMING, mod);
@@ -2530,9 +2636,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2530 module_put(mod); 2636 module_put(mod);
2531 blocking_notifier_call_chain(&module_notify_list, 2637 blocking_notifier_call_chain(&module_notify_list,
2532 MODULE_STATE_GOING, mod); 2638 MODULE_STATE_GOING, mod);
2533 mutex_lock(&module_mutex);
2534 free_module(mod); 2639 free_module(mod);
2535 mutex_unlock(&module_mutex);
2536 wake_up(&module_wq); 2640 wake_up(&module_wq);
2537 return ret; 2641 return ret;
2538 } 2642 }