From: Juraj Marcin <jmarcin@redhat.com>
To: qemu-devel@nongnu.org
Cc: Juraj Marcin <jmarcin@redhat.com>, Peter Xu <peterx@redhat.com>,
"Dr. David Alan Gilbert" <dave@treblig.org>,
Jiri Denemark <jdenemar@redhat.com>,
Fabiano Rosas <farosas@suse.de>
Subject: [PATCH v3 3/7] migration: Introduce postcopy incoming setup and cleanup functions
Date: Thu, 30 Oct 2025 22:49:07 +0100 [thread overview]
Message-ID: <20251030214915.1411860-4-jmarcin@redhat.com> (raw)
In-Reply-To: <20251030214915.1411860-1-jmarcin@redhat.com>
From: Juraj Marcin <jmarcin@redhat.com>
After moving postcopy_ram_listen_thread() to postcopy file, this patch
introduces a pair of functions, postcopy_incoming_setup() and
postcopy_incoming_cleanup(). These functions encapsulate setup and
cleanup of all incoming postcopy resources, postcopy-ram and postcopy
listen thread.
Furthermore, this patch also renames the postcopy_ram_listen_thread to
postcopy_listen_thread, as this thread handles not only postcopy-ram,
but also dirty-bitmaps and in the future it could handle other
postcopiable devices.
Signed-off-by: Juraj Marcin <jmarcin@redhat.com>
---
migration/migration.c | 2 +-
migration/postcopy-ram.c | 46 ++++++++++++++++++++++++++++++++++++----
migration/postcopy-ram.h | 3 ++-
migration/savevm.c | 25 ++--------------------
4 files changed, 47 insertions(+), 29 deletions(-)
diff --git a/migration/migration.c b/migration/migration.c
index 6e647c7c4a..9a367f717e 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -892,7 +892,7 @@ process_incoming_migration_co(void *opaque)
* but managed to complete within the precopy period, we can use
* the normal exit.
*/
- postcopy_ram_incoming_cleanup(mis);
+ postcopy_incoming_cleanup(mis);
} else if (ret >= 0) {
/*
* Postcopy was started, cleanup should happen at the end of the
diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
index 36d5415554..b47c955763 100644
--- a/migration/postcopy-ram.c
+++ b/migration/postcopy-ram.c
@@ -2082,10 +2082,8 @@ bool postcopy_is_paused(MigrationStatus status)
* Triggered by a postcopy_listen command; this thread takes over reading
* the input stream, leaving the main thread free to carry on loading the rest
* of the device state (from RAM).
- * (TODO:This could do with being in a postcopy file - but there again it's
- * just another input loop, not that postcopy specific)
*/
-void *postcopy_ram_listen_thread(void *opaque)
+static void *postcopy_listen_thread(void *opaque)
{
MigrationIncomingState *mis = migration_incoming_get_current();
QEMUFile *f = mis->from_src_file;
@@ -2151,7 +2149,7 @@ void *postcopy_ram_listen_thread(void *opaque)
*/
qemu_event_wait(&mis->main_thread_load_event);
}
- postcopy_ram_incoming_cleanup(mis);
+ postcopy_incoming_cleanup(mis);
if (load_res < 0) {
/*
@@ -2184,3 +2182,43 @@ void *postcopy_ram_listen_thread(void *opaque)
return NULL;
}
+
+int postcopy_incoming_setup(MigrationIncomingState *mis, Error **errp)
+{
+ /*
+ * Sensitise RAM - can now generate requests for blocks that don't exist
+ * However, at this point the CPU shouldn't be running, and the IO
+ * shouldn't be doing anything yet so don't actually expect requests
+ */
+ if (migrate_postcopy_ram()) {
+ if (postcopy_ram_incoming_setup(mis)) {
+ postcopy_ram_incoming_cleanup(mis);
+ error_setg(errp, "Failed to setup incoming postcopy RAM blocks");
+ return -1;
+ }
+ }
+
+ trace_loadvm_postcopy_handle_listen("after uffd");
+
+ if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, errp)) {
+ return -1;
+ }
+
+ mis->have_listen_thread = true;
+ postcopy_thread_create(mis, &mis->listen_thread,
+ MIGRATION_THREAD_DST_LISTEN,
+ postcopy_listen_thread, QEMU_THREAD_DETACHED);
+
+ return 0;
+}
+
+int postcopy_incoming_cleanup(MigrationIncomingState *mis)
+{
+ int rc = 0;
+
+ if (migrate_postcopy_ram()) {
+ rc = postcopy_ram_incoming_cleanup(mis);
+ }
+
+ return rc;
+}
diff --git a/migration/postcopy-ram.h b/migration/postcopy-ram.h
index 3e26db3e6b..a080dd65a7 100644
--- a/migration/postcopy-ram.h
+++ b/migration/postcopy-ram.h
@@ -199,6 +199,7 @@ bool postcopy_is_paused(MigrationStatus status);
void mark_postcopy_blocktime_begin(uintptr_t addr, uint32_t ptid,
RAMBlock *rb);
-void *postcopy_ram_listen_thread(void *opaque);
+int postcopy_incoming_setup(MigrationIncomingState *mis, Error **errp);
+int postcopy_incoming_cleanup(MigrationIncomingState *mis);
#endif
diff --git a/migration/savevm.c b/migration/savevm.c
index 97fdd08c08..6ae3f740b5 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -2112,32 +2112,11 @@ static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis,
trace_loadvm_postcopy_handle_listen("after discard");
- /*
- * Sensitise RAM - can now generate requests for blocks that don't exist
- * However, at this point the CPU shouldn't be running, and the IO
- * shouldn't be doing anything yet so don't actually expect requests
- */
- if (migrate_postcopy_ram()) {
- if (postcopy_ram_incoming_setup(mis)) {
- postcopy_ram_incoming_cleanup(mis);
- error_setg(errp, "Failed to setup incoming postcopy RAM blocks");
- return -1;
- }
- }
+ int rc = postcopy_incoming_setup(mis, errp);
- trace_loadvm_postcopy_handle_listen("after uffd");
-
- if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, errp)) {
- return -1;
- }
-
- mis->have_listen_thread = true;
- postcopy_thread_create(mis, &mis->listen_thread,
- MIGRATION_THREAD_DST_LISTEN,
- postcopy_ram_listen_thread, QEMU_THREAD_DETACHED);
trace_loadvm_postcopy_handle_listen("return");
- return 0;
+ return rc;
}
static void loadvm_postcopy_handle_run_bh(void *opaque)
--
2.51.0
next prev parent reply other threads:[~2025-10-30 21:51 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-30 21:49 [PATCH v3 0/7] migration: Introduce POSTCOPY_DEVICE state Juraj Marcin
2025-10-30 21:49 ` [PATCH v3 1/7] migration: Do not try to start VM if disk activation fails Juraj Marcin
2025-10-30 21:49 ` [PATCH v3 2/7] migration: Move postcopy_ram_listen_thread() to postcopy-ram.c Juraj Marcin
2025-10-30 22:28 ` Peter Xu
2025-10-30 21:49 ` Juraj Marcin [this message]
2025-10-30 22:35 ` [PATCH v3 3/7] migration: Introduce postcopy incoming setup and cleanup functions Peter Xu
2025-10-30 21:49 ` [PATCH v3 4/7] migration: Refactor all incoming cleanup info migration_incoming_destroy() Juraj Marcin
2025-10-30 22:49 ` Peter Xu
2025-10-31 11:03 ` Juraj Marcin
2025-10-31 16:29 ` Peter Xu
2025-10-31 17:01 ` Peter Xu
2025-10-30 21:49 ` [PATCH v3 5/7] migration: Respect exit-on-error when migration fails before resuming Juraj Marcin
2025-10-30 22:49 ` Peter Xu
2025-10-30 21:49 ` [PATCH v3 6/7] migration: Make postcopy listen thread joinable Juraj Marcin
2025-10-30 22:49 ` Peter Xu
2025-10-30 21:49 ` [PATCH v3 7/7] migration: Introduce POSTCOPY_DEVICE state Juraj Marcin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251030214915.1411860-4-jmarcin@redhat.com \
--to=jmarcin@redhat.com \
--cc=dave@treblig.org \
--cc=farosas@suse.de \
--cc=jdenemar@redhat.com \
--cc=peterx@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).