Skip to content

Commit 2a6526c

Browse files
committed
Merge tag 'linux_kselftest-kunit-fixes-6.8-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
Pull kunit fixes from Shuah Khan: "NULL vs IS_ERR() bug fixes, documentation update, MAINTAINERS file update to add Rae Moar as a reviewer, and a fix to run test suites only after module initialization completes" * tag 'linux_kselftest-kunit-fixes-6.8-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: Documentation: KUnit: Update the instructions on how to test static functions kunit: run test suites only after module initialization completes MAINTAINERS: kunit: Add Rae Moar as a reviewer kunit: device: Fix a NULL vs IS_ERR() check in init() kunit: Fix a NULL vs IS_ERR() bug
2 parents d1d873a + 1a9f2c7 commit 2a6526c

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

Documentation/dev-tools/kunit/usage.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,23 @@ Testing Static Functions
671671
------------------------
672672

673673
If we do not want to expose functions or variables for testing, one option is to
674-
conditionally ``#include`` the test file at the end of your .c file. For
675-
example:
674+
conditionally export the used symbol. For example:
675+
676+
.. code-block:: c
677+
678+
/* In my_file.c */
679+
680+
VISIBLE_IF_KUNIT int do_interesting_thing();
681+
EXPORT_SYMBOL_IF_KUNIT(do_interesting_thing);
682+
683+
/* In my_file.h */
684+
685+
#if IS_ENABLED(CONFIG_KUNIT)
686+
int do_interesting_thing(void);
687+
#endif
688+
689+
Alternatively, you could conditionally ``#include`` the test file at the end of
690+
your .c file. For example:
676691

677692
.. code-block:: c
678693

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11725,6 +11725,7 @@ F: fs/smb/server/
1172511725
KERNEL UNIT TESTING FRAMEWORK (KUnit)
1172611726
M: Brendan Higgins <[email protected]>
1172711727
M: David Gow <[email protected]>
11728+
R: Rae Moar <[email protected]>
1172811729
1172911730
1173011731
S: Maintained

lib/kunit/device.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ int kunit_bus_init(void)
4545
int error;
4646

4747
kunit_bus_device = root_device_register("kunit");
48-
if (!kunit_bus_device)
49-
return -ENOMEM;
48+
if (IS_ERR(kunit_bus_device))
49+
return PTR_ERR(kunit_bus_device);
5050

5151
error = bus_register(&kunit_bus_type);
5252
if (error)

lib/kunit/executor.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ void kunit_free_suite_set(struct kunit_suite_set suite_set)
146146
kfree(suite_set.start);
147147
}
148148

149+
/*
150+
* Filter and reallocate test suites. Must return the filtered test suites set
151+
* allocated at a valid virtual address or NULL in case of error.
152+
*/
149153
struct kunit_suite_set
150154
kunit_filter_suites(const struct kunit_suite_set *suite_set,
151155
const char *filter_glob,

lib/kunit/kunit-test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ static void kunit_device_cleanup_test(struct kunit *test)
720720
long action_was_run = 0;
721721

722722
test_device = kunit_device_register(test, "my_device");
723-
KUNIT_ASSERT_NOT_NULL(test, test_device);
723+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, test_device);
724724

725725
/* Add an action to verify cleanup. */
726726
devm_add_action(test_device, test_dev_action, &action_was_run);

lib/kunit/test.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/panic.h>
1818
#include <linux/sched/debug.h>
1919
#include <linux/sched.h>
20+
#include <linux/mm.h>
2021

2122
#include "debugfs.h"
2223
#include "device-impl.h"
@@ -801,12 +802,19 @@ static void kunit_module_exit(struct module *mod)
801802
};
802803
const char *action = kunit_action();
803804

805+
/*
806+
* Check if the start address is a valid virtual address to detect
807+
* if the module load sequence has failed and the suite set has not
808+
* been initialized and filtered.
809+
*/
810+
if (!suite_set.start || !virt_addr_valid(suite_set.start))
811+
return;
812+
804813
if (!action)
805814
__kunit_test_suites_exit(mod->kunit_suites,
806815
mod->num_kunit_suites);
807816

808-
if (suite_set.start)
809-
kunit_free_suite_set(suite_set);
817+
kunit_free_suite_set(suite_set);
810818
}
811819

812820
static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
@@ -816,12 +824,12 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
816824

817825
switch (val) {
818826
case MODULE_STATE_LIVE:
827+
kunit_module_init(mod);
819828
break;
820829
case MODULE_STATE_GOING:
821830
kunit_module_exit(mod);
822831
break;
823832
case MODULE_STATE_COMING:
824-
kunit_module_init(mod);
825833
break;
826834
case MODULE_STATE_UNFORMED:
827835
break;

0 commit comments

Comments
 (0)