Every read() your program makes is a system call: the CPU switches into the kernel, the kernel does the work, and it switches back. Do a million reads, pay for a million round trips into the kernel. And since Spectre and Meltdown, that trip got more expensive. Jens Axboe, who wrote io_uring, called two system calls per I/O “in these post spectre/meltdown days … a serious slowdown” 1.
io_uring is Linux’s answer. The program and the kernel share two ring buffers in memory, and most of the talking happens there. In this article, let’s see how that works.
Problem
read() and write() are synchronous: the call returns when the data is ready 1. If you want to keep doing other work while the disk is busy, you need an async interface. Linux had one, called aio, but Axboe lists why it wasn’t good enough 1:
- It’s only async for
O_DIRECT(unbuffered) I/O. For normal buffered I/O it “behaves in a synchronous manner”. - Even then, submitting can still block, e.g., when the device has no free request slots.
- Each I/O copies 104 bytes of bookkeeping between the program and the kernel.
- Each I/O needs at least two system calls: one to submit, one to wait.
So most programs ended up with their own thread pools to fake async I/O. Axboe’s view: “There is absolutely no reason that applications or libraries continue to need to create private IO offload thread pools” 1.
Two rings
The design started from efficiency: no copies, and as few system calls as possible 1. If you don’t want to copy, the program and the kernel have to share the memory that describes each request. If they share that memory, they need a way to coordinate without locks, because taking a lock shared with the kernel would itself need a system call.
The answer is a single producer, single consumer ring buffer. Two of them 1:
- The submission queue (SQ). The program writes requests into it, and the kernel takes them out.
- The completion queue (CQ). The kernel writes results into it, and the program takes them out.
A ring has an array of slots and two counters. The producer only ever moves the tail, the consumer only ever moves the head. The counters are free-running 32-bit integers, and the slot is the counter masked with the size, which is why the size must be a power of 2. If head equals tail, the ring is empty 1.
Since each counter has exactly one writer, nobody needs a lock. Try it:
What goes in the rings
A request is a submission queue entry (SQE). It’s 64 bytes and looks a lot like the arguments to a system call: an opcode (e.g., IORING_OP_READV), a file descriptor fd, an offset off, a buffer address addr, a length len, and a user_data field 1.
A result is a completion queue entry (CQE): the same user_data, a res, and flags. res is what the equivalent system call would have returned, e.g., the bytes read, or a negative error like -EIO 1.
Completions can arrive in any order 1. That’s what user_data is for. The kernel never touches it, it just copies it from the SQE to the CQE, so you put something in it (often a pointer to your own request) to tell which request finished.
One call for many requests
Writing to the SQ doesn’t tell the kernel anything. For that there’s one system call 1:
int io_uring_enter(unsigned int fd, unsigned int to_submit,
unsigned int min_complete, unsigned int flags,
sigset_t sig);
to_submit says how many new SQEs are ready. min_complete says how many completions to wait for. So one call can submit a whole batch and wait for it. And the program doesn’t even need a system call to check for results: the kernel updates the CQ tail directly, so the program can just look at it 1.
Here is a toy model of 64 reads. Compare one read() per request against io_uring with different batch sizes:
To give you a summary of what you just saw:
- With
read(), it’s one system call per request, and the program waits inside each one. - With io_uring, it’s one call per batch, and the requests in a batch are in flight at the same time.
- With SQPOLL (below), it’s no calls at all while the program keeps busy.
SQPOLL: no system calls at all
io_uring can go one step further. With IORING_SETUP_SQPOLL, the kernel starts a thread that watches the SQ. The program writes an SQE, moves the tail, and the kernel thread picks it up on its own 1.
That thread would burn a CPU while idle, so it goes to sleep after a while, one second by default. When it does, it sets IORING_SQ_NEED_WAKEUP in the SQ flags, and the program has to call io_uring_enter with IORING_ENTER_SQ_WAKEUP to wake it. “As long as the application keeps driving IO … we can effectively perform IO without performing a single system call” 1.
The tricky part: memory ordering
There is one catch with sharing memory between two parties running at the same time. Submitting a request is two steps: fill in the SQE fields, then move the tail. But the CPU is allowed to make those writes visible to others in a different order than you wrote them. “It’s perfectly legal for the processor to reorder these writes in any order it deems the most optimal” 1.
If the new tail becomes visible before the fields do, the kernel reads a half-written request. Try it, then turn on the barrier:
So the rule is: fill in the SQE, write_barrier(), then move the tail 1. The fields can land in any order among themselves, as long as all of them land before the tail. The CQ side mirrors it: the program does a read_barrier() before reading the CQ tail. Depending on the architecture, a barrier may be a no-op, but you still need it in the code 1. That’s why most programs use liburing, the helper library that hides the barriers and the ring setup 1.
How fast?
Axboe’s numbers, with his own warning that they “don’t carry a lot of absolute meaning” and are mostly useful as relative comparisons 1:
- Random 4k reads: about 1.7M IOPS per core with io_uring and polling, against 608K for aio.
- Without polling, io_uring still drove about 1.2M IOPS, around twice aio.
- A no-op request, which measures just the interface: 12M to 20M per second depending on the machine.
Final Thoughts
io_uring isn’t free. A few limits worth knowing:
- You own the memory ordering. Get a barrier wrong with the raw interface and the kernel reads garbage. Use liburing.
- Submit too many requests and the CQ can overflow. By default the CQ is twice the size of the SQ, which helps but “doesn’t completely remove the need” to keep track 1.
- Security. In Google’s kernel exploit bounty program, “60% of the submissions exploited the io_uring component of the Linux kernel”. Google disabled it on ChromeOS and on its production servers, blocks it for Android apps, and considers it “safe only for use by trusted components” 2.
The idea itself is small though: two arrays, four counters, and a rule about who writes which counter.
Footnotes
-
Jens Axboe, Efficient IO with io_uring. Limits of aio in section 1; design goals in 3; the rings in 4; SQE and CQE in 4.1; head, tail and masking in 4.2;
io_uring_enterin 5; memory ordering in 6; liburing in 7; SQPOLL in 8.3; performance in 9. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15 ↩16 ↩17 ↩18 ↩19 ↩20 -
Tamás Koczka, Google Security Blog, Learnings from kCTF VRP’s 42 Linux kernel exploits submissions, June 2023. ↩