[PATCH v3 3/5] vfio: selftests: Print the IOMMU modes when a requested mode can't be used
From: David Matlack
Date: Mon Nov 09 2026 - 16:10:07 EST
Print the names of all known IOMMU modes when passed an unrecognized
IOMMU mode, and the names of the available IOMMU modes when a test skips
because the IOMMU mode it requested is not available. If no IOMMU mode
is available at all, print the generic "No IOMMU mode is available"
message instead. Expose both lists via iommu_mode_names_all() and
iommu_mode_names_available() so that tests can also print them, e.g. in
their help output.
irq_test lets users choose the IOMMU mode with -t, so a typo or
unavailable mode should tell them which modes are valid.
Suggested-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://lore.kernel.org/kvm/auit0kVkAaWcNUvk@xxxxxxxxxx/
Signed-off-by: David Matlack <dmatlack@xxxxxxxxxx>
---
.../vfio/lib/include/libvfio/iommu.h | 9 ++++
tools/testing/selftests/vfio/lib/iommu.c | 44 ++++++++++++++++---
2 files changed, 48 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h b/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
index 8306ade5f069..309b231eb0d8 100644
--- a/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
+++ b/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h
@@ -52,6 +52,15 @@ bool iommu_mode_is_available(const char *iommu_mode);
*/
const char *iommu_mode_unavailable_msg(const char *iommu_mode);
+/* Returns a comma-separated list of the names of all IOMMU modes. */
+const char *iommu_mode_names_all(void);
+
+/*
+ * Returns a comma-separated list of the names of the IOMMU modes that are
+ * available on the current system, or "none" if no IOMMU mode is available.
+ */
+const char *iommu_mode_names_available(void);
+
int __iommu_map(struct iommu *iommu, struct dma_region *region);
static inline void iommu_map(struct iommu *iommu, struct dma_region *region)
diff --git a/tools/testing/selftests/vfio/lib/iommu.c b/tools/testing/selftests/vfio/lib/iommu.c
index 56696bd5c30d..bde53bd55208 100644
--- a/tools/testing/selftests/vfio/lib/iommu.c
+++ b/tools/testing/selftests/vfio/lib/iommu.c
@@ -105,6 +105,38 @@ static const struct iommu_mode *iommu_mode_detect(void)
return NULL;
}
+static const char *iommu_mode_names(bool include_unavailable)
+{
+ static char all_names[256];
+ static char avl_names[sizeof(all_names)];
+ char *names = include_unavailable ? all_names : avl_names;
+ int i, len = 0;
+
+ if (names[0])
+ return names;
+
+ for (i = 0; i < ARRAY_SIZE(iommu_modes); i++) {
+ if (!include_unavailable && !iommu_mode_probe(&iommu_modes[i]))
+ continue;
+
+ len += snprintf(names + len, sizeof(all_names) - len, "%s%s",
+ names[0] ? ", " : "", iommu_modes[i].name);
+ VFIO_ASSERT_LT(len, (int)sizeof(all_names));
+ }
+
+ return names[0] ? names : "none";
+}
+
+const char *iommu_mode_names_all(void)
+{
+ return iommu_mode_names(true);
+}
+
+const char *iommu_mode_names_available(void)
+{
+ return iommu_mode_names(false);
+}
+
/*
* Returns the IOMMU mode with the given name, or if iommu_mode is NULL, the
* most preferred IOMMU mode that is available (or NULL if none).
@@ -123,7 +155,8 @@ static const struct iommu_mode *iommu_mode_lookup(const char *iommu_mode)
return &iommu_modes[i];
}
- VFIO_FAIL("Unrecognized IOMMU mode: %s\n", iommu_mode);
+ VFIO_FAIL("Unrecognized IOMMU mode '%s'. Known modes: %s.\n",
+ iommu_mode, iommu_mode_names_all());
}
bool iommu_mode_is_available(const char *iommu_mode)
@@ -136,13 +169,14 @@ bool iommu_mode_is_available(const char *iommu_mode)
const char *iommu_mode_unavailable_msg(const char *iommu_mode)
{
- static char msg[128];
+ static char msg[512];
- if (!iommu_mode)
+ if (!iommu_mode || !iommu_mode_detect())
return NO_IOMMU_MODE_AVAILABLE_MSG;
- snprintf(msg, sizeof(msg), "IOMMU mode '%s' is not available",
- iommu_mode);
+ snprintf(msg, sizeof(msg),
+ "IOMMU mode '%s' is not available. Available modes: %s.",
+ iommu_mode, iommu_mode_names_available());
return msg;
}
--
2.56.0.506.g506a7eabc5-goog