Re: [PATCH] PCI: Fix AB-BA deadlock between remove and SR-IOV sysfs

From: Benjamin Block

Date: Mon Jul 06 2026 - 11:32:05 EST


On Mon, Jul 06, 2026 at 10:16:36AM +0200, Niklas Schnelle wrote:
> On Sun, 2026-07-05 at 23:24 +0800, Guangshuo Li wrote:
> > diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
> > index 5ec0b245a69b..5eae72508129 100644
> > --- a/drivers/pci/pci-sysfs.c
> > +++ b/drivers/pci/pci-sysfs.c
> > @@ -519,8 +519,35 @@ static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
> > if (kstrtoul(buf, 0, &val) < 0)
> > return -EINVAL;
> >
> > - if (val && device_remove_file_self(dev, attr))
> > + if (val && device_remove_file_self(dev, attr)) {
> > + /*
> > + * Mark the device as dead so that no new driver can bind
> > + * between the unbind and the removal below. Once the dead
> > + * flag is set, the device core will refuse any new driver
> > + * probe.
> > + */
> > + device_lock(dev);
> > + kill_device(dev);
> > + device_unlock(dev);
> > +
> > + /*
> > + * Unbind the driver before removing the device to avoid an
> > + * AB-BA deadlock between device_lock() and
> > + * pci_rescan_remove_lock. Without this, remove_store() takes
> > + * pci_rescan_remove_lock first via
> > + * pci_stop_and_remove_bus_device_locked(), then takes
> > + * device_lock() during driver release, while a concurrent
> > + * sriov_numvfs_store() takes device_lock() first and then
> > + * pci_rescan_remove_lock via SR-IOV VF removal.
> > + *
> > + * By unbinding first, the driver's .remove() callback,
> > + * including any SR-IOV VF cleanup, completes before
> > + * pci_rescan_remove_lock is acquired.
> > + */
> > + device_release_driver(dev);
> > pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
> > + }
> > +
> > return count;
> > }
> > static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL,
>
> This seems to be a complete duplicate of Ionut Nechita's patch[0] from
> April. The comments are close enough, but not identical, that I don't
> think this can really have been created independently.

Yep, +1.
This is not helpful. We still want the original patchset included (I sent a
ping a few minutes ago), but then in its complete form with the proper
authors.

--
Best Regards, Benjamin Block / Linux on IBM Z Kernel Development
IBM Deutschland Research & Development GmbH / https://www.ibm.com/privacy
Vors. Aufs.-R.: Wolfgang Wendt / Geschäftsführung: David Faller
Sitz der Ges.: Ehningen / Registergericht: AmtsG Stuttgart, HRB 243294