Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH] rust: io: mem: Fix exclusive MMIO region requests
@ 2026-10-28  2:00 Kwanghoon Son
       [not found] ` <sashiko-outbox-187333@kernel.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Kwanghoon Son @ 2026-10-28  2:00 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, Daniel Almeida, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: driver-core, rust-for-linux, linux-kernel, Kwanghoon Son

C's request_mem_region() reserves MMIO in the global resource tree.
Rust's ExclusiveIoMem reserves it under the device's resource instead,
so the global tree may not see the reservation.

Request the region from iomem_resource, matching request_mem_region()
and the existing release_mem_region() call.

Fixes: 1d0d4b28513b ("rust: io: mem: add a generic iomem abstraction")

Signed-off-by: Kwanghoon Son <asdfkwang@gmail.com>
---
The bug can be reproduced with QEMU arm64 virt's existing DT node
(CONFIG_FW_CFG_SYSFS=n):

    fw-cfg@9020000 {
        compatible = "qemu,fw-cfg-mmio";
        reg = <0 0x09020000 0 0x18>;
    };

A small Rust platform driver matches this node. Its probe maps the
resource and returns the mapping as driver data (excerpt):

    let io = pdev.io_request_by_index(0).ok_or(ENODEV)?
        .iomap_exclusive()?
        .into_devres()?;

Unbind and bind that driver again:

    cd /sys/bus/platform/drivers/mmio_repro
    echo 9020000.fw-cfg > unbind
    echo 9020000.fw-cfg > bind

Before the fix, unbind prints "Trying to free nonexistent resource"
and bind fails with EBUSY. With the fix, 100 cycles pass. This was
repeated in three fresh guests for each kernel.
---
 rust/kernel/io/mem.rs      | 2 +-
 rust/kernel/io/resource.rs | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/io/mem.rs b/rust/kernel/io/mem.rs
index 32a919099dcda79bc4b795a8a1c5f553c989b9b7..80da665821c036257900c6ea45b9419cb573c187 100644
--- a/rust/kernel/io/mem.rs
+++ b/rust/kernel/io/mem.rs
@@ -196,7 +196,7 @@ fn ioremap(dev: &'a Device<Bound>, resource: &Resource) -> Result<Self> {
         let size = resource.size();
         let name = resource.name().unwrap_or_default();
 
-        let region = resource
+        let region = Resource::iomem()
             .request_region(
                 start,
                 size,
diff --git a/rust/kernel/io/resource.rs b/rust/kernel/io/resource.rs
index 758ce62b3cce33c14e1e4c56a793af0e00657aa6..c99a2530186fc56bdc3405adc15d0801b98eae08 100644
--- a/rust/kernel/io/resource.rs
+++ b/rust/kernel/io/resource.rs
@@ -79,6 +79,12 @@ unsafe impl Sync for Region {}
 pub struct Resource(Opaque<bindings::resource>);
 
 impl Resource {
+    /// Returns the global I/O memory resource root.
+    pub(crate) fn iomem() -> &'static Self {
+        // SAFETY: iomem_resource is permanent; C serializes resource-tree updates.
+        unsafe { Self::from_raw(&raw mut bindings::iomem_resource) }
+    }
+
     /// Creates a reference to a [`Resource`] from a valid pointer.
     ///
     /// # Safety

---
base-commit: 8a36f53fd5ec3eff01f1b637d42cbdc3821d0493
change-id: 20261027-b4-rust-iomem-origin-master-fix-885f1ea244fb

Best regards,
-- 
Kwanghoon Son <asdfkwang@gmail.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] rust: io: mem: Fix exclusive MMIO region requests
       [not found] ` <sashiko-outbox-187333@kernel.org>
@ 2026-10-30  2:11   ` Kwanghoon Son
  0 siblings, 0 replies; 2+ messages in thread
From: Kwanghoon Son @ 2026-10-30  2:11 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: ojeda, gary, rust-for-linux

On Wed, Oct 28, 2026 at 02:12:54AM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> - [Low] rust: io: dead_code warning in Resource::iomem() when CONFIG_HAS_IOMEM=n
> - [Low] rust: io: missing #[inline] annotation on Resource::iomem()
> --
> 
> commit 87df13a505aabe5d12ec487bcf72bfc859780ac8
> Author: Kwanghoon Son <asdfkwang@gmail.com>
> 
> rust: io: mem: Fix exclusive MMIO region requests
> 
> This commit updates ExclusiveIoMem::ioremap() to request the MMIO region
> from the global iomem_resource root via a new Resource::iomem() helper
> rather than from the device's own resource.
> 
> > diff --git a/rust/kernel/io/resource.rs b/rust/kernel/io/resource.rs
> > index 758ce62b3cce3..c99a2530186fc 100644
> > --- a/rust/kernel/io/resource.rs
> > +++ b/rust/kernel/io/resource.rs
> > @@ -79,6 +79,12 @@ unsafe impl Sync for Region {}
> >  pub struct Resource(Opaque<bindings::resource>);
> >  
> >  impl Resource {
> > +    /// Returns the global I/O memory resource root.
> > +    pub(crate) fn iomem() -> &'static Self {
> > +        // SAFETY: iomem_resource is permanent; C serializes resource-tree updates.
> > +        unsafe { Self::from_raw(&raw mut bindings::iomem_resource) }
> > +    }
> 
> [Severity: Low]
> Will Resource::iomem() in rust/kernel/io/resource.rs trigger a dead_code
> warning (or a build failure with CONFIG_WERROR=y) when CONFIG_RUST=y and
> CONFIG_HAS_IOMEM=n (for example, on ARCH=um or s390 without CONFIG_PCI)?
> 
> In rust/kernel/io.rs, the mem module is gated on CONFIG_HAS_IOMEM while the
> resource module is compiled unconditionally:
> 
>     #[cfg(CONFIG_HAS_IOMEM)]
>     pub mod mem;
>     pub mod poll;
>     pub mod register;
>     pub mod resource;
> 
> Since the only caller of pub(crate) fn iomem() is ExclusiveIoMem::ioremap() in
> rust/kernel/io/mem.rs, should Resource::iomem() also be annotated with
> #[cfg(CONFIG_HAS_IOMEM)] (or made pub)?

I reproduced the dead_code warning.
I'll add #[cfg(CONFIG_HAS_IOMEM)] in v2, matching the condition on its only caller.

> 
> [Severity: Low]
> This isn't a bug, but should Resource::iomem() in rust/kernel/io/resource.rs
> also have an #[inline] attribute, matching the other small helper functions in
> the kernel crate abstractions?

I'd prefer to follow those and leave it without the attribute, unless
there is a specific reason to add it here.

I'll wait more reviews on this and send send v2!

Best regards,
Kwang.

> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20261027-b4-rust-iomem-origin-master-fix-v1-1-16215ae197af@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-10-30  2:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-28  2:00 [PATCH] rust: io: mem: Fix exclusive MMIO region requests Kwanghoon Son
     [not found] ` <sashiko-outbox-187333@kernel.org>
2026-10-30  2:11   ` Kwanghoon Son

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox