[PATCH 2/4] vfio: selftests: fix map limit tests to use last available iova

Alex Mastro posted 4 patches 3 months ago
There is a newer version of this series
[PATCH 2/4] vfio: selftests: fix map limit tests to use last available iova
Posted by Alex Mastro 3 months ago
Use the newly available vfio_pci_iova_ranges() to determine the last
legal IOVA, and use this as the basis for vfio_dma_map_limit_test tests.

Fixes: de8d1f2fd5a5 ("vfio: selftests: add end of address space DMA map/unmap tests")
Signed-off-by: Alex Mastro <amastro@fb.com>
---
 tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
index 4f1ea79a200c..37c2a342df8d 100644
--- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
+++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
@@ -3,6 +3,8 @@
 #include <sys/mman.h>
 #include <unistd.h>
 
+#include <uapi/linux/types.h>
+#include <linux/iommufd.h>
 #include <linux/limits.h>
 #include <linux/mman.h>
 #include <linux/sizes.h>
@@ -219,7 +221,10 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES();
 FIXTURE_SETUP(vfio_dma_map_limit_test)
 {
 	struct vfio_dma_region *region = &self->region;
+	struct iommu_iova_range *ranges;
 	u64 region_size = getpagesize();
+	iova_t last_iova;
+	size_t nranges;
 
 	/*
 	 * Over-allocate mmap by double the size to provide enough backing vaddr
@@ -232,8 +237,13 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
 			     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
 	ASSERT_NE(region->vaddr, MAP_FAILED);
 
-	/* One page prior to the end of address space */
-	region->iova = ~(iova_t)0 & ~(region_size - 1);
+	ranges = vfio_pci_iova_ranges(self->device, &nranges);
+	VFIO_ASSERT_NOT_NULL(ranges);
+	last_iova = ranges[nranges - 1].last;
+	free(ranges);
+
+	/* One page prior to the last iova */
+	region->iova = last_iova & ~(region_size - 1);
 	region->size = region_size;
 }
 
@@ -276,6 +286,9 @@ TEST_F(vfio_dma_map_limit_test, overflow)
 	struct vfio_dma_region *region = &self->region;
 	int rc;
 
+	if (region->iova != (~(iova_t)0 & ~(region->size - 1)))
+		SKIP(return, "IOMMU address space insufficient for overflow test");
+
 	region->size = self->mmap_size;
 
 	rc = __vfio_pci_dma_map(self->device, region);

-- 
2.47.3
Re: [PATCH 2/4] vfio: selftests: fix map limit tests to use last available iova
Posted by David Matlack 3 months ago
On Mon, Nov 10, 2025 at 1:11 PM Alex Mastro <amastro@fb.com> wrote:
> +       if (region->iova != (~(iova_t)0 & ~(region->size - 1)))
> +               SKIP(return, "IOMMU address space insufficient for overflow test");
> +

If, instead, this was:

  region->iova = ~(iova_t)0 & ~(region->size - 1);

then I think this test could be run on all platforms. The kernel
checks for overflow before it checks for valid iova ranges.
Re: [PATCH 2/4] vfio: selftests: fix map limit tests to use last available iova
Posted by Alex Williamson 3 months ago
On Mon, 10 Nov 2025 13:10:42 -0800
Alex Mastro <amastro@fb.com> wrote:

> Use the newly available vfio_pci_iova_ranges() to determine the last
> legal IOVA, and use this as the basis for vfio_dma_map_limit_test tests.
> 
> Fixes: de8d1f2fd5a5 ("vfio: selftests: add end of address space DMA map/unmap tests")
> Signed-off-by: Alex Mastro <amastro@fb.com>
> ---
>  tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 17 +++++++++++++++--
>  1 file changed, 15 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
> index 4f1ea79a200c..37c2a342df8d 100644
> --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
> +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
> @@ -3,6 +3,8 @@
>  #include <sys/mman.h>
>  #include <unistd.h>
>  
> +#include <uapi/linux/types.h>
> +#include <linux/iommufd.h>
>  #include <linux/limits.h>
>  #include <linux/mman.h>
>  #include <linux/sizes.h>
> @@ -219,7 +221,10 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES();
>  FIXTURE_SETUP(vfio_dma_map_limit_test)
>  {
>  	struct vfio_dma_region *region = &self->region;
> +	struct iommu_iova_range *ranges;
>  	u64 region_size = getpagesize();
> +	iova_t last_iova;
> +	size_t nranges;
>  
>  	/*
>  	 * Over-allocate mmap by double the size to provide enough backing vaddr
> @@ -232,8 +237,13 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
>  			     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
>  	ASSERT_NE(region->vaddr, MAP_FAILED);
>  
> -	/* One page prior to the end of address space */
> -	region->iova = ~(iova_t)0 & ~(region_size - 1);
> +	ranges = vfio_pci_iova_ranges(self->device, &nranges);
> +	VFIO_ASSERT_NOT_NULL(ranges);
> +	last_iova = ranges[nranges - 1].last;


Building on the imposed requirement of ordered ranges.  Thanks,

Alex


> +	free(ranges);
> +
> +	/* One page prior to the last iova */
> +	region->iova = last_iova & ~(region_size - 1);
>  	region->size = region_size;
>  }
>  
> @@ -276,6 +286,9 @@ TEST_F(vfio_dma_map_limit_test, overflow)
>  	struct vfio_dma_region *region = &self->region;
>  	int rc;
>  
> +	if (region->iova != (~(iova_t)0 & ~(region->size - 1)))
> +		SKIP(return, "IOMMU address space insufficient for overflow test");
> +
>  	region->size = self->mmap_size;
>  
>  	rc = __vfio_pci_dma_map(self->device, region);
>
Re: [PATCH 2/4] vfio: selftests: fix map limit tests to use last available iova
Posted by Alex Mastro 3 months ago
On Mon, Nov 10, 2025 at 02:31:52PM -0700, Alex Williamson wrote:
> On Mon, 10 Nov 2025 13:10:42 -0800
> Alex Mastro <amastro@fb.com> wrote:
> 
> > Use the newly available vfio_pci_iova_ranges() to determine the last
> > legal IOVA, and use this as the basis for vfio_dma_map_limit_test tests.
> > 
> > Fixes: de8d1f2fd5a5 ("vfio: selftests: add end of address space DMA map/unmap tests")
> > Signed-off-by: Alex Mastro <amastro@fb.com>
> > ---
> >  tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 17 +++++++++++++++--
> >  1 file changed, 15 insertions(+), 2 deletions(-)
> > 
> > diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
> > index 4f1ea79a200c..37c2a342df8d 100644
> > --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
> > +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
> > @@ -3,6 +3,8 @@
> >  #include <sys/mman.h>
> >  #include <unistd.h>
> >  
> > +#include <uapi/linux/types.h>
> > +#include <linux/iommufd.h>
> >  #include <linux/limits.h>
> >  #include <linux/mman.h>
> >  #include <linux/sizes.h>
> > @@ -219,7 +221,10 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES();
> >  FIXTURE_SETUP(vfio_dma_map_limit_test)
> >  {
> >  	struct vfio_dma_region *region = &self->region;
> > +	struct iommu_iova_range *ranges;
> >  	u64 region_size = getpagesize();
> > +	iova_t last_iova;
> > +	size_t nranges;
> >  
> >  	/*
> >  	 * Over-allocate mmap by double the size to provide enough backing vaddr
> > @@ -232,8 +237,13 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
> >  			     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
> >  	ASSERT_NE(region->vaddr, MAP_FAILED);
> >  
> > -	/* One page prior to the end of address space */
> > -	region->iova = ~(iova_t)0 & ~(region_size - 1);
> > +	ranges = vfio_pci_iova_ranges(self->device, &nranges);
> > +	VFIO_ASSERT_NOT_NULL(ranges);
> > +	last_iova = ranges[nranges - 1].last;
> 
> 
> Building on the imposed requirement of ordered ranges.  Thanks,

Agree. Will keep this code as-is given my plan to explicitly normalize the
ranges to be ordered in the helper.

> 
> Alex
> 
> 
> > +	free(ranges);
> > +
> > +	/* One page prior to the last iova */
> > +	region->iova = last_iova & ~(region_size - 1);
> >  	region->size = region_size;
> >  }
> >  
> > @@ -276,6 +286,9 @@ TEST_F(vfio_dma_map_limit_test, overflow)
> >  	struct vfio_dma_region *region = &self->region;
> >  	int rc;
> >  
> > +	if (region->iova != (~(iova_t)0 & ~(region->size - 1)))
> > +		SKIP(return, "IOMMU address space insufficient for overflow test");
> > +
> >  	region->size = self->mmap_size;
> >  
> >  	rc = __vfio_pci_dma_map(self->device, region);
> > 
>