From nobody Sun May 5 04:47:03 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677531510; cv=none; d=zohomail.com; s=zohoarc; b=iz3biDcYAkDuyKofgrtZ5GmfHRNH6gCBeKWZqg42mE1mj0E2q1gZtzz0XV6ZH2v+6c6ocYDv+7ETY30/Q+AsPBmON4i0hQEkfGl4+vvpt/SLBuhA9Kzz97/K8wwr7ZtFcVx1FNMYhwF50/890HznbOGuSfcr//jVnFTYRR5HxpA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677531510; 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=1h8Miah0yiHHD6DXxqSQC3FvUeZ2FBMPJ/kJ7lRo6HY=; b=TfRs5/DS8Ymc2ONoXVzKlfVhWCJHNLSJvWvK0m4ZpJCdtiXzFfErYrB31wu2GfAiqivDJJXABCyT5dpXPK3WwcH8EEDfvnqYrIoWKF+nieB+5R1Wtx12FG2JGFlDscJq2y12D18bEyglLxg9ejL/ogqOExm0MMNwhlZR4+zgo6s= 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+importer2=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 1677531510715945.5371766420812; Mon, 27 Feb 2023 12:58:30 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pWkYp-0003iV-4z; Mon, 27 Feb 2023 15:57:27 -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 1pWkYo-0003i0-9Y for qemu-devel@nongnu.org; Mon, 27 Feb 2023 15:57:26 -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 1pWkYl-0004lr-VP for qemu-devel@nongnu.org; Mon, 27 Feb 2023 15:57:26 -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-39-PDSZ-CAyPLWK3NL1IkaWrA-1; Mon, 27 Feb 2023 15:57:11 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 581B585CBE2; Mon, 27 Feb 2023 20:57:10 +0000 (UTC) Received: from localhost (unknown [10.39.192.23]) by smtp.corp.redhat.com (Postfix) with ESMTP id C6FDE40C945A; Mon, 27 Feb 2023 20:57:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677531435; 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=1h8Miah0yiHHD6DXxqSQC3FvUeZ2FBMPJ/kJ7lRo6HY=; b=dauU91mVnbYWTS/tKTbGa37fMqNUpRuPxS82dHsZi2PR/uQzOa05S/sYUlFtuMVIi6v6D8 dWx8t+HqH2IhfjiuFzcev/jcVyObwPvP/Y1N8lp8jJiB/411Q7jBh+aBwAvZSAs6kurQF/ ntyheHbZ+adP8/pKujBCEt5PfK+1xLo= X-MC-Unique: PDSZ-CAyPLWK3NL1IkaWrA-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Emanuele Giuseppe Esposito , qemu-block@nongnu.org, Hanna Reitz , Kevin Wolf , Paolo Bonzini , Stefan Hajnoczi Subject: [PATCH 1/3] block: make BlockBackend->quiesce_counter atomic Date: Mon, 27 Feb 2023 15:57:02 -0500 Message-Id: <20230227205704.1910562-2-stefanha@redhat.com> In-Reply-To: <20230227205704.1910562-1-stefanha@redhat.com> References: <20230227205704.1910562-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677531511132100003 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. Use qatomic_set()/qatomic_read() to make it clear that this field is accessed by multiple threads. 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. Signed-off-by: Stefan Hajnoczi --- block/block-backend.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/block/block-backend.c b/block/block-backend.c index 278b04ce69..f00bf2ab35 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,9 @@ 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) { + int new_counter =3D qatomic_read(&blk->quiesce_counter) + 1; + qatomic_set(&blk->quiesce_counter, new_counter); + if (new_counter =3D=3D 1) { if (blk->dev_ops && blk->dev_ops->drained_begin) { blk->dev_ops->drained_begin(blk->dev_opaque); } @@ -2586,7 +2588,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 +2599,14 @@ 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) { + int new_counter =3D qatomic_read(&blk->quiesce_counter) - 1; + qatomic_set(&blk->quiesce_counter, new_counter); + if (new_counter =3D=3D 0) { if (blk->dev_ops && blk->dev_ops->drained_end) { blk->dev_ops->drained_end(blk->dev_opaque); } --=20 2.39.2 From nobody Sun May 5 04:47:03 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677531512; cv=none; d=zohomail.com; s=zohoarc; b=k5yWUmmKg0JNEySzRk0yas0O3u7M3uRjjHXzBJAhlFj8+TK+k7epUyWV8BYiRiVASw8io36euKCkRN4iCkuc1k57gDEvNhCxf63PhRa1dXXT4cfVj1VWCUDxL809SvFtMHpwwwvUyDCN03hYzzyetEctO/ChgwHoDn8xezXJSV0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677531512; 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=NAE9hM9zaTwDQkfb8LozUrNwQHhptwbd7YSOI3y3kdk=; b=ZKUDcEgiNJ4t4DnMvG2WGM9KRdQT0X9PiwdfC/d00CVne1PODRCkQsA0ITXm5hw5qSYQqLv4anwRfjaqqQk5HWjn18I1cZg1ctppSDxZJdknmNB5+UsEVKjAlioGQo+1pOeO5zxDqfrwTVFbkceo3QmGWAQhs9GreVtwWLJgFIg= 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+importer2=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 1677531512465875.6852968392033; Mon, 27 Feb 2023 12:58:32 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pWkYr-0003kt-PF; Mon, 27 Feb 2023 15:57:29 -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 1pWkYp-0003jS-O2 for qemu-devel@nongnu.org; Mon, 27 Feb 2023 15:57:27 -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 1pWkYm-0004ly-Ko for qemu-devel@nongnu.org; Mon, 27 Feb 2023 15:57:27 -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-433-ndtPF59BOR29qc-BDQNpuA-1; Mon, 27 Feb 2023 15:57:14 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B03633813F2B; Mon, 27 Feb 2023 20:57:13 +0000 (UTC) Received: from localhost (unknown [10.39.192.23]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F1A3404BEC5; Mon, 27 Feb 2023 20:57:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677531435; 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=NAE9hM9zaTwDQkfb8LozUrNwQHhptwbd7YSOI3y3kdk=; b=PrMYRoX5WAd/U4HOFVa0DwA+Y9Rm0tqn1Kz5RqmBek4SoIumdG4Nwfq+VXpJtprusvEdop 65UG06vQNBX5k46uOjxxhNodiTl8lI4uB2tG521r+qO7jHOoNszZG1glWy5YQG2jDeOqbV 4iqgvSG3jWQ1uylwChyFmVSm1hNyaZI= X-MC-Unique: ndtPF59BOR29qc-BDQNpuA-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Emanuele Giuseppe Esposito , qemu-block@nongnu.org, Hanna Reitz , Kevin Wolf , Paolo Bonzini , Stefan Hajnoczi Subject: [PATCH 2/3] block: make BlockBackend->disable_request_queuing atomic Date: Mon, 27 Feb 2023 15:57:03 -0500 Message-Id: <20230227205704.1910562-3-stefanha@redhat.com> In-Reply-To: <20230227205704.1910562-1-stefanha@redhat.com> References: <20230227205704.1910562-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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+importer2=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677531513110100013 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 --- 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 f00bf2ab35..ec7eafd7fb 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 Sun May 5 04:47:03 2024 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1677531510; cv=none; d=zohomail.com; s=zohoarc; b=Ch8yqaK39Qtppyj9GaMaSuTTqTEhzCC6YXEmkHV38CEVBV8p71rvM8tRdPIhCLQ1ddy+lpRAFX7QPsXr/vlv6VJd9UIi84Ntkk4kczlxoIpbQ7ZdLUjMiuR2wLjqR3M2AM9ixLiPHAYho1L/MAirmBmGbf+9jZl1Q3L70M50Yzs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677531510; 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=p4GUR5X3j1jU9Wyto2daf0oQc6cHODkpkxYzZnI230k=; b=kfK7S6/AnI+wfmTLvN4aSLky4KlESLhIkTPWr8kNz+jfR7l1lCDDolwhhV8pkF0cH8oeAbXjA1pc27kt4uUIB+uQM9cs6fxxw+s9wyIhXgi50IYtkDaHCznYXEuVLzTGsIzWGoxntd9LddHel8VOb70RfFAidW3qiqgtkwEgdfs= 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+importer2=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 1677531510779576.1955815366433; Mon, 27 Feb 2023 12:58:30 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pWkYq-0003jq-6T; Mon, 27 Feb 2023 15:57:28 -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 1pWkYo-0003i3-Ba for qemu-devel@nongnu.org; Mon, 27 Feb 2023 15:57:26 -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 1pWkYm-0004ml-Jw for qemu-devel@nongnu.org; Mon, 27 Feb 2023 15:57:26 -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-125-6DOd5szGNCumPzHF-LIjUw-1; Mon, 27 Feb 2023 15:57:16 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EE32B3C0D859; Mon, 27 Feb 2023 20:57:15 +0000 (UTC) Received: from localhost (unknown [10.39.192.23]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6ABFDC15BAD; Mon, 27 Feb 2023 20:57:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677531439; 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=p4GUR5X3j1jU9Wyto2daf0oQc6cHODkpkxYzZnI230k=; b=BAWnzHD01AqXbbyt7B2QN4artkxQ6igC6cPaZFbPn6v42gmXcPzh5fRNZDTNw4jUEKWVa/ MWjpOqJki3u5r45ZXc98nWg02QxL7IqwTPMasKQi3HwZWsmErGE8VsGC0aic5XgeintphU pLCxIDmVzpQHbRLDypCzJ4Wj25iZXc4= X-MC-Unique: 6DOd5szGNCumPzHF-LIjUw-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Emanuele Giuseppe Esposito , qemu-block@nongnu.org, Hanna Reitz , Kevin Wolf , Paolo Bonzini , Stefan Hajnoczi Subject: [PATCH 3/3] block: protect BlockBackend->queued_requests with a lock Date: Mon, 27 Feb 2023 15:57:04 -0500 Message-Id: <20230227205704.1910562-4-stefanha@redhat.com> In-Reply-To: <20230227205704.1910562-1-stefanha@redhat.com> References: <20230227205704.1910562-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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+importer2=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=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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1677531511129100002 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 ec7eafd7fb..c4dd16f4da 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 @@ -2611,9 +2622,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