rust: build_assert: document and fix use with function arguments
Subsystems (7)
Alexandre Courbot
maintainerv3 cover rust: build_assert: document and fix use with function arguments
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, as pointed out by Gary Guo [3], 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.
This series fixes all possible points of failure in the kernel crate, and adds documentation to build_assert explaining how to properly inline functions for which this behavior may arise.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
---
Changes in v3:
- Add "Fixes:" tags.
- CC stable on fixup patches.
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- Turn into a series and address other similar cases in the kernel crate.
- Link to v1: https://patch.msgid.link/[email protected]
---
Alexandre Courbot (7):
rust: build_assert: add instructions for use with function arguments
rust: io: always inline functions using build_assert with arguments
rust: cpufreq: always inline functions using build_assert with arguments
rust: bits: always inline functions using build_assert with arguments
rust: sync: refcount: always inline functions using build_assert with arguments
rust: irq: always inline functions using build_assert with arguments
rust: num: bounded: add missing comment for always inlined function
rust/kernel/bits.rs | 6 ++++-- rust/kernel/build_assert.rs | 7 ++++++- rust/kernel/cpufreq.rs | 2 ++ rust/kernel/io.rs | 9 ++++++--- rust/kernel/io/resource.rs | 2 ++ rust/kernel/irq/flags.rs | 2 ++ rust/kernel/num/bounded.rs | 1 + rust/kernel/sync/refcount.rs | 3 ++- 8 files changed, 25 insertions(+), 7 deletions(-)
---
base-commit: ba65a4e7120a616d9c592750d9147f6dcafedffa
change-id: 20251127-io-build-assert-3579a5bfb81c
Best regards,
Signature · 1 line
Alexandre Courbot <[email protected]>
Alexandre Courbot
maintainerPATCH v3 1/7 rust: build_assert: add instructions for use with function arguments
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.
--- rust/kernel/build_assert.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
rust/kernel/build_assert.rs +6 -1
@@ -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)] /// fn bar(n: usize) { /// // `static_assert!(n > 1);` is not allowed /// build_assert!(n > 1); // Build-time check
Signature · 1 line
2.52.0
Alexandre Courbot
maintainerPATCH v3 2/7 rust: io: always inline functions using build_assert with arguments
In reply to Alexandre Courbotbuild_assert relies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path of build_assert might not be optimized out, triggering a build error.
--- rust/kernel/io.rs | 9 ++++++--- rust/kernel/io/resource.rs | 2 ++ 2 files changed, 8 insertions(+), 3 deletions(-)
rust/kernel/io.rs +6 -3
@@ -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 to optimize out error path of `io_addr_assert`. + #[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 to optimize out error path of `io_addr_assert`. + #[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 to optimize out error path of `build_assert`. +#[inline(always)] fn io_addr_assert <U >(&self, offset:usize ) ->usize {build_assert! (Self ::offset_valid ::<U >(offset,SIZE ));
rust/kernel/io/resource.rs +2 -0
@@ -226,6 +226,8 @@ impl Flags {/// Resource represents a memory region that must be ioremaped using `ioremap_np`. pub const IORESOURCE_MEM_NONPOSTED :Flags =Flags ::new (bindings::IORESOURCE_MEM_NONPOSTED ); +// Always inline to optimize out error path of `build_assert`. +#[inline(always)] const fn new (value:u32 ) ->Self {crate ::build_assert!(valueas u64 <= c_ulong::MAX as u64 );Flags (valueas c_ulong )
Signature · 1 line
2.52.0
Re: [PATCH v3 2/7] rust: io: always inline functions using build_assert with arguments
Quoted 9 lines · Alexandre Courbot
build_assertrelies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path ofbuild_assertmight not be optimized out, triggering a build error.Fixes ce30d94e6855 ("rust: add `io::{Io, IoRaw}` base types")Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>
Without patch 2/7, my Turing patchset will fail to compile with CLIPPY. So please merge this patchset, or at least just this patch, soon.
Alexandre Courbot
maintainerPATCH v3 3/7 rust: cpufreq: always inline functions using build_assert with arguments
In reply to Alexandre Courbotbuild_assert relies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path of build_assert might not be optimized out, triggering a build error.
--- rust/kernel/cpufreq.rs | 2 ++ 1 file changed, 2 insertions(+)
rust/kernel/cpufreq.rs +2 -0
@@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> { ..pin_init::zeroed () }; +// Always inline to optimize out error path of `build_assert`. +#[inline(always)] const fn copy_name (name: &'staticCStr ) -> [c_char ;CPUFREQ_NAME_LEN ] {let src = name.to_bytes_with_nul ();let mut dst = [0 ;CPUFREQ_NAME_LEN ];
Signature · 1 line
2.52.0
Gary Guo
maintainerRe: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Quoted 20 lines · On Mon, 08 Dec 2025 11:47:01 +0900 Alexandre Courbot <[email protected]>…
build_assertrelies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path ofbuild_assertmight not be optimized out, triggering a build error.Fixes c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")Acked-by Viresh Kumar <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- rust/kernel/cpufreq.rs | 2 ++ 1 file changed, 2 insertions(+)rust/kernel/cpufreq.rs +0
-0
rust/kernel/cpufreq.rs impl<T: Driver> Registration<T> { · line 1022
Quoted 8 lines
rust/kernel/cpufreq.rs +2
-0@@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> { ..pin_init::zeroed () }; +// Always inline to optimize out error path of `build_assert`. +#[inline(always)] const fn copy_name (name: &'staticCStr ) -> [c_char ;CPUFREQ_NAME_LEN ] {let src = name.to_bytes_with_nul ();let mut dst = [0 ;CPUFREQ_NAME_LEN ];
This change is not needed as this is a private function only used in const-eval only.
I wonder if I should add another macro to assert that the function is only used in const eval instead? Do you think it might be useful to have something like:
#[const_only]
const fn foo() {}or
const fn foo() {
const_only!();
}? If so, I can send a patch that adds this feature.
Implementation-wise, this will behave similar to build_error, where a function is going to be added that is never-linked but has a body for const eval.
Best,
Gary
Alexandre Courbot
maintainerRe: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Quoted 25 lines · Gary Guo
Quoted 22 lines · On Mon, 08 Dec 2025 11:47:01 +0900 Alexandre Courbot <[email protected]>…
build_assertrelies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path ofbuild_assertmight not be optimized out, triggering a build error.Fixes c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")Acked-by Viresh Kumar <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- rust/kernel/cpufreq.rs | 2 ++ 1 file changed, 2 insertions(+)rust/kernel/cpufreq.rs +0
-0@@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> { ..pin_init::zeroed () };
Quoted 9 lines
rust/kernel/cpufreq.rs impl<T: Driver> Registration<T> { · line 1022
Quoted 6 lines
rust/kernel/cpufreq.rs +2
-0+// Always inline to optimize out error path of `build_assert`. +#[inline(always)] const fn copy_name (name: &'staticCStr ) -> [c_char ;CPUFREQ_NAME_LEN ] {let src = name.to_bytes_with_nul ();let mut dst = [0 ;CPUFREQ_NAME_LEN ];This change is not needed as this is a private function only used in const-eval only.
... for now. :)
Quoted 14 lines
I wonder if I should add another macro to assert that the function is only used in const eval instead? Do you think it might be useful to have something like:
#[const_only] const fn foo() {}or
const fn foo() { const_only!(); }
Quoted 5 lines
? If so, I can send a patch that adds this feature.
Implementation-wise, this will behave similar to
build_error, where a function is going to be added that is never-linked but has a body for const eval.
It could be useful in the general sense, but for this particular case the rule "if you do build_assert on a function argument, then always inline it" also covers us in case copy_name gets used outside of const context, so isn't it the preferable workaround?
Gary Guo
maintainerRe: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Quoted 54 lines · On Tue, 09 Dec 2025 09:52:13 +0900 "Alexandre Courbot" <[email protected]>…
Quoted 25 lines · Gary Guo
Quoted 22 lines · On Mon, 08 Dec 2025 11:47:01 +0900 Alexandre Courbot <[email protected]>…
build_assertrelies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path ofbuild_assertmight not be optimized out, triggering a build error.Fixes c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")Acked-by Viresh Kumar <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- rust/kernel/cpufreq.rs | 2 ++ 1 file changed, 2 insertions(+)rust/kernel/cpufreq.rs +0
-0@@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> { ..pin_init::zeroed () };Quoted 9 lines
rust/kernel/cpufreq.rs impl<T: Driver> Registration<T> { · line 1022
Quoted 6 lines
rust/kernel/cpufreq.rs +2
-0+// Always inline to optimize out error path of `build_assert`. +#[inline(always)] const fn copy_name (name: &'staticCStr ) -> [c_char ;CPUFREQ_NAME_LEN ] {let src = name.to_bytes_with_nul ();let mut dst = [0 ;CPUFREQ_NAME_LEN ];This change is not needed as this is a private function only used in const-eval only.
... for now. :)
Quoted 16 lines
I wonder if I should add another macro to assert that the function is only used in const eval instead? Do you think it might be useful to have something like:
#[const_only] const fn foo() {}or
const fn foo() { const_only!(); }? If so, I can send a patch that adds this feature.
Quoted 8 lines
Quoted 3 lines
Implementation-wise, this will behave similar to
build_error, where a function is going to be added that is never-linked but has a body for const eval.It could be useful in the general sense, but for this particular case the rule "if you do
build_asserton a function argument, then always inline it" also covers us in casecopy_namegets used outside of const context, so isn't it the preferable workaround?
In this particular case the copy_name shouldn't be used at all outside const eval. It's specificially for building a table during const eval. It's a bug if it's outside, hence I think #[inline(always)] adds confusion to the reader of this code.
I get that you want to have a general rule of "if you're using something with build_assert!, then use #[inline(always)]", but I think applying that rule here is detrimental.
Hence I suggested adding a marker to indicate const-eval only function, so we can either say const-eval-only functions are fine without inline markers, or perhaps just use normal panicking-assertion inside these functions (as build_assert! behave identical to just assert! in const-eval).
Best,
Gary
Re: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
In reply to Gary GuoQuoted 2 lines · Gary Guo
? If so, I can send a patch that adds this feature.
Sounds like consteval in C++20, which is useful from time to time.
If we add it, then the attribute form may make a bit more "sense" conceptually (and we already also added the export one).
Cheers,
Miguel
Viresh Kumar
maintainerRe: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
In reply to Gary GuoQuoted 45 lines · On Mon, 08 Dec 2025 11:47:01 +0900 Alexandre Courbot <[email protected]>…
build_assertrelies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path ofbuild_assertmight not be optimized out, triggering a build error.Fixes c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")Acked-by Viresh Kumar <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- rust/kernel/cpufreq.rs | 2 ++ 1 file changed, 2 insertions(+)rust/kernel/cpufreq.rs +2
-0@@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> { ..pin_init::zeroed () }; +// Always inline to optimize out error path of `build_assert`. +#[inline(always)] const fn copy_name (name: &'staticCStr ) -> [c_char ;CPUFREQ_NAME_LEN ] {let src = name.to_bytes_with_nul ();let mut dst = [0 ;CPUFREQ_NAME_LEN ];This change is not needed as this is a private function only used in const-eval only.
I wonder if I should add another macro to assert that the function is only used in const eval instead? Do you think it might be useful to have something like:
#[const_only] const fn foo() {}or
const fn foo() { const_only!(); }
Quoted 5 lines
? If so, I can send a patch that adds this feature.
Implementation-wise, this will behave similar to
build_error, where a function is going to be added that is never-linked but has a body for const eval.
I already applied this from V2, should I drop this change ?
Signature · 1 line
viresh
Gary Guo
maintainerRe: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Quoted 48 lines · On Mon, 15 Dec 2025 10:36:55 +0530 Viresh Kumar <[email protected]> wrote:
Quoted 45 lines · On Mon, 08 Dec 2025 11:47:01 +0900 Alexandre Courbot <[email protected]>…
build_assertrelies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path ofbuild_assertmight not be optimized out, triggering a build error.Fixes c6af9a1191d0 ("rust: cpufreq: Extend abstractions for driver registration")Acked-by Viresh Kumar <[email protected]>Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>--- rust/kernel/cpufreq.rs | 2 ++ 1 file changed, 2 insertions(+)rust/kernel/cpufreq.rs +2
-0@@ -1015,6 +1015,8 @@ impl<T: Driver> Registration<T> { ..pin_init::zeroed () }; +// Always inline to optimize out error path of `build_assert`. +#[inline(always)] const fn copy_name (name: &'staticCStr ) -> [c_char ;CPUFREQ_NAME_LEN ] {let src = name.to_bytes_with_nul ();let mut dst = [0 ;CPUFREQ_NAME_LEN ];This change is not needed as this is a private function only used in const-eval only.
I wonder if I should add another macro to assert that the function is only used in const eval instead? Do you think it might be useful to have something like:
#[const_only] const fn foo() {}or
const fn foo() { const_only!(); }
Quoted 8 lines
Quoted 5 lines
? If so, I can send a patch that adds this feature.
Implementation-wise, this will behave similar to
build_error, where a function is going to be added that is never-linked but has a body for const eval.I already applied this from V2, should I drop this change ?
Thinking again about this I think #[inline(always)] is fine to keep as it can also be used to indicate "this function shall never be codegenned".
However I do still think the comment is confusing per-se as there is no "optimization" for this function at all.
RE: the patch I am fine either without this patch picked or having this patch in and fix the comment later.
Best,
Gary
Viresh Kumar
maintainerRe: [PATCH v3 3/7] rust: cpufreq: always inline functions using build_assert with arguments
Quoted 8 lines · Gary Guo
Thinking again about this I think
#[inline(always)]is fine to keep as it can also be used to indicate "this function shall never be codegenned".However I do still think the comment is confusing per-se as there is no "optimization" for this function at all.
RE: the patch I am fine either without this patch picked or having this patch in and fix the comment later.
Thanks Gary. I will keep the patch then and apply add-ons later.
Signature · 1 line
viresh
Alexandre Courbot
maintainerPATCH v3 4/7 rust: bits: always inline functions using build_assert with arguments
In reply to Alexandre Courbotbuild_assert relies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path of build_assert might not be optimized out, triggering a build error.
--- rust/kernel/bits.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
rust/kernel/bits.rs +4 -2
@@ -27,7 +27,8 @@ pub fn [<checked_bit_ $ty>](n: u32) -> Option<$ty> {/// /// This version is the default and should be used if `n` is known at /// compile time. -#[inline] +// Always inline to optimize out error path of `build_assert`. +#[inline(always)] pub const fn [<bit_ $ty>](n: u32) -> $ty {build_assert! (n < <$ty>::BITS ); (1 as $ty) << n
@@ -75,7 +76,8 @@ pub fn [<genmask_checked_ $ty>](range: RangeInclusive<u32>) -> Option<$ty> {/// This version is the default and should be used if the range is known /// at compile time. $(#[$genmask_ex])* - #[inline] + // Always inline to optimize out error path of `build_assert`. + #[inline(always)] pub const fn [<genmask_ $ty>](range: RangeInclusive<u32>) -> $ty { let start = *range.start(); let end = *range.end();
Signature · 1 line
2.52.0
Alexandre Courbot
maintainerPATCH v3 5/7 rust: sync: refcount: always inline functions using build_assert with arguments
In reply to Alexandre Courbotbuild_assert relies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path of build_assert might not be optimized out, triggering a build error.
--- rust/kernel/sync/refcount.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
rust/kernel/sync/refcount.rs +2 -1
@@ -23,7 +23,8 @@ impl Refcount {/// Construct a new [`Refcount`] from an initial value. /// /// The initial value should be non-saturated. -#[inline] +// Always inline to optimize out error path of `build_assert`. +#[inline(always)] pub fn new (value:i32 ) ->Self {build_assert! (value >=0 ,"initial value saturated" );// SAFETY: There are no safety requirements for this FFI call.
Signature · 1 line
2.52.0
Re: [PATCH v3 5/7] rust: sync: refcount: always inline functions using build_assert with arguments
Quoted 10 lines · Alexandre Courbot
build_assertrelies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path ofbuild_assertmight not be optimized out, triggering a build error.Fixes bb38f35b35f9 ("rust: implement `kernel::sync::Refcount`")Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>
Boqun et al.: do you want to pick this one or should I take it with your Acked-by?
Thanks!
Cheers,
Miguel
Boqun Feng
Acked-byRe: [PATCH v3 5/7] rust: sync: refcount: always inline functions using build_assert with arguments
Quoted 7 lines · Miguel Ojeda
Quoted 6 lines · Alexandre Courbot
build_assertrelies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path ofbuild_assertmight not be optimized out, triggering a build error.
Quoted 8 lines
Quoted 4 lines
Fixes bb38f35b35f9 ("rust: implement `kernel::sync::Refcount`")Reviewed-by Daniel Almeida <[email protected]>Signed-off-by Alexandre Courbot <[email protected]>Boqun et al.: do you want to pick this one or should I take it with your Acked-by?
Going via rust-next seems more appropriate to me, thanks!
Regards,
Boqun
Quoted 4 lines
Thanks!
Cheers,
Miguel
Alexandre Courbot
maintainerPATCH v3 6/7 rust: irq: always inline functions using build_assert with arguments
In reply to Alexandre Courbotbuild_assert relies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path of build_assert might not be optimized out, triggering a build error.
--- rust/kernel/irq/flags.rs | 2 ++ 1 file changed, 2 insertions(+)
rust/kernel/irq/flags.rs +2 -0
@@ -96,6 +96,8 @@ pub(crate) fn into_inner(self) -> c_ulong { self.0 } +// Always inline to optimize out error path of `build_assert`. +#[inline(always)] const fn new (value:u32 ) ->Self {build_assert! (valueas u64 <= c_ulong::MAX as u64 );Self (valueas c_ulong )
Signature · 1 line
2.52.0
Alexandre Courbot
maintainerPATCH v3 7/7 rust: num: bounded: add missing comment for always inlined function
In reply to Alexandre CourbotThis code is always inlined to avoid a build error if the error path of build_assert cannot be optimized out. Add a comment justifying the #[inline(always)] property to avoid it being taken away by mistake.
--- rust/kernel/num/bounded.rs | 1 + 1 file changed, 1 insertion(+)
rust/kernel/num/bounded.rs +1 -0
@@ -363,6 +363,7 @@ pub fn try_new(value: T) -> Option<Self> {/// assert_eq!(Bounded::<u8, 1>::from_expr(1).get(), 1); /// assert_eq!(Bounded::<u16, 8>::from_expr(0xff).get(), 0xff); /// ``` +// Always inline to optimize out error path of `build_assert`. #[inline(always)] pub fn from_expr (expr:T ) ->Self {crate ::build_assert!(
Signature · 1 line
2.52.0
Gary Guo
maintainerQuoted 10 lines · On Mon, 08 Dec 2025 11:46:58 +0900 Alexandre Courbot <[email protected]>…
build_assertrelies 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=1was set. The same behavior was also observed until different, very similar conditions [1][2].
Quoted 5 lines
The cause, as pointed out by Gary Guo [3], appears to be that the failing function is eventually using
build_assertwith 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.
That's an interesting observation, so #[inline] is fine without clippy but #[inline(always)] is needed when Clippy is used?
I know Clippy would affect codegen but this might be a first concrete example that it actually creates (non-perf) issues that I've countered in practice.
Quoted 5 lines
The fix is to annotate functions passing their argument to
build_assertwith#[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.
Yeah, I suppose when you draw parallelism with C BUILD_BUG macro, there are a few users of that in other macros, which are kinda force-inlined.
Quoted 44 lines
This series fixes all possible points of failure in the kernel crate, and adds documentation to
build_assertexplaining how to properly inline functions for which this behavior may arise.[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/Signed-off-by Alexandre Courbot <[email protected]>---
Changes in v3:
- Add "Fixes:" tags.
- CC stable on fixup patches.
- Link to v2: https://patch.msgid.link/[email protected]Changes in v2:
- Turn into a series and address other similar cases in the kernel crate.
- Link to v1: https://patch.msgid.link/[email protected]---
Alexandre Courbot (7):
rust:build_assert: add instructions for use with function arguments
rust: io: always inline functions usingbuild_assertwith arguments
rust: cpufreq: always inline functions usingbuild_assertwith arguments
rust: bits: always inline functions usingbuild_assertwith arguments
rust: sync: refcount: always inline functions usingbuild_assertwith arguments
rust: irq: always inline functions usingbuild_assertwith arguments
rust: num: bounded: add missing comment for always inlined functionrust/kernel/bits.rs | 6 ++++-- rust/kernel/build_assert.rs | 7 ++++++- rust/kernel/cpufreq.rs | 2 ++ rust/kernel/io.rs | 9 ++++++--- rust/kernel/io/resource.rs | 2 ++ rust/kernel/irq/flags.rs | 2 ++ rust/kernel/num/bounded.rs | 1 + rust/kernel/sync/refcount.rs | 3 ++- 8 files changed, 25 insertions(+), 7 deletions(-)---
base-commit:ba65a4e7120a616d9c592750d9147f6dcafedffa
change-id: 20251127-io-build-assert-3579a5bfb81cBest regards,
Alexandre Courbot
maintainerQuoted 13 lines · Gary Guo
Quoted 10 lines · On Mon, 08 Dec 2025 11:46:58 +0900 Alexandre Courbot <[email protected]>…
build_assertrelies 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=1was set. The same behavior was also observed until different, very similar conditions [1][2].
Quoted 8 lines
Quoted 5 lines
The cause, as pointed out by Gary Guo [3], appears to be that the failing function is eventually using
build_assertwith 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.That's an interesting observation, so
#[inline]is fine without clippy but#[inline(always)]is needed when Clippy is used?
Precisely. And sometimes just moving the code invoking the not-always-inlined function that invokes build_assert is enough to fix it, so I don't know of an accurately reproducible pattern. It also occurs pretty rarely.
Quoted 2 lines · Alexandre Courbot
rust: io: always inline functions using
build_assertwith arguments
rust: irq: always inline functions usingbuild_assertwith arguments
Applied to driver-core-linus, thanks!
Miguel Ojeda
AppliedQuoted 4 lines · Alexandre Courbot
rust: bits: always inline functions using
build_assertwith arguments
rust: sync: refcount: always inline functions usingbuild_assertwith arguments
rust: num: bounded: add missing comment for always inlined function
Applied to rust-fixes -- thanks everyone!
The first one I will apply it intorust-next.
Cheers,
Miguel
Miguel Ojeda
AppliedQuoted 2 lines · Alexandre Courbot
rust:
build_assert: add instructions for use with function arguments
Applied to rust-next -- thanks everyone!
Cheers,
Miguel