[RFC PATCH v6 1/4] rust: io: add FallibleIoCapable
From: Muchamad Coirul Anwar
Date: Fri Oct 16 2026 - 12:15:07 EST
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 | 66 +++++++++++++++++++++++++++++++++--------------
1 file changed, 46 insertions(+), 20 deletions(-)
diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index 95f46bb75f9e..516895ca2082 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -276,6 +276,36 @@ pub trait IoCapable<T>: IoBackend {
fn io_write<'a>(view: Self::View<'a, T>, value: T);
}
+/// Fallible counterpart of [`IoCapable`] for I/O backends where operations can fail at the
+/// transport level (e.g. I2C, SPI).
+///
+/// Infallible backends ([`IoCapable`] implementors) get this for free via blanket implementation.
+/// Fallible-only backends implement this trait directly without implementing [`IoCapable`]; the
+/// infallible [`Io::read`], [`Io::write`], and [`Io::update`] methods will then be unavailable,
+/// enforcing that callers use the `try_*` variants instead.
+pub trait FallibleIoCapable<T>: IoBackend {
+ /// Performs an I/O read of type `T` at `view` and returns the result, or an error if the
+ /// transport-level operation fails.
+ fn io_try_read<'a>(view: Self::View<'a, T>) -> Result<T>;
+
+ /// Performs an I/O write of `value` at `view`, or returns an error if the transport-level
+ /// 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`.
@@ -645,7 +675,7 @@ fn copy_to_slice(self, data: &mut [u8])
fn try_read8(self, offset: usize) -> Result<u8>
where
usize: IoLoc<Self::Target, u8, IoType = u8>,
- Self::Backend: IoCapable<u8>,
+ Self::Backend: FallibleIoCapable<u8>,
{
self.try_read(offset)
}
@@ -655,7 +685,7 @@ fn try_read8(self, offset: usize) -> Result<u8>
fn try_read16(self, offset: usize) -> Result<u16>
where
usize: IoLoc<Self::Target, u16, IoType = u16>,
- Self::Backend: IoCapable<u16>,
+ Self::Backend: FallibleIoCapable<u16>,
{
self.try_read(offset)
}
@@ -665,7 +695,7 @@ fn try_read16(self, offset: usize) -> Result<u16>
fn try_read32(self, offset: usize) -> Result<u32>
where
usize: IoLoc<Self::Target, u32, IoType = u32>,
- Self::Backend: IoCapable<u32>,
+ Self::Backend: FallibleIoCapable<u32>,
{
self.try_read(offset)
}
@@ -675,7 +705,7 @@ fn try_read32(self, offset: usize) -> Result<u32>
fn try_read64(self, offset: usize) -> Result<u64>
where
usize: IoLoc<Self::Target, u64, IoType = u64>,
- Self::Backend: IoCapable<u64>,
+ Self::Backend: FallibleIoCapable<u64>,
{
self.try_read(offset)
}
@@ -685,7 +715,7 @@ fn try_read64(self, offset: usize) -> Result<u64>
fn try_write8(self, value: u8, offset: usize) -> Result
where
usize: IoLoc<Self::Target, u8, IoType = u8>,
- Self::Backend: IoCapable<u8>,
+ Self::Backend: FallibleIoCapable<u8>,
{
self.try_write(offset, value)
}
@@ -695,7 +725,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, IoType = u16>,
- Self::Backend: IoCapable<u16>,
+ Self::Backend: FallibleIoCapable<u16>,
{
self.try_write(offset, value)
}
@@ -705,7 +735,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, IoType = u32>,
- Self::Backend: IoCapable<u32>,
+ Self::Backend: FallibleIoCapable<u32>,
{
self.try_write(offset, value)
}
@@ -715,7 +745,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, IoType = u64>,
- Self::Backend: IoCapable<u64>,
+ Self::Backend: FallibleIoCapable<u64>,
{
self.try_write(offset, value)
}
@@ -827,10 +857,10 @@ fn write64(self, value: u64, offset: usize)
fn try_read<T, L>(self, location: L) -> Result<T>
where
L: IoLoc<Self::Target, T>,
- Self::Backend: IoCapable<L::IoType>,
+ Self::Backend: FallibleIoCapable<L::IoType>,
{
let view = io_view::<Self, L::IoType>(self, location.offset())?;
- Ok(Self::Backend::io_read(view).into())
+ Ok(Self::Backend::io_try_read(view)?.into())
}
/// Generic fallible write with runtime bounds check.
@@ -860,12 +890,11 @@ fn try_read<T, L>(self, location: L) -> Result<T>
fn try_write<T, L>(self, location: L, value: T) -> Result
where
L: IoLoc<Self::Target, T>,
- Self::Backend: IoCapable<L::IoType>,
+ Self::Backend: FallibleIoCapable<L::IoType>,
{
let view = io_view::<Self, L::IoType>(self, location.offset())?;
let io_value = value.into();
- Self::Backend::io_write(view, io_value);
- Ok(())
+ Self::Backend::io_try_write(view, io_value)
}
/// Generic fallible write of a fully-located register value.
@@ -905,7 +934,7 @@ fn try_write_reg<T, L, V>(self, value: V) -> Result
where
L: IoLoc<Self::Target, T>,
V: LocatedRegister<Self::Target, Location = L, Value = T>,
- Self::Backend: IoCapable<L::IoType>,
+ Self::Backend: FallibleIoCapable<L::IoType>,
{
let (location, value) = value.into_io_op();
@@ -938,16 +967,13 @@ fn try_write_reg<T, L, V>(self, value: V) -> Result
fn try_update<T, L, F>(self, location: L, f: F) -> Result
where
L: IoLoc<Self::Target, T>,
- Self::Backend: IoCapable<L::IoType>,
+ Self::Backend: FallibleIoCapable<L::IoType>,
F: FnOnce(T) -> T,
{
let view = io_view::<Self, L::IoType>(self, location.offset())?;
-
- let value: T = Self::Backend::io_read(view).into();
+ let value: T = Self::Backend::io_try_read(view)?.into();
let io_value = f(value).into();
- Self::Backend::io_write(view, io_value);
-
- Ok(())
+ Self::Backend::io_try_write(view, io_value)
}
/// Generic infallible read with compile-time bounds check.
--
2.50.0