public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: David Matlack <dmatlack@google.com>
To: Alex Williamson <alex.williamson@redhat.com>
Cc: David Matlack <dmatlack@google.com>,
	Jason Gunthorpe <jgg@nvidia.com>, Josh Hilke <jrhilke@google.com>,
	 kvm@vger.kernel.org, Vipin Sharma <vipinsh@google.com>
Subject: [PATCH 04/12] vfio: selftests: Introduce struct iommu
Date: Wed,  8 Oct 2025 23:25:23 +0000	[thread overview]
Message-ID: <20251008232531.1152035-5-dmatlack@google.com> (raw)
In-Reply-To: <20251008232531.1152035-1-dmatlack@google.com>

Introduce struct iommu, which logically represents either a VFIO
container or an iommufd IOAS, depending on which IOMMU mode is used by
the test.

This will be used in a subsequent commit to allow devices to be added to
the same container/iommufd.

Signed-off-by: David Matlack <dmatlack@google.com>
---
 .../selftests/vfio/lib/include/vfio_util.h    | 16 ++---
 .../selftests/vfio/lib/vfio_pci_device.c      | 62 ++++++++++---------
 2 files changed, 42 insertions(+), 36 deletions(-)

diff --git a/tools/testing/selftests/vfio/lib/include/vfio_util.h b/tools/testing/selftests/vfio/lib/include/vfio_util.h
index 5b8d5444f105..14cd0bec45c0 100644
--- a/tools/testing/selftests/vfio/lib/include/vfio_util.h
+++ b/tools/testing/selftests/vfio/lib/include/vfio_util.h
@@ -160,15 +160,19 @@ struct vfio_pci_driver {
 	int msi;
 };
 
+struct iommu {
+	const struct iommu_mode *mode;
+	int container_fd;
+	int iommufd;
+	u32 ioas_id;
+	struct list_head dma_regions;
+};
+
 struct vfio_pci_device {
 	int fd;
-
-	const struct iommu_mode *iommu_mode;
 	int group_fd;
-	int container_fd;
 
-	int iommufd;
-	u32 ioas_id;
+	struct iommu *iommu;
 
 	struct vfio_device_info info;
 	struct vfio_region_info config_space;
@@ -177,8 +181,6 @@ 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];
 
diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
index da8edf297a4d..e4596a570422 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
@@ -32,7 +32,7 @@ iova_t __to_iova(struct vfio_pci_device *device, void *vaddr)
 {
 	struct vfio_dma_region *region;
 
-	list_for_each_entry(region, &device->dma_regions, link) {
+	list_for_each_entry(region, &device->iommu->dma_regions, link) {
 		if (vaddr < region->vaddr)
 			continue;
 
@@ -152,7 +152,7 @@ static void vfio_iommu_dma_map(struct vfio_pci_device *device,
 		.size = region->size,
 	};
 
-	ioctl_assert(device->container_fd, VFIO_IOMMU_MAP_DMA, &args);
+	ioctl_assert(device->iommu->container_fd, VFIO_IOMMU_MAP_DMA, &args);
 }
 
 static void iommufd_dma_map(struct vfio_pci_device *device,
@@ -166,21 +166,21 @@ static void iommufd_dma_map(struct vfio_pci_device *device,
 		.user_va = (u64)region->vaddr,
 		.iova = region->iova,
 		.length = region->size,
-		.ioas_id = device->ioas_id,
+		.ioas_id = device->iommu->ioas_id,
 	};
 
-	ioctl_assert(device->iommufd, IOMMU_IOAS_MAP, &args);
+	ioctl_assert(device->iommu->iommufd, IOMMU_IOAS_MAP, &args);
 }
 
 void vfio_pci_dma_map(struct vfio_pci_device *device,
 		      struct vfio_dma_region *region)
 {
-	if (device->iommufd)
+	if (device->iommu->iommufd)
 		iommufd_dma_map(device, region);
 	else
 		vfio_iommu_dma_map(device, region);
 
-	list_add(&region->link, &device->dma_regions);
+	list_add(&region->link, &device->iommu->dma_regions);
 }
 
 static void vfio_iommu_dma_unmap(struct vfio_pci_device *device,
@@ -192,7 +192,7 @@ static void vfio_iommu_dma_unmap(struct vfio_pci_device *device,
 		.size = region->size,
 	};
 
-	ioctl_assert(device->container_fd, VFIO_IOMMU_UNMAP_DMA, &args);
+	ioctl_assert(device->iommu->container_fd, VFIO_IOMMU_UNMAP_DMA, &args);
 }
 
 static void iommufd_dma_unmap(struct vfio_pci_device *device,
@@ -202,16 +202,16 @@ static void iommufd_dma_unmap(struct vfio_pci_device *device,
 		.size = sizeof(args),
 		.iova = region->iova,
 		.length = region->size,
-		.ioas_id = device->ioas_id,
+		.ioas_id = device->iommu->ioas_id,
 	};
 
-	ioctl_assert(device->iommufd, IOMMU_IOAS_UNMAP, &args);
+	ioctl_assert(device->iommu->iommufd, IOMMU_IOAS_UNMAP, &args);
 }
 
 void vfio_pci_dma_unmap(struct vfio_pci_device *device,
 			struct vfio_dma_region *region)
 {
-	if (device->iommufd)
+	if (device->iommu->iommufd)
 		iommufd_dma_unmap(device, region);
 	else
 		vfio_iommu_dma_unmap(device, region);
@@ -325,28 +325,28 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf
 	ioctl_assert(device->group_fd, VFIO_GROUP_GET_STATUS, &group_status);
 	VFIO_ASSERT_TRUE(group_status.flags & VFIO_GROUP_FLAGS_VIABLE);
 
-	ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->container_fd);
+	ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->container_fd);
 }
 
 static void vfio_pci_container_setup(struct vfio_pci_device *device, const char *bdf)
 {
-	unsigned long iommu_type = device->iommu_mode->iommu_type;
-	const char *path = device->iommu_mode->container_path;
+	unsigned long iommu_type = device->iommu->mode->iommu_type;
+	const char *path = device->iommu->mode->container_path;
 	int version;
 	int ret;
 
-	device->container_fd = open(path, O_RDWR);
-	VFIO_ASSERT_GE(device->container_fd, 0, "open(%s) failed\n", path);
+	device->iommu->container_fd = open(path, O_RDWR);
+	VFIO_ASSERT_GE(device->iommu->container_fd, 0, "open(%s) failed\n", path);
 
-	version = ioctl(device->container_fd, VFIO_GET_API_VERSION);
+	version = ioctl(device->iommu->container_fd, VFIO_GET_API_VERSION);
 	VFIO_ASSERT_EQ(version, VFIO_API_VERSION, "Unsupported version: %d\n", version);
 
 	vfio_pci_group_setup(device, bdf);
 
-	ret = ioctl(device->container_fd, VFIO_CHECK_EXTENSION, iommu_type);
+	ret = ioctl(device->iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type);
 	VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type);
 
-	ioctl_assert(device->container_fd, VFIO_SET_IOMMU, (void *)iommu_type);
+	ioctl_assert(device->iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type);
 
 	device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf);
 	VFIO_ASSERT_GE(device->fd, 0);
@@ -499,12 +499,12 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b
 	 * used to check if iommufd is enabled. In practice open() will never
 	 * return 0 unless stdin is closed.
 	 */
-	device->iommufd = open("/dev/iommu", O_RDWR);
-	VFIO_ASSERT_GT(device->iommufd, 0);
+	device->iommu->iommufd = open("/dev/iommu", O_RDWR);
+	VFIO_ASSERT_GT(device->iommu->iommufd, 0);
 
-	vfio_device_bind_iommufd(device->fd, device->iommufd);
-	device->ioas_id = iommufd_ioas_alloc(device->iommufd);
-	vfio_device_attach_iommufd_pt(device->fd, device->ioas_id);
+	vfio_device_bind_iommufd(device->fd, device->iommu->iommufd);
+	device->iommu->ioas_id = iommufd_ioas_alloc(device->iommu->iommufd);
+	vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id);
 }
 
 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_mode)
@@ -514,11 +514,14 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *iommu_
 	device = calloc(1, sizeof(*device));
 	VFIO_ASSERT_NOT_NULL(device);
 
-	INIT_LIST_HEAD(&device->dma_regions);
+	device->iommu = calloc(1, sizeof(*device->iommu));
+	VFIO_ASSERT_NOT_NULL(device->iommu);
 
-	device->iommu_mode = lookup_iommu_mode(iommu_mode);
+	INIT_LIST_HEAD(&device->iommu->dma_regions);
 
-	if (device->iommu_mode->container_path)
+	device->iommu->mode = lookup_iommu_mode(iommu_mode);
+
+	if (device->iommu->mode->container_path)
 		vfio_pci_container_setup(device, bdf);
 	else
 		vfio_pci_iommufd_setup(device, bdf);
@@ -547,13 +550,14 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *device)
 		VFIO_ASSERT_EQ(close(device->msi_eventfds[i]), 0);
 	}
 
-	if (device->iommufd) {
-		VFIO_ASSERT_EQ(close(device->iommufd), 0);
+	if (device->iommu->iommufd) {
+		VFIO_ASSERT_EQ(close(device->iommu->iommufd), 0);
 	} else {
 		VFIO_ASSERT_EQ(close(device->group_fd), 0);
-		VFIO_ASSERT_EQ(close(device->container_fd), 0);
+		VFIO_ASSERT_EQ(close(device->iommu->container_fd), 0);
 	}
 
+	free(device->iommu);
 	free(device);
 }
 
-- 
2.51.0.710.ga91ca5db03-goog


  parent reply	other threads:[~2025-10-08 23:26 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-08 23:25 [PATCH 00/12] vfio: selftests: Support for multi-device tests David Matlack
2025-10-08 23:25 ` [PATCH 01/12] vfio: selftests: Split run.sh into separate scripts David Matlack
2025-11-10  3:21   ` Raghavendra Rao Ananta
2025-11-10 18:19     ` David Matlack
2025-10-08 23:25 ` [PATCH 02/12] vfio: selftests: Allow passing multiple BDFs on the command line David Matlack
2025-11-10  3:45   ` Raghavendra Rao Ananta
2025-11-10 18:31     ` David Matlack
2025-10-08 23:25 ` [PATCH 03/12] vfio: selftests: Rename struct vfio_iommu_mode to iommu_mode David Matlack
2025-10-08 23:25 ` David Matlack [this message]
2025-10-08 23:25 ` [PATCH 05/12] vfio: selftests: Support multiple devices in the same container/iommufd David Matlack
2025-10-27 16:21   ` David Matlack
2025-11-07  3:37     ` Josh Hilke
2025-10-08 23:25 ` [PATCH 06/12] vfio: selftests: Eliminate overly chatty logging David Matlack
2025-10-08 23:25 ` [PATCH 07/12] vfio: selftests: Prefix logs with device BDF where relevant David Matlack
2025-11-10  4:54   ` Raghavendra Rao Ananta
2025-11-10 18:28     ` David Matlack
2025-10-08 23:25 ` [PATCH 08/12] vfio: selftests: Rename struct vfio_dma_region to dma_region David Matlack
2025-10-08 23:25 ` [PATCH 09/12] vfio: selftests: Move iommu_*() functions into iommu.c David Matlack
2025-10-08 23:25 ` [PATCH 10/12] vfio: selftests: Rename vfio_util.h to libvfio.h David Matlack
2025-10-08 23:25 ` [PATCH 11/12] vfio: selftests: Split libvfio.h into separate header files David Matlack
2025-10-08 23:25 ` [PATCH 12/12] vfio: selftests: Add vfio_pci_device_init_perf_test David Matlack
2025-10-16 16:12   ` David Matlack
2025-11-05 19:06 ` [PATCH 00/12] vfio: selftests: Support for multi-device tests Alex Williamson
2025-11-05 21:03   ` David Matlack
2025-11-05 21:54     ` Alex Williamson

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=20251008232531.1152035-5-dmatlack@google.com \
    --to=dmatlack@google.com \
    --cc=alex.williamson@redhat.com \
    --cc=jgg@nvidia.com \
    --cc=jrhilke@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=vipinsh@google.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