Re: [RFC PATCH v8 1/4] rust: io: add FallibleIoCapable
From: Markus Probst
Date: Tue Dec 01 2026 - 12:41:26 EST
On Tue, 2026-12-01 at 23:55 +0700, Muchamad Coirul Anwar wrote:
> Add a fallible I/O capability for backends whose accesses may fail
> independently of address validation.
>
> Route try_read, try_write, try_write_reg, and try_update through the
> fallible capability. Provide a blanket implementation for infallible
> IoCapable backends so existing users retain their current behavior.
>
> This allows transport-backed implementations such as I2C to expose only
> fallible access while keeping infallible operations unavailable.
>
> Link: https://lore.kernel.org/all/DJVQ852J7SOH.26YBIJTQ9B66G@xxxxxxxxxx/
> Suggested-by: Danilo Krummrich <dakr@xxxxxxxxxx>
> Signed-off-by: Muchamad Coirul Anwar <muchamadcoirulanwar@xxxxxxxxx>
> ---
> rust/kernel/io.rs | 62 ++++++++++++++++++++++++++++++++++-------------
> 1 file changed, 45 insertions(+), 17 deletions(-)
>
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index a877df11a03e..b93a540c28da 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -356,6 +356,31 @@ pub trait IoCapable<T>: IoBackend {
> fn io_write<'a>(view: Self::View<'a, T>, value: T);
> }
>
> +/// I/O backend capability for operations that may fail independently of address validation.
> +///
> +/// Infallible backends implementing [`IoCapable`] also support fallible operations. Backends
> +/// implementing only this trait cannot use the infallible [`Io`] accessors.
> +pub trait FallibleIoCapable<T>: IoBackend {
> + /// Read from `view`, returning a transport error if the operation fails.
> + fn io_try_read<'a>(view: Self::View<'a, T>) -> Result<T>;
> +
> + /// Write `value` to `view`, returning a transport error if the operation fails.
> + fn io_try_write<'a>(view: Self::View<'a, T>, value: T) -> Result;
> +}
> +
> +impl<B: IoCapable<T>, T> FallibleIoCapable<T> for B {
> + #[inline(always)]
> + fn io_try_read<'a>(view: Self::View<'a, T>) -> Result<T> {
> + Ok(Self::io_read(view))
> + }
> +
> + #[inline(always)]
> + fn io_try_write<'a>(view: Self::View<'a, T>, value: T) -> Result {
> + Self::io_write(view, value);
> + Ok(())
> + }
> +}
> +
> /// Trait indicating that an I/O backend supports memory copy operations.
> pub trait IoCopyable: IoBackend {
> /// Copy contents of `view` to `buffer`.
> @@ -759,7 +784,7 @@ fn copy_to_slice(self, data: &mut [u8])
> fn try_read8(self, offset: usize) -> Result<u8>
> where
> usize: IoLoc<Self::Target, u8>,
> - Self::Backend: IoCapable<u8>,
> + Self::Backend: FallibleIoCapable<u8>,
> {
> self.try_read(offset)
> }
> @@ -769,7 +794,7 @@ fn try_read8(self, offset: usize) -> Result<u8>
> fn try_read16(self, offset: usize) -> Result<u16>
> where
> usize: IoLoc<Self::Target, u16>,
> - Self::Backend: IoCapable<u16>,
> + Self::Backend: FallibleIoCapable<u16>,
> {
> self.try_read(offset)
> }
> @@ -779,7 +804,7 @@ fn try_read16(self, offset: usize) -> Result<u16>
> fn try_read32(self, offset: usize) -> Result<u32>
> where
> usize: IoLoc<Self::Target, u32>,
> - Self::Backend: IoCapable<u32>,
> + Self::Backend: FallibleIoCapable<u32>,
> {
> self.try_read(offset)
> }
> @@ -789,7 +814,7 @@ fn try_read32(self, offset: usize) -> Result<u32>
> fn try_read64(self, offset: usize) -> Result<u64>
> where
> usize: IoLoc<Self::Target, u64>,
> - Self::Backend: IoCapable<u64>,
> + Self::Backend: FallibleIoCapable<u64>,
> {
> self.try_read(offset)
> }
> @@ -799,7 +824,7 @@ fn try_read64(self, offset: usize) -> Result<u64>
> fn try_write8(self, value: u8, offset: usize) -> Result
> where
> usize: IoLoc<Self::Target, u8>,
> - Self::Backend: IoCapable<u8>,
> + Self::Backend: FallibleIoCapable<u8>,
> {
> self.try_write(offset, value)
> }
> @@ -809,7 +834,7 @@ fn try_write8(self, value: u8, offset: usize) -> Result
> fn try_write16(self, value: u16, offset: usize) -> Result
> where
> usize: IoLoc<Self::Target, u16>,
> - Self::Backend: IoCapable<u16>,
> + Self::Backend: FallibleIoCapable<u16>,
> {
> self.try_write(offset, value)
> }
> @@ -819,7 +844,7 @@ fn try_write16(self, value: u16, offset: usize) -> Result
> fn try_write32(self, value: u32, offset: usize) -> Result
> where
> usize: IoLoc<Self::Target, u32>,
> - Self::Backend: IoCapable<u32>,
> + Self::Backend: FallibleIoCapable<u32>,
> {
> self.try_write(offset, value)
> }
> @@ -829,7 +854,7 @@ fn try_write32(self, value: u32, offset: usize) -> Result
> fn try_write64(self, value: u64, offset: usize) -> Result
> where
> usize: IoLoc<Self::Target, u64>,
> - Self::Backend: IoCapable<u64>,
> + Self::Backend: FallibleIoCapable<u64>,
> {
> self.try_write(offset, value)
> }
> @@ -958,9 +983,12 @@ fn try_read<T, L>(self, location: L) -> Result<T>
> where
> T: AsReprMut,
> L: IoLoc<Self::Target, T>,
> - Self::Backend: IoCapable<<T as AsRepr>::Repr>,
> + Self::Backend: FallibleIoCapable<<T as AsRepr>::Repr>,
> {
> - Ok(io_read!(self, try: location))
> + let view = io_project!(self, try: location);
> + Ok(T::from_repr(Self::Backend::io_try_read(io_view_as_repr(
> + view,
> + ))?))
> }
>
> /// Generic fallible write with runtime bounds check.
> @@ -991,10 +1019,10 @@ fn try_write<T, L>(self, location: L, value: T) -> Result
> where
> T: AsRepr,
> L: IoLoc<Self::Target, T>,
> - Self::Backend: IoCapable<<T as AsRepr>::Repr>,
> + Self::Backend: FallibleIoCapable<<T as AsRepr>::Repr>,
> {
> - io_write!(self, try: location, value);
> - Ok(())
> + let view = io_project!(self, try: location);
> + Self::Backend::io_try_write(io_view_as_repr(view), T::into_repr(value))
> }
>
> /// Generic fallible write of a fully-located register value.
> @@ -1037,7 +1065,7 @@ fn try_write_reg<T, L, V>(self, value: V) -> Result
> T: AsRepr,
> L: IoLoc<Self::Target, T>,
> V: LocatedRegister<Self::Target, Location = L, Value = T>,
> - Self::Backend: IoCapable<<T as AsRepr>::Repr>,
> + Self::Backend: FallibleIoCapable<<T as AsRepr>::Repr>,
> {
> let (location, value) = value.into_io_op();
>
> @@ -1071,12 +1099,12 @@ fn try_update<T, L, F>(self, location: L, f: F) -> Result
> where
> T: AsReprMut,
> L: IoLoc<Self::Target, T>,
> - Self::Backend: IoCapable<<T as AsRepr>::Repr>,
> + Self::Backend: FallibleIoCapable<<T as AsRepr>::Repr>,
> F: FnOnce(T) -> T,
> {
> let view = io_project!(self, try: location);
> - view.write_val(f(view.read_val()));
> - Ok(())
> + let value = T::from_repr(Self::Backend::io_try_read(io_view_as_repr(view))?);
> + Self::Backend::io_try_write(io_view_as_repr(view), T::into_repr(f(value)))
> }
>
> /// Generic infallible read with compile-time bounds check.
Tested-by: Markus Probst <markus.probst@xxxxxxxxx>
Thanks
- Markus Probst
Attachment:
signature.asc
Description: This is a digitally signed message part