[PATCH v4 1/9] vfio: Determine noiommu in vfio_device registration

Yi Liu
2023-04-26T14:54:31Z

This adds a noiommu flag in vfio_device, hence caller of the
vfio_device_is_noiommu() just refers to the flag for noiommu
check.

Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Tested-by: Nicolin Chen <nicolinc@nvidia.com>
Tested-by: Yanting Jiang <yanting.jiang@intel.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
---
drivers/vfio/iommufd.c | 4 ++--
drivers/vfio/vfio.h | 7 ++++---
drivers/vfio/vfio_main.c | 4 ++++
include/linux/vfio.h | 1 +
4 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c
index 88b00c501015..895852ad37ed 100644
--- a/drivers/vfio/iommufd.c
+++ b/drivers/vfio/iommufd.c
@@ -18,7 +18,7 @@ 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 (vdev->noiommu) {
if (!capable(CAP_SYS_RAWIO))
return -EPERM;

@@ -59,7 +59,7 @@ void vfio_iommufd_unbind(struct vfio_device *vdev)
{
lockdep_assert_held(&vdev->dev_set->lock);

- if (vfio_device_is_noiommu(vdev))
+ if (vdev->noiommu)
return;

if (vdev->ops->unbind_iommufd)
diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
index 7b19c621e0e6..1ddf43863ad6 100644
--- a/drivers/vfio/vfio.h
+++ b/drivers/vfio/vfio.h
@@ -88,10 +88,11 @@ bool vfio_device_has_container(struct vfio_device *device);
int __init vfio_group_init(void);
void vfio_group_cleanup(void);

-static inline bool vfio_device_is_noiommu(struct vfio_device *vdev)
+static inline int vfio_device_set_noiommu(struct vfio_device *device)
{
- return IS_ENABLED(CONFIG_VFIO_NOIOMMU) &&
- vdev->group->type == VFIO_NO_IOMMU;
+ device->noiommu = IS_ENABLED(CONFIG_VFIO_NOIOMMU) &&
+ device->group->type == VFIO_NO_IOMMU;
+ return 0;
}

#if IS_ENABLED(CONFIG_VFIO_CONTAINER)
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 89497c933490..09be9df2ceca 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -276,6 +276,10 @@ static int __vfio_register_dev(struct vfio_device *device,
if (ret)
return ret;

+ ret = vfio_device_set_noiommu(device);
+ if (ret)
+ goto err_out;
+
ret = device_add(&device->device);
if (ret)
goto err_out;
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 2c137ea94a3e..4ee613924435 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -62,6 +62,7 @@ struct vfio_device {
struct iommufd_device *iommufd_device;
bool iommufd_attached;
#endif
+ bool noiommu;
};

/**
--
2.34.1