Skip to contentSkip to conversation

Trivial patches from multifd device state transfer support patch set

PATCH Superseded Maciej S. Szmigiero 4 participants 18 messages Oct 29, 2024 – Nov 4, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Trivial patches from multifd device state transfer support patch set

From: "Maciej S. Szmigiero" <[email protected]>

A new version of the multifd device state transfer support with VFIO consumer patch set is being prepared, the previous version and the associated discussion is available here:
https://lore.kernel.org/qemu-devel/[email protected]/

This new version was originally targeting QEMU 9.2 but such schedule proved to be too optimistic due to sheer number of invasive changes/rework required, especially with respect to the VFIO internal threads management and their synchronization with the migration core.

In addition to these changes, recently merged commit 3b5948f808e3 ("vfio/migration: Report only stop-copy size in vfio_state_pending_exact()") seems to have uncovered a race between multifd RAM and device state transfers:
RAM transfer sender finishes the multifd stream with a SYNC in ram_save_complete() but the multifd receive channels are only released from this SYNC after the migration is wholly complete in process_incoming_migration_bh().

The above causes problems if the multifd channels need to still be running after the RAM transfer is completed, for example because there is still remaining device state to be transferred.

Since QEMU 9.2 code freeze is coming I've separated small uncontroversial commits from that WiP main patch set here, some of which were already reviewed during previous main patch set iterations.

This way at least future code conflicts can be reduced and the amount of patches that need to be carried in the future versions of the main patch set is reduced.

Maciej S. Szmigiero (4):
vfio/migration: Add save_{iterate,complete_precopy}_started trace
events
migration/ram: Add load start trace event migration/multifd: Zero p->flags before starting filling a packet
migration: Document the BQL behavior of load SaveVMHandlers

 hw/vfio/migration.c           | 13 +++++++++++++
 hw/vfio/trace-events          |  3 +++
 include/hw/vfio/vfio-common.h |  3 +++
 include/migration/register.h  |  4 ++++
 migration/multifd.c           |  2 +-
 migration/ram.c               |  1 +
 migration/trace-events        |  1 +
 7 files changed, 26 insertions(+), 1 deletion(-)
#1 lore raw

PATCH 1/4 vfio/migration: Add save_{iterate, complete_precopy}_started trace events

From: "Maciej S. Szmigiero" <[email protected]>

This way both the start and end points of migrating a particular VFIO device are known.

Add also a vfio_save_iterate_empty_hit trace event so it is known when there's no more data to send for that device.

---
 hw/vfio/migration.c           | 13 +++++++++++++
 hw/vfio/trace-events          |  3 +++
 include/hw/vfio/vfio-common.h |  3 +++
 3 files changed, 19 insertions(+)
hw/vfio/migration.c +13 -0
index 992dc3b10257..1b1ddf527d69 100644
@@ -472,6 +472,9 @@ static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp)
         return -ENOMEM;
     }
 
+    migration->save_iterate_started = false;
+    migration->save_iterate_empty_hit = false;
+
     if (vfio_precopy_supported(vbasedev)) {
         switch (migration->device_state) {
         case VFIO_DEVICE_STATE_RUNNING:
@@ -602,9 +605,17 @@ static int vfio_save_iterate(QEMUFile *f, void *opaque)
     VFIOMigration *migration = vbasedev->migration;
     ssize_t data_size;
 
+    if (!migration->save_iterate_started) {
+        trace_vfio_save_iterate_started(vbasedev->name);
+        migration->save_iterate_started = true;
+    }
+
     data_size = vfio_save_block(f, migration);
     if (data_size < 0) {
         return data_size;
+    } else if (data_size == 0 && !migration->save_iterate_empty_hit) {
+        trace_vfio_save_iterate_empty_hit(vbasedev->name);
+        migration->save_iterate_empty_hit = true;
     }
 
     vfio_update_estimated_pending_data(migration, data_size);
@@ -630,6 +641,8 @@ static int vfio_save_complete_precopy(QEMUFile *f, void *opaque)
     int ret;
     Error *local_err = NULL;
 
+    trace_vfio_save_complete_precopy_started(vbasedev->name);
+
     /* We reach here with device state STOP or STOP_COPY only */
     ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP_COPY,
                                    VFIO_DEVICE_STATE_STOP, &local_err);
hw/vfio/trace-events +3 -0
index 29789e8d276d..e58deab232ed 100644
@@ -159,8 +159,11 @@ vfio_migration_state_notifier(const char *name, int state) " (%s) state %d"
 vfio_save_block(const char *name, int data_size) " (%s) data_size %d"
 vfio_save_cleanup(const char *name) " (%s)"
 vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d"
+vfio_save_complete_precopy_started(const char *name) " (%s)"
 vfio_save_device_config_state(const char *name) " (%s)"
 vfio_save_iterate(const char *name, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy initial size %"PRIu64" precopy dirty size %"PRIu64
+vfio_save_iterate_empty_hit(const char *name) " (%s)"
+vfio_save_iterate_started(const char *name) " (%s)"
 vfio_save_setup(const char *name, uint64_t data_buffer_size) " (%s) data buffer size %"PRIu64
 vfio_state_pending_estimate(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy %"PRIu64" postcopy %"PRIu64" precopy initial size %"PRIu64" precopy dirty size %"PRIu64
 vfio_state_pending_exact(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t stopcopy_size, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy %"PRIu64" postcopy %"PRIu64" stopcopy size %"PRIu64" precopy initial size %"PRIu64" precopy dirty size %"PRIu64
include/hw/vfio/vfio-common.h +3 -0
index fed499b199f0..997ee5af2d5b 100644
@@ -73,6 +73,9 @@ typedef struct VFIOMigration {
     uint64_t precopy_init_size;
     uint64_t precopy_dirty_size;
     bool initial_data_sent;
+
+    bool save_iterate_started;
+    bool save_iterate_empty_hit;
 } VFIOMigration;
 
 struct VFIOGroup;
#2 lore raw

Re: [PATCH 1/4] vfio/migration: Add save_{iterate,complete_precopy}_started trace events

Hi Maciej,

Quoted 38 lines · Maciej S. Szmigiero

External email: Use caution opening links or attachments

From: "Maciej S. Szmigiero" <[email protected]>

This way both the start and end points of migrating a particular VFIO device are known.

Add also a vfio_save_iterate_empty_hit trace event so it is known when there's no more data to send for that device.

---
  hw/vfio/migration.c           | 13 +++++++++++++
  hw/vfio/trace-events          |  3 +++
  include/hw/vfio/vfio-common.h |  3 +++
  3 files changed, 19 insertions(+)
hw/vfio/migration.c +5 -0
index 992dc3b10257..1b1ddf527d69 100644
@@ -472,6 +472,9 @@ static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp)
          return -ENOMEM;
      }
 
+    migration->save_iterate_started = false;
+    migration->save_iterate_empty_hit = false;
+
      if (vfio_precopy_supported(vbasedev)) {
          switch (migration->device_state) {
          case VFIO_DEVICE_STATE_RUNNING:
@@ -602,9 +605,17 @@ static int vfio_save_iterate(QEMUFile *f, void *opaque)
      VFIOMigration *migration = vbasedev->migration;
      ssize_t data_size;
 
+    if (!migration->save_iterate_started) {
+        trace_vfio_save_iterate_started(vbasedev->name);

hw/vfio/migration.c static int vfio_save_iterate(QEMUFile *f, void *opaque) · line 619

Quoted 10 lines
hw/vfio/migration.c +6 -0
+        migration->save_iterate_started = true;
+    }
+
      data_size = vfio_save_block(f, migration);
      if (data_size < 0) {
          return data_size;
+    } else if (data_size == 0 && !migration->save_iterate_empty_hit) {
+        trace_vfio_save_iterate_empty_hit(vbasedev->name);
+        migration->save_iterate_empty_hit = true;
      }

Can we instead use trace_vfio_save_iterate to understand if the device reached 0?

In any case, I think the above could fit better in vfio_save_block(), where ENOMSG indicates that the device has no more data to send during pre-copy phase:

...
if (data_size < 0) {
    /*
     * Pre-copy emptied all the device state for now. For more information,
     * please refer to the Linux kernel VFIO uAPI.
     */
    if (errno == ENOMSG) {
trace_vfio_save_iterate_empty_hit(vbasedev->name)              <--------------- move it here return 0;
    }

    return -errno;
}
...

If you move the trace there, maybe renaming it to trace_vfio_precopy_empty_hit() will be more accurate? And trying to avoid adding the extra VFIOMigration->save_iterate_empty_hit flag, can we simply trace it every time?

hw/vfio/migration.c static int vfio_save_complete_precopy(QEMUFile *f, void *opaque) · line 644

Quoted 7 lines
hw/vfio/migration.c +1 -0
      vfio_update_estimated_pending_data(migration, data_size);
@@ -630,6 +641,8 @@ static int vfio_save_complete_precopy(QEMUFile *f, void *opaque)
      int ret;
      Error *local_err = NULL;
 
+    trace_vfio_save_complete_precopy_started(vbasedev->name);

I assume this trace is used to measure how long it takes for vfio_save_complete_precopy() to run? If so, can we use trace_vmstate_downtime_save to achieve the same goal?

Thanks.

Quoted 34 lines
hw/vfio/migration.c +1 -0
+
      /* We reach here with device state STOP or STOP_COPY only */
      ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP_COPY,
                                     VFIO_DEVICE_STATE_STOP, &local_err);
hw/vfio/trace-events +3 -0
index 29789e8d276d..e58deab232ed 100644
@@ -159,8 +159,11 @@ vfio_migration_state_notifier(const char *name, int state) " (%s) state %d"
  vfio_save_block(const char *name, int data_size) " (%s) data_size %d"
  vfio_save_cleanup(const char *name) " (%s)"
  vfio_save_complete_precopy(const char *name, int ret) " (%s) ret %d"
+vfio_save_complete_precopy_started(const char *name) " (%s)"
  vfio_save_device_config_state(const char *name) " (%s)"
  vfio_save_iterate(const char *name, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy initial size %"PRIu64" precopy dirty size %"PRIu64
+vfio_save_iterate_empty_hit(const char *name) " (%s)"
+vfio_save_iterate_started(const char *name) " (%s)"
  vfio_save_setup(const char *name, uint64_t data_buffer_size) " (%s) data buffer size %"PRIu64
  vfio_state_pending_estimate(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy %"PRIu64" postcopy %"PRIu64" precopy initial size %"PRIu64" precopy dirty size %"PRIu64
  vfio_state_pending_exact(const char *name, uint64_t precopy, uint64_t postcopy, uint64_t stopcopy_size, uint64_t precopy_init_size, uint64_t precopy_dirty_size) " (%s) precopy %"PRIu64" postcopy %"PRIu64" stopcopy size %"PRIu64" precopy initial size %"PRIu64" precopy dirty size %"PRIu64
include/hw/vfio/vfio-common.h +3 -0
index fed499b199f0..997ee5af2d5b 100644
@@ -73,6 +73,9 @@ typedef struct VFIOMigration {
      uint64_t precopy_init_size;
      uint64_t precopy_dirty_size;
      bool initial_data_sent;
+
+    bool save_iterate_started;
+    bool save_iterate_empty_hit;
  } VFIOMigration;
 
  struct VFIOGroup;
#3 lore raw

Re: [PATCH 1/4] vfio/migration: Add save_{iterate,complete_precopy}_started trace events

Hi Avihai,

Quoted 39 lines · Avihai Horon

Hi Maciej,

Quoted 36 lines · Maciej S. Szmigiero

External email: Use caution opening links or attachments

From: "Maciej S. Szmigiero" <[email protected]>

This way both the start and end points of migrating a particular VFIO device are known.

Add also a vfio_save_iterate_empty_hit trace event so it is known when there's no more data to send for that device.

---
  hw/vfio/migration.c           | 13 +++++++++++++ hw/vfio/trace-events          |  3 +++ include/hw/vfio/vfio-common.h |  3 +++

  3 files changed, 19 insertions(+)
hw/vfio/migration.c +3 -0
index 992dc3b10257..1b1ddf527d69 100644
@@ -472,6 +472,9 @@ static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp)
           return -ENOMEM;
       }
 
+    migration->save_iterate_started = false;
+    migration->save_iterate_empty_hit = false;
+
       if (vfio_precopy_supported(vbasedev)) {
           switch (migration->device_state) {

          case VFIO_DEVICE_STATE_RUNNING:

hw/vfio/migration.c +0 -0
@@ -602,9 +605,17 @@ static int vfio_save_iterate(QEMUFile *f, void *opaque)
       VFIOMigration *migration = vbasedev->migration;
       ssize_t data_size;
 
Quoted 14 lines

hw/vfio/migration.c static int vfio_save_iterate(QEMUFile *f, void *opaque) · line 619

Quoted 12 lines
hw/vfio/migration.c +8 -0
+    if (!migration->save_iterate_started) {
+        trace_vfio_save_iterate_started(vbasedev->name);
+        migration->save_iterate_started = true;
+    }
+
       data_size = vfio_save_block(f, migration);
       if (data_size < 0) {
           return data_size;
+    } else if (data_size == 0 && !migration->save_iterate_empty_hit) {
+        trace_vfio_save_iterate_empty_hit(vbasedev->name);
+        migration->save_iterate_empty_hit = true;
       }

Can we instead use trace_vfio_save_iterate to understand if the device reached 0?

AFAIK there's not way to filter trace events by their parameters, like only logging vfio_save_iterate trace event if both parameters are zero.

It means that vfio_save_iterate has to be enabled unconditionally to serve as a replacement for vfio_save_iterate_empty_hit, which could result in it being logged/emitted many extra times (with non-zero parameters).

Because of that I think having a dedicated trace event for such occasion makes sense (it is also easily grep-able).

Quoted 13 lines

In any case, I think the above could fit better in vfio_save_block(), where ENOMSG indicates that the device has no more data to send during pre-copy phase:

...
if (data_size < 0) {
    /*
     * Pre-copy emptied all the device state for now. For more information,
     * please refer to the Linux kernel VFIO uAPI.
     */
    if (errno == ENOMSG) {
trace_vfio_save_iterate_empty_hit(vbasedev->name) <--------------- move it here
        return 0;
    }

Quoted 5 lines

    return -errno;
}
...

If you move the trace there, maybe renaming it to trace_vfio_precopy_empty_hit() will be more accurate?

This move and rename seems sensible to me.

Quoted 1 line

And trying to avoid adding the extra VFIOMigration->save_iterate_empty_hit flag, can we simply trace it every time?

Will have to do some tests to be sure but if there's possibility that we get ENOMSG many times then obviously we don't want to flood logs with this trace event in this case - we want to only log the "data present" -> "data not present" edge/change.

Quoted 9 lines

hw/vfio/migration.c static int vfio_save_complete_precopy(QEMUFile *f, void *opaque) · line 644

Quoted 7 lines
hw/vfio/migration.c +1 -0
       vfio_update_estimated_pending_data(migration, data_size);
@@ -630,6 +641,8 @@ static int vfio_save_complete_precopy(QEMUFile *f, void *opaque)
       int ret;
       Error *local_err = NULL;
 
+    trace_vfio_save_complete_precopy_started(vbasedev->name);

I assume this trace is used to measure how long it takes for vfio_save_complete_precopy() to run? If so, can we use trace_vmstate_downtime_save to achieve the same goal?

With an appropriate filtering I guess it could be used to extract the same data, although explicit VFIO trace point makes it easier to just look at these traces directly (less noise from other devices).

But at the same time trace_vfio_save_complete_precopy already exists and by this metric it would also be unnecessary.

Quoted 1 line

Thanks.

Thanks,
Maciej

#4 lore raw

Re: [PATCH 1/4] vfio/migration: Add save_{iterate,complete_precopy}_started trace events

Quoted 28 lines · Maciej S. Szmigiero

Hi Avihai,

Quoted 25 lines · Avihai Horon

Hi Maciej,

Quoted 22 lines · Maciej S. Szmigiero

External email: Use caution opening links or attachments

From: "Maciej S. Szmigiero" <[email protected]>

This way both the start and end points of migrating a particular VFIO device are known.

Add also a vfio_save_iterate_empty_hit trace event so it is known when there's no more data to send for that device.

---
  hw/vfio/migration.c           | 13 +++++++++++++ hw/vfio/trace-events          |  3 +++ include/hw/vfio/vfio-common.h |  3 +++

  3 files changed, 19 insertions(+)
hw/vfio/migration.c +0 -0
index 992dc3b10257..1b1ddf527d69 100644

(..)

Quoted 19 lines
Quoted 13 lines

In any case, I think the above could fit better in vfio_save_block(), where ENOMSG indicates that the device has no more data to send during pre-copy phase:

...
if (data_size < 0) {
     /*
      * Pre-copy emptied all the device state for now. For more information,
      * please refer to the Linux kernel VFIO uAPI.
      */
     if (errno == ENOMSG) { trace_vfio_save_iterate_empty_hit(vbasedev->name) <--------------- move it here return 0;
     }

Quoted 5 lines

     return -errno;
}
...

If you move the trace there, maybe renaming it to trace_vfio_precopy_empty_hit() will be more accurate?

Quoted 9 lines

This move and rename seems sensible to me.

Quoted 1 line

And trying to avoid adding the extra VFIOMigration->save_iterate_empty_hit flag, can we simply trace it every time?

Will have to do some tests to be sure but if there's possibility that we get ENOMSG many times then obviously we don't want to flood logs with this trace event in this case - we want to only log the "data present" -> "data not present" edge/change.

Indeed - it needs to be triggered only on the "non-empty"/"empty" change otherwise there are 3 repeats of this trace event per VFIO device in a rapid succession.

So some kind of a flag here is necessary: with the trace event being "re-armed" by a non-empty read from the VFIO device I get just one such event per device.

Thanks,
Maciej

#5 lore raw

Re: [PATCH 1/4] vfio/migration: Add save_{iterate,complete_precopy}_started trace events

In reply to Maciej S. Szmigiero
Quoted 66 lines · Maciej S. Szmigiero

External email: Use caution opening links or attachments

Hi Avihai,

Quoted 41 lines · Avihai Horon

Hi Maciej,

Quoted 38 lines · Maciej S. Szmigiero

External email: Use caution opening links or attachments

From: "Maciej S. Szmigiero" <[email protected]>

This way both the start and end points of migrating a particular VFIO device are known.

Add also a vfio_save_iterate_empty_hit trace event so it is known when there's no more data to send for that device.

---
  hw/vfio/migration.c           | 13 +++++++++++++ hw/vfio/trace-events          |  3 +++ include/hw/vfio/vfio-common.h |  3 +++

  3 files changed, 19 insertions(+)
hw/vfio/migration.c +3 -0
index 992dc3b10257..1b1ddf527d69 100644
@@ -472,6 +472,9 @@ static int vfio_save_setup(QEMUFile *f, void 
 *opaque, Error **errp)
           return -ENOMEM;
       }
 
+    migration->save_iterate_started = false;
+    migration->save_iterate_empty_hit = false;
+
       if (vfio_precopy_supported(vbasedev)) {
           switch (migration->device_state) {

          case VFIO_DEVICE_STATE_RUNNING:

hw/vfio/migration.c +0 -0
@@ -602,9 +605,17 @@ static int vfio_save_iterate(QEMUFile *f, void 
 *opaque)
       VFIOMigration *migration = vbasedev->migration;
       ssize_t data_size;
 
Quoted 14 lines

hw/vfio/migration.c static int vfio_save_iterate(QEMUFile *f, void *opaque) · line 619

Quoted 12 lines
hw/vfio/migration.c +8 -0
+    if (!migration->save_iterate_started) {
+        trace_vfio_save_iterate_started(vbasedev->name);
+        migration->save_iterate_started = true;
+    }
+
       data_size = vfio_save_block(f, migration);
       if (data_size < 0) {
           return data_size;
+    } else if (data_size == 0 && !migration->save_iterate_empty_hit) {
+        trace_vfio_save_iterate_empty_hit(vbasedev->name);
+        migration->save_iterate_empty_hit = true;
       }

Can we instead use trace_vfio_save_iterate to understand if the device reached 0?

AFAIK there's not way to filter trace events by their parameters, like only logging vfio_save_iterate trace event if both parameters are zero.

Quoted 7 lines

It means that vfio_save_iterate has to be enabled unconditionally to serve as a replacement for vfio_save_iterate_empty_hit, which could result in it being logged/emitted many extra times (with non-zero parameters).

Because of that I think having a dedicated trace event for such occasion makes sense (it is also easily grep-able).

Ahh, I understand.

Quoted 20 lines
Quoted 14 lines

In any case, I think the above could fit better in vfio_save_block(), where ENOMSG indicates that the device has no more data to send during pre-copy phase:

...
if (data_size < 0) {
     /*
      * Pre-copy emptied all the device state for now. For more information,
      * please refer to the Linux kernel VFIO uAPI.
      */
     if (errno == ENOMSG) { trace_vfio_save_iterate_empty_hit(vbasedev->name) <--------------- move it here return 0;
     }

Quoted 5 lines

     return -errno;
}
...

If you move the trace there, maybe renaming it to trace_vfio_precopy_empty_hit() will be more accurate?

Quoted 8 lines

This move and rename seems sensible to me.

Quoted 1 line

And trying to avoid adding the extra VFIOMigration->save_iterate_empty_hit flag, can we simply trace it every time?

Will have to do some tests to be sure but if there's possibility that we get ENOMSG many times then obviously we don't want to flood logs with this trace event in this case - we want to only log the "data present" -> "data not present" edge/change.

OK, so I guess a flag is really needed.
BTW, there is also trace_vfio_state_pending_exact, maybe it could do the job? It might get called multiple times but not as many as vfio_save_iterate.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/vfio/migration.c +0 -0
       vfio_update_estimated_pending_data(migration, data_size);
@@ -630,6 +641,8 @@ static int vfio_save_complete_precopy(QEMUFile 
 *f, void *opaque)
       int ret;
       Error *local_err = NULL;
 
Quoted 8 lines
Quoted 3 lines

hw/vfio/migration.c static int vfio_save_complete_precopy(QEMUFile *f, void *opaque) · line 644

Quoted 1 line
hw/vfio/migration.c +1 -0
+ trace_vfio_save_complete_precopy_started(vbasedev->name);

I assume this trace is used to measure how long it takes for vfio_save_complete_precopy() to run? If so, can we use trace_vmstate_downtime_save to achieve the same goal?

With an appropriate filtering I guess it could be used to extract the same data, although explicit VFIO trace point makes it easier to just look at these traces directly (less noise from other devices).

I see. Well, I don't have a strong opinion if it makes life easier.

Thanks.

Quoted 10 lines

But at the same time trace_vfio_save_complete_precopy already exists and by this metric it would also be unnecessary.

Quoted 1 line

Thanks.

Thanks,
Maciej

#6 lore raw

Re: [PATCH 1/4] vfio/migration: Add save_{iterate,complete_precopy}_started trace events

Quoted 100 lines · Avihai Horon
Quoted 64 lines · Maciej S. Szmigiero

External email: Use caution opening links or attachments

Hi Avihai,

Quoted 39 lines · Avihai Horon

Hi Maciej,

Quoted 36 lines · Maciej S. Szmigiero

External email: Use caution opening links or attachments

From: "Maciej S. Szmigiero" <[email protected]>

This way both the start and end points of migrating a particular VFIO device are known.

Add also a vfio_save_iterate_empty_hit trace event so it is known when there's no more data to send for that device.

---
  hw/vfio/migration.c           | 13 +++++++++++++ hw/vfio/trace-events          |  3 +++ include/hw/vfio/vfio-common.h |  3 +++

  3 files changed, 19 insertions(+)
hw/vfio/migration.c +3 -0
index 992dc3b10257..1b1ddf527d69 100644
@@ -472,6 +472,9 @@ static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp)
           return -ENOMEM;
       }
 
+    migration->save_iterate_started = false;
+    migration->save_iterate_empty_hit = false;
+
       if (vfio_precopy_supported(vbasedev)) {
           switch (migration->device_state) {

          case VFIO_DEVICE_STATE_RUNNING:

hw/vfio/migration.c +0 -0
@@ -602,9 +605,17 @@ static int vfio_save_iterate(QEMUFile *f, void *opaque)
       VFIOMigration *migration = vbasedev->migration;
       ssize_t data_size;
 
Quoted 14 lines

hw/vfio/migration.c static int vfio_save_iterate(QEMUFile *f, void *opaque) · line 619

Quoted 12 lines
hw/vfio/migration.c +8 -0
+    if (!migration->save_iterate_started) {
+        trace_vfio_save_iterate_started(vbasedev->name);
+        migration->save_iterate_started = true;
+    }
+
       data_size = vfio_save_block(f, migration);
       if (data_size < 0) {
           return data_size;
+    } else if (data_size == 0 && !migration->save_iterate_empty_hit) {
+        trace_vfio_save_iterate_empty_hit(vbasedev->name);
+        migration->save_iterate_empty_hit = true;
       }

Can we instead use trace_vfio_save_iterate to understand if the device reached 0?

AFAIK there's not way to filter trace events by their parameters, like only logging vfio_save_iterate trace event if both parameters are zero.

Quoted 7 lines

It means that vfio_save_iterate has to be enabled unconditionally to serve as a replacement for vfio_save_iterate_empty_hit, which could result in it being logged/emitted many extra times (with non-zero parameters).

Because of that I think having a dedicated trace event for such occasion makes sense (it is also easily grep-able).

Ahh, I understand.

Quoted 24 lines
Quoted 14 lines

In any case, I think the above could fit better in vfio_save_block(), where ENOMSG indicates that the device has no more data to send during pre-copy phase:

...
if (data_size < 0) {
     /*
      * Pre-copy emptied all the device state for now. For more information,
      * please refer to the Linux kernel VFIO uAPI.
      */
     if (errno == ENOMSG) { trace_vfio_save_iterate_empty_hit(vbasedev->name) <--------------- move it here return 0;
     }

Quoted 5 lines

     return -errno;
}
...

If you move the trace there, maybe renaming it to trace_vfio_precopy_empty_hit() will be more accurate?

This move and rename seems sensible to me.

Quoted 1 line

And trying to avoid adding the extra VFIOMigration->save_iterate_empty_hit flag, can we simply trace it every time?

Quoted 7 lines
Quoted 4 lines

Will have to do some tests to be sure but if there's possibility that we get ENOMSG many times then obviously we don't want to flood logs with this trace event in this case - we want to only log the "data present" -> "data not present" edge/change.

OK, so I guess a flag is really needed.
BTW, there is also trace_vfio_state_pending_exact, maybe it could do the job? It might get called multiple times but not as many as vfio_save_iterate.

In a quick test run it was still called/logged 5 times for each VFIO device so quite more often than the empty_hit one (which was logged just once per dev).

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

hw/vfio/migration.c static int vfio_save_complete_precopy(QEMUFile *f, void *opaque) · line 644

Quoted 8 lines
hw/vfio/migration.c +1 -0
       vfio_update_estimated_pending_data(migration, data_size);
@@ -630,6 +641,8 @@ static int vfio_save_complete_precopy(QEMUFile *f, void *opaque)
       int ret;
       Error *local_err = NULL;
 
+ trace_vfio_save_complete_precopy_started(vbasedev->name);

I assume this trace is used to measure how long it takes for vfio_save_complete_precopy() to run? If so, can we use trace_vmstate_downtime_save to achieve the same goal?

Quoted 19 lines
Quoted 4 lines

With an appropriate filtering I guess it could be used to extract the same data, although explicit VFIO trace point makes it easier to just look at these traces directly (less noise from other devices).

I see. Well, I don't have a strong opinion if it makes life easier.

Thanks.

Quoted 10 lines

But at the same time trace_vfio_save_complete_precopy already exists and by this metric it would also be unnecessary.

Quoted 1 line

Thanks.

Thanks,
Maciej

Thanks,
Maciej

#7 lore raw

Re: [PATCH 1/4] vfio/migration: Add save_{iterate,complete_precopy}_started trace events

Quoted 112 lines · Maciej S. Szmigiero

External email: Use caution opening links or attachments

Quoted 108 lines · Avihai Horon
Quoted 67 lines · Maciej S. Szmigiero

External email: Use caution opening links or attachments

Hi Avihai,

Quoted 41 lines · Avihai Horon

Hi Maciej,

Quoted 38 lines · Maciej S. Szmigiero

External email: Use caution opening links or attachments

From: "Maciej S. Szmigiero" <[email protected]>

This way both the start and end points of migrating a particular VFIO device are known.

Add also a vfio_save_iterate_empty_hit trace event so it is known when there's no more data to send for that device.

---
  hw/vfio/migration.c           | 13 +++++++++++++ hw/vfio/trace-events          |  3 +++ include/hw/vfio/vfio-common.h |  3 +++

  3 files changed, 19 insertions(+)
hw/vfio/migration.c +3 -0
index 992dc3b10257..1b1ddf527d69 100644
@@ -472,6 +472,9 @@ static int vfio_save_setup(QEMUFile *f, void 
 *opaque, Error **errp)
           return -ENOMEM;
       }
 
+    migration->save_iterate_started = false;
+    migration->save_iterate_empty_hit = false;
+
       if (vfio_precopy_supported(vbasedev)) {
           switch (migration->device_state) {

          case VFIO_DEVICE_STATE_RUNNING:

hw/vfio/migration.c +0 -0
@@ -602,9 +605,17 @@ static int vfio_save_iterate(QEMUFile *f, 
 void *opaque)
       VFIOMigration *migration = vbasedev->migration;
       ssize_t data_size;
 
Quoted 15 lines

hw/vfio/migration.c static int vfio_save_iterate(QEMUFile *f, void *opaque) · line 619

Quoted 13 lines
hw/vfio/migration.c +8 -0
+    if (!migration->save_iterate_started) {
+ trace_vfio_save_iterate_started(vbasedev->name);
+        migration->save_iterate_started = true;
+    }
+
       data_size = vfio_save_block(f, migration);
       if (data_size < 0) {
           return data_size;
+    } else if (data_size == 0 && 
 !migration->save_iterate_empty_hit) {
+ trace_vfio_save_iterate_empty_hit(vbasedev->name);
+        migration->save_iterate_empty_hit = true;
       }

Can we instead use trace_vfio_save_iterate to understand if the device reached 0?

AFAIK there's not way to filter trace events by their parameters, like only logging vfio_save_iterate trace event if both parameters are zero.

Quoted 7 lines

It means that vfio_save_iterate has to be enabled unconditionally to serve as a replacement for vfio_save_iterate_empty_hit, which could result in it being logged/emitted many extra times (with non-zero parameters).

Because of that I think having a dedicated trace event for such occasion makes sense (it is also easily grep-able).

Ahh, I understand.

Quoted 28 lines
Quoted 14 lines

In any case, I think the above could fit better in vfio_save_block(), where ENOMSG indicates that the device has no more data to send during pre-copy phase:

...
if (data_size < 0) {
     /*
      * Pre-copy emptied all the device state for now. For more information,
      * please refer to the Linux kernel VFIO uAPI.
      */
     if (errno == ENOMSG) { trace_vfio_save_iterate_empty_hit(vbasedev->name) <--------------- move it here return 0;
     }

Quoted 5 lines

     return -errno;
}
...

If you move the trace there, maybe renaming it to trace_vfio_precopy_empty_hit() will be more accurate?

This move and rename seems sensible to me.

Quoted 1 line

And trying to avoid adding the extra VFIOMigration->save_iterate_empty_hit flag, can we simply trace it every time?

Will have to do some tests to be sure but if there's possibility that we get ENOMSG many times then obviously we don't want to flood logs with this trace event in this case - we want to only log the "data present" -> "data not present" edge/change.

Quoted 5 lines
Quoted 2 lines

OK, so I guess a flag is really needed.
BTW, there is also trace_vfio_state_pending_exact, maybe it could do the job? It might get called multiple times but not as many as vfio_save_iterate.

In a quick test run it was still called/logged 5 times for each VFIO device so quite more often than the empty_hit one (which was logged just once per dev).

Yes, that is expected.

If that's too noisy for you then the empty_hit trace seems fine, IMHO.

Thanks.

#8 lore raw

PATCH 2/4 migration/ram: Add load start trace event

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

There's a RAM load complete trace event but there wasn't its start equivalent.

---
 migration/ram.c        | 1 +
 migration/trace-events | 1 +
 2 files changed, 2 insertions(+)
migration/ram.c +1 -0
index 326ce7eb791f..72cd1dbb22c4 100644
@@ -4294,6 +4294,7 @@ static int ram_load(QEMUFile *f, void *opaque, int version_id)
      * it will be necessary to reduce the granularity of this
      * critical section.
      */
+    trace_ram_load_start();
     WITH_RCU_READ_LOCK_GUARD() {
         if (postcopy_running) {
             /*
migration/trace-events +1 -0
index c65902f042bd..2a99a7baaea6 100644
@@ -115,6 +115,7 @@ colo_flush_ram_cache_end(void) ""
 save_xbzrle_page_skipping(void) ""
 save_xbzrle_page_overflow(void) ""
 ram_save_iterate_big_wait(uint64_t milliconds, int iterations) "big wait: %" PRIu64 " milliseconds, %d iterations"
+ram_load_start(void) ""
 ram_load_complete(int ret, uint64_t seq_iter) "exit_code %d seq iteration %" PRIu64
 ram_write_tracking_ramblock_start(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu"
 ram_write_tracking_ramblock_stop(const char *block_id, size_t page_size, void *addr, size_t length) "%s: page_size: %zu addr: %p length: %zu"
#10 lore raw

PATCH 3/4 migration/multifd: Zero p->flags before starting filling a packet

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

This way there aren't stale flags there.

p->flags can't contain SYNC to be sent at the next RAM packet since syncs are now handled separately in multifd_send_thread.

---
 migration/multifd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/multifd.c +1 -1
index 9b200f4ad912..c9b24caa64bc 100644
@@ -600,6 +600,7 @@ static void *multifd_send_thread(void *opaque)
          * qatomic_store_release() in multifd_send().
          */
         if (qatomic_load_acquire(&p->pending_job)) {
+            p->flags = 0;
             p->iovs_num = 0;
             assert(!multifd_payload_empty(p->data));
 
@@ -651,7 +652,6 @@ static void *multifd_send_thread(void *opaque)
                 }
                 /* p->next_packet_size will always be zero for a SYNC packet */
                 stat64_add(&mig_stats.multifd_bytes, p->packet_len);
-                p->flags = 0;
             }
 
             qatomic_set(&p->pending_sync, false);
#11 lore raw

PATCH 4/4 migration: Document the BQL behavior of load SaveVMHandlers

In reply to Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Some of these SaveVMHandlers were missing the BQL behavior annotation, making people wonder what it exactly is.

---
 include/migration/register.h | 4 ++++
 1 file changed, 4 insertions(+)
include/migration/register.h +4 -0
index f60e797894e5..c411d84876ec 100644
@@ -210,6 +210,8 @@ typedef struct SaveVMHandlers {
     void (*state_pending_exact)(void *opaque, uint64_t *must_precopy,
                                 uint64_t *can_postcopy);
 
+    /* This runs inside the BQL. */
+
     /**
      * @load_state
      *
@@ -227,6 +229,8 @@ typedef struct SaveVMHandlers {
      */
     int (*load_state)(QEMUFile *f, void *opaque, int version_id);
 
+    /* The following handlers run inside the BQL. */
+
     /**
      * @load_setup
      *
#13 lore raw

Re: [PATCH 4/4] migration: Document the BQL behavior of load SaveVMHandlers

In reply to Maciej S. Szmigiero
Quoted 24 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Some of these SaveVMHandlers were missing the BQL behavior annotation, making people wonder what it exactly is.

---
 include/migration/register.h | 4 ++++
 1 file changed, 4 insertions(+)
include/migration/register.h +2 -0
index f60e797894e5..c411d84876ec 100644
@@ -210,6 +210,8 @@ typedef struct SaveVMHandlers {
     void (*state_pending_exact)(void *opaque, uint64_t *must_precopy,
                                 uint64_t *can_postcopy);
 
+    /* This runs inside the BQL. */
+
     /**
      * @load_state
      *

include/migration/register.h typedef struct SaveVMHandlers { · line 232

Quoted 4 lines
include/migration/register.h +1 -0
@@ -227,6 +229,8 @@ typedef struct SaveVMHandlers {
      */
     int (*load_state)(QEMUFile *f, void *opaque, int version_id);
 
+    /* The following handlers run inside the BQL. */

If above also requires BQL, why not move this line upper?

OTOH, I think resume_prepare() doesn't require BQL..

Quoted 5 lines
include/migration/register.h +1 -0
+
     /**
      * @load_setup
      *
Signature · 1 line
Peter Xu
#14 lore raw

Re: [PATCH 4/4] migration: Document the BQL behavior of load SaveVMHandlers

Quoted 28 lines · Peter Xu
Quoted 27 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Some of these SaveVMHandlers were missing the BQL behavior annotation, making people wonder what it exactly is.

---
  include/migration/register.h | 4 ++++
  1 file changed, 4 insertions(+)
include/migration/register.h +2 -0
index f60e797894e5..c411d84876ec 100644
@@ -210,6 +210,8 @@ typedef struct SaveVMHandlers {
      void (*state_pending_exact)(void *opaque, uint64_t *must_precopy,
                                  uint64_t *can_postcopy);
 
+    /* This runs inside the BQL. */
+
      /**
       * @load_state
       *
@@ -227,6 +229,8 @@ typedef struct SaveVMHandlers {
       */
      int (*load_state)(QEMUFile *f, void *opaque, int version_id);
 
Quoted 3 lines

include/migration/register.h typedef struct SaveVMHandlers { · line 232

Quoted 1 line
include/migration/register.h +1 -0
+    /* The following handlers run inside the BQL. */

If above also requires BQL, why not move this line upper?

The reason for this is that my main patch set also adds "load_state_buffer" handler, which runs without BQL.

That handler is ordered next after "load_state" and I tried to avoid further comment churn here.

But if you prefer to change these comments in the patch introducing "load_state_buffer" handler instead then it's fine.

Quoted 1 line

OTOH, I think resume_prepare() doesn't require BQL..

Yes, it seems like resume_prepare() is only called outside BQL. Will update the patch.

Thanks,
Maciej

#15 lore raw

Re: [PATCH 4/4] migration: Document the BQL behavior of load SaveVMHandlers

Quoted 34 lines · Maciej S. Szmigiero
Quoted 21 lines · Peter Xu
Quoted 20 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

Some of these SaveVMHandlers were missing the BQL behavior annotation, making people wonder what it exactly is.

---
  include/migration/register.h | 4 ++++
  1 file changed, 4 insertions(+)
include/migration/register.h +2 -0
index f60e797894e5..c411d84876ec 100644
@@ -210,6 +210,8 @@ typedef struct SaveVMHandlers {
      void (*state_pending_exact)(void *opaque, uint64_t *must_precopy,
                                  uint64_t *can_postcopy);
+    /* This runs inside the BQL. */
+
      /**
Quoted 8 lines

include/migration/register.h typedef struct SaveVMHandlers { · line 231

Quoted 6 lines
include/migration/register.h +1 -0
       * @load_state
       *
@@ -227,6 +229,8 @@ typedef struct SaveVMHandlers {
       */
      int (*load_state)(QEMUFile *f, void *opaque, int version_id);
+    /* The following handlers run inside the BQL. */

If above also requires BQL, why not move this line upper?

The reason for this is that my main patch set also adds "load_state_buffer" handler, which runs without BQL.

Quoted 6 lines

That handler is ordered next after "load_state" and I tried to avoid further comment churn here.

But if you prefer to change these comments in the patch introducing "load_state_buffer" handler instead then it's fine.

Considering the current change is so small to start benefit readers.. I think it's ok we do this in one shot after load_state_buffer() change.

Quoted 4 lines
Quoted 1 line

OTOH, I think resume_prepare() doesn't require BQL..

Yes, it seems like resume_prepare() is only called outside BQL. Will update the patch.

Thanks,

Signature · 1 line
Peter Xu
#16 lore raw
In reply to Maciej S. Szmigiero
Quoted 33 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

A new version of the multifd device state transfer support with VFIO consumer patch set is being prepared, the previous version and the associated discussion is available here:
https://lore.kernel.org/qemu-devel/[email protected]/

This new version was originally targeting QEMU 9.2 but such schedule proved to be too optimistic due to sheer number of invasive changes/rework required, especially with respect to the VFIO internal threads management and their synchronization with the migration core.

In addition to these changes, recently merged commit 3b5948f808e3 ("vfio/migration: Report only stop-copy size in vfio_state_pending_exact()") seems to have uncovered a race between multifd RAM and device state transfers:
RAM transfer sender finishes the multifd stream with a SYNC in ram_save_complete() but the multifd receive channels are only released from this SYNC after the migration is wholly complete in process_incoming_migration_bh().

The above causes problems if the multifd channels need to still be running after the RAM transfer is completed, for example because there is still remaining device state to be transferred.

Since QEMU 9.2 code freeze is coming I've separated small uncontroversial commits from that WiP main patch set here, some of which were already reviewed during previous main patch set iterations.

This way at least future code conflicts can be reduced and the amount of patches that need to be carried in the future versions of the main patch set is reduced.

Quoted 6 lines

Maciej S. Szmigiero (4):
vfio/migration: Add save_{iterate,complete_precopy}_started trace
events
migration/ram: Add load start trace event migration/multifd: Zero p->flags before starting filling a packet
migration: Document the BQL behavior of load SaveVMHandlers

I queued patch 2-3. Patch 4 is ok to be merged even after softfreeze if it's a doc only change, but we don't need to rush either..

Thanks,

Signature · 1 line
Peter Xu
#17 lore raw
Quoted 41 lines · Peter Xu
Quoted 33 lines · Maciej S. Szmigiero

From: "Maciej S. Szmigiero" <[email protected]>

A new version of the multifd device state transfer support with VFIO consumer patch set is being prepared, the previous version and the associated discussion is available here:
https://lore.kernel.org/qemu-devel/[email protected]/

This new version was originally targeting QEMU 9.2 but such schedule proved to be too optimistic due to sheer number of invasive changes/rework required, especially with respect to the VFIO internal threads management and their synchronization with the migration core.

In addition to these changes, recently merged commit 3b5948f808e3 ("vfio/migration: Report only stop-copy size in vfio_state_pending_exact()") seems to have uncovered a race between multifd RAM and device state transfers:
RAM transfer sender finishes the multifd stream with a SYNC in ram_save_complete() but the multifd receive channels are only released from this SYNC after the migration is wholly complete in process_incoming_migration_bh().

The above causes problems if the multifd channels need to still be running after the RAM transfer is completed, for example because there is still remaining device state to be transferred.

Since QEMU 9.2 code freeze is coming I've separated small uncontroversial commits from that WiP main patch set here, some of which were already reviewed during previous main patch set iterations.

This way at least future code conflicts can be reduced and the amount of patches that need to be carried in the future versions of the main patch set is reduced.

Quoted 6 lines

Maciej S. Szmigiero (4):
vfio/migration: Add save_{iterate,complete_precopy}_started trace
events
migration/ram: Add load start trace event migration/multifd: Zero p->flags before starting filling a packet
migration: Document the BQL behavior of load SaveVMHandlers

Quoted 5 lines

I queued patch 2-3. Patch 4 is ok to be merged even after softfreeze if it's a doc only change, but we don't need to rush either..

Thanks,

Thanks!

Maciej