From: FUJITA Tomonori <tomo@flapping.org>
To: ojeda@kernel.org
Cc: a.hindborg@kernel.org, acourbot@nvidia.com, aliceryhl@google.com,
bjorn3_gh@protonmail.com, boqun@kernel.org, dakr@kernel.org,
daniel.almeida@collabora.com, gary@garyguo.net,
lossin@kernel.org, tamird@kernel.org, tmgross@umich.edu,
work@onurozkan.dev, rust-for-linux@vger.kernel.org,
linuxppc-dev@lists.ozlabs.org, mkchauras@gmail.com,
FUJITA Tomonori <fujita.tomonori@gmail.com>
Subject: [PATCH v1] rust: bug: add a KUnit test for warn_on!
Date: Wed, 16 Sep 2026 23:09:52 +0900 [thread overview]
Message-ID: <20260916140952.3290924-1-tomo@flapping.org> (raw)
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
The test runs warn_on!(false) and warn_on!(true) inside a KUnit warning
suppression block, then checks that one warning was counted. The counter
is incremented by the kernel warning path, not by warn_on! itself, so a
count of one means the warning was really reported.
warn_flags! has a different definition per architecture. The test does not
look at any of them, so it runs on every architecture that supports Rust.
The new CONFIG_RUST_BUG_KUNIT_TEST depends on BUG. With CONFIG_BUG=n,
warn_on! does nothing and no warning is counted.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/kernel/Kconfig.test | 11 +++++++++++
rust/kernel/bug.rs | 25 +++++++++++++++++++++++++
2 files changed, 36 insertions(+)
diff --git a/rust/kernel/Kconfig.test b/rust/kernel/Kconfig.test
index e6a5c7a795f0..966b07872ba1 100644
--- a/rust/kernel/Kconfig.test
+++ b/rust/kernel/Kconfig.test
@@ -83,4 +83,15 @@ config RUST_BITFIELD_KUNIT_TEST
If unsure, say N.
+config RUST_BUG_KUNIT_TEST
+ bool "KUnit tests for the Rust BUG/WARN functionality" if !KUNIT_ALL_TESTS
+ depends on BUG
+ default KUNIT_ALL_TESTS
+ help
+ This option enables KUnit tests for the Rust BUG/WARN functionality.
+ These are only for development and testing, not for regular
+ kernel use cases.
+
+ If unsure, say N.
+
endif
diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
index 3566f0234ca4..17adb1b44fe2 100644
--- a/rust/kernel/bug.rs
+++ b/rust/kernel/bug.rs
@@ -152,3 +152,28 @@ macro_rules! warn_on {
cond
}};
}
+
+#[cfg(CONFIG_RUST_BUG_KUNIT_TEST)]
+#[macros::kunit_tests(rust_kernel_bug)]
+mod tests {
+ // The counter is incremented by the kernel warning path, not by `warn_on!`
+ // itself. A count of one means the warning was really reported.
+ #[test]
+ fn test_warn_on() {
+ // SAFETY: `kunit_get_current_test()` is always safe to call (it has
+ // fallbacks for when no KUnit test is running).
+ let test = unsafe { bindings::kunit_get_current_test() };
+ // SAFETY: This function runs only as a KUnit test case, so `test` is a
+ // valid pointer to the running test.
+ let handle = unsafe { bindings::kunit_start_suppress_warning(test) };
+ assert!(!warn_on!(false));
+ assert!(warn_on!(true));
+ // SAFETY: `kunit_suppressed_warning_count()` accepts any value returned by
+ // `kunit_start_suppress_warning()`.
+ let suppressed_count = unsafe { bindings::kunit_suppressed_warning_count(handle) };
+ // SAFETY: `test` is valid as above. `kunit_end_suppress_warning()` accepts any
+ // value returned by `kunit_start_suppress_warning()`.
+ unsafe { bindings::kunit_end_suppress_warning(test, handle) };
+ assert_eq!(suppressed_count, 1);
+ }
+}
base-commit: fd73f4a6659897191fa0d40695fe370925dd3780
--
2.43.0
next reply other threads:[~2026-09-16 14:10 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-16 14:09 FUJITA Tomonori [this message]
2026-10-02 7:07 ` [PATCH v1] rust: bug: add a KUnit test for warn_on! Miguel Ojeda
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=20260916140952.3290924-1-tomo@flapping.org \
--to=tomo@flapping.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=lossin@kernel.org \
--cc=mkchauras@gmail.com \
--cc=ojeda@kernel.org \
--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