When the module' state is MODULE_STATE_COMING, the return err code from
kunit_module_init() can be used to return. So when kunit_module_init()
fails, it will not be notified to go to kunit_module_exit() and the empty
checking can be removed. Because if kunit_filter_suites() succeeds, it
will notify the module state to MODULE_STATE_GOING otherwise it will not
and the test suites will be freed in kunit_filter_suites(). The best
practice is return the err code from MODULE_STATE_COMING func.

And if kunit_filter_suites() fails, both suite_set.start and suite_set.end
will be NULL, and the code behind it is dead code. so return err if
it fails.

Signed-off-by: Jinjie Ruan <[email protected]>
---
 lib/kunit/test.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 29a5f5ea8112..145f70219f46 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -734,7 +734,7 @@ void __kunit_test_suites_exit(struct kunit_suite **suites, 
int num_suites)
 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
 
 #ifdef CONFIG_MODULES
-static void kunit_module_init(struct module *mod)
+static int kunit_module_init(struct module *mod)
 {
        struct kunit_suite_set suite_set = {
                mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
@@ -760,6 +760,8 @@ static void kunit_module_init(struct module *mod)
                kunit_exec_list_tests(&suite_set, true);
        else
                pr_err("kunit: unknown action '%s'\n", action);
+
+       return err;
 }
 
 static void kunit_module_exit(struct module *mod)
@@ -773,18 +775,18 @@ static void kunit_module_exit(struct module *mod)
                __kunit_test_suites_exit(mod->kunit_suites,
                                         mod->num_kunit_suites);
 
-       if (suite_set.start)
-               kunit_free_suite_set(suite_set);
+       kunit_free_suite_set(suite_set);
 }
 
 static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
                               void *data)
 {
        struct module *mod = data;
+       int ret = 0;
 
        switch (val) {
        case MODULE_STATE_COMING:
-               kunit_module_init(mod);
+               ret = kunit_module_init(mod);
                break;
        case MODULE_STATE_LIVE:
                break;
@@ -795,7 +797,7 @@ static int kunit_module_notify(struct notifier_block *nb, 
unsigned long val,
                break;
        }
 
-       return 0;
+       return notifier_from_errno(ret);
 }
 
 static struct notifier_block kunit_mod_nb = {
-- 
2.34.1

Reply via email to