Commit 90ec7819737d42a0ad1c2df1ff56016facae3c6e

Authored by Linus Torvalds

Merge git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus

* git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus:
  module: fix bne2 "gave up waiting for init of module libcrc32c"
  module: verify_export_symbols under the lock
  module: move find_module check to end
  module: make locking more fine-grained.
  module: Make module sysfs functions private.
  module: move sysfs exposure to end of load_module
  module: fix kdb's illicit use of struct module_use.
  module: Make the 'usage' lists be two-way

Showing 3 changed files Side-by-side Diff

include/linux/module.h
... ... @@ -181,6 +181,13 @@
181 181 void *__symbol_get_gpl(const char *symbol);
182 182 #define symbol_get(x) ((typeof(&x))(__symbol_get(MODULE_SYMBOL_PREFIX #x)))
183 183  
  184 +/* modules using other modules: kdb wants to see this. */
  185 +struct module_use {
  186 + struct list_head source_list;
  187 + struct list_head target_list;
  188 + struct module *source, *target;
  189 +};
  190 +
184 191 #ifndef __GENKSYMS__
185 192 #ifdef CONFIG_MODVERSIONS
186 193 /* Mark the CRC weak since genksyms apparently decides not to
... ... @@ -359,7 +366,9 @@
359 366  
360 367 #ifdef CONFIG_MODULE_UNLOAD
361 368 /* What modules depend on me? */
362   - struct list_head modules_which_use_me;
  369 + struct list_head source_list;
  370 + /* What modules do I depend on? */
  371 + struct list_head target_list;
363 372  
364 373 /* Who is waiting for us to be unloaded */
365 374 struct task_struct *waiter;
366 375  
367 376  
... ... @@ -663,43 +672,10 @@
663 672  
664 673 #endif /* CONFIG_MODULES */
665 674  
666   -struct device_driver;
667 675 #ifdef CONFIG_SYSFS
668   -struct module;
669   -
670 676 extern struct kset *module_kset;
671 677 extern struct kobj_type module_ktype;
672 678 extern int module_sysfs_initialized;
673   -
674   -int mod_sysfs_init(struct module *mod);
675   -int mod_sysfs_setup(struct module *mod,
676   - struct kernel_param *kparam,
677   - unsigned int num_params);
678   -int module_add_modinfo_attrs(struct module *mod);
679   -void module_remove_modinfo_attrs(struct module *mod);
680   -
681   -#else /* !CONFIG_SYSFS */
682   -
683   -static inline int mod_sysfs_init(struct module *mod)
684   -{
685   - return 0;
686   -}
687   -
688   -static inline int mod_sysfs_setup(struct module *mod,
689   - struct kernel_param *kparam,
690   - unsigned int num_params)
691   -{
692   - return 0;
693   -}
694   -
695   -static inline int module_add_modinfo_attrs(struct module *mod)
696   -{
697   - return 0;
698   -}
699   -
700   -static inline void module_remove_modinfo_attrs(struct module *mod)
701   -{ }
702   -
703 679 #endif /* CONFIG_SYSFS */
704 680  
705 681 #define symbol_request(x) try_then_request_module(symbol_get(x), "symbol:" #x)
kernel/debug/kdb/kdb_main.c
... ... @@ -1857,12 +1857,6 @@
1857 1857 }
1858 1858  
1859 1859 #if defined(CONFIG_MODULES)
1860   -/* modules using other modules */
1861   -struct module_use {
1862   - struct list_head list;
1863   - struct module *module_which_uses;
1864   -};
1865   -
1866 1860 /*
1867 1861 * kdb_lsmod - This function implements the 'lsmod' command. Lists
1868 1862 * currently loaded kernel modules.
... ... @@ -1894,9 +1888,9 @@
1894 1888 {
1895 1889 struct module_use *use;
1896 1890 kdb_printf(" [ ");
1897   - list_for_each_entry(use, &mod->modules_which_use_me,
1898   - list)
1899   - kdb_printf("%s ", use->module_which_uses->name);
  1891 + list_for_each_entry(use, &mod->source_list,
  1892 + source_list)
  1893 + kdb_printf("%s ", use->target->name);
1900 1894 kdb_printf("]\n");
1901 1895 }
1902 1896 #endif
... ... @@ -72,7 +72,11 @@
72 72 /* If this is set, the section belongs in the init part of the module */
73 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 80 * (delete uses stop_machine/add uses RCU list operations). */
77 81 DEFINE_MUTEX(module_mutex);
78 82 EXPORT_SYMBOL_GPL(module_mutex);
... ... @@ -90,7 +94,8 @@
90 94  
91 95 static 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. */
94 99 static unsigned long module_addr_min = -1UL, module_addr_max = 0;
95 100  
96 101 int register_module_notifier(struct notifier_block * nb)
... ... @@ -329,7 +334,7 @@
329 334 }
330 335  
331 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. */
333 338 const struct kernel_symbol *find_symbol(const char *name,
334 339 struct module **owner,
335 340 const unsigned long **crc,
... ... @@ -523,7 +528,8 @@
523 528 {
524 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 533 for_each_possible_cpu(cpu) {
528 534 per_cpu_ptr(mod->refptr, cpu)->incs = 0;
529 535 per_cpu_ptr(mod->refptr, cpu)->decs = 0;
530 536  
... ... @@ -535,20 +541,13 @@
535 541 mod->waiter = current;
536 542 }
537 543  
538   -/* modules using other modules */
539   -struct module_use
540   -{
541   - struct list_head list;
542   - struct module *module_which_uses;
543   -};
544   -
545 544 /* Does a already use b? */
546 545 static int already_uses(struct module *a, struct module *b)
547 546 {
548 547 struct module_use *use;
549 548  
550   - list_for_each_entry(use, &b->modules_which_use_me, list) {
551   - if (use->module_which_uses == a) {
  549 + list_for_each_entry(use, &b->source_list, source_list) {
  550 + if (use->source == a) {
552 551 DEBUGP("%s uses %s!\n", a->name, b->name);
553 552 return 1;
554 553 }
555 554  
556 555  
557 556  
558 557  
559 558  
560 559  
561 560  
562 561  
563 562  
564 563  
565 564  
566 565  
567 566  
... ... @@ -557,62 +556,68 @@
557 556 return 0;
558 557 }
559 558  
560   -/* Module a uses b */
561   -int use_module(struct module *a, struct module *b)
  559 +/*
  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 + */
  566 +static int add_module_usage(struct module *a, struct module *b)
562 567 {
563 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. */
569   - if (wait_event_interruptible_timeout(
570   - module_wq, (err = strong_try_module_get(b)) != -EBUSY,
571   - 30 * HZ) <= 0) {
572   - printk("%s: gave up waiting for init of module %s.\n",
573   - a->name, b->name);
  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() */
  585 +int ref_module(struct module *a, struct module *b)
  586 +{
  587 + int err;
  588 +
  589 + if (b == NULL || already_uses(a, b))
574 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 594 if (err)
579   - return 0;
  595 + return err;
580 596  
581   - DEBUGP("Allocating new usage for %s.\n", a->name);
582   - use = kmalloc(sizeof(*use), GFP_ATOMIC);
583   - if (!use) {
584   - printk("%s: out of memory loading\n", a->name);
  597 + err = add_module_usage(a, b);
  598 + if (err) {
585 599 module_put(b);
586   - return 0;
  600 + return err;
587 601 }
588   -
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;
  602 + return 0;
593 603 }
594   -EXPORT_SYMBOL_GPL(use_module);
  604 +EXPORT_SYMBOL_GPL(ref_module);
595 605  
596 606 /* Clear the unload stuff of the module. */
597 607 static 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) {
602   - struct module_use *use;
603   -
604   - list_for_each_entry(use, &i->modules_which_use_me, list) {
605   - if (use->module_which_uses == mod) {
606   - DEBUGP("%s unusing %s\n", mod->name, i->name);
607   - module_put(i);
608   - list_del(&use->list);
609   - kfree(use);
610   - sysfs_remove_link(i->holders_dir, mod->name);
611   - /* There can be at most one match. */
612   - break;
613   - }
614   - }
  611 + mutex_lock(&module_mutex);
  612 + list_for_each_entry_safe(use, tmp, &mod->target_list, target_list) {
  613 + struct module *i = use->target;
  614 + DEBUGP("%s unusing %s\n", mod->name, i->name);
  615 + module_put(i);
  616 + list_del(&use->source_list);
  617 + list_del(&use->target_list);
  618 + kfree(use);
615 619 }
  620 + mutex_unlock(&module_mutex);
616 621 }
617 622  
618 623 #ifdef CONFIG_MODULE_FORCE_UNLOAD
... ... @@ -735,7 +740,7 @@
735 740 goto out;
736 741 }
737 742  
738   - if (!list_empty(&mod->modules_which_use_me)) {
  743 + if (!list_empty(&mod->source_list)) {
739 744 /* Other modules depend on us: get rid of them first. */
740 745 ret = -EWOULDBLOCK;
741 746 goto out;
742 747  
743 748  
... ... @@ -779,13 +784,14 @@
779 784 blocking_notifier_call_chain(&module_notify_list,
780 785 MODULE_STATE_GOING, mod);
781 786 async_synchronize_full();
782   - mutex_lock(&module_mutex);
  787 +
783 788 /* Store the name of the last unloaded module for diagnostic purposes */
784 789 strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module));
785 790 ddebug_remove_module(mod->name);
786   - free_module(mod);
787 791  
788   - out:
  792 + free_module(mod);
  793 + return 0;
  794 +out:
789 795 mutex_unlock(&module_mutex);
790 796 return ret;
791 797 }
792 798  
... ... @@ -799,9 +805,9 @@
799 805  
800 806 /* Always include a trailing , so userspace can differentiate
801 807 between this and the old multi-field proc format. */
802   - list_for_each_entry(use, &mod->modules_which_use_me, list) {
  808 + list_for_each_entry(use, &mod->source_list, source_list) {
803 809 printed_something = 1;
804   - seq_printf(m, "%s,", use->module_which_uses->name);
  810 + seq_printf(m, "%s,", use->source->name);
805 811 }
806 812  
807 813 if (mod->init != NULL && mod->exit == NULL) {
808 814  
809 815  
... ... @@ -880,11 +886,11 @@
880 886 {
881 887 }
882 888  
883   -int use_module(struct module *a, struct module *b)
  889 +int ref_module(struct module *a, struct module *b)
884 890 {
885   - return strong_try_module_get(b) == 0;
  891 + return strong_try_module_get(b);
886 892 }
887   -EXPORT_SYMBOL_GPL(use_module);
  893 +EXPORT_SYMBOL_GPL(ref_module);
888 894  
889 895 static inline void module_unload_init(struct module *mod)
890 896 {
... ... @@ -1001,6 +1007,8 @@
1001 1007 {
1002 1008 const unsigned long *crc;
1003 1009  
  1010 + /* Since this should be found in kernel (which can't be removed),
  1011 + * no locking is necessary. */
1004 1012 if (!find_symbol(MODULE_SYMBOL_PREFIX "module_layout", NULL,
1005 1013 &crc, true, false))
1006 1014 BUG();
1007 1015  
1008 1016  
1009 1017  
1010 1018  
1011 1019  
1012 1020  
... ... @@ -1043,29 +1051,62 @@
1043 1051 }
1044 1052 #endif /* CONFIG_MODVERSIONS */
1045 1053  
1046   -/* Resolve a symbol for this module. I.e. if we find one, record usage.
1047   - Must be holding module_mutex. */
  1054 +/* Resolve a symbol for this module. I.e. if we find one, record usage. */
1048 1055 static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
1049 1056 unsigned int versindex,
1050 1057 const char *name,
1051   - struct module *mod)
  1058 + struct module *mod,
  1059 + char ownername[])
1052 1060 {
1053 1061 struct module *owner;
1054 1062 const struct kernel_symbol *sym;
1055 1063 const unsigned long *crc;
  1064 + int err;
1056 1065  
  1066 + mutex_lock(&module_mutex);
1057 1067 sym = find_symbol(name, &owner, &crc,
1058 1068 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
1059   - /* use_module can fail due to OOM,
1060   - or module initialization or unloading */
1061   - if (sym) {
1062   - if (!check_version(sechdrs, versindex, name, mod, crc, owner)
1063   - || !use_module(mod, owner))
1064   - sym = NULL;
  1069 + if (!sym)
  1070 + goto unlock;
  1071 +
  1072 + if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
  1073 + sym = ERR_PTR(-EINVAL);
  1074 + goto getname;
1065 1075 }
  1076 +
  1077 + err = ref_module(mod, owner);
  1078 + if (err) {
  1079 + sym = ERR_PTR(err);
  1080 + goto getname;
  1081 + }
  1082 +
  1083 +getname:
  1084 + /* We must make copy under the lock if we failed to get ref. */
  1085 + strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
  1086 +unlock:
  1087 + mutex_unlock(&module_mutex);
1066 1088 return sym;
1067 1089 }
1068 1090  
  1091 +static 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 +
1069 1110 /*
1070 1111 * /sys/module/foo/sections stuff
1071 1112 * J. Corbet <corbet@lwn.net>
1072 1113  
... ... @@ -1295,8 +1336,35 @@
1295 1336 #endif
1296 1337  
1297 1338 #ifdef CONFIG_SYSFS
1298   -int module_add_modinfo_attrs(struct module *mod)
  1339 +static void add_usage_links(struct module *mod)
1299 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 +
  1354 +static 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 +
  1366 +static int module_add_modinfo_attrs(struct module *mod)
  1367 +{
1300 1368 struct module_attribute *attr;
1301 1369 struct module_attribute *temp_attr;
1302 1370 int error = 0;
... ... @@ -1321,7 +1389,7 @@
1321 1389 return error;
1322 1390 }
1323 1391  
1324   -void module_remove_modinfo_attrs(struct module *mod)
  1392 +static void module_remove_modinfo_attrs(struct module *mod)
1325 1393 {
1326 1394 struct module_attribute *attr;
1327 1395 int i;
... ... @@ -1337,7 +1405,7 @@
1337 1405 kfree(mod->modinfo_attrs);
1338 1406 }
1339 1407  
1340   -int mod_sysfs_init(struct module *mod)
  1408 +static int mod_sysfs_init(struct module *mod)
1341 1409 {
1342 1410 int err;
1343 1411 struct kobject *kobj;
1344 1412  
... ... @@ -1371,12 +1439,16 @@
1371 1439 return err;
1372 1440 }
1373 1441  
1374   -int mod_sysfs_setup(struct module *mod,
  1442 +static int mod_sysfs_setup(struct module *mod,
1375 1443 struct kernel_param *kparam,
1376 1444 unsigned int num_params)
1377 1445 {
1378 1446 int err;
1379 1447  
  1448 + err = mod_sysfs_init(mod);
  1449 + if (err)
  1450 + goto out;
  1451 +
1380 1452 mod->holders_dir = kobject_create_and_add("holders", &mod->mkobj.kobj);
1381 1453 if (!mod->holders_dir) {
1382 1454 err = -ENOMEM;
... ... @@ -1391,6 +1463,8 @@
1391 1463 if (err)
1392 1464 goto out_unreg_param;
1393 1465  
  1466 + add_usage_links(mod);
  1467 +
1394 1468 kobject_uevent(&mod->mkobj.kobj, KOBJ_ADD);
1395 1469 return 0;
1396 1470  
... ... @@ -1400,6 +1474,7 @@
1400 1474 kobject_put(mod->holders_dir);
1401 1475 out_unreg:
1402 1476 kobject_put(&mod->mkobj.kobj);
  1477 +out:
1403 1478 return err;
1404 1479 }
1405 1480  
1406 1481  
1407 1482  
... ... @@ -1410,14 +1485,40 @@
1410 1485  
1411 1486 #else /* CONFIG_SYSFS */
1412 1487  
  1488 +static inline int mod_sysfs_init(struct module *mod)
  1489 +{
  1490 + return 0;
  1491 +}
  1492 +
  1493 +static inline int mod_sysfs_setup(struct module *mod,
  1494 + struct kernel_param *kparam,
  1495 + unsigned int num_params)
  1496 +{
  1497 + return 0;
  1498 +}
  1499 +
  1500 +static inline int module_add_modinfo_attrs(struct module *mod)
  1501 +{
  1502 + return 0;
  1503 +}
  1504 +
  1505 +static inline void module_remove_modinfo_attrs(struct module *mod)
  1506 +{
  1507 +}
  1508 +
1413 1509 static void mod_sysfs_fini(struct module *mod)
1414 1510 {
1415 1511 }
1416 1512  
  1513 +static void del_usage_links(struct module *mod)
  1514 +{
  1515 +}
  1516 +
1417 1517 #endif /* CONFIG_SYSFS */
1418 1518  
1419 1519 static void mod_kobject_remove(struct module *mod)
1420 1520 {
  1521 + del_usage_links(mod);
1421 1522 module_remove_modinfo_attrs(mod);
1422 1523 module_param_sysfs_remove(mod);
1423 1524 kobject_put(mod->mkobj.drivers_dir);
1424 1525  
1425 1526  
... ... @@ -1436,13 +1537,15 @@
1436 1537 return 0;
1437 1538 }
1438 1539  
1439   -/* Free a module, remove from lists, etc (must hold module_mutex). */
  1540 +/* Free a module, remove from lists, etc. */
1440 1541 static void free_module(struct module *mod)
1441 1542 {
1442 1543 trace_module_free(mod);
1443 1544  
1444 1545 /* Delete from various lists */
  1546 + mutex_lock(&module_mutex);
1445 1547 stop_machine(__unlink_module, mod, NULL);
  1548 + mutex_unlock(&module_mutex);
1446 1549 remove_notes_attrs(mod);
1447 1550 remove_sect_attrs(mod);
1448 1551 mod_kobject_remove(mod);
... ... @@ -1493,6 +1596,8 @@
1493 1596 /*
1494 1597 * Ensure that an exported symbol [global namespace] does not already exist
1495 1598 * in the kernel or in some other module's exported symbol table.
  1599 + *
  1600 + * You must hold the module_mutex.
1496 1601 */
1497 1602 static int verify_export_symbols(struct module *mod)
1498 1603 {
1499 1604  
1500 1605  
1501 1606  
... ... @@ -1558,21 +1663,23 @@
1558 1663 break;
1559 1664  
1560 1665 case SHN_UNDEF:
1561   - ksym = resolve_symbol(sechdrs, versindex,
1562   - strtab + sym[i].st_name, mod);
  1666 + ksym = resolve_symbol_wait(sechdrs, versindex,
  1667 + strtab + sym[i].st_name,
  1668 + mod);
1563 1669 /* Ok if resolved. */
1564   - if (ksym) {
  1670 + if (ksym && !IS_ERR(ksym)) {
1565 1671 sym[i].st_value = ksym->value;
1566 1672 break;
1567 1673 }
1568 1674  
1569 1675 /* Ok if weak. */
1570   - if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1676 + if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
1571 1677 break;
1572 1678  
1573   - printk(KERN_WARNING "%s: Unknown symbol %s\n",
1574   - mod->name, strtab + sym[i].st_name);
1575   - ret = -ENOENT;
  1679 + printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
  1680 + mod->name, strtab + sym[i].st_name,
  1681 + PTR_ERR(ksym));
  1682 + ret = PTR_ERR(ksym) ?: -ENOENT;
1576 1683 break;
1577 1684  
1578 1685 default:
1579 1686  
... ... @@ -1960,11 +2067,13 @@
1960 2067 void *ret = module_alloc(size);
1961 2068  
1962 2069 if (ret) {
  2070 + mutex_lock(&module_mutex);
1963 2071 /* Update module bounds. */
1964 2072 if ((unsigned long)ret < module_addr_min)
1965 2073 module_addr_min = (unsigned long)ret;
1966 2074 if ((unsigned long)ret + size > module_addr_max)
1967 2075 module_addr_max = (unsigned long)ret + size;
  2076 + mutex_unlock(&module_mutex);
1968 2077 }
1969 2078 return ret;
1970 2079 }
... ... @@ -2139,11 +2248,6 @@
2139 2248 goto free_mod;
2140 2249 }
2141 2250  
2142   - if (find_module(mod->name)) {
2143   - err = -EEXIST;
2144   - goto free_mod;
2145   - }
2146   -
2147 2251 mod->state = MODULE_STATE_COMING;
2148 2252  
2149 2253 /* Allow arches to frob section contents and sizes. */
... ... @@ -2234,11 +2338,6 @@
2234 2338 /* Now we've moved module, initialize linked lists, etc. */
2235 2339 module_unload_init(mod);
2236 2340  
2237   - /* add kobject, so we can reference it. */
2238   - err = mod_sysfs_init(mod);
2239   - if (err)
2240   - goto free_unload;
2241   -
2242 2341 /* Set up license info based on the info section */
2243 2342 set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
2244 2343  
... ... @@ -2363,11 +2462,6 @@
2363 2462 goto cleanup;
2364 2463 }
2365 2464  
2366   - /* Find duplicate symbols */
2367   - err = verify_export_symbols(mod);
2368   - if (err < 0)
2369   - goto cleanup;
2370   -
2371 2465 /* Set up and sort exception table */
2372 2466 mod->extable = section_objs(hdr, sechdrs, secstrings, "__ex_table",
2373 2467 sizeof(*mod->extable), &mod->num_exentries);
2374 2468  
... ... @@ -2426,7 +2520,19 @@
2426 2520 * function to insert in a way safe to concurrent readers.
2427 2521 * The mutex protects against concurrent writers.
2428 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 +
2429 2534 list_add_rcu(&mod->list, &modules);
  2535 + mutex_unlock(&module_mutex);
2430 2536  
2431 2537 err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, NULL);
2432 2538 if (err < 0)
... ... @@ -2435,6 +2541,7 @@
2435 2541 err = mod_sysfs_setup(mod, mod->kp, mod->num_kp);
2436 2542 if (err < 0)
2437 2543 goto unlink;
  2544 +
2438 2545 add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2439 2546 add_notes_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
2440 2547  
2441 2548  
2442 2549  
... ... @@ -2447,15 +2554,15 @@
2447 2554 return mod;
2448 2555  
2449 2556 unlink:
  2557 + mutex_lock(&module_mutex);
2450 2558 /* Unlink carefully: kallsyms could be walking list. */
2451 2559 list_del_rcu(&mod->list);
  2560 + unlock:
  2561 + mutex_unlock(&module_mutex);
2452 2562 synchronize_sched();
2453 2563 module_arch_cleanup(mod);
2454 2564 cleanup:
2455 2565 free_modinfo(mod);
2456   - kobject_del(&mod->mkobj.kobj);
2457   - kobject_put(&mod->mkobj.kobj);
2458   - free_unload:
2459 2566 module_unload_free(mod);
2460 2567 #if defined(CONFIG_MODULE_UNLOAD)
2461 2568 free_percpu(mod->refptr);
2462 2569  
2463 2570  
2464 2571  
... ... @@ -2502,20 +2609,11 @@
2502 2609 if (!capable(CAP_SYS_MODULE) || modules_disabled)
2503 2610 return -EPERM;
2504 2611  
2505   - /* Only one module load at a time, please */
2506   - if (mutex_lock_interruptible(&module_mutex) != 0)
2507   - return -EINTR;
2508   -
2509 2612 /* Do all the hard work */
2510 2613 mod = load_module(umod, len, uargs);
2511   - if (IS_ERR(mod)) {
2512   - mutex_unlock(&module_mutex);
  2614 + if (IS_ERR(mod))
2513 2615 return PTR_ERR(mod);
2514   - }
2515 2616  
2516   - /* Drop lock so they can recurse */
2517   - mutex_unlock(&module_mutex);
2518   -
2519 2617 blocking_notifier_call_chain(&module_notify_list,
2520 2618 MODULE_STATE_COMING, mod);
2521 2619  
2522 2620  
... ... @@ -2531,9 +2629,7 @@
2531 2629 module_put(mod);
2532 2630 blocking_notifier_call_chain(&module_notify_list,
2533 2631 MODULE_STATE_GOING, mod);
2534   - mutex_lock(&module_mutex);
2535 2632 free_module(mod);
2536   - mutex_unlock(&module_mutex);
2537 2633 wake_up(&module_wq);
2538 2634 return ret;
2539 2635 }