The test isn't going to work if a group doesn't exist. Normally this isn't a problem since VFIO isn't going to create a device if there is no group, but the special CONFIG_VFIO_NOIOMMU behavior allows bypassing this prevention. The new cap test effectively forces a group and breaks this config option. Move the cap test to vfio_group_find_or_alloc() which is the earliest time we know we have a group available and thus are not running in noiommu mode.
Unified · Split
| @@ -549,6 +549,16 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev) · 548 unmodified lines | ||
| 549 | 549 | if (!iommu_group) |
| 550 | 550 | return ERR_PTR(-EINVAL); |
| 551 | 551 | |
| 552 | + /* | |
| 553 | + * VFIO always sets IOMMU_CACHE because we offer no way for userspace to | |
| 554 | + * restore cache coherency. It has to be checked here because it is only | |
| 555 | + * valid for cases where we are using iommu groups. | |
| 556 | + */ | |
| 557 | + if (!iommu_capable(dev->bus, IOMMU_CAP_CACHE_COHERENCY)) { | |
| 558 | + iommu_group_put(iommu_group); | |
| 559 | + return ERR_PTR(-EINVAL); | |
| 560 | + } | |
| 561 | + | |
| 552 | 562 | group = vfio_group_get_from_iommu(iommu_group); |
| 553 | 563 | if (!group) |
| 554 | 564 | group = vfio_create_group(iommu_group, VFIO_IOMMU); |
| @@ -601,13 +611,6 @@ static int __vfio_register_dev(struct vfio_device *device, · 46 unmodified lines | ||
| 601 | 611 | |
| 602 | 612 | int vfio_register_group_dev(struct vfio_device *device) |
| 603 | 613 | { |
| 604 | − /* | |
| 605 | − * VFIO always sets IOMMU_CACHE because we offer no way for userspace to | |
| 606 | − * restore cache coherency. | |
| 607 | − */ | |
| 608 | − if (!iommu_capable(device->dev->bus, IOMMU_CAP_CACHE_COHERENCY)) | |
| 609 | − return -EINVAL; | |
| 610 | − | |
| 611 | 614 | return __vfio_register_dev(device, |
| 612 | 615 | vfio_group_find_or_alloc(device->dev)); |
| 613 | 616 | } |