Skip to contentSkip to conversation

aio-posix: fix race between epoll upgrade and aio_set_fd_handler()

PATCH Superseded Stefan Hajnoczi 2 participants 3 messages Mar 22, 2023 – Mar 23, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 aio-posix: fix race between epoll upgrade and aio_set_fd_handler()

If another thread calls aio_set_fd_handler() while the IOThread event loop is upgrading from ppoll(2) to epoll(7) then we might miss new AioHandlers. The epollfd will not monitor the new AioHandler's fd, resulting in hangs.

Take the AioHandler list lock while upgrading to epoll. This prevents AioHandlers from changing while epoll is being set up. If we cannot lock because we're in a nested event loop, then don't upgrade to epoll (it will happen next time we're not in a nested call).

The downside to taking the lock is that the aio_set_fd_handler() thread has to wait until the epoll upgrade is finished, which involves many epoll_ctl(2) system calls. However, this scenario is rare and I couldn't think of another solution that is still simple.

---
 util/fdmon-epoll.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
util/fdmon-epoll.c +18 -7
index e11a8a022e..8f1323ab2c 100644
@@ -127,6 +127,8 @@ static bool fdmon_epoll_try_enable(AioContext *ctx)
 
 bool fdmon_epoll_try_upgrade(AioContext *ctx, unsigned npfd)
 {
+    bool ok;
+
     if (ctx->epollfd < 0) {
         return false;
     }
@@ -136,14 +138,23 @@ bool fdmon_epoll_try_upgrade(AioContext *ctx, unsigned npfd)
         return false;
     }
 
-    if (npfd >= EPOLL_ENABLE_THRESHOLD) {
-        if (fdmon_epoll_try_enable(ctx)) {
-            return true;
-        } else {
-            fdmon_epoll_disable(ctx);
-        }
+    if (npfd < EPOLL_ENABLE_THRESHOLD) {
+        return false;
     }
-    return false;
+
+    /* The list must not change while we add fds to epoll */
+    if (!qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
+        return false;
+    }
+
+    ok = fdmon_epoll_try_enable(ctx);
+
+    qemu_lockcnt_unlock(&ctx->list_lock);
+
+    if (!ok) {
+        fdmon_epoll_disable(ctx);
+    }
+    return ok;
 }
 
 void fdmon_epoll_setup(AioContext *ctx)
Signature · 1 line
2.39.2
#1 lore raw

Il mer 22 mar 2023, 15:55 Stefan Hajnoczi <[email protected]> ha scritto:

util/fdmon-epoll.c bool fdmon_epoll_try_upgrade(AioContext *ctx, unsigned npfd) · line 152

Quoted 9 lines
util/fdmon-epoll.c +8 -0
+    /* The list must not change while we add fds to epoll */
+    if (!qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
+        return false;
+    }
+
+    ok = fdmon_epoll_try_enable(ctx);
+
+    qemu_lockcnt_unlock(&ctx->list_lock);

Shouldn't this be inc_and_unlock to balance the change made by dec_if_lock?

Paolo

+

Quoted 11 lines
util/fdmon-epoll.c +4 -1
+    if (!ok) {
+        fdmon_epoll_disable(ctx);
+    }
+    return ok;
 }
 
 void fdmon_epoll_setup(AioContext *ctx)
--

2.39.2

#2 lore raw
Quoted 13 lines · Paolo Bonzini

Il mer 22 mar 2023, 15:55 Stefan Hajnoczi <[email protected]> ha scritto:

util/fdmon-epoll.c bool fdmon_epoll_try_upgrade(AioContext *ctx, unsigned npfd) · line 152

Quoted 9 lines
util/fdmon-epoll.c +8 -0
+    /* The list must not change while we add fds to epoll */
+    if (!qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
+        return false;
+    }
+
+    ok = fdmon_epoll_try_enable(ctx);
+
+    qemu_lockcnt_unlock(&ctx->list_lock);

Shouldn't this be inc_and_unlock to balance the change made by dec_if_lock?

Yes, the caller expects list_lock to still be incremented. Thanks for catching this!

Stefan