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@kernel.org/ > Suggested-by: Danilo Krummrich > Signed-off-by: Muchamad Coirul Anwar > --- > 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: 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: IoBackend { > + /// Read from `view`, returning a transport error if the operation fails. > + fn io_try_read<'a>(view: Self::View<'a, T>) -> Result; > + > + /// 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, T> FallibleIoCapable for B { > + #[inline(always)] > + fn io_try_read<'a>(view: Self::View<'a, T>) -> Result { > + 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 > where > usize: IoLoc, > - Self::Backend: IoCapable, > + Self::Backend: FallibleIoCapable, > { > self.try_read(offset) > } > @@ -769,7 +794,7 @@ fn try_read8(self, offset: usize) -> Result > fn try_read16(self, offset: usize) -> Result > where > usize: IoLoc, > - Self::Backend: IoCapable, > + Self::Backend: FallibleIoCapable, > { > self.try_read(offset) > } > @@ -779,7 +804,7 @@ fn try_read16(self, offset: usize) -> Result > fn try_read32(self, offset: usize) -> Result > where > usize: IoLoc, > - Self::Backend: IoCapable, > + Self::Backend: FallibleIoCapable, > { > self.try_read(offset) > } > @@ -789,7 +814,7 @@ fn try_read32(self, offset: usize) -> Result > fn try_read64(self, offset: usize) -> Result > where > usize: IoLoc, > - Self::Backend: IoCapable, > + Self::Backend: FallibleIoCapable, > { > self.try_read(offset) > } > @@ -799,7 +824,7 @@ fn try_read64(self, offset: usize) -> Result > fn try_write8(self, value: u8, offset: usize) -> Result > where > usize: IoLoc, > - Self::Backend: IoCapable, > + Self::Backend: FallibleIoCapable, > { > 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::Backend: IoCapable, > + Self::Backend: FallibleIoCapable, > { > 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::Backend: IoCapable, > + Self::Backend: FallibleIoCapable, > { > 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::Backend: IoCapable, > + Self::Backend: FallibleIoCapable, > { > self.try_write(offset, value) > } > @@ -958,9 +983,12 @@ fn try_read(self, location: L) -> Result > where > T: AsReprMut, > L: IoLoc, > - Self::Backend: IoCapable<::Repr>, > + Self::Backend: FallibleIoCapable<::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(self, location: L, value: T) -> Result > where > T: AsRepr, > L: IoLoc, > - Self::Backend: IoCapable<::Repr>, > + Self::Backend: FallibleIoCapable<::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(self, value: V) -> Result > T: AsRepr, > L: IoLoc, > V: LocatedRegister, > - Self::Backend: IoCapable<::Repr>, > + Self::Backend: FallibleIoCapable<::Repr>, > { > let (location, value) = value.into_io_op(); > > @@ -1071,12 +1099,12 @@ fn try_update(self, location: L, f: F) -> Result > where > T: AsReprMut, > L: IoLoc, > - Self::Backend: IoCapable<::Repr>, > + Self::Backend: FallibleIoCapable<::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 Thanks - Markus Probst