Magic Buffers and io_uring Registered Buffers
摘要
作者发现内存映射的同一个物理区域可映射到两个连续虚拟地址范围,写入第一范围末尾时剩余数据会出现在第二范围起点,Fabien Giesen 称其为 “Magic Ring Buffer”;作者好奇此虚拟内存操作是否与 io_uring 注册缓冲兼容,结果证明兼容;他编写并开源了测试应用:用 MgCircularBuffer 构造 Magic Buffer,用 io_uring 注册缓冲注册其 “双 extent”,通过 “缝隙” 发送 kdb IPC 消息,再用 prep write fixed 将消息发送至连接的 KDB 实例,运行结果符合预期。
荐读理由
Magic Buffer 的 mmap 双区域重叠特性,可注册给 io_uring prep write fixed,直接用于 kdb IPC 消息发送,省掉普通 buffer 拷贝
原文
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
MgCircularBufferimplementation here, I know … it’s not circular)Uses
io_uring_register_buffersto register the buffer’s “double extent” with the kernelWrites a kdb IPC message across the “seam” in the magic buffer
Uses
io_uring_prep_write_fixedto send the message to a couple of connected KDB instances
It works as expected.
这条对你有帮助吗?