From 0bbd4ac12d1f90456a1dc430d403586816eb1c1a Mon Sep 17 00:00:00 2001 From: Fabian Bonk Date: Mon, 27 Apr 2020 23:27:37 +0200 Subject: [PATCH] check vfio dma allocation size before allocating --- src/libixy-vfio.c | 4 +--- src/memory.c | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libixy-vfio.c b/src/libixy-vfio.c index 7d00e0c..4682a79 100644 --- a/src/libixy-vfio.c +++ b/src/libixy-vfio.c @@ -22,8 +22,6 @@ #define MAX_INTERRUPT_VECTORS 32 #define MSIX_IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int) * (MAX_INTERRUPT_VECTORS + 1)) -ssize_t MIN_DMA_MEMORY = 4096; // we can not allocate less than page_size memory - void vfio_enable_dma(int device_fd) { // write to the command register (offset 4) in the PCIe config space int command_register_offset = 4; @@ -301,7 +299,7 @@ uint64_t vfio_map_dma(void* vaddr, uint32_t size) { struct vfio_iommu_type1_dma_map dma_map = { .vaddr = (uint64_t) vaddr, .iova = iova, - .size = size < MIN_DMA_MEMORY ? MIN_DMA_MEMORY : size, + .size = size, .argsz = sizeof(dma_map), .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE}; int cfd = get_vfio_container(); diff --git a/src/memory.c b/src/memory.c index aface15..589b5fd 100644 --- a/src/memory.c +++ b/src/memory.c @@ -19,6 +19,8 @@ // this variable is unused. volatile int VFIO_CONTAINER_FILE_DESCRIPTOR = -1; +size_t MIN_DMA_MEMORY = 4096; // we can not allocate less than page_size memory + // translate a virtual address to a physical one via /proc/self/pagemap static uintptr_t virt_to_phys(void* virt) { long pagesize = sysconf(_SC_PAGESIZE); @@ -45,6 +47,9 @@ struct dma_memory memory_allocate_dma(size_t size, bool require_contiguous) { if (VFIO_CONTAINER_FILE_DESCRIPTOR != -1) { // VFIO == -1 means that there is no VFIO container set, i.e. VFIO / IOMMU is not activated debug("allocating dma memory via VFIO"); + if (size < MIN_DMA_MEMORY) { + size = MIN_DMA_MEMORY; + } void* virt_addr = (void*) check_err(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_2MB, -1, 0), "mmap hugepage"); // create IOMMU mapping uint64_t iova = (uint64_t) vfio_map_dma(virt_addr, size);