Skip to contentSkip to conversation

vfio: Add SaveVMHanlders for VFIO device to support live migration

RFC PATCH In review Yulei Zhang 6 participants 12 messages Apr 10, 2018 – May 10, 2018

lore
Share & tools
lore
#0 lore raw

RFC PATCH v4 3/4 vfio: Add SaveVMHanlders for VFIO device to support live migration

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

Introduce new Ioctl VFIO_DEVICE_GET_DIRTY_BITMAP to fetch the memory bitmap that dirtied by vfio device during the iterative precopy stage to shorten the system downtime afterward.

For vfio pci device status migrate, during the system downtime, it will save the following states
1. pci configuration space addr0~addr5
2. pci configuration space msi_addr msi_data
3. pci device status fetch from device driver

And on the target side the vfio_load will restore the same states
1. re-setup the pci bar configuration
2. re-setup the pci device msi configuration
3. restore the pci device status

---
 hw/vfio/pci.c              | 195 +++++++++++++++++++++++++++++++++++++++++++--
 linux-headers/linux/vfio.h |  14 ++++
 2 files changed, 204 insertions(+), 5 deletions(-)
hw/vfio/pci.c +190 -5
index 13d8c73..ac6a9c7 100644
@@ -33,9 +33,14 @@
 #include "trace.h"
 #include "qapi/error.h"
 #include "migration/blocker.h"
+#include "migration/register.h"
+#include "exec/ram_addr.h"
 
 #define MSIX_CAP_LENGTH 12
 
+#define VFIO_SAVE_FLAG_SETUP 0
+#define VFIO_SAVE_FLAG_DEV_STATE 1
+
 static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
 static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
 static void vfio_vm_change_state_handler(void *pv, int running, RunState state);
@@ -2639,6 +2644,190 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+static uint64_t vfio_dirty_log_sync(VFIOPCIDevice *vdev)
+{
+    RAMBlock *block;
+    struct vfio_device_get_dirty_bitmap *d;
+    uint64_t page = 0;
+    ram_addr_t size;
+    unsigned long nr, bitmap;
+
+    RAMBLOCK_FOREACH(block) {
+        size = block->used_length;
+        nr = size >> TARGET_PAGE_BITS;
+        bitmap = (BITS_TO_LONGS(nr) + 1) * sizeof(unsigned long);
+        d = g_malloc0(sizeof(*d) +  bitmap);
+        d->start_addr = block->offset;
+        d->page_nr = nr;
+        if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_DIRTY_BITMAP, d)) {
+            error_report("vfio: Failed to get device dirty bitmap");
+            g_free(d);
+            goto exit;
+        }
+
+        if (d->page_nr) {
+            cpu_physical_memory_set_dirty_lebitmap(
+                                 (unsigned long *)&d->dirty_bitmap,
+                                 d->start_addr, d->page_nr);
+            page += d->page_nr;
+        }
+        g_free(d);
+    }
+
+exit:
+    return page;
+}
+
+static void vfio_save_live_pending(QEMUFile *f, void *opaque, uint64_t max_size,
+                   uint64_t *non_postcopiable_pending,
+                   uint64_t *postcopiable_pending)
+{
+    VFIOPCIDevice *vdev = opaque;
+    uint64_t pending;
+
+    qemu_mutex_lock_iothread();
+    rcu_read_lock();
+    pending = vfio_dirty_log_sync(vdev);
+    rcu_read_unlock();
+    qemu_mutex_unlock_iothread();
+    *non_postcopiable_pending += pending;
+}
+
+static int vfio_load(QEMUFile *f, void *opaque, int version_id)
+{
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int sz = vdev->device_state.size - VFIO_DEVICE_STATE_OFFSET;
+    uint8_t *buf = NULL;
+    uint32_t ctl, msi_lo, msi_hi, msi_data, bar_cfg, i;
+    bool msi_64bit;
+
+    if (qemu_get_byte(f) == VFIO_SAVE_FLAG_SETUP) {
+        goto exit;
+    }
+
+    /* retore pci bar configuration */
+    ctl = pci_default_read_config(pdev, PCI_COMMAND, 2);
+    vfio_pci_write_config(pdev, PCI_COMMAND,
+                          ctl & (!(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)), 2);
+    for (i = 0; i < PCI_ROM_SLOT; i++) {
+        bar_cfg = qemu_get_be32(f);
+        vfio_pci_write_config(pdev, PCI_BASE_ADDRESS_0 + i * 4, bar_cfg, 4);
+    }
+    vfio_pci_write_config(pdev, PCI_COMMAND,
+                          ctl | PCI_COMMAND_IO | PCI_COMMAND_MEMORY, 2);
+
+    /* restore msi configuration */
+    ctl = pci_default_read_config(pdev, pdev->msi_cap + PCI_MSI_FLAGS, 2);
+    msi_64bit = !!(ctl & PCI_MSI_FLAGS_64BIT);
+
+    vfio_pci_write_config(&vdev->pdev,
+                          pdev->msi_cap + PCI_MSI_FLAGS,
+                          ctl & (!PCI_MSI_FLAGS_ENABLE), 2);
+
+    msi_lo = qemu_get_be32(f);
+    vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_LO, msi_lo, 4);
+
+    if (msi_64bit) {
+        msi_hi = qemu_get_be32(f);
+        vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_HI,
+                              msi_hi, 4);
+    }
+    msi_data = qemu_get_be32(f);
+    vfio_pci_write_config(pdev,
+              pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32),
+              msi_data, 2);
+
+    vfio_pci_write_config(&vdev->pdev, pdev->msi_cap + PCI_MSI_FLAGS,
+                          ctl | PCI_MSI_FLAGS_ENABLE, 2);
+
+    buf = g_malloc(sz);
+    if (buf == NULL) {
+        error_report("vfio: Failed to allocate memory for migrate");
+        return -1;
+    }
+
+    qemu_get_buffer(f, buf, sz);
+    if (pwrite(vdev->vbasedev.fd, buf, sz,
+               vdev->device_state.offset + VFIO_DEVICE_STATE_OFFSET) != sz) {
+        error_report("vfio: Failed to write Device State Region");
+        return -1;
+    }
+
+    g_free(buf);
+
+exit:
+    return 0;
+}
+
+static int vfio_save_complete(QEMUFile *f, void *opaque)
+{
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int sz = vdev->device_state.size - VFIO_DEVICE_STATE_OFFSET;
+    uint8_t *buf = NULL;
+    uint32_t msi_cfg, msi_lo, msi_hi, msi_data, bar_cfg, i;
+    bool msi_64bit;
+
+    qemu_put_byte(f, VFIO_SAVE_FLAG_DEV_STATE);
+
+    for (i = 0; i < PCI_ROM_SLOT; i++) {
+        bar_cfg = pci_default_read_config(pdev, PCI_BASE_ADDRESS_0 + i * 4, 4);
+        qemu_put_be32(f, bar_cfg);
+    }
+
+    msi_cfg = pci_default_read_config(pdev, pdev->msi_cap + PCI_MSI_FLAGS, 2);
+    msi_64bit = !!(msi_cfg & PCI_MSI_FLAGS_64BIT);
+
+    msi_lo = pci_default_read_config(pdev,
+                                     pdev->msi_cap + PCI_MSI_ADDRESS_LO, 4);
+    qemu_put_be32(f, msi_lo);
+
+    if (msi_64bit) {
+        msi_hi = pci_default_read_config(pdev,
+                                         pdev->msi_cap + PCI_MSI_ADDRESS_HI,
+                                         4);
+        qemu_put_be32(f, msi_hi);
+    }
+
+    msi_data = pci_default_read_config(pdev,
+               pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32),
+               2);
+    qemu_put_be32(f, msi_data);
+
+    buf = g_malloc(sz);
+    if (buf == NULL) {
+        error_report("vfio: Failed to allocate memory for migrate");
+        goto exit;
+    }
+
+    if (pread(vdev->vbasedev.fd, buf, sz,
+              vdev->device_state.offset + VFIO_DEVICE_STATE_OFFSET) != sz) {
+        error_report("vfio: Failed to read Device State Region");
+        goto exit;
+    }
+
+    qemu_put_buffer(f, buf, sz);
+
+exit:
+    g_free(buf);
+
+    return 0;
+}
+
+static int vfio_save_setup(QEMUFile *f, void *opaque)
+{
+    qemu_put_byte(f, VFIO_SAVE_FLAG_SETUP);
+    return 0;
+}
+
+static SaveVMHandlers savevm_vfio_handlers = {
+    .save_setup = vfio_save_setup,
+    .save_live_pending = vfio_save_live_pending,
+    .save_live_complete_precopy = vfio_save_complete,
+    .load_state = vfio_load,
+};
+
 static void vfio_realize(PCIDevice *pdev, Error **errp)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
@@ -2898,6 +3087,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
     vfio_register_req_notifier(vdev);
     vfio_setup_resetfn_quirk(vdev);
     qemu_add_vm_change_state_handler(vfio_vm_change_state_handler, vdev);
+    register_savevm_live(NULL, "vfio-pci", 0, 1, &savevm_vfio_handlers, vdev);
 
     return;
 
@@ -3051,10 +3241,6 @@ static Property vfio_pci_dev_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
-static const VMStateDescription vfio_pci_vmstate = {
-    .name = "vfio-pci",
-};
-
 static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
@@ -3062,7 +3248,6 @@ static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 
     dc->reset = vfio_pci_reset;
     dc->props = vfio_pci_dev_properties;
-    dc->vmsd = &vfio_pci_vmstate;
     dc->desc = "VFIO-based PCI device assignment";
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
     pdc->realize = vfio_realize;
linux-headers/linux/vfio.h +14 -0
index 8f02f2f..2c911d9 100644
@@ -512,6 +512,20 @@ struct vfio_pci_hot_reset {
 
 #define VFIO_DEVICE_PCI_HOT_RESET	_IO(VFIO_TYPE, VFIO_BASE + 13)
 
+/**
+ * VFIO_DEVICE_GET_DIRTY_BITMAP - _IOW(VFIO_TYPE, VFIO_BASE + 14,
+ *				    struct vfio_device_get_dirty_bitmap)
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_device_get_dirty_bitmap {
+	__u64	       start_addr;
+	__u64	       page_nr;
+	__u8           dirty_bitmap[];
+};
+
+#define VFIO_DEVICE_GET_DIRTY_BITMAP	_IO(VFIO_TYPE, VFIO_BASE + 14)
+
 /* -------- API for Type1 VFIO IOMMU -------- */
 
 /**
Signature · 1 line
2.7.4
#1 lore raw
Quoted 13 lines · Yulei Zhang

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

Introduce new Ioctl VFIO_DEVICE_GET_DIRTY_BITMAP to fetch the memory bitmap that dirtied by vfio device during the iterative precopy stage to shorten the system downtime afterward.

For vfio pci device status migrate, during the system downtime, it will save the following states
1. pci configuration space addr0~addr5
2. pci configuration space msi_addr msi_data
3. pci device status fetch from device driver

Quoted 5 lines

And on the target side the vfio_load will restore the same states
1. re-setup the pci bar configuration
2. re-setup the pci device msi configuration
3. restore the pci device status

Can #1 and #2 be setup by vendor driver? Vendor driver knows capabilities of device and vendor driver can setup rather than have the restore code in common place and then handle all the capability cases here.

Quoted 68 lines
---
 hw/vfio/pci.c              | 195 +++++++++++++++++++++++++++++++++++++++++++--
 linux-headers/linux/vfio.h |  14 ++++
 2 files changed, 204 insertions(+), 5 deletions(-)
hw/vfio/pci.c +44 -0
index 13d8c73..ac6a9c7 100644
@@ -33,9 +33,14 @@
 #include "trace.h"
 #include "qapi/error.h"
 #include "migration/blocker.h"
+#include "migration/register.h"
+#include "exec/ram_addr.h"
 
 #define MSIX_CAP_LENGTH 12
 
+#define VFIO_SAVE_FLAG_SETUP 0
+#define VFIO_SAVE_FLAG_DEV_STATE 1
+
 static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
 static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
 static void vfio_vm_change_state_handler(void *pv, int running, RunState state);
@@ -2639,6 +2644,190 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+static uint64_t vfio_dirty_log_sync(VFIOPCIDevice *vdev)
+{
+    RAMBlock *block;
+    struct vfio_device_get_dirty_bitmap *d;
+    uint64_t page = 0;
+    ram_addr_t size;
+    unsigned long nr, bitmap;
+
+    RAMBLOCK_FOREACH(block) {
+        size = block->used_length;
+        nr = size >> TARGET_PAGE_BITS;
+        bitmap = (BITS_TO_LONGS(nr) + 1) * sizeof(unsigned long);
+        d = g_malloc0(sizeof(*d) +  bitmap);
+        d->start_addr = block->offset;
+        d->page_nr = nr;
+        if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_DIRTY_BITMAP, d)) {
+            error_report("vfio: Failed to get device dirty bitmap");
+            g_free(d);
+            goto exit;
+        }
+
+        if (d->page_nr) {
+            cpu_physical_memory_set_dirty_lebitmap(
+                                 (unsigned long *)&d->dirty_bitmap,
+                                 d->start_addr, d->page_nr);
+            page += d->page_nr;
+        }
+        g_free(d);
+    }
+
+exit:
+    return page;
+}
+
+static void vfio_save_live_pending(QEMUFile *f, void *opaque, uint64_t max_size,
+                   uint64_t *non_postcopiable_pending,
+                   uint64_t *postcopiable_pending)
+{
+    VFIOPCIDevice *vdev = opaque;

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2695

Quoted 10 lines
hw/vfio/pci.c +10 -0
+    uint64_t pending;
+
+    qemu_mutex_lock_iothread();
+    rcu_read_lock();
+    pending = vfio_dirty_log_sync(vdev);
+    rcu_read_unlock();
+    qemu_mutex_unlock_iothread();
+    *non_postcopiable_pending += pending;
+}
+

This doesn't provide a way to read device's state during pre-copy phase. device_state region can be used to read device specific data from vendor driver during pre-copy phase. That could be done by mmap device_state region during init and then ioctl vendor driver here to query size of data copied to device_state region and pending data size.

Quoted 51 lines
hw/vfio/pci.c +51 -0
+static int vfio_load(QEMUFile *f, void *opaque, int version_id)
+{
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int sz = vdev->device_state.size - VFIO_DEVICE_STATE_OFFSET;
+    uint8_t *buf = NULL;
+    uint32_t ctl, msi_lo, msi_hi, msi_data, bar_cfg, i;
+    bool msi_64bit;
+
+    if (qemu_get_byte(f) == VFIO_SAVE_FLAG_SETUP) {
+        goto exit;
+    }
+
+    /* retore pci bar configuration */
+    ctl = pci_default_read_config(pdev, PCI_COMMAND, 2);
+    vfio_pci_write_config(pdev, PCI_COMMAND,
+                          ctl & (!(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)), 2);
+    for (i = 0; i < PCI_ROM_SLOT; i++) {
+        bar_cfg = qemu_get_be32(f);
+        vfio_pci_write_config(pdev, PCI_BASE_ADDRESS_0 + i * 4, bar_cfg, 4);
+    }
+    vfio_pci_write_config(pdev, PCI_COMMAND,
+                          ctl | PCI_COMMAND_IO | PCI_COMMAND_MEMORY, 2);
+
+    /* restore msi configuration */
+    ctl = pci_default_read_config(pdev, pdev->msi_cap + PCI_MSI_FLAGS, 2);
+    msi_64bit = !!(ctl & PCI_MSI_FLAGS_64BIT);
+
+    vfio_pci_write_config(&vdev->pdev,
+                          pdev->msi_cap + PCI_MSI_FLAGS,
+                          ctl & (!PCI_MSI_FLAGS_ENABLE), 2);
+
+    msi_lo = qemu_get_be32(f);
+    vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_LO, msi_lo, 4);
+
+    if (msi_64bit) {
+        msi_hi = qemu_get_be32(f);
+        vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_HI,
+                              msi_hi, 4);
+    }
+    msi_data = qemu_get_be32(f);
+    vfio_pci_write_config(pdev,
+              pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32),
+              msi_data, 2);
+
+    vfio_pci_write_config(&vdev->pdev, pdev->msi_cap + PCI_MSI_FLAGS,
+                          ctl | PCI_MSI_FLAGS_ENABLE, 2);
+
+    buf = g_malloc(sz);
+    if (buf == NULL) {
+        error_report("vfio: Failed to allocate memory for migrate");

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2756

Quoted 10 lines
hw/vfio/pci.c +10 -0
+        return -1;
+    }
+
+    qemu_get_buffer(f, buf, sz);
+    if (pwrite(vdev->vbasedev.fd, buf, sz,
+               vdev->device_state.offset + VFIO_DEVICE_STATE_OFFSET) != sz) {
+        error_report("vfio: Failed to write Device State Region");
+        return -1;
+    }
+

Again here if device_state region in mmaped, QEMU would write buffer to that region and an ioclt can be used to convey that new data is written to the region along with size of data.

Quoted 43 lines
hw/vfio/pci.c +43 -0
+    g_free(buf);
+
+exit:
+    return 0;
+}
+
+static int vfio_save_complete(QEMUFile *f, void *opaque)
+{
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int sz = vdev->device_state.size - VFIO_DEVICE_STATE_OFFSET;
+    uint8_t *buf = NULL;
+    uint32_t msi_cfg, msi_lo, msi_hi, msi_data, bar_cfg, i;
+    bool msi_64bit;
+
+    qemu_put_byte(f, VFIO_SAVE_FLAG_DEV_STATE);
+
+    for (i = 0; i < PCI_ROM_SLOT; i++) {
+        bar_cfg = pci_default_read_config(pdev, PCI_BASE_ADDRESS_0 + i * 4, 4);
+        qemu_put_be32(f, bar_cfg);
+    }
+
+    msi_cfg = pci_default_read_config(pdev, pdev->msi_cap + PCI_MSI_FLAGS, 2);
+    msi_64bit = !!(msi_cfg & PCI_MSI_FLAGS_64BIT);
+
+    msi_lo = pci_default_read_config(pdev,
+                                     pdev->msi_cap + PCI_MSI_ADDRESS_LO, 4);
+    qemu_put_be32(f, msi_lo);
+
+    if (msi_64bit) {
+        msi_hi = pci_default_read_config(pdev,
+                                         pdev->msi_cap + PCI_MSI_ADDRESS_HI,
+                                         4);
+        qemu_put_be32(f, msi_hi);
+    }
+
+    msi_data = pci_default_read_config(pdev,
+               pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32),
+               2);
+    qemu_put_be32(f, msi_data);
+
+    buf = g_malloc(sz);
+    if (buf == NULL) {

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2809

Quoted 10 lines
hw/vfio/pci.c +10 -0
+        error_report("vfio: Failed to allocate memory for migrate");
+        goto exit;
+    }
+
+    if (pread(vdev->vbasedev.fd, buf, sz,
+              vdev->device_state.offset + VFIO_DEVICE_STATE_OFFSET) != sz) {
+        error_report("vfio: Failed to read Device State Region");
+        goto exit;
+    }
+

Again here getting buffer information from vendor driver and directly reading that data from mmaped region would help.

Quoted 11 lines
hw/vfio/pci.c +11 -0
+    qemu_put_buffer(f, buf, sz);
+
+exit:
+    g_free(buf);
+
+    return 0;
+}
+
+static int vfio_save_setup(QEMUFile *f, void *opaque)
+{
+    qemu_put_byte(f, VFIO_SAVE_FLAG_SETUP);

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2830

Quoted 10 lines
hw/vfio/pci.c +10 -0
+    return 0;
+}
+
+static SaveVMHandlers savevm_vfio_handlers = {
+    .save_setup = vfio_save_setup,
+    .save_live_pending = vfio_save_live_pending,
+    .save_live_complete_precopy = vfio_save_complete,
+    .load_state = vfio_load,
+};
+

Isn't .is_active, .save_live_iterate and .cleanup required? What is vendor driver have large amount of data in device's memory which vendor driver is aware of? and vendor driver would required multiple iterations to send that data to QEMU to save complete state of device?

Isn't there should be an ioclt to convey QEMU's MIGRATION_STATUS_* to vendor driver? Knowing migration status would help vendor driver take necessary action, like MIGRATION_STATUS_SETUP, MIGRATION_STATUS_CANCELLED, MIGRATION_STATUS_FAILED, MIGRATION_STATUS_COMPLETED, I think these are important states that vendor driver should know.

Thanks,
Kirti

Quoted 56 lines
hw/vfio/pci.c +1 -5
 static void vfio_realize(PCIDevice *pdev, Error **errp)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
@@ -2898,6 +3087,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
     vfio_register_req_notifier(vdev);
     vfio_setup_resetfn_quirk(vdev);
     qemu_add_vm_change_state_handler(vfio_vm_change_state_handler, vdev);
+    register_savevm_live(NULL, "vfio-pci", 0, 1, &savevm_vfio_handlers, vdev);
 
     return;
 
@@ -3051,10 +3241,6 @@ static Property vfio_pci_dev_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
-static const VMStateDescription vfio_pci_vmstate = {
-    .name = "vfio-pci",
-};
-
 static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
@@ -3062,7 +3248,6 @@ static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 
     dc->reset = vfio_pci_reset;
     dc->props = vfio_pci_dev_properties;
-    dc->vmsd = &vfio_pci_vmstate;
     dc->desc = "VFIO-based PCI device assignment";
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
     pdc->realize = vfio_realize;
linux-headers/linux/vfio.h +14 -0
index 8f02f2f..2c911d9 100644
@@ -512,6 +512,20 @@ struct vfio_pci_hot_reset {
 
 #define VFIO_DEVICE_PCI_HOT_RESET	_IO(VFIO_TYPE, VFIO_BASE + 13)
 
+/**
+ * VFIO_DEVICE_GET_DIRTY_BITMAP - _IOW(VFIO_TYPE, VFIO_BASE + 14,
+ *				    struct vfio_device_get_dirty_bitmap)
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_device_get_dirty_bitmap {
+	__u64	       start_addr;
+	__u64	       page_nr;
+	__u8           dirty_bitmap[];
+};
+
+#define VFIO_DEVICE_GET_DIRTY_BITMAP	_IO(VFIO_TYPE, VFIO_BASE + 14)
+
 /* -------- API for Type1 VFIO IOMMU -------- */
 
 /**
#2 lore raw
Quoted 28 lines

-----Original Message-----
From: Kirti Wankhede [mailto:[email protected]]
Sent: Monday, April 16, 2018 10:45 PM
To: Zhang, Yulei <[email protected]>; [email protected]
Cc: Tian, Kevin <[email protected]>; [email protected]; [email protected]; Wang, Zhi A <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC PATCH V4 3/4] vfio: Add SaveVMHanlders for VFIO device to support live migration

Quoted 4 lines · Yulei Zhang

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

Introduce new Ioctl VFIO_DEVICE_GET_DIRTY_BITMAP to fetch the

memory

Quoted 8 lines

bitmap that dirtied by vfio device during the iterative precopy stage to shorten the system downtime afterward.

For vfio pci device status migrate, during the system downtime, it will save the following states 1. pci configuration space addr0~addr5
2. pci configuration space msi_addr msi_data 3. pci device status fetch from device driver

Quoted 9 lines
Quoted 4 lines

And on the target side the vfio_load will restore the same states 1. re-setup the pci bar configuration 2. re-setup the pci device msi configuration 3. restore the pci device status

Can #1 and #2 be setup by vendor driver? Vendor driver knows capabilities of device and vendor driver can setup rather than have the restore code in common place and then handle all the capability cases here.

We need vfio to help setup the interrupt patch.

Quoted 77 lines
Quoted 4 lines
---
 hw/vfio/pci.c              | 195

+++++++++++++++++++++++++++++++++++++++++++--

Quoted 20 lines
 linux-headers/linux/vfio.h |  14 ++++
 2 files changed, 204 insertions(+), 5 deletions(-)
hw/vfio/pci.c index 13d8c73..ac6a9c7 renamed from hw/vfio/pci.c +0 -0

100644

hw/vfio/pci.c +5 -0
@@ -33,9 +33,14 @@
 #include "trace.h"
 #include "qapi/error.h"
 #include "migration/blocker.h"
+#include "migration/register.h"
+#include "exec/ram_addr.h"
 
 #define MSIX_CAP_LENGTH 12
 
+#define VFIO_SAVE_FLAG_SETUP 0
+#define VFIO_SAVE_FLAG_DEV_STATE 1
+
 static void vfio_disable_interrupts(VFIOPCIDevice *vdev);  static
Quoted 3 lines

void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled); static void vfio_vm_change_state_handler(void *pv, int running, RunState state); @@ -2639,6 +2644,190 @@ static void

vfio_unregister_req_notifier(VFIOPCIDevice *vdev)

Quoted 27 lines
     vdev->req_enabled = false;

}

hw/vfio/pci.c +24 -0
+static uint64_t vfio_dirty_log_sync(VFIOPCIDevice *vdev) {
+    RAMBlock *block;
+    struct vfio_device_get_dirty_bitmap *d;
+    uint64_t page = 0;
+    ram_addr_t size;
+    unsigned long nr, bitmap;
+
+    RAMBLOCK_FOREACH(block) {
+        size = block->used_length;
+        nr = size >> TARGET_PAGE_BITS;
+        bitmap = (BITS_TO_LONGS(nr) + 1) * sizeof(unsigned long);
+        d = g_malloc0(sizeof(*d) +  bitmap);
+        d->start_addr = block->offset;
+        d->page_nr = nr;
+        if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_DIRTY_BITMAP, d)) {
+            error_report("vfio: Failed to get device dirty bitmap");
+            g_free(d);
+            goto exit;
+        }
+
+        if (d->page_nr) {
+            cpu_physical_memory_set_dirty_lebitmap(
+                                 (unsigned long *)&d->dirty_bitmap,
+                                 d->start_addr, d->page_nr);

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)

Quoted 10 lines
hw/vfio/pci.c +10 -0
+            page += d->page_nr;
+        }
+        g_free(d);
+    }
+
+exit:
+    return page;
+}
+
+static void vfio_save_live_pending(QEMUFile *f, void *opaque, uint64_t

max_size,

Quoted 10 lines
hw/vfio/pci.c +10 -0
+                   uint64_t *non_postcopiable_pending,
+                   uint64_t *postcopiable_pending) {
+    VFIOPCIDevice *vdev = opaque;
+    uint64_t pending;
+
+    qemu_mutex_lock_iothread();
+    rcu_read_lock();
+    pending = vfio_dirty_log_sync(vdev);
+    rcu_read_unlock();
+    qemu_mutex_unlock_iothread();
Quoted 9 lines

hw/vfio/pci.c

Quoted 2 lines
hw/vfio/pci.c +2 -0
+    *non_postcopiable_pending += pending; }
+

This doesn't provide a way to read device's state during pre-copy phase. device_state region can be used to read device specific data from vendor driver during pre-copy phase. That could be done by mmap device_state region during init and then ioctl vendor driver here to query size of data copied to device_state region and pending data size.

Maybe size could be placed in the device state region instead of adding ioctl.

Quoted 148 lines
Quoted 7 lines
hw/vfio/pci.c +6 -0
+static int vfio_load(QEMUFile *f, void *opaque, int version_id) {
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int sz = vdev->device_state.size - VFIO_DEVICE_STATE_OFFSET;
+    uint8_t *buf = NULL;
+    uint32_t ctl, msi_lo, msi_hi, msi_data, bar_cfg, i;

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)

Quoted 10 lines
hw/vfio/pci.c +10 -0
+    bool msi_64bit;
+
+    if (qemu_get_byte(f) == VFIO_SAVE_FLAG_SETUP) {
+        goto exit;
+    }
+
+    /* retore pci bar configuration */
+    ctl = pci_default_read_config(pdev, PCI_COMMAND, 2);
+    vfio_pci_write_config(pdev, PCI_COMMAND,
+                          ctl & (!(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)),

2);

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)

Quoted 10 lines
hw/vfio/pci.c +10 -0
+    for (i = 0; i < PCI_ROM_SLOT; i++) {
+        bar_cfg = qemu_get_be32(f);
+        vfio_pci_write_config(pdev, PCI_BASE_ADDRESS_0 + i * 4, bar_cfg, 4);
+    }
+    vfio_pci_write_config(pdev, PCI_COMMAND,
+                          ctl | PCI_COMMAND_IO | PCI_COMMAND_MEMORY,
+ 2);
+
+    /* restore msi configuration */
+    ctl = pci_default_read_config(pdev, pdev->msi_cap + PCI_MSI_FLAGS,

2);

Quoted 8 lines
hw/vfio/pci.c +8 -0
+    msi_64bit = !!(ctl & PCI_MSI_FLAGS_64BIT);
+
+    vfio_pci_write_config(&vdev->pdev,
+                          pdev->msi_cap + PCI_MSI_FLAGS,
+                          ctl & (!PCI_MSI_FLAGS_ENABLE), 2);
+
+    msi_lo = qemu_get_be32(f);
+    vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_LO,

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)

Quoted 10 lines
hw/vfio/pci.c +10 -0
+ msi_lo, 4);
+
+    if (msi_64bit) {
+        msi_hi = qemu_get_be32(f);
+        vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_HI,
+                              msi_hi, 4);
+    }
+    msi_data = qemu_get_be32(f);
+    vfio_pci_write_config(pdev,
+              pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 :

PCI_MSI_DATA_32),

Quoted 8 lines
hw/vfio/pci.c +8 -0
+              msi_data, 2);
+
+    vfio_pci_write_config(&vdev->pdev, pdev->msi_cap + PCI_MSI_FLAGS,
+                          ctl | PCI_MSI_FLAGS_ENABLE, 2);
+
+    buf = g_malloc(sz);
+    if (buf == NULL) {
+        error_report("vfio: Failed to allocate memory for migrate");

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2756

Quoted 10 lines
hw/vfio/pci.c +10 -0
+        return -1;
+    }
+
+    qemu_get_buffer(f, buf, sz);
+    if (pwrite(vdev->vbasedev.fd, buf, sz,
+               vdev->device_state.offset + VFIO_DEVICE_STATE_OFFSET) != sz) {
+        error_report("vfio: Failed to write Device State Region");
+        return -1;
+    }
+

Again here if device_state region in mmaped, QEMU would write buffer to that region and an ioclt can be used to convey that new data is written to the region along with size of data.

Quoted 8 lines
hw/vfio/pci.c +8 -0
+    g_free(buf);
+
+exit:
+    return 0;
+}
+
+static int vfio_save_complete(QEMUFile *f, void *opaque) {
+    VFIOPCIDevice *vdev = opaque;

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)

Quoted 10 lines
hw/vfio/pci.c +10 -0
+    PCIDevice *pdev = &vdev->pdev;
+    int sz = vdev->device_state.size - VFIO_DEVICE_STATE_OFFSET;
+    uint8_t *buf = NULL;
+    uint32_t msi_cfg, msi_lo, msi_hi, msi_data, bar_cfg, i;
+    bool msi_64bit;
+
+    qemu_put_byte(f, VFIO_SAVE_FLAG_DEV_STATE);
+
+    for (i = 0; i < PCI_ROM_SLOT; i++) {
+        bar_cfg = pci_default_read_config(pdev, PCI_BASE_ADDRESS_0 + i *

4, 4);

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)

Quoted 4 lines
hw/vfio/pci.c +4 -0
+        qemu_put_be32(f, bar_cfg);
+    }
+
+    msi_cfg = pci_default_read_config(pdev, pdev->msi_cap +

PCI_MSI_FLAGS, 2);

Quoted 5 lines
hw/vfio/pci.c +5 -0
+    msi_64bit = !!(msi_cfg & PCI_MSI_FLAGS_64BIT);
+
+    msi_lo = pci_default_read_config(pdev,
+                                     pdev->msi_cap + PCI_MSI_ADDRESS_LO, 4);
+    qemu_put_be32(f, msi_lo);

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)

Quoted 10 lines
hw/vfio/pci.c +10 -0
+
+    if (msi_64bit) {
+        msi_hi = pci_default_read_config(pdev,
+                                         pdev->msi_cap + PCI_MSI_ADDRESS_HI,
+                                         4);
+        qemu_put_be32(f, msi_hi);
+    }
+
+    msi_data = pci_default_read_config(pdev,
+               pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 :

PCI_MSI_DATA_32),

Quoted 5 lines
hw/vfio/pci.c +5 -0
+               2);
+    qemu_put_be32(f, msi_data);
+
+    buf = g_malloc(sz);
+    if (buf == NULL) {

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2809

Quoted 10 lines
hw/vfio/pci.c +10 -0
+        error_report("vfio: Failed to allocate memory for migrate");
+        goto exit;
+    }
+
+    if (pread(vdev->vbasedev.fd, buf, sz,
+              vdev->device_state.offset + VFIO_DEVICE_STATE_OFFSET) != sz) {
+        error_report("vfio: Failed to read Device State Region");
+        goto exit;
+    }
+

Again here getting buffer information from vendor driver and directly reading that data from mmaped region would help.

Quoted 17 lines
hw/vfio/pci.c +17 -0
+    qemu_put_buffer(f, buf, sz);
+
+exit:
+    g_free(buf);
+
+    return 0;
+}
+
+static int vfio_save_setup(QEMUFile *f, void *opaque) {
+    qemu_put_byte(f, VFIO_SAVE_FLAG_SETUP);
+    return 0;
+}
+
+static SaveVMHandlers savevm_vfio_handlers = {
+    .save_setup = vfio_save_setup,
+    .save_live_pending = vfio_save_live_pending,
+    .save_live_complete_precopy = vfio_save_complete,
Quoted 9 lines

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2830

Quoted 3 lines
hw/vfio/pci.c +3 -0
+    .load_state = vfio_load,
+};
+

Isn't .is_active, .save_live_iterate and .cleanup required? What is vendor driver have large amount of data in device's memory which vendor driver is aware of? and vendor driver would required multiple iterations to send that data to QEMU to save complete state of device?

I suppose the vendor driver will copy the device's memory to the device region iteratively, and let qemu read from the device region and transfer the data to the target side in pre-copy stage, isn't it?

Quoted 7 lines

Isn't there should be an ioclt to convey QEMU's MIGRATION_STATUS_* to vendor driver? Knowing migration status would help vendor driver take necessary action, like MIGRATION_STATUS_SETUP, MIGRATION_STATUS_CANCELLED, MIGRATION_STATUS_FAILED, MIGRATION_STATUS_COMPLETED, I think these are important states that vendor driver should know.

Good point.

Quoted 61 lines

Thanks,
Kirti

Quoted 42 lines

static void vfio_realize(PCIDevice *pdev, Error **errp) {
VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); @@
-2898,6 +3087,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)

     vfio_register_req_notifier(vdev);
     vfio_setup_resetfn_quirk(vdev);
     qemu_add_vm_change_state_handler(vfio_vm_change_state_handler,

vdev);

hw/vfio/pci.c +2 -4
+    register_savevm_live(NULL, "vfio-pci", 0, 1,
+ &savevm_vfio_handlers, vdev);
 
     return;
@@ -3051,10 +3241,6 @@ static Property vfio_pci_dev_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
-static const VMStateDescription vfio_pci_vmstate = {
-    .name = "vfio-pci",
-};
-
 static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass); @@ -3062,7 +3248,6 @@

static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)

     dc->reset = vfio_pci_reset;
     dc->props = vfio_pci_dev_properties;
hw/vfio/pci.c +0 -1
-    dc->vmsd = &vfio_pci_vmstate;
     dc->desc = "VFIO-based PCI device assignment";
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
     pdc->realize = vfio_realize;
linux-headers/linux/vfio.h +3 -0
index 8f02f2f..2c911d9 100644
@@ -512,6 +512,20 @@ struct vfio_pci_hot_reset {
 
 #define VFIO_DEVICE_PCI_HOT_RESET	_IO(VFIO_TYPE, VFIO_BASE + 13)
 
+/**
+ * VFIO_DEVICE_GET_DIRTY_BITMAP - _IOW(VFIO_TYPE, VFIO_BASE + 14,
+ *				    struct vfio_device_get_dirty_bitmap)

linux-headers/linux/vfio.h struct vfio_pci_hot_reset { · line 527

Quoted 10 lines
linux-headers/linux/vfio.h +10 -0
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_device_get_dirty_bitmap {
+	__u64	       start_addr;
+	__u64	       page_nr;
+	__u8           dirty_bitmap[];
+};
+
+#define VFIO_DEVICE_GET_DIRTY_BITMAP	_IO(VFIO_TYPE,

VFIO_BASE + 14)

Quoted 5 lines
linux-headers/linux/vfio.h +1 -0
+
 /* -------- API for Type1 VFIO IOMMU -------- */
 
 /**
#3 lore raw
Quoted 13 lines · Yulei
Quoted 4 lines
Quoted 4 lines
hw/vfio/pci.c +4 -0
+static SaveVMHandlers savevm_vfio_handlers = {
+    .save_setup = vfio_save_setup,
+    .save_live_pending = vfio_save_live_pending,
+    .save_live_complete_precopy = vfio_save_complete,
Quoted 9 lines

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2830

Quoted 3 lines
hw/vfio/pci.c +3 -0
+    .load_state = vfio_load,
+};
+

Isn't .is_active, .save_live_iterate and .cleanup required? What is vendor driver have large amount of data in device's memory which vendor driver is aware of? and vendor driver would required multiple iterations to send that data to QEMU to save complete state of device?

Quoted 4 lines

I suppose the vendor driver will copy the device's memory to the device region iteratively, and let qemu read from the device region and transfer the data to the target side in pre-copy stage, isn't it?

As Dave mentioned in other mail in this thread, all data will not be copied only in pre-copy state. Some static data would be copied in pre-copy state but there could be significant amount of data in stop-and-copy state where iterations would be required. .is_active and .save_live_iterate would be required for that iterations.

.cleanup is required to provide an indication to vendor driver that migration is complete and vendor driver can cleanup all the extra allocations done for migration.

Thanks,
Kirti

#4 lore raw
In reply to Kirti Wankhede
Quoted 5 lines · Kirti Wankhede
Quoted 2 lines · Yulei Zhang

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

In the subject line: s/Hanlders/Handlers/

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#5 lore raw
In reply to Yulei Zhang
Quoted 13 lines · On Tue, 10 Apr 2018 14:03:13 +0800 Yulei Zhang <[email protected]> wrote:

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

Introduce new Ioctl VFIO_DEVICE_GET_DIRTY_BITMAP to fetch the memory bitmap that dirtied by vfio device during the iterative precopy stage to shorten the system downtime afterward.

For vfio pci device status migrate, during the system downtime, it will save the following states
1. pci configuration space addr0~addr5
2. pci configuration space msi_addr msi_data
3. pci device status fetch from device driver

Quoted 4 lines

And on the target side the vfio_load will restore the same states
1. re-setup the pci bar configuration
2. re-setup the pci device msi configuration
3. restore the pci device status

Interrupts are configured via ioctl, but I don't see any code here to configure the device into the correct interrupt state. How do we know the target device is compatible with the source device? Do we rely on the vendor driver to implicitly include some kind of device and version information and fail at the very end of the migration? It seems like we need to somehow front-load that sort of device compatibility checking since a vfio-pci device can be anything (ex. what happens if a user tries to migrate a GVT-g vGPU to an NVIDIA vGPU?). Thanks,

Alex

#6 lore raw
Quoted 41 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Tuesday, April 17, 2018 4:38 AM
To: Zhang, Yulei <[email protected]>
Cc: [email protected]; Tian, Kevin <[email protected]>; [email protected]; [email protected]; [email protected]; Wang, Zhi A <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH V4 3/4] vfio: Add SaveVMHanlders for VFIO device to support live migration

Quoted 4 lines · On Tue, 10 Apr 2018 14:03:13 +0800 Yulei Zhang <[email protected]> wrote:

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

Introduce new Ioctl VFIO_DEVICE_GET_DIRTY_BITMAP to fetch the

memory

Quoted 11 lines

bitmap that dirtied by vfio device during the iterative precopy stage to shorten the system downtime afterward.

For vfio pci device status migrate, during the system downtime, it will save the following states 1. pci configuration space addr0~addr5
2. pci configuration space msi_addr msi_data 3. pci device status fetch from device driver

And on the target side the vfio_load will restore the same states 1. re-setup the pci bar configuration 2. re-setup the pci device msi configuration 3. restore the pci device status

Interrupts are configured via ioctl, but I don't see any code here to configure the device into the correct interrupt state. How do we know the target device is compatible with the source device? Do we rely on the vendor driver to implicitly include some kind of device and version information and fail at the very end of the migration? It seems like we need to somehow front-load that sort of device compatibility checking since a vfio-pci device can be anything (ex. what happens if a user tries to migrate a GVT-g vGPU to an NVIDIA vGPU?). Thanks,

Quoted 1 line

Alex

We emulate the write to the pci configure space in vfio_load() which will help setup the interrupt state.
For compatibility I think currently the vendor driver would put version number or device specific info in the device state region, so during the pre-copy stage the target side will discover the difference and call off the migration.

#7 lore raw
Quoted 40 lines · Yulei
Quoted 40 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Tuesday, April 17, 2018 4:38 AM
To: Zhang, Yulei <[email protected]>
Cc: [email protected]; Tian, Kevin <[email protected]>; [email protected]; [email protected]; [email protected]; Wang, Zhi A <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH V4 3/4] vfio: Add SaveVMHanlders for VFIO device to support live migration

Quoted 4 lines · On Tue, 10 Apr 2018 14:03:13 +0800 Yulei Zhang <[email protected]> wrote:

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

Introduce new Ioctl VFIO_DEVICE_GET_DIRTY_BITMAP to fetch the

memory

Quoted 11 lines

bitmap that dirtied by vfio device during the iterative precopy stage to shorten the system downtime afterward.

For vfio pci device status migrate, during the system downtime, it will save the following states 1. pci configuration space addr0~addr5
2. pci configuration space msi_addr msi_data 3. pci device status fetch from device driver

And on the target side the vfio_load will restore the same states 1. re-setup the pci bar configuration 2. re-setup the pci device msi configuration 3. restore the pci device status

Interrupts are configured via ioctl, but I don't see any code here to configure the device into the correct interrupt state. How do we know the target device is compatible with the source device? Do we rely on the vendor driver to implicitly include some kind of device and version information and fail at the very end of the migration? It seems like we need to somehow front-load that sort of device compatibility checking since a vfio-pci device can be anything (ex. what happens if a user tries to migrate a GVT-g vGPU to an NVIDIA vGPU?). Thanks,

Quoted 4 lines
Quoted 1 line

Alex

We emulate the write to the pci configure space in vfio_load() which will help setup the interrupt state.

But you're only doing that for MSI, not MSI-X, we cannot simply say that we don't have an MSI-X devices right now and add it later or we'll end up with incompatible vmstate, we need to plan for how we'll support it within the save state stream now.

Quoted 3 lines

For compatibility I think currently the vendor driver would put version number or device specific info in the device state region, so during the pre-copy stage the target side will discover the difference and call off the migration.

Those sorts of things should be built into the device state region, we shouldn't rely on vendor drivers to make these kinds of considerations, we should build it into the API, which also allows QEMU to check state compatibility before attempting a migration. Thanks,

Alex

#8 lore raw
Quoted 63 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Tuesday, April 17, 2018 10:35 PM
To: Zhang, Yulei <[email protected]>
Cc: [email protected]; Tian, Kevin <[email protected]>; [email protected]; [email protected]; [email protected]; Wang, Zhi A <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH V4 3/4] vfio: Add SaveVMHanlders for VFIO device to support live migration

Quoted 9 lines · Yulei
Quoted 9 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Tuesday, April 17, 2018 4:38 AM
To: Zhang, Yulei <[email protected]>
Cc: [email protected]; Tian, Kevin <[email protected]>; [email protected]; [email protected]; [email protected]; Wang, Zhi A <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH V4 3/4] vfio: Add SaveVMHanlders for VFIO

device

Quoted 18 lines
Quoted 18 lines

to support live migration

Quoted 4 lines · On Tue, 10 Apr 2018 14:03:13 +0800 Yulei Zhang <[email protected]> wrote:

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

Introduce new Ioctl VFIO_DEVICE_GET_DIRTY_BITMAP to fetch the

memory

Quoted 8 lines

bitmap that dirtied by vfio device during the iterative precopy stage to shorten the system downtime afterward.

For vfio pci device status migrate, during the system downtime, it will save the following states 1. pci configuration space addr0~addr5
2. pci configuration space msi_addr msi_data 3. pci device status fetch from device driver

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

And on the target side the vfio_load will restore the same states 1. re-setup the pci bar configuration 2. re-setup the pci device msi configuration 3. restore the pci device status

Interrupts are configured via ioctl, but I don't see any code here to

configure

Quoted 3 lines
Quoted 3 lines

the device into the correct interrupt state. How do we know the target device is compatible with the source device? Do we rely on the vendor driver to implicitly include some kind of device and version information

and

Quoted 2 lines
Quoted 2 lines

fail at the very end of the migration? It seems like we need to somehow front-load that sort of device compatibility checking since a vfio-pci

device

Quoted 1 line
Quoted 1 line

can be anything (ex. what happens if a user tries to migrate a GVT-g

vGPU to

Quoted 4 lines
Quoted 3 lines

an NVIDIA vGPU?). Thanks,

Alex

Quoted 9 lines
Quoted 1 line

We emulate the write to the pci configure space in vfio_load() which will

help

Quoted 1 line

setup the interrupt state.

But you're only doing that for MSI, not MSI-X, we cannot simply say that we don't have an MSI-X devices right now and add it later or we'll end up with incompatible vmstate, we need to plan for how we'll support it within the save state stream now.

Agree with u.

Quoted 4 lines
Quoted 1 line

For compatibility I think currently the vendor driver would put version

number

Quoted 1 line

or device specific info in the device state region, so during the pre-copy

stage

Quoted 8 lines
Quoted 1 line

the target side will discover the difference and call off the migration.

Those sorts of things should be built into the device state region, we shouldn't rely on vendor drivers to make these kinds of considerations, we should build it into the API, which also allows QEMU to check state compatibility before attempting a migration. Thanks,

Alex

Not sure about how to check the compatibility before attempting a migration, to my understanding, qemu doesn't know the configuration on target side, target may call off the migration when it finds the input device state isn't compatible, and source vm will resume.

#9 lore raw
Quoted 85 lines · * Zhang, Yulei ([email protected]) wrote:
Quoted 63 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Tuesday, April 17, 2018 10:35 PM
To: Zhang, Yulei <[email protected]>
Cc: [email protected]; Tian, Kevin <[email protected]>; [email protected]; [email protected]; [email protected]; Wang, Zhi A <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH V4 3/4] vfio: Add SaveVMHanlders for VFIO device to support live migration

Quoted 9 lines · Yulei
Quoted 9 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Tuesday, April 17, 2018 4:38 AM
To: Zhang, Yulei <[email protected]>
Cc: [email protected]; Tian, Kevin <[email protected]>; [email protected]; [email protected]; [email protected]; Wang, Zhi A <[email protected]>; [email protected]; [email protected]
Subject: Re: [RFC PATCH V4 3/4] vfio: Add SaveVMHanlders for VFIO

device

Quoted 18 lines
Quoted 18 lines

to support live migration

Quoted 4 lines · On Tue, 10 Apr 2018 14:03:13 +0800 Yulei Zhang <[email protected]> wrote:

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

Introduce new Ioctl VFIO_DEVICE_GET_DIRTY_BITMAP to fetch the

memory

Quoted 8 lines

bitmap that dirtied by vfio device during the iterative precopy stage to shorten the system downtime afterward.

For vfio pci device status migrate, during the system downtime, it will save the following states 1. pci configuration space addr0~addr5
2. pci configuration space msi_addr msi_data 3. pci device status fetch from device driver

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

And on the target side the vfio_load will restore the same states 1. re-setup the pci bar configuration 2. re-setup the pci device msi configuration 3. restore the pci device status

Interrupts are configured via ioctl, but I don't see any code here to

configure

Quoted 3 lines
Quoted 3 lines

the device into the correct interrupt state. How do we know the target device is compatible with the source device? Do we rely on the vendor driver to implicitly include some kind of device and version information

and

Quoted 2 lines
Quoted 2 lines

fail at the very end of the migration? It seems like we need to somehow front-load that sort of device compatibility checking since a vfio-pci

device

Quoted 1 line
Quoted 1 line

can be anything (ex. what happens if a user tries to migrate a GVT-g

vGPU to

Quoted 4 lines
Quoted 3 lines

an NVIDIA vGPU?). Thanks,

Alex

Quoted 9 lines
Quoted 1 line

We emulate the write to the pci configure space in vfio_load() which will

help

Quoted 1 line

setup the interrupt state.

But you're only doing that for MSI, not MSI-X, we cannot simply say that we don't have an MSI-X devices right now and add it later or we'll end up with incompatible vmstate, we need to plan for how we'll support it within the save state stream now.

Agree with u.

Quoted 11 lines
Quoted 1 line

For compatibility I think currently the vendor driver would put version

number

Quoted 1 line

or device specific info in the device state region, so during the pre-copy

stage

Quoted 1 line

the target side will discover the difference and call off the migration.

Those sorts of things should be built into the device state region, we shouldn't rely on vendor drivers to make these kinds of considerations, we should build it into the API, which also allows QEMU to check state compatibility before attempting a migration. Thanks,

Quoted 6 lines
Quoted 1 line

Alex

Not sure about how to check the compatibility before attempting a migration, to my understanding, qemu doesn't know the configuration on target side, target may call off the migration when it finds the input device state isn't compatible, and source vm will resume.

There are a few parts to this:
a) Tools, like libvirt etc need to be able to detect that the two
configurations are actually compatible before they try - so there should be some way of exposing enough detail about the host/drivers for something somewhere to be able to say 'yes it should work' before even starting the migration.

b) The migration stream should contain enough information to be able
to *cleanly* detect an incompatibility so that the failure is obvious; so make sure you have enough information in there (preferably in the 'setup' part if it really is iterative). Then when loading check that data and print a clear reason why it's incompatible.

c) We've got to try and keep that compatibility across versions - so
migrations to a newer QEMU, or newer drivers (or depending on the hardware) to newer hardware should be able to work if possible. So the stream might need to have version identifiers in to help with that.

Dave

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#10 lore raw
In reply to Yulei Zhang
Quoted 98 lines · * Yulei Zhang ([email protected]) wrote:

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

Introduce new Ioctl VFIO_DEVICE_GET_DIRTY_BITMAP to fetch the memory bitmap that dirtied by vfio device during the iterative precopy stage to shorten the system downtime afterward.

For vfio pci device status migrate, during the system downtime, it will save the following states
1. pci configuration space addr0~addr5
2. pci configuration space msi_addr msi_data
3. pci device status fetch from device driver

And on the target side the vfio_load will restore the same states
1. re-setup the pci bar configuration
2. re-setup the pci device msi configuration
3. restore the pci device status

---
 hw/vfio/pci.c              | 195 +++++++++++++++++++++++++++++++++++++++++++--
 linux-headers/linux/vfio.h |  14 ++++
 2 files changed, 204 insertions(+), 5 deletions(-)
hw/vfio/pci.c +56 -0
index 13d8c73..ac6a9c7 100644
@@ -33,9 +33,14 @@
 #include "trace.h"
 #include "qapi/error.h"
 #include "migration/blocker.h"
+#include "migration/register.h"
+#include "exec/ram_addr.h"
 
 #define MSIX_CAP_LENGTH 12
 
+#define VFIO_SAVE_FLAG_SETUP 0
+#define VFIO_SAVE_FLAG_DEV_STATE 1
+
 static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
 static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
 static void vfio_vm_change_state_handler(void *pv, int running, RunState state);
@@ -2639,6 +2644,190 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+static uint64_t vfio_dirty_log_sync(VFIOPCIDevice *vdev)
+{
+    RAMBlock *block;
+    struct vfio_device_get_dirty_bitmap *d;
+    uint64_t page = 0;
+    ram_addr_t size;
+    unsigned long nr, bitmap;
+
+    RAMBLOCK_FOREACH(block) {
+        size = block->used_length;
+        nr = size >> TARGET_PAGE_BITS;
+        bitmap = (BITS_TO_LONGS(nr) + 1) * sizeof(unsigned long);
+        d = g_malloc0(sizeof(*d) +  bitmap);
+        d->start_addr = block->offset;
+        d->page_nr = nr;
+        if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_DIRTY_BITMAP, d)) {
+            error_report("vfio: Failed to get device dirty bitmap");
+            g_free(d);
+            goto exit;
+        }
+
+        if (d->page_nr) {
+            cpu_physical_memory_set_dirty_lebitmap(
+                                 (unsigned long *)&d->dirty_bitmap,
+                                 d->start_addr, d->page_nr);
+            page += d->page_nr;
+        }
+        g_free(d);
+    }
+
+exit:
+    return page;
+}
+
+static void vfio_save_live_pending(QEMUFile *f, void *opaque, uint64_t max_size,
+                   uint64_t *non_postcopiable_pending,
+                   uint64_t *postcopiable_pending)
+{
+    VFIOPCIDevice *vdev = opaque;
+    uint64_t pending;
+
+    qemu_mutex_lock_iothread();
+    rcu_read_lock();
+    pending = vfio_dirty_log_sync(vdev);
+    rcu_read_unlock();
+    qemu_mutex_unlock_iothread();
+    *non_postcopiable_pending += pending;
+}
+
+static int vfio_load(QEMUFile *f, void *opaque, int version_id)
+{

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2707

Quoted 10 lines
hw/vfio/pci.c +10 -0
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int sz = vdev->device_state.size - VFIO_DEVICE_STATE_OFFSET;
+    uint8_t *buf = NULL;
+    uint32_t ctl, msi_lo, msi_hi, msi_data, bar_cfg, i;
+    bool msi_64bit;
+
+    if (qemu_get_byte(f) == VFIO_SAVE_FLAG_SETUP) {
+        goto exit;
+    }

If you're building something complex like this, you might want to add some version flags at the start and a canary at the end to detect corruption.

Also note that the migration could fail at any point; so calling qemu_file_get_error is good practice at points before acting on data youv'e read with qemu_get_be* since it could be bogus if it's already failed.

Quoted 26 lines
hw/vfio/pci.c +26 -0
+    /* retore pci bar configuration */
+    ctl = pci_default_read_config(pdev, PCI_COMMAND, 2);
+    vfio_pci_write_config(pdev, PCI_COMMAND,
+                          ctl & (!(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)), 2);
+    for (i = 0; i < PCI_ROM_SLOT; i++) {
+        bar_cfg = qemu_get_be32(f);
+        vfio_pci_write_config(pdev, PCI_BASE_ADDRESS_0 + i * 4, bar_cfg, 4);
+    }
+    vfio_pci_write_config(pdev, PCI_COMMAND,
+                          ctl | PCI_COMMAND_IO | PCI_COMMAND_MEMORY, 2);
+
+    /* restore msi configuration */
+    ctl = pci_default_read_config(pdev, pdev->msi_cap + PCI_MSI_FLAGS, 2);
+    msi_64bit = !!(ctl & PCI_MSI_FLAGS_64BIT);
+
+    vfio_pci_write_config(&vdev->pdev,
+                          pdev->msi_cap + PCI_MSI_FLAGS,
+                          ctl & (!PCI_MSI_FLAGS_ENABLE), 2);
+
+    msi_lo = qemu_get_be32(f);
+    vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_LO, msi_lo, 4);
+
+    if (msi_64bit) {
+        msi_hi = qemu_get_be32(f);
+        vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_HI,
+                              msi_hi, 4);

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2744

Quoted 10 lines
hw/vfio/pci.c +10 -0
+    }
+    msi_data = qemu_get_be32(f);
+    vfio_pci_write_config(pdev,
+              pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32),
+              msi_data, 2);
+
+    vfio_pci_write_config(&vdev->pdev, pdev->msi_cap + PCI_MSI_FLAGS,
+                          ctl | PCI_MSI_FLAGS_ENABLE, 2);
+
+    buf = g_malloc(sz);

Please validate 'sz' before using it - just assume that the byte stream got horribly corrupted and you should really check for it.

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2748

Quoted 4 lines
hw/vfio/pci.c +4 -0
+    if (buf == NULL) {
+        error_report("vfio: Failed to allocate memory for migrate");
+        return -1;
+    }

g_malloc doesn't fail by returning NULL; it asserts; so use g_try_malloc.

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2753

Quoted 4 lines
hw/vfio/pci.c +4 -0
+    qemu_get_buffer(f, buf, sz);
+    if (pwrite(vdev->vbasedev.fd, buf, sz,
+               vdev->device_state.offset + VFIO_DEVICE_STATE_OFFSET) != sz) {
+        error_report("vfio: Failed to write Device State Region");

free the buffer here.

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2763

Quoted 10 lines
hw/vfio/pci.c +10 -0
+        return -1;
+    }
+
+    g_free(buf);
+
+exit:
+    return 0;
+}
+
+static int vfio_save_complete(QEMUFile *f, void *opaque)

Echoing Kirti's mail, this doesn't seem to be an iterative save routine; this is just all at the end; in which case use VMState.
If you want it iterative then you need to define the other functions.

Quoted 29 lines
hw/vfio/pci.c +29 -0
+{
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int sz = vdev->device_state.size - VFIO_DEVICE_STATE_OFFSET;
+    uint8_t *buf = NULL;
+    uint32_t msi_cfg, msi_lo, msi_hi, msi_data, bar_cfg, i;
+    bool msi_64bit;
+
+    qemu_put_byte(f, VFIO_SAVE_FLAG_DEV_STATE);
+
+    for (i = 0; i < PCI_ROM_SLOT; i++) {
+        bar_cfg = pci_default_read_config(pdev, PCI_BASE_ADDRESS_0 + i * 4, 4);
+        qemu_put_be32(f, bar_cfg);
+    }
+
+    msi_cfg = pci_default_read_config(pdev, pdev->msi_cap + PCI_MSI_FLAGS, 2);
+    msi_64bit = !!(msi_cfg & PCI_MSI_FLAGS_64BIT);
+
+    msi_lo = pci_default_read_config(pdev,
+                                     pdev->msi_cap + PCI_MSI_ADDRESS_LO, 4);
+    qemu_put_be32(f, msi_lo);
+
+    if (msi_64bit) {
+        msi_hi = pci_default_read_config(pdev,
+                                         pdev->msi_cap + PCI_MSI_ADDRESS_HI,
+                                         4);
+        qemu_put_be32(f, msi_hi);
+    }
+

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2802

Quoted 10 lines
hw/vfio/pci.c +10 -0
+    msi_data = pci_default_read_config(pdev,
+               pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32),
+               2);
+    qemu_put_be32(f, msi_data);
+
+    buf = g_malloc(sz);
+    if (buf == NULL) {
+        error_report("vfio: Failed to allocate memory for migrate");
+        goto exit;
+    }

Same about the allocation above.

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2806

Quoted 3 lines
hw/vfio/pci.c +3 -0
+    if (pread(vdev->vbasedev.fd, buf, sz,
+              vdev->device_state.offset + VFIO_DEVICE_STATE_OFFSET) != sz) {
+        error_report("vfio: Failed to read Device State Region");

Include errno?

Quoted 63 lines
hw/vfio/pci.c +25 -5
+        goto exit;
+    }
+
+    qemu_put_buffer(f, buf, sz);
+
+exit:
+    g_free(buf);
+
+    return 0;
+}
+
+static int vfio_save_setup(QEMUFile *f, void *opaque)
+{
+    qemu_put_byte(f, VFIO_SAVE_FLAG_SETUP);
+    return 0;
+}
+
+static SaveVMHandlers savevm_vfio_handlers = {
+    .save_setup = vfio_save_setup,
+    .save_live_pending = vfio_save_live_pending,
+    .save_live_complete_precopy = vfio_save_complete,
+    .load_state = vfio_load,
+};
+
 static void vfio_realize(PCIDevice *pdev, Error **errp)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
@@ -2898,6 +3087,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
     vfio_register_req_notifier(vdev);
     vfio_setup_resetfn_quirk(vdev);
     qemu_add_vm_change_state_handler(vfio_vm_change_state_handler, vdev);
+    register_savevm_live(NULL, "vfio-pci", 0, 1, &savevm_vfio_handlers, vdev);
 
     return;
 
@@ -3051,10 +3241,6 @@ static Property vfio_pci_dev_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
-static const VMStateDescription vfio_pci_vmstate = {
-    .name = "vfio-pci",
-};
-
 static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
@@ -3062,7 +3248,6 @@ static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 
     dc->reset = vfio_pci_reset;
     dc->props = vfio_pci_dev_properties;
-    dc->vmsd = &vfio_pci_vmstate;
     dc->desc = "VFIO-based PCI device assignment";
     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
     pdc->realize = vfio_realize;
linux-headers/linux/vfio.h +1 -0
index 8f02f2f..2c911d9 100644
@@ -512,6 +512,20 @@ struct vfio_pci_hot_reset {
 
 #define VFIO_DEVICE_PCI_HOT_RESET	_IO(VFIO_TYPE, VFIO_BASE + 13)
 
+/**

linux-headers/linux/vfio.h struct vfio_pci_hot_reset { · line 525

Quoted 10 lines
linux-headers/linux/vfio.h +10 -0
+ * VFIO_DEVICE_GET_DIRTY_BITMAP - _IOW(VFIO_TYPE, VFIO_BASE + 14,
+ *				    struct vfio_device_get_dirty_bitmap)
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+struct vfio_device_get_dirty_bitmap {
+	__u64	       start_addr;
+	__u64	       page_nr;
+	__u8           dirty_bitmap[];
+};

Header updates should be in a separate patch.

Dave

Quoted 8 lines
linux-headers/linux/vfio.h +2 -0
+#define VFIO_DEVICE_GET_DIRTY_BITMAP	_IO(VFIO_TYPE, VFIO_BASE + 14)
+
 /* -------- API for Type1 VFIO IOMMU -------- */
 
 /**
Signature · 1 line
2.7.4

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#11 lore raw
In reply to Yulei Zhang
Quoted 17 lines · Yulei Zhang

Instead of using vm state description, add SaveVMHandlers for VFIO device to support live migration.

Introduce new Ioctl VFIO_DEVICE_GET_DIRTY_BITMAP to fetch the memory bitmap that dirtied by vfio device during the iterative precopy stage to shorten the system downtime afterward.

For vfio pci device status migrate, during the system downtime, it will save the following states
1. pci configuration space addr0~addr5
2. pci configuration space msi_addr msi_data
3. pci device status fetch from device driver

And on the target side the vfio_load will restore the same states
1. re-setup the pci bar configuration
2. re-setup the pci device msi configuration
3. restore the pci device status

[...]

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2655

Quoted 9 lines
hw/vfio/pci.c +9 -0
+static uint64_t vfio_dirty_log_sync(VFIOPCIDevice *vdev)
+{
+    RAMBlock *block;
+    struct vfio_device_get_dirty_bitmap *d;
+    uint64_t page = 0;
+    ram_addr_t size;
+    unsigned long nr, bitmap;
+
+    RAMBLOCK_FOREACH(block) {

BTW, you are asking to sync all blocks of memory? Does vfio needs it? Things like acpi tables, or similar weird blocks looks strange, no?

Quoted 7 lines
hw/vfio/pci.c +7 -0
+        size = block->used_length;
+        nr = size >> TARGET_PAGE_BITS;
+        bitmap = (BITS_TO_LONGS(nr) + 1) * sizeof(unsigned long);
+        d = g_malloc0(sizeof(*d) +  bitmap);
+        d->start_addr = block->offset;
+        d->page_nr = nr;
+        if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_DIRTY_BITMAP, d)) {

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2672

Quoted 10 lines
hw/vfio/pci.c +10 -0
+            error_report("vfio: Failed to get device dirty bitmap");
+            g_free(d);
+            goto exit;
+        }
+
+        if (d->page_nr) {
+            cpu_physical_memory_set_dirty_lebitmap(
+                                 (unsigned long *)&d->dirty_bitmap,
+                                 d->start_addr, d->page_nr);
+            page += d->page_nr;

Are you sure that this are the number of dirty pages? It looks to me to the total number of pages in the region, no?

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2679

Quoted 7 lines
hw/vfio/pci.c +7 -0
+        }
+        g_free(d);
+    }
+
+exit:
+    return page;
+}

You walk the whole RAM on each bitmap. Could it be possible that driver knows _what_ memory regions it can have dirty pages on?

Quoted 4 lines
hw/vfio/pci.c +4 -0
+static void vfio_save_live_pending(QEMUFile *f, void *opaque, uint64_t max_size,
+                   uint64_t *non_postcopiable_pending,
+                   uint64_t *postcopiable_pending)
+{

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2694

Quoted 10 lines
hw/vfio/pci.c +10 -0
+    VFIOPCIDevice *vdev = opaque;
+    uint64_t pending;
+
+    qemu_mutex_lock_iothread();
+    rcu_read_lock();
+    pending = vfio_dirty_log_sync(vdev);
+    rcu_read_unlock();
+    qemu_mutex_unlock_iothread();
+    *non_postcopiable_pending += pending;
+}

As said in other emails, this is not for iterative migration, see how we do for ram (I have simplified it a bit):

static void ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,

                             uint64_t *res_precopy_only,
                             uint64_t *res_compatible,
                             uint64_t *res_postcopy_only)
{
    RAMState **temp = opaque;
    RAMState *rs = *temp;
    uint64_t remaining_size;

    remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE;

    if (remaining_size < max_size) {
        qemu_mutex_lock_iothread();
        rcu_read_lock();
        migration_bitmap_sync(rs);
        rcu_read_unlock();
        qemu_mutex_unlock_iothread();
        remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE;
    }

    *res_precopy_only += remaining_size;
}

I.e. we only do the sync stage if we don't have enough dirty pages on the bitmap.

BTW, once that we are here, independently of this, perhaps we should change the "sync" to take functions for each ramblock instead of for the whole ram.

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2707

Quoted 12 lines
hw/vfio/pci.c +12 -0
+static int vfio_load(QEMUFile *f, void *opaque, int version_id)
+{
+    VFIOPCIDevice *vdev = opaque;
+    PCIDevice *pdev = &vdev->pdev;
+    int sz = vdev->device_state.size - VFIO_DEVICE_STATE_OFFSET;
+    uint8_t *buf = NULL;
+    uint32_t ctl, msi_lo, msi_hi, msi_data, bar_cfg, i;
+    bool msi_64bit;
+
+    if (qemu_get_byte(f) == VFIO_SAVE_FLAG_SETUP) {
+        goto exit;
+    }

As said before, I would suggest that you add several fields:
- VERSION: So you can make incopatible changes
- FLAGS: compatible changes
- SIZE of the region? Rest of QEMU don't have it, I consider it an error. Having it makes way easier to analyze the stream and seach for errors.

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2712

Quoted 4 lines
hw/vfio/pci.c +4 -0
+    /* retore pci bar configuration */
+    ctl = pci_default_read_config(pdev, PCI_COMMAND, 2);
+    vfio_pci_write_config(pdev, PCI_COMMAND,
+                          ctl & (!(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)), 2);

Please forgive my pci ignorance, but are we really want to store the pci configuration every time that we receive an iteration stage?

Quoted 1 line

+ for (i = 0; i < PCI_ROM_SLOT; i++) {

Is this PCI_ROM_SLOT fixed by some spec or should we transfer it?

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2722

Quoted 9 lines
hw/vfio/pci.c +9 -0
+        bar_cfg = qemu_get_be32(f);
+        vfio_pci_write_config(pdev, PCI_BASE_ADDRESS_0 + i * 4, bar_cfg, 4);
+    }
+    vfio_pci_write_config(pdev, PCI_COMMAND,
+                          ctl | PCI_COMMAND_IO | PCI_COMMAND_MEMORY, 2);
+
+    /* restore msi configuration */
+    ctl = pci_default_read_config(pdev, pdev->msi_cap + PCI_MSI_FLAGS, 2);
+    msi_64bit = !!(ctl & PCI_MSI_FLAGS_64BIT);

old experence show this is a bad idea. See all the ifdefs for 32/64 bit on target/i386/machine.c

I really suggest to store on the stream a bool with msi_64bit value and send dummy values for 32bit mode and do nothing on destination. The simpler is your stream, the less bugs that you end with.

Quoted 29 lines
hw/vfio/pci.c +29 -0
+
+    vfio_pci_write_config(&vdev->pdev,
+                          pdev->msi_cap + PCI_MSI_FLAGS,
+                          ctl & (!PCI_MSI_FLAGS_ENABLE), 2);
+
+    msi_lo = qemu_get_be32(f);
+    vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_LO, msi_lo, 4);
+
+    if (msi_64bit) {
+        msi_hi = qemu_get_be32(f);
+        vfio_pci_write_config(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_HI,
+                              msi_hi, 4);
+    }
+    msi_data = qemu_get_be32(f);
+    vfio_pci_write_config(pdev,
+              pdev->msi_cap + (msi_64bit ? PCI_MSI_DATA_64 : PCI_MSI_DATA_32),
+              msi_data, 2);
+
+    vfio_pci_write_config(&vdev->pdev, pdev->msi_cap + PCI_MSI_FLAGS,
+                          ctl | PCI_MSI_FLAGS_ENABLE, 2);
+
+    buf = g_malloc(sz);
+    if (buf == NULL) {
+        error_report("vfio: Failed to allocate memory for migrate");
+        return -1;
+    }
+
+    qemu_get_buffer(f, buf, sz);
+    if (pwrite(vdev->vbasedev.fd, buf, sz,

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2761

Quoted 10 lines
hw/vfio/pci.c +10 -0
+               vdev->device_state.offset + VFIO_DEVICE_STATE_OFFSET) != sz) {
+        error_report("vfio: Failed to write Device State Region");
+        return -1;
+    }
+
+    g_free(buf);
+
+exit:
+    return 0;
+}

BTW, this looks easy to do with vmstate, no need to use save_live for this.

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 2820

Quoted 3 lines
hw/vfio/pci.c +3 -0
+static int vfio_save_setup(QEMUFile *f, void *opaque)
+{
+    qemu_put_byte(f, VFIO_SAVE_FLAG_SETUP);

I think it would be a good idea that it would be a good idea that you do the malloc() here. Failing migration on completion for a memory error in the completion stage is considered rude O:-)

Quoted 12 lines
hw/vfio/pci.c +0 -4
@@ -3051,10 +3241,6 @@ static Property vfio_pci_dev_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
-static const VMStateDescription vfio_pci_vmstate = {
-    .name = "vfio-pci",
-};
-
 static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);

hw/vfio/pci.c static void vfio_pci_dev_class_init(ObjectClass *klass, void *data) · line 3065

Quoted 4 lines
hw/vfio/pci.c +0 -1
@@ -3062,7 +3248,6 @@ static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
 
     dc->reset = vfio_pci_reset;
     dc->props = vfio_pci_dev_properties;
-    dc->vmsd = &vfio_pci_vmstate;

Seing what you are doing, I will put your configuration data here on the vmsd, and will only left the dirty pages/log handrinng for the register_savevm.

Or you can search for inspiration in virtio log handling. If you are just using ram, you don't need to transfer anything, you just need to make sure that you are called when the memory dirty bit is updated so you can add your log.

Later, Juan.