[PATCH v7 12/22] vfio-iommufd: Move noiommu support out of vfio_iommufd_bind()

Yi Liu
2023-03-16T12:56:06Z

into vfio_device_group_open(). This is also more consistent with what
will be done in vfio device cdev path.

Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
drivers/vfio/group.c | 9 +++++++++
drivers/vfio/iommufd.c | 35 ++++++++++++++++++-----------------
drivers/vfio/vfio.h | 9 +++++++++
3 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c
index 36e105960dd8..1ad8df9b295f 100644
--- a/drivers/vfio/group.c
+++ b/drivers/vfio/group.c
@@ -192,6 +192,15 @@ static int vfio_device_group_open(struct vfio_device_file *df)
vfio_device_group_get_kvm_safe(device);

df->iommufd = device->group->iommufd;
+ if (device->group->iommufd && vfio_device_is_noiommu(device)) {
+ if (device->open_count == 0) {
+ ret = vfio_iommufd_enable_noiommu_compat(device,
+ device->group->iommufd);
+ if (ret)
+ goto out_put_kvm;
+ }
+ df->iommufd = NULL;
+ }

ret = vfio_device_open(df);
if (ret) {
diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c
index ca0c16bb747e..7334b67dbb8a 100644
--- a/drivers/vfio/iommufd.c
+++ b/drivers/vfio/iommufd.c
@@ -10,6 +10,24 @@
MODULE_IMPORT_NS(IOMMUFD);
MODULE_IMPORT_NS(IOMMUFD_VFIO);

+int vfio_iommufd_enable_noiommu_compat(struct vfio_device *device,
+ struct iommufd_ctx *ictx)
+{
+ u32 ioas_id;
+
+ if (!capable(CAP_SYS_RAWIO))
+ return -EPERM;
+
+ /*
+ * Require no compat ioas to be assigned to proceed. The basic
+ * statement is that the user cannot have done something that
+ * implies they expected translation to exist
+ */
+ if (!iommufd_vfio_compat_ioas_get_id(ictx, &ioas_id))
+ return -EPERM;
+ return 0;
+}
+
int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)
{
u32 ioas_id;
@@ -18,20 +36,6 @@ int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx)

lockdep_assert_held(&vdev->dev_set->lock);

- if (vfio_device_is_noiommu(vdev)) {
- if (!capable(CAP_SYS_RAWIO))
- return -EPERM;
-
- /*
- * Require no compat ioas to be assigned to proceed. The basic
- * statement is that the user cannot have done something that
- * implies they expected translation to exist
- */
- if (!iommufd_vfio_compat_ioas_get_id(ictx, &ioas_id))
- return -EPERM;
- return 0;
- }
-
ret = vdev->ops->bind_iommufd(vdev, ictx, &device_id);
if (ret)
return ret;
@@ -59,9 +63,6 @@ void vfio_iommufd_unbind(struct vfio_device *vdev)
{
lockdep_assert_held(&vdev->dev_set->lock);

- if (vfio_device_is_noiommu(vdev))
- return;
-
if (vdev->ops->unbind_iommufd)
vdev->ops->unbind_iommufd(vdev);
}
diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
index 7d4108cbc185..136137b8618d 100644
--- a/drivers/vfio/vfio.h
+++ b/drivers/vfio/vfio.h
@@ -236,9 +236,18 @@ static inline void vfio_container_cleanup(void)
#endif

#if IS_ENABLED(CONFIG_IOMMUFD)
+int vfio_iommufd_enable_noiommu_compat(struct vfio_device *device,
+ struct iommufd_ctx *ictx);
int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx);
void vfio_iommufd_unbind(struct vfio_device *device);
#else
+static inline int
+vfio_iommufd_enable_noiommu_compat(struct vfio_device *device,
+ struct iommufd_ctx *ictx)
+{
+ return -EOPNOTSUPP;
+}
+
static inline int vfio_iommufd_bind(struct vfio_device *device,
struct iommufd_ctx *ictx)
{
--
2.34.1