On 6/21/2025 4:50 AM, David Matlack wrote:
Introduce 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.
Signed-off-by: David Matlack dmatlack@google.com
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
diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftests/vfio/Makefile index db3e4db1a6dd..828419537250 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -1,4 +1,5 @@ CFLAGS = $(KHDR_INCLUDES) +TEST_GEN_PROGS += vfio_pci_device_test include ../lib.mk include lib/libvfio.mk diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/testing/selftests/vfio/vfio_pci_device_test.c new file mode 100644 index 000000000000..6d3a33804be3 --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c @@ -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.
- */
+#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.
+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);
- 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
- ASSERT_TRUE(!munmap(mem, SZ_2M));
+}