Skip to content

Commit a3535c7

Browse files
committed
module: clean up load_module a little more.
1fb9341 made our locking in load_module more complicated: we grab the mutex once to insert the module in the list, then again to upgrade it once it's formed. Since the locking is self-contained, it's neater to do this in separate functions. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent f2e207f commit a3535c7

1 file changed

Lines changed: 68 additions & 39 deletions

File tree

‎kernel/module.c‎

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3145,12 +3145,72 @@ static int may_init_module(void)
31453145
return 0;
31463146
}
31473147

3148+
/*
3149+
* We try to place it in the list now to make sure it's unique before
3150+
* we dedicate too many resources. In particular, temporary percpu
3151+
* memory exhaustion.
3152+
*/
3153+
static int add_unformed_module(struct module *mod)
3154+
{
3155+
int err;
3156+
struct module *old;
3157+
3158+
mod->state = MODULE_STATE_UNFORMED;
3159+
3160+
again:
3161+
mutex_lock(&module_mutex);
3162+
if ((old = find_module_all(mod->name, true)) != NULL) {
3163+
if (old->state == MODULE_STATE_COMING
3164+
|| old->state == MODULE_STATE_UNFORMED) {
3165+
/* Wait in case it fails to load. */
3166+
mutex_unlock(&module_mutex);
3167+
err = wait_event_interruptible(module_wq,
3168+
finished_loading(mod->name));
3169+
if (err)
3170+
goto out_unlocked;
3171+
goto again;
3172+
}
3173+
err = -EEXIST;
3174+
goto out;
3175+
}
3176+
list_add_rcu(&mod->list, &modules);
3177+
err = 0;
3178+
3179+
out:
3180+
mutex_unlock(&module_mutex);
3181+
out_unlocked:
3182+
return err;
3183+
}
3184+
3185+
static int complete_formation(struct module *mod, struct load_info *info)
3186+
{
3187+
int err;
3188+
3189+
mutex_lock(&module_mutex);
3190+
3191+
/* Find duplicate symbols (must be called under lock). */
3192+
err = verify_export_symbols(mod);
3193+
if (err < 0)
3194+
goto out;
3195+
3196+
/* This relies on module_mutex for list integrity. */
3197+
module_bug_finalize(info->hdr, info->sechdrs, mod);
3198+
3199+
/* Mark state as coming so strong_try_module_get() ignores us,
3200+
* but kallsyms etc. can see us. */
3201+
mod->state = MODULE_STATE_COMING;
3202+
3203+
out:
3204+
mutex_unlock(&module_mutex);
3205+
return err;
3206+
}
3207+
31483208
/* Allocate and load the module: note that size of section 0 is always
31493209
zero, and we rely on this for optional sections. */
31503210
static int load_module(struct load_info *info, const char __user *uargs,
31513211
int flags)
31523212
{
3153-
struct module *mod, *old;
3213+
struct module *mod;
31543214
long err;
31553215

31563216
err = module_sig_check(info);
@@ -3168,31 +3228,10 @@ static int load_module(struct load_info *info, const char __user *uargs,
31683228
goto free_copy;
31693229
}
31703230

3171-
/*
3172-
* We try to place it in the list now to make sure it's unique
3173-
* before we dedicate too many resources. In particular,
3174-
* temporary percpu memory exhaustion.
3175-
*/
3176-
mod->state = MODULE_STATE_UNFORMED;
3177-
again:
3178-
mutex_lock(&module_mutex);
3179-
if ((old = find_module_all(mod->name, true)) != NULL) {
3180-
if (old->state == MODULE_STATE_COMING
3181-
|| old->state == MODULE_STATE_UNFORMED) {
3182-
/* Wait in case it fails to load. */
3183-
mutex_unlock(&module_mutex);
3184-
err = wait_event_interruptible(module_wq,
3185-
finished_loading(mod->name));
3186-
if (err)
3187-
goto free_module;
3188-
goto again;
3189-
}
3190-
err = -EEXIST;
3191-
mutex_unlock(&module_mutex);
3231+
/* Reserve our place in the list. */
3232+
err = add_unformed_module(mod);
3233+
if (err)
31923234
goto free_module;
3193-
}
3194-
list_add_rcu(&mod->list, &modules);
3195-
mutex_unlock(&module_mutex);
31963235

31973236
#ifdef CONFIG_MODULE_SIG
31983237
mod->sig_ok = info->sig_ok;
@@ -3245,21 +3284,11 @@ static int load_module(struct load_info *info, const char __user *uargs,
32453284

32463285
dynamic_debug_setup(info->debug, info->num_debug);
32473286

3248-
mutex_lock(&module_mutex);
3249-
/* Find duplicate symbols (must be called under lock). */
3250-
err = verify_export_symbols(mod);
3251-
if (err < 0)
3287+
/* Finally it's fully formed, ready to start executing. */
3288+
err = complete_formation(mod, info);
3289+
if (err)
32523290
goto ddebug_cleanup;
32533291

3254-
/* This relies on module_mutex for list integrity. */
3255-
module_bug_finalize(info->hdr, info->sechdrs, mod);
3256-
3257-
/* Mark state as coming so strong_try_module_get() ignores us,
3258-
* but kallsyms etc. can see us. */
3259-
mod->state = MODULE_STATE_COMING;
3260-
3261-
mutex_unlock(&module_mutex);
3262-
32633292
/* Module is ready to execute: parsing args may do that. */
32643293
err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp,
32653294
-32768, 32767, &ddebug_dyndbg_module_param_cb);
@@ -3283,8 +3312,8 @@ static int load_module(struct load_info *info, const char __user *uargs,
32833312
/* module_bug_cleanup needs module_mutex protection */
32843313
mutex_lock(&module_mutex);
32853314
module_bug_cleanup(mod);
3286-
ddebug_cleanup:
32873315
mutex_unlock(&module_mutex);
3316+
ddebug_cleanup:
32883317
dynamic_debug_remove(info->debug);
32893318
synchronize_sched();
32903319
kfree(mod->args);

0 commit comments

Comments
 (0)