Skip to contentSkip to conversation

Use io_uring_register_ring_fd() to skip fd operations

PATCH Applied Sam Li 2 participants 4 messages May 31, 2022

Subsystems (2)
lore
Share & tools
lore

Sam Li

author
#0 lore raw

PATCH v6 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 | 12 +++++++++++-
 meson.build      |  1 +
 2 files changed, 12 insertions(+), 1 deletion(-)
block/io_uring.c +11 -1
index 0b401512b9..d48e472e74 100644
@@ -18,6 +18,7 @@
 #include "qapi/error.h"
 #include "trace.h"
 
+
 /* io_uring ring size */
 #define MAX_ENTRIES 128
 
@@ -434,8 +435,17 @@ LuringState *luring_init(Error **errp)
     }
 
     ioq_init(&s->io_q);
-    return s;
+#ifdef CONFIG_LIBURING_REGISTER_RING_FD
+    if (io_uring_register_ring_fd(&s->ring) < 0) {
+        /*
+         * Only warn about this error: we will fallback to the non-optimized
+         * io_uring operations.
+         */
+        warn_report("failed to register linux io_uring ring file descriptor");
+    }
+#endif
 
+    return s;
 }
 
 void luring_cleanup(LuringState *s)
meson.build +1 -0
index 9ebc00f032..9ffc74d8ed 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>', dependencies:linux_io_uring))
 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 8 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.

Quoted 5 lines
---
 block/io_uring.c | 12 +++++++++++-
 meson.build      |  1 +
 2 files changed, 12 insertions(+), 1 deletion(-)

Thanks, applied to my block tree:
https://gitlab.com/stefanha/qemu/commits/block

Stefan

author
#2 lore raw
Quoted 10 lines · Stefan Hajnoczi <[email protected]> 于2022年5月31日周二 19:31写道:
Quoted 8 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.

Quoted 9 lines
Quoted 5 lines
---
 block/io_uring.c | 12 +++++++++++-
 meson.build      |  1 +
 2 files changed, 12 insertions(+), 1 deletion(-)

Thanks, applied to my block tree:
https://gitlab.com/stefanha/qemu/commits/block

Great! Sorry about the mistake before. I'm flushed.

Quoted 1 line

Stefan

#3 lore raw
Quoted 18 lines · Sam Li
Quoted 16 lines · Stefan Hajnoczi <[email protected]> 于2022年5月31日周二 19:31写道:
Quoted 13 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 | 12 +++++++++++-
 meson.build      |  1 +
 2 files changed, 12 insertions(+), 1 deletion(-)
Quoted 5 lines
Quoted 3 lines

Thanks, applied to my block tree:
https://gitlab.com/stefanha/qemu/commits/block

Great! Sorry about the mistake before. I'm flushed.

Don't worry, it's easy for #ifdef issues to slip through. Two techniques for testing #ifdefs:
1. To check whether compilation works both when the macro is defined and also when it is undefined, edit the #ifdef to force the compiler to take or not take the #ifdef. For example, if #ifdef FOO is compiled but you want to test the case where FOO is undefined, temporarily change it to #ifdef FOOX.
2. Add "#error foo" inside the #ifdef to test whether the compiler is parsing the code. If you expect the #ifdef to be compiled but there is no compilation error then there is a problem.

Stefan