Rust for Linux List
 help / color / mirror / Atom feed
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 v2 1/2] rust: bug: make warn_flags! take a once flag
Date: Wed,  2 Dec 2026 10:01:48 +0900	[thread overview]
Message-ID: <20261202010150.1202355-2-tomo@flapping.org> (raw)
In-Reply-To: <20261202010150.1202355-1-tomo@flapping.org>

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 (x86_64, arm64, riscv and s390),
  set BUGFLAG_ONCE in the bug entry. warn_bugflags(), which replaces
  bugflag_taint(), builds the flags.

- On 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.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Tested-by: Ivan Tverdokhleb <mbkchelofficialdev@gmail.com>
---
 rust/kernel/bug.rs | 47 ++++++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
index 3b40b0bd37b5..9efb63e00355 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,9 @@ macro_rules! warn_flags {
             );
         }
     };
+    ($file:expr, true) => {
+        $crate::do_once_lite!($crate::warn_flags!($file, false);)
+    };
 }
 
 #[macro_export]
@@ -108,34 +124,31 @@ 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 +161,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


  reply	other threads:[~2026-12-02  1:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  1:01 [PATCH v2 0/2] rust: bug: support WARN_ON_ONCE() FUJITA Tomonori
2026-12-02  1:01 ` FUJITA Tomonori [this message]
2026-12-02  1:01 ` [PATCH v2 2/2] rust: bug: add warn_on_once! FUJITA Tomonori

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=20261202010150.1202355-2-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