From nobody Wed Dec 17 05:41:37 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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1566937589; cv=none; d=zoho.com; s=zohoarc; b=n2Iv4Ns+rcSi0dnAQJqCs1OZgVL33nEBhEXXp8P33tAtl8St/98mbhUogQ67ARbV9WCLbnrNph4zdcxVb/cMlKYJXNpRVWPlmiJ234+vnsVRjCxwmzl6Pi7m+z/BOAEvWkBryRFI6r3Josbso1F4RDkzK0l+LG0Vy7l0DAVIVdI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566937589; 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:ARC-Authentication-Results; bh=9rMpZ4JyKEu65qZeB9dL2c4wFx2MrqWSFUImvWZWrts=; b=bBBgrg9djNcuCOdTAtrGR9xuSgSM6USBrGHpdqqIfOS3T8l+I+TfTZ40M/ROqPfqYq5CdmEYASQagHA26AQCfT7p3wtjk1jkJluJxiIW7PaVuGIA8GFxnUAudLhWwfBJbDSbpfBmdpynALIacWWQw9ok2TQEDhZtphzrNZe1+dA= 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 1566937589138197.10617155640296; Tue, 27 Aug 2019 13:26:29 -0700 (PDT) Received: from localhost ([::1]:58178 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i2i2r-00008V-Sx for importer@patchew.org; Tue, 27 Aug 2019 16:26:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51422) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i2huU-0006JF-Ex for qemu-devel@nongnu.org; Tue, 27 Aug 2019 16:17:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i2huT-0000rn-CZ for qemu-devel@nongnu.org; Tue, 27 Aug 2019 16:17:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49150) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i2huR-0000qO-2q; Tue, 27 Aug 2019 16:17:43 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3DCB57BDA9; Tue, 27 Aug 2019 20:17:40 +0000 (UTC) Received: from localhost (ovpn-116-148.ams2.redhat.com [10.36.116.148]) by smtp.corp.redhat.com (Postfix) with ESMTP id 74D535D9CC; Tue, 27 Aug 2019 20:17:34 +0000 (UTC) From: Stefan Hajnoczi To: qemu-devel@nongnu.org Date: Tue, 27 Aug 2019 21:16:33 +0100 Message-Id: <20190827201639.30368-7-stefanha@redhat.com> In-Reply-To: <20190827201639.30368-1-stefanha@redhat.com> References: <20190827201639.30368-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 27 Aug 2019 20:17:40 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 06/12] block/io: bdrv_co_do_copy_on_readv: lazy allocation 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: Fam Zheng , Kevin Wolf , Vladimir Sementsov-Ogievskiy , qemu-block@nongnu.org, Peter Maydell , Max Reitz , Stefan Hajnoczi , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Vladimir Sementsov-Ogievskiy Allocate bounce_buffer only if it is really needed. Also, sub-optimize allocation size (why not?). Signed-off-by: Vladimir Sementsov-Ogievskiy Acked-by: Stefan Hajnoczi Message-id: 20190604161514.262241-7-vsementsov@virtuozzo.com Message-Id: <20190604161514.262241-7-vsementsov@virtuozzo.com> Signed-off-by: Stefan Hajnoczi --- block/io.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/block/io.c b/block/io.c index 5817bb9405..4c7a7ac7b1 100644 --- a/block/io.c +++ b/block/io.c @@ -1236,7 +1236,7 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(Bdrv= Child *child, * modifying the image file. This is critical for zero-copy guest I/O * where anything might happen inside guest memory. */ - void *bounce_buffer; + void *bounce_buffer =3D NULL; =20 BlockDriver *drv =3D bs->drv; int64_t cluster_offset; @@ -1271,14 +1271,6 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(Bdr= vChild *child, trace_bdrv_co_do_copy_on_readv(bs, offset, bytes, cluster_offset, cluster_bytes); =20 - bounce_buffer =3D qemu_try_blockalign(bs, - MIN(MIN(max_transfer, cluster_byte= s), - MAX_BOUNCE_BUFFER)); - if (bounce_buffer =3D=3D NULL) { - ret =3D -ENOMEM; - goto err; - } - while (cluster_bytes) { int64_t pnum; =20 @@ -1305,6 +1297,17 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(Bdr= vChild *child, =20 /* Must copy-on-read; use the bounce buffer */ pnum =3D MIN(pnum, MAX_BOUNCE_BUFFER); + if (!bounce_buffer) { + int64_t max_we_need =3D MAX(pnum, cluster_bytes - pnum); + int64_t max_allowed =3D MIN(max_transfer, MAX_BOUNCE_BUFFE= R); + int64_t bounce_buffer_len =3D MIN(max_we_need, max_allowed= ); + + bounce_buffer =3D qemu_try_blockalign(bs, bounce_buffer_le= n); + if (!bounce_buffer) { + ret =3D -ENOMEM; + goto err; + } + } qemu_iovec_init_buf(&local_qiov, bounce_buffer, pnum); =20 ret =3D bdrv_driver_preadv(bs, cluster_offset, pnum, --=20 2.21.0