From nobody Sat May 18 00:18:41 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=1678223147; cv=none; d=zohomail.com; s=zohoarc; b=SnWK6x+p66YVgpv4CGnjG5wv3R4NnrmtTLC11yXnYk7oYpXpCPvIYp6Mq8SSfnKusP1i8Ph6P8q8Rx5/X6NCn8zyuIkYmcJjVzvy9RThB0LhpDyHpLkrmWNzDM/22B7dQZeKgFjh6xQoJjCA1Wb+B3xtQSO6ZsBjMg4YShVhKFQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678223147; h=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=nR0EVX41VM86kg5849QDlLjUDoJ/9wiGw9aZDX6pxBk=; b=QeEZ7jXBK2OpwXZY5OLM64QPdkUUplmzRQrogiAg8Q+sloyBTMaunGWk2duT7J59Af/pHaihYslRQd0phjCLqeXhdWlHx3T/MlLwYkHFXCrEo0TZC4+zmmN/jd7kFQOXLhemJXOPIg5ECdHQlug0PzLkxqscIADcGyBH9QqeA7c= 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 1678223147815489.3274414973646; Tue, 7 Mar 2023 13:05:47 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZeUF-00088g-Ax; Tue, 07 Mar 2023 16:04:43 -0500 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 1pZeUC-00087s-27 for qemu-devel@nongnu.org; Tue, 07 Mar 2023 16:04:40 -0500 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 1pZeU8-0002hD-EF for qemu-devel@nongnu.org; Tue, 07 Mar 2023 16:04:39 -0500 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-292-Af-xbui9Obq4I6K90Y2sug-1; Tue, 07 Mar 2023 16:04:33 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CF7E91C0A589; Tue, 7 Mar 2023 21:04:32 +0000 (UTC) Received: from localhost (unknown [10.39.192.86]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4A1BD44002; Tue, 7 Mar 2023 21:04:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678223074; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nR0EVX41VM86kg5849QDlLjUDoJ/9wiGw9aZDX6pxBk=; b=aIp+Fe2JAubq7MkI04sVCB/Ntc6xAPCdOlDORJShJQ+abTT73DgrQmC3llKgPw//rLljUS vH0wTIhVVw6uLf0befq2dsUHLsPNs7X5vpcncKwV8ioeGY+LFXcfqUqduWVeSY0fB4BDew /9wbzaoGRdAbSgsIWYSj44l15RDXuM0= X-MC-Unique: Af-xbui9Obq4I6K90Y2sug-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Hanna Reitz , Kevin Wolf , qemu-block@nongnu.org, Emanuele Giuseppe Esposito , Paolo Bonzini , Stefan Hajnoczi Subject: [PATCH v2 1/3] block: make BlockBackend->quiesce_counter atomic Date: Tue, 7 Mar 2023 16:04:25 -0500 Message-Id: <20230307210427.269214-2-stefanha@redhat.com> In-Reply-To: <20230307210427.269214-1-stefanha@redhat.com> References: <20230307210427.269214-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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=stefanha@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 autolearn=unavailable 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: 1678223149036100003 Content-Type: text/plain; charset="utf-8" The main loop thread increments/decrements BlockBackend->quiesce_counter when drained sections begin/end. The counter is read in the I/O code path. Therefore this field is used to communicate between threads without a lock. Acquire/release are not necessary because the BlockBackend->in_flight counter already uses sequentially consistent accesses and running I/O requests hold that counter when blk_wait_while_drained() is called. qatomic_read() can be used. Use qatomic_fetch_inc()/qatomic_fetch_dec() for modifications even though sequentially consistent atomic accesses are not strictly required here. They are, however, nicer to read than multiple calls to qatomic_read() and qatomic_set(). Since beginning and ending drain is not a hot path the extra cost doesn't matter. Signed-off-by: Stefan Hajnoczi Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- block/block-backend.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/block/block-backend.c b/block/block-backend.c index 278b04ce69..68807be32b 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -80,7 +80,7 @@ struct BlockBackend { NotifierList remove_bs_notifiers, insert_bs_notifiers; QLIST_HEAD(, BlockBackendAioNotifier) aio_notifiers; =20 - int quiesce_counter; + int quiesce_counter; /* atomic: written under BQL, read by other threa= ds */ CoQueue queued_requests; bool disable_request_queuing; =20 @@ -1057,7 +1057,7 @@ void blk_set_dev_ops(BlockBackend *blk, const BlockDe= vOps *ops, blk->dev_opaque =3D opaque; =20 /* Are we currently quiesced? Should we enforce this right now? */ - if (blk->quiesce_counter && ops && ops->drained_begin) { + if (qatomic_read(&blk->quiesce_counter) && ops && ops->drained_begin) { ops->drained_begin(opaque); } } @@ -1271,7 +1271,7 @@ static void coroutine_fn blk_wait_while_drained(Block= Backend *blk) { assert(blk->in_flight > 0); =20 - if (blk->quiesce_counter && !blk->disable_request_queuing) { + if (qatomic_read(&blk->quiesce_counter) && !blk->disable_request_queui= ng) { blk_dec_in_flight(blk); qemu_co_queue_wait(&blk->queued_requests, NULL); blk_inc_in_flight(blk); @@ -2568,7 +2568,7 @@ static void blk_root_drained_begin(BdrvChild *child) BlockBackend *blk =3D child->opaque; ThrottleGroupMember *tgm =3D &blk->public.throttle_group_member; =20 - if (++blk->quiesce_counter =3D=3D 1) { + if (qatomic_fetch_inc(&blk->quiesce_counter) =3D=3D 0) { if (blk->dev_ops && blk->dev_ops->drained_begin) { blk->dev_ops->drained_begin(blk->dev_opaque); } @@ -2586,7 +2586,7 @@ static bool blk_root_drained_poll(BdrvChild *child) { BlockBackend *blk =3D child->opaque; bool busy =3D false; - assert(blk->quiesce_counter); + assert(qatomic_read(&blk->quiesce_counter)); =20 if (blk->dev_ops && blk->dev_ops->drained_poll) { busy =3D blk->dev_ops->drained_poll(blk->dev_opaque); @@ -2597,12 +2597,12 @@ static bool blk_root_drained_poll(BdrvChild *child) static void blk_root_drained_end(BdrvChild *child) { BlockBackend *blk =3D child->opaque; - assert(blk->quiesce_counter); + assert(qatomic_read(&blk->quiesce_counter)); =20 assert(blk->public.throttle_group_member.io_limits_disabled); qatomic_dec(&blk->public.throttle_group_member.io_limits_disabled); =20 - if (--blk->quiesce_counter =3D=3D 0) { + if (qatomic_fetch_dec(&blk->quiesce_counter) =3D=3D 1) { if (blk->dev_ops && blk->dev_ops->drained_end) { blk->dev_ops->drained_end(blk->dev_opaque); } --=20 2.39.2 From nobody Sat May 18 00:18:41 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=1678223152; cv=none; d=zohomail.com; s=zohoarc; b=RXmDTIHYsR0iDerfDA4RqNUpd5CbTCd+nrBSnjiXc2pku3p8PTv+yZdU3vIsnFUC1J/q5j6T6dLkit0JotEEl3RIvlFnQA81dzXoQB3PCEf8N/zaShmL+7ez2ylwUusCz1cFaHSapu0ezsBBWbkXk7Y/2AAJatZ8sWO+7aNt0jk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678223152; h=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=4zdrikkYKWNCa5NfWeDK+tnGc1+1unoquqmdlo8eEg4=; b=mKFKpROPcHlVV1w28lhWy1xVeyxGF21rEYROk+5V+4KBgqa6JWL1cCSMtGvqjsbwVqxxVD1/UYNalJtwQX3I1ZuGg1ve6gYo+R8fnP7AYXb2lpbOaawv9wKlsVCucATl/0nSVtwt9TKQg0Nj0yD8b2nNG5kz+ldVkxVvIZ6egBI= 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 16782231522431003.5282663909046; Tue, 7 Mar 2023 13:05:52 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZeUH-0008AB-Jj; Tue, 07 Mar 2023 16:04:45 -0500 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 1pZeUG-00089C-Jl for qemu-devel@nongnu.org; Tue, 07 Mar 2023 16:04:44 -0500 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 1pZeUF-0002ik-8X for qemu-devel@nongnu.org; Tue, 07 Mar 2023 16:04:44 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-396-Uans5qCvMjSEBNT5Eh9mww-1; Tue, 07 Mar 2023 16:04:35 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9329F185A7A4; Tue, 7 Mar 2023 21:04:35 +0000 (UTC) Received: from localhost (unknown [10.39.192.86]) by smtp.corp.redhat.com (Postfix) with ESMTP id EB7B7140EBF4; Tue, 7 Mar 2023 21:04:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678223082; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4zdrikkYKWNCa5NfWeDK+tnGc1+1unoquqmdlo8eEg4=; b=ZyOlLdpVXHIeqCXOQx6jXQ4w661z1reg1LgtqJ3512hlW7NBnELNH6M2svKqrxW/DRmUUM BOvkXCC2bY4fa83M0ngrHTX/qH2R4maHaAYcLOPeYBbT7YmANv63B7Jj4Iz7g9gK62tlwG PhHU1k7ZpTZ/59k2q2emW3hN20QVD4s= X-MC-Unique: Uans5qCvMjSEBNT5Eh9mww-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Hanna Reitz , Kevin Wolf , qemu-block@nongnu.org, Emanuele Giuseppe Esposito , Paolo Bonzini , Stefan Hajnoczi Subject: [PATCH v2 2/3] block: make BlockBackend->disable_request_queuing atomic Date: Tue, 7 Mar 2023 16:04:26 -0500 Message-Id: <20230307210427.269214-3-stefanha@redhat.com> In-Reply-To: <20230307210427.269214-1-stefanha@redhat.com> References: <20230307210427.269214-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 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=stefanha@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 autolearn=unavailable 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: 1678223152838100006 Content-Type: text/plain; charset="utf-8" This field is accessed by multiple threads without a lock. Use explicit qatomic_read()/qatomic_set() calls. There is no need for acquire/release because blk_set_disable_request_queuing() doesn't provide any guarantees (it helps that it's used at BlockBackend creation time and not when there is I/O in flight). Signed-off-by: Stefan Hajnoczi Reviewed-by: Hanna Czenczek Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- block/block-backend.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/block/block-backend.c b/block/block-backend.c index 68807be32b..0cba4add20 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -82,7 +82,7 @@ struct BlockBackend { =20 int quiesce_counter; /* atomic: written under BQL, read by other threa= ds */ CoQueue queued_requests; - bool disable_request_queuing; + bool disable_request_queuing; /* atomic */ =20 VMChangeStateEntry *vmsh; bool force_allow_inactivate; @@ -1232,7 +1232,7 @@ void blk_set_allow_aio_context_change(BlockBackend *b= lk, bool allow) void blk_set_disable_request_queuing(BlockBackend *blk, bool disable) { IO_CODE(); - blk->disable_request_queuing =3D disable; + qatomic_set(&blk->disable_request_queuing, disable); } =20 static int coroutine_fn GRAPH_RDLOCK @@ -1271,7 +1271,8 @@ static void coroutine_fn blk_wait_while_drained(Block= Backend *blk) { assert(blk->in_flight > 0); =20 - if (qatomic_read(&blk->quiesce_counter) && !blk->disable_request_queui= ng) { + if (qatomic_read(&blk->quiesce_counter) && + !qatomic_read(&blk->disable_request_queuing)) { blk_dec_in_flight(blk); qemu_co_queue_wait(&blk->queued_requests, NULL); blk_inc_in_flight(blk); --=20 2.39.2 From nobody Sat May 18 00:18:41 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=1678223152; cv=none; d=zohomail.com; s=zohoarc; b=ONUj7dZHkaiYh09kNNIiWaGyhT6Er56vdXMA2c/tSecomorIKs+0C0LGG3d1IqtMW0bok/uDaTEt4F/A8CBFotTzL+z/wcAw2OKmVRN3ppAfIi7CVXG7REqDt/vQWmu4/xSkoSwyE9IVFKcE7O7DXZVshtKyRTmNWZXBg6Te7Mg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678223152; h=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=LKMFNjldcTsU810HycL2cuA/XyZV0C4BwRQGkbOqxBw=; b=kRGCiFXpGDhilvWkh4DmZhrEQJuf22y21/gw5XQ7Kjjd+9g/Rp4KkocvN9V4fpgsui8e1EVvXsunGLiqt6MSR2RTcD//5yiF0IL6yg8DbdzCusAanllcerLG8Pu6PdKTBmL7UDj2pXhJlWyJbrlHCmILNbOB94yQcTGea2BpsGc= 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 1678223152282575.1156537889545; Tue, 7 Mar 2023 13:05:52 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZeUH-0008A1-Fe; Tue, 07 Mar 2023 16:04:45 -0500 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 1pZeUF-00088k-Gt for qemu-devel@nongnu.org; Tue, 07 Mar 2023 16:04:43 -0500 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 1pZeUE-0002iW-1B for qemu-devel@nongnu.org; Tue, 07 Mar 2023 16:04:43 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-447-mOFH2HVqOMGfzyMLtFLm4w-1; Tue, 07 Mar 2023 16:04:38 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EA170802D38; Tue, 7 Mar 2023 21:04:37 +0000 (UTC) Received: from localhost (unknown [10.39.192.86]) by smtp.corp.redhat.com (Postfix) with ESMTP id 50F2C2026D4B; Tue, 7 Mar 2023 21:04:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678223081; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LKMFNjldcTsU810HycL2cuA/XyZV0C4BwRQGkbOqxBw=; b=ecAnkXkS9lPDjEVweATHeBhi675nFaEDrzNgn3Glc13KeXjlJgWadCMlxT0IOmAKnLCVlp aK266FZ6iZT2+Z25VraROAAefUYEaovgh8X9dZqcJPJoqDRpknXFvH5I8+HFkpN4WzDCLd 1OA7UXtDmEbXGJCIJKK4D5JtJDJBfkE= X-MC-Unique: mOFH2HVqOMGfzyMLtFLm4w-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Hanna Reitz , Kevin Wolf , qemu-block@nongnu.org, Emanuele Giuseppe Esposito , Paolo Bonzini , Stefan Hajnoczi Subject: [PATCH v2 3/3] block: protect BlockBackend->queued_requests with a lock Date: Tue, 7 Mar 2023 16:04:27 -0500 Message-Id: <20230307210427.269214-4-stefanha@redhat.com> In-Reply-To: <20230307210427.269214-1-stefanha@redhat.com> References: <20230307210427.269214-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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=stefanha@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 autolearn=unavailable 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: 1678223152837100005 Content-Type: text/plain; charset="utf-8" The CoQueue API offers thread-safety via the lock argument that qemu_co_queue_wait() and qemu_co_enter_next() take. BlockBackend currently does not make use of the lock argument. This means that multiple threads submitting I/O requests can corrupt the CoQueue's QSIMPLEQ. Add a QemuMutex and pass it to CoQueue APIs so that the queue is protected. While we're at it, also assert that the queue is empty when the BlockBackend is deleted. Signed-off-by: Stefan Hajnoczi Reviewed-by: Hanna Czenczek --- block/block-backend.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/block/block-backend.c b/block/block-backend.c index 0cba4add20..8c90d19bf1 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -81,6 +81,7 @@ struct BlockBackend { QLIST_HEAD(, BlockBackendAioNotifier) aio_notifiers; =20 int quiesce_counter; /* atomic: written under BQL, read by other threa= ds */ + QemuMutex queued_requests_lock; /* protects queued_requests */ CoQueue queued_requests; bool disable_request_queuing; /* atomic */ =20 @@ -368,6 +369,7 @@ BlockBackend *blk_new(AioContext *ctx, uint64_t perm, u= int64_t shared_perm) =20 block_acct_init(&blk->stats); =20 + qemu_mutex_init(&blk->queued_requests_lock); qemu_co_queue_init(&blk->queued_requests); notifier_list_init(&blk->remove_bs_notifiers); notifier_list_init(&blk->insert_bs_notifiers); @@ -485,6 +487,8 @@ static void blk_delete(BlockBackend *blk) assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers)); assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers)); assert(QLIST_EMPTY(&blk->aio_notifiers)); + assert(qemu_co_queue_empty(&blk->queued_requests)); + qemu_mutex_destroy(&blk->queued_requests_lock); QTAILQ_REMOVE(&block_backends, blk, link); drive_info_del(blk->legacy_dinfo); block_acct_cleanup(&blk->stats); @@ -1273,9 +1277,16 @@ static void coroutine_fn blk_wait_while_drained(Bloc= kBackend *blk) =20 if (qatomic_read(&blk->quiesce_counter) && !qatomic_read(&blk->disable_request_queuing)) { + /* + * Take lock before decrementing in flight counter so main loop th= read + * waits for us to enqueue ourselves before it can leave the drain= ed + * section. + */ + qemu_mutex_lock(&blk->queued_requests_lock); blk_dec_in_flight(blk); - qemu_co_queue_wait(&blk->queued_requests, NULL); + qemu_co_queue_wait(&blk->queued_requests, &blk->queued_requests_lo= ck); blk_inc_in_flight(blk); + qemu_mutex_unlock(&blk->queued_requests_lock); } } =20 @@ -2607,9 +2618,12 @@ static void blk_root_drained_end(BdrvChild *child) if (blk->dev_ops && blk->dev_ops->drained_end) { blk->dev_ops->drained_end(blk->dev_opaque); } - while (qemu_co_enter_next(&blk->queued_requests, NULL)) { + qemu_mutex_lock(&blk->queued_requests_lock); + while (qemu_co_enter_next(&blk->queued_requests, + &blk->queued_requests_lock)) { /* Resume all queued requests */ } + qemu_mutex_unlock(&blk->queued_requests_lock); } } =20 --=20 2.39.2