vfio: factor out a vfio_iommu_driver_allowed helper

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>
This commit is contained in:
Christoph Hellwig 2021-09-24 17:56:52 +02:00 committed by Alex Williamson
parent 38a68934aa
commit b00621603d
1 changed files with 19 additions and 14 deletions

View 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;