Skip to contentSkip to conversation

main-loop: fix qemu_notify_event for aio_notify optimization

PATCH Superseded Paolo Bonzini 2 participants 2 messages Jul 24, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 main-loop: fix qemu_notify_event for aio_notify optimization

aio_notify can be optimized away, and in fact almost always will. However, qemu_notify_event is used in places where this is incorrect---most notably, when handling SIGTERM. When aio_notify is optimized away, it is possible that QEMU enters a blocking ppoll immediately afterwards and stays there, without reaching main_loop_should_exit().

Fix this by using a bottom half. The bottom half can be optimized too, but it will cause the ppoll not to block. The hang is thus avoided.

---
 main-loop.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
main-loop.c +7 -1
index 82875a4..9d93478 100644
@@ -114,6 +114,11 @@ static int qemu_signal_init(void)
 #endif
 
 static AioContext *qemu_aio_context;
+static QEMUBH *qemu_notify_bh;
+
+static void notify_event_cb(void *opaque)
+{
+}
 
 AioContext *qemu_get_aio_context(void)
 {
@@ -125,7 +130,7 @@ void qemu_notify_event(void)
     if (!qemu_aio_context) {
         return;
     }
-    aio_notify(qemu_aio_context);
+    qemu_bh_schedule(qemu_notify_bh);
 }
 
 static GArray *gpollfds;
@@ -144,6 +149,7 @@ int qemu_init_main_loop(Error **errp)
     }
 
     qemu_aio_context = aio_context_new(&local_error);
+    qemu_notify_bh = qemu_bh_new(notify_event_cb, NULL);
     if (!qemu_aio_context) {
         error_propagate(errp, local_error);
         return -EMFILE;
Signature · 1 line
2.4.3
#1 lore raw
Quoted 20 lines · Paolo Bonzini

aio_notify can be optimized away, and in fact almost always will. However, qemu_notify_event is used in places where this is incorrect---most notably, when handling SIGTERM. When aio_notify is optimized away, it is possible that QEMU enters a blocking ppoll immediately afterwards and stays there, without reaching main_loop_should_exit().

Fix this by using a bottom half. The bottom half can be optimized too, but it will cause the ppoll not to block. The hang is thus avoided.

---
 main-loop.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
main-loop.c +0 -0
index 82875a4..9d93478 100644

main-loop.c static int qemu_signal_init(void) · line 121

Quoted 8 lines
main-loop.c +5 -0
@@ -114,6 +114,11 @@ static int qemu_signal_init(void)
 #endif
 
 static AioContext *qemu_aio_context;
+static QEMUBH *qemu_notify_bh;
+
+static void notify_event_cb(void *opaque)
+{
+}

Could we have a comment to explain why an empty callback is an interesting thing to run, please? I can sort of deduce from the commit message that the purpose of this apparently useless thing is just to provoke the main loop into leaving ppoll() and checking whatever condition it needs to, but it wouldn't be very clear to me just from the code...

thanks
-- PMM