Skip to contentSkip to conversation

VFIO migration will not be aborted in a corner scenario

Question Kunkun Jiang via 4 participants 11 messages Aug 11, 2025 – Aug 18, 2025

lore
Share & tools
lore
#0 lore raw

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation. Allowing the live migration process to continue could cause unrecoverable damage to the VM. In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

Thanks,
Kunkun Jiang

#1 lore raw

Hello,

+ Avihai

Quoted 6 lines · Kunkun Jiang

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation.

There is error handling in vfio_vmstate_change() :

        /*
         * Migration should be aborted in this case, but vm_state_notify()
         * currently does not support reporting failures.
         */
        migration_file_set_error(ret, local_err);
Quoted 3 lines

Allowing the live migration process to continue could cause unrecoverable damage to the VM. In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

I suggest you open an issue on :

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

with a detailed description of your environment :

Host HW, Host OS, QEMU version, QEMU command line, Guest OS, etc.

A template is provided when a new issue is created.

Thanks,

C.

#2 lore raw
Quoted 15 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello,

+ Avihai

Quoted 6 lines · Kunkun Jiang

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation.

Quoted 7 lines

There is error handling in vfio_vmstate_change() :

        /*
         * Migration should be aborted in this case, but vm_state_notify()
         * currently does not support reporting failures.
         */
        migration_file_set_error(ret, local_err);

Hmm, I think this only sets the error on src. On dst we don't have MigrationState->to_dst_file, so we end up just reporting the error. But even if we did set it, no one is checking if there is a migration error after vm_start() is called in process_incoming_migration_bh().

Quoted 2 lines
Quoted 2 lines

Allowing the live migration process to continue could cause unrecoverable damage to the VM.

What do you mean by unrecoverable damage to the VM?
If RESUMING->RUNNING transition fails, would a VFIO reset recover the device and allow the VM to continue operation with damage limited only to the VFIO device?

Quoted 4 lines
Quoted 3 lines

In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

The straightforward solution, as you suggested, is to exit dst upon error in RESUMING->RUNNING transition and notify about it to src through the return-path. However, I am not sure if failing the migration after vm_start() on dst is a bit late (as we start vCPUs and do migration_block_activate, etc.).

But I can think of another way to solve this, hopefully simpler. According to VFIO migration uAPI [1]:
 * RESUMING -> STOP
 *   Leaving RESUMING terminates a data transfer session and indicates the *   device should complete processing of the data delivered by write(). The *   kernel migration driver should complete the incorporation of data written *   to the data transfer FD into the device internal state and perform *   final validity and consistency checking of the new device state. If the *   user provided data is found to be incomplete, inconsistent, or otherwise *   invalid, the migration driver must fail the SET_STATE ioctl and *   optionally go to the ERROR state as described below.

So, IIUC, we can add an explicit RESUMING->STOP transition [2] after the device config is loaded (which is the last data the device is expected to receive). If this transition fails, it means something was wrong with migration, and we can send src an error msg via return-path (and not continue to vm_start()).

Maybe this approach is less complicated than the first one, and it will also work if src VM was paused prior migration. I already tested some POC and it seems to be working (at least with an artificial error i injected in RESUMING->STOP transition). Kunkun, can you apply the following diff [3] and check if this solves the issue?

And in general, what do you think? Should we go with this approach or do you have other ideas?

Thanks.

[1] https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/vfio.h#L1099
[2] Today RESUMING->STOP is done implicitly by the VFIO driver as part of RESUMING->RUNNING transition.
[3]

hw/vfio/migration-multifd.c +0 -0
index e4785031a7..66f8461f02 100644
@@ -267,6 +267,12 @@ static bool vfio_load_bufs_thread_load_config(VFIODevice *vbasedev,

     ret = vfio_load_device_config_state(f_in, vbasedev); bql_unlock();

+    ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP, +                                   VFIO_DEVICE_STATE_ERROR, errp); +    if (ret) {
+        return false;
+    }
+
     if (ret < 0) {
         error_setg(errp, "%s: vfio_load_device_config_state() failed: %d", vbasedev->name, ret);

hw/vfio/migration.c +0 -0
index 4c06e3db93..a707d17a5b 100644
@@ -737,6 +737,8 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)

         switch (data) {
         case VFIO_MIG_FLAG_DEV_CONFIG_STATE:
         {
+            Error *local_err = NULL;
+
             if (vfio_multifd_transfer_enabled(vbasedev)) { error_report("%s: got DEV_CONFIG_STATE in main migration " "channel but doing multifd transfer",
@@ -744,7 +746,19 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id) return -EINVAL;
             }

-            return vfio_load_device_config_state(f, opaque); +            ret = vfio_load_device_config_state(f, opaque); +            if (ret) {
+                return ret;
+            }
+
+            ret = vfio_migration_set_state_or_reset( +                vbasedev, VFIO_DEVICE_STATE_STOP, &local_err); +            if (ret) {
+                error_report_err(local_err);
+                return ret;
+            }
+
+            return 0;
         }
         case VFIO_MIG_FLAG_DEV_SETUP_STATE:
         {

migration/migration.c +0 -0
index 10c216d25d..fd498c864d 100644
@@ -91,6 +91,7 @@ enum mig_rp_message_type {

     MIG_RP_MSG_RECV_BITMAP,  /* send recved_bitmap back to source */ MIG_RP_MSG_RESUME_ACK,   /* tell source that we are ready to resume */ MIG_RP_MSG_SWITCHOVER_ACK, /* Tell source it's OK to do switchover */ +    MIG_RP_MSG_ERROR, /* Tell source that destination encountered an error */

     MIG_RP_MSG_MAX
 };
@@ -884,6 +885,11 @@ process_incoming_migration_co(void *opaque) ret = qemu_loadvm_state(mis->from_src_file); mis->loadvm_co = NULL;

+    if (ret) {
+        migrate_send_rp_error(mis);
+        error_report("SENT RP ERROR");
+    }
+ trace_vmstate_downtime_checkpoint("dst-precopy-loadvm-completed");

     ps = postcopy_state_get();
@@ -1126,6 +1132,11 @@ bool migration_has_all_channels(void) return true;
 }
+int migrate_send_rp_error(MigrationIncomingState *mis)
+{
+    return migrate_send_rp_message(mis, MIG_RP_MSG_ERROR, 0, NULL); +}
+
 int migrate_send_rp_switchover_ack(MigrationIncomingState *mis) {
     return migrate_send_rp_message(mis, MIG_RP_MSG_SWITCHOVER_ACK, 0, NULL); @@ -2614,6 +2625,10 @@ static void *source_return_path_thread(void *opaque) trace_source_return_path_thread_switchover_acked(); break;

+        case MIG_RP_MSG_ERROR:
+            error_setg(&err, "DST indicated error"); +            goto out;
+
         default:
             break;
         }

migration/migration.h +0 -0
index 01329bf824..f11ff7a199 100644
@@ -553,6 +553,7 @@ void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,

                                  char *block_name);
 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value); int migrate_send_rp_switchover_ack(MigrationIncomingState *mis); +int migrate_send_rp_error(MigrationIncomingState *mis);

 void dirty_bitmap_mig_before_vm_start(void);
 void dirty_bitmap_mig_cancel_outgoing(void);

Quoted 17 lines

I suggest you open an issue on :

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

with a detailed description of your environment :

  Host HW, Host OS, QEMU version, QEMU command line, Guest OS, etc.

A template is provided when a new issue is created.

Thanks,

C.

#3 lore raw

+peter
+fabiano

Quoted 198 lines · Avihai Horon
Quoted 15 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello,

+ Avihai

Quoted 6 lines · Kunkun Jiang

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation.

Quoted 7 lines

There is error handling in vfio_vmstate_change() :

        /*
         * Migration should be aborted in this case, but vm_state_notify()
         * currently does not support reporting failures.
         */
        migration_file_set_error(ret, local_err);

Hmm, I think this only sets the error on src. On dst we don't have MigrationState->to_dst_file, so we end up just reporting the error. But even if we did set it, no one is checking if there is a migration error after vm_start() is called in process_incoming_migration_bh().

Quoted 2 lines
Quoted 2 lines

Allowing the live migration process to continue could cause unrecoverable damage to the VM.

What do you mean by unrecoverable damage to the VM?
If RESUMING->RUNNING transition fails, would a VFIO reset recover the device and allow the VM to continue operation with damage limited only to the VFIO device?

Quoted 4 lines
Quoted 3 lines

In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

The straightforward solution, as you suggested, is to exit dst upon error in RESUMING->RUNNING transition and notify about it to src through the return-path. However, I am not sure if failing the migration after vm_start() on dst is a bit late (as we start vCPUs and do migration_block_activate, etc.).

But I can think of another way to solve this, hopefully simpler. According to VFIO migration uAPI [1]:
 * RESUMING -> STOP
 *   Leaving RESUMING terminates a data transfer session and indicates the *   device should complete processing of the data delivered by write(). The *   kernel migration driver should complete the incorporation of data written *   to the data transfer FD into the device internal state and perform *   final validity and consistency checking of the new device state. If the *   user provided data is found to be incomplete, inconsistent, or otherwise *   invalid, the migration driver must fail the SET_STATE ioctl and *   optionally go to the ERROR state as described below.

So, IIUC, we can add an explicit RESUMING->STOP transition [2] after the device config is loaded (which is the last data the device is expected to receive). If this transition fails, it means something was wrong with migration, and we can send src an error msg via return-path (and not continue to vm_start()).

Maybe this approach is less complicated than the first one, and it will also work if src VM was paused prior migration. I already tested some POC and it seems to be working (at least with an artificial error i injected in RESUMING->STOP transition). Kunkun, can you apply the following diff [3] and check if this solves the issue?

And in general, what do you think? Should we go with this approach or do you have other ideas?

Thanks.

[1] https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/vfio.h#L1099
[2] Today RESUMING->STOP is done implicitly by the VFIO driver as part of RESUMING->RUNNING transition.
[3]

hw/vfio/migration-multifd.c +6 -0
index e4785031a7..66f8461f02 100644
@@ -267,6 +267,12 @@ static bool vfio_load_bufs_thread_load_config(VFIODevice *vbasedev,
      ret = vfio_load_device_config_state(f_in, vbasedev);
      bql_unlock();
 
+    ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP,
+                                   VFIO_DEVICE_STATE_ERROR, errp);
+    if (ret) {
+        return false;
+    }
+
      if (ret < 0) {
          error_setg(errp, "%s: vfio_load_device_config_state() failed: %d",
                     vbasedev->name, ret);
hw/vfio/migration.c +15 -1
index 4c06e3db93..a707d17a5b 100644
@@ -737,6 +737,8 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)
          switch (data) {
          case VFIO_MIG_FLAG_DEV_CONFIG_STATE:
          {
+            Error *local_err = NULL;
+
              if (vfio_multifd_transfer_enabled(vbasedev)) {
                  error_report("%s: got DEV_CONFIG_STATE in main migration "
                               "channel but doing multifd transfer",
@@ -744,7 +746,19 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)
                  return -EINVAL;
              }
 
-            return vfio_load_device_config_state(f, opaque);
+            ret = vfio_load_device_config_state(f, opaque);
+            if (ret) {
+                return ret;
+            }
+
+            ret = vfio_migration_set_state_or_reset(
+                vbasedev, VFIO_DEVICE_STATE_STOP, &local_err);
+            if (ret) {
+                error_report_err(local_err);
+                return ret;
+            }
+
+            return 0;
          }
          case VFIO_MIG_FLAG_DEV_SETUP_STATE:
          {
migration/migration.c +15 -0
index 10c216d25d..fd498c864d 100644
@@ -91,6 +91,7 @@ enum mig_rp_message_type {
      MIG_RP_MSG_RECV_BITMAP,  /* send recved_bitmap back to source */
      MIG_RP_MSG_RESUME_ACK,   /* tell source that we are ready to resume */
      MIG_RP_MSG_SWITCHOVER_ACK, /* Tell source it's OK to do switchover */
+    MIG_RP_MSG_ERROR, /* Tell source that destination encountered an error */
 
      MIG_RP_MSG_MAX
  };
@@ -884,6 +885,11 @@ process_incoming_migration_co(void *opaque)
      ret = qemu_loadvm_state(mis->from_src_file);
      mis->loadvm_co = NULL;
 
+    if (ret) {
+        migrate_send_rp_error(mis);
+        error_report("SENT RP ERROR");
+    }
+
  trace_vmstate_downtime_checkpoint("dst-precopy-loadvm-completed");
 
      ps = postcopy_state_get();
@@ -1126,6 +1132,11 @@ bool migration_has_all_channels(void)
      return true;
  }
+int migrate_send_rp_error(MigrationIncomingState *mis)
+{
+    return migrate_send_rp_message(mis, MIG_RP_MSG_ERROR, 0, NULL);
+}
+
  int migrate_send_rp_switchover_ack(MigrationIncomingState *mis)
  {
      return migrate_send_rp_message(mis, MIG_RP_MSG_SWITCHOVER_ACK, 0, NULL);
@@ -2614,6 +2625,10 @@ static void *source_return_path_thread(void *opaque)
              trace_source_return_path_thread_switchover_acked();
              break;
 
+        case MIG_RP_MSG_ERROR:
+            error_setg(&err, "DST indicated error");
+            goto out;
+
          default:
              break;
          }
migration/migration.h +1 -0
index 01329bf824..f11ff7a199 100644
@@ -553,6 +553,7 @@ void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
                                   char *block_name);
  void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
  int migrate_send_rp_switchover_ack(MigrationIncomingState *mis);
+int migrate_send_rp_error(MigrationIncomingState *mis);
 
  void dirty_bitmap_mig_before_vm_start(void);
  void dirty_bitmap_mig_cancel_outgoing(void);
Quoted 18 lines

I suggest you open an issue on :

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

with a detailed description of your environment :

  Host HW, Host OS, QEMU version, QEMU command line, Guest OS, etc.

A template is provided when a new issue is created.

Thanks,

C.

#4 lore raw
Quoted 164 lines · Cédric Le Goater
(quoted diff) +2 -0
+peter
+fabiano
Quoted 160 lines · Avihai Horon
Quoted 15 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello,

+ Avihai

Quoted 6 lines · Kunkun Jiang

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation.

Quoted 7 lines

There is error handling in vfio_vmstate_change() :

        /*
         * Migration should be aborted in this case, but vm_state_notify()
         * currently does not support reporting failures.
         */
        migration_file_set_error(ret, local_err);

Hmm, I think this only sets the error on src. On dst we don't have MigrationState->to_dst_file, so we end up just reporting the error. But even if we did set it, no one is checking if there is a migration error after vm_start() is called in process_incoming_migration_bh().

Quoted 2 lines
Quoted 2 lines

Allowing the live migration process to continue could cause unrecoverable damage to the VM.

What do you mean by unrecoverable damage to the VM?
If RESUMING->RUNNING transition fails, would a VFIO reset recover the device and allow the VM to continue operation with damage limited only to the VFIO device?

Quoted 4 lines
Quoted 3 lines

In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

The straightforward solution, as you suggested, is to exit dst upon error in RESUMING->RUNNING transition and notify about it to src through the return-path. However, I am not sure if failing the migration after vm_start() on dst is a bit late (as we start vCPUs and do migration_block_activate, etc.).

But I can think of another way to solve this, hopefully simpler. According to VFIO migration uAPI [1]:
 * RESUMING -> STOP
 *   Leaving RESUMING terminates a data transfer session and indicates the *   device should complete processing of the data delivered by write(). The *   kernel migration driver should complete the incorporation of data written *   to the data transfer FD into the device internal state and perform *   final validity and consistency checking of the new device state. If the *   user provided data is found to be incomplete, inconsistent, or otherwise *   invalid, the migration driver must fail the SET_STATE ioctl and *   optionally go to the ERROR state as described below.

So, IIUC, we can add an explicit RESUMING->STOP transition [2] after the device config is loaded (which is the last data the device is expected to receive). If this transition fails, it means something was wrong with migration, and we can send src an error msg via return-path (and not continue to vm_start()).

Maybe this approach is less complicated than the first one, and it will also work if src VM was paused prior migration. I already tested some POC and it seems to be working (at least with an artificial error i injected in RESUMING->STOP transition). Kunkun, can you apply the following diff [3] and check if this solves the issue?

And in general, what do you think? Should we go with this approach or do you have other ideas?

Thanks.

[1] https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/vfio.h#L1099
[2] Today RESUMING->STOP is done implicitly by the VFIO driver as part of RESUMING->RUNNING transition.
[3]

hw/vfio/migration-multifd.c +6 -0
index e4785031a7..66f8461f02 100644
@@ -267,6 +267,12 @@ static bool vfio_load_bufs_thread_load_config(VFIODevice *vbasedev,
      ret = vfio_load_device_config_state(f_in, vbasedev);
      bql_unlock();
 
+    ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP,
+                                   VFIO_DEVICE_STATE_ERROR, errp);
+    if (ret) {
+        return false;
+    }
+
      if (ret < 0) {
          error_setg(errp, "%s: vfio_load_device_config_state() failed: %d",
                     vbasedev->name, ret);
hw/vfio/migration.c +15 -1
index 4c06e3db93..a707d17a5b 100644
@@ -737,6 +737,8 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)
          switch (data) {
          case VFIO_MIG_FLAG_DEV_CONFIG_STATE:
          {
+            Error *local_err = NULL;
+
              if (vfio_multifd_transfer_enabled(vbasedev)) {
                  error_report("%s: got DEV_CONFIG_STATE in main migration "
                               "channel but doing multifd transfer",
@@ -744,7 +746,19 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)
                  return -EINVAL;
              }
 
-            return vfio_load_device_config_state(f, opaque);
+            ret = vfio_load_device_config_state(f, opaque);
+            if (ret) {
+                return ret;
+            }
+
+            ret = vfio_migration_set_state_or_reset(
+                vbasedev, VFIO_DEVICE_STATE_STOP, &local_err);
+            if (ret) {
+                error_report_err(local_err);
+                return ret;
+            }
+
+            return 0;
          }
          case VFIO_MIG_FLAG_DEV_SETUP_STATE:
          {
migration/migration.c +11 -0
index 10c216d25d..fd498c864d 100644
@@ -91,6 +91,7 @@ enum mig_rp_message_type {
      MIG_RP_MSG_RECV_BITMAP,  /* send recved_bitmap back to source */
      MIG_RP_MSG_RESUME_ACK,   /* tell source that we are ready to resume */
      MIG_RP_MSG_SWITCHOVER_ACK, /* Tell source it's OK to do switchover */
+    MIG_RP_MSG_ERROR, /* Tell source that destination encountered an error */
 
      MIG_RP_MSG_MAX
  };
@@ -884,6 +885,11 @@ process_incoming_migration_co(void *opaque)
      ret = qemu_loadvm_state(mis->from_src_file);
      mis->loadvm_co = NULL;
 
+    if (ret) {
+        migrate_send_rp_error(mis);
+        error_report("SENT RP ERROR");
+    }
+
  trace_vmstate_downtime_checkpoint("dst-precopy-loadvm-completed");
 
      ps = postcopy_state_get();
@@ -1126,6 +1132,11 @@ bool migration_has_all_channels(void)
      return true;
  }
+int migrate_send_rp_error(MigrationIncomingState *mis)
+{
+    return migrate_send_rp_message(mis, MIG_RP_MSG_ERROR, 0, NULL);
+}
+
  int migrate_send_rp_switchover_ack(MigrationIncomingState *mis)
  {
      return migrate_send_rp_message(mis, MIG_RP_MSG_SWITCHOVER_ACK, 0, NULL);
@@ -2614,6 +2625,10 @@ static void *source_return_path_thread(void *opaque)
              trace_source_return_path_thread_switchover_acked();
              break;
 
Quoted 3 lines

migration/migration.c static void *source_return_path_thread(void *opaque) · line 2630

Quoted 3 lines
migration/migration.c +3 -0
+        case MIG_RP_MSG_ERROR:
+            error_setg(&err, "DST indicated error");
+            goto out;

If this is only a boolean, we can reuse RP_SHUT. Likely we could pass in an error to migration_incoming_state_destroy():

migration/migration.c +6 -3
index 42a2a6e8f2..2ebba7838a 100644
@@ -441,7 +441,7 @@ void migration_incoming_transport_cleanup(MigrationIncomingState *mis)
     }
 }
 
-void migration_incoming_state_destroy(void)
+void migration_incoming_state_destroy(bool has_error)
 {
     struct MigrationIncomingState *mis = migration_incoming_get_current();
 
@@ -466,8 +466,11 @@ void migration_incoming_state_destroy(void)
     qemu_loadvm_state_cleanup(mis);
 
     if (mis->to_src_file) {
-        /* Tell source that we are done */
-        migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
+        /* Tell source whether load succeeded */
+        if (!has_error) {
+            has_error = qemu_file_get_error(mis->from_src_file) != 0;
+        }
+        migrate_send_rp_shut(mis, has_error);
         qemu_fclose(mis->to_src_file);
         mis->to_src_file = NULL;
     }

Maybe it'll even work as late as process_incoming_migration_bh(), where vm_start() could fail - right now it couldn't, but if there'll be an error message reported upward then logically it can also set has_error=1 for the RP_SHUT message. Src QEMU relies on RP_SHUT message and retval=0 to quit src QEMU, otherwise QEMU should fail the migration and restart VM on src.

Quoted 36 lines
Quoted 35 lines
migration/migration.c +1 -0
+
          default:
              break;
          }
migration/migration.h +1 -0
index 01329bf824..f11ff7a199 100644
@@ -553,6 +553,7 @@ void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
                                   char *block_name);
  void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
  int migrate_send_rp_switchover_ack(MigrationIncomingState *mis);
+int migrate_send_rp_error(MigrationIncomingState *mis);
 
  void dirty_bitmap_mig_before_vm_start(void);
  void dirty_bitmap_mig_cancel_outgoing(void);
Quoted 18 lines

I suggest you open an issue on :

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

with a detailed description of your environment :

  Host HW, Host OS, QEMU version, QEMU command line, Guest OS, etc.

A template is provided when a new issue is created.

Thanks,

C.

Signature · 1 line
Peter Xu
#5 lore raw
Quoted 196 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 158 lines · Cédric Le Goater
(quoted diff) +2 -0
+peter
+fabiano
Quoted 154 lines · Avihai Horon
Quoted 13 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello,

+ Avihai

Quoted 5 lines · Kunkun Jiang

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

Quoted 8 lines
Quoted 1 line

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation.

There is error handling in vfio_vmstate_change() :

         /*
          * Migration should be aborted in this case, but vm_state_notify()
          * currently does not support reporting failures.
          */
         migration_file_set_error(ret, local_err);

Hmm, I think this only sets the error on src. On dst we don't have MigrationState->to_dst_file, so we end up just reporting the error. But even if we did set it, no one is checking if there is a migration error after vm_start() is called in process_incoming_migration_bh().

Quoted 1 line
Quoted 1 line

Allowing the live migration process to continue could cause unrecoverable damage to the VM.

What do you mean by unrecoverable damage to the VM?
If RESUMING->RUNNING transition fails, would a VFIO reset recover the device and allow the VM to continue operation with damage limited only to the VFIO device?

Quoted 3 lines
Quoted 3 lines

In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

The straightforward solution, as you suggested, is to exit dst upon error in RESUMING->RUNNING transition and notify about it to src through the return-path. However, I am not sure if failing the migration after vm_start() on dst is a bit late (as we start vCPUs and do migration_block_activate, etc.).

But I can think of another way to solve this, hopefully simpler. According to VFIO migration uAPI [1]:
* RESUMING -> STOP

   *   Leaving RESUMING terminates a data transfer session and indicates the
   *   device should complete processing of the data delivered by write(). The
   *   kernel migration driver should complete the incorporation of data written
   *   to the data transfer FD into the device internal state and perform
   *   final validity and consistency checking of the new device state. If the
   *   user provided data is found to be incomplete, inconsistent, or otherwise
   *   invalid, the migration driver must fail the SET_STATE ioctl and
   *   optionally go to the ERROR state as described below.

So, IIUC, we can add an explicit RESUMING->STOP transition [2] after the device config is loaded (which is the last data the device is expected to receive). If this transition fails, it means something was wrong with migration, and we can send src an error msg via return-path (and not continue to vm_start()).

Maybe this approach is less complicated than the first one, and it will also work if src VM was paused prior migration. I already tested some POC and it seems to be working (at least with an artificial error i injected in RESUMING->STOP transition). Kunkun, can you apply the following diff [3] and check if this solves the issue?

And in general, what do you think? Should we go with this approach or do you have other ideas?

Thanks.

[1] https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/vfio.h#L1099
[2] Today RESUMING->STOP is done implicitly by the VFIO driver as part of RESUMING->RUNNING transition.
[3]

hw/vfio/migration-multifd.c +6 -0
index e4785031a7..66f8461f02 100644
@@ -267,6 +267,12 @@ static bool vfio_load_bufs_thread_load_config(VFIODevice *vbasedev,
       ret = vfio_load_device_config_state(f_in, vbasedev);
       bql_unlock();
 
+    ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP,
+                                   VFIO_DEVICE_STATE_ERROR, errp);
+    if (ret) {
+        return false;
+    }
+
       if (ret < 0) {
           error_setg(errp, "%s: vfio_load_device_config_state() failed: %d",
                      vbasedev->name, ret);
hw/vfio/migration.c +15 -1
index 4c06e3db93..a707d17a5b 100644
@@ -737,6 +737,8 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)
           switch (data) {
           case VFIO_MIG_FLAG_DEV_CONFIG_STATE:
           {
+            Error *local_err = NULL;
+
               if (vfio_multifd_transfer_enabled(vbasedev)) {
                   error_report("%s: got DEV_CONFIG_STATE in main migration "
                                "channel but doing multifd transfer",
@@ -744,7 +746,19 @@ static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)
                   return -EINVAL;
               }
 
-            return vfio_load_device_config_state(f, opaque);
+            ret = vfio_load_device_config_state(f, opaque);
+            if (ret) {
+                return ret;
+            }
+
+            ret = vfio_migration_set_state_or_reset(
+                vbasedev, VFIO_DEVICE_STATE_STOP, &local_err);
+            if (ret) {
+                error_report_err(local_err);
+                return ret;
+            }
+
+            return 0;
           }
           case VFIO_MIG_FLAG_DEV_SETUP_STATE:
           {
migration/migration.c +11 -0
index 10c216d25d..fd498c864d 100644
@@ -91,6 +91,7 @@ enum mig_rp_message_type {
       MIG_RP_MSG_RECV_BITMAP,  /* send recved_bitmap back to source */
       MIG_RP_MSG_RESUME_ACK,   /* tell source that we are ready to resume */
       MIG_RP_MSG_SWITCHOVER_ACK, /* Tell source it's OK to do switchover */
+    MIG_RP_MSG_ERROR, /* Tell source that destination encountered an error */
 
       MIG_RP_MSG_MAX
   };
@@ -884,6 +885,11 @@ process_incoming_migration_co(void *opaque)
       ret = qemu_loadvm_state(mis->from_src_file);
       mis->loadvm_co = NULL;
 
+    if (ret) {
+        migrate_send_rp_error(mis);
+        error_report("SENT RP ERROR");
+    }
+
   trace_vmstate_downtime_checkpoint("dst-precopy-loadvm-completed");
 
       ps = postcopy_state_get();
@@ -1126,6 +1132,11 @@ bool migration_has_all_channels(void)
       return true;
   }
+int migrate_send_rp_error(MigrationIncomingState *mis)
+{
+    return migrate_send_rp_message(mis, MIG_RP_MSG_ERROR, 0, NULL);
+}
+
   int migrate_send_rp_switchover_ack(MigrationIncomingState *mis)
   {
       return migrate_send_rp_message(mis, MIG_RP_MSG_SWITCHOVER_ACK, 0, NULL);
@@ -2614,6 +2625,10 @@ static void *source_return_path_thread(void *opaque)
               trace_source_return_path_thread_switchover_acked();
               break;
 
Quoted 3 lines

migration/migration.c static void *source_return_path_thread(void *opaque) · line 2630

Quoted 3 lines
migration/migration.c +3 -0
+        case MIG_RP_MSG_ERROR:
+            error_setg(&err, "DST indicated error");
+            goto out;

If this is only a boolean, we can reuse RP_SHUT. Likely we could pass in an error to migration_incoming_state_destroy():

migration/migration.c +6 -3
index 42a2a6e8f2..2ebba7838a 100644
@@ -441,7 +441,7 @@ void migration_incoming_transport_cleanup(MigrationIncomingState *mis)
      }
  }
 
-void migration_incoming_state_destroy(void)
+void migration_incoming_state_destroy(bool has_error)
  {
      struct MigrationIncomingState *mis = migration_incoming_get_current();
 
@@ -466,8 +466,11 @@ void migration_incoming_state_destroy(void)
      qemu_loadvm_state_cleanup(mis);
 
      if (mis->to_src_file) {
-        /* Tell source that we are done */
-        migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0);
+        /* Tell source whether load succeeded */
+        if (!has_error) {
+            has_error = qemu_file_get_error(mis->from_src_file) != 0;
+        }
+        migrate_send_rp_shut(mis, has_error);
          qemu_fclose(mis->to_src_file);
          mis->to_src_file = NULL;
      }
Quoted 5 lines

Maybe it'll even work as late as process_incoming_migration_bh(), where vm_start() could fail - right now it couldn't, but if there'll be an error message reported upward then logically it can also set has_error=1 for the RP_SHUT message. Src QEMU relies on RP_SHUT message and retval=0 to quit src QEMU, otherwise QEMU should fail the migration and restart VM on src.

Ah, nice! I didn't notice RP_SHUT can take an error value.

Will try that, thanks!

Quoted 38 lines
Quoted 29 lines
Quoted 29 lines
migration/migration.c +1 -0
+
           default:
               break;
           }
migration/migration.h +1 -0
index 01329bf824..f11ff7a199 100644
@@ -553,6 +553,7 @@ void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
                                    char *block_name);
   void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
   int migrate_send_rp_switchover_ack(MigrationIncomingState *mis);
+int migrate_send_rp_error(MigrationIncomingState *mis);
 
   void dirty_bitmap_mig_before_vm_start(void);
   void dirty_bitmap_mig_cancel_outgoing(void);
Quoted 12 lines

I suggest you open an issue on :

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

with a detailed description of your environment :

Host HW, Host OS, QEMU version, QEMU command line, Guest OS, etc.

A template is provided when a new issue is created.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Thanks,

C.

--
Peter Xu

#6 lore raw
In reply to Avihai Horon
Quoted 60 lines · Avihai Horon
Quoted 15 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello,

+ Avihai

Quoted 6 lines · Kunkun Jiang

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation.

Quoted 7 lines

There is error handling in vfio_vmstate_change() :

        /*
         * Migration should be aborted in this case, but vm_state_notify()
         * currently does not support reporting failures.
         */
        migration_file_set_error(ret, local_err);

Hmm, I think this only sets the error on src. On dst we don't have MigrationState->to_dst_file, so we end up just reporting the error. But even if we did set it, no one is checking if there is a migration error after vm_start() is called in process_incoming_migration_bh().

Quoted 2 lines
Quoted 2 lines

Allowing the live migration process to continue could cause unrecoverable damage to the VM.

What do you mean by unrecoverable damage to the VM?
If RESUMING->RUNNING transition fails, would a VFIO reset recover the device and allow the VM to continue operation with damage limited only to the VFIO device?

Quoted 4 lines
Quoted 3 lines

In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

The straightforward solution, as you suggested, is to exit dst upon error in RESUMING->RUNNING transition and notify about it to src through the return-path. However, I am not sure if failing the migration after vm_start() on dst is a bit late (as we start vCPUs and do migration_block_activate, etc.).

But I can think of another way to solve this, hopefully simpler. According to VFIO migration uAPI [1]:
 * RESUMING -> STOP
 *   Leaving RESUMING terminates a data transfer session and indicates the *   device should complete processing of the data delivered by write(). The *   kernel migration driver should complete the incorporation of data written *   to the data transfer FD into the device internal state and perform *   final validity and consistency checking of the new device state. If the *   user provided data is found to be incomplete, inconsistent, or otherwise *   invalid, the migration driver must fail the SET_STATE ioctl and *   optionally go to the ERROR state as described below.

So, IIUC, we can add an explicit RESUMING->STOP transition [2] after the device config is loaded (which is the last data the device is expected to receive). If this transition fails, it means something was wrong with migration, and we can send src an error msg via return-path (and not continue to vm_start()).

Maybe this approach is less complicated than the first one, and it will also work if src VM was paused prior migration. I already tested some POC and it seems to be working (at least with an artificial error i injected in RESUMING->STOP transition). Kunkun, can you apply the following diff [3] and check if this solves the issue?

Quoted 7 lines

And in general, what do you think? Should we go with this approach or do you have other ideas?

Thanks.

[1] https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/vfio.h#L1099
[2] Today RESUMING->STOP is done implicitly by the VFIO driver as part of RESUMING->RUNNING transition.
[3]

Avihai,

Could you please send an RFC patch with Peter and Fabiano in cc: ? This will help to discuss the proposal and keep track of the issue.

Kunkun Jiang,

Could you please share details on your environment ?

Thanks,

C.

#7 lore raw

Hi Cédric,

Quoted 72 lines · Cédric Le Goater
Quoted 60 lines · Avihai Horon
Quoted 15 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello,

+ Avihai

Quoted 6 lines · Kunkun Jiang

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation.

Quoted 7 lines

There is error handling in vfio_vmstate_change() :

        /*
         * Migration should be aborted in this case, but vm_state_notify()
         * currently does not support reporting failures.
         */
        migration_file_set_error(ret, local_err);

Hmm, I think this only sets the error on src. On dst we don't have MigrationState->to_dst_file, so we end up just reporting the error. But even if we did set it, no one is checking if there is a migration error after vm_start() is called in process_incoming_migration_bh().

Quoted 2 lines
Quoted 2 lines

Allowing the live migration process to continue could cause unrecoverable damage to the VM.

What do you mean by unrecoverable damage to the VM?
If RESUMING->RUNNING transition fails, would a VFIO reset recover the device and allow the VM to continue operation with damage limited only to the VFIO device?

Quoted 4 lines
Quoted 3 lines

In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

The straightforward solution, as you suggested, is to exit dst upon error in RESUMING->RUNNING transition and notify about it to src through the return-path. However, I am not sure if failing the migration after vm_start() on dst is a bit late (as we start vCPUs and do migration_block_activate, etc.).

But I can think of another way to solve this, hopefully simpler. According to VFIO migration uAPI [1]:
  * RESUMING -> STOP
  *   Leaving RESUMING terminates a data transfer session and indicates the *   device should complete processing of the data delivered by write(). The *   kernel migration driver should complete the incorporation of data written *   to the data transfer FD into the device internal state and perform *   final validity and consistency checking of the new device state. If the *   user provided data is found to be incomplete, inconsistent, or otherwise *   invalid, the migration driver must fail the SET_STATE ioctl and *   optionally go to the ERROR state as described below.

So, IIUC, we can add an explicit RESUMING->STOP transition [2] after the device config is loaded (which is the last data the device is expected to receive). If this transition fails, it means something was wrong with migration, and we can send src an error msg via return-path (and not continue to vm_start()).

Maybe this approach is less complicated than the first one, and it will also work if src VM was paused prior migration. I already tested some POC and it seems to be working (at least with an artificial error i injected in RESUMING->STOP transition). Kunkun, can you apply the following diff [3] and check if this solves the issue?

Quoted 8 lines

And in general, what do you think? Should we go with this approach or do you have other ideas?

Thanks.

[1] https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/vfio.h#L1099

[2] Today RESUMING->STOP is done implicitly by the VFIO driver as part of RESUMING->RUNNING transition.
[3]

Avihai,

Quoted 7 lines

Could you please send an RFC patch with Peter and Fabiano in cc: ? This will help to discuss the proposal and keep track of the issue.

Kunkun Jiang,

Could you please share details on your environment ?

Sorry for the late reply.My test scenario is this:
Server: Kunpeng-920
VFUI device: SEC, a Kunpeng hardware accelerator
Host OS: openEuler 24.03, kernel 6.6, qemu 8.2
Guest OS: openEuler 24.03
Test steps:
1. Start a VM and run a task using SEC inside the VM
2. Start migration and inject an error into the SEC used by dst-VM
3. The SEC used by dst-VM will be reset. So RESUMING->RUNNING transition will fail with probability.(The timing has to be just right)

Quoted 7 lines

Thanks,

C.

.

#8 lore raw
In reply to Cédric Le Goater
Quoted 68 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Quoted 64 lines · Avihai Horon
Quoted 15 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello,

+ Avihai

Quoted 6 lines · Kunkun Jiang

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation.

Quoted 7 lines

There is error handling in vfio_vmstate_change() :

        /*
         * Migration should be aborted in this case, but vm_state_notify()
         * currently does not support reporting failures.
         */
        migration_file_set_error(ret, local_err);

Hmm, I think this only sets the error on src. On dst we don't have MigrationState->to_dst_file, so we end up just reporting the error. But even if we did set it, no one is checking if there is a migration error after vm_start() is called in process_incoming_migration_bh().

Quoted 2 lines
Quoted 2 lines

Allowing the live migration process to continue could cause unrecoverable damage to the VM.

What do you mean by unrecoverable damage to the VM?
If RESUMING->RUNNING transition fails, would a VFIO reset recover the device and allow the VM to continue operation with damage limited only to the VFIO device?

Quoted 4 lines
Quoted 3 lines

In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

The straightforward solution, as you suggested, is to exit dst upon error in RESUMING->RUNNING transition and notify about it to src through the return-path. However, I am not sure if failing the migration after vm_start() on dst is a bit late (as we start vCPUs and do migration_block_activate, etc.).

But I can think of another way to solve this, hopefully simpler. According to VFIO migration uAPI [1]:
  * RESUMING -> STOP
  *   Leaving RESUMING terminates a data transfer session and indicates the *   device should complete processing of the data delivered by write(). The *   kernel migration driver should complete the incorporation of data written *   to the data transfer FD into the device internal state and perform *   final validity and consistency checking of the new device state. If the *   user provided data is found to be incomplete, inconsistent, or otherwise *   invalid, the migration driver must fail the SET_STATE ioctl and *   optionally go to the ERROR state as described below.

So, IIUC, we can add an explicit RESUMING->STOP transition [2] after the device config is loaded (which is the last data the device is expected to receive). If this transition fails, it means something was wrong with migration, and we can send src an error msg via return-path (and not continue to vm_start()).

Maybe this approach is less complicated than the first one, and it will also work if src VM was paused prior migration. I already tested some POC and it seems to be working (at least with an artificial error i injected in RESUMING->STOP transition). Kunkun, can you apply the following diff [3] and check if this solves the issue?

And in general, what do you think? Should we go with this approach or do you have other ideas?

Thanks.

Quoted 9 lines
Quoted 3 lines

[1] https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/vfio.h#L1099
[2] Today RESUMING->STOP is done implicitly by the VFIO driver as part of RESUMING->RUNNING transition.
[3]

Avihai,

Could you please send an RFC patch with Peter and Fabiano in cc: ? This will help to discuss the proposal and keep track of the issue.

Yes, of course. But I am a bit busy with other stuff, so it may take me a few days to do that.

Of course, If anyone would like, feel free to send your own proposal.

Thanks.

Quoted 9 lines

Kunkun Jiang,

Could you please share details on your environment ?

Thanks,

C.

#9 lore raw
In reply to Avihai Horon

Hi Avihai,

Quoted 19 lines · Avihai Horon
Quoted 17 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello,

+ Avihai

Quoted 6 lines · Kunkun Jiang

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation.

There is error handling in vfio_vmstate_change() :

Quoted 8 lines
Quoted 5 lines

        /*
         * Migration should be aborted in this case, but vm_state_notify()
         * currently does not support reporting failures.
         */
        migration_file_set_error(ret, local_err);

Hmm, I think this only sets the error on src. On dst we don't have MigrationState->to_dst_file, so we end up just reporting the erro > But even if we did set it, no one is checking if there is a migration error after vm_start() is called in process_incoming_migration_bh().

Yes, that's what I mean.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Allowing the live migration process to continue could cause unrecoverable damage to the VM.

What do you mean by unrecoverable damage to the VM?
If RESUMING->RUNNING transition fails, would a VFIO reset recover the device and allow the VM to continue operation with damage limited only to the VFIO device?

I didn't express myself clearly, let me explain again.
In my test, I ran a task inside the VM that was using this vfio device.
In this corner scenario, the task is aborted immediately and the VM is still running normally. I mean it is unacceptable that the task is aborted directly.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

The straightforward solution, as you suggested, is to exit dst upon error in RESUMING->RUNNING transition and notify about it to src through the return-path. However, I am not sure if failing the migration after vm_start() on dst is a bit late (as we start vCPUs and do migration_block_activate, etc.).

I think vCPUs have not started yet. vCPUs will be started only after the all callbacks of vm_state_notify are executed. If there are any problems with my analysis, please point them out.

Quoted 17 lines

But I can think of another way to solve this, hopefully simpler. According to VFIO migration uAPI [1]:
 * RESUMING -> STOP
 *   Leaving RESUMING terminates a data transfer session and indicates the *   device should complete processing of the data delivered by
write(). The
 *   kernel migration driver should complete the incorporation of data
written
 *   to the data transfer FD into the device internal state and perform *   final validity and consistency checking of the new device state.
If the
 *   user provided data is found to be incomplete, inconsistent, or
otherwise
 *   invalid, the migration driver must fail the SET_STATE ioctl and *   optionally go to the ERROR state as described below.

Quoted 6 lines

So, IIUC, we can add an explicit RESUMING->STOP transition [2] after the device config is loaded (which is the last data the device is expected to receive). If this transition fails, it means something was wrong with migration, and we can send src an error msg via return-path (and not continue to vm_start()).

Maybe this approach is less complicated than the first one, and it will also work if src VM was paused prior migration. I already tested some POC and it seems to be working (at least with an artificial error i injected in RESUMING->STOP transition). Kunkun, can you apply the following diff [3] and check if this solves the issue?

Ok, I'll try it.

Quoted 2 lines

And in general, what do you think? Should we go with this approach or do you have other ideas?

My current approach is rather crude. As I said above, I think vcpu has not started yet, so I changed it like this.

hw/vfio/migration.c +0 -0
index 4c06e3db93..70ccb706c6 100644
@@ -878,7 +878,7 @@ static void vfio_vmstate_change_prepare(void *opaque, bool running,
 static void vfio_vmstate_change(void *opaque, bool running, RunState 

state)

 {
     VFIODevice *vbasedev = opaque;
-    enum vfio_device_mig_state new_state;
+    enum vfio_device_mig_state new_state, pre_state;
     Error *local_err = NULL;
     int ret;

@@ -899,6 +899,10 @@ static void vfio_vmstate_change(void *opaque, bool running, RunState state)

          * currently does not support reporting failures.
          */
         migration_file_set_error(ret, local_err);

+
+ if (pre_state == VFIO_DEVICE_STATE_RESUMING) {
+ exit(EXIT_FAILURE);
+ }

     }
Quoted 148 lines

Thanks.

[1] https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/vfio.h#L1099

[2] Today RESUMING->STOP is done implicitly by the VFIO driver as part of RESUMING->RUNNING transition.
[3]

hw/vfio/migration-multifd.c +6 -0
index e4785031a7..66f8461f02 100644
@@ -267,6 +267,12 @@ static bool 
 vfio_load_bufs_thread_load_config(VFIODevice *vbasedev,
      ret = vfio_load_device_config_state(f_in, vbasedev);
      bql_unlock();
 
+    ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP,
+                                   VFIO_DEVICE_STATE_ERROR, errp);
+    if (ret) {
+        return false;
+    }
+
      if (ret < 0) {
          error_setg(errp, "%s: vfio_load_device_config_state() failed: 

%d",
                    vbasedev->name, ret);

hw/vfio/migration.c +2 -0
index 4c06e3db93..a707d17a5b 100644
@@ -737,6 +737,8 @@ static int vfio_load_state(QEMUFile *f, void 
 *opaque, int version_id)
          switch (data) {
          case VFIO_MIG_FLAG_DEV_CONFIG_STATE:
          {
+            Error *local_err = NULL;
+
              if (vfio_multifd_transfer_enabled(vbasedev)) {
                  error_report("%s: got DEV_CONFIG_STATE in main 

migration "
                              "channel but doing multifd transfer",

hw/vfio/migration.c +13 -1
@@ -744,7 +746,19 @@ static int vfio_load_state(QEMUFile *f, void 
 *opaque, int version_id)
                  return -EINVAL;
              }
 
-            return vfio_load_device_config_state(f, opaque);
+            ret = vfio_load_device_config_state(f, opaque);
+            if (ret) {
+                return ret;
+            }
+
+            ret = vfio_migration_set_state_or_reset(
+                vbasedev, VFIO_DEVICE_STATE_STOP, &local_err);
+            if (ret) {
+                error_report_err(local_err);
+                return ret;
+            }
+
+            return 0;
          }
          case VFIO_MIG_FLAG_DEV_SETUP_STATE:

         {

migration/migration.c +0 -0
index 10c216d25d..fd498c864d 100644
@@ -91,6 +91,7 @@ enum mig_rp_message_type {
      MIG_RP_MSG_RECV_BITMAP,  /* send recved_bitmap back to source */
      MIG_RP_MSG_RESUME_ACK,   /* tell source that we are ready to 

resume */
     MIG_RP_MSG_SWITCHOVER_ACK, /* Tell source it's OK to do switchover */
+    MIG_RP_MSG_ERROR, /* Tell source that destination encountered an error */

     MIG_RP_MSG_MAX
 };

migration/migration.c +14 -0
@@ -884,6 +885,11 @@ process_incoming_migration_co(void *opaque)
      ret = qemu_loadvm_state(mis->from_src_file);
      mis->loadvm_co = NULL;
 
+    if (ret) {
+        migrate_send_rp_error(mis);
+        error_report("SENT RP ERROR");
+    }
+
  trace_vmstate_downtime_checkpoint("dst-precopy-loadvm-completed");
 
      ps = postcopy_state_get();
@@ -1126,6 +1132,11 @@ bool migration_has_all_channels(void)
      return true;
  }
+int migrate_send_rp_error(MigrationIncomingState *mis)
+{
+    return migrate_send_rp_message(mis, MIG_RP_MSG_ERROR, 0, NULL);
+}
+
  int migrate_send_rp_switchover_ack(MigrationIncomingState *mis)
  {
      return migrate_send_rp_message(mis, MIG_RP_MSG_SWITCHOVER_ACK, 0, 
 NULL);
@@ -2614,6 +2625,10 @@ static void *source_return_path_thread(void *opaque)
              trace_source_return_path_thread_switchover_acked();
              break;
 
+        case MIG_RP_MSG_ERROR:
+            error_setg(&err, "DST indicated error");
+            goto out;
+
          default:
              break;
          }
migration/migration.h +1 -0
index 01329bf824..f11ff7a199 100644
@@ -553,6 +553,7 @@ void 
 migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
                                   char *block_name);
  void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t 
 value);
  int migrate_send_rp_switchover_ack(MigrationIncomingState *mis);
+int migrate_send_rp_error(MigrationIncomingState *mis);
 

 void dirty_bitmap_mig_before_vm_start(void);
 void dirty_bitmap_mig_cancel_outgoing(void);

Quoted 6 lines

I suggest you open an issue on :

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

with a detailed description of your environment :

Quoted 11 lines

  Host HW, Host OS, QEMU version, QEMU command line, Guest OS, etc.

A template is provided when a new issue is created.

Thanks,

C.

.

#10 lore raw
Quoted 48 lines · Kunkun Jiang

External email: Use caution opening links or attachments

Hi Avihai,

Quoted 29 lines · Avihai Horon
Quoted 18 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello,

+ Avihai

Quoted 9 lines · Kunkun Jiang

Hi all,

While testing VFIO migration, I encountered an corner scenario case:
VFIO migration will not be aborted when the vfio device of dst-vm fails to transition from RESUMING to RUNNING state in vfio_vmstate_change.

I saw the comments in the vfio_vmstate_change but I don't understand why no action is taken for this situation.

Quoted 8 lines

There is error handling in vfio_vmstate_change() :

        /*
         * Migration should be aborted in this case, but vm_state_notify()
         * currently does not support reporting failures. */
        migration_file_set_error(ret, local_err);

Quoted 3 lines

Hmm, I think this only sets the error on src. On dst we don't have
MigrationState->to_dst_file, so we end up just reporting the erro > But even if we did set it, no one is checking if there is a migration error after vm_start() is called in process_incoming_migration_bh().

Yes, that's what I mean.

Quoted 9 lines
Quoted 4 lines
Quoted 4 lines

Allowing the live migration process to continue could cause unrecoverable damage to the VM.

What do you mean by unrecoverable damage to the VM?
If RESUMING->RUNNING transition fails, would a VFIO reset recover the device and allow the VM to continue operation with damage limited only to the VFIO device?

Quoted 5 lines

I didn't express myself clearly, let me explain again.
In my test, I ran a task inside the VM that was using this vfio device. In this corner scenario, the task is aborted immediately and the VM is still running normally. I mean it is unacceptable that the task is aborted directly.

Ah I understand.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

In this case, can we directly exit the dst-vm? Through the return-path mechanism, the src-vm can continue to run.

Looking forward to your reply.

Quoted 7 lines
Quoted 5 lines

The straightforward solution, as you suggested, is to exit dst upon error in RESUMING->RUNNING transition and notify about it to src through the return-path.
However, I am not sure if failing the migration after vm_start() on dst is a bit late (as we start vCPUs and do migration_block_activate, etc.).

I think vCPUs have not started yet. vCPUs will be started only after the all callbacks of vm_state_notify are executed.

Yes, in that case need to refactor vm_prepare_start/vm_state_notify to handle errors. May be a bit involved, need to further look into that.

Thanks.

Quoted 214 lines

If there are any problems with my analysis, please point them out.

Quoted 24 lines

But I can think of another way to solve this, hopefully simpler. According to VFIO migration uAPI [1]:
  * RESUMING -> STOP
  *   Leaving RESUMING terminates a data transfer session and indicates the *   device should complete processing of the data delivered by write(). The
  *   kernel migration driver should complete the incorporation of data written
  *   to the data transfer FD into the device internal state and perform *   final validity and consistency checking of the new device state. If the
  *   user provided data is found to be incomplete, inconsistent, or otherwise
  *   invalid, the migration driver must fail the SET_STATE ioctl and *   optionally go to the ERROR state as described below.

So, IIUC, we can add an explicit RESUMING->STOP transition [2] after the device config is loaded (which is the last data the device is expected to receive).
If this transition fails, it means something was wrong with migration, and we can send src an error msg via return-path (and not continue to vm_start()).

Quoted 6 lines

Maybe this approach is less complicated than the first one, and it will also work if src VM was paused prior migration.
I already tested some POC and it seems to be working (at least with an artificial error i injected in RESUMING->STOP transition). Kunkun, can you apply the following diff [3] and check if this solves the issue?

Ok, I'll try it.

Quoted 3 lines

And in general, what do you think? Should we go with this approach or do you have other ideas?

My current approach is rather crude. As I said above, I think vcpu has not started yet, so I changed it like this.

hw/vfio/migration.c +0 -0
index 4c06e3db93..70ccb706c6 100644
@@ -878,7 +878,7 @@ static void vfio_vmstate_change_prepare(void
 *opaque, bool running,

 static void vfio_vmstate_change(void *opaque, bool running, RunState state)
 {
     VFIODevice *vbasedev = opaque;
-    enum vfio_device_mig_state new_state;
+    enum vfio_device_mig_state new_state, pre_state; Error *local_err = NULL;
     int ret;

hw/vfio/migration.c +4 -0
@@ -899,6 +899,10 @@ static void vfio_vmstate_change(void *opaque, bool
 running, RunState state)
           * currently does not support reporting failures.
           */
          migration_file_set_error(ret, local_err);
+
+        if (pre_state == VFIO_DEVICE_STATE_RESUMING) {
+            exit(EXIT_FAILURE);
+        }
      }
Quoted 152 lines

Thanks.

[1]
https://elixir.bootlin.com/linux/v6.16/source/include/uapi/linux/vfio.h#L1099

[2] Today RESUMING->STOP is done implicitly by the VFIO driver as part of RESUMING->RUNNING transition.
[3]

hw/vfio/migration-multifd.c +6 -0
index e4785031a7..66f8461f02 100644
@@ -267,6 +267,12 @@ static bool
 vfio_load_bufs_thread_load_config(VFIODevice *vbasedev,
       ret = vfio_load_device_config_state(f_in, vbasedev);
       bql_unlock();
 
+    ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP,
+                                   VFIO_DEVICE_STATE_ERROR, errp);
+    if (ret) {
+        return false;
+    }
+
       if (ret < 0) {

          error_setg(errp, "%s: vfio_load_device_config_state() failed:
%d",
                     vbasedev->name, ret);

hw/vfio/migration.c +0 -0
index 4c06e3db93..a707d17a5b 100644
@@ -737,6 +737,8 @@ static int vfio_load_state(QEMUFile *f, void
 *opaque, int version_id)
           switch (data) {

          case VFIO_MIG_FLAG_DEV_CONFIG_STATE:
          {
+            Error *local_err = NULL;
+
              if (vfio_multifd_transfer_enabled(vbasedev)) { error_report("%s: got DEV_CONFIG_STATE in main migration "
                               "channel but doing multifd transfer",

hw/vfio/migration.c +13 -1
@@ -744,7 +746,19 @@ static int vfio_load_state(QEMUFile *f, void
 *opaque, int version_id)
                   return -EINVAL;
               }
 
-            return vfio_load_device_config_state(f, opaque);
+            ret = vfio_load_device_config_state(f, opaque);
+            if (ret) {
+                return ret;
+            }
+
+            ret = vfio_migration_set_state_or_reset(
+                vbasedev, VFIO_DEVICE_STATE_STOP, &local_err);
+            if (ret) {
+                error_report_err(local_err);
+                return ret;
+            }
+
+            return 0;
           }

          case VFIO_MIG_FLAG_DEV_SETUP_STATE:
          {

migration/migration.c +0 -0
index 10c216d25d..fd498c864d 100644
@@ -91,6 +91,7 @@ enum mig_rp_message_type {

      MIG_RP_MSG_RECV_BITMAP,  /* send recved_bitmap back to source */ MIG_RP_MSG_RESUME_ACK,   /* tell source that we are ready to resume */
      MIG_RP_MSG_SWITCHOVER_ACK, /* Tell source it's OK to do switchover */ +    MIG_RP_MSG_ERROR, /* Tell source that destination encountered an error */

      MIG_RP_MSG_MAX
  };

migration/migration.c +14 -0
@@ -884,6 +885,11 @@ process_incoming_migration_co(void *opaque)
       ret = qemu_loadvm_state(mis->from_src_file);
       mis->loadvm_co = NULL;
 
+    if (ret) {
+        migrate_send_rp_error(mis);
+        error_report("SENT RP ERROR");
+    }
+
 trace_vmstate_downtime_checkpoint("dst-precopy-loadvm-completed");
 
       ps = postcopy_state_get();
@@ -1126,6 +1132,11 @@ bool migration_has_all_channels(void)
       return true;
   }
+int migrate_send_rp_error(MigrationIncomingState *mis)
+{
+    return migrate_send_rp_message(mis, MIG_RP_MSG_ERROR, 0, NULL);
+}
+
   int migrate_send_rp_switchover_ack(MigrationIncomingState *mis)
   {
       return migrate_send_rp_message(mis, MIG_RP_MSG_SWITCHOVER_ACK, 0,
 NULL);
@@ -2614,6 +2625,10 @@ static void *source_return_path_thread(void 
 *opaque)
 trace_source_return_path_thread_switchover_acked();
               break;
 
+        case MIG_RP_MSG_ERROR:
+            error_setg(&err, "DST indicated error");
+            goto out;
+

          default:
              break;
          }

migration/migration.h +0 -0
index 01329bf824..f11ff7a199 100644
@@ -553,6 +553,7 @@ void
 migrate_send_rp_recv_bitmap(MigrationIncomingState *mis,
                                    char *block_name);

  void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value);
  int migrate_send_rp_switchover_ack(MigrationIncomingState *mis); +int migrate_send_rp_error(MigrationIncomingState *mis);

  void dirty_bitmap_mig_before_vm_start(void);
  void dirty_bitmap_mig_cancel_outgoing(void);

Quoted 6 lines

I suggest you open an issue on :

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

with a detailed description of your environment :

Quoted 11 lines

  Host HW, Host OS, QEMU version, QEMU command line, Guest OS, etc.

A template is provided when a new issue is created.

Thanks,

C.

.