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