From: FUJITA Tomonori <tomo@flapping.org>
To: aliceryhl@google.com, ojeda@kernel.org
Cc: a.hindborg@kernel.org, acourbot@nvidia.com,
bjorn3_gh@protonmail.com, boqun@kernel.org, dakr@kernel.org,
daniel.almeida@collabora.com, gary@garyguo.net,
lossin@kernel.org, mbkchelofficialdev@gmail.com,
tamird@kernel.org, tmgross@umich.edu, work@onurozkan.dev,
rust-for-linux@vger.kernel.org,
FUJITA Tomonori <fujita.tomonori@gmail.com>
Subject: [PATCH v1 2/2] rust: bug: add warn_on_once!
Date: Tue, 1 Dec 2026 08:09:14 +0900 [thread overview]
Message-ID: <20261130230914.739923-3-tomo@flapping.org> (raw)
In-Reply-To: <20261130230914.739923-1-tomo@flapping.org>
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
Add warn_on_once!, the Rust version of C WARN_ON_ONCE(). It reports a
warning only the first time the condition is true, and it returns the
condition every time.
Move the body of warn_on! to warn_inner!, which takes the once flag, and
make warn_on! and warn_on_once! call it.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/kernel/bug.rs | 49 ++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 45 insertions(+), 4 deletions(-)
diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
index 4c0120314cce..9c32747f4cbe 100644
--- a/rust/kernel/bug.rs
+++ b/rust/kernel/bug.rs
@@ -159,10 +159,10 @@ macro_rules! warn_flags {
};
}
-/// Report a warning if `cond` is true and return the condition's evaluation result.
+#[doc(hidden)]
#[macro_export]
-macro_rules! warn_on {
- ($cond:expr) => {{
+macro_rules! warn_inner {
+ ($cond:expr, $once:expr) => {{
let cond = $cond;
#[cfg(CONFIG_DEBUG_BUGVERBOSE_DETAILED)]
@@ -171,12 +171,29 @@ macro_rules! warn_on {
const COND_STR: &str = file!();
if cond {
- $crate::warn_flags!(COND_STR, false);
+ $crate::warn_flags!(COND_STR, $once);
}
cond
}};
}
+/// Report a warning if `cond` is true and return the condition's evaluation result.
+#[macro_export]
+macro_rules! warn_on {
+ ($cond:expr) => {
+ $crate::warn_inner!($cond, false)
+ };
+}
+
+/// Report a warning the first time `cond` is true, and return the condition's
+/// evaluation result every time.
+#[macro_export]
+macro_rules! warn_on_once {
+ ($cond:expr) => {
+ $crate::warn_inner!($cond, true)
+ };
+}
+
#[cfg(CONFIG_RUST_BUG_KUNIT_TEST)]
#[macros::kunit_tests(rust_kernel_bug)]
mod tests {
@@ -205,4 +222,28 @@ fn test_warn_on() {
assert_eq!(suppressed_count, 1);
}
+
+ #[test]
+ fn test_warn_on_once() {
+ // 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_once!(false));
+ assert!(warn_on_once!(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);
+ }
}
--
2.43.0
prev parent reply other threads:[~2026-11-30 23:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 23:09 [PATCH v1 0/2] rust: bug: support WARN_ON_ONCE() FUJITA Tomonori
2026-11-30 23:09 ` [PATCH v1 1/2] rust: bug: make warn_flags! take a once flag FUJITA Tomonori
2026-11-30 23:09 ` FUJITA Tomonori [this message]
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=20261130230914.739923-3-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=lossin@kernel.org \
--cc=mbkchelofficialdev@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