On Wed, 11 Aug 2021 17:14:51 +0200 Christoph Hellwig <hch@xxxxxx> wrote: > @@ -833,14 +789,61 @@ void vfio_uninit_group_dev(struct vfio_device *device) > } > EXPORT_SYMBOL_GPL(vfio_uninit_group_dev); > > -struct vfio_group *vfio_group_find_or_alloc(struct device *dev) > +#ifdef CONFIG_VFIO_NOIOMMU > +static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev) > { > struct iommu_group *iommu_group; > struct vfio_group *group; > + int ret; > > - iommu_group = vfio_iommu_group_get(dev); > - if (!iommu_group) > + iommu_group = iommu_group_alloc(); > + if (IS_ERR(iommu_group)) > + return ERR_CAST(iommu_group); > + > + iommu_group_set_name(iommu_group, "vfio-noiommu"); > + iommu_group_set_iommudata(iommu_group, &noiommu, NULL); > + ret = iommu_group_add_device(iommu_group, dev); > + if (ret) > + goto out_put_group; > + > + group = vfio_create_group(iommu_group); > + if (IS_ERR(group)) { > + ret = PTR_ERR(group); > + goto out_remove_device; > + } > + > + return group; > + > +out_remove_device: > + iommu_group_remove_device(dev); > +out_put_group: > + iommu_group_put(iommu_group); > + return ERR_PTR(ret); > +} > +#endif > + > +static struct vfio_group *vfio_group_find_or_alloc(struct device *dev) > +{ > + struct iommu_group *iommu_group; > + struct vfio_group *group; > + > + iommu_group = iommu_group_get(dev); > + if (!iommu_group) { > +#ifdef CONFIG_VFIO_NOIOMMU > + /* > + * With noiommu enabled, create an IOMMU group for devices that > + * don't already have one and don't have an iommu_ops on their > + * bus. Taint the kernel because we're about to give a DMA > + * capable device to a user without IOMMU protection. > + */ > + if (noiommu && !iommu_present(dev->bus)) { > + add_taint(TAINT_USER, LOCKDEP_STILL_OK); > + dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n"); > + return vfio_noiommu_group_alloc(dev); Nit, we taint regardless of the success of this function, should we move the tainting back into the function (using the flags to skip for mdev in subsequent patches) or swap the order to check the return value before tainting? Thanks, Alex