From: Alex Williamson <alex.williamson@nvidia.com>
To: kvm <kvm@vger.kernel.org>, Alex Williamson <alex@shazbot.org>
Cc: Alex Williamson <alex.williamson@nvidia.com>,
skhawaja@google.com, Jason Gunthorpe <jgg@ziepe.ca>,
Bjorn Helgaas <bhelgaas@google.com>,
Kevin Tian <kevin.tian@intel.com>,
linux-kernel <linux-kernel@vger.kernel.org>,
linux-pci <linux-pci@vger.kernel.org>
Subject: [RFC PATCH 2/5] PCI: Add pci_reset_bus_cond() for a caller-gated slot or bus reset
Date: Tue, 11 Aug 2026 22:53:20 -0600 [thread overview]
Message-ID: <20260812045325.2733631-3-alex.williamson@nvidia.com> (raw)
In-Reply-To: <20260812045325.2733631-1-alex.williamson@nvidia.com>
pci_reset_bus() locks every device affected by a slot or bus reset,
performs the reset, and unlocks the devices, all internally. A caller
has no way to evaluate what the reset would actually touch.
Add pci_reset_bus_cond(), which takes a callback invoked on each
affected device once they are all locked. A nonzero return value from
the callback aborts the reset and the value is returned to the caller.
This allows, for instance, the caller to validate that SR-IOV is not
enabled on any affected device under device_lock, where the value is
known stable across the reset.
Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Alex Williamson <alex.williamson@nvidia.com>
---
drivers/pci/pci.c | 97 ++++++++++++++++++++++++++++++++++++++-------
include/linux/pci.h | 3 ++
2 files changed, 85 insertions(+), 15 deletions(-)
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index b40b00c0c0c9..06728137c407 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5567,9 +5567,36 @@ int pci_probe_reset_slot(struct pci_slot *slot)
}
EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
+/* Call @cb on every device a slot or bus reset affects, stopping on error. */
+static int pci_walk_reset_check(struct pci_bus *bus, struct pci_slot *slot,
+ int (*cb)(struct pci_dev *dev, void *data),
+ void *data)
+{
+ struct pci_dev *dev;
+ int rc;
+
+ list_for_each_entry(dev, &bus->devices, bus_list) {
+ if (slot && (!dev->slot || dev->slot != slot))
+ continue;
+ rc = cb(dev, data);
+ if (rc)
+ return rc;
+ if (dev->subordinate) {
+ rc = pci_walk_reset_check(dev->subordinate,
+ NULL, cb, data);
+ if (rc)
+ return rc;
+ }
+ }
+
+ return 0;
+}
+
/**
* pci_try_reset_slot - Try to reset a PCI slot
* @slot: PCI slot to reset
+ * @check: optional per-device callback that can abort the reset
+ * @data: opaque argument for @check
*
* A PCI bus may host multiple slots, each slot may support a reset mechanism
* independent of other slots. For instance, some slots may support slot power
@@ -5582,7 +5609,9 @@ EXPORT_SYMBOL_GPL(pci_probe_reset_slot);
*
* Same as above except return -EAGAIN if the slot cannot be locked
*/
-static int pci_try_reset_slot(struct pci_slot *slot)
+static int pci_try_reset_slot(struct pci_slot *slot,
+ int (*check)(struct pci_dev *dev, void *data),
+ void *data)
{
int rc;
@@ -5591,10 +5620,14 @@ static int pci_try_reset_slot(struct pci_slot *slot)
return rc;
if (pci_slot_trylock(slot)) {
- pci_slot_save_and_disable_locked(slot);
- might_sleep();
- rc = pci_reset_hotplug_slot(slot->hotplug, PCI_RESET_DO_RESET);
- pci_slot_restore_locked(slot);
+ rc = check ? pci_walk_reset_check(slot->bus, slot, check, data) : 0;
+ if (!rc) {
+ pci_slot_save_and_disable_locked(slot);
+ might_sleep();
+ rc = pci_reset_hotplug_slot(slot->hotplug,
+ PCI_RESET_DO_RESET);
+ pci_slot_restore_locked(slot);
+ }
pci_slot_unlock(slot);
} else
rc = -EAGAIN;
@@ -5626,10 +5659,14 @@ static int pci_bus_reset(struct pci_bus *bus, bool probe)
/**
* pci_try_reset_bus - Try to reset a PCI bus
* @bus: top level PCI bus to reset
+ * @check: optional per-device callback that can abort the reset
+ * @data: opaque argument for @check
*
* Same as above except return -EAGAIN if the bus cannot be locked
*/
-static int pci_try_reset_bus(struct pci_bus *bus)
+static int pci_try_reset_bus(struct pci_bus *bus,
+ int (*check)(struct pci_dev *dev, void *data),
+ void *data)
{
int rc;
@@ -5638,10 +5675,13 @@ static int pci_try_reset_bus(struct pci_bus *bus)
return rc;
if (pci_bus_trylock(bus)) {
- pci_bus_save_and_disable_locked(bus);
- might_sleep();
- rc = pci_bridge_secondary_bus_reset(bus->self);
- pci_bus_restore_locked(bus);
+ rc = check ? pci_walk_reset_check(bus, NULL, check, data) : 0;
+ if (!rc) {
+ pci_bus_save_and_disable_locked(bus);
+ might_sleep();
+ rc = pci_bridge_secondary_bus_reset(bus->self);
+ pci_bus_restore_locked(bus);
+ }
pci_bus_unlock(bus);
} else
rc = -EAGAIN;
@@ -5680,7 +5720,7 @@ static int pci_reset_bridge(struct pci_dev *bridge, bool restore)
list_for_each_entry(slot, &bus->slots, list) {
if (restore)
- ret = pci_try_reset_slot(slot);
+ ret = pci_try_reset_slot(slot, NULL, NULL);
else
ret = pci_slot_reset(slot, PCI_RESET_DO_RESET);
@@ -5694,7 +5734,7 @@ static int pci_reset_bridge(struct pci_dev *bridge, bool restore)
mutex_unlock(&pci_slot_mutex);
if (restore)
- return pci_try_reset_bus(bus);
+ return pci_try_reset_bus(bus, NULL, NULL);
return pci_bus_reset(bridge->subordinate, PCI_RESET_DO_RESET);
}
@@ -5724,16 +5764,43 @@ int pci_probe_reset_bus(struct pci_bus *bus)
}
EXPORT_SYMBOL_GPL(pci_probe_reset_bus);
+/**
+ * pci_reset_bus_cond - conditionally reset the slot or bus containing a device
+ * @pdev: top level PCI device to reset via slot/bus
+ * @check: optional callback invoked on each affected device before the reset
+ * @data: opaque argument passed to @check
+ *
+ * Reset the slot or bus containing @pdev. Once the entire physical bus/slot
+ * hierarchy is locked, @check (if not NULL) is called on each of those locked
+ * devices. A nonzero return aborts the reset and is returned to the caller,
+ * otherwise the reset proceeds.
+ *
+ * NB. @check runs with the full set of device_locks noted above held; callbacks
+ * must take these locking semantics into account. Use NULL to perform an
+ * unconditional reset.
+ *
+ * Return: 0 on success, -ENOTTY if @pdev is not resettable, -EAGAIN if the
+ * devices cannot be locked, or the value returned by @check.
+ */
+int pci_reset_bus_cond(struct pci_dev *pdev,
+ int (*check)(struct pci_dev *dev, void *data),
+ void *data)
+{
+ return !pci_probe_reset_slot(pdev->slot) ?
+ pci_try_reset_slot(pdev->slot, check, data) :
+ pci_try_reset_bus(pdev->bus, check, data);
+}
+EXPORT_SYMBOL_GPL(pci_reset_bus_cond);
+
/**
* pci_reset_bus - Try to reset a PCI bus
* @pdev: top level PCI device to reset via slot/bus
*
- * Same as above except return -EAGAIN if the bus cannot be locked
+ * Same as above without the conditional check.
*/
int pci_reset_bus(struct pci_dev *pdev)
{
- return (!pci_probe_reset_slot(pdev->slot)) ?
- pci_try_reset_slot(pdev->slot) : pci_try_reset_bus(pdev->bus);
+ return pci_reset_bus_cond(pdev, NULL, NULL);
}
EXPORT_SYMBOL_GPL(pci_reset_bus);
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 64b308b6e61c..ab41a49f4eb7 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -1480,6 +1480,9 @@ int pci_try_reset_function(struct pci_dev *dev);
int pci_probe_reset_slot(struct pci_slot *slot);
int pci_probe_reset_bus(struct pci_bus *bus);
int pci_reset_bus(struct pci_dev *dev);
+int pci_reset_bus_cond(struct pci_dev *pdev,
+ int (*check)(struct pci_dev *dev, void *data),
+ void *data);
void pci_reset_secondary_bus(struct pci_dev *dev);
void pcibios_reset_secondary_bus(struct pci_dev *dev);
void pci_update_resource(struct pci_dev *dev, int resno);
--
2.53.0
next prev parent reply other threads:[~2026-08-12 4:53 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-12 4:53 [RFC PATCH 0/5] PCI/vfio-pci: Guard resets against active SR-IOV VFs Alex Williamson
2026-08-12 4:53 ` [RFC PATCH 1/5] PCI: Refuse function reset of an SR-IOV PF with enabled VFs Alex Williamson
2026-08-13 23:22 ` Samiullah Khawaja
2026-08-14 14:37 ` Alex Williamson
2026-08-14 15:54 ` Samiullah Khawaja
2026-08-17 12:18 ` Jason Gunthorpe
2026-08-18 7:07 ` Tian, Kevin
2026-08-18 14:03 ` Jason Gunthorpe
2026-08-18 20:39 ` Alex Williamson
2026-08-20 22:38 ` Samiullah Khawaja
2026-08-20 23:37 ` Jason Gunthorpe
2026-08-21 2:20 ` Samiullah Khawaja
2026-09-04 19:06 ` Jason Gunthorpe
2026-08-27 5:35 ` Tian, Kevin
2026-09-22 1:47 ` Samiullah Khawaja
2026-08-25 23:55 ` Samiullah Khawaja
2026-08-12 4:53 ` Alex Williamson [this message]
2026-08-12 4:53 ` [RFC PATCH 3/5] vfio/pci: Refuse to reset " Alex Williamson
2026-08-12 4:53 ` [RFC PATCH 4/5] PCI: Export pci_reset_supported() Alex Williamson
2026-08-12 4:53 ` [RFC PATCH 5/5] vfio/pci: Use pci_reset_supported() in place of reset_works Alex Williamson
2026-08-12 21:45 ` [RFC PATCH 0/5] PCI/vfio-pci: Guard resets against active SR-IOV VFs Bjorn Helgaas
2026-08-12 22:53 ` Alex Williamson
2026-08-17 20:23 ` Tony Nguyen
2026-08-13 22:30 ` Samiullah Khawaja
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=20260812045325.2733631-3-alex.williamson@nvidia.com \
--to=alex.williamson@nvidia.com \
--cc=alex@shazbot.org \
--cc=bhelgaas@google.com \
--cc=jgg@ziepe.ca \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=skhawaja@google.com \
/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
all inboxes | Powered by JetHome®