qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Steve Sistare <steven.sistare@oracle.com>
To: qemu-devel@nongnu.org
Cc: Alex Williamson <alex.williamson@redhat.com>,
	Cedric Le Goater <clg@redhat.com>, Yi Liu <yi.l.liu@intel.com>,
	Eric Auger <eric.auger@redhat.com>,
	Zhenzhong Duan <zhenzhong.duan@intel.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,
	Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>,
	Steve Sistare <steven.sistare@oracle.com>
Subject: [PATCH V1 25/26] vfio/iommufd: reconstruct device
Date: Wed, 29 Jan 2025 06:43:21 -0800	[thread overview]
Message-ID: <1738161802-172631-26-git-send-email-steven.sistare@oracle.com> (raw)
In-Reply-To: <1738161802-172631-1-git-send-email-steven.sistare@oracle.com>

Reconstruct userland device state after CPR.  During vfio_realize, skip
all ioctls that configure the device, as it was already configured in old
QEMU.

Save the ioas_id in vmstate, and skip its allocation in vfio_realize.
Because we skip ioctl's, it is not needed at realize time.  However, we do
need the range info, so defer the call to iommufd_cdev_get_info_iova_range
to a post_load handler, at which time the ioas_id is known.

Save the devid in vmstate.  It is used in one place during realize, to
fetch hw_caps device info, at vfio_device_hiod_realize ->
hiod_iommufd_vfio_realize -> iommufd_backend_get_device_info.  The hw_caps
is not needed until post load time (see the next paragraph), so defer
the call to vfio_device_hiod_realize to post load, at which time the
devid is known.

Save the hwpt_id in vmstate.  In realize, skip its allocation from
iommufd_cdev_attach -> iommufd_cdev_attach_container ->
iommufd_cdev_autodomains_get.  Rebuild userland structures to hold
hwpt_id by calling iommufd_cdev_rebuild_hwpt at post load time.
This depends on hw_caps as described above.

Lastly, change the owning process of the iommufd device in post load.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
---
 hw/vfio/cpr-iommufd.c         | 49 +++++++++++++++++++++++++++++++++++++++++++
 hw/vfio/iommufd.c             | 46 +++++++++++++++++++++++++++++++++++++---
 hw/vfio/trace-events          |  1 +
 include/hw/vfio/vfio-common.h |  3 +++
 4 files changed, 96 insertions(+), 3 deletions(-)

diff --git a/hw/vfio/cpr-iommufd.c b/hw/vfio/cpr-iommufd.c
index 053ff8c..711e5cf 100644
--- a/hw/vfio/cpr-iommufd.c
+++ b/hw/vfio/cpr-iommufd.c
@@ -14,6 +14,9 @@
 #include "migration/vmstate.h"
 #include "system/iommufd.h"
 
+#define IOMMUFD_CONTAINER(base) \
+    container_of(base, VFIOIOMMUFDContainer, bcontainer)
+
 static bool vfio_cpr_supported(VFIOIOMMUFDContainer *container, Error **errp)
 {
     if (!iommufd_change_process_capable(container->be)) {
@@ -29,6 +32,13 @@ static int vfio_container_post_load(void *opaque, int version_id)
     VFIOIOMMUFDContainer *container = opaque;
     VFIOContainerBase *bcontainer = &container->bcontainer;
     VFIODevice *vbasedev;
+    Error *err = NULL;
+    uint32_t ioas_id = container->ioas_id;
+
+    if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) {
+        error_report_err(err);
+        return -1;
+    }
 
     QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
         vbasedev->reused = false;
@@ -38,21 +48,41 @@ static int vfio_container_post_load(void *opaque, int version_id)
     return 0;
 }
 
+static int vfio_container_pre_save(void *opaque)
+{
+    VFIOIOMMUFDContainer *container = opaque;
+
+    /*
+     * The process has not changed yet, but proactively call the ioctl,
+     * and it will fail if any DMA mappings are not supported.
+     */
+    return iommufd_change_process(container->be);
+}
+
 static const VMStateDescription vfio_container_vmstate = {
     .name = "vfio-iommufd-container",
     .version_id = 0,
     .minimum_version_id = 0,
+    .pre_save = vfio_container_pre_save,
     .post_load = vfio_container_post_load,
     .needed = cpr_needed_for_reuse,
     .fields = (VMStateField[]) {
+        VMSTATE_UINT32(ioas_id, VFIOIOMMUFDContainer),
         VMSTATE_END_OF_LIST()
     }
 };
 
+static int iommufd_cpr_post_load(void *opaque, int version_id)
+{
+     IOMMUFDBackend *be = opaque;
+     return iommufd_change_process(be);
+}
+
 static const VMStateDescription iommufd_cpr_vmstate = {
     .name = "iommufd",
     .version_id = 0,
     .minimum_version_id = 0,
+    .post_load = iommufd_cpr_post_load,
     .needed = cpr_needed_for_reuse,
     .fields = (VMStateField[]) {
         VMSTATE_END_OF_LIST()
@@ -90,12 +120,31 @@ void vfio_iommufd_cpr_unregister_container(VFIOIOMMUFDContainer *container)
     vfio_cpr_unregister_container(bcontainer);
 }
 
+static int vfio_device_post_load(void *opaque, int version_id)
+{
+    VFIODevice *vbasedev = opaque;
+    VFIOIOMMUFDContainer *container = IOMMUFD_CONTAINER(vbasedev->bcontainer);
+    Error *err = NULL;
+
+    if (!vfio_device_hiod_realize(vbasedev, &err)) {
+        error_report_err(err);
+        return false;
+    }
+    if (!vbasedev->mdev) {
+        iommufd_cdev_rebuild_hwpt(vbasedev, container);
+    }
+    return true;
+}
+
 static const VMStateDescription vfio_device_vmstate = {
     .name = "vfio-iommufd-device",
     .version_id = 0,
     .minimum_version_id = 0,
+    .post_load = vfio_device_post_load,
     .needed = cpr_needed_for_reuse,
     .fields = (VMStateField[]) {
+        VMSTATE_INT32(devid, VFIODevice),
+        VMSTATE_UINT32(hwpt_id, VFIODevice),
         VMSTATE_END_OF_LIST()
     }
 };
diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c
index abd17b6..a007b6c 100644
--- a/hw/vfio/iommufd.c
+++ b/hw/vfio/iommufd.c
@@ -99,6 +99,11 @@ static bool iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp)
         goto err_kvm_device_add;
     }
 
+    if (vbasedev->reused) {
+        /* Already bound, and devid was set in iommufd_cdev_attach */
+        goto skip_bind;
+    }
+
     /* Bind device to iommufd */
     bind.iommufd = iommufd->fd;
     if (ioctl(vbasedev->fd, VFIO_DEVICE_BIND_IOMMUFD, &bind)) {
@@ -110,6 +115,8 @@ static bool iommufd_cdev_connect_and_bind(VFIODevice *vbasedev, Error **errp)
     vbasedev->devid = bind.out_devid;
     trace_iommufd_cdev_connect_and_bind(bind.iommufd, vbasedev->name,
                                         vbasedev->fd, vbasedev->devid);
+
+skip_bind:
     return true;
 err_bind:
     iommufd_cdev_kvm_device_del(vbasedev);
@@ -292,6 +299,7 @@ static bool iommufd_cdev_detach_ioas_hwpt(VFIODevice *vbasedev, Error **errp)
 static void iommufd_cdev_set_hwpt(VFIODevice *vbasedev, VFIOIOASHwpt *hwpt)
 {
     vbasedev->hwpt = hwpt;
+    vbasedev->hwpt_id = hwpt->hwpt_id;
     vbasedev->iommu_dirty_tracking = iommufd_hwpt_dirty_tracking(hwpt);
     QLIST_INSERT_HEAD(&hwpt->device_list, vbasedev, hwpt_next);
 }
@@ -324,6 +332,24 @@ static VFIOIOASHwpt *iommufd_cdev_make_hwpt(VFIODevice *vbasedev,
     return hwpt;
 }
 
+void iommufd_cdev_rebuild_hwpt(VFIODevice *vbasedev,
+                               VFIOIOMMUFDContainer *container)
+{
+    VFIOIOASHwpt *hwpt;
+    int hwpt_id = vbasedev->hwpt_id;
+
+    trace_iommufd_cdev_rebuild_hwpt(container->be->fd, hwpt_id);
+
+    QLIST_FOREACH(hwpt, &container->hwpt_list, next) {
+        if (hwpt->hwpt_id == hwpt_id) {
+            iommufd_cdev_set_hwpt(vbasedev, hwpt);
+            return;
+        }
+    }
+    hwpt = iommufd_cdev_make_hwpt(vbasedev, container, hwpt_id);
+    iommufd_cdev_set_hwpt(vbasedev, hwpt);
+}
+
 static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
                                          VFIOIOMMUFDContainer *container,
                                          Error **errp)
@@ -527,7 +553,8 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
      * FD to be connected and having a devid to be able to successfully call
      * iommufd_backend_get_device_info().
      */
-    if (!vfio_device_hiod_realize(vbasedev, errp)) {
+    if (!vbasedev->reused &&
+        !vfio_device_hiod_realize(vbasedev, errp)) {
         goto err_alloc_ioas;
     }
 
@@ -538,7 +565,8 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
             vbasedev->iommufd != container->be) {
             continue;
         }
-        if (!iommufd_cdev_attach_container(vbasedev, container, &err)) {
+        if (!vbasedev->reused &&
+            !iommufd_cdev_attach_container(vbasedev, container, &err)) {
             const char *msg = error_get_pretty(err);
 
             trace_iommufd_cdev_fail_attach_existing_container(msg);
@@ -555,6 +583,11 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
         }
     }
 
+    if (vbasedev->reused) {
+        ioas_id = -1;           /* ioas_id will be received from vmstate */
+        goto skip_ioas_alloc;
+    }
+
     /* Need to allocate a new dedicated container */
     if (!iommufd_backend_alloc_ioas(vbasedev->iommufd, &ioas_id, errp)) {
         goto err_alloc_ioas;
@@ -562,6 +595,7 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
 
     trace_iommufd_cdev_alloc_ioas(vbasedev->iommufd->fd, ioas_id);
 
+skip_ioas_alloc:
     container = VFIO_IOMMU_IOMMUFD(object_new(TYPE_VFIO_IOMMU_IOMMUFD));
     container->be = vbasedev->iommufd;
     container->ioas_id = ioas_id;
@@ -570,7 +604,8 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
     bcontainer = &container->bcontainer;
     vfio_address_space_insert(space, bcontainer);
 
-    if (!iommufd_cdev_attach_container(vbasedev, container, errp)) {
+    if (!vbasedev->reused &&
+        !iommufd_cdev_attach_container(vbasedev, container, errp)) {
         goto err_attach_container;
     }
 
@@ -579,6 +614,10 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
         goto err_discard_disable;
     }
 
+    if (vbasedev->reused) {
+        goto skip_info;
+    }
+
     if (!iommufd_cdev_get_info_iova_range(container, ioas_id, &err)) {
         error_append_hint(&err,
                    "Fallback to default 64bit IOVA range and 4K page size\n");
@@ -587,6 +626,7 @@ static bool iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
         bcontainer->pgsizes = qemu_real_host_page_size();
     }
 
+skip_info:
     bcontainer->listener = vfio_memory_listener;
     memory_listener_register(&bcontainer->listener, bcontainer->space->as);
 
diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events
index cab1cf1..25ff04c 100644
--- a/hw/vfio/trace-events
+++ b/hw/vfio/trace-events
@@ -176,6 +176,7 @@ iommufd_cdev_connect_and_bind(int iommufd, const char *name, int devfd, int devi
 iommufd_cdev_getfd(const char *dev, int devfd) " %s (fd=%d)"
 iommufd_cdev_attach_ioas_hwpt(int iommufd, const char *name, int devfd, int id) " [iommufd=%d] Successfully attached device %s (%d) to id=%d"
 iommufd_cdev_detach_ioas_hwpt(int iommufd, const char *name) " [iommufd=%d] Successfully detached %s"
+iommufd_cdev_rebuild_hwpt(int iommufd, int hwpt_id) " [iommufd=%d] hwpt %d"
 iommufd_cdev_fail_attach_existing_container(const char *msg) " %s"
 iommufd_cdev_alloc_ioas(int iommufd, int ioas_id) " [iommufd=%d] new IOMMUFD container with ioasid=%d"
 iommufd_cdev_device_info(char *name, int devfd, int num_irqs, int num_regions, int flags) " %s (%d) num_irqs=%d num_regions=%d flags=%d"
diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h
index add44d4..a359ea9 100644
--- a/include/hw/vfio/vfio-common.h
+++ b/include/hw/vfio/vfio-common.h
@@ -157,6 +157,7 @@ typedef struct VFIODevice {
     HostIOMMUDevice *hiod;
     int devid;
     IOMMUFDBackend *iommufd;
+    uint32_t hwpt_id;
     VFIOIOASHwpt *hwpt;
     QLIST_ENTRY(VFIODevice) hwpt_next;
 } VFIODevice;
@@ -270,6 +271,8 @@ bool vfio_cpr_register_container(VFIOContainerBase *bcontainer, Error **errp);
 void vfio_cpr_unregister_container(VFIOContainerBase *bcontainer);
 bool vfio_legacy_cpr_register_container(VFIOContainer *container, Error **errp);
 void vfio_legacy_cpr_unregister_container(VFIOContainer *container);
+void iommufd_cdev_rebuild_hwpt(VFIODevice *vbasedev,
+                               VFIOIOMMUFDContainer *container);
 bool iommufd_cdev_get_info_iova_range(VFIOIOMMUFDContainer *container,
                                       uint32_t ioas_id, Error **errp);
 bool vfio_iommufd_cpr_register_container(VFIOIOMMUFDContainer *container,
-- 
1.8.3.1



  parent reply	other threads:[~2025-01-29 14:46 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-29 14:42 [PATCH V1 00/26] Live update: vfio and iommufd Steve Sistare
2025-01-29 14:42 ` [PATCH V1 01/26] migration: cpr helpers Steve Sistare
2025-01-29 14:42 ` [PATCH V1 02/26] migration: lower handler priority Steve Sistare
2025-02-03 16:21   ` Fabiano Rosas
2025-02-03 16:58   ` Peter Xu
2025-02-06 13:39     ` Steven Sistare
2025-01-29 14:42 ` [PATCH V1 03/26] vfio: vfio_find_ram_discard_listener Steve Sistare
2025-02-03 16:57   ` Cédric Le Goater
2025-01-29 14:43 ` [PATCH V1 04/26] vfio/container: register container for cpr Steve Sistare
2025-02-03 17:01   ` Cédric Le Goater
2025-02-03 22:26     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 05/26] vfio/container: preserve descriptors Steve Sistare
2025-02-03 17:48   ` Cédric Le Goater
2025-02-03 22:26     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 06/26] vfio/container: preserve DMA mappings Steve Sistare
2025-02-03 18:25   ` Cédric Le Goater
2025-02-03 22:27     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 07/26] vfio/container: recover from unmap-all-vaddr failure Steve Sistare
2025-02-04 14:10   ` Cédric Le Goater
2025-02-04 16:13     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 08/26] pci: skip reset during cpr Steve Sistare
2025-02-04 14:14   ` Cédric Le Goater
2025-02-04 16:13     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 09/26] pci: export msix_is_pending Steve Sistare
2025-01-29 14:43 ` [PATCH V1 10/26] vfio-pci: refactor for cpr Steve Sistare
2025-02-04 14:39   ` Cédric Le Goater
2025-02-04 16:14     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 11/26] vfio-pci: skip reset during cpr Steve Sistare
2025-02-04 14:56   ` Cédric Le Goater
2025-02-04 16:15     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 12/26] vfio-pci: preserve MSI Steve Sistare
2025-02-05 16:48   ` Cédric Le Goater
2025-02-06 14:41     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 13/26] vfio-pci: preserve INTx Steve Sistare
2025-02-05 17:13   ` Cédric Le Goater
2025-02-06 14:43     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 14/26] migration: close kvm after cpr Steve Sistare
2025-01-29 14:43 ` [PATCH V1 15/26] migration: cpr_get_fd_param helper Steve Sistare
2025-01-29 14:43 ` [PATCH V1 16/26] vfio: return mr from vfio_get_xlat_addr Steve Sistare
2025-02-04 15:47   ` Cédric Le Goater
2025-02-04 17:42     ` Steven Sistare
2025-02-16 23:19       ` John Levon
2025-01-29 14:43 ` [PATCH V1 17/26] vfio: pass ramblock to vfio_container_dma_map Steve Sistare
2025-01-29 14:43 ` [PATCH V1 18/26] vfio/iommufd: define iommufd_cdev_make_hwpt Steve Sistare
2025-02-04 16:22   ` Cédric Le Goater
2025-02-04 17:42     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 19/26] vfio/iommufd: use IOMMU_IOAS_MAP_FILE Steve Sistare
2025-02-05 17:23   ` Cédric Le Goater
2025-02-05 22:01     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 20/26] vfio/iommufd: export iommufd_cdev_get_info_iova_range Steve Sistare
2025-02-05 17:33   ` Cédric Le Goater
2025-02-05 22:01     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 21/26] iommufd: change process ioctl Steve Sistare
2025-02-05 17:34   ` Cédric Le Goater
2025-02-05 22:02     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 22/26] vfio/iommufd: invariant device name Steve Sistare
2025-02-05 17:42   ` Cédric Le Goater
2025-02-05 22:02     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 23/26] vfio/iommufd: register container for cpr Steve Sistare
2025-02-05 17:45   ` Cédric Le Goater
2025-02-05 22:03     ` Steven Sistare
2025-01-29 14:43 ` [PATCH V1 24/26] vfio/iommufd: preserve descriptors Steve Sistare
2025-01-29 14:43 ` Steve Sistare [this message]
2025-01-29 14:43 ` [PATCH V1 26/26] iommufd: preserve DMA mappings Steve Sistare

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=1738161802-172631-26-git-send-email-steven.sistare@oracle.com \
    --to=steven.sistare@oracle.com \
    --cc=alex.williamson@redhat.com \
    --cc=clg@redhat.com \
    --cc=eric.auger@redhat.com \
    --cc=farosas@suse.de \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=yi.l.liu@intel.com \
    --cc=zhenzhong.duan@intel.com \
    /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).