qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vfio/migration: Add helper function to set state or reset device
@ 2023-12-31 10:48 Avihai Horon
  2024-01-02  9:20 ` Cédric Le Goater
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Avihai Horon @ 2023-12-31 10:48 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alex Williamson, Cédric Le Goater, Avihai Horon

There are several places where failure in setting the device state leads
to a device reset, which is done by setting ERROR as the recover state.

Add a helper function that sets the device state and resets the device
in case of failure. This will make the code cleaner and remove duplicate
comments.

Signed-off-by: Avihai Horon <avihaih@nvidia.com>
---
 hw/vfio/migration.c | 41 +++++++++++++++++------------------------
 1 file changed, 17 insertions(+), 24 deletions(-)

diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c
index 28d422b39f..70e6b1a709 100644
--- a/hw/vfio/migration.c
+++ b/hw/vfio/migration.c
@@ -163,6 +163,19 @@ reset_device:
     return ret;
 }
 
+/*
+ * Some device state transitions require resetting the device if they fail.
+ * This function sets the device in new_state and resets the device if that
+ * fails. Reset is done by using ERROR as the recover state.
+ */
+static int
+vfio_migration_set_state_or_reset(VFIODevice *vbasedev,
+                                  enum vfio_device_mig_state new_state)
+{
+    return vfio_migration_set_state(vbasedev, new_state,
+                                    VFIO_DEVICE_STATE_ERROR);
+}
+
 static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev,
                             uint64_t data_size)
 {
@@ -422,12 +435,7 @@ static void vfio_save_cleanup(void *opaque)
      * after migration has completed, so it won't increase downtime.
      */
     if (migration->device_state == VFIO_DEVICE_STATE_STOP_COPY) {
-        /*
-         * If setting the device in STOP state fails, the device should be
-         * reset. To do so, use ERROR state as a recover state.
-         */
-        vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP,
-                                 VFIO_DEVICE_STATE_ERROR);
+        vfio_migration_set_state_or_reset(vbasedev, VFIO_DEVICE_STATE_STOP);
     }
 
     g_free(migration->data_buffer);
@@ -699,12 +707,7 @@ static void vfio_vmstate_change_prepare(void *opaque, bool running,
                     VFIO_DEVICE_STATE_PRE_COPY_P2P :
                     VFIO_DEVICE_STATE_RUNNING_P2P;
 
-    /*
-     * If setting the device in new_state fails, the device should be reset.
-     * To do so, use ERROR state as a recover state.
-     */
-    ret = vfio_migration_set_state(vbasedev, new_state,
-                                   VFIO_DEVICE_STATE_ERROR);
+    ret = vfio_migration_set_state_or_reset(vbasedev, new_state);
     if (ret) {
         /*
          * Migration should be aborted in this case, but vm_state_notify()
@@ -736,12 +739,7 @@ static void vfio_vmstate_change(void *opaque, bool running, RunState state)
                 VFIO_DEVICE_STATE_STOP;
     }
 
-    /*
-     * If setting the device in new_state fails, the device should be reset.
-     * To do so, use ERROR state as a recover state.
-     */
-    ret = vfio_migration_set_state(vbasedev, new_state,
-                                   VFIO_DEVICE_STATE_ERROR);
+    ret = vfio_migration_set_state_or_reset(vbasedev, new_state);
     if (ret) {
         /*
          * Migration should be aborted in this case, but vm_state_notify()
@@ -770,12 +768,7 @@ static void vfio_migration_state_notifier(Notifier *notifier, void *data)
     case MIGRATION_STATUS_CANCELLING:
     case MIGRATION_STATUS_CANCELLED:
     case MIGRATION_STATUS_FAILED:
-        /*
-         * If setting the device in RUNNING state fails, the device should
-         * be reset. To do so, use ERROR state as a recover state.
-         */
-        vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RUNNING,
-                                 VFIO_DEVICE_STATE_ERROR);
+        vfio_migration_set_state_or_reset(vbasedev, VFIO_DEVICE_STATE_RUNNING);
     }
 }
 
-- 
2.26.3



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] vfio/migration: Add helper function to set state or reset device
  2023-12-31 10:48 [PATCH] vfio/migration: Add helper function to set state or reset device Avihai Horon
@ 2024-01-02  9:20 ` Cédric Le Goater
  2024-01-02  9:55 ` Philippe Mathieu-Daudé
  2024-01-02 10:22 ` Cédric Le Goater
  2 siblings, 0 replies; 4+ messages in thread
From: Cédric Le Goater @ 2024-01-02  9:20 UTC (permalink / raw)
  To: Avihai Horon, qemu-devel; +Cc: Alex Williamson

On 12/31/23 11:48, Avihai Horon wrote:
> There are several places where failure in setting the device state leads
> to a device reset, which is done by setting ERROR as the recover state.
> 
> Add a helper function that sets the device state and resets the device
> in case of failure. This will make the code cleaner and remove duplicate
> comments.
> 
> Signed-off-by: Avihai Horon <avihaih@nvidia.com>


Reviewed-by: Cédric Le Goater <clg@redhat.com>

Thanks,

C.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] vfio/migration: Add helper function to set state or reset device
  2023-12-31 10:48 [PATCH] vfio/migration: Add helper function to set state or reset device Avihai Horon
  2024-01-02  9:20 ` Cédric Le Goater
@ 2024-01-02  9:55 ` Philippe Mathieu-Daudé
  2024-01-02 10:22 ` Cédric Le Goater
  2 siblings, 0 replies; 4+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-01-02  9:55 UTC (permalink / raw)
  To: Avihai Horon, qemu-devel; +Cc: Alex Williamson, Cédric Le Goater

On 31/12/23 11:48, Avihai Horon wrote:
> There are several places where failure in setting the device state leads
> to a device reset, which is done by setting ERROR as the recover state.
> 
> Add a helper function that sets the device state and resets the device
> in case of failure. This will make the code cleaner and remove duplicate
> comments.
> 
> Signed-off-by: Avihai Horon <avihaih@nvidia.com>
> ---
>   hw/vfio/migration.c | 41 +++++++++++++++++------------------------
>   1 file changed, 17 insertions(+), 24 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] vfio/migration: Add helper function to set state or reset device
  2023-12-31 10:48 [PATCH] vfio/migration: Add helper function to set state or reset device Avihai Horon
  2024-01-02  9:20 ` Cédric Le Goater
  2024-01-02  9:55 ` Philippe Mathieu-Daudé
@ 2024-01-02 10:22 ` Cédric Le Goater
  2 siblings, 0 replies; 4+ messages in thread
From: Cédric Le Goater @ 2024-01-02 10:22 UTC (permalink / raw)
  To: Avihai Horon, qemu-devel; +Cc: Alex Williamson

On 12/31/23 11:48, Avihai Horon wrote:
> There are several places where failure in setting the device state leads
> to a device reset, which is done by setting ERROR as the recover state.
> 
> Add a helper function that sets the device state and resets the device
> in case of failure. This will make the code cleaner and remove duplicate
> comments.
> 
> Signed-off-by: Avihai Horon <avihaih@nvidia.com>


Applied to vfio-next.

Thanks,

C.





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-02 10:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-31 10:48 [PATCH] vfio/migration: Add helper function to set state or reset device Avihai Horon
2024-01-02  9:20 ` Cédric Le Goater
2024-01-02  9:55 ` Philippe Mathieu-Daudé
2024-01-02 10:22 ` Cédric Le Goater

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).