Skip to contentSkip to conversation

rust: io: gate ioremap/iounmap on CONFIG_HAS_IOMEM

PATCH Superseded Danilo Krummrich 1 participant 2 messages Aug 3

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/2 rust: io: gate ioremap/iounmap on CONFIG_HAS_IOMEM

s390 does not provide ioremap()/iounmap() when CONFIG_HAS_IOMEM is not set (which requires CONFIG_PCI on that architecture). This causes a build failure with Rust enabled on e.g. s390 allnoconfig:

In file included from rust/helpers/helpers.c:68:
rust/helpers/io.c:8:9: error: call to undeclared function 'ioremap'; ISO C99 and later do not support implicit function declarations
[-Wimplicit-function-declaration]
8 | return ioremap(offset, size);
| ^
rust/helpers/io.c:19:2: error: call to undeclared function 'iounmap'; ISO C99 and later do not support implicit function declarations
[-Wimplicit-function-declaration]
19 | iounmap(addr);

Guard the C helpers behind #ifdef CONFIG_HAS_IOMEM and provide a cfg-gated stub in IoMem::ioremap() that returns -EINVAL when IOMEM is unavailable, mirroring the C pattern used by devm_platform_ioremap_resource() and friends.

---
 rust/helpers/io.c     |  2 ++
 rust/kernel/io/mem.rs | 11 ++++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
rust/helpers/io.c +2 -0
index 397810864a24..1edbc274951c 100644
@@ -3,6 +3,7 @@
 #include <linux/io.h>
 #include <linux/ioport.h>
 
+#ifdef CONFIG_HAS_IOMEM
 __rust_helper void __iomem *rust_helper_ioremap(phys_addr_t offset, size_t size)
 {
 	return ioremap(offset, size);
@@ -18,6 +19,7 @@ __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)
 {
rust/kernel/io/mem.rs +10 -1
index fc2a3e24f8d5..3f596bc4fb26 100644
@@ -233,6 +233,12 @@ pub struct IoMem<'a, const SIZE: usize = 0> {
 }
 
 impl<'a, const SIZE: usize> IoMem<'a, SIZE> {
+    #[cfg(not(CONFIG_HAS_IOMEM))]
+    fn ioremap(_dev: &'a Device<Bound>, _resource: &Resource) -> Result<Self> {
+        Err(EINVAL)
+    }
+
+    #[cfg(CONFIG_HAS_IOMEM)]
     fn ioremap(dev: &'a Device<Bound>, resource: &Resource) -> Result<Self> {
         // Note: Some ioremap() implementations use types that depend on the CPU
         // word width rather than the bus address width.
@@ -286,8 +292,11 @@ pub fn into_devres(self) -> Result<Devres<IoMem<'static, SIZE>>> {
 
 impl<const SIZE: usize> Drop for IoMem<'_, SIZE> {
     fn drop(&mut self) {
+        #[cfg(CONFIG_HAS_IOMEM)]
         // SAFETY: Safe as by the invariant of `Io`.
-        unsafe { bindings::iounmap(self.io.addr() as *mut c_void) }
+        unsafe {
+            bindings::iounmap(self.io.addr() as *mut c_void)
+        }
     }
 }
 

base-commit: 667d0fb32149f023b8b34a1f6f3d384556eafb5a

Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 2/2 rust: io: gate ioremap doctests on CONFIG_HAS_IOMEM

The doc examples in io.rs and devres.rs directly call bindings::ioremap() and bindings::iounmap(), which do not exist when CONFIG_HAS_IOMEM is not set. This causes build failures with CONFIG_RUST_KERNEL_DOCTESTS=y on such configurations (e.g. s390 allnoconfig).

Gate the affected doctests with #![cfg(CONFIG_HAS_IOMEM)] so they are skipped when IOMEM is unavailable.

Fixes 3f70ebe63858 ("s390: Enable Rust support")
---
 rust/kernel/devres.rs | 1 +
 rust/kernel/io.rs     | 1 +
 2 files changed, 2 insertions(+)
rust/kernel/devres.rs +1 -0
index 11ce500e9b76..8ff8aedf251a 100644
@@ -59,6 +59,7 @@ struct Inner<T> {
 /// # Examples
 ///
 /// ```no_run
+/// # #![cfg(CONFIG_HAS_IOMEM)]
 /// use kernel::{
 ///     bindings,
 ///     device::{
rust/kernel/io.rs +1 -0
index fcc7678fd9e3..9e0c8a891656 100644
@@ -80,6 +80,7 @@ pub fn maxsize(&self) -> usize {
 /// # Examples
 ///
 /// ```no_run
+/// # #![cfg(CONFIG_HAS_IOMEM)]
 /// use kernel::{
 ///     bindings,
 ///     ffi::c_void,
Signature · 1 line
2.55.0