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

From: Manivannan Sadhasivam

Date: Mon Mar 02 2026 - 01:13:32 EST


On Sat, Feb 28, 2026 at 05:39:55PM +0100, Benjamin Block wrote:
> On Sat, Feb 28, 2026 at 08:43:33PM +0530, Manivannan Sadhasivam wrote:
> > On Sat, Feb 28, 2026 at 02:01:40PM +0200, Ionut Nechita (Wind River) wrote:
> > > 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_reentrant() 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 (reentrant 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.
> >
> > Just curious. Why can't you use mutex_trylock() here?
>
> One problem with mutex_trylock() is we don't know whether we ourself or
> someone else is holding the lock when it fails, we just know someone holds it;
> and we can't wait for someone else to release it when there is a chance we
> hold it ourself already. That was the problem with
> 05703271c3cd ("PCI/IOV: Add PCI rescan-remove locking when enabling/disabling SR-IOV")
> before it was reverted.
>

Okay, thanks for the info. I also failed to notice the mention of 'trylock' in
the cover letter.

But I think, instead of caching the owner task struct locally, you can make use
of mutex_get_owner() to extact the embedded owner task struct.

- Mani

--
மணிவண்ணன் சதாசிவம்