mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] rust: num: bounded: Always inline fits_within and from_expr
@ 2025-11-22  4:00 Alexandre Courbot
  2025-11-23 21:51 ` Miguel Ojeda
  0 siblings, 1 reply; 2+ messages in thread
From: Alexandre Courbot @ 2025-11-22  4:00 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich
  Cc: rust-for-linux, linux-kernel, kernel test robot, Alexandre Courbot

`from_expr` relies on `build_assert` to infer that the passed expression
fits the type's boundaries at build time. That inference can only be
successful its code (and that of `fits_within`, which performs the
check) is inlined, as a dedicated function would need to work with a
variable and cannot verify that property.

While inlining happens as expected in most cases, it is not guaranteed.
In particular, kernel options that optimize for size like
`CONFIG_CC_OPTIMIZE_FOR_SIZE` can result in `from_expr` not being
inlined.

Add `#[inline(always)]` attributes to both `fits_within` and `from_expr`
to make the compiler inline these functions more aggressively, as it
does not make sense to use them non-inlined anyway.

Fixes: 01e345e82ec3 ("rust: num: add Bounded integer wrapping type")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202511210055.RUsFNku1-lkp@intel.com/
Suggested-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 rust/kernel/num/bounded.rs | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs
index 18e45bf6f84b..92c41b2eb760 100644
--- a/rust/kernel/num/bounded.rs
+++ b/rust/kernel/num/bounded.rs
@@ -35,6 +35,7 @@ macro_rules! fits_within {
 }
 
 /// Returns `true` if `value` can be represented with at most `N` bits in a `T`.
+#[inline(always)]
 fn fits_within<T: Integer>(value: T, num_bits: u32) -> bool {
     fits_within!(value, T, num_bits)
 }
@@ -362,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);
     /// ```
+    #[inline(always)]
     pub fn from_expr(expr: T) -> Self {
         crate::build_assert!(
             fits_within(expr, N),

---
base-commit: 494de8f67b1e586b0190eb7f835e97c97f6b81b1
change-id: 20251122-bounded_ints_fix-2e276fece6b5

Best regards,
-- 
Alexandre Courbot <acourbot@nvidia.com>


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] rust: num: bounded: Always inline fits_within and from_expr
  2025-11-22  4:00 [PATCH] rust: num: bounded: Always inline fits_within and from_expr Alexandre Courbot
@ 2025-11-23 21:51 ` Miguel Ojeda
  0 siblings, 0 replies; 2+ messages in thread
From: Miguel Ojeda @ 2025-11-23 21:51 UTC (permalink / raw)
  To: Alexandre Courbot
  Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Danilo Krummrich, rust-for-linux, linux-kernel,
	kernel test robot

On Sat, Nov 22, 2025 at 5:01 AM Alexandre Courbot <acourbot@nvidia.com> wrote:
>
> `from_expr` relies on `build_assert` to infer that the passed expression
> fits the type's boundaries at build time. That inference can only be
> successful its code (and that of `fits_within`, which performs the
> check) is inlined, as a dedicated function would need to work with a
> variable and cannot verify that property.
>
> While inlining happens as expected in most cases, it is not guaranteed.
> In particular, kernel options that optimize for size like
> `CONFIG_CC_OPTIMIZE_FOR_SIZE` can result in `from_expr` not being
> inlined.
>
> Add `#[inline(always)]` attributes to both `fits_within` and `from_expr`
> to make the compiler inline these functions more aggressively, as it
> does not make sense to use them non-inlined anyway.
>
> Fixes: 01e345e82ec3 ("rust: num: add Bounded integer wrapping type")
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202511210055.RUsFNku1-lkp@intel.com/
> Suggested-by: Gary Guo <gary@garyguo.net>
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>

Applied to `rust-next` -- thanks!

    [ For reference, the errors look like:

        ld.lld: error: undefined symbol: rust_build_error
        >>> referenced by build_assert.rs:83 (rust/kernel/build_assert.rs:83)
        >>>
rust/doctests_kernel_generated.o:(<kernel::num::bounded::Bounded<u8,
1>>::from_expr) in archive vmlinux.a

          - Miguel ]

I could have rebased, but since you sent this nice patch with a good
commit message and kernel test robot reported it, let's add it :)

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-11-23 21:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-22  4:00 [PATCH] rust: num: bounded: Always inline fits_within and from_expr Alexandre Courbot
2025-11-23 21:51 ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®