Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Narayana Murty N <nnmlinux@linux.ibm.com>
To: alex@shazbot.org, dmatlack@google.com, shuah@kernel.org
Cc: amastro@fb.com, rananta@google.com, nnmlinux@linux.ibm.com,
	kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org, vaibhav@linux.ibm.com,
	sbhat@linux.ibm.com, harshpb@linux.ibm.com
Subject: [RFC PATCH 1/6] selftests/vfio: allow selecting IOMMU backend from environment
Date: Thu,  2 Jul 2026 23:28:01 -0400	[thread overview]
Message-ID: <20260703032806.40946-2-nnmlinux@linux.ibm.com> (raw)
In-Reply-To: <20260703032806.40946-1-nnmlinux@linux.ibm.com>

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.

Signed-off-by: Narayana Murty N <nnmlinux@linux.ibm.com>
---
 .../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(-)

diff --git a/tools/testing/selftests/vfio/lib/include/libvfio.h b/tools/testing/selftests/vfio/lib/include/libvfio.h
index 07862b470777..c0b4a2ab06c5 100644
--- a/tools/testing/selftests/vfio/lib/include/libvfio.h
+++ b/tools/testing/selftests/vfio/lib/include/libvfio.h
@@ -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.
diff --git a/tools/testing/selftests/vfio/lib/libvfio.c b/tools/testing/selftests/vfio/lib/libvfio.c
index 3a3d1ed635c1..617c8dc7288c 100644
--- a/tools/testing/selftests/vfio/lib/libvfio.c
+++ b/tools/testing/selftests/vfio/lib/libvfio.c
@@ -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;
+}
diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/testing/selftests/vfio/vfio_pci_device_test.c
index 93c11fd5e081..45b8bf60527a 100644
--- a/tools/testing/selftests/vfio/vfio_pci_device_test.c
+++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c
@@ -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);
 }
 
-- 
2.51.1


  reply	other threads:[~2026-07-03  7:53 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-03  3:28 [RFC PATCH 0/6] selftests/vfio: Add sPAPR TCE v2 coverage Narayana Murty N
2026-07-03  3:28 ` Narayana Murty N [this message]
2026-07-03  8:06   ` [RFC PATCH 1/6] selftests/vfio: allow selecting IOMMU backend from environment sashiko-bot
2026-07-03  3:28 ` [RFC PATCH 2/6] selftests/vfio: add sPAPR TCE v2 IOMMU mode Narayana Murty N
2026-07-03  8:09   ` sashiko-bot
2026-07-03  3:28 ` [RFC PATCH 3/6] selftests/vfio: add sPAPR TCE v2 DMA window helpers Narayana Murty N
2026-07-03  8:05   ` sashiko-bot
2026-07-03  3:28 ` [RFC PATCH 4/6] selftests/vfio: Exercise sPAPR DDW path for hugepage DMA mappings Narayana Murty N
2026-07-03  8:11   ` sashiko-bot
2026-07-03  3:28 ` [RFC PATCH 5/6] selftests/vfio: Accept sPAPR errno for DMA range overflow Narayana Murty N
2026-07-03  8:08   ` sashiko-bot
2026-07-03  3:28 ` [RFC PATCH 6/6] selftests/vfio: Enable VFIO selftests on ppc64 and ppc64le Narayana Murty N
2026-07-03  8:14   ` sashiko-bot
2026-07-03  8:28 ` [RFC PATCH 0/6] selftests/vfio: Add sPAPR TCE v2 coverage Harsh Prateek Bora

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260703032806.40946-2-nnmlinux@linux.ibm.com \
    --to=nnmlinux@linux.ibm.com \
    --cc=alex@shazbot.org \
    --cc=amastro@fb.com \
    --cc=dmatlack@google.com \
    --cc=harshpb@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=rananta@google.com \
    --cc=sbhat@linux.ibm.com \
    --cc=shuah@kernel.org \
    --cc=vaibhav@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox