Skip to content

Commit

Permalink
check vfio dma allocation size before allocating
Browse files Browse the repository at this point in the history
  • Loading branch information
bonkf committed Apr 27, 2020
1 parent 2359e5b commit 0bbd4ac
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/libixy-vfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 0bbd4ac

Please sign in to comment.