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>,
	"Steve Sistare" <steven.sistare@oracle.com>,
	"Zhenzhong Duan" <zhenzhong.duan@intel.com>,
	"Cédric Le Goater" <clg@redhat.com>
Subject: [PULL 21/27] vfio/iommufd: preserve descriptors
Date: Fri,  4 Jul 2025 10:45:22 +0200	[thread overview]
Message-ID: <20250704084528.1412959-22-clg@redhat.com> (raw)
In-Reply-To: <20250704084528.1412959-1-clg@redhat.com>

From: Steve Sistare <steven.sistare@oracle.com>

Save the iommu and vfio device fd in CPR state when it is created.
After CPR, the fd number is found in CPR state and reused.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
Reviewed-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Link: https://lore.kernel.org/qemu-devel/1751493538-202042-16-git-send-email-steven.sistare@oracle.com
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
 backends/iommufd.c    | 35 +++++++++++++++++++++++++++++------
 hw/vfio/cpr-iommufd.c | 10 ++++++++++
 hw/vfio/device.c      |  9 +--------
 3 files changed, 40 insertions(+), 14 deletions(-)

diff --git a/backends/iommufd.c b/backends/iommufd.c
index c554ce5385dd603ba35f97167ab8031750ecdc2f..e0917923bffbb60dc5c58235880335c4d5eb7df7 100644
--- a/backends/iommufd.c
+++ b/backends/iommufd.c
@@ -16,12 +16,18 @@
 #include "qemu/module.h"
 #include "qom/object_interfaces.h"
 #include "qemu/error-report.h"
+#include "migration/cpr.h"
 #include "monitor/monitor.h"
 #include "trace.h"
 #include "hw/vfio/vfio-device.h"
 #include <sys/ioctl.h>
 #include <linux/iommufd.h>
 
+static const char *iommufd_fd_name(IOMMUFDBackend *be)
+{
+    return object_get_canonical_path_component(OBJECT(be));
+}
+
 static void iommufd_backend_init(Object *obj)
 {
     IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
@@ -64,11 +70,27 @@ static bool iommufd_backend_can_be_deleted(UserCreatable *uc)
     return !be->users;
 }
 
+static void iommufd_backend_complete(UserCreatable *uc, Error **errp)
+{
+    IOMMUFDBackend *be = IOMMUFD_BACKEND(uc);
+    const char *name = iommufd_fd_name(be);
+
+    if (!be->owned) {
+        /* fd came from the command line. Fetch updated value from cpr state. */
+        if (cpr_is_incoming()) {
+            be->fd = cpr_find_fd(name, 0);
+        } else {
+            cpr_save_fd(name, 0, be->fd);
+        }
+    }
+}
+
 static void iommufd_backend_class_init(ObjectClass *oc, const void *data)
 {
     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
 
     ucc->can_be_deleted = iommufd_backend_can_be_deleted;
+    ucc->complete = iommufd_backend_complete;
 
     object_class_property_add_str(oc, "fd", NULL, iommufd_backend_set_fd);
 }
@@ -102,7 +124,7 @@ bool iommufd_backend_connect(IOMMUFDBackend *be, Error **errp)
     int fd;
 
     if (be->owned && !be->users) {
-        fd = qemu_open("/dev/iommu", O_RDWR, errp);
+        fd = cpr_open_fd("/dev/iommu", O_RDWR, iommufd_fd_name(be), 0, errp);
         if (fd < 0) {
             return false;
         }
@@ -127,14 +149,15 @@ void iommufd_backend_disconnect(IOMMUFDBackend *be)
         goto out;
     }
     be->users--;
-    if (!be->users && be->owned) {
-        close(be->fd);
-        be->fd = -1;
-    }
-out:
     if (!be->users) {
         vfio_iommufd_cpr_unregister_iommufd(be);
+        if (be->owned) {
+            cpr_delete_fd(iommufd_fd_name(be), 0);
+            close(be->fd);
+            be->fd = -1;
+        }
     }
+out:
     trace_iommufd_backend_disconnect(be->fd, be->users);
 }
 
diff --git a/hw/vfio/cpr-iommufd.c b/hw/vfio/cpr-iommufd.c
index 4166201e3f70441ff97aef23c3b6f8bf506c76ad..a72b68daa8e4da19b9cbf3ab50631211965cdf37 100644
--- a/hw/vfio/cpr-iommufd.c
+++ b/hw/vfio/cpr-iommufd.c
@@ -166,12 +166,18 @@ void vfio_iommufd_cpr_unregister_container(VFIOIOMMUFDContainer *container)
 void vfio_iommufd_cpr_register_device(VFIODevice *vbasedev)
 {
     if (!cpr_is_incoming()) {
+        /*
+         * Beware fd may have already been saved by vfio_device_set_fd,
+         * so call resave to avoid a duplicate entry.
+         */
+        cpr_resave_fd(vbasedev->name, 0, vbasedev->fd);
         vfio_cpr_save_device(vbasedev);
     }
 }
 
 void vfio_iommufd_cpr_unregister_device(VFIODevice *vbasedev)
 {
+    cpr_delete_fd(vbasedev->name, 0);
     vfio_cpr_delete_device(vbasedev->name);
 }
 
@@ -180,5 +186,9 @@ void vfio_cpr_load_device(VFIODevice *vbasedev)
     if (cpr_is_incoming()) {
         bool ret = vfio_cpr_find_device(vbasedev);
         g_assert(ret);
+
+        if (vbasedev->fd < 0) {
+            vbasedev->fd = cpr_find_fd(vbasedev->name, 0);
+        }
     }
 }
diff --git a/hw/vfio/device.c b/hw/vfio/device.c
index 0ae3f3c6607d329e08fd435d2934e23affc27264..96cf21462cb10f9ebaf1bda54fed13a95b79f223 100644
--- a/hw/vfio/device.c
+++ b/hw/vfio/device.c
@@ -351,14 +351,7 @@ void vfio_device_free_name(VFIODevice *vbasedev)
 
 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp)
 {
-    ERRP_GUARD();
-    int fd = monitor_fd_param(monitor_cur(), str, errp);
-
-    if (fd < 0) {
-        error_prepend(errp, "Could not parse remote object fd %s:", str);
-        return;
-    }
-    vbasedev->fd = fd;
+    vbasedev->fd = cpr_get_fd_param(vbasedev->dev->id, str, 0, errp);
 }
 
 static VFIODeviceIOOps vfio_device_io_ops_ioctl;
-- 
2.50.0



  parent reply	other threads:[~2025-07-04  8:47 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-04  8:45 [PULL 00/27] vfio queue Cédric Le Goater
2025-07-04  8:45 ` [PULL 01/27] vfio/container: Fix potential SIGSEGV when recover from unmap-all-vaddr failure Cédric Le Goater
2025-07-04  8:45 ` [PULL 02/27] vfio/container: Fix vfio_container_post_load() Cédric Le Goater
2025-07-04  8:45 ` [PULL 03/27] vfio-user: do not register vfio-user container with cpr Cédric Le Goater
2025-07-04  8:45 ` [PULL 04/27] i386/tdx: Build TDX only for 64-bit target Cédric Le Goater
2025-07-04  8:45 ` [PULL 05/27] b4: Drop linktrailermask Cédric Le Goater
2025-07-04  8:45 ` [PULL 06/27] Makefile: prune quilt source files for cscope Cédric Le Goater
2025-07-04  8:45 ` [PULL 07/27] vfio-pci: preserve MSI Cédric Le Goater
2025-07-04  8:45 ` [PULL 08/27] vfio-pci: preserve INTx Cédric Le Goater
2025-07-04  8:45 ` [PULL 09/27] migration: close kvm after cpr Cédric Le Goater
2025-07-04  8:45 ` [PULL 10/27] migration: cpr_get_fd_param helper Cédric Le Goater
2025-07-04  8:45 ` [PULL 11/27] backends/iommufd: iommufd_backend_map_file_dma Cédric Le Goater
2025-07-04  8:45 ` [PULL 12/27] backends/iommufd: change process ioctl Cédric Le Goater
2025-07-04  8:45 ` [PULL 13/27] physmem: qemu_ram_get_fd_offset Cédric Le Goater
2025-07-04  8:45 ` [PULL 14/27] vfio/iommufd: use IOMMU_IOAS_MAP_FILE Cédric Le Goater
2025-07-04  8:45 ` [PULL 15/27] vfio/iommufd: invariant device name Cédric Le Goater
2025-07-04  8:45 ` [PULL 16/27] vfio/iommufd: add vfio_device_free_name Cédric Le Goater
2025-07-04  8:45 ` [PULL 17/27] vfio/iommufd: device name blocker Cédric Le Goater
2025-07-04  8:45 ` [PULL 18/27] vfio/iommufd: register container for cpr Cédric Le Goater
2025-07-04  8:45 ` [PULL 19/27] migration: vfio cpr state hook Cédric Le Goater
2025-07-04  8:45 ` [PULL 20/27] vfio/iommufd: cpr state Cédric Le Goater
2025-07-04  8:45 ` Cédric Le Goater [this message]
2025-07-04  8:45 ` [PULL 22/27] vfio/iommufd: reconstruct device Cédric Le Goater
2025-07-04  8:45 ` [PULL 23/27] vfio/iommufd: reconstruct hwpt Cédric Le Goater
2025-07-04  8:45 ` [PULL 24/27] vfio/iommufd: change process Cédric Le Goater
2025-07-04  8:45 ` [PULL 25/27] iommufd: preserve DMA mappings Cédric Le Goater
2025-07-04  8:45 ` [PULL 26/27] vfio/container: delete old cpr register Cédric Le Goater
2025-07-04  8:45 ` [PULL 27/27] vfio: doc changes for cpr Cédric Le Goater
2025-07-04 17:49 ` [PULL 00/27] vfio queue Stefan Hajnoczi

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=20250704084528.1412959-22-clg@redhat.com \
    --to=clg@redhat.com \
    --cc=alex.williamson@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=steven.sistare@oracle.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).