Skip to contentSkip to conversation

VFIO migration related refactor and bug fix

PATCH In review Zhenzhong Duan 4 participants 16 messages Jul 3, 2023 – Jul 4, 2023

lore
Share & tools
lore
#0 lore raw

v6 cover VFIO migration related refactor and bug fix

Hello,

PATCH4,6,7 refactors the VFIO migration blocker related code based on suggestions from Joao and Cedric, so that code is simpler and redundant "Migration disabled" isn't printed.

But before that works, also found some hotplug bugs when testing blocker adding failed case. PATCH1-3,5 fix them.

See patch description for details.

v6: Update patch description with Joao suggested words
Move rename patch to last and keep int return type for internal functions per Joao
Move a check out from vfio_migration_deinit()
Add Reviewed-by
v5: PATCH1-2 are not sent in v5 as Cedric already picked them
Minor adjust on PATCH3 per Joao and Cedric
Move PATCH4 after refactor patch per Joao
Split refactor patch to three patches, PATCH4,5,7 per Cedric

v4: Rebased on [1] which contains Avihai's patchset [2]
Add more patches to fix resource leak issue, split based on different fix TAG per Joao
Change to not print "Migration disabled" with explicit enable-migration=off per Avihai
Rename vfio_block_giommu_migration to vfio_viommu_preset per Joao

v3: Add PATCH1,2 to fix hotplug bug
Fix bugs in PATCH3 Avihai and Joao pointed out

Tested vfio hotplug/unplug with vfio migration supported and unsupported cases, and different param of enable-migration=[on/off/auto] and -only-migratable.

[1] https://github.com/legoater/qemu/tree/vfio-next
[2] https://lists.nongnu.org/archive/html/qemu-devel/2023-06/msg06117.html

Thanks

Zhenzhong Duan (5):
vfio/pci: Disable INTx in vfio_realize error path vfio/migration: Change vIOMMU blocker from global to per device vfio/migration: Free resources when vfio_migration_realize fails vfio/migration: Remove print of "Migration disabled" vfio/migration: Return bool type for vfio_migration_realize()

 hw/vfio/common.c              | 51 ++---------------------------------
 hw/vfio/migration.c           | 51 ++++++++++++++++++++++++-----------
 hw/vfio/pci.c                 |  9 ++++---
 include/hw/vfio/vfio-common.h |  5 ++--
 4 files changed, 44 insertions(+), 72 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v6 3/7 vfio/pci: Disable INTx in vfio_realize error path

When vfio realize fails, INTx isn't disabled if it has been enabled. This may confuse host side with unhandled interrupt report.

Fixes c5478fea27ac ("vfio/pci: Respond to KVM irqchip change notifier")
---
 hw/vfio/pci.c | 3 +++
 1 file changed, 3 insertions(+)
hw/vfio/pci.c +3 -0
index ab6645ba60af..31c4ab250fbe 100644
@@ -3220,6 +3220,9 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
     return;
 
 out_deregister:
+    if (vdev->interrupt == VFIO_INT_INTx) {
+        vfio_intx_disable(vdev);
+    }
     pci_device_set_intx_routing_notifier(&vdev->pdev, NULL);
     if (vdev->irqchip_change_notifier.notify) {
         kvm_irqchip_remove_change_notifier(&vdev->irqchip_change_notifier);
Signature · 1 line
2.34.1
#2 lore raw

PATCH v6 4/7 vfio/migration: Change vIOMMU blocker from global to per device

In reply to Zhenzhong Duan

Contrary to multiple device blocker which needs to consider already-attached devices to unblock/block dynamically, the vIOMMU migration blocker is a device specific config. Meaning it only needs to know whether the device is bypassing or not the vIOMMU (via machine property, or per pxb-pcie::bypass_iommu), and does not need the state of currently present devices. For this reason, the vIOMMU global migration blocker can be consolidated into the per-device migration blocker, allowing us to remove some unnecessary code.

This change also makes vfio_mig_active() more accurate as it doesn't check for global blocker.

---
 hw/vfio/common.c              | 51 ++---------------------------------
 hw/vfio/migration.c           |  7 ++---
 hw/vfio/pci.c                 |  1 -
 include/hw/vfio/vfio-common.h |  3 +--
 4 files changed, 7 insertions(+), 55 deletions(-)
hw/vfio/common.c +2 -49
index 77e2ee0e5c6e..9aac21abb76e 100644
@@ -362,7 +362,6 @@ bool vfio_mig_active(void)
 }
 
 static Error *multiple_devices_migration_blocker;
-static Error *giommu_migration_blocker;
 
 static unsigned int vfio_migratable_device_num(void)
 {
@@ -420,55 +419,9 @@ void vfio_unblock_multiple_devices_migration(void)
     multiple_devices_migration_blocker = NULL;
 }
 
-static bool vfio_viommu_preset(void)
+bool vfio_viommu_preset(VFIODevice *vbasedev)
 {
-    VFIOAddressSpace *space;
-
-    QLIST_FOREACH(space, &vfio_address_spaces, list) {
-        if (space->as != &address_space_memory) {
-            return true;
-        }
-    }
-
-    return false;
-}
-
-int vfio_block_giommu_migration(VFIODevice *vbasedev, Error **errp)
-{
-    int ret;
-
-    if (giommu_migration_blocker ||
-        !vfio_viommu_preset()) {
-        return 0;
-    }
-
-    if (vbasedev->enable_migration == ON_OFF_AUTO_ON) {
-        error_setg(errp,
-                   "Migration is currently not supported with vIOMMU enabled");
-        return -EINVAL;
-    }
-
-    error_setg(&giommu_migration_blocker,
-               "Migration is currently not supported with vIOMMU enabled");
-    ret = migrate_add_blocker(giommu_migration_blocker, errp);
-    if (ret < 0) {
-        error_free(giommu_migration_blocker);
-        giommu_migration_blocker = NULL;
-    }
-
-    return ret;
-}
-
-void vfio_migration_finalize(void)
-{
-    if (!giommu_migration_blocker ||
-        vfio_viommu_preset()) {
-        return;
-    }
-
-    migrate_del_blocker(giommu_migration_blocker);
-    error_free(giommu_migration_blocker);
-    giommu_migration_blocker = NULL;
+    return vbasedev->group->container->space->as != &address_space_memory;
 }
 
 static void vfio_set_migration_error(int err)
hw/vfio/migration.c +4 -3
index 1db7d52ab2c1..e6e5e85f7580 100644
@@ -878,9 +878,10 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
         return ret;
     }
 
-    ret = vfio_block_giommu_migration(vbasedev, errp);
-    if (ret) {
-        return ret;
+    if (vfio_viommu_preset(vbasedev)) {
+        error_setg(&err, "%s: Migration is currently not supported "
+                   "with vIOMMU enabled", vbasedev->name);
+        return vfio_block_migration(vbasedev, err, errp);
     }
 
     trace_vfio_migration_realize(vbasedev->name);
hw/vfio/pci.c +0 -1
index 31c4ab250fbe..c2cf7454ece6 100644
@@ -3255,7 +3255,6 @@ static void vfio_instance_finalize(Object *obj)
      */
     vfio_put_device(vdev);
     vfio_put_group(group);
-    vfio_migration_finalize();
 }
 
 static void vfio_exitfn(PCIDevice *pdev)
include/hw/vfio/vfio-common.h +1 -2
index 93429b9abba0..45167c8a8a54 100644
@@ -227,7 +227,7 @@ extern VFIOGroupList vfio_group_list;
 bool vfio_mig_active(void);
 int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp);
 void vfio_unblock_multiple_devices_migration(void);
-int vfio_block_giommu_migration(VFIODevice *vbasedev, Error **errp);
+bool vfio_viommu_preset(VFIODevice *vbasedev);
 int64_t vfio_mig_bytes_transferred(void);
 void vfio_reset_bytes_transferred(void);
 
@@ -254,6 +254,5 @@ int vfio_spapr_remove_window(VFIOContainer *container,
 
 int vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
 void vfio_migration_exit(VFIODevice *vbasedev);
-void vfio_migration_finalize(void);
 
 #endif /* HW_VFIO_VFIO_COMMON_H */
Signature · 1 line
2.34.1
Reviewed-by on v6 4/7
#3 lore raw

Re: [PATCH v6 4/7] vfio/migration: Change vIOMMU blocker from global to per device

Quoted 8 lines · Zhenzhong Duan

Contrary to multiple device blocker which needs to consider already-attached devices to unblock/block dynamically, the vIOMMU migration blocker is a device specific config. Meaning it only needs to know whether the device is bypassing or not the vIOMMU (via machine property, or per pxb-pcie::bypass_iommu), and does not need the state of currently present devices. For this reason, the vIOMMU global migration blocker can be consolidated into the per-device migration blocker, allowing us to remove some unnecessary code.

Quoted 5 lines

This change also makes vfio_mig_active() more accurate as it doesn't check for global blocker.

Thanks,

C.

Quoted 127 lines
---
  hw/vfio/common.c              | 51 ++---------------------------------
  hw/vfio/migration.c           |  7 ++---
  hw/vfio/pci.c                 |  1 -
  include/hw/vfio/vfio-common.h |  3 +--
  4 files changed, 7 insertions(+), 55 deletions(-)
hw/vfio/common.c +2 -49
index 77e2ee0e5c6e..9aac21abb76e 100644
@@ -362,7 +362,6 @@ bool vfio_mig_active(void)
  }
 
  static Error *multiple_devices_migration_blocker;
-static Error *giommu_migration_blocker;
 
  static unsigned int vfio_migratable_device_num(void)
  {
@@ -420,55 +419,9 @@ void vfio_unblock_multiple_devices_migration(void)
      multiple_devices_migration_blocker = NULL;
  }
 
-static bool vfio_viommu_preset(void)
+bool vfio_viommu_preset(VFIODevice *vbasedev)
  {
-    VFIOAddressSpace *space;
-
-    QLIST_FOREACH(space, &vfio_address_spaces, list) {
-        if (space->as != &address_space_memory) {
-            return true;
-        }
-    }
-
-    return false;
-}
-
-int vfio_block_giommu_migration(VFIODevice *vbasedev, Error **errp)
-{
-    int ret;
-
-    if (giommu_migration_blocker ||
-        !vfio_viommu_preset()) {
-        return 0;
-    }
-
-    if (vbasedev->enable_migration == ON_OFF_AUTO_ON) {
-        error_setg(errp,
-                   "Migration is currently not supported with vIOMMU enabled");
-        return -EINVAL;
-    }
-
-    error_setg(&giommu_migration_blocker,
-               "Migration is currently not supported with vIOMMU enabled");
-    ret = migrate_add_blocker(giommu_migration_blocker, errp);
-    if (ret < 0) {
-        error_free(giommu_migration_blocker);
-        giommu_migration_blocker = NULL;
-    }
-
-    return ret;
-}
-
-void vfio_migration_finalize(void)
-{
-    if (!giommu_migration_blocker ||
-        vfio_viommu_preset()) {
-        return;
-    }
-
-    migrate_del_blocker(giommu_migration_blocker);
-    error_free(giommu_migration_blocker);
-    giommu_migration_blocker = NULL;
+    return vbasedev->group->container->space->as != &address_space_memory;
  }
 
  static void vfio_set_migration_error(int err)
hw/vfio/migration.c +4 -3
index 1db7d52ab2c1..e6e5e85f7580 100644
@@ -878,9 +878,10 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
          return ret;
      }
 
-    ret = vfio_block_giommu_migration(vbasedev, errp);
-    if (ret) {
-        return ret;
+    if (vfio_viommu_preset(vbasedev)) {
+        error_setg(&err, "%s: Migration is currently not supported "
+                   "with vIOMMU enabled", vbasedev->name);
+        return vfio_block_migration(vbasedev, err, errp);
      }
 
      trace_vfio_migration_realize(vbasedev->name);
hw/vfio/pci.c +0 -1
index 31c4ab250fbe..c2cf7454ece6 100644
@@ -3255,7 +3255,6 @@ static void vfio_instance_finalize(Object *obj)
       */
      vfio_put_device(vdev);
      vfio_put_group(group);
-    vfio_migration_finalize();
  }
 
  static void vfio_exitfn(PCIDevice *pdev)
include/hw/vfio/vfio-common.h +1 -2
index 93429b9abba0..45167c8a8a54 100644
@@ -227,7 +227,7 @@ extern VFIOGroupList vfio_group_list;
  bool vfio_mig_active(void);
  int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp);
  void vfio_unblock_multiple_devices_migration(void);
-int vfio_block_giommu_migration(VFIODevice *vbasedev, Error **errp);
+bool vfio_viommu_preset(VFIODevice *vbasedev);
  int64_t vfio_mig_bytes_transferred(void);
  void vfio_reset_bytes_transferred(void);
 
@@ -254,6 +254,5 @@ int vfio_spapr_remove_window(VFIOContainer *container,
 
  int vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
  void vfio_migration_exit(VFIODevice *vbasedev);
-void vfio_migration_finalize(void);
 
  #endif /* HW_VFIO_VFIO_COMMON_H */
#4 lore raw

PATCH v6 5/7 vfio/migration: Free resources when vfio_migration_realize fails

In reply to Zhenzhong Duan

When vfio_realize() succeeds, hot unplug will call vfio_exitfn() to free resources allocated in vfio_realize(); when vfio_realize() fails, vfio_exitfn() is never called and we need to free resources in vfio_realize().

In the case that vfio_migration_realize() fails,
e.g: with -only-migratable & enable-migration=off, we see below:

(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
0000:81:11.1: Migration disabled
Error: disallowing migration blocker (--only-migratable) for: 0000:81:11.1: Migration is disabled for VFIO device

If we hotplug again we should see same log as above, but we see:
(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
Error: vfio 0000:81:11.1: device is already attached

That's because some references to VFIO device isn't released. For resources allocated in vfio_migration_realize(), free them by jumping to out_deinit path with calling a new function vfio_migration_deinit(). For resources allocated in vfio_realize(), free them by jumping to de-register path in vfio_realize().

---
 hw/vfio/migration.c | 33 +++++++++++++++++++++++----------
 hw/vfio/pci.c       |  1 +
 2 files changed, 24 insertions(+), 10 deletions(-)
hw/vfio/migration.c +23 -10
index e6e5e85f7580..e3954570c853 100644
@@ -802,6 +802,17 @@ static int vfio_migration_init(VFIODevice *vbasedev)
     return 0;
 }
 
+static void vfio_migration_deinit(VFIODevice *vbasedev)
+{
+    VFIOMigration *migration = vbasedev->migration;
+
+    remove_migration_state_change_notifier(&migration->migration_state);
+    qemu_del_vm_change_state_handler(migration->vm_state);
+    unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
+    vfio_migration_free(vbasedev);
+    vfio_unblock_multiple_devices_migration();
+}
+
 static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
 {
     int ret;
@@ -866,7 +877,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
             error_setg(&err,
                        "%s: VFIO device doesn't support device dirty tracking",
                        vbasedev->name);
-            return vfio_block_migration(vbasedev, err, errp);
+            goto add_blocker;
         }
 
         warn_report("%s: VFIO device doesn't support device dirty tracking",
@@ -875,29 +886,31 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
 
     ret = vfio_block_multiple_devices_migration(vbasedev, errp);
     if (ret) {
-        return ret;
+        goto out_deinit;
     }
 
     if (vfio_viommu_preset(vbasedev)) {
         error_setg(&err, "%s: Migration is currently not supported "
                    "with vIOMMU enabled", vbasedev->name);
-        return vfio_block_migration(vbasedev, err, errp);
+        goto add_blocker;
     }
 
     trace_vfio_migration_realize(vbasedev->name);
     return 0;
+
+add_blocker:
+    ret = vfio_block_migration(vbasedev, err, errp);
+out_deinit:
+    if (ret) {
+        vfio_migration_deinit(vbasedev);
+    }
+    return ret;
 }
 
 void vfio_migration_exit(VFIODevice *vbasedev)
 {
     if (vbasedev->migration) {
-        VFIOMigration *migration = vbasedev->migration;
-
-        remove_migration_state_change_notifier(&migration->migration_state);
-        qemu_del_vm_change_state_handler(migration->vm_state);
-        unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
-        vfio_migration_free(vbasedev);
-        vfio_unblock_multiple_devices_migration();
+        vfio_migration_deinit(vbasedev);
     }
 
     if (vbasedev->migration_blocker) {
hw/vfio/pci.c +1 -0
index c2cf7454ece6..9154dd929d07 100644
@@ -3210,6 +3210,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
         ret = vfio_migration_realize(vbasedev, errp);
         if (ret) {
             error_report("%s: Migration disabled", vbasedev->name);
+            goto out_deregister;
         }
     }
 
Signature · 1 line
2.34.1
Reviewed-by on v6 5/7
#5 lore raw

Re: [PATCH v6 5/7] vfio/migration: Free resources when vfio_migration_realize fails

Quoted 16 lines · Zhenzhong Duan

When vfio_realize() succeeds, hot unplug will call vfio_exitfn() to free resources allocated in vfio_realize(); when vfio_realize() fails, vfio_exitfn() is never called and we need to free resources in vfio_realize().

In the case that vfio_migration_realize() fails,
e.g: with -only-migratable & enable-migration=off, we see below:

(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
0000:81:11.1: Migration disabled
Error: disallowing migration blocker (--only-migratable) for: 0000:81:11.1: Migration is disabled for VFIO device

If we hotplug again we should see same log as above, but we see:
(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
Error: vfio 0000:81:11.1: device is already attached

Quoted 7 lines

That's because some references to VFIO device isn't released. For resources allocated in vfio_migration_realize(), free them by jumping to out_deinit path with calling a new function vfio_migration_deinit(). For resources allocated in vfio_realize(), free them by jumping to de-register path in vfio_realize().

Quoted 89 lines
---
 hw/vfio/migration.c | 33 +++++++++++++++++++++++----------
 hw/vfio/pci.c       |  1 +
 2 files changed, 24 insertions(+), 10 deletions(-)
hw/vfio/migration.c +23 -10
index e6e5e85f7580..e3954570c853 100644
@@ -802,6 +802,17 @@ static int vfio_migration_init(VFIODevice *vbasedev)
     return 0;
 }
 
+static void vfio_migration_deinit(VFIODevice *vbasedev)
+{
+    VFIOMigration *migration = vbasedev->migration;
+
+    remove_migration_state_change_notifier(&migration->migration_state);
+    qemu_del_vm_change_state_handler(migration->vm_state);
+    unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
+    vfio_migration_free(vbasedev);
+    vfio_unblock_multiple_devices_migration();
+}
+
 static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
 {
     int ret;
@@ -866,7 +877,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
             error_setg(&err,
                        "%s: VFIO device doesn't support device dirty tracking",
                        vbasedev->name);
-            return vfio_block_migration(vbasedev, err, errp);
+            goto add_blocker;
         }
 
         warn_report("%s: VFIO device doesn't support device dirty tracking",
@@ -875,29 +886,31 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
 
     ret = vfio_block_multiple_devices_migration(vbasedev, errp);
     if (ret) {
-        return ret;
+        goto out_deinit;
     }
 
     if (vfio_viommu_preset(vbasedev)) {
         error_setg(&err, "%s: Migration is currently not supported "
                    "with vIOMMU enabled", vbasedev->name);
-        return vfio_block_migration(vbasedev, err, errp);
+        goto add_blocker;
     }
 
     trace_vfio_migration_realize(vbasedev->name);
     return 0;
+
+add_blocker:
+    ret = vfio_block_migration(vbasedev, err, errp);
+out_deinit:
+    if (ret) {
+        vfio_migration_deinit(vbasedev);
+    }
+    return ret;
 }
 
 void vfio_migration_exit(VFIODevice *vbasedev)
 {
     if (vbasedev->migration) {
-        VFIOMigration *migration = vbasedev->migration;
-
-        remove_migration_state_change_notifier(&migration->migration_state);
-        qemu_del_vm_change_state_handler(migration->vm_state);
-        unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
-        vfio_migration_free(vbasedev);
-        vfio_unblock_multiple_devices_migration();
+        vfio_migration_deinit(vbasedev);
     }
 
     if (vbasedev->migration_blocker) {
hw/vfio/pci.c +1 -0
index c2cf7454ece6..9154dd929d07 100644
@@ -3210,6 +3210,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
         ret = vfio_migration_realize(vbasedev, errp);
         if (ret) {
             error_report("%s: Migration disabled", vbasedev->name);
+            goto out_deregister;
         }
     }
 
#6 lore raw

Re: [PATCH v6 5/7] vfio/migration: Free resources when vfio_migration_realize fails

In reply to Zhenzhong Duan
Quoted 19 lines · Zhenzhong Duan

External email: Use caution opening links or attachments

When vfio_realize() succeeds, hot unplug will call vfio_exitfn() to free resources allocated in vfio_realize(); when vfio_realize() fails, vfio_exitfn() is never called and we need to free resources in vfio_realize().

In the case that vfio_migration_realize() fails,
e.g: with -only-migratable & enable-migration=off, we see below:

(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
0000:81:11.1: Migration disabled
Error: disallowing migration blocker (--only-migratable) for: 0000:81:11.1: Migration is disabled for VFIO device

If we hotplug again we should see same log as above, but we see:
(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
Error: vfio 0000:81:11.1: device is already attached

Quoted 5 lines

That's because some references to VFIO device isn't released. For resources allocated in vfio_migration_realize(), free them by jumping to out_deinit path with calling a new function vfio_migration_deinit(). For resources allocated in vfio_realize(), free them by jumping to de-register path in vfio_realize().

Should we add Fixes tag?

Thanks.

Quoted 93 lines
---
  hw/vfio/migration.c | 33 +++++++++++++++++++++++----------
  hw/vfio/pci.c       |  1 +
  2 files changed, 24 insertions(+), 10 deletions(-)
hw/vfio/migration.c +23 -10
index e6e5e85f7580..e3954570c853 100644
@@ -802,6 +802,17 @@ static int vfio_migration_init(VFIODevice *vbasedev)
      return 0;
  }
 
+static void vfio_migration_deinit(VFIODevice *vbasedev)
+{
+    VFIOMigration *migration = vbasedev->migration;
+
+    remove_migration_state_change_notifier(&migration->migration_state);
+    qemu_del_vm_change_state_handler(migration->vm_state);
+    unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
+    vfio_migration_free(vbasedev);
+    vfio_unblock_multiple_devices_migration();
+}
+
  static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
  {
      int ret;
@@ -866,7 +877,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
              error_setg(&err,
                         "%s: VFIO device doesn't support device dirty tracking",
                         vbasedev->name);
-            return vfio_block_migration(vbasedev, err, errp);
+            goto add_blocker;
          }
 
          warn_report("%s: VFIO device doesn't support device dirty tracking",
@@ -875,29 +886,31 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
 
      ret = vfio_block_multiple_devices_migration(vbasedev, errp);
      if (ret) {
-        return ret;
+        goto out_deinit;
      }
 
      if (vfio_viommu_preset(vbasedev)) {
          error_setg(&err, "%s: Migration is currently not supported "
                     "with vIOMMU enabled", vbasedev->name);
-        return vfio_block_migration(vbasedev, err, errp);
+        goto add_blocker;
      }
 
      trace_vfio_migration_realize(vbasedev->name);
      return 0;
+
+add_blocker:
+    ret = vfio_block_migration(vbasedev, err, errp);
+out_deinit:
+    if (ret) {
+        vfio_migration_deinit(vbasedev);
+    }
+    return ret;
  }
 
  void vfio_migration_exit(VFIODevice *vbasedev)
  {
      if (vbasedev->migration) {
-        VFIOMigration *migration = vbasedev->migration;
-
-        remove_migration_state_change_notifier(&migration->migration_state);
-        qemu_del_vm_change_state_handler(migration->vm_state);
-        unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
-        vfio_migration_free(vbasedev);
-        vfio_unblock_multiple_devices_migration();
+        vfio_migration_deinit(vbasedev);
      }
 
      if (vbasedev->migration_blocker) {
hw/vfio/pci.c +1 -1
index c2cf7454ece6..9154dd929d07 100644
@@ -3210,6 +3210,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
          ret = vfio_migration_realize(vbasedev, errp);
          if (ret) {
              error_report("%s: Migration disabled", vbasedev->name);
+            goto out_deregister;
          }
      }
 
--

2.34.1

#7 lore raw

Re: [PATCH v6 5/7] vfio/migration: Free resources when vfio_migration_realize fails

Quoted 21 lines · Avihai Horon
Quoted 19 lines · Zhenzhong Duan

External email: Use caution opening links or attachments

When vfio_realize() succeeds, hot unplug will call vfio_exitfn() to free resources allocated in vfio_realize(); when vfio_realize() fails, vfio_exitfn() is never called and we need to free resources in vfio_realize().

In the case that vfio_migration_realize() fails,
e.g: with -only-migratable & enable-migration=off, we see below:

(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
0000:81:11.1: Migration disabled
Error: disallowing migration blocker (--only-migratable) for: 0000:81:11.1: Migration is disabled for VFIO device

If we hotplug again we should see same log as above, but we see:
(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
Error: vfio 0000:81:11.1: device is already attached

Quoted 7 lines
Quoted 5 lines

That's because some references to VFIO device isn't released. For resources allocated in vfio_migration_realize(), free them by jumping to out_deinit path with calling a new function vfio_migration_deinit(). For resources allocated in vfio_realize(), free them by jumping to de-register path in vfio_realize().

Should we add Fixes tag?

Yes. It would help downstream backports. No need to resend for that.

Thanks,

C.

Quoted 97 lines

Thanks.

Quoted 93 lines

---
  hw/vfio/migration.c | 33 +++++++++++++++++++++++---------- hw/vfio/pci.c       |  1 +

  2 files changed, 24 insertions(+), 10 deletions(-)
hw/vfio/migration.c +13 -2
index e6e5e85f7580..e3954570c853 100644
@@ -802,6 +802,17 @@ static int vfio_migration_init(VFIODevice *vbasedev)
       return 0;
   }
 
+static void vfio_migration_deinit(VFIODevice *vbasedev)
+{
+    VFIOMigration *migration = vbasedev->migration;
+
+    remove_migration_state_change_notifier(&migration->migration_state);
+    qemu_del_vm_change_state_handler(migration->vm_state);
+    unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
+    vfio_migration_free(vbasedev);
+    vfio_unblock_multiple_devices_migration();
+}
+
   static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
   {
       int ret;
@@ -866,7 +877,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
               error_setg(&err,
                          "%s: VFIO device doesn't support device dirty tracking",
                          vbasedev->name);
-            return vfio_block_migration(vbasedev, err, errp);
+            goto add_blocker;
           }
 
           warn_report("%s: VFIO device doesn't support device dirty tracking",
@@ -875,29 +886,31 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
 
       ret = vfio_block_multiple_devices_migration(vbasedev, errp);
       if (ret) {
-        return ret;
+        goto out_deinit;
       }
 
       if (vfio_viommu_preset(vbasedev)) {

          error_setg(&err, "%s: Migration is currently not supported " "with vIOMMU enabled", vbasedev->name); -        return vfio_block_migration(vbasedev, err, errp); +        goto add_blocker;
      }

      trace_vfio_migration_realize(vbasedev->name); return 0;

hw/vfio/migration.c +8 -0
+
+add_blocker:
+    ret = vfio_block_migration(vbasedev, err, errp);
+out_deinit:
+    if (ret) {
+        vfio_migration_deinit(vbasedev);
+    }
+    return ret;

  }

  void vfio_migration_exit(VFIODevice *vbasedev)
  {
      if (vbasedev->migration) {
-        VFIOMigration *migration = vbasedev->migration;
-
-        remove_migration_state_change_notifier(&migration->migration_state); -        qemu_del_vm_change_state_handler(migration->vm_state);
-        unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev); -        vfio_migration_free(vbasedev);
-        vfio_unblock_multiple_devices_migration();
+        vfio_migration_deinit(vbasedev);
      }

      if (vbasedev->migration_blocker) {

hw/vfio/pci.c +1 -0
index c2cf7454ece6..9154dd929d07 100644
@@ -3210,6 +3210,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
           ret = vfio_migration_realize(vbasedev, errp);
           if (ret) {
               error_report("%s: Migration disabled", vbasedev->name);
+            goto out_deregister;
           }
       }
 
Signature · 1 line
2.34.1
Reviewed-by on v6 5/7
#8 lore raw

Re: [PATCH v6 5/7] vfio/migration: Free resources when vfio_migration_realize fails

In reply to Zhenzhong Duan
Quoted 16 lines · Zhenzhong Duan

When vfio_realize() succeeds, hot unplug will call vfio_exitfn() to free resources allocated in vfio_realize(); when vfio_realize() fails, vfio_exitfn() is never called and we need to free resources in vfio_realize().

In the case that vfio_migration_realize() fails,
e.g: with -only-migratable & enable-migration=off, we see below:

(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
0000:81:11.1: Migration disabled
Error: disallowing migration blocker (--only-migratable) for: 0000:81:11.1: Migration is disabled for VFIO device

If we hotplug again we should see same log as above, but we see:
(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
Error: vfio 0000:81:11.1: device is already attached

Quoted 7 lines

That's because some references to VFIO device isn't released. For resources allocated in vfio_migration_realize(), free them by jumping to out_deinit path with calling a new function vfio_migration_deinit(). For resources allocated in vfio_realize(), free them by jumping to de-register path in vfio_realize().

The vfio_migration_realize() routine is somewhat difficult to follow but I don't see how to improve it. May be could move the viommu test at the beginning ? Anyhow,

Thanks,

C.

Quoted 89 lines
---
  hw/vfio/migration.c | 33 +++++++++++++++++++++++----------
  hw/vfio/pci.c       |  1 +
  2 files changed, 24 insertions(+), 10 deletions(-)
hw/vfio/migration.c +23 -10
index e6e5e85f7580..e3954570c853 100644
@@ -802,6 +802,17 @@ static int vfio_migration_init(VFIODevice *vbasedev)
      return 0;
  }
 
+static void vfio_migration_deinit(VFIODevice *vbasedev)
+{
+    VFIOMigration *migration = vbasedev->migration;
+
+    remove_migration_state_change_notifier(&migration->migration_state);
+    qemu_del_vm_change_state_handler(migration->vm_state);
+    unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
+    vfio_migration_free(vbasedev);
+    vfio_unblock_multiple_devices_migration();
+}
+
  static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
  {
      int ret;
@@ -866,7 +877,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
              error_setg(&err,
                         "%s: VFIO device doesn't support device dirty tracking",
                         vbasedev->name);
-            return vfio_block_migration(vbasedev, err, errp);
+            goto add_blocker;
          }
 
          warn_report("%s: VFIO device doesn't support device dirty tracking",
@@ -875,29 +886,31 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
 
      ret = vfio_block_multiple_devices_migration(vbasedev, errp);
      if (ret) {
-        return ret;
+        goto out_deinit;
      }
 
      if (vfio_viommu_preset(vbasedev)) {
          error_setg(&err, "%s: Migration is currently not supported "
                     "with vIOMMU enabled", vbasedev->name);
-        return vfio_block_migration(vbasedev, err, errp);
+        goto add_blocker;
      }
 
      trace_vfio_migration_realize(vbasedev->name);
      return 0;
+
+add_blocker:
+    ret = vfio_block_migration(vbasedev, err, errp);
+out_deinit:
+    if (ret) {
+        vfio_migration_deinit(vbasedev);
+    }
+    return ret;
  }
 
  void vfio_migration_exit(VFIODevice *vbasedev)
  {
      if (vbasedev->migration) {
-        VFIOMigration *migration = vbasedev->migration;
-
-        remove_migration_state_change_notifier(&migration->migration_state);
-        qemu_del_vm_change_state_handler(migration->vm_state);
-        unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
-        vfio_migration_free(vbasedev);
-        vfio_unblock_multiple_devices_migration();
+        vfio_migration_deinit(vbasedev);
      }
 
      if (vbasedev->migration_blocker) {
hw/vfio/pci.c +1 -0
index c2cf7454ece6..9154dd929d07 100644
@@ -3210,6 +3210,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
          ret = vfio_migration_realize(vbasedev, errp);
          if (ret) {
              error_report("%s: Migration disabled", vbasedev->name);
+            goto out_deregister;
          }
      }
 
#9 lore raw

RE: [PATCH v6 5/7] vfio/migration: Free resources when vfio_migration_realize fails

Quoted 32 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Monday, July 3, 2023 11:45 PM
Subject: Re: [PATCH v6 5/7] vfio/migration: Free resources when vfio_migration_realize fails

Quoted 25 lines · Zhenzhong Duan

When vfio_realize() succeeds, hot unplug will call vfio_exitfn() to free resources allocated in vfio_realize(); when vfio_realize() fails, vfio_exitfn() is never called and we need to free resources in vfio_realize().

In the case that vfio_migration_realize() fails,
e.g: with -only-migratable & enable-migration=off, we see below:

(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off 0000:81:11.1: Migration disabled
Error: disallowing migration blocker (--only-migratable) for:
0000:81:11.1: Migration is disabled for VFIO device

If we hotplug again we should see same log as above, but we see:
(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable-migration=off
Error: vfio 0000:81:11.1: device is already attached

That's because some references to VFIO device isn't released. For resources allocated in vfio_migration_realize(), free them by jumping to out_deinit path with calling a new function vfio_migration_deinit(). For resources allocated in vfio_realize(), free them by jumping to de-register path in vfio_realize().

Quoted 4 lines
Quoted 1 line

The vfio_migration_realize() routine is somewhat difficult to follow but I don't see how to improve it. May be could move the viommu test at the beginning ?

Is your purpose to remove vfio_unblock_multiple_devices_migration() from vfio_migration_deinit()? Or other benefit I misses?

Thanks
Zhenzhong

#10 lore raw

RE: [PATCH v6 5/7] vfio/migration: Free resources when vfio_migration_realize fails

In reply to Zhenzhong Duan
Quoted 28 lines

-----Original Message-----
From: Duan, Zhenzhong <[email protected]>
Sent: Monday, July 3, 2023 3:15 PM
To: [email protected]
Cc: [email protected]; [email protected]; Martins, Joao <[email protected]>; [email protected]; Peng, Chao P <[email protected]>
Subject: [PATCH v6 5/7] vfio/migration: Free resources when vfio_migration_realize fails

When vfio_realize() succeeds, hot unplug will call vfio_exitfn() to free resources allocated in vfio_realize(); when vfio_realize() fails, vfio_exitfn() is never called and we need to free resources in vfio_realize().

In the case that vfio_migration_realize() fails,
e.g: with -only-migratable & enable-migration=off, we see below:

(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable- migration=off
0000:81:11.1: Migration disabled
Error: disallowing migration blocker (--only-migratable) for: 0000:81:11.1:
Migration is disabled for VFIO device

If we hotplug again we should see same log as above, but we see:
(qemu) device_add vfio-pci,host=81:11.1,id=vfio1,bus=root1,enable- migration=off
Error: vfio 0000:81:11.1: device is already attached

Quoted 6 lines

That's because some references to VFIO device isn't released.
For resources allocated in vfio_migration_realize(), free them by jumping to out_deinit path with calling a new function vfio_migration_deinit(). For resources allocated in vfio_realize(), free them by jumping to de-register path in vfio_realize().

Forgot fixes tag:

Fixes a22651053b59 ("vfio: Make vfio-pci device migration capable")

Thanks
Zhenzhong

Quoted 93 lines

---
hw/vfio/migration.c | 33 +++++++++++++++++++++++----------
hw/vfio/pci.c | 1 +

2 files changed, 24 insertions(+), 10 deletions(-)
hw/vfio/migration.c index renamed from hw/vfio/migration.c +0 -0

e6e5e85f7580..e3954570c853 100644

hw/vfio/migration.c +10 -0
@@ -802,6 +802,17 @@ static int vfio_migration_init(VFIODevice *vbasedev)
    return 0;
 }
 
+static void vfio_migration_deinit(VFIODevice *vbasedev) {
+    VFIOMigration *migration = vbasedev->migration;
+
+    remove_migration_state_change_notifier(&migration->migration_state);
+    qemu_del_vm_change_state_handler(migration->vm_state);
+    unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
+    vfio_migration_free(vbasedev);
+    vfio_unblock_multiple_devices_migration();
+}
+

static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error

**errp)  {
    int ret;
hw/vfio/migration.c +1 -1
@@ -866,7 +877,7 @@ int vfio_migration_realize(VFIODevice *vbasedev,
 Error **errp)
            error_setg(&err,
                       "%s: VFIO device doesn't support device dirty tracking",
                       vbasedev->name);
-            return vfio_block_migration(vbasedev, err, errp);
+            goto add_blocker;
        }
 

warn_report("%s: VFIO device doesn't support device dirty tracking",

hw/vfio/migration.c +11 -9
@@ -875,29 +886,31 @@ int vfio_migration_realize(VFIODevice *vbasedev,
 Error **errp)
 
    ret = vfio_block_multiple_devices_migration(vbasedev, errp);
    if (ret) {
-        return ret;
+        goto out_deinit;
    }
 
    if (vfio_viommu_preset(vbasedev)) {
        error_setg(&err, "%s: Migration is currently not supported "
                   "with vIOMMU enabled", vbasedev->name);
-        return vfio_block_migration(vbasedev, err, errp);
+        goto add_blocker;
    }
 
    trace_vfio_migration_realize(vbasedev->name);
    return 0;
+
+add_blocker:
+    ret = vfio_block_migration(vbasedev, err, errp);
+out_deinit:
+    if (ret) {
+        vfio_migration_deinit(vbasedev);
+    }
+    return ret;
 }
 
 void vfio_migration_exit(VFIODevice *vbasedev)  {
    if (vbasedev->migration) {
-        VFIOMigration *migration = vbasedev->migration;
-
-        remove_migration_state_change_notifier(&migration->migration_state);
-        qemu_del_vm_change_state_handler(migration->vm_state);
-        unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
-        vfio_migration_free(vbasedev);
-        vfio_unblock_multiple_devices_migration();
+        vfio_migration_deinit(vbasedev);
    }
 
    if (vbasedev->migration_blocker) {
hw/vfio/pci.c index c2cf7454ece6..9154dd929d07 renamed from hw/vfio/pci.c +0 -0

100644

hw/vfio/pci.c +1 -1
@@ -3210,6 +3210,7 @@ static void vfio_realize(PCIDevice *pdev, Error
 **errp)
        ret = vfio_migration_realize(vbasedev, errp);
        if (ret) {
            error_report("%s: Migration disabled", vbasedev->name);
+            goto out_deregister;
        }
    }
 
--

2.34.1

#11 lore raw

PATCH v6 6/7 vfio/migration: Remove print of "Migration disabled"

In reply to Zhenzhong Duan

Property enable_migration supports [on/off/auto].
In ON mode, error pointer is passed to errp and logged.
In OFF mode, we doesn't need to log "Migration disabled" as it's intentional. In AUTO mode, we should only ever see errors or warnings if the device supports migration and an error or incompatibility occurs while further probing or configuring it. Lack of support for migration shoundn't generate an error or warning.

---
 hw/vfio/pci.c | 1 -
 1 file changed, 1 deletion(-)
hw/vfio/pci.c +0 -1
index 9154dd929d07..eefd4ec330d9 100644
@@ -3209,7 +3209,6 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
     if (!pdev->failover_pair_id) {
         ret = vfio_migration_realize(vbasedev, errp);
         if (ret) {
-            error_report("%s: Migration disabled", vbasedev->name);
             goto out_deregister;
         }
     }
Signature · 1 line
2.34.1
Reviewed-by on v6 6/7
#12 lore raw

Re: [PATCH v6 6/7] vfio/migration: Remove print of "Migration disabled"

Quoted 10 lines · Zhenzhong Duan

Property enable_migration supports [on/off/auto].
In ON mode, error pointer is passed to errp and logged.
In OFF mode, we doesn't need to log "Migration disabled" as it's intentional. In AUTO mode, we should only ever see errors or warnings if the device supports migration and an error or incompatibility occurs while further probing or configuring it. Lack of support for migration shoundn't generate an error or warning.

Thanks,

C.

Quoted 16 lines
---
  hw/vfio/pci.c | 1 -
  1 file changed, 1 deletion(-)
hw/vfio/pci.c +0 -1
index 9154dd929d07..eefd4ec330d9 100644
@@ -3209,7 +3209,6 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
      if (!pdev->failover_pair_id) {
          ret = vfio_migration_realize(vbasedev, errp);
          if (ret) {
-            error_report("%s: Migration disabled", vbasedev->name);
              goto out_deregister;
          }
      }
#13 lore raw

PATCH v6 7/7 vfio/migration: Return bool type for vfio_migration_realize()

In reply to Zhenzhong Duan

Make vfio_migration_realize() adhere to the convention of other realize() callbacks(like qdev_realize) by returning bool instead of int.

---
 hw/vfio/migration.c           | 15 ++++++++++-----
 hw/vfio/pci.c                 |  3 +--
 include/hw/vfio/vfio-common.h |  2 +-
 3 files changed, 12 insertions(+), 8 deletions(-)
hw/vfio/migration.c +10 -5
index e3954570c853..2674f4bc472d 100644
@@ -846,7 +846,12 @@ void vfio_reset_bytes_transferred(void)
     bytes_transferred = 0;
 }
 
-int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
+/*
+ * Return true when either migration initialized or blocker registered.
+ * Currently only return false when adding blocker fails which will
+ * de-register vfio device.
+ */
+bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
 {
     Error *err = NULL;
     int ret;
@@ -854,7 +859,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
     if (vbasedev->enable_migration == ON_OFF_AUTO_OFF) {
         error_setg(&err, "%s: Migration is disabled for VFIO device",
                    vbasedev->name);
-        return vfio_block_migration(vbasedev, err, errp);
+        return !vfio_block_migration(vbasedev, err, errp);
     }
 
     ret = vfio_migration_init(vbasedev);
@@ -869,7 +874,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
                        vbasedev->name, ret, strerror(-ret));
         }
 
-        return vfio_block_migration(vbasedev, err, errp);
+        return !vfio_block_migration(vbasedev, err, errp);
     }
 
     if (!vbasedev->dirty_pages_supported) {
@@ -896,7 +901,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
     }
 
     trace_vfio_migration_realize(vbasedev->name);
-    return 0;
+    return true;
 
 add_blocker:
     ret = vfio_block_migration(vbasedev, err, errp);
@@ -904,7 +909,7 @@ out_deinit:
     if (ret) {
         vfio_migration_deinit(vbasedev);
     }
-    return ret;
+    return !ret;
 }
 
 void vfio_migration_exit(VFIODevice *vbasedev)
hw/vfio/pci.c +1 -2
index eefd4ec330d9..68dd99283620 100644
@@ -3207,8 +3207,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
     }
 
     if (!pdev->failover_pair_id) {
-        ret = vfio_migration_realize(vbasedev, errp);
-        if (ret) {
+        if (!vfio_migration_realize(vbasedev, errp)) {
             goto out_deregister;
         }
     }
include/hw/vfio/vfio-common.h +1 -1
index 45167c8a8a54..da43d273524e 100644
@@ -252,7 +252,7 @@ int vfio_spapr_create_window(VFIOContainer *container,
 int vfio_spapr_remove_window(VFIOContainer *container,
                              hwaddr offset_within_address_space);
 
-int vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
+bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
 void vfio_migration_exit(VFIODevice *vbasedev);
 
 #endif /* HW_VFIO_VFIO_COMMON_H */
Signature · 1 line
2.34.1
Reviewed-by on v6 7/7
#14 lore raw

Re: [PATCH v6 7/7] vfio/migration: Return bool type for vfio_migration_realize()

Quoted 6 lines · Zhenzhong Duan

Make vfio_migration_realize() adhere to the convention of other realize() callbacks(like qdev_realize) by returning bool instead of int.

Thanks,

C.

Quoted 87 lines
---
  hw/vfio/migration.c           | 15 ++++++++++-----
  hw/vfio/pci.c                 |  3 +--
  include/hw/vfio/vfio-common.h |  2 +-
  3 files changed, 12 insertions(+), 8 deletions(-)
hw/vfio/migration.c +10 -5
index e3954570c853..2674f4bc472d 100644
@@ -846,7 +846,12 @@ void vfio_reset_bytes_transferred(void)
      bytes_transferred = 0;
  }
 
-int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
+/*
+ * Return true when either migration initialized or blocker registered.
+ * Currently only return false when adding blocker fails which will
+ * de-register vfio device.
+ */
+bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
  {
      Error *err = NULL;
      int ret;
@@ -854,7 +859,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
      if (vbasedev->enable_migration == ON_OFF_AUTO_OFF) {
          error_setg(&err, "%s: Migration is disabled for VFIO device",
                     vbasedev->name);
-        return vfio_block_migration(vbasedev, err, errp);
+        return !vfio_block_migration(vbasedev, err, errp);
      }
 
      ret = vfio_migration_init(vbasedev);
@@ -869,7 +874,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
                         vbasedev->name, ret, strerror(-ret));
          }
 
-        return vfio_block_migration(vbasedev, err, errp);
+        return !vfio_block_migration(vbasedev, err, errp);
      }
 
      if (!vbasedev->dirty_pages_supported) {
@@ -896,7 +901,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
      }
 
      trace_vfio_migration_realize(vbasedev->name);
-    return 0;
+    return true;
 
  add_blocker:
      ret = vfio_block_migration(vbasedev, err, errp);
@@ -904,7 +909,7 @@ out_deinit:
      if (ret) {
          vfio_migration_deinit(vbasedev);
      }
-    return ret;
+    return !ret;
  }
 
  void vfio_migration_exit(VFIODevice *vbasedev)
hw/vfio/pci.c +1 -2
index eefd4ec330d9..68dd99283620 100644
@@ -3207,8 +3207,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
      }
 
      if (!pdev->failover_pair_id) {
-        ret = vfio_migration_realize(vbasedev, errp);
-        if (ret) {
+        if (!vfio_migration_realize(vbasedev, errp)) {
              goto out_deregister;
          }
      }
include/hw/vfio/vfio-common.h +1 -1
index 45167c8a8a54..da43d273524e 100644
@@ -252,7 +252,7 @@ int vfio_spapr_create_window(VFIOContainer *container,
  int vfio_spapr_remove_window(VFIOContainer *container,
                               hwaddr offset_within_address_space);
 
-int vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
+bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
  void vfio_migration_exit(VFIODevice *vbasedev);
 
  #endif /* HW_VFIO_VFIO_COMMON_H */
Reviewed-by on v6 7/7
#15 lore raw

Re: [PATCH v6 7/7] vfio/migration: Return bool type for vfio_migration_realize()

In reply to Zhenzhong Duan
Quoted 6 lines · Zhenzhong Duan

Make vfio_migration_realize() adhere to the convention of other realize() callbacks(like qdev_realize) by returning bool instead of int.

Quoted 87 lines
---
 hw/vfio/migration.c           | 15 ++++++++++-----
 hw/vfio/pci.c                 |  3 +--
 include/hw/vfio/vfio-common.h |  2 +-
 3 files changed, 12 insertions(+), 8 deletions(-)
hw/vfio/migration.c +10 -5
index e3954570c853..2674f4bc472d 100644
@@ -846,7 +846,12 @@ void vfio_reset_bytes_transferred(void)
     bytes_transferred = 0;
 }
 
-int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
+/*
+ * Return true when either migration initialized or blocker registered.
+ * Currently only return false when adding blocker fails which will
+ * de-register vfio device.
+ */
+bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
 {
     Error *err = NULL;
     int ret;
@@ -854,7 +859,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
     if (vbasedev->enable_migration == ON_OFF_AUTO_OFF) {
         error_setg(&err, "%s: Migration is disabled for VFIO device",
                    vbasedev->name);
-        return vfio_block_migration(vbasedev, err, errp);
+        return !vfio_block_migration(vbasedev, err, errp);
     }
 
     ret = vfio_migration_init(vbasedev);
@@ -869,7 +874,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
                        vbasedev->name, ret, strerror(-ret));
         }
 
-        return vfio_block_migration(vbasedev, err, errp);
+        return !vfio_block_migration(vbasedev, err, errp);
     }
 
     if (!vbasedev->dirty_pages_supported) {
@@ -896,7 +901,7 @@ int vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
     }
 
     trace_vfio_migration_realize(vbasedev->name);
-    return 0;
+    return true;
 
 add_blocker:
     ret = vfio_block_migration(vbasedev, err, errp);
@@ -904,7 +909,7 @@ out_deinit:
     if (ret) {
         vfio_migration_deinit(vbasedev);
     }
-    return ret;
+    return !ret;
 }
 
 void vfio_migration_exit(VFIODevice *vbasedev)
hw/vfio/pci.c +1 -2
index eefd4ec330d9..68dd99283620 100644
@@ -3207,8 +3207,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
     }
 
     if (!pdev->failover_pair_id) {
-        ret = vfio_migration_realize(vbasedev, errp);
-        if (ret) {
+        if (!vfio_migration_realize(vbasedev, errp)) {
             goto out_deregister;
         }
     }
include/hw/vfio/vfio-common.h +1 -1
index 45167c8a8a54..da43d273524e 100644
@@ -252,7 +252,7 @@ int vfio_spapr_create_window(VFIOContainer *container,
 int vfio_spapr_remove_window(VFIOContainer *container,
                              hwaddr offset_within_address_space);
 
-int vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
+bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp);
 void vfio_migration_exit(VFIODevice *vbasedev);
 
 #endif /* HW_VFIO_VFIO_COMMON_H */