From patchwork Wed Jun 20 07:10:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 931980 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 419bbT0Q2mz9s3Z for ; Wed, 20 Jun 2018 17:11:17 +1000 (AEST) Received: from localhost ([::1]:46368 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVXGs-0000YB-Na for incoming@patchwork.ozlabs.org; Wed, 20 Jun 2018 03:11:14 -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:'' 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_Lureau?= Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 = cur_mon; cur_mon = xxx; (do something, read cur_mon if necessary in the stack) cur_mon = 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é 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" -extern Monitor *cur_mon; +extern __thread Monitor *cur_mon; /* 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[]; QmpCommandList qmp_commands, qmp_cap_negotiation_commands; -Monitor *cur_mon; +__thread Monitor *cur_mon; 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" -Monitor *cur_mon = NULL; +__thread Monitor *cur_mon; 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, Error **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) {}