vfio: factor out a vfio_iommu_driver_allowed helper
authorChristoph Hellwig <hch@lst.de>
Fri, 24 Sep 2021 15:56:52 +0000 (17:56 +0200)
committerAlex Williamson <alex.williamson@redhat.com>
Thu, 30 Sep 2021 18:46:43 +0000 (12:46 -0600)
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>
Link: https://lore.kernel.org/r/20210924155705.4258-3-hch@lst.de
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
drivers/vfio/vfio.c

index b483b61b7c220d0f296208bb66827a750d0a766b..faf8e0d637bb94ef9447ee1aebb82345dc00a3cc 100644 (file)
@@ -257,8 +257,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
@@ -1034,13 +1049,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;
 
@@ -1112,15 +1124,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;