Skip to main content
← CONCEPTS / SYSTEM DESIGN
An end-to-end domain learning journey

Zero-Copy I/O, sendfile
& io_uring.

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.
Start the journey7 sections
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

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.

Full walkthrough · 7 stages
01
STAGE 01 · THE BOTTLENECKfoundation

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.

  1. DMA Read: Disk controller copies blocks into the Kernel Page Cache.
  2. CPU Copy 1: Kernel copies bytes from Page Cache into the User-Space Buffer, incurring a context switch back to user mode.
  3. CPU Copy 2: User process calls write(), switching to kernel mode and copying bytes into the Kernel Socket Buffer (sk_buff).
  4. DMA Write: NIC reads from sk_buff onto 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

NVMe DiskPage CacheUser BufferSocket Buffer
02
STAGE 02 · KERNEL BYPASSfoundation

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

NVMe DiskPage Cachesk_buff DescriptorsNIC Scatter-Gather
03
STAGE 03 · ALTERNATIVESintermediate

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 on read(), yet still triggers minor/major page faults on first access, TLB shootdowns on munmap(), and SIGBUS if 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, and io_uring when you need async random I/O or buffer transformation.
04
STAGE 04 · ASYNC RINGSintermediate

io_uring Shared Submission & Completion Ring Buffers.

Even with zero-copy data movement, issuing millions of small epoll_wait(), recv(), and pread() syscalls per second burns significant CPU—especially after Spectre/Meltdown syscall mitigations.

io_uring solves this by sharing two lock-free ring buffers between user space and the kernel via mmap():

  1. Submission Queue (SQ): User space writes Submission Queue Entries (SQEs) describing reads, writes, accepts, or timeouts and advances the SQ tail pointer.
  2. Completion Queue (CQ): The kernel executes operations asynchronously and writes Completion Queue Entries (CQEs) into the CQ ring.
  3. Registered Buffers & Files: Pre-registering fixed buffers (IORING_REGISTER_BUFFERS) pins pages once, avoiding per-I/O get_user_pages() overhead.

io_uring Shared Memory Ring Architecture

Event LoopSQ Ring (SQEs)CQ Ring (CQEs)Kernel / SQPOLLBatch submit / poll
05
STAGE 05 · PRODUCTION CODEintermediate

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;
}
06
STAGE 06 · REAL-WORLD GOTCHASadvanced

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:

  1. Linux Kernel TLS (kTLS): User space performs the TLS handshake, then passes the negotiated symmetric session keys to the kernel via setsockopt(SOL_TLS, TLS_TX, ...). Subsequent sendfile() calls encrypt directly inside the kernel (or offload encryption to a SmartNIC).
  2. Internal Plaintext Backbone: Keep sendfile() on trusted VPC/mTLS-offloaded sidecar paths while terminating public TLS at an edge proxy.
07
STAGE 07 · FAILURE MODESadvanced

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 with posix_fadvise(POSIX_FADV_WILLNEED) or io_uring async IORING_OP_READ.
  • Cold-Tail Eviction: Sequential cold reads can evict hot tail segments from the page cache; use O_DIRECT or POSIX_FADV_DONTNEED for cold backfill scans.
  • io_uring Kernel Attack Surface: Unprivileged io_uring has historically exposed kernel use-after-free CVEs; many container platforms disable io_uring by default via seccomp unless explicitly allowlisted for trusted data-plane pods.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

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?

Recommended Learning Flow