[PATCH V1 4/4] PCI: Re-check function 0 reset methods after scanning its slot

From: Vidya Sagar

Date: Thu Nov 19 2026 - 05:10:01 EST


pci_device_add() probes a device's reset methods before it adds the
device to bus->devices. pci_scan_slot() adds function 0 first, so when
function 0 is probed it is still alone on its bus and the "bus" probe,
which refuses a bus reset if other devices share the bus, succeeds. Once
the other functions are added, the same check refuses the reset, so
function 0 of a multi-function device lists "bus" but can't use it. For
example, on function 0 of a 4-function ConnectX-8:

# cat /sys/bus/pci/devices/0002:01:00.0/reset_method
flr bus cxl_bus
# echo bus > /sys/bus/pci/devices/0002:01:00.0/reset_method
bash: echo: write error: Invalid argument
# echo default > /sys/bus/pci/devices/0002:01:00.0/reset_method
# cat /sys/bus/pci/devices/0002:01:00.0/reset_method
flr

The other functions are probed after function 0 is on the bus, so they
are not affected.

At the end of pci_scan_slot(), if the scan added function 0 and other
functions, probe each of function 0's reset methods again and drop the
ones that now fail. By then device_add() has already created function
0's "reset_method" file and sent the "add" uevent, so userspace may have
changed the list already. Add pci_prune_reset_methods() for this: unlike
pci_init_reset_methods(), it only removes methods and keeps the order of
the rest. Hold the device lock while doing so. reset_method_store()
takes the same lock, so a write can't land between reading the list and
storing the result. pci_bus_add_device() takes this lock later in the
same scan, for device_initial_probe().

No driver can bind to function 0, and its runtime PM is not enabled,
until pci_bus_add_device(), which runs after the slot scan.

Signed-off-by: Vidya Sagar <vidyas@xxxxxxxxxx>
---
drivers/pci/pci.c | 35 +++++++++++++++++++++++++++++++++++
drivers/pci/pci.h | 1 +
drivers/pci/probe.c | 16 +++++++++++++++-
3 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 918a4d95f914..d9b26208a76c 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5306,6 +5306,41 @@ void pci_init_reset_methods(struct pci_dev *dev)
dev->reset_methods[i] = 0;
}

+/**
+ * pci_prune_reset_methods - drop reset methods that no longer work
+ * @dev: PCI device to check
+ *
+ * Probe each of @dev's current reset methods again and remove the ones
+ * that fail, keeping the order of the rest. Unlike pci_init_reset_methods(),
+ * this never adds a method, so a list set through sysfs is kept, except for
+ * methods that can no longer work.
+ *
+ * Context: The caller must hold the device lock of @dev, which serializes
+ * this against resets and writes to the "reset_method" sysfs file.
+ */
+void pci_prune_reset_methods(struct pci_dev *dev)
+{
+ u8 methods[PCI_NUM_RESET_METHODS] = {};
+ int m, i, n = 0, rc;
+
+ might_sleep();
+ device_lock_assert(&dev->dev);
+
+ for (i = 0; i < PCI_NUM_RESET_METHODS; i++) {
+ m = dev->reset_methods[i];
+ if (!m)
+ break;
+
+ rc = pci_reset_fn_methods[m].reset_fn(dev, PCI_RESET_PROBE);
+ if (!rc)
+ methods[n++] = m;
+ else if (rc != -ENOTTY)
+ break;
+ }
+
+ memcpy(dev->reset_methods, methods, sizeof(dev->reset_methods));
+}
+
/**
* pci_reset_function - quiesce and reset a PCI device function
* @dev: PCI device to reset
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 77efa4c5c960..6e82c82acdb6 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -242,6 +242,7 @@ int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vmai,

bool pci_reset_supported(struct pci_dev *dev);
void pci_init_reset_methods(struct pci_dev *dev);
+void pci_prune_reset_methods(struct pci_dev *dev);
int pci_bridge_secondary_bus_reset(struct pci_dev *dev);
int pci_bus_error_reset(struct pci_dev *dev);
int pci_try_reset_bridge(struct pci_dev *bridge);
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 9a178e0e5f7f..a36bf679c393 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -2874,7 +2874,7 @@ static int only_one_child(struct pci_bus *bus)
*/
int pci_scan_slot(struct pci_bus *bus, int devfn)
{
- struct pci_dev *dev;
+ struct pci_dev *dev, *dev0 = NULL;
int fn = 0, nr = 0;

if (only_one_child(bus) && (devfn > 0))
@@ -2887,6 +2887,8 @@ int pci_scan_slot(struct pci_bus *bus, int devfn)
nr++;
if (fn > 0)
dev->multifunction = 1;
+ else
+ dev0 = dev;
} else if (fn == 0) {
/*
* Function 0 is required unless we are running on
@@ -2899,6 +2901,18 @@ int pci_scan_slot(struct pci_bus *bus, int devfn)
fn = next_fn(bus, dev, fn);
} while (fn >= 0);

+ /*
+ * Function 0's reset methods were probed before the other functions
+ * were on bus->devices, so a bus reset looked possible then. Its
+ * "reset_method" file already exists and userspace may have changed
+ * the list, so only drop the methods that can't work now. The device
+ * lock keeps a write to that file from landing in the middle.
+ */
+ if (dev0 && !pci_dev_is_added(dev0) && nr > 1) {
+ guard(device)(&dev0->dev);
+ pci_prune_reset_methods(dev0);
+ }
+
/* Only one slot has PCIe device */
if (bus->self && nr)
pcie_aspm_init_link_state(bus->self);
--
2.43.0