From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680353; cv=none; d=zohomail.com; s=zohoarc; b=UUtyJFERh1Qbqu/idOgy2iWZfWrpuXeOl4dLK2zJdjZObfnhZA6QsYrrfUIk+7Du2+K7DN9DIwINsWW8qtlAufg+eAaY+MbjLTmzNYC/PA/lJEw2CduYTRza3IgqaiePsitrp960ZWlnG72afZk2lzZQ2Q+IzBwt6LU52B5Vhc4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680353; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=4xw4rNKkRFEB5kYS1rc2u91qGGJz0iDVX3Kx3bds5YY=; b=PRSRjsur6BBB7pDUP4i4GGVbOTGSKzO6e6waXMvgwKdcCUdpCJofTWGdGz5F2YW17oRd24BHw5mfzblYEtPMElEnD9UvyTP4jMpl3CWOj+zj8QbHv5rWchJvX4GNf9KdatcoBlm0URK1+mEi/SlqwH/eEhIDG+uAkM8SmPQeHSc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 159068035367664.84978379658969; Thu, 28 May 2020 08:39:13 -0700 (PDT) Received: from localhost ([::1]:56172 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKci-0005Oy-5y for importer@patchew.org; Thu, 28 May 2020 11:39:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32808) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKbZ-0003Uj-M1 for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:01 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:56318 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbY-0003qL-4T for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:01 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-361-vLQkfjpEMu6gJq5c9b6jgQ-1; Thu, 28 May 2020 11:37:57 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 60D69C7464; Thu, 28 May 2020 15:37:48 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4187A10013C0; Thu, 28 May 2020 15:37:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680279; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4xw4rNKkRFEB5kYS1rc2u91qGGJz0iDVX3Kx3bds5YY=; b=FOHyoR0Rfqip9YOA01OZCsrxrT1diUkWKrzGXBXvwxVXsHEGl+is1/ojp4skh/UdT5oGou X0cgOYBaV9bGUPL65gIL0Y758B/2Bc9IDnapFbh30ws3eunC/Mb9+OWvA/qVcNHDmu1VT4 4yObbIVUJarJIQBRRR/+HO63EkAK42Q= X-MC-Unique: vLQkfjpEMu6gJq5c9b6jgQ-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 01/12] monitor: Add Monitor parameter to monitor_set_cpu() Date: Thu, 28 May 2020 17:37:31 +0200 Message-Id: <20200528153742.274164-2-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 02:50:32 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Most callers actually don't have to rely on cur_mon, but already know for which monitor they call monitor_set_cpu(). Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake --- include/monitor/monitor.h | 2 +- monitor/hmp-cmds.c | 2 +- monitor/misc.c | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index 1018d754a6..0dcaefd4f9 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -33,7 +33,7 @@ int monitor_vprintf(Monitor *mon, const char *fmt, va_lis= t ap) GCC_FMT_ATTR(2, 0); int monitor_printf(Monitor *mon, const char *fmt, ...) GCC_FMT_ATTR(2, 3); void monitor_flush(Monitor *mon); -int monitor_set_cpu(int cpu_index); +int monitor_set_cpu(Monitor *mon, int cpu_index); int monitor_get_cpu_index(void); =20 void monitor_read_command(MonitorHMP *mon, int show_prompt); diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c index 9c61e769ca..5e22ee2556 100644 --- a/monitor/hmp-cmds.c +++ b/monitor/hmp-cmds.c @@ -969,7 +969,7 @@ void hmp_cpu(Monitor *mon, const QDict *qdict) /* XXX: drop the monitor_set_cpu() usage when all HMP commands that use it are converted to the QAPI */ cpu_index =3D qdict_get_int(qdict, "index"); - if (monitor_set_cpu(cpu_index) < 0) { + if (monitor_set_cpu(mon, cpu_index) < 0) { monitor_printf(mon, "invalid CPU index\n"); } } diff --git a/monitor/misc.c b/monitor/misc.c index f5207cd242..bdf49e49e5 100644 --- a/monitor/misc.c +++ b/monitor/misc.c @@ -130,7 +130,7 @@ char *qmp_human_monitor_command(const char *command_lin= e, bool has_cpu_index, cur_mon =3D &hmp.common; =20 if (has_cpu_index) { - int ret =3D monitor_set_cpu(cpu_index); + int ret =3D monitor_set_cpu(&hmp.common, cpu_index); if (ret < 0) { cur_mon =3D old_mon; error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", @@ -256,7 +256,7 @@ static void monitor_init_qmp_commands(void) } =20 /* Set the current CPU defined by the user. Callers must hold BQL. */ -int monitor_set_cpu(int cpu_index) +int monitor_set_cpu(Monitor *mon, int cpu_index) { CPUState *cpu; =20 @@ -264,8 +264,8 @@ int monitor_set_cpu(int cpu_index) if (cpu =3D=3D NULL) { return -1; } - g_free(cur_mon->mon_cpu_path); - cur_mon->mon_cpu_path =3D object_get_canonical_path(OBJECT(cpu)); + g_free(mon->mon_cpu_path); + mon->mon_cpu_path =3D object_get_canonical_path(OBJECT(cpu)); return 0; } =20 @@ -286,7 +286,7 @@ static CPUState *mon_get_cpu_sync(bool synchronize) if (!first_cpu) { return NULL; } - monitor_set_cpu(first_cpu->cpu_index); + monitor_set_cpu(cur_mon, first_cpu->cpu_index); cpu =3D first_cpu; } assert(cpu !=3D NULL); --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680462; cv=none; d=zohomail.com; s=zohoarc; b=AeATImiUbtm0yoiiOqwuFwlYOXh+Lid9xsLoWgNh9Ts/MQMlN4wNAvC1NzwK4iPCBIb7dwRgChr+Nbiweg8ndi20tsd47JuP6RjV1cU2b/AdWBeNZlHwPI3LIf1Xf6PWBQoPcI11tainTgjzIF8QWJ9OQulqT/b+tfDNg4tscd4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680462; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=w1UUFRB/zGBi6YaDsUq4tRMmidAVx7J2pDG7x84C96U=; b=V18ZHRfonhJqyeFrN0v9uJQhMuw3SgnbsVv08zu/YZ3mfKiINfO0UZlyaNbjAxthOPqnzY5/QfkQDKjZAGcpYySoAmq8+LIAvoOMIEiU+IuA/+GWw5fl+Yeiz8/4oNABI44VgXEQmrJXuVIFUeXehZpUKvrVYfdsbS4NVqAw6JI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680462106621.994577310231; Thu, 28 May 2020 08:41:02 -0700 (PDT) Received: from localhost ([::1]:36672 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKeS-0001rJ-KL for importer@patchew.org; Thu, 28 May 2020 11:41:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32834) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKbb-0003Xv-4Q for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:03 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:44724 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbX-0003po-Jl for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:02 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-185-WQTAphhkPDKV3Y6YL7kwcA-1; Thu, 28 May 2020 11:37:56 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F2CB31937FF9; Thu, 28 May 2020 15:37:49 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id AB22B10013C0; Thu, 28 May 2020 15:37:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680278; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w1UUFRB/zGBi6YaDsUq4tRMmidAVx7J2pDG7x84C96U=; b=g/p0EWQ/pKEqhyIZGqWb3MDDxjB9rCcL//7M1QQ4sFh7cAbC7HMIP0xRyEPib1jqL4IKT6 orGt/3BRjwGwN/qe3nM5Z3MUve41r6O2M6Jigi8PZnoIWu400TNYJ4kDAAWoTSBBg3W8kC 43GWyO1QUntv/n7yCkH6tYaDcB87mG0= X-MC-Unique: WQTAphhkPDKV3Y6YL7kwcA-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 02/12] monitor: Use getter/setter functions for cur_mon Date: Thu, 28 May 2020 17:37:32 +0200 Message-Id: <20200528153742.274164-3-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=kwolf@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 03:11:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" cur_mon really needs to be coroutine-local as soon as we move monitor command handlers to coroutines and let them yield. As a first step, just remove all direct accesses to cur_mon so that we can implement this in the getter function later. Signed-off-by: Kevin Wolf --- include/monitor/monitor.h | 3 ++- audio/wavcapture.c | 8 ++++---- dump/dump.c | 2 +- hw/scsi/vhost-scsi.c | 2 +- hw/virtio/vhost-vsock.c | 2 +- migration/fd.c | 4 ++-- monitor/hmp.c | 10 +++++----- monitor/misc.c | 14 +++++++++----- monitor/monitor.c | 15 ++++++++++++++- monitor/qmp-cmds-control.c | 2 ++ monitor/qmp-cmds.c | 2 +- monitor/qmp.c | 6 +++--- net/socket.c | 2 +- net/tap.c | 6 +++--- stubs/monitor-core.c | 5 ++++- tests/test-util-sockets.c | 22 +++++++++++----------- trace/control.c | 2 +- util/qemu-error.c | 4 ++-- util/qemu-print.c | 3 ++- util/qemu-sockets.c | 1 + 20 files changed, 70 insertions(+), 45 deletions(-) diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index 0dcaefd4f9..43cc746078 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -5,7 +5,6 @@ #include "qapi/qapi-types-misc.h" #include "qemu/readline.h" =20 -extern __thread Monitor *cur_mon; typedef struct MonitorHMP MonitorHMP; typedef struct MonitorOptions MonitorOptions; =20 @@ -13,6 +12,8 @@ typedef struct MonitorOptions MonitorOptions; =20 extern QemuOptsList qemu_mon_opts; =20 +Monitor *monitor_cur(void); +void monitor_set_cur(Monitor *mon); bool monitor_cur_is_qmp(void); =20 void monitor_init_globals(void); diff --git a/audio/wavcapture.c b/audio/wavcapture.c index 8d7ce2eda1..e7dc97d16e 100644 --- a/audio/wavcapture.c +++ b/audio/wavcapture.c @@ -1,5 +1,5 @@ #include "qemu/osdep.h" -#include "monitor/monitor.h" +#include "qemu/qemu-print.h" #include "qapi/error.h" #include "qemu/error-report.h" #include "audio.h" @@ -94,9 +94,9 @@ static void wav_capture_info (void *opaque) WAVState *wav =3D opaque; char *path =3D wav->path; =20 - monitor_printf (cur_mon, "Capturing audio(%d,%d,%d) to %s: %d bytes\n", - wav->freq, wav->bits, wav->nchannels, - path ? path : "", wav->bytes); + qemu_printf("Capturing audio(%d,%d,%d) to %s: %d bytes\n", + wav->freq, wav->bits, wav->nchannels, + path ? path : "", wav->bytes); } =20 static struct capture_ops wav_capture_ops =3D { diff --git a/dump/dump.c b/dump/dump.c index 248ea06370..36d26159a0 100644 --- a/dump/dump.c +++ b/dump/dump.c @@ -1989,7 +1989,7 @@ void qmp_dump_guest_memory(bool paging, const char *f= ile, =20 #if !defined(WIN32) if (strstart(file, "fd:", &p)) { - fd =3D monitor_get_fd(cur_mon, p, errp); + fd =3D monitor_get_fd(monitor_cur(), p, errp); if (fd =3D=3D -1) { return; } diff --git a/hw/scsi/vhost-scsi.c b/hw/scsi/vhost-scsi.c index c1b012aea4..3920825bd6 100644 --- a/hw/scsi/vhost-scsi.c +++ b/hw/scsi/vhost-scsi.c @@ -177,7 +177,7 @@ static void vhost_scsi_realize(DeviceState *dev, Error = **errp) } =20 if (vs->conf.vhostfd) { - vhostfd =3D monitor_fd_param(cur_mon, vs->conf.vhostfd, errp); + vhostfd =3D monitor_fd_param(monitor_cur(), vs->conf.vhostfd, errp= ); if (vhostfd =3D=3D -1) { error_prepend(errp, "vhost-scsi: unable to parse vhostfd: "); return; diff --git a/hw/virtio/vhost-vsock.c b/hw/virtio/vhost-vsock.c index 4a228f5168..e72c9005b4 100644 --- a/hw/virtio/vhost-vsock.c +++ b/hw/virtio/vhost-vsock.c @@ -317,7 +317,7 @@ static void vhost_vsock_device_realize(DeviceState *dev= , Error **errp) } =20 if (vsock->conf.vhostfd) { - vhostfd =3D monitor_fd_param(cur_mon, vsock->conf.vhostfd, errp); + vhostfd =3D monitor_fd_param(monitor_cur(), vsock->conf.vhostfd, e= rrp); if (vhostfd =3D=3D -1) { error_prepend(errp, "vhost-vsock: unable to parse vhostfd: "); return; diff --git a/migration/fd.c b/migration/fd.c index 0a29ecdebf..6f2f50475f 100644 --- a/migration/fd.c +++ b/migration/fd.c @@ -26,7 +26,7 @@ void fd_start_outgoing_migration(MigrationState *s, const char *fdname, Er= ror **errp) { QIOChannel *ioc; - int fd =3D monitor_get_fd(cur_mon, fdname, errp); + int fd =3D monitor_get_fd(monitor_cur(), fdname, errp); if (fd =3D=3D -1) { return; } @@ -55,7 +55,7 @@ static gboolean fd_accept_incoming_migration(QIOChannel *= ioc, void fd_start_incoming_migration(const char *fdname, Error **errp) { QIOChannel *ioc; - int fd =3D monitor_fd_param(cur_mon, fdname, errp); + int fd =3D monitor_fd_param(monitor_cur(), fdname, errp); if (fd =3D=3D -1) { return; } diff --git a/monitor/hmp.c b/monitor/hmp.c index d598dd02bb..f609fcf75b 100644 --- a/monitor/hmp.c +++ b/monitor/hmp.c @@ -1301,11 +1301,11 @@ cleanup: static void monitor_read(void *opaque, const uint8_t *buf, int size) { MonitorHMP *mon; - Monitor *old_mon =3D cur_mon; + Monitor *old_mon =3D monitor_cur(); int i; =20 - cur_mon =3D opaque; - mon =3D container_of(cur_mon, MonitorHMP, common); + monitor_set_cur(opaque); + mon =3D container_of(monitor_cur(), MonitorHMP, common); =20 if (mon->rs) { for (i =3D 0; i < size; i++) { @@ -1313,13 +1313,13 @@ static void monitor_read(void *opaque, const uint8_= t *buf, int size) } } else { if (size =3D=3D 0 || buf[size - 1] !=3D 0) { - monitor_printf(cur_mon, "corrupted command\n"); + monitor_printf(&mon->common, "corrupted command\n"); } else { handle_hmp_command(mon, (char *)buf); } } =20 - cur_mon =3D old_mon; + monitor_set_cur(old_mon); } =20 static void monitor_event(void *opaque, QEMUChrEvent event) diff --git a/monitor/misc.c b/monitor/misc.c index bdf49e49e5..6cf7f60872 100644 --- a/monitor/misc.c +++ b/monitor/misc.c @@ -126,13 +126,13 @@ char *qmp_human_monitor_command(const char *command_l= ine, bool has_cpu_index, =20 monitor_data_init(&hmp.common, false, true, false); =20 - old_mon =3D cur_mon; - cur_mon =3D &hmp.common; + old_mon =3D monitor_cur(); + monitor_set_cur(&hmp.common); =20 if (has_cpu_index) { int ret =3D monitor_set_cpu(&hmp.common, cpu_index); if (ret < 0) { - cur_mon =3D old_mon; + monitor_set_cur(old_mon); error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number"); goto out; @@ -140,7 +140,7 @@ char *qmp_human_monitor_command(const char *command_lin= e, bool has_cpu_index, } =20 handle_hmp_command(&hmp, command_line); - cur_mon =3D old_mon; + monitor_set_cur(old_mon); =20 qemu_mutex_lock(&hmp.common.mon_lock); if (qstring_get_length(hmp.common.outbuf) > 0) { @@ -258,6 +258,7 @@ static void monitor_init_qmp_commands(void) /* Set the current CPU defined by the user. Callers must hold BQL. */ int monitor_set_cpu(Monitor *mon, int cpu_index) { + Monitor *cur_mon =3D monitor_cur(); CPUState *cpu; =20 cpu =3D qemu_get_cpu(cpu_index); @@ -272,6 +273,7 @@ int monitor_set_cpu(Monitor *mon, int cpu_index) /* Callers must hold BQL. */ static CPUState *mon_get_cpu_sync(bool synchronize) { + Monitor *cur_mon =3D monitor_cur(); CPUState *cpu =3D NULL; =20 if (cur_mon->mon_cpu_path) { @@ -1232,6 +1234,7 @@ static void hmp_acl_remove(Monitor *mon, const QDict = *qdict) =20 void qmp_getfd(const char *fdname, Error **errp) { + Monitor *cur_mon =3D monitor_cur(); mon_fd_t *monfd; int fd, tmp_fd; =20 @@ -1272,6 +1275,7 @@ void qmp_getfd(const char *fdname, Error **errp) =20 void qmp_closefd(const char *fdname, Error **errp) { + Monitor *cur_mon =3D monitor_cur(); mon_fd_t *monfd; int tmp_fd; =20 @@ -1361,7 +1365,7 @@ AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdse= t_id, bool has_opaque, const char *opaque, Error **errp) { int fd; - Monitor *mon =3D cur_mon; + Monitor *mon =3D monitor_cur(); AddfdInfo *fdinfo; =20 fd =3D qemu_chr_fe_get_msgfd(&mon->chr); diff --git a/monitor/monitor.c b/monitor/monitor.c index 125494410a..182ba136b4 100644 --- a/monitor/monitor.c +++ b/monitor/monitor.c @@ -66,13 +66,24 @@ MonitorList mon_list; int mon_refcount; static bool monitor_destroyed; =20 -__thread Monitor *cur_mon; +static __thread Monitor *cur_monitor; + +Monitor *monitor_cur(void) +{ + return cur_monitor; +} + +void monitor_set_cur(Monitor *mon) +{ + cur_monitor =3D mon; +} =20 /** * Is the current monitor, if any, a QMP monitor? */ bool monitor_cur_is_qmp(void) { + Monitor *cur_mon =3D monitor_cur(); return cur_mon && monitor_is_qmp(cur_mon); } =20 @@ -209,6 +220,7 @@ int monitor_printf(Monitor *mon, const char *fmt, ...) */ int error_vprintf(const char *fmt, va_list ap) { + Monitor *cur_mon =3D monitor_cur(); if (cur_mon && !monitor_cur_is_qmp()) { return monitor_vprintf(cur_mon, fmt, ap); } @@ -217,6 +229,7 @@ int error_vprintf(const char *fmt, va_list ap) =20 int error_vprintf_unless_qmp(const char *fmt, va_list ap) { + Monitor *cur_mon =3D monitor_cur(); if (!cur_mon) { return vfprintf(stderr, fmt, ap); } diff --git a/monitor/qmp-cmds-control.c b/monitor/qmp-cmds-control.c index 8f04cfa6e6..a456762f6a 100644 --- a/monitor/qmp-cmds-control.c +++ b/monitor/qmp-cmds-control.c @@ -69,6 +69,7 @@ static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilit= yList *list, void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable, Error **errp) { + Monitor *cur_mon =3D monitor_cur(); MonitorQMP *mon; =20 assert(monitor_is_qmp(cur_mon)); @@ -119,6 +120,7 @@ static void query_commands_cb(const QmpCommand *cmd, vo= id *opaque) CommandInfoList *qmp_query_commands(Error **errp) { CommandInfoList *list =3D NULL; + Monitor *cur_mon =3D monitor_cur(); MonitorQMP *mon; =20 assert(monitor_is_qmp(cur_mon)); diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c index 864cbfa32e..c7bf6bb4dc 100644 --- a/monitor/qmp-cmds.c +++ b/monitor/qmp-cmds.c @@ -327,7 +327,7 @@ void qmp_add_client(const char *protocol, const char *f= dname, Chardev *s; int fd; =20 - fd =3D monitor_get_fd(cur_mon, fdname, errp); + fd =3D monitor_get_fd(monitor_cur(), fdname, errp); if (fd < 0) { return; } diff --git a/monitor/qmp.c b/monitor/qmp.c index d433ceae5b..5e9abd4711 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -139,12 +139,12 @@ static void monitor_qmp_dispatch(MonitorQMP *mon, QOb= ject *req) QDict *rsp; QDict *error; =20 - old_mon =3D cur_mon; - cur_mon =3D &mon->common; + old_mon =3D monitor_cur(); + monitor_set_cur(&mon->common); =20 rsp =3D qmp_dispatch(mon->commands, req, qmp_oob_enabled(mon)); =20 - cur_mon =3D old_mon; + monitor_set_cur(old_mon); =20 if (mon->commands =3D=3D &qmp_cap_negotiation_commands) { error =3D qdict_get_qdict(rsp, "error"); diff --git a/net/socket.c b/net/socket.c index c92354049b..93bee968a8 100644 --- a/net/socket.c +++ b/net/socket.c @@ -727,7 +727,7 @@ int net_init_socket(const Netdev *netdev, const char *n= ame, if (sock->has_fd) { int fd; =20 - fd =3D monitor_fd_param(cur_mon, sock->fd, errp); + fd =3D monitor_fd_param(monitor_cur(), sock->fd, errp); if (fd =3D=3D -1) { return -1; } diff --git a/net/tap.c b/net/tap.c index 6207f61f84..fd7d15936d 100644 --- a/net/tap.c +++ b/net/tap.c @@ -689,7 +689,7 @@ static void net_init_tap_one(const NetdevTapOptions *ta= p, NetClientState *peer, } =20 if (vhostfdname) { - vhostfd =3D monitor_fd_param(cur_mon, vhostfdname, &err); + vhostfd =3D monitor_fd_param(monitor_cur(), vhostfdname, &err); if (vhostfd =3D=3D -1) { if (tap->has_vhostforce && tap->vhostforce) { error_propagate(errp, err); @@ -789,7 +789,7 @@ int net_init_tap(const Netdev *netdev, const char *name, return -1; } =20 - fd =3D monitor_fd_param(cur_mon, tap->fd, &err); + fd =3D monitor_fd_param(monitor_cur(), tap->fd, &err); if (fd =3D=3D -1) { error_propagate(errp, err); return -1; @@ -836,7 +836,7 @@ int net_init_tap(const Netdev *netdev, const char *name, } =20 for (i =3D 0; i < nfds; i++) { - fd =3D monitor_fd_param(cur_mon, fds[i], &err); + fd =3D monitor_fd_param(monitor_cur(), fds[i], &err); if (fd =3D=3D -1) { error_propagate(errp, err); ret =3D -1; diff --git a/stubs/monitor-core.c b/stubs/monitor-core.c index 6cff1c4e1d..0cd2d864b2 100644 --- a/stubs/monitor-core.c +++ b/stubs/monitor-core.c @@ -3,7 +3,10 @@ #include "qemu-common.h" #include "qapi/qapi-emit-events.h" =20 -__thread Monitor *cur_mon; +Monitor *monitor_cur(void) +{ + return NULL; +} =20 void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp) { diff --git a/tests/test-util-sockets.c b/tests/test-util-sockets.c index 2ca1e99f17..36fabb5e46 100644 --- a/tests/test-util-sockets.c +++ b/tests/test-util-sockets.c @@ -53,27 +53,27 @@ static void test_fd_is_socket_good(void) static int mon_fd =3D -1; static const char *mon_fdname; =20 -int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) -{ - g_assert(cur_mon); - g_assert(mon =3D=3D cur_mon); - if (mon_fd =3D=3D -1 || !g_str_equal(mon_fdname, fdname)) { - error_setg(errp, "No fd named %s", fdname); - return -1; - } - return dup(mon_fd); -} - /* Syms in libqemustub.a are discarded at .o file granularity. * To replace monitor_get_fd() we must ensure everything in * stubs/monitor.c is defined, to make sure monitor.o is discarded * otherwise we get duplicate syms at link time. */ __thread Monitor *cur_mon; +Monitor *monitor_cur(void) { return cur_mon; } int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { abort(); } void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp) {} void monitor_init_hmp(Chardev *chr, bool use_readline, Error **errp) {} =20 +int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp) +{ + g_assert(cur_mon); + g_assert(mon =3D=3D cur_mon); + if (mon_fd =3D=3D -1 || !g_str_equal(mon_fdname, fdname)) { + error_setg(errp, "No fd named %s", fdname); + return -1; + } + return dup(mon_fd); +} =20 static void test_socket_fd_pass_name_good(void) { diff --git a/trace/control.c b/trace/control.c index 2ffe000818..62993daf64 100644 --- a/trace/control.c +++ b/trace/control.c @@ -176,7 +176,7 @@ void trace_enable_events(const char *line_buf) { if (is_help_option(line_buf)) { trace_list_events(); - if (cur_mon =3D=3D NULL) { + if (monitor_cur() =3D=3D NULL) { exit(0); } } else { diff --git a/util/qemu-error.c b/util/qemu-error.c index dac7c7dc50..8d4ed723f5 100644 --- a/util/qemu-error.c +++ b/util/qemu-error.c @@ -169,7 +169,7 @@ static void print_loc(void) int i; const char *const *argp; =20 - if (!cur_mon && progname) { + if (!monitor_cur() && progname) { fprintf(stderr, "%s:", progname); sep =3D " "; } @@ -206,7 +206,7 @@ static void vreport(report_type type, const char *fmt, = va_list ap) GTimeVal tv; gchar *timestr; =20 - if (error_with_timestamp && !cur_mon) { + if (error_with_timestamp && !monitor_cur()) { g_get_current_time(&tv); timestr =3D g_time_val_to_iso8601(&tv); error_printf("%s ", timestr); diff --git a/util/qemu-print.c b/util/qemu-print.c index e79d6b8396..69ba612f56 100644 --- a/util/qemu-print.c +++ b/util/qemu-print.c @@ -20,6 +20,7 @@ */ int qemu_vprintf(const char *fmt, va_list ap) { + Monitor *cur_mon =3D monitor_cur(); if (cur_mon) { return monitor_vprintf(cur_mon, fmt, ap); } @@ -48,7 +49,7 @@ int qemu_printf(const char *fmt, ...) int qemu_vfprintf(FILE *stream, const char *fmt, va_list ap) { if (!stream) { - return monitor_vprintf(cur_mon, fmt, ap); + return monitor_vprintf(monitor_cur(), fmt, ap); } return vfprintf(stream, fmt, ap); } diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index b37d288866..40c18ba142 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -1092,6 +1092,7 @@ fail: =20 static int socket_get_fd(const char *fdstr, int num, Error **errp) { + Monitor *cur_mon =3D monitor_cur(); int fd; if (num !=3D 1) { error_setg_errno(errp, EINVAL, "socket_get_fd: too many connection= s"); --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680357; cv=none; d=zohomail.com; s=zohoarc; b=Usx7bfZKAWKHr41xbk+xomDV6stabviQhj6MJCW0t0xiLcVLF8O7pfAa6Lj8FJwueDvaHDCB1SwTjQC5GD2KszDf8LOUZ/vHINsl702D0e8n7oLgPqsTSS1r9Wdy68qshSQz65NP0b7J0ZIgg6mof4hKi3S40YfhsuQQICobD/Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680357; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=IaYfUg0LoZ4HbRDH1lcQFLzl/VwdwL4XsBE6SSYAbK8=; b=BabJo1Z2wbTdrOTYXuzqrynAXhlNhyKjA6EzEHPA66QnRaqIvase4T80zJNLXfY0PnMh9Us5N64chDhrm7n/VKz0ykzP+Ywo0PyW5bkTHZkfpxCvcYn/M8ojHjUwJdh0JkRFHsc3Rx3i+Y+cs1AMXOzzRNST2T900Kck4QDJBgI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680357009788.5606589706254; Thu, 28 May 2020 08:39:17 -0700 (PDT) Received: from localhost ([::1]:56492 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKcl-0005ZT-OV for importer@patchew.org; Thu, 28 May 2020 11:39:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32818) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKba-0003VX-30 for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:02 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:57294 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbY-0003rd-Ul for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:01 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-274-WnekTmMBOri-NMyeoBePxQ-1; Thu, 28 May 2020 11:37:58 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 69D97EC1C9; Thu, 28 May 2020 15:37:51 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 48C1410013C0; Thu, 28 May 2020 15:37:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680280; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IaYfUg0LoZ4HbRDH1lcQFLzl/VwdwL4XsBE6SSYAbK8=; b=EWQa5oU4jNY4E8Ds92fRbUEq+PupUj/ydCMEJoUalBeeD6RJNAFTQHI9uyFqsVDUo3s+pc UThpBpsUa2npRetqQKCV/FVVFMwZvLHOr+L9pIyWMv0yuOouo1m/fbgpdCBT1jHjV6mc6A +ahqOe2oeFm/DKAnL7sb3JL9+vtoUxU= X-MC-Unique: WnekTmMBOri-NMyeoBePxQ-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 03/12] hmp: Set cur_mon only in handle_hmp_command() Date: Thu, 28 May 2020 17:37:33 +0200 Message-Id: <20200528153742.274164-4-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.61; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 06:31:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" cur_mon is updated relatively early in the command handling code even though only the command handler actually needs it. Move it to handle_hmp_command(). Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake --- monitor/hmp.c | 23 ++++++++++++----------- monitor/misc.c | 7 ------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/monitor/hmp.c b/monitor/hmp.c index f609fcf75b..79be6f26de 100644 --- a/monitor/hmp.c +++ b/monitor/hmp.c @@ -1061,6 +1061,7 @@ void handle_hmp_command(MonitorHMP *mon, const char *= cmdline) QDict *qdict; const HMPCommand *cmd; const char *cmd_start =3D cmdline; + Monitor *old_mon; =20 trace_handle_hmp_command(mon, cmdline); =20 @@ -1079,7 +1080,12 @@ void handle_hmp_command(MonitorHMP *mon, const char = *cmdline) return; } =20 + /* old_mon is non-NULL when called from qmp_human_monitor_command() */ + old_mon =3D monitor_cur(); + monitor_set_cur(&mon->common); cmd->cmd(&mon->common, qdict); + monitor_set_cur(old_mon); + qobject_unref(qdict); } =20 @@ -1300,26 +1306,21 @@ cleanup: =20 static void monitor_read(void *opaque, const uint8_t *buf, int size) { - MonitorHMP *mon; - Monitor *old_mon =3D monitor_cur(); + Monitor *mon =3D opaque; + MonitorHMP *hmp_mon =3D container_of(mon, MonitorHMP, common); int i; =20 - monitor_set_cur(opaque); - mon =3D container_of(monitor_cur(), MonitorHMP, common); - - if (mon->rs) { + if (hmp_mon->rs) { for (i =3D 0; i < size; i++) { - readline_handle_byte(mon->rs, buf[i]); + readline_handle_byte(hmp_mon->rs, buf[i]); } } else { if (size =3D=3D 0 || buf[size - 1] !=3D 0) { - monitor_printf(&mon->common, "corrupted command\n"); + monitor_printf(mon, "corrupted command\n"); } else { - handle_hmp_command(mon, (char *)buf); + handle_hmp_command(hmp_mon, (char *)buf); } } - - monitor_set_cur(old_mon); } =20 static void monitor_event(void *opaque, QEMUChrEvent event) diff --git a/monitor/misc.c b/monitor/misc.c index 6cf7f60872..e0ab265726 100644 --- a/monitor/misc.c +++ b/monitor/misc.c @@ -121,18 +121,13 @@ char *qmp_human_monitor_command(const char *command_l= ine, bool has_cpu_index, int64_t cpu_index, Error **errp) { char *output =3D NULL; - Monitor *old_mon; MonitorHMP hmp =3D {}; =20 monitor_data_init(&hmp.common, false, true, false); =20 - old_mon =3D monitor_cur(); - monitor_set_cur(&hmp.common); - if (has_cpu_index) { int ret =3D monitor_set_cpu(&hmp.common, cpu_index); if (ret < 0) { - monitor_set_cur(old_mon); error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number"); goto out; @@ -140,7 +135,6 @@ char *qmp_human_monitor_command(const char *command_lin= e, bool has_cpu_index, } =20 handle_hmp_command(&hmp, command_line); - monitor_set_cur(old_mon); =20 qemu_mutex_lock(&hmp.common.mon_lock); if (qstring_get_length(hmp.common.outbuf) > 0) { @@ -258,7 +252,6 @@ static void monitor_init_qmp_commands(void) /* Set the current CPU defined by the user. Callers must hold BQL. */ int monitor_set_cpu(Monitor *mon, int cpu_index) { - Monitor *cur_mon =3D monitor_cur(); CPUState *cpu; =20 cpu =3D qemu_get_cpu(cpu_index); --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680462; cv=none; d=zohomail.com; s=zohoarc; b=N1/aouVToO3IcSt6zO7AkB0YV1R5PrlGGm3dSY+JZ75H4Vsv4uNkLz3LZ9gjkzyzHUxyvZlYx8HlwkQZWI/jdPJO6AZW5odx0SUiaPRS1n5mCwLI51qjqm9He1ylYsobXRVMGbDju5X4Di3keTESl/ObGsYT8A7fJdQTNtmwGEg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680462; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=6knHWgY9k0hW6npSPjW6s6NOISm5VV69fKYc5XBJRVM=; b=VcAAAnW5ErHz8fbL/ND8sPF0321nxEYJCCQBMrG5zgt0sdUr7I3EwijdsNed16tc5ZznKx64/9s6GnQMfVUneLVL8fYo0gQE/1mWWUcF49Z3KPhwibrxY4LFAkZaDqqi4y9CkYJRAWV8hNHXtjwLSKUAf4AwB9Rkbqj3/IrNpnQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680462805526.8105987228928; Thu, 28 May 2020 08:41:02 -0700 (PDT) Received: from localhost ([::1]:36798 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKeT-0001vt-G9 for importer@patchew.org; Thu, 28 May 2020 11:41:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32858) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKbd-0003e2-7k for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:05 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:31336 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbc-0003uM-Fp for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:04 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-273-xxIcAfVkPmedibAkLpMyHA-1; Thu, 28 May 2020 11:37:57 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D3BB4474; Thu, 28 May 2020 15:37:52 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id B365810013C0; Thu, 28 May 2020 15:37:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680283; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6knHWgY9k0hW6npSPjW6s6NOISm5VV69fKYc5XBJRVM=; b=d9BLc9kkwUO7HIjLG4T+I/CpcWwM/17GeQjdenf7cL6hENv314QC8rJ2pnQAGvtoh23HbR WvBMLqYAkTwWvC7jSACryZxf4BfwjFJf7GA3q5Wu5oSO88JDvdUtGSfdGF7cRqe4dyshbY HSv3jJMCLG1IgfPV8GiNtuWnaJRKndo= X-MC-Unique: xxIcAfVkPmedibAkLpMyHA-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 04/12] qmp: Assert that no other monitor is active Date: Thu, 28 May 2020 17:37:34 +0200 Message-Id: <20200528153742.274164-5-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 02:50:32 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" monitor_qmp_dispatch() is never supposed to be called in the context of another monitor, so assert that monitor_cur() is NULL instead of saving and restoring it. Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake Reviewed-by: Markus Armbruster --- monitor/qmp.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/monitor/qmp.c b/monitor/qmp.c index 5e9abd4711..a04c512e3a 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -135,16 +135,15 @@ static void monitor_qmp_respond(MonitorQMP *mon, QDic= t *rsp) =20 static void monitor_qmp_dispatch(MonitorQMP *mon, QObject *req) { - Monitor *old_mon; QDict *rsp; QDict *error; =20 - old_mon =3D monitor_cur(); + assert(monitor_cur() =3D=3D NULL); monitor_set_cur(&mon->common); =20 rsp =3D qmp_dispatch(mon->commands, req, qmp_oob_enabled(mon)); =20 - monitor_set_cur(old_mon); + monitor_set_cur(NULL); =20 if (mon->commands =3D=3D &qmp_cap_negotiation_commands) { error =3D qdict_get_qdict(rsp, "error"); --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680358; cv=none; d=zohomail.com; s=zohoarc; b=dqq4u2GdXQp3/NjJXCr/jCthPsmNnJt3lfWqMD6n6fuGvwilFvdgUfHnQM7mCVbIq0Ib+u3mlacLcQcHPZTsAk3W8yxmJfbTc293s7eVc/4iJNwzvqbB2C/zNQiMTBfKv+ZoyKzDSCEFwabStXoCar1pzIAXCzBBX1w0M9AcMIo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680358; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=B4LexHoudOzQH8ZJmfkxtCN4z6E07Fs+hf9Vl3eAp8k=; b=fzrVhser2YOUUuTVbUNvvTnEU2cUul8gaIOyGn48w+kXyYWBHa691bPovarO14c5FCh31SOx0tD4XboNkCFnna2+Q/+W59USMy5cRdKv6qgYMpBpMEvEH+3jM258Y43EIAe/eSOiLBIjt4SJ6FWGMu1iOFy47Nk8Auyu4XZYu/w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680358505184.65617723687058; Thu, 28 May 2020 08:39:18 -0700 (PDT) Received: from localhost ([::1]:56702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKcn-0005fb-5v for importer@patchew.org; Thu, 28 May 2020 11:39:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32824) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKba-0003Wj-KS for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:02 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:25697 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbZ-0003sa-Jq for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:02 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-393-D-ABs1SKMkueEsiTvjxysA-1; Thu, 28 May 2020 11:37:59 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 491FF88EB5B; Thu, 28 May 2020 15:37:54 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 28F5E10013C0; Thu, 28 May 2020 15:37:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680281; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=B4LexHoudOzQH8ZJmfkxtCN4z6E07Fs+hf9Vl3eAp8k=; b=TxRS0KkSnHHixj0DJ6rMeqGEf5o7926Qc0ICf88GRH5NholZbnrJbb/SHZARpKuTYgouxB drdOv5xulVWmVQPPf/AFPUycZXfBEk7jKXvaDfMcGk21I4XcAybR5aki40Oyxis1j1pN7d fz6kibxbKTjepls7DHaOp/bQqhpjFl8= X-MC-Unique: D-ABs1SKMkueEsiTvjxysA-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 05/12] qmp: Call monitor_set_cur() only in qmp_dispatch() Date: Thu, 28 May 2020 17:37:35 +0200 Message-Id: <20200528153742.274164-6-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=kwolf@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 03:11:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The correct way to set the current monitor for a coroutine handler is different that for a blocking handler, so monitor_set_cur() can only be called in qmp_dispatch(). Signed-off-by: Kevin Wolf --- include/qapi/qmp/dispatch.h | 3 ++- monitor/qmp.c | 7 +------ qapi/qmp-dispatch.c | 8 +++++++- qga/main.c | 2 +- stubs/monitor-core.c | 4 ++++ tests/test-qmp-cmds.c | 6 +++--- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/qapi/qmp/dispatch.h b/include/qapi/qmp/dispatch.h index 5a9cf82472..0c2f467028 100644 --- a/include/qapi/qmp/dispatch.h +++ b/include/qapi/qmp/dispatch.h @@ -14,6 +14,7 @@ #ifndef QAPI_QMP_DISPATCH_H #define QAPI_QMP_DISPATCH_H =20 +#include "monitor/monitor.h" #include "qemu/queue.h" =20 typedef void (QmpCommandFunc)(QDict *, QObject **, Error **); @@ -49,7 +50,7 @@ const char *qmp_command_name(const QmpCommand *cmd); bool qmp_has_success_response(const QmpCommand *cmd); QDict *qmp_error_response(Error *err); QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request, - bool allow_oob); + bool allow_oob, Monitor *cur_mon); bool qmp_is_oob(const QDict *dict); =20 typedef void (*qmp_cmd_callback_fn)(const QmpCommand *cmd, void *opaque); diff --git a/monitor/qmp.c b/monitor/qmp.c index a04c512e3a..922fdb5541 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -138,12 +138,7 @@ static void monitor_qmp_dispatch(MonitorQMP *mon, QObj= ect *req) QDict *rsp; QDict *error; =20 - assert(monitor_cur() =3D=3D NULL); - monitor_set_cur(&mon->common); - - rsp =3D qmp_dispatch(mon->commands, req, qmp_oob_enabled(mon)); - - monitor_set_cur(NULL); + rsp =3D qmp_dispatch(mon->commands, req, qmp_oob_enabled(mon), &mon->c= ommon); =20 if (mon->commands =3D=3D &qmp_cap_negotiation_commands) { error =3D qdict_get_qdict(rsp, "error"); diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c index 79347e0864..2fdbc0fba4 100644 --- a/qapi/qmp-dispatch.c +++ b/qapi/qmp-dispatch.c @@ -89,7 +89,7 @@ bool qmp_is_oob(const QDict *dict) } =20 QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request, - bool allow_oob) + bool allow_oob, Monitor *cur_mon) { Error *err =3D NULL; bool oob; @@ -152,7 +152,13 @@ QDict *qmp_dispatch(const QmpCommandList *cmds, QObjec= t *request, args =3D qdict_get_qdict(dict, "arguments"); qobject_ref(args); } + + assert(monitor_cur() =3D=3D NULL); + monitor_set_cur(cur_mon); + cmd->fn(args, &ret, &err); + + monitor_set_cur(NULL); qobject_unref(args); if (err) { /* or assert(!ret) after reviewing all handlers: */ diff --git a/qga/main.c b/qga/main.c index f0e454f28d..1042c4e7d3 100644 --- a/qga/main.c +++ b/qga/main.c @@ -574,7 +574,7 @@ static void process_event(void *opaque, QObject *obj, E= rror *err) } =20 g_debug("processing command"); - rsp =3D qmp_dispatch(&ga_commands, obj, false); + rsp =3D qmp_dispatch(&ga_commands, obj, false, NULL); =20 end: ret =3D send_response(s, rsp); diff --git a/stubs/monitor-core.c b/stubs/monitor-core.c index 0cd2d864b2..e493df1027 100644 --- a/stubs/monitor-core.c +++ b/stubs/monitor-core.c @@ -8,6 +8,10 @@ Monitor *monitor_cur(void) return NULL; } =20 +void monitor_set_cur(Monitor *mon) +{ +} + void monitor_init_qmp(Chardev *chr, bool pretty, Error **errp) { } diff --git a/tests/test-qmp-cmds.c b/tests/test-qmp-cmds.c index d12ff47e26..5f1b245e19 100644 --- a/tests/test-qmp-cmds.c +++ b/tests/test-qmp-cmds.c @@ -152,7 +152,7 @@ static QObject *do_qmp_dispatch(bool allow_oob, const c= har *template, ...) req =3D qdict_from_vjsonf_nofail(template, ap); va_end(ap); =20 - resp =3D qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob); + resp =3D qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob, NULL); g_assert(resp); ret =3D qdict_get(resp, "return"); g_assert(ret); @@ -175,7 +175,7 @@ static void do_qmp_dispatch_error(bool allow_oob, Error= Class cls, req =3D qdict_from_vjsonf_nofail(template, ap); va_end(ap); =20 - resp =3D qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob); + resp =3D qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob, NULL); g_assert(resp); error =3D qdict_get_qdict(resp, "error"); g_assert(error); @@ -231,7 +231,7 @@ static void test_dispatch_cmd_success_response(void) QDict *resp; =20 qdict_put_str(req, "execute", "cmd-success-response"); - resp =3D qmp_dispatch(&qmp_commands, QOBJECT(req), false); + resp =3D qmp_dispatch(&qmp_commands, QOBJECT(req), false, NULL); g_assert_null(resp); qobject_unref(req); } --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680477; cv=none; d=zohomail.com; s=zohoarc; b=fay4cWs+aravpUet0qp2FMTHTq4OtvAUMMu2Kv/DNZLoKYfHqGA+lIafCyv6GdFclXm2F3+NZZhumuo2O5jayEROHIKEPFVlXQ9sCFTOqpJId2dOOjfF978kanw5t2wPr6wRYDTa34dckD888s2l+3R6RCXtWuUgUz9ErOYLP5A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680477; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=WiSPCp13UGRzy7IvL67HlfHcthifFpQiSk7BKE1sZPM=; b=UMjnCatuM3jERMtYIpIVOpe/Vf3o9oDc+72Fws5dXDmRJiTUnx8+XmasWXLVasaMuVRKH6cBtBwdorrrIMLDLcqAMSGrsBb4w1Ju4tI61S09pxv/ofLbrbfyXar18jgb3+CRGn7u7qebHR1dH1OFpich0sdeWIczu9TOfTSLa44= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680477792475.9720169305499; Thu, 28 May 2020 08:41:17 -0700 (PDT) Received: from localhost ([::1]:38210 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKei-0002yO-Fm for importer@patchew.org; Thu, 28 May 2020 11:41:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32856) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKbd-0003dJ-0g for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:05 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:51177 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKba-0003tM-PY for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:04 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-191-VrNrRcTwNNWnFhozHrJYbg-1; Thu, 28 May 2020 11:37:58 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 29082102C874; Thu, 28 May 2020 15:37:57 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 924F21001B07; Thu, 28 May 2020 15:37:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680282; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WiSPCp13UGRzy7IvL67HlfHcthifFpQiSk7BKE1sZPM=; b=Mx+zedPVo0PUh/EcOWa43e52fW7dvNv9FxJp4hHecbv9AAsmnY+1rmgCY+rdmmLxQNRarW meI/aoerT+KO0DKtz4Ga1G+g0IOBDWw2RVuXFZMHtfrenBpzuvi47Kze08J4J8HldWsALn ulmiYlzhfvrblHmjCqJMGBDGOJESwqo= X-MC-Unique: VrNrRcTwNNWnFhozHrJYbg-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 06/12] monitor: Make current monitor a per-coroutine property Date: Thu, 28 May 2020 17:37:36 +0200 Message-Id: <20200528153742.274164-7-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=kwolf@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 03:11:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This way, a monitor command handler will still be able to access the current monitor, but when it yields, all other code code will correctly get NULL from monitor_cur(). Outside of coroutine context, qemu_coroutine_self() returns the leader coroutine of the current thread. Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake --- include/monitor/monitor.h | 2 +- monitor/hmp.c | 4 ++-- monitor/monitor.c | 27 +++++++++++++++++++++------ qapi/qmp-dispatch.c | 4 ++-- stubs/monitor-core.c | 2 +- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/include/monitor/monitor.h b/include/monitor/monitor.h index 43cc746078..16072e325c 100644 --- a/include/monitor/monitor.h +++ b/include/monitor/monitor.h @@ -13,7 +13,7 @@ typedef struct MonitorOptions MonitorOptions; extern QemuOptsList qemu_mon_opts; =20 Monitor *monitor_cur(void); -void monitor_set_cur(Monitor *mon); +void monitor_set_cur(Coroutine *co, Monitor *mon); bool monitor_cur_is_qmp(void); =20 void monitor_init_globals(void); diff --git a/monitor/hmp.c b/monitor/hmp.c index 79be6f26de..3e73a4c3ce 100644 --- a/monitor/hmp.c +++ b/monitor/hmp.c @@ -1082,9 +1082,9 @@ void handle_hmp_command(MonitorHMP *mon, const char *= cmdline) =20 /* old_mon is non-NULL when called from qmp_human_monitor_command() */ old_mon =3D monitor_cur(); - monitor_set_cur(&mon->common); + monitor_set_cur(qemu_coroutine_self(), &mon->common); cmd->cmd(&mon->common, qdict); - monitor_set_cur(old_mon); + monitor_set_cur(qemu_coroutine_self(), old_mon); =20 qobject_unref(qdict); } diff --git a/monitor/monitor.c b/monitor/monitor.c index 182ba136b4..35003bb486 100644 --- a/monitor/monitor.c +++ b/monitor/monitor.c @@ -58,24 +58,38 @@ IOThread *mon_iothread; /* Bottom half to dispatch the requests received from I/O thread */ QEMUBH *qmp_dispatcher_bh; =20 -/* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */ +/* + * Protects mon_list, monitor_qapi_event_state, coroutine_mon, + * monitor_destroyed. + */ QemuMutex monitor_lock; static GHashTable *monitor_qapi_event_state; +static GHashTable *coroutine_mon; /* Maps Coroutine* to Monitor* */ =20 MonitorList mon_list; int mon_refcount; static bool monitor_destroyed; =20 -static __thread Monitor *cur_monitor; - Monitor *monitor_cur(void) { - return cur_monitor; + Monitor *mon; + + qemu_mutex_lock(&monitor_lock); + mon =3D g_hash_table_lookup(coroutine_mon, qemu_coroutine_self()); + qemu_mutex_unlock(&monitor_lock); + + return mon; } =20 -void monitor_set_cur(Monitor *mon) +void monitor_set_cur(Coroutine *co, Monitor *mon) { - cur_monitor =3D mon; + qemu_mutex_lock(&monitor_lock); + if (mon) { + g_hash_table_replace(coroutine_mon, co, mon); + } else { + g_hash_table_remove(coroutine_mon, co); + } + qemu_mutex_unlock(&monitor_lock); } =20 /** @@ -613,6 +627,7 @@ void monitor_init_globals_core(void) { monitor_qapi_event_init(); qemu_mutex_init(&monitor_lock); + coroutine_mon =3D g_hash_table_new(NULL, NULL); =20 /* * The dispatcher BH must run in the main loop thread, since we diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c index 2fdbc0fba4..5677ba92ca 100644 --- a/qapi/qmp-dispatch.c +++ b/qapi/qmp-dispatch.c @@ -154,11 +154,11 @@ QDict *qmp_dispatch(const QmpCommandList *cmds, QObje= ct *request, } =20 assert(monitor_cur() =3D=3D NULL); - monitor_set_cur(cur_mon); + monitor_set_cur(qemu_coroutine_self(), cur_mon); =20 cmd->fn(args, &ret, &err); =20 - monitor_set_cur(NULL); + monitor_set_cur(qemu_coroutine_self(), NULL); qobject_unref(args); if (err) { /* or assert(!ret) after reviewing all handlers: */ diff --git a/stubs/monitor-core.c b/stubs/monitor-core.c index e493df1027..635e37a6ba 100644 --- a/stubs/monitor-core.c +++ b/stubs/monitor-core.c @@ -8,7 +8,7 @@ Monitor *monitor_cur(void) return NULL; } =20 -void monitor_set_cur(Monitor *mon) +void monitor_set_cur(Coroutine *co, Monitor *mon) { } =20 --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680606; cv=none; d=zohomail.com; s=zohoarc; b=KVfNQfHlg92ZY3CZzVGnYEpIVfTx1unpVVCqPHEUlml6T7kN1F0uYU6DekALmRVwptcHUjX28390R4pEJjZFbIXv/ug8hqTTChc5be3wzfXlec9XZm8U3zzIWFEsQkmU/GjXJWiVC6RqaD++wmLMk19CTWqeCYHX0pNid4cspM4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680606; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=+RZrr6ic9RBOFFKgER+QKdZ9x8KUuan9eOf+y/q7WVY=; b=OOq6s/gTU9dSPHprNOgUY2EK9r+HKHxMBddT2JbQiUa8yfShE/rqA7/kD6OkADUCxJfdb7lwMtFPPi8IxSD1sQwe3sk8IHTaZeyRQl8tSXlvDTbS3qzBXqWCgmRyOYY1/n04kueUvMotLKY3hRgTR6/D3ippcgmNFT+EjFOTYic= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680606647709.1341941905157; Thu, 28 May 2020 08:43:26 -0700 (PDT) Received: from localhost ([::1]:46044 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKgn-0007nl-1V for importer@patchew.org; Thu, 28 May 2020 11:43:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32870) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKbf-0003lb-Qk for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:07 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:39978 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbe-0003vf-0L for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:07 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-184-kpVqrWN9Mz-MuPEEa--s9A-1; Thu, 28 May 2020 11:38:01 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0A1F81855A16; Thu, 28 May 2020 15:38:00 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7305C1002382; Thu, 28 May 2020 15:37:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680285; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+RZrr6ic9RBOFFKgER+QKdZ9x8KUuan9eOf+y/q7WVY=; b=e52mzOih8fPL1HIzIh1ZifgYs0eTrSBKJm1Zc2UQPfORmAONRy0/q/88VkTvuXPrHzHPi+ zJm9ff58iH/ILJPhJvshzmQTmq8alBQlV8Bnyw6xvAi4qA75XKspOkfmlXVvyj75VitiIp Cg8WvJ7uIMeFIvkxKGbZoEn+T4/LzCw= X-MC-Unique: kpVqrWN9Mz-MuPEEa--s9A-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 07/12] qapi: Add a 'coroutine' flag for commands Date: Thu, 28 May 2020 17:37:37 +0200 Message-Id: <20200528153742.274164-8-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.120; envelope-from=kwolf@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 01:51:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) This patch adds a new 'coroutine' flag to QMP command definitions that tells the QMP dispatcher that the command handler is safe to be run in a coroutine. The documentation of the new flag pretends that this flag is already used as intended, which it isn't yet after this patch. We'll implement this in another patch in this series. Signed-off-by: Kevin Wolf Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Markus Armbruster --- docs/devel/qapi-code-gen.txt | 12 ++++++++++++ include/qapi/qmp/dispatch.h | 1 + tests/test-qmp-cmds.c | 4 ++++ scripts/qapi/commands.py | 10 +++++++--- scripts/qapi/doc.py | 2 +- scripts/qapi/expr.py | 10 ++++++++-- scripts/qapi/introspect.py | 2 +- scripts/qapi/schema.py | 12 ++++++++---- tests/qapi-schema/test-qapi.py | 7 ++++--- tests/Makefile.include | 1 + tests/qapi-schema/oob-coroutine.err | 2 ++ tests/qapi-schema/oob-coroutine.json | 2 ++ tests/qapi-schema/oob-coroutine.out | 0 tests/qapi-schema/qapi-schema-test.json | 1 + tests/qapi-schema/qapi-schema-test.out | 2 ++ 15 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 tests/qapi-schema/oob-coroutine.err create mode 100644 tests/qapi-schema/oob-coroutine.json create mode 100644 tests/qapi-schema/oob-coroutine.out diff --git a/docs/devel/qapi-code-gen.txt b/docs/devel/qapi-code-gen.txt index a7794ef658..b8eb370a13 100644 --- a/docs/devel/qapi-code-gen.txt +++ b/docs/devel/qapi-code-gen.txt @@ -472,6 +472,7 @@ Syntax: '*gen': false, '*allow-oob': true, '*allow-preconfig': true, + '*coroutine': true, '*if': COND, '*features': FEATURES } =20 @@ -596,6 +597,17 @@ before the machine is built. It defaults to false. F= or example: QMP is available before the machine is built only when QEMU was started with --preconfig. =20 +Member 'coroutine' tells the QMP dispatcher whether the command handler +is safe to be run in a coroutine. It defaults to false. If it is true, +the command handler is called from coroutine context and may yield while +waiting for an external event (such as I/O completion) in order to avoid +blocking the guest and other background operations. + +It is an error to specify both 'coroutine': true and 'allow-oob': true +for a command. We don't currently have a use case for both together and +without a use case, it's not entirely clear what the semantics should +be. + The optional 'if' member specifies a conditional. See "Configuring the schema" below for more on this. =20 diff --git a/include/qapi/qmp/dispatch.h b/include/qapi/qmp/dispatch.h index 0c2f467028..9fd2b720a7 100644 --- a/include/qapi/qmp/dispatch.h +++ b/include/qapi/qmp/dispatch.h @@ -25,6 +25,7 @@ typedef enum QmpCommandOptions QCO_NO_SUCCESS_RESP =3D (1U << 0), QCO_ALLOW_OOB =3D (1U << 1), QCO_ALLOW_PRECONFIG =3D (1U << 2), + QCO_COROUTINE =3D (1U << 3), } QmpCommandOptions; =20 typedef struct QmpCommand diff --git a/tests/test-qmp-cmds.c b/tests/test-qmp-cmds.c index 5f1b245e19..d3413bfef0 100644 --- a/tests/test-qmp-cmds.c +++ b/tests/test-qmp-cmds.c @@ -36,6 +36,10 @@ void qmp_cmd_success_response(Error **errp) { } =20 +void qmp_coroutine_cmd(Error **errp) +{ +} + Empty2 *qmp_user_def_cmd0(Error **errp) { return g_new0(Empty2, 1); diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py index 6809b0fb6e..f50710306d 100644 --- a/scripts/qapi/commands.py +++ b/scripts/qapi/commands.py @@ -180,7 +180,8 @@ out: return ret =20 =20 -def gen_register_command(name, success_response, allow_oob, allow_preconfi= g): +def gen_register_command(name, success_response, allow_oob, allow_preconfi= g, + coroutine): options =3D [] =20 if not success_response: @@ -189,6 +190,8 @@ def gen_register_command(name, success_response, allow_= oob, allow_preconfig): options +=3D ['QCO_ALLOW_OOB'] if allow_preconfig: options +=3D ['QCO_ALLOW_PRECONFIG'] + if coroutine: + options +=3D ['QCO_COROUTINE'] =20 if not options: options =3D ['QCO_NO_OPTIONS'] @@ -271,7 +274,7 @@ void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds); =20 def visit_command(self, name, info, ifcond, features, arg_type, ret_type, gen, success_response, boxed, - allow_oob, allow_preconfig): + allow_oob, allow_preconfig, coroutine): if not gen: return # FIXME: If T is a user-defined type, the user is responsible @@ -289,7 +292,8 @@ void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds); self._genh.add(gen_marshal_decl(name)) self._genc.add(gen_marshal(name, arg_type, boxed, ret_type)) self._regy.add(gen_register_command(name, success_response, - allow_oob, allow_preconfig= )) + allow_oob, allow_preconfig, + coroutine)) =20 =20 def gen_commands(schema, output_dir, prefix): diff --git a/scripts/qapi/doc.py b/scripts/qapi/doc.py index 92f584edcf..11771de923 100644 --- a/scripts/qapi/doc.py +++ b/scripts/qapi/doc.py @@ -264,7 +264,7 @@ class QAPISchemaGenDocVisitor(QAPISchemaVisitor): =20 def visit_command(self, name, info, ifcond, features, arg_type, ret_type, gen, success_response, boxed, - allow_oob, allow_preconfig): + allow_oob, allow_preconfig, coroutine): doc =3D self.cur_doc self._gen.add(texi_msg('Command', doc, ifcond, texi_arguments(doc, diff --git a/scripts/qapi/expr.py b/scripts/qapi/expr.py index 2942520399..928cd1eb5c 100644 --- a/scripts/qapi/expr.py +++ b/scripts/qapi/expr.py @@ -88,10 +88,16 @@ def check_flags(expr, info): if key in expr and expr[key] is not False: raise QAPISemError( info, "flag '%s' may only use false value" % key) - for key in ['boxed', 'allow-oob', 'allow-preconfig']: + for key in ['boxed', 'allow-oob', 'allow-preconfig', 'coroutine']: if key in expr and expr[key] is not True: raise QAPISemError( info, "flag '%s' may only use true value" % key) + if 'allow-oob' in expr and 'coroutine' in expr: + # This is not necessarily a fundamental incompatibility, but we do= n't + # have a use case and the desired semantics isn't obvious. The sim= plest + # solution is to forbid it until we get a use case for it. + raise QAPISemError(info, "flags 'allow-oob' and 'coroutine' " + "are incompatible") =20 =20 def check_if(expr, info, source): @@ -342,7 +348,7 @@ def check_exprs(exprs): ['command'], ['data', 'returns', 'boxed', 'if', 'features', 'gen', 'success-response', 'allow-oob', - 'allow-preconfig']) + 'allow-preconfig', 'coroutine']) normalize_members(expr.get('data')) check_command(expr, info) elif meta =3D=3D 'event': diff --git a/scripts/qapi/introspect.py b/scripts/qapi/introspect.py index 23652be810..5907b09cd5 100644 --- a/scripts/qapi/introspect.py +++ b/scripts/qapi/introspect.py @@ -216,7 +216,7 @@ const QLitObject %(c_name)s =3D %(c_string)s; =20 def visit_command(self, name, info, ifcond, features, arg_type, ret_type, gen, success_response, boxed, - allow_oob, allow_preconfig): + allow_oob, allow_preconfig, coroutine): arg_type =3D arg_type or self._schema.the_empty_object_type ret_type =3D ret_type or self._schema.the_empty_object_type obj =3D {'arg-type': self._use_type(arg_type), diff --git a/scripts/qapi/schema.py b/scripts/qapi/schema.py index 78309a00f0..c44d391c3f 100644 --- a/scripts/qapi/schema.py +++ b/scripts/qapi/schema.py @@ -128,7 +128,7 @@ class QAPISchemaVisitor: =20 def visit_command(self, name, info, ifcond, features, arg_type, ret_type, gen, success_response, boxed, - allow_oob, allow_preconfig): + allow_oob, allow_preconfig, coroutine): pass =20 def visit_event(self, name, info, ifcond, features, arg_type, boxed): @@ -713,7 +713,8 @@ class QAPISchemaCommand(QAPISchemaEntity): =20 def __init__(self, name, info, doc, ifcond, features, arg_type, ret_type, - gen, success_response, boxed, allow_oob, allow_preconfig): + gen, success_response, boxed, allow_oob, allow_preconfig, + coroutine): super().__init__(name, info, doc, ifcond, features) assert not arg_type or isinstance(arg_type, str) assert not ret_type or isinstance(ret_type, str) @@ -726,6 +727,7 @@ class QAPISchemaCommand(QAPISchemaEntity): self.boxed =3D boxed self.allow_oob =3D allow_oob self.allow_preconfig =3D allow_preconfig + self.coroutine =3D coroutine =20 def check(self, schema): super().check(schema) @@ -768,7 +770,7 @@ class QAPISchemaCommand(QAPISchemaEntity): visitor.visit_command( self.name, self.info, self.ifcond, self.features, self.arg_type, self.ret_type, self.gen, self.success_response, - self.boxed, self.allow_oob, self.allow_preconfig) + self.boxed, self.allow_oob, self.allow_preconfig, self.corouti= ne) =20 =20 class QAPISchemaEvent(QAPISchemaEntity): @@ -1074,6 +1076,7 @@ class QAPISchema: boxed =3D expr.get('boxed', False) allow_oob =3D expr.get('allow-oob', False) allow_preconfig =3D expr.get('allow-preconfig', False) + coroutine =3D expr.get('coroutine', False) ifcond =3D expr.get('if') features =3D self._make_features(expr.get('features'), info) if isinstance(data, OrderedDict): @@ -1086,7 +1089,8 @@ class QAPISchema: self._def_entity(QAPISchemaCommand(name, info, doc, ifcond, featur= es, data, rets, gen, success_response, - boxed, allow_oob, allow_preconf= ig)) + boxed, allow_oob, allow_preconf= ig, + coroutine)) =20 def _def_event(self, expr, info, doc): name =3D expr['event'] diff --git a/tests/qapi-schema/test-qapi.py b/tests/qapi-schema/test-qapi.py index f396b471eb..e8db9d09d9 100755 --- a/tests/qapi-schema/test-qapi.py +++ b/tests/qapi-schema/test-qapi.py @@ -68,12 +68,13 @@ class QAPISchemaTestVisitor(QAPISchemaVisitor): =20 def visit_command(self, name, info, ifcond, features, arg_type, ret_type, gen, success_response, boxed, - allow_oob, allow_preconfig): + allow_oob, allow_preconfig, coroutine): print('command %s %s -> %s' % (name, arg_type and arg_type.name, ret_type and ret_type.name)) - print(' gen=3D%s success_response=3D%s boxed=3D%s oob=3D%s prec= onfig=3D%s' - % (gen, success_response, boxed, allow_oob, allow_preconfig)) + print(' gen=3D%s success_response=3D%s boxed=3D%s oob=3D%s prec= onfig=3D%s%s' + % (gen, success_response, boxed, allow_oob, allow_preconfig, + " coroutine=3DTrue" if coroutine else "")) self._print_if(ifcond) self._print_features(features) =20 diff --git a/tests/Makefile.include b/tests/Makefile.include index 03a74b60f6..bb69487436 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -290,6 +290,7 @@ qapi-schema +=3D missing-type.json qapi-schema +=3D nested-struct-data.json qapi-schema +=3D nested-struct-data-invalid-dict.json qapi-schema +=3D non-objects.json +qapi-schema +=3D oob-coroutine.json qapi-schema +=3D oob-test.json qapi-schema +=3D allow-preconfig-test.json qapi-schema +=3D pragma-doc-required-crap.json diff --git a/tests/qapi-schema/oob-coroutine.err b/tests/qapi-schema/oob-co= routine.err new file mode 100644 index 0000000000..c01a4992bd --- /dev/null +++ b/tests/qapi-schema/oob-coroutine.err @@ -0,0 +1,2 @@ +oob-coroutine.json: In command 'oob-command-1': +oob-coroutine.json:2: flags 'allow-oob' and 'coroutine' are incompatible diff --git a/tests/qapi-schema/oob-coroutine.json b/tests/qapi-schema/oob-c= oroutine.json new file mode 100644 index 0000000000..0f67663bcd --- /dev/null +++ b/tests/qapi-schema/oob-coroutine.json @@ -0,0 +1,2 @@ +# Check that incompatible flags allow-oob and coroutine are rejected +{ 'command': 'oob-command-1', 'allow-oob': true, 'coroutine': true } diff --git a/tests/qapi-schema/oob-coroutine.out b/tests/qapi-schema/oob-co= routine.out new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qa= pi-schema-test.json index 6b1f05afa7..3e29fe5b40 100644 --- a/tests/qapi-schema/qapi-schema-test.json +++ b/tests/qapi-schema/qapi-schema-test.json @@ -147,6 +147,7 @@ 'returns': 'UserDefTwo' } =20 { 'command': 'cmd-success-response', 'data': {}, 'success-response': false= } +{ 'command': 'coroutine-cmd', 'data': {}, 'coroutine': true } =20 # Returning a non-dictionary requires a name from the whitelist { 'command': 'guest-get-time', 'data': {'a': 'int', '*b': 'int' }, diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qap= i-schema-test.out index 891b4101e0..8868ca0dca 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -203,6 +203,8 @@ command user_def_cmd2 q_obj_user_def_cmd2-arg -> UserDe= fTwo gen=3DTrue success_response=3DTrue boxed=3DFalse oob=3DFalse preconfig= =3DFalse command cmd-success-response None -> None gen=3DTrue success_response=3DFalse boxed=3DFalse oob=3DFalse preconfi= g=3DFalse +command coroutine-cmd None -> None + gen=3DTrue success_response=3DTrue boxed=3DFalse oob=3DFalse preconfig= =3DFalse coroutine=3DTrue object q_obj_guest-get-time-arg member a: int optional=3DFalse member b: int optional=3DTrue --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680479; cv=none; d=zohomail.com; s=zohoarc; b=XF7BsUiseWpW8e4BARaxv+p0F1islNVDltAK/rJwDcZPQOVd5HIbovHu96A/a5accymCg41khedl7LsoYM1NNwgbdEDYNChW5xdzA93S6Q4zDbzzOqbBzbPYSQ4G7xB3mpock/xGMUFYmUeyPDZWy5rv+15R9PYD/FHhwGLnRsE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680479; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mQZIw8paBVeEAPFRJBgfmO8NP+aKctIQijNovxRYWf0=; b=VqK0P4O7yMjDp+gWJPXMf1kQbSnIZBV+kZwwQKPyzW+QMBR8w4Kk3QR/2eJ/54u1uPwZYYxFz/2FRRcMngO9tvuMq8gCfnW8BHpY7UvIpWTJlXz7lZBg74p7HPCJ2Uw2fWbIciZEcJQVfxNG620n+l16GrZQNtdpWsZUNJvXEiM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680479630576.4999839922455; Thu, 28 May 2020 08:41:19 -0700 (PDT) Received: from localhost ([::1]:38344 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKek-0003BC-9j for importer@patchew.org; Thu, 28 May 2020 11:41:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32892) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKbj-0003wC-IO for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:11 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:30904 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbh-0003wJ-4x for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:11 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-381-0N478f10PAOJArRL1qAqug-1; Thu, 28 May 2020 11:38:06 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C04E48730B4; Thu, 28 May 2020 15:38:05 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 54EA11002382; Thu, 28 May 2020 15:38:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680288; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mQZIw8paBVeEAPFRJBgfmO8NP+aKctIQijNovxRYWf0=; b=gWoXmaC+n6ofsuClXkW5PGFBsUBSXxKp52nOuAMUUsSYtwwdHvWHnoh1WfGDDhuzFKy00X Kf/l9n2QxAcsOwVCTlXQMJg3inrPp3Zl5oWFo1Mr9zuUDojjahRtV1t4b584WQrJcMUgAn VpX0kikC2PykHbIZWyKB1zBARu/2+V8= X-MC-Unique: 0N478f10PAOJArRL1qAqug-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 08/12] qmp: Move dispatcher to a coroutine Date: Thu, 28 May 2020 17:37:38 +0200 Message-Id: <20200528153742.274164-9-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=kwolf@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 03:11:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This moves the QMP dispatcher to a coroutine and runs all QMP command handlers that declare 'coroutine': true in coroutine context so they can avoid blocking the main loop while doing I/O or waiting for other events. For commands that are not declared safe to run in a coroutine, the dispatcher drops out of coroutine context by calling the QMP command handler from a bottom half. Signed-off-by: Kevin Wolf Reviewed-by: Markus Armbruster --- include/qapi/qmp/dispatch.h | 1 + monitor/monitor-internal.h | 6 +- monitor/monitor.c | 55 +++++++++++++--- monitor/qmp.c | 122 +++++++++++++++++++++++++++--------- qapi/qmp-dispatch.c | 52 +++++++++++++-- qapi/qmp-registry.c | 3 + util/aio-posix.c | 8 ++- 7 files changed, 201 insertions(+), 46 deletions(-) diff --git a/include/qapi/qmp/dispatch.h b/include/qapi/qmp/dispatch.h index 9fd2b720a7..af8d96c570 100644 --- a/include/qapi/qmp/dispatch.h +++ b/include/qapi/qmp/dispatch.h @@ -31,6 +31,7 @@ typedef enum QmpCommandOptions typedef struct QmpCommand { const char *name; + /* Runs in coroutine context if QCO_COROUTINE is set */ QmpCommandFunc *fn; QmpCommandOptions options; QTAILQ_ENTRY(QmpCommand) node; diff --git a/monitor/monitor-internal.h b/monitor/monitor-internal.h index b39e03b744..b55d6df07f 100644 --- a/monitor/monitor-internal.h +++ b/monitor/monitor-internal.h @@ -155,7 +155,9 @@ static inline bool monitor_is_qmp(const Monitor *mon) =20 typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList; extern IOThread *mon_iothread; -extern QEMUBH *qmp_dispatcher_bh; +extern Coroutine *qmp_dispatcher_co; +extern bool qmp_dispatcher_co_shutdown; +extern bool qmp_dispatcher_co_busy; extern QmpCommandList qmp_commands, qmp_cap_negotiation_commands; extern QemuMutex monitor_lock; extern MonitorList mon_list; @@ -173,7 +175,7 @@ void monitor_fdsets_cleanup(void); =20 void qmp_send_response(MonitorQMP *mon, const QDict *rsp); void monitor_data_destroy_qmp(MonitorQMP *mon); -void monitor_qmp_bh_dispatcher(void *data); +void coroutine_fn monitor_qmp_dispatcher_co(void *data); =20 int get_monitor_def(int64_t *pval, const char *name); void help_cmd(Monitor *mon, const char *name); diff --git a/monitor/monitor.c b/monitor/monitor.c index 35003bb486..50fb5b20d3 100644 --- a/monitor/monitor.c +++ b/monitor/monitor.c @@ -55,8 +55,32 @@ typedef struct { /* Shared monitor I/O thread */ IOThread *mon_iothread; =20 -/* Bottom half to dispatch the requests received from I/O thread */ -QEMUBH *qmp_dispatcher_bh; +/* Coroutine to dispatch the requests received from I/O thread */ +Coroutine *qmp_dispatcher_co; + +/* Set to true when the dispatcher coroutine should terminate */ +bool qmp_dispatcher_co_shutdown; + +/* + * qmp_dispatcher_co_busy is used for synchronisation between the + * monitor thread and the main thread to ensure that the dispatcher + * coroutine never gets scheduled a second time when it's already + * scheduled (scheduling the same coroutine twice is forbidden). + * + * It is true if the coroutine is active and processing requests. + * Additional requests may then be pushed onto a mon->qmp_requests, + * and @qmp_dispatcher_co_shutdown may be set without further ado. + * @qmp_dispatcher_co_busy must not be woken up in this case. + * + * If false, you also have to set @qmp_dispatcher_co_busy to true and + * wake up @qmp_dispatcher_co after pushing the new requests. + * + * The coroutine will automatically change this variable back to false + * before it yields. Nobody else may set the variable to false. + * + * Access must be atomic for thread safety. + */ +bool qmp_dispatcher_co_busy; =20 /* * Protects mon_list, monitor_qapi_event_state, coroutine_mon, @@ -608,9 +632,24 @@ void monitor_cleanup(void) } qemu_mutex_unlock(&monitor_lock); =20 - /* QEMUBHs needs to be deleted before destroying the I/O thread */ - qemu_bh_delete(qmp_dispatcher_bh); - qmp_dispatcher_bh =3D NULL; + /* + * The dispatcher needs to stop before destroying the I/O thread. + * + * We need to poll both qemu_aio_context and iohandler_ctx to make + * sure that the dispatcher coroutine keeps making progress and + * eventually terminates. qemu_aio_context is automatically + * polled by calling AIO_WAIT_WHILE on it, but we must poll + * iohandler_ctx manually. + */ + qmp_dispatcher_co_shutdown =3D true; + if (!atomic_xchg(&qmp_dispatcher_co_busy, true)) { + aio_co_wake(qmp_dispatcher_co); + } + + AIO_WAIT_WHILE(qemu_get_aio_context(), + (aio_poll(iohandler_get_aio_context(), false), + atomic_mb_read(&qmp_dispatcher_co_busy))); + if (mon_iothread) { iothread_destroy(mon_iothread); mon_iothread =3D NULL; @@ -634,9 +673,9 @@ void monitor_init_globals_core(void) * have commands assuming that context. It would be nice to get * rid of those assumptions. */ - qmp_dispatcher_bh =3D aio_bh_new(iohandler_get_aio_context(), - monitor_qmp_bh_dispatcher, - NULL); + qmp_dispatcher_co =3D qemu_coroutine_create(monitor_qmp_dispatcher_co,= NULL); + atomic_mb_set(&qmp_dispatcher_co_busy, true); + aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co); } =20 int monitor_init(MonitorOptions *opts, bool allow_hmp, Error **errp) diff --git a/monitor/qmp.c b/monitor/qmp.c index 922fdb5541..5a14062a5b 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -133,6 +133,10 @@ static void monitor_qmp_respond(MonitorQMP *mon, QDict= *rsp) } } =20 +/* + * Runs outside of coroutine context for OOB commands, but in + * coroutine context for everything else. + */ static void monitor_qmp_dispatch(MonitorQMP *mon, QObject *req) { QDict *rsp; @@ -205,43 +209,99 @@ static QMPRequest *monitor_qmp_requests_pop_any_with_= lock(void) return req_obj; } =20 -void monitor_qmp_bh_dispatcher(void *data) +void coroutine_fn monitor_qmp_dispatcher_co(void *data) { - QMPRequest *req_obj =3D monitor_qmp_requests_pop_any_with_lock(); + QMPRequest *req_obj =3D NULL; QDict *rsp; bool need_resume; MonitorQMP *mon; =20 - if (!req_obj) { - return; - } + while (true) { + assert(atomic_mb_read(&qmp_dispatcher_co_busy) =3D=3D true); =20 - mon =3D req_obj->mon; - /* qmp_oob_enabled() might change after "qmp_capabilities" */ - need_resume =3D !qmp_oob_enabled(mon) || - mon->qmp_requests->length =3D=3D QMP_REQ_QUEUE_LEN_MAX - 1; - qemu_mutex_unlock(&mon->qmp_queue_lock); - if (req_obj->req) { - QDict *qdict =3D qobject_to(QDict, req_obj->req); - QObject *id =3D qdict ? qdict_get(qdict, "id") : NULL; - trace_monitor_qmp_cmd_in_band(qobject_get_try_str(id) ?: ""); - monitor_qmp_dispatch(mon, req_obj->req); - } else { - assert(req_obj->err); - rsp =3D qmp_error_response(req_obj->err); - req_obj->err =3D NULL; - monitor_qmp_respond(mon, rsp); - qobject_unref(rsp); - } + /* + * Mark the dispatcher as not busy already here so that we + * don't miss any new requests coming in the middle of our + * processing. + */ + atomic_mb_set(&qmp_dispatcher_co_busy, false); + + while (!(req_obj =3D monitor_qmp_requests_pop_any_with_lock())) { + /* + * No more requests to process. Wait to be reentered from + * handle_qmp_command() when it pushes more requests, or + * from monitor_cleanup() when it requests shutdown. + */ + if (!qmp_dispatcher_co_shutdown) { + qemu_coroutine_yield(); + + /* + * busy must be set to true again by whoever + * rescheduled us to avoid double scheduling + */ + assert(atomic_xchg(&qmp_dispatcher_co_busy, false) =3D=3D = true); + } + + /* + * qmp_dispatcher_co_shutdown may have changed if we + * yielded and were reentered from monitor_cleanup() + */ + if (qmp_dispatcher_co_shutdown) { + return; + } + } =20 - if (need_resume) { - /* Pairs with the monitor_suspend() in handle_qmp_command() */ - monitor_resume(&mon->common); - } - qmp_request_free(req_obj); + if (atomic_xchg(&qmp_dispatcher_co_busy, true) =3D=3D true) { + /* + * Someone rescheduled us (probably because a new requests + * came in), but we didn't actually yield. Do that now, + * only to be immediately reentered and removed from the + * list of scheduled coroutines. + */ + qemu_coroutine_yield(); + } =20 - /* Reschedule instead of looping so the main loop stays responsive */ - qemu_bh_schedule(qmp_dispatcher_bh); + /* + * Move the coroutine from iohandler_ctx to qemu_aio_context for + * executing the command handler so that it can make progress if it + * involves an AIO_WAIT_WHILE(). + */ + aio_co_schedule(qemu_get_aio_context(), qmp_dispatcher_co); + qemu_coroutine_yield(); + + mon =3D req_obj->mon; + /* qmp_oob_enabled() might change after "qmp_capabilities" */ + need_resume =3D !qmp_oob_enabled(mon) || + mon->qmp_requests->length =3D=3D QMP_REQ_QUEUE_LEN_MAX - 1; + qemu_mutex_unlock(&mon->qmp_queue_lock); + if (req_obj->req) { + QDict *qdict =3D qobject_to(QDict, req_obj->req); + QObject *id =3D qdict ? qdict_get(qdict, "id") : NULL; + trace_monitor_qmp_cmd_in_band(qobject_get_try_str(id) ?: ""); + monitor_qmp_dispatch(mon, req_obj->req); + } else { + assert(req_obj->err); + rsp =3D qmp_error_response(req_obj->err); + req_obj->err =3D NULL; + monitor_qmp_respond(mon, rsp); + qobject_unref(rsp); + } + + if (need_resume) { + /* Pairs with the monitor_suspend() in handle_qmp_command() */ + monitor_resume(&mon->common); + } + qmp_request_free(req_obj); + + /* + * Yield and reschedule so the main loop stays responsive. + * + * Move back to iohandler_ctx so that nested event loops for + * qemu_aio_context don't start new monitor commands. + */ + aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co); + qemu_coroutine_yield(); + } } =20 static void handle_qmp_command(void *opaque, QObject *req, Error *err) @@ -302,7 +362,9 @@ static void handle_qmp_command(void *opaque, QObject *r= eq, Error *err) qemu_mutex_unlock(&mon->qmp_queue_lock); =20 /* Kick the dispatcher routine */ - qemu_bh_schedule(qmp_dispatcher_bh); + if (!atomic_xchg(&qmp_dispatcher_co_busy, true)) { + aio_co_wake(qmp_dispatcher_co); + } } =20 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c index 5677ba92ca..8ae5e59648 100644 --- a/qapi/qmp-dispatch.c +++ b/qapi/qmp-dispatch.c @@ -12,12 +12,16 @@ */ =20 #include "qemu/osdep.h" + +#include "block/aio.h" #include "qapi/error.h" #include "qapi/qmp/dispatch.h" #include "qapi/qmp/qdict.h" #include "qapi/qmp/qjson.h" #include "sysemu/runstate.h" #include "qapi/qmp/qbool.h" +#include "qemu/coroutine.h" +#include "qemu/main-loop.h" =20 static QDict *qmp_dispatch_check_obj(QDict *dict, bool allow_oob, Error **errp) @@ -88,6 +92,30 @@ bool qmp_is_oob(const QDict *dict) && !qdict_haskey(dict, "execute"); } =20 +typedef struct QmpDispatchBH { + const QmpCommand *cmd; + Monitor *cur_mon; + QDict *args; + QObject **ret; + Error **errp; + Coroutine *co; +} QmpDispatchBH; + +static void do_qmp_dispatch_bh(void *opaque) +{ + QmpDispatchBH *data =3D opaque; + + assert(monitor_cur() =3D=3D NULL); + monitor_set_cur(qemu_coroutine_self(), data->cur_mon); + data->cmd->fn(data->args, data->ret, data->errp); + monitor_set_cur(qemu_coroutine_self(), NULL); + aio_co_wake(data->co); +} + +/* + * Runs outside of coroutine context for OOB commands, but in coroutine co= ntext + * for everything else. + */ QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request, bool allow_oob, Monitor *cur_mon) { @@ -153,12 +181,26 @@ QDict *qmp_dispatch(const QmpCommandList *cmds, QObje= ct *request, qobject_ref(args); } =20 + assert(!(oob && qemu_in_coroutine())); assert(monitor_cur() =3D=3D NULL); - monitor_set_cur(qemu_coroutine_self(), cur_mon); - - cmd->fn(args, &ret, &err); - - monitor_set_cur(qemu_coroutine_self(), NULL); + if ((cmd->options & QCO_COROUTINE) || !qemu_in_coroutine()) { + monitor_set_cur(qemu_coroutine_self(), cur_mon); + cmd->fn(args, &ret, &err); + monitor_set_cur(qemu_coroutine_self(), NULL); + } else { + /* Must drop out of coroutine context for this one */ + QmpDispatchBH data =3D { + .cur_mon =3D cur_mon, + .cmd =3D cmd, + .args =3D args, + .ret =3D &ret, + .errp =3D &err, + .co =3D qemu_coroutine_self(), + }; + aio_bh_schedule_oneshot(qemu_get_aio_context(), do_qmp_dispatch_bh, + &data); + qemu_coroutine_yield(); + } qobject_unref(args); if (err) { /* or assert(!ret) after reviewing all handlers: */ diff --git a/qapi/qmp-registry.c b/qapi/qmp-registry.c index d0f9a1d3e3..58c65b5052 100644 --- a/qapi/qmp-registry.c +++ b/qapi/qmp-registry.c @@ -20,6 +20,9 @@ void qmp_register_command(QmpCommandList *cmds, const cha= r *name, { QmpCommand *cmd =3D g_malloc0(sizeof(*cmd)); =20 + /* QCO_COROUTINE and QCO_ALLOW_OOB are incompatible for now */ + assert(!((options & QCO_COROUTINE) && (options & QCO_ALLOW_OOB))); + cmd->name =3D name; cmd->fn =3D fn; cmd->enabled =3D true; diff --git a/util/aio-posix.c b/util/aio-posix.c index 1b2a3af65b..d427908415 100644 --- a/util/aio-posix.c +++ b/util/aio-posix.c @@ -15,6 +15,7 @@ =20 #include "qemu/osdep.h" #include "block/block.h" +#include "qemu/main-loop.h" #include "qemu/rcu.h" #include "qemu/rcu_queue.h" #include "qemu/sockets.h" @@ -563,8 +564,13 @@ bool aio_poll(AioContext *ctx, bool blocking) * There cannot be two concurrent aio_poll calls for the same AioConte= xt (or * an aio_poll concurrent with a GSource prepare/check/dispatch callba= ck). * We rely on this below to avoid slow locked accesses to ctx->notify_= me. + * + * aio_poll() may only be called in the AioContext's thread. iohandler= _ctx + * is special in that it runs in the main thread, but that thread's co= ntext + * is qemu_aio_context. */ - assert(in_aio_context_home_thread(ctx)); + assert(in_aio_context_home_thread(ctx =3D=3D iohandler_get_aio_context= () ? + qemu_get_aio_context() : ctx)); =20 /* aio_notify can avoid the expensive event_notifier_set if * everything (file descriptors, bottom halves, timers) will --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680654; cv=none; d=zohomail.com; s=zohoarc; b=LTyLZR8D5Xx27ePTt5Djex/+VbH/38dzq3VKCpoYAuLS8R24J9TVVEQ3Fi8+aaeWHw9onrbgn+vPEnGzKHyeYgsTULZHTKqej1LiXSHgXAeDDBW43TnP3uCENGINd0a7yWMTADJaXk9x8/jYhxBLuKlO2LhJCZYckqcnX8CY+Jw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680654; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=dWhHjTxv0LMjafdz/vis6speUPBVM7Q+9lyx8nVmOeI=; b=GRRmYmtIu3fIWN6mtRSg0QJJ98g0q3BopynveAuXm7d8ASi0fV9nIBpWnpqrkFBj9OeBlQk2gt1K2oprPQ4tRtlzs3vqhtrHgmT784stksLY1hDEibT6wDc/Fbke5FPjVGGsaCiCWCM0ol9E1eKHMdO8i4YU0hcC4SfazH925hQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680654185398.84176115333446; Thu, 28 May 2020 08:44:14 -0700 (PDT) Received: from localhost ([::1]:47746 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKhT-0001Yw-NS for importer@patchew.org; Thu, 28 May 2020 11:44:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32906) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKbm-00042u-20 for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:14 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:51256 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbl-0003xw-4I for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:13 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-55-hAXqZRYjNI2mnBdH7r56iA-1; Thu, 28 May 2020 11:38:10 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D4DBF800D24; Thu, 28 May 2020 15:38:09 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 180F310098AC; Thu, 28 May 2020 15:38:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680292; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dWhHjTxv0LMjafdz/vis6speUPBVM7Q+9lyx8nVmOeI=; b=eT8WIMlodZKFU1uSJ6Q1e4KrijPKd29henq3TZkxq01/4pREXlLUXxkOUONj7EYh+feJ5Q 7QMLYV+TRbXX/gVp+ot/Kc/ydzUBQecirBM+zZqu0m21HrPopIJo2T9TSItw4Kjknym9BU 1MKq5YRcdjzy3ph/JYaWn9WC2DZt3Ao= X-MC-Unique: hAXqZRYjNI2mnBdH7r56iA-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 09/12] hmp: Add support for coroutine command handlers Date: Thu, 28 May 2020 17:37:39 +0200 Message-Id: <20200528153742.274164-10-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.81; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 02:50:32 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Often, QMP command handlers are not only called to handle QMP commands, but also from a corresponding HMP command handler. In order to give them a consistent environment, optionally run HMP command handlers in a coroutine, too. The implementation is a lot simpler than in QMP because for HMP, we still block the VM while the coroutine is running. Signed-off-by: Kevin Wolf --- monitor/monitor-internal.h | 1 + monitor/hmp.c | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/monitor/monitor-internal.h b/monitor/monitor-internal.h index b55d6df07f..ad2e64be13 100644 --- a/monitor/monitor-internal.h +++ b/monitor/monitor-internal.h @@ -74,6 +74,7 @@ typedef struct HMPCommand { const char *help; const char *flags; /* p=3Dpreconfig */ void (*cmd)(Monitor *mon, const QDict *qdict); + bool coroutine; /* * @sub_table is a list of 2nd level of commands. If it does not exist, * cmd should be used. If it exists, sub_table[?].cmd should be diff --git a/monitor/hmp.c b/monitor/hmp.c index 3e73a4c3ce..ab0e3e279f 100644 --- a/monitor/hmp.c +++ b/monitor/hmp.c @@ -1056,12 +1056,25 @@ fail: return NULL; } =20 +typedef struct HandleHmpCommandCo { + Monitor *mon; + const HMPCommand *cmd; + QDict *qdict; + bool done; +} HandleHmpCommandCo; + +static void handle_hmp_command_co(void *opaque) +{ + HandleHmpCommandCo *data =3D opaque; + data->cmd->cmd(data->mon, data->qdict); + data->done =3D true; +} + void handle_hmp_command(MonitorHMP *mon, const char *cmdline) { QDict *qdict; const HMPCommand *cmd; const char *cmd_start =3D cmdline; - Monitor *old_mon; =20 trace_handle_hmp_command(mon, cmdline); =20 @@ -1080,11 +1093,24 @@ void handle_hmp_command(MonitorHMP *mon, const char= *cmdline) return; } =20 - /* old_mon is non-NULL when called from qmp_human_monitor_command() */ - old_mon =3D monitor_cur(); - monitor_set_cur(qemu_coroutine_self(), &mon->common); - cmd->cmd(&mon->common, qdict); - monitor_set_cur(qemu_coroutine_self(), old_mon); + if (!cmd->coroutine) { + /* old_mon is non-NULL when called from qmp_human_monitor_command(= ) */ + Monitor *old_mon =3D monitor_cur(); + monitor_set_cur(qemu_coroutine_self(), &mon->common); + cmd->cmd(&mon->common, qdict); + monitor_set_cur(qemu_coroutine_self(), old_mon); + } else { + HandleHmpCommandCo data =3D { + .mon =3D &mon->common, + .cmd =3D cmd, + .qdict =3D qdict, + .done =3D false, + }; + Coroutine *co =3D qemu_coroutine_create(handle_hmp_command_co, &da= ta); + monitor_set_cur(co, &mon->common); + aio_co_enter(qemu_get_aio_context(), co); + AIO_WAIT_WHILE(qemu_get_aio_context(), !data.done); + } =20 qobject_unref(qdict); } --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680726; cv=none; d=zohomail.com; s=zohoarc; b=BuXw04acBrCyaGnKqYtqRJK8wscMvDAi+A9sXP/swfmewrf4MzAVZre6+efzKTAN7mJjP7Y1P8JJuWwftE8E99dURaegIhg0sJGYbyvvPG4VDyth35BR4luhm/+aKHfStvlmOXkkjfHRPTKqLyNgKiQxNIxXGnZBir2GXP8o+K4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680726; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Lo5ghy4/6swF4SaorYSBPt6xs1iD/Sty59dCX1STZ9g=; b=hhR43p8R/+mLGhw1J7cJrNmuWZiaZL0mQ7UbwHNTS5Hu8ExdpAJB2miwAaiEOD21rOmnnHfeZMRDeHKiDz2R1ZE27naoeHJjsA13kWRF9RlnRZK8ij0rZfRtmALL/b+yijb7hSrWJAPzgLyPeqkvyLcAMBPRhZ8r6fIFNtWGV0I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680726321575.2739367113176; Thu, 28 May 2020 08:45:26 -0700 (PDT) Received: from localhost ([::1]:51918 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKij-000429-2h for importer@patchew.org; Thu, 28 May 2020 11:45:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32926) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKbs-00046f-AV for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:20 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:40383 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbr-000408-NG for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:20 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-175-mN3beuX3OGOKdJB-3DWwCg-1; Thu, 28 May 2020 11:38:15 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 542B4474; Thu, 28 May 2020 15:38:14 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2ACE010098AC; Thu, 28 May 2020 15:38:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680299; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Lo5ghy4/6swF4SaorYSBPt6xs1iD/Sty59dCX1STZ9g=; b=ixIynWnlM+omA1qWay5QgS5aNOTtG+CF46ELKMbTegj5lfbFpjdQZA0iAplHd2smBrX43m PPl6OpVDhAQUab0N3GwF0gSw7BCh2B8sKm+p7qG8tYRadLew5loXnpDSM7FS2cw721j+Ea 4kar33bmouuzn1o4X6hrBhhCvcdBbTI= X-MC-Unique: mN3beuX3OGOKdJB-3DWwCg-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 10/12] util/async: Add aio_co_reschedule_self() Date: Thu, 28 May 2020 17:37:40 +0200 Message-Id: <20200528153742.274164-11-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=207.211.31.120; envelope-from=kwolf@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 03:11:04 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Add a function that can be used to move the currently running coroutine to a different AioContext (and therefore potentially a different thread). Signed-off-by: Kevin Wolf --- include/block/aio.h | 10 ++++++++++ util/async.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/block/aio.h b/include/block/aio.h index b2f703fa3f..c37617b404 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -17,6 +17,7 @@ #ifdef CONFIG_LINUX_IO_URING #include #endif +#include "qemu/coroutine.h" #include "qemu/queue.h" #include "qemu/event_notifier.h" #include "qemu/thread.h" @@ -654,6 +655,15 @@ static inline bool aio_node_check(AioContext *ctx, boo= l is_external) */ void aio_co_schedule(AioContext *ctx, struct Coroutine *co); =20 +/** + * aio_co_reschedule_self: + * @new_ctx: the new context + * + * Move the currently running coroutine to new_ctx. If the coroutine is al= ready + * running in new_ctx, do nothing. + */ +void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx); + /** * aio_co_wake: * @co: the coroutine diff --git a/util/async.c b/util/async.c index 1319eee3bc..b68e73f488 100644 --- a/util/async.c +++ b/util/async.c @@ -559,6 +559,36 @@ void aio_co_schedule(AioContext *ctx, Coroutine *co) aio_context_unref(ctx); } =20 +typedef struct AioCoRescheduleSelf { + Coroutine *co; + AioContext *new_ctx; +} AioCoRescheduleSelf; + +static void aio_co_reschedule_self_bh(void *opaque) +{ + AioCoRescheduleSelf *data =3D opaque; + aio_co_schedule(data->new_ctx, data->co); +} + +void coroutine_fn aio_co_reschedule_self(AioContext *new_ctx) +{ + AioContext *old_ctx =3D qemu_get_current_aio_context(); + + if (old_ctx !=3D new_ctx) { + AioCoRescheduleSelf data =3D { + .co =3D qemu_coroutine_self(), + .new_ctx =3D new_ctx, + }; + /* + * We can't directly schedule the coroutine in the target context + * because this would be racy: The other thread could try to enter= the + * coroutine before it has yielded in this one. + */ + aio_bh_schedule_oneshot(old_ctx, aio_co_reschedule_self_bh, &data); + qemu_coroutine_yield(); + } +} + void aio_co_wake(struct Coroutine *co) { AioContext *ctx; --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680617; cv=none; d=zohomail.com; s=zohoarc; b=FUOhf9YdnVFa4mQmLRV/yOsnB/0LVrH/++issnVn/31U7obj9K96RWuaK+0cRurUfLkj4ykM/NMXhtpFoMLuOocHowguU0N5SLsdmgnw10kL5+QyPqb8vAM971f3TT7Wts/Nicrij1ApUzpabwazcO+pBBDgw5peOTYqH1pLHHw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680617; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=NqYKidgh5G1o4ZTcC+zTmHKmnxR2i9Cweaej/MX4Lv8=; b=g0MFJdGElaWpe4RWp+6SXdx6VcJXXdJKRG5eVuB9TOGpCmIoK+p9habjGi3T4Skw6/uc+TnHBEKVARudEbIJK2q+ZZvdPiZRi8i3kaFhHfig0DkkHlUlS+cC87iX1+gaYvpIu8WghgNkN1NsgK1KZiWEvqT6n2OwDH7TAGFM9j4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680617644188.92830790672303; Thu, 28 May 2020 08:43:37 -0700 (PDT) Received: from localhost ([::1]:46500 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKgy-0008Ba-AO for importer@patchew.org; Thu, 28 May 2020 11:43:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32930) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKbt-00048m-BE for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:21 -0400 Received: from us-smtp-1.mimecast.com ([205.139.110.61]:46942 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbs-00040Q-Jf for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:21 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-426-BbchL4vgMEmehycdPAQ31A-1; Thu, 28 May 2020 11:38:18 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 539DD1855A01; Thu, 28 May 2020 15:38:17 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9D18710013D0; Thu, 28 May 2020 15:38:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680299; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NqYKidgh5G1o4ZTcC+zTmHKmnxR2i9Cweaej/MX4Lv8=; b=fcNFiFpsnscqBPs488c6LVl1Vaiv4oCTgHV/O0prVL0UNthRGaTPvyMjTJcCzF7LKQVv9p +b0bdBBIRuVCqb9vrShwhC/bVzyKpP1dIVsLZNpPZQgSoZCV2VClrydibq0gbn3sowvplA zOaGj4BZml5Oe02iToDdqqukLVrsdtE= X-MC-Unique: BbchL4vgMEmehycdPAQ31A-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 11/12] block: Add bdrv_co_move_to_aio_context() Date: Thu, 28 May 2020 17:37:41 +0200 Message-Id: <20200528153742.274164-12-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.61; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 06:31:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Add a function to move the current coroutine to the AioContext of a given BlockDriverState. Signed-off-by: Kevin Wolf --- include/block/block.h | 6 ++++++ block.c | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/block/block.h b/include/block/block.h index 25e299605e..fb77062a1e 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -631,6 +631,12 @@ bool bdrv_debug_is_suspended(BlockDriverState *bs, con= st char *tag); */ AioContext *bdrv_get_aio_context(BlockDriverState *bs); =20 +/** + * Move the current coroutine to the AioContext of @bs and return the old + * AioContext of the coroutine. + */ +AioContext *coroutine_fn bdrv_co_move_to_aio_context(BlockDriverState *bs); + /** * Transfer control to @co in the aio context of @bs */ diff --git a/block.c b/block.c index 8416376c9b..cd576b991c 100644 --- a/block.c +++ b/block.c @@ -6215,6 +6215,16 @@ AioContext *bdrv_get_aio_context(BlockDriverState *b= s) return bs ? bs->aio_context : qemu_get_aio_context(); } =20 +AioContext *coroutine_fn bdrv_co_move_to_aio_context(BlockDriverState *bs) +{ + Coroutine *self =3D qemu_coroutine_self(); + AioContext *old_ctx =3D qemu_coroutine_get_aio_context(self); + AioContext *new_ctx =3D bdrv_get_aio_context(bs); + + aio_co_reschedule_self(new_ctx); + return old_ctx; +} + void bdrv_coroutine_enter(BlockDriverState *bs, Coroutine *co) { aio_co_enter(bdrv_get_aio_context(bs), co); --=20 2.25.4 From nobody Thu May 16 09:25:17 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1590680834; cv=none; d=zohomail.com; s=zohoarc; b=Ab3Hxoe+4YUoIjP+5XY62KyeFqMB1a3wXJRZ7SJ+mcAoeGovlOKEtkEyxMT76H9lUfWqT8uoryOPsXDheLO6BS9m57eoyCxkhFFryHsWYpbcyWeI4V3OeQ/J3bOJtrXx6q3gOJPyhr+pEnfUMdM45aBrscs+cV2zpHzsoj2F214= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1590680834; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=azQnLQ1i1iyF2ictkgxkew2ELybC3QNbZ2FtOoUwk78=; b=W5Ci8PNKbnr8eqyQEer60/RZ3dpKjlRjlpdRIR+hHbRPIRJToGS3Rpvoh8LVJ84K1NHz/jITxnBjNcTw3x7Ld/pEVlKbQExniooy1TbNkF/8FdFw4UPBaGqJMTE/zU47cp64NVW4y0O2dSFsa55qK/bqTfSC5fapUSjuWBhwRqg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1590680834049605.0232551220048; Thu, 28 May 2020 08:47:14 -0700 (PDT) Received: from localhost ([::1]:58564 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jeKkS-0007Eo-QY for importer@patchew.org; Thu, 28 May 2020 11:47:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32938) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jeKbw-0004CE-TH for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:25 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:41462 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jeKbw-000416-3l for qemu-devel@nongnu.org; Thu, 28 May 2020 11:38:24 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-276-pB2zTdvZOiu_UG-nw35fkw-1; Thu, 28 May 2020 11:38:21 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F134080572F; Thu, 28 May 2020 15:38:19 +0000 (UTC) Received: from linux.fritz.box.com (ovpn-114-117.ams2.redhat.com [10.36.114.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9D2BC10013D0; Thu, 28 May 2020 15:38:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1590680302; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=azQnLQ1i1iyF2ictkgxkew2ELybC3QNbZ2FtOoUwk78=; b=SHh7nNcwihrohUbjpZyBLcTN6aGu1OJkl3gU4nuSH0yoeR8JcL0YqrItALCwkH6EtsGOyN eDB9LrmzcPr09sWzYdDo6OAHLwPkNn514ya0lW+6/hyCubwebQOqwmZnJegaQjDJnIMkWZ NB6Pkq8IxtfVM0ThlsgJ7m2db74iYAc= X-MC-Unique: pB2zTdvZOiu_UG-nw35fkw-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PATCH v6 12/12] block: Convert 'block_resize' to coroutine Date: Thu, 28 May 2020 17:37:42 +0200 Message-Id: <20200528153742.274164-13-kwolf@redhat.com> In-Reply-To: <20200528153742.274164-1-kwolf@redhat.com> References: <20200528153742.274164-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.120; envelope-from=kwolf@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/28 01:51:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, marcandre.lureau@gmail.com, armbru@redhat.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" block_resize performs some I/O that could potentially take quite some time, so use it as an example for the new 'coroutine': true annotation in the QAPI schema. bdrv_truncate() requires that we're already in the right AioContext for the BlockDriverState if called in coroutine context. So instead of just taking the AioContext lock, move the QMP handler coroutine to the context. Call blk_unref() only after switching back because blk_unref() may only be called in the main thread. Signed-off-by: Kevin Wolf --- qapi/block-core.json | 3 ++- blockdev.c | 13 ++++++------- hmp-commands.hx | 1 + 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index 6fbacddab2..a6003d8a92 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -1291,7 +1291,8 @@ { 'command': 'block_resize', 'data': { '*device': 'str', '*node-name': 'str', - 'size': 'int' } } + 'size': 'int' }, + 'coroutine': true } =20 ## # @NewImageMode: diff --git a/blockdev.c b/blockdev.c index 72df193ca7..9b9287b5d6 100644 --- a/blockdev.c +++ b/blockdev.c @@ -2410,14 +2410,14 @@ BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bit= map_sha256(const char *node, return ret; } =20 -void qmp_block_resize(bool has_device, const char *device, - bool has_node_name, const char *node_name, - int64_t size, Error **errp) +void coroutine_fn qmp_block_resize(bool has_device, const char *device, + bool has_node_name, const char *node_na= me, + int64_t size, Error **errp) { Error *local_err =3D NULL; BlockBackend *blk =3D NULL; BlockDriverState *bs; - AioContext *aio_context; + AioContext *old_ctx; =20 bs =3D bdrv_lookup_bs(has_device ? device : NULL, has_node_name ? node_name : NULL, @@ -2427,8 +2427,7 @@ void qmp_block_resize(bool has_device, const char *de= vice, return; } =20 - aio_context =3D bdrv_get_aio_context(bs); - aio_context_acquire(aio_context); + old_ctx =3D bdrv_co_move_to_aio_context(bs); =20 if (size < 0) { error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"= ); @@ -2450,8 +2449,8 @@ void qmp_block_resize(bool has_device, const char *de= vice, bdrv_drained_end(bs); =20 out: + aio_co_reschedule_self(old_ctx); blk_unref(blk); - aio_context_release(aio_context); } =20 void qmp_block_stream(bool has_job_id, const char *job_id, const char *dev= ice, diff --git a/hmp-commands.hx b/hmp-commands.hx index 7f0f3974ad..4e56fe08ca 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -76,6 +76,7 @@ ERST .params =3D "device size", .help =3D "resize a block image", .cmd =3D hmp_block_resize, + .coroutine =3D true, }, =20 SRST --=20 2.25.4