@samitouri / QOSamiQemu / commits / 4f08b870f8

migration: Fix crash on second migration when cancel early

Marc-André reported an issue on QEMU crash when retrying a cancelled migration during early setup phase, see "Link:" for more information, and also easy way to reproduce. This patch is a replacement of the prior fix proposed by not only switching to migration_cleanup(), but also fixing it from CPR side, so that we track hup_source properly to know if src QEMU is waiting or the HUP signal. To put it simple: this chunk of special casing in migration_cancel() should not affect normal migration, but only cpr-transfer migration to cover the small window when the src QEMU is waiting for a HUP signal on cpr channel (so that src QEMU can continue the migration on the main channel). To achieve that, we'll also need to remember to detach the hup_source whenenver invoked: after that point, we should always be able to cleanup the migration. It's not a generic operation to explicitly detach a gsource from its context while in its dispatch() function. But it should be safe, because gsource disptch() will only happen with a boosted refcount for the dispatcher so that the gsource will not be freed until the callback completes. It's also safe to return G_SOURCE_REMOVE after the gsource is detached, as glib will simply ignore the G_SOURCE_REMOVE. One can refer to latest 2.86.5 glib code in g_main_dispatch() for that: https://github.com/GNOME/glib/blob/2.86.5/glib/gmain.c#L3592 When at this, add a bunch of assertions to make sure nothing surprises us. After this patch applied, the 2nd migration will not crash QEMU, instead it'll be in CANCELLING until the socket connection times out (it will take ~2min on my Fedora default kernel). During this process no 2nd migration will be allowed, and after it timed out migration can be restarted. It's because so far we don't have control over socket_connect_outgoing(), or anything yet managed by a task executed in qio_task_run_in_thread(). Speeding up the cancellation to be left for future. I also tested cpr-transfer by only providing cpr channel not the main channel (with -incoming defer), kickoff migration on source, then cancel it on source directly without providing the main channel. It keeps working. I wanted to add an unit test for that but it'll need to refactor current cpr-transfer tests first; let's leave it for later. Link: https://lore.kernel.org/r/20260417184742.293061-1-marcandre.lureau@redhat.com Reported-by: Marc-André Lureau <marcandre.lureau@redhat.com> Tested-by: Fabiano Rosas <farosas@suse.de> Reviewed-by: Fabiano Rosas <farosas@suse.de> Link: https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com Signed-off-by: Peter Xu <peterx@redhat.com>

Peter Xu committed Apr 21, 2026 at 13:58 UTC 4f08b870f8fd3a18e718d359206234e971aaa195
4 files changed +39 -8
include/migration/cpr.h
+1
@@ -57,6 +57,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp);
57 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
58 void *opaque);
59 void cpr_transfer_source_destroy(MigrationState *s);
60 +bool cpr_transfer_source_active(MigrationState *s);
61
62 void cpr_exec_init(void);
63 QEMUFile *cpr_exec_output(Error **errp);
migration/cpr-transfer.c
+10
@@ -6,6 +6,7 @@
6 */
7
8 #include "qemu/osdep.h"
9 +#include "qemu/main-loop.h"
10 #include "qapi/clone-visitor.h"
11 #include "qapi/error.h"
12 #include "qapi/qapi-visit-migration.h"
@@ -79,6 +80,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)
80 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
81 void *opaque)
82 {
83 + assert(bql_locked());
84 s->hup_source = qio_channel_create_watch(cpr_state_ioc(), G_IO_HUP);
85 g_source_set_callback(s->hup_source,
86 (GSourceFunc)func,
@@ -89,9 +91,17 @@ void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
91
92 void cpr_transfer_source_destroy(MigrationState *s)
93 {
94 + assert(bql_locked());
95 if (s->hup_source) {
96 g_source_destroy(s->hup_source);
97 g_source_unref(s->hup_source);
98 s->hup_source = NULL;
99 }
100 }
101 +
102 +bool cpr_transfer_source_active(MigrationState *s)
103 +{
104 + /* Whenever the HUP gsource is available, it's active. */
105 + assert(bql_locked());
106 + return s->hup_source;
107 +}
migration/migration.c
+23 -8
@@ -1502,14 +1502,19 @@ void migration_cancel(void)
1502 }
1503
1504 /*
1505 - * If migration_connect_outgoing has not been called, then there
1506 - * is no path that will complete the cancellation. Do it now.
1507 - */
1508 - if (setup && !s->to_dst_file) {
1509 - migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
1510 - MIGRATION_STATUS_CANCELLED);
1511 - cpr_state_close();
1512 - cpr_transfer_source_destroy(s);
1505 + * This is cpr-transfer specific processing.
1506 + *
1507 + * If this is true, it means cpr-transfer migration is waiting for the
1508 + * destination to send HUP event on CPR channel to continue the next
1509 + * phase. If so, do the cleanup proactively to avoid get stuck in
1510 + * CANCELLING state.
1511 + */
1512 + if (cpr_transfer_source_active(s)) {
1513 + assert(migrate_mode() == MIG_MODE_CPR_TRANSFER);
1514 + assert(setup && !s->to_dst_file);
1515 + migration_cleanup(s);
1516 + /* Now all things should have been released */
1517 + assert(!cpr_transfer_source_active(s));
1518 }
1519 }
1520
@@ -2045,12 +2050,22 @@ static gboolean migration_connect_outgoing_cb(QIOChannel *channel,
2050 MigrationState *s = migrate_get_current();
2051 Error *local_err = NULL;
2052
2053 + /*
2054 + * Detach and release the GSource right after use. We rely on this to
2055 + * detect this small cpr-transfer window of "waiting for HUP event".
2056 + */
2057 + cpr_transfer_source_destroy(s);
2058 +
2059 migration_connect_outgoing(s, opaque, &local_err);
2060
2061 if (local_err) {
2062 migration_connect_error_propagate(s, local_err);
2063 }
2064
2065 + /*
2066 + * This is redundant as we do cpr_transfer_source_destroy() at the
2067 + * entry, but it's benign; glib will just skip the detach.
2068 + */
2069 return G_SOURCE_REMOVE;
2070 }
2071
migration/migration.h
+5
@@ -512,6 +512,11 @@ struct MigrationState {
512
513 bool postcopy_package_loaded;
514
515 + /*
516 + * When set, it means cpr-transfer is waiting for the HUP signal from
517 + * destination to continue the 2nd step of migration via the main
518 + * channel.
519 + */
520 GSource *hup_source;
521
522 /*