Skip to content

Commit

Permalink
check vfio dma allocation size befor allocating
Browse files Browse the repository at this point in the history
  • Loading branch information
bonkf committed Apr 28, 2020
1 parent 2359e5b commit fcf1aef
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/libixy-vfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

#include <driver/device.h>

#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;
Expand Down
2 changes: 2 additions & 0 deletions src/libixy-vfio.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <stdint.h>

#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);

Expand Down
3 changes: 3 additions & 0 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fcf1aef

Please sign in to comment.