Skip to contentSkip to conversation

util/aio-posix: fix poll_ready handlers dropped in aio_prepare()

PATCH New Bin Guo 1 participant 1 message Apr 28

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

PATCH 1/2 util/aio-posix: fix poll_ready handlers dropped in aio_prepare()

When aio_prepare() calls poll_set_started(ctx, &ready_list, false) to disable poll mode, the last poll round may find ready handlers and mark them poll_ready=true while inserting them into a local ready_list. However, that local list is discarded on function return, so those handlers are silently dropped and must wait until the next GLib main loop iteration to be processed.

This was noted by commit 826cc32423 as a TODO comment:
/* TODO what to do with this list? */

Fix this by:
- Extending aio_pending() to scan poll_aio_handlers for any node with poll_ready=true, so GLib's check phase correctly reports pending work and triggers dispatch.
- Collecting poll_ready handlers in aio_dispatch() before calling aio_dispatch_ready_handlers(), so they are actually executed in the same dispatch cycle.

---
 util/aio-posix.c | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)
util/aio-posix.c +34 -3
index 488d964611..633bffd716 100644
@@ -255,9 +255,12 @@ bool aio_prepare(AioContext *ctx)
 {
     AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
 
-    /* Poll mode cannot be used with glib's event loop, disable it. */
+    /*
+     * Poll mode cannot be used with glib's event loop, disable it.
+     * Any handlers that fire in the last poll are marked poll_ready=true
+     * and will be picked up by aio_pending() / aio_dispatch().
+     */
     poll_set_started(ctx, &ready_list, false);
-    /* TODO what to do with this list? */
 
     ctx->fdmon_ops->gsource_prepare(ctx);
     return false;
@@ -265,7 +268,25 @@ bool aio_prepare(AioContext *ctx)
 
 bool aio_pending(AioContext *ctx)
 {
-    return ctx->fdmon_ops->gsource_check(ctx);
+    AioHandler *node;
+
+    if (ctx->fdmon_ops->gsource_check(ctx)) {
+        return true;
+    }
+
+    /*
+     * Check if any poll handlers became ready during aio_prepare()'s
+     * last poll (when poll mode is stopped, poll_set_started(false)).
+     */
+    qemu_lockcnt_inc(&ctx->list_lock);
+    QLIST_FOREACH_RCU(node, &ctx->poll_aio_handlers, node_poll) {
+        if (!QLIST_IS_INSERTED(node, node_deleted) && node->poll_ready) {
+            qemu_lockcnt_dec(&ctx->list_lock);
+            return true;
+        }
+    }
+    qemu_lockcnt_dec(&ctx->list_lock);
+    return false;
 }
 
 static void aio_free_deleted_handlers(AioContext *ctx)
@@ -395,6 +416,16 @@ void aio_dispatch(AioContext *ctx)
         ctx->fdmon_ops->dispatch(ctx);
     }
 
+    /* Collect handlers that became ready during aio_prepare()'s last poll */
+    {
+        AioHandler *node;
+        QLIST_FOREACH_RCU(node, &ctx->poll_aio_handlers, node_poll) {
+            if (!QLIST_IS_INSERTED(node, node_deleted) && node->poll_ready) {
+                aio_add_poll_ready_handler(&ready_list, node);
+            }
+        }
+    }
+
     /* block_ns is 0 because polling is disabled in the glib event loop */
     aio_dispatch_ready_handlers(ctx, &ready_list, 0);
 
Signature · 1 line
2.50.1 (Apple Git-155)