Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check vfio DMA allocation size before allocating #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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