Re: [PATCH v5 1/1] PCI/IOV: Add reentrant locking in sriov_add_vfs/sriov_del_vfs for complete serialization

From: Lukas Wunner

Date: Tue Mar 03 2026 - 13:26:27 EST


On Tue, Mar 03, 2026 at 10:09:02AM +0200, Ionut Nechita (Wind River) wrote:
> +++ b/drivers/pci/iov.c
> @@ -629,19 +629,23 @@ static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs)
> {
> unsigned int i;
> int rc;
> + bool locked;
>
> if (dev->no_vf_scan)
> return 0;
>
> + locked = pci_lock_rescan_remove_reentrant();
> for (i = 0; i < num_vfs; i++) {
> rc = pci_iov_add_virtfn(dev, i);
> if (rc)
> goto failed;
> }
> + pci_unlock_rescan_remove_reentrant(locked);

I think a nicer API would be to have a counter which
is incremented by pci_lock_rescan_remove_reentrant() if owner is current,
is decremented by pci_unlock_rescan_remove_reentrant() if owner is current
and pci_unlock_rescan_remove_reentrant() unlocks only if it hits 0.

No need for an atomic counter as this happens under the lock.

Then you don't need this bool which leaks out of the API into the callers.

Also, I would put this in the existing pci_lock_rescan_remove(),
i.e. without introducing a new _reentrant variant, because these
deadlocks exist elsewhere. They're known to happen on unplug in
pciehp as well.

Thanks,

Lukas