Skip to contentSkip to conversation

vfio/migration: Send VFIO_MIGRATION event before PRE_COPY_P2P transition

PATCH Superseded Avihai Horon 3 participants 4 messages Feb 1 – Feb 2

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 vfio/migration: Send VFIO_MIGRATION event before PRE_COPY_P2P transition

The VFIO_MIGRATION event notifies users when a VFIO device transitions to a new state.

One use case for this event is to prevent timeouts for RDMA connections to the migrated device. In this case, an external management application (not libvirt) consumes the events and disables the RDMA timeout mechanism when receiving the event for PRE_COPY_P2P state, which indicates that the device is non-responsive.

This is essential because RDMA connections typically have very low timeouts (tens of milliseconds), which can be far below migration downtime.

However, under heavy resource utilization, the device transition to PRE_COPY_P2P can take hundreds of milliseconds to complete. Since the VFIO_MIGRATION event is currently sent only after the transition completes, it arrives too late, after RDMA connections have already timed out.

To address this, send an additional "prepare" event immediately before initiating the PRE_COPY_P2P transition. This guarantees timely event delivery regardless of how long the actual state transition takes.

---
Changes from v1 (https://lore.kernel.org/qemu-devel/[email protected]/):
* Removed VFIO_MIGRATION_PREPARE event and instead added a new PREPARE_PRE_COPY_P2P state which is sent before PRE_COPY_P2P
transition
* Added details to commit message

---
 qapi/vfio.json      | 13 +++++++++++--
 hw/vfio/migration.c | 20 +++++++++++++-------
 2 files changed, 24 insertions(+), 9 deletions(-)
qapi/vfio.json +11 -2
index a1a9c5b673..2de87dc215 100644
@@ -11,7 +11,13 @@
 ##
 # @QapiVfioMigrationState:
 #
-# An enumeration of the VFIO device migration states.
+# An enumeration of the VFIO device migration states.  In addition to
+# the regular states, there are prepare states (with 'prepare' prefix)
+# which indicate that the device is just about to transition to the
+# corresponding state.  Note that seeing a prepare state for state X
+# doesn't guarantee that the next state will be X, as the state
+# transition can fail and the device may transition to a different
+# state instead.
 #
 # @stop: The device is stopped.
 #
@@ -32,11 +38,14 @@
 #     tracking its internal state and its internal state is available
 #     for reading.
 #
+# @prepare-pre-copy-p2p: The device is just about to move to
+#     pre-copy-p2p state.  (since 11.0)
+#
 # Since: 9.1
 ##
 { 'enum': 'QapiVfioMigrationState',
   'data': [ 'stop', 'running', 'stop-copy', 'resuming', 'running-p2p',
-            'pre-copy', 'pre-copy-p2p' ] }
+            'pre-copy', 'pre-copy-p2p', 'prepare-pre-copy-p2p' ] }
 
 ##
 # @VFIO_MIGRATION:
hw/vfio/migration.c +13 -7
index b4695030c7..3b215eb4ff 100644
@@ -68,7 +68,7 @@ static const char *mig_state_to_str(enum vfio_device_mig_state state)
 }
 
 static QapiVfioMigrationState
-mig_state_to_qapi_state(enum vfio_device_mig_state state)
+mig_state_to_qapi_state(enum vfio_device_mig_state state, bool prep)
 {
     switch (state) {
     case VFIO_DEVICE_STATE_STOP:
@@ -84,15 +84,17 @@ mig_state_to_qapi_state(enum vfio_device_mig_state state)
     case VFIO_DEVICE_STATE_PRE_COPY:
         return QAPI_VFIO_MIGRATION_STATE_PRE_COPY;
     case VFIO_DEVICE_STATE_PRE_COPY_P2P:
-        return QAPI_VFIO_MIGRATION_STATE_PRE_COPY_P2P;
+        return prep ? QAPI_VFIO_MIGRATION_STATE_PREPARE_PRE_COPY_P2P :
+                      QAPI_VFIO_MIGRATION_STATE_PRE_COPY_P2P;
     default:
         g_assert_not_reached();
     }
 }
 
-static void vfio_migration_send_event(VFIODevice *vbasedev)
+static void vfio_migration_send_event(VFIODevice *vbasedev,
+                                      enum vfio_device_mig_state state,
+                                      bool prep)
 {
-    VFIOMigration *migration = vbasedev->migration;
     DeviceState *dev = vbasedev->dev;
     g_autofree char *qom_path = NULL;
     Object *obj;
@@ -106,8 +108,8 @@ static void vfio_migration_send_event(VFIODevice *vbasedev)
     g_assert(obj);
     qom_path = object_get_canonical_path(obj);
 
-    qapi_event_send_vfio_migration(
-        dev->id, qom_path, mig_state_to_qapi_state(migration->device_state));
+    qapi_event_send_vfio_migration(dev->id, qom_path,
+                                   mig_state_to_qapi_state(state, prep));
 }
 
 static void vfio_migration_set_device_state(VFIODevice *vbasedev,
@@ -119,7 +121,7 @@ static void vfio_migration_set_device_state(VFIODevice *vbasedev,
                                           mig_state_to_str(state));
 
     migration->device_state = state;
-    vfio_migration_send_event(vbasedev);
+    vfio_migration_send_event(vbasedev, state, false);
 }
 
 int vfio_migration_set_state(VFIODevice *vbasedev,
@@ -146,6 +148,10 @@ int vfio_migration_set_state(VFIODevice *vbasedev,
         return 0;
     }
 
+    if (new_state == VFIO_DEVICE_STATE_PRE_COPY_P2P) {
+        vfio_migration_send_event(vbasedev, new_state, true);
+    }
+
     feature->argsz = sizeof(buf);
     feature->flags =
         VFIO_DEVICE_FEATURE_SET | VFIO_DEVICE_FEATURE_MIG_DEVICE_STATE;
Signature · 1 line
2.40.1
#1 lore raw

Hello Avihai,

Quoted 55 lines · Avihai Horon

The VFIO_MIGRATION event notifies users when a VFIO device transitions to a new state.

One use case for this event is to prevent timeouts for RDMA connections to the migrated device. In this case, an external management application (not libvirt) consumes the events and disables the RDMA timeout mechanism when receiving the event for PRE_COPY_P2P state, which indicates that the device is non-responsive.

This is essential because RDMA connections typically have very low timeouts (tens of milliseconds), which can be far below migration downtime.

However, under heavy resource utilization, the device transition to PRE_COPY_P2P can take hundreds of milliseconds to complete. Since the VFIO_MIGRATION event is currently sent only after the transition completes, it arrives too late, after RDMA connections have already timed out.

To address this, send an additional "prepare" event immediately before initiating the PRE_COPY_P2P transition. This guarantees timely event delivery regardless of how long the actual state transition takes.

---
Changes from v1 (https://lore.kernel.org/qemu-devel/[email protected]/):
* Removed VFIO_MIGRATION_PREPARE event and instead added a new PREPARE_PRE_COPY_P2P state which is sent before PRE_COPY_P2P
transition
* Added details to commit message

---
  qapi/vfio.json      | 13 +++++++++++--
  hw/vfio/migration.c | 20 +++++++++++++-------
  2 files changed, 24 insertions(+), 9 deletions(-)
qapi/vfio.json +7 -1
index a1a9c5b673..2de87dc215 100644
@@ -11,7 +11,13 @@
  ##
  # @QapiVfioMigrationState:
  #
-# An enumeration of the VFIO device migration states.
+# An enumeration of the VFIO device migration states.  In addition to
+# the regular states, there are prepare states (with 'prepare' prefix)
+# which indicate that the device is just about to transition to the
+# corresponding state.  Note that seeing a prepare state for state X
+# doesn't guarantee that the next state will be X, as the state
+# transition can fail and the device may transition to a different
+# state instead.
  #
  # @stop: The device is stopped.
  #

qapi/vfio.json line 41

Quoted 4 lines
qapi/vfio.json +1 -0
@@ -32,11 +38,14 @@
  #     tracking its internal state and its internal state is available
  #     for reading.
  #
+# @prepare-pre-copy-p2p: The device is just about to move to

Could you please rename to "pre-copy-p2p-prepare" ?

Peter, Fabiano,

Any preferences in the naming ? I would like to avoid "pre-pre-copy-p2p" but it is what this event looks like.

Quoted 30 lines
qapi/vfio.json +3 -1
+#     pre-copy-p2p state.  (since 11.0)
+#
  # Since: 9.1
  ##
  { 'enum': 'QapiVfioMigrationState',
    'data': [ 'stop', 'running', 'stop-copy', 'resuming', 'running-p2p',
-            'pre-copy', 'pre-copy-p2p' ] }
+            'pre-copy', 'pre-copy-p2p', 'prepare-pre-copy-p2p' ] }
 
  ##
  # @VFIO_MIGRATION:
hw/vfio/migration.c +2 -2
index b4695030c7..3b215eb4ff 100644
@@ -68,7 +68,7 @@ static const char *mig_state_to_str(enum vfio_device_mig_state state)
  }
 
  static QapiVfioMigrationState
-mig_state_to_qapi_state(enum vfio_device_mig_state state)
+mig_state_to_qapi_state(enum vfio_device_mig_state state, bool prep)
  {
      switch (state) {
      case VFIO_DEVICE_STATE_STOP:
@@ -84,15 +84,17 @@ mig_state_to_qapi_state(enum vfio_device_mig_state state)
      case VFIO_DEVICE_STATE_PRE_COPY:
          return QAPI_VFIO_MIGRATION_STATE_PRE_COPY;
      case VFIO_DEVICE_STATE_PRE_COPY_P2P:
-        return QAPI_VFIO_MIGRATION_STATE_PRE_COPY_P2P;
+        return prep ? QAPI_VFIO_MIGRATION_STATE_PREPARE_PRE_COPY_P2P :

hw/vfio/migration.c mig_state_to_qapi_state(enum vfio_device_mig_state state) · line 96

Quoted 10 lines
hw/vfio/migration.c +4 -1
+                      QAPI_VFIO_MIGRATION_STATE_PRE_COPY_P2P;
      default:
          g_assert_not_reached();
      }
  }
 
-static void vfio_migration_send_event(VFIODevice *vbasedev)
+static void vfio_migration_send_event(VFIODevice *vbasedev,
+                                      enum vfio_device_mig_state state,
+                                      bool prep) 

prep -> prepare

Quoted 26 lines
hw/vfio/migration.c +3 -4
  {
-    VFIOMigration *migration = vbasedev->migration;
      DeviceState *dev = vbasedev->dev;
      g_autofree char *qom_path = NULL;
      Object *obj;
@@ -106,8 +108,8 @@ static void vfio_migration_send_event(VFIODevice *vbasedev)
      g_assert(obj);
      qom_path = object_get_canonical_path(obj);
 
-    qapi_event_send_vfio_migration(
-        dev->id, qom_path, mig_state_to_qapi_state(migration->device_state));
+    qapi_event_send_vfio_migration(dev->id, qom_path,
+                                   mig_state_to_qapi_state(state, prep));
  }
 
  static void vfio_migration_set_device_state(VFIODevice *vbasedev,
@@ -119,7 +121,7 @@ static void vfio_migration_set_device_state(VFIODevice *vbasedev,
                                            mig_state_to_str(state));
 
      migration->device_state = state;
-    vfio_migration_send_event(vbasedev);
+    vfio_migration_send_event(vbasedev, state, false);
  }
 
  int vfio_migration_set_state(VFIODevice *vbasedev,

hw/vfio/migration.c int vfio_migration_set_state(VFIODevice *vbasedev, · line 150

Quoted 3 lines
hw/vfio/migration.c +0 -0
@@ -146,6 +148,10 @@ int vfio_migration_set_state(VFIODevice *vbasedev,
          return 0;
      }
 

A short comment (extracted from the commit log) here would be good.

hw/vfio/migration.c int vfio_migration_set_state(VFIODevice *vbasedev, · line 152

Quoted 2 lines
hw/vfio/migration.c +2 -0
+    if (new_state == VFIO_DEVICE_STATE_PRE_COPY_P2P) {
+        vfio_migration_send_event(vbasedev, new_state, true);

Please be explicit :

           vfio_migration_send_event(vbasedev, VFIO_DEVICE_STATE_PRE_COPY_P2P, true);

Thanks,

C.

Quoted 5 lines
hw/vfio/migration.c +2 -0
+    }
+
      feature->argsz = sizeof(buf);
      feature->flags =
          VFIO_DEVICE_FEATURE_SET | VFIO_DEVICE_FEATURE_MIG_DEVICE_STATE;
#2 lore raw
Quoted 8 lines · Cédric Le Goater
Quoted 1 line

+# @prepare-pre-copy-p2p: The device is just about to move to

Could you please rename to "pre-copy-p2p-prepare" ?

Peter, Fabiano,

Any preferences in the naming ? I would like to avoid "pre-pre-copy-p2p" but it is what this event looks like.

I'd also vote for pre-copy-p2p-prepare, as it also keeps making "pre-copy-" to be the prefix. No strong feelings.

Thanks,

Signature · 1 line
Peter Xu
#3 lore raw
In reply to Cédric Le Goater
Quoted 59 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello Avihai,

Quoted 53 lines · Avihai Horon

The VFIO_MIGRATION event notifies users when a VFIO device transitions to a new state.

One use case for this event is to prevent timeouts for RDMA connections to the migrated device. In this case, an external management application (not libvirt) consumes the events and disables the RDMA timeout mechanism when receiving the event for PRE_COPY_P2P state, which indicates that the device is non-responsive.

This is essential because RDMA connections typically have very low timeouts (tens of milliseconds), which can be far below migration downtime.

However, under heavy resource utilization, the device transition to PRE_COPY_P2P can take hundreds of milliseconds to complete. Since the VFIO_MIGRATION event is currently sent only after the transition completes, it arrives too late, after RDMA connections have already timed out.

To address this, send an additional "prepare" event immediately before initiating the PRE_COPY_P2P transition. This guarantees timely event delivery regardless of how long the actual state transition takes.

---
Changes from v1 (https://lore.kernel.org/qemu-devel/[email protected]/):
* Removed VFIO_MIGRATION_PREPARE event and instead added a new PREPARE_PRE_COPY_P2P state which is sent before PRE_COPY_P2P
   transition
* Added details to commit message
---
  qapi/vfio.json      | 13 +++++++++++--
  hw/vfio/migration.c | 20 +++++++++++++-------

  2 files changed, 24 insertions(+), 9 deletions(-)
qapi/vfio.json +7 -1
index a1a9c5b673..2de87dc215 100644
@@ -11,7 +11,13 @@
   ##
   # @QapiVfioMigrationState:
   #
-# An enumeration of the VFIO device migration states.
+# An enumeration of the VFIO device migration states.  In addition to
+# the regular states, there are prepare states (with 'prepare' prefix)
+# which indicate that the device is just about to transition to the
+# corresponding state.  Note that seeing a prepare state for state X
+# doesn't guarantee that the next state will be X, as the state
+# transition can fail and the device may transition to a different
+# state instead.
   #
   # @stop: The device is stopped.
Quoted 8 lines

qapi/vfio.json line 41

Quoted 6 lines
qapi/vfio.json +1 -0
   #
@@ -32,11 +38,14 @@
   #     tracking its internal state and its internal state is available
   #     for reading.
   #
+# @prepare-pre-copy-p2p: The device is just about to move to

Could you please rename to "pre-copy-p2p-prepare" ?

Ack.

Quoted 45 lines

Peter, Fabiano,

Any preferences in the naming ? I would like to avoid "pre-pre-copy-p2p" but it is what this event looks like.

Quoted 38 lines
qapi/vfio.json +3 -1
+#     pre-copy-p2p state.  (since 11.0)
+#
   # Since: 9.1
   ##
   { 'enum': 'QapiVfioMigrationState',
     'data': [ 'stop', 'running', 'stop-copy', 'resuming', 'running-p2p',
-            'pre-copy', 'pre-copy-p2p' ] }
+            'pre-copy', 'pre-copy-p2p', 'prepare-pre-copy-p2p' ] }
 
   ##
   # @VFIO_MIGRATION:
hw/vfio/migration.c +0 -0
index b4695030c7..3b215eb4ff 100644
@@ -68,7 +68,7 @@ static const char *mig_state_to_str(enum 
 vfio_device_mig_state state)
   }
 

  static QapiVfioMigrationState

hw/vfio/migration.c +1 -1
-mig_state_to_qapi_state(enum vfio_device_mig_state state)
+mig_state_to_qapi_state(enum vfio_device_mig_state state, bool prep)

  {
      switch (state) {
      case VFIO_DEVICE_STATE_STOP:

hw/vfio/migration.c +0 -0
@@ -84,15 +84,17 @@ mig_state_to_qapi_state(enum 
 vfio_device_mig_state state)

      case VFIO_DEVICE_STATE_PRE_COPY:
          return QAPI_VFIO_MIGRATION_STATE_PRE_COPY;
      case VFIO_DEVICE_STATE_PRE_COPY_P2P:
-        return QAPI_VFIO_MIGRATION_STATE_PRE_COPY_P2P;
+        return prep ? QAPI_VFIO_MIGRATION_STATE_PREPARE_PRE_COPY_P2P :
+                      QAPI_VFIO_MIGRATION_STATE_PRE_COPY_P2P; default:
          g_assert_not_reached();
      }
  }

Quoted 6 lines

hw/vfio/migration.c mig_state_to_qapi_state(enum vfio_device_mig_state state) · line 96

Quoted 4 lines
hw/vfio/migration.c +3 -1
-static void vfio_migration_send_event(VFIODevice *vbasedev)
+static void vfio_migration_send_event(VFIODevice *vbasedev,
+                                      enum vfio_device_mig_state state,
+                                      bool prep)

prep -> prepare

Ack.

Quoted 28 lines
Quoted 28 lines

  {
-    VFIOMigration *migration = vbasedev->migration; DeviceState *dev = vbasedev->dev; g_autofree char *qom_path = NULL; Object *obj;

hw/vfio/migration.c +2 -2
@@ -106,8 +108,8 @@ static void vfio_migration_send_event(VFIODevice 
 *vbasedev)
       g_assert(obj);
       qom_path = object_get_canonical_path(obj);
 
-    qapi_event_send_vfio_migration(
-        dev->id, qom_path, 
 mig_state_to_qapi_state(migration->device_state));
+    qapi_event_send_vfio_migration(dev->id, qom_path,
+ mig_state_to_qapi_state(state, prep));
   }

  static void vfio_migration_set_device_state(VFIODevice *vbasedev,

hw/vfio/migration.c +1 -1
@@ -119,7 +121,7 @@ static void 
 vfio_migration_set_device_state(VFIODevice *vbasedev,
 mig_state_to_str(state));
 
       migration->device_state = state;
-    vfio_migration_send_event(vbasedev);
+    vfio_migration_send_event(vbasedev, state, false);
   }
 
Quoted 7 lines

hw/vfio/migration.c int vfio_migration_set_state(VFIODevice *vbasedev, · line 150

Quoted 5 lines

  int vfio_migration_set_state(VFIODevice *vbasedev,

hw/vfio/migration.c +0 -0
@@ -146,6 +148,10 @@ int vfio_migration_set_state(VFIODevice *vbasedev,
           return 0;
       }
 

A short comment (extracted from the commit log) here would be good.

Ack.

Quoted 7 lines

hw/vfio/migration.c int vfio_migration_set_state(VFIODevice *vbasedev, · line 152

Quoted 4 lines
hw/vfio/migration.c +2 -0
+    if (new_state == VFIO_DEVICE_STATE_PRE_COPY_P2P) {
+        vfio_migration_send_event(vbasedev, new_state, true);

Please be explicit :
           vfio_migration_send_event(vbasedev, VFIO_DEVICE_STATE_PRE_COPY_P2P, true);

Ack.

Thanks.