io-uring missed wakeup.
cover io-uring missed wakeup.
When running code under qemu-system-arm a timer calibration loop early in the startup that takes a few milliseconds on hardware took 4-7s and sometimes over 60 seconds to finish.
This stall did go away if qemu was compiled with --disable-linux-io-uring. This is an attempt creating a testcase showing the issue and a fix.
Problem looks to be present for about a year. introduced in commit ded29e64c6f3 ("aio-posix: integrate fdmon into glib event loop")
Kenneth Johansson (2):
tests/unit/test-aio: aio_notify() after aio_poll() in the glib loop
fdmon-io_uring: submit re-armed poll sqes after dispatching handlers
include/qemu/aio.h | 10 ++++++++++ tests/unit/test-aio.c | 33 +++++++++++++++++++++++++++++++++ util/aio-posix.c | 4 ++++ util/fdmon-io_uring.c | 15 +++++++++++++++ 4 files changed, 62 insertions(+)
Signature · 1 line
2.51.0
PATCH 1/2 tests/unit/test-aio: aio_notify() after aio_poll() in the glib loop
aio_poll(qemu_aio_context) can run between the glib event loop's prepare and poll steps, e.g. in a vCPU thread holding the BQL. Check that aio_notify() still wakes the glib event loop afterwards. With the io_uring fd monitor it does not: the poll times out.
test passes when compiled with --disable-linux-io-uring test fails when compiled with --enable-linux-io-uring
--- tests/unit/test-aio.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
tests/unit/test-aio.c +33 -0
@@ -830,6 +830,38 @@ static void test_worker_thread_co_enter(void)g_assert (!aio_poll (ctx, false)); } +/* +* aio_poll() can run between the glib event loop's prepare and poll steps, +* e.g. in a vCPU thread holding the BQL. aio_notify() must still wake the +* glib event loop afterwards. +*/ +static void test_source_notify_after_aio_poll (void ) +{ +GMainContext *gctx =g_main_context_default (); +GPollFD fds[16 ]; +gint max_priority, timeout, nfds; + +do {}while (g_main_context_iteration (NULL , false)); + +g_assert (g_main_context_acquire (gctx)); +g_main_context_prepare (gctx, &max_priority); + nfds =g_main_context_query (gctx, max_priority, &timeout, + fds,G_N_ELEMENTS (fds)); + g_assert_cmpint(nfds, <=, G_N_ELEMENTS(fds)); + +aio_notify (ctx); +aio_poll (ctx, false); + +aio_notify (ctx); + g_assert_cmpint(g_poll(fds, nfds, 1000), >, 0); + +g_main_context_check (gctx, max_priority, fds, nfds); +g_main_context_dispatch (gctx); +g_main_context_release (gctx); + +do {}while (g_main_context_iteration (NULL , false)); +} +/* End of tests. */ int main (int argc,char **argv)
@@ -869,5 +901,6 @@ int main(int argc, char **argv)g_test_add_func ("/aio-gsource/event/wait/no-flush-cb" , test_source_wait_event_notifier_noflush);g_test_add_func ("/aio-gsource/event/flush" , test_source_flush_event_notifier);g_test_add_func ("/aio-gsource/timer/schedule" , test_source_timer_schedule); +g_test_add_func ("/aio-gsource/notify-after-aio-poll" , test_source_notify_after_aio_poll);return g_test_run (); }
Signature · 1 line
2.51.0
PATCH 2/2 fdmon-io_uring: submit re-armed poll sqes after dispatching handlers
In reply to Kenneth JohanssonThe io_uring fd monitor watches each fd with a one-shot poll request. When the fd becomes ready, aio_poll() queues a new poll request for it, but only submits it with the next wait.
A vCPU thread holding the BQL (e.g. blk_pread() from a device model) can run aio_poll(qemu_aio_context) while the main loop sleeps in ppoll() on the ring fd. The new poll request for the AioContext's notifier is then never submitted, so aio_notify() no longer wakes the main loop, and timers stall until ppoll() times out.
Add an optional FDMonOps::post_dispatch() hook that aio_poll() calls after the ready handlers have run, and submit the queued poll requests there.
--- include/qemu/aio.h | 10 ++++++++++ util/aio-posix.c | 4 ++++ util/fdmon-io_uring.c | 15 +++++++++++++++ 3 files changed, 29 insertions(+)
include/qemu/aio.h +10 -0
@@ -125,6 +125,16 @@ typedef struct {*/ bool (*dispatch)(AioContext *ctx); +/* +* post_dispatch: +* @ctx: the AioContext +* +* Called by aio_poll() after ready handlers have been dispatched. +* +* This function is optional and may be NULL. +*/ +void (*post_dispatch)(AioContext *ctx); +/* * gsource_prepare: * @ctx: the AioContext
util/aio-posix.c +4 -0
@@ -745,6 +745,10 @@ bool aio_poll(AioContext *ctx, bool blocking) progress |=aio_bh_poll (ctx); progress |=aio_dispatch_ready_handlers (ctx, &ready_list, dispatch_ns); +if (ctx->fdmon_ops->post_dispatch) { + ctx->fdmon_ops->post_dispatch (ctx); + } +aio_free_deleted_handlers (ctx);if (ctx->poll_max_ns) {
util/fdmon-io_uring.c +15 -0
@@ -433,6 +433,20 @@ static int fdmon_io_uring_wait(AioContext *ctx, AioHandlerList *ready_list,return process_cq_ring (ctx, ready_list); } +/* +* Called by aio_poll() after dispatching handlers. Submit IORING_OP_POLL_ADD +* re-arms now rather than with the next wait: aio_poll() may run in a vCPU +* thread holding the BQL while the glib event loop sleeps in ppoll(). +*/ +static void fdmon_io_uring_post_dispatch (AioContext *ctx) +{ +if (io_uring_sq_ready (&ctx->fdmon_io_uring)) { +while (io_uring_submit (&ctx->fdmon_io_uring) == -EINTR ) { +/* Keep trying if syscall was interrupted */ + } + } +} +static bool fdmon_io_uring_need_wait (AioContext *ctx) {/* Have io_uring events completed? */
@@ -458,6 +472,7 @@ static const FDMonOps fdmon_io_uring_ops = { .wait = fdmon_io_uring_wait, .need_wait = fdmon_io_uring_need_wait, .dispatch = fdmon_io_uring_dispatch, + .post_dispatch = fdmon_io_uring_post_dispatch, .gsource_prepare = fdmon_io_uring_gsource_prepare, .gsource_check = fdmon_io_uring_gsource_check, .gsource_dispatch = fdmon_io_uring_gsource_dispatch,
Signature · 1 line
2.51.0