From: Ravi Kumar Bandi <ravib@amazon.com>
To: <ravib@amazon.com>, <kwilczynski@kernel.org>,
<ilpo.jarvinen@linux.intel.com>
Cc: <bhelgaas@google.com>, <linux-kernel@vger.kernel.org>,
<linux-pci@vger.kernel.org>
Subject: [PATCH v2] PCI/sysfs: Check if device disabled in resource mmap handler
Date: Wed, 13 May 2026 04:13:32 +0000 [thread overview]
Message-ID: <20260513041332.24105-1-ravib@amazon.com> (raw)
In-Reply-To: <20260512084315.32564-1-ravib@amazon.com>
pci_mmap_resource() does not check if device is enabled 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@amazon.com>
---
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..83d580256c4b 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 (pci_dev_is_disconnected(pdev))
+ return -ENODEV;
+
if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
return -EINVAL;
--
2.47.3
prev parent reply other threads:[~2026-05-13 4:13 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-12 8:43 [PATCH] PCI/sysfs: Check IORESOURCE_DISABLED in resource mmap handler Ravi Kumar Bandi
2026-05-12 9:26 ` Krzysztof Wilczyński
2026-05-12 9:43 ` Ilpo Järvinen
2026-05-12 9:54 ` Krzysztof Wilczyński
2026-05-13 4:04 ` Ravi Kumar Bandi
2026-05-13 5:07 ` Krzysztof Wilczyński
2026-05-13 6:48 ` Ravi Kumar Bandi
2026-05-13 4:13 ` Ravi Kumar Bandi [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260513041332.24105-1-ravib@amazon.com \
--to=ravib@amazon.com \
--cc=bhelgaas@google.com \
--cc=ilpo.jarvinen@linux.intel.com \
--cc=kwilczynski@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox