From patchwork Wed Apr 18 09:02:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 899990 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40Qx8D72yrz9s3G for ; Wed, 18 Apr 2018 19:07:08 +1000 (AEST) Received: from localhost ([::1]:42399 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f8j3T-00039U-1G for incoming@patchwork.ozlabs.org; Wed, 18 Apr 2018 05:07:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56339) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1f8izQ-0000Qh-Fs for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:03:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1f8izL-0005u9-QN for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:02:56 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:47260 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1f8izL-0005tm-M5 for qemu-devel@nongnu.org; Wed, 18 Apr 2018 05:02:51 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3815F4022909 for ; Wed, 18 Apr 2018 09:02:51 +0000 (UTC) Received: from xz-mi.redhat.com (ovpn-12-25.pek2.redhat.com [10.72.12.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3A77521568B2; Wed, 18 Apr 2018 09:02:47 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 18 Apr 2018 17:02:38 +0800 Message-Id: <20180418090239.13090-3-peterx@redhat.com> In-Reply-To: <20180418090239.13090-1-peterx@redhat.com> References: <20180418090239.13090-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 18 Apr 2018 09:02:51 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Wed, 18 Apr 2018 09:02:51 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'peterx@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v2 2/3] monitor: take mon_lock where proper X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Markus Armbruster , peterx@redhat.com, "Dr . David Alan Gilbert" , Stefan Hajnoczi , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Went through all the montior.h APIs to make sure existing Monitor related APIs will always take the new monitor lock when necessary. Signed-off-by: Peter Xu --- monitor.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/monitor.c b/monitor.c index c93aa4e22b..f4951cafbc 100644 --- a/monitor.c +++ b/monitor.c @@ -306,16 +306,20 @@ void monitor_read_command(Monitor *mon, int show_prompt) if (!mon->rs) return; + qemu_mutex_lock(&mon->mon_lock); readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL); if (show_prompt) readline_show_prompt(mon->rs); + qemu_mutex_unlock(&mon->mon_lock); } int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func, void *opaque) { if (mon->rs) { + qemu_mutex_lock(&mon->mon_lock); readline_start(mon->rs, "Password: ", 1, readline_func, opaque); + qemu_mutex_unlock(&mon->mon_lock); /* prompt is printed on return from the command handler */ return 0; } else { @@ -1308,8 +1312,7 @@ void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable, cur_mon->qmp.commands = &qmp_commands; } -/* set the current CPU defined by the user */ -int monitor_set_cpu(int cpu_index) +static int monitor_set_cpu_locked(Monitor *mon, int cpu_index) { CPUState *cpu; @@ -1317,15 +1320,28 @@ int monitor_set_cpu(int cpu_index) if (cpu == NULL) { return -1; } - g_free(cur_mon->mon_cpu_path); - cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu)); + g_free(mon->mon_cpu_path); + mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu)); return 0; } +/* set the current CPU defined by the user */ +int monitor_set_cpu(int cpu_index) +{ + int ret; + + qemu_mutex_lock(&cur_mon->mon_lock); + ret = monitor_set_cpu_locked(cur_mon, cpu_index); + qemu_mutex_unlock(&cur_mon->mon_lock); + + return ret; +} + static CPUState *mon_get_cpu_sync(bool synchronize) { CPUState *cpu; + qemu_mutex_lock(&cur_mon->mon_lock); if (cur_mon->mon_cpu_path) { cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path, TYPE_CPU, NULL); @@ -1336,11 +1352,14 @@ static CPUState *mon_get_cpu_sync(bool synchronize) } if (!cur_mon->mon_cpu_path) { if (!first_cpu) { + qemu_mutex_unlock(&cur_mon->mon_lock); return NULL; } - monitor_set_cpu(first_cpu->cpu_index); + monitor_set_cpu_locked(cur_mon, first_cpu->cpu_index); cpu = first_cpu; } + qemu_mutex_unlock(&cur_mon->mon_lock); + if (synchronize) { cpu_synchronize_state(cpu); } @@ -2239,6 +2258,7 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) { mon_fd_t *monfd; + qemu_mutex_lock(&mon->mon_lock); QLIST_FOREACH(monfd, &mon->fds, next) { int fd; @@ -2252,9 +2272,10 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) QLIST_REMOVE(monfd, next); g_free(monfd->name); g_free(monfd); - + qemu_mutex_unlock(&mon->mon_lock); return fd; } + qemu_mutex_unlock(&mon->mon_lock); error_setg(errp, "File descriptor named '%s' has not been found", fdname); return -1;