How DMA and the IOMMU Work: Addresses, Translation, and Why Devices Cannot Touch Arbitrary Host Memory

A network interface that just received a frame does not interrupt the CPU so the processor can copy the payload. The device writes the frame into host memory itself. The same pattern runs for disks, GPUs, and USB controllers. The mechanism is Direct Memory Access: the device is given a list of addresses and lengths, and a hardware engine moves the bytes.

That engine does not speak the same address language as a userspace load. A CPU uses virtual addresses that the MMU translates through page tables. A device traditionally used physical addresses. On a modern system an IOMMU sits between the device and DRAM, translating the addresses the device is allowed to use into the physical frames the kernel actually owns. Without that translation, a compromised or buggy device can read or write any host memory it can address.

This article is about that contract: the addresses a driver hands a device, why pages must be pinned, how scatter-gather lists work, and what the IOMMU actually translates. It is not a second tour of CPU page tables or of the flash translation layer. Related reading: How Virtual Memory Works, How the Linux Buddy and SLUB Allocators Work, and How Linux Interrupts Work.

The contract

DMA is a bus transaction initiated by a device, not by the CPU. The device supplies an address and a direction (host-to-device or device-to-host). The memory controller completes the transfer. The CPU is free to run other code while the engine works. When the transfer finishes, the device raises an interrupt or writes a completion record that the driver later harvests.

Three address spaces appear in a modern path:

  • CPU virtual addresses — what a process or the kernel uses after the MMU.
  • Physical addresses — the DRAM addresses the memory controller understands.
  • Bus / IOVA addresses — the addresses the device is programmed with. On systems without an IOMMU these are usually physical addresses (or a subset limited by the device's address width). With an IOMMU they are IO virtual addresses that the IOMMU translates.

A driver therefore cannot simply hand a kernel virtual pointer to the device. It must obtain a bus address that the device is allowed to use, and it must ensure the underlying physical pages stay resident for the duration of the transfer.

Why pages are pinned

The kernel can move or reclaim a page that is not pinned. If a device is in the middle of writing into that page when the page is migrated or swapped out, the write lands on the wrong frame or is lost. Pinning (in Linux, usually via get_page / pin_user_pages or the DMA API's internal accounting) raises the page's reference count so the VM subsystem will not reclaim or migrate it until the driver releases the pin.

Pinning is not free. A pinned page cannot be swapped, cannot be migrated for compaction or NUMA balancing, and counts against locked-memory limits. That is why high-order contiguous allocations for DMA can fail even when free memory looks abundant: the buddy allocator must find physically contiguous frames that are also allowed to be pinned, and migrate types exist precisely so the kernel can keep movable and unmovable pages from mixing.

Scatter-gather and the DMA API

Most devices can accept a list of (address, length) pairs rather than a single contiguous buffer. That list is a scatter-gather list. The kernel's DMA API (dma_map_sg, dma_unmap_sg, and the single-buffer equivalents) turns a list of pages or a kernel buffer into a list of bus addresses the device can consume, and later reverses the mapping.

On a system without an IOMMU the mapping is often a simple conversion from physical address to bus address, possibly with a bounce buffer if the device cannot address the high memory the buffer occupies. On a system with an IOMMU the mapping allocates an IOVA range, installs page-table entries that point at the pinned physical frames, and returns the IOVA to the driver. The device never sees the real physical addresses.

Unmapping is mandatory. Leaving an IOMMU mapping live after the transfer finishes is both a leak of IOVA space and a window in which a malicious device can still touch the pages.

What the IOMMU actually does

An IOMMU (Intel VT-d, AMD-Vi, ARM SMMU, and equivalents) is a second page-table walker that sits on the path between devices and memory. Each device, or each PCI function, is assigned a domain with its own page tables. When the device issues a transaction with an IOVA, the IOMMU walks those tables, checks permissions, and either replaces the address with the physical address or faults the transaction.

The benefits are isolation and flexibility:

  • A compromised NIC cannot read kernel memory or another guest's pages unless the IOMMU tables explicitly allow it.
  • The kernel can give a device a contiguous IOVA range even when the underlying physical pages are scattered.
  • Address-width mismatches disappear: a 32-bit device can be given IOVAs below 4 GiB while the real frames live anywhere.

The costs are translation latency (mitigated by an IOTLB), the memory required for the IOMMU page tables, and the requirement that every DMA mapping be installed and torn down correctly. A driver that maps a buffer and then forgets to unmap it leaves a permanent hole in the device's allowed address space.

Bounce buffers and legacy constraints

Older devices and some buses cannot address the full physical space. The classic example is a 32-bit PCI device on a machine with more than 4 GiB of RAM. The kernel's DMA layer detects the mismatch and, when necessary, copies the data through a bounce buffer that lives in the device's addressable range. The copy is invisible to the driver that used the DMA API correctly; it is the price of supporting the device.

Modern devices with 64-bit addressing and an IOMMU rarely need bounce buffers for address-width reasons. They still appear when a device requires a particular alignment or when the IOMMU is disabled.

Misconceptions

DMA is zero-copy. It avoids a CPU copy in the data path. The kernel may still copy into a bounce buffer, and the pages must be pinned and mapped. Zero CPU cycles on the payload is not the same as zero work.

The device sees the same addresses the kernel does. Without an IOMMU it often sees physical addresses. With an IOMMU it sees IOVAs that exist only in its domain. A kernel virtual address is useless to the device in either case.

Pinning is only for userspace pages. Kernel buffers that will be the target of DMA are also pinned for the duration of the mapping. The DMA API hides the pin/unpin for ordinary kernel allocations.

An IOMMU makes DMA slower in every case. The IOTLB absorbs repeated translations the same way a CPU TLB does. For large transfers the translation cost is amortized. The security and fragmentation benefits are usually the reason the IOMMU is left on.

Where this sits next to other machinery

CPU virtual memory (covered in How Virtual Memory Works) translates addresses for loads and stores issued by cores. The IOMMU translates addresses issued by devices. The two page-table formats are similar in spirit and often different in detail; a process page table is not an IOMMU page table.

The buddy allocator supplies the physical frames. Once those frames are pinned for DMA they become unmovable, which is why the migrate-type segregation described in the buddy/SLUB article exists. Interrupts notify the driver that a transfer has completed; the interrupt path itself is independent of how the bytes moved.

Takeaways

DMA lets a device move data while the CPU does other work. The addresses the device is given are bus addresses, not kernel virtual addresses. Pages must stay resident for the whole transfer. An IOMMU turns those bus addresses into a private, permission-checked view of host memory so that a device cannot touch frames it was never granted. The kernel's DMA API is the interface that hides the mapping, the pinning, and the unmapping; drivers that bypass it re-implement the same rules and usually get one of them wrong.

Next Post Previous Post