* [PATCH v2 0/2] rust: bug: support WARN_ON_ONCE()
@ 2026-12-02 1:01 FUJITA Tomonori
2026-12-02 1:01 ` [PATCH v2 1/2] rust: bug: make warn_flags! take a once flag FUJITA Tomonori
2026-12-02 1:01 ` [PATCH v2 2/2] rust: bug: add warn_on_once! FUJITA Tomonori
0 siblings, 2 replies; 3+ messages in thread
From: FUJITA Tomonori @ 2026-12-02 1:01 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
v2:
- 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 | 92 ++++++++++++++++++++++++++++++++++++----------
1 file changed, 72 insertions(+), 20 deletions(-)
base-commit: 82d8023ec40420ed61d7a96a173ef862741f1753
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 1/2] rust: bug: make warn_flags! take a once flag
2026-12-02 1:01 [PATCH v2 0/2] rust: bug: support WARN_ON_ONCE() FUJITA Tomonori
@ 2026-12-02 1:01 ` FUJITA Tomonori
2026-12-02 1:01 ` [PATCH v2 2/2] rust: bug: add warn_on_once! FUJITA Tomonori
1 sibling, 0 replies; 3+ messages in thread
From: FUJITA Tomonori @ 2026-12-02 1:01 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 (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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2 2/2] rust: bug: add warn_on_once!
2026-12-02 1:01 [PATCH v2 0/2] rust: bug: support WARN_ON_ONCE() FUJITA Tomonori
2026-12-02 1:01 ` [PATCH v2 1/2] rust: bug: make warn_flags! take a once flag FUJITA Tomonori
@ 2026-12-02 1:01 ` FUJITA Tomonori
1 sibling, 0 replies; 3+ messages in thread
From: FUJITA Tomonori @ 2026-12-02 1:01 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.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Tested-by: Ivan Tverdokhleb <mbkchelofficialdev@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 9efb63e00355..add9fbfa4fc5 100644
--- a/rust/kernel/bug.rs
+++ b/rust/kernel/bug.rs
@@ -149,10 +149,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)]
@@ -161,12 +161,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 {
@@ -195,4 +212,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
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-02 1:03 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 1:01 [PATCH v2 0/2] rust: bug: support WARN_ON_ONCE() FUJITA Tomonori
2026-12-02 1:01 ` [PATCH v2 1/2] rust: bug: make warn_flags! take a once flag FUJITA Tomonori
2026-12-02 1:01 ` [PATCH v2 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