Zero-copy I/O keeps file bytes inside the Linux kernel page cache and streams them straight to a network socket via DMA scatter-gather, while io_uring batches asynchronous syscalls over shared memory ring buffers.
TL;DR30-second simplified summary▾
Serving a file or log segment over a network socket with standard read() and write() syscalls forces the CPU to copy every byte four times across two privilege boundaries: disk to kernel page cache via DMA, kernel page cache to user-space buffer via CPU, user-space buffer to kernel socket buffer via CPU, and socket buffer to the NIC via DMA. At 10–40 Gbps, those CPU copies and four context switches per chunk saturate L1/L2 caches and memory bandwidth long before the disk or NIC hits capacity.
Zero-copy transfer via sendfile() (and splice() between pipes and sockets) keeps the payload entirely inside kernel memory. After the initial DMA read into the kernel page cache, the kernel appends only lightweight buffer descriptors (page pointer, offset, and length) to the socket buffer, allowing the NIC to pull bytes directly from the page cache via DMA scatter-gather. User space never touches the payload, dropping CPU copies from two to zero.
When workloads perform thousands of small random reads, writes, or TLS-encrypted transfers—where sendfile() alone cannot transform bytes in user space—Linux io_uring pairs a memory-mapped Submission Queue (SQ) and Completion Queue (CQ) shared between user space and the kernel. Applications enqueue dozens of I/O descriptors into the SQ ring and reap completions from the CQ ring with a single io_uring_enter() call (or zero syscalls under SQPOLL mode).
Key trade-offs and failure modes: sendfile() cannot modify bytes in transit (breaking user-space TLS unless kTLS is enabled) and blocks if pages are not resident in the page cache; io_uring expands kernel attack surface if unprivileged containers lack seccomp filtering, and fixed-buffer registration requires careful lifecycle pinning so freed user memory is never referenced by in-flight DMA.
From patterns to decisions.
Understand how sendfile(), splice(), and io_uring shared submission/completion ring buffers bypass user-space memory copies and scale I/O-bound brokers, proxies, and storage engines.
Why Traditional read() + write() Saturates CPU and Memory Bandwidth.
When an application calls read(fd, buf, len) followed by write(sock, buf, len), the Linux kernel performs two DMA transfers and two CPU-driven memcpy() passes across the user/kernel boundary.
- DMA Read: Disk controller copies blocks into the Kernel Page Cache.
- CPU Copy 1: Kernel copies bytes from Page Cache into the User-Space Buffer, incurring a context switch back to user mode.
- CPU Copy 2: User process calls
write(), switching to kernel mode and copying bytes into the Kernel Socket Buffer (sk_buff). - DMA Write: NIC reads from
sk_buffonto the wire.
Because buf is only an opaque relay, the CPU wastes cycles polluting its L1/L2 caches and stalling on memory bus bandwidth.
Traditional POSIX read() + write() Data Path
sendfile() and DMA Scatter-Gather Mechanics.
Introduced in Linux 2.1 and refined with scatter-gather DMA (NETIF_F_SG), sendfile(out_fd, in_fd, &offset, count) instructs the kernel to transfer bytes directly from a file descriptor to a socket without returning to user space.
Instead of copying payload bytes into sk_buff, the kernel attaches page descriptors (memory address, offset, and length) pointing directly to the Kernel Page Cache frames. The NIC's DMA engine reads those non-contiguous pages directly from RAM.
- Context switches drop from 4 to 2.
- CPU copies drop from 2 to 0 (assuming NIC scatter-gather support).
- Limitation: Works only when bytes need no user-space transformation (e.g., Kafka plain-text consumer fetches, static media serving, or TLS offload via Linux
kTLS).
Zero-Copy sendfile() with Scatter-Gather DMA
splice(), mmap(), and Page-Fault Traps.
Engineers often consider mmap() a universal zero-copy silver bullet, but mmap() introduces subtle hazards on large or concurrent workloads:
mmap()+write(): Avoids the user-space copy onread(), yet still triggers minor/major page faults on first access, TLB shootdowns onmunmap(), andSIGBUSif the underlying file is truncated concurrently.splice(fd_in, ..., fd_out, ...): Moves page references between any two file descriptors via an internal kernel pipe buffer, enabling zero-copy proxying between two sockets (socket -> pipe -> socket).- Rule of thumb: Use
sendfile()for file-to-socket streaming,splice()for socket-to-socket proxies, andio_uringwhen you need async random I/O or buffer transformation.
Bounded sendfile() Loop with Partial-Write & EAGAIN Handling.
On non-blocking sockets, sendfile() can return fewer bytes than requested (partial write) or fail with EAGAIN/EINTR. Never assume a single call transfers the entire file; track the offset explicitly, retry on EINTR, yield to epoll/io_uring on EAGAIN, and bound retries on transient errors.
#include <errno.h>
#include <sys/sendfile.h>
#include <unistd.h>
ssize_t stream_file_zero_copy(int sock_fd, int file_fd, off_t *offset, size_t remaining) {
int eagain_retries = 0;
const int max_eagain_retries = 5;
while (remaining > 0) {
ssize_t sent = sendfile(sock_fd, file_fd, offset, remaining);
if (sent > 0) {
remaining -= (size_t)sent;
eagain_retries = 0;
continue;
}
if (sent == 0) break; // EOF reached
if (errno == EINTR) continue; // Interrupted by signal, retry immediately
if (errno == EAGAIN || errno == EWOULDBLOCK) {
if (++eagain_retries > max_eagain_retries) {
return -1; // Caller should re-arm epoll EPOLLOUT and resume from *offset
}
usleep(1000 * (1 << (eagain_retries - 1))); // Bounded backoff before yielding
continue;
}
return -1; // Fatal socket or file error: caller closes/cleans up descriptor
}
return 0;
}Why User-Space TLS Breaks Zero-Copy (and How kTLS Restores It).
If your server terminates TLS in user space (standard OpenSSL SSL_write()), the application must read plaintext into user memory to encrypt it—completely defeating sendfile().
Two production workarounds exist:
- Linux Kernel TLS (
kTLS): User space performs the TLS handshake, then passes the negotiated symmetric session keys to the kernel viasetsockopt(SOL_TLS, TLS_TX, ...). Subsequentsendfile()calls encrypt directly inside the kernel (or offload encryption to a SmartNIC). - Internal Plaintext Backbone: Keep
sendfile()on trusted VPC/mTLS-offloaded sidecar paths while terminating public TLS at an edge proxy.
Page Cache Thrashing, Head-of-Line Blocking & io_uring Security.
Zero-copy I/O shifts bottlenecks from CPU memcpy() to kernel memory and syscall boundaries:
- Page Cache Miss Stalls:
sendfile()blocks the calling thread on disk I/O if the target file segment was evicted from the page cache. High-throughput brokers mitigate this withposix_fadvise(POSIX_FADV_WILLNEED)orio_uringasyncIORING_OP_READ. - Cold-Tail Eviction: Sequential cold reads can evict hot tail segments from the page cache; use
O_DIRECTorPOSIX_FADV_DONTNEEDfor cold backfill scans. - io_uring Kernel Attack Surface: Unprivileged
io_uringhas historically exposed kernel use-after-free CVEs; many container platforms disableio_uringby default via seccomp unless explicitly allowlisted for trusted data-plane pods.
Practice: Make the trade-off yourself.
A Kafka broker serves 20 Gbps of plain-text consumer traffic from hot log segments in RAM, and CPU profiling shows 65% of time spent in `copy_user_enhanced_fast_string`. Which change addresses this directly?