Skip to contentSkip to conversation

selftests/vfio: Add sPAPR TCE v2 coverage

RFC PATCH In review Narayana Murty N 4 participants 24 messages Jul 3 – Jul 10

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover selftests/vfio: Add sPAPR TCE v2 coverage

This RFC adds PowerPC sPAPR TCE v2 support to VFIO selftests.

The series adds a selftest IOMMU mode for the legacy VFIO container backend, adds helpers for default and dynamic DMA windows, and exercises the sPAPR DDW path through the DMA mapping tests.

sPAPR TCE v2 remains a legacy VFIO container backend. This series does not add iommufd or VFIO cdev support for sPAPR.

The main points are
1. add VFIO_SELFTESTS_IOMMU_MODE for selecting a backend
2. add vfio_spapr_tce_v2_iommu as the PowerPC VFIO backend
3. preserve the platform default DMA window
4. create and tear down only selftest-created DDWs
5. prepare the DMA window before IOVA allocation
6. exercise DDW creation for hugepage DMA mappings
7. accept sPAPR-specific errno for invalid DMA ranges

Example:

VFIO_SELFTESTS_IOMMU_MODE=vfio_spapr_tce_v2_iommu
./vfio_dma_mapping_test <BDF>

Observed coverage includes anonymous mappings through the default window and 2MB hugetlb mappings through a DDW. 1GB hugetlb mappings may skip when the platform rejects the requested DDW characteristics.

Feedback is requested from the VFIO and PowerPC communities on:
1. whether the sPAPR TCE v2 helpers should remain in the common VFIO selftest library or move into a sPAPR-specific test helper?
2. whether DDW setup through hugepage mappings is the right initial coverage?
3. whether the sPAPR-specific overflow errno expectation should be accepted, skipped, or hidden behind a backend helper?
4. whether PowerPC should generate only the sPAPR TCE v2 fixture variants, or keep the generic IOMMU-mode matrix and skip unsupported modes.

TODO: vfio_spapr_tce_v1_iommu yet to be covered.

Narayana Murty N (6):
selftests/vfio: allow selecting IOMMU backend from environment selftests/vfio: add sPAPR TCE v2 IOMMU mode selftests/vfio: add sPAPR TCE v2 DMA window helpers selftests/vfio: Exercise sPAPR DDW path for hugepage DMA mappings selftests/vfio: Accept sPAPR errno for DMA range overflow selftests/vfio: Enable VFIO selftests on ppc64 and ppc64le

 .../selftests/vfio/lib/include/libvfio.h      |   9 +
 .../vfio/lib/include/libvfio/iommu.h          |  26 ++
 tools/testing/selftests/vfio/lib/iommu.c      | 280 +++++++++++++++++-
 tools/testing/selftests/vfio/lib/libvfio.c    |  37 +++
 .../selftests/vfio/vfio_dma_mapping_test.c    |  30 ++
 .../selftests/vfio/vfio_pci_device_test.c     |   4 +-
 tools/testing/selftests/vfio/Makefile         |   2 +-
 7 files changed, 382 insertions(+), 6 deletions(-)
Signature · 1 line
2.51.1
#4 lore raw

RFC PATCH 2/6 selftests/vfio: add sPAPR TCE v2 IOMMU mode

Add a PowerPC-specific VFIO selftest IOMMU mode for sPAPR TCE v2.

The new mode uses the legacy VFIO container interface with VFIO_SPAPR_TCE_v2_IOMMU. It does not use iommufd or VFIO cdev.

On PowerPC, generate the sPAPR TCE v2 fixture variant so the tests exercise the backend that is valid for this platform.

---
 tools/testing/selftests/vfio/lib/include/libvfio/iommu.h | 8 ++++++++
 tools/testing/selftests/vfio/lib/iommu.c                 | 7 +++++++
 2 files changed, 15 insertions(+)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +8 -0
index e9a3386a4719..ba027a7270d0 100644
@@ -66,17 +66,25 @@ struct iommu_iova_range *iommu_iova_ranges(struct iommu *iommu, u32 *nranges);
 #define MODE_IOMMUFD_COMPAT_TYPE1 "iommufd_compat_type1"
 #define MODE_IOMMUFD_COMPAT_TYPE1V2 "iommufd_compat_type1v2"
 #define MODE_IOMMUFD "iommufd"
+#ifdef __powerpc__
+#define MODE_VFIO_SPAPR_TCE_V2_IOMMU "vfio_spapr_tce_v2_iommu"
+#endif
 
 /*
  * Generator for VFIO selftests fixture variants that replicate across all
  * possible IOMMU modes. Tests must define FIXTURE_VARIANT_ADD_IOMMU_MODE()
  * which should then use FIXTURE_VARIANT_ADD() to create the variant.
  */
+#ifdef __powerpc__
+#define FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(...) \
+FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_spapr_tce_v2_iommu, ##__VA_ARGS__)
+#else
 #define FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(...) \
 FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1_iommu, ##__VA_ARGS__); \
 FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__); \
 FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1, ##__VA_ARGS__); \
 FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1v2, ##__VA_ARGS__); \
 FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd, ##__VA_ARGS__)
+#endif
 
 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_IOMMU_H */
tools/testing/selftests/vfio/lib/iommu.c +7 -0
index 035dac069d60..e86457a715bf 100644
@@ -47,6 +47,13 @@ static const struct iommu_mode iommu_modes[] = {
 	{
 		.name = MODE_IOMMUFD,
 	},
+#ifdef __powerpc__
+	{
+		.name = MODE_VFIO_SPAPR_TCE_V2_IOMMU,
+		.container_path = "/dev/vfio/vfio",
+		.iommu_type = VFIO_SPAPR_TCE_v2_IOMMU,
+	},
+#endif
 };
 
 static const struct iommu_mode *lookup_iommu_mode(const char *iommu_mode)
Signature · 1 line
2.51.1
#1 lore raw

RFC PATCH 1/6 selftests/vfio: allow selecting IOMMU backend from environment

In reply to Narayana Murty N

Add support for selecting the IOMMU mode through the environment variable VFIO_SELFTESTS_IOMMU_MODE. This allows tests to be run with different IOMMU backends without modifying test code.

The environment variable is validated against the list of supported modes. On PowerPC, this includes the sPAPR TCE v2 mode which will be added in a subsequent patch.

If the environment variable is not set, tests use their default mode, preserving existing behavior.

---
 .../selftests/vfio/lib/include/libvfio.h      |  9 +++++
 tools/testing/selftests/vfio/lib/libvfio.c    | 39 +++++++++++++++++++
 .../selftests/vfio/vfio_pci_device_test.c     |  4 +-
 3 files changed, 50 insertions(+), 2 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio.h +9 -0
index 07862b470777..c0b4a2ab06c5 100644
@@ -24,6 +24,15 @@
 const char *vfio_selftests_get_bdf(int *argc, char *argv[]);
 char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs);
 
+/*
+ * Return the IOMMU mode string from the environment variable
+ * $VFIO_SELFTESTS_IOMMU_MODE.
+ *
+ * Returns NULL if the environment variable is not set, allowing
+ * tests to use their default mode.
+ */
+const char *vfio_selftests_get_iommu_mode(void);
+
 /*
  * Reserve virtual address space of size at an address satisfying
  * (vaddr % align) == offset.
tools/testing/selftests/vfio/lib/libvfio.c +39 -0
index 3a3d1ed635c1..617c8dc7288c 100644
@@ -101,3 +101,42 @@ void *mmap_reserve(size_t size, size_t align, size_t offset)
 
 	return map_align;
 }
+
+static const char *get_iommu_mode_env(void)
+{
+	const char *mode;
+	static const char * const valid_modes[] = {
+		MODE_VFIO_TYPE1_IOMMU,
+		MODE_VFIO_TYPE1V2_IOMMU,
+		MODE_IOMMUFD_COMPAT_TYPE1,
+		MODE_IOMMUFD_COMPAT_TYPE1V2,
+		MODE_IOMMUFD,
+#ifdef __powerpc__
+		MODE_VFIO_SPAPR_TCE_V2_IOMMU,
+#endif
+	};
+	int i;
+
+	mode = getenv("VFIO_SELFTESTS_IOMMU_MODE");
+	if (!mode)
+		return NULL;
+
+	for (i = 0; i < ARRAY_SIZE(valid_modes); i++) {
+		if (!strcmp(mode, valid_modes[i]))
+			return mode;
+	}
+
+	fprintf(stderr, "Invalid VFIO_SELFTESTS_IOMMU_MODE: %s\n", mode);
+	fprintf(stderr, "Valid modes are:\n");
+	for (i = 0; i < ARRAY_SIZE(valid_modes); i++)
+		fprintf(stderr, "  %s\n", valid_modes[i]);
+	exit(KSFT_SKIP);
+}
+
+const char *vfio_selftests_get_iommu_mode(void)
+{
+	return get_iommu_mode_env();
+}
+
+	return map_align;
+}
tools/testing/selftests/vfio/vfio_pci_device_test.c +2 -2
index 93c11fd5e081..45b8bf60527a 100644
@@ -29,7 +29,7 @@ FIXTURE(vfio_pci_device_test) {
 
 FIXTURE_SETUP(vfio_pci_device_test)
 {
-	self->iommu = iommu_init(default_iommu_mode);
+	self->iommu = iommu_init(vfio_selftests_get_iommu_mode());
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
 }
 
@@ -121,7 +121,7 @@ FIXTURE_VARIANT_ADD(vfio_pci_irq_test, msix) {
 
 FIXTURE_SETUP(vfio_pci_irq_test)
 {
-	self->iommu = iommu_init(default_iommu_mode);
+	self->iommu = iommu_init(vfio_selftests_get_iommu_mode());
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
 }
 
Signature · 1 line
2.51.1
#6 lore raw

RFC PATCH 3/6 selftests/vfio: add sPAPR TCE v2 DMA window helpers

In reply to Narayana Murty N

Add helper support for sPAPR TCE v2 DMA windows in the VFIO selftest library.

Track the platform default DMA window separately from selftest-created dynamic DMA windows. The default window is discovered with VFIO_IOMMU_SPAPR_TCE_GET_INFO and is not removed during cleanup.

Add helpers to create and remove DDWs, return the active IOVA range, and register/unregister memory around DMA map/unmap operations. Window selection is done before IOVA allocation; the map path only validates, registers memory, and calls VFIO_IOMMU_MAP_DMA.

---
 .../vfio/lib/include/libvfio/iommu.h          |  18 ++
 tools/testing/selftests/vfio/lib/iommu.c      | 273 +++++++++++++++++-
 tools/testing/selftests/vfio/lib/libvfio.c    |   2 -
 3 files changed, 288 insertions(+), 5 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +18 -0
index ba027a7270d0..c9b9ab929f1f 100644
@@ -24,12 +24,26 @@ struct dma_region {
 	u64 size;
 };
 
+struct spapr_tce_window {
+	u64 start;
+	u64 size;
+	u32 page_shift;
+	bool valid;
+	bool dynamic;
+	bool remove_on_cleanup;
+};
+
 struct iommu {
 	const struct iommu_mode *mode;
 	int container_fd;
 	int iommufd;
 	u32 ioas_id;
 	struct list_head dma_regions;
+#ifdef __powerpc__
+	struct spapr_tce_window default_window;
+	struct spapr_tce_window ddw_window;
+	struct spapr_tce_window *active_window;
+#endif
 };
 
 struct iommu *iommu_init(const char *iommu_mode);
@@ -61,6 +75,10 @@ iova_t iommu_hva2iova(struct iommu *iommu, void *vaddr);
 
 struct iommu_iova_range *iommu_iova_ranges(struct iommu *iommu, u32 *nranges);
 
+int iommu_prepare_dma_window(struct iommu *iommu, u64 min_size,
+			     u64 page_size, bool force_dynamic);
+bool iommu_supports_unmap_all(struct iommu *iommu);
+
 #define MODE_VFIO_TYPE1_IOMMU "vfio_type1_iommu"
 #define MODE_VFIO_TYPE1V2_IOMMU "vfio_type1v2_iommu"
 #define MODE_IOMMUFD_COMPAT_TYPE1 "iommufd_compat_type1"
tools/testing/selftests/vfio/lib/iommu.c +270 -3

314 lines not on this page · raw message

tools/testing/selftests/vfio/lib/libvfio.c +0 -2
index 617c8dc7288c..12f731963b05 100644
@@ -138,5 +138,3 @@ const char *vfio_selftests_get_iommu_mode(void)
 	return get_iommu_mode_env();
 }
 
-	return map_align;
-}
Signature · 1 line
2.51.1
#10 lore raw

RFC PATCH 4/6 selftests/vfio: Exercise sPAPR DDW path for hugepage DMA mappings

In reply to Narayana Murty N

Prepare the DMA window before IOVA allocation in the DMA mapping tests.

Anonymous mappings use the default sPAPR DMA window. HugeTLB mappings force a DDW with the requested page size, allowing the test to exercise DDW creation, mapping, unmapping, and cleanup.

Skip gracefully when hugepage allocation fails or when the platform does not support the requested DDW characteristics. Also skip unmap-all when the backend cannot pair it with the sPAPR memory unregister flow.

---
 .../selftests/vfio/vfio_dma_mapping_test.c    | 22 +++++++++++++++++++
 1 file changed, 22 insertions(+)
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +22 -0
index 7d0de8c79de1..4411fdbd56da 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <stdio.h>
+#include <string.h>
 #include <sys/mman.h>
 #include <unistd.h>
 
@@ -119,8 +120,19 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |
 
 FIXTURE_SETUP(vfio_dma_mapping_test)
 {
+	const u64 page_size = variant->size ?: getpagesize();
+	const bool force_dynamic = !!variant->size;
+	int ret;
+
 	self->iommu = iommu_init(variant->iommu_mode);
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
+
+	ret = iommu_prepare_dma_window(self->iommu, page_size, page_size,
+				       force_dynamic);
+	if (ret)
+		SKIP(return, "DMA window unavailable: %s (%d)\n",
+		     strerror(-ret), -ret);
+
 	self->iova_allocator = iova_allocator_init(self->iommu);
 }
 
@@ -227,6 +239,7 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
 	u64 region_size = getpagesize();
 	iova_t last_iova;
 	u32 nranges;
+	int ret;
 
 	/*
 	 * Over-allocate mmap by double the size to provide enough backing vaddr
@@ -236,6 +249,13 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
 
 	self->iommu = iommu_init(variant->iommu_mode);
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
+
+	ret = iommu_prepare_dma_window(self->iommu, region_size, region_size,
+				       false);
+	if (ret)
+		SKIP(return, "DMA window unavailable: %s (%d)\n",
+		     strerror(-ret), -ret);
+
 	region->vaddr = mmap(NULL, self->mmap_size, PROT_READ | PROT_WRITE,
 			     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
 	ASSERT_NE(region->vaddr, MAP_FAILED);
@@ -277,6 +297,8 @@ TEST_F(vfio_dma_map_limit_test, unmap_all)
 	u64 unmapped;
 	int rc;
 
+	if (!iommu_supports_unmap_all(self->iommu))
+		SKIP(return, "IOMMU backend does not support unmap-all\n");
 	iommu_map(self->iommu, region);
 	ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr));
 
Signature · 1 line
2.51.1
#14 lore raw

RFC PATCH 5/6 selftests/vfio: Accept sPAPR errno for DMA range overflow

In reply to Narayana Murty N

Accept sPAPR errno for DMA range overflow

The DMA map limit test expects an overflowing IOVA range to fail with -EOVERFLOW. That matches the Type1 and iommufd paths.

On sPAPR TCE v2, the same invalid range is rejected as outside the active TCE window and currently returns -ENXIO. Treat that as the expected ppc result for this RFC while keeping the existing -EOVERFLOW expectation for other backends.

This keeps the overflow test enabled for sPAPR TCE v2 instead of skipping it.

---
 tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +8 -0
index 4411fdbd56da..cd2d3276a46c 100644
@@ -316,10 +316,18 @@ TEST_F(vfio_dma_map_limit_test, overflow)
 	region->size = self->mmap_size;
 
 	rc = __iommu_map(self->iommu, region);
+#ifdef __powerpc__
+	ASSERT_EQ(rc, -ENXIO);
+#else
 	ASSERT_EQ(rc, -EOVERFLOW);
+#endif
 
 	rc = __iommu_unmap(self->iommu, region, NULL);
+#ifdef __powerpc__
+	ASSERT_EQ(rc, -ENXIO);
+#else
 	ASSERT_EQ(rc, -EOVERFLOW);
+#endif
 }
 
 int main(int argc, char *argv[])
Signature · 1 line
2.51.1
#17 lore raw

RFC PATCH 6/6 selftests/vfio: Enable VFIO selftests on ppc64 and ppc64le

In reply to Narayana Murty N

Enable VFIO selftests to build on ppc64 and ppc64le made the changes to Makefile to include ppc64 and ppc64le.

With sPAPR TCE v2 support added to the VFIO selftest library, PowerPC systems can run the DMA mapping tests against the legacy VFIO container backend.

---
 tools/testing/selftests/vfio/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/vfio/Makefile +1 -1
index 2c32c48db509..8fd6afd334e5 100644
@@ -1,6 +1,6 @@
 ARCH ?= $(shell uname -m)
 
-ifeq (,$(filter $(ARCH),aarch64 arm64 x86 x86_64))
+ifeq (,$(filter $(ARCH),aarch64 arm64 x86 x86_64 ppc64 ppc64le))
 # Do nothing on unsupported architectures
 include ../lib.mk
 else
Signature · 1 line
2.51.1
#7 lore raw

Re: [RFC PATCH 3/6] selftests/vfio: add sPAPR TCE v2 DMA window helpers

In reply to Narayana Murty N
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] `vfio_iommu_map()` incorrectly returns a clobbered `errno` if `VFIO_IOMMU_MAP_DMA` fails.…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] vfio_iommu_map() incorrectly returns a clobbered errno if VFIO_IOMMU_MAP_DMA fails.
- [Medium] If memory unregistration fails during unmap, the region is left in the dma_regions list despite being successfully unmapped in the IOMMU.
- [Medium] Failed DDW creation leaves iommu->active_window pointing to an invalidated window, leading to stale IOVA ranges being returned. --

commit 2deeaa397a1d09614f068ff5ae82a0732c573cd4
Author: Narayana Murty N <[email protected]>

selftests/vfio: add sPAPR TCE v2 DMA window helpers

This commit adds helper support for sPAPR TCE v2 DMA windows in the VFIO selftest library. It tracks the platform default DMA window separately from selftest-created dynamic DMA windows, and adds helpers to create, remove, and register memory around DMA map/unmap operations.

Quoted 4 lines
tools/testing/selftests/vfio/lib/iommu.c +0 -0
index e86457a715bf7..19d19bb45a14b 100644

[ ... ]

tools/testing/selftests/vfio/lib/iommu.c int __iommu_hva2iova(struct iommu *iommu, void *vaddr, iova_t *iova) · line 303

Quoted 11 lines
tools/testing/selftests/vfio/lib/iommu.c +11 -0
+	/*
+	 * Neither default nor DDW is sufficient.
+	 * Remove only the selftest-created DDW, then create a new DDW.
+	 */
+	ret = spapr_tce_remove_window(iommu, &iommu->ddw_window);
+	if (ret)
+		return ret;
+
+	ret = spapr_tce_create_ddw(iommu, min_size, page_shift);
+	if (ret)
+		return ret;

[Severity: Medium]
If spapr_tce_create_ddw() fails here, does it leave iommu->active_window pointing to a window that was just invalidated by spapr_tce_remove_window()?

If iommu->active_window was previously set to &iommu->ddw_window, it is not reset to NULL on this error path. Later, spapr_tce_iova_ranges() reads iommu->active_window->start without checking if the window is valid:

static struct iommu_iova_range *spapr_tce_iova_ranges(...)

{
        ...
        ranges[0].start = iommu->active_window->start;
        ranges[0].last = iommu->active_window->start +
                         iommu->active_window->size - 1;
        ...
}

Could this return stale IOVA ranges to the caller?

Quoted 4 lines
tools/testing/selftests/vfio/lib/iommu.c +4 -0
+
+	iommu->active_window = &iommu->ddw_window;
+	return 0;
+}

[ ... ]

Quoted 16 lines
tools/testing/selftests/vfio/lib/iommu.c +10 -1
@@ -113,9 +342,27 @@ static int vfio_iommu_map(struct iommu *iommu, struct dma_region *region)
 		.iova = region->iova,
 		.size = region->size,
 	};
+	int ret;
 
-	if (ioctl(iommu->container_fd, VFIO_IOMMU_MAP_DMA, &args))
+#ifdef __powerpc__
+	if (iommu_is_spapr_tce_v2(iommu)) {
+		if (!spapr_tce_iova_inside_window(iommu, region))
+			return -EINVAL;
+
+		ret = spapr_register_memory(iommu, region);
+		if (ret)
+			return ret;
+	}

tools/testing/selftests/vfio/lib/iommu.c static int vfio_iommu_map(struct iommu *iommu, struct dma_region *region) · line 365

Quoted 10 lines
tools/testing/selftests/vfio/lib/iommu.c +9 -0
+#endif
+
+	ret = ioctl(iommu->container_fd, VFIO_IOMMU_MAP_DMA, &args);
+	if (ret) {
+#ifdef __powerpc__
+		if (iommu_is_spapr_tce_v2(iommu))
+			spapr_unregister_memory(iommu, region);
+#endif
 		return -errno;
+	}

[Severity: Medium]
Can the cleanup call to spapr_unregister_memory() clobber the errno from the failed VFIO_IOMMU_MAP_DMA ioctl?

Since spapr_unregister_memory() performs its own ioctl, it might overwrite the thread-local errno. Will this result in vfio_iommu_map() returning the result of the cleanup operation instead of the actual mapping failure?

Quoted 3 lines
tools/testing/selftests/vfio/lib/iommu.c +0 -0
 	return 0;
 }

[ ... ]

Quoted 9 lines
tools/testing/selftests/vfio/lib/iommu.c +3 -2
@@ -177,8 +424,18 @@ static int __vfio_iommu_unmap(int fd, u64 iova, u64 size, u32 flags, u64 *unmapp
 static int vfio_iommu_unmap(struct iommu *iommu, struct dma_region *region,
 			    u64 *unmapped)
 {
-	return __vfio_iommu_unmap(iommu->container_fd, region->iova,
-				  region->size, 0, unmapped);
+	int ret;
+
+	ret = __vfio_iommu_unmap(iommu->container_fd, region->iova,

tools/testing/selftests/vfio/lib/iommu.c static int __vfio_iommu_unmap(int fd, u64 iova, u64 size, u32 flags, u64 *unmapp · line 439

Quoted 10 lines
tools/testing/selftests/vfio/lib/iommu.c +9 -0
+				 region->size, 0, unmapped);
+	if (ret)
+		return ret;
+#ifdef __powerpc__
+	if (iommu_is_spapr_tce_v2(iommu))
+		ret = spapr_unregister_memory(iommu, region);
+#endif
+
+	return ret;
 }

[Severity: Medium]
If __vfio_iommu_unmap() succeeds but spapr_unregister_memory() fails, does returning this error bypass the region cleanup in __iommu_unmap()?

In __iommu_unmap():

    if (iommu->iommufd)
        ret = iommufd_unmap(iommu, region, unmapped);
    else
        ret = vfio_iommu_unmap(iommu, region, unmapped);

    if (ret)
        return ret;

    list_del_init(&region->link);

If we return the unregistration failure here, the early return in __iommu_unmap() skips list_del_init(). Will this leave the successfully unmapped region in the dma_regions list and corrupt internal state tracking?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#2 lore raw

Re: [RFC PATCH 1/6] selftests/vfio: allow selecting IOMMU backend from environment

In reply to Narayana Murty N
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] A syntax error is introduced at the end of the file due to stray duplicated code. - [Low] The macro…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] A syntax error is introduced at the end of the file due to stray duplicated code.
- [Low] The macro MODE_VFIO_SPAPR_TCE_V2_IOMMU is referenced but never defined, breaking the build on PowerPC. --

commit 711defe4ecc8034b88efefbbf07a52494153b474
Author: Narayana Murty N <[email protected]>

selftests/vfio: allow selecting IOMMU backend from environment

This commit adds support for selecting the IOMMU mode through the VFIO_SELFTESTS_IOMMU_MODE environment variable. It validates the environment variable against a list of supported modes, including sPAPR TCE v2 on PowerPC.

Quoted 4 lines
tools/testing/selftests/vfio/lib/libvfio.c +0 -0
index 3a3d1ed635c1d..617c8dc7288ca 100644

[ ... ]

Quoted 6 lines
tools/testing/selftests/vfio/lib/libvfio.c +2 -0
@@ -101,3 +101,42 @@ void *mmap_reserve(size_t size, size_t align, size_t offset)
 
 	return map_align;
 }
+
+static const char *get_iommu_mode_env(void)

tools/testing/selftests/vfio/lib/libvfio.c void *mmap_reserve(size_t size, size_t align, size_t offset) · line 115

Quoted 10 lines
tools/testing/selftests/vfio/lib/libvfio.c +10 -0
+{
+	const char *mode;
+	static const char * const valid_modes[] = {
+		MODE_VFIO_TYPE1_IOMMU,
+		MODE_VFIO_TYPE1V2_IOMMU,
+		MODE_IOMMUFD_COMPAT_TYPE1,
+		MODE_IOMMUFD_COMPAT_TYPE1V2,
+		MODE_IOMMUFD,
+#ifdef __powerpc__
+		MODE_VFIO_SPAPR_TCE_V2_IOMMU,

[Severity: Low]
Does this break the build on PowerPC because MODE_VFIO_SPAPR_TCE_V2_IOMMU is referenced but never defined?

I notice this is fixed in a later commit 8dfc3e7df9b7153c07746fbc4679447d70174937 ("selftests/vfio: add sPAPR TCE v2 IOMMU mode"), but introducing an undefined identifier here will cause git bisect failures for PowerPC builds.

Quoted 2 lines
tools/testing/selftests/vfio/lib/libvfio.c +2 -0
+#endif
+	};

[ ... ]

tools/testing/selftests/vfio/lib/libvfio.c void *mmap_reserve(size_t size, size_t align, size_t offset) · line 142

Quoted 7 lines
tools/testing/selftests/vfio/lib/libvfio.c +7 -0
+const char *vfio_selftests_get_iommu_mode(void)
+{
+	return get_iommu_mode_env();
+}
+
+	return map_align;
+}

[Severity: Low]
Is this stray return statement and closing brace a copy-paste error?

It causes a syntax error at the file scope. I see this is removed in a later commit 2deeaa397a1d09614f068ff5ae82a0732c573cd4 ("selftests/vfio: add sPAPR TCE v2 DMA window helpers"), but having it in this patch breaks the build and git bisect.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#15 lore raw

Re: [RFC PATCH 5/6] selftests/vfio: Accept sPAPR errno for DMA range overflow

In reply to Narayana Murty N
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Architecture-specific macro breaks test execution with alternative IOMMU backends on PowerPC.…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Architecture-specific macro breaks test execution with alternative IOMMU backends on PowerPC.
- [Medium] The test's expectation of -ENXIO codifies and relies upon an integer overflow bug in the test library's user-space validation helper. --

commit 93dbee7bb3c363a49eb5141c94b3669c7520cd26
Author: Narayana Murty N <[email protected]>

selftests/vfio: Accept sPAPR errno for DMA range overflow

The DMA map limit test expects an overflowing IOVA range to fail with -EOVERFLOW. This commit modifies the test to expect -ENXIO when compiled for powerpc to account for sPAPR TCE v2 rejecting the range as outside the active TCE window.

tools/testing/selftests/vfio/vfio_dma_mapping_test.c TEST_F(vfio_dma_map_limit_test, overflow) · line 320

Quoted 10 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +2 -0
index 4411fdbd56da5..cd2d3276a46c4 100644
@@ -316,10 +316,18 @@ TEST_F(vfio_dma_map_limit_test, overflow)
 	region->size = self->mmap_size;
 
 	rc = __iommu_map(self->iommu, region);
+#ifdef __powerpc__
+	ASSERT_EQ(rc, -ENXIO);

[Severity: Medium]
Will this architecture-specific macro break test execution if an alternative IOMMU backend is used on PowerPC?

Since earlier patches in this series allow selecting the IOMMU backend via the VFIO_SELFTESTS_IOMMU_MODE environment variable, running the test on PowerPC with iommufd would return the generic -EOVERFLOW error. Because of the #ifdef, the test would erroneously expect -ENXIO and fail.

Would it be better to base this branch on the active backend type (e.g., inspecting variant->iommu_mode) rather than the compilation target?

[Severity: Medium]
Does expecting -ENXIO here codify a bug in the user-space validation helper?

In the overflow test, the region->iova is set to a large value so that region->iova + region->size wraps around to 0.

Looking at spapr_tce_iova_inside_window() introduced earlier in the series, it evaluates:

tools/testing/selftests/vfio/lib/iommu.c:spapr_tce_iova_inside_window() {
    ...
    if (region->iova + region->size > window->start + window->size)
        return false;
    ...
}

Due to the wrap-around, 0 > 0 + window->size evaluates to false, causing the validation helper to return true. This bypasses user-space validation, allowing the invalid IOVA to be sent to the kernel via VFIO_IOMMU_MAP_DMA, which then rejects it with -ENXIO.

If the user-space bounds check is fixed to catch wrap-arounds (for example, checking if region->iova + region->size < region->iova), the helper would correctly return false and the user-space map attempt would return -EINVAL.

Would that fix cause this test to fail because it now explicitly expects -ENXIO?

Quoted 11 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +6 -0
+#else
 	ASSERT_EQ(rc, -EOVERFLOW);
+#endif
 
 	rc = __iommu_unmap(self->iommu, region, NULL);
+#ifdef __powerpc__
+	ASSERT_EQ(rc, -ENXIO);
+#else
 	ASSERT_EQ(rc, -EOVERFLOW);
+#endif
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#5 lore raw

Re: [RFC PATCH 2/6] selftests/vfio: add sPAPR TCE v2 IOMMU mode

In reply to Narayana Murty N
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The fallback default IOMMU mode remains hardcoded to MODE_IOMMUFD, causing test suites to crash…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The fallback default IOMMU mode remains hardcoded to MODE_IOMMUFD, causing test suites to crash out-of-the-box on PowerPC where IOMMUFD is not supported for VFIO. --

commit 8dfc3e7df9b7153c07746fbc4679447d70174937
Author: Narayana Murty N <[email protected]>

selftests/vfio: add sPAPR TCE v2 IOMMU mode

Add a PowerPC-specific VFIO selftest IOMMU mode for sPAPR TCE v2. The new mode uses the legacy VFIO container interface with VFIO_SPAPR_TCE_v2_IOMMU.

[ ... ]

Quoted 6 lines
tools/testing/selftests/vfio/lib/iommu.c +0 -0
index 035dac069d601..e86457a715bf7 100644
@@ -47,6 +47,13 @@ static const struct iommu_mode iommu_modes[] = {
 	{

tools/testing/selftests/vfio/lib/iommu.c static const struct iommu_mode iommu_modes[] = { · line 57

Quoted 10 lines
tools/testing/selftests/vfio/lib/iommu.c +7 -0
 		.name = MODE_IOMMUFD,
 	},
+#ifdef __powerpc__
+	{
+		.name = MODE_VFIO_SPAPR_TCE_V2_IOMMU,
+		.container_path = "/dev/vfio/vfio",
+		.iommu_type = VFIO_SPAPR_TCE_v2_IOMMU,
+	},
+#endif
 };

[Severity: Medium]
Since default_iommu_mode remains hardcoded to MODE_IOMMUFD earlier in this file, does this cause test suites like vfio_pci_driver_test to crash out-of-the-box on PowerPC systems?

For instance, device_has_selftests_driver() uses this default mode directly before checking platform-specific fixtures:

tools/testing/selftests/vfio/vfio_pci_driver_test.c:device_has_selftests_driver() {
    ...
    iommu = iommu_init(default_iommu_mode);
    ...
}

If /dev/iommu cannot be opened or device bindings fail on PowerPC because IOMMUFD isn't supported, does this trigger a failed assertion (VFIO_ASSERT_GT(iommu->iommufd, 0)) and abort execution rather than skipping gracefully?

Should default_iommu_mode be conditionally defined for PowerPC to use MODE_VFIO_SPAPR_TCE_V2_IOMMU as the default fallback?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#11 lore raw

Re: [RFC PATCH 4/6] selftests/vfio: Exercise sPAPR DDW path for hugepage DMA mappings

In reply to Narayana Murty N
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The selftest helper `spapr_tce_iova_inside_window` fails to check for arithmetic overflow when…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The selftest helper spapr_tce_iova_inside_window fails to check for arithmetic overflow when validating whether an IOVA range fits within the window. --

commit 09ce4770fae5d9e1ed108bb9feb0bc727a9b8bcc
Author: Narayana Murty N <[email protected]>

selftests/vfio: Exercise sPAPR DDW path for hugepage DMA mappings

Prepare the DMA window before IOVA allocation in the DMA mapping tests. Anonymous mappings use the default sPAPR DMA window, while HugeTLB mappings force a DDW with the requested page size. The tests are skipped gracefully if allocation fails or if the platform lacks support.

Quoted 4 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +0 -0
index 7d0de8c79de13..4411fdbd56da5 100644

[ ... ]

tools/testing/selftests/vfio/vfio_dma_mapping_test.c FIXTURE_SETUP(vfio_dma_map_limit_test) · line 254

Quoted 7 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +3 -0
@@ -236,6 +249,13 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
 
 	self->iommu = iommu_init(variant->iommu_mode);
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
+
+	ret = iommu_prepare_dma_window(self->iommu, region_size, region_size,
+				       false);

[Severity: Medium]
Will this setup cause the test suite to silently pass invalid mappings in the overflow test?

In tools/testing/selftests/vfio/lib/iommu.c, spapr_tce_iova_inside_window() checks bounds using:

    if (region->iova + region->size > window->start + window->size)
        return false;

When the vfio_dma_map_limit_test's overflow test constructs a wrapping IOVA (region->iova = ~(iova_t)0 & ~(region->size - 1)), region->iova + region->size will wrap around to 0.

Since 0 is not greater than window->start + window->size, the check incorrectly evaluates to false, making the function return true. Does this missing overflow check defeat the wrapper's bounds validation and pass invalid mapping requests down to the kernel ioctl?

Quoted 7 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +4 -0
+	if (ret)
+		SKIP(return, "DMA window unavailable: %s (%d)\n",
+		     strerror(-ret), -ret);
+
 	region->vaddr = mmap(NULL, self->mmap_size, PROT_READ | PROT_WRITE,
 			     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
 	ASSERT_NE(region->vaddr, MAP_FAILED);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#18 lore raw

Re: [RFC PATCH 6/6] selftests/vfio: Enable VFIO selftests on ppc64 and ppc64le

In reply to Narayana Murty N
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The VFIO selftests will still be silently skipped on PowerPC when building from the top-level…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The VFIO selftests will still be silently skipped on PowerPC when building from the top-level kernel directory (e.g., via make kselftest). --

commit e8d7fbeb96b4d48f400e3fc2cba19a75370e70e6
Author: Narayana Murty N <[email protected]>

selftests/vfio: Enable VFIO selftests on ppc64 and ppc64le

This commit modifies the VFIO selftests Makefile to enable building on PowerPC architectures. It allows PowerPC systems to run DMA mapping tests against the legacy VFIO container backend.

tools/testing/selftests/vfio/Makefile line 3

Quoted 9 lines
tools/testing/selftests/vfio/Makefile +1 -1
index e6e8cb52ab030..dc686a38ab0a9 100644
@@ -1,6 +1,6 @@
 ARCH ?= $(shell uname -m)
 
-ifeq (,$(filter $(ARCH),aarch64 arm64 x86 x86_64))
+ifeq (,$(filter $(ARCH),aarch64 arm64 x86 x86_64 ppc64 ppc64le))

[Severity: Medium]
Will this filter correctly match the architecture when building via the top-level make kselftest?

The top-level kernel build system evaluates SUBARCH using scripts/subarch.include, which maps both ppc64 and ppc64le to the canonical name powerpc, exporting ARCH=powerpc to sub-makes.

Because powerpc is not included in this filter list, the tests will still be silently skipped during automated testing when invoked from the top-level kernel directory.

Quoted 3 lines
tools/testing/selftests/vfio/Makefile +0 -0
 # Do nothing on unsupported architectures
 include ../lib.mk
 else
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#19 lore raw
In reply to Narayana Murty N

+ linuxppc-dev

Quoted 56 lines · Narayana Murty N

This RFC adds PowerPC sPAPR TCE v2 support to VFIO selftests.

The series adds a selftest IOMMU mode for the legacy VFIO container backend, adds helpers for default and dynamic DMA windows, and exercises the sPAPR DDW path through the DMA mapping tests.

sPAPR TCE v2 remains a legacy VFIO container backend. This series does not add iommufd or VFIO cdev support for sPAPR.

The main points are
1. add VFIO_SELFTESTS_IOMMU_MODE for selecting a backend
2. add vfio_spapr_tce_v2_iommu as the PowerPC VFIO backend
3. preserve the platform default DMA window
4. create and tear down only selftest-created DDWs
5. prepare the DMA window before IOVA allocation
6. exercise DDW creation for hugepage DMA mappings
7. accept sPAPR-specific errno for invalid DMA ranges

Example:

VFIO_SELFTESTS_IOMMU_MODE=vfio_spapr_tce_v2_iommu
./vfio_dma_mapping_test <BDF>

Observed coverage includes anonymous mappings through the default window and 2MB hugetlb mappings through a DDW. 1GB hugetlb mappings may skip when the platform rejects the requested DDW characteristics.

Feedback is requested from the VFIO and PowerPC communities on:
1. whether the sPAPR TCE v2 helpers should remain in the common VFIO selftest library or move into a sPAPR-specific test helper?
2. whether DDW setup through hugepage mappings is the right initial coverage?
3. whether the sPAPR-specific overflow errno expectation should be accepted, skipped, or hidden behind a backend helper?
4. whether PowerPC should generate only the sPAPR TCE v2 fixture variants, or keep the generic IOMMU-mode matrix and skip unsupported modes.

TODO: vfio_spapr_tce_v1_iommu yet to be covered.

Narayana Murty N (6):
selftests/vfio: allow selecting IOMMU backend from environment selftests/vfio: add sPAPR TCE v2 IOMMU mode selftests/vfio: add sPAPR TCE v2 DMA window helpers selftests/vfio: Exercise sPAPR DDW path for hugepage DMA mappings selftests/vfio: Accept sPAPR errno for DMA range overflow selftests/vfio: Enable VFIO selftests on ppc64 and ppc64le

  .../selftests/vfio/lib/include/libvfio.h      |   9 +
  .../vfio/lib/include/libvfio/iommu.h          |  26 ++
  tools/testing/selftests/vfio/lib/iommu.c      | 280 +++++++++++++++++-
  tools/testing/selftests/vfio/lib/libvfio.c    |  37 +++
  .../selftests/vfio/vfio_dma_mapping_test.c    |  30 ++
  .../selftests/vfio/vfio_pci_device_test.c     |   4 +-
  tools/testing/selftests/vfio/Makefile         |   2 +-
  7 files changed, 382 insertions(+), 6 deletions(-)
maintainer
#3 lore raw

Re: [RFC PATCH 1/6] selftests/vfio: allow selecting IOMMU backend from environment

In reply to Narayana Murty N
Quoted 27 lines · Narayana Murty N

Add support for selecting the IOMMU mode through the environment variable VFIO_SELFTESTS_IOMMU_MODE. This allows tests to be run with different IOMMU backends without modifying test code.

The environment variable is validated against the list of supported modes. On PowerPC, this includes the sPAPR TCE v2 mode which will be added in a subsequent patch.

If the environment variable is not set, tests use their default mode, preserving existing behavior.

tools/testing/selftests/vfio/vfio_pci_device_test.c +1 -1
index 93c11fd5e081..45b8bf60527a 100644
@@ -29,7 +29,7 @@ FIXTURE(vfio_pci_device_test) {
 
 FIXTURE_SETUP(vfio_pci_device_test)
 {
-	self->iommu = iommu_init(default_iommu_mode);
+	self->iommu = iommu_init(vfio_selftests_get_iommu_mode());
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
 }
 

tools/testing/selftests/vfio/vfio_pci_device_test.c FIXTURE_VARIANT_ADD(vfio_pci_irq_test, msix) { · line 126

Quoted 7 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c +1 -1
@@ -121,7 +121,7 @@ FIXTURE_VARIANT_ADD(vfio_pci_irq_test, msix) {
 
 FIXTURE_SETUP(vfio_pci_irq_test)
 {
-	self->iommu = iommu_init(default_iommu_mode);
+	self->iommu = iommu_init(vfio_selftests_get_iommu_mode());
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
 }

Rather than forcing the user to pass in the mode via environment variable, can vfio_selftests_get_iommu_mode() pick a reasonable default based on what the host supports? i.e. Is it possible for vfio_selftests_get_iommu_mode() to now when to pick MODE_VFIO_SPAPR_TCE_V2_IOMMU as the default mode?

The new KVM device-posted interrupt test that uses the VFIO selftests library also wants this to pick between MODE_IOMMUFD and MODE_VFIO_TYPE1_IOMMU.

https://lore.kernel.org/kvm/[email protected]/

And this would also fix VFIO selftests that get run on kernels where IOMMUFD is not enabled.

maintainer
#8 lore raw

Re: [RFC PATCH 3/6] selftests/vfio: add sPAPR TCE v2 DMA window helpers

In reply to Narayana Murty N
Quoted 12 lines · Narayana Murty N

Add helper support for sPAPR TCE v2 DMA windows in the VFIO selftest library.

Track the platform default DMA window separately from selftest-created dynamic DMA windows. The default window is discovered with VFIO_IOMMU_SPAPR_TCE_GET_INFO and is not removed during cleanup.

Add helpers to create and remove DDWs, return the active IOVA range, and register/unregister memory around DMA map/unmap operations. Window selection is done before IOVA allocation; the map path only validates, registers memory, and calls VFIO_IOMMU_MAP_DMA.

Quoted 5 lines
---
 .../vfio/lib/include/libvfio/iommu.h          |  18 ++
 tools/testing/selftests/vfio/lib/iommu.c      | 273 +++++++++++++++++-
 tools/testing/selftests/vfio/lib/libvfio.c    |   2 -

Let's move all the spapr code to its own .c file under lib and only compile it on powerpc, and a new header file so that the code in iommu.c can call into it as needed.

Please also avoid adding ifdefs within function bodies. The generic code in iommu.c should unconditionally call spapr routines and those routines should be stubbed out on non-powerpc architectures.

maintainer
#9 lore raw

Re: [RFC PATCH 3/6] selftests/vfio: add sPAPR TCE v2 DMA window helpers

In reply to Narayana Murty N
Quoted 7 lines · Narayana Murty N

Add helper support for sPAPR TCE v2 DMA windows in the VFIO selftest library.

Track the platform default DMA window separately from selftest-created dynamic DMA windows. The default window is discovered with VFIO_IOMMU_SPAPR_TCE_GET_INFO and is not removed during cleanup.

Quoted 8 lines

Add helpers to create and remove DDWs, return the active IOVA range, and register/unregister memory around DMA map/unmap operations. Window selection is done before IOVA allocation; the map path only validates, registers memory, and calls VFIO_IOMMU_MAP_DMA.

+static int spapr_tce_read_default_window(struct iommu *iommu)

Please add vfio_iommu_*() prefix to these routines that wrap VFIO_IOMMU_* ioctls.

e.g.

spapr_tce_read_default_window() -> vfio_iommu_spapr_tce_get_default()

 spapr_tce_create_ddw()          -> vfio_iommu_spapr_tce_create_window()
 spapr_tce_remove_ddw()          -> vfio_iommu_spapr_tce_remove_window()
 spapr_register_memory()         -> vfio_iommu_spapr_register_memory()
 spapr_unregister_memory()       -> vfio_iommu_spapr_unregister_memory()

This helps with readability since it makes it clear these functions are interacting with the VFIO IOMMU driver.

Quoted 59 lines
tools/testing/selftests/vfio/lib/iommu.c +59 -0
+{
+	struct vfio_iommu_spapr_tce_info info = {
+		.argsz = sizeof(info),
+	};
+
+	if (iommu->default_window.valid)
+		return 0;
+
+	if (ioctl(iommu->container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info))
+		return -errno;
+
+	iommu->default_window.start = info.dma32_window_start;
+	iommu->default_window.size = info.dma32_window_size;
+	iommu->default_window.page_shift = page_size_to_shift(getpagesize());
+	iommu->default_window.valid = true;
+	iommu->default_window.dynamic = false;
+	iommu->default_window.remove_on_cleanup = false;
+
+	return 0;
+}
+
+static int spapr_tce_create_ddw(struct iommu *iommu, u64 min_size, u32 page_shift)
+{
+	struct vfio_iommu_spapr_tce_create create = {
+		.argsz = sizeof(create),
+		.page_shift = page_shift,
+		.levels = 1,
+		.window_size = min_size,
+	};
+
+	if (ioctl(iommu->container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create))
+		return -errno;
+
+	iommu->ddw_window.start = create.start_addr;
+	iommu->ddw_window.size = create.window_size;
+	iommu->ddw_window.page_shift = page_shift;
+	iommu->ddw_window.valid = true;
+	iommu->ddw_window.dynamic = true;
+	iommu->ddw_window.remove_on_cleanup = true;
+
+	return 0;
+}
+
+static int spapr_tce_remove_window(struct iommu *iommu, struct spapr_tce_window *window)
+{
+	struct vfio_iommu_spapr_tce_remove remove = {
+		.argsz = sizeof(remove),
+		.start_addr = window->start,
+	};
+
+	if (!window->valid || !window->remove_on_cleanup)
+		return 0;
+
+	if (ioctl(iommu->container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove))
+		return -errno;
+
+	window->valid = false;
+	return 0;
+}
maintainer
#12 lore raw

Re: [RFC PATCH 4/6] selftests/vfio: Exercise sPAPR DDW path for hugepage DMA mappings

In reply to Narayana Murty N
Quoted 27 lines · Narayana Murty N

Prepare the DMA window before IOVA allocation in the DMA mapping tests.

Anonymous mappings use the default sPAPR DMA window. HugeTLB mappings force a DDW with the requested page size, allowing the test to exercise DDW creation, mapping, unmapping, and cleanup.

Skip gracefully when hugepage allocation fails or when the platform does not support the requested DDW characteristics. Also skip unmap-all when the backend cannot pair it with the sPAPR memory unregister flow.

---
 .../selftests/vfio/vfio_dma_mapping_test.c    | 22 +++++++++++++++++++
 1 file changed, 22 insertions(+)
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +1 -0
index 7d0de8c79de1..4411fdbd56da 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <stdio.h>
+#include <string.h>
 #include <sys/mman.h>
 #include <unistd.h>
 

tools/testing/selftests/vfio/vfio_dma_mapping_test.c FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB | · line 124

Quoted 5 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +2 -0
@@ -119,8 +120,19 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |
 
 FIXTURE_SETUP(vfio_dma_mapping_test)
 {
+	const u64 page_size = variant->size ?: getpagesize();
+	const bool force_dynamic = !!variant->size;

This is kind of arbitrary right? Should we add explicit variants for dynamic/default windows (see below). But we need some way to skip the dynamic window tests on non-spapr iommus...

tools/testing/selftests/vfio/vfio_dma_mapping_test.c +12 -7
index cd2d3276a46c..b54c529ebedb 100644
@@ -103,15 +103,21 @@ FIXTURE_VARIANT(vfio_dma_mapping_test) {
        const char *iommu_mode;
        u64 size;
        int mmap_flags;
+       bool dynamic_window;
 };
 
-#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _size, _mmap_flags) \
-FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, _iommu_mode ## _ ## _name) {               \
-       .iommu_mode = #_iommu_mode,                                            \
-       .size = (_size),                                                       \
-       .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | (_mmap_flags),             \
+#define __FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _size, _mmap_flags, _dynamic_window) \
+FIXTURE_VARIANT_ADD(vfio_dma_mapping_test,_iommu_mode ## _ ## _name) {   \
+       .iommu_mode = #_iommu_mode,                                                               \
+       .size = (_size),                                                                          \
+       .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | (_mmap_flags),                                \
+       .dynamic_window = _dynamic_window,                                                        \
 }
 
+#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _size, _mmap_flags)  \
+__FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _size, _mmap_flags, false); \
+__FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name ## _dynamic_window, _size, _mmap_flags, true) \
+
 FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous, 0, 0);
 FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_2mb, SZ_2M, MAP_HUGETLB | MAP_HUGE_2MB);
 FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB | MAP_HUGE_1GB);
@@ -121,14 +127,13 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |
 FIXTURE_SETUP(vfio_dma_mapping_test)
 {
        const u64 page_size = variant->size ?: getpagesize();
-       const bool force_dynamic = !!variant->size;
        int ret;
 
        self->iommu = iommu_init(variant->iommu_mode);
        self->device = vfio_pci_device_init(device_bdf, self->iommu);
 
        ret = iommu_prepare_dma_window(self->iommu, page_size, page_size,
-                                      force_dynamic);
+                                      variant->dynamic_window);
        if (ret)
                SKIP(return, "DMA window unavailable: %s (%d)\n",
                     strerror(-ret), -ret);
Quoted 48 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +19 -0
+	int ret;
+
 	self->iommu = iommu_init(variant->iommu_mode);
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
+
+	ret = iommu_prepare_dma_window(self->iommu, page_size, page_size,
+				       force_dynamic);
+	if (ret)
+		SKIP(return, "DMA window unavailable: %s (%d)\n",
+		     strerror(-ret), -ret);
+
 	self->iova_allocator = iova_allocator_init(self->iommu);
 }
 
@@ -227,6 +239,7 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
 	u64 region_size = getpagesize();
 	iova_t last_iova;
 	u32 nranges;
+	int ret;
 
 	/*
 	 * Over-allocate mmap by double the size to provide enough backing vaddr
@@ -236,6 +249,13 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
 
 	self->iommu = iommu_init(variant->iommu_mode);
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
+
+	ret = iommu_prepare_dma_window(self->iommu, region_size, region_size,
+				       false);
+	if (ret)
+		SKIP(return, "DMA window unavailable: %s (%d)\n",
+		     strerror(-ret), -ret);
+
 	region->vaddr = mmap(NULL, self->mmap_size, PROT_READ | PROT_WRITE,
 			     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
 	ASSERT_NE(region->vaddr, MAP_FAILED);
@@ -277,6 +297,8 @@ TEST_F(vfio_dma_map_limit_test, unmap_all)
 	u64 unmapped;
 	int rc;
 
+	if (!iommu_supports_unmap_all(self->iommu))
+		SKIP(return, "IOMMU backend does not support unmap-all\n");
 	iommu_map(self->iommu, region);
 	ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr));
 
Signature · 1 line
2.51.1
maintainer
#13 lore raw

Re: [RFC PATCH 4/6] selftests/vfio: Exercise sPAPR DDW path for hugepage DMA mappings

Quoted 29 lines · David Matlack
Quoted 28 lines · Narayana Murty N

Prepare the DMA window before IOVA allocation in the DMA mapping tests.

Anonymous mappings use the default sPAPR DMA window. HugeTLB mappings force a DDW with the requested page size, allowing the test to exercise DDW creation, mapping, unmapping, and cleanup.

Skip gracefully when hugepage allocation fails or when the platform does not support the requested DDW characteristics. Also skip unmap-all when the backend cannot pair it with the sPAPR memory unregister flow.

---
 .../selftests/vfio/vfio_dma_mapping_test.c    | 22 +++++++++++++++++++
 1 file changed, 22 insertions(+)
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +1 -0
index 7d0de8c79de1..4411fdbd56da 100644
@@ -1,5 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <stdio.h>
+#include <string.h>
 #include <sys/mman.h>
 #include <unistd.h>
 
@@ -119,8 +120,19 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |
 
Quoted 8 lines

tools/testing/selftests/vfio/vfio_dma_mapping_test.c FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |

Quoted 4 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +2 -0
 FIXTURE_SETUP(vfio_dma_mapping_test)
 {
+	const u64 page_size = variant->size ?: getpagesize();
+	const bool force_dynamic = !!variant->size;

This is kind of arbitrary right? Should we add explicit variants for dynamic/default windows (see below). But we need some way to skip the dynamic window tests on non-spapr iommus...

Maybe the flow can be something like this:

iommu = iommu_init(); /* sets up default window */

  if (variant->dynamic_window) {
          ret = iommu_dma_window_create(...); /* always fails on non-sapr iommus */
          if (ret)
                  SKIP(...);
  }

Then I think you also want to add some call to gracefully skip tests if the current window does not support the desired mapping. e.g.

  if (!iommu_dma_window_fits(region)) /* always succeeds on non-spapr iommus */
          SKIP(...);
Quoted 97 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +12 -7
index cd2d3276a46c..b54c529ebedb 100644
@@ -103,15 +103,21 @@ FIXTURE_VARIANT(vfio_dma_mapping_test) {
        const char *iommu_mode;
        u64 size;
        int mmap_flags;
+       bool dynamic_window;
 };
 
-#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _size, _mmap_flags) \
-FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, _iommu_mode ## _ ## _name) {               \
-       .iommu_mode = #_iommu_mode,                                            \
-       .size = (_size),                                                       \
-       .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | (_mmap_flags),             \
+#define __FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _size, _mmap_flags, _dynamic_window) \
+FIXTURE_VARIANT_ADD(vfio_dma_mapping_test,_iommu_mode ## _ ## _name) {   \
+       .iommu_mode = #_iommu_mode,                                                               \
+       .size = (_size),                                                                          \
+       .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | (_mmap_flags),                                \
+       .dynamic_window = _dynamic_window,                                                        \
 }
 
+#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _size, _mmap_flags)  \
+__FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _size, _mmap_flags, false); \
+__FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name ## _dynamic_window, _size, _mmap_flags, true) \
+
 FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous, 0, 0);
 FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_2mb, SZ_2M, MAP_HUGETLB | MAP_HUGE_2MB);
 FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB | MAP_HUGE_1GB);
@@ -121,14 +127,13 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |
 FIXTURE_SETUP(vfio_dma_mapping_test)
 {
        const u64 page_size = variant->size ?: getpagesize();
-       const bool force_dynamic = !!variant->size;
        int ret;
 
        self->iommu = iommu_init(variant->iommu_mode);
        self->device = vfio_pci_device_init(device_bdf, self->iommu);
 
        ret = iommu_prepare_dma_window(self->iommu, page_size, page_size,
-                                      force_dynamic);
+                                      variant->dynamic_window);
        if (ret)
                SKIP(return, "DMA window unavailable: %s (%d)\n",
                     strerror(-ret), -ret);
Quoted 49 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +19 -0
+	int ret;
+
 	self->iommu = iommu_init(variant->iommu_mode);
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
+
+	ret = iommu_prepare_dma_window(self->iommu, page_size, page_size,
+				       force_dynamic);
+	if (ret)
+		SKIP(return, "DMA window unavailable: %s (%d)\n",
+		     strerror(-ret), -ret);
+
 	self->iova_allocator = iova_allocator_init(self->iommu);
 }
 
@@ -227,6 +239,7 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
 	u64 region_size = getpagesize();
 	iova_t last_iova;
 	u32 nranges;
+	int ret;
 
 	/*
 	 * Over-allocate mmap by double the size to provide enough backing vaddr
@@ -236,6 +249,13 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
 
 	self->iommu = iommu_init(variant->iommu_mode);
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
+
+	ret = iommu_prepare_dma_window(self->iommu, region_size, region_size,
+				       false);
+	if (ret)
+		SKIP(return, "DMA window unavailable: %s (%d)\n",
+		     strerror(-ret), -ret);
+
 	region->vaddr = mmap(NULL, self->mmap_size, PROT_READ | PROT_WRITE,
 			     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
 	ASSERT_NE(region->vaddr, MAP_FAILED);
@@ -277,6 +297,8 @@ TEST_F(vfio_dma_map_limit_test, unmap_all)
 	u64 unmapped;
 	int rc;
 
+	if (!iommu_supports_unmap_all(self->iommu))
+		SKIP(return, "IOMMU backend does not support unmap-all\n");
 	iommu_map(self->iommu, region);
 	ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr));
 
Signature · 1 line
2.51.1
maintainer
#16 lore raw

Re: [RFC PATCH 5/6] selftests/vfio: Accept sPAPR errno for DMA range overflow

In reply to Narayana Murty N
Quoted 23 lines · Narayana Murty N

Accept sPAPR errno for DMA range overflow

The DMA map limit test expects an overflowing IOVA range to fail with -EOVERFLOW. That matches the Type1 and iommufd paths.

On sPAPR TCE v2, the same invalid range is rejected as outside the active TCE window and currently returns -ENXIO. Treat that as the expected ppc result for this RFC while keeping the existing -EOVERFLOW expectation for other backends.

This keeps the overflow test enabled for sPAPR TCE v2 instead of skipping it.

---
 tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +0 -0
index 4411fdbd56da..cd2d3276a46c 100644

tools/testing/selftests/vfio/vfio_dma_mapping_test.c TEST_F(vfio_dma_map_limit_test, overflow) · line 323

Quoted 8 lines
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +4 -0
@@ -316,10 +316,18 @@ TEST_F(vfio_dma_map_limit_test, overflow)
 	region->size = self->mmap_size;
 
 	rc = __iommu_map(self->iommu, region);
+#ifdef __powerpc__
+	ASSERT_EQ(rc, -ENXIO);
+#else
 	ASSERT_EQ(rc, -EOVERFLOW);
+#endif

This should be based on the IOMMU mode, not __powerpc__, as that is what actually determines what gets returned.

maintainer
#20 lore raw
In reply to Narayana Murty N
Quoted 1 line · Narayana Murty N

This RFC adds PowerPC sPAPR TCE v2 support to VFIO selftests.

nit: Please use "vfio: selftests: ..." for commit shortlogs.

Quoted 3 lines

The series adds a selftest IOMMU mode for the legacy VFIO container backend, adds helpers for default and dynamic DMA windows, and exercises the sPAPR DDW path through the DMA mapping tests.

Thanks for sending. Is there anyway I could run these tests on PowerPC, e.g. with QEMU, to try things out? I don't have access to any hardware.

Quoted 17 lines

sPAPR TCE v2 remains a legacy VFIO container backend. This series does not add iommufd or VFIO cdev support for sPAPR.

The main points are
1. add VFIO_SELFTESTS_IOMMU_MODE for selecting a backend
2. add vfio_spapr_tce_v2_iommu as the PowerPC VFIO backend
3. preserve the platform default DMA window
4. create and tear down only selftest-created DDWs
5. prepare the DMA window before IOVA allocation
6. exercise DDW creation for hugepage DMA mappings
7. accept sPAPR-specific errno for invalid DMA ranges

Example:

VFIO_SELFTESTS_IOMMU_MODE=vfio_spapr_tce_v2_iommu
./vfio_dma_mapping_test <BDF>

Quoted 7 lines

Observed coverage includes anonymous mappings through the default window and 2MB hugetlb mappings through a DDW. 1GB hugetlb mappings may skip when the platform rejects the requested DDW characteristics.

Feedback is requested from the VFIO and PowerPC communities on:
1. whether the sPAPR TCE v2 helpers should remain in the common VFIO selftest library or move into a sPAPR-specific test helper?

Responded in the patch. Let's move to their own file.

Quoted 2 lines

2. whether DDW setup through hugepage mappings is the right initial coverage?

One thing I'm not sure is how much value you get out of exercising the DDW stuff through that test specifically. Would it make more sense to have a DDW-specific test so you can test all the edge cases you care about directly instead? Then vfio_dma_mapping_test can stay generic.

BTW, is that the only test that passes right now? What is the status of the other VFIO selftests on PowerPC after this series?

Quoted 2 lines

3. whether the sPAPR-specific overflow errno expectation should be accepted, skipped, or hidden behind a backend helper?

Responded in the patch. Let's keep the check but base it off IOMMU mode not __powerpc__. Also it'd be good to avoid duplicating the code with a helper function or macro but that's a small thing.

Quoted 2 lines

4. whether PowerPC should generate only the sPAPR TCE v2 fixture variants, or keep the generic IOMMU-mode matrix and skip unsupported modes.

For (4), I think skipping unsupported modes would be a good improvement. Even on x86 and ARM we would want that feature. e.g. It's possible for someone to run VFIO selftests on a kernel that has VFIO Type1 IOMMUs enabled but not IOMMUFD. Today that will cause all the tests to fail. But ideally it would wouldn't. Tests that just want the default IOMMU mode should use Type1, and tests that want to replicate across all IOMMU modes should skip the IOMMUFD variants. Same thing applies to the SPAPR IOMMU mode.

Quoted 23 lines

TODO: vfio_spapr_tce_v1_iommu yet to be covered.

Narayana Murty N (6):
selftests/vfio: allow selecting IOMMU backend from environment selftests/vfio: add sPAPR TCE v2 IOMMU mode selftests/vfio: add sPAPR TCE v2 DMA window helpers selftests/vfio: Exercise sPAPR DDW path for hugepage DMA mappings selftests/vfio: Accept sPAPR errno for DMA range overflow selftests/vfio: Enable VFIO selftests on ppc64 and ppc64le

 .../selftests/vfio/lib/include/libvfio.h      |   9 +
 .../vfio/lib/include/libvfio/iommu.h          |  26 ++
 tools/testing/selftests/vfio/lib/iommu.c      | 280 +++++++++++++++++-
 tools/testing/selftests/vfio/lib/libvfio.c    |  37 +++
 .../selftests/vfio/vfio_dma_mapping_test.c    |  30 ++
 .../selftests/vfio/vfio_pci_device_test.c     |   4 +-
 tools/testing/selftests/vfio/Makefile         |   2 +-
 7 files changed, 382 insertions(+), 6 deletions(-)
Signature · 1 line
2.51.1
#21 lore raw

Hi David,
Thank you for your detailed review comments on this series.

Quoted 3 lines · David Matlack
Quoted 1 line · Narayana Murty N

This RFC adds PowerPC sPAPR TCE v2 support to VFIO selftests.

nit: Please use "vfio: selftests: ..." for commit shortlogs.

Agree, will fix all commit shortlogs in v2.

Quoted 6 lines
Quoted 4 lines

The series adds a selftest IOMMU mode for the legacy VFIO container backend, adds helpers for default and dynamic DMA windows, and exercises the sPAPR DDW path through the DMA mapping tests.

Thanks for sending. Is there anyway I could run these tests on PowerPC, e.g. with QEMU, to try things out? I don't have access to any hardware.

I had tested this patch series on real power h/w (pseries and powernv). However I am not sure if the required firmware support is implemented in QEMU. I will check and get back on this.

Quoted 17 lines
Quoted 17 lines

sPAPR TCE v2 remains a legacy VFIO container backend. This series does not add iommufd or VFIO cdev support for sPAPR.

The main points are
1. add VFIO_SELFTESTS_IOMMU_MODE for selecting a backend
2. add vfio_spapr_tce_v2_iommu as the PowerPC VFIO backend
3. preserve the platform default DMA window
4. create and tear down only selftest-created DDWs
5. prepare the DMA window before IOVA allocation
6. exercise DDW creation for hugepage DMA mappings
7. accept sPAPR-specific errno for invalid DMA ranges

Example:

VFIO_SELFTESTS_IOMMU_MODE=vfio_spapr_tce_v2_iommu
./vfio_dma_mapping_test <BDF>

Quoted 8 lines
Quoted 7 lines

Observed coverage includes anonymous mappings through the default window and 2MB hugetlb mappings through a DDW. 1GB hugetlb mappings may skip when the platform rejects the requested DDW characteristics.

Feedback is requested from the VFIO and PowerPC communities on:
1. whether the sPAPR TCE v2 helpers should remain in the common VFIO selftest library or move into a sPAPR-specific test helper?

Responded in the patch. Let's move to their own file.

Will do. v2 will have:

  lib/iommu_spapr.c         — all sPAPR logic, compiled only on powerpc lib/iommu_spapr_stub.c    — empty stubs returning -EOPNOTSUPP, compiled on all other architectures lib/include/libvfio/iommu_spapr.h — declarations

iommu.c will call the sPAPR helpers unconditionally with no #ifdefs in function bodies.

Quoted 6 lines
Quoted 2 lines

2. whether DDW setup through hugepage mappings is the right initial coverage?

One thing I'm not sure is how much value you get out of exercising the DDW stuff through that test specifically. Would it make more sense to have a DDW-specific test so you can test all the edge cases you care about directly instead? Then vfio_dma_mapping_test can stay generic.

Agreed. For v2 I plan to introduce a dedicated vfio_spapr_ddw_test that covers DDW creation, window selection, page-size negotiation, and removal directly, and keep vfio_dma_mapping_test generic. The hugepage variants in vfio_dma_mapping_test will then just exercise the mapping path itself without being the primary DDW coverage vehicle.

Quoted 3 lines

BTW, is that the only test that passes right now? What is the status of the other VFIO selftests on PowerPC after this series?

  vfio_dma_mapping_test    — anonymous and 2 MB hugetlb variants PASS; vfio_dma_mapping_mmio_test  — PASS (overflow test passes with -ENXIO fix). vfio_pci_device_test     — PASS.
Below tests are not supported:
  vfio_pci_driver_test
  vfio_iommufd_setup_test
currently it fails unsupported tests when triggred via scripts/run.sh

I will start including test logs in cover letter starting with RFC V2.

Quoted 6 lines
Quoted 3 lines

3. whether the sPAPR-specific overflow errno expectation should be accepted, skipped, or hidden behind a backend helper?

Responded in the patch. Let's keep the check but base it off IOMMU mode not __powerpc__. Also it'd be good to avoid duplicating the code with a helper function or macro but that's a small thing.

Agreed. Will replace the #ifdef __powerpc__ guard with a runtime check:

  int expected = iommu_is_spapr(self->iommu) ? -ENXIO : -EOVERFLOW; ASSERT_EQ(rc, expected);
I will add a helper to avoid code duplication.

Quoted 10 lines
Quoted 2 lines

4. whether PowerPC should generate only the sPAPR TCE v2 fixture variants, or keep the generic IOMMU-mode matrix and skip unsupported modes.

For (4), I think skipping unsupported modes would be a good improvement. Even on x86 and ARM we would want that feature. e.g. It's possible for someone to run VFIO selftests on a kernel that has VFIO Type1 IOMMUs enabled but not IOMMUFD. Today that will cause all the tests to fail. But ideally it would wouldn't. Tests that just want the default IOMMU mode should use Type1, and tests that want to replicate across all IOMMU modes should skip the IOMMUFD variants. Same thing applies to the SPAPR IOMMU mode.

This is a great point and it also addresses the concern raised in the Patch 1/6 review about forcing the user to set VFIO_SELFTESTS_IOMMU_MODE.

Once again thanks for the thorough review.
I shall address all the review comments on the series before sending v2.

Regards,
Narayana

Quoted 22 lines
Quoted 22 lines

TODO: vfio_spapr_tce_v1_iommu yet to be covered.

Narayana Murty N (6):
selftests/vfio: allow selecting IOMMU backend from environment selftests/vfio: add sPAPR TCE v2 IOMMU mode selftests/vfio: add sPAPR TCE v2 DMA window helpers selftests/vfio: Exercise sPAPR DDW path for hugepage DMA mappings selftests/vfio: Accept sPAPR errno for DMA range overflow selftests/vfio: Enable VFIO selftests on ppc64 and ppc64le

  .../selftests/vfio/lib/include/libvfio.h      |   9 +
  .../vfio/lib/include/libvfio/iommu.h          |  26 ++
  tools/testing/selftests/vfio/lib/iommu.c      | 280 +++++++++++++++++-
  tools/testing/selftests/vfio/lib/libvfio.c    |  37 +++
  .../selftests/vfio/vfio_dma_mapping_test.c    |  30 ++
  .../selftests/vfio/vfio_pci_device_test.c     |   4 +-
  tools/testing/selftests/vfio/Makefile         |   2 +-
  7 files changed, 382 insertions(+), 6 deletions(-)
Signature · 1 line
2.51.1
maintainer
#22 lore raw
Quoted 10 lines · Narayana Murty N
Quoted 4 lines
Quoted 3 lines

Feedback is requested from the VFIO and PowerPC communities on:
1. whether the sPAPR TCE v2 helpers should remain in the common VFIO selftest library or move into a sPAPR-specific test helper?

Responded in the patch. Let's move to their own file.

Will do. v2 will have:

lib/iommu_spapr.c — all sPAPR logic, compiled only on powerpc
lib/iommu_spapr_stub.c — empty stubs returning -EOPNOTSUPP,
compiled on all other architectures
lib/include/libvfio/iommu_spapr.h — declarations

Just one small thing here: You can put all the stubs in iommu_spapr.h as static inlines, then you don't need the extra C file. e.g.

#ifdef __powerpc__

... prototypes ...

#else

... static inline stubs ...

#endif

#23 lore raw
Quoted 17 lines · David Matlack
Quoted 10 lines · Narayana Murty N
Quoted 4 lines
Quoted 3 lines

Feedback is requested from the VFIO and PowerPC communities on:
1. whether the sPAPR TCE v2 helpers should remain in the common VFIO selftest library or move into a sPAPR-specific test helper?

Responded in the patch. Let's move to their own file.

Will do. v2 will have:

lib/iommu_spapr.c — all sPAPR logic, compiled only on powerpc
lib/iommu_spapr_stub.c — empty stubs returning -EOPNOTSUPP,
compiled on all other architectures
lib/include/libvfio/iommu_spapr.h — declarations

Just one small thing here: You can put all the stubs in iommu_spapr.h as static inlines, then you don't need the extra C file. e.g.

#ifdef __powerpc__

Quoted 7 lines

... prototypes ...

#else

... static inline stubs ...

#endif

Sure, will follow the same.

regards,
Narayana