All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vidya Sagar <vidyas@nvidia.com>
To: Bjorn Helgaas <bhelgaas@google.com>,
	Dan Williams <djbw@kernel.org>,
	"Dave Jiang" <dave.jiang@intel.com>,
	Jonathan Cameron <Jonathan.Cameron@huawei.com>,
	Joerg Roedel <joerg.roedel@amd.com>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	"Nicolin Chen" <nicolinc@nvidia.com>,
	Kevin Tian <kevin.tian@intel.com>
Cc: <vsethi@nvidia.com>, <kthota@nvidia.com>,
	<mmaddireddy@nvidia.com>, <kumarahul@nvidia.com>,
	<sagar.tv@gmail.com>, <linux-pci@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, Vidya Sagar <vidyas@nvidia.com>
Subject: [PATCH V1 4/4] PCI: Re-check function 0 reset methods after scanning its slot
Date: Thu, 19 Nov 2026 15:36:28 +0530	[thread overview]
Message-ID: <20261119100628.1948990-5-vidyas@nvidia.com> (raw)
In-Reply-To: <20261119100628.1948990-1-vidyas@nvidia.com>

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@nvidia.com>
---
 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


  parent reply	other threads:[~2026-11-19 10:09 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 10:06 [PATCH V1 0/4] PCI: Don't list reset methods that can't reset the device Vidya Sagar
2026-11-19 10:06 ` [PATCH V1 1/4] PCI/CXL: Don't offer 'cxl_bus' reset where a bus reset can't work Vidya Sagar
2026-11-19 10:20   ` sashiko-bot
2026-11-19 10:06 ` [PATCH V1 2/4] PCI: Don't run IOMMU reset hooks when only probing reset methods Vidya Sagar
2026-11-19 10:14   ` sashiko-bot
2026-11-19 16:39   ` Nicolin Chen
2026-11-19 10:06 ` [PATCH V1 3/4] PCI/sysfs: Take the device lock when setting " Vidya Sagar
2026-11-19 10:20   ` sashiko-bot
2026-11-19 10:06 ` Vidya Sagar [this message]
2026-11-19 10:32   ` [PATCH V1 4/4] PCI: Re-check function 0 reset methods after scanning its slot sashiko-bot

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=20261119100628.1948990-5-vidyas@nvidia.com \
    --to=vidyas@nvidia.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=bhelgaas@google.com \
    --cc=dave.jiang@intel.com \
    --cc=djbw@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=joerg.roedel@amd.com \
    --cc=kevin.tian@intel.com \
    --cc=kthota@nvidia.com \
    --cc=kumarahul@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=mmaddireddy@nvidia.com \
    --cc=nicolinc@nvidia.com \
    --cc=sagar.tv@gmail.com \
    --cc=vsethi@nvidia.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.