aio-posix: integrate fdmon into glib event loop
cover aio-posix: integrate fdmon into glib event loop
This series hooks the epoll and io_uring file descriptor monitoring implementations up to the glib event loop so that even the main loop thread can use them. Until now the main loop thread was stuck using ppoll(2) because the glib event loop semantics weren't supported by fdmon-epoll.c and fdmon-io_uring.c.
The motivation for doing this is to make io_uring available in every QEMU thread on Linux hosts. This will make it easier to add features that use io_uring in the future. I am preparing a follow-up series that unifies block/io_uring.c and util/fdmon-io_uring.c so that AioContext has only one io_uring context instead of two.
Expect performance results in the coming days comparing as ppoll(2) vs epoll(7) vs io_uring(7).
Stefan Hajnoczi (4):
aio-posix: fix polling mode with fdmon-io_uring tests/unit: skip test-nested-aio-poll with io_uring
aio-posix: integrate fdmon into glib event loop
aio: remove aio_context_use_g_source()
meson.build | 2 +- include/block/aio.h | 39 ++++++++++++-- util/aio-posix.h | 5 ++ tests/unit/test-aio.c | 7 ++- tests/unit/test-nested-aio-poll.c | 13 ++--- util/aio-posix.c | 81 ++++++---------------------- util/aio-win32.c | 4 -- util/async.c | 1 - util/fdmon-epoll.c | 52 ++++++++++++++++-- util/fdmon-io_uring.c | 78 ++++++++++++++++++++++----- util/fdmon-poll.c | 88 ++++++++++++++++++++++++++++++- 11 files changed, 268 insertions(+), 102 deletions(-)
Signature · 1 line
2.49.0
PATCH 1/4 aio-posix: fix polling mode with fdmon-io_uring
The io_uring(7) file descriptor monitor cannot enter polling mode because it needs to submit a POLL_ADD SQE every time a file descriptor becomes active. Submitting SQEs only happens in FDMonOps->wait() outside of polling mode.
Fix this using the multi-shot mechanism introduced in Linux 5.13 and liburing 2.1. Stable and enterprise Linux distros ship 5.14+ as of March 2025, so it is safe to require this. Note that fdmon-io_uring is currently not enabled at runtime and is not essential, so QEMU can still be built without it on older hosts.
In multi-shot mode, a POLL_ADD SQE remains active until canceled with POLL_REMOVE. This avoids the need to submit a new SQE every time a file descriptor becomes active.
When POLL_REMOVE is processed by the host kernel, the multi-shot POLL_ADD operation completes with -ECANCELED. Adjust the code slightly to take this into account.
--- meson.build | 2 +- util/fdmon-io_uring.c | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 14 deletions(-)
meson.build +1 -1
@@ -1144,7 +1144,7 @@ linux_io_uring_test = ''' linux_io_uring = not_found if not get_option('linux_io_uring').auto() or have_block - linux_io_uring = dependency('liburing', version: '>=0.3', + linux_io_uring = dependency('liburing', version: '>=2.1', required: get_option('linux_io_uring'), method: 'pkg-config') if not cc.links(linux_io_uring_test)
util/fdmon-io_uring.c +21 -13
@@ -124,8 +124,7 @@ static AioHandler *dequeue(AioHandlerSList *head, unsigned *flags)/* * Don't clear FDMON_IO_URING_REMOVE. It's sticky so it can serve two * purposes: telling fill_sq_ring() to submit IORING_OP_POLL_REMOVE and -* telling process_cqe() to delete the AioHandler when its -* IORING_OP_POLL_ADD completes. +* telling process_cqe() to ignore IORING_OP_POLL_ADD completions. */ *flags =qatomic_fetch_and (&node->flags, ~(FDMON_IO_URING_PENDING |FDMON_IO_URING_ADD ));
@@ -166,12 +165,12 @@ static void fdmon_io_uring_update(AioContext *ctx, } } -static void add_poll_add_sqe (AioContext *ctx,AioHandler *node) +static void add_poll_multishot_sqe (AioContext *ctx,AioHandler *node) {struct io_uring_sqe *sqe =get_sqe (ctx);int events =poll_events_from_pfd (node->pfd.events); -io_uring_prep_poll_add (sqe, node->pfd.fd, events); +io_uring_prep_poll_multishot (sqe, node->pfd.fd, events);io_uring_sqe_set_data (sqe, node); }
@@ -213,7 +212,7 @@ static void fill_sq_ring(AioContext *ctx)while ((node =dequeue (&submit_list, &flags))) {/* Order matters, just in case both flags were set */ if (flags &FDMON_IO_URING_ADD ) { -add_poll_add_sqe (ctx, node); +add_poll_multishot_sqe (ctx, node); }if (flags &FDMON_IO_URING_REMOVE ) {add_poll_remove_sqe (ctx, node);
@@ -234,21 +233,30 @@ static bool process_cqe(AioContext *ctx,return false; } + flags =qatomic_read (&node->flags); +/* -* Deletion can only happen when IORING_OP_POLL_ADD completes. If we race -* with enqueue() here then we can safely clear the FDMON_IO_URING_REMOVE -* bit before IORING_OP_POLL_REMOVE is submitted. +* poll_multishot cancelled by poll_remove? Or completed early because fd +* was closed before poll_remove finished? */ - flags =qatomic_fetch_and (&node->flags, ~FDMON_IO_URING_REMOVE ); -if (flags &FDMON_IO_URING_REMOVE ) { +if (cqe->res == -ECANCELED || cqe->res == -EBADF ) { +assert (!(cqe->flags &IORING_CQE_F_MORE )); +assert (flags &FDMON_IO_URING_REMOVE );QLIST_INSERT_HEAD_RCU (&ctx->deleted_aio_handlers, node, node_deleted);return false; } -aio_add_ready_handler (ready_list, node,pfd_events_from_poll (cqe->res)); +/* Ignore if it becomes ready during removal */ +if (flags &FDMON_IO_URING_REMOVE ) { +return false; + } -/* IORING_OP_POLL_ADD is one-shot so we must re-arm it */ -add_poll_add_sqe (ctx, node); +/* Multi-shot can stop at any time, so re-arm if necessary */ +if (!(cqe->flags &IORING_CQE_F_MORE )) { +add_poll_multishot_sqe (ctx, node); + } + +aio_add_ready_handler (ready_list, node,pfd_events_from_poll (cqe->res));return true; }
Signature · 1 line
2.49.0
PATCH 2/4 tests/unit: skip test-nested-aio-poll with io_uring
In reply to Stefan Hajnoczitest-nested-aio-poll relies on internal details of how fdmon-poll.c handles AioContext polling. Skip it when other fdmon implementations are in use.
Note that this test is only built on POSIX systems so it is safe to include "util/aio-posix.h".
--- tests/unit/test-nested-aio-poll.c | 7 +++++++ 1 file changed, 7 insertions(+)
tests/unit/test-nested-aio-poll.c +7 -0
@@ -15,6 +15,7 @@#include "qemu/osdep.h" #include "block/aio.h" #include "qapi/error.h" +#include "util/aio-posix.h" typedef struct { AioContext *ctx;
@@ -71,6 +72,12 @@ static void test(void) .ctx = aio_context_new(&error_abort), }; +if (td.ctx->fdmon_ops != &fdmon_poll_ops) { +/* This test is tied to fdmon-poll.c */ +g_test_skip ("fdmon_poll_ops not in use" ); +return ; + } +qemu_set_current_aio_context (td.ctx);/* Enable polling */
Signature · 1 line
2.49.0
PATCH 3/4 aio-posix: integrate fdmon into glib event loop
In reply to Stefan HajnocziAioContext's glib integration only supports ppoll(2) file descriptor monitoring. epoll(7) and io_uring(7) disable themselves and switch back to ppoll(2) when the glib event loop is used. The main loop thread cannot use epoll(7) or io_uring(7) because it always uses the glib event loop.
Future QEMU features may require io_uring(7). One example is uring_cmd support in FUSE exports. Each feature could create its own io_uring(7) context and integrate it into the event loop, but this is inefficient due to extra syscalls. It would be more efficient to reuse the AioContext's existing fdmon-io_uring.c io_uring(7) context because fdmon-io_uring.c will already be active on systems where Linux io_uring is available.
In order to keep fdmon-io_uring.c's AioContext operational even when the glib event loop is used, extend FDMonOps with an API similar to GSourceFuncs so that file descriptor monitoring can integrate into the glib event loop.
A quick summary of the GSourceFuncs API:
- prepare() is called each event loop iteration before waiting for file descriptors and timers.
- check() is called to determine whether events are ready to be dispatched after waiting.
- dispatch() is called to process events.
More details here: https://docs.gtk.org/glib/struct.SourceFuncs.html
Move the ppoll(2)-specific code from aio-posix.c into fdmon-poll.c and also implement epoll(7)- and io_uring(7)-specific file descriptor monitoring code for glib event loops.
Note that it's still faster to use aio_poll() rather than the glib event loop since glib waits for file descriptor activity with ppoll(2) and does not support adaptive polling. But at least epoll(7) and io_uring(7) now work in glib event loops.
Splitting this into multiple commits without temporarily breaking AioContext proved difficult so this commit makes all the changes. The next commit will remove the aio_context_use_g_source() API because it is no longer needed.
--- include/block/aio.h | 36 ++++++++++++++++++ util/aio-posix.h | 5 +++ tests/unit/test-aio.c | 7 +++- util/aio-posix.c | 69 ++++++++------------------------- util/fdmon-epoll.c | 52 ++++++++++++++++++++++--- util/fdmon-io_uring.c | 44 +++++++++++++++++++++- util/fdmon-poll.c | 88 ++++++++++++++++++++++++++++++++++++++++++- 7 files changed, 239 insertions(+), 62 deletions(-)
include/block/aio.h +36 -0
@@ -106,6 +106,38 @@ typedef struct {* Returns: true if ->wait() should be called, false otherwise. */ bool (*need_wait)(AioContext *ctx); + +/* +* gsource_prepare: +* @ctx: the AioContext +* +* Prepare for the glib event loop to wait for events instead of the usual +* ->wait() call. See glib's GSourceFuncs->prepare(). +*/ +void (*gsource_prepare)(AioContext *ctx); + +/* +* gsource_check: +* @ctx: the AioContext +* +* Called by the glib event loop from glib's GSourceFuncs->check() after +* waiting for events. +* +* Returns: true when ready to be dispatched. +*/ +bool (*gsource_check)(AioContext *ctx); + +/* +* gsource_dispatch: +* @ctx: the AioContext +* @ready_list: list for handlers that become ready +* +* Place ready AioHandlers on ready_list. Called as part of the glib event +* loop from glib's GSourceFuncs->dispatch(). +* +* Called with list_lock incremented. +*/ +void (*gsource_dispatch)(AioContext *ctx,AioHandlerList *ready_list); } FDMonOps;/*
@@ -222,6 +254,7 @@ struct AioContext {/* State for file descriptor monitoring using Linux io_uring */ struct io_uring fdmon_io_uring;AioHandlerSList submit_list; +gpointer io_uring_fd_tag;#endif /* TimerLists for calling timers - one per clock type. Has its own
@@ -254,6 +287,9 @@ struct AioContext {/* epoll(7) state used when built with CONFIG_EPOLL */ int epollfd; +/* The GSource unix fd tag for epollfd */ +gpointer epollfd_tag; +const FDMonOps *fdmon_ops; };
util/aio-posix.h +5 -0
@@ -47,9 +47,14 @@ void aio_add_ready_handler(AioHandlerList *ready_list, AioHandler *node,extern const FDMonOps fdmon_poll_ops; +/* Switch back to poll(2). list_lock must be held. */ +void fdmon_poll_downgrade (AioContext *ctx); +#ifdef CONFIG_EPOLL_CREATE1 bool fdmon_epoll_try_upgrade (AioContext *ctx,unsigned npfd);void fdmon_epoll_setup (AioContext *ctx); + +/* list_lock must be held */ void fdmon_epoll_disable (AioContext *ctx);#else static inline bool fdmon_epoll_try_upgrade (AioContext *ctx,unsigned npfd)
tests/unit/test-aio.c +6 -1
@@ -527,7 +527,12 @@ static void test_source_bh_delete_from_cb(void) g_assert_cmpint(data1.n, ==, data1.max);g_assert (data1.bh ==NULL ); -assert (g_main_context_iteration (NULL , false)); +/* +* There may be up to one more iteration due to the aio_notify +* EventNotifier. +*/ +g_main_context_iteration (NULL , false); +assert (!g_main_context_iteration (NULL , false)); }
util/aio-posix.c +15 -54
121 lines not on this page · raw message
util/fdmon-epoll.c +47 -5
83 lines not on this page · raw message
util/fdmon-io_uring.c +43 -1
79 lines not on this page · raw message
util/fdmon-poll.c +87 -1
104 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH 4/4 aio: remove aio_context_use_g_source()
In reply to Stefan HajnocziThere is no need for aio_context_use_g_source() now that epoll(7) and io_uring(7) file descriptor monitoring works with the glib event loop. AioContext doesn't need to be notified that GSource is being used.
--- include/block/aio.h | 3 --- tests/unit/test-nested-aio-poll.c | 6 ------ util/aio-posix.c | 12 ------------ util/aio-win32.c | 4 ---- util/async.c | 1 - 5 files changed, 26 deletions(-)
include/block/aio.h +0 -3
@@ -728,9 +728,6 @@ void aio_context_setup(AioContext *ctx);*/ void aio_context_destroy (AioContext *ctx); -/* Used internally, do not call outside AioContext code */ -void aio_context_use_g_source (AioContext *ctx); -/** * aio_context_set_poll_params: * @ctx: the aio context
tests/unit/test-nested-aio-poll.c +0 -6
@@ -83,12 +83,6 @@ static void test(void)/* Enable polling */ aio_context_set_poll_params (td.ctx,1000000 ,2 ,2 , &error_abort); -/* -* The GSource is unused but this has the side-effect of changing the fdmon -* that AioContext uses. -*/ -aio_get_g_source (td.ctx); -/* Make the event notifier active (set) right away */ event_notifier_init (&td.poll_notifier,1 ); aio_set_event_notifier(td.ctx, &td.poll_notifier,
util/aio-posix.c +0 -12
@@ -736,18 +736,6 @@ void aio_context_destroy(AioContext *ctx)aio_free_deleted_handlers (ctx); } -void aio_context_use_g_source (AioContext *ctx) -{ -/* -* Disable io_uring when the glib main loop is used because it doesn't -* support mixed glib/aio_poll() usage. It relies on aio_poll() being -* called regularly so that changes to the monitored file descriptors are -* submitted, otherwise a list of pending fd handlers builds up. -*/ -fdmon_io_uring_destroy (ctx); -aio_free_deleted_handlers (ctx); -} -void aio_context_set_poll_params (AioContext *ctx,int64_t max_ns,int64_t grow,int64_t shrink,Error **errp) {
util/aio-win32.c +0 -4
@@ -427,10 +427,6 @@ void aio_context_destroy(AioContext *ctx) { } -void aio_context_use_g_source (AioContext *ctx) -{ -} -void aio_context_set_poll_params (AioContext *ctx,int64_t max_ns,int64_t grow,int64_t shrink,Error **errp) {
util/async.c +0 -1
@@ -430,7 +430,6 @@ static GSourceFuncs aio_source_funcs = {GSource *aio_get_g_source (AioContext *ctx) { -aio_context_use_g_source (ctx);g_source_ref (&ctx->source);return &ctx->source; }
Signature · 1 line
2.49.0