From nobody Thu May 2 05:52:27 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 ARC-Seal: i=1; a=rsa-sha256; t=1599760106; cv=none; d=zohomail.com; s=zohoarc; b=ian8K/mnQKcsL3sCOsf7Vhd1FGPIPauOkqDN5+4fGYc+I3cdzniKLmAOwzoBm3AEgxsD7SQ3Gc6sqnoG6O7TJAXJK7/YzRseX2xyABBmVYRRy1pg0fJrYFujNOy2rny3XP1QF9/u9lBA0wlhbrd53lTpLQXeOJCI9NlamJoKOBI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599760106; 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=CCQk4gxUq9a7nkIZ5yTEM8VEAw+njngKoOhqkpjyAFQ=; b=D/KOL0EBKwOcAjCg7yDUd50AWs4W9A6bmeBe5QUGUlUmM5YayfzEhicDuYVBr8yuztAo39JH2Fz5JniZdllnvWkCXShbZbByv/3EZFRkuY7G8HeTVV47Be3R2Amqen70hcuUBS3mMKnmCw21/POHHUvJO/gejrZODNn8p9nGzHg= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15997601067279.670550480113207; Thu, 10 Sep 2020 10:48:26 -0700 (PDT) Received: from localhost ([::1]:35154 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kGQgL-00085K-Fq for importer@patchew.org; Thu, 10 Sep 2020 13:48:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46650) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kGQer-0005Zr-EV; Thu, 10 Sep 2020 13:46:53 -0400 Received: from fanzine.igalia.com ([178.60.130.6]:46657) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kGQep-0004TJ-4Q; Thu, 10 Sep 2020 13:46:53 -0400 Received: from [81.0.34.67] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1kGQeR-0007HB-Gc; Thu, 10 Sep 2020 19:46:27 +0200 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1kGQeC-0006qB-R4; Thu, 10 Sep 2020 19:46:12 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=CCQk4gxUq9a7nkIZ5yTEM8VEAw+njngKoOhqkpjyAFQ=; b=mK5BgWou8XwBqWd7YoGIYaHGcCyKLrcqtcg+e6N3xhCQKZPt1+TrkNnbLVTiiwQF0LHpnd5CHftsuoZs9BsNFL9CWOqEtp5nlyDaRfqPbfqz7QJj4ajC49YEcUx5xBg2JPLS5Fbfe4CryOOJ/zcusOLccaJzBDzPJIhV7DbH9YkMtUhB+/Rxv/NX5JocE+jCAtavqhq/I+1Nv51hwiM7EsETPT9tj0naA4fPHwIkzN/RSfeUdWxi8YLnsWDibbupP2cFVn5qEfuzfXeq+UcCpxXg7PFIzO+z1r3vXy9lQ5SQQXUvhyWspWuixBbtps4KqraAKj65D3LPIKBQml5EhQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH v3 1/2] qcow2: Report BDRV_BLOCK_ZERO more accurately in bdrv_co_block_status() Date: Thu, 10 Sep 2020 19:46:01 +0200 Message-Id: <104cbe2dfac940e4c5ca0a7e2eb22a0245e206f7.1599759873.git.berto@igalia.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=178.60.130.6; envelope-from=berto@igalia.com; helo=fanzine.igalia.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/10 13:46:27 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] 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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, 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 , Vladimir Sementsov-Ogievskiy , Alberto Garcia , qemu-block@nongnu.org, Max Reitz 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" If a BlockDriverState supports backing files but has none then any unallocated area reads back as zeroes. bdrv_co_block_status() is only reporting this is if want_zero is true, but this is an inexpensive test and there is no reason not to do it in all cases. Suggested-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Alberto Garcia Reviewed-by: Vladimir Sementsov-Ogievskiy --- block/io.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/block/io.c b/block/io.c index ad3a51ed53..1b0ae29610 100644 --- a/block/io.c +++ b/block/io.c @@ -2408,16 +2408,16 @@ static int coroutine_fn bdrv_co_block_status(BlockD= riverState *bs, =20 if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) { ret |=3D BDRV_BLOCK_ALLOCATED; - } else if (want_zero && bs->drv->supports_backing) { - if (bs->backing) { + } else if (bs->drv->supports_backing) { + if (!bs->backing) { + ret |=3D BDRV_BLOCK_ZERO; + } else if (want_zero) { BlockDriverState *bs2 =3D bs->backing->bs; int64_t size2 =3D bdrv_getlength(bs2); =20 if (size2 >=3D 0 && offset >=3D size2) { ret |=3D BDRV_BLOCK_ZERO; } - } else { - ret |=3D BDRV_BLOCK_ZERO; } } =20 --=20 2.20.1 From nobody Thu May 2 05:52:27 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 ARC-Seal: i=1; a=rsa-sha256; t=1599760088; cv=none; d=zohomail.com; s=zohoarc; b=KFm2U+qkuSvLI1YO4AxtEvCMUD5JwtK0lXig8h/ouJK97HrWTMxEc/AnPzIhrYxAsplIZ6n7lKbdi4QRs0i/N7g3Q0ZM/Y3QxhsUJUZlY2z73gKNl0hQI/Sqmk5YheHTee8Vx7d6x4Fz4qOd+RXj9uM8TZSUxaltoodJWX0Xve8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599760088; 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=oh7p+GuAjxIB7Ji7FUWAB9kUgg2V3ZEstJErTlgEaAQ=; b=NBiKa5VJkQaESN0w13k+0ofTgcC+icOpgf8TBcgbHOykrprXrTg8hBEUu5D8hhOQM+eF/Xv/FXNiNN1cQVb2mrCx9VyrJY2Hd46tiFqNpo3h9HRFfXiz9WzYT80zA2anm/akd8wRneNa67834vVClozm5dvtuZCl5QhGnzwituM= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 159976008864671.85110358693328; Thu, 10 Sep 2020 10:48:08 -0700 (PDT) Received: from localhost ([::1]:33204 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kGQg3-0007HN-63 for importer@patchew.org; Thu, 10 Sep 2020 13:48:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46654) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kGQer-0005aY-Qf; Thu, 10 Sep 2020 13:46:53 -0400 Received: from fanzine.igalia.com ([178.60.130.6]:46658) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kGQep-0004TI-32; Thu, 10 Sep 2020 13:46:53 -0400 Received: from [81.0.34.67] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1kGQeR-0007HC-Gy; Thu, 10 Sep 2020 19:46:27 +0200 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1kGQeC-0006qD-S6; Thu, 10 Sep 2020 19:46:12 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=oh7p+GuAjxIB7Ji7FUWAB9kUgg2V3ZEstJErTlgEaAQ=; b=FpW+EQPfC+/hV9lYmYXoZ8NjLdGUrqL2FR75aaTxi3QqDOHsQFgqO9Nh/1pr9621r7jgBQdqd2RdN46MV7Ugu4LWOXIaq1qLfTVRHarPWQJSmIbd7/3uUOxnzYycGxl51hvMtrLHCqIZ4c154ARVH0nGcZ0v/VMmXC0gZKtSXJ8xIMAioh4ge0gKrFxWAbXoCfVsOzN6tzARl4dfpr7nYNElk40YG7ihPFN2peVyAI/vrbmWKKVpiTRGz3s5bKhGMJIfRtrjdQ8fETY0DypY9xIW2k7UEsgfX6gXEhZEiLLL/+wrTVvULplR5nRqkARL1JWNeHb/CCCGzLGQkuz9Dg==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH v3 2/2] qcow2: Skip copy-on-write when allocating a zero cluster Date: Thu, 10 Sep 2020 19:46:02 +0200 Message-Id: <0350dd55702eacd701e0aea924b101db7448a517.1599759873.git.berto@igalia.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=178.60.130.6; envelope-from=berto@igalia.com; helo=fanzine.igalia.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/10 13:46:27 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] 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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, 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 , Vladimir Sementsov-Ogievskiy , Alberto Garcia , qemu-block@nongnu.org, Max Reitz 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" Since commit c8bb23cbdbe32f5c326365e0a82e1b0e68cdcd8a when a write request results in a new allocation QEMU first tries to see if the rest of the cluster outside the written area contains only zeroes. In that case, instead of doing a normal copy-on-write operation and writing explicit zero buffers to disk, the code zeroes the whole cluster efficiently using pwrite_zeroes() with BDRV_REQ_NO_FALLBACK. This improves performance very significantly but it only happens when we are writing to an area that was completely unallocated before. Zero clusters (QCOW2_CLUSTER_ZERO_*) are treated like normal clusters and are therefore slower to allocate. This happens because the code uses bdrv_is_allocated_above() rather bdrv_block_status_above(). The former is not as accurate for this purpose but it is faster. However in the case of qcow2 the underlying call does already report zero clusters just fine so there is no reason why we cannot use that information. After testing 4KB writes on an image that only contains zero clusters this patch results in almost five times more IOPS. Signed-off-by: Alberto Garcia Reviewed-by: Vladimir Sementsov-Ogievskiy --- include/block/block.h | 2 ++ block/io.c | 27 +++++++++++++++++++++++++++ block/qcow2.c | 35 +++++++++++++++++++---------------- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/include/block/block.h b/include/block/block.h index 6e36154061..71f5678de7 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -496,6 +496,8 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t off= set, int64_t bytes, int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base, bool include_base, int64_t offset, int64_t byt= es, int64_t *pnum); +int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset, + int64_t bytes); =20 bool bdrv_is_read_only(BlockDriverState *bs); int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only, diff --git a/block/io.c b/block/io.c index 1b0ae29610..5950ad87be 100644 --- a/block/io.c +++ b/block/io.c @@ -2557,6 +2557,33 @@ int bdrv_block_status(BlockDriverState *bs, int64_t = offset, int64_t bytes, offset, bytes, pnum, map, file); } =20 +/* + * Check @bs (and its backing chain) to see if the range defined + * by @offset and @bytes is known to read as zeroes. + * Return 1 if that is the case, 0 otherwise and -errno on error. + * This test is meant to be fast rather than accurate so returning 0 + * does not guarantee non-zero data. + */ +int coroutine_fn bdrv_co_is_zero_fast(BlockDriverState *bs, int64_t offset, + int64_t bytes) +{ + int ret; + int64_t pnum =3D bytes; + + if (!bytes) { + return 1; + } + + ret =3D bdrv_common_block_status_above(bs, NULL, false, offset, + bytes, &pnum, NULL, NULL); + + if (ret < 0) { + return ret; + } + + return (pnum =3D=3D bytes) && (ret & BDRV_BLOCK_ZERO); +} + int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset, int64_t bytes, int64_t *pnum) { diff --git a/block/qcow2.c b/block/qcow2.c index da56b1a4df..68ab6562e3 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2391,26 +2391,26 @@ static bool merge_cow(uint64_t offset, unsigned byt= es, return false; } =20 -static bool is_unallocated(BlockDriverState *bs, int64_t offset, int64_t b= ytes) -{ - int64_t nr; - return !bytes || - (!bdrv_is_allocated_above(bs, NULL, false, offset, bytes, &nr) && - nr =3D=3D bytes); -} - -static bool is_zero_cow(BlockDriverState *bs, QCowL2Meta *m) +/* + * Return 1 if the COW regions read as zeroes, 0 if not, < 0 on error. + * Note that returning 0 does not guarantee non-zero data. + */ +static int is_zero_cow(BlockDriverState *bs, QCowL2Meta *m) { /* * This check is designed for optimization shortcut so it must be * efficient. - * Instead of is_zero(), use is_unallocated() as it is faster (but not - * as accurate and can result in false negatives). + * Instead of is_zero(), use bdrv_co_is_zero_fast() as it is + * faster (but not as accurate and can result in false negatives). */ - return is_unallocated(bs, m->offset + m->cow_start.offset, - m->cow_start.nb_bytes) && - is_unallocated(bs, m->offset + m->cow_end.offset, - m->cow_end.nb_bytes); + int ret =3D bdrv_co_is_zero_fast(bs, m->offset + m->cow_start.offset, + m->cow_start.nb_bytes); + if (ret <=3D 0) { + return ret; + } + + return bdrv_co_is_zero_fast(bs, m->offset + m->cow_end.offset, + m->cow_end.nb_bytes); } =20 static int handle_alloc_space(BlockDriverState *bs, QCowL2Meta *l2meta) @@ -2436,7 +2436,10 @@ static int handle_alloc_space(BlockDriverState *bs, = QCowL2Meta *l2meta) continue; } =20 - if (!is_zero_cow(bs, m)) { + ret =3D is_zero_cow(bs, m); + if (ret < 0) { + return ret; + } else if (ret =3D=3D 0) { continue; } =20 --=20 2.20.1