From fcf1aef792a05cff085d30d6bc4a79dbb85274c3 Mon Sep 17 00:00:00 2001 From: Fabian Bonk Date: Tue, 28 Apr 2020 13:36:05 +0200 Subject: [PATCH] check vfio dma allocation size befor allocating --- src/libixy-vfio.c | 4 ++-- src/libixy-vfio.h | 2 ++ src/memory.c | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libixy-vfio.c b/src/libixy-vfio.c index 7d00e0c..24c5b62 100644 --- a/src/libixy-vfio.c +++ b/src/libixy-vfio.c @@ -18,12 +18,12 @@ #include +#include "libixy-vfio.h" + #define IRQ_SET_BUF_LEN (sizeof(struct vfio_irq_set) + sizeof(int)) #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; diff --git a/src/libixy-vfio.h b/src/libixy-vfio.h index 17966d8..47df5fa 100644 --- a/src/libixy-vfio.h +++ b/src/libixy-vfio.h @@ -3,6 +3,8 @@ #include +#define MIN_DMA_MEMORY 4096 // we can not allocate less than page_size memory + // enables DMA on a VFIO device void vfio_enable_dma(int device_fd); diff --git a/src/memory.c b/src/memory.c index aface15..bf96495 100644 --- a/src/memory.c +++ b/src/memory.c @@ -45,6 +45,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);