From nobody Fri May 17 05:50:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621333271; cv=none; d=zohomail.com; s=zohoarc; b=E0+Y7ncbJQvZRQDFH/rn6e1G2z5jlkKbwDIE1eYlDGoK/rgVa4ejWxZW4woatC8cvviMwRGduZSDqhU13c5erUaSVqPx+UsiwwP2IHhwUO4z+0Rd5TizG3hyYblrkanlHbxEBKHrAXC4zfqPTOABLd21wPIXhMAXxBLzAqDC9Yo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621333271; 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=9lZ83aft9SpgzX0qL/+FL99/aJ1UNdMSfOcWCB4ES+I=; b=A10SftioX3eKCWOdD3KUct2OMuuowqDeoSZL/cWdXLZpS4GxuYfsLUWJlb4NDdii3y8rgbfq71wwLpvaxKB3Zle3JnN3nJzVbLqvbfaLcKTzZu6q6dq1mBWh9l5PK9soIcnsNx5i6uCe0/MeBUluXTUh3pWZMjxOzP/TBIkOOZw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621333271300856.1901741914381; Tue, 18 May 2021 03:21:11 -0700 (PDT) Received: from localhost ([::1]:41902 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liwqc-0006dW-8A for importer@patchew.org; Tue, 18 May 2021 06:21:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43010) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweD-0002mS-W2 for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:22 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:27997) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweB-0007h5-VH for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:21 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-167--cQIQgs_NDu03ZIDUQy-RA-1; Tue, 18 May 2021 06:08:17 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D41E3100747E; Tue, 18 May 2021 10:08:15 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 340BC687C1; Tue, 18 May 2021 10:08:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621332499; 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=9lZ83aft9SpgzX0qL/+FL99/aJ1UNdMSfOcWCB4ES+I=; b=NxdS06Srx5aZFfAwxNRPJJYPP9MSWJaO8sOlznwN8UYkW8h6vJkOzIsLAbEipb2NA3PdAd +T7J7oDm5sZzjcOpHB5R8+hyVUfNukxS6mOHfyuZp+kdu3eipVuc9cl2vcpU8rC5ksnrKv 1uypXRjoFNiBUDkRZsKHQgTP7VTNFzE= X-MC-Unique: -cQIQgs_NDu03ZIDUQy-RA-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 1/7] block-copy: streamline choice of copy_range vs. read/write Date: Tue, 18 May 2021 12:07:51 +0200 Message-Id: <20210518100757.31243-2-eesposit@redhat.com> In-Reply-To: <20210518100757.31243-1-eesposit@redhat.com> References: <20210518100757.31243-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eesposit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=216.205.24.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini Put the logic to determine the copy size in a separate function, so that there is a simple state machine for the possible methods of copying data from one BlockDriverState to the other. While at it, store the common computation of block_copy_max_transfer into a new field of BlockCopyState, and make sure that we always obey max_transfer; that's more efficient even for the COPY_RANGE_READ_WRITE case. Signed-off-by: Emanuele Giuseppe Esposito Signed-off-by: Paolo Bonzini --- block/block-copy.c | 59 ++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index 37c8e8504b..10ce51a244 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -28,6 +28,13 @@ #define BLOCK_COPY_MAX_WORKERS 64 #define BLOCK_COPY_SLICE_TIME 100000000ULL /* ns */ =20 +typedef enum { + COPY_READ_WRITE_CLUSTER, + COPY_READ_WRITE, + COPY_RANGE_SMALL, + COPY_RANGE_FULL +} BlockCopyMethod; + static coroutine_fn int block_copy_task_entry(AioTask *task); =20 typedef struct BlockCopyCallState { @@ -85,8 +92,8 @@ typedef struct BlockCopyState { BdrvDirtyBitmap *copy_bitmap; int64_t in_flight_bytes; int64_t cluster_size; - bool use_copy_range; - int64_t copy_size; + BlockCopyMethod method; + int64_t max_transfer; uint64_t len; QLIST_HEAD(, BlockCopyTask) tasks; /* All tasks from all block-copy ca= lls */ QLIST_HEAD(, BlockCopyCallState) calls; @@ -148,6 +155,23 @@ static bool coroutine_fn block_copy_wait_one(BlockCopy= State *s, int64_t offset, return true; } =20 +static inline int64_t block_copy_chunk_size(BlockCopyState *s) +{ + switch (s->method) { + case COPY_READ_WRITE_CLUSTER: + return s->cluster_size; + case COPY_READ_WRITE: + case COPY_RANGE_SMALL: + return MIN(MAX(s->cluster_size, BLOCK_COPY_MAX_BUFFER), + s->max_transfer); + case COPY_RANGE_FULL: + return MIN(MAX(s->cluster_size, BLOCK_COPY_MAX_COPY_RANGE), + s->max_transfer); + default: + abort(); + } +} + /* * Search for the first dirty area in offset/bytes range and create task at * the beginning of it. @@ -157,8 +181,9 @@ static BlockCopyTask *block_copy_task_create(BlockCopyS= tate *s, int64_t offset, int64_t bytes) { BlockCopyTask *task; - int64_t max_chunk =3D MIN_NON_ZERO(s->copy_size, call_state->max_chunk= ); + int64_t max_chunk =3D block_copy_chunk_size(s); =20 + max_chunk =3D MIN_NON_ZERO(max_chunk, call_state->max_chunk); if (!bdrv_dirty_bitmap_next_dirty_area(s->copy_bitmap, offset, offset + bytes, max_chunk, &offset, &bytes)) @@ -265,28 +290,27 @@ BlockCopyState *block_copy_state_new(BdrvChild *sourc= e, BdrvChild *target, .len =3D bdrv_dirty_bitmap_size(copy_bitmap), .write_flags =3D write_flags, .mem =3D shres_create(BLOCK_COPY_MAX_MEM), + .max_transfer =3D QEMU_ALIGN_DOWN(block_copy_max_transfer(source, = target) + , cluster_size), }; =20 - if (block_copy_max_transfer(source, target) < cluster_size) { + if (s->max_transfer < cluster_size) { /* * copy_range does not respect max_transfer. We don't want to both= er * with requests smaller than block-copy cluster size, so fallback= to * buffered copying (read and write respect max_transfer on their * behalf). */ - s->use_copy_range =3D false; - s->copy_size =3D cluster_size; + s->method =3D COPY_READ_WRITE_CLUSTER; } else if (write_flags & BDRV_REQ_WRITE_COMPRESSED) { /* Compression supports only cluster-size writes and no copy-range= . */ - s->use_copy_range =3D false; - s->copy_size =3D cluster_size; + s->method =3D COPY_READ_WRITE_CLUSTER; } else { /* * We enable copy-range, but keep small copy_size, until first * successful copy_range (look at block_copy_do_copy). */ - s->use_copy_range =3D use_copy_range; - s->copy_size =3D MAX(s->cluster_size, BLOCK_COPY_MAX_BUFFER); + s->method =3D use_copy_range ? COPY_RANGE_SMALL : COPY_READ_WRITE; } =20 ratelimit_init(&s->rate_limit); @@ -369,30 +393,25 @@ static int coroutine_fn block_copy_do_copy(BlockCopyS= tate *s, return ret; } =20 - if (s->use_copy_range) { + if (s->method >=3D COPY_RANGE_SMALL) { ret =3D bdrv_co_copy_range(s->source, offset, s->target, offset, n= bytes, 0, s->write_flags); if (ret < 0) { trace_block_copy_copy_range_fail(s, offset, ret); - s->use_copy_range =3D false; - s->copy_size =3D MAX(s->cluster_size, BLOCK_COPY_MAX_BUFFER); + s->method =3D COPY_READ_WRITE; /* Fallback to read+write with allocated buffer */ } else { - if (s->use_copy_range) { + if (s->method =3D=3D COPY_RANGE_SMALL) { /* * Successful copy-range. Now increase copy_size. copy_ra= nge * does not respect max_transfer (it's a TODO), so we fact= or * that in here. * - * Note: we double-check s->use_copy_range for the case wh= en + * Note: we double-check s->method for the case when * parallel block-copy request unsets it during previous * bdrv_co_copy_range call. */ - s->copy_size =3D - MIN(MAX(s->cluster_size, BLOCK_COPY_MAX_COPY_RANGE= ), - QEMU_ALIGN_DOWN(block_copy_max_transfer(s->sou= rce, - s->tar= get), - s->cluster_size)); + s->method =3D COPY_RANGE_FULL; } goto out; } --=20 2.30.2 From nobody Fri May 17 05:50:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621332902; cv=none; d=zohomail.com; s=zohoarc; b=Pj6mM64wJVkMo4j18KO4qatIe6K0Y5V5dIsMM9My1r+F/8P1HH46pSJUcWAa8M6JJhcQ3nSdsTqJYDtuQlbc4MJA0JMyCtGnM7vFJUf3PEzXfwiP7us4yS0jJIr0sFtuMMbCFga4p69Y74fffDXI5Fag42DO0drAu0fC1XmXEV8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621332902; 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=7DbdcXoXCJGjezvlQj1alpxoSnh3a9pgSnLpwfxFi1c=; b=ZUWrNpkFS0xcxY+7+6Z193r5832QVSDndF5i0naNzlByyRXcLJZFI8UKsPuLm+Cx3+zAPKJo4S7sAFvn4XyM99TQOrPHDo4091enuqjGxKgdk/T0AixPWJXwFXLkg3IU5bQAqYcWTAWzWgCsM0Yxh4CHkrKTb8WHKy5lk8oWHFE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621332902970811.5467065607844; Tue, 18 May 2021 03:15:02 -0700 (PDT) Received: from localhost ([::1]:54900 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liwkf-0004AI-PI for importer@patchew.org; Tue, 18 May 2021 06:15:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43048) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweF-0002tR-Iu for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:29626) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweD-0007hF-BX for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:23 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-288-YCh4mNp0P5Gzi4aRPHuH_g-1; Tue, 18 May 2021 06:08:19 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5C9631936B6C; Tue, 18 May 2021 10:08:18 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 33B05781E2; Tue, 18 May 2021 10:08:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621332500; 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=7DbdcXoXCJGjezvlQj1alpxoSnh3a9pgSnLpwfxFi1c=; b=PWxTD/oHuWOn3S7VuPco1Z/A/W5eLIthhNF9X3B+TpfkR2A0p58T6ShX6VM/OaTLo3CtCg b4IZxJUEEo+O1ADKNRettIp4Zt3h8Kvwvqcp6qAQ+On+sYiHOli0uNBeRq4pZmL8v7Sg07 6s9CQM12YOPAQpW57X4SqGQ069uOrlc= X-MC-Unique: YCh4mNp0P5Gzi4aRPHuH_g-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 2/7] block-copy: improve documentation of BlockCopyTask and BlockCopyState types and functions Date: Tue, 18 May 2021 12:07:52 +0200 Message-Id: <20210518100757.31243-3-eesposit@redhat.com> In-Reply-To: <20210518100757.31243-1-eesposit@redhat.com> References: <20210518100757.31243-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eesposit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" As done in BlockCopyCallState, categorize BlockCopyTask and BlockCopyState in IN, State and OUT fields. This is just to understand which field has to be protected with a lock. BlockCopyTask .zeroes is a special case, because it is only initialized and then read by the coroutine in block_copy_task_entry. Also set block_copy_task_create as coroutine_fn because: 1) it is static and only invoked by coroutine functions 2) next patches will introduce and use a CoMutex lock there Signed-off-by: Emanuele Giuseppe Esposito --- block/block-copy.c | 49 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index 10ce51a244..d2d3839dec 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -67,13 +67,28 @@ typedef struct BlockCopyCallState { typedef struct BlockCopyTask { AioTask task; =20 + /* + * IN parameters. Initialized in block_copy_task_create() + * and never changed. + */ BlockCopyState *s; BlockCopyCallState *call_state; int64_t offset; - int64_t bytes; + int64_t bytes; /* only re-set in task_shrink, before running the task = */ + + /* + * "local" parameter: used only to communicate between + * the caller (block_copy_dirty_clusters) and the aiotask + * coroutine running block_copy_task_entry + */ bool zeroes; - QLIST_ENTRY(BlockCopyTask) list; + + /* State */ CoQueue wait_queue; /* coroutines blocked on this task */ + + /* To reference all call states from BlockCopyState */ + QLIST_ENTRY(BlockCopyTask) list; + } BlockCopyTask; =20 static int64_t task_end(BlockCopyTask *task) @@ -89,15 +104,25 @@ typedef struct BlockCopyState { */ BdrvChild *source; BdrvChild *target; - BdrvDirtyBitmap *copy_bitmap; + + /* State */ int64_t in_flight_bytes; - int64_t cluster_size; BlockCopyMethod method; - int64_t max_transfer; - uint64_t len; QLIST_HEAD(, BlockCopyTask) tasks; /* All tasks from all block-copy ca= lls */ QLIST_HEAD(, BlockCopyCallState) calls; + /* State fields that use a thread-safe API */ + BdrvDirtyBitmap *copy_bitmap; + ProgressMeter *progress; + SharedResource *mem; + RateLimit rate_limit; =20 + /* + * IN parameters. Initialized in block_copy_state_new() + * and never changed. + */ + int64_t cluster_size; + int64_t max_transfer; + uint64_t len; BdrvRequestFlags write_flags; =20 /* @@ -115,12 +140,6 @@ typedef struct BlockCopyState { * block_copy_reset_unallocated() every time it does. */ bool skip_unallocated; - - ProgressMeter *progress; - - SharedResource *mem; - - RateLimit rate_limit; } BlockCopyState; =20 static BlockCopyTask *find_conflicting_task(BlockCopyState *s, @@ -176,9 +195,9 @@ static inline int64_t block_copy_chunk_size(BlockCopySt= ate *s) * Search for the first dirty area in offset/bytes range and create task at * the beginning of it. */ -static BlockCopyTask *block_copy_task_create(BlockCopyState *s, - BlockCopyCallState *call_stat= e, - int64_t offset, int64_t bytes) +static coroutine_fn BlockCopyTask *block_copy_task_create(BlockCopyState *= s, + BlockCopyCallState *call_s= tate, + int64_t offset, int64_t by= tes) { BlockCopyTask *task; int64_t max_chunk =3D block_copy_chunk_size(s); --=20 2.30.2 From nobody Fri May 17 05:50:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621334490; cv=none; d=zohomail.com; s=zohoarc; b=g5NsbfscgyCvR3oiJMC+F2d9napcoLrifjDGzUH7mbap0Ey0j6vQ0Iq4JdLcUzbOnDeo3Jc9CEW0PtK1qMgmNcoYeDbg/QBcoraLVb4OYyldHM+uUNHik8M+3adb2t6Mblvp8D2z7+ayBByGEKB4kP1FmBpxPPXKy89r03ZxuG0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621334490; 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=a1NGgCqnH5sGnNafRw9R0B/64f8svZZ3GiBz6nY3GbI=; b=KjpINou964paa6DkQDNLnyzhjIqvhh3fpfarzOYxpJF3LfGffKkEilPggPxF6/kvWK23lfw3nqYXfFHQUk3OSmO04qm5fsARRA4Sm2poFcORqEQY4UN2zVNANUrBfced5AyHTaN+RdIt2j45wnk1fLSHG/Ljvy3R+iWt/jTZklI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16213344909152.2595646326491305; Tue, 18 May 2021 03:41:30 -0700 (PDT) Received: from localhost ([::1]:41914 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lixAH-00047Y-Lk for importer@patchew.org; Tue, 18 May 2021 06:41:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43094) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweM-0002x8-Hi for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:31 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:52941) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweG-0007ke-5Y for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:30 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-17-nCFMQCijO1Woa3hjXvtQKw-1; Tue, 18 May 2021 06:08:22 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0671180293C; Tue, 18 May 2021 10:08:21 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id B650450FA1; Tue, 18 May 2021 10:08:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621332503; 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=a1NGgCqnH5sGnNafRw9R0B/64f8svZZ3GiBz6nY3GbI=; b=UjvgFqCm8PfBy3/bLuwNmv7kNsOMk+yMW0mbW/4Rf3sa5gbgX9jhMVlo2zvGZpCTVoPw3+ 2iWEaTTWtp6lvtCHy8N3c71xhTNqW+qGNxtDIxsrh2FirXksUJjPcHxEN1L1s06E4xEXXo bxuT7+t1e0dljzPXpdYUUe2qcSaFkGM= X-MC-Unique: nCFMQCijO1Woa3hjXvtQKw-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 3/7] block-copy: move progress_set_remaining in block_copy_task_end Date: Tue, 18 May 2021 12:07:53 +0200 Message-Id: <20210518100757.31243-4-eesposit@redhat.com> In-Reply-To: <20210518100757.31243-1-eesposit@redhat.com> References: <20210518100757.31243-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eesposit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=216.205.24.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Moving this function in task_end ensures to update the progress anyways, even if there is an error. It also helps in next patch, allowing task_end to have only one critical section. Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Vladimir Sementsov-Ogievskiy --- block/block-copy.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index d2d3839dec..2e610b4142 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -264,6 +264,9 @@ static void coroutine_fn block_copy_task_end(BlockCopyT= ask *task, int ret) bdrv_set_dirty_bitmap(task->s->copy_bitmap, task->offset, task->by= tes); } QLIST_REMOVE(task, list); + progress_set_remaining(task->s->progress, + bdrv_get_dirty_count(task->s->copy_bitmap) + + task->s->in_flight_bytes); qemu_co_queue_restart_all(&task->wait_queue); } =20 @@ -645,9 +648,6 @@ block_copy_dirty_clusters(BlockCopyCallState *call_stat= e) } if (s->skip_unallocated && !(ret & BDRV_BLOCK_ALLOCATED)) { block_copy_task_end(task, 0); - progress_set_remaining(s->progress, - bdrv_get_dirty_count(s->copy_bitmap) + - s->in_flight_bytes); trace_block_copy_skip_range(s, task->offset, task->bytes); offset =3D task_end(task); bytes =3D end - offset; --=20 2.30.2 From nobody Fri May 17 05:50:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621334845; cv=none; d=zohomail.com; s=zohoarc; b=PY8Z+F2gWk5LjQi4zVrTcWhs+HBGzFYOy4tmTQ84Rs1Y3XK+9D0/1PXXNyTCvHj+DOWpNCHaDdW6mcMdKwtB2gZdESGl+HsWxmXkSgO4z+ygDG/+ongiQ1DQiSWzLEJpzAwLB8L1gUkH054YFlxJq5G33wfRUnGFMWiVeF1eGeI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621334845; 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=hH0KaJLTuxEGlx+BcDzKz/h562Ra2sccX4igiEtrYOA=; b=ayLW66k8lZsIhvVUpk3bjCcf5k1tktF6zIAWbSdswEDbOikD/hPuBKc8V9S18vn53NMB1pEI/VWufWNKBjvxsiqPJukE2EuitsL6ocbB7sfdp/kPZv4s+DtochyfP4xFQ6hH7yJ8zcnY4OpZqxApBbcZGVKFb0CrWVnCMmF+8D4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621334845323216.45499037256468; Tue, 18 May 2021 03:47:25 -0700 (PDT) Received: from localhost ([::1]:53954 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lixG0-0004ti-1O for importer@patchew.org; Tue, 18 May 2021 06:47:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43164) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweZ-00033S-9Z for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:44 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:33460) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweR-0007ny-4v for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:43 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-600-xxyx_3V9PFWOrFUrkVoeUg-1; Tue, 18 May 2021 06:08:28 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 73D8180006E; Tue, 18 May 2021 10:08:27 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 64F2D50FA1; Tue, 18 May 2021 10:08:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621332512; 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=hH0KaJLTuxEGlx+BcDzKz/h562Ra2sccX4igiEtrYOA=; b=aVgXB7O5H987YB7f9mK9sQ6t1nuovBq3I7ACt62us0SaGSSwKD+1ehcNSgdD8sybpoxENT o84ZjDvRiRPuz5yDUs1VRLUPU0qf5pDKkSWuoX2Q85AOqA18fOwOXIUiys/L56dAR5jqKQ tz7u0VRSSoOAYq1lfvOWbTgjfNxTR5o= X-MC-Unique: xxyx_3V9PFWOrFUrkVoeUg-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 4/7] block-copy: add a CoMutex to the BlockCopyTask list Date: Tue, 18 May 2021 12:07:54 +0200 Message-Id: <20210518100757.31243-5-eesposit@redhat.com> In-Reply-To: <20210518100757.31243-1-eesposit@redhat.com> References: <20210518100757.31243-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eesposit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Because the list of tasks is only modified by coroutine functions, add a CoMutex in order to protect them. Use the same mutex to protect also BlockCopyState in_flight_bytes field to avoid adding additional syncronization primitives. Signed-off-by: Emanuele Giuseppe Esposito --- block/block-copy.c | 55 +++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index 2e610b4142..3a949fab64 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -83,7 +83,7 @@ typedef struct BlockCopyTask { */ bool zeroes; =20 - /* State */ + /* State. Protected by tasks_lock */ CoQueue wait_queue; /* coroutines blocked on this task */ =20 /* To reference all call states from BlockCopyState */ @@ -106,8 +106,9 @@ typedef struct BlockCopyState { BdrvChild *target; =20 /* State */ - int64_t in_flight_bytes; + int64_t in_flight_bytes; /* protected by tasks_lock */ BlockCopyMethod method; + CoMutex tasks_lock; QLIST_HEAD(, BlockCopyTask) tasks; /* All tasks from all block-copy ca= lls */ QLIST_HEAD(, BlockCopyCallState) calls; /* State fields that use a thread-safe API */ @@ -142,8 +143,10 @@ typedef struct BlockCopyState { bool skip_unallocated; } BlockCopyState; =20 -static BlockCopyTask *find_conflicting_task(BlockCopyState *s, - int64_t offset, int64_t bytes) +/* Called with lock held */ +static BlockCopyTask *find_conflicting_task_locked(BlockCopyState *s, + int64_t offset, + int64_t bytes) { BlockCopyTask *t; =20 @@ -163,13 +166,16 @@ static BlockCopyTask *find_conflicting_task(BlockCopy= State *s, static bool coroutine_fn block_copy_wait_one(BlockCopyState *s, int64_t of= fset, int64_t bytes) { - BlockCopyTask *task =3D find_conflicting_task(s, offset, bytes); + BlockCopyTask *task; + + QEMU_LOCK_GUARD(&s->tasks_lock); + task =3D find_conflicting_task_locked(s, offset, bytes); =20 if (!task) { return false; } =20 - qemu_co_queue_wait(&task->wait_queue, NULL); + qemu_co_queue_wait(&task->wait_queue, &s->tasks_lock); =20 return true; } @@ -213,11 +219,7 @@ static coroutine_fn BlockCopyTask *block_copy_task_cre= ate(BlockCopyState *s, assert(QEMU_IS_ALIGNED(offset, s->cluster_size)); bytes =3D QEMU_ALIGN_UP(bytes, s->cluster_size); =20 - /* region is dirty, so no existent tasks possible in it */ - assert(!find_conflicting_task(s, offset, bytes)); - bdrv_reset_dirty_bitmap(s->copy_bitmap, offset, bytes); - s->in_flight_bytes +=3D bytes; =20 task =3D g_new(BlockCopyTask, 1); *task =3D (BlockCopyTask) { @@ -228,7 +230,13 @@ static coroutine_fn BlockCopyTask *block_copy_task_cre= ate(BlockCopyState *s, .bytes =3D bytes, }; qemu_co_queue_init(&task->wait_queue); - QLIST_INSERT_HEAD(&s->tasks, task, list); + + WITH_QEMU_LOCK_GUARD(&s->tasks_lock) { + s->in_flight_bytes +=3D bytes; + /* region is dirty, so no existent tasks possible in it */ + assert(!find_conflicting_task_locked(s, offset, bytes)); + QLIST_INSERT_HEAD(&s->tasks, task, list); + } =20 return task; } @@ -249,25 +257,29 @@ static void coroutine_fn block_copy_task_shrink(Block= CopyTask *task, =20 assert(new_bytes > 0 && new_bytes < task->bytes); =20 - task->s->in_flight_bytes -=3D task->bytes - new_bytes; bdrv_set_dirty_bitmap(task->s->copy_bitmap, task->offset + new_bytes, task->bytes - new_byte= s); =20 - task->bytes =3D new_bytes; - qemu_co_queue_restart_all(&task->wait_queue); + WITH_QEMU_LOCK_GUARD(&task->s->tasks_lock) { + task->s->in_flight_bytes -=3D task->bytes - new_bytes; + task->bytes =3D new_bytes; + qemu_co_queue_restart_all(&task->wait_queue); + } } =20 static void coroutine_fn block_copy_task_end(BlockCopyTask *task, int ret) { - task->s->in_flight_bytes -=3D task->bytes; if (ret < 0) { bdrv_set_dirty_bitmap(task->s->copy_bitmap, task->offset, task->by= tes); } - QLIST_REMOVE(task, list); - progress_set_remaining(task->s->progress, - bdrv_get_dirty_count(task->s->copy_bitmap) + - task->s->in_flight_bytes); - qemu_co_queue_restart_all(&task->wait_queue); + WITH_QEMU_LOCK_GUARD(&task->s->tasks_lock) { + task->s->in_flight_bytes -=3D task->bytes; + QLIST_REMOVE(task, list); + progress_set_remaining(task->s->progress, + bdrv_get_dirty_count(task->s->copy_bitmap) + + task->s->in_flight_bytes); + qemu_co_queue_restart_all(&task->wait_queue); + } } =20 void block_copy_state_free(BlockCopyState *s) @@ -336,6 +348,7 @@ BlockCopyState *block_copy_state_new(BdrvChild *source,= BdrvChild *target, } =20 ratelimit_init(&s->rate_limit); + qemu_co_mutex_init(&s->tasks_lock); QLIST_INIT(&s->tasks); QLIST_INIT(&s->calls); =20 @@ -586,9 +599,11 @@ int64_t block_copy_reset_unallocated(BlockCopyState *s, =20 if (!ret) { bdrv_reset_dirty_bitmap(s->copy_bitmap, offset, bytes); + qemu_co_mutex_lock(&s->tasks_lock); progress_set_remaining(s->progress, bdrv_get_dirty_count(s->copy_bitmap) + s->in_flight_bytes); + qemu_co_mutex_unlock(&s->tasks_lock); } =20 *count =3D bytes; --=20 2.30.2 From nobody Fri May 17 05:50:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621334613; cv=none; d=zohomail.com; s=zohoarc; b=LRm4ib+8hFZhB2u9s2f4Gj5CwHzRXFS2QdQyjB6hS1RZr83Sq6qIIRnsFrr26+nNvxCrDr/ltkM1qdZVP0fj2F52v4gqaYQK+SHSbvxIzaiCi4VXJiM3nwMrjRhNZNZXyOnnRSKsTp0sV89BIVFEd6e3abJvht75X69lulyfO6s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621334613; 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=Pmjkdb0+ERHWohcq8Mm7mZ2buw4kA6iUKxv1ovZ4H2g=; b=l+7Fgg4eCKnO0KMimMlfLjr4uvP2v79QqVt46UY/7t/2wyXVIwRTZUWFqGk10OWU1EYMXo3E43cMOtJzs/++Zp8umcT4PvVMZCueEbsBgDL2bUFsMBzNnxJZyBLDG07jL7XI8mw97yqqewWxSx6kAGieVlYeo0awcYo7aWZAjTo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621334613669480.2336217584856; Tue, 18 May 2021 03:43:33 -0700 (PDT) Received: from localhost ([::1]:46658 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lixCG-0008CU-Jk for importer@patchew.org; Tue, 18 May 2021 06:43:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43150) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweY-00031O-OF for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:42 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:39931) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweR-0007nq-4U for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:42 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-27-7KsAzXWJPF-n56WG2qJj_A-1; Tue, 18 May 2021 06:08:31 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E1D80107ACC7; Tue, 18 May 2021 10:08:29 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id D17E5687D4; Tue, 18 May 2021 10:08:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621332512; 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=Pmjkdb0+ERHWohcq8Mm7mZ2buw4kA6iUKxv1ovZ4H2g=; b=JDlZGzeJPcOm4lWti0VBHo9n88XxEWlBIPkJ8LAITXCJU1YVAPeQYrmTlC9Kviy6YBvGf1 4z8EWrVSKk2MrVPu+AKmXgiHe6+h0ky5+F2A2y0klDu1W9ZvuwoydfntYuq3qbgJhjAz66 7SX29TairvXwKm+6fYWYpfszTLhY1XI= X-MC-Unique: 7KsAzXWJPF-n56WG2qJj_A-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 5/7] block-copy: add QemuMutex lock for BlockCopyCallState list Date: Tue, 18 May 2021 12:07:55 +0200 Message-Id: <20210518100757.31243-6-eesposit@redhat.com> In-Reply-To: <20210518100757.31243-1-eesposit@redhat.com> References: <20210518100757.31243-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eesposit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=216.205.24.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" As for BlockCopyTask, add a lock to protect BlockCopyCallState ret and sleep_state fields. Also move ret, finished and cancelled in the OUT fields of BlockCopyCallState. Here a QemuMutex is used to protect QemuCoSleep field, since it can be concurrently invoked also from outside threads. .finished, .cancelled and reads to .ret and .error_is_read will be protected in the following patch. .sleep state is handled in the series "coroutine: new sleep/wake API" Signed-off-by: Emanuele Giuseppe Esposito --- block/block-copy.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index 3a949fab64..d5ed5932b0 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -55,13 +55,14 @@ typedef struct BlockCopyCallState { QLIST_ENTRY(BlockCopyCallState) list; =20 /* State */ - int ret; bool finished; - QemuCoSleep sleep; - bool cancelled; + QemuCoSleep sleep; /* TODO: protect API with a lock */ =20 /* OUT parameters */ + bool cancelled; + /* Fields protected by calls_lock in BlockCopyState */ bool error_is_read; + int ret; } BlockCopyCallState; =20 typedef struct BlockCopyTask { @@ -110,6 +111,7 @@ typedef struct BlockCopyState { BlockCopyMethod method; CoMutex tasks_lock; QLIST_HEAD(, BlockCopyTask) tasks; /* All tasks from all block-copy ca= lls */ + QemuMutex calls_lock; QLIST_HEAD(, BlockCopyCallState) calls; /* State fields that use a thread-safe API */ BdrvDirtyBitmap *copy_bitmap; @@ -289,6 +291,7 @@ void block_copy_state_free(BlockCopyState *s) } =20 ratelimit_destroy(&s->rate_limit); + qemu_mutex_destroy(&s->calls_lock); bdrv_release_dirty_bitmap(s->copy_bitmap); shres_destroy(s->mem); g_free(s); @@ -349,6 +352,7 @@ BlockCopyState *block_copy_state_new(BdrvChild *source,= BdrvChild *target, =20 ratelimit_init(&s->rate_limit); qemu_co_mutex_init(&s->tasks_lock); + qemu_mutex_init(&s->calls_lock); QLIST_INIT(&s->tasks); QLIST_INIT(&s->calls); =20 @@ -492,11 +496,14 @@ static coroutine_fn int block_copy_task_entry(AioTask= *task) =20 ret =3D block_copy_do_copy(t->s, t->offset, t->bytes, t->zeroes, &error_is_read); - if (ret < 0 && !t->call_state->ret) { - t->call_state->ret =3D ret; - t->call_state->error_is_read =3D error_is_read; - } else { - progress_work_done(t->s->progress, t->bytes); + + WITH_QEMU_LOCK_GUARD(&t->s->calls_lock) { + if (ret < 0 && !t->call_state->ret) { + t->call_state->ret =3D ret; + t->call_state->error_is_read =3D error_is_read; + } else { + progress_work_done(t->s->progress, t->bytes); + } } co_put_to_shres(t->s->mem, t->bytes); block_copy_task_end(t, ret); @@ -740,7 +747,9 @@ static int coroutine_fn block_copy_common(BlockCopyCall= State *call_state) { int ret; =20 + qemu_mutex_lock(&call_state->s->calls_lock); QLIST_INSERT_HEAD(&call_state->s->calls, call_state, list); + qemu_mutex_unlock(&call_state->s->calls_lock); =20 do { ret =3D block_copy_dirty_clusters(call_state); @@ -767,7 +776,9 @@ static int coroutine_fn block_copy_common(BlockCopyCall= State *call_state) call_state->cb(call_state->cb_opaque); } =20 + qemu_mutex_lock(&call_state->s->calls_lock); QLIST_REMOVE(call_state, list); + qemu_mutex_unlock(&call_state->s->calls_lock); =20 return ret; } --=20 2.30.2 From nobody Fri May 17 05:50:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621333153; cv=none; d=zohomail.com; s=zohoarc; b=BhUtwEvRDr89sprOHFMnsTrAm+BtPTP0qflbFkSk+HML1KY159iKHrCKFJ8vPwIPMKBsQDvtyC7nlNKDgbFM2MxKSIZI8pEchhMLRJhyrH8EzL3wjolIWIIlVMOhqFe+G+lmCxBHbhEe6xMREa4545QlrjB3GzppES90e9FPV8M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621333153; 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=joBPXV+SrOTvRgajV8yLHT0KP8iwBIMAdM/nOqwbuT8=; b=SHWiyZhxV5Gm44yUcE/KFY/4UOAFBQFilzU3Oxcyxjx81YcvApICNZFNQwzR/5VOJIfyL5Wth8LyKUvBMIG9OTN9ZtuS9rVoXm3EWPol08d1OArj7Wd0XwhzpYub80e56uQQ48SHRSZHPlvwzwOMyxFfygBx53nVdb6FPlHGaaQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621333153038933.3978357277738; Tue, 18 May 2021 03:19:13 -0700 (PDT) Received: from localhost ([::1]:35852 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1liwoh-0002Qp-SV for importer@patchew.org; Tue, 18 May 2021 06:19:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43214) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liwee-0003EJ-JR for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:48 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:43223) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweW-0007oi-Ks for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:48 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-302-xwXsnFyUNeaZIb3U8ZYrCQ-1; Tue, 18 May 2021 06:08:33 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 94FAF1936B60; Tue, 18 May 2021 10:08:32 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4D93950FA1; Tue, 18 May 2021 10:08:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621332514; 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=joBPXV+SrOTvRgajV8yLHT0KP8iwBIMAdM/nOqwbuT8=; b=BHlfkQvybeupjRNSBUDVV29Tksay4PGffb4OMZ4QvfRuhrPgQ87k01WeLuElPDvv5cLzaE t9zEn9ueJXn0mNgYov1cpuKWyAhSPCoz/8rGfyoJYc2Mg3CUdznQxFJLkmNGKunrhKeGRA NXlXSV4FaRco/f5ut4UyEuZGOWUuMSE= X-MC-Unique: xwXsnFyUNeaZIb3U8ZYrCQ-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 6/7] block-copy: atomic .cancelled and .finished fields in BlockCopyCallState Date: Tue, 18 May 2021 12:07:56 +0200 Message-Id: <20210518100757.31243-7-eesposit@redhat.com> In-Reply-To: <20210518100757.31243-1-eesposit@redhat.com> References: <20210518100757.31243-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eesposit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" By adding acquire/release pairs, we ensure that .ret and .error_is_read fields are written by block_copy_dirty_clusters before .finished is true. Signed-off-by: Emanuele Giuseppe Esposito --- block/block-copy.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index d5ed5932b0..573e96fefb 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -55,11 +55,11 @@ typedef struct BlockCopyCallState { QLIST_ENTRY(BlockCopyCallState) list; =20 /* State */ - bool finished; + bool finished; /* atomic */ QemuCoSleep sleep; /* TODO: protect API with a lock */ =20 /* OUT parameters */ - bool cancelled; + bool cancelled; /* atomic */ /* Fields protected by calls_lock in BlockCopyState */ bool error_is_read; int ret; @@ -646,7 +646,8 @@ block_copy_dirty_clusters(BlockCopyCallState *call_stat= e) assert(QEMU_IS_ALIGNED(offset, s->cluster_size)); assert(QEMU_IS_ALIGNED(bytes, s->cluster_size)); =20 - while (bytes && aio_task_pool_status(aio) =3D=3D 0 && !call_state->can= celled) { + while (bytes && aio_task_pool_status(aio) =3D=3D 0 && + !qatomic_read(&call_state->cancelled)) { BlockCopyTask *task; int64_t status_bytes; =20 @@ -754,7 +755,7 @@ static int coroutine_fn block_copy_common(BlockCopyCall= State *call_state) do { ret =3D block_copy_dirty_clusters(call_state); =20 - if (ret =3D=3D 0 && !call_state->cancelled) { + if (ret =3D=3D 0 && !qatomic_read(&call_state->cancelled)) { ret =3D block_copy_wait_one(call_state->s, call_state->offset, call_state->bytes); } @@ -768,9 +769,9 @@ static int coroutine_fn block_copy_common(BlockCopyCall= State *call_state) * 2. We have waited for some intersecting block-copy request * It may have failed and produced new dirty bits. */ - } while (ret > 0 && !call_state->cancelled); + } while (ret > 0 && !qatomic_read(&call_state->cancelled)); =20 - call_state->finished =3D true; + qatomic_store_release(&call_state->finished, true); =20 if (call_state->cb) { call_state->cb(call_state->cb_opaque); @@ -833,35 +834,37 @@ void block_copy_call_free(BlockCopyCallState *call_st= ate) return; } =20 - assert(call_state->finished); + assert(qatomic_load_acquire(&call_state->finished)); g_free(call_state); } =20 bool block_copy_call_finished(BlockCopyCallState *call_state) { - return call_state->finished; + return qatomic_load_acquire(&call_state->finished); } =20 bool block_copy_call_succeeded(BlockCopyCallState *call_state) { - return call_state->finished && !call_state->cancelled && - call_state->ret =3D=3D 0; + return qatomic_load_acquire(&call_state->finished) && + !qatomic_read(&call_state->cancelled) && + call_state->ret =3D=3D 0; } =20 bool block_copy_call_failed(BlockCopyCallState *call_state) { - return call_state->finished && !call_state->cancelled && - call_state->ret < 0; + return qatomic_load_acquire(&call_state->finished) && + !qatomic_read(&call_state->cancelled) && + call_state->ret < 0; } =20 bool block_copy_call_cancelled(BlockCopyCallState *call_state) { - return call_state->cancelled; + return qatomic_read(&call_state->cancelled); } =20 int block_copy_call_status(BlockCopyCallState *call_state, bool *error_is_= read) { - assert(call_state->finished); + assert(qatomic_load_acquire(&call_state->finished)); if (error_is_read) { *error_is_read =3D call_state->error_is_read; } @@ -870,7 +873,7 @@ int block_copy_call_status(BlockCopyCallState *call_sta= te, bool *error_is_read) =20 void block_copy_call_cancel(BlockCopyCallState *call_state) { - call_state->cancelled =3D true; + qatomic_set(&call_state->cancelled, true); block_copy_kick(call_state); } =20 --=20 2.30.2 From nobody Fri May 17 05:50:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621333994; cv=none; d=zohomail.com; s=zohoarc; b=LTPBo6aaZaCJxAbm3YXF43YMwZ6+zpCB2OLDRx1ebEfa1RoXEQPkJluUI82KrPxWgXeZbw6FPNh269NfDMNqEYMib78bm3Qh9JNh0o3QlqMhyW9HXfOHLb0XDlyEccQMyoelS16Ka9K7YdWlBV7H84wjCMUMGxSBXoZSPwi3JsE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621333994; 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=FYDo54qY83IwJhQYjq5Rxgz+9nMMFeAafRwA+Be84us=; b=fRJCGoCofoJHXocyMOyQ/5rAQ7hA8E9Ldro7mNS3nEFFLTO0O7b6oV8lwFtu1WuY/iVoyVYtsFTS+dptDFR+B3ROiP8TXOks1r3kxtrzmyDs+tqyapmHmq9hhVrj78+pJKuDnCypv0Q4XJZcK3utk3wDxWlV1sFXEh2ay973Ouc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621333994036479.13064618341434; Tue, 18 May 2021 03:33:14 -0700 (PDT) Received: from localhost ([::1]:46038 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lix2G-0003mH-Sy for importer@patchew.org; Tue, 18 May 2021 06:33:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43186) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweb-00034K-0K for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:46088) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1liweX-0007pY-Kl for qemu-devel@nongnu.org; Tue, 18 May 2021 06:08:44 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-444-LQoZIBCfMdCHY89pbQQxtw-1; Tue, 18 May 2021 06:08:36 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id A22A7100945E; Tue, 18 May 2021 10:08:35 +0000 (UTC) Received: from localhost.localdomain (ovpn-114-236.ams2.redhat.com [10.36.114.236]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1770C687D4; Tue, 18 May 2021 10:08:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621332519; 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=FYDo54qY83IwJhQYjq5Rxgz+9nMMFeAafRwA+Be84us=; b=To0pbxYoDu6AjidFsOP441ILPEoqUl6c6Lg3P91J+aTTYXt3Syisn9qLWcsF11AaqkyIY9 iB9z+GafULGsdM0qN6EFgMf1XMvGFTVc+jIiZWBkW4n1m94rX9ay5fJrXj201CLACR9ij+ aEH5vXjkKJgBB6QjIgWbQhfXcjGgG4M= X-MC-Unique: LQoZIBCfMdCHY89pbQQxtw-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v2 7/7] block-copy: protect BlockCopyState .method fields Date: Tue, 18 May 2021 12:07:57 +0200 Message-Id: <20210518100757.31243-8-eesposit@redhat.com> In-Reply-To: <20210518100757.31243-1-eesposit@redhat.com> References: <20210518100757.31243-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eesposit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.374, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" With tasks and calls lock protecting all State fields, .method is the last BlockCopyState field left unprotected. Set it as atomic. Signed-off-by: Emanuele Giuseppe Esposito --- block/block-copy.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/block/block-copy.c b/block/block-copy.c index 573e96fefb..ebccb7fbc6 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -108,7 +108,7 @@ typedef struct BlockCopyState { =20 /* State */ int64_t in_flight_bytes; /* protected by tasks_lock */ - BlockCopyMethod method; + BlockCopyMethod method; /* atomic */ CoMutex tasks_lock; QLIST_HEAD(, BlockCopyTask) tasks; /* All tasks from all block-copy ca= lls */ QemuMutex calls_lock; @@ -184,7 +184,7 @@ static bool coroutine_fn block_copy_wait_one(BlockCopyS= tate *s, int64_t offset, =20 static inline int64_t block_copy_chunk_size(BlockCopyState *s) { - switch (s->method) { + switch (qatomic_read(&s->method)) { case COPY_READ_WRITE_CLUSTER: return s->cluster_size; case COPY_READ_WRITE: @@ -338,16 +338,17 @@ BlockCopyState *block_copy_state_new(BdrvChild *sourc= e, BdrvChild *target, * buffered copying (read and write respect max_transfer on their * behalf). */ - s->method =3D COPY_READ_WRITE_CLUSTER; + qatomic_set(&s->method, COPY_READ_WRITE_CLUSTER); } else if (write_flags & BDRV_REQ_WRITE_COMPRESSED) { /* Compression supports only cluster-size writes and no copy-range= . */ - s->method =3D COPY_READ_WRITE_CLUSTER; + qatomic_set(&s->method, COPY_READ_WRITE_CLUSTER); } else { /* * We enable copy-range, but keep small copy_size, until first * successful copy_range (look at block_copy_do_copy). */ - s->method =3D use_copy_range ? COPY_RANGE_SMALL : COPY_READ_WRITE; + qatomic_set(&s->method, use_copy_range ? COPY_RANGE_SMALL : + COPY_READ_WRITE); } =20 ratelimit_init(&s->rate_limit); @@ -432,26 +433,24 @@ static int coroutine_fn block_copy_do_copy(BlockCopyS= tate *s, return ret; } =20 - if (s->method >=3D COPY_RANGE_SMALL) { + if (qatomic_read(&s->method) >=3D COPY_RANGE_SMALL) { ret =3D bdrv_co_copy_range(s->source, offset, s->target, offset, n= bytes, 0, s->write_flags); if (ret < 0) { trace_block_copy_copy_range_fail(s, offset, ret); - s->method =3D COPY_READ_WRITE; + qatomic_set(&s->method, COPY_READ_WRITE); /* Fallback to read+write with allocated buffer */ } else { - if (s->method =3D=3D COPY_RANGE_SMALL) { - /* - * Successful copy-range. Now increase copy_size. copy_ra= nge - * does not respect max_transfer (it's a TODO), so we fact= or - * that in here. - * - * Note: we double-check s->method for the case when - * parallel block-copy request unsets it during previous - * bdrv_co_copy_range call. - */ - s->method =3D COPY_RANGE_FULL; - } + /* + * Successful copy-range. Now increase copy_size. copy_range + * does not respect max_transfer (it's a TODO), so we factor + * that in here. + * + * Note: we double-check s->method for the case when + * parallel block-copy request unsets it during previous + * bdrv_co_copy_range call. + */ + qatomic_cmpxchg(&s->method, COPY_RANGE_SMALL, COPY_RANGE_FULL); goto out; } } --=20 2.30.2