[PATCH v3 16/24] migration/multifd: Send final SYNC only after device state is complete

Maciej S. Szmigiero posted 24 patches 5 days, 22 hours ago
[PATCH v3 16/24] migration/multifd: Send final SYNC only after device state is complete
Posted by Maciej S. Szmigiero 5 days, 22 hours ago
From: "Maciej S. Szmigiero" <maciej.szmigiero@oracle.com>

Currently, ram_save_complete() sends a final SYNC multifd packet near this
function end, after sending all of the remaining RAM data.

On the receive side, this SYNC packet will cause multifd channel threads
to block, waiting for the final sem_sync posting in
multifd_recv_terminate_threads().

However, multifd_recv_terminate_threads() won't be called until the
migration is complete, which causes a problem if multifd channels are
still required for transferring device state data after RAM transfer is
complete but before finishing the migration process.

Defer sending the final SYNC packet to the end of sending of
post-switchover iterable data instead if device state transfer is possible.

Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com>
---
 migration/multifd-nocomp.c | 18 +++++++++++++++++-
 migration/multifd.h        |  1 +
 migration/ram.c            | 10 +++++++++-
 migration/savevm.c         | 11 +++++++++++
 4 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/migration/multifd-nocomp.c b/migration/multifd-nocomp.c
index 90c0927b9bcb..db87b1262ffa 100644
--- a/migration/multifd-nocomp.c
+++ b/migration/multifd-nocomp.c
@@ -348,7 +348,7 @@ retry:
     return true;
 }
 
-int multifd_ram_flush_and_sync(void)
+int multifd_ram_flush(void)
 {
     if (!migrate_multifd()) {
         return 0;
@@ -361,6 +361,22 @@ int multifd_ram_flush_and_sync(void)
         }
     }
 
+    return 0;
+}
+
+int multifd_ram_flush_and_sync(void)
+{
+    int ret;
+
+    if (!migrate_multifd()) {
+        return 0;
+    }
+
+    ret = multifd_ram_flush();
+    if (ret) {
+        return ret;
+    }
+
     return multifd_send_sync_main();
 }
 
diff --git a/migration/multifd.h b/migration/multifd.h
index 05ddfb4bf119..3abf9578e2ae 100644
--- a/migration/multifd.h
+++ b/migration/multifd.h
@@ -376,6 +376,7 @@ static inline uint32_t multifd_ram_page_count(void)
 
 void multifd_ram_save_setup(void);
 void multifd_ram_save_cleanup(void);
+int multifd_ram_flush(void);
 int multifd_ram_flush_and_sync(void);
 void multifd_ram_payload_alloc(MultiFDPages_t *pages);
 void multifd_ram_payload_free(MultiFDPages_t *pages);
diff --git a/migration/ram.c b/migration/ram.c
index 05ff9eb32876..cf7bea3f073b 100644
--- a/migration/ram.c
+++ b/migration/ram.c
@@ -3283,7 +3283,15 @@ static int ram_save_complete(QEMUFile *f, void *opaque)
         }
     }
 
-    ret = multifd_ram_flush_and_sync();
+    if (migration_has_device_state_support()) {
+        /*
+         * Can't do the final SYNC here since device state might still
+         * be transferring via multifd channels.
+         */
+        ret = multifd_ram_flush();
+    } else {
+        ret = multifd_ram_flush_and_sync();
+    }
     if (ret < 0) {
         return ret;
     }
diff --git a/migration/savevm.c b/migration/savevm.c
index 6ea9054c4083..98049cb9b09a 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -37,6 +37,7 @@
 #include "migration/register.h"
 #include "migration/global_state.h"
 #include "migration/channel-block.h"
+#include "multifd.h"
 #include "ram.h"
 #include "qemu-file.h"
 #include "savevm.h"
@@ -1496,6 +1497,7 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)
     int64_t start_ts_each, end_ts_each;
     SaveStateEntry *se;
     int ret;
+    bool multifd_device_state = migration_has_device_state_support();
 
     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
         if (!se->ops ||
@@ -1528,6 +1530,15 @@ int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)
                                     end_ts_each - start_ts_each);
     }
 
+    if (multifd_device_state) {
+        /* Send the final SYNC */
+        ret = multifd_send_sync_main();
+        if (ret) {
+            qemu_file_set_error(f, ret);
+            return -1;
+        }
+    }
+
     trace_vmstate_downtime_checkpoint("src-iterable-saved");
 
     return 0;