From: "Gary Guo" <gary@garyguo.net>
To: "FUJITA Tomonori" <tomo@flapping.org>, <mbkchelofficialdev@gmail.com>
Cc: <fujita.tomonori@gmail.com>, <ojeda@kernel.org>,
<boqun@kernel.org>, <gary@garyguo.net>,
<bjorn3_gh@protonmail.com>, <lossin@kernel.org>,
<a.hindborg@kernel.org>, <aliceryhl@google.com>,
<tmgross@umich.edu>, <dakr@kernel.org>,
<daniel.almeida@collabora.com>, <tamird@kernel.org>,
<acourbot@nvidia.com>, <work@onurozkan.dev>, <liam@infradead.org>,
<rust-for-linux@vger.kernel.org>, <linux-mm@kvack.org>
Subject: Re: [PATCH] rust: bug: add warn_once! macro
Date: Thu, 26 Nov 2026 15:49:13 +0000 [thread overview]
Message-ID: <DN59EXEWINPV.3UUBOIK6EROAC@garyguo.net> (raw)
In-Reply-To: <20261124.225103.2152350984946270550.tomo@flapping.org>
On Tue Nov 24, 2026 at 1:51 PM GMT, FUJITA Tomonori wrote:
> On Tue, 24 Nov 2026 15:27:07 +0200
> Ivan Tverdokhleb <mbkchelofficialdev@gmail.com> wrote:
>
>> 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.
>
> C WARN_ONCE() takes a format string. This macro is the Rust version
> of WARN_ON_ONCE().
>
>> 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.
>
> C WARN_ON_ONCE() warns only once on these architectures too. I have a
> local warn_on_once!() patch that does the same on all architectures.
> I can post it if needed.
>
> Also, this patch does not build on UML, and test_warn_once fails on
> x86_64.
>
>> --- 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) {
>
> Miguel said that "Erroneous Behavior" cases like this should use
> debug_assert!() plus pr_warn_once!() [1]. Also, the C side uses
> VM_WARN_ON_ONCE() in __zap_vma_range(), which warns only with
> CONFIG_DEBUG_VM.
>
> [1] https://lore.kernel.org/rust-for-linux/CANiq72=tKVD13GD2Bzmt_LnC4SiOX1nkHQPcz2BS=P=b23e0Yw@mail.gmail.com/
For zap_vma_range I think we do actually want WARN_ONCE. For something important
like zapping a VMA range if thngs are used incorrectly we do want to figure out.
A debug_assert is not as useful because it's not enabled on kernel shipped to
end users. A pr_warn_once is not useful because you don't know who's calling the
library and causing issue, and also a dmesg warning is mostly ignored by people
like a WARN.
I think a condition is unlikely to be triggered from invalid userspace inputs,
and the misuse is actually a bug, we should prefer WARN_ON because that's how
can get things fixed.
Best,
Gary
next prev parent reply other threads:[~2026-11-26 15:49 UTC|newest]
Thread overview: 7+ 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-26 15:49 ` Gary Guo [this message]
2026-11-26 17:30 ` Ivan Tverdokhleb
2026-11-24 16:53 ` kernel test robot
2026-11-25 13:27 ` sashiko-bot
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=DN59EXEWINPV.3UUBOIK6EROAC@garyguo.net \
--to=gary@garyguo.net \
--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=liam@infradead.org \
--cc=linux-mm@kvack.org \
--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=tomo@flapping.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.