Skip to contentSkip to conversation

migration: Fix multifd+postcopy break on qemu-7.2 machine type

PATCH In review Peter Xu 3 participants 17 messages Oct 7 – Oct 15

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

v2 cover migration: Fix multifd+postcopy break on qemu-7.2 machine type

CI run: https://gitlab.com/peterx/qemu/-/pipelines/2918307713

v2 changelog:
- Comments, docs, commit log updates [Fabiano]
- Move migration_channel_peek/identify() out of migration_incoming_channel_install() [Fabiano]
- Make migration_incoming_setup() return success status [Fabiano]
- Remove a redundant CH_POSTCOPY path in migration_channel_identify() [Fabiano]

Currently, upstream is broken with migration with qemu-7.2 machine types, simplest reproducer:

      $ QTEST_QEMU_MACHINE_TYPE=pc-q35-7.2 \
          QTEST_QEMU_BINARY=./qemu-system-x86_64 \
          ./tests/qtest/migration-test --full \
          -r /x86_64/migration/multifd+postcopy/preempt/plain

This series should fix it. Note that this solution will not work for TLS, and due to the fact that TLS is not peekable (which the design of this series currently rely on), TLS is not fixable. But as long as the user doesn't enable all four features (multifd, postcopy, preempt, tls) then it will still work. I have much more info in patch 6, feel free to read.

Patch layout:

Patch 1:    Added one more test for multifd+postcopy recovery non-tls
Patch 2-5:  Something I found either buggy or good to have cleanups
Patch 6:    Fix the problem on incoming side
Patch 7:    Fix the problem on outgoing side

Tests I've done:

- The qtest, concurrent execution of a lot of "migration-test --full", which I normally use to catch race conditions. Nothing pops up.

- Test migration with all three features enabled (multifd, postcopy-ram, postcopy-preempt) on different machine types and binaries combinations,

  |----------+---------+-------------+--------|
  | Machine  | Source  | Destination | Result |
  |----------+---------+-------------+--------|
  | q35-11.2 | master  | master      | PASS   | --+
  |          | master  | patched     | PASS   |   |-----> 11.2 machine not affected
  |          | patched | master      | PASS   |   |
  |          | patched | patched     | PASS   | --+
  |----------+---------+-------------+--------|
  | q35-7.2  | master  | master      | FAIL   | --------> current breakage
  |          | master  | patched     | PASS   | --+
  |          | patched | master      | PASS   |   |-----> patch any side should fix
  |          | patched | patched     | PASS   | --+
  |----------+---------+-------------+--------|

- Manual test of qemu-7.2 binary <-> patched new binary with either (1) multifd migration, or (2) postcopy + preempt migration. NOTE: qemu-7.2 doesn't support enabling all.

Comments welcomed, thanks.

Peter Xu (7):
tests/migration-test: Add multifd+postcopy test for preempt recovery
migration: Add two more trace points for incoming channel mgmt
migration: Fix socket listen backlog
migration: Split migration_channel_process_incoming()
migration: Dump type of channel in set_incoming_channel tracepoint
migration: Fix incoming channel race with multifd and preempt_pre_7_2
migration: Postpone the outgoing preempt channel for preempt_pre_7_2

 migration/channel.h                    |   6 +-
 migration/migration.h                  |  35 ++
 migration/channel.c                    | 446 +++++++++++++++++++++----
 migration/migration.c                  |  67 +++-
 migration/socket.c                     |  25 +-
 tests/qtest/migration/postcopy-tests.c |  11 +
 migration/trace-events                 |   8 +-
 7 files changed, 510 insertions(+), 88 deletions(-)
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH v2 1/7 tests/migration-test: Add multifd+postcopy test for preempt recovery

We have one for TLS, but we don't have the non-TLS version. Add it.

---
 tests/qtest/migration/postcopy-tests.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
tests/qtest/migration/postcopy-tests.c +11 -0
index 13a5759655..6c4fd31152 100644
@@ -89,6 +89,15 @@ static void test_multifd_postcopy_preempt(char *name, MigrateCommon *args)
     test_postcopy_common(args);
 }
 
+static void test_multifd_postcopy_preempt_recovery(char *name,
+                                                   MigrateCommon *args)
+{
+    args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
+    args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT] = true;
+
+    test_postcopy_recovery_common(args, POSTCOPY_FAIL_NONE);
+}
+
 void migration_test_add_postcopy(MigrationTestEnv *env)
 {
     migration_test_add_postcopy_smoke(env);
@@ -113,6 +122,8 @@ void migration_test_add_postcopy(MigrationTestEnv *env)
                            test_multifd_postcopy);
         migration_test_add("/migration/multifd+postcopy/preempt/plain",
                            test_multifd_postcopy_preempt);
+        migration_test_add("/migration/multifd+postcopy/preempt/recovery/plain",
+                           test_multifd_postcopy_preempt_recovery);
         if (env->is_x86) {
             migration_test_add("/migration/postcopy/suspend",
                                test_postcopy_suspend);
Signature · 1 line
2.55.0
Reviewed-by on v2 1/7
#2 lore raw

Re: [PATCH v2 1/7] tests/migration-test: Add multifd+postcopy test for preempt recovery

Quoted 28 lines · Peter Xu

We have one for TLS, but we don't have the non-TLS version. Add it.

---
 tests/qtest/migration/postcopy-tests.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
tests/qtest/migration/postcopy-tests.c +9 -0
index 13a5759655..6c4fd31152 100644
@@ -89,6 +89,15 @@ static void test_multifd_postcopy_preempt(char *name, MigrateCommon *args)
     test_postcopy_common(args);
 }
 
+static void test_multifd_postcopy_preempt_recovery(char *name,
+                                                   MigrateCommon *args)
+{
+    args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
+    args->start.caps[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT] = true;
+
+    test_postcopy_recovery_common(args, POSTCOPY_FAIL_NONE);
+}
+
 void migration_test_add_postcopy(MigrationTestEnv *env)
 {
     migration_test_add_postcopy_smoke(env);

tests/qtest/migration/postcopy-tests.c void migration_test_add_postcopy(MigrationTestEnv *env) · line 129

Quoted 8 lines
tests/qtest/migration/postcopy-tests.c +2 -0
@@ -113,6 +122,8 @@ void migration_test_add_postcopy(MigrationTestEnv *env)
                            test_multifd_postcopy);
         migration_test_add("/migration/multifd+postcopy/preempt/plain",
                            test_multifd_postcopy_preempt);
+        migration_test_add("/migration/multifd+postcopy/preempt/recovery/plain",
+                           test_multifd_postcopy_preempt_recovery);
         if (env->is_x86) {
             migration_test_add("/migration/postcopy/suspend",
                                test_postcopy_suspend);
author
#3 lore raw

PATCH v2 2/7 migration: Add two more trace points for incoming channel mgmt

In reply to Peter Xu

We have trace points for both multifd channel and preempt channel when a new channel is established on the destination, but no main channel. We can normally deduce this by analyzing the socket channel tracepoints, but add one for debugging purpose so in a trace log we can see when all channels are established.

Add one more for start / resume of incoming migration too.

---
 migration/migration.c  | 3 +++
 migration/trace-events | 2 ++
 2 files changed, 5 insertions(+)
migration/migration.c +3 -0
index fab7949fe1..3ffc0c2c6a 100644
@@ -852,6 +852,7 @@ bool migration_incoming_setup(QIOChannel *ioc, uint8_t channel, Error **errp)
         assert(!mis->from_src_file);
         mis->from_src_file = f;
         qemu_file_set_blocking(f, false, &error_abort);
+        trace_migration_main_channel_received();
         break;
 
     case CH_MULTIFD:
@@ -918,6 +919,8 @@ static bool postcopy_try_recover(void)
 
 void migration_start_incoming(void)
 {
+    trace_migration_start_incoming();
+
     if (postcopy_try_recover()) {
         return;
     }
migration/trace-events +2 -0
index db911fe6f2..68bcf12dc7 100644
@@ -200,6 +200,8 @@ postcopy_preempt_enabled(bool value) "%d"
 migration_precopy_complete(void) ""
 migration_call_notifiers(int type) "type=%d"
 migration_request_switchover_ack_legacy(const char *requester, unsigned int switchover_ack_pending_num_legacy) "Requester %s, switchover_ack_pending_num_legacy %u"
+migration_main_channel_received(void) ""
+migration_start_incoming(void) ""
 
 # migration-stats
 migration_transferred_bytes(uint64_t qemu_file, uint64_t multifd, uint64_t rdma) "qemu_file %" PRIu64 " multifd %" PRIu64 " RDMA %" PRIu64
Signature · 1 line
2.55.0
Reviewed-by on v2 2/7
#4 lore raw

Re: [PATCH v2 2/7] migration: Add two more trace points for incoming channel mgmt

Quoted 40 lines · Peter Xu

We have trace points for both multifd channel and preempt channel when a new channel is established on the destination, but no main channel. We can normally deduce this by analyzing the socket channel tracepoints, but add one for debugging purpose so in a trace log we can see when all channels are established.

Add one more for start / resume of incoming migration too.

---
 migration/migration.c  | 3 +++
 migration/trace-events | 2 ++
 2 files changed, 5 insertions(+)
migration/migration.c +3 -0
index fab7949fe1..3ffc0c2c6a 100644
@@ -852,6 +852,7 @@ bool migration_incoming_setup(QIOChannel *ioc, uint8_t channel, Error **errp)
         assert(!mis->from_src_file);
         mis->from_src_file = f;
         qemu_file_set_blocking(f, false, &error_abort);
+        trace_migration_main_channel_received();
         break;
 
     case CH_MULTIFD:
@@ -918,6 +919,8 @@ static bool postcopy_try_recover(void)
 
 void migration_start_incoming(void)
 {
+    trace_migration_start_incoming();
+
     if (postcopy_try_recover()) {
         return;
     }
migration/trace-events +0 -0
index db911fe6f2..68bcf12dc7 100644

migration/trace-events postcopy_preempt_enabled(bool value) "%d" · line 207

Quoted 8 lines
migration/trace-events +2 -0
@@ -200,6 +200,8 @@ postcopy_preempt_enabled(bool value) "%d"
 migration_precopy_complete(void) ""
 migration_call_notifiers(int type) "type=%d"
 migration_request_switchover_ack_legacy(const char *requester, unsigned int switchover_ack_pending_num_legacy) "Requester %s, switchover_ack_pending_num_legacy %u"
+migration_main_channel_received(void) ""
+migration_start_incoming(void) ""
 
 # migration-stats
 migration_transferred_bytes(uint64_t qemu_file, uint64_t multifd, uint64_t rdma) "qemu_file %" PRIu64 " multifd %" PRIu64 " RDMA %" PRIu64
author
#5 lore raw

PATCH v2 3/7 migration: Fix socket listen backlog

In reply to Peter Xu

QEMU incoming side may have set a wrong listen() backlog. After the support of multifd + postcopy now we have four possibilities on setting the backlog, some of them are wrong:

  Vanilla:            1 (correct)
  Multifd:            N (wrong, should be N+1)
  Preempt:            2 (correct)
  Multifd+Preempt:    N (wrong, should be N+2)

No real report for this, found by code observation only. Logically the backlog (pending accept requests on destination on the listening sockets) needs to be the maximum of possible channels. Otherwise if the accept queue full on destination, source QEMU may hit connection errors.

Cc Daniel P. Berrangé <[email protected]>
Fixes 0705e56496 ("multifd: Use number of channels as listen backlog")
Fixes e274188612 ("migration: enable multifd and postcopy together")
---
 migration/socket.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
migration/socket.c +18 -7
index f57f1a8e7c..31bfa1eeeb 100644
@@ -139,21 +139,32 @@ socket_incoming_migration_end(void *opaque)
     object_unref(OBJECT(listener));
 }
 
+static int socket_get_max_channels(void)
+{
+    /* The main channel is always wanted */
+    int num = 1;
+
+    if (migrate_multifd()) {
+        num += migrate_multifd_channels();
+    }
+
+    if (migrate_postcopy_preempt()) {
+        /* The preempt channel */
+        num += 1;
+    }
+
+    return num;
+}
+
 void socket_connect_incoming(SocketAddress *saddr, Error **errp)
 {
     QIONetListener *listener = qio_net_listener_new();
     MigrationIncomingState *mis = migration_incoming_get_current();
     size_t i;
-    int num = 1;
+    int num = socket_get_max_channels();
 
     qio_net_listener_set_name(listener, "migration-socket-listener");
 
-    if (migrate_multifd()) {
-        num = migrate_multifd_channels();
-    } else if (migrate_postcopy_preempt()) {
-        num = RAM_CHANNEL_MAX;
-    }
-
     if (qio_net_listener_open_sync(listener, saddr, num, errp) < 0) {
         object_unref(OBJECT(listener));
         return;
Signature · 1 line
2.55.0
Reviewed-by on v2 3/7
#6 lore raw

Re: [PATCH v2 3/7] migration: Fix socket listen backlog

Quoted 9 lines · Peter Xu

QEMU incoming side may have set a wrong listen() backlog. After the support of multifd + postcopy now we have four possibilities on setting the backlog, some of them are wrong:

  Vanilla:            1 (correct)
  Multifd:            N (wrong, should be N+1)
  Preempt:            2 (correct)
  Multifd+Preempt:    N (wrong, should be N+2)
Quoted 4 lines

No real report for this, found by code observation only. Logically the backlog (pending accept requests on destination on the listening sockets) needs to be the maximum of possible channels. Otherwise if the accept queue full on destination, source QEMU may hit connection errors.

From a robustness POV, we might also consider retrying connections if we see a transient failure. Though I'm not sure offhand what errrno would result on the client when the accept() queue is full, hopefully there's something distinct.

This would mitigate a risk of DoS from an unauthorized client making its own incoming connections concurrently. Such a client is not necessarily malicious, as there are monitoring tools or port scanners that randomly open connections to TCP listener sockets.

Quoted 7 lines
Cc Daniel P. Berrangé <[email protected]>
Fixes 0705e56496 ("multifd: Use number of channels as listen backlog")
Fixes e274188612 ("migration: enable multifd and postcopy together")
---
 migration/socket.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
author
#7 lore raw

Re: [PATCH v2 3/7] migration: Fix socket listen backlog

Quoted 15 lines · Daniel P. Berrangé
Quoted 9 lines · Peter Xu

QEMU incoming side may have set a wrong listen() backlog. After the support of multifd + postcopy now we have four possibilities on setting the backlog, some of them are wrong:

  Vanilla:            1 (correct)
  Multifd:            N (wrong, should be N+1)
  Preempt:            2 (correct)
  Multifd+Preempt:    N (wrong, should be N+2)
Quoted 4 lines

No real report for this, found by code observation only. Logically the backlog (pending accept requests on destination on the listening sockets) needs to be the maximum of possible channels. Otherwise if the accept queue full on destination, source QEMU may hit connection errors.

Quoted 4 lines

From a robustness POV, we might also consider retrying connections if we see a transient failure. Though I'm not sure offhand what errrno would result on the client when the accept() queue is full, hopefully there's something distinct.

I tried to find but didn't see an explicit errno for it.. The hope is even if hit it will fail migration with some errno, then a retry would work. It'll be similar to when there's other random network issues when starting migration.

Quoted 6 lines

This would mitigate a risk of DoS from an unauthorized client making its own incoming connections concurrently. Such a client is not necessarily malicious, as there are monitoring tools or port scanners that randomly open connections to TCP listener sockets.

I expect ports can be scanned, but I didn't expect adhoc connections then drop quickly..

If that exists, it should be able to break incoming side of qemu with current code indeed. AFAIU what we do is we deliver the channel to migration process and then we'll see qemufile IO error.. not too bad since it will very occasionally fail migration, but source unaffected.

But I agree, I think we should be more error prone. Looks like TLS is not affected due to the fact that TLS handshake process guaranteed anything forwarded to migration core will be verified and solid.

And I believe the migration handshake feature will fix this as a side
effect: when we will attach a header to all channels during handshake, then at least randomly connected sockets will fail the check and then we can drop the channel silently until it passes our header check. I'll keep that in mind when working on the feature, which is in near future plan now.

Quoted 10 lines
Quoted 8 lines
Cc Daniel P. Berrangé <[email protected]>
Fixes 0705e56496 ("multifd: Use number of channels as listen backlog")
Fixes e274188612 ("migration: enable multifd and postcopy together")
---
 migration/socket.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

Thanks,

Signature · 1 line
Peter Xu
Reviewed-by on v2 3/7
#8 lore raw

Re: [PATCH v2 3/7] migration: Fix socket listen backlog

In reply to Peter Xu
Quoted 56 lines · Peter Xu

QEMU incoming side may have set a wrong listen() backlog. After the support of multifd + postcopy now we have four possibilities on setting the backlog, some of them are wrong:

  Vanilla:            1 (correct)
  Multifd:            N (wrong, should be N+1)
  Preempt:            2 (correct)
  Multifd+Preempt:    N (wrong, should be N+2)

No real report for this, found by code observation only. Logically the backlog (pending accept requests on destination on the listening sockets) needs to be the maximum of possible channels. Otherwise if the accept queue full on destination, source QEMU may hit connection errors.

Cc Daniel P. Berrangé <[email protected]>
Fixes 0705e56496 ("multifd: Use number of channels as listen backlog")
Fixes e274188612 ("migration: enable multifd and postcopy together")
---
 migration/socket.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
migration/socket.c +18 -1
index f57f1a8e7c..31bfa1eeeb 100644
@@ -139,21 +139,32 @@ socket_incoming_migration_end(void *opaque)
     object_unref(OBJECT(listener));
 }
 
+static int socket_get_max_channels(void)
+{
+    /* The main channel is always wanted */
+    int num = 1;
+
+    if (migrate_multifd()) {
+        num += migrate_multifd_channels();
+    }
+
+    if (migrate_postcopy_preempt()) {
+        /* The preempt channel */
+        num += 1;
+    }
+
+    return num;
+}
+
 void socket_connect_incoming(SocketAddress *saddr, Error **errp)
 {
     QIONetListener *listener = qio_net_listener_new();
     MigrationIncomingState *mis = migration_incoming_get_current();
     size_t i;
-    int num = 1;
+    int num = socket_get_max_channels();
 
     qio_net_listener_set_name(listener, "migration-socket-listener");

migration/socket.c socket_incoming_migration_end(void *opaque) · line 170

Quoted 10 lines
migration/socket.c +0 -6
 
-    if (migrate_multifd()) {
-        num = migrate_multifd_channels();
-    } else if (migrate_postcopy_preempt()) {
-        num = RAM_CHANNEL_MAX;
-    }
-
     if (qio_net_listener_open_sync(listener, saddr, num, errp) < 0) {
         object_unref(OBJECT(listener));
         return;
author
#9 lore raw

PATCH v2 4/7 migration: Split migration_channel_process_incoming()

In reply to Peter Xu

Introduce two smaller functions for it,

- migration_incoming_channel_install(): peek at channels whenever possible, and install the channel to one of the slots (main, multifd, preempt)

- migration_incoming_error_propagate(): handling an error happened during channel establish phase of an incoming migration.

When at it, move the set channel tracepoint into the 1st helper, replacing the existing tracepoint with trace_migration_channel_process_incoming(). So far, they should fire almost the same time if the channel peek worked. This fact will change in follow up refactorings.

I also moved migration_ioc_register_yank() to be after identify, hopefully it makes more sense: we'd better be careful on registration of yank, or we should be prepared for explosions. Previously it worked, my guess is when error happens we never unregister yank but hang dest QEMU or quit, depending on exit_on_error value.

No real functional change intended.

---
 migration/channel.c    | 57 +++++++++++++++++++++++++++++-------------
 migration/trace-events |  1 +
 2 files changed, 41 insertions(+), 17 deletions(-)
migration/channel.c +40 -17
index 266ae8f776..8407ea46cb 100644
@@ -197,6 +197,42 @@ out:
     return channel;
 }
 
+static void migration_incoming_error_propagate(MigrationIncomingState *mis,
+                                               Error *error)
+{
+    error_report_err(error);
+    migrate_set_state(&mis->state, mis->state, MIGRATION_STATUS_FAILED);
+    if (mis->exit_on_error) {
+        exit(EXIT_FAILURE);
+    }
+}
+
+static bool migration_incoming_channel_install(MigrationIncomingState *mis,
+                                               QIOChannel *ioc,
+                                               Error **errp)
+{
+    MigChannelType ch = migration_channel_identify(mis, ioc, errp);
+
+    if (!ch) {
+        assert(*errp);
+        return false;
+    }
+
+    trace_migration_set_incoming_channel(ioc,
+                                         object_get_typename(OBJECT(ioc)));
+    migration_ioc_register_yank(ioc);
+
+    if (migration_incoming_setup(ioc, ch, errp)) {
+        migration_start_incoming();
+    }
+
+    if (*errp) {
+        return false;
+    }
+
+    return true;
+}
+
 /**
  * @migration_channel_process_incoming - Create new incoming migration channel
  *
@@ -209,31 +245,18 @@ void migration_channel_process_incoming(QIOChannel *ioc)
 {
     MigrationIncomingState *mis = migration_incoming_get_current();
     Error *local_err = NULL;
-    MigChannelType ch;
 
-    trace_migration_set_incoming_channel(
+    trace_migration_channel_process_incoming(
         ioc, object_get_typename(OBJECT(ioc)));
 
     if (migrate_channel_requires_tls_upgrade(ioc)) {
         migration_tls_channel_process_incoming(ioc, &local_err);
     } else {
-        migration_ioc_register_yank(ioc);
-        ch = migration_channel_identify(mis, ioc, &local_err);
-        if (!ch) {
-            goto out;
-        }
-
-        if (migration_incoming_setup(ioc, ch, &local_err)) {
-            migration_start_incoming();
-        }
+        migration_incoming_channel_install(mis, ioc, &local_err);
     }
-out:
+
     if (local_err) {
-        error_report_err(local_err);
-        migrate_set_state(&mis->state, mis->state, MIGRATION_STATUS_FAILED);
-        if (mis->exit_on_error) {
-            exit(EXIT_FAILURE);
-        }
+        migration_incoming_error_propagate(mis, local_err);
     }
 }
 
migration/trace-events +1 -0
index 68bcf12dc7..c93c9f9e75 100644
@@ -207,6 +207,7 @@ migration_start_incoming(void) ""
 migration_transferred_bytes(uint64_t qemu_file, uint64_t multifd, uint64_t rdma) "qemu_file %" PRIu64 " multifd %" PRIu64 " RDMA %" PRIu64
 
 # channel.c
+migration_channel_process_incoming(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
 migration_set_incoming_channel(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
 migration_set_outgoing_channel(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
 
Signature · 1 line
2.55.0
Reviewed-by on v2 4/7
#10 lore raw

Re: [PATCH v2 4/7] migration: Split migration_channel_process_incoming()

Quoted 115 lines · Peter Xu

Introduce two smaller functions for it,

- migration_incoming_channel_install(): peek at channels whenever possible, and install the channel to one of the slots (main, multifd, preempt)

- migration_incoming_error_propagate(): handling an error happened during channel establish phase of an incoming migration.

When at it, move the set channel tracepoint into the 1st helper, replacing the existing tracepoint with trace_migration_channel_process_incoming(). So far, they should fire almost the same time if the channel peek worked. This fact will change in follow up refactorings.

I also moved migration_ioc_register_yank() to be after identify, hopefully it makes more sense: we'd better be careful on registration of yank, or we should be prepared for explosions. Previously it worked, my guess is when error happens we never unregister yank but hang dest QEMU or quit, depending on exit_on_error value.

No real functional change intended.

---
 migration/channel.c    | 57 +++++++++++++++++++++++++++++-------------
 migration/trace-events |  1 +
 2 files changed, 41 insertions(+), 17 deletions(-)
migration/channel.c +40 -17
index 266ae8f776..8407ea46cb 100644
@@ -197,6 +197,42 @@ out:
     return channel;
 }
 
+static void migration_incoming_error_propagate(MigrationIncomingState *mis,
+                                               Error *error)
+{
+    error_report_err(error);
+    migrate_set_state(&mis->state, mis->state, MIGRATION_STATUS_FAILED);
+    if (mis->exit_on_error) {
+        exit(EXIT_FAILURE);
+    }
+}
+
+static bool migration_incoming_channel_install(MigrationIncomingState *mis,
+                                               QIOChannel *ioc,
+                                               Error **errp)
+{
+    MigChannelType ch = migration_channel_identify(mis, ioc, errp);
+
+    if (!ch) {
+        assert(*errp);
+        return false;
+    }
+
+    trace_migration_set_incoming_channel(ioc,
+                                         object_get_typename(OBJECT(ioc)));
+    migration_ioc_register_yank(ioc);
+
+    if (migration_incoming_setup(ioc, ch, errp)) {
+        migration_start_incoming();
+    }
+
+    if (*errp) {
+        return false;
+    }
+
+    return true;
+}
+
 /**
  * @migration_channel_process_incoming - Create new incoming migration channel
  *
@@ -209,31 +245,18 @@ void migration_channel_process_incoming(QIOChannel *ioc)
 {
     MigrationIncomingState *mis = migration_incoming_get_current();
     Error *local_err = NULL;
-    MigChannelType ch;
 
-    trace_migration_set_incoming_channel(
+    trace_migration_channel_process_incoming(
         ioc, object_get_typename(OBJECT(ioc)));
 
     if (migrate_channel_requires_tls_upgrade(ioc)) {
         migration_tls_channel_process_incoming(ioc, &local_err);
     } else {
-        migration_ioc_register_yank(ioc);
-        ch = migration_channel_identify(mis, ioc, &local_err);
-        if (!ch) {
-            goto out;
-        }
-
-        if (migration_incoming_setup(ioc, ch, &local_err)) {
-            migration_start_incoming();
-        }
+        migration_incoming_channel_install(mis, ioc, &local_err);
     }
-out:
+
     if (local_err) {
-        error_report_err(local_err);
-        migrate_set_state(&mis->state, mis->state, MIGRATION_STATUS_FAILED);
-        if (mis->exit_on_error) {
-            exit(EXIT_FAILURE);
-        }
+        migration_incoming_error_propagate(mis, local_err);
     }
 }
 
migration/trace-events +0 -0
index 68bcf12dc7..c93c9f9e75 100644

migration/trace-events migration_start_incoming(void) "" · line 212

Quoted 6 lines
migration/trace-events +1 -0
@@ -207,6 +207,7 @@ migration_start_incoming(void) ""
 migration_transferred_bytes(uint64_t qemu_file, uint64_t multifd, uint64_t rdma) "qemu_file %" PRIu64 " multifd %" PRIu64 " RDMA %" PRIu64
 
 # channel.c
+migration_channel_process_incoming(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
 migration_set_incoming_channel(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
 migration_set_outgoing_channel(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
author
#11 lore raw

PATCH v2 5/7 migration: Dump type of channel in set_incoming_channel tracepoint

In reply to Peter Xu

Add this info to be crystal clear on what channel we're installing.

---
 migration/channel.h    |  3 ++-
 migration/channel.c    | 10 +++++++++-
 migration/trace-events |  2 +-
 3 files changed, 12 insertions(+), 3 deletions(-)
migration/channel.h +2 -1
index a7d0d29058..678712f291 100644
@@ -24,7 +24,8 @@ typedef enum {
     CH_NONE,
     CH_MAIN,
     CH_MULTIFD,
-    CH_POSTCOPY
+    CH_POSTCOPY,
+    CH_NUM,
 } MigChannelType;
 
 void migration_channel_process_incoming(QIOChannel *ioc);
migration/channel.c +9 -1
index 8407ea46cb..b9065efd03 100644
@@ -34,6 +34,13 @@
 #include "trace.h"
 #include "yank_functions.h"
 
+static const char *const mig_channel_str[CH_NUM] = {
+    [CH_NONE] = "NONE",
+    [CH_MAIN] = "MAIN",
+    [CH_MULTIFD] = "MULTIFD",
+    [CH_POSTCOPY] = "PREEMPT",
+};
+
 void migration_connect_outgoing(MigrationState *s, MigrationAddress *addr,
                                 Error **errp)
 {
@@ -219,7 +226,8 @@ static bool migration_incoming_channel_install(MigrationIncomingState *mis,
     }
 
     trace_migration_set_incoming_channel(ioc,
-                                         object_get_typename(OBJECT(ioc)));
+                                         object_get_typename(OBJECT(ioc)),
+                                         mig_channel_str[ch]);
     migration_ioc_register_yank(ioc);
 
     if (migration_incoming_setup(ioc, ch, errp)) {
migration/trace-events +1 -1
index c93c9f9e75..8840d82f8d 100644
@@ -208,7 +208,7 @@ migration_transferred_bytes(uint64_t qemu_file, uint64_t multifd, uint64_t rdma)
 
 # channel.c
 migration_channel_process_incoming(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
-migration_set_incoming_channel(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
+migration_set_incoming_channel(void *ioc, const char *ioctype, const char *type) "ioc=%p ioctype=%s type=%s"
 migration_set_outgoing_channel(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
 
 # global_state.c
Signature · 1 line
2.55.0
Reviewed-by on v2 5/7
#12 lore raw

Re: [PATCH v2 5/7] migration: Dump type of channel in set_incoming_channel tracepoint

Quoted 56 lines · Peter Xu

Add this info to be crystal clear on what channel we're installing.

---
 migration/channel.h    |  3 ++-
 migration/channel.c    | 10 +++++++++-
 migration/trace-events |  2 +-
 3 files changed, 12 insertions(+), 3 deletions(-)
migration/channel.h +2 -1
index a7d0d29058..678712f291 100644
@@ -24,7 +24,8 @@ typedef enum {
     CH_NONE,
     CH_MAIN,
     CH_MULTIFD,
-    CH_POSTCOPY
+    CH_POSTCOPY,
+    CH_NUM,
 } MigChannelType;
 
 void migration_channel_process_incoming(QIOChannel *ioc);
migration/channel.c +9 -1
index 8407ea46cb..b9065efd03 100644
@@ -34,6 +34,13 @@
 #include "trace.h"
 #include "yank_functions.h"
 
+static const char *const mig_channel_str[CH_NUM] = {
+    [CH_NONE] = "NONE",
+    [CH_MAIN] = "MAIN",
+    [CH_MULTIFD] = "MULTIFD",
+    [CH_POSTCOPY] = "PREEMPT",
+};
+
 void migration_connect_outgoing(MigrationState *s, MigrationAddress *addr,
                                 Error **errp)
 {
@@ -219,7 +226,8 @@ static bool migration_incoming_channel_install(MigrationIncomingState *mis,
     }
 
     trace_migration_set_incoming_channel(ioc,
-                                         object_get_typename(OBJECT(ioc)));
+                                         object_get_typename(OBJECT(ioc)),
+                                         mig_channel_str[ch]);
     migration_ioc_register_yank(ioc);
 
     if (migration_incoming_setup(ioc, ch, errp)) {
migration/trace-events +0 -0
index c93c9f9e75..8840d82f8d 100644

migration/trace-events migration_transferred_bytes(uint64_t qemu_file, uint64_t multifd, uint64_t rdma) · line 214

Quoted 8 lines
migration/trace-events +1 -1
@@ -208,7 +208,7 @@ migration_transferred_bytes(uint64_t qemu_file, uint64_t multifd, uint64_t rdma)
 
 # channel.c
 migration_channel_process_incoming(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
-migration_set_incoming_channel(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
+migration_set_incoming_channel(void *ioc, const char *ioctype, const char *type) "ioc=%p ioctype=%s type=%s"
 migration_set_outgoing_channel(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
 
 # global_state.c
author
#13 lore raw

PATCH v2 6/7 migration: Fix incoming channel race with multifd and preempt_pre_7_2

In reply to Peter Xu

PROBLEM
=======

Migration reliably hangs with old machine types (7.2-) when these migration capabilities are enabled: multifd, postcopy-ram, postcopy-preempt.

Simplest reproducer :

  $ QTEST_QEMU_MACHINE_TYPE=pc-q35-7.2 \
      QTEST_QEMU_BINARY=./qemu-system-x86_64 \
      ./tests/qtest/migration-test --full \
      -r /x86_64/migration/multifd+postcopy/preempt/plain

Current workaround is to kill destination QEMU, cancel the migration on source (with/without yank), disable one or more of the features, and retry.

The hang is caused by receiving preempt channel before most of the multifd channels, the peek buffer operation will stuck forever.

Incoming side QEMU hang (with relevant tracepoints enabled):

migrate_set_state new state setup migration_socket_incoming_accepted migration_set_incoming_channel ioc=0x557d82c1caa0 ioctype=qio-channel-socket migration_socket_incoming_accepted migration_set_incoming_channel ioc=0x557d83023840 ioctype=qio-channel-socket multifd_recv_new_channel channel 1 migration_socket_incoming_accepted migration_set_incoming_channel ioc=0x557d830c08c0 ioctype=qio-channel-socket multifd_recv_new_channel channel 0
multifd_recv_thread_start 1
migration_socket_incoming_accepted migration_set_incoming_channel ioc=0x557d8284a010 ioctype=qio-channel-socket multifd_recv_thread_start 0
multifd_recv_unfill channel 0 packet_num 0 flags 0x1 next packet size 0 multifd_recv_unfill channel 1 packet_num 1 flags 0x1 next packet size 0 [dest qemu hangs here]

Incoming side backtrace:

__syscall_cancel_arch
__internal_syscall_cancel
__syscall_cancel
recvmsg
qio_channel_socket_readv
qio_channel_readv_full
migration_channel_read_peek
migration_channel_identify
migration_channel_process_incoming
socket_accept_incoming_migration
qio_net_listener_channel_func
qio_channel_fd_source_dispatch
g_main_context_dispatch_unlocked.lto_priv
g_main_context_dispatch
glib_pollfds_poll
os_host_main_loop_wait
main_loop_wait
qemu_main_loop
qemu_default_main
main

This patch tries to fix it, from incoming side first. This patch alone should enable single-direction migration to this new QEMU. The next patch will propose a fix for the outgoing path, fixing new->old migrations.

BACKGROUND
==========

preempt_pre_7_2 used to be a flag marking QEMU releases 7.1/7.2 only because they have a flaw on channel establishment on preempt channel.

Now, after we have multifd + postcopy support, new QEMU binaries can migrate old machine types with both features enabled. Juraj reported constant migration hang at the very beginning for those old machine types in our downstream systems with such setup. Juraj found it's because of this flag.

QEMU source initiates channels in this order on current master when both features enabled with postcopy-preempt mode (machine type 7.2, implies preempt_pre_7_2=on):

- First, main channel connect() happens,
- Second, postcopy_preempt_setup() invokes connect() for preempt channel,
- Last, in migration_thread(), multifd_send_setup() invokes connect()s for all the multifd channels

However on an old QEMU, it expect the channels arrive in order of "main"->"multifd"->"preempt". Even on a new QEMU, the destination expects the same order of arrival on the channels when the channels are not peekable.

For peekable channels (non-tls sockets), this time we'll likely see preempt channel appearing very soon, peeking at it causing main thread hang, like mentioned in the PROBLEM section above.

SOLUTION
========

One major problem of current hang is we try to peek at preempt channel, which offers nothing at the beginning.

Since we'll need to accept stream from old binaries (QEMU 10.1 to 11.1), arrival of preempt channel early is unavoidable.

What we can do, though, is to allow concurrent monitoring of all channels rather than one. Meanwhile, we shouldn't peek at anything that has no data.

migration_channel_read_peek() almost does the wait, but it's too late. This patch proposes to watch all channels from the start without reading at them at all, relying on an IO watch attached. This will make sure (1) for preempt channel, we never read on it (watch can be gracefully disarmed when we know it's preempt channel later) (2) since we have one IO watch for each channel, then we can allow accept() to happen in almost any order, process only when there's some header to read.

To maintain these watches, a new GArray @channels_early is introduced, with each element to be a (ioc, GSource) tuple (@MigEarlyIncomingChannel), so that QEMU can manage all the IO watches later with it. Here I used GSource rather than io_tag because the GSource may or may not be attached to the default main thread gcontext: when OOB is used, it may be the iothread context instead. IOW, g_source_remove() on io_tags stops working, which only works for default main context.

The trick this patch used is when we have the ability to wait on all channel headers at the same time, we pick one by one from main and multifd channels (since both of the types will contain headers dumped right at the very beginning), leave preempt channel alone. It means, after dest QEMU finds all main and multifd channels, there are three possibilities:

- If preempt not enabled, we finished full channel establishment, done.

- If preempt enabled,

- If we have one registered IO watch with the IOC, it must be the preempt channel,

- If we have no further IO watch, wait for the next accept(), which must be the preempt channel.

This will make channel establish work on incoming side even with the old QEMU binaries.

While working on it, I managed to refactor the peeking logic quite a bit:
we used to have a very convoluted migration_channel_identify() function, taking care of two different cases:

(1) when the channel is peekable (like normal socket channels),
(2) when the channel is not peekable (like TLS or file migrations).

This patch refactored that part to make it crystal clear on how we make decisions on which channel is which, and split the peek/identify paths.

One example incoming migration from pc-q35-7.2 after this patch applied, and with relevant tracepoints set (8 multifd channels, 1 preempt channel):

migrate_set_state new state setup
migration_channel_process_incoming ioc=0x557dc8af4e20 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc8af4e20 ioctype=qio-channel-socket migration_channel_process_incoming ioc=0x557dc8abc5d0 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc8abc5d0 ioctype=qio-channel-socket migration_channel_process_incoming ioc=0x557dc953d680 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc953d680 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc8abc5d0 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc898a820 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc898a820 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc953d680 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc919bd70 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc919bd70 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc898a820 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc8734b20 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc8734b20 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc919bd70 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc898bdf0 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc898bdf0 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc8734b20 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc968cfd0 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc968cfd0 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc898bdf0 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc8c31810 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc8c31810 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc968cfd0 ioctype=qio-channel-socket type=MULTIFD migration_set_incoming_channel ioc=0x557dc8c31810 ioctype=qio-channel-socket type=MULTIFD migration_set_incoming_channel ioc=0x557dc8af4e20 ioctype=qio-channel-socket type=MAIN migration_start_incoming
migrate_set_state new state active
migration_channel_process_incoming ioc=0x557dc8966160 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc8966160 ioctype=qio-channel-socket type=PREEMPT

While at it, I renamed the four channel incoming/outgoing tracepoints, make all to follow migration_[incoming|outgoing]_channel_*(). Especially for incoming, then we can enable migration_incoming_channel* for incoming channel debuggings.

FINAL NOTE ON TLS
=================

Note that this solution isn't a magic bullet fixing everything. TLS is not fixable with this approach because TLS isn't peekable channel, hence the watch won't help.

We can have other ways to hack it to make it work, e.g. we can make TLS channels peekable too, but I don't want to pollute the TLS iochannel only for this: let's hope handshake will arrive soon so we can drop these code.

For now, when TLS is involved with such old machine type (7.2-), one needs to migrate it out with any combination of features except enabling all four (multifd + TLS + postcopy + preempt) to a patched binary, after that, all combinations will start working.

Fixes e27418861288 ("migration: enable multifd and postcopy together")
---
 migration/channel.h    |   3 +
 migration/migration.h  |  35 ++++
 migration/channel.c    | 413 +++++++++++++++++++++++++++++++++++------
 migration/migration.c  |  31 +++-
 migration/trace-events |   7 +-
 5 files changed, 418 insertions(+), 71 deletions(-)
migration/channel.h +3 -0
index 678712f291..40bd339a3e 100644
@@ -49,4 +49,7 @@ bool migration_channel_parse_input(const char *uri,
                                    MigrationChannel **main_channelp,
                                    MigrationChannel **cpr_channelp,
                                    Error **errp);
+
+void migration_incoming_free_early_channels(MigrationIncomingState *mis);
+
 #endif
migration/migration.h +35 -0
index 683bc7bdd5..6f23e1805b 100644
@@ -90,6 +90,28 @@ typedef enum {
     PREEMPT_THREAD_QUIT,
 } PreemptThreadStatus;
 
+typedef struct {
+    /*
+     * The GSource we have allocated to track G_IO_IN events for the
+     * channel.  Note: it may not be attached to the default main gcontext,
+     * for example, when in case of io watch created during an OOB
+     * migrate-recover command.
+     */
+    GSource *source;
+    /* The channel we will identify later after getting some buffers to read */
+    QIOChannel *ioc;
+} MigEarlyIncomingChannel;
+
+typedef struct {
+    GArray *channels;
+    /*
+     * In most cases, channels are only operated with BQl, but since we
+     * have OOB command support, don't assume it, just use a standalone
+     * mutex to protect any access to the channels array.
+     */
+    QemuMutex mutex;
+} MigEarlyIncomingChannels;
+
 /* State for the incoming migration */
 struct MigrationIncomingState {
     QEMUFile *from_src_file;
@@ -255,6 +277,19 @@ struct MigrationIncomingState {
 
     /* Do exit on incoming migration failure */
     bool exit_on_error;
+
+    /*
+     * Maintains all channels only at early stage.  After incoming
+     * migration started, this should be an empty array and no use anymore.
+     *
+     * TODO: we logically only need this to make new QEMU binaries work for
+     * old machines who have @preempt_pre_7_2 set on the source, but it's
+     * also good to have this early stage anyway, making sure when peek the
+     * channel there will be something in the channel.  Ideally, we should
+     * manage all migration channels in the future with proper headers,
+     * then we can drop this.
+     */
+    MigEarlyIncomingChannels channels_early;
 };
 
 MigrationIncomingState *migration_incoming_get_current(void);
migration/channel.c +352 -61

473 lines not on this page · raw message

migration/migration.c +24 -7
index 3ffc0c2c6a..b140fb6950 100644
@@ -334,6 +334,10 @@ void migration_object_init(void)
     current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
 
     current_incoming->exit_on_error = INMIGRATE_DEFAULT_EXIT_ON_ERROR;
+    /* zero_terminated=false, clear_=true (NOTE, "clear_" is not a typo) */
+    current_incoming->channels_early.channels =
+        g_array_new(FALSE, TRUE, sizeof(MigEarlyIncomingChannel));
+    qemu_mutex_init(&current_incoming->channels_early.mutex);
 
     migration_object_check(current_migration, &error_fatal);
 
@@ -447,6 +451,24 @@ void migration_incoming_state_destroy(void)
     MigrationIncomingState *mis = migration_incoming_get_current();
     PostcopyState ps = postcopy_state_get();
 
+    /* Cleanup listener to make sure no further accept() for sockets */
+    migration_incoming_transport_cleanup(mis);
+
+    /*
+     * It's safer to free channel watches earlier than most of the rest, in
+     * case the IO watches could fire in the monitor iothread concurrently
+     * against this function.
+     *
+     * Above migration_incoming_transport_cleanup() should have disarmed
+     * anything that we could accept() new sockets.
+     *
+     * Here return of migration_incoming_free_early_channels() makes sure
+     * even if something already fired concurrently, it won't really do
+     * anything but return - see migration_incoming_early_channel_remove()
+     * and its return code for details.
+     */
+    migration_incoming_free_early_channels(mis);
+
     multifd_recv_cleanup();
 
     if (ps != POSTCOPY_INCOMING_NONE) {
@@ -491,7 +513,6 @@ void migration_incoming_state_destroy(void)
         mis->postcopy_remote_fds = NULL;
     }
 
-    migration_incoming_transport_cleanup(mis);
     qemu_event_reset(&mis->main_thread_load_event);
 
     if (mis->page_requested) {
@@ -833,10 +854,6 @@ out:
     migrate_incoming_unref_outgoing_state();
 }
 
-/*
- * Returns whether all the necessary channels to proceed with the
- * incoming migration have been established without error.
- */
 bool migration_incoming_setup(QIOChannel *ioc, uint8_t channel, Error **errp)
 {
     MigrationIncomingState *mis = migration_incoming_get_current();
@@ -865,13 +882,13 @@ bool migration_incoming_setup(QIOChannel *ioc, uint8_t channel, Error **errp)
         assert(!mis->postcopy_qemufile_dst);
         f = qemu_file_new_input(ioc);
         postcopy_preempt_new_channel(mis, f);
-        return false;
+        break;
 
     default:
         g_assert_not_reached();
     }
 
-    return migration_has_main_and_multifd_channels();
+    return true;
 }
 
 void migration_outgoing_setup(QIOChannel *ioc)
migration/trace-events +4 -3
index 8840d82f8d..a6e11451f1 100644
@@ -207,9 +207,10 @@ migration_start_incoming(void) ""
 migration_transferred_bytes(uint64_t qemu_file, uint64_t multifd, uint64_t rdma) "qemu_file %" PRIu64 " multifd %" PRIu64 " RDMA %" PRIu64
 
 # channel.c
-migration_channel_process_incoming(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
-migration_set_incoming_channel(void *ioc, const char *ioctype, const char *type) "ioc=%p ioctype=%s type=%s"
-migration_set_outgoing_channel(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
+migration_outgoing_channel_set(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
+migration_incoming_channel_process(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
+migration_incoming_channel_set(void *ioc, const char *ioctype, const char *type) "ioc=%p ioctype=%s type=%s"
+migration_incoming_channel_watch(void *ioc, const char *ioctype) "ioc=%p ioctype=%s"
 
 # global_state.c
 migrate_state_too_big(void) ""
Signature · 1 line
2.55.0
Reviewed-by on v2 6/7
#14 lore raw

Re: [PATCH v2 6/7] migration: Fix incoming channel race with multifd and preempt_pre_7_2

Quoted 210 lines · Peter Xu

PROBLEM
=======

Migration reliably hangs with old machine types (7.2-) when these migration capabilities are enabled: multifd, postcopy-ram, postcopy-preempt.

Simplest reproducer :

  $ QTEST_QEMU_MACHINE_TYPE=pc-q35-7.2 \
      QTEST_QEMU_BINARY=./qemu-system-x86_64 \
      ./tests/qtest/migration-test --full \
      -r /x86_64/migration/multifd+postcopy/preempt/plain

Current workaround is to kill destination QEMU, cancel the migration on source (with/without yank), disable one or more of the features, and retry.

The hang is caused by receiving preempt channel before most of the multifd channels, the peek buffer operation will stuck forever.

Incoming side QEMU hang (with relevant tracepoints enabled):

migrate_set_state new state setup migration_socket_incoming_accepted migration_set_incoming_channel ioc=0x557d82c1caa0 ioctype=qio-channel-socket migration_socket_incoming_accepted migration_set_incoming_channel ioc=0x557d83023840 ioctype=qio-channel-socket multifd_recv_new_channel channel 1 migration_socket_incoming_accepted migration_set_incoming_channel ioc=0x557d830c08c0 ioctype=qio-channel-socket multifd_recv_new_channel channel 0
multifd_recv_thread_start 1
migration_socket_incoming_accepted migration_set_incoming_channel ioc=0x557d8284a010 ioctype=qio-channel-socket multifd_recv_thread_start 0
multifd_recv_unfill channel 0 packet_num 0 flags 0x1 next packet size 0 multifd_recv_unfill channel 1 packet_num 1 flags 0x1 next packet size 0 [dest qemu hangs here]

Incoming side backtrace:

__syscall_cancel_arch
__internal_syscall_cancel
__syscall_cancel
recvmsg
qio_channel_socket_readv
qio_channel_readv_full
migration_channel_read_peek
migration_channel_identify
migration_channel_process_incoming
socket_accept_incoming_migration
qio_net_listener_channel_func
qio_channel_fd_source_dispatch
g_main_context_dispatch_unlocked.lto_priv
g_main_context_dispatch
glib_pollfds_poll
os_host_main_loop_wait
main_loop_wait
qemu_main_loop
qemu_default_main
main

This patch tries to fix it, from incoming side first. This patch alone should enable single-direction migration to this new QEMU. The next patch will propose a fix for the outgoing path, fixing new->old migrations.

BACKGROUND
==========

preempt_pre_7_2 used to be a flag marking QEMU releases 7.1/7.2 only because they have a flaw on channel establishment on preempt channel.

Now, after we have multifd + postcopy support, new QEMU binaries can migrate old machine types with both features enabled. Juraj reported constant migration hang at the very beginning for those old machine types in our downstream systems with such setup. Juraj found it's because of this flag.

QEMU source initiates channels in this order on current master when both features enabled with postcopy-preempt mode (machine type 7.2, implies preempt_pre_7_2=on):

- First, main channel connect() happens,
- Second, postcopy_preempt_setup() invokes connect() for preempt channel,
- Last, in migration_thread(), multifd_send_setup() invokes connect()s for all the multifd channels

However on an old QEMU, it expect the channels arrive in order of "main"->"multifd"->"preempt". Even on a new QEMU, the destination expects the same order of arrival on the channels when the channels are not peekable.

For peekable channels (non-tls sockets), this time we'll likely see preempt channel appearing very soon, peeking at it causing main thread hang, like mentioned in the PROBLEM section above.

SOLUTION
========

One major problem of current hang is we try to peek at preempt channel, which offers nothing at the beginning.

Since we'll need to accept stream from old binaries (QEMU 10.1 to 11.1), arrival of preempt channel early is unavoidable.

What we can do, though, is to allow concurrent monitoring of all channels rather than one. Meanwhile, we shouldn't peek at anything that has no data.

migration_channel_read_peek() almost does the wait, but it's too late. This patch proposes to watch all channels from the start without reading at them at all, relying on an IO watch attached. This will make sure (1) for preempt channel, we never read on it (watch can be gracefully disarmed when we know it's preempt channel later) (2) since we have one IO watch for each channel, then we can allow accept() to happen in almost any order, process only when there's some header to read.

To maintain these watches, a new GArray @channels_early is introduced, with each element to be a (ioc, GSource) tuple (@MigEarlyIncomingChannel), so that QEMU can manage all the IO watches later with it. Here I used GSource rather than io_tag because the GSource may or may not be attached to the default main thread gcontext: when OOB is used, it may be the iothread context instead. IOW, g_source_remove() on io_tags stops working, which only works for default main context.

The trick this patch used is when we have the ability to wait on all channel headers at the same time, we pick one by one from main and multifd channels (since both of the types will contain headers dumped right at the very beginning), leave preempt channel alone. It means, after dest QEMU finds all main and multifd channels, there are three possibilities:

- If preempt not enabled, we finished full channel establishment, done.

- If preempt enabled,

- If we have one registered IO watch with the IOC, it must be the preempt channel,

- If we have no further IO watch, wait for the next accept(), which must be the preempt channel.

This will make channel establish work on incoming side even with the old QEMU binaries.

While working on it, I managed to refactor the peeking logic quite a bit:
we used to have a very convoluted migration_channel_identify() function, taking care of two different cases:

(1) when the channel is peekable (like normal socket channels),
(2) when the channel is not peekable (like TLS or file migrations).

This patch refactored that part to make it crystal clear on how we make decisions on which channel is which, and split the peek/identify paths.

One example incoming migration from pc-q35-7.2 after this patch applied, and with relevant tracepoints set (8 multifd channels, 1 preempt channel):

migrate_set_state new state setup
migration_channel_process_incoming ioc=0x557dc8af4e20 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc8af4e20 ioctype=qio-channel-socket migration_channel_process_incoming ioc=0x557dc8abc5d0 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc8abc5d0 ioctype=qio-channel-socket migration_channel_process_incoming ioc=0x557dc953d680 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc953d680 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc8abc5d0 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc898a820 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc898a820 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc953d680 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc919bd70 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc919bd70 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc898a820 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc8734b20 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc8734b20 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc919bd70 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc898bdf0 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc898bdf0 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc8734b20 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc968cfd0 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc968cfd0 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc898bdf0 ioctype=qio-channel-socket type=MULTIFD migration_channel_process_incoming ioc=0x557dc8c31810 ioctype=qio-channel-socket migration_incoming_channel_watch ioc=0x557dc8c31810 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc968cfd0 ioctype=qio-channel-socket type=MULTIFD migration_set_incoming_channel ioc=0x557dc8c31810 ioctype=qio-channel-socket type=MULTIFD migration_set_incoming_channel ioc=0x557dc8af4e20 ioctype=qio-channel-socket type=MAIN migration_start_incoming
migrate_set_state new state active
migration_channel_process_incoming ioc=0x557dc8966160 ioctype=qio-channel-socket migration_set_incoming_channel ioc=0x557dc8966160 ioctype=qio-channel-socket type=PREEMPT

While at it, I renamed the four channel incoming/outgoing tracepoints, make all to follow migration_[incoming|outgoing]_channel_*(). Especially for incoming, then we can enable migration_incoming_channel* for incoming channel debuggings.

FINAL NOTE ON TLS
=================

Note that this solution isn't a magic bullet fixing everything. TLS is not fixable with this approach because TLS isn't peekable channel, hence the watch won't help.

We can have other ways to hack it to make it work, e.g. we can make TLS channels peekable too, but I don't want to pollute the TLS iochannel only for this: let's hope handshake will arrive soon so we can drop these code.

For now, when TLS is involved with such old machine type (7.2-), one needs to migrate it out with any combination of features except enabling all four (multifd + TLS + postcopy + preempt) to a patched binary, after that, all combinations will start working.

Quoted 4 lines
Fixes e27418861288 ("migration: enable multifd and postcopy together")
author
#15 lore raw

PATCH v2 7/7 migration: Postpone the outgoing preempt channel for preempt_pre_7_2

In reply to Peter Xu

This is the corresponding outgoing fix for the same problem mentioned in the previous patch.

The idea is simple: either migration to an old QEMU or when TLS is enabled, we rely on correct ordering of channels to make it work: first main, then multifd, finally preempt.

This patch re-orders multifd and postcopy preempt channel on outgoing side, but only for the old machine types where preempt_pre_7_2 set, to make it follow the right orders to create the channels.

Keep the resume path alone because the resume path never involve multifd channels, even if multifd is enabled. For that, fun fact: when multifd is enabled with postcopy preempt, network interruption will break all multifd channels, but they're never re-connected, until they got recycled at the end of postcopy migration. OTOH, multifd_recv_all_channels_created() reports true even after postcopy network down, as multifd_recv_state->count is never decremented.

NOTE: this will move the preempt channel connection no matter multifd is enabled or not. It should fix multifd's case, but should have zero impact on when multifd is not enabled.

Fixes e27418861288 ("migration: enable multifd and postcopy together")
---
 migration/migration.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
migration/migration.c +24 -9
index b140fb6950..53cb6d3821 100644
@@ -3729,6 +3729,21 @@ static void *migration_thread(void *opaque)
         goto out;
     }
 
+    /*
+     * Only for old 7.1/7.2 machine types (preempt mode supported since
+     * 7.1).  Connect the preempt channel only after multifd channels,
+     * because some QEMU versions (v10.1 to v11.1) will assume the order of
+     * channels to be: (1) main, then (2) multifd channels, then (3)
+     * preempt channel.  IOW, (3) must be after (2).
+     *
+     * multifd_send_setup() makes sure all multifd channels be connected
+     * first, then initiate preempt channel here guarantees the ordering to
+     * those QEMU binaries.
+     */
+    if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
+        postcopy_preempt_setup(s);
+    }
+
     bql_lock();
     qemu_savevm_state_header(s->to_dst_file);
     bql_unlock();
@@ -4009,16 +4024,16 @@ void migration_start_outgoing(MigrationState *s)
         open_return_path_on_source(s);
     }
 
-    /*
-     * This needs to be done before resuming a postcopy.  Note: for newer
-     * QEMUs we will delay the channel creation until postcopy_start(), to
-     * avoid disorder of channel creations.
-     */
-    if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
-        postcopy_preempt_setup(s);
-    }
-
     if (resume) {
+        /*
+         * This needs to be done before resuming a postcopy.  Note: for newer
+         * QEMUs we will delay the channel creation until postcopy_start(), to
+         * avoid disorder of channel creations.
+         */
+        if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
+            postcopy_preempt_setup(s);
+        }
+
         /* Wakeup the main migration thread to do the recovery */
         migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP,
                           MIGRATION_STATUS_POSTCOPY_RECOVER);
Signature · 1 line
2.55.0
Reviewed-by on v2 7/7
#16 lore raw

Re: [PATCH v2 7/7] migration: Postpone the outgoing preempt channel for preempt_pre_7_2

Quoted 71 lines · Peter Xu

This is the corresponding outgoing fix for the same problem mentioned in the previous patch.

The idea is simple: either migration to an old QEMU or when TLS is enabled, we rely on correct ordering of channels to make it work: first main, then multifd, finally preempt.

This patch re-orders multifd and postcopy preempt channel on outgoing side, but only for the old machine types where preempt_pre_7_2 set, to make it follow the right orders to create the channels.

Keep the resume path alone because the resume path never involve multifd channels, even if multifd is enabled. For that, fun fact: when multifd is enabled with postcopy preempt, network interruption will break all multifd channels, but they're never re-connected, until they got recycled at the end of postcopy migration. OTOH, multifd_recv_all_channels_created() reports true even after postcopy network down, as multifd_recv_state->count is never decremented.

NOTE: this will move the preempt channel connection no matter multifd is enabled or not. It should fix multifd's case, but should have zero impact on when multifd is not enabled.

Fixes e27418861288 ("migration: enable multifd and postcopy together")
---
 migration/migration.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
migration/migration.c +17 -9
index b140fb6950..53cb6d3821 100644
@@ -3729,6 +3729,21 @@ static void *migration_thread(void *opaque)
         goto out;
     }
 
+    /*
+     * Only for old 7.1/7.2 machine types (preempt mode supported since
+     * 7.1).  Connect the preempt channel only after multifd channels,
+     * because some QEMU versions (v10.1 to v11.1) will assume the order of
+     * channels to be: (1) main, then (2) multifd channels, then (3)
+     * preempt channel.  IOW, (3) must be after (2).
+     *
+     * multifd_send_setup() makes sure all multifd channels be connected
+     * first, then initiate preempt channel here guarantees the ordering to
+     * those QEMU binaries.
+     */
+    if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
+        postcopy_preempt_setup(s);
+    }
+
     bql_lock();
     qemu_savevm_state_header(s->to_dst_file);
     bql_unlock();
@@ -4009,16 +4024,16 @@ void migration_start_outgoing(MigrationState *s)
         open_return_path_on_source(s);
     }
 
-    /*
-     * This needs to be done before resuming a postcopy.  Note: for newer
-     * QEMUs we will delay the channel creation until postcopy_start(), to
-     * avoid disorder of channel creations.
-     */
-    if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
-        postcopy_preempt_setup(s);
-    }
-
     if (resume) {
+        /*
+         * This needs to be done before resuming a postcopy.  Note: for newer

migration/migration.c void migration_start_outgoing(MigrationState *s) · line 4039

Quoted 10 lines
migration/migration.c +7 -0
+         * QEMUs we will delay the channel creation until postcopy_start(), to
+         * avoid disorder of channel creations.
+         */
+        if (migrate_postcopy_preempt() && s->preempt_pre_7_2) {
+            postcopy_preempt_setup(s);
+        }
+
         /* Wakeup the main migration thread to do the recovery */
         migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP,
                           MIGRATION_STATUS_POSTCOPY_RECOVER);