From nobody Tue Nov 11 19:19:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1565871155; cv=none; d=zoho.com; s=zohoarc; b=nuI7kNTs/UnSudHcIFhW0H/LlL3upZFv0kxbgfYXMAjq7j67fOddvzKy1ga+KGWl33LoTdBE6L8zO85iMNITSAYMvaFRgGg9bMAao7+0c3bz3Yx0fCSxXzU2pxNnrFBz0ewUpeJWeQuO6Ic+239e/HKLgbqqmBx/5/YO60Wr02E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1565871155; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=MyGN0dcNpeY2UCcxMQ4i7wbhWg+StWk14VOrqge5Mlc=; b=bnTSRHfozXgtW3klU0ElNnI0rGg757aWo8Uhp8UGKBx70tspsrgp+ovbopkAZDW/v5maaF7qUmNgwu1Gy3nntwyLWwCsK8nj8fyaHVvFgaJ548R4mmJmQS6IuBW0NjZ3FHwTp6sRVU1JdBaG1jJIaxPPICXnUxf70Q083Ej4xRA= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1565871155342371.0135887489811; Thu, 15 Aug 2019 05:12:35 -0700 (PDT) Received: from localhost ([::1]:41326 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hyEcH-0004Kr-VJ for importer@patchew.org; Thu, 15 Aug 2019 08:12:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37008) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hyEah-0002Uy-65 for qemu-devel@nongnu.org; Thu, 15 Aug 2019 08:10:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hyEaf-00030u-Ja for qemu-devel@nongnu.org; Thu, 15 Aug 2019 08:10:51 -0400 Received: from relay.sw.ru ([185.231.240.75]:35652) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hyEac-0002vG-A9; Thu, 15 Aug 2019 08:10:46 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92) (envelope-from ) id 1hyEaZ-00060k-0C; Thu, 15 Aug 2019 15:10:43 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Date: Thu, 15 Aug 2019 15:10:39 +0300 Message-Id: <20190815121042.121309-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20190815121042.121309-1-vsementsov@virtuozzo.com> References: <20190815121042.121309-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v3 1/4] block: introduce aio task pool 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: kwolf@redhat.com, vsementsov@virtuozzo.com, armbru@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Common interface for aio task loops. To be used for improving performance of synchronous io loops in qcow2, block-stream, copy-on-read, and may be other places. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- include/block/aio_task.h | 54 +++++++++++++++++ block/aio_task.c | 124 +++++++++++++++++++++++++++++++++++++++ block/Makefile.objs | 2 + 3 files changed, 180 insertions(+) create mode 100644 include/block/aio_task.h create mode 100644 block/aio_task.c diff --git a/include/block/aio_task.h b/include/block/aio_task.h new file mode 100644 index 0000000000..58b4d99e59 --- /dev/null +++ b/include/block/aio_task.h @@ -0,0 +1,54 @@ +/* + * Aio tasks loops + * + * Copyright (c) 2019 Virtuozzo International GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#ifndef BLOCK_AIO_TASK_H +#define BLOCK_AIO_TASK_H + +#include "qemu/coroutine.h" + +typedef struct AioTaskPool AioTaskPool; +typedef struct AioTask AioTask; +typedef int coroutine_fn (*AioTaskFunc)(AioTask *task); +struct AioTask { + AioTaskPool *pool; + AioTaskFunc func; + int ret; +}; + +AioTaskPool *aio_task_pool_new(int max_busy_tasks); +void aio_task_pool_free(AioTaskPool *); + +/* error code of failed task or 0 if all is OK */ +int aio_task_pool_status(AioTaskPool *pool); + +bool aio_task_pool_empty(AioTaskPool *pool); + +/* User provides filled @task, however task->pool will be set automaticall= y */ +void coroutine_fn aio_task_pool_start_task(AioTaskPool *pool, AioTask *tas= k); + +void coroutine_fn aio_task_pool_wait_slot(AioTaskPool *pool); +void coroutine_fn aio_task_pool_wait_one(AioTaskPool *pool); +void coroutine_fn aio_task_pool_wait_all(AioTaskPool *pool); + +#endif /* BLOCK_AIO_TASK_H */ diff --git a/block/aio_task.c b/block/aio_task.c new file mode 100644 index 0000000000..3eacfd1f40 --- /dev/null +++ b/block/aio_task.c @@ -0,0 +1,124 @@ +/* + * Aio tasks loops + * + * Copyright (c) 2019 Virtuozzo International GmbH. + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#include "qemu/osdep.h" +#include "block/aio.h" +#include "block/aio_task.h" + +struct AioTaskPool { + Coroutine *main_co; + int status; + int max_busy_tasks; + int busy_tasks; + bool waiting; +}; + +static void coroutine_fn aio_task_co(void *opaque) +{ + AioTask *task =3D opaque; + AioTaskPool *pool =3D task->pool; + + assert(pool->busy_tasks < pool->max_busy_tasks); + pool->busy_tasks++; + + task->ret =3D task->func(task); + + pool->busy_tasks--; + + if (task->ret < 0 && pool->status =3D=3D 0) { + pool->status =3D task->ret; + } + + g_free(task); + + if (pool->waiting) { + pool->waiting =3D false; + aio_co_wake(pool->main_co); + } +} + +void coroutine_fn aio_task_pool_wait_one(AioTaskPool *pool) +{ + assert(pool->busy_tasks > 0); + assert(qemu_coroutine_self() =3D=3D pool->main_co); + + pool->waiting =3D true; + qemu_coroutine_yield(); + + assert(!pool->waiting); + assert(pool->busy_tasks < pool->max_busy_tasks); +} + +void coroutine_fn aio_task_pool_wait_slot(AioTaskPool *pool) +{ + if (pool->busy_tasks < pool->max_busy_tasks) { + return; + } + + aio_task_pool_wait_one(pool); +} + +void coroutine_fn aio_task_pool_wait_all(AioTaskPool *pool) +{ + while (pool->busy_tasks > 0) { + aio_task_pool_wait_one(pool); + } +} + +void coroutine_fn aio_task_pool_start_task(AioTaskPool *pool, AioTask *tas= k) +{ + aio_task_pool_wait_slot(pool); + + task->pool =3D pool; + qemu_coroutine_enter(qemu_coroutine_create(aio_task_co, task)); +} + +AioTaskPool *aio_task_pool_new(int max_busy_tasks) +{ + AioTaskPool *pool =3D g_new0(AioTaskPool, 1); + + pool->main_co =3D qemu_coroutine_self(); + pool->max_busy_tasks =3D max_busy_tasks; + + return pool; +} + +void aio_task_pool_free(AioTaskPool *pool) +{ + g_free(pool); +} + +int aio_task_pool_status(AioTaskPool *pool) +{ + if (!pool) { + return 0; /* Sugar for lazy allocation of aio pool */ + } + + return pool->status; +} + +bool aio_task_pool_empty(AioTaskPool *pool) +{ + return pool->busy_tasks =3D=3D 0; +} diff --git a/block/Makefile.objs b/block/Makefile.objs index 35f3bca4d9..c2eb8c8769 100644 --- a/block/Makefile.objs +++ b/block/Makefile.objs @@ -40,6 +40,8 @@ block-obj-y +=3D throttle.o copy-on-read.o =20 block-obj-y +=3D crypto.o =20 +block-obj-y +=3D aio_task.o + common-obj-y +=3D stream.o =20 nfs.o-libs :=3D $(LIBNFS_LIBS) --=20 2.18.0 From nobody Tue Nov 11 19:19:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1565871296; cv=none; d=zoho.com; s=zohoarc; b=Vjzi5+Fl/metiYepx2+iCsMRwQ0AkULAmy0I1/ZBGJQikGo5dfW+ejUPRLvwHLgXed/0a9aj7tstVnq06ODLKzKlZDg66vyHq8frsOYZd1i2OAJRh2eNGsdCcUsTOj4d3nl7Z167aSI5wncKp+kru3r4mFLFNdla35NIMI8NHPs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1565871296; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=PPPQorsS0QpMrXhaRYAnb+5NAjKaZ8/SoV+4FKhNjOs=; b=dCPSLxXcp9FeImo1nxgbF0h39Z0ZL/aUPhB0M9KfTfuMbStjmU0AVOJUTt55WPnN7HoH3BZ43BWXCPfL67W7w9BbGT/wjPbjQlY4ClEZJpal8fmwUIJbfGJtZrdFXtnJ8CN7m2meNZ1Y4q176uAao7vGL1Rk5Qq0ry65XY1C358= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1565871295428498.860391475027; Thu, 15 Aug 2019 05:14:55 -0700 (PDT) Received: from localhost ([::1]:41322 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hyEc4-0003yg-PI for importer@patchew.org; Thu, 15 Aug 2019 08:12:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37007) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hyEah-0002Ux-5u for qemu-devel@nongnu.org; Thu, 15 Aug 2019 08:10:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hyEaf-00031M-Ns for qemu-devel@nongnu.org; Thu, 15 Aug 2019 08:10:51 -0400 Received: from relay.sw.ru ([185.231.240.75]:35644) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hyEac-0002vI-AA; Thu, 15 Aug 2019 08:10:46 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92) (envelope-from ) id 1hyEaZ-00060k-4k; Thu, 15 Aug 2019 15:10:43 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Date: Thu, 15 Aug 2019 15:10:40 +0300 Message-Id: <20190815121042.121309-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20190815121042.121309-1-vsementsov@virtuozzo.com> References: <20190815121042.121309-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v3 2/4] block/qcow2: refactor qcow2_co_preadv_part 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: kwolf@redhat.com, vsementsov@virtuozzo.com, armbru@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Further patch will run partial requests of iterations of qcow2_co_preadv in parallel for performance reasons. To prepare for this, separate part which may be parallelized into separate function (qcow2_co_preadv_task). While being here, also separate encrypted clusters reading to own function, like it is done for compressed reading. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- qapi/block-core.json | 2 +- block/qcow2.c | 205 +++++++++++++++++++++++-------------------- 2 files changed, 111 insertions(+), 96 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index 0d43d4f37c..dd80aa11db 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -3266,7 +3266,7 @@ 'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev', 'pwritev_zero', 'pwritev_done', 'empty_image_prepare', 'l1_shrink_write_table', 'l1_shrink_free_l2_clusters', - 'cor_write', 'cluster_alloc_space', 'none'] } + 'cor_write', 'cluster_alloc_space', 'none', 'read_encrypted'] } =20 ## # @BlkdebugIOType: diff --git a/block/qcow2.c b/block/qcow2.c index 93ab7edcea..89afb4272e 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1967,17 +1967,114 @@ out: return ret; } =20 +static coroutine_fn int +qcow2_co_preadv_encrypted(BlockDriverState *bs, + uint64_t file_cluster_offset, + uint64_t offset, + uint64_t bytes, + QEMUIOVector *qiov, + uint64_t qiov_offset) +{ + int ret; + BDRVQcow2State *s =3D bs->opaque; + uint8_t *buf; + + assert(bs->encrypted && s->crypto); + assert(bytes <=3D QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); + + /* + * For encrypted images, read everything into a temporary + * contiguous buffer on which the AES functions can work. + * Also, decryption in a separate buffer is better as it + * prevents the guest from learning information about the + * encrypted nature of the virtual disk. + */ + + buf =3D qemu_try_blockalign(s->data_file->bs, bytes); + if (buf =3D=3D NULL) { + return -ENOMEM; + } + + BLKDBG_EVENT(bs->file, BLKDBG_READ_ENCRYPTED); + ret =3D bdrv_co_pread(s->data_file, + file_cluster_offset + offset_into_cluster(s, offse= t), + bytes, buf, 0); + if (ret < 0) { + goto fail; + } + + assert((offset & (BDRV_SECTOR_SIZE - 1)) =3D=3D 0); + assert((bytes & (BDRV_SECTOR_SIZE - 1)) =3D=3D 0); + if (qcow2_co_decrypt(bs, file_cluster_offset, offset, buf, bytes) < 0)= { + ret =3D -EIO; + goto fail; + } + qemu_iovec_from_buf(qiov, qiov_offset, buf, bytes); + +fail: + qemu_vfree(buf); + + return ret; +} + +static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs, + QCow2ClusterType cluster_type, + uint64_t file_cluster_offset, + uint64_t offset, uint64_t byt= es, + QEMUIOVector *qiov, + size_t qiov_offset) +{ + BDRVQcow2State *s =3D bs->opaque; + int offset_in_cluster =3D offset_into_cluster(s, offset); + + switch (cluster_type) { + case QCOW2_CLUSTER_ZERO_PLAIN: + case QCOW2_CLUSTER_ZERO_ALLOC: + /* Both zero types are handled in qcow2_co_preadv_part */ + g_assert_not_reached(); + + case QCOW2_CLUSTER_UNALLOCATED: + assert(bs->backing); /* otherwise handled in qcow2_co_preadv_part = */ + + BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); + return bdrv_co_preadv_part(bs->backing, offset, bytes, + qiov, qiov_offset, 0); + + case QCOW2_CLUSTER_COMPRESSED: + return qcow2_co_preadv_compressed(bs, file_cluster_offset, + offset, bytes, qiov, qiov_offset= ); + + case QCOW2_CLUSTER_NORMAL: + if ((file_cluster_offset & 511) !=3D 0) { + return -EIO; + } + + if (bs->encrypted) { + return qcow2_co_preadv_encrypted(bs, file_cluster_offset, + offset, bytes, qiov, qiov_off= set); + } + + BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); + return bdrv_co_preadv_part(s->data_file, + file_cluster_offset + offset_in_cluster, + bytes, qiov, qiov_offset, 0); + + default: + g_assert_not_reached(); + } + + g_assert_not_reached(); +} + static coroutine_fn int qcow2_co_preadv_part(BlockDriverState *bs, uint64_t offset, uint64_t byt= es, QEMUIOVector *qiov, size_t qiov_offset, int flags) { BDRVQcow2State *s =3D bs->opaque; - int offset_in_cluster; int ret; unsigned int cur_bytes; /* number of bytes in current iteration */ uint64_t cluster_offset =3D 0; - uint8_t *cluster_data =3D NULL; =20 while (bytes !=3D 0) { =20 @@ -1992,111 +2089,29 @@ static coroutine_fn int qcow2_co_preadv_part(Block= DriverState *bs, ret =3D qcow2_get_cluster_offset(bs, offset, &cur_bytes, &cluster_= offset); qemu_co_mutex_unlock(&s->lock); if (ret < 0) { - goto fail; + return ret; } =20 - offset_in_cluster =3D offset_into_cluster(s, offset); - - switch (ret) { - case QCOW2_CLUSTER_UNALLOCATED: - - if (bs->backing) { - BLKDBG_EVENT(bs->file, BLKDBG_READ_BACKING_AIO); - ret =3D bdrv_co_preadv_part(bs->backing, offset, cur_bytes, - qiov, qiov_offset, 0); - if (ret < 0) { - goto fail; - } - } else { - /* Note: in this case, no need to wait */ - qemu_iovec_memset(qiov, qiov_offset, 0, cur_bytes); - } - break; - - case QCOW2_CLUSTER_ZERO_PLAIN: - case QCOW2_CLUSTER_ZERO_ALLOC: + if (ret =3D=3D QCOW2_CLUSTER_ZERO_PLAIN || + ret =3D=3D QCOW2_CLUSTER_ZERO_ALLOC || + (ret =3D=3D QCOW2_CLUSTER_UNALLOCATED && !bs->backing)) + { qemu_iovec_memset(qiov, qiov_offset, 0, cur_bytes); - break; - - case QCOW2_CLUSTER_COMPRESSED: - ret =3D qcow2_co_preadv_compressed(bs, cluster_offset, - offset, cur_bytes, - qiov, qiov_offset); + } else { + ret =3D qcow2_co_preadv_task(bs, ret, + cluster_offset, offset, cur_bytes, + qiov, qiov_offset); if (ret < 0) { - goto fail; - } - - break; - - case QCOW2_CLUSTER_NORMAL: - if ((cluster_offset & 511) !=3D 0) { - ret =3D -EIO; - goto fail; - } - - if (bs->encrypted) { - assert(s->crypto); - - /* - * For encrypted images, read everything into a temporary - * contiguous buffer on which the AES functions can work. - */ - if (!cluster_data) { - cluster_data =3D - qemu_try_blockalign(s->data_file->bs, - QCOW_MAX_CRYPT_CLUSTERS - * s->cluster_size); - if (cluster_data =3D=3D NULL) { - ret =3D -ENOMEM; - goto fail; - } - } - - assert(cur_bytes <=3D QCOW_MAX_CRYPT_CLUSTERS * s->cluster= _size); - - BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); - ret =3D bdrv_co_pread(s->data_file, - cluster_offset + offset_in_cluster, - cur_bytes, cluster_data, 0); - if (ret < 0) { - goto fail; - } - - assert((offset & (BDRV_SECTOR_SIZE - 1)) =3D=3D 0); - assert((cur_bytes & (BDRV_SECTOR_SIZE - 1)) =3D=3D 0); - if (qcow2_co_decrypt(bs, cluster_offset, offset, - cluster_data, cur_bytes) < 0) { - ret =3D -EIO; - goto fail; - } - qemu_iovec_from_buf(qiov, qiov_offset, cluster_data, cur_b= ytes); - } else { - BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); - ret =3D bdrv_co_preadv_part(s->data_file, - cluster_offset + offset_in_clust= er, - cur_bytes, qiov, qiov_offset, 0); - if (ret < 0) { - goto fail; - } + return ret; } - break; - - default: - g_assert_not_reached(); - ret =3D -EIO; - goto fail; } =20 bytes -=3D cur_bytes; offset +=3D cur_bytes; qiov_offset +=3D cur_bytes; } - ret =3D 0; - -fail: - qemu_vfree(cluster_data); =20 - return ret; + return 0; } =20 /* Check if it's possible to merge a write request with the writing of --=20 2.18.0 From nobody Tue Nov 11 19:19:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1565871296; cv=none; d=zoho.com; s=zohoarc; b=nNqSE5OuI2KvkdXTpJSBihOc0HXtEeLp1wcSW7kJeCTzALC0VbW1nuA1mD/cS07x+TaHaeudQAl5eCvV8IGGWP8xdi38dy8hhyZxVAnjnWtLGgIqsYx170t88gw7T0cpmzfJLj5oxTirA+8cJLwLuqSnNIQHuHFBxU3udcLPsxc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1565871296; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=Gl8KgiYI0XRkr45zDGhQrX8ROYwdHlXw6m3Aut+wugo=; b=ROMSsJH2uzTpabtW5SuEPdDT3PfajA2Ssp0ugQRlKP7czrbwKNZL1XBVsbUZeFpwljajeqLNWHsiaJSnfIV1Rup2Nnuqd2mHoHvqizJ6DbMP+KYtYYpHpmZWXVz6l5C2lAnumj466JlGis+fguP0d46GrTL2JXkykepf5N/OZ6Y= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1565871295290158.41214752823214; Thu, 15 Aug 2019 05:14:55 -0700 (PDT) Received: from localhost ([::1]:41324 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hyEc6-00040F-VM for importer@patchew.org; Thu, 15 Aug 2019 08:12:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37009) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hyEah-0002Uz-6T for qemu-devel@nongnu.org; Thu, 15 Aug 2019 08:10:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hyEaf-00030z-Jq for qemu-devel@nongnu.org; Thu, 15 Aug 2019 08:10:51 -0400 Received: from relay.sw.ru ([185.231.240.75]:35640) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hyEac-0002vF-AB; Thu, 15 Aug 2019 08:10:46 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92) (envelope-from ) id 1hyEaZ-00060k-93; Thu, 15 Aug 2019 15:10:43 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Date: Thu, 15 Aug 2019 15:10:41 +0300 Message-Id: <20190815121042.121309-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20190815121042.121309-1-vsementsov@virtuozzo.com> References: <20190815121042.121309-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v3 3/4] block/qcow2: refactor qcow2_co_pwritev_part 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: kwolf@redhat.com, vsementsov@virtuozzo.com, armbru@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Similarly to previous commit, prepare for parallelizing write-loop iterations. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- block/qcow2.c | 153 +++++++++++++++++++++++++++++--------------------- 1 file changed, 89 insertions(+), 64 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 89afb4272e..3aaa180e2b 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2234,6 +2234,87 @@ static int handle_alloc_space(BlockDriverState *bs, = QCowL2Meta *l2meta) return 0; } =20 +/* + * qcow2_co_pwritev_task + * Called with s->lock unlocked + * l2meta - if not NULL, qcow2_co_do_pwritev() will consume it. Caller mu= st not + * use it somehow after qcow2_co_pwritev_task() call + */ +static coroutine_fn int qcow2_co_pwritev_task(BlockDriverState *bs, + uint64_t file_cluster_offset, + uint64_t offset, uint64_t by= tes, + QEMUIOVector *qiov, + uint64_t qiov_offset, + QCowL2Meta *l2meta) +{ + int ret; + BDRVQcow2State *s =3D bs->opaque; + void *crypt_buf =3D NULL; + int offset_in_cluster =3D offset_into_cluster(s, offset); + QEMUIOVector encrypted_qiov; + + if (bs->encrypted) { + assert(s->crypto); + assert(bytes <=3D QCOW_MAX_CRYPT_CLUSTERS * s->cluster_size); + crypt_buf =3D qemu_try_blockalign(bs->file->bs, bytes); + if (crypt_buf =3D=3D NULL) { + ret =3D -ENOMEM; + goto out_unlocked; + } + qemu_iovec_to_buf(qiov, qiov_offset, crypt_buf, bytes); + + if (qcow2_co_encrypt(bs, file_cluster_offset, offset, + crypt_buf, bytes) < 0) { + ret =3D -EIO; + goto out_unlocked; + } + + qemu_iovec_init_buf(&encrypted_qiov, crypt_buf, bytes); + qiov =3D &encrypted_qiov; + qiov_offset =3D 0; + } + + /* Try to efficiently initialize the physical space with zeroes */ + ret =3D handle_alloc_space(bs, l2meta); + if (ret < 0) { + goto out_unlocked; + } + + /* + * 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 (!merge_cow(offset, bytes, qiov, qiov_offset, l2meta)) { + BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); + trace_qcow2_writev_data(qemu_coroutine_self(), + file_cluster_offset + offset_in_cluster); + ret =3D bdrv_co_pwritev_part(s->data_file, + file_cluster_offset + offset_in_cluster, + bytes, qiov, qiov_offset, 0); + if (ret < 0) { + goto out_unlocked; + } + } + + qemu_co_mutex_lock(&s->lock); + + ret =3D qcow2_handle_l2meta(bs, &l2meta, true); + goto out_locked; + +out_unlocked: + qemu_co_mutex_lock(&s->lock); + +out_locked: + qcow2_handle_l2meta(bs, &l2meta, false); + qemu_co_mutex_unlock(&s->lock); + + qemu_vfree(crypt_buf); + + return ret; +} + static coroutine_fn int qcow2_co_pwritev_part( BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, size_t qiov_offset, int flags) @@ -2243,15 +2324,10 @@ static coroutine_fn int qcow2_co_pwritev_part( int ret; unsigned int cur_bytes; /* number of sectors in current iteration */ uint64_t cluster_offset; - QEMUIOVector encrypted_qiov; - uint64_t bytes_done =3D 0; - uint8_t *cluster_data =3D NULL; QCowL2Meta *l2meta =3D NULL; =20 trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes); =20 - qemu_co_mutex_lock(&s->lock); - while (bytes !=3D 0) { =20 l2meta =3D NULL; @@ -2265,6 +2341,8 @@ static coroutine_fn int qcow2_co_pwritev_part( - offset_in_cluster); } =20 + qemu_co_mutex_lock(&s->lock); + ret =3D qcow2_alloc_cluster_offset(bs, offset, &cur_bytes, &cluster_offset, &l2meta); if (ret < 0) { @@ -2282,73 +2360,20 @@ static coroutine_fn int qcow2_co_pwritev_part( =20 qemu_co_mutex_unlock(&s->lock); =20 - if (bs->encrypted) { - assert(s->crypto); - if (!cluster_data) { - cluster_data =3D qemu_try_blockalign(bs->file->bs, - QCOW_MAX_CRYPT_CLUSTERS - * s->cluster_size); - if (cluster_data =3D=3D NULL) { - ret =3D -ENOMEM; - goto out_unlocked; - } - } - - assert(cur_bytes <=3D QCOW_MAX_CRYPT_CLUSTERS * s->cluster_siz= e); - qemu_iovec_to_buf(qiov, qiov_offset + bytes_done, - cluster_data, cur_bytes); - - if (qcow2_co_encrypt(bs, cluster_offset, offset, - cluster_data, cur_bytes) < 0) { - ret =3D -EIO; - goto out_unlocked; - } - - qemu_iovec_init_buf(&encrypted_qiov, cluster_data, cur_bytes); - } - - /* Try to efficiently initialize the physical space with zeroes */ - ret =3D handle_alloc_space(bs, l2meta); + ret =3D qcow2_co_pwritev_task(bs, cluster_offset, offset, cur_byte= s, + qiov, qiov_offset, l2meta); + l2meta =3D NULL; /* l2meta is consumed by qcow2_co_pwritev_task() = */ if (ret < 0) { - goto out_unlocked; - } - - /* 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 (!merge_cow(offset, cur_bytes, - bs->encrypted ? &encrypted_qiov : qiov, - bs->encrypted ? 0 : qiov_offset + bytes_done, l2met= a)) - { - BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); - trace_qcow2_writev_data(qemu_coroutine_self(), - cluster_offset + offset_in_cluster); - ret =3D bdrv_co_pwritev_part( - s->data_file, cluster_offset + offset_in_cluster, cur_= bytes, - bs->encrypted ? &encrypted_qiov : qiov, - bs->encrypted ? 0 : qiov_offset + bytes_done, 0); - if (ret < 0) { - goto out_unlocked; - } - } - - qemu_co_mutex_lock(&s->lock); - - ret =3D qcow2_handle_l2meta(bs, &l2meta, true); - if (ret) { - goto out_locked; + goto fail_nometa; } =20 bytes -=3D cur_bytes; offset +=3D cur_bytes; - bytes_done +=3D cur_bytes; + qiov_offset +=3D cur_bytes; trace_qcow2_writev_done_part(qemu_coroutine_self(), cur_bytes); } ret =3D 0; - goto out_locked; =20 -out_unlocked: qemu_co_mutex_lock(&s->lock); =20 out_locked: @@ -2356,7 +2381,7 @@ out_locked: =20 qemu_co_mutex_unlock(&s->lock); =20 - qemu_vfree(cluster_data); +fail_nometa: trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); =20 return ret; --=20 2.18.0 From nobody Tue Nov 11 19:19:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1565871239; cv=none; d=zoho.com; s=zohoarc; b=kWIcCO0hXnkwH6g3jONUQUJCW7JPuE7S8hLxdttLozdKvv9mDUZ0YpfiLrJN/uUGlJsRdFUtXC6VixMDkgOBOP+nOIorAHVUROyKe4FKslIo955LLKXAIgII0qKjm+ouABhkCcn7CdEjSROJAdDsrcB8SdJvRnOfeLPHHaR6b8A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1565871239; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=3Mit8S9SvNd0LImaTQ/Jsa/Q3NCrZSspMgqkZZ3ezQc=; b=JANm2G/CKREmiMeC3MoiDtAe03q2hlFabPfh16wGdsSfVXlLVrptIHYK9cVdJJgvm6ES7FScOzG1oK9cutgJEZZ08ysmewGPzhR3YgcUHSX3abnOQHowngmtziAektW3FZaHkwadKVp2T6867HOLnv0oob3R9q25TI1ujd89kkI= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1565871239946742.2748513691794; Thu, 15 Aug 2019 05:13:59 -0700 (PDT) Received: from localhost ([::1]:41344 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hyEdi-0006om-O0 for importer@patchew.org; Thu, 15 Aug 2019 08:13:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37016) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1hyEah-0002Vx-UC for qemu-devel@nongnu.org; Thu, 15 Aug 2019 08:10:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hyEaf-000329-Uo for qemu-devel@nongnu.org; Thu, 15 Aug 2019 08:10:51 -0400 Received: from relay.sw.ru ([185.231.240.75]:35648) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hyEac-0002vH-A7; Thu, 15 Aug 2019 08:10:46 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92) (envelope-from ) id 1hyEaZ-00060k-FS; Thu, 15 Aug 2019 15:10:43 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Date: Thu, 15 Aug 2019 15:10:42 +0300 Message-Id: <20190815121042.121309-5-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20190815121042.121309-1-vsementsov@virtuozzo.com> References: <20190815121042.121309-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v3 4/4] block/qcow2: introduce parallel subrequest handling in read and write 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: kwolf@redhat.com, vsementsov@virtuozzo.com, armbru@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, den@openvz.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" It improves performance for fragmented qcow2 images. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- block/qcow2.h | 3 ++ block/qcow2.c | 125 ++++++++++++++++++++++++++++++++++++++++----- block/trace-events | 1 + 3 files changed, 117 insertions(+), 12 deletions(-) diff --git a/block/qcow2.h b/block/qcow2.h index 998bcdaef1..fdfa9c31cd 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -65,6 +65,9 @@ #define QCOW2_MAX_BITMAPS 65535 #define QCOW2_MAX_BITMAP_DIRECTORY_SIZE (1024 * QCOW2_MAX_BITMAPS) =20 +/* Maximum of parallel sub-request per guest request */ +#define QCOW2_MAX_WORKERS 8 + /* indicate that the refcount of the referenced cluster is exactly one. */ #define QCOW_OFLAG_COPIED (1ULL << 63) /* indicate that the cluster is compressed (they never have the copied fla= g) */ diff --git a/block/qcow2.c b/block/qcow2.c index 3aaa180e2b..36b41e8536 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -40,6 +40,7 @@ #include "qapi/qobject-input-visitor.h" #include "qapi/qapi-visit-block-core.h" #include "crypto.h" +#include "block/aio_task.h" =20 /* Differences with QCOW: @@ -2017,6 +2018,60 @@ fail: return ret; } =20 +typedef struct Qcow2AioTask { + AioTask task; + + BlockDriverState *bs; + QCow2ClusterType cluster_type; /* only for read */ + uint64_t file_cluster_offset; + uint64_t offset; + uint64_t bytes; + QEMUIOVector *qiov; + uint64_t qiov_offset; + QCowL2Meta *l2meta; /* only for write */ +} Qcow2AioTask; + +static coroutine_fn int qcow2_co_preadv_task_entry(AioTask *task); +static coroutine_fn int qcow2_add_task(BlockDriverState *bs, + AioTaskPool *pool, + AioTaskFunc func, + QCow2ClusterType cluster_type, + uint64_t file_cluster_offset, + uint64_t offset, + uint64_t bytes, + QEMUIOVector *qiov, + size_t qiov_offset, + QCowL2Meta *l2meta) +{ + Qcow2AioTask local_task; + Qcow2AioTask *task =3D pool ? g_new(Qcow2AioTask, 1) : &local_task; + + *task =3D (Qcow2AioTask) { + .task.func =3D func, + .bs =3D bs, + .cluster_type =3D cluster_type, + .qiov =3D qiov, + .file_cluster_offset =3D file_cluster_offset, + .offset =3D offset, + .bytes =3D bytes, + .qiov_offset =3D qiov_offset, + .l2meta =3D l2meta, + }; + + trace_qcow2_add_task(qemu_coroutine_self(), bs, pool, + func =3D=3D qcow2_co_preadv_task_entry ? "read" := "write", + cluster_type, file_cluster_offset, offset, bytes, + qiov, qiov_offset); + + if (!pool) { + return func(&task->task); + } + + aio_task_pool_start_task(pool, &task->task); + + return 0; +} + static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs, QCow2ClusterType cluster_type, uint64_t file_cluster_offset, @@ -2066,18 +2121,28 @@ static coroutine_fn int qcow2_co_preadv_task(BlockD= riverState *bs, g_assert_not_reached(); } =20 +static coroutine_fn int qcow2_co_preadv_task_entry(AioTask *task) +{ + Qcow2AioTask *t =3D container_of(task, Qcow2AioTask, task); + + assert(!t->l2meta); + + return qcow2_co_preadv_task(t->bs, t->cluster_type, t->file_cluster_of= fset, + t->offset, t->bytes, t->qiov, t->qiov_offs= et); +} + static coroutine_fn int qcow2_co_preadv_part(BlockDriverState *bs, uint64_t offset, uint64_t byt= es, QEMUIOVector *qiov, size_t qiov_offset, int flags) { BDRVQcow2State *s =3D bs->opaque; - int ret; + int ret =3D 0; unsigned int cur_bytes; /* number of bytes in current iteration */ uint64_t cluster_offset =3D 0; + AioTaskPool *aio =3D NULL; =20 - while (bytes !=3D 0) { - + while (bytes !=3D 0 && aio_task_pool_status(aio) =3D=3D 0) { /* prepare next request */ cur_bytes =3D MIN(bytes, INT_MAX); if (s->crypto) { @@ -2089,7 +2154,7 @@ static coroutine_fn int qcow2_co_preadv_part(BlockDri= verState *bs, ret =3D qcow2_get_cluster_offset(bs, offset, &cur_bytes, &cluster_= offset); qemu_co_mutex_unlock(&s->lock); if (ret < 0) { - return ret; + goto out; } =20 if (ret =3D=3D QCOW2_CLUSTER_ZERO_PLAIN || @@ -2098,11 +2163,14 @@ static coroutine_fn int qcow2_co_preadv_part(BlockD= riverState *bs, { qemu_iovec_memset(qiov, qiov_offset, 0, cur_bytes); } else { - ret =3D qcow2_co_preadv_task(bs, ret, - cluster_offset, offset, cur_bytes, - qiov, qiov_offset); + if (!aio && cur_bytes !=3D bytes) { + aio =3D aio_task_pool_new(QCOW2_MAX_WORKERS); + } + ret =3D qcow2_add_task(bs, aio, qcow2_co_preadv_task_entry, re= t, + cluster_offset, offset, cur_bytes, + qiov, qiov_offset, NULL); if (ret < 0) { - return ret; + goto out; } } =20 @@ -2111,7 +2179,16 @@ static coroutine_fn int qcow2_co_preadv_part(BlockDr= iverState *bs, qiov_offset +=3D cur_bytes; } =20 - return 0; +out: + if (aio) { + aio_task_pool_wait_all(aio); + if (ret =3D=3D 0) { + ret =3D aio_task_pool_status(aio); + } + g_free(aio); + } + + return ret; } =20 /* Check if it's possible to merge a write request with the writing of @@ -2315,6 +2392,17 @@ out_locked: return ret; } =20 +static coroutine_fn int qcow2_co_pwritev_task_entry(AioTask *task) +{ + Qcow2AioTask *t =3D container_of(task, Qcow2AioTask, task); + + assert(!t->cluster_type); + + return qcow2_co_pwritev_task(t->bs, t->file_cluster_offset, + t->offset, t->bytes, t->qiov, t->qiov_off= set, + t->l2meta); +} + static coroutine_fn int qcow2_co_pwritev_part( BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, size_t qiov_offset, int flags) @@ -2325,10 +2413,11 @@ static coroutine_fn int qcow2_co_pwritev_part( unsigned int cur_bytes; /* number of sectors in current iteration */ uint64_t cluster_offset; QCowL2Meta *l2meta =3D NULL; + AioTaskPool *aio =3D NULL; =20 trace_qcow2_writev_start_req(qemu_coroutine_self(), offset, bytes); =20 - while (bytes !=3D 0) { + while (bytes !=3D 0 && aio_task_pool_status(aio) =3D=3D 0) { =20 l2meta =3D NULL; =20 @@ -2360,8 +2449,12 @@ static coroutine_fn int qcow2_co_pwritev_part( =20 qemu_co_mutex_unlock(&s->lock); =20 - ret =3D qcow2_co_pwritev_task(bs, cluster_offset, offset, cur_byte= s, - qiov, qiov_offset, l2meta); + if (!aio && cur_bytes !=3D bytes) { + aio =3D aio_task_pool_new(QCOW2_MAX_WORKERS); + } + ret =3D qcow2_add_task(bs, aio, qcow2_co_pwritev_task_entry, 0, + cluster_offset, offset, cur_bytes, + qiov, qiov_offset, l2meta); l2meta =3D NULL; /* l2meta is consumed by qcow2_co_pwritev_task() = */ if (ret < 0) { goto fail_nometa; @@ -2382,6 +2475,14 @@ out_locked: qemu_co_mutex_unlock(&s->lock); =20 fail_nometa: + if (aio) { + aio_task_pool_wait_all(aio); + if (ret =3D=3D 0) { + ret =3D aio_task_pool_status(aio); + } + g_free(aio); + } + trace_qcow2_writev_done_req(qemu_coroutine_self(), ret); =20 return ret; diff --git a/block/trace-events b/block/trace-events index d724df0117..7f51550ba3 100644 --- a/block/trace-events +++ b/block/trace-events @@ -61,6 +61,7 @@ file_paio_submit(void *acb, void *opaque, int64_t offset,= int count, int type) " file_copy_file_range(void *bs, int src, int64_t src_off, int dst, int64_t = dst_off, int64_t bytes, int flags, int64_t ret) "bs %p src_fd %d offset %"P= RIu64" dst_fd %d offset %"PRIu64" bytes %"PRIu64" flags %d ret %"PRId64 =20 # qcow2.c +qcow2_add_task(void *co, void *bs, void *pool, const char *action, int clu= ster_type, uint64_t file_cluster_offset, uint64_t offset, uint64_t bytes, v= oid *qiov, size_t qiov_offset) "co %p bs %p pool %p: %s: cluster_type %d fi= le_cluster_offset %" PRIu64 " offset %" PRIu64 " bytes %" PRIu64 " qiov %p = qiov_offset %zu" qcow2_writev_start_req(void *co, int64_t offset, int bytes) "co %p offset = 0x%" PRIx64 " bytes %d" qcow2_writev_done_req(void *co, int ret) "co %p ret %d" qcow2_writev_start_part(void *co) "co %p" --=20 2.18.0