Skip to contentSkip to conversation

vfio: selftests: Add MMIO DMA mapping test

PATCH Superseded Alex Mastro 2 participants 8 messages Jan 13 – Jan 14

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover vfio: selftests: Add MMIO DMA mapping test

Test IOMMU mapping the BAR mmaps created during vfio_pci_device_setup().

All IOMMU modes are tested: vfio_type1 variants are expected to succeed, while non-type1 modes are expected to fail. iommufd compat mode can be updated to expect success once kernel support lands; native iommufd will not support mapping vaddrs backed by MMIO (it will support dma-buf based MMIO mapping instead).

To: Alex Williamson <[email protected]>
To: David Matlack <[email protected]>
To: Shuah Khan <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: Jason Gunthorpe <[email protected]>

Changes in v2:
- Split into patch series
- Factor out mmap_aligned() for vaddr alignment
- Align BAR mmaps to improve hugepage IOMMU mapping efficiency
- Centralize MODE_* string definitions
- Add is_power_of_2() assertion for BAR size
- Simplify align calculation to min(size, 1G)
- Add map_bar_misaligned test case
- Link to v1: https://lore.kernel.org/all/[email protected]

---
Alex Mastro (3):
vfio: selftests: Centralize IOMMU mode name definitions
vfio: selftests: Align BAR mmaps for efficient IOMMU mapping
vfio: selftests: Add vfio_dma_mapping_mmio_test

 tools/testing/selftests/vfio/Makefile              |   1 +
 tools/testing/selftests/vfio/lib/include/libvfio.h |   9 ++
 .../selftests/vfio/lib/include/libvfio/iommu.h     |   6 +
 tools/testing/selftests/vfio/lib/iommu.c           |  12 +-
 tools/testing/selftests/vfio/lib/libvfio.c         |  25 ++++
 tools/testing/selftests/vfio/lib/vfio_pci_device.c |  24 +++-
 .../selftests/vfio/vfio_dma_mapping_mmio_test.c    | 144 +++++++++++++++++++++
 .../testing/selftests/vfio/vfio_dma_mapping_test.c |   2 +-
 8 files changed, 215 insertions(+), 8 deletions(-)

---
base-commit: d721f52e31553a848e0e9947ca15a49c5674aef3
change-id: 20260112-map-mmio-test-b4e4c2d917a9

Best regards,

Signature · 1 line
Alex Mastro <[email protected]>
#1 lore raw

PATCH v2 1/3 vfio: selftests: Centralize IOMMU mode name definitions

Replace scattered string literals with MODE_* macros in iommu.h. This provides a single source of truth for IOMMU mode name strings.

---
 tools/testing/selftests/vfio/lib/include/libvfio/iommu.h |  6 ++++++
 tools/testing/selftests/vfio/lib/iommu.c                 | 12 ++++++------
 tools/testing/selftests/vfio/vfio_dma_mapping_test.c     |  2 +-
 3 files changed, 13 insertions(+), 7 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +6 -0
index 5c9b9dc6d993..e9a3386a4719 100644
@@ -61,6 +61,12 @@ iova_t iommu_hva2iova(struct iommu *iommu, void *vaddr);
 
 struct iommu_iova_range *iommu_iova_ranges(struct iommu *iommu, u32 *nranges);
 
+#define MODE_VFIO_TYPE1_IOMMU "vfio_type1_iommu"
+#define MODE_VFIO_TYPE1V2_IOMMU "vfio_type1v2_iommu"
+#define MODE_IOMMUFD_COMPAT_TYPE1 "iommufd_compat_type1"
+#define MODE_IOMMUFD_COMPAT_TYPE1V2 "iommufd_compat_type1v2"
+#define MODE_IOMMUFD "iommufd"
+
 /*
  * Generator for VFIO selftests fixture variants that replicate across all
  * possible IOMMU modes. Tests must define FIXTURE_VARIANT_ADD_IOMMU_MODE()
tools/testing/selftests/vfio/lib/iommu.c +6 -6
index 8079d43523f3..27d1d13abfeb 100644
@@ -21,32 +21,32 @@
 #include "../../../kselftest.h"
 #include <libvfio.h>
 
-const char *default_iommu_mode = "iommufd";
+const char *default_iommu_mode = MODE_IOMMUFD;
 
 /* Reminder: Keep in sync with FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(). */
 static const struct iommu_mode iommu_modes[] = {
 	{
-		.name = "vfio_type1_iommu",
+		.name = MODE_VFIO_TYPE1_IOMMU,
 		.container_path = "/dev/vfio/vfio",
 		.iommu_type = VFIO_TYPE1_IOMMU,
 	},
 	{
-		.name = "vfio_type1v2_iommu",
+		.name = MODE_VFIO_TYPE1V2_IOMMU,
 		.container_path = "/dev/vfio/vfio",
 		.iommu_type = VFIO_TYPE1v2_IOMMU,
 	},
 	{
-		.name = "iommufd_compat_type1",
+		.name = MODE_IOMMUFD_COMPAT_TYPE1,
 		.container_path = "/dev/iommu",
 		.iommu_type = VFIO_TYPE1_IOMMU,
 	},
 	{
-		.name = "iommufd_compat_type1v2",
+		.name = MODE_IOMMUFD_COMPAT_TYPE1V2,
 		.container_path = "/dev/iommu",
 		.iommu_type = VFIO_TYPE1v2_IOMMU,
 	},
 	{
-		.name = "iommufd",
+		.name = MODE_IOMMUFD,
 	},
 };
 
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +1 -1
index 5397822c3dd4..7cd396aa205c 100644
@@ -166,7 +166,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
 	 * IOMMUFD compatibility-mode does not support huge mappings when
 	 * using VFIO_TYPE1_IOMMU.
 	 */
-	if (!strcmp(variant->iommu_mode, "iommufd_compat_type1"))
+	if (!strcmp(variant->iommu_mode, MODE_IOMMUFD_COMPAT_TYPE1))
 		mapping_size = SZ_4K;
 
 	ASSERT_EQ(0, rc);
Signature · 1 line
2.47.3
#2 lore raw

PATCH v2 2/3 vfio: selftests: Align BAR mmaps for efficient IOMMU mapping

In reply to Alex Mastro

Update vfio_pci_bar_map() to align BAR mmaps for efficient huge page mappings. The manual mmap alignment can be removed once mmap(!MAP_FIXED) on vfio device fds improves to automatically return well-aligned addresses.

---
 tools/testing/selftests/vfio/lib/include/libvfio.h |  9 ++++++++
 tools/testing/selftests/vfio/lib/libvfio.c         | 25 ++++++++++++++++++++++
 tools/testing/selftests/vfio/lib/vfio_pci_device.c | 24 ++++++++++++++++++++-
 3 files changed, 57 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/include/libvfio.h +9 -0
index 279ddcd70194..5ebf8503586e 100644
@@ -23,4 +23,13 @@
 const char *vfio_selftests_get_bdf(int *argc, char *argv[]);
 char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs);
 
+/*
+ * Reserve virtual address space of size at an address satisfying
+ * (vaddr % align) == offset.
+ *
+ * Returns the reserved vaddr. The caller is responsible for unmapping
+ * the returned region.
+ */
+void *mmap_aligned(size_t size, size_t align, size_t offset);
+
 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H */
tools/testing/selftests/vfio/lib/libvfio.c +25 -0
index a23a3cc5be69..4529bb1e69d1 100644
@@ -2,6 +2,9 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/mman.h>
+
+#include <linux/align.h>
 
 #include "../../../kselftest.h"
 #include <libvfio.h>
@@ -76,3 +79,25 @@ const char *vfio_selftests_get_bdf(int *argc, char *argv[])
 
 	return vfio_selftests_get_bdfs(argc, argv, &nr_bdfs)[0];
 }
+
+void *mmap_aligned(size_t size, size_t align, size_t offset)
+{
+	void *map_base, *map_align;
+	size_t delta;
+
+	VFIO_ASSERT_GT(align, offset);
+	delta = align - offset;
+
+	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 + delta, align) - delta);
+
+	if (map_align > map_base)
+		VFIO_ASSERT_EQ(munmap(map_base, map_align - map_base), 0);
+
+	VFIO_ASSERT_EQ(munmap(map_align + size, map_base + align - map_align), 0);
+
+	return map_align;
+}
tools/testing/selftests/vfio/lib/vfio_pci_device.c +23 -1
index 13fdb4b0b10f..03f35011b5f7 100644
@@ -12,10 +12,14 @@
 #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/log2.h>
 #include <linux/mman.h>
 #include <linux/overflow.h>
+#include <linux/sizes.h>
 #include <linux/types.h>
 #include <linux/vfio.h>
 
@@ -124,20 +128,38 @@ 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 *vaddr;
 	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_TRUE(is_power_of_2(bar->info.size));
 
 	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,
+	size = bar->info.size;
+
+	/*
+	 * Align BAR mmaps to improve page fault granularity during potential
+	 * subsequent IOMMU mapping of these BAR vaddr. 1G for x86 is the
+	 * largest hugepage size across any architecture, so no benefit from
+	 * larger alignment. BARs smaller than 1G will be aligned by their
+	 * power-of-two size, guaranteeing sufficient alignment for smaller
+	 * hugepages, if present.
+	 */
+	align = min_t(size_t, size, SZ_1G);
+
+	vaddr = mmap_aligned(size, align, 0);
+	bar->vaddr = mmap(vaddr, 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)
Signature · 1 line
2.47.3
maintainer
#3 lore raw

Re: [PATCH v2 2/3] vfio: selftests: Align BAR mmaps for efficient IOMMU mapping

Quoted 4 lines · Alex Mastro

Update vfio_pci_bar_map() to align BAR mmaps for efficient huge page mappings. The manual mmap alignment can be removed once mmap(!MAP_FIXED) on vfio device fds improves to automatically return well-aligned addresses.

Please also mention that you added MADV_HUGEPAGE and why, and that you dropped MAP_FILE (just mention that it was unnecessary in the first place).

Quoted 14 lines
---
 tools/testing/selftests/vfio/lib/include/libvfio.h |  9 ++++++++
 tools/testing/selftests/vfio/lib/libvfio.c         | 25 ++++++++++++++++++++++
 tools/testing/selftests/vfio/lib/vfio_pci_device.c | 24 ++++++++++++++++++++-
 3 files changed, 57 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/include/libvfio.h +0 -0
index 279ddcd70194..5ebf8503586e 100644
@@ -23,4 +23,13 @@
 const char *vfio_selftests_get_bdf(int *argc, char *argv[]);

tools/testing/selftests/vfio/lib/include/libvfio.h line 33

Quoted 10 lines
tools/testing/selftests/vfio/lib/include/libvfio.h +8 -0
 char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs);
 
+/*
+ * Reserve virtual address space of size at an address satisfying
+ * (vaddr % align) == offset.
+ *
+ * Returns the reserved vaddr. The caller is responsible for unmapping
+ * the returned region.
+ */
+void *mmap_aligned(size_t size, size_t align, size_t offset);

nit: Perhaps we should name this mmap_reserve()? The current name implies something is being mmap'ed.

Quoted 62 lines
tools/testing/selftests/vfio/lib/include/libvfio.h +1 -0
+
 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H */
tools/testing/selftests/vfio/lib/libvfio.c +25 -0
index a23a3cc5be69..4529bb1e69d1 100644
@@ -2,6 +2,9 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/mman.h>
+
+#include <linux/align.h>
 
 #include "../../../kselftest.h"
 #include <libvfio.h>
@@ -76,3 +79,25 @@ const char *vfio_selftests_get_bdf(int *argc, char *argv[])
 
 	return vfio_selftests_get_bdfs(argc, argv, &nr_bdfs)[0];
 }
+
+void *mmap_aligned(size_t size, size_t align, size_t offset)
+{
+	void *map_base, *map_align;
+	size_t delta;
+
+	VFIO_ASSERT_GT(align, offset);
+	delta = align - offset;
+
+	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 + delta, align) - delta);
+
+	if (map_align > map_base)
+		VFIO_ASSERT_EQ(munmap(map_base, map_align - map_base), 0);
+
+	VFIO_ASSERT_EQ(munmap(map_align + size, map_base + align - map_align), 0);
+
+	return map_align;
+}
tools/testing/selftests/vfio/lib/vfio_pci_device.c +4 -0
index 13fdb4b0b10f..03f35011b5f7 100644
@@ -12,10 +12,14 @@
 #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/log2.h>
 #include <linux/mman.h>
 #include <linux/overflow.h>
+#include <linux/sizes.h>
 #include <linux/types.h>
 #include <linux/vfio.h>
 

tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_region_get(struct vfio_pci_device *device, int index, · line 133

Quoted 6 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +2 -0
@@ -124,20 +128,38 @@ 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 *vaddr;
 	int prot = 0;

uber-nit: Put vaddr after prot to preserve the reverse-fir-tree ordering of variables.

Here's the tip tree documentation:

https://docs.kernel.org/process/maintainer-tip.html#variable-declarations

I should probably document somewhere that this is preferred in VFIO selftests as well.

Quoted 37 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +17 -1
 	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_TRUE(is_power_of_2(bar->info.size));
 
 	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,
+	size = bar->info.size;
+
+	/*
+	 * Align BAR mmaps to improve page fault granularity during potential
+	 * subsequent IOMMU mapping of these BAR vaddr. 1G for x86 is the
+	 * largest hugepage size across any architecture, so no benefit from
+	 * larger alignment. BARs smaller than 1G will be aligned by their
+	 * power-of-two size, guaranteeing sufficient alignment for smaller
+	 * hugepages, if present.
+	 */
+	align = min_t(size_t, size, SZ_1G);
+
+	vaddr = mmap_aligned(size, align, 0);
+	bar->vaddr = mmap(vaddr, 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)
 
Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH v2 2/3] vfio: selftests: Align BAR mmaps for efficient IOMMU mapping

Quoted 9 lines · David Matlack
Quoted 4 lines · Alex Mastro

Update vfio_pci_bar_map() to align BAR mmaps for efficient huge page mappings. The manual mmap alignment can be removed once mmap(!MAP_FIXED) on vfio device fds improves to automatically return well-aligned addresses.

Please also mention that you added MADV_HUGEPAGE and why, and that you dropped MAP_FILE (just mention that it was unnecessary in the first place).

Ack

Quoted 17 lines
Quoted 17 lines
---
 tools/testing/selftests/vfio/lib/include/libvfio.h |  9 ++++++++
 tools/testing/selftests/vfio/lib/libvfio.c         | 25 ++++++++++++++++++++++
 tools/testing/selftests/vfio/lib/vfio_pci_device.c | 24 ++++++++++++++++++++-
 3 files changed, 57 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/include/libvfio.h +0 -0
index 279ddcd70194..5ebf8503586e 100644
@@ -23,4 +23,13 @@
 const char *vfio_selftests_get_bdf(int *argc, char *argv[]);
 char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs);
 
Quoted 11 lines

tools/testing/selftests/vfio/lib/include/libvfio.h line 33

Quoted 8 lines
tools/testing/selftests/vfio/lib/include/libvfio.h +8 -0
+/*
+ * Reserve virtual address space of size at an address satisfying
+ * (vaddr % align) == offset.
+ *
+ * Returns the reserved vaddr. The caller is responsible for unmapping
+ * the returned region.
+ */
+void *mmap_aligned(size_t size, size_t align, size_t offset);

nit: Perhaps we should name this mmap_reserve()? The current name implies something is being mmap'ed.

SGTM

Quoted 73 lines
Quoted 63 lines
tools/testing/selftests/vfio/lib/include/libvfio.h +1 -0
+
 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H */
tools/testing/selftests/vfio/lib/libvfio.c +25 -0
index a23a3cc5be69..4529bb1e69d1 100644
@@ -2,6 +2,9 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/mman.h>
+
+#include <linux/align.h>
 
 #include "../../../kselftest.h"
 #include <libvfio.h>
@@ -76,3 +79,25 @@ const char *vfio_selftests_get_bdf(int *argc, char *argv[])
 
 	return vfio_selftests_get_bdfs(argc, argv, &nr_bdfs)[0];
 }
+
+void *mmap_aligned(size_t size, size_t align, size_t offset)
+{
+	void *map_base, *map_align;
+	size_t delta;
+
+	VFIO_ASSERT_GT(align, offset);
+	delta = align - offset;
+
+	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 + delta, align) - delta);
+
+	if (map_align > map_base)
+		VFIO_ASSERT_EQ(munmap(map_base, map_align - map_base), 0);
+
+	VFIO_ASSERT_EQ(munmap(map_align + size, map_base + align - map_align), 0);
+
+	return map_align;
+}
tools/testing/selftests/vfio/lib/vfio_pci_device.c +4 -0
index 13fdb4b0b10f..03f35011b5f7 100644
@@ -12,10 +12,14 @@
 #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/log2.h>
 #include <linux/mman.h>
 #include <linux/overflow.h>
+#include <linux/sizes.h>
 #include <linux/types.h>
 #include <linux/vfio.h>
 

tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_region_get(struct vfio_pci_device *device, int index, · line 133

Quoted 6 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +2 -0
@@ -124,20 +128,38 @@ 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 *vaddr;
 	int prot = 0;

uber-nit: Put vaddr after prot to preserve the reverse-fir-tree ordering of variables.

Quoted 6 lines

Here's the tip tree documentation:

https://docs.kernel.org/process/maintainer-tip.html#variable-declarations

I should probably document somewhere that this is preferred in VFIO selftests as well.

Ah, thanks. I usually try to do this but missed it here.

Quoted 38 lines
Quoted 38 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +17 -1
 	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_TRUE(is_power_of_2(bar->info.size));
 
 	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,
+	size = bar->info.size;
+
+	/*
+	 * Align BAR mmaps to improve page fault granularity during potential
+	 * subsequent IOMMU mapping of these BAR vaddr. 1G for x86 is the
+	 * largest hugepage size across any architecture, so no benefit from
+	 * larger alignment. BARs smaller than 1G will be aligned by their
+	 * power-of-two size, guaranteeing sufficient alignment for smaller
+	 * hugepages, if present.
+	 */
+	align = min_t(size_t, size, SZ_1G);
+
+	vaddr = mmap_aligned(size, align, 0);
+	bar->vaddr = mmap(vaddr, 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)
 
Signature · 1 line
2.47.3
#5 lore raw

PATCH v2 3/3 vfio: selftests: Add vfio_dma_mapping_mmio_test

In reply to Alex Mastro

Test IOMMU mapping the BAR mmaps created during vfio_pci_device_setup().

All IOMMU modes are tested: vfio_type1 variants are expected to succeed, while non-type1 modes are expected to fail. iommufd compat mode can be updated to expect success once kernel support lands; native iommufd will not support mapping vaddrs backed by MMIO (it will support dma-buf based MMIO mapping instead).

---
 tools/testing/selftests/vfio/Makefile              |   1 +
 .../selftests/vfio/vfio_dma_mapping_mmio_test.c    | 144 +++++++++++++++++++++
 2 files changed, 145 insertions(+)
tools/testing/selftests/vfio/Makefile +1 -0
index 3c796ca99a50..ead27892ab65 100644
@@ -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/vfio_dma_mapping_mmio_test.c new file +144 -0

144 lines not on this page · raw message

Signature · 1 line
2.47.3
maintainer
#6 lore raw

Re: [PATCH v2 3/3] vfio: selftests: Add vfio_dma_mapping_mmio_test

tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c line 76

Quoted 12 lines · Alex Mastro
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +12 -0
+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 = largest_mapped_bar(self->device);
+
+	if (!self->bar)
+		SKIP(return, "No mappable BAR found on device %s", device_bdf);
+
+	if (self->bar->info.size < 2 * getpagesize())
+		SKIP(return, "BAR too small (size=0x%llx)", self->bar->info.size);

It seems like the selftest should only skip map_partial_bar if the BAR is less than 2 pages. map_full_bar would still be a valid test to run.

#7 lore raw

Re: [PATCH v2 3/3] vfio: selftests: Add vfio_dma_mapping_mmio_test

Quoted 17 lines · David Matlack

tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c line 76

Quoted 12 lines · Alex Mastro
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file +12 -0
+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 = largest_mapped_bar(self->device);
+
+	if (!self->bar)
+		SKIP(return, "No mappable BAR found on device %s", device_bdf);
+
+	if (self->bar->info.size < 2 * getpagesize())
+		SKIP(return, "BAR too small (size=0x%llx)", self->bar->info.size);

It seems like the selftest should only skip map_partial_bar if the BAR is less than 2 pages. map_full_bar would still be a valid test to run.

True. This was me being lax.