From nobody Sat May 4 00:47:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1495539703977114.97466713243273; Tue, 23 May 2017 04:41:43 -0700 (PDT) Received: from localhost ([::1]:47877 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD8C6-0006o9-Ir for importer@patchew.org; Tue, 23 May 2017 07:41:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46663) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD7ux-0000AY-Ew for qemu-devel@nongnu.org; Tue, 23 May 2017 07:24:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dD7us-0000Ho-Go for qemu-devel@nongnu.org; Tue, 23 May 2017 07:23:59 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:55469) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dD7us-00085L-0u; Tue, 23 May 2017 07:23:54 -0400 Received: from [192.168.13.244] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1dD7uD-0002iR-PJ; Tue, 23 May 2017 13:23:13 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1dD7uD-0005Lf-MW; Tue, 23 May 2017 14:23:13 +0300 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=TN6joPgqUqKLt0aAc5U8gBOUkNdvM8MbyavLXmC6TbU=; b=N0YAn1TRumAc4qGAmmmquTN527sHS4/hEf4cgM4XE3efmjAFy2weurNkRK27JhE/Gl1UvdKBv+GumP3AzZQoZD2m8pGZ/Je+ZHfXkHu/b1yS49uqJURkuhQt+MCY015AcrTlRUnwFr1gDdeR7CkQViq97GC5ulT2dWf5sH98YbbcJtUm5Lweaj8D8wzKTi3cL+ybkhzpTu5vx6gVbYCmacLrGZw7y284t2s8eOQ4/YTNrfKZ4C4WWH7RTlyQr7bWRzPqpBzz96Yx13LKb8ydTQwcio/OUwXMcCcdELHxbknlrUY/ur7YCk8X6VPNbS4jHnmPWoXFQOBXl+472ijq6A==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 23 May 2017 13:22:56 +0200 Message-Id: <504d7d07c4227d4615b72f46deb34ba77efbea2c.1495536228.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH 1/7] qcow2: Remove unused Error in do_perform_cow() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , "Denis V . Lunev" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" qcow2_encrypt_sectors() does not need an Error parameter, and we're not checking its value anyway, so we can safely remove it. Signed-off-by: Alberto Garcia Reviewed-by: Eric Blake --- block/qcow2-cluster.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 347d94b0d2..3dc170e421 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -440,16 +440,14 @@ static int coroutine_fn do_perform_cow(BlockDriverSta= te *bs, } =20 if (bs->encrypted) { - Error *err =3D NULL; int64_t sector =3D (src_cluster_offset + offset_in_cluster) >> BDRV_SECTOR_BITS; assert(s->cipher); assert((offset_in_cluster & ~BDRV_SECTOR_MASK) =3D=3D 0); assert((bytes & ~BDRV_SECTOR_MASK) =3D=3D 0); if (qcow2_encrypt_sectors(s, sector, iov.iov_base, iov.iov_base, - bytes >> BDRV_SECTOR_BITS, true, &err) <= 0) { + bytes >> BDRV_SECTOR_BITS, true, NULL) <= 0) { ret =3D -EIO; - error_free(err); goto out; } } --=20 2.11.0 From nobody Sat May 4 00:47:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149553887136961.55449118775607; Tue, 23 May 2017 04:27:51 -0700 (PDT) Received: from localhost ([::1]:47805 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD7yf-0003DU-TQ for importer@patchew.org; Tue, 23 May 2017 07:27:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46666) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD7ux-0000Aa-EH for qemu-devel@nongnu.org; Tue, 23 May 2017 07:24:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dD7us-0000Hi-HJ for qemu-devel@nongnu.org; Tue, 23 May 2017 07:23:59 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:55466) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dD7us-00085H-0m; Tue, 23 May 2017 07:23:54 -0400 Received: from [192.168.13.244] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1dD7uD-0002iV-Qh; Tue, 23 May 2017 13:23:13 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1dD7uD-0005Li-Nv; Tue, 23 May 2017 14:23:13 +0300 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=lXQbx2BlVVoyWSpbJ20Ay9p+5eJnM7NwbujpDRW5xt8=; b=i7ss5F9RRPVtLn/TUdNLwH2jk2zZAiO8+j+3Su6sNp4GvfydQaCdljkji8p+WDiiDLX10biTvEKG0HzFavHtIY22LwQH/y4iyVW5jQ9O2U4moQ8YQpREwiUvHcrzoyNH0Ii5Q8FM60bhyp0c/V/u0mVOh3A+JfBHHZi7rOt4azPVJDoKCcXv+4iiC48D6Rb0tbdxBLgw6tTbyfOk4fyVFp7kJV7zzTiUpkoitoIEcAzXUO3wh+zsyq8jn0F7UNfDVzY/EY2HRhD8WBleEGqKUhNWPtT8+8RxH1DYz/DqXnUmTUeqJqTZbLVDY4dq6C2dr6A7Gk8UamMF+RS5KNFNOA==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 23 May 2017 13:22:57 +0200 Message-Id: <95b03a81b53b6cd20c78112545072eb3c669c7d5.1495536228.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH 2/7] qcow2: Use unsigned int for both members of Qcow2COWRegion X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , "Denis V . Lunev" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Qcow2COWRegion has two attributes: - The offset of the COW region from the start of the first cluster touched by the I/O request. Since it's always going to be positive and the maximum request size is at most INT_MAX, we can use a regular unsigned int to store this offset. - The size of the COW region in bytes. This is guaranteed to be >=3D 0, so we should use an unsigned type instead. In x86_64 this reduces the size of Qcow2COWRegion from 16 to 8 bytes. It will also help keep some assertions simpler now that we know that there are no negative numbers. The prototype of do_perform_cow() is also updated to reflect these changes. Signed-off-by: Alberto Garcia --- block/qcow2-cluster.c | 4 ++-- block/qcow2.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 3dc170e421..6757875ec9 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -406,8 +406,8 @@ int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t se= ctor_num, static int coroutine_fn do_perform_cow(BlockDriverState *bs, uint64_t src_cluster_offset, uint64_t cluster_offset, - int offset_in_cluster, - int bytes) + unsigned offset_in_cluster, + unsigned bytes) { BDRVQcow2State *s =3D bs->opaque; QEMUIOVector qiov; diff --git a/block/qcow2.h b/block/qcow2.h index 1801dc30dc..c26ee0a33d 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -301,10 +301,10 @@ typedef struct Qcow2COWRegion { * Offset of the COW region in bytes from the start of the first clust= er * touched by the request. */ - uint64_t offset; + unsigned offset; =20 /** Number of bytes to copy */ - int nb_bytes; + unsigned nb_bytes; } Qcow2COWRegion; =20 /** --=20 2.11.0 From nobody Sat May 4 00:47:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1495540328013985.182127457749; Tue, 23 May 2017 04:52:08 -0700 (PDT) Received: from localhost ([::1]:47958 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD8MA-00078N-OE for importer@patchew.org; Tue, 23 May 2017 07:52:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46668) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD7ux-0000Ab-Ez for qemu-devel@nongnu.org; Tue, 23 May 2017 07:24:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dD7us-0000HV-F2 for qemu-devel@nongnu.org; Tue, 23 May 2017 07:23:59 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:55463) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dD7us-00085F-0r; Tue, 23 May 2017 07:23:54 -0400 Received: from [192.168.13.244] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1dD7uD-0002iW-SB; Tue, 23 May 2017 13:23:13 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1dD7uD-0005Ll-P8; Tue, 23 May 2017 14:23:13 +0300 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=pNRJ0cGcgV6jlNtSye7A43+EdZi4ecivQyu2VCjM4S8=; b=SzAYxfuBD9M7oD7TXAXfPTlUdoBQ39OreFAmH1JfCmpMWjYJpzkLilEO//MAIu52Mw6KLbMeTyhi7e5TXk7eJRZij9A1TQcNDq7kZor/LxfXv0vtWDzshukN3rvj94DohA1jvsABIJZsIURKc/NYnOth99B1rOWs45+ofOHBxVaZRzUJ6tm7mnVuLPybgyVXWEZ8+thlvB6z4Vh114bsH3llwUllMDa17xxq1fQ/mVbXJlnTXBYzNeqZIxmsMQ/MjwuWFyNKKK9i2uY/SPujWAeDvD3HG718Tx4KxuDuGrAuGFI0LsE9P4nPLUrz3HUy/slTTgd7LXeaJkq00SZyYQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 23 May 2017 13:22:58 +0200 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH 3/7] qcow2: Make perform_cow() call do_perform_cow() twice X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , "Denis V . Lunev" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Instead of calling perform_cow() twice with a different COW region each time, call it just once and make perform_cow() handle both regions. This patch simply moves code around. The next one will do the actual reordering of the COW operations. Signed-off-by: Alberto Garcia --- block/qcow2-cluster.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 6757875ec9..59a0ffba1a 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -414,6 +414,10 @@ static int coroutine_fn do_perform_cow(BlockDriverStat= e *bs, struct iovec iov; int ret; =20 + if (bytes =3D=3D 0) { + return 0; + } + iov.iov_len =3D bytes; iov.iov_base =3D qemu_try_blockalign(bs, iov.iov_len); if (iov.iov_base =3D=3D NULL) { @@ -751,31 +755,40 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockD= riverState *bs, return cluster_offset; } =20 -static int perform_cow(BlockDriverState *bs, QCowL2Meta *m, Qcow2COWRegion= *r) +static int perform_cow(BlockDriverState *bs, QCowL2Meta *m) { BDRVQcow2State *s =3D bs->opaque; + Qcow2COWRegion *start =3D &m->cow_start; + Qcow2COWRegion *end =3D &m->cow_end; int ret; =20 - if (r->nb_bytes =3D=3D 0) { + if (start->nb_bytes =3D=3D 0 && end->nb_bytes =3D=3D 0) { return 0; } =20 qemu_co_mutex_unlock(&s->lock); - ret =3D do_perform_cow(bs, m->offset, m->alloc_offset, r->offset, r->n= b_bytes); + ret =3D do_perform_cow(bs, m->offset, m->alloc_offset, + start->offset, start->nb_bytes); + if (ret < 0) { + goto fail; + } + + ret =3D do_perform_cow(bs, m->offset, m->alloc_offset, + end->offset, end->nb_bytes); + +fail: qemu_co_mutex_lock(&s->lock); =20 - if (ret < 0) { - return ret; - } - /* * Before we update the L2 table to actually point to the new cluster,= we * need to be sure that the refcounts have been increased and COW was * handled. */ - qcow2_cache_depends_on_flush(s->l2_table_cache); + if (ret =3D=3D 0) { + qcow2_cache_depends_on_flush(s->l2_table_cache); + } =20 - return 0; + return ret; } =20 int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) @@ -795,12 +808,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, = QCowL2Meta *m) } =20 /* copy content of unmodified sectors */ - ret =3D perform_cow(bs, m, &m->cow_start); - if (ret < 0) { - goto err; - } - - ret =3D perform_cow(bs, m, &m->cow_end); + ret =3D perform_cow(bs, m); if (ret < 0) { goto err; } --=20 2.11.0 From nobody Sat May 4 00:47:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1495540429108201.75524030989504; Tue, 23 May 2017 04:53:49 -0700 (PDT) Received: from localhost ([::1]:47986 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD8Nn-0000B8-Kv for importer@patchew.org; Tue, 23 May 2017 07:53:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46673) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD7ux-0000Ag-Fb for qemu-devel@nongnu.org; Tue, 23 May 2017 07:24:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dD7us-0000Hw-Jr for qemu-devel@nongnu.org; Tue, 23 May 2017 07:23:59 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:55464) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dD7us-00085G-0z; Tue, 23 May 2017 07:23:54 -0400 Received: from [192.168.13.244] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1dD7uD-0002iX-Sb; Tue, 23 May 2017 13:23:13 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1dD7uD-0005Lo-QP; Tue, 23 May 2017 14:23:13 +0300 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=yDShEMWC3NFwPZXSeuguHhqlk/iEXYahTZj2i1SbV14=; b=b44gkMlkeXlp3ay/8gkv0ounPUhz6NxS9LFUdlJEzVhrVoabRbVztw+t+imdFT1Y1bBVZcZJ1XHy7NiFcA14O58vPJi6xg3NnkQBDiII9GjbPrhYtdzR+VkhQTMZdSCDt3dMcgMe7/eLZo2m4HQjQT4+bWgyD6O+TOsdiVHatnJZtC72RbvfyK6V+7euPKvn7wiDE7EJlGnIUIGmrifF07ARJQOAxBy3aOiWf2zw4Mvd2a7mQ1WjVWwwDm0EwNC7lP7b3706z2SKr5HhXzhZxpBqmjV6SdrUJzrKb27ztkFoTZCpCn/844y8DDJOB4nJIVAB/D0dpBqT66opi5LOLw==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 23 May 2017 13:22:59 +0200 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH 4/7] qcow2: Split do_perform_cow() into _read(), _encrypt() and _write() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , "Denis V . Lunev" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch splits do_perform_cow() into three separate functions to read, encrypt and write the COW regions. perform_cow() can now read both regions first, then encrypt them and finally write them to disk. The memory allocation is also done in this function now, using one single buffer large enough to hold both regions. Signed-off-by: Alberto Garcia --- block/qcow2-cluster.c | 114 +++++++++++++++++++++++++++++++++++++---------= ---- 1 file changed, 84 insertions(+), 30 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 59a0ffba1a..643c48088b 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -403,34 +403,26 @@ int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t = sector_num, return 0; } =20 -static int coroutine_fn do_perform_cow(BlockDriverState *bs, - uint64_t src_cluster_offset, - uint64_t cluster_offset, - unsigned offset_in_cluster, - unsigned bytes) +static int coroutine_fn do_perform_cow_read(BlockDriverState *bs, + uint64_t src_cluster_offset, + unsigned offset_in_cluster, + uint8_t *buffer, + unsigned bytes) { - BDRVQcow2State *s =3D bs->opaque; QEMUIOVector qiov; - struct iovec iov; + struct iovec iov =3D { .iov_base =3D buffer, .iov_len =3D bytes }; int ret; =20 if (bytes =3D=3D 0) { return 0; } =20 - iov.iov_len =3D bytes; - iov.iov_base =3D qemu_try_blockalign(bs, iov.iov_len); - if (iov.iov_base =3D=3D NULL) { - return -ENOMEM; - } - qemu_iovec_init_external(&qiov, &iov, 1); =20 BLKDBG_EVENT(bs->file, BLKDBG_COW_READ); =20 if (!bs->drv) { - ret =3D -ENOMEDIUM; - goto out; + return -ENOMEDIUM; } =20 /* Call .bdrv_co_readv() directly instead of using the public block-la= yer @@ -440,39 +432,63 @@ static int coroutine_fn do_perform_cow(BlockDriverSta= te *bs, ret =3D bs->drv->bdrv_co_preadv(bs, src_cluster_offset + offset_in_clu= ster, bytes, &qiov, 0); if (ret < 0) { - goto out; + return ret; } =20 - if (bs->encrypted) { + return 0; +} + +static bool coroutine_fn do_perform_cow_encrypt(BlockDriverState *bs, + uint64_t src_cluster_offse= t, + unsigned offset_in_cluster, + uint8_t *buffer, + unsigned bytes) +{ + if (bytes && bs->encrypted) { + BDRVQcow2State *s =3D bs->opaque; int64_t sector =3D (src_cluster_offset + offset_in_cluster) >> BDRV_SECTOR_BITS; assert(s->cipher); assert((offset_in_cluster & ~BDRV_SECTOR_MASK) =3D=3D 0); assert((bytes & ~BDRV_SECTOR_MASK) =3D=3D 0); - if (qcow2_encrypt_sectors(s, sector, iov.iov_base, iov.iov_base, + if (qcow2_encrypt_sectors(s, sector, buffer, buffer, bytes >> BDRV_SECTOR_BITS, true, NULL) <= 0) { - ret =3D -EIO; - goto out; + return false; } } + return true; +} + +static int coroutine_fn do_perform_cow_write(BlockDriverState *bs, + uint64_t cluster_offset, + unsigned offset_in_cluster, + uint8_t *buffer, + unsigned bytes) +{ + QEMUIOVector qiov; + struct iovec iov =3D { .iov_base =3D buffer, .iov_len =3D bytes }; + int ret; + + if (bytes =3D=3D 0) { + return 0; + } + + qemu_iovec_init_external(&qiov, &iov, 1); =20 ret =3D qcow2_pre_write_overlap_check(bs, 0, cluster_offset + offset_in_cluster, bytes); if (ret < 0) { - goto out; + return ret; } =20 BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE); ret =3D bdrv_co_pwritev(bs->file, cluster_offset + offset_in_cluster, bytes, &qiov, 0); if (ret < 0) { - goto out; + return ret; } =20 - ret =3D 0; -out: - qemu_vfree(iov.iov_base); - return ret; + return 0; } =20 =20 @@ -760,22 +776,59 @@ static int perform_cow(BlockDriverState *bs, QCowL2Me= ta *m) BDRVQcow2State *s =3D bs->opaque; Qcow2COWRegion *start =3D &m->cow_start; Qcow2COWRegion *end =3D &m->cow_end; + unsigned buffer_size =3D start->nb_bytes + end->nb_bytes; + uint8_t *start_buffer, *end_buffer; int ret; =20 + assert(start->nb_bytes <=3D UINT_MAX - end->nb_bytes); + if (start->nb_bytes =3D=3D 0 && end->nb_bytes =3D=3D 0) { return 0; } =20 + /* Reserve a buffer large enough to store the data from both the + * start and end COW regions */ + start_buffer =3D qemu_try_blockalign(bs, buffer_size); + if (start_buffer =3D=3D NULL) { + return -ENOMEM; + } + /* The part of the buffer where the end region is located */ + end_buffer =3D start_buffer + start->nb_bytes; + qemu_co_mutex_unlock(&s->lock); - ret =3D do_perform_cow(bs, m->offset, m->alloc_offset, - start->offset, start->nb_bytes); + /* First we read the existing data from both COW regions */ + ret =3D do_perform_cow_read(bs, m->offset, start->offset, + start_buffer, start->nb_bytes); if (ret < 0) { goto fail; } =20 - ret =3D do_perform_cow(bs, m->offset, m->alloc_offset, - end->offset, end->nb_bytes); + ret =3D do_perform_cow_read(bs, m->offset, end->offset, + end_buffer, end->nb_bytes); + if (ret < 0) { + goto fail; + } + + /* Encrypt the data if necessary before writing it */ + if (bs->encrypted) { + if (!do_perform_cow_encrypt(bs, m->offset, start->offset, + start_buffer, start->nb_bytes) || + !do_perform_cow_encrypt(bs, m->offset, end->offset, + end_buffer, end->nb_bytes)) { + ret =3D -EIO; + goto fail; + } + } + + /* And now we can write everything */ + ret =3D do_perform_cow_write(bs, m->alloc_offset, start->offset, + start_buffer, start->nb_bytes); + if (ret < 0) { + goto fail; + } =20 + ret =3D do_perform_cow_write(bs, m->alloc_offset, end->offset, + end_buffer, end->nb_bytes); fail: qemu_co_mutex_lock(&s->lock); =20 @@ -788,6 +841,7 @@ fail: qcow2_cache_depends_on_flush(s->l2_table_cache); } =20 + qemu_vfree(start_buffer); return ret; } =20 --=20 2.11.0 From nobody Sat May 4 00:47:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1495540505721832.94441455994; Tue, 23 May 2017 04:55:05 -0700 (PDT) Received: from localhost ([::1]:48012 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD8P2-00018a-FT for importer@patchew.org; Tue, 23 May 2017 07:55:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46662) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD7ux-0000AX-Dq for qemu-devel@nongnu.org; Tue, 23 May 2017 07:24:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dD7us-0000Hc-Gi for qemu-devel@nongnu.org; Tue, 23 May 2017 07:23:59 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:55467) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dD7us-00085I-12; Tue, 23 May 2017 07:23:54 -0400 Received: from [192.168.13.244] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1dD7uD-0002iY-Te; Tue, 23 May 2017 13:23:13 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1dD7uD-0005Lr-RM; Tue, 23 May 2017 14:23:13 +0300 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=SSRXc/4b/QF1sLwoRspYZVebbK8vJJiolbKVHg0E/zs=; b=mm2/fL3Ggnv+zV4HPJLEsQ+4ZEAf/iibx8ugd0uErztqOa1PSkuH2V6peA3rgmP7sSkfpeTrcLCvW+CdpvByponx5tH9jD06sWu5sxUT2RyXHQx2L7cG2ap9aqXCJgDzxmmj6yHM/JCTgOB+luMpjtVr8z22KW89RoVg0r0+Zv5OZQLKSl7apoIB9dB95oSZ2YtYFXsBVtBVQrycp7s/X+sE1npBqyynJfMjxbNLjDp02/zRejnirBaa/33pv0KTapUusgImSM35SFFMZjrmnLfrofanQhmS/ObWESxEXXYhktRSNO1BVj07N240KhMM24Gtz2siLK/0HeavmMlthQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 23 May 2017 13:23:00 +0200 Message-Id: <47a9c0b2ade031f343757c1cd8b8aa475b34e595.1495536228.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH 5/7] qcow2: Allow reading both COW regions with only one request X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , "Denis V . Lunev" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Reading both COW regions requires two separate requests, but it's perfectly possible to merge them and perform only one. This generally improves performance, particularly on rotating disk drives. The downside is that the data in the middle region is read but discarded. This patch takes a conservative approach and only merges reads when the size of the middle region is <=3D 16KB. Signed-off-by: Alberto Garcia --- block/qcow2-cluster.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 643c48088b..67a761731d 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -777,34 +777,53 @@ static int perform_cow(BlockDriverState *bs, QCowL2Me= ta *m) Qcow2COWRegion *start =3D &m->cow_start; Qcow2COWRegion *end =3D &m->cow_end; unsigned buffer_size =3D start->nb_bytes + end->nb_bytes; + unsigned data_bytes =3D end->offset - (start->offset + start->nb_bytes= ); + bool merge_reads =3D false; uint8_t *start_buffer, *end_buffer; int ret; =20 assert(start->nb_bytes <=3D UINT_MAX - end->nb_bytes); + assert(buffer_size <=3D UINT_MAX - data_bytes); + assert(start->offset + start->nb_bytes <=3D end->offset); =20 if (start->nb_bytes =3D=3D 0 && end->nb_bytes =3D=3D 0) { return 0; } =20 - /* Reserve a buffer large enough to store the data from both the - * start and end COW regions */ + /* If we have to read both the start and end COW regions and the + * middle region is not too large then perform just one read + * operation */ + if (start->nb_bytes && end->nb_bytes && data_bytes <=3D 16384) { + merge_reads =3D true; + buffer_size +=3D data_bytes; + } + + /* Reserve a buffer large enough to store all the data that we're + * going to read */ start_buffer =3D qemu_try_blockalign(bs, buffer_size); if (start_buffer =3D=3D NULL) { return -ENOMEM; } /* The part of the buffer where the end region is located */ - end_buffer =3D start_buffer + start->nb_bytes; + end_buffer =3D start_buffer + buffer_size - end->nb_bytes; =20 qemu_co_mutex_unlock(&s->lock); - /* First we read the existing data from both COW regions */ - ret =3D do_perform_cow_read(bs, m->offset, start->offset, - start_buffer, start->nb_bytes); - if (ret < 0) { - goto fail; - } + /* First we read the existing data from both COW regions. We + * either read the whole region in one go, or the start and end + * regions separately. */ + if (merge_reads) { + ret =3D do_perform_cow_read(bs, m->offset, start->offset, + start_buffer, buffer_size); + } else { + ret =3D do_perform_cow_read(bs, m->offset, start->offset, + start_buffer, start->nb_bytes); + if (ret < 0) { + goto fail; + } =20 - ret =3D do_perform_cow_read(bs, m->offset, end->offset, - end_buffer, end->nb_bytes); + ret =3D do_perform_cow_read(bs, m->offset, end->offset, + end_buffer, end->nb_bytes); + } if (ret < 0) { goto fail; } --=20 2.11.0 From nobody Sat May 4 00:47:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1495539658291456.03443296443686; Tue, 23 May 2017 04:40:58 -0700 (PDT) Received: from localhost ([::1]:47876 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD8BM-0006C7-0t for importer@patchew.org; Tue, 23 May 2017 07:40:56 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46660) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD7ux-0000AU-DN for qemu-devel@nongnu.org; Tue, 23 May 2017 07:24:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dD7us-0000HR-Ez for qemu-devel@nongnu.org; Tue, 23 May 2017 07:23:59 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:55471) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dD7us-00085M-0i; Tue, 23 May 2017 07:23:54 -0400 Received: from [192.168.13.244] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1dD7uD-0002iZ-UW; Tue, 23 May 2017 13:23:14 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1dD7uD-0005Lu-SU; Tue, 23 May 2017 14:23:13 +0300 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=bJ9hA7kHI+deIrAK5i0azQ7Yoxe7jl8IbTha89NJMrk=; b=M0RUikvncuecntxT31Xz+CZqb1Rq80h9p4sYlWC6yTvckeyM3QBH9iS7vPOeKGPJC6W+8EyfvFQONXRLvU/KrVMv8PVoBjDInF71ju92LUKra8yFQvOjvKmhKr72pMZl2NyrRY4zqgZdcsk8lnPXz5lx8+UgrtbhLD7wW+tHskuNS1kOk+BsYT1g4JEZ1a+N2LiEZ23QE45+OP5Dm65qosdptH5z6i0n3i2G//9XA/JOsCjWD7oMSGALRR1TXJsASHmRMB1TTKyENkl4PM+sjFYzbiSjI6EvYejqMGJC9Dz7J5/7aeXxYlNjAJw9059JAIwexlyHMZbsqFC4YJc2gA==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 23 May 2017 13:23:01 +0200 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH 6/7] qcow2: Pass a QEMUIOVector to do_perform_cow_{read, write}() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , "Denis V . Lunev" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Instead of passing a single buffer pointer to do_perform_cow_write(), pass a QEMUIOVector. This will allow us to merge the write requests for the COW regions and the actual data into a single one. Although do_perform_cow_read() does not strictly need to change its API, we're doing it here as well for consistency. Signed-off-by: Alberto Garcia --- block/qcow2-cluster.c | 51 ++++++++++++++++++++++++-----------------------= ---- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 67a761731d..ae737adf88 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -406,19 +406,14 @@ int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t = sector_num, static int coroutine_fn do_perform_cow_read(BlockDriverState *bs, uint64_t src_cluster_offset, unsigned offset_in_cluster, - uint8_t *buffer, - unsigned bytes) + QEMUIOVector *qiov) { - QEMUIOVector qiov; - struct iovec iov =3D { .iov_base =3D buffer, .iov_len =3D bytes }; int ret; =20 - if (bytes =3D=3D 0) { + if (qiov->size =3D=3D 0) { return 0; } =20 - qemu_iovec_init_external(&qiov, &iov, 1); - BLKDBG_EVENT(bs->file, BLKDBG_COW_READ); =20 if (!bs->drv) { @@ -430,7 +425,7 @@ static int coroutine_fn do_perform_cow_read(BlockDriver= State *bs, * which can lead to deadlock when block layer copy-on-read is enabled. */ ret =3D bs->drv->bdrv_co_preadv(bs, src_cluster_offset + offset_in_clu= ster, - bytes, &qiov, 0); + qiov->size, qiov, 0); if (ret < 0) { return ret; } @@ -462,28 +457,23 @@ static bool coroutine_fn do_perform_cow_encrypt(Block= DriverState *bs, static int coroutine_fn do_perform_cow_write(BlockDriverState *bs, uint64_t cluster_offset, unsigned offset_in_cluster, - uint8_t *buffer, - unsigned bytes) + QEMUIOVector *qiov) { - QEMUIOVector qiov; - struct iovec iov =3D { .iov_base =3D buffer, .iov_len =3D bytes }; int ret; =20 - if (bytes =3D=3D 0) { + if (qiov->size =3D=3D 0) { return 0; } =20 - qemu_iovec_init_external(&qiov, &iov, 1); - ret =3D qcow2_pre_write_overlap_check(bs, 0, - cluster_offset + offset_in_cluster, bytes); + cluster_offset + offset_in_cluster, qiov->size); if (ret < 0) { return ret; } =20 BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE); ret =3D bdrv_co_pwritev(bs->file, cluster_offset + offset_in_cluster, - bytes, &qiov, 0); + qiov->size, qiov, 0); if (ret < 0) { return ret; } @@ -780,6 +770,7 @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta= *m) unsigned data_bytes =3D end->offset - (start->offset + start->nb_bytes= ); bool merge_reads =3D false; uint8_t *start_buffer, *end_buffer; + QEMUIOVector qiov; int ret; =20 assert(start->nb_bytes <=3D UINT_MAX - end->nb_bytes); @@ -807,22 +798,25 @@ static int perform_cow(BlockDriverState *bs, QCowL2Me= ta *m) /* The part of the buffer where the end region is located */ end_buffer =3D start_buffer + buffer_size - end->nb_bytes; =20 + qemu_iovec_init(&qiov, 3); + qemu_co_mutex_unlock(&s->lock); /* First we read the existing data from both COW regions. We * either read the whole region in one go, or the start and end * regions separately. */ if (merge_reads) { - ret =3D do_perform_cow_read(bs, m->offset, start->offset, - start_buffer, buffer_size); + qemu_iovec_add(&qiov, start_buffer, buffer_size); + ret =3D do_perform_cow_read(bs, m->offset, start->offset, &qiov); } else { - ret =3D do_perform_cow_read(bs, m->offset, start->offset, - start_buffer, start->nb_bytes); + qemu_iovec_add(&qiov, start_buffer, start->nb_bytes); + ret =3D do_perform_cow_read(bs, m->offset, start->offset, &qiov); if (ret < 0) { goto fail; } =20 - ret =3D do_perform_cow_read(bs, m->offset, end->offset, - end_buffer, end->nb_bytes); + qemu_iovec_reset(&qiov); + qemu_iovec_add(&qiov, end_buffer, end->nb_bytes); + ret =3D do_perform_cow_read(bs, m->offset, end->offset, &qiov); } if (ret < 0) { goto fail; @@ -840,14 +834,16 @@ static int perform_cow(BlockDriverState *bs, QCowL2Me= ta *m) } =20 /* And now we can write everything */ - ret =3D do_perform_cow_write(bs, m->alloc_offset, start->offset, - start_buffer, start->nb_bytes); + qemu_iovec_reset(&qiov); + qemu_iovec_add(&qiov, start_buffer, start->nb_bytes); + ret =3D do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov= ); if (ret < 0) { goto fail; } =20 - ret =3D do_perform_cow_write(bs, m->alloc_offset, end->offset, - end_buffer, end->nb_bytes); + qemu_iovec_reset(&qiov); + qemu_iovec_add(&qiov, end_buffer, end->nb_bytes); + ret =3D do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov); fail: qemu_co_mutex_lock(&s->lock); =20 @@ -861,6 +857,7 @@ fail: } =20 qemu_vfree(start_buffer); + qemu_iovec_destroy(&qiov); return ret; } =20 --=20 2.11.0 From nobody Sat May 4 00:47:14 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; dkim=fail spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1495539014796237.46569145970204; Tue, 23 May 2017 04:30:14 -0700 (PDT) Received: from localhost ([::1]:47814 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD80z-0005Eh-4h for importer@patchew.org; Tue, 23 May 2017 07:30:13 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46659) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dD7ux-0000AT-DK for qemu-devel@nongnu.org; Tue, 23 May 2017 07:24:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dD7us-0000I9-Lk for qemu-devel@nongnu.org; Tue, 23 May 2017 07:23:59 -0400 Received: from fanzine.igalia.com ([91.117.99.155]:55473) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dD7us-00085N-0i; Tue, 23 May 2017 07:23:54 -0400 Received: from [192.168.13.244] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1dD7uD-0002ib-Vb; Tue, 23 May 2017 13:23:14 +0200 Received: from berto by perseus.local with local (Exim 4.89) (envelope-from ) id 1dD7uD-0005Lx-TY; Tue, 23 May 2017 14:23:13 +0300 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=References:In-Reply-To:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=/FpTYsArkxVm7LLOtDyN+xl0xIplFVFj9TrxtiiOOi4=; b=WuFcNpRa1UEVZkPUKRvGI7qQgeZh+7+hiTUVnXkRf+4300R0DlURW4I73HZsViqpu89+U1VBfKbqSA5zMFLKlmPTJxMuJqrD0SqXkR75Gv38tyFQUxzzkTWIYzX0/99CeJlYPEf3tnxOtEKCGBhiN7fmDkqvfhk+9TIh4jvg7n8aPKk8QAiG8eTEJxPc1mfv/t7L2S3fGH4zdZQ4lpy3woiRYv5WRl/4mcxfvKDEwBZdUrQagQmL/dUf5PU24UDagzSpzuyT2AjhPgIe6pz0FWP9D1f4DcKr5gei8g0gG+ohwrdRPAs5RqTdEuXuAqXCCKDkuJMD4vBWOvp7lMC+TQ==; From: Alberto Garcia To: qemu-devel@nongnu.org Date: Tue, 23 May 2017 13:23:02 +0200 Message-Id: <4086b9ad1d1c912298f7c981f2c94a9f9a7026a3.1495536228.git.berto@igalia.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 91.117.99.155 Subject: [Qemu-devel] [PATCH 7/7] qcow2: Merge the writing of the COW regions with the guest data X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , "Denis V . Lunev" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If the guest tries to write data that results on the allocation of a new cluster, instead of writing the guest data first and then the data from the COW regions, write everything together using one single I/O operation. This can improve the write performance by 25% or more, depending on several factors such as the media type, the cluster size and the I/O request size. Signed-off-by: Alberto Garcia --- block/qcow2-cluster.c | 34 ++++++++++++++++++++++-------- block/qcow2.c | 58 ++++++++++++++++++++++++++++++++++++++++++-----= ---- block/qcow2.h | 7 +++++++ 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index ae737adf88..e926dad6bb 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -776,6 +776,7 @@ static int perform_cow(BlockDriverState *bs, QCowL2Meta= *m) assert(start->nb_bytes <=3D UINT_MAX - end->nb_bytes); assert(buffer_size <=3D UINT_MAX - data_bytes); assert(start->offset + start->nb_bytes <=3D end->offset); + assert(!m->data_qiov || m->data_qiov->size =3D=3D data_bytes); =20 if (start->nb_bytes =3D=3D 0 && end->nb_bytes =3D=3D 0) { return 0; @@ -833,17 +834,32 @@ static int perform_cow(BlockDriverState *bs, QCowL2Me= ta *m) } } =20 - /* And now we can write everything */ - qemu_iovec_reset(&qiov); - qemu_iovec_add(&qiov, start_buffer, start->nb_bytes); - ret =3D do_perform_cow_write(bs, m->alloc_offset, start->offset, &qiov= ); - if (ret < 0) { - goto fail; + /* And now we can write everything. If we have the guest data we + * can write everything in one single operation */ + if (m->data_qiov) { + qemu_iovec_reset(&qiov); + qemu_iovec_add(&qiov, start_buffer, start->nb_bytes); + qemu_iovec_concat(&qiov, m->data_qiov, 0, data_bytes); + qemu_iovec_add(&qiov, end_buffer, end->nb_bytes); + /* NOTE: we have a write_aio blkdebug event here followed by + * a cow_write one in do_perform_cow_write(), but there's only + * one single I/O operation */ + BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); + ret =3D do_perform_cow_write(bs, m->alloc_offset, start->offset, &= qiov); + } else { + /* If there's no guest data then write both COW regions separately= */ + qemu_iovec_reset(&qiov); + qemu_iovec_add(&qiov, start_buffer, start->nb_bytes); + ret =3D do_perform_cow_write(bs, m->alloc_offset, start->offset, &= qiov); + if (ret < 0) { + goto fail; + } + + qemu_iovec_reset(&qiov); + qemu_iovec_add(&qiov, end_buffer, end->nb_bytes); + ret =3D do_perform_cow_write(bs, m->alloc_offset, end->offset, &qi= ov); } =20 - qemu_iovec_reset(&qiov); - qemu_iovec_add(&qiov, end_buffer, end->nb_bytes); - ret =3D do_perform_cow_write(bs, m->alloc_offset, end->offset, &qiov); fail: qemu_co_mutex_lock(&s->lock); =20 diff --git a/block/qcow2.c b/block/qcow2.c index a8d61f0981..71023ab773 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1575,6 +1575,38 @@ fail: return ret; } =20 +/* Check if it's possible to merge a write request with the writing of + * the data from the COW regions */ +static bool can_merge_cow(uint64_t offset, unsigned bytes, + QEMUIOVector *hd_qiov, QCowL2Meta *l2meta) +{ + QCowL2Meta *m; + + for (m =3D l2meta; m !=3D NULL; m =3D m->next) { + /* If both COW regions are empty then there's nothing to merge */ + if (m->cow_start.nb_bytes =3D=3D 0 && m->cow_end.nb_bytes =3D=3D 0= ) { + continue; + } + + /* The data (middle) region must be immediately after the + * start region */ + if (l2meta_cow_start(m) + m->cow_start.nb_bytes !=3D offset) { + continue; + } + + /* The end region must be immediately after the data (middle) + * region */ + if (m->offset + m->cow_end.offset !=3D offset + bytes) { + continue; + } + + m->data_qiov =3D hd_qiov; + return true; + } + + return false; +} + static coroutine_fn int qcow2_co_pwritev(BlockDriverState *bs, uint64_t of= fset, uint64_t bytes, QEMUIOVector *qio= v, int flags) @@ -1657,16 +1689,22 @@ static coroutine_fn int qcow2_co_pwritev(BlockDrive= rState *bs, uint64_t offset, goto fail; } =20 - qemu_co_mutex_unlock(&s->lock); - BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); - trace_qcow2_writev_data(qemu_coroutine_self(), - cluster_offset + offset_in_cluster); - ret =3D bdrv_co_pwritev(bs->file, - cluster_offset + offset_in_cluster, - cur_bytes, &hd_qiov, 0); - qemu_co_mutex_lock(&s->lock); - if (ret < 0) { - goto fail; + /* If we need to do COW, check if it's possible to merge the + * writing of the guest data together with that of the COW regions. + * If it's not possible (or not necessary) then write the + * guest data now. */ + if (!can_merge_cow(offset, cur_bytes, &hd_qiov, l2meta)) { + qemu_co_mutex_unlock(&s->lock); + BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); + trace_qcow2_writev_data(qemu_coroutine_self(), + cluster_offset + offset_in_cluster); + ret =3D bdrv_co_pwritev(bs->file, + cluster_offset + offset_in_cluster, + cur_bytes, &hd_qiov, 0); + qemu_co_mutex_lock(&s->lock); + if (ret < 0) { + goto fail; + } } =20 while (l2meta !=3D NULL) { diff --git a/block/qcow2.h b/block/qcow2.h index c26ee0a33d..87b15eb4aa 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -343,6 +343,13 @@ typedef struct QCowL2Meta */ Qcow2COWRegion cow_end; =20 + /** + * The I/O vector with the data from the actual guest write request. + * If non-NULL, this is meant to be merged together with the data + * from @cow_start and @cow_end into one single write operation. + */ + QEMUIOVector *data_qiov; + /** Pointer to next L2Meta of the same write request */ struct QCowL2Meta *next; =20 --=20 2.11.0