Skip to contentSkip to conversation

migration/multifd: Fix rb->receivedmap cleanup race

PATCH In review Fabiano Rosas 2 participants 8 messages Sep 17, 2024

lore
Share & tools
lore
#0 lore raw

cover migration/multifd: Fix rb->receivedmap cleanup race

v2: Keep skipping the cpu_synchronize_all_post_init() call if the postcopy listen thread is live. Don't copy stable on the first patch.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/1457418838
====
v1:
https://lore.kernel.org/r/[email protected]

This fixes the crash we've been seing recently in migration-test. The first patch is a cleanup to have only one place calling qemu_loadvm_state_cleanup() and the second patch reorders the cleanup calls to make multifd_recv_cleanup() run first and stop the recv threads.

Fabiano Rosas (2):
migration/savevm: Remove extra load cleanup calls migration/multifd: Fix rb->receivedmap cleanup race

 migration/migration.c | 1 +
 migration/savevm.c    | 8 ++++----
 2 files changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/2 migration/savevm: Remove extra load cleanup calls

There are two qemu_loadvm_state_cleanup() calls that were introduced when qemu_loadvm_state_setup() was still called before loading the configuration section, so there was state to be cleaned up if the header checks failed.

However, commit 9e14b84908 ("migration/savevm: load_header before load_setup") has moved that configuration section part to qemu_loadvm_state_header() which now happens before qemu_loadvm_state_setup().

Remove the cleanup calls that are now misplaced.

Fixes 9e14b84908 ("migration/savevm: load_header before load_setup")
---
 migration/savevm.c | 2 --
 1 file changed, 2 deletions(-)
migration/savevm.c +0 -2
index d500eae979..d0759694fd 100644
@@ -2732,13 +2732,11 @@ static int qemu_loadvm_state_header(QEMUFile *f)
     if (migrate_get_current()->send_configuration) {
         if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
             error_report("Configuration section missing");
-            qemu_loadvm_state_cleanup();
             return -EINVAL;
         }
         ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
 
         if (ret) {
-            qemu_loadvm_state_cleanup();
             return ret;
         }
     }
Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH 1/2] migration/savevm: Remove extra load cleanup calls

Quoted 5 lines · Fabiano Rosas

There are two qemu_loadvm_state_cleanup() calls that were introduced when qemu_loadvm_state_setup() was still called before loading the configuration section, so there was state to be cleaned up if the header checks failed.

Quoted 8 lines

However, commit 9e14b84908 ("migration/savevm: load_header before load_setup") has moved that configuration section part to qemu_loadvm_state_header() which now happens before qemu_loadvm_state_setup().

Remove the cleanup calls that are now misplaced.

Fixes 9e14b84908 ("migration/savevm: load_header before load_setup")

Considering it's a cleanup, do you mind if I further remove this Fixes but just mention it in commit message (so as to make Michael's life easier when backport)?

Quoted 27 lines
---
 migration/savevm.c | 2 --
 1 file changed, 2 deletions(-)
migration/savevm.c +0 -2
index d500eae979..d0759694fd 100644
@@ -2732,13 +2732,11 @@ static int qemu_loadvm_state_header(QEMUFile *f)
     if (migrate_get_current()->send_configuration) {
         if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
             error_report("Configuration section missing");
-            qemu_loadvm_state_cleanup();
             return -EINVAL;
         }
         ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
 
         if (ret) {
-            qemu_loadvm_state_cleanup();
             return ret;
         }
     }
Signature · 1 line
2.35.3
Signature · 1 line
Peter Xu
#3 lore raw

Re: [PATCH 1/2] migration/savevm: Remove extra load cleanup calls

Quoted 11 lines · Peter Xu
Quoted 10 lines · Fabiano Rosas

There are two qemu_loadvm_state_cleanup() calls that were introduced when qemu_loadvm_state_setup() was still called before loading the configuration section, so there was state to be cleaned up if the header checks failed.

However, commit 9e14b84908 ("migration/savevm: load_header before load_setup") has moved that configuration section part to qemu_loadvm_state_header() which now happens before qemu_loadvm_state_setup().

Quoted 7 lines
Quoted 3 lines

Remove the cleanup calls that are now misplaced.

Fixes 9e14b84908 ("migration/savevm: load_header before load_setup")

Considering it's a cleanup, do you mind if I further remove this Fixes but just mention it in commit message (so as to make Michael's life easier when backport)?

Sure, go ahead.

Quoted 28 lines
Quoted 28 lines
---
 migration/savevm.c | 2 --
 1 file changed, 2 deletions(-)
migration/savevm.c +0 -2
index d500eae979..d0759694fd 100644
@@ -2732,13 +2732,11 @@ static int qemu_loadvm_state_header(QEMUFile *f)
     if (migrate_get_current()->send_configuration) {
         if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
             error_report("Configuration section missing");
-            qemu_loadvm_state_cleanup();
             return -EINVAL;
         }
         ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
 
         if (ret) {
-            qemu_loadvm_state_cleanup();
             return ret;
         }
     }
Signature · 1 line
2.35.3
#4 lore raw

PATCH 2/2 migration/multifd: Fix rb->receivedmap cleanup race

In reply to Fabiano Rosas

Fix a segmentation fault in multifd when rb->receivedmap is cleared too early.

After commit 5ef7e26bdb ("migration/multifd: solve zero page causing multiple page faults"), multifd started using the rb->receivedmap bitmap, which belongs to ram.c and is initialized and *freed* from the ram SaveVMHandlers.

Multifd threads are live until migration_incoming_state_destroy(), which is called after qemu_loadvm_state_cleanup(), leading to a crash when accessing rb->receivedmap.

process_incoming_migration_co() ...
qemu_loadvm_state() multifd_nocomp_recv()

    qemu_loadvm_state_cleanup()          ramblock_recv_bitmap_set_offset()
      rb->receivedmap = NULL               set_bit_atomic(..., rb->receivedmap)

...
migration_incoming_state_destroy()
multifd_recv_cleanup()
multifd_recv_terminate_threads(NULL)

Move the loadvm cleanup into migration_incoming_state_destroy(), after multifd_recv_cleanup() to ensure multifd threads have already exited when rb->receivedmap is cleared.

Adjust the postcopy listen thread comment to indicate that we still want to skip the cpu synchronization.

Fixes 5ef7e26bdb ("migration/multifd: solve zero page causing multiple page faults")
---
 migration/migration.c | 1 +
 migration/savevm.c    | 6 ++++--
 2 files changed, 5 insertions(+), 2 deletions(-)
migration/migration.c +1 -0
index 3dea06d577..b190a574b1 100644
@@ -378,6 +378,7 @@ void migration_incoming_state_destroy(void)
     struct MigrationIncomingState *mis = migration_incoming_get_current();
 
     multifd_recv_cleanup();
+    qemu_loadvm_state_cleanup();
 
     if (mis->to_src_file) {
         /* Tell source that we are done */
migration/savevm.c +4 -2
index d0759694fd..7e1e27182a 100644
@@ -2979,7 +2979,10 @@ int qemu_loadvm_state(QEMUFile *f)
     trace_qemu_loadvm_state_post_main(ret);
 
     if (mis->have_listen_thread) {
-        /* Listen thread still going, can't clean up yet */
+        /*
+         * Postcopy listen thread still going, don't synchronize the
+         * cpus yet.
+         */
         return ret;
     }
 
@@ -3022,7 +3025,6 @@ int qemu_loadvm_state(QEMUFile *f)
         }
     }
 
-    qemu_loadvm_state_cleanup();
     cpu_synchronize_all_post_init();
 
     return ret;
Signature · 1 line
2.35.3
Reviewed-by on v1 2/2
#5 lore raw
Quoted 25 lines · Fabiano Rosas

Fix a segmentation fault in multifd when rb->receivedmap is cleared too early.

After commit 5ef7e26bdb ("migration/multifd: solve zero page causing multiple page faults"), multifd started using the rb->receivedmap bitmap, which belongs to ram.c and is initialized and *freed* from the ram SaveVMHandlers.

Multifd threads are live until migration_incoming_state_destroy(), which is called after qemu_loadvm_state_cleanup(), leading to a crash when accessing rb->receivedmap.

process_incoming_migration_co() ...
qemu_loadvm_state() multifd_nocomp_recv()

    qemu_loadvm_state_cleanup()          ramblock_recv_bitmap_set_offset()
      rb->receivedmap = NULL               set_bit_atomic(..., rb->receivedmap)

...
migration_incoming_state_destroy()
multifd_recv_cleanup()
multifd_recv_terminate_threads(NULL)

Move the loadvm cleanup into migration_incoming_state_destroy(), after multifd_recv_cleanup() to ensure multifd threads have already exited when rb->receivedmap is cleared.

Quoted 6 lines

Adjust the postcopy listen thread comment to indicate that we still want to skip the cpu synchronization.

Fixes 5ef7e26bdb ("migration/multifd: solve zero page causing multiple page faults")

One trivial question below..

migration/migration.c void migration_incoming_state_destroy(void) · line 380

Quoted 13 lines
---
 migration/migration.c | 1 +
 migration/savevm.c    | 6 ++++--
 2 files changed, 5 insertions(+), 2 deletions(-)
migration/migration.c +0 -0
index 3dea06d577..b190a574b1 100644
@@ -378,6 +378,7 @@ void migration_incoming_state_destroy(void)
     struct MigrationIncomingState *mis = migration_incoming_get_current();
 
     multifd_recv_cleanup();

Would you mind I add a comment squashed here when queue?

/*
* RAM state cleanup needs to happen after multifd cleanup, because
* multifd threads can use some of its states (receivedmap). */

Quoted 31 lines
migration/migration.c +1 -0
+    qemu_loadvm_state_cleanup();
 
     if (mis->to_src_file) {
         /* Tell source that we are done */
migration/savevm.c +4 -2
index d0759694fd..7e1e27182a 100644
@@ -2979,7 +2979,10 @@ int qemu_loadvm_state(QEMUFile *f)
     trace_qemu_loadvm_state_post_main(ret);
 
     if (mis->have_listen_thread) {
-        /* Listen thread still going, can't clean up yet */
+        /*
+         * Postcopy listen thread still going, don't synchronize the
+         * cpus yet.
+         */
         return ret;
     }
 
@@ -3022,7 +3025,6 @@ int qemu_loadvm_state(QEMUFile *f)
         }
     }
 
-    qemu_loadvm_state_cleanup();
     cpu_synchronize_all_post_init();
 
     return ret;
Signature · 1 line
2.35.3
Signature · 1 line
Peter Xu
#6 lore raw
Quoted 49 lines · Peter Xu
Quoted 25 lines · Fabiano Rosas

Fix a segmentation fault in multifd when rb->receivedmap is cleared too early.

After commit 5ef7e26bdb ("migration/multifd: solve zero page causing multiple page faults"), multifd started using the rb->receivedmap bitmap, which belongs to ram.c and is initialized and *freed* from the ram SaveVMHandlers.

Multifd threads are live until migration_incoming_state_destroy(), which is called after qemu_loadvm_state_cleanup(), leading to a crash when accessing rb->receivedmap.

process_incoming_migration_co() ...
qemu_loadvm_state() multifd_nocomp_recv()

    qemu_loadvm_state_cleanup()          ramblock_recv_bitmap_set_offset()
      rb->receivedmap = NULL               set_bit_atomic(..., rb->receivedmap)

...
migration_incoming_state_destroy()
multifd_recv_cleanup()
multifd_recv_terminate_threads(NULL)

Move the loadvm cleanup into migration_incoming_state_destroy(), after multifd_recv_cleanup() to ensure multifd threads have already exited when rb->receivedmap is cleared.

Quoted 6 lines

Adjust the postcopy listen thread comment to indicate that we still want to skip the cpu synchronization.

Fixes 5ef7e26bdb ("migration/multifd: solve zero page causing multiple page faults")

One trivial question below..

Quoted 12 lines
---
 migration/migration.c | 1 +
 migration/savevm.c    | 6 ++++--
 2 files changed, 5 insertions(+), 2 deletions(-)
migration/migration.c +0 -0
index 3dea06d577..b190a574b1 100644
@@ -378,6 +378,7 @@ void migration_incoming_state_destroy(void)
     struct MigrationIncomingState *mis = migration_incoming_get_current();
 
Quoted 8 lines

migration/migration.c void migration_incoming_state_destroy(void) · line 380

Quoted 1 line
migration/migration.c +0 -0
     multifd_recv_cleanup();

Would you mind I add a comment squashed here when queue?

/*
* RAM state cleanup needs to happen after multifd cleanup, because
* multifd threads can use some of its states (receivedmap). */

Yeah, that's ok.

Quoted 32 lines
Quoted 32 lines
migration/migration.c +1 -0
+    qemu_loadvm_state_cleanup();
 
     if (mis->to_src_file) {
         /* Tell source that we are done */
migration/savevm.c +4 -2
index d0759694fd..7e1e27182a 100644
@@ -2979,7 +2979,10 @@ int qemu_loadvm_state(QEMUFile *f)
     trace_qemu_loadvm_state_post_main(ret);
 
     if (mis->have_listen_thread) {
-        /* Listen thread still going, can't clean up yet */
+        /*
+         * Postcopy listen thread still going, don't synchronize the
+         * cpus yet.
+         */
         return ret;
     }
 
@@ -3022,7 +3025,6 @@ int qemu_loadvm_state(QEMUFile *f)
         }
     }
 
-    qemu_loadvm_state_cleanup();
     cpu_synchronize_all_post_init();
 
     return ret;
Signature · 1 line
2.35.3
#7 lore raw
In reply to Fabiano Rosas
Quoted 14 lines · Fabiano Rosas

v2: Keep skipping the cpu_synchronize_all_post_init() call if the postcopy listen thread is live. Don't copy stable on the first patch.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/1457418838
====
v1:
https://lore.kernel.org/r/[email protected]

This fixes the crash we've been seing recently in migration-test. The first patch is a cleanup to have only one place calling qemu_loadvm_state_cleanup() and the second patch reorders the cleanup calls to make multifd_recv_cleanup() run first and stop the recv threads.

Quoted 3 lines

Fabiano Rosas (2):
migration/savevm: Remove extra load cleanup calls migration/multifd: Fix rb->receivedmap cleanup race

queued.

Let's see whether this can quiesce all multifd cancel test failures.. If not, we can still follow that up.

Signature · 1 line
Peter Xu