@@ -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,
@@ -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;
}
@@ -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);
}
@@ -10,6 +10,8 @@
#include <linux/errno.h>
#include <linux/err.h>
+#define IOMMUFD_INVALID_ID 0
+
struct device;
struct iommufd_device;
struct page;
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(-)