* [PATCH 1/8] migration: Fix race that dest preempt thread close too early
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
@ 2023-09-18 17:28 ` Fabiano Rosas
2023-09-18 17:28 ` [PATCH 2/8] migration: Fix possible race when setting rp_state.error Fabiano Rosas
` (10 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Fabiano Rosas @ 2023-09-18 17:28 UTC (permalink / raw)
To: qemu-devel; +Cc: Juan Quintela, Peter Xu, Stefan Hajnoczi, Leonardo Bras
From: Peter Xu <peterx@redhat.com>
We hit intermit CI issue on failing at migration-test over the unit test
preempt/plain:
qemu-system-x86_64: Unable to read from socket: Connection reset by peer
Memory content inconsistency at 5b43000 first_byte = bd last_byte = bc current = 4f hit_edge = 1
**
ERROR:../tests/qtest/migration-test.c:300:check_guests_ram: assertion failed: (bad == 0)
(test program exited with status code -6)
Fabiano debugged into it and found that the preempt thread can quit even
without receiving all the pages, which can cause guest not receiving all
the pages and corrupt the guest memory.
To make sure preempt thread finished receiving all the pages, we can rely
on the page_requested_count being zero because preempt channel will only
receive requested page faults. Note, not all the faulted pages are required
to be sent via the preempt channel/thread; imagine the case when a
requested page is just queued into the background main channel for
migration, the src qemu will just still send it via the background channel.
Here instead of spinning over reading the count, we add a condvar so the
main thread can wait on it if that unusual case happened, without burning
the cpu for no good reason, even if the duration is short; so even if we
spin in this rare case is probably fine. It's just better to not do so.
The condvar is only used when that special case is triggered. Some memory
ordering trick is needed to guarantee it from happening (against the
preempt thread status field), so the main thread will always get a kick
when that triggers correctly.
Closes: https://gitlab.com/qemu-project/qemu/-/issues/1886
Debugged-by: Fabiano Rosas <farosas@suse.de>
Signed-off-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
migration/migration.c | 3 ++-
migration/migration.h | 13 ++++++++++++-
migration/postcopy-ram.c | 38 +++++++++++++++++++++++++++++++++++++-
3 files changed, 51 insertions(+), 3 deletions(-)
diff --git a/migration/migration.c b/migration/migration.c
index d61e572742..3ee1e6b0d6 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -153,6 +153,7 @@ void migration_object_init(void)
qemu_sem_init(¤t_incoming->postcopy_qemufile_dst_done, 0);
qemu_mutex_init(¤t_incoming->page_request_mutex);
+ qemu_cond_init(¤t_incoming->page_request_cond);
current_incoming->page_requested = g_tree_new(page_request_addr_cmp);
migration_object_check(current_migration, &error_fatal);
@@ -367,7 +368,7 @@ int migrate_send_rp_req_pages(MigrationIncomingState *mis,
* things like g_tree_lookup() will return TRUE (1) when found.
*/
g_tree_insert(mis->page_requested, aligned, (gpointer)1);
- mis->page_requested_count++;
+ qatomic_inc(&mis->page_requested_count);
trace_postcopy_page_req_add(aligned, mis->page_requested_count);
}
}
diff --git a/migration/migration.h b/migration/migration.h
index c390500604..cdaa10d515 100644
--- a/migration/migration.h
+++ b/migration/migration.h
@@ -196,7 +196,10 @@ struct MigrationIncomingState {
/* A tree of pages that we requested to the source VM */
GTree *page_requested;
- /* For debugging purpose only, but would be nice to keep */
+ /*
+ * For postcopy only, count the number of requested page faults that
+ * still haven't been resolved.
+ */
int page_requested_count;
/*
* The mutex helps to maintain the requested pages that we sent to the
@@ -210,6 +213,14 @@ struct MigrationIncomingState {
* contains valid information.
*/
QemuMutex page_request_mutex;
+ /*
+ * If postcopy preempt is enabled, there is a chance that the main
+ * thread finished loading its data before the preempt channel has
+ * finished loading the urgent pages. If that happens, the two threads
+ * will use this condvar to synchronize, so the main thread will always
+ * wait until all pages received.
+ */
+ QemuCond page_request_cond;
/*
* Number of devices that have yet to approve switchover. When this reaches
diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
index 29aea9456d..5408e028c6 100644
--- a/migration/postcopy-ram.c
+++ b/migration/postcopy-ram.c
@@ -599,6 +599,30 @@ int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
if (mis->preempt_thread_status == PREEMPT_THREAD_CREATED) {
/* Notify the fast load thread to quit */
mis->preempt_thread_status = PREEMPT_THREAD_QUIT;
+ /*
+ * Update preempt_thread_status before reading count. Note: mutex
+ * lock only provide ACQUIRE semantic, and it doesn't stops this
+ * write to be reordered after reading the count.
+ */
+ smp_mb();
+ /*
+ * It's possible that the preempt thread is still handling the last
+ * pages to arrive which were requested by guest page faults.
+ * Making sure nothing is left behind by waiting on the condvar if
+ * that unlikely case happened.
+ */
+ WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
+ if (qatomic_read(&mis->page_requested_count)) {
+ /*
+ * It is guaranteed to receive a signal later, because the
+ * count>0 now, so it's destined to be decreased to zero
+ * very soon by the preempt thread.
+ */
+ qemu_cond_wait(&mis->page_request_cond,
+ &mis->page_request_mutex);
+ }
+ }
+ /* Notify the fast load thread to quit */
if (mis->postcopy_qemufile_dst) {
qemu_file_shutdown(mis->postcopy_qemufile_dst);
}
@@ -1277,8 +1301,20 @@ static int qemu_ufd_copy_ioctl(MigrationIncomingState *mis, void *host_addr,
*/
if (g_tree_lookup(mis->page_requested, host_addr)) {
g_tree_remove(mis->page_requested, host_addr);
- mis->page_requested_count--;
+ int left_pages = qatomic_dec_fetch(&mis->page_requested_count);
+
trace_postcopy_page_req_del(host_addr, mis->page_requested_count);
+ /* Order the update of count and read of preempt status */
+ smp_mb();
+ if (mis->preempt_thread_status == PREEMPT_THREAD_QUIT &&
+ left_pages == 0) {
+ /*
+ * This probably means the main thread is waiting for us.
+ * Notify that we've finished receiving the last requested
+ * page.
+ */
+ qemu_cond_signal(&mis->page_request_cond);
+ }
}
qemu_mutex_unlock(&mis->page_request_mutex);
mark_postcopy_blocktime_end((uintptr_t)host_addr);
--
2.35.3
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 2/8] migration: Fix possible race when setting rp_state.error
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
2023-09-18 17:28 ` [PATCH 1/8] migration: Fix race that dest preempt thread close too early Fabiano Rosas
@ 2023-09-18 17:28 ` Fabiano Rosas
2023-09-18 17:28 ` [PATCH 3/8] migration: Fix possible races when shutting down the return path Fabiano Rosas
` (9 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Fabiano Rosas @ 2023-09-18 17:28 UTC (permalink / raw)
To: qemu-devel; +Cc: Juan Quintela, Peter Xu, Stefan Hajnoczi, Leonardo Bras
We don't need to set the rp_state.error right after a shutdown because
qemu_file_shutdown() always sets the QEMUFile error, so the return
path thread would have seen it and set the rp error itself.
Setting the error outside of the thread is also racy because the
thread could clear it after we set it.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
migration/migration.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/migration/migration.c b/migration/migration.c
index 3ee1e6b0d6..d426b69ada 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -2074,7 +2074,6 @@ static int await_return_path_close_on_source(MigrationState *ms)
* waiting for the destination.
*/
qemu_file_shutdown(ms->rp_state.from_dst_file);
- mark_source_rp_bad(ms);
}
trace_await_return_path_close_on_source_joining();
qemu_thread_join(&ms->rp_state.rp_thread);
--
2.35.3
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 3/8] migration: Fix possible races when shutting down the return path
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
2023-09-18 17:28 ` [PATCH 1/8] migration: Fix race that dest preempt thread close too early Fabiano Rosas
2023-09-18 17:28 ` [PATCH 2/8] migration: Fix possible race when setting rp_state.error Fabiano Rosas
@ 2023-09-18 17:28 ` Fabiano Rosas
2023-09-18 17:28 ` [PATCH 4/8] migration: Fix possible race when shutting down to_dst_file Fabiano Rosas
` (8 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Fabiano Rosas @ 2023-09-18 17:28 UTC (permalink / raw)
To: qemu-devel; +Cc: Juan Quintela, Peter Xu, Stefan Hajnoczi, Leonardo Bras
We cannot call qemu_file_shutdown() on the return path file without
taking the file lock. The return path thread could be running it's
cleanup code and have just cleared the from_dst_file pointer.
Checking ms->to_dst_file for errors could also race with
migrate_fd_cleanup() which clears the to_dst_file pointer.
Protect both accesses by taking the file lock.
This was caught by inspection, it should be rare, but the next patches
will start calling this code from other places, so let's do the
correct thing.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
migration/migration.c | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/migration/migration.c b/migration/migration.c
index d426b69ada..15b7258bb2 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -2064,17 +2064,18 @@ static int open_return_path_on_source(MigrationState *ms,
static int await_return_path_close_on_source(MigrationState *ms)
{
/*
- * If this is a normal exit then the destination will send a SHUT and the
- * rp_thread will exit, however if there's an error we need to cause
- * it to exit.
+ * If this is a normal exit then the destination will send a SHUT
+ * and the rp_thread will exit, however if there's an error we
+ * need to cause it to exit. shutdown(2), if we have it, will
+ * cause it to unblock if it's stuck waiting for the destination.
*/
- if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) {
- /*
- * shutdown(2), if we have it, will cause it to unblock if it's stuck
- * waiting for the destination.
- */
- qemu_file_shutdown(ms->rp_state.from_dst_file);
+ WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) {
+ if (ms->to_dst_file && ms->rp_state.from_dst_file &&
+ qemu_file_get_error(ms->to_dst_file)) {
+ qemu_file_shutdown(ms->rp_state.from_dst_file);
+ }
}
+
trace_await_return_path_close_on_source_joining();
qemu_thread_join(&ms->rp_state.rp_thread);
ms->rp_state.rp_thread_created = false;
--
2.35.3
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 4/8] migration: Fix possible race when shutting down to_dst_file
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
` (2 preceding siblings ...)
2023-09-18 17:28 ` [PATCH 3/8] migration: Fix possible races when shutting down the return path Fabiano Rosas
@ 2023-09-18 17:28 ` Fabiano Rosas
2023-09-18 17:28 ` [PATCH 5/8] migration: Remove redundant cleanup of postcopy_qemufile_src Fabiano Rosas
` (7 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Fabiano Rosas @ 2023-09-18 17:28 UTC (permalink / raw)
To: qemu-devel; +Cc: Juan Quintela, Peter Xu, Stefan Hajnoczi, Leonardo Bras
It's not safe to call qemu_file_shutdown() on the to_dst_file without
first checking for the file's presence under the lock. The cleanup of
this file happens at postcopy_pause() and migrate_fd_cleanup() which
are not necessarily running in the same thread as migrate_fd_cancel().
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
migration/migration.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/migration/migration.c b/migration/migration.c
index 15b7258bb2..6e09463466 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -1246,7 +1246,7 @@ static void migrate_fd_error(MigrationState *s, const Error *error)
static void migrate_fd_cancel(MigrationState *s)
{
int old_state ;
- QEMUFile *f = migrate_get_current()->to_dst_file;
+
trace_migrate_fd_cancel();
WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
@@ -1272,11 +1272,13 @@ static void migrate_fd_cancel(MigrationState *s)
* If we're unlucky the migration code might be stuck somewhere in a
* send/write while the network has failed and is waiting to timeout;
* if we've got shutdown(2) available then we can force it to quit.
- * The outgoing qemu file gets closed in migrate_fd_cleanup that is
- * called in a bh, so there is no race against this cancel.
*/
- if (s->state == MIGRATION_STATUS_CANCELLING && f) {
- qemu_file_shutdown(f);
+ if (s->state == MIGRATION_STATUS_CANCELLING) {
+ WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) {
+ if (s->to_dst_file) {
+ qemu_file_shutdown(s->to_dst_file);
+ }
+ }
}
if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) {
Error *local_err = NULL;
@@ -1536,12 +1538,14 @@ void qmp_migrate_pause(Error **errp)
{
MigrationState *ms = migrate_get_current();
MigrationIncomingState *mis = migration_incoming_get_current();
- int ret;
+ int ret = 0;
if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) {
/* Source side, during postcopy */
qemu_mutex_lock(&ms->qemu_file_lock);
- ret = qemu_file_shutdown(ms->to_dst_file);
+ if (ms->to_dst_file) {
+ ret = qemu_file_shutdown(ms->to_dst_file);
+ }
qemu_mutex_unlock(&ms->qemu_file_lock);
if (ret) {
error_setg(errp, "Failed to pause source migration");
--
2.35.3
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 5/8] migration: Remove redundant cleanup of postcopy_qemufile_src
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
` (3 preceding siblings ...)
2023-09-18 17:28 ` [PATCH 4/8] migration: Fix possible race when shutting down to_dst_file Fabiano Rosas
@ 2023-09-18 17:28 ` Fabiano Rosas
2023-09-18 17:28 ` [PATCH 6/8] migration: Consolidate return path closing code Fabiano Rosas
` (6 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Fabiano Rosas @ 2023-09-18 17:28 UTC (permalink / raw)
To: qemu-devel; +Cc: Juan Quintela, Peter Xu, Stefan Hajnoczi, Leonardo Bras
This file is owned by the return path thread which is already doing
cleanup.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
migration/migration.c | 6 ------
1 file changed, 6 deletions(-)
diff --git a/migration/migration.c b/migration/migration.c
index 6e09463466..4372b0fbbf 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -1178,12 +1178,6 @@ static void migrate_fd_cleanup(MigrationState *s)
qemu_fclose(tmp);
}
- if (s->postcopy_qemufile_src) {
- migration_ioc_unregister_yank_from_file(s->postcopy_qemufile_src);
- qemu_fclose(s->postcopy_qemufile_src);
- s->postcopy_qemufile_src = NULL;
- }
-
assert(!migration_is_active(s));
if (s->state == MIGRATION_STATUS_CANCELLING) {
--
2.35.3
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 6/8] migration: Consolidate return path closing code
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
` (4 preceding siblings ...)
2023-09-18 17:28 ` [PATCH 5/8] migration: Remove redundant cleanup of postcopy_qemufile_src Fabiano Rosas
@ 2023-09-18 17:28 ` Fabiano Rosas
2023-09-18 17:28 ` [PATCH 7/8] migration: Replace the return path retry logic Fabiano Rosas
` (5 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Fabiano Rosas @ 2023-09-18 17:28 UTC (permalink / raw)
To: qemu-devel; +Cc: Juan Quintela, Peter Xu, Stefan Hajnoczi, Leonardo Bras
We'll start calling the await_return_path_close_on_source() function
from other parts of the code, so move all of the related checks and
tracepoints into it.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
migration/migration.c | 29 ++++++++++++++---------------
1 file changed, 14 insertions(+), 15 deletions(-)
diff --git a/migration/migration.c b/migration/migration.c
index 4372b0fbbf..f6c0250d33 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -2061,6 +2061,14 @@ static int open_return_path_on_source(MigrationState *ms,
/* Returns 0 if the RP was ok, otherwise there was an error on the RP */
static int await_return_path_close_on_source(MigrationState *ms)
{
+ int ret;
+
+ if (!ms->rp_state.rp_thread_created) {
+ return 0;
+ }
+
+ trace_migration_return_path_end_before();
+
/*
* If this is a normal exit then the destination will send a SHUT
* and the rp_thread will exit, however if there's an error we
@@ -2078,7 +2086,10 @@ static int await_return_path_close_on_source(MigrationState *ms)
qemu_thread_join(&ms->rp_state.rp_thread);
ms->rp_state.rp_thread_created = false;
trace_await_return_path_close_on_source_close();
- return ms->rp_state.error;
+
+ ret = ms->rp_state.error;
+ trace_migration_return_path_end_after(ret);
+ return ret;
}
static inline void
@@ -2374,20 +2385,8 @@ static void migration_completion(MigrationState *s)
goto fail;
}
- /*
- * If rp was opened we must clean up the thread before
- * cleaning everything else up (since if there are no failures
- * it will wait for the destination to send it's status in
- * a SHUT command).
- */
- if (s->rp_state.rp_thread_created) {
- int rp_error;
- trace_migration_return_path_end_before();
- rp_error = await_return_path_close_on_source(s);
- trace_migration_return_path_end_after(rp_error);
- if (rp_error) {
- goto fail;
- }
+ if (await_return_path_close_on_source(s)) {
+ goto fail;
}
if (qemu_file_get_error(s->to_dst_file)) {
--
2.35.3
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 7/8] migration: Replace the return path retry logic
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
` (5 preceding siblings ...)
2023-09-18 17:28 ` [PATCH 6/8] migration: Consolidate return path closing code Fabiano Rosas
@ 2023-09-18 17:28 ` Fabiano Rosas
2023-09-18 17:28 ` [PATCH 8/8] migration: Move return path cleanup to main migration thread Fabiano Rosas
` (4 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Fabiano Rosas @ 2023-09-18 17:28 UTC (permalink / raw)
To: qemu-devel; +Cc: Juan Quintela, Peter Xu, Stefan Hajnoczi, Leonardo Bras
Replace the return path retry logic with finishing and restarting the
thread. This fixes a race when resuming the migration that leads to a
segfault.
Currently when doing postcopy we consider that an IO error on the
return path file could be due to a network intermittency. We then keep
the thread alive but have it do cleanup of the 'from_dst_file' and
wait on the 'postcopy_pause_rp' semaphore. When the user issues a
migrate resume, a new return path is opened and the thread is allowed
to continue.
There's a race condition in the above mechanism. It is possible for
the new return path file to be setup *before* the cleanup code in the
return path thread has had a chance to run, leading to the *new* file
being closed and the pointer set to NULL. When the thread is released
after the resume, it tries to dereference 'from_dst_file' and crashes:
Thread 7 "return path" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffd1dbf700 (LWP 9611)]
0x00005555560e4893 in qemu_file_get_error_obj (f=0x0, errp=0x0) at ../migration/qemu-file.c:154
154 return f->last_error;
(gdb) bt
#0 0x00005555560e4893 in qemu_file_get_error_obj (f=0x0, errp=0x0) at ../migration/qemu-file.c:154
#1 0x00005555560e4983 in qemu_file_get_error (f=0x0) at ../migration/qemu-file.c:206
#2 0x0000555555b9a1df in source_return_path_thread (opaque=0x555556e06000) at ../migration/migration.c:1876
#3 0x000055555602e14f in qemu_thread_start (args=0x55555782e780) at ../util/qemu-thread-posix.c:541
#4 0x00007ffff38d76ea in start_thread (arg=0x7fffd1dbf700) at pthread_create.c:477
#5 0x00007ffff35efa6f in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
Here's the race (important bit is open_return_path happening before
migration_release_dst_files):
migration | qmp | return path
--------------------------+-----------------------------+---------------------------------
qmp_migrate_pause()
shutdown(ms->to_dst_file)
f->last_error = -EIO
migrate_detect_error()
postcopy_pause()
set_state(PAUSED)
wait(postcopy_pause_sem)
qmp_migrate(resume)
migrate_fd_connect()
resume = state == PAUSED
open_return_path <-- TOO SOON!
set_state(RECOVER)
post(postcopy_pause_sem)
(incoming closes to_src_file)
res = qemu_file_get_error(rp)
migration_release_dst_files()
ms->rp_state.from_dst_file = NULL
post(postcopy_pause_rp_sem)
postcopy_pause_return_path_thread()
wait(postcopy_pause_rp_sem)
rp = ms->rp_state.from_dst_file
goto retry
qemu_file_get_error(rp)
SIGSEGV
-------------------------------------------------------------------------------------------
We can keep the retry logic without having the thread alive and
waiting. The only piece of data used by it is the 'from_dst_file' and
it is only allowed to proceed after a migrate resume is issued and the
semaphore released at migrate_fd_connect().
Move the retry logic to outside the thread by waiting for the thread
to finish before pausing the migration.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
migration/migration.c | 60 ++++++++-----------------------------------
migration/migration.h | 1 -
2 files changed, 11 insertions(+), 50 deletions(-)
diff --git a/migration/migration.c b/migration/migration.c
index f6c0250d33..af78f7ee54 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -1787,18 +1787,6 @@ static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname,
}
}
-/* Return true to retry, false to quit */
-static bool postcopy_pause_return_path_thread(MigrationState *s)
-{
- trace_postcopy_pause_return_path();
-
- qemu_sem_wait(&s->postcopy_pause_rp_sem);
-
- trace_postcopy_pause_return_path_continued();
-
- return true;
-}
-
static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name)
{
RAMBlock *block = qemu_ram_block_by_name(block_name);
@@ -1882,7 +1870,6 @@ static void *source_return_path_thread(void *opaque)
trace_source_return_path_thread_entry();
rcu_register_thread();
-retry:
while (!ms->rp_state.error && !qemu_file_get_error(rp) &&
migration_is_setup_or_active(ms->state)) {
trace_source_return_path_thread_loop_top();
@@ -2004,26 +1991,7 @@ retry:
}
out:
- res = qemu_file_get_error(rp);
- if (res) {
- if (res && migration_in_postcopy()) {
- /*
- * Maybe there is something we can do: it looks like a
- * network down issue, and we pause for a recovery.
- */
- migration_release_dst_files(ms);
- rp = NULL;
- if (postcopy_pause_return_path_thread(ms)) {
- /*
- * Reload rp, reset the rest. Referencing it is safe since
- * it's reset only by us above, or when migration completes
- */
- rp = ms->rp_state.from_dst_file;
- ms->rp_state.error = false;
- goto retry;
- }
- }
-
+ if (qemu_file_get_error(rp)) {
trace_source_return_path_thread_bad_end();
mark_source_rp_bad(ms);
}
@@ -2034,8 +2002,7 @@ out:
return NULL;
}
-static int open_return_path_on_source(MigrationState *ms,
- bool create_thread)
+static int open_return_path_on_source(MigrationState *ms)
{
ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file);
if (!ms->rp_state.from_dst_file) {
@@ -2044,11 +2011,6 @@ static int open_return_path_on_source(MigrationState *ms,
trace_open_return_path_on_source();
- if (!create_thread) {
- /* We're done */
- return 0;
- }
-
qemu_thread_create(&ms->rp_state.rp_thread, "return path",
source_return_path_thread, ms, QEMU_THREAD_JOINABLE);
ms->rp_state.rp_thread_created = true;
@@ -2088,6 +2050,7 @@ static int await_return_path_close_on_source(MigrationState *ms)
trace_await_return_path_close_on_source_close();
ret = ms->rp_state.error;
+ ms->rp_state.error = false;
trace_migration_return_path_end_after(ret);
return ret;
}
@@ -2563,6 +2526,13 @@ static MigThrError postcopy_pause(MigrationState *s)
qemu_file_shutdown(file);
qemu_fclose(file);
+ /*
+ * We're already pausing, so ignore any errors on the return
+ * path and just wait for the thread to finish. It will be
+ * re-created when we resume.
+ */
+ await_return_path_close_on_source(s);
+
migrate_set_state(&s->state, s->state,
MIGRATION_STATUS_POSTCOPY_PAUSED);
@@ -2580,12 +2550,6 @@ static MigThrError postcopy_pause(MigrationState *s)
if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) {
/* Woken up by a recover procedure. Give it a shot */
- /*
- * Firstly, let's wake up the return path now, with a new
- * return path channel.
- */
- qemu_sem_post(&s->postcopy_pause_rp_sem);
-
/* Do the resume logic */
if (postcopy_do_resume(s) == 0) {
/* Let's continue! */
@@ -3275,7 +3239,7 @@ void migrate_fd_connect(MigrationState *s, Error *error_in)
* QEMU uses the return path.
*/
if (migrate_postcopy_ram() || migrate_return_path()) {
- if (open_return_path_on_source(s, !resume)) {
+ if (open_return_path_on_source(s)) {
error_setg(&local_err, "Unable to open return-path for postcopy");
migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED);
migrate_set_error(s, local_err);
@@ -3339,7 +3303,6 @@ static void migration_instance_finalize(Object *obj)
qemu_sem_destroy(&ms->rate_limit_sem);
qemu_sem_destroy(&ms->pause_sem);
qemu_sem_destroy(&ms->postcopy_pause_sem);
- qemu_sem_destroy(&ms->postcopy_pause_rp_sem);
qemu_sem_destroy(&ms->rp_state.rp_sem);
qemu_sem_destroy(&ms->rp_state.rp_pong_acks);
qemu_sem_destroy(&ms->postcopy_qemufile_src_sem);
@@ -3359,7 +3322,6 @@ static void migration_instance_init(Object *obj)
migrate_params_init(&ms->parameters);
qemu_sem_init(&ms->postcopy_pause_sem, 0);
- qemu_sem_init(&ms->postcopy_pause_rp_sem, 0);
qemu_sem_init(&ms->rp_state.rp_sem, 0);
qemu_sem_init(&ms->rp_state.rp_pong_acks, 0);
qemu_sem_init(&ms->rate_limit_sem, 0);
diff --git a/migration/migration.h b/migration/migration.h
index cdaa10d515..972597f4de 100644
--- a/migration/migration.h
+++ b/migration/migration.h
@@ -393,7 +393,6 @@ struct MigrationState {
/* Needed by postcopy-pause state */
QemuSemaphore postcopy_pause_sem;
- QemuSemaphore postcopy_pause_rp_sem;
/*
* Whether we abort the migration if decompression errors are
* detected at the destination. It is left at false for qemu
--
2.35.3
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 8/8] migration: Move return path cleanup to main migration thread
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
` (6 preceding siblings ...)
2023-09-18 17:28 ` [PATCH 7/8] migration: Replace the return path retry logic Fabiano Rosas
@ 2023-09-18 17:28 ` Fabiano Rosas
2023-09-27 12:39 ` [PATCH 0/8] migration fixes Fabiano Rosas
` (3 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Fabiano Rosas @ 2023-09-18 17:28 UTC (permalink / raw)
To: qemu-devel; +Cc: Juan Quintela, Peter Xu, Stefan Hajnoczi, Leonardo Bras
Now that the return path thread is allowed to finish during a paused
migration, we can move the cleanup of the QEMUFiles to the main
migration thread.
Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
migration/migration.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/migration/migration.c b/migration/migration.c
index af78f7ee54..e2ed85b5be 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -98,6 +98,7 @@ static int migration_maybe_pause(MigrationState *s,
int *current_active_state,
int new_state);
static void migrate_fd_cancel(MigrationState *s);
+static int await_return_path_close_on_source(MigrationState *s);
static bool migration_needs_multiple_sockets(void)
{
@@ -1178,6 +1179,12 @@ static void migrate_fd_cleanup(MigrationState *s)
qemu_fclose(tmp);
}
+ /*
+ * We already cleaned up to_dst_file, so errors from the return
+ * path might be due to that, ignore them.
+ */
+ await_return_path_close_on_source(s);
+
assert(!migration_is_active(s));
if (s->state == MIGRATION_STATUS_CANCELLING) {
@@ -1997,7 +2004,6 @@ out:
}
trace_source_return_path_thread_end();
- migration_release_dst_files(ms);
rcu_unregister_thread();
return NULL;
}
@@ -2051,6 +2057,9 @@ static int await_return_path_close_on_source(MigrationState *ms)
ret = ms->rp_state.error;
ms->rp_state.error = false;
+
+ migration_release_dst_files(ms);
+
trace_migration_return_path_end_after(ret);
return ret;
}
--
2.35.3
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH 0/8] migration fixes
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
` (7 preceding siblings ...)
2023-09-18 17:28 ` [PATCH 8/8] migration: Move return path cleanup to main migration thread Fabiano Rosas
@ 2023-09-27 12:39 ` Fabiano Rosas
2023-09-27 13:58 ` Peter Xu
2023-09-27 17:58 ` Stefan Hajnoczi
` (2 subsequent siblings)
11 siblings, 1 reply; 16+ messages in thread
From: Fabiano Rosas @ 2023-09-27 12:39 UTC (permalink / raw)
To: qemu-devel; +Cc: Juan Quintela, Peter Xu, Stefan Hajnoczi
Fabiano Rosas <farosas@suse.de> writes:
> This series contains fixes for the two currently know failures that
> show up in migration tests plus a set of fixes for some theoretical
> race conditions around QEMUFile handling.
>
> Patch 1 addresses the issue found in the postcopy/preempt/plain test:
> https://gitlab.com/qemu-project/qemu/-/issues/1886
>
> Patch 7 fixes a rare crash during the postocpy/preempt/recovery/plain test:
>
> Thread 7 "return path" received signal SIGSEGV, Segmentation fault.
> 0x00005555560e4893 in qemu_file_get_error_obj (f=0x0, errp=0x0) at ../migration/qemu-file.c:154
> 154 return f->last_error;
>
> CI run: https://gitlab.com/farosas/qemu/-/pipelines/1008652837
>
> Fabiano Rosas (7):
> migration: Fix possible race when setting rp_state.error
> migration: Fix possible races when shutting down the return path
> migration: Fix possible race when shutting down to_dst_file
> migration: Remove redundant cleanup of postcopy_qemufile_src
> migration: Consolidate return path closing code
> migration: Replace the return path retry logic
> migration: Move return path cleanup to main migration thread
>
> Peter Xu (1):
> migration: Fix race that dest preempt thread close too early
>
> migration/migration.c | 145 +++++++++++++++------------------------
> migration/migration.h | 14 +++-
> migration/postcopy-ram.c | 38 +++++++++-
> 3 files changed, 106 insertions(+), 91 deletions(-)
Ping
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 0/8] migration fixes
2023-09-27 12:39 ` [PATCH 0/8] migration fixes Fabiano Rosas
@ 2023-09-27 13:58 ` Peter Xu
0 siblings, 0 replies; 16+ messages in thread
From: Peter Xu @ 2023-09-27 13:58 UTC (permalink / raw)
To: Fabiano Rosas; +Cc: qemu-devel, Juan Quintela, Stefan Hajnoczi
On Wed, Sep 27, 2023 at 09:39:33AM -0300, Fabiano Rosas wrote:
> Fabiano Rosas <farosas@suse.de> writes:
>
> > This series contains fixes for the two currently know failures that
> > show up in migration tests plus a set of fixes for some theoretical
> > race conditions around QEMUFile handling.
> >
> > Patch 1 addresses the issue found in the postcopy/preempt/plain test:
> > https://gitlab.com/qemu-project/qemu/-/issues/1886
> >
> > Patch 7 fixes a rare crash during the postocpy/preempt/recovery/plain test:
> >
> > Thread 7 "return path" received signal SIGSEGV, Segmentation fault.
> > 0x00005555560e4893 in qemu_file_get_error_obj (f=0x0, errp=0x0) at ../migration/qemu-file.c:154
> > 154 return f->last_error;
> >
> > CI run: https://gitlab.com/farosas/qemu/-/pipelines/1008652837
> >
> > Fabiano Rosas (7):
> > migration: Fix possible race when setting rp_state.error
> > migration: Fix possible races when shutting down the return path
> > migration: Fix possible race when shutting down to_dst_file
> > migration: Remove redundant cleanup of postcopy_qemufile_src
> > migration: Consolidate return path closing code
> > migration: Replace the return path retry logic
> > migration: Move return path cleanup to main migration thread
> >
> > Peter Xu (1):
> > migration: Fix race that dest preempt thread close too early
> >
> > migration/migration.c | 145 +++++++++++++++------------------------
> > migration/migration.h | 14 +++-
> > migration/postcopy-ram.c | 38 +++++++++-
> > 3 files changed, 106 insertions(+), 91 deletions(-)
>
> Ping
Stefan,
This is the series I mentioned to you before that will solve all known
migration-test intermittent failures. It covers two bugs we're aware of.
I've already provided all R-bs on the patches.
Feel free to merge this series if you want even before Juan's back.
Thanks!
--
Peter Xu
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 0/8] migration fixes
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
` (8 preceding siblings ...)
2023-09-27 12:39 ` [PATCH 0/8] migration fixes Fabiano Rosas
@ 2023-09-27 17:58 ` Stefan Hajnoczi
2023-09-27 20:14 ` Stefan Hajnoczi
2023-10-02 22:55 ` Michael Tokarev
11 siblings, 0 replies; 16+ messages in thread
From: Stefan Hajnoczi @ 2023-09-27 17:58 UTC (permalink / raw)
To: Fabiano Rosas; +Cc: qemu-devel, Juan Quintela, Peter Xu
[-- Attachment #1: Type: text/plain, Size: 1570 bytes --]
On Mon, Sep 18, 2023 at 02:28:14PM -0300, Fabiano Rosas wrote:
> This series contains fixes for the two currently know failures that
> show up in migration tests plus a set of fixes for some theoretical
> race conditions around QEMUFile handling.
>
> Patch 1 addresses the issue found in the postcopy/preempt/plain test:
> https://gitlab.com/qemu-project/qemu/-/issues/1886
>
> Patch 7 fixes a rare crash during the postocpy/preempt/recovery/plain test:
>
> Thread 7 "return path" received signal SIGSEGV, Segmentation fault.
> 0x00005555560e4893 in qemu_file_get_error_obj (f=0x0, errp=0x0) at ../migration/qemu-file.c:154
> 154 return f->last_error;
>
> CI run: https://gitlab.com/farosas/qemu/-/pipelines/1008652837
>
> Fabiano Rosas (7):
> migration: Fix possible race when setting rp_state.error
> migration: Fix possible races when shutting down the return path
> migration: Fix possible race when shutting down to_dst_file
> migration: Remove redundant cleanup of postcopy_qemufile_src
> migration: Consolidate return path closing code
> migration: Replace the return path retry logic
> migration: Move return path cleanup to main migration thread
>
> Peter Xu (1):
> migration: Fix race that dest preempt thread close too early
>
> migration/migration.c | 145 +++++++++++++++------------------------
> migration/migration.h | 14 +++-
> migration/postcopy-ram.c | 38 +++++++++-
> 3 files changed, 106 insertions(+), 91 deletions(-)
Applied to staging.
Thanks,
Stefan
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 0/8] migration fixes
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
` (9 preceding siblings ...)
2023-09-27 17:58 ` Stefan Hajnoczi
@ 2023-09-27 20:14 ` Stefan Hajnoczi
2023-10-02 22:55 ` Michael Tokarev
11 siblings, 0 replies; 16+ messages in thread
From: Stefan Hajnoczi @ 2023-09-27 20:14 UTC (permalink / raw)
To: Fabiano Rosas; +Cc: qemu-devel, Juan Quintela, Peter Xu, Stefan Hajnoczi
[-- Attachment #1: Type: text/plain, Size: 115 bytes --]
Applied, thanks.
Please update the changelog at https://wiki.qemu.org/ChangeLog/8.2 for any user-visible changes.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 0/8] migration fixes
2023-09-18 17:28 [PATCH 0/8] migration fixes Fabiano Rosas
` (10 preceding siblings ...)
2023-09-27 20:14 ` Stefan Hajnoczi
@ 2023-10-02 22:55 ` Michael Tokarev
2023-10-02 23:29 ` Peter Xu
11 siblings, 1 reply; 16+ messages in thread
From: Michael Tokarev @ 2023-10-02 22:55 UTC (permalink / raw)
To: Fabiano Rosas, qemu-devel; +Cc: Juan Quintela, Peter Xu, Stefan Hajnoczi
18.09.2023 20:28, Fabiano Rosas wrote:
> This series contains fixes for the two currently know failures that
> show up in migration tests plus a set of fixes for some theoretical
> race conditions around QEMUFile handling.
>
> Patch 1 addresses the issue found in the postcopy/preempt/plain test:
> https://gitlab.com/qemu-project/qemu/-/issues/1886
>
> Patch 7 fixes a rare crash during the postocpy/preempt/recovery/plain test:
>
> Thread 7 "return path" received signal SIGSEGV, Segmentation fault.
> 0x00005555560e4893 in qemu_file_get_error_obj (f=0x0, errp=0x0) at ../migration/qemu-file.c:154
> 154 return f->last_error;
>
> CI run: https://gitlab.com/farosas/qemu/-/pipelines/1008652837
>
> Fabiano Rosas (7):
> migration: Fix possible race when setting rp_state.error
> migration: Fix possible races when shutting down the return path
> migration: Fix possible race when shutting down to_dst_file
> migration: Remove redundant cleanup of postcopy_qemufile_src
> migration: Consolidate return path closing code
> migration: Replace the return path retry logic
> migration: Move return path cleanup to main migration thread
>
> Peter Xu (1):
> migration: Fix race that dest preempt thread close too early
>
> migration/migration.c | 145 +++++++++++++++------------------------
> migration/migration.h | 14 +++-
> migration/postcopy-ram.c | 38 +++++++++-
> 3 files changed, 106 insertions(+), 91 deletions(-)
What can be done for -stable?
At least the whole thing applies cleanly to 8.1.
Thanks,
/mjt
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 0/8] migration fixes
2023-10-02 22:55 ` Michael Tokarev
@ 2023-10-02 23:29 ` Peter Xu
2023-10-02 23:36 ` Michael Tokarev
0 siblings, 1 reply; 16+ messages in thread
From: Peter Xu @ 2023-10-02 23:29 UTC (permalink / raw)
To: Michael Tokarev; +Cc: Fabiano Rosas, qemu-devel, Juan Quintela, Stefan Hajnoczi
On Tue, Oct 03, 2023 at 01:55:35AM +0300, Michael Tokarev wrote:
> 18.09.2023 20:28, Fabiano Rosas wrote:
> > This series contains fixes for the two currently know failures that
> > show up in migration tests plus a set of fixes for some theoretical
> > race conditions around QEMUFile handling.
> >
> > Patch 1 addresses the issue found in the postcopy/preempt/plain test:
> > https://gitlab.com/qemu-project/qemu/-/issues/1886
> >
> > Patch 7 fixes a rare crash during the postocpy/preempt/recovery/plain test:
> >
> > Thread 7 "return path" received signal SIGSEGV, Segmentation fault.
> > 0x00005555560e4893 in qemu_file_get_error_obj (f=0x0, errp=0x0) at ../migration/qemu-file.c:154
> > 154 return f->last_error;
> >
> > CI run: https://gitlab.com/farosas/qemu/-/pipelines/1008652837
> >
> > Fabiano Rosas (7):
> > migration: Fix possible race when setting rp_state.error
> > migration: Fix possible races when shutting down the return path
> > migration: Fix possible race when shutting down to_dst_file
> > migration: Remove redundant cleanup of postcopy_qemufile_src
> > migration: Consolidate return path closing code
> > migration: Replace the return path retry logic
> > migration: Move return path cleanup to main migration thread
> >
> > Peter Xu (1):
> > migration: Fix race that dest preempt thread close too early
> >
> > migration/migration.c | 145 +++++++++++++++------------------------
> > migration/migration.h | 14 +++-
> > migration/postcopy-ram.c | 38 +++++++++-
> > 3 files changed, 106 insertions(+), 91 deletions(-)
>
> What can be done for -stable?
>
> At least the whole thing applies cleanly to 8.1.
It seems v8.0.5 needs at least a few conflict resolutions.
I'd say we go apply those to 8.1.1, and we can skip 8.0 unless someone
requests for it.
Thanks,
--
Peter Xu
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 0/8] migration fixes
2023-10-02 23:29 ` Peter Xu
@ 2023-10-02 23:36 ` Michael Tokarev
0 siblings, 0 replies; 16+ messages in thread
From: Michael Tokarev @ 2023-10-02 23:36 UTC (permalink / raw)
To: Peter Xu; +Cc: Fabiano Rosas, qemu-devel, Juan Quintela, Stefan Hajnoczi
03.10.2023 02:29, Peter Xu:
> On Tue, Oct 03, 2023 at 01:55:35AM +0300, Michael Tokarev wrote:
>> What can be done for -stable?
>>
>> At least the whole thing applies cleanly to 8.1.
>
> It seems v8.0.5 needs at least a few conflict resolutions.
As I stated in recent 8.0.5 stable announce, it was the last
from 8.0.x series unless someone really needs a new version.
So far no one said anything, so be it the last one :)
> I'd say we go apply those to 8.1.1, and we can skip 8.0 unless someone
> requests for it.
Okay, that's exactly what I thought, - queued whole thing,
will fire up some testing tomorrow.
THank you!
/mjt
^ permalink raw reply [flat|nested] 16+ messages in thread