Skip to contentSkip to conversation

vfio/migration: Block VFIO migration with postcopy and background snapshot

PATCH Superseded Avihai Horon 3 participants 25 messages Aug 28, 2023 – Aug 30, 2023

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover vfio/migration: Block VFIO migration with postcopy and background snapshot

Hello,

Recently added VFIO migration is not compatible with some of the pre-existing migration features. This was overlooked and today these combinations are not blocked by QEMU. This series fixes it.

Postcopy migration:
VFIO migration is not compatible with postcopy migration. A VFIO device in the destination can't handle page faults for pages that have not been sent yet. Doing such migration will cause the VM to crash in the destination.

Background snapshot:
Background snapshot allows creating a snapshot of the VM while it's running and keeping it small by not including dirty RAM pages.

The way it works is by first stopping the VM, saving the non-iterable devices' state and then starting the VM and saving the RAM while write protecting it with UFFD. The resulting snapshot represents the VM state at snapshot start.

VFIO migration is not compatible with background snapshot. First of all, VFIO device state is not even saved in background snapshot because only non-iterable device state is saved. But even if it was saved, after starting the VM, a VFIO device could dirty pages without it being detected by UFFD write protection. This would corrupt the snapshot, as the RAM in it would not represent the RAM at snapshot start.

This series blocks these combinations explicitly:
If a VFIO device is added when postcopy or background snapshot are on, a migration blocker will be added. If a VFIO device is present, setting postcopy or background snapshot capabilities will fail with an appropriate error message.

Note that this series is based on the P2P series [1] sent a few weeks ago.

Comments and suggestions will be greatly appreciated.

Thanks.

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

Avihai Horon (6):
migration: Add migration prefix to functions in target.c vfio/migration: Fail adding device with enable-migration=on and
existing blocker
vfio/migration: Add vfio_migratable_devices_num() vfio/migration: Change vfio_mig_active() semantics vfio/migration: Block VFIO migration with postcopy migration vfio/migration: Block VFIO migration with background snapshot

 include/hw/vfio/vfio-common.h |   4 ++
 migration/migration.h         |   7 +-
 hw/vfio/common.c              | 120 +++++++++++++++++++++++++++++-----
 hw/vfio/migration.c           |  12 ++++
 migration/migration.c         |   6 +-
 migration/options.c           |  26 ++++++++
 migration/savevm.c            |   2 +-
 migration/target.c            |  36 ++++++++--
 8 files changed, 187 insertions(+), 26 deletions(-)
Signature · 1 line
2.26.3
#1 lore raw

PATCH 1/6 migration: Add migration prefix to functions in target.c

The functions in target.c are not static, yet they don't have a proper migration prefix. Add such prefix.

---
 migration/migration.h | 4 ++--
 migration/migration.c | 6 +++---
 migration/savevm.c    | 2 +-
 migration/target.c    | 8 ++++----
 4 files changed, 10 insertions(+), 10 deletions(-)
migration/migration.h +2 -2
index 6eea18db36..c5695de214 100644
@@ -512,8 +512,8 @@ void migration_consume_urgent_request(void);
 bool migration_rate_limit(void);
 void migration_cancel(const Error *error);
 
-void populate_vfio_info(MigrationInfo *info);
-void reset_vfio_bytes_transferred(void);
+void migration_populate_vfio_info(MigrationInfo *info);
+void migration_reset_vfio_bytes_transferred(void);
 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
 
 #endif
migration/migration.c +3 -3
index 5528acb65e..92866a8f49 100644
@@ -1039,7 +1039,7 @@ static void fill_source_migration_info(MigrationInfo *info)
         populate_time_info(info, s);
         populate_ram_info(info, s);
         populate_disk_info(info);
-        populate_vfio_info(info);
+        migration_populate_vfio_info(info);
         break;
     case MIGRATION_STATUS_COLO:
         info->has_status = true;
@@ -1048,7 +1048,7 @@ static void fill_source_migration_info(MigrationInfo *info)
     case MIGRATION_STATUS_COMPLETED:
         populate_time_info(info, s);
         populate_ram_info(info, s);
-        populate_vfio_info(info);
+        migration_populate_vfio_info(info);
         break;
     case MIGRATION_STATUS_FAILED:
         info->has_status = true;
@@ -1641,7 +1641,7 @@ static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
      */
     memset(&mig_stats, 0, sizeof(mig_stats));
     memset(&compression_counters, 0, sizeof(compression_counters));
-    reset_vfio_bytes_transferred();
+    migration_reset_vfio_bytes_transferred();
 
     return true;
 }
migration/savevm.c +1 -1
index a2cb8855e2..5bf8b59a7d 100644
@@ -1622,7 +1622,7 @@ static int qemu_savevm_state(QEMUFile *f, Error **errp)
     migrate_init(ms);
     memset(&mig_stats, 0, sizeof(mig_stats));
     memset(&compression_counters, 0, sizeof(compression_counters));
-    reset_vfio_bytes_transferred();
+    migration_reset_vfio_bytes_transferred();
     ms->to_dst_file = f;
 
     qemu_mutex_unlock_iothread();
migration/target.c +4 -4
index f39c9a8d88..a6ffa9a5ce 100644
@@ -15,7 +15,7 @@
 #endif
 
 #ifdef CONFIG_VFIO
-void populate_vfio_info(MigrationInfo *info)
+void migration_populate_vfio_info(MigrationInfo *info)
 {
     if (vfio_mig_active()) {
         info->vfio = g_malloc0(sizeof(*info->vfio));
@@ -23,16 +23,16 @@ void populate_vfio_info(MigrationInfo *info)
     }
 }
 
-void reset_vfio_bytes_transferred(void)
+void migration_reset_vfio_bytes_transferred(void)
 {
     vfio_reset_bytes_transferred();
 }
 #else
-void populate_vfio_info(MigrationInfo *info)
+void migration_populate_vfio_info(MigrationInfo *info)
 {
 }
 
-void reset_vfio_bytes_transferred(void)
+void migration_reset_vfio_bytes_transferred(void)
 {
 }
 #endif
Signature · 1 line
2.26.3
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] migration: Add migration prefix to functions in target.c

Quoted 2 lines · Avihai Horon

The functions in target.c are not static, yet they don't have a proper migration prefix. Add such prefix.

Thanks,

C.

Quoted 101 lines
---
  migration/migration.h | 4 ++--
  migration/migration.c | 6 +++---
  migration/savevm.c    | 2 +-
  migration/target.c    | 8 ++++----
  4 files changed, 10 insertions(+), 10 deletions(-)
migration/migration.h +2 -2
index 6eea18db36..c5695de214 100644
@@ -512,8 +512,8 @@ void migration_consume_urgent_request(void);
  bool migration_rate_limit(void);
  void migration_cancel(const Error *error);
 
-void populate_vfio_info(MigrationInfo *info);
-void reset_vfio_bytes_transferred(void);
+void migration_populate_vfio_info(MigrationInfo *info);
+void migration_reset_vfio_bytes_transferred(void);
  void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
 
  #endif
migration/migration.c +3 -3
index 5528acb65e..92866a8f49 100644
@@ -1039,7 +1039,7 @@ static void fill_source_migration_info(MigrationInfo *info)
          populate_time_info(info, s);
          populate_ram_info(info, s);
          populate_disk_info(info);
-        populate_vfio_info(info);
+        migration_populate_vfio_info(info);
          break;
      case MIGRATION_STATUS_COLO:
          info->has_status = true;
@@ -1048,7 +1048,7 @@ static void fill_source_migration_info(MigrationInfo *info)
      case MIGRATION_STATUS_COMPLETED:
          populate_time_info(info, s);
          populate_ram_info(info, s);
-        populate_vfio_info(info);
+        migration_populate_vfio_info(info);
          break;
      case MIGRATION_STATUS_FAILED:
          info->has_status = true;
@@ -1641,7 +1641,7 @@ static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
       */
      memset(&mig_stats, 0, sizeof(mig_stats));
      memset(&compression_counters, 0, sizeof(compression_counters));
-    reset_vfio_bytes_transferred();
+    migration_reset_vfio_bytes_transferred();
 
      return true;
  }
migration/savevm.c +1 -1
index a2cb8855e2..5bf8b59a7d 100644
@@ -1622,7 +1622,7 @@ static int qemu_savevm_state(QEMUFile *f, Error **errp)
      migrate_init(ms);
      memset(&mig_stats, 0, sizeof(mig_stats));
      memset(&compression_counters, 0, sizeof(compression_counters));
-    reset_vfio_bytes_transferred();
+    migration_reset_vfio_bytes_transferred();
      ms->to_dst_file = f;
 
      qemu_mutex_unlock_iothread();
migration/target.c +4 -4
index f39c9a8d88..a6ffa9a5ce 100644
@@ -15,7 +15,7 @@
  #endif
 
  #ifdef CONFIG_VFIO
-void populate_vfio_info(MigrationInfo *info)
+void migration_populate_vfio_info(MigrationInfo *info)
  {
      if (vfio_mig_active()) {
          info->vfio = g_malloc0(sizeof(*info->vfio));
@@ -23,16 +23,16 @@ void populate_vfio_info(MigrationInfo *info)
      }
  }
 
-void reset_vfio_bytes_transferred(void)
+void migration_reset_vfio_bytes_transferred(void)
  {
      vfio_reset_bytes_transferred();
  }
  #else
-void populate_vfio_info(MigrationInfo *info)
+void migration_populate_vfio_info(MigrationInfo *info)
  {
  }
 
-void reset_vfio_bytes_transferred(void)
+void migration_reset_vfio_bytes_transferred(void)
  {
  }
  #endif
#3 lore raw

Re: [PATCH 1/6] migration: Add migration prefix to functions in target.c

In reply to Avihai Horon
Quoted 4 lines · Avihai Horon

The functions in target.c are not static, yet they don't have a proper migration prefix. Add such prefix.

No issue on the patch itself, but just noticed that we have hard-coded vfio calls in migration paths.. that's slightly unfortunate. :(

Quoted 27 lines
---
 migration/migration.h | 4 ++--
 migration/migration.c | 6 +++---
 migration/savevm.c    | 2 +-
 migration/target.c    | 8 ++++----
 4 files changed, 10 insertions(+), 10 deletions(-)
migration/migration.h +2 -2
index 6eea18db36..c5695de214 100644
@@ -512,8 +512,8 @@ void migration_consume_urgent_request(void);
 bool migration_rate_limit(void);
 void migration_cancel(const Error *error);
 
-void populate_vfio_info(MigrationInfo *info);
-void reset_vfio_bytes_transferred(void);
+void migration_populate_vfio_info(MigrationInfo *info);
+void migration_reset_vfio_bytes_transferred(void);
 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
 
 #endif
migration/migration.c +0 -0
index 5528acb65e..92866a8f49 100644

migration/migration.c static void fill_source_migration_info(MigrationInfo *info) · line 1042

Quoted 5 lines
migration/migration.c +1 -1
@@ -1039,7 +1039,7 @@ static void fill_source_migration_info(MigrationInfo *info)
         populate_time_info(info, s);
         populate_ram_info(info, s);
         populate_disk_info(info);
-        populate_vfio_info(info);
+        migration_populate_vfio_info(info);

(maybe in the future we should have SaveVMHandlers hooks for populating
data for ram/disk/vfio/..., or some other way to not hard-code these)

Quoted 13 lines
migration/migration.c +1 -1
         break;
     case MIGRATION_STATUS_COLO:
         info->has_status = true;
@@ -1048,7 +1048,7 @@ static void fill_source_migration_info(MigrationInfo *info)
     case MIGRATION_STATUS_COMPLETED:
         populate_time_info(info, s);
         populate_ram_info(info, s);
-        populate_vfio_info(info);
+        migration_populate_vfio_info(info);
         break;
     case MIGRATION_STATUS_FAILED:
         info->has_status = true;

migration/migration.c static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc, · line 1644

Quoted 5 lines
migration/migration.c +1 -1
@@ -1641,7 +1641,7 @@ static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
      */
     memset(&mig_stats, 0, sizeof(mig_stats));
     memset(&compression_counters, 0, sizeof(compression_counters));
-    reset_vfio_bytes_transferred();
+    migration_reset_vfio_bytes_transferred();

Could this already be done during vfio_save_setup(), to avoid calling an vfio function directly in migration.c?

Again, not a request for this patchset, but more to see whether it'll work to be moved there.

Thanks,

Quoted 3 lines
migration/migration.c +0 -0
     return true;
 }
Signature · 1 line
Peter Xu
#4 lore raw

Re: [PATCH 1/6] migration: Add migration prefix to functions in target.c

Quoted 32 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 4 lines · Avihai Horon

The functions in target.c are not static, yet they don't have a proper migration prefix. Add such prefix.

No issue on the patch itself, but just noticed that we have hard-coded vfio calls in migration paths.. that's slightly unfortunate. :(

Quoted 21 lines
---
  migration/migration.h | 4 ++--
  migration/migration.c | 6 +++---
  migration/savevm.c    | 2 +-
  migration/target.c    | 8 ++++----
  4 files changed, 10 insertions(+), 10 deletions(-)
migration/migration.h +2 -2
index 6eea18db36..c5695de214 100644
@@ -512,8 +512,8 @@ void migration_consume_urgent_request(void);
  bool migration_rate_limit(void);
  void migration_cancel(const Error *error);
 
-void populate_vfio_info(MigrationInfo *info);
-void reset_vfio_bytes_transferred(void);
+void migration_populate_vfio_info(MigrationInfo *info);
+void migration_reset_vfio_bytes_transferred(void);
  void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
 
Quoted 13 lines

migration/migration.c static void fill_source_migration_info(MigrationInfo *info) · line 1042

Quoted 11 lines
migration/migration.h +0 -0
  #endif
migration/migration.c +1 -1
index 5528acb65e..92866a8f49 100644
@@ -1039,7 +1039,7 @@ static void fill_source_migration_info(MigrationInfo *info)
          populate_time_info(info, s);
          populate_ram_info(info, s);
          populate_disk_info(info);
-        populate_vfio_info(info);
+        migration_populate_vfio_info(info);

(maybe in the future we should have SaveVMHandlers hooks for populating
data for ram/disk/vfio/..., or some other way to not hard-code these)

This sounds like a good idea.

Quoted 22 lines
Quoted 14 lines
migration/migration.c +1 -1
          break;
      case MIGRATION_STATUS_COLO:
          info->has_status = true;
@@ -1048,7 +1048,7 @@ static void fill_source_migration_info(MigrationInfo *info)
      case MIGRATION_STATUS_COMPLETED:
          populate_time_info(info, s);
          populate_ram_info(info, s);
-        populate_vfio_info(info);
+        migration_populate_vfio_info(info);
          break;
      case MIGRATION_STATUS_FAILED:
          info->has_status = true;

migration/migration.c static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc, · line 1644

Quoted 5 lines
migration/migration.c +1 -1
@@ -1641,7 +1641,7 @@ static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc,
       */
      memset(&mig_stats, 0, sizeof(mig_stats));
      memset(&compression_counters, 0, sizeof(compression_counters));
-    reset_vfio_bytes_transferred();
+    migration_reset_vfio_bytes_transferred();

Could this already be done during vfio_save_setup(), to avoid calling an vfio function directly in migration.c?

Quoted 2 lines

Again, not a request for this patchset, but more to see whether it'll work to be moved there.

VFIO bytes_transferred is a global variable for all VFIO devices.
Resetting it in vfio_save_setup() means that if there are multiple VFIO devices, it will be reset multiple times and that doesn't seem clean IMHO.

But maybe it would be a good idea to extend the VFIO migration info: make it per device and maybe split it to pre-copy data, stop-copy data, etc. Then we can reset it in vfio_save_setup().

Thanks.

#5 lore raw

PATCH 2/6 vfio/migration: Fail adding device with enable-migration=on and existing blocker

In reply to Avihai Horon

If a device with enable-migration=on is added and it causes a migration blocker, adding the device should fail with a proper error.

This is not the case with multiple device migration blocker when the blocker already exists. If the blocker already exists and a device with enable-migration=on is added which causes a migration blocker, adding the device will succeed.

Fix it by failing adding the device in such case.

Fixes 8bbcb64a71d8 ("vfio/migration: Make VFIO migration non-experimental")
---
 hw/vfio/common.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/vfio/common.c +5 -2
index 8a8d074e18..237101d038 100644
@@ -394,8 +394,7 @@ int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp)
 {
     int ret;
 
-    if (multiple_devices_migration_blocker ||
-        vfio_multiple_devices_migration_is_supported()) {
+    if (vfio_multiple_devices_migration_is_supported()) {
         return 0;
     }
 
@@ -405,6 +404,10 @@ int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp)
         return -EINVAL;
     }
 
+    if (multiple_devices_migration_blocker) {
+        return 0;
+    }
+
     error_setg(&multiple_devices_migration_blocker,
                "Multiple VFIO devices migration is supported only if all of "
                "them support P2P migration");
Signature · 1 line
2.26.3
Reviewed-by on v1 2/6
#6 lore raw

Re: [PATCH 2/6] vfio/migration: Fail adding device with enable-migration=on and existing blocker

Quoted 9 lines · Avihai Horon

If a device with enable-migration=on is added and it causes a migration blocker, adding the device should fail with a proper error.

This is not the case with multiple device migration blocker when the blocker already exists. If the blocker already exists and a device with enable-migration=on is added which causes a migration blocker, adding the device will succeed.

Fix it by failing adding the device in such case.

Thanks,

C.

Quoted 32 lines
Fixes 8bbcb64a71d8 ("vfio/migration: Make VFIO migration non-experimental")
---
  hw/vfio/common.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
hw/vfio/common.c +5 -2
index 8a8d074e18..237101d038 100644
@@ -394,8 +394,7 @@ int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp)
  {
      int ret;
 
-    if (multiple_devices_migration_blocker ||
-        vfio_multiple_devices_migration_is_supported()) {
+    if (vfio_multiple_devices_migration_is_supported()) {
          return 0;
      }
 
@@ -405,6 +404,10 @@ int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp)
          return -EINVAL;
      }
 
+    if (multiple_devices_migration_blocker) {
+        return 0;
+    }
+
      error_setg(&multiple_devices_migration_blocker,
                 "Multiple VFIO devices migration is supported only if all of "
                 "them support P2P migration");
#7 lore raw

PATCH 3/6 vfio/migration: Add vfio_migratable_devices_num()

In reply to Avihai Horon

Add vfio_migratable_devices_num() function, which returns the number of VFIO devices that are using VFIO migration, and use it in vfio_multiple_devices_migration_is_supported().

This is done in preparation for next patches which will block VFIO migration with postcopy migration or background snapshot, as they are not compatible together.

---
 hw/vfio/common.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
hw/vfio/common.c +18 -4
index 237101d038..57a76feab1 100644
@@ -363,6 +363,23 @@ bool vfio_mig_active(void)
 
 static Error *multiple_devices_migration_blocker;
 
+static unsigned int vfio_migratable_devices_num(void)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    unsigned int device_num = 0;
+
+    QLIST_FOREACH(group, &vfio_group_list, next) {
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            if (vbasedev->migration) {
+                device_num++;
+            }
+        }
+    }
+
+    return device_num;
+}
+
 /*
  * Multiple devices migration is allowed only if all devices support P2P
  * migration. Single device migration is allowed regardless of P2P migration
@@ -372,14 +389,11 @@ static bool vfio_multiple_devices_migration_is_supported(void)
 {
     VFIOGroup *group;
     VFIODevice *vbasedev;
-    unsigned int device_num = 0;
     bool all_support_p2p = true;
 
     QLIST_FOREACH(group, &vfio_group_list, next) {
         QLIST_FOREACH(vbasedev, &group->device_list, next) {
             if (vbasedev->migration) {
-                device_num++;
-
                 if (!(vbasedev->migration->mig_flags & VFIO_MIGRATION_P2P)) {
                     all_support_p2p = false;
                 }
@@ -387,7 +401,7 @@ static bool vfio_multiple_devices_migration_is_supported(void)
         }
     }
 
-    return all_support_p2p || device_num <= 1;
+    return all_support_p2p || vfio_migratable_devices_num() <= 1;
 }
 
 int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp)
Signature · 1 line
2.26.3
Reviewed-by on v1 3/6
#8 lore raw

Re: [PATCH 3/6] vfio/migration: Add vfio_migratable_devices_num()

Quoted 9 lines · Avihai Horon

Add vfio_migratable_devices_num() function, which returns the number of VFIO devices that are using VFIO migration, and use it in vfio_multiple_devices_migration_is_supported().

This is done in preparation for next patches which will block VFIO migration with postcopy migration or background snapshot, as they are not compatible together.

Thanks,

C.

Quoted 56 lines
---
  hw/vfio/common.c | 22 ++++++++++++++++++----
  1 file changed, 18 insertions(+), 4 deletions(-)
hw/vfio/common.c +18 -4
index 237101d038..57a76feab1 100644
@@ -363,6 +363,23 @@ bool vfio_mig_active(void)
 
  static Error *multiple_devices_migration_blocker;
 
+static unsigned int vfio_migratable_devices_num(void)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    unsigned int device_num = 0;
+
+    QLIST_FOREACH(group, &vfio_group_list, next) {
+        QLIST_FOREACH(vbasedev, &group->device_list, next) {
+            if (vbasedev->migration) {
+                device_num++;
+            }
+        }
+    }
+
+    return device_num;
+}
+
  /*
   * Multiple devices migration is allowed only if all devices support P2P
   * migration. Single device migration is allowed regardless of P2P migration
@@ -372,14 +389,11 @@ static bool vfio_multiple_devices_migration_is_supported(void)
  {
      VFIOGroup *group;
      VFIODevice *vbasedev;
-    unsigned int device_num = 0;
      bool all_support_p2p = true;
 
      QLIST_FOREACH(group, &vfio_group_list, next) {
          QLIST_FOREACH(vbasedev, &group->device_list, next) {
              if (vbasedev->migration) {
-                device_num++;
-
                  if (!(vbasedev->migration->mig_flags & VFIO_MIGRATION_P2P)) {
                      all_support_p2p = false;
                  }
@@ -387,7 +401,7 @@ static bool vfio_multiple_devices_migration_is_supported(void)
          }
      }
 
-    return all_support_p2p || device_num <= 1;
+    return all_support_p2p || vfio_migratable_devices_num() <= 1;
  }
 
  int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp)
#9 lore raw

PATCH 4/6 vfio/migration: Change vfio_mig_active() semantics

In reply to Avihai Horon

vfio_mig_active() is used by migration_populate_vfio_info() to populate VFIO migration info when it is active. Currently, VFIO migration is considered active if there are VFIO devices and none of them has a migration blocker.

Change that and consider VFIO migration to be active if there is a VFIO device that is using VFIO migration, regardless of whether a device has migration blocker or not.

This is done in preparation for next patches which will block VFIO migration with postcopy migration or background snapshot, as they are not compatible together. It will allow adding a migration blocker for such cases even if the VFIO device already has a blocker.

Note that migration_populate_vfio_info() still behaves correctly, as if there is a VFIO device with migration blocker, migration can't be started and thus migration_populate_vfio_info() will never be called.

---
 hw/vfio/common.c | 24 +++++-------------------
 1 file changed, 5 insertions(+), 19 deletions(-)
hw/vfio/common.c +5 -19
index 57a76feab1..373f6e5932 100644
@@ -342,25 +342,6 @@ static int vfio_bitmap_alloc(VFIOBitmap *vbmap, hwaddr size)
 static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
                                  uint64_t size, ram_addr_t ram_addr);
 
-bool vfio_mig_active(void)
-{
-    VFIOGroup *group;
-    VFIODevice *vbasedev;
-
-    if (QLIST_EMPTY(&vfio_group_list)) {
-        return false;
-    }
-
-    QLIST_FOREACH(group, &vfio_group_list, next) {
-        QLIST_FOREACH(vbasedev, &group->device_list, next) {
-            if (vbasedev->migration_blocker) {
-                return false;
-            }
-        }
-    }
-    return true;
-}
-
 static Error *multiple_devices_migration_blocker;
 
 static unsigned int vfio_migratable_devices_num(void)
@@ -446,6 +427,11 @@ void vfio_unblock_multiple_devices_migration(void)
     multiple_devices_migration_blocker = NULL;
 }
 
+bool vfio_mig_active(void)
+{
+    return vfio_migratable_devices_num();
+}
+
 bool vfio_viommu_preset(VFIODevice *vbasedev)
 {
     return vbasedev->group->container->space->as != &address_space_memory;
Signature · 1 line
2.26.3
#10 lore raw

PATCH 5/6 vfio/migration: Block VFIO migration with postcopy migration

In reply to Avihai Horon

VFIO migration is not compatible with postcopy migration. A VFIO device in the destination can't handle page faults for pages that have not been sent yet.

Doing such migration will cause the VM to crash in the destination:

qemu-system-x86_64: VFIO_MAP_DMA failed: Bad address
qemu-system-x86_64: vfio_dma_map(0x55a28c7659d0, 0xc0000, 0xb000, 0x7f1b11a00000) = -14 (Bad address)
qemu: hardware error: vfio: DMA mapping failed, unable to continue

To prevent this and to be explicit about supported features, block VFIO migration with postcopy migration: Fail setting postcopy capability if a VFIO device is present, and add a migration blocker if a VFIO device is added when postcopy capability is on.

---
 include/hw/vfio/vfio-common.h |  2 ++
 migration/migration.h         |  2 ++
 hw/vfio/common.c              | 43 +++++++++++++++++++++++++++++++++++
 hw/vfio/migration.c           |  6 +++++
 migration/options.c           | 19 ++++++++++++++++
 migration/target.c            | 19 ++++++++++++++++
 6 files changed, 91 insertions(+)
include/hw/vfio/vfio-common.h +2 -0
index e9b8954595..c0b58f2bb7 100644
@@ -227,6 +227,8 @@ 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_postcopy_migration(VFIODevice *vbasedev, Error **errp);
+void vfio_unblock_postcopy_migration(void);
 bool vfio_viommu_preset(VFIODevice *vbasedev);
 int64_t vfio_mig_bytes_transferred(void);
 void vfio_reset_bytes_transferred(void);
migration/migration.h +2 -0
index c5695de214..21a6423408 100644
@@ -514,6 +514,8 @@ void migration_cancel(const Error *error);
 
 void migration_populate_vfio_info(MigrationInfo *info);
 void migration_reset_vfio_bytes_transferred(void);
+bool migration_vfio_mig_active(void);
+void migration_vfio_unblock_postcopy_migration(void);
 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
 
 #endif
hw/vfio/common.c +43 -0

61 lines not on this page · raw message

hw/vfio/migration.c +6 -0
index 71855468fe..76406e9ae9 100644
@@ -856,6 +856,7 @@ static void vfio_migration_deinit(VFIODevice *vbasedev)
     unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
     vfio_migration_free(vbasedev);
     vfio_unblock_multiple_devices_migration();
+    vfio_unblock_postcopy_migration();
 }
 
 static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
@@ -939,6 +940,11 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
         goto out_deinit;
     }
 
+    ret = vfio_block_postcopy_migration(vbasedev, errp);
+    if (ret) {
+        goto out_deinit;
+    }
+
     if (vfio_viommu_preset(vbasedev)) {
         error_setg(&err, "%s: Migration is currently not supported "
                    "with vIOMMU enabled", vbasedev->name);
migration/options.c +19 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
             error_setg(errp, "Postcopy is not yet compatible with multifd");
             return false;
         }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }
     }
 
     if (new_caps[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
@@ -612,6 +617,16 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
     return true;
 }
 
+/*
+ * Devices might have added migration blockers based on migration capabilities
+ * values when those devices were added. Remove such blockers according to new
+ * changes in migration capabilities.
+ */
+static void migration_caps_remove_blockers(void)
+{
+    migration_vfio_unblock_postcopy_migration();
+}
+
 bool migrate_cap_set(int cap, bool value, Error **errp)
 {
     MigrationState *s = migrate_get_current();
@@ -629,6 +644,8 @@ bool migrate_cap_set(int cap, bool value, Error **errp)
         return false;
     }
     s->capabilities[cap] = value;
+    migration_caps_remove_blockers();
+
     return true;
 }
 
@@ -678,6 +695,8 @@ void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
     for (cap = params; cap; cap = cap->next) {
         s->capabilities[cap->value->capability] = cap->value->state;
     }
+
+    migration_caps_remove_blockers();
 }
 
 /* parameters */
migration/target.c +19 -0
index a6ffa9a5ce..690ecb4dd5 100644
@@ -27,6 +27,16 @@ void migration_reset_vfio_bytes_transferred(void)
 {
     vfio_reset_bytes_transferred();
 }
+
+bool migration_vfio_mig_active(void)
+{
+    return vfio_mig_active();
+}
+
+void migration_vfio_unblock_postcopy_migration(void)
+{
+    vfio_unblock_postcopy_migration();
+}
 #else
 void migration_populate_vfio_info(MigrationInfo *info)
 {
@@ -35,4 +45,13 @@ void migration_populate_vfio_info(MigrationInfo *info)
 void migration_reset_vfio_bytes_transferred(void)
 {
 }
+
+bool migration_vfio_mig_active(void)
+{
+    return false;
+}
+
+void migration_vfio_unblock_postcopy_migration()
+{
+}
 #endif
Signature · 1 line
2.26.3
#11 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 217 lines · Avihai Horon

VFIO migration is not compatible with postcopy migration. A VFIO device in the destination can't handle page faults for pages that have not been sent yet.

Doing such migration will cause the VM to crash in the destination:

qemu-system-x86_64: VFIO_MAP_DMA failed: Bad address
qemu-system-x86_64: vfio_dma_map(0x55a28c7659d0, 0xc0000, 0xb000, 0x7f1b11a00000) = -14 (Bad address)
qemu: hardware error: vfio: DMA mapping failed, unable to continue

To prevent this and to be explicit about supported features, block VFIO migration with postcopy migration: Fail setting postcopy capability if a VFIO device is present, and add a migration blocker if a VFIO device is added when postcopy capability is on.

---
  include/hw/vfio/vfio-common.h |  2 ++
  migration/migration.h         |  2 ++
  hw/vfio/common.c              | 43 +++++++++++++++++++++++++++++++++++
  hw/vfio/migration.c           |  6 +++++
  migration/options.c           | 19 ++++++++++++++++
  migration/target.c            | 19 ++++++++++++++++
  6 files changed, 91 insertions(+)
include/hw/vfio/vfio-common.h +2 -0
index e9b8954595..c0b58f2bb7 100644
@@ -227,6 +227,8 @@ 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_postcopy_migration(VFIODevice *vbasedev, Error **errp);
+void vfio_unblock_postcopy_migration(void);
  bool vfio_viommu_preset(VFIODevice *vbasedev);
  int64_t vfio_mig_bytes_transferred(void);
  void vfio_reset_bytes_transferred(void);
migration/migration.h +2 -0
index c5695de214..21a6423408 100644
@@ -514,6 +514,8 @@ void migration_cancel(const Error *error);
 
  void migration_populate_vfio_info(MigrationInfo *info);
  void migration_reset_vfio_bytes_transferred(void);
+bool migration_vfio_mig_active(void);
+void migration_vfio_unblock_postcopy_migration(void);
  void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
 
  #endif
hw/vfio/common.c +43 -0
index 373f6e5932..7461194b2b 100644
@@ -40,6 +40,7 @@
  #include "trace.h"
  #include "qapi/error.h"
  #include "migration/migration.h"
+#include "migration/options.h"
  #include "migration/misc.h"
  #include "migration/blocker.h"
  #include "migration/qemu-file.h"
@@ -343,6 +344,7 @@ static int vfio_get_dirty_bitmap(VFIOContainer *container, uint64_t iova,
                                   uint64_t size, ram_addr_t ram_addr);
 
  static Error *multiple_devices_migration_blocker;
+static Error *postcopy_migration_blocker;
 
  static unsigned int vfio_migratable_devices_num(void)
  {
@@ -427,6 +429,47 @@ void vfio_unblock_multiple_devices_migration(void)
      multiple_devices_migration_blocker = NULL;
  }
 
+int vfio_block_postcopy_migration(VFIODevice *vbasedev, Error **errp)
+{
+    int ret;
+
+    if (!migrate_postcopy_ram()) {
+        return 0;
+    }
+
+    if (vbasedev->enable_migration == ON_OFF_AUTO_ON) {
+        error_setg(errp,
+                   "VFIO migration is not compatible with postcopy migration");
+        return -EINVAL;
+    }
+
+    if (postcopy_migration_blocker) {
+        return 0;
+    }
+
+    error_setg(&postcopy_migration_blocker,
+               "VFIO migration is not compatible with postcopy migration");
+    ret = migrate_add_blocker(postcopy_migration_blocker, errp);
+    if (ret < 0) {
+        error_free(postcopy_migration_blocker);
+        postcopy_migration_blocker = NULL;
+    }
+
+    return ret;
+}
+
+void vfio_unblock_postcopy_migration(void)
+{
+    if (!postcopy_migration_blocker ||
+        (vfio_migratable_devices_num() && migrate_postcopy_ram())) {
+        return;
+    }
+
+    migrate_del_blocker(postcopy_migration_blocker);
+    error_free(postcopy_migration_blocker);
+    postcopy_migration_blocker = NULL;
+}
+
  bool vfio_mig_active(void)
  {
      return vfio_migratable_devices_num();
hw/vfio/migration.c +6 -0
index 71855468fe..76406e9ae9 100644
@@ -856,6 +856,7 @@ static void vfio_migration_deinit(VFIODevice *vbasedev)
      unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
      vfio_migration_free(vbasedev);
      vfio_unblock_multiple_devices_migration();
+    vfio_unblock_postcopy_migration();
  }
 
  static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
@@ -939,6 +940,11 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
          goto out_deinit;
      }
 
+    ret = vfio_block_postcopy_migration(vbasedev, errp);
+    if (ret) {
+        goto out_deinit;
+    }
+
      if (vfio_viommu_preset(vbasedev)) {
          error_setg(&err, "%s: Migration is currently not supported "
                     "with vIOMMU enabled", vbasedev->name);
migration/options.c +19 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
              error_setg(errp, "Postcopy is not yet compatible with multifd");
              return false;
          }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }
      }
 
      if (new_caps[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
@@ -612,6 +617,16 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
      return true;
  }
 
+/*
+ * Devices might have added migration blockers based on migration capabilities
+ * values when those devices were added. Remove such blockers according to new
+ * changes in migration capabilities.
+ */
+static void migration_caps_remove_blockers(void)
+{
+    migration_vfio_unblock_postcopy_migration();
+}
+
  bool migrate_cap_set(int cap, bool value, Error **errp)
  {
      MigrationState *s = migrate_get_current();
@@ -629,6 +644,8 @@ bool migrate_cap_set(int cap, bool value, Error **errp)
          return false;
      }
      s->capabilities[cap] = value;
+    migration_caps_remove_blockers();
+
      return true;
  }
 
@@ -678,6 +695,8 @@ void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
      for (cap = params; cap; cap = cap->next) {
          s->capabilities[cap->value->capability] = cap->value->state;
      }
+
+    migration_caps_remove_blockers();
  }
 
  /* parameters */
migration/target.c +10 -0
index a6ffa9a5ce..690ecb4dd5 100644
@@ -27,6 +27,16 @@ void migration_reset_vfio_bytes_transferred(void)
  {
      vfio_reset_bytes_transferred();
  }
+
+bool migration_vfio_mig_active(void)
+{
+    return vfio_mig_active();
+}
+
+void migration_vfio_unblock_postcopy_migration(void)
+{
+    vfio_unblock_postcopy_migration();
+}
  #else
  void migration_populate_vfio_info(MigrationInfo *info)
  {

migration/target.c void migration_populate_vfio_info(MigrationInfo *info) · line 54

Quoted 10 lines
migration/target.c +7 -0
@@ -35,4 +45,13 @@ void migration_populate_vfio_info(MigrationInfo *info)
  void migration_reset_vfio_bytes_transferred(void)
  {
  }
+
+bool migration_vfio_mig_active(void)
+{
+    return false;
+}
+
+void migration_vfio_unblock_postcopy_migration()

Missing 'void' above.

Thanks,

C.

Quoted 3 lines
migration/target.c +2 -0
+{
+}
  #endif
#12 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 239 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 235 lines · Avihai Horon

VFIO migration is not compatible with postcopy migration. A VFIO device in the destination can't handle page faults for pages that have not been sent yet.

Doing such migration will cause the VM to crash in the destination:

qemu-system-x86_64: VFIO_MAP_DMA failed: Bad address
qemu-system-x86_64: vfio_dma_map(0x55a28c7659d0, 0xc0000, 0xb000, 0x7f1b11a00000) = -14 (Bad address)
qemu: hardware error: vfio: DMA mapping failed, unable to continue

To prevent this and to be explicit about supported features, block VFIO migration with postcopy migration: Fail setting postcopy capability if a VFIO device is present, and add a migration blocker if a VFIO device is added when postcopy capability is on.

---
  include/hw/vfio/vfio-common.h |  2 ++ migration/migration.h         |  2 ++
  hw/vfio/common.c              | 43 +++++++++++++++++++++++++++++++++++ hw/vfio/migration.c           |  6 +++++ migration/options.c           | 19 ++++++++++++++++ migration/target.c            | 19 ++++++++++++++++

  6 files changed, 91 insertions(+)
include/hw/vfio/vfio-common.h +2 -0
index e9b8954595..c0b58f2bb7 100644
@@ -227,6 +227,8 @@ 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_postcopy_migration(VFIODevice *vbasedev, Error **errp);
+void vfio_unblock_postcopy_migration(void);
   bool vfio_viommu_preset(VFIODevice *vbasedev);
   int64_t vfio_mig_bytes_transferred(void);
   void vfio_reset_bytes_transferred(void);
migration/migration.h +2 -0
index c5695de214..21a6423408 100644
@@ -514,6 +514,8 @@ void migration_cancel(const Error *error);
 
   void migration_populate_vfio_info(MigrationInfo *info);
   void migration_reset_vfio_bytes_transferred(void);
+bool migration_vfio_mig_active(void);
+void migration_vfio_unblock_postcopy_migration(void);
   void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
 
   #endif
hw/vfio/common.c +2 -0
index 373f6e5932..7461194b2b 100644
@@ -40,6 +40,7 @@
   #include "trace.h"
   #include "qapi/error.h"
   #include "migration/migration.h"
+#include "migration/options.h"
   #include "migration/misc.h"
   #include "migration/blocker.h"
   #include "migration/qemu-file.h"
@@ -343,6 +344,7 @@ static int vfio_get_dirty_bitmap(VFIOContainer 
 *container, uint64_t iova,
                                    uint64_t size, ram_addr_t ram_addr);
 
   static Error *multiple_devices_migration_blocker;
+static Error *postcopy_migration_blocker;
 
   static unsigned int vfio_migratable_devices_num(void)

  {

hw/vfio/common.c +41 -0
@@ -427,6 +429,47 @@ void vfio_unblock_multiple_devices_migration(void)
       multiple_devices_migration_blocker = NULL;
   }
 
+int vfio_block_postcopy_migration(VFIODevice *vbasedev, Error **errp)
+{
+    int ret;
+
+    if (!migrate_postcopy_ram()) {
+        return 0;
+    }
+
+    if (vbasedev->enable_migration == ON_OFF_AUTO_ON) {
+        error_setg(errp,
+                   "VFIO migration is not compatible with postcopy 
 migration");
+        return -EINVAL;
+    }
+
+    if (postcopy_migration_blocker) {
+        return 0;
+    }
+
+    error_setg(&postcopy_migration_blocker,
+               "VFIO migration is not compatible with postcopy 
 migration");
+    ret = migrate_add_blocker(postcopy_migration_blocker, errp);
+    if (ret < 0) {
+        error_free(postcopy_migration_blocker);
+        postcopy_migration_blocker = NULL;
+    }
+
+    return ret;
+}
+
+void vfio_unblock_postcopy_migration(void)
+{
+    if (!postcopy_migration_blocker ||
+        (vfio_migratable_devices_num() && migrate_postcopy_ram())) {
+        return;
+    }
+
+    migrate_del_blocker(postcopy_migration_blocker);
+    error_free(postcopy_migration_blocker);
+    postcopy_migration_blocker = NULL;
+}
+
   bool vfio_mig_active(void)

  {
      return vfio_migratable_devices_num();

hw/vfio/migration.c +1 -0
index 71855468fe..76406e9ae9 100644
@@ -856,6 +856,7 @@ static void vfio_migration_deinit(VFIODevice 
 *vbasedev)
       unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
       vfio_migration_free(vbasedev);
       vfio_unblock_multiple_devices_migration();
+    vfio_unblock_postcopy_migration();
   }
 

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

hw/vfio/migration.c +5 -0
@@ -939,6 +940,11 @@ bool vfio_migration_realize(VFIODevice 
 *vbasedev, Error **errp)
           goto out_deinit;
       }
 
+    ret = vfio_block_postcopy_migration(vbasedev, errp);
+    if (ret) {
+        goto out_deinit;
+    }
+
       if (vfio_viommu_preset(vbasedev)) {

          error_setg(&err, "%s: Migration is currently not supported " "with vIOMMU enabled", vbasedev->name);

migration/options.c +5 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool 
 *new_caps, Error **errp)
               error_setg(errp, "Postcopy is not yet compatible with multifd");
               return false;
           }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO 
 migration");
+            return false;
+        }
       }

      if (new_caps[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {

migration/options.c +2 -0
@@ -612,6 +617,16 @@ bool migrate_caps_check(bool *old_caps, bool 
 *new_caps, Error **errp)
       return true;
   }
 
+/*
+ * Devices might have added migration blockers based on migration 

capabilities
+ * values when those devices were added. Remove such blockers according to new
+ * changes in migration capabilities.
+ */

migration/options.c +5 -0
+static void migration_caps_remove_blockers(void)
+{
+    migration_vfio_unblock_postcopy_migration();
+}
+

  bool migrate_cap_set(int cap, bool value, Error **errp) {
      MigrationState *s = migrate_get_current();

migration/options.c +4 -0
@@ -629,6 +644,8 @@ bool migrate_cap_set(int cap, bool value, Error 
 **errp)
           return false;
       }
       s->capabilities[cap] = value;
+    migration_caps_remove_blockers();
+
       return true;
   }
@@ -678,6 +695,8 @@ void 
 qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params,
       for (cap = params; cap; cap = cap->next) {
           s->capabilities[cap->value->capability] = cap->value->state;
       }
+
+    migration_caps_remove_blockers();
   }
 

  /* parameters */

migration/target.c +12 -0
index a6ffa9a5ce..690ecb4dd5 100644
@@ -27,6 +27,16 @@ void migration_reset_vfio_bytes_transferred(void)
   {
       vfio_reset_bytes_transferred();
   }
+
+bool migration_vfio_mig_active(void)
+{
+    return vfio_mig_active();
+}
+
+void migration_vfio_unblock_postcopy_migration(void)
+{
+    vfio_unblock_postcopy_migration();
+}
   #else
   void migration_populate_vfio_info(MigrationInfo *info)
   {
@@ -35,4 +45,13 @@ void migration_populate_vfio_info(MigrationInfo 
 *info)
   void migration_reset_vfio_bytes_transferred(void)
   {
   }
+
+bool migration_vfio_mig_active(void)
Quoted 9 lines

migration/target.c void migration_populate_vfio_info(MigrationInfo *info) · line 54

Quoted 5 lines
migration/target.c +5 -0
+{
+    return false;
+}
+
+void migration_vfio_unblock_postcopy_migration()

Missing 'void' above.

Oh, right. Next time I will also test compilation without VFIO config.

Thanks.

#13 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

In reply to Avihai Horon

migration/options.c bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp) · line 506

Quoted 13 lines · Avihai Horon
migration/options.c +5 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
             error_setg(errp, "Postcopy is not yet compatible with multifd");
             return false;
         }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

Is it possible to do it in a generic way?

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early? For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Thanks,

Quoted 6 lines
migration/options.c +0 -0
     }
 
     if (new_caps[MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT]) {
@@ -612,6 +617,16 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
     return true;
 }
Signature · 1 line
Peter Xu
#14 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 19 lines · Peter Xu

External email: Use caution opening links or attachments

migration/options.c bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp) · line 506

Quoted 13 lines · Avihai Horon
migration/options.c +5 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
              error_setg(errp, "Postcopy is not yet compatible with multifd");
              return false;
          }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

Quoted 2 lines

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

In that case a migration blocker will be added.

Quoted 2 lines

Is it possible to do it in a generic way?

What comes to my mind is to let devices register a handler for a "caps change" notification and allow them to object. But maybe that's a bit of an overkill.

Quoted 9 lines

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early? For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Yes, I think this could work and it will simplify things because we could also drop the VFIO migration blockers code.
The downside is that the user will know migration is blocked only when he tries to migrate, and migrate_caps_check() will not block setting postcopy when a VFIO device is already attached. I don't have a strong opinion here, so if it's fine by you and everyone else, I could change that to what you suggested.

Thanks.

#15 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 24 lines · Avihai Horon
Quoted 19 lines · Peter Xu

External email: Use caution opening links or attachments

migration/options.c bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp) · line 506

Quoted 13 lines · Avihai Horon
migration/options.c +5 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
              error_setg(errp, "Postcopy is not yet compatible with multifd");
              return false;
          }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

Quoted 2 lines

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

Quoted 8 lines

In that case a migration blocker will be added.

Quoted 2 lines

Is it possible to do it in a generic way?

What comes to my mind is to let devices register a handler for a "caps change" notification and allow them to object.
But maybe that's a bit of an overkill.

This one also sounds better than hard-codes to me.

Quoted 11 lines
Quoted 10 lines

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early? For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Quoted 7 lines

Yes, I think this could work and it will simplify things because we could also drop the VFIO migration blockers code.
The downside is that the user will know migration is blocked only when he tries to migrate, and migrate_caps_check() will not block setting postcopy when a VFIO device is already attached.
I don't have a strong opinion here, so if it's fine by you and everyone else, I could change that to what you suggested.

Failing later would be fine in this case to me; my expectation is VFIO users should be advanced already anyway (as the whole solution is still pretty involved comparing to a generic VM migration) and shouldn't try to trigger that at all in real life. IOW I'd expect this check will be there just for sanity, rather than being relied on to let people be aware of it by the error message.

Meanwhile the blocker + caps check is slightly complicated to me to guard both sides. So I'd vote for failing at the QMP command. But we can wait and see whether there's other votes.

Thanks,

Signature · 1 line
Peter Xu
#16 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 49 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 20 lines · Avihai Horon
Quoted 19 lines · Peter Xu

External email: Use caution opening links or attachments

migration/options.c bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp) · line 506

Quoted 13 lines · Avihai Horon
migration/options.c +5 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
               error_setg(errp, "Postcopy is not yet compatible with multifd");
               return false;
           }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

Quoted 8 lines
Quoted 2 lines

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

In that case a migration blocker will be added.

Quoted 1 line

Is it possible to do it in a generic way?

What comes to my mind is to let devices register a handler for a "caps change" notification and allow them to object.
But maybe that's a bit of an overkill.

This one also sounds better than hard-codes to me.

Quoted 8 lines
Quoted 8 lines

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early? For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Quoted 7 lines

Yes, I think this could work and it will simplify things because we could also drop the VFIO migration blockers code.
The downside is that the user will know migration is blocked only when he tries to migrate, and migrate_caps_check() will not block setting postcopy when a VFIO device is already attached.
I don't have a strong opinion here, so if it's fine by you and everyone else, I could change that to what you suggested.

Quoted 6 lines

Failing later would be fine in this case to me; my expectation is VFIO users should be advanced already anyway (as the whole solution is still pretty involved comparing to a generic VM migration) and shouldn't try to trigger that at all in real life. IOW I'd expect this check will be there just for sanity, rather than being relied on to let people be aware of it by the error message.

Yes, I agree with you.

Quoted 4 lines

Meanwhile the blocker + caps check is slightly complicated to me to guard both sides. So I'd vote for failing at the QMP command. But we can wait and see whether there's other votes.

Sure.
So I will do the checking in vfio_save_setup(), unless someone else has a better idea.

Thanks.

#17 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 61 lines · Avihai Horon
Quoted 49 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 20 lines · Avihai Horon
Quoted 19 lines · Peter Xu

External email: Use caution opening links or attachments

migration/options.c bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp) · line 506

Quoted 13 lines · Avihai Horon
migration/options.c +5 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
                error_setg(errp, "Postcopy is not yet compatible with multifd");
                return false;
            }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

Quoted 8 lines
Quoted 2 lines

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

In that case a migration blocker will be added.

Quoted 1 line

Is it possible to do it in a generic way?

What comes to my mind is to let devices register a handler for a "caps change" notification and allow them to object.
But maybe that's a bit of an overkill.

This one also sounds better than hard-codes to me.

Quoted 8 lines
Quoted 8 lines

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early?  For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Quoted 7 lines

Yes, I think this could work and it will simplify things because we could also drop the VFIO migration blockers code.
The downside is that the user will know migration is blocked only when he tries to migrate, and migrate_caps_check() will not block setting postcopy when a VFIO device is already attached.
I don't have a strong opinion here, so if it's fine by you and everyone else, I could change that to what you suggested.

Quoted 6 lines

Failing later would be fine in this case to me; my expectation is VFIO users should be advanced already anyway (as the whole solution is still pretty involved comparing to a generic VM migration) and shouldn't try to trigger that at all in real life.  IOW I'd expect this check will be there just for sanity, rather than being relied on to let people be aware of it by the error message.

Yes, I agree with you.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Meanwhile the blocker + caps check is slightly complicated to me to guard both sides.  So I'd vote for failing at the QMP command.  But we can wait and see whether there's other votes.

Sure.
So I will do the checking in vfio_save_setup(), unless someone else has a better idea.

Just to recap for my understanding,

vfio_save_setup() would test migrate_postcopy_ram() and update a new 'Error *err' parameter of the .save_setup() op which would be taken into account in qemu_savevm_state_setup(). Is that correct ?

Thanks,

C.

#18 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 71 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 67 lines · Avihai Horon
Quoted 51 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 22 lines · Avihai Horon
Quoted 21 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 5 lines · Avihai Horon
migration/options.c +0 -0
index 1d1e1321b0..e201053563 100644

migration/options.c bool migrate_caps_check(bool *old_caps, bool · line 508

Quoted 10 lines
migration/options.c +5 -0
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool 
 *new_caps, Error **errp)
                error_setg(errp, "Postcopy is not yet compatible with multifd");
                return false;
            }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with 
 VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

Quoted 8 lines
Quoted 2 lines

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

In that case a migration blocker will be added.

Quoted 1 line

Is it possible to do it in a generic way?

What comes to my mind is to let devices register a handler for a "caps change" notification and allow them to object.
But maybe that's a bit of an overkill.

This one also sounds better than hard-codes to me.

Quoted 8 lines
Quoted 8 lines

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early?  For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Quoted 7 lines

Yes, I think this could work and it will simplify things because we could also drop the VFIO migration blockers code.
The downside is that the user will know migration is blocked only when he tries to migrate, and migrate_caps_check() will not block setting postcopy when a VFIO device is already attached.
I don't have a strong opinion here, so if it's fine by you and everyone else, I could change that to what you suggested.

Quoted 6 lines

Failing later would be fine in this case to me; my expectation is VFIO users should be advanced already anyway (as the whole solution is still pretty involved comparing to a generic VM migration) and shouldn't try to trigger that at all in real life.  IOW I'd expect this check will be there just for sanity, rather than being relied on to let people be aware of it by the error message.

Yes, I agree with you.

Quoted 4 lines

Meanwhile the blocker + caps check is slightly complicated to me to guard both sides.  So I'd vote for failing at the QMP command.  But we can wait and see whether there's other votes.

Quoted 9 lines
Quoted 2 lines

Sure.
So I will do the checking in vfio_save_setup(), unless someone else has a better idea.

Just to recap for my understanding,

vfio_save_setup() would test migrate_postcopy_ram() and update a new 'Error *err' parameter of the .save_setup() op which would be taken into account in qemu_savevm_state_setup(). Is that correct ?

Yes.
But I wonder if it would be simpler to call migrate_set_error() directly from vfio_save_setup() instead of adding "Error *err" argument to .save_setup() and changing all other users. What do you prefer?

Thanks.

#19 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 76 lines · Avihai Horon
Quoted 74 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 61 lines · Avihai Horon
Quoted 49 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 20 lines · Avihai Horon
Quoted 19 lines · Peter Xu

External email: Use caution opening links or attachments

migration/options.c bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp) · line 506

Quoted 13 lines · Avihai Horon
migration/options.c +5 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
                error_setg(errp, "Postcopy is not yet compatible with multifd");
                return false;
            }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

Quoted 8 lines
Quoted 2 lines

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

In that case a migration blocker will be added.

Quoted 1 line

Is it possible to do it in a generic way?

What comes to my mind is to let devices register a handler for a "caps change" notification and allow them to object.
But maybe that's a bit of an overkill.

This one also sounds better than hard-codes to me.

Quoted 8 lines
Quoted 8 lines

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early?  For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Quoted 7 lines

Yes, I think this could work and it will simplify things because we could also drop the VFIO migration blockers code.
The downside is that the user will know migration is blocked only when he tries to migrate, and migrate_caps_check() will not block setting postcopy when a VFIO device is already attached.
I don't have a strong opinion here, so if it's fine by you and everyone else, I could change that to what you suggested.

Quoted 6 lines

Failing later would be fine in this case to me; my expectation is VFIO users should be advanced already anyway (as the whole solution is still pretty involved comparing to a generic VM migration) and shouldn't try to trigger that at all in real life.  IOW I'd expect this check will be there just for sanity, rather than being relied on to let people be aware of it by the error message.

Yes, I agree with you.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Meanwhile the blocker + caps check is slightly complicated to me to guard both sides.  So I'd vote for failing at the QMP command.  But we can wait and see whether there's other votes.

Sure.
So I will do the checking in vfio_save_setup(), unless someone else has a better idea.

Just to recap for my understanding,

Quoted 7 lines
Quoted 4 lines

vfio_save_setup() would test migrate_postcopy_ram() and update a new 'Error *err' parameter of the .save_setup() op which would be taken into account in qemu_savevm_state_setup(). Is that correct ?

Yes.
But I wonder if it would be simpler to call migrate_set_error() directly from vfio_save_setup() instead of adding "Error *err" argument to .save_setup() and changing all other users. What do you prefer?

Well, with my downstreamer hat, I would prefer a simpler solution for the VFIO postcopy limitation first. That said, there is value in adding a 'Error *' parameter to the .save_setup() op and letting the top routine qemu_savevm_state_setup() propagate. Other SaveVMhandler could start using it. even VFIO has multiple error_report() in vfio_save_setup() which could be propagated to the top callers.

Let's try that first. I will check your new series on top of 8.0

Thanks,

C.

Quoted 3 lines

Thanks.

#20 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 90 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 78 lines · Avihai Horon
Quoted 76 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 63 lines · Avihai Horon
Quoted 51 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 22 lines · Avihai Horon
Quoted 21 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 5 lines · Avihai Horon
migration/options.c +0 -0
index 1d1e1321b0..e201053563 100644

migration/options.c bool migrate_caps_check(bool *old_caps, · line 508

Quoted 10 lines
migration/options.c +5 -0
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, 
 bool *new_caps, Error **errp)
                error_setg(errp, "Postcopy is not yet compatible with multifd");
                return false;
            }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with 
 VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

Quoted 8 lines
Quoted 2 lines

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

In that case a migration blocker will be added.

Quoted 1 line

Is it possible to do it in a generic way?

What comes to my mind is to let devices register a handler for a "caps change" notification and allow them to object.
But maybe that's a bit of an overkill.

This one also sounds better than hard-codes to me.

Quoted 8 lines
Quoted 8 lines

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early?  For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Quoted 7 lines

Yes, I think this could work and it will simplify things because we could also drop the VFIO migration blockers code.
The downside is that the user will know migration is blocked only when he tries to migrate, and migrate_caps_check() will not block setting postcopy when a VFIO device is already attached.
I don't have a strong opinion here, so if it's fine by you and everyone else, I could change that to what you suggested.

Quoted 6 lines

Failing later would be fine in this case to me; my expectation is VFIO users should be advanced already anyway (as the whole solution is still pretty involved comparing to a generic VM migration) and shouldn't try to trigger that at all in real life.  IOW I'd expect this check will be there just for sanity, rather than being relied on to let people be aware of it by the error message.

Yes, I agree with you.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Meanwhile the blocker + caps check is slightly complicated to me to guard both sides.  So I'd vote for failing at the QMP command. But we can wait and see whether there's other votes.

Sure.
So I will do the checking in vfio_save_setup(), unless someone else has a better idea.

Just to recap for my understanding,

Quoted 7 lines
Quoted 4 lines

vfio_save_setup() would test migrate_postcopy_ram() and update a new 'Error *err' parameter of the .save_setup() op which would be taken into account in qemu_savevm_state_setup(). Is that correct ?

Yes.
But I wonder if it would be simpler to call migrate_set_error() directly from vfio_save_setup() instead of adding "Error *err" argument to .save_setup() and changing all other users. What do you prefer?

Quoted 9 lines

Well, with my downstreamer hat, I would prefer a simpler solution for the VFIO postcopy limitation first. That said, there is value in adding a 'Error *' parameter to the .save_setup() op and letting the top routine qemu_savevm_state_setup() propagate. Other SaveVMhandler could start using it. even VFIO has multiple error_report() in vfio_save_setup() which could be propagated to the top callers.

Let's try that first. I will check your new series on top of 8.0

OK, so just making sure, you want to add "Error *err" argument to .save_setup() first and see how it goes, right?

Thanks.

#21 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 97 lines · Avihai Horon
Quoted 95 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 76 lines · Avihai Horon
Quoted 74 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 61 lines · Avihai Horon
Quoted 49 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 20 lines · Avihai Horon
Quoted 19 lines · Peter Xu

External email: Use caution opening links or attachments

migration/options.c bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp) · line 506

Quoted 13 lines · Avihai Horon
migration/options.c +5 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
                error_setg(errp, "Postcopy is not yet compatible with multifd");
                return false;
            }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

Quoted 8 lines
Quoted 2 lines

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

In that case a migration blocker will be added.

Quoted 1 line

Is it possible to do it in a generic way?

What comes to my mind is to let devices register a handler for a "caps change" notification and allow them to object.
But maybe that's a bit of an overkill.

This one also sounds better than hard-codes to me.

Quoted 8 lines
Quoted 8 lines

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early?  For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Quoted 7 lines

Yes, I think this could work and it will simplify things because we could also drop the VFIO migration blockers code.
The downside is that the user will know migration is blocked only when he tries to migrate, and migrate_caps_check() will not block setting postcopy when a VFIO device is already attached.
I don't have a strong opinion here, so if it's fine by you and everyone else, I could change that to what you suggested.

Quoted 6 lines

Failing later would be fine in this case to me; my expectation is VFIO users should be advanced already anyway (as the whole solution is still pretty involved comparing to a generic VM migration) and shouldn't try to trigger that at all in real life.  IOW I'd expect this check will be there just for sanity, rather than being relied on to let people be aware of it by the error message.

Yes, I agree with you.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Meanwhile the blocker + caps check is slightly complicated to me to guard both sides.  So I'd vote for failing at the QMP command. But we can wait and see whether there's other votes.

Sure.
So I will do the checking in vfio_save_setup(), unless someone else has a better idea.

Just to recap for my understanding,

Quoted 7 lines
Quoted 4 lines

vfio_save_setup() would test migrate_postcopy_ram() and update a new 'Error *err' parameter of the .save_setup() op which would be taken into account in qemu_savevm_state_setup(). Is that correct ?

Yes.
But I wonder if it would be simpler to call migrate_set_error() directly from vfio_save_setup() instead of adding "Error *err" argument to .save_setup() and changing all other users. What do you prefer?

Well, with my downstreamer hat, I would prefer a simpler solution for the VFIO postcopy limitation first. That said, there is value in adding a 'Error *' parameter to the .save_setup() op and letting the top routine qemu_savevm_state_setup() propagate. Other SaveVMhandler could start using it. even VFIO has multiple error_report() in vfio_save_setup() which could be propagated to the top callers.

Quoted 3 lines
Quoted 2 lines

Let's try that first. I will check your new series on top of 8.0

OK, so just making sure, you want to add "Error *err" argument to .save_setup() first and see how it goes, right?

yes. Sorry. that was not clear.

Thanks

C.

#22 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 98 lines · Cédric Le Goater
Quoted 97 lines · Avihai Horon
Quoted 95 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 76 lines · Avihai Horon
Quoted 74 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 61 lines · Avihai Horon
Quoted 49 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 20 lines · Avihai Horon
Quoted 19 lines · Peter Xu

External email: Use caution opening links or attachments

migration/options.c bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp) · line 506

Quoted 13 lines · Avihai Horon
migration/options.c +5 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
                error_setg(errp, "Postcopy is not yet compatible with multifd");
                return false;
            }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

Quoted 8 lines
Quoted 2 lines

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

In that case a migration blocker will be added.

Quoted 1 line

Is it possible to do it in a generic way?

What comes to my mind is to let devices register a handler for a "caps change" notification and allow them to object.
But maybe that's a bit of an overkill.

This one also sounds better than hard-codes to me.

Quoted 8 lines
Quoted 8 lines

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early?  For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Quoted 7 lines

Yes, I think this could work and it will simplify things because we could also drop the VFIO migration blockers code.
The downside is that the user will know migration is blocked only when he tries to migrate, and migrate_caps_check() will not block setting postcopy when a VFIO device is already attached.
I don't have a strong opinion here, so if it's fine by you and everyone else, I could change that to what you suggested.

Quoted 6 lines

Failing later would be fine in this case to me; my expectation is VFIO users should be advanced already anyway (as the whole solution is still pretty involved comparing to a generic VM migration) and shouldn't try to trigger that at all in real life.  IOW I'd expect this check will be there just for sanity, rather than being relied on to let people be aware of it by the error message.

Yes, I agree with you.

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Meanwhile the blocker + caps check is slightly complicated to me to guard both sides.  So I'd vote for failing at the QMP command. But we can wait and see whether there's other votes.

Sure.
So I will do the checking in vfio_save_setup(), unless someone else has a better idea.

Just to recap for my understanding,

Quoted 7 lines
Quoted 4 lines

vfio_save_setup() would test migrate_postcopy_ram() and update a new 'Error *err' parameter of the .save_setup() op which would be taken into account in qemu_savevm_state_setup(). Is that correct ?

Yes.
But I wonder if it would be simpler to call migrate_set_error() directly from vfio_save_setup() instead of adding "Error *err" argument to .save_setup() and changing all other users. What do you prefer?

Well, with my downstreamer hat, I would prefer a simpler solution for the VFIO postcopy limitation first. That said, there is value in adding a 'Error *' parameter to the .save_setup() op and letting the top routine qemu_savevm_state_setup() propagate. Other SaveVMhandler could start using it. even VFIO has multiple error_report() in vfio_save_setup() which could be propagated to the top callers.

Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Let's try that first. I will check your new series on top of 8.0

OK, so just making sure, you want to add "Error *err" argument to .save_setup() first and see how it goes, right?

yes. Sorry. that was not clear.

I just remembered one pity of failing at save_setup() is it won't fail qmp command "migrate" itself, but only reflected in query-migrate later.

If we want to make it even better (no strong opinion here, of now), we can have it separate from save_setup(), e.g., SaveVMHandlers.save_prepare(), so that it can be called even at migrate_prepare() and fail the QMP command with proper errors.

Thanks,

Signature · 1 line
Peter Xu
#23 lore raw

Re: [PATCH 5/6] vfio/migration: Block VFIO migration with postcopy migration

Quoted 97 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 93 lines · Cédric Le Goater
Quoted 92 lines · Avihai Horon
Quoted 81 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 70 lines · Avihai Horon
Quoted 69 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 56 lines · Avihai Horon
Quoted 49 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 20 lines · Avihai Horon
Quoted 19 lines · Peter Xu

External email: Use caution opening links or attachments

migration/options.c bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp) · line 506

Quoted 13 lines · Avihai Horon
migration/options.c +5 -0
index 1d1e1321b0..e201053563 100644
@@ -499,6 +499,11 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
                error_setg(errp, "Postcopy is not yet compatible with multifd");
                return false;
            }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Postcopy is not compatible with VFIO migration");
+            return false;
+        }

Hmm.. this will add yet another vfio hard-coded line into migration/..

Quoted 8 lines
Quoted 2 lines

What will happen if the vfio device is hot plugged after enabling postcopy-ram here?

In that case a migration blocker will be added.

Quoted 1 line

Is it possible to do it in a generic way?

What comes to my mind is to let devices register a handler for a "caps change" notification and allow them to object.
But maybe that's a bit of an overkill.

This one also sounds better than hard-codes to me.

Quoted 8 lines
Quoted 8 lines

I was thinking the only unified place to do such check is when migration starts, as long as we switch to SETUP all caps are locked and doesn't allow any change until it finishes or fails.

So, can we do this check inside vfio_save_setup(), allow vfio_save_setup() to fail the whole migration early? For example, maybe we should have an Error** passed in, then if it fails it calls migrate_set_error, so reflected in query-migrate later too.

Quoted 7 lines

Yes, I think this could work and it will simplify things because we could also drop the VFIO migration blockers code.
The downside is that the user will know migration is blocked only when he tries to migrate, and migrate_caps_check() will not block setting postcopy when a VFIO device is already attached.
I don't have a strong opinion here, so if it's fine by you and everyone else, I could change that to what you suggested.

Quoted 6 lines

Failing later would be fine in this case to me; my expectation is VFIO users should be advanced already anyway (as the whole solution is still pretty involved comparing to a generic VM migration) and shouldn't try to trigger that at all in real life. IOW I'd expect this check will be there just for sanity, rather than being relied on to let people be aware of it by the error message.

Quoted 7 lines

Yes, I agree with you.

Quoted 3 lines

Meanwhile the blocker + caps check is slightly complicated to me to guard both sides. So I'd vote for failing at the QMP command. But we can wait and see whether there's other votes.

Sure.
So I will do the checking in vfio_save_setup(), unless someone else has a better idea.

Just to recap for my understanding,

Quoted 7 lines
Quoted 4 lines

vfio_save_setup() would test migrate_postcopy_ram() and update a new 'Error *err' parameter of the .save_setup() op which would be taken into account in qemu_savevm_state_setup(). Is that correct ?

Yes.
But I wonder if it would be simpler to call migrate_set_error() directly from vfio_save_setup() instead of adding "Error *err" argument to .save_setup() and changing all other users. What do you prefer?

Quoted 9 lines

Well, with my downstreamer hat, I would prefer a simpler solution for the VFIO postcopy limitation first. That said, there is value in adding a 'Error *' parameter to the .save_setup() op and letting the top routine qemu_savevm_state_setup() propagate. Other SaveVMhandler could start using it. even VFIO has multiple error_report() in vfio_save_setup() which could be propagated to the top callers.

Let's try that first. I will check your new series on top of 8.0

OK, so just making sure, you want to add "Error *err" argument to .save_setup() first and see how it goes, right?

Quoted 8 lines
Quoted 1 line

yes. Sorry. that was not clear.

I just remembered one pity of failing at save_setup() is it won't fail qmp command "migrate" itself, but only reflected in query-migrate later.

If we want to make it even better (no strong opinion here, of now), we can have it separate from save_setup(), e.g., SaveVMHandlers.save_prepare(), so that it can be called even at migrate_prepare() and fail the QMP command with proper errors.

Sounds good.
I will do it and send v2, hopefully tomorrow.

Thanks.

#24 lore raw

PATCH 6/6 vfio/migration: Block VFIO migration with background snapshot

In reply to Avihai Horon

Background snapshot allows creating a snapshot of the VM while it's running and keeping it small by not including dirty RAM pages.

The way it works is by first stopping the VM, saving the non-iterable devices' state and then starting the VM and saving the RAM while write protecting it with UFFD. The resulting snapshot represents the VM state at snapshot start.

VFIO migration is not compatible with background snapshot. First of all, VFIO device state is not even saved in background snapshot because only non-iterable device state is saved. But even if it was saved, after starting the VM, a VFIO device could dirty pages without it being detected by UFFD write protection. This would corrupt the snapshot, as the RAM in it would not represent the RAM at snapshot start.

To prevent this and to be explicit about supported features, block VFIO migration with background snapshot: Fail setting background snapshot capability if a VFIO device is present, and add a migration blocker if a VFIO device is added when background snapshot capability is on.

---
 include/hw/vfio/vfio-common.h |  2 ++
 migration/migration.h         |  1 +
 hw/vfio/common.c              | 42 +++++++++++++++++++++++++++++++++++
 hw/vfio/migration.c           |  6 +++++
 migration/options.c           |  7 ++++++
 migration/target.c            |  9 ++++++++
 6 files changed, 67 insertions(+)
include/hw/vfio/vfio-common.h +2 -0
index c0b58f2bb7..bb94f320f1 100644
@@ -229,6 +229,8 @@ int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp);
 void vfio_unblock_multiple_devices_migration(void);
 int vfio_block_postcopy_migration(VFIODevice *vbasedev, Error **errp);
 void vfio_unblock_postcopy_migration(void);
+int vfio_block_background_snapshot(VFIODevice *vbasedev, Error **errp);
+void vfio_unblock_background_snapshot(void);
 bool vfio_viommu_preset(VFIODevice *vbasedev);
 int64_t vfio_mig_bytes_transferred(void);
 void vfio_reset_bytes_transferred(void);
migration/migration.h +1 -0
index 21a6423408..3077ed430b 100644
@@ -516,6 +516,7 @@ void migration_populate_vfio_info(MigrationInfo *info);
 void migration_reset_vfio_bytes_transferred(void);
 bool migration_vfio_mig_active(void);
 void migration_vfio_unblock_postcopy_migration(void);
+void migration_vfio_unblock_background_snapshot(void);
 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page);
 
 #endif
hw/vfio/common.c +42 -0

54 lines not on this page · raw message

hw/vfio/migration.c +6 -0
index 76406e9ae9..adf98ac8e3 100644
@@ -857,6 +857,7 @@ static void vfio_migration_deinit(VFIODevice *vbasedev)
     vfio_migration_free(vbasedev);
     vfio_unblock_multiple_devices_migration();
     vfio_unblock_postcopy_migration();
+    vfio_unblock_background_snapshot();
 }
 
 static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
@@ -945,6 +946,11 @@ bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
         goto out_deinit;
     }
 
+    ret = vfio_block_background_snapshot(vbasedev, errp);
+    if (ret) {
+        goto out_deinit;
+    }
+
     if (vfio_viommu_preset(vbasedev)) {
         error_setg(&err, "%s: Migration is currently not supported "
                    "with vIOMMU enabled", vbasedev->name);
migration/options.c +7 -0
index e201053563..2e13363de6 100644
@@ -537,6 +537,12 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
                 return false;
             }
         }
+
+        if (migration_vfio_mig_active()) {
+            error_setg(errp, "Background-snapshot is not compatible with VFIO "
+                             "migration");
+            return false;
+        }
     }
 
 #ifdef CONFIG_LINUX
@@ -625,6 +631,7 @@ bool migrate_caps_check(bool *old_caps, bool *new_caps, Error **errp)
 static void migration_caps_remove_blockers(void)
 {
     migration_vfio_unblock_postcopy_migration();
+    migration_vfio_unblock_background_snapshot();
 }
 
 bool migrate_cap_set(int cap, bool value, Error **errp)
migration/target.c +9 -0
index 690ecb4dd5..c2be0b39db 100644
@@ -37,6 +37,11 @@ void migration_vfio_unblock_postcopy_migration(void)
 {
     vfio_unblock_postcopy_migration();
 }
+
+void migration_vfio_unblock_background_snapshot(void)
+{
+    vfio_unblock_background_snapshot();
+}
 #else
 void migration_populate_vfio_info(MigrationInfo *info)
 {
@@ -54,4 +59,8 @@ bool migration_vfio_mig_active(void)
 void migration_vfio_unblock_postcopy_migration()
 {
 }
+
+void migration_vfio_unblock_background_snapshot(void)
+{
+}
 #endif
Signature · 1 line
2.26.3