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>,
"Jonathan Corbet" <corbet@lwn.net>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Randy Dunlap" <rdunlap@infradead.org>
Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
workflows@vger.kernel.org, linux-doc@vger.kernel.org,
Malte Wechter <maltewechter@gmail.com>
Subject: [PATCH v3 2/4] kunit: add assertions for lockdep inside of kunit
Date: Tue, 13 Oct 2026 12:22:22 +0200 [thread overview]
Message-ID: <20261013-lockdep-kunit-v3-2-cdb8ff720c1f@gmail.com> (raw)
In-Reply-To: <20261013-lockdep-kunit-v3-0-cdb8ff720c1f@gmail.com>
assert that `debug_locks` are on prior to running a kunit testcase and
assert that `debug_locks` has not been turned off while the test has run.
The assertions are gated behind `CONFIG_KUNIT_FAIL_TEST_ON_LOCKDEP=y`,
if it is not enabled, writing and running kunit test cases that trigger
a lockdep warning does _not_ fail the test if the test itself passes.
If enabled, it marks any test that triggers a lockdep warning as failed,
and forcefully skips all proceeding test cases, implying that the kernel
is now seen as unreliable since `debug_locks` are off. Failing a test
when lockdep reports a problem is useful for any unit test that takes
locks, locking bugs are caught by the test that triggered them,
instead of being silently logged and missed.
Signed-off-by: Malte Wechter <maltewechter@gmail.com>
---
lib/kunit/test.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 0e3ed457daca..1292a0799c33 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -6,6 +6,7 @@
* Author: Brendan Higgins <brendanhiggins@google.com>
*/
+#include <linux/debug_locks.h>
#include <kunit/resource.h>
#include <kunit/test.h>
#include <kunit/test-bug.h>
@@ -80,6 +81,29 @@ void __printf(3, 4) __kunit_fail_current_test_impl(const char *file, int line, c
kunit_kfree(current->kunit_test, buffer);
}
+/*
+ * Fails current test if test turns off debug_locks.
+ */
+static void kunit_check_lockdep(struct kunit *test, int prev_debug_locks)
+{
+ if (IS_ENABLED(CONFIG_KUNIT_FAIL_TEST_ON_LOCKDEP) && !debug_locks && prev_debug_locks) {
+ kunit_err(test, "Test triggered a lockdep warning");
+ kunit_set_failure(test);
+ }
+}
+
+/*
+ * Skip a KUnit test if debug_locks is turned off by lockdep.
+ */
+static void kunit_skip_if_lockdep(struct kunit *test, struct kunit_suite *suite)
+{
+ if (IS_ENABLED(CONFIG_KUNIT_FAIL_TEST_ON_LOCKDEP) && !debug_locks) {
+ suite->forced_skip = true;
+ test->forced_skip = true;
+ kunit_skip(test, "force skip: debug_locks are off");
+ }
+}
+
/*
* Enable KUnit tests to run.
*/
@@ -462,6 +486,8 @@ static void kunit_run_case_internal(struct kunit *test,
{
struct timespec64 start, end;
+ kunit_skip_if_lockdep(test, suite);
+
if (suite->init) {
int ret;
@@ -512,6 +538,7 @@ static void kunit_try_run_case(void *data)
struct kunit *test = ctx->test;
struct kunit_suite *suite = ctx->suite;
struct kunit_case *test_case = ctx->test_case;
+ int debug_locks_snapshot = debug_locks;
current->kunit_test = test;
@@ -521,6 +548,8 @@ static void kunit_try_run_case(void *data)
* thread will resume control and handle any necessary clean up.
*/
kunit_run_case_internal(test, suite, test_case);
+
+ kunit_check_lockdep(test, debug_locks_snapshot);
}
static void kunit_try_run_case_cleanup(void *data)
--
2.54.0
next prev parent reply other threads:[~2026-10-13 10:22 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 10:22 [PATCH v3 0/4] kunit: add optional assertions to catch taint and lockdep warnings Malte Wechter
2026-10-13 10:22 ` [PATCH v3 1/4] kunit: add logic for tracking forcefully skipped test cases/suites Malte Wechter
2026-10-13 10:22 ` Malte Wechter [this message]
2026-10-13 10:22 ` [PATCH v3 3/4] kunit: add assertions for TAINT_WARN inside of kunit Malte Wechter
2026-10-13 10:22 ` [PATCH v3 4/4] Documentation: kunit: add documentation and examples to kunit documentation 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=20261013-lockdep-kunit-v3-2-cdb8ff720c1f@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=corbet@lwn.net \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=david@davidgow.net \
--cc=gary@garyguo.net \
--cc=kunit-dev@googlegroups.com \
--cc=linux-doc@vger.kernel.org \
--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=rdunlap@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
--cc=workflows@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox