vfio: selftests: Add vfio_dma_mapping_mmio_test
Subsystems (4)
PATCH 1/1 vfio: selftests: Add vfio_dma_mapping_mmio_test
Test MMIO-backed DMA mappings by iommu_map()-ing mmap'ed BAR regions. Also update vfio_pci_bar_map() to align BAR mmaps for efficient huge page mappings.
Only vfio_type1 variants are tested; iommufd variants can be added once kernel support lands. The manual mmap alignment can be removed once mmap(!MAP_FIXED) on vfio device fds improves to automatically return well-aligned addresses.
---
Sanity test run:
$ ./vfio_dma_mapping_mmio_test 0000:05:00.0 TAP version 13 1..4 # Starting 4 tests from 2 test cases. # RUN vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_full_bar ... Mapping BAR4: vaddr=0x7fad40000000 size=0x2000000000 iova=0x2000000000 # OK vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_full_bar ok 1 vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_full_bar # RUN vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_partial_bar ... Mapping BAR4 (partial): vaddr=0x7fad40000000 size=0x1000 iova=0x0 # OK vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_partial_bar ok 2 vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_partial_bar # RUN vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_full_bar ... Mapping BAR4: vaddr=0x7fad40000000 size=0x2000000000 iova=0x2000000000 # OK vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_full_bar ok 3 vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_full_bar # RUN vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_partial_bar ... Mapping BAR4 (partial): vaddr=0x7fad40000000 size=0x1000 iova=0x0 # OK vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_partial_bar ok 4 vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_partial_bar # PASSED: 4 / 4 tests passed. # Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0 --- tools/testing/selftests/vfio/Makefile | 1 + tools/testing/selftests/vfio/lib/vfio_pci_device.c | 28 ++++- .../selftests/vfio/vfio_dma_mapping_mmio_test.c | 132 +++++++++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/Makefile +1 -0
@@ -1,5 +1,6 @@ CFLAGS = $(KHDR_INCLUDES) TEST_GEN_PROGS += vfio_dma_mapping_test +TEST_GEN_PROGS += vfio_dma_mapping_mmio_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_device_init_perf_test
tools/testing/selftests/vfio/lib/vfio_pci_device.c +27 -1
@@ -12,10 +12,13 @@#include <sys/mman.h> #include <uapi/linux/types.h> +#include <linux/align.h> #include <linux/iommufd.h> +#include <linux/kernel.h> #include <linux/limits.h> #include <linux/mman.h> #include <linux/overflow.h> +#include <linux/sizes.h> #include <linux/types.h> #include <linux/vfio.h>
@@ -124,20 +127,43 @@ static void vfio_pci_region_get(struct vfio_pci_device *device, int index,static void vfio_pci_bar_map (struct vfio_pci_device *device,int index) {struct vfio_pci_bar *bar = &device->bars[index]; +size_t align, size; +void *map_base, *map_align;int prot =0 ;VFIO_ASSERT_LT (index,PCI_STD_NUM_BARS );VFIO_ASSERT_NULL (bar->vaddr);VFIO_ASSERT_TRUE (bar->info.flags &VFIO_REGION_INFO_FLAG_MMAP ); +VFIO_ASSERT_GT (bar->info.size,0 );if (bar->info.flags &VFIO_REGION_INFO_FLAG_READ ) prot |=PROT_READ ;if (bar->info.flags &VFIO_REGION_INFO_FLAG_WRITE ) prot |=PROT_WRITE ; - bar->vaddr =mmap (NULL , bar->info.size, prot,MAP_FILE |MAP_SHARED , +/* +* Align the mmap for more efficient IOMMU mapping. +* The largest PUD size supporting huge pfnmap is 1GiB. +*/ + size = bar->info.size; + align =min_t (u64,1ULL <<__builtin_ctzll (size),SZ_1G ); + + map_base =mmap (NULL , size + align,PROT_NONE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +VFIO_ASSERT_NE (map_base,MAP_FAILED ); + + map_align = (void *)ALIGN ((uintptr_t )map_base, align); + +if (map_align > map_base) +munmap (map_base, map_align - map_base); +if (align > (size_t )(map_align - map_base)) +munmap (map_align + size, align - (map_align - map_base)); + + bar->vaddr =mmap (map_align, size, prot,MAP_SHARED |MAP_FIXED , device->fd, bar->info.offset);VFIO_ASSERT_NE (bar->vaddr,MAP_FAILED ); + +madvise (bar->vaddr, size,MADV_HUGEPAGE ); }static void vfio_pci_bar_unmap (struct vfio_pci_device *device,int index)
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +132 -0
132 lines not on this page · raw message
---
base-commit: d721f52e31553a848e0e9947ca15a49c5674aef3
change-id: 20260107-scratch-amastro-vfio-dma-mapping-mmio-test-eecf25d9a742
Best regards,
Signature · 1 line
Alex Mastro <[email protected]>
Quoted 25 lines · Alex Mastro
tools/testing/selftests/vfio/lib/vfio_pci_device.c +9
-1@@ -124,20 +127,43 @@ static void vfio_pci_region_get(struct vfio_pci_device *device, int index,static void vfio_pci_bar_map (struct vfio_pci_device *device,int index) {struct vfio_pci_bar *bar = &device->bars[index]; +size_t align, size; +void *map_base, *map_align;int prot =0 ;VFIO_ASSERT_LT (index,PCI_STD_NUM_BARS );VFIO_ASSERT_NULL (bar->vaddr);VFIO_ASSERT_TRUE (bar->info.flags &VFIO_REGION_INFO_FLAG_MMAP ); +VFIO_ASSERT_GT (bar->info.size,0 );if (bar->info.flags &VFIO_REGION_INFO_FLAG_READ ) prot |=PROT_READ ;if (bar->info.flags &VFIO_REGION_INFO_FLAG_WRITE ) prot |=PROT_WRITE ; - bar->vaddr = mmap(NULL, bar->info.size, prot, MAP_FILE | MAP_SHARED, +/* +* Align the mmap for more efficient IOMMU mapping. +* The largest PUD size supporting huge pfnmap is 1GiB. +*/ + size = bar->info.size; + align =min_t (u64,1ULL <<__builtin_ctzll (size),SZ_1G );
tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_region_get(struct vfio_pci_device *device, int index, · line 159
Quoted 10 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +10
-0+ + map_base =mmap (NULL , size + align,PROT_NONE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +VFIO_ASSERT_NE (map_base,MAP_FAILED ); + + map_align = (void *)ALIGN ((uintptr_t )map_base, align); + +if (map_align > map_base) +munmap (map_base, map_align - map_base); +if (align > (size_t )(map_align - map_base))
I realized that this is tautological. Will fix in v2.
Quoted 8 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +5
-0+munmap (map_align + size, align - (map_align - map_base)); + + bar->vaddr =mmap (map_align, size, prot,MAP_SHARED |MAP_FIXED , device->fd, bar->info.offset);VFIO_ASSERT_NE (bar->vaddr,MAP_FAILED ); + +madvise (bar->vaddr, size,MADV_HUGEPAGE ); }
David Matlack
maintainerQuoted 1 line · Alex Mastro
Test MMIO-backed DMA mappings by iommu_map()-ing mmap'ed BAR regions.
Thanks for adding this!
Quoted 5 lines
Also update
vfio_pci_bar_map()to align BAR mmaps for efficient huge page mappings.Only
vfio_type1variants are tested; iommufd variants can be added once kernel support lands.
Are there plans to support mapping BARs via virtual address in iommufd? I thought the plan was only to support via dma-bufs. Maybe Jason can confirm.
Assuming not, should we add negative tests here to make sure iommufd does not allow mapping BARs?
And then we can add dma-buf tests in a future commit.
Quoted 80 lines
The manual mmap alignment can be removed
oncemmap(!MAP_FIXED)on vfio device fds improves to automatically return well-aligned addresses.Signed-off-by Alex Mastro <[email protected]>---
Sanity test run:$ ./vfio_dma_mapping_mmio_test 0000:05:00.0 TAP version 13 1..4 # Starting 4 tests from 2 test cases. # RUN vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_full_bar ... Mapping BAR4: vaddr=0x7fad40000000 size=0x2000000000 iova=0x2000000000 # OK vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_full_bar ok 1 vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_full_bar # RUN vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_partial_bar ... Mapping BAR4 (partial): vaddr=0x7fad40000000 size=0x1000 iova=0x0 # OK vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_partial_bar ok 2 vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_partial_bar # RUN vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_full_bar ... Mapping BAR4: vaddr=0x7fad40000000 size=0x2000000000 iova=0x2000000000 # OK vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_full_bar ok 3 vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_full_bar # RUN vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_partial_bar ... Mapping BAR4 (partial): vaddr=0x7fad40000000 size=0x1000 iova=0x0 # OK vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_partial_bar ok 4 vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_partial_bar # PASSED: 4 / 4 tests passed. # Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0 --- tools/testing/selftests/vfio/Makefile | 1 + tools/testing/selftests/vfio/lib/vfio_pci_device.c | 28 ++++- .../selftests/vfio/vfio_dma_mapping_mmio_test.c | 132 +++++++++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-)tools/testing/selftests/vfio/Makefile +1
-0@@ -1,5 +1,6 @@ CFLAGS = $(KHDR_INCLUDES) TEST_GEN_PROGS += vfio_dma_mapping_test +TEST_GEN_PROGS += vfio_dma_mapping_mmio_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_device_init_perf_testtools/testing/selftests/vfio/lib/vfio_pci_device.c +6
-0@@ -12,10 +12,13 @@#include <sys/mman.h> #include <uapi/linux/types.h> +#include <linux/align.h> #include <linux/iommufd.h> +#include <linux/kernel.h> #include <linux/limits.h> #include <linux/mman.h> #include <linux/overflow.h> +#include <linux/sizes.h> #include <linux/types.h> #include <linux/vfio.h> @@ -124,20 +127,43 @@ static void vfio_pci_region_get(struct vfio_pci_device *device, int index,static void vfio_pci_bar_map (struct vfio_pci_device *device,int index) {struct vfio_pci_bar *bar = &device->bars[index]; +size_t align, size; +void *map_base, *map_align;int prot =0 ;VFIO_ASSERT_LT (index,PCI_STD_NUM_BARS );VFIO_ASSERT_NULL (bar->vaddr);VFIO_ASSERT_TRUE (bar->info.flags &VFIO_REGION_INFO_FLAG_MMAP ); +VFIO_ASSERT_GT (bar->info.size,0 );if (bar->info.flags &VFIO_REGION_INFO_FLAG_READ ) prot |=PROT_READ ;
tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_region_get(struct vfio_pci_device *device, int index, · line 149
Quoted 10 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +6
-1if (bar->info.flags &VFIO_REGION_INFO_FLAG_WRITE ) prot |=PROT_WRITE ; - bar->vaddr = mmap(NULL, bar->info.size, prot, MAP_FILE | MAP_SHARED, +/* +* Align the mmap for more efficient IOMMU mapping. +* The largest PUD size supporting huge pfnmap is 1GiB. +*/ + size = bar->info.size; + align =min_t (u64,1ULL <<__builtin_ctzll (size),SZ_1G );
What's the reason to align to 1ULL << __builtin_ctzll(size) and not just size?
Quoted 8 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +8
-0+ + map_base =mmap (NULL , size + align,PROT_NONE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +VFIO_ASSERT_NE (map_base,MAP_FAILED ); + + map_align = (void *)ALIGN ((uintptr_t )map_base, align); + +if (map_align > map_base)
tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_region_get(struct vfio_pci_device *device, int index, · line 167
Quoted 10 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +7
-0+munmap (map_base, map_align - map_base); +if (align > (size_t )(map_align - map_base)) +munmap (map_align + size, align - (map_align - map_base)); + + bar->vaddr =mmap (map_align, size, prot,MAP_SHARED |MAP_FIXED , device->fd, bar->info.offset);VFIO_ASSERT_NE (bar->vaddr,MAP_FAILED ); + +madvise (bar->vaddr, size,MADV_HUGEPAGE ); }
Can you split these changes out into a precursor commit? I think they stand on their own.
Quoted 27 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +0
-0static void vfio_pci_bar_unmap (struct vfio_pci_device *device,int index)tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +19
-0@@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <stdio.h> +#include <sys/mman.h> +#include <unistd.h> + +#include <uapi/linux/types.h> +#include <linux/pci_regs.h> +#include <linux/sizes.h> +#include <linux/vfio.h> + +#include <libvfio.h> + +#include "../kselftest_harness.h" + +static const char *device_bdf; + +static int largest_mapped_bar (struct vfio_pci_device *device) +{ +int bar_idx =-1 ;
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c line 29
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +10
-0+u64 bar_size =0 ; + +for (int i =0 ; i <PCI_STD_NUM_BARS ; i++) { +struct vfio_pci_bar *bar = &device->bars[i]; + +if (!bar->vaddr) +continue ; + +if (!(bar->info.flags &VFIO_REGION_INFO_FLAG_WRITE )) +continue ;
nit: Add a comment here. I assume this is because iommu_map() tries to create writable IOMMU mappings?
Speaking of, maybe we can add a test that creating writable IOMMU mappings fails for read-only BARs?
Quoted 15 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +15
-0+ +if (bar->info.size > bar_size) { + bar_size = bar->info.size; + bar_idx = i; + } + } + +return bar_idx; +} + +FIXTURE (vfio_dma_mapping_mmio_test) { +struct iommu *iommu; +struct vfio_pci_device *device; +struct iova_allocator *iova_allocator; +int bar_idx;
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c line 54
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +10
-0+}; + +FIXTURE_VARIANT (vfio_dma_mapping_mmio_test) { +const char *iommu_mode; +}; + +#define FIXTURE_VARIANT_ADD_IOMMU_MODE (_iommu_mode) \ +FIXTURE_VARIANT_ADD(vfio_dma_mapping_mmio_test, _iommu_mode) { \ + .iommu_mode = #_iommu_mode, \ +}
nit: Alignment of trailing backslashes is off.
Quoted 18 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +18
-0+ +FIXTURE_VARIANT_ADD_IOMMU_MODE (vfio_type1_iommu); +FIXTURE_VARIANT_ADD_IOMMU_MODE (vfio_type1v2_iommu); + +#undef FIXTURE_VARIANT_ADD_IOMMU_MODE + +FIXTURE_SETUP (vfio_dma_mapping_mmio_test) +{ + self->iommu =iommu_init (variant->iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); + self->iova_allocator =iova_allocator_init (self->iommu); + self->bar_idx =largest_mapped_bar (self->device); +} + +FIXTURE_TEARDOWN (vfio_dma_mapping_mmio_test) +{ +iova_allocator_cleanup (self->iova_allocator); +vfio_pci_device_cleanup (self->device);
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c line 82
Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +10
-0+iommu_cleanup (self->iommu); +} + +TEST_F(vfio_dma_mapping_mmio_test, map_full_bar) +{ +struct vfio_pci_bar *bar; +struct dma_region region; + +if (self->bar_idx <0 ) +SKIP (return,"No mappable BAR found on device %s" , device_bdf);
I think you can do this in the FIXTURE_SETUP() to avoid duplication.
Quoted 59 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +50
-1+ + bar = &self->device->bars[self->bar_idx]; + + region = (struct dma_region ) { + .vaddr = bar->vaddr, + .size = bar->info.size, + .iova =iova_allocator_alloc (self->iova_allocator, bar->info.size), + }; + +printf ("Mapping BAR%d: vaddr=%p size=0x%lx iova=0x%lx\n" , + self->bar_idx, region.vaddr, region.size, region.iova); + +iommu_map (self->iommu, ®ion); +iommu_unmap (self->iommu, ®ion); +} + +TEST_F(vfio_dma_mapping_mmio_test, map_partial_bar) +{ +struct vfio_pci_bar *bar; +struct dma_region region; +size_t page_size; + +if (self->bar_idx <0 ) +SKIP (return,"No mappable BAR found on device %s" , device_bdf); + + bar = &self->device->bars[self->bar_idx]; + page_size =getpagesize (); + +if (bar->info.size <2 * page_size) +SKIP (return,"BAR%d too small for partial mapping test (size=0x%llx)" , + self->bar_idx, bar->info.size); + + region = (struct dma_region ) { + .vaddr = bar->vaddr, + .size = page_size, + .iova =iova_allocator_alloc (self->iova_allocator, page_size), + }; + +printf ("Mapping BAR%d (partial): vaddr=%p size=0x%lx iova=0x%lx\n" , + self->bar_idx, region.vaddr, region.size, region.iova); + +iommu_map (self->iommu, ®ion); +iommu_unmap (self->iommu, ®ion); +} + +int main (int argc,char *argv[]) +{ + device_bdf =vfio_selftests_get_bdf (&argc, argv); +return test_harness_run (argc, argv); +} ---base-commit:
d721f52e31553a848e0e9947ca15a49c5674aef3
change-id: 20260107-scratch-amastro-vfio-dma-mapping-mmio-test-eecf25d9a742Best regards,
Signature · 1 line
Alex Mastro <[email protected]>
Quoted 8 lines · David Matlack
Quoted 1 line · Alex Mastro
Test MMIO-backed DMA mappings by iommu_map()-ing mmap'ed BAR regions.
Thanks for adding this!
Quoted 3 lines
Also update
vfio_pci_bar_map()to align BAR mmaps for efficient huge page mappings.
Quoted 6 lines
Quoted 2 lines
Only
vfio_type1variants are tested; iommufd variants can be added once kernel support lands.Are there plans to support mapping BARs via virtual address in iommufd? I thought the plan was only to support via dma-bufs. Maybe Jason can confirm.
Only dmabuf.
Quoted 2 lines
Assuming not, should we add negative tests here to make sure iommufd does not allow mapping BARs?
Yes
Quoted 1 line
And then we can add dma-buf tests in a future commit.
Yes
Jason
Quoted 9 lines · Jason Gunthorpe
Quoted 8 lines · David Matlack
Quoted 1 line · Alex Mastro
Test MMIO-backed DMA mappings by iommu_map()-ing mmap'ed BAR regions.
Thanks for adding this!
Quoted 3 lines
Also update
vfio_pci_bar_map()to align BAR mmaps for efficient huge page mappings.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Only
vfio_type1variants are tested; iommufd variants can be added once kernel support lands.Are there plans to support mapping BARs via virtual address in iommufd? I thought the plan was only to support via dma-bufs. Maybe Jason can confirm.
Only dmabuf.
Ack. I got confused. I had thought iommufd's vfio container compatibility mode was going to support this, but realized that doesn't make sense given past discussions about the pitfalls of achieving these mappings the legacy way.
Quoted 5 lines
Quoted 3 lines
Assuming not, should we add negative tests here to make sure iommufd does not allow mapping BARs?
Yes
Will add.
Quoted 6 lines
Quoted 2 lines
And then we can add dma-buf tests in a future commit.
Yes
Jason
Quoted 17 lines · Alex Mastro
Quoted 16 lines · Jason Gunthorpe
Quoted 14 lines · David Matlack
Quoted 1 line · Alex Mastro
Test MMIO-backed DMA mappings by iommu_map()-ing mmap'ed BAR regions.
Thanks for adding this!
Quoted 5 lines
Also update
vfio_pci_bar_map()to align BAR mmaps for efficient huge page mappings.Only
vfio_type1variants are tested; iommufd variants can be added once kernel support lands.Are there plans to support mapping BARs via virtual address in iommufd? I thought the plan was only to support via dma-bufs. Maybe Jason can confirm.
Quoted 5 lines
Quoted 1 line
Only dmabuf.
Ack. I got confused. I had thought iommufd's vfio container compatibility mode was going to support this, but realized that doesn't make sense given past discussions about the pitfalls of achieving these mappings the legacy way.
Oh, I was thinking about a compatability only flow only in the type 1 emulation that internally magically converts a VMA to a dmabuf, but I haven't written anything.. It is a bit tricky and the type 1 emulation has not been as popular as I expected??
Jason
Alex Williamson
maintainerQuoted 20 lines · On Thu, 8 Jan 2026 10:10:44 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 19 lines · Alex Mastro
Quoted 17 lines · Jason Gunthorpe
Quoted 8 lines · David Matlack
Quoted 1 line · Alex Mastro
Test MMIO-backed DMA mappings by iommu_map()-ing mmap'ed BAR regions.
Thanks for adding this!
Quoted 3 lines
Also update
vfio_pci_bar_map()to align BAR mmaps for efficient huge page mappings.Quoted 6 lines
Quoted 2 lines
Only
vfio_type1variants are tested; iommufd variants can be added once kernel support lands.Are there plans to support mapping BARs via virtual address in iommufd? I thought the plan was only to support via dma-bufs. Maybe Jason can confirm.
Only dmabuf.
Quoted 8 lines
Quoted 3 lines
Ack. I got confused. I had thought iommufd's vfio container compatibility mode was going to support this, but realized that doesn't make sense given past discussions about the pitfalls of achieving these mappings the legacy way.
Oh, I was thinking about a compatability only flow only in the type 1 emulation that internally magically converts a VMA to a dmabuf, but I haven't written anything.. It is a bit tricky and the type 1 emulation has not been as popular as I expected??
In part because of this gap, I'd guess. Thanks,
Alex
David Matlack
maintainerQuoted 28 lines · Alex Williamson
Quoted 24 lines · On Thu, 8 Jan 2026 10:10:44 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 22 lines · Alex Mastro
Quoted 9 lines · Jason Gunthorpe
Quoted 8 lines · David Matlack
Quoted 1 line · Alex Mastro
Test MMIO-backed DMA mappings by iommu_map()-ing mmap'ed BAR regions.
Thanks for adding this!
Quoted 3 lines
Also update
vfio_pci_bar_map()to align BAR mmaps for efficient huge page mappings.Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Only
vfio_type1variants are tested; iommufd variants can be added once kernel support lands.Are there plans to support mapping BARs via virtual address in iommufd? I thought the plan was only to support via dma-bufs. Maybe Jason can confirm.
Only dmabuf.
Ack. I got confused. I had thought iommufd's vfio container compatibility mode was going to support this, but realized that doesn't make sense given past discussions about the pitfalls of achieving these mappings the legacy way.
Quoted 6 lines
Quoted 4 lines
Oh, I was thinking about a compatability only flow only in the type 1 emulation that internally magically converts a VMA to a dmabuf, but I haven't written anything.. It is a bit tricky and the type 1 emulation has not been as popular as I expected??
In part because of this gap, I'd guess. Thanks,
Lack of huge mappings in the IOMMU when using VFIO_TYPE1_IOMMU is another gap I'm aware of. vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap fails when IOMMUFD_VFIO_CONTAINER is enabled.
Is the plan to address all the gaps so IOMMUFD_VFIO_CONTAINER can be made the default and the type1 code can be dropped from the upstream kernel?
Quoted 11 lines · David Matlack
Quoted 6 lines
Quoted 4 lines
Oh, I was thinking about a compatability only flow only in the type 1 emulation that internally magically converts a VMA to a dmabuf, but I haven't written anything.. It is a bit tricky and the type 1 emulation has not been as popular as I expected??
In part because of this gap, I'd guess. Thanks,
Lack of huge mappings in the IOMMU when using
VFIO_TYPE1_IOMMUis another gap I'm aware of. vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap fails whenIOMMUFD_VFIO_CONTAINERis enabled.
What is this? I'm not aware of it..
Quoted 3 lines
Is the plan to address all the gaps so
IOMMUFD_VFIO_CONTAINERcan be made the default and the type1 code can be dropped from the upstream kernel?
This was a dream for sure, distros can decide if they want to continue to support both or have an option to do just one.
Jason
David Matlack
maintainerQuoted 6 lines · Jason Gunthorpe
Quoted 5 lines · David Matlack
Quoted 5 lines
Quoted 4 lines
Oh, I was thinking about a compatability only flow only in the type 1 emulation that internally magically converts a VMA to a dmabuf, but I haven't written anything.. It is a bit tricky and the type 1 emulation has not been as popular as I expected??
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
In part because of this gap, I'd guess. Thanks,
Lack of huge mappings in the IOMMU when using
VFIO_TYPE1_IOMMUis another gap I'm aware of. vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap fails whenIOMMUFD_VFIO_CONTAINERis enabled.What is this? I'm not aware of it..
It's one of the test cases within tools/testing/selftests/vfio/vfio_dma_mapping_test.c.
Here's the output when running with CONFIG_IOMMUFD_VFIO_CONTAINER=y:
# RUN vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap ...
Mapped HVA 0x7f0480000000 (size 0x40000000) at IOVA 0x0
Searching for IOVA 0x0 in /sys/kernel/debug/iommu/intel/0000:6a:01.0/domain_translation_struct Found IOMMU mappings for IOVA 0x0:
PGD: 0x0000000203475027
P4D: 0x0000000203476027
PUD: 0x0000000203477027
PMD: 0x00000001e7562027
PTE: 0x00000041c0000067
# tools/testing/selftests/vfio/vfio_dma_mapping_test.c:188:dma_map_unmap:Expected 0 (0) == mapping.pte (282394099815) # dma_map_unmap: Test terminated by assertion
# FAIL vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap
Quoted 21 lines · David Matlack
Quoted 6 lines · Jason Gunthorpe
Quoted 5 lines · David Matlack
Quoted 5 lines
Quoted 4 lines
Oh, I was thinking about a compatability only flow only in the type 1 emulation that internally magically converts a VMA to a dmabuf, but I haven't written anything.. It is a bit tricky and the type 1 emulation has not been as popular as I expected??
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
In part because of this gap, I'd guess. Thanks,
Lack of huge mappings in the IOMMU when using
VFIO_TYPE1_IOMMUis another gap I'm aware of. vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap fails whenIOMMUFD_VFIO_CONTAINERis enabled.What is this? I'm not aware of it..
It's one of the test cases within
tools/testing/selftests/vfio/vfio_dma_mapping_test.c.Here's the output when running with
CONFIG_IOMMUFD_VFIO_CONTAINER=y:
Quoted 12 lines
# RUN vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap ...
Mapped HVA0x7f0480000000(size0x40000000) at IOVA0x0
Searching for IOVA0x0in /sys/kernel/debug/iommu/intel/0000:6a:01.0/domain_translation_struct Found IOMMU mappings for IOVA0x0:
PGD:0x0000000203475027
P4D:0x0000000203476027
PUD:0x0000000203477027
PMD:0x00000001e7562027
PTE:0x00000041c0000067
# tools/testing/selftests/vfio/vfio_dma_mapping_test.c:188:dma_map_unmap:Expected 0 (0) == mapping.pte (282394099815) #dma_map_unmap: Test terminated by assertion
# FAIL vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap
I can't think of any reason this would fail, I think your tests have found a real bug?? Can you check into it, what kernel call fails and where does the kernel code come from?
I don't think I can run these tests with the HW I have??
Jason
David Matlack
maintainerQuoted 35 lines · Jason Gunthorpe
Quoted 21 lines · David Matlack
Quoted 6 lines · Jason Gunthorpe
Quoted 5 lines · David Matlack
Quoted 5 lines
Quoted 4 lines
Oh, I was thinking about a compatability only flow only in the type 1 emulation that internally magically converts a VMA to a dmabuf, but I haven't written anything.. It is a bit tricky and the type 1 emulation has not been as popular as I expected??
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
In part because of this gap, I'd guess. Thanks,
Lack of huge mappings in the IOMMU when using
VFIO_TYPE1_IOMMUis another gap I'm aware of. vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap fails whenIOMMUFD_VFIO_CONTAINERis enabled.What is this? I'm not aware of it..
It's one of the test cases within
tools/testing/selftests/vfio/vfio_dma_mapping_test.c.Here's the output when running with
CONFIG_IOMMUFD_VFIO_CONTAINER=y:Quoted 12 lines
# RUN vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap ...
Mapped HVA0x7f0480000000(size0x40000000) at IOVA0x0
Searching for IOVA0x0in /sys/kernel/debug/iommu/intel/0000:6a:01.0/domain_translation_struct Found IOMMU mappings for IOVA0x0:
PGD:0x0000000203475027
P4D:0x0000000203476027
PUD:0x0000000203477027
PMD:0x00000001e7562027
PTE:0x00000041c0000067
# tools/testing/selftests/vfio/vfio_dma_mapping_test.c:188:dma_map_unmap:Expected 0 (0) == mapping.pte (282394099815) #dma_map_unmap: Test terminated by assertion
# FAIL vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap
Quoted 3 lines
I can't think of any reason this would fail, I think your tests have found a real bug?? Can you check into it, what kernel call fails and where does the kernel code come from?
Oh I thought it was by design. This code in iommufd_vfio_set_iommu():
/*
* The difference between TYPE1 and TYPE1v2 is the ability to unmap in
* the middle of mapped ranges. This is complicated by huge page support
* which creates single large IOPTEs that cannot be split by the iommu
* driver. TYPE1 is very old at this point and likely nothing uses it,
* however it is simple enough to emulate by simply disabling the
* problematic large IOPTEs. Then we can safely unmap within any range. */
if (type == VFIO_TYPE1_IOMMU)
rc = iopt_disable_large_pages(&ioas->iopt);
git-blame says some guy named Jason Gunthorpe wrote it :P
Quoted 2 lines
I don't think I can run these tests with the HW I have??
FWIW all you need any PCI device that can be bound to vfio-pci and mapped by VT-d. This test does not rely on any of the VFIO selftests drivers to trigger DMA.
Quoted 53 lines · David Matlack
Quoted 35 lines · Jason Gunthorpe
Quoted 21 lines · David Matlack
Quoted 6 lines · Jason Gunthorpe
Quoted 5 lines · David Matlack
Quoted 5 lines
Quoted 4 lines
Oh, I was thinking about a compatability only flow only in the type 1 emulation that internally magically converts a VMA to a dmabuf, but I haven't written anything.. It is a bit tricky and the type 1 emulation has not been as popular as I expected??
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
In part because of this gap, I'd guess. Thanks,
Lack of huge mappings in the IOMMU when using
VFIO_TYPE1_IOMMUis another gap I'm aware of. vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap fails whenIOMMUFD_VFIO_CONTAINERis enabled.What is this? I'm not aware of it..
It's one of the test cases within
tools/testing/selftests/vfio/vfio_dma_mapping_test.c.Here's the output when running with
CONFIG_IOMMUFD_VFIO_CONTAINER=y:Quoted 12 lines
# RUN vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap ...
Mapped HVA0x7f0480000000(size0x40000000) at IOVA0x0
Searching for IOVA0x0in /sys/kernel/debug/iommu/intel/0000:6a:01.0/domain_translation_struct Found IOMMU mappings for IOVA0x0:
PGD:0x0000000203475027
P4D:0x0000000203476027
PUD:0x0000000203477027
PMD:0x00000001e7562027
PTE:0x00000041c0000067
# tools/testing/selftests/vfio/vfio_dma_mapping_test.c:188:dma_map_unmap:Expected 0 (0) == mapping.pte (282394099815) #dma_map_unmap: Test terminated by assertion
# FAIL vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmapQuoted 3 lines
I can't think of any reason this would fail, I think your tests have found a real bug?? Can you check into it, what kernel call fails and where does the kernel code come from?
Oh I thought it was by design. This code in
iommufd_vfio_set_iommu():/*
* The difference between TYPE1 and TYPE1v2 is the ability to unmap in
* the middle of mapped ranges. This is complicated by huge page support
* which creates single large IOPTEs that cannot be split by the iommu
* driver. TYPE1 is very old at this point and likely nothing uses it,
* however it is simple enough to emulate by simply disabling the
* problematic large IOPTEs. Then we can safely unmap within any range. */
if (type ==VFIO_TYPE1_IOMMU)
rc =iopt_disable_large_pages(&ioas->iopt);
Quoted 1 line
git-blame says some guy named Jason Gunthorpe wrote it :P
Er, maybe I mis understood the output then?
This is not a "failure" though, the map succeeded and gave a small page mapping.
This is not reflecting a bug in iommufd but a bug in the TYPE1 support in VFIO itself because it definitely cannot maintain the required unmap anywhere semantic if it mapped in a 1G huge page like this.
Basically, if you are mapping with TYPE1 mode then this should be triggered:
if (!strcmp(variant->iommu_mode, "iommufd_compat_type1"))
mapping_size = SZ_4K;And VFIO should be the one to fail, not iommufd.
If you really want to test TYPE1 you need to test what makes it unique, which is that you can map any VMA and then unmap any slice of it. Including within what should otherwise be a 1G page.
But I doubt anyone cares enough to fix this, so just exclude VFIO_TYPE1_IOMMU from this test?
Jason
David Matlack
maintainerQuoted 71 lines · Jason Gunthorpe
Quoted 53 lines · David Matlack
Quoted 35 lines · Jason Gunthorpe
Quoted 21 lines · David Matlack
Quoted 6 lines · Jason Gunthorpe
Quoted 5 lines · David Matlack
Quoted 5 lines
Quoted 4 lines
Oh, I was thinking about a compatability only flow only in the type 1 emulation that internally magically converts a VMA to a dmabuf, but I haven't written anything.. It is a bit tricky and the type 1 emulation has not been as popular as I expected??
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
In part because of this gap, I'd guess. Thanks,
Lack of huge mappings in the IOMMU when using
VFIO_TYPE1_IOMMUis another gap I'm aware of. vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap fails whenIOMMUFD_VFIO_CONTAINERis enabled.What is this? I'm not aware of it..
It's one of the test cases within
tools/testing/selftests/vfio/vfio_dma_mapping_test.c.Here's the output when running with
CONFIG_IOMMUFD_VFIO_CONTAINER=y:Quoted 12 lines
# RUN vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmap ...
Mapped HVA0x7f0480000000(size0x40000000) at IOVA0x0
Searching for IOVA0x0in /sys/kernel/debug/iommu/intel/0000:6a:01.0/domain_translation_struct Found IOMMU mappings for IOVA0x0:
PGD:0x0000000203475027
P4D:0x0000000203476027
PUD:0x0000000203477027
PMD:0x00000001e7562027
PTE:0x00000041c0000067
# tools/testing/selftests/vfio/vfio_dma_mapping_test.c:188:dma_map_unmap:Expected 0 (0) == mapping.pte (282394099815) #dma_map_unmap: Test terminated by assertion
# FAIL vfio_dma_mapping_test.vfio_type1_iommu_anonymous_hugetlb_1gb.dma_map_unmapQuoted 3 lines
I can't think of any reason this would fail, I think your tests have found a real bug?? Can you check into it, what kernel call fails and where does the kernel code come from?
Oh I thought it was by design. This code in
iommufd_vfio_set_iommu():/*
* The difference between TYPE1 and TYPE1v2 is the ability to unmap in
* the middle of mapped ranges. This is complicated by huge page support
* which creates single large IOPTEs that cannot be split by the iommu
* driver. TYPE1 is very old at this point and likely nothing uses it,
* however it is simple enough to emulate by simply disabling the
* problematic large IOPTEs. Then we can safely unmap within any range. */
if (type ==VFIO_TYPE1_IOMMU)
rc =iopt_disable_large_pages(&ioas->iopt);Quoted 1 line
git-blame says some guy named Jason Gunthorpe wrote it :P
Er, maybe I mis understood the output then?
This is not a "failure" though, the map succeeded and gave a small page mapping.
This is not reflecting a bug in iommufd but a bug in the TYPE1 support in VFIO itself because it definitely cannot maintain the required unmap anywhere semantic if it mapped in a 1G huge page like this.
Basically, if you are mapping with TYPE1 mode then this should be triggered:
if (!strcmp(variant->iommu_mode, "iommufd_compat_type1")) mapping_size = SZ_4K;
Quoted 8 lines
And VFIO should be the one to fail, not iommufd.
If you really want to test TYPE1 you need to test what makes it unique, which is that you can map any VMA and then unmap any slice of it. Including within what should otherwise be a 1G page.
But I doubt anyone cares enough to fix this, so just exclude
VFIO_TYPE1_IOMMUfrom this test?
Ah, ok, thanks for the explanation. So VFIO_TYPE1_IOMMU should always use 4K mappings regardless of backend (VFIO or iommufd) so that unmap can work as intended.
I think excluding VFIO_TYPE1_IOMMU from this assertion makes sense if we don't care about fixing it.
Quoted 10 lines · David Matlack
Quoted 6 lines
If you really want to test TYPE1 you need to test what makes it unique, which is that you can map any VMA and then unmap any slice of it. Including within what should otherwise be a 1G page.
But I doubt anyone cares enough to fix this, so just exclude
VFIO_TYPE1_IOMMUfrom this test?Ah, ok, thanks for the explanation. So
VFIO_TYPE1_IOMMUshould always use 4K mappings regardless of backend (VFIO or iommufd) so that unmap can work as intended.
IDK, I think you should just ignore testing TYPE1v0. The actual real semantics that it had are quite confusing and iommufd provides an emulation that is going to be functionally OK (indeed, functionally more capable) but is not the exactly the same.
The old comment here is sort of enlightening:
+ * vfio-iommu-type1 (v1) - User mappings were coalesced together to + * avoid tracking individual mappings. This means that the granularity + * of the original mapping was lost and the user was allowed to attempt + * to unmap any range. Depending on the contiguousness of physical + * memory and page sizes supported by the IOMMU, arbitrary unmaps may + * or may not have worked. We only guaranteed unmap granularity + * matching the original mapping; even though it was untracked here, + * the original mappings are reflected in IOMMU mappings. This + * resulted in a couple unusual behaviors. First, if a range is not + * able to be unmapped, ex. a set of 4k pages that was mapped as a + * 2M hugepage into the IOMMU, the unmap ioctl returns success but with + * a zero sized unmap. Also, if an unmap request overlaps the first + * address of a hugepage, the IOMMU will unmap the entire hugepage. + * This also returns success and the returned unmap size reflects the + * actual size unmapped.
iommufd does not try to do this "returned unmap size reflects the actual size unmapped" part, it always unmaps exactly what was requested, because it disables huge pages.
Jason
Alex Williamson
maintainerQuoted 35 lines · On Fri, 9 Jan 2026 14:01:53 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 10 lines · David Matlack
Quoted 6 lines
If you really want to test TYPE1 you need to test what makes it unique, which is that you can map any VMA and then unmap any slice of it. Including within what should otherwise be a 1G page.
But I doubt anyone cares enough to fix this, so just exclude
VFIO_TYPE1_IOMMUfrom this test?Ah, ok, thanks for the explanation. So
VFIO_TYPE1_IOMMUshould always use 4K mappings regardless of backend (VFIO or iommufd) so that unmap can work as intended.IDK, I think you should just ignore testing TYPE1v0. The actual real semantics that it had are quite confusing and iommufd provides an emulation that is going to be functionally OK (indeed, functionally more capable) but is not the exactly the same.
The old comment here is sort of enlightening:
(quoted diff) +15
-0+ * vfio-iommu-type1 (v1) - User mappings were coalesced together to + * avoid tracking individual mappings. This means that the granularity + * of the original mapping was lost and the user was allowed to attempt + * to unmap any range. Depending on the contiguousness of physical + * memory and page sizes supported by the IOMMU, arbitrary unmaps may + * or may not have worked. We only guaranteed unmap granularity + * matching the original mapping; even though it was untracked here, + * the original mappings are reflected in IOMMU mappings. This + * resulted in a couple unusual behaviors. First, if a range is not + * able to be unmapped, ex. a set of 4k pages that was mapped as a + * 2M hugepage into the IOMMU, the unmap ioctl returns success but with + * a zero sized unmap. Also, if an unmap request overlaps the first + * address of a hugepage, the IOMMU will unmap the entire hugepage. + * This also returns success and the returned unmap size reflects the + * actual size unmapped.
Quoted 3 lines
iommufd does not try to do this "returned unmap size reflects the actual size unmapped" part, it always unmaps exactly what was requested, because it disables huge pages.
I think there was also some splitting code in the IOMMU drivers that has since been removed that may have made the v1 interface slightly more sane. It certainly never restricted mappings to PAGE_SIZE in order to allow arbitrary unmaps, it relied on users to do sane things and examine the results. Those "sane things" sort of became the v2 interface.
In any case, we've had v2 for a long time and if IOMMUFD compat make v1 more bloated and slow such that users realize they're using an old, crappy interface, that's probably for the best. Examining what page size is used for v1 is probably not worthwhile though. Thanks,
Alex
David Matlack
maintainerQuoted 50 lines · Alex Williamson
Quoted 35 lines · On Fri, 9 Jan 2026 14:01:53 -0400 Jason Gunthorpe <[email protected]> wrote:
Quoted 10 lines · David Matlack
Quoted 6 lines
If you really want to test TYPE1 you need to test what makes it unique, which is that you can map any VMA and then unmap any slice of it. Including within what should otherwise be a 1G page.
But I doubt anyone cares enough to fix this, so just exclude
VFIO_TYPE1_IOMMUfrom this test?Ah, ok, thanks for the explanation. So
VFIO_TYPE1_IOMMUshould always use 4K mappings regardless of backend (VFIO or iommufd) so that unmap can work as intended.IDK, I think you should just ignore testing TYPE1v0. The actual real semantics that it had are quite confusing and iommufd provides an emulation that is going to be functionally OK (indeed, functionally more capable) but is not the exactly the same.
The old comment here is sort of enlightening:
(quoted diff) +15
-0+ * vfio-iommu-type1 (v1) - User mappings were coalesced together to + * avoid tracking individual mappings. This means that the granularity + * of the original mapping was lost and the user was allowed to attempt + * to unmap any range. Depending on the contiguousness of physical + * memory and page sizes supported by the IOMMU, arbitrary unmaps may + * or may not have worked. We only guaranteed unmap granularity + * matching the original mapping; even though it was untracked here, + * the original mappings are reflected in IOMMU mappings. This + * resulted in a couple unusual behaviors. First, if a range is not + * able to be unmapped, ex. a set of 4k pages that was mapped as a + * 2M hugepage into the IOMMU, the unmap ioctl returns success but with + * a zero sized unmap. Also, if an unmap request overlaps the first + * address of a hugepage, the IOMMU will unmap the entire hugepage. + * This also returns success and the returned unmap size reflects the + * actual size unmapped.Quoted 3 lines
iommufd does not try to do this "returned unmap size reflects the actual size unmapped" part, it always unmaps exactly what was requested, because it disables huge pages.
I think there was also some splitting code in the IOMMU drivers that has since been removed that may have made the v1 interface slightly more sane. It certainly never restricted mappings to
PAGE_SIZEin order to allow arbitrary unmaps, it relied on users to do sane things and examine the results. Those "sane things" sort of became the v2 interface.
Quoted 4 lines
In any case, we've had v2 for a long time and if IOMMUFD compat make v1 more bloated and slow such that users realize they're using an old, crappy interface, that's probably for the best. Examining what page size is used for v1 is probably not worthwhile though. Thanks,
Ack. I'll send a patch to skip the page size checks for v1.
Quoted 103 lines · David Matlack
Quoted 1 line · Alex Mastro
Test MMIO-backed DMA mappings by iommu_map()-ing mmap'ed BAR regions.
Thanks for adding this!
Quoted 5 lines
Also update
vfio_pci_bar_map()to align BAR mmaps for efficient huge page mappings.Only
vfio_type1variants are tested; iommufd variants can be added once kernel support lands.Are there plans to support mapping BARs via virtual address in iommufd? I thought the plan was only to support via dma-bufs. Maybe Jason can confirm.
Assuming not, should we add negative tests here to make sure iommufd does not allow mapping BARs?
And then we can add dma-buf tests in a future commit.
Quoted 83 lines
The manual mmap alignment can be removed
oncemmap(!MAP_FIXED)on vfio device fds improves to automatically return well-aligned addresses.Signed-off-by Alex Mastro <[email protected]>---
Sanity test run:$ ./vfio_dma_mapping_mmio_test 0000:05:00.0 TAP version 13 1..4 # Starting 4 tests from 2 test cases. # RUN vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_full_bar ... Mapping BAR4: vaddr=0x7fad40000000 size=0x2000000000 iova=0x2000000000 # OK vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_full_bar ok 1 vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_full_bar # RUN vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_partial_bar ... Mapping BAR4 (partial): vaddr=0x7fad40000000 size=0x1000 iova=0x0 # OK vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_partial_bar ok 2 vfio_dma_mapping_mmio_test.vfio_type1_iommu.map_partial_bar # RUN vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_full_bar ... Mapping BAR4: vaddr=0x7fad40000000 size=0x2000000000 iova=0x2000000000 # OK vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_full_bar ok 3 vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_full_bar # RUN vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_partial_bar ... Mapping BAR4 (partial): vaddr=0x7fad40000000 size=0x1000 iova=0x0 # OK vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_partial_bar ok 4 vfio_dma_mapping_mmio_test.vfio_type1v2_iommu.map_partial_bar # PASSED: 4 / 4 tests passed. # Totals: pass:4 fail:0 xfail:0 xpass:0 skip:0 error:0 --- tools/testing/selftests/vfio/Makefile | 1 + tools/testing/selftests/vfio/lib/vfio_pci_device.c | 28 ++++- .../selftests/vfio/vfio_dma_mapping_mmio_test.c | 132 +++++++++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-)tools/testing/selftests/vfio/Makefile +1
-0@@ -1,5 +1,6 @@ CFLAGS = $(KHDR_INCLUDES) TEST_GEN_PROGS += vfio_dma_mapping_test +TEST_GEN_PROGS += vfio_dma_mapping_mmio_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_device_init_perf_testtools/testing/selftests/vfio/lib/vfio_pci_device.c +6
-0@@ -12,10 +12,13 @@#include <sys/mman.h> #include <uapi/linux/types.h> +#include <linux/align.h> #include <linux/iommufd.h> +#include <linux/kernel.h> #include <linux/limits.h> #include <linux/mman.h> #include <linux/overflow.h> +#include <linux/sizes.h> #include <linux/types.h> #include <linux/vfio.h> @@ -124,20 +127,43 @@ static void vfio_pci_region_get(struct vfio_pci_device *device, int index,static void vfio_pci_bar_map (struct vfio_pci_device *device,int index) {struct vfio_pci_bar *bar = &device->bars[index]; +size_t align, size; +void *map_base, *map_align;int prot =0 ;VFIO_ASSERT_LT (index,PCI_STD_NUM_BARS );VFIO_ASSERT_NULL (bar->vaddr);VFIO_ASSERT_TRUE (bar->info.flags &VFIO_REGION_INFO_FLAG_MMAP ); +VFIO_ASSERT_GT (bar->info.size,0 );if (bar->info.flags &VFIO_REGION_INFO_FLAG_READ ) prot |=PROT_READ ;if (bar->info.flags &VFIO_REGION_INFO_FLAG_WRITE ) prot |=PROT_WRITE ;
Quoted 10 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_region_get(struct vfio_pci_device *device, int index, · line 149
Quoted 7 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +6
-1- bar->vaddr = mmap(NULL, bar->info.size, prot, MAP_FILE | MAP_SHARED, +/* +* Align the mmap for more efficient IOMMU mapping. +* The largest PUD size supporting huge pfnmap is 1GiB. +*/ + size = bar->info.size; + align =min_t (u64,1ULL <<__builtin_ctzll (size),SZ_1G );What's the reason to align to 1ULL <<
__builtin_ctzll(size)and not just size?
This was inspired by QEMU's hw/vfio/region.c which also does this rounding up of size to the next power of two [1].
I'm now realizing that's only necessary for regions with VFIO_REGION_INFO_CAP_SPARSE_MMAP where there are multiple mmaps per region, and each mmap's size is less than the size of the BAR. Here, since we're mapping the entire BAR which must be pow2, it shouldn't be necessary.
It's also making me realize...
That the existing vfio_pci_device_setup() BAR mapping path isn't accounting for the possibility of SPARE_MMAP where attempting to mmap the entire region would fail due to non-mmap'able holes.
The intent of QEMU's mmap alignment code is imperfect in the SPARE_MMAP case? After a hole, the next mmap'able range could be some arbitrary page-aligned offset into the region. It's not helpful mmap some region offset which is maximally 4K-aligned at a 1G-aligned vaddr.
I think to be optimal, QEMU should be attempting to align the vaddr for bar mmaps such that
vaddr % {2M,1G} == region_offset % {2M,1G}
Would love someone to sanity check me on this. Kind of a diversion.
[1] https://github.com/qemu/qemu/blob/master/hw/vfio/region.c#L255-L286
Quoted 14 lines
Quoted 14 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +13
-0+ + map_base =mmap (NULL , size + align,PROT_NONE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +VFIO_ASSERT_NE (map_base,MAP_FAILED ); + + map_align = (void *)ALIGN ((uintptr_t )map_base, align); + +if (map_align > map_base) +munmap (map_base, map_align - map_base); +if (align > (size_t )(map_align - map_base)) +munmap (map_align + size, align - (map_align - map_base)); + + bar->vaddr = mmap(map_align, size, prot, MAP_SHARED | MAP_FIXED,
Quoted 8 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_region_get(struct vfio_pci_device *device, int index, · line 167
Quoted 5 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +2
-0device->fd, bar->info.offset);VFIO_ASSERT_NE (bar->vaddr,MAP_FAILED ); + +madvise (bar->vaddr, size,MADV_HUGEPAGE ); }Can you split these changes out into a precursor commit? I think they stand on their own.
Ack.
Quoted 33 lines
Quoted 33 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +0
-0static void vfio_pci_bar_unmap (struct vfio_pci_device *device,int index)tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +24
-0@@ -0,0 +1,132 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <stdio.h> +#include <sys/mman.h> +#include <unistd.h> + +#include <uapi/linux/types.h> +#include <linux/pci_regs.h> +#include <linux/sizes.h> +#include <linux/vfio.h> + +#include <libvfio.h> + +#include "../kselftest_harness.h" + +static const char *device_bdf; + +static int largest_mapped_bar (struct vfio_pci_device *device) +{ +int bar_idx =-1 ; +u64 bar_size =0 ; + +for (int i =0 ; i <PCI_STD_NUM_BARS ; i++) { +struct vfio_pci_bar *bar = &device->bars[i]; +
Quoted 8 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c line 29
Quoted 5 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +5
-0+if (!bar->vaddr) +continue ; + +if (!(bar->info.flags &VFIO_REGION_INFO_FLAG_WRITE )) +continue ;nit: Add a comment here. I assume this is because
iommu_map()tries to create writable IOMMU mappings?
Yes, and I'll actually make a stronger check for both READ|WRITE here since iommu_map() always maps with VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE.
Quoted 3 lines
Speaking of, maybe we can add a test that creating writable IOMMU mappings fails for read-only BARs?
I think I'll have to look into this as a follow-on. I'm not sure how to validate it yet without mocks or similar since I don't have such HW.
Quoted 20 lines
Quoted 20 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +19
-0+ +if (bar->info.size > bar_size) { + bar_size = bar->info.size; + bar_idx = i; + } + } + +return bar_idx; +} + +FIXTURE (vfio_dma_mapping_mmio_test) { +struct iommu *iommu; +struct vfio_pci_device *device; +struct iova_allocator *iova_allocator; +int bar_idx; +}; + +FIXTURE_VARIANT (vfio_dma_mapping_mmio_test) { +const char *iommu_mode;
Quoted 8 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c line 54
Quoted 6 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +6
-0+}; + +#define FIXTURE_VARIANT_ADD_IOMMU_MODE (_iommu_mode) \ +FIXTURE_VARIANT_ADD(vfio_dma_mapping_mmio_test, _iommu_mode) { \ + .iommu_mode = #_iommu_mode, \ +}nit: Alignment of trailing backslashes is off.
Ack
Quoted 23 lines
Quoted 23 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +22
-0+ +FIXTURE_VARIANT_ADD_IOMMU_MODE (vfio_type1_iommu); +FIXTURE_VARIANT_ADD_IOMMU_MODE (vfio_type1v2_iommu); + +#undef FIXTURE_VARIANT_ADD_IOMMU_MODE + +FIXTURE_SETUP (vfio_dma_mapping_mmio_test) +{ + self->iommu =iommu_init (variant->iommu_mode); + self->device =vfio_pci_device_init (device_bdf, self->iommu); + self->iova_allocator =iova_allocator_init (self->iommu); + self->bar_idx =largest_mapped_bar (self->device); +} + +FIXTURE_TEARDOWN (vfio_dma_mapping_mmio_test) +{ +iova_allocator_cleanup (self->iova_allocator); +vfio_pci_device_cleanup (self->device); +iommu_cleanup (self->iommu); +} + +TEST_F (vfio_dma_mapping_mmio_test, map_full_bar)
Quoted 8 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c line 82
Quoted 6 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +6
-0+{ +struct vfio_pci_bar *bar; +struct dma_region region; + +if (self->bar_idx <0 ) +SKIP (return,"No mappable BAR found on device %s" , device_bdf);I think you can do this in the
FIXTURE_SETUP()to avoid duplication.
Ack
Quoted 60 lines
Quoted 60 lines
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +50
-1+ + bar = &self->device->bars[self->bar_idx]; + + region = (struct dma_region ) { + .vaddr = bar->vaddr, + .size = bar->info.size, + .iova =iova_allocator_alloc (self->iova_allocator, bar->info.size), + }; + +printf ("Mapping BAR%d: vaddr=%p size=0x%lx iova=0x%lx\n" , + self->bar_idx, region.vaddr, region.size, region.iova); + +iommu_map (self->iommu, ®ion); +iommu_unmap (self->iommu, ®ion); +} + +TEST_F(vfio_dma_mapping_mmio_test, map_partial_bar) +{ +struct vfio_pci_bar *bar; +struct dma_region region; +size_t page_size; + +if (self->bar_idx <0 ) +SKIP (return,"No mappable BAR found on device %s" , device_bdf); + + bar = &self->device->bars[self->bar_idx]; + page_size =getpagesize (); + +if (bar->info.size <2 * page_size) +SKIP (return,"BAR%d too small for partial mapping test (size=0x%llx)" , + self->bar_idx, bar->info.size); + + region = (struct dma_region ) { + .vaddr = bar->vaddr, + .size = page_size, + .iova =iova_allocator_alloc (self->iova_allocator, page_size), + }; + +printf ("Mapping BAR%d (partial): vaddr=%p size=0x%lx iova=0x%lx\n" , + self->bar_idx, region.vaddr, region.size, region.iova); + +iommu_map (self->iommu, ®ion); +iommu_unmap (self->iommu, ®ion); +} + +int main (int argc,char *argv[]) +{ + device_bdf =vfio_selftests_get_bdf (&argc, argv); +return test_harness_run (argc, argv); +} ---base-commit:
d721f52e31553a848e0e9947ca15a49c5674aef3
change-id: 20260107-scratch-amastro-vfio-dma-mapping-mmio-test-eecf25d9a742Best regards,
Signature · 1 line
Alex Mastro <[email protected]>
Quoted 7 lines · Alex Mastro
This was inspired by QEMU's
hw/vfio/region.cwhich also does this rounding up of size to the next power of two [1].I'm now realizing that's only necessary for regions with
VFIO_REGION_INFO_CAP_SPARSE_MMAPwhere there are multiple mmaps per region, and each mmap's size is less than the size of the BAR. Here, since we're mapping the entire BAR which must be pow2, it shouldn't be necessary.
You only need to do this dance if you care about having large PTEs under the VMAs, which is probably something worth testing both scenarios.
Quoted 11 lines
The intent of QEMU's mmap alignment code is imperfect in the
SPARE_MMAPcase? After a hole, the next mmap'able range could be some arbitrary page-aligned offset into the region. It's not helpful mmap some region offset which is maximally 4K-aligned at a 1G-aligned vaddr.I think to be optimal, QEMU should be attempting to align the vaddr for bar mmaps such that
vaddr % {2M,1G} ==
region_offset% {2M,1G}Would love someone to sanity check me on this. Kind of a diversion.
What you write is correct. Ankit recently discovered this bug in qemu. It happens not just with SPARSE_MMAP but also when mmmaping around the MSI-X hole..
I also advocated for what you write here that qemu should ensure:
vaddr % region_size == region_offset % region_size
Until VFIO learns to align its VMAs on its own via Peter's work.
Jason
Quoted 4 lines · Jason Gunthorpe
Quoted 3 lines · Alex Mastro
This was inspired by QEMU's
hw/vfio/region.cwhich also does this rounding up of size to the next power of two [1].
Quoted 8 lines
Quoted 4 lines
I'm now realizing that's only necessary for regions with
VFIO_REGION_INFO_CAP_SPARSE_MMAPwhere there are multiple mmaps per region, and each mmap's size is less than the size of the BAR. Here, since we're mapping the entire BAR which must be pow2, it shouldn't be necessary.You only need to do this dance if you care about having large PTEs under the VMAs, which is probably something worth testing both scenarios.
Yep, makes sense. The test takes a long time to run without this due potentially faulting a 128G BAR region 4K at a time during VFIO_IOMMU_MAP_DMA.
Quoted 9 lines
Quoted 9 lines
The intent of QEMU's mmap alignment code is imperfect in the
SPARE_MMAPcase? After a hole, the next mmap'able range could be some arbitrary page-aligned offset into the region. It's not helpful mmap some region offset which is maximally 4K-aligned at a 1G-aligned vaddr.I think to be optimal, QEMU should be attempting to align the vaddr for bar mmaps such that
Quoted 7 lines
Quoted 3 lines
vaddr % {2M,1G} ==
region_offset% {2M,1G}Would love someone to sanity check me on this. Kind of a diversion.
What you write is correct. Ankit recently discovered this bug in qemu. It happens not just with
SPARSE_MMAPbut also when mmmaping around the MSI-X hole..
Is my mental model broken? I thought MSI-X holes in a VFIO-exposed BAR region implied SPARSE_MMAP? I didn't think there was another way for the uapi to express hole-yness.
Quoted 4 lines
I also advocated for what you write here that qemu should ensure:
vaddr %
region_size==region_offset%region_size
Why region_size out of curiosity? Assuming perfect knowledge of kernel internals I would have expected something like this:
hw/vfio/region.c +18 -2
@@ -238,6 +238,18 @@ static void vfio_subregion_unmap(VFIORegion *region, int index) region->mmaps[index].mmap =NULL ; } +/* +* Return the next value greater than or equal to `input` such that +* (value % align) == offset. +*/ +static size_t align_offset (size_t input,size_t offset,size_t align) +{ +size_t remainder = input % align; +size_t delta = (align + offset - remainder) % align; + +return input + delta; +} +int vfio_region_mmap (VFIORegion *region) {int i, ret, prot =0 ;
@@ -252,7 +264,11 @@ int vfio_region_mmap(VFIORegion *region) prot |= region->flags &VFIO_REGION_INFO_FLAG_WRITE ?PROT_WRITE :0 ;for (i =0 ; i < region->nr_mmaps; i++) { -size_t align =MIN (1ULL <<ctz64 (region->mmaps[i].size),1 * GiB); +size_t size = region->mmaps[i].size; +size_t offs = region->mmaps[i].offset; +size_t align = size >= GiB ? GiB : + size >=2 * MiB ?2 * MiB : +getpagesize ();void *map_base, *map_align;/*
@@ -275,7 +291,7 @@ int vfio_region_mmap(VFIORegion *region) fd =vfio_device_get_region_fd (region->vbasedev, region->nr); - map_align = (void *)ROUND_UP ((uintptr_t )map_base, (uintptr_t )align); + map_align = (void *)align_offset ((size_t )map_base, offs % align, align);munmap (map_base, map_align - map_base);munmap (map_align + region->mmaps[i].size, align - (map_align - map_base));
Quoted 14 lines · Alex Mastro
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Alex Mastro
The intent of QEMU's mmap alignment code is imperfect in the
SPARE_MMAPcase? After a hole, the next mmap'able range could be some arbitrary page-aligned offset into the region. It's not helpful mmap some region offset which is maximally 4K-aligned at a 1G-aligned vaddr.I think to be optimal, QEMU should be attempting to align the vaddr for bar mmaps such that
vaddr % {2M,1G} ==
region_offset% {2M,1G}Would love someone to sanity check me on this. Kind of a diversion.
Quoted 7 lines
Quoted 3 lines
What you write is correct. Ankit recently discovered this bug in qemu. It happens not just with
SPARSE_MMAPbut also when mmmaping around the MSI-X hole..Is my mental model broken? I thought MSI-X holes in a VFIO-exposed BAR region implied
SPARSE_MMAP? I didn't think there was another way for the uapi to express hole-yness.
Yes, it was broken. Creating MSI-X table holes with SPARSE_MMAP ended back in 2017 and was superseded by VFIO_REGION_INFO_CAP_MSIX_MAPPABLE [1].
[1] https://lore.kernel.org/all/[email protected]/
Only nvgrace-gpu and some i915 reference SPARSE_MMAP today.
Alex Williamson
maintainerQuoted 7 lines · On Wed, 7 Jan 2026 19:36:44 -0800 Alex Mastro <[email protected]> wrote:
Quoted 3 lines · David Matlack
Speaking of, maybe we can add a test that creating writable IOMMU mappings fails for read-only BARs?
I think I'll have to look into this as a follow-on. I'm not sure how to validate it yet without mocks or similar since I don't have such HW.
I think the read-only aspect would be in the mmap, not the BAR itself, ie. can we create a read-write DMA mapping to a read-only mmap.
ROM BARs are the only BARs that are read-only, but they can share a decoder with the standard BARs and therefore have a separate enable in the BAR register itself. Due to this, and their general usage, it's never been necessary to allow mmap of the ROM BAR, therefore we cannot actually DMA map the ROM BAR. Thanks,
Alex
David Matlack
maintainerQuoted 5 lines · Alex Williamson
Quoted 2 lines · On Wed, 7 Jan 2026 19:36:44 -0800 Alex Mastro <[email protected]> wrote:
Quoted 1 line · David Matlack
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Speaking of, maybe we can add a test that creating writable IOMMU mappings fails for read-only BARs?
I think I'll have to look into this as a follow-on. I'm not sure how to validate it yet without mocks or similar since I don't have such HW.
I think the read-only aspect would be in the mmap, not the BAR itself, ie. can we create a read-write DMA mapping to a read-only mmap.
Good point. So it'd be better to have a test of that in vfio_dma_mapping_test. No need to use a BAR mapping.
Quoted 5 lines
ROM BARs are the only BARs that are read-only, but they can share a decoder with the standard BARs and therefore have a separate enable in the BAR register itself. Due to this, and their general usage, it's never been necessary to allow mmap of the ROM BAR, therefore we cannot actually DMA map the ROM BAR. Thanks,
Ahh, good to know, thanks for the context!