Skip to contentSkip to conversation

migration: Fix crash on second migration when cancel early

PATCH In review Peter Xu 2 participants 8 messages Apr 21 – May 7

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

PATCH 1/1 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.

---
 include/migration/cpr.h  |  1 +
 migration/migration.h    |  5 +++++
 migration/cpr-transfer.c | 10 ++++++++++
 migration/migration.c    | 31 +++++++++++++++++++++++--------
 4 files changed, 39 insertions(+), 8 deletions(-)
include/migration/cpr.h +1 -0
index 5850fd1788..ebf09a2f0a 100644
@@ -57,6 +57,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp);
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque);
 void cpr_transfer_source_destroy(MigrationState *s);
+bool cpr_transfer_source_active(MigrationState *s);
 
 void cpr_exec_init(void);
 QEMUFile *cpr_exec_output(Error **errp);
migration/migration.h +5 -0
index b6888daced..2bc2787480 100644
@@ -514,6 +514,11 @@ struct MigrationState {
     bool postcopy_package_loaded;
     QemuEvent postcopy_package_loaded_event;
 
+    /*
+     * When set, it means cpr-transfer is waiting for the HUP signal from
+     * destination to continue the 2nd step of migration via the main
+     * channel.
+     */
     GSource *hup_source;
 
     /*
migration/cpr-transfer.c +10 -0
index 61d5c9dce2..9defe7bad7 100644
@@ -6,6 +6,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/main-loop.h"
 #include "qapi/clone-visitor.h"
 #include "qapi/error.h"
 #include "qapi/qapi-visit-migration.h"
@@ -79,6 +80,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque)
 {
+    assert(bql_locked());
     s->hup_source = qio_channel_create_watch(cpr_state_ioc(), G_IO_HUP);
     g_source_set_callback(s->hup_source,
                           (GSourceFunc)func,
@@ -89,9 +91,17 @@ void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
 
 void cpr_transfer_source_destroy(MigrationState *s)
 {
+    assert(bql_locked());
     if (s->hup_source) {
         g_source_destroy(s->hup_source);
         g_source_unref(s->hup_source);
         s->hup_source = NULL;
     }
 }
+
+bool cpr_transfer_source_active(MigrationState *s)
+{
+    /* Whenever the HUP gsource is available, it's active. */
+    assert(bql_locked());
+    return s->hup_source;
+}
migration/migration.c +23 -8
index 5c9aaa6e58..58c1e56766 100644
@@ -1469,14 +1469,19 @@ void migration_cancel(void)
     }
 
     /*
-     * If migration_connect_outgoing has not been called, then there
-     * is no path that will complete the cancellation. Do it now.
-     */
-    if (setup && !s->to_dst_file) {
-        migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
-                          MIGRATION_STATUS_CANCELLED);
-        cpr_state_close();
-        cpr_transfer_source_destroy(s);
+     * This is cpr-transfer specific processing.
+     *
+     * If this is true, it means cpr-transfer migration is waiting for the
+     * destination to send HUP event on CPR channel to continue the next
+     * phase.  If so, do the cleanup proactively to avoid get stuck in
+     * CANCELLING state.
+     */
+    if (cpr_transfer_source_active(s)) {
+        assert(migrate_mode() == MIG_MODE_CPR_TRANSFER);
+        assert(setup && !s->to_dst_file);
+        migration_cleanup(s);
+        /* Now all things should have been released */
+        assert(!cpr_transfer_source_active(s));
     }
 }
 
@@ -2009,12 +2014,22 @@ static gboolean migration_connect_outgoing_cb(QIOChannel *channel,
     MigrationState *s = migrate_get_current();
     Error *local_err = NULL;
 
+    /*
+     * Detach and release the GSource right after use.  We rely on this to
+     * detect this small cpr-transfer window of "waiting for HUP event".
+     */
+    cpr_transfer_source_destroy(s);
+
     migration_connect_outgoing(s, opaque, &local_err);
 
     if (local_err) {
         migration_connect_error_propagate(s, local_err);
     }
 
+    /*
+     * This is redundant as we do cpr_transfer_source_destroy() at the
+     * entry, but it's benign; glib will just skip the detach.
+     */
     return G_SOURCE_REMOVE;
 }
 
Signature · 1 line
2.53.0
#1 lore raw
Quoted 97 lines · Peter Xu

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.

---
 include/migration/cpr.h  |  1 +
 migration/migration.h    |  5 +++++
 migration/cpr-transfer.c | 10 ++++++++++
 migration/migration.c    | 31 +++++++++++++++++++++++--------
 4 files changed, 39 insertions(+), 8 deletions(-)
include/migration/cpr.h +1 -0
index 5850fd1788..ebf09a2f0a 100644
@@ -57,6 +57,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp);
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque);
 void cpr_transfer_source_destroy(MigrationState *s);
+bool cpr_transfer_source_active(MigrationState *s);
 
 void cpr_exec_init(void);
 QEMUFile *cpr_exec_output(Error **errp);
migration/migration.h +5 -0
index b6888daced..2bc2787480 100644
@@ -514,6 +514,11 @@ struct MigrationState {
     bool postcopy_package_loaded;
     QemuEvent postcopy_package_loaded_event;
 
+    /*
+     * When set, it means cpr-transfer is waiting for the HUP signal from
+     * destination to continue the 2nd step of migration via the main
+     * channel.
+     */
     GSource *hup_source;
 
     /*
migration/cpr-transfer.c +1 -0
index 61d5c9dce2..9defe7bad7 100644
@@ -6,6 +6,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/main-loop.h"
 #include "qapi/clone-visitor.h"
 #include "qapi/error.h"
 #include "qapi/qapi-visit-migration.h"

migration/cpr-transfer.c QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp) · line 84

Quoted 5 lines
migration/cpr-transfer.c +1 -0
@@ -79,6 +80,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque)
 {
+    assert(bql_locked());
     s->hup_source = qio_channel_create_watch(cpr_state_ioc(), G_IO_HUP);

Before I review the patch in detail, let me just make a high level comment here:

I wonder if we should have a "register" in the iochannel layer for the several watches we create. So we could at migration_cancel time call g_clear_handle/g_source_destroy on all at the same time. The management of these source ids is getting too particular.

I'm seeing that exec, fd and file migration all ignore the id returned by channel-watch.c. In the case of exec this is causing qio_channel_command_finalize() to be skipped, leaving the exec'ed command process behind! So what I did as an experiment was to register watches like this:

  static inline void migration_watch_data(void) {};
  
  qio_channel_add_watch_full(ioc, G_IO_IN, exec_accept_incoming_migration,
                             migration_watch_data, NULL,
                             g_main_context_get_thread_default());

and at migrate_cancel():

  while(g_source_remove_by_user_data(migration_watch_data));

It feels to me that a wrapper around this, or even a hashtable of "func ptr->GSource" or "str->GSource id" would allow us to call a (say) qio_channel_clear_watches() and avoid having to do the management in each of the clients.

We already have stuff like IOWatchPoll which kind of already wraps the watch creation in a sense.

Quoted 74 lines
migration/cpr-transfer.c +8 -0
     g_source_set_callback(s->hup_source,
                           (GSourceFunc)func,
@@ -89,9 +91,17 @@ void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
 
 void cpr_transfer_source_destroy(MigrationState *s)
 {
+    assert(bql_locked());
     if (s->hup_source) {
         g_source_destroy(s->hup_source);
         g_source_unref(s->hup_source);
         s->hup_source = NULL;
     }
 }
+
+bool cpr_transfer_source_active(MigrationState *s)
+{
+    /* Whenever the HUP gsource is available, it's active. */
+    assert(bql_locked());
+    return s->hup_source;
+}
migration/migration.c +23 -8
index 5c9aaa6e58..58c1e56766 100644
@@ -1469,14 +1469,19 @@ void migration_cancel(void)
     }
 
     /*
-     * If migration_connect_outgoing has not been called, then there
-     * is no path that will complete the cancellation. Do it now.
-     */
-    if (setup && !s->to_dst_file) {
-        migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
-                          MIGRATION_STATUS_CANCELLED);
-        cpr_state_close();
-        cpr_transfer_source_destroy(s);
+     * This is cpr-transfer specific processing.
+     *
+     * If this is true, it means cpr-transfer migration is waiting for the
+     * destination to send HUP event on CPR channel to continue the next
+     * phase.  If so, do the cleanup proactively to avoid get stuck in
+     * CANCELLING state.
+     */
+    if (cpr_transfer_source_active(s)) {
+        assert(migrate_mode() == MIG_MODE_CPR_TRANSFER);
+        assert(setup && !s->to_dst_file);
+        migration_cleanup(s);
+        /* Now all things should have been released */
+        assert(!cpr_transfer_source_active(s));
     }
 }
 
@@ -2009,12 +2014,22 @@ static gboolean migration_connect_outgoing_cb(QIOChannel *channel,
     MigrationState *s = migrate_get_current();
     Error *local_err = NULL;
 
+    /*
+     * Detach and release the GSource right after use.  We rely on this to
+     * detect this small cpr-transfer window of "waiting for HUP event".
+     */
+    cpr_transfer_source_destroy(s);
+
     migration_connect_outgoing(s, opaque, &local_err);
 
     if (local_err) {
         migration_connect_error_propagate(s, local_err);
     }
 
+    /*
+     * This is redundant as we do cpr_transfer_source_destroy() at the
+     * entry, but it's benign; glib will just skip the detach.
+     */
     return G_SOURCE_REMOVE;
 }
#2 lore raw
Quoted 113 lines · Fabiano Rosas
Quoted 97 lines · Peter Xu

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.

---
 include/migration/cpr.h  |  1 +
 migration/migration.h    |  5 +++++
 migration/cpr-transfer.c | 10 ++++++++++
 migration/migration.c    | 31 +++++++++++++++++++++++--------
 4 files changed, 39 insertions(+), 8 deletions(-)
include/migration/cpr.h +1 -0
index 5850fd1788..ebf09a2f0a 100644
@@ -57,6 +57,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp);
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque);
 void cpr_transfer_source_destroy(MigrationState *s);
+bool cpr_transfer_source_active(MigrationState *s);
 
 void cpr_exec_init(void);
 QEMUFile *cpr_exec_output(Error **errp);
migration/migration.h +5 -0
index b6888daced..2bc2787480 100644
@@ -514,6 +514,11 @@ struct MigrationState {
     bool postcopy_package_loaded;
     QemuEvent postcopy_package_loaded_event;
 
+    /*
+     * When set, it means cpr-transfer is waiting for the HUP signal from
+     * destination to continue the 2nd step of migration via the main
+     * channel.
+     */
     GSource *hup_source;
 
     /*
migration/cpr-transfer.c +1 -0
index 61d5c9dce2..9defe7bad7 100644
@@ -6,6 +6,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/main-loop.h"
 #include "qapi/clone-visitor.h"
 #include "qapi/error.h"
 #include "qapi/qapi-visit-migration.h"

migration/cpr-transfer.c QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp) · line 84

Quoted 5 lines
migration/cpr-transfer.c +1 -0
@@ -79,6 +80,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque)
 {
+    assert(bql_locked());
     s->hup_source = qio_channel_create_watch(cpr_state_ioc(), G_IO_HUP);

Before I review the patch in detail, let me just make a high level comment here:

I wonder if we should have a "register" in the iochannel layer for the several watches we create. So we could at migration_cancel time call g_clear_handle/g_source_destroy on all at the same time. The management of these source ids is getting too particular.

Quoted 7 lines

I'm seeing that exec, fd and file migration all ignore the id returned by channel-watch.c. In the case of exec this is causing qio_channel_command_finalize() to be skipped, leaving the exec'ed command process behind! So what I did as an experiment was to register watches like this:

  static inline void migration_watch_data(void) {};

Oops, not inline.

author
#3 lore raw
In reply to Fabiano Rosas
Quoted 32 lines · Fabiano Rosas
Quoted 30 lines · Peter Xu

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.

Quoted 8 lines
Quoted 8 lines

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.

[1]

Quoted 68 lines
Quoted 59 lines

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.

---
 include/migration/cpr.h  |  1 +
 migration/migration.h    |  5 +++++
 migration/cpr-transfer.c | 10 ++++++++++
 migration/migration.c    | 31 +++++++++++++++++++++++--------
 4 files changed, 39 insertions(+), 8 deletions(-)
include/migration/cpr.h +1 -0
index 5850fd1788..ebf09a2f0a 100644
@@ -57,6 +57,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp);
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque);
 void cpr_transfer_source_destroy(MigrationState *s);
+bool cpr_transfer_source_active(MigrationState *s);
 
 void cpr_exec_init(void);
 QEMUFile *cpr_exec_output(Error **errp);
migration/migration.h +5 -0
index b6888daced..2bc2787480 100644
@@ -514,6 +514,11 @@ struct MigrationState {
     bool postcopy_package_loaded;
     QemuEvent postcopy_package_loaded_event;
 
+    /*
+     * When set, it means cpr-transfer is waiting for the HUP signal from
+     * destination to continue the 2nd step of migration via the main
+     * channel.
+     */
     GSource *hup_source;
 
     /*
migration/cpr-transfer.c +1 -0
index 61d5c9dce2..9defe7bad7 100644
@@ -6,6 +6,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/main-loop.h"
 #include "qapi/clone-visitor.h"
 #include "qapi/error.h"
 #include "qapi/qapi-visit-migration.h"

migration/cpr-transfer.c QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp) · line 84

Quoted 5 lines
migration/cpr-transfer.c +1 -0
@@ -79,6 +80,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque)
 {
+    assert(bql_locked());
     s->hup_source = qio_channel_create_watch(cpr_state_ioc(), G_IO_HUP);

Before I review the patch in detail, let me just make a high level comment here:

Quoted 2 lines

I wonder if we should have a "register" in the iochannel layer for the several watches we create. So we could at migration_cancel time call

Yes, we need to do this part of mgmt better either now or at some point..

Said that, just to mention: what you're discussing below seems to be about dest QEMU, not src QEMU.

Here the "unmanaged async tasks" I mentioned above [1] is only about src QEMU. It only applies to socket channels and socket_connect_outgoing(). It is slightly even trickier, IIUC, because instead of creating a watch directly, it creates a pthread and run the task there, then when the task fn() completes that thread will inject one event back to the main event loop. See qio_task_thread_worker() and the @completion gsource.

One thing I can try to do is to work out fast cancellation for sockets here on src side, so we don't need to wait for that 2min timeout.. But it would still be nice to have this fix land first because it fixes a crash.. so it may still be something on top but I can start look into. I actually don't know how frequent users will suffer from the 2min timeout: normally when the host:port isn't available we should just get disconnected fast.

Starting from now, I'll only discuss about dest QEMU side (IOW, may not be directly relevant to this patch).

Quoted 7 lines

g_clear_handle/g_source_destroy on all at the same time. The management of these source ids is getting too particular.

I'm seeing that exec, fd and file migration all ignore the id returned by channel-watch.c. In the case of exec this is causing qio_channel_command_finalize() to be skipped, leaving the exec'ed command process behind! So what I did as an experiment was to register

Are we?

We're on the same page at least on that the gsources are not yet managed. But I am not sure they're leaked.

exec_connect_incoming() does qio_channel_add_watch_full(), within itself it will release the refcount of the IO watch gsource. It means it'll be a dangling gsource on the main context. So after the spawn of the new process, it will still be finalized properly?

Quoted 8 lines

watches like this:

  static inline void migration_watch_data(void) {};
  
  qio_channel_add_watch_full(ioc, G_IO_IN, exec_accept_incoming_migration,
                             migration_watch_data, NULL,
                             g_main_context_get_thread_default());
Quoted 8 lines

and at migrate_cancel():

  while(g_source_remove_by_user_data(migration_watch_data));

It feels to me that a wrapper around this, or even a hashtable of "func ptr->GSource" or "str->GSource id" would allow us to call a (say) qio_channel_clear_watches() and avoid having to do the management in each of the clients.

In general, this whole idea sounds reasonable. I just want to check with you on which side of QEMU we're talking about.

To me, dest QEMU is less of a concern when it can easily be killed. But still it's good to be able to manage those. Src QEMU is more important from that perspective.

Quoted 79 lines

We already have stuff like IOWatchPoll which kind of already wraps the watch creation in a sense.

Quoted 74 lines
migration/cpr-transfer.c +8 -0
     g_source_set_callback(s->hup_source,
                           (GSourceFunc)func,
@@ -89,9 +91,17 @@ void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
 
 void cpr_transfer_source_destroy(MigrationState *s)
 {
+    assert(bql_locked());
     if (s->hup_source) {
         g_source_destroy(s->hup_source);
         g_source_unref(s->hup_source);
         s->hup_source = NULL;
     }
 }
+
+bool cpr_transfer_source_active(MigrationState *s)
+{
+    /* Whenever the HUP gsource is available, it's active. */
+    assert(bql_locked());
+    return s->hup_source;
+}
migration/migration.c +23 -8
index 5c9aaa6e58..58c1e56766 100644
@@ -1469,14 +1469,19 @@ void migration_cancel(void)
     }
 
     /*
-     * If migration_connect_outgoing has not been called, then there
-     * is no path that will complete the cancellation. Do it now.
-     */
-    if (setup && !s->to_dst_file) {
-        migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
-                          MIGRATION_STATUS_CANCELLED);
-        cpr_state_close();
-        cpr_transfer_source_destroy(s);
+     * This is cpr-transfer specific processing.
+     *
+     * If this is true, it means cpr-transfer migration is waiting for the
+     * destination to send HUP event on CPR channel to continue the next
+     * phase.  If so, do the cleanup proactively to avoid get stuck in
+     * CANCELLING state.
+     */
+    if (cpr_transfer_source_active(s)) {
+        assert(migrate_mode() == MIG_MODE_CPR_TRANSFER);
+        assert(setup && !s->to_dst_file);
+        migration_cleanup(s);
+        /* Now all things should have been released */
+        assert(!cpr_transfer_source_active(s));
     }
 }
 
@@ -2009,12 +2014,22 @@ static gboolean migration_connect_outgoing_cb(QIOChannel *channel,
     MigrationState *s = migrate_get_current();
     Error *local_err = NULL;
 
+    /*
+     * Detach and release the GSource right after use.  We rely on this to
+     * detect this small cpr-transfer window of "waiting for HUP event".
+     */
+    cpr_transfer_source_destroy(s);
+
     migration_connect_outgoing(s, opaque, &local_err);
 
     if (local_err) {
         migration_connect_error_propagate(s, local_err);
     }
 
+    /*
+     * This is redundant as we do cpr_transfer_source_destroy() at the
+     * entry, but it's benign; glib will just skip the detach.
+     */
     return G_SOURCE_REMOVE;
 }
Signature · 1 line
Peter Xu
#4 lore raw
Quoted 112 lines · Peter Xu
Quoted 32 lines · Fabiano Rosas
Quoted 30 lines · Peter Xu

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.

Quoted 8 lines
Quoted 8 lines

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.

[1]

Quoted 68 lines
Quoted 59 lines

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.

---
 include/migration/cpr.h  |  1 +
 migration/migration.h    |  5 +++++
 migration/cpr-transfer.c | 10 ++++++++++
 migration/migration.c    | 31 +++++++++++++++++++++++--------
 4 files changed, 39 insertions(+), 8 deletions(-)
include/migration/cpr.h +1 -0
index 5850fd1788..ebf09a2f0a 100644
@@ -57,6 +57,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp);
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque);
 void cpr_transfer_source_destroy(MigrationState *s);
+bool cpr_transfer_source_active(MigrationState *s);
 
 void cpr_exec_init(void);
 QEMUFile *cpr_exec_output(Error **errp);
migration/migration.h +5 -0
index b6888daced..2bc2787480 100644
@@ -514,6 +514,11 @@ struct MigrationState {
     bool postcopy_package_loaded;
     QemuEvent postcopy_package_loaded_event;
 
+    /*
+     * When set, it means cpr-transfer is waiting for the HUP signal from
+     * destination to continue the 2nd step of migration via the main
+     * channel.
+     */
     GSource *hup_source;
 
     /*
migration/cpr-transfer.c +1 -0
index 61d5c9dce2..9defe7bad7 100644
@@ -6,6 +6,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/main-loop.h"
 #include "qapi/clone-visitor.h"
 #include "qapi/error.h"
 #include "qapi/qapi-visit-migration.h"

migration/cpr-transfer.c QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp) · line 84

Quoted 5 lines
migration/cpr-transfer.c +1 -0
@@ -79,6 +80,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque)
 {
+    assert(bql_locked());
     s->hup_source = qio_channel_create_watch(cpr_state_ioc(), G_IO_HUP);

Before I review the patch in detail, let me just make a high level comment here:

Quoted 8 lines
Quoted 2 lines

I wonder if we should have a "register" in the iochannel layer for the several watches we create. So we could at migration_cancel time call

Yes, we need to do this part of mgmt better either now or at some point..

Said that, just to mention: what you're discussing below seems to be about dest QEMU, not src QEMU.

Indeed, I haven't looked closely into this patch yet.

Quoted 20 lines

Here the "unmanaged async tasks" I mentioned above [1] is only about src QEMU. It only applies to socket channels and socket_connect_outgoing(). It is slightly even trickier, IIUC, because instead of creating a watch directly, it creates a pthread and run the task there, then when the task fn() completes that thread will inject one event back to the main event loop. See qio_task_thread_worker() and the @completion gsource.

One thing I can try to do is to work out fast cancellation for sockets here on src side, so we don't need to wait for that 2min timeout.. But it would still be nice to have this fix land first because it fixes a crash.. so it may still be something on top but I can start look into. I actually don't know how frequent users will suffer from the 2min timeout: normally when the host:port isn't available we should just get disconnected fast.

Starting from now, I'll only discuss about dest QEMU side (IOW, may not be directly relevant to this patch).

Quoted 3 lines

g_clear_handle/g_source_destroy on all at the same time. The management of these source ids is getting too particular.

Quoted 7 lines
Quoted 4 lines

I'm seeing that exec, fd and file migration all ignore the id returned by channel-watch.c. In the case of exec this is causing qio_channel_command_finalize() to be skipped, leaving the exec'ed command process behind! So what I did as an experiment was to register

Are we?

Yes.

Quoted 8 lines

We're on the same page at least on that the gsources are not yet managed. But I am not sure they're leaked.

exec_connect_incoming() does qio_channel_add_watch_full(), within itself it will release the refcount of the IO watch gsource. It means it'll be a dangling gsource on the main context. So after the spawn of the new process, it will still be finalized properly?

When the migration_with_exec functional test fails [0], the test issues qmp-quit to both src and dst. The dst will exit before ever having dispatched the gsource and exec_accept_incoming_migration() is not executed at all. In that case the gsource and the ioc are never freed and the spawned process is left behind.

This oneliner triggers it for me:

for i in $(seq 1 1000); do \
echo "$i ============="; \
make -j$(nproc) check-func-quick || break; done; ps aux | grep socat

[0] - due to the startup race as the AI overlords told us. Patch coming soon!

Quoted 17 lines
Quoted 8 lines

watches like this:

  static inline void migration_watch_data(void) {};
  
  qio_channel_add_watch_full(ioc, G_IO_IN, exec_accept_incoming_migration,
                             migration_watch_data, NULL,
                             g_main_context_get_thread_default());
Quoted 8 lines

and at migrate_cancel():

  while(g_source_remove_by_user_data(migration_watch_data));

It feels to me that a wrapper around this, or even a hashtable of "func ptr->GSource" or "str->GSource id" would allow us to call a (say) qio_channel_clear_watches() and avoid having to do the management in each of the clients.

Quoted 7 lines

In general, this whole idea sounds reasonable. I just want to check with you on which side of QEMU we're talking about.

To me, dest QEMU is less of a concern when it can easily be killed. But still it's good to be able to manage those. Src QEMU is more important from that perspective.

It applies to both src and dst. Whenever we have to add a watch. The non-uniformity of having the source removed maybe after it dispatches if we return G_SOURCE_REMOVE or maybe it doesn't dispatch and then it needs to be explicitly removed is error prone I think.

Looking at callers of qio_channel_add_watch[_full], all of them just store the gsource "tag" and later remove it. Having the callers each implement their own way of keeping track of the GSource pointer/id just to be able to free them later on seems unnecessary to me.

The caller could still decide when to destroy the source, but it could have semantics more like: "I'm done with all the sources".

Or just make it part of the iochannel finalize routine. A small list of source ids per channel seems reasonable.

Quoted 79 lines
Quoted 79 lines

We already have stuff like IOWatchPoll which kind of already wraps the watch creation in a sense.

Quoted 74 lines
migration/cpr-transfer.c +8 -0
     g_source_set_callback(s->hup_source,
                           (GSourceFunc)func,
@@ -89,9 +91,17 @@ void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
 
 void cpr_transfer_source_destroy(MigrationState *s)
 {
+    assert(bql_locked());
     if (s->hup_source) {
         g_source_destroy(s->hup_source);
         g_source_unref(s->hup_source);
         s->hup_source = NULL;
     }
 }
+
+bool cpr_transfer_source_active(MigrationState *s)
+{
+    /* Whenever the HUP gsource is available, it's active. */
+    assert(bql_locked());
+    return s->hup_source;
+}
migration/migration.c +23 -8
index 5c9aaa6e58..58c1e56766 100644
@@ -1469,14 +1469,19 @@ void migration_cancel(void)
     }
 
     /*
-     * If migration_connect_outgoing has not been called, then there
-     * is no path that will complete the cancellation. Do it now.
-     */
-    if (setup && !s->to_dst_file) {
-        migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
-                          MIGRATION_STATUS_CANCELLED);
-        cpr_state_close();
-        cpr_transfer_source_destroy(s);
+     * This is cpr-transfer specific processing.
+     *
+     * If this is true, it means cpr-transfer migration is waiting for the
+     * destination to send HUP event on CPR channel to continue the next
+     * phase.  If so, do the cleanup proactively to avoid get stuck in
+     * CANCELLING state.
+     */
+    if (cpr_transfer_source_active(s)) {
+        assert(migrate_mode() == MIG_MODE_CPR_TRANSFER);
+        assert(setup && !s->to_dst_file);
+        migration_cleanup(s);
+        /* Now all things should have been released */
+        assert(!cpr_transfer_source_active(s));
     }
 }
 
@@ -2009,12 +2014,22 @@ static gboolean migration_connect_outgoing_cb(QIOChannel *channel,
     MigrationState *s = migrate_get_current();
     Error *local_err = NULL;
 
+    /*
+     * Detach and release the GSource right after use.  We rely on this to
+     * detect this small cpr-transfer window of "waiting for HUP event".
+     */
+    cpr_transfer_source_destroy(s);
+
     migration_connect_outgoing(s, opaque, &local_err);
 
     if (local_err) {
         migration_connect_error_propagate(s, local_err);
     }
 
+    /*
+     * This is redundant as we do cpr_transfer_source_destroy() at the
+     * entry, but it's benign; glib will just skip the detach.
+     */
     return G_SOURCE_REMOVE;
 }
author
#5 lore raw
Quoted 15 lines · Fabiano Rosas
Quoted 8 lines

We're on the same page at least on that the gsources are not yet managed. But I am not sure they're leaked.

exec_connect_incoming() does qio_channel_add_watch_full(), within itself it will release the refcount of the IO watch gsource. It means it'll be a dangling gsource on the main context. So after the spawn of the new process, it will still be finalized properly?

When the migration_with_exec functional test fails [0], the test issues qmp-quit to both src and dst. The dst will exit before ever having dispatched the gsource and exec_accept_incoming_migration() is not executed at all. In that case the gsource and the ioc are never freed and the spawned process is left behind.

Quoted 8 lines

This oneliner triggers it for me:

for i in $(seq 1 1000); do \
echo "$i ============="; \
make -j$(nproc) check-func-quick || break; done; ps aux | grep socat

[0] - due to the startup race as the AI overlords told us. Patch coming soon!

Ohhhh, so it's about some failure path, ok.

Quoted 31 lines
Quoted 18 lines
Quoted 9 lines

watches like this:

  static inline void migration_watch_data(void) {};
  
  qio_channel_add_watch_full(ioc, G_IO_IN, exec_accept_incoming_migration,
                             migration_watch_data, NULL,
                             g_main_context_get_thread_default());
Quoted 8 lines

and at migrate_cancel():

  while(g_source_remove_by_user_data(migration_watch_data));

It feels to me that a wrapper around this, or even a hashtable of "func ptr->GSource" or "str->GSource id" would allow us to call a (say) qio_channel_clear_watches() and avoid having to do the management in each of the clients.

Quoted 7 lines

In general, this whole idea sounds reasonable. I just want to check with you on which side of QEMU we're talking about.

To me, dest QEMU is less of a concern when it can easily be killed. But still it's good to be able to manage those. Src QEMU is more important from that perspective.

It applies to both src and dst. Whenever we have to add a watch. The non-uniformity of having the source removed maybe after it dispatches if we return G_SOURCE_REMOVE or maybe it doesn't dispatch and then it needs to be explicitly removed is error prone I think.

Quoted 4 lines

Looking at callers of qio_channel_add_watch[_full], all of them just store the gsource "tag" and later remove it. Having the callers each implement their own way of keeping track of the GSource pointer/id just to be able to free them later on seems unnecessary to me.

Yes, and we also need to keep in mind that the ID can be reused by glib context right after removal of the gsource from the context, afaik.

Example I randomly picked: qio_channel_websock_handshake_io(), it remembers the ID into hs_io_tag but it also needs to be very careful in its callback function so that whenever qio_channel_websock_handshake_send() would return FALSE (which should really be G_SOURCE_REMOVE..) it must reset hs_io_tag.

Quoted 6 lines

The caller could still decide when to destroy the source, but it could have semantics more like: "I'm done with all the sources".

Or just make it part of the iochannel finalize routine. A small list of source ids per channel seems reasonable.

Sounds good in general.

But one thing to mention is, IIUC even with this it won't fix the problem you hit above with leftover spawned process and IOC.. because IOC's finalize() won't get called..

Signature · 1 line
Peter Xu
Tested-by Reviewed-by on v1 1/1
#6 lore raw
In reply to Peter Xu
Quoted 166 lines · Peter Xu

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.

---
 include/migration/cpr.h  |  1 +
 migration/migration.h    |  5 +++++
 migration/cpr-transfer.c | 10 ++++++++++
 migration/migration.c    | 31 +++++++++++++++++++++++--------
 4 files changed, 39 insertions(+), 8 deletions(-)
include/migration/cpr.h +1 -0
index 5850fd1788..ebf09a2f0a 100644
@@ -57,6 +57,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp);
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque);
 void cpr_transfer_source_destroy(MigrationState *s);
+bool cpr_transfer_source_active(MigrationState *s);
 
 void cpr_exec_init(void);
 QEMUFile *cpr_exec_output(Error **errp);
migration/migration.h +5 -0
index b6888daced..2bc2787480 100644
@@ -514,6 +514,11 @@ struct MigrationState {
     bool postcopy_package_loaded;
     QemuEvent postcopy_package_loaded_event;
 
+    /*
+     * When set, it means cpr-transfer is waiting for the HUP signal from
+     * destination to continue the 2nd step of migration via the main
+     * channel.
+     */
     GSource *hup_source;
 
     /*
migration/cpr-transfer.c +10 -0
index 61d5c9dce2..9defe7bad7 100644
@@ -6,6 +6,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/main-loop.h"
 #include "qapi/clone-visitor.h"
 #include "qapi/error.h"
 #include "qapi/qapi-visit-migration.h"
@@ -79,6 +80,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque)
 {
+    assert(bql_locked());
     s->hup_source = qio_channel_create_watch(cpr_state_ioc(), G_IO_HUP);
     g_source_set_callback(s->hup_source,
                           (GSourceFunc)func,
@@ -89,9 +91,17 @@ void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
 
 void cpr_transfer_source_destroy(MigrationState *s)
 {
+    assert(bql_locked());
     if (s->hup_source) {
         g_source_destroy(s->hup_source);
         g_source_unref(s->hup_source);
         s->hup_source = NULL;
     }
 }
+
+bool cpr_transfer_source_active(MigrationState *s)
+{
+    /* Whenever the HUP gsource is available, it's active. */
+    assert(bql_locked());
+    return s->hup_source;
+}
migration/migration.c +19 -8
index 5c9aaa6e58..58c1e56766 100644
@@ -1469,14 +1469,19 @@ void migration_cancel(void)
     }
 
     /*
-     * If migration_connect_outgoing has not been called, then there
-     * is no path that will complete the cancellation. Do it now.
-     */
-    if (setup && !s->to_dst_file) {
-        migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
-                          MIGRATION_STATUS_CANCELLED);
-        cpr_state_close();
-        cpr_transfer_source_destroy(s);
+     * This is cpr-transfer specific processing.
+     *
+     * If this is true, it means cpr-transfer migration is waiting for the
+     * destination to send HUP event on CPR channel to continue the next
+     * phase.  If so, do the cleanup proactively to avoid get stuck in
+     * CANCELLING state.
+     */
+    if (cpr_transfer_source_active(s)) {
+        assert(migrate_mode() == MIG_MODE_CPR_TRANSFER);
+        assert(setup && !s->to_dst_file);
+        migration_cleanup(s);
+        /* Now all things should have been released */
+        assert(!cpr_transfer_source_active(s));
     }
 }
 
@@ -2009,12 +2014,22 @@ static gboolean migration_connect_outgoing_cb(QIOChannel *channel,
     MigrationState *s = migrate_get_current();
     Error *local_err = NULL;
 
+    /*
+     * Detach and release the GSource right after use.  We rely on this to
+     * detect this small cpr-transfer window of "waiting for HUP event".
+     */
+    cpr_transfer_source_destroy(s);
+
     migration_connect_outgoing(s, opaque, &local_err);
 

migration/migration.c static gboolean migration_connect_outgoing_cb(QIOChannel *channel, · line 2034

Quoted 10 lines
migration/migration.c +4 -0
     if (local_err) {
         migration_connect_error_propagate(s, local_err);
     }
 
+    /*
+     * This is redundant as we do cpr_transfer_source_destroy() at the
+     * entry, but it's benign; glib will just skip the detach.
+     */
     return G_SOURCE_REMOVE;
 }

Sorry for taking too long on this, it fell off my queue. Looks good to me. I reproduced the original issue and tested this patch under load with hacked tests:

/mode/transfer/defer + early cancel
/cancel/src/after/setup + 2nd migrate

BTW, I found it weird that we cannot start a second migration on the incoming after the first one having never properly started. Trying to actually do it without shutting down both QEMUs was fun. A bunch of asserts on the incoming side due to yank. After hacking a little and using migrate_recover to start the second migration on the incoming, I managed to make it work, but it hanged mid-transfer for some reason. Probably some iochannel reset needed.

author
#7 lore raw
Quoted 183 lines · Fabiano Rosas
Quoted 166 lines · Peter Xu

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.

---
 include/migration/cpr.h  |  1 +
 migration/migration.h    |  5 +++++
 migration/cpr-transfer.c | 10 ++++++++++
 migration/migration.c    | 31 +++++++++++++++++++++++--------
 4 files changed, 39 insertions(+), 8 deletions(-)
include/migration/cpr.h +1 -0
index 5850fd1788..ebf09a2f0a 100644
@@ -57,6 +57,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp);
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque);
 void cpr_transfer_source_destroy(MigrationState *s);
+bool cpr_transfer_source_active(MigrationState *s);
 
 void cpr_exec_init(void);
 QEMUFile *cpr_exec_output(Error **errp);
migration/migration.h +5 -0
index b6888daced..2bc2787480 100644
@@ -514,6 +514,11 @@ struct MigrationState {
     bool postcopy_package_loaded;
     QemuEvent postcopy_package_loaded_event;
 
+    /*
+     * When set, it means cpr-transfer is waiting for the HUP signal from
+     * destination to continue the 2nd step of migration via the main
+     * channel.
+     */
     GSource *hup_source;
 
     /*
migration/cpr-transfer.c +10 -0
index 61d5c9dce2..9defe7bad7 100644
@@ -6,6 +6,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/main-loop.h"
 #include "qapi/clone-visitor.h"
 #include "qapi/error.h"
 #include "qapi/qapi-visit-migration.h"
@@ -79,6 +80,7 @@ QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp)
 void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
                                 void *opaque)
 {
+    assert(bql_locked());
     s->hup_source = qio_channel_create_watch(cpr_state_ioc(), G_IO_HUP);
     g_source_set_callback(s->hup_source,
                           (GSourceFunc)func,
@@ -89,9 +91,17 @@ void cpr_transfer_add_hup_watch(MigrationState *s, QIOChannelFunc func,
 
 void cpr_transfer_source_destroy(MigrationState *s)
 {
+    assert(bql_locked());
     if (s->hup_source) {
         g_source_destroy(s->hup_source);
         g_source_unref(s->hup_source);
         s->hup_source = NULL;
     }
 }
+
+bool cpr_transfer_source_active(MigrationState *s)
+{
+    /* Whenever the HUP gsource is available, it's active. */
+    assert(bql_locked());
+    return s->hup_source;
+}
migration/migration.c +19 -8
index 5c9aaa6e58..58c1e56766 100644
@@ -1469,14 +1469,19 @@ void migration_cancel(void)
     }
 
     /*
-     * If migration_connect_outgoing has not been called, then there
-     * is no path that will complete the cancellation. Do it now.
-     */
-    if (setup && !s->to_dst_file) {
-        migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING,
-                          MIGRATION_STATUS_CANCELLED);
-        cpr_state_close();
-        cpr_transfer_source_destroy(s);
+     * This is cpr-transfer specific processing.
+     *
+     * If this is true, it means cpr-transfer migration is waiting for the
+     * destination to send HUP event on CPR channel to continue the next
+     * phase.  If so, do the cleanup proactively to avoid get stuck in
+     * CANCELLING state.
+     */
+    if (cpr_transfer_source_active(s)) {
+        assert(migrate_mode() == MIG_MODE_CPR_TRANSFER);
+        assert(setup && !s->to_dst_file);
+        migration_cleanup(s);
+        /* Now all things should have been released */
+        assert(!cpr_transfer_source_active(s));
     }
 }
 
@@ -2009,12 +2014,22 @@ static gboolean migration_connect_outgoing_cb(QIOChannel *channel,
     MigrationState *s = migrate_get_current();
     Error *local_err = NULL;
 
+    /*
+     * Detach and release the GSource right after use.  We rely on this to
+     * detect this small cpr-transfer window of "waiting for HUP event".
+     */
+    cpr_transfer_source_destroy(s);
+
     migration_connect_outgoing(s, opaque, &local_err);
 

migration/migration.c static gboolean migration_connect_outgoing_cb(QIOChannel *channel, · line 2034

Quoted 10 lines
migration/migration.c +4 -0
     if (local_err) {
         migration_connect_error_propagate(s, local_err);
     }
 
+    /*
+     * This is redundant as we do cpr_transfer_source_destroy() at the
+     * entry, but it's benign; glib will just skip the detach.
+     */
     return G_SOURCE_REMOVE;
 }

Sorry for taking too long on this, it fell off my queue. Looks good to me. I reproduced the original issue and tested this patch under load with hacked tests:

Quoted 5 lines

/mode/transfer/defer + early cancel
/cancel/src/after/setup + 2nd migrate

Thanks for double checking those! Then let me pick this patch up.

Quoted 8 lines

BTW, I found it weird that we cannot start a second migration on the incoming after the first one having never properly started. Trying to actually do it without shutting down both QEMUs was fun. A bunch of asserts on the incoming side due to yank. After hacking a little and using migrate_recover to start the second migration on the incoming, I managed to make it work, but it hanged mid-transfer for some reason. Probably some iochannel reset needed.

Yeah, I am guessing we need to first fill the gaps on destination side to properly manage those async tasks on port listens, etc.. and those can be the owners of dangling iochannels.

Signature · 1 line
Peter Xu