5d360b
From 6baaf82a7742a1de9160146b08ba0cc86b3d4e79 Mon Sep 17 00:00:00 2001
5d360b
From: Paolo Bonzini <pbonzini@redhat.com>
5d360b
Date: Wed, 10 Jan 2018 17:02:21 +0100
5d360b
Subject: [PATCH 2/2] main-loop: Acquire main_context lock around
5d360b
 os_host_main_loop_wait.
5d360b
5d360b
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
5d360b
Message-id: <20180110170221.28975-1-pbonzini@redhat.com>
5d360b
Patchwork-id: 78541
5d360b
O-Subject: [RHEL7.5 qemu-kvm PATCH] main-loop: Acquire main_context lock around os_host_main_loop_wait.
5d360b
Bugzilla: 1473536
5d360b
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
5d360b
RH-Acked-by: John Snow <jsnow@redhat.com>
5d360b
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
5d360b
5d360b
Bugzilla: 1473536
5d360b
5d360b
Brew build: https://brewweb.engineering.redhat.com/brew/taskinfo?taskID=14912977
5d360b
5d360b
When running virt-rescue the serial console hangs from time to time.
5d360b
Virt-rescue runs an ordinary Linux kernel "appliance", but there is
5d360b
only a single idle process running inside, so the qemu main loop is
5d360b
largely idle.  With virt-rescue >= 1.37 you may be able to observe the
5d360b
hang by doing:
5d360b
5d360b
  $ virt-rescue -e ^] --scratch
5d360b
  ><rescue> while true; do ls -l /usr/bin; done
5d360b
5d360b
The hang in virt-rescue can be resolved by pressing a key on the
5d360b
serial console.
5d360b
5d360b
Possibly with the same root cause, we also observed hangs during very
5d360b
early boot of regular Linux VMs with a serial console.  Those hangs
5d360b
are extremely rare, but you may be able to observe them by running
5d360b
this command on baremetal for a sufficiently long time:
5d360b
5d360b
  $ while libguestfs-test-tool -t 60 >& /tmp/log ; do echo -n . ; done
5d360b
5d360b
(Check in /tmp/log that the failure was caused by a hang during early
5d360b
boot, and not some other reason)
5d360b
5d360b
During investigation of this bug, Paolo Bonzini wrote:
5d360b
5d360b
> glib is expecting QEMU to use g_main_context_acquire around accesses to
5d360b
> GMainContext.  However QEMU is not doing that, instead it is taking its
5d360b
> own mutex.  So we should add g_main_context_acquire and
5d360b
> g_main_context_release in the two implementations of
5d360b
> os_host_main_loop_wait; these should undo the effect of Frediano's
5d360b
> glib patch.
5d360b
5d360b
This patch exactly implements Paolo's suggestion in that paragraph.
5d360b
5d360b
This fixes the serial console hang in my testing, across 3 different
5d360b
physical machines (AMD, Intel Core i7 and Intel Xeon), over many hours
5d360b
of automated testing.  I wasn't able to reproduce the early boot hangs
5d360b
(but as noted above, these are extremely rare in any case).
5d360b
5d360b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1435432
5d360b
Reported-by: Richard W.M. Jones <rjones@redhat.com>
5d360b
Tested-by: Richard W.M. Jones <rjones@redhat.com>
5d360b
Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
5d360b
Message-Id: <20170331205133.23906-1-rjones@redhat.com>
5d360b
[Paolo: this is actually a glib bug: recent glib versions are also
5d360b
expecting g_main_context_acquire around g_poll---but that is not
5d360b
documented and probably not even intended].
5d360b
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
5d360b
(cherry picked from commit ecbddbb106114f90008024b4e6c3ba1c38d7ca0e)
5d360b
5d360b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
5d360b
---
5d360b
 main-loop.c | 11 +++++++++++
5d360b
 1 file changed, 11 insertions(+)
5d360b
5d360b
diff --git a/main-loop.c b/main-loop.c
5d360b
index cf36645..a93d37b 100644
5d360b
--- a/main-loop.c
5d360b
+++ b/main-loop.c
5d360b
@@ -192,9 +192,12 @@ static void glib_pollfds_poll(void)
5d360b
 
5d360b
 static int os_host_main_loop_wait(uint32_t timeout)
5d360b
 {
5d360b
+    GMainContext *context = g_main_context_default();
5d360b
     int ret;
5d360b
     static int spin_counter;
5d360b
 
5d360b
+    g_main_context_acquire(context);
5d360b
+
5d360b
     glib_pollfds_fill(&timeout);
5d360b
 
5d360b
     /* If the I/O thread is very busy or we are incorrectly busy waiting in
5d360b
@@ -230,6 +233,9 @@ static int os_host_main_loop_wait(uint32_t timeout)
5d360b
     }
5d360b
 
5d360b
     glib_pollfds_poll();
5d360b
+
5d360b
+    g_main_context_release(context);
5d360b
+
5d360b
     return ret;
5d360b
 }
5d360b
 #else
5d360b
@@ -385,12 +391,15 @@ static int os_host_main_loop_wait(uint32_t timeout)
5d360b
     fd_set rfds, wfds, xfds;
5d360b
     int nfds;
5d360b
 
5d360b
+    g_main_context_acquire(context);
5d360b
+
5d360b
     /* XXX: need to suppress polling by better using win32 events */
5d360b
     ret = 0;
5d360b
     for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
5d360b
         ret |= pe->func(pe->opaque);
5d360b
     }
5d360b
     if (ret != 0) {
5d360b
+        g_main_context_release(context);
5d360b
         return ret;
5d360b
     }
5d360b
 
5d360b
@@ -440,6 +449,8 @@ static int os_host_main_loop_wait(uint32_t timeout)
5d360b
         g_main_context_dispatch(context);
5d360b
     }
5d360b
 
5d360b
+    g_main_context_release(context);
5d360b
+
5d360b
     return select_ret || g_poll_ret;
5d360b
 }
5d360b
 #endif
5d360b
-- 
5d360b
1.8.3.1
5d360b