vfio: selftests: Incorporate IOVA range info
Subsystems (4)
Alex Williamson
maintainerPATCH 1/1 vfio: selftests: Incorporate IOVA range info
From: Alex Williamson <[email protected]>
Not all IOMMUs support the same virtual address width as the processor, for instance older Intel consumer platforms only support 39-bits of IOMMU address space. On such platforms, using the virtual address as the IOVA and mappings at the top of the address space both fail.
VFIO and IOMMUFD have facilities for retrieving valid IOVA ranges, VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE and IOMMU_IOAS_IOVA_RANGES, respectively. These provide compatible arrays of ranges from which we can construct a simple allocator and record the maximum supported IOVA address.
Use this new allocator in place of reusing the virtual address, and incorporate the maximum supported IOVA into the limit testing. This latter change doesn't test quite the same absolute end-of-address space behavior but still seems to have some value. Testing for overflow is skipped when a reduced address space is supported as the desired errno is not generated.
---
This happened upon another interesting vfio-compat difference for IOMMUFD, native type1 returns the correct set of IOVA ranges after VFIO_SET_IOMMU, vfio-compat requires the next step of calling VFIO_GROUP_GET_DEVICE_FD to attach the device to the IOAS. If checked prior to this, the IOVA range is reported as the full 64-bit address space. ISTR this is known, but it's sufficiently subtle to make note of again.
.../selftests/vfio/lib/include/vfio_util.h | 10 ++ .../selftests/vfio/lib/vfio_pci_device.c | 134 ++++++++++++++++++ .../selftests/vfio/vfio_dma_mapping_test.c | 7 +- .../selftests/vfio/vfio_pci_driver_test.c | 2 +- 4 files changed, 150 insertions(+), 3 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +10 -0
@@ -10,6 +10,8 @@#include "../../../kselftest.h" +#define ALIGN (x, a) (((x) + (a - 1)) & (~((a) - 1))) +#define VFIO_LOG_AND_EXIT (...) do { \ fprintf(stderr, " " __VA_ARGS__); \ fprintf(stderr, "\n"); \
@@ -183,6 +185,12 @@ struct vfio_pci_device {int msi_eventfds[PCI_MSIX_FLAGS_QSIZE +1 ];struct vfio_pci_driver driver; + +int nr_iova_ranges; +struct vfio_iova_range *iova_ranges; +int iova_range_idx; +iova_t iova_next; +iova_t iova_max; };/*
@@ -206,6 +214,8 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_void vfio_pci_device_cleanup (struct vfio_pci_device *device);void vfio_pci_device_reset (struct vfio_pci_device *device); +iova_t vfio_pci_get_next_iova (struct vfio_pci_device *device,size_t size); +int __vfio_pci_dma_map (struct vfio_pci_device *device,struct vfio_dma_region *region); int __vfio_pci_dma_unmap(struct vfio_pci_device *device,
tools/testing/selftests/vfio/lib/vfio_pci_device.c +134 -0
168 lines not on this page · raw message
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +5 -2
@@ -142,7 +142,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)else ASSERT_NE (region.vaddr,MAP_FAILED ); - region.iova = (u64 )region.vaddr; + region.iova =vfio_pci_get_next_iova (self->device, size); region.size = size;vfio_pci_dma_map (self->device, ®ion);
@@ -233,7 +233,7 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)ASSERT_NE (region->vaddr,MAP_FAILED );/* One page prior to the end of address space */ - region->iova = ~(iova_t )0 & ~(region_size -1 ); + region->iova = self->device->iova_max & ~(region_size -1 ); region->size = region_size; }
@@ -276,6 +276,9 @@ TEST_F(vfio_dma_map_limit_test, overflow)struct vfio_dma_region *region = &self->region;int rc; +if (self->device->iova_max !=UINT64_MAX ) +SKIP (return,"IOMMU address space insufficient for overflow test\n" ); + region->size = self->mmap_size; rc =__vfio_pci_dma_map (self->device, region);
tools/testing/selftests/vfio/vfio_pci_driver_test.c +1 -1
@@ -29,7 +29,7 @@ static void region_setup(struct vfio_pci_device *device,VFIO_ASSERT_NE (vaddr,MAP_FAILED ); region->vaddr = vaddr; - region->iova = (u64 )vaddr; + region->iova =vfio_pci_get_next_iova (self->device, size); region->size = size;vfio_pci_dma_map (device, region);
Signature · 1 line
2.51.1
Quoted 23 lines · Alex Williamson
From: Alex Williamson <[email protected]>
Not all IOMMUs support the same virtual address width as the processor, for instance older Intel consumer platforms only support 39-bits of IOMMU address space. On such platforms, using the virtual address as the IOVA and mappings at the top of the address space both fail.
VFIO and IOMMUFD have facilities for retrieving valid IOVA ranges,
VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGEandIOMMU_IOAS_IOVA_RANGES, respectively. These provide compatible arrays of ranges from which we can construct a simple allocator and record the maximum supported IOVA address.Use this new allocator in place of reusing the virtual address, and incorporate the maximum supported IOVA into the limit testing. This latter change doesn't test quite the same absolute end-of-address space behavior but still seems to have some value. Testing for overflow is skipped when a reduced address space is supported as the desired errno is not generated.
Signed-off-by Alex Williamson <[email protected]>---
Quoted 7 lines
This happened upon another interesting vfio-compat difference for IOMMUFD, native type1 returns the correct set of IOVA ranges after
VFIO_SET_IOMMU, vfio-compat requires the next step of callingVFIO_GROUP_GET_DEVICE_FDto attach the device to the IOAS. If checked prior to this, the IOVA range is reported as the full 64-bit address space. ISTR this is known, but it's sufficiently subtle to make note of again.
Maybe we should fail in this in between state rather than give wrong information?
Jason
Alex Williamson
maintainerQuoted 11 lines · On Sat, 8 Nov 2025 18:58:42 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 9 lines · Alex Williamson
---
This happened upon another interesting vfio-compat difference for IOMMUFD, native type1 returns the correct set of IOVA ranges after
VFIO_SET_IOMMU, vfio-compat requires the next step of callingVFIO_GROUP_GET_DEVICE_FDto attach the device to the IOAS. If checked prior to this, the IOVA range is reported as the full 64-bit address space. ISTR this is known, but it's sufficiently subtle to make note of again.
Quoted 2 lines
Maybe we should fail in this in between state rather than give wrong information?
That's probably an improvement, maybe with a WARN_ONCE indicating the ordering requirement for vfio-compat. Thanks,
Alex