Skip to content
HN On Hacker News ↗

Magic Buffers and io_uring Registered Buffers – Some Assembly Required

▲ 25 points 2 comments by tosh 2w ago HN discussion ↗

Pangram verdict · v3.3

We believe that this document is fully human-written

0 %

AI likelihood · overall

Human
100% human-written 0% AI-generated
SEGMENTS · HUMAN 1 of 1
SEGMENTS · AI 0 of 1
WORD COUNT 174
PEAK AI % 0% · §1
Analyzed
Jun 18
backend: pangram/v3.3
Segments scanned
1 windows
avg 174 words each
Distribution
100 / 0%
human / AI fraction
Verdict
Human
Pangram v3.3

Article text · 174 words · 1 segments analyzed

Human AI-generated
§1 Human · 0%

There’s a really cool little mmap gadget that maps the same underlying memory region into two contiguous virtual memory address ranges. When you write past the end of the first mapped region, the remaining bytes end up in the second mapped region (and at the start of the first one). Fabien Giesen calls this a “Magic Ring Buffer”, which is good enough for me. I wondered whether the virtual memory contortions would survive contact with IO Uring’s registered buffers. It turns out (spoiler alert) that they do, and it’s virtual memory all the way down. I wrote a little test application, and have published it on my Github with some more comments here. Essentially, what the app does is:

Constructs a Magic Buffer (using my MgCircularBuffer implementation here, I know … it’s not circular) Uses io_uring_register_buffers to register the buffer’s “double extent” with the kernel Writes a kdb IPC message across the “seam” in the magic buffer Uses io_uring_prep_write_fixed to send the message to a couple of connected KDB instances

It works as expected.