[POC 18/23] module: Refactor add_unformed_module()

Petr Mladek
2020-01-17T15:04:15Z

The livepatching code will need to add another condition to avoid
waiting. Let's make the code slightly less hairy.

Of course, it is a matter of taste. The main ideas of refactoring:

+ Make it clear what happens when old->state == MODULE_STATE_LIVE.

+ Make it more clear when we are leaving the function immediately
and when we are doing some extra actions.

+ Be able to add another check that has to be done outside
module_mutex and can result into an immediate return.

Signed-off-by: Petr Mladek <pmladek@suse.com>
---
kernel/module.c | 28 +++++++++++++++-------------
1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/kernel/module.c b/kernel/module.c
index b3f11524f8f9..ac45d465ff23 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -3721,28 +3721,30 @@ static int add_unformed_module(struct module *mod)
again:
mutex_lock(&module_mutex);
old = find_module_all(mod->name, strlen(mod->name), true);
+
if (old != NULL) {
- if (old->state != MODULE_STATE_LIVE) {
- /* Wait in case it fails to load. */
+ if (old->state == MODULE_STATE_LIVE) {
mutex_unlock(&module_mutex);
- err = wait_event_interruptible(module_wq,
- finished_loading(mod->name));
- if (err)
- goto out_unlocked;
- goto again;
+ return -EEXIST;
}
- err = -EEXIST;
- goto out;
+
+ /* Wait in case it fails to load. */
+ mutex_unlock(&module_mutex);
+ err = wait_event_interruptible(module_wq,
+ finished_loading(mod->name));
+ if (err)
+ return err;
+
+ /* Did the load succeded or failed? */
+ goto again;
}
+
mod_update_bounds(mod);
list_add_rcu(&mod->list, &modules);
mod_tree_insert(mod);
- err = 0;

-out:
mutex_unlock(&module_mutex);
-out_unlocked:
- return err;
+ return 0;
}

static int complete_formation(struct module *mod, struct load_info *info)
--
2.16.4