migration: Threadify loadvm process
cover migration: Threadify loadvm process
This is v1, however not 10.2 material. The earliest I see fit would still be 11.0+ even if everything goes extremely smooth.
Removal of RFC is only about that I'm more confident this should be able to land without breaking something too easily, as I smoked it slightly more cross-archs this time. AFAIU the best (and possibly only..) way to prove it solid is to merge it.. likely in the early phase of a dev cycle.
The plan is we'll try to get to more device setups too soon, before it could land.
Background
==========
Nowadays, live migration heavily depends on threads. For example, most of the major features that will be used nowadays in live migration (multifd, postcopy, mapped-ram, vfio, etc.) all work with threads internally.
But still, from time to time, we'll see some coroutines floating around the migration context. The major one is precopy's loadvm, which is internally a coroutine. It is still a critical path that any live migration depends on.
A mixture of using both coroutines and threads is prone to issues. Some examples can refer to commit e65cec5e5d ("migration/ram: Yield periodically to the main loop") or commit 7afbdada7e ("migration/postcopy: ensure preempt channel is ready before loading states").
It was a coroutine since this work (thanks to Fabiano, the archeologist, digging the link):
https://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01136.html
Overview
========
This series tries to move migration further into the thread-based model, by allowing the loadvm process to happen in a thread rather than in the main thread with a coroutine.
Luckily, since the qio channel code is always ready for both cases, IO paths should all be fine.
Note that loadvm for postcopy already happens in a ram load thread which is separate. However, RAM is just the simple case here, even it has its own challenges (on atomically update of the pgtables), its complexity lies in the kernel.
For precopy, loadvm has quite a few operations that will need BQL. The question is we can't take BQL for the whole process of loadvm, because that'll block the main thread from executions (e.g. QMP hangs). Here, the finer granule we can push BQL the better. This series so far chose somewhere in the middle, by taking BQL on majorly these two places:
- CPU synchronizations
- Device START/FULL sections
After this series applied, most of the rest loadvm path will run without BQL anymore. There is a more detailed discussion / todo in the commit message of patch "migration: Thread-ify precopy vmstate load process" explaning how to further split the BQL critical sections.
After the series applied, the only leftover pieces in migration/ that would use a coroutine is snapshot save/load/delete jobs.
Tests
=====
Default CI passes.
RDMA unit tests pass as usual. I also tried out cancellation / failure tests over RDMA channels, making sure nothing is stuck.
I also roughly measured how long it takes to run the whole 80+ migration qtest suite, and see no measurable difference before / after this series.
I didn't test COLO, I wanted to but the doc example didn't work.
Risks
=====
This series has the risk of breaking things. I would be surprised if it didn't..
The current way of taking BQL during FULL section load may cause issues, it means when the IOs are unstable we could be waiting for IO (in the new migration incoming thread) with BQL held. This is low possibility, though, only happens when the network halts during flushing the device states. However still possible. One solution is to further breakdown the BQL critical sections to smaller sections, as mentioned in TODO.
Anything more than welcomed: suggestions, questions, objections, tests..
TODO
====
- Finer grained BQL breakdown
Peter Xu (13):
io: Add qio_channel_wait_cond() helper
migration: Properly wait on G_IO_IN when peeking messages migration/rdma: Fix wrong context in qio_channel_rdma_shutdown() migration/rdma: Allow qemu_rdma_wait_comp_channel work with thread migration/rdma: Change io_create_watch() to return immediately
migration: Introduce WITH_BQL_HELD() / WITH_BQL_RELEASED()
migration: Pass in bql_held information from qemu_loadvm_state()
migration: Thread-ify precopy vmstate load process migration/rdma: Remove coroutine path in qemu_rdma_wait_comp_channel migration/postcopy: Remove workaround on wait preempt channel migration/ram: Remove workaround on ram yield during load
migration: Allow blocking mode for incoming live migration migration/vfio: Drop BQL dependency for loadvm SWITCHOVER_START
include/io/channel.h | 15 +++ include/migration/colo.h | 6 +- migration/migration.h | 109 +++++++++++++++++-- migration/savevm.h | 4 +- hw/vfio/migration-multifd.c | 3 - io/channel.c | 21 ++-- migration/channel.c | 7 +- migration/colo-stubs.c | 2 +- migration/colo.c | 26 ++--- migration/migration.c | 81 ++++++++------ migration/qemu-file.c | 6 +- migration/ram.c | 13 +-- migration/rdma.c | 204 ++++++++---------------------------- migration/savevm.c | 98 +++++++++-------- migration/trace-events | 4 +- 15 files changed, 291 insertions(+), 308 deletions(-)
Signature · 1 line
2.50.1
PATCH 1/13 io: Add qio_channel_wait_cond() helper
Add the helper to wait for QIO channel's IO availability in any context (coroutine, or non-coroutine). Use it tree-wide for three occurences.
--- include/io/channel.h | 15 +++++++++++++++ io/channel.c | 21 +++++++++++---------- migration/qemu-file.c | 6 +----- 3 files changed, 27 insertions(+), 15 deletions(-)
include/io/channel.h +15 -0
@@ -871,6 +871,21 @@ void qio_channel_wake_read(QIOChannel *ioc);void qio_channel_wait (QIOChannel *ioc,GIOCondition condition); +/** +* qio_channel_wait_cond: +* @ioc: the channel object +* @condition: the I/O condition to wait for +* +* Block execution from the current thread until +* the condition indicated by @condition becomes +* available. +* +* This will work with/without a coroutine context, by automatically select +* the proper API to wait. +*/ +void qio_channel_wait_cond (QIOChannel *ioc, +GIOCondition condition); +/** * qio_channel_set_aio_fd_handler: * @ioc: the channel object
io/channel.c +11 -10
@@ -159,11 +159,7 @@ int coroutine_mixed_fn qio_channel_readv_full_all_eof(QIOChannel *ioc, len =qio_channel_readv_full (ioc, local_iov, nlocal_iov, local_fds, local_nfds, flags, errp);if (len ==QIO_CHANNEL_ERR_BLOCK ) { -if (qemu_in_coroutine ()) { -qio_channel_yield (ioc,G_IO_IN ); - }else { -qio_channel_wait (ioc,G_IO_IN ); - } +qio_channel_wait_cond (ioc,G_IO_IN );continue ; }
@@ -268,11 +264,7 @@ int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc, nfds, flags, errp);if (len ==QIO_CHANNEL_ERR_BLOCK ) { -if (qemu_in_coroutine ()) { -qio_channel_yield (ioc,G_IO_OUT ); - }else { -qio_channel_wait (ioc,G_IO_OUT ); - } +qio_channel_wait_cond (ioc,G_IO_OUT );continue ; }if (len <0 ) {
@@ -774,6 +766,15 @@ void qio_channel_wait(QIOChannel *ioc,g_main_context_unref (ctxt); } +void qio_channel_wait_cond (QIOChannel *ioc, +GIOCondition condition) +{ +if (qemu_in_coroutine ()) { +qio_channel_yield (ioc, condition); + }else { +qio_channel_wait (ioc, condition); + } +}static void qio_channel_finalize (Object *obj) {
migration/qemu-file.c +1 -5
@@ -343,11 +343,7 @@ static ssize_t coroutine_mixed_fn qemu_fill_buffer(QEMUFile *f)QIO_CHANNEL_READ_FLAG_FD_PRESERVE_BLOCKING , &local_error);if (len ==QIO_CHANNEL_ERR_BLOCK ) { -if (qemu_in_coroutine ()) { -qio_channel_yield (f->ioc,G_IO_IN ); - }else { -qio_channel_wait (f->ioc,G_IO_IN ); - } +qio_channel_wait_cond (f->ioc,G_IO_IN ); } }while (len ==QIO_CHANNEL_ERR_BLOCK );
Signature · 1 line
2.50.1
Re: [PATCH 01/13] io: Add qio_channel_wait_cond() helper
Quoted 6 lines · Peter Xu
Add the helper to wait for QIO channel's IO availability in any context (coroutine, or non-coroutine). Use it tree-wide for three occurences.
Cc Daniel P. Berrangé<[email protected]>Signed-off-by Peter Xu<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 01/13] io: Add qio_channel_wait_cond() helper
In reply to Peter XuQuoted 11 lines · Peter Xu
Add the helper to wait for QIO channel's IO availability in any context (coroutine, or non-coroutine). Use it tree-wide for three occurences.
Cc Daniel P. Berrangé <[email protected]>Signed-off-by Peter Xu <[email protected]>--- include/io/channel.h | 15 +++++++++++++++ io/channel.c | 21 +++++++++++---------- migration/qemu-file.c | 6 +----- 3 files changed, 27 insertions(+), 15 deletions(-)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH 2/13 migration: Properly wait on G_IO_IN when peeking messages
In reply to Peter Xumigration_channel_read_peek() used to do explicit waits of a short period when peeking message needs retry. Replace it with explicit polls on the io channel, exactly like what qemu_fill_buffer() does.
--- migration/channel.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
migration/channel.c +1 -6
@@ -135,12 +135,7 @@ int migration_channel_read_peek(QIOChannel *ioc,break ; } -/* 1ms sleep. */ -if (qemu_in_coroutine ()) { -qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,1000000 ); - }else { -g_usleep (1000 ); - } +qio_channel_wait_cond (ioc,G_IO_IN ); }return 0 ;
Signature · 1 line
2.50.1
Re: [PATCH 02/13] migration: Properly wait on G_IO_IN when peeking messages
Quoted 5 lines · Peter Xu
migration_channel_read_peek()used to do explicit waits of a short period when peeking message needs retry. Replace it with explicit polls on the io channel, exactly like whatqemu_fill_buffer()does.Signed-off-by Peter Xu<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 02/13] migration: Properly wait on G_IO_IN when peeking messages
In reply to Peter XuQuoted 8 lines · Peter Xu
migration_channel_read_peek()used to do explicit waits of a short period when peeking message needs retry. Replace it with explicit polls on the io channel, exactly like whatqemu_fill_buffer()does.Signed-off-by Peter Xu <[email protected]>--- migration/channel.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 02/13] migration: Properly wait on G_IO_IN when peeking messages
Quoted 11 lines · Daniel P. Berrangé
Quoted 8 lines · Peter Xu
migration_channel_read_peek()used to do explicit waits of a short period when peeking message needs retry. Replace it with explicit polls on the io channel, exactly like whatqemu_fill_buffer()does.Signed-off-by Peter Xu <[email protected]>--- migration/channel.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)Reviewed-by Daniel P. Berrangé <[email protected]>
Thanks both for the fast reviews. I queued patches 1-2 in advance for 10.2.
Signature · 1 line
Peter Xu
PATCH 3/13 migration/rdma: Fix wrong context in qio_channel_rdma_shutdown()
In reply to Peter XuThe rdmaout should be a cache of rioc->rdmaout, not rioc->rdmain.
--- migration/rdma.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
migration/rdma.c +1 -1
@@ -2986,7 +2986,7 @@ qio_channel_rdma_shutdown(QIOChannel *ioc,RCU_READ_LOCK_GUARD (); rdmain =qatomic_rcu_read (&rioc->rdmain); - rdmaout =qatomic_rcu_read (&rioc->rdmain); + rdmaout =qatomic_rcu_read (&rioc->rdmaout);switch (how) {case QIO_CHANNEL_SHUTDOWN_READ :
Signature · 1 line
2.50.1
PATCH 4/13 migration/rdma: Allow qemu_rdma_wait_comp_channel work with thread
In reply to Peter XuIt's almost there, except that currently it relies on a global flag showing that it's in incoming migration.
Change it to detect coroutine instead.
Then we achieved two things in one shot:
- Drop migration_started_on_destination, which is not needed anymore, and
- It starts to work in a thread when loadvm using RDMA
--- migration/rdma.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
migration/rdma.c +1 -14
@@ -357,13 +357,6 @@ typedef struct RDMAContext {/* Index of the next RAMBlock received during block registration */ unsigned int next_src_index; -/* -* Migration on *destination* started. -* Then use coroutine yield function. -* Source runs in a thread, so we don't care. -*/ -int migration_started_on_destination; -int total_registrations;int total_writes;
@@ -1352,12 +1345,7 @@ static int qemu_rdma_wait_comp_channel(RDMAContext *rdma, {struct rdma_cm_event *cm_event; -/* -* Coroutine doesn't start until migration_fd_process_incoming() -* so don't yield unless we know we're running inside of a coroutine. -*/ -if (rdma->migration_started_on_destination && -migration_incoming_get_current ()->state ==MIGRATION_STATUS_ACTIVE ) { +if (qemu_in_coroutine ()) {yield_until_fd_readable (comp_channel->fd); }else {/* This is the source side, we're in a separate thread
@@ -3884,7 +3872,6 @@ static void rdma_accept_incoming_migration(void *opaque)return ; } - rdma->migration_started_on_destination =1 ;migration_fd_process_incoming (f); }
Signature · 1 line
2.50.1
Re: [PATCH 04/13] migration/rdma: Allow qemu_rdma_wait_comp_channel work with thread
Quoted 11 lines · Peter Xu
It's almost there, except that currently it relies on a global flag showing that it's in incoming migration.
Change it to detect coroutine instead.
Then we achieved two things in one shot:
- Drop
migration_started_on_destination, which is not needed anymore, and
- It starts to work in a thread when loadvm using RDMASigned-off-by Peter Xu<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 04/13] migration/rdma: Allow qemu_rdma_wait_comp_channel work with thread
In reply to Peter XuQuoted 11 lines · Peter Xu
It's almost there, except that currently it relies on a global flag showing that it's in incoming migration.
Change it to detect coroutine instead.
Then we achieved two things in one shot:
- Drop
migration_started_on_destination, which is not needed anymore, and
- It starts to work in a thread when loadvm using RDMASigned-off-by Peter Xu <[email protected]>
Quoted 44 lines
--- migration/rdma.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)migration/rdma.c +1
-14@@ -357,13 +357,6 @@ typedef struct RDMAContext {/* Index of the next RAMBlock received during block registration */ unsigned int next_src_index; -/* -* Migration on *destination* started. -* Then use coroutine yield function. -* Source runs in a thread, so we don't care. -*/ -int migration_started_on_destination; -int total_registrations;int total_writes;@@ -1352,12 +1345,7 @@ static int qemu_rdma_wait_comp_channel(RDMAContext *rdma, {struct rdma_cm_event *cm_event; -/* -* Coroutine doesn't start until migration_fd_process_incoming() -* so don't yield unless we know we're running inside of a coroutine. -*/ -if (rdma->migration_started_on_destination && -migration_incoming_get_current ()->state ==MIGRATION_STATUS_ACTIVE ) { +if (qemu_in_coroutine ()) {yield_until_fd_readable (comp_channel->fd); }else {/* This is the source side, we're in a separate thread @@ -3884,7 +3872,6 @@ static void rdma_accept_incoming_migration(void *opaque)return ; } - rdma->migration_started_on_destination =1 ;migration_fd_process_incoming (f); }
PATCH 5/13 migration/rdma: Change io_create_watch() to return immediately
In reply to Peter XuThe old RDMA's io_create_watch() isn't really doing much work anyway. For G_IO_OUT, it already does return immediately. For G_IO_IN, it will try to detect some RDMA context length however normally nobody will be able to set it at all.
Simplify the code so that RDMA iochannels simply always rely on synchronous reads and writes. It is highly likely what 6ddd2d76ca6f86f was talking about, that the async model isn't really working well.
To be eplicit, incoming migration should always have marked the iochannel to be nonblocking. For non-RDMA channels, what happens with current master branch is when we have nothing to read, QEMU yields the coroutine at qemu_fill_buffer(). For RDMA, what I see is it always polls on its own and it yields at qemu_rdma_wait_comp_channel(). A sample stack:
#0 qemu_coroutine_yield #1 0x0000562e46e51f77 in yield_until_fd_readable #2 0x0000562e46927823 in qemu_rdma_wait_comp_channel #3 0x0000562e46927b35 in qemu_rdma_block_for_wrid #4 0x0000562e46927e6f in qemu_rdma_post_send_control #5 0x0000562e4692857f in qemu_rdma_exchange_recv #6 0x0000562e4692ab5e in qio_channel_rdma_readv #7 0x0000562e46c1f2d7 in qio_channel_readv_full #8 0x0000562e46c13a6e in qemu_fill_buffer #9 0x0000562e46c14ba8 in qemu_peek_byte
#10 0x0000562e46c14c09 in qemu_get_byte
#11 0x0000562e46c14e2a in qemu_get_be32
#12 0x0000562e46c14e8a in qemu_get_be64
#13 0x0000562e46913f08 in ram_load_precopy
#14 0x0000562e46914448 in ram_load
#15 0x0000562e469186e3 in vmstate_load
#16 0x0000562e4691ce6d in qemu_loadvm_section_part_end #17 0x0000562e4691d99b in qemu_loadvm_state_main #18 0x0000562e4691db87 in qemu_loadvm_state #19 0x0000562e468f2e87 in process_incoming_migration_co
This patch may or may not help in reality, the whole IO watch may or may not be working at all for RDMA iochannels. In all cases, this patch makes sure above will be the only place that RDMA can poll on IOs.
--- migration/rdma.c | 69 +++--------------------------------------------- 1 file changed, 3 insertions(+), 66 deletions(-)
migration/rdma.c +3 -66
@@ -2776,56 +2776,14 @@ static gbooleanqio_channel_rdma_source_prepare (GSource *source, gint *timeout) { -QIOChannelRDMASource *rsource = (QIOChannelRDMASource *)source; -RDMAContext *rdma; -GIOCondition cond =0 ; *timeout =-1 ; - -RCU_READ_LOCK_GUARD (); -if (rsource->condition ==G_IO_IN ) { - rdma =qatomic_rcu_read (&rsource->rioc->rdmain); - }else { - rdma =qatomic_rcu_read (&rsource->rioc->rdmaout); - } - -if (!rdma) { -error_report ("RDMAContext is NULL when prepare Gsource" ); -return FALSE; - } - -if (rdma->wr_data[0 ].control_len) { - cond |=G_IO_IN ; - } - cond |=G_IO_OUT ; - -return cond & rsource->condition; +return TRUE; }static gboolean qio_channel_rdma_source_check (GSource *source) { -QIOChannelRDMASource *rsource = (QIOChannelRDMASource *)source; -RDMAContext *rdma; -GIOCondition cond =0 ; - -RCU_READ_LOCK_GUARD (); -if (rsource->condition ==G_IO_IN ) { - rdma =qatomic_rcu_read (&rsource->rioc->rdmain); - }else { - rdma =qatomic_rcu_read (&rsource->rioc->rdmaout); - } - -if (!rdma) { -error_report ("RDMAContext is NULL when check Gsource" ); -return FALSE; - } - -if (rdma->wr_data[0 ].control_len) { - cond |=G_IO_IN ; - } - cond |=G_IO_OUT ; - -return cond & rsource->condition; +return TRUE; } static gboolean
@@ -2835,29 +2793,8 @@ qio_channel_rdma_source_dispatch(GSource *source, {QIOChannelFunc func = (QIOChannelFunc )callback;QIOChannelRDMASource *rsource = (QIOChannelRDMASource *)source; -RDMAContext *rdma; -GIOCondition cond =0 ; - -RCU_READ_LOCK_GUARD (); -if (rsource->condition ==G_IO_IN ) { - rdma =qatomic_rcu_read (&rsource->rioc->rdmain); - }else { - rdma =qatomic_rcu_read (&rsource->rioc->rdmaout); - } - -if (!rdma) { -error_report ("RDMAContext is NULL when dispatch Gsource" ); -return FALSE; - } - -if (rdma->wr_data[0 ].control_len) { - cond |=G_IO_IN ; - } - cond |=G_IO_OUT ; -return (*func)(QIO_CHANNEL (rsource->rioc), - (cond & rsource->condition), - user_data); +return (*func)(QIO_CHANNEL (rsource->rioc), rsource->condition, user_data); } static void
Signature · 1 line
2.50.1
Re: [PATCH 05/13] migration/rdma: Change io_create_watch() to return immediately
Quoted 10 lines · Peter Xu
The old RDMA's
io_create_watch()isn't really doing much work anyway. ForG_IO_OUT, it already does return immediately. ForG_IO_IN, it will try to detect some RDMA context length however normally nobody will be able to set it at all.Simplify the code so that RDMA iochannels simply always rely on synchronous reads and writes. It is highly likely what
6ddd2d76ca6f86fwas talking about, that the async model isn't really working well.To be eplicit, incoming migration should always have marked the iochannel
s/eplicit/explicit ?
Quoted 26 lines
to be nonblocking. For non-RDMA channels, what happens with current master branch is when we have nothing to read, QEMU yields the coroutine at
qemu_fill_buffer(). For RDMA, what I see is it always polls on its own and it yields atqemu_rdma_wait_comp_channel(). A sample stack:#0 qemu_coroutine_yield #1 0x0000562e46e51f77 in yield_until_fd_readable #2 0x0000562e46927823 in qemu_rdma_wait_comp_channel #3 0x0000562e46927b35 in qemu_rdma_block_for_wrid #4 0x0000562e46927e6f in qemu_rdma_post_send_control #5 0x0000562e4692857f in qemu_rdma_exchange_recv #6 0x0000562e4692ab5e in qio_channel_rdma_readv #7 0x0000562e46c1f2d7 in qio_channel_readv_full #8 0x0000562e46c13a6e in qemu_fill_buffer #9 0x0000562e46c14ba8 in qemu_peek_byte#10
0x0000562e46c14c09inqemu_get_byte
#110x0000562e46c14e2ainqemu_get_be32
#120x0000562e46c14e8ainqemu_get_be64
#130x0000562e46913f08inram_load_precopy
#140x0000562e46914448inram_load
#150x0000562e469186e3invmstate_load
#160x0000562e4691ce6dinqemu_loadvm_section_part_end#170x0000562e4691d99binqemu_loadvm_state_main#180x0000562e4691db87inqemu_loadvm_state#190x0000562e468f2e87inprocess_incoming_migration_co
Quoted 3 lines
This patch may or may not help in reality, the whole IO watch may or may not be working at all for RDMA iochannels. In all cases, this patch makes sure above will be the only place that RDMA can poll on IOs.
make sense to me.
Quoted 101 lines
Tested-by Zhijian Li (Fujitsu) <[email protected]>Signed-off-by Peter Xu <[email protected]>--- migration/rdma.c | 69 +++--------------------------------------------- 1 file changed, 3 insertions(+), 66 deletions(-)migration/rdma.c +3
-66@@ -2776,56 +2776,14 @@ static gbooleanqio_channel_rdma_source_prepare (GSource *source, gint *timeout) { -QIOChannelRDMASource *rsource = (QIOChannelRDMASource *)source; -RDMAContext *rdma; -GIOCondition cond =0 ; *timeout =-1 ; - -RCU_READ_LOCK_GUARD (); -if (rsource->condition ==G_IO_IN ) { - rdma =qatomic_rcu_read (&rsource->rioc->rdmain); - }else { - rdma =qatomic_rcu_read (&rsource->rioc->rdmaout); - } - -if (!rdma) { -error_report ("RDMAContext is NULL when prepare Gsource" ); -return FALSE; - } - -if (rdma->wr_data[0 ].control_len) { - cond |=G_IO_IN ; - } - cond |=G_IO_OUT ; - -return cond & rsource->condition; +return TRUE; }static gboolean qio_channel_rdma_source_check (GSource *source) { -QIOChannelRDMASource *rsource = (QIOChannelRDMASource *)source; -RDMAContext *rdma; -GIOCondition cond =0 ; - -RCU_READ_LOCK_GUARD (); -if (rsource->condition ==G_IO_IN ) { - rdma =qatomic_rcu_read (&rsource->rioc->rdmain); - }else { - rdma =qatomic_rcu_read (&rsource->rioc->rdmaout); - } - -if (!rdma) { -error_report ("RDMAContext is NULL when check Gsource" ); -return FALSE; - } - -if (rdma->wr_data[0 ].control_len) { - cond |=G_IO_IN ; - } - cond |=G_IO_OUT ; - -return cond & rsource->condition; +return TRUE; } static gboolean@@ -2835,29 +2793,8 @@ qio_channel_rdma_source_dispatch(GSource *source, {QIOChannelFunc func = (QIOChannelFunc )callback;QIOChannelRDMASource *rsource = (QIOChannelRDMASource *)source; -RDMAContext *rdma; -GIOCondition cond =0 ; - -RCU_READ_LOCK_GUARD (); -if (rsource->condition ==G_IO_IN ) { - rdma =qatomic_rcu_read (&rsource->rioc->rdmain); - }else { - rdma =qatomic_rcu_read (&rsource->rioc->rdmaout); - } - -if (!rdma) { -error_report ("RDMAContext is NULL when dispatch Gsource" ); -return FALSE; - } - -if (rdma->wr_data[0 ].control_len) { - cond |=G_IO_IN ; - } - cond |=G_IO_OUT ; -return (*func)(QIO_CHANNEL (rsource->rioc), - (cond & rsource->condition), - user_data); +return (*func)(QIO_CHANNEL (rsource->rioc), rsource->condition, user_data); } static void
PATCH 6/13 migration: Introduce WITH_BQL_HELD() / WITH_BQL_RELEASED()
In reply to Peter XuIntroduce the helpers to conditionally take or release BQL for a process.
--- migration/migration.h | 95 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)
migration/migration.h +95 -0
@@ -20,6 +20,7 @@#include "qobject/json-writer.h" #include "qemu/thread.h" #include "qemu/coroutine.h" +#include "qemu/main-loop.h" #include "io/channel.h" #include "io/channel-buffer.h" #include "net/announce.h"
@@ -42,6 +43,100 @@#define MIGRATION_THREAD_DST_LISTEN "mig/dst/listen"#define MIGRATION_THREAD_DST_PREEMPT "mig/dst/preempt" +struct WithBqlHeldAuto ; +typedef struct WithBqlHeldAuto WithBqlHeldAuto ; + +static inline WithBqlHeldAuto * +with_bql_held_auto_lock (bool bql_held,const char *file,int line) +{ +assert (bql_held ==bql_locked ()); +if (!bql_held) { +bql_lock_impl (file, line); +return (WithBqlHeldAuto *)1 ; + } +return (WithBqlHeldAuto *)2 ; +} + +static inline void +with_bql_held_auto_unlock (WithBqlHeldAuto *v) +{ +assert (bql_locked ()); +if (v == (WithBqlHeldAuto *)1 ) { +bql_unlock (); + } +} + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (WithBqlHeldAuto, with_bql_held_auto_unlock); + +#define _WITH_BQL_HELD (bql_held, var) \ + for (g_autoptr(WithBqlHeldAuto) var = \ + with_bql_held_auto_lock(bql_held, __FILE__, __LINE__); \ + var; \ + with_bql_held_auto_unlock(var), var = NULL) + +/** +* WITH_BQL_HELD(): Run a block of code, making sure BQL is held +* @bql_held: Whether BQL is already held +* +* Example use case: +* +* WITH_BQL_HELD(bql_held) { +* // BQL is guaranteed to be held within this block. +* // If bql_held==false, bql will be released when the block finishes. +* } +*/ +#define WITH_BQL_HELD (bql_held) \ + _WITH_BQL_HELD(bql_held, glue(with_bql_held_var, __COUNTER__)) + +struct WithBqlReleaseAuto ; +typedef struct WithBqlReleaseAuto WithBqlReleaseAuto ; + +static inline WithBqlReleaseAuto * +with_bql_release_auto_unlock (bool bql_held) +{ +assert (bql_held ==bql_locked ()); +if (bql_held) { +bql_unlock (); +return (WithBqlReleaseAuto *)1 ; + } +return (WithBqlReleaseAuto *)2 ; +} + +static inline void +with_bql_release_auto_lock (WithBqlReleaseAuto *v) +{ +assert (!bql_locked ()); +if (v == (WithBqlReleaseAuto *)1 ) { +/* +* NOTE: cleanup function cannot take more than 1 argument. Keep +* it simple here by not passing __FILE__/__LINE__ from the caller. +*/ +bql_lock_impl (__FILE__, __LINE__); + } +} + +G_DEFINE_AUTOPTR_CLEANUP_FUNC (WithBqlReleaseAuto, with_bql_release_auto_lock); + +#define _WITH_BQL_RELEASED (bql_held, var) \ + for (g_autoptr(WithBqlReleaseAuto) var = \ + with_bql_release_auto_unlock(bql_held); \ + var; \ + with_bql_release_auto_lock(var), var = NULL) + +/** +* WITH_BQL_RELEASED(): Run a block of code, making sure BQL is release +* @bql_held: Whether BQL is already held +* +* Example use case: +* +* WITH_BQL_RELEASE(bql_held) { +* // BQL is guaranteed to be release within this block. +* // If bql_held==true, bql will be re-taken when the block finishes. +* } +*/ +#define WITH_BQL_RELEASED (bql_held) \ + _WITH_BQL_RELEASED(bql_held, glue(with_bql_release_var, __COUNTER__)) +struct PostcopyBlocktimeContext ;typedef struct ThreadPool ThreadPool ;
Signature · 1 line
2.50.1
Re: [PATCH 06/13] migration: Introduce WITH_BQL_HELD() / WITH_BQL_RELEASED()
Quoted 3 lines · Peter Xu
Introduce the helpers to conditionally take or release BQL for a process.
Signed-off-by Peter Xu<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH 7/13 migration: Pass in bql_held information from qemu_loadvm_state()
In reply to Peter XuTeach qemu_loadvm_state() and some of the internal functions to know whether we're holding BQL or not.
So far, all the callers still always pass in TRUE, hence no functional change expected. But it may change in the near future.
To reviewers: even if this is not functional change yet, it'll be the major core functional change after we switch to threadified loadvm soon. Please Treat it as one to add explicit code to mark out which part of incoming live migration would need to be executed always with the BQL, or would need to be run always without BQL.
--- migration/savevm.h | 4 +-- migration/colo.c | 2 +- migration/migration.c | 2 +- migration/savevm.c | 72 ++++++++++++++++++++++++++++++------------- 4 files changed, 55 insertions(+), 25 deletions(-)
migration/savevm.h +2 -2
@@ -64,10 +64,10 @@ void qemu_savevm_send_colo_enable(QEMUFile *f);void qemu_savevm_live_state (QEMUFile *f);int qemu_save_device_state (QEMUFile *f); -int qemu_loadvm_state (QEMUFile *f,Error **errp); +int qemu_loadvm_state (QEMUFile *f,bool bql_held,Error **errp);void qemu_loadvm_state_cleanup (MigrationIncomingState *mis);int qemu_loadvm_state_main (QEMUFile *f,MigrationIncomingState *mis, -Error **errp); +bool bql_held,Error **errp);int qemu_load_device_state (QEMUFile *f,Error **errp);int qemu_loadvm_approve_switchover (void ); int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,
migration/colo.c +1 -1
@@ -686,7 +686,7 @@ static void colo_incoming_process_checkpoint(MigrationIncomingState *mis,bql_lock ();cpu_synchronize_all_states (); - ret =qemu_loadvm_state_main (mis->from_src_file, mis, errp); + ret =qemu_loadvm_state_main (mis->from_src_file, mis, true, errp);bql_unlock ();if (ret <0 ) {
migration/migration.c +1 -1
@@ -878,7 +878,7 @@ process_incoming_migration_co(void *opaque) MIGRATION_STATUS_ACTIVE); mis->loadvm_co =qemu_coroutine_self (); - ret =qemu_loadvm_state (mis->from_src_file, &local_err); + ret =qemu_loadvm_state (mis->from_src_file, true, &local_err); mis->loadvm_co =NULL ;trace_vmstate_downtime_checkpoint ("dst-precopy-loadvm-completed" );
migration/savevm.c +51 -21
188 lines not on this page · raw message
Signature · 1 line
2.50.1
Re: [PATCH 07/13] migration: Pass in bql_held information from qemu_loadvm_state()
Quoted 2 lines · Peter Xu
Teach
qemu_loadvm_state()and some of the internal functions to know whether we're holding BQL or not.
Actually, this commit does more: not only pass the bql_held information, but also by introduce some WITH_BQL_HELD() sections.
IMHO it could be split:
1. only add bql_held parameters, which are used only to passthorough to called functions. That's just to simplify further commits. Make the information available. At this commit, we only need to check, that passed information is correct (is it really held, may be add some comments/assertions to make it obvious)
2. one or more commits, which prepares different functions to be called in thread: support bql_held=false by introducing WITH_BQL_HELD() sections. In such commit, we should lookthorgh the whole function and check that it actually prepared to be called from thread.
Hmm, or without [1.], there may be several commits to prepare different functions. Or maybe, even one commit as it is, but change commit subject and message somehow, to reflect all the changes..
Quoted 12 lines
So far, all the callers still always pass in TRUE, hence no functional change expected. But it may change in the near future.
To reviewers: even if this is not functional change yet, it'll be the major core functional change after we switch to threadified loadvm soon. Please Treat it as one to add explicit code to mark out which part of incoming live migration would need to be executed always with the BQL, or would need to be run always without BQL.
Signed-off-by Peter Xu <[email protected]>---
[..]
migration/colo.c static void colo_incoming_process_checkpoint(MigrationIncomingState *mis, · line 689
Quoted 10 lines
migration/colo.c +1
-1@@ -686,7 +686,7 @@ static void colo_incoming_process_checkpoint(MigrationIncomingState *mis,bql_lock ();cpu_synchronize_all_states (); - ret =qemu_loadvm_state_main (mis->from_src_file, mis, errp); + ret =qemu_loadvm_state_main (mis->from_src_file, mis, true, errp);
That one is obvious..
migration/migration.c process_incoming_migration_co(void *opaque) · line 881
Quoted 13 lines
migration/colo.c +0
-0bql_unlock ();if (ret <0 ) {migration/migration.c +1
-1@@ -878,7 +878,7 @@ process_incoming_migration_co(void *opaque) MIGRATION_STATUS_ACTIVE); mis->loadvm_co =qemu_coroutine_self (); - ret =qemu_loadvm_state (mis->from_src_file, &local_err); + ret =qemu_loadvm_state (mis->from_src_file, true, &local_err);
Here, why are we sure? Are coroutines triggered by QMP command always run under BQL?
Maybe, worth an assertion.
Quoted 13 lines
migration/migration.c +0
-0mis->loadvm_co =NULL ;trace_vmstate_downtime_checkpoint ("dst-precopy-loadvm-completed" );migration/savevm.c +1
-1@@ -154,11 +154,12 @@ static void qemu_loadvm_thread_pool_destroy(MigrationIncomingState *mis) } static bool qemu_loadvm_thread_pool_wait(MigrationState *s, -MigrationIncomingState *mis) + MigrationIncomingState *mis,
migration/savevm.c static void qemu_loadvm_thread_pool_destroy(MigrationIncomingState *mis) · line 164
Quoted 10 lines
migration/savevm.c +4
-3+ bool bql_held) { -bql_unlock ();/* Let load threads do work requiring BQL */ -thread_pool_wait (mis->load_threads); -bql_lock (); +WITH_BQL_RELEASED (bql_held) { +thread_pool_wait (mis->load_threads); + }return !migrate_has_error (s);
The function is now prepared to be called from thread, as migrate_has_error() has own mutex.
migration/savevm.c static void *postcopy_ram_listen_thread(void *opaque) · line 2121
Quoted 7 lines
migration/savevm.c +1
-1}@@ -2117,7 +2118,7 @@ static void *postcopy_ram_listen_thread(void *opaque)qemu_file_set_blocking (f, true, &error_fatal);/* TODO: sanity check that only postcopiable data will be loaded here */ - load_res =qemu_loadvm_state_main (f, mis, &local_err); + load_res =qemu_loadvm_state_main (f, mis, true, &local_err);
Is it correct? I see, a bit later, postcopy_ram_listen_thread() does
bql_lock();
migration_incoming_state_destroy();
bql_unlock();so, I assume, that before this, when we call qemu_loadvm_state_main(), BQL is not actually locked?
Quoted 12 lines
migration/savevm.c +2
-1/* * This is tricky, but, mis->from_src_file can change after it @@ -2420,7 +2421,8 @@ static void loadvm_postcopy_handle_resume(MigrationIncomingState *mis)* Returns: Negative values on error * */ -static int loadvm_handle_cmd_packaged (MigrationIncomingState *mis,Error **errp) +static int loadvm_handle_cmd_packaged (MigrationIncomingState *mis, +bool bql_held,Error **errp) {int ret;
[..]
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 07/13] migration: Pass in bql_held information from qemu_loadvm_state()
Quoted 21 lines · Vladimir Sementsov-Ogievskiy
Quoted 2 lines · Peter Xu
Teach
qemu_loadvm_state()and some of the internal functions to know whether we're holding BQL or not.Actually, this commit does more: not only pass the
bql_heldinformation, but also by introduce someWITH_BQL_HELD()sections.IMHO it could be split:
1. only add
bql_heldparameters, which are used only to passthorough to called functions. That's just to simplify further commits. Make the information available. At this commit, we only need to check, that passed information is correct (is it really held, may be add some comments/assertions to make it obvious)2. one or more commits, which prepares different functions to be called in thread: support bql_held=false by introducing
WITH_BQL_HELD()sections. In such commit, we should lookthorgh the whole function and check that it actually prepared to be called from thread.
Quoted 3 lines
Hmm, or without [1.], there may be several commits to prepare different functions. Or maybe, even one commit as it is, but change commit subject and message somehow, to reflect all the changes..
I prefer the last one you said.
This patch is indeed not easy to review, and it'll be the core of this whole work (it'll be even more obvious if without the complexity due to COLO and RDMA's coroutines.. that was separate from BQL status, hence addressed in the next patch).
So the big question is, for the loadvm process, what must run with BQL held, what must run with BQL released, and the rest will be "it's ok to run with BQL, but optional".
Those things, no matter when drafting as a patch, or review, should better happen in one patch, IMHO, otherwise it's unclear how each patch can be reviewed if something was missed, when details scattered into multiple patches.
I updated the commit message with below:
migration: Pass in bql_held information from qemu_loadvm_state()
Teach qemu_loadvm_state() and some of the internal functions to know whether we're holding BQL or not.
Notes for some callers that are not obvious on BQL status:
- process_incoming_migration_co() always invokes qemu_loadvm_state() with BQL held, which is the core loadvm coroutine. The coroutine was explicitly entered from migration_incoming_process() which used to run in the main thread.
- postcopy_listen_thread() always invokes qemu_loadvm_state() without BQL, which receives postcopy pages while VM is already started and running on destination (or to be started very soon).
- qemu_load_device_state() / qmp_xen_load_devices_state() / load_snapshot() all are invoked with BQL held.
The rest takes bql_held from upper layers.
To reviewers: even if this is not functional change yet, it'll be the major core functional change after we switch to threadified loadvm soon. Please treat it as one to add explicit code to mark out which part of incoming live migration would need to be executed always with the BQL, or would need to be run always without BQL.
Would it look better?
Quoted 28 lines
Quoted 13 lines
So far, all the callers still always pass in TRUE, hence no functional change expected. But it may change in the near future.
To reviewers: even if this is not functional change yet, it'll be the major core functional change after we switch to threadified loadvm soon. Please Treat it as one to add explicit code to mark out which part of incoming live migration would need to be executed always with the BQL, or would need to be run always without BQL.
Signed-off-by Peter Xu <[email protected]>---
[..]
migration/colo.c static void colo_incoming_process_checkpoint(MigrationIncomingState *mis, · line 688
Quoted 9 lines
migration/colo.c +1
-1@@ -686,7 +686,7 @@ static void colo_incoming_process_checkpoint(MigrationIncomingState *mis,bql_lock ();cpu_synchronize_all_states (); - ret =qemu_loadvm_state_main (mis->from_src_file, mis, errp); + ret =qemu_loadvm_state_main (mis->from_src_file, mis, true, errp);That one is obvious..
Quoted 13 lines
migration/migration.c process_incoming_migration_co(void *opaque) · line 880
Quoted 11 lines
migration/colo.c +0
-0bql_unlock ();if (ret <0 ) {migration/migration.c +1
-1@@ -878,7 +878,7 @@ process_incoming_migration_co(void *opaque) MIGRATION_STATUS_ACTIVE); mis->loadvm_co =qemu_coroutine_self (); - ret =qemu_loadvm_state (mis->from_src_file, &local_err); + ret =qemu_loadvm_state (mis->from_src_file, true, &local_err);Here, why are we sure? Are coroutines triggered by QMP command always run under BQL?
Yes. QMP handlers must run with BQL, except oob commands.
Quoted 2 lines
Maybe, worth an assertion.
Sure I can add it. I'll then switch that to assert(!bql) in the next patch when it becomes a thread.
Quoted 33 lines
Quoted 11 lines
migration/migration.c +0
-0mis->loadvm_co =NULL ;trace_vmstate_downtime_checkpoint ("dst-precopy-loadvm-completed" );migration/savevm.c +0
-1@@ -154,11 +154,12 @@ static void qemu_loadvm_thread_pool_destroy(MigrationIncomingState *mis) } static bool qemu_loadvm_thread_pool_wait(MigrationState *s, -MigrationIncomingState *mis)migration/savevm.c static void qemu_loadvm_thread_pool_destroy(MigrationIncomingState *mis) · line 162
Quoted 10 lines
migration/savevm.c +5
-3+MigrationIncomingState *mis, + bool bql_held) { -bql_unlock ();/* Let load threads do work requiring BQL */ -thread_pool_wait (mis->load_threads); -bql_lock (); + WITH_BQL_RELEASED(bql_held) { + thread_pool_wait(mis->load_threads); + }return !migrate_has_error (s);The function is now prepared to be called from thread, as
migrate_has_error()has own mutex.migration/savevm.c static void *postcopy_ram_listen_thread(void *opaque) · line 2120
Quoted 6 lines
migration/savevm.c +1
-1}@@ -2117,7 +2118,7 @@ static void *postcopy_ram_listen_thread(void *opaque)qemu_file_set_blocking (f, true, &error_fatal);/* TODO: sanity check that only postcopiable data will be loaded here */ - load_res =qemu_loadvm_state_main (f, mis, &local_err); + load_res =qemu_loadvm_state_main (f, mis, true, &local_err);Is it correct? I see, a bit later,
postcopy_ram_listen_thread()does
Quoted 5 lines
bql_lock(); migration_incoming_state_destroy(); bql_unlock();so, I assume, that before this, when we call
qemu_loadvm_state_main(), BQL is not actually locked?
Thanks for the careful review, this line was wrong, and it was reverted to the right change in the next patch.. hence after the whole patchset applied it'll be correct.
I guess it might be a fixup I applied to the wrong patch. I'll move that change here. It should be false here indeed.
Quoted 19 lines
Quoted 12 lines
migration/savevm.c +2
-1/* * This is tricky, but, mis->from_src_file can change after it @@ -2420,7 +2421,8 @@ static void loadvm_postcopy_handle_resume(MigrationIncomingState *mis)* Returns: Negative values on error * */ -static int loadvm_handle_cmd_packaged (MigrationIncomingState *mis,Error **errp) +static int loadvm_handle_cmd_packaged (MigrationIncomingState *mis, +bool bql_held,Error **errp) {int ret;[..]
Signature · 2 lines
Best regards, Vladimir
Signature · 1 line
Peter Xu
PATCH 8/13 migration: Thread-ify precopy vmstate load process
In reply to Peter XuMigration module was there for 10+ years. Initially, it was in most cases based on coroutines. As more features were added into the framework, like postcopy, multifd, etc.. it became a mixture of threads and coroutines.
I'm guessing coroutines just can't fix all issues that migration want to resolve.
After all these years, migration is now heavily based on a threaded model.
Now there's still a major part of migration framework that is still not thread-based, which is precopy load. We do load in a separate thread in postcopy since the 1st day postcopy was introduced, however that requires a separate state transition from precopy loading all devices first, which still happens in the main thread of a coroutine.
This patch tries to move the migration incoming side to be run inside a separate thread (mig/dst/main) just like the src (mig/src/main). The entrance to be migration_incoming_thread().
Quite a few things are needed to make it fly.. One note here is we need to change all these things in one patch to not break anything. The other way to do this is add code to make all paths (that this patch touched) be ready for either coroutine or thread. That may cause confusions in another way. So reviewers, please take my sincere apology on the hardness of reviewing this patch: it covers a few modules at the same time, and with some risky changes.
BQL Analysis
============
Firstly, when moving it over to the thread, it means the thread cannot take BQL during the whole process of loading anymore, because otherwise it can block main thread from using the BQL for all kinds of other concurrent tasks (for example, processing QMP / HMP commands).
Here the first question to ask is: what needs BQL during precopy load, and what doesn't?
Most of the load process shouldn't need BQL, especially when it's about RAM. After all, RAM is still the major chunk of data to move for a live migration process. VFIO started to change that, though, but still, VFIO is per-device so that shouldn't need BQL either in most cases.
Generic device loads will need BQL, likely not when receiving VMSDs, but when applying them. One example is any post_load() could potentially inject memory regions causing memory transactions to happen. That'll need to update the global address spaces, hence requires BQL. The other one is CPU sync operations, even if the sync alone may not need BQL (which is still to be further justified), run_on_cpu() will need it.
For that, qemu_loadvm_state() and qemu_loadvm_state_main() functions need to now take a "bql_held" parameter saying whether bql is held. We could use things like BQL_LOCK_GUARD(), but this patch goes with explicit lockings rather than relying on bql_locked TLS variable. In case of migration, we always know whether BQL is held in different context as long as we can still pass that information downwards.
COLO
====
COLO assumed the dest VM load happens in a coroutine. After this patch, it's not anymore. Change that by invoking colo_incoming_co() directly from the migration_incoming_thread().
The name (colo_incoming_co()) isn't proper anymore. Change it to colo_incoming_wait(), removing the coroutine annotation alongside.
Remove all the bql_lock() implications in COLO, e.g., colo_incoming_co() used to release the lock for a short period while join(). Now it's not needed. Instead, taking BQL but only when needed (colo_release_ram_cache).
At the meantime, there's colo_incoming_co variable that used to store the COLO incoming coroutine, only to be kicked off when a secondary failover happens.
To recap, what should happen for such failover should be (taking example of a QMP command x-colo-lost-heartbeat triggering on dest QEMU):
- The QMP command will kick off both the coroutine and the COLO
thread (colo_process_incoming_thread()), with something like:
/* Notify COLO incoming thread that failover work is finished */
qemu_event_set(&mis->colo_incoming_event);
qemu_coroutine_enter(mis->colo_incoming_co); - The coroutine, which yielded itself before, now resumes after enter(),
then it'll wait for the join():
mis->colo_incoming_co = qemu_coroutine_self();
qemu_coroutine_yield();
mis->colo_incoming_co = NULL;
/* Wait checkpoint incoming thread exit before free resource */
qemu_thread_join(&th);Here, when switching to a thread model, it should be fine removing colo_incoming_co variable completely, because if so, the incoming thread will (instead of yielding the coroutine) wait at qemu_thread_join() until the colo thread completes execution (after receiving colo_incoming_event).
RDMA
====
With the prior patch making sure io_watch won't block for RDMA iochannels, RDMA threads should only block at its io_readv/io_writev functions. When a disconnection is detected (as in rdma_cm_poll_handler()), the update to "errored" field will be immediately reflected in the migration incoming thread. Hence the coroutine for RDMA is not needed anymore to kick the thread out.
When the thread is available, we also can't have rdma_cm_poll_handler() keep polling the fd and operate on it in the main thread. Drop it completely, and it should be fine because qemu_rdma_wait_comp_channel() should also be monitoring it.
This almost reverts commit 923709896b1b01fb982c93492ad01b233e6b6023.
We need to do this change in this same patch that we introduce the thread, unfortunately, otherwise we can have a risk of racing.
TODO
====
Currently the BQL is taken during loading of a START|FULL section. When the IO hangs (e.g. network issue) during this process, it could potentially block others like the monitor servers. One solution is breaking BQL to smaller granule and leave IOs to be always BQL-free. That'll need more justifications.
For example, there are at least four things that need some closer attention:
- SaveVMHandlers's load_state(): this likely DO NOT need BQL, but we need to justify all of them (not to mention, some of them look like prone to be rewritten as VMSDs..)
- VMSD's pre_load(): in most cases, this DO NOT really need BQL, but sometimes maybe it will! Double checking on this will be needed.
- VMSD's post_load(): in many cases, this DO need BQL, for example on address space operations. Likely we should just take it for any post_load().
- VMSD field's get(): this is tricky! It could internally be anything even if it was only a field. E.g. there can be users to use a SINGLE field to load a whole VMSD, which can further introduce more possibilities.
In general, QEMUFile IOs should not need BQL, that is when receiving the VMSD data and waiting for e.g. the socket buffer to get refilled. But that's the easy part.
--- include/migration/colo.h | 6 ++-- migration/migration.h | 14 +++----- migration/colo-stubs.c | 2 +- migration/colo.c | 24 ++++--------- migration/migration.c | 77 +++++++++++++++++++++++++--------------- migration/rdma.c | 34 +----------------- migration/savevm.c | 8 ++--- migration/trace-events | 4 +-- 8 files changed, 69 insertions(+), 100 deletions(-)
include/migration/colo.h +2 -4
@@ -44,12 +44,10 @@ void colo_do_failover(void);void colo_checkpoint_delay_set (void );/* -* Starts COLO incoming process. Called from process_incoming_migration_co() +* Starts COLO incoming process. Called from migration_incoming_thread() * after loading the state. -* -* Called with BQL locked, may temporary release BQL. */ -void coroutine_fn colo_incoming_co(void); +void colo_incoming_wait (void );void colo_shutdown (void );#endif
migration/migration.h +5 -9
@@ -214,6 +214,10 @@ struct MigrationIncomingState {bool have_listen_thread;QemuThread listen_thread; +/* Migration main recv thread */ +bool have_recv_thread; +QemuThread recv_thread; +/* For the kernel to send us notifications */ int userfault_fd;/* To notify the fault_thread to wake, e.g., when need to quit */
@@ -272,15 +276,7 @@ struct MigrationIncomingState {MigrationStatus state; -/* -* The incoming migration coroutine, non-NULL during qemu_loadvm_state(). -* Used to wake the migration incoming coroutine from rdma code. How much is -* it safe - it's a question. -*/ -Coroutine *loadvm_co; - -/* The coroutine we should enter (back) after failover */ -Coroutine *colo_incoming_co; +/* Notify secondary VM to move on */ QemuEvent colo_incoming_event;/* Optional load threads pool and its thread exit request flag */
migration/colo-stubs.c +1 -1
@@ -9,7 +9,7 @@ void colo_shutdown(void) { } -void coroutine_fn colo_incoming_co(void) +void colo_incoming_wait (void ) { }
migration/colo.c +6 -18
@@ -147,11 +147,6 @@ static void secondary_vm_do_failover(void) }/* Notify COLO incoming thread that failover work is finished */ qemu_event_set (&mis->colo_incoming_event); - -/* For Secondary VM, jump to incoming co */ -if (mis->colo_incoming_co) { -qemu_coroutine_enter (mis->colo_incoming_co); - } }static void primary_vm_do_failover (void )
@@ -848,10 +843,8 @@ static void *colo_process_incoming_thread(void *opaque) mis->to_src_file =qemu_file_get_return_path (mis->from_src_file);/* -* Note: the communication between Primary side and Secondary side -* should be sequential, we set the fd to unblocked in migration incoming -* coroutine, and here we are in the COLO incoming thread, so it is ok to -* set the fd back to blocked. +* Here we are in the COLO incoming thread, so it is ok to set the fd +* to blocking. */ if (!qemu_file_set_blocking (mis->from_src_file, true, &local_err)) {error_report_err (local_err);
@@ -927,27 +920,22 @@ out:return NULL ; } -void coroutine_fn colo_incoming_co(void) +/* Wait for failover */ +void colo_incoming_wait (void ) {MigrationIncomingState *mis =migration_incoming_get_current ();QemuThread th; -assert (bql_locked ());assert (migration_incoming_colo_enabled ());qemu_thread_create (&th,MIGRATION_THREAD_DST_COLO , colo_process_incoming_thread, mis,QEMU_THREAD_JOINABLE ); - mis->colo_incoming_co =qemu_coroutine_self (); -qemu_coroutine_yield (); - mis->colo_incoming_co =NULL ; - -bql_unlock ();/* Wait checkpoint incoming thread exit before free resource */ qemu_thread_join (&th); -bql_lock (); -/* We hold the global BQL, so it is safe here */ +bql_lock ();colo_release_ram_cache (); +bql_unlock (); }
migration/migration.c +48 -29
135 lines not on this page · raw message
migration/rdma.c +1 -33
@@ -3051,37 +3051,6 @@ int rdma_control_save_page(QEMUFile *f, ram_addr_t block_offset,static void rdma_accept_incoming_migration (void *opaque); -static void rdma_cm_poll_handler (void *opaque) -{ -RDMAContext *rdma = opaque; -struct rdma_cm_event *cm_event; -MigrationIncomingState *mis =migration_incoming_get_current (); - -if (rdma_get_cm_event (rdma->channel, &cm_event) <0 ) { -error_report ("get_cm_event failed %d" , errno); -return ; - } - -if (cm_event->event ==RDMA_CM_EVENT_DISCONNECTED || - cm_event->event ==RDMA_CM_EVENT_DEVICE_REMOVAL ) { -if (!rdma->errored && -migration_incoming_get_current ()->state != -MIGRATION_STATUS_COMPLETED ) { -error_report ("receive cm event, cm event is %d" , cm_event->event); - rdma->errored = true; -if (rdma->return_path) { - rdma->return_path->errored = true; - } - } -rdma_ack_cm_event (cm_event); -if (mis->loadvm_co) { -qemu_coroutine_enter (mis->loadvm_co); - } -return ; - } -rdma_ack_cm_event (cm_event); -} -static int qemu_rdma_accept (RDMAContext *rdma) {Error *err =NULL ;
@@ -3199,8 +3168,7 @@ static int qemu_rdma_accept(RDMAContext *rdma)NULL , (void *)(intptr_t)rdma->return_path); }else { -qemu_set_fd_handler (rdma->channel->fd, rdma_cm_poll_handler, -NULL , rdma); +qemu_set_fd_handler (rdma->channel->fd,NULL ,NULL ,NULL ); } ret =rdma_accept (rdma->cm_id, &conn_param);
migration/savevm.c +4 -4
@@ -2118,7 +2118,7 @@ static void *postcopy_ram_listen_thread(void *opaque)qemu_file_set_blocking (f, true, &error_fatal);/* TODO: sanity check that only postcopiable data will be loaded here */ - load_res =qemu_loadvm_state_main (f, mis, true, &local_err); + load_res =qemu_loadvm_state_main (f, mis, false, &local_err);/* * This is tricky, but, mis->from_src_file can change after it
@@ -2415,11 +2415,11 @@ static void loadvm_postcopy_handle_resume(MigrationIncomingState *mis)* Immediately following this command is a blob of data containing an embedded * chunk of migration stream; read it and load it. * -* @mis: Incoming state -* @length: Length of packaged data to read +* @mis: Incoming state +* @bql_held: Whether BQL is held already +* @errp: The Error** to set when returning failures. * * Returns: Negative values on error -* */ static int loadvm_handle_cmd_packaged (MigrationIncomingState *mis,bool bql_held,Error **errp)
migration/trace-events +2 -2
@@ -193,8 +193,8 @@ source_return_path_thread_resume_ack(uint32_t v) "%"PRIu32 source_return_path_thread_switchover_acked(void) "" migration_thread_low_pending(uint64_t pending) "%" PRIu64 migrate_transferred(uint64_t transferred, uint64_t time_spent, uint64_t bandwidth, uint64_t avail_bw, uint64_t size) "transferred %" PRIu64 " time_spent %" PRIu64 " bandwidth %" PRIu64 " switchover_bw %" PRIu64 " max_size %" PRId64 -process_incoming_migration_co_end(int ret, int ps) "ret=%d postcopy-state=%d" -process_incoming_migration_co_postcopy_end_main(void) "" +process_incoming_migration_end(int ret, int ps) "ret=%d postcopy-state=%d" +process_incoming_migration_postcopy_end_main(void) "" postcopy_preempt_enabled(bool value) "%d" migration_precopy_complete(void) ""
Signature · 1 line
2.50.1
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 149 lines · Peter Xu
Migration module was there for 10+ years. Initially, it was in most cases based on coroutines. As more features were added into the framework, like postcopy, multifd, etc.. it became a mixture of threads and coroutines.
I'm guessing coroutines just can't fix all issues that migration want to resolve.
After all these years, migration is now heavily based on a threaded model.
Now there's still a major part of migration framework that is still not thread-based, which is precopy load. We do load in a separate thread in postcopy since the 1st day postcopy was introduced, however that requires a separate state transition from precopy loading all devices first, which still happens in the main thread of a coroutine.
This patch tries to move the migration incoming side to be run inside a separate thread (mig/dst/main) just like the src (mig/src/main). The entrance to be
migration_incoming_thread().Quite a few things are needed to make it fly.. One note here is we need to change all these things in one patch to not break anything. The other way to do this is add code to make all paths (that this patch touched) be ready for either coroutine or thread. That may cause confusions in another way. So reviewers, please take my sincere apology on the hardness of reviewing this patch: it covers a few modules at the same time, and with some risky changes.
BQL Analysis
============Firstly, when moving it over to the thread, it means the thread cannot take BQL during the whole process of loading anymore, because otherwise it can block main thread from using the BQL for all kinds of other concurrent tasks (for example, processing QMP / HMP commands).
Here the first question to ask is: what needs BQL during precopy load, and what doesn't?
Most of the load process shouldn't need BQL, especially when it's about RAM. After all, RAM is still the major chunk of data to move for a live migration process. VFIO started to change that, though, but still, VFIO is per-device so that shouldn't need BQL either in most cases.
Generic device loads will need BQL, likely not when receiving VMSDs, but when applying them. One example is any
post_load()could potentially inject memory regions causing memory transactions to happen. That'll need to update the global address spaces, hence requires BQL. The other one is CPU sync operations, even if the sync alone may not need BQL (which is still to be further justified),run_on_cpu()will need it.For that,
qemu_loadvm_state()andqemu_loadvm_state_main()functions need to now take a "bql_held" parameter saying whether bql is held. We could use things likeBQL_LOCK_GUARD(), but this patch goes with explicit lockings rather than relying onbql_lockedTLS variable. In case of migration, we always know whether BQL is held in different context as long as we can still pass that information downwards.COLO
====COLO assumed the dest VM load happens in a coroutine. After this patch, it's not anymore. Change that by invoking
colo_incoming_co()directly from themigration_incoming_thread().The name (
colo_incoming_co()) isn't proper anymore. Change it tocolo_incoming_wait(), removing the coroutine annotation alongside.Remove all the
bql_lock()implications in COLO, e.g.,colo_incoming_co()used to release the lock for a short period whilejoin(). Now it's not needed. Instead, taking BQL but only when needed (colo_release_ram_cache).At the meantime, there's
colo_incoming_covariable that used to store the COLO incoming coroutine, only to be kicked off when a secondary failover happens.To recap, what should happen for such failover should be (taking example of a QMP command x-colo-lost-heartbeat triggering on dest QEMU):
- The QMP command will kick off both the coroutine and the COLO thread (colo_process_incoming_thread()), with something like: /* Notify COLO incoming thread that failover work is finished */ qemu_event_set(&mis->colo_incoming_event); qemu_coroutine_enter(mis->colo_incoming_co); - The coroutine, which yielded itself before, now resumes after enter(), then it'll wait for the join(): mis->colo_incoming_co = qemu_coroutine_self(); qemu_coroutine_yield(); mis->colo_incoming_co = NULL; /* Wait checkpoint incoming thread exit before free resource */ qemu_thread_join(&th);Here, when switching to a thread model, it should be fine removing
colo_incoming_covariable completely, because if so, the incoming thread will (instead of yielding the coroutine) wait atqemu_thread_join()until the colo thread completes execution (after receivingcolo_incoming_event).RDMA
====With the prior patch making sure
io_watchwon't block for RDMA iochannels, RDMA threads should only block at its io_readv/io_writev functions. When a disconnection is detected (as inrdma_cm_poll_handler()), the update to "errored" field will be immediately reflected in the migration incoming thread. Hence the coroutine for RDMA is not needed anymore to kick the thread out.When the thread is available, we also can't have
rdma_cm_poll_handler()keep polling the fd and operate on it in the main thread. Drop it completely, and it should be fine becauseqemu_rdma_wait_comp_channel()should also be monitoring it.This almost reverts commit
923709896b1b01fb982c93492ad01b233e6b6023.We need to do this change in this same patch that we introduce the thread, unfortunately, otherwise we can have a risk of racing.
TODO
====Currently the BQL is taken during loading of a START|FULL section. When the IO hangs (e.g. network issue) during this process, it could potentially block others like the monitor servers. One solution is breaking BQL to smaller granule and leave IOs to be always BQL-free. That'll need more justifications.
For example, there are at least four things that need some closer attention:
- SaveVMHandlers's
load_state(): this likely DO NOT need BQL, but we need to justify all of them (not to mention, some of them look like prone to be rewritten as VMSDs..)- VMSD's
pre_load(): in most cases, this DO NOT really need BQL, but sometimes maybe it will! Double checking on this will be needed.- VMSD's
post_load(): in many cases, this DO need BQL, for example on address space operations. Likely we should just take it for anypost_load().- VMSD field's
get(): this is tricky! It could internally be anything even if it was only a field. E.g. there can be users to use a SINGLE field to load a whole VMSD, which can further introduce more possibilities.
Quoted 5 lines
In general, QEMUFile IOs should not need BQL, that is when receiving the VMSD data and waiting for e.g. the socket buffer to get refilled. But that's the easy part.
Signed-off-by Peter Xu <[email protected]>
Both COLO and RDMA changes look good to me
And with an addition fixes[1] for COLO, the whole patchset:
[1] https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 376 lines
--- include/migration/colo.h | 6 ++-- migration/migration.h | 14 +++----- migration/colo-stubs.c | 2 +- migration/colo.c | 24 ++++--------- migration/migration.c | 77 +++++++++++++++++++++++++--------------- migration/rdma.c | 34 +----------------- migration/savevm.c | 8 ++--- migration/trace-events | 4 +-- 8 files changed, 69 insertions(+), 100 deletions(-)include/migration/colo.h +2
-4@@ -44,12 +44,10 @@ void colo_do_failover(void);void colo_checkpoint_delay_set (void );/* -* Starts COLO incoming process. Called from process_incoming_migration_co() +* Starts COLO incoming process. Called from migration_incoming_thread() * after loading the state. -* -* Called with BQL locked, may temporary release BQL. */ -void coroutine_fn colo_incoming_co(void); +void colo_incoming_wait (void );void colo_shutdown (void );#endif migration/migration.h +5
-9@@ -214,6 +214,10 @@ struct MigrationIncomingState {bool have_listen_thread;QemuThread listen_thread; +/* Migration main recv thread */ +bool have_recv_thread; +QemuThread recv_thread; +/* For the kernel to send us notifications */ int userfault_fd;/* To notify the fault_thread to wake, e.g., when need to quit */ @@ -272,15 +276,7 @@ struct MigrationIncomingState {MigrationStatus state; -/* -* The incoming migration coroutine, non-NULL during qemu_loadvm_state(). -* Used to wake the migration incoming coroutine from rdma code. How much is -* it safe - it's a question. -*/ -Coroutine *loadvm_co; - -/* The coroutine we should enter (back) after failover */ -Coroutine *colo_incoming_co; +/* Notify secondary VM to move on */ QemuEvent colo_incoming_event;/* Optional load threads pool and its thread exit request flag */ migration/colo-stubs.c +1
-1@@ -9,7 +9,7 @@ void colo_shutdown(void) { } -void coroutine_fn colo_incoming_co(void) +void colo_incoming_wait (void ) { }migration/colo.c +6
-18@@ -147,11 +147,6 @@ static void secondary_vm_do_failover(void) }/* Notify COLO incoming thread that failover work is finished */ qemu_event_set (&mis->colo_incoming_event); - -/* For Secondary VM, jump to incoming co */ -if (mis->colo_incoming_co) { -qemu_coroutine_enter (mis->colo_incoming_co); - } }static void primary_vm_do_failover (void )@@ -848,10 +843,8 @@ static void *colo_process_incoming_thread(void *opaque) mis->to_src_file =qemu_file_get_return_path (mis->from_src_file);/* -* Note: the communication between Primary side and Secondary side -* should be sequential, we set the fd to unblocked in migration incoming -* coroutine, and here we are in the COLO incoming thread, so it is ok to -* set the fd back to blocked. +* Here we are in the COLO incoming thread, so it is ok to set the fd +* to blocking. */ if (!qemu_file_set_blocking (mis->from_src_file, true, &local_err)) {error_report_err (local_err);@@ -927,27 +920,22 @@ out:return NULL ; } -void coroutine_fn colo_incoming_co(void) +/* Wait for failover */ +void colo_incoming_wait (void ) {MigrationIncomingState *mis =migration_incoming_get_current ();QemuThread th; -assert (bql_locked ());assert (migration_incoming_colo_enabled ());qemu_thread_create (&th,MIGRATION_THREAD_DST_COLO , colo_process_incoming_thread, mis,QEMU_THREAD_JOINABLE ); - mis->colo_incoming_co =qemu_coroutine_self (); -qemu_coroutine_yield (); - mis->colo_incoming_co =NULL ; - -bql_unlock ();/* Wait checkpoint incoming thread exit before free resource */ qemu_thread_join (&th); -bql_lock (); -/* We hold the global BQL, so it is safe here */ +bql_lock ();colo_release_ram_cache (); +bql_unlock (); }migration/migration.c +48
-29@@ -491,6 +491,11 @@ void migration_incoming_state_destroy(void) mis->postcopy_qemufile_dst =NULL ; } +if (mis->have_recv_thread) { +qemu_thread_join (&mis->recv_thread); + mis->have_recv_thread = false; + } +cpr_set_incoming_mode (MIG_MODE_NONE );yank_unregister_instance (MIGRATION_YANK_INSTANCE ); }@@ -861,30 +866,46 @@ static void process_incoming_migration_bh(void *opaque)migration_incoming_state_destroy (); } -static void coroutine_fn -process_incoming_migration_co (void *opaque) +static void migration_incoming_state_destroy_bh (void *opaque) +{ +struct MigrationIncomingState *mis = opaque; + +migration_incoming_state_destroy (); + +if (mis->exit_on_error) { +/* +* NOTE: this exit() should better happen in the main thread, as +* the exit notifier may require BQL which can deadlock. See +* commit e7bc0204e57836 for example. +*/ +exit (EXIT_FAILURE ); + } +} + +static void *migration_incoming_thread (void *opaque) {MigrationState *s =migrate_get_current (); -MigrationIncomingState *mis =migration_incoming_get_current (); +MigrationIncomingState *mis = opaque;PostcopyState ps;int ret;Error *local_err =NULL ; +rcu_register_thread (); +assert (mis->from_src_file); +assert (!bql_locked ()); mis->largest_page_size =qemu_ram_pagesize_largest ();postcopy_state_set (POSTCOPY_INCOMING_NONE );migrate_set_state (&mis->state,MIGRATION_STATUS_SETUP ,MIGRATION_STATUS_ACTIVE ); - mis->loadvm_co =qemu_coroutine_self (); - ret =qemu_loadvm_state (mis->from_src_file, true, &local_err); - mis->loadvm_co =NULL ; + ret =qemu_loadvm_state (mis->from_src_file, false, &local_err);trace_vmstate_downtime_checkpoint ("dst-precopy-loadvm-completed" ); ps =postcopy_state_get (); -trace_process_incoming_migration_co_end (ret, ps); +trace_process_incoming_migration_end (ret, ps);if (ps !=POSTCOPY_INCOMING_NONE ) {if (ps ==POSTCOPY_INCOMING_ADVISE ) {/* @@ -898,7 +919,7 @@ process_incoming_migration_co(void *opaque)* Postcopy was started, cleanup should happen at the end of the * postcopy thread. */ -trace_process_incoming_migration_co_postcopy_end_main (); +trace_process_incoming_migration_postcopy_end_main (); goto out; }/* Else if something went wrong then just fall out of the normal exit */ @@ -911,8 +932,8 @@ process_incoming_migration_co(void *opaque) }if (migration_incoming_colo_enabled ()) { -/* yield until COLO exit */ -colo_incoming_co (); +/* wait until COLO exits */ +colo_incoming_wait (); }migration_bh_schedule (process_incoming_migration_bh, mis);@@ -924,28 +945,22 @@ fail:migrate_set_error (s, local_err);error_free (local_err); -migration_incoming_state_destroy (); + WITH_QEMU_LOCK_GUARD(&s->error_mutex) { +error_report_err (s->error); + s->error =NULL ; + } -if (mis->exit_on_error) { - WITH_QEMU_LOCK_GUARD(&s->error_mutex) { -error_report_err (s->error); - s->error =NULL ; - } +/* +* There's some step of the destroy process that will need to happen in +* the main thread (e.g. joining this thread itself). Leave to a BH. +*/ +migration_bh_schedule (migration_incoming_state_destroy_bh, (void *)mis); -exit (EXIT_FAILURE ); - }else { -/* -* Report the error here in case that QEMU abruptly exits -* when postcopy is enabled. -*/ - WITH_QEMU_LOCK_GUARD(&s->error_mutex) { -error_report_err (s->error); - s->error =NULL ; - } - } out:/* Pairs with the refcount taken in qmp_migrate_incoming() */ migrate_incoming_unref_outgoing_state (); +rcu_unregister_thread (); +return NULL ; }/** @@ -963,8 +978,12 @@ static void migration_incoming_setup(QEMUFile *f)void migration_incoming_process (void ) { -Coroutine *co =qemu_coroutine_create (process_incoming_migration_co,NULL ); -qemu_coroutine_enter (co); +MigrationIncomingState *mis =migration_incoming_get_current (); + + mis->have_recv_thread = true; +qemu_thread_create (&mis->recv_thread,"mig/dst/main" , + migration_incoming_thread, mis, +QEMU_THREAD_JOINABLE ); }/* Returns true if recovered from a paused migration, otherwise false */ migration/rdma.c +1
-33@@ -3051,37 +3051,6 @@ int rdma_control_save_page(QEMUFile *f, ram_addr_t block_offset,static void rdma_accept_incoming_migration (void *opaque); -static void rdma_cm_poll_handler (void *opaque) -{ -RDMAContext *rdma = opaque; -struct rdma_cm_event *cm_event; -MigrationIncomingState *mis =migration_incoming_get_current (); - -if (rdma_get_cm_event (rdma->channel, &cm_event) <0 ) { -error_report ("get_cm_event failed %d" , errno); -return ; - } - -if (cm_event->event ==RDMA_CM_EVENT_DISCONNECTED || - cm_event->event ==RDMA_CM_EVENT_DEVICE_REMOVAL ) { -if (!rdma->errored && -migration_incoming_get_current ()->state != -MIGRATION_STATUS_COMPLETED ) { -error_report ("receive cm event, cm event is %d" , cm_event->event); - rdma->errored = true; -if (rdma->return_path) { - rdma->return_path->errored = true; - } - } -rdma_ack_cm_event (cm_event); -if (mis->loadvm_co) { -qemu_coroutine_enter (mis->loadvm_co); - } -return ; - } -rdma_ack_cm_event (cm_event); -} -static int qemu_rdma_accept (RDMAContext *rdma) {Error *err =NULL ;@@ -3199,8 +3168,7 @@ static int qemu_rdma_accept(RDMAContext *rdma)NULL , (void *)(intptr_t)rdma->return_path); }else { -qemu_set_fd_handler (rdma->channel->fd, rdma_cm_poll_handler, -NULL , rdma); +qemu_set_fd_handler (rdma->channel->fd,NULL ,NULL ,NULL ); } ret =rdma_accept (rdma->cm_id, &conn_param);migration/savevm.c +4
-4@@ -2118,7 +2118,7 @@ static void *postcopy_ram_listen_thread(void *opaque)qemu_file_set_blocking (f, true, &error_fatal);/* TODO: sanity check that only postcopiable data will be loaded here */ - load_res =qemu_loadvm_state_main (f, mis, true, &local_err); + load_res =qemu_loadvm_state_main (f, mis, false, &local_err);/* * This is tricky, but, mis->from_src_file can change after it @@ -2415,11 +2415,11 @@ static void loadvm_postcopy_handle_resume(MigrationIncomingState *mis)* Immediately following this command is a blob of data containing an embedded * chunk of migration stream; read it and load it. * -* @mis: Incoming state -* @length: Length of packaged data to read +* @mis: Incoming state +* @bql_held: Whether BQL is held already +* @errp: The Error** to set when returning failures. * * Returns: Negative values on error -* */ static int loadvm_handle_cmd_packaged (MigrationIncomingState *mis,bool bql_held,Error **errp)migration/trace-events +2
-2@@ -193,8 +193,8 @@ source_return_path_thread_resume_ack(uint32_t v) "%"PRIu32 source_return_path_thread_switchover_acked(void) "" migration_thread_low_pending(uint64_t pending) "%" PRIu64 migrate_transferred(uint64_t transferred, uint64_t time_spent, uint64_t bandwidth, uint64_t avail_bw, uint64_t size) "transferred %" PRIu64 " time_spent %" PRIu64 " bandwidth %" PRIu64 " switchover_bw %" PRIu64 " max_size %" PRId64 -process_incoming_migration_co_end(int ret, int ps) "ret=%d postcopy-state=%d" -process_incoming_migration_co_postcopy_end_main(void) "" +process_incoming_migration_end(int ret, int ps) "ret=%d postcopy-state=%d" +process_incoming_migration_postcopy_end_main(void) "" postcopy_preempt_enabled(bool value) "%d" migration_precopy_complete(void) ""
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 9 lines · Zhijian Li (Fujitsu)
Both COLO and RDMA changes look good to me
Reviewed-by Li Zhijian <[email protected]> # COLO and RDMAAnd with an addition fixes[1] for COLO, the whole patchset:
This is helpful, thanks a lot Zhijian.
Signature · 1 line
Peter Xu
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
In reply to Peter XuQuoted 30 lines · Peter Xu
Migration module was there for 10+ years. Initially, it was in most cases based on coroutines. As more features were added into the framework, like postcopy, multifd, etc.. it became a mixture of threads and coroutines.
I'm guessing coroutines just can't fix all issues that migration want to resolve.
After all these years, migration is now heavily based on a threaded model.
Now there's still a major part of migration framework that is still not thread-based, which is precopy load. We do load in a separate thread in postcopy since the 1st day postcopy was introduced, however that requires a separate state transition from precopy loading all devices first, which still happens in the main thread of a coroutine.
This patch tries to move the migration incoming side to be run inside a separate thread (mig/dst/main) just like the src (mig/src/main). The entrance to be
migration_incoming_thread().Quite a few things are needed to make it fly.. One note here is we need to change all these things in one patch to not break anything. The other way to do this is add code to make all paths (that this patch touched) be ready for either coroutine or thread. That may cause confusions in another way. So reviewers, please take my sincere apology on the hardness of reviewing this patch: it covers a few modules at the same time, and with some risky changes.
BQL Analysis
============
Quoted 8 lines
Firstly, when moving it over to the thread, it means the thread cannot take BQL during the whole process of loading anymore, because otherwise it can block main thread from using the BQL for all kinds of other concurrent tasks (for example, processing QMP / HMP commands).
Here the first question to ask is: what needs BQL during precopy load, and what doesn't?
I just noticed that the BQL held at process_incoming_migration_co is also responsible for stopping qmp_migrate_set_capabilities from being dispatched.
Any point during incoming migration when BQL is unlocked we have a window where a capability could be changed. Same for parameters, for that matter.
To make matters worse, the -incoming cmdline will trigger qmp_migrate_incoming->...->migration_transport_compatible early on, but until the channels finally connect and process_incoming_migration_co starts it's possible to just change a capability in an incompatible way and the transport will never be validated again.
One example:
-- >8 --
From 99bd88aa0a8b6d4e7c52196f25d344a2800b3d89 Mon Sep 17 00:00:00 2001
From: Fabiano Rosas <[email protected]>
Date: Thu, 8 Jan 2026 17:21:20 -0300
Subject: [PATCH] tmp
--- tests/qtest/migration/precopy-tests.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tests/qtest/migration/precopy-tests.c +8 -0
@@ -158,6 +158,13 @@ static int new_rdma_link(char *buffer, bool ipv6)return -1 ; } +static void *migrate_rdma_set_caps (QTestState *from,QTestState *to) +{ +migrate_set_capability (to,"mapped-ram" , true); + +return NULL ; +} +static void __test_precopy_rdma_plain (MigrateCommon *args,bool ipv6) {char buffer[128 ] = {};
@@ -185,6 +192,7 @@ static void __test_precopy_rdma_plain(MigrateCommon *args, bool ipv6) args->listen_uri = uri; args->connect_uri = uri; + args->start_hook = migrate_rdma_set_caps;test_precopy_common (args); }
Signature · 1 line
2.51.0
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 37 lines · Fabiano Rosas
Quoted 35 lines · Peter Xu
Migration module was there for 10+ years. Initially, it was in most cases based on coroutines. As more features were added into the framework, like postcopy, multifd, etc.. it became a mixture of threads and coroutines.
I'm guessing coroutines just can't fix all issues that migration want to resolve.
After all these years, migration is now heavily based on a threaded model.
Now there's still a major part of migration framework that is still not thread-based, which is precopy load. We do load in a separate thread in postcopy since the 1st day postcopy was introduced, however that requires a separate state transition from precopy loading all devices first, which still happens in the main thread of a coroutine.
This patch tries to move the migration incoming side to be run inside a separate thread (mig/dst/main) just like the src (mig/src/main). The entrance to be
migration_incoming_thread().Quite a few things are needed to make it fly.. One note here is we need to change all these things in one patch to not break anything. The other way to do this is add code to make all paths (that this patch touched) be ready for either coroutine or thread. That may cause confusions in another way. So reviewers, please take my sincere apology on the hardness of reviewing this patch: it covers a few modules at the same time, and with some risky changes.
BQL Analysis
============Firstly, when moving it over to the thread, it means the thread cannot take BQL during the whole process of loading anymore, because otherwise it can block main thread from using the BQL for all kinds of other concurrent tasks (for example, processing QMP / HMP commands).
Quoted 7 lines
Quoted 3 lines
Here the first question to ask is: what needs BQL during precopy load, and what doesn't?
I just noticed that the BQL held at
process_incoming_migration_cois also responsible for stoppingqmp_migrate_set_capabilitiesfrom being dispatched.
I don't know if it is by design, or even if it will be guaranteed to work..
Consider the migration incoming rocoutine runs into qemu_get_byte(), and then proactively yield the migration coroutine (qemu_coroutine_yield()) when the incoming port is blocked on read..
AFAIU, a proper fix for that (note, this will currently break tests) is:
bool migration_is_running(void)
{
- MigrationState *s = current_migration;
+ MigrationStatus state;
- if (!s) {
- return false;
+ if (runstate_check(RUN_STATE_INMIGRATE)) {
+ MigrationIncomingState *mis = migration_incoming_get_current();
+
+ if (!mis) {
+ return false;
+ }
+
+ state = mis->state;
+ } else {
+ MigrationState *s = migrate_get_current();
+
+ if (!s) {
+ return false;
+ }
+
+ state = s->state;
}- switch (s->state) {
+ switch (state) {
case MIGRATION_STATUS_ACTIVE:
case MIGRATION_STATUS_POSTCOPY_DEVICE:
case MIGRATION_STATUS_POSTCOPY_ACTIVE:
Quoted 10 lines
Any point during incoming migration when BQL is unlocked we have a window where a capability could be changed. Same for parameters, for that matter.
To make matters worse, the -incoming cmdline will trigger qmp_migrate_incoming->...->migration_transport_compatible early on, but until the channels finally connect and
process_incoming_migration_costarts it's possible to just change a capability in an incompatible way and the transport will never be validated again.
Right. Above should fix it, but I believe it also means after "-incoming tcp:xxx" (or anything not "defer") we should forbid changing migration caps or params on destination.
As discussed above, that'll at least break our qtests. But frankly speaking I think that's the right thing to do.. I hope libvirt always works with "defer" and never update any caps/params after QMP migrate_incoming.
So I wonder if I should continue with above patch, and then fix our qtests. Your work from the other "merge caps+params" might also work here, actually, if we make sure everything will be set alone with the QMP migrate_incoming single command.
Let me know your initial thoughts, then I'll see what I can do..
Thanks,
Quoted 42 lines
One example:
-- >8 --
From99bd88aa0a8b6d4e7c52196f25d344a2800b3d89Mon Sep 17 00:00:00 2001
From: Fabiano Rosas <[email protected]>
Date: Thu, 8 Jan 2026 17:21:20 -0300
Subject: [PATCH] tmp--- tests/qtest/migration/precopy-tests.c | 8 ++++++++ 1 file changed, 8 insertions(+)tests/qtest/migration/precopy-tests.c +8
-0@@ -158,6 +158,13 @@ static int new_rdma_link(char *buffer, bool ipv6)return -1 ; } +static void *migrate_rdma_set_caps (QTestState *from,QTestState *to) +{ +migrate_set_capability (to,"mapped-ram" , true); + +return NULL ; +} +static void __test_precopy_rdma_plain (MigrateCommon *args,bool ipv6) {char buffer[128 ] = {};@@ -185,6 +192,7 @@ static void __test_precopy_rdma_plain(MigrateCommon *args, bool ipv6) args->listen_uri = uri; args->connect_uri = uri; + args->start_hook = migrate_rdma_set_caps;test_precopy_common (args); }Signature · 1 line
2.51.0
Signature · 1 line
Peter Xu
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 42 lines · Peter Xu
Quoted 41 lines · Fabiano Rosas
Quoted 38 lines · Peter Xu
Migration module was there for 10+ years. Initially, it was in most cases based on coroutines. As more features were added into the framework, like postcopy, multifd, etc.. it became a mixture of threads and coroutines.
I'm guessing coroutines just can't fix all issues that migration want to resolve.
After all these years, migration is now heavily based on a threaded model.
Now there's still a major part of migration framework that is still not thread-based, which is precopy load. We do load in a separate thread in postcopy since the 1st day postcopy was introduced, however that requires a separate state transition from precopy loading all devices first, which still happens in the main thread of a coroutine.
This patch tries to move the migration incoming side to be run inside a separate thread (mig/dst/main) just like the src (mig/src/main). The entrance to be
migration_incoming_thread().Quite a few things are needed to make it fly.. One note here is we need to change all these things in one patch to not break anything. The other way to do this is add code to make all paths (that this patch touched) be ready for either coroutine or thread. That may cause confusions in another way. So reviewers, please take my sincere apology on the hardness of reviewing this patch: it covers a few modules at the same time, and with some risky changes.
BQL Analysis
============Firstly, when moving it over to the thread, it means the thread cannot take BQL during the whole process of loading anymore, because otherwise it can block main thread from using the BQL for all kinds of other concurrent tasks (for example, processing QMP / HMP commands).
Here the first question to ask is: what needs BQL during precopy load, and what doesn't?
Quoted 6 lines
Quoted 3 lines
I just noticed that the BQL held at
process_incoming_migration_cois also responsible for stoppingqmp_migrate_set_capabilitiesfrom being dispatched.I don't know if it is by design, or even if it will be guaranteed to work..
Regardless, we shouldn't rely on the BQL for this. The BQL should be left as last resort for things that interact across subsystems. If someone is issuing a migration command during a migration, the migration code is exquisitely positioned to handle that itself.
Quoted 27 lines
Consider the migration incoming rocoutine runs into
qemu_get_byte(), and then proactively yield the migration coroutine (qemu_coroutine_yield()) when the incoming port is blocked on read..AFAIU, a proper fix for that (note, this will currently break tests) is:
bool
migration_is_running(void)
{(quoted diff) +14
-3- MigrationState *s = current_migration; + MigrationStatus state; - if (!s) { - return false; + if (runstate_check(RUN_STATE_INMIGRATE)) { + MigrationIncomingState *mis = migration_incoming_get_current(); + + if (!mis) { + return false; + } + + state = mis->state; + } else { + MigrationState *s = migrate_get_current(); + + if (!s) { + return false;
Review
Quoted 10 lines
(quoted diff) +4
-1+ } + + state = s->state; } - switch (s->state) { + switch (state) { case MIGRATION_STATUS_ACTIVE: case MIGRATION_STATUS_POSTCOPY_DEVICE: case MIGRATION_STATUS_POSTCOPY_ACTIVE:
LGTM
Quoted 11 lines
Quoted 10 lines
Any point during incoming migration when BQL is unlocked we have a window where a capability could be changed. Same for parameters, for that matter.
To make matters worse, the -incoming cmdline will trigger qmp_migrate_incoming->...->migration_transport_compatible early on, but until the channels finally connect and
process_incoming_migration_costarts it's possible to just change a capability in an incompatible way and the transport will never be validated again.
Quoted 4 lines
Right. Above should fix it, but I believe it also means after "-incoming tcp:xxx" (or anything not "defer") we should forbid changing migration caps or params on destination.
Parameters are never forbidden, right? And we cannot forbid them with is_running because some parameters are allowed to be changed while running.
I feel we should have a more fine grained way of saying "this option cannot be set at this moment", instead of just using the state as a proxy. States can change, while the fact that from a certain point on, certain options should not be touched anymore doesn't change.
Maybe a little infra like bdrv_op_is_blocked, i.e, a list of blocked operations. It could be set in qmp_migrate and checked in qmp_set_parameters/caps.
Quoted 10 lines
As discussed above, that'll at least break our qtests. But frankly speaking I think that's the right thing to do.. I hope libvirt always works with "defer" and never update any caps/params after QMP
migrate_incoming.So I wonder if I should continue with above patch, and then fix our qtests. Your work from the other "merge caps+params" might also work here, actually, if we make sure everything will be set alone with the QMP
migrate_incomingsingle command.
For incoming, yes. And this is maybe a point in favor of adding the 'config'.
For outgoing, there's still the point I mentioned above about how to restrict _some_ options to be allowed at runtime and others not.
Quoted 2 lines
Let me know your initial thoughts, then I'll see what I can do..
We should fix the bug, I think your patch is good for that.
Although this kind of overlaps with some things we've been discussing with Prasad. I'd be super happy if the code magically stopped using QAPI's MigrationStatus for internal tracking of migration state and blocking of commands and so on.
Whatever comes first =)
---
Side note, did we ever discuss something like this?
struct MigrationState {
<state>
union {
<outgoing>
<incoming>
}
}
there's so much stuff in these structs...
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 48 lines · Fabiano Rosas
Quoted 46 lines · Peter Xu
Quoted 44 lines · Fabiano Rosas
Quoted 30 lines · Peter Xu
Migration module was there for 10+ years. Initially, it was in most cases based on coroutines. As more features were added into the framework, like postcopy, multifd, etc.. it became a mixture of threads and coroutines.
I'm guessing coroutines just can't fix all issues that migration want to resolve.
After all these years, migration is now heavily based on a threaded model.
Now there's still a major part of migration framework that is still not thread-based, which is precopy load. We do load in a separate thread in postcopy since the 1st day postcopy was introduced, however that requires a separate state transition from precopy loading all devices first, which still happens in the main thread of a coroutine.
This patch tries to move the migration incoming side to be run inside a separate thread (mig/dst/main) just like the src (mig/src/main). The entrance to be
migration_incoming_thread().Quite a few things are needed to make it fly.. One note here is we need to change all these things in one patch to not break anything. The other way to do this is add code to make all paths (that this patch touched) be ready for either coroutine or thread. That may cause confusions in another way. So reviewers, please take my sincere apology on the hardness of reviewing this patch: it covers a few modules at the same time, and with some risky changes.
BQL Analysis
============Quoted 8 lines
Firstly, when moving it over to the thread, it means the thread cannot take BQL during the whole process of loading anymore, because otherwise it can block main thread from using the BQL for all kinds of other concurrent tasks (for example, processing QMP / HMP commands).
Here the first question to ask is: what needs BQL during precopy load, and what doesn't?
I just noticed that the BQL held at
process_incoming_migration_cois also responsible for stoppingqmp_migrate_set_capabilitiesfrom being dispatched.
Quoted 7 lines
Quoted 2 lines
I don't know if it is by design, or even if it will be guaranteed to work..
Regardless, we shouldn't rely on the BQL for this. The BQL should be left as last resort for things that interact across subsystems. If someone is issuing a migration command during a migration, the migration code is exquisitely positioned to handle that itself.
Yes I agree.
Quoted 52 lines
Quoted 28 lines
Consider the migration incoming rocoutine runs into
qemu_get_byte(), and then proactively yield the migration coroutine (qemu_coroutine_yield()) when the incoming port is blocked on read..AFAIU, a proper fix for that (note, this will currently break tests) is:
bool
migration_is_running(void)
{(quoted diff) +14
-3- MigrationState *s = current_migration; + MigrationStatus state; - if (!s) { - return false; + if (runstate_check(RUN_STATE_INMIGRATE)) { + MigrationIncomingState *mis = migration_incoming_get_current(); + + if (!mis) { + return false; + } + + state = mis->state; + } else { + MigrationState *s = migrate_get_current(); + + if (!s) { + return false;Review
Quoted 10 lines
(quoted diff) +4
-1+ } + + state = s->state; } - switch (s->state) { + switch (state) { case MIGRATION_STATUS_ACTIVE: case MIGRATION_STATUS_POSTCOPY_DEVICE: case MIGRATION_STATUS_POSTCOPY_ACTIVE:LGTM
Quoted 11 lines
Quoted 10 lines
Any point during incoming migration when BQL is unlocked we have a window where a capability could be changed. Same for parameters, for that matter.
To make matters worse, the -incoming cmdline will trigger qmp_migrate_incoming->...->migration_transport_compatible early on, but until the channels finally connect and
process_incoming_migration_costarts it's possible to just change a capability in an incompatible way and the transport will never be validated again.
Quoted 8 lines
Quoted 4 lines
Right. Above should fix it, but I believe it also means after "-incoming tcp:xxx" (or anything not "defer") we should forbid changing migration caps or params on destination.
Parameters are never forbidden, right? And we cannot forbid them with
is_runningbecause some parameters are allowed to be changed while running.
Right, my above statement was definitely inaccurate.
After merging caps and params we only have params. We should only allow some params to be changed anytime. Most of the params (including caps) should not allow changing during migration live on either src/dst.
Quoted 5 lines
I feel we should have a more fine grained way of saying "this option cannot be set at this moment", instead of just using the state as a proxy. States can change, while the fact that from a certain point on, certain options should not be touched anymore doesn't change.
IIUC that's what migration_is_running() about?
It's a matter of if such finer granularity is necessary for us. Let's assume the simple scheme is:
(a) Some params are allowed to be modified anytime, examples, downtime-limit, max-bandwidth, max-postcopy-bandwidth, etc.
(b) All the rest params and all capabilities are not allowed to be modified when migration_is_running() returns true (my fixed version above)
The whitelisted (a) should really be the smallest set of params, and justified one by one or it should fall into (b).
My hope is the simple scheme should work for us already. After merging caps, it's only about some params that can be set anytime, rest params can only be set when migration is not running.
Quoted 4 lines
Maybe a little infra like
bdrv_op_is_blocked, i.e, a list of blocked operations. It could be set inqmp_migrateand checked in qmp_set_parameters/caps.
Any example you can think of, that above simple scheme won't work for us?
Quoted 21 lines
Quoted 11 lines
As discussed above, that'll at least break our qtests. But frankly speaking I think that's the right thing to do.. I hope libvirt always works with "defer" and never update any caps/params after QMP
migrate_incoming.So I wonder if I should continue with above patch, and then fix our qtests. Your work from the other "merge caps+params" might also work here, actually, if we make sure everything will be set alone with the QMP
migrate_incomingsingle command.For incoming, yes. And this is maybe a point in favor of adding the 'config'.
For outgoing, there's still the point I mentioned above about how to restrict _some_ options to be allowed at runtime and others not.
Quoted 2 lines
Let me know your initial thoughts, then I'll see what I can do..
Quoted 8 lines
We should fix the bug, I think your patch is good for that.
Although this kind of overlaps with some things we've been discussing with Prasad. I'd be super happy if the code magically stopped using QAPI's MigrationStatus for internal tracking of migration state and blocking of commands and so on.
Whatever comes first =)
I didn't follow as closely on the discussion there. I don't know if changing MigrationStatus is a good idea.. we should have some really good reason to make libvirt and all mgmt need a change.. but maybe I misread the discussion. I can wait until I read something solid if Prasad is going to propose something.
Quoted 12 lines
---
Side note, did we ever discuss something like this?struct MigrationState { <state> union { <outgoing> <incoming> } }
Quoted 1 line
there's so much stuff in these structs...
Yeah.. When merging state, we'll also need to be careful on overlapped fields, e.g. expecting a COMPLETED state (from a completed incoming migration) when starting a new outgoing migration.
We can likely leave this for later.
Signature · 1 line
Peter Xu
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 127 lines · Peter Xu
Quoted 48 lines · Fabiano Rosas
Quoted 46 lines · Peter Xu
Quoted 44 lines · Fabiano Rosas
Quoted 30 lines · Peter Xu
Migration module was there for 10+ years. Initially, it was in most cases based on coroutines. As more features were added into the framework, like postcopy, multifd, etc.. it became a mixture of threads and coroutines.
I'm guessing coroutines just can't fix all issues that migration want to resolve.
After all these years, migration is now heavily based on a threaded model.
Now there's still a major part of migration framework that is still not thread-based, which is precopy load. We do load in a separate thread in postcopy since the 1st day postcopy was introduced, however that requires a separate state transition from precopy loading all devices first, which still happens in the main thread of a coroutine.
This patch tries to move the migration incoming side to be run inside a separate thread (mig/dst/main) just like the src (mig/src/main). The entrance to be
migration_incoming_thread().Quite a few things are needed to make it fly.. One note here is we need to change all these things in one patch to not break anything. The other way to do this is add code to make all paths (that this patch touched) be ready for either coroutine or thread. That may cause confusions in another way. So reviewers, please take my sincere apology on the hardness of reviewing this patch: it covers a few modules at the same time, and with some risky changes.
BQL Analysis
============Quoted 8 lines
Firstly, when moving it over to the thread, it means the thread cannot take BQL during the whole process of loading anymore, because otherwise it can block main thread from using the BQL for all kinds of other concurrent tasks (for example, processing QMP / HMP commands).
Here the first question to ask is: what needs BQL during precopy load, and what doesn't?
I just noticed that the BQL held at
process_incoming_migration_cois also responsible for stoppingqmp_migrate_set_capabilitiesfrom being dispatched.Quoted 7 lines
Quoted 2 lines
I don't know if it is by design, or even if it will be guaranteed to work..
Regardless, we shouldn't rely on the BQL for this. The BQL should be left as last resort for things that interact across subsystems. If someone is issuing a migration command during a migration, the migration code is exquisitely positioned to handle that itself.
Yes I agree.
Quoted 52 lines
Quoted 28 lines
Consider the migration incoming rocoutine runs into
qemu_get_byte(), and then proactively yield the migration coroutine (qemu_coroutine_yield()) when the incoming port is blocked on read..AFAIU, a proper fix for that (note, this will currently break tests) is:
bool
migration_is_running(void)
{(quoted diff) +14
-3- MigrationState *s = current_migration; + MigrationStatus state; - if (!s) { - return false; + if (runstate_check(RUN_STATE_INMIGRATE)) { + MigrationIncomingState *mis = migration_incoming_get_current(); + + if (!mis) { + return false; + } + + state = mis->state; + } else { + MigrationState *s = migrate_get_current(); + + if (!s) { + return false;Review
Quoted 10 lines
(quoted diff) +4
-1+ } + + state = s->state; } - switch (s->state) { + switch (state) { case MIGRATION_STATUS_ACTIVE: case MIGRATION_STATUS_POSTCOPY_DEVICE: case MIGRATION_STATUS_POSTCOPY_ACTIVE:LGTM
Quoted 11 lines
Quoted 10 lines
Any point during incoming migration when BQL is unlocked we have a window where a capability could be changed. Same for parameters, for that matter.
To make matters worse, the -incoming cmdline will trigger qmp_migrate_incoming->...->migration_transport_compatible early on, but until the channels finally connect and
process_incoming_migration_costarts it's possible to just change a capability in an incompatible way and the transport will never be validated again.Quoted 8 lines
Quoted 4 lines
Right. Above should fix it, but I believe it also means after "-incoming tcp:xxx" (or anything not "defer") we should forbid changing migration caps or params on destination.
Parameters are never forbidden, right? And we cannot forbid them with
is_runningbecause some parameters are allowed to be changed while running.Right, my above statement was definitely inaccurate.
After merging caps and params we only have params. We should only allow some params to be changed anytime. Most of the params (including caps) should not allow changing during migration live on either src/dst.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
I feel we should have a more fine grained way of saying "this option cannot be set at this moment", instead of just using the state as a proxy. States can change, while the fact that from a certain point on, certain options should not be touched anymore doesn't change.
IIUC that's what
migration_is_running()about?
At a high level, yes, but I think that's actually a downside of migration_is_running. It's not explicit.
E.g.: qmp_migrate -> migration_transport_compatible is the first time capabilities are checked in the code. Guess what migration_is_running returns at that point? (it's false)
If there's ever a change in BQL behavior, there'll be a bug and we'll see another patch extending the scope of migration_is_running, either adding existing states to it, or as Prasad proposed, adding a new state that is set a little later/earlier.
What we technically want is to stop accepting new capabilities as soon as we're about to use them for the first time. Not as soon as (state == ACTIVE || SETUP || ...) which is a more high level definition.
Quoted 6 lines
It's a matter of if such finer granularity is necessary for us. Let's assume the simple scheme is:
(a) Some params are allowed to be modified anytime, examples, downtime-limit, max-bandwidth, max-postcopy-bandwidth, etc.
What about during migration_cleanup() or migrate-cancel? Won't there ever be something in the path of setting these params that would require a "healthy" migration?
Quoted 10 lines
(b) All the rest params and all capabilities are not allowed to be modified when
migration_is_running()returns true (my fixed version above)The whitelisted (a) should really be the smallest set of params, and justified one by one or it should fall into (b).
My hope is the simple scheme should work for us already. After merging caps, it's only about some params that can be set anytime, rest params can only be set when migration is not running.
Sure, I'd just like to avoid adding another set of "if (s->parameters->has_foobar)" if possible.
Quoted 7 lines
Quoted 4 lines
Maybe a little infra like
bdrv_op_is_blocked, i.e, a list of blocked operations. It could be set inqmp_migrateand checked in qmp_set_parameters/caps.Any example you can think of, that above simple scheme won't work for us?
It works. I think it's fragile due to:
- the reliance on BQL to avoid concurrent invocations;
- the possible addition of new states for unrelated reasons risks regressions and it's just extra work to make sure everything is still blocked at the time it needs to be blocked.
- any bugs need to be fixed by moving states around, which is dubious since the migration flow itself (which the states should represent) has not changed in a while.
We're still seeing patches such as dc487044d5 ("migration: Make migration_has_failed() work even for CANCELLING") and the recent patch from Prasad. So there is something changing elsewhere and exposing these bugs and it's not immediately clear what that something is (or else we would have avoided the bug).
To be clear, I'm not asking we solve these issues at this moment, it's fine if we go forward with the simple scheme. I'm just trying to clarify what I think the problems are.
Quoted 28 lines
Quoted 28 lines
Quoted 11 lines
As discussed above, that'll at least break our qtests. But frankly speaking I think that's the right thing to do.. I hope libvirt always works with "defer" and never update any caps/params after QMP
migrate_incoming.So I wonder if I should continue with above patch, and then fix our qtests. Your work from the other "merge caps+params" might also work here, actually, if we make sure everything will be set alone with the QMP
migrate_incomingsingle command.For incoming, yes. And this is maybe a point in favor of adding the 'config'.
For outgoing, there's still the point I mentioned above about how to restrict _some_ options to be allowed at runtime and others not.
Quoted 2 lines
Let me know your initial thoughts, then I'll see what I can do..
We should fix the bug, I think your patch is good for that.
Although this kind of overlaps with some things we've been discussing with Prasad. I'd be super happy if the code magically stopped using QAPI's MigrationStatus for internal tracking of migration state and blocking of commands and so on.
Quoted 8 lines
Quoted 1 line
Whatever comes first =)
I didn't follow as closely on the discussion there. I don't know if changing MigrationStatus is a good idea.. we should have some really good reason to make libvirt and all mgmt need a change.. but maybe I misread the discussion. I can wait until I read something solid if Prasad is going to propose something.
We're moving towards decoupling MigrationStatus from these ordinary code checks. MigrationStatus should be used for informing management about a relevant change in the migration stage of progression.
As it stands, we're either "exposing implementation details" or "inadvertently changing the API". Both are undesirable.
Of course, we might introduce the "internal state is off-sync with externally visible states" issue, but hopefully we'll catch that during review. =)
Quoted 12 lines
Quoted 12 lines
---
Side note, did we ever discuss something like this?struct MigrationState { <state> union { <outgoing> <incoming> } }
Quoted 7 lines
Quoted 1 line
there's so much stuff in these structs...
Yeah.. When merging state, we'll also need to be careful on overlapped fields, e.g. expecting a COMPLETED state (from a completed incoming migration) when starting a new outgoing migration.
We can likely leave this for later.
No worries, I'm just collecting your opinions on it.
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 144 lines · Fabiano Rosas
Quoted 127 lines · Peter Xu
Quoted 48 lines · Fabiano Rosas
Quoted 46 lines · Peter Xu
Quoted 44 lines · Fabiano Rosas
Quoted 30 lines · Peter Xu
Migration module was there for 10+ years. Initially, it was in most cases based on coroutines. As more features were added into the framework, like postcopy, multifd, etc.. it became a mixture of threads and coroutines.
I'm guessing coroutines just can't fix all issues that migration want to resolve.
After all these years, migration is now heavily based on a threaded model.
Now there's still a major part of migration framework that is still not thread-based, which is precopy load. We do load in a separate thread in postcopy since the 1st day postcopy was introduced, however that requires a separate state transition from precopy loading all devices first, which still happens in the main thread of a coroutine.
This patch tries to move the migration incoming side to be run inside a separate thread (mig/dst/main) just like the src (mig/src/main). The entrance to be
migration_incoming_thread().Quite a few things are needed to make it fly.. One note here is we need to change all these things in one patch to not break anything. The other way to do this is add code to make all paths (that this patch touched) be ready for either coroutine or thread. That may cause confusions in another way. So reviewers, please take my sincere apology on the hardness of reviewing this patch: it covers a few modules at the same time, and with some risky changes.
BQL Analysis
============Quoted 8 lines
Firstly, when moving it over to the thread, it means the thread cannot take BQL during the whole process of loading anymore, because otherwise it can block main thread from using the BQL for all kinds of other concurrent tasks (for example, processing QMP / HMP commands).
Here the first question to ask is: what needs BQL during precopy load, and what doesn't?
I just noticed that the BQL held at
process_incoming_migration_cois also responsible for stoppingqmp_migrate_set_capabilitiesfrom being dispatched.Quoted 7 lines
Quoted 2 lines
I don't know if it is by design, or even if it will be guaranteed to work..
Regardless, we shouldn't rely on the BQL for this. The BQL should be left as last resort for things that interact across subsystems. If someone is issuing a migration command during a migration, the migration code is exquisitely positioned to handle that itself.
Yes I agree.
Quoted 52 lines
Quoted 28 lines
Consider the migration incoming rocoutine runs into
qemu_get_byte(), and then proactively yield the migration coroutine (qemu_coroutine_yield()) when the incoming port is blocked on read..AFAIU, a proper fix for that (note, this will currently break tests) is:
bool
migration_is_running(void)
{(quoted diff) +14
-3- MigrationState *s = current_migration; + MigrationStatus state; - if (!s) { - return false; + if (runstate_check(RUN_STATE_INMIGRATE)) { + MigrationIncomingState *mis = migration_incoming_get_current(); + + if (!mis) { + return false; + } + + state = mis->state; + } else { + MigrationState *s = migrate_get_current(); + + if (!s) { + return false;Review
Quoted 10 lines
(quoted diff) +4
-1+ } + + state = s->state; } - switch (s->state) { + switch (state) { case MIGRATION_STATUS_ACTIVE: case MIGRATION_STATUS_POSTCOPY_DEVICE: case MIGRATION_STATUS_POSTCOPY_ACTIVE:LGTM
Quoted 11 lines
Quoted 10 lines
Any point during incoming migration when BQL is unlocked we have a window where a capability could be changed. Same for parameters, for that matter.
To make matters worse, the -incoming cmdline will trigger qmp_migrate_incoming->...->migration_transport_compatible early on, but until the channels finally connect and
process_incoming_migration_costarts it's possible to just change a capability in an incompatible way and the transport will never be validated again.Quoted 8 lines
Quoted 4 lines
Right. Above should fix it, but I believe it also means after "-incoming tcp:xxx" (or anything not "defer") we should forbid changing migration caps or params on destination.
Parameters are never forbidden, right? And we cannot forbid them with
is_runningbecause some parameters are allowed to be changed while running.Right, my above statement was definitely inaccurate.
After merging caps and params we only have params. We should only allow some params to be changed anytime. Most of the params (including caps) should not allow changing during migration live on either src/dst.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
I feel we should have a more fine grained way of saying "this option cannot be set at this moment", instead of just using the state as a proxy. States can change, while the fact that from a certain point on, certain options should not be touched anymore doesn't change.
IIUC that's what
migration_is_running()about?At a high level, yes, but I think that's actually a downside of
migration_is_running. It's not explicit.E.g.:
qmp_migrate->migration_transport_compatibleis the first time capabilities are checked in the code. Guess whatmigration_is_runningreturns at that point? (it's false)
Quoted 8 lines
If there's ever a change in BQL behavior, there'll be a bug and we'll see another patch extending the scope of
migration_is_running, either adding existing states to it, or as Prasad proposed, adding a new state that is set a little later/earlier.What we technically want is to stop accepting new capabilities as soon as we're about to use them for the first time. Not as soon as (state == ACTIVE || SETUP || ...) which is a more high level definition.
Isn't migration_incoming_state_setup() almost immediately invoked (after migration_transport_compatible() check passed), which will start to make the retval of migration_is_running() reflecting the fact?
IMHO as long as QMP handlers are run with any lock (currently, BQL), then such layout should keep working? That will make sure QMP "migrate" and "migrate_set_parameters" be serialized, that's, AFAICT, what we only need.
Quoted 11 lines
Quoted 7 lines
It's a matter of if such finer granularity is necessary for us. Let's assume the simple scheme is:
(a) Some params are allowed to be modified anytime, examples, downtime-limit, max-bandwidth, max-postcopy-bandwidth, etc.
What about during
migration_cleanup()or migrate-cancel? Won't there ever be something in the path of setting these params that would require a "healthy" migration?
If we'll introduce FAILING, then plus CANCELLING I think we should cover all race against migration_cleanup()? As long as we'll have both FAILING and CANCELLING to return true for migration_is_running().
Quoted 23 lines
Quoted 11 lines
(b) All the rest params and all capabilities are not allowed to be modified when
migration_is_running()returns true (my fixed version above)The whitelisted (a) should really be the smallest set of params, and justified one by one or it should fall into (b).
My hope is the simple scheme should work for us already. After merging caps, it's only about some params that can be set anytime, rest params can only be set when migration is not running.
Sure, I'd just like to avoid adding another set of "if (
s->parameters->has_foobar)" if possible.Quoted 7 lines
Quoted 4 lines
Maybe a little infra like
bdrv_op_is_blocked, i.e, a list of blocked operations. It could be set inqmp_migrateand checked in qmp_set_parameters/caps.Any example you can think of, that above simple scheme won't work for us?
Quoted 7 lines
It works. I think it's fragile due to:
- the reliance on BQL to avoid concurrent invocations;
- the possible addition of new states for unrelated reasons risks regressions and it's just extra work to make sure everything is still blocked at the time it needs to be blocked.
Yes, we'll need to make sure when new states introduced it needs to report correctly in migration_is_running(). I think it's not an issue because we need to do it right with/without relying on the function to ban updates on caps/params.. It always must be done right as migration_is_running() is also used in other use cases.
Quoted 56 lines
- any bugs need to be fixed by moving states around, which is dubious since the migration flow itself (which the states should represent) has not changed in a while.
We're still seeing patches such as
dc487044d5("migration: Makemigration_has_failed()work even for CANCELLING") and the recent patch from Prasad. So there is something changing elsewhere and exposing these bugs and it's not immediately clear what that something is (or else we would have avoided the bug).To be clear, I'm not asking we solve these issues at this moment, it's fine if we go forward with the simple scheme. I'm just trying to clarify what I think the problems are.
Quoted 28 lines
Quoted 28 lines
Quoted 11 lines
As discussed above, that'll at least break our qtests. But frankly speaking I think that's the right thing to do.. I hope libvirt always works with "defer" and never update any caps/params after QMP
migrate_incoming.So I wonder if I should continue with above patch, and then fix our qtests. Your work from the other "merge caps+params" might also work here, actually, if we make sure everything will be set alone with the QMP
migrate_incomingsingle command.For incoming, yes. And this is maybe a point in favor of adding the 'config'.
For outgoing, there's still the point I mentioned above about how to restrict _some_ options to be allowed at runtime and others not.
Quoted 2 lines
Let me know your initial thoughts, then I'll see what I can do..
We should fix the bug, I think your patch is good for that.
Although this kind of overlaps with some things we've been discussing with Prasad. I'd be super happy if the code magically stopped using QAPI's MigrationStatus for internal tracking of migration state and blocking of commands and so on.
Quoted 8 lines
Quoted 1 line
Whatever comes first =)
I didn't follow as closely on the discussion there. I don't know if changing MigrationStatus is a good idea.. we should have some really good reason to make libvirt and all mgmt need a change.. but maybe I misread the discussion. I can wait until I read something solid if Prasad is going to propose something.
We're moving towards decoupling MigrationStatus from these ordinary code checks. MigrationStatus should be used for informing management about a relevant change in the migration stage of progression.
Quoted 6 lines
As it stands, we're either "exposing implementation details" or "inadvertently changing the API". Both are undesirable.
Of course, we might introduce the "internal state is off-sync with externally visible states" issue, but hopefully we'll catch that during review. =)
I need to confess I'm still a bit lost on what it is about, and what is the problem we're solving here.. But I can always wait and read the patches when they come up.. :)
Quoted 23 lines
Quoted 13 lines
Quoted 13 lines
---
Side note, did we ever discuss something like this?struct MigrationState { <state> union { <outgoing> <incoming> } }Quoted 7 lines
Quoted 1 line
there's so much stuff in these structs...
Yeah.. When merging state, we'll also need to be careful on overlapped fields, e.g. expecting a COMPLETED state (from a completed incoming migration) when starting a new outgoing migration.
We can likely leave this for later.
No worries, I'm just collecting your opinions on it.
Signature · 1 line
Peter Xu
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 274 lines · Peter Xu
Quoted 144 lines · Fabiano Rosas
Quoted 127 lines · Peter Xu
Quoted 48 lines · Fabiano Rosas
Quoted 46 lines · Peter Xu
Quoted 44 lines · Fabiano Rosas
Quoted 30 lines · Peter Xu
Migration module was there for 10+ years. Initially, it was in most cases based on coroutines. As more features were added into the framework, like postcopy, multifd, etc.. it became a mixture of threads and coroutines.
I'm guessing coroutines just can't fix all issues that migration want to resolve.
After all these years, migration is now heavily based on a threaded model.
Now there's still a major part of migration framework that is still not thread-based, which is precopy load. We do load in a separate thread in postcopy since the 1st day postcopy was introduced, however that requires a separate state transition from precopy loading all devices first, which still happens in the main thread of a coroutine.
This patch tries to move the migration incoming side to be run inside a separate thread (mig/dst/main) just like the src (mig/src/main). The entrance to be
migration_incoming_thread().Quite a few things are needed to make it fly.. One note here is we need to change all these things in one patch to not break anything. The other way to do this is add code to make all paths (that this patch touched) be ready for either coroutine or thread. That may cause confusions in another way. So reviewers, please take my sincere apology on the hardness of reviewing this patch: it covers a few modules at the same time, and with some risky changes.
BQL Analysis
============Quoted 8 lines
Firstly, when moving it over to the thread, it means the thread cannot take BQL during the whole process of loading anymore, because otherwise it can block main thread from using the BQL for all kinds of other concurrent tasks (for example, processing QMP / HMP commands).
Here the first question to ask is: what needs BQL during precopy load, and what doesn't?
I just noticed that the BQL held at
process_incoming_migration_cois also responsible for stoppingqmp_migrate_set_capabilitiesfrom being dispatched.Quoted 7 lines
Quoted 2 lines
I don't know if it is by design, or even if it will be guaranteed to work..
Regardless, we shouldn't rely on the BQL for this. The BQL should be left as last resort for things that interact across subsystems. If someone is issuing a migration command during a migration, the migration code is exquisitely positioned to handle that itself.
Yes I agree.
Quoted 52 lines
Quoted 28 lines
Consider the migration incoming rocoutine runs into
qemu_get_byte(), and then proactively yield the migration coroutine (qemu_coroutine_yield()) when the incoming port is blocked on read..AFAIU, a proper fix for that (note, this will currently break tests) is:
bool
migration_is_running(void)
{(quoted diff) +14
-3- MigrationState *s = current_migration; + MigrationStatus state; - if (!s) { - return false; + if (runstate_check(RUN_STATE_INMIGRATE)) { + MigrationIncomingState *mis = migration_incoming_get_current(); + + if (!mis) { + return false; + } + + state = mis->state; + } else { + MigrationState *s = migrate_get_current(); + + if (!s) { + return false;Review
Quoted 10 lines
(quoted diff) +4
-1+ } + + state = s->state; } - switch (s->state) { + switch (state) { case MIGRATION_STATUS_ACTIVE: case MIGRATION_STATUS_POSTCOPY_DEVICE: case MIGRATION_STATUS_POSTCOPY_ACTIVE:LGTM
Quoted 11 lines
Quoted 10 lines
Any point during incoming migration when BQL is unlocked we have a window where a capability could be changed. Same for parameters, for that matter.
To make matters worse, the -incoming cmdline will trigger qmp_migrate_incoming->...->migration_transport_compatible early on, but until the channels finally connect and
process_incoming_migration_costarts it's possible to just change a capability in an incompatible way and the transport will never be validated again.Quoted 8 lines
Quoted 4 lines
Right. Above should fix it, but I believe it also means after "-incoming tcp:xxx" (or anything not "defer") we should forbid changing migration caps or params on destination.
Parameters are never forbidden, right? And we cannot forbid them with
is_runningbecause some parameters are allowed to be changed while running.Right, my above statement was definitely inaccurate.
After merging caps and params we only have params. We should only allow some params to be changed anytime. Most of the params (including caps) should not allow changing during migration live on either src/dst.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
I feel we should have a more fine grained way of saying "this option cannot be set at this moment", instead of just using the state as a proxy. States can change, while the fact that from a certain point on, certain options should not be touched anymore doesn't change.
IIUC that's what
migration_is_running()about?At a high level, yes, but I think that's actually a downside of
migration_is_running. It's not explicit.E.g.:
qmp_migrate->migration_transport_compatibleis the first time capabilities are checked in the code. Guess whatmigration_is_runningreturns at that point? (it's false)Quoted 8 lines
If there's ever a change in BQL behavior, there'll be a bug and we'll see another patch extending the scope of
migration_is_running, either adding existing states to it, or as Prasad proposed, adding a new state that is set a little later/earlier.What we technically want is to stop accepting new capabilities as soon as we're about to use them for the first time. Not as soon as (state == ACTIVE || SETUP || ...) which is a more high level definition.
Isn't
migration_incoming_state_setup()almost immediately invoked (aftermigration_transport_compatible()check passed), which will start to make the retval ofmigration_is_running()reflecting the fact?IMHO as long as QMP handlers are run with any lock (currently, BQL), then such layout should keep working? That will make sure QMP "migrate" and "
migrate_set_parameters" be serialized, that's, AFAICT, what we only need.Quoted 11 lines
Quoted 7 lines
It's a matter of if such finer granularity is necessary for us. Let's assume the simple scheme is:
(a) Some params are allowed to be modified anytime, examples, downtime-limit, max-bandwidth, max-postcopy-bandwidth, etc.
What about during
migration_cleanup()or migrate-cancel? Won't there ever be something in the path of setting these params that would require a "healthy" migration?If we'll introduce FAILING, then plus CANCELLING I think we should cover all race against
migration_cleanup()? As long as we'll have both FAILING and CANCELLING to return true formigration_is_running().Quoted 23 lines
Quoted 11 lines
(b) All the rest params and all capabilities are not allowed to be modified when
migration_is_running()returns true (my fixed version above)The whitelisted (a) should really be the smallest set of params, and justified one by one or it should fall into (b).
My hope is the simple scheme should work for us already. After merging caps, it's only about some params that can be set anytime, rest params can only be set when migration is not running.
Sure, I'd just like to avoid adding another set of "if (
s->parameters->has_foobar)" if possible.Quoted 7 lines
Quoted 4 lines
Maybe a little infra like
bdrv_op_is_blocked, i.e, a list of blocked operations. It could be set inqmp_migrateand checked in qmp_set_parameters/caps.Any example you can think of, that above simple scheme won't work for us?
Quoted 7 lines
It works. I think it's fragile due to:
- the reliance on BQL to avoid concurrent invocations;
- the possible addition of new states for unrelated reasons risks regressions and it's just extra work to make sure everything is still blocked at the time it needs to be blocked.
Yes, we'll need to make sure when new states introduced it needs to report correctly in
migration_is_running(). I think it's not an issue because we need to do it right with/without relying on the function to ban updates on caps/params.. It always must be done right asmigration_is_running()is also used in other use cases.Quoted 59 lines
- any bugs need to be fixed by moving states around, which is dubious since the migration flow itself (which the states should represent) has not changed in a while.
We're still seeing patches such as
dc487044d5("migration: Makemigration_has_failed()work even for CANCELLING") and the recent patch from Prasad. So there is something changing elsewhere and exposing these bugs and it's not immediately clear what that something is (or else we would have avoided the bug).To be clear, I'm not asking we solve these issues at this moment, it's fine if we go forward with the simple scheme. I'm just trying to clarify what I think the problems are.
Quoted 28 lines
Quoted 28 lines
Quoted 11 lines
As discussed above, that'll at least break our qtests. But frankly speaking I think that's the right thing to do.. I hope libvirt always works with "defer" and never update any caps/params after QMP
migrate_incoming.So I wonder if I should continue with above patch, and then fix our qtests. Your work from the other "merge caps+params" might also work here, actually, if we make sure everything will be set alone with the QMP
migrate_incomingsingle command.For incoming, yes. And this is maybe a point in favor of adding the 'config'.
For outgoing, there's still the point I mentioned above about how to restrict _some_ options to be allowed at runtime and others not.
Quoted 2 lines
Let me know your initial thoughts, then I'll see what I can do..
We should fix the bug, I think your patch is good for that.
Although this kind of overlaps with some things we've been discussing with Prasad. I'd be super happy if the code magically stopped using QAPI's MigrationStatus for internal tracking of migration state and blocking of commands and so on.
Quoted 8 lines
Quoted 1 line
Whatever comes first =)
I didn't follow as closely on the discussion there. I don't know if changing MigrationStatus is a good idea.. we should have some really good reason to make libvirt and all mgmt need a change.. but maybe I misread the discussion. I can wait until I read something solid if Prasad is going to propose something.
We're moving towards decoupling MigrationStatus from these ordinary code checks. MigrationStatus should be used for informing management about a relevant change in the migration stage of progression.
As it stands, we're either "exposing implementation details" or "inadvertently changing the API". Both are undesirable.
Quoted 31 lines
Quoted 3 lines
Of course, we might introduce the "internal state is off-sync with externally visible states" issue, but hopefully we'll catch that during review. =)
I need to confess I'm still a bit lost on what it is about, and what is the problem we're solving here.. But I can always wait and read the patches when they come up.. :)
Quoted 23 lines
Quoted 13 lines
Quoted 13 lines
---
Side note, did we ever discuss something like this?struct MigrationState { <state> union { <outgoing> <incoming> } }Quoted 7 lines
Quoted 1 line
there's so much stuff in these structs...
Yeah.. When merging state, we'll also need to be careful on overlapped fields, e.g. expecting a COMPLETED state (from a completed incoming migration) when starting a new outgoing migration.
We can likely leave this for later.
No worries, I'm just collecting your opinions on it.
Another one for the pile:
#5 0x00007f0beda4fb32 in __assert_fail (assertion=0x55b8c3ed8cb8 "mode >= 0 ...) at assert.c:103 #6 0x000055b8c3a1d7a9 in migrate_mode () at ../migration/options.c:882 #7 0x000055b8c3a1084e in fill_source_migration_info (info=0x55b8f1291650) at ../migration/migration.c:1322 #8 0x000055b8c3a10cae in qmp_query_migrate (errp=0x7fff5742ae80) at ../migration/migration.c:1438 #9 0x000055b8c3d4bc2a in qmp_marshal_query_migrate (args=0x55b8f11f2280, ret=0x7f0becd25da8, errp=0x7f0becd25da0) at qapi/qapi-commands-migration.c:48
#10 0x000055b8c3d9a13b in do_qmp_dispatch_bh (opaque=0x7f0becd25e40) at ../qapi/qmp-dispatch.c:128 #11 0x000055b8c3dc6366 in aio_bh_call (bh=0x55b8f12922d0) at ../util/async.c:173 #12 0x000055b8c3dc6482 in aio_bh_poll (ctx=0x55b8f10741e0) at ../util/async.c:220 #13 0x000055b8c3da9832 in aio_poll (ctx=0x55b8f10741e0, blocking=false) at ../util/aio-posix.c:719 #14 0x000055b8c3cea017 in monitor_cleanup () at ../monitor/monitor.c:676
#15 0x000055b8c39ef646 in qemu_cleanup (status=0) at ../system/runstate.c:999
#16 0x000055b8c3cec38e in qemu_default_main (opaque=0x0) at ../system/main.c:51 #17 0x000055b8c3cec430 in main (argc=33, argv=0x7fff5742b208) at #../system/main.c:93
(gdb) p/x mode
$8 = 0xcccccccc
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 17 lines · Fabiano Rosas
Another one for the pile:
#5 0x00007f0beda4fb32 in __assert_fail (assertion=0x55b8c3ed8cb8 "mode >= 0 ...) at assert.c:103 #6 0x000055b8c3a1d7a9 in migrate_mode () at ../migration/options.c:882 #7 0x000055b8c3a1084e in fill_source_migration_info (info=0x55b8f1291650) at ../migration/migration.c:1322 #8 0x000055b8c3a10cae in qmp_query_migrate (errp=0x7fff5742ae80) at ../migration/migration.c:1438 #9 0x000055b8c3d4bc2a in qmp_marshal_query_migrate (args=0x55b8f11f2280, ret=0x7f0becd25da8, errp=0x7f0becd25da0) at qapi/qapi-commands-migration.c:48#10
0x000055b8c3d9a13bindo_qmp_dispatch_bh(opaque=0x7f0becd25e40) at../qapi/qmp-dispatch.c:128#110x000055b8c3dc6366inaio_bh_call(bh=0x55b8f12922d0) at../util/async.c:173#120x000055b8c3dc6482inaio_bh_poll(ctx=0x55b8f10741e0) at../util/async.c:220#130x000055b8c3da9832inaio_poll(ctx=0x55b8f10741e0, blocking=false) at../util/aio-posix.c:719#140x000055b8c3cea017inmonitor_cleanup() at../monitor/monitor.c:676
#150x000055b8c39ef646inqemu_cleanup(status=0) at../system/runstate.c:999
#160x000055b8c3cec38einqemu_default_main(opaque=0x0) at../system/main.c:51#170x000055b8c3cec430in main (argc=33, argv=0x7fff5742b208) at #../system/main.c:93
Quoted 2 lines
(gdb) p/x mode
$8 =0xcccccccc
What's the reproducer? Is it easy to reproduce?
I wonder if current_migration released already, or if monitor should still process any QMP handler if the VM is shutting down..
Is this only happening after this series applied? I can't yet see how the threadify affected it..
Signature · 1 line
Peter Xu
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 18 lines · Peter Xu
Quoted 17 lines · Fabiano Rosas
Another one for the pile:
#5 0x00007f0beda4fb32 in __assert_fail (assertion=0x55b8c3ed8cb8 "mode >= 0 ...) at assert.c:103 #6 0x000055b8c3a1d7a9 in migrate_mode () at ../migration/options.c:882 #7 0x000055b8c3a1084e in fill_source_migration_info (info=0x55b8f1291650) at ../migration/migration.c:1322 #8 0x000055b8c3a10cae in qmp_query_migrate (errp=0x7fff5742ae80) at ../migration/migration.c:1438 #9 0x000055b8c3d4bc2a in qmp_marshal_query_migrate (args=0x55b8f11f2280, ret=0x7f0becd25da8, errp=0x7f0becd25da0) at qapi/qapi-commands-migration.c:48#10
0x000055b8c3d9a13bindo_qmp_dispatch_bh(opaque=0x7f0becd25e40) at../qapi/qmp-dispatch.c:128#110x000055b8c3dc6366inaio_bh_call(bh=0x55b8f12922d0) at../util/async.c:173#120x000055b8c3dc6482inaio_bh_poll(ctx=0x55b8f10741e0) at../util/async.c:220#130x000055b8c3da9832inaio_poll(ctx=0x55b8f10741e0, blocking=false) at../util/aio-posix.c:719#140x000055b8c3cea017inmonitor_cleanup() at../monitor/monitor.c:676
#150x000055b8c39ef646inqemu_cleanup(status=0) at../system/runstate.c:999
#160x000055b8c3cec38einqemu_default_main(opaque=0x0) at../system/main.c:51#170x000055b8c3cec430in main (argc=33, argv=0x7fff5742b208) at #../system/main.c:93
Quoted 5 lines
Quoted 2 lines
(gdb) p/x mode
$8 =0xccccccccWhat's the reproducer? Is it easy to reproduce?
Just hammering some make check instances in parallel, as usual.
Quoted 3 lines
I wonder if
current_migrationreleased already, or if monitor should still process any QMP handler if the VM is shutting down..
monitor_cleanup says it will continue dispatching and just never send a response to the command. It seems migration will need it's own way of tracking this.
Maybe as a first step we could clear current_migration at migration_instance_finalize().
(I see device-introspect-test doesn't like it. I'll look into it)
Quoted 2 lines
Is this only happening after this series applied? I can't yet see how the threadify affected it..
No, sorry, I posted here because I think this affects this series' assumptions about the BQL.
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
Quoted 40 lines · Fabiano Rosas
Quoted 18 lines · Peter Xu
Quoted 17 lines · Fabiano Rosas
Another one for the pile:
#5 0x00007f0beda4fb32 in __assert_fail (assertion=0x55b8c3ed8cb8 "mode >= 0 ...) at assert.c:103 #6 0x000055b8c3a1d7a9 in migrate_mode () at ../migration/options.c:882 #7 0x000055b8c3a1084e in fill_source_migration_info (info=0x55b8f1291650) at ../migration/migration.c:1322 #8 0x000055b8c3a10cae in qmp_query_migrate (errp=0x7fff5742ae80) at ../migration/migration.c:1438 #9 0x000055b8c3d4bc2a in qmp_marshal_query_migrate (args=0x55b8f11f2280, ret=0x7f0becd25da8, errp=0x7f0becd25da0) at qapi/qapi-commands-migration.c:48#10
0x000055b8c3d9a13bindo_qmp_dispatch_bh(opaque=0x7f0becd25e40) at../qapi/qmp-dispatch.c:128#110x000055b8c3dc6366inaio_bh_call(bh=0x55b8f12922d0) at../util/async.c:173#120x000055b8c3dc6482inaio_bh_poll(ctx=0x55b8f10741e0) at../util/async.c:220#130x000055b8c3da9832inaio_poll(ctx=0x55b8f10741e0, blocking=false) at../util/aio-posix.c:719#140x000055b8c3cea017inmonitor_cleanup() at../monitor/monitor.c:676
#150x000055b8c39ef646inqemu_cleanup(status=0) at../system/runstate.c:999
#160x000055b8c3cec38einqemu_default_main(opaque=0x0) at../system/main.c:51#170x000055b8c3cec430in main (argc=33, argv=0x7fff5742b208) at #../system/main.c:93Quoted 5 lines
Quoted 2 lines
(gdb) p/x mode
$8 =0xccccccccWhat's the reproducer? Is it easy to reproduce?
Just hammering some make check instances in parallel, as usual.
Quoted 3 lines
I wonder if
current_migrationreleased already, or if monitor should still process any QMP handler if the VM is shutting down..
monitor_cleanupsays it will continue dispatching and just never send a response to the command. It seems migration will need it's own way of tracking this.Maybe as a first step we could clear
current_migrationatmigration_instance_finalize().
(I see device-introspect-test doesn't like it. I'll look into it)
Quoted 5 lines
Quoted 2 lines
Is this only happening after this series applied? I can't yet see how the threadify affected it..
No, sorry, I posted here because I think this affects this series' assumptions about the BQL.
Aha, you reminded me on this work, remember to check it out (I forgot most of it.. as usual..):
https://lore.kernel.org/qemu-devel/[email protected]
I also recall besides the singleton idea, we can also take a mutex and release current_migration at finalize(), then fetch current_migration will instead need to take a refcount and also with mutex held.
Anyway, thanks for looking into it. Now, it's all yours. :)
Signature · 1 line
Peter Xu
Re: [PATCH 08/13] migration: Thread-ify precopy vmstate load process
In reply to Peter XuQuoted 104 lines · On Wed, 22 Oct 2025 15:26:07 -0400 Peter Xu <[email protected]> wrote:
Migration module was there for 10+ years. Initially, it was in most cases based on coroutines. As more features were added into the framework, like postcopy, multifd, etc.. it became a mixture of threads and coroutines.
I'm guessing coroutines just can't fix all issues that migration want to resolve.
After all these years, migration is now heavily based on a threaded model.
Now there's still a major part of migration framework that is still not thread-based, which is precopy load. We do load in a separate thread in postcopy since the 1st day postcopy was introduced, however that requires a separate state transition from precopy loading all devices first, which still happens in the main thread of a coroutine.
This patch tries to move the migration incoming side to be run inside a separate thread (mig/dst/main) just like the src (mig/src/main). The entrance to be
migration_incoming_thread().Quite a few things are needed to make it fly.. One note here is we need to change all these things in one patch to not break anything. The other way to do this is add code to make all paths (that this patch touched) be ready for either coroutine or thread. That may cause confusions in another way. So reviewers, please take my sincere apology on the hardness of reviewing this patch: it covers a few modules at the same time, and with some risky changes.
BQL Analysis
============Firstly, when moving it over to the thread, it means the thread cannot take BQL during the whole process of loading anymore, because otherwise it can block main thread from using the BQL for all kinds of other concurrent tasks (for example, processing QMP / HMP commands).
Here the first question to ask is: what needs BQL during precopy load, and what doesn't?
Most of the load process shouldn't need BQL, especially when it's about RAM. After all, RAM is still the major chunk of data to move for a live migration process. VFIO started to change that, though, but still, VFIO is per-device so that shouldn't need BQL either in most cases.
Generic device loads will need BQL, likely not when receiving VMSDs, but when applying them. One example is any
post_load()could potentially inject memory regions causing memory transactions to happen. That'll need to update the global address spaces, hence requires BQL. The other one is CPU sync operations, even if the sync alone may not need BQL (which is still to be further justified),run_on_cpu()will need it.For that,
qemu_loadvm_state()andqemu_loadvm_state_main()functions need to now take a "bql_held" parameter saying whether bql is held. We could use things likeBQL_LOCK_GUARD(), but this patch goes with explicit lockings rather than relying onbql_lockedTLS variable. In case of migration, we always know whether BQL is held in different context as long as we can still pass that information downwards.COLO
====COLO assumed the dest VM load happens in a coroutine. After this patch, it's not anymore. Change that by invoking
colo_incoming_co()directly from themigration_incoming_thread().The name (
colo_incoming_co()) isn't proper anymore. Change it tocolo_incoming_wait(), removing the coroutine annotation alongside.Remove all the
bql_lock()implications in COLO, e.g.,colo_incoming_co()used to release the lock for a short period whilejoin(). Now it's not needed. Instead, taking BQL but only when needed (colo_release_ram_cache).At the meantime, there's
colo_incoming_covariable that used to store the COLO incoming coroutine, only to be kicked off when a secondary failover happens.To recap, what should happen for such failover should be (taking example of a QMP command x-colo-lost-heartbeat triggering on dest QEMU):
- The QMP command will kick off both the coroutine and the COLO
thread (colo_process_incoming_thread()), with something like: /* Notify COLO incoming thread that failover work is finished */ qemu_event_set(&mis->colo_incoming_event); qemu_coroutine_enter(mis->colo_incoming_co);- The coroutine, which yielded itself before, now resumes after
enter(),then it'll wait for the join(): mis->colo_incoming_co = qemu_coroutine_self(); qemu_coroutine_yield(); mis->colo_incoming_co = NULL; /* Wait checkpoint incoming thread exit before free resource */ qemu_thread_join(&th);Here, when switching to a thread model, it should be fine removing
colo_incoming_covariable completely, because if so, the incoming thread will (instead of yielding the coroutine) wait atqemu_thread_join()until the colo thread completes execution (after receivingcolo_incoming_event).[...]
Signed-off-by Peter Xu <[email protected]>
Quoted 11 lines
--- include/migration/colo.h | 6 ++-- migration/migration.h | 14 +++----- migration/colo-stubs.c | 2 +- migration/colo.c | 24 ++++--------- migration/migration.c | 77 +++++++++++++++++++++++++--------------- migration/rdma.c | 34 +----------------- migration/savevm.c | 8 ++--- migration/trace-events | 4 +-- 8 files changed, 69 insertions(+), 100 deletions(-)
Looks good to me, you got the COLO parts exactly right. Thank you for putting so much effort in this.
Best Regards,
Lukas Straub
PATCH 9/13 migration/rdma: Remove coroutine path in qemu_rdma_wait_comp_channel
In reply to Peter XuNow after thread-ified dest VM load during precopy, we will always in a thread context rather than within a coroutine. We can remove this path now.
--- migration/rdma.c | 90 ++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 49 deletions(-)
migration/rdma.c +41 -49
@@ -29,7 +29,6 @@#include "qemu/rcu.h" #include "qemu/sockets.h" #include "qemu/bitmap.h" -#include "qemu/coroutine.h" #include "system/memory.h" #include <sys/socket.h> #include <netdb.h>
@@ -1345,61 +1344,54 @@ static int qemu_rdma_wait_comp_channel(RDMAContext *rdma, {struct rdma_cm_event *cm_event; -if (qemu_in_coroutine ()) { -yield_until_fd_readable (comp_channel->fd); - }else { -/* This is the source side, we're in a separate thread -* or destination prior to migration_fd_process_incoming() -* after postcopy, the destination also in a separate thread. -* we can't yield; so we have to poll the fd. -* But we need to be able to handle 'cancel' or an error -* without hanging forever. -*/ -while (!rdma->errored && !rdma->received_error) { -GPollFD pfds[2 ]; - pfds[0 ].fd = comp_channel->fd; - pfds[0 ].events =G_IO_IN |G_IO_HUP |G_IO_ERR ; - pfds[0 ].revents =0 ; - - pfds[1 ].fd = rdma->channel->fd; - pfds[1 ].events =G_IO_IN |G_IO_HUP |G_IO_ERR ; - pfds[1 ].revents =0 ; - -/* 0.1s timeout, should be fine for a 'cancel' */ -switch (qemu_poll_ns (pfds,2 ,100 *1000 *1000 )) { -case 2 : -case 1 :/* fd active */ -if (pfds[0 ].revents) { -return 0 ; - } +/* +* This is the source or dest side, either during precopy or +* postcopy. We're always in a separate thread when reaching here. +* Poll the fd. We need to be able to handle 'cancel' or an error +* without hanging forever. +*/ +while (!rdma->errored && !rdma->received_error) { +GPollFD pfds[2 ]; + pfds[0 ].fd = comp_channel->fd; + pfds[0 ].events =G_IO_IN |G_IO_HUP |G_IO_ERR ; + pfds[0 ].revents =0 ; + + pfds[1 ].fd = rdma->channel->fd; + pfds[1 ].events =G_IO_IN |G_IO_HUP |G_IO_ERR ; + pfds[1 ].revents =0 ; + +/* 0.1s timeout, should be fine for a 'cancel' */ +switch (qemu_poll_ns (pfds,2 ,100 *1000 *1000 )) { +case 2 : +case 1 :/* fd active */ +if (pfds[0 ].revents) { +return 0 ; + } -if (pfds[1 ].revents) { -if (rdma_get_cm_event (rdma->channel, &cm_event) <0 ) { -return -1 ; - } +if (pfds[1 ].revents) { +if (rdma_get_cm_event (rdma->channel, &cm_event) <0 ) { +return -1 ; + } -if (cm_event->event ==RDMA_CM_EVENT_DISCONNECTED || - cm_event->event ==RDMA_CM_EVENT_DEVICE_REMOVAL ) { -rdma_ack_cm_event (cm_event); -return -1 ; - } +if (cm_event->event ==RDMA_CM_EVENT_DISCONNECTED || + cm_event->event ==RDMA_CM_EVENT_DEVICE_REMOVAL ) {rdma_ack_cm_event (cm_event); +return -1 ; } -break ; +rdma_ack_cm_event (cm_event); + } +break ; -case 0 :/* Timeout, go around again */ -break ; +case 0 :/* Timeout, go around again */ +break ; -default :/* Error of some type - -* I don't trust errno from qemu_poll_ns -*/ -return -1 ; - } +default :/* Error of some type - don't trust errno from qemu_poll_ns */ +return -1 ; + } -if (migrate_get_current ()->state ==MIGRATION_STATUS_CANCELLING ) { -/* Bail out and let the cancellation happen */ -return -1 ; - } +if (migrate_get_current ()->state ==MIGRATION_STATUS_CANCELLING ) { +/* Bail out and let the cancellation happen */ +return -1 ; } }
Signature · 1 line
2.50.1
PATCH 10/13 migration/postcopy: Remove workaround on wait preempt channel
In reply to Peter XuThis reverts commit 7afbdada7effbc2b97281bfbce0c6df351a3cf88.
Now after switching to a thread in loadvm process, the main thread should be able to accept() even if loading the package could cause a page fault in userfaultfd path.
--- migration/savevm.c | 21 --------------------- 1 file changed, 21 deletions(-)
migration/savevm.c +0 -21
@@ -2452,27 +2452,6 @@ static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis,QEMUFile *packf =qemu_file_new_input (QIO_CHANNEL (bioc)); -/* -* Before loading the guest states, ensure that the preempt channel has -* been ready to use, as some of the states (e.g. via virtio_load) might -* trigger page faults that will be handled through the preempt channel. -* So yield to the main thread in the case that the channel create event -* hasn't been dispatched. -* -* TODO: if we can move migration loadvm out of main thread, then we -* won't block main thread from polling the accept() fds. We can drop -* this as a whole when that is done. -*/ -do { -if (!migrate_postcopy_preempt () || !qemu_in_coroutine () || - mis->postcopy_qemufile_dst) { -break ; - } - -aio_co_schedule (qemu_get_current_aio_context (),qemu_coroutine_self ()); -qemu_coroutine_yield (); - }while (1 ); - ret =qemu_loadvm_state_main (packf, mis, bql_held, errp);trace_loadvm_handle_cmd_packaged_main (ret);qemu_fclose (packf);
Signature · 1 line
2.50.1
PATCH 11/13 migration/ram: Remove workaround on ram yield during load
In reply to Peter XuThis reverts e65cec5e5d97927d22b39167d3e8edeffc771788.
RAM load path had a hack in the past explicitly yield the thread to the main coroutine when RAM load spinning in a tight loop. Not needed now because precopy RAM load now happens without the main thread.
--- migration/ram.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
migration/ram.c +1 -12
@@ -4304,7 +4304,7 @@ static int parse_ramblocks(QEMUFile *f, ram_addr_t total_ram_bytes)static int ram_load_precopy (QEMUFile *f) {MigrationIncomingState *mis =migration_incoming_get_current (); -int flags =0 , ret =0 , invalid_flags =0 , i =0 ; +int flags =0 , ret =0 , invalid_flags =0 ;if (migrate_mapped_ram ()) { invalid_flags |= (RAM_SAVE_FLAG_HOOK | RAM_SAVE_FLAG_MULTIFD_FLUSH |
@@ -4317,17 +4317,6 @@ static int ram_load_precopy(QEMUFile *f)void *host =NULL , *host_bak =NULL ;uint8_t ch; -/* -* Yield periodically to let main loop run, but an iteration of -* the main loop is expensive, so do it each some iterations -*/ -if ((i &32767 ) ==0 &&qemu_in_coroutine ()) { -aio_co_schedule (qemu_get_current_aio_context (), -qemu_coroutine_self ()); -qemu_coroutine_yield (); - } - i++; - addr =qemu_get_be64 (f); ret =qemu_file_get_error (f);if (ret) {
Signature · 1 line
2.50.1
PATCH 12/13 migration: Allow blocking mode for incoming live migration
In reply to Peter XuWe used to set nonblocking mode for incoming live migration. It was required because while running inside a coroutine we cannot block in a syscall. Instead of that, when a syscall needs blocking, we need to register to the iochannel's GIO events and proactively yield the coroutine so as to make sure other things can keep running on the QEMU main thread. When the migration channel has pending data to read, QEMU main thread will re-enter the blocked incoming migration coroutine.
Now, incoming migration is completely run inside a thread now. We can safely switch to blocking mode for the main incoming channel.
It's still slightly more efficient to use blocking mode than nonblocking, because QEMU can avoid creating temporary mainloops and avoid the extra poll() syscall. Now it's safe to directly block in the iochannel's syscall (e.g. io_readv()).
--- migration/migration.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
migration/migration.c +3 -1
@@ -973,7 +973,9 @@ static void migration_incoming_setup(QEMUFile *f)assert (!mis->from_src_file); mis->from_src_file = f; -qemu_file_set_blocking (f, false, &error_abort); + +/* Incoming migration runs in a thread now, nonblocking is not needed */ +qemu_file_set_blocking (f, true, &error_abort); }void migration_incoming_process (void )
Signature · 1 line
2.50.1
PATCH 13/13 migration/vfio: Drop BQL dependency for loadvm SWITCHOVER_START
In reply to Peter XuThat was there only because we used to do loadvm in a coroutine on the main thread with BQL held, while there was a locking order issue.
Now make the API to not depend on BQL anymore. Instead making sure we don't take BQL invoking switchover_start() hooks.
--- hw/vfio/migration-multifd.c | 3 --- migration/savevm.c | 3 +-- 2 files changed, 1 insertion(+), 5 deletions(-)
hw/vfio/migration-multifd.c +0 -3
@@ -766,13 +766,10 @@ int vfio_multifd_switchover_start(VFIODevice *vbasedev)assert (multifd); -/* The lock order is load_bufs_mutex -> BQL so unlock BQL here first */ -bql_unlock (); WITH_QEMU_LOCK_GUARD(&multifd->load_bufs_mutex) {assert (!multifd->load_bufs_thread_running); multifd->load_bufs_thread_running = true; } -bql_lock ();qemu_loadvm_start_load_thread (vfio_load_bufs_thread, vbasedev);
migration/savevm.c +1 -2
@@ -2648,8 +2648,7 @@ static int loadvm_process_command(QEMUFile *f, bool bql_held, Error **errp)return loadvm_process_enable_colo (mis, errp);case MIG_CMD_SWITCHOVER_START : - WITH_BQL_HELD(bql_held) { -/* TODO: drop the BQL dependency */ + WITH_BQL_RELEASED(bql_held) { ret =loadvm_postcopy_handle_switchover_start (errp); }return ret;
Signature · 1 line
2.50.1
Quoted 10 lines · Peter Xu
This is v1, however not 10.2 material. The earliest I see fit would still be 11.0+ even if everything goes extremely smooth.
Removal of RFC is only about that I'm more confident this should be able to land without breaking something too easily, as I smoked it slightly more cross-archs this time. AFAIU the best (and possibly only..) way to prove it solid is to merge it.. likely in the early phase of a dev cycle.
The plan is we'll try to get to more device setups too soon, before it could land.
I forgot to attach a changelog, sorry, here it is:
rfc->v1:
- Collected tags
- Try to split the major patch 5 to smaller one [Dave]
- One with WITH_BQL_*() macros, rewritten to allow internal "return" or nesting [Vladimir]
- One patch "migration: Pass in bql_held information from qemu_loadvm_state()", the patch itself contain no functional change. However it should contain all changes relevant to BQL, hence maybe it would help review.
- One patch contains the rest that need to be in one patch.
- Refine commit message for patch "migration/rdma: Change io_create_watch() to return immediately" [Fabiano]
- Pre-requisite patch to rework migration_channel_read_peek(), removing the sleep functions [Fabiano]
- Added patch "io: Add qio_channel_wait_cond() helper"
- Added patch "migration: Properly wait on G_IO_IN when peeking messages"
- Squashed patch "migration/rdma: Remove rdma_cm_poll_handler" into patch "migration: Thread-ify precopy vmstate load process", otherwise the patch when split can be racy.
- Changed the vfio patch to be at the end, instead of adding dead code directly remove the bql lock/unlock, making sure that API is invoked without BQL instead.
- Added one patch "migration: Allow blocking mode for incoming live migration", so that after the threadify work switching incoming main channel to be in blocking mode. Reason in commit message.
It may not be complete, so there can be small touch ups here and there that are not mentioned.
Signature · 1 line
Peter Xu
Quoted 82 lines · On Wed, 22 Oct 2025 15:25:59 -0400 Peter Xu <[email protected]> wrote:
This is v1, however not 10.2 material. The earliest I see fit would still be 11.0+ even if everything goes extremely smooth.
Removal of RFC is only about that I'm more confident this should be able to land without breaking something too easily, as I smoked it slightly more cross-archs this time. AFAIU the best (and possibly only..) way to prove it solid is to merge it.. likely in the early phase of a dev cycle.
The plan is we'll try to get to more device setups too soon, before it could land.
Background
==========Nowadays, live migration heavily depends on threads. For example, most of the major features that will be used nowadays in live migration (multifd, postcopy, mapped-ram, vfio, etc.) all work with threads internally.
But still, from time to time, we'll see some coroutines floating around the migration context. The major one is precopy's loadvm, which is internally a coroutine. It is still a critical path that any live migration depends on.
A mixture of using both coroutines and threads is prone to issues. Some examples can refer to commit
e65cec5e5d("migration/ram: Yield periodically to the main loop") or commit7afbdada7e("migration/postcopy: ensure preempt channel is ready before loading states").It was a coroutine since this work (thanks to Fabiano, the archeologist, digging the link):
https://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01136.html
[...]
Tests
=====Default CI passes.
RDMA unit tests pass as usual. I also tried out cancellation / failure tests over RDMA channels, making sure nothing is stuck.
I also roughly measured how long it takes to run the whole 80+ migration qtest suite, and see no measurable difference before / after this series.
I didn't test COLO, I wanted to but the doc example didn't work.
Risks
=====This series has the risk of breaking things. I would be surprised if it didn't..
The current way of taking BQL during FULL section load may cause issues, it means when the IOs are unstable we could be waiting for IO (in the new migration incoming thread) with BQL held. This is low possibility, though, only happens when the network halts during flushing the device states. However still possible. One solution is to further breakdown the BQL critical sections to smaller sections, as mentioned in TODO.
Anything more than welcomed: suggestions, questions, objections, tests..
TODO
====- Finer grained BQL breakdown
Peter Xu (13):
io: Addqio_channel_wait_cond()helper
migration: Properly wait onG_IO_INwhen peeking messages migration/rdma: Fix wrong context inqio_channel_rdma_shutdown()migration/rdma: Allowqemu_rdma_wait_comp_channelwork with thread migration/rdma: Changeio_create_watch()to return immediately
migration: IntroduceWITH_BQL_HELD()/WITH_BQL_RELEASED()
migration: Pass inbql_heldinformation fromqemu_loadvm_state()
migration: Thread-ify precopy vmstate load process migration/rdma: Remove coroutine path inqemu_rdma_wait_comp_channelmigration/postcopy: Remove workaround on wait preempt channel migration/ram: Remove workaround on ram yield during load
migration: Allow blocking mode for incoming live migration migration/vfio: Drop BQL dependency for loadvmSWITCHOVER_START
Quoted 17 lines
include/io/channel.h | 15 +++ include/migration/colo.h | 6 +- migration/migration.h | 109 +++++++++++++++++-- migration/savevm.h | 4 +- hw/vfio/migration-multifd.c | 3 - io/channel.c | 21 ++-- migration/channel.c | 7 +- migration/colo-stubs.c | 2 +- migration/colo.c | 26 ++--- migration/migration.c | 81 ++++++++------ migration/qemu-file.c | 6 +- migration/ram.c | 13 +-- migration/rdma.c | 204 ++++++++---------------------------- migration/savevm.c | 98 +++++++++-------- migration/trace-events | 4 +- 15 files changed, 291 insertions(+), 308 deletions(-)
Works well in my COLO testing. Fro the whole series:
Quoted 103 lines · Lukas Straub
Quoted 82 lines · On Wed, 22 Oct 2025 15:25:59 -0400 Peter Xu <[email protected]> wrote:
This is v1, however not 10.2 material. The earliest I see fit would still be 11.0+ even if everything goes extremely smooth.
Removal of RFC is only about that I'm more confident this should be able to land without breaking something too easily, as I smoked it slightly more cross-archs this time. AFAIU the best (and possibly only..) way to prove it solid is to merge it.. likely in the early phase of a dev cycle.
The plan is we'll try to get to more device setups too soon, before it could land.
Background
==========Nowadays, live migration heavily depends on threads. For example, most of the major features that will be used nowadays in live migration (multifd, postcopy, mapped-ram, vfio, etc.) all work with threads internally.
But still, from time to time, we'll see some coroutines floating around the migration context. The major one is precopy's loadvm, which is internally a coroutine. It is still a critical path that any live migration depends on.
A mixture of using both coroutines and threads is prone to issues. Some examples can refer to commit
e65cec5e5d("migration/ram: Yield periodically to the main loop") or commit7afbdada7e("migration/postcopy: ensure preempt channel is ready before loading states").It was a coroutine since this work (thanks to Fabiano, the archeologist, digging the link):
https://lists.gnu.org/archive/html/qemu-devel/2012-08/msg01136.html
[...]
Tests
=====Default CI passes.
RDMA unit tests pass as usual. I also tried out cancellation / failure tests over RDMA channels, making sure nothing is stuck.
I also roughly measured how long it takes to run the whole 80+ migration qtest suite, and see no measurable difference before / after this series.
I didn't test COLO, I wanted to but the doc example didn't work.
Risks
=====This series has the risk of breaking things. I would be surprised if it didn't..
The current way of taking BQL during FULL section load may cause issues, it means when the IOs are unstable we could be waiting for IO (in the new migration incoming thread) with BQL held. This is low possibility, though, only happens when the network halts during flushing the device states. However still possible. One solution is to further breakdown the BQL critical sections to smaller sections, as mentioned in TODO.
Anything more than welcomed: suggestions, questions, objections, tests..
TODO
====- Finer grained BQL breakdown
Peter Xu (13):
io: Addqio_channel_wait_cond()helper
migration: Properly wait onG_IO_INwhen peeking messages migration/rdma: Fix wrong context inqio_channel_rdma_shutdown()migration/rdma: Allowqemu_rdma_wait_comp_channelwork with thread migration/rdma: Changeio_create_watch()to return immediately
migration: IntroduceWITH_BQL_HELD()/WITH_BQL_RELEASED()
migration: Pass inbql_heldinformation fromqemu_loadvm_state()
migration: Thread-ify precopy vmstate load process migration/rdma: Remove coroutine path inqemu_rdma_wait_comp_channelmigration/postcopy: Remove workaround on wait preempt channel migration/ram: Remove workaround on ram yield during load
migration: Allow blocking mode for incoming live migration migration/vfio: Drop BQL dependency for loadvmSWITCHOVER_STARTQuoted 17 lines
include/io/channel.h | 15 +++ include/migration/colo.h | 6 +- migration/migration.h | 109 +++++++++++++++++-- migration/savevm.h | 4 +- hw/vfio/migration-multifd.c | 3 - io/channel.c | 21 ++-- migration/channel.c | 7 +- migration/colo-stubs.c | 2 +- migration/colo.c | 26 ++--- migration/migration.c | 81 ++++++++------ migration/qemu-file.c | 6 +- migration/ram.c | 13 +-- migration/rdma.c | 204 ++++++++---------------------------- migration/savevm.c | 98 +++++++++-------- migration/trace-events | 4 +- 15 files changed, 291 insertions(+), 308 deletions(-)
Quoted 3 lines
Works well in my COLO testing. Fro the whole series:
Tested-by Lukas Straub <[email protected]>
Thanks for the testing.
Instead of applying it all over, the major change on COLO is patch 8, I'll move the tag over if no objections.
Signature · 1 line
Peter Xu