vfio: Introduce selftests for VFIO
Subsystems (6)
David Matlack
maintainercover vfio: Introduce selftests for VFIO
This series introduces VFIO selftests, located intools/testing/selftests/vfio/.
VFIO selftests aim to enable kernel developers to write and run tests that take the form of userspace programs that interact with VFIO and IOMMUFD uAPIs. VFIO selftests can be used to write functional tests for new features, regression tests for bugs, and performance tests for optimizations.
These tests are designed to interact with real PCI devices, i.e. they do not rely on mocking out or faking any behavior in the kernel. This allows the tests to exercise not only VFIO but also IOMMUFD, the IOMMU driver, interrupt remapping, IRQ handling, etc.
For more background on the motivation and design of this series, please see the RFC:
https://lore.kernel.org/kvm/[email protected]/
This series can also be found on GitHub:
https://github.com/dmatlack/linux/tree/vfio/selftests/v1
Changelog
-----------------------------------------------------------------------
RFC: https://lore.kernel.org/kvm/[email protected]/
- Add symlink to linux/pci_ids.h instead of copying (Jason)
- Add symlinks to drivers/dma/*/*.h instead of copying (Jason)
- Automatically replicate vfio_dma_mapping_test across backing sources using fixture variants (Jason)
- Automatically replicate vfio_dma_mapping_test and vfio_pci_driver_test across all iommu_modes using fixture variants (Jason)
- Invert access() check in vfio_dma_mapping_test (me)
- Use driver_override instead of add/remove_id (Alex)
- Allow tests to get BDF from env var (Alex)
- Use KSFT_FAIL instead of 1 to exit with failure (Alex)
- Unconditionally create $(LIBVFIO_O_DIRS) to avoid target conflict with ../cgroup/lib/libcgroup.mk when building KVM selftests (me)
- Allow VFIO selftests to run automatically by switching from TEST_GEN_PROGS_EXTENDED to TEST_GEN_PROGS. Automatically run selftests will use $VFIO_SELFTESTS_BDF environment variable to know which device to use (Alex)
- Replace hardcoded SZ_4K with getpagesize() in vfio_dma_mapping_test to support platforms with other page sizes (me)
- Make all global variables static where possible (me)
- Pass argc and argv to test_harness_main() so that users can pass flags to the kselftest harness (me)
Instructions
-----------------------------------------------------------------------
Running VFIO selftests requires at a PCI device bound to vfio-pci for the tests to use. The address of this device is passed to the test as a segment:bus:device.function string, which must match the path to the device in /sys/bus/pci/devices/ (e.g. 0000:00:04.0).
Once you have chosen a device, there is a helper script provided to unbind the device from its current driver, bind it to vfio-pci, export the environment variable $VFIO_SELFTESTS_BDF, and launch a shell:
$ tools/testing/selftests/vfio/run.sh -d 0000:00:04.0 -s
The -d option tells the script which device to use and the -s option tells the script to launch a shell.
Additionally, the VFIO selftest vfio_dma_mapping_test has test cases that rely on HugeTLB pages being available, otherwise they are skipped. To enable those tests make sure at least 1 2MB and 1 1GB HugeTLB pages are available.
$ echo 1 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages $ echo 1 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
To run all VFIO selftests using make:
$ make -C tools/testing/selftests/vfio run_tests
To run individual tests:
$ tools/testing/selftests/vfio/vfio_dma_mapping_test $ tools/testing/selftests/vfio/vfio_dma_mapping_test -v iommufd_anonymous_hugetlb_2mb $ tools/testing/selftests/vfio/vfio_dma_mapping_test -r vfio_dma_mapping_test.iommufd_anonymous_hugetlb_2mb.dma_map_unmap
The environment variable $VFIO_SELFTESTS_BDF can be overridden for a specific test by passing in the BDF on the command line as the last positional argument.
$ tools/testing/selftests/vfio/vfio_dma_mapping_test 0000:00:04.0 $ tools/testing/selftests/vfio/vfio_dma_mapping_test -v iommufd_anonymous_hugetlb_2mb 0000:00:04.0 $ tools/testing/selftests/vfio/vfio_dma_mapping_test -r vfio_dma_mapping_test.iommufd_anonymous_hugetlb_2mb.dma_map_unmap 0000:00:04.0
When you are done, free the HugeTLB pages and exit the shell started by run.sh. Exiting the shell will cause the device to be unbound from vfio-pci and bound back to its original driver.
$ echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages $ echo 0 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages $ exit
It's also possible to use run.sh to run just a single test hermetically, rather than dropping into a shell:
$ tools/testing/selftests/vfio/run.sh -d 0000:00:04.0 -- tools/testing/selftests/vfio/vfio_dma_mapping_test -v iommufd_anonymous
Tests
-----------------------------------------------------------------------
There are 5 tests in this series, mostly to demonstrate as a proof-of-concept:
- tools/testing/selftests/vfio/vfio_pci_device_test.c
- tools/testing/selftests/vfio/vfio_pci_driver_test.c
- tools/testing/selftests/vfio/vfio_iommufd_setup_test.c
- tools/testing/selftests/vfio/vfio_dma_mapping_test.c
- tools/testing/selftests/kvm/vfio_pci_device_irq_test.c
Future Areas of Development
-----------------------------------------------------------------------
Library:
- Driver support for devices that can be used on AMD, ARM, and other platforms (e.g. mlx5).
- Driver support for a device available in QEMU VMs (e.g. pcie-ats-testdev [1])
- Support for tests that use multiple devices.
- Support for IOMMU groups with multiple devices.
- Support for multiple devices sharing the same container/iommufd.
- Sharing TEST_ASSERT() macros and other common code between KVM and VFIO selftests.
Tests:
- DMA mapping performance tests for BARs/HugeTLB/etc.
- Porting tests from
https://github.com/awilliam/tests/commits/for-clg/ to selftests.
- Live Update selftests.
- Porting Sean's KVM selftest for posted interrupts to use the VFIO selftests library [2]
[1] https://github.com/Joelgranados/qemu/blob/pcie-testdev/hw/misc/pcie-ats-testdev.c
[2] https://lore.kernel.org/kvm/[email protected]/
David Matlack (28):
selftests: Create tools/testing/selftests/vfio
vfio: selftests: Add a helper library for VFIO selftests
vfio: selftests: Introduce vfio_pci_device_test
tools headers: Add stub definition for __iomem
tools headers: Import asm-generic MMIO helpers
tools headers: Import x86 MMIO helper overrides
tools headers: Import iosubmit_cmds512()
tools headers: Add symlink to linux/pci_ids.h
vfio: selftests: Keep track of DMA regions mapped into the device
vfio: selftests: Enable asserting MSI eventfds not firing
vfio: selftests: Add a helper for matching vendor+device IDs
vfio: selftests: Add driver framework
vfio: sefltests: Add vfio_pci_driver_test
dmaengine: ioat: Move system_has_dca_enabled() to dma.h
vfio: selftests: Add driver for Intel CBDMA
dmaengine: idxd: Allow registers.h to be included from tools/
vfio: selftests: Add driver for Intel DSA
vfio: selftests: Move helper to get cdev path to libvfio
vfio: selftests: Encapsulate IOMMU mode
vfio: selftests: Replicate tests across all iommu_modes
vfio: selftests: Add vfio_type1v2_mode
vfio: selftests: Add iommufd_compat_type1{,v2} modes
vfio: selftests: Add iommufd mode
vfio: selftests: Make iommufd the default iommu_mode
vfio: selftests: Add a script to help with running VFIO selftests
KVM: selftests: Build and link sefltests/vfio/lib into KVM selftests
KVM: selftests: Test sending a vfio-pci device IRQ to a VM
KVM: selftests: Add -d option to vfio_pci_device_irq_test for
device-sent MSIs
Josh Hilke (5):
vfio: selftests: Test basic VFIO and IOMMUFD integration
vfio: selftests: Move vfio dma mapping test to their own file
vfio: selftests: Add test to reset vfio device.
vfio: selftests: Add DMA mapping tests for 2M and 1G HugeTLB
vfio: selftests: Validate 2M/1G HugeTLB are mapped as 2M/1G in IOMMU
MAINTAINERS | 7 + drivers/dma/idxd/registers.h | 4 + drivers/dma/ioat/dma.h | 2 + drivers/dma/ioat/hw.h | 3 - tools/arch/x86/include/asm/io.h | 101 +++ tools/arch/x86/include/asm/special_insns.h | 27 + tools/include/asm-generic/io.h | 482 ++++++++++++++ tools/include/asm/io.h | 11 + tools/include/linux/compiler.h | 4 + tools/include/linux/io.h | 4 +- tools/include/linux/pci_ids.h | 1 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/kvm/Makefile.kvm | 4 + .../testing/selftests/kvm/include/kvm_util.h | 4 + tools/testing/selftests/kvm/lib/kvm_util.c | 21 + .../selftests/kvm/vfio_pci_device_irq_test.c | 172 +++++ tools/testing/selftests/vfio/.gitignore | 7 + tools/testing/selftests/vfio/Makefile | 21 + .../selftests/vfio/lib/drivers/dsa/dsa.c | 416 ++++++++++++ .../vfio/lib/drivers/dsa/registers.h | 1 + .../selftests/vfio/lib/drivers/ioat/hw.h | 1 + .../selftests/vfio/lib/drivers/ioat/ioat.c | 235 +++++++ .../vfio/lib/drivers/ioat/registers.h | 1 + .../selftests/vfio/lib/include/vfio_util.h | 295 +++++++++ tools/testing/selftests/vfio/lib/libvfio.mk | 24 + .../selftests/vfio/lib/vfio_pci_device.c | 594 ++++++++++++++++++ .../selftests/vfio/lib/vfio_pci_driver.c | 126 ++++ tools/testing/selftests/vfio/run.sh | 109 ++++ .../selftests/vfio/vfio_dma_mapping_test.c | 199 ++++++ .../selftests/vfio/vfio_iommufd_setup_test.c | 127 ++++ .../selftests/vfio/vfio_pci_device_test.c | 176 ++++++ .../selftests/vfio/vfio_pci_driver_test.c | 247 ++++++++ 32 files changed, 3423 insertions(+), 4 deletions(-) create mode 100644 tools/arch/x86/include/asm/io.h create mode 100644 tools/arch/x86/include/asm/special_insns.h create mode 100644 tools/include/asm-generic/io.h create mode 100644 tools/include/asm/io.h create mode 120000 tools/include/linux/pci_ids.h create mode 100644 tools/testing/selftests/kvm/vfio_pci_device_irq_test.c create mode 100644 tools/testing/selftests/vfio/.gitignore create mode 100644 tools/testing/selftests/vfio/Makefile create mode 100644 tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c create mode 120000 tools/testing/selftests/vfio/lib/drivers/dsa/registers.h create mode 120000 tools/testing/selftests/vfio/lib/drivers/ioat/hw.h create mode 100644 tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c create mode 120000 tools/testing/selftests/vfio/lib/drivers/ioat/registers.h create mode 100644 tools/testing/selftests/vfio/lib/include/vfio_util.h create mode 100644 tools/testing/selftests/vfio/lib/libvfio.mk create mode 100644 tools/testing/selftests/vfio/lib/vfio_pci_device.c create mode 100644 tools/testing/selftests/vfio/lib/vfio_pci_driver.c create mode 100755 tools/testing/selftests/vfio/run.sh create mode 100644 tools/testing/selftests/vfio/vfio_dma_mapping_test.c create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_setup_test.c create mode 100644 tools/testing/selftests/vfio/vfio_pci_device_test.c create mode 100644 tools/testing/selftests/vfio/vfio_pci_driver_test.c
base-commit: e271ed52b344ac02d4581286961d0c40acc54c03
prerequisite-patch-id: c1decca4653262d3d2451e6fd4422ebff9c0b589
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 1/33 selftests: Create tools/testing/selftests/vfio
Create the directory tools/testing/selftests/vfio with a stub Makefile and hook it up to the top-level selftests Makefile.
This directory will be used in subsequent commits to host selftests for the VFIO subsystem.
--- MAINTAINERS | 7 +++++++ tools/testing/selftests/Makefile | 1 + tools/testing/selftests/vfio/.gitignore | 7 +++++++ tools/testing/selftests/vfio/Makefile | 2 ++ 4 files changed, 17 insertions(+) create mode 100644 tools/testing/selftests/vfio/.gitignore create mode 100644 tools/testing/selftests/vfio/Makefile
MAINTAINERS +7 -0
@@ -25900,6 +25900,7 @@ F: drivers/vfio/ F: include/linux/vfio.h F: include/linux/vfio_pci_core.h F: include/uapi/linux/vfio.h +F: tools/testing/selftests/vfio/ VFIO FSL-MC DRIVER L: [email protected]
@@ -25972,6 +25973,12 @@ L: [email protected] S: Maintained F: drivers/vfio/pci/virtio +VFIO SELFTESTS +R: David Matlack <[email protected]> +L: [email protected] +S: Maintained +F: tools/testing/selftests/vfio/ + VGA_SWITCHEROO R: Lukas Wunner <[email protected]> S: Maintained
tools/testing/selftests/Makefile +1 -0
@@ -123,6 +123,7 @@ TARGETS += uevent TARGETS += user_events TARGETS += vDSO TARGETS += mm +TARGETS += vfio TARGETS += x86 TARGETS += x86/bugs TARGETS += zram
tools/testing/selftests/vfio/.gitignore new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-only +* +!/**/ +!*.c +!*.h +!*.S +!*.sh
tools/testing/selftests/vfio/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +CFLAGS = $(KHDR_INCLUDES) +include ../lib.mk
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 2/33 vfio: selftests: Add a helper library for VFIO selftests
In reply to David MatlackAdd a basic helper library to be used by VFIO selftests.
The basic unit of the library is struct vfio_pci_device, which represents a single PCI device that is bound to the vfio-pci driver. The library currently only supports a single device per group and container, and VFIO IOMMU types.
The code in this library was heavily based on prior work done by Raghavendra Rao Ananta <[email protected]>, and the VFIO_ASSERT*() macros were written by Vipin Sharma <[email protected]>.
Separate that Makefile rules for building the library into a separate script so that the library can be built by and linked into KVM selftests in a subsequent commit.
--- tools/testing/selftests/vfio/Makefile | 14 + .../selftests/vfio/lib/include/vfio_util.h | 140 +++++++ tools/testing/selftests/vfio/lib/libvfio.mk | 15 + .../selftests/vfio/lib/vfio_pci_device.c | 365 ++++++++++++++++++ 4 files changed, 534 insertions(+) create mode 100644 tools/testing/selftests/vfio/lib/include/vfio_util.h create mode 100644 tools/testing/selftests/vfio/lib/libvfio.mk create mode 100644 tools/testing/selftests/vfio/lib/vfio_pci_device.c
tools/testing/selftests/vfio/Makefile +14 -0
@@ -1,2 +1,16 @@ CFLAGS = $(KHDR_INCLUDES) include ../lib.mk +include lib/libvfio.mk + +CFLAGS += -I$(top_srcdir)/tools/include +CFLAGS += -MD +CFLAGS += $(EXTRA_CFLAGS) + +$(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O) + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@ + +TEST_GEN_PROGS_O = $(patsubst %, %.o, $(TEST_GEN_PROGS)) +TEST_DEP_FILES = $(patsubst %.o, %.d, $(TEST_GEN_PROGS_O) $(LIBVFIO_O)) +-include $(TEST_DEP_FILES) + +EXTRA_CLEAN += $(TEST_GEN_PROGS_O) $(TEST_DEP_FILES)
tools/testing/selftests/vfio/lib/include/vfio_util.h new file +140 -0
140 lines not on this page · raw message
tools/testing/selftests/vfio/lib/libvfio.mk new file +15 -0
@@ -0,0 +1,15 @@ +VFIO_DIR := $(selfdir)/vfio + +LIBVFIO_C := lib/vfio_pci_device.c + +LIBVFIO_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBVFIO_C)) + +LIBVFIO_O_DIRS := $(shell dirname $(LIBVFIO_O) | uniq) +$(shell mkdir -p $(LIBVFIO_O_DIRS)) + +CFLAGS += -I$(VFIO_DIR)/lib/include + +$(LIBVFIO_O): $(OUTPUT)/%.o : $(VFIO_DIR)/%.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ + +EXTRA_CLEAN += $(LIBVFIO_O)
tools/testing/selftests/vfio/lib/vfio_pci_device.c new file +365 -0
365 lines not on this page · raw message
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 3/33 vfio: selftests: Introduce vfio_pci_device_test
In reply to David MatlackIntroduce a basic VFIO selftest called vfio_pci_device_test to demonstrate the functionality of the VFIO selftest library and provide some test coverage of basic VFIO operations, including:
- Mapping and unmapping DMA
- Mapping and unmapping BARs
- Enabling, triggering, and disabling MSI and MSI-x
- Reading and writing to PCI config space
This test should work with most PCI devices, as long as they are bound to vfio-pci.
--- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_pci_device_test.c | 178 ++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_pci_device_test.c
tools/testing/selftests/vfio/Makefile +1 -0
@@ -1,4 +1,5 @@ CFLAGS = $(KHDR_INCLUDES) +TEST_GEN_PROGS += vfio_pci_device_test include ../lib.mk include lib/libvfio.mk
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +178 -0
178 lines not on this page · raw message
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
Re: [PATCH 03/33] vfio: selftests: Introduce vfio_pci_device_test
Quoted 49 lines · David Matlack
Introduce a basic VFIO selftest called
vfio_pci_device_testto demonstrate the functionality of the VFIO selftest library and provide some test coverage of basic VFIO operations, including:- Mapping and unmapping DMA
- Mapping and unmapping BARs
- Enabling, triggering, and disabling MSI and MSI-x
- Reading and writing to PCI config spaceThis test should work with most PCI devices, as long as they are bound to vfio-pci.
Signed-off-by David Matlack <[email protected]>--- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_pci_device_test.c | 178 ++++++++++++++++++ 2 files changed, 179 insertions(+)create mode 100644
tools/testing/selftests/vfio/vfio_pci_device_test.ctools/testing/selftests/vfio/Makefile +1
-0@@ -1,4 +1,5 @@ CFLAGS = $(KHDR_INCLUDES) +TEST_GEN_PROGS += vfio_pci_device_test include ../lib.mk include lib/libvfio.mktools/testing/selftests/vfio/vfio_pci_device_test.c new file +14
-0@@ -0,0 +1,178 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <fcntl.h> +#include <stdlib.h> + +#include <sys/ioctl.h> +#include <sys/mman.h> + +#include <linux/limits.h> +#include <linux/pci_regs.h> +#include <linux/sizes.h> +#include <linux/vfio.h> + +#include <vfio_util.h> +
tools/testing/selftests/vfio/vfio_pci_device_test.c line 24
Quoted 10 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +10
-0+#include "../kselftest_harness.h" + +static const char *device_bdf; + +/* +* Limit the number of MSIs enabled/disabled by the test regardless of the +* number of MSIs the device itself supports, e.g. to avoid hitting IRTE limits. +*/ +#define MAX_TEST_MSI 16U +
Now that AMD IOMMU supports upto 2048 IRTEs per device, I wonder if we can include a test with max MSIs 2048.
Quoted 16 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +16
-0+FIXTURE (vfio_pci_device_test) { +struct vfio_pci_device *device; +}; + +FIXTURE_SETUP (vfio_pci_device_test) +{ + self->device =vfio_pci_device_init (device_bdf,VFIO_TYPE1_IOMMU ); +} + +FIXTURE_TEARDOWN (vfio_pci_device_test) +{ +vfio_pci_device_cleanup (self->device); +} + +TEST_F(vfio_pci_device_test, dma_map_unmap) +{
tools/testing/selftests/vfio/vfio_pci_device_test.c line 50
Quoted 10 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +10
-0+const u64 size =SZ_2M ; +const u64 iova =SZ_4G ; +void *mem; + + mem =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); +ASSERT_NE (mem,MAP_FAILED ); + +vfio_pci_dma_map (self->device, iova, size, mem); +printf ("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n" , mem, size, iova); +vfio_pci_dma_unmap (self->device, iova, size);
I am slightly confused here. Because You are having an assert on munmap and not on any of the vfio_pci_dma_(map/unmap). This test case is not testing VFIO.
Thanks
Sairaj
Quoted 4 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +4
-0+ +ASSERT_TRUE (!munmap (mem,SZ_2M )); +} +
Re: [PATCH 03/33] vfio: selftests: Introduce vfio_pci_device_test
Quoted 89 lines · Sairaj Kodilkar
Quoted 58 lines · David Matlack
Introduce a basic VFIO selftest called
vfio_pci_device_testto demonstrate the functionality of the VFIO selftest library and provide some test coverage of basic VFIO operations, including:- Mapping and unmapping DMA
- Mapping and unmapping BARs
- Enabling, triggering, and disabling MSI and MSI-x
- Reading and writing to PCI config spaceThis test should work with most PCI devices, as long as they are bound to vfio-pci.
Signed-off-by David Matlack <[email protected]>---
tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_pci_device_test.c | 178 ++++++++++++++++++2 files changed, 179 insertions(+)create mode 100644
tools/testing/selftests/vfio/vfio_pci_device_test.ctools/testing/selftests/vfio/Makefile +1
-0@@ -1,4 +1,5 @@ CFLAGS = $(KHDR_INCLUDES) +TEST_GEN_PROGS += vfio_pci_device_testinclude
../lib.mk
includelib/libvfio.mktools/testing/selftests/vfio/vfio_pci_device_test.c new file +20
-0@@ -0,0 +1,178 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <fcntl.h> +#include <stdlib.h> + +#include <sys/ioctl.h> +#include <sys/mman.h> + +#include <linux/limits.h> +#include <linux/pci_regs.h> +#include <linux/sizes.h> +#include <linux/vfio.h> + +#include <vfio_util.h> + +#include "../kselftest_harness.h" + +static const char *device_bdf; + +/* +* Limit the number of MSIs enabled/disabled by the test regardless of the
+ * number of MSIs the device itself supports, e.g. to avoid hitting IRTE limits.
+ */tools/testing/selftests/vfio/vfio_pci_device_test.c
Quoted 2 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +2
-0+#define MAX_TEST_MSI 16U +Now that AMD IOMMU supports upto 2048 IRTEs per device, I wonder if we can include a test with max MSIs 2048.
Quoted 24 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +21
-0+FIXTURE (vfio_pci_device_test) { + struct vfio_pci_device *device; +}; + +FIXTURE_SETUP (vfio_pci_device_test) +{ + self->device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU); +} + +FIXTURE_TEARDOWN (vfio_pci_device_test) +{ + vfio_pci_device_cleanup(self->device); +} + +TEST_F(vfio_pci_device_test, dma_map_unmap) +{ + const u64 size = SZ_2M; + const u64 iova = SZ_4G; + void *mem; + + mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED |
MAP_ANONYMOUS, -1, 0);
+ASSERT_NE(mem, MAP_FAILED);
+
Quoted 8 lines
Quoted 4 lines
+
vfio_pci_dma_map(self->device, iova, size, mem);
+printf("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n", mem, size, iova);
+vfio_pci_dma_unmap(self->device, iova, size);I am slightly confused here. Because You are having an assert on munmap and not on any of the
vfio_pci_dma_(map/unmap). This test case is not testing VFIO.
I missed to see ioctl_assert. Please ignore this :) Sorry about that.
Quoted 8 lines
Thanks
Sairajtools/testing/selftests/vfio/vfio_pci_device_test.c line 54
Quoted 4 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +4
-0+ + ASSERT_TRUE(!munmap(mem, SZ_2M)); +} +
Thanks
Sairaj
David Matlack
maintainerRe: [PATCH 03/33] vfio: selftests: Introduce vfio_pci_device_test
Quoted 13 lines · Sairaj Kodilkar
Quoted 12 lines · Sairaj Kodilkar
Review
Quoted 8 lines · David Matlack
+/*
+ * Limit the number of MSIs enabled/disabled by the test regardless of the
+ * number of MSIs the device itself supports, e.g. to avoid hitting IRTE limits.
+ */(quoted diff) +2
-0+#define MAX_TEST_MSI 16U +Now that AMD IOMMU supports upto 2048 IRTEs per device, I wonder if we can include a test with max MSIs 2048.
That sounds worth doing. I originally added this because I was hitting IRTE limits on an Intel host and a ~6.6 kernel.
Is there some way the test can detect from userspace that the IOMMU supports 2048 IRTEs that we could key off to decide what value of MAX_TEST_MSI to use?
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +3
-0+ +vfio_pci_dma_map (self->device, iova, size, mem); + printf("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n", mem, size,iova);
+vfio_pci_dma_unmap(self->device, iova, size);
Quoted 5 lines
Quoted 3 lines
I am slightly confused here. Because You are having an assert on munmap and not on any of the
vfio_pci_dma_(map/unmap). This test case is not testing VFIO.I missed to see
ioctl_assert. Please ignore this :) Sorry about that.
No worries, it's not very obvious :)
vfio_pci_dma_map() and vfio_pci_dma_unmap() both return void right now and perform internal asserts since all current users of those functions want to assert success.
If and when we have a use-case to assert that map or unmap fails (which I think we'll definitely have) we can add __vfio_pci_dma_map() and __vfio_pci_dma_unmap() variants that return int instead of void.
Re: [PATCH 03/33] vfio: selftests: Introduce vfio_pci_device_test
Quoted 15 lines · David Matlack
Quoted 13 lines · Sairaj Kodilkar
Quoted 12 lines · Sairaj Kodilkar
Review
Quoted 8 lines · David Matlack
+/*
+ * Limit the number of MSIs enabled/disabled by the test regardless of the
+ * number of MSIs the device itself supports, e.g. to avoid hitting IRTE limits.
+ */(quoted diff) +2
-0+#define MAX_TEST_MSI 16U +Now that AMD IOMMU supports upto 2048 IRTEs per device, I wonder if we can include a test with max MSIs 2048.
Quoted 7 lines
That sounds worth doing. I originally added this because I was hitting IRTE limits on an Intel host and a ~6.6 kernel.
Is there some way the test can detect from userspace that the IOMMU supports 2048 IRTEs that we could key off to decide what value of
MAX_TEST_MSIto use?
The feature is published to userspace through
$ cat /sys/class/iommu/ivhd0/amd-iommu/features 25bf732fa2295afe:53d
The output is in format "efr1:efr2". The Bit 9-8 of efr2 shows the support for 2048 interrupts (efr2 & 0x300).
Please refer 3.4.13 Extended Feature 2 Register of IOMMU specs [1] for more details.
[1] https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/specifications/48882_IOMMU.pdf
Note that, when device is behind PCIe-PCI bridge the IOMMU may hit IRTE limit early as multiple devices share same IRTE table. (But this is a corner case and I doubt that 2K capable device is kept behind the bridge).
Thanks
Sairaj
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +3
-0+ +vfio_pci_dma_map (self->device, iova, size, mem); + printf("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n", mem, size,iova);
+vfio_pci_dma_unmap(self->device, iova, size);Quoted 5 lines
Quoted 3 lines
I am slightly confused here. Because You are having an assert on munmap and not on any of the
vfio_pci_dma_(map/unmap). This test case is not testing VFIO.I missed to see
ioctl_assert. Please ignore this :) Sorry about that.No worries, it's not very obvious :)
Quoted 7 lines
vfio_pci_dma_map()andvfio_pci_dma_unmap()both return void right now and perform internal asserts since all current users of those functions want to assert success.If and when we have a use-case to assert that map or unmap fails (which I think we'll definitely have) we can add
__vfio_pci_dma_map()and__vfio_pci_dma_unmap()variants that return int instead of void.
Yep we can. Another question, why do we need assert on mmunmap ? If mmunmap fails then its not really a fault of VFIO.
Thanks
Sairaj
David Matlack
maintainerRe: [PATCH 03/33] vfio: selftests: Introduce vfio_pci_device_test
Quoted 38 lines · Sairaj Kodilkar
Quoted 15 lines · David Matlack
Quoted 13 lines · Sairaj Kodilkar
Quoted 12 lines · Sairaj Kodilkar
Review
Quoted 8 lines · David Matlack
+/*
+ * Limit the number of MSIs enabled/disabled by the test regardless of the
+ * number of MSIs the device itself supports, e.g. to avoid hitting IRTE limits.
+ */(quoted diff) +2
-0+#define MAX_TEST_MSI 16U +Now that AMD IOMMU supports upto 2048 IRTEs per device, I wonder if we can include a test with max MSIs 2048.
Quoted 7 lines
That sounds worth doing. I originally added this because I was hitting IRTE limits on an Intel host and a ~6.6 kernel.
Is there some way the test can detect from userspace that the IOMMU supports 2048 IRTEs that we could key off to decide what value of
MAX_TEST_MSIto use?The feature is published to userspace through
$ cat /sys/class/iommu/ivhd0/amd-iommu/features 25bf732fa2295afe:53dThe output is in format "efr1:efr2". The Bit 9-8 of efr2 shows the support for 2048 interrupts (efr2 &
0x300).Please refer 3.4.13 Extended Feature 2 Register of IOMMU specs [1] for more details.
Quoted 7 lines
[1]
https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/specifications/48882_IOMMU.pdfNote that, when device is behind PCIe-PCI bridge the IOMMU may hit IRTE limit early as multiple devices share same IRTE table. (But this is a corner case and I doubt that 2K capable device is kept behind the bridge).
Thanks. We could definitely read that and allow up to 2048 MSIs in this test. Would you be ok if we defer that to a future commit though? This series is already quite big :)
Quoted 20 lines
Quoted 20 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +3
-0+ +vfio_pci_dma_map (self->device, iova, size, mem); + printf("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n", mem, size,iova);
+vfio_pci_dma_unmap(self->device, iova, size);Quoted 5 lines
Quoted 3 lines
I am slightly confused here. Because You are having an assert on munmap and not on any of the
vfio_pci_dma_(map/unmap). This test case is not testing VFIO.I missed to see
ioctl_assert. Please ignore this :) Sorry about that.No worries, it's not very obvious :)
vfio_pci_dma_map()andvfio_pci_dma_unmap()both return void right now and perform internal asserts since all current users of those functions want to assert success.
Quoted 6 lines
Quoted 3 lines
If and when we have a use-case to assert that map or unmap fails (which I think we'll definitely have) we can add
__vfio_pci_dma_map()and__vfio_pci_dma_unmap()variants that return int instead of void.Yep we can. Another question, why do we need assert on mmunmap ? If mmunmap fails then its not really a fault of VFIO.
You're right, it's very unlikely (almost impossible) to be VFIO's fault if munmap() fails. But it would be a sign of a bug in the test, so it is still good to detect so we can fix it.
Re: [PATCH 03/33] vfio: selftests: Introduce vfio_pci_device_test
Quoted 42 lines · David Matlack
Quoted 41 lines · Sairaj Kodilkar
Quoted 15 lines · David Matlack
Quoted 13 lines · Sairaj Kodilkar
Quoted 12 lines · Sairaj Kodilkar
Review
Quoted 8 lines · David Matlack
+/*
+ * Limit the number of MSIs enabled/disabled by the test regardless of the
+ * number of MSIs the device itself supports, e.g. to avoid hitting IRTE limits.
+ */(quoted diff) +2
-0+#define MAX_TEST_MSI 16U +Now that AMD IOMMU supports upto 2048 IRTEs per device, I wonder if we can include a test with max MSIs 2048.
Quoted 7 lines
That sounds worth doing. I originally added this because I was hitting IRTE limits on an Intel host and a ~6.6 kernel.
Is there some way the test can detect from userspace that the IOMMU supports 2048 IRTEs that we could key off to decide what value of
MAX_TEST_MSIto use?The feature is published to userspace through
$ cat /sys/class/iommu/ivhd0/amd-iommu/features 25bf732fa2295afe:53dThe output is in format "efr1:efr2". The Bit 9-8 of efr2 shows the support for 2048 interrupts (efr2 &
0x300).Please refer 3.4.13 Extended Feature 2 Register of IOMMU specs [1] for more details.
[1]
https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/specifications/48882_IOMMU.pdf
Quoted 9 lines
Quoted 4 lines
Note that, when device is behind PCIe-PCI bridge the IOMMU may hit IRTE limit early as multiple devices share same IRTE table. (But this is a corner case and I doubt that 2K capable device is kept behind the bridge).
Thanks. We could definitely read that and allow up to 2048 MSIs in this test. Would you be ok if we defer that to a future commit though? This series is already quite big :)
Sure ! no problem.
Quoted 24 lines
Quoted 24 lines
Quoted 23 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
tools/testing/selftests/vfio/vfio_pci_device_test.c new file +3
-0+ +vfio_pci_dma_map (self->device, iova, size, mem); + printf("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n", mem, size,iova);
+vfio_pci_dma_unmap(self->device, iova, size);Quoted 5 lines
Quoted 3 lines
I am slightly confused here. Because You are having an assert on munmap and not on any of the
vfio_pci_dma_(map/unmap). This test case is not testing VFIO.I missed to see
ioctl_assert. Please ignore this :) Sorry about that.No worries, it's not very obvious :)
vfio_pci_dma_map()andvfio_pci_dma_unmap()both return void right now and perform internal asserts since all current users of those functions want to assert success.If and when we have a use-case to assert that map or unmap fails (which I think we'll definitely have) we can add
__vfio_pci_dma_map()and__vfio_pci_dma_unmap()variants that return int instead of void.
Quoted 6 lines
Quoted 2 lines
Yep we can. Another question, why do we need assert on mmunmap ? If mmunmap fails then its not really a fault of VFIO.
You're right, it's very unlikely (almost impossible) to be VFIO's fault if
munmap()fails. But it would be a sign of a bug in the test, so it is still good to detect so we can fix it.
Understood. Thanks !
-Sairaj
David Matlack
maintainerPATCH 4/33 vfio: selftests: Test basic VFIO and IOMMUFD integration
In reply to David MatlackFrom: Josh Hilke <[email protected]>
Add a vfio test suite which verifies that userspace can bind and unbind devices, allocate I/O address space, and attach a device to an IOMMU domain using the cdev + IOMMUfd VFIO interface.
--- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_iommufd_setup_test.c | 157 ++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_setup_test.c
tools/testing/selftests/vfio/Makefile +1 -0
@@ -1,4 +1,5 @@ CFLAGS = $(KHDR_INCLUDES) +TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test include ../lib.mk include lib/libvfio.mk
tools/testing/selftests/vfio/vfio_iommufd_setup_test.c new file +157 -0
157 lines not on this page · raw message
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 5/33 vfio: selftests: Move vfio dma mapping test to their own file
In reply to David MatlackFrom: Josh Hilke <[email protected]>
Move the dma_map_unmap test from vfio_pci_device_test to a new test:vfio_dma_mapping_test. We are going to add more complex dma mapping tests, so it makes sense to separate this from the vfio pci device test which is more of a sanity check for vfio pci functionality.
--- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_dma_mapping_test.c | 49 +++++++++++++++++++ .../selftests/vfio/vfio_pci_device_test.c | 16 ------ 3 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/vfio/vfio_dma_mapping_test.c
tools/testing/selftests/vfio/Makefile +1 -0
@@ -1,4 +1,5 @@ CFLAGS = $(KHDR_INCLUDES) +TEST_GEN_PROGS += vfio_dma_mapping_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test include ../lib.mk
tools/testing/selftests/vfio/vfio_dma_mapping_test.c new file +49 -0
49 lines not on this page · raw message
tools/testing/selftests/vfio/vfio_pci_device_test.c +0 -16
@@ -36,22 +36,6 @@ FIXTURE_TEARDOWN(vfio_pci_device_test)vfio_pci_device_cleanup (self->device); } -TEST_F(vfio_pci_device_test, dma_map_unmap) -{ -const u64 size =SZ_2M ; -const u64 iova =SZ_4G ; -void *mem; - - mem =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); -ASSERT_NE (mem,MAP_FAILED ); - -vfio_pci_dma_map (self->device, iova, size, mem); -printf ("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n" , mem, size, iova); -vfio_pci_dma_unmap (self->device, iova, size); - -ASSERT_TRUE (!munmap (mem,SZ_2M )); -} -#define read_pci_id_from_sysfs (_file) ({ \ char __sysfs_path[PATH_MAX]; \ char __buf[32]; \
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 6/33 vfio: selftests: Add test to reset vfio device.
In reply to David MatlackFrom: Josh Hilke <[email protected]>
Add a test to vfio_pci_device_test which resets the device. If reset is not supported by the device, the test is skipped.
--- tools/testing/selftests/vfio/lib/include/vfio_util.h | 1 + tools/testing/selftests/vfio/lib/vfio_pci_device.c | 5 +++++ tools/testing/selftests/vfio/vfio_pci_device_test.c | 8 ++++++++ 3 files changed, 14 insertions(+)
tools/testing/selftests/vfio/lib/include/vfio_util.h +1 -0
@@ -83,6 +83,7 @@ const char *vfio_selftests_get_bdf(int *argc, char *argv[]);struct vfio_pci_device *vfio_pci_device_init (const char *bdf,int iommu_type);void vfio_pci_device_cleanup (struct vfio_pci_device *device); +void vfio_pci_device_reset (struct vfio_pci_device *device);void vfio_pci_dma_map (struct vfio_pci_device *device,u64 iova,u64 size,void *vaddr);
tools/testing/selftests/vfio/lib/vfio_pci_device.c +5 -0
@@ -202,6 +202,11 @@ void vfio_pci_config_access(struct vfio_pci_device *device, bool write, write ? "write to" : "read from", config); } +void vfio_pci_device_reset (struct vfio_pci_device *device) +{ +ioctl_assert (device->fd,VFIO_DEVICE_RESET ,NULL ); +} +static unsigned int vfio_pci_get_group_from_dev (const char *bdf) {char dev_iommu_group_path[PATH_MAX ] = {0 };
tools/testing/selftests/vfio/vfio_pci_device_test.c +8 -0
@@ -155,6 +155,14 @@ TEST_F(vfio_pci_irq_test, enable_trigger_disable)vfio_pci_irq_disable (self->device, variant->irq_index); } +TEST_F(vfio_pci_device_test, reset) +{ +if (!(self->device->info.flags &VFIO_DEVICE_FLAGS_RESET )) +SKIP (return,"Device does not support reset\n" ); + +vfio_pci_device_reset (self->device); +} +int main (int argc,char *argv[]) { device_bdf =vfio_selftests_get_bdf (&argc, argv);
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 7/33 vfio: selftests: Add DMA mapping tests for 2M and 1G HugeTLB
In reply to David MatlackFrom: Josh Hilke <[email protected]>
Add test coverage of mapping 2M and 1G HugeTLB to vfio_dma_mapping_test using a fixture variant. If there isn't enough HugeTLB memory available for the test, just skip them.
--- .../selftests/vfio/vfio_dma_mapping_test.c | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-)
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +34 -6
@@ -1,8 +1,10 @@// SPDX-License-Identifier: GPL-2.0-only -#include <fcntl.h> - +#include <stdio.h> #include <sys/mman.h> +#include <unistd.h> +#include <linux/limits.h> +#include <linux/mman.h> #include <linux/sizes.h> #include <linux/vfio.h>
@@ -16,6 +18,25 @@ FIXTURE(vfio_dma_mapping_test) {struct vfio_pci_device *device; }; +FIXTURE_VARIANT (vfio_dma_mapping_test) { +u64 size; +int mmap_flags; +}; + +FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous) { + .mmap_flags =MAP_ANONYMOUS |MAP_PRIVATE , +}; + +FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous_hugetlb_2mb) { + .size =SZ_2M , + .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB | MAP_HUGE_2MB, +}; + +FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous_hugetlb_1gb) { + .size =SZ_1G , + .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB | MAP_HUGE_1GB, +}; +FIXTURE_SETUP (vfio_dma_mapping_test) { self->device =vfio_pci_device_init (device_bdf,VFIO_TYPE1_IOMMU );
@@ -28,15 +49,22 @@ FIXTURE_TEARDOWN(vfio_dma_mapping_test) TEST_F(vfio_dma_mapping_test, dma_map_unmap) { -const u64 size =SZ_2M ; -const u64 iova =SZ_4G ; +const u64 size = variant->size ?:getpagesize (); +const int flags = variant->mmap_flags; +const u64 iova =0 ;void *mem; - mem =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_SHARED |MAP_ANONYMOUS ,-1 ,0 ); -ASSERT_NE (mem,MAP_FAILED ); + mem =mmap (NULL , size,PROT_READ |PROT_WRITE , flags,-1 ,0 ); + +/* Skip the test if there aren't enough HugeTLB pages available. */ +if (flags &MAP_HUGETLB && mem ==MAP_FAILED ) +SKIP (return,"mmap() failed: %s (%d)\n" ,strerror (errno), errno); +else +ASSERT_NE (mem,MAP_FAILED );vfio_pci_dma_map (self->device, iova, size, mem);printf ("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n" , mem, size, iova); +vfio_pci_dma_unmap (self->device, iova, size);ASSERT_TRUE (!munmap (mem, size));
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 8/33 vfio: selftests: Validate 2M/1G HugeTLB are mapped as 2M/1G in IOMMU
In reply to David MatlackFrom: Josh Hilke <[email protected]>
Update vfio dma mapping test to verify that the IOMMU uses 2M and 1G mappings when 2M and 1G HugeTLB pages are mapped into a device respectively.
This validation is done by inspecting the contents of the I/O page tables via /sys/kernel/debug/iommu/intel/. This validation is skipped if that directory is not available (i.e. non-Intel IOMMUs).
--- .../selftests/vfio/vfio_dma_mapping_test.c | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+)
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +111 -0
@@ -14,6 +14,83 @@static const char *device_bdf; +struct iommu_mapping { +u64 pgd; +u64 p4d; +u64 pud; +u64 pmd; +u64 pte; +}; + +static void parse_next_value (char **line,u64 *value) +{ +char *token; + + token =strtok_r (*line," \t|\n" , line); +if (!token) +return ; + +/* Caller verifies `value`. No need to check return value. */ +sscanf (token,"0x%lx" , value); +} + +static int intel_iommu_mapping_get (const char *bdf,u64 iova, +struct iommu_mapping *mapping) +{ +char iommu_mapping_path[PATH_MAX ], line[PATH_MAX ]; +u64 line_iova =-1 ; +int ret = -ENOENT ; +FILE *file; +char *rest; + +snprintf (iommu_mapping_path,sizeof (iommu_mapping_path), +"/sys/kernel/debug/iommu/intel/%s/domain_translation_struct" , + bdf); + +printf ("Searching for IOVA 0x%lx in %s\n" , iova, iommu_mapping_path); + + file =fopen (iommu_mapping_path,"r" ); +VFIO_ASSERT_NOT_NULL (file,"fopen(%s) failed" , iommu_mapping_path); + +while (fgets (line,sizeof (line), file)) { + rest = line; + +parse_next_value (&rest, &line_iova); +if (line_iova != (iova /getpagesize ())) +continue ; + +/* +* Ensure each struct field is initialized in case of empty +* page table values. +*/ +memset (mapping,0 ,sizeof (*mapping)); +parse_next_value (&rest, &mapping->pgd); +parse_next_value (&rest, &mapping->p4d); +parse_next_value (&rest, &mapping->pud); +parse_next_value (&rest, &mapping->pmd); +parse_next_value (&rest, &mapping->pte); + + ret =0 ; +break ; + } + +fclose (file); + +if (ret) +printf ("IOVA not found\n" ); + +return ret; +} + +static int iommu_mapping_get (const char *bdf,u64 iova, +struct iommu_mapping *mapping) +{ +if (!access ("/sys/kernel/debug/iommu/intel" ,F_OK )) +return intel_iommu_mapping_get (bdf, iova, mapping); + +return -EOPNOTSUPP ; +} +FIXTURE (vfio_dma_mapping_test) {struct vfio_pci_device *device; };
@@ -51,8 +128,10 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) {const u64 size = variant->size ?:getpagesize ();const int flags = variant->mmap_flags; +struct iommu_mapping mapping;const u64 iova =0 ;void *mem; +int rc; mem =mmap (NULL , size,PROT_READ |PROT_WRITE , flags,-1 ,0 );
@@ -65,7 +144,39 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)vfio_pci_dma_map (self->device, iova, size, mem);printf ("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n" , mem, size, iova); + rc =iommu_mapping_get (device_bdf, iova, &mapping); +if (rc == -EOPNOTSUPP ) + goto unmap; + +ASSERT_EQ (0 , rc); +printf ("Found IOMMU mappings for IOVA 0x%lx:\n" , iova); +printf ("PGD: 0x%016lx\n" , mapping.pgd); +printf ("P4D: 0x%016lx\n" , mapping.p4d); +printf ("PUD: 0x%016lx\n" , mapping.pud); +printf ("PMD: 0x%016lx\n" , mapping.pmd); +printf ("PTE: 0x%016lx\n" , mapping.pte); + +switch (size) { +case SZ_4K : +ASSERT_NE (0 , mapping.pte); +break ; +case SZ_2M : +ASSERT_EQ (0 , mapping.pte); +ASSERT_NE (0 , mapping.pmd); +break ; +case SZ_1G : +ASSERT_EQ (0 , mapping.pte); +ASSERT_EQ (0 , mapping.pmd); +ASSERT_NE (0 , mapping.pud); +break ; +default : +VFIO_FAIL ("Unrecognized size: 0x%lx\n" , size); + } + +unmap:vfio_pci_dma_unmap (self->device, iova, size); +printf ("Unmapped IOVA 0x%lx\n" , iova); +ASSERT_NE (0 ,iommu_mapping_get (device_bdf, iova, &mapping));ASSERT_TRUE (!munmap (mem, size)); }
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 9/33 tools headers: Add stub definition for __iomem
In reply to David MatlackAdd an empty definition for __iomem so that kernel headers that use __iomem can be imported into tools/include/ with less modifications.
--- tools/include/linux/compiler.h | 4 ++++ 1 file changed, 4 insertions(+)
tools/include/linux/compiler.h +4 -0
@@ -138,6 +138,10 @@# define __force#endif +#ifndef __iomem +# define __iomem +#endif +#ifndef __weak# define __weak __attribute__((weak))#endif
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 10/33 tools headers: Import asm-generic MMIO helpers
In reply to David MatlackImport the asm-generic MMIO helper functions from the kernel headers into tools/include/. The top-level include is <linux/io.h> which then includes the arch-specific <asm/io.h>, which then includes <asm-generic/io.h>. This layout is chosen to match the kernel header layout and to appease checkpatch.pl (which warns against including <asm/io.h> or <asm-generic/io.h> directly).
Changes made when importing:
- Add missing includes at the top.
- Stub out mmiowb_set_pending().
- Stub out _THIS_IP_.
- Stub out log_*_mmio() calls.
- Drop the CONFIG_64BIT checks, since tools/include/linux/types.h always defines u64.
--- tools/include/asm-generic/io.h | 482 +++++++++++++++++++++++++++++++++ tools/include/asm/io.h | 7 + tools/include/linux/io.h | 4 +- 3 files changed, 492 insertions(+), 1 deletion(-) create mode 100644 tools/include/asm-generic/io.h create mode 100644 tools/include/asm/io.h
tools/include/asm-generic/io.h new file +482 -0
@@ -0,0 +1,482 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_ASM_GENERIC_IO_H +#define _TOOLS_ASM_GENERIC_IO_H + +#include <asm/barrier.h> +#include <asm/byteorder.h> + +#include <linux/compiler.h> +#include <linux/kernel.h> +#include <linux/types.h> + +#ifndef mmiowb_set_pending +#define mmiowb_set_pending () do { } while (0) +#endif + +#ifndef __io_br +#define __io_br () barrier() +#endif + +/* prevent prefetching of coherent DMA data ahead of a dma-complete */ +#ifndef __io_ar +#ifdef rmb +#define __io_ar (v) rmb() +#else +#define __io_ar (v) barrier() +#endif +#endif + +/* flush writes to coherent DMA data before possibly triggering a DMA read */ +#ifndef __io_bw +#ifdef wmb +#define __io_bw () wmb() +#else +#define __io_bw () barrier() +#endif +#endif + +/* serialize device access against a spin_unlock, usually handled there. */ +#ifndef __io_aw +#define __io_aw () mmiowb_set_pending() +#endif + +#ifndef __io_pbw +#define __io_pbw () __io_bw() +#endif + +#ifndef __io_paw +#define __io_paw () __io_aw() +#endif + +#ifndef __io_pbr +#define __io_pbr () __io_br() +#endif + +#ifndef __io_par +#define __io_par (v) __io_ar(v) +#endif + +#ifndef _THIS_IP_ +#define _THIS_IP_ 0 +#endif + +static inline void log_write_mmio (u64 val,u8 width,volatile void __iomem *addr, +unsigned long caller_addr,unsigned long caller_addr0) {} +static inline void log_post_write_mmio (u64 val,u8 width,volatile void __iomem *addr, +unsigned long caller_addr,unsigned long caller_addr0) {} +static inline void log_read_mmio (u8 width,const volatile void __iomem *addr, +unsigned long caller_addr,unsigned long caller_addr0) {} +static inline void log_post_read_mmio (u64 val,u8 width,const volatile void __iomem *addr, +unsigned long caller_addr,unsigned long caller_addr0) {} + +/* +* __raw_{read,write}{b,w,l,q}() access memory in native endianness. +* +* On some architectures memory mapped IO needs to be accessed differently. +* On the simple architectures, we just read/write the memory location +* directly. +*/ + +#ifndef __raw_readb +#define __raw_readb __raw_readb +static inline u8 __raw_readb (const volatile void __iomem *addr) +{ +return *(const volatile u8 __force *)addr; +} +#endif + +#ifndef __raw_readw +#define __raw_readw __raw_readw +static inline u16 __raw_readw (const volatile void __iomem *addr) +{ +return *(const volatile u16 __force *)addr; +} +#endif + +#ifndef __raw_readl +#define __raw_readl __raw_readl +static inline u32 __raw_readl (const volatile void __iomem *addr) +{ +return *(const volatile u32 __force *)addr; +} +#endif + +#ifndef __raw_readq +#define __raw_readq __raw_readq +static inline u64 __raw_readq (const volatile void __iomem *addr) +{ +return *(const volatile u64 __force *)addr; +} +#endif + +#ifndef __raw_writeb +#define __raw_writeb __raw_writeb +static inline void __raw_writeb (u8 value,volatile void __iomem *addr) +{ + *(volatile u8 __force *)addr = value; +} +#endif + +#ifndef __raw_writew +#define __raw_writew __raw_writew +static inline void __raw_writew (u16 value,volatile void __iomem *addr) +{ + *(volatile u16 __force *)addr = value; +} +#endif + +#ifndef __raw_writel +#define __raw_writel __raw_writel +static inline void __raw_writel (u32 value,volatile void __iomem *addr) +{ + *(volatile u32 __force *)addr = value; +} +#endif + +#ifndef __raw_writeq +#define __raw_writeq __raw_writeq +static inline void __raw_writeq (u64 value,volatile void __iomem *addr) +{ + *(volatile u64 __force *)addr = value; +} +#endif + +/* +* {read,write}{b,w,l,q}() access little endian memory and return result in +* native endianness. +*/ + +#ifndef readb +#define readb readb +static inline u8 readb (const volatile void __iomem *addr) +{ +u8 val; + +log_read_mmio (8 , addr, _THIS_IP_, _RET_IP_); +__io_br (); + val =__raw_readb (addr); +__io_ar (val); +log_post_read_mmio (val,8 , addr, _THIS_IP_, _RET_IP_); +return val; +} +#endif + +#ifndef readw +#define readw readw +static inline u16 readw (const volatile void __iomem *addr) +{ +u16 val; + +log_read_mmio (16 , addr, _THIS_IP_, _RET_IP_); +__io_br (); + val =__le16_to_cpu ((__le16 __force )__raw_readw (addr)); +__io_ar (val); +log_post_read_mmio (val,16 , addr, _THIS_IP_, _RET_IP_); +return val; +} +#endif + +#ifndef readl +#define readl readl +static inline u32 readl (const volatile void __iomem *addr) +{ +u32 val; + +log_read_mmio (32 , addr, _THIS_IP_, _RET_IP_); +__io_br (); + val =__le32_to_cpu ((__le32 __force )__raw_readl (addr)); +__io_ar (val); +log_post_read_mmio (val,32 , addr, _THIS_IP_, _RET_IP_); +return val; +} +#endif + +#ifndef readq +#define readq readq +static inline u64 readq (const volatile void __iomem *addr) +{ +u64 val; + +log_read_mmio (64 , addr, _THIS_IP_, _RET_IP_); +__io_br (); + val =__le64_to_cpu ((__le64 __force )__raw_readq (addr)); +__io_ar (val); +log_post_read_mmio (val,64 , addr, _THIS_IP_, _RET_IP_); +return val; +} +#endif + +#ifndef writeb +#define writeb writeb +static inline void writeb (u8 value,volatile void __iomem *addr) +{ +log_write_mmio (value,8 , addr, _THIS_IP_, _RET_IP_); +__io_bw (); +__raw_writeb (value, addr); +__io_aw (); +log_post_write_mmio (value,8 , addr, _THIS_IP_, _RET_IP_); +} +#endif + +#ifndef writew +#define writew writew +static inline void writew (u16 value,volatile void __iomem *addr) +{ +log_write_mmio (value,16 , addr, _THIS_IP_, _RET_IP_); +__io_bw (); +__raw_writew ((u16 __force )cpu_to_le16 (value), addr); +__io_aw (); +log_post_write_mmio (value,16 , addr, _THIS_IP_, _RET_IP_); +} +#endif + +#ifndef writel +#define writel writel +static inline void writel (u32 value,volatile void __iomem *addr) +{ +log_write_mmio (value,32 , addr, _THIS_IP_, _RET_IP_); +__io_bw (); +__raw_writel ((u32 __force )__cpu_to_le32 (value), addr); +__io_aw (); +log_post_write_mmio (value,32 , addr, _THIS_IP_, _RET_IP_); +} +#endif + +#ifndef writeq +#define writeq writeq +static inline void writeq (u64 value,volatile void __iomem *addr) +{ +log_write_mmio (value,64 , addr, _THIS_IP_, _RET_IP_); +__io_bw (); +__raw_writeq ((u64 __force )__cpu_to_le64 (value), addr); +__io_aw (); +log_post_write_mmio (value,64 , addr, _THIS_IP_, _RET_IP_); +} +#endif + +/* +* {read,write}{b,w,l,q}_relaxed() are like the regular version, but +* are not guaranteed to provide ordering against spinlocks or memory +* accesses. +*/ +#ifndef readb_relaxed +#define readb_relaxed readb_relaxed +static inline u8 readb_relaxed (const volatile void __iomem *addr) +{ +u8 val; + +log_read_mmio (8 , addr, _THIS_IP_, _RET_IP_); + val =__raw_readb (addr); +log_post_read_mmio (val,8 , addr, _THIS_IP_, _RET_IP_); +return val; +} +#endif + +#ifndef readw_relaxed +#define readw_relaxed readw_relaxed +static inline u16 readw_relaxed (const volatile void __iomem *addr) +{ +u16 val; + +log_read_mmio (16 , addr, _THIS_IP_, _RET_IP_); + val =__le16_to_cpu ((__le16 __force )__raw_readw (addr)); +log_post_read_mmio (val,16 , addr, _THIS_IP_, _RET_IP_); +return val; +} +#endif + +#ifndef readl_relaxed +#define readl_relaxed readl_relaxed +static inline u32 readl_relaxed (const volatile void __iomem *addr) +{ +u32 val; + +log_read_mmio (32 , addr, _THIS_IP_, _RET_IP_); + val =__le32_to_cpu ((__le32 __force )__raw_readl (addr)); +log_post_read_mmio (val,32 , addr, _THIS_IP_, _RET_IP_); +return val; +} +#endif + +#if defined(readq) && !defined(readq_relaxed) +#define readq_relaxed readq_relaxed +static inline u64 readq_relaxed (const volatile void __iomem *addr) +{ +u64 val; + +log_read_mmio (64 , addr, _THIS_IP_, _RET_IP_); + val =__le64_to_cpu ((__le64 __force )__raw_readq (addr)); +log_post_read_mmio (val,64 , addr, _THIS_IP_, _RET_IP_); +return val; +} +#endif + +#ifndef writeb_relaxed +#define writeb_relaxed writeb_relaxed +static inline void writeb_relaxed (u8 value,volatile void __iomem *addr) +{ +log_write_mmio (value,8 , addr, _THIS_IP_, _RET_IP_); +__raw_writeb (value, addr); +log_post_write_mmio (value,8 , addr, _THIS_IP_, _RET_IP_); +} +#endif + +#ifndef writew_relaxed +#define writew_relaxed writew_relaxed +static inline void writew_relaxed (u16 value,volatile void __iomem *addr) +{ +log_write_mmio (value,16 , addr, _THIS_IP_, _RET_IP_); +__raw_writew ((u16 __force )cpu_to_le16 (value), addr); +log_post_write_mmio (value,16 , addr, _THIS_IP_, _RET_IP_); +} +#endif + +#ifndef writel_relaxed +#define writel_relaxed writel_relaxed +static inline void writel_relaxed (u32 value,volatile void __iomem *addr) +{ +log_write_mmio (value,32 , addr, _THIS_IP_, _RET_IP_); +__raw_writel ((u32 __force )__cpu_to_le32 (value), addr); +log_post_write_mmio (value,32 , addr, _THIS_IP_, _RET_IP_); +} +#endif + +#if defined(writeq) && !defined(writeq_relaxed) +#define writeq_relaxed writeq_relaxed +static inline void writeq_relaxed (u64 value,volatile void __iomem *addr) +{ +log_write_mmio (value,64 , addr, _THIS_IP_, _RET_IP_); +__raw_writeq ((u64 __force )__cpu_to_le64 (value), addr); +log_post_write_mmio (value,64 , addr, _THIS_IP_, _RET_IP_); +} +#endif + +/* +* {read,write}s{b,w,l,q}() repeatedly access the same memory address in +* native endianness in 8-, 16-, 32- or 64-bit chunks (@count times). +*/ +#ifndef readsb +#define readsb readsb +static inline void readsb (const volatile void __iomem *addr,void *buffer, +unsigned int count) +{ +if (count) { +u8 *buf = buffer; + +do { +u8 x =__raw_readb (addr); + *buf++ = x; + }while (--count); + } +} +#endif + +#ifndef readsw +#define readsw readsw +static inline void readsw (const volatile void __iomem *addr,void *buffer, +unsigned int count) +{ +if (count) { +u16 *buf = buffer; + +do { +u16 x =__raw_readw (addr); + *buf++ = x; + }while (--count); + } +} +#endif + +#ifndef readsl +#define readsl readsl +static inline void readsl (const volatile void __iomem *addr,void *buffer, +unsigned int count) +{ +if (count) { +u32 *buf = buffer; + +do { +u32 x =__raw_readl (addr); + *buf++ = x; + }while (--count); + } +} +#endif + +#ifndef readsq +#define readsq readsq +static inline void readsq (const volatile void __iomem *addr,void *buffer, +unsigned int count) +{ +if (count) { +u64 *buf = buffer; + +do { +u64 x =__raw_readq (addr); + *buf++ = x; + }while (--count); + } +} +#endif + +#ifndef writesb +#define writesb writesb +static inline void writesb (volatile void __iomem *addr,const void *buffer, +unsigned int count) +{ +if (count) { +const u8 *buf = buffer; + +do { +__raw_writeb (*buf++, addr); + }while (--count); + } +} +#endif + +#ifndef writesw +#define writesw writesw +static inline void writesw (volatile void __iomem *addr,const void *buffer, +unsigned int count) +{ +if (count) { +const u16 *buf = buffer; + +do { +__raw_writew (*buf++, addr); + }while (--count); + } +} +#endif + +#ifndef writesl +#define writesl writesl +static inline void writesl (volatile void __iomem *addr,const void *buffer, +unsigned int count) +{ +if (count) { +const u32 *buf = buffer; + +do { +__raw_writel (*buf++, addr); + }while (--count); + } +} +#endif + +#ifndef writesq +#define writesq writesq +static inline void writesq (volatile void __iomem *addr,const void *buffer, +unsigned int count) +{ +if (count) { +const u64 *buf = buffer; + +do { +__raw_writeq (*buf++, addr); + }while (--count); + } +} +#endif + +#endif /* _TOOLS_ASM_GENERIC_IO_H */
tools/include/asm/io.h new file +7 -0
@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_ASM_IO_H +#define _TOOLS_ASM_IO_H + +#include <asm-generic/io.h> + +#endif /* _TOOLS_ASM_IO_H */
tools/include/linux/io.h +3 -1
@@ -2,4 +2,6 @@#ifndef _TOOLS_IO_H#define _TOOLS_IO_H -#endif +#include <asm/io.h> + +#endif /* _TOOLS_IO_H */
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 11/33 tools headers: Import x86 MMIO helper overrides
In reply to David MatlackImport the x86-specific overrides for <asm-generic/io.h> from the kernel headers into tools/include/.
Changes made when importing:
- Replace CONFIG_X86_64 with __x86_64__.
--- tools/arch/x86/include/asm/io.h | 75 +++++++++++++++++++++++++++++++++ tools/include/asm/io.h | 4 ++ 2 files changed, 79 insertions(+) create mode 100644 tools/arch/x86/include/asm/io.h
tools/arch/x86/include/asm/io.h new file +75 -0
@@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_ASM_X86_IO_H +#define _TOOLS_ASM_X86_IO_H + +#include <linux/compiler.h> +#include <linux/types.h> + +#define build_mmio_read(name, size, type, reg, barrier) \ +static inline type name(const volatile void __iomem *addr) \ +{ type ret; asm volatile("mov" size " %1,%0":reg (ret) \ +:"m" (*(volatile type __force *)addr) barrier); return ret; } + +#define build_mmio_write(name, size, type, reg, barrier) \ +static inline void name(type val, volatile void __iomem *addr) \ +{ asm volatile("mov" size " %0,%1": :reg (val), \ +"m" (*(volatile type __force *)addr) barrier); } + +build_mmio_read(readb, "b", unsigned char, "=q", :"memory") +build_mmio_read(readw, "w", unsigned short, "=r", :"memory") +build_mmio_read(readl, "l", unsigned int, "=r", :"memory") + +build_mmio_read(__readb, "b", unsigned char, "=q", ) +build_mmio_read(__readw, "w", unsigned short, "=r", ) +build_mmio_read(__readl, "l", unsigned int, "=r", ) + +build_mmio_write(writeb, "b", unsigned char, "q", :"memory") +build_mmio_write(writew, "w", unsigned short, "r", :"memory") +build_mmio_write(writel, "l", unsigned int, "r", :"memory") + +build_mmio_write(__writeb, "b", unsigned char, "q", ) +build_mmio_write(__writew, "w", unsigned short, "r", ) +build_mmio_write(__writel, "l", unsigned int, "r", ) + +#define readb readb +#define readw readw +#define readl readl +#define readb_relaxed(a) __readb(a) +#define readw_relaxed(a) __readw(a) +#define readl_relaxed(a) __readl(a) +#define __raw_readb __readb +#define __raw_readw __readw +#define __raw_readl __readl + +#define writeb writeb +#define writew writew +#define writel writel +#define writeb_relaxed(v, a) __writeb(v, a) +#define writew_relaxed(v, a) __writew(v, a) +#define writel_relaxed(v, a) __writel(v, a) +#define __raw_writeb __writeb +#define __raw_writew __writew +#define __raw_writel __writel + +#ifdef __x86_64__ + +build_mmio_read(readq, "q", u64, "=r", :"memory") +build_mmio_read(__readq, "q", u64, "=r", ) +build_mmio_write(writeq, "q", u64, "r", :"memory") +build_mmio_write(__writeq, "q", u64, "r", ) + +#define readq_relaxed(a) __readq(a) +#define writeq_relaxed(v, a) __writeq(v, a) + +#define __raw_readq __readq +#define __raw_writeq __writeq + +/* Let people know that we have them */ +#define readq readq +#define writeq writeq + +#endif /* __x86_64__ */ + +#include <asm-generic/io.h> + +#endif /* _TOOLS_ASM_X86_IO_H */
tools/include/asm/io.h +4 -0
@@ -2,6 +2,10 @@#ifndef _TOOLS_ASM_IO_H#define _TOOLS_ASM_IO_H +#if defined(__i386__) || defined(__x86_64__) +#include "../../arch/x86/include/asm/io.h" +#else #include <asm-generic/io.h> +#endif #endif /* _TOOLS_ASM_IO_H */
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 12/33 tools headers: Import iosubmit_cmds512()
In reply to David MatlackImport iosubmit_cmds512() from arch/x86/include/asm/io.h into tools/ so it can be used by VFIO selftests to interact with Intel DSA devices.
Also pull in movdir64b() from arch/x86/include/asm/special_insns.h into tools/, which is the underlying instruction used by iosubmit_cmds512().
Changes made when importing: None
--- tools/arch/x86/include/asm/io.h | 26 +++++++++++++++++++++ tools/arch/x86/include/asm/special_insns.h | 27 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tools/arch/x86/include/asm/special_insns.h
tools/arch/x86/include/asm/io.h +26 -0
@@ -4,6 +4,7 @@#include <linux/compiler.h> #include <linux/types.h> +#include "special_insns.h" #define build_mmio_read (name, size, type, reg, barrier) \ static inline type name(const volatile void __iomem *addr) \
@@ -72,4 +73,29 @@ build_mmio_write(__writeq, "q", u64, "r", )#include <asm-generic/io.h> +/** +* iosubmit_cmds512 - copy data to single MMIO location, in 512-bit units +* @dst: destination, in MMIO space (must be 512-bit aligned) +* @src: source +* @count: number of 512 bits quantities to submit +* +* Submit data from kernel space to MMIO space, in units of 512 bits at a +* time. Order of access is not guaranteed, nor is a memory barrier +* performed afterwards. +* +* Warning: Do not use this helper unless your driver has checked that the CPU +* instruction is supported on the platform. +*/ +static inline void iosubmit_cmds512 (void __iomem *dst,const void *src, +size_t count) +{ +const u8 *from = src; +const u8 *end = from + count *64 ; + +while (from < end) { +movdir64b (dst, from); + from +=64 ; + } +} +#endif /* _TOOLS_ASM_X86_IO_H */
tools/arch/x86/include/asm/special_insns.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_ASM_X86_SPECIAL_INSNS_H +#define _TOOLS_ASM_X86_SPECIAL_INSNS_H + +/* The dst parameter must be 64-bytes aligned */ +static inline void movdir64b (void *dst,const void *src) +{ +const struct {char _[64 ]; } *__src = src; +struct {char _[64 ]; } *__dst = dst; + +/* +* MOVDIR64B %(rdx), rax. +* +* Both __src and __dst must be memory constraints in order to tell the +* compiler that no other memory accesses should be reordered around +* this one. +* +* Also, both must be supplied as lvalues because this tells +* the compiler what the object is (its size) the instruction accesses. +* I.e., not the pointers but what they point to, thus the deref'ing '*'. +*/ + asmvolatile (".byte 0x66, 0x0f, 0x38, 0xf8, 0x02" + :"+m" (*__dst) + :"m" (*__src),"a" (__dst),"d" (__src)); +} + +#endif /* _TOOLS_ASM_X86_SPECIAL_INSNS_H */
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
Re: [PATCH 12/33] tools headers: Import iosubmit_cmds512()
Quoted 3 lines · David Matlack
Import
iosubmit_cmds512()fromarch/x86/include/asm/io.hintotools/so it can be used by VFIO selftests to interact with Intel DSA devices.
minor: perhaps move this patch to be near the one that adds the DSA driver? (in case there's a next revision)
Anyway,
Quoted 90 lines
Also pull in
movdir64b()fromarch/x86/include/asm/special_insns.hintotools/, which is the underlying instruction used byiosubmit_cmds512().Changes made when importing: None
Signed-off-by David Matlack <[email protected]>--- tools/arch/x86/include/asm/io.h | 26 +++++++++++++++++++++ tools/arch/x86/include/asm/special_insns.h | 27 ++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tools/arch/x86/include/asm/special_insns.htools/arch/x86/include/asm/io.h +26
-0@@ -4,6 +4,7 @@#include <linux/compiler.h> #include <linux/types.h> +#include "special_insns.h" #define build_mmio_read (name, size, type, reg, barrier) \ static inline type name(const volatile void __iomem *addr) \@@ -72,4 +73,29 @@ build_mmio_write(__writeq, "q", u64, "r", )#include <asm-generic/io.h> +/** +* iosubmit_cmds512 - copy data to single MMIO location, in 512-bit units +* @dst: destination, in MMIO space (must be 512-bit aligned) +* @src: source +* @count: number of 512 bits quantities to submit +* +* Submit data from kernel space to MMIO space, in units of 512 bits at a +* time. Order of access is not guaranteed, nor is a memory barrier +* performed afterwards. +* +* Warning: Do not use this helper unless your driver has checked that the CPU +* instruction is supported on the platform. +*/ +static inline void iosubmit_cmds512 (void __iomem *dst,const void *src, +size_t count) +{ +const u8 *from = src; +const u8 *end = from + count *64 ; + +while (from < end) { +movdir64b (dst, from); + from +=64 ; + } +} +#endif /* _TOOLS_ASM_X86_IO_H */ tools/arch/x86/include/asm/special_insns.h new file +27
-0@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_ASM_X86_SPECIAL_INSNS_H +#define _TOOLS_ASM_X86_SPECIAL_INSNS_H + +/* The dst parameter must be 64-bytes aligned */ +static inline void movdir64b (void *dst,const void *src) +{ +const struct {char _[64 ]; } *__src = src; +struct {char _[64 ]; } *__dst = dst; + +/* +* MOVDIR64B %(rdx), rax. +* +* Both __src and __dst must be memory constraints in order to tell the +* compiler that no other memory accesses should be reordered around +* this one. +* +* Also, both must be supplied as lvalues because this tells +* the compiler what the object is (its size) the instruction accesses. +* I.e., not the pointers but what they point to, thus the deref'ing '*'. +*/ + asmvolatile (".byte 0x66, 0x0f, 0x38, 0xf8, 0x02" + :"+m" (*__dst) + :"m" (*__src),"a" (__dst),"d" (__src)); +} + +#endif /* _TOOLS_ASM_X86_SPECIAL_INSNS_H */ Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
Signature · 1 line
Vinicius
David Matlack
maintainerRe: [PATCH 12/33] tools headers: Import iosubmit_cmds512()
Quoted 9 lines · Vinicius Costa Gomes
Quoted 3 lines · David Matlack
Import
iosubmit_cmds512()fromarch/x86/include/asm/io.hintotools/so it can be used by VFIO selftests to interact with Intel DSA devices.minor: perhaps move this patch to be near the one that adds the DSA driver? (in case there's a next revision)
Will do. Thanks for the review!
David Matlack
maintainerPATCH 13/33 tools headers: Add symlink to linux/pci_ids.h
In reply to David MatlackAdd a symlink to include/linux/pci_ids.h to tools/include/. This will be used by VFIO selftests in subsequent commits to match device and vendor IDs.
--- tools/include/linux/pci_ids.h | 1 + 1 file changed, 1 insertion(+) create mode 120000 tools/include/linux/pci_ids.h
tools/include/linux/pci_ids.h new file +1 -0
@@ -0,0 +1 @@ +../../../include/linux/pci_ids.h
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 14/33 vfio: selftests: Keep track of DMA regions mapped into the device
In reply to David MatlackKeep track of the list of DMA regions that are mapped into the device using a linked list and a new struct vfio_dma_region and use that to add {__,}to_iova() for converting host virtual addresses into IOVAs.
This will be used in a subsequent commit to map multiple DMA regions into a device that are then used by drivers.
--- .../selftests/vfio/lib/include/vfio_util.h | 23 +++++++-- .../selftests/vfio/lib/vfio_pci_device.c | 49 ++++++++++++++++--- .../selftests/vfio/vfio_dma_mapping_test.c | 21 +++++--- 3 files changed, 75 insertions(+), 18 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +20 -3
@@ -51,6 +51,17 @@ struct vfio_pci_bar {void *vaddr; }; +typedef u64 iova_t ; + +#define INVALID_IOVA UINT64_MAX + +struct vfio_dma_region { +struct list_head link; +void *vaddr; +iova_t iova; +u64 size; +}; +struct vfio_pci_device {int fd;int group_fd;
@@ -63,6 +74,8 @@ struct vfio_pci_device {struct vfio_irq_info msi_info;struct vfio_irq_info msix_info; +struct list_head dma_regions; +/* eventfds for MSI and MSI-x interrupts */ int msi_eventfds[PCI_MSIX_FLAGS_QSIZE +1 ]; };
@@ -85,9 +98,10 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type);void vfio_pci_device_cleanup (struct vfio_pci_device *device);void vfio_pci_device_reset (struct vfio_pci_device *device); -void vfio_pci_dma_map (struct vfio_pci_device *device,u64 iova,u64 size, -void *vaddr); -void vfio_pci_dma_unmap (struct vfio_pci_device *device,u64 iova,u64 size); +void vfio_pci_dma_map (struct vfio_pci_device *device, +struct vfio_dma_region *region); +void vfio_pci_dma_unmap (struct vfio_pci_device *device, +struct vfio_dma_region *region);void vfio_pci_config_access (struct vfio_pci_device *device,bool write,size_t config,size_t size,void *data);
@@ -138,4 +152,7 @@ static inline void vfio_pci_msix_disable(struct vfio_pci_device *device) vfio_pci_irq_disable(device, VFIO_PCI_MSIX_IRQ_INDEX); } +iova_t __to_iova (struct vfio_pci_device *device,void *vaddr); +iova_t to_iova (struct vfio_pci_device *device,void *vaddr); +#endif /* SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H */
tools/testing/selftests/vfio/lib/vfio_pci_device.c +42 -7
82 lines not on this page · raw message
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +13 -8
@@ -128,21 +128,25 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) {const u64 size = variant->size ?:getpagesize ();const int flags = variant->mmap_flags; +struct vfio_dma_region region;struct iommu_mapping mapping;const u64 iova =0 ; -void *mem;int rc; - mem =mmap (NULL , size,PROT_READ |PROT_WRITE , flags,-1 ,0 ); + region.iova = iova; + region.size = size; + region.vaddr =mmap (NULL , size,PROT_READ |PROT_WRITE , flags,-1 ,0 );/* Skip the test if there aren't enough HugeTLB pages available. */ -if (flags &MAP_HUGETLB && mem ==MAP_FAILED ) +if (flags &MAP_HUGETLB && region.vaddr ==MAP_FAILED )SKIP (return,"mmap() failed: %s (%d)\n" ,strerror (errno), errno);else -ASSERT_NE (mem,MAP_FAILED ); +ASSERT_NE (region.vaddr,MAP_FAILED ); -vfio_pci_dma_map (self->device, iova, size, mem); -printf ("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n" , mem, size, iova); +vfio_pci_dma_map (self->device, ®ion); +printf ("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n" , region.vaddr, size, iova); + +ASSERT_EQ (iova,to_iova (self->device, region.vaddr)); rc =iommu_mapping_get (device_bdf, iova, &mapping);if (rc == -EOPNOTSUPP )
@@ -174,11 +178,12 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) } unmap: -vfio_pci_dma_unmap (self->device, iova, size); +vfio_pci_dma_unmap (self->device, ®ion);printf ("Unmapped IOVA 0x%lx\n" , iova); +ASSERT_EQ (INVALID_IOVA ,__to_iova (self->device, region.vaddr));ASSERT_NE (0 ,iommu_mapping_get (device_bdf, iova, &mapping)); -ASSERT_TRUE (!munmap (mem, size)); +ASSERT_TRUE (!munmap (region.vaddr, size)); }int main (int argc,char *argv[])
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 15/33 vfio: selftests: Enable asserting MSI eventfds not firing
In reply to David MatlackMake it possible to assert that a given MSI eventfd did _not_ fire by adding a helper to mark an eventfd non-blocking. Demonstrate this in vfio_pci_device_test by asserting the MSI eventfd did not fire before vfio_pci_irq_trigger().
--- tools/testing/selftests/vfio/lib/include/vfio_util.h | 12 ++++++++++++ tools/testing/selftests/vfio/vfio_pci_device_test.c | 10 +++++++++- 2 files changed, 21 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +12 -0
@@ -2,6 +2,7 @@#ifndef SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H #define SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H +#include <fcntl.h> #include <string.h> #include <linux/vfio.h> #include <linux/list.h>
@@ -130,6 +131,17 @@ void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index,void vfio_pci_irq_disable (struct vfio_pci_device *device,u32 index);void vfio_pci_irq_trigger (struct vfio_pci_device *device,u32 index,u32 vector); +static inline void fcntl_set_nonblock (int fd) +{ +int r; + + r =fcntl (fd,F_GETFL ,0 ); +VFIO_ASSERT_NE (r,-1 ,"F_GETFL failed for fd %d\n" , fd); + + r =fcntl (fd,F_SETFL , r |O_NONBLOCK ); +VFIO_ASSERT_NE (r,-1 ,"F_SETFL O_NONBLOCK failed for fd %d\n" , fd); +} +static inline void vfio_pci_msi_enable (struct vfio_pci_device *device,u32 vector,int count) {
tools/testing/selftests/vfio/vfio_pci_device_test.c +9 -1
@@ -129,6 +129,7 @@ FIXTURE_TEARDOWN(vfio_pci_irq_test) TEST_F(vfio_pci_irq_test, enable_trigger_disable) {bool msix = variant->irq_index ==VFIO_PCI_MSIX_IRQ_INDEX ; +int msi_eventfd;u32 count;u64 value;int i;
@@ -147,8 +148,15 @@ TEST_F(vfio_pci_irq_test, enable_trigger_disable)printf ("MSI%s: enabled %d interrupts\n" , msix ?"-x" :"" , count);for (i =0 ; i < count; i++) { + msi_eventfd = self->device->msi_eventfds[i]; + +fcntl_set_nonblock (msi_eventfd); +ASSERT_EQ (-1 ,read (msi_eventfd, &value,8 )); +ASSERT_EQ (EAGAIN , errno); +vfio_pci_irq_trigger (self->device, variant->irq_index, i); -ASSERT_EQ (8 ,read (self->device->msi_eventfds[i], &value,8 )); + +ASSERT_EQ (8 ,read (msi_eventfd, &value,8 ));ASSERT_EQ (1 , value); }
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 16/33 vfio: selftests: Add a helper for matching vendor+device IDs
In reply to David MatlackAdd a helper function for matching a device against a given vendor and device ID. This will be used in a subsequent commit to match devices against drivers.
--- tools/testing/selftests/vfio/lib/include/vfio_util.h | 7 +++++++ tools/testing/selftests/vfio/vfio_pci_device_test.c | 4 +--- 2 files changed, 8 insertions(+), 3 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +7 -0
@@ -167,4 +167,11 @@ static inline void vfio_pci_msix_disable(struct vfio_pci_device *device)iova_t __to_iova (struct vfio_pci_device *device,void *vaddr);iova_t to_iova (struct vfio_pci_device *device,void *vaddr); +static inline bool vfio_pci_device_match (struct vfio_pci_device *device, +u16 vendor_id,u16 device_id) +{ +return (vendor_id ==vfio_pci_config_readw (device,PCI_VENDOR_ID )) && + (device_id ==vfio_pci_config_readw (device,PCI_DEVICE_ID )); +} +#endif /* SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H */
tools/testing/selftests/vfio/vfio_pci_device_test.c +1 -3
@@ -56,9 +56,7 @@ TEST_F(vfio_pci_device_test, config_space_read_write)/* Check that Vendor and Device match what the kernel reports. */ vendor =read_pci_id_from_sysfs ("vendor" ); device =read_pci_id_from_sysfs ("device" ); - -ASSERT_EQ (vendor,vfio_pci_config_readw (self->device,PCI_VENDOR_ID )); -ASSERT_EQ (device,vfio_pci_config_readw (self->device,PCI_DEVICE_ID )); +ASSERT_TRUE (vfio_pci_device_match (self->device, vendor, device));printf ("Vendor: %04x, Device: %04x\n" , vendor, device);
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 17/33 vfio: selftests: Add driver framework
In reply to David MatlackAdd a driver framework to VFIO selftests, so that devices can generate DMA and interrupts in a common way that can be then utilized by tests. This will enable VFIO selftests to exercise real hardware DMA and interrupt paths, without needing any device-specific code in the test itself.
Subsequent commits will introduce drivers for specific devices.
--- .../selftests/vfio/lib/include/vfio_util.h | 92 ++++++++++++++ tools/testing/selftests/vfio/lib/libvfio.mk | 1 + .../selftests/vfio/lib/vfio_pci_device.c | 5 + .../selftests/vfio/lib/vfio_pci_driver.c | 116 ++++++++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 tools/testing/selftests/vfio/lib/vfio_pci_driver.c
tools/testing/selftests/vfio/lib/include/vfio_util.h +92 -0
@@ -63,6 +63,85 @@ struct vfio_dma_region {u64 size; }; +struct vfio_pci_device ; + +struct vfio_pci_driver_ops { +const char *name; + +/** +* @probe() - Check if the driver supports the given device. +* +* Return: 0 on success, non-0 on failure. +*/ +int (*probe)(struct vfio_pci_device *device); + +/** +* @init() - Initialize the driver for @device. +* +* Must be called after device->driver.region has been initialized. +*/ +void (*init)(struct vfio_pci_device *device); + +/** +* remove() - Deinitialize the driver for @device. +*/ +void (*remove)(struct vfio_pci_device *device); + +/** +* memcpy_start() - Kick off @count repeated memcpy operations from +* [@src, @src + @size) to [@dst, @dst + @size). +* +* Guarantees: +* - The device will attempt DMA reads on [src, src + size). +* - The device will attempt DMA writes on [dst, dst + size). +* - The device will not generate any interrupts. +* +* memcpy_start() returns immediately, it does not wait for the +* copies to complete. +*/ +void (*memcpy_start)(struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size,u64 count); + +/** +* memcpy_wait() - Wait until the memcpy operations started by +* memcpy_start() have finished. +* +* Guarantees: +* - All in-flight DMAs initiated by memcpy_start() are fully complete +* before memcpy_wait() returns. +* +* Returns non-0 if the driver detects that an error occurred during the +* memcpy, 0 otherwise. +*/ +int (*memcpy_wait)(struct vfio_pci_device *device); + +/** +* send_msi() - Make the device send the MSI device->driver.msi. +* +* Guarantees: +* - The device will send the MSI once. +*/ +void (*send_msi)(struct vfio_pci_device *device); +}; + +struct vfio_pci_driver { +const struct vfio_pci_driver_ops *ops; +bool initialized; +bool memcpy_in_progress; + +/* Region to be used by the driver (e.g. for in-memory descriptors) */ +struct vfio_dma_region region; + +/* The maximum size that can be passed to memcpy_start(). */ +u64 max_memcpy_size; + +/* The maximum count that can be passed to memcpy_start(). */ +u64 max_memcpy_count; + +/* The MSI vector the device will signal in ops->send_msi(). */ +int msi; +}; +struct vfio_pci_device {int fd;int group_fd;
@@ -79,6 +158,8 @@ struct vfio_pci_device {/* eventfds for MSI and MSI-x interrupts */ int msi_eventfds[PCI_MSIX_FLAGS_QSIZE +1 ]; + +struct vfio_pci_driver driver; };/*
@@ -174,4 +255,15 @@ static inline bool vfio_pci_device_match(struct vfio_pci_device *device, (device_id == vfio_pci_config_readw(device, PCI_DEVICE_ID)); } +void vfio_pci_driver_probe (struct vfio_pci_device *device); +void vfio_pci_driver_init (struct vfio_pci_device *device); +void vfio_pci_driver_remove (struct vfio_pci_device *device); +int vfio_pci_driver_memcpy (struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size); +void vfio_pci_driver_memcpy_start (struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size, +u64 count); +int vfio_pci_driver_memcpy_wait (struct vfio_pci_device *device); +void vfio_pci_driver_send_msi (struct vfio_pci_device *device); +#endif /* SELFTESTS_VFIO_LIB_INCLUDE_VFIO_UTIL_H */
tools/testing/selftests/vfio/lib/libvfio.mk +1 -0
@@ -1,6 +1,7 @@ VFIO_DIR := $(selfdir)/vfio LIBVFIO_C := lib/vfio_pci_device.c +LIBVFIO_C += lib/vfio_pci_driver.c LIBVFIO_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBVFIO_C))
tools/testing/selftests/vfio/lib/vfio_pci_device.c +5 -0
@@ -344,6 +344,8 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type)vfio_pci_iommu_setup (device, iommu_type);vfio_pci_device_setup (device, bdf); +vfio_pci_driver_probe (device); +return device; }
@@ -351,6 +353,9 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *device) {int i; +if (device->driver.initialized) +vfio_pci_driver_remove (device); +vfio_pci_bar_unmap_all (device);VFIO_ASSERT_EQ (close (device->fd),0 );
tools/testing/selftests/vfio/lib/vfio_pci_driver.c new file +116 -0
116 lines not on this page · raw message
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 18/33 vfio: sefltests: Add vfio_pci_driver_test
In reply to David MatlackAdd a new selftest that tests all driver operations. This test serves both as a demonstration of the driver framework, and also as a correctness test for future drivers.
--- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_pci_driver_test.c | 236 ++++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_pci_driver_test.c
tools/testing/selftests/vfio/Makefile +1 -0
@@ -2,6 +2,7 @@ CFLAGS = $(KHDR_INCLUDES) TEST_GEN_PROGS += vfio_dma_mapping_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test +TEST_GEN_PROGS += vfio_pci_driver_test include ../lib.mk include lib/libvfio.mk
tools/testing/selftests/vfio/vfio_pci_driver_test.c new file +236 -0
236 lines not on this page · raw message
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 19/33 dmaengine: ioat: Move system_has_dca_enabled() to dma.h
In reply to David MatlackMove the function prototype for system_has_dca_enabled() from hw.h to dma.h. This allows hw.h to be included from tools/, which will be used in a subsysequent commit to implement a userspace driver for Intel CBDMA devices in tools/testing/selftests/vfio.
No functional change intended.
--- drivers/dma/ioat/dma.h | 2 ++ drivers/dma/ioat/hw.h | 3 --- 2 files changed, 2 insertions(+), 3 deletions(-)
drivers/dma/ioat/dma.h +2 -0
@@ -19,6 +19,8 @@#define IOAT_DMA_DCA_ANY_CPU ~0 +int system_has_dca_enabled (struct pci_dev *pdev); +#define to_ioatdma_device (dev) container_of(dev, struct ioatdma_device, dma_dev)#define to_dev (ioat_chan) (&(ioat_chan)->ioat_dma->pdev->dev)#define to_pdev (ioat_chan) ((ioat_chan)->ioat_dma->pdev)
drivers/dma/ioat/hw.h +0 -3
@@ -63,9 +63,6 @@#define IOAT_VER_3_3 0x33/* Version 3.3 */ #define IOAT_VER_3_4 0x34/* Version 3.4 */ - -int system_has_dca_enabled (struct pci_dev *pdev); -#define IOAT_DESC_SZ 64struct ioat_dma_descriptor {
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
Re: [PATCH 19/33] dmaengine: ioat: Move system_has_dca_enabled() to dma.h
Quoted 8 lines · David Matlack
Move the function prototype for
system_has_dca_enabled()fromhw.htodma.h. This allowshw.hto be included fromtools/, which will be used in a subsysequent commit to implement a userspace driver for Intel CBDMA devices intools/testing/selftests/vfio.No functional change intended.
Signed-off-by David Matlack <[email protected]>
Quoted 32 lines
--- drivers/dma/ioat/dma.h | 2 ++ drivers/dma/ioat/hw.h | 3 --- 2 files changed, 2 insertions(+), 3 deletions(-)drivers/dma/ioat/dma.h +2
-0@@ -19,6 +19,8 @@#define IOAT_DMA_DCA_ANY_CPU ~0 +int system_has_dca_enabled (struct pci_dev *pdev); +#define to_ioatdma_device (dev) container_of(dev, struct ioatdma_device, dma_dev)#define to_dev (ioat_chan) (&(ioat_chan)->ioat_dma->pdev->dev)#define to_pdev (ioat_chan) ((ioat_chan)->ioat_dma->pdev)drivers/dma/ioat/hw.h +0
-3@@ -63,9 +63,6 @@#define IOAT_VER_3_3 0x33/* Version 3.3 */ #define IOAT_VER_3_4 0x34/* Version 3.4 */ - -int system_has_dca_enabled (struct pci_dev *pdev); -#define IOAT_DESC_SZ 64struct ioat_dma_descriptor {
David Matlack
maintainerPATCH 20/33 vfio: selftests: Add driver for Intel CBDMA
In reply to David MatlackAdd a driver for the Intel CBDMA device. This driver is based on and named after the Linux driver for this device (drivers/dma/ioat/) and also based on previous work from Peter Shier <[email protected]>.
The driver aims to be as simple as possible. It uses a single descriptor to issue DMA operations, and only supports the copy operation. For "DMA storms", the driver kicks off the maximum number of maximum-sized DMA operations. On Skylake server parts, this was 2^16-1 copies of size 2M and lasts about 15 seconds.
Create symlinks to drivers/dma/ioat/{hw.h,registers.h} to get access to various macros (e.g. IOAT_CHANCMD_RESET) and struct ioat_dma_descriptor.
--- .../selftests/vfio/lib/drivers/ioat/hw.h | 1 + .../selftests/vfio/lib/drivers/ioat/ioat.c | 235 ++++++++++++++++++ .../vfio/lib/drivers/ioat/registers.h | 1 + tools/testing/selftests/vfio/lib/libvfio.mk | 7 + .../selftests/vfio/lib/vfio_pci_driver.c | 10 +- 5 files changed, 253 insertions(+), 1 deletion(-) create mode 120000 tools/testing/selftests/vfio/lib/drivers/ioat/hw.h create mode 100644 tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c create mode 120000 tools/testing/selftests/vfio/lib/drivers/ioat/registers.h
tools/testing/selftests/vfio/lib/drivers/ioat/hw.h new file +1 -0
@@ -0,0 +1 @@ +../../../../../../../drivers/dma/ioat/hw.h
tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c new file +235 -0
235 lines not on this page · raw message
tools/testing/selftests/vfio/lib/drivers/ioat/registers.h new file +1 -0
@@ -0,0 +1 @@ +../../../../../../../drivers/dma/ioat/registers.h
tools/testing/selftests/vfio/lib/libvfio.mk +7 -0
@@ -1,8 +1,15 @@ +include $(top_srcdir)/scripts/subarch.include +ARCH ?= $(SUBARCH) + VFIO_DIR := $(selfdir)/vfio LIBVFIO_C := lib/vfio_pci_device.c LIBVFIO_C += lib/vfio_pci_driver.c +ifeq ($(ARCH:x86_64=x86),x86) +LIBVFIO_C += lib/drivers/ioat/ioat.c +endif + LIBVFIO_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBVFIO_C)) LIBVFIO_O_DIRS := $(shell dirname $(LIBVFIO_O) | uniq)
tools/testing/selftests/vfio/lib/vfio_pci_driver.c +9 -1
@@ -4,7 +4,15 @@#include "../../../kselftest.h" #include <vfio_util.h> -static struct vfio_pci_driver_ops *driver_ops[] = {}; +#ifdef __x86_64__ +extern struct vfio_pci_driver_ops ioat_ops; +#endif + +static struct vfio_pci_driver_ops *driver_ops[] = { +#ifdef __x86_64__ + &ioat_ops, +#endif +};void vfio_pci_driver_probe (struct vfio_pci_device *device) {
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
Re: [PATCH 20/33] vfio: selftests: Add driver for Intel CBDMA
Quoted 10 lines · David Matlack
Add a driver for the Intel CBDMA device. This driver is based on and named after the Linux driver for this device (
drivers/dma/ioat/) and also based on previous work from Peter Shier <[email protected]>.The driver aims to be as simple as possible. It uses a single descriptor to issue DMA operations, and only supports the copy operation. For "DMA storms", the driver kicks off the maximum number of maximum-sized DMA operations. On Skylake server parts, this was 2^16-1 copies of size 2M and lasts about 15 seconds.
Quoted 6 lines
Create symlinks to
drivers/dma/ioat/{hw.h,registers.h}to get access to various macros (e.g.IOAT_CHANCMD_RESET) and structioat_dma_descriptor.Cc Dave Jiang <[email protected]>Cc Dan Williams <[email protected]>Signed-off-by David Matlack <[email protected]>
Quoted 309 lines
--- .../selftests/vfio/lib/drivers/ioat/hw.h | 1 + .../selftests/vfio/lib/drivers/ioat/ioat.c | 235 ++++++++++++++++++ .../vfio/lib/drivers/ioat/registers.h | 1 + tools/testing/selftests/vfio/lib/libvfio.mk | 7 + .../selftests/vfio/lib/vfio_pci_driver.c | 10 +- 5 files changed, 253 insertions(+), 1 deletion(-) create mode 120000 tools/testing/selftests/vfio/lib/drivers/ioat/hw.h create mode 100644 tools/testing/selftests/vfio/lib/drivers/ioat/ioat.c create mode 120000 tools/testing/selftests/vfio/lib/drivers/ioat/registers.htools/testing/selftests/vfio/lib/drivers/ioat/hw.h new file +1
-0@@ -0,0 +1 @@ +../../../../../../../drivers/dma/ioat/hw.htools/testing/selftests/vfio/lib/drivers/ioat/ioat.c new file +235
-0@@ -0,0 +1,235 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <stdint.h> +#include <unistd.h> + +#include <linux/errno.h> +#include <linux/io.h> +#include <linux/pci_ids.h> +#include <linux/sizes.h> + +#include <vfio_util.h> + +#include "hw.h" +#include "registers.h" + +#define IOAT_DMACOUNT_MAX UINT16_MAX + +struct ioat_state { +/* Single descriptor used to issue DMA memcpy operations */ +struct ioat_dma_descriptor desc; + +/* Copy buffers used by ioat_send_msi() to generate an interrupt. */ +u64 send_msi_src; +u64 send_msi_dst; +}; + +static inline struct ioat_state *to_ioat_state (struct vfio_pci_device *device) +{ +return device->driver.region.vaddr; +} + +static inline void *ioat_channel_registers (struct vfio_pci_device *device) +{ +return device->bars[0 ].vaddr +IOAT_CHANNEL_MMIO_SIZE ; +} + +static int ioat_probe (struct vfio_pci_device *device) +{ +u8 version; +int r; + +if (!vfio_pci_device_match (device,PCI_VENDOR_ID_INTEL , +PCI_DEVICE_ID_INTEL_IOAT_SKX )) +return -EINVAL ; + +VFIO_ASSERT_NOT_NULL (device->bars[0 ].vaddr); + + version =readb (device->bars[0 ].vaddr +IOAT_VER_OFFSET ); +switch (version) { +case IOAT_VER_3_2 : +case IOAT_VER_3_3 : + r =0 ; +break ; +default : +printf ("ioat: Unsupported version: 0x%x\n" , version); + r = -EINVAL ; + } +return r; +} + +static u64 ioat_channel_status (void *bar) +{ +return readq (bar +IOAT_CHANSTS_OFFSET ) &IOAT_CHANSTS_STATUS ; +} + +static void ioat_clear_errors (struct vfio_pci_device *device) +{ +void *registers =ioat_channel_registers (device); +u32 errors; + + errors =vfio_pci_config_readl (device,IOAT_PCI_CHANERR_INT_OFFSET ); +vfio_pci_config_writel (device,IOAT_PCI_CHANERR_INT_OFFSET , errors); + + errors =vfio_pci_config_readl (device,IOAT_PCI_DMAUNCERRSTS_OFFSET ); +vfio_pci_config_writel (device,IOAT_PCI_CHANERR_INT_OFFSET , errors); + + errors =readl (registers +IOAT_CHANERR_OFFSET ); +writel (errors, registers +IOAT_CHANERR_OFFSET ); +} + +static void ioat_reset (struct vfio_pci_device *device) +{ +void *registers =ioat_channel_registers (device); +u32 sleep_ms =1 , attempts =5000 / sleep_ms; +u8 chancmd; + +ioat_clear_errors (device); + +writeb (IOAT_CHANCMD_RESET , registers +IOAT2_CHANCMD_OFFSET ); + +for (;;) { + chancmd =readb (registers +IOAT2_CHANCMD_OFFSET ); +if (!(chancmd &IOAT_CHANCMD_RESET )) +break ; + +VFIO_ASSERT_GT (--attempts,0 ); +usleep (sleep_ms *1000 ); + } + +VFIO_ASSERT_EQ (ioat_channel_status (registers),IOAT_CHANSTS_HALTED ); +} + +static void ioat_init (struct vfio_pci_device *device) +{ +struct ioat_state *ioat =to_ioat_state (device); +u8 intrctrl; + +VFIO_ASSERT_GE (device->driver.region.size,sizeof (*ioat)); + +vfio_pci_config_writew (device,PCI_COMMAND , +PCI_COMMAND_MEMORY | +PCI_COMMAND_MASTER | +PCI_COMMAND_INTX_DISABLE ); + +ioat_reset (device); + +/* Enable the use of MXI-x interrupts for channel interrupts. */ + intrctrl =IOAT_INTRCTRL_MSIX_VECTOR_CONTROL ; +writeb (intrctrl, device->bars[0 ].vaddr +IOAT_INTRCTRL_OFFSET ); + +vfio_pci_msix_enable (device,0 , device->msix_info.count); + + device->driver.msi =0 ; + device->driver.max_memcpy_size = +1UL <<readb (device->bars[0 ].vaddr +IOAT_XFERCAP_OFFSET ); + device->driver.max_memcpy_count =IOAT_DMACOUNT_MAX ; +} + +static void ioat_remove (struct vfio_pci_device *device) +{ +ioat_reset (device); +vfio_pci_msix_disable (device); +} + +static void ioat_handle_error (struct vfio_pci_device *device) +{ +void *registers =ioat_channel_registers (device); + +printf ("Error detected during memcpy operation!\n" +" CHANERR: 0x%x\n" +" CHANERR_INT: 0x%x\n" +" DMAUNCERRSTS: 0x%x\n" , +readl (registers +IOAT_CHANERR_OFFSET ), +vfio_pci_config_readl (device,IOAT_PCI_CHANERR_INT_OFFSET ), +vfio_pci_config_readl (device,IOAT_PCI_DMAUNCERRSTS_OFFSET )); + +ioat_reset (device); +} + +static int ioat_memcpy_wait (struct vfio_pci_device *device) +{ +void *registers =ioat_channel_registers (device); +u64 status; +int r =0 ; + +/* Wait until all operations complete. */ +for (;;) { + status =ioat_channel_status (registers); +if (status ==IOAT_CHANSTS_DONE ) +break ; + +if (status ==IOAT_CHANSTS_HALTED ) { +ioat_handle_error (device); +return -1 ; + } + } + +/* Put the channel into the SUSPENDED state. */ +writeb (IOAT_CHANCMD_SUSPEND , registers +IOAT2_CHANCMD_OFFSET ); +for (;;) { + status =ioat_channel_status (registers); +if (status ==IOAT_CHANSTS_SUSPENDED ) +break ; + } + +return r; +} + +static void __ioat_memcpy_start (struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size, +u16 count,bool interrupt) +{ +void *registers =ioat_channel_registers (device); +struct ioat_state *ioat =to_ioat_state (device); +u64 desc_iova; +u16 chanctrl; + + desc_iova =to_iova (device, &ioat->desc); + ioat->desc = (struct ioat_dma_descriptor ) { + .ctl_f.op =IOAT_OP_COPY , + .ctl_f.int_en = interrupt, + .src_addr = src, + .dst_addr = dst, + .size = size, + .next = desc_iova, + }; + +/* Tell the device the address of the descriptor. */ +writeq (desc_iova, registers +IOAT2_CHAINADDR_OFFSET ); + +/* (Re)Enable the channel interrupt and abort on any errors */ + chanctrl =IOAT_CHANCTRL_INT_REARM |IOAT_CHANCTRL_ANY_ERR_ABORT_EN ; +writew (chanctrl, registers +IOAT_CHANCTRL_OFFSET ); + +/* Kick off @count DMA copy operation(s). */ +writew (count, registers +IOAT_CHAN_DMACOUNT_OFFSET ); +} + +static void ioat_memcpy_start (struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size, +u64 count) +{ +__ioat_memcpy_start (device, src, dst, size, count, false); +} + +static void ioat_send_msi (struct vfio_pci_device *device) +{ +struct ioat_state *ioat =to_ioat_state (device); + +__ioat_memcpy_start (device, +to_iova (device, &ioat->send_msi_src), +to_iova (device, &ioat->send_msi_dst), +sizeof (ioat->send_msi_src),1 , true); + +VFIO_ASSERT_EQ (ioat_memcpy_wait (device),0 ); +} + +const struct vfio_pci_driver_ops ioat_ops = { + .name ="ioat" , + .probe = ioat_probe, + .init = ioat_init, + .remove = ioat_remove, + .memcpy_start = ioat_memcpy_start, + .memcpy_wait = ioat_memcpy_wait, + .send_msi = ioat_send_msi, +};tools/testing/selftests/vfio/lib/drivers/ioat/registers.h new file +1
-0@@ -0,0 +1 @@ +../../../../../../../drivers/dma/ioat/registers.htools/testing/selftests/vfio/lib/libvfio.mk +7
-0@@ -1,8 +1,15 @@ +include $(top_srcdir)/scripts/subarch.include +ARCH ?= $(SUBARCH) + VFIO_DIR := $(selfdir)/vfio LIBVFIO_C := lib/vfio_pci_device.c LIBVFIO_C += lib/vfio_pci_driver.c +ifeq ($(ARCH:x86_64=x86),x86) +LIBVFIO_C += lib/drivers/ioat/ioat.c +endif + LIBVFIO_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBVFIO_C)) LIBVFIO_O_DIRS := $(shell dirname $(LIBVFIO_O) | uniq)tools/testing/selftests/vfio/lib/vfio_pci_driver.c +9
-1@@ -4,7 +4,15 @@#include "../../../kselftest.h" #include <vfio_util.h> -static struct vfio_pci_driver_ops *driver_ops[] = {}; +#ifdef __x86_64__ +extern struct vfio_pci_driver_ops ioat_ops; +#endif + +static struct vfio_pci_driver_ops *driver_ops[] = { +#ifdef __x86_64__ + &ioat_ops, +#endif +};void vfio_pci_driver_probe (struct vfio_pci_device *device) {
David Matlack
maintainerPATCH 21/33 dmaengine: idxd: Allow registers.h to be included from tools/
In reply to David MatlackAllow drivers/dma/idxd/registers.h to be included from userspace in tools/ by adjusting the include path to uapi/linux/idxd.h if __KERNEL__ is not defined.
A subsequent commit will use registers.h to implement a userspace driver for Intel DSA devices in tools/testing/selftests/vfio.
--- drivers/dma/idxd/registers.h | 4 ++++ 1 file changed, 4 insertions(+)
drivers/dma/idxd/registers.h +4 -0
@@ -3,7 +3,11 @@#ifndef _IDXD_REGISTERS_H_#define _IDXD_REGISTERS_H_ +#ifdef __KERNEL__#include <uapi/linux/idxd.h> +#else +#include <linux/idxd.h> +#endif /* PCI Config */ #define PCI_DEVICE_ID_INTEL_DSA_GNRD 0x11fb
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
Re: [PATCH 21/33] dmaengine: idxd: Allow registers.h to be included from tools/
Quoted 8 lines · David Matlack
Allow
drivers/dma/idxd/registers.hto be included from userspace intools/by adjusting the include path touapi/linux/idxd.hif__KERNEL__is not defined.A subsequent commit will use
registers.hto implement a userspace driver for Intel DSA devices intools/testing/selftests/vfio.Signed-off-by David Matlack <[email protected]>
Quoted 23 lines
--- drivers/dma/idxd/registers.h | 4 ++++ 1 file changed, 4 insertions(+)drivers/dma/idxd/registers.h +4
-0@@ -3,7 +3,11 @@#ifndef _IDXD_REGISTERS_H_#define _IDXD_REGISTERS_H_ +#ifdef __KERNEL__#include <uapi/linux/idxd.h> +#else +#include <linux/idxd.h> +#endif /* PCI Config */ #define PCI_DEVICE_ID_INTEL_DSA_GNRD 0x11fbSignature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
Signature · 1 line
Vinicius
David Matlack
maintainerPATCH 22/33 vfio: selftests: Add driver for Intel DSA
In reply to David MatlackAdd a driver to VFIO selftests for Intel DSA devices.
For now the driver only supports up to 32 batches and 1024 copies per batch, which were the limits of the hardware this commit was tested with. This is sufficient to generate 9+ minutes of DMA memcpys at a rate of over 30 GB/s. This should be plenty to stress test VFIO and the IOMMU.
The driver does not yet support requesting interrupt handles, as this commit was not tested against hardware that requires it.
--- .../selftests/vfio/lib/drivers/dsa/dsa.c | 416 ++++++++++++++++++ .../vfio/lib/drivers/dsa/registers.h | 1 + tools/testing/selftests/vfio/lib/libvfio.mk | 1 + .../selftests/vfio/lib/vfio_pci_driver.c | 2 + 4 files changed, 420 insertions(+) create mode 100644 tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c create mode 120000 tools/testing/selftests/vfio/lib/drivers/dsa/registers.h
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +416 -0
@@ -0,0 +1,416 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <stdint.h> +#include <unistd.h> + +#include <linux/bits.h> +#include <linux/errno.h> +#include <linux/idxd.h> +#include <linux/io.h> +#include <linux/pci_ids.h> +#include <linux/sizes.h> + +#include <vfio_util.h> + +#include "registers.h" + +/* Vectors 1+ are available for work queue completion interrupts. */ +#define MSIX_VECTOR 1 + +struct dsa_state { +/* Descriptors for copy and batch operations. */ +struct dsa_hw_desc batch[32 ]; +struct dsa_hw_desc copy[1024 ]; + +/* Completion records for copy and batch operations. */ +struct dsa_completion_record copy_completion; +struct dsa_completion_record batch_completion; + +/* Cached device registers (and derived data) for easy access */ +union gen_cap_reg gen_cap; +union wq_cap_reg wq_cap; +union group_cap_reg group_cap; +union engine_cap_reg engine_cap; +union offsets_reg table_offsets; +void *wqcfg_table; +void *grpcfg_table; +u64 max_batches; +u64 max_copies_per_batch; + +/* The number of ongoing memcpy operations. */ +u64 memcpy_count; + +/* Buffers used by dsa_send_msi() to generate an interrupt */ +u64 send_msi_src; +u64 send_msi_dst; +}; + +static inline struct dsa_state *to_dsa_state (struct vfio_pci_device *device) +{ +return device->driver.region.vaddr; +} + +static bool dsa_int_handle_request_required (struct vfio_pci_device *device) +{ +void *bar0 = device->bars[0 ].vaddr; +union gen_cap_reg gen_cap; +u32 cmd_cap; + + gen_cap.bits =readq (bar0 +IDXD_GENCAP_OFFSET ); +if (!gen_cap.cmd_cap) +return false; + + cmd_cap =readl (bar0 +IDXD_CMDCAP_OFFSET ); +return (cmd_cap >>IDXD_CMD_REQUEST_INT_HANDLE ) &1 ; +} + +static int dsa_probe (struct vfio_pci_device *device) +{ +if (!vfio_pci_device_match (device,PCI_VENDOR_ID_INTEL , +PCI_DEVICE_ID_INTEL_DSA_SPR0 )) +return -EINVAL ; + +if (dsa_int_handle_request_required (device)) { +printf ("Device requires requesting interrupt handles\n" ); +return -EINVAL ; + } + +return 0 ; +} + +static void dsa_check_sw_err (struct vfio_pci_device *device) +{ +void *reg = device->bars[0 ].vaddr +IDXD_SWERR_OFFSET ; +union sw_err_reg err = {}; +int i; + +for (i =0 ; i <ARRAY_SIZE (err.bits); i++) { + err.bits[i] = readq(reg + offsetof(union sw_err_reg, bits[i])); + +/* No errors */ +if (i ==0 && !err.valid) +return ; + } + +fprintf (stderr,"SWERR: 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n" , + err.bits[0 ], err.bits[1 ], err.bits[2 ], err.bits[3 ]); + +fprintf (stderr," valid: 0x%x\n" , err.valid); +fprintf (stderr," overflow: 0x%x\n" , err.overflow); +fprintf (stderr," desc_valid: 0x%x\n" , err.desc_valid); +fprintf (stderr," wq_idx_valid: 0x%x\n" , err.wq_idx_valid); +fprintf (stderr," batch: 0x%x\n" , err.batch); +fprintf (stderr," fault_rw: 0x%x\n" , err.fault_rw); +fprintf (stderr," priv: 0x%x\n" , err.priv); +fprintf (stderr," error: 0x%x\n" , err.error); +fprintf (stderr," wq_idx: 0x%x\n" , err.wq_idx); +fprintf (stderr," operation: 0x%x\n" , err.operation); +fprintf (stderr," pasid: 0x%x\n" , err.pasid); +fprintf (stderr," batch_idx: 0x%x\n" , err.batch_idx); +fprintf (stderr," invalid_flags: 0x%x\n" , err.invalid_flags); +fprintf (stderr," fault_addr: 0x%lx\n" , err.fault_addr); + +VFIO_FAIL ("Software Error Detected!\n" ); +} + +static void dsa_command (struct vfio_pci_device *device,u32 cmd) +{ +union idxd_command_reg cmd_reg = { .cmd = cmd }; +u32 sleep_ms =1 , attempts =5000 / sleep_ms; +void *bar0 = device->bars[0 ].vaddr; +u32 status; +u8 err; + +writel (cmd_reg.bits, bar0 +IDXD_CMD_OFFSET ); + +for (;;) { +dsa_check_sw_err (device); + + status =readl (bar0 +IDXD_CMDSTS_OFFSET ); +if (!(status &IDXD_CMDSTS_ACTIVE )) +break ; + +VFIO_ASSERT_GT (--attempts,0 ); +usleep (sleep_ms *1000 ); + } + + err = status &IDXD_CMDSTS_ERR_MASK ; +VFIO_ASSERT_EQ (err,0 ,"Error issuing command 0x%x: 0x%x\n" , cmd, err); +} + +static void dsa_wq_init (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); +union wq_cap_reg wq_cap = dsa->wq_cap; +union wqcfg wqcfg; +u64 wqcfg_size; +int i; + +VFIO_ASSERT_GT ((u32 )wq_cap.num_wqs,0 ); + + wqcfg = (union wqcfg ) { + .wq_size = wq_cap.total_wq_size, + .mode =1 , + .priority =1 , +/* +* Disable Address Translation Service (if enabled) so that VFIO +* selftests using this driver can generate I/O page faults. +*/ + .wq_ats_disable = wq_cap.wq_ats_support, + .max_xfer_shift = dsa->gen_cap.max_xfer_shift, + .max_batch_shift = dsa->gen_cap.max_batch_shift, + .op_config[0 ] =BIT (DSA_OPCODE_MEMMOVE ) |BIT (DSA_OPCODE_BATCH ), + }; + + wqcfg_size =1UL << (wq_cap.wqcfg_size +IDXD_WQCFG_MIN ); + +for (i =0 ; i < wqcfg_size /sizeof (wqcfg.bits[0 ]); i++) + writel(wqcfg.bits[i], dsa->wqcfg_table + offsetof(union wqcfg, bits[i])); +} + +static void dsa_group_init (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); +union group_cap_reg group_cap = dsa->group_cap; +union engine_cap_reg engine_cap = dsa->engine_cap; + +VFIO_ASSERT_GT ((u32 )group_cap.num_groups,0 ); +VFIO_ASSERT_GT ((u32 )engine_cap.num_engines,0 ); + +/* Assign work queue 0 and engine 0 to group 0 */ + writeq(1, dsa->grpcfg_table + offsetof(struct grpcfg, wqs[0])); +writeq (1 , dsa->grpcfg_table + offsetof(struct grpcfg , engines)); +} + +static void dsa_register_cache_init (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); +void *bar0 = device->bars[0 ].vaddr; + + dsa->gen_cap.bits =readq (bar0 +IDXD_GENCAP_OFFSET ); + dsa->wq_cap.bits =readq (bar0 +IDXD_WQCAP_OFFSET ); + dsa->group_cap.bits =readq (bar0 +IDXD_GRPCAP_OFFSET ); + dsa->engine_cap.bits =readq (bar0 +IDXD_ENGCAP_OFFSET ); + + dsa->table_offsets.bits[0 ] =readq (bar0 +IDXD_TABLE_OFFSET ); + dsa->table_offsets.bits[1 ] =readq (bar0 +IDXD_TABLE_OFFSET +8 ); + + dsa->wqcfg_table = bar0 + dsa->table_offsets.wqcfg *IDXD_TABLE_MULT ; + dsa->grpcfg_table = bar0 + dsa->table_offsets.grpcfg *IDXD_TABLE_MULT ; + + dsa->max_batches =1U << (dsa->wq_cap.total_wq_size +IDXD_WQCFG_MIN ); + dsa->max_batches =min (dsa->max_batches,ARRAY_SIZE (dsa->batch)); + + dsa->max_copies_per_batch =1UL << dsa->gen_cap.max_batch_shift; + dsa->max_copies_per_batch =min (dsa->max_copies_per_batch,ARRAY_SIZE (dsa->copy)); +} + +static void dsa_init (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); + +VFIO_ASSERT_GE (device->driver.region.size,sizeof (*dsa)); + +vfio_pci_config_writew (device,PCI_COMMAND , +PCI_COMMAND_MEMORY | +PCI_COMMAND_MASTER | +PCI_COMMAND_INTX_DISABLE ); + +dsa_command (device,IDXD_CMD_RESET_DEVICE ); + +dsa_register_cache_init (device); +dsa_wq_init (device); +dsa_group_init (device); + +dsa_command (device,IDXD_CMD_ENABLE_DEVICE ); +dsa_command (device,IDXD_CMD_ENABLE_WQ ); + +vfio_pci_msix_enable (device,MSIX_VECTOR ,1 ); + + device->driver.max_memcpy_count = + dsa->max_batches * dsa->max_copies_per_batch; + device->driver.max_memcpy_size =1UL << dsa->gen_cap.max_xfer_shift; + device->driver.msi =MSIX_VECTOR ; +} + +static void dsa_remove (struct vfio_pci_device *device) +{ +dsa_command (device,IDXD_CMD_RESET_DEVICE ); +vfio_pci_msix_disable (device); +} + +static int dsa_completion_wait (struct vfio_pci_device *device, +struct dsa_completion_record *completion) +{ +u8 status; + +for (;;) { +dsa_check_sw_err (device); + + status =READ_ONCE (completion->status); +if (status) +break ; + +usleep (1000 ); + } + +if (status ==DSA_COMP_SUCCESS ) +return 0 ; + +printf ("Error detected during memcpy operation: 0x%x\n" , status); +return -1 ; +} + +static void dsa_copy_desc_init (struct vfio_pci_device *device, +struct dsa_hw_desc *desc, +iova_t src,iova_t dst,u64 size, +bool interrupt) +{ +struct dsa_state *dsa =to_dsa_state (device); +u16 flags; + + flags =IDXD_OP_FLAG_CRAV |IDXD_OP_FLAG_RCR ; + +if (interrupt) + flags |=IDXD_OP_FLAG_RCI ; + + *desc = (struct dsa_hw_desc ) { + .opcode =DSA_OPCODE_MEMMOVE , + .flags = flags, + .priv =1 , + .src_addr = src, + .dst_addr = dst, + .xfer_size = size, + .completion_addr =to_iova (device, &dsa->copy_completion), + .int_handle = interrupt ?MSIX_VECTOR :0 , + }; +} + +static void dsa_batch_desc_init (struct vfio_pci_device *device, +struct dsa_hw_desc *desc, +u64 count) +{ +struct dsa_state *dsa =to_dsa_state (device); + + *desc = (struct dsa_hw_desc ) { + .opcode =DSA_OPCODE_BATCH , + .flags =IDXD_OP_FLAG_CRAV , + .priv =1 , + .completion_addr =to_iova (device, &dsa->batch_completion), + .desc_list_addr =to_iova (device, &dsa->copy[0 ]), + .desc_count = count, + }; +} + +static void dsa_desc_write (struct vfio_pci_device *device,struct dsa_hw_desc *desc) +{ +/* Write the contents (not address) of the 64-byte descriptor to the device. */ +iosubmit_cmds512 (device->bars[2 ].vaddr, desc,1 ); +} + +static void dsa_memcpy_one (struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size,bool interrupt) +{ +struct dsa_state *dsa =to_dsa_state (device); + +memset (&dsa->copy_completion,0 ,sizeof (dsa->copy_completion)); + +dsa_copy_desc_init (device, &dsa->copy[0 ], src, dst, size, interrupt); +dsa_desc_write (device, &dsa->copy[0 ]); +} + +static void dsa_memcpy_batch (struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size,u64 count) +{ +struct dsa_state *dsa =to_dsa_state (device); +int i; + +memset (&dsa->batch_completion,0 ,sizeof (dsa->batch_completion)); + +for (i =0 ; i <ARRAY_SIZE (dsa->copy); i++) { +struct dsa_hw_desc *copy_desc = &dsa->copy[i]; + +dsa_copy_desc_init (device, copy_desc, src, dst, size, false); + +/* Don't request completions for individual copies. */ + copy_desc->flags &= ~IDXD_OP_FLAG_RCR ; + } + +for (i =0 ; i <ARRAY_SIZE (dsa->batch) && count; i++) { +struct dsa_hw_desc *batch_desc = &dsa->batch[i]; +int nr_copies; + + nr_copies =min (count, dsa->max_copies_per_batch); + count -= nr_copies; + +/* +* Batches must have at least 2 copies, so handle the case where +* there is exactly 1 copy left by doing one less copy in this +* batch and then 2 in the next. +*/ +if (count ==1 ) { + nr_copies--; + count++; + } + +dsa_batch_desc_init (device, batch_desc, nr_copies); + +/* Request a completion for the last batch. */ +if (!count) + batch_desc->flags |=IDXD_OP_FLAG_RCR ; + +dsa_desc_write (device, batch_desc); + } + +VFIO_ASSERT_EQ (count,0 ,"Failed to start %lu copies.\n" , count); +} + +static void dsa_memcpy_start (struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size,u64 count) +{ +struct dsa_state *dsa =to_dsa_state (device); + +/* DSA devices require at least 2 copies per batch. */ +if (count ==1 ) +dsa_memcpy_one (device, src, dst, size, false); +else +dsa_memcpy_batch (device, src, dst, size, count); + + dsa->memcpy_count = count; +} + +static int dsa_memcpy_wait (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); +int r; + +if (dsa->memcpy_count ==1 ) + r =dsa_completion_wait (device, &dsa->copy_completion); +else + r =dsa_completion_wait (device, &dsa->batch_completion); + + dsa->memcpy_count =0 ; + +return r; +} + +static void dsa_send_msi (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); + +dsa_memcpy_one (device, +to_iova (device, &dsa->send_msi_src), +to_iova (device, &dsa->send_msi_dst), +sizeof (dsa->send_msi_src), true); + +VFIO_ASSERT_EQ (dsa_completion_wait (device, &dsa->copy_completion),0 ); +} + +const struct vfio_pci_driver_ops dsa_ops = { + .name ="dsa" , + .probe = dsa_probe, + .init = dsa_init, + .remove = dsa_remove, + .memcpy_start = dsa_memcpy_start, + .memcpy_wait = dsa_memcpy_wait, + .send_msi = dsa_send_msi, +};
tools/testing/selftests/vfio/lib/drivers/dsa/registers.h new file +1 -0
@@ -0,0 +1 @@ +../../../../../../../drivers/dma/idxd/registers.h
tools/testing/selftests/vfio/lib/libvfio.mk +1 -0
@@ -8,6 +8,7 @@ LIBVFIO_C += lib/vfio_pci_driver.c ifeq ($(ARCH:x86_64=x86),x86) LIBVFIO_C += lib/drivers/ioat/ioat.c +LIBVFIO_C += lib/drivers/dsa/dsa.c endif LIBVFIO_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBVFIO_C))
tools/testing/selftests/vfio/lib/vfio_pci_driver.c +2 -0
@@ -5,11 +5,13 @@#include <vfio_util.h> #ifdef __x86_64__ +extern struct vfio_pci_driver_ops dsa_ops;extern struct vfio_pci_driver_ops ioat_ops;#endif static struct vfio_pci_driver_ops *driver_ops[] = { #ifdef __x86_64__ + &dsa_ops, &ioat_ops, #endif };
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
Re: [PATCH 22/33] vfio: selftests: Add driver for Intel DSA
Quoted 10 lines · David Matlack
Add a driver to VFIO selftests for Intel DSA devices.
For now the driver only supports up to 32 batches and 1024 copies per batch, which were the limits of the hardware this commit was tested with. This is sufficient to generate 9+ minutes of DMA memcpys at a rate of over 30 GB/s. This should be plenty to stress test VFIO and the IOMMU.
The driver does not yet support requesting interrupt handles, as this commit was not tested against hardware that requires it.
Just some questions, for the code:
Quoted 77 lines
Cc Vinicius Costa Gomes <[email protected]>Cc Dave Jiang <[email protected]>Signed-off-by David Matlack <[email protected]>--- .../selftests/vfio/lib/drivers/dsa/dsa.c | 416 ++++++++++++++++++ .../vfio/lib/drivers/dsa/registers.h | 1 + tools/testing/selftests/vfio/lib/libvfio.mk | 1 + .../selftests/vfio/lib/vfio_pci_driver.c | 2 + 4 files changed, 420 insertions(+) create mode 100644 tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c create mode 120000 tools/testing/selftests/vfio/lib/drivers/dsa/registers.htools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +59
-0@@ -0,0 +1,416 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <stdint.h> +#include <unistd.h> + +#include <linux/bits.h> +#include <linux/errno.h> +#include <linux/idxd.h> +#include <linux/io.h> +#include <linux/pci_ids.h> +#include <linux/sizes.h> + +#include <vfio_util.h> + +#include "registers.h" + +/* Vectors 1+ are available for work queue completion interrupts. */ +#define MSIX_VECTOR 1 + +struct dsa_state { +/* Descriptors for copy and batch operations. */ +struct dsa_hw_desc batch[32 ]; +struct dsa_hw_desc copy[1024 ]; + +/* Completion records for copy and batch operations. */ +struct dsa_completion_record copy_completion; +struct dsa_completion_record batch_completion; + +/* Cached device registers (and derived data) for easy access */ +union gen_cap_reg gen_cap; +union wq_cap_reg wq_cap; +union group_cap_reg group_cap; +union engine_cap_reg engine_cap; +union offsets_reg table_offsets; +void *wqcfg_table; +void *grpcfg_table; +u64 max_batches; +u64 max_copies_per_batch; + +/* The number of ongoing memcpy operations. */ +u64 memcpy_count; + +/* Buffers used by dsa_send_msi() to generate an interrupt */ +u64 send_msi_src; +u64 send_msi_dst; +}; + +static inline struct dsa_state *to_dsa_state (struct vfio_pci_device *device) +{ +return device->driver.region.vaddr; +} + +static bool dsa_int_handle_request_required (struct vfio_pci_device *device) +{ +void *bar0 = device->bars[0 ].vaddr; +union gen_cap_reg gen_cap; +u32 cmd_cap; + + gen_cap.bits =readq (bar0 +IDXD_GENCAP_OFFSET ); +if (!gen_cap.cmd_cap)
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c line 69
Quoted 10 lines
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +10
-0+return false; + + cmd_cap =readl (bar0 +IDXD_CMDCAP_OFFSET ); +return (cmd_cap >>IDXD_CMD_REQUEST_INT_HANDLE ) &1 ; +} + +static int dsa_probe (struct vfio_pci_device *device) +{ +if (!vfio_pci_device_match (device,PCI_VENDOR_ID_INTEL , +PCI_DEVICE_ID_INTEL_DSA_SPR0 ))
What are you thinking about adding support for multiple device ids?
Quoted 174 lines
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +174
-0+return -EINVAL ; + +if (dsa_int_handle_request_required (device)) { +printf ("Device requires requesting interrupt handles\n" ); +return -EINVAL ; + } + +return 0 ; +} + +static void dsa_check_sw_err (struct vfio_pci_device *device) +{ +void *reg = device->bars[0 ].vaddr +IDXD_SWERR_OFFSET ; +union sw_err_reg err = {}; +int i; + +for (i =0 ; i <ARRAY_SIZE (err.bits); i++) { + err.bits[i] = readq(reg + offsetof(union sw_err_reg, bits[i])); + +/* No errors */ +if (i ==0 && !err.valid) +return ; + } + +fprintf (stderr,"SWERR: 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n" , + err.bits[0 ], err.bits[1 ], err.bits[2 ], err.bits[3 ]); + +fprintf (stderr," valid: 0x%x\n" , err.valid); +fprintf (stderr," overflow: 0x%x\n" , err.overflow); +fprintf (stderr," desc_valid: 0x%x\n" , err.desc_valid); +fprintf (stderr," wq_idx_valid: 0x%x\n" , err.wq_idx_valid); +fprintf (stderr," batch: 0x%x\n" , err.batch); +fprintf (stderr," fault_rw: 0x%x\n" , err.fault_rw); +fprintf (stderr," priv: 0x%x\n" , err.priv); +fprintf (stderr," error: 0x%x\n" , err.error); +fprintf (stderr," wq_idx: 0x%x\n" , err.wq_idx); +fprintf (stderr," operation: 0x%x\n" , err.operation); +fprintf (stderr," pasid: 0x%x\n" , err.pasid); +fprintf (stderr," batch_idx: 0x%x\n" , err.batch_idx); +fprintf (stderr," invalid_flags: 0x%x\n" , err.invalid_flags); +fprintf (stderr," fault_addr: 0x%lx\n" , err.fault_addr); + +VFIO_FAIL ("Software Error Detected!\n" ); +} + +static void dsa_command (struct vfio_pci_device *device,u32 cmd) +{ +union idxd_command_reg cmd_reg = { .cmd = cmd }; +u32 sleep_ms =1 , attempts =5000 / sleep_ms; +void *bar0 = device->bars[0 ].vaddr; +u32 status; +u8 err; + +writel (cmd_reg.bits, bar0 +IDXD_CMD_OFFSET ); + +for (;;) { +dsa_check_sw_err (device); + + status =readl (bar0 +IDXD_CMDSTS_OFFSET ); +if (!(status &IDXD_CMDSTS_ACTIVE )) +break ; + +VFIO_ASSERT_GT (--attempts,0 ); +usleep (sleep_ms *1000 ); + } + + err = status &IDXD_CMDSTS_ERR_MASK ; +VFIO_ASSERT_EQ (err,0 ,"Error issuing command 0x%x: 0x%x\n" , cmd, err); +} + +static void dsa_wq_init (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); +union wq_cap_reg wq_cap = dsa->wq_cap; +union wqcfg wqcfg; +u64 wqcfg_size; +int i; + +VFIO_ASSERT_GT ((u32 )wq_cap.num_wqs,0 ); + + wqcfg = (union wqcfg ) { + .wq_size = wq_cap.total_wq_size, + .mode =1 , + .priority =1 , +/* +* Disable Address Translation Service (if enabled) so that VFIO +* selftests using this driver can generate I/O page faults. +*/ + .wq_ats_disable = wq_cap.wq_ats_support, + .max_xfer_shift = dsa->gen_cap.max_xfer_shift, + .max_batch_shift = dsa->gen_cap.max_batch_shift, + .op_config[0 ] =BIT (DSA_OPCODE_MEMMOVE ) |BIT (DSA_OPCODE_BATCH ), + }; + + wqcfg_size =1UL << (wq_cap.wqcfg_size +IDXD_WQCFG_MIN ); + +for (i =0 ; i < wqcfg_size /sizeof (wqcfg.bits[0 ]); i++) + writel(wqcfg.bits[i], dsa->wqcfg_table + offsetof(union wqcfg, bits[i])); +} + +static void dsa_group_init (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); +union group_cap_reg group_cap = dsa->group_cap; +union engine_cap_reg engine_cap = dsa->engine_cap; + +VFIO_ASSERT_GT ((u32 )group_cap.num_groups,0 ); +VFIO_ASSERT_GT ((u32 )engine_cap.num_engines,0 ); + +/* Assign work queue 0 and engine 0 to group 0 */ + writeq(1, dsa->grpcfg_table + offsetof(struct grpcfg, wqs[0])); +writeq (1 , dsa->grpcfg_table + offsetof(struct grpcfg , engines)); +} + +static void dsa_register_cache_init (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); +void *bar0 = device->bars[0 ].vaddr; + + dsa->gen_cap.bits =readq (bar0 +IDXD_GENCAP_OFFSET ); + dsa->wq_cap.bits =readq (bar0 +IDXD_WQCAP_OFFSET ); + dsa->group_cap.bits =readq (bar0 +IDXD_GRPCAP_OFFSET ); + dsa->engine_cap.bits =readq (bar0 +IDXD_ENGCAP_OFFSET ); + + dsa->table_offsets.bits[0 ] =readq (bar0 +IDXD_TABLE_OFFSET ); + dsa->table_offsets.bits[1 ] =readq (bar0 +IDXD_TABLE_OFFSET +8 ); + + dsa->wqcfg_table = bar0 + dsa->table_offsets.wqcfg *IDXD_TABLE_MULT ; + dsa->grpcfg_table = bar0 + dsa->table_offsets.grpcfg *IDXD_TABLE_MULT ; + + dsa->max_batches =1U << (dsa->wq_cap.total_wq_size +IDXD_WQCFG_MIN ); + dsa->max_batches =min (dsa->max_batches,ARRAY_SIZE (dsa->batch)); + + dsa->max_copies_per_batch =1UL << dsa->gen_cap.max_batch_shift; + dsa->max_copies_per_batch =min (dsa->max_copies_per_batch,ARRAY_SIZE (dsa->copy)); +} + +static void dsa_init (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); + +VFIO_ASSERT_GE (device->driver.region.size,sizeof (*dsa)); + +vfio_pci_config_writew (device,PCI_COMMAND , +PCI_COMMAND_MEMORY | +PCI_COMMAND_MASTER | +PCI_COMMAND_INTX_DISABLE ); + +dsa_command (device,IDXD_CMD_RESET_DEVICE ); + +dsa_register_cache_init (device); +dsa_wq_init (device); +dsa_group_init (device); + +dsa_command (device,IDXD_CMD_ENABLE_DEVICE ); +dsa_command (device,IDXD_CMD_ENABLE_WQ ); + +vfio_pci_msix_enable (device,MSIX_VECTOR ,1 ); + + device->driver.max_memcpy_count = + dsa->max_batches * dsa->max_copies_per_batch; + device->driver.max_memcpy_size =1UL << dsa->gen_cap.max_xfer_shift; + device->driver.msi =MSIX_VECTOR ; +} + +static void dsa_remove (struct vfio_pci_device *device) +{ +dsa_command (device,IDXD_CMD_RESET_DEVICE ); +vfio_pci_msix_disable (device); +} + +static int dsa_completion_wait (struct vfio_pci_device *device, +struct dsa_completion_record *completion) +{
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c line 253
Quoted 10 lines
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +10
-0+u8 status; + +for (;;) { +dsa_check_sw_err (device); + + status =READ_ONCE (completion->status); +if (status) +break ; + +usleep (1000 );
Another minor/thing to think about: using umonitor/umwait.
Quoted 204 lines
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +163
-0+ } + +if (status ==DSA_COMP_SUCCESS ) +return 0 ; + +printf ("Error detected during memcpy operation: 0x%x\n" , status); +return -1 ; +} + +static void dsa_copy_desc_init (struct vfio_pci_device *device, +struct dsa_hw_desc *desc, +iova_t src,iova_t dst,u64 size, +bool interrupt) +{ +struct dsa_state *dsa =to_dsa_state (device); +u16 flags; + + flags =IDXD_OP_FLAG_CRAV |IDXD_OP_FLAG_RCR ; + +if (interrupt) + flags |=IDXD_OP_FLAG_RCI ; + + *desc = (struct dsa_hw_desc ) { + .opcode =DSA_OPCODE_MEMMOVE , + .flags = flags, + .priv =1 , + .src_addr = src, + .dst_addr = dst, + .xfer_size = size, + .completion_addr =to_iova (device, &dsa->copy_completion), + .int_handle = interrupt ?MSIX_VECTOR :0 , + }; +} + +static void dsa_batch_desc_init (struct vfio_pci_device *device, +struct dsa_hw_desc *desc, +u64 count) +{ +struct dsa_state *dsa =to_dsa_state (device); + + *desc = (struct dsa_hw_desc ) { + .opcode =DSA_OPCODE_BATCH , + .flags =IDXD_OP_FLAG_CRAV , + .priv =1 , + .completion_addr =to_iova (device, &dsa->batch_completion), + .desc_list_addr =to_iova (device, &dsa->copy[0 ]), + .desc_count = count, + }; +} + +static void dsa_desc_write (struct vfio_pci_device *device,struct dsa_hw_desc *desc) +{ +/* Write the contents (not address) of the 64-byte descriptor to the device. */ +iosubmit_cmds512 (device->bars[2 ].vaddr, desc,1 ); +} + +static void dsa_memcpy_one (struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size,bool interrupt) +{ +struct dsa_state *dsa =to_dsa_state (device); + +memset (&dsa->copy_completion,0 ,sizeof (dsa->copy_completion)); + +dsa_copy_desc_init (device, &dsa->copy[0 ], src, dst, size, interrupt); +dsa_desc_write (device, &dsa->copy[0 ]); +} + +static void dsa_memcpy_batch (struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size,u64 count) +{ +struct dsa_state *dsa =to_dsa_state (device); +int i; + +memset (&dsa->batch_completion,0 ,sizeof (dsa->batch_completion)); + +for (i =0 ; i <ARRAY_SIZE (dsa->copy); i++) { +struct dsa_hw_desc *copy_desc = &dsa->copy[i]; + +dsa_copy_desc_init (device, copy_desc, src, dst, size, false); + +/* Don't request completions for individual copies. */ + copy_desc->flags &= ~IDXD_OP_FLAG_RCR ; + } + +for (i =0 ; i <ARRAY_SIZE (dsa->batch) && count; i++) { +struct dsa_hw_desc *batch_desc = &dsa->batch[i]; +int nr_copies; + + nr_copies =min (count, dsa->max_copies_per_batch); + count -= nr_copies; + +/* +* Batches must have at least 2 copies, so handle the case where +* there is exactly 1 copy left by doing one less copy in this +* batch and then 2 in the next. +*/ +if (count ==1 ) { + nr_copies--; + count++; + } + +dsa_batch_desc_init (device, batch_desc, nr_copies); + +/* Request a completion for the last batch. */ +if (!count) + batch_desc->flags |=IDXD_OP_FLAG_RCR ; + +dsa_desc_write (device, batch_desc); + } + +VFIO_ASSERT_EQ (count,0 ,"Failed to start %lu copies.\n" , count); +} + +static void dsa_memcpy_start (struct vfio_pci_device *device, +iova_t src,iova_t dst,u64 size,u64 count) +{ +struct dsa_state *dsa =to_dsa_state (device); + +/* DSA devices require at least 2 copies per batch. */ +if (count ==1 ) +dsa_memcpy_one (device, src, dst, size, false); +else +dsa_memcpy_batch (device, src, dst, size, count); + + dsa->memcpy_count = count; +} + +static int dsa_memcpy_wait (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); +int r; + +if (dsa->memcpy_count ==1 ) + r =dsa_completion_wait (device, &dsa->copy_completion); +else + r =dsa_completion_wait (device, &dsa->batch_completion); + + dsa->memcpy_count =0 ; + +return r; +} + +static void dsa_send_msi (struct vfio_pci_device *device) +{ +struct dsa_state *dsa =to_dsa_state (device); + +dsa_memcpy_one (device, +to_iova (device, &dsa->send_msi_src), +to_iova (device, &dsa->send_msi_dst), +sizeof (dsa->send_msi_src), true); + +VFIO_ASSERT_EQ (dsa_completion_wait (device, &dsa->copy_completion),0 ); +} + +const struct vfio_pci_driver_ops dsa_ops = { + .name ="dsa" , + .probe = dsa_probe, + .init = dsa_init, + .remove = dsa_remove, + .memcpy_start = dsa_memcpy_start, + .memcpy_wait = dsa_memcpy_wait, + .send_msi = dsa_send_msi, +};tools/testing/selftests/vfio/lib/drivers/dsa/registers.h new file +1
-0@@ -0,0 +1 @@ +../../../../../../../drivers/dma/idxd/registers.htools/testing/selftests/vfio/lib/libvfio.mk +1
-0@@ -8,6 +8,7 @@ LIBVFIO_C += lib/vfio_pci_driver.c ifeq ($(ARCH:x86_64=x86),x86) LIBVFIO_C += lib/drivers/ioat/ioat.c +LIBVFIO_C += lib/drivers/dsa/dsa.c endif LIBVFIO_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBVFIO_C))tools/testing/selftests/vfio/lib/vfio_pci_driver.c +2
-0@@ -5,11 +5,13 @@#include <vfio_util.h> #ifdef __x86_64__ +extern struct vfio_pci_driver_ops dsa_ops;extern struct vfio_pci_driver_ops ioat_ops;#endif static struct vfio_pci_driver_ops *driver_ops[] = { #ifdef __x86_64__ + &dsa_ops, &ioat_ops, #endif };Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
Signature · 1 line
Vinicius
David Matlack
maintainerRe: [PATCH 22/33] vfio: selftests: Add driver for Intel DSA
Quoted 8 lines · Vinicius Costa Gomes
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c line 69
Quoted 5 lines · David Matlack
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +5
-0+ +static int dsa_probe (struct vfio_pci_device *device) +{ +if (!vfio_pci_device_match (device,PCI_VENDOR_ID_INTEL , +PCI_DEVICE_ID_INTEL_DSA_SPR0 ))What are you thinking about adding support for multiple device ids?
I haven't given it much thought yet. But we could definitely support fancier device matching (e.g. multiple acceptable device ids) if/when a use-case for that arises.
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +7
-0+static int dsa_completion_wait (struct vfio_pci_device *device, +struct dsa_completion_record *completion) +{ +u8 status; + +for (;;) { +dsa_check_sw_err (device);
Quoted 8 lines
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c line 253
Quoted 6 lines
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +6
-0+ + status =READ_ONCE (completion->status); +if (status) +break ; + +usleep (1000 );Another minor/thing to think about: using umonitor/umwait.
Thanks for the tip, I hadn't considered that. But I think for this driver, keeping things as simple as possible is best. This code is only used for testing so I don't think we care enough about efficiency to justify using unmonitor/umwait here.
Vinicius Costa Gomes
maintainerRe: [PATCH 22/33] vfio: selftests: Add driver for Intel DSA
Quoted 11 lines · David Matlack
Quoted 8 lines · Vinicius Costa Gomes
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c line 69
Quoted 5 lines · David Matlack
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +5
-0+ +static int dsa_probe (struct vfio_pci_device *device) +{ +if (!vfio_pci_device_match (device,PCI_VENDOR_ID_INTEL , +PCI_DEVICE_ID_INTEL_DSA_SPR0 ))What are you thinking about adding support for multiple device ids?
Quoted 4 lines
I haven't given it much thought yet. But we could definitely support fancier device matching (e.g. multiple acceptable device ids) if/when a use-case for that arises.
Fair enough. I just wanted to bring this up (hoping that's more a matter of "when" than "if" :-))
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +7
-0+static int dsa_completion_wait (struct vfio_pci_device *device, +struct dsa_completion_record *completion) +{ +u8 status; + +for (;;) { +dsa_check_sw_err (device);Quoted 8 lines
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c line 253
Quoted 6 lines
tools/testing/selftests/vfio/lib/drivers/dsa/dsa.c new file +6
-0+ + status =READ_ONCE (completion->status); +if (status) +break ; + +usleep (1000 );Another minor/thing to think about: using umonitor/umwait.
Quoted 4 lines
Thanks for the tip, I hadn't considered that. But I think for this driver, keeping things as simple as possible is best. This code is only used for testing so I don't think we care enough about efficiency to justify using unmonitor/umwait here.
Yeah, agreed.
Cheers,
Signature · 1 line
Vinicius
David Matlack
maintainerPATCH 23/33 vfio: selftests: Move helper to get cdev path to libvfio
In reply to David MatlackMove the helper function to get the VFIO cdev path to libvfio so that it can be used in libvfio in a subsequent commit.
No functional change intended.
--- .../selftests/vfio/lib/include/vfio_util.h | 1 + .../selftests/vfio/lib/vfio_pci_device.c | 31 +++++++++++++++++ .../selftests/vfio/vfio_iommufd_setup_test.c | 34 ++----------------- 3 files changed, 34 insertions(+), 32 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +1 -0
@@ -175,6 +175,7 @@ struct vfio_pci_device {* If BDF cannot be determined then the test will exit with KSFT_SKIP. */ const char *vfio_selftests_get_bdf (int *argc,char *argv[]); +const char *vfio_pci_get_cdev_path (const char *bdf);struct vfio_pci_device *vfio_pci_device_init (const char *bdf,int iommu_type);void vfio_pci_device_cleanup (struct vfio_pci_device *device);
tools/testing/selftests/vfio/lib/vfio_pci_device.c +31 -0
@@ -1,4 +1,5 @@// SPDX-License-Identifier: GPL-2.0-only +#include <dirent.h> #include <fcntl.h> #include <libgen.h> #include <stdlib.h>
@@ -332,6 +333,36 @@ static void vfio_pci_device_setup(struct vfio_pci_device *device, const char *bd device->msi_eventfds[i] =-1 ; } +const char *vfio_pci_get_cdev_path (const char *bdf) +{ +char dir_path[PATH_MAX ]; +struct dirent *entry; +char *cdev_path; +DIR *dir; + + cdev_path =calloc (PATH_MAX ,1 ); +VFIO_ASSERT_NOT_NULL (cdev_path); + +snprintf (dir_path,sizeof (dir_path),"/sys/bus/pci/devices/%s/vfio-dev/" , bdf); + + dir =opendir (dir_path); +VFIO_ASSERT_NOT_NULL (dir,"Failed to open directory %s\n" , dir_path); + +while ((entry =readdir (dir)) !=NULL ) { +/* Find the file that starts with "vfio" */ +if (strncmp ("vfio" , entry->d_name,4 )) +continue ; + +snprintf (cdev_path,PATH_MAX ,"/dev/vfio/devices/%s" , entry->d_name); +break ; + } + +VFIO_ASSERT_NE (cdev_path[0 ],0 ,"Failed to find vfio cdev file.\n" ); +VFIO_ASSERT_EQ (closedir (dir),0 ); + +return cdev_path; +} +struct vfio_pci_device *vfio_pci_device_init (const char *bdf,int iommu_type) {struct vfio_pci_device *device;
tools/testing/selftests/vfio/vfio_iommufd_setup_test.c +2 -32
@@ -1,8 +1,4 @@// SPDX-License-Identifier: GPL-2.0 -#include <assert.h> -#include <dirent.h> -#include <fcntl.h> -#include <uapi/linux/types.h> #include <linux/limits.h> #include <linux/sizes.h>
@@ -11,7 +7,6 @@#include <stdint.h> #include <stdio.h> -#include <string.h> #include <sys/ioctl.h> #include <unistd.h>
@@ -19,32 +14,7 @@#include "../kselftest_harness.h" static const char iommu_dev_path[] ="/dev/iommu" ; -static char cdev_path[PATH_MAX ] = {'\0' }; - -static void set_cdev_path (const char *bdf) -{ -char dir_path[PATH_MAX ]; -DIR *dir; -struct dirent *entry; - -snprintf (dir_path,sizeof (dir_path),"/sys/bus/pci/devices/%s/vfio-dev/" , bdf); - - dir =opendir (dir_path); -assert (dir); - -/* Find the file named "vfio<number>" */ -while ((entry =readdir (dir)) !=NULL ) { -if (!strncmp ("vfio" , entry->d_name,4 )) { -snprintf (cdev_path,sizeof (cdev_path),"/dev/vfio/devices/%s" , - entry->d_name); -break ; - } - } - -assert (strlen (cdev_path) >0 ); - -closedir (dir); -} +static const char *cdev_path;static int vfio_device_bind_iommufd_ioctl (int cdev_fd,int iommufd) {
@@ -150,7 +120,7 @@ int main(int argc, char *argv[]) {const char *device_bdf =vfio_selftests_get_bdf (&argc, argv); -set_cdev_path (device_bdf); + cdev_path =vfio_pci_get_cdev_path (device_bdf);printf ("Using cdev device %s\n" , cdev_path);return test_harness_run (argc, argv);
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 24/33 vfio: selftests: Encapsulate IOMMU mode
In reply to David MatlackEncapsulate the "IOMMU mode" a test should use behind a new struct. In the future this will be used to support other types of IOMMUs besides VFIO_TYPE1_IOMMU, and allow users to select the mode on the command line.
No functional change intended.
--- .../selftests/vfio/lib/include/vfio_util.h | 12 +++++- .../selftests/vfio/lib/vfio_pci_device.c | 42 ++++++++++++++++--- .../selftests/vfio/vfio_dma_mapping_test.c | 2 +- .../selftests/vfio/vfio_pci_device_test.c | 4 +- .../selftests/vfio/vfio_pci_driver_test.c | 4 +- 5 files changed, 52 insertions(+), 12 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +11 -1
@@ -47,6 +47,12 @@ VFIO_LOG_AND_EXIT(_fmt, ##__VA_ARGS__); \ }while (0 ) +struct vfio_iommu_mode { +const char *name; +const char *container_path; +unsigned long iommu_type; +}; +struct vfio_pci_bar {struct vfio_region_info info;void *vaddr;
@@ -144,6 +150,8 @@ struct vfio_pci_driver {struct vfio_pci_device {int fd; + +const struct vfio_iommu_mode *iommu_mode;int group_fd;int container_fd;
@@ -177,7 +185,9 @@ struct vfio_pci_device {const char *vfio_selftests_get_bdf (int *argc,char *argv[]);const char *vfio_pci_get_cdev_path (const char *bdf); -struct vfio_pci_device *vfio_pci_device_init (const char *bdf,int iommu_type); +extern const char *default_iommu_mode; + +struct vfio_pci_device *vfio_pci_device_init (const char *bdf,const char *iommu_mode);void vfio_pci_device_cleanup (struct vfio_pci_device *device);void vfio_pci_device_reset (struct vfio_pci_device *device);
tools/testing/selftests/vfio/lib/vfio_pci_device.c +36 -6
77 lines not on this page · raw message
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +1 -1
@@ -116,7 +116,7 @@ FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous_hugetlb_1gb) {FIXTURE_SETUP (vfio_dma_mapping_test) { - self->device =vfio_pci_device_init (device_bdf,VFIO_TYPE1_IOMMU ); + self->device =vfio_pci_device_init (device_bdf, default_iommu_mode); }FIXTURE_TEARDOWN (vfio_dma_mapping_test)
tools/testing/selftests/vfio/vfio_pci_device_test.c +2 -2
@@ -28,7 +28,7 @@ FIXTURE(vfio_pci_device_test) {FIXTURE_SETUP (vfio_pci_device_test) { - self->device =vfio_pci_device_init (device_bdf,VFIO_TYPE1_IOMMU ); + self->device =vfio_pci_device_init (device_bdf, default_iommu_mode); }FIXTURE_TEARDOWN (vfio_pci_device_test)
@@ -116,7 +116,7 @@ FIXTURE_VARIANT_ADD(vfio_pci_irq_test, msix) {FIXTURE_SETUP (vfio_pci_irq_test) { - self->device =vfio_pci_device_init (device_bdf,VFIO_TYPE1_IOMMU ); + self->device =vfio_pci_device_init (device_bdf, default_iommu_mode); }FIXTURE_TEARDOWN (vfio_pci_irq_test)
tools/testing/selftests/vfio/vfio_pci_driver_test.c +2 -2
@@ -63,7 +63,7 @@ FIXTURE_SETUP(vfio_pci_driver_test) {struct vfio_pci_driver *driver; - self->device =vfio_pci_device_init (device_bdf,VFIO_TYPE1_IOMMU ); + self->device =vfio_pci_device_init (device_bdf, default_iommu_mode); driver = &self->device->driver;
@@ -225,7 +225,7 @@ int main(int argc, char *argv[]) device_bdf =vfio_selftests_get_bdf (&argc, argv); - device =vfio_pci_device_init (device_bdf,VFIO_TYPE1_IOMMU ); + device =vfio_pci_device_init (device_bdf, default_iommu_mode);if (!device->driver.ops) {fprintf (stderr,"No driver found for device %s\n" , device_bdf);return KSFT_SKIP ;
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 25/33 vfio: selftests: Replicate tests across all iommu_modes
In reply to David MatlackAutomatically replicate vfio_dma_mapping_test and vfio_pci_driver_test across all supported IOMMU modes using fixture variants. Both of these tests exercise DMA mapping to some degree so having automatic coverage across all IOMMU modes will help catch bugs.
--- .../selftests/vfio/lib/include/vfio_util.h | 8 +++++++ .../selftests/vfio/lib/vfio_pci_device.c | 1 + .../selftests/vfio/vfio_dma_mapping_test.c | 24 +++++++++---------- .../selftests/vfio/vfio_pci_driver_test.c | 13 +++++++++- 4 files changed, 32 insertions(+), 14 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +8 -0
@@ -53,6 +53,14 @@ struct vfio_iommu_mode {unsigned long iommu_type; }; +/* +* Generator for VFIO selftests fixture variants that replicate across all +* possible IOMMU modes. Tests must define FIXTURE_VARIANT_ADD_IOMMU_MODE() +* which should then use FIXTURE_VARIANT_ADD() to create the variant. +*/ +#define FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (...) \ +FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1_iommu, ##__VA_ARGS__) +struct vfio_pci_bar {struct vfio_region_info info;void *vaddr;
tools/testing/selftests/vfio/lib/vfio_pci_device.c +1 -0
@@ -364,6 +364,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)return cdev_path; } +/* Reminder: Keep in sync with FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(). */ static const struct vfio_iommu_mode iommu_modes[] = { { .name ="vfio_type1_iommu" ,
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +11 -13
@@ -96,27 +96,25 @@ FIXTURE(vfio_dma_mapping_test) { };FIXTURE_VARIANT (vfio_dma_mapping_test) { +const char *iommu_mode;u64 size;int mmap_flags; }; -FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous) { - .mmap_flags =MAP_ANONYMOUS |MAP_PRIVATE , -}; - -FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous_hugetlb_2mb) { - .size =SZ_2M , - .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB | MAP_HUGE_2MB, -}; +#define FIXTURE_VARIANT_ADD_IOMMU_MODE (_iommu_mode, _name, _size, _mmap_flags) \ +FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, _iommu_mode ## _ ## _name) { \ + .iommu_mode = #_iommu_mode, \ + .size = (_size), \ + .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | (_mmap_flags), \ +} -FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous_hugetlb_1gb) { - .size =SZ_1G , - .mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB | MAP_HUGE_1GB, -}; +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (anonymous,0 ,0 ); +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (anonymous_hugetlb_2mb,SZ_2M ,MAP_HUGETLB |MAP_HUGE_2MB ); +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (anonymous_hugetlb_1gb,SZ_1G ,MAP_HUGETLB |MAP_HUGE_1GB );FIXTURE_SETUP (vfio_dma_mapping_test) { - self->device =vfio_pci_device_init (device_bdf, default_iommu_mode); + self->device =vfio_pci_device_init (device_bdf, variant->iommu_mode); }FIXTURE_TEARDOWN (vfio_dma_mapping_test)
tools/testing/selftests/vfio/vfio_pci_driver_test.c +12 -1
@@ -59,11 +59,22 @@ FIXTURE(vfio_pci_driver_test) {iova_t unmapped_iova; }; +FIXTURE_VARIANT (vfio_pci_driver_test) { +const char *iommu_mode; +}; + +#define FIXTURE_VARIANT_ADD_IOMMU_MODE (_iommu_mode) \ +FIXTURE_VARIANT_ADD(vfio_pci_driver_test, _iommu_mode) { \ + .iommu_mode = #_iommu_mode, \ +} + +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (); +FIXTURE_SETUP (vfio_pci_driver_test) {struct vfio_pci_driver *driver; - self->device =vfio_pci_device_init (device_bdf, default_iommu_mode); + self->device =vfio_pci_device_init (device_bdf, variant->iommu_mode); driver = &self->device->driver;
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 26/33 vfio: selftests: Add vfio_type1v2_mode
In reply to David MatlackAdd a new IOMMU mode for using VFIO_TYPE1v2_IOMMU.
--- tools/testing/selftests/vfio/lib/include/vfio_util.h | 3 ++- tools/testing/selftests/vfio/lib/vfio_pci_device.c | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +2 -1
@@ -59,7 +59,8 @@ struct vfio_iommu_mode {* which should then use FIXTURE_VARIANT_ADD() to create the variant. */ #define FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (...) \ -FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1_iommu, ##__VA_ARGS__) +FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1_iommu, ##__VA_ARGS__); \ +FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__)struct vfio_pci_bar {struct vfio_region_info info;
tools/testing/selftests/vfio/lib/vfio_pci_device.c +5 -0
@@ -371,6 +371,11 @@ static const struct vfio_iommu_mode iommu_modes[] = { .container_path = "/dev/vfio/vfio", .iommu_type = VFIO_TYPE1_IOMMU, }, + { + .name = "vfio_type1v2_iommu", + .container_path = "/dev/vfio/vfio", + .iommu_type = VFIO_TYPE1v2_IOMMU, + }, };const char *default_iommu_mode ="vfio_type1_iommu" ;
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 27/33 vfio: selftests: Add iommufd_compat_type1{,v2} modes
In reply to David MatlackAdd new IOMMU modes for using iommufd in compatibility mode with VFIO_TYPE1_IOMMU and VFIO_TYPE1v2_IOMMU.
In these modes, VFIO selftests will open /dev/iommu and treats it as a container FD (as if it had opened /dev/vfio/vfio) and the kernel translates the container ioctls to iommufd calls transparently.
--- tools/testing/selftests/vfio/lib/include/vfio_util.h | 4 +++- tools/testing/selftests/vfio/lib/vfio_pci_device.c | 10 ++++++++++ tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 12 ++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +3 -1
@@ -60,7 +60,9 @@ struct vfio_iommu_mode {*/ #define FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES (...) \ FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1_iommu, ##__VA_ARGS__); \ -FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__) +FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__); \ +FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1, ##__VA_ARGS__); \ +FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd_compat_type1v2, ##__VA_ARGS__)struct vfio_pci_bar {struct vfio_region_info info;
tools/testing/selftests/vfio/lib/vfio_pci_device.c +10 -0
@@ -376,6 +376,16 @@ static const struct vfio_iommu_mode iommu_modes[] = { .container_path = "/dev/vfio/vfio", .iommu_type = VFIO_TYPE1v2_IOMMU, }, + { + .name = "iommufd_compat_type1", + .container_path = "/dev/iommu", + .iommu_type = VFIO_TYPE1_IOMMU, + }, + { + .name = "iommufd_compat_type1v2", + .container_path = "/dev/iommu", + .iommu_type = VFIO_TYPE1v2_IOMMU, + }, };const char *default_iommu_mode ="vfio_type1_iommu" ;
tools/testing/selftests/vfio/vfio_dma_mapping_test.c +10 -2
@@ -128,6 +128,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)const int flags = variant->mmap_flags;struct vfio_dma_region region;struct iommu_mapping mapping; +u64 mapping_size = size;const u64 iova =0 ;int rc;
@@ -150,6 +151,13 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)if (rc == -EOPNOTSUPP ) goto unmap; +/* +* IOMMUFD compatibility-mode does not support huge mappings when +* using VFIO_TYPE1_IOMMU. +*/ +if (!strcmp (variant->iommu_mode,"iommufd_compat_type1" )) + mapping_size =SZ_4K ; +ASSERT_EQ (0 , rc);printf ("Found IOMMU mappings for IOVA 0x%lx:\n" , iova);printf ("PGD: 0x%016lx\n" , mapping.pgd);
@@ -158,7 +166,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)printf ("PMD: 0x%016lx\n" , mapping.pmd);printf ("PTE: 0x%016lx\n" , mapping.pte); -switch (size) { +switch (mapping_size) {case SZ_4K :ASSERT_NE (0 , mapping.pte);break ;
@@ -172,7 +180,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)ASSERT_NE (0 , mapping.pud);break ;default : -VFIO_FAIL ("Unrecognized size: 0x%lx\n" , size); +VFIO_FAIL ("Unrecognized size: 0x%lx\n" , mapping_size); } unmap:
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 28/33 vfio: selftests: Add iommufd mode
In reply to David MatlackAdd a new IOMMU mode for using iommufd directly. In this mode userspace opens /dev/iommu and binds it to a device FD acquired through /dev/vfio/devices/vfioX.
--- .../selftests/vfio/lib/include/vfio_util.h | 6 +- .../selftests/vfio/lib/vfio_pci_device.c | 175 ++++++++++++++---- 2 files changed, 146 insertions(+), 35 deletions(-)
tools/testing/selftests/vfio/lib/include/vfio_util.h +5 -1
@@ -62,7 +62,8 @@ struct vfio_iommu_mode { FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1_iommu, ##__VA_ARGS__); \ FIXTURE_VARIANT_ADD_IOMMU_MODE(vfio_type1v2_iommu, ##__VA_ARGS__); \ 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_compat_type1v2, ##__VA_ARGS__); \ +FIXTURE_VARIANT_ADD_IOMMU_MODE(iommufd, ##__VA_ARGS__)struct vfio_pci_bar {struct vfio_region_info info;
@@ -166,6 +167,9 @@ struct vfio_pci_device {int group_fd;int container_fd; +int iommufd; +u32 ioas_id; +struct vfio_device_info info;struct vfio_region_info config_space;struct vfio_pci_bar bars[PCI_STD_NUM_BARS ];
tools/testing/selftests/vfio/lib/vfio_pci_device.c +141 -34
260 lines not on this page · raw message
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 29/33 vfio: selftests: Make iommufd the default iommu_mode
In reply to David MatlackNow that VFIO selftests support iommufd, make it the default mode. IOMMUFD is the successor to VFIO_TYPE1{,v2}_IOMMU and all new features are being added there, so it's a slightly better fit as the default mode.
--- tools/testing/selftests/vfio/lib/vfio_pci_device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/vfio/lib/vfio_pci_device.c +1 -1
@@ -437,7 +437,7 @@ static const struct vfio_iommu_mode iommu_modes[] = { }, }; -const char *default_iommu_mode ="vfio_type1_iommu" ; +const char *default_iommu_mode ="iommufd" ;static const struct vfio_iommu_mode *lookup_iommu_mode (const char *iommu_mode) {
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 30/33 vfio: selftests: Add a script to help with running VFIO selftests
In reply to David MatlackIntroduce run.sh, a script to help with running VFIO selftests. The script is intended to be used for both humans manually running VFIO selftests, and to incorporate into test automation where VFIO selftests may run alongside other tests. As such the script aims to be hermetic, returning the system to the state it was before the test started.
The script takes as input the BDF of a device to use and a command to run (typically the command would be a VFIO selftest). e.g.
$ ./run.sh -d 0000:6a:01.0 ./vfio_pci_device_test
or
$ ./run.sh -d 0000:6a:01.0 -- ./vfio_pci_device_test
The script then handles unbinding device 0000:6a:01.0 from its current driver, binding it to vfio-pci, running the test, unbinding from vfio-pci, and binding back to the original driver.
When run.sh runs the provided test, it does so by appending the BDF as the last parameter. For example:
$ ./run.sh -d 0000:6a:01.0 -- echo hello
Results in the following being printed to stdout:
hello 0000:6a:01.0
The script also supports a mode where it can break out into a shell so that multiple tests can be run manually.
$ ./run.sh -d 0000:6a:01.0 -s $ echo $VFIO_SELFTESTS_BDF $ ./vfio_pci_device_test $ exit
Choosing which device to use is up to the user.
In the future this script should be extensible to tests that want to use multiple devices. The script can support accepting -d BDF multiple times and parse them into an array, setup all the devices, pass the list of BDFs to the test, and then cleanup all the devices.
--- tools/testing/selftests/vfio/Makefile | 1 + tools/testing/selftests/vfio/run.sh | 109 ++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100755 tools/testing/selftests/vfio/run.sh
tools/testing/selftests/vfio/Makefile +1 -0
@@ -3,6 +3,7 @@ TEST_GEN_PROGS += vfio_dma_mapping_test TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_driver_test +TEST_PROGS_EXTENDED := run.sh include ../lib.mk include lib/libvfio.mk
tools/testing/selftests/vfio/run.sh new file +109 -0
109 lines not on this page · raw message
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 31/33 KVM: selftests: Build and link sefltests/vfio/lib into KVM selftests
In reply to David MatlackInclude libvfio.mk into the KVM selftests Makefile and link it into all KVM selftests by adding it to LIBKVM_OBJS.
Note that KVM selftests build their own copy of sefltests/vfio/lib and the resulting object files are placed in $(OUTPUT)/lib. This allows the KVM and VFIO selftests to apply different CFLAGS when building without conflicting with each other.
--- tools/testing/selftests/kvm/Makefile.kvm | 3 +++ 1 file changed, 3 insertions(+)
tools/testing/selftests/kvm/Makefile.kvm +3 -0
@@ -274,12 +274,15 @@ pgste-option = $(call try-run, echo 'int main(void) { return 0; }' | \ LDLIBS += -ldl LDFLAGS += -pthread $(no-pie-option) $(pgste-option) +include ../vfio/lib/libvfio.mk + LIBKVM_C := $(filter %.c,$(LIBKVM)) LIBKVM_S := $(filter %.S,$(LIBKVM)) LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C)) LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S)) LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING)) LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) $(LIBCGROUP_O) +LIBKVM_OBJS += $(LIBVFIO_O) SPLIT_TEST_GEN_PROGS := $(patsubst %, $(OUTPUT)/%, $(SPLIT_TESTS)) SPLIT_TEST_GEN_OBJ := $(patsubst %, $(OUTPUT)/$(SRCARCH)/%.o, $(SPLIT_TESTS))
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 32/33 KVM: selftests: Test sending a vfio-pci device IRQ to a VM
In reply to David MatlackAdd a new selftest called vfio_pci_device_irq_test that routes and delivers an MSI from a vfio-pci device into a guest.
Note that this test currently uses vfio_pci_irq_trigger(), in which VFIO emulates a device sending the MSI. In the future we want to replace this with a real MSI so we can test the hardware path (e.g. VT-d on Intel).
This test only supports x86_64 for now, but can be ported to other architectures in the future.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../testing/selftests/kvm/include/kvm_util.h | 4 + tools/testing/selftests/kvm/lib/kvm_util.c | 21 +++ .../selftests/kvm/vfio_pci_device_irq_test.c | 123 ++++++++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 tools/testing/selftests/kvm/vfio_pci_device_irq_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -146,6 +146,7 @@ TEST_GEN_PROGS_x86 += rseq_test TEST_GEN_PROGS_x86 += steal_time TEST_GEN_PROGS_x86 += system_counter_offset_test TEST_GEN_PROGS_x86 += pre_fault_memory_test +TEST_GEN_PROGS_x86 += vfio_pci_device_irq_test # Compiled outputs used by test targets TEST_GEN_PROGS_EXTENDED_x86 += x86/nx_huge_pages_test
tools/testing/selftests/kvm/include/kvm_util.h +4 -0
@@ -907,6 +907,8 @@ void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...);void kvm_irq_line (struct kvm_vm *vm,uint32_t irq,int level);int _kvm_irq_line (struct kvm_vm *vm,uint32_t irq,int level); +void kvm_add_irqfd (struct kvm_vm *vm,u32 gsi,int fd); +#define KVM_MAX_IRQ_ROUTES 4096struct kvm_irq_routing *kvm_gsi_routing_create (void );
@@ -915,6 +917,8 @@ void kvm_gsi_routing_irqchip_add(struct kvm_irq_routing *routing,int _kvm_gsi_routing_write (struct kvm_vm *vm,struct kvm_irq_routing *routing);void kvm_gsi_routing_write (struct kvm_vm *vm,struct kvm_irq_routing *routing); +void kvm_route_gsi (struct kvm_vm *vm,struct kvm_irq_routing_entry *entry); +const char *exit_reason_str (unsigned int exit_reason); vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
tools/testing/selftests/kvm/lib/kvm_util.c +21 -0
@@ -1891,6 +1891,16 @@ void kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level)TEST_ASSERT (ret >=0 ,KVM_IOCTL_ERROR (KVM_IRQ_LINE , ret)); } +void kvm_add_irqfd (struct kvm_vm *vm,u32 gsi,int fd) +{ +struct kvm_irqfd arg = { + .gsi = gsi, + .fd = fd, + }; + +vm_ioctl (vm,KVM_IRQFD , &arg); +} +struct kvm_irq_routing *kvm_gsi_routing_create (void ) {struct kvm_irq_routing *routing;
@@ -1941,6 +1951,17 @@ void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing)TEST_ASSERT (!ret,KVM_IOCTL_ERROR (KVM_SET_GSI_ROUTING , ret)); } +void kvm_route_gsi (struct kvm_vm *vm,struct kvm_irq_routing_entry *entry) +{ +u8 buf[sizeof (struct kvm_irq_routing ) +sizeof (*entry)] = {}; +struct kvm_irq_routing *routes = (void *)&buf; + + routes->nr =1 ; + routes->entries[0 ] = *entry; + +vm_ioctl (vm,KVM_SET_GSI_ROUTING , routes); +} +/* * VM Dump *
tools/testing/selftests/kvm/vfio_pci_device_irq_test.c new file +123 -0
123 lines not on this page · raw message
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerPATCH 33/33 KVM: selftests: Add -d option to vfio_pci_device_irq_test for device-sent MSIs
In reply to David MatlackAdd the -d option to vfio_pci_device_irq_test that will make the device send an MSI rather than synthesizing an eventfd notification from VFIO. This requires a VFIO selftest driver for the device that supports the send_msi() function.
This option allows the test to exercise IRQ Bypass (e.g. VT-d device-posted interrupts in Intel).
--- .../selftests/kvm/vfio_pci_device_irq_test.c | 61 +++++++++++++++++-- 1 file changed, 55 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/vfio_pci_device_irq_test.c +55 -6
@@ -7,6 +7,8 @@#include <pthread.h> #include <time.h> #include <linux/vfio.h> +#include <linux/sizes.h> +#include <vfio_util.h> static bool guest_ready_for_irq;
@@ -60,10 +62,53 @@ void *vcpu_thread_main(void *arg)static void help (const char *name) { -printf ("Usage: %s [-i iommu_mode] [segment:bus:device.function]\n" , name); +printf ("Usage: %s [-i iommu_mode] [-d] [segment:bus:device.function]\n" , name); +printf (" -d: Send a real MSI from the device, rather than synthesizing\n" +" an eventfd signal from VFIO. Note that this option requires\n" +" a VFIO selftests driver that supports the device.\n" );exit (KSFT_FAIL ); } +static int setup_msi (struct vfio_pci_device *device,bool use_device_msi) +{ +const int flags =MAP_SHARED |MAP_ANONYMOUS ; +const int prot =PROT_READ |PROT_WRITE ; +struct vfio_dma_region *region; + +if (use_device_msi) { +/* A driver is required to generate an MSI. */ +TEST_REQUIRE (device->driver.ops); + +/* Set up a DMA-able region for the driver to use. */ + region = &device->driver.region; + region->iova =0 ; + region->size =SZ_2M ; + region->vaddr =mmap (NULL , region->size, prot, flags,-1 ,0 ); +TEST_ASSERT (region->vaddr !=MAP_FAILED ,"mmap() failed\n" ); +vfio_pci_dma_map (device, region); + +vfio_pci_driver_init (device); + +return device->driver.msi; + } + +TEST_REQUIRE (device->msix_info.count >0 ); +vfio_pci_msix_enable (device,0 ,1 ); +return 0 ; +} + +static void send_msi (struct vfio_pci_device *device,bool use_device_msi,int msi) +{ +if (use_device_msi) { +printf ("Sending MSI %d from the device\n" , msi); +TEST_ASSERT_EQ (msi, device->driver.msi); +vfio_pci_driver_send_msi (device); + }else { +printf ("Notifying the eventfd for MSI %d from VFIO\n" , msi); +vfio_pci_irq_trigger (device,VFIO_PCI_MSIX_IRQ_INDEX , msi); + } +} +int main (int argc,char **argv) {/* Random non-reserved vector and GSI to use for the device IRQ */
@@ -73,19 +118,24 @@ int main(int argc, char **argv)struct timespec start, elapsed;struct vfio_pci_device *device;const char *iommu_mode =NULL ; +bool use_device_msi = false;const char *device_bdf;struct kvm_vcpu *vcpu;pthread_t vcpu_thread;struct kvm_vm *vm; +int msi;int c; device_bdf =vfio_selftests_get_bdf (&argc, argv); -while ((c =getopt (argc, argv,"i:" )) !=-1 ) { +while ((c =getopt (argc, argv,"i:d" )) !=-1 ) {switch (c) {case 'i' : iommu_mode = optarg;break ; +case 'd' : + use_device_msi = true; +break ;default :help (argv[0 ]); }
@@ -95,10 +145,9 @@ int main(int argc, char **argv)vm_install_exception_handler (vm, vector, guest_irq_handler); device =vfio_pci_device_init (device_bdf, iommu_mode); -TEST_REQUIRE (device->msix_info.count >0 ); + msi =setup_msi (device, use_device_msi); -vfio_pci_msix_enable (device,0 ,1 ); -kvm_add_irqfd (vm, gsi, device->msi_eventfds[0 ]); +kvm_add_irqfd (vm, gsi, device->msi_eventfds[msi]);kvm_route_msi (vm, gsi, vcpu, vector);pthread_create (&vcpu_thread,NULL , vcpu_thread_main, vcpu);
@@ -106,7 +155,7 @@ int main(int argc, char **argv)while (!READ_ONCE (guest_ready_for_irq))sync_global_from_guest (vm, guest_ready_for_irq); -vfio_pci_irq_trigger (device,VFIO_PCI_MSIX_IRQ_INDEX ,0 ); +send_msi (device, use_device_msi, msi);clock_gettime (CLOCK_MONOTONIC , &start);
Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
David Matlack
maintainerQuoted 3 lines · David Matlack
This series introduces VFIO selftests, located in
tools/testing/selftests/vfio/.
Hi Alex,
I wanted to discuss how you would like to proceed with this series.
The series is quite large, so one thing I was wondering is if you think it should be split up into separate series to make it easier to review and merge. Something like this:
- Patches 01-08 + 30 (VFIO selftests library, some basic tests, and run script)
- Patches 09-22 (driver framework)
- Patches 23-28 (iommufd support)
- Patches 31-33 (integration with KVM selftests)
I also was curious about your thoughts on maintenance of VFIO selftests, since I don't think we discussed that in the RFC. I am happy to help maintain VFIO selftests in whatever way makes the most sense. For now I added tools/testing/selftests/vfio under the top-level VFIO section in MAINTAINERS (so you would be the maintainer) and then also added a separate section for VFIO selftests with myself as a Reviewer (see PATCH 01). Reviewer felt like a better choice than Maintainer for myself since I am new to VFIO upstream (I've primarily worked on KVM in the past).
Thanks.
--David
Quoted 18 lines · On Fri, 25 Jul 2025 09:47:48 -0700 David Matlack <[email protected]> wrote:
Quoted 3 lines · David Matlack
This series introduces VFIO selftests, located in
tools/testing/selftests/vfio/.Hi Alex,
I wanted to discuss how you would like to proceed with this series.
The series is quite large, so one thing I was wondering is if you think it should be split up into separate series to make it easier to review and merge. Something like this:
- Patches 01-08 + 30 (VFIO selftests library, some basic tests, and run script)
- Patches 09-22 (driver framework)
- Patches 23-28 (iommufd support)
- Patches 31-33 (integration with KVM selftests)
Quoted 9 lines
I also was curious about your thoughts on maintenance of VFIO selftests, since I don't think we discussed that in the RFC. I am happy to help maintain VFIO selftests in whatever way makes the most sense. For now I added
tools/testing/selftests/vfiounder the top-level VFIO section inMAINTAINERS(so you would be the maintainer) and then also added a separate section for VFIO selftests with myself as a Reviewer (see PATCH 01). Reviewer felt like a better choice than Maintainer for myself since I am new to VFIO upstream (I've primarily worked on KVM in the past).
Hi David,
There's a lot of potential here and I'd like to see it proceed. I've got various unit tests that we could incorporate over time and obviously picking up Aaron's latency would be useful as well.
Something that we should continue to try to improve is the automation. These tests are often targeting a specific feature, so matching a device to a unit test becomes a barrier to automated runs. I wonder if we might be able to reach a point where the test runner can select appropriate devices from a pool of devices specified via environment variables.
An incremental approach like you're suggesting is usually the best course. Implement the framework and something basic, then build on it. 30+ patches is a bit much to chew on initially.
Your recommendation for MAINTAINERS sounds good to me. I'm not too familiar with the selftests, so I'll clearly be looking for your input once we've established the initial code. Thanks,
Alex
Quoted 31 lines · Alex Williamson
Quoted 18 lines · On Fri, 25 Jul 2025 09:47:48 -0700 David Matlack <[email protected]> wrote:
Quoted 3 lines · David Matlack
This series introduces VFIO selftests, located in
tools/testing/selftests/vfio/.Hi Alex,
I wanted to discuss how you would like to proceed with this series.
The series is quite large, so one thing I was wondering is if you think it should be split up into separate series to make it easier to review and merge. Something like this:
- Patches 01-08 + 30 (VFIO selftests library, some basic tests, and run script)
- Patches 09-22 (driver framework)
- Patches 23-28 (iommufd support)
- Patches 31-33 (integration with KVM selftests)Quoted 9 lines
I also was curious about your thoughts on maintenance of VFIO selftests, since I don't think we discussed that in the RFC. I am happy to help maintain VFIO selftests in whatever way makes the most sense. For now I added
tools/testing/selftests/vfiounder the top-level VFIO section inMAINTAINERS(so you would be the maintainer) and then also added a separate section for VFIO selftests with myself as a Reviewer (see PATCH 01). Reviewer felt like a better choice than Maintainer for myself since I am new to VFIO upstream (I've primarily worked on KVM in the past).
Quoted 3 lines
Hi David,
There's a lot of potential here and I'd like to see it proceed.
+1 too, I really lack time at the moment to do much with this but I'm half inclined to suggest Alex should say it should be merged in 6 weeks (to motivate any reviewing) and we can continue to work on it in-tree.
As they are self tests I think there is alot more value in having the tests than having perfect tests.
Quoted 6 lines
Something that we should continue to try to improve is the automation. These tests are often targeting a specific feature, so matching a device to a unit test becomes a barrier to automated runs. I wonder if we might be able to reach a point where the test runner can select appropriate devices from a pool of devices specified via environment variables.
Makes a lot of sense to me!
I'd just put Dave as the VFIO selftest co-maintainer though - a pennance for doing so much work :)
Jason
David Matlack
maintainerQuoted 18 lines · Jason Gunthorpe
Quoted 12 lines · Alex Williamson
Quoted 9 lines · On Fri, 25 Jul 2025 09:47:48 -0700 David Matlack <[email protected]> wrote:
I also was curious about your thoughts on maintenance of VFIO selftests, since I don't think we discussed that in the RFC. I am happy to help maintain VFIO selftests in whatever way makes the most sense. For now I added
tools/testing/selftests/vfiounder the top-level VFIO section inMAINTAINERS(so you would be the maintainer) and then also added a separate section for VFIO selftests with myself as a Reviewer (see PATCH 01). Reviewer felt like a better choice than Maintainer for myself since I am new to VFIO upstream (I've primarily worked on KVM in the past).Quoted 3 lines
Hi David,
There's a lot of potential here and I'd like to see it proceed.
Quoted 7 lines
+1 too, I really lack time at the moment to do much with this but I'm half inclined to suggest Alex should say it should be merged in 6 weeks (to motivate any reviewing) and we can continue to work on it in-tree.
As they are self tests I think there is alot more value in having the tests than having perfect tests.
They have been quite useful already within Google. Internally we have something almost identical to the RFC and have been using that for testing our 6.6-based kernel continuously since March. Already they have caught one (self-inflicted) regression where 1GiB HugeTLB pages started getting mapped with 2MiB mappings in the IOMMU, and have been very helpful with new development (e.g. Aaron's work, and Live Update support).
So I agree, it's probably net positive to merge early and then iterate in-tree. Especially since these are only tests and not e.g. load-bearing kernel code (although I still want to hold a high bar for the selftests code).
The only patches to hold off merging would be 31-33, since those should probably go through the KVM tree? And of course we need Acks for the drivers/dma/{ioat,idxd} changes, but the changes there are pretty minor.
Quoted 6 lines
Quoted 6 lines
Something that we should continue to try to improve is the automation. These tests are often targeting a specific feature, so matching a device to a unit test becomes a barrier to automated runs. I wonder if we might be able to reach a point where the test runner can select appropriate devices from a pool of devices specified via environment variables.
Yes, I'd like to improve on this as well. Within Google we've recently split up run.sh into separate setup.sh and cleanup.sh scripts, and now store metadata about what devices are set up for VFIO selftests in files. Storing metadata in files has been especially useful for testing Live Updates, since we need the automation to remember what devices are in use across a kexec.
For now we still pass in the BDFs to tests via command line, but we could have tests themselves look and see what devices are available for use and then pick one or multiple as needed. The location of the metadata files can have some reasonable default path and with an option to override with a custom path via environment variable.
There's lots of directions we could go in, and it really depends on how folks want to use/run VFIO selftests, so I would like to learn more about that as well.
Quoted 5 lines
Makes a lot of sense to me!
I'd just put Dave as the VFIO selftest co-maintainer though - a pennance for doing so much work :)
Hah! I will accept my penance if necessary. :)
Quoted 2 lines
Jason
David Matlack
maintainerQuoted 41 lines · David Matlack
Quoted 18 lines · Jason Gunthorpe
Quoted 12 lines · Alex Williamson
Quoted 9 lines · On Fri, 25 Jul 2025 09:47:48 -0700 David Matlack <[email protected]> wrote:
I also was curious about your thoughts on maintenance of VFIO selftests, since I don't think we discussed that in the RFC. I am happy to help maintain VFIO selftests in whatever way makes the most sense. For now I added
tools/testing/selftests/vfiounder the top-level VFIO section inMAINTAINERS(so you would be the maintainer) and then also added a separate section for VFIO selftests with myself as a Reviewer (see PATCH 01). Reviewer felt like a better choice than Maintainer for myself since I am new to VFIO upstream (I've primarily worked on KVM in the past).Quoted 3 lines
Hi David,
There's a lot of potential here and I'd like to see it proceed.
Quoted 7 lines
+1 too, I really lack time at the moment to do much with this but I'm half inclined to suggest Alex should say it should be merged in 6 weeks (to motivate any reviewing) and we can continue to work on it in-tree.
As they are self tests I think there is alot more value in having the tests than having perfect tests.
They have been quite useful already within Google. Internally we have something almost identical to the RFC and have been using that for testing our 6.6-based kernel continuously since March. Already they have caught one (self-inflicted) regression where 1GiB HugeTLB pages started getting mapped with 2MiB mappings in the IOMMU, and have been very helpful with new development (e.g. Aaron's work, and Live Update support).
So I agree, it's probably net positive to merge early and then iterate in-tree. Especially since these are only tests and not e.g. load-bearing kernel code (although I still want to hold a high bar for the selftests code).
Quoted 4 lines
The only patches to hold off merging would be 31-33, since those should probably go through the KVM tree? And of course we need Acks for the
drivers/dma/{ioat,idxd}changes, but the changes there are pretty minor.
Alex, how would you like to proceed?
Quoted 42 lines · On Mon, 18 Aug 2025 11:59:39 -0700 David Matlack <[email protected]> wrote:
Quoted 41 lines · David Matlack
Quoted 18 lines · Jason Gunthorpe
Quoted 12 lines · Alex Williamson
Quoted 9 lines · On Fri, 25 Jul 2025 09:47:48 -0700 David Matlack <[email protected]> wrote:
I also was curious about your thoughts on maintenance of VFIO selftests, since I don't think we discussed that in the RFC. I am happy to help maintain VFIO selftests in whatever way makes the most sense. For now I added
tools/testing/selftests/vfiounder the top-level VFIO section inMAINTAINERS(so you would be the maintainer) and then also added a separate section for VFIO selftests with myself as a Reviewer (see PATCH 01). Reviewer felt like a better choice than Maintainer for myself since I am new to VFIO upstream (I've primarily worked on KVM in the past).Quoted 3 lines
Hi David,
There's a lot of potential here and I'd like to see it proceed.
Quoted 7 lines
+1 too, I really lack time at the moment to do much with this but I'm half inclined to suggest Alex should say it should be merged in 6 weeks (to motivate any reviewing) and we can continue to work on it in-tree.
As they are self tests I think there is alot more value in having the tests than having perfect tests.
They have been quite useful already within Google. Internally we have something almost identical to the RFC and have been using that for testing our 6.6-based kernel continuously since March. Already they have caught one (self-inflicted) regression where 1GiB HugeTLB pages started getting mapped with 2MiB mappings in the IOMMU, and have been very helpful with new development (e.g. Aaron's work, and Live Update support).
So I agree, it's probably net positive to merge early and then iterate in-tree. Especially since these are only tests and not e.g. load-bearing kernel code (although I still want to hold a high bar for the selftests code).
Quoted 6 lines
Quoted 4 lines
The only patches to hold off merging would be 31-33, since those should probably go through the KVM tree? And of course we need Acks for the
drivers/dma/{ioat,idxd}changes, but the changes there are pretty minor.Alex, how would you like to proceed?
I think we need an ack from Shuah for the overall inclusion in tools/testing/selftests/
AFAICT the tools include files don't seem to have any central authority, so maybe we just need to chase those ioat/idxd acks, along with Shuah's and we can get this rolling and follow-up with the latter KVM patches once the base is merged. Thanks,
Alex
David Matlack
maintainerQuoted 53 lines · Alex Williamson
Quoted 42 lines · On Mon, 18 Aug 2025 11:59:39 -0700 David Matlack <[email protected]> wrote:
Quoted 41 lines · David Matlack
Quoted 18 lines · Jason Gunthorpe
Quoted 12 lines · Alex Williamson
Quoted 9 lines · On Fri, 25 Jul 2025 09:47:48 -0700 David Matlack <[email protected]> wrote:
I also was curious about your thoughts on maintenance of VFIO selftests, since I don't think we discussed that in the RFC. I am happy to help maintain VFIO selftests in whatever way makes the most sense. For now I added
tools/testing/selftests/vfiounder the top-level VFIO section inMAINTAINERS(so you would be the maintainer) and then also added a separate section for VFIO selftests with myself as a Reviewer (see PATCH 01). Reviewer felt like a better choice than Maintainer for myself since I am new to VFIO upstream (I've primarily worked on KVM in the past).Quoted 3 lines
Hi David,
There's a lot of potential here and I'd like to see it proceed.
Quoted 7 lines
+1 too, I really lack time at the moment to do much with this but I'm half inclined to suggest Alex should say it should be merged in 6 weeks (to motivate any reviewing) and we can continue to work on it in-tree.
As they are self tests I think there is alot more value in having the tests than having perfect tests.
They have been quite useful already within Google. Internally we have something almost identical to the RFC and have been using that for testing our 6.6-based kernel continuously since March. Already they have caught one (self-inflicted) regression where 1GiB HugeTLB pages started getting mapped with 2MiB mappings in the IOMMU, and have been very helpful with new development (e.g. Aaron's work, and Live Update support).
So I agree, it's probably net positive to merge early and then iterate in-tree. Especially since these are only tests and not e.g. load-bearing kernel code (although I still want to hold a high bar for the selftests code).
Quoted 6 lines
Quoted 4 lines
The only patches to hold off merging would be 31-33, since those should probably go through the KVM tree? And of course we need Acks for the
drivers/dma/{ioat,idxd}changes, but the changes there are pretty minor.Alex, how would you like to proceed?
Quoted 7 lines
I think we need an ack from Shuah for the overall inclusion in
tools/testing/selftests/AFAICT the tools include files don't seem to have any central authority, so maybe we just need to chase those ioat/idxd acks, along with Shuah's and we can get this rolling and follow-up with the latter KVM patches once the base is merged. Thanks,
Sounds good.
And yeah, I also don't see any maintainers listed for tools/include/ or tools/arch/x86/include/. Jason left some comments on the RFC that reduced the delta in v1, but that's the only feedback I've gotten so far there.
I will try emailing Shuah and the ioat/idxd maintainers directly as a next step, since it has been about 2 months since I posted this series and we haven't heard anything yet.
Thanks for the help.
Quoted 70 lines · On Mon, 18 Aug 2025 13:33:52 -0700 David Matlack <[email protected]> wrote:
Quoted 53 lines · Alex Williamson
Quoted 42 lines · On Mon, 18 Aug 2025 11:59:39 -0700 David Matlack <[email protected]> wrote:
Quoted 41 lines · David Matlack
Quoted 18 lines · Jason Gunthorpe
Quoted 12 lines · Alex Williamson
Quoted 9 lines · On Fri, 25 Jul 2025 09:47:48 -0700 David Matlack <[email protected]> wrote:
I also was curious about your thoughts on maintenance of VFIO selftests, since I don't think we discussed that in the RFC. I am happy to help maintain VFIO selftests in whatever way makes the most sense. For now I added
tools/testing/selftests/vfiounder the top-level VFIO section inMAINTAINERS(so you would be the maintainer) and then also added a separate section for VFIO selftests with myself as a Reviewer (see PATCH 01). Reviewer felt like a better choice than Maintainer for myself since I am new to VFIO upstream (I've primarily worked on KVM in the past).Quoted 3 lines
Hi David,
There's a lot of potential here and I'd like to see it proceed.
Quoted 7 lines
+1 too, I really lack time at the moment to do much with this but I'm half inclined to suggest Alex should say it should be merged in 6 weeks (to motivate any reviewing) and we can continue to work on it in-tree.
As they are self tests I think there is alot more value in having the tests than having perfect tests.
They have been quite useful already within Google. Internally we have something almost identical to the RFC and have been using that for testing our 6.6-based kernel continuously since March. Already they have caught one (self-inflicted) regression where 1GiB HugeTLB pages started getting mapped with 2MiB mappings in the IOMMU, and have been very helpful with new development (e.g. Aaron's work, and Live Update support).
So I agree, it's probably net positive to merge early and then iterate in-tree. Especially since these are only tests and not e.g. load-bearing kernel code (although I still want to hold a high bar for the selftests code).
Quoted 6 lines
Quoted 4 lines
The only patches to hold off merging would be 31-33, since those should probably go through the KVM tree? And of course we need Acks for the
drivers/dma/{ioat,idxd}changes, but the changes there are pretty minor.Alex, how would you like to proceed?
Quoted 7 lines
I think we need an ack from Shuah for the overall inclusion in
tools/testing/selftests/AFAICT the tools include files don't seem to have any central authority, so maybe we just need to chase those ioat/idxd acks, along with Shuah's and we can get this rolling and follow-up with the latter KVM patches once the base is merged. Thanks,
Sounds good.
And yeah, I also don't see any maintainers listed for
tools/include/ortools/arch/x86/include/. Jason left some comments on the RFC that reduced the delta in v1, but that's the only feedback I've gotten so far there.
Quoted 5 lines
I will try emailing Shuah and the ioat/idxd maintainers directly as a next step, since it has been about 2 months since I posted this series and we haven't heard anything yet.
Thanks for the help.
I think we have all the required acks now and reviews just suggest some minor patch shuffling, right?. You were also going to switch from reviewer to maintainer of the selftests in MAINTAINERS ;)
Are you planning to collect those acks, add the minor changes, drop the trailing KVM changes to come in through the existing kvm selftests and repost?
With KVM Forum coming up, I'd like to try to get this squared away and into the vfio next branch by next week. Thanks,
Alex
David Matlack
maintainerQuoted 11 lines · Alex Williamson
I think we have all the required acks now and reviews just suggest some minor patch shuffling, right?. You were also going to switch from reviewer to maintainer of the selftests in
MAINTAINERS;)Are you planning to collect those acks, add the minor changes, drop the trailing KVM changes to come in through the existing kvm selftests and repost?
With KVM Forum coming up, I'd like to try to get this squared away and into the vfio next branch by next week. Thanks,
Heh, I was just going to send a similar email to you :)
That plan sounds good to me. I will prep a new series with those changes and send it out at the beginning of next week (fingers crossed).
Thanks.
Quoted 50 lines · Alex Williamson
Quoted 47 lines · On Mon, 18 Aug 2025 11:59:39 -0700 David Matlack <[email protected]> wrote:
Quoted 45 lines · David Matlack
Quoted 18 lines · Jason Gunthorpe
Quoted 12 lines · Alex Williamson
Quoted 9 lines · On Fri, 25 Jul 2025 09:47:48 -0700 David Matlack <[email protected]> wrote:
I also was curious about your thoughts on maintenance of VFIO selftests, since I don't think we discussed that in the RFC. I am happy to help maintain VFIO selftests in whatever way makes the most sense. For now I added
tools/testing/selftests/vfiounder the top-level VFIO section inMAINTAINERS(so you would be the maintainer) and then also added a separate section for VFIO selftests with myself as a Reviewer (see PATCH 01). Reviewer felt like a better choice than Maintainer for myself since I am new to VFIO upstream (I've primarily worked on KVM in the past).Quoted 3 lines
Hi David,
There's a lot of potential here and I'd like to see it proceed.
Quoted 7 lines
+1 too, I really lack time at the moment to do much with this but I'm half inclined to suggest Alex should say it should be merged in 6 weeks (to motivate any reviewing) and we can continue to work on it in-tree.
As they are self tests I think there is alot more value in having the tests than having perfect tests.
They have been quite useful already within Google. Internally we have something almost identical to the RFC and have been using that for testing our 6.6-based kernel continuously since March. Already they have caught one (self-inflicted) regression where 1GiB HugeTLB pages started getting mapped with 2MiB mappings in the IOMMU, and have been very helpful with new development (e.g. Aaron's work, and Live Update support).
So I agree, it's probably net positive to merge early and then iterate in-tree. Especially since these are only tests and not e.g. load-bearing kernel code (although I still want to hold a high bar for the selftests code).
The only patches to hold off merging would be 31-33, since those should probably go through the KVM tree? And of course we need Acks for the
drivers/dma/{ioat,idxd}changes, but the changes there are pretty minor.
Quoted 4 lines
Quoted 1 line
Alex, how would you like to proceed?
I think we need an ack from Shuah for the overall inclusion in
tools/testing/selftests/
Glad to see these test in the kselftest suite.
Quoted 6 lines
AFAICT the tools include files don't seem to have any central authority, so maybe we just need to chase those ioat/idxd acks, along with Shuah's and we can get this rolling and follow-up with the latter KVM patches once the base is merged. Thanks,
thanks,
-- Shuah
Quoted 2 lines · David Matlack
This series introduces VFIO selftests, located in
tools/testing/selftests/vfio/.
Sorry for coming late to the party. Only recently got some cycles to go through this. This seems very similar to what we are trying to do with iommutests [3].
Quoted 6 lines
VFIO selftests aim to enable kernel developers to write and run tests that take the form of userspace programs that interact with VFIO and IOMMUFD uAPIs. VFIO selftests can be used to write functional tests for new features, regression tests for bugs, and performance tests for optimizations.
Have you considered implementing something outside the kernel sources? Something similar to fstests [1] or blktests [2]?
I ask because I have always seen it as a suit that tests (regression and performance) the Linux kernel as well as the hardware. By hardware I mean IOMMU devices, peripherals as well as their QEMU implementations. Since the scope is quite big, seemed (to me) like a good idea to keep it out of the kernel sources.
Can you speak to the pros/cons of having it in selftests? (sorry if this was already answered)
Quoted 5 lines
These tests are designed to interact with real PCI devices, i.e. they do not rely on mocking out or faking any behavior in the kernel. This allows the tests to exercise not only VFIO but also IOMMUFD, the IOMMU driver, interrupt remapping, IRQ handling, etc.
And depending on how you execute them, you might also be exercising the QEMU emulation paths. You could even test different HW firmwares if you wanted to :)
Quoted 10 lines
For more background on the motivation and design of this series, please see the RFC:
https://lore.kernel.org/kvm/[email protected]/
This series can also be found on GitHub:
...
Quoted 7 lines
Instructions
-----------------------------------------------------------------------Running VFIO selftests requires at a PCI device bound to vfio-pci for the tests to use. The address of this device is passed to the test as a segment:bus:device.function string, which must match the path to the device in
/sys/bus/pci/devices/(e.g. 0000:00:04.0).
Would you be able to autodetect the devices that are vfio-testable? I saw this question in the thread, but did not see the answer (sorry if I missed it).
Quoted 4 lines
Once you have chosen a device, there is a helper script provided to unbind the device from its current driver, bind it to vfio-pci, export the environment variable $VFIO_SELFTESTS_BDF, and launch a shell:
If I'm reading the series correctly there is a fair amount of helper code needed: device (un)binding, checking capabilities, setting up DMAable memory.... Have you considered a library like libvfn [4] (is there any other?) to take care of all this?
One of the good things about having all this in a library is that it can be used in other contexts besides testing.
Quoted 6 lines
$ tools/testing/selftests/vfio/run.sh -d 0000:00:04.0 -sThe -d option tells the script which device to use and the -s option tells the script to launch a shell.
Quoted 7 lines
Additionally, the VFIO selftest
vfio_dma_mapping_testhas test cases that rely on HugeTLB pages being available, otherwise they are skipped. To enable those tests make sure at least 1 2MB and 1 1GB HugeTLB pages are available.$ echo 1 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages $ echo 1 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
Should this be automatic? You can just modify nr_hugepages everytime you execute a test that requires it.
Quoted 4 lines
To run all VFIO selftests using make:
$ make -C tools/testing/selftests/vfio run_tests
This ties back to having the test suit outside the Linux Kernel sources. I might not always want/have a Linux Kernel selftests. Like if I would want to test the Intel/AMD IOMMU implementation in QEMU.
Quoted 15 lines
To run individual tests:
$ tools/testing/selftests/vfio/vfio_dma_mapping_test $ tools/testing/selftests/vfio/vfio_dma_mapping_test -v iommufd_anonymous_hugetlb_2mb $ tools/testing/selftests/vfio/vfio_dma_mapping_test -r vfio_dma_mapping_test.iommufd_anonymous_hugetlb_2mb.dma_map_unmapThe environment variable $VFIO_SELFTESTS_BDF can be overridden for a specific test by passing in the BDF on the command line as the last positional argument.
$ tools/testing/selftests/vfio/vfio_dma_mapping_test 0000:00:04.0 $ tools/testing/selftests/vfio/vfio_dma_mapping_test -v iommufd_anonymous_hugetlb_2mb 0000:00:04.0 $ tools/testing/selftests/vfio/vfio_dma_mapping_test -r vfio_dma_mapping_test.iommufd_anonymous_hugetlb_2mb.dma_map_unmap 0000:00:04.0
Quoted 7 lines
When you are done, free the HugeTLB pages and exit the shell started by
run.sh. Exiting the shell will cause the device to be unbound from vfio-pci and bound back to its original driver.$ echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages $ echo 0 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages $ exit
As before: Can this be done automatically?
Quoted 23 lines
It's also possible to use
run.shto run just a single test hermetically, rather than dropping into a shell:$ tools/testing/selftests/vfio/run.sh -d 0000:00:04.0 -- tools/testing/selftests/vfio/vfio_dma_mapping_test -v iommufd_anonymousTests
-----------------------------------------------------------------------There are 5 tests in this series, mostly to demonstrate as a proof-of-concept:
-
tools/testing/selftests/vfio/vfio_pci_device_test.c
-tools/testing/selftests/vfio/vfio_pci_driver_test.c
-tools/testing/selftests/vfio/vfio_iommufd_setup_test.c
-tools/testing/selftests/vfio/vfio_dma_mapping_test.c
-tools/testing/selftests/kvm/vfio_pci_device_irq_test.cFuture Areas of Development
-----------------------------------------------------------------------Library:
Quoted 9 lines
- Driver support for devices that can be used on AMD, ARM, and other platforms (e.g. mlx5).
- Driver support for a device available in QEMU VMs (e.g. pcie-ats-testdev [1])
- Support for tests that use multiple devices.
- Support for IOMMU groups with multiple devices.
- Support for multiple devices sharing the same container/iommufd.
- SharingTEST_ASSERT()macros and other common code between KVM and VFIO selftests.
Same as before: How about a lib?
Quoted 10 lines
Tests:
- DMA mapping performance tests for BARs/HugeTLB/etc.
- Porting tests from
https://github.com/awilliam/tests/commits/for-clg/ to selftests.
- Live Update selftests.
- Porting Sean's KVM selftest for posted interrupts to use the VFIO selftests library [2]
...
Quoted 6 lines
base-commit:
e271ed52b344ac02d4581286961d0c40acc54c03
prerequisite-patch-id:c1decca4653262d3d2451e6fd4422ebff9c0b589Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-goog
Best
[1] https://github.com/kdave/xfstests
[2] https://github.com/linux-blktests/blktests
[3] https://github.com/SamsungDS/iommutests
[4] https://github.com/SamsungDS/libvfn
Signature · 1 line
Joel Granados
David Matlack
maintainerQuoted 16 lines · Joel Granados
Quoted 2 lines · David Matlack
This series introduces VFIO selftests, located in
tools/testing/selftests/vfio/.Sorry for coming late to the party. Only recently got some cycles to go through this. This seems very similar to what we are trying to do with iommutests [3].
Quoted 6 lines
VFIO selftests aim to enable kernel developers to write and run tests that take the form of userspace programs that interact with VFIO and IOMMUFD uAPIs. VFIO selftests can be used to write functional tests for new features, regression tests for bugs, and performance tests for optimizations.
Have you considered implementing something outside the kernel sources? Something similar to fstests [1] or blktests [2]?
Quoted 8 lines
I ask because I have always seen it as a suit that tests (regression and performance) the Linux kernel as well as the hardware. By hardware I mean IOMMU devices, peripherals as well as their QEMU implementations. Since the scope is quite big, seemed (to me) like a good idea to keep it out of the kernel sources.
Can you speak to the pros/cons of having it in selftests? (sorry if this was already answered)
I talked about this a bit in the RFC cover letter, but did not copy it over to the v1 cover letter. Relevant excerpts below:
: We chose selftests to host these tests primarily to enable integration : with the existing KVM selftests. As explained in the next section, : enabling KVM developers to test the interaction between VFIO and KVM is : one of the motivators of this series.
:
: [...]
:
: - It enables testing the interaction between VFIO and KVM. There are
: some paths in KVM that are only exercised through VFIO, such as IRQ : bypass. VFIO selftests provides a helper library to enable KVM : developers to write KVM selftests to test those interactions [3].
:
: [...]
:
: To support testing the interactions between VFIO and KVM, the VFIO : selftests support sharing its library with the KVM selftest. The patches : at the end of this series demonstrate how that works.
:
: Essentially, we allow the KVM selftests to build their own copy of : tools/testing/selftests/vfio/lib/ and link it into KVM selftests : binaries. This requires minimal changes to the KVM selftests Makefile. :
: [3] https://lore.kernel.org/kvm/[email protected]/
If we had these tests out of tree, then I dn't see a clear path to being able to test the interaction between VFIO and KVM.
I am also probably biased/influenced coming from KVM development, where we use KVM selftests. I have found it very valuable as a developer to be able to send code changes along with the tests for those changes in a single series, without having to work across multiple repositories. And I am very used to that workflow.
Lastly, since the goal of my series is only to enable testing the kernel (VFIO, IOMMUFD, KVM, IOMMU drivers, etc.), co-locating with the kernel makes sense. I was not looking test hardware or emulators for correctness.
I do see that there is a lot of overlap between testing the kernel and testing hardware/emulators, and that there's likely code we can share. But I also want to be practical and make sure we solve the problem of testing the kernel.
Quoted 9 lines
Quoted 6 lines
These tests are designed to interact with real PCI devices, i.e. they do not rely on mocking out or faking any behavior in the kernel. This allows the tests to exercise not only VFIO but also IOMMUFD, the IOMMU driver, interrupt remapping, IRQ handling, etc.
And depending on how you execute them, you might also be exercising the QEMU emulation paths. You could even test different HW firmwares if you wanted to :)
That's true, but not a problem I'm looking to solve (testing hardware or testing QEMU). My only goal is testing the kernel.
Quoted 15 lines
Quoted 11 lines
For more background on the motivation and design of this series, please see the RFC:
https://lore.kernel.org/kvm/[email protected]/
This series can also be found on GitHub:
...
Quoted 3 lines
Instructions
-----------------------------------------------------------------------
Quoted 7 lines
Quoted 4 lines
Running VFIO selftests requires at a PCI device bound to vfio-pci for the tests to use. The address of this device is passed to the test as a segment:bus:device.function string, which must match the path to the device in
/sys/bus/pci/devices/(e.g. 0000:00:04.0).Would you be able to autodetect the devices that are vfio-testable? I saw this question in the thread, but did not see the answer (sorry if I missed it).
Eventually I think that could be possible. But for now it is up to the user to deide which devices to use.
Quoted 9 lines
Quoted 5 lines
Once you have chosen a device, there is a helper script provided to unbind the device from its current driver, bind it to vfio-pci, export the environment variable $VFIO_SELFTESTS_BDF, and launch a shell:
If I'm reading the series correctly there is a fair amount of helper code needed: device (un)binding, checking capabilities, setting up DMAable memory.... Have you considered a library like libvfn [4] (is there any other?) to take care of all this?
I didn't consider using an external library since it seemed like there would be several downsides, and the amount of helper code here was quite small IMO:
.../selftests/vfio/lib/include/vfio_util.h | 295 +++++++++ .../selftests/vfio/lib/vfio_pci_device.c | 594 ++++++++++++++++++ .../selftests/vfio/lib/vfio_pci_driver.c | 126 ++++ tools/testing/selftests/vfio/run.sh | 109 ++++
My concerns with using an external helper library are:
- It will make writing tests more difficult because it will require making changes both to the external library and to selftests. This means dealing with 2 different repositories, 2 different code review processes, 2 different maintaineres, etc. Not to mention needing to first change the library before writing the test.
The harder it is to write tests, the less tests will be written IMO.
- It will make maintaining VFIO selftests more difficult as we have to maintain compatability with the library and deal with breaking changes (or never make breaking changes to the library, which makes it harder to maintain and extend).
Contrast this to having the helper code co-located with the tests we can make whatever changes we want as needed and keep things as simple as possible.
It can be good to avoid duplicate code across projects, but in this case it doesn't seem like it enhances our ability to test the kernel or solves any specific problem.
One place where libvfn could make sense is if someone wants to support running VFIO selftests that require a driver with NVMe devices. Implementing that driver with the help of libvfn may be simpler than implementing one from scratch. But I think we should cross that bridge when we get there and compare the 2 approaches. The VFIO selftests driver API is pretty simple so implementing an NVMe driver in VFIO selftests might still be the simpler option.
Quoted 3 lines
One of the good things about having all this in a library is that it can be used in other contexts besides testing.
True but if it comes at the cost of making VFIO tests harder to write and maintain, I don't think it's necessarily a good idea.
Quoted 12 lines
Quoted 12 lines
$ tools/testing/selftests/vfio/run.sh -d 0000:00:04.0 -sThe -d option tells the script which device to use and the -s option tells the script to launch a shell.
Additionally, the VFIO selftest
vfio_dma_mapping_testhas test cases that rely on HugeTLB pages being available, otherwise they are skipped. To enable those tests make sure at least 1 2MB and 1 1GB HugeTLB pages are available.
Quoted 4 lines
Quoted 2 lines
$ echo 1 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages $ echo 1 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepagesShould this be automatic? You can just modify
nr_hugepageseverytime you execute a test that requires it.
Yeah I think we could probably do this automatically. As you can probably tell, I left most of the environment setup to the user for now but a lot of this can be automated in the seltests themselves.
Quoted 8 lines
Quoted 5 lines
To run all VFIO selftests using make:
$ make -C tools/testing/selftests/vfio run_testsThis ties back to having the test suit outside the Linux Kernel sources. I might not always want/have a Linux Kernel selftests. Like if I would want to test the Intel/AMD IOMMU implementation in QEMU.
Agreed, but the goal of this series is not to test the correctness of QEMU or hardware. It's to enable testing the kernel code.
If you want to test the correctness of QEMU, testing from the VFIO/userspace level might not be the right approach anyway. You probably want to be able to interact directly with the IOMMU registers, interrupt handlers, etc. e.g. Something like intel-iommu.c in kvm-unit-tests[*], combined with higher-level application testing.
VFIO selftests would be kind of a weird middle-ground where you can't actually control the interactions with the hardware/QEMU, (since the test is running in userspace and going through the kernel), and at the same time it's not a realistic application.
[*] https://gitlab.com/kvm-unit-tests/kvm-unit-tests/-/blob/master/x86/intel-iommu.c
Quoted 16 lines
Quoted 16 lines
To run individual tests:
$ tools/testing/selftests/vfio/vfio_dma_mapping_test $ tools/testing/selftests/vfio/vfio_dma_mapping_test -v iommufd_anonymous_hugetlb_2mb $ tools/testing/selftests/vfio/vfio_dma_mapping_test -r vfio_dma_mapping_test.iommufd_anonymous_hugetlb_2mb.dma_map_unmapThe environment variable $VFIO_SELFTESTS_BDF can be overridden for a specific test by passing in the BDF on the command line as the last positional argument.
$ tools/testing/selftests/vfio/vfio_dma_mapping_test 0000:00:04.0 $ tools/testing/selftests/vfio/vfio_dma_mapping_test -v iommufd_anonymous_hugetlb_2mb 0000:00:04.0 $ tools/testing/selftests/vfio/vfio_dma_mapping_test -r vfio_dma_mapping_test.iommufd_anonymous_hugetlb_2mb.dma_map_unmap 0000:00:04.0
Quoted 8 lines
Quoted 7 lines
When you are done, free the HugeTLB pages and exit the shell started by
run.sh. Exiting the shell will cause the device to be unbound from vfio-pci and bound back to its original driver.$ echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages $ echo 0 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages $ exitAs before: Can this be done automatically?
Yeah, most likely this can be done automatically.
Quoted 33 lines
Quoted 24 lines
It's also possible to use
run.shto run just a single test hermetically, rather than dropping into a shell:$ tools/testing/selftests/vfio/run.sh -d 0000:00:04.0 -- tools/testing/selftests/vfio/vfio_dma_mapping_test -v iommufd_anonymousTests
-----------------------------------------------------------------------There are 5 tests in this series, mostly to demonstrate as a proof-of-concept:
-
tools/testing/selftests/vfio/vfio_pci_device_test.c
-tools/testing/selftests/vfio/vfio_pci_driver_test.c
-tools/testing/selftests/vfio/vfio_iommufd_setup_test.c
-tools/testing/selftests/vfio/vfio_dma_mapping_test.c
-tools/testing/selftests/kvm/vfio_pci_device_irq_test.cFuture Areas of Development
-----------------------------------------------------------------------Library:
Quoted 9 lines
- Driver support for devices that can be used on AMD, ARM, and other platforms (e.g. mlx5).
- Driver support for a device available in QEMU VMs (e.g. pcie-ats-testdev [1])
- Support for tests that use multiple devices.
- Support for IOMMU groups with multiple devices.
- Support for multiple devices sharing the same container/iommufd.
- SharingTEST_ASSERT()macros and other common code between KVM and VFIO selftests.
Quoted 1 line
Same as before: How about a lib?
That is how I plan to approach it. Just like tools/testing/selftests/vfio/lib can be shared with KVM selftests, we can create a selftests library for this common stuff and share it between VFIO and KVM.
But if you are referring to an external library that is outside of the kernel tree, see my comments above.
Quoted 13 lines
Quoted 11 lines
Tests:
- DMA mapping performance tests for BARs/HugeTLB/etc.
- Porting tests from
https://github.com/awilliam/tests/commits/for-clg/ to selftests.
- Live Update selftests.
- Porting Sean's KVM selftest for posted interrupts to use the VFIO selftests library [2]...
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
base-commit:
e271ed52b344ac02d4581286961d0c40acc54c03
prerequisite-patch-id:c1decca4653262d3d2451e6fd4422ebff9c0b589Signature · 1 line
2.50.0.rc2.701.gf1e915cc24-googBest
Thanks for taking a look!
Quoted 9 lines
[1] https://github.com/kdave/xfstests
[2] https://github.com/linux-blktests/blktests
[3] https://github.com/SamsungDS/iommutests
[4] https://github.com/SamsungDS/libvfnSignature · 1 line
Joel Granados
David Matlack
maintainerQuoted 8 lines · David Matlack
Quoted 6 lines · Joel Granados
Quoted 2 lines · David Matlack
This series introduces VFIO selftests, located in
tools/testing/selftests/vfio/.Sorry for coming late to the party. Only recently got some cycles to go through this. This seems very similar to what we are trying to do with iommutests [3].
Joel and I synced offline. We decided the best path forward for now is to proceed with VFIO selftests and iommutests in parallel, then look for opportunities to share code once both have matured a bit.