qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] monitor: Protect outbuf from concurrent access
@ 2011-09-01 19:35 Luiz Capitulino
  2011-09-01 19:47 ` Daniel P. Berrange
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Luiz Capitulino @ 2011-09-01 19:35 UTC (permalink / raw)
  To: qemu-devel; +Cc: Marian Krcmarik, Alon Levy

Sometimes, when having lots of VMs running on a RHEV host and the user
attempts to close a SPICE window, libvirt will get corrupted json from
QEMU.

After some investigation, I found out that the problem is that different
SPICE threads are calling monitor functions (such as
monitor_protocol_event()) in parallel which causes concurrent access
to the monitor's internal buffer outbuf[].

This fixes the problem by protecting accesses to outbuf[] with a mutex.

Honestly speaking, I'm not completely sure this the best thing to do
because the monitor itself and other qemu subsystems are not thread safe,
so having subsystems like SPICE assuming the contrary seems a bit
catastrophic to me...

Anyways, this commit fixes the problem at hand.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 monitor.c |   16 +++++++++++++++-
 1 files changed, 15 insertions(+), 1 deletions(-)

diff --git a/monitor.c b/monitor.c
index 04f465a..61d4d93 100644
--- a/monitor.c
+++ b/monitor.c
@@ -57,6 +57,7 @@
 #include "json-parser.h"
 #include "osdep.h"
 #include "cpu.h"
+#include "qemu-thread.h"
 #ifdef CONFIG_SIMPLE_TRACE
 #include "trace.h"
 #endif
@@ -144,6 +145,7 @@ struct Monitor {
     int suspend_cnt;
     uint8_t outbuf[1024];
     int outbuf_index;
+    QemuMutex mutex;
     ReadLineState *rs;
     MonitorControl *mc;
     CPUState *mon_cpu;
@@ -246,10 +248,14 @@ static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
 
 void monitor_flush(Monitor *mon)
 {
+    qemu_mutex_lock(&mon->mutex);
+
     if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
         qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
         mon->outbuf_index = 0;
     }
+
+    qemu_mutex_unlock(&mon->mutex);
 }
 
 /* flush at every end of line or if the buffer is full */
@@ -257,6 +263,8 @@ static void monitor_puts(Monitor *mon, const char *str)
 {
     char c;
 
+    qemu_mutex_lock(&mon->mutex);
+
     for(;;) {
         c = *str++;
         if (c == '\0')
@@ -265,9 +273,14 @@ static void monitor_puts(Monitor *mon, const char *str)
             mon->outbuf[mon->outbuf_index++] = '\r';
         mon->outbuf[mon->outbuf_index++] = c;
         if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
-            || c == '\n')
+            || c == '\n') {
+            qemu_mutex_unlock(&mon->mutex);
             monitor_flush(mon);
+            qemu_mutex_lock(&mon->mutex);
+        }
     }
+
+    qemu_mutex_unlock(&mon->mutex);
 }
 
 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
@@ -5275,6 +5288,7 @@ void monitor_init(CharDriverState *chr, int flags)
 
     mon = g_malloc0(sizeof(*mon));
 
+    qemu_mutex_init(&mon->mutex);
     mon->chr = chr;
     mon->flags = flags;
     if (flags & MONITOR_USE_READLINE) {
-- 
1.7.7.rc0.72.g4b5ea

^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2011-09-05  7:48 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-09-01 19:35 [Qemu-devel] [PATCH] monitor: Protect outbuf from concurrent access Luiz Capitulino
2011-09-01 19:47 ` Daniel P. Berrange
2011-09-01 21:03 ` Jan Kiszka
2011-09-02  1:34 ` Anthony Liguori
2011-09-02  9:41   ` Daniel P. Berrange
2011-09-02 11:26     ` Jan Kiszka
2011-09-02 13:39   ` Gerd Hoffmann
2011-09-02 14:03     ` Anthony Liguori
2011-09-02 14:24     ` Luiz Capitulino
2011-09-02 14:28     ` Anthony Liguori
2011-09-02 15:18       ` Gerd Hoffmann
2011-09-02 15:20         ` Anthony Liguori
2011-09-02 15:31         ` Paolo Bonzini
2011-09-02 15:37           ` Anthony Liguori
2011-09-05  7:48           ` Gerd Hoffmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).