Skip to contentSkip to conversation

vfio: selftests: Automatically detect the IOMMU mode

PATCH Superseded David Matlack 3 participants 11 messages Nov 3 – Nov 4

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

David Matlack

author maintainer
#0 lore raw

v2 cover vfio: selftests: Automatically detect the IOMMU mode

This series makes the VFIO selftests library automatically detect which IOMMU mode to use, rather than hardcoding iommufd as the default, and makes tests skip, rather than fail, when the IOMMU mode they need is not available. This was suggested by Sean while reviewing the KVM selftest irq_test, which currently has to probe for the IOMMU mode itself [1].

iommufd is an optional feature, and VFIO's iommufd support additionally requires CONFIG_VFIO_DEVICE_CDEV. On otherwise functional kernels that only support the legacy VFIO container, the VFIO selftests (and irq_test, when using a VFIO device) currently fail with an unhelpful assert:

tools/testing/selftests/vfio/lib/iommu.c:448: Assertion Failure

Expression: iommu->iommufd > 0
Observed: 0xffffffffffffffff > 0
[errno: 2 - No such file or directory]

Patch 1 makes iommu_init(NULL) pick the most preferred IOMMU mode that is available on the current system, and drops the global default_iommu_mode. iommu_modes[] is now ordered by preference: iommufd, vfio_type1v2_iommu, vfio_type1_iommu, iommufd_compat_type1v2, iommufd_compat_type1. A mode is considered available if its container (or /dev/iommu) can be opened and, for container modes, if VFIO_CHECK_EXTENSION reports support for the mode's IOMMU type. iommufd mode additionally requires VFIO device cdevs.

Patch 2 adds iommu_mode_is_available(), iommu_mode_unavailable_msg(), and SKIP_IF_IOMMU_MODE_UNAVAILABLE(), and uses them to skip tests when the IOMMU mode they need is not available. Tests skip via the kselftest harness's SKIP() rather than iommu_init() calling ksft_exit_skip(), since the latter produces malformed TAP output when called from within a harness test.

Patch 3 makes the VFIO selftests library print the names of all known IOMMU modes when passed an unrecognized mode, and exposes the list via iommu_mode_names().

Patch 4 converts irq_test to use the VFIO selftests library to pick the IOMMU mode, and drops its probe_iommu_type().

Patch 5 lists the known IOMMU modes in irq_test's help output.

This series is based on vfio/next (vfio-v7.4-rc1).

v2:
- Print the known IOMMU modes when passed an unrecognized mode (patch 3) [Sean]
- Print a helpful message when irq_test skips because the IOMMU mode is not available (patch 4) [Sean]
- List the known IOMMU modes in irq_test's help output (patch 5)
- Add iommu_mode_unavailable_msg() so that the VFIO selftests and irq_test print the same message when skipping, including "No IOMMU mode is available. Is VFIO (or IOMMUFD) loaded and enabled?" when no IOMMU mode is available (patches 1, 2, and 4)

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

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

Testing
-----------------------------------------------------------------------

Tested in a QEMU VM with an emulated igb device behind an emulated Intel IOMMU, bound to vfio-pci, on a kernel with CONFIG_IOMMUFD=y, CONFIG_VFIO_DEVICE_CDEV=y, and CONFIG_VFIO_CONTAINER=y. To simulate kernels with fewer IOMMU modes available, /dev/iommu and /dev/vfio/vfio were deleted in the VM:

- All IOMMU modes available: All VFIO selftests pass. irq_test -d passes using iommufd. irq_test -d -t bogus fails and lists the known modes.

- Only the legacy VFIO container available (/dev/iommu deleted): All VFIO selftests pass, with the iommufd and iommufd_compat variants skipped instead of failing. irq_test -d passes using vfio_type1v2_iommu, and skips if -t iommufd is passed.

- No IOMMU mode available (/dev/iommu and /dev/vfio/vfio deleted):
vfio_pci_device_test, vfio_pci_driver_test, and irq_test -d all skip with a message explaining why, and with well-formed TAP output.

David Matlack (5):
vfio: selftests: Automatically detect the IOMMU mode in iommu_init()
vfio: selftests: Skip tests when the IOMMU mode is not available
vfio: selftests: Print the known IOMMU modes on an unrecognized mode
KVM: selftests: Use VFIO selftests library to pick IOMMU mode in
irq_test
KVM: selftests: List the known IOMMU modes in irq_test's help output

 tools/testing/selftests/kvm/irq_test.c        |  23 +---
 .../vfio/lib/include/libvfio/iommu.h          |  38 +++++-
 tools/testing/selftests/vfio/lib/iommu.c      | 108 ++++++++++++++++--
 .../vfio/vfio_dma_mapping_mmio_test.c         |   2 +
 .../selftests/vfio/vfio_dma_mapping_test.c    |   4 +
 .../vfio/vfio_pci_device_init_perf_test.c     |   2 +
 .../selftests/vfio/vfio_pci_device_test.c     |   8 +-
 .../selftests/vfio/vfio_pci_driver_test.c     |   9 +-
 .../selftests/vfio/vfio_pci_sriov_uapi_test.c |   4 +
 9 files changed, 162 insertions(+), 36 deletions(-)

base-commit: 076d7e989cb69f6ded7d60f2a080eb909930061b

Signature · 1 line
2.56.0.385.gac671c819a-goog
author maintainer
#1 lore raw

PATCH v2 1/5 vfio: selftests: Automatically detect the IOMMU mode in iommu_init()

Automatically detect the IOMMU mode that is most likely to work on the current system when iommu_init() is passed a NULL mode, rather than always using iommufd. Drop the global default_iommu_mode and convert its users to pass NULL to iommu_init().

iommufd is an optional feature, and VFIO's iommufd support additionally requires CONFIG_VFIO_DEVICE_CDEV. Hardcoding iommufd as the default causes tests to fail with an unhelpful assert on otherwise functional kernels that only support the legacy VFIO container:

tools/testing/selftests/vfio/lib/iommu.c:448: Assertion Failure

Expression: iommu->iommufd > 0
Observed: 0xffffffffffffffff > 0
[errno: 2 - No such file or directory]

Order iommu_modes[] by preference and pick the first mode that is available. Consider a mode available if its container (or /dev/iommu) can be opened and, for container modes, if VFIO_CHECK_EXTENSION reports support for the mode's IOMMU type. iommufd mode additionally requires VFIO device cdevs. If no mode is available, fail with a message that explains why.

---
 .../vfio/lib/include/libvfio/iommu.h          |  6 +-
 tools/testing/selftests/vfio/lib/iommu.c      | 68 ++++++++++++++++---
 .../selftests/vfio/vfio_pci_device_test.c     |  4 +-
 .../selftests/vfio/vfio_pci_driver_test.c     |  2 +-
 4 files changed, 64 insertions(+), 16 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +4 -2
index e9a3386a4719..fb0a681ecc72 100644
@@ -15,8 +15,6 @@ struct iommu_mode {
 	unsigned long iommu_type;
 };
 
-extern const char *default_iommu_mode;
-
 struct dma_region {
 	struct list_head link;
 	void *vaddr;
@@ -32,6 +30,10 @@ struct iommu {
 	struct list_head dma_regions;
 };
 
+/*
+ * Pass NULL for iommu_mode to automatically pick the mode most likely to work
+ * on the current system.
+ */
 struct iommu *iommu_init(const char *iommu_mode);
 void iommu_cleanup(struct iommu *iommu);
 
tools/testing/selftests/vfio/lib/iommu.c +57 -11

102 lines not on this page · raw message

tools/testing/selftests/vfio/vfio_pci_device_test.c +2 -2
index 93c11fd5e081..59a7c1f71804 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(NULL);
 	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(NULL);
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
 }
 
tools/testing/selftests/vfio/vfio_pci_driver_test.c +1 -1
index 9538a3a436a6..f8f99ee6bb1a 100644
@@ -255,7 +255,7 @@ static bool device_has_selftests_driver(const char *bdf)
 	struct iommu *iommu;
 	bool has_driver;
 
-	iommu = iommu_init(default_iommu_mode);
+	iommu = iommu_init(NULL);
 	device = vfio_pci_device_init(device_bdf, iommu);
 
 	has_driver = !!device->driver.ops;
Signature · 1 line
2.56.0.385.gac671c819a-goog
author maintainer
#2 lore raw

PATCH v2 2/5 vfio: selftests: Skip tests when the IOMMU mode is not available

In reply to David Matlack

Skip tests when the IOMMU mode they need is not available on the current system, instead of failing. Add iommu_mode_is_available() to check whether a given IOMMU mode (or, if NULL, any IOMMU mode) is available, iommu_mode_unavailable_msg() to explain why a test is skipping, and SKIP_IF_IOMMU_MODE_UNAVAILABLE() to skip a kselftest harness test if the mode is not available.

On a kernel without iommufd, for example, all of the iommufd and iommufd_compat test variants currently fail on an assert in iommu_init(). Skip them instead, with a message that explains why:

ok 33 vfio_pci_driver_test.iommufd.send_msi # SKIP IOMMU mode 'iommufd' is not available

Use SKIP_IF_IOMMU_MODE_UNAVAILABLE() in all tests that run across multiple IOMMU modes, and in the tests that let the library pick the mode by passing NULL to iommu_init(). Also skip vfio_pci_driver_test, which calls iommu_init() from main() to probe the device, if no IOMMU mode is available.

Skip from the test via the kselftest harness's SKIP(), rather than from within iommu_init() via ksft_exit_skip(). ksft_exit_skip() is not aware of the harness, and so would emit a bogus "1..0 # SKIP" plan line in the middle of the TAP output, and the harness would report the reason for the skip as "unknown".

---
 .../vfio/lib/include/libvfio/iommu.h          | 31 ++++++++++++++++++-
 tools/testing/selftests/vfio/lib/iommu.c      | 20 ++++++++++++
 .../vfio/vfio_dma_mapping_mmio_test.c         |  2 ++
 .../selftests/vfio/vfio_dma_mapping_test.c    |  4 +++
 .../vfio/vfio_pci_device_init_perf_test.c     |  2 ++
 .../selftests/vfio/vfio_pci_device_test.c     |  4 +++
 .../selftests/vfio/vfio_pci_driver_test.c     |  7 +++++
 .../selftests/vfio/vfio_pci_sriov_uapi_test.c |  4 +++
 8 files changed, 73 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +30 -1
index fb0a681ecc72..f204c71494fe 100644
@@ -32,11 +32,25 @@ struct iommu {
 
 /*
  * Pass NULL for iommu_mode to automatically pick the mode most likely to work
- * on the current system.
+ * on the current system. Use iommu_mode_is_available() to check that the mode
+ * is available before calling iommu_init(), e.g. to skip the test if not.
  */
 struct iommu *iommu_init(const char *iommu_mode);
 void iommu_cleanup(struct iommu *iommu);
 
+/*
+ * Returns true if the given IOMMU mode is usable on the current system. If
+ * iommu_mode is NULL, returns true if any IOMMU mode is usable.
+ */
+bool iommu_mode_is_available(const char *iommu_mode);
+
+/*
+ * Returns a message explaining that the given IOMMU mode (or, if iommu_mode is
+ * NULL, any IOMMU mode) is not available, e.g. for tests to print when
+ * skipping.
+ */
+const char *iommu_mode_unavailable_msg(const char *iommu_mode);
+
 int __iommu_map(struct iommu *iommu, struct dma_region *region);
 
 static inline void iommu_map(struct iommu *iommu, struct dma_region *region)
@@ -81,4 +95,19 @@ 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__)
 
+/*
+ * Skip the current kselftest harness test if the given IOMMU mode is not
+ * available on the current system, or if no IOMMU mode is available if
+ * _iommu_mode is NULL. Like SKIP(), statement is executed after marking the
+ * test as skipped (e.g. "return"), and this can be used anywhere SKIP() can be
+ * used.
+ */
+#define SKIP_IF_IOMMU_MODE_UNAVAILABLE(statement, _iommu_mode) do {		\
+	const char *__iommu_mode = (_iommu_mode);				\
+										\
+	if (!iommu_mode_is_available(__iommu_mode))				\
+		SKIP(statement, "%s",						\
+		     iommu_mode_unavailable_msg(__iommu_mode));			\
+} while (0)
+
 #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_IOMMU_H */
tools/testing/selftests/vfio/lib/iommu.c +20 -0
index 0393536370e7..526bb21f2766 100644
@@ -111,6 +111,26 @@ static const struct iommu_mode *iommu_mode_lookup(const char *iommu_mode)
 	VFIO_FAIL("Unrecognized IOMMU mode: %s\n", iommu_mode);
 }
 
+bool iommu_mode_is_available(const char *iommu_mode)
+{
+	if (!iommu_mode)
+		return iommu_mode_detect();
+
+	return iommu_mode_probe(iommu_mode_lookup(iommu_mode));
+}
+
+const char *iommu_mode_unavailable_msg(const char *iommu_mode)
+{
+	static char msg[128];
+
+	if (!iommu_mode)
+		return NO_IOMMU_MODE_AVAILABLE_MSG;
+
+	snprintf(msg, sizeof(msg), "IOMMU mode '%s' is not available",
+		 iommu_mode);
+	return msg;
+}
+
 int __iommu_hva2iova(struct iommu *iommu, void *vaddr, iova_t *iova)
 {
 	struct dma_region *region;
tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c +2 -0
index d7f25ef77671..add7d3881930 100644
@@ -64,6 +64,8 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES();
 
 FIXTURE_SETUP(vfio_dma_mapping_mmio_test)
 {
+	SKIP_IF_IOMMU_MODE_UNAVAILABLE(return, variant->iommu_mode);
+
 	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);
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +4 -0
index 7d0de8c79de1..e42170aecf6e 100644
@@ -119,6 +119,8 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB |
 
 FIXTURE_SETUP(vfio_dma_mapping_test)
 {
+	SKIP_IF_IOMMU_MODE_UNAVAILABLE(return, variant->iommu_mode);
+
 	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);
@@ -228,6 +230,8 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
 	iova_t last_iova;
 	u32 nranges;
 
+	SKIP_IF_IOMMU_MODE_UNAVAILABLE(return, variant->iommu_mode);
+
 	/*
 	 * Over-allocate mmap by double the size to provide enough backing vaddr
 	 * for overflow tests
tools/testing/selftests/vfio/vfio_pci_device_init_perf_test.c +2 -0
index e1a54e153cd3..8a98c86096be 100644
@@ -44,6 +44,8 @@ FIXTURE_SETUP(vfio_pci_device_init_perf_test)
 {
 	int i;
 
+	SKIP_IF_IOMMU_MODE_UNAVAILABLE(return, variant->iommu_mode);
+
 	self->iommu = iommu_init(variant->iommu_mode);
 	self->threads = calloc_assert(nr_devices, sizeof(self->threads[0]));
 	self->thread_args = calloc_assert(nr_devices, sizeof(self->thread_args[0]));
tools/testing/selftests/vfio/vfio_pci_device_test.c +4 -0
index 59a7c1f71804..1b1baa1e1c85 100644
@@ -29,6 +29,8 @@ FIXTURE(vfio_pci_device_test) {
 
 FIXTURE_SETUP(vfio_pci_device_test)
 {
+	SKIP_IF_IOMMU_MODE_UNAVAILABLE(return, NULL);
+
 	self->iommu = iommu_init(NULL);
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
 }
@@ -121,6 +123,8 @@ FIXTURE_VARIANT_ADD(vfio_pci_irq_test, msix) {
 
 FIXTURE_SETUP(vfio_pci_irq_test)
 {
+	SKIP_IF_IOMMU_MODE_UNAVAILABLE(return, NULL);
+
 	self->iommu = iommu_init(NULL);
 	self->device = vfio_pci_device_init(device_bdf, self->iommu);
 }
tools/testing/selftests/vfio/vfio_pci_driver_test.c +7 -0
index f8f99ee6bb1a..56178c25c2fd 100644
@@ -80,6 +80,8 @@ FIXTURE_SETUP(vfio_pci_driver_test)
 {
 	struct vfio_pci_driver *driver;
 
+	SKIP_IF_IOMMU_MODE_UNAVAILABLE(return, variant->iommu_mode);
+
 	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);
@@ -270,6 +272,11 @@ int main(int argc, char *argv[])
 {
 	device_bdf = vfio_selftests_get_bdf(&argc, argv);
 
+	if (!iommu_mode_is_available(NULL)) {
+		fprintf(stderr, "%s\n", iommu_mode_unavailable_msg(NULL));
+		return KSFT_SKIP;
+	}
+
 	if (!device_has_selftests_driver(device_bdf)) {
 		fprintf(stderr, "No driver found for device %s\n", device_bdf);
 		return KSFT_SKIP;
tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c +4 -0
index f3a018b8699d..32a1e651c815 100644
@@ -90,6 +90,8 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(null_uuid, NULL);
 
 FIXTURE_SETUP(vfio_pci_sriov_uapi_test)
 {
+	SKIP_IF_IOMMU_MODE_UNAVAILABLE(return, variant->iommu_mode);
+
 	self->iommu = iommu_init(variant->iommu_mode);
 
 	self->pf_token = UUID_1;
@@ -163,6 +165,8 @@ TEST(failed_second_open_does_not_clobber_token)
 	struct iommu *iommu;
 	int ret;
 
+	SKIP_IF_IOMMU_MODE_UNAVAILABLE(return, MODE_IOMMUFD);
+
 	iommu = iommu_init(MODE_IOMMUFD);
 
 	/* Create and bind PF using UUID_1 */
Signature · 1 line
2.56.0.385.gac671c819a-goog
#3 lore raw

Re: [PATCH v2 2/5] vfio: selftests: Skip tests when the IOMMU mode is not available

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] vfio: selftests: iommu_mode_is_available() misses CONFIG_VFIO_GROUP check --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] vfio: selftests: iommu_mode_is_available() misses CONFIG_VFIO_GROUP check --

commit 254a1695e07cd0eb0e8a8f399f3d939f79af60d2
Author: David Matlack <[email protected]>

vfio: selftests: Skip tests when the IOMMU mode is not available

This commit adds iommu_mode_is_available(), iommu_mode_unavailable_msg(), and SKIP_IF_IOMMU_MODE_UNAVAILABLE() to skip VFIO selftest variants when the requested IOMMU mode is unavailable on the system.

Quoted 5 lines
tools/testing/selftests/vfio/lib/iommu.c +0 -0
index 0393536370e79..526bb21f27666 100644

tools/testing/selftests/vfio/lib/iommu.c static const struct iommu_mode *iommu_mode_lookup(const char *iommu_mode) · line 120

Quoted 10 lines
tools/testing/selftests/vfio/lib/iommu.c +7 -0
@@ -111,6 +111,26 @@ static const struct iommu_mode *iommu_mode_lookup(const char *iommu_mode)
 	VFIO_FAIL("Unrecognized IOMMU mode: %s\n", iommu_mode);
 }
 
+bool iommu_mode_is_available(const char *iommu_mode)
+{
+	if (!iommu_mode)
+		return iommu_mode_detect();
+
+	return iommu_mode_probe(iommu_mode_lookup(iommu_mode));
+}

[Severity: Medium]
Can iommu_mode_is_available() return true for iommufd_compat_type1, iommufd_compat_type1v2, and NULL when CONFIG_VFIO_GROUP is disabled (for example on a cdev-only VFIO kernel with CONFIG_IOMMUFD=y and CONFIG_VFIO_DEVICE_CDEV=y) or when the vfio module is not loaded?

In tools/testing/selftests/vfio/lib/iommu.c, iommu_mode_probe() only checks for /dev/vfio/devices when !mode->container_path:

	/* iommufd mode requires VFIO device cdevs (CONFIG_VFIO_DEVICE_CDEV). */
	if (!mode->container_path && access("/dev/vfio/devices", F_OK))
		return false;

	fd = open(path, O_RDWR);
	if (fd < 0)
		return false;

	available = !mode->container_path ||
		    ioctl(fd, VFIO_CHECK_EXTENSION, mode->iommu_type) > 0;

For iommufd_compat_type1 and iommufd_compat_type1v2, mode->container_path is "/dev/iommu". Because drivers/iommu/iommufd/vfio_compat.c is compiled into iommufd unconditionally regardless of CONFIG_VFIO_GROUP, opening /dev/iommu and calling VFIO_CHECK_EXTENSION succeeds whenever iommufd is present.

However, vfio_pci_device_init() and device_init() in vfio_pci_sriov_uapi_test.c route all modes with a non-NULL mode->container_path through vfio_pci_container_setup() -> vfio_pci_group_setup(), which opens /dev/vfio/<group_id> and asserts on failure when CONFIG_VFIO_GROUP is disabled.

Should iommu_mode_probe() also verify that VFIO group support is available (such as checking access("/sys/class/vfio", F_OK) == 0) when mode->container_path is non-NULL?

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

Re: [PATCH v2 2/5] vfio: selftests: Skip tests when the IOMMU mode is not available

Quoted 32 lines · On Mon, Nov 2, 2026 at 4:30 PM <[email protected]> wrote:

[Severity: Medium]
Can iommu_mode_is_available() return true for iommufd_compat_type1, iommufd_compat_type1v2, and NULL when CONFIG_VFIO_GROUP is disabled (for example on a cdev-only VFIO kernel with CONFIG_IOMMUFD=y and CONFIG_VFIO_DEVICE_CDEV=y) or when the vfio module is not loaded?

In tools/testing/selftests/vfio/lib/iommu.c, iommu_mode_probe() only checks for /dev/vfio/devices when !mode->container_path:

        /* iommufd mode requires VFIO device cdevs (CONFIG_VFIO_DEVICE_CDEV). */
        if (!mode->container_path && access("/dev/vfio/devices", F_OK))
                return false;

        fd = open(path, O_RDWR);
        if (fd < 0)
                return false;

        available = !mode->container_path ||
                    ioctl(fd, VFIO_CHECK_EXTENSION, mode->iommu_type) > 0;

For iommufd_compat_type1 and iommufd_compat_type1v2, mode->container_path is "/dev/iommu". Because drivers/iommu/iommufd/vfio_compat.c is compiled into iommufd unconditionally regardless of CONFIG_VFIO_GROUP, opening /dev/iommu and calling VFIO_CHECK_EXTENSION succeeds whenever iommufd is present.

However, vfio_pci_device_init() and device_init() in vfio_pci_sriov_uapi_test.c route all modes with a non-NULL mode->container_path through vfio_pci_container_setup() -> vfio_pci_group_setup(), which opens /dev/vfio/<group_id> and asserts on failure when CONFIG_VFIO_GROUP is disabled.

Quoted 3 lines

Should iommu_mode_probe() also verify that VFIO group support is available (such as checking access("/sys/class/vfio", F_OK) == 0) when mode->container_path is non-NULL?

Good catch. I will add a check for /sys/class/vfio in the next version.

author maintainer
#5 lore raw

PATCH v2 3/5 vfio: selftests: Print the known IOMMU modes on an unrecognized mode

In reply to David Matlack

Print the names of all known IOMMU modes when iommu_init() is passed an unrecognized IOMMU mode, and expose the list of names via iommu_mode_names() so that tests can also print it, e.g. in their help output.

Users can specify the IOMMU mode for some tests on the command line (e.g. irq_test -t), and so a typo or unsupported mode should tell the user what the valid options are rather than leaving them to go read the library.

---
 .../vfio/lib/include/libvfio/iommu.h          |  3 +++
 tools/testing/selftests/vfio/lib/iommu.c      | 20 ++++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +3 -0
index f204c71494fe..42d1de742c0c 100644
@@ -51,6 +51,9 @@ 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(void);
+
 int __iommu_map(struct iommu *iommu, struct dma_region *region);
 
 static inline void iommu_map(struct iommu *iommu, struct dma_region *region)
tools/testing/selftests/vfio/lib/iommu.c +19 -1
index 526bb21f2766..64614325c49b 100644
@@ -90,6 +90,23 @@ static const struct iommu_mode *iommu_mode_detect(void)
 	return NULL;
 }
 
+const char *iommu_mode_names(void)
+{
+	static char names[256];
+	int i, len = 0;
+
+	if (names[0])
+		return names;
+
+	for (i = 0; i < ARRAY_SIZE(iommu_modes); i++) {
+		len += snprintf(names + len, sizeof(names) - len, "%s%s",
+				i ? ", " : "", iommu_modes[i].name);
+		VFIO_ASSERT_LT(len, (int)sizeof(names));
+	}
+
+	return names;
+}
+
 /*
  * 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).
@@ -108,7 +125,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());
 }
 
 bool iommu_mode_is_available(const char *iommu_mode)
Signature · 1 line
2.56.0.385.gac671c819a-goog
author maintainer
#6 lore raw

PATCH v2 4/5 KVM: selftests: Use VFIO selftests library to pick IOMMU mode in irq_test

In reply to David Matlack

Use the VFIO selftests library to pick the IOMMU mode in irq_test and drop probe_iommu_type(). Pass NULL to iommu_init() when the user does not specify a mode with -t, and skip the test if the requested mode (or, if NULL, any mode) is not available.

The VFIO selftests library now automatically picks the IOMMU mode that is most likely to work on the current system, so there is no need for each test to probe for a mode itself.

---
 tools/testing/selftests/kvm/irq_test.c | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)
tools/testing/selftests/kvm/irq_test.c +2 -18
index 791e576c334c..8a345472df5f 100644
@@ -148,22 +148,6 @@ static void kvm_set_empty_gsi_routing(struct kvm_vm *vm)
 	vm_ioctl(vm, KVM_SET_GSI_ROUTING, &routing);
 }
 
-static const char *probe_iommu_type(void)
-{
-	int io_fd;
-
-	io_fd = open("/dev/iommu", O_RDONLY);
-	if (io_fd >= 0) {
-		close(io_fd);
-		return MODE_IOMMUFD;
-	}
-
-	io_fd = __open_path_or_exit("/dev/vfio/vfio", O_RDONLY,
-				    "Is VFIO (or IOMMUFD) loaded and enabled?");
-	close(io_fd);
-	return MODE_VFIO_TYPE1_IOMMU;
-}
-
 static void help(const char *name)
 {
 	printf("Usage: %s [-a] [-d <segment:bus:device.function>] [-e] [-h] [-i nr_irqs] [-m] [-n] [-t iommu_type] [-v nr_vcpus] [-x]\n", name);
@@ -266,8 +250,8 @@ int main(int argc, char **argv)
 	}
 
 	if (device_bdf) {
-		if (!iommu_type)
-			iommu_type = probe_iommu_type();
+		__TEST_REQUIRE(iommu_mode_is_available(iommu_type), "%s",
+			       iommu_mode_unavailable_msg(iommu_type));
 		iommu = iommu_init(iommu_type);
 		device = vfio_pci_device_init(device_bdf, iommu);
 		msix = vfio_setup_msi(device);
Signature · 1 line
2.56.0.385.gac671c819a-goog
author maintainer
#7 lore raw

PATCH v2 5/5 KVM: selftests: List the known IOMMU modes in irq_test's help output

In reply to David Matlack

List the names of all known IOMMU modes in the help output for irq_test's -t option, using iommu_mode_names() from the VFIO selftests library, and clarify that the IOMMU mode is auto-detected by default.

The help output currently claims that the only valid modes are vfio_type1_iommu and iommufd, which was never true, and would get stale any time a mode is added to the VFIO selftests library.

---
 tools/testing/selftests/kvm/irq_test.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/irq_test.c +2 -1
index 8a345472df5f..ef69f9d7ac3d 100644
@@ -159,7 +159,8 @@ static void help(const char *name)
 	printf("-i	The number of IRQs to generate during the test\n");
 	printf("-m	Pin target vCPU to random physical CPU before triggering interrupt\n");
 	printf("-n	Deliver 50 percent of IRQs as non-maskable interrupts\n");
-	printf("-t	Override the IOMMU type to use (vfio_type1_iommu or iommufd)\n");
+	printf("-t	Override the IOMMU mode to use instead of auto-detecting it (%s)\n",
+	       iommu_mode_names());
 	printf("-v	Number of vCPUS to run\n");
 	printf("-x	Use xAPIC mode instead of x2APIC mode in the guest\n");
 	printf("\n");
Signature · 1 line
2.56.0.385.gac671c819a-goog
#8 lore raw

Re: [PATCH v2 5/5] KVM: selftests: List the known IOMMU modes in irq_test's help output

tools/testing/selftests/kvm/irq_test.c static void help(const char *name)

Quoted 18 lines · David Matlack

List the names of all known IOMMU modes in the help output for irq_test's -t option, using iommu_mode_names() from the VFIO selftests library, and clarify that the IOMMU mode is auto-detected by default.

The help output currently claims that the only valid modes are vfio_type1_iommu and iommufd, which was never true, and would get stale any time a mode is added to the VFIO selftests library.

---
 tools/testing/selftests/kvm/irq_test.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/irq_test.c +0 -0
index 8a345472df5f..ef69f9d7ac3d 100644
@@ -159,7 +159,8 @@ static void help(const char *name)

If you're going to do s/type/mode below, then the initial help lines should also use iommu_mode.

tools/testing/selftests/kvm/irq_test.c static void help(const char *name) · line 162

Quoted 5 lines
tools/testing/selftests/kvm/irq_test.c +1 -1
 	printf("-i	The number of IRQs to generate during the test\n");
 	printf("-m	Pin target vCPU to random physical CPU before triggering interrupt\n");
 	printf("-n	Deliver 50 percent of IRQs as non-maskable interrupts\n");
-	printf("-t	Override the IOMMU type to use (vfio_type1_iommu or iommufd)\n");
+	printf("-t	Override the IOMMU mode to use instead of auto-detecting it (%s)\n",

IMO, the "auto-detecting" blurb does more harm than good. It makes the code harder to read, and for me at least it's just extra noise in understanding what the '-t' option does. It also makes the line reaaaaaaally long.

I know the parentheses are pre-existing, but that was somewhat ok when there were only two options. With a big list, it's again noisy to reach the end and have to think about that the closing ')' is terminating.

What about this?

	printf("-t      Override the IOMMU mode.  Options: %s\n", iommu_mode_names());

To go from:

-t Override the IOMMU mode to use instead of auto-detecting it (iommufd, vfio_type1v2_iommu, vfio_type1_iommu, iommufd_compat_type1v2, iommufd_compat_type1)

to

-t Override the IOMMU mode. Options: iommufd, vfio_type1v2_iommu, vfio_type1_iommu, iommufd_compat_type1v2, iommufd_compat_type1

tools/testing/selftests/kvm/irq_test.c static void help(const char *name) · line 163

Quoted 1 line
tools/testing/selftests/kvm/irq_test.c +1 -0
+	       iommu_mode_names());

This really should spit out only the available modes. For the internal assert, it makes sense to print all modes since that's just doing a lookup, but it's annoying to have to manually probe to see what's available.

Juggling all vs. available is mildly tricky to avoid having to dynamically allocate, but not that hard in the end. And IMO, it's totally ok to print a misleading message if a mode becomes (un)available mid-test, especially since this helper is unlikely to be used except in terminal situations.

E.g.

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));
	}

	if (!names[0])
		return NO_IOMMU_MODE_AVAILABLE_MSG;

	return names;
}

const char *iommu_mode_names(void)

{
	return __iommu_mode_names(false);
}

Side topic, the use of parantheses in iommu_mode_lookup() also yields hard to read output. E.g. if there's a typo in the command line, the error message is rather hard to visually parse:

Unrecognized IOMMU mode: a (known modes: iommufd, vfio_type1v2_iommu, vfio_type1_iommu, iommufd_compat_type1v2, iommufd_compat_type1)

Coupled with the above, how about this:

	VFIO_FAIL("Unrecognized IOMMU mode '%s'.  Known modes: %s.\n",
		  iommu_mode, __iommu_mode_names(true));

to generate:

Unrecognized IOMMU mode 'a'. Known modes: iommufd, vfio_type1v2_iommu, vfio_type1_iommu, iommufd_compat_type1v2, iommufd_compat_type1.

Which to me makes it much easier to identify the bad input.

Quoted 6 lines
tools/testing/selftests/kvm/irq_test.c +0 -0
 	printf("-v	Number of vCPUS to run\n");
 	printf("-x	Use xAPIC mode instead of x2APIC mode in the guest\n");
 	printf("\n");
Signature · 1 line
2.56.0.385.gac671c819a-goog
author maintainer
#9 lore raw

Re: [PATCH v2 5/5] KVM: selftests: List the known IOMMU modes in irq_test's help output

Quoted 5 lines · Sean Christopherson

Review static void help(const char *name)

Quoted 1 line · David Matlack
(quoted diff) +0 -0
@@ -159,7 +159,8 @@ static void help(const char *name)

If you're going to do s/type/mode below, then the initial help lines should also use iommu_mode.

Will do. I'll also rename the iommu_type variable in main() to iommu_mode.

Quoted 10 lines

tools/testing/selftests/kvm/irq_test.c static void help(const char *name) · line 162

Quoted 2 lines
tools/testing/selftests/kvm/irq_test.c +1 -1
-	printf("-t	Override the IOMMU type to use (vfio_type1_iommu or iommufd)\n");
+	printf("-t	Override the IOMMU mode to use instead of auto-detecting it (%s)\n",

IMO, the "auto-detecting" blurb does more harm than good. It makes the code harder to read, and for me at least it's just extra noise in understanding what the '-t' option does. It also makes the line reaaaaaaally long.

I know the parentheses are pre-existing, but that was somewhat ok when there were only two options. With a big list, it's again noisy to reach the end and have to think about that the closing ')' is terminating.

Agreed that the auto-detect blurb and the parentheses are just noise. I went with a slight variation, see below.

Quoted 36 lines

This really should spit out only the available modes. For the internal assert, it makes sense to print all modes since that's just doing a lookup, but it's annoying to have to manually probe to see what's available.

Juggling all vs. available is mildly tricky to avoid having to dynamically allocate, but not that hard in the end. And IMO, it's totally ok to print a misleading message if a mode becomes (un)available mid-test, especially since this helper is unlikely to be used except in terminal situations.

E.g.

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));
	}

	if (!names[0])
		return NO_IOMMU_MODE_AVAILABLE_MSG;

	return names;
}
Quoted 4 lines

const char *iommu_mode_names(void)

{
	return __iommu_mode_names(false);
}

How about listing both? That keeps -h consistent with how other KVM selftests list the options for their arguments (e.g. backing_src_help() lists every backing src type), while also showing which modes can actually be used. e.g. on a system with only the legacy VFIO container:

  # ./irq_test -h
  ...
  -t	Override the IOMMU mode to use
  	Options: iommufd, vfio_type1v2_iommu, vfio_type1_iommu, iommufd_compat_type1v2, iommufd_compat_type1
  	Available: vfio_type1v2_iommu, vfio_type1_iommu

The available modes also get printed when the requested mode is not available, i.e. when the __TEST_REQUIRE() fails:

  # ./irq_test -d 0000:01:00.0 -t iommufd
  1..0 # SKIP - IOMMU mode 'iommufd' is not available.  Available modes: vfio_type1v2_iommu, vfio_type1_iommu.

libvfio exposes iommu_mode_names_all() and iommu_mode_names_available() for this, built on your helper for juggling all vs. available.

Quoted 4 lines

Coupled with the above, how about this:

	VFIO_FAIL("Unrecognized IOMMU mode '%s'.  Known modes: %s.\n",
		  iommu_mode, __iommu_mode_names(true));

Will do.

Thanks!

#10 lore raw

Re: [PATCH v2 5/5] KVM: selftests: List the known IOMMU modes in irq_test's help output

Quoted 11 lines · David Matlack
Quoted 4 lines · Sean Christopherson

const char *iommu_mode_names(void)

{
	return __iommu_mode_names(false);
}

How about listing both? That keeps -h consistent with how other KVM selftests list the options for their arguments (e.g. backing_src_help() lists every backing src type), while also showing which modes can actually be used. e.g. on a system with only the legacy VFIO container:

Quoted 5 lines
  # ./irq_test -h
  ...
  -t	Override the IOMMU mode to use
  	Options: iommufd, vfio_type1v2_iommu, vfio_type1_iommu, iommufd_compat_type1v2, iommufd_compat_type1
  	Available: vfio_type1v2_iommu, vfio_type1_iommu

That'd be awesome!

Quoted 8 lines

The available modes also get printed when the requested mode is not available, i.e. when the __TEST_REQUIRE() fails:

  # ./irq_test -d 0000:01:00.0 -t iommufd
  1..0 # SKIP - IOMMU mode 'iommufd' is not available.  Available modes: vfio_type1v2_iommu, vfio_type1_iommu.

libvfio exposes iommu_mode_names_all() and iommu_mode_names_available() for this, built on your helper for juggling all vs. available.