All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] PCI/IOV: Don't make sriov_numvfs reads wait while VFs are enabled
@ 2026-11-20  8:01 Ionut Nechita (Wind River)
  2026-11-20  8:08 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ionut Nechita (Wind River) @ 2026-11-20  8:01 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: linux-pci, linux-kernel, Pierre Crégut, Alex Williamson,
	Ionut Nechita

From: Ionut Nechita <ionut.nechita@windriver.com>

Since commit 35ff867b7657 ("PCI/IOV: Serialize sysfs sriov_numvfs
reads vs writes"), sriov_numvfs_show() takes the device lock so that a
read does not return a stale num_VFs while sriov_numvfs_store() is
enabling VFs and they are becoming visible. The store holds the device
lock for the whole SR-IOV change, and disabling SR-IOV can hold it for
a very long time: with vfio-pci, sriov_disable() waits for the VF
driver unbind, which waits for the userspace driver to release the VF.

  sriov_numvfs_store                       sriov_numvfs_show
    device_lock(PF)                          device_lock(PF)  <-- waits
    vfio_pci_core_sriov_configure
      sriov_disable
        pci_iov_remove_virtfn
          device_release_driver(VF)
            vfio_unregister_group_dev()      <-- waits for the vfio user

Until the user releases the VF, every read of sriov_numvfs on the PF
sleeps in D state. Inventory tools and agents that read sriov_numvfs
for all PCI devices hang with it. This was hit with an Intel FEC
accelerator on vfio-pci, where the SR-IOV FEC operator reconfigures the
PF while a DPDK application holds one of its VFs.

num_VFs is only set once the VFs have been added, and only cleared
once they have all been removed. A non-zero num_VFs therefore never
reports fewer VFs than exist, so it can be returned without the lock.
Only wait for the device lock when it is contended and num_VFs is 0,
which is the case the existing serialization is for: an enable in
progress. Use READ_ONCE()/WRITE_ONCE() for the lockless access.

While SR-IOV is being disabled, a reader now gets the old num_VFs
until sriov_disable() clears it, instead of waiting for 0. That is the
value it would have read just before the write started, and it is
still an upper bound of the VFs that exist.

The remaining wait, for an enable in progress or another holder of the
device lock while num_VFs is 0, can still be long. Make it
interruptible, so that a reader can at least be killed.

Fixes: 35ff867b7657 ("PCI/IOV: Serialize sysfs sriov_numvfs reads vs writes")
Signed-off-by: Ionut Nechita <ionut.nechita@windriver.com>
---
 drivers/pci/iov.c | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index 9185934a9..20d0d03e5 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -420,10 +420,28 @@ static ssize_t sriov_numvfs_show(struct device *dev,
 	struct pci_dev *pdev = to_pci_dev(dev);
 	u16 num_vfs;
 
-	/* Serialize vs sriov_numvfs_store() so readers see valid num_VFs */
-	device_lock(&pdev->dev);
-	num_vfs = pdev->sriov->num_VFs;
-	device_unlock(&pdev->dev);
+	/*
+	 * Serialize vs sriov_numvfs_store() so readers see valid num_VFs
+	 * while VFs are being enabled.  num_VFs is only set once the VFs
+	 * have been added, and only cleared once they have all been
+	 * removed, so a non-zero value never reports fewer VFs than exist
+	 * and can be returned without waiting.  Disabling SR-IOV can hold
+	 * the device lock for a long time, e.g. while a VF driver unbind
+	 * waits for a vfio user to release the VF, and readers should not
+	 * sleep behind that.
+	 */
+	if (device_trylock(&pdev->dev)) {
+		num_vfs = pdev->sriov->num_VFs;
+		device_unlock(&pdev->dev);
+	} else {
+		num_vfs = READ_ONCE(pdev->sriov->num_VFs);
+		if (!num_vfs) {
+			if (device_lock_interruptible(&pdev->dev))
+				return -EINTR;
+			num_vfs = pdev->sriov->num_VFs;
+			device_unlock(&pdev->dev);
+		}
+	}
 
 	return sysfs_emit(buf, "%u\n", num_vfs);
 }
@@ -716,7 +734,7 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
 		goto err_pcibios;
 
 	kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE);
-	iov->num_VFs = nr_virtfn;
+	WRITE_ONCE(iov->num_VFs, nr_virtfn);
 
 	return 0;
 
@@ -764,7 +782,7 @@ static void sriov_disable(struct pci_dev *dev)
 	if (iov->link != dev->devfn)
 		sysfs_remove_link(&dev->dev.kobj, "dep_link");
 
-	iov->num_VFs = 0;
+	WRITE_ONCE(iov->num_VFs, 0);
 	pci_iov_set_numvfs(dev, 0);
 }
 
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] PCI/IOV: Don't make sriov_numvfs reads wait while VFs are enabled
  2026-11-20  8:01 [PATCH] PCI/IOV: Don't make sriov_numvfs reads wait while VFs are enabled Ionut Nechita (Wind River)
@ 2026-11-20  8:08 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-20  8:08 UTC (permalink / raw)
  To: Ionut Nechita (Wind River); +Cc: linux-pci

> From: Ionut Nechita <ionut.nechita@windriver.com>
> 
> Since commit 35ff867b7657 ("PCI/IOV: Serialize sysfs sriov_numvfs
> reads vs writes"), sriov_numvfs_show() takes the device lock so that a
> read does not return a stale num_VFs while sriov_numvfs_store() is
> enabling VFs and they are becoming visible. The store holds the device
> lock for the whole SR-IOV change, and disabling SR-IOV can hold it for
> a very long time: with vfio-pci, sriov_disable() waits for the VF
> driver unbind, which waits for the userspace driver to release the VF.
> 
>   sriov_numvfs_store                       sriov_numvfs_show
>     device_lock(PF)                          device_lock(PF)  <-- waits
>     vfio_pci_core_sriov_configure
>       sriov_disable
>         pci_iov_remove_virtfn
> [ ... ]
> device lock while num_VFs is 0, can still be long. Make it
> interruptible, so that a reader can at least be killed.
> 
> Fixes: 35ff867b7657 ("PCI/IOV: Serialize sysfs sriov_numvfs reads vs writes")
> Signed-off-by: Ionut Nechita <ionut.nechita@windriver.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120080143.56701-1-ionut.nechita@windriver.com?part=1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-20  8:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  8:01 [PATCH] PCI/IOV: Don't make sriov_numvfs reads wait while VFs are enabled Ionut Nechita (Wind River)
2026-11-20  8:08 ` sashiko-bot

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.