Skip to contentSkip to conversation

migration: Fix state transition in postcopy_start() error handling

PATCH In review Juraj Marcin 4 participants 5 messages Aug 26, 2025 – Sep 29, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 migration: Fix state transition in postcopy_start() error handling

From: Juraj Marcin <[email protected]>

Commit 48814111366b ("migration: Always set DEVICE state") introduced DEVICE state to postcopy, which moved the actual state transition that leads to POSTCOPY_ACTIVE.

However, the error handling part of the postcopy_start() function still expects the state POSTCOPY_ACTIVE, but depending on where an error happens, now the state can be either ACTIVE, DEVICE or CANCELLING, but never POSTCOPY_ACTIVE, as this transition now happens just before a successful return from the function.

Instead, accept any state except CANCELLING when transitioning to FAILED state.

Fixes 48814111366b ("migration: Always set DEVICE state")

---
In the RFC[1] where this patch was discussed, there was also a suggestion for a helper function migrate_set_failure() that would check if the state is not CANCELLING and then set migration error and FAILED state. I discussed the implementation with Peter, and we came to a conclusion that instead of patching such clean-up on top of the current error handling code, it might be more useful to do a larger refactor and clean-up of all error handling in the migration code.

Such clean-up should reduce the number of places where we need to explicitly transition to a FAILED state (ideally to one, or only a couple of places), and instead only set an appropriate migration error using migrate_set_error(). Additionally, it would also refactor inappropriate uses of QEMUFile errors where the error is not really an error of the underlying channel and migrate_set_error() should be used instead.

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

---
 migration/migration.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
migration/migration.c +3 -2
index 10c216d25d..32b8ce5613 100644
@@ -2872,8 +2872,9 @@ static int postcopy_start(MigrationState *ms, Error **errp)
 fail_closefb:
     qemu_fclose(fb);
 fail:
-    migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
-                          MIGRATION_STATUS_FAILED);
+    if (ms->state != MIGRATION_STATUS_CANCELLING) {
+        migrate_set_state(&ms->state, ms->state, MIGRATION_STATUS_FAILED);
+    }
     migration_block_activate(NULL);
     migration_call_notifiers(ms, MIG_EVENT_PRECOPY_FAILED, NULL);
     bql_unlock();
Signature · 1 line
2.50.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

Commit 48814111366b ("migration: Always set DEVICE state") introduced DEVICE state to postcopy, which moved the actual state transition that leads to POSTCOPY_ACTIVE.

However, the error handling part of the postcopy_start() function still expects the state POSTCOPY_ACTIVE, but depending on where an error happens, now the state can be either ACTIVE, DEVICE or CANCELLING, but never POSTCOPY_ACTIVE, as this transition now happens just before a successful return from the function.

Quoted 6 lines

Instead, accept any state except CANCELLING when transitioning to FAILED state.

Fixes 48814111366b ("migration: Always set DEVICE state")

Thanks, Juraj!

Quoted 10 lines

---
In the RFC[1] where this patch was discussed, there was also a suggestion for a helper function migrate_set_failure() that would check if the state is not CANCELLING and then set migration error and FAILED state. I discussed the implementation with Peter, and we came to a conclusion that instead of patching such clean-up on top of the current error handling code, it might be more useful to do a larger refactor and clean-up of all error handling in the migration code.

Quoted 7 lines

Such clean-up should reduce the number of places where we need to explicitly transition to a FAILED state (ideally to one, or only a couple of places), and instead only set an appropriate migration error using migrate_set_error(). Additionally, it would also refactor inappropriate uses of QEMUFile errors where the error is not really an error of the underlying channel and migrate_set_error() should be used instead.

Fabiano: we discussed something around the FAILED status before as well. If you started working on something in this area, please shoot!

Quoted 25 lines

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

---
 migration/migration.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
migration/migration.c +3 -2
index 10c216d25d..32b8ce5613 100644
@@ -2872,8 +2872,9 @@ static int postcopy_start(MigrationState *ms, Error **errp)
 fail_closefb:
     qemu_fclose(fb);
 fail:
-    migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
-                          MIGRATION_STATUS_FAILED);
+    if (ms->state != MIGRATION_STATUS_CANCELLING) {
+        migrate_set_state(&ms->state, ms->state, MIGRATION_STATUS_FAILED);
+    }
     migration_block_activate(NULL);
     migration_call_notifiers(ms, MIG_EVENT_PRECOPY_FAILED, NULL);
     bql_unlock();
Signature · 1 line
2.50.1
Signature · 1 line
Peter Xu
Reviewed-by
#2 lore raw
Quoted 16 lines · Peter Xu
Quoted 15 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

Commit 48814111366b ("migration: Always set DEVICE state") introduced DEVICE state to postcopy, which moved the actual state transition that leads to POSTCOPY_ACTIVE.

However, the error handling part of the postcopy_start() function still expects the state POSTCOPY_ACTIVE, but depending on where an error happens, now the state can be either ACTIVE, DEVICE or CANCELLING, but never POSTCOPY_ACTIVE, as this transition now happens just before a successful return from the function.

Instead, accept any state except CANCELLING when transitioning to FAILED state.

Quoted 8 lines
Quoted 3 lines
Fixes 48814111366b ("migration: Always set DEVICE state")

Thanks, Juraj!

Quoted 18 lines
Quoted 10 lines

---
In the RFC[1] where this patch was discussed, there was also a suggestion for a helper function migrate_set_failure() that would check if the state is not CANCELLING and then set migration error and FAILED state. I discussed the implementation with Peter, and we came to a conclusion that instead of patching such clean-up on top of the current error handling code, it might be more useful to do a larger refactor and clean-up of all error handling in the migration code.

Quoted 7 lines

Such clean-up should reduce the number of places where we need to explicitly transition to a FAILED state (ideally to one, or only a couple of places), and instead only set an appropriate migration error using migrate_set_error(). Additionally, it would also refactor inappropriate uses of QEMUFile errors where the error is not really an error of the underlying channel and migrate_set_error() should be used instead.

Quoted 3 lines

Fabiano: we discussed something around the FAILED status before as well. If you started working on something in this area, please shoot!

I don't have anything planned, it's just the thread that I already linked in the previous version of this patch. Juraj is aware.

Quoted 25 lines
Quoted 25 lines

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

---
 migration/migration.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
migration/migration.c +3 -2
index 10c216d25d..32b8ce5613 100644
@@ -2872,8 +2872,9 @@ static int postcopy_start(MigrationState *ms, Error **errp)
 fail_closefb:
     qemu_fclose(fb);
 fail:
-    migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
-                          MIGRATION_STATUS_FAILED);
+    if (ms->state != MIGRATION_STATUS_CANCELLING) {
+        migrate_set_state(&ms->state, ms->state, MIGRATION_STATUS_FAILED);
+    }
     migration_block_activate(NULL);
     migration_call_notifiers(ms, MIG_EVENT_PRECOPY_FAILED, NULL);
     bql_unlock();
Signature · 1 line
2.50.1
#3 lore raw
In reply to Juraj Marcin
Quoted 36 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

Commit 48814111366b ("migration: Always set DEVICE state") introduced DEVICE state to postcopy, which moved the actual state transition that leads to POSTCOPY_ACTIVE.

However, the error handling part of the postcopy_start() function still expects the state POSTCOPY_ACTIVE, but depending on where an error happens, now the state can be either ACTIVE, DEVICE or CANCELLING, but never POSTCOPY_ACTIVE, as this transition now happens just before a successful return from the function.

Instead, accept any state except CANCELLING when transitioning to FAILED state.

Fixes 48814111366b ("migration: Always set DEVICE state")

---
In the RFC[1] where this patch was discussed, there was also a suggestion for a helper function migrate_set_failure() that would check if the state is not CANCELLING and then set migration error and FAILED state. I discussed the implementation with Peter, and we came to a conclusion that instead of patching such clean-up on top of the current error handling code, it might be more useful to do a larger refactor and clean-up of all error handling in the migration code.

Such clean-up should reduce the number of places where we need to explicitly transition to a FAILED state (ideally to one, or only a couple of places), and instead only set an appropriate migration error using migrate_set_error(). Additionally, it would also refactor inappropriate uses of QEMUFile errors where the error is not really an error of the underlying channel and migrate_set_error() should be used instead.

Quoted 1 line

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

Ping? Can we apply this to the master branch, so I can pick it up for the stable series?

Thanks,

/mjt

#4 lore raw
Quoted 37 lines · Michael Tokarev
Quoted 36 lines · Juraj Marcin

From: Juraj Marcin <[email protected]>

Commit 48814111366b ("migration: Always set DEVICE state") introduced DEVICE state to postcopy, which moved the actual state transition that leads to POSTCOPY_ACTIVE.

However, the error handling part of the postcopy_start() function still expects the state POSTCOPY_ACTIVE, but depending on where an error happens, now the state can be either ACTIVE, DEVICE or CANCELLING, but never POSTCOPY_ACTIVE, as this transition now happens just before a successful return from the function.

Instead, accept any state except CANCELLING when transitioning to FAILED state.

Fixes 48814111366b ("migration: Always set DEVICE state")

---
In the RFC[1] where this patch was discussed, there was also a suggestion for a helper function migrate_set_failure() that would check if the state is not CANCELLING and then set migration error and FAILED state. I discussed the implementation with Peter, and we came to a conclusion that instead of patching such clean-up on top of the current error handling code, it might be more useful to do a larger refactor and clean-up of all error handling in the migration code.

Such clean-up should reduce the number of places where we need to explicitly transition to a FAILED state (ideally to one, or only a couple of places), and instead only set an appropriate migration error using migrate_set_error(). Additionally, it would also refactor inappropriate uses of QEMUFile errors where the error is not really an error of the underlying channel and migrate_set_error() should be used instead.

Quoted 4 lines
Quoted 1 line

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

Ping? Can we apply this to the master branch, so I can pick it up for the stable series?

Apologies for the delay, queued. Will send the PR this week.

Signature · 1 line
Peter Xu