Skip to contentSkip to conversation

Use io_uring_register_ring_fd() to skip fd operations

PATCH Superseded Sam Li 2 participants 2 messages May 31, 2022

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Sam Li

author
#0 lore raw

PATCH v5 1/1 Use io_uring_register_ring_fd() to skip fd operations

Linux recently added a new io_uring(7) optimization API that QEMU doesn't take advantage of yet. The liburing library that QEMU uses has added a corresponding new API calling io_uring_register_ring_fd(). When this API is called after creating the ring, the io_uring_submit() library function passes a flag to the io_uring_enter(2) syscall allowing it to skip the ring file descriptor fdget()/fdput() operations. This saves some CPU cycles.

---
 block/io_uring.c | 13 ++++++++++++-
 meson.build      |  1 +
 2 files changed, 13 insertions(+), 1 deletion(-)
block/io_uring.c +12 -1
index 0b401512b9..4d691d8373 100644
@@ -17,6 +17,9 @@
 #include "qemu/coroutine.h"
 #include "qapi/error.h"
 #include "trace.h"
+#ifdef CONFIG_LIBURING_REGISTER_RING_FD
+    io_uring_register_ring_fd(&s->ring);
+#endif
 
 /* io_uring ring size */
 #define MAX_ENTRIES 128
@@ -434,8 +437,16 @@ LuringState *luring_init(Error **errp)
     }
 
     ioq_init(&s->io_q);
-    return s;
+    if (io_uring_register_ring_fd(&s->ring) < 0) {
+        /*
+         * Only warn about this error: we will fallback to the non-optimized
+         * io_uring operations.
+         */
+        error_reportf_err(*errp,
+                          "failed to register linux io_uring ring file descriptor");
+    }
 
+    return s;
 }
 
 void luring_cleanup(LuringState *s)
meson.build +1 -0
index 9ebc00f032..927e6ec1a4 100644
@@ -1733,6 +1733,7 @@ config_host_data.set('CONFIG_LIBNFS', libnfs.found())
 config_host_data.set('CONFIG_LIBSSH', libssh.found())
 config_host_data.set('CONFIG_LINUX_AIO', libaio.found())
 config_host_data.set('CONFIG_LINUX_IO_URING', linux_io_uring.found())
+config_host_data.set('CONFIG_LIBURING_REGISTER_RING_FD', cc.has_function('io_uring_register_ring_fd', prefix: '#include <liburing.h>'))
 config_host_data.set('CONFIG_LIBPMEM', libpmem.found())
 config_host_data.set('CONFIG_NUMA', numa.found())
 config_host_data.set('CONFIG_OPENGL', opengl.found())
Signature · 1 line
2.35.3
#1 lore raw
Quoted 20 lines · Sam Li

Linux recently added a new io_uring(7) optimization API that QEMU doesn't take advantage of yet. The liburing library that QEMU uses has added a corresponding new API calling io_uring_register_ring_fd(). When this API is called after creating the ring, the io_uring_submit() library function passes a flag to the io_uring_enter(2) syscall allowing it to skip the ring file descriptor fdget()/fdput() operations. This saves some CPU cycles.

---
 block/io_uring.c | 13 ++++++++++++-
 meson.build      |  1 +
 2 files changed, 13 insertions(+), 1 deletion(-)
block/io_uring.c +0 -0
index 0b401512b9..4d691d8373 100644

block/io_uring.c line 22

Quoted 6 lines
block/io_uring.c +3 -0
@@ -17,6 +17,9 @@
 #include "qemu/coroutine.h"
 #include "qapi/error.h"
 #include "trace.h"
+#ifdef CONFIG_LIBURING_REGISTER_RING_FD
+    io_uring_register_ring_fd(&s->ring);
+#endif

When CONFIG_LIBURING_REGISTER_RING_FD is defined the compiler will report an error here since it is not possible to call a function at the top level of a C source file.

There is already a io_uring_register_ring_fd() call inside luring_init(), so this one can be removed from the patch. Please move the #ifdef into luring_init() so io_uring_register_ring_fd() is only called when the API is available.

Quoted 5 lines
block/io_uring.c +0 -0
 /* io_uring ring size */
 #define MAX_ENTRIES 128
@@ -434,8 +437,16 @@ LuringState *luring_init(Error **errp)
     }

block/io_uring.c LuringState *luring_init(Error **errp) · line 446

Quoted 10 lines
block/io_uring.c +7 -1
 
     ioq_init(&s->io_q);
-    return s;
+    if (io_uring_register_ring_fd(&s->ring) < 0) {
+        /*
+         * Only warn about this error: we will fallback to the non-optimized
+         * io_uring operations.
+         */
+        error_reportf_err(*errp,
+                          "failed to register linux io_uring ring file descriptor");

Returning a non-NULL LuringState while also setting errp is likely to cause problems. QEMU's error_set() functions have assert(*errp == NULL) so the caller will abort later on if another error occurs after luring_init() set errp. The Error API expects errp to be set zero or one times, not more. Please use warn_report() instead, then errp won't be involved.

Quoted 11 lines
block/io_uring.c +2 -0
+    }
 
+    return s;
 }
 
 void luring_cleanup(LuringState *s)
meson.build +0 -0
index 9ebc00f032..927e6ec1a4 100644

meson.build config_host_data.set('CONFIG_LIBNFS', libnfs.found()) · line 1736

Quoted 4 lines
meson.build +1 -0
@@ -1733,6 +1733,7 @@ config_host_data.set('CONFIG_LIBNFS', libnfs.found())
 config_host_data.set('CONFIG_LIBSSH', libssh.found())
 config_host_data.set('CONFIG_LINUX_AIO', libaio.found())
 config_host_data.set('CONFIG_LINUX_IO_URING', linux_io_uring.found())
+config_host_data.set('CONFIG_LIBURING_REGISTER_RING_FD', cc.has_function('io_uring_register_ring_fd', prefix: '#include <liburing.h>'))

I checked build/meson-logs/meson-log.txt after compiling to see if io_uring_register_ring_fd() was detected correctly. Detection always fails because meson is compiling the test program without liburing (there is a linker error because the io_uring_register_ring_fd symbol cannot be found). This can be solved by adding a dependency on linux_io_uring:

config_host_data.set('CONFIG_LIBURING_REGISTER_RING_FD',
cc.has_function('io_uring_register_ring_fd', prefix: '#include <liburing.h>', dependencies: linux_io_uring))

Stefan