monitor: do not use mb_read/mb_set for suspend_cnt

Clean up monitor_event to just use monitor_suspend/monitor_resume,
using mon->mux_out to protect against incorrect nesting (especially
on startup).

The only remaining case of reading suspend_cnt is in the can_read
callback, which is just advisory and can use qatomic_read.

As an extra benefit, mux_out is now simply protected by mon_lock.
Also, moving the prompt to the beginning of the main loop removes
it from the output in some error cases where QEMU does not actually
start successfully.  It is not a full fix and it would be nice to
also remove the monitor heading, but this is already a small (though
unintentional) improvement.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini
2023-03-03 13:32:13 +01:00
parent 4cb96b9742
commit 6ee7c82d0d
5 changed files with 39 additions and 38 deletions
+19 -22
View File
@@ -1401,45 +1401,42 @@ static void monitor_read(void *opaque, const uint8_t *buf, int size)
static void monitor_event(void *opaque, QEMUChrEvent event)
{
Monitor *mon = opaque;
MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
switch (event) {
case CHR_EVENT_MUX_IN:
qemu_mutex_lock(&mon->mon_lock);
mon->mux_out = 0;
qemu_mutex_unlock(&mon->mon_lock);
if (mon->reset_seen) {
readline_restart(hmp_mon->rs);
if (mon->mux_out) {
mon->mux_out = 0;
monitor_resume(mon);
monitor_flush(mon);
} else {
qatomic_mb_set(&mon->suspend_cnt, 0);
}
qemu_mutex_unlock(&mon->mon_lock);
break;
case CHR_EVENT_MUX_OUT:
if (mon->reset_seen) {
if (qatomic_mb_read(&mon->suspend_cnt) == 0) {
monitor_printf(mon, "\n");
}
monitor_flush(mon);
monitor_suspend(mon);
} else {
qatomic_inc(&mon->suspend_cnt);
}
qemu_mutex_lock(&mon->mon_lock);
mon->mux_out = 1;
if (!mon->mux_out) {
if (mon->reset_seen && !mon->suspend_cnt) {
monitor_puts_locked(mon, "\n");
} else {
monitor_flush_locked(mon);
}
monitor_suspend(mon);
mon->mux_out = 1;
}
qemu_mutex_unlock(&mon->mon_lock);
break;
case CHR_EVENT_OPENED:
monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
"information\n", QEMU_VERSION);
if (!mon->mux_out) {
readline_restart(hmp_mon->rs);
readline_show_prompt(hmp_mon->rs);
}
qemu_mutex_lock(&mon->mon_lock);
mon->reset_seen = 1;
if (!mon->mux_out) {
/* Suspend-resume forces the prompt to be printed. */
monitor_suspend(mon);
monitor_resume(mon);
}
qemu_mutex_unlock(&mon->mon_lock);
mon_refcount++;
break;
+1 -2
View File
@@ -94,7 +94,6 @@ typedef struct HMPCommand {
struct Monitor {
CharBackend chr;
int reset_seen;
int suspend_cnt; /* Needs to be accessed atomically */
bool is_qmp;
bool skip_flush;
@@ -115,8 +114,8 @@ struct Monitor {
QLIST_HEAD(, mon_fd_t) fds;
GString *outbuf;
guint out_watch;
/* Read under either BQL or mon_lock, written with BQL+mon_lock. */
int mux_out;
int reset_seen;
};
struct MonitorHMP {
+7 -2
View File
@@ -569,10 +569,15 @@ static void monitor_accept_input(void *opaque)
{
Monitor *mon = opaque;
if (!monitor_is_qmp(mon)) {
qemu_mutex_lock(&mon->mon_lock);
if (!monitor_is_qmp(mon) && mon->reset_seen) {
MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
assert(hmp_mon->rs);
readline_restart(hmp_mon->rs);
qemu_mutex_unlock(&mon->mon_lock);
readline_show_prompt(hmp_mon->rs);
} else {
qemu_mutex_unlock(&mon->mon_lock);
}
qemu_chr_fe_accept_input(&mon->chr);
@@ -603,7 +608,7 @@ int monitor_can_read(void *opaque)
{
Monitor *mon = opaque;
return !qatomic_mb_read(&mon->suspend_cnt);
return !qatomic_read(&mon->suspend_cnt);
}
void monitor_list_append(Monitor *mon)