Skip to contentSkip to conversation

Live update: vfio and iommufd

PATCH Applied Steve Sistare 4 participants 34 messages Jul 2, 2025 – Mar 27 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v6 cover Live update: vfio and iommufd

NOTE: this V6 series depends on the patch
vfio-user: do not register vfio-user container with cpr
which is in vfio-next.

Support vfio and iommufd devices with the cpr-transfer live migration mode. Devices that do not support live migration can still support cpr-transfer, allowing live update to a new version of QEMU on the same host, with no loss of guest connectivity.

No user-visible interfaces are added.

For legacy containers:

Pass vfio device descriptors to new QEMU. In new QEMU, during vfio_realize, skip the ioctls that configure the device, because it is already configured.

Use VFIO_DMA_UNMAP_FLAG_VADDR to abandon the old VA's for DMA mapped regions, and use VFIO_DMA_MAP_FLAG_VADDR to register the new VA in new QEMU and update the locked memory accounting. The physical pages remain pinned, because the descriptor of the device that locked them remains open, so DMA to those pages continues without interruption. Mediated devices are not supported, however, because they require the VA to always be valid, and there is a brief window where no VA is registered.

Save the MSI message area as part of vfio-pci vmstate, and pass the interrupt and notifier eventfd's to new QEMU. New QEMU loads the MSI data, then the vfio-pci post_load handler finds the eventfds in CPR state, rebuilds vector data structures, and attaches the interrupts to the new KVM instance. This logic also applies to iommufd containers.

For iommufd containers:

Use IOMMU_IOAS_MAP_FILE to register memory regions for DMA when they are backed by a file (including a memfd), so DMA mappings do not depend on VA, which can differ after live update. This allows mediated devices to be supported.

Pass the iommufd and vfio device descriptors from old to new QEMU. In new QEMU, during vfio_realize, skip the ioctls that configure the device, because it is already configured.

In new QEMU, call ioctl(IOMMU_IOAS_CHANGE_PROCESS) to update mm ownership and locked memory accounting.

Patches 3 to 8 are specific to legacy containers. Patches 21 to 36 are specific to iommufd containers. The remainder apply to both.

Changes from previous versions:
* V1 of this series contains minor changes from the "Live update: vfio" and "Live update: iommufd" series, mainly bug fixes and refactored patches.

Changes in V2:
* refactored various vfio code snippets into new cpr helpers
* refactored vfio struct members into cpr-specific structures
* refactored various small changes into their own patches
* split complex patches. Notably:
- split "refactor for cpr" into 5 patches
- split "reconstruct device" into 4 patches
* refactored vfio_connect_container using helpers and made its error recovery more robust.
* moved vfio pci msi/vector/intx cpr functions to cpr.c
* renamed "reused" to cpr_reused and cpr.reused
* squashed vfio_cpr_[un]register_container to their call sites
* simplified iommu_type setting after cpr
* added cpr_open_fd and cpr_is_incoming helpers
* removed changes from vfio_legacy_dma_map, and instead temporarily override dma_map and dma_unmap ops.
* deleted error_report and returned Error to callers where possible.
* simplified the memory_get_xlat_addr interface
* fixed flags passed to iommufd_backend_alloc_hwpt
* defined MIG_PRI_UNINITIALIZED
* added maintainers

Changes in V3:
* removed cleanup patches that were already pulled
* rebased to latest master

Changes in V4:
* added SPDX-License-Identifier
* patch "vfio/container: preserve descriptors"
- rewrote search loop in vfio_container_connect
- do not return pfd from vfio_cpr_container_match
- add helper for VFIO_GROUP_GET_DEVICE_FD
* deleted patch "export vfio_legacy_dma_map"
* patch "vfio/container: restore DMA vaddr"
- deleted redundant error_report from vfio_legacy_cpr_dma_map
- save old dma_map function
* patch "vfio-pci: skip reset during cpr"
- use cpr_is_incoming instead of cpr_reused
* renamed err -> local_err in all new code
* patch "export MSI functions"
- renamed with vfio_pci prefix, and defined wrappers for low level routines instead of exporting them.
* patch "close kvm after cpr"
- fixed build error for !CONFIG_KVM
* added the cpr_resave_fd helper
* dropped patch "pass ramblock to vfio_container_dma_map", relying on "pass MemoryRegion" from the vfio-user series instead.
* deleted "reused" variables, replaced with cpr_is_incoming()
* renamed cpr_needed_for_reuse -> cpr_incoming_needed
* rewrote patch "pci: skip reset during cpr"
* rebased to latest master

for iommufd:
* deleted redundant error_report from iommufd_backend_map_file_dma
* added interface doc for dma_map_file
* check return value of cpr_open_fd
* deleted "export iommufd_cdev_get_info_iova_range"
* deleted "reconstruct device"
* deleted "reconstruct hw_caps"
* deleted "define hwpt constructors"
* seperated cpr registration for iommufd be and vfio container
* correctly attach to multiple containers per iommufd using ioas_id
* simplified "reconstruct hwpt" by matching against hwpt_id.
* added patch "add vfio_device_free_name"

Changes in V5:
* dropped: vfio/pci: vfio_pci_put_device on failure
* added: "vfio: doc changes for cpr"
* deleted unnecessary include of vfio-cpr.h
* fixed compilation for !CONFIG_VFIO and !CONFIG_IOMMUFD
* misc minor changes
* Added RB's, rebased to master

Changes in V6:
* dropped already-pulled patches
* converted remaining g_free in "add vfio_device_free_name"
* fixed iommufd_backend_disconnect in "preserve descriptors"
* tweaked vfio_cpr_load_device in "preserve descriptors"
* added trace_vfio_cpr_find_device in "cpr state"
* rewrote vfio_notifier_init and vfio_msix_vector_use
* rewrote the notifier in "close kvm after cpr"
* Added RB's, rebased to master

Steve Sistare (21):
vfio-pci: preserve MSI
vfio-pci: preserve INTx
migration: close kvm after cpr
migration: cpr_get_fd_param helper
backends/iommufd: iommufd_backend_map_file_dma
backends/iommufd: change process ioctl
physmem: qemu_ram_get_fd_offset
vfio/iommufd: use IOMMU_IOAS_MAP_FILE
vfio/iommufd: invariant device name
vfio/iommufd: add vfio_device_free_name
vfio/iommufd: device name blocker
vfio/iommufd: register container for cpr
migration: vfio cpr state hook
vfio/iommufd: cpr state
vfio/iommufd: preserve descriptors
vfio/iommufd: reconstruct device
vfio/iommufd: reconstruct hwpt
vfio/iommufd: change process
iommufd: preserve DMA mappings
vfio/container: delete old cpr register
vfio: doc changes for cpr

 docs/devel/migration/CPR.rst          |   5 +-
 qapi/migration.json                   |   6 +-
 hw/vfio/pci.h                         |   2 +
 include/exec/cpu-common.h             |   1 +
 include/hw/vfio/vfio-container-base.h |  15 +++
 include/hw/vfio/vfio-cpr.h            |  29 ++++-
 include/hw/vfio/vfio-device.h         |   3 +
 include/migration/cpr.h               |  14 +++
 include/system/iommufd.h              |   7 ++
 include/system/kvm.h                  |   1 +
 accel/kvm/kvm-all.c                   |  32 +++++
 backends/iommufd.c                    | 107 +++++++++++++++-
 hw/vfio/ap.c                          |   4 +-
 hw/vfio/ccw.c                         |   4 +-
 hw/vfio/container-base.c              |   9 ++
 hw/vfio/cpr-iommufd.c                 | 224 ++++++++++++++++++++++++++++++++++
 hw/vfio/cpr-legacy.c                  |   2 +
 hw/vfio/cpr.c                         | 144 ++++++++++++++++++++--
 hw/vfio/device.c                      |  40 ++++--
 hw/vfio/helpers.c                     |  11 ++
 hw/vfio/iommufd-stubs.c               |  18 +++
 hw/vfio/iommufd.c                     |  81 ++++++++++--
 hw/vfio/pci.c                         | 109 ++++++++++++++++-
 hw/vfio/platform.c                    |   2 +-
 migration/cpr.c                       |  52 ++++++--
 system/physmem.c                      |   5 +
 backends/trace-events                 |   2 +
 hw/vfio/meson.build                   |   2 +
 hw/vfio/trace-events                  |   3 +
 29 files changed, 871 insertions(+), 63 deletions(-)
 create mode 100644 hw/vfio/cpr-iommufd.c
 create mode 100644 hw/vfio/iommufd-stubs.c
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v6 1/21 vfio-pci: preserve MSI

Save the MSI message area as part of vfio-pci vmstate, and preserve the interrupt and notifier eventfd's. migrate_incoming loads the MSI data, then the vfio-pci post_load handler finds the eventfds in CPR state, rebuilds vector data structures, and attaches the interrupts to the new KVM instance.

---
 hw/vfio/pci.h              |  2 +
 include/hw/vfio/vfio-cpr.h |  8 ++++
 hw/vfio/cpr.c              | 97 ++++++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/pci.c              | 52 ++++++++++++++++++++++++-
 4 files changed, 157 insertions(+), 2 deletions(-)
hw/vfio/pci.h +2 -0
index 5ba7330..495fae7 100644
@@ -218,6 +218,8 @@ void vfio_pci_add_kvm_msi_virq(VFIOPCIDevice *vdev, VFIOMSIVector *vector,
 void vfio_pci_prepare_kvm_msi_virq_batch(VFIOPCIDevice *vdev);
 void vfio_pci_commit_kvm_msi_virq_batch(VFIOPCIDevice *vdev);
 bool vfio_pci_intx_enable(VFIOPCIDevice *vdev, Error **errp);
+void vfio_pci_msix_set_notifiers(VFIOPCIDevice *vdev);
+void vfio_pci_msi_set_handler(VFIOPCIDevice *vdev, int nr);
 
 uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len);
 void vfio_pci_write_config(PCIDevice *pdev,
include/hw/vfio/vfio-cpr.h +8 -0
index 8bf85b9..25e74ee 100644
@@ -15,6 +15,7 @@
 struct VFIOContainer;
 struct VFIOContainerBase;
 struct VFIOGroup;
+struct VFIOPCIDevice;
 
 typedef struct VFIOContainerCPR {
     Error *blocker;
@@ -52,6 +53,13 @@ void vfio_cpr_giommu_remap(struct VFIOContainerBase *bcontainer,
 bool vfio_cpr_ram_discard_register_listener(
     struct VFIOContainerBase *bcontainer, MemoryRegionSection *section);
 
+void vfio_cpr_save_vector_fd(struct VFIOPCIDevice *vdev, const char *name,
+                             int nr, int fd);
+int vfio_cpr_load_vector_fd(struct VFIOPCIDevice *vdev, const char *name,
+                            int nr);
+void vfio_cpr_delete_vector_fd(struct VFIOPCIDevice *vdev, const char *name,
+                               int nr);
+
 extern const VMStateDescription vfio_cpr_pci_vmstate;
 
 #endif /* HW_VFIO_VFIO_CPR_H */
hw/vfio/cpr.c +97 -0

122 lines not on this page · raw message

hw/vfio/pci.c +50 -2

106 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Reviewed-by on v6 1/21
#2 lore raw

Re: [PATCH V6 01/21] vfio-pci: preserve MSI

Quoted 6 lines · Steve Sistare

Save the MSI message area as part of vfio-pci vmstate, and preserve the interrupt and notifier eventfd's. migrate_incoming loads the MSI data, then the vfio-pci post_load handler finds the eventfds in CPR state, rebuilds vector data structures, and attaches the interrupts to the new KVM instance.

Quoted 7 lines
---
  hw/vfio/pci.h              |  2 +
  include/hw/vfio/vfio-cpr.h |  8 ++++
  hw/vfio/cpr.c              | 97 ++++++++++++++++++++++++++++++++++++++++++++++
  hw/vfio/pci.c              | 52 ++++++++++++++++++++++++-
  4 files changed, 157 insertions(+), 2 deletions(-)

Thanks,

C.

#3 lore raw

PATCH v6 2/21 vfio-pci: preserve INTx

In reply to Steve Sistare

Preserve vfio INTx state across cpr-transfer. Preserve VFIOINTx fields as follows:
pin : Recover this from the vfio config in kernel space interrupt : Preserve its eventfd descriptor across exec. unmask : Ditto
route.irq : This could perhaps be recovered in vfio_pci_post_load by
calling pci_device_route_intx_to_irq(pin), whose implementation reads config space for a bridge device such as ich9. However, there is no guarantee that the bridge vmstate is read before vfio vmstate. Rather than fiddling with MigrationPriority for vmstate handlers, explicitly save route.irq in vfio vmstate.
pending : save in vfio vmstate.
mmap_timeout, mmap_timer : Re-initialize
bool kvm_accel : Re-initialize

In vfio_realize, defer calling vfio_intx_enable until the vmstate is available, in vfio_pci_post_load. Modify vfio_intx_enable and vfio_intx_kvm_enable to skip vfio initialization, but still perform kvm initialization.

---
 hw/vfio/cpr.c | 27 ++++++++++++++++++++++++++-
 hw/vfio/pci.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 79 insertions(+), 3 deletions(-)
hw/vfio/cpr.c +26 -1
index e467373..f5555ca 100644
@@ -139,7 +139,11 @@ static int vfio_cpr_pci_post_load(void *opaque, int version_id)
         vfio_cpr_claim_vectors(vdev, nr_vectors, false);
 
     } else if (vfio_pci_read_config(pdev, PCI_INTERRUPT_PIN, 1)) {
-        g_assert_not_reached();      /* completed in a subsequent patch */
+        Error *local_err = NULL;
+        if (!vfio_pci_intx_enable(vdev, &local_err)) {
+            error_report_err(local_err);
+            return -1;
+        }
     }
 
     return 0;
@@ -152,6 +156,26 @@ static bool pci_msix_present(void *opaque, int version_id)
     return msix_present(pdev);
 }
 
+static const VMStateDescription vfio_intx_vmstate = {
+    .name = "vfio-cpr-intx",
+    .version_id = 0,
+    .minimum_version_id = 0,
+    .fields = (VMStateField[]) {
+        VMSTATE_BOOL(pending, VFIOINTx),
+        VMSTATE_UINT32(route.mode, VFIOINTx),
+        VMSTATE_INT32(route.irq, VFIOINTx),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+#define VMSTATE_VFIO_INTX(_field, _state) {                         \
+    .name       = (stringify(_field)),                              \
+    .size       = sizeof(VFIOINTx),                                 \
+    .vmsd       = &vfio_intx_vmstate,                               \
+    .flags      = VMS_STRUCT,                                       \
+    .offset     = vmstate_offset_value(_state, _field, VFIOINTx),   \
+}
+
 const VMStateDescription vfio_cpr_pci_vmstate = {
     .name = "vfio-cpr-pci",
     .version_id = 0,
@@ -162,6 +186,7 @@ const VMStateDescription vfio_cpr_pci_vmstate = {
     .fields = (VMStateField[]) {
         VMSTATE_PCI_DEVICE(pdev, VFIOPCIDevice),
         VMSTATE_MSIX_TEST(pdev, VFIOPCIDevice, pci_msix_present),
+        VMSTATE_VFIO_INTX(intx, VFIOPCIDevice),
         VMSTATE_END_OF_LIST()
     }
 };
hw/vfio/pci.c +53 -2

85 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Reviewed-by on v6 2/21
#4 lore raw

Re: [PATCH V6 02/21] vfio-pci: preserve INTx

Quoted 20 lines · Steve Sistare

Preserve vfio INTx state across cpr-transfer. Preserve VFIOINTx fields as follows:
pin : Recover this from the vfio config in kernel space interrupt : Preserve its eventfd descriptor across exec. unmask : Ditto
route.irq : This could perhaps be recovered in vfio_pci_post_load by
calling pci_device_route_intx_to_irq(pin), whose implementation reads config space for a bridge device such as ich9. However, there is no guarantee that the bridge vmstate is read before vfio vmstate. Rather than fiddling with MigrationPriority for vmstate handlers, explicitly save route.irq in vfio vmstate.
pending : save in vfio vmstate.
mmap_timeout, mmap_timer : Re-initialize
bool kvm_accel : Re-initialize

In vfio_realize, defer calling vfio_intx_enable until the vmstate is available, in vfio_pci_post_load. Modify vfio_intx_enable and vfio_intx_kvm_enable to skip vfio initialization, but still perform kvm initialization.

Quoted 5 lines
---
  hw/vfio/cpr.c | 27 ++++++++++++++++++++++++++-
  hw/vfio/pci.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
  2 files changed, 79 insertions(+), 3 deletions(-)

Thanks,

C.

#5 lore raw

PATCH v6 3/21 migration: close kvm after cpr

In reply to Steve Sistare

cpr-transfer breaks vfio network connectivity to and from the guest, and the host system log shows:
irq bypass consumer (token 00000000a03c32e5) registration fails: -16
which is EBUSY. This occurs because KVM descriptors are still open in the old QEMU process. Close them.

---
 include/hw/vfio/vfio-cpr.h    |  2 ++
 include/hw/vfio/vfio-device.h |  2 ++
 include/system/kvm.h          |  1 +
 accel/kvm/kvm-all.c           | 32 ++++++++++++++++++++++++++++++++
 hw/vfio/cpr-legacy.c          |  2 ++
 hw/vfio/cpr.c                 | 21 +++++++++++++++++++++
 hw/vfio/helpers.c             | 11 +++++++++++
 7 files changed, 71 insertions(+)
include/hw/vfio/vfio-cpr.h +2 -0
index 25e74ee..099d54f 100644
@@ -62,4 +62,6 @@ void vfio_cpr_delete_vector_fd(struct VFIOPCIDevice *vdev, const char *name,
 
 extern const VMStateDescription vfio_cpr_pci_vmstate;
 
+void vfio_cpr_add_kvm_notifier(void);
+
 #endif /* HW_VFIO_VFIO_CPR_H */
include/hw/vfio/vfio-device.h +2 -0
index c616652..f503837 100644
@@ -283,4 +283,6 @@ void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
                       DeviceState *dev, bool ram_discard);
 int vfio_device_get_aw_bits(VFIODevice *vdev);
+
+void vfio_kvm_device_close(void);
 #endif /* HW_VFIO_VFIO_COMMON_H */
include/system/kvm.h +1 -0
index 7cc60d2..4896a3c 100644
@@ -195,6 +195,7 @@ bool kvm_has_sync_mmu(void);
 int kvm_has_vcpu_events(void);
 int kvm_max_nested_state_length(void);
 int kvm_has_gsi_routing(void);
+void kvm_close(void);
 
 /**
  * kvm_arm_supports_user_irq
accel/kvm/kvm-all.c +32 -0
index d095d1b..8141854 100644
@@ -515,16 +515,23 @@ static int do_kvm_destroy_vcpu(CPUState *cpu)
         goto err;
     }
 
+    /* If I am the CPU that created coalesced_mmio_ring, then discard it */
+    if (s->coalesced_mmio_ring == (void *)cpu->kvm_run + PAGE_SIZE) {
+        s->coalesced_mmio_ring = NULL;
+    }
+
     ret = munmap(cpu->kvm_run, mmap_size);
     if (ret < 0) {
         goto err;
     }
+    cpu->kvm_run = NULL;
 
     if (cpu->kvm_dirty_gfns) {
         ret = munmap(cpu->kvm_dirty_gfns, s->kvm_dirty_ring_bytes);
         if (ret < 0) {
             goto err;
         }
+        cpu->kvm_dirty_gfns = NULL;
     }
 
     kvm_park_vcpu(cpu);
@@ -608,6 +615,31 @@ err:
     return ret;
 }
 
+void kvm_close(void)
+{
+    CPUState *cpu;
+
+    if (!kvm_state || kvm_state->fd == -1) {
+        return;
+    }
+
+    CPU_FOREACH(cpu) {
+        cpu_remove_sync(cpu);
+        close(cpu->kvm_fd);
+        cpu->kvm_fd = -1;
+        close(cpu->kvm_vcpu_stats_fd);
+        cpu->kvm_vcpu_stats_fd = -1;
+    }
+
+    if (kvm_state && kvm_state->fd != -1) {
+        close(kvm_state->vmfd);
+        kvm_state->vmfd = -1;
+        close(kvm_state->fd);
+        kvm_state->fd = -1;
+    }
+    kvm_state = NULL;
+}
+
 /*
  * dirty pages logging control
  */
hw/vfio/cpr-legacy.c +2 -0
index a84c324..daa3523 100644
@@ -177,6 +177,8 @@ bool vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp)
                                          MIG_MODE_CPR_TRANSFER, -1) == 0;
     }
 
+    vfio_cpr_add_kvm_notifier();
+
     vmstate_register(NULL, -1, &vfio_container_vmstate, container);
 
     /* During incoming CPR, divert calls to dma_map. */
hw/vfio/cpr.c +21 -0
index f5555ca..0e903cd 100644
@@ -190,3 +190,24 @@ const VMStateDescription vfio_cpr_pci_vmstate = {
         VMSTATE_END_OF_LIST()
     }
 };
+
+static NotifierWithReturn kvm_close_notifier;
+
+static int vfio_cpr_kvm_close_notifier(NotifierWithReturn *notifier,
+                                       MigrationEvent *e,
+                                       Error **errp)
+{
+    if (e->type == MIG_EVENT_PRECOPY_DONE) {
+        vfio_kvm_device_close();
+    }
+    return 0;
+}
+
+void vfio_cpr_add_kvm_notifier(void)
+{
+    if (!kvm_close_notifier.notify) {
+        migration_add_notifier_mode(&kvm_close_notifier,
+                                    vfio_cpr_kvm_close_notifier,
+                                    MIG_MODE_CPR_TRANSFER);
+    }
+}
hw/vfio/helpers.c +11 -0
index d0dbab1..9a5f621 100644
@@ -117,6 +117,17 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
 int vfio_kvm_device_fd = -1;
 #endif
 
+void vfio_kvm_device_close(void)
+{
+#ifdef CONFIG_KVM
+    kvm_close();
+    if (vfio_kvm_device_fd != -1) {
+        close(vfio_kvm_device_fd);
+        vfio_kvm_device_fd = -1;
+    }
+#endif
+}
+
 int vfio_kvm_device_add_fd(int fd, Error **errp)
 {
 #ifdef CONFIG_KVM
Signature · 1 line
1.8.3.1
#6 lore raw

Re: [PATCH V6 03/21] migration: close kvm after cpr

cc Paolo.

After incorporating Peter's feedback, IMO this version reads well:
* kvm exports kvm_close
* vfio exports vfio_kvm_device_close
* vfio-cpr registers a notifier that calls vfio_kvm_device_close

- Steve

Quoted 177 lines · Steve Sistare

cpr-transfer breaks vfio network connectivity to and from the guest, and the host system log shows:
irq bypass consumer (token 00000000a03c32e5) registration fails: -16
which is EBUSY. This occurs because KVM descriptors are still open in the old QEMU process. Close them.

---
  include/hw/vfio/vfio-cpr.h    |  2 ++
  include/hw/vfio/vfio-device.h |  2 ++
  include/system/kvm.h          |  1 +
  accel/kvm/kvm-all.c           | 32 ++++++++++++++++++++++++++++++++
  hw/vfio/cpr-legacy.c          |  2 ++
  hw/vfio/cpr.c                 | 21 +++++++++++++++++++++
  hw/vfio/helpers.c             | 11 +++++++++++
  7 files changed, 71 insertions(+)
include/hw/vfio/vfio-cpr.h +2 -0
index 25e74ee..099d54f 100644
@@ -62,4 +62,6 @@ void vfio_cpr_delete_vector_fd(struct VFIOPCIDevice *vdev, const char *name,
 
  extern const VMStateDescription vfio_cpr_pci_vmstate;
 
+void vfio_cpr_add_kvm_notifier(void);
+
  #endif /* HW_VFIO_VFIO_CPR_H */
include/hw/vfio/vfio-device.h +2 -0
index c616652..f503837 100644
@@ -283,4 +283,6 @@ void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
  void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
                        DeviceState *dev, bool ram_discard);
  int vfio_device_get_aw_bits(VFIODevice *vdev);
+
+void vfio_kvm_device_close(void);
  #endif /* HW_VFIO_VFIO_COMMON_H */
include/system/kvm.h +1 -0
index 7cc60d2..4896a3c 100644
@@ -195,6 +195,7 @@ bool kvm_has_sync_mmu(void);
  int kvm_has_vcpu_events(void);
  int kvm_max_nested_state_length(void);
  int kvm_has_gsi_routing(void);
+void kvm_close(void);
 
  /**
   * kvm_arm_supports_user_irq
accel/kvm/kvm-all.c +32 -0
index d095d1b..8141854 100644
@@ -515,16 +515,23 @@ static int do_kvm_destroy_vcpu(CPUState *cpu)
          goto err;
      }
 
+    /* If I am the CPU that created coalesced_mmio_ring, then discard it */
+    if (s->coalesced_mmio_ring == (void *)cpu->kvm_run + PAGE_SIZE) {
+        s->coalesced_mmio_ring = NULL;
+    }
+
      ret = munmap(cpu->kvm_run, mmap_size);
      if (ret < 0) {
          goto err;
      }
+    cpu->kvm_run = NULL;
 
      if (cpu->kvm_dirty_gfns) {
          ret = munmap(cpu->kvm_dirty_gfns, s->kvm_dirty_ring_bytes);
          if (ret < 0) {
              goto err;
          }
+        cpu->kvm_dirty_gfns = NULL;
      }
 
      kvm_park_vcpu(cpu);
@@ -608,6 +615,31 @@ err:
      return ret;
  }
 
+void kvm_close(void)
+{
+    CPUState *cpu;
+
+    if (!kvm_state || kvm_state->fd == -1) {
+        return;
+    }
+
+    CPU_FOREACH(cpu) {
+        cpu_remove_sync(cpu);
+        close(cpu->kvm_fd);
+        cpu->kvm_fd = -1;
+        close(cpu->kvm_vcpu_stats_fd);
+        cpu->kvm_vcpu_stats_fd = -1;
+    }
+
+    if (kvm_state && kvm_state->fd != -1) {
+        close(kvm_state->vmfd);
+        kvm_state->vmfd = -1;
+        close(kvm_state->fd);
+        kvm_state->fd = -1;
+    }
+    kvm_state = NULL;
+}
+
  /*
   * dirty pages logging control
   */
hw/vfio/cpr-legacy.c +2 -0
index a84c324..daa3523 100644
@@ -177,6 +177,8 @@ bool vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp)
                                           MIG_MODE_CPR_TRANSFER, -1) == 0;
      }
 
+    vfio_cpr_add_kvm_notifier();
+
      vmstate_register(NULL, -1, &vfio_container_vmstate, container);
 
      /* During incoming CPR, divert calls to dma_map. */
hw/vfio/cpr.c +21 -0
index f5555ca..0e903cd 100644
@@ -190,3 +190,24 @@ const VMStateDescription vfio_cpr_pci_vmstate = {
          VMSTATE_END_OF_LIST()
      }
  };
+
+static NotifierWithReturn kvm_close_notifier;
+
+static int vfio_cpr_kvm_close_notifier(NotifierWithReturn *notifier,
+                                       MigrationEvent *e,
+                                       Error **errp)
+{
+    if (e->type == MIG_EVENT_PRECOPY_DONE) {
+        vfio_kvm_device_close();
+    }
+    return 0;
+}
+
+void vfio_cpr_add_kvm_notifier(void)
+{
+    if (!kvm_close_notifier.notify) {
+        migration_add_notifier_mode(&kvm_close_notifier,
+                                    vfio_cpr_kvm_close_notifier,
+                                    MIG_MODE_CPR_TRANSFER);
+    }
+}
hw/vfio/helpers.c +11 -0
index d0dbab1..9a5f621 100644
@@ -117,6 +117,17 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,
  int vfio_kvm_device_fd = -1;
  #endif
 
+void vfio_kvm_device_close(void)
+{
+#ifdef CONFIG_KVM
+    kvm_close();
+    if (vfio_kvm_device_fd != -1) {
+        close(vfio_kvm_device_fd);
+        vfio_kvm_device_fd = -1;
+    }
+#endif
+}
+
  int vfio_kvm_device_add_fd(int fd, Error **errp)
  {
  #ifdef CONFIG_KVM
#7 lore raw

RE: [PATCH V6 03/21] migration: close kvm after cpr

Quoted 141 lines

-----Original Message-----
From: Steven Sistare <[email protected]>
Subject: Re: [PATCH V6 03/21] migration: close kvm after cpr

cc Paolo.

After incorporating Peter's feedback, IMO this version reads well:
* kvm exports kvm_close
* vfio exports vfio_kvm_device_close
* vfio-cpr registers a notifier that calls vfio_kvm_device_close

- Steve

Quoted 6 lines · Steve Sistare

cpr-transfer breaks vfio network connectivity to and from the guest, and the host system log shows:
irq bypass consumer (token 00000000a03c32e5) registration fails: -16
which is EBUSY. This occurs because KVM descriptors are still open in the old QEMU process. Close them.

Quoted 8 lines
---
  include/hw/vfio/vfio-cpr.h    |  2 ++
  include/hw/vfio/vfio-device.h |  2 ++
  include/system/kvm.h          |  1 +
  accel/kvm/kvm-all.c           | 32

++++++++++++++++++++++++++++++++

include/hw/vfio/vfio-cpr.h void vfio_cpr_delete_vector_fd(struct VFIOPCIDevice

Quoted 10 lines
  hw/vfio/cpr-legacy.c          |  2 ++
  hw/vfio/cpr.c                 | 21 +++++++++++++++++++++
  hw/vfio/helpers.c             | 11 +++++++++++
  7 files changed, 71 insertions(+)
include/hw/vfio/vfio-cpr.h +0 -0
index 25e74ee..099d54f 100644
@@ -62,4 +62,6 @@ void vfio_cpr_delete_vector_fd(struct VFIOPCIDevice

*vdev, const char *name,

include/hw/vfio/vfio-device.h void vfio_device_set_fd(VFIODevice *vbasedev,

Quoted 11 lines
include/hw/vfio/vfio-cpr.h +2 -0
  extern const VMStateDescription vfio_cpr_pci_vmstate;
 
+void vfio_cpr_add_kvm_notifier(void);
+
  #endif /* HW_VFIO_VFIO_CPR_H */
include/hw/vfio/vfio-device.h +0 -0
index c616652..f503837 100644
@@ -283,4 +283,6 @@ void vfio_device_set_fd(VFIODevice *vbasedev,

const char *str, Error **errp);

include/hw/vfio/vfio-device.h void vfio_device_set_fd(VFIODevice *vbasedev, · line 283

Quoted 1 line
include/hw/vfio/vfio-device.h +0 -0
  void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps

*ops,

Quoted 22 lines
include/hw/vfio/vfio-device.h +2 -0
                        DeviceState *dev, bool ram_discard);
  int vfio_device_get_aw_bits(VFIODevice *vdev);
+
+void vfio_kvm_device_close(void);
  #endif /* HW_VFIO_VFIO_COMMON_H */
include/system/kvm.h +1 -0
index 7cc60d2..4896a3c 100644
@@ -195,6 +195,7 @@ bool kvm_has_sync_mmu(void);
  int kvm_has_vcpu_events(void);
  int kvm_max_nested_state_length(void);
  int kvm_has_gsi_routing(void);
+void kvm_close(void);
 
  /**
   * kvm_arm_supports_user_irq
accel/kvm/kvm-all.c +0 -0
index d095d1b..8141854 100644

accel/kvm/kvm-all.c static int do_kvm_destroy_vcpu(CPUState *cpu) · line 518

Quoted 4 lines
accel/kvm/kvm-all.c +1 -0
@@ -515,16 +515,23 @@ static int do_kvm_destroy_vcpu(CPUState *cpu)
          goto err;
      }
 
+    /* If I am the CPU that created coalesced_mmio_ring, then discard it

*/

hw/vfio/cpr-legacy.c bool

Quoted 56 lines
accel/kvm/kvm-all.c +31 -0
+    if (s->coalesced_mmio_ring == (void *)cpu->kvm_run + PAGE_SIZE) {
+        s->coalesced_mmio_ring = NULL;
+    }
+
      ret = munmap(cpu->kvm_run, mmap_size);
      if (ret < 0) {
          goto err;
      }
+    cpu->kvm_run = NULL;
 
      if (cpu->kvm_dirty_gfns) {
          ret = munmap(cpu->kvm_dirty_gfns, s->kvm_dirty_ring_bytes);
          if (ret < 0) {
              goto err;
          }
+        cpu->kvm_dirty_gfns = NULL;
      }
 
      kvm_park_vcpu(cpu);
@@ -608,6 +615,31 @@ err:
      return ret;
  }
 
+void kvm_close(void)
+{
+    CPUState *cpu;
+
+    if (!kvm_state || kvm_state->fd == -1) {
+        return;
+    }
+
+    CPU_FOREACH(cpu) {
+        cpu_remove_sync(cpu);
+        close(cpu->kvm_fd);
+        cpu->kvm_fd = -1;
+        close(cpu->kvm_vcpu_stats_fd);
+        cpu->kvm_vcpu_stats_fd = -1;
+    }
+
+    if (kvm_state && kvm_state->fd != -1) {
+        close(kvm_state->vmfd);
+        kvm_state->vmfd = -1;
+        close(kvm_state->fd);
+        kvm_state->fd = -1;
+    }
+    kvm_state = NULL;
+}
+
  /*
   * dirty pages logging control
   */
hw/vfio/cpr-legacy.c +0 -0
index a84c324..daa3523 100644
@@ -177,6 +177,8 @@ bool

vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp)

Quoted 1 line
Quoted 4 lines

MIG_MODE_CPR_TRANSFER, -1) == 0;

Quoted 3 lines
      }

+ vfio_cpr_add_kvm_notifier();

Hi Steven, I just noticed this, do we need to do same for iommufd? Do we need to delete notifier when all VFIO devices hot unplugged?

I see Cedric has just sent a PR, if I'm right, maybe a following up patch to address it?

Thanks
Zhenzhong

#8 lore raw

Re: [PATCH V6 03/21] migration: close kvm after cpr

Quoted 139 lines · Zhenzhong
Quoted 139 lines

-----Original Message-----
From: Steven Sistare <[email protected]>
Subject: Re: [PATCH V6 03/21] migration: close kvm after cpr

cc Paolo.

After incorporating Peter's feedback, IMO this version reads well:
* kvm exports kvm_close
* vfio exports vfio_kvm_device_close
* vfio-cpr registers a notifier that calls vfio_kvm_device_close

- Steve

Quoted 6 lines · Steve Sistare

cpr-transfer breaks vfio network connectivity to and from the guest, and the host system log shows:

    irq bypass consumer (token 00000000a03c32e5) registration fails: -16

which is EBUSY. This occurs because KVM descriptors are still open in the old QEMU process. Close them.

Quoted 8 lines
---
   include/hw/vfio/vfio-cpr.h    |  2 ++
   include/hw/vfio/vfio-device.h |  2 ++
   include/system/kvm.h          |  1 +
   accel/kvm/kvm-all.c           | 32

++++++++++++++++++++++++++++++++

include/hw/vfio/vfio-cpr.h void vfio_cpr_delete_vector_fd(struct VFIOPCIDevice

Quoted 10 lines
   hw/vfio/cpr-legacy.c          |  2 ++
   hw/vfio/cpr.c                 | 21 +++++++++++++++++++++
   hw/vfio/helpers.c             | 11 +++++++++++
   7 files changed, 71 insertions(+)
include/hw/vfio/vfio-cpr.h +0 -0
index 25e74ee..099d54f 100644
@@ -62,4 +62,6 @@ void vfio_cpr_delete_vector_fd(struct VFIOPCIDevice

*vdev, const char *name,

include/hw/vfio/vfio-device.h void vfio_device_set_fd(VFIODevice *vbasedev,

Quoted 11 lines
include/hw/vfio/vfio-cpr.h +2 -0
   extern const VMStateDescription vfio_cpr_pci_vmstate;
 
+void vfio_cpr_add_kvm_notifier(void);
+
   #endif /* HW_VFIO_VFIO_CPR_H */
include/hw/vfio/vfio-device.h +0 -0
index c616652..f503837 100644
@@ -283,4 +283,6 @@ void vfio_device_set_fd(VFIODevice *vbasedev,

const char *str, Error **errp);

include/hw/vfio/vfio-device.h void vfio_device_set_fd(VFIODevice *vbasedev, · line 283

Quoted 1 line
include/hw/vfio/vfio-device.h +0 -0
   void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps

*ops,

Quoted 22 lines
include/hw/vfio/vfio-device.h +2 -0
                         DeviceState *dev, bool ram_discard);
   int vfio_device_get_aw_bits(VFIODevice *vdev);
+
+void vfio_kvm_device_close(void);
   #endif /* HW_VFIO_VFIO_COMMON_H */
include/system/kvm.h +1 -0
index 7cc60d2..4896a3c 100644
@@ -195,6 +195,7 @@ bool kvm_has_sync_mmu(void);
   int kvm_has_vcpu_events(void);
   int kvm_max_nested_state_length(void);
   int kvm_has_gsi_routing(void);
+void kvm_close(void);
 
   /**
    * kvm_arm_supports_user_irq
accel/kvm/kvm-all.c +0 -0
index d095d1b..8141854 100644

accel/kvm/kvm-all.c static int do_kvm_destroy_vcpu(CPUState *cpu) · line 518

Quoted 4 lines
accel/kvm/kvm-all.c +1 -0
@@ -515,16 +515,23 @@ static int do_kvm_destroy_vcpu(CPUState *cpu)
           goto err;
       }
 
+    /* If I am the CPU that created coalesced_mmio_ring, then discard it

*/

hw/vfio/cpr-legacy.c bool

Quoted 56 lines
accel/kvm/kvm-all.c +31 -0
+    if (s->coalesced_mmio_ring == (void *)cpu->kvm_run + PAGE_SIZE) {
+        s->coalesced_mmio_ring = NULL;
+    }
+
       ret = munmap(cpu->kvm_run, mmap_size);
       if (ret < 0) {
           goto err;
       }
+    cpu->kvm_run = NULL;
 
       if (cpu->kvm_dirty_gfns) {
           ret = munmap(cpu->kvm_dirty_gfns, s->kvm_dirty_ring_bytes);
           if (ret < 0) {
               goto err;
           }
+        cpu->kvm_dirty_gfns = NULL;
       }
 
       kvm_park_vcpu(cpu);
@@ -608,6 +615,31 @@ err:
       return ret;
   }
 
+void kvm_close(void)
+{
+    CPUState *cpu;
+
+    if (!kvm_state || kvm_state->fd == -1) {
+        return;
+    }
+
+    CPU_FOREACH(cpu) {
+        cpu_remove_sync(cpu);
+        close(cpu->kvm_fd);
+        cpu->kvm_fd = -1;
+        close(cpu->kvm_vcpu_stats_fd);
+        cpu->kvm_vcpu_stats_fd = -1;
+    }
+
+    if (kvm_state && kvm_state->fd != -1) {
+        close(kvm_state->vmfd);
+        kvm_state->vmfd = -1;
+        close(kvm_state->fd);
+        kvm_state->fd = -1;
+    }
+    kvm_state = NULL;
+}
+
   /*
    * dirty pages logging control
    */
hw/vfio/cpr-legacy.c +0 -0
index a84c324..daa3523 100644
@@ -177,6 +177,8 @@ bool

vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp)

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

MIG_MODE_CPR_TRANSFER, -1) == 0;

Quoted 3 lines
       }

+ vfio_cpr_add_kvm_notifier();

Hi Steven, I just noticed this, do we need to do same for iommufd?

Yes, and that call is added in patch
"vfio/iommufd: register container for cpr"

Quoted 1 line

Do we need to delete notifier when all VFIO devices hot unplugged?

No need. The notifier will be called, and close the kvm descriptors and vfio_kvm_device_fd. Not strictly necessary if vfio devices are no longer present, but not harmful either.

- Steve

#9 lore raw

RE: [PATCH V6 03/21] migration: close kvm after cpr

Quoted 160 lines

-----Original Message-----
From: Steven Sistare <[email protected]>
Subject: Re: [PATCH V6 03/21] migration: close kvm after cpr

Quoted 11 lines · Zhenzhong
Quoted 11 lines

-----Original Message-----
From: Steven Sistare <[email protected]>
Subject: Re: [PATCH V6 03/21] migration: close kvm after cpr

cc Paolo.

After incorporating Peter's feedback, IMO this version reads well:
* kvm exports kvm_close
* vfio exports vfio_kvm_device_close
* vfio-cpr registers a notifier that calls vfio_kvm_device_close

Quoted 6 lines
Quoted 6 lines

- Steve

Quoted 3 lines · Steve Sistare

cpr-transfer breaks vfio network connectivity to and from the guest, and the host system log shows:
irq bypass consumer (token 00000000a03c32e5) registration fails:

-16

Quoted 17 lines
Quoted 17 lines
Quoted 11 lines

which is EBUSY. This occurs because KVM descriptors are still open in the old QEMU process. Close them.

---
   include/hw/vfio/vfio-cpr.h    |  2 ++
   include/hw/vfio/vfio-device.h |  2 ++
   include/system/kvm.h          |  1 +
   accel/kvm/kvm-all.c           | 32

++++++++++++++++++++++++++++++++

Quoted 5 lines
   hw/vfio/cpr-legacy.c          |  2 ++
   hw/vfio/cpr.c                 | 21 +++++++++++++++++++++
   hw/vfio/helpers.c             | 11 +++++++++++
   7 files changed, 71 insertions(+)
Quoted 5 lines
Quoted 5 lines

include/hw/vfio/vfio-cpr.h void vfio_cpr_delete_vector_fd(struct

Quoted 5 lines
include/hw/vfio/vfio-cpr.h +0 -0
index 25e74ee..099d54f 100644
@@ -62,4 +62,6 @@ void vfio_cpr_delete_vector_fd(struct

VFIOPCIDevice

Quoted 30 lines
Quoted 30 lines

*vdev, const char *name,

include/hw/vfio/vfio-device.h void vfio_device_set_fd(VFIODevice *vbasedev,

Quoted 11 lines
include/hw/vfio/vfio-cpr.h +2 -0
   extern const VMStateDescription vfio_cpr_pci_vmstate;
 
+void vfio_cpr_add_kvm_notifier(void);
+
   #endif /* HW_VFIO_VFIO_CPR_H */
include/hw/vfio/vfio-device.h +0 -0
index c616652..f503837 100644
@@ -283,4 +283,6 @@ void vfio_device_set_fd(VFIODevice *vbasedev,

const char *str, Error **errp);

include/hw/vfio/vfio-device.h void vfio_device_set_fd(VFIODevice *vbasedev, · line 283

Quoted 1 line
include/hw/vfio/vfio-device.h +0 -0
   void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps

*ops,

Quoted 15 lines
include/hw/vfio/vfio-device.h +2 -0
                         DeviceState *dev, bool ram_discard);
   int vfio_device_get_aw_bits(VFIODevice *vdev);
+
+void vfio_kvm_device_close(void);
   #endif /* HW_VFIO_VFIO_COMMON_H */
include/system/kvm.h +1 -0
index 7cc60d2..4896a3c 100644
@@ -195,6 +195,7 @@ bool kvm_has_sync_mmu(void);
   int kvm_has_vcpu_events(void);
   int kvm_max_nested_state_length(void);
   int kvm_has_gsi_routing(void);
+void kvm_close(void);
 
Quoted 7 lines
Quoted 7 lines

accel/kvm/kvm-all.c static int do_kvm_destroy_vcpu(CPUState

Quoted 7 lines
include/system/kvm.h +0 -0
   /**
    * kvm_arm_supports_user_irq
accel/kvm/kvm-all.c +0 -0
index d095d1b..8141854 100644
@@ -515,16 +515,23 @@ static int do_kvm_destroy_vcpu(CPUState

*cpu)

Quoted 4 lines
Quoted 4 lines

accel/kvm/kvm-all.c static int do_kvm_destroy_vcpu(CPUState *cpu)

Quoted 4 lines
accel/kvm/kvm-all.c +1 -0
           goto err;
       }
 
+    /* If I am the CPU that created coalesced_mmio_ring, then discard

it

Quoted 2 lines
Quoted 2 lines

*/

Quoted 1 line
accel/kvm/kvm-all.c +1 -0
+    if (s->coalesced_mmio_ring == (void *)cpu->kvm_run + PAGE_SIZE)

{

Quoted 11 lines
Quoted 11 lines

accel/kvm/kvm-all.c static int do_kvm_destroy_vcpu(CPUState *cpu)

Quoted 11 lines
accel/kvm/kvm-all.c +4 -0
+        s->coalesced_mmio_ring = NULL;
+    }
+
       ret = munmap(cpu->kvm_run, mmap_size);
       if (ret < 0) {
           goto err;
       }
+    cpu->kvm_run = NULL;
 
       if (cpu->kvm_dirty_gfns) {
           ret = munmap(cpu->kvm_dirty_gfns,

s->kvm_dirty_ring_bytes);

Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
accel/kvm/kvm-all.c +26 -0
           if (ret < 0) {
               goto err;
           }
+        cpu->kvm_dirty_gfns = NULL;
       }
 
       kvm_park_vcpu(cpu);
@@ -608,6 +615,31 @@ err:
       return ret;
   }
 
+void kvm_close(void)
+{
+    CPUState *cpu;
+
+    if (!kvm_state || kvm_state->fd == -1) {
+        return;
+    }
+
+    CPU_FOREACH(cpu) {
+        cpu_remove_sync(cpu);
+        close(cpu->kvm_fd);
+        cpu->kvm_fd = -1;
+        close(cpu->kvm_vcpu_stats_fd);
+        cpu->kvm_vcpu_stats_fd = -1;
+    }
+
+    if (kvm_state && kvm_state->fd != -1) {
+        close(kvm_state->vmfd);
+        kvm_state->vmfd = -1;
+        close(kvm_state->fd);
+        kvm_state->fd = -1;
+    }
+    kvm_state = NULL;
+}
+
   /*
Quoted 8 lines
Quoted 8 lines

hw/vfio/cpr-legacy.c bool

Quoted 7 lines
accel/kvm/kvm-all.c +0 -0
    * dirty pages logging control
    */
hw/vfio/cpr-legacy.c +0 -0
index a84c324..daa3523 100644
@@ -177,6 +177,8 @@ bool

vfio_legacy_cpr_register_container(VFIOContainer *container, Error

**errp)

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

MIG_MODE_CPR_TRANSFER, -1) == 0;

Quoted 3 lines
       }

+ vfio_cpr_add_kvm_notifier();

Hi Steven, I just noticed this, do we need to do same for iommufd?

Quoted 8 lines

Yes, and that call is added in patch
"vfio/iommufd: register container for cpr"

Quoted 1 line

Do we need to delete notifier when all VFIO devices hot unplugged?

No need. The notifier will be called, and close the kvm descriptors and vfio_kvm_device_fd. Not strictly necessary if vfio devices are no longer present, but not harmful either.

Clear, no problem.

Thanks
Zhenzhong

#10 lore raw

PATCH v6 4/21 migration: cpr_get_fd_param helper

In reply to Steve Sistare

Add the helper function cpr_get_fd_param, to use when preserving a file descriptor that is opened externally and passed to QEMU. cpr_get_fd_param returns a descriptor number either from a QEMU command-line parameter, from a getfd command, or from CPR state.

When a descriptor is passed to new QEMU via SCM_RIGHTS, its number changes. Hence, during CPR, the command-line parameter is ignored in new QEMU, and over-ridden by the value found in CPR state.

Similarly, if the descriptor was originally specified by a getfd command in old QEMU, the fd number is not known outside of QEMU, and it changes when sent to new QEMU via SCM_RIGHTS. Hence the user cannot send getfd to new QEMU, but when the user sends a hotplug command that references the fd, cpr_get_fd_param finds its value in CPR state.

---
 include/migration/cpr.h |  2 ++
 migration/cpr.c         | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
include/migration/cpr.h +2 -0
index 07858e9..eb27a93 100644
@@ -32,6 +32,8 @@ void cpr_state_close(void);
 struct QIOChannel *cpr_state_ioc(void);
 
 bool cpr_incoming_needed(void *opaque);
+int cpr_get_fd_param(const char *name, const char *fdname, int index,
+                     Error **errp);
 
 QEMUFile *cpr_transfer_output(MigrationChannel *channel, Error **errp);
 QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp);
migration/cpr.c +37 -0
index a50a57e..535d587 100644
@@ -13,6 +13,7 @@
 #include "migration/qemu-file.h"
 #include "migration/savevm.h"
 #include "migration/vmstate.h"
+#include "monitor/monitor.h"
 #include "system/runstate.h"
 #include "trace.h"
 
@@ -264,3 +265,39 @@ bool cpr_incoming_needed(void *opaque)
     MigMode mode = migrate_mode();
     return mode == MIG_MODE_CPR_TRANSFER;
 }
+
+/*
+ * cpr_get_fd_param: find a descriptor and return its value.
+ *
+ * @name: CPR name for the descriptor
+ * @fdname: An integer-valued string, or a name passed to a getfd command
+ * @index: CPR index of the descriptor
+ * @errp: returned error message
+ *
+ * If CPR is not being performed, then use @fdname to find the fd.
+ * If CPR is being performed, then ignore @fdname, and look for @name
+ * and @index in CPR state.
+ *
+ * On success returns the fd value, else returns -1.
+ */
+int cpr_get_fd_param(const char *name, const char *fdname, int index,
+                     Error **errp)
+{
+    ERRP_GUARD();
+    int fd;
+
+    if (cpr_is_incoming()) {
+        fd = cpr_find_fd(name, index);
+        if (fd < 0) {
+            error_setg(errp, "cannot find saved value for fd %s", fdname);
+        }
+    } else {
+        fd = monitor_fd_param(monitor_cur(), fdname, errp);
+        if (fd >= 0) {
+            cpr_save_fd(name, index, fd);
+        } else {
+            error_prepend(errp, "Could not parse object fd %s:", fdname);
+        }
+    }
+    return fd;
+}
Signature · 1 line
1.8.3.1
#11 lore raw

PATCH v6 5/21 backends/iommufd: iommufd_backend_map_file_dma

In reply to Steve Sistare

Define iommufd_backend_map_file_dma to implement IOMMU_IOAS_MAP_FILE. This will be called as a substitute for iommufd_backend_map_dma, so the error conditions for BARs are copied as-is from that function.

---
 include/system/iommufd.h |  3 +++
 backends/iommufd.c       | 34 ++++++++++++++++++++++++++++++++++
 backends/trace-events    |  1 +
 3 files changed, 38 insertions(+)
include/system/iommufd.h +3 -0
index 283861b..2d24d93 100644
@@ -43,6 +43,9 @@ void iommufd_backend_disconnect(IOMMUFDBackend *be);
 bool iommufd_backend_alloc_ioas(IOMMUFDBackend *be, uint32_t *ioas_id,
                                 Error **errp);
 void iommufd_backend_free_id(IOMMUFDBackend *be, uint32_t id);
+int iommufd_backend_map_file_dma(IOMMUFDBackend *be, uint32_t ioas_id,
+                                 hwaddr iova, ram_addr_t size, int fd,
+                                 unsigned long start, bool readonly);
 int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,
                             ram_addr_t size, void *vaddr, bool readonly);
 int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
backends/iommufd.c +34 -0
index c2c47ab..3a2ecc7 100644
@@ -172,6 +172,40 @@ int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,
     return ret;
 }
 
+int iommufd_backend_map_file_dma(IOMMUFDBackend *be, uint32_t ioas_id,
+                                 hwaddr iova, ram_addr_t size,
+                                 int mfd, unsigned long start, bool readonly)
+{
+    int ret, fd = be->fd;
+    struct iommu_ioas_map_file map = {
+        .size = sizeof(map),
+        .flags = IOMMU_IOAS_MAP_READABLE |
+                 IOMMU_IOAS_MAP_FIXED_IOVA,
+        .ioas_id = ioas_id,
+        .fd = mfd,
+        .start = start,
+        .iova = iova,
+        .length = size,
+    };
+
+    if (!readonly) {
+        map.flags |= IOMMU_IOAS_MAP_WRITEABLE;
+    }
+
+    ret = ioctl(fd, IOMMU_IOAS_MAP_FILE, &map);
+    trace_iommufd_backend_map_file_dma(fd, ioas_id, iova, size, mfd, start,
+                                       readonly, ret);
+    if (ret) {
+        ret = -errno;
+
+        /* TODO: Not support mapping hardware PCI BAR region for now. */
+        if (errno == EFAULT) {
+            warn_report("IOMMU_IOAS_MAP_FILE failed: %m, PCI BAR?");
+        }
+    }
+    return ret;
+}
+
 int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
                               hwaddr iova, ram_addr_t size)
 {
backends/trace-events +1 -0
index 7278214..e5f3e70 100644
@@ -11,6 +11,7 @@ iommufd_backend_connect(int fd, bool owned, uint32_t users) "fd=%d owned=%d user
 iommufd_backend_disconnect(int fd, uint32_t users) "fd=%d users=%d"
 iommu_backend_set_fd(int fd) "pre-opened /dev/iommu fd=%d"
 iommufd_backend_map_dma(int iommufd, uint32_t ioas, uint64_t iova, uint64_t size, void *vaddr, bool readonly, int ret) " iommufd=%d ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" addr=%p readonly=%d (%d)"
+iommufd_backend_map_file_dma(int iommufd, uint32_t ioas, uint64_t iova, uint64_t size, int fd, unsigned long start, bool readonly, int ret) " iommufd=%d ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" fd=%d start=%ld readonly=%d (%d)"
 iommufd_backend_unmap_dma_non_exist(int iommufd, uint32_t ioas, uint64_t iova, uint64_t size, int ret) " Unmap nonexistent mapping: iommufd=%d ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" (%d)"
 iommufd_backend_unmap_dma(int iommufd, uint32_t ioas, uint64_t iova, uint64_t size, int ret) " iommufd=%d ioas=%d iova=0x%"PRIx64" size=0x%"PRIx64" (%d)"
 iommufd_backend_alloc_ioas(int iommufd, uint32_t ioas) " iommufd=%d ioas=%d"
Signature · 1 line
1.8.3.1
#12 lore raw

PATCH v6 6/21 backends/iommufd: change process ioctl

In reply to Steve Sistare

Define the change process ioctl

---
 include/system/iommufd.h |  3 +++
 backends/iommufd.c       | 24 ++++++++++++++++++++++++
 backends/trace-events    |  1 +
 3 files changed, 28 insertions(+)
include/system/iommufd.h +3 -0
index 2d24d93..db5f2c7 100644
@@ -69,6 +69,9 @@ bool iommufd_backend_invalidate_cache(IOMMUFDBackend *be, uint32_t id,
                                       uint32_t *entry_num, void *data,
                                       Error **errp);
 
+bool iommufd_change_process_capable(IOMMUFDBackend *be);
+bool iommufd_change_process(IOMMUFDBackend *be, Error **errp);
+
 #define TYPE_HOST_IOMMU_DEVICE_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
 OBJECT_DECLARE_TYPE(HostIOMMUDeviceIOMMUFD, HostIOMMUDeviceIOMMUFDClass,
                     HOST_IOMMU_DEVICE_IOMMUFD)
backends/iommufd.c +24 -0
index 3a2ecc7..87f81a0 100644
@@ -73,6 +73,30 @@ static void iommufd_backend_class_init(ObjectClass *oc, const void *data)
     object_class_property_add_str(oc, "fd", NULL, iommufd_backend_set_fd);
 }
 
+bool iommufd_change_process_capable(IOMMUFDBackend *be)
+{
+    struct iommu_ioas_change_process args = {.size = sizeof(args)};
+
+    /*
+     * Call IOMMU_IOAS_CHANGE_PROCESS to verify it is a recognized ioctl.
+     * This is a no-op if the process has not changed since DMA was mapped.
+     */
+    return !ioctl(be->fd, IOMMU_IOAS_CHANGE_PROCESS, &args);
+}
+
+bool iommufd_change_process(IOMMUFDBackend *be, Error **errp)
+{
+    struct iommu_ioas_change_process args = {.size = sizeof(args)};
+    bool ret = !ioctl(be->fd, IOMMU_IOAS_CHANGE_PROCESS, &args);
+
+    if (!ret) {
+        error_setg_errno(errp, errno, "IOMMU_IOAS_CHANGE_PROCESS fd %d failed",
+                         be->fd);
+    }
+    trace_iommufd_change_process(be->fd, ret);
+    return ret;
+}
+
 bool iommufd_backend_connect(IOMMUFDBackend *be, Error **errp)
 {
     int fd;
backends/trace-events +1 -0
index e5f3e70..56132d3 100644
@@ -7,6 +7,7 @@ dbus_vmstate_loading(const char *id) "id: %s"
 dbus_vmstate_saving(const char *id) "id: %s"
 
 # iommufd.c
+iommufd_change_process(int fd, bool ret) "fd=%d (%d)"
 iommufd_backend_connect(int fd, bool owned, uint32_t users) "fd=%d owned=%d users=%d"
 iommufd_backend_disconnect(int fd, uint32_t users) "fd=%d users=%d"
 iommu_backend_set_fd(int fd) "pre-opened /dev/iommu fd=%d"
Signature · 1 line
1.8.3.1
#13 lore raw

Re: [PATCH V6 06/21] backends/iommufd: change process ioctl

Hello,

I'm running Linux 6.8.12 and when running QEMU 10.1 or above, it segfaults when submitting the IOMMU_IOAS_CHANGE_PROCESS ioctl which is not supported on my kernel. Here is an example strace:

3388260 22:48:30.540879 openat(AT_FDCWD, "/dev/iommu", O_RDWR|O_CLOEXEC) = 25
3388260 22:48:30.540942 ioctl(25, IOMMU_IOAS_CHANGE_PROCESS, 0x7ffe91d96490) = -1 ENOTTY (Inappropriate ioctl for device)
3388260 22:48:30.541003 close(25) = 0
3388260 22:48:30.541059 close(26) = 0
3388260 22:48:30.541120 --- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=NULL} ---

I heard through the grapevine that this is a known issue and was fixed. My question is what QEMU version should I expect this fix to land and roughly what the timeframe for it is (assuming it hasn't been landed already)?

Landon Clipp

#14 lore raw

RE: [PATCH V6 06/21] backends/iommufd: change process ioctl

I think you need to try v6.13 kernel or above.

# git tag --contains 829ed626499c11c9d11c65e93febc1e0da7cd61b
v6.13
v6.13-rc1
v6.13-rc2
v6.13-rc3
v6.13-rc4
v6.13-rc5
v6.13-rc6
v6.13-rc7
v6.14
...
Quoted 25 lines

-----Original Message-----
From: Landon Clipp <[email protected]>
Subject: Re: [PATCH V6 06/21] backends/iommufd: change process ioctl

Hello,

I'm running Linux 6.8.12 and when running QEMU 10.1 or above, it segfaults when submitting the IOMMU_IOAS_CHANGE_PROCESS ioctl which is not supported on my
kernel. Here is an example strace:

3388260 22:48:30.540879 openat(AT_FDCWD, "/dev/iommu", O_RDWR|O_CLOEXEC) = 25
3388260 22:48:30.540942 ioctl(25, IOMMU_IOAS_CHANGE_PROCESS, 0x7ffe91d96490) = -1 ENOTTY (Inappropriate ioctl for device)
3388260 22:48:30.541003 close(25) = 0
3388260 22:48:30.541059 close(26) = 0
3388260 22:48:30.541120 --- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=NULL} ---

I heard through the grapevine that this is a known issue and was fixed. My question is what QEMU version should I expect this fix to land and roughly what the timeframe for it is (assuming it hasn't been landed already)?

Landon Clipp

#15 lore raw

Re: [PATCH V6 06/21] backends/iommufd: change process ioctl

In reply to Landon Clipp

Hello,

Quoted 11 lines · Landon Clipp

Hello,

I'm running Linux 6.8.12 and when running QEMU 10.1 or above, it segfaults when submitting the IOMMU_IOAS_CHANGE_PROCESS ioctl which is not supported on my kernel. Here is an example strace:

3388260 22:48:30.540879 openat(AT_FDCWD, "/dev/iommu", O_RDWR|O_CLOEXEC) = 25
3388260 22:48:30.540942 ioctl(25, IOMMU_IOAS_CHANGE_PROCESS, 0x7ffe91d96490) = -1 ENOTTY (Inappropriate ioctl for device)
3388260 22:48:30.541003 close(25) = 0
3388260 22:48:30.541059 close(26) = 0
3388260 22:48:30.541120 --- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=NULL} ---

a QEMU backtrace would be more useful.

Quoted 3 lines

I heard through the grapevine that this is a known issue and was fixed. My question is what QEMU version should I expect this fix to land and roughly what the timeframe for it is (assuming it hasn't been landed already)?

QEMU shouldn't segv. I believe you are using CPR ?

Please open a gitlab issue on :

https://gitlab.com/qemu-project/qemu/-/work_items

Thanks,

C.

#16 lore raw

PATCH v6 7/21 physmem: qemu_ram_get_fd_offset

In reply to Steve Sistare

Define qemu_ram_get_fd_offset, so CPR can map a memory region using IOMMU_IOAS_MAP_FILE in a subsequent patch.

---
 include/exec/cpu-common.h | 1 +
 system/physmem.c          | 5 +++++
 2 files changed, 6 insertions(+)
include/exec/cpu-common.h +1 -0
index a684855..9b658a3 100644
@@ -85,6 +85,7 @@ void qemu_ram_unset_idstr(RAMBlock *block);
 const char *qemu_ram_get_idstr(RAMBlock *rb);
 void *qemu_ram_get_host_addr(RAMBlock *rb);
 ram_addr_t qemu_ram_get_offset(RAMBlock *rb);
+ram_addr_t qemu_ram_get_fd_offset(RAMBlock *rb);
 ram_addr_t qemu_ram_get_used_length(RAMBlock *rb);
 ram_addr_t qemu_ram_get_max_length(RAMBlock *rb);
 bool qemu_ram_is_shared(RAMBlock *rb);
system/physmem.c +5 -0
index ff0ca40..130c148 100644
@@ -1593,6 +1593,11 @@ ram_addr_t qemu_ram_get_offset(RAMBlock *rb)
     return rb->offset;
 }
 
+ram_addr_t qemu_ram_get_fd_offset(RAMBlock *rb)
+{
+    return rb->fd_offset;
+}
+
 ram_addr_t qemu_ram_get_used_length(RAMBlock *rb)
 {
     return rb->used_length;
Signature · 1 line
1.8.3.1
#17 lore raw

PATCH v6 8/21 vfio/iommufd: use IOMMU_IOAS_MAP_FILE

In reply to Steve Sistare

Use IOMMU_IOAS_MAP_FILE when the mapped region is backed by a file. Such a mapping can be preserved without modification during CPR, because it depends on the file's address space, which does not change, rather than on the process's address space, which does change.

---
 include/hw/vfio/vfio-container-base.h | 15 +++++++++++++++
 hw/vfio/container-base.c              |  9 +++++++++
 hw/vfio/iommufd.c                     | 13 +++++++++++++
 3 files changed, 37 insertions(+)
include/hw/vfio/vfio-container-base.h +15 -0
index 3cd86ec..bded6e9 100644
@@ -168,6 +168,21 @@ struct VFIOIOMMUClass {
                    hwaddr iova, ram_addr_t size,
                    void *vaddr, bool readonly, MemoryRegion *mr);
     /**
+     * @dma_map_file
+     *
+     * Map a file range for the container.
+     *
+     * @bcontainer: #VFIOContainerBase to use for map
+     * @iova: start address to map
+     * @size: size of the range to map
+     * @fd: descriptor of the file to map
+     * @start: starting file offset of the range to map
+     * @readonly: map read only if true
+     */
+    int (*dma_map_file)(const VFIOContainerBase *bcontainer,
+                        hwaddr iova, ram_addr_t size,
+                        int fd, unsigned long start, bool readonly);
+    /**
      * @dma_unmap
      *
      * Unmap an address range from the container.
hw/vfio/container-base.c +9 -0
index d834bd4..5630497 100644
@@ -78,7 +78,16 @@ int vfio_container_dma_map(VFIOContainerBase *bcontainer,
                            void *vaddr, bool readonly, MemoryRegion *mr)
 {
     VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
+    RAMBlock *rb = mr->ram_block;
+    int mfd = rb ? qemu_ram_get_fd(rb) : -1;
 
+    if (mfd >= 0 && vioc->dma_map_file) {
+        unsigned long start = vaddr - qemu_ram_get_host_addr(rb);
+        unsigned long offset = qemu_ram_get_fd_offset(rb);
+
+        return vioc->dma_map_file(bcontainer, iova, size, mfd, start + offset,
+                                  readonly);
+    }
     g_assert(vioc->dma_map);
     return vioc->dma_map(bcontainer, iova, size, vaddr, readonly, mr);
 }
hw/vfio/iommufd.c +13 -0
index d3efef7..962a1e2 100644
@@ -45,6 +45,18 @@ static int iommufd_cdev_map(const VFIOContainerBase *bcontainer, hwaddr iova,
                                    iova, size, vaddr, readonly);
 }
 
+static int iommufd_cdev_map_file(const VFIOContainerBase *bcontainer,
+                                 hwaddr iova, ram_addr_t size,
+                                 int fd, unsigned long start, bool readonly)
+{
+    const VFIOIOMMUFDContainer *container =
+        container_of(bcontainer, VFIOIOMMUFDContainer, bcontainer);
+
+    return iommufd_backend_map_file_dma(container->be,
+                                        container->ioas_id,
+                                        iova, size, fd, start, readonly);
+}
+
 static int iommufd_cdev_unmap(const VFIOContainerBase *bcontainer,
                               hwaddr iova, ram_addr_t size,
                               IOMMUTLBEntry *iotlb, bool unmap_all)
@@ -807,6 +819,7 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, const void *data)
     VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
 
     vioc->dma_map = iommufd_cdev_map;
+    vioc->dma_map_file = iommufd_cdev_map_file;
     vioc->dma_unmap = iommufd_cdev_unmap;
     vioc->attach_device = iommufd_cdev_attach;
     vioc->detach_device = iommufd_cdev_detach;
Signature · 1 line
1.8.3.1
#18 lore raw

PATCH v6 9/21 vfio/iommufd: invariant device name

In reply to Steve Sistare

cpr-transfer will use the device name as a key to find the value of the device descriptor in new QEMU. However, if the descriptor number is specified by a command-line fd parameter, then vfio_device_get_name creates a name that includes the fd number. This causes a chicken-and-egg problem: new QEMU must know the fd number to construct a name to find the fd number.

To fix, create an invariant name based on the id command-line parameter, if id is defined. The user will need to provide such an id to use CPR.

---
 hw/vfio/device.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
hw/vfio/device.c +10 -5
index d91c695..3cd365f 100644
@@ -316,12 +316,17 @@ bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp)
             error_setg(errp, "Use FD passing only with iommufd backend");
             return false;
         }
-        /*
-         * Give a name with fd so any function printing out vbasedev->name
-         * will not break.
-         */
         if (!vbasedev->name) {
-            vbasedev->name = g_strdup_printf("VFIO_FD%d", vbasedev->fd);
+
+            if (vbasedev->dev->id) {
+                vbasedev->name = g_strdup(vbasedev->dev->id);
+                return true;
+            } else {
+                /*
+                 * Assign a name so any function printing it will not break.
+                 */
+                vbasedev->name = g_strdup_printf("VFIO_FD%d", vbasedev->fd);
+            }
         }
     }
 
Signature · 1 line
1.8.3.1
#19 lore raw

PATCH v6 10/21 vfio/iommufd: add vfio_device_free_name

In reply to Steve Sistare

Define vfio_device_free_name to free the name created by vfio_device_get_name. A subsequent patch will do more there. No functional change.

---
 include/hw/vfio/vfio-device.h | 1 +
 hw/vfio/ap.c                  | 4 ++--
 hw/vfio/ccw.c                 | 4 ++--
 hw/vfio/device.c              | 5 +++++
 hw/vfio/pci.c                 | 2 +-
 hw/vfio/platform.c            | 2 +-
 6 files changed, 12 insertions(+), 6 deletions(-)
include/hw/vfio/vfio-device.h +1 -0
index f503837..1901a35 100644
@@ -279,6 +279,7 @@ int vfio_device_get_irq_info(VFIODevice *vbasedev, int index,
 
 /* Returns 0 on success, or a negative errno. */
 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp);
+void vfio_device_free_name(VFIODevice *vbasedev);
 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp);
 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops,
                       DeviceState *dev, bool ram_discard);
hw/vfio/ap.c +2 -2
index 1df4438..7719f24 100644
@@ -265,7 +265,7 @@ static void vfio_ap_realize(DeviceState *dev, Error **errp)
 
 error:
     error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->name);
-    g_free(vbasedev->name);
+    vfio_device_free_name(vbasedev);
 }
 
 static void vfio_ap_unrealize(DeviceState *dev)
@@ -275,7 +275,7 @@ static void vfio_ap_unrealize(DeviceState *dev)
     vfio_ap_unregister_irq_notifier(vapdev, VFIO_AP_REQ_IRQ_INDEX);
     vfio_ap_unregister_irq_notifier(vapdev, VFIO_AP_CFG_CHG_IRQ_INDEX);
     vfio_device_detach(&vapdev->vdev);
-    g_free(vapdev->vdev.name);
+    vfio_device_free_name(&vapdev->vdev);
 }
 
 static const Property vfio_ap_properties[] = {
hw/vfio/ccw.c +2 -2
index cea9d6e..9560b8d 100644
@@ -619,7 +619,7 @@ out_io_notifier_err:
 out_region_err:
     vfio_device_detach(vbasedev);
 out_attach_dev_err:
-    g_free(vbasedev->name);
+    vfio_device_free_name(vbasedev);
 out_unrealize:
     if (cdc->unrealize) {
         cdc->unrealize(cdev);
@@ -637,7 +637,7 @@ static void vfio_ccw_unrealize(DeviceState *dev)
     vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX);
     vfio_ccw_put_region(vcdev);
     vfio_device_detach(&vcdev->vdev);
-    g_free(vcdev->vdev.name);
+    vfio_device_free_name(&vcdev->vdev);
 
     if (cdc->unrealize) {
         cdc->unrealize(cdev);
hw/vfio/device.c +5 -0
index 3cd365f..97eddd0 100644
@@ -333,6 +333,11 @@ bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp)
     return true;
 }
 
+void vfio_device_free_name(VFIODevice *vbasedev)
+{
+    g_clear_pointer(&vbasedev->name, g_free);
+}
+
 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp)
 {
     ERRP_GUARD();
hw/vfio/pci.c +1 -1
index dd0b2a0..1093b28 100644
@@ -2996,7 +2996,7 @@ void vfio_pci_put_device(VFIOPCIDevice *vdev)
 
     vfio_device_detach(&vdev->vbasedev);
 
-    g_free(vdev->vbasedev.name);
+    vfio_device_free_name(&vdev->vbasedev);
     g_free(vdev->msix);
 }
 
hw/vfio/platform.c +1 -1
index 9a21f2e..5c1795a 100644
@@ -530,7 +530,7 @@ static bool vfio_base_device_init(VFIODevice *vbasedev, Error **errp)
 {
     /* @fd takes precedence over @sysfsdev which takes precedence over @host */
     if (vbasedev->fd < 0 && vbasedev->sysfsdev) {
-        g_free(vbasedev->name);
+        vfio_device_free_name(vbasedev);
         vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
     } else if (vbasedev->fd < 0) {
         if (!vbasedev->name || strchr(vbasedev->name, '/')) {
Signature · 1 line
1.8.3.1
#20 lore raw

PATCH v6 11/21 vfio/iommufd: device name blocker

In reply to Steve Sistare

If an invariant device name cannot be created, block CPR.

---
 include/hw/vfio/vfio-cpr.h |  1 +
 hw/vfio/device.c           | 11 +++++++++++
 2 files changed, 12 insertions(+)
include/hw/vfio/vfio-cpr.h +1 -0
index 099d54f..76eafc0 100644
@@ -29,6 +29,7 @@ typedef struct VFIOContainerCPR {
 
 typedef struct VFIODeviceCPR {
     Error *mdev_blocker;
+    Error *id_blocker;
 } VFIODeviceCPR;
 
 bool vfio_legacy_cpr_register_container(struct VFIOContainer *container,
hw/vfio/device.c +11 -0
index 97eddd0..0ae3f3c 100644
@@ -28,6 +28,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/units.h"
+#include "migration/cpr.h"
+#include "migration/blocker.h"
 #include "monitor/monitor.h"
 #include "vfio-helpers.h"
 
@@ -324,8 +326,16 @@ bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp)
             } else {
                 /*
                  * Assign a name so any function printing it will not break.
+                 * The fd number changes across processes, so this cannot be
+                 * used as an invariant name for CPR.
                  */
                 vbasedev->name = g_strdup_printf("VFIO_FD%d", vbasedev->fd);
+                error_setg(&vbasedev->cpr.id_blocker,
+                           "vfio device with fd=%d needs an id property",
+                           vbasedev->fd);
+                return migrate_add_blocker_modes(&vbasedev->cpr.id_blocker,
+                                                 errp, MIG_MODE_CPR_TRANSFER,
+                                                 -1) == 0;
             }
         }
     }
@@ -336,6 +346,7 @@ bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp)
 void vfio_device_free_name(VFIODevice *vbasedev)
 {
     g_clear_pointer(&vbasedev->name, g_free);
+    migrate_del_blocker(&vbasedev->cpr.id_blocker);
 }
 
 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp)
Signature · 1 line
1.8.3.1
#21 lore raw

PATCH v6 12/21 vfio/iommufd: register container for cpr

In reply to Steve Sistare

Register a vfio iommufd container and device for CPR, replacing the generic CPR register call with a more specific iommufd register call. Add a blocker if the kernel does not support IOMMU_IOAS_CHANGE_PROCESS.

This is mostly boiler plate. The fields to to saved and restored are added in subsequent patches.

---
 include/hw/vfio/vfio-cpr.h | 12 +++++++
 include/system/iommufd.h   |  1 +
 backends/iommufd.c         | 10 ++++++
 hw/vfio/cpr-iommufd.c      | 86 ++++++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/iommufd.c          |  6 ++--
 hw/vfio/meson.build        |  1 +
 6 files changed, 114 insertions(+), 2 deletions(-)
 create mode 100644 hw/vfio/cpr-iommufd.c
include/hw/vfio/vfio-cpr.h +12 -0
index 76eafc0..e0e3ee2 100644
@@ -15,7 +15,10 @@
 struct VFIOContainer;
 struct VFIOContainerBase;
 struct VFIOGroup;
+struct VFIODevice;
 struct VFIOPCIDevice;
+struct VFIOIOMMUFDContainer;
+struct IOMMUFDBackend;
 
 typedef struct VFIOContainerCPR {
     Error *blocker;
@@ -43,6 +46,15 @@ bool vfio_cpr_register_container(struct VFIOContainerBase *bcontainer,
                                  Error **errp);
 void vfio_cpr_unregister_container(struct VFIOContainerBase *bcontainer);
 
+bool vfio_iommufd_cpr_register_container(struct VFIOIOMMUFDContainer *container,
+                                         Error **errp);
+void vfio_iommufd_cpr_unregister_container(
+    struct VFIOIOMMUFDContainer *container);
+bool vfio_iommufd_cpr_register_iommufd(struct IOMMUFDBackend *be, Error **errp);
+void vfio_iommufd_cpr_unregister_iommufd(struct IOMMUFDBackend *be);
+void vfio_iommufd_cpr_register_device(struct VFIODevice *vbasedev);
+void vfio_iommufd_cpr_unregister_device(struct VFIODevice *vbasedev);
+
 int vfio_cpr_group_get_device_fd(int d, const char *name);
 
 bool vfio_cpr_container_match(struct VFIOContainer *container,
include/system/iommufd.h +1 -0
index db5f2c7..c9c72ff 100644
@@ -32,6 +32,7 @@ struct IOMMUFDBackend {
     /*< protected >*/
     int fd;            /* /dev/iommu file descriptor */
     bool owned;        /* is the /dev/iommu opened internally */
+    Error *cpr_blocker;/* set if be does not support CPR */
     uint32_t users;
 
     /*< public >*/
backends/iommufd.c +10 -0
index 87f81a0..c554ce5 100644
@@ -108,6 +108,13 @@ bool iommufd_backend_connect(IOMMUFDBackend *be, Error **errp)
         }
         be->fd = fd;
     }
+    if (!be->users && !vfio_iommufd_cpr_register_iommufd(be, errp)) {
+        if (be->owned) {
+            close(be->fd);
+            be->fd = -1;
+        }
+        return false;
+    }
     be->users++;
 
     trace_iommufd_backend_connect(be->fd, be->owned, be->users);
@@ -125,6 +132,9 @@ void iommufd_backend_disconnect(IOMMUFDBackend *be)
         be->fd = -1;
     }
 out:
+    if (!be->users) {
+        vfio_iommufd_cpr_unregister_iommufd(be);
+    }
     trace_iommufd_backend_disconnect(be->fd, be->users);
 }
 
hw/vfio/cpr-iommufd.c new file +86 -0

86 lines not on this page · raw message

hw/vfio/iommufd.c +4 -2
index 962a1e2..ff291be 100644
@@ -446,7 +446,7 @@ static void iommufd_cdev_container_destroy(VFIOIOMMUFDContainer *container)
     if (!QLIST_EMPTY(&bcontainer->device_list)) {
         return;
     }
-    vfio_cpr_unregister_container(bcontainer);
+    vfio_iommufd_cpr_unregister_container(container);
     vfio_listener_unregister(bcontainer);
     iommufd_backend_free_id(container->be, container->ioas_id);
     object_unref(container);
@@ -592,7 +592,7 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
         goto err_listener_register;
     }
 
-    if (!vfio_cpr_register_container(bcontainer, errp)) {
+    if (!vfio_iommufd_cpr_register_container(container, errp)) {
         goto err_listener_register;
     }
 
@@ -623,6 +623,7 @@ found_container:
     }
 
     vfio_device_prepare(vbasedev, bcontainer, &dev_info);
+    vfio_iommufd_cpr_register_device(vbasedev);
 
     trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
                                    vbasedev->num_regions, vbasedev->flags);
@@ -660,6 +661,7 @@ static void iommufd_cdev_detach(VFIODevice *vbasedev)
     iommufd_cdev_container_destroy(container);
     vfio_address_space_put(space);
 
+    vfio_iommufd_cpr_unregister_device(vbasedev);
     iommufd_cdev_unbind_and_disconnect(vbasedev);
     close(vbasedev->fd);
 }
hw/vfio/meson.build +1 -0
index 63ea393..7a88174 100644
@@ -31,6 +31,7 @@ system_ss.add(when: 'CONFIG_VFIO', if_true: files(
 ))
 system_ss.add(when: ['CONFIG_VFIO', 'CONFIG_IOMMUFD'], if_true: files(
   'iommufd.c',
+  'cpr-iommufd.c',
 ))
 system_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
   'display.c',
Signature · 1 line
1.8.3.1
Reviewed-by on v6 12/21
#22 lore raw

RE: [PATCH V6 12/21] vfio/iommufd: register container for cpr

Quoted 6 lines

-----Original Message-----
From: Steve Sistare <[email protected]>
Subject: [PATCH V6 12/21] vfio/iommufd: register container for cpr

Quoted 8 lines

Register a vfio iommufd container and device for CPR, replacing the generic CPR register call with a more specific iommufd register call. Add a blocker if the kernel does not support IOMMU_IOAS_CHANGE_PROCESS.

This is mostly boiler plate. The fields to to saved and restored are added in subsequent patches.

Thanks
Zhenzhong

#23 lore raw

PATCH v6 13/21 migration: vfio cpr state hook

In reply to Steve Sistare

Define a list of vfio devices in CPR state, in a subsection so that older QEMU can be live updated to this version. However, new QEMU will not be live updateable to old QEMU. This is acceptable because CPR is not yet commonly used, and updates to older versions are unusual.

The contents of each device object will be defined by the vfio subsystem in a subsequent patch.

---
 include/hw/vfio/vfio-cpr.h |  1 +
 include/migration/cpr.h    | 12 ++++++++++++
 hw/vfio/cpr-iommufd.c      |  2 ++
 hw/vfio/iommufd-stubs.c    | 18 ++++++++++++++++++
 migration/cpr.c            | 15 ++++++---------
 hw/vfio/meson.build        |  1 +
 6 files changed, 40 insertions(+), 9 deletions(-)
 create mode 100644 hw/vfio/iommufd-stubs.c
include/hw/vfio/vfio-cpr.h +1 -0
index e0e3ee2..c94d5e0 100644
@@ -74,6 +74,7 @@ void vfio_cpr_delete_vector_fd(struct VFIOPCIDevice *vdev, const char *name,
                                int nr);
 
 extern const VMStateDescription vfio_cpr_pci_vmstate;
+extern const VMStateDescription vmstate_cpr_vfio_devices;
 
 void vfio_cpr_add_kvm_notifier(void);
 
include/migration/cpr.h +12 -0
index eb27a93..3fc19a7 100644
@@ -9,11 +9,23 @@
 #define MIGRATION_CPR_H
 
 #include "qapi/qapi-types-migration.h"
+#include "qemu/queue.h"
 
 #define MIG_MODE_NONE           -1
 
 #define QEMU_CPR_FILE_MAGIC     0x51435052
 #define QEMU_CPR_FILE_VERSION   0x00000001
+#define CPR_STATE "CprState"
+
+typedef QLIST_HEAD(CprFdList, CprFd) CprFdList;
+typedef QLIST_HEAD(CprVFIODeviceList, CprVFIODevice) CprVFIODeviceList;
+
+typedef struct CprState {
+    CprFdList fds;
+    CprVFIODeviceList vfio_devices;
+} CprState;
+
+extern CprState cpr_state;
 
 void cpr_save_fd(const char *name, int id, int fd);
 void cpr_delete_fd(const char *name, int id);
hw/vfio/cpr-iommufd.c +2 -0
index 2f58b43..f95773b 100644
@@ -14,6 +14,8 @@
 #include "system/iommufd.h"
 #include "vfio-iommufd.h"
 
+const VMStateDescription vmstate_cpr_vfio_devices;  /* TBD in a later patch */
+
 static bool vfio_cpr_supported(IOMMUFDBackend *be, Error **errp)
 {
     if (!iommufd_change_process_capable(be)) {
hw/vfio/iommufd-stubs.c new file +18 -0
new file mode 100644
index 0000000..0be5276
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2025 Oracle and/or its affiliates.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "migration/cpr.h"
+#include "migration/vmstate.h"
+
+const VMStateDescription vmstate_cpr_vfio_devices = {
+    .name = CPR_STATE "/vfio devices",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (const VMStateField[]){
+        VMSTATE_END_OF_LIST()
+    }
+};
migration/cpr.c +6 -9
index 535d587..42ad0b0 100644
@@ -7,6 +7,7 @@
 
 #include "qemu/osdep.h"
 #include "qapi/error.h"
+#include "hw/vfio/vfio-device.h"
 #include "migration/cpr.h"
 #include "migration/misc.h"
 #include "migration/options.h"
@@ -20,13 +21,7 @@
 /*************************************************************************/
 /* cpr state container for all information to be saved. */
 
-typedef QLIST_HEAD(CprFdList, CprFd) CprFdList;
-
-typedef struct CprState {
-    CprFdList fds;
-} CprState;
-
-static CprState cpr_state;
+CprState cpr_state;
 
 /****************************************************************************/
 
@@ -127,8 +122,6 @@ int cpr_open_fd(const char *path, int flags, const char *name, int id,
 }
 
 /*************************************************************************/
-#define CPR_STATE "CprState"
-
 static const VMStateDescription vmstate_cpr_state = {
     .name = CPR_STATE,
     .version_id = 1,
@@ -136,6 +129,10 @@ static const VMStateDescription vmstate_cpr_state = {
     .fields = (VMStateField[]) {
         VMSTATE_QLIST_V(fds, CprState, 1, vmstate_cpr_fd, CprFd, next),
         VMSTATE_END_OF_LIST()
+    },
+    .subsections = (const VMStateDescription * const []) {
+        &vmstate_cpr_vfio_devices,
+        NULL
     }
 };
 /*************************************************************************/
hw/vfio/meson.build +1 -0
index 7a88174..bfaf6be 100644
@@ -33,6 +33,7 @@ system_ss.add(when: ['CONFIG_VFIO', 'CONFIG_IOMMUFD'], if_true: files(
   'iommufd.c',
   'cpr-iommufd.c',
 ))
+system_ss.add(when: 'CONFIG_IOMMUFD', if_false: files('iommufd-stubs.c'))
 system_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files(
   'display.c',
 ))
Signature · 1 line
1.8.3.1
Reviewed-by on v6 13/21
#24 lore raw

RE: [PATCH V6 13/21] migration: vfio cpr state hook

Quoted 11 lines

-----Original Message-----
From: Steve Sistare <[email protected]>
Subject: [PATCH V6 13/21] migration: vfio cpr state hook

Define a list of vfio devices in CPR state, in a subsection so that older QEMU can be live updated to this version. However, new QEMU will not be live updateable to old QEMU. This is acceptable because CPR is not yet commonly used, and updates to older versions are unusual.

Quoted 4 lines

The contents of each device object will be defined by the vfio subsystem in a subsequent patch.

Thanks
Zhenzhong

#25 lore raw

PATCH v6 14/21 vfio/iommufd: cpr state

In reply to Steve Sistare

VFIO iommufd devices will need access to ioas_id, devid, and hwpt_id in new QEMU at realize time, so add them to CPR state. Define CprVFIODevice as the object which holds the state and is serialized to the vmstate file. Define accessors to copy state between VFIODevice and CprVFIODevice.

---
 include/hw/vfio/vfio-cpr.h |  3 ++
 hw/vfio/cpr-iommufd.c      | 98 +++++++++++++++++++++++++++++++++++++++++++++-
 hw/vfio/iommufd.c          |  2 +
 hw/vfio/trace-events       |  3 ++
 4 files changed, 105 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-cpr.h +3 -0
index c94d5e0..4c17cb3 100644
@@ -33,6 +33,8 @@ typedef struct VFIOContainerCPR {
 typedef struct VFIODeviceCPR {
     Error *mdev_blocker;
     Error *id_blocker;
+    uint32_t hwpt_id;
+    uint32_t ioas_id;
 } VFIODeviceCPR;
 
 bool vfio_legacy_cpr_register_container(struct VFIOContainer *container,
@@ -54,6 +56,7 @@ bool vfio_iommufd_cpr_register_iommufd(struct IOMMUFDBackend *be, Error **errp);
 void vfio_iommufd_cpr_unregister_iommufd(struct IOMMUFDBackend *be);
 void vfio_iommufd_cpr_register_device(struct VFIODevice *vbasedev);
 void vfio_iommufd_cpr_unregister_device(struct VFIODevice *vbasedev);
+void vfio_cpr_load_device(struct VFIODevice *vbasedev);
 
 int vfio_cpr_group_get_device_fd(int d, const char *name);
 
hw/vfio/cpr-iommufd.c +97 -1

119 lines not on this page · raw message

hw/vfio/iommufd.c +2 -0
index ff291be..f0d57ea 100644
@@ -515,6 +515,8 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
     const VFIOIOMMUClass *iommufd_vioc =
         VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
 
+    vfio_cpr_load_device(vbasedev);
+
     if (vbasedev->fd < 0) {
         devfd = iommufd_cdev_getfd(vbasedev->sysfsdev, errp);
         if (devfd < 0) {
hw/vfio/trace-events +3 -0
index e1728c4..8ec0ad0 100644
@@ -197,6 +197,9 @@ iommufd_cdev_alloc_ioas(int iommufd, int ioas_id) " [iommufd=%d] new IOMMUFD con
 iommufd_cdev_device_info(char *name, int devfd, int num_irqs, int num_regions, int flags) " %s (%d) num_irqs=%d num_regions=%d flags=%d"
 iommufd_cdev_pci_hot_reset_dep_devices(int domain, int bus, int slot, int function, int dev_id) "\t%04x:%02x:%02x.%x devid %d"
 
+# cpr-iommufd.c
+vfio_cpr_find_device(uint32_t ioas_id, int devid, uint32_t hwpt_id) "ioas_id %u, devid %d, hwpt_id %u"
+
 # device.c
 vfio_device_get_region_info_type(const char *name, int index, uint32_t type, uint32_t subtype) "%s index %d, %08x/%08x"
 vfio_device_reset_handler(void) ""
Signature · 1 line
1.8.3.1
#26 lore raw

PATCH v6 15/21 vfio/iommufd: preserve descriptors

In reply to Steve Sistare

Save the iommu and vfio device fd in CPR state when it is created. After CPR, the fd number is found in CPR state and reused.

---
 backends/iommufd.c    | 35 +++++++++++++++++++++++++++++------
 hw/vfio/cpr-iommufd.c | 10 ++++++++++
 hw/vfio/device.c      |  9 +--------
 3 files changed, 40 insertions(+), 14 deletions(-)
backends/iommufd.c +29 -6
index c554ce5..e091792 100644
@@ -16,12 +16,18 @@
 #include "qemu/module.h"
 #include "qom/object_interfaces.h"
 #include "qemu/error-report.h"
+#include "migration/cpr.h"
 #include "monitor/monitor.h"
 #include "trace.h"
 #include "hw/vfio/vfio-device.h"
 #include <sys/ioctl.h>
 #include <linux/iommufd.h>
 
+static const char *iommufd_fd_name(IOMMUFDBackend *be)
+{
+    return object_get_canonical_path_component(OBJECT(be));
+}
+
 static void iommufd_backend_init(Object *obj)
 {
     IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
@@ -64,11 +70,27 @@ static bool iommufd_backend_can_be_deleted(UserCreatable *uc)
     return !be->users;
 }
 
+static void iommufd_backend_complete(UserCreatable *uc, Error **errp)
+{
+    IOMMUFDBackend *be = IOMMUFD_BACKEND(uc);
+    const char *name = iommufd_fd_name(be);
+
+    if (!be->owned) {
+        /* fd came from the command line. Fetch updated value from cpr state. */
+        if (cpr_is_incoming()) {
+            be->fd = cpr_find_fd(name, 0);
+        } else {
+            cpr_save_fd(name, 0, be->fd);
+        }
+    }
+}
+
 static void iommufd_backend_class_init(ObjectClass *oc, const void *data)
 {
     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
 
     ucc->can_be_deleted = iommufd_backend_can_be_deleted;
+    ucc->complete = iommufd_backend_complete;
 
     object_class_property_add_str(oc, "fd", NULL, iommufd_backend_set_fd);
 }
@@ -102,7 +124,7 @@ bool iommufd_backend_connect(IOMMUFDBackend *be, Error **errp)
     int fd;
 
     if (be->owned && !be->users) {
-        fd = qemu_open("/dev/iommu", O_RDWR, errp);
+        fd = cpr_open_fd("/dev/iommu", O_RDWR, iommufd_fd_name(be), 0, errp);
         if (fd < 0) {
             return false;
         }
@@ -127,14 +149,15 @@ void iommufd_backend_disconnect(IOMMUFDBackend *be)
         goto out;
     }
     be->users--;
-    if (!be->users && be->owned) {
-        close(be->fd);
-        be->fd = -1;
-    }
-out:
     if (!be->users) {
         vfio_iommufd_cpr_unregister_iommufd(be);
+        if (be->owned) {
+            cpr_delete_fd(iommufd_fd_name(be), 0);
+            close(be->fd);
+            be->fd = -1;
+        }
     }
+out:
     trace_iommufd_backend_disconnect(be->fd, be->users);
 }
 
hw/vfio/cpr-iommufd.c +10 -0
index 4166201..a72b68d 100644
@@ -166,12 +166,18 @@ void vfio_iommufd_cpr_unregister_container(VFIOIOMMUFDContainer *container)
 void vfio_iommufd_cpr_register_device(VFIODevice *vbasedev)
 {
     if (!cpr_is_incoming()) {
+        /*
+         * Beware fd may have already been saved by vfio_device_set_fd,
+         * so call resave to avoid a duplicate entry.
+         */
+        cpr_resave_fd(vbasedev->name, 0, vbasedev->fd);
         vfio_cpr_save_device(vbasedev);
     }
 }
 
 void vfio_iommufd_cpr_unregister_device(VFIODevice *vbasedev)
 {
+    cpr_delete_fd(vbasedev->name, 0);
     vfio_cpr_delete_device(vbasedev->name);
 }
 
@@ -180,5 +186,9 @@ void vfio_cpr_load_device(VFIODevice *vbasedev)
     if (cpr_is_incoming()) {
         bool ret = vfio_cpr_find_device(vbasedev);
         g_assert(ret);
+
+        if (vbasedev->fd < 0) {
+            vbasedev->fd = cpr_find_fd(vbasedev->name, 0);
+        }
     }
 }
hw/vfio/device.c +1 -8
index 0ae3f3c..96cf214 100644
@@ -351,14 +351,7 @@ void vfio_device_free_name(VFIODevice *vbasedev)
 
 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp)
 {
-    ERRP_GUARD();
-    int fd = monitor_fd_param(monitor_cur(), str, errp);
-
-    if (fd < 0) {
-        error_prepend(errp, "Could not parse remote object fd %s:", str);
-        return;
-    }
-    vbasedev->fd = fd;
+    vbasedev->fd = cpr_get_fd_param(vbasedev->dev->id, str, 0, errp);
 }
 
 static VFIODeviceIOOps vfio_device_io_ops_ioctl;
Signature · 1 line
1.8.3.1
#27 lore raw

PATCH v6 16/21 vfio/iommufd: reconstruct device

In reply to Steve Sistare

Reconstruct userland device state after CPR. During vfio_realize, skip all ioctls that configure the device, as it was already configured in old QEMU.

Skip bind, and use the devid from CPR state.

Skip allocation of, and attachment to, ioas_id. Recover ioas_id from CPR state, and use it to find a matching container, if any, before creating a new one.

This reconstruction is not complete. hwpt_id is handled in a subsequent patch.

---
 hw/vfio/iommufd.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
hw/vfio/iommufd.c +28 -2
index f0d57ea..a650517 100644
@@ -25,6 +25,7 @@
 #include "system/reset.h"
 #include "qemu/cutils.h"
 #include "qemu/chardev_open.h"
+#include "migration/cpr.h"
 #include "pci.h"
 #include "vfio-iommufd.h"
 #include "vfio-helpers.h"
@@ -121,6 +122,10 @@ static bool iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp)
         goto err_kvm_device_add;
     }
 
+    if (cpr_is_incoming()) {
+        goto skip_bind;
+    }
+
     /* Bind device to iommufd */
     bind.iommufd = iommufd->fd;
     if (ioctl(vbasedev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind)) {
@@ -132,6 +137,8 @@ static bool iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp)
     vbasedev->devid = bind.out_devid;
     trace_iommufd_cdev_connect_and_bind(bind.iommufd, vbasedev->name,
                                         vbasedev->fd, vbasedev->devid);
+
+skip_bind:
     return true;
 err_bind:
     iommufd_cdev_kvm_device_del(vbasedev);
@@ -421,7 +428,9 @@ static bool iommufd_cdev_attach_container(VFIODevice *vbasedev,
         return iommufd_cdev_autodomains_get(vbasedev, container, errp);
     }
 
-    return !iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp);
+    /* If CPR, we are already attached to ioas_id. */
+    return cpr_is_incoming() ||
+           !iommufd_cdev_attach_ioas_hwpt(vbasedev, container->ioas_id, errp);
 }
 
 static void iommufd_cdev_detach_container(VFIODevice *vbasedev,
@@ -510,6 +519,7 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
     VFIOAddressSpace *space;
     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
     int ret, devfd;
+    bool res;
     uint32_t ioas_id;
     Error *err = NULL;
     const VFIOIOMMUClass *iommufd_vioc =
@@ -540,7 +550,16 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
             vbasedev->iommufd != container->be) {
             continue;
         }
-        if (!iommufd_cdev_attach_container(vbasedev, container, &err)) {
+
+        if (!cpr_is_incoming()) {
+            res = iommufd_cdev_attach_container(vbasedev, container, &err);
+        } else if (vbasedev->cpr.ioas_id == container->ioas_id) {
+            res = true;
+        } else {
+            continue;
+        }
+
+        if (!res) {
             const char *msg = error_get_pretty(err);
 
             trace_iommufd_cdev_fail_attach_existing_container(msg);
@@ -557,6 +576,11 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
         }
     }
 
+    if (cpr_is_incoming()) {
+        ioas_id = vbasedev->cpr.ioas_id;
+        goto skip_ioas_alloc;
+    }
+
     /* Need to allocate a new dedicated container */
     if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) {
         goto err_alloc_ioas;
@@ -564,10 +588,12 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
 
     trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id);
 
+skip_ioas_alloc:
     container = VFIO_IOMMU_IOMMUFD(object_new(TYPE_VFIO_IOMMU_IOMMUFD));
     container->be = vbasedev->iommufd;
     container->ioas_id = ioas_id;
     QLIST_INIT(&container->hwpt_list);
+    vbasedev->cpr.ioas_id = ioas_id;
 
     bcontainer = &container->bcontainer;
     vfio_address_space_insert(space, bcontainer);
Signature · 1 line
1.8.3.1
#28 lore raw

PATCH v6 17/21 vfio/iommufd: reconstruct hwpt

In reply to Steve Sistare

Skip allocation of, and attachment to, hwpt_id. Recover it from CPR state.

---
 hw/vfio/iommufd.c | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)
hw/vfio/iommufd.c +22 -8
index a650517..48c590b 100644
@@ -332,7 +332,14 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
 
     /* Try to find a domain */
     QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
-        ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
+        if (!cpr_is_incoming()) {
+            ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
+        } else if (vbasedev->cpr.hwpt_id == hwpt->hwpt_id) {
+            ret = 0;
+        } else {
+            continue;
+        }
+
         if (ret) {
             /* -EINVAL means the domain is incompatible with the device. */
             if (ret == -EINVAL) {
@@ -349,6 +356,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
             return false;
         } else {
             vbasedev->hwpt = hwpt;
+            vbasedev->cpr.hwpt_id = hwpt->hwpt_id;
             QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
             vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
             return true;
@@ -371,6 +379,11 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
         flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
     }
 
+    if (cpr_is_incoming()) {
+        hwpt_id = vbasedev->cpr.hwpt_id;
+        goto skip_alloc;
+    }
+
     if (!iommufd_backend_alloc_hwpt(iommufd, vbasedev->devid,
                                     container->ioas_id, flags,
                                     IOMMU_HWPT_DATA_NONE, 0, NULL,
@@ -378,19 +391,20 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
         return false;
     }
 
+    ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt_id, errp);
+    if (ret) {
+        iommufd_backend_free_id(container->be, hwpt_id);
+        return false;
+    }
+
+skip_alloc:
     hwpt = g_malloc0(sizeof(*hwpt));
     hwpt->hwpt_id = hwpt_id;
     hwpt->hwpt_flags = flags;
     QLIST_INIT(&hwpt->device_list);
 
-    ret = iommufd_cdev_attach_ioas_hwpt(vbasedev, hwpt->hwpt_id, errp);
-    if (ret) {
-        iommufd_backend_free_id(container->be, hwpt->hwpt_id);
-        g_free(hwpt);
-        return false;
-    }
-
     vbasedev->hwpt = hwpt;
+    vbasedev->cpr.hwpt_id = hwpt->hwpt_id;
     vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
     QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
     QLIST_INSERT_HEAD(&container->hwpt_list, hwpt, next);
Signature · 1 line
1.8.3.1
#29 lore raw

PATCH v6 18/21 vfio/iommufd: change process

In reply to Steve Sistare

Finish CPR by change the owning process of the iommufd device in post load.

---
 hw/vfio/cpr-iommufd.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
hw/vfio/cpr-iommufd.c +30 -0
index a72b68d..cbeab57 100644
@@ -112,10 +112,40 @@ static bool vfio_cpr_supported(IOMMUFDBackend *be, Error **errp)
     return true;
 }
 
+static int iommufd_cpr_pre_save(void *opaque)
+{
+    IOMMUFDBackend *be = opaque;
+
+    /*
+     * The process has not changed yet, but proactively try the ioctl,
+     * and it will fail if any DMA mappings are not supported.
+     */
+    if (!iommufd_change_process_capable(be)) {
+        error_report("some memory regions do not support "
+                     "IOMMU_IOAS_CHANGE_PROCESS");
+        return -1;
+    }
+    return 0;
+}
+
+static int iommufd_cpr_post_load(void *opaque, int version_id)
+{
+     IOMMUFDBackend *be = opaque;
+     Error *local_err = NULL;
+
+     if (!iommufd_change_process(be, &local_err)) {
+        error_report_err(local_err);
+        return -1;
+     }
+     return 0;
+}
+
 static const VMStateDescription iommufd_cpr_vmstate = {
     .name = "iommufd",
     .version_id = 0,
     .minimum_version_id = 0,
+    .pre_save = iommufd_cpr_pre_save,
+    .post_load = iommufd_cpr_post_load,
     .needed = cpr_incoming_needed,
     .fields = (VMStateField[]) {
         VMSTATE_END_OF_LIST()
Signature · 1 line
1.8.3.1
#30 lore raw

PATCH v6 19/21 iommufd: preserve DMA mappings

In reply to Steve Sistare

During cpr-transfer load in new QEMU, the vfio_memory_listener causes spurious calls to map and unmap DMA regions, as devices are created and the address space is built. This memory was already already mapped by the device in old QEMU, so suppress the map and unmap callbacks during incoming CPR.

---
 backends/iommufd.c | 8 ++++++++
 1 file changed, 8 insertions(+)
backends/iommufd.c +8 -0
index e091792..2a33c7a 100644
@@ -245,6 +245,10 @@ int iommufd_backend_map_file_dma(IOMMUFDBackend *be, uint32_t ioas_id,
         .length = size,
     };
 
+    if (cpr_is_incoming()) {
+        return 0;
+    }
+
     if (!readonly) {
         map.flags |= IOMMU_IOAS_MAP_WRITEABLE;
     }
@@ -274,6 +278,10 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
         .length = size,
     };
 
+    if (cpr_is_incoming()) {
+        return 0;
+    }
+
     ret = ioctl(fd, IOMMU_IOAS_UNMAP, &unmap);
     /*
      * IOMMUFD takes mapping as some kind of object, unmapping
Signature · 1 line
1.8.3.1
#31 lore raw

PATCH v6 20/21 vfio/container: delete old cpr register

In reply to Steve Sistare

vfio_cpr_[un]register_container is no longer used since they were subsumed by container type-specific registration. Delete them.

---
 include/hw/vfio/vfio-cpr.h |  4 ----
 hw/vfio/cpr.c              | 13 -------------
 2 files changed, 17 deletions(-)
include/hw/vfio/vfio-cpr.h +0 -4
index 4c17cb3..e9cd9b2 100644
@@ -44,10 +44,6 @@ void vfio_legacy_cpr_unregister_container(struct VFIOContainer *container);
 int vfio_cpr_reboot_notifier(NotifierWithReturn *notifier, MigrationEvent *e,
                              Error **errp);
 
-bool vfio_cpr_register_container(struct VFIOContainerBase *bcontainer,
-                                 Error **errp);
-void vfio_cpr_unregister_container(struct VFIOContainerBase *bcontainer);
-
 bool vfio_iommufd_cpr_register_container(struct VFIOIOMMUFDContainer *container,
                                          Error **errp);
 void vfio_iommufd_cpr_unregister_container(
hw/vfio/cpr.c +0 -13
index 0e903cd..af0f12a 100644
@@ -29,19 +29,6 @@ int vfio_cpr_reboot_notifier(NotifierWithReturn *notifier,
     return 0;
 }
 
-bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp)
-{
-    migration_add_notifier_mode(&bcontainer->cpr_reboot_notifier,
-                                vfio_cpr_reboot_notifier,
-                                MIG_MODE_CPR_REBOOT);
-    return true;
-}
-
-void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer)
-{
-    migration_remove_notifier(&bcontainer->cpr_reboot_notifier);
-}
-
 #define STRDUP_VECTOR_FD_NAME(vdev, name)   \
     g_strdup_printf("%s_%s", (vdev)->vbasedev.name, (name))
 
Signature · 1 line
1.8.3.1
#32 lore raw

PATCH v6 21/21 vfio: doc changes for cpr

In reply to Steve Sistare

Update documentation to say that cpr-transfer supports vfio and iommufd.

---
 docs/devel/migration/CPR.rst | 5 ++---
 qapi/migration.json          | 6 ++++--
 2 files changed, 6 insertions(+), 5 deletions(-)
docs/devel/migration/CPR.rst +2 -3
index 7897873..0a0fd4f 100644
@@ -152,8 +152,7 @@ cpr-transfer mode
 This mode allows the user to transfer a guest to a new QEMU instance
 on the same host with minimal guest pause time, by preserving guest
 RAM in place, albeit with new virtual addresses in new QEMU.  Devices
-and their pinned memory pages will also be preserved in a future QEMU
-release.
+and their pinned memory pages are also preserved for VFIO and IOMMUFD.
 
 The user starts new QEMU on the same host as old QEMU, with command-
 line arguments to create the same machine, plus the ``-incoming``
@@ -322,6 +321,6 @@ Futures
 
 cpr-transfer mode is based on a capability to transfer open file
 descriptors from old to new QEMU.  In the future, descriptors for
-vfio, iommufd, vhost, and char devices could be transferred,
+vhost, and char devices could be transferred,
 preserving those devices and their kernel state without interruption,
 even if they do not explicitly support live migration.
qapi/migration.json +4 -2
index 4963f6c..e8a7d3b 100644
@@ -620,8 +620,10 @@
 #
 # @cpr-transfer: This mode allows the user to transfer a guest to a
 #     new QEMU instance on the same host with minimal guest pause
-#     time by preserving guest RAM in place.  Devices and their pinned
-#     pages will also be preserved in a future QEMU release.
+#     time by preserving guest RAM in place.
+#
+#     Devices and their pinned pages are also preserved for VFIO and
+#     IOMMUFD. (since 10.1)
 #
 #     The user starts new QEMU on the same host as old QEMU, with
 #     command-line arguments to create the same machine, plus the
Signature · 1 line
1.8.3.1
#33 lore raw
In reply to Steve Sistare
Quoted 189 lines · Steve Sistare

NOTE: this V6 series depends on the patch
vfio-user: do not register vfio-user container with cpr
which is in vfio-next.

Support vfio and iommufd devices with the cpr-transfer live migration mode. Devices that do not support live migration can still support cpr-transfer, allowing live update to a new version of QEMU on the same host, with no loss of guest connectivity.

No user-visible interfaces are added.

For legacy containers:

Pass vfio device descriptors to new QEMU. In new QEMU, during vfio_realize, skip the ioctls that configure the device, because it is already configured.

Use VFIO_DMA_UNMAP_FLAG_VADDR to abandon the old VA's for DMA mapped regions, and use VFIO_DMA_MAP_FLAG_VADDR to register the new VA in new QEMU and update the locked memory accounting. The physical pages remain pinned, because the descriptor of the device that locked them remains open, so DMA to those pages continues without interruption. Mediated devices are not supported, however, because they require the VA to always be valid, and there is a brief window where no VA is registered.

Save the MSI message area as part of vfio-pci vmstate, and pass the interrupt and notifier eventfd's to new QEMU. New QEMU loads the MSI data, then the vfio-pci post_load handler finds the eventfds in CPR state, rebuilds vector data structures, and attaches the interrupts to the new KVM instance. This logic also applies to iommufd containers.

For iommufd containers:

Use IOMMU_IOAS_MAP_FILE to register memory regions for DMA when they are backed by a file (including a memfd), so DMA mappings do not depend on VA, which can differ after live update. This allows mediated devices to be supported.

Pass the iommufd and vfio device descriptors from old to new QEMU. In new QEMU, during vfio_realize, skip the ioctls that configure the device, because it is already configured.

In new QEMU, call ioctl(IOMMU_IOAS_CHANGE_PROCESS) to update mm ownership and locked memory accounting.

Patches 3 to 8 are specific to legacy containers. Patches 21 to 36 are specific to iommufd containers. The remainder apply to both.

Changes from previous versions:
* V1 of this series contains minor changes from the "Live update: vfio" and "Live update: iommufd" series, mainly bug fixes and refactored patches.

Changes in V2:
* refactored various vfio code snippets into new cpr helpers
* refactored vfio struct members into cpr-specific structures
* refactored various small changes into their own patches
* split complex patches. Notably:
- split "refactor for cpr" into 5 patches
- split "reconstruct device" into 4 patches
* refactored vfio_connect_container using helpers and made its error recovery more robust.
* moved vfio pci msi/vector/intx cpr functions to cpr.c
* renamed "reused" to cpr_reused and cpr.reused
* squashed vfio_cpr_[un]register_container to their call sites
* simplified iommu_type setting after cpr
* added cpr_open_fd and cpr_is_incoming helpers
* removed changes from vfio_legacy_dma_map, and instead temporarily override dma_map and dma_unmap ops.
* deleted error_report and returned Error to callers where possible.
* simplified the memory_get_xlat_addr interface
* fixed flags passed to iommufd_backend_alloc_hwpt
* defined MIG_PRI_UNINITIALIZED
* added maintainers

Changes in V3:
* removed cleanup patches that were already pulled
* rebased to latest master

Changes in V4:
* added SPDX-License-Identifier
* patch "vfio/container: preserve descriptors"
- rewrote search loop in vfio_container_connect
- do not return pfd from vfio_cpr_container_match
- add helper for VFIO_GROUP_GET_DEVICE_FD
* deleted patch "export vfio_legacy_dma_map"
* patch "vfio/container: restore DMA vaddr"
- deleted redundant error_report from vfio_legacy_cpr_dma_map
- save old dma_map function
* patch "vfio-pci: skip reset during cpr"
- use cpr_is_incoming instead of cpr_reused
* renamed err -> local_err in all new code
* patch "export MSI functions"
- renamed with vfio_pci prefix, and defined wrappers for low level routines instead of exporting them.
* patch "close kvm after cpr"
- fixed build error for !CONFIG_KVM
* added the cpr_resave_fd helper
* dropped patch "pass ramblock to vfio_container_dma_map", relying on "pass MemoryRegion" from the vfio-user series instead.
* deleted "reused" variables, replaced with cpr_is_incoming()
* renamed cpr_needed_for_reuse -> cpr_incoming_needed
* rewrote patch "pci: skip reset during cpr"
* rebased to latest master

for iommufd:
* deleted redundant error_report from iommufd_backend_map_file_dma
* added interface doc for dma_map_file
* check return value of cpr_open_fd
* deleted "export iommufd_cdev_get_info_iova_range"
* deleted "reconstruct device"
* deleted "reconstruct hw_caps"
* deleted "define hwpt constructors"
* seperated cpr registration for iommufd be and vfio container
* correctly attach to multiple containers per iommufd using ioas_id
* simplified "reconstruct hwpt" by matching against hwpt_id.
* added patch "add vfio_device_free_name"

Changes in V5:
* dropped: vfio/pci: vfio_pci_put_device on failure
* added: "vfio: doc changes for cpr"
* deleted unnecessary include of vfio-cpr.h
* fixed compilation for !CONFIG_VFIO and !CONFIG_IOMMUFD
* misc minor changes
* Added RB's, rebased to master

Changes in V6:
* dropped already-pulled patches
* converted remaining g_free in "add vfio_device_free_name"
* fixed iommufd_backend_disconnect in "preserve descriptors"
* tweaked vfio_cpr_load_device in "preserve descriptors"
* added trace_vfio_cpr_find_device in "cpr state"
* rewrote vfio_notifier_init and vfio_msix_vector_use
* rewrote the notifier in "close kvm after cpr"
* Added RB's, rebased to master

Steve Sistare (21):
vfio-pci: preserve MSI
vfio-pci: preserve INTx
migration: close kvm after cpr
migration: cpr_get_fd_param helper
backends/iommufd: iommufd_backend_map_file_dma
backends/iommufd: change process ioctl
physmem: qemu_ram_get_fd_offset
vfio/iommufd: use IOMMU_IOAS_MAP_FILE
vfio/iommufd: invariant device name
vfio/iommufd: add vfio_device_free_name
vfio/iommufd: device name blocker
vfio/iommufd: register container for cpr
migration: vfio cpr state hook
vfio/iommufd: cpr state
vfio/iommufd: preserve descriptors
vfio/iommufd: reconstruct device
vfio/iommufd: reconstruct hwpt
vfio/iommufd: change process
iommufd: preserve DMA mappings
vfio/container: delete old cpr register
vfio: doc changes for cpr

  docs/devel/migration/CPR.rst          |   5 +-
  qapi/migration.json                   |   6 +-
  hw/vfio/pci.h                         |   2 +
  include/exec/cpu-common.h             |   1 +
  include/hw/vfio/vfio-container-base.h |  15 +++
  include/hw/vfio/vfio-cpr.h            |  29 ++++-
  include/hw/vfio/vfio-device.h         |   3 +
  include/migration/cpr.h               |  14 +++
  include/system/iommufd.h              |   7 ++
  include/system/kvm.h                  |   1 +
  accel/kvm/kvm-all.c                   |  32 +++++
  backends/iommufd.c                    | 107 +++++++++++++++-
  hw/vfio/ap.c                          |   4 +-
  hw/vfio/ccw.c                         |   4 +-
  hw/vfio/container-base.c              |   9 ++
  hw/vfio/cpr-iommufd.c                 | 224 ++++++++++++++++++++++++++++++++++
  hw/vfio/cpr-legacy.c                  |   2 +
  hw/vfio/cpr.c                         | 144 ++++++++++++++++++++--
  hw/vfio/device.c                      |  40 ++++--
  hw/vfio/helpers.c                     |  11 ++
  hw/vfio/iommufd-stubs.c               |  18 +++
  hw/vfio/iommufd.c                     |  81 ++++++++++--
  hw/vfio/pci.c                         | 109 ++++++++++++++++-
  hw/vfio/platform.c                    |   2 +-
  migration/cpr.c                       |  52 ++++++--
  system/physmem.c                      |   5 +
  backends/trace-events                 |   2 +
  hw/vfio/meson.build                   |   2 +
  hw/vfio/trace-events                  |   3 +
  29 files changed, 871 insertions(+), 63 deletions(-)
Quoted 3 lines

create mode 100644 hw/vfio/cpr-iommufd.c
create mode 100644 hw/vfio/iommufd-stubs.c

Applied to vfio-next.

Thanks,

C.