提交 6cff3e85 编写于 作者: P Paolo Bonzini 提交者: Luiz Capitulino

monitor: protect outbuf and mux_out with mutex

This lets the block layer emit QMP events from outside the I/O thread.
Reviewed-by: NLuiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
Signed-off-by: NLuiz Capitulino <lcapitulino@redhat.com>
上级 9005b2a7
...@@ -191,13 +191,18 @@ typedef struct MonitorQAPIEventState { ...@@ -191,13 +191,18 @@ typedef struct MonitorQAPIEventState {
struct Monitor { struct Monitor {
CharDriverState *chr; CharDriverState *chr;
int mux_out;
int reset_seen; int reset_seen;
int flags; int flags;
int suspend_cnt; int suspend_cnt;
bool skip_flush; bool skip_flush;
QemuMutex out_lock;
QString *outbuf; QString *outbuf;
guint watch; guint out_watch;
/* Read under either BQL or out_lock, written with BQL+out_lock. */
int mux_out;
ReadLineState *rs; ReadLineState *rs;
MonitorControl *mc; MonitorControl *mc;
CPUState *mon_cpu; CPUState *mon_cpu;
...@@ -270,17 +275,22 @@ int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, ...@@ -270,17 +275,22 @@ int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
} }
} }
static void monitor_flush_locked(Monitor *mon);
static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond, static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
void *opaque) void *opaque)
{ {
Monitor *mon = opaque; Monitor *mon = opaque;
mon->watch = 0; qemu_mutex_lock(&mon->out_lock);
monitor_flush(mon); mon->out_watch = 0;
monitor_flush_locked(mon);
qemu_mutex_unlock(&mon->out_lock);
return FALSE; return FALSE;
} }
void monitor_flush(Monitor *mon) /* Called with mon->out_lock held. */
static void monitor_flush_locked(Monitor *mon)
{ {
int rc; int rc;
size_t len; size_t len;
...@@ -307,18 +317,26 @@ void monitor_flush(Monitor *mon) ...@@ -307,18 +317,26 @@ void monitor_flush(Monitor *mon)
QDECREF(mon->outbuf); QDECREF(mon->outbuf);
mon->outbuf = tmp; mon->outbuf = tmp;
} }
if (mon->watch == 0) { if (mon->out_watch == 0) {
mon->watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT, mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT,
monitor_unblocked, mon); monitor_unblocked, mon);
} }
} }
} }
void monitor_flush(Monitor *mon)
{
qemu_mutex_lock(&mon->out_lock);
monitor_flush_locked(mon);
qemu_mutex_unlock(&mon->out_lock);
}
/* flush at every end of line */ /* flush at every end of line */
static void monitor_puts(Monitor *mon, const char *str) static void monitor_puts(Monitor *mon, const char *str)
{ {
char c; char c;
qemu_mutex_lock(&mon->out_lock);
for(;;) { for(;;) {
c = *str++; c = *str++;
if (c == '\0') if (c == '\0')
...@@ -328,9 +346,10 @@ static void monitor_puts(Monitor *mon, const char *str) ...@@ -328,9 +346,10 @@ static void monitor_puts(Monitor *mon, const char *str)
} }
qstring_append_chr(mon->outbuf, c); qstring_append_chr(mon->outbuf, c);
if (c == '\n') { if (c == '\n') {
monitor_flush(mon); monitor_flush_locked(mon);
} }
} }
qemu_mutex_unlock(&mon->out_lock);
} }
void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
...@@ -581,6 +600,7 @@ static void handle_user_command(Monitor *mon, const char *cmdline); ...@@ -581,6 +600,7 @@ static void handle_user_command(Monitor *mon, const char *cmdline);
static void monitor_data_init(Monitor *mon) static void monitor_data_init(Monitor *mon)
{ {
memset(mon, 0, sizeof(Monitor)); memset(mon, 0, sizeof(Monitor));
qemu_mutex_init(&mon->out_lock);
mon->outbuf = qstring_new(); mon->outbuf = qstring_new();
/* Use *mon_cmds by default. */ /* Use *mon_cmds by default. */
mon->cmd_table = mon_cmds; mon->cmd_table = mon_cmds;
...@@ -589,6 +609,7 @@ static void monitor_data_init(Monitor *mon) ...@@ -589,6 +609,7 @@ static void monitor_data_init(Monitor *mon)
static void monitor_data_destroy(Monitor *mon) static void monitor_data_destroy(Monitor *mon)
{ {
QDECREF(mon->outbuf); QDECREF(mon->outbuf);
qemu_mutex_destroy(&mon->out_lock);
} }
char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index, char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
...@@ -616,11 +637,13 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index, ...@@ -616,11 +637,13 @@ char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
handle_user_command(&hmp, command_line); handle_user_command(&hmp, command_line);
cur_mon = old_mon; cur_mon = old_mon;
qemu_mutex_lock(&hmp.out_lock);
if (qstring_get_length(hmp.outbuf) > 0) { if (qstring_get_length(hmp.outbuf) > 0) {
output = g_strdup(qstring_get_str(hmp.outbuf)); output = g_strdup(qstring_get_str(hmp.outbuf));
} else { } else {
output = g_strdup(""); output = g_strdup("");
} }
qemu_mutex_unlock(&hmp.out_lock);
out: out:
monitor_data_destroy(&hmp); monitor_data_destroy(&hmp);
...@@ -5180,7 +5203,9 @@ static void monitor_event(void *opaque, int event) ...@@ -5180,7 +5203,9 @@ static void monitor_event(void *opaque, int event)
switch (event) { switch (event) {
case CHR_EVENT_MUX_IN: case CHR_EVENT_MUX_IN:
qemu_mutex_lock(&mon->out_lock);
mon->mux_out = 0; mon->mux_out = 0;
qemu_mutex_unlock(&mon->out_lock);
if (mon->reset_seen) { if (mon->reset_seen) {
readline_restart(mon->rs); readline_restart(mon->rs);
monitor_resume(mon); monitor_resume(mon);
...@@ -5200,7 +5225,9 @@ static void monitor_event(void *opaque, int event) ...@@ -5200,7 +5225,9 @@ static void monitor_event(void *opaque, int event)
} else { } else {
mon->suspend_cnt++; mon->suspend_cnt++;
} }
qemu_mutex_lock(&mon->out_lock);
mon->mux_out = 1; mon->mux_out = 1;
qemu_mutex_unlock(&mon->out_lock);
break; break;
case CHR_EVENT_OPENED: case CHR_EVENT_OPENED:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册