From nobody Sat May 18 14:01:34 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1684405138; cv=none; d=zohomail.com; s=zohoarc; b=XmG0M9YNOm6Ezds9C0K1JlwhBMzjHbbNy49+jCNt9FTbQpsOTxpi1yEoID1hcpTTCnOAulYLweblhyjmPVvoR74f7nUlJuemnpfJp7WxSS9KKyxs4xv4Ol0QLzsvXYAtI/EC02vRZ5MDuWiUnLvwMAvkT3DAI2VoxK4CJG48wiA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684405138; 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=SJIcYdZiEX3LyHd3jqDR3lyTstBBflPYOIz6+wi9CP8=; b=XxiylmvWQh8obStiQS+ialAz27fvC4c/IqAmL1JJbkZGQVsVSY5RDcsQacnt33iVyprtk+6DJE7hePxc03p1CjX27E34h0QoCnbxZF4OKfRorWYjPaYqOgado94lAE4SSk3qp0IlQmiYbQAOhW8PLN8MJUE3xKKPPmMxOiLoPVM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1684405138714692.4066044554347; Thu, 18 May 2023 03:18:58 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzaiP-0007un-Ca; Thu, 18 May 2023 06:18:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzaiN-0007p4-TA for qemu-devel@nongnu.org; Thu, 18 May 2023 06:18:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzaiM-0004nX-D1 for qemu-devel@nongnu.org; Thu, 18 May 2023 06:18:31 -0400 Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-663-p9UuvD2sNnm55fEMzz35wg-1; Thu, 18 May 2023 06:18:28 -0400 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-94f7a2b21fdso38969966b.2 for ; Thu, 18 May 2023 03:18:28 -0700 (PDT) Received: from [192.168.10.118] ([2001:b07:6468:f312:1c09:f536:3de6:228c]) by smtp.gmail.com with ESMTPSA id j1-20020a170906410100b0096b55be592asm780668ejk.92.2023.05.18.03.18.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 03:18:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684405109; 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=SJIcYdZiEX3LyHd3jqDR3lyTstBBflPYOIz6+wi9CP8=; b=MSSgrKyXE6CYxi5GnjM+5MkxYfKX1iQg12pG2Z+S8ow0ayiRDX7GlsoxR1H5Tx7hU530g7 Onz5V0a/dO+2WeOsDqFRU67bkDwOFKyJz4aBmbiODRc87dyr5IvY1R/6H4/hCzFX1q5Zaj g0bb/f38ZeMUIDujpcF832DWtuQg7ow= X-MC-Unique: p9UuvD2sNnm55fEMzz35wg-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684405107; x=1686997107; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SJIcYdZiEX3LyHd3jqDR3lyTstBBflPYOIz6+wi9CP8=; b=adIqC6/oc9spDEQ7IbPO35mc/cHIsrWlCCP8paz49jpalIxF5h0kPUoCD3LxkG5qVw B38U289VlupKDUIMUYnFGXZHnUzT/mLVsgiDfqr5lkX4FSuVeSNgtEYxaacVP65iIFC9 r6HZLpauouH6fpygHdes50rTradiVOOtdMv7PHfAhpb6XPP0MSdZEcIuflYtNdf9PmlJ cXYWdElX5sLNPmyshfxBmP6sGLglynYmfTzeP+fsE1BWriQljfv/ycNcgJjq9cHFKiFm l20tkUAoyi70TBkMCOxGgbY3veHQ2PitGKZZHXapWsiX+IYy8cSGr+UkNuIgYymIWOW/ sTqQ== X-Gm-Message-State: AC+VfDyb/uZkqsruKVg3px14t5CZs7NrJ4EGJQfZ+Ww8mSf+1arcR87M ICxU+FCAjIYoY2yPUbChitQqpB1NtDamcQrtxKlSnTX+XiWxHoVD7kkIzmnjmMOjsaK9Hwcr7TH Bn6uhem8L2HyM/d93mYci/ul9f6SrKyHJEgkzJxW2cRoaNjPZ2USUZKeY7cLvOY+fQP3uGyLlf4 4= X-Received: by 2002:a17:907:9605:b0:93e:fa12:aa1a with SMTP id gb5-20020a170907960500b0093efa12aa1amr40710359ejc.1.1684405106824; Thu, 18 May 2023 03:18:26 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4Jxn3sDcTRpKZpJlBQzuxTAiqjsx+dO7Ru7KQBH8uLAF8AfuofQnzPF+nFAf8FI4ujxnfHcA== X-Received: by 2002:a17:907:9605:b0:93e:fa12:aa1a with SMTP id gb5-20020a170907960500b0093efa12aa1amr40710341ejc.1.1684405106437; Thu, 18 May 2023 03:18:26 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: armbru@redhat.com, kwolf@redhat.com Subject: [PATCH 1/5] monitor: cleanup detection of qmp_dispatcher_co shutting down Date: Thu, 18 May 2023 12:18:19 +0200 Message-Id: <20230518101823.992158-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230518101823.992158-1-pbonzini@redhat.com> References: <20230518101823.992158-1-pbonzini@redhat.com> MIME-Version: 1.0 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=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1684405140841100003 Content-Type: text/plain; charset="utf-8" Instead of overloading qmp_dispatcher_co_busy, make the coroutine pointer NULL. This will make things break spectacularly if somebody tries to start a request after monitor_cleanup(). AIO_WAIT_WHILE_UNLOCKED() does not need qatomic_mb_read(), because the macro contains all the necessary memory barriers. Signed-off-by: Paolo Bonzini --- monitor/monitor.c | 2 +- monitor/qmp.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/monitor/monitor.c b/monitor/monitor.c index 15f97538ef2b..c4ed2547c25f 100644 --- a/monitor/monitor.c +++ b/monitor/monitor.c @@ -686,7 +686,7 @@ void monitor_cleanup(void) =20 AIO_WAIT_WHILE_UNLOCKED(NULL, (aio_poll(iohandler_get_aio_context(), false), - qatomic_mb_read(&qmp_dispatcher_co_busy))); + qatomic_read(&qmp_dispatcher_co))); =20 /* * We need to explicitly stop the I/O thread (but not destroy it), diff --git a/monitor/qmp.c b/monitor/qmp.c index 092c527b6fc9..f0cc6dc886f8 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -226,6 +226,7 @@ void coroutine_fn monitor_qmp_dispatcher_co(void *data) =20 /* On shutdown, don't take any more requests from the queue */ if (qmp_dispatcher_co_shutdown) { + qatomic_set(&qmp_dispatcher_co, NULL); return; } =20 @@ -250,6 +251,7 @@ void coroutine_fn monitor_qmp_dispatcher_co(void *data) * yielded and were reentered from monitor_cleanup() */ if (qmp_dispatcher_co_shutdown) { + qatomic_set(&qmp_dispatcher_co, NULL); return; } } --=20 2.40.1 From nobody Sat May 18 14:01:34 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1684405131; cv=none; d=zohomail.com; s=zohoarc; b=XdjaGgw/TcFXy9bFbmMpyNmqyxYgrU0wVoMaCg1dFZzn38D4/X33DpvKC5z3LIz3cx1Ov4bRikZn+tocxgM5koCvxC4evhU/KmUITY7XlRdKFGIVZoCKLl+nuu8Z1ekdwq61ClX5f8jD6zUMvQ37NVK5wpbAwq0hS1iwEEtpUzs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684405131; 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=2wwZA+6KLKuWG8tmFCtySGPofQTlJlFNtLmCibsN2TU=; b=U4J/3cYAot5zuAy6d5A++oF8sjc9FTepZbA5QwH8j5r7oYBd9IRxIKgvS6mCd77Y5BnMHHgzF7Vpwa/bKKeB7qOaUgWpqo6fZ5S8WZIKemiq3SrZyNUPK2HT27TTa4fdECqrhCR4uFLlMC0ByQkDbwscSOlHCx2WWShY/QceUss= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1684405131958399.0326806840412; Thu, 18 May 2023 03:18:51 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzaiS-00084l-Jv; Thu, 18 May 2023 06:18:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzaiP-0007xL-Le for qemu-devel@nongnu.org; Thu, 18 May 2023 06:18:33 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzaiO-0004np-1i for qemu-devel@nongnu.org; Thu, 18 May 2023 06:18:33 -0400 Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-615-_IAGP7t8PFWEH6-3YVBr7g-1; Thu, 18 May 2023 06:18:30 -0400 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-9663552473fso262449766b.1 for ; Thu, 18 May 2023 03:18:29 -0700 (PDT) Received: from [192.168.10.118] ([2001:b07:6468:f312:1c09:f536:3de6:228c]) by smtp.gmail.com with ESMTPSA id gv41-20020a1709072be900b0095ef7268ba9sm769439ejc.41.2023.05.18.03.18.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 03:18:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684405111; 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=2wwZA+6KLKuWG8tmFCtySGPofQTlJlFNtLmCibsN2TU=; b=Ps+WQS68AgNAoOSsnc8kHocb0JCvPxAjy81uhiJ4V/FPHsbltL1pY8D9xVIdQxPLeMQ0/9 iPhHivtQUX88LmbGNZp8Sx9d/C06EotUx8OHSYDexNFKiLbb9sP58HCzUHE7E7jwrCIwWi SIec2Q206O0W1tPyYMUHyZmHa40/Yw0= X-MC-Unique: _IAGP7t8PFWEH6-3YVBr7g-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684405108; x=1686997108; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2wwZA+6KLKuWG8tmFCtySGPofQTlJlFNtLmCibsN2TU=; b=VSCplS2NIbwuujfTrWw+zu55Sq9eHkZfCvOnfVoEESkaxMoZ7fQAmQJNIpVe52Gkb/ 2BWSGLtVBxoqe0O9f6cUbjo2IQ+VkpsxxuF3waMitjqCkRbVSHqdidRlAzUj0N4vnv+9 23Bb0J5roTNJMZNLEtVrbvR11lDKOwxqCGLrh2b/UoFHKUskLhbGbsppMZ4h3wLilg47 16Ouw97XYSsYVGiM1gd4hbbKIrpNsxnbG/hb2IGV2vtD0S2vAnlGF1jflJ4kkrMRWtbR PGyOgbXJPB9F/gWZs7U3Vy/Qmrk6vL48/jT/0S9WtSWhTZdPS/SK3DMtXpwt/CuYDRAN oMHg== X-Gm-Message-State: AC+VfDywWfm88bLZa14eSXo6btLk30NJrAi3A0H/uWVk6NNCpGH9YAcC ZWmWngRg+OZBDQ6oGNAnrjqcbX3zTUwmJSgTycgXoqQTPqwjldfC6PCNpPHUCY8omgqfnoOKnNv 1arEv+M21ETjfMYWdaC+psE9CjVJBoFFXYpg7lGcAlXROO3QvTwgW65e1PAKrBo2Sl74dEggY4U 8= X-Received: by 2002:a17:907:a0c:b0:966:2123:e0ca with SMTP id bb12-20020a1709070a0c00b009662123e0camr37263936ejc.34.1684405108404; Thu, 18 May 2023 03:18:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7P5xpBfsP17t1F0Bo2pWcziOJhXimqzTuOznJvb+NQA/O6lsVYWI6ZHFD09qG51PSmc3dLsw== X-Received: by 2002:a17:907:a0c:b0:966:2123:e0ca with SMTP id bb12-20020a1709070a0c00b009662123e0camr37263916ejc.34.1684405108006; Thu, 18 May 2023 03:18:28 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: armbru@redhat.com, kwolf@redhat.com Subject: [PATCH 2/5] monitor: cleanup fetching of QMP requests Date: Thu, 18 May 2023 12:18:20 +0200 Message-Id: <20230518101823.992158-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230518101823.992158-1-pbonzini@redhat.com> References: <20230518101823.992158-1-pbonzini@redhat.com> MIME-Version: 1.0 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=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1684405132972100001 Content-Type: text/plain; charset="utf-8" Use a continue statement so that "after going to sleep" is treated the same way as "after processing a request". Pull the monitor_lock critical section out of monitor_qmp_requests_pop_any_with_lock() and protect qmp_dispatcher_co_shutdown with the monitor_lock. The two changes are complex to separate because monitor_qmp_dispatcher_co() previously had a complicated logic to check for shutdown both before and after going to sleep. Signed-off-by: Paolo Bonzini --- monitor/monitor.c | 9 +++++++-- monitor/qmp.c | 38 ++++++++++++++------------------------ 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/monitor/monitor.c b/monitor/monitor.c index c4ed2547c25f..042a1ab918f9 100644 --- a/monitor/monitor.c +++ b/monitor/monitor.c @@ -56,7 +56,10 @@ IOThread *mon_iothread; /* Coroutine to dispatch the requests received from I/O thread */ Coroutine *qmp_dispatcher_co; =20 -/* Set to true when the dispatcher coroutine should terminate */ +/* + * Set to true when the dispatcher coroutine should terminate. Protected + * by monitor_lock. + */ bool qmp_dispatcher_co_shutdown; =20 /* @@ -679,7 +682,9 @@ void monitor_cleanup(void) * we'll just leave them in the queue without sending a response * and monitor_data_destroy() will free them. */ - qmp_dispatcher_co_shutdown =3D true; + WITH_QEMU_LOCK_GUARD(&monitor_lock) { + qmp_dispatcher_co_shutdown =3D true; + } if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) { aio_co_wake(qmp_dispatcher_co); } diff --git a/monitor/qmp.c b/monitor/qmp.c index f0cc6dc886f8..7516b92a4d3e 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -178,7 +178,10 @@ static QMPRequest *monitor_qmp_requests_pop_any_with_l= ock(void) Monitor *mon; MonitorQMP *qmp_mon; =20 - QEMU_LOCK_GUARD(&monitor_lock); + /* On shutdown, don't take any more requests from the queue */ + if (qmp_dispatcher_co_shutdown) { + return NULL; + } =20 QTAILQ_FOREACH(mon, &mon_list, entry) { if (!monitor_is_qmp(mon)) { @@ -215,6 +218,10 @@ void coroutine_fn monitor_qmp_dispatcher_co(void *data) MonitorQMP *mon; =20 while (true) { + /* + * busy must be set to true again by whoever + * rescheduled us to avoid double scheduling + */ assert(qatomic_mb_read(&qmp_dispatcher_co_busy) =3D=3D true); =20 /* @@ -224,36 +231,18 @@ void coroutine_fn monitor_qmp_dispatcher_co(void *dat= a) */ qatomic_mb_set(&qmp_dispatcher_co_busy, false); =20 - /* On shutdown, don't take any more requests from the queue */ - if (qmp_dispatcher_co_shutdown) { - qatomic_set(&qmp_dispatcher_co, NULL); - return; + WITH_QEMU_LOCK_GUARD(&monitor_lock) { + req_obj =3D monitor_qmp_requests_pop_any_with_lock(); } =20 - while (!(req_obj =3D monitor_qmp_requests_pop_any_with_lock())) { + if (!req_obj) { /* * 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(qatomic_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) { - qatomic_set(&qmp_dispatcher_co, NULL); - return; - } + qemu_coroutine_yield(); + continue; } =20 trace_monitor_qmp_in_band_dequeue(req_obj, @@ -342,6 +331,7 @@ void coroutine_fn monitor_qmp_dispatcher_co(void *data) aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co); qemu_coroutine_yield(); } + qatomic_set(&qmp_dispatcher_co, NULL); } =20 static void handle_qmp_command(void *opaque, QObject *req, Error *err) --=20 2.40.1 From nobody Sat May 18 14:01:34 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1684405169; cv=none; d=zohomail.com; s=zohoarc; b=b8/tnPzSA6iO9C/02xjy4XOJvPrllxkSnUmjYPtFawivpcU8UcDKy/0npa+20ofSoUjb+wGOZPGaUHCd9gIcblzipGZ2XNhaLO7Vn2r39Q28Jp9m+ApKbQ0BM2iVLpPu501MfLg4zgZIe5rSFHLhb+YmH2XflPGHnzgNcFx6RLg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684405169; 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=16tuWLfs0lbwm4hWYeMDGeOyNmcCnS6+7x4zt9UM/tE=; b=fWblTPHze/M6bjImsTlXSEfvk/9T2lLjcRc2QWM6wWp6elI0Ms5o8pFCHyfeVc03yghKHXdJptZeJl6oCQTiZbOYKcnSy0LtzIiXqe1BFVpn8RjSIvWGNCVjI7u6Xqr8N9pdk+5+gUl99sb2H/Ikf0/S7mbT2lq3byNQqHA2gpA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1684405169068590.268926397026; Thu, 18 May 2023 03:19:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzaiT-0008EO-Hn; Thu, 18 May 2023 06:18:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzaiR-00082k-FA for qemu-devel@nongnu.org; Thu, 18 May 2023 06:18:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzaiP-0004o4-NJ for qemu-devel@nongnu.org; Thu, 18 May 2023 06:18:35 -0400 Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-111-rxXRLAxoM662tJqhM7_beA-1; Thu, 18 May 2023 06:18:31 -0400 Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-510ec47c66aso295891a12.0 for ; Thu, 18 May 2023 03:18:31 -0700 (PDT) Received: from [192.168.10.118] ([2001:b07:6468:f312:1c09:f536:3de6:228c]) by smtp.gmail.com with ESMTPSA id qn6-20020a170907210600b0096637a19dccsm766612ejb.210.2023.05.18.03.18.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 03:18:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684405113; 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=16tuWLfs0lbwm4hWYeMDGeOyNmcCnS6+7x4zt9UM/tE=; b=gbGqyqw7xuBaTxdGrWt7cajCu42sLi8VJHqm8zPpA67GmVwcyqwZ8/V/onnN8IRWmTsIam pCf4A2dsZ/LWAJF5DhwjW4NLw/KRMnsJSuXoeC7JLQ/mbRMuqH3fIwe4xSes565hZeR2nk Mk+oEZYjk6WVjUHa7Yc0Qy7yictZlX0= X-MC-Unique: rxXRLAxoM662tJqhM7_beA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684405110; x=1686997110; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=16tuWLfs0lbwm4hWYeMDGeOyNmcCnS6+7x4zt9UM/tE=; b=FAssxtGJPsnfpf004KzSOY1PThn/Ch1S4N9yDiu0eukMEjFLlTzTjLgqJSrLAqaLkd DjZY3/i8517eKaWCg69tiez8PmjkGXtkfOy8CgnQAQ2Q01Og/MZ0jFEOJpXQFrQcphO3 YgRRemr+7W4I2viylF51wTXMkzn9szTDcZnM+fSdex0FPGCvygFmH51tNaZtBWwlvEPK +M0of//P0W7tbIVvkwbxXw5oa3ItAxbnyWR07SeMUDkhI5AoZEmVTMbJ08PyTFQrS0Q/ en7RFSLRVCBO/lNqfmYrr9yFoc2ih3C9r1gJu1+vmZbmDFYa8j10dCtilHHtGXk7CFXI 5tIw== X-Gm-Message-State: AC+VfDwQEN0bbU9nEXiqq8mpeI9lVL3TQqftaLaq8H03fdhh3borlx8N rQk1h4+E2iSPH5soQWQaooOGD7wpBtQFVHN0KxEAT5tGnApXPnYs64lhiWUCM3QB2jfGrXJODkQ 1UKJuy8qdcoA9pDwEZ4o8kAFqUtViOEsypcFQKqikPd4xIo8cdlG9G9NmsgOaltOn1lRyh+MOm5 I= X-Received: by 2002:a17:907:a01:b0:94a:5d5c:fe6f with SMTP id bb1-20020a1709070a0100b0094a5d5cfe6fmr39579611ejc.47.1684405110037; Thu, 18 May 2023 03:18:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7vujvBE0iVradmdFUWKMWwWLBUQcVUSmgvxYe8Kv/2oFursZ0irY2JLjw/kafxlqmOBs9pXw== X-Received: by 2002:a17:907:a01:b0:94a:5d5c:fe6f with SMTP id bb1-20020a1709070a0100b0094a5d5cfe6fmr39579593ejc.47.1684405109715; Thu, 18 May 2023 03:18:29 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: armbru@redhat.com, kwolf@redhat.com Subject: [PATCH 3/5] monitor: introduce qmp_dispatcher_co_wake Date: Thu, 18 May 2023 12:18:21 +0200 Message-Id: <20230518101823.992158-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230518101823.992158-1-pbonzini@redhat.com> References: <20230518101823.992158-1-pbonzini@redhat.com> MIME-Version: 1.0 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=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1684405169611100001 Content-Type: text/plain; charset="utf-8" This makes it possible to turn qmp_dispatcher_co_busy into a static variable. Signed-off-by: Paolo Bonzini --- monitor/monitor-internal.h | 2 +- monitor/monitor.c | 26 +------------------------- monitor/qmp.c | 32 +++++++++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/monitor/monitor-internal.h b/monitor/monitor-internal.h index 61c9b6916db3..252de856812f 100644 --- a/monitor/monitor-internal.h +++ b/monitor/monitor-internal.h @@ -165,7 +165,6 @@ typedef QTAILQ_HEAD(MonitorList, Monitor) MonitorList; extern IOThread *mon_iothread; 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; @@ -183,6 +182,7 @@ void monitor_fdsets_cleanup(void); void qmp_send_response(MonitorQMP *mon, const QDict *rsp); void monitor_data_destroy_qmp(MonitorQMP *mon); void coroutine_fn monitor_qmp_dispatcher_co(void *data); +void qmp_dispatcher_co_wake(void); =20 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name); void handle_hmp_command(MonitorHMP *mon, const char *cmdline); diff --git a/monitor/monitor.c b/monitor/monitor.c index 042a1ab918f9..dc352f9e9d95 100644 --- a/monitor/monitor.c +++ b/monitor/monitor.c @@ -62,27 +62,6 @@ Coroutine *qmp_dispatcher_co; */ bool qmp_dispatcher_co_shutdown; =20 -/* - * 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 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; - /* * Protects mon_list, monitor_qapi_event_state, coroutine_mon, * monitor_destroyed. @@ -685,9 +664,7 @@ void monitor_cleanup(void) WITH_QEMU_LOCK_GUARD(&monitor_lock) { qmp_dispatcher_co_shutdown =3D true; } - if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) { - aio_co_wake(qmp_dispatcher_co); - } + qmp_dispatcher_co_wake(); =20 AIO_WAIT_WHILE_UNLOCKED(NULL, (aio_poll(iohandler_get_aio_context(), false), @@ -742,7 +719,6 @@ void monitor_init_globals(void) * rid of those assumptions. */ qmp_dispatcher_co =3D qemu_coroutine_create(monitor_qmp_dispatcher_co,= NULL); - qatomic_mb_set(&qmp_dispatcher_co_busy, true); aio_co_schedule(iohandler_get_aio_context(), qmp_dispatcher_co); } =20 diff --git a/monitor/qmp.c b/monitor/qmp.c index 7516b92a4d3e..baa1f6652e07 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -33,6 +33,27 @@ #include "qapi/qmp/qlist.h" #include "trace.h" =20 +/* + * 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 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. + */ +static bool qmp_dispatcher_co_busy =3D true; + struct QMPRequest { /* Owner of the request */ MonitorQMP *mon; @@ -334,6 +355,13 @@ void coroutine_fn monitor_qmp_dispatcher_co(void *data) qatomic_set(&qmp_dispatcher_co, NULL); } =20 +void qmp_dispatcher_co_wake(void) +{ + if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) { + aio_co_wake(qmp_dispatcher_co); + } +} + static void handle_qmp_command(void *opaque, QObject *req, Error *err) { MonitorQMP *mon =3D opaque; @@ -395,9 +423,7 @@ static void handle_qmp_command(void *opaque, QObject *r= eq, Error *err) } =20 /* Kick the dispatcher routine */ - if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) { - aio_co_wake(qmp_dispatcher_co); - } + qmp_dispatcher_co_wake(); } =20 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size) --=20 2.40.1 From nobody Sat May 18 14:01:34 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1684405132; cv=none; d=zohomail.com; s=zohoarc; b=RdHTd0HTDSqukGiWqi7v9ZfUz3DeKZSlEiLAO/Kf2Pc5cW7Q26UVkiBCcpc2AmcupcMu0GEat5xp7DaisZfFXlBSFBjVfYES1ATq7r72+QGeVtqW09ZfZguUqarAcDkfdQ33iPeIcQ1KbeJEr5d47hWDa2bSFyBU+gZZ1XO7sQ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684405132; 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=XAlu0C1AW8k1irt69rl1xQFZoyTCaUGINn+k0Ms/AEs=; b=ZlL0DZQyFahW9p9N17S7Ibrw46JbZKOw3iHpFWOJCdWkAZhoGwMR8tZkHstDd9wlrlgUUooyXQNsfvYM4EvN2x96cra1/GZVO0uwPyGCnmf8hMxwaI3yYNFJL0SCS98uBZl/IcItVrOykj/TzF4EoG11vygRZGLcpPvxvDD0sxk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 168440513290718.508037558069418; Thu, 18 May 2023 03:18:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzaiV-0008Qd-D4; Thu, 18 May 2023 06:18:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzaiS-00089Q-Nw for qemu-devel@nongnu.org; Thu, 18 May 2023 06:18:36 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzaiR-0004oH-5G for qemu-devel@nongnu.org; Thu, 18 May 2023 06:18:36 -0400 Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-37-eLTtV8r1NHm7pRC2HHxmgA-1; Thu, 18 May 2023 06:18:33 -0400 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-9662ead7bf8so211032866b.0 for ; Thu, 18 May 2023 03:18:33 -0700 (PDT) Received: from [192.168.10.118] ([2001:b07:6468:f312:1c09:f536:3de6:228c]) by smtp.gmail.com with ESMTPSA id c14-20020aa7c74e000000b0050bc13e5aa9sm411540eds.63.2023.05.18.03.18.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 03:18:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684405114; 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=XAlu0C1AW8k1irt69rl1xQFZoyTCaUGINn+k0Ms/AEs=; b=f55fW4Z3u5kET5/b44GhxJqtQCYlFPi+Q0lRac0AAht58KD61GMnzxM6ebvhwmmCznG7oR uTuOtN6CLWIYaXoJugicJ55vIPAoMD1TpxQzdDMBRfhpqum7aMZxrhcoUcMi+0xMYGfGmx zMNXH4C5wb7rpYHCrRgkhy4IGFRhP1c= X-MC-Unique: eLTtV8r1NHm7pRC2HHxmgA-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684405112; x=1686997112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XAlu0C1AW8k1irt69rl1xQFZoyTCaUGINn+k0Ms/AEs=; b=FQcr3IRKOdHFgKtuP+PE6+8F5tFCSVqj61/jP4xXZPMi1rnJrlokfdpNGi2ExdnSpe FSkHSJS79/ERDpdAP3E2LlJ89E4AXv92YI3jSFNMyy9FxuDK/rONqo4V++A5itjNjkLe eR9Z3fYsJ9K0NMBzqgTHJp881OUw1VZP/SSKWS+PlVJiZDdKgNN4gf/Kmg+6bu3ZToVV 9DrcbH15F9Qs5PnxnDWcxRXt7hABoGuQkneEF4dGFdnaUjhuDo3NRSEyFe0+3+wHVy9O 7+eX6A5qWrZtHBtl8NnD6XPM2an9twbu/bAwN+Fq/0YQiDh+/pFK6uPaUNCmESUJYngq Y0aQ== X-Gm-Message-State: AC+VfDydXDOzsnQdNNOKtHqGecI6wnTGigIL4Qp1xaTLStM7gRwroKKM kSbZchr0v63rle6AejpSMaHK/o36eI7eBW6V8V3Z3jE75dHBRJ8SOF1ADkI9kjnKrz3Wan9OoG1 7w1blekc4yUG+YoGcJtwQMaI1jIojUOVS4sfE/6ToTTOGNemTcAlTIlUBv/VJM5s3qmAyrNbHqH w= X-Received: by 2002:a17:907:7282:b0:96a:4654:9a49 with SMTP id dt2-20020a170907728200b0096a46549a49mr23855246ejc.67.1684405111813; Thu, 18 May 2023 03:18:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6vNyv5j9fWGZYqwapLBhytRqF28856eEvVOpjSY3J5ejPn5+nKki+bWc6lGWxx95L+g9RzOA== X-Received: by 2002:a17:907:7282:b0:96a:4654:9a49 with SMTP id dt2-20020a170907728200b0096a46549a49mr23855223ejc.67.1684405111452; Thu, 18 May 2023 03:18:31 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: armbru@redhat.com, kwolf@redhat.com Subject: [PATCH 4/5] monitor: extract request dequeuing to a new function Date: Thu, 18 May 2023 12:18:22 +0200 Message-Id: <20230518101823.992158-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230518101823.992158-1-pbonzini@redhat.com> References: <20230518101823.992158-1-pbonzini@redhat.com> MIME-Version: 1.0 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=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1684405133426100005 Content-Type: text/plain; charset="utf-8" Signed-off-by: Paolo Bonzini --- monitor/qmp.c | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/monitor/qmp.c b/monitor/qmp.c index baa1f6652e07..9ec28be2ee10 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -199,11 +199,6 @@ static QMPRequest *monitor_qmp_requests_pop_any_with_l= ock(void) Monitor *mon; MonitorQMP *qmp_mon; =20 - /* On shutdown, don't take any more requests from the queue */ - if (qmp_dispatcher_co_shutdown) { - return NULL; - } - QTAILQ_FOREACH(mon, &mon_list, entry) { if (!monitor_is_qmp(mon)) { continue; @@ -231,14 +226,9 @@ static QMPRequest *monitor_qmp_requests_pop_any_with_l= ock(void) return req_obj; } =20 -void coroutine_fn monitor_qmp_dispatcher_co(void *data) +static QMPRequest *monitor_qmp_dispatcher_pop_any(void) { - QMPRequest *req_obj =3D NULL; - QDict *rsp; - bool oob_enabled; - MonitorQMP *mon; - - while (true) { + for (;;) { /* * busy must be set to true again by whoever * rescheduled us to avoid double scheduling @@ -253,19 +243,36 @@ void coroutine_fn monitor_qmp_dispatcher_co(void *dat= a) qatomic_mb_set(&qmp_dispatcher_co_busy, false); =20 WITH_QEMU_LOCK_GUARD(&monitor_lock) { + QMPRequest *req_obj; + + /* On shutdown, don't take any more requests from the queue */ + if (qmp_dispatcher_co_shutdown) { + return NULL; + } + req_obj =3D monitor_qmp_requests_pop_any_with_lock(); + if (req_obj) { + return req_obj; + } } =20 - if (!req_obj) { - /* - * 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. - */ - qemu_coroutine_yield(); - continue; - } + /* + * 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. + */ + qemu_coroutine_yield(); + } +} =20 +void coroutine_fn monitor_qmp_dispatcher_co(void *data) +{ + QMPRequest *req_obj; + QDict *rsp; + bool oob_enabled; + MonitorQMP *mon; + + while ((req_obj =3D monitor_qmp_dispatcher_pop_any()) !=3D NULL) { trace_monitor_qmp_in_band_dequeue(req_obj, req_obj->mon->qmp_requests->leng= th); =20 --=20 2.40.1 From nobody Sat May 18 14:01:34 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1684405177; cv=none; d=zohomail.com; s=zohoarc; b=C8hFQkVHXzx5ZXP46IHqtylkhmku6L3cvrckm/WapgGyFN4pwqq+46DADj6ZxUiTIIJ5j+kEM2HYK/cDhyZ8wXxdft0n3qyVTyHNn7vUbpDXlN+nOwkolmkiH15ES8fnUohGBOMHCgiI6txr8ZOWGvY+6v56TTlVU9mb7X9rJ1Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684405177; 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=3DIkeHi4eqsWbxNFyJ78KEvxq2ca7sFGIK3jJY+FtD8=; b=A7AHmrkvO+NGS+o+CbEEgE6blNpCTpwu8VDV4DYDAp2ftNF9hFZcQZ04l3Ly2hqkX8DvuIlBxCGWrrHijwZuMfeSvhN510lDXgdfsf+jZPULmrgYNcyQ+AaAwwDcJifpvVQrfitx9sFjIHtOwzlh4Pw2Z6sGDAQ7J4dUys5o+X8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1684405177451410.0561421027372; Thu, 18 May 2023 03:19:37 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzaiV-0008RO-PP; Thu, 18 May 2023 06:18:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzaiU-0008Na-IY for qemu-devel@nongnu.org; Thu, 18 May 2023 06:18:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pzaiS-0004oU-V7 for qemu-devel@nongnu.org; Thu, 18 May 2023 06:18:38 -0400 Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-596-oMIigJ-lPqCGv5plBcIrgQ-1; Thu, 18 May 2023 06:18:35 -0400 Received: by mail-ed1-f71.google.com with SMTP id 4fb4d7f45d1cf-506b21104faso371537a12.1 for ; Thu, 18 May 2023 03:18:34 -0700 (PDT) Received: from [192.168.10.118] ([2001:b07:6468:f312:1c09:f536:3de6:228c]) by smtp.gmail.com with ESMTPSA id a12-20020a170906274c00b0095850aef138sm782638ejd.6.2023.05.18.03.18.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 03:18:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684405116; 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=3DIkeHi4eqsWbxNFyJ78KEvxq2ca7sFGIK3jJY+FtD8=; b=GnFQVfX0eEOr5WyF/+aKH5KhjwzyNwj6OKiBqf3H82FVOsqiL9XIVGM0WInR7NxKbIzCQo EFfUt27pUZr5fDWC9zEAoiqCvMTEZRzlBn08qcs8i+fjdMk2cCqo/mYGbtza0v4eXmH2zF 3+ZByZWrjTawcLYTzo3H/8F9u5bZyQU= X-MC-Unique: oMIigJ-lPqCGv5plBcIrgQ-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684405113; x=1686997113; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3DIkeHi4eqsWbxNFyJ78KEvxq2ca7sFGIK3jJY+FtD8=; b=bHnH0l4whGLYi4koi9TI6injT8wjH3g881d69mkB+SV3ca3RWmf2l7ZF48YWB3EQdV tlgh4GWc8kdGqN2WDIaFg86T4SgwyjydJfxWaTjqUKk+xiZx9XtJ38AyMpUgLNeFasjM pT7vJlu0tWFFFFCiraXN2CODPasTKipahoq8pJrHOPyuotlKsuutEpeVh4Tyr20H11O4 EK6mLPipQsIk3so0YUahe5Zmpstiuf0wVbMa1bhgubXDWn45jbq0iRpdbXZQaRiXJRP3 BevUxAQywzeM9LsRmsFgz/YHOeykJWl81ZaqqBqtqqZBM2vBYuP41kBlpBiIft4t3njm hc9Q== X-Gm-Message-State: AC+VfDxKRbwnQvd1eqwWNPA68zr4bojgeef6I1QKkeDscGspJL9MbgHc 5OieGmfp5wzhAKYkvFv/i8ZDAq0kp0nEXbVpmIrIADc7HogyO3mHvP64hegXjFToIdOOIxGIwst NHTw3N2VGAWMgzTfDf/AskOn3hOvoUUqXyOM+Al7dfebgyQYChlADKaaTt6IqS6TqMYkGfFlkVU 4= X-Received: by 2002:a17:907:1c21:b0:96f:2315:da2 with SMTP id nc33-20020a1709071c2100b0096f23150da2mr2328667ejc.18.1684405113441; Thu, 18 May 2023 03:18:33 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ54IZ0tOStDxLkqK5Ss4npKaRE1Bq2+nI9B/g9tPQlqUGsP5uw/NSwhGYvHip4GU7624nJ4dg== X-Received: by 2002:a17:907:1c21:b0:96f:2315:da2 with SMTP id nc33-20020a1709071c2100b0096f23150da2mr2328641ejc.18.1684405112997; Thu, 18 May 2023 03:18:32 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: armbru@redhat.com, kwolf@redhat.com Subject: [PATCH 5/5] monitor: do not use mb_read/mb_set Date: Thu, 18 May 2023 12:18:23 +0200 Message-Id: <20230518101823.992158-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230518101823.992158-1-pbonzini@redhat.com> References: <20230518101823.992158-1-pbonzini@redhat.com> MIME-Version: 1.0 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=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com 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_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1684405179834100007 Content-Type: text/plain; charset="utf-8" Instead of relying on magic memory barriers, document the pattern that is being used. It is the one based on Dekker's algorithm, and in this case it is embodied as follows: enqueue request; sleeping =3D true; smp_mb(); smp_mb(); if (sleeping) kick(); if (!have a request) yield(); qatomic_mb_set() can be kept---but it will be shortly renamed to qatomic_set_mb() to clarify that the write occurs _before_ the barrier, just in the right column of the pseudocode above. Signed-off-by: Paolo Bonzini --- monitor/qmp.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/monitor/qmp.c b/monitor/qmp.c index 9ec28be2ee10..aee8ce4cf006 100644 --- a/monitor/qmp.c +++ b/monitor/qmp.c @@ -39,13 +39,16 @@ * 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 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. + * It is true if the coroutine will process at least one more request + * before going to sleep. Either it has been kicked already, or it + * is active and processing requests. Additional requests may therefore + * be pushed onto mon->qmp_requests, and @qmp_dispatcher_co_shutdown may + * be set without further ado. @qmp_dispatcher_co 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. + * If false, you have to wake up @qmp_dispatcher_co after pushing new + * requests. You also have to set @qmp_dispatcher_co_busy to true + * before waking up the coroutine. * * The coroutine will automatically change this variable back to false * before it yields. Nobody else may set the variable to false. @@ -230,15 +233,18 @@ static QMPRequest *monitor_qmp_dispatcher_pop_any(voi= d) { for (;;) { /* - * busy must be set to true again by whoever - * rescheduled us to avoid double scheduling + * To avoid double scheduling, busy is true on entry to + * monitor_qmp_dispatcher_co(), and must be set again before + * aio_co_wake()-ing it. */ - assert(qatomic_mb_read(&qmp_dispatcher_co_busy) =3D=3D true); + assert(qatomic_read(&qmp_dispatcher_co_busy) =3D=3D true); =20 /* * Mark the dispatcher as not busy already here so that we * don't miss any new requests coming in the middle of our * processing. + * + * Clear qmp_dispatcher_co_busy before reading request. */ qatomic_mb_set(&qmp_dispatcher_co_busy, false); =20 @@ -364,6 +370,9 @@ void coroutine_fn monitor_qmp_dispatcher_co(void *data) =20 void qmp_dispatcher_co_wake(void) { + /* Write request before reading qmp_dispatcher_co_busy. */ + smp_mb__before_rmw(); + if (!qatomic_xchg(&qmp_dispatcher_co_busy, true)) { aio_co_wake(qmp_dispatcher_co); } --=20 2.40.1