Why Sending a File to the NIC Copies Data Four Times

A static file is already sitting on disk, and the server just needs to send it to the client as-is. Application code neither parses nor modifies the file contents, yet with a standard read() followed by write(), the bytes still pass through a user-space buffer, moving back and forth between kernel space and user space.

DMA lets storage devices and network cards exchange data directly with main memory, and zero-copy goes a step further by cutting down CPU-driven memory copies. To understand both, don’t just memorize a slogan about “how many copies and how many switches.” Trace the data path to see which step the hardware handles, which step the CPU handles, and whether user space ever needed the file contents in the first place.

Without DMA, the CPU has to move device data itself

In the early days of Programmed I/O (PIO), the CPU issued disk read and write commands and also moved data through device registers itself. Once data volumes grew, the CPU spent huge chunks of time copying bytes between devices and memory, leaving little time for other useful work.

DMA (Direct Memory Access) changed that division of labor. The CPU or driver sets up the memory region, direction, and length, configures the device or DMA engine, and starts the transfer. From there, hardware moves data directly between the device and main memory. When the transfer finishes, the device notifies the CPU, usually via an interrupt or a completion status visible to polling.

1
2
3
CPU: prepare descriptors, start I/O ─────────────→ handle completion
↑
Device DMA: device buffer ←────→ main memory

Modern storage and network devices usually have their own bus-master DMA capabilities, so you shouldn’t picture the DMA controller as a single separate chip that all data has to pass through. IOMMU address mapping, cache coherence, descriptor management, and completion handling still involve the CPU and the OS. What DMA saves is the CPU copying payload bytes one by one; it does not take the CPU out of I/O altogether.

What happens to file data under plain read() + write()

Take the classic Linux case: sending a regular file over a TCP socket. Assume the requested data isn’t in the page cache yet, both the storage device and the NIC support DMA, and the application loops over read() and write() using its own buffer.

First, read() traps into the kernel. The storage device transfers the file data into the page cache via DMA. Once the data is ready, the CPU copies it from the page cache into the user buffer supplied by the process. On Linux, standard file I/O and file mappings normally go through the page cache, so later reads that hit the cache don’t have to touch the storage device again.

Second, the application calls write() to push that user-space buffer into the socket. The kernel typically copies the data from the user buffer into kernel memory used by the socket send path, after which the NIC reads the outgoing data via DMA and transmits the packets.

1
2
3
4
5
Storage device ──DMA──→ Page cache ──CPU copy──→ User buffer
│
│ write()
↓
NIC ←──DMA── Socket send buffer ←─CPU copy─┘

In this simplified model, a single payload makes four trips:

  1. Storage device to page cache, handled by device DMA.
  2. Page cache to user buffer, handled by the CPU.
  3. User buffer to socket send memory, handled by the CPU.
  4. Socket send memory to the NIC, handled by NIC DMA.

read() and write() are two system calls, and each involves entering the kernel and returning to user space, which is why people usually summarize this as “four user/kernel transitions.” It’s more accurate to call these privilege-level or mode transitions rather than process context switches. Before and after a system call, you may still be running on the exact same thread; only the execution privilege level and code location changed. A scheduler context switch only happens on top of that if the thread gets switched out while waiting on I/O.

These counts aren’t fixed across every environment either. A page cache hit skips the first storage DMA. Different filesystems, devices, network stacks, and hardware offloads change the actual path, and a single syscall may only transfer part of the requested bytes. “Four moves and four mode transitions” is a mental model for the classic path, not a benchmark measurement.

When you’re only forwarding a file, the user buffer adds nothing

If your program needs to parse, compress, encrypt, or rewrite file contents, bringing the bytes into user space makes sense. Serving a static file is different: the application only decides which file to send, where to start, and how many bytes to send. Business logic never looks at the contents.

In that case, the two middle CPU copies mostly exist just to cross the boundary between kernel space and user space:

1
Page cache → User buffer → Socket send memory

The goal of zero-copy is to keep the payload inside kernel-managed pages and pass it downstream via references, mappings, or device descriptors. The “zero” in zero-copy generally means cutting down or eliminating CPU copies of the full payload, especially copies into user space. The physical transfers from disk to memory and from memory to the NIC still happen.

mmap drops the read() copy, but the write() path stays

mmap() maps a file into the process’s virtual address space. When the application touches that address range, page tables map those virtual pages to the underlying file pages. If a page hasn’t been loaded yet, accessing it triggers a page fault, and the kernel reads the file data into the page cache before wiring up the mapping.

1
File page cache ←──page table mapping──→ Process virtual address

No kernel virtual address is handed directly to user space, and no separate full-sized user buffer is allocated. The process simply accesses the same file pages through its own virtual addresses. When the application then calls write(socket, mapped_addr, length), however, the kernel still receives a user-space address and, on the standard send path, copies the payload into kernel memory for the socket.

Under the same assumptions as before, mmap + write cuts the four data moves down to three: storage DMA into the page cache, a CPU copy from the page cache into socket send memory, and NIC DMA out to the wire. It gets rid of the read() copy from the page cache into a user buffer.

How many mode transitions happen depends on how you count. If you create a fresh mapping for every transfer, mmap() and write() are still two syscalls. If the mapping is reused over time, later sends don’t call mmap() again, though touching a page for the first time can still trigger a page fault. Mechanically labeling mmap + write as “always four switches” isn’t accurate.

mmap fits best when the program actually wants address-based access to a file, has good access locality, or can reuse the mapping. Mapping large files also brings trade-offs around virtual address space, page-fault overhead, and edge cases like the file being truncated while mapped. For pure file forwarding where you never inspect the bytes, it still leaves the application holding a file mapping it doesn’t need.

sendfile keeps file-to-socket transfers inside the kernel

Linux has offered sendfile() since kernel 2.2. The caller passes an output file descriptor, an input file descriptor, an offset, and a byte count, and the kernel moves data directly between the two descriptors. There is no need for the application to read() into a user buffer and then write() it back out.

1
ssize_t sent = sendfile(socket_fd, file_fd, &offset, count);

For sending a file to a socket, the standard path simplifies to:

1
Storage device ──DMA──→ Page cache ──kernel handling──→ Socket ──DMA──→ NIC

The application makes a single sendfile() call, so a normal return involves just one trip into the kernel and one trip back to user space. The file contents never pass through a user buffer, skipping the user-space copy from read().

If the send path still needs to copy page-cache data into a separate socket data buffer, that leaves three data moves: one storage DMA, one in-kernel CPU copy, and one NIC DMA. Compared to read + write, that saves one system call and one full CPU copy.

Keep in mind that sendfile() can return fewer bytes than requested, and on a non-blocking socket it may return EAGAIN. The caller still has to loop on the return value to send the rest. A zero-copy API optimizes the data path; it doesn’t change I/O semantics like short writes, error handling, or backpressure.

With scatter-gather NICs, the socket only needs page references

A NIC that supports scatter-gather DMA can read data from multiple non-contiguous physical memory regions using a list of (memory address, length) descriptors. When sending a file, the kernel no longer has to copy page-cache contents into a single contiguous socket buffer. Instead, the socket send structures hold references to those pages, and the NIC pulls the bytes straight from them via DMA.

1
2
3
4
5
Storage device ──DMA──→ Page cache pages
│
├─ Kernel builds page refs & TX descriptors
↓
NIC ←─────DMA──────┘

In this model, the full file payload only moves physically twice: from storage into main memory, and from main memory into the NIC. The CPU still handles the system call, maintains page references, builds protocol headers, submits transmit descriptors, and handles completions, but it no longer copies the file payload from one memory buffer to another. Whether checksums and segmentation still consume CPU cycles depends on NIC offload features and protocol settings.

That’s the most accurate meaning of “zero-copy” in file transmission: zero payload copies into user space, and on an ideal send path, zero CPU copies of the payload at all, while DMA still performs the two unavoidable device transfers.

Comparing the paths side by side

The numbers below stick to the same assumptions: data starts outside the page cache, exits through a DMA-capable NIC, and only full-payload data movements are counted. Specific kernels and hardware may behave differently.

Approach System call path Data moves Full-payload CPU copies
read + write Two syscalls, typically four mode transitions 4 2
mmap + write Map then write; transitions depend on mapping reuse 3 1
sendfile (send path still copies) One syscall, typically two mode transitions 3 1
sendfile + scatter-gather DMA One syscall, typically two mode transitions 2 0

This table only describes one classic path: forwarding a file untouched. Receive-side zero-copy, user-generated data, disk-to-disk copies, pipe forwarding, and TLS-encrypted sends all come with different interfaces and constraints. Linux also provides splice(), copy_file_range(), and MSG_ZEROCOPY, each built for different sources and destinations. You can’t swap one for another just because they all carry the “zero-copy” label.

Zero-copy fits paths that don’t touch the data

When the application’s job is limited to authorization, picking a file, and choosing a byte range, sendfile() stops file bytes from taking a pointless detour through user space. Static file servers, download endpoints, and some proxy paths generally benefit from it. When the file is already hot in the page cache, the storage transfer disappears too, and the win comes mainly from cutting CPU copies and syscall overhead.

If your application has to modify the payload, copies may be unavoidable. Application-level compression, response assembly, or encryption without kernel offload all need the CPU (or an accelerator) to read bytes and produce new ones. In those cases, measuring actual CPU utilization, throughput, page faults, and cache hit rates under real traffic matters far more than picking an API just because it’s called “zero-copy.”

DMA and zero-copy solve two adjacent layers of the same problem: DMA cuts CPU byte-copying between devices and memory, and zero-copy cuts unnecessary CPU copies between memory buffers. Tracing the data path step by step to see who owns the data and who moves it will tell you far more about whether an optimization fits your system than memorizing “four copies down to two.”

References