* [PATCH v1 0/2] rust: bug: support WARN_ON_ONCE()
@ 2026-11-30 23:09 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 ` [PATCH v1 2/2] rust: bug: add warn_on_once! FUJITA Tomonori
0 siblings, 2 replies; 3+ messages in thread
From: FUJITA Tomonori @ 2026-11-30 23:09 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
FUJITA Tomonori (2):
rust: bug: make warn_flags! take a once flag
rust: bug: add warn_on_once!
rust/kernel/bug.rs | 128 +++++++++++++++++++++++++++++++++------------
1 file changed, 95 insertions(+), 33 deletions(-)
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.43.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v1 1/2] rust: bug: make warn_flags! take a once flag
2026-11-30 23:09 [PATCH v1 0/2] rust: bug: support WARN_ON_ONCE() FUJITA Tomonori
@ 2026-11-30 23:09 ` FUJITA Tomonori
2026-11-30 23:09 ` [PATCH v1 2/2] rust: bug: add warn_on_once! FUJITA Tomonori
1 sibling, 0 replies; 3+ messages in thread
From: FUJITA Tomonori @ 2026-11-30 23:09 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 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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v1 2/2] rust: bug: add warn_on_once!
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
1 sibling, 0 replies; 3+ messages in thread
From: FUJITA Tomonori @ 2026-11-30 23:09 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>
---
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 23:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v1 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