From patchwork Tue May 18 10:07:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 12264523 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 127EDC433B4 for ; Tue, 18 May 2021 10:32:50 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 9AAC36108D for ; Tue, 18 May 2021 10:32:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9AAC36108D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:46038 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lix1s-0003mH-Jn for qemu-devel@archiver.kernel.org; Tue, 18 May 2021 06:32:48 -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 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= 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 X-MC-Unique: LQoZIBCfMdCHY89pbQQxtw-1 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) 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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 { /* 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 calls */ QemuMutex calls_lock; @@ -184,7 +184,7 @@ static bool coroutine_fn block_copy_wait_one(BlockCopyState *s, int64_t offset, 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 *source, BdrvChild *target, * buffered copying (read and write respect max_transfer on their * behalf). */ - s->method = 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 = 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 = use_copy_range ? COPY_RANGE_SMALL : COPY_READ_WRITE; + qatomic_set(&s->method, use_copy_range ? COPY_RANGE_SMALL : + COPY_READ_WRITE); } ratelimit_init(&s->rate_limit); @@ -432,26 +433,24 @@ static int coroutine_fn block_copy_do_copy(BlockCopyState *s, return ret; } - if (s->method >= COPY_RANGE_SMALL) { + if (qatomic_read(&s->method) >= COPY_RANGE_SMALL) { ret = bdrv_co_copy_range(s->source, offset, s->target, offset, nbytes, 0, s->write_flags); if (ret < 0) { trace_block_copy_copy_range_fail(s, offset, ret); - s->method = COPY_READ_WRITE; + qatomic_set(&s->method, COPY_READ_WRITE); /* Fallback to read+write with allocated buffer */ } else { - if (s->method == COPY_RANGE_SMALL) { - /* - * 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. - */ - s->method = 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; } }