[PATCH v3] rust: Mark all from() for Error functions inline

alistair23@gmail.com posted 1 patch 1 week ago
rust/kernel/alloc/kvec/errors.rs | 3 +++
rust/kernel/error.rs             | 6 ++++++
rust/kernel/ptr/projection.rs    | 2 +-
rust/kernel/xarray.rs            | 1 +
4 files changed, 11 insertions(+), 1 deletion(-)
[PATCH v3] rust: Mark all from() for Error functions inline
Posted by alistair23@gmail.com 1 week ago
From: Alistair Francis <alistair.francis@wdc.com>

Mark all of the existing

impl From<...> for Error {
    fn from(err: ...) -> Self {
        ...
    }
}

functions as `#[inline]`

There was a recent request [1] to inline the simple from() Error
functions to avoid the function call overhead.

This patch inlines all of the functions (except for the pin-initi and
syn) and converts one existing `inline(always)` to `inline` to match
the styles.

Link: https://lore.kernel.org/all/CAH5fLggOnVguVErjySphX__vs3iYAo4O+J6hrXW546JrWSq5ZA@mail.gmail.com/ [1]
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
Acked-by: Danilo Krummrich <dakr@kernel.org>
---
v3:
 - Convert Link to use lore.kernel.org
 - Drop change to syn

 rust/kernel/alloc/kvec/errors.rs | 3 +++
 rust/kernel/error.rs             | 6 ++++++
 rust/kernel/ptr/projection.rs    | 2 +-
 rust/kernel/xarray.rs            | 1 +
 4 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/alloc/kvec/errors.rs b/rust/kernel/alloc/kvec/errors.rs
index e7de5049ee47..985c5f2c3962 100644
--- a/rust/kernel/alloc/kvec/errors.rs
+++ b/rust/kernel/alloc/kvec/errors.rs
@@ -15,6 +15,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 }
 
 impl<T> From<PushError<T>> for Error {
+    #[inline]
     fn from(_: PushError<T>) -> Error {
         // Returning ENOMEM isn't appropriate because the system is not out of memory. The vector
         // is just full and we are refusing to resize it.
@@ -32,6 +33,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 }
 
 impl From<RemoveError> for Error {
+    #[inline]
     fn from(_: RemoveError) -> Error {
         EINVAL
     }
@@ -55,6 +57,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 }
 
 impl<T> From<InsertError<T>> for Error {
+    #[inline]
     fn from(_: InsertError<T>) -> Error {
         EINVAL
     }
diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 258b12afdcba..935787c2a91c 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -216,36 +216,42 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 }
 
 impl From<AllocError> for Error {
+    #[inline]
     fn from(_: AllocError) -> Error {
         code::ENOMEM
     }
 }
 
 impl From<TryFromIntError> for Error {
+    #[inline]
     fn from(_: TryFromIntError) -> Error {
         code::EINVAL
     }
 }
 
 impl From<Utf8Error> for Error {
+    #[inline]
     fn from(_: Utf8Error) -> Error {
         code::EINVAL
     }
 }
 
 impl From<LayoutError> for Error {
+    #[inline]
     fn from(_: LayoutError) -> Error {
         code::ENOMEM
     }
 }
 
 impl From<fmt::Error> for Error {
+    #[inline]
     fn from(_: fmt::Error) -> Error {
         code::EINVAL
     }
 }
 
 impl From<core::convert::Infallible> for Error {
+    #[inline]
     fn from(e: core::convert::Infallible) -> Error {
         match e {}
     }
diff --git a/rust/kernel/ptr/projection.rs b/rust/kernel/ptr/projection.rs
index 140ea8e21617..7e37390da6c6 100644
--- a/rust/kernel/ptr/projection.rs
+++ b/rust/kernel/ptr/projection.rs
@@ -13,7 +13,7 @@
 pub struct OutOfBound;
 
 impl From<OutOfBound> for Error {
-    #[inline(always)]
+    #[inline]
     fn from(_: OutOfBound) -> Self {
         ERANGE
     }
diff --git a/rust/kernel/xarray.rs b/rust/kernel/xarray.rs
index a49d6db28845..46e5f43223fe 100644
--- a/rust/kernel/xarray.rs
+++ b/rust/kernel/xarray.rs
@@ -172,6 +172,7 @@ pub struct StoreError<T> {
 }
 
 impl<T> From<StoreError<T>> for Error {
+    #[inline]
     fn from(value: StoreError<T>) -> Self {
         value.error
     }
-- 
2.53.0
Re: [PATCH v3] rust: Mark all from() for Error functions inline
Posted by Miguel Ojeda 6 days, 10 hours ago
On Thu, Mar 26, 2026 at 3:04 AM <alistair23@gmail.com> wrote:
>
> From: Alistair Francis <alistair.francis@wdc.com>
>
> Mark all of the existing
>
> impl From<...> for Error {
>     fn from(err: ...) -> Self {
>         ...
>     }
> }
>
> functions as `#[inline]`
>
> There was a recent request [1] to inline the simple from() Error
> functions to avoid the function call overhead.
>
> This patch inlines all of the functions (except for the pin-initi and
> syn) and converts one existing `inline(always)` to `inline` to match
> the styles.
>
> Link: https://lore.kernel.org/all/CAH5fLggOnVguVErjySphX__vs3iYAo4O+J6hrXW546JrWSq5ZA@mail.gmail.com/ [1]
> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
> Acked-by: Danilo Krummrich <dakr@kernel.org>

Applied to `rust-next` -- thanks everyone!

    [ Dropped `projection.rs` since it is in another tree and already marked
      as `inline(always)` and reworded accordingly. Changed Link tag to
      Gary's original message and added Suggested-by. - Miguel ]

Alistair: please take a look at the reword in case you disagree. Thanks!

Cheers,
Miguel
Re: [PATCH v3] rust: Mark all from() for Error functions inline
Posted by Andreas Hindborg 6 days, 11 hours ago
<alistair23@gmail.com> writes:

> From: Alistair Francis <alistair.francis@wdc.com>
>
> Mark all of the existing
>
> impl From<...> for Error {
>     fn from(err: ...) -> Self {
>         ...
>     }
> }
>
> functions as `#[inline]`
>
> There was a recent request [1] to inline the simple from() Error
> functions to avoid the function call overhead.
>
> This patch inlines all of the functions (except for the pin-initi and
> syn) and converts one existing `inline(always)` to `inline` to match
> the styles.
>
> Link: https://lore.kernel.org/all/CAH5fLggOnVguVErjySphX__vs3iYAo4O+J6hrXW546JrWSq5ZA@mail.gmail.com/ [1]
> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
> Acked-by: Danilo Krummrich <dakr@kernel.org>

Acked-by: Andreas Hindborg <a.hindborg@kernel.org>


Best regards,
Andreas Hindborg