Skip to contentSkip to conversation

migration: Two migration bug fixes

PATCH Applied Avihai Horon 3 participants 11 messages Mar 28, 2024

lore
Share & tools
lore
#0 lore raw

cover migration: Two migration bug fixes

Hello,

This small series fixes two migration bugs I stumbled upon recently. Comments are welcome, thanks for reviewing.

Avihai Horon (2):
migration: Set migration error in migration_completion() migration/postcopy: Ensure postcopy_start() sets errp if it fails

 migration/migration.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
Signature · 1 line
2.26.3
#1 lore raw

PATCH 1/2 migration: Set migration error in migration_completion()

After commit 9425ef3f990a ("migration: Use migrate_has_error() in close_return_path_on_source()"), close_return_path_on_source() assumes that migration error is set if an error occurs during migration.

This may not be true if migration errors in migration_completion(). For example, if qemu_savevm_state_complete_precopy() errors, migration error will not be set.

This in turn, will cause a migration hang bug, similar to the bug that was fixed by commit 22b04245f0d5 ("migration: Join the return path thread before releasing to_dst_file"), as shutdown() will not be issued for the return-path channel.

Fix it by ensuring migration error is set in case of error in migration_completion().

---
 migration/migration.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
migration/migration.c +10 -0
index 9fe8fd2afd7..b73ae3a72c4 100644
@@ -2799,6 +2799,7 @@ static void migration_completion(MigrationState *s)
 {
     int ret = 0;
     int current_active_state = s->state;
+    Error *local_err = NULL;
 
     if (s->state == MIGRATION_STATUS_ACTIVE) {
         ret = migration_completion_precopy(s, &current_active_state);
@@ -2832,6 +2833,15 @@ static void migration_completion(MigrationState *s)
     return;
 
 fail:
+    if (qemu_file_get_error_obj(s->to_dst_file, &local_err)) {
+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    } else if (ret) {
+        error_setg_errno(&local_err, -ret, "Error in migration completion");
+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    }
+
     migration_completion_failed(s, current_active_state);
 }
 
Signature · 1 line
2.26.3
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH for-9.0 1/2] migration: Set migration error in migration_completion()

Quoted 13 lines · Avihai Horon

After commit 9425ef3f990a ("migration: Use migrate_has_error() in close_return_path_on_source()"), close_return_path_on_source() assumes that migration error is set if an error occurs during migration.

This may not be true if migration errors in migration_completion(). For example, if qemu_savevm_state_complete_precopy() errors, migration error will not be set.

This in turn, will cause a migration hang bug, similar to the bug that was fixed by commit 22b04245f0d5 ("migration: Join the return path thread before releasing to_dst_file"), as shutdown() will not be issued for the return-path channel.

Quoted 4 lines

Fix it by ensuring migration error is set in case of error in migration_completion().

I'll attach this if it looks all right to you:

Fixes 9425ef3f990a ("migration: Use migrate_has_error() in close_return_path_on_source()")

Thanks,

Quoted 36 lines
---
 migration/migration.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
migration/migration.c +10 -0
index 9fe8fd2afd7..b73ae3a72c4 100644
@@ -2799,6 +2799,7 @@ static void migration_completion(MigrationState *s)
 {
     int ret = 0;
     int current_active_state = s->state;
+    Error *local_err = NULL;
 
     if (s->state == MIGRATION_STATUS_ACTIVE) {
         ret = migration_completion_precopy(s, &current_active_state);
@@ -2832,6 +2833,15 @@ static void migration_completion(MigrationState *s)
     return;
 
 fail:
+    if (qemu_file_get_error_obj(s->to_dst_file, &local_err)) {
+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    } else if (ret) {
+        error_setg_errno(&local_err, -ret, "Error in migration completion");
+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    }
+
     migration_completion_failed(s, current_active_state);
 }
 
Signature · 1 line
2.26.3
Signature · 1 line
Peter Xu
#3 lore raw

Re: [PATCH for-9.0 1/2] migration: Set migration error in migration_completion()

Quoted 20 lines · Peter Xu

External email: Use caution opening links or attachments

Quoted 16 lines · Avihai Horon

After commit 9425ef3f990a ("migration: Use migrate_has_error() in close_return_path_on_source()"), close_return_path_on_source() assumes that migration error is set if an error occurs during migration.

This may not be true if migration errors in migration_completion(). For example, if qemu_savevm_state_complete_precopy() errors, migration error will not be set.

This in turn, will cause a migration hang bug, similar to the bug that was fixed by commit 22b04245f0d5 ("migration: Join the return path thread before releasing to_dst_file"), as shutdown() will not be issued for the return-path channel.

Fix it by ensuring migration error is set in case of error in migration_completion().

Quoted 6 lines
Quoted 1 line

I'll attach this if it looks all right to you:

Fixes 9425ef3f990a ("migration: Use migrate_has_error() in close_return_path_on_source()")

Yes, sure, go ahead.

Thanks.

Quoted 42 lines

Thanks,

Quoted 33 lines
---
  migration/migration.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
migration/migration.c +10 -1
index 9fe8fd2afd7..b73ae3a72c4 100644
@@ -2799,6 +2799,7 @@ static void migration_completion(MigrationState *s)
  {
      int ret = 0;
      int current_active_state = s->state;
+    Error *local_err = NULL;
 
      if (s->state == MIGRATION_STATUS_ACTIVE) {
          ret = migration_completion_precopy(s, &current_active_state);
@@ -2832,6 +2833,15 @@ static void migration_completion(MigrationState *s)
      return;
 
  fail:
+    if (qemu_file_get_error_obj(s->to_dst_file, &local_err)) {
+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    } else if (ret) {
+        error_setg_errno(&local_err, -ret, "Error in migration completion");
+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    }
+
      migration_completion_failed(s, current_active_state);
  }
 
--
Quoted 3 lines

2.26.3

--
Peter Xu

#4 lore raw

Re: [PATCH for-9.0 1/2] migration: Set migration error in migration_completion()

In reply to Avihai Horon

Hello Avihai,

Quoted 7 lines · Avihai Horon

After commit 9425ef3f990a ("migration: Use migrate_has_error() in close_return_path_on_source()"), close_return_path_on_source() assumes that migration error is set if an error occurs during migration.

This may not be true if migration errors in migration_completion(). For example, if qemu_savevm_state_complete_precopy() errors, migration error will not be set

Out of curiosity, could you describe a bit more the context ? Did vfio_save_complete_precopy() fail ? why ?

We should propagate errors of .save_live_complete_precopy() handlers as it was done .save_setup handlers(). For 9.1.

Quoted 4 lines

This in turn, will cause a migration hang bug, similar to the bug that was fixed by commit 22b04245f0d5 ("migration: Join the return path thread before releasing to_dst_file"), as shutdown() will not be issued for the return-path channel.

yes, but this test :

    if (ret < 0) {
        goto fail;
    }

will skip the close_return_path_on_source() call. Won't it ? So I don't understand how it can be an issue. Am I missing something ?

Quoted 2 lines

Fix it by ensuring migration error is set in case of error in migration_completion().

Why didn't you add a reference to commit 9425ef3f990a ?

Quoted 18 lines
---
  migration/migration.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
migration/migration.c +1 -0
index 9fe8fd2afd7..b73ae3a72c4 100644
@@ -2799,6 +2799,7 @@ static void migration_completion(MigrationState *s)
  {
      int ret = 0;
      int current_active_state = s->state;
+    Error *local_err = NULL;
 
      if (s->state == MIGRATION_STATUS_ACTIVE) {
          ret = migration_completion_precopy(s, &current_active_state);

migration/migration.c static void migration_completion(MigrationState *s) · line 2840

Quoted 8 lines
migration/migration.c +5 -0
@@ -2832,6 +2833,15 @@ static void migration_completion(MigrationState *s)
      return;
 
  fail:
+    if (qemu_file_get_error_obj(s->to_dst_file, &local_err)) {
+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    } else if (ret) {
+        error_setg_errno(&local_err, -ret, "Error in migration completion");

The 'ret = -1' case could be improved with error_setg(). As a followup.

Thanks,

C.

Quoted 7 lines
migration/migration.c +4 -0
+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    }
+
      migration_completion_failed(s, current_active_state);
  }
 
#5 lore raw

Re: [PATCH for-9.0 1/2] migration: Set migration error in migration_completion()

Quoted 10 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello Avihai,

Quoted 4 lines · Avihai Horon

After commit 9425ef3f990a ("migration: Use migrate_has_error() in close_return_path_on_source()"), close_return_path_on_source() assumes that migration error is set if an error occurs during migration.

Quoted 6 lines
Quoted 3 lines

This may not be true if migration errors in migration_completion(). For example, if qemu_savevm_state_complete_precopy() errors, migration error will not be set

Out of curiosity, could you describe a bit more the context ? Did vfio_save_complete_precopy() fail ? why ?

Yep, vfio_save_complete_precopy() failed (but it failed while I was experimenting with an unofficial debug FW).

Quoted 3 lines

We should propagate errors of .save_live_complete_precopy() handlers as it was done .save_setup handlers(). For 9.1.

Agreed.

Quoted 6 lines
Quoted 5 lines

This in turn, will cause a migration hang bug, similar to the bug that was fixed by commit 22b04245f0d5 ("migration: Join the return path thread before releasing to_dst_file"), as shutdown() will not be issued for the return-path channel.

Quoted 8 lines

yes, but this test :

    if (ret < 0) {
        goto fail;
    }

will skip the close_return_path_on_source() call. Won't it ? So I don't understand how it can be an issue. Am I missing something ?

It will skip the close_return_path_on_source() call in migration_completion(), but there is another close_return_path_on_source() call in migrate_fd_cleanup().

Quoted 5 lines
Quoted 3 lines

Fix it by ensuring migration error is set in case of error in migration_completion().

Why didn't you add a reference to commit 9425ef3f990a ?

I thought this commit didn't introduce this bug, but looking again in the mailing list [1], it kinda did:
The hang bug was fully fixed by commit 22b04245f0d ("migration: Join the return path thread before releasing to_dst_file") and then 9425ef3f990a re-introduced the bug, but only for migration_completion() case. So, you are right, a fixes line with 9425ef3f990a should be added.

Thanks.

[1] https://lore.kernel.org/all/[email protected]/

Quoted 49 lines
Quoted 24 lines

---
  migration/migration.c | 10 ++++++++++

  1 file changed, 10 insertions(+)
migration/migration.c +1 -0
index 9fe8fd2afd7..b73ae3a72c4 100644
@@ -2799,6 +2799,7 @@ static void migration_completion(MigrationState 
 *s)
   {
       int ret = 0;
       int current_active_state = s->state;
+    Error *local_err = NULL;
 
       if (s->state == MIGRATION_STATUS_ACTIVE) {

          ret = migration_completion_precopy(s, &current_active_state);

migration/migration.c +0 -0
@@ -2832,6 +2833,15 @@ static void 
 migration_completion(MigrationState *s)
       return;
 
Quoted 7 lines

  fail:
+    if (qemu_file_get_error_obj(s->to_dst_file, &local_err)) { +        migrate_set_error(s, local_err);
+        error_free(local_err);
+    } else if (ret) {
+        error_setg_errno(&local_err, -ret, "Error in migration completion");

The 'ret = -1' case could be improved with error_setg(). As a followup.

Thanks,

C.

Quoted 7 lines

+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    }
+
      migration_completion_failed(s, current_active_state); }

#6 lore raw

Re: [PATCH for-9.0 1/2] migration: Set migration error in migration_completion()

Quoted 35 lines · Avihai Horon
Quoted 10 lines · Cédric Le Goater

External email: Use caution opening links or attachments

Hello Avihai,

Quoted 4 lines · Avihai Horon

After commit 9425ef3f990a ("migration: Use migrate_has_error() in close_return_path_on_source()"), close_return_path_on_source() assumes that migration error is set if an error occurs during migration.

Quoted 6 lines
Quoted 3 lines

This may not be true if migration errors in migration_completion(). For example, if qemu_savevm_state_complete_precopy() errors, migration error will not be set

Out of curiosity, could you describe a bit more the context ? Did vfio_save_complete_precopy() fail ? why ?

Yep, vfio_save_complete_precopy() failed (but it failed while I was experimenting with an unofficial debug FW).

Quoted 3 lines

We should propagate errors of .save_live_complete_precopy() handlers as it was done .save_setup handlers(). For 9.1.

Agreed.

Quoted 8 lines
Quoted 5 lines

This in turn, will cause a migration hang bug, similar to the bug that was fixed by commit 22b04245f0d5 ("migration: Join the return path thread before releasing to_dst_file"), as shutdown() will not be issued for the return-path channel.

yes, but this test :

Quoted 8 lines
Quoted 6 lines

    if (ret < 0) {
        goto fail;
    }

will skip the close_return_path_on_source() call. Won't it ? So I don't understand how it can be an issue. Am I missing something ?

It will skip the close_return_path_on_source() call in migration_completion(), but there is another close_return_path_on_source() call in migrate_fd_cleanup().

OK. Found it. This is a code path I hadn't explored yet.

Thanks,

C.

Quoted 62 lines
Quoted 6 lines
Quoted 4 lines

Fix it by ensuring migration error is set in case of error in migration_completion().

Why didn't you add a reference to commit 9425ef3f990a ?

I thought this commit didn't introduce this bug, but looking again in the mailing list [1], it kinda did:
The hang bug was fully fixed by commit 22b04245f0d ("migration: Join the return path thread before releasing to_dst_file") and then 9425ef3f990a re-introduced the bug, but only for migration_completion() case. So, you are right, a fixes line with 9425ef3f990a should be added.

Thanks.

[1] https://lore.kernel.org/all/[email protected]/

Quoted 46 lines
Quoted 22 lines

---
  migration/migration.c | 10 ++++++++++

  1 file changed, 10 insertions(+)
migration/migration.c +1 -0
index 9fe8fd2afd7..b73ae3a72c4 100644
@@ -2799,6 +2799,7 @@ static void migration_completion(MigrationState *s)
   {
       int ret = 0;
       int current_active_state = s->state;
+    Error *local_err = NULL;
 
       if (s->state == MIGRATION_STATUS_ACTIVE) {
           ret = migration_completion_precopy(s, &current_active_state);
@@ -2832,6 +2833,15 @@ static void migration_completion(MigrationState *s)
       return;
 
Quoted 6 lines

  fail:
+    if (qemu_file_get_error_obj(s->to_dst_file, &local_err)) {
+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    } else if (ret) {
+        error_setg_errno(&local_err, -ret, "Error in migration completion");

The 'ret = -1' case could be improved with error_setg(). As a followup.

Thanks,

C.

Quoted 7 lines

+        migrate_set_error(s, local_err);
+        error_free(local_err);
+    }
+
      migration_completion_failed(s, current_active_state); }

#7 lore raw

PATCH 2/2 migration/postcopy: Ensure postcopy_start() sets errp if it fails

In reply to Avihai Horon

There are several places where postcopy_start() fails without setting errp. This can cause a null pointer de-reference, as in case of error, the caller of postcopy_start() copies/prints the error set in errp.

Fix it by setting errp in all of postcopy_start() error paths.

Fixes 908927db28ea ("migration: Update error description whenever migration fails")
---
 migration/migration.c | 8 ++++++++
 1 file changed, 8 insertions(+)
migration/migration.c +8 -0
index b73ae3a72c4..86bf76e9258 100644
@@ -2510,6 +2510,8 @@ static int postcopy_start(MigrationState *ms, Error **errp)
         migration_wait_main_channel(ms);
         if (postcopy_preempt_establish_channel(ms)) {
             migrate_set_state(&ms->state, ms->state, MIGRATION_STATUS_FAILED);
+            error_setg(errp, "%s: Failed to establish preempt channel",
+                       __func__);
             return -1;
         }
     }
@@ -2525,17 +2527,22 @@ static int postcopy_start(MigrationState *ms, Error **errp)
 
     ret = migration_stop_vm(ms, RUN_STATE_FINISH_MIGRATE);
     if (ret < 0) {
+        error_setg_errno(errp, -ret, "%s: Failed to stop the VM", __func__);
         goto fail;
     }
 
     ret = migration_maybe_pause(ms, &cur_state,
                                 MIGRATION_STATUS_POSTCOPY_ACTIVE);
     if (ret < 0) {
+        error_setg_errno(errp, -ret, "%s: Failed in migration_maybe_pause()",
+                         __func__);
         goto fail;
     }
 
     ret = bdrv_inactivate_all();
     if (ret < 0) {
+        error_setg_errno(errp, -ret, "%s: Failed in bdrv_inactivate_all()",
+                         __func__);
         goto fail;
     }
     restart_block = true;
@@ -2612,6 +2619,7 @@ static int postcopy_start(MigrationState *ms, Error **errp)
 
     /* Now send that blob */
     if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
+        error_setg(errp, "%s: Failed to send packaged data", __func__);
         goto fail_closefb;
     }
     qemu_fclose(fb);
Signature · 1 line
2.26.3
Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH for-9.0 2/2] migration/postcopy: Ensure postcopy_start() sets errp if it fails

Quoted 8 lines · Avihai Horon

There are several places where postcopy_start() fails without setting errp. This can cause a null pointer de-reference, as in case of error, the caller of postcopy_start() copies/prints the error set in errp.

Fix it by setting errp in all of postcopy_start() error paths.

Fixes 908927db28ea ("migration: Update error description whenever migration fails")

Thanks,

C.

Quoted 48 lines
---
  migration/migration.c | 8 ++++++++
  1 file changed, 8 insertions(+)
migration/migration.c +8 -0
index b73ae3a72c4..86bf76e9258 100644
@@ -2510,6 +2510,8 @@ static int postcopy_start(MigrationState *ms, Error **errp)
          migration_wait_main_channel(ms);
          if (postcopy_preempt_establish_channel(ms)) {
              migrate_set_state(&ms->state, ms->state, MIGRATION_STATUS_FAILED);
+            error_setg(errp, "%s: Failed to establish preempt channel",
+                       __func__);
              return -1;
          }
      }
@@ -2525,17 +2527,22 @@ static int postcopy_start(MigrationState *ms, Error **errp)
 
      ret = migration_stop_vm(ms, RUN_STATE_FINISH_MIGRATE);
      if (ret < 0) {
+        error_setg_errno(errp, -ret, "%s: Failed to stop the VM", __func__);
          goto fail;
      }
 
      ret = migration_maybe_pause(ms, &cur_state,
                                  MIGRATION_STATUS_POSTCOPY_ACTIVE);
      if (ret < 0) {
+        error_setg_errno(errp, -ret, "%s: Failed in migration_maybe_pause()",
+                         __func__);
          goto fail;
      }
 
      ret = bdrv_inactivate_all();
      if (ret < 0) {
+        error_setg_errno(errp, -ret, "%s: Failed in bdrv_inactivate_all()",
+                         __func__);
          goto fail;
      }
      restart_block = true;
@@ -2612,6 +2619,7 @@ static int postcopy_start(MigrationState *ms, Error **errp)
 
      /* Now send that blob */
      if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) {
+        error_setg(errp, "%s: Failed to send packaged data", __func__);
          goto fail_closefb;
      }
      qemu_fclose(fb);
Reviewed-by on v1 2/2
#9 lore raw

Re: [PATCH for-9.0 2/2] migration/postcopy: Ensure postcopy_start() sets errp if it fails

In reply to Avihai Horon
Quoted 7 lines · Avihai Horon

There are several places where postcopy_start() fails without setting errp. This can cause a null pointer de-reference, as in case of error, the caller of postcopy_start() copies/prints the error set in errp.

Fix it by setting errp in all of postcopy_start() error paths.

Fixes 908927db28ea ("migration: Update error description whenever migration fails")

I think this should need:

Cc qemu-stable <[email protected]>
Quoted 1 line
Signature · 1 line
Peter Xu
Applied
#10 lore raw
In reply to Avihai Horon
Quoted 8 lines · Avihai Horon

Hello,

This small series fixes two migration bugs I stumbled upon recently. Comments are welcome, thanks for reviewing.

Avihai Horon (2):
migration: Set migration error in migration_completion() migration/postcopy: Ensure postcopy_start() sets errp if it fails

queued for 9.0-rc2, thanks.

Signature · 1 line
Peter Xu