From: Malte Wechter <maltewechter@gmail.com>
To: "Brendan Higgins" <brendan.higgins@linux.dev>,
"David Gow" <david@davidgow.net>,
"Rae Moar" <raemoar63@gmail.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>
Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
Malte Wechter <maltewechter@gmail.com>
Subject: [PATCH v2 1/2] kunit: add extra assertions to KUnit test cases
Date: Mon, 24 Aug 2026 15:32:30 +0200 [thread overview]
Message-ID: <20260824-lockdep-kunit-v2-1-36a6fec9f72e@gmail.com> (raw)
In-Reply-To: <20260824-lockdep-kunit-v2-0-36a6fec9f72e@gmail.com>
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.
Signed-off-by: Malte Wechter <maltewechter@gmail.com>
---
lib/kunit/Kconfig | 12 ++++++++++++
lib/kunit/try-catch.c | 23 ++++++++++++++++++++++-
2 files changed, 34 insertions(+), 1 deletion(-)
diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig
index 94ff8e4089bfb..38801f7493669 100644
--- a/lib/kunit/Kconfig
+++ b/lib/kunit/Kconfig
@@ -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
diff --git a/lib/kunit/try-catch.c b/lib/kunit/try-catch.c
index d84a879f0a789..7eea3af4c9671 100644
--- a/lib/kunit/try-catch.c
+++ b/lib/kunit/try-catch.c
@@ -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)
--
2.51.2
next prev parent reply other threads:[~2026-08-24 13:32 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-24 13:32 [PATCH v2 0/2] kunit: add optional assertions to catch taint and lockdep warnings Malte Wechter
2026-08-24 13:32 ` Malte Wechter [this message]
2026-08-25 11:00 ` [PATCH v2 1/2] kunit: add extra assertions to KUnit test cases David Gow
2026-09-01 9:19 ` Malte Wechter
2026-08-24 13:32 ` [PATCH v2 2/2] kunit: add KUnit test to assert kernel state before KUnit suites are run Malte Wechter
2026-08-25 11:00 ` David Gow
2026-09-01 11:02 ` Malte Wechter
2026-10-06 6:47 ` David Gow
2026-10-06 9:46 ` Malte Wechter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260824-lockdep-kunit-v2-1-36a6fec9f72e@gmail.com \
--to=maltewechter@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brendan.higgins@linux.dev \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=david@davidgow.net \
--cc=gary@garyguo.net \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=raemoar63@gmail.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.