vfio: selftests: Add iommufd_compat_type1{,v2} modes

Add new IOMMU modes for using iommufd in compatibility mode with
VFIO_TYPE1_IOMMU and VFIO_TYPE1v2_IOMMU.

In these modes, VFIO selftests will open /dev/iommu and treats it as a
container FD (as if it had opened /dev/vfio/vfio) and the kernel
translates the container ioctls to iommufd calls transparently.

Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20250822212518.4156428-28-dmatlack@google.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
This commit is contained in:
David Matlack
2025-08-27 12:14:10 -06:00
committed by Alex Williamson
parent 0969c685ba
commit d1a17495bb
3 changed files with 23 additions and 3 deletions
@@ -60,7 +60,9 @@ struct vfio_iommu_mode {
*/
#define FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(...) \
FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1_iommu, ##__VA_ARGS__); \
FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__)
FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__); \
FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1, ##__VA_ARGS__); \
FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1v2, ##__VA_ARGS__)
struct vfio_pci_bar {
struct vfio_region_info info;
@@ -376,6 +376,16 @@ static const struct vfio_iommu_mode iommu_modes[] = {
.container_path = "/dev/vfio/vfio",
.iommu_type = VFIO_TYPE1v2_IOMMU,
},
{
.name = "iommufd_compat_type1",
.container_path = "/dev/iommu",
.iommu_type = VFIO_TYPE1_IOMMU,
},
{
.name = "iommufd_compat_type1v2",
.container_path = "/dev/iommu",
.iommu_type = VFIO_TYPE1v2_IOMMU,
},
};
const char *default_iommu_mode = "vfio_type1_iommu";
@@ -128,6 +128,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
const int flags = variant->mmap_flags;
struct vfio_dma_region region;
struct iommu_mapping mapping;
u64 mapping_size = size;
int rc;
region.vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
@@ -150,6 +151,13 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
if (rc == -EOPNOTSUPP)
goto unmap;
/*
* IOMMUFD compatibility-mode does not support huge mappings when
* using VFIO_TYPE1_IOMMU.
*/
if (!strcmp(variant->iommu_mode, "iommufd_compat_type1"))
mapping_size = SZ_4K;
ASSERT_EQ(0, rc);
printf("Found IOMMU mappings for IOVA 0x%lx:\n", region.iova);
printf("PGD: 0x%016lx\n", mapping.pgd);
@@ -158,7 +166,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
printf("PMD: 0x%016lx\n", mapping.pmd);
printf("PTE: 0x%016lx\n", mapping.pte);
switch (size) {
switch (mapping_size) {
case SZ_4K:
ASSERT_NE(0, mapping.pte);
break;
@@ -172,7 +180,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
ASSERT_NE(0, mapping.pud);
break;
default:
VFIO_FAIL("Unrecognized size: 0x%lx\n", size);
VFIO_FAIL("Unrecognized size: 0x%lx\n", mapping_size);
}
unmap: