[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 35/37] vfio/platform: Make vfio cdev pre-openable by passing a
From: |
Zhenzhong Duan |
Subject: |
[PATCH v3 35/37] vfio/platform: Make vfio cdev pre-openable by passing a file handle |
Date: |
Thu, 26 Oct 2023 18:31:02 +0800 |
This gives management tools like libvirt a chance to open the vfio
cdev with privilege and pass FD to qemu. This way qemu never needs
to have privilege to open a VFIO or iommu cdev node.
Signed-off-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
hw/vfio/platform.c | 41 +++++++++++++++++++++++++++++++++--------
1 file changed, 33 insertions(+), 8 deletions(-)
diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c
index a1c25e0337..aa0b2b9583 100644
--- a/hw/vfio/platform.c
+++ b/hw/vfio/platform.c
@@ -35,6 +35,7 @@
#include "hw/platform-bus.h"
#include "hw/qdev-properties.h"
#include "sysemu/kvm.h"
+#include "monitor/monitor.h"
/*
* Functions used whatever the injection method
@@ -529,14 +530,13 @@ static VFIODeviceOps vfio_platform_ops = {
*/
static int vfio_base_device_init(VFIODevice *vbasedev, Error **errp)
{
- struct stat st;
int ret;
- /* @sysfsdev takes precedence over @host */
- if (vbasedev->sysfsdev) {
+ /* @fd takes precedence over @sysfsdev which takes precedence over @host */
+ if (vbasedev->fd < 0 && vbasedev->sysfsdev) {
g_free(vbasedev->name);
vbasedev->name = g_path_get_basename(vbasedev->sysfsdev);
- } else {
+ } else if (vbasedev->fd < 0) {
if (!vbasedev->name || strchr(vbasedev->name, '/')) {
error_setg(errp, "wrong host device name");
return -EINVAL;
@@ -546,10 +546,9 @@ static int vfio_base_device_init(VFIODevice *vbasedev,
Error **errp)
vbasedev->name);
}
- if (stat(vbasedev->sysfsdev, &st) < 0) {
- error_setg_errno(errp, errno,
- "failed to get the sysfs host device file status");
- return -errno;
+ ret = vfio_device_get_name(vbasedev, errp);
+ if (ret) {
+ return ret;
}
ret = vfio_attach_device(vbasedev->name, vbasedev,
@@ -656,6 +655,28 @@ static Property vfio_platform_dev_properties[] = {
DEFINE_PROP_END_OF_LIST(),
};
+static void vfio_platform_instance_init(Object *obj)
+{
+ VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(obj);
+
+ vdev->vbasedev.fd = -1;
+}
+
+#ifdef CONFIG_IOMMUFD
+static void vfio_platform_set_fd(Object *obj, const char *str, Error **errp)
+{
+ VFIOPlatformDevice *vdev = VFIO_PLATFORM_DEVICE(obj);
+ int fd = -1;
+
+ fd = monitor_fd_param(monitor_cur(), str, errp);
+ if (fd == -1) {
+ error_prepend(errp, "Could not parse remote object fd %s:", str);
+ return;
+ }
+ vdev->vbasedev.fd = fd;
+}
+#endif
+
static void vfio_platform_class_init(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
@@ -663,6 +684,9 @@ static void vfio_platform_class_init(ObjectClass *klass,
void *data)
dc->realize = vfio_platform_realize;
device_class_set_props(dc, vfio_platform_dev_properties);
+#ifdef CONFIG_IOMMUFD
+ object_class_property_add_str(klass, "fd", NULL, vfio_platform_set_fd);
+#endif
dc->vmsd = &vfio_platform_vmstate;
dc->desc = "VFIO-based platform device assignment";
sbc->connect_irq_notifier = vfio_start_irqfd_injection;
@@ -675,6 +699,7 @@ static const TypeInfo vfio_platform_dev_info = {
.name = TYPE_VFIO_PLATFORM,
.parent = TYPE_SYS_BUS_DEVICE,
.instance_size = sizeof(VFIOPlatformDevice),
+ .instance_init = vfio_platform_instance_init,
.class_init = vfio_platform_class_init,
.class_size = sizeof(VFIOPlatformDeviceClass),
};
--
2.34.1
- [PATCH v3 21/37] vfio/spapr: Move prereg_listener into spapr container, (continued)
- [PATCH v3 21/37] vfio/spapr: Move prereg_listener into spapr container, Zhenzhong Duan, 2023/10/26
- [PATCH v3 23/37] Add iommufd configure option, Zhenzhong Duan, 2023/10/26
- [PATCH v3 26/37] vfio/iommufd: Implement the iommufd backend, Zhenzhong Duan, 2023/10/26
- [PATCH v3 27/37] vfio/iommufd: Switch to manual hwpt allocation, Zhenzhong Duan, 2023/10/26
- [PATCH v3 28/37] vfio/iommufd: Add support for iova_ranges, Zhenzhong Duan, 2023/10/26
- [PATCH v3 30/37] vfio/pci: Extract out a helper vfio_pci_get_pci_hot_reset_info, Zhenzhong Duan, 2023/10/26
- [PATCH v3 31/37] vfio/pci: Adapt vfio pci hot reset support with iommufd BE, Zhenzhong Duan, 2023/10/26
- [PATCH v3 29/37] vfio/iommufd: Bypass EEH if iommufd backend, Zhenzhong Duan, 2023/10/26
- [PATCH v3 32/37] vfio/pci: Allow the selection of a given iommu backend, Zhenzhong Duan, 2023/10/26
- [PATCH v3 33/37] vfio/pci: Make vfio cdev pre-openable by passing a file handle, Zhenzhong Duan, 2023/10/26
- [PATCH v3 35/37] vfio/platform: Make vfio cdev pre-openable by passing a file handle,
Zhenzhong Duan <=
- [PATCH v3 34/37] vfio: Allow the selection of a given iommu backend for platform ap and ccw, Zhenzhong Duan, 2023/10/26
- [PATCH v3 36/37] vfio/ap: Make vfio cdev pre-openable by passing a file handle, Zhenzhong Duan, 2023/10/26
- [PATCH v3 37/37] vfio/ccw: Make vfio cdev pre-openable by passing a file handle, Zhenzhong Duan, 2023/10/26
- Re: [PATCH v3 00/37] vfio: Adopt iommufd, Markus Armbruster, 2023/10/26