[PATCH] rust: io: always inline methods leading to build_assert

Alexandre Courbot posted 1 patch 4 days, 8 hours ago
rust/kernel/io.rs | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
[PATCH] rust: io: always inline methods leading to build_assert
Posted by Alexandre Courbot 4 days, 8 hours ago
`build_assert` relies on the compiler to optimize out its error path,
lest build fails with the dreaded error:

    ERROR: modpost: "rust_build_error" [drivers/gpu/nova-core/nova_core.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. Removing the CLIPPY option
makes the error go away, but that's obviously not a great workaround.
Clippy appears to influence the way the compiler optimizes things,
making it on occasion generate a method where we would need it to inline
in order to satisfy a `build_assert`.

Fix this by instructing the compiler to always inline the methods
leading to `build_assert`. This stronger directive is effective even
when `CLIPPY=1` is specified, which gets rid of this error.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
This is the same fix as for another build error triggered by the use of
`build_assert` [1], which signals that all callers of this macro should
all be tagged with `#[inline(always)]`, as inlining is a requirement for
`build_assert` to perform properly anyway.

[1] https://lore.kernel.org/all/DEEUYUOAEZU3.1J1HM2YQ10EX1@nvidia.com/
---
 rust/kernel/io.rs | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index 98e8b84e68d1..f161ec8056ce 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -142,7 +142,8 @@ macro_rules! define_read {
         /// Bound checks are performed on compile time, hence if the offset is not known at compile
         /// time, the build will fail.
         $(#[$attr])*
-        #[inline]
+        // Always inline so the error path of `io_addr_assert` is optimized out.
+        #[inline(always)]
         pub fn $name(&self, offset: usize) -> $type_name {
             let addr = self.io_addr_assert::<$type_name>(offset);
 
@@ -171,7 +172,8 @@ macro_rules! define_write {
         /// Bound checks are performed on compile time, hence if the offset is not known at compile
         /// time, the build will fail.
         $(#[$attr])*
-        #[inline]
+        // Always inline so the error path of `io_addr_assert` is optimized out.
+        #[inline(always)]
         pub fn $name(&self, value: $type_name, offset: usize) {
             let addr = self.io_addr_assert::<$type_name>(offset);
 
@@ -239,7 +241,8 @@ fn io_addr<U>(&self, offset: usize) -> Result<usize> {
         self.addr().checked_add(offset).ok_or(EINVAL)
     }
 
-    #[inline]
+    // Always inline so the error path of `build_assert!` is optimized out.
+    #[inline(always)]
     fn io_addr_assert<U>(&self, offset: usize) -> usize {
         build_assert!(Self::offset_valid::<U>(offset, SIZE));
 

---
base-commit: ea34511aaf755349999a1067b2984a541bee1492
change-id: 20251127-io-build-assert-3579a5bfb81c

Best regards,
-- 
Alexandre Courbot <acourbot@nvidia.com>
Re: [PATCH] rust: io: always inline methods leading to build_assert
Posted by Daniel Almeida 4 days, 6 hours ago

> On 27 Nov 2025, at 10:30, Alexandre Courbot <acourbot@nvidia.com> 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" [drivers/gpu/nova-core/nova_core.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. Removing the CLIPPY option
> makes the error go away, but that's obviously not a great workaround.
> Clippy appears to influence the way the compiler optimizes things,
> making it on occasion generate a method where we would need it to inline
> in order to satisfy a `build_assert`.
> 
> Fix this by instructing the compiler to always inline the methods
> leading to `build_assert`. This stronger directive is effective even
> when `CLIPPY=1` is specified, which gets rid of this error.
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> This is the same fix as for another build error triggered by the use of
> `build_assert` [1], which signals that all callers of this macro should
> all be tagged with `#[inline(always)]`, as inlining is a requirement for
> `build_assert` to perform properly anyway.
> 
> [1] https://lore.kernel.org/all/DEEUYUOAEZU3.1J1HM2YQ10EX1@nvidia.com/
> ---
> rust/kernel/io.rs | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index 98e8b84e68d1..f161ec8056ce 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -142,7 +142,8 @@ macro_rules! define_read {
>         /// Bound checks are performed on compile time, hence if the offset is not known at compile
>         /// time, the build will fail.
>         $(#[$attr])*
> -        #[inline]
> +        // Always inline so the error path of `io_addr_assert` is optimized out.
> +        #[inline(always)]
>         pub fn $name(&self, offset: usize) -> $type_name {
>             let addr = self.io_addr_assert::<$type_name>(offset);
> 
> @@ -171,7 +172,8 @@ macro_rules! define_write {
>         /// Bound checks are performed on compile time, hence if the offset is not known at compile
>         /// time, the build will fail.
>         $(#[$attr])*
> -        #[inline]
> +        // Always inline so the error path of `io_addr_assert` is optimized out.
> +        #[inline(always)]
>         pub fn $name(&self, value: $type_name, offset: usize) {
>             let addr = self.io_addr_assert::<$type_name>(offset);
> 
> @@ -239,7 +241,8 @@ fn io_addr<U>(&self, offset: usize) -> Result<usize> {
>         self.addr().checked_add(offset).ok_or(EINVAL)
>     }
> 
> -    #[inline]
> +    // Always inline so the error path of `build_assert!` is optimized out.
> +    #[inline(always)]
>     fn io_addr_assert<U>(&self, offset: usize) -> usize {
>         build_assert!(Self::offset_valid::<U>(offset, SIZE));
> 
> 
> ---
> base-commit: ea34511aaf755349999a1067b2984a541bee1492
> change-id: 20251127-io-build-assert-3579a5bfb81c
> 
> Best regards,
> -- 
> Alexandre Courbot <acourbot@nvidia.com>
> 
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>

I also faced this with genmask, by the way, i.e.: using genmask with an
in-bounds constant would trigger a build error. Very confusingly, this would be
randomly solved by moving the genmask invocation around in the code.

I wonder if the same fix is needed for it as well?

— Daniel
Re: [PATCH] rust: io: always inline methods leading to build_assert
Posted by Alexandre Courbot 3 days, 21 hours ago
On Thu Nov 27, 2025 at 11:53 PM JST, Daniel Almeida wrote:
>
>
>> On 27 Nov 2025, at 10:30, Alexandre Courbot <acourbot@nvidia.com> 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" [drivers/gpu/nova-core/nova_core.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. Removing the CLIPPY option
>> makes the error go away, but that's obviously not a great workaround.
>> Clippy appears to influence the way the compiler optimizes things,
>> making it on occasion generate a method where we would need it to inline
>> in order to satisfy a `build_assert`.
>> 
>> Fix this by instructing the compiler to always inline the methods
>> leading to `build_assert`. This stronger directive is effective even
>> when `CLIPPY=1` is specified, which gets rid of this error.
>> 
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>> This is the same fix as for another build error triggered by the use of
>> `build_assert` [1], which signals that all callers of this macro should
>> all be tagged with `#[inline(always)]`, as inlining is a requirement for
>> `build_assert` to perform properly anyway.
>> 
>> [1] https://lore.kernel.org/all/DEEUYUOAEZU3.1J1HM2YQ10EX1@nvidia.com/
>> ---
>> rust/kernel/io.rs | 9 ++++++---
>> 1 file changed, 6 insertions(+), 3 deletions(-)
>> 
>> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
>> index 98e8b84e68d1..f161ec8056ce 100644
>> --- a/rust/kernel/io.rs
>> +++ b/rust/kernel/io.rs
>> @@ -142,7 +142,8 @@ macro_rules! define_read {
>>         /// Bound checks are performed on compile time, hence if the offset is not known at compile
>>         /// time, the build will fail.
>>         $(#[$attr])*
>> -        #[inline]
>> +        // Always inline so the error path of `io_addr_assert` is optimized out.
>> +        #[inline(always)]
>>         pub fn $name(&self, offset: usize) -> $type_name {
>>             let addr = self.io_addr_assert::<$type_name>(offset);
>> 
>> @@ -171,7 +172,8 @@ macro_rules! define_write {
>>         /// Bound checks are performed on compile time, hence if the offset is not known at compile
>>         /// time, the build will fail.
>>         $(#[$attr])*
>> -        #[inline]
>> +        // Always inline so the error path of `io_addr_assert` is optimized out.
>> +        #[inline(always)]
>>         pub fn $name(&self, value: $type_name, offset: usize) {
>>             let addr = self.io_addr_assert::<$type_name>(offset);
>> 
>> @@ -239,7 +241,8 @@ fn io_addr<U>(&self, offset: usize) -> Result<usize> {
>>         self.addr().checked_add(offset).ok_or(EINVAL)
>>     }
>> 
>> -    #[inline]
>> +    // Always inline so the error path of `build_assert!` is optimized out.
>> +    #[inline(always)]
>>     fn io_addr_assert<U>(&self, offset: usize) -> usize {
>>         build_assert!(Self::offset_valid::<U>(offset, SIZE));
>> 
>> 
>> ---
>> base-commit: ea34511aaf755349999a1067b2984a541bee1492
>> change-id: 20251127-io-build-assert-3579a5bfb81c
>> 
>> Best regards,
>> -- 
>> Alexandre Courbot <acourbot@nvidia.com>
>> 
>> 
>
> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
>
> I also faced this with genmask, by the way, i.e.: using genmask with an
> in-bounds constant would trigger a build error. Very confusingly, this would be
> randomly solved by moving the genmask invocation around in the code.
>
> I wonder if the same fix is needed for it as well?

What you described is exactly the symptoms I was experimenting (notably
the "moving around sometimes fixes it" thing) so yeah, I think this also
applies to `bit_*` and `genmask_*` (and anything that invokes
`build_assert`, really).
Re: [PATCH] rust: io: always inline methods leading to build_assert
Posted by Alexandre Courbot 3 days, 20 hours ago
On Fri Nov 28, 2025 at 9:27 AM JST, Alexandre Courbot wrote:
> On Thu Nov 27, 2025 at 11:53 PM JST, Daniel Almeida wrote:
>>
>>
>>> On 27 Nov 2025, at 10:30, Alexandre Courbot <acourbot@nvidia.com> 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" [drivers/gpu/nova-core/nova_core.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. Removing the CLIPPY option
>>> makes the error go away, but that's obviously not a great workaround.
>>> Clippy appears to influence the way the compiler optimizes things,
>>> making it on occasion generate a method where we would need it to inline
>>> in order to satisfy a `build_assert`.
>>> 
>>> Fix this by instructing the compiler to always inline the methods
>>> leading to `build_assert`. This stronger directive is effective even
>>> when `CLIPPY=1` is specified, which gets rid of this error.
>>> 
>>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>>> ---
>>> This is the same fix as for another build error triggered by the use of
>>> `build_assert` [1], which signals that all callers of this macro should
>>> all be tagged with `#[inline(always)]`, as inlining is a requirement for
>>> `build_assert` to perform properly anyway.
>>> 
>>> [1] https://lore.kernel.org/all/DEEUYUOAEZU3.1J1HM2YQ10EX1@nvidia.com/
>>> ---
>>> rust/kernel/io.rs | 9 ++++++---
>>> 1 file changed, 6 insertions(+), 3 deletions(-)
>>> 
>>> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
>>> index 98e8b84e68d1..f161ec8056ce 100644
>>> --- a/rust/kernel/io.rs
>>> +++ b/rust/kernel/io.rs
>>> @@ -142,7 +142,8 @@ macro_rules! define_read {
>>>         /// Bound checks are performed on compile time, hence if the offset is not known at compile
>>>         /// time, the build will fail.
>>>         $(#[$attr])*
>>> -        #[inline]
>>> +        // Always inline so the error path of `io_addr_assert` is optimized out.
>>> +        #[inline(always)]
>>>         pub fn $name(&self, offset: usize) -> $type_name {
>>>             let addr = self.io_addr_assert::<$type_name>(offset);
>>> 
>>> @@ -171,7 +172,8 @@ macro_rules! define_write {
>>>         /// Bound checks are performed on compile time, hence if the offset is not known at compile
>>>         /// time, the build will fail.
>>>         $(#[$attr])*
>>> -        #[inline]
>>> +        // Always inline so the error path of `io_addr_assert` is optimized out.
>>> +        #[inline(always)]
>>>         pub fn $name(&self, value: $type_name, offset: usize) {
>>>             let addr = self.io_addr_assert::<$type_name>(offset);
>>> 
>>> @@ -239,7 +241,8 @@ fn io_addr<U>(&self, offset: usize) -> Result<usize> {
>>>         self.addr().checked_add(offset).ok_or(EINVAL)
>>>     }
>>> 
>>> -    #[inline]
>>> +    // Always inline so the error path of `build_assert!` is optimized out.
>>> +    #[inline(always)]
>>>     fn io_addr_assert<U>(&self, offset: usize) -> usize {
>>>         build_assert!(Self::offset_valid::<U>(offset, SIZE));
>>> 
>>> 
>>> ---
>>> base-commit: ea34511aaf755349999a1067b2984a541bee1492
>>> change-id: 20251127-io-build-assert-3579a5bfb81c
>>> 
>>> Best regards,
>>> -- 
>>> Alexandre Courbot <acourbot@nvidia.com>
>>> 
>>> 
>>
>> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
>>
>> I also faced this with genmask, by the way, i.e.: using genmask with an
>> in-bounds constant would trigger a build error. Very confusingly, this would be
>> randomly solved by moving the genmask invocation around in the code.
>>
>> I wonder if the same fix is needed for it as well?
>
> What you described is exactly the symptoms I was experimenting (notably
> the "moving around sometimes fixes it" thing) so yeah, I think this also
> applies to `bit_*` and `genmask_*` (and anything that invokes
> `build_assert`, really).

... anything that invoke `builds_assert` with one of its arguments as
parameter, that is. There are uses in e.g. `locked_by.rs` that validate
valid properties rather than function arguments, and they are not
affected by this.
Re: [PATCH] rust: io: always inline methods leading to build_assert
Posted by Miguel Ojeda 4 days, 5 hours ago
On Thu, Nov 27, 2025 at 3:54 PM Daniel Almeida
<daniel.almeida@collabora.com> wrote:
>
> I also faced this with genmask, by the way, i.e.: using genmask with an
> in-bounds constant would trigger a build error. Very confusingly, this would be
> randomly solved by moving the genmask invocation around in the code.
>
> I wonder if the same fix is needed for it as well?

If you still have the ability to reproduce it, then please do check!

Cheers,
Miguel