diff mbox series

[v6,17/24] vfio-iommufd: Make vfio_iommufd_bind() selectively return devid

Message ID 20230308132903.465159-18-yi.l.liu@intel.com (mailing list archive)
State New, archived
Headers show
Series cover-letter: Add vfio_device cdev for iommufd support | expand

Commit Message

Yi Liu March 8, 2023, 1:28 p.m. UTC
bind_iommufd() will generate an ID to represent this bond, it is needed
by userspace for further usage. devid is stored in vfio_device_file to
avoid passing devid pointer in multiple places.

Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
 drivers/vfio/iommufd.c   | 13 ++++++++++---
 drivers/vfio/vfio.h      |  6 ++++--
 drivers/vfio/vfio_main.c |  8 +++++---
 include/linux/iommufd.h  |  2 ++
 4 files changed, 21 insertions(+), 8 deletions(-)

Comments

Tian, Kevin March 10, 2023, 8:31 a.m. UTC | #1
> From: Liu, Yi L <yi.l.liu@intel.com>
> Sent: Wednesday, March 8, 2023 9:29 PM
> 
> bind_iommufd() will generate an ID to represent this bond, it is needed
> by userspace for further usage. devid is stored in vfio_device_file to
> avoid passing devid pointer in multiple places.

after removing vfio_iommufd_bind() then the caller can directly get
the id when calling .bind_iommufd().
diff mbox series

Patch

diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c
index 8c518f8bd39a..b2cdb6b2b37f 100644
--- a/drivers/vfio/iommufd.c
+++ b/drivers/vfio/iommufd.c
@@ -28,7 +28,8 @@  static int vfio_iommufd_device_probe_comapt_noiommu(struct vfio_device *vdev,
 	return 0;
 }
 
-int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)
+int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx,
+		      u32 *devid)
 {
 	u32 device_id;
 	int ret;
@@ -44,8 +45,14 @@  int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)
 	if (WARN_ON(!vdev->ops->bind_iommufd))
 		return -ENODEV;
 
-	/* The legacy path has no way to return the device id */
-	return vdev->ops->bind_iommufd(vdev, ictx, &device_id);
+	ret = vdev->ops->bind_iommufd(vdev, ictx, &device_id);
+	if (ret)
+		return ret;
+
+	if (devid)
+		*devid = device_id;
+
+	return 0;
 }
 
 int vfio_iommufd_attach_compat_ioas(struct vfio_device *vdev,
diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
index 615ffd58562b..98cee2f765e9 100644
--- a/drivers/vfio/vfio.h
+++ b/drivers/vfio/vfio.h
@@ -24,6 +24,7 @@  struct vfio_device_file {
 	spinlock_t kvm_ref_lock; /* protect kvm field */
 	struct kvm *kvm;
 	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
+	u32 devid; /* only valid when iommufd is valid */
 };
 
 void vfio_device_put_registration(struct vfio_device *device);
@@ -236,13 +237,14 @@  static inline void vfio_container_cleanup(void)
 #endif
 
 #if IS_ENABLED(CONFIG_IOMMUFD)
-int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx);
+int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx,
+		      u32 *devid);
 void vfio_iommufd_unbind(struct vfio_device *device);
 int vfio_iommufd_attach_compat_ioas(struct vfio_device *vdev,
 				    struct iommufd_ctx *ictx);
 #else
 static inline int vfio_iommufd_bind(struct vfio_device *device,
-				    struct iommufd_ctx *ictx)
+				    struct iommufd_ctx *ictx, u32 *devid)
 {
 	return -EOPNOTSUPP;
 }
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 8c73df1a400e..a66ca138059b 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -444,7 +444,7 @@  static int vfio_device_first_open(struct vfio_device_file *df)
 	 * to be done here just go ahead to open device.
 	 */
 	if (iommufd)
-		ret = vfio_iommufd_bind(device, iommufd);
+		ret = vfio_iommufd_bind(device, iommufd, &df->devid);
 	else if (vfio_device_group_uses_container(df))
 		ret = vfio_device_group_use_iommu(device);
 	if (ret)
@@ -476,10 +476,12 @@  static void vfio_device_last_close(struct vfio_device_file *df)
 
 	if (device->ops->close_device)
 		device->ops->close_device(device);
-	if (iommufd)
+	if (iommufd) {
 		vfio_iommufd_unbind(device);
-	else if (vfio_device_group_uses_container(df))
+		df->devid = IOMMUFD_INVALID_ID;
+	} else if (vfio_device_group_uses_container(df)) {
 		vfio_device_group_unuse_iommu(device);
+	}
 	module_put(device->dev->driver->owner);
 }
 
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index 7a0d7f2c4237..48b9bfab9891 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -10,6 +10,8 @@ 
 #include <linux/errno.h>
 #include <linux/err.h>
 
+#define IOMMUFD_INVALID_ID 0
+
 struct device;
 struct iommufd_device;
 struct page;