vfio_dma_mapping_test currently uses iova=vaddr as part of DMA mapping
validation. The assumption that these IOVAs are legal has held up on all
the hardware we've tested so far, but but is not guaranteed. Make the
test more robust by using iova_allocator to vend IOVAs, which queries
legally accessible IOVAs from the underlying IOMMUFD or VFIO container.

Signed-off-by: Alex Mastro <[email protected]>
---
 tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c 
b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
index 37c2a342df8d..c1a015385b0f 100644
--- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
+++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
@@ -95,6 +95,7 @@ static int iommu_mapping_get(const char *bdf, u64 iova,
 
 FIXTURE(vfio_dma_mapping_test) {
        struct vfio_pci_device *device;
+       struct iova_allocator iova_allocator;
 };
 
 FIXTURE_VARIANT(vfio_dma_mapping_test) {
@@ -118,11 +119,16 @@ 
FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |
 
 FIXTURE_SETUP(vfio_dma_mapping_test)
 {
+       int ret;
+
        self->device = vfio_pci_device_init(device_bdf, variant->iommu_mode);
+       ret = iova_allocator_init(self->device, &self->iova_allocator);
+       VFIO_ASSERT_EQ(ret, 0);
 }
 
 FIXTURE_TEARDOWN(vfio_dma_mapping_test)
 {
+       iova_allocator_deinit(&self->iova_allocator);
        vfio_pci_device_cleanup(self->device);
 }
 
@@ -144,7 +150,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
        else
                ASSERT_NE(region.vaddr, MAP_FAILED);
 
-       region.iova = (u64)region.vaddr;
+       region.iova = iova_allocator_alloc(&self->iova_allocator, size);
        region.size = size;
 
        vfio_pci_dma_map(self->device, &region);

-- 
2.47.3


Reply via email to