diff mbox series

[02/14] vfio: factor out a vfio_iommu_driver_allowed helper

Message ID 20210825161916.50393-3-hch@lst.de (mailing list archive)
State New, archived
Headers show
Series [01/14] vfio: Move vfio_iommu_group_get() to vfio_register_group_dev() | expand

Commit Message

Christoph Hellwig Aug. 25, 2021, 4:19 p.m. UTC
Factor out a little helper to make the checks for the noiommu driver less
ugly.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/vfio.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

Comments

Tian, Kevin Aug. 26, 2021, 1:49 a.m. UTC | #1
> From: Christoph Hellwig <hch@lst.de>
> Sent: Thursday, August 26, 2021 12:19 AM
> 
> Factor out a little helper to make the checks for the noiommu driver less
> ugly.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/vfio/vfio.c | 33 +++++++++++++++++++--------------
>  1 file changed, 19 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
> index 5bd520f0dc6107..6705349ed93378 100644
> --- a/drivers/vfio/vfio.c
> +++ b/drivers/vfio/vfio.c
> @@ -267,8 +267,23 @@ static const struct vfio_iommu_driver_ops
> vfio_noiommu_ops = {
>  	.attach_group = vfio_noiommu_attach_group,
>  	.detach_group = vfio_noiommu_detach_group,
>  };
> -#endif
> 
> +/*
> + * Only noiommu containers can use vfio-noiommu and noiommu
> containers can only
> + * use vfio-noiommu.
> + */
> +static inline bool vfio_iommu_driver_allowed(struct vfio_container
> *container,
> +		const struct vfio_iommu_driver *driver)
> +{
> +	return container->noiommu == (driver->ops == &vfio_noiommu_ops);
> +}
> +#else
> +static inline bool vfio_iommu_driver_allowed(struct vfio_container
> *container,
> +		const struct vfio_iommu_driver *driver)
> +{
> +	return true;
> +}
> +#endif /* CONFIG_VFIO_NOIOMMU */
> 
>  /**
>   * IOMMU driver registration
> @@ -1031,13 +1046,10 @@ static long vfio_ioctl_check_extension(struct
> vfio_container *container,
>  			list_for_each_entry(driver, &vfio.iommu_drivers_list,
>  					    vfio_next) {
> 
> -#ifdef CONFIG_VFIO_NOIOMMU
>  				if (!list_empty(&container->group_list) &&
> -				    (container->noiommu !=
> -				     (driver->ops == &vfio_noiommu_ops)))
> +				    !vfio_iommu_driver_allowed(container,
> +							       driver))
>  					continue;
> -#endif
> -
>  				if (!try_module_get(driver->ops->owner))
>  					continue;
> 
> @@ -1109,15 +1121,8 @@ static long vfio_ioctl_set_iommu(struct
> vfio_container *container,
>  	list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
>  		void *data;
> 
> -#ifdef CONFIG_VFIO_NOIOMMU
> -		/*
> -		 * Only noiommu containers can use vfio-noiommu and
> noiommu
> -		 * containers can only use vfio-noiommu.
> -		 */
> -		if (container->noiommu != (driver->ops ==
> &vfio_noiommu_ops))
> +		if (!vfio_iommu_driver_allowed(container, driver))
>  			continue;
> -#endif
> -
>  		if (!try_module_get(driver->ops->owner))
>  			continue;
> 
> --
> 2.30.2
diff mbox series

Patch

diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index 5bd520f0dc6107..6705349ed93378 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -267,8 +267,23 @@  static const struct vfio_iommu_driver_ops vfio_noiommu_ops = {
 	.attach_group = vfio_noiommu_attach_group,
 	.detach_group = vfio_noiommu_detach_group,
 };
-#endif
 
+/*
+ * Only noiommu containers can use vfio-noiommu and noiommu containers can only
+ * use vfio-noiommu.
+ */
+static inline bool vfio_iommu_driver_allowed(struct vfio_container *container,
+		const struct vfio_iommu_driver *driver)
+{
+	return container->noiommu == (driver->ops == &vfio_noiommu_ops);
+}
+#else
+static inline bool vfio_iommu_driver_allowed(struct vfio_container *container,
+		const struct vfio_iommu_driver *driver)
+{
+	return true;
+}
+#endif /* CONFIG_VFIO_NOIOMMU */
 
 /**
  * IOMMU driver registration
@@ -1031,13 +1046,10 @@  static long vfio_ioctl_check_extension(struct vfio_container *container,
 			list_for_each_entry(driver, &vfio.iommu_drivers_list,
 					    vfio_next) {
 
-#ifdef CONFIG_VFIO_NOIOMMU
 				if (!list_empty(&container->group_list) &&
-				    (container->noiommu !=
-				     (driver->ops == &vfio_noiommu_ops)))
+				    !vfio_iommu_driver_allowed(container,
+							       driver))
 					continue;
-#endif
-
 				if (!try_module_get(driver->ops->owner))
 					continue;
 
@@ -1109,15 +1121,8 @@  static long vfio_ioctl_set_iommu(struct vfio_container *container,
 	list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) {
 		void *data;
 
-#ifdef CONFIG_VFIO_NOIOMMU
-		/*
-		 * Only noiommu containers can use vfio-noiommu and noiommu
-		 * containers can only use vfio-noiommu.
-		 */
-		if (container->noiommu != (driver->ops == &vfio_noiommu_ops))
+		if (!vfio_iommu_driver_allowed(container, driver))
 			continue;
-#endif
-
 		if (!try_module_get(driver->ops->owner))
 			continue;