Direct memory access (DMA) is a hardware mechanism that lets a peripheral device transfer data directly to or from main memory without the CPU copying every word itself. The processor or driver only sets up the transfer, the device or a DMA controller moves the data, and an interrupt signals completion.
In operating systems, DMA is the foundation of fast I/O for storage controllers, network cards, GPUs and audio hardware. Device drivers must prepare memory buffers, translate addresses, keep caches coherent and, on modern machines, work with an IOMMU that restricts which memory each device may touch.
How a DMA transfer works
A DMA transfer separates control from data movement. The driver allocates a buffer, obtains a device-visible bus address for it, and writes the address, length and direction into device registers or a descriptor in memory. The device then reads or writes memory over the system bus, typically PCI Express, while the CPU executes other code.
| Step | Actor | Action |
|---|---|---|
| Buffer preparation | Driver | Allocates and pins memory for the transfer |
| Address mapping | Kernel DMA API | Produces a bus address, often through the IOMMU |
| Programming the device | Driver | Writes descriptors with address, length and direction |
| Data movement | Device or DMA controller | Writes data into memory without CPU copies |
| Completion signal | Device | Raises an interrupt or updates a status field |
| Unmapping and use | Driver | Synchronizes caches and hands data to the kernel |
Modern devices process rings of descriptors rather than one buffer at a time. A network card, for example, keeps receive and transmit rings in memory, and the driver refills them continuously, so thousands of packets per second flow with few interrupts and no per-byte CPU work.
DMA compared with programmed I/O and polling
The alternative to DMA is programmed I/O, in which the CPU reads or writes each word through port-mapped or memory-mapped I/O registers. Programmed I/O is simple and suits small control transfers, but for bulk data it consumes CPU cycles proportional to the data size. DMA reverses that cost: setup overhead is fixed, and the CPU stays free during the transfer.
| Method | CPU involvement | Best suited for |
|---|---|---|
| Programmed I/O with polling | CPU copies data and checks status in a loop | Tiny transfers, early boot, simple hardware |
| Interrupt-driven I/O | CPU copies data after each interrupt | Low-rate devices such as keyboards |
| Direct memory access | CPU only sets up and completes transfers | Disks, network cards, GPUs, audio streams |
For very small transfers, DMA can be slower than programmed I/O because mapping buffers and handling completion cost more than copying a few bytes. High-performance drivers therefore use copy thresholds, copying short packets and using DMA for larger ones.
Types of DMA and transfer models
DMA hardware comes in two main architectures. Older PCs used a third-party DMA controller, the Intel 8237, which moved data on behalf of ISA devices. Modern PCI Express devices use bus mastering, also called first-party DMA, in which the device itself initiates memory reads and writes.
| Variant | How it works | Typical use |
|---|---|---|
| Third-party DMA | Central controller moves data for the device | Legacy ISA floppy and sound devices |
| Bus mastering DMA | Device drives the bus and accesses memory itself | PCI Express storage and network adapters |
| Scatter-gather DMA | Device follows a list of separate memory chunks | Fragmented buffers and network packets |
| Burst mode | Device holds the bus for a whole block | Fast block transfers |
| Cycle stealing | Device takes single bus cycles between CPU accesses | Older systems balancing CPU and I/O |
Scatter-gather support is especially important because virtual memory places a contiguous buffer in physically scattered page frames. With scatter-gather lists, a device can fill those frames directly, avoiding the need for large physically contiguous allocations.
Address translation, cache coherence and the IOMMU
Devices do not see the CPU’s virtual addresses. A driver must hand the device a bus address, which may equal the physical address or may be translated by an IOMMU. Operating systems provide a DMA API, such as dma_map_single() in Linux or the DMA adapter interfaces in Windows, so drivers do not compute these addresses themselves.
Cache coherence is the second concern. If the CPU holds stale data in its cache while a device writes to memory, the CPU may read old values. x86 systems keep DMA hardware-coherent, but many ARM systems require drivers to flush or invalidate caches around transfers, which the DMA API performs through sync calls.
The IOMMU, branded Intel VT-d or AMD-Vi, gives every device its own address translation tables. It lets the kernel restrict a device to its assigned buffers, enables safe passthrough of devices to virtual machines, and lets 32-bit devices reach memory above 4 GB without bounce buffers.
Security risks and common driver mistakes
Because a bus-mastering device can access memory directly, a malicious or faulty device can read secrets or overwrite kernel memory. These DMA attacks have been demonstrated through Thunderbolt, FireWire and PCI Express ports. Modern systems counter them with IOMMU-based kernel DMA protection, which blocks external devices until a driver maps specific buffers.
Driver bugs around DMA are notoriously hard to reproduce, because they depend on timing, caching and memory placement. The table lists frequent mistakes and their remedies.
| Mistake | Consequence | Fix |
|---|---|---|
| Passing a virtual address to the device | Device writes to the wrong memory | Use the operating system DMA mapping API |
| Skipping cache synchronization | CPU reads stale or corrupted data | Call the sync functions before and after transfers |
| Freeing a buffer during a transfer | Memory corruption after reuse | Unmap and free only after completion |
| Ignoring the device addressing limit | Transfers fail above 4 GB | Set the DMA mask correctly |
| Disabling the IOMMU for speed | Exposure to DMA attacks | Keep it enabled and tune mappings instead |
Understanding it simply
Imagine a manager who needs a large pile of boxes moved from a truck into a warehouse. Carrying every box personally would waste the whole day. Instead, the manager tells a delivery crew where to put the boxes and goes back to other work, and the crew reports back when they are done.
Direct memory access works the same way. The processor tells a device, such as a disk or network card, where in memory the data should go, and the device moves it on its own. This keeps the computer fast, because the processor is not stuck copying data byte by byte.
Applied in practice
A storage engineer evaluates a database server with an NVMe solid-state drive that reads about 3 GB per second. The NVMe controller uses bus mastering DMA with submission and completion queues, so the Linux driver only places commands in a queue that list the destination pages.
During a benchmark, the drive streams data straight into the database’s buffer pages while CPU utilization for I/O stays in single digits. When the engineer compares an older test configuration that used a bounce buffer because the IOMMU mapping was misconfigured, throughput drops noticeably and an extra memory copy appears in the profiler.
After correcting the DMA mask and IOMMU settings, the server returns to direct transfers, and the engineer confirms that full drive throughput is reached while the processors remain available for query execution.