[PATCH v1 1/1] PCI/IOV: Add nested locking in sriov_add_vfs/sriov_del_vfs for complete serialization
From: Ionut Nechita (Wind River)
Date: Sat Feb 14 2026 - 14:34:30 EST
From: Ionut Nechita <ionut.nechita@xxxxxxxxxxxxx>
After reverting commit 05703271c3cd ("PCI/IOV: Add PCI rescan-remove
locking when enabling/disabling SR-IOV") and moving the lock to
sriov_numvfs_store(), the path through driver .remove() (e.g. rmmod,
or manual unbind) that calls pci_disable_sriov() directly remains
unprotected against concurrent hotplug events. This affects any SR-IOV
capable driver that calls pci_disable_sriov() from its .remove()
callback (i40e, ice, mlx5, bnxt, etc.).
On s390, platform-generated hot-unplug events for VFs can race with
sriov_del_vfs() when a PF driver is being unloaded. The platform event
handler takes pci_rescan_remove_lock, but sriov_del_vfs() does not,
leading to double removal and list corruption.
We cannot use a plain mutex_lock() here because sriov_del_vfs() may also
be called from paths that already hold pci_rescan_remove_lock (e.g.
remove_store -> pci_stop_and_remove_bus_device_locked, or
sriov_numvfs_store with the lock taken by the previous patch). Using
mutex_lock() in those cases would deadlock.
Instead, introduce owner tracking for pci_rescan_remove_lock via a new
pci_lock_rescan_remove_nested() helper. This function checks if the
current task already holds the lock:
- If the lock is not held: acquires it and returns true, providing
full serialization against concurrent hotplug events (including
platform-generated events on s390).
- If the lock is already held by the current task (nested call from
remove_store or sriov_numvfs_store paths): returns false without
re-acquiring, avoiding deadlock while the caller already provides
the necessary serialization.
- If the lock is held by another task (concurrent hotplug): blocks
until the lock is released, then acquires it, providing complete
serialization. This is the key improvement over a trylock approach.
Fixes: 18f9e9d150fc ("PCI/IOV: Factor out sriov_add_vfs()")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ionut Nechita <ionut_n2001@xxxxxxxxx>
Signed-off-by: Ionut Nechita <ionut.nechita@xxxxxxxxxxxxx>
---
drivers/pci/iov.c | 10 ++++++++++
drivers/pci/pci.h | 1 +
drivers/pci/probe.c | 12 ++++++++++++
3 files changed, 23 insertions(+)
diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index 4a659c34935e..38372ac0e2ad 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -629,19 +629,25 @@ static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
{
unsigned int i;
int rc;
+ bool nested;
if (dev->no_vf_scan)
return 0;
+ nested = !pci_lock_rescan_remove_nested();
for (i = 0; i < num_vfs; i++) {
rc = pci_iov_add_virtfn(dev, i);
if (rc)
goto failed;
}
+ if (!nested)
+ pci_unlock_rescan_remove();
return 0;
failed:
while (i--)
pci_iov_remove_virtfn(dev, i);
+ if (!nested)
+ pci_unlock_rescan_remove();
return rc;
}
@@ -764,10 +770,14 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
static void sriov_del_vfs(struct pci_dev *dev)
{
struct pci_sriov *iov = dev->sriov;
+ bool nested;
int i;
+ nested = !pci_lock_rescan_remove_nested();
for (i = 0; i < iov->num_VFs; i++)
pci_iov_remove_virtfn(dev, i);
+ if (!nested)
+ pci_unlock_rescan_remove();
}
static void sriov_disable(struct pci_dev *dev)
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index c8a0522e2e1f..7d3b705728fd 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -367,6 +367,7 @@ static inline void pci_remove_legacy_files(struct pci_bus *bus) { }
/* Lock for read/write access to pci device and bus lists */
extern struct rw_semaphore pci_bus_sem;
extern struct mutex pci_slot_mutex;
+bool pci_lock_rescan_remove_nested(void);
extern raw_spinlock_t pci_lock;
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 7711f579fa1d..5f38ed0c641a 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -3478,19 +3478,31 @@ EXPORT_SYMBOL_GPL(pci_rescan_bus);
* routines should always be executed under this mutex.
*/
DEFINE_MUTEX(pci_rescan_remove_lock);
+static struct task_struct *pci_rescan_remove_owner;
void pci_lock_rescan_remove(void)
{
mutex_lock(&pci_rescan_remove_lock);
+ pci_rescan_remove_owner = current;
}
EXPORT_SYMBOL_GPL(pci_lock_rescan_remove);
void pci_unlock_rescan_remove(void)
{
+ pci_rescan_remove_owner = NULL;
mutex_unlock(&pci_rescan_remove_lock);
}
EXPORT_SYMBOL_GPL(pci_unlock_rescan_remove);
+bool pci_lock_rescan_remove_nested(void)
+{
+ if (pci_rescan_remove_owner == current)
+ return false;
+ pci_lock_rescan_remove();
+ return true;
+}
+EXPORT_SYMBOL_GPL(pci_lock_rescan_remove_nested);
+
static int __init pci_sort_bf_cmp(const struct device *d_a,
const struct device *d_b)
{
--
2.53.0