mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/5] vfio: selftests: Automatically detect the IOMMU mode
@ 2026-11-03  0:22 David Matlack
  2026-11-03  0:22 ` [PATCH v2 1/5] vfio: selftests: Automatically detect the IOMMU mode in iommu_init() David Matlack
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: David Matlack @ 2026-11-03  0:22 UTC (permalink / raw)
  To: kvm, linux-kernel, linux-kselftest
  Cc: Alex Williamson, David Matlack, Paolo Bonzini,
	Sean Christopherson, Shuah Khan

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/20261029221710.1871112-1-dmatlack@google.com/

[1] https://lore.kernel.org/kvm/aiIG2L_KdPwuxYA1@google.com/

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
-- 
2.56.0.385.gac671c819a-goog


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-03  0:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-03  0:22 [PATCH v2 0/5] vfio: selftests: Automatically detect the IOMMU mode David Matlack
2026-11-03  0:22 ` [PATCH v2 1/5] vfio: selftests: Automatically detect the IOMMU mode in iommu_init() David Matlack
2026-11-03  0:22 ` [PATCH v2 2/5] vfio: selftests: Skip tests when the IOMMU mode is not available David Matlack
2026-11-03  0:22 ` [PATCH v2 3/5] vfio: selftests: Print the known IOMMU modes on an unrecognized mode David Matlack
2026-11-03  0:22 ` [PATCH v2 4/5] KVM: selftests: Use VFIO selftests library to pick IOMMU mode in irq_test David Matlack
2026-11-03  0:22 ` [PATCH v2 5/5] KVM: selftests: List the known IOMMU modes in irq_test's help output David Matlack

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®