io_uring fixes
cover io_uring fixes
Hi,
Patch 1 here is the real meat of this, patch 2 is just a slight improvement. For patch 1, it can literally yield a 50-80x improvement on the io_uring side for idle systems, where ppoll() ends up sleeping for 500 msec while there's IO to submit! I noticed this running the io_uring regression tests in a vm, where I use a variety of block devices for some of the tests. They would often randomly time out on AHCI devices, while running them on a virtio-blk or nvme device would finish in one second or so. I then wrote a reproducer to try and grok this and had claude dive into this, which helped me better grasp the various event loops.
Please take a look and tell me what you think. Some variant of patch 1 should definitely be considered, but let me know if this is the right approach. I can easily test anything.
Also note - this seems to trigger more easily or consistently on aarch64, which is where I run most of my local/immediate testing.
util/fdmon-io_uring.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
Signature · 1 line
Jens Axboe
PATCH 1/2 fdmon-io_uring: notify main loop when SQEs are queued
When a vCPU thread handles MMIO (holding BQL), aio_co_enter() runs the block I/O coroutine inline on the vCPU thread because qemu_get_current_aio_context() returns the main AioContext when BQL is held. The coroutine calls luring_co_submit() which queues an SQE via fdmon_io_uring_add_sqe(), but the actual io_uring_submit() only happens in gsource_prepare() on the main loop thread.
Since the coroutine ran inline (not via aio_co_schedule()), no BH is scheduled and aio_notify() is never called. The main loop remains asleep in ppoll() with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.
Fix this by calling aio_notify() after queuing the SQE. This wakes the main loop via the eventfd so it can run gsource_prepare() and submit the pending SQE promptly.
This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few usleep() to simulate processing of read data, it's very noticeable. The below example reads 128MB with O_DIRECT in 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on /dev/sda yields:
time sudo ./iotest /dev/sda
________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 millis
while on a virtio-blk or NVMe device we get:
time sudo ./iotest /dev/vdb
________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millis
time sudo ./iotest /dev/nvme0n1
________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 millis
where the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest /dev/sda
________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millis
as now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest /dev/sda
________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millis
--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)
util/fdmon-io_uring.c +8 -0
@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx,trace_fdmon_io_uring_add_sqe (ctx, opaque, sqe->opcode, sqe->fd, sqe->off, cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }static void fdmon_special_cqe_handler (CqeHandler *cqe_handler)
Signature · 1 line
2.51.0
Re: [PATCH 1/2] fdmon-io_uring: notify main loop when SQEs are queued
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.
Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of aio_add_sqe()).
Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,
util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }
Makes sense to me.
At first I wondered if we should use defer_call() for the aio_notify() to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, the aio_notify() shouldn't make a difference anyway because we're already in the right thread.
I suppose the other variation could be have another io_uring_enter() call here (but then probably really through defer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.
Fiona, does this fix your case, too?
Kevin
Re: [PATCH 1/2] fdmon-io_uring: notify main loop when SQEs are queued
Am 13.02.26 um 5:05 PM schrieb Kevin Wolf:
Quoted 111 lines
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }Makes sense to me.
At first I wondered if we should use
defer_call()for theaio_notify()to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, theaio_notify()shouldn't make a difference anyway because we're already in the right thread.
Quoted 7 lines
I suppose the other variation could be have another
io_uring_enter()call here (but then probably really throughdefer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.Fiona, does this fix your case, too?
Yes, it does fix my issue [0] and the second patch gives another small improvement :)
Would it be slightly cleaner to have aio_add_sqe() call aio_notify() itself? Since aio-posix.c calls downwards into fdmon-io_uring.c, it would feel nicer to me to not have fdmon-io_uring.c call "back up". I guess it also depends on whether we expect another future fdmon implementation with .add_sqe() to also benefit from it.
[0]:
https://lore.kernel.org/qemu-devel/[email protected]/
Best Regards,
Fiona
Re: [PATCH 1/2] fdmon-io_uring: notify main loop when SQEs are queued
Quoted 117 lines · Fiona Ebner
Am 13.02.26 um 5:05 PM schrieb Kevin Wolf:
Quoted 116 lines
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }Makes sense to me.
At first I wondered if we should use
defer_call()for theaio_notify()to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, theaio_notify()shouldn't make a difference anyway because we're already in the right thread.I suppose the other variation could be have another
io_uring_enter()call here (but then probably really throughdefer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.
It's possible to call io_uring_enter(). QEMU currently doesn't use IORING_SETUP_SINGLE_ISSUER, so it's okay for multiple threads to call io_uring_enter() on the same io_uring fd.
I experimented with IORING_SETUP_SINGLE_ISSUER (as well as IORING_SETUP_COOP_TASKRUN and IORING_SETUP_TASKRUN_FLAG) in the past and didn't measure a performance improvement:
https://lore.kernel.org/qemu-devel/[email protected]/
Jens, any advice regarding these flags?
Stefan
Re: [PATCH 1/2] fdmon-io_uring: notify main loop when SQEs are queued
Quoted 119 lines · Stefan Hajnoczi
Quoted 117 lines · Fiona Ebner
Am 13.02.26 um 5:05 PM schrieb Kevin Wolf:
Quoted 116 lines
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }Makes sense to me.
At first I wondered if we should use
defer_call()for theaio_notify()to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, theaio_notify()shouldn't make a difference anyway because we're already in the right thread.I suppose the other variation could be have another
io_uring_enter()call here (but then probably really throughdefer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.
Quoted 3 lines
It's possible to call
io_uring_enter(). QEMU currently doesn't useIORING_SETUP_SINGLE_ISSUER, so it's okay for multiple threads to callio_uring_enter()on the sameio_uringfd.
I would not recommend that, see below.
Quoted 6 lines
I experimented with
IORING_SETUP_SINGLE_ISSUER(as well asIORING_SETUP_COOP_TASKRUNandIORING_SETUP_TASKRUN_FLAG) in the past and didn't measure a performance improvement:
https://lore.kernel.org/qemu-devel/[email protected]/Jens, any advice regarding these flags?
None other than "yes you should use them" - it's an expanding area of "let's make that faster", so if you tested something older, then that may be why as we didn't have a lot earlier. We're toying with getting rid of the uring_lock for SINGLE_ISSUER, for example.
Hence I think having multiple threads do enter is a design mistake, and one that might snowball down the line and make it harder to step back and make SINGLE_ISSUER work for you. Certain features also end up being gated behing DEFER_TASKRUN, which requires SINGLE_ISSUER as well.
tldr - don't have multiple threads do enter on the same ring, ever, if it can be avoided. It's a design mistake.
Signature · 1 line
Jens Axboe
Re: [PATCH 1/2] fdmon-io_uring: notify main loop when SQEs are queued
Quoted 138 lines · Jens Axboe
Quoted 119 lines · Stefan Hajnoczi
Quoted 117 lines · Fiona Ebner
Am 13.02.26 um 5:05 PM schrieb Kevin Wolf:
Quoted 116 lines
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }Makes sense to me.
At first I wondered if we should use
defer_call()for theaio_notify()to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, theaio_notify()shouldn't make a difference anyway because we're already in the right thread.I suppose the other variation could be have another
io_uring_enter()call here (but then probably really throughdefer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.Quoted 3 lines
It's possible to call
io_uring_enter(). QEMU currently doesn't useIORING_SETUP_SINGLE_ISSUER, so it's okay for multiple threads to callio_uring_enter()on the sameio_uringfd.I would not recommend that, see below.
Quoted 6 lines
I experimented with
IORING_SETUP_SINGLE_ISSUER(as well asIORING_SETUP_COOP_TASKRUNandIORING_SETUP_TASKRUN_FLAG) in the past and didn't measure a performance improvement:
https://lore.kernel.org/qemu-devel/[email protected]/Jens, any advice regarding these flags?
None other than "yes you should use them" - it's an expanding area of "let's make that faster", so if you tested something older, then that may be why as we didn't have a lot earlier. We're toying with getting rid of the
uring_lockforSINGLE_ISSUER, for example.
Quoted 7 lines
Hence I think having multiple threads do enter is a design mistake, and one that might snowball down the line and make it harder to step back and make
SINGLE_ISSUERwork for you. Certain features also end up being gated behingDEFER_TASKRUN, which requiresSINGLE_ISSUERas well.tldr - don't have multiple threads do enter on the same ring, ever, if it can be avoided. It's a design mistake.
That's useful information, thanks. I will resurrect the patches to add modern io_uring_setup() flags and we'll document the assumption that only one thread invokes io_uring_enter().
Stefan
Re: [PATCH 1/2] fdmon-io_uring: notify main loop when SQEs are queued
In reply to Fiona EbnerQuoted 120 lines · Fiona Ebner
Am 13.02.26 um 5:05 PM schrieb Kevin Wolf:
Quoted 111 lines
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }Makes sense to me.
At first I wondered if we should use
defer_call()for theaio_notify()to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, theaio_notify()shouldn't make a difference anyway because we're already in the right thread.Quoted 7 lines
I suppose the other variation could be have another
io_uring_enter()call here (but then probably really throughdefer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.Fiona, does this fix your case, too?
Quoted 8 lines
Yes, it does fix my issue [0] and the second patch gives another small improvement :)
Would it be slightly cleaner to have
aio_add_sqe()callaio_notify()itself? Sinceaio-posix.ccalls downwards intofdmon-io_uring.c, it would feel nicer to me to not havefdmon-io_uring.ccall "back up". I guess it also depends on whether we expect another future fdmon implementation with .add_sqe() to also benefit from it.
Calling aio_notify() from aio-posix.c:aio_add_sqe() sounds better to me because fdmon-io_uring.c has to be careful about calling aio_*() APIs to avoid loops.
Stefan
Quoted 7 lines
[0]:
https://lore.kernel.org/qemu-devel/[email protected]/Best Regards,
Fiona
Re: [PATCH 1/2] fdmon-io_uring: notify main loop when SQEs are queued
Quoted 130 lines · Stefan Hajnoczi
Quoted 120 lines · Fiona Ebner
Am 13.02.26 um 5:05 PM schrieb Kevin Wolf:
Quoted 111 lines
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }Makes sense to me.
At first I wondered if we should use
defer_call()for theaio_notify()to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, theaio_notify()shouldn't make a difference anyway because we're already in the right thread.Quoted 7 lines
I suppose the other variation could be have another
io_uring_enter()call here (but then probably really throughdefer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.Fiona, does this fix your case, too?
Quoted 8 lines
Yes, it does fix my issue [0] and the second patch gives another small improvement :)
Would it be slightly cleaner to have
aio_add_sqe()callaio_notify()itself? Sinceaio-posix.ccalls downwards intofdmon-io_uring.c, it would feel nicer to me to not havefdmon-io_uring.ccall "back up". I guess it also depends on whether we expect another future fdmon implementation with .add_sqe() to also benefit from it.
Quoted 3 lines
Calling
aio_notify()from aio-posix.c:aio_add_sqe() sounds better to me becausefdmon-io_uring.chas to be careful about calling aio_*() APIs to avoid loops.
Would anyone care to make that edit? I'm on a plane and gone for a bit, so won't get back to this for the next week. But I would love to see a fix go in, as this issue has been plaguing me with test timeouts for quite a while on the CI front. And seems like I'm not alone, if the patches fix Fiona's issues as well.
Signature · 1 line
Jens Axboe
PATCH v2 1/1 aio-posix: notify main loop when SQEs are queued
Quoted 135 lines · Jens Axboe
Quoted 130 lines · Stefan Hajnoczi
Quoted 120 lines · Fiona Ebner
Am 13.02.26 um 5:05 PM schrieb Kevin Wolf:
Quoted 111 lines
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }Makes sense to me.
At first I wondered if we should use
defer_call()for theaio_notify()to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, theaio_notify()shouldn't make a difference anyway because we're already in the right thread.Quoted 7 lines
I suppose the other variation could be have another
io_uring_enter()call here (but then probably really throughdefer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.Fiona, does this fix your case, too?
Quoted 8 lines
Yes, it does fix my issue [0] and the second patch gives another small improvement :)
Would it be slightly cleaner to have
aio_add_sqe()callaio_notify()itself? Sinceaio-posix.ccalls downwards intofdmon-io_uring.c, it would feel nicer to me to not havefdmon-io_uring.ccall "back up". I guess it also depends on whether we expect another future fdmon implementation with .add_sqe() to also benefit from it.Quoted 3 lines
Calling
aio_notify()from aio-posix.c:aio_add_sqe() sounds better to me becausefdmon-io_uring.chas to be careful about calling aio_*() APIs to avoid loops.
Quoted 5 lines
Would anyone care to make that edit? I'm on a plane and gone for a bit, so won't get back to this for the next week. But I would love to see a fix go in, as this issue has been plaguing me with test timeouts for quite a while on the CI front. And seems like I'm not alone, if the patches fix Fiona's issues as well.
Still on a plane but tested this one and it works for me too. Does seem like a better approach, rather than stuff it in the fdmon part.
Feel free to run with this one and also to update the commit message if you want. Thanks!
commit a8a94e7a05964d470b8fba50c9d4769489c21752
Author: Jens Axboe <[email protected]> Date: Fri Feb 13 06:52:14 2026 -0700
aio-posix: notify main loop when SQEs are queued
When a vCPU thread handles MMIO (holding BQL), aio_co_enter() runs the block I/O coroutine inline on the vCPU thread because qemu_get_current_aio_context() returns the main AioContext when BQL is held. The coroutine calls luring_co_submit() which queues an SQE via fdmon_io_uring_add_sqe(), but the actual io_uring_submit() only happens in gsource_prepare() on the main loop thread.
Since the coroutine ran inline (not via aio_co_schedule()), no BH is scheduled and aio_notify() is never called. The main loop remains asleep in ppoll() with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.
Fix this by calling aio_notify() after queuing the SQE. This wakes the main loop via the eventfd so it can run gsource_prepare() and submit the pending SQE promptly.
This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few usleep() to simulate processing of read data, it's very noticeable. The below example reads 128MB with O_DIRECT in 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on /dev/sda yields:
time sudo ./iotest /dev/sda
________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 millis
while on a virtio-blk or NVMe device we get:
time sudo ./iotest /dev/vdb
________________________________________________________
Executed in 1.25 secs fish external
usr time 1.40 millis 0.30 millis 1.10 millis
sys time 17.61 millis 1.43 millis 16.18 millis
time sudo ./iotest /dev/nvme0n1
________________________________________________________
Executed in 1.26 secs fish external
usr time 6.11 millis 0.52 millis 5.59 millis
sys time 13.94 millis 1.50 millis 12.43 millis
where the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest /dev/sda
________________________________________________________
Executed in 1.23 secs fish external
usr time 2.70 millis 39.00 micros 2.66 millis
sys time 4.97 millis 977.00 micros 3.99 millis
as now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest /dev/sda
________________________________________________________
Executed in 1.30 secs fish external
usr time 2.14 millis 0.14 millis 2.00 millis
sys time 16.93 millis 1.16 millis 15.76 millis
util/aio-posix.c +8 -0
@@ -813,5 +813,13 @@ void aio_add_sqe(void (*prep_sqe)(struct io_uring_sqe *sqe, void *opaque), {AioContext *ctx =qemu_get_current_aio_context (); ctx->fdmon_ops->add_sqe (ctx, prep_sqe, opaque, cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }#endif /* CONFIG_LINUX_IO_URING */
Signature · 1 line
Jens Axboe
Stefan Hajnoczi
AppliedRe: [PATCH v2] aio-posix: notify main loop when SQEs are queued
Quoted 223 lines · Jens Axboe
Quoted 135 lines · Jens Axboe
Quoted 130 lines · Stefan Hajnoczi
Quoted 120 lines · Fiona Ebner
Am 13.02.26 um 5:05 PM schrieb Kevin Wolf:
Quoted 111 lines
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }Makes sense to me.
At first I wondered if we should use
defer_call()for theaio_notify()to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, theaio_notify()shouldn't make a difference anyway because we're already in the right thread.Quoted 7 lines
I suppose the other variation could be have another
io_uring_enter()call here (but then probably really throughdefer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.Fiona, does this fix your case, too?
Quoted 8 lines
Yes, it does fix my issue [0] and the second patch gives another small improvement :)
Would it be slightly cleaner to have
aio_add_sqe()callaio_notify()itself? Sinceaio-posix.ccalls downwards intofdmon-io_uring.c, it would feel nicer to me to not havefdmon-io_uring.ccall "back up". I guess it also depends on whether we expect another future fdmon implementation with .add_sqe() to also benefit from it.Quoted 3 lines
Calling
aio_notify()from aio-posix.c:aio_add_sqe() sounds better to me becausefdmon-io_uring.chas to be careful about calling aio_*() APIs to avoid loops.Quoted 5 lines
Would anyone care to make that edit? I'm on a plane and gone for a bit, so won't get back to this for the next week. But I would love to see a fix go in, as this issue has been plaguing me with test timeouts for quite a while on the CI front. And seems like I'm not alone, if the patches fix Fiona's issues as well.
Still on a plane but tested this one and it works for me too. Does seem like a better approach, rather than stuff it in the fdmon part.
Feel free to run with this one and also to update the commit message if you want. Thanks!
commit
a8a94e7a05964d470b8fba50c9d4769489c21752Author: Jens Axboe <[email protected]> Date: Fri Feb 13 06:52:14 2026 -0700aio-posix: notify main loop when SQEs are queued
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external
usr time 1.40 millis 0.30 millis 1.10 millis
sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external
usr time 6.11 millis 0.52 millis 5.59 millis
sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external
usr time 2.70 millis 39.00 micros 2.66 millis
sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
Quoted 8 lines
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external
usr time 2.14 millis 0.14 millis 2.00 millis
sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>
Thanks, applied to my block tree together with Patch 2 from v1:
https://gitlab.com/stefanha/qemu/commits/block
Stefan
Re: [PATCH v2] aio-posix: notify main loop when SQEs are queued
Quoted 2 lines · Stefan Hajnoczi
Thanks, applied to my block tree together with Patch 2 from v1:
https://gitlab.com/stefanha/qemu/commits/block
Great, thank you!
Signature · 1 line
Jens Axboe
Re: [PATCH v2] aio-posix: notify main loop when SQEs are queued
In reply to Jens AxboeAm 18.02.2026 um 17:41 hat Jens Axboe geschrieben:
Quoted 237 lines · Jens Axboe
Quoted 135 lines · Stefan Hajnoczi
Quoted 130 lines · Fiona Ebner
Quoted 120 lines
Am 13.02.26 um 5:05 PM schrieb Kevin Wolf:
Quoted 111 lines
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }Makes sense to me.
At first I wondered if we should use
defer_call()for theaio_notify()to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, theaio_notify()shouldn't make a difference anyway because we're already in the right thread.Quoted 7 lines
I suppose the other variation could be have another
io_uring_enter()call here (but then probably really throughdefer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.Fiona, does this fix your case, too?
Quoted 8 lines
Yes, it does fix my issue [0] and the second patch gives another small improvement :)
Would it be slightly cleaner to have
aio_add_sqe()callaio_notify()itself? Sinceaio-posix.ccalls downwards intofdmon-io_uring.c, it would feel nicer to me to not havefdmon-io_uring.ccall "back up". I guess it also depends on whether we expect another future fdmon implementation with .add_sqe() to also benefit from it.Quoted 3 lines
Calling
aio_notify()from aio-posix.c:aio_add_sqe() sounds better to me becausefdmon-io_uring.chas to be careful about calling aio_*() APIs to avoid loops.Quoted 5 lines
Would anyone care to make that edit? I'm on a plane and gone for a bit, so won't get back to this for the next week. But I would love to see a fix go in, as this issue has been plaguing me with test timeouts for quite a while on the CI front. And seems like I'm not alone, if the patches fix Fiona's issues as well.
Still on a plane but tested this one and it works for me too. Does seem like a better approach, rather than stuff it in the fdmon part.
Feel free to run with this one and also to update the commit message if you want. Thanks!
commit
a8a94e7a05964d470b8fba50c9d4769489c21752Author: Jens Axboe <[email protected]> Date: Fri Feb 13 06:52:14 2026 -0700aio-posix: notify main loop when SQEs are queued
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external
usr time 1.40 millis 0.30 millis 1.10 millis
sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external
usr time 6.11 millis 0.52 millis 5.59 millis
sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external
usr time 2.70 millis 39.00 micros 2.66 millis
sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external
usr time 2.14 millis 0.14 millis 2.00 millis
sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>util/aio-posix.c +0
-0
util/aio-posix.c void aio_add_sqe(void (*prep_sqe)(struct io_uring_sqe *sqe, void *opaque), · line 819
Quoted 7 lines
util/aio-posix.c +4
-0@@ -813,5 +813,13 @@ void aio_add_sqe(void (*prep_sqe)(struct io_uring_sqe *sqe, void *opaque), {AioContext *ctx =qemu_get_current_aio_context (); ctx->fdmon_ops->add_sqe (ctx, prep_sqe, opaque, cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the
I think the comment could even be more generic here. This is not specific to coroutines, but the scenario is just that a vCPU thread holding the BQL performs I/O.
util/aio-posix.c void aio_add_sqe(void (*prep_sqe)(struct io_uring_sqe *sqe, void *opaque), · line 825
Quoted 6 lines
util/aio-posix.c +4
-0+* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ + aio_notify(ctx); }#endif /* CONFIG_LINUX_IO_URING */
With or without a changed comment to that effect:
Re: [PATCH v2] aio-posix: notify main loop when SQEs are queued
Quoted 233 lines · Kevin Wolf
Am 18.02.2026 um 17:41 hat Jens Axboe geschrieben:
Quoted 232 lines · Jens Axboe
Quoted 135 lines · Stefan Hajnoczi
Quoted 130 lines · Fiona Ebner
Quoted 120 lines
Am 13.02.26 um 5:05 PM schrieb Kevin Wolf:
Quoted 111 lines
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).Quoted 80 lines
Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external usr time 1.40 millis 0.30 millis 1.10 millis sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external usr time 6.11 millis 0.52 millis 5.59 millis sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external usr time 2.70 millis 39.00 micros 2.66 millis sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external usr time 2.14 millis 0.14 millis 2.00 millis sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 8 ++++++++ 1 file changed, 8 insertions(+)util/fdmon-io_uring.c +0
-0@@ -181,6 +181,14 @@ static void fdmon_io_uring_add_sqe(AioContext *ctx, trace_fdmon_io_uring_add_sqe(ctx, opaque, sqe->opcode, sqe->fd, sqe->off,util/fdmon-io_uring.c static void fdmon_io_uring_add_sqe(AioContext *ctx, · line 192
Quoted 10 lines
util/fdmon-io_uring.c +8
-0cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the +* actual io_uring_submit() only happens in gsource_prepare(). Without +* this notify, ppoll() can sleep up to 499ms before submitting. +*/ +aio_notify (ctx); }Makes sense to me.
At first I wondered if we should use
defer_call()for theaio_notify()to batch the submission, but of course holding the BQL will already take care of that. And in iothreads where there is no BQL, theaio_notify()shouldn't make a difference anyway because we're already in the right thread.Quoted 7 lines
I suppose the other variation could be have another
io_uring_enter()call here (but then probably really throughdefer_call()) to avoid waiting for another CPU to submit the request in its main loop. But I don't really have an intuition if that would make things better or worse in the common case.Fiona, does this fix your case, too?
Quoted 8 lines
Yes, it does fix my issue [0] and the second patch gives another small improvement :)
Would it be slightly cleaner to have
aio_add_sqe()callaio_notify()itself? Sinceaio-posix.ccalls downwards intofdmon-io_uring.c, it would feel nicer to me to not havefdmon-io_uring.ccall "back up". I guess it also depends on whether we expect another future fdmon implementation with .add_sqe() to also benefit from it.Quoted 3 lines
Calling
aio_notify()from aio-posix.c:aio_add_sqe() sounds better to me becausefdmon-io_uring.chas to be careful about calling aio_*() APIs to avoid loops.Quoted 5 lines
Would anyone care to make that edit? I'm on a plane and gone for a bit, so won't get back to this for the next week. But I would love to see a fix go in, as this issue has been plaguing me with test timeouts for quite a while on the CI front. And seems like I'm not alone, if the patches fix Fiona's issues as well.
Still on a plane but tested this one and it works for me too. Does seem like a better approach, rather than stuff it in the fdmon part.
Feel free to run with this one and also to update the commit message if you want. Thanks!
commit
a8a94e7a05964d470b8fba50c9d4769489c21752Author: Jens Axboe <[email protected]> Date: Fri Feb 13 06:52:14 2026 -0700aio-posix: notify main loop when SQEs are queued
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Since the coroutine ran inline (not via
aio_co_schedule()), no BH is scheduled andaio_notify()is never called. The main loop remains asleep inppoll()with up to a 499ms timeout, leaving the SQE unsubmitted until the next timer fires.Fix this by calling
aio_notify()after queuing the SQE. This wakes the main loop via the eventfd so it can rungsource_prepare()and submit the pending SQE promptly.This is a generic fix that benefits all devices using aio=io_uring. Without it, AHCI/SATA devices see MUCH worse I/O latency since they use MMIO (not ioeventfd like virtio) and have no other mechanism to wake the main loop after queuing block I/O.
This is usually a bit hard to detect, as it also relies on the ppoll loop not waking up for other activity, and micro benchmarks tend not to see it because they don't have any real processing time. With a synthetic test case that has a few
usleep()to simulate processing of read data, it's very noticeable. The below example reads 128MB withO_DIRECTin 128KB chunks in batches of 16, and has a 1ms delay before each batch submit, and a 1ms delay after processing each completion. Running it on/dev/sdayields:time sudo ./iotest
/dev/sda________________________________________________________
Executed in 25.76 secs fish external
usr time 6.19 millis 783.00 micros 5.41 millis
sys time 12.43 millis 642.00 micros 11.79 milliswhile on a virtio-blk or NVMe device we get:
time sudo ./iotest
/dev/vdb________________________________________________________
Executed in 1.25 secs fish external
usr time 1.40 millis 0.30 millis 1.10 millis
sys time 17.61 millis 1.43 millis 16.18 millistime sudo ./iotest
/dev/nvme0n1________________________________________________________
Executed in 1.26 secs fish external
usr time 6.11 millis 0.52 millis 5.59 millis
sys time 13.94 millis 1.50 millis 12.43 milliswhere the latter are consistent. If we run the same test but keep the socket for the ssh connection active by having activity there, then the sda test looks as follows:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.23 secs fish external
usr time 2.70 millis 39.00 micros 2.66 millis
sys time 4.97 millis 977.00 micros 3.99 millisas now the ppoll loop is woken all the time anyway.
After this fix, on an idle system:
time sudo ./iotest
/dev/sda________________________________________________________
Executed in 1.30 secs fish external
usr time 2.14 millis 0.14 millis 2.00 millis
sys time 16.93 millis 1.16 millis 15.76 millisSigned-off-by Jens Axboe <[email protected]>
Quoted 16 lines
util/aio-posix.c void aio_add_sqe(void (*prep_sqe)(struct io_uring_sqe *sqe, void *opaque), · line 819
Quoted 12 lines
util/aio-posix.c +4
-0@@ -813,5 +813,13 @@ void aio_add_sqe(void (*prep_sqe)(struct io_uring_sqe *sqe, void *opaque), {AioContext *ctx =qemu_get_current_aio_context (); ctx->fdmon_ops->add_sqe (ctx, prep_sqe, opaque, cqe_handler); + +/* +* Wake the main loop if it is sleeping in ppoll(). When a vCPU thread +* runs a coroutine inline (holding BQL), it queues SQEs here but the I think the comment could even be more generic here. This is not specific to coroutines, but the scenario is just that a vCPU thread holding the BQL performs I/O.
Good idea, I generalized the comment when merging the patch.
Stefan
Re: [PATCH 1/2] fdmon-io_uring: notify main loop when SQEs are queued
In reply to Kevin WolfQuoted 11 lines · Kevin Wolf
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 6 lines
When a vCPU thread handles MMIO (holding BQL),
aio_co_enter()runs the block I/O coroutine inline on the vCPU thread becauseqemu_get_current_aio_context()returns the main AioContext when BQL is held. The coroutine callsluring_co_submit()which queues an SQE viafdmon_io_uring_add_sqe(), but the actualio_uring_submit()only happens ingsource_prepare()on the main loop thread.Ouch! Yes, looks like we completely missed I/O submitted in vCPU threads in the recent changes (or I guess worker threads in theory, but I don't think there any that actually make use of
aio_add_sqe()).
Worker threads don't have an AioContext, so they cannot call aio_add_sqe().
Stefan
PATCH 2/2 fdmon-io_uring: check CQ ring directly in gsource_check
In reply to Jens Axboegsource_check() only looks at the ppoll revents for the io_uring fd, but CQEs can be posted during gsource_prepare()'s io_uring_submit() call via kernel task_work processing on syscall exit. These completions are already sitting in the CQ ring but the ring fd may not be signaled yet, causing gsource_check() to return false.
Add a fallback io_uring_cq_ready() check so completions that arrive during submission are dispatched immediately rather than waiting for the next ppoll() cycle.
--- util/fdmon-io_uring.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
util/fdmon-io_uring.c +13 -1
@@ -352,7 +352,19 @@ static void fdmon_io_uring_gsource_prepare(AioContext *ctx)static bool fdmon_io_uring_gsource_check (AioContext *ctx) {gpointer tag = ctx->io_uring_fd_tag; -return g_source_query_unix_fd (&ctx->source, tag) &G_IO_IN ; + +/* Check ppoll revents (normal path) */ +if (g_source_query_unix_fd (&ctx->source, tag) &G_IO_IN ) { +return true; + } + +/* +* Also check for CQEs that may have been posted during prepare's +* io_uring_submit() via task_work on syscall exit. Without this, +* the main loop can miss completions and sleep in ppoll() until the +* next timer fires. +*/ +return io_uring_cq_ready (&ctx->fdmon_io_uring); }/* Dispatch CQE handlers that are ready */
Signature · 1 line
2.51.0
Re: [PATCH 2/2] fdmon-io_uring: check CQ ring directly in gsource_check
Am 13.02.2026 um 15:26 hat Jens Axboe geschrieben:
Quoted 11 lines
gsource_check()only looks at the ppoll revents for theio_uringfd, but CQEs can be posted during gsource_prepare()'sio_uring_submit()call via kerneltask_workprocessing on syscall exit. These completions are already sitting in the CQ ring but the ring fd may not be signaled yet, causinggsource_check()to return false.Add a fallback
io_uring_cq_ready()check so completions that arrive during submission are dispatched immediately rather than waiting for the nextppoll()cycle.Signed-off-by Jens Axboe <[email protected]>
Re: [PATCH 2/2] fdmon-io_uring: check CQ ring directly in gsource_check
In reply to Jens AxboeQuoted 6 lines · Jens Axboe
gsource_check()only looks at the ppoll revents for theio_uringfd, but CQEs can be posted during gsource_prepare()'sio_uring_submit()call via kerneltask_workprocessing on syscall exit. These completions are already sitting in the CQ ring but the ring fd may not be signaled yet, causinggsource_check()to return false.
Quoted 8 lines
Add a fallback
io_uring_cq_ready()check so completions that arrive during submission are dispatched immediately rather than waiting for the nextppoll()cycle.Signed-off-by Jens Axboe <[email protected]>--- util/fdmon-io_uring.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
Am 13.02.26 um 3:33 PM schrieb Jens Axboe:
Quoted 17 lines
Hi,
Patch 1 here is the real meat of this, patch 2 is just a slight improvement. For patch 1, it can literally yield a 50-80x improvement on the
io_uringside for idle systems, whereppoll()ends up sleeping for 500 msec while there's IO to submit! I noticed this running theio_uringregression tests in a vm, where I use a variety of block devices for some of the tests. They would often randomly time out on AHCI devices, while running them on a virtio-blk or nvme device would finish in one second or so. I then wrote a reproducer to try and grok this and had claude dive into this, which helped me better grasp the various event loops.Please take a look and tell me what you think. Some variant of patch 1 should definitely be considered, but let me know if this is the right approach. I can easily test anything.
Quoted 6 lines
Also note - this seems to trigger more easily or consistently on aarch64, which is where I run most of my local/immediate testing.
util/fdmon-io_uring.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
Am 13.02.26 um 3:33 PM schrieb Jens Axboe:
Quoted 17 lines
Hi,
Patch 1 here is the real meat of this, patch 2 is just a slight improvement. For patch 1, it can literally yield a 50-80x improvement on the
io_uringside for idle systems, whereppoll()ends up sleeping for 500 msec while there's IO to submit! I noticed this running theio_uringregression tests in a vm, where I use a variety of block devices for some of the tests. They would often randomly time out on AHCI devices, while running them on a virtio-blk or nvme device would finish in one second or so. I then wrote a reproducer to try and grok this and had claude dive into this, which helped me better grasp the various event loops.Please take a look and tell me what you think. Some variant of patch 1 should definitely be considered, but let me know if this is the right approach. I can easily test anything.
Quoted 6 lines
Also note - this seems to trigger more easily or consistently on aarch64, which is where I run most of my local/immediate testing.
util/fdmon-io_uring.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
CC-ing qemu-stable since this affects the 10.2 branch. The fixes are already applied to master as
2ae361ef1d aio-posix: notify main loop when SQEs are queued 961fcc0f22 fdmon-io_uring: check CQ ring directly in gsource_check
Best Regards,
Fiona
Quoted 25 lines · Fiona Ebner
Am 13.02.26 um 3:33 PM schrieb Jens Axboe:
Quoted 17 lines
Hi,
Patch 1 here is the real meat of this, patch 2 is just a slight improvement. For patch 1, it can literally yield a 50-80x improvement on the
io_uringside for idle systems, whereppoll()ends up sleeping for 500 msec while there's IO to submit! I noticed this running theio_uringregression tests in a vm, where I use a variety of block devices for some of the tests. They would often randomly time out on AHCI devices, while running them on a virtio-blk or nvme device would finish in one second or so. I then wrote a reproducer to try and grok this and had claude dive into this, which helped me better grasp the various event loops.Please take a look and tell me what you think. Some variant of patch 1 should definitely be considered, but let me know if this is the right approach. I can easily test anything.
Quoted 6 lines
Also note - this seems to trigger more easily or consistently on aarch64, which is where I run most of my local/immediate testing.
util/fdmon-io_uring.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
Quoted 5 lines
CC-ing qemu-stable since this affects the 10.2 branch. The fixes are already applied to master as
2ae361ef1daio-posix: notify main loop when SQEs are queued961fcc0f22fdmon-io_uring: check CQ ring directly ingsource_check
Yes please, would be nice for them to hit the stable release(s) as well. Thanks Fiona!
Signature · 1 line
Jens Axboe
Michael Tokarev
AppliedQuoted 5 lines · Fiona Ebner
CC-ing qemu-stable since this affects the 10.2 branch. The fixes are already applied to master as
2ae361ef1daio-posix: notify main loop when SQEs are queued961fcc0f22fdmon-io_uring: check CQ ring directly ingsource_check
Picked up. Thank you!
/mjt