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 v1 1/2] rust: bug: make warn_flags! take a once flag
Date: Tue,  1 Dec 2026 08:09:13 +0900	[thread overview]
Message-ID: <20261130230914.739923-2-tomo@flapping.org> (raw)
In-Reply-To: <20261130230914.739923-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 and ARM, use do_once_lite!, as C does with DO_ONCE_LITE_IF().
  LoongArch uses it too, since Rust calls a C helper there. A
  WARN_ON_ONCE() helper would share one once state among all Rust
  callers.

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

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/kernel/bug.rs | 83 +++++++++++++++++++++++++++++-----------------
 1 file changed, 52 insertions(+), 31 deletions(-)

diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
index 3b40b0bd37b5..4c0120314cce 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,23 +96,30 @@ macro_rules! warn_flags {
 #[cfg(not(testlib))]
 #[cfg(all(CONFIG_BUG, CONFIG_UML))]
 macro_rules! warn_flags {
-    ($file:expr, $flags:expr) => {
+    ($file:expr, $once:expr) => {
         if false {
             _ = $file;
-            _ = $flags;
         }
 
-        // SAFETY:
-        // - The file name is a null-terminated string with `'static` lifetime.
-        // - `TAINT_WARN` is a valid taint number.
-        // - `fmt` can be NULL, and NULL is passed.
-        unsafe {
-            $crate::bindings::warn_slowpath_fmt(
-                $crate::str::CStrExt::as_char_ptr($crate::c_str!(::core::file!())),
-                line!() as $crate::ffi::c_int,
-                $crate::bindings::TAINT_WARN,
-                ::core::ptr::null(),
-            );
+        let warn = || {
+            // SAFETY:
+            // - The file name is a null-terminated string with `'static` lifetime.
+            // - `TAINT_WARN` is a valid taint number.
+            // - `fmt` can be NULL, and NULL is passed.
+            unsafe {
+                $crate::bindings::warn_slowpath_fmt(
+                    $crate::str::CStrExt::as_char_ptr($crate::c_str!(::core::file!())),
+                    line!() as $crate::ffi::c_int,
+                    $crate::bindings::TAINT_WARN,
+                    ::core::ptr::null(),
+                );
+            }
+        };
+
+        if $once {
+            $crate::do_once_lite!(warn())
+        } else {
+            warn()
         }
     };
 }
@@ -108,14 +129,21 @@ 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, $once:expr) => {
         if false {
             _ = $file;
-            _ = $flags;
         }
 
-        // SAFETY: It is always safe to call `WARN_ON()`.
-        unsafe { $crate::bindings::WARN_ON(true) }
+        let warn = || {
+            // SAFETY: It is always safe to call `WARN_ON()`.
+            unsafe { $crate::bindings::WARN_ON(true) };
+        };
+
+        if $once {
+            $crate::do_once_lite!(warn())
+        } else {
+            warn()
+        }
     };
 }
 
@@ -123,19 +151,14 @@ macro_rules! warn_flags {
 #[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 +171,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-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 ` FUJITA Tomonori [this message]
2026-11-30 23:09 ` [PATCH v1 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=20261130230914.739923-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