module: fix bne2 "gave up waiting for init of module libcrc32c"

Problem: it's hard to avoid an init routine stumbling over a
request_module these days. And it's not clear it's always a bad idea:
for example, a module like kvm with dynamic dependencies on kvm-intel
or kvm-amd would be neater if it could simply request_module the right
one.

In this particular case, it's libcrc32c:

libcrc32c_mod_init
crypto_alloc_shash
crypto_alloc_tfm
crypto_find_alg
crypto_alg_mod_lookup
crypto_larval_lookup
request_module

If another module is waiting inside resolve_symbol() for libcrc32c to
finish initializing (ie. bne2 depends on libcrc32c) then it does so
holding the module lock, and our request_module() can't make progress
until that is released.

Waiting inside resolve_symbol() without the lock isn't all that hard:
we just need to pass the -EBUSY up the call chain so we can sleep
where we don't hold the lock. Error reporting is a bit trickier: we
need to copy the name of the unfinished module before releasing the
lock.

Other notes:
1) This also fixes a theoretical issue where a weak dependency would allow
symbol version mismatches to be ignored.
2) We rename use_module to ref_module to make life easier for the only
external user (the out-of-tree ksplice patches).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Tim Abbot <tabbott@ksplice.com>
Tested-by: Brandon Philips <bphilips@suse.de>

+59 -32
+59 -32
kernel/module.c
··· 582 } 583 584 /* Module a uses b: caller needs module_mutex() */ 585 - int use_module(struct module *a, struct module *b) 586 { 587 int err; 588 589 - if (b == NULL || already_uses(a, b)) return 1; 590 - 591 - /* If we're interrupted or time out, we fail. */ 592 - if (wait_event_interruptible_timeout( 593 - module_wq, (err = strong_try_module_get(b)) != -EBUSY, 594 - 30 * HZ) <= 0) { 595 - printk("%s: gave up waiting for init of module %s.\n", 596 - a->name, b->name); 597 return 0; 598 - } 599 600 - /* If strong_try_module_get() returned a different error, we fail. */ 601 if (err) 602 - return 0; 603 604 err = add_module_usage(a, b); 605 if (err) { 606 module_put(b); 607 - return 0; 608 } 609 - return 1; 610 } 611 - EXPORT_SYMBOL_GPL(use_module); 612 613 /* Clear the unload stuff of the module. */ 614 static void module_unload_free(struct module *mod) ··· 886 { 887 } 888 889 - int use_module(struct module *a, struct module *b) 890 { 891 - return strong_try_module_get(b) == 0; 892 } 893 - EXPORT_SYMBOL_GPL(use_module); 894 895 static inline void module_unload_init(struct module *mod) 896 { ··· 1055 static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, 1056 unsigned int versindex, 1057 const char *name, 1058 - struct module *mod) 1059 { 1060 struct module *owner; 1061 const struct kernel_symbol *sym; 1062 const unsigned long *crc; 1063 1064 mutex_lock(&module_mutex); 1065 sym = find_symbol(name, &owner, &crc, 1066 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1067 - /* use_module can fail due to OOM, 1068 - or module initialization or unloading */ 1069 - if (sym) { 1070 - if (!check_version(sechdrs, versindex, name, mod, crc, owner) 1071 - || !use_module(mod, owner)) 1072 - sym = NULL; 1073 } 1074 mutex_unlock(&module_mutex); 1075 return sym; 1076 } 1077 1078 /* ··· 1663 break; 1664 1665 case SHN_UNDEF: 1666 - ksym = resolve_symbol(sechdrs, versindex, 1667 - strtab + sym[i].st_name, mod); 1668 /* Ok if resolved. */ 1669 - if (ksym) { 1670 sym[i].st_value = ksym->value; 1671 break; 1672 } 1673 1674 /* Ok if weak. */ 1675 - if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1676 break; 1677 1678 - printk(KERN_WARNING "%s: Unknown symbol %s\n", 1679 - mod->name, strtab + sym[i].st_name); 1680 - ret = -ENOENT; 1681 break; 1682 1683 default:
··· 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)) 590 return 0; 591 592 + /* If module isn't available, we fail. */ 593 + err = strong_try_module_get(b); 594 if (err) 595 + return err; 596 597 err = add_module_usage(a, b); 598 if (err) { 599 module_put(b); 600 + return err; 601 } 602 + return 0; 603 } 604 + EXPORT_SYMBOL_GPL(ref_module); 605 606 /* Clear the unload stuff of the module. */ 607 static void module_unload_free(struct module *mod) ··· 893 { 894 } 895 896 + int ref_module(struct module *a, struct module *b) 897 { 898 + return strong_try_module_get(b); 899 } 900 + EXPORT_SYMBOL_GPL(ref_module); 901 902 static inline void module_unload_init(struct module *mod) 903 { ··· 1062 static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, 1063 unsigned int versindex, 1064 const char *name, 1065 + struct module *mod, 1066 + char ownername[]) 1067 { 1068 struct module *owner; 1069 const struct kernel_symbol *sym; 1070 const unsigned long *crc; 1071 + int err; 1072 1073 mutex_lock(&module_mutex); 1074 sym = find_symbol(name, &owner, &crc, 1075 !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); 1076 + if (!sym) 1077 + goto unlock; 1078 + 1079 + if (!check_version(sechdrs, versindex, name, mod, crc, owner)) { 1080 + sym = ERR_PTR(-EINVAL); 1081 + goto getname; 1082 } 1083 + 1084 + err = ref_module(mod, owner); 1085 + if (err) { 1086 + sym = ERR_PTR(err); 1087 + goto getname; 1088 + } 1089 + 1090 + getname: 1091 + /* We must make copy under the lock if we failed to get ref. */ 1092 + strncpy(ownername, module_name(owner), MODULE_NAME_LEN); 1093 + unlock: 1094 mutex_unlock(&module_mutex); 1095 return sym; 1096 + } 1097 + 1098 + static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs, 1099 + unsigned int versindex, 1100 + const char *name, 1101 + struct module *mod) 1102 + { 1103 + const struct kernel_symbol *ksym; 1104 + char ownername[MODULE_NAME_LEN]; 1105 + 1106 + if (wait_event_interruptible_timeout(module_wq, 1107 + !IS_ERR(ksym = resolve_symbol(sechdrs, versindex, name, 1108 + mod, ownername)) || 1109 + PTR_ERR(ksym) != -EBUSY, 1110 + 30 * HZ) <= 0) { 1111 + printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n", 1112 + mod->name, ownername); 1113 + } 1114 + return ksym; 1115 } 1116 1117 /* ··· 1638 break; 1639 1640 case SHN_UNDEF: 1641 + ksym = resolve_symbol_wait(sechdrs, versindex, 1642 + strtab + sym[i].st_name, 1643 + mod); 1644 /* Ok if resolved. */ 1645 + if (ksym && !IS_ERR(ksym)) { 1646 sym[i].st_value = ksym->value; 1647 break; 1648 } 1649 1650 /* Ok if weak. */ 1651 + if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK) 1652 break; 1653 1654 + printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n", 1655 + mod->name, strtab + sym[i].st_name, 1656 + PTR_ERR(ksym)); 1657 + ret = PTR_ERR(ksym) ?: -ENOENT; 1658 break; 1659 1660 default: