Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ivan Tverdokhleb" <mbkchelofficialdev@gmail.com>,
	"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>,
	linux-mm@kvack.org
Subject: Re: [PATCH] rust: bug: add warn_once! macro
Date: Wed, 25 Nov 2026 13:27:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-233490@kernel.org> (raw)
In-Reply-To: <20261124132707.101769-1-mbkchelofficialdev@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] rust: bug: warn_once! breaks build when CONFIG_GENERIC_BUG is
  disabled
- [Medium] rust: bug: test_warn_once() always fails with
  suppressed_count == 2
- [Low] rust: bug: condition-only macro is named warn_once! instead of
  warn_on_once!
--

commit a4018fab2b94d6f23a5439e460aa5e9ca9e0714c
Author: Ivan Tverdokhleb <mbkchelofficialdev@gmail.com>

rust: bug: add warn_once! macro

This commit adds a warn_once! macro that sets BUGFLAG_ONCE when reporting a
warning, adds a KUnit test for it, and uses it in VmaRef::zap_vma_range().

> diff --git a/rust/kernel/bug.rs b/rust/kernel/bug.rs
> index 3b40b0bd37b50..a0db5c902ecba 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) => {{

[Severity: Low]
Should this macro be named warn_on_once! rather than warn_once!?  In C,
WARN_ON(cond) and WARN_ON_ONCE(cond) take only a condition, whereas
WARN(cond, fmt, ...) and WARN_ONCE(cond, fmt, ...) take a format string.
Since the existing condition-only macro in this file is warn_on!, naming
this warn_on_once! would stay consistent and leave warn_once! available for
formatted warnings.

> +        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;

[Severity: Medium]
Does referencing $crate::bindings::BUGFLAG_ONCE unconditionally here break
the build when CONFIG_GENERIC_BUG is disabled, such as on CONFIG_UML=y or
CONFIG_BUG=n?

In include/asm-generic/bug.h, BUGFLAG_ONCE is only defined under
#ifdef CONFIG_GENERIC_BUG, so bindgen does not generate bindings::BUGFLAG_ONCE
when CONFIG_GENERIC_BUG is not set, and VmaRef::zap_vma_range() expands
warn_once! unconditionally in the kernel crate.

> +
> +            $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);
> +    }
>  }

[Severity: Medium]
Does test_warn_once() always fail with suppressed_count == 2?

On CONFIG_GENERIC_BUG architectures, __report_bug() checks KUnit warning
suppression and returns before checking BUGFLAG_ONCE or setting
BUGFLAG_DONE:

lib/bug.c:__report_bug() {
	...
	/*
	 * Before the once logic so suppressed warnings do not consume
	 * the single-fire budget of WARN_ON_ONCE().
	 */
	if (warning && kunit_is_suppressed_warning(true))
		return BUG_TRAP_TYPE_WARN;

	disable_trace_on_warning();

	if (warning && once) {
		if (done)
			return BUG_TRAP_TYPE_WARN;

		/*
		 * Since this is the only store, concurrency is not an issue.
		 */
		bug->flags |= BUGFLAG_DONE;
	}
	...
}

And on CONFIG_ARM, CONFIG_LOONGARCH and CONFIG_UML, warn_flags! ignores
$flags and warns on every call.  In both cases, both loop iterations
increment the suppressed warning counter, so assert_eq!(suppressed_count, 1)
fails.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124132707.101769-1-mbkchelofficialdev@gmail.com?part=1


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

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 13:27 [PATCH] rust: bug: add warn_once! macro Ivan Tverdokhleb
2026-11-24 13:51 ` FUJITA Tomonori
2026-11-24 13:54   ` Ivan Tverdokhleb
2026-11-24 16:53 ` kernel test robot
2026-11-25 13:27 ` sashiko-bot [this message]

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=sashiko-outbox-233490@kernel.org \
    --to=sashiko-bot@kernel.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=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=lossin@kernel.org \
    --cc=mbkchelofficialdev@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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