kunit: add optional assertions to catch taint and lockdep warnings
v2 cover kunit: add optional assertions to catch taint and lockdep warnings
this patch tries to expand KUnit by using existing kernel diagnostics to catch certain locking related bugs. Specifically this adds assertions for debug_locks from lockdep and the TAINT_WARN flag from panic. This tries to prevent bugs as: circular locking dependency and sleeping function called from invalid context.
Add config CONFIG_KUNIT_EXTRA_ASSERTS that enables extra assertions to be emitted: First, before _any_ KUnit test suite is ran, this is to ensure that the extra assertions are triggered by a KUnit test and not prior. Secondly, assertions a made for each test case, failing them if the assertions fail, even if the KUnit test is marked as passed.
---
Changes in v2:
- Implement changes directly in C KUnit instead of Rust KUnit wrapper
- Remove rust specific configs and add general CONFIG_KUNIT_EXTRA_ASSERTS config
- Link to v1: https://patch.msgid.link/[email protected]
To: Brendan Higgins <[email protected]>
To: David Gow <[email protected]>
To: Rae Moar <[email protected]>
To: Miguel Ojeda <[email protected]>
To: Boqun Feng <[email protected]>
To: Gary Guo <[email protected]>
To: Björn Roy Baron <[email protected]>
To: Benno Lossin <[email protected]>
To: Andreas Hindborg <[email protected]>
To: Alice Ryhl <[email protected]>
To: Trevor Gross <[email protected]>
To: Danilo Krummrich <[email protected]>
To: Daniel Almeida <[email protected]>
To: Tamir Duberstein <[email protected]>
To: Alexandre Courbot <[email protected]>
To: Onur Özkan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
Malte Wechter (2):
kunit: add extra assertions to KUnit test cases
kunit: add KUnit test to assert kernel state before KUnit suites are run
lib/kunit/Kconfig | 12 ++++++++++++ lib/kunit/executor.c | 8 +++++++- lib/kunit/test.c | 30 ++++++++++++++++++++++++++++++ lib/kunit/try-catch.c | 23 ++++++++++++++++++++++- 4 files changed, 71 insertions(+), 2 deletions(-)
---
base-commit: 8cdeaa50eae8dad34885515f62559ee83e7e8dda
change-id: 20260812-lockdep-kunit-9628f4bcad90
Best regards,
Signature · 1 line
Malte Wechter <[email protected]>
PATCH v2 1/2 kunit: add extra assertions to KUnit test cases
add extra assertions for each individual test case, that checks that both debug_locks and TAINT_WARN are intact after the test is run. The assertions are optional behind CONFIG_KUNIT_EXTRA_ASSERTS.
--- lib/kunit/Kconfig | 12 ++++++++++++ lib/kunit/try-catch.c | 23 ++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-)
lib/kunit/Kconfig +12 -0
@@ -142,4 +142,16 @@ config KUNIT_UML_PCI If unsure, say N. +config KUNIT_EXTRA_ASSERTS + bool "Enable extra assertions in KUnit tests" + depends on LOCKDEP + default n + help + Enables all extra assertions for KUnit which includes asserting `TAINT_WARN` and + `debug_locks` from lockdep. A KUnit test suite (and test case) is inserted + at the start of all KUnit test suites. This makes assertions prior to running any + tests, as a pre-test integrity check. Assertions are made after each test case which + asserts that each test case did not trigger either `TAINT_WARN` or `debug_locks`. + + If unsure, say N. endif # KUNIT
lib/kunit/try-catch.c +22 -1
@@ -41,6 +41,11 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)struct completion *task_done;int exit_code, time_remaining; +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +int debug_locks_snapshot = debug_locks; +int tainted_warn_snapshot =test_taint (TAINT_WARN ); +#endif + try_catch->context = context; try_catch->try_result =0 ; task_struct = kthread_create(kunit_generic_run_threadfn_adapter,
@@ -70,7 +75,23 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)put_task_struct (task_struct); exit_code = try_catch->try_result; -if (!exit_code) +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +bool extra_assert = false; + +if (debug_locks_snapshot != debug_locks && !exit_code) { + extra_assert = true; + try_catch->try_result = -EDEADLK ; +kunit_err (test,"Test triggered lockdep\n" ); + }else if (tainted_warn_snapshot !=test_taint (TAINT_WARN ) && !exit_code) { + extra_assert = true; + try_catch->try_result = -EDEADLK ; +kunit_err (test,"Test tainted kernel with TAINT_WARN\n" ); + } +#else +bool extra_assert = false; +#endif + +if (!exit_code && !extra_assert)return ;if (exit_code == -EFAULT )
Signature · 1 line
2.51.2
PATCH v2 2/2 kunit: add KUnit test to assert kernel state before KUnit suites are run
In reply to Malte Wechteradd pre-defined KUnit test suite and test case that asserts both debug_locks and TAINT_WARN prior to running any (user) KUnit tests. This asserts integrity before tests are run.
--- lib/kunit/executor.c | 8 +++++++- lib/kunit/test.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-)
lib/kunit/executor.c +7 -1
@@ -290,9 +290,15 @@ void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)size_t num_suites = suite_set->end - suite_set->start;bool autorun =kunit_autorun (); +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +size_t num_suites_plus_extra = num_suites+1 ; +#else +size_t num_suites_plus_extra = num_suites; +#endif +if (autorun && (builtin || num_suites)) {pr_info ("KTAP version 1\n" ); -pr_info ("1..%zu\n" , num_suites); +pr_info ("1..%zu\n" , num_suites_plus_extra); }__kunit_test_suites_init (suite_set->start, num_suites, autorun);
lib/kunit/test.c +30 -0
@@ -835,6 +835,30 @@ bool kunit_enabled(void)return enable_param; } +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +#define DEBUG_LOCKS_OK 1 +#define TAINT_WARN_OK 0 + +static void pre_kunit_assert (struct kunit *test) +{ +KUNIT_EXPECT_EQ_MSG (test, debug_locks,DEBUG_LOCKS_OK , +"debug_locks are off before any test ran" ); +KUNIT_EXPECT_EQ_MSG (test,test_taint (TAINT_WARN ),TAINT_WARN_OK , +"kernel already TAINT_WARN tainted before any test ran" ); +} + +static struct kunit_case pre_kunit_assert_cases[] = { +KUNIT_CASE (pre_kunit_assert), + {} +}; + +static struct kunit_suite pre_kunit_assert_clean_state_suite = { + .name ="pre_kunit_extra_asserts" , + .test_cases = pre_kunit_assert_cases, +}; + +#endif /* CONFIG_RUST_KUNIT_EXTRA_ASSERTS */ +int __kunit_test_suites_init (struct kunit_suite *const *const suites,int num_suites,bool run_tests) {
@@ -857,6 +881,12 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ }static_branch_inc (&kunit_running); +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +kunit_init_suite (&pre_kunit_assert_clean_state_suite); +if (run_tests) +kunit_run_tests (&pre_kunit_assert_clean_state_suite); +#endif +for (i =0 ; i < num_suites; i++) {kunit_init_suite (suites[i]);if (run_tests)
Signature · 1 line
2.51.2
David Gow
maintainerRe: [PATCH v2 1/2] kunit: add extra assertions to KUnit test cases
In reply to Malte WechterLe 24/08/2026 à 21:32, Malte Wechter a écrit :
Quoted 6 lines
add extra assertions for each individual test case, that checks that both
debug_locksandTAINT_WARNare intact after the test is run. The assertions are optional behindCONFIG_KUNIT_EXTRA_ASSERTS.Signed-off-by Malte Wechter <[email protected]>---
Thanks for this.
I'd prefer to split this up into two separate changes, one to check for warnings, and one for lockdep issues.
There's already a patch series to handle lockdep failures here:
https://lore.kernel.org/all/[email protected]/
It's pretty similar to this, but may have some good ideas in it.
As for WARN, we have a new feature which allows individual WARN calls to be expected and ignored. This works by having the WARN() macro directly check if a test is failing. The difference there is that the code only runs if the current thread is part of the test (so it wouldn't be affected by a WARN() in another, non-test thread). There are advantages and disadvantages to this, but it's probably sensible to be consistent here.
Take a look at this for more details on how it works:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=85347718ab0dd7ede9c3e1dcff2d604c7073df05
(That being said, the current implementation does seem to work pretty well even with the backtrace suppression, so continuing to use this implementation is not necessarily a dealbreaker.)
Finally, I'd appreciate there being some more documentation of this, if possible. In particular, it'd be nice to extend the KUnit documentation in Documentation/dev-tools/kunit to mention this and provide some examples of how to enable it. In particular, it'd be good to have an example kunit.py invocation which enables lockdep, e.g. ./tools/testing/kunit/kunit.py run --kconfig_add CONFIG_KUNIT_EXTRA_ASSERTS=y --kconfig_add CONFIG_PROVE_LOCKING=y --kconfig_add CONFIG_DEBUG_KERNEL=y
Thanks again for looking into this!
Cheers,
-- David
Quoted 14 lines
lib/kunit/Kconfig | 12 ++++++++++++ lib/kunit/try-catch.c | 23 ++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-)lib/kunit/Kconfig +2
-0@@ -142,4 +142,16 @@ config KUNIT_UML_PCI If unsure, say N. +config KUNIT_EXTRA_ASSERTS + bool "Enable extra assertions in KUnit tests"
lib/kunit/Kconfig config KUNIT_UML_PCI · line 156
Quoted 10 lines
lib/kunit/Kconfig +10
-0+ depends on LOCKDEP + default n + help + Enables all extra assertions for KUnit which includes asserting `TAINT_WARN` and + `debug_locks` from lockdep. A KUnit test suite (and test case) is inserted + at the start of all KUnit test suites. This makes assertions prior to running any + tests, as a pre-test integrity check. Assertions are made after each test case which + asserts that each test case did not trigger either `TAINT_WARN` or `debug_locks`. + + If unsure, say N.
A few notes here:
- I'd rather have the taint/warn and lockdep assertions separate here.
- A more descriptive name than "EXTRA_ASSERTS" would be nice. Perhaps something like KUNIT_FAIL_TEST_ON_WARN / KUNIT_FAIL_ON_LOCKDEP or similar?
lib/kunit/try-catch.c void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) · line 47
Quoted 13 lines
lib/kunit/Kconfig +0
-0endif # KUNITlib/kunit/try-catch.c +4
-0@@ -41,6 +41,11 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)struct completion *task_done;int exit_code, time_remaining; +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +int debug_locks_snapshot = debug_locks; +int tainted_warn_snapshot =test_taint (TAINT_WARN ); +#endif
Nit: Let's keep the preprocessor #ifdef/#endif lines un-indented here.
lib/kunit/try-catch.c void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) · line 79
Quoted 11 lines
lib/kunit/try-catch.c +3
-1+ try_catch->context = context; try_catch->try_result =0 ; task_struct = kthread_create(kunit_generic_run_threadfn_adapter,@@ -70,7 +75,23 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)put_task_struct (task_struct); exit_code = try_catch->try_result; -if (!exit_code) +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +bool extra_assert = false;
Please define all variables at the top of the function / scope.
That being said, we don't need this extra_assert variable at all if you move the setting of try_result above moving it to exit_code above?
lib/kunit/try-catch.c void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) · line 83
Quoted 4 lines
lib/kunit/try-catch.c +4
-0+ +if (debug_locks_snapshot != debug_locks && !exit_code) { + extra_assert = true; + try_catch->try_result = -EDEADLK ;
Why are we setting this, and then never handling it?
lib/kunit/try-catch.c void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) · line 87
Quoted 4 lines
lib/kunit/try-catch.c +4
-0+kunit_err (test,"Test triggered lockdep\n" ); + }else if (tainted_warn_snapshot !=test_taint (TAINT_WARN ) && !exit_code) { + extra_assert = true; + try_catch->try_result = -EDEADLK ;
-EDEADLK made sense for the lockdep implementation, but makes less sense for WARN(). If you really want to keep it, please document it.
That being said, it should be possible to do this outside of the try/catch scope, as both lockdep and taints are global. And if you fail the test using the hook mechanism (like the warning suppression does), you won't need a separate check afterwards.
lib/kunit/try-catch.c void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) · line 91
Quoted 4 lines
lib/kunit/try-catch.c +4
-0+ kunit_err(test, "Test tainted kernel with TAINT_WARN\n"); + } +#else +bool extra_assert = false;
If we declare this unconditionally at the top of the function, there's no need to have it in an #ifdef.
Quoted 7 lines
lib/kunit/try-catch.c +3
-0+#endif + +if (!exit_code && !extra_assert)return ;if (exit_code == -EFAULT )
David Gow
maintainerRe: [PATCH v2 2/2] kunit: add KUnit test to assert kernel state before KUnit suites are run
In reply to Malte WechterLe 24/08/2026 à 21:32, Malte Wechter a écrit :
Quoted 6 lines
add pre-defined KUnit test suite and test case that asserts both
debug_locksandTAINT_WARNprior to running any (user) KUnit tests. This asserts integrity before tests are run.Signed-off-by Malte Wechter <[email protected]>---
I'm not quite as convinced by this as I am by the first patch. While ensuring the state of the system is good before tests are run is useful, this does seem a bit heavy-handed in some respects.
This could probably use a more detailed description, particularly describing why such a test is useful, and why it would need to be implemented in a special way.
And I do think the implementation here is a bit _too_ special-cased. One other possibility would be to prepend this suite using kunit_merge_suite_sets(), so we don't need to have any special handling of (e.g.) the test count. This could also allow this special suite to be filtered out (which has both advantages and disadvantages).
It might also be nice to have this configurable independently from the other checks, and maybe at runtime (via a KUnit module / command-line parameter), particularly if this can't be filtered on. And, as before, this definitely needs to be documented. People need to know how to enable it, and where all of these extra results from tests they didn't enable came from.
Thoughts?
Cheers,
-- David
lib/kunit/executor.c void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin) · line 293
Quoted 13 lines
lib/kunit/executor.c | 8 +++++++- lib/kunit/test.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-)lib/kunit/executor.c +1
-0@@ -290,9 +290,15 @@ void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)size_t num_suites = suite_set->end - suite_set->start;bool autorun =kunit_autorun (); +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS
Nit: Let's not indent the #ifdefs.
lib/kunit/executor.c void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin) · line 298
Quoted 5 lines
lib/kunit/executor.c +5
-0+size_t num_suites_plus_extra = num_suites+1 ; +#else +size_t num_suites_plus_extra = num_suites; +#endif +
I'm not particularly happy with this way of adding an extra suite.
Quoted 12 lines
lib/kunit/executor.c +1
-1if (autorun && (builtin || num_suites)) {pr_info ("KTAP version 1\n" ); -pr_info ("1..%zu\n" , num_suites); +pr_info ("1..%zu\n" , num_suites_plus_extra); }__kunit_test_suites_init (suite_set->start, num_suites, autorun);lib/kunit/test.c +0
-0
lib/kunit/test.c bool kunit_enabled(void) · line 840
Quoted 6 lines
lib/kunit/test.c +3
-0@@ -835,6 +835,30 @@ bool kunit_enabled(void)return enable_param; } +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +#define DEBUG_LOCKS_OK 1 +#define TAINT_WARN_OK 0
Not totally sold on these #defines: I think I'd prefer to just have the literal 1/0.
lib/kunit/test.c bool kunit_enabled(void) · line 848
Quoted 8 lines
lib/kunit/test.c +8
-0+ +static void pre_kunit_assert (struct kunit *test) +{ +KUNIT_EXPECT_EQ_MSG (test, debug_locks,DEBUG_LOCKS_OK , +"debug_locks are off before any test ran" ); +KUNIT_EXPECT_EQ_MSG (test,test_taint (TAINT_WARN ),TAINT_WARN_OK , +"kernel already TAINT_WARN tainted before any test ran" ); +}
If we are going to generate a special suite, let's have the taint and lockdep checks as separate tests.
This would also make it easier to have them be configurable separately.
lib/kunit/test.c bool kunit_enabled(void) · line 856
Quoted 8 lines
lib/kunit/test.c +8
-0+ +static struct kunit_case pre_kunit_assert_cases[] = { +KUNIT_CASE (pre_kunit_assert), + {} +}; + +static struct kunit_suite pre_kunit_assert_clean_state_suite = { + .name ="pre_kunit_extra_asserts" ,
I think we could probably find a better name for this. "initial_system_state" or similar might be better?
Quoted 22 lines
lib/kunit/test.c +11
-0+ .test_cases = pre_kunit_assert_cases, +}; + +#endif /* CONFIG_RUST_KUNIT_EXTRA_ASSERTS */ +int __kunit_test_suites_init (struct kunit_suite *const *const suites,int num_suites,bool run_tests) {@@ -857,6 +881,12 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ }static_branch_inc (&kunit_running); +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +kunit_init_suite (&pre_kunit_assert_clean_state_suite); +if (run_tests) +kunit_run_tests (&pre_kunit_assert_clean_state_suite); +#endif +for (i =0 ; i < num_suites; i++) {kunit_init_suite (suites[i]);if (run_tests)
Re: [PATCH v2 1/2] kunit: add extra assertions to KUnit test cases
In reply to David GowDen tirs. 25. aug. 2026 kl. 13.00 skrev David Gow <[email protected]>:
Quoted 6 lines · Malte Wechter
Quoted 4 lines
add extra assertions for each individual test case, that checks that both
debug_locksandTAINT_WARNare intact after the test is run. The assertions are optional behindCONFIG_KUNIT_EXTRA_ASSERTS.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Malte Wechter <[email protected]>---
Thanks for this.
I'd prefer to split this up into two separate changes, one to check for warnings, and one for lockdep issues.
I agree that this would be better.
Quoted 20 lines
There's already a patch series to handle lockdep failures here:
https://lore.kernel.org/all/[email protected]/It's pretty similar to this, but may have some good ideas in it.
As for WARN, we have a new feature which allows individual WARN calls to be expected and ignored. This works by having the
WARN()macro directly check if a test is failing. The difference there is that the code only runs if the current thread is part of the test (so it wouldn't be affected by aWARN()in another, non-test thread). There are advantages and disadvantages to this, but it's probably sensible to be consistent here.Take a look at this for more details on how it works:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=85347718ab0dd7ede9c3e1dcff2d604c7073df05(That being said, the current implementation does seem to work pretty well even with the backtrace suppression, so continuing to use this implementation is not necessarily a dealbreaker.)
Quoted 8 lines
Finally, I'd appreciate there being some more documentation of this, if possible. In particular, it'd be nice to extend the KUnit documentation in
Documentation/dev-tools/kunitto mention this and provide some examples of how to enable it. In particular, it'd be good to have an examplekunit.pyinvocation which enables lockdep, e.g../tools/testing/kunit/kunit.pyrun --kconfig_addCONFIG_KUNIT_EXTRA_ASSERTS=y--kconfig_addCONFIG_PROVE_LOCKING=y--kconfig_addCONFIG_DEBUG_KERNEL=y
Will do!
Quoted 82 lines
Thanks again for looking into this!
Cheers,
-- DavidQuoted 14 lines
lib/kunit/Kconfig | 12 ++++++++++++ lib/kunit/try-catch.c | 23 ++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-)lib/kunit/Kconfig +2
-0@@ -142,4 +142,16 @@ config KUNIT_UML_PCI If unsure, say N. +config KUNIT_EXTRA_ASSERTS + bool "Enable extra assertions in KUnit tests"lib/kunit/Kconfig config KUNIT_UML_PCI · line 156
Quoted 10 lines
lib/kunit/Kconfig +10
-0+ depends on LOCKDEP + default n + help + Enables all extra assertions for KUnit which includes asserting `TAINT_WARN` and + `debug_locks` from lockdep. A KUnit test suite (and test case) is inserted + at the start of all KUnit test suites. This makes assertions prior to running any + tests, as a pre-test integrity check. Assertions are made after each test case which + asserts that each test case did not trigger either `TAINT_WARN` or `debug_locks`. + + If unsure, say N.A few notes here:
- I'd rather have the taint/warn and lockdep assertions separate here.
- A more descriptive name than "EXTRA_ASSERTS" would be nice. Perhaps something likeKUNIT_FAIL_TEST_ON_WARN/KUNIT_FAIL_ON_LOCKDEPor similar?lib/kunit/try-catch.c void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) · line 47
Quoted 13 lines
lib/kunit/Kconfig +0
-0endif # KUNITlib/kunit/try-catch.c +4
-0@@ -41,6 +41,11 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)struct completion *task_done;int exit_code, time_remaining; +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +int debug_locks_snapshot = debug_locks; +int tainted_warn_snapshot =test_taint (TAINT_WARN ); +#endif Nit: Let's keep the preprocessor #ifdef/#endif lines un-indented here.
lib/kunit/try-catch.c void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) · line 79
Quoted 11 lines
lib/kunit/try-catch.c +3
-1+ try_catch->context = context; try_catch->try_result =0 ; task_struct = kthread_create(kunit_generic_run_threadfn_adapter,@@ -70,7 +75,23 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)put_task_struct (task_struct); exit_code = try_catch->try_result; -if (!exit_code) +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +bool extra_assert = false;Please define all variables at the top of the function / scope.
That being said, we don't need this
extra_assertvariable at all if you move the setting oftry_resultabove moving it toexit_codeabove?lib/kunit/try-catch.c void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) · line 83
Quoted 4 lines
lib/kunit/try-catch.c +4
-0+ +if (debug_locks_snapshot != debug_locks && !exit_code) { + extra_assert = true; + try_catch->try_result = -EDEADLK ;Why are we setting this, and then never handling it?
lib/kunit/try-catch.c void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) · line 87
Quoted 4 lines
lib/kunit/try-catch.c +4
-0+kunit_err (test,"Test triggered lockdep\n" ); + }else if (tainted_warn_snapshot !=test_taint (TAINT_WARN ) && !exit_code) { + extra_assert = true; + try_catch->try_result = -EDEADLK ;
Quoted 7 lines
-EDEADLKmade sense for the lockdep implementation, but makes less sense forWARN(). If you really want to keep it, please document it.That being said, it should be possible to do this outside of the try/catch scope, as both lockdep and taints are global. And if you fail the test using the hook mechanism (like the warning suppression does), you won't need a separate check afterwards.
I believe that installing two additional hooks (one for lockdep and one for warn, possibly a third for the initial suite check) is a cleaner approach. I
will move the assertion logic into KUnit hooks instead of having it inside of the try_catch_run.
Quoted 17 lines
lib/kunit/try-catch.c void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) · line 91
Quoted 5 lines
lib/kunit/try-catch.c +4
-0+ kunit_err(test, "Test tainted kernel with TAINT_WARN\n"); + } +#else +bool extra_assert = false;If we declare this unconditionally at the top of the function, there's no need to have it in an #ifdef.
Quoted 7 lines
lib/kunit/try-catch.c +3
-0+#endif + +if (!exit_code && !extra_assert)return ;if (exit_code == -EFAULT )
Best regards,
Malte :)
Re: [PATCH v2 2/2] kunit: add KUnit test to assert kernel state before KUnit suites are run
In reply to David GowDen tirs. 25. aug. 2026 kl. 13.00 skrev David Gow <[email protected]>:
Quoted 23 lines · Malte Wechter
Quoted 6 lines
add pre-defined KUnit test suite and test case that asserts both
debug_locksandTAINT_WARNprior to running any (user) KUnit tests. This asserts integrity before tests are run.Signed-off-by Malte Wechter <[email protected]>---
I'm not quite as convinced by this as I am by the first patch. While ensuring the state of the system is good before tests are run is useful, this does seem a bit heavy-handed in some respects.
This could probably use a more detailed description, particularly describing why such a test is useful, and why it would need to be implemented in a special way.
And I do think the implementation here is a bit _too_ special-cased. One other possibility would be to prepend this suite using
kunit_merge_suite_sets(), so we don't need to have any special handling of (e.g.) the test count. This could also allow this special suite to be filtered out (which has both advantages and disadvantages).
Quoted 8 lines
It might also be nice to have this configurable independently from the other checks, and maybe at runtime (via a KUnit module / command-line parameter), particularly if this can't be filtered on. And, as before, this definitely needs to be documented. People need to know how to enable it, and where all of these extra results from tests they didn't enable came from.
Thoughts?
I get your point, the purpose of this special test suite is to assert that the kernel is in a "fine" state before any unit tests are run. If this
check is left out the false positives could occur if the system is in a bad state before the tests are run.
The reason that the case was handled differently compared to other was because this assertion _must_ be run before any other tests, and when kunit_merge_suite_sets() is called from kunit_run_all_tests() it also filters the test suites, which would not give any guarentee that this
special suite gets run first.
I do agree that this initial way is maybe a bit coarse, and i will see if i can find a better fit for this assertion. But i dont want to leave it out.
Quoted 88 lines
Cheers,
-- Davidlib/kunit/executor.c void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin) · line 293
Quoted 13 lines
lib/kunit/executor.c | 8 +++++++- lib/kunit/test.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-)lib/kunit/executor.c +1
-0@@ -290,9 +290,15 @@ void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)size_t num_suites = suite_set->end - suite_set->start;bool autorun =kunit_autorun (); +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS Nit: Let's not indent the #ifdefs.
lib/kunit/executor.c void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin) · line 298
Quoted 5 lines
lib/kunit/executor.c +5
-0+size_t num_suites_plus_extra = num_suites+1 ; +#else +size_t num_suites_plus_extra = num_suites; +#endif +I'm not particularly happy with this way of adding an extra suite.
Quoted 12 lines
lib/kunit/executor.c +1
-1if (autorun && (builtin || num_suites)) {pr_info ("KTAP version 1\n" ); -pr_info ("1..%zu\n" , num_suites); +pr_info ("1..%zu\n" , num_suites_plus_extra); }__kunit_test_suites_init (suite_set->start, num_suites, autorun);lib/kunit/test.c +0
-0lib/kunit/test.c bool kunit_enabled(void) · line 840
Quoted 6 lines
lib/kunit/test.c +3
-0@@ -835,6 +835,30 @@ bool kunit_enabled(void)return enable_param; } +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +#define DEBUG_LOCKS_OK 1 +#define TAINT_WARN_OK 0Not totally sold on these #defines: I think I'd prefer to just have the literal 1/0.
lib/kunit/test.c bool kunit_enabled(void) · line 848
Quoted 8 lines
lib/kunit/test.c +8
-0+ +static void pre_kunit_assert (struct kunit *test) +{ +KUNIT_EXPECT_EQ_MSG (test, debug_locks,DEBUG_LOCKS_OK , +"debug_locks are off before any test ran" ); +KUNIT_EXPECT_EQ_MSG (test,test_taint (TAINT_WARN ),TAINT_WARN_OK , +"kernel already TAINT_WARN tainted before any test ran" ); +}If we are going to generate a special suite, let's have the taint and lockdep checks as separate tests.
This would also make it easier to have them be configurable separately.
lib/kunit/test.c bool kunit_enabled(void) · line 856
Quoted 8 lines
lib/kunit/test.c +8
-0+ +static struct kunit_case pre_kunit_assert_cases[] = { +KUNIT_CASE (pre_kunit_assert), + {} +}; + +static struct kunit_suite pre_kunit_assert_clean_state_suite = { + .name ="pre_kunit_extra_asserts" ,I think we could probably find a better name for this. "
initial_system_state" or similar might be better?Quoted 12 lines
lib/kunit/test.c +5
-0+ .test_cases = pre_kunit_assert_cases, +}; + +#endif /* CONFIG_RUST_KUNIT_EXTRA_ASSERTS */ +int __kunit_test_suites_init (struct kunit_suite *const *const suites,int num_suites,bool run_tests) {@@ -857,6 +881,12 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ }static_branch_inc (&kunit_running);
Quoted 11 lines
lib/kunit/test.c int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ · line 892
Quoted 10 lines
lib/kunit/test.c +6
-0+#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +kunit_init_suite (&pre_kunit_assert_clean_state_suite); +if (run_tests) +kunit_run_tests (&pre_kunit_assert_clean_state_suite); +#endif +for (i =0 ; i < num_suites; i++) {kunit_init_suite (suites[i]);if (run_tests)
Best regards,
Malte :)
David Gow
maintainerRe: [PATCH v2 2/2] kunit: add KUnit test to assert kernel state before KUnit suites are run
Le 01/09/2026 à 19:02, Malte Wechter a écrit :
Quoted 44 lines
Den tirs. 25. aug. 2026 kl. 13.00 skrev David Gow <[email protected]>:
Quoted 23 lines · Malte Wechter
Quoted 6 lines
add pre-defined KUnit test suite and test case that asserts both
debug_locksandTAINT_WARNprior to running any (user) KUnit tests. This asserts integrity before tests are run.Signed-off-by Malte Wechter <[email protected]>---
I'm not quite as convinced by this as I am by the first patch. While ensuring the state of the system is good before tests are run is useful, this does seem a bit heavy-handed in some respects.
This could probably use a more detailed description, particularly describing why such a test is useful, and why it would need to be implemented in a special way.
And I do think the implementation here is a bit _too_ special-cased. One other possibility would be to prepend this suite using
kunit_merge_suite_sets(), so we don't need to have any special handling of (e.g.) the test count. This could also allow this special suite to be filtered out (which has both advantages and disadvantages).Quoted 8 lines
It might also be nice to have this configurable independently from the other checks, and maybe at runtime (via a KUnit module / command-line parameter), particularly if this can't be filtered on. And, as before, this definitely needs to be documented. People need to know how to enable it, and where all of these extra results from tests they didn't enable came from.
Thoughts?
I get your point, the purpose of this special test suite is to assert that the kernel is in a "fine" state before any unit tests are run. If this
check is left out the false positives could occur if the system is in a bad state before the tests are run.
The reason that the case was handled differently compared to other was because this assertion _must_ be run before any other tests, and whenkunit_merge_suite_sets()is called fromkunit_run_all_tests()it also filters the test suites, which would not give any guarentee that this
special suite gets run first.
Quoted 3 lines
I do agree that this initial way is maybe a bit coarse, and i will see if i can find a better fit for this assertion. But i dont want to leave it out.
Had a chat with Andreas about this yesterday, and we think we've come up with another possible option here: instead of doing one check to verify the state before any KUnit tests are run, have an option to check the taint before _every_ test, and to skip it if the kernel isn't clean.
There are a few consequences of this:
- You'd need to be able to mask out taints you don't care about, especially TAINT_TEST (as without it all tests but the first would fail). Of course, that might still be useful if you really want to ensure isolation between tests (e.g. --run_isolated)
- If the kernel is already tainted, and so all tests are skipped, kunit.py should report 'no tests run' as an error. I think this actually only reports an error if no tests are executed at the moment, so this may need looking at.
- This will trigger if the kernel is tainted by some other part of the kernel either between KUnit tests, or in another kthread. However, in this case, the results will look like a series of successful tests, followed by skipped ones, with no failed tests. (And hence won't trigger a failure exit code from kunit.py). This is likely to be incredibly rare, but we could add a fail-on-skip option or similar if needed.
Thoughts?
Cheers,
-- David
Quoted 101 lines
Quoted 88 lines
Cheers,
-- Davidlib/kunit/executor.c void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin) · line 293
Quoted 13 lines
lib/kunit/executor.c | 8 +++++++- lib/kunit/test.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-)lib/kunit/executor.c +1
-0@@ -290,9 +290,15 @@ void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)size_t num_suites = suite_set->end - suite_set->start;bool autorun =kunit_autorun (); +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS Nit: Let's not indent the #ifdefs.
lib/kunit/executor.c void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin) · line 298
Quoted 5 lines
lib/kunit/executor.c +5
-0+size_t num_suites_plus_extra = num_suites+1 ; +#else +size_t num_suites_plus_extra = num_suites; +#endif +I'm not particularly happy with this way of adding an extra suite.
Quoted 12 lines
lib/kunit/executor.c +1
-1if (autorun && (builtin || num_suites)) {pr_info ("KTAP version 1\n" ); -pr_info ("1..%zu\n" , num_suites); +pr_info ("1..%zu\n" , num_suites_plus_extra); }__kunit_test_suites_init (suite_set->start, num_suites, autorun);lib/kunit/test.c +0
-0lib/kunit/test.c bool kunit_enabled(void) · line 840
Quoted 6 lines
lib/kunit/test.c +3
-0@@ -835,6 +835,30 @@ bool kunit_enabled(void)return enable_param; } +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +#define DEBUG_LOCKS_OK 1 +#define TAINT_WARN_OK 0Not totally sold on these #defines: I think I'd prefer to just have the literal 1/0.
lib/kunit/test.c bool kunit_enabled(void) · line 848
Quoted 8 lines
lib/kunit/test.c +8
-0+ +static void pre_kunit_assert (struct kunit *test) +{ +KUNIT_EXPECT_EQ_MSG (test, debug_locks,DEBUG_LOCKS_OK , +"debug_locks are off before any test ran" ); +KUNIT_EXPECT_EQ_MSG (test,test_taint (TAINT_WARN ),TAINT_WARN_OK , +"kernel already TAINT_WARN tainted before any test ran" ); +}If we are going to generate a special suite, let's have the taint and lockdep checks as separate tests.
This would also make it easier to have them be configurable separately.
lib/kunit/test.c bool kunit_enabled(void) · line 856
Quoted 8 lines
lib/kunit/test.c +8
-0+ +static struct kunit_case pre_kunit_assert_cases[] = { +KUNIT_CASE (pre_kunit_assert), + {} +}; + +static struct kunit_suite pre_kunit_assert_clean_state_suite = { + .name ="pre_kunit_extra_asserts" ,I think we could probably find a better name for this. "
initial_system_state" or similar might be better?Quoted 12 lines
lib/kunit/test.c +5
-0+ .test_cases = pre_kunit_assert_cases, +}; + +#endif /* CONFIG_RUST_KUNIT_EXTRA_ASSERTS */ +int __kunit_test_suites_init (struct kunit_suite *const *const suites,int num_suites,bool run_tests) {@@ -857,6 +881,12 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ }static_branch_inc (&kunit_running);Quoted 11 lines
lib/kunit/test.c int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ · line 892
Quoted 10 lines
lib/kunit/test.c +6
-0+#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +kunit_init_suite (&pre_kunit_assert_clean_state_suite); +if (run_tests) +kunit_run_tests (&pre_kunit_assert_clean_state_suite); +#endif +for (i =0 ; i < num_suites; i++) {kunit_init_suite (suites[i]);if (run_tests)Best regards,
Malte :)
Re: [PATCH v2 2/2] kunit: add KUnit test to assert kernel state before KUnit suites are run
Hi,
- I was actually considering this as an option, and as you might also believe, extending KUnit with a test case that is guaranteed to run before any user implemented a test case, might be too complex of a task.
- For the taint i could implement the inverse of test_taint(flag) that simply checks that no taints except flag is set. Or it could be tailored specifically for the TAINT_TEST.
- And for the case where the kernel becomes tainted somewhere in the middle and starts skipping unit tests, it is a rare case and would require more work to be able to distinguish between a KUnit test triggering a taint/lockdep or somewhere else in the kernel. I believe this is important to recognize and document, but the change is still good even with this edge case present.
Best regards,
Malte :)
Den tirs. 6. okt. 2026 kl. 08.47 skrev David Gow <[email protected]>:
Quoted 178 lines · Malte Wechter
Quoted 44 lines
Den tirs. 25. aug. 2026 kl. 13.00 skrev David Gow <[email protected]>:
Quoted 23 lines · Malte Wechter
Quoted 6 lines
add pre-defined KUnit test suite and test case that asserts both
debug_locksandTAINT_WARNprior to running any (user) KUnit tests. This asserts integrity before tests are run.Signed-off-by Malte Wechter <[email protected]>---
I'm not quite as convinced by this as I am by the first patch. While ensuring the state of the system is good before tests are run is useful, this does seem a bit heavy-handed in some respects.
This could probably use a more detailed description, particularly describing why such a test is useful, and why it would need to be implemented in a special way.
And I do think the implementation here is a bit _too_ special-cased. One other possibility would be to prepend this suite using
kunit_merge_suite_sets(), so we don't need to have any special handling of (e.g.) the test count. This could also allow this special suite to be filtered out (which has both advantages and disadvantages).Quoted 8 lines
It might also be nice to have this configurable independently from the other checks, and maybe at runtime (via a KUnit module / command-line parameter), particularly if this can't be filtered on. And, as before, this definitely needs to be documented. People need to know how to enable it, and where all of these extra results from tests they didn't enable came from.
Thoughts?
I get your point, the purpose of this special test suite is to assert that the kernel is in a "fine" state before any unit tests are run. If this
check is left out the false positives could occur if the system is in a bad state before the tests are run.
The reason that the case was handled differently compared to other was because this assertion _must_ be run before any other tests, and whenkunit_merge_suite_sets()is called fromkunit_run_all_tests()it also filters the test suites, which would not give any guarentee that this
special suite gets run first.Quoted 3 lines
I do agree that this initial way is maybe a bit coarse, and i will see if i can find a better fit for this assertion. But i dont want to leave it out.
Had a chat with Andreas about this yesterday, and we think we've come up with another possible option here: instead of doing one check to verify the state before any KUnit tests are run, have an option to check the taint before _every_ test, and to skip it if the kernel isn't clean.
There are a few consequences of this:
- You'd need to be able to mask out taints you don't care about, especiallyTAINT_TEST(as without it all tests but the first would fail). Of course, that might still be useful if you really want to ensure isolation between tests (e.g. --run_isolated)
- If the kernel is already tainted, and so all tests are skipped,kunit.pyshould report 'no tests run' as an error. I think this actually only reports an error if no tests are executed at the moment, so this may need looking at.
- This will trigger if the kernel is tainted by some other part of the kernel either between KUnit tests, or in another kthread. However, in this case, the results will look like a series of successful tests, followed by skipped ones, with no failed tests. (And hence won't trigger a failure exit code fromkunit.py). This is likely to be incredibly rare, but we could add a fail-on-skip option or similar if needed.Thoughts?
Cheers,
-- DavidQuoted 101 lines
Quoted 88 lines
Cheers,
-- Davidlib/kunit/executor.c void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin) · line 293
Quoted 13 lines
lib/kunit/executor.c | 8 +++++++- lib/kunit/test.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-)lib/kunit/executor.c +1
-0@@ -290,9 +290,15 @@ void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)size_t num_suites = suite_set->end - suite_set->start;bool autorun =kunit_autorun (); +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS Nit: Let's not indent the #ifdefs.
lib/kunit/executor.c void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin) · line 298
Quoted 5 lines
lib/kunit/executor.c +5
-0+size_t num_suites_plus_extra = num_suites+1 ; +#else +size_t num_suites_plus_extra = num_suites; +#endif +I'm not particularly happy with this way of adding an extra suite.
Quoted 12 lines
lib/kunit/executor.c +1
-1if (autorun && (builtin || num_suites)) {pr_info ("KTAP version 1\n" ); -pr_info ("1..%zu\n" , num_suites); +pr_info ("1..%zu\n" , num_suites_plus_extra); }__kunit_test_suites_init (suite_set->start, num_suites, autorun);lib/kunit/test.c +0
-0lib/kunit/test.c bool kunit_enabled(void) · line 840
Quoted 6 lines
lib/kunit/test.c +3
-0@@ -835,6 +835,30 @@ bool kunit_enabled(void)return enable_param; } +#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +#define DEBUG_LOCKS_OK 1 +#define TAINT_WARN_OK 0Not totally sold on these #defines: I think I'd prefer to just have the literal 1/0.
lib/kunit/test.c bool kunit_enabled(void) · line 848
Quoted 8 lines
lib/kunit/test.c +8
-0+ +static void pre_kunit_assert (struct kunit *test) +{ +KUNIT_EXPECT_EQ_MSG (test, debug_locks,DEBUG_LOCKS_OK , +"debug_locks are off before any test ran" ); +KUNIT_EXPECT_EQ_MSG (test,test_taint (TAINT_WARN ),TAINT_WARN_OK , +"kernel already TAINT_WARN tainted before any test ran" ); +}If we are going to generate a special suite, let's have the taint and lockdep checks as separate tests.
This would also make it easier to have them be configurable separately.
lib/kunit/test.c bool kunit_enabled(void) · line 856
Quoted 8 lines
lib/kunit/test.c +8
-0+ +static struct kunit_case pre_kunit_assert_cases[] = { +KUNIT_CASE (pre_kunit_assert), + {} +}; + +static struct kunit_suite pre_kunit_assert_clean_state_suite = { + .name ="pre_kunit_extra_asserts" ,I think we could probably find a better name for this. "
initial_system_state" or similar might be better?Quoted 12 lines
lib/kunit/test.c +5
-0+ .test_cases = pre_kunit_assert_cases, +}; + +#endif /* CONFIG_RUST_KUNIT_EXTRA_ASSERTS */ +int __kunit_test_suites_init (struct kunit_suite *const *const suites,int num_suites,bool run_tests) {@@ -857,6 +881,12 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ }static_branch_inc (&kunit_running);Quoted 11 lines
lib/kunit/test.c int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ · line 892
Quoted 10 lines
lib/kunit/test.c +6
-0+#ifdef CONFIG_KUNIT_EXTRA_ASSERTS +kunit_init_suite (&pre_kunit_assert_clean_state_suite); +if (run_tests) +kunit_run_tests (&pre_kunit_assert_clean_state_suite); +#endif +for (i =0 ; i < num_suites; i++) {kunit_init_suite (suites[i]);if (run_tests)Best regards,
Malte :)