aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c382
1 files changed, 241 insertions, 141 deletions
diff --git a/kernel/module.c b/kernel/module.c
index 1016b75b026a..6c562828c85c 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -59,8 +59,6 @@
59#define CREATE_TRACE_POINTS 59#define CREATE_TRACE_POINTS
60#include <trace/events/module.h> 60#include <trace/events/module.h>
61 61
62EXPORT_TRACEPOINT_SYMBOL(module_get);
63
64#if 0 62#if 0
65#define DEBUGP printk 63#define DEBUGP printk
66#else 64#else
@@ -74,11 +72,19 @@ EXPORT_TRACEPOINT_SYMBOL(module_get);
74/* 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 */
75#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) 73#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
76 74
77/* 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.
78 * (delete uses stop_machine/add uses RCU list operations). */ 80 * (delete uses stop_machine/add uses RCU list operations). */
79DEFINE_MUTEX(module_mutex); 81DEFINE_MUTEX(module_mutex);
80EXPORT_SYMBOL_GPL(module_mutex); 82EXPORT_SYMBOL_GPL(module_mutex);
81static LIST_HEAD(modules); 83static LIST_HEAD(modules);
84#ifdef CONFIG_KGDB_KDB
85struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
86#endif /* CONFIG_KGDB_KDB */
87
82 88
83/* Block module loading/unloading? */ 89/* Block module loading/unloading? */
84int modules_disabled = 0; 90int modules_disabled = 0;
@@ -88,7 +94,8 @@ static DECLARE_WAIT_QUEUE_HEAD(module_wq);
88 94
89static BLOCKING_NOTIFIER_HEAD(module_notify_list); 95static BLOCKING_NOTIFIER_HEAD(module_notify_list);
90 96
91/* Bounds of module allocation, for speeding __module_address */ 97/* Bounds of module allocation, for speeding __module_address.
98 * Protected by module_mutex. */
92static unsigned long module_addr_min = -1UL, module_addr_max = 0; 99static unsigned long module_addr_min = -1UL, module_addr_max = 0;
93 100
94int register_module_notifier(struct notifier_block * nb) 101int register_module_notifier(struct notifier_block * nb)
@@ -178,8 +185,6 @@ extern const struct kernel_symbol __start___ksymtab_gpl[];
178extern const struct kernel_symbol __stop___ksymtab_gpl[]; 185extern const struct kernel_symbol __stop___ksymtab_gpl[];
179extern const struct kernel_symbol __start___ksymtab_gpl_future[]; 186extern const struct kernel_symbol __start___ksymtab_gpl_future[];
180extern const struct kernel_symbol __stop___ksymtab_gpl_future[]; 187extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
181extern const struct kernel_symbol __start___ksymtab_gpl_future[];
182extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
183extern const unsigned long __start___kcrctab[]; 188extern const unsigned long __start___kcrctab[];
184extern const unsigned long __start___kcrctab_gpl[]; 189extern const unsigned long __start___kcrctab_gpl[];
185extern const unsigned long __start___kcrctab_gpl_future[]; 190extern const unsigned long __start___kcrctab_gpl_future[];
@@ -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,
@@ -515,12 +520,16 @@ MODINFO_ATTR(srcversion);
515static char last_unloaded_module[MODULE_NAME_LEN+1]; 520static char last_unloaded_module[MODULE_NAME_LEN+1];
516 521
517#ifdef CONFIG_MODULE_UNLOAD 522#ifdef CONFIG_MODULE_UNLOAD
523
524EXPORT_TRACEPOINT_SYMBOL(module_get);
525
518/* Init the unload section of the module. */ 526/* Init the unload section of the module. */
519static void module_unload_init(struct module *mod) 527static void module_unload_init(struct module *mod)
520{ 528{
521 int cpu; 529 int cpu;
522 530
523 INIT_LIST_HEAD(&mod->modules_which_use_me); 531 INIT_LIST_HEAD(&mod->source_list);
532 INIT_LIST_HEAD(&mod->target_list);
524 for_each_possible_cpu(cpu) { 533 for_each_possible_cpu(cpu) {
525 per_cpu_ptr(mod->refptr, cpu)->incs = 0; 534 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
526 per_cpu_ptr(mod->refptr, cpu)->decs = 0; 535 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
@@ -532,20 +541,13 @@ static void module_unload_init(struct module *mod)
532 mod->waiter = current; 541 mod->waiter = current;
533} 542}
534 543
535/* modules using other modules */
536struct module_use
537{
538 struct list_head list;
539 struct module *module_which_uses;
540};
541
542/* Does a already use b? */ 544/* Does a already use b? */
543static int already_uses(struct module *a, struct module *b) 545static int already_uses(struct module *a, struct module *b)
544{ 546{
545 struct module_use *use; 547 struct module_use *use;
546 548
547 list_for_each_entry(use, &b->modules_which_use_me, list) { 549 list_for_each_entry(use, &b->source_list, source_list) {
548 if (use->module_which_uses == a) { 550 if (use->source == a) {
549 DEBUGP("%s uses %s!\n", a->name, b->name); 551 DEBUGP("%s uses %s!\n", a->name, b->name);
550 return 1; 552 return 1;
551 } 553 }
@@ -554,62 +556,68 @@ static int already_uses(struct module *a, struct module *b)
554 return 0; 556 return 0;
555} 557}
556 558
557/* Module a uses b */ 559/*
558int 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)
559{ 567{
560 struct module_use *use; 568 struct module_use *use;
561 int no_warn, err;
562 569
563 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;
564 588
565 /* If we're interrupted or time out, we fail. */ 589 if (b == NULL || already_uses(a, b))
566 if (wait_event_interruptible_timeout(
567 module_wq, (err = strong_try_module_get(b)) != -EBUSY,
568 30 * HZ) <= 0) {
569 printk("%s: gave up waiting for init of module %s.\n",
570 a->name, b->name);
571 return 0; 590 return 0;
572 }
573 591
574 /* 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);
575 if (err) 594 if (err)
576 return 0; 595 return err;
577 596
578 DEBUGP("Allocating new usage for %s.\n", a->name); 597 err = add_module_usage(a, b);
579 use = kmalloc(sizeof(*use), GFP_ATOMIC); 598 if (err) {
580 if (!use) {
581 printk("%s: out of memory loading\n", a->name);
582 module_put(b); 599 module_put(b);
583 return 0; 600 return err;
584 } 601 }
585 602 return 0;
586 use->module_which_uses = a;
587 list_add(&use->list, &b->modules_which_use_me);
588 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
589 return 1;
590} 603}
591EXPORT_SYMBOL_GPL(use_module); 604EXPORT_SYMBOL_GPL(ref_module);
592 605
593/* Clear the unload stuff of the module. */ 606/* Clear the unload stuff of the module. */
594static void module_unload_free(struct module *mod) 607static void module_unload_free(struct module *mod)
595{ 608{
596 struct module *i; 609 struct module_use *use, *tmp;
597
598 list_for_each_entry(i, &modules, list) {
599 struct module_use *use;
600 610
601 list_for_each_entry(use, &i->modules_which_use_me, list) { 611 mutex_lock(&module_mutex);
602 if (use->module_which_uses == mod) { 612 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
603 DEBUGP("%s unusing %s\n", mod->name, i->name); 613 struct module *i = use->target;
604 module_put(i); 614 DEBUGP("%s unusing %s\n", mod->name, i->name);
605 list_del(&use->list); 615 module_put(i);
606 kfree(use); 616 list_del(&use->source_list);
607 sysfs_remove_link(i->holders_dir, mod->name); 617 list_del(&use->target_list);
608 /* There can be at most one match. */ 618 kfree(use);
609 break;
610 }
611 }
612 } 619 }
620 mutex_unlock(&module_mutex);
613} 621}
614 622
615#ifdef CONFIG_MODULE_FORCE_UNLOAD 623#ifdef CONFIG_MODULE_FORCE_UNLOAD
@@ -723,16 +731,8 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
723 return -EFAULT; 731 return -EFAULT;
724 name[MODULE_NAME_LEN-1] = '\0'; 732 name[MODULE_NAME_LEN-1] = '\0';
725 733
726 /* Create stop_machine threads since free_module relies on 734 if (mutex_lock_interruptible(&module_mutex) != 0)
727 * a non-failing stop_machine call. */ 735 return -EINTR;
728 ret = stop_machine_create();
729 if (ret)
730 return ret;
731
732 if (mutex_lock_interruptible(&module_mutex) != 0) {
733 ret = -EINTR;
734 goto out_stop;
735 }
736 736
737 mod = find_module(name); 737 mod = find_module(name);
738 if (!mod) { 738 if (!mod) {
@@ -740,7 +740,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
740 goto out; 740 goto out;
741 } 741 }
742 742
743 if (!list_empty(&mod->modules_which_use_me)) { 743 if (!list_empty(&mod->source_list)) {
744 /* Other modules depend on us: get rid of them first. */ 744 /* Other modules depend on us: get rid of them first. */
745 ret = -EWOULDBLOCK; 745 ret = -EWOULDBLOCK;
746 goto out; 746 goto out;
@@ -784,16 +784,14 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
784 blocking_notifier_call_chain(&module_notify_list, 784 blocking_notifier_call_chain(&module_notify_list,
785 MODULE_STATE_GOING, mod); 785 MODULE_STATE_GOING, mod);
786 async_synchronize_full(); 786 async_synchronize_full();
787 mutex_lock(&module_mutex); 787
788 /* Store the name of the last unloaded module for diagnostic purposes */ 788 /* Store the name of the last unloaded module for diagnostic purposes */
789 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); 789 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
790 ddebug_remove_module(mod->name);
791 free_module(mod);
792 790
793 out: 791 free_module(mod);
792 return 0;
793out:
794 mutex_unlock(&module_mutex); 794 mutex_unlock(&module_mutex);
795out_stop:
796 stop_machine_destroy();
797 return ret; 795 return ret;
798} 796}
799 797
@@ -806,9 +804,9 @@ static inline void print_unload_info(struct seq_file *m, struct module *mod)
806 804
807 /* Always include a trailing , so userspace can differentiate 805 /* Always include a trailing , so userspace can differentiate
808 between this and the old multi-field proc format. */ 806 between this and the old multi-field proc format. */
809 list_for_each_entry(use, &mod->modules_which_use_me, list) { 807 list_for_each_entry(use, &mod->source_list, source_list) {
810 printed_something = 1; 808 printed_something = 1;
811 seq_printf(m, "%s,", use->module_which_uses->name); 809 seq_printf(m, "%s,", use->source->name);
812 } 810 }
813 811
814 if (mod->init != NULL && mod->exit == NULL) { 812 if (mod->init != NULL && mod->exit == NULL) {
@@ -867,8 +865,7 @@ void module_put(struct module *module)
867 smp_wmb(); /* see comment in module_refcount */ 865 smp_wmb(); /* see comment in module_refcount */
868 __this_cpu_inc(module->refptr->decs); 866 __this_cpu_inc(module->refptr->decs);
869 867
870 trace_module_put(module, _RET_IP_, 868 trace_module_put(module, _RET_IP_);
871 __this_cpu_read(module->refptr->decs));
872 /* Maybe they're waiting for us to drop reference? */ 869 /* Maybe they're waiting for us to drop reference? */
873 if (unlikely(!module_is_live(module))) 870 if (unlikely(!module_is_live(module)))
874 wake_up_process(module->waiter); 871 wake_up_process(module->waiter);
@@ -888,11 +885,11 @@ static inline void module_unload_free(struct module *mod)
888{ 885{
889} 886}
890 887
891int use_module(struct module *a, struct module *b) 888int ref_module(struct module *a, struct module *b)
892{ 889{
893 return strong_try_module_get(b) == 0; 890 return strong_try_module_get(b);
894} 891}
895EXPORT_SYMBOL_GPL(use_module); 892EXPORT_SYMBOL_GPL(ref_module);
896 893
897static inline void module_unload_init(struct module *mod) 894static inline void module_unload_init(struct module *mod)
898{ 895{
@@ -1009,6 +1006,8 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1009{ 1006{
1010 const unsigned long *crc; 1007 const unsigned long *crc;
1011 1008
1009 /* Since this should be found in kernel (which can't be removed),
1010 * no locking is necessary. */
1012 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL, 1011 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1013 &crc, true, false)) 1012 &crc, true, false))
1014 BUG(); 1013 BUG();
@@ -1051,29 +1050,62 @@ static inline int same_magic(const char *amagic, const char *bmagic,
1051} 1050}
1052#endif /* CONFIG_MODVERSIONS */ 1051#endif /* CONFIG_MODVERSIONS */
1053 1052
1054/* 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. */
1055 Must be holding module_mutex. */
1056static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, 1054static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1057 unsigned int versindex, 1055 unsigned int versindex,
1058 const char *name, 1056 const char *name,
1059 struct module *mod) 1057 struct module *mod,
1058 char ownername[])
1060{ 1059{
1061 struct module *owner; 1060 struct module *owner;
1062 const struct kernel_symbol *sym; 1061 const struct kernel_symbol *sym;
1063 const unsigned long *crc; 1062 const unsigned long *crc;
1063 int err;
1064 1064
1065 mutex_lock(&module_mutex);
1065 sym = find_symbol(name, &owner, &crc, 1066 sym = find_symbol(name, &owner, &crc,
1066 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1067 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1067 /* use_module can fail due to OOM, 1068 if (!sym)
1068 or module initialization or unloading */ 1069 goto unlock;
1069 if (sym) { 1070
1070 if (!check_version(sechdrs, versindex, name, mod, crc, owner) 1071 if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
1071 || !use_module(mod, owner)) 1072 sym = ERR_PTR(-EINVAL);
1072 sym = NULL; 1073 goto getname;
1074 }
1075
1076 err = ref_module(mod, owner);
1077 if (err) {
1078 sym = ERR_PTR(err);
1079 goto getname;
1073 } 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);
1074 return sym; 1087 return sym;
1075} 1088}
1076 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
1077/* 1109/*
1078 * /sys/module/foo/sections stuff 1110 * /sys/module/foo/sections stuff
1079 * J. Corbet <corbet@lwn.net> 1111 * J. Corbet <corbet@lwn.net>
@@ -1192,7 +1224,7 @@ struct module_notes_attrs {
1192 struct bin_attribute attrs[0]; 1224 struct bin_attribute attrs[0];
1193}; 1225};
1194 1226
1195static ssize_t module_notes_read(struct kobject *kobj, 1227static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1196 struct bin_attribute *bin_attr, 1228 struct bin_attribute *bin_attr,
1197 char *buf, loff_t pos, size_t count) 1229 char *buf, loff_t pos, size_t count)
1198{ 1230{
@@ -1303,7 +1335,34 @@ static inline void remove_notes_attrs(struct module *mod)
1303#endif 1335#endif
1304 1336
1305#ifdef CONFIG_SYSFS 1337#ifdef CONFIG_SYSFS
1306int 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)
1307{ 1366{
1308 struct module_attribute *attr; 1367 struct module_attribute *attr;
1309 struct module_attribute *temp_attr; 1368 struct module_attribute *temp_attr;
@@ -1329,7 +1388,7 @@ int module_add_modinfo_attrs(struct module *mod)
1329 return error; 1388 return error;
1330} 1389}
1331 1390
1332void module_remove_modinfo_attrs(struct module *mod) 1391static void module_remove_modinfo_attrs(struct module *mod)
1333{ 1392{
1334 struct module_attribute *attr; 1393 struct module_attribute *attr;
1335 int i; 1394 int i;
@@ -1345,7 +1404,7 @@ void module_remove_modinfo_attrs(struct module *mod)
1345 kfree(mod->modinfo_attrs); 1404 kfree(mod->modinfo_attrs);
1346} 1405}
1347 1406
1348int mod_sysfs_init(struct module *mod) 1407static int mod_sysfs_init(struct module *mod)
1349{ 1408{
1350 int err; 1409 int err;
1351 struct kobject *kobj; 1410 struct kobject *kobj;
@@ -1379,12 +1438,16 @@ out:
1379 return err; 1438 return err;
1380} 1439}
1381 1440
1382int mod_sysfs_setup(struct module *mod, 1441static int mod_sysfs_setup(struct module *mod,
1383 struct kernel_param *kparam, 1442 struct kernel_param *kparam,
1384 unsigned int num_params) 1443 unsigned int num_params)
1385{ 1444{
1386 int err; 1445 int err;
1387 1446
1447 err = mod_sysfs_init(mod);
1448 if (err)
1449 goto out;
1450
1388 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); 1451 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1389 if (!mod->holders_dir) { 1452 if (!mod->holders_dir) {
1390 err = -ENOMEM; 1453 err = -ENOMEM;
@@ -1399,6 +1462,8 @@ int mod_sysfs_setup(struct module *mod,
1399 if (err) 1462 if (err)
1400 goto out_unreg_param; 1463 goto out_unreg_param;
1401 1464
1465 add_usage_links(mod);
1466
1402 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); 1467 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1403 return 0; 1468 return 0;
1404 1469
@@ -1408,6 +1473,7 @@ out_unreg_holders:
1408 kobject_put(mod->holders_dir); 1473 kobject_put(mod->holders_dir);
1409out_unreg: 1474out_unreg:
1410 kobject_put(&mod->mkobj.kobj); 1475 kobject_put(&mod->mkobj.kobj);
1476out:
1411 return err; 1477 return err;
1412} 1478}
1413 1479
@@ -1418,14 +1484,40 @@ static void mod_sysfs_fini(struct module *mod)
1418 1484
1419#else /* CONFIG_SYSFS */ 1485#else /* CONFIG_SYSFS */
1420 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
1421static void mod_sysfs_fini(struct module *mod) 1508static void mod_sysfs_fini(struct module *mod)
1422{ 1509{
1423} 1510}
1424 1511
1512static void del_usage_links(struct module *mod)
1513{
1514}
1515
1425#endif /* CONFIG_SYSFS */ 1516#endif /* CONFIG_SYSFS */
1426 1517
1427static void mod_kobject_remove(struct module *mod) 1518static void mod_kobject_remove(struct module *mod)
1428{ 1519{
1520 del_usage_links(mod);
1429 module_remove_modinfo_attrs(mod); 1521 module_remove_modinfo_attrs(mod);
1430 module_param_sysfs_remove(mod); 1522 module_param_sysfs_remove(mod);
1431 kobject_put(mod->mkobj.drivers_dir); 1523 kobject_put(mod->mkobj.drivers_dir);
@@ -1444,17 +1536,22 @@ static int __unlink_module(void *_mod)
1444 return 0; 1536 return 0;
1445} 1537}
1446 1538
1447/* Free a module, remove from lists, etc (must hold module_mutex). */ 1539/* Free a module, remove from lists, etc. */
1448static void free_module(struct module *mod) 1540static void free_module(struct module *mod)
1449{ 1541{
1450 trace_module_free(mod); 1542 trace_module_free(mod);
1451 1543
1452 /* Delete from various lists */ 1544 /* Delete from various lists */
1545 mutex_lock(&module_mutex);
1453 stop_machine(__unlink_module, mod, NULL); 1546 stop_machine(__unlink_module, mod, NULL);
1547 mutex_unlock(&module_mutex);
1454 remove_notes_attrs(mod); 1548 remove_notes_attrs(mod);
1455 remove_sect_attrs(mod); 1549 remove_sect_attrs(mod);
1456 mod_kobject_remove(mod); 1550 mod_kobject_remove(mod);
1457 1551
1552 /* Remove dynamic debug info */
1553 ddebug_remove_module(mod->name);
1554
1458 /* Arch-specific cleanup. */ 1555 /* Arch-specific cleanup. */
1459 module_arch_cleanup(mod); 1556 module_arch_cleanup(mod);
1460 1557
@@ -1501,6 +1598,8 @@ EXPORT_SYMBOL_GPL(__symbol_get);
1501/* 1598/*
1502 * Ensure that an exported symbol [global namespace] does not already exist 1599 * Ensure that an exported symbol [global namespace] does not already exist
1503 * 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.
1504 */ 1603 */
1505static int verify_export_symbols(struct module *mod) 1604static int verify_export_symbols(struct module *mod)
1506{ 1605{
@@ -1566,21 +1665,23 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1566 break; 1665 break;
1567 1666
1568 case SHN_UNDEF: 1667 case SHN_UNDEF:
1569 ksym = resolve_symbol(sechdrs, versindex, 1668 ksym = resolve_symbol_wait(sechdrs, versindex,
1570 strtab + sym[i].st_name, mod); 1669 strtab + sym[i].st_name,
1670 mod);
1571 /* Ok if resolved. */ 1671 /* Ok if resolved. */
1572 if (ksym) { 1672 if (ksym && !IS_ERR(ksym)) {
1573 sym[i].st_value = ksym->value; 1673 sym[i].st_value = ksym->value;
1574 break; 1674 break;
1575 } 1675 }
1576 1676
1577 /* Ok if weak. */ 1677 /* Ok if weak. */
1578 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1678 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1579 break; 1679 break;
1580 1680
1581 printk(KERN_WARNING "%s: Unknown symbol %s\n", 1681 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1582 mod->name, strtab + sym[i].st_name); 1682 mod->name, strtab + sym[i].st_name,
1583 ret = -ENOENT; 1683 PTR_ERR(ksym));
1684 ret = PTR_ERR(ksym) ?: -ENOENT;
1584 break; 1685 break;
1585 1686
1586 default: 1687 default:
@@ -1963,16 +2064,24 @@ static void dynamic_debug_setup(struct _ddebug *debug, unsigned int num)
1963#endif 2064#endif
1964} 2065}
1965 2066
2067static void dynamic_debug_remove(struct _ddebug *debug)
2068{
2069 if (debug)
2070 ddebug_remove_module(debug->modname);
2071}
2072
1966static void *module_alloc_update_bounds(unsigned long size) 2073static void *module_alloc_update_bounds(unsigned long size)
1967{ 2074{
1968 void *ret = module_alloc(size); 2075 void *ret = module_alloc(size);
1969 2076
1970 if (ret) { 2077 if (ret) {
2078 mutex_lock(&module_mutex);
1971 /* Update module bounds. */ 2079 /* Update module bounds. */
1972 if ((unsigned long)ret < module_addr_min) 2080 if ((unsigned long)ret < module_addr_min)
1973 module_addr_min = (unsigned long)ret; 2081 module_addr_min = (unsigned long)ret;
1974 if ((unsigned long)ret + size > module_addr_max) 2082 if ((unsigned long)ret + size > module_addr_max)
1975 module_addr_max = (unsigned long)ret + size; 2083 module_addr_max = (unsigned long)ret + size;
2084 mutex_unlock(&module_mutex);
1976 } 2085 }
1977 return ret; 2086 return ret;
1978} 2087}
@@ -2022,6 +2131,9 @@ static noinline struct module *load_module(void __user *umod,
2022 long err = 0; 2131 long err = 0;
2023 void *ptr = NULL; /* Stops spurious gcc warning */ 2132 void *ptr = NULL; /* Stops spurious gcc warning */
2024 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;
2025 2137
2026 mm_segment_t old_fs; 2138 mm_segment_t old_fs;
2027 2139
@@ -2146,11 +2258,6 @@ static noinline struct module *load_module(void __user *umod,
2146 goto free_mod; 2258 goto free_mod;
2147 } 2259 }
2148 2260
2149 if (find_module(mod->name)) {
2150 err = -EEXIST;
2151 goto free_mod;
2152 }
2153
2154 mod->state = MODULE_STATE_COMING; 2261 mod->state = MODULE_STATE_COMING;
2155 2262
2156 /* Allow arches to frob section contents and sizes. */ 2263 /* Allow arches to frob section contents and sizes. */
@@ -2166,6 +2273,8 @@ static noinline struct module *load_module(void __user *umod,
2166 goto free_mod; 2273 goto free_mod;
2167 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2274 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2168 } 2275 }
2276 /* Keep this around for failure path. */
2277 percpu = mod_percpu(mod);
2169 2278
2170 /* Determine total sizes, and put offsets in sh_entsize. For now 2279 /* Determine total sizes, and put offsets in sh_entsize. For now
2171 this is done generically; there doesn't appear to be any 2280 this is done generically; there doesn't appear to be any
@@ -2239,11 +2348,6 @@ static noinline struct module *load_module(void __user *umod,
2239 /* Now we've moved module, initialize linked lists, etc. */ 2348 /* Now we've moved module, initialize linked lists, etc. */
2240 module_unload_init(mod); 2349 module_unload_init(mod);
2241 2350
2242 /* add kobject, so we can reference it. */
2243 err = mod_sysfs_init(mod);
2244 if (err)
2245 goto free_unload;
2246
2247 /* Set up license info based on the info section */ 2351 /* Set up license info based on the info section */
2248 set_license(mod, get_modinfo(sechdrs, infoindex, "license")); 2352 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2249 2353
@@ -2368,11 +2472,6 @@ static noinline struct module *load_module(void __user *umod,
2368 goto cleanup; 2472 goto cleanup;
2369 } 2473 }
2370 2474
2371 /* Find duplicate symbols */
2372 err = verify_export_symbols(mod);
2373 if (err < 0)
2374 goto cleanup;
2375
2376 /* Set up and sort exception table */ 2475 /* Set up and sort exception table */
2377 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table", 2476 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2378 sizeof(*mod->extable), &mod->num_exentries); 2477 sizeof(*mod->extable), &mod->num_exentries);
@@ -2387,15 +2486,9 @@ static noinline struct module *load_module(void __user *umod,
2387 kfree(strmap); 2486 kfree(strmap);
2388 strmap = NULL; 2487 strmap = NULL;
2389 2488
2390 if (!mod->taints) { 2489 if (!mod->taints)
2391 struct _ddebug *debug;
2392 unsigned int num_debug;
2393
2394 debug = section_objs(hdr, sechdrs, secstrings, "__verbose", 2490 debug = section_objs(hdr, sechdrs, secstrings, "__verbose",
2395 sizeof(*debug), &num_debug); 2491 sizeof(*debug), &num_debug);
2396 if (debug)
2397 dynamic_debug_setup(debug, num_debug);
2398 }
2399 2492
2400 err = module_finalize(hdr, sechdrs, mod); 2493 err = module_finalize(hdr, sechdrs, mod);
2401 if (err < 0) 2494 if (err < 0)
@@ -2431,7 +2524,22 @@ static noinline struct module *load_module(void __user *umod,
2431 * function to insert in a way safe to concurrent readers. 2524 * function to insert in a way safe to concurrent readers.
2432 * The mutex protects against concurrent writers. 2525 * The mutex protects against concurrent writers.
2433 */ 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
2434 list_add_rcu(&mod->list, &modules); 2541 list_add_rcu(&mod->list, &modules);
2542 mutex_unlock(&module_mutex);
2435 2543
2436 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);
2437 if (err < 0) 2545 if (err < 0)
@@ -2440,6 +2548,7 @@ static noinline struct module *load_module(void __user *umod,
2440 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp); 2548 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2441 if (err < 0) 2549 if (err < 0)
2442 goto unlink; 2550 goto unlink;
2551
2443 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2552 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2444 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2553 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2445 2554
@@ -2452,15 +2561,17 @@ static noinline struct module *load_module(void __user *umod,
2452 return mod; 2561 return mod;
2453 2562
2454 unlink: 2563 unlink:
2564 mutex_lock(&module_mutex);
2455 /* Unlink carefully: kallsyms could be walking list. */ 2565 /* Unlink carefully: kallsyms could be walking list. */
2456 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);
2457 synchronize_sched(); 2571 synchronize_sched();
2458 module_arch_cleanup(mod); 2572 module_arch_cleanup(mod);
2459 cleanup: 2573 cleanup:
2460 free_modinfo(mod); 2574 free_modinfo(mod);
2461 kobject_del(&mod->mkobj.kobj);
2462 kobject_put(&mod->mkobj.kobj);
2463 free_unload:
2464 module_unload_free(mod); 2575 module_unload_free(mod);
2465#if defined(CONFIG_MODULE_UNLOAD) 2576#if defined(CONFIG_MODULE_UNLOAD)
2466 free_percpu(mod->refptr); 2577 free_percpu(mod->refptr);
@@ -2471,7 +2582,7 @@ static noinline struct module *load_module(void __user *umod,
2471 module_free(mod, mod->module_core); 2582 module_free(mod, mod->module_core);
2472 /* 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! */
2473 free_percpu: 2584 free_percpu:
2474 percpu_modfree(mod); 2585 free_percpu(percpu);
2475 free_mod: 2586 free_mod:
2476 kfree(args); 2587 kfree(args);
2477 kfree(strmap); 2588 kfree(strmap);
@@ -2507,19 +2618,10 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2507 if (!capable(CAP_SYS_MODULE) || modules_disabled) 2618 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2508 return -EPERM; 2619 return -EPERM;
2509 2620
2510 /* Only one module load at a time, please */
2511 if (mutex_lock_interruptible(&module_mutex) != 0)
2512 return -EINTR;
2513
2514 /* Do all the hard work */ 2621 /* Do all the hard work */
2515 mod = load_module(umod, len, uargs); 2622 mod = load_module(umod, len, uargs);
2516 if (IS_ERR(mod)) { 2623 if (IS_ERR(mod))
2517 mutex_unlock(&module_mutex);
2518 return PTR_ERR(mod); 2624 return PTR_ERR(mod);
2519 }
2520
2521 /* Drop lock so they can recurse */
2522 mutex_unlock(&module_mutex);
2523 2625
2524 blocking_notifier_call_chain(&module_notify_list, 2626 blocking_notifier_call_chain(&module_notify_list,
2525 MODULE_STATE_COMING, mod); 2627 MODULE_STATE_COMING, mod);
@@ -2536,9 +2638,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2536 module_put(mod); 2638 module_put(mod);
2537 blocking_notifier_call_chain(&module_notify_list, 2639 blocking_notifier_call_chain(&module_notify_list,
2538 MODULE_STATE_GOING, mod); 2640 MODULE_STATE_GOING, mod);
2539 mutex_lock(&module_mutex);
2540 free_module(mod); 2641 free_module(mod);
2541 mutex_unlock(&module_mutex);
2542 wake_up(&module_wq); 2642 wake_up(&module_wq);
2543 return ret; 2643 return ret;
2544 } 2644 }