Re: [PATCH v6 07/27] vfio/pci: Preserve vfio-pci device files across Live Update

From: Vipin Sharma

Date: Tue Nov 24 2026 - 19:22:22 EST


On Thu, Nov 19, 2026 at 04:06:38PM -0800, David Matlack wrote:
> On Tue Nov 17, 2026 at 10:44 PM PST, Vipin Sharma wrote:
> > How would a userspace opt-in? New kernel can change a capability visiblity
> > or virt and write permission of the bits inside a capability. Kernel
> > cannot know if userspace used the capability or not. Creating a VFIO
> > device in the new kernel by default results in a opt-in and currently
> > there is no way for userspace to opt-out unless we add a new
> > functionality where userspace can disable/enable features something
> > similar to KVM capabilities.
> >
> > For now, I think we don't need to worry about it. As this version will
> > be a base and if any serialized data with flags set received in this
> > kernel will lead to erroring out restore.
>
> We don't need an opt-in mechanism in this series. Detecting mismatches
> is enough to make Live Update safe in both directions: if the next
> kernel would emulate the device in an incompatible way, retrieve should
> fail.
>
> But an opt-in is needed to make rollback possible once vfio-pci starts
> changing its emulation. I imagine something like a VFIO_DEVICE_FEATURE
> that userspace can use to enable new emulation behavior right after
> opening the device, with the enabled bits recorded in a field appended
> to struct vfio_pci_core_device_ser behind a required flag. Devices that
> don't opt in keep the baseline emulation and stay compatible with older
> kernels. But that can come in a follow-on series.
>
> > In the next version, I will not preserve pci_config_map. Hopefully,
> > anyone changing VFIO code doesn't forget to update flags if they change
> > capability or permission bits (I will write comments at appropriate
> > places.)
> >
> > If we want to be very careful, we can do two things in this series:
> >
> > 1. Create a per bit valid mask for the vconfig and compare this between
> > new and old kernel during restore. In the new kernel, VFIO then takes
> > each bit from the preserved copy only where the mask says it was
> > valid, and keeps its own freshly initialized value elsewhere.
> > 2. Create a hash or store virt and writable bits in config space.
> > Compare it with the new kernel.
> >
> > Both of the above combined will safeguard against forgotten flag update
> > in the code.
> >
> > We don't have to do it but it safeguards against mistakes. Let me know
> > what you prefer.
>
> I think we should do both in this series, as two per-bit masks over
> config space, preserved in place of pci_config_map:
>
> - virt[]: the bits whose value vfio-pci keeps in vconfig and returns
> instead of the device's value. This says which bits of the preserved
> vconfig are valid.
>
> - write[]: the bits whose writes take effect. For bits in virt they
> update vconfig, and for other bits they are forwarded to the device
> (possibly after validation). Writes to other bits are ignored.
>
> The masks should describe what vfio-pci actually does rather than copy
> perm->virt and perm->write.
>
> vfio-pci can keep changing its implementation in ways that don't break
> Live Update, e.g. start intercepting writes to a register to sanity
> check them before forwarding them to the device.
>
> Something like:
>
> struct vfio_pci_core_device_ser {
> ...
> u8 virt[PCI_CFG_SPACE_EXP_SIZE];
> u8 write[PCI_CFG_SPACE_EXP_SIZE];
> u8 vconfig[PCI_CFG_SPACE_EXP_SIZE];
> } __packed;
>
>
> [ Note: I'd probably store virt[], write[], and vconfig[] on their
> own folios to avoid multi-order folio allocations, but I am embedding
> them here for simplicity. ]
>
> The outgoing kernel computes virt[] and write[] through a helper in
> vfio_pci_config.c and saves vconfig[] during freeze().
>
> The incoming kernel computes its own virt[] and write[] masks and
> requires both to match exactly with what's in vfio_pci_core_device_ser,
> otherwise it fails the retrieve. On take-over it uses the preserved
> value for bits in virt and its own freshly initialized value for the
> rest, like your option 1.

For now, we will be rejecting any changes and in future when we add
userspace opt-in mechanism then we can loosen this check, am I right?

>
> I'd store the full masks rather than a hash, so that a mismatch can be
> pinpointed.

Yes.