Re: [PATCH v2 1/2] rust: io: gate ioremap/iounmap on CONFIG_HAS_IOMEM

From: Danilo Krummrich

Date: Thu Aug 06 2026 - 11:47:42 EST


On Wed Aug 5, 2026 at 11:39 PM CEST, Arnd Bergmann wrote:
> Can you also hide the actual I/O accessors in this case?
> While s390 without CONFIG_PCI still provides the asm-generic
> version of those, that is technically a mistake, and it would
> be nice not to.
>
> I'm guessing that there is enough kernel code that still expects
> these to be present for C, but if all rust code has the correct
> HAS_IOMEM dependencies, it would be cleaner not to reference
> since there is no correct way to call them without ioremap().

As things are right now, I think something like in [1] should work, but we'd
also need to cfg-gate every single doc-test that uses I/O primitives, which is
slightly annoying.

In any case, I'm not sure it would be a huge benefit anyway. Unlike in C, where
I/O accessors operate on raw void pointers, the Rust primitives are typed. So,
users have no way of actually calling them without being able to obtain a
mapping in the first place.

- Danilo

[1]

diff --git a/rust/helpers/io.c b/rust/helpers/io.c
index 1edbc274951c..29120ea9d7d8 100644
--- a/rust/helpers/io.c
+++ b/rust/helpers/io.c
@@ -19,7 +19,6 @@ __rust_helper void rust_helper_iounmap(void __iomem *addr)
{
iounmap(addr);
}
-#endif /* CONFIG_HAS_IOMEM */

__rust_helper u8 rust_helper_readb(const void __iomem *addr)
{
@@ -108,6 +107,7 @@ __rust_helper void rust_helper_writeq_relaxed(u64 value, void __iomem *addr)
writeq_relaxed(value, addr);
}
#endif
+#endif /* CONFIG_HAS_IOMEM */

__rust_helper resource_size_t rust_helper_resource_size(struct resource *res)
{
diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index d4063ee41200..d91fc2e4ae9b 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -728,6 +728,7 @@ fn io_addr_assert<U>(&self, offset: usize) -> usize {
}
}

+#[cfg(CONFIG_HAS_IOMEM)]
/// Implements [`IoCapable`] on `$mmio` for `$ty` using `$read_fn` and `$write_fn`.
macro_rules! impl_mmio_io_capable {
($mmio:ident, $(#[$attr:meta])* $ty:ty, $read_fn:ident, $write_fn:ident) => {
@@ -746,10 +747,14 @@ unsafe fn io_write(&self, value: $ty, address: usize) {
};
}

+#[cfg(CONFIG_HAS_IOMEM)]
// MMIO regions support 8, 16, and 32-bit accesses.
impl_mmio_io_capable!(Mmio, u8, readb, writeb);
+#[cfg(CONFIG_HAS_IOMEM)]
impl_mmio_io_capable!(Mmio, u16, readw, writew);
+#[cfg(CONFIG_HAS_IOMEM)]
impl_mmio_io_capable!(Mmio, u32, readl, writel);
+#[cfg(CONFIG_HAS_IOMEM)]
// MMIO regions on 64-bit systems also support 64-bit accesses.
impl_mmio_io_capable!(
Mmio,
@@ -843,10 +848,14 @@ pub fn relaxed(&self) -> &RelaxedMmio<SIZE> {
}
}

+#[cfg(CONFIG_HAS_IOMEM)]
// MMIO regions support 8, 16, and 32-bit accesses.
impl_mmio_io_capable!(RelaxedMmio, u8, readb_relaxed, writeb_relaxed);
+#[cfg(CONFIG_HAS_IOMEM)]
impl_mmio_io_capable!(RelaxedMmio, u16, readw_relaxed, writew_relaxed);
+#[cfg(CONFIG_HAS_IOMEM)]
impl_mmio_io_capable!(RelaxedMmio, u32, readl_relaxed, writel_relaxed);
+#[cfg(CONFIG_HAS_IOMEM)]
// MMIO regions on 64-bit systems also support 64-bit accesses.
impl_mmio_io_capable!(
RelaxedMmio,