[PATCH] PCI/sysfs: Check IORESOURCE_DISABLED in resource mmap handler

From: Ravi Kumar Bandi

Date: Tue May 12 2026 - 04:49:27 EST


pci_mmap_resource() does not check IORESOURCE_DISABLED before mapping
a PCI BAR resource into userspace. This allows new mmaps to succeed
even after a device has been marked disabled or soft-unplugged by the
driver to prevent further access.

Add the check to return -ENODEV when the resource is disabled, blocking
new userspace mmaps of BAR resources after device removal.

Tested by marking the PCI BAR resource as disabled and verifying that
a subsequent mmap attempt fails with -ENODEV.

$ sudo python3 -c "
import os, mmap, errno
try:
fd = os.open('/sys/bus/pci/devices/0001:01:00.0/resource0',os.O_RDONLY)
mmap.mmap(fd, 4096, access=mmap.ACCESS_READ)
print('mmap succeeded')
except OSError as e:
print(f'mmap failed - {e}')
"
mmap failed - [Errno 19] No such device
$

Signed-off-by: Ravi Kumar Bandi <ravib@xxxxxxxxxx>
---
drivers/pci/pci-sysfs.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index d37860841260..61ae259f8ca8 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -1089,6 +1089,9 @@ static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *a
if (ret)
return ret;

+ if (res->flags & IORESOURCE_DISABLED)
+ return -ENODEV;
+
if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
return -EINVAL;

--
2.47.3