[PATCH v3 0/5] vfio: selftests: Automatically detect the IOMMU mode

From: David Matlack

Date: Mon Nov 09 2026 - 16:12:46 EST


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 [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 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]

irq_test avoids this by probing for the IOMMU mode itself, but its probe
picks iommufd whenever /dev/iommu exists, even if VFIO device cdevs are
not available.

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.

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.

Patch 3 makes the VFIO selftests library print the names of all known
IOMMU modes when passed an unrecognized mode, and the names of the
available modes when a test skips because the mode it requested is not
available. It also adds iommu_mode_names_all() and
iommu_mode_names_available() so that tests can print both lists.

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 and available IOMMU modes in irq_test's help
output.

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

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 -h lists all five IOMMU modes as
available, and irq_test -d -t bogus fails and lists the known modes.

- Only the legacy VFIO container available (/dev/iommu deleted): All
VFIO selftests pass. The iommufd and iommufd_compat variants and
vfio_iommufd_setup_test skip instead of failing. irq_test -d passes
using vfio_type1v2_iommu. irq_test -d -t iommufd skips and lists the
available modes, as does irq_test -h.

- No IOMMU mode available (/dev/iommu and /dev/vfio/vfio deleted): All
VFIO selftests and irq_test -d skip with a message explaining why,
and with well-formed TAP output. irq_test -h lists no available
modes.

Also tested with CONFIG_VFIO_GROUP=n (cdev-only VFIO):

- All VFIO selftests pass, with the iommufd variants running and the
vfio_type1* and iommufd_compat variants skipped. Without the VFIO
group check added in v3, the iommufd_compat variants fail instead.
irq_test -d passes using iommufd, and irq_test -h lists only iommufd
as available.

Changelog
---------

v3:
- Require VFIO group support (CONFIG_VFIO_GROUP) for container modes,
including the iommufd_compat modes (patch 1) [Sashiko [2]]
- List the available IOMMU modes in irq_test's help output, alongside
all known modes, and when the requested mode is not available
(patches 3 and 5) [Sean]
- Simplify the -t help text in irq_test (patch 5) [Sean]
- Make the unrecognized IOMMU mode error easier to read (patch 3)
[Sean]
- Rename iommu_type to iommu_mode in irq_test (patches 4 and 5) [Sean]
- Skip vfio_iommufd_setup_test when iommufd is not available (patch 2)

v2: https://lore.kernel.org/kvm/20261103002205.3348519-1-dmatlack@xxxxxxxxxx/
v1: https://lore.kernel.org/kvm/20261029221710.1871112-1-dmatlack@xxxxxxxxxx/

[1] https://lore.kernel.org/kvm/aiIG2L_KdPwuxYA1@xxxxxxxxxx/
[2] https://lore.kernel.org/kvm/sashiko-outbox-194380@xxxxxxxxxx/

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 IOMMU modes when a requested mode can't be
used
KVM: selftests: Use VFIO selftests library to pick IOMMU mode in
irq_test
KVM: selftests: List the IOMMU modes in irq_test's help output

tools/testing/selftests/kvm/irq_test.c | 32 ++--
.../vfio/lib/include/libvfio/iommu.h | 43 +++++-
tools/testing/selftests/vfio/lib/iommu.c | 139 ++++++++++++++++--
.../vfio/vfio_dma_mapping_mmio_test.c | 2 +
.../selftests/vfio/vfio_dma_mapping_test.c | 4 +
.../selftests/vfio/vfio_iommufd_setup_test.c | 2 +
.../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 +
10 files changed, 205 insertions(+), 40 deletions(-)


base-commit: 076d7e989cb69f6ded7d60f2a080eb909930061b
--
2.56.0.506.g506a7eabc5-goog