From nobody Wed May 1 11:12:38 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1573808522; cv=none; d=zoho.com; s=zohoarc; b=Cxy+MC5YsjJjrAiG7L0kMHw+mlR5W4feEm/J2myiJIOkOntVkihPJQTwfzP8mZN1xpuvR5UPogKm5Irm4FmLZ4kkr2N/0d42tfyiJC3IRpwfrXyVrhzSImUEYmPFyU3ru9e5qwnQf5szt9TCL3+ZoxnR1g3/1xUZfU0LvnLeqhA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573808522; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=jtAk8t1QDBFP2k8mf6q1JHZTbPSHZjH834anMfUHwHc=; b=drY5uPOp4VZu1QIa+RWo7iVvsXzgw0wSw5kANv+f2W4V//WmmRcHjEqTzd7SnFlI7gymeKpo1/evIRfIJZC/aQPR5ObWH1bAP7gtyMr7hpqkRzb4GfsXGR7lu872RB2L4mXUv5T13dVNx1ZKx2/HaCgKaQVz1VAyx+LlzIl65kY= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573808522416796.9503980646338; Fri, 15 Nov 2019 01:02:02 -0800 (PST) Received: from localhost ([::1]:36810 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVXUP-0007VV-9C for importer@patchew.org; Fri, 15 Nov 2019 04:02:01 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46440) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iVXRp-0005Hv-0W for qemu-devel@nongnu.org; Fri, 15 Nov 2019 03:59:22 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iVXRm-0007Hc-HG for qemu-devel@nongnu.org; Fri, 15 Nov 2019 03:59:20 -0500 Received: from proxmox-new.maurer-it.com ([212.186.127.180]:10777) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iVXRm-0007H3-6r for qemu-devel@nongnu.org; Fri, 15 Nov 2019 03:59:18 -0500 Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id B63C145969; Fri, 15 Nov 2019 09:59:15 +0100 (CET) From: Wolfgang Bumiller To: qemu-devel@nongnu.org Subject: [PATCH v3 for-4.2] monitor/qmp: resume monitor when clearing its queue Date: Fri, 15 Nov 2019 09:59:14 +0100 Message-Id: <20191115085914.21287-1-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.186.127.180 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: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Gerd Hoffmann , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" When a monitor's queue is filled up in handle_qmp_command() it gets suspended. It's the dispatcher bh's job currently to resume the monitor, which it does after processing an event from the queue. However, it is possible for a CHR_EVENT_CLOSED event to be processed before before the bh is scheduled, which will clear the queue without resuming the monitor, thereby preventing the dispatcher from reaching the resume() call. Any new connections to the qmp socket will be accept()ed and show the greeting, but will not respond to any messages sent afterwards (as they will not be read from the still-suspended socket). Fix this by resuming the monitor when clearing a queue which was filled up. Signed-off-by: Wolfgang Bumiller Reviewed-by: Markus Armbruster --- Changes since v2: * Rename `monitor_qmp_cleanup_queues_and_resume` to use singular `queue`. * Make the `need_resume` condition to actually be readable the same way the comment above it describes it. * Update comments as suggested by Markus and linewrap at column 70. * and comment ypo fixups Changes from v1 to v2: * Update commit message to include the resulting symptoms. * Moved the resume code from `monitor_qmp_cleanup_req_queue_locked` to `monitor_qmp_cleanup_queues` to avoid an unnecessary resume when destroying the monitor (as the `_locked` version is also used by `monitor_data_destroy()`. * Renamed `monitor_qmp_cleanup_queues` to `monitor_qmp_cleanup_queues_and_resume` to reflect the change and be verbose about it for potential future users of the function. Currently the only user is `monitor_qmp_event()` in the `CHR_EVENT_CLOSED` case, which is exactly the problematic case currentl= y. monitor/qmp.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/monitor/qmp.c b/monitor/qmp.c index 9d9e5d8b27..b67a8e7d1f 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -75,10 +75,35 @@ static void monitor_qmp_cleanup_req_queue_locked(Monito= rQMP *mon) } } =20 -static void monitor_qmp_cleanup_queues(MonitorQMP *mon) +static void monitor_qmp_cleanup_queue_and_resume(MonitorQMP *mon) { qemu_mutex_lock(&mon->qmp_queue_lock); + + /* + * Same condition as in monitor_qmp_bh_dispatcher(), but before + * removing an element from the queue (hence no `- 1`). + * Also, the queue should not be empty either, otherwise the + * monitor hasn't been suspended yet (or was already resumed). + */ + bool need_resume =3D (!qmp_oob_enabled(mon) || + mon->qmp_requests->length =3D=3D QMP_REQ_QUEUE_LEN_MAX) + && !g_queue_is_empty(mon->qmp_requests); + monitor_qmp_cleanup_req_queue_locked(mon); + + if (need_resume) { + /* + * handle_qmp_command() suspended the monitor because the + * request queue filled up, to be resumed when the queue has + * space again. We just emptied it; resume the monitor. + * + * Without this, the monitor would remain suspended forever + * when we get here while the monitor is suspended. An + * unfortunately timed CHR_EVENT_CLOSED can do the trick. + */ + monitor_resume(&mon->common); + } + qemu_mutex_unlock(&mon->qmp_queue_lock); } =20 @@ -263,9 +288,10 @@ static void handle_qmp_command(void *opaque, QObject *= req, Error *err) =20 /* * Suspend the monitor when we can't queue more requests after - * this one. Dequeuing in monitor_qmp_bh_dispatcher() will resume - * it. Note that when OOB is disabled, we queue at most one - * command, for backward compatibility. + * this one. Dequeuing in monitor_qmp_bh_dispatcher() or + * monitor_qmp_cleanup_queue_and_resume() will resume it. + * Note that when OOB is disabled, we queue at most one command, + * for backward compatibility. */ if (!qmp_oob_enabled(mon) || mon->qmp_requests->length =3D=3D QMP_REQ_QUEUE_LEN_MAX - 1) { @@ -332,7 +358,7 @@ static void monitor_qmp_event(void *opaque, int event) * stdio, it's possible that stdout is still open when stdin * is closed. */ - monitor_qmp_cleanup_queues(mon); + monitor_qmp_cleanup_queue_and_resume(mon); json_message_parser_destroy(&mon->parser); json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL); --=20 2.20.1