qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Cédric Le Goater" <clg@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Alex Williamson" <alex.williamson@redhat.com>,
	"Eric Auger" <eric.auger@redhat.com>,
	"Avihai Horon" <avihaih@nvidia.com>,
	"Cédric Le Goater" <clg@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: [PULL 15/17] vfio/migration: Add helper function to set state or reset device
Date: Mon,  8 Jan 2024 08:32:30 +0100	[thread overview]
Message-ID: <20240108073232.118228-16-clg@redhat.com> (raw)
In-Reply-To: <20240108073232.118228-1-clg@redhat.com>

From: Avihai Horon <avihaih@nvidia.com>

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>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 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 28d422b39f9f70e94a2f396b0fb064c5de17dc28..70e6b1a709f9b67e4c9eb41033d76347275cac42 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.43.0



  parent reply	other threads:[~2024-01-08  7:34 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-08  7:32 [PULL 00/17] vfio queue Cédric Le Goater
2024-01-08  7:32 ` [PULL 01/17] vfio/spapr: Extend VFIOIOMMUOps with a release handler Cédric Le Goater
2024-01-08  7:32 ` [PULL 02/17] vfio/container: Introduce vfio_legacy_setup() for further cleanups Cédric Le Goater
2024-01-08  7:32 ` [PULL 03/17] vfio/container: Initialize VFIOIOMMUOps under vfio_init_container() Cédric Le Goater
2024-01-08  7:32 ` [PULL 04/17] vfio/container: Introduce a VFIOIOMMU QOM interface Cédric Le Goater
2024-01-08  7:32 ` [PULL 05/17] vfio/container: Introduce a VFIOIOMMU legacy " Cédric Le Goater
2024-01-08  7:32 ` [PULL 06/17] vfio/container: Intoduce a new VFIOIOMMUClass::setup handler Cédric Le Goater
2024-01-08  7:32 ` [PULL 07/17] vfio/spapr: Introduce a sPAPR VFIOIOMMU QOM interface Cédric Le Goater
2024-01-08  7:32 ` [PULL 08/17] vfio/iommufd: Introduce a VFIOIOMMU iommufd " Cédric Le Goater
2024-01-08  7:32 ` [PULL 09/17] vfio/spapr: Only compile sPAPR IOMMU support when needed Cédric Le Goater
2024-01-08  7:32 ` [PULL 10/17] vfio/iommufd: Remove CONFIG_IOMMUFD usage Cédric Le Goater
2024-01-08  7:32 ` [PULL 11/17] vfio/container: Replace basename with g_path_get_basename Cédric Le Goater
2024-01-08  7:32 ` [PULL 12/17] hw/vfio: fix iteration over global VFIODevice list Cédric Le Goater
2024-01-08  7:32 ` [PULL 13/17] vfio/iommufd: Remove the use of stat() to check file existence Cédric Le Goater
2024-01-08  7:32 ` [PULL 14/17] vfio/container: Rename vfio_init_container to vfio_set_iommu Cédric Le Goater
2024-01-08  7:32 ` Cédric Le Goater [this message]
2024-01-08  7:32 ` [PULL 16/17] backends/iommufd: Remove check on number of backend users Cédric Le Goater
2024-01-08  7:32 ` [PULL 17/17] backends/iommufd: Remove mutex Cédric Le Goater
2024-01-08 13:16 ` [PULL 00/17] vfio queue Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240108073232.118228-16-clg@redhat.com \
    --to=clg@redhat.com \
    --cc=alex.williamson@redhat.com \
    --cc=avihaih@nvidia.com \
    --cc=eric.auger@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).