Skip to contentSkip to conversation

Fix stop/cont vs. connect races

PATCH Superseded Paolo Bonzini 2 participants 8 messages Oct 19, 2012 – Oct 23, 2012

lore
Share & tools
lore
#0 lore raw

v2 cover Fix stop/cont vs. connect races

Here are two patches to make the handling more uniform when stop/cont commands are sent to a QEMU instance that is waiting for incoming migration. Right now the handling will change, depending on whether the command came before or after the source has connected.

The behavior that makes most sense is the one you have if the command comes while migration is in progress and the monitor is blocked, so ensure that we always resolve the race like that.

This will also help when incoming migration is moved to a coroutine, which (by design) will cause the monitor not to block.

Paolo Bonzini (2):
qmp: handle stop/cont in INMIGRATE state
migration: go to paused state after finishing incoming migration with -S

 migration.c      |  2 +-
 qapi-schema.json |  7 +------
 qerror.h         |  3 ---
 qmp.c            | 17 +++++++++++------
 vl.c             |  2 +-

5 file modificati, 14 inserzioni(+), 17 rimozioni(-)

Signature · 1 line
1.7.12.1
#1 lore raw

PATCH v2 1/2 qmp: handle stop/cont in INMIGRATE state

Right now, stop followed by an incoming migration will let the virtual machine start. cont before an incoming migration instead will fail.

This is bad because the actual behavior is not predictable; it is racy with respect to the start of the incoming migration. That's because incoming migration is blocking, and thus will delay the processing of stop/cont until the end of the migration.

In addition, there's nothing that really prevents the user from typing the block device's passwords before incoming migration is done, so returning the DeviceEncrypted error is also helpful in the QMP case.

Both things can be fixed by just toggling the autostart variable when stop/cont are called in INMIGRATE state.

Note that libvirt is currently working around the race by looping if the MigrationExpected answer is returned. After this patch, the command will return right away without ever raising an error.

---

        v1->v2: kill MigrationExpected and QERR_MIGRATION_EXPECTED, yay!
 qapi-schema.json |  7 +------
 qerror.h         |  3 ---
 qmp.c            | 17 +++++++++++------

3 file modificati, 12 inserzioni(+), 15 rimozioni(-)

qapi-schema.json +1 -6
index c615ee2..7e102c5 100644
@@ -22,15 +22,11 @@
 # @KVMMissingCap: the requested operation can't be fulfilled because a
 #                 required KVM capability is missing
 #
-# @MigrationExpected: the requested operation can't be fulfilled because a
-#                     migration process is expected
-#
 # Since: 1.2
 ##
 { 'enum': 'ErrorClass',
   'data': [ 'GenericError', 'CommandNotFound', 'DeviceEncrypted',
-            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap',
-            'MigrationExpected' ] }
+            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap' ] }
 
 ##
 # @add_client
@@ -1299,7 +1295,6 @@
 # Since:  0.14.0
 #
 # Returns:  If successful, nothing
-#           If the QEMU is waiting for an incoming migration, MigrationExpected
 #           If QEMU was started with an encrypted block device and a key has
 #              not yet been set, DeviceEncrypted.
 #
qerror.h +0 -3
index c91708c..5e98a39 100644
@@ -165,9 +165,6 @@ void assert_no_error(Error *err);
 #define QERR_MIGRATION_NOT_SUPPORTED \
     ERROR_CLASS_GENERIC_ERROR, "State blocked by non-migratable device '%s'"
 
-#define QERR_MIGRATION_EXPECTED \
-    ERROR_CLASS_MIGRATION_EXPECTED, "An incoming migration is expected before this command can be executed"
-
 #define QERR_MISSING_PARAMETER \
     ERROR_CLASS_GENERIC_ERROR, "Parameter '%s' is missing"
 
qmp.c +11 -6
index 36c54c5..2c8d559 100644
@@ -85,7 +85,11 @@ void qmp_quit(Error **err)
 
 void qmp_stop(Error **errp)
 {
-    vm_stop(RUN_STATE_PAUSED);
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        autostart = 0;
+    } else {
+        vm_stop(RUN_STATE_PAUSED);
+    }
 }
 
 void qmp_system_reset(Error **errp)
@@ -144,10 +148,7 @@ void qmp_cont(Error **errp)
 {
     Error *local_err = NULL;
 
-    if (runstate_check(RUN_STATE_INMIGRATE)) {
-        error_set(errp, QERR_MIGRATION_EXPECTED);
-        return;
-    } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
+    if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
                runstate_check(RUN_STATE_SHUTDOWN)) {
         error_set(errp, QERR_RESET_REQUIRED);
         return;
@@ -162,7 +163,11 @@ void qmp_cont(Error **errp)
         return;
     }
 
-    vm_start();
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        autostart = 1;
+    } else {
+        vm_start();
+    }
 }
 
 void qmp_system_wakeup(Error **errp)
Signature · 1 line
1.7.12.1
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] qmp: handle stop/cont in INMIGRATE state

Quoted 14 lines · On Fri, 19 Oct 2012 16:45:23 +0200 Paolo Bonzini <[email protected]> wrote:

Right now, stop followed by an incoming migration will let the virtual machine start. cont before an incoming migration instead will fail.

This is bad because the actual behavior is not predictable; it is racy with respect to the start of the incoming migration. That's because incoming migration is blocking, and thus will delay the processing of stop/cont until the end of the migration.

In addition, there's nothing that really prevents the user from typing the block device's passwords before incoming migration is done, so returning the DeviceEncrypted error is also helpful in the QMP case.

Quoted 8 lines

Both things can be fixed by just toggling the autostart variable when stop/cont are called in INMIGRATE state.

Note that libvirt is currently working around the race by looping if the MigrationExpected answer is returned. After this patch, the command will return right away without ever raising an error.

Could you please add a note in cont's and stop's entries in qapi-schema.json explaining their behavior on INMIGRATE?

Otherwise series looks good.

Quoted 93 lines

---

        v1->v2: kill MigrationExpected and QERR_MIGRATION_EXPECTED, yay!
 qapi-schema.json |  7 +------
 qerror.h         |  3 ---
 qmp.c            | 17 +++++++++++------

3 file modificati, 12 inserzioni(+), 15 rimozioni(-)

qapi-schema.json +1 -6
index c615ee2..7e102c5 100644
@@ -22,15 +22,11 @@
 # @KVMMissingCap: the requested operation can't be fulfilled because a
 #                 required KVM capability is missing
 #
-# @MigrationExpected: the requested operation can't be fulfilled because a
-#                     migration process is expected
-#
 # Since: 1.2
 ##
 { 'enum': 'ErrorClass',
   'data': [ 'GenericError', 'CommandNotFound', 'DeviceEncrypted',
-            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap',
-            'MigrationExpected' ] }
+            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap' ] }
 
 ##
 # @add_client
@@ -1299,7 +1295,6 @@
 # Since:  0.14.0
 #
 # Returns:  If successful, nothing
-#           If the QEMU is waiting for an incoming migration, MigrationExpected
 #           If QEMU was started with an encrypted block device and a key has
 #              not yet been set, DeviceEncrypted.
 #
qerror.h +0 -3
index c91708c..5e98a39 100644
@@ -165,9 +165,6 @@ void assert_no_error(Error *err);
 #define QERR_MIGRATION_NOT_SUPPORTED \
     ERROR_CLASS_GENERIC_ERROR, "State blocked by non-migratable device '%s'"
 
-#define QERR_MIGRATION_EXPECTED \
-    ERROR_CLASS_MIGRATION_EXPECTED, "An incoming migration is expected before this command can be executed"
-
 #define QERR_MISSING_PARAMETER \
     ERROR_CLASS_GENERIC_ERROR, "Parameter '%s' is missing"
 
qmp.c +11 -6
index 36c54c5..2c8d559 100644
@@ -85,7 +85,11 @@ void qmp_quit(Error **err)
 
 void qmp_stop(Error **errp)
 {
-    vm_stop(RUN_STATE_PAUSED);
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        autostart = 0;
+    } else {
+        vm_stop(RUN_STATE_PAUSED);
+    }
 }
 
 void qmp_system_reset(Error **errp)
@@ -144,10 +148,7 @@ void qmp_cont(Error **errp)
 {
     Error *local_err = NULL;
 
-    if (runstate_check(RUN_STATE_INMIGRATE)) {
-        error_set(errp, QERR_MIGRATION_EXPECTED);
-        return;
-    } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
+    if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
                runstate_check(RUN_STATE_SHUTDOWN)) {
         error_set(errp, QERR_RESET_REQUIRED);
         return;
@@ -162,7 +163,11 @@ void qmp_cont(Error **errp)
         return;
     }
 
-    vm_start();
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        autostart = 1;
+    } else {
+        vm_start();
+    }
 }
 
 void qmp_system_wakeup(Error **errp)
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] qmp: handle stop/cont in INMIGRATE state

Il 22/10/2012 17:06, Luiz Capitulino ha scritto:

Quoted 10 lines
Quoted 5 lines
Quoted 5 lines

Note that libvirt is currently working around the race by looping if the MigrationExpected answer is returned. After this patch, the command will return right away without ever raising an error.

Could you please add a note in cont's and stop's entries in qapi-schema.json explaining their behavior on INMIGRATE?

Otherwise series looks good.

Can I do that as a followup?

Paolo

#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/2] qmp: handle stop/cont in INMIGRATE state

Quoted 5 lines · On Mon, 22 Oct 2012 17:35:58 +0200 Paolo Bonzini <[email protected]> wrote:

Il 22/10/2012 17:06, Luiz Capitulino ha scritto:

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Note that libvirt is currently working around the race by looping if the MigrationExpected answer is returned. After this patch, the command will return right away without ever raising an error.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line
Quoted 1 line

Could you please add a note in cont's and stop's entries in qapi-schema.json explaining their behavior on INMIGRATE?

Otherwise series looks good.

Can I do that as a followup?

You can re-work 1/2 and submit only that one. I'll order the two patches correctly in my queue.

#5 lore raw

PATCH v3 1/2 qmp: handle stop/cont in INMIGRATE state

Right now, stop followed by an incoming migration will let the virtual machine start. cont before an incoming migration instead will fail.

This is bad because the actual behavior is not predictable; it is racy with respect to the start of the incoming migration. That's because incoming migration is blocking, and thus will delay the processing of stop/cont until the end of the migration.

In addition, there's nothing that really prevents the user from typing the block device's passwords before incoming migration is done, so returning the DeviceEncrypted error is also helpful in the QMP case.

Both things can be fixed by just toggling the autostart variable when stop/cont are called in INMIGRATE state.

Note that libvirt is currently working around the race by looping if the MigrationExpected answer is returned. After this patch, the command will return right away without ever raising an error.

---
 qapi-schema.json | 23 ++++++++++++++---------
 qerror.h         |  3 ---
 qmp.c            | 17 +++++++++++------

3 file modificati, 25 inserzioni(+), 18 rimozioni(-)

qapi-schema.json +14 -9
index c615ee2..6b14edc 100644
@@ -22,15 +22,11 @@
 # @KVMMissingCap: the requested operation can't be fulfilled because a
 #                 required KVM capability is missing
 #
-# @MigrationExpected: the requested operation can't be fulfilled because a
-#                     migration process is expected
-#
 # Since: 1.2
 ##
 { 'enum': 'ErrorClass',
   'data': [ 'GenericError', 'CommandNotFound', 'DeviceEncrypted',
-            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap',
-            'MigrationExpected' ] }
+            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap' ] }
 
 ##
 # @add_client
@@ -149,7 +145,11 @@
 #
 # @finish-migrate: guest is paused to finish the migration process
 #
-# @inmigrate: guest is paused waiting for an incoming migration
+# @inmigrate: guest is paused waiting for an incoming migration.  Note
+# that this state does not tell whether the machine will start at the
+# end of the migration.  This depends on the command-line -S option and
+# any invocation of 'stop' or 'cont' that has happened since QEMU was
+# started.
 #
 # @internal-error: An internal error that prevents further guest execution
 # has occurred
@@ -1210,7 +1210,9 @@
 # Since:  0.14.0
 #
 # Notes:  This function will succeed even if the guest is already in the stopped
-#         state
+#         state.  In "inmigrate" state, it will ensure that the guest
+#         remains paused once migration finishes, as if the -S option was
+#         passed on the command line.
 ##
 { 'command': 'stop' }
 
@@ -1299,11 +1301,14 @@
 # Since:  0.14.0
 #
 # Returns:  If successful, nothing
-#           If the QEMU is waiting for an incoming migration, MigrationExpected
 #           If QEMU was started with an encrypted block device and a key has
 #              not yet been set, DeviceEncrypted.
 #
-# Notes:  This command will succeed if the guest is currently running.
+# Notes:  This command will succeed if the guest is currently running.  It
+#         will also succeed if the guest is in the "inmigrate" state; in
+#         this case, the effect of the command is to make sure the guest
+#         starts once migration finishes, removing the effect of the -S
+#         command line option if it was passed.
 ##
 { 'command': 'cont' }
 
qerror.h +0 -3
index c91708c..5e98a39 100644
@@ -165,9 +165,6 @@ void assert_no_error(Error *err);
 #define QERR_MIGRATION_NOT_SUPPORTED \
     ERROR_CLASS_GENERIC_ERROR, "State blocked by non-migratable device '%s'"
 
-#define QERR_MIGRATION_EXPECTED \
-    ERROR_CLASS_MIGRATION_EXPECTED, "An incoming migration is expected before this command can be executed"
-
 #define QERR_MISSING_PARAMETER \
     ERROR_CLASS_GENERIC_ERROR, "Parameter '%s' is missing"
 
qmp.c +11 -6
index 36c54c5..2c8d559 100644
@@ -85,7 +85,11 @@ void qmp_quit(Error **err)
 
 void qmp_stop(Error **errp)
 {
-    vm_stop(RUN_STATE_PAUSED);
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        autostart = 0;
+    } else {
+        vm_stop(RUN_STATE_PAUSED);
+    }
 }
 
 void qmp_system_reset(Error **errp)
@@ -144,10 +148,7 @@ void qmp_cont(Error **errp)
 {
     Error *local_err = NULL;
 
-    if (runstate_check(RUN_STATE_INMIGRATE)) {
-        error_set(errp, QERR_MIGRATION_EXPECTED);
-        return;
-    } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
+    if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
                runstate_check(RUN_STATE_SHUTDOWN)) {
         error_set(errp, QERR_RESET_REQUIRED);
         return;
@@ -162,7 +163,11 @@ void qmp_cont(Error **errp)
         return;
     }
 
-    vm_start();
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        autostart = 1;
+    } else {
+        vm_start();
+    }
 }
 
 void qmp_system_wakeup(Error **errp)
Signature · 1 line
1.7.12.1
#6 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] qmp: handle stop/cont in INMIGRATE state

Quoted 14 lines · On Tue, 23 Oct 2012 14:54:21 +0200 Paolo Bonzini <[email protected]> wrote:

Right now, stop followed by an incoming migration will let the virtual machine start. cont before an incoming migration instead will fail.

This is bad because the actual behavior is not predictable; it is racy with respect to the start of the incoming migration. That's because incoming migration is blocking, and thus will delay the processing of stop/cont until the end of the migration.

In addition, there's nothing that really prevents the user from typing the block device's passwords before incoming migration is done, so returning the DeviceEncrypted error is also helpful in the QMP case.

Quoted 8 lines

Both things can be fixed by just toggling the autostart variable when stop/cont are called in INMIGRATE state.

Note that libvirt is currently working around the race by looping if the MigrationExpected answer is returned. After this patch, the command will return right away without ever raising an error.

Applied series to the qmp branch, thanks.

Quoted 124 lines
---
 qapi-schema.json | 23 ++++++++++++++---------
 qerror.h         |  3 ---
 qmp.c            | 17 +++++++++++------

3 file modificati, 25 inserzioni(+), 18 rimozioni(-)

qapi-schema.json +14 -9
index c615ee2..6b14edc 100644
@@ -22,15 +22,11 @@
 # @KVMMissingCap: the requested operation can't be fulfilled because a
 #                 required KVM capability is missing
 #
-# @MigrationExpected: the requested operation can't be fulfilled because a
-#                     migration process is expected
-#
 # Since: 1.2
 ##
 { 'enum': 'ErrorClass',
   'data': [ 'GenericError', 'CommandNotFound', 'DeviceEncrypted',
-            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap',
-            'MigrationExpected' ] }
+            'DeviceNotActive', 'DeviceNotFound', 'KVMMissingCap' ] }
 
 ##
 # @add_client
@@ -149,7 +145,11 @@
 #
 # @finish-migrate: guest is paused to finish the migration process
 #
-# @inmigrate: guest is paused waiting for an incoming migration
+# @inmigrate: guest is paused waiting for an incoming migration.  Note
+# that this state does not tell whether the machine will start at the
+# end of the migration.  This depends on the command-line -S option and
+# any invocation of 'stop' or 'cont' that has happened since QEMU was
+# started.
 #
 # @internal-error: An internal error that prevents further guest execution
 # has occurred
@@ -1210,7 +1210,9 @@
 # Since:  0.14.0
 #
 # Notes:  This function will succeed even if the guest is already in the stopped
-#         state
+#         state.  In "inmigrate" state, it will ensure that the guest
+#         remains paused once migration finishes, as if the -S option was
+#         passed on the command line.
 ##
 { 'command': 'stop' }
 
@@ -1299,11 +1301,14 @@
 # Since:  0.14.0
 #
 # Returns:  If successful, nothing
-#           If the QEMU is waiting for an incoming migration, MigrationExpected
 #           If QEMU was started with an encrypted block device and a key has
 #              not yet been set, DeviceEncrypted.
 #
-# Notes:  This command will succeed if the guest is currently running.
+# Notes:  This command will succeed if the guest is currently running.  It
+#         will also succeed if the guest is in the "inmigrate" state; in
+#         this case, the effect of the command is to make sure the guest
+#         starts once migration finishes, removing the effect of the -S
+#         command line option if it was passed.
 ##
 { 'command': 'cont' }
 
qerror.h +0 -3
index c91708c..5e98a39 100644
@@ -165,9 +165,6 @@ void assert_no_error(Error *err);
 #define QERR_MIGRATION_NOT_SUPPORTED \
     ERROR_CLASS_GENERIC_ERROR, "State blocked by non-migratable device '%s'"
 
-#define QERR_MIGRATION_EXPECTED \
-    ERROR_CLASS_MIGRATION_EXPECTED, "An incoming migration is expected before this command can be executed"
-
 #define QERR_MISSING_PARAMETER \
     ERROR_CLASS_GENERIC_ERROR, "Parameter '%s' is missing"
 
qmp.c +11 -6
index 36c54c5..2c8d559 100644
@@ -85,7 +85,11 @@ void qmp_quit(Error **err)
 
 void qmp_stop(Error **errp)
 {
-    vm_stop(RUN_STATE_PAUSED);
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        autostart = 0;
+    } else {
+        vm_stop(RUN_STATE_PAUSED);
+    }
 }
 
 void qmp_system_reset(Error **errp)
@@ -144,10 +148,7 @@ void qmp_cont(Error **errp)
 {
     Error *local_err = NULL;
 
-    if (runstate_check(RUN_STATE_INMIGRATE)) {
-        error_set(errp, QERR_MIGRATION_EXPECTED);
-        return;
-    } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
+    if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
                runstate_check(RUN_STATE_SHUTDOWN)) {
         error_set(errp, QERR_RESET_REQUIRED);
         return;
@@ -162,7 +163,11 @@ void qmp_cont(Error **errp)
         return;
     }
 
-    vm_start();
+    if (runstate_check(RUN_STATE_INMIGRATE)) {
+        autostart = 1;
+    } else {
+        vm_start();
+    }
 }
 
 void qmp_system_wakeup(Error **errp)
#7 lore raw

PATCH v2 2/2 migration: go to paused state after finishing incoming migration with -S

In reply to Paolo Bonzini

At the end of migration the machine has started already, and cannot be destroyed without losing the guest's data. Hence, prelaunch is the wrong state. Go to the paused state instead. QEMU would reach that state anyway (after running the guest for the blink of an eye) if the "stop" command had been received after the start of migration.

---
 migration.c | 2 +-
 vl.c        | 2 +-

2 file modificati, 2 inserzioni(+), 2 rimozioni(-)

migration.c +1 -1
index 32d43e7..72abd3c 100644
@@ -108,7 +108,7 @@ static void process_incoming_migration_co(void *opaque)
     if (autostart) {
         vm_start();
     } else {
-        runstate_set(RUN_STATE_PRELAUNCH);
+        runstate_set(RUN_STATE_PAUSED);
     }
 }
 
vl.c +1 -1
index 5b357a3..8b1e0b2 100644
@@ -341,7 +341,7 @@ static const RunStateTransition runstate_transitions_def[] = {
     { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
 
     { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
-    { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
+    { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
 
     { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
     { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
Signature · 1 line
1.7.12.1