Linux Internals
DMA Explained: How Devices Move Data Without the CPU
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيEvery time your server reads a file from disk or receives a packet from the network, bytes have to travel between a device and main memory. If the CPU had to copy every one of those bytes itself, a fast NVMe drive or a 10 Gbit/s network card would keep several cores busy doing nothing but moving data. Direct Memory Access (DMA) solves this: the device writes to and reads from RAM on its own, and the CPU only sets up the transfer and handles the notification when it finishes. This article explains how DMA works, what bus mastering means, why the IOMMU matters, and how all of it shows up on a Linux VPS.
The problem DMA solves
Before DMA became universal, many devices used programmed I/O (PIO). The CPU read a word from a device register, wrote it to memory, and repeated that loop for the entire transfer. PIO is simple, but it scales badly because the CPU stalls on every slow I/O access.
With DMA the roles change. The driver prepares a buffer in memory, tells the device where it is and how big it is, and then returns to other work. The device moves the data directly across the bus. When it is done, it raises an interrupt (see our article on MSI-X interrupts) so the driver knows the buffer is ready.
Bus mastering: devices that drive the bus
Early PCs had a central DMA controller on the motherboard that performed transfers on behalf of devices. Modern PCI Express devices instead use bus mastering: the device itself issues memory read and write transactions on the bus, exactly as the CPU would. A NIC, an NVMe controller, or a GPU is a bus master that can initiate its own transactions.
In Linux, a driver enables this with pci_set_master(). Without it, the device cannot start DMA at all. You can see the flag in lspci -vv output as BusMaster+ under the device's control register.
Descriptor rings
High-performance devices do not receive one buffer at a time. They work with descriptor rings: circular arrays in memory where each entry describes a buffer (its address, length, and status flags). The driver fills descriptors and advances a "tail" pointer by writing to a device register, often called a doorbell. The device walks the ring, performs DMA for each descriptor, marks it complete, and moves on. This design lets thousands of transfers be in flight with minimal CPU involvement.
Scatter-gather
Memory that looks contiguous to a process is usually scattered across many physical pages. Scatter-gather DMA lets a single I/O operation reference a list of physical segments, so the kernel does not have to copy data into one large contiguous buffer first.
The IOMMU: address translation and protection for devices
A device performing DMA needs an address. Originally that was simply a physical address, which raises two problems. First, a buggy or malicious device could write anywhere in RAM. Second, some devices could only address limited ranges (for example 32-bit addresses), forcing the kernel to use "bounce buffers" in low memory.
The IOMMU (Intel VT-d, AMD-Vi, or the SMMU on Arm) fixes both. It sits between devices and memory and translates I/O virtual addresses (IOVAs) into physical addresses using page tables, much like the CPU's MMU does for processes. A device can only reach memory that has been explicitly mapped for it.
Linux drivers never handle these details directly. They call the DMA mapping API: dma_map_single(), dma_map_sg(), or dma_alloc_coherent(). The API returns a dma_addr_t that is valid from the device's point of view, whether or not an IOMMU is present. That is why one driver works on bare metal and in a VM alike.
IOMMU and virtualization
The IOMMU is also what makes device passthrough (VFIO) safe. When a hypervisor assigns a physical device to a guest, the IOMMU confines that device's DMA to the guest's memory. Without it, a passed-through device could read the host's or other tenants' memory.
NVMe: DMA all the way down
NVMe is a good example of a protocol designed around DMA. The host creates submission and completion queues in memory. To read data, the driver writes a command into a submission queue entry. That entry contains either a PRP list (Physical Region Pages) or an SGL (Scatter Gather List) that points at the destination buffers. The driver rings a doorbell register, the controller fetches the command by DMA, transfers the data by DMA into the buffers, and writes a completion entry by DMA. The CPU never touches the payload.
NVMe supports many queue pairs, typically one per CPU core, so each core can submit I/O without lock contention.
What this means on a VPS
On a KVM-based VPS you normally do not see the physical NVMe drive or NIC directly. You see virtio devices: virtio-blk or virtio-scsi for storage and virtio-net for networking. Virtio uses the same ideas: shared rings ("virtqueues") in guest memory, descriptors that point at buffers, and notifications instead of per-byte copying. The hypervisor or a vhost backend then performs the real DMA on the physical hardware.
A few practical commands help you see this layer:
# List PCI devices and look for virtio controllers
lspci -nn | grep -i virtio
# Check whether bus mastering is enabled for a device
sudo lspci -vv -s 00:04.0 | grep -i busmaster
# See whether an IOMMU is active (mostly relevant on bare metal)
dmesg | grep -iE 'iommu|dmar|amd-vi'
# Inspect block device queues (multi-queue in action)
ls /sys/block/vda/mq/For most workloads you do not need to tune DMA yourself. What matters is choosing a platform with fast storage and modern virtio drivers, and keeping the guest kernel reasonably current so it benefits from multi-queue support. If you are comparing plans, our pricing page lists the storage and CPU options for each location.
When DMA details become relevant
- Benchmarking storage: if
fioshows high CPU usage at moderate IOPS, check that you are using a modern I/O path (io_uringorlibaiowithdirect=1) rather than buffered I/O with extra copies. - Kernel logs: messages like
DMAR: DRHD: handling faulton a dedicated server indicate a device tried to access memory the IOMMU did not map, usually a driver or firmware bug.
FAQ
Does DMA mean the CPU is completely idle during I/O?
No. The CPU still prepares descriptors, maps buffers, rings doorbells, and handles completion interrupts or polling. DMA removes the per-byte copying, which is by far the most expensive part, but some per-request work always remains.
Is the IOMMU required for DMA to work?
No. DMA works without an IOMMU, using physical addresses directly. The IOMMU adds isolation and address translation, which are essential for safe device passthrough and helpful against faulty devices, but many systems run DMA without it.
Can I control DMA settings inside my VPS?
Only to a limited extent. The physical DMA is handled by the host. Inside the guest you can choose efficient I/O interfaces, keep virtio drivers current, and observe queue configuration, but IOMMU and passthrough settings belong to the host platform.