Linux Kernel Selftest development
 help / color / mirror / Atom feed
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 1/2] rust: kunit: add config to fail kunit tests if a lockdep warning is triggered
Date: Tue, 18 Aug 2026 17:20:21 +0200	[thread overview]
Message-ID: <20260818-lockdep-kunit-v1-1-66ceb1a272e3@gmail.com> (raw)
In-Reply-To: <20260818-lockdep-kunit-v1-0-66ceb1a272e3@gmail.com>

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@gmail.com>
---
 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


  reply	other threads:[~2026-08-18 15:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-18 15:20 [PATCH 0/2] rust: kunit: add optional assertions to catch taint and lockdep warnings Malte Wechter
2026-08-18 15:20 ` Malte Wechter [this message]
2026-08-21  8:40   ` [PATCH 1/2] rust: kunit: add config to fail kunit tests if a lockdep warning is triggered Andreas Hindborg
2026-08-18 15:20 ` [PATCH 2/2] rust: kunit: add config to fail kunit if TAINT_WARN is set Malte Wechter
2026-08-21  9:01 ` [PATCH 0/2] rust: kunit: add optional assertions to catch taint and lockdep warnings David Gow
2026-08-21  9:03   ` Miguel Ojeda
2026-08-21 10:18     ` 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=20260818-lockdep-kunit-v1-1-66ceb1a272e3@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox