Skip to contentSkip to conversation

vfio: selftests: Fix MMIO test failures in iommufd compat mode

PATCH In review Yi Lai 3 participants 5 messages Mar 20 – Mar 23

Subsystems (4)
lore
Share & tools
lore

Yi Lai

author
#0 lore raw

cover vfio: selftests: Fix MMIO test failures in iommufd compat mode

This series fixes two issues in the vfio_dma_mapping_mmio_test selftest when running in iommufd compat mode.

The first patch fixes a segmentation fault caused by an uninitialized list head when __iommu_map() fails (as expected for MMIO regions in iommufd).

The second patch fixes a test assertion failure by aligning the check for the __iommu_unmap() return value based on the iommufd mode (native vs. compat).

Yi Lai (2):
vfio: selftests: Fix iommufd compat mode __iommu_unmap() crash
vfio: selftests: Align __iommu_unmap() check with iommufd compat mode

 .../selftests/vfio/vfio_dma_mapping_mmio_test.c        | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 vfio: selftests: Fix iommufd compat mode __iommu_unmap() crash

When running vfio_dma_mapping_mmio_test in MODE_IOMMUFD_COMPAT* mode, the test crashes with "Test terminated unexpectedly by signal 11".

The crash happens because:
1. __iommu_map() fails as expected for MMIO mappings in iommufd. Consequently, the region.link remains uninitialized.
2. The test proceeds to call __iommu_unmap().
3. In iommufd compat mode, the kernel returns 0 for unmapping a non-existent range.
4. __iommu_unmap() calls list_del_init(&region->link), dereferencing the uninitialized pointer.

Fix this by explicitly initializing region.link using INIT_LIST_HEAD.

---
 tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c +1 -0
index 957a89ce7b3a..4f7ecdca0215 100644
@@ -88,6 +88,7 @@ static void do_mmio_map_test(struct iommu *iommu,
 		.vaddr = vaddr,
 		.size = size,
 		.iova = iova_allocator_alloc(iova_allocator, size),
+		.link = LIST_HEAD_INIT(region.link),
 	};
 
 	/*
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/2 vfio: selftests: Align __iommu_unmap() check with iommufd compat mode

In reply to Yi Lai

When __iommu_map() fails (as expected for MMIO in iommufd modes), the test proceeds to call __iommu_unmap() for cleanup.

The behavior of unmapping a non-existent range differs between iommufd native and compat modes:
- Native iommufd returns -ENOENT (failure).
- Compat iommufd returns 0 (success), mimicking legacy VFIO behavior.

The previous code asserted that __iommu_unmap() always fails, which caused test failures in compat mode. Fix this by checking the return value based on the iommufd mode.

---
 .../testing/selftests/vfio/vfio_dma_mapping_mmio_test.c  | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c +8 -1
index 4f7ecdca0215..e6a2b2ff91f0 100644
@@ -101,7 +101,14 @@ static void do_mmio_map_test(struct iommu *iommu,
 		iommu_unmap(iommu, &region);
 	} else {
 		VFIO_ASSERT_NE(__iommu_map(iommu, &region), 0);
-		VFIO_ASSERT_NE(__iommu_unmap(iommu, &region, NULL), 0);
+		/*
+		 * Native IOMMUFD returns -ENOENT and Compat IOMMUFD returns 0
+		 * for unmapping a non-existent range.
+		 */
+		if (!strcmp(iommu->mode->name, MODE_IOMMUFD))
+			VFIO_ASSERT_NE(__iommu_unmap(iommu, &region, NULL), 0);
+		else
+			VFIO_ASSERT_EQ(__iommu_unmap(iommu, &region, NULL), 0);
 	}
 }
 
Signature · 1 line
2.43.0
maintainer
#3 lore raw
In reply to Yi Lai
Quoted 9 lines · Yi Lai

This series fixes two issues in the vfio_dma_mapping_mmio_test selftest when running in iommufd compat mode.

The first patch fixes a segmentation fault caused by an uninitialized list head when __iommu_map() fails (as expected for MMIO regions in iommufd).

The second patch fixes a test assertion failure by aligning the check for the __iommu_unmap() return value based on the iommufd mode (native vs. compat).

Alex Mastro also sent a fix for these issues here:

https://lore.kernel.org/kvm/[email protected]/

Can you take a look and see if it looks ok to you? I think I prefer Alex's fix. We shouldn't even be calling __iommu_unmap() after __iommu_map() fails.

#4 lore raw
Quoted 11 lines · David Matlack
Quoted 9 lines · Yi Lai

This series fixes two issues in the vfio_dma_mapping_mmio_test selftest when running in iommufd compat mode.

The first patch fixes a segmentation fault caused by an uninitialized list head when __iommu_map() fails (as expected for MMIO regions in iommufd).

The second patch fixes a test assertion failure by aligning the check for the __iommu_unmap() return value based on the iommufd mode (native vs. compat).

Quoted 7 lines

Alex Mastro also sent a fix for these issues here:

https://lore.kernel.org/kvm/[email protected]/

Can you take a look and see if it looks ok to you? I think I prefer Alex's fix. We shouldn't even be calling __iommu_unmap() after __iommu_map() fails.

Yes, just remove calling __iommu_unmap() is a more direct fix for the two issues I encoutered. Agree with the fix.