Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH v3 0/2] rust: bug: support WARN_ON_ONCE()
@ 2026-12-04 23:19 FUJITA Tomonori
  2026-12-04 23:19 ` [PATCH v3 1/2] rust: bug: make warn_flags! take a once flag FUJITA Tomonori
  2026-12-04 23:19 ` [PATCH v3 2/2] rust: bug: add warn_on_once! FUJITA Tomonori
  0 siblings, 2 replies; 3+ messages in thread
From: FUJITA Tomonori @ 2026-12-04 23:19 UTC (permalink / raw)
  To: aliceryhl, ojeda
  Cc: a.hindborg, acourbot, bjorn3_gh, boqun, dakr, daniel.almeida,
	gary, lossin, mbkchelofficialdev, tamird, tmgross, work,
	rust-for-linux, FUJITA Tomonori

From: FUJITA Tomonori <fujita.tomonori@gmail.com>

This series adds warn_on_once!, the Rust version of C WARN_ON_ONCE().
Alice asked for it for the TODO in VmaRef::zap_vma_range() [1].

Patch 1 makes warn_flags! take a once flag. Patch 2 adds warn_on_once!
and a KUnit test.

Ran the KUnit test on x86_64, UML, ARM and LoongArch.

[1] https://lore.kernel.org/r/20261130141543.3699023-1-aliceryhl@google.com

v3:
- Use braces for the do_once_lite! call (Gary)
- Do not list the architectures that use the bug table in the commit
  message of patch 1 (Alice)
- Add a comment in test_warn_on_once() about why the once behavior is
  not tested (Alice)
v2: https://lore.kernel.org/all/20261202010150.1202355-1-tomo@flapping.org/
- Select the once path at macro expansion time instead of a runtime branch
  (Gary)
- Explain why a WARN_ON_ONCE() C helper does not work (Gary)
v1: https://lore.kernel.org/all/20261130230914.739923-1-tomo@flapping.org/

FUJITA Tomonori (2):
  rust: bug: make warn_flags! take a once flag
  rust: bug: add warn_on_once!

 rust/kernel/bug.rs | 99 ++++++++++++++++++++++++++++++++++++----------
 1 file changed, 79 insertions(+), 20 deletions(-)


base-commit: 891145d7e8a02f22dd7715a331d800e9daece17d
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v3 1/2] rust: bug: make warn_flags! take a once flag
  2026-12-04 23:19 [PATCH v3 0/2] rust: bug: support WARN_ON_ONCE() FUJITA Tomonori
@ 2026-12-04 23:19 ` FUJITA Tomonori
  2026-12-04 23:19 ` [PATCH v3 2/2] rust: bug: add warn_on_once! FUJITA Tomonori
  1 sibling, 0 replies; 3+ messages in thread
From: FUJITA Tomonori @ 2026-12-04 23:19 UTC (permalink / raw)
  To: aliceryhl, ojeda
  Cc: a.hindborg, acourbot, bjorn3_gh, boqun, dakr, daniel.almeida,
	gary, lossin, mbkchelofficialdev, tamird, tmgross, work,
	rust-for-linux, FUJITA Tomonori

From: FUJITA Tomonori <fujita.tomonori@gmail.com>

Prepare for warn_on_once!, the Rust version of C WARN_ON_ONCE().

warn_flags! now takes a bool for "once" instead of bug flags. The once
state is kept as C does:

- When warn_flags! uses the bug table, set BUGFLAG_ONCE in the bug
  entry. warn_bugflags(), which replaces bugflag_taint(), builds the
  flags.

- Otherwise (UML, ARM and LoongArch), use do_once_lite!. A
  WARN_ON_ONCE() C helper does not work. The helper would have only one
  once state, and all Rust callers would share it. After one Rust caller
  warns, no other Rust caller would warn.

warn_on! passes false, so its behavior does not change.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Tested-by: Ivan Tverdokhleb <mbkchelofficialdev@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/kernel/bug.rs | 51 ++++++++++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 18 deletions(-)

diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
index 3b40b0bd37b5..6ca323051dba 100644
--- a/rust/kernel/bug.rs
+++ b/rust/kernel/bug.rs
@@ -6,14 +6,28 @@
 //!
 //! C header: [`include/asm-generic/bug.h`](srctree/include/asm-generic/bug.h)
 
+#[doc(hidden)]
+#[cfg(not(testlib))]
+#[cfg(all(CONFIG_BUG, not(CONFIG_UML), not(CONFIG_LOONGARCH), not(CONFIG_ARM)))]
+pub const fn warn_bugflags(once: bool) -> u32 {
+    // Same as `BUGFLAG_WARNING | BUGFLAG_TAINT(TAINT_WARN)` in C.
+    let flags = bindings::BUGFLAG_WARNING | (bindings::TAINT_WARN << 8);
+
+    if once {
+        flags | bindings::BUGFLAG_ONCE
+    } else {
+        flags
+    }
+}
+
 #[macro_export]
 #[doc(hidden)]
 #[cfg(not(testlib))]
 #[cfg(all(CONFIG_BUG, not(CONFIG_UML), not(CONFIG_LOONGARCH), not(CONFIG_ARM)))]
 #[cfg(CONFIG_DEBUG_BUGVERBOSE)]
 macro_rules! warn_flags {
-    ($file:expr, $flags:expr) => {
-        const FLAGS: u32 = $crate::bindings::BUGFLAG_WARNING | $flags;
+    ($file:expr, $once:expr) => {
+        const FLAGS: u32 = $crate::bug::warn_bugflags($once);
         const _FILE: &[u8] = $file.as_bytes();
         // Plus one for null-terminator.
         static FILE: [u8; _FILE.len() + 1] = {
@@ -52,8 +66,8 @@ macro_rules! warn_flags {
 #[cfg(all(CONFIG_BUG, not(CONFIG_UML), not(CONFIG_LOONGARCH), not(CONFIG_ARM)))]
 #[cfg(not(CONFIG_DEBUG_BUGVERBOSE))]
 macro_rules! warn_flags {
-    ($file:expr, $flags:expr) => {
-        const FLAGS: u32 = $crate::bindings::BUGFLAG_WARNING | $flags;
+    ($file:expr, $once:expr) => {
+        const FLAGS: u32 = $crate::bug::warn_bugflags($once);
 
         if false {
             _ = $file;
@@ -82,10 +96,9 @@ macro_rules! warn_flags {
 #[cfg(not(testlib))]
 #[cfg(all(CONFIG_BUG, CONFIG_UML))]
 macro_rules! warn_flags {
-    ($file:expr, $flags:expr) => {
+    ($file:expr, false) => {
         if false {
             _ = $file;
-            _ = $flags;
         }
 
         // SAFETY:
@@ -101,6 +114,11 @@ macro_rules! warn_flags {
             );
         }
     };
+    ($file:expr, true) => {
+        $crate::do_once_lite! {
+            $crate::warn_flags!($file, false);
+        }
+    };
 }
 
 #[macro_export]
@@ -108,34 +126,33 @@ macro_rules! warn_flags {
 #[cfg(not(testlib))]
 #[cfg(all(CONFIG_BUG, any(CONFIG_LOONGARCH, CONFIG_ARM)))]
 macro_rules! warn_flags {
-    ($file:expr, $flags:expr) => {
+    ($file:expr, false) => {
         if false {
             _ = $file;
-            _ = $flags;
         }
 
         // SAFETY: It is always safe to call `WARN_ON()`.
         unsafe { $crate::bindings::WARN_ON(true) }
     };
+    ($file:expr, true) => {
+        $crate::do_once_lite! {
+            $crate::warn_flags!($file, false);
+        }
+    };
 }
 
 #[macro_export]
 #[doc(hidden)]
 #[cfg(any(testlib, not(CONFIG_BUG)))]
 macro_rules! warn_flags {
-    ($file:expr, $flags:expr) => {
+    ($file:expr, $once:expr) => {
         if false {
             _ = $file;
-            _ = $flags;
+            _ = $once;
         }
     };
 }
 
-#[doc(hidden)]
-pub const fn bugflag_taint(value: u32) -> u32 {
-    value << 8
-}
-
 /// Report a warning if `cond` is true and return the condition's evaluation result.
 #[macro_export]
 macro_rules! warn_on {
@@ -148,9 +165,7 @@ macro_rules! warn_on {
         const COND_STR: &str = file!();
 
         if cond {
-            const WARN_ON_FLAGS: u32 = $crate::bug::bugflag_taint($crate::bindings::TAINT_WARN);
-
-            $crate::warn_flags!(COND_STR, WARN_ON_FLAGS);
+            $crate::warn_flags!(COND_STR, false);
         }
         cond
     }};
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v3 2/2] rust: bug: add warn_on_once!
  2026-12-04 23:19 [PATCH v3 0/2] rust: bug: support WARN_ON_ONCE() FUJITA Tomonori
  2026-12-04 23:19 ` [PATCH v3 1/2] rust: bug: make warn_flags! take a once flag FUJITA Tomonori
@ 2026-12-04 23:19 ` FUJITA Tomonori
  1 sibling, 0 replies; 3+ messages in thread
From: FUJITA Tomonori @ 2026-12-04 23:19 UTC (permalink / raw)
  To: aliceryhl, ojeda
  Cc: a.hindborg, acourbot, bjorn3_gh, boqun, dakr, daniel.almeida,
	gary, lossin, mbkchelofficialdev, tamird, tmgross, work,
	rust-for-linux, FUJITA Tomonori

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.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Tested-by: Ivan Tverdokhleb <mbkchelofficialdev@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/kernel/bug.rs | 52 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 48 insertions(+), 4 deletions(-)

diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
index 6ca323051dba..b675b0654431 100644
--- a/rust/kernel/bug.rs
+++ b/rust/kernel/bug.rs
@@ -153,10 +153,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:tt) => {{
         let cond = $cond;
 
         #[cfg(CONFIG_DEBUG_BUGVERBOSE_DETAILED)]
@@ -165,12 +165,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 {
@@ -199,4 +216,31 @@ 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) };
+
+        // The once behavior is not tested. With the bug table, a suppressed
+        // warning does not set `BUGFLAG_DONE`, so a second call is counted
+        // again.
+        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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04 23:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 23:19 [PATCH v3 0/2] rust: bug: support WARN_ON_ONCE() FUJITA Tomonori
2026-12-04 23:19 ` [PATCH v3 1/2] rust: bug: make warn_flags! take a once flag FUJITA Tomonori
2026-12-04 23:19 ` [PATCH v3 2/2] rust: bug: add warn_on_once! FUJITA Tomonori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox