Skip to contentSkip to conversation

migration: Add COLO multifd support and COLO migration unit test

PATCH Superseded Lukas Straub 4 participants 37 messages Jan 25 – Feb 6 Arm/Amlogic Meson SoC support

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v3 cover migration: Add COLO multifd support and COLO migration unit test

Hello everyone,
This adds COLO multifd support and migration unit tests for COLO migration and failover.

Regards,
Lukas

---
Changes in v3:
- Fix peter's review comments.
- Fix COLO with Q35 machine
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]

Changes in v2:
- Fix review comments
- Hide stderr in colo migration test since the logged errors are expected
- Add benchmarking data for multifd
- Add myself as maintainer for COLO migration framework
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

---
Lukas Straub (10):
MAINTAINERS: Add myself as maintainer for COLO migration framework
MAINTAINERS: Remove Hailiang Zhang from COLO migration framework Move ram state receive into multifd_ram_state_recv()
multifd: Add COLO support
colo: Fix crash during device vmstate load
migration-test: Add COLO migration unit test
Convert colo main documentation to restructuredText qemu-colo.rst: Miscellaneous changes
qemu-colo.rst: Add my copyright
qemu-colo.rst: Simplify the block replication setup

 MAINTAINERS                        |   6 +-
 docs/COLO-FT.txt                   | 334 ----------------------------------
 docs/system/index.rst              |   1 +
 docs/system/qemu-colo.rst          | 362 +++++++++++++++++++++++++++++++++++++
 migration/colo.c                   |   1 +
 migration/meson.build              |   2 +-
 migration/multifd-colo.c           |  50 +++++
 migration/multifd-colo.h           |  26 +++
 migration/multifd-nocomp.c         |  10 +-
 migration/multifd.c                |  19 +-
 migration/multifd.h                |   5 +-
 tests/qtest/meson.build            |   7 +-
 tests/qtest/migration-test.c       |   1 +
 tests/qtest/migration/colo-tests.c | 199 ++++++++++++++++++++
 tests/qtest/migration/framework.h  |   5 +
 15 files changed, 687 insertions(+), 341 deletions(-)

---
base-commit: fea2d7a784fc3627a8aa72875f51fe7634b04b81
change-id: 20251230-colo_unit_test_multifd-8bf58dcebd46

Best regards,

Signature · 1 line
Lukas Straub <[email protected]>
#1 lore raw

PATCH v3 1/10 MAINTAINERS: Add myself as maintainer for COLO migration framework

I am ready to maintain it.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index e23354235dc70a6224dd00ce92b0a049fbc8edfc..689d79b82d39ec8c2bb15dacb7928df5649756cd 100644
@@ -3851,6 +3851,7 @@ F: qapi/yank.json
 
 COLO Framework
 M: Hailiang Zhang <[email protected]>
+M: Lukas Straub <[email protected]>
 S: Maintained
 F: migration/colo*
 F: include/migration/colo.h
Signature · 1 line
2.39.5
#2 lore raw

PATCH v3 2/10 MAINTAINERS: Remove Hailiang Zhang from COLO migration framework

In reply to Lukas Straub

His last email to the mailing list is from December 2021:
https://lore.kernel.org/qemu-devel/[email protected]/

---
 MAINTAINERS | 1 -
 1 file changed, 1 deletion(-)
MAINTAINERS +0 -1
index 689d79b82d39ec8c2bb15dacb7928df5649756cd..1e9bdd87c3a2f84f3abfc56986cd793976810fdd 100644
@@ -3850,7 +3850,6 @@ F: include/qemu/yank.h
 F: qapi/yank.json
 
 COLO Framework
-M: Hailiang Zhang <[email protected]>
 M: Lukas Straub <[email protected]>
 S: Maintained
 F: migration/colo*
Signature · 1 line
2.39.5
#3 lore raw

PATCH v3 3/10 Move ram state receive into multifd_ram_state_recv()

In reply to Lukas Straub

This is in preparation for the next patch.

---
 migration/multifd.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
migration/multifd.c +10 -1
index ad6261688fdf98a5c7f4ee9fb80ba2901201a33e..332e6fc58053462419f3171f6c320ac37648ef7b 100644
@@ -1253,6 +1253,15 @@ static int multifd_device_state_recv(MultiFDRecvParams *p, Error **errp)
     return ret;
 }
 
+static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp)
+{
+    int ret;
+
+    ret = multifd_recv_state->ops->recv(p, errp);
+
+    return ret;
+}
+
 static void *multifd_recv_thread(void *opaque)
 {
     MigrationState *s = migrate_get_current();
@@ -1387,7 +1396,7 @@ static void *multifd_recv_thread(void *opaque)
                 assert(use_packets);
                 ret = multifd_device_state_recv(p, &local_err);
             } else {
-                ret = multifd_recv_state->ops->recv(p, &local_err);
+                ret = multifd_ram_state_recv(p, &local_err);
             }
             if (ret != 0) {
                 break;
Signature · 1 line
2.39.5
Reviewed-by on v3 3/10
#4 lore raw

Re: [PATCH v3 03/10] Move ram state receive into multifd_ram_state_recv()

Quoted 4 lines · Lukas Straub

This is in preparation for the next patch.

Quoted 33 lines
---
 migration/multifd.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
migration/multifd.c +10 -1
index ad6261688fdf98a5c7f4ee9fb80ba2901201a33e..332e6fc58053462419f3171f6c320ac37648ef7b 100644
@@ -1253,6 +1253,15 @@ static int multifd_device_state_recv(MultiFDRecvParams *p, Error **errp)
     return ret;
 }
 
+static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp)
+{
+    int ret;
+
+    ret = multifd_recv_state->ops->recv(p, errp);
+
+    return ret;
+}
+
 static void *multifd_recv_thread(void *opaque)
 {
     MigrationState *s = migrate_get_current();
@@ -1387,7 +1396,7 @@ static void *multifd_recv_thread(void *opaque)
                 assert(use_packets);
                 ret = multifd_device_state_recv(p, &local_err);
             } else {
-                ret = multifd_recv_state->ops->recv(p, &local_err);
+                ret = multifd_ram_state_recv(p, &local_err);
             }
             if (ret != 0) {
                 break;
#5 lore raw

PATCH v3 4/10 multifd: Add COLO support

In reply to Lukas Straub

Like in the normal ram_load() path, put the received pages into the colo cache and mark the pages in the bitmap so that they will be flushed to the guest later.

Multifd with COLO is useful to reduce the VM pause time during checkpointing for latency sensitive workloads. In such workloads the worst-case latency is especially important.

Also, this is already worth it for the precopy phase as it helps with converging. Moreover, multifd migration is the preferred way to do migration nowadays and this allows to use multifd compression with COLO.

Benchmark:
Cluster nodes
- Intel Xenon E5-2630 v3
- 48Gb RAM
- 10G Ethernet
Guest
- Windows Server 2016
- 6Gb RAM
- 4 cores
Workload
- Upload a file to the guest with SMB to simulate moderate memory dirtying
- Measure the memory transfer time portion of each checkpoint
- 600ms COLO checkpoint interval

Results
Plain
idle mean: 4.50ms 99per: 10.33ms
load mean: 24.30ms 99per: 78.05ms
Multifd-4
idle mean: 6.48ms 99per: 10.41ms
load mean: 14.12ms 99per: 31.27ms

Evaluation
While multifd has slightly higher latency when the guest idles, it is 10ms faster under load and more importantly it's worst case latency is less than 1/2 of plain under load as can be seen in the 99. Percentile.

---
 MAINTAINERS                |  1 +
 migration/meson.build      |  2 +-
 migration/multifd-colo.c   | 50 ++++++++++++++++++++++++++++++++++++++++++++++
 migration/multifd-colo.h   | 26 ++++++++++++++++++++++++
 migration/multifd-nocomp.c | 10 +++++++++-
 migration/multifd.c        |  8 ++++++++
 migration/multifd.h        |  5 ++++-
 7 files changed, 99 insertions(+), 3 deletions(-)
MAINTAINERS +1 -0
index 1e9bdd87c3a2f84f3abfc56986cd793976810fdd..883f0a8f4eb92d0bf0f89fcab4674ccc4aed1cc1 100644
@@ -3853,6 +3853,7 @@ COLO Framework
 M: Lukas Straub <[email protected]>
 S: Maintained
 F: migration/colo*
+F: migration/multifd-colo.*
 F: include/migration/colo.h
 F: include/migration/failover.h
 F: docs/COLO-FT.txt
migration/meson.build +1 -1
index c7f39bdb55239ecb0e775c77b90a1aa9e6a4a9ce..c9f0f5f9f2137536497e53e960ce70654ad1b394 100644
@@ -39,7 +39,7 @@ system_ss.add(files(
 ), gnutls, zlib)
 
 if get_option('replication').allowed()
-  system_ss.add(files('colo-failover.c', 'colo.c'))
+  system_ss.add(files('colo-failover.c', 'colo.c', 'multifd-colo.c'))
 else
   system_ss.add(files('colo-stubs.c'))
 endif
migration/multifd-colo.c new file +50 -0

50 lines not on this page · raw message

migration/multifd-colo.h new file +26 -0
new file mode 100644
index 0000000000000000000000000000000000000000..82eaf3f48c47de2f090f9de52f9d57a337d4754a
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo header
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_MIGRATION_MULTIFD_COLO_H
+#define QEMU_MIGRATION_MULTIFD_COLO_H
+
+#ifdef CONFIG_REPLICATION
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p);
+void multifd_colo_process_recv(MultiFDRecvParams *p);
+
+#else
+
+static inline void multifd_colo_prepare_recv(MultiFDRecvParams *p) {}
+static inline void multifd_colo_process_recv(MultiFDRecvParams *p) {}
+
+#endif
+#endif
migration/multifd-nocomp.c +9 -1
index 9be79b3b8e00371ebff9e112766c225bec260bf7..9f7a792fa761b3bc30b971b35f464103a61787f0 100644
@@ -16,6 +16,7 @@
 #include "file.h"
 #include "migration-stats.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "migration.h"
 #include "qapi/error.h"
@@ -269,7 +270,6 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         return -1;
     }
 
-    p->host = p->block->host;
     for (i = 0; i < p->normal_num; i++) {
         uint64_t offset = be64_to_cpu(packet->offset[i]);
 
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;
+    } else {
+        p->host = p->block->host;
+    }
+
     return 0;
 }
 
migration/multifd.c +8 -0
index 332e6fc58053462419f3171f6c320ac37648ef7b..220ed8564960fdabc58e4baa069dd252c8ad293c 100644
@@ -29,6 +29,7 @@
 #include "qemu-file.h"
 #include "trace.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "qemu/yank.h"
 #include "io/channel-file.h"
@@ -1258,6 +1259,13 @@ static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp)
     int ret;
 
     ret = multifd_recv_state->ops->recv(p, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migrate_colo()) {
+        multifd_colo_process_recv(p);
+    }
 
     return ret;
 }
migration/multifd.h +4 -1
index 89a395aef2b09a6762c45b5361e0ab63256feff6..fbc35702b062fdc3213ce92baed35994f5967c2b 100644
@@ -279,7 +279,10 @@ typedef struct {
     uint64_t packets_recved;
     /* ramblock */
     RAMBlock *block;
-    /* ramblock host address */
+    /*
+     * Normally, it points to ramblock's host address.  When COLO
+     * is enabled, it points to the mirror cache for the ramblock.
+     */
     uint8_t *host;
     /* buffers to recv */
     struct iovec *iov;
Signature · 1 line
2.39.5
#6 lore raw

Re: [PATCH v3 04/10] multifd: Add COLO support

Quoted 36 lines · Lukas Straub

Like in the normal ram_load() path, put the received pages into the colo cache and mark the pages in the bitmap so that they will be flushed to the guest later.

Multifd with COLO is useful to reduce the VM pause time during checkpointing for latency sensitive workloads. In such workloads the worst-case latency is especially important.

Also, this is already worth it for the precopy phase as it helps with converging. Moreover, multifd migration is the preferred way to do migration nowadays and this allows to use multifd compression with COLO.

Benchmark:
Cluster nodes
- Intel Xenon E5-2630 v3
- 48Gb RAM
- 10G Ethernet
Guest
- Windows Server 2016
- 6Gb RAM
- 4 cores
Workload
- Upload a file to the guest with SMB to simulate moderate memory dirtying
- Measure the memory transfer time portion of each checkpoint
- 600ms COLO checkpoint interval

Results
Plain
idle mean: 4.50ms 99per: 10.33ms
load mean: 24.30ms 99per: 78.05ms
Multifd-4
idle mean: 6.48ms 99per: 10.41ms
load mean: 14.12ms 99per: 31.27ms

Quoted 5 lines

Evaluation
While multifd has slightly higher latency when the guest idles, it is 10ms faster under load and more importantly it's worst case latency is less than 1/2 of plain under load as can be seen in the 99. Percentile.

Why the multifd get higher latency when the guest idles? The status same with normal live migration? Where is the time spent? The Sorry, I don't know this background yet.

Thanks
Chen

Quoted 206 lines
---
 MAINTAINERS                |  1 +
 migration/meson.build      |  2 +-
 migration/multifd-colo.c   | 50 ++++++++++++++++++++++++++++++++++++++++++++++
 migration/multifd-colo.h   | 26 ++++++++++++++++++++++++
 migration/multifd-nocomp.c | 10 +++++++++-
 migration/multifd.c        |  8 ++++++++
 migration/multifd.h        |  5 ++++-
 7 files changed, 99 insertions(+), 3 deletions(-)
MAINTAINERS +1 -0
index 1e9bdd87c3a2f84f3abfc56986cd793976810fdd..883f0a8f4eb92d0bf0f89fcab4674ccc4aed1cc1 100644
@@ -3853,6 +3853,7 @@ COLO Framework
 M: Lukas Straub <[email protected]>
 S: Maintained
 F: migration/colo*
+F: migration/multifd-colo.*
 F: include/migration/colo.h
 F: include/migration/failover.h
 F: docs/COLO-FT.txt
migration/meson.build +1 -1
index c7f39bdb55239ecb0e775c77b90a1aa9e6a4a9ce..c9f0f5f9f2137536497e53e960ce70654ad1b394 100644
@@ -39,7 +39,7 @@ system_ss.add(files(
 ), gnutls, zlib)
 
 if get_option('replication').allowed()
-  system_ss.add(files('colo-failover.c', 'colo.c'))
+  system_ss.add(files('colo-failover.c', 'colo.c', 'multifd-colo.c'))
 else
   system_ss.add(files('colo-stubs.c'))
 endif
migration/multifd-colo.c new file +50 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c47f5044663969e0c9af56da5ec34902d635810a
@@ -0,0 +1,50 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo implementation
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "exec/target_page.h"
+#include "qemu/error-report.h"
+#include "qapi/error.h"
+#include "ram.h"
+#include "multifd.h"
+#include "options.h"
+#include "io/channel-socket.h"
+#include "migration/colo.h"
+#include "multifd-colo.h"
+#include "system/ramblock.h"
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p)
+{
+    /*
+     * While we're still in precopy state (not yet in colo state), we copy
+     * received pages to both guest and cache. No need to set dirty bits,
+     * since guest and cache memory are in sync.
+     */
+    if (migration_incoming_in_colo_state()) {
+        colo_record_bitmap(p->block, p->normal, p->normal_num);
+        colo_record_bitmap(p->block, p->zero, p->zero_num);
+    }
+}
+
+void multifd_colo_process_recv(MultiFDRecvParams *p)
+{
+    if (!migration_incoming_in_colo_state()) {
+        for (int i = 0; i < p->normal_num; i++) {
+            void *guest = p->block->host + p->normal[i];
+            void *cache = p->host + p->normal[i];
+            memcpy(guest, cache, multifd_ram_page_size());
+        }
+        for (int i = 0; i < p->zero_num; i++) {
+            void *guest = p->block->host + p->zero[i];
+            memset(guest, 0, multifd_ram_page_size());
+        }
+    }
+}
migration/multifd-colo.h new file +26 -0
new file mode 100644
index 0000000000000000000000000000000000000000..82eaf3f48c47de2f090f9de52f9d57a337d4754a
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo header
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_MIGRATION_MULTIFD_COLO_H
+#define QEMU_MIGRATION_MULTIFD_COLO_H
+
+#ifdef CONFIG_REPLICATION
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p);
+void multifd_colo_process_recv(MultiFDRecvParams *p);
+
+#else
+
+static inline void multifd_colo_prepare_recv(MultiFDRecvParams *p) {}
+static inline void multifd_colo_process_recv(MultiFDRecvParams *p) {}
+
+#endif
+#endif
migration/multifd-nocomp.c +9 -1
index 9be79b3b8e00371ebff9e112766c225bec260bf7..9f7a792fa761b3bc30b971b35f464103a61787f0 100644
@@ -16,6 +16,7 @@
 #include "file.h"
 #include "migration-stats.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "migration.h"
 #include "qapi/error.h"
@@ -269,7 +270,6 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         return -1;
     }
 
-    p->host = p->block->host;
     for (i = 0; i < p->normal_num; i++) {
         uint64_t offset = be64_to_cpu(packet->offset[i]);
 
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;
+    } else {
+        p->host = p->block->host;
+    }
+
     return 0;
 }
 
migration/multifd.c +8 -0
index 332e6fc58053462419f3171f6c320ac37648ef7b..220ed8564960fdabc58e4baa069dd252c8ad293c 100644
@@ -29,6 +29,7 @@
 #include "qemu-file.h"
 #include "trace.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "qemu/yank.h"
 #include "io/channel-file.h"
@@ -1258,6 +1259,13 @@ static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp)
     int ret;
 
     ret = multifd_recv_state->ops->recv(p, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migrate_colo()) {
+        multifd_colo_process_recv(p);
+    }
 
     return ret;
 }
migration/multifd.h +4 -2
index 89a395aef2b09a6762c45b5361e0ab63256feff6..fbc35702b062fdc3213ce92baed35994f5967c2b 100644
@@ -279,7 +279,10 @@ typedef struct {
     uint64_t packets_recved;
     /* ramblock */
     RAMBlock *block;
-    /* ramblock host address */
+    /*
+     * Normally, it points to ramblock's host address.  When COLO
+     * is enabled, it points to the mirror cache for the ramblock.
+     */
     uint8_t *host;
     /* buffers to recv */
     struct iovec *iov;
 
--

2.39.5

#7 lore raw

Re: [PATCH v3 04/10] multifd: Add COLO support

Quoted 43 lines · On Mon, 26 Jan 2026 18:36:39 +0800 Zhang Chen <[email protected]> wrote:
Quoted 36 lines · Lukas Straub

Like in the normal ram_load() path, put the received pages into the colo cache and mark the pages in the bitmap so that they will be flushed to the guest later.

Multifd with COLO is useful to reduce the VM pause time during checkpointing for latency sensitive workloads. In such workloads the worst-case latency is especially important.

Also, this is already worth it for the precopy phase as it helps with converging. Moreover, multifd migration is the preferred way to do migration nowadays and this allows to use multifd compression with COLO.

Benchmark:
Cluster nodes
- Intel Xenon E5-2630 v3
- 48Gb RAM
- 10G Ethernet
Guest
- Windows Server 2016
- 6Gb RAM
- 4 cores
Workload
- Upload a file to the guest with SMB to simulate moderate memory dirtying
- Measure the memory transfer time portion of each checkpoint
- 600ms COLO checkpoint interval

Results
Plain
idle mean: 4.50ms 99per: 10.33ms
load mean: 24.30ms 99per: 78.05ms
Multifd-4
idle mean: 6.48ms 99per: 10.41ms
load mean: 14.12ms 99per: 31.27ms

Quoted 5 lines

Evaluation
While multifd has slightly higher latency when the guest idles, it is 10ms faster under load and more importantly it's worst case latency is less than 1/2 of plain under load as can be seen in the 99. Percentile.

Quoted 3 lines

Why the multifd get higher latency when the guest idles? The status same with normal live migration? Where is the time spent? The Sorry, I don't know this background yet.

Not sure, it could be more overhead due to coordinating the multifd threads.

But it also can be explained from the sample variation. Here I also calculate the standard deviation of the sample.
60% of samples are within +- one stddev.

plain idle: mean 4.50 99per 10.33 stddev 1.80 plain load: mean 24.30 99per 78.05 stddev 13.65 multifd-4 idle: mean 6.48 99per 10.41 stddev 2.53 multifd-4 load: mean 14.12 99per 31.27 stddev 7.48

So, I don't think its a significant difference.

Quoted 210 lines

Thanks
Chen

Quoted 206 lines
---
 MAINTAINERS                |  1 +
 migration/meson.build      |  2 +-
 migration/multifd-colo.c   | 50 ++++++++++++++++++++++++++++++++++++++++++++++
 migration/multifd-colo.h   | 26 ++++++++++++++++++++++++
 migration/multifd-nocomp.c | 10 +++++++++-
 migration/multifd.c        |  8 ++++++++
 migration/multifd.h        |  5 ++++-
 7 files changed, 99 insertions(+), 3 deletions(-)
MAINTAINERS +1 -0
index 1e9bdd87c3a2f84f3abfc56986cd793976810fdd..883f0a8f4eb92d0bf0f89fcab4674ccc4aed1cc1 100644
@@ -3853,6 +3853,7 @@ COLO Framework
 M: Lukas Straub <[email protected]>
 S: Maintained
 F: migration/colo*
+F: migration/multifd-colo.*
 F: include/migration/colo.h
 F: include/migration/failover.h
 F: docs/COLO-FT.txt
migration/meson.build +1 -1
index c7f39bdb55239ecb0e775c77b90a1aa9e6a4a9ce..c9f0f5f9f2137536497e53e960ce70654ad1b394 100644
@@ -39,7 +39,7 @@ system_ss.add(files(
 ), gnutls, zlib)
 
 if get_option('replication').allowed()
-  system_ss.add(files('colo-failover.c', 'colo.c'))
+  system_ss.add(files('colo-failover.c', 'colo.c', 'multifd-colo.c'))
 else
   system_ss.add(files('colo-stubs.c'))
 endif
migration/multifd-colo.c new file +50 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c47f5044663969e0c9af56da5ec34902d635810a
@@ -0,0 +1,50 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo implementation
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "exec/target_page.h"
+#include "qemu/error-report.h"
+#include "qapi/error.h"
+#include "ram.h"
+#include "multifd.h"
+#include "options.h"
+#include "io/channel-socket.h"
+#include "migration/colo.h"
+#include "multifd-colo.h"
+#include "system/ramblock.h"
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p)
+{
+    /*
+     * While we're still in precopy state (not yet in colo state), we copy
+     * received pages to both guest and cache. No need to set dirty bits,
+     * since guest and cache memory are in sync.
+     */
+    if (migration_incoming_in_colo_state()) {
+        colo_record_bitmap(p->block, p->normal, p->normal_num);
+        colo_record_bitmap(p->block, p->zero, p->zero_num);
+    }
+}
+
+void multifd_colo_process_recv(MultiFDRecvParams *p)
+{
+    if (!migration_incoming_in_colo_state()) {
+        for (int i = 0; i < p->normal_num; i++) {
+            void *guest = p->block->host + p->normal[i];
+            void *cache = p->host + p->normal[i];
+            memcpy(guest, cache, multifd_ram_page_size());
+        }
+        for (int i = 0; i < p->zero_num; i++) {
+            void *guest = p->block->host + p->zero[i];
+            memset(guest, 0, multifd_ram_page_size());
+        }
+    }
+}
migration/multifd-colo.h new file +26 -0
new file mode 100644
index 0000000000000000000000000000000000000000..82eaf3f48c47de2f090f9de52f9d57a337d4754a
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo header
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_MIGRATION_MULTIFD_COLO_H
+#define QEMU_MIGRATION_MULTIFD_COLO_H
+
+#ifdef CONFIG_REPLICATION
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p);
+void multifd_colo_process_recv(MultiFDRecvParams *p);
+
+#else
+
+static inline void multifd_colo_prepare_recv(MultiFDRecvParams *p) {}
+static inline void multifd_colo_process_recv(MultiFDRecvParams *p) {}
+
+#endif
+#endif
migration/multifd-nocomp.c +9 -1
index 9be79b3b8e00371ebff9e112766c225bec260bf7..9f7a792fa761b3bc30b971b35f464103a61787f0 100644
@@ -16,6 +16,7 @@
 #include "file.h"
 #include "migration-stats.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "migration.h"
 #include "qapi/error.h"
@@ -269,7 +270,6 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         return -1;
     }
 
-    p->host = p->block->host;
     for (i = 0; i < p->normal_num; i++) {
         uint64_t offset = be64_to_cpu(packet->offset[i]);
 
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;
+    } else {
+        p->host = p->block->host;
+    }
+
     return 0;
 }
 
migration/multifd.c +8 -0
index 332e6fc58053462419f3171f6c320ac37648ef7b..220ed8564960fdabc58e4baa069dd252c8ad293c 100644
@@ -29,6 +29,7 @@
 #include "qemu-file.h"
 #include "trace.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "qemu/yank.h"
 #include "io/channel-file.h"
@@ -1258,6 +1259,13 @@ static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp)
     int ret;
 
     ret = multifd_recv_state->ops->recv(p, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migrate_colo()) {
+        multifd_colo_process_recv(p);
+    }
 
     return ret;
 }
migration/multifd.h +4 -2
index 89a395aef2b09a6762c45b5361e0ab63256feff6..fbc35702b062fdc3213ce92baed35994f5967c2b 100644
@@ -279,7 +279,10 @@ typedef struct {
     uint64_t packets_recved;
     /* ramblock */
     RAMBlock *block;
-    /* ramblock host address */
+    /*
+     * Normally, it points to ramblock's host address.  When COLO
+     * is enabled, it points to the mirror cache for the ramblock.
+     */
     uint8_t *host;
     /* buffers to recv */
     struct iovec *iov;
 
--

2.39.5

#8 lore raw

Re: [PATCH v3 04/10] multifd: Add COLO support

In reply to Lukas Straub
Quoted 107 lines · Lukas Straub

Like in the normal ram_load() path, put the received pages into the colo cache and mark the pages in the bitmap so that they will be flushed to the guest later.

Multifd with COLO is useful to reduce the VM pause time during checkpointing for latency sensitive workloads. In such workloads the worst-case latency is especially important.

Also, this is already worth it for the precopy phase as it helps with converging. Moreover, multifd migration is the preferred way to do migration nowadays and this allows to use multifd compression with COLO.

Benchmark:
Cluster nodes
- Intel Xenon E5-2630 v3
- 48Gb RAM
- 10G Ethernet
Guest
- Windows Server 2016
- 6Gb RAM
- 4 cores
Workload
- Upload a file to the guest with SMB to simulate moderate memory dirtying
- Measure the memory transfer time portion of each checkpoint
- 600ms COLO checkpoint interval

Results
Plain
idle mean: 4.50ms 99per: 10.33ms
load mean: 24.30ms 99per: 78.05ms
Multifd-4
idle mean: 6.48ms 99per: 10.41ms
load mean: 14.12ms 99per: 31.27ms

Evaluation
While multifd has slightly higher latency when the guest idles, it is 10ms faster under load and more importantly it's worst case latency is less than 1/2 of plain under load as can be seen in the 99. Percentile.

---
 MAINTAINERS                |  1 +
 migration/meson.build      |  2 +-
 migration/multifd-colo.c   | 50 ++++++++++++++++++++++++++++++++++++++++++++++
 migration/multifd-colo.h   | 26 ++++++++++++++++++++++++
 migration/multifd-nocomp.c | 10 +++++++++-
 migration/multifd.c        |  8 ++++++++
 migration/multifd.h        |  5 ++++-
 7 files changed, 99 insertions(+), 3 deletions(-)
MAINTAINERS +1 -0
index 1e9bdd87c3a2f84f3abfc56986cd793976810fdd..883f0a8f4eb92d0bf0f89fcab4674ccc4aed1cc1 100644
@@ -3853,6 +3853,7 @@ COLO Framework
 M: Lukas Straub <[email protected]>
 S: Maintained
 F: migration/colo*
+F: migration/multifd-colo.*
 F: include/migration/colo.h
 F: include/migration/failover.h
 F: docs/COLO-FT.txt
migration/meson.build +1 -1
index c7f39bdb55239ecb0e775c77b90a1aa9e6a4a9ce..c9f0f5f9f2137536497e53e960ce70654ad1b394 100644
@@ -39,7 +39,7 @@ system_ss.add(files(
 ), gnutls, zlib)
 
 if get_option('replication').allowed()
-  system_ss.add(files('colo-failover.c', 'colo.c'))
+  system_ss.add(files('colo-failover.c', 'colo.c', 'multifd-colo.c'))
 else
   system_ss.add(files('colo-stubs.c'))
 endif
migration/multifd-colo.c new file +21 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c47f5044663969e0c9af56da5ec34902d635810a
@@ -0,0 +1,50 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo implementation
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "exec/target_page.h"
+#include "qemu/error-report.h"
+#include "qapi/error.h"
+#include "ram.h"
+#include "multifd.h"
+#include "options.h"
+#include "io/channel-socket.h"
+#include "migration/colo.h"
+#include "multifd-colo.h"

migration/multifd-colo.c line 31

Quoted 10 lines
migration/multifd-colo.c new file +10 -0
+#include "system/ramblock.h"
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p)
+{
+    /*
+     * While we're still in precopy state (not yet in colo state), we copy
+     * received pages to both guest and cache. No need to set dirty bits,
+     * since guest and cache memory are in sync.
+     */
+    if (migration_incoming_in_colo_state()) {

What's the relationship between migration_incoming_in_colo_state() and migration_incoming_colo_enabled()? ram_load_precopy() checks both. Would migration_incoming_colo_enabled affect multifd as well?

The multifd recv threads will be running until after process_incoming_migration_bh(), which is when migration_incoming_disable_colo() runs.

Also, is the colo_cache guaranteed to be around until multifd threads exit?

migration/multifd-colo.c line 44

Quoted 13 lines
migration/multifd-colo.c new file +13 -0
+        colo_record_bitmap(p->block, p->normal, p->normal_num);
+        colo_record_bitmap(p->block, p->zero, p->zero_num);
+    }
+}
+
+void multifd_colo_process_recv(MultiFDRecvParams *p)
+{
+    if (!migration_incoming_in_colo_state()) {
+        for (int i = 0; i < p->normal_num; i++) {
+            void *guest = p->block->host + p->normal[i];
+            void *cache = p->host + p->normal[i];
+            memcpy(guest, cache, multifd_ram_page_size());
+        }

I see some differences between what ram.c does and what multifd will do after this patch regarding which flags are checked and order of copies (code below):

ram.c:

- migration_incoming_colo_enabled && migration_incoming_in_colo_state:
Reads from stream into colo_cache.

- migration_incoming_colo_enabled && !migration_incoming_in_colo_state:
Reads from stream into guest and then memcpy into colo_cache.

- !migration_incoming_colo_enabled
Reads from stream into guest.

multifd.c:

- migrate_colo:
Reads from stream into colo_cache.

- !migration_incoming_in_colo_state:
memcpy from colo_cache into guest.

- !migration_incoming_colo_enabled
???

The resulting state should be the same, but I wonder if we want to i) use the same checks in multifd and ii) when not in colo state, copy first into guest (using readv) and later memcpy into the colo_cache.

---
ram.c:

host = host_from_ram_block_offset(block, addr);

if (migration_incoming_colo_enabled()) {
    if (migration_incoming_in_colo_state()) {
        host = colo_cache_from_block_offset(block, addr, true);
    } else {
        host_bak = colo_cache_from_block_offset(block, addr, false);
    }
}
qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
if (host_bak) {
    memcpy(host_bak, host, TARGET_PAGE_SIZE);
}

multifd:

if (migrate_colo()) {
    p->host = p->block->colo_cache;
}
for (int i = 0; i < p->normal_num; i++) {
    p->iov[i].iov_base = p->host + p->normal[i];
}

return qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);

if (!migration_incoming_in_colo_state()) {
    for (int i = 0; i < p->normal_num; i++) {
        void *guest = p->block->host + p->normal[i];
        void *cache = p->host + p->normal[i];
        memcpy(guest, cache, multifd_ram_page_size());
    }
}

---

migration/multifd-colo.c line 48

Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+        for (int i = 0; i < p->zero_num; i++) {
+            void *guest = p->block->host + p->zero[i];
+            memset(guest, 0, multifd_ram_page_size());
+        }

At multifd_nocomp_recv, there will be a call to multifd_recv_zero_page_process(), which by that point will have p->host == p->block->colo_cache, so it looks like that function will do some zero page processing in the colo_cache, setting the rb->receivedmap for pages in the colo_cache and potentially also doing a memcpy. Is this intended?

I'm thinking that maybe it would overall be better to hook colo directly in to multifd_nocomp_recv:

static int multifd_nocomp_recv(MultiFDRecvParams *p, Error **errp)

{
    uint32_t flags;

    if (migrate_mapped_ram()) {
        return multifd_file_recv_data(p, errp);
    }

    flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;

    if (flags != MULTIFD_FLAG_NOCOMP) {
        error_setg(errp, "multifd %u: flags received %x flags expected %x",
                   p->id, flags, MULTIFD_FLAG_NOCOMP);
        return -1;
    }

+ if (migration_incoming_colo_enabled() && migration_incoming_in_colo_state()) {
+ p->host = p->block->colo_cache;
+ } // or else{}, depending on how deal with zero pages in the cache

    multifd_recv_zero_page_process(p);

    if (!p->normal_num) {
        return 0;
    }

    for (int i = 0; i < p->normal_num; i++) {
        p->iov[i].iov_base = p->host + p->normal[i];
        p->iov[i].iov_len = multifd_ram_page_size();
        ramblock_recv_bitmap_set_offset(p->block, p->normal[i]);
    }

+ ret = qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);
+ if (ret != 0) {
+ return ret;
+ }
+
+ if (migration_incoming_colo_enabled()) {
+ multifd_colo_process_recv();
+ }

    return ret;
}
Quoted 55 lines
migration/multifd-colo.c new file +2 -0
+    }
+}
migration/multifd-colo.h new file +26 -0
new file mode 100644
index 0000000000000000000000000000000000000000..82eaf3f48c47de2f090f9de52f9d57a337d4754a
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo header
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_MIGRATION_MULTIFD_COLO_H
+#define QEMU_MIGRATION_MULTIFD_COLO_H
+
+#ifdef CONFIG_REPLICATION
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p);
+void multifd_colo_process_recv(MultiFDRecvParams *p);
+
+#else
+
+static inline void multifd_colo_prepare_recv(MultiFDRecvParams *p) {}
+static inline void multifd_colo_process_recv(MultiFDRecvParams *p) {}
+
+#endif
+#endif
migration/multifd-nocomp.c +1 -1
index 9be79b3b8e00371ebff9e112766c225bec260bf7..9f7a792fa761b3bc30b971b35f464103a61787f0 100644
@@ -16,6 +16,7 @@
 #include "file.h"
 #include "migration-stats.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "migration.h"
 #include "qapi/error.h"
@@ -269,7 +270,6 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         return -1;
     }
 
-    p->host = p->block->host;
     for (i = 0; i < p->normal_num; i++) {
         uint64_t offset = be64_to_cpu(packet->offset[i]);
 

migration/multifd-nocomp.c int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp) · line 300

Quoted 7 lines
migration/multifd-nocomp.c +4 -0
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;

Can't you just use p->block->colo_cache later? I don't see why p->host needs to be set beforehand even in the non-colo case.

Quoted 20 lines
migration/multifd-nocomp.c +4 -0
+    } else {
+        p->host = p->block->host;
+    }
+
     return 0;
 }
 
migration/multifd.c +1 -0
index 332e6fc58053462419f3171f6c320ac37648ef7b..220ed8564960fdabc58e4baa069dd252c8ad293c 100644
@@ -29,6 +29,7 @@
 #include "qemu-file.h"
 #include "trace.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "qemu/yank.h"
 #include "io/channel-file.h"

migration/multifd.c static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp) · line 1268

Quoted 10 lines
migration/multifd.c +7 -0
@@ -1258,6 +1259,13 @@ static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp)
     int ret;
 
     ret = multifd_recv_state->ops->recv(p, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migrate_colo()) {
+        multifd_colo_process_recv(p);
+    }

Either put all of colo hooks in multifd.c or multifd-nocomp.c. I think the latter is more appropriate as we have mapped_ram already in there. Let's drop patch 3 and put this in multifd_nocomp_recv().

Quoted 19 lines
migration/multifd.c +0 -0
     return ret;
 }
migration/multifd.h +4 -1
index 89a395aef2b09a6762c45b5361e0ab63256feff6..fbc35702b062fdc3213ce92baed35994f5967c2b 100644
@@ -279,7 +279,10 @@ typedef struct {
     uint64_t packets_recved;
     /* ramblock */
     RAMBlock *block;
-    /* ramblock host address */
+    /*
+     * Normally, it points to ramblock's host address.  When COLO
+     * is enabled, it points to the mirror cache for the ramblock.
+     */
     uint8_t *host;
     /* buffers to recv */
     struct iovec *iov;
#9 lore raw

Re: [PATCH v3 04/10] multifd: Add COLO support

Quoted 115 lines · On Mon, 26 Jan 2026 11:33:13 -0300 Fabiano Rosas <[email protected]> wrote:
Quoted 113 lines · Lukas Straub

Like in the normal ram_load() path, put the received pages into the colo cache and mark the pages in the bitmap so that they will be flushed to the guest later.

Multifd with COLO is useful to reduce the VM pause time during checkpointing for latency sensitive workloads. In such workloads the worst-case latency is especially important.

Also, this is already worth it for the precopy phase as it helps with converging. Moreover, multifd migration is the preferred way to do migration nowadays and this allows to use multifd compression with COLO.

Benchmark:
Cluster nodes
- Intel Xenon E5-2630 v3
- 48Gb RAM
- 10G Ethernet
Guest
- Windows Server 2016
- 6Gb RAM
- 4 cores
Workload
- Upload a file to the guest with SMB to simulate moderate memory dirtying
- Measure the memory transfer time portion of each checkpoint
- 600ms COLO checkpoint interval

Results
Plain
idle mean: 4.50ms 99per: 10.33ms
load mean: 24.30ms 99per: 78.05ms
Multifd-4
idle mean: 6.48ms 99per: 10.41ms
load mean: 14.12ms 99per: 31.27ms

Evaluation
While multifd has slightly higher latency when the guest idles, it is 10ms faster under load and more importantly it's worst case latency is less than 1/2 of plain under load as can be seen in the 99. Percentile.

---
 MAINTAINERS                |  1 +
 migration/meson.build      |  2 +-
 migration/multifd-colo.c   | 50 ++++++++++++++++++++++++++++++++++++++++++++++
 migration/multifd-colo.h   | 26 ++++++++++++++++++++++++
 migration/multifd-nocomp.c | 10 +++++++++-
 migration/multifd.c        |  8 ++++++++
 migration/multifd.h        |  5 ++++-
 7 files changed, 99 insertions(+), 3 deletions(-)
MAINTAINERS +1 -0
index 1e9bdd87c3a2f84f3abfc56986cd793976810fdd..883f0a8f4eb92d0bf0f89fcab4674ccc4aed1cc1 100644
@@ -3853,6 +3853,7 @@ COLO Framework
 M: Lukas Straub <[email protected]>
 S: Maintained
 F: migration/colo*
+F: migration/multifd-colo.*
 F: include/migration/colo.h
 F: include/migration/failover.h
 F: docs/COLO-FT.txt
migration/meson.build +1 -1
index c7f39bdb55239ecb0e775c77b90a1aa9e6a4a9ce..c9f0f5f9f2137536497e53e960ce70654ad1b394 100644
@@ -39,7 +39,7 @@ system_ss.add(files(
 ), gnutls, zlib)
 
 if get_option('replication').allowed()
-  system_ss.add(files('colo-failover.c', 'colo.c'))
+  system_ss.add(files('colo-failover.c', 'colo.c', 'multifd-colo.c'))
 else
   system_ss.add(files('colo-stubs.c'))
 endif
migration/multifd-colo.c new file +27 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c47f5044663969e0c9af56da5ec34902d635810a
@@ -0,0 +1,50 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo implementation
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "exec/target_page.h"
+#include "qemu/error-report.h"
+#include "qapi/error.h"
+#include "ram.h"
+#include "multifd.h"
+#include "options.h"
+#include "io/channel-socket.h"
+#include "migration/colo.h"
+#include "multifd-colo.h"
+#include "system/ramblock.h"
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p)
+{
+    /*
+     * While we're still in precopy state (not yet in colo state), we copy
Quoted 8 lines

migration/multifd-colo.c line 31

Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+     * received pages to both guest and cache. No need to set dirty bits,
+     * since guest and cache memory are in sync.
+     */
+    if (migration_incoming_in_colo_state()) {  

What's the relationship between migration_incoming_in_colo_state() and migration_incoming_colo_enabled()? ram_load_precopy() checks both. Would migration_incoming_colo_enabled affect multifd as well?

So first off migration_incoming_colo_enabled() and migrate_colo() are equivalent in practice since
121ccedc2b migration: block incoming colo when capability is disabled

(I have some cleanup patches lying around, but that will be for later)

For colo, we do normal precopy migration and at the end we go into colo state and then
migration_incoming_in_colo_state() will be true.

Here we check migrate_colo() outside of these functions as Peter requested that in a previous version.

Quoted 4 lines

The multifd recv threads will be running until after process_incoming_migration_bh(), which is when migration_incoming_disable_colo() runs.

That is not an issue as we use migrate_colo() here.

Quoted 3 lines

Also, is the colo_cache guaranteed to be around until multifd threads exit?

This is an issue. I will fix it in the next version.

Quoted 35 lines
Quoted 4 lines
migration/multifd-colo.c new file +3 -0
+        colo_record_bitmap(p->block, p->normal, p->normal_num);
+        colo_record_bitmap(p->block, p->zero, p->zero_num);
+    }

migration/multifd-colo.c line 44

Quoted 10 lines
migration/multifd-colo.c new file +10 -0
+}
+
+void multifd_colo_process_recv(MultiFDRecvParams *p)
+{
+    if (!migration_incoming_in_colo_state()) {
+        for (int i = 0; i < p->normal_num; i++) {
+            void *guest = p->block->host + p->normal[i];
+            void *cache = p->host + p->normal[i];
+            memcpy(guest, cache, multifd_ram_page_size());
+        }  

I see some differences between what ram.c does and what multifd will do after this patch regarding which flags are checked and order of copies (code below):

ram.c:

- migration_incoming_colo_enabled && migration_incoming_in_colo_state:
Reads from stream into colo_cache.

- migration_incoming_colo_enabled && !migration_incoming_in_colo_state:
Reads from stream into guest and then memcpy into colo_cache.

- !migration_incoming_colo_enabled
Reads from stream into guest.

multifd.c:

- migrate_colo:
Reads from stream into colo_cache.

Quoted 8 lines

- !migration_incoming_in_colo_state:
memcpy from colo_cache into guest.

- !migration_incoming_colo_enabled
???

The resulting state should be the same, but I wonder if we want to i) use the same checks in multifd

migration_incoming_colo_enabled() shouldn't even exist anymore, so I'm not using it here. migrate_colo() is much easier to reason about.

Quoted 2 lines

and ii) when not in colo state, copy first
into guest (using readv) and later memcpy into the colo_cache.

I think it is easier the way it is now.

Quoted 42 lines

---
ram.c:

host = host_from_ram_block_offset(block, addr);

if (migration_incoming_colo_enabled()) {
    if (migration_incoming_in_colo_state()) {
        host = colo_cache_from_block_offset(block, addr, true);
    } else {
        host_bak = colo_cache_from_block_offset(block, addr, false);
    }
}
qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
if (host_bak) {
    memcpy(host_bak, host, TARGET_PAGE_SIZE);
}

multifd:

if (migrate_colo()) {
    p->host = p->block->colo_cache;
}
for (int i = 0; i < p->normal_num; i++) {
    p->iov[i].iov_base = p->host + p->normal[i];
}

return qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);

if (!migration_incoming_in_colo_state()) {
    for (int i = 0; i < p->normal_num; i++) {
        void *guest = p->block->host + p->normal[i];
        void *cache = p->host + p->normal[i];
        memcpy(guest, cache, multifd_ram_page_size());
    }
}

---

migration/multifd-colo.c line 48

Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+        for (int i = 0; i < p->zero_num; i++) {
+            void *guest = p->block->host + p->zero[i];
+            memset(guest, 0, multifd_ram_page_size());
+        }  
Quoted 6 lines

At multifd_nocomp_recv, there will be a call to multifd_recv_zero_page_process(), which by that point will have p->host == p->block->colo_cache, so it looks like that function will do some zero page processing in the colo_cache, setting the rb->receivedmap for pages in the colo_cache and potentially also doing a memcpy. Is this intended?

rb->receivedmap is only for postcopy, right? So it doesn't apply with colo.

Quoted 3 lines

I'm thinking that maybe it would overall be better to hook colo directly in to multifd_nocomp_recv:

But then it will only work for nocomp, right? It feels like the wrong level of abstraction to me.

Quoted 103 lines

static int multifd_nocomp_recv(MultiFDRecvParams *p, Error **errp)

{
    uint32_t flags;

    if (migrate_mapped_ram()) {
        return multifd_file_recv_data(p, errp);
    }

    flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;

    if (flags != MULTIFD_FLAG_NOCOMP) {
        error_setg(errp, "multifd %u: flags received %x flags expected %x",
                   p->id, flags, MULTIFD_FLAG_NOCOMP);
        return -1;
    }
(quoted diff) +11 -0
+    if (migration_incoming_colo_enabled() && migration_incoming_in_colo_state()) {
+        p->host = p->block->colo_cache;
+    } // or else{}, depending on how deal with zero pages in the cache
 
    multifd_recv_zero_page_process(p);
 
    if (!p->normal_num) {
        return 0;
    }
 
    for (int i = 0; i < p->normal_num; i++) {
        p->iov[i].iov_base = p->host + p->normal[i];
        p->iov[i].iov_len = multifd_ram_page_size();
        ramblock_recv_bitmap_set_offset(p->block, p->normal[i]);
    }
+    ret = qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migration_incoming_colo_enabled()) {
+        multifd_colo_process_recv();
+    }
 
    return ret;

}

Quoted 58 lines
migration/multifd-colo.c new file +2 -0
+    }
+}
migration/multifd-colo.h new file +26 -0
new file mode 100644
index 0000000000000000000000000000000000000000..82eaf3f48c47de2f090f9de52f9d57a337d4754a
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo header
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_MIGRATION_MULTIFD_COLO_H
+#define QEMU_MIGRATION_MULTIFD_COLO_H
+
+#ifdef CONFIG_REPLICATION
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p);
+void multifd_colo_process_recv(MultiFDRecvParams *p);
+
+#else
+
+static inline void multifd_colo_prepare_recv(MultiFDRecvParams *p) {}
+static inline void multifd_colo_process_recv(MultiFDRecvParams *p) {}
+
+#endif
+#endif
migration/multifd-nocomp.c +1 -1
index 9be79b3b8e00371ebff9e112766c225bec260bf7..9f7a792fa761b3bc30b971b35f464103a61787f0 100644
@@ -16,6 +16,7 @@
 #include "file.h"
 #include "migration-stats.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "migration.h"
 #include "qapi/error.h"
@@ -269,7 +270,6 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         return -1;
     }
 
-    p->host = p->block->host;
     for (i = 0; i < p->normal_num; i++) {
         uint64_t offset = be64_to_cpu(packet->offset[i]);
 
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
Quoted 7 lines

migration/multifd-nocomp.c int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp) · line 300

Quoted 4 lines
migration/multifd-nocomp.c +4 -0
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;  

Can't you just use p->block->colo_cache later? I don't see why p->host needs to be set beforehand even in the non-colo case.

We should not touch the guest ram directly while in colo state, since the incoming guest is running and we either want to receive and apply a whole checkpoint with all ram into colo cache and all device state, or if anything goes wrong during checkpointing, keep the currently running guest on the incoming side in pristine state.

I have written more about colo migration here:

https://lore.kernel.org/qemu-devel/20260117204913.584e1829@penguin/
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 23 lines
Quoted 23 lines
migration/multifd-nocomp.c +4 -0
+    } else {
+        p->host = p->block->host;
+    }
+
     return 0;
 }
 
migration/multifd.c +1 -0
index 332e6fc58053462419f3171f6c320ac37648ef7b..220ed8564960fdabc58e4baa069dd252c8ad293c 100644
@@ -29,6 +29,7 @@
 #include "qemu-file.h"
 #include "trace.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "qemu/yank.h"
 #include "io/channel-file.h"
@@ -1258,6 +1259,13 @@ static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp)
     int ret;
 
Quoted 12 lines

migration/multifd.c static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp) · line 1268

Quoted 8 lines
migration/multifd.c +7 -0
     ret = multifd_recv_state->ops->recv(p, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migrate_colo()) {
+        multifd_colo_process_recv(p);
+    }  

Either put all of colo hooks in multifd.c or multifd-nocomp.c. I think the latter is more appropriate as we have mapped_ram already in there. Let's drop patch 3 and put this in multifd_nocomp_recv().

Again, it also should work with compression and multifd_nocomp_recv() is for nocomp only.

Quoted 20 lines
Quoted 20 lines
migration/multifd.c +0 -0
     return ret;
 }
migration/multifd.h +4 -1
index 89a395aef2b09a6762c45b5361e0ab63256feff6..fbc35702b062fdc3213ce92baed35994f5967c2b 100644
@@ -279,7 +279,10 @@ typedef struct {
     uint64_t packets_recved;
     /* ramblock */
     RAMBlock *block;
-    /* ramblock host address */
+    /*
+     * Normally, it points to ramblock's host address.  When COLO
+     * is enabled, it points to the mirror cache for the ramblock.
+     */
     uint8_t *host;
     /* buffers to recv */
     struct iovec *iov;  
#10 lore raw

Re: [PATCH v3 04/10] multifd: Add COLO support

Quoted 13 lines · Lukas Straub
Quoted 4 lines
Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+void multifd_colo_prepare_recv(MultiFDRecvParams *p)
+{
+    /*
+     * While we're still in precopy state (not yet in colo state), we copy
Quoted 8 lines

migration/multifd-colo.c line 31

Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+     * received pages to both guest and cache. No need to set dirty bits,
+     * since guest and cache memory are in sync.
+     */
+    if (migration_incoming_in_colo_state()) {  

What's the relationship between migration_incoming_in_colo_state() and migration_incoming_colo_enabled()? ram_load_precopy() checks both. Would migration_incoming_colo_enabled affect multifd as well?

Quoted 6 lines

So first off migration_incoming_colo_enabled() and migrate_colo() are equivalent in practice since
121ccedc2b migration: block incoming colo when capability is disabled

(I have some cleanup patches lying around, but that will be for later)

Ok, I think those are important because when having multifd and non-multifd code for the same feature, it's useful to be able to compare the two. So some degree of uniformity would be nice.

Quoted 121 lines

For colo, we do normal precopy migration and at the end we go into colo state and then
migration_incoming_in_colo_state() will be true.

Here we check migrate_colo() outside of these functions as Peter requested that in a previous version.

Quoted 4 lines

The multifd recv threads will be running until after process_incoming_migration_bh(), which is when migration_incoming_disable_colo() runs.

That is not an issue as we use migrate_colo() here.

Quoted 3 lines

Also, is the colo_cache guaranteed to be around until multifd threads exit?

This is an issue. I will fix it in the next version.

Quoted 35 lines
Quoted 4 lines
migration/multifd-colo.c new file +3 -0
+        colo_record_bitmap(p->block, p->normal, p->normal_num);
+        colo_record_bitmap(p->block, p->zero, p->zero_num);
+    }

migration/multifd-colo.c line 44

Quoted 10 lines
migration/multifd-colo.c new file +10 -0
+}
+
+void multifd_colo_process_recv(MultiFDRecvParams *p)
+{
+    if (!migration_incoming_in_colo_state()) {
+        for (int i = 0; i < p->normal_num; i++) {
+            void *guest = p->block->host + p->normal[i];
+            void *cache = p->host + p->normal[i];
+            memcpy(guest, cache, multifd_ram_page_size());
+        }  

I see some differences between what ram.c does and what multifd will do after this patch regarding which flags are checked and order of copies (code below):

ram.c:

- migration_incoming_colo_enabled && migration_incoming_in_colo_state:
Reads from stream into colo_cache.

- migration_incoming_colo_enabled && !migration_incoming_in_colo_state:
Reads from stream into guest and then memcpy into colo_cache.

- !migration_incoming_colo_enabled
Reads from stream into guest.

multifd.c:

- migrate_colo:
Reads from stream into colo_cache.

Quoted 8 lines

- !migration_incoming_in_colo_state:
memcpy from colo_cache into guest.

- !migration_incoming_colo_enabled
???

The resulting state should be the same, but I wonder if we want to i) use the same checks in multifd

migration_incoming_colo_enabled() shouldn't even exist anymore, so I'm not using it here. migrate_colo() is much easier to reason about.

Quoted 2 lines

and ii) when not in colo state, copy first
into guest (using readv) and later memcpy into the colo_cache.

I think it is easier the way it is now.

Quoted 42 lines

---
ram.c:

host = host_from_ram_block_offset(block, addr);

if (migration_incoming_colo_enabled()) {
    if (migration_incoming_in_colo_state()) {
        host = colo_cache_from_block_offset(block, addr, true);
    } else {
        host_bak = colo_cache_from_block_offset(block, addr, false);
    }
}
qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
if (host_bak) {
    memcpy(host_bak, host, TARGET_PAGE_SIZE);
}

multifd:

if (migrate_colo()) {
    p->host = p->block->colo_cache;
}
for (int i = 0; i < p->normal_num; i++) {
    p->iov[i].iov_base = p->host + p->normal[i];
}

return qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);

if (!migration_incoming_in_colo_state()) {
    for (int i = 0; i < p->normal_num; i++) {
        void *guest = p->block->host + p->normal[i];
        void *cache = p->host + p->normal[i];
        memcpy(guest, cache, multifd_ram_page_size());
    }
}

---

migration/multifd-colo.c line 48

Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+        for (int i = 0; i < p->zero_num; i++) {
+            void *guest = p->block->host + p->zero[i];
+            memset(guest, 0, multifd_ram_page_size());
+        }  
Quoted 6 lines

At multifd_nocomp_recv, there will be a call to multifd_recv_zero_page_process(), which by that point will have p->host == p->block->colo_cache, so it looks like that function will do some zero page processing in the colo_cache, setting the rb->receivedmap for pages in the colo_cache and potentially also doing a memcpy. Is this intended?

Quoted 3 lines

rb->receivedmap is only for postcopy, right? So it doesn't apply with colo.

It's not anymore since commit 5ef7e26bdb ("migration/multifd: solve zero page causing multiple page faults"). So it seems we might be doing extra work on top of the colo_cache.

Quoted 7 lines
Quoted 3 lines

I'm thinking that maybe it would overall be better to hook colo directly in to multifd_nocomp_recv:

But then it will only work for nocomp, right? It feels like the wrong level of abstraction to me.

Ah, nocomp != ram indeed.

Quoted 111 lines
Quoted 103 lines

static int multifd_nocomp_recv(MultiFDRecvParams *p, Error **errp)

{
    uint32_t flags;

    if (migrate_mapped_ram()) {
        return multifd_file_recv_data(p, errp);
    }

    flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;

    if (flags != MULTIFD_FLAG_NOCOMP) {
        error_setg(errp, "multifd %u: flags received %x flags expected %x",
                   p->id, flags, MULTIFD_FLAG_NOCOMP);
        return -1;
    }
(quoted diff) +11 -0
+    if (migration_incoming_colo_enabled() && migration_incoming_in_colo_state()) {
+        p->host = p->block->colo_cache;
+    } // or else{}, depending on how deal with zero pages in the cache
 
    multifd_recv_zero_page_process(p);
 
    if (!p->normal_num) {
        return 0;
    }
 
    for (int i = 0; i < p->normal_num; i++) {
        p->iov[i].iov_base = p->host + p->normal[i];
        p->iov[i].iov_len = multifd_ram_page_size();
        ramblock_recv_bitmap_set_offset(p->block, p->normal[i]);
    }
+    ret = qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migration_incoming_colo_enabled()) {
+        multifd_colo_process_recv();
+    }
 
    return ret;

}

Quoted 58 lines
(quoted diff) +2 -0
+    }
+}
migration/multifd-colo.h new file +26 -0
new file mode 100644
index 0000000000000000000000000000000000000000..82eaf3f48c47de2f090f9de52f9d57a337d4754a
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo header
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_MIGRATION_MULTIFD_COLO_H
+#define QEMU_MIGRATION_MULTIFD_COLO_H
+
+#ifdef CONFIG_REPLICATION
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p);
+void multifd_colo_process_recv(MultiFDRecvParams *p);
+
+#else
+
+static inline void multifd_colo_prepare_recv(MultiFDRecvParams *p) {}
+static inline void multifd_colo_process_recv(MultiFDRecvParams *p) {}
+
+#endif
+#endif
migration/multifd-nocomp.c +1 -1
index 9be79b3b8e00371ebff9e112766c225bec260bf7..9f7a792fa761b3bc30b971b35f464103a61787f0 100644
@@ -16,6 +16,7 @@
 #include "file.h"
 #include "migration-stats.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "migration.h"
 #include "qapi/error.h"
@@ -269,7 +270,6 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         return -1;
     }
 
-    p->host = p->block->host;
     for (i = 0; i < p->normal_num; i++) {
         uint64_t offset = be64_to_cpu(packet->offset[i]);
 
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
Quoted 7 lines

migration/multifd-nocomp.c int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp) · line 300

Quoted 4 lines
migration/multifd-nocomp.c +4 -0
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;  

Can't you just use p->block->colo_cache later? I don't see why p->host needs to be set beforehand even in the non-colo case.

Quoted 6 lines

We should not touch the guest ram directly while in colo state, since the incoming guest is running and we either want to receive and apply a whole checkpoint with all ram into colo cache and all device state, or if anything goes wrong during checkpointing, keep the currently running guest on the incoming side in pristine state.

I was asking about setting p->host at this specific point. I don't think any of this fits the unfill function. However, I see those were suggested by Peter so let's not go back and forth.

Quoted 64 lines

I have written more about colo migration here:

https://lore.kernel.org/qemu-devel/20260117204913.584e1829@penguin/
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 23 lines
Quoted 23 lines
migration/multifd-nocomp.c +4 -0
+    } else {
+        p->host = p->block->host;
+    }
+
     return 0;
 }
 
migration/multifd.c +1 -0
index 332e6fc58053462419f3171f6c320ac37648ef7b..220ed8564960fdabc58e4baa069dd252c8ad293c 100644
@@ -29,6 +29,7 @@
 #include "qemu-file.h"
 #include "trace.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "qemu/yank.h"
 #include "io/channel-file.h"
@@ -1258,6 +1259,13 @@ static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp)
     int ret;
 
Quoted 12 lines

migration/multifd.c static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp) · line 1268

Quoted 8 lines
migration/multifd.c +7 -0
     ret = multifd_recv_state->ops->recv(p, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migrate_colo()) {
+        multifd_colo_process_recv(p);
+    }  

Either put all of colo hooks in multifd.c or multifd-nocomp.c. I think the latter is more appropriate as we have mapped_ram already in there. Let's drop patch 3 and put this in multifd_nocomp_recv().

Again, it also should work with compression and multifd_nocomp_recv() is for nocomp only.

Quoted 20 lines
Quoted 20 lines
migration/multifd.c +0 -0
     return ret;
 }
migration/multifd.h +4 -1
index 89a395aef2b09a6762c45b5361e0ab63256feff6..fbc35702b062fdc3213ce92baed35994f5967c2b 100644
@@ -279,7 +279,10 @@ typedef struct {
     uint64_t packets_recved;
     /* ramblock */
     RAMBlock *block;
-    /* ramblock host address */
+    /*
+     * Normally, it points to ramblock's host address.  When COLO
+     * is enabled, it points to the mirror cache for the ramblock.
+     */
     uint8_t *host;
     /* buffers to recv */
     struct iovec *iov;  
#11 lore raw

Re: [PATCH v3 04/10] multifd: Add COLO support

Quoted 19 lines · Fabiano Rosas
Quoted 17 lines · Lukas Straub
Quoted 4 lines
Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+void multifd_colo_prepare_recv(MultiFDRecvParams *p)
+{
+    /*
+     * While we're still in precopy state (not yet in colo state), we copy
Quoted 8 lines

migration/multifd-colo.c line 31

Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+     * received pages to both guest and cache. No need to set dirty bits,
+     * since guest and cache memory are in sync.
+     */
+    if (migration_incoming_in_colo_state()) {  

What's the relationship between migration_incoming_in_colo_state() and migration_incoming_colo_enabled()? ram_load_precopy() checks both. Would migration_incoming_colo_enabled affect multifd as well?

So first off migration_incoming_colo_enabled() and migrate_colo() are equivalent in practice since
121ccedc2b migration: block incoming colo when capability is disabled

Quoted 6 lines
Quoted 2 lines

(I have some cleanup patches lying around, but that will be for later)

Ok, I think those are important because when having multifd and non-multifd code for the same feature, it's useful to be able to compare the two. So some degree of uniformity would be nice.

I second. We can drop those in this series before adding multifd support, likely together with MIG_CMD_ENABLE_COLO as well; I don't think COLO needs to worry about old binaries. It should always use the same QEMU binary on both sides.

The patch needs to rename MIG_CMD_ENABLE_COLO to MIG_CMD_DEPRECATED_0 or something, to make the rest MIG_CMD compatible to old binaries, though.

Quoted 122 lines
Quoted 122 lines

For colo, we do normal precopy migration and at the end we go into colo state and then
migration_incoming_in_colo_state() will be true.

Here we check migrate_colo() outside of these functions as Peter requested that in a previous version.

Quoted 4 lines

The multifd recv threads will be running until after process_incoming_migration_bh(), which is when migration_incoming_disable_colo() runs.

That is not an issue as we use migrate_colo() here.

Quoted 3 lines

Also, is the colo_cache guaranteed to be around until multifd threads exit?

This is an issue. I will fix it in the next version.

Quoted 35 lines
Quoted 4 lines
migration/multifd-colo.c new file +3 -0
+        colo_record_bitmap(p->block, p->normal, p->normal_num);
+        colo_record_bitmap(p->block, p->zero, p->zero_num);
+    }

migration/multifd-colo.c line 44

Quoted 10 lines
migration/multifd-colo.c new file +10 -0
+}
+
+void multifd_colo_process_recv(MultiFDRecvParams *p)
+{
+    if (!migration_incoming_in_colo_state()) {
+        for (int i = 0; i < p->normal_num; i++) {
+            void *guest = p->block->host + p->normal[i];
+            void *cache = p->host + p->normal[i];
+            memcpy(guest, cache, multifd_ram_page_size());
+        }  

I see some differences between what ram.c does and what multifd will do after this patch regarding which flags are checked and order of copies (code below):

ram.c:

- migration_incoming_colo_enabled && migration_incoming_in_colo_state:
Reads from stream into colo_cache.

- migration_incoming_colo_enabled && !migration_incoming_in_colo_state:
Reads from stream into guest and then memcpy into colo_cache.

- !migration_incoming_colo_enabled
Reads from stream into guest.

multifd.c:

- migrate_colo:
Reads from stream into colo_cache.

Quoted 8 lines

- !migration_incoming_in_colo_state:
memcpy from colo_cache into guest.

- !migration_incoming_colo_enabled
???

The resulting state should be the same, but I wonder if we want to i) use the same checks in multifd

migration_incoming_colo_enabled() shouldn't even exist anymore, so I'm not using it here. migrate_colo() is much easier to reason about.

Quoted 2 lines

and ii) when not in colo state, copy first
into guest (using readv) and later memcpy into the colo_cache.

I think it is easier the way it is now.

Quoted 48 lines

---
ram.c:

host = host_from_ram_block_offset(block, addr);

if (migration_incoming_colo_enabled()) {
    if (migration_incoming_in_colo_state()) {
        host = colo_cache_from_block_offset(block, addr, true);
    } else {
        host_bak = colo_cache_from_block_offset(block, addr, false);
    }
}
qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
if (host_bak) {
    memcpy(host_bak, host, TARGET_PAGE_SIZE);
}

multifd:

if (migrate_colo()) {
    p->host = p->block->colo_cache;
}
for (int i = 0; i < p->normal_num; i++) {
    p->iov[i].iov_base = p->host + p->normal[i];
}

return qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);

if (!migration_incoming_in_colo_state()) {
    for (int i = 0; i < p->normal_num; i++) {
        void *guest = p->block->host + p->normal[i];
        void *cache = p->host + p->normal[i];
        memcpy(guest, cache, multifd_ram_page_size());
    }
}

---

migration/multifd-colo.c line 48

Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+        for (int i = 0; i < p->zero_num; i++) {
+            void *guest = p->block->host + p->zero[i];
+            memset(guest, 0, multifd_ram_page_size());
+        }  

At multifd_nocomp_recv, there will be a call to multifd_recv_zero_page_process(), which by that point will have p->host == p->block->colo_cache, so it looks like that function will do some zero page processing in the colo_cache, setting the rb->receivedmap for pages in the colo_cache and potentially also doing a memcpy. Is this intended?

Quoted 7 lines
Quoted 3 lines

rb->receivedmap is only for postcopy, right? So it doesn't apply with colo.

It's not anymore since commit 5ef7e26bdb ("migration/multifd: solve zero page causing multiple page faults"). So it seems we might be doing extra work on top of the colo_cache.

IIUC not extra, but exactly what will be needed.

The logic was about "in a vanilla precopy, if we see one page arriving the 1st time we don't need to zero the buffer because the buffer should be zero allocated".

In COLO's case, COLO always puts RAM data into colo_cache, hence it should apply to colo_cache too, avoiding unnecessary memset() for colo_cache instead.

E.g. colo_cache is allocated from qemu_anon_ram_alloc(), it's also guaranteed to be zeros when never touched.

Quoted 129 lines
Quoted 8 lines
Quoted 4 lines

I'm thinking that maybe it would overall be better to hook colo directly in to multifd_nocomp_recv:

But then it will only work for nocomp, right? It feels like the wrong level of abstraction to me.

Ah, nocomp != ram indeed.

Quoted 111 lines
Quoted 103 lines

static int multifd_nocomp_recv(MultiFDRecvParams *p, Error **errp)

{
    uint32_t flags;

    if (migrate_mapped_ram()) {
        return multifd_file_recv_data(p, errp);
    }

    flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;

    if (flags != MULTIFD_FLAG_NOCOMP) {
        error_setg(errp, "multifd %u: flags received %x flags expected %x",
                   p->id, flags, MULTIFD_FLAG_NOCOMP);
        return -1;
    }
(quoted diff) +11 -0
+    if (migration_incoming_colo_enabled() && migration_incoming_in_colo_state()) {
+        p->host = p->block->colo_cache;
+    } // or else{}, depending on how deal with zero pages in the cache
 
    multifd_recv_zero_page_process(p);
 
    if (!p->normal_num) {
        return 0;
    }
 
    for (int i = 0; i < p->normal_num; i++) {
        p->iov[i].iov_base = p->host + p->normal[i];
        p->iov[i].iov_len = multifd_ram_page_size();
        ramblock_recv_bitmap_set_offset(p->block, p->normal[i]);
    }
+    ret = qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migration_incoming_colo_enabled()) {
+        multifd_colo_process_recv();
+    }
 
    return ret;

}

Quoted 58 lines
(quoted diff) +2 -0
+    }
+}
migration/multifd-colo.h new file +26 -0
new file mode 100644
index 0000000000000000000000000000000000000000..82eaf3f48c47de2f090f9de52f9d57a337d4754a
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo header
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_MIGRATION_MULTIFD_COLO_H
+#define QEMU_MIGRATION_MULTIFD_COLO_H
+
+#ifdef CONFIG_REPLICATION
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p);
+void multifd_colo_process_recv(MultiFDRecvParams *p);
+
+#else
+
+static inline void multifd_colo_prepare_recv(MultiFDRecvParams *p) {}
+static inline void multifd_colo_process_recv(MultiFDRecvParams *p) {}
+
+#endif
+#endif
migration/multifd-nocomp.c +1 -1
index 9be79b3b8e00371ebff9e112766c225bec260bf7..9f7a792fa761b3bc30b971b35f464103a61787f0 100644
@@ -16,6 +16,7 @@
 #include "file.h"
 #include "migration-stats.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "migration.h"
 #include "qapi/error.h"
@@ -269,7 +270,6 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         return -1;
     }
 
-    p->host = p->block->host;
     for (i = 0; i < p->normal_num; i++) {
         uint64_t offset = be64_to_cpu(packet->offset[i]);
 
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
Quoted 7 lines

migration/multifd-nocomp.c int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp) · line 300

Quoted 4 lines
migration/multifd-nocomp.c +4 -0
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;  

Can't you just use p->block->colo_cache later? I don't see why p->host needs to be set beforehand even in the non-colo case.

Quoted 6 lines

We should not touch the guest ram directly while in colo state, since the incoming guest is running and we either want to receive and apply a whole checkpoint with all ram into colo cache and all device state, or if anything goes wrong during checkpointing, keep the currently running guest on the incoming side in pristine state.

Quoted 3 lines

I was asking about setting p->host at this specific point. I don't think any of this fits the unfill function. However, I see those were suggested by Peter so let's not go back and forth.

Actually I don't know why p->host existed before this work; IIUC we could have always used p->block->host. Maybe when Juan was developing this Juan kept COLO in mind; or maybe Juan wanted to avoid frequent p->block pointer reference.

IIUC, we could remove p->host, but when we need to access "the buffer of the ramblock" we'll need to call a helper to fetch that (either ramblock's buffer, or colo_cache, per migrate_colo()). And it might be slightly slower than p->host indeed.

Quoted 66 lines
Quoted 65 lines

I have written more about colo migration here:

https://lore.kernel.org/qemu-devel/20260117204913.584e1829@penguin/
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 23 lines
Quoted 23 lines
migration/multifd-nocomp.c +4 -0
+    } else {
+        p->host = p->block->host;
+    }
+
     return 0;
 }
 
migration/multifd.c +1 -0
index 332e6fc58053462419f3171f6c320ac37648ef7b..220ed8564960fdabc58e4baa069dd252c8ad293c 100644
@@ -29,6 +29,7 @@
 #include "qemu-file.h"
 #include "trace.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "qemu/yank.h"
 #include "io/channel-file.h"
@@ -1258,6 +1259,13 @@ static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp)
     int ret;
 
Quoted 12 lines

migration/multifd.c static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp) · line 1268

Quoted 8 lines
migration/multifd.c +7 -0
     ret = multifd_recv_state->ops->recv(p, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migrate_colo()) {
+        multifd_colo_process_recv(p);
+    }  

Either put all of colo hooks in multifd.c or multifd-nocomp.c. I think the latter is more appropriate as we have mapped_ram already in there. Let's drop patch 3 and put this in multifd_nocomp_recv().

Again, it also should work with compression and multifd_nocomp_recv() is for nocomp only.

Quoted 20 lines
Quoted 20 lines
migration/multifd.c +0 -0
     return ret;
 }
migration/multifd.h +4 -1
index 89a395aef2b09a6762c45b5361e0ab63256feff6..fbc35702b062fdc3213ce92baed35994f5967c2b 100644
@@ -279,7 +279,10 @@ typedef struct {
     uint64_t packets_recved;
     /* ramblock */
     RAMBlock *block;
-    /* ramblock host address */
+    /*
+     * Normally, it points to ramblock's host address.  When COLO
+     * is enabled, it points to the mirror cache for the ramblock.
+     */
     uint8_t *host;
     /* buffers to recv */
     struct iovec *iov;  
Signature · 1 line
Peter Xu
#12 lore raw

Re: [PATCH v3 04/10] multifd: Add COLO support

Quoted 307 lines · Peter Xu
Quoted 19 lines · Fabiano Rosas
Quoted 17 lines · Lukas Straub
Quoted 4 lines
Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+void multifd_colo_prepare_recv(MultiFDRecvParams *p)
+{
+    /*
+     * While we're still in precopy state (not yet in colo state), we copy
Quoted 8 lines

migration/multifd-colo.c line 31

Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+     * received pages to both guest and cache. No need to set dirty bits,
+     * since guest and cache memory are in sync.
+     */
+    if (migration_incoming_in_colo_state()) {  

What's the relationship between migration_incoming_in_colo_state() and migration_incoming_colo_enabled()? ram_load_precopy() checks both. Would migration_incoming_colo_enabled affect multifd as well?

So first off migration_incoming_colo_enabled() and migrate_colo() are equivalent in practice since
121ccedc2b migration: block incoming colo when capability is disabled

Quoted 6 lines
Quoted 2 lines

(I have some cleanup patches lying around, but that will be for later)

Ok, I think those are important because when having multifd and non-multifd code for the same feature, it's useful to be able to compare the two. So some degree of uniformity would be nice.

I second. We can drop those in this series before adding multifd support, likely together with MIG_CMD_ENABLE_COLO as well; I don't think COLO needs to worry about old binaries. It should always use the same QEMU binary on both sides.

The patch needs to rename MIG_CMD_ENABLE_COLO to MIG_CMD_DEPRECATED_0 or something, to make the rest MIG_CMD compatible to old binaries, though.

Quoted 122 lines
Quoted 122 lines

For colo, we do normal precopy migration and at the end we go into colo state and then
migration_incoming_in_colo_state() will be true.

Here we check migrate_colo() outside of these functions as Peter requested that in a previous version.

Quoted 4 lines

The multifd recv threads will be running until after process_incoming_migration_bh(), which is when migration_incoming_disable_colo() runs.

That is not an issue as we use migrate_colo() here.

Quoted 3 lines

Also, is the colo_cache guaranteed to be around until multifd threads exit?

This is an issue. I will fix it in the next version.

Quoted 35 lines
Quoted 4 lines
migration/multifd-colo.c new file +3 -0
+        colo_record_bitmap(p->block, p->normal, p->normal_num);
+        colo_record_bitmap(p->block, p->zero, p->zero_num);
+    }

migration/multifd-colo.c line 44

Quoted 10 lines
migration/multifd-colo.c new file +10 -0
+}
+
+void multifd_colo_process_recv(MultiFDRecvParams *p)
+{
+    if (!migration_incoming_in_colo_state()) {
+        for (int i = 0; i < p->normal_num; i++) {
+            void *guest = p->block->host + p->normal[i];
+            void *cache = p->host + p->normal[i];
+            memcpy(guest, cache, multifd_ram_page_size());
+        }  

I see some differences between what ram.c does and what multifd will do after this patch regarding which flags are checked and order of copies (code below):

ram.c:

- migration_incoming_colo_enabled && migration_incoming_in_colo_state:
Reads from stream into colo_cache.

- migration_incoming_colo_enabled && !migration_incoming_in_colo_state:
Reads from stream into guest and then memcpy into colo_cache.

- !migration_incoming_colo_enabled
Reads from stream into guest.

multifd.c:

- migrate_colo:
Reads from stream into colo_cache.

Quoted 8 lines

- !migration_incoming_in_colo_state:
memcpy from colo_cache into guest.

- !migration_incoming_colo_enabled
???

The resulting state should be the same, but I wonder if we want to i) use the same checks in multifd

migration_incoming_colo_enabled() shouldn't even exist anymore, so I'm not using it here. migrate_colo() is much easier to reason about.

Quoted 2 lines

and ii) when not in colo state, copy first
into guest (using readv) and later memcpy into the colo_cache.

I think it is easier the way it is now.

Quoted 48 lines

---
ram.c:

host = host_from_ram_block_offset(block, addr);

if (migration_incoming_colo_enabled()) {
    if (migration_incoming_in_colo_state()) {
        host = colo_cache_from_block_offset(block, addr, true);
    } else {
        host_bak = colo_cache_from_block_offset(block, addr, false);
    }
}
qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
if (host_bak) {
    memcpy(host_bak, host, TARGET_PAGE_SIZE);
}

multifd:

if (migrate_colo()) {
    p->host = p->block->colo_cache;
}
for (int i = 0; i < p->normal_num; i++) {
    p->iov[i].iov_base = p->host + p->normal[i];
}

return qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);

if (!migration_incoming_in_colo_state()) {
    for (int i = 0; i < p->normal_num; i++) {
        void *guest = p->block->host + p->normal[i];
        void *cache = p->host + p->normal[i];
        memcpy(guest, cache, multifd_ram_page_size());
    }
}

---

migration/multifd-colo.c line 48

Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+        for (int i = 0; i < p->zero_num; i++) {
+            void *guest = p->block->host + p->zero[i];
+            memset(guest, 0, multifd_ram_page_size());
+        }  

At multifd_nocomp_recv, there will be a call to multifd_recv_zero_page_process(), which by that point will have p->host == p->block->colo_cache, so it looks like that function will do some zero page processing in the colo_cache, setting the rb->receivedmap for pages in the colo_cache and potentially also doing a memcpy. Is this intended?

Quoted 7 lines
Quoted 3 lines

rb->receivedmap is only for postcopy, right? So it doesn't apply with colo.

It's not anymore since commit 5ef7e26bdb ("migration/multifd: solve zero page causing multiple page faults"). So it seems we might be doing extra work on top of the colo_cache.

IIUC not extra, but exactly what will be needed.

The logic was about "in a vanilla precopy, if we see one page arriving the 1st time we don't need to zero the buffer because the buffer should be zero allocated".

In COLO's case, COLO always puts RAM data into colo_cache, hence it should apply to colo_cache too, avoiding unnecessary memset() for colo_cache instead.

E.g. colo_cache is allocated from qemu_anon_ram_alloc(), it's also guaranteed to be zeros when never touched.

Quoted 129 lines
Quoted 8 lines
Quoted 4 lines

I'm thinking that maybe it would overall be better to hook colo directly in to multifd_nocomp_recv:

But then it will only work for nocomp, right? It feels like the wrong level of abstraction to me.

Ah, nocomp != ram indeed.

Quoted 117 lines
Quoted 103 lines

static int multifd_nocomp_recv(MultiFDRecvParams *p, Error **errp)

{
    uint32_t flags;

    if (migrate_mapped_ram()) {
        return multifd_file_recv_data(p, errp);
    }

    flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;

    if (flags != MULTIFD_FLAG_NOCOMP) {
        error_setg(errp, "multifd %u: flags received %x flags expected %x",
                   p->id, flags, MULTIFD_FLAG_NOCOMP);
        return -1;
    }
(quoted diff) +11 -0
+    if (migration_incoming_colo_enabled() && migration_incoming_in_colo_state()) {
+        p->host = p->block->colo_cache;
+    } // or else{}, depending on how deal with zero pages in the cache
 
    multifd_recv_zero_page_process(p);
 
    if (!p->normal_num) {
        return 0;
    }
 
    for (int i = 0; i < p->normal_num; i++) {
        p->iov[i].iov_base = p->host + p->normal[i];
        p->iov[i].iov_len = multifd_ram_page_size();
        ramblock_recv_bitmap_set_offset(p->block, p->normal[i]);
    }
+    ret = qio_channel_readv_all(p->c, p->iov, p->normal_num, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migration_incoming_colo_enabled()) {
+        multifd_colo_process_recv();
+    }
 
    return ret;

}

Quoted 58 lines
(quoted diff) +2 -0
+    }
+}
migration/multifd-colo.h new file +26 -0
new file mode 100644
index 0000000000000000000000000000000000000000..82eaf3f48c47de2f090f9de52f9d57a337d4754a
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * multifd colo header
+ *
+ * Copyright (c) Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_MIGRATION_MULTIFD_COLO_H
+#define QEMU_MIGRATION_MULTIFD_COLO_H
+
+#ifdef CONFIG_REPLICATION
+
+void multifd_colo_prepare_recv(MultiFDRecvParams *p);
+void multifd_colo_process_recv(MultiFDRecvParams *p);
+
+#else
+
+static inline void multifd_colo_prepare_recv(MultiFDRecvParams *p) {}
+static inline void multifd_colo_process_recv(MultiFDRecvParams *p) {}
+
+#endif
+#endif
migration/multifd-nocomp.c +1 -1
index 9be79b3b8e00371ebff9e112766c225bec260bf7..9f7a792fa761b3bc30b971b35f464103a61787f0 100644
@@ -16,6 +16,7 @@
 #include "file.h"
 #include "migration-stats.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "migration.h"
 #include "qapi/error.h"
@@ -269,7 +270,6 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         return -1;
     }
 
-    p->host = p->block->host;
     for (i = 0; i < p->normal_num; i++) {
         uint64_t offset = be64_to_cpu(packet->offset[i]);
 
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
Quoted 7 lines

migration/multifd-nocomp.c int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp) · line 300

Quoted 4 lines
migration/multifd-nocomp.c +4 -0
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;  

Can't you just use p->block->colo_cache later? I don't see why p->host needs to be set beforehand even in the non-colo case.

We should not touch the guest ram directly while in colo state, since the incoming guest is running and we either want to receive and apply a whole checkpoint with all ram into colo cache and all device state, or if anything goes wrong during checkpointing, keep the currently running guest on the incoming side in pristine state.

Quoted 9 lines
Quoted 3 lines

I was asking about setting p->host at this specific point. I don't think any of this fits the unfill function. However, I see those were suggested by Peter so let's not go back and forth.

Actually I don't know why p->host existed before this work; IIUC we could have always used p->block->host. Maybe when Juan was developing this Juan kept COLO in mind; or maybe Juan wanted to avoid frequent p->block pointer reference.

Maybe p->block was being reset at some point and p->host was passed being the point where the (whatever) lock was release. I checked and today there's no such thing. The p->mutex seems to be there just to protect against this in multifd_recv_sync_main:

WITH_QEMU_LOCK_GUARD(&p->mutex) {
    if (multifd_recv_state->packet_num < p->packet_num) {
        multifd_recv_state->packet_num = p->packet_num;
    }
}
Quoted 5 lines

IIUC, we could remove p->host, but when we need to access "the buffer of the ramblock" we'll need to call a helper to fetch that (either ramblock's buffer, or colo_cache, per migrate_colo()). And it might be slightly slower than p->host indeed.

Yeah, let's keep it, the compression code also uses it, there's no point removing it now.

Quoted 66 lines
Quoted 66 lines
Quoted 65 lines

I have written more about colo migration here:

https://lore.kernel.org/qemu-devel/20260117204913.584e1829@penguin/
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 23 lines
Quoted 23 lines
migration/multifd-nocomp.c +4 -0
+    } else {
+        p->host = p->block->host;
+    }
+
     return 0;
 }
 
migration/multifd.c +1 -0
index 332e6fc58053462419f3171f6c320ac37648ef7b..220ed8564960fdabc58e4baa069dd252c8ad293c 100644
@@ -29,6 +29,7 @@
 #include "qemu-file.h"
 #include "trace.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "qemu/yank.h"
 #include "io/channel-file.h"
@@ -1258,6 +1259,13 @@ static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp)
     int ret;
 
Quoted 12 lines

migration/multifd.c static int multifd_ram_state_recv(MultiFDRecvParams *p, Error **errp) · line 1268

Quoted 8 lines
migration/multifd.c +7 -0
     ret = multifd_recv_state->ops->recv(p, errp);
+    if (ret != 0) {
+        return ret;
+    }
+
+    if (migrate_colo()) {
+        multifd_colo_process_recv(p);
+    }  

Either put all of colo hooks in multifd.c or multifd-nocomp.c. I think the latter is more appropriate as we have mapped_ram already in there. Let's drop patch 3 and put this in multifd_nocomp_recv().

Again, it also should work with compression and multifd_nocomp_recv() is for nocomp only.

Quoted 20 lines
Quoted 20 lines
migration/multifd.c +0 -0
     return ret;
 }
migration/multifd.h +4 -1
index 89a395aef2b09a6762c45b5361e0ab63256feff6..fbc35702b062fdc3213ce92baed35994f5967c2b 100644
@@ -279,7 +279,10 @@ typedef struct {
     uint64_t packets_recved;
     /* ramblock */
     RAMBlock *block;
-    /* ramblock host address */
+    /*
+     * Normally, it points to ramblock's host address.  When COLO
+     * is enabled, it points to the mirror cache for the ramblock.
+     */
     uint8_t *host;
     /* buffers to recv */
     struct iovec *iov;  
#13 lore raw

Re: [PATCH v3 04/10] multifd: Add COLO support

Quoted 34 lines · Fabiano Rosas
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 11 lines

migration/multifd-nocomp.c int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp) · line 300

Quoted 8 lines
migration/multifd-nocomp.c +4 -0
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;  

Can't you just use p->block->colo_cache later? I don't see why p->host needs to be set beforehand even in the non-colo case.

We should not touch the guest ram directly while in colo state, since the incoming guest is running and we either want to receive and apply a whole checkpoint with all ram into colo cache and all device state, or if anything goes wrong during checkpointing, keep the currently running guest on the incoming side in pristine state.

Quoted 9 lines
Quoted 3 lines

I was asking about setting p->host at this specific point. I don't think any of this fits the unfill function. However, I see those were suggested by Peter so let's not go back and forth.

Actually I don't know why p->host existed before this work; IIUC we could have always used p->block->host. Maybe when Juan was developing this Juan kept COLO in mind; or maybe Juan wanted to avoid frequent p->block pointer reference.

Maybe p->block was being reset at some point and p->host was passed being the point where the (whatever) lock was release. I checked and today there's no such thing. The p->mutex seems to be there just to protect against this in multifd_recv_sync_main:

Quoted 5 lines
WITH_QEMU_LOCK_GUARD(&p->mutex) {
    if (multifd_recv_state->packet_num < p->packet_num) {
        multifd_recv_state->packet_num = p->packet_num;
    }
}

It should be protected by various checks over migration_is_running().

E.g., QMP device-add & device-del are forbidden so no new pc-dimm hotplug / removal allowed. Similarly, virtio_mem_is_busy() would return true during migration too.

We should definitely make sure ramblock will not be reset during the whole lifecycle of migration; I believe we're not ready for that..

Signature · 1 line
Peter Xu
#14 lore raw

Re: [PATCH v3 04/10] multifd: Add COLO support

Quoted 41 lines · Peter Xu
Quoted 34 lines · Fabiano Rosas
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 11 lines

Review int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp) · line 300

Quoted 8 lines
(quoted diff) +4 -0
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;  

Can't you just use p->block->colo_cache later? I don't see why p->host needs to be set beforehand even in the non-colo case.

We should not touch the guest ram directly while in colo state, since the incoming guest is running and we either want to receive and apply a whole checkpoint with all ram into colo cache and all device state, or if anything goes wrong during checkpointing, keep the currently running guest on the incoming side in pristine state.

Quoted 9 lines
Quoted 3 lines

I was asking about setting p->host at this specific point. I don't think any of this fits the unfill function. However, I see those were suggested by Peter so let's not go back and forth.

Actually I don't know why p->host existed before this work; IIUC we could have always used p->block->host. Maybe when Juan was developing this Juan kept COLO in mind; or maybe Juan wanted to avoid frequent p->block pointer reference.

Maybe p->block was being reset at some point and p->host was passed being the point where the (whatever) lock was release. I checked and today there's no such thing. The p->mutex seems to be there just to protect against this in multifd_recv_sync_main:

Quoted 5 lines
WITH_QEMU_LOCK_GUARD(&p->mutex) {
    if (multifd_recv_state->packet_num < p->packet_num) {
        multifd_recv_state->packet_num = p->packet_num;
    }
}
Quoted 8 lines

It should be protected by various checks over migration_is_running().

E.g., QMP device-add & device-del are forbidden so no new pc-dimm hotplug / removal allowed. Similarly, virtio_mem_is_busy() would return true during migration too.

We should definitely make sure ramblock will not be reset during the whole lifecycle of migration; I believe we're not ready for that..

The pointer reset, not the block. Anyway, it doesn't happen.

#15 lore raw

Re: [PATCH v3 04/10] multifd: Add COLO support

In reply to Fabiano Rosas
Quoted 113 lines · On Wed, 28 Jan 2026 09:30:24 -0300 Fabiano Rosas <[email protected]> wrote:
Quoted 90 lines · Peter Xu
Quoted 16 lines · Fabiano Rosas
Quoted 14 lines · Lukas Straub
Quoted 13 lines

[...]

migration/multifd-colo.c line 48

Quoted 4 lines
migration/multifd-colo.c new file +4 -0
+        for (int i = 0; i < p->zero_num; i++) {
+            void *guest = p->block->host + p->zero[i];
+            memset(guest, 0, multifd_ram_page_size());
+        }    

At multifd_nocomp_recv, there will be a call to multifd_recv_zero_page_process(), which by that point will have p->host == p->block->colo_cache, so it looks like that function will do some zero page processing in the colo_cache, setting the rb->receivedmap for pages in the colo_cache and potentially also doing a memcpy. Is this intended?

Quoted 7 lines
Quoted 3 lines

rb->receivedmap is only for postcopy, right? So it doesn't apply with colo.

It's not anymore since commit 5ef7e26bdb ("migration/multifd: solve zero page causing multiple page faults"). So it seems we might be doing extra work on top of the colo_cache.

IIUC not extra, but exactly what will be needed.

The logic was about "in a vanilla precopy, if we see one page arriving the 1st time we don't need to zero the buffer because the buffer should be zero allocated".

In COLO's case, COLO always puts RAM data into colo_cache, hence it should apply to colo_cache too, avoiding unnecessary memset() for colo_cache instead.

E.g. colo_cache is allocated from qemu_anon_ram_alloc(), it's also guaranteed to be zeros when never touched.

Quoted 52 lines
Quoted 8 lines
Quoted 4 lines

I'm thinking that maybe it would overall be better to hook colo directly in to multifd_nocomp_recv:

But then it will only work for nocomp, right? It feels like the wrong level of abstraction to me.

Ah, nocomp != ram indeed.

Quoted 40 lines
Quoted 26 lines
Quoted 26 lines

[...]

migration/multifd-nocomp.c +1 -1
index 9be79b3b8e00371ebff9e112766c225bec260bf7..9f7a792fa761b3bc30b971b35f464103a61787f0 100644
@@ -16,6 +16,7 @@
 #include "file.h"
 #include "migration-stats.h"
 #include "multifd.h"
+#include "multifd-colo.h"
 #include "options.h"
 #include "migration.h"
 #include "qapi/error.h"
@@ -269,7 +270,6 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         return -1;
     }
 
-    p->host = p->block->host;
     for (i = 0; i < p->normal_num; i++) {
         uint64_t offset = be64_to_cpu(packet->offset[i]);
 
@@ -294,6 +294,14 @@ int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp)
         p->zero[i] = offset;
     }
 
Quoted 7 lines

migration/multifd-nocomp.c int multifd_ram_unfill_packet(MultiFDRecvParams *p, Error **errp) · line 300

Quoted 4 lines
migration/multifd-nocomp.c +4 -0
+    if (migrate_colo()) {
+        multifd_colo_prepare_recv(p);
+        assert(p->block->colo_cache);
+        p->host = p->block->colo_cache;    

Can't you just use p->block->colo_cache later? I don't see why p->host needs to be set beforehand even in the non-colo case.

We should not touch the guest ram directly while in colo state, since the incoming guest is running and we either want to receive and apply a whole checkpoint with all ram into colo cache and all device state, or if anything goes wrong during checkpointing, keep the currently running guest on the incoming side in pristine state.

Quoted 9 lines
Quoted 3 lines

I was asking about setting p->host at this specific point. I don't think any of this fits the unfill function. However, I see those were suggested by Peter so let's not go back and forth.

Actually I don't know why p->host existed before this work; IIUC we could have always used p->block->host. Maybe when Juan was developing this Juan kept COLO in mind; or maybe Juan wanted to avoid frequent p->block pointer reference.

Maybe p->block was being reset at some point and p->host was passed being the point where the (whatever) lock was release. I checked and today there's no such thing. The p->mutex seems to be there just to protect against this in multifd_recv_sync_main:

WITH_QEMU_LOCK_GUARD(&p->mutex) {
    if (multifd_recv_state->packet_num < p->packet_num) {
        multifd_recv_state->packet_num = p->packet_num;
    }
}
Quoted 9 lines
Quoted 5 lines

IIUC, we could remove p->host, but when we need to access "the buffer of the ramblock" we'll need to call a helper to fetch that (either ramblock's buffer, or colo_cache, per migrate_colo()). And it might be slightly slower than p->host indeed.

Yeah, let's keep it, the compression code also uses it, there's no point removing it now.

Actually p->host was there first p->block was added later for COLO in 5d1d1fcf4 multifd: Add the ramblock to MultiFDRecvParams

#16 lore raw

PATCH v3 5/10 colo: Fix crash during device vmstate load

In reply to Lukas Straub

With colo we load device vmstate during each checkpoint, on top of a vm that was already running. Some devices expect a reset before loading vmstate on such a previously running vm.

This fixes a crash when using COLO with Q35 machine.

---
 migration/colo.c | 1 +
 1 file changed, 1 insertion(+)
migration/colo.c +1 -0
index db783f6fa77500386d923dd97e522883027e71d8..627b3706687036554eda3909b4194116a7640493 100644
@@ -727,6 +727,7 @@ static void colo_incoming_process_checkpoint(MigrationIncomingState *mis,
 
     bql_lock();
     vmstate_loading = true;
+    qemu_system_reset(SHUTDOWN_CAUSE_SNAPSHOT_LOAD);
     colo_flush_ram_cache();
     ret = qemu_load_device_state(fb, errp);
     if (ret < 0) {
Signature · 1 line
2.39.5
Reviewed-by on v3 5/10
#17 lore raw

Re: [PATCH v3 05/10] colo: Fix crash during device vmstate load

Quoted 7 lines · Lukas Straub

With colo we load device vmstate during each checkpoint, on top of a vm that was already running. Some devices expect a reset before loading vmstate on such a previously running vm.

This fixes a crash when using COLO with Q35 machine.

Yes makes sense, maybe you can add some comments into the code too since this was overlooked before,

Have you tried to measure how many overheads will this introduce to loading each snapshot?

Quoted 20 lines
---
 migration/colo.c | 1 +
 1 file changed, 1 insertion(+)
migration/colo.c +1 -0
index db783f6fa77500386d923dd97e522883027e71d8..627b3706687036554eda3909b4194116a7640493 100644
@@ -727,6 +727,7 @@ static void colo_incoming_process_checkpoint(MigrationIncomingState *mis,
 
     bql_lock();
     vmstate_loading = true;
+    qemu_system_reset(SHUTDOWN_CAUSE_SNAPSHOT_LOAD);
     colo_flush_ram_cache();
     ret = qemu_load_device_state(fb, errp);
     if (ret < 0) {
Signature · 2 lines
-- 
2.39.5
Signature · 1 line
Peter Xu
#18 lore raw

Re: [PATCH v3 05/10] colo: Fix crash during device vmstate load

Quoted 9 lines · On Tue, 27 Jan 2026 15:38:55 -0500 Peter Xu <[email protected]> wrote:
Quoted 7 lines · Lukas Straub

With colo we load device vmstate during each checkpoint, on top of a vm that was already running. Some devices expect a reset before loading vmstate on such a previously running vm.

This fixes a crash when using COLO with Q35 machine.

Quoted 7 lines

Yes makes sense, maybe you can add some comments into the code too since this was overlooked before,

Have you tried to measure how many overheads will this introduce to loading each snapshot?

It's a large overhead actually, between 10-20 milliseconds.

Regards,
Lukas Straub

Quoted 22 lines
Quoted 21 lines
---
 migration/colo.c | 1 +
 1 file changed, 1 insertion(+)
migration/colo.c +1 -0
index db783f6fa77500386d923dd97e522883027e71d8..627b3706687036554eda3909b4194116a7640493 100644
@@ -727,6 +727,7 @@ static void colo_incoming_process_checkpoint(MigrationIncomingState *mis,
 
     bql_lock();
     vmstate_loading = true;
+    qemu_system_reset(SHUTDOWN_CAUSE_SNAPSHOT_LOAD);
     colo_flush_ram_cache();
     ret = qemu_load_device_state(fb, errp);
     if (ret < 0) {
Signature · 2 lines
-- 
2.39.5
#19 lore raw

Re: [PATCH v3 05/10] colo: Fix crash during device vmstate load

Quoted 15 lines · Lukas Straub
Quoted 12 lines · On Tue, 27 Jan 2026 15:38:55 -0500 Peter Xu <[email protected]> wrote:
Quoted 7 lines · Lukas Straub

With colo we load device vmstate during each checkpoint, on top of a vm that was already running. Some devices expect a reset before loading vmstate on such a previously running vm.

This fixes a crash when using COLO with Q35 machine.

Yes makes sense, maybe you can add some comments into the code too since this was overlooked before,

Quoted 6 lines
Quoted 4 lines

Have you tried to measure how many overheads will this introduce to loading each snapshot?

It's a large overhead actually, between 10-20 milliseconds.

This can be mentioned in the commit message.

IIUC reset() may or may not be required while loading a snapshot. Normally, a device reset() should reset all dev registers and internal states, OTOH loadvm() will reload most of them once more.. so less efficient.

Maybe there's chance to "fix" q35 instead reducing this overhead, but I'll leave that to be your call; to me this fix is clean from maint POV.

Thanks,

Signature · 1 line
Peter Xu
#20 lore raw

Re: [PATCH v3 05/10] colo: Fix crash during device vmstate load

Quoted 30 lines · On Mon, 2 Feb 2026 09:12:33 -0500 Peter Xu <[email protected]> wrote:
Quoted 15 lines · Lukas Straub
Quoted 12 lines · On Tue, 27 Jan 2026 15:38:55 -0500 Peter Xu <[email protected]> wrote:
Quoted 7 lines · Lukas Straub

With colo we load device vmstate during each checkpoint, on top of a vm that was already running. Some devices expect a reset before loading vmstate on such a previously running vm.

This fixes a crash when using COLO with Q35 machine.

Yes makes sense, maybe you can add some comments into the code too since this was overlooked before,

Quoted 6 lines
Quoted 4 lines

Have you tried to measure how many overheads will this introduce to loading each snapshot?

It's a large overhead actually, between 10-20 milliseconds.

This can be mentioned in the commit message.

IIUC reset() may or may not be required while loading a snapshot. Normally, a device reset() should reset all dev registers and internal states, OTOH loadvm() will reload most of them once more.. so less efficient.

Quoted 5 lines

Maybe there's chance to "fix" q35 instead reducing this overhead, but I'll leave that to be your call; to me this fix is clean from maint POV.

Thanks,

Yes, I think this fix is fine for now. It more correct like this and we can improve performance later while keeping it correct.

Regards,
Lukas Straub

#21 lore raw

PATCH v3 6/10 migration-test: Add COLO migration unit test

In reply to Lukas Straub

Add a COLO migration test for COLO migration and failover.

---
 MAINTAINERS                        |   1 +
 tests/qtest/meson.build            |   7 +-
 tests/qtest/migration-test.c       |   1 +
 tests/qtest/migration/colo-tests.c | 199 +++++++++++++++++++++++++++++++++++++
 tests/qtest/migration/framework.h  |   5 +
 5 files changed, 212 insertions(+), 1 deletion(-)
MAINTAINERS +1 -0
index 883f0a8f4eb92d0bf0f89fcab4674ccc4aed1cc1..2a8b9b2d051883c1b7adce9c1afec80d16a317f8 100644
@@ -3856,6 +3856,7 @@ F: migration/colo*
 F: migration/multifd-colo.*
 F: include/migration/colo.h
 F: include/migration/failover.h
+F: tests/qtest/migration/colo-tests.c
 F: docs/COLO-FT.txt
 
 COLO Proxy
tests/qtest/meson.build +6 -1
index dfb83650c643d884daad53a66034ab7aa8c45509..624f7744ec9bd81c8823075b966bc95f7750a667 100644
@@ -371,6 +371,11 @@ if gnutls.found()
   endif
 endif
 
+migration_colo_files = []
+if get_option('replication').allowed()
+  migration_colo_files = [files('migration/colo-tests.c')]
+endif
+
 qtests = {
   'aspeed_hace-test': files('aspeed-hace-utils.c', 'aspeed_hace-test.c'),
   'aspeed_smc-test': files('aspeed-smc-utils.c', 'aspeed_smc-test.c'),
@@ -382,7 +387,7 @@ qtests = {
                              'migration/migration-util.c') + dbus_vmstate1,
   'erst-test': files('erst-test.c'),
   'ivshmem-test': [rt, '../../contrib/ivshmem-server/ivshmem-server.c'],
-  'migration-test': test_migration_files + migration_tls_files,
+  'migration-test': test_migration_files + migration_tls_files + migration_colo_files,
   'pxe-test': files('boot-sector.c'),
   'pnv-xive2-test': files('pnv-xive2-common.c', 'pnv-xive2-flush-sync.c',
                           'pnv-xive2-nvpg_bar.c'),
tests/qtest/migration-test.c +1 -0
index 08936871741535c926eeac40a7d7c3f461c72fd0..e582f05c7dc2673dbd05a936df8feb6c964b5bbc 100644
@@ -55,6 +55,7 @@ int main(int argc, char **argv)
     migration_test_add_precopy(env);
     migration_test_add_cpr(env);
     migration_test_add_misc(env);
+    migration_test_add_colo(env);
 
     ret = g_test_run();
 
tests/qtest/migration/colo-tests.c new file +199 -0

199 lines not on this page · raw message

tests/qtest/migration/framework.h +5 -0
index 40984d04930da2d181326d9f6a742bde49018103..80eef758932ce9c301ed6c0f6383d18756144870 100644
@@ -264,5 +264,10 @@ void migration_test_add_file(MigrationTestEnv *env);
 void migration_test_add_precopy(MigrationTestEnv *env);
 void migration_test_add_cpr(MigrationTestEnv *env);
 void migration_test_add_misc(MigrationTestEnv *env);
+#ifdef CONFIG_REPLICATION
+void migration_test_add_colo(MigrationTestEnv *env);
+#else
+static inline void migration_test_add_colo(MigrationTestEnv *env) {};
+#endif
 
 #endif /* TEST_FRAMEWORK_H */
Signature · 1 line
2.39.5
#23 lore raw

Re: [PATCH v3 06/10] migration-test: Add COLO migration unit test

In reply to Lukas Straub

tests/qtest/migration/colo-tests.c line 174

Quoted 6 lines · Lukas Straub
tests/qtest/migration/colo-tests.c new file +6 -0
+void migration_test_add_colo(MigrationTestEnv *env)
+{
+    if (!env->has_kvm) {
+        g_test_skip("COLO requires KVM accelerator");
+        return;
+    }

I'm OK if you want to explicitly bypass others, but could you explanation why?

Thanks,

Signature · 1 line
Peter Xu
#24 lore raw

Re: [PATCH v3 06/10] migration-test: Add COLO migration unit test

Quoted 11 lines · On Tue, 27 Jan 2026 15:49:31 -0500 Peter Xu <[email protected]> wrote:

tests/qtest/migration/colo-tests.c line 174

Quoted 6 lines · Lukas Straub
tests/qtest/migration/colo-tests.c new file +6 -0
+void migration_test_add_colo(MigrationTestEnv *env)
+{
+    if (!env->has_kvm) {
+        g_test_skip("COLO requires KVM accelerator");
+        return;
+    }  

I'm OK if you want to explicitly bypass others, but could you explanation why?

Quoted 2 lines

Thanks,

It used to hang with TCG. Now it crashes, since migration_bitmap_sync_precopy assumes bql is held. Something for later.

#6 0x00007ffff7471517 in __assert_fail

    (assertion=assertion@entry=0x555555f17aee "bql_locked() != locked", file=file@entry=0x555555f17ab0 "../system/cpus.c", line=line@entry=535, function=function@entry=0x55555609bfd0 <__PRETTY_FUNCTION__.9> "bql_update_status") at ./assert/assert.c:105
#7  0x0000555555b09f1e in bql_update_status (locked=locked@entry=false) at ../system/cpus.c:535
#8  0x0000555555ec60e7 in qemu_mutex_pre_unlock (mutex=0x555557166700 <bql>, file=0x555555efe1dc "../cpu-common.c", line=164) at ../util/qemu-thread-common.h:57
#9  qemu_mutex_pre_unlock (line=164, file=0x555555efe1dc "../cpu-common.c", mutex=0x555557166700 <bql>) at ../util/qemu-thread-common.h:48

#10 qemu_cond_wait_impl (cond=0x5555571442c0 <qemu_work_cond>, mutex=0x555557166700 <bql>, file=0x555555efe1dc "../cpu-common.c", line=164) at ../util/qemu-thread-posix.c:224 #11 0x000055555589e6c8 in do_run_on_cpu (cpu=<optimized out>, func=<optimized out>, data=..., mutex=0x555557166700 <bql>) at ../cpu-common.c:164 #12 0x0000555555b17a06 in memory_global_after_dirty_log_sync () at ../system/memory.c:2938
#13 0x0000555555b55b47 in migration_bitmap_sync (rs=0x7fffe8001340, last_stage=last_stage@entry=true) at ../migration/ram.c:1157
#14 0x0000555555b56721 in migration_bitmap_sync_precopy (last_stage=last_stage@entry=true) at ../migration/ram.c:1195
#15 0x0000555555b59f8a in ram_save_complete (f=0x5555575db620, opaque=<optimized out>) at ../migration/ram.c:3381
#16 0x0000555555b5e4f5 in qemu_savevm_complete (se=se@entry=0x5555574c0d80, f=f@entry=0x5555575db620) at ../migration/savevm.c:1521
#17 0x0000555555b60437 in qemu_savevm_state_complete_precopy_iterable (f=f@entry=0x5555575db620, in_postcopy=in_postcopy@entry=false) at ../migration/savevm.c:1627 #18 0x0000555555b60a4f in qemu_savevm_state_complete_precopy (iterable_only=true, f=0x5555575db620) at ../migration/savevm.c:1719
#19 qemu_savevm_live_state (f=0x5555575db620) at ../migration/savevm.c:1855
#20 0x0000555555b65ed9 in colo_do_checkpoint_transaction (fb=<optimized out>, bioc=<optimized out>, s=0x5555574c0070) at ../migration/colo.c:474 #21 colo_process_checkpoint (s=0x5555574c0070) at ../migration/colo.c:592
#22 migrate_start_colo_process (s=0x5555574c0070) at ../migration/colo.c:655
#23 0x0000555555b4971e in migration_iteration_finish (s=0x5555574c0070) at ../migration/migration.c:3297
#24 migration_thread (opaque=opaque@entry=0x5555574c0070) at ../migration/migration.c:3584
#25 0x0000555555ec58c0 in qemu_thread_start (args=0x5555576583e0) at ../util/qemu-thread-posix.c:393
#26 0x00007ffff74d2aa4 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#27 0x00007ffff755fc6c in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78

#25 lore raw

Re: [PATCH v3 06/10] migration-test: Add COLO migration unit test

Quoted 14 lines · Lukas Straub
Quoted 11 lines · On Tue, 27 Jan 2026 15:49:31 -0500 Peter Xu <[email protected]> wrote:

Review

Quoted 6 lines · Lukas Straub
(quoted diff) +6 -0
+void migration_test_add_colo(MigrationTestEnv *env)
+{
+    if (!env->has_kvm) {
+        g_test_skip("COLO requires KVM accelerator");
+        return;
+    }  

I'm OK if you want to explicitly bypass others, but could you explanation why?

Quoted 5 lines
Quoted 2 lines

Thanks,

It used to hang with TCG. Now it crashes, since migration_bitmap_sync_precopy assumes bql is held. Something for later.

If we want to keep COLO around and be serious, let's try to make COLO the same standard we target for migration in general whenever possible. We shouldn't randomly workaround bugs. We should fix it.

It looks to me there's some locking issue instead.

Iterator's complete() requires BQL. Would a patch like below makes sense to you?

migration/colo.c +4 -1
index db783f6fa7..b3ea137120 100644
@@ -458,8 +458,8 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
     /* Note: device state is saved into buffer */
     ret = qemu_save_device_state(fb);
 
-    bql_unlock();
     if (ret < 0) {
+        bql_unlock();
         goto out;
     }
 
@@ -473,6 +473,9 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
      */
     qemu_savevm_live_state(s->to_dst_file);
 
+    /* Save live state requires BQL */
+    bql_unlock();
+
     qemu_fflush(fb);
 
     /*
Quoted 24 lines

#6 0x00007ffff7471517 in __assert_fail

    (assertion=assertion@entry=0x555555f17aee "bql_locked() != locked", file=file@entry=0x555555f17ab0 "../system/cpus.c", line=line@entry=535, function=function@entry=0x55555609bfd0 <__PRETTY_FUNCTION__.9> "bql_update_status") at ./assert/assert.c:105
#7  0x0000555555b09f1e in bql_update_status (locked=locked@entry=false) at ../system/cpus.c:535
#8  0x0000555555ec60e7 in qemu_mutex_pre_unlock (mutex=0x555557166700 <bql>, file=0x555555efe1dc "../cpu-common.c", line=164) at ../util/qemu-thread-common.h:57
#9  qemu_mutex_pre_unlock (line=164, file=0x555555efe1dc "../cpu-common.c", mutex=0x555557166700 <bql>) at ../util/qemu-thread-common.h:48

#10 qemu_cond_wait_impl (cond=0x5555571442c0 <qemu_work_cond>, mutex=0x555557166700 <bql>, file=0x555555efe1dc "../cpu-common.c", line=164) at ../util/qemu-thread-posix.c:224 #11 0x000055555589e6c8 in do_run_on_cpu (cpu=<optimized out>, func=<optimized out>, data=..., mutex=0x555557166700 <bql>) at ../cpu-common.c:164 #12 0x0000555555b17a06 in memory_global_after_dirty_log_sync () at ../system/memory.c:2938
#13 0x0000555555b55b47 in migration_bitmap_sync (rs=0x7fffe8001340, last_stage=last_stage@entry=true) at ../migration/ram.c:1157
#14 0x0000555555b56721 in migration_bitmap_sync_precopy (last_stage=last_stage@entry=true) at ../migration/ram.c:1195
#15 0x0000555555b59f8a in ram_save_complete (f=0x5555575db620, opaque=<optimized out>) at ../migration/ram.c:3381
#16 0x0000555555b5e4f5 in qemu_savevm_complete (se=se@entry=0x5555574c0d80, f=f@entry=0x5555575db620) at ../migration/savevm.c:1521
#17 0x0000555555b60437 in qemu_savevm_state_complete_precopy_iterable (f=f@entry=0x5555575db620, in_postcopy=in_postcopy@entry=false) at ../migration/savevm.c:1627 #18 0x0000555555b60a4f in qemu_savevm_state_complete_precopy (iterable_only=true, f=0x5555575db620) at ../migration/savevm.c:1719
#19 qemu_savevm_live_state (f=0x5555575db620) at ../migration/savevm.c:1855
#20 0x0000555555b65ed9 in colo_do_checkpoint_transaction (fb=<optimized out>, bioc=<optimized out>, s=0x5555574c0070) at ../migration/colo.c:474 #21 colo_process_checkpoint (s=0x5555574c0070) at ../migration/colo.c:592
#22 migrate_start_colo_process (s=0x5555574c0070) at ../migration/colo.c:655
#23 0x0000555555b4971e in migration_iteration_finish (s=0x5555574c0070) at ../migration/migration.c:3297
#24 migration_thread (opaque=opaque@entry=0x5555574c0070) at ../migration/migration.c:3584
#25 0x0000555555ec58c0 in qemu_thread_start (args=0x5555576583e0) at ../util/qemu-thread-posix.c:393
#26 0x00007ffff74d2aa4 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#27 0x00007ffff755fc6c in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78

Signature · 1 line
Peter Xu
#26 lore raw

Re: [PATCH v3 06/10] migration-test: Add COLO migration unit test

Quoted 45 lines · On Mon, 2 Feb 2026 09:26:06 -0500 Peter Xu <[email protected]> wrote:
Quoted 14 lines · Lukas Straub
Quoted 11 lines · On Tue, 27 Jan 2026 15:49:31 -0500 Peter Xu <[email protected]> wrote:

Review

Quoted 6 lines · Lukas Straub
(quoted diff) +6 -0
+void migration_test_add_colo(MigrationTestEnv *env)
+{
+    if (!env->has_kvm) {
+        g_test_skip("COLO requires KVM accelerator");
+        return;
+    }    

I'm OK if you want to explicitly bypass others, but could you explanation why?

Quoted 5 lines
Quoted 2 lines

Thanks,

It used to hang with TCG. Now it crashes, since migration_bitmap_sync_precopy assumes bql is held. Something for later.

If we want to keep COLO around and be serious, let's try to make COLO the same standard we target for migration in general whenever possible. We shouldn't randomly workaround bugs. We should fix it.

It looks to me there's some locking issue instead.

Iterator's complete() requires BQL. Would a patch like below makes sense to you?

migration/colo.c +1 -1
index db783f6fa7..b3ea137120 100644
@@ -458,8 +458,8 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
     /* Note: device state is saved into buffer */
     ret = qemu_save_device_state(fb);
 
-    bql_unlock();
     if (ret < 0) {
+        bql_unlock();
         goto out;
     }
 

migration/colo.c static int colo_do_checkpoint_transaction(MigrationState *s, · line 481

Quoted 9 lines
migration/colo.c +3 -0
@@ -473,6 +473,9 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
      */
     qemu_savevm_live_state(s->to_dst_file);
 
+    /* Save live state requires BQL */
+    bql_unlock();
+
     qemu_fflush(fb);
 
     /*

I already tested that and it works. However, we have to be very careful around the locking here and I don't think it is safe to take the bql on the primary here:

The secondary has the bql held at this point:

    colo_receive_check_message(mis->from_src_file,
                       COLO_MESSAGE_VMSTATE_SEND, &local_err);
    ...
    bql_lock();
    cpu_synchronize_all_states();
    ret = qemu_loadvm_state_main(mis->from_src_file, mis, errp);
    bql_unlock();

On the primary there is a filter-mirror mirroring incoming packets to the secondary filter-redirector. However since the secondary migration holds bql the receiving filter is blocked and will not receive anything from the socket. Thus filter-mirror on the primary also may get blocked during send and block the mainloop (It uses blocking IO).

Now if the primary migration thread wants to take the bql it will deadlock.

So I think this is something to fix in a separate series since it is more involved.

Regards,
Lukas Straub

Quoted 28 lines
Quoted 25 lines

#6 0x00007ffff7471517 in __assert_fail

    (assertion=assertion@entry=0x555555f17aee "bql_locked() != locked", file=file@entry=0x555555f17ab0 "../system/cpus.c", line=line@entry=535, function=function@entry=0x55555609bfd0 <__PRETTY_FUNCTION__.9> "bql_update_status") at ./assert/assert.c:105
#7  0x0000555555b09f1e in bql_update_status (locked=locked@entry=false) at ../system/cpus.c:535
#8  0x0000555555ec60e7 in qemu_mutex_pre_unlock (mutex=0x555557166700 <bql>, file=0x555555efe1dc "../cpu-common.c", line=164) at ../util/qemu-thread-common.h:57
#9  qemu_mutex_pre_unlock (line=164, file=0x555555efe1dc "../cpu-common.c", mutex=0x555557166700 <bql>) at ../util/qemu-thread-common.h:48

#10 qemu_cond_wait_impl (cond=0x5555571442c0 <qemu_work_cond>, mutex=0x555557166700 <bql>, file=0x555555efe1dc "../cpu-common.c", line=164) at ../util/qemu-thread-posix.c:224 #11 0x000055555589e6c8 in do_run_on_cpu (cpu=<optimized out>, func=<optimized out>, data=..., mutex=0x555557166700 <bql>) at ../cpu-common.c:164 #12 0x0000555555b17a06 in memory_global_after_dirty_log_sync () at ../system/memory.c:2938
#13 0x0000555555b55b47 in migration_bitmap_sync (rs=0x7fffe8001340, last_stage=last_stage@entry=true) at ../migration/ram.c:1157
#14 0x0000555555b56721 in migration_bitmap_sync_precopy (last_stage=last_stage@entry=true) at ../migration/ram.c:1195
#15 0x0000555555b59f8a in ram_save_complete (f=0x5555575db620, opaque=<optimized out>) at ../migration/ram.c:3381
#16 0x0000555555b5e4f5 in qemu_savevm_complete (se=se@entry=0x5555574c0d80, f=f@entry=0x5555575db620) at ../migration/savevm.c:1521
#17 0x0000555555b60437 in qemu_savevm_state_complete_precopy_iterable (f=f@entry=0x5555575db620, in_postcopy=in_postcopy@entry=false) at ../migration/savevm.c:1627 #18 0x0000555555b60a4f in qemu_savevm_state_complete_precopy (iterable_only=true, f=0x5555575db620) at ../migration/savevm.c:1719
#19 qemu_savevm_live_state (f=0x5555575db620) at ../migration/savevm.c:1855
#20 0x0000555555b65ed9 in colo_do_checkpoint_transaction (fb=<optimized out>, bioc=<optimized out>, s=0x5555574c0070) at ../migration/colo.c:474 #21 colo_process_checkpoint (s=0x5555574c0070) at ../migration/colo.c:592
#22 migrate_start_colo_process (s=0x5555574c0070) at ../migration/colo.c:655
#23 0x0000555555b4971e in migration_iteration_finish (s=0x5555574c0070) at ../migration/migration.c:3297
#24 migration_thread (opaque=opaque@entry=0x5555574c0070) at ../migration/migration.c:3584
#25 0x0000555555ec58c0 in qemu_thread_start (args=0x5555576583e0) at ../util/qemu-thread-posix.c:393
#26 0x00007ffff74d2aa4 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:447
#27 0x00007ffff755fc6c in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78

#27 lore raw

Re: [PATCH v3 06/10] migration-test: Add COLO migration unit test

Quoted 56 lines · Lukas Straub
Quoted 53 lines · On Mon, 2 Feb 2026 09:26:06 -0500 Peter Xu <[email protected]> wrote:
Quoted 14 lines · Lukas Straub
Quoted 11 lines · On Tue, 27 Jan 2026 15:49:31 -0500 Peter Xu <[email protected]> wrote:

Review

Quoted 6 lines · Lukas Straub
(quoted diff) +6 -0
+void migration_test_add_colo(MigrationTestEnv *env)
+{
+    if (!env->has_kvm) {
+        g_test_skip("COLO requires KVM accelerator");
+        return;
+    }    

I'm OK if you want to explicitly bypass others, but could you explanation why?

Quoted 5 lines
Quoted 2 lines

Thanks,

It used to hang with TCG. Now it crashes, since migration_bitmap_sync_precopy assumes bql is held. Something for later.

If we want to keep COLO around and be serious, let's try to make COLO the same standard we target for migration in general whenever possible. We shouldn't randomly workaround bugs. We should fix it.

It looks to me there's some locking issue instead.

Iterator's complete() requires BQL. Would a patch like below makes sense to you?

migration/colo.c +4 -1
index db783f6fa7..b3ea137120 100644
@@ -458,8 +458,8 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
     /* Note: device state is saved into buffer */
     ret = qemu_save_device_state(fb);
 
-    bql_unlock();
     if (ret < 0) {
+        bql_unlock();
         goto out;
     }
 
@@ -473,6 +473,9 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
      */
     qemu_savevm_live_state(s->to_dst_file);
 
+    /* Save live state requires BQL */
+    bql_unlock();
+
     qemu_fflush(fb);
 
Quoted 7 lines

migration/colo.c static int colo_do_checkpoint_transaction(MigrationState *s, · line 481

Quoted 1 line
migration/colo.c +0 -0
     /*

I already tested that and it works. However, we have to be very careful around the locking here and I don't think it is safe to take the bql on the primary here:

The secondary has the bql held at this point:

This is definitely an interesting piece of code... one question:

Quoted 6 lines
    colo_receive_check_message(mis->from_src_file,
                       COLO_MESSAGE_VMSTATE_SEND, &local_err);
    ...
    bql_lock();
    cpu_synchronize_all_states();

Why this is needed at all? ^^^^^^^^^^^^^^^

The qemu_loadvm_state_main() line right below should only load RAM. I don't see how it has anything to do with CPU register states..

Quoted 8 lines
    ret = qemu_loadvm_state_main(mis->from_src_file, mis, errp);
    bql_unlock();

On the primary there is a filter-mirror mirroring incoming packets to the secondary filter-redirector. However since the secondary migration holds bql the receiving filter is blocked and will not receive anything from the socket. Thus filter-mirror on the primary also may get blocked during send and block the mainloop (It uses blocking IO).

Hmm... could you explain why a blocking IO operation to mirror some packets require holding BQL? This sounds wrong on its own.

Quoted 6 lines

Now if the primary migration thread wants to take the bql it will deadlock.

So I think this is something to fix in a separate series since it is more involved.

Yes it might be involved, but this is really not something like "let's make it simple for now and improve it later". This is "OK this function _requires_ this lock, but let's not take this lock and leave it for later". It's not something we can put aside, afaiu. We should really fix it.

How far do you think we can fix it? Could you explain the problem better?

It might be helpful if you can reproduce the hang, then attach the logs from both QEMU on a full thread backtrace dump. I'll see what I can help.

Thanks,

Signature · 1 line
Peter Xu
#28 lore raw

Re: [PATCH v3 06/10] migration-test: Add COLO migration unit test

Quoted 74 lines · On Tue, 3 Feb 2026 16:21:05 -0500 Peter Xu <[email protected]> wrote:
Quoted 56 lines · Lukas Straub
Quoted 53 lines · On Mon, 2 Feb 2026 09:26:06 -0500 Peter Xu <[email protected]> wrote:
Quoted 14 lines · Lukas Straub
Quoted 11 lines · On Tue, 27 Jan 2026 15:49:31 -0500 Peter Xu <[email protected]> wrote:

Review

Quoted 6 lines · Lukas Straub
(quoted diff) +6 -0
+void migration_test_add_colo(MigrationTestEnv *env)
+{
+    if (!env->has_kvm) {
+        g_test_skip("COLO requires KVM accelerator");
+        return;
+    }      

I'm OK if you want to explicitly bypass others, but could you explanation why?

Quoted 5 lines
Quoted 2 lines

Thanks,

It used to hang with TCG. Now it crashes, since migration_bitmap_sync_precopy assumes bql is held. Something for later.

If we want to keep COLO around and be serious, let's try to make COLO the same standard we target for migration in general whenever possible. We shouldn't randomly workaround bugs. We should fix it.

It looks to me there's some locking issue instead.

Iterator's complete() requires BQL. Would a patch like below makes sense to you?

migration/colo.c +4 -1
index db783f6fa7..b3ea137120 100644
@@ -458,8 +458,8 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
     /* Note: device state is saved into buffer */
     ret = qemu_save_device_state(fb);
 
-    bql_unlock();
     if (ret < 0) {
+        bql_unlock();
         goto out;
     }
 
@@ -473,6 +473,9 @@ static int colo_do_checkpoint_transaction(MigrationState *s,
      */
     qemu_savevm_live_state(s->to_dst_file);
 
+    /* Save live state requires BQL */
+    bql_unlock();
+
     qemu_fflush(fb);
 
Quoted 7 lines

migration/colo.c static int colo_do_checkpoint_transaction(MigrationState *s, · line 481

Quoted 1 line
migration/colo.c +0 -0
     /*  

I already tested that and it works. However, we have to be very careful around the locking here and I don't think it is safe to take the bql on the primary here:

The secondary has the bql held at this point:

This is definitely an interesting piece of code... one question:

Quoted 6 lines
    colo_receive_check_message(mis->from_src_file,
                       COLO_MESSAGE_VMSTATE_SEND, &local_err);
    ...
    bql_lock();
    cpu_synchronize_all_states();  
Quoted 4 lines

Why this is needed at all? ^^^^^^^^^^^^^^^

The qemu_loadvm_state_main() line right below should only load RAM. I don't see how it has anything to do with CPU register states..

You are right we don't need this and the lock is needed here. Then I'm fine with removing the lock here and adding one on the primary side.

Quoted 4 lines
Quoted 4 lines
    ret = qemu_loadvm_state_main(mis->from_src_file, mis, errp);
    bql_unlock();
Quoted 8 lines
Quoted 5 lines

On the primary there is a filter-mirror mirroring incoming packets to the secondary filter-redirector. However since the secondary migration holds bql the receiving filter is blocked and will not receive anything from the socket. Thus filter-mirror on the primary also may get blocked during send and block the mainloop (It uses blocking IO).

Hmm... could you explain why a blocking IO operation to mirror some packets require holding BQL? This sounds wrong on its own.

Yes there is no need for the BQL, it just is wrong. The tap fd gets a POLLIN event, main loop takes BQL and calls the tap fd callback. Tap reads a packet from the fd and calls qemu_send_packet_async() which puts it through the net-filters and filter-mirror does a blocking send, blocking the main loop while BQL is held.

Quoted 21 lines
Quoted 7 lines

Now if the primary migration thread wants to take the bql it will deadlock.

So I think this is something to fix in a separate series since it is more involved.

Yes it might be involved, but this is really not something like "let's make it simple for now and improve it later". This is "OK this function _requires_ this lock, but let's not take this lock and leave it for later". It's not something we can put aside, afaiu. We should really fix it..

How far do you think we can fix it? Could you explain the problem better?

It might be helpful if you can reproduce the hang, then attach the logs from both QEMU on a full thread backtrace dump. I'll see what I can help.

Thanks,

Tested-by on v3 6/10
#29 lore raw

Re: [PATCH v3 06/10] migration-test: Add COLO migration unit test

In reply to Lukas Straub
Quoted 270 lines · Lukas Straub

Add a COLO migration test for COLO migration and failover.

---
 MAINTAINERS                        |   1 +
 tests/qtest/meson.build            |   7 +-
 tests/qtest/migration-test.c       |   1 +
 tests/qtest/migration/colo-tests.c | 199 +++++++++++++++++++++++++++++++++++++
 tests/qtest/migration/framework.h  |   5 +
 5 files changed, 212 insertions(+), 1 deletion(-)
MAINTAINERS +1 -0
index 883f0a8f4eb92d0bf0f89fcab4674ccc4aed1cc1..2a8b9b2d051883c1b7adce9c1afec80d16a317f8 100644
@@ -3856,6 +3856,7 @@ F: migration/colo*
 F: migration/multifd-colo.*
 F: include/migration/colo.h
 F: include/migration/failover.h
+F: tests/qtest/migration/colo-tests.c
 F: docs/COLO-FT.txt
 
 COLO Proxy
tests/qtest/meson.build +6 -1
index dfb83650c643d884daad53a66034ab7aa8c45509..624f7744ec9bd81c8823075b966bc95f7750a667 100644
@@ -371,6 +371,11 @@ if gnutls.found()
   endif
 endif
 
+migration_colo_files = []
+if get_option('replication').allowed()
+  migration_colo_files = [files('migration/colo-tests.c')]
+endif
+
 qtests = {
   'aspeed_hace-test': files('aspeed-hace-utils.c', 'aspeed_hace-test.c'),
   'aspeed_smc-test': files('aspeed-smc-utils.c', 'aspeed_smc-test.c'),
@@ -382,7 +387,7 @@ qtests = {
                              'migration/migration-util.c') + dbus_vmstate1,
   'erst-test': files('erst-test.c'),
   'ivshmem-test': [rt, '../../contrib/ivshmem-server/ivshmem-server.c'],
-  'migration-test': test_migration_files + migration_tls_files,
+  'migration-test': test_migration_files + migration_tls_files + migration_colo_files,
   'pxe-test': files('boot-sector.c'),
   'pnv-xive2-test': files('pnv-xive2-common.c', 'pnv-xive2-flush-sync.c',
                           'pnv-xive2-nvpg_bar.c'),
tests/qtest/migration-test.c +1 -0
index 08936871741535c926eeac40a7d7c3f461c72fd0..e582f05c7dc2673dbd05a936df8feb6c964b5bbc 100644
@@ -55,6 +55,7 @@ int main(int argc, char **argv)
     migration_test_add_precopy(env);
     migration_test_add_cpr(env);
     migration_test_add_misc(env);
+    migration_test_add_colo(env);
 
     ret = g_test_run();
 
tests/qtest/migration/colo-tests.c new file +199 -0
new file mode 100644
index 0000000000000000000000000000000000000000..0586970e206f01ed6e7aa3429321aefc1de7be37
@@ -0,0 +1,199 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * QTest testcases for COLO migration
+ *
+ * Copyright (c) 2025 Lukas Straub <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu/osdep.h"
+#include "libqtest.h"
+#include "migration/framework.h"
+#include "migration/migration-qmp.h"
+#include "migration/migration-util.h"
+#include "qemu/module.h"
+
+static int test_colo_common(MigrateCommon *args,
+                            bool failover_during_checkpoint,
+                            bool primary_failover)
+{
+    QTestState *from, *to;
+    void *data_hook = NULL;
+
+    /*
+     * For the COLO test, both VMs will run in parallel. Thus both VMs want to
+     * open the image read/write at the same time. Using read-only=on is not
+     * possible here, because ide-hd does not support read-only backing image.
+     *
+     * So use -snapshot, where each qemu instance creates its own writable
+     * snapshot internally while leaving the real image read-only.
+     */
+    args->start.opts_source = "-snapshot";
+    args->start.opts_target = "-snapshot";
+
+    /*
+     * COLO migration code logs many errors when the migration socket
+     * is shut down, these are expected so we hide them here.
+     */
+    args->start.hide_stderr = true;
+
+    args->start.oob = true;
+    args->start.caps[MIGRATION_CAPABILITY_X_COLO] = true;
+
+    if (migrate_start(&from, &to, args->listen_uri, &args->start)) {
+        return -1;
+    }
+
+    migrate_set_parameter_int(from, "x-checkpoint-delay", 300);
+
+    if (args->start_hook) {
+        data_hook = args->start_hook(from, to);
+    }
+
+    migrate_ensure_converge(from);
+    wait_for_serial("src_serial");
+
+    migrate_qmp(from, to, args->connect_uri, NULL, "{}");
+
+    wait_for_migration_status(from, "colo", NULL);
+    wait_for_resume(to, get_dst());
+
+    wait_for_serial("src_serial");
+    wait_for_serial("dest_serial");
+
+    /* wait for 3 checkpoints */
+    for (int i = 0; i < 3; i++) {
+        qtest_qmp_eventwait(to, "RESUME");
+        wait_for_serial("src_serial");
+        wait_for_serial("dest_serial");
+    }
+
+    if (failover_during_checkpoint) {
+        qtest_qmp_eventwait(to, "STOP");
+    }
+    if (primary_failover) {
+        qtest_qmp_assert_success(from, "{'exec-oob': 'yank', 'id': 'yank-cmd', "
+                                            "'arguments': {'instances':"
+                                                "[{'type': 'migration'}]}}");
+        qtest_qmp_assert_success(from, "{'execute': 'x-colo-lost-heartbeat'}");
+        wait_for_serial("src_serial");
+    } else {
+        qtest_qmp_assert_success(to, "{'exec-oob': 'yank', 'id': 'yank-cmd', "
+                                        "'arguments': {'instances':"
+                                            "[{'type': 'migration'}]}}");
+        qtest_qmp_assert_success(to, "{'execute': 'x-colo-lost-heartbeat'}");
+        wait_for_serial("dest_serial");
+    }
+
+    if (args->end_hook) {
+        args->end_hook(from, to, data_hook);
+    }
+
+    migrate_end(from, to, !primary_failover);
+
+    return 0;
+}
+
+static void test_colo_plain_common(MigrateCommon *args,
+                                   bool failover_during_checkpoint,
+                                   bool primary_failover)
+{
+    args->listen_uri = "tcp:127.0.0.1:0";
+    test_colo_common(args, failover_during_checkpoint, primary_failover);
+}
+
+static void *hook_start_multifd(QTestState *from, QTestState *to)
+{
+    return migrate_hook_start_precopy_tcp_multifd_common(from, to, "none");
+}
+
+static void test_colo_multifd_common(MigrateCommon *args,
+                                     bool failover_during_checkpoint,
+                                     bool primary_failover)
+{
+    args->listen_uri = "defer";
+    args->start_hook = hook_start_multifd;
+    args->start.caps[MIGRATION_CAPABILITY_MULTIFD] = true;
+    test_colo_common(args, failover_during_checkpoint, primary_failover);
+}
+
+static void test_colo_plain_primary_failover(char *name, MigrateCommon *args)
+{
+    test_colo_plain_common(args, false, true);
+}
+
+static void test_colo_plain_secondary_failover(char *name, MigrateCommon *args)
+{
+    test_colo_plain_common(args, false, false);
+}
+
+static void test_colo_multifd_primary_failover(char *name, MigrateCommon *args)
+{
+    test_colo_multifd_common(args, false, true);
+}
+
+static void test_colo_multifd_secondary_failover(char *name,
+                                                 MigrateCommon *args)
+{
+    test_colo_multifd_common(args, false, false);
+}
+
+static void test_colo_plain_primary_failover_checkpoint(char *name,
+                                                        MigrateCommon *args)
+{
+    test_colo_plain_common(args, true, true);
+}
+
+static void test_colo_plain_secondary_failover_checkpoint(char *name,
+                                                          MigrateCommon *args)
+{
+    test_colo_plain_common(args, true, false);
+}
+
+static void test_colo_multifd_primary_failover_checkpoint(char *name,
+                                                          MigrateCommon *args)
+{
+    test_colo_multifd_common(args, true, true);
+}
+
+static void test_colo_multifd_secondary_failover_checkpoint(char *name,
+                                                            MigrateCommon *args)
+{
+    test_colo_multifd_common(args, true, false);
+}
+
+void migration_test_add_colo(MigrationTestEnv *env)
+{
+    if (!env->has_kvm) {
+        g_test_skip("COLO requires KVM accelerator");
+        return;
+    }
+
+    if (!env->full_set) {
+        return;
+    }
+
+    migration_test_add("/migration/colo/plain/primary_failover",
+                       test_colo_plain_primary_failover);
+    migration_test_add("/migration/colo/plain/secondary_failover",
+                       test_colo_plain_secondary_failover);
+
+    migration_test_add("/migration/colo/multifd/primary_failover",
+                       test_colo_multifd_primary_failover);
+    migration_test_add("/migration/colo/multifd/secondary_failover",
+                       test_colo_multifd_secondary_failover);
+
+    migration_test_add("/migration/colo/plain/primary_failover_checkpoint",
+                       test_colo_plain_primary_failover_checkpoint);
+    migration_test_add("/migration/colo/plain/secondary_failover_checkpoint",
+                       test_colo_plain_secondary_failover_checkpoint);
+
+    migration_test_add("/migration/colo/multifd/primary_failover_checkpoint",
+                       test_colo_multifd_primary_failover_checkpoint);
+    migration_test_add("/migration/colo/multifd/secondary_failover_checkpoint",
+                       test_colo_multifd_secondary_failover_checkpoint);
+}
tests/qtest/migration/framework.h +0 -0
index 40984d04930da2d181326d9f6a742bde49018103..80eef758932ce9c301ed6c0f6383d18756144870 100644

tests/qtest/migration/framework.h void migration_test_add_file(MigrationTestEnv *env); · line 273

Quoted 10 lines
tests/qtest/migration/framework.h +5 -0
@@ -264,5 +264,10 @@ void migration_test_add_file(MigrationTestEnv *env);
 void migration_test_add_precopy(MigrationTestEnv *env);
 void migration_test_add_cpr(MigrationTestEnv *env);
 void migration_test_add_misc(MigrationTestEnv *env);
+#ifdef CONFIG_REPLICATION
+void migration_test_add_colo(MigrationTestEnv *env);
+#else
+static inline void migration_test_add_colo(MigrationTestEnv *env) {};
+#endif
 
 #endif /* TEST_FRAMEWORK_H */

It survived my stress run. It hit once the race at migration_shutdown() where current_migration is already freed, but we can ignore that because it's preexisting.

#30 lore raw

PATCH v3 7/10 Convert colo main documentation to restructuredText

In reply to Lukas Straub
---
 MAINTAINERS               |   2 +-
 docs/COLO-FT.txt          | 334 ------------------------------------------
 docs/system/index.rst     |   1 +
 docs/system/qemu-colo.rst | 360 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 362 insertions(+), 335 deletions(-)
MAINTAINERS +1 -1
index 2a8b9b2d051883c1b7adce9c1afec80d16a317f8..7d396183cef0f5e2064e016cf479765b97820b71 100644
@@ -3857,7 +3857,7 @@ F: migration/multifd-colo.*
 F: include/migration/colo.h
 F: include/migration/failover.h
 F: tests/qtest/migration/colo-tests.c
-F: docs/COLO-FT.txt
+F: docs/system/qemu-colo.rst
 
 COLO Proxy
 M: Zhang Chen <[email protected]>
docs/COLO-FT.txt deleted +0 -334

334 lines not on this page · raw message

docs/system/index.rst +1 -0
index 427b020483104f6589878bbf255a367ae114c61b..6268c41aea9c74dc3e59d896b5ae082360bfbb1a 100644
@@ -41,3 +41,4 @@ or Hypervisor.Framework.
    igvm
    vm-templating
    sriov
+   qemu-colo
docs/system/qemu-colo.rst new file +360 -0

360 lines not on this page · raw message

Signature · 1 line
2.39.5
#31 lore raw

PATCH v3 8/10 qemu-colo.rst: Miscellaneous changes

In reply to Lukas Straub
---
 docs/system/qemu-colo.rst | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)
docs/system/qemu-colo.rst +18 -17
index 4b5fbbf398f8a5c4ea6baad615bde94b2b4678d2..a70e61aa09391cda933031535fa982d27cf6654b 100644
@@ -1,13 +1,6 @@
 Qemu COLO Fault Tolerance
 =========================
 
-| Copyright (c) 2016 Intel Corporation
-| Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
-| Copyright (c) 2016 Fujitsu, Corp.
-
-This work is licensed under the terms of the GNU GPL, version 2 or later.
-See the COPYING file in the top-level directory.
-
 This document gives an overview of COLO's design and how to use it.
 
 Background
@@ -82,8 +75,8 @@ Overview::
         |   Storage     |  |External Network|       | External Network | |   Storage    |
         +---------------+  +----------------+       +------------------+ +--------------+
 
-Components introduction
-^^^^^^^^^^^^^^^^^^^^^^^
+Components
+^^^^^^^^^^
 You can see there are several components in COLO's diagram of architecture.
 Their functions are described below.
 
@@ -157,14 +150,21 @@ in test procedure.
 
 Test procedure
 --------------
-Note: Here we are running both instances on the same host for testing,
+
+Setup
+^^^^^
+
+Here we are running both instances on the same host for testing,
 change the IP Addresses if you want to run it on two hosts. Initially
 ``127.0.0.1`` is the Primary Host and ``127.0.0.2`` is the Secondary Host.
 
+COLO uses double the guest ram size on the secondary side. The Qemu version
+should be the same on both hosts.
+
 Startup qemu
 ^^^^^^^^^^^^
 **1. Primary**:
-Note: Initially, ``$imagefolder/primary.qcow2`` needs to be copied to all hosts.
+Initially, ``$imagefolder/primary.qcow2`` needs to be copied to all hosts.
 You don't need to change any IP's here, because ``0.0.0.0`` listens on any
 interface. The chardev's with ``127.0.0.1`` IP's loopback to the local qemu
 instance::
@@ -192,7 +192,7 @@ instance::
 
 
 **2. Secondary**:
-Note: Active and hidden images need to be created only once and the
+Active and hidden images need to be created only once and the
 size should be the same as ``primary.qcow2``. Again, you don't need to change
 any IP's here, except for the ``$primary_ip`` variable::
 
@@ -353,8 +353,9 @@ Wait until disk is synced, then::
     {"execute": "migrate-set-capabilities", "arguments":{ "capabilities": [ {"capability": "x-colo", "state": true } ] } }
     {"execute": "migrate", "arguments":{ "uri": "tcp:127.0.0.1:9998" } }
 
-TODO
-----
-1. Support shared storage.
-2. Develop the heartbeat part.
-3. Reduce checkpoint VM’s downtime while doing checkpoint.
+| Copyright (c) 2016 Intel Corporation
+| Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
+| Copyright (c) 2016 Fujitsu, Corp.
+
+This work is licensed under the terms of the GNU GPL, version 2 or later.
+See the COPYING file in the top-level directory.
Signature · 1 line
2.39.5
#32 lore raw

Re: [PATCH v3 08/10] qemu-colo.rst: Miscellaneous changes

Quoted 2 lines · Lukas Straub

It seems this patch doesn't offer any major changes and merging it with the previous patch(7/10) would be more appropriate.

Thanks

Chen

Quoted 85 lines
---
 docs/system/qemu-colo.rst | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)
docs/system/qemu-colo.rst +18 -18
index 4b5fbbf398f8a5c4ea6baad615bde94b2b4678d2..a70e61aa09391cda933031535fa982d27cf6654b 100644
@@ -1,13 +1,6 @@
 Qemu COLO Fault Tolerance
 =========================
 
-| Copyright (c) 2016 Intel Corporation
-| Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
-| Copyright (c) 2016 Fujitsu, Corp.
-
-This work is licensed under the terms of the GNU GPL, version 2 or later.
-See the COPYING file in the top-level directory.
-
 This document gives an overview of COLO's design and how to use it.
 
 Background
@@ -82,8 +75,8 @@ Overview::
         |   Storage     |  |External Network|       | External Network | |   Storage    |
         +---------------+  +----------------+       +------------------+ +--------------+
 
-Components introduction
-^^^^^^^^^^^^^^^^^^^^^^^
+Components
+^^^^^^^^^^
 You can see there are several components in COLO's diagram of architecture.
 Their functions are described below.
 
@@ -157,14 +150,21 @@ in test procedure.
 
 Test procedure
 --------------
-Note: Here we are running both instances on the same host for testing,
+
+Setup
+^^^^^
+
+Here we are running both instances on the same host for testing,
 change the IP Addresses if you want to run it on two hosts. Initially
 ``127.0.0.1`` is the Primary Host and ``127.0.0.2`` is the Secondary Host.
 
+COLO uses double the guest ram size on the secondary side. The Qemu version
+should be the same on both hosts.
+
 Startup qemu
 ^^^^^^^^^^^^
 **1. Primary**:
-Note: Initially, ``$imagefolder/primary.qcow2`` needs to be copied to all hosts.
+Initially, ``$imagefolder/primary.qcow2`` needs to be copied to all hosts.
 You don't need to change any IP's here, because ``0.0.0.0`` listens on any
 interface. The chardev's with ``127.0.0.1`` IP's loopback to the local qemu
 instance::
@@ -192,7 +192,7 @@ instance::
 
 
 **2. Secondary**:
-Note: Active and hidden images need to be created only once and the
+Active and hidden images need to be created only once and the
 size should be the same as ``primary.qcow2``. Again, you don't need to change
 any IP's here, except for the ``$primary_ip`` variable::
 
@@ -353,8 +353,9 @@ Wait until disk is synced, then::
     {"execute": "migrate-set-capabilities", "arguments":{ "capabilities": [ {"capability": "x-colo", "state": true } ] } }
     {"execute": "migrate", "arguments":{ "uri": "tcp:127.0.0.1:9998" } }
 
-TODO
-----
-1. Support shared storage.
-2. Develop the heartbeat part.
-3. Reduce checkpoint VM’s downtime while doing checkpoint.
+| Copyright (c) 2016 Intel Corporation
+| Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
+| Copyright (c) 2016 Fujitsu, Corp.
+
+This work is licensed under the terms of the GNU GPL, version 2 or later.
+See the COPYING file in the top-level directory.
 
--

2.39.5

#33 lore raw

Re: [PATCH v3 08/10] qemu-colo.rst: Miscellaneous changes

Quoted 6 lines · On Mon, 26 Jan 2026 18:21:10 +0800 Zhang Chen <[email protected]> wrote:
Quoted 2 lines · Lukas Straub

It seems this patch doesn't offer any major changes and merging it with the previous patch(7/10) would be more appropriate.

It's better to have it separately to see what is changed exactly. The previous patch only does 1:1 conversion from text to rst without any other changes.

If merged with the previous patch it becomes harder to see that I moved and added a few lines here, which are changes that where not in the original text file.

Regards,
Lukas Straub

Quoted 90 lines

Thanks

Chen

Quoted 85 lines
---
 docs/system/qemu-colo.rst | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)
docs/system/qemu-colo.rst +18 -18
index 4b5fbbf398f8a5c4ea6baad615bde94b2b4678d2..a70e61aa09391cda933031535fa982d27cf6654b 100644
@@ -1,13 +1,6 @@
 Qemu COLO Fault Tolerance
 =========================
 
-| Copyright (c) 2016 Intel Corporation
-| Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
-| Copyright (c) 2016 Fujitsu, Corp.
-
-This work is licensed under the terms of the GNU GPL, version 2 or later.
-See the COPYING file in the top-level directory.
-
 This document gives an overview of COLO's design and how to use it.
 
 Background
@@ -82,8 +75,8 @@ Overview::
         |   Storage     |  |External Network|       | External Network | |   Storage    |
         +---------------+  +----------------+       +------------------+ +--------------+
 
-Components introduction
-^^^^^^^^^^^^^^^^^^^^^^^
+Components
+^^^^^^^^^^
 You can see there are several components in COLO's diagram of architecture.
 Their functions are described below.
 
@@ -157,14 +150,21 @@ in test procedure.
 
 Test procedure
 --------------
-Note: Here we are running both instances on the same host for testing,
+
+Setup
+^^^^^
+
+Here we are running both instances on the same host for testing,
 change the IP Addresses if you want to run it on two hosts. Initially
 ``127.0.0.1`` is the Primary Host and ``127.0.0.2`` is the Secondary Host.
 
+COLO uses double the guest ram size on the secondary side. The Qemu version
+should be the same on both hosts.
+
 Startup qemu
 ^^^^^^^^^^^^
 **1. Primary**:
-Note: Initially, ``$imagefolder/primary.qcow2`` needs to be copied to all hosts.
+Initially, ``$imagefolder/primary.qcow2`` needs to be copied to all hosts.
 You don't need to change any IP's here, because ``0.0.0.0`` listens on any
 interface. The chardev's with ``127.0.0.1`` IP's loopback to the local qemu
 instance::
@@ -192,7 +192,7 @@ instance::
 
 
 **2. Secondary**:
-Note: Active and hidden images need to be created only once and the
+Active and hidden images need to be created only once and the
 size should be the same as ``primary.qcow2``. Again, you don't need to change
 any IP's here, except for the ``$primary_ip`` variable::
 
@@ -353,8 +353,9 @@ Wait until disk is synced, then::
     {"execute": "migrate-set-capabilities", "arguments":{ "capabilities": [ {"capability": "x-colo", "state": true } ] } }
     {"execute": "migrate", "arguments":{ "uri": "tcp:127.0.0.1:9998" } }
 
-TODO
-----
-1. Support shared storage.
-2. Develop the heartbeat part.
-3. Reduce checkpoint VM’s downtime while doing checkpoint.
+| Copyright (c) 2016 Intel Corporation
+| Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
+| Copyright (c) 2016 Fujitsu, Corp.
+
+This work is licensed under the terms of the GNU GPL, version 2 or later.
+See the COPYING file in the top-level directory.
 
--

2.39.5

#34 lore raw

PATCH v3 9/10 qemu-colo.rst: Add my copyright

In reply to Lukas Straub

I have so far contributed 61 commits to the colo project, waranting the addition of my copyright to this file.

---
 docs/system/qemu-colo.rst | 1 +
 1 file changed, 1 insertion(+)
docs/system/qemu-colo.rst +1 -0
index a70e61aa09391cda933031535fa982d27cf6654b..75abbd80298df79223cb8e70064a5dc83d70f4eb 100644
@@ -356,6 +356,7 @@ Wait until disk is synced, then::
 | Copyright (c) 2016 Intel Corporation
 | Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
 | Copyright (c) 2016 Fujitsu, Corp.
+| Copyright (c) 2026 Lukas Straub <[email protected]>
 
 This work is licensed under the terms of the GNU GPL, version 2 or later.
 See the COPYING file in the top-level directory.
Signature · 1 line
2.39.5
Reviewed-by on v3 9/10
#35 lore raw

Re: [PATCH v3 09/10] qemu-colo.rst: Add my copyright

Quoted 5 lines · Lukas Straub

I have so far contributed 61 commits to the colo project, waranting the addition of my copyright to this file.

Thanks
Chen

Quoted 20 lines
---
 docs/system/qemu-colo.rst | 1 +
 1 file changed, 1 insertion(+)
docs/system/qemu-colo.rst +1 -1
index a70e61aa09391cda933031535fa982d27cf6654b..75abbd80298df79223cb8e70064a5dc83d70f4eb 100644
@@ -356,6 +356,7 @@ Wait until disk is synced, then::
 | Copyright (c) 2016 Intel Corporation
 | Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
 | Copyright (c) 2016 Fujitsu, Corp.
+| Copyright (c) 2026 Lukas Straub <[email protected]>
 
 This work is licensed under the terms of the GNU GPL, version 2 or later.
 See the COPYING file in the top-level directory.
 
--

2.39.5

#36 lore raw

PATCH v3 10/10 qemu-colo.rst: Simplify the block replication setup

In reply to Lukas Straub

On the primary side we don't actually need the replication block driver, since it only passes trough all IO. So simplify the setup and also use 'blockdev-add' instead of 'human-monitor-command'.

---
 docs/system/qemu-colo.rst | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
docs/system/qemu-colo.rst +7 -7
index 75abbd80298df79223cb8e70064a5dc83d70f4eb..f7d3b6439cf3401a58c412634239d1a43999a10e 100644
@@ -240,8 +240,8 @@ Note:
 **4.** On Primary VM's QEMU monitor, issue command::
 
     {"execute":"qmp_capabilities"}
-    {"execute": "human-monitor-command", "arguments": {"command-line": "drive_add -n buddy driver=replication,mode=primary,file.driver=nbd,file.host=127.0.0.2,file.port=9999,file.export=parent0,node-name=replication0"}}
-    {"execute": "x-blockdev-change", "arguments":{"parent": "colo-disk0", "node": "replication0" } }
+    {"execute": "blockdev-add", "arguments": {"driver": "nbd", "node-name": "nbd0", "server": {"type": "inet", "host": "127.0.0.2", "port": "9999"}, "export": "parent0", "detect-zeroes": "on"} }
+    {"execute": "x-blockdev-change", "arguments":{"parent": "colo-disk0", "node": "nbd0" } }
     {"execute": "migrate-set-capabilities", "arguments": {"capabilities": [ {"capability": "x-colo", "state": true } ] } }
     {"execute": "migrate", "arguments": {"uri": "tcp:127.0.0.2:9998" } }
 
@@ -269,7 +269,7 @@ Primary Failover
 The Secondary died, resume on the Primary::
 
     {"execute": "x-blockdev-change", "arguments":{ "parent": "colo-disk0", "child": "children.1"} }
-    {"execute": "human-monitor-command", "arguments":{ "command-line": "drive_del replication0" } }
+    {"execute": "blockdev-del", "arguments": {"node-name": "nbd0"} }
     {"execute": "object-del", "arguments":{ "id": "comp0" } }
     {"execute": "object-del", "arguments":{ "id": "iothread1" } }
     {"execute": "object-del", "arguments":{ "id": "m0" } }
@@ -309,8 +309,8 @@ Wait until disk is synced, then::
     {"execute": "stop"}
     {"execute": "block-job-cancel", "arguments":{ "device": "resync"} }
 
-    {"execute": "human-monitor-command", "arguments":{ "command-line": "drive_add -n buddy driver=replication,mode=primary,file.driver=nbd,file.host=127.0.0.2,file.port=9999,file.export=parent0,node-name=replication0"}}
-    {"execute": "x-blockdev-change", "arguments":{ "parent": "colo-disk0", "node": "replication0" } }
+    {"execute": "blockdev-add", "arguments": {"driver": "nbd", "node-name": "nbd0", "server": {"type": "inet", "host": "127.0.0.2", "port": "9999"}, "export": "parent0", "detect-zeroes": "on"} }
+    {"execute": "x-blockdev-change", "arguments":{ "parent": "colo-disk0", "node": "nbd0" } }
 
     {"execute": "object-add", "arguments":{ "qom-type": "filter-mirror", "id": "m0", "netdev": "hn0", "queue": "tx", "outdev": "mirror0" } }
     {"execute": "object-add", "arguments":{ "qom-type": "filter-redirector", "id": "redire0", "netdev": "hn0", "queue": "rx", "indev": "compare_out" } }
@@ -341,8 +341,8 @@ Wait until disk is synced, then::
     {"execute": "stop"}
     {"execute": "block-job-cancel", "arguments":{ "device": "resync" } }
 
-    {"execute": "human-monitor-command", "arguments":{ "command-line": "drive_add -n buddy driver=replication,mode=primary,file.driver=nbd,file.host=127.0.0.1,file.port=9999,file.export=parent0,node-name=replication0"}}
-    {"execute": "x-blockdev-change", "arguments":{ "parent": "colo-disk0", "node": "replication0" } }
+    {"execute": "blockdev-add", "arguments": {"driver": "nbd", "node-name": "nbd0", "server": {"type": "inet", "host": "127.0.0.1", "port": "9999"}, "export": "parent0", "detect-zeroes": "on"} }
+    {"execute": "x-blockdev-change", "arguments":{ "parent": "colo-disk0", "node": "nbd0" } }
 
     {"execute": "object-add", "arguments":{ "qom-type": "filter-mirror", "id": "m0", "insert": "before", "position": "id=rew0", "netdev": "hn0", "queue": "tx", "outdev": "mirror0" } }
     {"execute": "object-add", "arguments":{ "qom-type": "filter-redirector", "id": "redire0", "insert": "before", "position": "id=rew0", "netdev": "hn0", "queue": "rx", "indev": "compare_out" } }
Signature · 1 line
2.39.5