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>,
Andrew Jones <ajones@ventanamicro.com>,
Ard Biesheuvel <ardb@kernel.org>,
Arnaldo Carvalho de Melo <acme@redhat.com>,
Bibo Mao <maobibo@loongson.cn>,
Claudio Imbrenda <imbrenda@linux.ibm.com>,
Dan Williams <dan.j.williams@intel.com>,
Dave Jiang <dave.jiang@intel.com>,
David Matlack <dmatlack@google.com>,
dmaengine@vger.kernel.org, Huacai Chen <chenhuacai@kernel.org>,
James Houghton <jthoughton@google.com>,
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,
"Mike Rapoport (Microsoft)" <rppt@kernel.org>,
Paolo Bonzini <pbonzini@redhat.com>,
Pasha Tatashin <pasha.tatashin@soleen.com>,
"Pratik R. Sampat" <prsampat@amd.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>,
Wei Yang <richard.weiyang@gmail.com>,
"Yury Norov [NVIDIA]" <yury.norov@gmail.com>
Subject: [PATCH 14/33] vfio: selftests: Keep track of DMA regions mapped into the device
Date: Fri, 20 Jun 2025 23:20:12 +0000 [thread overview]
Message-ID: <20250620232031.2705638-15-dmatlack@google.com> (raw)
In-Reply-To: <20250620232031.2705638-1-dmatlack@google.com>
Keep 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.
Signed-off-by: David Matlack <dmatlack@google.com>
---
.../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(-)
diff --git a/tools/testing/selftests/vfio/lib/include/vfio_util.h b/tools/testing/selftests/vfio/lib/include/vfio_util.h
index 234403b442af..db08646c2819 100644
--- a/tools/testing/selftests/vfio/lib/include/vfio_util.h
+++ b/tools/testing/selftests/vfio/lib/include/vfio_util.h
@@ -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 */
diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
index 98cce0a6ecd7..36b4b30b75cf 100644
--- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c
+++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c
@@ -26,6 +26,33 @@
VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #_arg, __ret); \
} while (0)
+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) {
+ if (vaddr < region->vaddr)
+ continue;
+
+ if (vaddr >= region->vaddr + region->size)
+ continue;
+
+ return region->iova + (vaddr - region->vaddr);
+ }
+
+ return INVALID_IOVA;
+}
+
+iova_t to_iova(struct vfio_pci_device *device, void *vaddr)
+{
+ iova_t iova;
+
+ iova = __to_iova(device, vaddr);
+ VFIO_ASSERT_NE(iova, INVALID_IOVA, "%p is not mapped into device.\n", vaddr);
+
+ return iova;
+}
+
static void vfio_pci_irq_set(struct vfio_pci_device *device,
u32 index, u32 vector, u32 count, int *fds)
{
@@ -112,28 +139,34 @@ static void vfio_pci_irq_get(struct vfio_pci_device *device, u32 index,
ioctl_assert(device->fd, VFIO_DEVICE_GET_IRQ_INFO, irq_info);
}
-void vfio_pci_dma_map(struct vfio_pci_device *device, u64 iova, u64 size, void *vaddr)
+void vfio_pci_dma_map(struct vfio_pci_device *device,
+ struct vfio_dma_region *region)
{
struct vfio_iommu_type1_dma_map map = {
.argsz = sizeof(map),
.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
- .vaddr = (u64)vaddr,
- .iova = iova,
- .size = size,
+ .vaddr = (u64)region->vaddr,
+ .iova = region->iova,
+ .size = region->size,
};
ioctl_assert(device->container_fd, VFIO_IOMMU_MAP_DMA, &map);
+
+ list_add(®ion->link, &device->dma_regions);
}
-void vfio_pci_dma_unmap(struct vfio_pci_device *device, u64 iova, u64 size)
+void vfio_pci_dma_unmap(struct vfio_pci_device *device,
+ struct vfio_dma_region *region)
{
struct vfio_iommu_type1_dma_unmap unmap = {
.argsz = sizeof(unmap),
- .iova = iova,
- .size = size,
+ .iova = region->iova,
+ .size = region->size,
};
ioctl_assert(device->container_fd, VFIO_IOMMU_UNMAP_DMA, &unmap);
+
+ list_del(®ion->link);
}
static void vfio_pci_region_get(struct vfio_pci_device *device, int index,
@@ -260,6 +293,8 @@ static void vfio_pci_iommu_setup(struct vfio_pci_device *device, unsigned long i
{
int ret;
+ INIT_LIST_HEAD(&device->dma_regions);
+
ret = ioctl(device->container_fd, VFIO_CHECK_EXTENSION, iommu_type);
VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type);
diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
index 9cdf25b293c5..8cec09ba8ec3 100644
--- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
+++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
@@ -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[])
--
2.50.0.rc2.701.gf1e915cc24-goog
next prev parent reply other threads:[~2025-06-20 23:21 UTC|newest]
Thread overview: 61+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-20 23:19 [PATCH 00/33] vfio: Introduce selftests for VFIO David Matlack
2025-06-20 23:19 ` [PATCH 01/33] selftests: Create tools/testing/selftests/vfio David Matlack
2025-06-20 23:20 ` [PATCH 02/33] vfio: selftests: Add a helper library for VFIO selftests David Matlack
2025-06-20 23:20 ` [PATCH 03/33] vfio: selftests: Introduce vfio_pci_device_test David Matlack
2025-06-26 11:27 ` Sairaj Kodilkar
2025-06-26 11:44 ` Sairaj Kodilkar
2025-06-26 16:29 ` David Matlack
2025-06-27 4:57 ` Sairaj Kodilkar
2025-06-27 23:08 ` David Matlack
2025-06-30 6:18 ` Sairaj Kodilkar
2025-06-20 23:20 ` [PATCH 04/33] vfio: selftests: Test basic VFIO and IOMMUFD integration David Matlack
2025-06-20 23:20 ` [PATCH 05/33] vfio: selftests: Move vfio dma mapping test to their own file David Matlack
2025-06-20 23:20 ` [PATCH 06/33] vfio: selftests: Add test to reset vfio device David Matlack
2025-06-20 23:20 ` [PATCH 07/33] vfio: selftests: Add DMA mapping tests for 2M and 1G HugeTLB David Matlack
2025-06-20 23:20 ` [PATCH 08/33] vfio: selftests: Validate 2M/1G HugeTLB are mapped as 2M/1G in IOMMU David Matlack
2025-06-20 23:20 ` [PATCH 09/33] tools headers: Add stub definition for __iomem David Matlack
2025-06-20 23:20 ` [PATCH 10/33] tools headers: Import asm-generic MMIO helpers David Matlack
2025-06-20 23:20 ` [PATCH 11/33] tools headers: Import x86 MMIO helper overrides David Matlack
2025-06-20 23:20 ` [PATCH 12/33] tools headers: Import iosubmit_cmds512() David Matlack
2025-08-18 23:25 ` Vinicius Costa Gomes
2025-08-18 23:46 ` David Matlack
2025-06-20 23:20 ` [PATCH 13/33] tools headers: Add symlink to linux/pci_ids.h David Matlack
2025-06-20 23:20 ` David Matlack [this message]
2025-06-20 23:20 ` [PATCH 15/33] vfio: selftests: Enable asserting MSI eventfds not firing David Matlack
2025-06-20 23:20 ` [PATCH 16/33] vfio: selftests: Add a helper for matching vendor+device IDs David Matlack
2025-06-20 23:20 ` [PATCH 17/33] vfio: selftests: Add driver framework David Matlack
2025-06-20 23:20 ` [PATCH 18/33] vfio: sefltests: Add vfio_pci_driver_test David Matlack
2025-06-20 23:20 ` [PATCH 19/33] dmaengine: ioat: Move system_has_dca_enabled() to dma.h David Matlack
2025-08-19 22:07 ` Dave Jiang
2025-06-20 23:20 ` [PATCH 20/33] vfio: selftests: Add driver for Intel CBDMA David Matlack
2025-08-19 22:07 ` Dave Jiang
2025-06-20 23:20 ` [PATCH 21/33] dmaengine: idxd: Allow registers.h to be included from tools/ David Matlack
2025-08-18 23:26 ` Vinicius Costa Gomes
2025-06-20 23:20 ` [PATCH 22/33] vfio: selftests: Add driver for Intel DSA David Matlack
2025-08-18 23:41 ` Vinicius Costa Gomes
2025-08-19 16:31 ` David Matlack
2025-08-19 18:52 ` Vinicius Costa Gomes
2025-06-20 23:20 ` [PATCH 23/33] vfio: selftests: Move helper to get cdev path to libvfio David Matlack
2025-06-20 23:20 ` [PATCH 24/33] vfio: selftests: Encapsulate IOMMU mode David Matlack
2025-06-20 23:20 ` [PATCH 25/33] vfio: selftests: Replicate tests across all iommu_modes David Matlack
2025-06-20 23:20 ` [PATCH 26/33] vfio: selftests: Add vfio_type1v2_mode David Matlack
2025-06-20 23:20 ` [PATCH 27/33] vfio: selftests: Add iommufd_compat_type1{,v2} modes David Matlack
2025-06-20 23:20 ` [PATCH 28/33] vfio: selftests: Add iommufd mode David Matlack
2025-06-20 23:20 ` [PATCH 29/33] vfio: selftests: Make iommufd the default iommu_mode David Matlack
2025-06-20 23:20 ` [PATCH 30/33] vfio: selftests: Add a script to help with running VFIO selftests David Matlack
2025-06-20 23:20 ` [PATCH 31/33] KVM: selftests: Build and link sefltests/vfio/lib into KVM selftests David Matlack
2025-06-20 23:20 ` [PATCH 32/33] KVM: selftests: Test sending a vfio-pci device IRQ to a VM David Matlack
2025-06-20 23:20 ` [PATCH 33/33] KVM: selftests: Add -d option to vfio_pci_device_irq_test for device-sent MSIs David Matlack
2025-07-25 16:47 ` [PATCH 00/33] vfio: Introduce selftests for VFIO David Matlack
2025-07-28 16:27 ` Alex Williamson
2025-07-29 22:26 ` Jason Gunthorpe
2025-07-31 20:55 ` David Matlack
2025-08-18 18:59 ` David Matlack
2025-08-18 19:37 ` Alex Williamson
2025-08-18 20:33 ` David Matlack
2025-08-21 20:10 ` Alex Williamson
2025-08-21 21:03 ` David Matlack
2025-08-19 14:50 ` Shuah Khan
2025-08-05 15:08 ` Joel Granados
2025-08-12 15:04 ` David Matlack
2025-08-19 17:48 ` 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=20250620232031.2705638-15-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=ajones@ventanamicro.com \
--cc=alex.williamson@redhat.com \
--cc=ardb@kernel.org \
--cc=chenhuacai@kernel.org \
--cc=dan.j.williams@intel.com \
--cc=dave.jiang@intel.com \
--cc=dmaengine@vger.kernel.org \
--cc=imbrenda@linux.ibm.com \
--cc=jgg@nvidia.com \
--cc=joel.granados@kernel.org \
--cc=jrhilke@google.com \
--cc=jthoughton@google.com \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=maobibo@loongson.cn \
--cc=pasha.tatashin@soleen.com \
--cc=pbonzini@redhat.com \
--cc=prsampat@amd.com \
--cc=richard.weiyang@gmail.com \
--cc=rppt@kernel.org \
--cc=saeedm@nvidia.com \
--cc=seanjc@google.com \
--cc=shuah@kernel.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).