mirror of
https://github.com/torvalds/linux.git
synced 2026-05-31 02:24:24 +02:00
vfio: selftests: Add DMA mapping tests for 2M and 1G HugeTLB
Add test coverage of mapping 2M and 1G HugeTLB to vfio_dma_mapping_test using a fixture variant. If there isn't enough HugeTLB memory available for the test, just skip them. Signed-off-by: Josh Hilke <jrhilke@google.com> [switch from command line option to fixture variant] Acked-by: Shuah Khan <skhan@linuxfoundation.org> Signed-off-by: David Matlack <dmatlack@google.com> Link: https://lore.kernel.org/r/20250822212518.4156428-8-dmatlack@google.com Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
This commit is contained in:
parent
a0fd0af504
commit
751f6b5d06
|
|
@ -1,8 +1,10 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <linux/limits.h>
|
||||
#include <linux/mman.h>
|
||||
#include <linux/sizes.h>
|
||||
#include <linux/vfio.h>
|
||||
|
||||
|
|
@ -16,6 +18,25 @@ FIXTURE(vfio_dma_mapping_test) {
|
|||
struct vfio_pci_device *device;
|
||||
};
|
||||
|
||||
FIXTURE_VARIANT(vfio_dma_mapping_test) {
|
||||
u64 size;
|
||||
int mmap_flags;
|
||||
};
|
||||
|
||||
FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous) {
|
||||
.mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE,
|
||||
};
|
||||
|
||||
FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous_hugetlb_2mb) {
|
||||
.size = SZ_2M,
|
||||
.mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB | MAP_HUGE_2MB,
|
||||
};
|
||||
|
||||
FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, anonymous_hugetlb_1gb) {
|
||||
.size = SZ_1G,
|
||||
.mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_HUGETLB | MAP_HUGE_1GB,
|
||||
};
|
||||
|
||||
FIXTURE_SETUP(vfio_dma_mapping_test)
|
||||
{
|
||||
self->device = vfio_pci_device_init(device_bdf, VFIO_TYPE1_IOMMU);
|
||||
|
|
@ -28,17 +49,24 @@ FIXTURE_TEARDOWN(vfio_dma_mapping_test)
|
|||
|
||||
TEST_F(vfio_dma_mapping_test, dma_map_unmap)
|
||||
{
|
||||
const u64 size = SZ_2M;
|
||||
const u64 size = variant->size ?: getpagesize();
|
||||
const int flags = variant->mmap_flags;
|
||||
void *mem;
|
||||
u64 iova;
|
||||
|
||||
mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
ASSERT_NE(mem, MAP_FAILED);
|
||||
mem = 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)
|
||||
SKIP(return, "mmap() failed: %s (%d)\n", strerror(errno), errno);
|
||||
else
|
||||
ASSERT_NE(mem, MAP_FAILED);
|
||||
|
||||
iova = (u64)mem;
|
||||
|
||||
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_unmap(self->device, iova, size);
|
||||
|
||||
ASSERT_TRUE(!munmap(mem, size));
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user