]> www.infradead.org Git - users/hch/misc.git/commitdiff
vfio: selftests: Test basic VFIO and IOMMUFD integration
authorJosh Hilke <jrhilke@google.com>
Fri, 22 Aug 2025 21:24:51 +0000 (21:24 +0000)
committerAlex Williamson <alex.williamson@redhat.com>
Wed, 27 Aug 2025 18:14:02 +0000 (12:14 -0600)
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.

Signed-off-by: Josh Hilke <jrhilke@google.com>
Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20250822212518.4156428-5-dmatlack@google.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
tools/testing/selftests/vfio/Makefile
tools/testing/selftests/vfio/vfio_iommufd_setup_test.c [new file with mode: 0644]

index 8284195372507944d3373ba9629374c0677dcf48..e4a5d6eadff394ea3ff9d7a693876ee84fa053e5 100644 (file)
@@ -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
diff --git a/tools/testing/selftests/vfio/vfio_iommufd_setup_test.c b/tools/testing/selftests/vfio/vfio_iommufd_setup_test.c
new file mode 100644 (file)
index 0000000..f45335d
--- /dev/null
@@ -0,0 +1,157 @@
+// 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>
+#include <linux/vfio.h>
+#include <linux/iommufd.h>
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include <vfio_util.h>
+#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 int vfio_device_bind_iommufd_ioctl(int cdev_fd, int iommufd)
+{
+       struct vfio_device_bind_iommufd bind_args = {
+               .argsz = sizeof(bind_args),
+               .iommufd = iommufd,
+       };
+
+       return ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind_args);
+}
+
+static int vfio_device_get_info_ioctl(int cdev_fd)
+{
+       struct vfio_device_info info_args = { .argsz = sizeof(info_args) };
+
+       return ioctl(cdev_fd, VFIO_DEVICE_GET_INFO, &info_args);
+}
+
+static int vfio_device_ioas_alloc_ioctl(int iommufd, struct iommu_ioas_alloc *alloc_args)
+{
+       *alloc_args = (struct iommu_ioas_alloc){
+               .size = sizeof(struct iommu_ioas_alloc),
+       };
+
+       return ioctl(iommufd, IOMMU_IOAS_ALLOC, alloc_args);
+}
+
+static int vfio_device_attach_iommufd_pt_ioctl(int cdev_fd, u32 pt_id)
+{
+       struct vfio_device_attach_iommufd_pt attach_args = {
+               .argsz = sizeof(attach_args),
+               .pt_id = pt_id,
+       };
+
+       return ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_args);
+}
+
+static int vfio_device_detach_iommufd_pt_ioctl(int cdev_fd)
+{
+       struct vfio_device_detach_iommufd_pt detach_args = {
+               .argsz = sizeof(detach_args),
+       };
+
+       return ioctl(cdev_fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_args);
+}
+
+FIXTURE(vfio_cdev) {
+       int cdev_fd;
+       int iommufd;
+};
+
+FIXTURE_SETUP(vfio_cdev)
+{
+       ASSERT_LE(0, (self->cdev_fd = open(cdev_path, O_RDWR, 0)));
+       ASSERT_LE(0, (self->iommufd = open(iommu_dev_path, O_RDWR, 0)));
+}
+
+FIXTURE_TEARDOWN(vfio_cdev)
+{
+       ASSERT_EQ(0, close(self->cdev_fd));
+       ASSERT_EQ(0, close(self->iommufd));
+}
+
+TEST_F(vfio_cdev, bind)
+{
+       ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, self->iommufd));
+       ASSERT_EQ(0, vfio_device_get_info_ioctl(self->cdev_fd));
+}
+
+TEST_F(vfio_cdev, get_info_without_bind_fails)
+{
+       ASSERT_NE(0, vfio_device_get_info_ioctl(self->cdev_fd));
+}
+
+TEST_F(vfio_cdev, bind_bad_iommufd_fails)
+{
+       ASSERT_NE(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, -2));
+}
+
+TEST_F(vfio_cdev, repeated_bind_fails)
+{
+       ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, self->iommufd));
+       ASSERT_NE(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, self->iommufd));
+}
+
+TEST_F(vfio_cdev, attach_detatch_pt)
+{
+       struct iommu_ioas_alloc alloc_args;
+
+       ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, self->iommufd));
+       ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, &alloc_args));
+       ASSERT_EQ(0, vfio_device_attach_iommufd_pt_ioctl(self->cdev_fd, alloc_args.out_ioas_id));
+       ASSERT_EQ(0, vfio_device_detach_iommufd_pt_ioctl(self->cdev_fd));
+}
+
+TEST_F(vfio_cdev, attach_invalid_pt_fails)
+{
+       ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, self->iommufd));
+       ASSERT_NE(0, vfio_device_attach_iommufd_pt_ioctl(self->cdev_fd, UINT32_MAX));
+}
+
+int main(int argc, char *argv[])
+{
+       const char *device_bdf = vfio_selftests_get_bdf(&argc, argv);
+
+       set_cdev_path(device_bdf);
+       printf("Using cdev device %s\n", cdev_path);
+
+       return test_harness_run(argc, argv);
+}