From: Ravi Kumar Bandi <ravib@amazon.com>
To: <kwilczynski@kernel.org>
Cc: <bhelgaas@google.com>, <ilpo.jarvinen@linux.intel.com>,
<linux-kernel@vger.kernel.org>, <linux-pci@vger.kernel.org>,
<ravib@amazon.com>
Subject: Re: [PATCH] PCI/sysfs: Check IORESOURCE_DISABLED in resource mmap handler
Date: Wed, 13 May 2026 04:04:01 +0000 [thread overview]
Message-ID: <20260513040401.16035-1-ravib@amazon.com> (raw)
In-Reply-To: <20260512094621.GB1539393@rocinante>
On Tue, May 12, 2026 at 18:26:14 +0900, Krzysztof wrote:
> > > > 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.
> > >
> > > Which driver disables resources? Would this be some Amazon-specific thing
> > > you are trying to fix? Or are you just manually disabling a given device
> > > using sysfs, or something like this?
> > >
> > > What is your use case here?
Thank you Krzysztof, Ilpo for reviewing the patch.
The use case is surprise removal of the PCIe device that does not have a
PRSNT# routed to the CPU. Based on other internal states, the
proprietary endpoint driver marks the BAR resources as disabled by
updating this flag to prevent further userspace access to the mermory
regions (mmap) to prevent kernel hang/crash. Currently, BAR memory
regions will be allowed to be mmapped using the sysfs resource files
even if drivers soft_unplug the device.
> > > For "soft-unplugged" device we have pci_dev_set_disconnected(), but this
> > > does not check current flags set.
Thanks for the suggestion. pci_dev_set_disconnected() couldn't be used
directly as it's defined in drivers/pci/pci.h and not part of exported
kernel headers, the endpoint driver is currently setting it as below
along with the flags:
xchg(&dev->error_state, pci_channel_io_perm_failure);
smp_wmb();
[...]
pdev->resource[bar].flags |= IORESOURCE_DISABLED;
[...]
I'll replace checking for IORESOURCE_DISABLED flag
with pci_dev_is_disconnected() instead and make endpoint driver adapt
to using pci_dev_set_disconnected().
> > > > 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.
> > >
> > > Can you explain how did you do this?
Based on the internal logic, endpoint driver sets the resource flags as
below to indicate soft_unplug, which pci-sysfs use to to reject mmap
of the resources.
pdev->resource[bar].flags |= IORESOURCE_DISABLED;
> > > > @@ -1089,6 +1089,9 @@ static int pci_mmap_resource(struct kobject *kobj
> > > > if (ret)
> > > > return ret;
> > > >
> > > > + if (res->flags & IORESOURCE_DISABLED)
> > > > + return -ENODEV;
> > > > +
> > >
> > > This probably would be better if it checked IORESOURCE_DISABLED and
> > > IORESOURCE_UNSET, but then probably using resource_assigned() would
> > > be even better.
Thank you, but as you suggested above, I will update the patch to use
pci_dev_is_disconnected() instead.
Regards,
Ravi Kumar Bandi
next prev parent reply other threads:[~2026-05-13 4:04 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 [this message]
2026-05-13 5:07 ` Krzysztof Wilczyński
2026-05-13 6:48 ` Ravi Kumar Bandi
2026-05-13 4:13 ` [PATCH v2] PCI/sysfs: Check if device disabled " Ravi Kumar Bandi
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=20260513040401.16035-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