[PATCH 1/2] rust: kunit: add config to fail kunit tests if a lockdep warning is triggered
From: Malte Wechter
Date: Tue Aug 18 2026 - 11:21:22 EST
When running KUnit tests and a lockdep warning is triggered, the test is
still marked as passed based only on if test assertions are true.
Thus, add a Kconfig option CONFIG_RUST_LOCKDEP_KUNIT_DEBUG_LOCKS. When
this option selected, if lockdep triggers during a test, fail the test.
Signed-off-by: Malte Wechter <maltewechter@xxxxxxxxx>
---
lib/kunit/Kconfig | 13 +++++++++++++
rust/macros/kunit.rs | 10 ++++++++++
2 files changed, 23 insertions(+)
diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig
index 94ff8e4089bfb..30bac00c42ce1 100644
--- a/lib/kunit/Kconfig
+++ b/lib/kunit/Kconfig
@@ -142,4 +142,17 @@ config KUNIT_UML_PCI
If unsure, say N.
+config RUST_LOCKDEP_KUNIT_DEBUG_LOCKS
+ bool "Enable extra debug_locks assertion in Rust KUnit tests"
+ depends on RUST
+ depends on LOCKDEP
+ default n
+ help
+ Adds an extra assertion to each Rust kunit test case that asserts
+ that the debug_locks flag from `lockdep` is unchanged. This is useful
+ when writing unit tests that could potentially trigger a lockdep warning,
+ this makes it so the KUnit test does not succeed if the test assertions are
+ true, but a lockdep warning is triggered.
+
+ If unsure, say N.
endif # KUNIT
diff --git a/rust/macros/kunit.rs b/rust/macros/kunit.rs
index ae20ed6768f15..d1cd0349f86f0 100644
--- a/rust/macros/kunit.rs
+++ b/rust/macros/kunit.rs
@@ -144,9 +144,19 @@ macro_rules! assert_eq {
// here to reduce the length of the assert message.
#(#cfg_attrs)*
{
+ #[cfg(CONFIG_RUST_LOCKDEP_KUNIT_DEBUG_LOCKS)]
+ let __debug_locks_snapshot = ::kernel::bindings::debug_locks;
+
(*_test).status = ::kernel::bindings::kunit_status_KUNIT_SUCCESS;
use ::kernel::kunit::is_test_result_ok;
assert!(is_test_result_ok(#test()));
+
+ #[cfg(CONFIG_RUST_LOCKDEP_KUNIT_DEBUG_LOCKS)]
+ {
+ let __debug_locks_ok =
+ ::kernel::bindings::debug_locks == __debug_locks_snapshot;
+ assert!(__debug_locks_ok);
+ }
}
}
});
--
2.51.2