Re: [PATCH v2 3/7] rust: always inline some init methods
From: Alexandre Courbot
Date: Fri May 29 2026 - 11:12:09 EST
On Thu May 28, 2026 at 2:54 AM JST, Gary Guo wrote:
> On Wed May 27, 2026 at 12:52 PM BST, Alexandre Courbot wrote:
>> These methods should always be inlined for optimization reasons. Failure
>> to do so can also produce symbol names larger than what `modpost`
>> supports.
>>
>> Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
>> ---
>> rust/kernel/alloc/kbox.rs | 2 ++
>> rust/kernel/init.rs | 1 +
>> rust/kernel/sync/arc.rs | 2 ++
>> rust/kernel/types.rs | 3 +++
>> 4 files changed, 8 insertions(+)
>>
>> diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
>> index 2f8c16473c2c..592fffc72e52 100644
>> --- a/rust/kernel/alloc/kbox.rs
>> +++ b/rust/kernel/alloc/kbox.rs
>> @@ -433,6 +433,7 @@ impl<T, A> InPlaceWrite<T> for Box<MaybeUninit<T>, A>
>> {
>> type Initialized = Box<T, A>;
>>
>> + #[inline(always)]
>> fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
>> let slot = self.as_mut_ptr();
>> // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
>> @@ -442,6 +443,7 @@ fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E
>> Ok(unsafe { Box::assume_init(self) })
>> }
>>
>> + #[inline(always)]
>> fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
>> let slot = self.as_mut_ptr();
>> // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
>> diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs
>> index 7a0d4559d7b5..36d4bf1959fb 100644
>> --- a/rust/kernel/init.rs
>> +++ b/rust/kernel/init.rs
>> @@ -151,6 +151,7 @@ fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Self::Pinne
>> /// type.
>> ///
>> /// If `T: !Unpin` it will not be able to move afterwards.
>> + #[inline(always)]
>> fn pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> error::Result<Self::PinnedSelf>
>> where
>> Error: From<E>,
>> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
>> index 18d6c0d62ce0..973f15165606 100644
>> --- a/rust/kernel/sync/arc.rs
>> +++ b/rust/kernel/sync/arc.rs
>> @@ -712,6 +712,7 @@ fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
>> impl<T> InPlaceWrite<T> for UniqueArc<MaybeUninit<T>> {
>> type Initialized = UniqueArc<T>;
>>
>> + #[inline(always)]
>> fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
>> let slot = self.as_mut_ptr();
>> // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
>> @@ -721,6 +722,7 @@ fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E
>> Ok(unsafe { self.assume_init() })
>> }
>>
>> + #[inline(always)]
>> fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
>> let slot = self.as_mut_ptr();
>> // SAFETY: When init errors/panics, slot will get deallocated but not dropped,
>> diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
>> index ac316fd7b538..381a06ee0d59 100644
>> --- a/rust/kernel/types.rs
>> +++ b/rust/kernel/types.rs
>> @@ -367,6 +367,7 @@ pub const fn zeroed() -> Self {
>> /// This function is safe, because the `T` inside of an `Opaque` is allowed to be
>> /// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
>> /// to verify at that point that the inner value is valid.
>> + #[inline(always)]
>> pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
>> // SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
>> // initialize the `T`.
>
> Are you sure this (and other `impl PinInit`-returning functions) are actually
> causing issue? They just return an initializer and the actual init code is not
> marked as `#[inline]`.
Indeed, this works fine without marking them. I have been a bit
heavy-handed, thanks for catching this.