Skip to content

Commit 831c37a

Browse files
dmatlackawilliam
authored andcommitted
vfio: selftests: Stop passing device for IOMMU operations
Drop the struct vfio_pci_device wrappers for IOMMU map/unmap functions and require tests to directly call iommu_map(), iommu_unmap(), etc. This results in more concise code, and also makes it clear the map operations are happening on a struct iommu, not necessarily on a specific device, especially when multi-device tests are introduced. Do the same for iova_allocator_init() as that function only needs the struct iommu, not struct vfio_pci_device. Reviewed-by: Alex Mastro <amastro@fb.com> Tested-by: Alex Mastro <amastro@fb.com> Reviewed-by: Raghavendra Rao Ananta <rananta@google.com> Signed-off-by: David Matlack <dmatlack@google.com> Link: https://lore.kernel.org/r/20251126231733.3302983-14-dmatlack@google.com Signed-off-by: Alex Williamson <alex@shazbot.org>
1 parent 2607a43 commit 831c37a

4 files changed

Lines changed: 22 additions & 65 deletions

File tree

tools/testing/selftests/vfio/lib/include/vfio_util.h

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -260,52 +260,10 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *device);
260260

261261
void vfio_pci_device_reset(struct vfio_pci_device *device);
262262

263-
static inline struct iommu_iova_range *vfio_pci_iova_ranges(struct vfio_pci_device *device,
264-
u32 *nranges)
265-
{
266-
return iommu_iova_ranges(device->iommu, nranges);
267-
}
268-
269-
struct iova_allocator *iova_allocator_init(struct vfio_pci_device *device);
263+
struct iova_allocator *iova_allocator_init(struct iommu *iommu);
270264
void iova_allocator_cleanup(struct iova_allocator *allocator);
271265
iova_t iova_allocator_alloc(struct iova_allocator *allocator, size_t size);
272266

273-
static inline int __vfio_pci_dma_map(struct vfio_pci_device *device,
274-
struct dma_region *region)
275-
{
276-
return __iommu_map(device->iommu, region);
277-
}
278-
279-
static inline void vfio_pci_dma_map(struct vfio_pci_device *device,
280-
struct dma_region *region)
281-
{
282-
VFIO_ASSERT_EQ(__vfio_pci_dma_map(device, region), 0);
283-
}
284-
285-
static inline int __vfio_pci_dma_unmap(struct vfio_pci_device *device,
286-
struct dma_region *region,
287-
u64 *unmapped)
288-
{
289-
return __iommu_unmap(device->iommu, region, unmapped);
290-
}
291-
292-
static inline void vfio_pci_dma_unmap(struct vfio_pci_device *device,
293-
struct dma_region *region)
294-
{
295-
VFIO_ASSERT_EQ(__vfio_pci_dma_unmap(device, region, NULL), 0);
296-
}
297-
298-
static inline int __vfio_pci_dma_unmap_all(struct vfio_pci_device *device,
299-
u64 *unmapped)
300-
{
301-
return __iommu_unmap_all(device->iommu, unmapped);
302-
}
303-
304-
static inline void vfio_pci_dma_unmap_all(struct vfio_pci_device *device)
305-
{
306-
VFIO_ASSERT_EQ(__vfio_pci_dma_unmap_all(device, NULL), 0);
307-
}
308-
309267
void vfio_pci_config_access(struct vfio_pci_device *device, bool write,
310268
size_t config, size_t size, void *data);
311269

tools/testing/selftests/vfio/lib/iova_allocator.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
#include <vfio_util.h>
2323

24-
struct iova_allocator *iova_allocator_init(struct vfio_pci_device *device)
24+
struct iova_allocator *iova_allocator_init(struct iommu *iommu)
2525
{
2626
struct iova_allocator *allocator;
2727
struct iommu_iova_range *ranges;
2828
u32 nranges;
2929

30-
ranges = vfio_pci_iova_ranges(device, &nranges);
30+
ranges = iommu_iova_ranges(iommu, &nranges);
3131
VFIO_ASSERT_NOT_NULL(ranges);
3232

3333
allocator = malloc(sizeof(*allocator));

tools/testing/selftests/vfio/vfio_dma_mapping_test.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ FIXTURE_SETUP(vfio_dma_mapping_test)
122122
{
123123
self->iommu = iommu_init(variant->iommu_mode);
124124
self->device = vfio_pci_device_init(device_bdf, self->iommu);
125-
self->iova_allocator = iova_allocator_init(self->device);
125+
self->iova_allocator = iova_allocator_init(self->iommu);
126126
}
127127

128128
FIXTURE_TEARDOWN(vfio_dma_mapping_test)
@@ -153,7 +153,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
153153
region.iova = iova_allocator_alloc(self->iova_allocator, size);
154154
region.size = size;
155155

156-
vfio_pci_dma_map(self->device, &region);
156+
iommu_map(self->iommu, &region);
157157
printf("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n", region.vaddr, size, region.iova);
158158

159159
ASSERT_EQ(region.iova, to_iova(self->device, region.vaddr));
@@ -195,7 +195,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap)
195195
}
196196

197197
unmap:
198-
rc = __vfio_pci_dma_unmap(self->device, &region, &unmapped);
198+
rc = __iommu_unmap(self->iommu, &region, &unmapped);
199199
ASSERT_EQ(rc, 0);
200200
ASSERT_EQ(unmapped, region.size);
201201
printf("Unmapped IOVA 0x%lx\n", region.iova);
@@ -245,7 +245,7 @@ FIXTURE_SETUP(vfio_dma_map_limit_test)
245245
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
246246
ASSERT_NE(region->vaddr, MAP_FAILED);
247247

248-
ranges = vfio_pci_iova_ranges(self->device, &nranges);
248+
ranges = iommu_iova_ranges(self->iommu, &nranges);
249249
VFIO_ASSERT_NOT_NULL(ranges);
250250
last_iova = ranges[nranges - 1].last;
251251
free(ranges);
@@ -268,10 +268,10 @@ TEST_F(vfio_dma_map_limit_test, unmap_range)
268268
u64 unmapped;
269269
int rc;
270270

271-
vfio_pci_dma_map(self->device, region);
271+
iommu_map(self->iommu, region);
272272
ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr));
273273

274-
rc = __vfio_pci_dma_unmap(self->device, region, &unmapped);
274+
rc = __iommu_unmap(self->iommu, region, &unmapped);
275275
ASSERT_EQ(rc, 0);
276276
ASSERT_EQ(unmapped, region->size);
277277
}
@@ -282,10 +282,10 @@ TEST_F(vfio_dma_map_limit_test, unmap_all)
282282
u64 unmapped;
283283
int rc;
284284

285-
vfio_pci_dma_map(self->device, region);
285+
iommu_map(self->iommu, region);
286286
ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr));
287287

288-
rc = __vfio_pci_dma_unmap_all(self->device, &unmapped);
288+
rc = __iommu_unmap_all(self->iommu, &unmapped);
289289
ASSERT_EQ(rc, 0);
290290
ASSERT_EQ(unmapped, region->size);
291291
}
@@ -298,10 +298,10 @@ TEST_F(vfio_dma_map_limit_test, overflow)
298298
region->iova = ~(iova_t)0 & ~(region->size - 1);
299299
region->size = self->mmap_size;
300300

301-
rc = __vfio_pci_dma_map(self->device, region);
301+
rc = __iommu_map(self->iommu, region);
302302
ASSERT_EQ(rc, -EOVERFLOW);
303303

304-
rc = __vfio_pci_dma_unmap(self->device, region, NULL);
304+
rc = __iommu_unmap(self->iommu, region, NULL);
305305
ASSERT_EQ(rc, -EOVERFLOW);
306306
}
307307

tools/testing/selftests/vfio/vfio_pci_driver_test.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static const char *device_bdf;
1818
ASSERT_EQ(EAGAIN, errno); \
1919
} while (0)
2020

21-
static void region_setup(struct vfio_pci_device *device,
21+
static void region_setup(struct iommu *iommu,
2222
struct iova_allocator *iova_allocator,
2323
struct dma_region *region, u64 size)
2424
{
@@ -33,13 +33,12 @@ static void region_setup(struct vfio_pci_device *device,
3333
region->iova = iova_allocator_alloc(iova_allocator, size);
3434
region->size = size;
3535

36-
vfio_pci_dma_map(device, region);
36+
iommu_map(iommu, region);
3737
}
3838

39-
static void region_teardown(struct vfio_pci_device *device,
40-
struct dma_region *region)
39+
static void region_teardown(struct iommu *iommu, struct dma_region *region)
4140
{
42-
vfio_pci_dma_unmap(device, region);
41+
iommu_unmap(iommu, region);
4342
VFIO_ASSERT_EQ(munmap(region->vaddr, region->size), 0);
4443
}
4544

@@ -76,12 +75,12 @@ FIXTURE_SETUP(vfio_pci_driver_test)
7675

7776
self->iommu = iommu_init(variant->iommu_mode);
7877
self->device = vfio_pci_device_init(device_bdf, self->iommu);
79-
self->iova_allocator = iova_allocator_init(self->device);
78+
self->iova_allocator = iova_allocator_init(self->iommu);
8079

8180
driver = &self->device->driver;
8281

83-
region_setup(self->device, self->iova_allocator, &self->memcpy_region, SZ_1G);
84-
region_setup(self->device, self->iova_allocator, &driver->region, SZ_2M);
82+
region_setup(self->iommu, self->iova_allocator, &self->memcpy_region, SZ_1G);
83+
region_setup(self->iommu, self->iova_allocator, &driver->region, SZ_2M);
8584

8685
/* Any IOVA that doesn't overlap memcpy_region and driver->region. */
8786
self->unmapped_iova = iova_allocator_alloc(self->iova_allocator, SZ_1G);
@@ -110,8 +109,8 @@ FIXTURE_TEARDOWN(vfio_pci_driver_test)
110109

111110
vfio_pci_driver_remove(self->device);
112111

113-
region_teardown(self->device, &self->memcpy_region);
114-
region_teardown(self->device, &driver->region);
112+
region_teardown(self->iommu, &self->memcpy_region);
113+
region_teardown(self->iommu, &driver->region);
115114

116115
iova_allocator_cleanup(self->iova_allocator);
117116
vfio_pci_device_cleanup(self->device);

0 commit comments

Comments
 (0)