Re: [PATCH v2 03/11] rust: io: use pointer types instead of address

From: Andreas Hindborg

Date: Mon Apr 27 2026 - 11:05:57 EST


Gary Guo <gary@xxxxxxxxxxx> writes:

> This carries the size information with the pointer type and metadata, makes
> it possible to use I/O projections and paves the way for IO view types.
>
> With this change, minimum size information becomes available through types;
> so `KnownSize::MIN_SIZE` can be used and `IoKnownSize` trait is no longer
> necessary. The trait is kept for compatibility and can be removed when
> users stop using it for bounds.
>
> PCI config space uses only offsets and not pointers like MMIO; for this
> null pointers (with proper size metadata) is used. This is okay as I/O
> trait impl and I/O projections can operate on invalid pointers, and for PCI
> config space we will only use address info and ignore the provenance.
>
> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>
> ---
> rust/kernel/devres.rs | 2 +-
> rust/kernel/io.rs | 123 +++++++++++++++++++++-----------------------------
> rust/kernel/io/mem.rs | 2 +-
> rust/kernel/pci/io.rs | 74 ++++++++++++++++++------------
> 4 files changed, 99 insertions(+), 102 deletions(-)
>
> diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs
> index 3e22c63efb98..ea86e9c62cdf 100644
> --- a/rust/kernel/devres.rs
> +++ b/rust/kernel/devres.rs
> @@ -101,7 +101,7 @@ struct Inner<T> {
> /// impl<const SIZE: usize> Drop for IoMem<SIZE> {
> /// fn drop(&mut self) {
> /// // SAFETY: `self.0.addr()` is guaranteed to be properly mapped by `Self::new`.
> -/// unsafe { bindings::iounmap(self.0.addr() as *mut c_void); };
> +/// unsafe { bindings::iounmap(self.0.as_ptr().cast()); };
> /// }
> /// }
> ///
> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
> index 0b9c97c0a1d7..1682f2a0d20d 100644
> --- a/rust/kernel/io.rs
> +++ b/rust/kernel/io.rs
> @@ -105,8 +105,8 @@ pub fn new_region(addr: usize, size: usize) -> Result<Self> {
> impl<T: ?Sized + KnownSize> MmioRaw<T> {
> /// Returns the base address of the MMIO region.
> #[inline]
> - pub fn addr(&self) -> usize {
> - self.addr.addr()
> + pub fn as_ptr(&self) -> *mut T {
> + self.addr
> }
>
> /// Returns the size of the MMIO region.
> @@ -166,7 +166,7 @@ pub fn size(&self) -> usize {
> /// impl<const SIZE: usize> Drop for IoMem<SIZE> {
> /// fn drop(&mut self) {
> /// // SAFETY: `self.0.addr()` is guaranteed to be properly mapped by `Self::new`.
> -/// unsafe { bindings::iounmap(self.0.addr() as *mut c_void); };
> +/// unsafe { bindings::iounmap(self.0.as_ptr().cast()); };
> /// }
> /// }
> ///
> @@ -217,14 +217,14 @@ pub trait IoCapable<T> {
> /// # Safety
> ///
> /// The range `[address..address + size_of::<T>()]` must be within the bounds of `Self`.
> - unsafe fn io_read(&self, address: usize) -> T;
> + unsafe fn io_read(&self, address: *mut T) -> T;
>
> /// Performs an I/O write of `value` at `address`.
> ///
> /// # Safety
> ///
> /// The range `[address..address + size_of::<T>()]` must be within the bounds of `Self`.
> - unsafe fn io_write(&self, value: T, address: usize);
> + unsafe fn io_write(&self, value: T, address: *mut T);
> }
>

In v1 you had a requirement for `address` to be aligned. Why did you
drop that?


Best regards,
Andreas Hindborg