[PATCH v5 00/11] migration: fast snapshot load

Aadeshveer Singh posted 11 patches 1 week, 1 day ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260816174631.1547811-1-aadeshveer07@gmail.com
Maintainers: Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>, Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>, Paolo Bonzini <pbonzini@redhat.com>, "Philippe Mathieu-Daudé" <philmd@mailo.com>, Laurent Vivier <lvivier@redhat.com>
docs/devel/migration/fast-snapshot-load.rst |  82 +++++
docs/devel/migration/features.rst           |   1 +
include/qemu/notify.h                       |   2 +
include/system/ramblock.h                   |   6 +
migration/migration.c                       |  66 ++--
migration/migration.h                       |   5 +
migration/options.c                         |  20 +-
migration/postcopy-ram.c                    | 333 +++++++++++++++++---
migration/postcopy-ram.h                    |   8 +-
migration/qemu-file.c                       |  11 +-
migration/qemu-file.h                       |   4 +-
migration/ram.c                             | 101 +++++-
migration/savevm.c                          |  16 +
migration/savevm.h                          |   2 +
migration/trace-events                      |   2 +
tests/qtest/migration/file-tests.c          |  24 ++
tests/qtest/migration/misc-tests.c          |  52 ---
util/notify.c                               |   5 +
18 files changed, 597 insertions(+), 143 deletions(-)
create mode 100644 docs/devel/migration/fast-snapshot-load.rst
[PATCH v5 00/11] migration: fast snapshot load
Posted by Aadeshveer Singh 1 week, 1 day ago
This series implements a "fast snapshot load" mechanism to
significantly reduce the perceived resume time of a VM from a snapshot
file.

Currently, resuming a VM from a snapshot file requires loading all RAM
pages into the QEMU instance before execution begins. This extension
allows the user to run the VM nearly instantly by loading only the
required device states up front and loading RAM pages lazily, by
trapping access to pages that have not yet been loaded.

Using the Linux userfaultfd syscall, a fault thread catches all page
faults caused by the guest and loads in the pages required to keep
the VM running. Concurrently, an eager background thread iteratively
loads all remaining pages into RAM so the guest does not have to
depend on the fault thread indefinitely.

Much of code is reused from postcopy for fault handling and precopy
for reading mapped ram file. Implementation revolves around two
threads named the fault thread and eager load thread. Fault thread as
name suggests catches page faults by the guest and serves them using
userfaultfd. Postcopy fault thread is reused but instead of requesting
source for a page it loads the page directly by reading from file. In
order to remove the dependency of guest on fault thread indefinitely
the eager load thread loads in the entire RAM sequentially, and after
iterating through the entire RAM signals fault thread to exit and
calls cleanup.

In order to prevent the case of a page being loaded twice(in the
case when eager load thread is loading it and fault thread also
tries to serve fault on same page) a bitmap called pending_bmap is
used to track pages which are pending and not being loaded by any
thread. Atomic operations on this bitmap allows coordination between
threads to prevent any unwanted behaviours

This patch series was tested on a single machine:
host OS   : Fedora 44
host RAM  : 32GB DDR5(4KB pagesize)
host CPU  : Intel Ultra9 185H(22 cores, x86_64)

Guests tested:
- KVM enabled
  guest OS  : Fedora 44
  guest RAM : 16GB(4KB pagesize)
  guest CPU : 4 cores(x86_64)
- host using 2MB hugepages
  guest OS  : Debian 13
  guest RAM : 16GB(16KB pagesize)
  guest CPU : 4 cores(x86_64)

Future direction:
- Add support for multifd
- Add support for vhost-user

---
v4 -> v5
- Modify pending_bmap(Patch 6) to use variable size for better
  efficient atomic state management
- Added a comment(Patch 7) explaining postcopy_mapped_ram_load_page
  explaining why cases where guest page is larger than host page are
  disabled following what remote postcopy follows
- Add a new check(Patch 9) in migrate_prepare to bail out if migrate
  was used with fast snapshot load enabled, as suggested by Peter


Aadeshveer Singh (11):
  migration: Propagate error in postcopy setup functions
  migration: Extract blocktime marking helper
  migration: Rename postcopy_listen_thread_bh
  migration: Use file_bmap for RAMBlock during incoming file load
  migration: Make qemu_get_buffer_at() thread-safe
  migration: add RAMBlock field and helper for fast snapshot load
  migration: add support for fault thread to load pages from disk
  migration: add eager load thread and setup for fast snapshot load
  migration: update capability conflict test for postcopy-ram+mapped-ram
  migration/tests: Add test for fast snapshot load
  docs/migration: Add documentation for fast snapshot load feature

 docs/devel/migration/fast-snapshot-load.rst |  82 +++++
 docs/devel/migration/features.rst           |   1 +
 include/qemu/notify.h                       |   2 +
 include/system/ramblock.h                   |   6 +
 migration/migration.c                       |  66 ++--
 migration/migration.h                       |   5 +
 migration/options.c                         |  20 +-
 migration/postcopy-ram.c                    | 333 +++++++++++++++++---
 migration/postcopy-ram.h                    |   8 +-
 migration/qemu-file.c                       |  11 +-
 migration/qemu-file.h                       |   4 +-
 migration/ram.c                             | 101 +++++-
 migration/savevm.c                          |  16 +
 migration/savevm.h                          |   2 +
 migration/trace-events                      |   2 +
 tests/qtest/migration/file-tests.c          |  24 ++
 tests/qtest/migration/misc-tests.c          |  52 ---
 util/notify.c                               |   5 +
 18 files changed, 597 insertions(+), 143 deletions(-)
 create mode 100644 docs/devel/migration/fast-snapshot-load.rst

-- 
2.55.0
Re: [PATCH v5 00/11] migration: fast snapshot load
Posted by Peter Xu 6 days, 11 hours ago
Hey, Aadeshveer,

On Sun, Aug 16, 2026 at 11:16:20PM +0530, Aadeshveer Singh wrote:
> v4 -> v5
> - Modify pending_bmap(Patch 6) to use variable size for better
>   efficient atomic state management
> - Added a comment(Patch 7) explaining postcopy_mapped_ram_load_page
>   explaining why cases where guest page is larger than host page are
>   disabled following what remote postcopy follows
> - Add a new check(Patch 9) in migrate_prepare to bail out if migrate
>   was used with fast snapshot load enabled, as suggested by Peter

There're something you didn't mention but we discussed it only during the
meeting about host/guest psize problem.  For this time, I'll do it for you.
Aadeshveer, please remember to mention such conclusions and findings in the
future when you will be working on other patches.

The quick summary: Aadeshveer found postcopy doesn't support guest psize >
host psize since the start, which will already fail the set capabilities in
postcopy_ram_supported_by_host().  It means we can also skip supporting
that in fast snapshot load.

Personally, I still prefer removal of all such guest psize > host psize
handlings in your current code; postcopy_place_page() isn't ready, so the
current code won't work anyway for that setup.  With it removed, then we
can stick pending_bmap with host psize, which can also cause less confusion
and make it easier to read.  But I think it's not a huge deal, and AFAIU
the code works as expected.  So I think it's fine for now.

If you're going to prepare a small series on top of this, please consider
reverting those changes back to the simple version with one patch on top.

Other than that, thanks for your hard work in the past few months!  Since I
believe this code still should function all well, I queued it for 11.2.

-- 
Peter Xu
Re: [PATCH v5 00/11] migration: fast snapshot load
Posted by Aadeshveer Singh 5 days, 12 hours ago
Hi Peter,

Noted, on documenting the external discussions and including them in
future cover letters and commit messages.

REgarding the guest psize > host psize code cleanup, I'd be happy to
send a small follow-up on top of current patches reverting the checks
and simplifying the pending_bmap logic

Thank you, really excited to see the series queued for 11.2. I can't
thank you enough for your guidance, patience, and mentorship
throughout the summer.

On Tue, Aug 18, 2026 at 11:15 PM Peter Xu <peterx@redhat.com> wrote:
>
> Hey, Aadeshveer,
>
> On Sun, Aug 16, 2026 at 11:16:20PM +0530, Aadeshveer Singh wrote:
> > v4 -> v5
> > - Modify pending_bmap(Patch 6) to use variable size for better
> >   efficient atomic state management
> > - Added a comment(Patch 7) explaining postcopy_mapped_ram_load_page
> >   explaining why cases where guest page is larger than host page are
> >   disabled following what remote postcopy follows
> > - Add a new check(Patch 9) in migrate_prepare to bail out if migrate
> >   was used with fast snapshot load enabled, as suggested by Peter
>
> There're something you didn't mention but we discussed it only during the
> meeting about host/guest psize problem.  For this time, I'll do it for you.
> Aadeshveer, please remember to mention such conclusions and findings in the
> future when you will be working on other patches.
>
> The quick summary: Aadeshveer found postcopy doesn't support guest psize >
> host psize since the start, which will already fail the set capabilities in
> postcopy_ram_supported_by_host().  It means we can also skip supporting
> that in fast snapshot load.
>
> Personally, I still prefer removal of all such guest psize > host psize
> handlings in your current code; postcopy_place_page() isn't ready, so the
> current code won't work anyway for that setup.  With it removed, then we
> can stick pending_bmap with host psize, which can also cause less confusion
> and make it easier to read.  But I think it's not a huge deal, and AFAIU
> the code works as expected.  So I think it's fine for now.
>
> If you're going to prepare a small series on top of this, please consider
> reverting those changes back to the simple version with one patch on top.
>
> Other than that, thanks for your hard work in the past few months!  Since I
> believe this code still should function all well, I queued it for 11.2.
>
> --
> Peter Xu
>
Re: [PATCH v5 00/11] migration: fast snapshot load
Posted by Peter Xu 5 days, 13 hours ago
On Sun, Aug 16, 2026 at 11:16:20PM +0530, Aadeshveer Singh wrote:
> This series implements a "fast snapshot load" mechanism to
> significantly reduce the perceived resume time of a VM from a snapshot
> file.

This series breaks Windows builds... we'll need three fixups into three
patches to fix it.  Attached at the end.

For Aadeshveer: in the future you can check Windows build of your own
patches locally by running this:

  $ make docker-test-build@fedora-win64-cross

I do hit Rust build failures nowadays with Windows, though, so you may need
this if you have enabled rust builds instead (I also normally use J=N for
concurrency):

  $ J=8 EXTRA_CONFIGURE_OPTS=--disable-rust make docker-test-build@fedora-win64-cross

For Fabiano: if you want, you can also directly pick up the relevant
patches I queued in my -next branch, or squash the fixups attached, or pick
your own fix. I'll leave that to you to decide.

  https://gitlab.com/peterx/qemu/-/tree/next

Thanks,

===8<===



From 572b0f8b611e430e5ed8651d99851975878624c7 Mon Sep 17 00:00:00 2001
From: Peter Xu <peterx@redhat.com>
Date: Tue, 18 Aug 2026 16:38:08 -0400
Subject: [PATCH 1/3] fixup! migration: Propagate error in postcopy setup
 functions

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 migration/postcopy-ram.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
index 81d06917fe..3c85fa1b19 100644
--- a/migration/postcopy-ram.c
+++ b/migration/postcopy-ram.c
@@ -1906,7 +1906,7 @@ int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
     g_assert_not_reached();
 }
 
-int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
+int postcopy_ram_incoming_setup(MigrationIncomingState *mis, Error **errp)
 {
     g_assert_not_reached();
 }
-- 
2.54.0




From 4273cc6ed223de1781513af9a62c1672c3f5591b Mon Sep 17 00:00:00 2001
From: Peter Xu <peterx@redhat.com>
Date: Wed, 19 Aug 2026 10:30:35 -0400
Subject: [PATCH 2/3] fixup! migration: Extract blocktime marking helper

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 migration/postcopy-ram.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
index 3c85fa1b19..fdc65c3545 100644
--- a/migration/postcopy-ram.c
+++ b/migration/postcopy-ram.c
@@ -1934,6 +1934,14 @@ void mark_postcopy_blocktime_begin(uintptr_t addr, uint32_t ptid,
                                    RAMBlock *rb)
 {
 }
+
+bool try_mark_postcopy_blocktime_begin(MigrationIncomingState *mis,
+                                       RAMBlock *rb, ram_addr_t start,
+                                       uint64_t haddr, uint32_t tid)
+{
+    g_assert_not_reached();
+    return false;
+}
 #endif
 
 /* ------------------------------------------------------------------------- */
-- 
2.54.0



From d2d397de73fe39b424b2871fe4ba0df7937cc680 Mon Sep 17 00:00:00 2001
From: Peter Xu <peterx@redhat.com>
Date: Tue, 18 Aug 2026 16:43:29 -0400
Subject: [PATCH 3/3] fixup! migration: add eager load thread and setup for
 fast snapshot load

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 migration/postcopy-ram.c | 135 ++++++++++++++++++++-------------------
 1 file changed, 71 insertions(+), 64 deletions(-)

diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
index fdc65c3545..885ab58fca 100644
--- a/migration/postcopy-ram.c
+++ b/migration/postcopy-ram.c
@@ -39,6 +39,8 @@
 #include "qemu/mmap-alloc.h"
 #include "options.h"
 
+static void postcopy_incoming_complete_bh(void *opaque);
+
 /* Arbitrary limit on size of each discard command,
  * keeps them around ~200 bytes
  */
@@ -1872,6 +1874,70 @@ int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
     }
 }
 
+/*
+ * Called by postcopy_ram_eager_load_thread over all blocks to load in all the
+ * pending pages of given ram block
+ */
+static int ram_block_load_eager(RAMBlock *rb, void *opaque)
+{
+    MigrationIncomingState *mis = migration_incoming_get_current();
+    MigrationState *s = migrate_get_current();
+    Error *errp = NULL;
+    void *host = qemu_ram_get_host_addr(rb);
+    void *target;
+
+    for (ram_addr_t page_loc = 0; page_loc < rb->used_length;
+         page_loc += qemu_ram_pagesize(rb)) {
+        target = (uint8_t *)host + page_loc;
+        if (!postcopy_mapped_ram_load_page(mis, rb, page_loc, (uint64_t)target,
+                                           RAM_CHANNEL_PRECOPY, &errp)) {
+            migrate_error_propagate(s, errp);
+            return -1;
+        }
+    }
+    return 0;
+}
+
+/*
+ * Used by fast snapshot load to eagerly load in all pages of RAM and schedule
+ * cleanup after entire RAM is loaded
+ */
+static void *postcopy_ram_eager_load_thread(void *opaque)
+{
+    MigrationIncomingState *mis = opaque;
+    MigrationStatus next_state;
+
+    trace_postcopy_ram_eager_load_thread_entry();
+    rcu_register_thread();
+    qemu_event_set(&mis->thread_sync_event);
+
+    if (foreach_not_ignored_block(ram_block_load_eager, NULL)) {
+        next_state = MIGRATION_STATUS_FAILED;
+    } else {
+        next_state = MIGRATION_STATUS_COMPLETED;
+    }
+    migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
+                      next_state);
+
+    postcopy_state_set(POSTCOPY_INCOMING_END);
+    migration_bh_schedule(postcopy_incoming_complete_bh, mis);
+
+    rcu_unregister_thread();
+    trace_postcopy_ram_eager_load_thread_exit();
+    return NULL;
+}
+
+/*
+ * Create thread for eager loading in fast snapshot load case
+ */
+void postcopy_ram_eager_load_setup(MigrationIncomingState *mis)
+{
+    postcopy_thread_create(
+        mis, &mis->eager_load_thread, MIGRATION_THREAD_DST_SNAPSHOT_LOAD,
+        postcopy_ram_eager_load_thread, QEMU_THREAD_JOINABLE);
+    mis->have_eager_load_thread = true;
+}
+
 #else
 /* No target OS support, stubs just fail */
 void fill_destination_postcopy_migration_info(MigrationInfo *info)
@@ -1942,6 +2008,11 @@ bool try_mark_postcopy_blocktime_begin(MigrationIncomingState *mis,
     g_assert_not_reached();
     return false;
 }
+
+void postcopy_ram_eager_load_setup(MigrationIncomingState *mis)
+{
+    g_assert_not_reached();
+}
 #endif
 
 /* ------------------------------------------------------------------------- */
@@ -2426,67 +2497,3 @@ int postcopy_incoming_cleanup(MigrationIncomingState *mis)
 
     return rc;
 }
-
-/*
- * Called by postcopy_ram_eager_load_thread over all blocks to load in all the
- * pending pages of given ram block
- */
-static int ram_block_load_eager(RAMBlock *rb, void *opaque)
-{
-    MigrationIncomingState *mis = migration_incoming_get_current();
-    MigrationState *s = migrate_get_current();
-    Error *errp = NULL;
-    void *host = qemu_ram_get_host_addr(rb);
-    void *target;
-
-    for (ram_addr_t page_loc = 0; page_loc < rb->used_length;
-         page_loc += qemu_ram_pagesize(rb)) {
-        target = (uint8_t *)host + page_loc;
-        if (!postcopy_mapped_ram_load_page(mis, rb, page_loc, (uint64_t)target,
-                                           RAM_CHANNEL_PRECOPY, &errp)) {
-            migrate_error_propagate(s, errp);
-            return -1;
-        }
-    }
-    return 0;
-}
-
-/*
- * Used by fast snapshot load to eagerly load in all pages of RAM and schedule
- * cleanup after entire RAM is loaded
- */
-static void *postcopy_ram_eager_load_thread(void *opaque)
-{
-    MigrationIncomingState *mis = opaque;
-    MigrationStatus next_state;
-
-    trace_postcopy_ram_eager_load_thread_entry();
-    rcu_register_thread();
-    qemu_event_set(&mis->thread_sync_event);
-
-    if (foreach_not_ignored_block(ram_block_load_eager, NULL)) {
-        next_state = MIGRATION_STATUS_FAILED;
-    } else {
-        next_state = MIGRATION_STATUS_COMPLETED;
-    }
-    migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
-                      next_state);
-
-    postcopy_state_set(POSTCOPY_INCOMING_END);
-    migration_bh_schedule(postcopy_incoming_complete_bh, mis);
-
-    rcu_unregister_thread();
-    trace_postcopy_ram_eager_load_thread_exit();
-    return NULL;
-}
-
-/*
- * Create thread for eager loading in fast snapshot load case
- */
-void postcopy_ram_eager_load_setup(MigrationIncomingState *mis)
-{
-    postcopy_thread_create(
-        mis, &mis->eager_load_thread, MIGRATION_THREAD_DST_SNAPSHOT_LOAD,
-        postcopy_ram_eager_load_thread, QEMU_THREAD_JOINABLE);
-    mis->have_eager_load_thread = true;
-}
-- 
2.54.0


-- 
Peter Xu
Re: [PATCH v5 00/11] migration: fast snapshot load
Posted by Aadeshveer Singh 5 days, 12 hours ago
On Wed, Aug 19, 2026 at 9:13 PM Peter Xu <peterx@redhat.com> wrote:
>
> On Sun, Aug 16, 2026 at 11:16:20PM +0530, Aadeshveer Singh wrote:
> > This series implements a "fast snapshot load" mechanism to
> > significantly reduce the perceived resume time of a VM from a snapshot
> > file.
>
> This series breaks Windows builds... we'll need three fixups into three
> patches to fix it.  Attached at the end.
>
> For Aadeshveer: in the future you can check Windows build of your own
> patches locally by running this:
>
>   $ make docker-test-build@fedora-win64-cross
>
> I do hit Rust build failures nowadays with Windows, though, so you may need
> this if you have enabled rust builds instead (I also normally use J=N for
> concurrency):
>
>   $ J=8 EXTRA_CONFIGURE_OPTS=--disable-rust make docker-test-build@fedora-win64-cross
>

Thanks for catching the Windows build failure and writing the fixups!
I've noted down the test command and will make sure any future work
does not break the windows build.

> For Fabiano: if you want, you can also directly pick up the relevant
> patches I queued in my -next branch, or squash the fixups attached, or pick
> your own fix. I'll leave that to you to decide.
>
>   https://gitlab.com/peterx/qemu/-/tree/next
>
> Thanks,
>
> ===8<===
>
>
>
> From 572b0f8b611e430e5ed8651d99851975878624c7 Mon Sep 17 00:00:00 2001
> From: Peter Xu <peterx@redhat.com>
> Date: Tue, 18 Aug 2026 16:38:08 -0400
> Subject: [PATCH 1/3] fixup! migration: Propagate error in postcopy setup
>  functions
>
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  migration/postcopy-ram.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
> index 81d06917fe..3c85fa1b19 100644
> --- a/migration/postcopy-ram.c
> +++ b/migration/postcopy-ram.c
> @@ -1906,7 +1906,7 @@ int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
>      g_assert_not_reached();
>  }
>
> -int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
> +int postcopy_ram_incoming_setup(MigrationIncomingState *mis, Error **errp)
>  {
>      g_assert_not_reached();
>  }
> --
> 2.54.0
>
>
>
>
> From 4273cc6ed223de1781513af9a62c1672c3f5591b Mon Sep 17 00:00:00 2001
> From: Peter Xu <peterx@redhat.com>
> Date: Wed, 19 Aug 2026 10:30:35 -0400
> Subject: [PATCH 2/3] fixup! migration: Extract blocktime marking helper
>
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  migration/postcopy-ram.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
>
> diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
> index 3c85fa1b19..fdc65c3545 100644
> --- a/migration/postcopy-ram.c
> +++ b/migration/postcopy-ram.c
> @@ -1934,6 +1934,14 @@ void mark_postcopy_blocktime_begin(uintptr_t addr, uint32_t ptid,
>                                     RAMBlock *rb)
>  {
>  }
> +
> +bool try_mark_postcopy_blocktime_begin(MigrationIncomingState *mis,
> +                                       RAMBlock *rb, ram_addr_t start,
> +                                       uint64_t haddr, uint32_t tid)
> +{
> +    g_assert_not_reached();
> +    return false;
> +}
>  #endif
>
>  /* ------------------------------------------------------------------------- */
> --
> 2.54.0
>
>
>
> From d2d397de73fe39b424b2871fe4ba0df7937cc680 Mon Sep 17 00:00:00 2001
> From: Peter Xu <peterx@redhat.com>
> Date: Tue, 18 Aug 2026 16:43:29 -0400
> Subject: [PATCH 3/3] fixup! migration: add eager load thread and setup for
>  fast snapshot load
>
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  migration/postcopy-ram.c | 135 ++++++++++++++++++++-------------------
>  1 file changed, 71 insertions(+), 64 deletions(-)
>
> diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
> index fdc65c3545..885ab58fca 100644
> --- a/migration/postcopy-ram.c
> +++ b/migration/postcopy-ram.c
> @@ -39,6 +39,8 @@
>  #include "qemu/mmap-alloc.h"
>  #include "options.h"
>
> +static void postcopy_incoming_complete_bh(void *opaque);
> +
>  /* Arbitrary limit on size of each discard command,
>   * keeps them around ~200 bytes
>   */
> @@ -1872,6 +1874,70 @@ int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
>      }
>  }
>
> +/*
> + * Called by postcopy_ram_eager_load_thread over all blocks to load in all the
> + * pending pages of given ram block
> + */
> +static int ram_block_load_eager(RAMBlock *rb, void *opaque)
> +{
> +    MigrationIncomingState *mis = migration_incoming_get_current();
> +    MigrationState *s = migrate_get_current();
> +    Error *errp = NULL;
> +    void *host = qemu_ram_get_host_addr(rb);
> +    void *target;
> +
> +    for (ram_addr_t page_loc = 0; page_loc < rb->used_length;
> +         page_loc += qemu_ram_pagesize(rb)) {
> +        target = (uint8_t *)host + page_loc;
> +        if (!postcopy_mapped_ram_load_page(mis, rb, page_loc, (uint64_t)target,
> +                                           RAM_CHANNEL_PRECOPY, &errp)) {
> +            migrate_error_propagate(s, errp);
> +            return -1;
> +        }
> +    }
> +    return 0;
> +}
> +
> +/*
> + * Used by fast snapshot load to eagerly load in all pages of RAM and schedule
> + * cleanup after entire RAM is loaded
> + */
> +static void *postcopy_ram_eager_load_thread(void *opaque)
> +{
> +    MigrationIncomingState *mis = opaque;
> +    MigrationStatus next_state;
> +
> +    trace_postcopy_ram_eager_load_thread_entry();
> +    rcu_register_thread();
> +    qemu_event_set(&mis->thread_sync_event);
> +
> +    if (foreach_not_ignored_block(ram_block_load_eager, NULL)) {
> +        next_state = MIGRATION_STATUS_FAILED;
> +    } else {
> +        next_state = MIGRATION_STATUS_COMPLETED;
> +    }
> +    migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
> +                      next_state);
> +
> +    postcopy_state_set(POSTCOPY_INCOMING_END);
> +    migration_bh_schedule(postcopy_incoming_complete_bh, mis);
> +
> +    rcu_unregister_thread();
> +    trace_postcopy_ram_eager_load_thread_exit();
> +    return NULL;
> +}
> +
> +/*
> + * Create thread for eager loading in fast snapshot load case
> + */
> +void postcopy_ram_eager_load_setup(MigrationIncomingState *mis)
> +{
> +    postcopy_thread_create(
> +        mis, &mis->eager_load_thread, MIGRATION_THREAD_DST_SNAPSHOT_LOAD,
> +        postcopy_ram_eager_load_thread, QEMU_THREAD_JOINABLE);
> +    mis->have_eager_load_thread = true;
> +}
> +
>  #else
>  /* No target OS support, stubs just fail */
>  void fill_destination_postcopy_migration_info(MigrationInfo *info)
> @@ -1942,6 +2008,11 @@ bool try_mark_postcopy_blocktime_begin(MigrationIncomingState *mis,
>      g_assert_not_reached();
>      return false;
>  }
> +
> +void postcopy_ram_eager_load_setup(MigrationIncomingState *mis)
> +{
> +    g_assert_not_reached();
> +}
>  #endif
>
>  /* ------------------------------------------------------------------------- */
> @@ -2426,67 +2497,3 @@ int postcopy_incoming_cleanup(MigrationIncomingState *mis)
>
>      return rc;
>  }
> -
> -/*
> - * Called by postcopy_ram_eager_load_thread over all blocks to load in all the
> - * pending pages of given ram block
> - */
> -static int ram_block_load_eager(RAMBlock *rb, void *opaque)
> -{
> -    MigrationIncomingState *mis = migration_incoming_get_current();
> -    MigrationState *s = migrate_get_current();
> -    Error *errp = NULL;
> -    void *host = qemu_ram_get_host_addr(rb);
> -    void *target;
> -
> -    for (ram_addr_t page_loc = 0; page_loc < rb->used_length;
> -         page_loc += qemu_ram_pagesize(rb)) {
> -        target = (uint8_t *)host + page_loc;
> -        if (!postcopy_mapped_ram_load_page(mis, rb, page_loc, (uint64_t)target,
> -                                           RAM_CHANNEL_PRECOPY, &errp)) {
> -            migrate_error_propagate(s, errp);
> -            return -1;
> -        }
> -    }
> -    return 0;
> -}
> -
> -/*
> - * Used by fast snapshot load to eagerly load in all pages of RAM and schedule
> - * cleanup after entire RAM is loaded
> - */
> -static void *postcopy_ram_eager_load_thread(void *opaque)
> -{
> -    MigrationIncomingState *mis = opaque;
> -    MigrationStatus next_state;
> -
> -    trace_postcopy_ram_eager_load_thread_entry();
> -    rcu_register_thread();
> -    qemu_event_set(&mis->thread_sync_event);
> -
> -    if (foreach_not_ignored_block(ram_block_load_eager, NULL)) {
> -        next_state = MIGRATION_STATUS_FAILED;
> -    } else {
> -        next_state = MIGRATION_STATUS_COMPLETED;
> -    }
> -    migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
> -                      next_state);
> -
> -    postcopy_state_set(POSTCOPY_INCOMING_END);
> -    migration_bh_schedule(postcopy_incoming_complete_bh, mis);
> -
> -    rcu_unregister_thread();
> -    trace_postcopy_ram_eager_load_thread_exit();
> -    return NULL;
> -}
> -
> -/*
> - * Create thread for eager loading in fast snapshot load case
> - */
> -void postcopy_ram_eager_load_setup(MigrationIncomingState *mis)
> -{
> -    postcopy_thread_create(
> -        mis, &mis->eager_load_thread, MIGRATION_THREAD_DST_SNAPSHOT_LOAD,
> -        postcopy_ram_eager_load_thread, QEMU_THREAD_JOINABLE);
> -    mis->have_eager_load_thread = true;
> -}
> --
> 2.54.0
>
>
> --
> Peter Xu
>