Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Ivan Tverdokhleb <mbkchelofficialdev@gmail.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>,
	Miguel Ojeda <ojeda@kernel.org>
Cc: "Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Liam R . Howlett" <liam@infradead.org>,
	rust-for-linux@vger.kernel.org, linux-mm@kvack.org,
	"Ivan Tverdokhleb" <mbkchelofficialdev@gmail.com>
Subject: [PATCH] rust: bug: add warn_once! macro
Date: Tue, 24 Nov 2026 15:27:07 +0200	[thread overview]
Message-ID: <20261124132707.101769-1-mbkchelofficialdev@gmail.com> (raw)

Add a Rust counterpart of the C WARN_ONCE() macro. It behaves like
warn_on!(), but additionally sets BUGFLAG_ONCE, so the warning is
reported only the first time a given call site triggers.

The macro returns the evaluated condition, like warn_on!(), so it can be
used directly in an `if`.

Use it in Vma::zap_vma_range() to report an invalid range, which
resolves the pending TODO there.

On configurations where warn_flags!() ignores the flags (UML, ARM and
LoongArch), the macro behaves like warn_on!() and warns every time.

Signed-off-by: Ivan Tverdokhleb <mbkchelofficialdev@gmail.com>
---
 rust/kernel/bug.rs     | 46 ++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/mm/virt.rs |  3 +--
 2 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
index 3b40b0bd3..a0db5c902 100644
--- a/rust/kernel/bug.rs
+++ b/rust/kernel/bug.rs
@@ -156,6 +156,27 @@ macro_rules! warn_on {
     }};
 }
 
+/// Report a warning on the first time `cond` is true and return the condition's evaluation result.
+#[macro_export]
+macro_rules! warn_once {
+    ($cond:expr) => {{
+        let cond = $cond;
+
+        #[cfg(CONFIG_DEBUG_BUGVERBOSE_DETAILED)]
+        const COND_STR: &str = concat!("[", stringify!($cond), "] ", file!());
+        #[cfg(not(CONFIG_DEBUG_BUGVERBOSE_DETAILED))]
+        const COND_STR: &str = file!();
+
+        if cond {
+            const WARN_ON_FLAGS: u32 = $crate::bug::bugflag_taint($crate::bindings::TAINT_WARN)
+                | $crate::bindings::BUGFLAG_ONCE;
+
+            $crate::warn_flags!(COND_STR, WARN_ON_FLAGS);
+        }
+        cond
+    }};
+}
+
 #[cfg(CONFIG_RUST_BUG_KUNIT_TEST)]
 #[macros::kunit_tests(rust_kernel_bug)]
 mod tests {
@@ -184,4 +205,29 @@ fn test_warn_on() {
 
         assert_eq!(suppressed_count, 1);
     }
+
+    #[test]
+    fn test_warn_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) };
+
+        for _ in 0..2 {
+            assert!(warn_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);
+    }
 }
diff --git a/rust/kernel/mm/virt.rs b/rust/kernel/mm/virt.rs
index 63eb730b0..567665958 100644
--- a/rust/kernel/mm/virt.rs
+++ b/rust/kernel/mm/virt.rs
@@ -115,8 +115,7 @@ pub fn end(&self) -> usize {
     #[inline]
     pub fn zap_vma_range(&self, address: usize, size: usize) {
         let (end, did_overflow) = address.overflowing_add(size);
-        if did_overflow || address < self.start() || self.end() < end {
-            // TODO: call WARN_ONCE once Rust version of it is added
+        if crate::warn_once!(did_overflow || address < self.start() || self.end() < end) {
             return;
         }
 
-- 
2.56.1



             reply	other threads:[~2026-11-24 13:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 13:27 Ivan Tverdokhleb [this message]
2026-11-24 13:51 ` [PATCH] rust: bug: add warn_once! macro 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=20261124132707.101769-1-mbkchelofficialdev@gmail.com \
    --to=mbkchelofficialdev@gmail.com \
    --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=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=lossin@kernel.org \
    --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