From: Gary Guo `MmioRaw::new_region` may fail if the IO memory region is insufficiently aligned or not as large as the minimum size requested by the caller. In such cases, as `IoMem` is yet to be constructed, there is no destructor run and the IO mapping is leaked. Call `iounmap` manually in such case instead. Fixes: 1d0d4b28513b ("rust: io: mem: add a generic iomem abstraction") Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-26b373ff-c31b-4f16-9765-2ff8fbfd4184 Cc: stable@vger.kernel.org Signed-off-by: Gary Guo --- rust/kernel/io/mem.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/rust/kernel/io/mem.rs b/rust/kernel/io/mem.rs index 32a919099dcd..5bf5766ad36a 100644 --- a/rust/kernel/io/mem.rs +++ b/rust/kernel/io/mem.rs @@ -294,7 +294,14 @@ fn ioremap(dev: &'a Device, resource: &Resource) -> Result { return Err(ENOMEM); } - let io = MmioRaw::new_region(addr as usize, size)?; + let io = match MmioRaw::new_region(addr.addr(), size) { + Ok(io) => io, + Err(err) => { + // SAFETY: `addr` is the returned address of `ioremap`/`ioremap_np` and is not null. + unsafe { bindings::iounmap(addr) }; + Err(err)? + } + }; Ok(IoMem { dev, io }) } --- base-commit: 7cfbf79c66c63e807ea452abf0546e6f6d6833a6 change-id: 20261118-dev-iomem-eb3b678b5a8b Best regards, -- Gary Guo