diff options
author | Rusty Russell <rusty@rustcorp.com.au> | 2013-01-21 01:48:59 -0500 |
---|---|---|
committer | Rusty Russell <rusty@rustcorp.com.au> | 2013-01-21 01:50:09 -0500 |
commit | a3535c7e4f4495fe947f7901d25447d80e04fe52 (patch) | |
tree | 1a1f78c2b6cc07561b624e7443dc6493b5b8e5e3 /kernel/module.c | |
parent | f2e207f32422c7b9624d3393db015dfd118d9d22 (diff) |
module: clean up load_module a little more.
1fb9341ac34825aa40354e74d9a2c69df7d2c304 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>
Diffstat (limited to 'kernel/module.c')
-rw-r--r-- | kernel/module.c | 107 |
1 files changed, 68 insertions, 39 deletions
diff --git a/kernel/module.c b/kernel/module.c index cc000dd6e4a8..921bed4794e9 100644 --- a/kernel/module.c +++ b/kernel/module.c | |||
@@ -3145,12 +3145,72 @@ static int may_init_module(void) | |||
3145 | return 0; | 3145 | return 0; |
3146 | } | 3146 | } |
3147 | 3147 | ||
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 | |||
3148 | /* Allocate and load the module: note that size of section 0 is always | 3208 | /* Allocate and load the module: note that size of section 0 is always |
3149 | zero, and we rely on this for optional sections. */ | 3209 | zero, and we rely on this for optional sections. */ |
3150 | static int load_module(struct load_info *info, const char __user *uargs, | 3210 | static int load_module(struct load_info *info, const char __user *uargs, |
3151 | int flags) | 3211 | int flags) |
3152 | { | 3212 | { |
3153 | struct module *mod, *old; | 3213 | struct module *mod; |
3154 | long err; | 3214 | long err; |
3155 | 3215 | ||
3156 | err = module_sig_check(info); | 3216 | err = module_sig_check(info); |
@@ -3168,31 +3228,10 @@ static int load_module(struct load_info *info, const char __user *uargs, | |||
3168 | goto free_copy; | 3228 | goto free_copy; |
3169 | } | 3229 | } |
3170 | 3230 | ||
3171 | /* | 3231 | /* Reserve our place in the list. */ |
3172 | * We try to place it in the list now to make sure it's unique | 3232 | err = add_unformed_module(mod); |
3173 | * before we dedicate too many resources. In particular, | 3233 | if (err) |
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); | ||
3192 | goto free_module; | 3234 | goto free_module; |
3193 | } | ||
3194 | list_add_rcu(&mod->list, &modules); | ||
3195 | mutex_unlock(&module_mutex); | ||
3196 | 3235 | ||
3197 | #ifdef CONFIG_MODULE_SIG | 3236 | #ifdef CONFIG_MODULE_SIG |
3198 | mod->sig_ok = info->sig_ok; | 3237 | mod->sig_ok = info->sig_ok; |
@@ -3245,21 +3284,11 @@ again: | |||
3245 | 3284 | ||
3246 | dynamic_debug_setup(info->debug, info->num_debug); | 3285 | dynamic_debug_setup(info->debug, info->num_debug); |
3247 | 3286 | ||
3248 | mutex_lock(&module_mutex); | 3287 | /* Finally it's fully formed, ready to start executing. */ |
3249 | /* Find duplicate symbols (must be called under lock). */ | 3288 | err = complete_formation(mod, info); |
3250 | err = verify_export_symbols(mod); | 3289 | if (err) |
3251 | if (err < 0) | ||
3252 | goto ddebug_cleanup; | 3290 | goto ddebug_cleanup; |
3253 | 3291 | ||
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 | |||
3263 | /* Module is ready to execute: parsing args may do that. */ | 3292 | /* Module is ready to execute: parsing args may do that. */ |
3264 | err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, | 3293 | err = parse_args(mod->name, mod->args, mod->kp, mod->num_kp, |
3265 | -32768, 32767, &ddebug_dyndbg_module_param_cb); | 3294 | -32768, 32767, &ddebug_dyndbg_module_param_cb); |
@@ -3283,8 +3312,8 @@ again: | |||
3283 | /* module_bug_cleanup needs module_mutex protection */ | 3312 | /* module_bug_cleanup needs module_mutex protection */ |
3284 | mutex_lock(&module_mutex); | 3313 | mutex_lock(&module_mutex); |
3285 | module_bug_cleanup(mod); | 3314 | module_bug_cleanup(mod); |
3286 | ddebug_cleanup: | ||
3287 | mutex_unlock(&module_mutex); | 3315 | mutex_unlock(&module_mutex); |
3316 | ddebug_cleanup: | ||
3288 | dynamic_debug_remove(info->debug); | 3317 | dynamic_debug_remove(info->debug); |
3289 | synchronize_sched(); | 3318 | synchronize_sched(); |
3290 | kfree(mod->args); | 3319 | kfree(mod->args); |