From: Joel Fernandes <joelagnelf@nvidia.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: linux-kernel@vger.kernel.org, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
acourbot@nvidia.com, "Alistair Popple" <apopple@nvidia.com>,
"Timur Tabi" <ttabi@nvidia.com>,
rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] rust: print: Fix issue with rust_build_error
Date: Mon, 22 Sep 2025 15:15:59 -0400 [thread overview]
Message-ID: <20250922191559.GA2463388@joelbox2> (raw)
In-Reply-To: <CAH5fLggXGMHM1bhmaUb94gDu_LiUbMGugARpOZoUNSTU3phm=g@mail.gmail.com>
On Sun, Sep 21, 2025 at 12:46:26PM +0200, Alice Ryhl wrote:
> On Sat, Sep 20, 2025 at 6:20 PM Joel Fernandes <joelagnelf@nvidia.com> wrote:
> >
> > When printing just before calling io.write32(), modpost fails due to
> > build_assert's missing rust_build_error symbol. The issue is that, the
> > printk arguments are passed as reference in bindings code, thus Rust
> > cannot trust its value and fails to optimize away the build_assert.
> >
> > The issue can be reproduced with the following simple snippet:
> > let offset = 0;
> > pr_err!("{}", offset);
> > io.write32(base, offset);
> >
> > Fix it by just using a closure to call printk. Rust captures the
> > arguments into the closure's arguments thus breaking the dependency.
> > This can be fixed by simply creating a variable alias for each variable
> > however the closure is a simple and concise fix.
> >
> > Another approach with using const-generics for the io.write32 API was
> > investigated, but it cannot work with code that dynamically calculates
> > the write offset.
> >
> > Disassembly of users of pr_err!() with/without patch shows identical
> > code generation, thus the fix has no difference in the final binary.
> >
> > Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com>
>
> The actual bug is that write32 uses build_error!.
I don't think that is the issue, I spoke with Gary and he educated me that
failure of the compiler to do simple compiler optimizations (I am guessing in
this case, dead-code elimination) is a compiler bug. Even in the case where
the write offset is dynamic at runtime, since the caller of write should be
using something like try_write or checking for the offset bounds, the
build_error should be optimized out. Right?
> Trying to change the printing macros is just a band-aid. Someone already
> mentioned that it breaks the ? operator. I think this change is a bad idea.
> We should fix the actual problem, rather than making random changes to
> other parts of the kernel to work around build_error!'s inherent fragility.
I don't think the fragility is in build_error since the direction here is the
bug is in the compiler. So if the compiler optimizes things correctly, we may
conclude that build_error is the correct thing to call. Or is there some
other reason you think build_error is fragile?
As for this patch (and its being a bad idea), I think it was already
mentioned that this patch was not intended for a permanent solution but
rather as a starting point for an investigation. I am probably to take the
blame for not tagging it as 'RFC' though. I encourage people personally to
send patches (whether good or bad ideas) to code I maintain. Lets have mercy
on ideas, they may be bad sometimes but every once in a while, they may
happen to turn out to be good too. How would one know if they didn't try? ;-)
thanks,
- Joel
next prev parent reply other threads:[~2025-09-22 19:16 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-20 16:19 [PATCH] rust: print: Fix issue with rust_build_error Joel Fernandes
2025-09-20 19:34 ` Boqun Feng
2025-09-21 0:53 ` Joel Fernandes
2025-09-20 20:09 ` Benno Lossin
2025-09-21 0:45 ` Joel Fernandes
2025-09-21 7:12 ` Benno Lossin
2025-09-21 9:03 ` Benno Lossin
2025-09-22 10:29 ` Gary Guo
2025-09-22 11:25 ` Miguel Ojeda
2025-09-21 9:13 ` Miguel Ojeda
2025-09-22 19:01 ` Joel Fernandes
2025-09-21 10:46 ` Alice Ryhl
2025-09-22 19:15 ` Joel Fernandes [this message]
2025-09-21 20:56 ` kernel test robot
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=20250922191559.GA2463388@joelbox2 \
--to=joelagnelf@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=apopple@nvidia.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=ttabi@nvidia.com \
/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;
as well as URLs for NNTP newsgroup(s).