Skip to content

Commit 2a3f0a5

Browse files
Steve Sistarelegoater
authored andcommitted
vfio/iommufd: preserve descriptors
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>
1 parent f2f3e46 commit 2a3f0a5

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

backends/iommufd.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616
#include "qemu/module.h"
1717
#include "qom/object_interfaces.h"
1818
#include "qemu/error-report.h"
19+
#include "migration/cpr.h"
1920
#include "monitor/monitor.h"
2021
#include "trace.h"
2122
#include "hw/vfio/vfio-device.h"
2223
#include <sys/ioctl.h>
2324
#include <linux/iommufd.h>
2425

26+
static const char *iommufd_fd_name(IOMMUFDBackend *be)
27+
{
28+
return object_get_canonical_path_component(OBJECT(be));
29+
}
30+
2531
static void iommufd_backend_init(Object *obj)
2632
{
2733
IOMMUFDBackend *be = IOMMUFD_BACKEND(obj);
@@ -64,11 +70,27 @@ static bool iommufd_backend_can_be_deleted(UserCreatable *uc)
6470
return !be->users;
6571
}
6672

73+
static void iommufd_backend_complete(UserCreatable *uc, Error **errp)
74+
{
75+
IOMMUFDBackend *be = IOMMUFD_BACKEND(uc);
76+
const char *name = iommufd_fd_name(be);
77+
78+
if (!be->owned) {
79+
/* fd came from the command line. Fetch updated value from cpr state. */
80+
if (cpr_is_incoming()) {
81+
be->fd = cpr_find_fd(name, 0);
82+
} else {
83+
cpr_save_fd(name, 0, be->fd);
84+
}
85+
}
86+
}
87+
6788
static void iommufd_backend_class_init(ObjectClass *oc, const void *data)
6889
{
6990
UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
7091

7192
ucc->can_be_deleted = iommufd_backend_can_be_deleted;
93+
ucc->complete = iommufd_backend_complete;
7294

7395
object_class_property_add_str(oc, "fd", NULL, iommufd_backend_set_fd);
7496
}
@@ -102,7 +124,7 @@ bool iommufd_backend_connect(IOMMUFDBackend *be, Error **errp)
102124
int fd;
103125

104126
if (be->owned && !be->users) {
105-
fd = qemu_open("/dev/iommu", O_RDWR, errp);
127+
fd = cpr_open_fd("/dev/iommu", O_RDWR, iommufd_fd_name(be), 0, errp);
106128
if (fd < 0) {
107129
return false;
108130
}
@@ -127,14 +149,15 @@ void iommufd_backend_disconnect(IOMMUFDBackend *be)
127149
goto out;
128150
}
129151
be->users--;
130-
if (!be->users && be->owned) {
131-
close(be->fd);
132-
be->fd = -1;
133-
}
134-
out:
135152
if (!be->users) {
136153
vfio_iommufd_cpr_unregister_iommufd(be);
154+
if (be->owned) {
155+
cpr_delete_fd(iommufd_fd_name(be), 0);
156+
close(be->fd);
157+
be->fd = -1;
158+
}
137159
}
160+
out:
138161
trace_iommufd_backend_disconnect(be->fd, be->users);
139162
}
140163

hw/vfio/cpr-iommufd.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,18 @@ void vfio_iommufd_cpr_unregister_container(VFIOIOMMUFDContainer *container)
166166
void vfio_iommufd_cpr_register_device(VFIODevice *vbasedev)
167167
{
168168
if (!cpr_is_incoming()) {
169+
/*
170+
* Beware fd may have already been saved by vfio_device_set_fd,
171+
* so call resave to avoid a duplicate entry.
172+
*/
173+
cpr_resave_fd(vbasedev->name, 0, vbasedev->fd);
169174
vfio_cpr_save_device(vbasedev);
170175
}
171176
}
172177

173178
void vfio_iommufd_cpr_unregister_device(VFIODevice *vbasedev)
174179
{
180+
cpr_delete_fd(vbasedev->name, 0);
175181
vfio_cpr_delete_device(vbasedev->name);
176182
}
177183

@@ -180,5 +186,9 @@ void vfio_cpr_load_device(VFIODevice *vbasedev)
180186
if (cpr_is_incoming()) {
181187
bool ret = vfio_cpr_find_device(vbasedev);
182188
g_assert(ret);
189+
190+
if (vbasedev->fd < 0) {
191+
vbasedev->fd = cpr_find_fd(vbasedev->name, 0);
192+
}
183193
}
184194
}

hw/vfio/device.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,7 @@ void vfio_device_free_name(VFIODevice *vbasedev)
351351

352352
void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp)
353353
{
354-
ERRP_GUARD();
355-
int fd = monitor_fd_param(monitor_cur(), str, errp);
356-
357-
if (fd < 0) {
358-
error_prepend(errp, "Could not parse remote object fd %s:", str);
359-
return;
360-
}
361-
vbasedev->fd = fd;
354+
vbasedev->fd = cpr_get_fd_param(vbasedev->dev->id, str, 0, errp);
362355
}
363356

364357
static VFIODeviceIOOps vfio_device_io_ops_ioctl;

0 commit comments

Comments
 (0)