Skip to contentSkip to conversation

vfio/pci: Base Live Update support for VFIO device files

PATCH Superseded David Matlack 11 participants 120 messages Jan 29 – Apr 1

Subsystems (9)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

David Matlack

author maintainer
#0 lore raw

v2 cover vfio/pci: Base Live Update support for VFIO device files

This series can be found on GitHub:

https://github.com/dmatlack/linux/tree/liveupdate/vfio/cdev/v2

This series adds the base support to preserve a VFIO device file across a Live Update. "Base support" means that this allows userspace to safely preserve a VFIO device file with LIVEUPDATE_SESSION_PRESERVE_FD and retrieve it with LIVEUPDATE_SESSION_RETRIEVE_FD, but the device itself is not preserved in a fully running state across Live Update.

This series aims to provide a foundation on which to build the rest of the device preservation infrastructure, including:

- Preservation of iommufd files [1]
- Preservation of IOMMU driver state
- Preservation of PCI state (BAR resources, device state, bridge state, ...)
- Preservation of vfio-pci driver state

Testing
-------

The patches at the end of this series provide comprehensive selftests for the new code added by this series. The selftests have been validated in both a VM environment using a virtio-net PCIe device, and in a baremetal environment on an Intel EMR server with an Intel DSA PCIe device.

Here is an example of how to run the new selftests:

vfio_pci_liveupdate_uapi_test:

  $ tools/testing/selftests/vfio/scripts/setup.sh 0000:00:04.0
  $ tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test 0000:00:04.0
  $ tools/testing/selftests/vfio/scripts/cleanup.sh

vfio_pci_liveupdate_kexec_test:

  $ tools/testing/selftests/vfio/scripts/setup.sh 0000:00:04.0
  $ tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test --stage 1 0000:00:04.0
  $ kexec ...
  $ tools/testing/selftests/vfio/scripts/setup.sh 0000:00:04.0
  $ tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test --stage 2 0000:00:04.0
  $ tools/testing/selftests/vfio/scripts/cleanup.sh

It is also possible to run vfio_pci_liveupdate_kexec_test multiple times to preserve multiple devices simultaneously across a Live Update. This series has been tested with up to 8 devices concurrently preserved.

Changelog
---------

v2:
- Rebase on top of linux-next (tag: next-20260115)
- Add missing EXPORT_SYMBOL_GPLs in LUO (Zhu)
- Add Missing EXPORT_SYMBOL_GPLs for vfio_device_fops (Zhu)
- Fix circular dependency between vfio-pci-core and vfio-pci (Zhu)
- Handle pci=assign-busses (Lukas)
- Drop driver_override patch (Jason)
- Use kho_alloc_preserve(), kho_unpreserve_free(), kho_restore_free() (Pasha)
- Don't access PCI FLB after device initialization (Jason)
- Fix folio leak in vfio_pci_liveupdate_retrieve() (Alex)
- Add Documentation (Pasha)

v1: https://lore.kernel.org/kvm/[email protected]/

rfc: https://lore.kernel.org/kvm/[email protected]/

Cc Saeed Mahameed <[email protected]>
Cc Adithya Jayachandran <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Parav Pandit <[email protected]>
Cc Leon Romanovsky <[email protected]>
Cc William Tu <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Lukas Wunner <[email protected]>
Cc Pasha Tatashin <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc Pratyush Yadav <[email protected]>
Cc Samiullah Khawaja <[email protected]>
Cc Chris Li <[email protected]>
Cc Josh Hilke <[email protected]>
Cc David Rientjes <[email protected]>

[1] https://lore.kernel.org/linux-iommu/[email protected]/

David Matlack (13):
liveupdate: Export symbols needed by modules
PCI: Add API to track PCI devices preserved across Live Update
PCI: Inherit bus numbers from previous kernel during Live Update vfio/pci: Notify PCI subsystem about devices preserved across Live
Update
vfio: Enforce preserved devices are retrieved via
LIVEUPDATE_SESSION_RETRIEVE_FD
vfio/pci: Store incoming Live Update state in struct
vfio_pci_core_device
docs: liveupdate: Document VFIO device file preservation
vfio: selftests: Add Makefile support for TEST_GEN_PROGS_EXTENDED
vfio: selftests: Add vfio_pci_liveupdate_uapi_test
vfio: selftests: Expose iommu_modes to tests
vfio: selftests: Expose low-level helper routines for setting up
struct vfio_pci_device
vfio: selftests: Verify that opening VFIO device fails during Live
Update
vfio: selftests: Add continuous DMA to vfio_pci_liveupdate_kexec_test

Vipin Sharma (9):
vfio/pci: Register a file handler with Live Update Orchestrator vfio/pci: Preserve vfio-pci device files across Live Update vfio/pci: Retrieve preserved device files after Live Update vfio/pci: Skip reset of preserved device after Live Update selftests/liveupdate: Move luo_test_utils.* into a reusable library selftests/liveupdate: Add helpers to preserve/retrieve FDs
vfio: selftests: Build liveupdate library in VFIO selftests
vfio: selftests: Initialize vfio_pci_device using a VFIO cdev FD
vfio: selftests: Add vfio_pci_liveupdate_kexec_test

 Documentation/userspace-api/liveupdate.rst    | 144 ++++++++++
 MAINTAINERS                                   |   1 +
 drivers/pci/Makefile                          |   1 +
 drivers/pci/liveupdate.c                      | 212 +++++++++++++++
 drivers/pci/probe.c                           |  23 +-
 drivers/vfio/device_cdev.c                    |  25 +-
 drivers/vfio/group.c                          |   9 +
 drivers/vfio/pci/Makefile                     |   1 +
 drivers/vfio/pci/vfio_pci.c                   |  11 +-
 drivers/vfio/pci/vfio_pci_core.c              |  24 +-
 drivers/vfio/pci/vfio_pci_liveupdate.c        | 246 +++++++++++++++++
 drivers/vfio/pci/vfio_pci_priv.h              |  16 ++
 drivers/vfio/vfio.h                           |  13 -
 drivers/vfio/vfio_main.c                      |  23 +-
 include/linux/kho/abi/pci.h                   |  55 ++++
 include/linux/kho/abi/vfio_pci.h              |  45 +++
 include/linux/pci.h                           |  47 ++++
 include/linux/vfio.h                          |  58 ++++
 include/linux/vfio_pci_core.h                 |   2 +
 kernel/liveupdate/luo_core.c                  |   1 +
 kernel/liveupdate/luo_file.c                  |   2 +
 tools/testing/selftests/liveupdate/.gitignore |   1 +
 tools/testing/selftests/liveupdate/Makefile   |  14 +-
 .../include/libliveupdate.h}                  |  11 +-
 .../selftests/liveupdate/lib/libliveupdate.mk |  20 ++
 .../{luo_test_utils.c => lib/liveupdate.c}    |  43 ++-
 .../selftests/liveupdate/luo_kexec_simple.c   |   2 +-
 .../selftests/liveupdate/luo_multi_session.c  |   2 +-
 tools/testing/selftests/vfio/Makefile         |  23 +-
 .../vfio/lib/include/libvfio/iommu.h          |   2 +
 .../lib/include/libvfio/vfio_pci_device.h     |   8 +
 tools/testing/selftests/vfio/lib/iommu.c      |   4 +-
 .../selftests/vfio/lib/vfio_pci_device.c      |  60 ++--
 .../vfio/vfio_pci_liveupdate_kexec_test.c     | 256 ++++++++++++++++++
 .../vfio/vfio_pci_liveupdate_uapi_test.c      |  93 +++++++
 35 files changed, 1410 insertions(+), 88 deletions(-)
 create mode 100644 drivers/pci/liveupdate.c
 create mode 100644 drivers/vfio/pci/vfio_pci_liveupdate.c
 create mode 100644 include/linux/kho/abi/pci.h
 create mode 100644 include/linux/kho/abi/vfio_pci.h
 rename tools/testing/selftests/liveupdate/{luo_test_utils.h => lib/include/libliveupdate.h} (80%)
 create mode 100644 tools/testing/selftests/liveupdate/lib/libliveupdate.mk
 rename tools/testing/selftests/liveupdate/{luo_test_utils.c => lib/liveupdate.c} (89%)
 create mode 100644 tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c
 create mode 100644 tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c

base-commit: 9b7977f9e39b7768c70c2aa497f04e7569fd3e00

Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#1 lore raw

PATCH v2 1/22 liveupdate: Export symbols needed by modules

Export liveupdate_enabled(), liveupdate_register_file_handler(), and liveupdate_unregister_file_handler(). All of these will be used by vfio-pci in a subsequent commit, which can be built as a module.

---
 kernel/liveupdate/luo_core.c | 1 +
 kernel/liveupdate/luo_file.c | 2 ++
 2 files changed, 3 insertions(+)
kernel/liveupdate/luo_core.c +1 -0
index dda7bb57d421..59d7793d9444 100644
@@ -255,6 +255,7 @@ bool liveupdate_enabled(void)
 {
 	return luo_global.enabled;
 }
+EXPORT_SYMBOL_GPL(liveupdate_enabled);
 
 /**
  * DOC: LUO ioctl Interface
kernel/liveupdate/luo_file.c +2 -0
index 35d2a8b1a0df..32759e846bc9 100644
@@ -872,6 +872,7 @@ int liveupdate_register_file_handler(struct liveupdate_file_handler *fh)
 	luo_session_resume();
 	return err;
 }
+EXPORT_SYMBOL_GPL(liveupdate_register_file_handler);
 
 /**
  * liveupdate_unregister_file_handler - Unregister a liveupdate file handler
@@ -917,3 +918,4 @@ int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh)
 	liveupdate_test_register(fh);
 	return err;
 }
+EXPORT_SYMBOL_GPL(liveupdate_unregister_file_handler);
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
Reviewed-by on v2 1/22
#2 lore raw

Re: [PATCH v2 01/22] liveupdate: Export symbols needed by modules

Quoted 10 lines · David Matlack

Export liveupdate_enabled(), liveupdate_register_file_handler(), and liveupdate_unregister_file_handler(). All of these will be used by vfio-pci in a subsequent commit, which can be built as a module.

---
 kernel/liveupdate/luo_core.c | 1 +
 kernel/liveupdate/luo_file.c | 2 ++
 2 files changed, 3 insertions(+)
Quoted 32 lines
kernel/liveupdate/luo_core.c +1 -0
index dda7bb57d421..59d7793d9444 100644
@@ -255,6 +255,7 @@ bool liveupdate_enabled(void)
 {
 	return luo_global.enabled;
 }
+EXPORT_SYMBOL_GPL(liveupdate_enabled);
 
 /**
  * DOC: LUO ioctl Interface
kernel/liveupdate/luo_file.c +2 -0
index 35d2a8b1a0df..32759e846bc9 100644
@@ -872,6 +872,7 @@ int liveupdate_register_file_handler(struct liveupdate_file_handler *fh)
 	luo_session_resume();
 	return err;
 }
+EXPORT_SYMBOL_GPL(liveupdate_register_file_handler);
 
 /**
  * liveupdate_unregister_file_handler - Unregister a liveupdate file handler
@@ -917,3 +918,4 @@ int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh)
 	liveupdate_test_register(fh);
 	return err;
 }
+EXPORT_SYMBOL_GPL(liveupdate_unregister_file_handler);
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
Reviewed-by on v2 1/22
#3 lore raw

Re: [PATCH v2 01/22] liveupdate: Export symbols needed by modules

In reply to David Matlack
Quoted 42 lines · David Matlack

Export liveupdate_enabled(), liveupdate_register_file_handler(), and liveupdate_unregister_file_handler(). All of these will be used by vfio-pci in a subsequent commit, which can be built as a module.

---
kernel/liveupdate/luo_core.c | 1 +
kernel/liveupdate/luo_file.c | 2 ++

2 files changed, 3 insertions(+)
kernel/liveupdate/luo_core.c +1 -0
index dda7bb57d421..59d7793d9444 100644
@@ -255,6 +255,7 @@ bool liveupdate_enabled(void)
 {
 	return luo_global.enabled;
 }
+EXPORT_SYMBOL_GPL(liveupdate_enabled);
 
 /**
 * DOC: LUO ioctl Interface
kernel/liveupdate/luo_file.c +2 -0
index 35d2a8b1a0df..32759e846bc9 100644
@@ -872,6 +872,7 @@ int liveupdate_register_file_handler(struct liveupdate_file_handler *fh)
 	luo_session_resume();
 	return err;
 }
+EXPORT_SYMBOL_GPL(liveupdate_register_file_handler);
 
 /**
 * liveupdate_unregister_file_handler - Unregister a liveupdate file handler
@@ -917,3 +918,4 @@ int liveupdate_unregister_file_handler(struct liveupdate_file_handler *fh)
 	liveupdate_test_register(fh);
 	return err;
 }
+EXPORT_SYMBOL_GPL(liveupdate_unregister_file_handler);
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#4 lore raw

PATCH v2 2/22 PCI: Add API to track PCI devices preserved across Live Update

In reply to David Matlack

Add an API to enable the PCI subsystem to track all devices that are preserved across a Live Update, including both incoming devices (passed from the previous kernel) and outgoing devices (passed to the next kernel).

Use PCI segment number and BDF to keep track of devices across Live Update. This means the kernel must keep both identifiers constant across a Live Update for any preserved device. VFs are not supported for now, since that requires preserving SR-IOV state on the device to ensure the same number of VFs appear after kexec and with the same BDFs.

Drivers that preserve devices across Live Update can now register their struct liveupdate_file_handler with the PCI subsystem so that the PCI subsystem can allocate and manage File-Lifecycle-Bound (FLB) global data to track the list of incoming and outgoing preserved devices.

pci_liveupdate_register_fh(driver_fh)
pci_liveupdate_unregister_fh(driver_fh)

Drivers can notify the PCI subsystem whenever a device is preserved and unpreserved with the following APIs:

pci_liveupdate_outgoing_preserve(pci_dev)
pci_liveupdate_outgoing_unpreserve(pci_dev)

After a Live Update, the PCI subsystem fetches its FLB global data from the previous kernel from the Live Update Orchestrator (LUO) during device initialization to determine which devices were preserved.

Drivers can check if a device was preserved before userspace retrieves the file for it via pci_dev->liveupdate_incoming.

Once a driver has finished restoring an incoming preserved device, it can notify the PCI subsystem with the following call, which clears pci_dev->liveupdate_incoming.

pci_liveupdate_incoming_finish(pci_dev)

This API will be used in subsequent commits by the vfio-pci driver to preserve VFIO devices across Live Update and by the PCI subsystem.

---
 drivers/pci/Makefile        |   1 +
 drivers/pci/liveupdate.c    | 212 ++++++++++++++++++++++++++++++++++++
 drivers/pci/probe.c         |   2 +
 include/linux/kho/abi/pci.h |  55 ++++++++++
 include/linux/pci.h         |  47 ++++++++
 5 files changed, 317 insertions(+)
 create mode 100644 drivers/pci/liveupdate.c
 create mode 100644 include/linux/kho/abi/pci.h
drivers/pci/Makefile +1 -0
index 8c259a9a8796..a32f7658b9e5 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_PROC_FS)		+= proc.o
 obj-$(CONFIG_SYSFS)		+= pci-sysfs.o slot.o
 obj-$(CONFIG_ACPI)		+= pci-acpi.o
 obj-$(CONFIG_GENERIC_PCI_IOMAP) += iomap.o
+obj-$(CONFIG_LIVEUPDATE)	+= liveupdate.o
 endif
 
 obj-$(CONFIG_OF)		+= of.o
drivers/pci/liveupdate.c new file +212 -0

212 lines not on this page · raw message

drivers/pci/probe.c +2 -0
index 37329095e5fe..af6356c5a156 100644
@@ -2060,6 +2060,8 @@ int pci_setup_device(struct pci_dev *dev)
 	if (pci_early_dump)
 		early_dump_pci_device(dev);
 
+	pci_liveupdate_setup_device(dev);
+
 	/* Need to have dev->class ready */
 	dev->cfg_size = pci_cfg_space_size(dev);
 
include/linux/kho/abi/pci.h new file +55 -0

55 lines not on this page · raw message

include/linux/pci.h +47 -0

63 lines not on this page · raw message

Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
#5 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 11 lines · David Matlack

Add an API to enable the PCI subsystem to track all devices that are preserved across a Live Update, including both incoming devices (passed from the previous kernel) and outgoing devices (passed to the next kernel).

Use PCI segment number and BDF to keep track of devices across Live Update. This means the kernel must keep both identifiers constant across a Live Update for any preserved device. VFs are not supported for now, since that requires preserving SR-IOV state on the device to ensure the same number of VFs appear after kexec and with the same BDFs.

Quoted 7 lines

Drivers that preserve devices across Live Update can now register their struct liveupdate_file_handler with the PCI subsystem so that the PCI subsystem can allocate and manage File-Lifecycle-Bound (FLB) global data to track the list of incoming and outgoing preserved devices.

pci_liveupdate_register_fh(driver_fh)
pci_liveupdate_unregister_fh(driver_fh)

Can the above 2 functions support the virtual devices? For example, bonding, veth, iSWAP and RXE.

These virtual devices do not have BDF. As such, I am not sure if your patches take these virtual devices in to account.

Thanks a lot.

Zhu Yanjun

Quoted 409 lines

Drivers can notify the PCI subsystem whenever a device is preserved and unpreserved with the following APIs:

pci_liveupdate_outgoing_preserve(pci_dev)
pci_liveupdate_outgoing_unpreserve(pci_dev)

After a Live Update, the PCI subsystem fetches its FLB global data from the previous kernel from the Live Update Orchestrator (LUO) during device initialization to determine which devices were preserved.

Drivers can check if a device was preserved before userspace retrieves the file for it via pci_dev->liveupdate_incoming.

Once a driver has finished restoring an incoming preserved device, it can notify the PCI subsystem with the following call, which clears pci_dev->liveupdate_incoming.

pci_liveupdate_incoming_finish(pci_dev)

This API will be used in subsequent commits by the vfio-pci driver to preserve VFIO devices across Live Update and by the PCI subsystem.

---
  drivers/pci/Makefile        |   1 +
  drivers/pci/liveupdate.c    | 212 ++++++++++++++++++++++++++++++++++++
  drivers/pci/probe.c         |   2 +
  include/linux/kho/abi/pci.h |  55 ++++++++++
  include/linux/pci.h         |  47 ++++++++
  5 files changed, 317 insertions(+)

create mode 100644 drivers/pci/liveupdate.c
create mode 100644 include/linux/kho/abi/pci.h

drivers/pci/Makefile +1 -0
index 8c259a9a8796..a32f7658b9e5 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_PROC_FS)		+= proc.o
  obj-$(CONFIG_SYSFS)		+= pci-sysfs.o slot.o
  obj-$(CONFIG_ACPI)		+= pci-acpi.o
  obj-$(CONFIG_GENERIC_PCI_IOMAP) += iomap.o
+obj-$(CONFIG_LIVEUPDATE)	+= liveupdate.o
  endif
 
  obj-$(CONFIG_OF)		+= of.o
drivers/pci/liveupdate.c new file +212 -0
new file mode 100644
index 000000000000..182cfc793b80
@@ -0,0 +1,212 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * David Matlack <[email protected]>
+ */
+
+#include <linux/bsearch.h>
+#include <linux/io.h>
+#include <linux/kexec_handover.h>
+#include <linux/kho/abi/pci.h>
+#include <linux/liveupdate.h>
+#include <linux/mutex.h>
+#include <linux/mm.h>
+#include <linux/pci.h>
+#include <linux/sort.h>
+
+static DEFINE_MUTEX(pci_flb_outgoing_lock);
+
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_dev *dev = NULL;
+	int max_nr_devices = 0;
+	struct pci_ser *ser;
+	unsigned long size;
+
+	for_each_pci_dev(dev)
+		max_nr_devices++;
+
+	size = struct_size_t(struct pci_ser, devices, max_nr_devices);
+
+	ser = kho_alloc_preserve(size);
+	if (IS_ERR(ser))
+		return PTR_ERR(ser);
+
+	ser->max_nr_devices = max_nr_devices;
+
+	args->obj = ser;
+	args->data = virt_to_phys(ser);
+	return 0;
+}
+
+static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_ser *ser = args->obj;
+
+	WARN_ON_ONCE(ser->nr_devices);
+	kho_unpreserve_free(ser);
+}
+
+static int pci_flb_retrieve(struct liveupdate_flb_op_args *args)
+{
+	args->obj = phys_to_virt(args->data);
+	return 0;
+}
+
+static void pci_flb_finish(struct liveupdate_flb_op_args *args)
+{
+	kho_restore_free(args->obj);
+}
+
+static struct liveupdate_flb_ops pci_liveupdate_flb_ops = {
+	.preserve = pci_flb_preserve,
+	.unpreserve = pci_flb_unpreserve,
+	.retrieve = pci_flb_retrieve,
+	.finish = pci_flb_finish,
+	.owner = THIS_MODULE,
+};
+
+static struct liveupdate_flb pci_liveupdate_flb = {
+	.ops = &pci_liveupdate_flb_ops,
+	.compatible = PCI_LUO_FLB_COMPATIBLE,
+};
+
+#define INIT_PCI_DEV_SER(_dev) {		\
+	.domain = pci_domain_nr((_dev)->bus),	\
+	.bdf = pci_dev_id(_dev),		\
+}
+
+static int pci_dev_ser_cmp(const void *__a, const void *__b)
+{
+	const struct pci_dev_ser *a = __a, *b = __b;
+
+	return cmp_int(a->domain << 16 | a->bdf, b->domain << 16 | b->bdf);
+}
+
+static struct pci_dev_ser *pci_ser_find(struct pci_ser *ser,
+					struct pci_dev *dev)
+{
+	const struct pci_dev_ser key = INIT_PCI_DEV_SER(dev);
+
+	return bsearch(&key, ser->devices, ser->nr_devices,
+		       sizeof(key), pci_dev_ser_cmp);
+}
+
+static int pci_ser_delete(struct pci_ser *ser, struct pci_dev *dev)
+{
+	struct pci_dev_ser *dev_ser;
+	int i;
+
+	dev_ser = pci_ser_find(ser, dev);
+	if (!dev_ser)
+		return -ENOENT;
+
+	for (i = dev_ser - ser->devices; i < ser->nr_devices - 1; i++)
+		ser->devices[i] = ser->devices[i + 1];
+
+	ser->nr_devices--;
+	return 0;
+}
+
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+	struct pci_dev_ser new = INIT_PCI_DEV_SER(dev);
+	struct pci_ser *ser;
+	int i, ret;
+
+	/* Preserving VFs is not supported yet. */
+	if (dev->is_virtfn)
+		return -EINVAL;
+
+	guard(mutex)(&pci_flb_outgoing_lock);
+
+	if (dev->liveupdate_outgoing)
+		return -EBUSY;
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return ret;
+
+	if (ser->nr_devices == ser->max_nr_devices)
+		return -E2BIG;
+
+	for (i = ser->nr_devices; i > 0; i--) {
+		struct pci_dev_ser *prev = &ser->devices[i - 1];
+		int cmp = pci_dev_ser_cmp(&new, prev);
+
+		if (WARN_ON_ONCE(!cmp))
+			return -EBUSY;
+
+		if (cmp > 0)
+			break;
+
+		ser->devices[i] = *prev;
+	}
+
+	ser->devices[i] = new;
+	ser->nr_devices++;
+	dev->liveupdate_outgoing = true;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_preserve);
+
+void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	guard(mutex)(&pci_flb_outgoing_lock);
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+	if (WARN_ON_ONCE(ret))
+		return;
+
+	WARN_ON_ONCE(pci_ser_delete(ser, dev));
+	dev->liveupdate_outgoing = false;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_unpreserve);
+
+u32 pci_liveupdate_incoming_nr_devices(void)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return 0;
+
+	return ser->nr_devices;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_nr_devices);
+
+void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return;
+
+	dev->liveupdate_incoming = !!pci_ser_find(ser, dev);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_setup_device);
+
+void pci_liveupdate_incoming_finish(struct pci_dev *dev)
+{
+	dev->liveupdate_incoming = false;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_finish);
+
+int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh)
+{
+	return liveupdate_register_flb(fh, &pci_liveupdate_flb);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_register_fh);
+
+int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh)
+{
+	return liveupdate_unregister_flb(fh, &pci_liveupdate_flb);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_unregister_fh);
drivers/pci/probe.c +2 -0
index 37329095e5fe..af6356c5a156 100644
@@ -2060,6 +2060,8 @@ int pci_setup_device(struct pci_dev *dev)
  	if (pci_early_dump)
  		early_dump_pci_device(dev);
 
+	pci_liveupdate_setup_device(dev);
+
  	/* Need to have dev->class ready */
  	dev->cfg_size = pci_cfg_space_size(dev);
 
include/linux/kho/abi/pci.h new file +55 -0
new file mode 100644
index 000000000000..6577767f8da6
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * David Matlack <[email protected]>
+ */
+
+#ifndef _LINUX_KHO_ABI_PCI_H
+#define _LINUX_KHO_ABI_PCI_H
+
+#include <linux/compiler.h>
+#include <linux/types.h>
+
+/**
+ * DOC: PCI File-Lifecycle Bound (FLB) Live Update ABI
+ *
+ * This header defines the ABI for preserving core PCI state across kexec using
+ * Live Update File-Lifecycle Bound (FLB) data.
+ *
+ * This interface is a contract. Any modification to any of the serialization
+ * structs defined here constitutes a breaking change. Such changes require
+ * incrementing the version number in the PCI_LUO_FLB_COMPATIBLE string.
+ */
+
+#define PCI_LUO_FLB_COMPATIBLE "pci-v1"
+
+/**
+ * struct pci_dev_ser - Serialized state about a single PCI device.
+ *
+ * @domain: The device's PCI domain number (segment).
+ * @bdf: The device's PCI bus, device, and function number.
+ */
+struct pci_dev_ser {
+	u16 domain;
+	u16 bdf;
+} __packed;
+
+/**
+ * struct pci_ser - PCI Subsystem Live Update State
+ *
+ * This struct tracks state about all devices that are being preserved across
+ * a Live Update for the next kernel.
+ *
+ * @max_nr_devices: The length of the devices[] flexible array.
+ * @nr_devices: The number of devices that were preserved.
+ * @devices: Flexible array of pci_dev_ser structs for each device. Guaranteed
+ *           to be sorted ascending by domain and bdf.
+ */
+struct pci_ser {
+	u64 max_nr_devices;
+	u64 nr_devices;
+	struct pci_dev_ser devices[];
+} __packed;
+
+#endif /* _LINUX_KHO_ABI_PCI_H */
include/linux/pci.h +47 -0
index 7e36936bb37a..9ead6d84aef6 100644
@@ -40,6 +40,7 @@
  #include <linux/resource_ext.h>
  #include <linux/msi_api.h>
  #include <uapi/linux/pci.h>
+#include <linux/liveupdate.h>
 
  #include <linux/pci_ids.h>
 
@@ -582,6 +583,10 @@ struct pci_dev {
  	u8		tph_mode;	/* TPH mode */
  	u8		tph_req_type;	/* TPH requester type */
  #endif
+#ifdef CONFIG_LIVEUPDATE
+	unsigned int	liveupdate_incoming:1;	/* Preserved by previous kernel */
+	unsigned int	liveupdate_outgoing:1;	/* Preserved for next kernel */
+#endif
  };
 
  static inline struct pci_dev *pci_physfn(struct pci_dev *dev)
@@ -2854,4 +2859,46 @@ void pci_uevent_ers(struct pci_dev *pdev, enum  pci_ers_result err_type);
  	WARN_ONCE(condition, "%s %s: " fmt, \
  		  dev_driver_string(&(pdev)->dev), pci_name(pdev), ##arg)
 
+#ifdef CONFIG_LIVEUPDATE
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev);
+void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev);
+void pci_liveupdate_setup_device(struct pci_dev *dev);
+u32 pci_liveupdate_incoming_nr_devices(void);
+void pci_liveupdate_incoming_finish(struct pci_dev *dev);
+int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh);
+int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh);
+#else /* !CONFIG_LIVEUPDATE */
+static inline int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev)
+{
+}
+
+static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+}
+
+static inline u32 pci_liveupdate_incoming_nr_devices(void)
+{
+	return 0;
+}
+
+static inline void pci_liveupdate_incoming_finish(struct pci_dev *dev)
+{
+}
+
+static inline int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh)
+{
+	return -EOPNOTSUPP;
+}
+#endif /* !CONFIG_LIVEUPDATE */
+
  #endif /* LINUX_PCI_H */
Signature · 2 lines
Best Regards,
Yanjun.Zhu
author maintainer
#6 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 18 lines · Zhu Yanjun
Quoted 16 lines · David Matlack

Add an API to enable the PCI subsystem to track all devices that are preserved across a Live Update, including both incoming devices (passed from the previous kernel) and outgoing devices (passed to the next kernel).

Use PCI segment number and BDF to keep track of devices across Live Update. This means the kernel must keep both identifiers constant across a Live Update for any preserved device. VFs are not supported for now, since that requires preserving SR-IOV state on the device to ensure the same number of VFs appear after kexec and with the same BDFs.

Drivers that preserve devices across Live Update can now register their struct liveupdate_file_handler with the PCI subsystem so that the PCI subsystem can allocate and manage File-Lifecycle-Bound (FLB) global data to track the list of incoming and outgoing preserved devices.

Quoted 8 lines
Quoted 2 lines

pci_liveupdate_register_fh(driver_fh)
pci_liveupdate_unregister_fh(driver_fh)

Can the above 2 functions support the virtual devices? For example, bonding, veth, iSWAP and RXE.

These virtual devices do not have BDF. As such, I am not sure if your patches take these virtual devices in to account.

No this patch series only supports PCI devices, since those are the only devices so far we've needed to support.

I am not familiar with any of the devices that you mentioned. If they are virtual then does that mean it's all just software? In that case I would be curious to know what problem is solved by preserving them in the kernel, vs. tearing them down and rebuilding them across a Live Udpate.

#7 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 25 lines · David Matlack
Quoted 17 lines · Zhu Yanjun
Quoted 16 lines · David Matlack

Add an API to enable the PCI subsystem to track all devices that are preserved across a Live Update, including both incoming devices (passed from the previous kernel) and outgoing devices (passed to the next kernel).

Use PCI segment number and BDF to keep track of devices across Live Update. This means the kernel must keep both identifiers constant across a Live Update for any preserved device. VFs are not supported for now, since that requires preserving SR-IOV state on the device to ensure the same number of VFs appear after kexec and with the same BDFs.

Drivers that preserve devices across Live Update can now register their struct liveupdate_file_handler with the PCI subsystem so that the PCI subsystem can allocate and manage File-Lifecycle-Bound (FLB) global data to track the list of incoming and outgoing preserved devices.

Quoted 7 lines
Quoted 2 lines

pci_liveupdate_register_fh(driver_fh)
pci_liveupdate_unregister_fh(driver_fh)

Can the above 2 functions support the virtual devices? For example, bonding, veth, iSWAP and RXE.

These virtual devices do not have BDF. As such, I am not sure if your patches take these virtual devices in to account.

Quoted 8 lines

No this patch series only supports PCI devices, since those are the only devices so far we've needed to support.

I am not familiar with any of the devices that you mentioned. If they are virtual then does that mean it's all just software? In that case I would be curious to know what problem is solved by preserving them in the kernel, vs. tearing them down and rebuilding them across a Live Udpate.

Bonding, veth, rxe, and siw can be used in KVM environments.

Although these are software-only virtual devices with no associated hardware,

they may maintain state that is observable by userspace.

As a result, Live Update should preserve their state across the update.

Zhu Yanjun

author maintainer
#8 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 36 lines · Yanjun.Zhu
Quoted 25 lines · David Matlack
Quoted 17 lines · Zhu Yanjun
Quoted 16 lines · David Matlack

Add an API to enable the PCI subsystem to track all devices that are preserved across a Live Update, including both incoming devices (passed from the previous kernel) and outgoing devices (passed to the next kernel).

Use PCI segment number and BDF to keep track of devices across Live Update. This means the kernel must keep both identifiers constant across a Live Update for any preserved device. VFs are not supported for now, since that requires preserving SR-IOV state on the device to ensure the same number of VFs appear after kexec and with the same BDFs.

Drivers that preserve devices across Live Update can now register their struct liveupdate_file_handler with the PCI subsystem so that the PCI subsystem can allocate and manage File-Lifecycle-Bound (FLB) global data to track the list of incoming and outgoing preserved devices.

Quoted 7 lines
Quoted 2 lines

pci_liveupdate_register_fh(driver_fh)
pci_liveupdate_unregister_fh(driver_fh)

Can the above 2 functions support the virtual devices? For example, bonding, veth, iSWAP and RXE.

These virtual devices do not have BDF. As such, I am not sure if your patches take these virtual devices in to account.

Quoted 8 lines

No this patch series only supports PCI devices, since those are the only devices so far we've needed to support.

I am not familiar with any of the devices that you mentioned. If they are virtual then does that mean it's all just software? In that case I would be curious to know what problem is solved by preserving them in the kernel, vs. tearing them down and rebuilding them across a Live Udpate.

Quoted 8 lines

Bonding, veth, rxe, and siw can be used in KVM environments.

Although these are software-only virtual devices with no associated hardware,

they may maintain state that is observable by userspace.

As a result, Live Update should preserve their state across the update.

Sorry for taking so long to get back to you.

Userspace should serialize the state of those devices out of the kernel, persist it in memory or on disk across Live Update, and then recreate it after Live Update.

This is why, for example, KVM does not need to direclty participate in Live Update. The VMM serializes all KVM state from the kernel, saves it, and then constructs a completely new KVM VM after the Live Update using that state. KVM will only need to participate in Live Update once there is hardware requirement. For example, I believe some of the Confidential Computing hardware virtualization extensions require certain memory structures used by hardware remain in place and cannot be reallocated after Live Update. KVM would have to participate to preserve those and reassociate them with the right VM after Live Update.

#9 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

In reply to David Matlack
Quoted 16 lines · David Matlack

Add an API to enable the PCI subsystem to track all devices that are preserved across a Live Update, including both incoming devices (passed from the previous kernel) and outgoing devices (passed to the next kernel).

Use PCI segment number and BDF to keep track of devices across Live Update. This means the kernel must keep both identifiers constant across a Live Update for any preserved device. VFs are not supported for now, since that requires preserving SR-IOV state on the device to ensure the same number of VFs appear after kexec and with the same BDFs.

Drivers that preserve devices across Live Update can now register their struct liveupdate_file_handler with the PCI subsystem so that the PCI subsystem can allocate and manage File-Lifecycle-Bound (FLB) global data to track the list of incoming and outgoing preserved devices.

Quoted 8 lines

pci_liveupdate_register_fh(driver_fh)
pci_liveupdate_unregister_fh(driver_fh)

Drivers can notify the PCI subsystem whenever a device is preserved and unpreserved with the following APIs:

pci_liveupdate_outgoing_preserve(pci_dev)
pci_liveupdate_outgoing_unpreserve(pci_dev)

nit: Preserve and Unpreserve can only be done from outgoing kernel, maybe remove the "outgoing" from the function name.

Quoted 64 lines

After a Live Update, the PCI subsystem fetches its FLB global data from the previous kernel from the Live Update Orchestrator (LUO) during device initialization to determine which devices were preserved.

Drivers can check if a device was preserved before userspace retrieves the file for it via pci_dev->liveupdate_incoming.

Once a driver has finished restoring an incoming preserved device, it can notify the PCI subsystem with the following call, which clears pci_dev->liveupdate_incoming.

pci_liveupdate_incoming_finish(pci_dev)

This API will be used in subsequent commits by the vfio-pci driver to preserve VFIO devices across Live Update and by the PCI subsystem.

---

drivers/pci/Makefile        |   1 +
drivers/pci/liveupdate.c    | 212 ++++++++++++++++++++++++++++++++++++
drivers/pci/probe.c         |   2 +
include/linux/kho/abi/pci.h |  55 ++++++++++
include/linux/pci.h         |  47 ++++++++
5 files changed, 317 insertions(+)

create mode 100644 drivers/pci/liveupdate.c
create mode 100644 include/linux/kho/abi/pci.h

drivers/pci/Makefile +0 -0
index 8c259a9a8796..a32f7658b9e5 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_PROC_FS)		+= proc.o

obj-$(CONFIG_SYSFS) += pci-sysfs.o slot.o
obj-$(CONFIG_ACPI) += pci-acpi.o
obj-$(CONFIG_GENERIC_PCI_IOMAP) += iomap.o
+obj-$(CONFIG_LIVEUPDATE) += liveupdate.o
endif

obj-$(CONFIG_OF) += of.o

drivers/pci/liveupdate.c new file +18 -0
new file mode 100644
index 000000000000..182cfc793b80
@@ -0,0 +1,212 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * David Matlack <[email protected]>
+ */
+
+#include <linux/bsearch.h>
+#include <linux/io.h>
+#include <linux/kexec_handover.h>
+#include <linux/kho/abi/pci.h>
+#include <linux/liveupdate.h>
+#include <linux/mutex.h>
+#include <linux/mm.h>
+#include <linux/pci.h>
+#include <linux/sort.h>
+
+static DEFINE_MUTEX(pci_flb_outgoing_lock);

drivers/pci/liveupdate.c line 28

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_dev *dev = NULL;
+	int max_nr_devices = 0;
+	struct pci_ser *ser;
+	unsigned long size;
+
+	for_each_pci_dev(dev)
+		max_nr_devices++;

This will not work for SRIOV as new devices will be registered when sriov is enabled and the max will increase. As we are not handling VFs with this patch series, this can be fine?

Quoted 331 lines
drivers/pci/liveupdate.c new file +184 -0
+
+	size = struct_size_t(struct pci_ser, devices, max_nr_devices);
+
+	ser = kho_alloc_preserve(size);
+	if (IS_ERR(ser))
+		return PTR_ERR(ser);
+
+	ser->max_nr_devices = max_nr_devices;
+
+	args->obj = ser;
+	args->data = virt_to_phys(ser);
+	return 0;
+}
+
+static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_ser *ser = args->obj;
+
+	WARN_ON_ONCE(ser->nr_devices);
+	kho_unpreserve_free(ser);
+}
+
+static int pci_flb_retrieve(struct liveupdate_flb_op_args *args)
+{
+	args->obj = phys_to_virt(args->data);
+	return 0;
+}
+
+static void pci_flb_finish(struct liveupdate_flb_op_args *args)
+{
+	kho_restore_free(args->obj);
+}
+
+static struct liveupdate_flb_ops pci_liveupdate_flb_ops = {
+	.preserve = pci_flb_preserve,
+	.unpreserve = pci_flb_unpreserve,
+	.retrieve = pci_flb_retrieve,
+	.finish = pci_flb_finish,
+	.owner = THIS_MODULE,
+};
+
+static struct liveupdate_flb pci_liveupdate_flb = {
+	.ops = &pci_liveupdate_flb_ops,
+	.compatible = PCI_LUO_FLB_COMPATIBLE,
+};
+
+#define INIT_PCI_DEV_SER(_dev) {		\
+	.domain = pci_domain_nr((_dev)->bus),	\
+	.bdf = pci_dev_id(_dev),		\
+}
+
+static int pci_dev_ser_cmp(const void *__a, const void *__b)
+{
+	const struct pci_dev_ser *a = __a, *b = __b;
+
+	return cmp_int(a->domain << 16 | a->bdf, b->domain << 16 | b->bdf);
+}
+
+static struct pci_dev_ser *pci_ser_find(struct pci_ser *ser,
+					struct pci_dev *dev)
+{
+	const struct pci_dev_ser key = INIT_PCI_DEV_SER(dev);
+
+	return bsearch(&key, ser->devices, ser->nr_devices,
+		       sizeof(key), pci_dev_ser_cmp);
+}
+
+static int pci_ser_delete(struct pci_ser *ser, struct pci_dev *dev)
+{
+	struct pci_dev_ser *dev_ser;
+	int i;
+
+	dev_ser = pci_ser_find(ser, dev);
+	if (!dev_ser)
+		return -ENOENT;
+
+	for (i = dev_ser - ser->devices; i < ser->nr_devices - 1; i++)
+		ser->devices[i] = ser->devices[i + 1];
+
+	ser->nr_devices--;
+	return 0;
+}
+
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+	struct pci_dev_ser new = INIT_PCI_DEV_SER(dev);
+	struct pci_ser *ser;
+	int i, ret;
+
+	/* Preserving VFs is not supported yet. */
+	if (dev->is_virtfn)
+		return -EINVAL;
+
+	guard(mutex)(&pci_flb_outgoing_lock);
+
+	if (dev->liveupdate_outgoing)
+		return -EBUSY;
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return ret;
+
+	if (ser->nr_devices == ser->max_nr_devices)
+		return -E2BIG;
+
+	for (i = ser->nr_devices; i > 0; i--) {
+		struct pci_dev_ser *prev = &ser->devices[i - 1];
+		int cmp = pci_dev_ser_cmp(&new, prev);
+
+		if (WARN_ON_ONCE(!cmp))
+			return -EBUSY;
+
+		if (cmp > 0)
+			break;
+
+		ser->devices[i] = *prev;
+	}
+
+	ser->devices[i] = new;
+	ser->nr_devices++;
+	dev->liveupdate_outgoing = true;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_preserve);
+
+void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	guard(mutex)(&pci_flb_outgoing_lock);
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+	if (WARN_ON_ONCE(ret))
+		return;
+
+	WARN_ON_ONCE(pci_ser_delete(ser, dev));
+	dev->liveupdate_outgoing = false;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_unpreserve);
+
+u32 pci_liveupdate_incoming_nr_devices(void)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return 0;
+
+	return ser->nr_devices;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_nr_devices);
+
+void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return;
+
+	dev->liveupdate_incoming = !!pci_ser_find(ser, dev);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_setup_device);
+
+void pci_liveupdate_incoming_finish(struct pci_dev *dev)
+{
+	dev->liveupdate_incoming = false;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_finish);
+
+int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh)
+{
+	return liveupdate_register_flb(fh, &pci_liveupdate_flb);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_register_fh);
+
+int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh)
+{
+	return liveupdate_unregister_flb(fh, &pci_liveupdate_flb);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_unregister_fh);
drivers/pci/probe.c +2 -0
index 37329095e5fe..af6356c5a156 100644
@@ -2060,6 +2060,8 @@ int pci_setup_device(struct pci_dev *dev)
 	if (pci_early_dump)
 		early_dump_pci_device(dev);
 
+	pci_liveupdate_setup_device(dev);
+
 	/* Need to have dev->class ready */
 	dev->cfg_size = pci_cfg_space_size(dev);
 
include/linux/kho/abi/pci.h new file +55 -0
new file mode 100644
index 000000000000..6577767f8da6
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * David Matlack <[email protected]>
+ */
+
+#ifndef _LINUX_KHO_ABI_PCI_H
+#define _LINUX_KHO_ABI_PCI_H
+
+#include <linux/compiler.h>
+#include <linux/types.h>
+
+/**
+ * DOC: PCI File-Lifecycle Bound (FLB) Live Update ABI
+ *
+ * This header defines the ABI for preserving core PCI state across kexec using
+ * Live Update File-Lifecycle Bound (FLB) data.
+ *
+ * This interface is a contract. Any modification to any of the serialization
+ * structs defined here constitutes a breaking change. Such changes require
+ * incrementing the version number in the PCI_LUO_FLB_COMPATIBLE string.
+ */
+
+#define PCI_LUO_FLB_COMPATIBLE "pci-v1"
+
+/**
+ * struct pci_dev_ser - Serialized state about a single PCI device.
+ *
+ * @domain: The device's PCI domain number (segment).
+ * @bdf: The device's PCI bus, device, and function number.
+ */
+struct pci_dev_ser {
+	u16 domain;
+	u16 bdf;
+} __packed;
+
+/**
+ * struct pci_ser - PCI Subsystem Live Update State
+ *
+ * This struct tracks state about all devices that are being preserved across
+ * a Live Update for the next kernel.
+ *
+ * @max_nr_devices: The length of the devices[] flexible array.
+ * @nr_devices: The number of devices that were preserved.
+ * @devices: Flexible array of pci_dev_ser structs for each device. Guaranteed
+ *           to be sorted ascending by domain and bdf.
+ */
+struct pci_ser {
+	u64 max_nr_devices;
+	u64 nr_devices;
+	struct pci_dev_ser devices[];
+} __packed;
+
+#endif /* _LINUX_KHO_ABI_PCI_H */
include/linux/pci.h +47 -0
index 7e36936bb37a..9ead6d84aef6 100644
@@ -40,6 +40,7 @@
 #include <linux/resource_ext.h>
 #include <linux/msi_api.h>
 #include <uapi/linux/pci.h>
+#include <linux/liveupdate.h>
 
 #include <linux/pci_ids.h>
 
@@ -582,6 +583,10 @@ struct pci_dev {
 	u8		tph_mode;	/* TPH mode */
 	u8		tph_req_type;	/* TPH requester type */
 #endif
+#ifdef CONFIG_LIVEUPDATE
+	unsigned int	liveupdate_incoming:1;	/* Preserved by previous kernel */
+	unsigned int	liveupdate_outgoing:1;	/* Preserved for next kernel */
+#endif
 };
 
 static inline struct pci_dev *pci_physfn(struct pci_dev *dev)
@@ -2854,4 +2859,46 @@ void pci_uevent_ers(struct pci_dev *pdev, enum  pci_ers_result err_type);
 	WARN_ONCE(condition, "%s %s: " fmt, \
 		  dev_driver_string(&(pdev)->dev), pci_name(pdev), ##arg)
 
+#ifdef CONFIG_LIVEUPDATE
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev);
+void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev);
+void pci_liveupdate_setup_device(struct pci_dev *dev);
+u32 pci_liveupdate_incoming_nr_devices(void);
+void pci_liveupdate_incoming_finish(struct pci_dev *dev);
+int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh);
+int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh);
+#else /* !CONFIG_LIVEUPDATE */
+static inline int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev)
+{
+}
+
+static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+}
+
+static inline u32 pci_liveupdate_incoming_nr_devices(void)
+{
+	return 0;
+}
+
+static inline void pci_liveupdate_incoming_finish(struct pci_dev *dev)
+{
+}
+
+static inline int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh)
+{
+	return -EOPNOTSUPP;
+}
+#endif /* !CONFIG_LIVEUPDATE */
+
 #endif /* LINUX_PCI_H */
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#10 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 10 lines · Samiullah Khawaja
Quoted 5 lines · David Matlack

Drivers can notify the PCI subsystem whenever a device is preserved and unpreserved with the following APIs:

pci_liveupdate_outgoing_preserve(pci_dev)
pci_liveupdate_outgoing_unpreserve(pci_dev)

nit: Preserve and Unpreserve can only be done from outgoing kernel, maybe remove the "outgoing" from the function name.

That's reasonable, I can make that change in v3.

Quoted 5 lines
Quoted 5 lines
drivers/pci/liveupdate.c new file +5 -0
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+      struct pci_dev *dev = NULL;
+      int max_nr_devices = 0;
+      struct pci_ser *ser;
Quoted 8 lines

drivers/pci/liveupdate.c line 28

Quoted 4 lines
drivers/pci/liveupdate.c new file +4 -0
+      unsigned long size;
+
+      for_each_pci_dev(dev)
+              max_nr_devices++;

This will not work for SRIOV as new devices will be registered when sriov is enabled and the max will increase. As we are not handling VFs with this patch series, this can be fine?

That's right. Since this series does not support VFs (pci_liveupdate_outgoing_preserve(VF) will return -EINVAL), I dropped the logic from v1 that included the maximum possible number of VFs in max_nr_devices.

Once we add support for preserving VFs, we can update this logic.

Another way that pci_liveupdate_outgoing_preserve() could fail due to running out of room is if hot-plugged devices are preserved. But I don't think that's an important problem to solve right now.

#11 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 6 lines · David Matlack
Quoted 5 lines · Samiullah Khawaja
Quoted 3 lines · David Matlack

Drivers can notify the PCI subsystem whenever a device is preserved and unpreserved with the following APIs:

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

pci_liveupdate_outgoing_preserve(pci_dev)
pci_liveupdate_outgoing_unpreserve(pci_dev)

nit: Preserve and Unpreserve can only be done from outgoing kernel, maybe remove the "outgoing" from the function name.

That's reasonable, I can make that change in v3.

I should have added it earlier, but the same applies to the pci_liveupdate_incoming_finish() as it can only be done with incoming kernel. Maybe we can remove incoming from it also for consistency?

Quoted 25 lines
Quoted 14 lines

drivers/pci/liveupdate.c line 28

Quoted 10 lines
drivers/pci/liveupdate.c new file +9 -0
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+      struct pci_dev *dev = NULL;
+      int max_nr_devices = 0;
+      struct pci_ser *ser;
+      unsigned long size;
+
+      for_each_pci_dev(dev)
+              max_nr_devices++;

This will not work for SRIOV as new devices will be registered when sriov is enabled and the max will increase. As we are not handling VFs with this patch series, this can be fine?

That's right. Since this series does not support VFs (pci_liveupdate_outgoing_preserve(VF) will return -EINVAL), I dropped the logic from v1 that included the maximum possible number of VFs in max_nr_devices.

Once we add support for preserving VFs, we can update this logic.

Another way that pci_liveupdate_outgoing_preserve() could fail due to running out of room is if hot-plugged devices are preserved. But I don't think that's an important problem to solve right now.

author maintainer
#12 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 11 lines · Samiullah Khawaja
Quoted 9 lines · David Matlack
Quoted 8 lines · Samiullah Khawaja
Quoted 5 lines · David Matlack

Drivers can notify the PCI subsystem whenever a device is preserved and unpreserved with the following APIs:

pci_liveupdate_outgoing_preserve(pci_dev)
pci_liveupdate_outgoing_unpreserve(pci_dev)

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

nit: Preserve and Unpreserve can only be done from outgoing kernel, maybe remove the "outgoing" from the function name.

That's reasonable, I can make that change in v3.

I should have added it earlier, but the same applies to the pci_liveupdate_incoming_finish() as it can only be done with incoming kernel. Maybe we can remove incoming from it also for consistency?

Yeah I can make that change as well.

I was erring on the side of being overly explicity with the incoming/outgoing terminology, but I agree it's unnecessary in these APIs.

#13 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

In reply to David Matlack
Quoted 70 lines · David Matlack

Add an API to enable the PCI subsystem to track all devices that are preserved across a Live Update, including both incoming devices (passed from the previous kernel) and outgoing devices (passed to the next kernel).

Use PCI segment number and BDF to keep track of devices across Live Update. This means the kernel must keep both identifiers constant across a Live Update for any preserved device. VFs are not supported for now, since that requires preserving SR-IOV state on the device to ensure the same number of VFs appear after kexec and with the same BDFs.

Drivers that preserve devices across Live Update can now register their struct liveupdate_file_handler with the PCI subsystem so that the PCI subsystem can allocate and manage File-Lifecycle-Bound (FLB) global data to track the list of incoming and outgoing preserved devices.

pci_liveupdate_register_fh(driver_fh)
pci_liveupdate_unregister_fh(driver_fh)

Drivers can notify the PCI subsystem whenever a device is preserved and unpreserved with the following APIs:

pci_liveupdate_outgoing_preserve(pci_dev)
pci_liveupdate_outgoing_unpreserve(pci_dev)

After a Live Update, the PCI subsystem fetches its FLB global data from the previous kernel from the Live Update Orchestrator (LUO) during device initialization to determine which devices were preserved.

Drivers can check if a device was preserved before userspace retrieves the file for it via pci_dev->liveupdate_incoming.

Once a driver has finished restoring an incoming preserved device, it can notify the PCI subsystem with the following call, which clears pci_dev->liveupdate_incoming.

pci_liveupdate_incoming_finish(pci_dev)

This API will be used in subsequent commits by the vfio-pci driver to preserve VFIO devices across Live Update and by the PCI subsystem.

---
 drivers/pci/Makefile        |   1 +
 drivers/pci/liveupdate.c    | 212 ++++++++++++++++++++++++++++++++++++
 drivers/pci/probe.c         |   2 +
 include/linux/kho/abi/pci.h |  55 ++++++++++
 include/linux/pci.h         |  47 ++++++++
 5 files changed, 317 insertions(+)
 create mode 100644 drivers/pci/liveupdate.c
 create mode 100644 include/linux/kho/abi/pci.h
drivers/pci/Makefile +1 -0
index 8c259a9a8796..a32f7658b9e5 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_PROC_FS)		+= proc.o
 obj-$(CONFIG_SYSFS)		+= pci-sysfs.o slot.o
 obj-$(CONFIG_ACPI)		+= pci-acpi.o
 obj-$(CONFIG_GENERIC_PCI_IOMAP) += iomap.o
+obj-$(CONFIG_LIVEUPDATE)	+= liveupdate.o
 endif
 
 obj-$(CONFIG_OF)		+= of.o
drivers/pci/liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..182cfc793b80

drivers/pci/liveupdate.c line 4

Quoted 4 lines
drivers/pci/liveupdate.c new file +4 -0
@@ -0,0 +1,212 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2025, Google LLC.

Nit: Should these be 2026 now?

Quoted 118 lines
drivers/pci/liveupdate.c new file +118 -0
+ * David Matlack <[email protected]>
+ */
+
+#include <linux/bsearch.h>
+#include <linux/io.h>
+#include <linux/kexec_handover.h>
+#include <linux/kho/abi/pci.h>
+#include <linux/liveupdate.h>
+#include <linux/mutex.h>
+#include <linux/mm.h>
+#include <linux/pci.h>
+#include <linux/sort.h>
+
+static DEFINE_MUTEX(pci_flb_outgoing_lock);
+
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_dev *dev = NULL;
+	int max_nr_devices = 0;
+	struct pci_ser *ser;
+	unsigned long size;
+
+	for_each_pci_dev(dev)
+		max_nr_devices++;
+
+	size = struct_size_t(struct pci_ser, devices, max_nr_devices);
+
+	ser = kho_alloc_preserve(size);
+	if (IS_ERR(ser))
+		return PTR_ERR(ser);
+
+	ser->max_nr_devices = max_nr_devices;
+
+	args->obj = ser;
+	args->data = virt_to_phys(ser);
+	return 0;
+}
+
+static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_ser *ser = args->obj;
+
+	WARN_ON_ONCE(ser->nr_devices);
+	kho_unpreserve_free(ser);
+}
+
+static int pci_flb_retrieve(struct liveupdate_flb_op_args *args)
+{
+	args->obj = phys_to_virt(args->data);
+	return 0;
+}
+
+static void pci_flb_finish(struct liveupdate_flb_op_args *args)
+{
+	kho_restore_free(args->obj);
+}
+
+static struct liveupdate_flb_ops pci_liveupdate_flb_ops = {
+	.preserve = pci_flb_preserve,
+	.unpreserve = pci_flb_unpreserve,
+	.retrieve = pci_flb_retrieve,
+	.finish = pci_flb_finish,
+	.owner = THIS_MODULE,
+};
+
+static struct liveupdate_flb pci_liveupdate_flb = {
+	.ops = &pci_liveupdate_flb_ops,
+	.compatible = PCI_LUO_FLB_COMPATIBLE,
+};
+
+#define INIT_PCI_DEV_SER(_dev) {		\
+	.domain = pci_domain_nr((_dev)->bus),	\
+	.bdf = pci_dev_id(_dev),		\
+}
+
+static int pci_dev_ser_cmp(const void *__a, const void *__b)
+{
+	const struct pci_dev_ser *a = __a, *b = __b;
+
+	return cmp_int(a->domain << 16 | a->bdf, b->domain << 16 | b->bdf);
+}
+
+static struct pci_dev_ser *pci_ser_find(struct pci_ser *ser,
+					struct pci_dev *dev)
+{
+	const struct pci_dev_ser key = INIT_PCI_DEV_SER(dev);
+
+	return bsearch(&key, ser->devices, ser->nr_devices,
+		       sizeof(key), pci_dev_ser_cmp);
+}
+
+static int pci_ser_delete(struct pci_ser *ser, struct pci_dev *dev)
+{
+	struct pci_dev_ser *dev_ser;
+	int i;
+
+	dev_ser = pci_ser_find(ser, dev);
+	if (!dev_ser)
+		return -ENOENT;
+
+	for (i = dev_ser - ser->devices; i < ser->nr_devices - 1; i++)
+		ser->devices[i] = ser->devices[i + 1];
+
+	ser->nr_devices--;
+	return 0;
+}
+
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+	struct pci_dev_ser new = INIT_PCI_DEV_SER(dev);
+	struct pci_ser *ser;
+	int i, ret;
+
+	/* Preserving VFs is not supported yet. */
+	if (dev->is_virtfn)
+		return -EINVAL;
+
+	guard(mutex)(&pci_flb_outgoing_lock);

drivers/pci/liveupdate.c line 132

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+
+	if (dev->liveupdate_outgoing)
+		return -EBUSY;
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return ret;
+
+	if (ser->nr_devices == ser->max_nr_devices)
+		return -E2BIG;

I'm wondering how (or if) this handles hot-plugged devices? max_nr_devices is calculated based on for_each_pci_dev at the time of the first preservation.. what happens if a device is hotplugged after the first device is preserved but before the second one is, does max_nr_devices become stale? Since ser->max_nr_devices will not reflect the actual possible device count, potentially leading to an unnecessary -E2BIG failure?

Quoted 35 lines
drivers/pci/liveupdate.c new file +35 -0
+
+	for (i = ser->nr_devices; i > 0; i--) {
+		struct pci_dev_ser *prev = &ser->devices[i - 1];
+		int cmp = pci_dev_ser_cmp(&new, prev);
+
+		if (WARN_ON_ONCE(!cmp))
+			return -EBUSY;
+
+		if (cmp > 0)
+			break;
+
+		ser->devices[i] = *prev;
+	}
+
+	ser->devices[i] = new;
+	ser->nr_devices++;
+	dev->liveupdate_outgoing = true;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_preserve);
+
+void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	guard(mutex)(&pci_flb_outgoing_lock);
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+	if (WARN_ON_ONCE(ret))
+		return;
+
+	WARN_ON_ONCE(pci_ser_delete(ser, dev));
+	dev->liveupdate_outgoing = false;
+}

drivers/pci/liveupdate.c line 177

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_unpreserve);
+
+u32 pci_liveupdate_incoming_nr_devices(void)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return 0;

Masking this error looks troubled, in the following patch, I see that the retval 0 is treated as a fresh boot, but the IOMMU mappings for that BDF might still be preserved? Which could lead to DMA aliasing issues, without a hint of what happened since we don't even log anything.

Maybe we could have something like the following:

int pci_liveupdate_incoming_nr_devices(void)

{
	struct pci_ser *ser;
	int ret;

	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
	if (ret) {
		if (ret != -ENOENT)
			pr_warn("PCI: Failed to retrieve preservation list: %d\n", ret);
		return ret;
	}

	return ser->nr_devices;
}

drivers/pci/liveupdate.c line 190

Quoted 13 lines
drivers/pci/liveupdate.c new file +13 -0
+
+	return ser->nr_devices;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_nr_devices);
+
+void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return;

We should log something here either at info / debug level since the error isn't bubbled up and the luo_core doesn't scream about it either.

drivers/pci/liveupdate.c line 192

Quoted 2 lines
drivers/pci/liveupdate.c new file +2 -0
+
+	dev->liveupdate_incoming = !!pci_ser_find(ser, dev);

This feels a little hacky, shall we go for something like:

dev->liveupdate_incoming = (pci_ser_find(ser, dev) != NULL); ?

Quoted 107 lines
drivers/pci/liveupdate.c new file +20 -0
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_setup_device);
+
+void pci_liveupdate_incoming_finish(struct pci_dev *dev)
+{
+	dev->liveupdate_incoming = false;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_finish);
+
+int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh)
+{
+	return liveupdate_register_flb(fh, &pci_liveupdate_flb);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_register_fh);
+
+int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh)
+{
+	return liveupdate_unregister_flb(fh, &pci_liveupdate_flb);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_unregister_fh);
drivers/pci/probe.c +2 -0
index 37329095e5fe..af6356c5a156 100644
@@ -2060,6 +2060,8 @@ int pci_setup_device(struct pci_dev *dev)
 	if (pci_early_dump)
 		early_dump_pci_device(dev);
 
+	pci_liveupdate_setup_device(dev);
+
 	/* Need to have dev->class ready */
 	dev->cfg_size = pci_cfg_space_size(dev);
 
include/linux/kho/abi/pci.h new file +55 -0
new file mode 100644
index 000000000000..6577767f8da6
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * David Matlack <[email protected]>
+ */
+
+#ifndef _LINUX_KHO_ABI_PCI_H
+#define _LINUX_KHO_ABI_PCI_H
+
+#include <linux/compiler.h>
+#include <linux/types.h>
+
+/**
+ * DOC: PCI File-Lifecycle Bound (FLB) Live Update ABI
+ *
+ * This header defines the ABI for preserving core PCI state across kexec using
+ * Live Update File-Lifecycle Bound (FLB) data.
+ *
+ * This interface is a contract. Any modification to any of the serialization
+ * structs defined here constitutes a breaking change. Such changes require
+ * incrementing the version number in the PCI_LUO_FLB_COMPATIBLE string.
+ */
+
+#define PCI_LUO_FLB_COMPATIBLE "pci-v1"
+
+/**
+ * struct pci_dev_ser - Serialized state about a single PCI device.
+ *
+ * @domain: The device's PCI domain number (segment).
+ * @bdf: The device's PCI bus, device, and function number.
+ */
+struct pci_dev_ser {
+	u16 domain;
+	u16 bdf;
+} __packed;
+
+/**
+ * struct pci_ser - PCI Subsystem Live Update State
+ *
+ * This struct tracks state about all devices that are being preserved across
+ * a Live Update for the next kernel.
+ *
+ * @max_nr_devices: The length of the devices[] flexible array.
+ * @nr_devices: The number of devices that were preserved.
+ * @devices: Flexible array of pci_dev_ser structs for each device. Guaranteed
+ *           to be sorted ascending by domain and bdf.
+ */
+struct pci_ser {
+	u64 max_nr_devices;
+	u64 nr_devices;
+	struct pci_dev_ser devices[];
+} __packed;
+
+#endif /* _LINUX_KHO_ABI_PCI_H */
include/linux/pci.h +1 -0
index 7e36936bb37a..9ead6d84aef6 100644
@@ -40,6 +40,7 @@
 #include <linux/resource_ext.h>
 #include <linux/msi_api.h>
 #include <uapi/linux/pci.h>
+#include <linux/liveupdate.h>
 
 #include <linux/pci_ids.h>
 

include/linux/pci.h struct pci_dev { · line 590

Quoted 8 lines
include/linux/pci.h +4 -0
@@ -582,6 +583,10 @@ struct pci_dev {
 	u8		tph_mode;	/* TPH mode */
 	u8		tph_req_type;	/* TPH requester type */
 #endif
+#ifdef CONFIG_LIVEUPDATE
+	unsigned int	liveupdate_incoming:1;	/* Preserved by previous kernel */
+	unsigned int	liveupdate_outgoing:1;	/* Preserved for next kernel */
+#endif
 };

This would start another anon bitfield container, should we move this above within the existing bitfield? If we've run pahole and found this to be better, then this should be fine.

include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2861

Quoted 6 lines
include/linux/pci.h +0 -0
 static inline struct pci_dev *pci_physfn(struct pci_dev *dev)
@@ -2854,4 +2859,46 @@ void pci_uevent_ers(struct pci_dev *pdev, enum  pci_ers_result err_type);
 	WARN_ONCE(condition, "%s %s: " fmt, \
 		  dev_driver_string(&(pdev)->dev), pci_name(pdev), ##arg)
 

[ ---->8------]

include/linux/pci.h void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type); · line 2904

Quoted 3 lines
include/linux/pci.h +2 -0
+#endif /* !CONFIG_LIVEUPDATE */
+
 #endif /* LINUX_PCI_H */

Thanks,
Praan

author maintainer
#14 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 4 lines · Pranjal Shrivastava
Quoted 1 line · David Matlack

+ * Copyright (c) 2025, Google LLC.

Nit: Should these be 2026 now?

Yes! Thanks for catching that.

Quoted 22 lines
Quoted 11 lines
drivers/pci/liveupdate.c new file +11 -0
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+     struct pci_dev_ser new = INIT_PCI_DEV_SER(dev);
+     struct pci_ser *ser;
+     int i, ret;
+
+     /* Preserving VFs is not supported yet. */
+     if (dev->is_virtfn)
+             return -EINVAL;
+
+     guard(mutex)(&pci_flb_outgoing_lock);

drivers/pci/liveupdate.c line 132

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+
+     if (dev->liveupdate_outgoing)
+             return -EBUSY;
+
+     ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+     if (ret)
+             return ret;
+
+     if (ser->nr_devices == ser->max_nr_devices)
+             return -E2BIG;
Quoted 7 lines

I'm wondering how (or if) this handles hot-plugged devices? max_nr_devices is calculated based on for_each_pci_dev at the time of the first preservation.. what happens if a device is hotplugged after the first device is preserved but before the second one is, does max_nr_devices become stale? Since ser->max_nr_devices will not reflect the actual possible device count, potentially leading to an unnecessary -E2BIG failure?

Yes, it's possible to run out space to preserve devices if devices are hot-plugged and then preserved. But I think it's better to defer handling such a use-case exists (unless you see an obvious simple solution). So far I am not seeing preserving hot-plugged devices across Live Update as a high priority use-case to support.

Quoted 5 lines
Quoted 5 lines
drivers/pci/liveupdate.c new file +5 -0
+u32 pci_liveupdate_incoming_nr_devices(void)
+{
+     struct pci_ser *ser;
+     int ret;
+
Quoted 8 lines

drivers/pci/liveupdate.c line 177

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+     ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+     if (ret)
+             return 0;

Masking this error looks troubled, in the following patch, I see that the retval 0 is treated as a fresh boot, but the IOMMU mappings for that BDF might still be preserved? Which could lead to DMA aliasing issues, without a hint of what happened since we don't even log anything.

All fo the non-0 errors indicate there are 0 incoming devices at the time of the call, so I think returning 0 is appropriate.

- EOPNOTSUPP: Live Update is not enabled.
- ENODATA: Live Update is finished (all incoming devices have been restored).
- ENOTENT: No PCI data was preserved across the Live Update.

None of these cover the case where an IOMMU mapping for BDF X is preserved, but device X is not preserved. This is a case we should handle in some way... but here is not that place.

Quoted 4 lines

Maybe we could have something like the following:

int pci_liveupdate_incoming_nr_devices(void)

Quoted 8 lines
{
        struct pci_ser *ser;
        int ret;

        ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
        if (ret) {
                if (ret != -ENOENT)
                        pr_warn("PCI: Failed to retrieve preservation list: %d\n", ret);

This would cause this warning to get printed if Live Update was disabled, or if no PCI devices were preserved. But both of those are not error scenarios.

Quoted 11 lines

drivers/pci/liveupdate.c line 190

Quoted 8 lines
drivers/pci/liveupdate.c new file +8 -0
+void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+     struct pci_ser *ser;
+     int ret;
+
+     ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+     if (ret)
+             return;

We should log something here either at info / debug level since the error isn't bubbled up and the luo_core doesn't scream about it either.

Any error from liveupdate_flb_get_incoming() simply means there are no incoming devices. So I don't think there's any error to report in dmesg.

Quoted 5 lines
Quoted 1 line

+ dev->liveupdate_incoming = !!pci_ser_find(ser, dev);

This feels a little hacky, shall we go for something like:

dev->liveupdate_incoming = (pci_ser_find(ser, dev) != NULL); ?

In my experience in the kernel (mostly from KVM), explicity comparison to NULL is less preferred to treating a pointer as a boolean. But I'm ok with following whatever is the locally preferred style for this kind of check.

Quoted 5 lines
Quoted 5 lines
include/linux/pci.h +1 -0
@@ -582,6 +583,10 @@ struct pci_dev {
      u8              tph_mode;       /* TPH mode */
      u8              tph_req_type;   /* TPH requester type */
 #endif
+#ifdef CONFIG_LIVEUPDATE
Quoted 8 lines

include/linux/pci.h struct pci_dev { · line 590

Quoted 4 lines
include/linux/pci.h +3 -0
+     unsigned int    liveupdate_incoming:1;  /* Preserved by previous kernel */
+     unsigned int    liveupdate_outgoing:1;  /* Preserved for next kernel */
+#endif
 };

This would start another anon bitfield container, should we move this above within the existing bitfield? If we've run pahole and found this to be better, then this should be fine.

Yeah I simply appended these new fields to the very end of the struct. If we care about optimizing the packing of struct pci_dev I can find a better place to put it.

#15 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 38 lines · David Matlack
Quoted 4 lines · Pranjal Shrivastava
Quoted 1 line · David Matlack

+ * Copyright (c) 2025, Google LLC.

Nit: Should these be 2026 now?

Yes! Thanks for catching that.

Quoted 22 lines
Quoted 11 lines
drivers/pci/liveupdate.c new file +11 -0
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+     struct pci_dev_ser new = INIT_PCI_DEV_SER(dev);
+     struct pci_ser *ser;
+     int i, ret;
+
+     /* Preserving VFs is not supported yet. */
+     if (dev->is_virtfn)
+             return -EINVAL;
+
+     guard(mutex)(&pci_flb_outgoing_lock);

drivers/pci/liveupdate.c line 132

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+
+     if (dev->liveupdate_outgoing)
+             return -EBUSY;
+
+     ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+     if (ret)
+             return ret;
+
+     if (ser->nr_devices == ser->max_nr_devices)
+             return -E2BIG;
Quoted 7 lines

I'm wondering how (or if) this handles hot-plugged devices? max_nr_devices is calculated based on for_each_pci_dev at the time of the first preservation.. what happens if a device is hotplugged after the first device is preserved but before the second one is, does max_nr_devices become stale? Since ser->max_nr_devices will not reflect the actual possible device count, potentially leading to an unnecessary -E2BIG failure?

Quoted 6 lines

Yes, it's possible to run out space to preserve devices if devices are hot-plugged and then preserved. But I think it's better to defer handling such a use-case exists (unless you see an obvious simple solution). So far I am not seeing preserving hot-plugged devices across Live Update as a high priority use-case to support.

Ack. If we aren't supporting preservation for hot-plug at this point. Let's mention that somewhere? Maybe just a little comment or the kdoc?

Quoted 33 lines
Quoted 5 lines
Quoted 5 lines
drivers/pci/liveupdate.c new file +5 -0
+u32 pci_liveupdate_incoming_nr_devices(void)
+{
+     struct pci_ser *ser;
+     int ret;
+
Quoted 8 lines

drivers/pci/liveupdate.c line 177

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+     ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+     if (ret)
+             return 0;

Masking this error looks troubled, in the following patch, I see that the retval 0 is treated as a fresh boot, but the IOMMU mappings for that BDF might still be preserved? Which could lead to DMA aliasing issues, without a hint of what happened since we don't even log anything.

All fo the non-0 errors indicate there are 0 incoming devices at the time of the call, so I think returning 0 is appropriate.

- EOPNOTSUPP: Live Update is not enabled.
- ENODATA: Live Update is finished (all incoming devices have been restored).
- ENOTENT: No PCI data was preserved across the Live Update.

None of these cover the case where an IOMMU mapping for BDF X is preserved, but device X is not preserved. This is a case we should handle in some way... but here is not that place.

Quoted 8 lines

Maybe we could have something like the following:

int pci_liveupdate_incoming_nr_devices(void)

{
        struct pci_ser *ser;
        int ret;
Quoted 9 lines
Quoted 4 lines
        ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
        if (ret) {
                if (ret != -ENOENT)
                        pr_warn("PCI: Failed to retrieve preservation list: %d\n", ret);

This would cause this warning to get printed if Live Update was disabled, or if no PCI devices were preserved. But both of those are not error scenarios.

I agree, the snippet was just an example. What I'm trying to say here is, what if the retval is -ENOMEM / -ENODATA, the existing code will treat it as a fresh boot because it believes there are no incoming devices. However, since this was an incoming device which failed to be retrieved, there's a chance that it's IOMMU mapping was preserved too. By returning 0, the PCI core will feel free to rebalance bus numbers or reassign BARs. For instance, if the IOMMU already inherited mappings for BDF 02:00.0, but the PCI core (due to this masked error) reassigns a different device to that BDF, we face DMA aliasing or IOMMU faults. Am I missing some context here?

Quoted 18 lines
Quoted 11 lines

drivers/pci/liveupdate.c line 190

Quoted 8 lines
drivers/pci/liveupdate.c new file +8 -0
+void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+     struct pci_ser *ser;
+     int ret;
+
+     ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+     if (ret)
+             return;

We should log something here either at info / debug level since the error isn't bubbled up and the luo_core doesn't scream about it either.

Any error from liveupdate_flb_get_incoming() simply means there are no incoming devices. So I don't think there's any error to report in dmesg.

Quoted 2 lines
Quoted 1 line

+ dev->liveupdate_incoming = !!pci_ser_find(ser, dev);

Quoted 9 lines
Quoted 3 lines

This feels a little hacky, shall we go for something like:

dev->liveupdate_incoming = (pci_ser_find(ser, dev) != NULL); ?

In my experience in the kernel (mostly from KVM), explicity comparison to NULL is less preferred to treating a pointer as a boolean. But I'm ok with following whatever is the locally preferred style for this kind of check.

No strong feelings there, I see both being used in drivers/pci.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
include/linux/pci.h +1 -0
@@ -582,6 +583,10 @@ struct pci_dev {
      u8              tph_mode;       /* TPH mode */
      u8              tph_req_type;   /* TPH requester type */
 #endif
+#ifdef CONFIG_LIVEUPDATE
Quoted 8 lines

include/linux/pci.h struct pci_dev { · line 590

Quoted 4 lines
include/linux/pci.h +3 -0
+     unsigned int    liveupdate_incoming:1;  /* Preserved by previous kernel */
+     unsigned int    liveupdate_outgoing:1;  /* Preserved for next kernel */
+#endif
 };

This would start another anon bitfield container, should we move this above within the existing bitfield? If we've run pahole and found this to be better, then this should be fine.

Quoted 3 lines

Yeah I simply appended these new fields to the very end of the struct. If we care about optimizing the packing of struct pci_dev I can find a better place to put it.

If you have pahole handy, it would be great to see if these can slide into an existing hole. If not, no big deal for v3.. we can keep it as is

Thanks,
Praan

#16 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 46 lines · Pranjal Shrivastava
Quoted 38 lines · David Matlack
Quoted 4 lines · Pranjal Shrivastava
Quoted 1 line · David Matlack

+ * Copyright (c) 2025, Google LLC.

Nit: Should these be 2026 now?

Yes! Thanks for catching that.

Quoted 22 lines
Quoted 11 lines
drivers/pci/liveupdate.c new file +11 -0
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+     struct pci_dev_ser new = INIT_PCI_DEV_SER(dev);
+     struct pci_ser *ser;
+     int i, ret;
+
+     /* Preserving VFs is not supported yet. */
+     if (dev->is_virtfn)
+             return -EINVAL;
+
+     guard(mutex)(&pci_flb_outgoing_lock);

drivers/pci/liveupdate.c line 132

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+
+     if (dev->liveupdate_outgoing)
+             return -EBUSY;
+
+     ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+     if (ret)
+             return ret;
+
+     if (ser->nr_devices == ser->max_nr_devices)
+             return -E2BIG;
Quoted 7 lines

I'm wondering how (or if) this handles hot-plugged devices? max_nr_devices is calculated based on for_each_pci_dev at the time of the first preservation.. what happens if a device is hotplugged after the first device is preserved but before the second one is, does max_nr_devices become stale? Since ser->max_nr_devices will not reflect the actual possible device count, potentially leading to an unnecessary -E2BIG failure?

Quoted 6 lines

Yes, it's possible to run out space to preserve devices if devices are hot-plugged and then preserved. But I think it's better to defer handling such a use-case exists (unless you see an obvious simple solution). So far I am not seeing preserving hot-plugged devices across Live Update as a high priority use-case to support.

Quoted 24 lines

Ack. If we aren't supporting preservation for hot-plug at this point. Let's mention that somewhere? Maybe just a little comment or the kdoc?

Quoted 21 lines
Quoted 5 lines
Quoted 5 lines
drivers/pci/liveupdate.c new file +5 -0
+u32 pci_liveupdate_incoming_nr_devices(void)
+{
+     struct pci_ser *ser;
+     int ret;
+
Quoted 8 lines

drivers/pci/liveupdate.c line 177

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+     ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+     if (ret)
+             return 0;

Masking this error looks troubled, in the following patch, I see that the retval 0 is treated as a fresh boot, but the IOMMU mappings for that BDF might still be preserved? Which could lead to DMA aliasing issues, without a hint of what happened since we don't even log anything.

All fo the non-0 errors indicate there are 0 incoming devices at the time of the call, so I think returning 0 is appropriate.

- EOPNOTSUPP: Live Update is not enabled.
- ENODATA: Live Update is finished (all incoming devices have been restored).
- ENOTENT: No PCI data was preserved across the Live Update.

The flb_retrive_one seems to call:

err = flb->ops->retrieve(&args);

which could be anything honestly.. since the luo_core doesn't scream about it, maybe the caller should?

Thanks,
Praan

Quoted 86 lines
Quoted 12 lines

None of these cover the case where an IOMMU mapping for BDF X is preserved, but device X is not preserved. This is a case we should handle in some way... but here is not that place.

Quoted 8 lines

Maybe we could have something like the following:

int pci_liveupdate_incoming_nr_devices(void)

{
        struct pci_ser *ser;
        int ret;
Quoted 9 lines
Quoted 4 lines
        ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
        if (ret) {
                if (ret != -ENOENT)
                        pr_warn("PCI: Failed to retrieve preservation list: %d\n", ret);

This would cause this warning to get printed if Live Update was disabled, or if no PCI devices were preserved. But both of those are not error scenarios.

I agree, the snippet was just an example. What I'm trying to say here is, what if the retval is -ENOMEM / -ENODATA, the existing code will treat it as a fresh boot because it believes there are no incoming devices. However, since this was an incoming device which failed to be retrieved, there's a chance that it's IOMMU mapping was preserved too. By returning 0, the PCI core will feel free to rebalance bus numbers or reassign BARs. For instance, if the IOMMU already inherited mappings for BDF 02:00.0, but the PCI core (due to this masked error) reassigns a different device to that BDF, we face DMA aliasing or IOMMU faults. Am I missing some context here?

Quoted 18 lines
Quoted 11 lines

drivers/pci/liveupdate.c line 190

Quoted 8 lines
drivers/pci/liveupdate.c new file +8 -0
+void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+     struct pci_ser *ser;
+     int ret;
+
+     ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+     if (ret)
+             return;

We should log something here either at info / debug level since the error isn't bubbled up and the luo_core doesn't scream about it either.

Any error from liveupdate_flb_get_incoming() simply means there are no incoming devices. So I don't think there's any error to report in dmesg.

Quoted 2 lines
Quoted 1 line

+ dev->liveupdate_incoming = !!pci_ser_find(ser, dev);

Quoted 9 lines
Quoted 3 lines

This feels a little hacky, shall we go for something like:

dev->liveupdate_incoming = (pci_ser_find(ser, dev) != NULL); ?

In my experience in the kernel (mostly from KVM), explicity comparison to NULL is less preferred to treating a pointer as a boolean. But I'm ok with following whatever is the locally preferred style for this kind of check.

No strong feelings there, I see both being used in drivers/pci.

Quoted 14 lines
Quoted 5 lines
Quoted 5 lines
include/linux/pci.h +1 -0
@@ -582,6 +583,10 @@ struct pci_dev {
      u8              tph_mode;       /* TPH mode */
      u8              tph_req_type;   /* TPH requester type */
 #endif
+#ifdef CONFIG_LIVEUPDATE
Quoted 8 lines

include/linux/pci.h struct pci_dev { · line 590

Quoted 4 lines
include/linux/pci.h +3 -0
+     unsigned int    liveupdate_incoming:1;  /* Preserved by previous kernel */
+     unsigned int    liveupdate_outgoing:1;  /* Preserved for next kernel */
+#endif
 };

This would start another anon bitfield container, should we move this above within the existing bitfield? If we've run pahole and found this to be better, then this should be fine.

Quoted 3 lines

Yeah I simply appended these new fields to the very end of the struct. If we care about optimizing the packing of struct pci_dev I can find a better place to put it.

If you have pahole handy, it would be great to see if these can slide into an existing hole. If not, no big deal for v3.. we can keep it as is

Thanks,
Praan

#17 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

In reply to David Matlack
Quoted 4 lines · David Matlack

Add an API to enable the PCI subsystem to track all devices that are preserved across a Live Update, including both incoming devices (passed from the previous kernel) and outgoing devices (passed to the next kernel).

I'd probably describe this in the "outgoing ... incoming" order since that's the order of operation.

Quoted 10 lines

Use PCI segment number and BDF to keep track of devices across Live Update. This means the kernel must keep both identifiers constant across a Live Update for any preserved device. VFs are not supported for now, since that requires preserving SR-IOV state on the device to ensure the same number of VFs appear after kexec and with the same BDFs.

Drivers that preserve devices across Live Update can now register their struct liveupdate_file_handler with the PCI subsystem so that the PCI subsystem can allocate and manage File-Lifecycle-Bound (FLB) global data to track the list of incoming and outgoing preserved devices.

In what sense is this "global"? I assume it doesn't mean global scope; does it mean something that persists across the kexec?

Quoted 8 lines

pci_liveupdate_register_fh(driver_fh)
pci_liveupdate_unregister_fh(driver_fh)

Drivers can notify the PCI subsystem whenever a device is preserved and unpreserved with the following APIs:

pci_liveupdate_outgoing_preserve(pci_dev)
pci_liveupdate_outgoing_unpreserve(pci_dev)

IIUC this is basically asking the PCI core to preserve the generic PCI parts of a device, i.e., the PCI core and the driver collaborate to preserve it?

I know what "preserve" means: "please maintain the original state." The sort of made-up "unpreserve" sounds nicely symmetric, but I don't think it means "destroy the original state." It feels like more of a "cancel" of the original request to preserve something. Maybe not worth any change, but I suspect this operation is going to be part of the user interface for the administrator, and the description of "unpreserve" will probably include something like "cancel."

Quoted 6 lines

After a Live Update, the PCI subsystem fetches its FLB global data from the previous kernel from the Live Update Orchestrator (LUO) during device initialization to determine which devices were preserved.

Drivers can check if a device was preserved before userspace retrieves the file for it via pci_dev->liveupdate_incoming.

I see how drivers need to know whether their device has been preserved and needs to be adopted, but what does userspace have to do with this?

I don't know what the file is, but it doesn't sound related to the PCI core, so maybe it should be mentioned elsewhere?

I assume the PCI core preserves a device based only on the indication from the previous kernel.

Quoted 38 lines

Once a driver has finished restoring an incoming preserved device, it can notify the PCI subsystem with the following call, which clears pci_dev->liveupdate_incoming.

pci_liveupdate_incoming_finish(pci_dev)

This API will be used in subsequent commits by the vfio-pci driver to preserve VFIO devices across Live Update and by the PCI subsystem.

---
 drivers/pci/Makefile        |   1 +
 drivers/pci/liveupdate.c    | 212 ++++++++++++++++++++++++++++++++++++
 drivers/pci/probe.c         |   2 +
 include/linux/kho/abi/pci.h |  55 ++++++++++
 include/linux/pci.h         |  47 ++++++++
 5 files changed, 317 insertions(+)
 create mode 100644 drivers/pci/liveupdate.c
 create mode 100644 include/linux/kho/abi/pci.h
drivers/pci/Makefile +1 -0
index 8c259a9a8796..a32f7658b9e5 100644
@@ -16,6 +16,7 @@ obj-$(CONFIG_PROC_FS)		+= proc.o
 obj-$(CONFIG_SYSFS)		+= pci-sysfs.o slot.o
 obj-$(CONFIG_ACPI)		+= pci-acpi.o
 obj-$(CONFIG_GENERIC_PCI_IOMAP) += iomap.o
+obj-$(CONFIG_LIVEUPDATE)	+= liveupdate.o
 endif
 
 obj-$(CONFIG_OF)		+= of.o
drivers/pci/liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..182cfc793b80

drivers/pci/liveupdate.c line 4

Quoted 4 lines
drivers/pci/liveupdate.c new file +4 -0
@@ -0,0 +1,212 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2025, Google LLC.

2026

Quoted 14 lines
drivers/pci/liveupdate.c new file +14 -0
+ * David Matlack <[email protected]>
+ */
+
+#include <linux/bsearch.h>
+#include <linux/io.h>
+#include <linux/kexec_handover.h>
+#include <linux/kho/abi/pci.h>
+#include <linux/liveupdate.h>
+#include <linux/mutex.h>
+#include <linux/mm.h>
+#include <linux/pci.h>
+#include <linux/sort.h>
+
+static DEFINE_MUTEX(pci_flb_outgoing_lock);

drivers/pci/liveupdate.c line 28

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_dev *dev = NULL;
+	int max_nr_devices = 0;
+	struct pci_ser *ser;
+	unsigned long size;
+
+	for_each_pci_dev(dev)
+		max_nr_devices++;

How is this protected against hotplug?

Quoted 8 lines
drivers/pci/liveupdate.c new file +8 -0
+	size = struct_size_t(struct pci_ser, devices, max_nr_devices);
+
+	ser = kho_alloc_preserve(size);
+	if (IS_ERR(ser))
+		return PTR_ERR(ser);
+
+	ser->max_nr_devices = max_nr_devices;
+

drivers/pci/liveupdate.c line 47

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+	args->obj = ser;
+	args->data = virt_to_phys(ser);
+	return 0;
+}
+
+static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_ser *ser = args->obj;
+
+	WARN_ON_ONCE(ser->nr_devices);

What is this warning telling the user? Is there something wrong? What would I do to fix things if I saw this, and how would I know what to do?

Is this telling us that we're undoing a "preserve" operation, which is supposed to involve "unpreserve" on each device that has previously been marked for preservation, but something (what?) forgot to unpreserve one of the devices?

Seems like maybe a debugging aid, but probably not something an indication that the administrator did something wrong?

Quoted 15 lines
drivers/pci/liveupdate.c new file +15 -0
+	kho_unpreserve_free(ser);
+}
+
+static int pci_flb_retrieve(struct liveupdate_flb_op_args *args)
+{
+	args->obj = phys_to_virt(args->data);
+	return 0;
+}
+
+static void pci_flb_finish(struct liveupdate_flb_op_args *args)
+{
+	kho_restore_free(args->obj);
+}
+
+static struct liveupdate_flb_ops pci_liveupdate_flb_ops = {

drivers/pci/liveupdate.c line 72

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+	.preserve = pci_flb_preserve,
+	.unpreserve = pci_flb_unpreserve,
+	.retrieve = pci_flb_retrieve,
+	.finish = pci_flb_finish,
+	.owner = THIS_MODULE,
+};
+
+static struct liveupdate_flb pci_liveupdate_flb = {
+	.ops = &pci_liveupdate_flb_ops,
+	.compatible = PCI_LUO_FLB_COMPATIBLE,

I don't see anything in this series that checks this. Maybe omit it until it's used?

Quoted 42 lines
drivers/pci/liveupdate.c new file +42 -0
+};
+
+#define INIT_PCI_DEV_SER(_dev) {		\
+	.domain = pci_domain_nr((_dev)->bus),	\
+	.bdf = pci_dev_id(_dev),		\
+}
+
+static int pci_dev_ser_cmp(const void *__a, const void *__b)
+{
+	const struct pci_dev_ser *a = __a, *b = __b;
+
+	return cmp_int(a->domain << 16 | a->bdf, b->domain << 16 | b->bdf);
+}
+
+static struct pci_dev_ser *pci_ser_find(struct pci_ser *ser,
+					struct pci_dev *dev)
+{
+	const struct pci_dev_ser key = INIT_PCI_DEV_SER(dev);
+
+	return bsearch(&key, ser->devices, ser->nr_devices,
+		       sizeof(key), pci_dev_ser_cmp);
+}
+
+static int pci_ser_delete(struct pci_ser *ser, struct pci_dev *dev)
+{
+	struct pci_dev_ser *dev_ser;
+	int i;
+
+	dev_ser = pci_ser_find(ser, dev);
+	if (!dev_ser)
+		return -ENOENT;
+
+	for (i = dev_ser - ser->devices; i < ser->nr_devices - 1; i++)
+		ser->devices[i] = ser->devices[i + 1];
+
+	ser->nr_devices--;
+	return 0;
+}
+
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+	struct pci_dev_ser new = INIT_PCI_DEV_SER(dev);

drivers/pci/liveupdate.c line 124

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+	struct pci_ser *ser;
+	int i, ret;
+
+	/* Preserving VFs is not supported yet. */
+	if (dev->is_virtfn)
+		return -EINVAL;
+
+	guard(mutex)(&pci_flb_outgoing_lock);
+
+	if (dev->liveupdate_outgoing)

Is there a real need to keep "dev->liveupdate_outgoing", as opposed to just searching ser->devices[]? It looks like the only use is to keep from adding a device to ser->devices[] twice.

drivers/pci/liveupdate.c line 127

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+		return -EBUSY;
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);

I guess this "get" must correspond with the kho_alloc_preserve() in pci_flb_preserve()? It's sort of annoying that there's nothing in the function names to connect them because it's hard to find the source.

Quoted 25 lines
drivers/pci/liveupdate.c new file +25 -0
+	if (ret)
+		return ret;
+
+	if (ser->nr_devices == ser->max_nr_devices)
+		return -E2BIG;
+
+	for (i = ser->nr_devices; i > 0; i--) {
+		struct pci_dev_ser *prev = &ser->devices[i - 1];
+		int cmp = pci_dev_ser_cmp(&new, prev);
+
+		if (WARN_ON_ONCE(!cmp))
+			return -EBUSY;
+
+		if (cmp > 0)
+			break;
+
+		ser->devices[i] = *prev;
+	}
+
+	ser->devices[i] = new;
+	ser->nr_devices++;
+	dev->liveupdate_outgoing = true;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_preserve);

drivers/pci/liveupdate.c line 162

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+
+void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	guard(mutex)(&pci_flb_outgoing_lock);
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+	if (WARN_ON_ONCE(ret))

I'm a little dubious about all the WARN_ON_ONCE() calls here. They seem like debugging aids that we would want to remove eventually, so maybe they should just be out-of-tree to begin with.

drivers/pci/liveupdate.c line 165

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+		return;
+
+	WARN_ON_ONCE(pci_ser_delete(ser, dev));

I don't like putting code with side effects inside WARN_ON() because the important code gets hidden.

Quoted 6 lines
drivers/pci/liveupdate.c new file +6 -0
+	dev->liveupdate_outgoing = false;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_unpreserve);
+
+u32 pci_liveupdate_incoming_nr_devices(void)
+{

drivers/pci/liveupdate.c line 181

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+	struct pci_ser *ser;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return 0;
+
+	return ser->nr_devices;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_nr_devices);

Currently only called from drivers/pci/probe.c; does this really need to be declared in include/linux/pci.h and exported? We can do that later if/when needed. Put in drivers/pci/pci.h if only needed in drivers/pci/.

drivers/pci/liveupdate.c line 192

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return;
+
+	dev->liveupdate_incoming = !!pci_ser_find(ser, dev);

I think this would be easier to read as:

  if (pci_ser_find(ser, dev))
    dev->liveupdate_incoming = true;

drivers/pci/liveupdate.c line 192

Quoted 2 lines
drivers/pci/liveupdate.c new file +2 -0
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_setup_device);

Currently only called from pci_setup_device(); does this really need to be declared in include/linux/pci.h and exported? We can do that later if/when needed.

drivers/pci/liveupdate.c line 198

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+void pci_liveupdate_incoming_finish(struct pci_dev *dev)
+{
+	dev->liveupdate_incoming = false;

What is this useful for? Does the PCI core need to *do* anything after a driver finishes its own adoption of a preserved device? I assume everything the PCI core does, i.e., rebuilding its pci_dev and related things based on KHO data, must be done before the driver sees the device.

include/linux/kho/abi/pci.h

Quoted 32 lines
drivers/pci/liveupdate.c new file +14 -0
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_finish);
+
+int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh)
+{
+	return liveupdate_register_flb(fh, &pci_liveupdate_flb);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_register_fh);
+
+int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh)
+{
+	return liveupdate_unregister_flb(fh, &pci_liveupdate_flb);
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_unregister_fh);
drivers/pci/probe.c +2 -0
index 37329095e5fe..af6356c5a156 100644
@@ -2060,6 +2060,8 @@ int pci_setup_device(struct pci_dev *dev)
 	if (pci_early_dump)
 		early_dump_pci_device(dev);
 
+	pci_liveupdate_setup_device(dev);
+
 	/* Need to have dev->class ready */
 	dev->cfg_size = pci_cfg_space_size(dev);
 
include/linux/kho/abi/pci.h new file +0 -0
new file mode 100644
index 000000000000..6577767f8da6

Seems unusual to make an abi/ subdirectory when that's the only thing in kho/, but I assume you have plans for more.

There are several *-abi.h files in include/uapi/, but this abi/ directory is the ABI-ish name in include/linux (except for the include/soc/tegra/bpmp-abi.h oddity).

Quoted 25 lines
include/linux/kho/abi/pci.h new file +24 -0
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * David Matlack <[email protected]>
+ */
+
+#ifndef _LINUX_KHO_ABI_PCI_H
+#define _LINUX_KHO_ABI_PCI_H
+
+#include <linux/compiler.h>
+#include <linux/types.h>
+
+/**
+ * DOC: PCI File-Lifecycle Bound (FLB) Live Update ABI
+ *
+ * This header defines the ABI for preserving core PCI state across kexec using
+ * Live Update File-Lifecycle Bound (FLB) data.
+ *
+ * This interface is a contract. Any modification to any of the serialization
+ * structs defined here constitutes a breaking change. Such changes require
+ * incrementing the version number in the PCI_LUO_FLB_COMPATIBLE string.
+ */
+

include/linux/kho/abi/pci.h line 34

Quoted 10 lines
include/linux/kho/abi/pci.h new file +10 -0
+#define PCI_LUO_FLB_COMPATIBLE "pci-v1"
+
+/**
+ * struct pci_dev_ser - Serialized state about a single PCI device.
+ *
+ * @domain: The device's PCI domain number (segment).
+ * @bdf: The device's PCI bus, device, and function number.
+ */
+struct pci_dev_ser {
+	u16 domain;

ACPI _SEG is limited to 16 bits, but I think all the Linux interfaces use "int" and VMD creates domains starting with 0x10000 to avoid colliding with ACPI segment numbers. I think we should probably use u32 here (and for the Linux interfaces, but that's another patch).

Quoted 94 lines
include/linux/kho/abi/pci.h new file +21 -0
+	u16 bdf;
+} __packed;
+
+/**
+ * struct pci_ser - PCI Subsystem Live Update State
+ *
+ * This struct tracks state about all devices that are being preserved across
+ * a Live Update for the next kernel.
+ *
+ * @max_nr_devices: The length of the devices[] flexible array.
+ * @nr_devices: The number of devices that were preserved.
+ * @devices: Flexible array of pci_dev_ser structs for each device. Guaranteed
+ *           to be sorted ascending by domain and bdf.
+ */
+struct pci_ser {
+	u64 max_nr_devices;
+	u64 nr_devices;
+	struct pci_dev_ser devices[];
+} __packed;
+
+#endif /* _LINUX_KHO_ABI_PCI_H */
include/linux/pci.h +47 -0
index 7e36936bb37a..9ead6d84aef6 100644
@@ -40,6 +40,7 @@
 #include <linux/resource_ext.h>
 #include <linux/msi_api.h>
 #include <uapi/linux/pci.h>
+#include <linux/liveupdate.h>
 
 #include <linux/pci_ids.h>
 
@@ -582,6 +583,10 @@ struct pci_dev {
 	u8		tph_mode;	/* TPH mode */
 	u8		tph_req_type;	/* TPH requester type */
 #endif
+#ifdef CONFIG_LIVEUPDATE
+	unsigned int	liveupdate_incoming:1;	/* Preserved by previous kernel */
+	unsigned int	liveupdate_outgoing:1;	/* Preserved for next kernel */
+#endif
 };
 
 static inline struct pci_dev *pci_physfn(struct pci_dev *dev)
@@ -2854,4 +2859,46 @@ void pci_uevent_ers(struct pci_dev *pdev, enum  pci_ers_result err_type);
 	WARN_ONCE(condition, "%s %s: " fmt, \
 		  dev_driver_string(&(pdev)->dev), pci_name(pdev), ##arg)
 
+#ifdef CONFIG_LIVEUPDATE
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev);
+void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev);
+void pci_liveupdate_setup_device(struct pci_dev *dev);
+u32 pci_liveupdate_incoming_nr_devices(void);
+void pci_liveupdate_incoming_finish(struct pci_dev *dev);
+int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh);
+int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh);
+#else /* !CONFIG_LIVEUPDATE */
+static inline int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev)
+{
+}
+
+static inline void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+}
+
+static inline u32 pci_liveupdate_incoming_nr_devices(void)
+{
+	return 0;
+}
+
+static inline void pci_liveupdate_incoming_finish(struct pci_dev *dev)
+{
+}
+
+static inline int pci_liveupdate_register_fh(struct liveupdate_file_handler *fh)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int pci_liveupdate_unregister_fh(struct liveupdate_file_handler *fh)
+{
+	return -EOPNOTSUPP;
+}
+#endif /* !CONFIG_LIVEUPDATE */
+
 #endif /* LINUX_PCI_H */
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#18 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 8 lines · Bjorn Helgaas
Quoted 4 lines · David Matlack

Add an API to enable the PCI subsystem to track all devices that are preserved across a Live Update, including both incoming devices (passed from the previous kernel) and outgoing devices (passed to the next kernel).

I'd probably describe this in the "outgoing ... incoming" order since that's the order of operation.

Will do.

Quoted 6 lines
Quoted 6 lines

Use PCI segment number and BDF to keep track of devices across Live Update. This means the kernel must keep both identifiers constant across a Live Update for any preserved device. VFs are not supported for now, since that requires preserving SR-IOV state on the device to ensure the same number of VFs appear after kexec and with the same BDFs.

Quoted 7 lines
Quoted 4 lines

Drivers that preserve devices across Live Update can now register their struct liveupdate_file_handler with the PCI subsystem so that the PCI subsystem can allocate and manage File-Lifecycle-Bound (FLB) global data to track the list of incoming and outgoing preserved devices.

In what sense is this "global"? I assume it doesn't mean global scope; does it mean something that persists across the kexec?

Global in the sense that there is only one struct pci_ser in the system. There is no global variable for it though. It is created pci_flb_preserve() when the first device is preserved (and destroyed after the last device is unpreserved/cancelled).

So it's lifecycle is bound to the number of devices. It is maintained by LUO (which does not know about devices, it just knows about files being preserved). For the PCI FLB, each file happens to correspond to a device (these are vfio-pci device files), but LUO doesn't need to know or care.

Quoted 14 lines
Quoted 9 lines

pci_liveupdate_register_fh(driver_fh)
pci_liveupdate_unregister_fh(driver_fh)

Drivers can notify the PCI subsystem whenever a device is preserved and unpreserved with the following APIs:

pci_liveupdate_outgoing_preserve(pci_dev)
pci_liveupdate_outgoing_unpreserve(pci_dev)

IIUC this is basically asking the PCI core to preserve the generic PCI parts of a device, i.e., the PCI core and the driver collaborate to preserve it?

Quoted 7 lines

I know what "preserve" means: "please maintain the original state." The sort of made-up "unpreserve" sounds nicely symmetric, but I don't think it means "destroy the original state." It feels like more of a "cancel" of the original request to preserve something. Maybe not worth any change, but I suspect this operation is going to be part of the user interface for the administrator, and the description of "unpreserve" will probably include something like "cancel."

Yeah it is a cancel of the original request. I chose the name to match the callback op that this function implements (liveupdate_flb_ops.unpreserve).

Quoted 7 lines
Quoted 6 lines

After a Live Update, the PCI subsystem fetches its FLB global data from the previous kernel from the Live Update Orchestrator (LUO) during device initialization to determine which devices were preserved.

Drivers can check if a device was preserved before userspace retrieves the file for it via pci_dev->liveupdate_incoming.

Quoted 8 lines

I see how drivers need to know whether their device has been preserved and needs to be adopted, but what does userspace have to do with this?

I don't know what the file is, but it doesn't sound related to the PCI core, so maybe it should be mentioned elsewhere?

I assume the PCI core preserves a device based only on the indication from the previous kernel.

At the Live Update Orchestrator level the PCI device preservation is driven by userspace preserving files via ioctls. So that framing leaked into this commit message. I will rephrase this to be more in the context of devices that the PCI code actually cares about.

Quoted 4 lines
Quoted 2 lines

+/*
+ * Copyright (c) 2025, Google LLC.

2026

Will do.

Quoted 11 lines

drivers/pci/liveupdate.c line 28

Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_dev *dev = NULL;
+	int max_nr_devices = 0;
+	struct pci_ser *ser;
+	unsigned long size;
+
+	for_each_pci_dev(dev)
+		max_nr_devices++;

How is this protected against hotplug?

Pranjal raised this as well. Here was my reply:

.  Yes, it's possible to run out space to preserve devices if devices are
.  hot-plugged and then preserved. But I think it's better to defer
.  handling such a use-case exists (unless you see an obvious simple
.  solution). So far I am not seeing preserving hot-plugged devices
.  across Live Update as a high priority use-case to support.

I am going to add a comment here in the next revision to clarify that. I will also add a comment clarifying why this code doesn't bother to account for VFs created after this call (preserving VFs are explicitly disallowed to be preserved in this patch since they require additional support).

Quoted 24 lines
Quoted 9 lines
drivers/pci/liveupdate.c new file +8 -0
+	size = struct_size_t(struct pci_ser, devices, max_nr_devices);
+
+	ser = kho_alloc_preserve(size);
+	if (IS_ERR(ser))
+		return PTR_ERR(ser);
+
+	ser->max_nr_devices = max_nr_devices;
+

drivers/pci/liveupdate.c line 47

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+	args->obj = ser;
+	args->data = virt_to_phys(ser);
+	return 0;
+}
+
+static void pci_flb_unpreserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_ser *ser = args->obj;
+
+	WARN_ON_ONCE(ser->nr_devices);

What is this warning telling the user? Is there something wrong? What would I do to fix things if I saw this, and how would I know what to do?

Quoted 7 lines

Is this telling us that we're undoing a "preserve" operation, which is supposed to involve "unpreserve" on each device that has previously been marked for preservation, but something (what?) forgot to unpreserve one of the devices?

Seems like maybe a debugging aid, but probably not something an indication that the administrator did something wrong?

This WARN_ON (and the others you flagged) are all "this should never happen" scenarios. For example if this triggers it means a bug somewhere in the kernel (likely in LUO), or some sort of corruption.

Would you prefer I omit such WARNs or should I just a better accompanying log message or comment?

Quoted 21 lines
Quoted 21 lines
drivers/pci/liveupdate.c new file +20 -0
+	kho_unpreserve_free(ser);
+}
+
+static int pci_flb_retrieve(struct liveupdate_flb_op_args *args)
+{
+	args->obj = phys_to_virt(args->data);
+	return 0;
+}
+
+static void pci_flb_finish(struct liveupdate_flb_op_args *args)
+{
+	kho_restore_free(args->obj);
+}
+
+static struct liveupdate_flb_ops pci_liveupdate_flb_ops = {
+	.preserve = pci_flb_preserve,
+	.unpreserve = pci_flb_unpreserve,
+	.retrieve = pci_flb_retrieve,
+	.finish = pci_flb_finish,
+	.owner = THIS_MODULE,
Quoted 8 lines

drivers/pci/liveupdate.c line 72

Quoted 5 lines
drivers/pci/liveupdate.c new file +5 -0
+};
+
+static struct liveupdate_flb pci_liveupdate_flb = {
+	.ops = &pci_liveupdate_flb_ops,
+	.compatible = PCI_LUO_FLB_COMPATIBLE,

I don't see anything in this series that checks this. Maybe omit it until it's used?

LUO checks this when it receives preserved data from the previous kernel and needs to hand it back to the users of that data. This prevents passing binary data between 2 kernels that don't speak the same ABI.

Quoted 9 lines
Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+int pci_liveupdate_outgoing_preserve(struct pci_dev *dev)
+{
+	struct pci_dev_ser new = INIT_PCI_DEV_SER(dev);
+	struct pci_ser *ser;
+	int i, ret;
+
+	/* Preserving VFs is not supported yet. */
+	if (dev->is_virtfn)
+		return -EINVAL;
Quoted 8 lines

drivers/pci/liveupdate.c line 124

Quoted 4 lines
drivers/pci/liveupdate.c new file +4 -0
+
+	guard(mutex)(&pci_flb_outgoing_lock);
+
+	if (dev->liveupdate_outgoing)

Is there a real need to keep "dev->liveupdate_outgoing", as opposed to just searching ser->devices[]? It looks like the only use is to keep from adding a device to ser->devices[] twice.

Yes that's the only use-case in this series. I think there will be more usecases in the future for checking if a device is outgoing (for example in pci_device_shutdown() to avoid pci_clear_master()). I can defer adding this bit for now if you prefer.

Quoted 8 lines

drivers/pci/liveupdate.c line 127

Quoted 4 lines
drivers/pci/liveupdate.c new file +3 -0
+		return -EBUSY;
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);

I guess this "get" must correspond with the kho_alloc_preserve() in pci_flb_preserve()? It's sort of annoying that there's nothing in the function names to connect them because it's hard to find the source.

Yeah, the LUO FLB flow took me a little while to wrap my head around.

liveupdate_flb_get_outgoing() actually just returns the data pci_flb_preserve() already allocated. It does not trigger pci_flb_preserve().

pci_flb_preserve() is triggered when the first file is preserved, for files that have registered their file handler with PCI (pci_liveupdate_register_fh()).

So what happens in practice is:

0. VFIO calls pci_liveupdate_register_fh(). This is what connects VFIO files with pci_liveupdate_flb.

1. Userpace preserves a vfio-pci file using
LIVEUPDATE_SESSION_PRESERVE_FD.

a. If this is the first file to be preserved associated with
pci_liveupdate_flb, then LUO will call pci_flb_preserve(). This allocates struct pci_ser.

b. LUO calls VFIO preserve callback to preserve the VFIO device
file.

c. VFIO's preserve callback calls pci_liveupdate_outgoing_preserve()
to notify PCI of which PCI device is getting preserved.

c. pci_liveupdate_outgoing_preserve() calls
liveupdate_flb_get_outgoing() to get the data allocated in (a). Note that this data may have been allocated a long time ago since
(a) only happens when the first device is preserved.

(And note I am saying "first" but I really mean when the count goes from 0 to 1.)

It's kind of convoluted but it has some properties that I think make it worth it:

- It works for any kind of file (not just devices).

- It allows other drivers to participate in PCI preservation by registering thier own file handler via pci_liveupdate_register_fh() (so it's not VFIO-specific).

- The lifecycle is managed by LUO (create on first file preserve, destroy on last file unpreserve/cancel). So it avoids PCI subsystem having to maintain any global variables for struct pci_ser.

Quoted 5 lines
Quoted 5 lines
drivers/pci/liveupdate.c new file +5 -0
+void pci_liveupdate_outgoing_unpreserve(struct pci_dev *dev)
+{
+	struct pci_ser *ser;
+	int ret;
+
Quoted 8 lines

drivers/pci/liveupdate.c line 162

Quoted 4 lines
drivers/pci/liveupdate.c new file +4 -0
+	guard(mutex)(&pci_flb_outgoing_lock);
+
+	ret = liveupdate_flb_get_outgoing(&pci_liveupdate_flb, (void **)&ser);
+	if (WARN_ON_ONCE(ret))

I'm a little dubious about all the WARN_ON_ONCE() calls here. They seem like debugging aids that we would want to remove eventually, so maybe they should just be out-of-tree to begin with.

This is another "should never happen" guard.

Quoted 6 lines

drivers/pci/liveupdate.c line 165

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+		return;
+
+	WARN_ON_ONCE(pci_ser_delete(ser, dev));

I don't like putting code with side effects inside WARN_ON() because the important code gets hidden.

Ack, I'll separate out the WARN.

Quoted 13 lines
Quoted 13 lines
drivers/pci/liveupdate.c new file +13 -0
+	dev->liveupdate_outgoing = false;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_outgoing_unpreserve);
+
+u32 pci_liveupdate_incoming_nr_devices(void)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return 0;
+
Quoted 8 lines

drivers/pci/liveupdate.c line 181

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+	return ser->nr_devices;
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_incoming_nr_devices);

Currently only called from drivers/pci/probe.c; does this really need to be declared in include/linux/pci.h and exported? We can do that later if/when needed. Put in drivers/pci/pci.h if only needed in drivers/pci/.

Will do.

Quoted 13 lines

drivers/pci/liveupdate.c line 192

Quoted 10 lines
drivers/pci/liveupdate.c new file +10 -0
+void pci_liveupdate_setup_device(struct pci_dev *dev)
+{
+	struct pci_ser *ser;
+	int ret;
+
+	ret = liveupdate_flb_get_incoming(&pci_liveupdate_flb, (void **)&ser);
+	if (ret)
+		return;
+
+	dev->liveupdate_incoming = !!pci_ser_find(ser, dev);

I think this would be easier to read as:

Quoted 2 lines
  if (pci_ser_find(ser, dev))
    dev->liveupdate_incoming = true;

Will do.

Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+}
+EXPORT_SYMBOL_GPL(pci_liveupdate_setup_device);

Currently only called from pci_setup_device(); does this really need to be declared in include/linux/pci.h and exported? We can do that later if/when needed.

Ack, I'll take this out of include/.

Quoted 9 lines

drivers/pci/liveupdate.c line 198

Quoted 3 lines
drivers/pci/liveupdate.c new file +3 -0
+void pci_liveupdate_incoming_finish(struct pci_dev *dev)
+{
+	dev->liveupdate_incoming = false;

What is this useful for? Does the PCI core need to *do* anything after a driver finishes its own adoption of a preserved device? I assume everything the PCI core does, i.e., rebuilding its pci_dev and related things based on KHO data, must be done before the driver sees the device.

VFIO needs to know this before userspace retrieves the file associated the preserved device from LUO. See patch 08:

https://lore.kernel.org/kvm/[email protected]/

Quoted 9 lines

include/linux/kho/abi/pci.h

Quoted 5 lines
include/linux/kho/abi/pci.h new file +0 -0
new file mode 100644
index 000000000000..6577767f8da6

Seems unusual to make an abi/ subdirectory when that's the only thing in kho/, but I assume you have plans for more.

Quoted 3 lines

There are several *-abi.h files in include/uapi/, but this abi/ directory is the ABI-ish name in include/linux (except for the include/soc/tegra/bpmp-abi.h oddity).

This directory hosts all the ABI structures that are passed from kernel-to-kernel across a Live Update.

  $ ls include/linux/kho/abi
  kexec_handover.h  luo.h  memblock.h  memfd.h  pci.h  vfio_pci.h

I agree the directory structure doesn't need to be quite so deep though.

Quoted 5 lines
Quoted 5 lines

+/**
+ * struct pci_dev_ser - Serialized state about a single PCI device.
+ *
+ * @domain: The device's PCI domain number (segment).
+ * @bdf: The device's PCI bus, device, and function number.

Quoted 8 lines

include/linux/kho/abi/pci.h line 34

Quoted 3 lines

+ */

include/linux/kho/abi/pci.h new file +2 -0
+struct pci_dev_ser {
+	u16 domain;

ACPI _SEG is limited to 16 bits, but I think all the Linux interfaces use "int" and VMD creates domains starting with 0x10000 to avoid colliding with ACPI segment numbers. I think we should probably use u32 here (and for the Linux interfaces, but that's another patch).

Will do.

Thank you for the review!

maintainer
#19 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 20 lines · On Thu, 26 Feb 2026 00:28:28 +0000 David Matlack <[email protected]> wrote:
Quoted 11 lines

drivers/pci/liveupdate.c line 28

Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_dev *dev = NULL;
+	int max_nr_devices = 0;
+	struct pci_ser *ser;
+	unsigned long size;
+
+	for_each_pci_dev(dev)
+		max_nr_devices++;  

How is this protected against hotplug?

Pranjal raised this as well. Here was my reply:

.  Yes, it's possible to run out space to preserve devices if devices are
.  hot-plugged and then preserved. But I think it's better to defer
.  handling such a use-case exists (unless you see an obvious simple
.  solution). So far I am not seeing preserving hot-plugged devices
.  across Live Update as a high priority use-case to support.
Quoted 5 lines

I am going to add a comment here in the next revision to clarify that. I will also add a comment clarifying why this code doesn't bother to account for VFs created after this call (preserving VFs are explicitly disallowed to be preserved in this patch since they require additional support).

TBH, without SR-IOV support and some examples of in-kernel PF preservation in support of vfio-pci VFs, it seems like this only supports a very niche use case. I expect the majority of vfio-pci devices are VFs and I don't think we want to present a solution where the requirement is to move the PF driver to userspace. It's not clear, for example, how we can have vfio-pci variant drivers relying on in-kernel channels to PF drivers to support migration in this model. Thanks,

Alex

author maintainer
#20 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 29 lines · Alex Williamson
Quoted 20 lines · On Thu, 26 Feb 2026 00:28:28 +0000 David Matlack <[email protected]> wrote:
Quoted 11 lines

drivers/pci/liveupdate.c line 28

Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+ struct pci_dev *dev = NULL;
+ int max_nr_devices = 0;
+ struct pci_ser *ser;
+ unsigned long size;
+
+ for_each_pci_dev(dev)
+         max_nr_devices++;

How is this protected against hotplug?

Pranjal raised this as well. Here was my reply:

.  Yes, it's possible to run out space to preserve devices if devices are
.  hot-plugged and then preserved. But I think it's better to defer
.  handling such a use-case exists (unless you see an obvious simple
.  solution). So far I am not seeing preserving hot-plugged devices
.  across Live Update as a high priority use-case to support.
Quoted 5 lines

I am going to add a comment here in the next revision to clarify that. I will also add a comment clarifying why this code doesn't bother to account for VFs created after this call (preserving VFs are explicitly disallowed to be preserved in this patch since they require additional support).

Quoted 3 lines

TBH, without SR-IOV support and some examples of in-kernel PF preservation in support of vfio-pci VFs, it seems like this only supports a very niche use case.

The intent is to start by supporting a simple use-case and expand to more complex scenarios over time, including preserving VFs. Full GPU passthrough is common at cloud providers so even non-VF preservation support is valuable.

Quoted 3 lines

I expect the majority of vfio-pci
devices are VFs and I don't think we want to present a solution where the requirement is to move the PF driver to userspace.

JasonG recommended the upstream support for VF preservation be limited to cases where the PF is also bound to VFIO:

https://lore.kernel.org/lkml/[email protected]/

Within Google we have a way to support in-kernel PF drivers but we are trying to focus on simpler use-cases first upstream.

Quoted 3 lines

It's not clear,
for example, how we can have vfio-pci variant drivers relying on in-kernel channels to PF drivers to support migration in this model.

Agree this still needs to be fleshed out and designed. I think the roadmap will be something like:

1. Get non-VF preservation working end-to-end (device fully preserved
and doing DMA continuously during Live Update).
2. Extend to support VF preservation where the PF is also bound to vfio-pci.
3. (Maybe) Extend to support in-kernel PF drivers.

This series is the first step of #1. I have line of sight to how #2 could work since it's all VFIO.

maintainer
#21 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 58 lines · On Fri, 27 Feb 2026 09:19:28 -0800 David Matlack <[email protected]> wrote:
Quoted 29 lines · Alex Williamson
Quoted 20 lines · On Thu, 26 Feb 2026 00:28:28 +0000 David Matlack <[email protected]> wrote:
Quoted 11 lines

drivers/pci/liveupdate.c line 28

Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+ struct pci_dev *dev = NULL;
+ int max_nr_devices = 0;
+ struct pci_ser *ser;
+ unsigned long size;
+
+ for_each_pci_dev(dev)
+         max_nr_devices++;  

How is this protected against hotplug?

Pranjal raised this as well. Here was my reply:

.  Yes, it's possible to run out space to preserve devices if devices are
.  hot-plugged and then preserved. But I think it's better to defer
.  handling such a use-case exists (unless you see an obvious simple
.  solution). So far I am not seeing preserving hot-plugged devices
.  across Live Update as a high priority use-case to support.
Quoted 5 lines

I am going to add a comment here in the next revision to clarify that. I will also add a comment clarifying why this code doesn't bother to account for VFs created after this call (preserving VFs are explicitly disallowed to be preserved in this patch since they require additional support).

Quoted 3 lines

TBH, without SR-IOV support and some examples of in-kernel PF preservation in support of vfio-pci VFs, it seems like this only supports a very niche use case.

The intent is to start by supporting a simple use-case and expand to more complex scenarios over time, including preserving VFs. Full GPU passthrough is common at cloud providers so even non-VF preservation support is valuable.

Quoted 3 lines

I expect the majority of vfio-pci
devices are VFs and I don't think we want to present a solution where the requirement is to move the PF driver to userspace.

JasonG recommended the upstream support for VF preservation be limited to cases where the PF is also bound to VFIO:

https://lore.kernel.org/lkml/[email protected]/

Within Google we have a way to support in-kernel PF drivers but we are trying to focus on simpler use-cases first upstream.

Quoted 3 lines

It's not clear,
for example, how we can have vfio-pci variant drivers relying on in-kernel channels to PF drivers to support migration in this model.

Agree this still needs to be fleshed out and designed. I think the roadmap will be something like:

Quoted 7 lines

1. Get non-VF preservation working end-to-end (device fully preserved
and doing DMA continuously during Live Update).
2. Extend to support VF preservation where the PF is also bound to vfio-pci.
3. (Maybe) Extend to support in-kernel PF drivers.

This series is the first step of #1. I have line of sight to how #2 could work since it's all VFIO.

Without 3, does this become a mainstream feature?

There's obviously a knee jerk reaction that moving PF drivers into userspace is a means to circumvent the GPL that was evident at LPC, even if the real reason is "in-kernel is hard".

Related to that, there's also not much difference between a userspace driver and an out-of-tree driver when it comes to adding in-kernel code for their specific support requirements. Therefore, unless migration is entirely accomplished via a shared dmabuf between PF and VF, orchestrated through userspace, I'm not sure how we get to migration, making KHO vs migration a binary choice. I have trouble seeing how that's a viable intermediate step. Thanks,

Alex

author maintainer
#22 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 67 lines · Alex Williamson
Quoted 63 lines · On Fri, 27 Feb 2026 09:19:28 -0800 David Matlack <[email protected]> wrote:
Quoted 29 lines · Alex Williamson
Quoted 20 lines · On Thu, 26 Feb 2026 00:28:28 +0000 David Matlack <[email protected]> wrote:
Quoted 11 lines

drivers/pci/liveupdate.c line 28

Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+ struct pci_dev *dev = NULL;
+ int max_nr_devices = 0;
+ struct pci_ser *ser;
+ unsigned long size;
+
+ for_each_pci_dev(dev)
+         max_nr_devices++;

How is this protected against hotplug?

Pranjal raised this as well. Here was my reply:

.  Yes, it's possible to run out space to preserve devices if devices are
.  hot-plugged and then preserved. But I think it's better to defer
.  handling such a use-case exists (unless you see an obvious simple
.  solution). So far I am not seeing preserving hot-plugged devices
.  across Live Update as a high priority use-case to support.
Quoted 5 lines

I am going to add a comment here in the next revision to clarify that. I will also add a comment clarifying why this code doesn't bother to account for VFs created after this call (preserving VFs are explicitly disallowed to be preserved in this patch since they require additional support).

Quoted 3 lines

TBH, without SR-IOV support and some examples of in-kernel PF preservation in support of vfio-pci VFs, it seems like this only supports a very niche use case.

The intent is to start by supporting a simple use-case and expand to more complex scenarios over time, including preserving VFs. Full GPU passthrough is common at cloud providers so even non-VF preservation support is valuable.

Quoted 3 lines

I expect the majority of vfio-pci
devices are VFs and I don't think we want to present a solution where the requirement is to move the PF driver to userspace.

JasonG recommended the upstream support for VF preservation be limited to cases where the PF is also bound to VFIO:

https://lore.kernel.org/lkml/[email protected]/

Within Google we have a way to support in-kernel PF drivers but we are trying to focus on simpler use-cases first upstream.

Quoted 3 lines

It's not clear,
for example, how we can have vfio-pci variant drivers relying on in-kernel channels to PF drivers to support migration in this model.

Agree this still needs to be fleshed out and designed. I think the roadmap will be something like:

1. Get non-VF preservation working end-to-end (device fully preserved
and doing DMA continuously during Live Update).
2. Extend to support VF preservation where the PF is also bound to vfio-pci.
3. (Maybe) Extend to support in-kernel PF drivers.

Quoted 4 lines
Quoted 2 lines

This series is the first step of #1. I have line of sight to how #2 could work since it's all VFIO.

Without 3, does this become a mainstream feature?

I do think there will be enough demand for (3) that it will be worth doing. But I also think ordering the steps this way makes sense from an iterative development point of view.

Quoted 11 lines

There's obviously a knee jerk reaction that moving PF drivers into userspace is a means to circumvent the GPL that was evident at LPC, even if the real reason is "in-kernel is hard".

Related to that, there's also not much difference between a userspace driver and an out-of-tree driver when it comes to adding in-kernel code for their specific support requirements. Therefore, unless migration is entirely accomplished via a shared dmabuf between PF and VF, orchestrated through userspace, I'm not sure how we get to migration, making KHO vs migration a binary choice. I have trouble seeing how that's a viable intermediate step. Thanks,

What do you mean by "migration" in this context?

maintainer
#23 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 89 lines · On Fri, 27 Feb 2026 14:19:45 -0800 David Matlack <[email protected]> wrote:
Quoted 67 lines · Alex Williamson
Quoted 63 lines · On Fri, 27 Feb 2026 09:19:28 -0800 David Matlack <[email protected]> wrote:
Quoted 29 lines · Alex Williamson
Quoted 20 lines · On Thu, 26 Feb 2026 00:28:28 +0000 David Matlack <[email protected]> wrote:
Quoted 11 lines

Review

Quoted 9 lines
(quoted diff) +9 -0
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+ struct pci_dev *dev = NULL;
+ int max_nr_devices = 0;
+ struct pci_ser *ser;
+ unsigned long size;
+
+ for_each_pci_dev(dev)
+         max_nr_devices++;  

How is this protected against hotplug?

Pranjal raised this as well. Here was my reply:

.  Yes, it's possible to run out space to preserve devices if devices are
.  hot-plugged and then preserved. But I think it's better to defer
.  handling such a use-case exists (unless you see an obvious simple
.  solution). So far I am not seeing preserving hot-plugged devices
.  across Live Update as a high priority use-case to support.
Quoted 5 lines

I am going to add a comment here in the next revision to clarify that. I will also add a comment clarifying why this code doesn't bother to account for VFs created after this call (preserving VFs are explicitly disallowed to be preserved in this patch since they require additional support).

Quoted 3 lines

TBH, without SR-IOV support and some examples of in-kernel PF preservation in support of vfio-pci VFs, it seems like this only supports a very niche use case.

The intent is to start by supporting a simple use-case and expand to more complex scenarios over time, including preserving VFs. Full GPU passthrough is common at cloud providers so even non-VF preservation support is valuable.

Quoted 3 lines

I expect the majority of vfio-pci
devices are VFs and I don't think we want to present a solution where the requirement is to move the PF driver to userspace.

JasonG recommended the upstream support for VF preservation be limited to cases where the PF is also bound to VFIO:

https://lore.kernel.org/lkml/[email protected]/

Within Google we have a way to support in-kernel PF drivers but we are trying to focus on simpler use-cases first upstream.

Quoted 3 lines

It's not clear,
for example, how we can have vfio-pci variant drivers relying on in-kernel channels to PF drivers to support migration in this model.

Agree this still needs to be fleshed out and designed. I think the roadmap will be something like:

1. Get non-VF preservation working end-to-end (device fully preserved
and doing DMA continuously during Live Update).
2. Extend to support VF preservation where the PF is also bound to vfio-pci.
3. (Maybe) Extend to support in-kernel PF drivers.

Quoted 4 lines
Quoted 2 lines

This series is the first step of #1. I have line of sight to how #2 could work since it's all VFIO.

Without 3, does this become a mainstream feature?

I do think there will be enough demand for (3) that it will be worth doing. But I also think ordering the steps this way makes sense from an iterative development point of view.

Quoted 11 lines

There's obviously a knee jerk reaction that moving PF drivers into userspace is a means to circumvent the GPL that was evident at LPC, even if the real reason is "in-kernel is hard".

Related to that, there's also not much difference between a userspace driver and an out-of-tree driver when it comes to adding in-kernel code for their specific support requirements. Therefore, unless migration is entirely accomplished via a shared dmabuf between PF and VF, orchestrated through userspace, I'm not sure how we get to migration, making KHO vs migration a binary choice. I have trouble seeing how that's a viable intermediate step. Thanks,

Quoted 1 line

What do you mean by "migration" in this context?

Live migration support, it's the primary use case currently where we have vfio-pci variant drivers on VFs communicating with in-kernel PF drivers. Thanks,

Alex

author maintainer
#24 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 93 lines · Alex Williamson
Quoted 89 lines · On Fri, 27 Feb 2026 14:19:45 -0800 David Matlack <[email protected]> wrote:
Quoted 67 lines · Alex Williamson
Quoted 63 lines · On Fri, 27 Feb 2026 09:19:28 -0800 David Matlack <[email protected]> wrote:
Quoted 29 lines · Alex Williamson
Quoted 20 lines · On Thu, 26 Feb 2026 00:28:28 +0000 David Matlack <[email protected]> wrote:
Quoted 11 lines

Review

Quoted 9 lines
(quoted diff) +9 -0
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+ struct pci_dev *dev = NULL;
+ int max_nr_devices = 0;
+ struct pci_ser *ser;
+ unsigned long size;
+
+ for_each_pci_dev(dev)
+         max_nr_devices++;

How is this protected against hotplug?

Pranjal raised this as well. Here was my reply:

.  Yes, it's possible to run out space to preserve devices if devices are
.  hot-plugged and then preserved. But I think it's better to defer
.  handling such a use-case exists (unless you see an obvious simple
.  solution). So far I am not seeing preserving hot-plugged devices
.  across Live Update as a high priority use-case to support.
Quoted 5 lines

I am going to add a comment here in the next revision to clarify that. I will also add a comment clarifying why this code doesn't bother to account for VFs created after this call (preserving VFs are explicitly disallowed to be preserved in this patch since they require additional support).

Quoted 3 lines

TBH, without SR-IOV support and some examples of in-kernel PF preservation in support of vfio-pci VFs, it seems like this only supports a very niche use case.

The intent is to start by supporting a simple use-case and expand to more complex scenarios over time, including preserving VFs. Full GPU passthrough is common at cloud providers so even non-VF preservation support is valuable.

Quoted 3 lines

I expect the majority of vfio-pci
devices are VFs and I don't think we want to present a solution where the requirement is to move the PF driver to userspace.

JasonG recommended the upstream support for VF preservation be limited to cases where the PF is also bound to VFIO:

https://lore.kernel.org/lkml/[email protected]/

Within Google we have a way to support in-kernel PF drivers but we are trying to focus on simpler use-cases first upstream.

Quoted 3 lines

It's not clear,
for example, how we can have vfio-pci variant drivers relying on in-kernel channels to PF drivers to support migration in this model.

Agree this still needs to be fleshed out and designed. I think the roadmap will be something like:

1. Get non-VF preservation working end-to-end (device fully preserved
and doing DMA continuously during Live Update).
2. Extend to support VF preservation where the PF is also bound to vfio-pci.
3. (Maybe) Extend to support in-kernel PF drivers.

Quoted 4 lines
Quoted 2 lines

This series is the first step of #1. I have line of sight to how #2 could work since it's all VFIO.

Without 3, does this become a mainstream feature?

I do think there will be enough demand for (3) that it will be worth doing. But I also think ordering the steps this way makes sense from an iterative development point of view.

Quoted 11 lines

There's obviously a knee jerk reaction that moving PF drivers into userspace is a means to circumvent the GPL that was evident at LPC, even if the real reason is "in-kernel is hard".

Related to that, there's also not much difference between a userspace driver and an out-of-tree driver when it comes to adding in-kernel code for their specific support requirements. Therefore, unless migration is entirely accomplished via a shared dmabuf between PF and VF, orchestrated through userspace, I'm not sure how we get to migration, making KHO vs migration a binary choice. I have trouble seeing how that's a viable intermediate step. Thanks,

Quoted 5 lines
Quoted 1 line

What do you mean by "migration" in this context?

Live migration support, it's the primary use case currently where we have vfio-pci variant drivers on VFs communicating with in-kernel PF drivers. Thanks,

I see so you're saying if those users wanted Live Update support and we didn't do (3), they would have to give up their Live Migration support. So that would be additional motivation to do (3).

Jason, does this change your mind about whether (3) is worth doing, or whether it should be prioritized over (2)?

I think I still lean toward doing (2) before (3) since Live Update is most useful in setups that cannot support Live Migration. If you can support Live Migration, you have a reasonable way to update host software with minimal impact to the VM. Live Update really shines in scenarios where Live Migraiton is untenable, since host upgrades require VM terminations. In the limit, Live Update can have lower impact on the VM than Live Migration, since there is no state transfer across hosts. But Live Migration can enable more maintenance scenarios than Live Update (like HW maintenance, and firmware upgrades). So I think both are valuable to support.

#25 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 10 lines · David Matlack
Quoted 3 lines

Live migration support, it's the primary use case currently where we have vfio-pci variant drivers on VFs communicating with in-kernel PF drivers. Thanks,

I see so you're saying if those users wanted Live Update support and we didn't do (3), they would have to give up their Live Migration support. So that would be additional motivation to do (3).

Jason, does this change your mind about whether (3) is worth doing, or whether it should be prioritized over (2)?

No, not at all. Live migration of VFIO devices has turned into a valuable but niche feature. This live update is a different niche feature. I don't see an issue with making them exclusive as the starting point.

I think people will eventually be interested in having live update as an option on otherwise live migration capable hypervisors, but that brings a whole other set of problems that are hard to solve..

Quoted 3 lines

software with minimal impact to the VM. Live Update really shines in scenarios where Live Migraiton is untenable, since host upgrades require VM terminations.

Yes, exactly. It is more important to bring a new capability than to optimize something existing.

Jason

#26 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

In reply to Alex Williamson
Quoted 3 lines · Alex Williamson

There's obviously a knee jerk reaction that moving PF drivers into userspace is a means to circumvent the GPL that was evident at LPC, even if the real reason is "in-kernel is hard".

Given we already have GPL licensed kernel drivers for the PFs it doesn't seem like a reasonable worry to me to fret about some cut down version of a kernel driver running in userspace.

Further, let's be honest here, the people most interested in all of this are doing it to support their proprietary VMMs. I'm pushing that we must have at least a reference implementation in qemu before the kernel parts should be merged..

Jason

author maintainer
#27 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

Quoted 7 lines · Jason Gunthorpe
Quoted 3 lines · Alex Williamson

There's obviously a knee jerk reaction that moving PF drivers into userspace is a means to circumvent the GPL that was evident at LPC, even if the real reason is "in-kernel is hard".

Quoted 8 lines

Given we already have GPL licensed kernel drivers for the PFs it doesn't seem like a reasonable worry to me to fret about some cut down version of a kernel driver running in userspace.

Further, let's be honest here, the people most interested in all of this are doing it to support their proprietary VMMs. I'm pushing that we must have at least a reference implementation in qemu before the kernel parts should be merged..

Yes, this is why we built VFIO selftests with the driver framework [1]. We wanted to ensure Live Update support could be well tested upstream independent of VMM adoption and without requiring complex orchestration.

Google is also working on upstreaming Live Update support into Cloud Hypervisor and the kernel pieces land.

[1] https://lore.kernel.org/kvm/[email protected]/

#28 lore raw

Re: [PATCH v2 02/22] PCI: Add API to track PCI devices preserved across Live Update

In reply to Alex Williamson
Quoted 28 lines · Alex Williamson
Quoted 20 lines · On Thu, 26 Feb 2026 00:28:28 +0000 David Matlack <[email protected]> wrote:
Quoted 11 lines

drivers/pci/liveupdate.c line 28

Quoted 9 lines
drivers/pci/liveupdate.c new file +9 -0
+static int pci_flb_preserve(struct liveupdate_flb_op_args *args)
+{
+	struct pci_dev *dev = NULL;
+	int max_nr_devices = 0;
+	struct pci_ser *ser;
+	unsigned long size;
+
+	for_each_pci_dev(dev)
+		max_nr_devices++;  

How is this protected against hotplug?

Pranjal raised this as well. Here was my reply:

.  Yes, it's possible to run out space to preserve devices if devices are
.  hot-plugged and then preserved. But I think it's better to defer
.  handling such a use-case exists (unless you see an obvious simple
.  solution). So far I am not seeing preserving hot-plugged devices
.  across Live Update as a high priority use-case to support.
Quoted 5 lines

I am going to add a comment here in the next revision to clarify that. I will also add a comment clarifying why this code doesn't bother to account for VFs created after this call (preserving VFs are explicitly disallowed to be preserved in this patch since they require additional support).

Quoted 3 lines

TBH, without SR-IOV support and some examples of in-kernel PF preservation in support of vfio-pci VFs, it seems like this only supports a very niche use case.

Well, this is a super complex problem overall and it has to start someplace digestible. There are real use cases of PF only devices, like GPUs for example, where this is entirely sufficient even without SRIOV support.

I expect a long trickle of series building on an enhacing this mechanism one brick at a time.

Quoted 3 lines

I expect the majority of vfio-pci devices are VFs and I don't think we want to present a solution where the requirement is to move the PF driver to userspace.

Well, I do, and am strongly advocating for this. As all these series show supporting live update in a kernel driver is fiendishly complex and most kernel drivers do more than just some bare minimum to operate a PF for SRIOV.

Given we already have PF drivers in userspace and that is working well, lets start there. If people really want to tackle the nasty problems of a kernel side PF driver then they can go do that as a followup.

Quoted 3 lines

It's not clear, for example, how we can have vfio-pci variant drivers relying on in-kernel channels to PF drivers to support migration in this model. Thanks,

Probably not without tremendous work to make the PF driver side kexecable.

The initial use cases for this don't include VFIO migration, I think you could say people are interested in this significantly because VFIO migration isn't viable in the devices they want to use..

Sure it would be nice, but again, lets focus on the basic simple cases, nothing precludes building more and more complexity into the kernel to preserve more and more state across the kexec down the road.

Jason

author maintainer
#29 lore raw

PATCH v2 3/22 PCI: Inherit bus numbers from previous kernel during Live Update

In reply to David Matlack

Inherit bus numbers from the previous kernel during a Live Update when one or more PCI devices are being preserved. This is necessary so that preserved devices can DMA through the IOMMU during a Live Update (changing bus numbers would break IOMMU translation).

---
 drivers/pci/probe.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/pci/probe.c +18 -3
index af6356c5a156..ca6e5f79debb 100644
@@ -1351,6 +1351,20 @@ static bool pci_ea_fixed_busnrs(struct pci_dev *dev, u8 *sec, u8 *sub)
 	return true;
 }
 
+static bool pci_assign_all_busses(void)
+{
+	/*
+	 * During a Live Update where devices are preserved by the previous
+	 * kernel, inherit all bus numbers assigned by the previous kernel. Bus
+	 * numbers must remain stable for preserved devices so that they can
+	 * perform DMA during the Live Update uninterrupted.
+	 */
+	if (pci_liveupdate_incoming_nr_devices())
+		return false;
+
+	return pcibios_assign_all_busses();
+}
+
 /*
  * pci_scan_bridge_extend() - Scan buses behind a bridge
  * @bus: Parent bus the bridge is on
@@ -1378,6 +1392,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 				  int max, unsigned int available_buses,
 				  int pass)
 {
+	bool assign_all_busses = pci_assign_all_busses();
 	struct pci_bus *child;
 	int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
 	u32 buses, i, j = 0;
@@ -1424,7 +1439,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 	pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
 			      bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
 
-	if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
+	if ((secondary || subordinate) && !assign_all_busses &&
 	    !is_cardbus && !broken) {
 		unsigned int cmax, buses;
 
@@ -1467,7 +1482,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 		 * do in the second pass.
 		 */
 		if (!pass) {
-			if (pcibios_assign_all_busses() || broken || is_cardbus)
+			if (assign_all_busses || broken || is_cardbus)
 
 				/*
 				 * Temporarily disable forwarding of the
@@ -1542,7 +1557,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 							max+i+1))
 					break;
 				while (parent->parent) {
-					if ((!pcibios_assign_all_busses()) &&
+					if (!assign_all_busses &&
 					    (parent->busn_res.end > max) &&
 					    (parent->busn_res.end <= max+i)) {
 						j = 1;
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
#30 lore raw

Re: [PATCH v2 03/22] PCI: Inherit bus numbers from previous kernel during Live Update

Quoted 18 lines · David Matlack

Inherit bus numbers from the previous kernel during a Live Update when one or more PCI devices are being preserved. This is necessary so that preserved devices can DMA through the IOMMU during a Live Update (changing bus numbers would break IOMMU translation).

---
 drivers/pci/probe.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/pci/probe.c +0 -0
index af6356c5a156..ca6e5f79debb 100644
@@ -1351,6 +1351,20 @@ static bool pci_ea_fixed_busnrs(struct pci_dev *dev, u8 *sec, u8 *sub)
 	return true;
 }
 

drivers/pci/probe.c static bool pci_ea_fixed_busnrs(struct pci_dev *dev, u8 *sec, u8 *sub) · line 1363

Quoted 10 lines
drivers/pci/probe.c +10 -0
+static bool pci_assign_all_busses(void)
+{
+	/*
+	 * During a Live Update where devices are preserved by the previous
+	 * kernel, inherit all bus numbers assigned by the previous kernel. Bus
+	 * numbers must remain stable for preserved devices so that they can
+	 * perform DMA during the Live Update uninterrupted.
+	 */
+	if (pci_liveupdate_incoming_nr_devices())
+		return false;

Following the comment on Patch 2 regarding propagating errors, the check if (pci_liveupdate_incoming_nr_devices()) should be made explicit to distinguish between "Preservation Active" and "Retrieval Failed".

Quoted 34 lines
drivers/pci/probe.c +7 -2
+
+	return pcibios_assign_all_busses();
+}
+
 /*
  * pci_scan_bridge_extend() - Scan buses behind a bridge
  * @bus: Parent bus the bridge is on
@@ -1378,6 +1392,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 				  int max, unsigned int available_buses,
 				  int pass)
 {
+	bool assign_all_busses = pci_assign_all_busses();
 	struct pci_bus *child;
 	int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
 	u32 buses, i, j = 0;
@@ -1424,7 +1439,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 	pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
 			      bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
 
-	if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
+	if ((secondary || subordinate) && !assign_all_busses &&
 	    !is_cardbus && !broken) {
 		unsigned int cmax, buses;
 
@@ -1467,7 +1482,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 		 * do in the second pass.
 		 */
 		if (!pass) {
-			if (pcibios_assign_all_busses() || broken || is_cardbus)
+			if (assign_all_busses || broken || is_cardbus)
 
 				/*
 				 * Temporarily disable forwarding of the

drivers/pci/probe.c static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev, · line 1563

Quoted 8 lines
drivers/pci/probe.c +1 -1
@@ -1542,7 +1557,7 @@ static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev,
 							max+i+1))
 					break;
 				while (parent->parent) {
-					if ((!pcibios_assign_all_busses()) &&
+					if (!assign_all_busses &&
 					    (parent->busn_res.end > max) &&
 					    (parent->busn_res.end <= max+i)) {
 						j = 1;

Looks like we over-ride the pci=assign-busses boot param here. We should document how this change affects the pci=assign-busses kernel command line. If both are present, the inheritance required by LUO would likely take precedence to prevent DMA corruption, but a doc update & a warning to the user would be nice.

Thanks,
Praan

author maintainer
#31 lore raw

Re: [PATCH v2 03/22] PCI: Inherit bus numbers from previous kernel during Live Update

Quoted 8 lines · Pranjal Shrivastava

drivers/pci/probe.c static bool pci_ea_fixed_busnrs(struct pci_dev *dev, u8 *sec, u8 *sub) · line 1363

Quoted 2 lines · David Matlack
drivers/pci/probe.c +2 -0
+     if (pci_liveupdate_incoming_nr_devices())
+             return false;

Following the comment on Patch 2 regarding propagating errors, the check if (pci_liveupdate_incoming_nr_devices()) should be made explicit to distinguish between "Preservation Active" and "Retrieval Failed".

As mentioned in the previous patch, the errors mean "no incoming devices" rather than "retrieval failed".

Quoted 7 lines

drivers/pci/probe.c static int pci_scan_bridge_extend(struct pci_bus *bus, struct pci_dev *dev, · line 1563

Quoted 6 lines
                              while (parent->parent) {
drivers/pci/probe.c +1 -1
-                                     if ((!pcibios_assign_all_busses()) &&
+                                     if (!assign_all_busses &&
                                          (parent->busn_res.end > max) &&
                                          (parent->busn_res.end <= max+i)) {
                                              j = 1;
Quoted 5 lines

Looks like we over-ride the pci=assign-busses boot param here. We should document how this change affects the pci=assign-busses kernel command line. If both are present, the inheritance required by LUO would likely take precedence to prevent DMA corruption, but a doc update & a warning to the user would be nice.

Good call, I'll add a log message and update kernel-parameters.txt in v3.

#32 lore raw

Re: [PATCH v2 03/22] PCI: Inherit bus numbers from previous kernel during Live Update

In reply to David Matlack
Quoted 4 lines · David Matlack

Inherit bus numbers from the previous kernel during a Live Update when one or more PCI devices are being preserved. This is necessary so that preserved devices can DMA through the IOMMU during a Live Update (changing bus numbers would break IOMMU translation).

I think changing bus numbers would break DMA regardless of whether an IOMMU is involved. Completions carrying the data for DMA reads are routed back to the Requester ID of the read.

author maintainer
#33 lore raw

Re: [PATCH v2 03/22] PCI: Inherit bus numbers from previous kernel during Live Update

Quoted 9 lines · Bjorn Helgaas
Quoted 4 lines · David Matlack

Inherit bus numbers from the previous kernel during a Live Update when one or more PCI devices are being preserved. This is necessary so that preserved devices can DMA through the IOMMU during a Live Update (changing bus numbers would break IOMMU translation).

I think changing bus numbers would break DMA regardless of whether an IOMMU is involved. Completions carrying the data for DMA reads are routed back to the Requester ID of the read.

Ahh, makes sense. I'll clarify the commit message in the next version.

#34 lore raw

Re: [PATCH v2 03/22] PCI: Inherit bus numbers from previous kernel during Live Update

Quoted 7 lines · David Matlack
Quoted 6 lines · Bjorn Helgaas
Quoted 4 lines · David Matlack

Inherit bus numbers from the previous kernel during a Live Update when one or more PCI devices are being preserved. This is necessary so that preserved devices can DMA through the IOMMU during a Live Update (changing bus numbers would break IOMMU translation).

Quoted 6 lines
Quoted 3 lines

I think changing bus numbers would break DMA regardless of whether an IOMMU is involved. Completions carrying the data for DMA reads are routed back to the Requester ID of the read.

Ahh, makes sense. I'll clarify the commit message in the next version.

More broadly you can't shouldn't the fabric topology while Memory Enable is active.

Renumbering or readdressing the fabric requires disabling and flushing any memory transactions.

From that reasoning it is clearer that you can't do that if the device is expected to hitlesslly continue performing memory operations.

That may be a clearer long term basis for describing the requirements here.

Jason

maintainer
#35 lore raw

Re: [PATCH v2 03/22] PCI: Inherit bus numbers from previous kernel during Live Update

Quoted 18 lines · On Thu, 26 Feb 2026 10:40:57 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 7 lines · David Matlack
Quoted 6 lines · Bjorn Helgaas
Quoted 4 lines · David Matlack

Inherit bus numbers from the previous kernel during a Live Update when one or more PCI devices are being preserved. This is necessary so that preserved devices can DMA through the IOMMU during a Live Update (changing bus numbers would break IOMMU translation).

Quoted 6 lines
Quoted 3 lines

I think changing bus numbers would break DMA regardless of whether an IOMMU is involved. Completions carrying the data for DMA reads are routed back to the Requester ID of the read.

Ahh, makes sense. I'll clarify the commit message in the next version.

More broadly you can't shouldn't the fabric topology while Memory Enable is active.

Quoted 8 lines

Renumbering or readdressing the fabric requires disabling and flushing any memory transactions.

From that reasoning it is clearer that you can't do that if the device is expected to hitlesslly continue performing memory operations.

That may be a clearer long term basis for describing the requirements here.

Not only fabric topology, but also routing. ACS overrides on the command line would need to be enforced between the original and kexec kernel such that IOMMU groups are deterministic. Thanks,

Alex

#36 lore raw

Re: [PATCH v2 03/22] PCI: Inherit bus numbers from previous kernel during Live Update

Quoted 1 line · Alex Williamson

Not only fabric topology, but also routing.

Yes

Quoted 3 lines

ACS overrides on the
command line would need to be enforced between the original and kexec kernel such that IOMMU groups are deterministic. Thanks,

That's a good point, I think as a reasonable starting point we should require live update preserved devices to have singleton iommu_groups on both sides. That is easy to check and enforce, and it doesn't matter how the administrator makes that happen.

You also can't change the ACS flags while traffic is flowing because that changes the fabric routing too. This should take care to enforce that restriction as well.

Jason

author maintainer
#37 lore raw

Re: [PATCH v2 03/22] PCI: Inherit bus numbers from previous kernel during Live Update

Quoted 7 lines · Jason Gunthorpe
Quoted 1 line · Alex Williamson

Not only fabric topology, but also routing.

Yes

Quoted 8 lines
Quoted 3 lines

ACS overrides on the
command line would need to be enforced between the original and kexec kernel such that IOMMU groups are deterministic. Thanks,

That's a good point, I think as a reasonable starting point we should require live update preserved devices to have singleton iommu_groups on both sides. That is easy to check and enforce, and it doesn't matter how the administrator makes that happen.

I can add that in the next version.

Quoted 3 lines

You also can't change the ACS flags while traffic is flowing because that changes the fabric routing too. This should take care to enforce that restriction as well.

I'll look into this but will probably defer it to a future series. We need several more pieces in place until we are ready to support traffic to/from preserved devices during the Live Update.

#38 lore raw

Re: [PATCH v2 03/22] PCI: Inherit bus numbers from previous kernel during Live Update

Quoted 3 lines · David Matlack

I'll look into this but will probably defer it to a future series. We need several more pieces in place until we are ready to support traffic to/from preserved devices during the Live Update.

Even traffic directed to the IOMMU is sensitive to the ACS flags and they can't be changed..

Jason

author maintainer
#39 lore raw

PATCH v2 4/22 vfio/pci: Register a file handler with Live Update Orchestrator

In reply to David Matlack

From: Vipin Sharma <[email protected]>

Register a live update file handler for vfio-pci device files. Add stub implementations of all required callbacks so that registration does not fail (i.e. to avoid breaking git-bisect).

This file handler will be extended in subsequent commits to enable a device bound to vfio-pci to run without interruption while the host is going through a kexec Live Update.

---
 MAINTAINERS                            |  1 +
 drivers/vfio/pci/Makefile              |  1 +
 drivers/vfio/pci/vfio_pci.c            |  9 +++-
 drivers/vfio/pci/vfio_pci_liveupdate.c | 69 ++++++++++++++++++++++++++
 drivers/vfio/pci/vfio_pci_priv.h       | 14 ++++++
 include/linux/kho/abi/vfio_pci.h       | 28 +++++++++++
 6 files changed, 121 insertions(+), 1 deletion(-)
 create mode 100644 drivers/vfio/pci/vfio_pci_liveupdate.c
 create mode 100644 include/linux/kho/abi/vfio_pci.h
MAINTAINERS +1 -0
index a671e3d4e8be..7d6cdecedb05 100644
@@ -27520,6 +27520,7 @@ F:	Documentation/ABI/testing/debugfs-vfio
 F:	Documentation/ABI/testing/sysfs-devices-vfio-dev
 F:	Documentation/driver-api/vfio.rst
 F:	drivers/vfio/
+F:	include/linux/kho/abi/vfio_pci.h
 F:	include/linux/vfio.h
 F:	include/linux/vfio_pci_core.h
 F:	include/uapi/linux/vfio.h
drivers/vfio/pci/Makefile +1 -0
index e0a0757dd1d2..23305ebc418b 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o
 
 vfio-pci-y := vfio_pci.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
+vfio-pci-$(CONFIG_LIVEUPDATE) += vfio_pci_liveupdate.o
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
 
 obj-$(CONFIG_MLX5_VFIO_PCI)           += mlx5/
drivers/vfio/pci/vfio_pci.c +8 -1
index 0c771064c0b8..19e88322af2c 100644
@@ -258,6 +258,10 @@ static int __init vfio_pci_init(void)
 	int ret;
 	bool is_disable_vga = true;
 
+	ret = vfio_pci_liveupdate_init();
+	if (ret)
+		return ret;
+
 #ifdef CONFIG_VFIO_PCI_VGA
 	is_disable_vga = disable_vga;
 #endif
@@ -266,8 +270,10 @@ static int __init vfio_pci_init(void)
 
 	/* Register and scan for devices */
 	ret = pci_register_driver(&vfio_pci_driver);
-	if (ret)
+	if (ret) {
+		vfio_pci_liveupdate_cleanup();
 		return ret;
+	}
 
 	vfio_pci_fill_ids();
 
@@ -281,6 +287,7 @@ module_init(vfio_pci_init);
 static void __exit vfio_pci_cleanup(void)
 {
 	pci_unregister_driver(&vfio_pci_driver);
+	vfio_pci_liveupdate_cleanup();
 }
 module_exit(vfio_pci_cleanup);
 
drivers/vfio/pci/vfio_pci_liveupdate.c new file +69 -0

69 lines not on this page · raw message

drivers/vfio/pci/vfio_pci_priv.h +14 -0
index 27ac280f00b9..68966ec64e51 100644
@@ -133,4 +133,18 @@ static inline void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev,
 }
 #endif
 
+#ifdef CONFIG_LIVEUPDATE
+int __init vfio_pci_liveupdate_init(void);
+void vfio_pci_liveupdate_cleanup(void);
+#else
+static inline int vfio_pci_liveupdate_init(void)
+{
+	return 0;
+}
+
+static inline void vfio_pci_liveupdate_cleanup(void)
+{
+}
+#endif /* CONFIG_LIVEUPDATE */
+
 #endif
include/linux/kho/abi/vfio_pci.h new file +28 -0
new file mode 100644
index 000000000000..37a845eed972
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * Vipin Sharma <[email protected]>
+ * David Matlack <[email protected]>
+ */
+
+#ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
+#define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
+
+/**
+ * DOC: VFIO PCI Live Update ABI
+ *
+ * This header defines the ABI for preserving the state of a VFIO PCI device
+ * files across a kexec reboot using LUO.
+ *
+ * Device metadata is serialized into memory which is then handed to the next
+ * kernel via KHO.
+ *
+ * This interface is a contract. Any modification to any of the serialization
+ * structs defined here constitutes a breaking change. Such changes require
+ * incrementing the version number in the VFIO_PCI_LUO_FH_COMPATIBLE string.
+ */
+
+#define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1"
+
+#endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
#40 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

Quoted 137 lines · David Matlack

From: Vipin Sharma <[email protected]>

Register a live update file handler for vfio-pci device files. Add stub implementations of all required callbacks so that registration does not fail (i.e. to avoid breaking git-bisect).

This file handler will be extended in subsequent commits to enable a device bound to vfio-pci to run without interruption while the host is going through a kexec Live Update.

---
  MAINTAINERS                            |  1 +
  drivers/vfio/pci/Makefile              |  1 +
  drivers/vfio/pci/vfio_pci.c            |  9 +++-
  drivers/vfio/pci/vfio_pci_liveupdate.c | 69 ++++++++++++++++++++++++++
  drivers/vfio/pci/vfio_pci_priv.h       | 14 ++++++
  include/linux/kho/abi/vfio_pci.h       | 28 +++++++++++
  6 files changed, 121 insertions(+), 1 deletion(-)

create mode 100644 drivers/vfio/pci/vfio_pci_liveupdate.c create mode 100644 include/linux/kho/abi/vfio_pci.h

MAINTAINERS +1 -0
index a671e3d4e8be..7d6cdecedb05 100644
@@ -27520,6 +27520,7 @@ F:	Documentation/ABI/testing/debugfs-vfio
  F:	Documentation/ABI/testing/sysfs-devices-vfio-dev
  F:	Documentation/driver-api/vfio.rst
  F:	drivers/vfio/
+F:	include/linux/kho/abi/vfio_pci.h
  F:	include/linux/vfio.h
  F:	include/linux/vfio_pci_core.h
  F:	include/uapi/linux/vfio.h
drivers/vfio/pci/Makefile +1 -0
index e0a0757dd1d2..23305ebc418b 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o
 
  vfio-pci-y := vfio_pci.o
  vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
+vfio-pci-$(CONFIG_LIVEUPDATE) += vfio_pci_liveupdate.o
  obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
 
  obj-$(CONFIG_MLX5_VFIO_PCI)           += mlx5/
drivers/vfio/pci/vfio_pci.c +8 -1
index 0c771064c0b8..19e88322af2c 100644
@@ -258,6 +258,10 @@ static int __init vfio_pci_init(void)
  	int ret;
  	bool is_disable_vga = true;
 
+	ret = vfio_pci_liveupdate_init();
+	if (ret)
+		return ret;
+
  #ifdef CONFIG_VFIO_PCI_VGA
  	is_disable_vga = disable_vga;
  #endif
@@ -266,8 +270,10 @@ static int __init vfio_pci_init(void)
 
  	/* Register and scan for devices */
  	ret = pci_register_driver(&vfio_pci_driver);
-	if (ret)
+	if (ret) {
+		vfio_pci_liveupdate_cleanup();
  		return ret;
+	}
 
  	vfio_pci_fill_ids();
 
@@ -281,6 +287,7 @@ module_init(vfio_pci_init);
  static void __exit vfio_pci_cleanup(void)
  {
  	pci_unregister_driver(&vfio_pci_driver);
+	vfio_pci_liveupdate_cleanup();
  }
  module_exit(vfio_pci_cleanup);
 
drivers/vfio/pci/vfio_pci_liveupdate.c new file +48 -0
new file mode 100644
index 000000000000..b84e63c0357b
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * Vipin Sharma <[email protected]>
+ * David Matlack <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/kho/abi/vfio_pci.h>
+#include <linux/liveupdate.h>
+#include <linux/errno.h>
+
+#include "vfio_pci_priv.h"
+
+static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handler *handler,
+					     struct file *file)
+{
+	return false;
+}
+
+static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
+{
+	return -EOPNOTSUPP;
+}
+
+static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args)
+{
+}
+
+static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
+{
+	return -EOPNOTSUPP;
+}
+
+static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *args)
+{
+}
+
+static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
+	.can_preserve = vfio_pci_liveupdate_can_preserve,
+	.preserve = vfio_pci_liveupdate_preserve,
+	.unpreserve = vfio_pci_liveupdate_unpreserve,
+	.retrieve = vfio_pci_liveupdate_retrieve,
+	.finish = vfio_pci_liveupdate_finish,
+	.owner = THIS_MODULE,
+};

drivers/vfio/pci/vfio_pci_liveupdate.c line 58

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +10 -0
+
+static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
+	.ops = &vfio_pci_liveupdate_file_ops,
+	.compatible = VFIO_PCI_LUO_FH_COMPATIBLE,
+};
+
+int __init vfio_pci_liveupdate_init(void)
+{
+	if (!liveupdate_enabled())
+		return 0;

813 int liveupdate_register_file_handler(struct liveupdate_file_handler *fh) 814 {
815         struct liveupdate_file_handler *fh_iter;
816         int err;
817
818         if (!liveupdate_enabled())

819                 return -EOPNOTSUPP;

In the function liveupdate_register_file_handler, liveupdate_enabled is also checked. as such, it is not necessary to check here?

+
+ return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+}
+
+void vfio_pci_liveupdate_cleanup(void)
+{
+ if (!liveupdate_enabled())
+ return;
+

ditto

Zhu Yanjun

Quoted 59 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +2 -0
+	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
+}
drivers/vfio/pci/vfio_pci_priv.h +14 -0
index 27ac280f00b9..68966ec64e51 100644
@@ -133,4 +133,18 @@ static inline void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev,
  }
  #endif
 
+#ifdef CONFIG_LIVEUPDATE
+int __init vfio_pci_liveupdate_init(void);
+void vfio_pci_liveupdate_cleanup(void);
+#else
+static inline int vfio_pci_liveupdate_init(void)
+{
+	return 0;
+}
+
+static inline void vfio_pci_liveupdate_cleanup(void)
+{
+}
+#endif /* CONFIG_LIVEUPDATE */
+
  #endif
include/linux/kho/abi/vfio_pci.h new file +28 -0
new file mode 100644
index 000000000000..37a845eed972
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * Vipin Sharma <[email protected]>
+ * David Matlack <[email protected]>
+ */
+
+#ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
+#define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
+
+/**
+ * DOC: VFIO PCI Live Update ABI
+ *
+ * This header defines the ABI for preserving the state of a VFIO PCI device
+ * files across a kexec reboot using LUO.
+ *
+ * Device metadata is serialized into memory which is then handed to the next
+ * kernel via KHO.
+ *
+ * This interface is a contract. Any modification to any of the serialization
+ * structs defined here constitutes a breaking change. Such changes require
+ * incrementing the version number in the VFIO_PCI_LUO_FH_COMPATIBLE string.
+ */
+
+#define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1"
+
+#endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
author maintainer
#41 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

Quoted 13 lines · Yanjun.Zhu

drivers/vfio/pci/vfio_pci_liveupdate.c line 58

Quoted 4 lines · David Matlack
drivers/vfio/pci/vfio_pci_liveupdate.c new file +4 -0
+int __init vfio_pci_liveupdate_init(void)
+{
+	if (!liveupdate_enabled())
+		return 0;

813 int liveupdate_register_file_handler(struct liveupdate_file_handler *fh) 814 {
815         struct liveupdate_file_handler *fh_iter;
816         int err;
817
818         if (!liveupdate_enabled())

Quoted 4 lines

819                 return -EOPNOTSUPP;

In the function liveupdate_register_file_handler, liveupdate_enabled is also checked. as such, it is not necessary to check here?

Yeah that is a bit odd. I see that memfd_luo_init() just checks for -EOPNOTSUPP. We can do the same thing here.

#42 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

Quoted 14 lines · David Matlack
Quoted 13 lines · Yanjun.Zhu

drivers/vfio/pci/vfio_pci_liveupdate.c line 58

Quoted 4 lines · David Matlack
drivers/vfio/pci/vfio_pci_liveupdate.c new file +4 -0
+int __init vfio_pci_liveupdate_init(void)
+{
+	if (!liveupdate_enabled())
+		return 0;

813 int liveupdate_register_file_handler(struct liveupdate_file_handler *fh) 814 {
815         struct liveupdate_file_handler *fh_iter;
816         int err;
817
818         if (!liveupdate_enabled())

Quoted 7 lines
Quoted 4 lines

819                 return -EOPNOTSUPP;

In the function liveupdate_register_file_handler, liveupdate_enabled is also checked. as such, it is not necessary to check here?

Yeah that is a bit odd. I see that memfd_luo_init() just checks for -EOPNOTSUPP. We can do the same thing here.

If we move to checking the return value for -EOPNOTSUPP, we should ensure the pattern is consistent for the upcoming PCI registration in Patch 7 as well. With this addressed:

Thanks,
Praan

maintainer
#43 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

In reply to David Matlack
Quoted 41 lines · On Thu, 29 Jan 2026 21:24:51 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/vfio_pci.c +8 -1
index 0c771064c0b8..19e88322af2c 100644
@@ -258,6 +258,10 @@ static int __init vfio_pci_init(void)
 	int ret;
 	bool is_disable_vga = true;
 
+	ret = vfio_pci_liveupdate_init();
+	if (ret)
+		return ret;
+
 #ifdef CONFIG_VFIO_PCI_VGA
 	is_disable_vga = disable_vga;
 #endif
@@ -266,8 +270,10 @@ static int __init vfio_pci_init(void)
 
 	/* Register and scan for devices */
 	ret = pci_register_driver(&vfio_pci_driver);
-	if (ret)
+	if (ret) {
+		vfio_pci_liveupdate_cleanup();
 		return ret;
+	}
 
 	vfio_pci_fill_ids();
 
@@ -281,6 +287,7 @@ module_init(vfio_pci_init);
 static void __exit vfio_pci_cleanup(void)
 {
 	pci_unregister_driver(&vfio_pci_driver);
+	vfio_pci_liveupdate_cleanup();
 }
 module_exit(vfio_pci_cleanup);
 
drivers/vfio/pci/vfio_pci_liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..b84e63c0357b
@@ -0,0 +1,69 @@

...

Quoted 11 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +11 -0
+static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
+	.can_preserve = vfio_pci_liveupdate_can_preserve,
+	.preserve = vfio_pci_liveupdate_preserve,
+	.unpreserve = vfio_pci_liveupdate_unpreserve,
+	.retrieve = vfio_pci_liveupdate_retrieve,
+	.finish = vfio_pci_liveupdate_finish,
+	.owner = THIS_MODULE,
+};
+
+static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
+	.ops = &vfio_pci_liveupdate_file_ops,

drivers/vfio/pci/vfio_pci_liveupdate.c line 61

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +10 -0
+	.compatible = VFIO_PCI_LUO_FH_COMPATIBLE,
+};
+
+int __init vfio_pci_liveupdate_init(void)
+{
+	if (!liveupdate_enabled())
+		return 0;
+
+	return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+}

liveupdate_register_file_handler() "pins" vfio-pci with a try_module_get(). Since this is done in our module_init function and unregister occurs in our module_exit function, rather than relative to any actual device binding or usage, this means vfio-pci CANNOT be unloaded. That seems bad. Thanks,

Alex

Quoted 8 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +8 -0
+
+void vfio_pci_liveupdate_cleanup(void)
+{
+	if (!liveupdate_enabled())
+		return;
+
+	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
+}
#44 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

Quoted 63 lines · Alex Williamson
Quoted 41 lines · On Thu, 29 Jan 2026 21:24:51 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/vfio_pci.c +8 -1
index 0c771064c0b8..19e88322af2c 100644
@@ -258,6 +258,10 @@ static int __init vfio_pci_init(void)
 	int ret;
 	bool is_disable_vga = true;
 
+	ret = vfio_pci_liveupdate_init();
+	if (ret)
+		return ret;
+
 #ifdef CONFIG_VFIO_PCI_VGA
 	is_disable_vga = disable_vga;
 #endif
@@ -266,8 +270,10 @@ static int __init vfio_pci_init(void)
 
 	/* Register and scan for devices */
 	ret = pci_register_driver(&vfio_pci_driver);
-	if (ret)
+	if (ret) {
+		vfio_pci_liveupdate_cleanup();
 		return ret;
+	}
 
 	vfio_pci_fill_ids();
 
@@ -281,6 +287,7 @@ module_init(vfio_pci_init);
 static void __exit vfio_pci_cleanup(void)
 {
 	pci_unregister_driver(&vfio_pci_driver);
+	vfio_pci_liveupdate_cleanup();
 }
 module_exit(vfio_pci_cleanup);
 
drivers/vfio/pci/vfio_pci_liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..b84e63c0357b
@@ -0,0 +1,69 @@

...

Quoted 19 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +19 -0
+static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
+	.can_preserve = vfio_pci_liveupdate_can_preserve,
+	.preserve = vfio_pci_liveupdate_preserve,
+	.unpreserve = vfio_pci_liveupdate_unpreserve,
+	.retrieve = vfio_pci_liveupdate_retrieve,
+	.finish = vfio_pci_liveupdate_finish,
+	.owner = THIS_MODULE,
+};
+
+static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
+	.ops = &vfio_pci_liveupdate_file_ops,
+	.compatible = VFIO_PCI_LUO_FH_COMPATIBLE,
+};
+
+int __init vfio_pci_liveupdate_init(void)
+{
+	if (!liveupdate_enabled())
+		return 0;
+
Quoted 8 lines

drivers/vfio/pci/vfio_pci_liveupdate.c

Quoted 2 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +2 -0
+	return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+}

liveupdate_register_file_handler() "pins" vfio-pci with a try_module_get(). Since this is done in our module_init function and unregister occurs in our module_exit function, rather than relative to any actual device binding or usage, this means vfio-pci CANNOT be unloaded. That seems bad. Thanks,

Hmm... IIUC the concern here is about liveupdate policy if the user wants to unload a module which was previously marked for preservation?

AFAICT, In such a case, the user is expected to close the LUO session FD, which "unpreserves" the FD. Finally, when rmmod is executed, the __exit (vfio_pci_cleanup) calls vfio_pci_liveupdate_cleanup() which ends up calling liveupdate_unregister_file_handler(), thereby dropping the ref held by the liveupdate orchestrator which allows the module to be unloaded.

I think we should document this policy somewhere or have a dev_warn to scream at the users when they try unloading the module without closing the session FD.

Thanks,
Praan

Quoted 11 lines

Alex

Quoted 8 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +8 -0
+
+void vfio_pci_liveupdate_cleanup(void)
+{
+	if (!liveupdate_enabled())
+		return;
+
+	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
+}
#45 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

Quoted 76 lines · Pranjal Shrivastava
Quoted 63 lines · Alex Williamson
Quoted 41 lines · On Thu, 29 Jan 2026 21:24:51 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/vfio_pci.c +8 -1
index 0c771064c0b8..19e88322af2c 100644
@@ -258,6 +258,10 @@ static int __init vfio_pci_init(void)
 	int ret;
 	bool is_disable_vga = true;
 
+	ret = vfio_pci_liveupdate_init();
+	if (ret)
+		return ret;
+
 #ifdef CONFIG_VFIO_PCI_VGA
 	is_disable_vga = disable_vga;
 #endif
@@ -266,8 +270,10 @@ static int __init vfio_pci_init(void)
 
 	/* Register and scan for devices */
 	ret = pci_register_driver(&vfio_pci_driver);
-	if (ret)
+	if (ret) {
+		vfio_pci_liveupdate_cleanup();
 		return ret;
+	}
 
 	vfio_pci_fill_ids();
 
@@ -281,6 +287,7 @@ module_init(vfio_pci_init);
 static void __exit vfio_pci_cleanup(void)
 {
 	pci_unregister_driver(&vfio_pci_driver);
+	vfio_pci_liveupdate_cleanup();
 }
 module_exit(vfio_pci_cleanup);
 
drivers/vfio/pci/vfio_pci_liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..b84e63c0357b
@@ -0,0 +1,69 @@

...

Quoted 19 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +19 -0
+static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
+	.can_preserve = vfio_pci_liveupdate_can_preserve,
+	.preserve = vfio_pci_liveupdate_preserve,
+	.unpreserve = vfio_pci_liveupdate_unpreserve,
+	.retrieve = vfio_pci_liveupdate_retrieve,
+	.finish = vfio_pci_liveupdate_finish,
+	.owner = THIS_MODULE,
+};
+
+static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
+	.ops = &vfio_pci_liveupdate_file_ops,
+	.compatible = VFIO_PCI_LUO_FH_COMPATIBLE,
+};
+
+int __init vfio_pci_liveupdate_init(void)
+{
+	if (!liveupdate_enabled())
+		return 0;
+
Quoted 8 lines

drivers/vfio/pci/vfio_pci_liveupdate.c

Quoted 2 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +2 -0
+	return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+}

liveupdate_register_file_handler() "pins" vfio-pci with a try_module_get(). Since this is done in our module_init function and unregister occurs in our module_exit function, rather than relative to any actual device binding or usage, this means vfio-pci CANNOT be unloaded. That seems bad. Thanks,

Hmm... IIUC the concern here is about liveupdate policy if the user wants to unload a module which was previously marked for preservation?

Quoted 7 lines

AFAICT, In such a case, the user is expected to close the LUO session FD, which "unpreserves" the FD. Finally, when rmmod is executed, the __exit (vfio_pci_cleanup) calls vfio_pci_liveupdate_cleanup() which ends up calling liveupdate_unregister_file_handler(), thereby dropping the ref held by the liveupdate orchestrator which allows the module to be unloaded.

Ohh wait, You're right, Alex. I just realized the __exit won't even be reached because of the internal pin. The current implementation creates a catch-22 where the module pins itself during init and can't reach the unregister call in exit. I believe we should drop the ref when the user closes the session FD? Additionally, should we move try_module_get out of the global liveupdate_register_file_handler() and instead take the ref only when a file is actually marked for preservation?

Thanks,
Praan

Quoted 18 lines

I think we should document this policy somewhere or have a dev_warn to scream at the users when they try unloading the module without closing the session FD.

Thanks,
Praan

Quoted 11 lines

Alex

Quoted 8 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +8 -0
+
+void vfio_pci_liveupdate_cleanup(void)
+{
+	if (!liveupdate_enabled())
+		return;
+
+	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
+}
#46 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

Quoted 86 lines · Pranjal Shrivastava
Quoted 76 lines · Pranjal Shrivastava
Quoted 63 lines · Alex Williamson
Quoted 41 lines · On Thu, 29 Jan 2026 21:24:51 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/vfio_pci.c +8 -1
index 0c771064c0b8..19e88322af2c 100644
@@ -258,6 +258,10 @@ static int __init vfio_pci_init(void)
  int ret;
  bool is_disable_vga = true;
 
+ ret = vfio_pci_liveupdate_init();
+ if (ret)
+         return ret;
+
 #ifdef CONFIG_VFIO_PCI_VGA
  is_disable_vga = disable_vga;
 #endif
@@ -266,8 +270,10 @@ static int __init vfio_pci_init(void)
 
  /* Register and scan for devices */
  ret = pci_register_driver(&vfio_pci_driver);
- if (ret)
+ if (ret) {
+         vfio_pci_liveupdate_cleanup();
          return ret;
+ }
 
  vfio_pci_fill_ids();
 
@@ -281,6 +287,7 @@ module_init(vfio_pci_init);
 static void __exit vfio_pci_cleanup(void)
 {
  pci_unregister_driver(&vfio_pci_driver);
+ vfio_pci_liveupdate_cleanup();
 }
 module_exit(vfio_pci_cleanup);
 
drivers/vfio/pci/vfio_pci_liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..b84e63c0357b
@@ -0,0 +1,69 @@

...

Quoted 19 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +19 -0
+static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
+ .can_preserve = vfio_pci_liveupdate_can_preserve,
+ .preserve = vfio_pci_liveupdate_preserve,
+ .unpreserve = vfio_pci_liveupdate_unpreserve,
+ .retrieve = vfio_pci_liveupdate_retrieve,
+ .finish = vfio_pci_liveupdate_finish,
+ .owner = THIS_MODULE,
+};
+
+static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
+ .ops = &vfio_pci_liveupdate_file_ops,
+ .compatible = VFIO_PCI_LUO_FH_COMPATIBLE,
+};
+
+int __init vfio_pci_liveupdate_init(void)
+{
+ if (!liveupdate_enabled())
+         return 0;
+
Quoted 8 lines
Quoted 2 lines

+ return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh); +}

liveupdate_register_file_handler() "pins" vfio-pci with a try_module_get(). Since this is done in our module_init function and unregister occurs in our module_exit function, rather than relative to any actual device binding or usage, this means vfio-pci CANNOT be unloaded. That seems bad. Thanks,

Hmm... IIUC the concern here is about liveupdate policy if the user wants to unload a module which was previously marked for preservation?

Quoted 7 lines

AFAICT, In such a case, the user is expected to close the LUO session FD, which "unpreserves" the FD. Finally, when rmmod is executed, the __exit (vfio_pci_cleanup) calls vfio_pci_liveupdate_cleanup() which ends up calling liveupdate_unregister_file_handler(), thereby dropping the ref held by the liveupdate orchestrator which allows the module to be unloaded.

Quoted 7 lines

Ohh wait, You're right, Alex. I just realized the __exit won't even be reached because of the internal pin. The current implementation creates a catch-22 where the module pins itself during init and can't reach the unregister call in exit. I believe we should drop the ref when the user closes the session FD? Additionally, should we move try_module_get out of the global liveupdate_register_file_handler() and instead take the ref only when a file is actually marked for preservation?

If we don't do try_module_get during registration, the registered file handler can go away on module unload while LUO is using the handler during FD preservation. This makes it racy. Maybe register/unregister need to move outside the module_init/exit.

Quoted 22 lines

Thanks,
Praan

Quoted 18 lines

I think we should document this policy somewhere or have a dev_warn to scream at the users when they try unloading the module without closing the session FD.

Thanks,
Praan

Quoted 11 lines

Alex

Quoted 8 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +8 -0
+
+void vfio_pci_liveupdate_cleanup(void)
+{
+ if (!liveupdate_enabled())
+         return;
+
+ liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
+}
author maintainer
#47 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

In reply to Alex Williamson
Quoted 16 lines · Alex Williamson

drivers/vfio/pci/vfio_pci_liveupdate.c line 61

Quoted 7 lines · On Thu, 29 Jan 2026 21:24:51 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/vfio_pci_liveupdate.c new file +7 -0
+int __init vfio_pci_liveupdate_init(void)
+{
+	if (!liveupdate_enabled())
+		return 0;
+
+	return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+}

liveupdate_register_file_handler() "pins" vfio-pci with a try_module_get(). Since this is done in our module_init function and unregister occurs in our module_exit function, rather than relative to any actual device binding or usage, this means vfio-pci CANNOT be unloaded. That seems bad. Thanks,

Good point. So a better approach that would allow vfio-pci to be unloaded would be to register the file handler when the number of devices bound to vfio-pci goes from 0->1 and then unregister on 1->0.

#48 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

Quoted 19 lines · David Matlack
Quoted 16 lines · Alex Williamson

drivers/vfio/pci/vfio_pci_liveupdate.c line 61

Quoted 7 lines · On Thu, 29 Jan 2026 21:24:51 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/vfio_pci_liveupdate.c new file +7 -0
+int __init vfio_pci_liveupdate_init(void)
+{
+   if (!liveupdate_enabled())
+           return 0;
+
+   return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+}

liveupdate_register_file_handler() "pins" vfio-pci with a try_module_get(). Since this is done in our module_init function and unregister occurs in our module_exit function, rather than relative to any actual device binding or usage, this means vfio-pci CANNOT be unloaded. That seems bad. Thanks,

Quoted 3 lines

Good point. So a better approach that would allow vfio-pci to be unloaded would be to register the file handler when the number of devices bound to vfio-pci goes from 0->1 and then unregister on 1->0.

Yeah maybe a kref that gets inc/dec in probe/remove. I have a similar problem with iommufd preservation, but I think I can handle it based on the number of iommufd open.

I am wondering whether this file handler registration kref stuff can be moved into LUO by adding it to LUO file_handler and the modules only call get/put?

author maintainer
#49 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

Quoted 25 lines · Samiullah Khawaja
Quoted 19 lines · David Matlack
Quoted 16 lines · Alex Williamson

drivers/vfio/pci/vfio_pci_liveupdate.c line 61

Quoted 7 lines · On Thu, 29 Jan 2026 21:24:51 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/vfio_pci_liveupdate.c new file +7 -0
+int __init vfio_pci_liveupdate_init(void)
+{
+   if (!liveupdate_enabled())
+           return 0;
+
+   return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+}

liveupdate_register_file_handler() "pins" vfio-pci with a try_module_get(). Since this is done in our module_init function and unregister occurs in our module_exit function, rather than relative to any actual device binding or usage, this means vfio-pci CANNOT be unloaded. That seems bad. Thanks,

Quoted 3 lines

Good point. So a better approach that would allow vfio-pci to be unloaded would be to register the file handler when the number of devices bound to vfio-pci goes from 0->1 and then unregister on 1->0.

Quoted 7 lines

Yeah maybe a kref that gets inc/dec in probe/remove. I have a similar problem with iommufd preservation, but I think I can handle it based on the number of iommufd open.

I am wondering whether this file handler registration kref stuff can be moved into LUO by adding it to LUO file_handler and the modules only call get/put?

Circling back here, the new plan is for LUO to not take a module reference in liveupdate_register_file_handler():

https://lore.kernel.org/lkml/[email protected]/

In the next version of this series I am going to rebase on top of Pasha's changes and keep the call to liveupdate_register_file_handler() where it is.

#50 lore raw

Re: [PATCH v2 04/22] vfio/pci: Register a file handler with Live Update Orchestrator

In reply to David Matlack
Quoted 89 lines · David Matlack

From: Vipin Sharma <[email protected]>

Register a live update file handler for vfio-pci device files. Add stub implementations of all required callbacks so that registration does not fail (i.e. to avoid breaking git-bisect).

This file handler will be extended in subsequent commits to enable a device bound to vfio-pci to run without interruption while the host is going through a kexec Live Update.

---
  MAINTAINERS                            |  1 +
  drivers/vfio/pci/Makefile              |  1 +
  drivers/vfio/pci/vfio_pci.c            |  9 +++-
  drivers/vfio/pci/vfio_pci_liveupdate.c | 69 ++++++++++++++++++++++++++
  drivers/vfio/pci/vfio_pci_priv.h       | 14 ++++++
  include/linux/kho/abi/vfio_pci.h       | 28 +++++++++++
  6 files changed, 121 insertions(+), 1 deletion(-)

create mode 100644 drivers/vfio/pci/vfio_pci_liveupdate.c create mode 100644 include/linux/kho/abi/vfio_pci.h

MAINTAINERS +1 -0
index a671e3d4e8be..7d6cdecedb05 100644
@@ -27520,6 +27520,7 @@ F:	Documentation/ABI/testing/debugfs-vfio
  F:	Documentation/ABI/testing/sysfs-devices-vfio-dev
  F:	Documentation/driver-api/vfio.rst
  F:	drivers/vfio/
+F:	include/linux/kho/abi/vfio_pci.h
  F:	include/linux/vfio.h
  F:	include/linux/vfio_pci_core.h
  F:	include/uapi/linux/vfio.h
drivers/vfio/pci/Makefile +1 -0
index e0a0757dd1d2..23305ebc418b 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o
 
  vfio-pci-y := vfio_pci.o
  vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
+vfio-pci-$(CONFIG_LIVEUPDATE) += vfio_pci_liveupdate.o
  obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
 
  obj-$(CONFIG_MLX5_VFIO_PCI)           += mlx5/
drivers/vfio/pci/vfio_pci.c +8 -1
index 0c771064c0b8..19e88322af2c 100644
@@ -258,6 +258,10 @@ static int __init vfio_pci_init(void)
  	int ret;
  	bool is_disable_vga = true;
 
+	ret = vfio_pci_liveupdate_init();
+	if (ret)
+		return ret;
+
  #ifdef CONFIG_VFIO_PCI_VGA
  	is_disable_vga = disable_vga;
  #endif
@@ -266,8 +270,10 @@ static int __init vfio_pci_init(void)
 
  	/* Register and scan for devices */
  	ret = pci_register_driver(&vfio_pci_driver);
-	if (ret)
+	if (ret) {
+		vfio_pci_liveupdate_cleanup();
  		return ret;
+	}
 
  	vfio_pci_fill_ids();
 
@@ -281,6 +287,7 @@ module_init(vfio_pci_init);
  static void __exit vfio_pci_cleanup(void)
  {
  	pci_unregister_driver(&vfio_pci_driver);
+	vfio_pci_liveupdate_cleanup();
  }
  module_exit(vfio_pci_cleanup);
 
drivers/vfio/pci/vfio_pci_liveupdate.c new file +0 -0
new file mode 100644
index 000000000000..b84e63c0357b

drivers/vfio/pci/vfio_pci_liveupdate.c line 4

Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +4 -0
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (c) 2025, Google LLC.

The live update support for vfio-pci was initiated in 2025, but developments are into 2026. Update the copyright to 2026 or 2025 - 2026.

Zhu Yanjun

Quoted 122 lines
drivers/vfio/pci/vfio_pci_liveupdate.c new file +65 -0
+ * Vipin Sharma <[email protected]>
+ * David Matlack <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/kho/abi/vfio_pci.h>
+#include <linux/liveupdate.h>
+#include <linux/errno.h>
+
+#include "vfio_pci_priv.h"
+
+static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handler *handler,
+					     struct file *file)
+{
+	return false;
+}
+
+static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
+{
+	return -EOPNOTSUPP;
+}
+
+static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args)
+{
+}
+
+static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
+{
+	return -EOPNOTSUPP;
+}
+
+static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *args)
+{
+}
+
+static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
+	.can_preserve = vfio_pci_liveupdate_can_preserve,
+	.preserve = vfio_pci_liveupdate_preserve,
+	.unpreserve = vfio_pci_liveupdate_unpreserve,
+	.retrieve = vfio_pci_liveupdate_retrieve,
+	.finish = vfio_pci_liveupdate_finish,
+	.owner = THIS_MODULE,
+};
+
+static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
+	.ops = &vfio_pci_liveupdate_file_ops,
+	.compatible = VFIO_PCI_LUO_FH_COMPATIBLE,
+};
+
+int __init vfio_pci_liveupdate_init(void)
+{
+	if (!liveupdate_enabled())
+		return 0;
+
+	return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+}
+
+void vfio_pci_liveupdate_cleanup(void)
+{
+	if (!liveupdate_enabled())
+		return;
+
+	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
+}
drivers/vfio/pci/vfio_pci_priv.h +14 -0
index 27ac280f00b9..68966ec64e51 100644
@@ -133,4 +133,18 @@ static inline void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev,
  }
  #endif
 
+#ifdef CONFIG_LIVEUPDATE
+int __init vfio_pci_liveupdate_init(void);
+void vfio_pci_liveupdate_cleanup(void);
+#else
+static inline int vfio_pci_liveupdate_init(void)
+{
+	return 0;
+}
+
+static inline void vfio_pci_liveupdate_cleanup(void)
+{
+}
+#endif /* CONFIG_LIVEUPDATE */
+
  #endif
include/linux/kho/abi/vfio_pci.h new file +28 -0
new file mode 100644
index 000000000000..37a845eed972
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Copyright (c) 2025, Google LLC.
+ * Vipin Sharma <[email protected]>
+ * David Matlack <[email protected]>
+ */
+
+#ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
+#define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
+
+/**
+ * DOC: VFIO PCI Live Update ABI
+ *
+ * This header defines the ABI for preserving the state of a VFIO PCI device
+ * files across a kexec reboot using LUO.
+ *
+ * Device metadata is serialized into memory which is then handed to the next
+ * kernel via KHO.
+ *
+ * This interface is a contract. Any modification to any of the serialization
+ * structs defined here constitutes a breaking change. Such changes require
+ * incrementing the version number in the VFIO_PCI_LUO_FH_COMPATIBLE string.
+ */
+
+#define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1"
+
+#endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
Signature · 2 lines
Best Regards,
Yanjun.Zhu
author maintainer
#51 lore raw

PATCH v2 5/22 vfio/pci: Preserve vfio-pci device files across Live Update

In reply to David Matlack

From: Vipin Sharma <[email protected]>

Implement the live update file handler callbacks to preserve a vfio-pci device across a Live Update. Subsequent commits will enable userspace to then retrieve this file after the Live Update.

Live Update support is scoped only to cdev files (i.e. not VFIO_GROUP_GET_DEVICE_FD files).

State about each device is serialized into a new ABI struct vfio_pci_core_device_ser. The contents of this struct are preserved across the Live Update to the next kernel using a combination of Kexec-Handover (KHO) to preserve the page(s) holding the struct and the Live Update Orchestrator (LUO) to preserve the physical address of the struct.

For now the only contents of struct vfio_pci_core_device_ser the device's PCI segment number and BDF, so that the device can be uniquely identified after the Live Update.

Require that userspace disables interrupts on the device prior to freeze() so that the device does not send any interrupts until new interrupt handlers have been set up by the next kernel.

Reset the device and restore its state in the freeze() callback. This ensures the device can be received by the next kernel in a consistent state. Eventually this will be dropped and the device can be preserved across in a running state, but that requires further work in VFIO and the core PCI layer.

Note that LUO holds a reference to this file when it is preserved. So VFIO is guaranteed that vfio_df_device_last_close() will not be called on this device no matter what userspace does.

---
 drivers/vfio/pci/vfio_pci.c            |  2 +-
 drivers/vfio/pci/vfio_pci_liveupdate.c | 84 +++++++++++++++++++++++++-
 drivers/vfio/pci/vfio_pci_priv.h       |  2 +
 drivers/vfio/vfio.h                    | 13 ----
 drivers/vfio/vfio_main.c               | 10 +--
 include/linux/kho/abi/vfio_pci.h       | 15 +++++
 include/linux/vfio.h                   | 28 +++++++++
 7 files changed, 129 insertions(+), 25 deletions(-)
drivers/vfio/pci/vfio_pci.c +1 -1
index 19e88322af2c..0260afb9492d 100644
@@ -125,7 +125,7 @@ static int vfio_pci_open_device(struct vfio_device *core_vdev)
 	return 0;
 }
 
-static const struct vfio_device_ops vfio_pci_ops = {
+const struct vfio_device_ops vfio_pci_ops = {
 	.name		= "vfio-pci",
 	.init		= vfio_pci_core_init_dev,
 	.release	= vfio_pci_core_release_dev,
drivers/vfio/pci/vfio_pci_liveupdate.c +82 -2

113 lines not on this page · raw message

drivers/vfio/pci/vfio_pci_priv.h +2 -0
index 68966ec64e51..d3da79b7b03c 100644
@@ -11,6 +11,8 @@
 /* Cap maximum number of ioeventfds per device (arbitrary) */
 #define VFIO_PCI_IOEVENTFD_MAX		1000
 
+extern const struct vfio_device_ops vfio_pci_ops;
+
 struct vfio_pci_ioeventfd {
 	struct list_head	next;
 	struct vfio_pci_core_device	*vdev;
drivers/vfio/vfio.h +0 -13
index 50128da18bca..6b89edbbf174 100644
@@ -16,17 +16,6 @@ struct iommufd_ctx;
 struct iommu_group;
 struct vfio_container;
 
-struct vfio_device_file {
-	struct vfio_device *device;
-	struct vfio_group *group;
-
-	u8 access_granted;
-	u32 devid; /* only valid when iommufd is valid */
-	spinlock_t kvm_ref_lock; /* protect kvm field */
-	struct kvm *kvm;
-	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
-};
-
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
 int vfio_df_open(struct vfio_device_file *df);
@@ -34,8 +23,6 @@ void vfio_df_close(struct vfio_device_file *df);
 struct vfio_device_file *
 vfio_allocate_device_file(struct vfio_device *device);
 
-extern const struct file_operations vfio_device_fops;
-
 #ifdef CONFIG_VFIO_NOIOMMU
 extern bool vfio_noiommu __read_mostly;
 #else
drivers/vfio/vfio_main.c +1 -9
index f7df90c423b4..276f615f0c28 100644
@@ -1436,15 +1436,7 @@ const struct file_operations vfio_device_fops = {
 	.show_fdinfo	= vfio_device_show_fdinfo,
 #endif
 };
-
-static struct vfio_device *vfio_device_from_file(struct file *file)
-{
-	struct vfio_device_file *df = file->private_data;
-
-	if (file->f_op != &vfio_device_fops)
-		return NULL;
-	return df->device;
-}
+EXPORT_SYMBOL_GPL(vfio_device_fops);
 
 /**
  * vfio_file_is_valid - True if the file is valid vfio file
include/linux/kho/abi/vfio_pci.h +15 -0
index 37a845eed972..9bf58a2f3820 100644
@@ -9,6 +9,9 @@
 #ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
 #define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
 
+#include <linux/compiler.h>
+#include <linux/types.h>
+
 /**
  * DOC: VFIO PCI Live Update ABI
  *
@@ -25,4 +28,16 @@
 
 #define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1"
 
+/**
+ * struct vfio_pci_core_device_ser - Serialized state of a single VFIO PCI
+ * device.
+ *
+ * @bdf: The device's PCI bus, device, and function number.
+ * @domain: The device's PCI domain number (segment).
+ */
+struct vfio_pci_core_device_ser {
+	u16 bdf;
+	u16 domain;
+} __packed;
+
 #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
include/linux/vfio.h +28 -0
index e90859956514..9aa1587fea19 100644
@@ -81,6 +81,34 @@ struct vfio_device {
 #endif
 };
 
+struct vfio_device_file {
+	struct vfio_device *device;
+	struct vfio_group *group;
+
+	u8 access_granted;
+	u32 devid; /* only valid when iommufd is valid */
+	spinlock_t kvm_ref_lock; /* protect kvm field */
+	struct kvm *kvm;
+	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
+};
+
+extern const struct file_operations vfio_device_fops;
+
+static inline struct vfio_device_file *to_vfio_device_file(struct file *file)
+{
+	if (file->f_op != &vfio_device_fops)
+		return NULL;
+
+	return file->private_data;
+}
+
+static inline struct vfio_device *vfio_device_from_file(struct file *file)
+{
+	struct vfio_device_file *df = to_vfio_device_file(file);
+
+	return df ? df->device : NULL;
+}
+
 /**
  * struct vfio_device_ops - VFIO bus driver device callbacks
  *
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
Reviewed-by on v2 5/22
#52 lore raw

Re: [PATCH v2 05/22] vfio/pci: Preserve vfio-pci device files across Live Update

Quoted 317 lines · David Matlack

From: Vipin Sharma <[email protected]>

Implement the live update file handler callbacks to preserve a vfio-pci device across a Live Update. Subsequent commits will enable userspace to then retrieve this file after the Live Update.

Live Update support is scoped only to cdev files (i.e. not VFIO_GROUP_GET_DEVICE_FD files).

State about each device is serialized into a new ABI struct vfio_pci_core_device_ser. The contents of this struct are preserved across the Live Update to the next kernel using a combination of Kexec-Handover (KHO) to preserve the page(s) holding the struct and the Live Update Orchestrator (LUO) to preserve the physical address of the struct.

For now the only contents of struct vfio_pci_core_device_ser the device's PCI segment number and BDF, so that the device can be uniquely identified after the Live Update.

Require that userspace disables interrupts on the device prior to freeze() so that the device does not send any interrupts until new interrupt handlers have been set up by the next kernel.

Reset the device and restore its state in the freeze() callback. This ensures the device can be received by the next kernel in a consistent state. Eventually this will be dropped and the device can be preserved across in a running state, but that requires further work in VFIO and the core PCI layer.

Note that LUO holds a reference to this file when it is preserved. So VFIO is guaranteed that vfio_df_device_last_close() will not be called on this device no matter what userspace does.

---

drivers/vfio/pci/vfio_pci.c            |  2 +-
drivers/vfio/pci/vfio_pci_liveupdate.c | 84 +++++++++++++++++++++++++-
drivers/vfio/pci/vfio_pci_priv.h       |  2 +
drivers/vfio/vfio.h                    | 13 ----
drivers/vfio/vfio_main.c               | 10 +--
include/linux/kho/abi/vfio_pci.h       | 15 +++++
include/linux/vfio.h                   | 28 +++++++++
7 files changed, 129 insertions(+), 25 deletions(-)
drivers/vfio/pci/vfio_pci.c +1 -1
index 19e88322af2c..0260afb9492d 100644
@@ -125,7 +125,7 @@ static int vfio_pci_open_device(struct vfio_device *core_vdev)
 	return 0;
 }
 
-static const struct vfio_device_ops vfio_pci_ops = {
+const struct vfio_device_ops vfio_pci_ops = {
 	.name		= "vfio-pci",
 	.init		= vfio_pci_core_init_dev,
 	.release	= vfio_pci_core_release_dev,
drivers/vfio/pci/vfio_pci_liveupdate.c +82 -2
index b84e63c0357b..f01de98f1b75 100644
@@ -8,25 +8,104 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/kexec_handover.h>
 #include <linux/kho/abi/vfio_pci.h>
 #include <linux/liveupdate.h>
 #include <linux/errno.h>
+#include <linux/vfio.h>
 
 #include "vfio_pci_priv.h"
 
 static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handler *handler,
 					     struct file *file)
 {
-	return false;
+	struct vfio_device_file *df = to_vfio_device_file(file);
+
+	if (!df)
+		return false;
+
+	/* Live Update support is limited to cdev files. */
+	if (df->group)
+		return false;
+
+	return df->device->ops == &vfio_pci_ops;
 }
 
 static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 {
-	return -EOPNOTSUPP;
+	struct vfio_device *device = vfio_device_from_file(args->file);
+	struct vfio_pci_core_device_ser *ser;
+	struct vfio_pci_core_device *vdev;
+	struct pci_dev *pdev;
+
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	pdev = vdev->pdev;
+
+	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
+		return -EINVAL;
+
+	if (vfio_pci_is_intel_display(pdev))
+		return -EINVAL;
+
+	ser = kho_alloc_preserve(sizeof(*ser));
+	if (IS_ERR(ser))
+		return PTR_ERR(ser);
+
+	ser->bdf = pci_dev_id(pdev);
+	ser->domain = pci_domain_nr(pdev->bus);
+
+	args->serialized_data = virt_to_phys(ser);
+	return 0;
 }
 
 static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args)
 {
+	kho_unpreserve_free(phys_to_virt(args->serialized_data));
+}
+
+static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args)
+{
+	struct vfio_device *device = vfio_device_from_file(args->file);
+	struct vfio_pci_core_device *vdev;
+	struct pci_dev *pdev;
+	int ret;
+
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	pdev = vdev->pdev;
+
+	guard(mutex)(&device->dev_set->lock);
+
+	/*
+	 * Userspace must disable interrupts on the device prior to freeze so
+	 * that the device does not send any interrupts until new interrupt
+	 * handlers have been established by the next kernel.
+	 */
+	if (vdev->irq_type != VFIO_PCI_NUM_IRQS) {
+		pci_err(pdev, "Freeze failed! Interrupts are still enabled.\n");
+		return -EINVAL;
+	}
+
+	pci_dev_lock(pdev);
+
+	ret = pci_load_saved_state(pdev, vdev->pci_saved_state);
+	if (ret)
+		goto out;
+
+	/*
+	 * Reset the device and restore it back to its original state before
+	 * handing it to the next kernel.
+	 *
+	 * Eventually both of these should be dropped and the device should be
+	 * kept running with its current state across the Live Update.
+	 */
+	if (vdev->reset_works)
+		ret = __pci_reset_function_locked(pdev);
+
+	pci_restore_state(pdev);
+
+out:
+	pci_dev_unlock(pdev);
+	return ret;
 }
 
 static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
@@ -42,6 +121,7 @@ static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
 	.can_preserve = vfio_pci_liveupdate_can_preserve,
 	.preserve = vfio_pci_liveupdate_preserve,
 	.unpreserve = vfio_pci_liveupdate_unpreserve,
+	.freeze = vfio_pci_liveupdate_freeze,
 	.retrieve = vfio_pci_liveupdate_retrieve,
 	.finish = vfio_pci_liveupdate_finish,
 	.owner = THIS_MODULE,
drivers/vfio/pci/vfio_pci_priv.h +2 -0
index 68966ec64e51..d3da79b7b03c 100644
@@ -11,6 +11,8 @@
 /* Cap maximum number of ioeventfds per device (arbitrary) */
 #define VFIO_PCI_IOEVENTFD_MAX		1000
 
+extern const struct vfio_device_ops vfio_pci_ops;
+
 struct vfio_pci_ioeventfd {
 	struct list_head	next;
 	struct vfio_pci_core_device	*vdev;
drivers/vfio/vfio.h +0 -11
index 50128da18bca..6b89edbbf174 100644
@@ -16,17 +16,6 @@ struct iommufd_ctx;
 struct iommu_group;
 struct vfio_container;
 
-struct vfio_device_file {
-	struct vfio_device *device;
-	struct vfio_group *group;
-
-	u8 access_granted;
-	u32 devid; /* only valid when iommufd is valid */
-	spinlock_t kvm_ref_lock; /* protect kvm field */
-	struct kvm *kvm;
-	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
-};
-
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
 int vfio_df_open(struct vfio_device_file *df);
@@ -34,8 +23,6 @@ void vfio_df_close(struct vfio_device_file *df);

struct vfio_device_file * vfio_allocate_device_file(struct vfio_device *device);

drivers/vfio/vfio.h +0 -2
-extern const struct file_operations vfio_device_fops;
-

#ifdef CONFIG_VFIO_NOIOMMU
extern bool vfio_noiommu __read_mostly;
#else

drivers/vfio/vfio_main.c +1 -9
index f7df90c423b4..276f615f0c28 100644
@@ -1436,15 +1436,7 @@ const struct file_operations vfio_device_fops = {
 	.show_fdinfo	= vfio_device_show_fdinfo,
 #endif
 };
-
-static struct vfio_device *vfio_device_from_file(struct file *file)
-{
-	struct vfio_device_file *df = file->private_data;
-
-	if (file->f_op != &vfio_device_fops)
-		return NULL;
-	return df->device;
-}
+EXPORT_SYMBOL_GPL(vfio_device_fops);
 
 /**
 * vfio_file_is_valid - True if the file is valid vfio file
include/linux/kho/abi/vfio_pci.h +15 -0
index 37a845eed972..9bf58a2f3820 100644
@@ -9,6 +9,9 @@
 #ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
 #define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
 
+#include <linux/compiler.h>
+#include <linux/types.h>
+
 /**
 * DOC: VFIO PCI Live Update ABI
 *
@@ -25,4 +28,16 @@
 
 #define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1"
 
+/**
+ * struct vfio_pci_core_device_ser - Serialized state of a single VFIO PCI
+ * device.
+ *
+ * @bdf: The device's PCI bus, device, and function number.
+ * @domain: The device's PCI domain number (segment).
+ */
+struct vfio_pci_core_device_ser {
+	u16 bdf;
+	u16 domain;
+} __packed;
+
 #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
include/linux/vfio.h +28 -0
index e90859956514..9aa1587fea19 100644
@@ -81,6 +81,34 @@ struct vfio_device {
 #endif
 };
 
+struct vfio_device_file {
+	struct vfio_device *device;
+	struct vfio_group *group;
+
+	u8 access_granted;
+	u32 devid; /* only valid when iommufd is valid */
+	spinlock_t kvm_ref_lock; /* protect kvm field */
+	struct kvm *kvm;
+	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
+};
+
+extern const struct file_operations vfio_device_fops;
+
+static inline struct vfio_device_file *to_vfio_device_file(struct file *file)
+{
+	if (file->f_op != &vfio_device_fops)
+		return NULL;
+
+	return file->private_data;
+}
+
+static inline struct vfio_device *vfio_device_from_file(struct file *file)
+{
+	struct vfio_device_file *df = to_vfio_device_file(file);
+
+	return df ? df->device : NULL;
+}
+
 /**
 * struct vfio_device_ops - VFIO bus driver device callbacks
 *
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
#53 lore raw

Re: [PATCH v2 05/22] vfio/pci: Preserve vfio-pci device files across Live Update

In reply to David Matlack
Quoted 152 lines · David Matlack

From: Vipin Sharma <[email protected]>

Implement the live update file handler callbacks to preserve a vfio-pci device across a Live Update. Subsequent commits will enable userspace to then retrieve this file after the Live Update.

Live Update support is scoped only to cdev files (i.e. not VFIO_GROUP_GET_DEVICE_FD files).

State about each device is serialized into a new ABI struct vfio_pci_core_device_ser. The contents of this struct are preserved across the Live Update to the next kernel using a combination of Kexec-Handover (KHO) to preserve the page(s) holding the struct and the Live Update Orchestrator (LUO) to preserve the physical address of the struct.

For now the only contents of struct vfio_pci_core_device_ser the device's PCI segment number and BDF, so that the device can be uniquely identified after the Live Update.

Require that userspace disables interrupts on the device prior to freeze() so that the device does not send any interrupts until new interrupt handlers have been set up by the next kernel.

Reset the device and restore its state in the freeze() callback. This ensures the device can be received by the next kernel in a consistent state. Eventually this will be dropped and the device can be preserved across in a running state, but that requires further work in VFIO and the core PCI layer.

Note that LUO holds a reference to this file when it is preserved. So VFIO is guaranteed that vfio_df_device_last_close() will not be called on this device no matter what userspace does.

---
 drivers/vfio/pci/vfio_pci.c            |  2 +-
 drivers/vfio/pci/vfio_pci_liveupdate.c | 84 +++++++++++++++++++++++++-
 drivers/vfio/pci/vfio_pci_priv.h       |  2 +
 drivers/vfio/vfio.h                    | 13 ----
 drivers/vfio/vfio_main.c               | 10 +--
 include/linux/kho/abi/vfio_pci.h       | 15 +++++
 include/linux/vfio.h                   | 28 +++++++++
 7 files changed, 129 insertions(+), 25 deletions(-)
drivers/vfio/pci/vfio_pci.c +1 -1
index 19e88322af2c..0260afb9492d 100644
@@ -125,7 +125,7 @@ static int vfio_pci_open_device(struct vfio_device *core_vdev)
 	return 0;
 }
 
-static const struct vfio_device_ops vfio_pci_ops = {
+const struct vfio_device_ops vfio_pci_ops = {
 	.name		= "vfio-pci",
 	.init		= vfio_pci_core_init_dev,
 	.release	= vfio_pci_core_release_dev,
drivers/vfio/pci/vfio_pci_liveupdate.c +65 -2
index b84e63c0357b..f01de98f1b75 100644
@@ -8,25 +8,104 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/kexec_handover.h>
 #include <linux/kho/abi/vfio_pci.h>
 #include <linux/liveupdate.h>
 #include <linux/errno.h>
+#include <linux/vfio.h>
 
 #include "vfio_pci_priv.h"
 
 static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handler *handler,
 					     struct file *file)
 {
-	return false;
+	struct vfio_device_file *df = to_vfio_device_file(file);
+
+	if (!df)
+		return false;
+
+	/* Live Update support is limited to cdev files. */
+	if (df->group)
+		return false;
+
+	return df->device->ops == &vfio_pci_ops;
 }
 
 static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 {
-	return -EOPNOTSUPP;
+	struct vfio_device *device = vfio_device_from_file(args->file);
+	struct vfio_pci_core_device_ser *ser;
+	struct vfio_pci_core_device *vdev;
+	struct pci_dev *pdev;
+
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	pdev = vdev->pdev;
+
+	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
+		return -EINVAL;
+
+	if (vfio_pci_is_intel_display(pdev))
+		return -EINVAL;
+
+	ser = kho_alloc_preserve(sizeof(*ser));
+	if (IS_ERR(ser))
+		return PTR_ERR(ser);
+
+	ser->bdf = pci_dev_id(pdev);
+	ser->domain = pci_domain_nr(pdev->bus);
+
+	args->serialized_data = virt_to_phys(ser);
+	return 0;
 }
 
 static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args)
 {
+	kho_unpreserve_free(phys_to_virt(args->serialized_data));
+}
+
+static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args)
+{
+	struct vfio_device *device = vfio_device_from_file(args->file);
+	struct vfio_pci_core_device *vdev;
+	struct pci_dev *pdev;
+	int ret;
+
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	pdev = vdev->pdev;
+
+	guard(mutex)(&device->dev_set->lock);
+
+	/*
+	 * Userspace must disable interrupts on the device prior to freeze so
+	 * that the device does not send any interrupts until new interrupt
+	 * handlers have been established by the next kernel.
+	 */
+	if (vdev->irq_type != VFIO_PCI_NUM_IRQS) {
+		pci_err(pdev, "Freeze failed! Interrupts are still enabled.\n");
+		return -EINVAL;
+	}
+
+	pci_dev_lock(pdev);
+
+	ret = pci_load_saved_state(pdev, vdev->pci_saved_state);
+	if (ret)
+		goto out;

drivers/vfio/pci/vfio_pci_liveupdate.c line 102

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +10 -0
+
+	/*
+	 * Reset the device and restore it back to its original state before
+	 * handing it to the next kernel.
+	 *
+	 * Eventually both of these should be dropped and the device should be
+	 * kept running with its current state across the Live Update.
+	 */
+	if (vdev->reset_works)
+		ret = __pci_reset_function_locked(pdev);

I see the 'Eventually both of these should be dropped' comment, which acknowledges that a reset is a v1 crutch. However, I wanted to clarify the fallback strategy here.

If vdev->reset_works is false, we skip the reset but still jump into the new kernel. For devices that don't support FLR, are we comfortable jumping with the device potentially still hot?

Quoted 124 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +7 -0
+
+	pci_restore_state(pdev);
+
+out:
+	pci_dev_unlock(pdev);
+	return ret;
 }
 
 static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
@@ -42,6 +121,7 @@ static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
 	.can_preserve = vfio_pci_liveupdate_can_preserve,
 	.preserve = vfio_pci_liveupdate_preserve,
 	.unpreserve = vfio_pci_liveupdate_unpreserve,
+	.freeze = vfio_pci_liveupdate_freeze,
 	.retrieve = vfio_pci_liveupdate_retrieve,
 	.finish = vfio_pci_liveupdate_finish,
 	.owner = THIS_MODULE,
drivers/vfio/pci/vfio_pci_priv.h +2 -0
index 68966ec64e51..d3da79b7b03c 100644
@@ -11,6 +11,8 @@
 /* Cap maximum number of ioeventfds per device (arbitrary) */
 #define VFIO_PCI_IOEVENTFD_MAX		1000
 
+extern const struct vfio_device_ops vfio_pci_ops;
+
 struct vfio_pci_ioeventfd {
 	struct list_head	next;
 	struct vfio_pci_core_device	*vdev;
drivers/vfio/vfio.h +0 -13
index 50128da18bca..6b89edbbf174 100644
@@ -16,17 +16,6 @@ struct iommufd_ctx;
 struct iommu_group;
 struct vfio_container;
 
-struct vfio_device_file {
-	struct vfio_device *device;
-	struct vfio_group *group;
-
-	u8 access_granted;
-	u32 devid; /* only valid when iommufd is valid */
-	spinlock_t kvm_ref_lock; /* protect kvm field */
-	struct kvm *kvm;
-	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
-};
-
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
 int vfio_df_open(struct vfio_device_file *df);
@@ -34,8 +23,6 @@ void vfio_df_close(struct vfio_device_file *df);
 struct vfio_device_file *
 vfio_allocate_device_file(struct vfio_device *device);
 
-extern const struct file_operations vfio_device_fops;
-
 #ifdef CONFIG_VFIO_NOIOMMU
 extern bool vfio_noiommu __read_mostly;
 #else
drivers/vfio/vfio_main.c +1 -9
index f7df90c423b4..276f615f0c28 100644
@@ -1436,15 +1436,7 @@ const struct file_operations vfio_device_fops = {
 	.show_fdinfo	= vfio_device_show_fdinfo,
 #endif
 };
-
-static struct vfio_device *vfio_device_from_file(struct file *file)
-{
-	struct vfio_device_file *df = file->private_data;
-
-	if (file->f_op != &vfio_device_fops)
-		return NULL;
-	return df->device;
-}
+EXPORT_SYMBOL_GPL(vfio_device_fops);
 
 /**
  * vfio_file_is_valid - True if the file is valid vfio file
include/linux/kho/abi/vfio_pci.h +15 -0
index 37a845eed972..9bf58a2f3820 100644
@@ -9,6 +9,9 @@
 #ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
 #define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
 
+#include <linux/compiler.h>
+#include <linux/types.h>
+
 /**
  * DOC: VFIO PCI Live Update ABI
  *
@@ -25,4 +28,16 @@
 
 #define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1"
 
+/**
+ * struct vfio_pci_core_device_ser - Serialized state of a single VFIO PCI
+ * device.
+ *
+ * @bdf: The device's PCI bus, device, and function number.
+ * @domain: The device's PCI domain number (segment).
+ */
+struct vfio_pci_core_device_ser {
+	u16 bdf;
+	u16 domain;
+} __packed;
+
 #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
include/linux/vfio.h +3 -0
index e90859956514..9aa1587fea19 100644
@@ -81,6 +81,34 @@ struct vfio_device {
 #endif
 };
 
+struct vfio_device_file {
+	struct vfio_device *device;
+	struct vfio_group *group;

include/linux/vfio.h struct vfio_device { · line 96

Quoted 10 lines
include/linux/vfio.h +10 -0
+
+	u8 access_granted;
+	u32 devid; /* only valid when iommufd is valid */
+	spinlock_t kvm_ref_lock; /* protect kvm field */
+	struct kvm *kvm;
+	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
+};
+
+extern const struct file_operations vfio_device_fops;
+

There seem to be two extern declarations for vfio_device_fops in both vfio_pci_priv.h and include/linux/vfio.h. Could we consolidate these?

Quoted 5 lines
include/linux/vfio.h +5 -0
+static inline struct vfio_device_file *to_vfio_device_file(struct file *file)
+{
+	if (file->f_op != &vfio_device_fops)
+		return NULL;
+

include/linux/vfio.h struct vfio_device { · line 111

Quoted 10 lines
include/linux/vfio.h +10 -0
+	return file->private_data;
+}
+
+static inline struct vfio_device *vfio_device_from_file(struct file *file)
+{
+	struct vfio_device_file *df = to_vfio_device_file(file);
+
+	return df ? df->device : NULL;
+}
+

I'm a little uncomfortable with this part. Why is it necessary to expose the internal vfio_device_file structure to drivers? If this is only to support vfio_device_from_file(), could we not keep the structure private and just export the helper function instead?

Exposing internal state into the public API introduces some maintenance constraints for e.g. if vfio_main.c ever changes how it tracks file-to-device mappings or its internal security state (like access_granted), it now has to worry about breaking external drivers.

I believe we expose the struct just to power these static inline helper (mainly vfio_device_from_file) ? Instead, could we treat vfio_device_file as an opaque type in the public header (like struct iommu_group) and move the implementation of vfio_device_from_file() into vfio_main.c as an exported symbol? This gives drivers the vfio_device pointer they need without leaking the core's private internals. Maybe something like the following (untested):

drivers/vfio/vfio_main.c +12 -0
index f7df90c423b4..71e3dda53187 100644
@@ -1446,6 +1446,18 @@ static struct vfio_device *vfio_device_from_file(struct file *file)
        return df->device;
 }
 
+struct vfio_device *vfio_device_from_file(struct file *file)
+{
+       struct vfio_device_file *df;
+
+       if (file->f_op != &vfio_device_fops)
+               return NULL;
+
+       df = file->private_data;
+       return df->device;
+}
+EXPORT_SYMBOL_GPL(vfio_device_from_file);
+
 /**
  * vfio_file_is_valid - True if the file is valid vfio file
  * @file: VFIO group file or VFIO device file
include/linux/vfio.h +9 -0
index e90859956514..182f192c5641 100644
@@ -81,6 +81,15 @@ struct vfio_device {
 #endif
 };
 
+
+struct vfio_device_file;
+
+extern const struct file_operations vfio_device_fops;
+
+/* Public API for drivers */
+struct vfio_device *vfio_device_from_file(struct file *file);
+ [...]
+
 
Quoted 6 lines
include/linux/vfio.h +0 -0
 /**
  * struct vfio_device_ops - VFIO bus driver device callbacks
  *
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog

Thanks,
Praan

author maintainer
#54 lore raw

Re: [PATCH v2 05/22] vfio/pci: Preserve vfio-pci device files across Live Update

Quoted 12 lines · Pranjal Shrivastava

Review

Quoted 9 lines · David Matlack
(quoted diff) +9 -0
+     /*
+      * Reset the device and restore it back to its original state before
+      * handing it to the next kernel.
+      *
+      * Eventually both of these should be dropped and the device should be
+      * kept running with its current state across the Live Update.
+      */
+     if (vdev->reset_works)
+             ret = __pci_reset_function_locked(pdev);
Quoted 7 lines

I see the 'Eventually both of these should be dropped' comment, which acknowledges that a reset is a v1 crutch. However, I wanted to clarify the fallback strategy here.

If vdev->reset_works is false, we skip the reset but still jump into the new kernel. For devices that don't support FLR, are we comfortable jumping with the device potentially still hot?

That situation is already possible today. Simply use a VFIO device that does not support reset, close it, and then kexec into a new kernel. vfio_pci_core_disable() (which runs when the last reference to the VFIO device file is dropped) also skips the reset, and kexec does not reset devices.

Note that we still restore the state, which at least will reset the device's configuration back to a sane default, including disabling bus mastering.

Quoted 8 lines
Quoted 8 lines
include/linux/vfio.h +0 -0
index e90859956514..9aa1587fea19 100644
@@ -81,6 +81,34 @@ struct vfio_device {
 #endif
 };
 
Quoted 16 lines

include/linux/vfio.h struct vfio_device { · line 96

Quoted 13 lines
include/linux/vfio.h +13 -0
+struct vfio_device_file {
+     struct vfio_device *device;
+     struct vfio_group *group;
+
+     u8 access_granted;
+     u32 devid; /* only valid when iommufd is valid */
+     spinlock_t kvm_ref_lock; /* protect kvm field */
+     struct kvm *kvm;
+     struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
+};
+
+extern const struct file_operations vfio_device_fops;
+

There seem to be two extern declarations for vfio_device_fops in both vfio_pci_priv.h and include/linux/vfio.h. Could we consolidate these?

Sure I can clean those up.

Quoted 12 lines
Quoted 12 lines
include/linux/vfio.h +12 -0
+static inline struct vfio_device_file *to_vfio_device_file(struct file *file)
+{
+     if (file->f_op != &vfio_device_fops)
+             return NULL;
+
+     return file->private_data;
+}
+
+static inline struct vfio_device *vfio_device_from_file(struct file *file)
+{
+     struct vfio_device_file *df = to_vfio_device_file(file);
+
Quoted 8 lines

include/linux/vfio.h struct vfio_device { · line 111

Quoted 3 lines
include/linux/vfio.h +3 -0
+     return df ? df->device : NULL;
+}
+

I'm a little uncomfortable with this part. Why is it necessary to expose the internal vfio_device_file structure to drivers? If this is only to support vfio_device_from_file(), could we not keep the structure private and just export the helper function instead?

Yeah, no problem.

Quoted 11 lines

Exposing internal state into the public API introduces some maintenance constraints for e.g. if vfio_main.c ever changes how it tracks file-to-device mappings or its internal security state (like access_granted), it now has to worry about breaking external drivers.

I believe we expose the struct just to power these static inline helper (mainly vfio_device_from_file) ? Instead, could we treat vfio_device_file as an opaque type in the public header (like struct iommu_group) and move the implementation of vfio_device_from_file() into vfio_main.c as an exported symbol? This gives drivers the vfio_device pointer they need without leaking the core's private internals.

Good points.

maintainer
#55 lore raw

Re: [PATCH v2 05/22] vfio/pci: Preserve vfio-pci device files across Live Update

In reply to David Matlack
Quoted 81 lines · On Thu, 29 Jan 2026 21:24:52 +0000 David Matlack <[email protected]> wrote:

From: Vipin Sharma <[email protected]>

Implement the live update file handler callbacks to preserve a vfio-pci device across a Live Update. Subsequent commits will enable userspace to then retrieve this file after the Live Update.

Live Update support is scoped only to cdev files (i.e. not VFIO_GROUP_GET_DEVICE_FD files).

State about each device is serialized into a new ABI struct vfio_pci_core_device_ser. The contents of this struct are preserved across the Live Update to the next kernel using a combination of Kexec-Handover (KHO) to preserve the page(s) holding the struct and the Live Update Orchestrator (LUO) to preserve the physical address of the struct.

For now the only contents of struct vfio_pci_core_device_ser the device's PCI segment number and BDF, so that the device can be uniquely identified after the Live Update.

Require that userspace disables interrupts on the device prior to freeze() so that the device does not send any interrupts until new interrupt handlers have been set up by the next kernel.

Reset the device and restore its state in the freeze() callback. This ensures the device can be received by the next kernel in a consistent state. Eventually this will be dropped and the device can be preserved across in a running state, but that requires further work in VFIO and the core PCI layer.

Note that LUO holds a reference to this file when it is preserved. So VFIO is guaranteed that vfio_df_device_last_close() will not be called on this device no matter what userspace does.

---
 drivers/vfio/pci/vfio_pci.c            |  2 +-
 drivers/vfio/pci/vfio_pci_liveupdate.c | 84 +++++++++++++++++++++++++-
 drivers/vfio/pci/vfio_pci_priv.h       |  2 +
 drivers/vfio/vfio.h                    | 13 ----
 drivers/vfio/vfio_main.c               | 10 +--
 include/linux/kho/abi/vfio_pci.h       | 15 +++++
 include/linux/vfio.h                   | 28 +++++++++
 7 files changed, 129 insertions(+), 25 deletions(-)
drivers/vfio/pci/vfio_pci.c +1 -1
index 19e88322af2c..0260afb9492d 100644
@@ -125,7 +125,7 @@ static int vfio_pci_open_device(struct vfio_device *core_vdev)
 	return 0;
 }
 
-static const struct vfio_device_ops vfio_pci_ops = {
+const struct vfio_device_ops vfio_pci_ops = {
 	.name		= "vfio-pci",
 	.init		= vfio_pci_core_init_dev,
 	.release	= vfio_pci_core_release_dev,
drivers/vfio/pci/vfio_pci_liveupdate.c +3 -1
index b84e63c0357b..f01de98f1b75 100644
@@ -8,25 +8,104 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/kexec_handover.h>
 #include <linux/kho/abi/vfio_pci.h>
 #include <linux/liveupdate.h>
 #include <linux/errno.h>
+#include <linux/vfio.h>
 
 #include "vfio_pci_priv.h"
 
 static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handler *handler,
 					     struct file *file)
 {
-	return false;
+	struct vfio_device_file *df = to_vfio_device_file(file);

drivers/vfio/pci/vfio_pci_liveupdate.c line 32

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +9 -0
+
+	if (!df)
+		return false;
+
+	/* Live Update support is limited to cdev files. */
+	if (df->group)
+		return false;
+
+	return df->device->ops == &vfio_pci_ops;
 }

Why can't we use vfio_device_cdev_opened() here and avoid all the new exposure in public headers?

      struct vfio_device *device = vfio_device_from_file(file);                 
                                                                                
      if (!device)                                                              
          return false;                                                         
                                                                                
      if (!vfio_device_cdev_opened(device))
          return false;

      return device->ops == &vfio_pci_ops;
Quoted 7 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +3 -1
 static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 {
-	return -EOPNOTSUPP;
+	struct vfio_device *device = vfio_device_from_file(args->file);
+	struct vfio_pci_core_device_ser *ser;
+	struct vfio_pci_core_device *vdev;

drivers/vfio/pci/vfio_pci_liveupdate.c line 48

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +10 -0
+	struct pci_dev *pdev;
+
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	pdev = vdev->pdev;
+
+	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
+		return -EINVAL;
+
+	if (vfio_pci_is_intel_display(pdev))
+		return -EINVAL;

Some comments describing what's missing, if these are TODO or DONTCARE would be useful.

Quoted 30 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +26 -0
+
+	ser = kho_alloc_preserve(sizeof(*ser));
+	if (IS_ERR(ser))
+		return PTR_ERR(ser);
+
+	ser->bdf = pci_dev_id(pdev);
+	ser->domain = pci_domain_nr(pdev->bus);
+
+	args->serialized_data = virt_to_phys(ser);
+	return 0;
 }
 
 static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args)
 {
+	kho_unpreserve_free(phys_to_virt(args->serialized_data));
+}
+
+static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args)
+{
+	struct vfio_device *device = vfio_device_from_file(args->file);
+	struct vfio_pci_core_device *vdev;
+	struct pci_dev *pdev;
+	int ret;
+
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	pdev = vdev->pdev;
+
+	guard(mutex)(&device->dev_set->lock);
+
+	/*

drivers/vfio/pci/vfio_pci_liveupdate.c line 88

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +10 -0
+	 * Userspace must disable interrupts on the device prior to freeze so
+	 * that the device does not send any interrupts until new interrupt
+	 * handlers have been established by the next kernel.
+	 */
+	if (vdev->irq_type != VFIO_PCI_NUM_IRQS) {
+		pci_err(pdev, "Freeze failed! Interrupts are still enabled.\n");
+		return -EINVAL;
+	}
+
+	pci_dev_lock(pdev);

device_lock() is a dangerous source of deadlocks, for instance how can we know the freeze isn't occurring with an outstanding driver unbind?

Quoted 83 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +21 -0
+
+	ret = pci_load_saved_state(pdev, vdev->pci_saved_state);
+	if (ret)
+		goto out;
+
+	/*
+	 * Reset the device and restore it back to its original state before
+	 * handing it to the next kernel.
+	 *
+	 * Eventually both of these should be dropped and the device should be
+	 * kept running with its current state across the Live Update.
+	 */
+	if (vdev->reset_works)
+		ret = __pci_reset_function_locked(pdev);
+
+	pci_restore_state(pdev);
+
+out:
+	pci_dev_unlock(pdev);
+	return ret;
 }
 
 static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
@@ -42,6 +121,7 @@ static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
 	.can_preserve = vfio_pci_liveupdate_can_preserve,
 	.preserve = vfio_pci_liveupdate_preserve,
 	.unpreserve = vfio_pci_liveupdate_unpreserve,
+	.freeze = vfio_pci_liveupdate_freeze,
 	.retrieve = vfio_pci_liveupdate_retrieve,
 	.finish = vfio_pci_liveupdate_finish,
 	.owner = THIS_MODULE,
drivers/vfio/pci/vfio_pci_priv.h +2 -0
index 68966ec64e51..d3da79b7b03c 100644
@@ -11,6 +11,8 @@
 /* Cap maximum number of ioeventfds per device (arbitrary) */
 #define VFIO_PCI_IOEVENTFD_MAX		1000
 
+extern const struct vfio_device_ops vfio_pci_ops;
+
 struct vfio_pci_ioeventfd {
 	struct list_head	next;
 	struct vfio_pci_core_device	*vdev;
drivers/vfio/vfio.h +0 -13
index 50128da18bca..6b89edbbf174 100644
@@ -16,17 +16,6 @@ struct iommufd_ctx;
 struct iommu_group;
 struct vfio_container;
 
-struct vfio_device_file {
-	struct vfio_device *device;
-	struct vfio_group *group;
-
-	u8 access_granted;
-	u32 devid; /* only valid when iommufd is valid */
-	spinlock_t kvm_ref_lock; /* protect kvm field */
-	struct kvm *kvm;
-	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
-};
-
 void vfio_device_put_registration(struct vfio_device *device);
 bool vfio_device_try_get_registration(struct vfio_device *device);
 int vfio_df_open(struct vfio_device_file *df);
@@ -34,8 +23,6 @@ void vfio_df_close(struct vfio_device_file *df);
 struct vfio_device_file *
 vfio_allocate_device_file(struct vfio_device *device);
 
-extern const struct file_operations vfio_device_fops;
-
 #ifdef CONFIG_VFIO_NOIOMMU
 extern bool vfio_noiommu __read_mostly;
 #else
drivers/vfio/vfio_main.c +0 -0
index f7df90c423b4..276f615f0c28 100644
@@ -1436,15 +1436,7 @@ const struct file_operations vfio_device_fops = {
 	.show_fdinfo	= vfio_device_show_fdinfo,
 #endif
 };

drivers/vfio/vfio_main.c const struct file_operations vfio_device_fops = { · line 1439

Quoted 10 lines
drivers/vfio/vfio_main.c +1 -9
-
-static struct vfio_device *vfio_device_from_file(struct file *file)
-{
-	struct vfio_device_file *df = file->private_data;
-
-	if (file->f_op != &vfio_device_fops)
-		return NULL;
-	return df->device;
-}
+EXPORT_SYMBOL_GPL(vfio_device_fops);

Seems we just need to export vfio_device_from_file(). Thanks,

Alex

Quoted 73 lines
drivers/vfio/vfio_main.c +0 -0
 /**
  * vfio_file_is_valid - True if the file is valid vfio file
include/linux/kho/abi/vfio_pci.h +15 -0
index 37a845eed972..9bf58a2f3820 100644
@@ -9,6 +9,9 @@
 #ifndef _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
 #define _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H
 
+#include <linux/compiler.h>
+#include <linux/types.h>
+
 /**
  * DOC: VFIO PCI Live Update ABI
  *
@@ -25,4 +28,16 @@
 
 #define VFIO_PCI_LUO_FH_COMPATIBLE "vfio-pci-v1"
 
+/**
+ * struct vfio_pci_core_device_ser - Serialized state of a single VFIO PCI
+ * device.
+ *
+ * @bdf: The device's PCI bus, device, and function number.
+ * @domain: The device's PCI domain number (segment).
+ */
+struct vfio_pci_core_device_ser {
+	u16 bdf;
+	u16 domain;
+} __packed;
+
 #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
include/linux/vfio.h +28 -0
index e90859956514..9aa1587fea19 100644
@@ -81,6 +81,34 @@ struct vfio_device {
 #endif
 };
 
+struct vfio_device_file {
+	struct vfio_device *device;
+	struct vfio_group *group;
+
+	u8 access_granted;
+	u32 devid; /* only valid when iommufd is valid */
+	spinlock_t kvm_ref_lock; /* protect kvm field */
+	struct kvm *kvm;
+	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
+};
+
+extern const struct file_operations vfio_device_fops;
+
+static inline struct vfio_device_file *to_vfio_device_file(struct file *file)
+{
+	if (file->f_op != &vfio_device_fops)
+		return NULL;
+
+	return file->private_data;
+}
+
+static inline struct vfio_device *vfio_device_from_file(struct file *file)
+{
+	struct vfio_device_file *df = to_vfio_device_file(file);
+
+	return df ? df->device : NULL;
+}
+
 /**
  * struct vfio_device_ops - VFIO bus driver device callbacks
  *
author maintainer
#56 lore raw

Re: [PATCH v2 05/22] vfio/pci: Preserve vfio-pci device files across Live Update

Quoted 12 lines · Alex Williamson
Quoted 10 lines · On Thu, 29 Jan 2026 21:24:52 +0000 David Matlack <[email protected]> wrote:

static bool vfio_pci_liveupdate_can_preserve(struct liveupdate_file_handler *handler,

drivers/vfio/pci/vfio_pci_liveupdate.c +6 -1
 					     struct file *file)
 {
-	return false;
+	struct vfio_device_file *df = to_vfio_device_file(file);
+
+	if (!df)
+		return false;
+
+	/* Live Update support is limited to cdev files. */
Quoted 8 lines

drivers/vfio/pci/vfio_pci_liveupdate.c line 32

Quoted 5 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +4 -0
+	if (df->group)
+		return false;
+
+	return df->device->ops == &vfio_pci_ops;
 }

Why can't we use vfio_device_cdev_opened() here and avoid all the new exposure in public headers?

I thought I explored using vfio_device_cdev_opened() but I can't remember now why I went with df->group. Maybe there wasn't a good reason. I'll switch to vfio_device_cdev_opened() in the next version.

Quoted 12 lines
Quoted 12 lines

static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) {

drivers/vfio/pci/vfio_pci_liveupdate.c +8 -1
-	return -EOPNOTSUPP;
+	struct vfio_device *device = vfio_device_from_file(args->file);
+	struct vfio_pci_core_device_ser *ser;
+	struct vfio_pci_core_device *vdev;
+	struct pci_dev *pdev;
+
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	pdev = vdev->pdev;
+
Quoted 8 lines

drivers/vfio/pci/vfio_pci_liveupdate.c line 48

Quoted 5 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +5 -0
+	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
+		return -EINVAL;
+
+	if (vfio_pci_is_intel_display(pdev))
+		return -EINVAL;

Some comments describing what's missing, if these are TODO or DONTCARE would be useful.

Will do.

Quoted 18 lines
Quoted 18 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +18 -0
+static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args)
+{
+	struct vfio_device *device = vfio_device_from_file(args->file);
+	struct vfio_pci_core_device *vdev;
+	struct pci_dev *pdev;
+	int ret;
+
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	pdev = vdev->pdev;
+
+	guard(mutex)(&device->dev_set->lock);
+
+	/*
+	 * Userspace must disable interrupts on the device prior to freeze so
+	 * that the device does not send any interrupts until new interrupt
+	 * handlers have been established by the next kernel.
+	 */
+	if (vdev->irq_type != VFIO_PCI_NUM_IRQS) {
Quoted 8 lines

drivers/vfio/pci/vfio_pci_liveupdate.c line 88

Quoted 5 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +5 -0
+		pci_err(pdev, "Freeze failed! Interrupts are still enabled.\n");
+		return -EINVAL;
+	}
+
+	pci_dev_lock(pdev);

device_lock() is a dangerous source of deadlocks, for instance how can we know the freeze isn't occurring with an outstanding driver unbind?

I can change this to a try-lock and return an error if taking the lock fails. The freeze() callbacks are triggered by liveupdate_reboot() which is called from kernel_kexec(). So returning an error to userspace is possible.

My only concern is whether using try-lock would make kexec flaky, or if it would only fail if userspace is misbehavior (e.g. unbinding drivers while kexecing).

Quoted 11 lines

drivers/vfio/vfio_main.c const struct file_operations vfio_device_fops = { · line 1439

Quoted 9 lines
drivers/vfio/vfio_main.c +1 -8
-static struct vfio_device *vfio_device_from_file(struct file *file)
-{
-	struct vfio_device_file *df = file->private_data;
-
-	if (file->f_op != &vfio_device_fops)
-		return NULL;
-	return df->device;
-}
+EXPORT_SYMBOL_GPL(vfio_device_fops);

Seems we just need to export vfio_device_from_file(). Thanks,

Will do.

author maintainer
#57 lore raw

PATCH v2 6/22 vfio/pci: Retrieve preserved device files after Live Update

In reply to David Matlack

From: Vipin Sharma <[email protected]>

Enable userspace to retrieve preserved VFIO device files from VFIO after a Live Update by implementing the retrieve() and finish() file handler callbacks.

Use an anonymous inode when creating the file, since the retrieved device file is not opened through any particular cdev inode, and the cdev inode does not matter in practice.

For now the retrieved file is functionally equivalent a opening the corresponding VFIO cdev file. Subsequent commits will leverage the preserved state associated with the retrieved file to preserve bits of the device across Live Update.

---
 drivers/vfio/device_cdev.c             | 21 ++++++---
 drivers/vfio/pci/vfio_pci_liveupdate.c | 60 +++++++++++++++++++++++++-
 drivers/vfio/vfio_main.c               | 13 ++++++
 include/linux/vfio.h                   | 12 ++++++
 4 files changed, 98 insertions(+), 8 deletions(-)
drivers/vfio/device_cdev.c +14 -7
index 8ceca24ac136..935f84a35875 100644
@@ -16,14 +16,8 @@ void vfio_init_device_cdev(struct vfio_device *device)
 	device->cdev.owner = THIS_MODULE;
 }
 
-/*
- * device access via the fd opened by this function is blocked until
- * .open_device() is called successfully during BIND_IOMMUFD.
- */
-int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
+int __vfio_device_fops_cdev_open(struct vfio_device *device, struct file *filep)
 {
-	struct vfio_device *device = container_of(inode->i_cdev,
-						  struct vfio_device, cdev);
 	struct vfio_device_file *df;
 	int ret;
 
@@ -52,6 +46,19 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 	vfio_device_put_registration(device);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(__vfio_device_fops_cdev_open);
+
+/*
+ * device access via the fd opened by this function is blocked until
+ * .open_device() is called successfully during BIND_IOMMUFD.
+ */
+int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
+{
+	struct vfio_device *device = container_of(inode->i_cdev,
+						  struct vfio_device, cdev);
+
+	return __vfio_device_fops_cdev_open(device, filep);
+}
 
 static void vfio_df_get_kvm_safe(struct vfio_device_file *df)
 {
drivers/vfio/pci/vfio_pci_liveupdate.c +59 -1

84 lines not on this page · raw message

drivers/vfio/vfio_main.c +13 -0
index 276f615f0c28..89c5feef75d5 100644
@@ -13,6 +13,7 @@
 #include <linux/cdev.h>
 #include <linux/compat.h>
 #include <linux/device.h>
+#include <linux/device/class.h>
 #include <linux/fs.h>
 #include <linux/idr.h>
 #include <linux/iommu.h>
@@ -1758,6 +1759,18 @@ int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data,
 }
 EXPORT_SYMBOL(vfio_dma_rw);
 
+struct vfio_device *vfio_find_device(const void *data, device_match_t match)
+{
+	struct device *device;
+
+	device = class_find_device(vfio.device_class, NULL, data, match);
+	if (!device)
+		return NULL;
+
+	return container_of(device, struct vfio_device, device);
+}
+EXPORT_SYMBOL_GPL(vfio_find_device);
+
 /*
  * Module/class support
  */
include/linux/vfio.h +12 -0
index 9aa1587fea19..dc592dc00f89 100644
@@ -419,4 +419,16 @@ int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
 void vfio_virqfd_disable(struct virqfd **pvirqfd);
 void vfio_virqfd_flush_thread(struct virqfd **pvirqfd);
 
+#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)
+int __vfio_device_fops_cdev_open(struct vfio_device *device, struct file *filep);
+#else
+static inline int __vfio_device_fops_cdev_open(struct vfio_device *device,
+					       struct file *filep)
+{
+	return -EOPNOTSUPP;
+}
+#endif /* IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) */
+
+struct vfio_device *vfio_find_device(const void *data, device_match_t match);
+
 #endif /* VFIO_H */
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
Reviewed-by on v2 6/22
#58 lore raw

Re: [PATCH v2 06/22] vfio/pci: Retrieve preserved device files after Live Update

Quoted 211 lines · David Matlack

From: Vipin Sharma <[email protected]>

Enable userspace to retrieve preserved VFIO device files from VFIO after a Live Update by implementing the retrieve() and finish() file handler callbacks.

Use an anonymous inode when creating the file, since the retrieved device file is not opened through any particular cdev inode, and the cdev inode does not matter in practice.

For now the retrieved file is functionally equivalent a opening the corresponding VFIO cdev file. Subsequent commits will leverage the preserved state associated with the retrieved file to preserve bits of the device across Live Update.

---

drivers/vfio/device_cdev.c             | 21 ++++++---
drivers/vfio/pci/vfio_pci_liveupdate.c | 60 +++++++++++++++++++++++++-
drivers/vfio/vfio_main.c               | 13 ++++++
include/linux/vfio.h                   | 12 ++++++
4 files changed, 98 insertions(+), 8 deletions(-)
drivers/vfio/device_cdev.c +14 -7
index 8ceca24ac136..935f84a35875 100644
@@ -16,14 +16,8 @@ void vfio_init_device_cdev(struct vfio_device *device)
 	device->cdev.owner = THIS_MODULE;
 }
 
-/*
- * device access via the fd opened by this function is blocked until
- * .open_device() is called successfully during BIND_IOMMUFD.
- */
-int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
+int __vfio_device_fops_cdev_open(struct vfio_device *device, struct file *filep)
 {
-	struct vfio_device *device = container_of(inode->i_cdev,
-						  struct vfio_device, cdev);
 	struct vfio_device_file *df;
 	int ret;
 
@@ -52,6 +46,19 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 	vfio_device_put_registration(device);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(__vfio_device_fops_cdev_open);
+
+/*
+ * device access via the fd opened by this function is blocked until
+ * .open_device() is called successfully during BIND_IOMMUFD.
+ */
+int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
+{
+	struct vfio_device *device = container_of(inode->i_cdev,
+						  struct vfio_device, cdev);
+
+	return __vfio_device_fops_cdev_open(device, filep);
+}
 
 static void vfio_df_get_kvm_safe(struct vfio_device_file *df)
 {
drivers/vfio/pci/vfio_pci_liveupdate.c +59 -1
index f01de98f1b75..7f4117181fd0 100644
@@ -8,6 +8,8 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/anon_inodes.h>
+#include <linux/file.h>
 #include <linux/kexec_handover.h>
 #include <linux/kho/abi/vfio_pci.h>
 #include <linux/liveupdate.h>
@@ -108,13 +110,68 @@ static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args)
 	return ret;
 }
 
+static int match_device(struct device *dev, const void *arg)
+{
+	struct vfio_device *device = container_of(dev, struct vfio_device, device);
+	const struct vfio_pci_core_device_ser *ser = arg;
+	struct pci_dev *pdev;
+
+	pdev = dev_is_pci(device->dev) ? to_pci_dev(device->dev) : NULL;
+	if (!pdev)
+		return false;
+
+	return ser->bdf == pci_dev_id(pdev) && ser->domain == pci_domain_nr(pdev->bus);
+}
+
 static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
 {
-	return -EOPNOTSUPP;
+	struct vfio_pci_core_device_ser *ser;
+	struct vfio_device *device;
+	struct file *file;
+	int ret;
+
+	ser = phys_to_virt(args->serialized_data);
+
+	device = vfio_find_device(ser, match_device);
+	if (!device)
+		return -ENODEV;
+
+	/*
+	 * Simulate opening the character device using an anonymous inode. The
+	 * returned file has the same properties as a cdev file (e.g. operations
+	 * are blocked until BIND_IOMMUFD is called).
+	 */
+	file = anon_inode_getfile_fmode("[vfio-device-liveupdate]",
+					&vfio_device_fops, NULL,
+					O_RDWR, FMODE_PREAD | FMODE_PWRITE);
+	if (IS_ERR(file)) {
+		ret = PTR_ERR(file);
+		goto out;
+	}
+
+	ret = __vfio_device_fops_cdev_open(device, file);
+	if (ret) {
+		fput(file);
+		goto out;
+	}
+
+	args->file = file;
+
+out:
+	/* Drop the reference from vfio_find_device() */
+	put_device(&device->device);
+
+	return ret;
+}
+
+static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args *args)
+{
+	return args->retrieved;
 }
 
 static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *args)
 {
+	kho_restore_free(phys_to_virt(args->serialized_data));
 }
 
 static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
@@ -123,6 +180,7 @@ static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
 	.unpreserve = vfio_pci_liveupdate_unpreserve,
 	.freeze = vfio_pci_liveupdate_freeze,
 	.retrieve = vfio_pci_liveupdate_retrieve,
+	.can_finish = vfio_pci_liveupdate_can_finish,
 	.finish = vfio_pci_liveupdate_finish,
 	.owner = THIS_MODULE,
 };
drivers/vfio/vfio_main.c +13 -0
index 276f615f0c28..89c5feef75d5 100644
@@ -13,6 +13,7 @@
 #include <linux/cdev.h>
 #include <linux/compat.h>
 #include <linux/device.h>
+#include <linux/device/class.h>
 #include <linux/fs.h>
 #include <linux/idr.h>
 #include <linux/iommu.h>
@@ -1758,6 +1759,18 @@ int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data,
 }
 EXPORT_SYMBOL(vfio_dma_rw);
 
+struct vfio_device *vfio_find_device(const void *data, device_match_t match)
+{
+	struct device *device;
+
+	device = class_find_device(vfio.device_class, NULL, data, match);
+	if (!device)
+		return NULL;
+
+	return container_of(device, struct vfio_device, device);
+}
+EXPORT_SYMBOL_GPL(vfio_find_device);
+
 /*
 * Module/class support
 */
include/linux/vfio.h +12 -0
index 9aa1587fea19..dc592dc00f89 100644
@@ -419,4 +419,16 @@ int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
 void vfio_virqfd_disable(struct virqfd **pvirqfd);
 void vfio_virqfd_flush_thread(struct virqfd **pvirqfd);
 
+#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)
+int __vfio_device_fops_cdev_open(struct vfio_device *device, struct file *filep);
+#else
+static inline int __vfio_device_fops_cdev_open(struct vfio_device *device,
+					       struct file *filep)
+{
+	return -EOPNOTSUPP;
+}
+#endif /* IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) */
+
+struct vfio_device *vfio_find_device(const void *data, device_match_t match);
+
 #endif /* VFIO_H */
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
Reviewed-by on v2 6/22
#59 lore raw

Re: [PATCH v2 06/22] vfio/pci: Retrieve preserved device files after Live Update

In reply to David Matlack
Quoted 18 lines · David Matlack

From: Vipin Sharma <[email protected]>

Enable userspace to retrieve preserved VFIO device files from VFIO after a Live Update by implementing the retrieve() and finish() file handler callbacks.

Use an anonymous inode when creating the file, since the retrieved device file is not opened through any particular cdev inode, and the cdev inode does not matter in practice.

For now the retrieved file is functionally equivalent a opening the corresponding VFIO cdev file. Subsequent commits will leverage the preserved state associated with the retrieved file to preserve bits of the device across Live Update.

Quoted 7 lines
---
 drivers/vfio/device_cdev.c             | 21 ++++++---
 drivers/vfio/pci/vfio_pci_liveupdate.c | 60 +++++++++++++++++++++++++-
 drivers/vfio/vfio_main.c               | 13 ++++++
 include/linux/vfio.h                   | 12 ++++++
 4 files changed, 98 insertions(+), 8 deletions(-)

The use of anon_inode_getfile_fmode() is clever! The abstraction is clean as well.

Thanks!
Praan

Quoted 186 lines
drivers/vfio/device_cdev.c +14 -7
index 8ceca24ac136..935f84a35875 100644
@@ -16,14 +16,8 @@ void vfio_init_device_cdev(struct vfio_device *device)
 	device->cdev.owner = THIS_MODULE;
 }
 
-/*
- * device access via the fd opened by this function is blocked until
- * .open_device() is called successfully during BIND_IOMMUFD.
- */
-int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
+int __vfio_device_fops_cdev_open(struct vfio_device *device, struct file *filep)
 {
-	struct vfio_device *device = container_of(inode->i_cdev,
-						  struct vfio_device, cdev);
 	struct vfio_device_file *df;
 	int ret;
 
@@ -52,6 +46,19 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 	vfio_device_put_registration(device);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(__vfio_device_fops_cdev_open);
+
+/*
+ * device access via the fd opened by this function is blocked until
+ * .open_device() is called successfully during BIND_IOMMUFD.
+ */
+int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
+{
+	struct vfio_device *device = container_of(inode->i_cdev,
+						  struct vfio_device, cdev);
+
+	return __vfio_device_fops_cdev_open(device, filep);
+}
 
 static void vfio_df_get_kvm_safe(struct vfio_device_file *df)
 {
drivers/vfio/pci/vfio_pci_liveupdate.c +59 -1
index f01de98f1b75..7f4117181fd0 100644
@@ -8,6 +8,8 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/anon_inodes.h>
+#include <linux/file.h>
 #include <linux/kexec_handover.h>
 #include <linux/kho/abi/vfio_pci.h>
 #include <linux/liveupdate.h>
@@ -108,13 +110,68 @@ static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args)
 	return ret;
 }
 
+static int match_device(struct device *dev, const void *arg)
+{
+	struct vfio_device *device = container_of(dev, struct vfio_device, device);
+	const struct vfio_pci_core_device_ser *ser = arg;
+	struct pci_dev *pdev;
+
+	pdev = dev_is_pci(device->dev) ? to_pci_dev(device->dev) : NULL;
+	if (!pdev)
+		return false;
+
+	return ser->bdf == pci_dev_id(pdev) && ser->domain == pci_domain_nr(pdev->bus);
+}
+
 static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
 {
-	return -EOPNOTSUPP;
+	struct vfio_pci_core_device_ser *ser;
+	struct vfio_device *device;
+	struct file *file;
+	int ret;
+
+	ser = phys_to_virt(args->serialized_data);
+
+	device = vfio_find_device(ser, match_device);
+	if (!device)
+		return -ENODEV;
+
+	/*
+	 * Simulate opening the character device using an anonymous inode. The
+	 * returned file has the same properties as a cdev file (e.g. operations
+	 * are blocked until BIND_IOMMUFD is called).
+	 */
+	file = anon_inode_getfile_fmode("[vfio-device-liveupdate]",
+					&vfio_device_fops, NULL,
+					O_RDWR, FMODE_PREAD | FMODE_PWRITE);
+	if (IS_ERR(file)) {
+		ret = PTR_ERR(file);
+		goto out;
+	}
+
+	ret = __vfio_device_fops_cdev_open(device, file);
+	if (ret) {
+		fput(file);
+		goto out;
+	}
+
+	args->file = file;
+
+out:
+	/* Drop the reference from vfio_find_device() */
+	put_device(&device->device);
+
+	return ret;
+}
+
+static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args *args)
+{
+	return args->retrieved;
 }
 
 static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *args)
 {
+	kho_restore_free(phys_to_virt(args->serialized_data));
 }
 
 static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
@@ -123,6 +180,7 @@ static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
 	.unpreserve = vfio_pci_liveupdate_unpreserve,
 	.freeze = vfio_pci_liveupdate_freeze,
 	.retrieve = vfio_pci_liveupdate_retrieve,
+	.can_finish = vfio_pci_liveupdate_can_finish,
 	.finish = vfio_pci_liveupdate_finish,
 	.owner = THIS_MODULE,
 };
drivers/vfio/vfio_main.c +13 -0
index 276f615f0c28..89c5feef75d5 100644
@@ -13,6 +13,7 @@
 #include <linux/cdev.h>
 #include <linux/compat.h>
 #include <linux/device.h>
+#include <linux/device/class.h>
 #include <linux/fs.h>
 #include <linux/idr.h>
 #include <linux/iommu.h>
@@ -1758,6 +1759,18 @@ int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data,
 }
 EXPORT_SYMBOL(vfio_dma_rw);
 
+struct vfio_device *vfio_find_device(const void *data, device_match_t match)
+{
+	struct device *device;
+
+	device = class_find_device(vfio.device_class, NULL, data, match);
+	if (!device)
+		return NULL;
+
+	return container_of(device, struct vfio_device, device);
+}
+EXPORT_SYMBOL_GPL(vfio_find_device);
+
 /*
  * Module/class support
  */
include/linux/vfio.h +12 -0
index 9aa1587fea19..dc592dc00f89 100644
@@ -419,4 +419,16 @@ int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
 void vfio_virqfd_disable(struct virqfd **pvirqfd);
 void vfio_virqfd_flush_thread(struct virqfd **pvirqfd);
 
+#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)
+int __vfio_device_fops_cdev_open(struct vfio_device *device, struct file *filep);
+#else
+static inline int __vfio_device_fops_cdev_open(struct vfio_device *device,
+					       struct file *filep)
+{
+	return -EOPNOTSUPP;
+}
+#endif /* IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) */
+
+struct vfio_device *vfio_find_device(const void *data, device_match_t match);
+
 #endif /* VFIO_H */
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
maintainer
#60 lore raw

Re: [PATCH v2 06/22] vfio/pci: Retrieve preserved device files after Live Update

In reply to David Matlack
Quoted 46 lines · On Thu, 29 Jan 2026 21:24:53 +0000 David Matlack <[email protected]> wrote:

From: Vipin Sharma <[email protected]>

Enable userspace to retrieve preserved VFIO device files from VFIO after a Live Update by implementing the retrieve() and finish() file handler callbacks.

Use an anonymous inode when creating the file, since the retrieved device file is not opened through any particular cdev inode, and the cdev inode does not matter in practice.

For now the retrieved file is functionally equivalent a opening the corresponding VFIO cdev file. Subsequent commits will leverage the preserved state associated with the retrieved file to preserve bits of the device across Live Update.

---
 drivers/vfio/device_cdev.c             | 21 ++++++---
 drivers/vfio/pci/vfio_pci_liveupdate.c | 60 +++++++++++++++++++++++++-
 drivers/vfio/vfio_main.c               | 13 ++++++
 include/linux/vfio.h                   | 12 ++++++
 4 files changed, 98 insertions(+), 8 deletions(-)
drivers/vfio/device_cdev.c +1 -7
index 8ceca24ac136..935f84a35875 100644
@@ -16,14 +16,8 @@ void vfio_init_device_cdev(struct vfio_device *device)
 	device->cdev.owner = THIS_MODULE;
 }
 
-/*
- * device access via the fd opened by this function is blocked until
- * .open_device() is called successfully during BIND_IOMMUFD.
- */
-int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
+int __vfio_device_fops_cdev_open(struct vfio_device *device, struct file *filep)
 {
-	struct vfio_device *device = container_of(inode->i_cdev,
-						  struct vfio_device, cdev);
 	struct vfio_device_file *df;
 	int ret;
 

drivers/vfio/device_cdev.c int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep) · line 49

Quoted 4 lines
drivers/vfio/device_cdev.c +1 -0
@@ -52,6 +46,19 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 	vfio_device_put_registration(device);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(__vfio_device_fops_cdev_open);

I really dislike that we're exporting the underscore variant, which implies it's an internal function that the caller should understand the constraints, without outlining any constraints.

I'm not sure what a good alternative is. We can drop fops since this isn't called from file_operations. Maybe vfio_device_cdev_open_file().

Quoted 65 lines
drivers/vfio/device_cdev.c +12 -0
+
+/*
+ * device access via the fd opened by this function is blocked until
+ * .open_device() is called successfully during BIND_IOMMUFD.
+ */
+int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
+{
+	struct vfio_device *device = container_of(inode->i_cdev,
+						  struct vfio_device, cdev);
+
+	return __vfio_device_fops_cdev_open(device, filep);
+}
 
 static void vfio_df_get_kvm_safe(struct vfio_device_file *df)
 {
drivers/vfio/pci/vfio_pci_liveupdate.c +32 -1
index f01de98f1b75..7f4117181fd0 100644
@@ -8,6 +8,8 @@
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
+#include <linux/anon_inodes.h>
+#include <linux/file.h>
 #include <linux/kexec_handover.h>
 #include <linux/kho/abi/vfio_pci.h>
 #include <linux/liveupdate.h>
@@ -108,13 +110,68 @@ static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args)
 	return ret;
 }
 
+static int match_device(struct device *dev, const void *arg)
+{
+	struct vfio_device *device = container_of(dev, struct vfio_device, device);
+	const struct vfio_pci_core_device_ser *ser = arg;
+	struct pci_dev *pdev;
+
+	pdev = dev_is_pci(device->dev) ? to_pci_dev(device->dev) : NULL;
+	if (!pdev)
+		return false;
+
+	return ser->bdf == pci_dev_id(pdev) && ser->domain == pci_domain_nr(pdev->bus);
+}
+
 static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
 {
-	return -EOPNOTSUPP;
+	struct vfio_pci_core_device_ser *ser;
+	struct vfio_device *device;
+	struct file *file;
+	int ret;
+
+	ser = phys_to_virt(args->serialized_data);
+
+	device = vfio_find_device(ser, match_device);
+	if (!device)
+		return -ENODEV;
+
+	/*
+	 * Simulate opening the character device using an anonymous inode. The
+	 * returned file has the same properties as a cdev file (e.g. operations
+	 * are blocked until BIND_IOMMUFD is called).
+	 */
+	file = anon_inode_getfile_fmode("[vfio-device-liveupdate]",

drivers/vfio/pci/vfio_pci_liveupdate.c static int vfio_pci_liveupdate_freeze(struct liveupdate_file_op_args *args) · line 154

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +10 -0
+					&vfio_device_fops, NULL,
+					O_RDWR, FMODE_PREAD | FMODE_PWRITE);
+	if (IS_ERR(file)) {
+		ret = PTR_ERR(file);
+		goto out;
+	}
+
+	ret = __vfio_device_fops_cdev_open(device, file);
+	if (ret) {
+		fput(file);

Don't we end up calling vfio_device_fops.release with NULL file->private_data here with inevitable segfaults? Thanks,

Alex

Quoted 83 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +17 -0
+		goto out;
+	}
+
+	args->file = file;
+
+out:
+	/* Drop the reference from vfio_find_device() */
+	put_device(&device->device);
+
+	return ret;
+}
+
+static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args *args)
+{
+	return args->retrieved;
 }
 
 static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *args)
 {
+	kho_restore_free(phys_to_virt(args->serialized_data));
 }
 
 static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
@@ -123,6 +180,7 @@ static const struct liveupdate_file_ops vfio_pci_liveupdate_file_ops = {
 	.unpreserve = vfio_pci_liveupdate_unpreserve,
 	.freeze = vfio_pci_liveupdate_freeze,
 	.retrieve = vfio_pci_liveupdate_retrieve,
+	.can_finish = vfio_pci_liveupdate_can_finish,
 	.finish = vfio_pci_liveupdate_finish,
 	.owner = THIS_MODULE,
 };
drivers/vfio/vfio_main.c +13 -0
index 276f615f0c28..89c5feef75d5 100644
@@ -13,6 +13,7 @@
 #include <linux/cdev.h>
 #include <linux/compat.h>
 #include <linux/device.h>
+#include <linux/device/class.h>
 #include <linux/fs.h>
 #include <linux/idr.h>
 #include <linux/iommu.h>
@@ -1758,6 +1759,18 @@ int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data,
 }
 EXPORT_SYMBOL(vfio_dma_rw);
 
+struct vfio_device *vfio_find_device(const void *data, device_match_t match)
+{
+	struct device *device;
+
+	device = class_find_device(vfio.device_class, NULL, data, match);
+	if (!device)
+		return NULL;
+
+	return container_of(device, struct vfio_device, device);
+}
+EXPORT_SYMBOL_GPL(vfio_find_device);
+
 /*
  * Module/class support
  */
include/linux/vfio.h +12 -0
index 9aa1587fea19..dc592dc00f89 100644
@@ -419,4 +419,16 @@ int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
 void vfio_virqfd_disable(struct virqfd **pvirqfd);
 void vfio_virqfd_flush_thread(struct virqfd **pvirqfd);
 
+#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)
+int __vfio_device_fops_cdev_open(struct vfio_device *device, struct file *filep);
+#else
+static inline int __vfio_device_fops_cdev_open(struct vfio_device *device,
+					       struct file *filep)
+{
+	return -EOPNOTSUPP;
+}
+#endif /* IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV) */
+
+struct vfio_device *vfio_find_device(const void *data, device_match_t match);
+
 #endif /* VFIO_H */
author maintainer
#61 lore raw

Re: [PATCH v2 06/22] vfio/pci: Retrieve preserved device files after Live Update

Quoted 16 lines · Alex Williamson

drivers/vfio/device_cdev.c int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep) · line 49

Quoted 9 lines · On Thu, 29 Jan 2026 21:24:53 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/device_cdev.c +1 -0
index 8ceca24ac136..935f84a35875 100644
@@ -52,6 +46,19 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 	vfio_device_put_registration(device);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(__vfio_device_fops_cdev_open);

I really dislike that we're exporting the underscore variant, which implies it's an internal function that the caller should understand the constraints, without outlining any constraints.

Quoted 2 lines

I'm not sure what a good alternative is. We can drop fops since this isn't called from file_operations. Maybe vfio_device_cdev_open_file().

Ack. Due to the bug you pointed out below, I think the changes in this file will look fairly different in the next version. But no matter what I'll avoid exporting a underscore variant without outlining the constraints.

Quoted 11 lines
Quoted 11 lines
drivers/vfio/device_cdev.c +11 -0
+	/*
+	 * Simulate opening the character device using an anonymous inode. The
+	 * returned file has the same properties as a cdev file (e.g. operations
+	 * are blocked until BIND_IOMMUFD is called).
+	 */
+	file = anon_inode_getfile_fmode("[vfio-device-liveupdate]",
+					&vfio_device_fops, NULL,
+					O_RDWR, FMODE_PREAD | FMODE_PWRITE);
+	if (IS_ERR(file)) {
+		ret = PTR_ERR(file);
+		goto out;
Quoted 8 lines

drivers/vfio/device_cdev.c int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)

Quoted 5 lines
drivers/vfio/device_cdev.c +5 -0
+	}
+
+	ret = __vfio_device_fops_cdev_open(device, file);
+	if (ret) {
+		fput(file);

Don't we end up calling vfio_device_fops.release with NULL file->private_data here with inevitable segfaults? Thanks,

Yes indeed. In that case I think we need to call vfio_device_try_get_registration() and vfio_allocate_device_file() before anon_inode_getfile_fmode(). We could play games with file->fops to avoid it calling vfio_device_fops.release here instead, but that feels hacky.

maintainer
#62 lore raw

Re: [PATCH v2 06/22] vfio/pci: Retrieve preserved device files after Live Update

Quoted 17 lines · David Matlack
Quoted 16 lines · Alex Williamson

drivers/vfio/device_cdev.c int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep) · line 49

Quoted 9 lines · On Thu, 29 Jan 2026 21:24:53 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/device_cdev.c +1 -0
index 8ceca24ac136..935f84a35875 100644
@@ -52,6 +46,19 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 	vfio_device_put_registration(device);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(__vfio_device_fops_cdev_open);

I really dislike that we're exporting the underscore variant, which implies it's an internal function that the caller should understand the constraints, without outlining any constraints.

Quoted 7 lines
Quoted 2 lines

I'm not sure what a good alternative is. We can drop fops since this isn't called from file_operations. Maybe vfio_device_cdev_open_file().

Ack. Due to the bug you pointed out below, I think the changes in this file will look fairly different in the next version. But no matter what I'll avoid exporting a underscore variant without outlining the constraints.

I haven't yet had a chance to read v3 so maybe you already solved this problem. But I dealt with some similar problems for the memfd patches
[0] and the hugetlb patches [1]. What I did was to just use/add a internal header (mm/internal.h or mm/hugetlb_internal.h). This lets you share functions within your subsystem but avoid exporting everywhere else.

I see that there already is drivers/vfio/vfio.h so perhaps you can use that to avoid exporting these functions outside the subsystem?

[0] https://git.kernel.org/torvalds/c/ed6f45f81bf9
[1] https://lore.kernel.org/linux-mm/[email protected]/

[...]

Signature · 2 lines
Regards,
Pratyush Yadav
author maintainer
#63 lore raw

PATCH v2 7/22 vfio/pci: Notify PCI subsystem about devices preserved across Live Update

In reply to David Matlack

Notify the PCI subsystem about devices vfio-pci is preserving across Live Update by registering the vfio-pci liveupdate file handler with the PCI subsystem's FLB handler.

Notably this will ensure that devices preserved through vfio-pci will have their PCI bus numbers preserved across Live Update, allowing VFIO to use BDF as a key to identify the device across the Live Update and (in the future) allow the device to continue DMA operations across the Live Update.

This also enables VFIO to detect that a device was preserved before userspace first retrieves the file from it, which will be used in subsequent commits.

---
 drivers/vfio/pci/vfio_pci_liveupdate.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/vfio/pci/vfio_pci_liveupdate.c +24 -1
index 7f4117181fd0..ad915352303f 100644
@@ -53,6 +53,8 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 	if (IS_ERR(ser))
 		return PTR_ERR(ser);
 
+	pci_liveupdate_outgoing_preserve(pdev);
+
 	ser->bdf = pci_dev_id(pdev);
 	ser->domain = pci_domain_nr(pdev->bus);
 
@@ -62,6 +64,9 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 
 static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args)
 {
+	struct vfio_device *device = vfio_device_from_file(args->file);
+
+	pci_liveupdate_outgoing_unpreserve(to_pci_dev(device->dev));
 	kho_unpreserve_free(phys_to_virt(args->serialized_data));
 }
 
@@ -171,6 +176,9 @@ static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args *args)
 
 static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *args)
 {
+	struct vfio_device *device = vfio_device_from_file(args->file);
+
+	pci_liveupdate_incoming_finish(to_pci_dev(device->dev));
 	kho_restore_free(phys_to_virt(args->serialized_data));
 }
 
@@ -192,10 +200,24 @@ static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
 
 int __init vfio_pci_liveupdate_init(void)
 {
+	int ret;
+
 	if (!liveupdate_enabled())
 		return 0;
 
-	return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+	ret = liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+	if (ret)
+		return ret;
+
+	ret = pci_liveupdate_register_fh(&vfio_pci_liveupdate_fh);
+	if (ret)
+		goto error;
+
+	return 0;
+
+error:
+	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
+	return ret;
 }
 
 void vfio_pci_liveupdate_cleanup(void)
@@ -203,5 +225,6 @@ void vfio_pci_liveupdate_cleanup(void)
 	if (!liveupdate_enabled())
 		return;
 
+	WARN_ON_ONCE(pci_liveupdate_unregister_fh(&vfio_pci_liveupdate_fh));
 	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
 }
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
Reviewed-by on v2 7/22
#64 lore raw

Re: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update

Quoted 55 lines · David Matlack

Notify the PCI subsystem about devices vfio-pci is preserving across Live Update by registering the vfio-pci liveupdate file handler with the PCI subsystem's FLB handler.

Notably this will ensure that devices preserved through vfio-pci will have their PCI bus numbers preserved across Live Update, allowing VFIO to use BDF as a key to identify the device across the Live Update and (in the future) allow the device to continue DMA operations across the Live Update.

This also enables VFIO to detect that a device was preserved before userspace first retrieves the file from it, which will be used in subsequent commits.

---
 drivers/vfio/pci/vfio_pci_liveupdate.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/vfio/pci/vfio_pci_liveupdate.c +8 -0
index 7f4117181fd0..ad915352303f 100644
@@ -53,6 +53,8 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 	if (IS_ERR(ser))
 		return PTR_ERR(ser);
 
+	pci_liveupdate_outgoing_preserve(pdev);
+
 	ser->bdf = pci_dev_id(pdev);
 	ser->domain = pci_domain_nr(pdev->bus);
 
@@ -62,6 +64,9 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 
 static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args)
 {
+	struct vfio_device *device = vfio_device_from_file(args->file);
+
+	pci_liveupdate_outgoing_unpreserve(to_pci_dev(device->dev));
 	kho_unpreserve_free(phys_to_virt(args->serialized_data));
 }
 
@@ -171,6 +176,9 @@ static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args *args)
 
 static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *args)
 {
+	struct vfio_device *device = vfio_device_from_file(args->file);
+
+	pci_liveupdate_incoming_finish(to_pci_dev(device->dev));
 	kho_restore_free(phys_to_virt(args->serialized_data));
 }
 
@@ -192,10 +200,24 @@ static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
 
 int __init vfio_pci_liveupdate_init(void)

drivers/vfio/pci/vfio_pci_liveupdate.c static struct liveupdate_file_handler vfio_pci_liveupdate_fh = { · line 210

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +5 -1
 {
+	int ret;
+
 	if (!liveupdate_enabled())
 		return 0;
 
-	return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+	ret = liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+	if (ret)
+		return ret;

Nit: We might need to handle the retval here if we remove the liveupdate_enabled() check above (as discussed in patch 2).

Quoted 14 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +10 -0
+
+	ret = pci_liveupdate_register_fh(&vfio_pci_liveupdate_fh);
+	if (ret)
+		goto error;
+
+	return 0;
+
+error:
+	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
+	return ret;
 }
 
 void vfio_pci_liveupdate_cleanup(void)

drivers/vfio/pci/vfio_pci_liveupdate.c void vfio_pci_liveupdate_cleanup(void) · line 228

Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
@@ -203,5 +225,6 @@ void vfio_pci_liveupdate_cleanup(void)
 	if (!liveupdate_enabled())
 		return;
 
+	WARN_ON_ONCE(pci_liveupdate_unregister_fh(&vfio_pci_liveupdate_fh));

same here.

drivers/vfio/pci/vfio_pci_liveupdate.c void vfio_pci_liveupdate_cleanup(void) · line 230

Quoted 2 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +0 -0
 	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
 }

Thanks,
Praan

author maintainer
#65 lore raw

Re: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update

Quoted 9 lines · Pranjal Shrivastava
Quoted 7 lines · David Matlack

int __init vfio_pci_liveupdate_init(void)
{

drivers/vfio/pci/vfio_pci_liveupdate.c +2 -0
+     int ret;
+
      if (!liveupdate_enabled())
              return 0;
Quoted 7 lines

drivers/vfio/pci/vfio_pci_liveupdate.c static struct liveupdate_file_handler vfio_pci_liveupdate_fh = { · line 210

Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +3 -1
-     return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+     ret = liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+     if (ret)
+             return ret;

Nit: We might need to handle the retval here if we remove the liveupdate_enabled() check above (as discussed in patch 2).

I think you mean for the below call to pci_liveupdate_register_fh(), but yes agreed :).

maintainer
#66 lore raw

Re: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update

In reply to David Matlack
Quoted 24 lines · On Thu, 29 Jan 2026 21:24:54 +0000 David Matlack <[email protected]> wrote:

Notify the PCI subsystem about devices vfio-pci is preserving across Live Update by registering the vfio-pci liveupdate file handler with the PCI subsystem's FLB handler.

Notably this will ensure that devices preserved through vfio-pci will have their PCI bus numbers preserved across Live Update, allowing VFIO to use BDF as a key to identify the device across the Live Update and (in the future) allow the device to continue DMA operations across the Live Update.

This also enables VFIO to detect that a device was preserved before userspace first retrieves the file from it, which will be used in subsequent commits.

---
 drivers/vfio/pci/vfio_pci_liveupdate.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/vfio/pci/vfio_pci_liveupdate.c +0 -0
index 7f4117181fd0..ad915352303f 100644

drivers/vfio/pci/vfio_pci_liveupdate.c static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) · line 56

Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
@@ -53,6 +53,8 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 	if (IS_ERR(ser))
 		return PTR_ERR(ser);
 
+	pci_liveupdate_outgoing_preserve(pdev);

Why do we get to ignore the return value here?

Quoted 51 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +22 -1
+
 	ser->bdf = pci_dev_id(pdev);
 	ser->domain = pci_domain_nr(pdev->bus);
 
@@ -62,6 +64,9 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 
 static void vfio_pci_liveupdate_unpreserve(struct liveupdate_file_op_args *args)
 {
+	struct vfio_device *device = vfio_device_from_file(args->file);
+
+	pci_liveupdate_outgoing_unpreserve(to_pci_dev(device->dev));
 	kho_unpreserve_free(phys_to_virt(args->serialized_data));
 }
 
@@ -171,6 +176,9 @@ static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args *args)
 
 static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *args)
 {
+	struct vfio_device *device = vfio_device_from_file(args->file);
+
+	pci_liveupdate_incoming_finish(to_pci_dev(device->dev));
 	kho_restore_free(phys_to_virt(args->serialized_data));
 }
 
@@ -192,10 +200,24 @@ static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
 
 int __init vfio_pci_liveupdate_init(void)
 {
+	int ret;
+
 	if (!liveupdate_enabled())
 		return 0;
 
-	return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+	ret = liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+	if (ret)
+		return ret;
+
+	ret = pci_liveupdate_register_fh(&vfio_pci_liveupdate_fh);
+	if (ret)
+		goto error;
+
+	return 0;
+
+error:
+	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
+	return ret;
 }
 
 void vfio_pci_liveupdate_cleanup(void)

drivers/vfio/pci/vfio_pci_liveupdate.c void vfio_pci_liveupdate_cleanup(void) · line 228

Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
@@ -203,5 +225,6 @@ void vfio_pci_liveupdate_cleanup(void)
 	if (!liveupdate_enabled())
 		return;
 
+	WARN_ON_ONCE(pci_liveupdate_unregister_fh(&vfio_pci_liveupdate_fh));

This is propagation of a poor API choice in liveupdate, the unregister should return void, it shouldn't be allowed to fail, IMO. Thanks,

Alex

Quoted 2 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +0 -0
 	liveupdate_unregister_file_handler(&vfio_pci_liveupdate_fh);
 }
author maintainer
#67 lore raw

Re: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update

Quoted 10 lines · Alex Williamson
Quoted 8 lines · On Thu, 29 Jan 2026 21:24:54 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/vfio_pci_liveupdate.c +0 -0
index 7f4117181fd0..ad915352303f 100644
@@ -53,6 +53,8 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 	if (IS_ERR(ser))
 		return PTR_ERR(ser);
 
Quoted 3 lines

drivers/vfio/pci/vfio_pci_liveupdate.c static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args) · line 56

Quoted 1 line
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
+	pci_liveupdate_outgoing_preserve(pdev);

Why do we get to ignore the return value here?

Ugh, we do not. This is a bug, thanks for catching.

Quoted 8 lines

drivers/vfio/pci/vfio_pci_liveupdate.c void vfio_pci_liveupdate_cleanup(void) · line 228

Quoted 5 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
@@ -203,5 +225,6 @@ void vfio_pci_liveupdate_cleanup(void)
 	if (!liveupdate_enabled())
 		return;
 
+	WARN_ON_ONCE(pci_liveupdate_unregister_fh(&vfio_pci_liveupdate_fh));

This is propagation of a poor API choice in liveupdate, the unregister should return void, it shouldn't be allowed to fail, IMO. Thanks,

Ack. I'll make pci_liveupdate_unregister_fh() unable to fail and follow up with the maintainers of LUO about liveupdate_unregister_flb().

#68 lore raw

Re: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update

In reply to Alex Williamson
Quoted 8 lines · Alex Williamson

drivers/vfio/pci/vfio_pci_liveupdate.c void vfio_pci_liveupdate_cleanup(void) · line 228

Quoted 5 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
@@ -203,5 +225,6 @@ void vfio_pci_liveupdate_cleanup(void)
 	if (!liveupdate_enabled())
 		return;
 
+	WARN_ON_ONCE(pci_liveupdate_unregister_fh(&vfio_pci_liveupdate_fh));

This is propagation of a poor API choice in liveupdate, the unregister should return void, it shouldn't be allowed to fail, IMO. Thanks,

+1

"destroy" functions that fail are evil. :)

Jason

maintainer
#69 lore raw

Re: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update

Quoted 6 lines · Jason Gunthorpe
Quoted 4 lines · Alex Williamson
Quoted 4 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +0 -0
@@ -203,5 +225,6 @@ void vfio_pci_liveupdate_cleanup(void)
    if (!liveupdate_enabled())
            return;
 
Quoted 8 lines
Quoted 4 lines

drivers/vfio/pci/vfio_pci_liveupdate.c void vfio_pci_liveupdate_cleanup(void) · line 228

Quoted 1 line
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
+   WARN_ON_ONCE(pci_liveupdate_unregister_fh(&vfio_pci_liveupdate_fh));

This is propagation of a poor API choice in liveupdate, the unregister should return void, it shouldn't be allowed to fail, IMO. Thanks,

+1

"destroy" functions that fail are evil. :)

Generally, I agree that unregister, free, or destroy functions shouldn't return errors. However, this situation is tricky because the state is controlled by user space. The only relavent scenario where this function fails is:

"-EBUSY if the live update session is active and cannot be quiesced."

If there are active sessions (incoming or outgoing) with preserved data, how can we safely unregister while they are in-flight? It is a rare condition, but since the user can cause it, they need to be notified that unloading the module right now is not a good idea.

Pasha

Quoted 2 lines

Jason

#70 lore raw

Re: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update

Quoted 4 lines · Pasha Tatashin

If there are active sessions (incoming or outgoing) with preserved data, how can we safely unregister while they are in-flight? It is a rare condition, but since the user can cause it, they need to be notified that unloading the module right now is not a good idea.

IMHO blow up the kernel or something in the core code, you can't stop module unloading once it starts so it is pointless to propogate this

Jason

maintainer
#71 lore raw

Re: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update

Quoted 10 lines · Jason Gunthorpe
Quoted 4 lines · Pasha Tatashin

If there are active sessions (incoming or outgoing) with preserved data, how can we safely unregister while they are in-flight? It is a rare condition, but since the user can cause it, they need to be notified that unloading the module right now is not a good idea.

IMHO blow up the kernel or something in the core code, you can't stop module unloading once it starts so it is pointless to propogate this

I see, I will send a separate LUO patches to make unregister functions return void.

Pasha

Quoted 2 lines

Jason

#72 lore raw

Re: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update

In reply to David Matlack
Quoted 6 lines · David Matlack
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
@@ -192,10 +200,24 @@ static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
 
 int __init vfio_pci_liveupdate_init(void)
 {
+	int ret;

drivers/vfio/pci/vfio_pci_liveupdate.c static struct liveupdate_file_handler vfio_pci_liveupdate_fh = { · line 212

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +6 -1
+
 	if (!liveupdate_enabled())
 		return 0;
 
-	return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+	ret = liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+	if (ret)
+		return ret;
+
+	ret = pci_liveupdate_register_fh(&vfio_pci_liveupdate_fh);

May be we should rename pci_liveupdate_register_fh() to something like pci_register_to_liveupdate_flb(). In the current way, it is confusing to why same handler is getting registered at two places.

Above name will also make it similar to IOMMU series as well
https://lore.kernel.org/kvm/[email protected]/

author maintainer
#73 lore raw

Re: [PATCH v2 07/22] vfio/pci: Notify PCI subsystem about devices preserved across Live Update

Quoted 23 lines · Vipin Sharma
Quoted 6 lines · David Matlack
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
@@ -192,10 +200,24 @@ static struct liveupdate_file_handler vfio_pci_liveupdate_fh = {
 
 int __init vfio_pci_liveupdate_init(void)
 {
+     int ret;

drivers/vfio/pci/vfio_pci_liveupdate.c static struct liveupdate_file_handler vfio_pci_liveupdate_fh = { · line 212

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +6 -1
+
      if (!liveupdate_enabled())
              return 0;
 
-     return liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+     ret = liveupdate_register_file_handler(&vfio_pci_liveupdate_fh);
+     if (ret)
+             return ret;
+
+     ret = pci_liveupdate_register_fh(&vfio_pci_liveupdate_fh);

May be we should rename pci_liveupdate_register_fh() to something like pci_register_to_liveupdate_flb(). In the current way, it is confusing to why same handler is getting registered at two places.

Quoted 2 lines

Above name will also make it similar to IOMMU series as well
https://lore.kernel.org/kvm/[email protected]/

Will do.

author maintainer
#74 lore raw

PATCH v2 8/22 vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD

In reply to David Matlack

Enforce that files for incoming (preserved by previous kernel) VFIO devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD rather than by opening the corresponding VFIO character device or via VFIO_GROUP_GET_DEVICE_FD.

Both of these methods would result in VFIO initializing the device without access to the preserved state of the device passed by the previous kernel.

---
 drivers/vfio/device_cdev.c |  4 ++++
 drivers/vfio/group.c       |  9 +++++++++
 include/linux/vfio.h       | 18 ++++++++++++++++++
 3 files changed, 31 insertions(+)
drivers/vfio/device_cdev.c +4 -0
index 935f84a35875..355447e2add3 100644
@@ -57,6 +57,10 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 	struct vfio_device *device = container_of(inode->i_cdev,
 						  struct vfio_device, cdev);
 
+	/* Device file must be retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD */
+	if (vfio_liveupdate_incoming_is_preserved(device))
+		return -EBUSY;
+
 	return __vfio_device_fops_cdev_open(device, filep);
 }
 
drivers/vfio/group.c +9 -0
index d47ffada6912..63fc4d656215 100644
@@ -311,6 +311,15 @@ static int vfio_group_ioctl_get_device_fd(struct vfio_group *group,
 	if (IS_ERR(device))
 		return PTR_ERR(device);
 
+	/*
+	 * This device was preserved across a Live Update. Accessing it via
+	 * VFIO_GROUP_GET_DEVICE_FD is not allowed.
+	 */
+	if (vfio_liveupdate_incoming_is_preserved(device)) {
+		vfio_device_put_registration(device);
+		return -EBUSY;
+	}
+
 	fd = FD_ADD(O_CLOEXEC, vfio_device_open_file(device));
 	if (fd < 0)
 		vfio_device_put_registration(device);
include/linux/vfio.h +18 -0
index dc592dc00f89..0921847b18b5 100644
@@ -16,6 +16,7 @@
 #include <linux/cdev.h>
 #include <uapi/linux/vfio.h>
 #include <linux/iova_bitmap.h>
+#include <linux/pci.h>
 
 struct kvm;
 struct iommufd_ctx;
@@ -431,4 +432,21 @@ static inline int __vfio_device_fops_cdev_open(struct vfio_device *device,
 
 struct vfio_device *vfio_find_device(const void *data, device_match_t match);
 
+#ifdef CONFIG_LIVEUPDATE
+static inline bool vfio_liveupdate_incoming_is_preserved(struct vfio_device *device)
+{
+	struct device *d = device->dev;
+
+	if (dev_is_pci(d))
+		return to_pci_dev(d)->liveupdate_incoming;
+
+	return false;
+}
+#else
+static inline bool vfio_liveupdate_incoming_is_preserved(struct vfio_device *device)
+{
+	return false;
+}
+#endif
+
 #endif /* VFIO_H */
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
Reviewed-by on v2 8/22
#75 lore raw

Re: [PATCH v2 08/22] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD

Quoted 9 lines · David Matlack

Enforce that files for incoming (preserved by previous kernel) VFIO devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD rather than by opening the corresponding VFIO character device or via VFIO_GROUP_GET_DEVICE_FD.

Both of these methods would result in VFIO initializing the device without access to the preserved state of the device passed by the previous kernel.

Quoted 7 lines
---
 drivers/vfio/device_cdev.c |  4 ++++
 drivers/vfio/group.c       |  9 +++++++++
 include/linux/vfio.h       | 18 ++++++++++++++++++
 3 files changed, 31 insertions(+)

Thanks,
Praan

maintainer
#76 lore raw

Re: [PATCH v2 08/22] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD

In reply to David Matlack
Quoted 36 lines · On Thu, 29 Jan 2026 21:24:55 +0000 David Matlack <[email protected]> wrote:

Enforce that files for incoming (preserved by previous kernel) VFIO devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD rather than by opening the corresponding VFIO character device or via VFIO_GROUP_GET_DEVICE_FD.

Both of these methods would result in VFIO initializing the device without access to the preserved state of the device passed by the previous kernel.

---
 drivers/vfio/device_cdev.c |  4 ++++
 drivers/vfio/group.c       |  9 +++++++++
 include/linux/vfio.h       | 18 ++++++++++++++++++
 3 files changed, 31 insertions(+)
drivers/vfio/device_cdev.c +4 -0
index 935f84a35875..355447e2add3 100644
@@ -57,6 +57,10 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 	struct vfio_device *device = container_of(inode->i_cdev,
 						  struct vfio_device, cdev);
 
+	/* Device file must be retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD */
+	if (vfio_liveupdate_incoming_is_preserved(device))
+		return -EBUSY;
+
 	return __vfio_device_fops_cdev_open(device, filep);
 }
 
drivers/vfio/group.c +0 -0
index d47ffada6912..63fc4d656215 100644

drivers/vfio/group.c static int vfio_group_ioctl_get_device_fd(struct vfio_group *group, · line 320

Quoted 10 lines
drivers/vfio/group.c +7 -0
@@ -311,6 +311,15 @@ static int vfio_group_ioctl_get_device_fd(struct vfio_group *group,
 	if (IS_ERR(device))
 		return PTR_ERR(device);
 
+	/*
+	 * This device was preserved across a Live Update. Accessing it via
+	 * VFIO_GROUP_GET_DEVICE_FD is not allowed.
+	 */
+	if (vfio_liveupdate_incoming_is_preserved(device)) {
+		vfio_device_put_registration(device);
+		return -EBUSY;

Is this an EPERM issue then?

Quoted 27 lines
drivers/vfio/group.c +2 -0
+	}
+
 	fd = FD_ADD(O_CLOEXEC, vfio_device_open_file(device));
 	if (fd < 0)
 		vfio_device_put_registration(device);
include/linux/vfio.h +7 -0
index dc592dc00f89..0921847b18b5 100644
@@ -16,6 +16,7 @@
 #include <linux/cdev.h>
 #include <uapi/linux/vfio.h>
 #include <linux/iova_bitmap.h>
+#include <linux/pci.h>
 
 struct kvm;
 struct iommufd_ctx;
@@ -431,4 +432,21 @@ static inline int __vfio_device_fops_cdev_open(struct vfio_device *device,
 
 struct vfio_device *vfio_find_device(const void *data, device_match_t match);
 
+#ifdef CONFIG_LIVEUPDATE
+static inline bool vfio_liveupdate_incoming_is_preserved(struct vfio_device *device)
+{
+	struct device *d = device->dev;
+
+	if (dev_is_pci(d))

include/linux/vfio.h static inline int __vfio_device_fops_cdev_open(struct vfio_device *device, · line 450

Quoted 10 lines
include/linux/vfio.h +10 -0
+		return to_pci_dev(d)->liveupdate_incoming;
+
+	return false;
+}
+#else
+static inline bool vfio_liveupdate_incoming_is_preserved(struct vfio_device *device)
+{
+	return false;
+}
+#endif

Why does this need to be in the public header versus drivers/vfio/vfio.h?

author maintainer
#77 lore raw

Re: [PATCH v2 08/22] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD

Quoted 11 lines · Alex Williamson

drivers/vfio/group.c static int vfio_group_ioctl_get_device_fd(struct vfio_group *group, · line 320

Quoted 7 lines · On Thu, 29 Jan 2026 21:24:55 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/group.c +7 -0
+	/*
+	 * This device was preserved across a Live Update. Accessing it via
+	 * VFIO_GROUP_GET_DEVICE_FD is not allowed.
+	 */
+	if (vfio_liveupdate_incoming_is_preserved(device)) {
+		vfio_device_put_registration(device);
+		return -EBUSY;

Is this an EPERM issue then?

I was thinking EBUSY in the sense that the device is only temporarily inaccesible through this interface due it being in a preserved state as part of a Live Update. Once the preserved device file is retreived and closed, the device can be accessed again through VFIO_GROUP_GET_DEVICE_FD.

EPERM might lead to confusion that there is a filesystem permission issue?

Quoted 11 lines
Quoted 11 lines
include/linux/vfio.h +11 -0
+#ifdef CONFIG_LIVEUPDATE
+static inline bool vfio_liveupdate_incoming_is_preserved(struct vfio_device *device)
+{
+	struct device *d = device->dev;
+
+	if (dev_is_pci(d))
+		return to_pci_dev(d)->liveupdate_incoming;
+
+	return false;
+}
+#else
Quoted 8 lines

include/linux/vfio.h static inline int __vfio_device_fops_cdev_open(struct vfio_device *device, · line 450

Quoted 5 lines
include/linux/vfio.h +5 -0
+static inline bool vfio_liveupdate_incoming_is_preserved(struct vfio_device *device)
+{
+	return false;
+}
+#endif

Why does this need to be in the public header versus drivers/vfio/vfio.h?

No good reason. I'll make it private.

maintainer
#78 lore raw

Re: [PATCH v2 08/22] vfio: Enforce preserved devices are retrieved via LIVEUPDATE_SESSION_RETRIEVE_FD

Quoted 13 lines · On Thu, 26 Feb 2026 23:27:47 +0000 David Matlack <[email protected]> wrote:
Quoted 11 lines · Alex Williamson

drivers/vfio/group.c static int vfio_group_ioctl_get_device_fd(struct vfio_group *group, · line 320

Quoted 7 lines · On Thu, 29 Jan 2026 21:24:55 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/group.c +7 -0
+	/*
+	 * This device was preserved across a Live Update. Accessing it via
+	 * VFIO_GROUP_GET_DEVICE_FD is not allowed.
+	 */
+	if (vfio_liveupdate_incoming_is_preserved(device)) {
+		vfio_device_put_registration(device);
+		return -EBUSY;  

Is this an EPERM issue then?

Quoted 8 lines

I was thinking EBUSY in the sense that the device is only temporarily inaccesible through this interface due it being in a preserved state as part of a Live Update. Once the preserved device file is retreived and closed, the device can be accessed again through VFIO_GROUP_GET_DEVICE_FD.

EPERM might lead to confusion that there is a filesystem permission issue?

Ok, fair explanation. Thanks,

Alex

author maintainer
#79 lore raw

PATCH v2 9/22 vfio/pci: Store incoming Live Update state in struct vfio_pci_core_device

In reply to David Matlack

Stash a pointer to a device's incoming Live Updated state in struct vfio_pci_core_device. This will enable subsequent commits to use the preserved state when initializing the device.

To enable VFIO to safely access this pointer during device enablement, require that the device is fully enabled before returning true from can_finish(). This is synchronized by vfio_pci_core.c setting vdev->liveupdate_incoming_state to NULL under dev_set lock once it's done using it.

---
 drivers/vfio/pci/vfio_pci_core.c       |  2 +-
 drivers/vfio/pci/vfio_pci_liveupdate.c | 17 ++++++++++++++++-
 include/linux/vfio_pci_core.h          |  1 +
 3 files changed, 18 insertions(+), 2 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +1 -1
index 3a11e6f450f7..b01b94d81e28 100644
@@ -569,7 +569,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
 	if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
 		vdev->has_vga = true;
 
-
+	vdev->liveupdate_incoming_state = NULL;
 	return 0;
 
 out_free_zdev:
drivers/vfio/pci/vfio_pci_liveupdate.c +16 -1
index ad915352303f..1ad7379c70c4 100644
@@ -131,6 +131,7 @@ static int match_device(struct device *dev, const void *arg)
 static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
 {
 	struct vfio_pci_core_device_ser *ser;
+	struct vfio_pci_core_device *vdev;
 	struct vfio_device *device;
 	struct file *file;
 	int ret;
@@ -160,6 +161,9 @@ static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
 		goto out;
 	}
 
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	vdev->liveupdate_incoming_state = ser;
+
 	args->file = file;
 
 out:
@@ -171,7 +175,18 @@ static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
 
 static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args *args)
 {
-	return args->retrieved;
+	struct vfio_pci_core_device *vdev;
+	struct vfio_device *device;
+
+	if (!args->retrieved)
+		return false;
+
+	device = vfio_device_from_file(args->file);
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+
+	/* Check that vdev->liveupdate_incoming_state is no longer in use. */
+	guard(mutex)(&device->dev_set->lock);
+	return !vdev->liveupdate_incoming_state;
 }
 
 static void vfio_pci_liveupdate_finish(struct liveupdate_file_op_args *args)
include/linux/vfio_pci_core.h +1 -0
index 1ac86896875c..350c30f84a13 100644
@@ -143,6 +143,7 @@ struct vfio_pci_core_device {
 	struct notifier_block	nb;
 	struct rw_semaphore	memory_lock;
 	struct list_head	dmabufs;
+	struct vfio_pci_core_device_ser *liveupdate_incoming_state;
 };
 
 enum vfio_pci_io_width {
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
#80 lore raw

Re: [PATCH v2 09/22] vfio/pci: Store incoming Live Update state in struct vfio_pci_core_device

Quoted 59 lines · David Matlack

Stash a pointer to a device's incoming Live Updated state in struct vfio_pci_core_device. This will enable subsequent commits to use the preserved state when initializing the device.

To enable VFIO to safely access this pointer during device enablement, require that the device is fully enabled before returning true from can_finish(). This is synchronized by vfio_pci_core.c setting vdev->liveupdate_incoming_state to NULL under dev_set lock once it's done using it.

---
 drivers/vfio/pci/vfio_pci_core.c       |  2 +-
 drivers/vfio/pci/vfio_pci_liveupdate.c | 17 ++++++++++++++++-
 include/linux/vfio_pci_core.h          |  1 +
 3 files changed, 18 insertions(+), 2 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +1 -1
index 3a11e6f450f7..b01b94d81e28 100644
@@ -569,7 +569,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
 	if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
 		vdev->has_vga = true;
 
-
+	vdev->liveupdate_incoming_state = NULL;
 	return 0;
 
 out_free_zdev:
drivers/vfio/pci/vfio_pci_liveupdate.c +6 -1
index ad915352303f..1ad7379c70c4 100644
@@ -131,6 +131,7 @@ static int match_device(struct device *dev, const void *arg)
 static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
 {
 	struct vfio_pci_core_device_ser *ser;
+	struct vfio_pci_core_device *vdev;
 	struct vfio_device *device;
 	struct file *file;
 	int ret;
@@ -160,6 +161,9 @@ static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
 		goto out;
 	}
 
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+	vdev->liveupdate_incoming_state = ser;
+
 	args->file = file;
 
 out:
@@ -171,7 +175,18 @@ static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args)
 
 static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args *args)
 {
-	return args->retrieved;
+	struct vfio_pci_core_device *vdev;
+	struct vfio_device *device;

drivers/vfio/pci/vfio_pci_liveupdate.c static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args) · line 189

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +10 -0
+
+	if (!args->retrieved)
+		return false;
+
+	device = vfio_device_from_file(args->file);
+	vdev = container_of(device, struct vfio_pci_core_device, vdev);
+
+	/* Check that vdev->liveupdate_incoming_state is no longer in use. */
+	guard(mutex)(&device->dev_set->lock);
+	return !vdev->liveupdate_incoming_state;

Since we set this to NULL in the success path of vfio_pci_core_enable() I'm wondering if a failure in vfio_pci_core_enable could cause a resource leak? Because vfio_pci_liveupdate_can_finish() returns false as long as that pointer is valid, a single device failure will perpetually block the LIVEUPDATE_SESSION_FINISH IOCTL for the entire session preventing the LUO from reclaiming KHO memory.

Shall we also set vdev->liveupdate_incoming_state = NULL on the error paths of vfio_pci_core_enable() ?

Thanks,
Praan

author maintainer
#81 lore raw

Re: [PATCH v2 09/22] vfio/pci: Store incoming Live Update state in struct vfio_pci_core_device

Quoted 25 lines · Pranjal Shrivastava
Quoted 5 lines · David Matlack

static bool vfio_pci_liveupdate_can_finish(struct liveupdate_file_op_args *args) {

drivers/vfio/pci/vfio_pci_liveupdate.c +2 -1
-     return args->retrieved;
+     struct vfio_pci_core_device *vdev;
+     struct vfio_device *device;

drivers/vfio/pci/vfio_pci_liveupdate.c static int vfio_pci_liveupdate_retrieve(struct liveupdate_file_op_args *args) · line 189

Quoted 10 lines
drivers/vfio/pci/vfio_pci_liveupdate.c +10 -0
+
+     if (!args->retrieved)
+             return false;
+
+     device = vfio_device_from_file(args->file);
+     vdev = container_of(device, struct vfio_pci_core_device, vdev);
+
+     /* Check that vdev->liveupdate_incoming_state is no longer in use. */
+     guard(mutex)(&device->dev_set->lock);
+     return !vdev->liveupdate_incoming_state;

Since we set this to NULL in the success path of vfio_pci_core_enable() I'm wondering if a failure in vfio_pci_core_enable could cause a resource leak? Because vfio_pci_liveupdate_can_finish() returns false as long as that pointer is valid, a single device failure will perpetually block the LIVEUPDATE_SESSION_FINISH IOCTL for the entire session preventing the LUO from reclaiming KHO memory.

Quoted 2 lines

Shall we also set vdev->liveupdate_incoming_state = NULL on the error paths of vfio_pci_core_enable() ?

LIVEUPDATE_SESSION_FINISH will also perpetually fail if userspace never calls ioctl(VFIO_DEVICE_BIND_IOMMUFD) (which is what triggers vfio_pci_core_enable()). Or if that ioctl fails before it gets to vfio_pci_core_enable().

It's not a great situation to be in, but this is why can_finish() exists as a callback. Userspace must properly and correctly restore all of the state in the session before the session can be cleaned up. And the kernel is not going to handle every possible edge case (some files in a session are restored but some are not), at least not initially. If userspace gets stuck and cannot recover a resource then userspace will have to reboot the host to get back to a healthy state.

author maintainer
#82 lore raw

PATCH v2 10/22 vfio/pci: Skip reset of preserved device after Live Update

In reply to David Matlack

From: Vipin Sharma <[email protected]>

Do not reset the device when a Live Update preserved vfio-pci device is retrieved and first enabled. vfio_pci_liveupdate_freeze() guarantees the device is reset prior to Live Update, so there's no reason to reset it again after Live Update.

Since VFIO normally uses the initial reset to detect if the device supports function resets, pass that from the previous kernel via struct vfio_pci_core_dev_ser.

---
 drivers/vfio/pci/vfio_pci_core.c       | 22 +++++++++++++++++-----
 drivers/vfio/pci/vfio_pci_liveupdate.c |  1 +
 include/linux/kho/abi/vfio_pci.h       |  2 ++
 include/linux/vfio_pci_core.h          |  1 +
 4 files changed, 21 insertions(+), 5 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +17 -5
index b01b94d81e28..c9f73f597797 100644
@@ -515,12 +515,24 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
 	if (ret)
 		goto out_power;
 
-	/* If reset fails because of the device lock, fail this path entirely */
-	ret = pci_try_reset_function(pdev);
-	if (ret == -EAGAIN)
-		goto out_disable_device;
+	if (vdev->liveupdate_incoming_state) {
+		/*
+		 * This device was preserved by the previous kernel across a
+		 * Live Update, so it does not need to be reset.
+		 */
+		vdev->reset_works = vdev->liveupdate_incoming_state->reset_works;
+	} else {
+		/*
+		 * If reset fails because of the device lock, fail this path
+		 * entirely.
+		 */
+		ret = pci_try_reset_function(pdev);
+		if (ret == -EAGAIN)
+			goto out_disable_device;
+
+		vdev->reset_works = !ret;
+	}
 
-	vdev->reset_works = !ret;
 	pci_save_state(pdev);
 	vdev->pci_saved_state = pci_store_saved_state(pdev);
 	if (!vdev->pci_saved_state)
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
index 1ad7379c70c4..c52d6bdb455f 100644
@@ -57,6 +57,7 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 
 	ser->bdf = pci_dev_id(pdev);
 	ser->domain = pci_domain_nr(pdev->bus);
+	ser->reset_works = vdev->reset_works;
 
 	args->serialized_data = virt_to_phys(ser);
 	return 0;
include/linux/kho/abi/vfio_pci.h +2 -0
index 9bf58a2f3820..6c3d3c6dfc09 100644
@@ -34,10 +34,12 @@
  *
  * @bdf: The device's PCI bus, device, and function number.
  * @domain: The device's PCI domain number (segment).
+ * @reset_works: Non-zero if the device supports function resets.
  */
 struct vfio_pci_core_device_ser {
 	u16 bdf;
 	u16 domain;
+	u8 reset_works;
 } __packed;
 
 #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
include/linux/vfio_pci_core.h +1 -0
index 350c30f84a13..95835298e29e 100644
@@ -16,6 +16,7 @@
 #include <linux/types.h>
 #include <linux/uuid.h>
 #include <linux/notifier.h>
+#include <linux/kho/abi/vfio_pci.h>
 
 #ifndef VFIO_PCI_CORE_H
 #define VFIO_PCI_CORE_H
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
#83 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Hi David,

Quoted 40 lines · On Thu, 29 Jan 2026 21:24:57 +0000 David Matlack <[email protected]> wrote:

From: Vipin Sharma <[email protected]>

Do not reset the device when a Live Update preserved vfio-pci device is retrieved and first enabled. vfio_pci_liveupdate_freeze() guarantees the device is reset prior to Live Update, so there's no reason to reset it again after Live Update.

Since VFIO normally uses the initial reset to detect if the device supports function resets, pass that from the previous kernel via struct vfio_pci_core_dev_ser.

---
 drivers/vfio/pci/vfio_pci_core.c       | 22 +++++++++++++++++-----
 drivers/vfio/pci/vfio_pci_liveupdate.c |  1 +
 include/linux/kho/abi/vfio_pci.h       |  2 ++
 include/linux/vfio_pci_core.h          |  1 +
 4 files changed, 21 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/drivers/vfio/pci/vfio_pci_core.c index b01b94d81e28..c9f73f597797 100644 --- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c

drivers/vfio/pci/vfio_pci_core.c +0 -1
@@ -515,12 +515,24 @@ int vfio_pci_core_enable(struct
 vfio_pci_core_device *vdev) if (ret)
 		goto out_power;
 
-	/* If reset fails because of the device lock, fail this path

entirely */

drivers/vfio/pci/vfio_pci_core.c +3 -3
-	ret = pci_try_reset_function(pdev);
-	if (ret == -EAGAIN)
-		goto out_disable_device;
+	if (vdev->liveupdate_incoming_state) {
+		/*
+		 * This device was preserved by the previous kernel

across a

drivers/vfio/pci/vfio_pci_core.c +3 -0
+		 * Live Update, so it does not need to be reset.
+		 */
+		vdev->reset_works =
Quoted 1 line

vdev->liveupdate_incoming_state->reset_works;

Just wondering what happened to skipping the bus master clearing. I understand this version does not preserve the device itself yet; I’m just curious whether there were specific difficulties that led to dropping the earlier patch which skipped clearing bus master.

Quoted 58 lines
drivers/vfio/pci/vfio_pci_core.c +3 -0
+	} else {
+		/*
+		 * If reset fails because of the device lock, fail

this path

drivers/vfio/pci/vfio_pci_core.c +8 -1
+		 * entirely.
+		 */
+		ret = pci_try_reset_function(pdev);
+		if (ret == -EAGAIN)
+			goto out_disable_device;
+
+		vdev->reset_works = !ret;
+	}
 
-	vdev->reset_works = !ret;
 	pci_save_state(pdev);
 	vdev->pci_saved_state = pci_store_saved_state(pdev);
 	if (!vdev->pci_saved_state)
(quoted diff) +0 -0

b/drivers/vfio/pci/vfio_pci_liveupdate.c index 1ad7379c70c4..c52d6bdb455f 100644 --- a/drivers/vfio/pci/vfio_pci_liveupdate.c +++ b/drivers/vfio/pci/vfio_pci_liveupdate.c @@ -57,6 +57,7 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)

drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
 	ser->bdf = pci_dev_id(pdev);
 	ser->domain = pci_domain_nr(pdev->bus);
+	ser->reset_works = vdev->reset_works;
 
 	args->serialized_data = virt_to_phys(ser);
 	return 0;
(quoted diff) +0 -0

b/include/linux/kho/abi/vfio_pci.h index 9bf58a2f3820..6c3d3c6dfc09 100644 --- a/include/linux/kho/abi/vfio_pci.h
+++ b/include/linux/kho/abi/vfio_pci.h

include/linux/kho/abi/vfio_pci.h +2 -0
@@ -34,10 +34,12 @@
  *
  * @bdf: The device's PCI bus, device, and function number.
  * @domain: The device's PCI domain number (segment).
+ * @reset_works: Non-zero if the device supports function resets.
  */
 struct vfio_pci_core_device_ser {
 	u16 bdf;
 	u16 domain;
+	u8 reset_works;
 } __packed;
 
 #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
(quoted diff) +0 -0

b/include/linux/vfio_pci_core.h index 350c30f84a13..95835298e29e 100644 --- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h

include/linux/vfio_pci_core.h +1 -0
@@ -16,6 +16,7 @@
 #include <linux/types.h>
 #include <linux/uuid.h>
 #include <linux/notifier.h>
+#include <linux/kho/abi/vfio_pci.h>
 
 #ifndef VFIO_PCI_CORE_H
 #define VFIO_PCI_CORE_H
author maintainer
#84 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 20 lines · Jacob Pan
Quoted 18 lines · On Thu, 29 Jan 2026 21:24:57 +0000 David Matlack <[email protected]> wrote:
(quoted diff) +0 -0

b/drivers/vfio/pci/vfio_pci_core.c index b01b94d81e28..c9f73f597797 100644 --- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c

drivers/vfio/pci/vfio_pci_core.c +0 -1
@@ -515,12 +515,24 @@ int vfio_pci_core_enable(struct
 vfio_pci_core_device *vdev) if (ret)
 		goto out_power;
 
-	/* If reset fails because of the device lock, fail this path

entirely */

drivers/vfio/pci/vfio_pci_core.c +3 -3
-	ret = pci_try_reset_function(pdev);
-	if (ret == -EAGAIN)
-		goto out_disable_device;
+	if (vdev->liveupdate_incoming_state) {
+		/*
+		 * This device was preserved by the previous kernel

across a

drivers/vfio/pci/vfio_pci_core.c +1 -0
+		 * Live Update, so it does not need to be reset.
Quoted 8 lines
Quoted 3 lines
(quoted diff) +2 -0
+		 */
+		vdev->reset_works =

vdev->liveupdate_incoming_state->reset_works;

Just wondering what happened to skipping the bus master clearing. I understand this version does not preserve the device itself yet; I’m just curious whether there were specific difficulties that led to dropping the earlier patch which skipped clearing bus master.

Hi Jacob,

There's several places where bus master gets cleared that we need to eventually eliminate to fully preserve the device.

1. vfio_pci_liveupdate_freeze() clears it during shutdown when it

    restores vdev->pci_saved_state.

2. pci_device_shutdown() clears it during shutdown.
3. vfio_pci_core_enable() clears it when the preserved device file is bound to an iommufd after the Live Update (in vfio_pci_core_enable()).

I think it would be safe to skip (3) in this series, since that's very similar to how this series skips resets during vfio_pci_core_enable() for preserved devices.

But I don't think it would be safe to skip (1) or (2) until the attached iommufd is fully preserved.

If you are just asking about (3) then I agree it could be skipped and I can include that in the next version.

#85 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Hi David,

Quoted 47 lines · On Thu, 29 Jan 2026 22:33:13 +0000 David Matlack <[email protected]> wrote:
Quoted 21 lines · Jacob Pan
Quoted 18 lines · On Thu, 29 Jan 2026 21:24:57 +0000 David Matlack <[email protected]> wrote:
(quoted diff) +0 -0

b/drivers/vfio/pci/vfio_pci_core.c index b01b94d81e28..c9f73f597797 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -515,12 +515,24 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) if (ret)

drivers/vfio/pci/vfio_pci_core.c +0 -1
 		goto out_power;
 
-	/* If reset fails because of the device lock, fail this

path entirely */

drivers/vfio/pci/vfio_pci_core.c +3 -3
-	ret = pci_try_reset_function(pdev);
-	if (ret == -EAGAIN)
-		goto out_disable_device;
+	if (vdev->liveupdate_incoming_state) {
+		/*
+		 * This device was preserved by the previous

kernel across a

drivers/vfio/pci/vfio_pci_core.c +1 -0
+		 * Live Update, so it does not need to be reset.
Quoted 8 lines
Quoted 3 lines
(quoted diff) +2 -0
+		 */
+		vdev->reset_works =

vdev->liveupdate_incoming_state->reset_works;

Just wondering what happened to skipping the bus master clearing. I understand this version does not preserve the device itself yet; I’m just curious whether there were specific difficulties that led to dropping the earlier patch which skipped clearing bus master.

Hi Jacob,

There's several places where bus master gets cleared that we need to eventually eliminate to fully preserve the device.

1. vfio_pci_liveupdate_freeze() clears it during shutdown when it

    restores vdev->pci_saved_state.

2. pci_device_shutdown() clears it during shutdown.
3. vfio_pci_core_enable() clears it when the preserved device file is bound to an iommufd after the Live Update (in vfio_pci_core_enable()).

I think it would be safe to skip (3) in this series, since that's very similar to how this series skips resets during vfio_pci_core_enable() for preserved devices.

Quoted 5 lines

But I don't think it would be safe to skip (1) or (2) until the attached iommufd is fully preserved.

If you are just asking about (3) then I agree it could be skipped and I can include that in the next version.

I was just asking about (3) and trying to understand the asymmetric handling compared to reset. I don’t have a strong preference since this is temporary—thanks for the explanation.

I’ve been testing my noiommu cdev patches on top of yours, and so far they behave the same as with a real IOMMU. As you noted, however, final device preservation still depends on iommufd.

maintainer
#86 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

In reply to David Matlack
Quoted 39 lines · On Thu, 29 Jan 2026 21:24:57 +0000 David Matlack <[email protected]> wrote:

From: Vipin Sharma <[email protected]>

Do not reset the device when a Live Update preserved vfio-pci device is retrieved and first enabled. vfio_pci_liveupdate_freeze() guarantees the device is reset prior to Live Update, so there's no reason to reset it again after Live Update.

Since VFIO normally uses the initial reset to detect if the device supports function resets, pass that from the previous kernel via struct vfio_pci_core_dev_ser.

---
 drivers/vfio/pci/vfio_pci_core.c       | 22 +++++++++++++++++-----
 drivers/vfio/pci/vfio_pci_liveupdate.c |  1 +
 include/linux/kho/abi/vfio_pci.h       |  2 ++
 include/linux/vfio_pci_core.h          |  1 +
 4 files changed, 21 insertions(+), 5 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +7 -4
index b01b94d81e28..c9f73f597797 100644
@@ -515,12 +515,24 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
 	if (ret)
 		goto out_power;
 
-	/* If reset fails because of the device lock, fail this path entirely */
-	ret = pci_try_reset_function(pdev);
-	if (ret == -EAGAIN)
-		goto out_disable_device;
+	if (vdev->liveupdate_incoming_state) {
+		/*
+		 * This device was preserved by the previous kernel across a
+		 * Live Update, so it does not need to be reset.
+		 */
+		vdev->reset_works = vdev->liveupdate_incoming_state->reset_works;
+	} else {

drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 534

Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10 -0
+		/*
+		 * If reset fails because of the device lock, fail this path
+		 * entirely.
+		 */
+		ret = pci_try_reset_function(pdev);
+		if (ret == -EAGAIN)
+			goto out_disable_device;
+
+		vdev->reset_works = !ret;
+	}

This could maybe be incrementally cleaner in a
int vfio_pci_core_probe_reset(struct vfio_pci_core_device *vdev) helper.

drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 537

Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +0 -1
-	vdev->reset_works = !ret;
 	pci_save_state(pdev);
 	vdev->pci_saved_state = pci_store_saved_state(pdev);

Isn't this a problem too? In the first kernel we store the initial, post reset state of the device, now we're storing some arbitrary state. This is the state we're restore when the device is closed.

Quoted 35 lines
drivers/vfio/pci/vfio_pci_core.c +0 -0
 	if (!vdev->pci_saved_state)
drivers/vfio/pci/vfio_pci_liveupdate.c +1 -0
index 1ad7379c70c4..c52d6bdb455f 100644
@@ -57,6 +57,7 @@ static int vfio_pci_liveupdate_preserve(struct liveupdate_file_op_args *args)
 
 	ser->bdf = pci_dev_id(pdev);
 	ser->domain = pci_domain_nr(pdev->bus);
+	ser->reset_works = vdev->reset_works;
 
 	args->serialized_data = virt_to_phys(ser);
 	return 0;
include/linux/kho/abi/vfio_pci.h +2 -0
index 9bf58a2f3820..6c3d3c6dfc09 100644
@@ -34,10 +34,12 @@
  *
  * @bdf: The device's PCI bus, device, and function number.
  * @domain: The device's PCI domain number (segment).
+ * @reset_works: Non-zero if the device supports function resets.
  */
 struct vfio_pci_core_device_ser {
 	u16 bdf;
 	u16 domain;
+	u8 reset_works;
 } __packed;
 
 #endif /* _LINUX_LIVEUPDATE_ABI_VFIO_PCI_H */
include/linux/vfio_pci_core.h +0 -0
index 350c30f84a13..95835298e29e 100644

include/linux/vfio_pci_core.h line 22

Quoted 7 lines
include/linux/vfio_pci_core.h +1 -0
@@ -16,6 +16,7 @@
 #include <linux/types.h>
 #include <linux/uuid.h>
 #include <linux/notifier.h>
+#include <linux/kho/abi/vfio_pci.h>
 
 #ifndef VFIO_PCI_CORE_H
 #define VFIO_PCI_CORE_H

Wouldn't a forward declaration do, and the kho/abi include can be kept out of the public header? Also should be in the previous patch? Thanks,

Alex

author maintainer
#87 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 20 lines · Alex Williamson
Quoted 17 lines · On Thu, 29 Jan 2026 21:24:57 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/vfio_pci_core.c +13 -4
-	/* If reset fails because of the device lock, fail this path entirely */
-	ret = pci_try_reset_function(pdev);
-	if (ret == -EAGAIN)
-		goto out_disable_device;
+	if (vdev->liveupdate_incoming_state) {
+		/*
+		 * This device was preserved by the previous kernel across a
+		 * Live Update, so it does not need to be reset.
+		 */
+		vdev->reset_works = vdev->liveupdate_incoming_state->reset_works;
+	} else {
+		/*
+		 * If reset fails because of the device lock, fail this path
+		 * entirely.
+		 */
+		ret = pci_try_reset_function(pdev);
+		if (ret == -EAGAIN)
Quoted 8 lines

drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 534

Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +4 -0
+			goto out_disable_device;
+
+		vdev->reset_works = !ret;
+	}

This could maybe be incrementally cleaner in a
int vfio_pci_core_probe_reset(struct vfio_pci_core_device *vdev) helper.

Will do.

Quoted 8 lines

drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 537

Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +0 -1
-	vdev->reset_works = !ret;
 	pci_save_state(pdev);
 	vdev->pci_saved_state = pci_store_saved_state(pdev);

Isn't this a problem too? In the first kernel we store the initial, post reset state of the device, now we're storing some arbitrary state. This is the state we're restore when the device is closed.

The previous kernel resets the device and restores it back to its post reset state in vfio_pci_liveupdate_freeze() before handing off control to the next kernel. So my intention here is that VFIO will receive the device in that state, allowing it to call pci_store_saved_state() here to capture the post reset state of the device again.

Eventually we want to drop the reset in vfio_pci_liveupdate_freeze() and preserve vdev->pci_saved_state across the Live Update. But I was hoping to add that in a follow up series to avoid this one getting too long.

Quoted 10 lines
Quoted 10 lines
include/linux/vfio_pci_core.h +1 -0
index 350c30f84a13..95835298e29e 100644
@@ -16,6 +16,7 @@
 #include <linux/types.h>
 #include <linux/uuid.h>
 #include <linux/notifier.h>
+#include <linux/kho/abi/vfio_pci.h>
 
Quoted 5 lines

include/linux/vfio_pci_core.h line 22

Quoted 2 lines
include/linux/vfio_pci_core.h +0 -0
 #ifndef VFIO_PCI_CORE_H
 #define VFIO_PCI_CORE_H

Wouldn't a forward declaration do, and the kho/abi include can be kept out of the public header? Also should be in the previous patch?

Will do.

maintainer
#88 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 19 lines · On Fri, 27 Feb 2026 00:51:18 +0000 David Matlack <[email protected]> wrote:
Quoted 10 lines · Alex Williamson

drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 537

Quoted 4 lines · On Thu, 29 Jan 2026 21:24:57 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/vfio_pci_core.c +0 -1
-	vdev->reset_works = !ret;
 	pci_save_state(pdev);
 	vdev->pci_saved_state = pci_store_saved_state(pdev);  

Isn't this a problem too? In the first kernel we store the initial, post reset state of the device, now we're storing some arbitrary state. This is the state we're restore when the device is closed.

The previous kernel resets the device and restores it back to its post reset state in vfio_pci_liveupdate_freeze() before handing off control to the next kernel. So my intention here is that VFIO will receive the device in that state, allowing it to call pci_store_saved_state() here to capture the post reset state of the device again.

Quoted 3 lines

Eventually we want to drop the reset in vfio_pci_liveupdate_freeze() and preserve vdev->pci_saved_state across the Live Update. But I was hoping to add that in a follow up series to avoid this one getting too long.

I appreciate reviewing this in smaller chunks, but how does userspace know whether the kernel contains a stub implementation of liveupdate or behaves according to the end goal?

Also, didn't we violate our own contract in this patch by adding the reset_works field to the serialization data without updating the compatibility string? Thanks,

Alex

author maintainer
#89 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 23 lines · Alex Williamson
Quoted 19 lines · On Fri, 27 Feb 2026 00:51:18 +0000 David Matlack <[email protected]> wrote:
Quoted 10 lines · Alex Williamson
Quoted 4 lines · On Thu, 29 Jan 2026 21:24:57 +0000 David Matlack <[email protected]> wrote:

- vdev->reset_works = !ret; pci_save_state(pdev);
vdev->pci_saved_state = pci_store_saved_state(pdev);

Isn't this a problem too? In the first kernel we store the initial, post reset state of the device, now we're storing some arbitrary state. This is the state we're restore when the device is closed.

The previous kernel resets the device and restores it back to its post reset state in vfio_pci_liveupdate_freeze() before handing off control to the next kernel. So my intention here is that VFIO will receive the device in that state, allowing it to call pci_store_saved_state() here to capture the post reset state of the device again.

Quoted 7 lines
Quoted 3 lines

Eventually we want to drop the reset in vfio_pci_liveupdate_freeze() and preserve vdev->pci_saved_state across the Live Update. But I was hoping to add that in a follow up series to avoid this one getting too long.

I appreciate reviewing this in smaller chunks, but how does userspace know whether the kernel contains a stub implementation of liveupdate or behaves according to the end goal?

Would a new VFIO_DEVICE_INFO_CAP be a good way to communicate this information to userspace?

Quoted 3 lines

Also, didn't we violate our own contract in this patch by adding the reset_works field to the serialization data without updating the compatibility string? Thanks,

Yes, I will fix that in v3.

maintainer
#90 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 28 lines · On Fri, 27 Feb 2026 09:07:48 -0800 David Matlack <[email protected]> wrote:
Quoted 27 lines · Alex Williamson
Quoted 22 lines · On Fri, 27 Feb 2026 00:51:18 +0000 David Matlack <[email protected]> wrote:
Quoted 10 lines · Alex Williamson
Quoted 4 lines · On Thu, 29 Jan 2026 21:24:57 +0000 David Matlack <[email protected]> wrote:

- vdev->reset_works = !ret; pci_save_state(pdev);
vdev->pci_saved_state = pci_store_saved_state(pdev);

Isn't this a problem too? In the first kernel we store the initial, post reset state of the device, now we're storing some arbitrary state. This is the state we're restore when the device is closed.

The previous kernel resets the device and restores it back to its post reset state in vfio_pci_liveupdate_freeze() before handing off control to the next kernel. So my intention here is that VFIO will receive the device in that state, allowing it to call pci_store_saved_state() here to capture the post reset state of the device again.

Eventually we want to drop the reset in vfio_pci_liveupdate_freeze() and preserve vdev->pci_saved_state across the Live Update. But I was hoping to add that in a follow up series to avoid this one getting too long.

Quoted 6 lines
Quoted 3 lines

I appreciate reviewing this in smaller chunks, but how does userspace know whether the kernel contains a stub implementation of liveupdate or behaves according to the end goal?

Would a new VFIO_DEVICE_INFO_CAP be a good way to communicate this information to userspace?

Sorry if I don't have the whole model in my head yet, but is exposing the restriction to the vfio user of the device sufficient to manage the liveupdate orchestration? For example, a VFIO_DEVICE_INFO_CAP pushes the knowledge to QEMU... what does QEMU do with that knowledge? Who imposes the policy decision to decide what support is sufficient? Thanks,

Alex

author maintainer
#91 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 36 lines · Alex Williamson
Quoted 32 lines · On Fri, 27 Feb 2026 09:07:48 -0800 David Matlack <[email protected]> wrote:
Quoted 30 lines · Alex Williamson
Quoted 19 lines · On Fri, 27 Feb 2026 00:51:18 +0000 David Matlack <[email protected]> wrote:
Quoted 10 lines · Alex Williamson
Quoted 4 lines · On Thu, 29 Jan 2026 21:24:57 +0000 David Matlack <[email protected]> wrote:

- vdev->reset_works = !ret; pci_save_state(pdev);
vdev->pci_saved_state = pci_store_saved_state(pdev);

Isn't this a problem too? In the first kernel we store the initial, post reset state of the device, now we're storing some arbitrary state. This is the state we're restore when the device is closed.

The previous kernel resets the device and restores it back to its post reset state in vfio_pci_liveupdate_freeze() before handing off control to the next kernel. So my intention here is that VFIO will receive the device in that state, allowing it to call pci_store_saved_state() here to capture the post reset state of the device again.

Quoted 3 lines

Eventually we want to drop the reset in vfio_pci_liveupdate_freeze() and preserve vdev->pci_saved_state across the Live Update. But I was hoping to add that in a follow up series to avoid this one getting too long.

I appreciate reviewing this in smaller chunks, but how does userspace know whether the kernel contains a stub implementation of liveupdate or behaves according to the end goal?

Quoted 8 lines
Quoted 2 lines

Would a new VFIO_DEVICE_INFO_CAP be a good way to communicate this information to userspace?

Sorry if I don't have the whole model in my head yet, but is exposing the restriction to the vfio user of the device sufficient to manage the liveupdate orchestration? For example, a VFIO_DEVICE_INFO_CAP pushes the knowledge to QEMU... what does QEMU do with that knowledge? Who imposes the policy decision to decide what support is sufficient?

Hm.. good questions. I don't think we want userspace inspecting bits exposed by the kernel and trying to infer exactly what's being preserved and whether it's "good enough" to use. And such a UAPI would become tech debt once we finish development, I suspect.

A better approach would be to hide this support from userspace until we decide it is ready for production use-cases.

To enable development and testing, we can add an opt-in mechanism, such as CONFIG_EXPERIMENTAL or a kernel parameter. For example, adding something like this to vfio_pci_liveupdate_preserve():

if (!IS_ENABLED(CONFIG_EXPERIMENTAL)) {
pr_warn("vfio-pci file preservation requires

CONFIG_EXPERIMENTAL to enable!\n");
        return -EOPNOTSUPP;
}

Once we feel the support is ready, we can just submit a patch to delete those lines, and there will be no left-over UAPI.

author maintainer
#92 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 13 lines · David Matlack
Quoted 5 lines · Alex Williamson

Sorry if I don't have the whole model in my head yet, but is exposing the restriction to the vfio user of the device sufficient to manage the liveupdate orchestration? For example, a VFIO_DEVICE_INFO_CAP pushes the knowledge to QEMU... what does QEMU do with that knowledge? Who imposes the policy decision to decide what support is sufficient?

Hm.. good questions. I don't think we want userspace inspecting bits exposed by the kernel and trying to infer exactly what's being preserved and whether it's "good enough" to use. And such a UAPI would become tech debt once we finish development, I suspect.

Quoted 4 lines

A better approach would be to hide this support from userspace until we decide it is ready for production use-cases.

To enable development and testing, we can add an opt-in mechanism

Here is what I am trending towards sending in v3 as the opt-in mechanism:

drivers/vfio/pci/Kconfig +21 -0
index 1e82b44bda1a..770231554221 100644
@@ -58,6 +58,27 @@ config VFIO_PCI_ZDEV_KVM
 config VFIO_PCI_DMABUF
        def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER
 
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across a Live
+         Update. The eventual goal is that preserved devices can run
+         uninterrupted during a Live Update, including DMA to preserved
+         memory buffers and P2P. However there are many steps still needed to
+         achieve this, including:
+
+          - Preservation of iommufd files
+          - Preservation of IOMMU driver state
+          - Preservation of PCI state (BAR resources, device state, ...)
+          - Preservation of vfio-pci driver state
+
+         This option should only be enabled by developers working on
+         implementing this support. Once enough support has landed in the
+         kernel, this option will no longer be marked EXPERIMENTAL.
+
+         If you don't know what to do here, say N.
+
 source "drivers/vfio/pci/mlx5/Kconfig"
 
 source "drivers/vfio/pci/hisilicon/Kconfig"
#93 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Hi David,

Quoted 43 lines · On Thu, 12 Mar 2026 23:39:45 +0000 David Matlack <[email protected]> wrote:
Quoted 15 lines · David Matlack
Quoted 6 lines · Alex Williamson

Sorry if I don't have the whole model in my head yet, but is exposing the restriction to the vfio user of the device sufficient to manage the liveupdate orchestration? For example, a VFIO_DEVICE_INFO_CAP pushes the knowledge to QEMU... what does QEMU do with that knowledge? Who imposes the policy decision to decide what support is sufficient?

Hm.. good questions. I don't think we want userspace inspecting bits exposed by the kernel and trying to infer exactly what's being preserved and whether it's "good enough" to use. And such a UAPI would become tech debt once we finish development, I suspect.

Quoted 4 lines

A better approach would be to hide this support from userspace until we decide it is ready for production use-cases.

To enable development and testing, we can add an opt-in mechanism

Here is what I am trending towards sending in v3 as the opt-in mechanism:

drivers/vfio/pci/Kconfig +5 -0
index 1e82b44bda1a..770231554221 100644
@@ -58,6 +58,27 @@ config VFIO_PCI_ZDEV_KVM
 config VFIO_PCI_DMABUF
        def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER
 
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across a

Live

drivers/vfio/pci/Kconfig +2 -0
+         Update. The eventual goal is that preserved devices can run
+         uninterrupted during a Live Update, including DMA to

preserved
+ memory buffers and P2P. However there are many steps still
needed to

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 72

Quoted 4 lines
drivers/vfio/pci/Kconfig +4 -0
+         achieve this, including:
+
+          - Preservation of iommufd files
+          - Preservation of IOMMU driver state

In the interim, what do you think about moving forward with noiommu mode without preserving IOMMU driver state? I’ve tested your vfio_pci_liveupdate_kexec_test with the noiommu cdev, and it works as expected.

Quoted 14 lines

+ - Preservation of PCI state (BAR resources, device state,
...)

drivers/vfio/pci/Kconfig +4 -0
+          - Preservation of vfio-pci driver state
+
+         This option should only be enabled by developers working on
+         implementing this support. Once enough support has landed

in the

drivers/vfio/pci/Kconfig +4 -0
+         kernel, this option will no longer be marked EXPERIMENTAL.
+
+         If you don't know what to do here, say N.
+
 source "drivers/vfio/pci/mlx5/Kconfig"
 
 source "drivers/vfio/pci/hisilicon/Kconfig"
#94 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 13 lines · Jacob Pan

Hi David,

Quoted 11 lines
drivers/vfio/pci/Kconfig +5 -0
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across a

Live

(quoted diff) +2 -0
+         Update. The eventual goal is that preserved devices can run
+         uninterrupted during a Live Update, including DMA to

preserved
+ memory buffers and P2P. However there are many steps still
needed to

Quoted 9 lines

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 72

Quoted 4 lines
drivers/vfio/pci/Kconfig +4 -0
+         achieve this, including:
+
+          - Preservation of iommufd files
+          - Preservation of IOMMU driver state

In the interim, what do you think about moving forward with noiommu mode without preserving IOMMU driver state? I’ve tested your vfio_pci_liveupdate_kexec_test with the noiommu cdev, and it works as expected.

Just curious, are you using qemu VM without iommu device for noiommu work or do you have actual system without iommu hardware for testing?

#95 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Hi Vipin,

Quoted 25 lines · On Mon, 16 Mar 2026 09:28:32 -0700 Vipin Sharma <[email protected]> wrote:
Quoted 14 lines · Jacob Pan

Hi David,

Quoted 12 lines
drivers/vfio/pci/Kconfig +5 -0
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across

a Live
+ Update. The eventual goal is that preserved devices can
run
+ uninterrupted during a Live Update, including DMA to
preserved
+ memory buffers and P2P. However there are many steps
still needed to

Quoted 9 lines

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 72

Quoted 4 lines
drivers/vfio/pci/Kconfig +4 -0
+         achieve this, including:
+
+          - Preservation of iommufd files
+          - Preservation of IOMMU driver state  

In the interim, what do you think about moving forward with noiommu mode without preserving IOMMU driver state? I’ve tested your vfio_pci_liveupdate_kexec_test with the noiommu cdev, and it works as expected.

Quoted 2 lines

Just curious, are you using qemu VM without iommu device for noiommu work or do you have actual system without iommu hardware for testing?

I am using a qemu VM without iommu device and with an assigned nvme device. I use a low level nvme userspace driver (modified to use iommufd noiommu mode) for testing.
https://github.com/SamsungDS/libvfn

author maintainer
#96 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

In reply to Jacob Pan
Quoted 12 lines · Jacob Pan
Quoted 9 lines · On Thu, 12 Mar 2026 23:39:45 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/Kconfig +5 -0
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across a

Live

(quoted diff) +2 -0
+         Update. The eventual goal is that preserved devices can run
+         uninterrupted during a Live Update, including DMA to

preserved

Quoted 8 lines

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 72

Quoted 6 lines

+ memory buffers and P2P. However there are many steps still
needed to

drivers/vfio/pci/Kconfig +4 -0
+         achieve this, including:
+
+          - Preservation of iommufd files
+          - Preservation of IOMMU driver state

In the interim, what do you think about moving forward with noiommu mode without preserving IOMMU driver state?

I haven't gotten a chance to look at your noiommu patches yet, but I do think noiommu will probably be a good way to develop the VFIO and PCI support for Live Update without blocking on IOMMU driver support.

Quoted 3 lines

I’ve tested your
vfio_pci_liveupdate_kexec_test with the noiommu cdev, and it works as expected.

Would you be able to send patches to add noiommu support to VFIO selftests? That would be very helpful toward allowing us to develop and test Live Update support with noiommu.

#97 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Hi David,

Quoted 28 lines · On Mon, 16 Mar 2026 11:11:20 -0700 David Matlack <[email protected]> wrote:
Quoted 13 lines · Jacob Pan
Quoted 10 lines · On Thu, 12 Mar 2026 23:39:45 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/Kconfig +5 -0
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across

a Live
+ Update. The eventual goal is that preserved devices can
run
+ uninterrupted during a Live Update, including DMA to
preserved

Quoted 8 lines

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 72

Quoted 6 lines

+ memory buffers and P2P. However there are many steps
still needed to

drivers/vfio/pci/Kconfig +4 -0
+         achieve this, including:
+
+          - Preservation of iommufd files
+          - Preservation of IOMMU driver state  

In the interim, what do you think about moving forward with noiommu mode without preserving IOMMU driver state?

I haven't gotten a chance to look at your noiommu patches yet, but I do think noiommu will probably be a good way to develop the VFIO and PCI support for Live Update without blocking on IOMMU driver support.

Quoted 7 lines
Quoted 3 lines

I’ve tested your
vfio_pci_liveupdate_kexec_test with the noiommu cdev, and it works as expected.

Would you be able to send patches to add noiommu support to VFIO selftests? That would be very helpful toward allowing us to develop and test Live Update support with noiommu.

It is included in the noiommu patchset; the only difference is that the VFIO cdev name is prefixed with noiommu, which is not strictly required and is done out of caution.

/dev/vfio/
|-- devices
| `-- noiommu-vfio0

https://lore.kernel.org/linux-iommu/[email protected]/

author maintainer
#98 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 46 lines · Jacob Pan

Hi David,

Quoted 28 lines · On Mon, 16 Mar 2026 11:11:20 -0700 David Matlack <[email protected]> wrote:
Quoted 13 lines · Jacob Pan
Quoted 10 lines · On Thu, 12 Mar 2026 23:39:45 +0000 David Matlack <[email protected]> wrote:
drivers/vfio/pci/Kconfig +5 -0
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across

a Live
+ Update. The eventual goal is that preserved devices can
run
+ uninterrupted during a Live Update, including DMA to
preserved

Quoted 8 lines

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 72

Quoted 6 lines

+ memory buffers and P2P. However there are many steps
still needed to

drivers/vfio/pci/Kconfig +4 -0
+         achieve this, including:
+
+          - Preservation of iommufd files
+          - Preservation of IOMMU driver state

In the interim, what do you think about moving forward with noiommu mode without preserving IOMMU driver state?

I haven't gotten a chance to look at your noiommu patches yet, but I do think noiommu will probably be a good way to develop the VFIO and PCI support for Live Update without blocking on IOMMU driver support.

Quoted 7 lines
Quoted 3 lines

I’ve tested your
vfio_pci_liveupdate_kexec_test with the noiommu cdev, and it works as expected.

Would you be able to send patches to add noiommu support to VFIO selftests? That would be very helpful toward allowing us to develop and test Live Update support with noiommu.

It is included in the noiommu patchset; the only difference is that the VFIO cdev name is prefixed with noiommu, which is not strictly required and is done out of caution.

Quoted 5 lines

/dev/vfio/
|-- devices
| `-- noiommu-vfio0

https://lore.kernel.org/linux-iommu/[email protected]/

This one flew under my radar. I will take a look and respond there, thanks!

#99 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

In reply to David Matlack
Quoted 40 lines · David Matlack
Quoted 13 lines · David Matlack
Quoted 5 lines · Alex Williamson

Sorry if I don't have the whole model in my head yet, but is exposing the restriction to the vfio user of the device sufficient to manage the liveupdate orchestration? For example, a VFIO_DEVICE_INFO_CAP pushes the knowledge to QEMU... what does QEMU do with that knowledge? Who imposes the policy decision to decide what support is sufficient?

Hm.. good questions. I don't think we want userspace inspecting bits exposed by the kernel and trying to infer exactly what's being preserved and whether it's "good enough" to use. And such a UAPI would become tech debt once we finish development, I suspect.

Quoted 4 lines

A better approach would be to hide this support from userspace until we decide it is ready for production use-cases.

To enable development and testing, we can add an opt-in mechanism

Here is what I am trending towards sending in v3 as the opt-in mechanism:

drivers/vfio/pci/Kconfig +11 -0
index 1e82b44bda1a..770231554221 100644
@@ -58,6 +58,27 @@ config VFIO_PCI_ZDEV_KVM
 config VFIO_PCI_DMABUF
        def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER
 
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across a Live
+         Update. The eventual goal is that preserved devices can run
+         uninterrupted during a Live Update, including DMA to preserved
+         memory buffers and P2P. However there are many steps still needed to
+         achieve this, including:
+
+          - Preservation of iommufd files

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 81

Quoted 10 lines
drivers/vfio/pci/Kconfig +10 -0
+          - Preservation of IOMMU driver state
+          - Preservation of PCI state (BAR resources, device state, ...)
+          - Preservation of vfio-pci driver state
+
+         This option should only be enabled by developers working on
+         implementing this support. Once enough support has landed in the
+         kernel, this option will no longer be marked EXPERIMENTAL.
+
+         If you don't know what to do here, say N.
+

To use VFIO liveupdate, user has to do at least two things:
1. Enable CONFIG_LIVEUPDATE
2. Pass VFIO FD to a live update session.

This means someone using it has to know what live update is and intentionally pass the VFIO FDs. Isn't act of doing this itself an opt-in mechanism?

I am not sure providing VFIO_PCI_LIVEUPDATE alleviate Alex's concern about how userspace will know that sufficient VFIO support exists. May be write in liveupdate documentation (PATCH 11 of this series) that support is experimental?

author maintainer
#100 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 57 lines · Vipin Sharma
Quoted 40 lines · David Matlack
Quoted 13 lines · David Matlack
Quoted 5 lines · Alex Williamson

Sorry if I don't have the whole model in my head yet, but is exposing the restriction to the vfio user of the device sufficient to manage the liveupdate orchestration? For example, a VFIO_DEVICE_INFO_CAP pushes the knowledge to QEMU... what does QEMU do with that knowledge? Who imposes the policy decision to decide what support is sufficient?

Hm.. good questions. I don't think we want userspace inspecting bits exposed by the kernel and trying to infer exactly what's being preserved and whether it's "good enough" to use. And such a UAPI would become tech debt once we finish development, I suspect.

Quoted 4 lines

A better approach would be to hide this support from userspace until we decide it is ready for production use-cases.

To enable development and testing, we can add an opt-in mechanism

Here is what I am trending towards sending in v3 as the opt-in mechanism:

drivers/vfio/pci/Kconfig +11 -0
index 1e82b44bda1a..770231554221 100644
@@ -58,6 +58,27 @@ config VFIO_PCI_ZDEV_KVM
 config VFIO_PCI_DMABUF
        def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER
 
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across a Live
+         Update. The eventual goal is that preserved devices can run
+         uninterrupted during a Live Update, including DMA to preserved
+         memory buffers and P2P. However there are many steps still needed to
+         achieve this, including:
+
+          - Preservation of iommufd files

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 81

Quoted 10 lines
drivers/vfio/pci/Kconfig +10 -0
+          - Preservation of IOMMU driver state
+          - Preservation of PCI state (BAR resources, device state, ...)
+          - Preservation of vfio-pci driver state
+
+         This option should only be enabled by developers working on
+         implementing this support. Once enough support has landed in the
+         kernel, this option will no longer be marked EXPERIMENTAL.
+
+         If you don't know what to do here, say N.
+

To use VFIO liveupdate, user has to do at least two things:
1. Enable CONFIG_LIVEUPDATE
2. Pass VFIO FD to a live update session.

Quoted 3 lines

This means someone using it has to know what live update is and intentionally pass the VFIO FDs. Isn't act of doing this itself an opt-in mechanism?

If it is, then I can leave this out. Alex?

My thinking was: Distros are free to enable LIVEUPDATE and use it. The support it enables today is all fully functional (albeit new). vfio-cdev, OTOH, is not. A separate Kconfig can help express that difference.

Consider that LIVEUPDATE could be enabled by default in a future release, but vfio-cdev support might not be ready yet at that point.

Quoted 2 lines

I am not sure providing VFIO_PCI_LIVEUPDATE alleviate Alex's concern about how userspace will know that sufficient VFIO support exists.

I was thinking we can flip VFIO_PCI_LIVEUPDATE to be enabled by default (if LIVEUPDATE and VFIO_PCI are enabled), and drop "(EXPERIMENTAL)" from the option title. That would be how distros and downstream users of the kernel know that sufficient support exists to enable VFIO_PCI_LIVEUPDATE.

Quoted 2 lines

May be write in liveupdate documentation (PATCH 11 of this series) that support is experimental?

The documentation in patch 11 includes largely the same text that I put under VFIO_PCI_LIVEU"PDATE. But I can explicitly mention "experimental" as well if that's what you're asking.

#101 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 64 lines · David Matlack
Quoted 57 lines · Vipin Sharma
Quoted 40 lines · David Matlack
Quoted 13 lines · David Matlack
Quoted 5 lines · Alex Williamson

Sorry if I don't have the whole model in my head yet, but is exposing the restriction to the vfio user of the device sufficient to manage the liveupdate orchestration? For example, a VFIO_DEVICE_INFO_CAP pushes the knowledge to QEMU... what does QEMU do with that knowledge? Who imposes the policy decision to decide what support is sufficient?

Hm.. good questions. I don't think we want userspace inspecting bits exposed by the kernel and trying to infer exactly what's being preserved and whether it's "good enough" to use. And such a UAPI would become tech debt once we finish development, I suspect.

Quoted 4 lines

A better approach would be to hide this support from userspace until we decide it is ready for production use-cases.

To enable development and testing, we can add an opt-in mechanism

Here is what I am trending towards sending in v3 as the opt-in mechanism:

drivers/vfio/pci/Kconfig +11 -0
index 1e82b44bda1a..770231554221 100644
@@ -58,6 +58,27 @@ config VFIO_PCI_ZDEV_KVM
 config VFIO_PCI_DMABUF
        def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER
 
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across a Live
+         Update. The eventual goal is that preserved devices can run
+         uninterrupted during a Live Update, including DMA to preserved
+         memory buffers and P2P. However there are many steps still needed to
+         achieve this, including:
+
+          - Preservation of iommufd files

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 81

Quoted 10 lines
drivers/vfio/pci/Kconfig +10 -0
+          - Preservation of IOMMU driver state
+          - Preservation of PCI state (BAR resources, device state, ...)
+          - Preservation of vfio-pci driver state
+
+         This option should only be enabled by developers working on
+         implementing this support. Once enough support has landed in the
+         kernel, this option will no longer be marked EXPERIMENTAL.
+
+         If you don't know what to do here, say N.
+

To use VFIO liveupdate, user has to do at least two things:
1. Enable CONFIG_LIVEUPDATE
2. Pass VFIO FD to a live update session.

Quoted 3 lines

This means someone using it has to know what live update is and intentionally pass the VFIO FDs. Isn't act of doing this itself an opt-in mechanism?

If it is, then I can leave this out. Alex?

Quoted 7 lines

My thinking was: Distros are free to enable LIVEUPDATE and use it. The support it enables today is all fully functional (albeit new). vfio-cdev, OTOH, is not. A separate Kconfig can help express that difference.

Consider that LIVEUPDATE could be enabled by default in a future release, but vfio-cdev support might not be ready yet at that point.

But that also requires point 2 above i.e. userspace explicitly passing VFIO FD to liveupdate. Unless there is a capability mechanism like KVM then userspace cannot know what is exactly supported. Also, users who are using these APIs will already be advanced users and have to know many details about what liveupdate supports or not.

Quoted 10 lines
Quoted 3 lines

I am not sure providing VFIO_PCI_LIVEUPDATE alleviate Alex's concern about how userspace will know that sufficient VFIO support exists.

I was thinking we can flip VFIO_PCI_LIVEUPDATE to be enabled by default (if LIVEUPDATE and VFIO_PCI are enabled), and drop "(EXPERIMENTAL)" from the option title. That would be how distros and downstream users of the kernel know that sufficient support exists to enable VFIO_PCI_LIVEUPDATE.

Quoted 6 lines
Quoted 2 lines

May be write in liveupdate documentation (PATCH 11 of this series) that support is experimental?

The documentation in patch 11 includes largely the same text that I put under VFIO_PCI_LIVEU"PDATE. But I can explicitly mention "experimental" as well if that's what you're asking.

Yeah, even though documentation do get stale but I think there is no better way in this scenario.

author maintainer
#102 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 71 lines · Vipin Sharma
Quoted 69 lines · David Matlack
Quoted 57 lines · Vipin Sharma
Quoted 40 lines · David Matlack
Quoted 13 lines · David Matlack
Quoted 5 lines · Alex Williamson

Sorry if I don't have the whole model in my head yet, but is exposing the restriction to the vfio user of the device sufficient to manage the liveupdate orchestration? For example, a VFIO_DEVICE_INFO_CAP pushes the knowledge to QEMU... what does QEMU do with that knowledge? Who imposes the policy decision to decide what support is sufficient?

Hm.. good questions. I don't think we want userspace inspecting bits exposed by the kernel and trying to infer exactly what's being preserved and whether it's "good enough" to use. And such a UAPI would become tech debt once we finish development, I suspect.

Quoted 4 lines

A better approach would be to hide this support from userspace until we decide it is ready for production use-cases.

To enable development and testing, we can add an opt-in mechanism

Here is what I am trending towards sending in v3 as the opt-in mechanism:

drivers/vfio/pci/Kconfig +11 -0
index 1e82b44bda1a..770231554221 100644
@@ -58,6 +58,27 @@ config VFIO_PCI_ZDEV_KVM
 config VFIO_PCI_DMABUF
        def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER
 
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across a Live
+         Update. The eventual goal is that preserved devices can run
+         uninterrupted during a Live Update, including DMA to preserved
+         memory buffers and P2P. However there are many steps still needed to
+         achieve this, including:
+
+          - Preservation of iommufd files

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 81

Quoted 10 lines
drivers/vfio/pci/Kconfig +10 -0
+          - Preservation of IOMMU driver state
+          - Preservation of PCI state (BAR resources, device state, ...)
+          - Preservation of vfio-pci driver state
+
+         This option should only be enabled by developers working on
+         implementing this support. Once enough support has landed in the
+         kernel, this option will no longer be marked EXPERIMENTAL.
+
+         If you don't know what to do here, say N.
+

To use VFIO liveupdate, user has to do at least two things:
1. Enable CONFIG_LIVEUPDATE
2. Pass VFIO FD to a live update session.

Quoted 3 lines

This means someone using it has to know what live update is and intentionally pass the VFIO FDs. Isn't act of doing this itself an opt-in mechanism?

If it is, then I can leave this out. Alex?

My thinking was: Distros are free to enable LIVEUPDATE and use it. The support it enables today is all fully functional (albeit new). vfio-cdev, OTOH, is not. A separate Kconfig can help express that difference.

Quoted 6 lines
Quoted 2 lines

Consider that LIVEUPDATE could be enabled by default in a future release, but vfio-cdev support might not be ready yet at that point.

But that also requires point 2 above i.e. userspace explicitly passing VFIO FD to liveupdate. Unless there is a capability mechanism like KVM then userspace cannot know what is exactly supported.

Yes that is why I propose not exposing the support to userspace at all until it is ready, by compiling it out of kernel via new Kconfig. This way it does not get accidentally enabled in distros or downstream kernels before it is ready.

Quoted 3 lines

Also, users who
are using these APIs will already be advanced users and have to know many details about what liveupdate supports or not.

VMMs will be the ones preserving VFIO cdev files. I think you are suggesting they should know what versions of Linux support what kind of preservation? Like QEMU would know that Linux 7.1-7.4 supports partial VFIO preservation and 7.5+ supports fully? That does not sound like a good situation to be in.

I think it's much better to hide the support behind Kconfig until its ready. That way the PRESERVE_FD ioctl just fails on kernels that do not fully support (because VFIO_PCI_LIVEUPDATE is not enabled), and succeeds on kernels that do fully support.

If someone wants to enable and use VFIO_PCI_LIVEUPDATE while it is still marked experimental, they're on their own.

Quoted 12 lines
Quoted 12 lines
Quoted 2 lines

I am not sure providing VFIO_PCI_LIVEUPDATE alleviate Alex's concern about how userspace will know that sufficient VFIO support exists.

I was thinking we can flip VFIO_PCI_LIVEUPDATE to be enabled by default (if LIVEUPDATE and VFIO_PCI are enabled), and drop "(EXPERIMENTAL)" from the option title. That would be how distros and downstream users of the kernel know that sufficient support exists to enable VFIO_PCI_LIVEUPDATE.

Quoted 2 lines

May be write in liveupdate documentation (PATCH 11 of this series) that support is experimental?

Quoted 6 lines
Quoted 3 lines

The documentation in patch 11 includes largely the same text that I put under VFIO_PCI_LIVEU"PDATE. But I can explicitly mention "experimental" as well if that's what you're asking.

Yeah, even though documentation do get stale but I think there is no better way in this scenario.

Ok will do.

#103 lore raw

Re: [PATCH v2 10/22] vfio/pci: Skip reset of preserved device after Live Update

Quoted 88 lines · David Matlack
Quoted 71 lines · Vipin Sharma
Quoted 69 lines · David Matlack
Quoted 57 lines · Vipin Sharma
Quoted 40 lines · David Matlack
Quoted 13 lines · David Matlack
Quoted 5 lines · Alex Williamson

Sorry if I don't have the whole model in my head yet, but is exposing the restriction to the vfio user of the device sufficient to manage the liveupdate orchestration? For example, a VFIO_DEVICE_INFO_CAP pushes the knowledge to QEMU... what does QEMU do with that knowledge? Who imposes the policy decision to decide what support is sufficient?

Hm.. good questions. I don't think we want userspace inspecting bits exposed by the kernel and trying to infer exactly what's being preserved and whether it's "good enough" to use. And such a UAPI would become tech debt once we finish development, I suspect.

Quoted 4 lines

A better approach would be to hide this support from userspace until we decide it is ready for production use-cases.

To enable development and testing, we can add an opt-in mechanism

Here is what I am trending towards sending in v3 as the opt-in mechanism:

drivers/vfio/pci/Kconfig +11 -0
index 1e82b44bda1a..770231554221 100644
@@ -58,6 +58,27 @@ config VFIO_PCI_ZDEV_KVM
 config VFIO_PCI_DMABUF
        def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER
 
+config VFIO_PCI_LIVEUPDATE
+       bool "VFIO PCI support for Live Update (EXPERIMENTAL)"
+       depends on LIVEUPDATE && VFIO_PCI
+       help
+         Support for preserving devices bound to vfio-pci across a Live
+         Update. The eventual goal is that preserved devices can run
+         uninterrupted during a Live Update, including DMA to preserved
+         memory buffers and P2P. However there are many steps still needed to
+         achieve this, including:
+
+          - Preservation of iommufd files

drivers/vfio/pci/Kconfig config VFIO_PCI_ZDEV_KVM · line 81

Quoted 10 lines
drivers/vfio/pci/Kconfig +10 -0
+          - Preservation of IOMMU driver state
+          - Preservation of PCI state (BAR resources, device state, ...)
+          - Preservation of vfio-pci driver state
+
+         This option should only be enabled by developers working on
+         implementing this support. Once enough support has landed in the
+         kernel, this option will no longer be marked EXPERIMENTAL.
+
+         If you don't know what to do here, say N.
+

To use VFIO liveupdate, user has to do at least two things:
1. Enable CONFIG_LIVEUPDATE
2. Pass VFIO FD to a live update session.

Quoted 3 lines

This means someone using it has to know what live update is and intentionally pass the VFIO FDs. Isn't act of doing this itself an opt-in mechanism?

If it is, then I can leave this out. Alex?

My thinking was: Distros are free to enable LIVEUPDATE and use it. The support it enables today is all fully functional (albeit new). vfio-cdev, OTOH, is not. A separate Kconfig can help express that difference.

Quoted 6 lines
Quoted 2 lines

Consider that LIVEUPDATE could be enabled by default in a future release, but vfio-cdev support might not be ready yet at that point.

But that also requires point 2 above i.e. userspace explicitly passing VFIO FD to liveupdate. Unless there is a capability mechanism like KVM then userspace cannot know what is exactly supported.

Yes that is why I propose not exposing the support to userspace at all until it is ready, by compiling it out of kernel via new Kconfig. This way it does not get accidentally enabled in distros or downstream kernels before it is ready.

Quoted 3 lines

Also, users who
are using these APIs will already be advanced users and have to know many details about what liveupdate supports or not.

Quoted 5 lines

VMMs will be the ones preserving VFIO cdev files. I think you are suggesting they should know what versions of Linux support what kind of preservation? Like QEMU would know that Linux 7.1-7.4 supports partial VFIO preservation and 7.5+ supports fully? That does not sound like a good situation to be in.

I agree, for VMM its better to just assume it is a complete preservation feature but it is an experimental code in kernel.

Quoted 9 lines

I think it's much better to hide the support behind Kconfig until its ready. That way the PRESERVE_FD ioctl just fails on kernels that do not fully support (because VFIO_PCI_LIVEUPDATE is not enabled), and succeeds on kernels that do fully support.

If someone wants to enable and use VFIO_PCI_LIVEUPDATE while it is still marked experimental, they're on their own.

Sounds good. Thanks!

author maintainer
#104 lore raw

PATCH v2 11/22 docs: liveupdate: Document VFIO device file preservation

In reply to David Matlack

Add documentation for preserving VFIO device files across a Live Update, as well as some generic file preservation documentation. This documentation will be extended in the future as new types of files are supported and new dependency/ordering requirements are added.

---
 Documentation/userspace-api/liveupdate.rst | 144 +++++++++++++++++++++
 1 file changed, 144 insertions(+)
Documentation/userspace-api/liveupdate.rst +144 -0
index 41c0473e4f16..dbf1e4aeddd7 100644
@@ -14,6 +14,150 @@ ioctl uAPI
 ===========
 .. kernel-doc:: include/uapi/linux/liveupdate.h
 
+File Preservation
+=================
+
+Files can be preserved across Live Update in sessions. Since only one process
+can open /dev/liveupdate, sessions must be created by a centralized process
+(e.g. "luod") and then passed via UDS to lower privilege processes (e.g. VMMs)
+for them to preserve their own files.
+
+luod::
+
+  luo_fd = open("/dev/liveupdate", ...);
+
+  ...
+
+  // Create a new session with the given name.
+  struct liveupdate_ioctl_create_session arg = {
+          .size = sizeof(arg),
+	  .name = SESSION_NAME,
+  };
+  ioctl(luo_fd, LIVEUPDATE_IOCTL_CREATE_SESSION, &arg);
+
+  // Send session_fd to the VMM over UDS.
+  send_session_fd(..., arg.fd);
+
+VMM::
+
+  // Receive the newly created session from luod over UDS
+  session_fd = create_session(SESSION_NAME);
+
+  ...
+
+  // Preserve a file with a unique token value in the session.
+  struct liveupdate_session_preserve_fd arg = {
+          .size = sizeof(arg),
+          .fd = fd,
+          .token = TOKEN,
+  }
+  ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, &arg);
+
+Files can be unpreserved with the LIVEUPDATE_SESSION_UNPRESERVE_FD ioctl. They
+are also unpreserved once the last reference to the session is dropped.  To
+carry preserved files across a Live Update, references must be kept on the
+session files through the reboot(LINUX_REBOOT_CMD_KEXEC) syscall.
+
+While a file is preserved in a session, the kernel holds an extra reference
+to it to prevent it from being destroyed.
+
+Only the following types of files support LIVEUPDATE_SESSION_PRESERVE_FD. More
+types of files are expected to be added in the future.
+
+ - memfd
+ - VFIO character device files (vfio-pci only)
+
+File Retrieval
+==============
+
+Files that are preserved in a session retrieved after
+reboot(LINUX_REBOOT_CMD_KEXEC).
+
+luod::
+
+  luo_fd = open("/dev/liveupdate", ...);
+
+  ...
+
+  struct liveupdate_ioctl_retrieve_session arg = {
+          .size = sizeof(arg),
+	  .name = SESSION_NAME,
+  };
+  ioctl(luo_fd, LIVEUPDATE_IOCTL_RETRIEVE_SESSION, &arg);
+
+  // Send session_fd to VMM over UDS.
+  send_session_fd(..., arg.fd);
+
+VMM::
+
+  // Receive the retrieved session from luod over UDS
+  session_fd = retrieve_session(SESSION_NAME);
+
+  ...
+
+  // Retrieve the file associated with the token from the session.
+  struct liveupdate_session_retrieve_fd arg = {
+          .size = sizeof(arg),
+          .token = TOKEN,
+  };
+  ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg);
+
+  ...
+
+  ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, ...);
+
+A session can only be finished once all of the files within it have been
+retrieved, and are fully restored from the kernel's perspective. The exact
+requirements will vary by file type.
+
+VFIO Character Device (cdev) Files
+==================================
+
+The kernel supports preserving VFIO character device files across Live Update
+within a session::
+
+  device_fd = open("/dev/vfio/devices/X");
+
+  ...
+
+  ioctl(session_fd, LIVEUPDATE_SESSION_PRESERVE_FD, { ..., device_fd, ...});
+
+Attempting to preserve files acquired via VFIO_GROUP_GET_DEVICE_FD will fail.
+
+Since the kernel holds an extra reference to files preserved in sessions, there
+is no way for the underlying PCI device to be unbound from vfio-pci while it
+is being preserved.
+
+When a VFIO device file is preserved in a session, interrupts must be disabled
+on the device prior to reboot(LINUX_REBOOT_CMD_KEXEC), or the kexec will fail.
+
+Preserved VFIO device files can be retrieved after a Live Update just like any
+other preserved file::
+
+  ioctl(session_fd, LIVEUPDATE_SESSION_RETRIEVE_FD, &arg);
+  device_fd = arg.fd;
+
+  ...
+
+  ioctl(session_fd, LIVEUPDATE_SESSION_FINISH, ...);
+
+Prior to LIVEUPDATE_SESSION_FINISH, preserved devices must be retrieved from
+the session and bound to an iommufd. Attempting to open the device through
+its character device (/dev/vfio/devices/X) or VFIO_GROUP_GET_DEVICE_FD will
+fail with -EBUSY.
+
+The eventual goal of these support is to preserve devices running uninterrupted
+across a Live Update. However there are many steps still needed to achieve this
+(see Future Work below). So for now, VFIO will reset and restore the device
+back into an idle state during reboot(LINUX_REBOOT_CMD_KEXEC).
+
+Future work:
+
+ - Preservation of iommufd files
+ - Preservation of IOMMU driver state
+ - Preservation of PCI state (BAR resources, device state, bridge state, ...)
+ - Preservation of vfio-pci driver state
+
 See Also
 ========
 
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
#105 lore raw

Re: [PATCH v2 11/22] docs: liveupdate: Document VFIO device file preservation

Quoted 15 lines · David Matlack

Add documentation for preserving VFIO device files across a Live Update, as well as some generic file preservation documentation. This documentation will be extended in the future as new types of files are supported and new dependency/ordering requirements are added.

---
 Documentation/userspace-api/liveupdate.rst | 144 +++++++++++++++++++++
 1 file changed, 144 insertions(+)
Documentation/userspace-api/liveupdate.rst +0 -0
index 41c0473e4f16..dbf1e4aeddd7 100644

Documentation/userspace-api/liveupdate.rst ioctl uAPI · line 18

Quoted 5 lines
Documentation/userspace-api/liveupdate.rst +2 -0
@@ -14,6 +14,150 @@ ioctl uAPI
 ===========
 .. kernel-doc:: include/uapi/linux/liveupdate.h
 
+File Preservation
+=================

Should we use the existing way where documentation is written in code files and referenced here? Maybe luo_file.c for this?

Documentation/userspace-api/liveupdate.rst ioctl uAPI · line 71

Quoted 3 lines
Documentation/userspace-api/liveupdate.rst +3 -0
+
+File Retrieval
+==============

Same as above.

Documentation/userspace-api/liveupdate.rst ioctl uAPI · line 115

Quoted 5 lines
Documentation/userspace-api/liveupdate.rst +5 -0
+
+
+VFIO Character Device (cdev) Files
+==================================
+

vfio_pci_liveupdate.c

author maintainer
#106 lore raw

Re: [PATCH v2 11/22] docs: liveupdate: Document VFIO device file preservation

Quoted 20 lines · Vipin Sharma
Quoted 18 lines · David Matlack

Add documentation for preserving VFIO device files across a Live Update, as well as some generic file preservation documentation. This documentation will be extended in the future as new types of files are supported and new dependency/ordering requirements are added.

---
 Documentation/userspace-api/liveupdate.rst | 144 +++++++++++++++++++++
 1 file changed, 144 insertions(+)
Documentation/userspace-api/liveupdate.rst +0 -0
index 41c0473e4f16..dbf1e4aeddd7 100644
@@ -14,6 +14,150 @@ ioctl uAPI
 ===========
 .. kernel-doc:: include/uapi/linux/liveupdate.h
 
Quoted 5 lines

Documentation/userspace-api/liveupdate.rst ioctl uAPI · line 18

Quoted 2 lines
Documentation/userspace-api/liveupdate.rst +2 -0
+File Preservation
+=================

Should we use the existing way where documentation is written in code files and referenced here? Maybe luo_file.c for this?

Good idea. I will look into that. It would be nice to also have this documentation in the code.

author maintainer
#107 lore raw

PATCH v2 12/22 selftests/liveupdate: Move luo_test_utils.* into a reusable library

In reply to David Matlack

From: Vipin Sharma <[email protected]>

Move luo_test_utils.[ch] into a lib/ directory and pull the rules to build them out into a separate make script. This will enable these utilities to be also built by and used within other selftests (such as VFIO) in subsequent commits.

No functional change intended.

---
 tools/testing/selftests/liveupdate/.gitignore |  1 +
 tools/testing/selftests/liveupdate/Makefile   | 14 ++++---------
 .../include/libliveupdate.h}                  |  8 ++++----
 .../selftests/liveupdate/lib/libliveupdate.mk | 20 +++++++++++++++++++
 .../{luo_test_utils.c => lib/liveupdate.c}    |  2 +-
 .../selftests/liveupdate/luo_kexec_simple.c   |  2 +-
 .../selftests/liveupdate/luo_multi_session.c  |  2 +-
 7 files changed, 32 insertions(+), 17 deletions(-)
 rename tools/testing/selftests/liveupdate/{luo_test_utils.h => lib/include/libliveupdate.h} (87%)
 create mode 100644 tools/testing/selftests/liveupdate/lib/libliveupdate.mk
 rename tools/testing/selftests/liveupdate/{luo_test_utils.c => lib/liveupdate.c} (99%)
tools/testing/selftests/liveupdate/.gitignore +1 -0
index 661827083ab6..18a0c7036cf3 100644
@@ -3,6 +3,7 @@
 !/**/
 !*.c
 !*.h
+!*.mk
 !*.sh
 !.gitignore
 !config
tools/testing/selftests/liveupdate/Makefile +4 -10
index 080754787ede..a060cc21f27f 100644
@@ -1,7 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
-LIB_C += luo_test_utils.c
-
 TEST_GEN_PROGS += liveupdate
 
 TEST_GEN_PROGS_EXTENDED += luo_kexec_simple
@@ -10,25 +8,21 @@ TEST_GEN_PROGS_EXTENDED += luo_multi_session
 TEST_FILES += do_kexec.sh
 
 include ../lib.mk
+include lib/libliveupdate.mk
 
 CFLAGS += $(KHDR_INCLUDES)
 CFLAGS += -Wall -O2 -Wno-unused-function
 CFLAGS += -MD
 
-LIB_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIB_C))
 TEST_O := $(patsubst %, %.o, $(TEST_GEN_PROGS))
 TEST_O += $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED))
 
-TEST_DEP_FILES := $(patsubst %.o, %.d, $(LIB_O))
+TEST_DEP_FILES := $(patsubst %.o, %.d, $(LIBLIVEUPDATE_O))
 TEST_DEP_FILES += $(patsubst %.o, %.d, $(TEST_O))
 -include $(TEST_DEP_FILES)
 
-$(LIB_O): $(OUTPUT)/%.o: %.c
-	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
-
-$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: %.o $(LIB_O)
-	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIB_O) $(LDLIBS) -o $@
+$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: %.o $(LIBLIVEUPDATE_O)
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBLIVEUPDATE_O) $(LDLIBS) -o $@
 
-EXTRA_CLEAN += $(LIB_O)
 EXTRA_CLEAN += $(TEST_O)
 EXTRA_CLEAN += $(TEST_DEP_FILES)
tools/testing/selftests/liveupdate/lib/include/libliveupdate.h renamed from tools/testing/selftests/liveupdate/luo_test_utils.h +4 -4
similarity index 87%
rename from tools/testing/selftests/liveupdate/luo_test_utils.h
rename to tools/testing/selftests/liveupdate/lib/include/libliveupdate.h
index 90099bf49577..4390a2737930 100644
@@ -7,13 +7,13 @@
  * Utility functions for LUO kselftests.
  */
 
-#ifndef LUO_TEST_UTILS_H
-#define LUO_TEST_UTILS_H
+#ifndef SELFTESTS_LIVEUPDATE_LIB_LIVEUPDATE_H
+#define SELFTESTS_LIVEUPDATE_LIB_LIVEUPDATE_H
 
 #include <errno.h>
 #include <string.h>
 #include <linux/liveupdate.h>
-#include "../kselftest.h"
+#include "../../../kselftest.h"
 
 #define LUO_DEVICE "/dev/liveupdate"
 
@@ -41,4 +41,4 @@ typedef void (*luo_test_stage2_fn)(int luo_fd, int state_session_fd);
 int luo_test(int argc, char *argv[], const char *state_session_name,
 	     luo_test_stage1_fn stage1, luo_test_stage2_fn stage2);
 
-#endif /* LUO_TEST_UTILS_H */
+#endif /* SELFTESTS_LIVEUPDATE_LIB_LIVEUPDATE_H */
tools/testing/selftests/liveupdate/lib/libliveupdate.mk new file +20 -0
new file mode 100644
index 000000000000..fffd95b085b6
@@ -0,0 +1,20 @@
+include $(top_srcdir)/scripts/subarch.include
+ARCH ?= $(SUBARCH)
+
+LIBLIVEUPDATE_SRCDIR := $(selfdir)/liveupdate/lib
+
+LIBLIVEUPDATE_C := liveupdate.c
+
+LIBLIVEUPDATE_OUTPUT := $(OUTPUT)/libliveupdate
+
+LIBLIVEUPDATE_O := $(patsubst %.c, $(LIBLIVEUPDATE_OUTPUT)/%.o, $(LIBLIVEUPDATE_C))
+
+LIBLIVEUPDATE_O_DIRS := $(shell dirname $(LIBLIVEUPDATE_O) | uniq)
+$(shell mkdir -p $(LIBLIVEUPDATE_O_DIRS))
+
+CFLAGS += -I$(LIBLIVEUPDATE_SRCDIR)/include
+
+$(LIBLIVEUPDATE_O): $(LIBLIVEUPDATE_OUTPUT)/%.o : $(LIBLIVEUPDATE_SRCDIR)/%.c
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
+
+EXTRA_CLEAN += $(LIBLIVEUPDATE_OUTPUT)
tools/testing/selftests/liveupdate/lib/liveupdate.c renamed from tools/testing/selftests/liveupdate/luo_test_utils.c +1 -1
similarity index 99%
rename from tools/testing/selftests/liveupdate/luo_test_utils.c
rename to tools/testing/selftests/liveupdate/lib/liveupdate.c
index 3c8721c505df..60121873f685 100644
@@ -21,7 +21,7 @@
 #include <errno.h>
 #include <stdarg.h>
 
-#include "luo_test_utils.h"
+#include <libliveupdate.h>
 
 int luo_open_device(void)
 {
tools/testing/selftests/liveupdate/luo_kexec_simple.c +1 -1
index d7ac1f3dc4cb..786ac93b9ae3 100644
@@ -8,7 +8,7 @@
  * across a single kexec reboot.
  */
 
-#include "luo_test_utils.h"
+#include <libliveupdate.h>
 
 #define TEST_SESSION_NAME "test-session"
 #define TEST_MEMFD_TOKEN 0x1A
tools/testing/selftests/liveupdate/luo_multi_session.c +1 -1
index 0ee2d795beef..aac24a5f5ce3 100644
@@ -9,7 +9,7 @@
  * files.
  */
 
-#include "luo_test_utils.h"
+#include <libliveupdate.h>
 
 #define SESSION_EMPTY_1 "multi-test-empty-1"
 #define SESSION_EMPTY_2 "multi-test-empty-2"
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#108 lore raw

PATCH v2 13/22 selftests/liveupdate: Add helpers to preserve/retrieve FDs

In reply to David Matlack

From: Vipin Sharma <[email protected]>

Add helper functions to preserve and retrieve file descriptors from an LUO session. These will be used be used in subsequent commits to preserve FDs other than memfd.

No functional change intended.

---
 .../liveupdate/lib/include/libliveupdate.h    |  3 ++
 .../selftests/liveupdate/lib/liveupdate.c     | 41 +++++++++++++++----
 2 files changed, 35 insertions(+), 9 deletions(-)
tools/testing/selftests/liveupdate/lib/include/libliveupdate.h +3 -0
index 4390a2737930..4c93d043d2b3 100644
@@ -26,6 +26,9 @@ int luo_create_session(int luo_fd, const char *name);
 int luo_retrieve_session(int luo_fd, const char *name);
 int luo_session_finish(int session_fd);
 
+int luo_session_preserve_fd(int session_fd, int fd, int token);
+int luo_session_retrieve_fd(int session_fd, int token);
+
 int create_and_preserve_memfd(int session_fd, int token, const char *data);
 int restore_and_verify_memfd(int session_fd, int token, const char *expected_data);
 
tools/testing/selftests/liveupdate/lib/liveupdate.c +32 -9

65 lines not on this page · raw message

Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#109 lore raw

PATCH v2 14/22 vfio: selftests: Build liveupdate library in VFIO selftests

In reply to David Matlack

From: Vipin Sharma <[email protected]>

Import and build liveupdate selftest library in VFIO selftests.

It allows to use liveupdate ioctls in VFIO selftests

---
 tools/testing/selftests/vfio/Makefile | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
tools/testing/selftests/vfio/Makefile +8 -3
index 3c796ca99a50..1e50998529fd 100644
@@ -12,6 +12,7 @@ TEST_FILES += scripts/setup.sh
 
 include ../lib.mk
 include lib/libvfio.mk
+include ../liveupdate/lib/libliveupdate.mk
 
 CFLAGS += -I$(top_srcdir)/tools/include
 CFLAGS += -MD
@@ -19,11 +20,15 @@ CFLAGS += $(EXTRA_CFLAGS)
 
 LDFLAGS += -pthread
 
-$(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O)
-	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@
+LIBS_O := $(LIBVFIO_O)
+LIBS_O += $(LIBLIVEUPDATE_O)
+
+$(TEST_GEN_PROGS): %: %.o $(LIBS_O)
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBS_O) $(LDLIBS) -o $@
 
 TEST_GEN_PROGS_O = $(patsubst %, %.o, $(TEST_GEN_PROGS))
-TEST_DEP_FILES = $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O) $(LIBVFIO_O))
+TEST_DEP_FILES := $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O))
+TEST_DEP_FILES += $(patsubst %.o, %.d, $(LIBS_O))
 -include $(TEST_DEP_FILES)
 
 EXTRA_CLEAN += $(TEST_GEN_PROGS_O) $(TEST_DEP_FILES)
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#110 lore raw

PATCH v2 15/22 vfio: selftests: Add Makefile support for TEST_GEN_PROGS_EXTENDED

In reply to David Matlack

Add Makefile support for TEST_GEN_PROGS_EXTENDED targets. These tests are not run by default.

TEST_GEN_PROGS_EXTENDED will be used for Live Update selftests in subsequent commits. These selftests must be run manually because they require the user/runner to perform additional actions, such as kexec, during the test.

---
 tools/testing/selftests/vfio/Makefile | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/testing/selftests/vfio/Makefile +7 -4
index 1e50998529fd..f9c040094d4a 100644
@@ -23,12 +23,15 @@ LDFLAGS += -pthread
 LIBS_O := $(LIBVFIO_O)
 LIBS_O += $(LIBLIVEUPDATE_O)
 
-$(TEST_GEN_PROGS): %: %.o $(LIBS_O)
+$(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): %: %.o $(LIBS_O)
 	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBS_O) $(LDLIBS) -o $@
 
-TEST_GEN_PROGS_O = $(patsubst %, %.o, $(TEST_GEN_PROGS))
-TEST_DEP_FILES := $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O))
+TESTS_O := $(patsubst %, %.o, $(TEST_GEN_PROGS))
+TESTS_O += $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED))
+
+TEST_DEP_FILES := $(patsubst %.o, %.d, $(TESTS_O))
 TEST_DEP_FILES += $(patsubst %.o, %.d, $(LIBS_O))
 -include $(TEST_DEP_FILES)
 
-EXTRA_CLEAN += $(TEST_GEN_PROGS_O) $(TEST_DEP_FILES)
+EXTRA_CLEAN += $(TESTS_O)
+EXTRA_CLEAN += $(TEST_DEP_FILES)
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#111 lore raw

PATCH v2 16/22 vfio: selftests: Add vfio_pci_liveupdate_uapi_test

In reply to David Matlack

Add a selftest to exercise preserving a various VFIO files through /dev/liveupdate. Ensure that VFIO cdev device files can be preserved and everything else (group-based device files, group files, and container files) all fail.

---
 tools/testing/selftests/vfio/Makefile         |  1 +
 .../vfio/vfio_pci_liveupdate_uapi_test.c      | 93 +++++++++++++++++++
 2 files changed, 94 insertions(+)
 create mode 100644 tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c
tools/testing/selftests/vfio/Makefile +1 -0
index f9c040094d4a..666310872217 100644
@@ -4,6 +4,7 @@ TEST_GEN_PROGS += vfio_iommufd_setup_test
 TEST_GEN_PROGS += vfio_pci_device_test
 TEST_GEN_PROGS += vfio_pci_device_init_perf_test
 TEST_GEN_PROGS += vfio_pci_driver_test
+TEST_GEN_PROGS += vfio_pci_liveupdate_uapi_test
 
 TEST_FILES += scripts/cleanup.sh
 TEST_FILES += scripts/lib.sh
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
#112 lore raw

Re: [PATCH v2 16/22] vfio: selftests: Add vfio_pci_liveupdate_uapi_test

Quoted 7 lines · David Matlack
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +7 -0
+
+TEST_F(vfio_pci_liveupdate_uapi_test, preserve_device)
+{
+	int ret;
+
+	ret = luo_session_preserve_fd(self->session_fd, self->device->fd, 0);
+

tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c line 63

Quoted 10 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +10 -0
+	/* Preservation should only be supported for VFIO cdev files. */
+	ASSERT_EQ(ret, self->iommu->iommufd ? 0 : -ENOENT);
+}
+
+TEST_F(vfio_pci_liveupdate_uapi_test, preserve_group_fails)
+{
+	int ret;
+
+	if (self->iommu->iommufd)
+		return;

for iommufd variant, should we mark test as skipped instead of showing it as passed?

tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c line 87

Quoted 8 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +8 -0
+int main(int argc, char *argv[])
+{
+	int fd;
+
+	fd = luo_open_device();
+	if (fd < 0) {
+		printf("open(%s) failed: %s, skipping\n", LUO_DEVICE, strerror(errno));
+		return KSFT_SKIP;

Both can be combined using ksft_exit_skip()

author maintainer
#113 lore raw

Re: [PATCH v2 16/22] vfio: selftests: Add vfio_pci_liveupdate_uapi_test

Quoted 14 lines · Vipin Sharma
Quoted 12 lines · David Matlack
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +12 -0
+
+TEST_F(vfio_pci_liveupdate_uapi_test, preserve_device)
+{
+     int ret;
+
+     ret = luo_session_preserve_fd(self->session_fd, self->device->fd, 0);
+
+     /* Preservation should only be supported for VFIO cdev files. */
+     ASSERT_EQ(ret, self->iommu->iommufd ? 0 : -ENOENT);
+}
+
+TEST_F(vfio_pci_liveupdate_uapi_test, preserve_group_fails)
Quoted 8 lines

tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c line 63

Quoted 5 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +5 -0
+{
+     int ret;
+
+     if (self->iommu->iommufd)
+             return;

for iommufd variant, should we mark test as skipped instead of showing it as passed?

Will do.

Quoted 10 lines

tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c line 87

Quoted 8 lines
tools/testing/selftests/vfio/vfio_pci_liveupdate_uapi_test.c new file +8 -0
+int main(int argc, char *argv[])
+{
+     int fd;
+
+     fd = luo_open_device();
+     if (fd < 0) {
+             printf("open(%s) failed: %s, skipping\n", LUO_DEVICE, strerror(errno));
+             return KSFT_SKIP;

Both can be combined using ksft_exit_skip()

Will do.

Thanks!

author maintainer
#114 lore raw

PATCH v2 17/22 vfio: selftests: Initialize vfio_pci_device using a VFIO cdev FD

In reply to David Matlack

From: Vipin Sharma <[email protected]>

Use the given VFIO cdev FD to initialize vfio_pci_device in VFIO selftests. Add the assertion to make sure that passed cdev FD is not used with legacy VFIO APIs. If VFIO cdev FD is provided then do not open the device instead use the FD for any interaction with the device.

This API will allow to write selftests where VFIO device FD is preserved using liveupdate and retrieved later using liveupdate ioctl after kexec.

---
 .../lib/include/libvfio/vfio_pci_device.h     |  3 ++
 .../selftests/vfio/lib/vfio_pci_device.c      | 33 ++++++++++++++-----
 2 files changed, 27 insertions(+), 9 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +3 -0
index 2858885a89bb..896dfde88118 100644
@@ -38,6 +38,9 @@ struct vfio_pci_device {
 #define dev_info(_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
 #define dev_err(_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf, ##__VA_ARGS__)
 
+struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
+					       struct iommu *iommu,
+					       int device_fd);
 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iommu);
 void vfio_pci_device_cleanup(struct vfio_pci_device *device);
 
tools/testing/selftests/vfio/lib/vfio_pci_device.c +24 -9
index fac4c0ecadef..08bb582eaa8f 100644
@@ -318,19 +318,27 @@ static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id)
 	ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args);
 }
 
-static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *bdf)
+static void vfio_pci_iommufd_setup(struct vfio_pci_device *device,
+				   const char *bdf, int device_fd)
 {
-	const char *cdev_path = vfio_pci_get_cdev_path(bdf);
+	const char *cdev_path;
 
-	device->fd = open(cdev_path, O_RDWR);
-	VFIO_ASSERT_GE(device->fd, 0);
-	free((void *)cdev_path);
+	if (device_fd >= 0) {
+		device->fd = device_fd;
+	} else {
+		cdev_path = vfio_pci_get_cdev_path(bdf);
+		device->fd = open(cdev_path, O_RDWR);
+		VFIO_ASSERT_GE(device->fd, 0);
+		free((void *)cdev_path);
+	}
 
 	vfio_device_bind_iommufd(device->fd, device->iommu->iommufd);
 	vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id);
 }
 
-struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iommu)
+struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
+					       struct iommu *iommu,
+					       int device_fd)
 {
 	struct vfio_pci_device *device;
 
@@ -341,10 +349,12 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iomm
 	device->iommu = iommu;
 	device->bdf = bdf;
 
-	if (iommu->mode->container_path)
+	if (iommu->mode->container_path) {
+		VFIO_ASSERT_EQ(device_fd, -1);
 		vfio_pci_container_setup(device, bdf);
-	else
-		vfio_pci_iommufd_setup(device, bdf);
+	} else {
+		vfio_pci_iommufd_setup(device, bdf, device_fd);
+	}
 
 	vfio_pci_device_setup(device);
 	vfio_pci_driver_probe(device);
@@ -352,6 +362,11 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iomm
 	return device;
 }
 
+struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iommu)
+{
+	return __vfio_pci_device_init(bdf, iommu, /*device_fd=*/-1);
+}
+
 void vfio_pci_device_cleanup(struct vfio_pci_device *device)
 {
 	int i;
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#115 lore raw

PATCH v2 18/22 vfio: selftests: Add vfio_pci_liveupdate_kexec_test

In reply to David Matlack

From: Vipin Sharma <[email protected]>

Add a selftest to exercise preserving a vfio-pci device across a Live Update. For now the test is extremely simple and just verifies that the device file can be preserved and retrieved. In the future this test will be extended to verify more parts about device preservation as they are implemented.

This test is added to TEST_GEN_PROGS_EXTENDED since it must be run manually along with a kexec.

To run this test manually:

 $ tools/testing/selftests/vfio/scripts/setup.sh 0000:00:04.0
 $ tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test --stage 1 0000:00:04.0
 $ kexec ...   # NOTE: Exact method will be distro-dependent
 $ tools/testing/selftests/vfio/scripts/setup.sh 0000:00:04.0
 $ tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test --stage 2 0000:00:04.0

The second call to setup.sh is necessary because preserved devices are not bound to a driver after Live Update. Such devices must be manually bound by userspace after Live Update via driver_override.

This test is considered passing if all commands exit with 0.

---
 tools/testing/selftests/vfio/Makefile         |  4 +
 .../vfio/vfio_pci_liveupdate_kexec_test.c     | 89 +++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100644 tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c
tools/testing/selftests/vfio/Makefile +4 -0
index 666310872217..55d685f6e540 100644
@@ -6,6 +6,10 @@ TEST_GEN_PROGS += vfio_pci_device_init_perf_test
 TEST_GEN_PROGS += vfio_pci_driver_test
 TEST_GEN_PROGS += vfio_pci_liveupdate_uapi_test
 
+# This test must be run manually since it requires the user/automation to
+# perform a kexec during the test.
+TEST_GEN_PROGS_EXTENDED += vfio_pci_liveupdate_kexec_test
+
 TEST_FILES += scripts/cleanup.sh
 TEST_FILES += scripts/lib.sh
 TEST_FILES += scripts/run.sh
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#116 lore raw

PATCH v2 19/22 vfio: selftests: Expose iommu_modes to tests

In reply to David Matlack

Expose the list of iommu_modes to enable tests that want to iterate through all possible iommu modes.

---
 tools/testing/selftests/vfio/lib/include/libvfio/iommu.h | 2 ++
 tools/testing/selftests/vfio/lib/iommu.c                 | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +2 -0
index 5c9b9dc6d993..a03ff2281f11 100644
@@ -15,6 +15,8 @@ struct iommu_mode {
 	unsigned long iommu_type;
 };
 
+extern const struct iommu_mode iommu_modes[];
+extern const int nr_iommu_modes;
 extern const char *default_iommu_mode;
 
 struct dma_region {
tools/testing/selftests/vfio/lib/iommu.c +3 -1
index 58b7fb7430d4..add35dbc83f8 100644
@@ -23,7 +23,7 @@
 const char *default_iommu_mode = "iommufd";
 
 /* Reminder: Keep in sync with FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(). */
-static const struct iommu_mode iommu_modes[] = {
+const struct iommu_mode iommu_modes[] = {
 	{
 		.name = "vfio_type1_iommu",
 		.container_path = "/dev/vfio/vfio",
@@ -49,6 +49,8 @@ static const struct iommu_mode iommu_modes[] = {
 	},
 };
 
+const int nr_iommu_modes = ARRAY_SIZE(iommu_modes);
+
 static const struct iommu_mode *lookup_iommu_mode(const char *iommu_mode)
 {
 	int i;
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#117 lore raw

PATCH v2 20/22 vfio: selftests: Expose low-level helper routines for setting up struct vfio_pci_device

In reply to David Matlack

Expose a few low-level helper routings for setting up vfio_pci_device structs. These routines will be used in a subsequent commit to assert that VFIO_GROUP_GET_DEVICE_FD fails under certain conditions.

---
 .../lib/include/libvfio/vfio_pci_device.h     |  5 +++
 .../selftests/vfio/lib/vfio_pci_device.c      | 33 +++++++++++++------
 2 files changed, 28 insertions(+), 10 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +5 -0
index 896dfde88118..2389c7698335 100644
@@ -125,4 +125,9 @@ static inline bool vfio_pci_device_match(struct vfio_pci_device *device,
 
 const char *vfio_pci_get_cdev_path(const char *bdf);
 
+/* Low-level routines for setting up a struct vfio_pci_device */
+struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iommu *iommu);
+void vfio_pci_group_setup(struct vfio_pci_device *device);
+void vfio_pci_iommu_setup(struct vfio_pci_device *device);
+
 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */
tools/testing/selftests/vfio/lib/vfio_pci_device.c +23 -10
index 08bb582eaa8f..76597c84e956 100644
@@ -198,7 +198,7 @@ static unsigned int vfio_pci_get_group_from_dev(const char *bdf)
 	return group;
 }
 
-static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf)
+void vfio_pci_group_setup(struct vfio_pci_device *device)
 {
 	struct vfio_group_status group_status = {
 		.argsz = sizeof(group_status),
@@ -206,7 +206,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
 	char group_path[32];
 	int group;
 
-	group = vfio_pci_get_group_from_dev(bdf);
+	group = vfio_pci_get_group_from_dev(device->bdf);
 	snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group);
 
 	device->group_fd = open(group_path, O_RDWR);
@@ -218,14 +218,12 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
 	ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->container_fd);
 }
 
-static void vfio_pci_container_setup(struct vfio_pci_device *device, const char *bdf)
+void vfio_pci_iommu_setup(struct vfio_pci_device *device)
 {
 	struct iommu *iommu = device->iommu;
 	unsigned long iommu_type = iommu->mode->iommu_type;
 	int ret;
 
-	vfio_pci_group_setup(device, bdf);
-
 	ret = ioctl(iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type);
 	VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type);
 
@@ -235,8 +233,14 @@ static void vfio_pci_container_setup(struct vfio_pci_device *device, const char
 	 * because the IOMMU type is already set.
 	 */
 	(void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type);
+}
 
-	device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf);
+static void vfio_pci_container_setup(struct vfio_pci_device *device)
+{
+	vfio_pci_group_setup(device);
+	vfio_pci_iommu_setup(device);
+
+	device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, device->bdf);
 	VFIO_ASSERT_GE(device->fd, 0);
 }
 
@@ -336,9 +340,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device,
 	vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id);
 }
 
-struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
-					       struct iommu *iommu,
-					       int device_fd)
+struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iommu *iommu)
 {
 	struct vfio_pci_device *device;
 
@@ -349,9 +351,20 @@ struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
 	device->iommu = iommu;
 	device->bdf = bdf;
 
+	return device;
+}
+
+struct vfio_pci_device *__vfio_pci_device_init(const char *bdf,
+					       struct iommu *iommu,
+					       int device_fd)
+{
+	struct vfio_pci_device *device;
+
+	device = vfio_pci_device_alloc(bdf, iommu);
+
 	if (iommu->mode->container_path) {
 		VFIO_ASSERT_EQ(device_fd, -1);
-		vfio_pci_container_setup(device, bdf);
+		vfio_pci_container_setup(device);
 	} else {
 		vfio_pci_iommufd_setup(device, bdf, device_fd);
 	}
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#118 lore raw

PATCH v2 21/22 vfio: selftests: Verify that opening VFIO device fails during Live Update

In reply to David Matlack

Verify that opening a VFIO device through its cdev file and via VFIO_GROUP_GET_DEVICE_FD both fail with -EBUSY if the device was preserved across a Live Update. When a device file is preserve across a Live Update, the file must be retrieved from /dev/liveupdate, not from VFIO directly.

---
 .../vfio/vfio_pci_liveupdate_kexec_test.c     | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +38 -0
index 15b3e3af91d1..65c48196e44e 100644
@@ -36,6 +36,42 @@ static void before_kexec(int luo_fd)
 	daemonize_and_wait();
 }
 
+static void check_open_vfio_device_fails(void)
+{
+	const char *cdev_path = vfio_pci_get_cdev_path(device_bdf);
+	struct vfio_pci_device *device;
+	struct iommu *iommu;
+	int ret, i;
+
+	printf("Checking open(%s) fails\n", cdev_path);
+	ret = open(cdev_path, O_RDWR);
+	VFIO_ASSERT_EQ(ret, -1);
+	VFIO_ASSERT_EQ(errno, EBUSY);
+	free((void *)cdev_path);
+
+	for (i = 0; i < nr_iommu_modes; i++) {
+		if (!iommu_modes[i].container_path)
+			continue;
+
+		iommu = iommu_init(iommu_modes[i].name);
+
+		device = vfio_pci_device_alloc(device_bdf, iommu);
+		vfio_pci_group_setup(device);
+		vfio_pci_iommu_setup(device);
+
+		printf("Checking ioctl(group_fd, VFIO_GROUP_GET_DEVICE_FD, \"%s\") fails (%s)\n",
+		       device_bdf, iommu_modes[i].name);
+
+		ret = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, device->bdf);
+		VFIO_ASSERT_EQ(ret, -1);
+		VFIO_ASSERT_EQ(errno, EBUSY);
+
+		close(device->group_fd);
+		free(device);
+		iommu_cleanup(iommu);
+	}
+}
+
 static void after_kexec(int luo_fd, int state_session_fd)
 {
 	struct vfio_pci_device *device;
@@ -44,6 +80,8 @@ static void after_kexec(int luo_fd, int state_session_fd)
 	int device_fd;
 	int stage;
 
+	check_open_vfio_device_fails();
+
 	restore_and_read_stage(state_session_fd, STATE_TOKEN, &stage);
 	VFIO_ASSERT_EQ(stage, 2);
 
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#119 lore raw

PATCH v2 22/22 vfio: selftests: Add continuous DMA to vfio_pci_liveupdate_kexec_test

In reply to David Matlack

Add a long-running DMA memcpy operation to vfio_pci_liveupdate_kexec_test so that the device attempts to perform DMAs continuously during the Live Update.

At this point iommufd preservation is not supported and bus mastering is not kept enabled on the device during across the kexec, so most of these DMAs will be dropped. However this test ensures that the current device preservation support does not lead to system instability or crashes if the device is active. And once iommufd and bus mastering are preserved, this test can be relaxed to check that the DMA operations completed successfully.

---
 .../vfio/vfio_pci_liveupdate_kexec_test.c     | 129 ++++++++++++++++++
 1 file changed, 129 insertions(+)
tools/testing/selftests/vfio/vfio_pci_liveupdate_kexec_test.c +129 -0
index 65c48196e44e..36bddfbb88ed 100644
@@ -1,8 +1,16 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
+#include <linux/sizes.h>
+#include <sys/mman.h>
+
 #include <libliveupdate.h>
 #include <libvfio.h>
 
+#define MEMCPY_SIZE SZ_1G
+#define DRIVER_SIZE SZ_1M
+#define MEMFD_SIZE (MEMCPY_SIZE + DRIVER_SIZE)
+
+static struct dma_region memcpy_region;
 static const char *device_bdf;
 
 static char state_session[LIVEUPDATE_SESSION_NAME_LENGTH];
@@ -11,8 +19,89 @@ static char device_session[LIVEUPDATE_SESSION_NAME_LENGTH];
 enum {
 	STATE_TOKEN,
 	DEVICE_TOKEN,
+	MEMFD_TOKEN,
 };
 
+static void dma_memcpy_one(struct vfio_pci_device *device)
+{
+	void *src = memcpy_region.vaddr, *dst;
+	u64 size;
+
+	size = min_t(u64, memcpy_region.size / 2, device->driver.max_memcpy_size);
+	dst = src + size;
+
+	memset(src, 1, size);
+	memset(dst, 0, size);
+
+	printf("Kicking off 1 DMA memcpy operations of size 0x%lx...\n", size);
+	vfio_pci_driver_memcpy(device,
+			       to_iova(device, src),
+			       to_iova(device, dst),
+			       size);
+
+	VFIO_ASSERT_EQ(memcmp(src, dst, size), 0);
+}
+
+static void dma_memcpy_start(struct vfio_pci_device *device)
+{
+	void *src = memcpy_region.vaddr, *dst;
+	u64 count, size;
+
+	size = min_t(u64, memcpy_region.size / 2, device->driver.max_memcpy_size);
+	dst = src + size;
+
+	/*
+	 * Rough Math: If we assume the device will perform memcpy at a rate of
+	 * 30GB/s then 7200GB of transfers will run for about 4 minutes.
+	 */
+	count = (u64)7200 * SZ_1G / size;
+	count = min_t(u64, count, device->driver.max_memcpy_count);
+
+	memset(src, 1, size / 2);
+	memset(dst, 0, size / 2);
+
+	printf("Kicking off %lu DMA memcpy operations of size 0x%lx...\n", count, size);
+	vfio_pci_driver_memcpy_start(device,
+				     to_iova(device, src),
+				     to_iova(device, dst),
+				     size, count);
+}
+
+static void dma_memfd_map(struct vfio_pci_device *device, int fd)
+{
+	void *vaddr;
+
+	vaddr = mmap(NULL, MEMFD_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
+	VFIO_ASSERT_NE(vaddr, MAP_FAILED);
+
+	memcpy_region.iova = SZ_4G;
+	memcpy_region.size = MEMCPY_SIZE;
+	memcpy_region.vaddr = vaddr;
+	iommu_map(device->iommu, &memcpy_region);
+
+	device->driver.region.iova = memcpy_region.iova + memcpy_region.size;
+	device->driver.region.size = DRIVER_SIZE;
+	device->driver.region.vaddr = vaddr + memcpy_region.size;
+	iommu_map(device->iommu, &device->driver.region);
+}
+
+static void dma_memfd_setup(struct vfio_pci_device *device, int session_fd)
+{
+	int fd, ret;
+
+	fd = memfd_create("dma-buffer", 0);
+	VFIO_ASSERT_GE(fd, 0);
+
+	ret = fallocate(fd, 0, 0, MEMFD_SIZE);
+	VFIO_ASSERT_EQ(ret, 0);
+
+	printf("Preserving memfd of size 0x%x in session\n", MEMFD_SIZE);
+	ret = luo_session_preserve_fd(session_fd, fd, MEMFD_TOKEN);
+	VFIO_ASSERT_EQ(ret, 0);
+
+	dma_memfd_map(device, fd);
+}
+
 static void before_kexec(int luo_fd)
 {
 	struct vfio_pci_device *device;
@@ -32,6 +121,27 @@ static void before_kexec(int luo_fd)
 	ret = luo_session_preserve_fd(session_fd, device->fd, DEVICE_TOKEN);
 	VFIO_ASSERT_EQ(ret, 0);
 
+	dma_memfd_setup(device, session_fd);
+
+	/*
+	 * If the device has a selftests driver, kick off a long-running DMA
+	 * operation to exercise the device trying to DMA during a Live Update.
+	 * Since iommufd preservation is not supported yet, these DMAs should be
+	 * dropped. So this is just looking to verify that the system does not
+	 * fall over and crash as a result of a busy device being preserved.
+	 */
+	if (device->driver.ops) {
+		vfio_pci_driver_init(device);
+		dma_memcpy_start(device);
+
+		/*
+		 * Disable interrupts on the device or freeze() will fail.
+		 * Unfortunately there isn't a way to easily have a test for
+		 * that here since the check happens during shutdown.
+		 */
+		vfio_pci_msix_disable(device);
+	}
+
 	close(luo_fd);
 	daemonize_and_wait();
 }
@@ -78,6 +188,7 @@ static void after_kexec(int luo_fd, int state_session_fd)
 	struct iommu *iommu;
 	int session_fd;
 	int device_fd;
+	int memfd;
 	int stage;
 
 	check_open_vfio_device_fails();
@@ -88,6 +199,10 @@ static void after_kexec(int luo_fd, int state_session_fd)
 	session_fd = luo_retrieve_session(luo_fd, device_session);
 	VFIO_ASSERT_GE(session_fd, 0);
 
+	printf("Retrieving memfd from LUO\n");
+	memfd = luo_session_retrieve_fd(session_fd, MEMFD_TOKEN);
+	VFIO_ASSERT_GE(memfd, 0);
+
 	printf("Finishing the session before retrieving the device (should fail)\n");
 	VFIO_ASSERT_NE(luo_session_finish(session_fd), 0);
 
@@ -109,9 +224,23 @@ static void after_kexec(int luo_fd, int state_session_fd)
 	 */
 	device = __vfio_pci_device_init(device_bdf, iommu, device_fd);
 
+	dma_memfd_map(device, memfd);
+
 	printf("Finishing the session\n");
 	VFIO_ASSERT_EQ(luo_session_finish(session_fd), 0);
 
+	/*
+	 * Once iommufd preservation is supported and the device is kept fully
+	 * running across the Live Update, this should wait for the long-
+	 * running DMA memcpy operation kicked off in before_kexec() to
+	 * complete. But for now we expect the device to be reset so just
+	 * trigger a single memcpy to make sure it's still functional.
+	 */
+	if (device->driver.ops) {
+		vfio_pci_driver_init(device);
+		dma_memcpy_one(device);
+	}
+
 	vfio_pci_device_cleanup(device);
 	iommu_cleanup(iommu);
 }
Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog