From nobody Tue Feb 10 10:07:48 2026 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.zohomail.com; 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 1541153044248252.13239831952387; Fri, 2 Nov 2018 03:04:04 -0700 (PDT) Received: from localhost ([::1]:50541 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gIWJ5-0004f0-2O for importer@patchew.org; Fri, 02 Nov 2018 06:03:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34291) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gIWH7-0002Ws-Fb for qemu-devel@nongnu.org; Fri, 02 Nov 2018 06:01:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gIWH1-0000Yh-I3 for qemu-devel@nongnu.org; Fri, 02 Nov 2018 06:01:57 -0400 Received: from smtp03.citrix.com ([162.221.156.55]:1102) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gIWGv-00006r-DM; Fri, 02 Nov 2018 06:01:45 -0400 X-IronPort-AV: E=Sophos;i="5.54,455,1534809600"; d="scan'208";a="69460964" From: Tim Smith To: , , Date: Fri, 2 Nov 2018 10:01:09 +0000 Message-ID: <154115286959.11300.498371710893672725.stgit@dhcp-3-135.uk.xensource.com> In-Reply-To: <154115285434.11300.8459925605672823399.stgit@dhcp-3-135.uk.xensource.com> References: <154115285434.11300.8459925605672823399.stgit@dhcp-3-135.uk.xensource.com> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 162.221.156.55 Subject: [Qemu-devel] [PATCH 3/3] Avoid repeated memory allocation in xen_disk 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: Anthony Perard , Kevin Wolf , Paul Durrant , Stefano Stabellini , Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" xen_disk currently allocates memory to hold the data for each ioreq as that ioreq is used, and frees it afterwards. Because it requires page-aligned blocks, this interacts poorly with non-page-aligned allocations and balloons the heap. Instead, allocate the maximum possible requirement, which is BLKIF_MAX_SEGMENTS_PER_REQUEST pages (currently 11 pages) when the ioreq is created, and keep that allocation until it is destroyed. Since the ioreqs themselves are re-used via a free list, this should actually improve memory usage. Signed-off-by: Tim Smith Acked-by: Anthony PERARD Reviewed-by: Paul Durrant --- hw/block/xen_disk.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c index b506e23868..faaeefba29 100644 --- a/hw/block/xen_disk.c +++ b/hw/block/xen_disk.c @@ -112,7 +112,6 @@ static void ioreq_reset(struct ioreq *ioreq) memset(&ioreq->req, 0, sizeof(ioreq->req)); ioreq->status =3D 0; ioreq->start =3D 0; - ioreq->buf =3D NULL; ioreq->size =3D 0; ioreq->presync =3D 0; =20 @@ -137,6 +136,11 @@ static struct ioreq *ioreq_start(struct XenBlkDev *blk= dev) /* allocate new struct */ ioreq =3D g_malloc0(sizeof(*ioreq)); ioreq->blkdev =3D blkdev; + /* We cannot need more pages per ioreq than this, and we do re-use + * ioreqs, so allocate the memory once here, to be freed in + * blk_free() when the ioreq is freed. */ + ioreq->buf =3D qemu_memalign(XC_PAGE_SIZE, BLKIF_MAX_SEGMENTS_PER_= REQUEST + * XC_PAGE_SIZE); blkdev->requests_total++; qemu_iovec_init(&ioreq->v, 1); } else { @@ -313,14 +317,12 @@ static void qemu_aio_complete(void *opaque, int ret) if (ret =3D=3D 0) { ioreq_grant_copy(ioreq); } - qemu_vfree(ioreq->buf); break; case BLKIF_OP_WRITE: case BLKIF_OP_FLUSH_DISKCACHE: if (!ioreq->req.nr_segments) { break; } - qemu_vfree(ioreq->buf); break; default: break; @@ -392,12 +394,10 @@ static int ioreq_runio_qemu_aio(struct ioreq *ioreq) { struct XenBlkDev *blkdev =3D ioreq->blkdev; =20 - ioreq->buf =3D qemu_memalign(XC_PAGE_SIZE, ioreq->size); if (ioreq->req.nr_segments && (ioreq->req.operation =3D=3D BLKIF_OP_WRITE || ioreq->req.operation =3D=3D BLKIF_OP_FLUSH_DISKCACHE) && ioreq_grant_copy(ioreq)) { - qemu_vfree(ioreq->buf); goto err; } =20 @@ -990,6 +990,7 @@ static int blk_free(struct XenDevice *xendev) ioreq =3D QLIST_FIRST(&blkdev->freelist); QLIST_REMOVE(ioreq, list); qemu_iovec_destroy(&ioreq->v); + qemu_vfree(ioreq->buf); g_free(ioreq); } =20