From patchwork Fri Nov 27 13:35:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Shinkevich X-Patchwork-Id: 11936369 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32B81C2D0E4 for ; Fri, 27 Nov 2020 13:42:07 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 84BC32224B for ; Fri, 27 Nov 2020 13:42:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 84BC32224B Authentication-Results: mail.kernel.org; dmarc=pass (p=none dis=none) header.from=nongnu.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:49396 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kie0j-0002xu-Ht for qemu-devel@archiver.kernel.org; Fri, 27 Nov 2020 08:42:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46218) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kidvp-0006LW-25; Fri, 27 Nov 2020 08:37:02 -0500 Received: from relay.sw.ru ([185.231.240.75]:52608 helo=relay3.sw.ru) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kidvm-0003I8-SN; Fri, 27 Nov 2020 08:37:00 -0500 Received: from [172.16.25.136] (helo=localhost.sw.ru) by relay3.sw.ru with esmtp (Exim 4.94) (envelope-from ) id 1kidvK-00AfjY-N5; Fri, 27 Nov 2020 16:36:30 +0300 To: qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, kwolf@redhat.com, mreitz@redhat.com, mdroth@linux.vnet.ibm.com, thuth@redhat.com, lvivier@redhat.com, armbru@redhat.com, dgilbert@redhat.com, pbonzini@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, andrey.shinkevich@virtuozzo.com Subject: [PATCH v3 2/5] monitor: drain requests queue with 'channel closed' event Date: Fri, 27 Nov 2020 16:35:43 +0300 Message-Id: <1606484146-913540-3-git-send-email-andrey.shinkevich@virtuozzo.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1606484146-913540-1-git-send-email-andrey.shinkevich@virtuozzo.com> References: <1606484146-913540-1-git-send-email-andrey.shinkevich@virtuozzo.com> Received-SPF: pass client-ip=185.231.240.75; envelope-from=andrey.shinkevich@virtuozzo.com; helo=relay3.sw.ru X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no 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: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Reply-to: Andrey Shinkevich X-Patchwork-Original-From: Andrey Shinkevich via From: Andrey Shinkevich When CHR_EVENT_CLOSED comes, the QMP requests queue may still contain unprocessed commands. It can happen with QMP capability OOB enabled. Let the dispatcher complete handling requests rest in the monitor queue. Signed-off-by: Andrey Shinkevich --- monitor/qmp.c | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/monitor/qmp.c b/monitor/qmp.c index 7169366..a86ed35 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -75,36 +75,32 @@ static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP *mon) } } -static void monitor_qmp_cleanup_queue_and_resume(MonitorQMP *mon) +/* + * Let unprocessed QMP commands be handled. + */ +static void monitor_qmp_drain_queue(MonitorQMP *mon) { - qemu_mutex_lock(&mon->qmp_queue_lock); + bool q_is_empty = false; - /* - * Same condition as in monitor_qmp_dispatcher_co(), 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 = (!qmp_oob_enabled(mon) || - mon->qmp_requests->length == QMP_REQ_QUEUE_LEN_MAX) - && !g_queue_is_empty(mon->qmp_requests); + while (!q_is_empty) { + qemu_mutex_lock(&mon->qmp_queue_lock); + q_is_empty = g_queue_is_empty(mon->qmp_requests); + qemu_mutex_unlock(&mon->qmp_queue_lock); - monitor_qmp_cleanup_req_queue_locked(mon); + if (!q_is_empty) { + if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) { + /* Kick the dispatcher coroutine */ + aio_co_wake(qmp_dispatcher_co); + } else { + /* Let the dispatcher do its job for a while */ + g_usleep(40); + } + } + } - 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. - */ + if (qatomic_mb_read(&mon->common.suspend_cnt)) { monitor_resume(&mon->common); } - - qemu_mutex_unlock(&mon->qmp_queue_lock); } void qmp_send_response(MonitorQMP *mon, const QDict *rsp) @@ -418,7 +414,7 @@ static void monitor_qmp_event(void *opaque, QEMUChrEvent event) * stdio, it's possible that stdout is still open when stdin * is closed. */ - monitor_qmp_cleanup_queue_and_resume(mon); + monitor_qmp_drain_queue(mon); json_message_parser_destroy(&mon->parser); json_message_parser_init(&mon->parser, handle_qmp_command, mon, NULL);