Commit 9bea7f23 authored by Rusty Russell's avatar Rusty Russell

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: default avatarRusty Russell <rusty@rustcorp.com.au>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Tim Abbot <tabbott@ksplice.com>
Tested-by: default avatarBrandon Philips <bphilips@suse.de>
parent be593f4c
...@@ -582,33 +582,26 @@ static int add_module_usage(struct module *a, struct module *b) ...@@ -582,33 +582,26 @@ static int add_module_usage(struct module *a, struct module *b)
} }
/* Module a uses b: caller needs module_mutex() */ /* Module a uses b: caller needs module_mutex() */
int use_module(struct module *a, struct module *b) int ref_module(struct module *a, struct module *b)
{ {
int err; int err;
if (b == NULL || already_uses(a, b)) return 1; if (b == NULL || already_uses(a, b))
/* If we're interrupted or time out, we fail. */
if (wait_event_interruptible_timeout(
module_wq, (err = strong_try_module_get(b)) != -EBUSY,
30 * HZ) <= 0) {
printk("%s: gave up waiting for init of module %s.\n",
a->name, b->name);
return 0; return 0;
}
/* If strong_try_module_get() returned a different error, we fail. */ /* If module isn't available, we fail. */
err = strong_try_module_get(b);
if (err) if (err)
return 0; return err;
err = add_module_usage(a, b); err = add_module_usage(a, b);
if (err) { if (err) {
module_put(b); module_put(b);
return 0; return err;
} }
return 1; return 0;
} }
EXPORT_SYMBOL_GPL(use_module); EXPORT_SYMBOL_GPL(ref_module);
/* Clear the unload stuff of the module. */ /* Clear the unload stuff of the module. */
static void module_unload_free(struct module *mod) static void module_unload_free(struct module *mod)
...@@ -893,11 +886,11 @@ static inline void module_unload_free(struct module *mod) ...@@ -893,11 +886,11 @@ static inline void module_unload_free(struct module *mod)
{ {
} }
int use_module(struct module *a, struct module *b) int ref_module(struct module *a, struct module *b)
{ {
return strong_try_module_get(b) == 0; return strong_try_module_get(b);
} }
EXPORT_SYMBOL_GPL(use_module); EXPORT_SYMBOL_GPL(ref_module);
static inline void module_unload_init(struct module *mod) static inline void module_unload_init(struct module *mod)
{ {
...@@ -1062,26 +1055,58 @@ static inline int same_magic(const char *amagic, const char *bmagic, ...@@ -1062,26 +1055,58 @@ static inline int same_magic(const char *amagic, const char *bmagic,
static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs, static const struct kernel_symbol *resolve_symbol(Elf_Shdr *sechdrs,
unsigned int versindex, unsigned int versindex,
const char *name, const char *name,
struct module *mod) struct module *mod,
char ownername[])
{ {
struct module *owner; struct module *owner;
const struct kernel_symbol *sym; const struct kernel_symbol *sym;
const unsigned long *crc; const unsigned long *crc;
int err;
mutex_lock(&module_mutex); mutex_lock(&module_mutex);
sym = find_symbol(name, &owner, &crc, sym = find_symbol(name, &owner, &crc,
!(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true); !(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
/* use_module can fail due to OOM, if (!sym)
or module initialization or unloading */ goto unlock;
if (sym) {
if (!check_version(sechdrs, versindex, name, mod, crc, owner) if (!check_version(sechdrs, versindex, name, mod, crc, owner)) {
|| !use_module(mod, owner)) sym = ERR_PTR(-EINVAL);
sym = NULL; goto getname;
} }
err = ref_module(mod, owner);
if (err) {
sym = ERR_PTR(err);
goto getname;
}
getname:
/* We must make copy under the lock if we failed to get ref. */
strncpy(ownername, module_name(owner), MODULE_NAME_LEN);
unlock:
mutex_unlock(&module_mutex); mutex_unlock(&module_mutex);
return sym; return sym;
} }
static const struct kernel_symbol *resolve_symbol_wait(Elf_Shdr *sechdrs,
unsigned int versindex,
const char *name,
struct module *mod)
{
const struct kernel_symbol *ksym;
char ownername[MODULE_NAME_LEN];
if (wait_event_interruptible_timeout(module_wq,
!IS_ERR(ksym = resolve_symbol(sechdrs, versindex, name,
mod, ownername)) ||
PTR_ERR(ksym) != -EBUSY,
30 * HZ) <= 0) {
printk(KERN_WARNING "%s: gave up waiting for init of module %s.\n",
mod->name, ownername);
}
return ksym;
}
/* /*
* /sys/module/foo/sections stuff * /sys/module/foo/sections stuff
* J. Corbet <corbet@lwn.net> * J. Corbet <corbet@lwn.net>
...@@ -1638,21 +1663,23 @@ static int simplify_symbols(Elf_Shdr *sechdrs, ...@@ -1638,21 +1663,23 @@ static int simplify_symbols(Elf_Shdr *sechdrs,
break; break;
case SHN_UNDEF: case SHN_UNDEF:
ksym = resolve_symbol(sechdrs, versindex, ksym = resolve_symbol_wait(sechdrs, versindex,
strtab + sym[i].st_name, mod); strtab + sym[i].st_name,
mod);
/* Ok if resolved. */ /* Ok if resolved. */
if (ksym) { if (ksym && !IS_ERR(ksym)) {
sym[i].st_value = ksym->value; sym[i].st_value = ksym->value;
break; break;
} }
/* Ok if weak. */ /* Ok if weak. */
if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK) if (!ksym && ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
break; break;
printk(KERN_WARNING "%s: Unknown symbol %s\n", printk(KERN_WARNING "%s: Unknown symbol %s (err %li)\n",
mod->name, strtab + sym[i].st_name); mod->name, strtab + sym[i].st_name,
ret = -ENOENT; PTR_ERR(ksym));
ret = PTR_ERR(ksym) ?: -ENOENT;
break; break;
default: default:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment