All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Edwin Peer" <epeer@nvidia.com>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"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>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Viresh Kumar" <viresh.kumar@linaro.org>,
	"Will Deacon" <will@kernel.org>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Mark Rutland" <mark.rutland@arm.com>
Cc: <rust-for-linux@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-pm@vger.kernel.org>
Subject: Re: [PATCH v2 1/7] rust: build_assert: add instructions for use with function arguments
Date: Wed, 03 Dec 2025 12:18:34 +0900	[thread overview]
Message-ID: <DEO995E7PYDL.WLIHRO9TPA7P@nvidia.com> (raw)
In-Reply-To: <10bd4b34-f28d-45cb-8d6c-8383cd63b56b@nvidia.com>

On Tue Dec 2, 2025 at 4:53 AM JST, Edwin Peer wrote:
> On 11/27/25 18:11, Alexandre Courbot wrote:
>
>> `build_assert` relies on the compiler to optimize out its error path,
>> lest build fails with the dreaded error:
>>
>>     ERROR: modpost: "rust_build_error" [path/to/module.ko] undefined!
>>
>> It has been observed that very trivial code performing I/O accesses
>> (sometimes even using an immediate value) would seemingly randomly fail
>> with this error whenever `CLIPPY=1` was set. The same behavior was also
>> observed until different, very similar conditions [1][2].
>>
>> The cause appears to be that the failing function is eventually using
>> `build_assert` with its argument, but is only annotated with
>> `#[inline]`. This gives the compiler freedom to not inline the function,
>> which it notably did when Clippy was active, triggering the error.
>>
>> The fix is to annotate functions passing their argument to
>> `build_assert` with `#[inline(always)]`, telling the compiler to be as
>> aggressive as possible with their inlining. This is also the correct
>> behavior as inlining is mandatory for correct behavior in these cases.
>>
>> Add a paragraph instructing to annotate such functions with
>> `#[inline(always)]` in `build_assert`'s documentation, and split its
>> example to illustrate.
>>
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>>  rust/kernel/build_assert.rs | 7 ++++++-
>>  1 file changed, 6 insertions(+), 1 deletion(-)
>>
>> diff --git a/rust/kernel/build_assert.rs b/rust/kernel/build_assert.rs
>> index 6331b15d7c4d..f8124dbc663f 100644
>> --- a/rust/kernel/build_assert.rs
>> +++ b/rust/kernel/build_assert.rs
>> @@ -61,8 +61,13 @@ macro_rules! build_error {
>>  ///     build_assert!(N > 1); // Build-time check
>>  ///     assert!(N > 1); // Run-time check
>>  /// }
>> +/// ```
>>  ///
>> -/// #[inline]
>> +/// When a condition depends on a function argument, the function must be annotated with
>> +/// `#[inline(always)]`. Without this attribute, the compiler may choose to not inline the
>> +/// function, preventing it from optimizing out the error path.
>> +/// ```
>> +/// #[inline(always)]
>
> The compiler may still choose to not inline the function, even under
> `#[inline(always)]`:
>
> "#[inline(always)] suggests that inline expansion should always be
> performed." [1] 
>
> "Note: In every form the attribute is a hint. The compiler may ignore
> it." [also 1]

This is true, but AFAICT this is the best we can do as of today (and in
practice it thankfully never failed so far). If there is a more reliable
way to always inline functions we should definitely use that though.

  reply	other threads:[~2025-12-03  3:18 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-28  2:11 [PATCH v2 0/7] rust: build_assert: document and fix use with function arguments Alexandre Courbot
2025-11-28  2:11 ` [PATCH v2 1/7] rust: build_assert: add instructions for " Alexandre Courbot
2025-11-30 21:44   ` John Hubbard
2025-11-30 21:56     ` Miguel Ojeda
2025-11-30 22:00       ` John Hubbard
2025-11-30 22:42         ` Miguel Ojeda
2025-12-01  0:52           ` John Hubbard
2025-12-01  3:44             ` Miguel Ojeda
2025-12-01  4:36               ` John Hubbard
2025-12-01 16:43                 ` Miguel Ojeda
2025-12-01 19:31                   ` John Hubbard
2025-12-01 23:06                     ` Miguel Ojeda
2025-12-02  1:38                       ` John Hubbard
2025-12-01 19:53   ` Edwin Peer
2025-12-03  3:18     ` Alexandre Courbot [this message]
2025-11-28  2:11 ` [PATCH v2 2/7] rust: io: always inline functions using build_assert with arguments Alexandre Courbot
2025-12-01 20:06   ` Edwin Peer
2025-12-02 10:14     ` Alice Ryhl
2025-11-28  2:11 ` [PATCH v2 3/7] rust: cpufreq: " Alexandre Courbot
2025-11-28  6:12   ` Viresh Kumar
2025-11-28  9:32     ` Alice Ryhl
2025-11-28  9:55       ` Viresh Kumar
2025-11-28  2:11 ` [PATCH v2 4/7] rust: bits: " Alexandre Courbot
2025-11-28  2:11 ` [PATCH v2 5/7] rust: sync: refcount: " Alexandre Courbot
2025-11-28  2:11 ` [PATCH v2 6/7] rust: irq: " Alexandre Courbot
2025-11-28  2:11 ` [PATCH v2 7/7] rust: num: bounded: add missing comment for always inlined function Alexandre Courbot
2025-11-28 14:02 ` [PATCH v2 0/7] rust: build_assert: document and fix use with function arguments Daniel Almeida
2025-11-30 16:09 ` Daniel Almeida
2025-11-30 17:48   ` Miguel Ojeda
2025-12-03  3:19     ` Alexandre Courbot
2025-12-03 18:37       ` Miguel Ojeda

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=DEO995E7PYDL.WLIHRO9TPA7P@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=epeer@nvidia.com \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=viresh.kumar@linaro.org \
    --cc=will@kernel.org \
    /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.