aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/module.c')
-rw-r--r--kernel/module.c357
1 files changed, 224 insertions, 133 deletions
diff --git a/kernel/module.c b/kernel/module.c
index 1016b75b026a..8c6b42840dd1 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 610
598 list_for_each_entry(i, &modules, list) { 611 mutex_lock(&module_mutex);
599 struct module_use *use; 612 list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
600 613 struct module *i = use->target;
601 list_for_each_entry(use, &i->modules_which_use_me, list) { 614 DEBUGP("%s unusing %s\n", mod->name, i->name);
602 if (use->module_which_uses == mod) { 615 module_put(i);
603 DEBUGP("%s unusing %s\n", mod->name, i->name); 616 list_del(&use->source_list);
604 module_put(i); 617 list_del(&use->target_list);
605 list_del(&use->list); 618 kfree(use);
606 kfree(use);
607 sysfs_remove_link(i->holders_dir, mod->name);
608 /* There can be at most one match. */
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,15 @@ 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); 790 ddebug_remove_module(mod->name);
791 free_module(mod);
792 791
793 out: 792 free_module(mod);
793 return 0;
794out:
794 mutex_unlock(&module_mutex); 795 mutex_unlock(&module_mutex);
795out_stop:
796 stop_machine_destroy();
797 return ret; 796 return ret;
798} 797}
799 798
@@ -806,9 +805,9 @@ static inline void print_unload_info(struct seq_file *m, struct module *mod)
806 805
807 /* Always include a trailing , so userspace can differentiate 806 /* Always include a trailing , so userspace can differentiate
808 between this and the old multi-field proc format. */ 807 between this and the old multi-field proc format. */
809 list_for_each_entry(use, &mod->modules_which_use_me, list) { 808 list_for_each_entry(use, &mod->source_list, source_list) {
810 printed_something = 1; 809 printed_something = 1;
811 seq_printf(m, "%s,", use->module_which_uses->name); 810 seq_printf(m, "%s,", use->source->name);
812 } 811 }
813 812
814 if (mod->init != NULL && mod->exit == NULL) { 813 if (mod->init != NULL && mod->exit == NULL) {
@@ -867,8 +866,7 @@ void module_put(struct module *module)
867 smp_wmb(); /* see comment in module_refcount */ 866 smp_wmb(); /* see comment in module_refcount */
868 __this_cpu_inc(module->refptr->decs); 867 __this_cpu_inc(module->refptr->decs);
869 868
870 trace_module_put(module, _RET_IP_, 869 trace_module_put(module, _RET_IP_);
871 __this_cpu_read(module->refptr->decs));
872 /* Maybe they're waiting for us to drop reference? */ 870 /* Maybe they're waiting for us to drop reference? */
873 if (unlikely(!module_is_live(module))) 871 if (unlikely(!module_is_live(module)))
874 wake_up_process(module->waiter); 872 wake_up_process(module->waiter);
@@ -888,11 +886,11 @@ static inline void module_unload_free(struct module *mod)
888{ 886{
889} 887}
890 888
891int use_module(struct module *a, struct module *b) 889int ref_module(struct module *a, struct module *b)
892{ 890{
893 return strong_try_module_get(b) == 0; 891 return strong_try_module_get(b);
894} 892}
895EXPORT_SYMBOL_GPL(use_module); 893EXPORT_SYMBOL_GPL(ref_module);
896 894
897static inline void module_unload_init(struct module *mod) 895static inline void module_unload_init(struct module *mod)
898{ 896{
@@ -1009,6 +1007,8 @@ static inline int check_modstruct_version(Elf_Shdr *sechdrs,
1009{ 1007{
1010 const unsigned long *crc; 1008 const unsigned long *crc;
1011 1009
1010 /* Since this should be found in kernel (which can't be removed),
1011 * no locking is necessary. */
1012 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL, 1012 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1013 &crc, true, false)) 1013 &crc, true, false))
1014 BUG(); 1014 BUG();
@@ -1051,29 +1051,62 @@ static inline int same_magic(const char *amagic, const char *bmagic,
1051} 1051}
1052#endif /* CONFIG_MODVERSIONS */ 1052#endif /* CONFIG_MODVERSIONS */
1053 1053
1054/* 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. */
1055 Must be holding module_mutex. */
1056static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, 1055static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1057 unsigned int versindex, 1056 unsigned int versindex,
1058 const char *name, 1057 const char *name,
1059 struct module *mod) 1058 struct module *mod,
1059 char ownername[])
1060{ 1060{
1061 struct module *owner; 1061 struct module *owner;
1062 const struct kernel_symbol *sym; 1062 const struct kernel_symbol *sym;
1063 const unsigned long *crc; 1063 const unsigned long *crc;
1064 int err;
1064 1065
1066 mutex_lock(&module_mutex);
1065 sym = find_symbol(name, &owner, &crc, 1067 sym = find_symbol(name, &owner, &crc,
1066 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1068 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1067 /* use_module can fail due to OOM, 1069 if (!sym)
1068 or module initialization or unloading */ 1070 goto unlock;
1069 if (sym) { 1071
1070 if (!check_version(sechdrs, versindex, name, mod, crc, owner) 1072 if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
1071 || !use_module(mod, owner)) 1073 sym = ERR_PTR(-EINVAL);
1072 sym = NULL; 1074 goto getname;
1075 }
1076
1077 err = ref_module(mod, owner);
1078 if (err) {
1079 sym = ERR_PTR(err);
1080 goto getname;
1073 } 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);
1074 return sym; 1088 return sym;
1075} 1089}
1076 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
1077/* 1110/*
1078 * /sys/module/foo/sections stuff 1111 * /sys/module/foo/sections stuff
1079 * J. Corbet <corbet@lwn.net> 1112 * J. Corbet <corbet@lwn.net>
@@ -1192,7 +1225,7 @@ struct module_notes_attrs {
1192 struct bin_attribute attrs[0]; 1225 struct bin_attribute attrs[0];
1193}; 1226};
1194 1227
1195static ssize_t module_notes_read(struct kobject *kobj, 1228static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
1196 struct bin_attribute *bin_attr, 1229 struct bin_attribute *bin_attr,
1197 char *buf, loff_t pos, size_t count) 1230 char *buf, loff_t pos, size_t count)
1198{ 1231{
@@ -1303,7 +1336,34 @@ static inline void remove_notes_attrs(struct module *mod)
1303#endif 1336#endif
1304 1337
1305#ifdef CONFIG_SYSFS 1338#ifdef CONFIG_SYSFS
1306int 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)
1307{ 1367{
1308 struct module_attribute *attr; 1368 struct module_attribute *attr;
1309 struct module_attribute *temp_attr; 1369 struct module_attribute *temp_attr;
@@ -1329,7 +1389,7 @@ int module_add_modinfo_attrs(struct module *mod)
1329 return error; 1389 return error;
1330} 1390}
1331 1391
1332void module_remove_modinfo_attrs(struct module *mod) 1392static void module_remove_modinfo_attrs(struct module *mod)
1333{ 1393{
1334 struct module_attribute *attr; 1394 struct module_attribute *attr;
1335 int i; 1395 int i;
@@ -1345,7 +1405,7 @@ void module_remove_modinfo_attrs(struct module *mod)
1345 kfree(mod->modinfo_attrs); 1405 kfree(mod->modinfo_attrs);
1346} 1406}
1347 1407
1348int mod_sysfs_init(struct module *mod) 1408static int mod_sysfs_init(struct module *mod)
1349{ 1409{
1350 int err; 1410 int err;
1351 struct kobject *kobj; 1411 struct kobject *kobj;
@@ -1379,12 +1439,16 @@ out:
1379 return err; 1439 return err;
1380} 1440}
1381 1441
1382int mod_sysfs_setup(struct module *mod, 1442static int mod_sysfs_setup(struct module *mod,
1383 struct kernel_param *kparam, 1443 struct kernel_param *kparam,
1384 unsigned int num_params) 1444 unsigned int num_params)
1385{ 1445{
1386 int err; 1446 int err;
1387 1447
1448 err = mod_sysfs_init(mod);
1449 if (err)
1450 goto out;
1451
1388 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj); 1452 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1389 if (!mod->holders_dir) { 1453 if (!mod->holders_dir) {
1390 err = -ENOMEM; 1454 err = -ENOMEM;
@@ -1399,6 +1463,8 @@ int mod_sysfs_setup(struct module *mod,
1399 if (err) 1463 if (err)
1400 goto out_unreg_param; 1464 goto out_unreg_param;
1401 1465
1466 add_usage_links(mod);
1467
1402 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD); 1468 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1403 return 0; 1469 return 0;
1404 1470
@@ -1408,6 +1474,7 @@ out_unreg_holders:
1408 kobject_put(mod->holders_dir); 1474 kobject_put(mod->holders_dir);
1409out_unreg: 1475out_unreg:
1410 kobject_put(&mod->mkobj.kobj); 1476 kobject_put(&mod->mkobj.kobj);
1477out:
1411 return err; 1478 return err;
1412} 1479}
1413 1480
@@ -1418,14 +1485,40 @@ static void mod_sysfs_fini(struct module *mod)
1418 1485
1419#else /* CONFIG_SYSFS */ 1486#else /* CONFIG_SYSFS */
1420 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
1421static void mod_sysfs_fini(struct module *mod) 1509static void mod_sysfs_fini(struct module *mod)
1422{ 1510{
1423} 1511}
1424 1512
1513static void del_usage_links(struct module *mod)
1514{
1515}
1516
1425#endif /* CONFIG_SYSFS */ 1517#endif /* CONFIG_SYSFS */
1426 1518
1427static void mod_kobject_remove(struct module *mod) 1519static void mod_kobject_remove(struct module *mod)
1428{ 1520{
1521 del_usage_links(mod);
1429 module_remove_modinfo_attrs(mod); 1522 module_remove_modinfo_attrs(mod);
1430 module_param_sysfs_remove(mod); 1523 module_param_sysfs_remove(mod);
1431 kobject_put(mod->mkobj.drivers_dir); 1524 kobject_put(mod->mkobj.drivers_dir);
@@ -1444,13 +1537,15 @@ static int __unlink_module(void *_mod)
1444 return 0; 1537 return 0;
1445} 1538}
1446 1539
1447/* Free a module, remove from lists, etc (must hold module_mutex). */ 1540/* Free a module, remove from lists, etc. */
1448static void free_module(struct module *mod) 1541static void free_module(struct module *mod)
1449{ 1542{
1450 trace_module_free(mod); 1543 trace_module_free(mod);
1451 1544
1452 /* Delete from various lists */ 1545 /* Delete from various lists */
1546 mutex_lock(&module_mutex);
1453 stop_machine(__unlink_module, mod, NULL); 1547 stop_machine(__unlink_module, mod, NULL);
1548 mutex_unlock(&module_mutex);
1454 remove_notes_attrs(mod); 1549 remove_notes_attrs(mod);
1455 remove_sect_attrs(mod); 1550 remove_sect_attrs(mod);
1456 mod_kobject_remove(mod); 1551 mod_kobject_remove(mod);
@@ -1501,6 +1596,8 @@ EXPORT_SYMBOL_GPL(__symbol_get);
1501/* 1596/*
1502 * Ensure that an exported symbol [global namespace] does not already exist 1597 * Ensure that an exported symbol [global namespace] does not already exist
1503 * 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.
1504 */ 1601 */
1505static int verify_export_symbols(struct module *mod) 1602static int verify_export_symbols(struct module *mod)
1506{ 1603{
@@ -1566,21 +1663,23 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
1566 break; 1663 break;
1567 1664
1568 case SHN_UNDEF: 1665 case SHN_UNDEF:
1569 ksym = resolve_symbol(sechdrs, versindex, 1666 ksym = resolve_symbol_wait(sechdrs, versindex,
1570 strtab + sym[i].st_name, mod); 1667 strtab + sym[i].st_name,
1668 mod);
1571 /* Ok if resolved. */ 1669 /* Ok if resolved. */
1572 if (ksym) { 1670 if (ksym && !IS_ERR(ksym)) {
1573 sym[i].st_value = ksym->value; 1671 sym[i].st_value = ksym->value;
1574 break; 1672 break;
1575 } 1673 }
1576 1674
1577 /* Ok if weak. */ 1675 /* Ok if weak. */
1578 if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1676 if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1579 break; 1677 break;
1580 1678
1581 printk(KERN_WARNING "%s: Unknown symbol %s\n", 1679 printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
1582 mod->name, strtab + sym[i].st_name); 1680 mod->name, strtab + sym[i].st_name,
1583 ret = -ENOENT; 1681 PTR_ERR(ksym));
1682 ret = PTR_ERR(ksym) ?: -ENOENT;
1584 break; 1683 break;
1585 1684
1586 default: 1685 default:
@@ -1968,11 +2067,13 @@ static void *module_alloc_update_bounds(unsigned long size)
1968 void *ret = module_alloc(size); 2067 void *ret = module_alloc(size);
1969 2068
1970 if (ret) { 2069 if (ret) {
2070 mutex_lock(&module_mutex);
1971 /* Update module bounds. */ 2071 /* Update module bounds. */
1972 if ((unsigned long)ret < module_addr_min) 2072 if ((unsigned long)ret < module_addr_min)
1973 module_addr_min = (unsigned long)ret; 2073 module_addr_min = (unsigned long)ret;
1974 if ((unsigned long)ret + size > module_addr_max) 2074 if ((unsigned long)ret + size > module_addr_max)
1975 module_addr_max = (unsigned long)ret + size; 2075 module_addr_max = (unsigned long)ret + size;
2076 mutex_unlock(&module_mutex);
1976 } 2077 }
1977 return ret; 2078 return ret;
1978} 2079}
@@ -2022,6 +2123,7 @@ static noinline struct module *load_module(void __user *umod,
2022 long err = 0; 2123 long err = 0;
2023 void *ptr = NULL; /* Stops spurious gcc warning */ 2124 void *ptr = NULL; /* Stops spurious gcc warning */
2024 unsigned long symoffs, stroffs, *strmap; 2125 unsigned long symoffs, stroffs, *strmap;
2126 void __percpu *percpu;
2025 2127
2026 mm_segment_t old_fs; 2128 mm_segment_t old_fs;
2027 2129
@@ -2146,11 +2248,6 @@ static noinline struct module *load_module(void __user *umod,
2146 goto free_mod; 2248 goto free_mod;
2147 } 2249 }
2148 2250
2149 if (find_module(mod->name)) {
2150 err = -EEXIST;
2151 goto free_mod;
2152 }
2153
2154 mod->state = MODULE_STATE_COMING; 2251 mod->state = MODULE_STATE_COMING;
2155 2252
2156 /* Allow arches to frob section contents and sizes. */ 2253 /* Allow arches to frob section contents and sizes. */
@@ -2166,6 +2263,8 @@ static noinline struct module *load_module(void __user *umod,
2166 goto free_mod; 2263 goto free_mod;
2167 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC; 2264 sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
2168 } 2265 }
2266 /* Keep this around for failure path. */
2267 percpu = mod_percpu(mod);
2169 2268
2170 /* Determine total sizes, and put offsets in sh_entsize. For now 2269 /* Determine total sizes, and put offsets in sh_entsize. For now
2171 this is done generically; there doesn't appear to be any 2270 this is done generically; there doesn't appear to be any
@@ -2239,11 +2338,6 @@ static noinline struct module *load_module(void __user *umod,
2239 /* Now we've moved module, initialize linked lists, etc. */ 2338 /* Now we've moved module, initialize linked lists, etc. */
2240 module_unload_init(mod); 2339 module_unload_init(mod);
2241 2340
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 */ 2341 /* Set up license info based on the info section */
2248 set_license(mod, get_modinfo(sechdrs, infoindex, "license")); 2342 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2249 2343
@@ -2368,11 +2462,6 @@ static noinline struct module *load_module(void __user *umod,
2368 goto cleanup; 2462 goto cleanup;
2369 } 2463 }
2370 2464
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 */ 2465 /* Set up and sort exception table */
2377 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table", 2466 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2378 sizeof(*mod->extable), &mod->num_exentries); 2467 sizeof(*mod->extable), &mod->num_exentries);
@@ -2431,7 +2520,19 @@ static noinline struct module *load_module(void __user *umod,
2431 * function to insert in a way safe to concurrent readers. 2520 * function to insert in a way safe to concurrent readers.
2432 * The mutex protects against concurrent writers. 2521 * The mutex protects against concurrent writers.
2433 */ 2522 */
2523 mutex_lock(&module_mutex);
2524 if (find_module(mod->name)) {
2525 err = -EEXIST;
2526 goto unlock;
2527 }
2528
2529 /* Find duplicate symbols */
2530 err = verify_export_symbols(mod);
2531 if (err < 0)
2532 goto unlock;
2533
2434 list_add_rcu(&mod->list, &modules); 2534 list_add_rcu(&mod->list, &modules);
2535 mutex_unlock(&module_mutex);
2435 2536
2436 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL); 2537 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2437 if (err < 0) 2538 if (err < 0)
@@ -2440,6 +2541,7 @@ static noinline struct module *load_module(void __user *umod,
2440 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp); 2541 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2441 if (err < 0) 2542 if (err < 0)
2442 goto unlink; 2543 goto unlink;
2544
2443 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2545 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2444 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs); 2546 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2445 2547
@@ -2452,15 +2554,15 @@ static noinline struct module *load_module(void __user *umod,
2452 return mod; 2554 return mod;
2453 2555
2454 unlink: 2556 unlink:
2557 mutex_lock(&module_mutex);
2455 /* Unlink carefully: kallsyms could be walking list. */ 2558 /* Unlink carefully: kallsyms could be walking list. */
2456 list_del_rcu(&mod->list); 2559 list_del_rcu(&mod->list);
2560 unlock:
2561 mutex_unlock(&module_mutex);
2457 synchronize_sched(); 2562 synchronize_sched();
2458 module_arch_cleanup(mod); 2563 module_arch_cleanup(mod);
2459 cleanup: 2564 cleanup:
2460 free_modinfo(mod); 2565 free_modinfo(mod);
2461 kobject_del(&mod->mkobj.kobj);
2462 kobject_put(&mod->mkobj.kobj);
2463 free_unload:
2464 module_unload_free(mod); 2566 module_unload_free(mod);
2465#if defined(CONFIG_MODULE_UNLOAD) 2567#if defined(CONFIG_MODULE_UNLOAD)
2466 free_percpu(mod->refptr); 2568 free_percpu(mod->refptr);
@@ -2471,7 +2573,7 @@ static noinline struct module *load_module(void __user *umod,
2471 module_free(mod, mod->module_core); 2573 module_free(mod, mod->module_core);
2472 /* mod will be freed with core. Don't access it beyond this line! */ 2574 /* mod will be freed with core. Don't access it beyond this line! */
2473 free_percpu: 2575 free_percpu:
2474 percpu_modfree(mod); 2576 free_percpu(percpu);
2475 free_mod: 2577 free_mod:
2476 kfree(args); 2578 kfree(args);
2477 kfree(strmap); 2579 kfree(strmap);
@@ -2507,19 +2609,10 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2507 if (!capable(CAP_SYS_MODULE) || modules_disabled) 2609 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2508 return -EPERM; 2610 return -EPERM;
2509 2611
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 */ 2612 /* Do all the hard work */
2515 mod = load_module(umod, len, uargs); 2613 mod = load_module(umod, len, uargs);
2516 if (IS_ERR(mod)) { 2614 if (IS_ERR(mod))
2517 mutex_unlock(&module_mutex);
2518 return PTR_ERR(mod); 2615 return PTR_ERR(mod);
2519 }
2520
2521 /* Drop lock so they can recurse */
2522 mutex_unlock(&module_mutex);
2523 2616
2524 blocking_notifier_call_chain(&module_notify_list, 2617 blocking_notifier_call_chain(&module_notify_list,
2525 MODULE_STATE_COMING, mod); 2618 MODULE_STATE_COMING, mod);
@@ -2536,9 +2629,7 @@ SYSCALL_DEFINE3(init_module, void __user *, umod,
2536 module_put(mod); 2629 module_put(mod);
2537 blocking_notifier_call_chain(&module_notify_list, 2630 blocking_notifier_call_chain(&module_notify_list,
2538 MODULE_STATE_GOING, mod); 2631 MODULE_STATE_GOING, mod);
2539 mutex_lock(&module_mutex);
2540 free_module(mod); 2632 free_module(mod);
2541 mutex_unlock(&module_mutex);
2542 wake_up(&module_wq); 2633 wake_up(&module_wq);
2543 return ret; 2634 return ret;
2544 } 2635 }