Skip to contentSkip to conversation

aio-posix: enable io_uring SINGLE_ISSUER and TASKRUN flags

RFC Superseded Stefan Hajnoczi 1 participant 4 messages Jul 24, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover aio-posix: enable io_uring SINGLE_ISSUER and TASKRUN flags

Do not merge this series. The performance effects are not significant. I am sharing this mainly to archive the patches and in case someone has ideas on how to improve this.

Bernd Schubert mentioned io_uring_setup(2) flags that may improve performance:
- IORING_SETUP_SINGLE_ISSUER: optimization when only 1 thread uses an io_uring context
- IORING_SETUP_COOP_TASKRUN: avoids IPIs
- IORING_SETUP_TASKRUN_FLAG: makes COOP_TASKRUN work with userspace CQ ring polling

Suraj Shirvankar already started work on SINGLE_ISSUER in the past:
https://lore.kernel.org/qemu-devel/[email protected]/

Where this differs from Suraj's previous work is that I have worked around the need for the main loop AioContext to be shared by multiple threads (vCPU threads and the migration thread).

Here are the performance numbers for fio bs=4k in a 4 vCPU guest with 1 IOThread using a virtio-blk disk backed by a local NVMe drive:

                      IOPS               IOPS
Benchmark             SINGLE_ISSUER      SINGLE_ISSUER|COOP_TASKRUN|TASKRUN_FLAG
randread  iodepth=1   54,045 (+1.2%)     54,189 (+1.5%)
randread  iodepth=64  318,135 (+0.1%)    315,632 (-0.68%)
randwrite iodepth=1   141,918 (-0.44%)   143,337 (+0.55%)
randwrite iodepth=64  323,948 (-0.015%)  322,755 (-0.38%)

You can find detailed benchmarking results here including the fio output, fio command-line, and guest libvirt domain XML:
https://gitlab.com/stefanha/virt-playbooks/-/tree/io_uring-flags/notebook/fio-output
https://gitlab.com/stefanha/virt-playbooks/-/blob/io_uring-flags/files/fio.sh
https://gitlab.com/stefanha/virt-playbooks/-/blob/io_uring-flags/files/test.xml.j2

Stefan Hajnoczi (3):
iothread: create AioContext in iothread_run()
aio-posix: enable IORING_SETUP_SINGLE_ISSUER
aio-posix: enable IORING_SETUP_COOP_TASKRUN |
IORING_SETUP_TASKRUN_FLAG

 include/system/iothread.h |   1 -
 iothread.c                | 140 +++++++++++++++++++++-----------------
 util/fdmon-io_uring.c     |  26 ++++++-
 3 files changed, 101 insertions(+), 66 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

RFC 1/3 iothread: create AioContext in iothread_run()

The IOThread's AioContext is currently created in iothread_init() where it's easy to propagate errors before spawning the thread that runs iothread_run(). However, this means that aio_context_new() is called from the main loop thread rather than from the IOThread.

In order to use Linux io_uring's IORING_SETUP_SINGLE_ISSUER feature in the next commit, only one thread can use the io_uring context and therefore iothread.c must call aio_context_new() from iothread_run() instead of iothread_init().

Extract the iothread_run() arguments into an IOThreadRunArgs struct where an Error *error field can be used to report back initialization errors. This works pretty well thanks to the init_done_sem semaphore that is already used by iothread_init() to wait for iothread_run() to initialize.

Move iothread_run() further down for proximity with iothread_init() and to avoid adding a function prototype for iothread_set_aio_context_params().

---
 include/system/iothread.h |   1 -
 iothread.c                | 140 +++++++++++++++++++++-----------------
 2 files changed, 78 insertions(+), 63 deletions(-)
include/system/iothread.h +0 -1
index d95c17a645..ec4e798d5e 100644
@@ -29,7 +29,6 @@ struct IOThread {
     bool run_gcontext;          /* whether we should run gcontext */
     GMainContext *worker_context;
     GMainLoop *main_loop;
-    QemuSemaphore init_done_sem; /* is thread init done? */
     bool stopping;              /* has iothread_stop() been called? */
     bool running;               /* should iothread_run() continue? */
     int thread_id;
iothread.c +78 -62

185 lines not on this page · raw message

Signature · 1 line
2.50.1
#2 lore raw

RFC 2/3 aio-posix: enable IORING_SETUP_SINGLE_ISSUER

In reply to Stefan Hajnoczi

IORING_SETUP_SINGLE_ISSUER enables optimizations in the host Linux kernel's io_uring code when the io_uring context is only used from a single thread. This is true is QEMU because io_uring SQEs are submitted from the same thread that processes the CQEs.

---
 util/fdmon-io_uring.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
util/fdmon-io_uring.c +18 -1
index 28b93c8ab9..4798439097 100644
@@ -456,13 +456,30 @@ static const FDMonOps fdmon_io_uring_ops = {
     .add_sqe = fdmon_io_uring_add_sqe,
 };
 
+static inline bool is_creating_iothread(void)
+{
+    return qemu_get_thread_id() != getpid();
+}
+
 void fdmon_io_uring_setup(AioContext *ctx, Error **errp)
 {
     int ret;
+    /* TODO only enable these flags if they are available in the host's kernel headers */
+    unsigned flags = 0;
 
     ctx->io_uring_fd_tag = NULL;
 
-    ret = io_uring_queue_init(FDMON_IO_URING_ENTRIES, &ctx->fdmon_io_uring, 0);
+    /*
+     * The main thread's AioContexts are created from the main loop thread but
+     * may be accessed from multiple threads (e.g. vCPUs or the migration
+     * thread). IOThread AioContexts are only accessed from the IOThread
+     * itself.
+     */
+    if (is_creating_iothread()) {
+        flags = IORING_SETUP_SINGLE_ISSUER;
+    }
+
+    ret = io_uring_queue_init(FDMON_IO_URING_ENTRIES, &ctx->fdmon_io_uring, flags);
     if (ret != 0) {
         error_setg_errno(errp, -ret, "Failed to initialize io_uring");
         return;
Signature · 1 line
2.50.1
#3 lore raw

RFC 3/3 aio-posix: enable IORING_SETUP_COOP_TASKRUN | IORING_SETUP_TASKRUN_FLAG

In reply to Stefan Hajnoczi

The IORING_SETUP_COOP_TASKRUN flag reduces interprocessor interrupts when an io_uring event occurs on a different CPU. The idea is that the QEMU thread will wait for a CQE anyway, so there is no need to interrupt the CPU that it is on.

The IORING_SETUP_TASKRUN_FLAG ensures that QEMU's io_uring CQ ring polling still works with COOP_TASKRUN. The kernel will set a flag in the SQ ring (this is not a typo, the flag is located in the SQ ring even though it pertains to the CQ ring) that can be polled from userspace.

---
 util/fdmon-io_uring.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
util/fdmon-io_uring.c +6 -3
index 4798439097..649dc18907 100644
@@ -428,13 +428,16 @@ static int fdmon_io_uring_wait(AioContext *ctx, AioHandlerList *ready_list,
 
 static bool fdmon_io_uring_need_wait(AioContext *ctx)
 {
+    struct io_uring *ring = &ctx->fdmon_io_uring;
+
     /* Have io_uring events completed? */
-    if (io_uring_cq_ready(&ctx->fdmon_io_uring)) {
+    if (io_uring_cq_ready(ring) ||
+        IO_URING_READ_ONCE(*ring->sq.kflags) & IORING_SQ_TASKRUN) {
         return true;
     }
 
     /* Are there pending sqes to submit? */
-    if (io_uring_sq_ready(&ctx->fdmon_io_uring)) {
+    if (io_uring_sq_ready(ring)) {
         return true;
     }
 
@@ -465,7 +468,7 @@ void fdmon_io_uring_setup(AioContext *ctx, Error **errp)
 {
     int ret;
     /* TODO only enable these flags if they are available in the host's kernel headers */
-    unsigned flags = 0;
+    unsigned flags = IORING_SETUP_COOP_TASKRUN | IORING_SETUP_TASKRUN_FLAG;
 
     ctx->io_uring_fd_tag = NULL;
 
Signature · 1 line
2.50.1