kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Matlack <dmatlack@google.com>
To: Alex Williamson <alex.williamson@redhat.com>
Cc: Aaron Lewis <aaronlewis@google.com>,
	 Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	 Adithya Jayachandran <ajayachandra@nvidia.com>,
	Arnaldo Carvalho de Melo <acme@redhat.com>,
	 Dan Williams <dan.j.williams@intel.com>,
	Dave Jiang <dave.jiang@intel.com>,
	 David Matlack <dmatlack@google.com>,
	dmaengine@vger.kernel.org,  Jason Gunthorpe <jgg@nvidia.com>,
	Joel Granados <joel.granados@kernel.org>,
	 Josh Hilke <jrhilke@google.com>,
	Kevin Tian <kevin.tian@intel.com>,
	kvm@vger.kernel.org,  linux-kselftest@vger.kernel.org,
	Paolo Bonzini <pbonzini@redhat.com>,
	 Pasha Tatashin <pasha.tatashin@soleen.com>,
	Saeed Mahameed <saeedm@nvidia.com>,
	 Sean Christopherson <seanjc@google.com>,
	Shuah Khan <shuah@kernel.org>,
	 Vinicius Costa Gomes <vinicius.gomes@intel.com>,
	Vipin Sharma <vipinsh@google.com>,
	 "Yury Norov [NVIDIA]" <yury.norov@gmail.com>,
	Shuah Khan <skhan@linuxfoundation.org>
Subject: [PATCH v2 04/30] vfio: selftests: Test basic VFIO and IOMMUFD integration
Date: Fri, 22 Aug 2025 21:24:51 +0000	[thread overview]
Message-ID: <20250822212518.4156428-5-dmatlack@google.com> (raw)
In-Reply-To: <20250822212518.4156428-1-dmatlack@google.com>

From: Josh Hilke <jrhilke@google.com>

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

diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftests/vfio/Makefile
index 828419537250..e4a5d6eadff3 100644
--- a/tools/testing/selftests/vfio/Makefile
+++ b/tools/testing/selftests/vfio/Makefile
@@ -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
index 000000000000..f45335d9260f
--- /dev/null
+++ b/tools/testing/selftests/vfio/vfio_iommufd_setup_test.c
@@ -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);
+}
-- 
2.51.0.rc2.233.g662b1ed5c5-goog


  parent reply	other threads:[~2025-08-22 21:26 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-22 21:24 [PATCH v2 00/30] vfio: Introduce selftests for VFIO David Matlack
2025-08-22 21:24 ` [PATCH v2 01/30] selftests: Create tools/testing/selftests/vfio David Matlack
2025-08-22 21:24 ` [PATCH v2 02/30] vfio: selftests: Add a helper library for VFIO selftests David Matlack
2025-08-22 21:24 ` [PATCH v2 03/30] vfio: selftests: Introduce vfio_pci_device_test David Matlack
2025-08-22 21:24 ` David Matlack [this message]
2025-08-22 21:24 ` [PATCH v2 05/30] vfio: selftests: Move vfio dma mapping test to their own file David Matlack
2025-08-22 21:24 ` [PATCH v2 06/30] vfio: selftests: Add test to reset vfio device David Matlack
2025-08-22 21:24 ` [PATCH v2 07/30] vfio: selftests: Add DMA mapping tests for 2M and 1G HugeTLB David Matlack
2025-08-22 21:24 ` [PATCH v2 08/30] vfio: selftests: Validate 2M/1G HugeTLB are mapped as 2M/1G in IOMMU David Matlack
2025-08-22 21:24 ` [PATCH v2 09/30] vfio: selftests: Keep track of DMA regions mapped into the device David Matlack
2025-08-22 21:24 ` [PATCH v2 10/30] vfio: selftests: Enable asserting MSI eventfds not firing David Matlack
2025-08-22 21:24 ` [PATCH v2 11/30] vfio: selftests: Add a helper for matching vendor+device IDs David Matlack
2025-08-22 21:24 ` [PATCH v2 12/30] vfio: selftests: Add driver framework David Matlack
2025-08-22 21:25 ` [PATCH v2 13/30] vfio: sefltests: Add vfio_pci_driver_test David Matlack
2025-08-22 21:25 ` [PATCH v2 14/30] tools headers: Add stub definition for __iomem David Matlack
2025-08-22 21:25 ` [PATCH v2 15/30] tools headers: Import asm-generic MMIO helpers David Matlack
2025-08-22 21:25 ` [PATCH v2 16/30] tools headers: Import x86 MMIO helper overrides David Matlack
2025-08-22 21:25 ` [PATCH v2 17/30] tools headers: Add symlink to linux/pci_ids.h David Matlack
2025-08-22 21:25 ` [PATCH v2 18/30] dmaengine: ioat: Move system_has_dca_enabled() to dma.h David Matlack
2025-08-22 21:25 ` [PATCH v2 19/30] vfio: selftests: Add driver for Intel CBDMA David Matlack
2025-08-22 21:25 ` [PATCH v2 20/30] tools headers: Import iosubmit_cmds512() David Matlack
2025-08-22 21:25 ` [PATCH v2 21/30] dmaengine: idxd: Allow registers.h to be included from tools/ David Matlack
2025-08-22 21:25 ` [PATCH v2 22/30] vfio: selftests: Add driver for Intel DSA David Matlack
2025-08-22 21:25 ` [PATCH v2 23/30] vfio: selftests: Move helper to get cdev path to libvfio David Matlack
2025-08-22 21:25 ` [PATCH v2 24/30] vfio: selftests: Encapsulate IOMMU mode David Matlack
2025-08-22 21:25 ` [PATCH v2 25/30] vfio: selftests: Replicate tests across all iommu_modes David Matlack
2025-08-22 21:25 ` [PATCH v2 26/30] vfio: selftests: Add vfio_type1v2_mode David Matlack
2025-08-22 21:25 ` [PATCH v2 27/30] vfio: selftests: Add iommufd_compat_type1{,v2} modes David Matlack
2025-08-22 21:25 ` [PATCH v2 28/30] vfio: selftests: Add iommufd mode David Matlack
2025-08-22 21:25 ` [PATCH v2 29/30] vfio: selftests: Make iommufd the default iommu_mode David Matlack
2025-08-22 21:25 ` [PATCH v2 30/30] vfio: selftests: Add a script to help with running VFIO selftests David Matlack
2025-08-27 18:55 ` [PATCH v2 00/30] vfio: Introduce selftests for VFIO Alex Williamson
2025-08-27 20:20   ` David Matlack

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250822212518.4156428-5-dmatlack@google.com \
    --to=dmatlack@google.com \
    --cc=aaronlewis@google.com \
    --cc=acme@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=ajayachandra@nvidia.com \
    --cc=alex.williamson@redhat.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=dmaengine@vger.kernel.org \
    --cc=jgg@nvidia.com \
    --cc=joel.granados@kernel.org \
    --cc=jrhilke@google.com \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=pasha.tatashin@soleen.com \
    --cc=pbonzini@redhat.com \
    --cc=saeedm@nvidia.com \
    --cc=seanjc@google.com \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=vinicius.gomes@intel.com \
    --cc=vipinsh@google.com \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).