Ask a Python service what asynchronous file I/O means and you will usually get the same answer: a thread pool. loop.run_in_executor(), asyncio.to_thread(), or a library that hides one of those behind an await. Node.js does the same thing with libuv's filesystem thread pool. It looks concurrent because the API returns a future, but underneath, every "async" read is a thread parked in pread64().

That is not a criticism. Threads are a perfectly good way to hide blocking I/O, and for a handful of operations they are hard to beat on simplicity. But the model has a ceiling: concurrency costs a stack, a context switch, and one system call per operation. At a few thousand outstanding reads, the overhead becomes the workload.

Linux has had a different answer since 5.1: io_uring. Instead of calling into the kernel for every operation, userspace and the kernel share two ring buffers. You write requests into one, the kernel writes results into the other, and a single system call can hand over an entire batch. This article is about how that actually works, what it does and does not fix, and how to run it without getting bitten by the operational details.

Two rings, one file descriptor

io_uring_setup(2) creates a context and returns a file descriptor. That descriptor is then mmap(2)-ed to expose:

  • The submission queue (SQ), where your program places submission queue entries (SQEs).
  • The completion queue (CQ), where the kernel places completion queue entries (CQEs).
  • On kernels before 5.4, a third mapping for the SQE array; since 5.4 the SQ and CQ rings share one mapping (IORING_FEAT_SINGLE_MMAP).

An SQE is 64 bytes and describes one operation: opcode, file descriptor, offset, buffer pointer, length, flags, and a 64-bit user_data field. A CQE is 16 bytes: the same user_data, a result, and flags. The kernel writes exactly one CQE per SQE in the normal case, and the result is a negated errno, not -1 plus errno:

if (cqe->res < 0) fprintf(stderr, "read failed: %s\n", strerror(-cqe->res)); 

Two consequences follow immediately, and both surprise people:

  1. Completion order is not submission order. The ring is a queue, not a sequence. The man page is explicit that requests may execute and complete in any order, so you correlate work through user_data, not through position.
  2. A full submission queue is not an error. io_uring_get_sqe() returns NULL when there is no room. You submit what you have, reap completions, and try again.

By default the completion ring is twice the size of the submission ring, and cq_entries is rounded up to a power of two. That extra room is not decoration: if the CQ fills, the kernel has to fall back to a slower overflow path. Kernels with IORING_FEAT_NODROP (5.5+) avoid losing events, but you still pay for the detour.

The smallest useful loop

The raw API is three syscalls and two mmap calls. Nobody should write that by hand, which is why liburing exists. A single request looks like this:

#include <liburing.h> struct io_uring ring; struct io_uring_sqe *sqe; struct io_uring_cqe *cqe; char buf[4096]; int ret; ret = io_uring_queue_init(256, &ring, 0); if (ret < 0) { errno = -ret; perror("io_uring_queue_init"); } sqe = io_uring_get_sqe(&ring); if (!sqe) { /* submission queue full: submit and reap first */ } io_uring_prep_read(sqe, fd, buf, sizeof(buf), 0); io_uring_sqe_set_data(sqe, buf); io_uring_submit(&ring); ret = io_uring_wait_cqe(&ring, &cqe); if (ret < 0) { errno = -ret; perror("io_uring_wait_cqe"); } if (cqe->res < 0) fprintf(stderr, "read: %s\n", strerror(-cqe->res)); else printf("read %d bytes\n", cqe->res); io_uring_cqe_seen(&ring, cqe); io_uring_queue_exit(&ring); 

A detail that trips up almost everyone: io_uring_wait_cqe() returns 0 or -errno for the wait itself. The result of your read is in cqe->res. Mixing those two up produces code that reports success while every operation failed.

A complete example: batched file copy

One read tells you nothing about whether io_uring is worth it. The point is keeping several operations in flight without a thread per operation. Here is a complete uring-cp that copies a regular file with eight chunks in flight, using fixed offsets so completion order does not matter.

/* uring-cp.c - copy a file with QD operations in flight. * build: gcc -O2 -Wall -Wextra -o uring-cp uring-cp.c -luring */ #include <errno.h> #include <fcntl.h> #include <liburing.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #define QD 8 #define CHUNK (64 * 1024) enum { SLOT_FREE, SLOT_READ, SLOT_WRITE }; struct slot { char *buf; off_t base; /* offset of this chunk in the file */ off_t cur; /* next byte to read */ size_t want; /* chunk size */ size_t got; /* bytes read so far */ int state; }; static int in_fd, out_fd; static struct io_uring ring; static struct slot slots[QD]; static off_t file_size, next_off; static unsigned inflight; static void fail(const char *what) { perror(what); exit(1); } static void submit_read(struct slot *s) { struct io_uring_sqe *sqe = io_uring_get_sqe(&ring); if (!sqe) { fprintf(stderr, "submission queue full\n"); exit(1); } io_uring_prep_read(sqe, in_fd, s->buf + s->got, (unsigned)(s->want - s->got), (__u64)s->cur); io_uring_sqe_set_data(sqe, s); s->state = SLOT_READ; inflight++; } static void submit_write(struct slot *s) { struct io_uring_sqe *sqe = io_uring_get_sqe(&ring); if (!sqe) { fprintf(stderr, "submission queue full\n"); exit(1); } io_uring_prep_write(sqe, out_fd, s->buf, (unsigned)s->got, (__u64)s->base); io_uring_sqe_set_data(sqe, s); s->state = SLOT_WRITE; inflight++; } int main(int argc, char **argv) { struct stat st; int ret, rc = 0; if (argc != 3) { fprintf(stderr, "usage: %s SRC DST\n", argv[0]); return 2; } in_fd = open(argv[1], O_RDONLY); if (in_fd < 0) fail("open src"); out_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); if (out_fd < 0) fail("open dst"); if (fstat(in_fd, &st) < 0) fail("fstat"); if (!S_ISREG(st.st_mode)) { fprintf(stderr, "%s: not a regular file\n", argv[1]); return 1; } file_size = st.st_size; ret = io_uring_queue_init(QD, &ring, 0); if (ret < 0) { errno = -ret; fail("io_uring_queue_init"); } for (int i = 0; i < QD; i++) { slots[i].buf = malloc(CHUNK); if (!slots[i].buf) fail("malloc"); slots[i].state = SLOT_FREE; } for (;;) { /* Hand free slots to new chunks. */ for (int i = 0; i < QD; i++) { struct slot *s = &slots[i]; if (s->state != SLOT_FREE || next_off >= file_size) continue; s->base = s->cur = next_off; s->want = (size_t)((file_size - next_off < CHUNK) ? (file_size - next_off) : CHUNK); s->got = 0; next_off += (off_t)s->want; submit_read(s); } if (inflight == 0) break; ret = io_uring_submit(&ring); if (ret < 0) { errno = -ret; fail("io_uring_submit"); } struct io_uring_cqe *cqe; ret = io_uring_wait_cqe(&ring, &cqe); if (ret < 0) { errno = -ret; fail("io_uring_wait_cqe"); } struct slot *s = io_uring_cqe_get_data(cqe); int res = cqe->res; io_uring_cqe_seen(&ring, cqe); inflight--; if (s->state == SLOT_READ) { if (res < 0) { errno = -res; fail("read"); } if (res == 0) { fprintf(stderr, "short file: stopped at %lld\n", (long long)(s->base + (off_t)s->got)); rc = 1; } s->got += (size_t)res; s->cur += res; if (s->got < s->want && res > 0) submit_read(s); /* continue after a short read */ else if (s->got > 0) submit_write(s); else s->state = SLOT_FREE; } else { if (res < 0) { errno = -res; fail("write"); } if ((size_t)res != s->got) { fprintf(stderr, "short write at %lld\n", (long long)s->base); return 1; } s->state = SLOT_FREE; } } if (fsync(out_fd) < 0) fail("fsync"); io_uring_queue_exit(&ring); for (int i = 0; i < QD; i++) free(slots[i].buf); close(in_fd); close(out_fd); return rc; } 

Build and run it:

sudo apt install liburing-dev gcc -O2 -Wall -Wextra -o uring-cp uring-cp.c -luring ./uring-cp disk.img copy.img cmp disk.img copy.img && echo identical 

There are two deliberate design choices here. First, every chunk gets a fixed offset before submission, so out-of-order completion cannot corrupt the output. Second, a slot is not reused until its write completes, which keeps at most eight buffers alive. If you strace the copy, the per-operation pread64 and pwrite64 calls are gone; the data path runs through io_uring_enter.

Do not expect miracles on a page-cached file. The kernel still has to copy bytes into your buffer, and for a single small read the setup cost dominates. The win shows up when you have many operations, a deep queue, or a device that can overlap requests.

The flags that actually change behaviour

The default ring is interrupt-driven: you submit, the kernel interrupts, you reap. Several setup flags change that, and each has a cost:

  • IORING_SETUP_SQPOLL starts a kernel thread that polls the submission queue. You can submit without calling io_uring_enter() at all. If the thread goes idle, the kernel sets IORING_SQ_NEED_WAKEUP and you must call io_uring_enter(..., IORING_ENTER_SQ_WAKEUP) to wake it. It costs a busy CPU. Unprivileged use has been allowed since 5.13.
  • IORING_SETUP_IOPOLL busy-polls the device instead of waiting for an interrupt. It only works with O_DIRECT on storage that supports polling; for NVMe, the driver must be loaded with poll_queues.
  • IORING_SETUP_COOP_TASKRUN (5.19) and IORING_SETUP_DEFER_TASKRUN (6.1) reduce the interrupts and task-work round trips used to deliver completions. DEFER_TASKRUN requires IORING_SETUP_SINGLE_ISSUER (6.0) and requires you to call io_uring_enter() with IORING_ENTER_GETEVENTS; if you forget, completions can sit there.
  • Registered buffers and files. io_uring_register_buffers() pins a set of buffers once, and io_uring_register_files() registers descriptors once, so the kernel does not repeat that work for every operation. The fixed variants (io_uring_prep_read_fixed(), IOSQE_FIXED_FILE) then refer to them by index.

For a quick buffer registration:

struct iovec iov = { .iov_base = buf, .iov_len = CHUNK }; if (io_uring_register_buffers(&ring, &iov, 1) < 0) perror("io_uring_register_buffers"); io_uring_prep_read_fixed(sqe, fd, buf, CHUNK, offset, 0); 

Registration is not free either: it pins memory, and it changes the lifetime rules for those buffers. Use it when the same buffers are reused thousands of times, not in a one-shot script.

Where the system call went, and where it did not

io_uring removes the system call per operation. It does not remove the work.

When the kernel cannot issue an operation without blocking, it hands it to io-wq, the ring's asynchronous worker pool. Buffered reads and writes on regular files are the common case: the initial attempt may be non-blocking, but if the page is not in cache the operation ends up in a worker thread. That is still a real improvement for a server, because the worker is not your thread and the queue stays deep, but it is not the same thing as "the disk became non-blocking."

Three practical consequences:

  1. Batching is the point. A ring used for one operation at a time is slower than read(2): you pay setup plus a submit and a wait. Fill the queue.
  2. Stream ordering needs care. For TCP, having multiple outstanding sends or receives on the same socket is generally unsafe unless you link them with IOSQE_IO_LINK or use the multi-shot/bundle facilities. The kernel may reorder execution, and a stream does not tolerate that.
  3. Durability is unchanged. A completion means the kernel accepted the operation, not that the data is on stable storage. fsync() still means what it meant before.

Running it without losing a weekend

This is where most io_uring deployments fail, and it has nothing to do with the API.

Containers block it by default

Docker's default seccomp profile denies io_uring_setup, io_uring_enter, and io_uring_register, citing security vulnerabilities that can be exploited to break out of containers (moby/moby#46762). A program that works perfectly on the host returns EPERM in a stock container. The fix is a custom seccomp profile that allows those three syscalls, not --privileged. The same applies to any runtime that copies that profile.

The kernel has a kill switch

Since Linux 6.6, /proc/sys/kernel/io_uring_disabled controls creation of new rings:

  • 0 — everyone can create rings (the default).
  • 1 — unprivileged processes are blocked unless they have CAP_SYS_ADMIN or are in the group named by io_uring_group.
  • 2 — nobody can create a new ring; io_uring_setup() returns EPERM.

Existing rings keep working, which matters for long-running services during a policy change. To restrict a fleet that does not need io_uring at all:

# /etc/sysctl.d/90-io-uring.conf kernel.io_uring_disabled = 2 

Or allow it only for one group:

# /etc/sysctl.d/90-io-uring.conf kernel.io_uring_disabled = 1 kernel.io_uring_group = 1001 

Verify the value on the running kernel before you debug an application:

cat /proc/sys/kernel/io_uring_disabled uname -r 

Locked memory and queue sizing

io_uring_queue_init() can fail with -ENOMEM even when the machine has plenty of RAM: the rings need locked memory. The liburing man page suggests reducing the entry count or raising the memlock limit. Do not paper over it with a huge queue; size the queue to the number of operations you actually keep in flight.

A full CQ is the other quiet failure. If you see IORING_SQ_CQ_OVERFLOW in the SQ flags, your completion ring is too small or you are not reaping often enough. On kernels with IORING_FEAT_NODROP, events are not lost, but the overflow path is slower and the pressure shows up as latency.

Restricting a ring you do not trust

If you are embedding io_uring in something that runs third-party code, the restrictions API is the part to read. Create the ring with IORING_SETUP_R_DISABLED, register an allowlist with IORING_REGISTER_RESTRICTIONS, and only then call IORING_REGISTER_ENABLE_RINGS. Restrictions can allow specific opcodes and flags, and can require registered files or buffers. It is an allowlist: anything not listed is rejected. This is also how you can give a worker the ring without giving it the whole syscall surface.

When to reach for it

Use io_uring when you have many operations that can overlap and the syscall or thread overhead is measurable: storage engines, proxies, backup and indexing tools that walk large trees, anything doing thousands of small reads. The complete copy above is a teaching example; a real tool would add a bounded work queue, cancellation, and proper error reporting per chunk.

Do not use it for a shell script, a one-shot file read, or a service handling a dozen requests per second. A thread pool is easier to reason about, easier to debug, and fast enough. And if your bottleneck is the device or the network, a different submission API will not create bandwidth.

Measure before and after. If strace -c shows most of the time in pread64 while the CPU is idle, the ring is worth testing. If the device is already saturated, it is not.

Sources