Re: [PATCH v7 03/12] PCI: liveupdate: Track incoming preserved PCI devices

From: David Matlack

Date: Wed Jul 22 2026 - 13:07:27 EST


On Tue, Jul 21, 2026 at 4:46 PM Pasha Tatashin
<pasha.tatashin@xxxxxxxxxx> wrote:
>
> On 07-21 16:18, David Matlack wrote:
> > On Tue, Jul 21, 2026 at 4:16 PM David Matlack <dmatlack@xxxxxxxxxx> wrote:
> > >
> > > On Tue, Jul 21, 2026 at 4:02 PM Samiullah Khawaja <skhawaja@xxxxxxxxxx> wrote:
> > > >
> > > > On Mon, Jul 20, 2026 at 02:54:51PM -0700, David Matlack wrote:
> > > > >On Fri, Jul 17, 2026 at 2:47 PM Pasha Tatashin
> > > > ><pasha.tatashin@xxxxxxxxxx> wrote:
> > > > >>
> > > > >> On Fri, 10 Jul 2026 21:26:06 +0000, David Matlack <dmatlack@xxxxxxxxxx> wrote:
> > > > >> > diff --git a/drivers/pci/liveupdate.c b/drivers/pci/liveupdate.c
> > > > >> > index 03075ce06ac9..df6a02240aa4 100644
> > > > >> > --- a/drivers/pci/liveupdate.c
> > > > >> > +++ b/drivers/pci/liveupdate.c
> > > > >> > @@ -298,6 +377,87 @@ void pci_liveupdate_unpreserve(struct pci_dev *dev)
> > > > >> > [ ... skip 76 lines ... ]
> > > > >> > + /*
> > > > >> > + * Hold the ref on the incoming FLB until pci_liveupdate_finish() so
> > > > >> > + * that dev->liveupdate.incoming cannot get freed while the PCI core
> > > > >> > + * has a pointer to it. It's better to leak the incoming FLB than do a
> > > > >> > + * use-after-free if driver does not call pci_liveupdate_finish().
> > > > >> > + */
> > > > >>
> > > > >> I am confused by this. Each preserved PCI device must have an associated
> > > > >> FD preserved with it via LUO. I.e., vfiofd would need to be preserved. If
> > > > >> the vfiofd was not reclaimed, and finish is not possible, that vfiofd
> > > > >> would still be owned by LUO, and therefore PCI FLB refcount would stay
> > > > >> positive.
> > > > >>
> > > > >> However, if finish is possible, and this is the last vfiofd that is
> > > > >> finished, FLB will be freed as soon as the reference count reaches zero,
> > > > >> which I would think is the expected behavior.
> > > > >>
> > > > >> What is the point of holding a reference here, instead of only for the
> > > > >> duration of FLB access, i.e. to make sure we are accessing a valid data?
> > > > >
> > > > >The duration of the access is from here until pci_liveupdate_finish()
> > > > >because that it when the pointer (dev->liveupdate.incoming) is
> > > > >cleared. So that is why the PCI core holds the reference from here
> > > > >until pci_liveupdate_finish().
> > > >
> > > > I think LUO gives you guarantee that this pointer remains valid until
> > > > the FLB finish(), because all the vfiofd that were preserved have not
> > > > finished. And when all vfiofd have finished, LUO lets you know that the
> > > > pointer is becoming invalid so you can unset these in the finish()
> > > > callback from LUO?
> > > >
> > > > If I understand this correctly, I think by keeping long references until
> > > > finish you are replicating the vfiofd bound lifecycle that LUO already
> > > > gives you.
> > >
> > > Yes that's right. I am taking the approach of not trusting drivers
> > > (i.e. VFIO) and having the PCI core maintain it's own references so
> > > that FLB finish() is guaranteed to never be called while a device has
> > > device->liveupdate.incoming set.
> > >
> > > Alternatively, the PCI core could avoid taking a reference and instead
> > > check for driver bugs in FLB finish(). I previously explored that
> > > approach and ran into a circular lock dependency between the FLB mutex
> > > and pci_liveupdate.rwsem. But I can look into that again, or just do a
> > > lockless walk of all PCI devices and panic if any still has
> > > dev->liveupdate.incoming set. It will only happen if the driver is
> > > completely broken so panicking seems fine.
> >
> > Pasha what do you think of this approach?
>
> I like it, let's do lockless sanity checking in flb finish callback, and
> avoid messing with references.

Great! I'll take this approach in v8.

> >
> > It avoids the PCI core taking a long reference (your conern) and also
> > avoids the PCI core needing to take lots of short references to access
> > dev->liveupdate.incoming (my concern). So seems like a good
> > compromise.