[RFC PATCH] kunit: run test suites only after module initialization completes

From: Marco Pagani
Date: Wed Sep 27 2023 - 11:50:17 EST


Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in
kunit_free_suite_set()") causes test suites to run while the test
module is still in MODULE_STATE_COMING. In that state, the module
is not fully initialized, lacking sysfs, module_memory, args, init
function. This behavior can cause all sorts of problems while using
fake devices.

This RFC patch restores the normal execution flow, waiting for module
initialization to complete before running the tests, and uses the
refcount to avoid calling kunit_free_suite_set() if load_module()
fails.

Fixes: 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()")
Signed-off-by: Marco Pagani <marpagan@xxxxxxxxxx>
---
lib/kunit/test.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 421f13981412..242f26ad387a 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -784,13 +784,13 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val,

switch (val) {
case MODULE_STATE_LIVE:
+ kunit_module_init(mod);
break;
case MODULE_STATE_GOING:
- kunit_module_exit(mod);
+ if (module_refcount(mod) == -1)
+ kunit_module_exit(mod);
break;
case MODULE_STATE_COMING:
- kunit_module_init(mod);
- break;
case MODULE_STATE_UNFORMED:
break;
}
--
2.41.0