Skip to contentSkip to conversation

migration: Fix RP shutdown order

PATCH In review Fabiano Rosas 2 participants 4 messages Feb 26, 2024 – Feb 27, 2024

lore
Share & tools
lore
#0 lore raw

cover migration: Fix RP shutdown order

These were extracted from:
[PATCH 00/14] migration: Improve error reporting
https://lore.kernel.org/r/[email protected]

We're currently relying on the presence of a QEMUFile error in the to_dst_file to infer whether the return path file (rp_state.from_dst_file) might be hanging at a recvmsg() system call.

This has always been buggy because we actually clear the to_dst_file pointer and close the file before attempting any of the above.

Move the RP cleanup before the to_dst_file cleanup, at both the migrate_fd_cleanup() and postcopy_pause() paths to make sure we reference a valid and open to_dst_file.

Also replace the error checking to use s->error instead of f->last_error. This removes one more dependency on QEMUFile::last_error.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/1191131909

Cédric Le Goater (1):
migration: Use migrate_has_error() in close_return_path_on_source()

Fabiano Rosas (1):
migration: Join the return path thread before releasing to_dst_file

 migration/migration.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/2 migration: Join the return path thread before releasing to_dst_file

The return path thread might hang at a blocking system call. Before joining the thread we might need to issue a shutdown() on the socket file descriptor to release it. To determine whether the shutdown() is necessary we look at the QEMUFile error.

Make sure we only clean up the QEMUFile after the return path has been waited for.

This fixes a hang when qemu_savevm_state_setup() produced an error that was detected by migration_detect_error(). That skips migration_completion() so close_return_path_on_source() would get stuck waiting for the RP thread to terminate.

---
 migration/migration.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
migration/migration.c +9 -13
index ab21de2cad..3161be7cde 100644
@@ -1326,6 +1326,8 @@ static void migrate_fd_cleanup(MigrationState *s)
 
     qemu_savevm_state_cleanup();
 
+    close_return_path_on_source(s);
+
     if (s->to_dst_file) {
         QEMUFile *tmp;
 
@@ -1350,12 +1352,6 @@ 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.
-     */
-    close_return_path_on_source(s);
-
     assert(!migration_is_active(s));
 
     if (s->state == MIGRATION_STATUS_CANCELLING) {
@@ -2874,6 +2870,13 @@ static MigThrError postcopy_pause(MigrationState *s)
     while (true) {
         QEMUFile *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.
+         */
+        close_return_path_on_source(s);
+
         /*
          * Current channel is possibly broken. Release it.  Note that this is
          * guaranteed even without lock because to_dst_file should only be
@@ -2893,13 +2896,6 @@ 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.
-         */
-        close_return_path_on_source(s);
-
         migrate_set_state(&s->state, s->state,
                           MIGRATION_STATUS_POSTCOPY_PAUSED);
 
Signature · 1 line
2.35.3
#2 lore raw

PATCH 2/2 migration: Use migrate_has_error() in close_return_path_on_source()

In reply to Fabiano Rosas

From: Cédric Le Goater <[email protected]>

close_return_path_on_source() retrieves the migration error from the the QEMUFile '->to_dst_file' to know if a shutdown is required. This shutdown is required to exit the return-path thread.

Avoid relying on '->to_dst_file' and use migrate_has_error() instead.

(using to_dst_file is a heuristic to infer whether rp_state.from_dst_file might be stuck on a recvmsg(). Using a generic method for detecting errors is more reliable. We also want to reduce dependency on QEMUFile::last_error)

[added some words about the motivation for this patch]
---
 migration/migration.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
migration/migration.c +1 -2
index 3161be7cde..5316bbe670 100644
@@ -2380,8 +2380,7 @@ static bool close_return_path_on_source(MigrationState *ms)
      * cause it to unblock if it's stuck waiting for the destination.
      */
     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)) {
+        if (migrate_has_error(ms) && ms->rp_state.from_dst_file) {
             qemu_file_shutdown(ms->rp_state.from_dst_file);
         }
     }
Signature · 1 line
2.35.3
#3 lore raw
In reply to Fabiano Rosas
Quoted 21 lines · Fabiano Rosas

These were extracted from:
[PATCH 00/14] migration: Improve error reporting
https://lore.kernel.org/r/[email protected]

We're currently relying on the presence of a QEMUFile error in the to_dst_file to infer whether the return path file (rp_state.from_dst_file) might be hanging at a recvmsg() system call.

This has always been buggy because we actually clear the to_dst_file pointer and close the file before attempting any of the above.

Move the RP cleanup before the to_dst_file cleanup, at both the migrate_fd_cleanup() and postcopy_pause() paths to make sure we reference a valid and open to_dst_file.

Also replace the error checking to use s->error instead of f->last_error. This removes one more dependency on QEMUFile::last_error.

CI run: https://gitlab.com/farosas/qemu/-/pipelines/1191131909

Quoted 8 lines

Cédric Le Goater (1):
migration: Use migrate_has_error() in close_return_path_on_source()

Fabiano Rosas (1):
migration: Join the return path thread before releasing to_dst_file

 migration/migration.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

Queued for now. Comments / tags still welcomed if any.

Signature · 1 line
Peter Xu