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@xxxxxxxxx> --- 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(-) 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; -- 2.34.1