Skip to contentSkip to conversation

migration/postcopy: report destination states only after the run state change

PATCH Superseded Denis V. Lunev 3 participants 9 messages Oct 2 – Oct 13

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover migration/postcopy: report destination states only after the run state change

On the postcopy destination, the postcopy-active and completed states can be reported while the VM is still in RUN_STATE_INMIGRATE. The run state only changes in loadvm_postcopy_handle_run_bh(), but loadvm_postcopy_handle_run() switches to postcopy-active before it schedules the BH, and the listen thread can report completed before the BH has run. Precopy does not have this problem: the run state changes before completed is reported.

We hit this in production. A VM with a persistent dirty bitmap migrates its bitmaps in postcopy, so libvirt runs the Finish phase as soon as the source enters postcopy-active. Our libvirt reattaches disks on the destination in Finish, and the blockdev-add calls land while the destination is still in RUN_STATE_INMIGRATE. The new nodes are created inactive, the device cannot be attached to them ("Block node is read-only"), and the guest is lost on both hosts. A bitmap-only postcopy makes the window wide, because the listen thread has almost nothing to load.

The series orders both destination states after the run state change and adds a qtest check that fails on master. The last patch gives the source's postcopy-active the same meaning. It takes Peter's idea from the review of Juraj's RFC [1]: record the package-loaded ping and send the pong later [2]. Here the pong is sent from the run BH, after the run state change, rather than from loadvm_postcopy_handle_run(). It keeps the source in postcopy-device longer, so it can be dropped without affecting the rest.

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/[email protected]/

Many thanks to Juraj for the postcopy-device state in 7b842fe354c. It is what gives the source a point to wait at, and this series only moves that point to where the destination has really left RUN_STATE_INMIGRATE.

Cc Peter Xu <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Juraj Marcin <[email protected]>

Denis V. Lunev (4):
migration/postcopy: report COMPLETED only after the run BH has run migration/postcopy: set destination postcopy-active in the run BH tests/qtest/migration: check the destination run state in postcopy
tests
migration/postcopy: answer the package-loaded ping from the run BH

 migration/migration.c                  |  3 +++
 migration/migration.h                  |  4 ++++
 migration/postcopy-ram.c               |  4 ++++
 migration/savevm.c                     | 18 +++++++++++++-----
 tests/qtest/migration/framework.c      |  6 ++++--
 tests/qtest/migration/migration-qmp.c  | 10 ++++++++--
 tests/qtest/migration/migration-qmp.h  |  3 ++-
 tests/qtest/migration/migration-util.c |  8 ++++++++
 tests/qtest/migration/migration-util.h |  2 ++
 9 files changed, 48 insertions(+), 10 deletions(-)

base-commit: f7ada39edacaa5c26b30e98b94017b0b2ccbcf94

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/4 migration/postcopy: report COMPLETED only after the run BH has run

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

The postcopy listen thread waits for main_thread_load_event before it reports COMPLETED. That event is set as soon as qemu_loadvm_state_main() returns in the main thread, while the run state is changed later, in loadvm_postcopy_handle_run_bh(). The listen thread can therefore report COMPLETED while the VM is still in RUN_STATE_INMIGRATE, easily so in a bitmap-only postcopy where it has little to load.

Management reacting to COMPLETED then finds the VM still incoming: a blockdev-add creates an inactive node, since bds_tree_init() forces BDRV_O_INACTIVE in RUN_STATE_INMIGRATE, and attaching a device to it fails with "Block node is read-only". Precopy gets this right:
process_incoming_migration_bh() changes the run state before it reports COMPLETED.

Set an event at the end of the run BH and wait for it in the listen thread, unless loading failed before CMD_POSTCOPY_RUN and the BH was never scheduled.

Cc Peter Xu <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Juraj Marcin <[email protected]>
---
 migration/migration.c    | 2 ++
 migration/migration.h    | 2 ++
 migration/postcopy-ram.c | 4 ++++
 migration/savevm.c       | 1 +
 4 files changed, 9 insertions(+)
migration/migration.c +2 -0
index fab7949fe1..8944acf3cd 100644
@@ -324,6 +324,7 @@ void migration_object_init(void)
     qemu_mutex_init(&current_incoming->rp_mutex);
     qemu_mutex_init(&current_incoming->postcopy_prio_thread_mutex);
     qemu_event_init(&current_incoming->main_thread_load_event, false);
+    qemu_event_init(&current_incoming->postcopy_run_event, false);
     qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
     qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
     qemu_sem_init(&current_incoming->postcopy_pause_sem_fast_load, 0);
@@ -493,6 +494,7 @@ void migration_incoming_state_destroy(void)
 
     migration_incoming_transport_cleanup(mis);
     qemu_event_reset(&mis->main_thread_load_event);
+    qemu_event_reset(&mis->postcopy_run_event);
 
     if (mis->page_requested) {
         g_tree_destroy(mis->page_requested);
migration/migration.h +2 -0
index 683bc7bdd5..c65212ff63 100644
@@ -108,6 +108,8 @@ struct MigrationIncomingState {
      * loading state.
      */
     QemuEvent main_thread_load_event;
+    /* Set once the postcopy run BH has changed the run state */
+    QemuEvent postcopy_run_event;
 
     /* For network announces */
     AnnounceTimer  announce_timer;
migration/postcopy-ram.c +4 -0
index 5509cbdcb8..fd2c139cfe 100644
@@ -2443,6 +2443,10 @@ static void *postcopy_listen_thread(void *opaque)
      * state yet; wait for the end of the main thread.
      */
     qemu_event_wait(&mis->main_thread_load_event);
+    /* The VM run state is changed later, in loadvm_postcopy_handle_run_bh() */
+    if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING) {
+        qemu_event_wait(&mis->postcopy_run_event);
+    }
 
     /*
      * Device load in the main thread has finished, we should be in
migration/savevm.c +1 -0
index 4b590ea672..5ec09d4889 100644
@@ -2263,6 +2263,7 @@ static void loadvm_postcopy_handle_run_bh(void *opaque)
     }
 
     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-vm-started");
+    qemu_event_set(&mis->postcopy_run_event);
 }
 
 /* After all discards we can start running and asking for pages */

base-commit: f7ada39edacaa5c26b30e98b94017b0b2ccbcf94

Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/4] migration/postcopy: report COMPLETED only after the run BH has run

Quoted 16 lines · Denis V. Lunev

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

The postcopy listen thread waits for main_thread_load_event before it reports COMPLETED. That event is set as soon as qemu_loadvm_state_main() returns in the main thread, while the run state is changed later, in loadvm_postcopy_handle_run_bh(). The listen thread can therefore report COMPLETED while the VM is still in RUN_STATE_INMIGRATE, easily so in a bitmap-only postcopy where it has little to load.

Management reacting to COMPLETED then finds the VM still incoming: a blockdev-add creates an inactive node, since bds_tree_init() forces BDRV_O_INACTIVE in RUN_STATE_INMIGRATE, and attaching a device to it fails with "Block node is read-only". Precopy gets this right:
process_incoming_migration_bh() changes the run state before it reports COMPLETED.

Quoted 3 lines

Set an event at the end of the run BH and wait for it in the listen thread, unless loading failed before CMD_POSTCOPY_RUN and the BH was never scheduled.

This in general looks reasonable, only some question to impl details,

Quoted 37 lines
Cc Peter Xu <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Juraj Marcin <[email protected]>
---
 migration/migration.c    | 2 ++
 migration/migration.h    | 2 ++
 migration/postcopy-ram.c | 4 ++++
 migration/savevm.c       | 1 +
 4 files changed, 9 insertions(+)
migration/migration.c +2 -0
index fab7949fe1..8944acf3cd 100644
@@ -324,6 +324,7 @@ void migration_object_init(void)
     qemu_mutex_init(&current_incoming->rp_mutex);
     qemu_mutex_init(&current_incoming->postcopy_prio_thread_mutex);
     qemu_event_init(&current_incoming->main_thread_load_event, false);
+    qemu_event_init(&current_incoming->postcopy_run_event, false);
     qemu_sem_init(&current_incoming->postcopy_pause_sem_dst, 0);
     qemu_sem_init(&current_incoming->postcopy_pause_sem_fault, 0);
     qemu_sem_init(&current_incoming->postcopy_pause_sem_fast_load, 0);
@@ -493,6 +494,7 @@ void migration_incoming_state_destroy(void)
 
     migration_incoming_transport_cleanup(mis);
     qemu_event_reset(&mis->main_thread_load_event);
+    qemu_event_reset(&mis->postcopy_run_event);
 
     if (mis->page_requested) {
         g_tree_destroy(mis->page_requested);
migration/migration.h +0 -0
index 683bc7bdd5..c65212ff63 100644

migration/migration.h struct MigrationIncomingState { · line 112

Quoted 5 lines
migration/migration.h +2 -0
@@ -108,6 +108,8 @@ struct MigrationIncomingState {
      * loading state.
      */
     QemuEvent main_thread_load_event;
+    /* Set once the postcopy run BH has changed the run state */
+    QemuEvent postcopy_run_event;

Can we just move main_thread_load_event post to be run_bh? AFAIU that's the last thing postcopy does anyway, then the bh does whatever offloaded later.

IOW, my read is the new run event should always be posted after the main_thread_load, then we don't need two.

migration/postcopy-ram.c static void *postcopy_listen_thread(void *opaque) · line 2447

Quoted 13 lines
migration/migration.h +0 -0
     /* For network announces */
     AnnounceTimer  announce_timer;
migration/postcopy-ram.c +2 -0
index 5509cbdcb8..fd2c139cfe 100644
@@ -2443,6 +2443,10 @@ static void *postcopy_listen_thread(void *opaque)
      * state yet; wait for the end of the main thread.
      */
     qemu_event_wait(&mis->main_thread_load_event);
+    /* The VM run state is changed later, in loadvm_postcopy_handle_run_bh() */
+    if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING) {

Why the condition, rather than always waiting for it? If it happened already, the wait() will consume the posted.

Thanks,

Quoted 22 lines
migration/postcopy-ram.c +2 -0
+        qemu_event_wait(&mis->postcopy_run_event);
+    }
 
     /*
      * Device load in the main thread has finished, we should be in
migration/savevm.c +1 -0
index 4b590ea672..5ec09d4889 100644
@@ -2263,6 +2263,7 @@ static void loadvm_postcopy_handle_run_bh(void *opaque)
     }
 
     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-vm-started");
+    qemu_event_set(&mis->postcopy_run_event);
 }
 
 /* After all discards we can start running and asking for pages */

base-commit: f7ada39edacaa5c26b30e98b94017b0b2ccbcf94

Signature · 1 line
2.53.0
Signature · 1 line
Peter Xu
#3 lore raw

PATCH 2/4 migration/postcopy: set destination postcopy-active in the run BH

In reply to Denis V. Lunev

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

Commit 7b842fe354c ("migration: Introduce POSTCOPY_DEVICE state") made the destination enter POSTCOPY_ACTIVE in loadvm_postcopy_handle_run(), before it schedules loadvm_postcopy_handle_run_bh(), and only the BH starts the VM or moves it to RUN_STATE_PAUSED. Management reacting to the destination's postcopy-active event can therefore still find the VM in RUN_STATE_INMIGRATE, where blockdev-add creates inactive nodes and attaching a device to them fails with "Block node is read-only".

Move the transition to the end of the run BH, after vm_start() or runstate_set(), as process_incoming_migration_bh() does for precopy COMPLETED. Without a return path the listen thread enters POSTCOPY_ACTIVE directly, and the fast snapshot load path switches right after scheduling the BH; in both cases the state is no longer POSTCOPY_DEVICE when the BH runs and the transition is a no-op.

Cc Peter Xu <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Juraj Marcin <[email protected]>
---
 migration/savevm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
migration/savevm.c +4 -5
index 5ec09d4889..bc1bc98a0e 100644
@@ -2263,6 +2263,10 @@ static void loadvm_postcopy_handle_run_bh(void *opaque)
     }
 
     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-vm-started");
+
+    /* Already POSTCOPY_ACTIVE without a return path or in fast snapshot load */
+    migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_DEVICE,
+                      MIGRATION_STATUS_POSTCOPY_ACTIVE);
     qemu_event_set(&mis->postcopy_run_event);
 }
 
@@ -2277,11 +2281,6 @@ static int loadvm_postcopy_handle_run(MigrationIncomingState *mis, Error **errp)
         return -1;
     }
 
-    /* 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,
-                          MIGRATION_STATUS_POSTCOPY_ACTIVE);
-    }
     postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
     migration_bh_schedule(loadvm_postcopy_handle_run_bh, mis);
 
Signature · 1 line
2.53.0
#4 lore raw

Re: [PATCH 2/4] migration/postcopy: set destination postcopy-active in the run BH

Quoted 9 lines · Denis V. Lunev

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

Commit 7b842fe354c ("migration: Introduce POSTCOPY_DEVICE state") made the destination enter POSTCOPY_ACTIVE in loadvm_postcopy_handle_run(), before it schedules loadvm_postcopy_handle_run_bh(), and only the BH starts the VM or moves it to RUN_STATE_PAUSED. Management reacting to the destination's postcopy-active event can therefore still find the VM in RUN_STATE_INMIGRATE, where blockdev-add creates inactive nodes and attaching a device to them fails with "Block node is read-only".

Yes, this is a fact that we have two "status" during postcopy phase, "query migrate" and "query status". I don't think we have clear definition on how they can correlate, and if there's strict order of state changes..

Pure question is, why not the mgmt listen to the vmstate change event directly (or both)? IOW, I believe mgmt still can have full knowledge of the status quo depending on how mgmt is implemented.

Quoted 4 lines

Move the transition to the end of the run BH, after vm_start() or runstate_set(), as process_incoming_migration_bh() does for precopy COMPLETED. Without a return path the listen thread enters

True, it is still reasonable to at least keep them aligned, that if we update vmstate first then always do that.

Quoted 17 lines

POSTCOPY_ACTIVE directly, and the fast snapshot load path switches right after scheduling the BH; in both cases the state is no longer POSTCOPY_DEVICE when the BH runs and the transition is a no-op.

Cc Peter Xu <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Juraj Marcin <[email protected]>
---
 migration/savevm.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
migration/savevm.c +0 -0
index 5ec09d4889..bc1bc98a0e 100644

migration/savevm.c static void loadvm_postcopy_handle_run_bh(void *opaque) · line 2269

Quoted 7 lines
migration/savevm.c +4 -0
@@ -2263,6 +2263,10 @@ static void loadvm_postcopy_handle_run_bh(void *opaque)
     }
 
     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-vm-started");
+
+    /* Already POSTCOPY_ACTIVE without a return path or in fast snapshot load */
+    migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_DEVICE,
+                      MIGRATION_STATUS_POSTCOPY_ACTIVE);

I think the "if" is good here, rather than relying on migrate_set_state() behavior.. I actually want to change it, low priority but still: the current "if old state is not specified silently ignore" behavior is error prone.

Quoted 18 lines
migration/savevm.c +0 -5
     qemu_event_set(&mis->postcopy_run_event);
 }
 
@@ -2277,11 +2281,6 @@ static int loadvm_postcopy_handle_run(MigrationIncomingState *mis, Error **errp)
         return -1;
     }
 
-    /* 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,
-                          MIGRATION_STATUS_POSTCOPY_ACTIVE);
-    }
     postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
     migration_bh_schedule(loadvm_postcopy_handle_run_bh, mis);
 
Signature · 1 line
2.53.0
Signature · 1 line
Peter Xu
#5 lore raw

PATCH 3/4 tests/qtest/migration: check the destination run state in postcopy tests

In reply to Denis V. Lunev

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

The destination must not report postcopy-active or completed while it is still in RUN_STATE_INMIGRATE. The destination autostarts in these tests, so the run state change emits RESUME and the order is visible in the event stream: record a MIGRATION postcopy-active or completed event seen before RESUME and fail the postcopy tests on it once the destination has completed.

migrate_postcopy_start() waited for the destination's RESUME with qtest_qmp_eventwait(), which reads events past the event callback and drops the ones it does not want. Poll query-status instead, so every destination event reaches the callback.

Cc Peter Xu <[email protected]>
Cc Fabiano Rosas <[email protected]>
Cc Juraj Marcin <[email protected]>
---
 tests/qtest/migration/framework.c      |  6 ++++--
 tests/qtest/migration/migration-qmp.c  | 10 ++++++++--
 tests/qtest/migration/migration-qmp.h  |  3 ++-
 tests/qtest/migration/migration-util.c |  8 ++++++++
 tests/qtest/migration/migration-util.h |  2 ++
 5 files changed, 24 insertions(+), 5 deletions(-)
tests/qtest/migration/framework.c +4 -2
index b7aef6b21c..1cf18233f7 100644
@@ -667,6 +667,8 @@ static void migrate_postcopy_complete(QTestState *from, QTestState *to,
     MigrationTestEnv *env = migration_get_env();
 
     wait_for_migration_complete(from);
+    wait_for_migration_complete(to);
+    g_assert(!dst_state.migrated_before_resume);
 
     if (args->start.suspend_me) {
         /* wakeup succeeds only if guest is suspended */
@@ -695,7 +697,7 @@ void test_postcopy_common(MigrateCommon *args)
     if (migrate_postcopy_prepare(&from, &to, &hook_data, args)) {
         return;
     }
-    migrate_postcopy_start(from, to, &src_state);
+    migrate_postcopy_start(from, to, &src_state, &dst_state);
     migrate_postcopy_complete(from, to, hook_data, args);
 }
 
@@ -842,7 +844,7 @@ void test_postcopy_recovery_common(MigrateCommon *args,
     migrate_set_parameter_int(from, "max-postcopy-bandwidth", 4096);
 
     /* Now we start the postcopy */
-    migrate_postcopy_start(from, to, &src_state);
+    migrate_postcopy_start(from, to, &src_state, &dst_state);
 
     /*
      * Wait until postcopy is really started; we can only run the
tests/qtest/migration/migration-qmp.c +8 -2
index 437b5eaeff..77befe1027 100644
@@ -549,10 +549,16 @@ void migrate_cancel(QTestState *who)
 }
 
 void migrate_postcopy_start(QTestState *from, QTestState *to,
-                            QTestMigrationState *src_state)
+                            QTestMigrationState *src_state,
+                            QTestMigrationState *dst_state)
 {
     qtest_qmp_assert_success(from, "{ 'execute': 'migrate-start-postcopy' }");
 
     wait_for_stop(from, src_state);
-    qtest_qmp_eventwait(to, "RESUME");
+
+    /* Poll so that every destination event passes the event callback */
+    while (!dst_state->resume_seen) {
+        qtest_qmp_assert_success(to, "{ 'execute': 'query-status' }");
+        g_usleep(1000);
+    }
 }
tests/qtest/migration/migration-qmp.h +2 -1
index 940ffd5950..f275cd7692 100644
@@ -46,6 +46,7 @@ void migrate_continue(QTestState *who, const char *state);
 void migrate_recover(QTestState *who, const char *uri);
 void migrate_cancel(QTestState *who);
 void migrate_postcopy_start(QTestState *from, QTestState *to,
-                            QTestMigrationState *src_state);
+                            QTestMigrationState *src_state,
+                            QTestMigrationState *dst_state);
 
 #endif /* MIGRATION_QMP_H */
tests/qtest/migration/migration-util.c +8 -0
index db66d8b24f..4b5663c5a6 100644
@@ -178,6 +178,14 @@ bool migrate_watch_for_events(QTestState *who, const char *name,
     } else if (g_str_equal(name, "RESUME")) {
         state->resume_seen = true;
         return true;
+    } else if (g_str_equal(name, "MIGRATION") && !state->resume_seen) {
+        const char *status = qdict_get_str(qdict_get_qdict(event, "data"),
+                                           "status");
+
+        if (g_str_equal(status, "postcopy-active") ||
+            g_str_equal(status, "completed")) {
+            state->migrated_before_resume = true;
+        }
     }
 
     return false;
tests/qtest/migration/migration-util.h +2 -0
index 694773e594..f195aeea48 100644
@@ -22,6 +22,8 @@ typedef struct QTestMigrationState {
     bool resume_seen;
     bool suspend_seen;
     bool suspend_me;
+    /* postcopy-active or completed reported before RESUME */
+    bool migrated_before_resume;
 } QTestMigrationState;
 
 bool migrate_watch_for_events(QTestState *who, const char *name,
Signature · 1 line
2.53.0
#6 lore raw

PATCH 4/4 migration/postcopy: answer the package-loaded ping from the run BH

In reply to Denis V. Lunev

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

Commit 7b842fe354c ("migration: Introduce POSTCOPY_DEVICE state") keeps the source in POSTCOPY_DEVICE until the destination answers the QEMU_VM_PING_PACKAGED_LOADED ping packaged right before CMD_POSTCOPY_RUN. The destination answers it from the generic MIG_CMD_PING handler as soon as it reads it, so the source's postcopy-active means the device state is loaded, not that the destination has left RUN_STATE_INMIGRATE. Management acting on the destination once the source reports postcopy-active can still race with loadvm_postcopy_handle_run_bh().

Record the ping in the MIG_CMD_PING handler instead of answering it, and send the pong at the end of the run BH, after the run state change.

The wire format is unchanged: the pong only arrives later, and an older destination keeps answering at once, so mixed versions interoperate. The cost is that the source stays in POSTCOPY_DEVICE for the duration of the run BH, including block layer activation when the destination autostarts.

---
 migration/migration.c | 1 +
 migration/migration.h | 2 ++
 migration/savevm.c    | 8 ++++++++
 3 files changed, 11 insertions(+)
migration/migration.c +1 -0
index 8944acf3cd..f93f99f30e 100644
@@ -495,6 +495,7 @@ void migration_incoming_state_destroy(void)
     migration_incoming_transport_cleanup(mis);
     qemu_event_reset(&mis->main_thread_load_event);
     qemu_event_reset(&mis->postcopy_run_event);
+    mis->postcopy_package_loaded_ping = false;
 
     if (mis->page_requested) {
         g_tree_destroy(mis->page_requested);
migration/migration.h +2 -0
index c65212ff63..80401391f8 100644
@@ -110,6 +110,8 @@ struct MigrationIncomingState {
     QemuEvent main_thread_load_event;
     /* Set once the postcopy run BH has changed the run state */
     QemuEvent postcopy_run_event;
+    /* QEMU_VM_PING_PACKAGED_LOADED received, answered by the run BH */
+    bool postcopy_package_loaded_ping;
 
     /* For network announces */
     AnnounceTimer  announce_timer;
migration/savevm.c +8 -0
index bc1bc98a0e..e20471892f 100644
@@ -2267,6 +2267,10 @@ static void loadvm_postcopy_handle_run_bh(void *opaque)
     /* Already POSTCOPY_ACTIVE without a return path or in fast snapshot load */
     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_DEVICE,
                       MIGRATION_STATUS_POSTCOPY_ACTIVE);
+    if (mis->postcopy_package_loaded_ping) {
+        mis->postcopy_package_loaded_ping = false;
+        migrate_send_rp_pong(mis, QEMU_VM_PING_PACKAGED_LOADED);
+    }
     qemu_event_set(&mis->postcopy_run_event);
 }
 
@@ -2618,6 +2622,10 @@ static int loadvm_process_command(QEMUFile *f, Error **errp)
                        tmp32);
             return -1;
         }
+        if (tmp32 == QEMU_VM_PING_PACKAGED_LOADED) {
+            mis->postcopy_package_loaded_ping = true;
+            return 0;
+        }
         migrate_send_rp_pong(mis, tmp32);
         return 0;
 
Signature · 1 line
2.53.0
Reviewed-by on v1 4/4
#7 lore raw

Re: [PATCH 4/4] migration/postcopy: answer the package-loaded ping from the run BH

Quoted 21 lines · Denis V. Lunev

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

Commit 7b842fe354c ("migration: Introduce POSTCOPY_DEVICE state") keeps the source in POSTCOPY_DEVICE until the destination answers the QEMU_VM_PING_PACKAGED_LOADED ping packaged right before CMD_POSTCOPY_RUN. The destination answers it from the generic MIG_CMD_PING handler as soon as it reads it, so the source's postcopy-active means the device state is loaded, not that the destination has left RUN_STATE_INMIGRATE. Management acting on the destination once the source reports postcopy-active can still race with loadvm_postcopy_handle_run_bh().

Record the ping in the MIG_CMD_PING handler instead of answering it, and send the pong at the end of the run BH, after the run state change.

The wire format is unchanged: the pong only arrives later, and an older destination keeps answering at once, so mixed versions interoperate. The cost is that the source stays in POSTCOPY_DEVICE for the duration of the run BH, including block layer activation when the destination autostarts.

Quoted 5 lines

The hope is this should work for Juraj's problem too,

https://lore.kernel.org/all/[email protected]/

If there is a repost, maybe worth mention it in commit log, but we can also wait for more inputs. This looks reasonable to me on its own,

Signature · 1 line
Peter Xu
Reviewed-by Tested-by on v1 4/4
#8 lore raw

Re: [PATCH 4/4] migration/postcopy: answer the package-loaded ping from the run BH

In reply to Denis V. Lunev
Quoted 26 lines · Denis V. Lunev

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

Commit 7b842fe354c ("migration: Introduce POSTCOPY_DEVICE state") keeps the source in POSTCOPY_DEVICE until the destination answers the QEMU_VM_PING_PACKAGED_LOADED ping packaged right before CMD_POSTCOPY_RUN. The destination answers it from the generic MIG_CMD_PING handler as soon as it reads it, so the source's postcopy-active means the device state is loaded, not that the destination has left RUN_STATE_INMIGRATE. Management acting on the destination once the source reports postcopy-active can still race with loadvm_postcopy_handle_run_bh().

Record the ping in the MIG_CMD_PING handler instead of answering it, and send the pong at the end of the run BH, after the run state change.

The wire format is unchanged: the pong only arrives later, and an older destination keeps answering at once, so mixed versions interoperate. The cost is that the source stays in POSTCOPY_DEVICE for the duration of the run BH, including block layer activation when the destination autostarts.

Quoted 5 lines
---
 migration/migration.c | 1 +
 migration/migration.h | 2 ++
 migration/savevm.c    | 8 ++++++++
 3 files changed, 11 insertions(+)

I have tested the patch and it does fix the issue where source becomes unrecoverable if destination dies in CPU sync, that I tried to fix.