From nobody Mon Apr 29 11:43:40 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1529478723166187.64727811196713; Wed, 20 Jun 2018 00:12:03 -0700 (PDT) Received: from localhost ([::1]:46370 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVXHe-00015V-Eg for importer@patchew.org; Wed, 20 Jun 2018 03:12:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47389) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVXGQ-0000UH-Dw for qemu-devel@nongnu.org; Wed, 20 Jun 2018 03:10:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVXGO-0002mq-V2 for qemu-devel@nongnu.org; Wed, 20 Jun 2018 03:10:46 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:49520 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 1fVXGO-0002mm-Pp for qemu-devel@nongnu.org; Wed, 20 Jun 2018 03:10:44 -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 773958790A for ; Wed, 20 Jun 2018 07:10:44 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-142.nay.redhat.com [10.66.14.142]) by smtp.corp.redhat.com (Postfix) with ESMTP id 823972156880; Wed, 20 Jun 2018 07:10:41 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 20 Jun 2018 15:10:40 +0800 Message-Id: <20180620071040.28729-1-peterx@redhat.com> MIME-Version: 1.0 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.1]); Wed, 20 Jun 2018 07:10:44 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.1]); Wed, 20 Jun 2018 07:10:44 +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:'' Content-Transfer-Encoding: quoted-printable 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 v4] monitor: let cur_mon be per-thread 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=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" After the Out-Of-Band work, the monitor iothread may be accessing the cur_mon as well (via monitor_qmp_dispatch_one()). Let's convert the cur_mon variable to be a per-thread variable to make sure there won't be a race between threads when accessing the variable. Note that thread variables are not initialized to a valid value when new thread is created. However for our case we don't need to set it up, since the cur_mon variable is only used in such a pattern: old_mon =3D cur_mon; cur_mon =3D xxx; (do something, read cur_mon if necessary in the stack) cur_mon =3D old_mon; It plays a role as stack variable, so no need to be initialized at all. We only need to make sure the variable won't be changed unexpectedly by other threads. Reviewed-by: Eric Blake Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Stefan Hajnoczi [peterx: touch up commit message a bit] Signed-off-by: Peter Xu Reviewed-by: Markus Armbruster --- v4: - touch up the commit message since now we have OOB command already v3: - fix code style warning from patchew v2: - drop qemu-thread changes --- include/monitor/monitor.h | 2 +- monitor.c | 2 +- stubs/monitor.c | 2 +- tests/test-util-sockets.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index d6ab70cae2..2ef5e04b37 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -6,7 +6,7 @@ #include "qapi/qapi-types-misc.h" #include "qemu/readline.h" =20 -extern Monitor *cur_mon; +extern __thread Monitor *cur_mon; =20 /* flags for monitor_init */ /* 0x01 unused */ diff --git a/monitor.c b/monitor.c index 885e000f9b..94365a8ae6 100644 --- a/monitor.c +++ b/monitor.c @@ -282,7 +282,7 @@ static mon_cmd_t info_cmds[]; =20 QmpCommandList qmp_commands, qmp_cap_negotiation_commands; =20 -Monitor *cur_mon; +__thread Monitor *cur_mon; =20 static void monitor_command_cb(void *opaque, const char *cmdline, void *readline_opaque); diff --git a/stubs/monitor.c b/stubs/monitor.c index e018c8f594..3890771bb5 100644 --- a/stubs/monitor.c +++ b/stubs/monitor.c @@ -3,7 +3,7 @@ #include "qemu-common.h" #include "monitor/monitor.h" =20 -Monitor *cur_mon =3D NULL; +__thread Monitor *cur_mon; =20 int monitor_get_fd(Monitor *mon, const char *name, Error **errp) { diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c index acadd85e8f..6195a3ac36 100644 --- a/tests/test-util-sockets.c +++ b/tests/test-util-sockets.c @@ -69,7 +69,7 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Erro= r **errp) * stubs/monitor.c is defined, to make sure monitor.o is discarded * otherwise we get duplicate syms at link time. */ -Monitor *cur_mon; +__thread Monitor *cur_mon; void monitor_init(Chardev *chr, int flags) {} =20 =20 --=20 2.17.1