Direct memory access

On this page 7

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.

StepActorAction
Buffer preparationDriverAllocates and pins memory for the transfer
Address mappingKernel DMA APIProduces a bus address, often through the IOMMU
Programming the deviceDriverWrites descriptors with address, length and direction
Data movementDevice or DMA controllerWrites data into memory without CPU copies
Completion signalDeviceRaises an interrupt or updates a status field
Unmapping and useDriverSynchronizes caches and hands data to the kernel
Steps of a typical DMA read from a device

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.

MethodCPU involvementBest suited for
Programmed I/O with pollingCPU copies data and checks status in a loopTiny transfers, early boot, simple hardware
Interrupt-driven I/OCPU copies data after each interruptLow-rate devices such as keyboards
Direct memory accessCPU only sets up and completes transfersDisks, network cards, GPUs, audio streams
Comparison of device data transfer methods

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.

VariantHow it worksTypical use
Third-party DMACentral controller moves data for the deviceLegacy ISA floppy and sound devices
Bus mastering DMADevice drives the bus and accesses memory itselfPCI Express storage and network adapters
Scatter-gather DMADevice follows a list of separate memory chunksFragmented buffers and network packets
Burst modeDevice holds the bus for a whole blockFast block transfers
Cycle stealingDevice takes single bus cycles between CPU accessesOlder systems balancing CPU and I/O
DMA variants and their characteristics

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.

MistakeConsequenceFix
Passing a virtual address to the deviceDevice writes to the wrong memoryUse the operating system DMA mapping API
Skipping cache synchronizationCPU reads stale or corrupted dataCall the sync functions before and after transfers
Freeing a buffer during a transferMemory corruption after reuseUnmap and free only after completion
Ignoring the device addressing limitTransfers fail above 4 GBSet the DMA mask correctly
Disabling the IOMMU for speedExposure to DMA attacksKeep it enabled and tune mappings instead
Common DMA mistakes and fixes

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.