From: Denis V. Lunev <[email protected]>

A post_load hook which maps or unmaps a memory region commits a memory
transaction, and each commit regenerates the flatviews it touches at a
cost which grows with the number of regions in the machine. A device
which remaps per vCPU, as the Hyper-V SynIC does, pays that once per
vCPU on the destination.

Add a post_load_mr_update callback, called after post_load, which only
brings the device's memory regions in line with the state post_load
restored. During a load the calls are queued and made in stream order
inside one memory transaction, once the device state has been read and
before the guest can start: at the end of qemu_loadvm_state() for
precopy, snapshot and xen loads, and for postcopy in the POSTCOPY_RUN
handler, before the bottom half which starts the guest is scheduled.
The queue is run even when the load fails, as the calls would have been
made inline.

The update is a callback of its own rather than a way to defer
post_load, so that only the region work moves and the device state
other sections read is restored where it always was. What may go in it
is then checkable by reading it: nothing else in the load may depend on
the regions it maps, and a lookup made during the batch resolves
against the topology from before it. It cannot fail, so error reporting
of the load is unchanged.

The batch spans the load rather than one device because a remap which
repeats per vCPU sits in a per-vCPU section, and no smaller unit of the
stream covers the repeats.

Only the thread which opened the batch queues. The postcopy listen
thread, which reads the stream while the package is processed, makes
any call it reaches at once.

CC: Peter Xu <[email protected]>
CC: Fabiano Rosas <[email protected]>
CC: Paolo Bonzini <[email protected]>
CC: Zhao Liu <[email protected]>
Signed-off-by: Denis V. Lunev <[email protected]>
---
 include/migration/vmstate.h |  30 ++++++++++
 migration/savevm.c          |  12 ++++
 migration/vmstate.c         |  50 +++++++++++++++++
 tests/unit/test-vmstate.c   | 109 ++++++++++++++++++++++++++++++++++++
 4 files changed, 201 insertions(+)

diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h
index 2901877308..9f7ae1a224 100644
--- a/include/migration/vmstate.h
+++ b/include/migration/vmstate.h
@@ -305,6 +305,19 @@ struct VMStateDescription {
     bool (*pre_load_errp)(void *opaque, Error **errp);
     int (*post_load)(void *opaque, int version_id);
     bool (*post_load_errp)(void *opaque, int version_id, Error **errp);
+
+    /*
+     * Maps, unmaps or moves the device's memory regions to match the state
+     * restored by .post_load() or .post_load_errp(), and does nothing else.
+     *
+     * Called after the post_load hook. During a load the calls are batched:
+     * they run once the device state has been read and before the guest can
+     * start, in stream order, inside one memory transaction, so the load
+     * rebuilds the topology once rather than once per call. Nothing else in
+     * the load may depend on the regions mapped here, and a lookup made
+     * during the batch resolves against the topology from before it.
+     */
+    void (*post_load_mr_update)(void *opaque);
     int (*pre_save)(void *opaque);
     bool (*pre_save_errp)(void *opaque, Error **errp);
 
@@ -1291,6 +1304,23 @@ bool vmstate_save_vmsd(QEMUFile *f, const 
VMStateDescription *vmsd,
 
 bool vmstate_section_needed(const VMStateDescription *vmsd, void *opaque);
 
+/**
+ * vmstate_mr_update_batch_begin: Batch post_load_mr_update calls
+ *
+ * Until vmstate_mr_update_batch_end(), a post_load_mr_update call reached
+ * from the calling thread is queued rather than made. Other threads still
+ * make it at once.
+ */
+void vmstate_mr_update_batch_begin(void);
+
+/**
+ * vmstate_mr_update_batch_end: Make the queued post_load_mr_update calls
+ *
+ * Makes them in the order they were queued. Does nothing if no batch is
+ * open.
+ */
+void vmstate_mr_update_batch_end(void);
+
 #define  VMSTATE_INSTANCE_ID_ANY  -1
 
 /* Returns: 0 on success, -1 on failure */
diff --git a/migration/savevm.c b/migration/savevm.c
index ac48db7754..910199b868 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -2226,6 +2226,13 @@ static int 
loadvm_postcopy_handle_listen(MigrationIncomingState *mis,
     return rc;
 }
 
+static void loadvm_run_mr_updates(void)
+{
+    memory_region_transaction_begin();
+    vmstate_mr_update_batch_end();
+    memory_region_transaction_commit();
+}
+
 static void loadvm_postcopy_handle_run_bh(void *opaque)
 {
     MigrationIncomingState *mis = opaque;
@@ -2276,6 +2283,8 @@ static int 
loadvm_postcopy_handle_run(MigrationIncomingState *mis, Error **errp)
         return -1;
     }
 
+    loadvm_run_mr_updates();
+
     /* We might be already in POSTCOPY_ACTIVE if there is no return path */
     if (mis->state == MIGRATION_STATUS_POSTCOPY_DEVICE) {
         migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_DEVICE,
@@ -3153,9 +3162,12 @@ int qemu_loadvm_state(QEMUFile *f, Error **errp)
      * vm_prepare_start() closes it again before anything runs.
      */
     flatview_dispatch_defer_begin();
+    vmstate_mr_update_batch_begin();
 
     ret = qemu_loadvm_state_main(f, mis, errp);
 
+    loadvm_run_mr_updates();
+
     flatview_dispatch_defer_end();
 
     qemu_event_set(&mis->main_thread_load_event);
diff --git a/migration/vmstate.c b/migration/vmstate.c
index 24004565bf..f4a3ae3552 100644
--- a/migration/vmstate.c
+++ b/migration/vmstate.c
@@ -18,6 +18,7 @@
 #include "qobject/json-writer.h"
 #include "qemu-file.h"
 #include "qemu/bitops.h"
+#include "qemu/coroutine-tls.h"
 #include "qemu/error-report.h"
 #include "trace.h"
 
@@ -250,6 +251,51 @@ static bool vmstate_load_field(QEMUFile *f, void *pv, 
size_t size,
     return true;
 }
 
+typedef struct VMStateMRUpdate {
+    void (*fn)(void *opaque);
+    void *opaque;
+    QSIMPLEQ_ENTRY(VMStateMRUpdate) entry;
+} VMStateMRUpdate;
+
+static QSIMPLEQ_HEAD(, VMStateMRUpdate) vmstate_mr_updates =
+    QSIMPLEQ_HEAD_INITIALIZER(vmstate_mr_updates);
+QEMU_DEFINE_STATIC_CO_TLS(bool, vmstate_mr_update_batching);
+
+void vmstate_mr_update_batch_begin(void)
+{
+    assert(!get_vmstate_mr_update_batching());
+    assert(QSIMPLEQ_EMPTY(&vmstate_mr_updates));
+    set_vmstate_mr_update_batching(true);
+}
+
+void vmstate_mr_update_batch_end(void)
+{
+    VMStateMRUpdate *u;
+
+    set_vmstate_mr_update_batching(false);
+
+    while ((u = QSIMPLEQ_FIRST(&vmstate_mr_updates))) {
+        QSIMPLEQ_REMOVE_HEAD(&vmstate_mr_updates, entry);
+        u->fn(u->opaque);
+        g_free(u);
+    }
+}
+
+static void vmstate_mr_update(const VMStateDescription *vmsd, void *opaque)
+{
+    VMStateMRUpdate *u;
+
+    if (!get_vmstate_mr_update_batching()) {
+        vmsd->post_load_mr_update(opaque);
+        return;
+    }
+
+    u = g_new(VMStateMRUpdate, 1);
+    u->fn = vmsd->post_load_mr_update;
+    u->opaque = opaque;
+    QSIMPLEQ_INSERT_TAIL(&vmstate_mr_updates, u, entry);
+}
+
 static bool vmstate_post_load(const VMStateDescription *vmsd,
                               void *opaque, int version_id, Error **errp)
 {
@@ -274,6 +320,10 @@ static bool vmstate_post_load(const VMStateDescription 
*vmsd,
         }
     }
 
+    if (vmsd->post_load_mr_update) {
+        vmstate_mr_update(vmsd, opaque);
+    }
+
     return true;
 }
 
diff --git a/tests/unit/test-vmstate.c b/tests/unit/test-vmstate.c
index 23f3173070..975032c2d7 100644
--- a/tests/unit/test-vmstate.c
+++ b/tests/unit/test-vmstate.c
@@ -29,6 +29,7 @@
 #include "../migration/qemu-file.h"
 #include "../migration/savevm.h"
 #include "qemu/module.h"
+#include "qemu/thread.h"
 #include "io/channel-file.h"
 #include "qapi/error.h"
 
@@ -1561,6 +1562,113 @@ static void test_tmp_struct(void)
     g_assert_cmpint(obj.f, ==, 8); /* From the child->parent */
 }
 
+/* Batched post_load_mr_update calls */
+
+static int mr_order;
+static int mr_a_post_load_ran;
+static int mr_a_ran;
+static int mr_b_ran;
+
+static int mr_a_post_load(void *opaque, int version_id)
+{
+    mr_a_post_load_ran = ++mr_order;
+    return 0;
+}
+
+static void mr_a_update(void *opaque)
+{
+    mr_a_ran = ++mr_order;
+}
+
+static void mr_b_update(void *opaque)
+{
+    mr_b_ran = ++mr_order;
+}
+
+static const VMStateDescription vmstate_mr_a = {
+    .name = "test/mr_a",
+    .version_id = 1,
+    .post_load = mr_a_post_load,
+    .post_load_mr_update = mr_a_update,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT32(a, TestStruct),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_mr_b = {
+    .name = "test/mr_b",
+    .version_id = 1,
+    .post_load_mr_update = mr_b_update,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT32(a, TestStruct),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const uint8_t mr_wire[] = {
+    /* uint32 a */ 0x00, 0x00, 0x00, 0x01,
+    QEMU_VM_EOF,
+};
+
+static void mr_reset(void)
+{
+    mr_order = 0;
+    mr_a_post_load_ran = 0;
+    mr_a_ran = 0;
+    mr_b_ran = 0;
+}
+
+static void *mr_load_thread(void *opaque)
+{
+    SUCCESS(load_vmstate_one(&vmstate_mr_a, opaque, 1, mr_wire,
+                             sizeof(mr_wire)));
+    return NULL;
+}
+
+static void mr_load(const VMStateDescription *vmsd, TestStruct *obj)
+{
+    SUCCESS(load_vmstate_one(vmsd, obj, 1, mr_wire, sizeof(mr_wire)));
+}
+
+static void test_post_load_mr_update(void)
+{
+    TestStruct obj = {};
+    QemuThread thread;
+
+    /* Without a batch the update follows its post_load at once */
+    mr_reset();
+    mr_load(&vmstate_mr_a, &obj);
+    g_assert_cmpint(mr_a_post_load_ran, ==, 1);
+    g_assert_cmpint(mr_a_ran, ==, 2);
+
+    /* A batch holds the updates back, keeps their order, not post_load */
+    mr_reset();
+    vmstate_mr_update_batch_begin();
+    mr_load(&vmstate_mr_a, &obj);
+    mr_load(&vmstate_mr_b, &obj);
+    g_assert_cmpint(mr_a_post_load_ran, ==, 1);
+    g_assert_cmpint(mr_a_ran, ==, 0);
+    g_assert_cmpint(mr_b_ran, ==, 0);
+    vmstate_mr_update_batch_end();
+    g_assert_cmpint(mr_a_ran, ==, 2);
+    g_assert_cmpint(mr_b_ran, ==, 3);
+
+    /* Ending twice, as after postcopy RUN, is harmless */
+    vmstate_mr_update_batch_end();
+    g_assert_cmpint(mr_a_ran, ==, 2);
+
+    /* Another thread, like the postcopy listen thread, is not batched */
+    mr_reset();
+    vmstate_mr_update_batch_begin();
+    qemu_thread_create(&thread, "mr-load", mr_load_thread, &obj,
+                       QEMU_THREAD_JOINABLE);
+    qemu_thread_join(&thread);
+    g_assert_cmpint(mr_a_ran, ==, 2);
+    vmstate_mr_update_batch_end();
+    g_assert_cmpint(mr_a_ran, ==, 2);
+}
+
 int main(int argc, char **argv)
 {
     g_autofree char *temp_file = g_strdup_printf("%s/vmst.test.XXXXXX",
@@ -1605,6 +1713,7 @@ int main(int argc, char **argv)
     g_test_add_func("/vmstate/qlist/save/saveqlist", test_save_qlist);
     g_test_add_func("/vmstate/qlist/load/loadqlist", test_load_qlist);
     g_test_add_func("/vmstate/tmp_struct", test_tmp_struct);
+    g_test_add_func("/vmstate/post_load/mr_update", test_post_load_mr_update);
     g_test_run();
 
     close(temp_fd);
-- 
2.53.0


Reply via email to