Give User Space Direct Device Access With VFIO
Bind a PCI device to vfio-pci on Linux 6.12 and drive it from user space with the VFIO container and group ioctls: mmap a BAR, map DMA and read registers.

Sometimes the right driver for a PCI device is not in the kernel at all. A virtual machine monitor passes a GPU to a guest, a packet-processing framework such as DPDK drives a NIC from user space to avoid the kernel network stack, and a test rig pokes a prototype board. All of them use VFIO, the framework that exposes a PCI device to an unprivileged process in a way that stays safe, because every DMA the device performs goes through the IOMMU and is confined to memory the process explicitly mapped. This post uses Linux 6.12 to bind a device to vfio-pci, walks the container and group ioctl sequence in C, maps a BAR and reads a register. It builds on the IOMMU explanation and on BAR mapping.
The interface is documented in the kernel's VFIO guide, with the structure definitions in include/uapi/linux/vfio.h.
Why VFIO and not /dev/mem or UIO
/dev/mem and the older UIO framework give a process raw access to device memory, but neither controls DMA. A buggy or hostile program can program the device to DMA over kernel memory, so these interfaces require full trust and cannot be used by unprivileged code. VFIO refuses to hand out a device until the IOMMU can isolate it, and then ties the device's address space to the process: the device sees only the mappings the process created.
The unit of ownership is the IOMMU group, the set of devices that cannot be isolated from each other. VFIO requires every device in the group to be bound to vfio-pci (or to no driver) before the group becomes usable, and that is what makes assignment safe.
Bind the device to vfio-pci
First load the driver. Then free the device from its current driver and give it to vfio-pci. The driver_override file forces the next bind to a named driver:
sudo modprobe vfio-pci
echo 0000:02:00.0 | sudo tee /sys/bus/pci/devices/0000:02:00.0/driver/unbind
echo vfio-pci | sudo tee /sys/bus/pci/devices/0000:02:00.0/driver_override
echo 0000:02:00.0 | sudo tee /sys/bus/pci/drivers_probe0000:02:00.0
vfio-pci
0000:02:00.0The last write asks the PCI core to probe the device again, and with the override set, vfio-pci takes it. Confirm it and find the group number:
lspci -k -s 02:00.0 | grep driver
readlink /sys/bus/pci/devices/0000:02:00.0/iommu_group
ls /dev/vfio Kernel driver in use: vfio-pci
../../../kernel/iommu_groups/12
12 vfioThe device is now owned by VFIO, and /dev/vfio/12 is the file for its group. The group's file permissions control who may use it, so a udev rule that gives your user that file is how unprivileged access is granted. Any other device in group 12 must be unbound or also on vfio-pci before the group is viable.
The ioctl sequence
A VFIO program has three objects: a container (an IOMMU address space), a group (the isolation unit) and a device. The legacy container interface sets them up in this order:
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/vfio.h>
int main(void)
{
int container, group, device;
struct vfio_group_status gstatus = { .argsz = sizeof(gstatus) };
struct vfio_region_info region = { .argsz = sizeof(region) };
volatile uint32_t *bar0;
container = open("/dev/vfio/vfio", O_RDWR);
if (container < 0 ||
ioctl(container, VFIO_GET_API_VERSION) != VFIO_API_VERSION ||
!ioctl(container, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
fprintf(stderr, "vfio container unavailable\n");
return 1;
}
group = open("/dev/vfio/12", O_RDWR);
if (group < 0) {
perror("open group");
return 1;
}
ioctl(group, VFIO_GROUP_GET_STATUS, &gstatus);
if (!(gstatus.flags & VFIO_GROUP_FLAGS_VIABLE)) {
fprintf(stderr, "group not viable: bind every device in it\n");
return 1;
}
ioctl(group, VFIO_GROUP_SET_CONTAINER, &container);
ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
device = ioctl(group, VFIO_GROUP_GET_DEVICE_FD, "0000:02:00.0");
if (device < 0) {
perror("get device");
return 1;
}
region.index = VFIO_PCI_BAR0_REGION_INDEX;
ioctl(device, VFIO_DEVICE_GET_REGION_INFO, ®ion);
printf("BAR0 size 0x%llx offset 0x%llx\n",
(unsigned long long)region.size,
(unsigned long long)region.offset);
bar0 = mmap(NULL, region.size, PROT_READ | PROT_WRITE, MAP_SHARED,
device, region.offset);
if (bar0 == MAP_FAILED) {
perror("mmap");
return 1;
}
printf("register 0x00 = 0x%08x\n", bar0[0]);
return 0;
}For QEMU's edu device with a 1 MiB BAR 0, the program prints the following. The offset is the file position VFIO assigns to the region inside the device file. VFIO puts the region index in the high bits of that position, so BAR 0 sits at offset zero and BAR 1 at a large multiple of 2 to the power of 40:
BAR0 size 0x100000 offset 0x0
register 0x00 = 0x010000edThe order is not arbitrary. VFIO_GROUP_SET_CONTAINER attaches the group to the container before the container can select an IOMMU model, and VFIO_SET_IOMMU with VFIO_TYPE1_IOMMU picks the type 1 model, the one used on x86, Arm and others. Only after that does VFIO_GROUP_GET_DEVICE_FD return a device file descriptor. Each ioctl here has a return value that a real program checks. They are omitted above only to keep the sequence readable.
The argsz field in each structure is mandatory and holds the structure's size, so the kernel can extend the structure in later versions without breaking old callers.
Map memory for the device's DMA
A device reaches memory only through the IOVAs you map into the container. To give the device a buffer, allocate it in the process and map it, choosing the IOVA yourself:
struct vfio_iommu_type1_dma_map map = {
.argsz = sizeof(map),
.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
.vaddr = (uintptr_t)buf,
.iova = 0x100000,
.size = 0x10000,
};
ioctl(container, VFIO_IOMMU_MAP_DMA, &map);Now the address 0x100000 means the process's buffer to the device, and you write 0x100000 into the device's DMA registers. The kernel pins the pages, since the device may write to them at any moment and they must not be swapped or moved. Address and size must be page-aligned. Anything outside this mapping causes an IOMMU fault, which is the isolation the framework exists to provide. VFIO_IOMMU_UNMAP_DMA removes a mapping.
Pinning counts against the process's locked-memory limit, so a large mapping fails with -ENOMEM unless RLIMIT_MEMLOCK is raised. This is the most common error in a first program, and the fix is ulimit -l or a limit in the unit file.
Interrupts and the device file
A device interrupt reaches user space as an eventfd. VFIO_DEVICE_SET_IRQS associates an eventfd with an interrupt index (INTx, MSI or MSI-X), and the program reads the eventfd to learn the interrupt fired. For virtual machines the same mechanism connects a physical interrupt to KVM, which injects it into the guest without a context switch to user space. Configuration space is also available as a region, VFIO_PCI_CONFIG_REGION_INDEX, readable with pread() on the device file.
The newer interface
Kernels since 6.6 provide device files that do not need a group: /dev/vfio/devices/vfio0 and an IOMMUFD context, /dev/iommu, which replaces the container. The device cdev interface has the same safety guarantees with a simpler ownership model, and it is where new features such as nested translation land. The container and group interface in this post is still supported in 6.12 and is the one older tools use, so knowing it remains necessary. Check which one your tool uses before changing permissions.
Security checks before you trust it
Three things decide whether the isolation is real. The IOMMU must be enabled, which you confirm by the presence of /sys/kernel/iommu_groups/ entries. The group must contain only devices you intend to give away, since a device you forgot belongs to the process too. And the platform must have interrupt remapping, since without it a device can inject arbitrary interrupts. VFIO refuses VFIO_TYPE1_IOMMU if the isolation it requires is missing, and an allow_unsafe_interrupts module parameter exists to override that refusal. Setting it is a decision to trust the hardware, so it does not belong on a multi-tenant host.


