[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 32/47] vfio/platform: Make vfio cdev pre-openable by passing a fil
From: |
Cédric Le Goater |
Subject: |
[PULL 32/47] vfio/platform: Make vfio cdev pre-openable by passing a file handle |
Date: |
Tue, 19 Dec 2023 19:56:28 +0100 |
From: Zhenzhong Duan <zhenzhong.duan@intel.com>
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>
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Tested-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
hw/vfio/platform.c | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)
diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c
index
98ae4bc655b401250bd4cb943e4efec97f486a5f..a97d9c6234ccac0ba26b44d3e2290616eda5debd
100644
--- a/hw/vfio/platform.c
+++ b/hw/vfio/platform.c
@@ -531,14 +531,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;
@@ -548,10 +547,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,
@@ -658,6 +656,20 @@ 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)
+{
+ vfio_device_set_fd(&VFIO_PLATFORM_DEVICE(obj)->vbasedev, str, errp);
+}
+#endif
+
static void vfio_platform_class_init(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
@@ -665,6 +677,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;
@@ -677,6 +692,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.43.0
- [PULL 28/47] vfio/iommufd: Enable pci hot reset through iommufd cdev interface, (continued)
- [PULL 28/47] vfio/iommufd: Enable pci hot reset through iommufd cdev interface, Cédric Le Goater, 2023/12/19
- [PULL 30/47] vfio/pci: Make vfio cdev pre-openable by passing a file handle, Cédric Le Goater, 2023/12/19
- [PULL 33/47] vfio/ap: Allow the selection of a given iommu backend, Cédric Le Goater, 2023/12/19
- [PULL 37/47] vfio: Make VFIOContainerBase poiner parameter const in VFIOIOMMUOps callbacks, Cédric Le Goater, 2023/12/19
- [PULL 36/47] vfio/ccw: Make vfio cdev pre-openable by passing a file handle, Cédric Le Goater, 2023/12/19
- [PULL 27/47] vfio/pci: Introduce a vfio pci hot reset interface, Cédric Le Goater, 2023/12/19
- [PULL 29/47] vfio/pci: Allow the selection of a given iommu backend, Cédric Le Goater, 2023/12/19
- [PULL 34/47] vfio/ap: Make vfio cdev pre-openable by passing a file handle, Cédric Le Goater, 2023/12/19
- [PULL 35/47] vfio/ccw: Allow the selection of a given iommu backend, Cédric Le Goater, 2023/12/19
- [PULL 38/47] hw/arm: Activate IOMMUFD for virt machines, Cédric Le Goater, 2023/12/19
- [PULL 32/47] vfio/platform: Make vfio cdev pre-openable by passing a file handle,
Cédric Le Goater <=
- [PULL 45/47] vfio: Introduce a helper function to initialize VFIODevice, Cédric Le Goater, 2023/12/19
- [PULL 46/47] docs/devel: Add VFIO iommufd backend documentation, Cédric Le Goater, 2023/12/19
- [PULL 42/47] vfio/platform: Move VFIODevice initializations in vfio_platform_instance_init, Cédric Le Goater, 2023/12/19
- [PULL 44/47] vfio/ccw: Move VFIODevice initializations in vfio_ccw_instance_init, Cédric Le Goater, 2023/12/19
- [PULL 39/47] kconfig: Activate IOMMUFD for s390x machines, Cédric Le Goater, 2023/12/19
- [PULL 41/47] vfio/pci: Move VFIODevice initializations in vfio_instance_init, Cédric Le Goater, 2023/12/19
- [PULL 43/47] vfio/ap: Move VFIODevice initializations in vfio_ap_instance_init, Cédric Le Goater, 2023/12/19
- [PULL 47/47] hw/ppc/Kconfig: Imply VFIO_PCI, Cédric Le Goater, 2023/12/19
- [PULL 40/47] hw/i386: Activate IOMMUFD for q35 machines, Cédric Le Goater, 2023/12/19
- Re: [PULL 00/47] vfio queue, Stefan Hajnoczi, 2023/12/20