From nobody Fri May 3 13:03:42 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.zohomail.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 1544544747607966.1705836529959; Tue, 11 Dec 2018 08:12:27 -0800 (PST) Received: from localhost ([::1]:39670 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWke2-0007HX-94 for importer@patchew.org; Tue, 11 Dec 2018 11:12:26 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55470) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWkUh-0008Vk-TZ for qemu-devel@nongnu.org; Tue, 11 Dec 2018 11:02:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gWkUc-0007eG-W7 for qemu-devel@nongnu.org; Tue, 11 Dec 2018 11:02:47 -0500 Received: from mo6-p02-ob.smtp.rzone.de ([2a01:238:20a:202:5302::2]:19233) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gWkUc-0007cp-Dv; Tue, 11 Dec 2018 11:02:42 -0500 Received: from sender by smtp.strato.de (RZmta 44.7 AUTH) with ESMTPSA id i02767uBBG2Q2eY (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp521r1 with 521 ECDH bits, eq. 15360 bits RSA)) (Client did not present a certificate); Tue, 11 Dec 2018 17:02:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1544544159; s=strato-dkim-0002; d=aepfle.de; h=Message-Id:Date:Subject:Cc:To:From:X-RZG-CLASS-ID:X-RZG-AUTH:From: Subject:Sender; bh=H8oX2FNrorO/m1CvGFfvnTNC6zgKbZqko+mzLtXYL2M=; b=k6yFGB/NWgM9zqjVqV6KQBWv7+spxBIYnq0rl/1cE71B8rtTABwVXxig++MCGlEC4k Y4eia7SMR93BB/CH2Y6ef0WDDHDKOek2Da/S+PaYUDb5ZvOak+JZaw4H9KN4Gf5btZkG agY4ILNEUeUNg6xngzqKi2BgXzU82kkMtECpmtdZIbBuBvT/PFLckxCZEBZJcr7lzN2U oBWmxu3HFNHYg6tlng5egGidEk0YhRriwdZvwg/5H+mkyHqyB5HchAT3pSfM8VyJOcQG sXkUCkBTGnGaGLU3wz9d2ug53HfRTOv70m+tFZD6XZR8q6muFGJaYx9B8du0Nhy2Z00+ sXoA== X-RZG-AUTH: ":P2EQZWCpfu+qG7CngxMFH1J+3q8wa/QXkBR9MXjAuzpOGUivpc0WAr0t3Sj8140FKdfYth0=" X-RZG-CLASS-ID: mo00 From: Olaf Hering To: qemu-devel@nongnu.org Date: Tue, 11 Dec 2018 17:02:24 +0100 Message-Id: <20181211160224.22181-1-olaf@aepfle.de> X-Mailer: git-send-email 2.16.4 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 2a01:238:20a:202:5302::2 Subject: [Qemu-devel] [PATCH v1] xen_disk: fix memory leak 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 , Olaf Hering , Stefano Stabellini , "open list:Block layer core" , Max Reitz , Anthony Perard , "open list:X86" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" There are some code paths that clobber ioreq->buf, which leads to a huge memory leak after a few hours of runtime. One code path is qemu_aio_complete, which might be called recursive. Another one is ioreq_reset, which might clobber ioreq->buf as well. Add wrappers to free ioreq->buf before reassignment. Signed-off-by: Olaf Hering --- hw/block/xen_disk.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c index 36eff94f84..e15eefe625 100644 --- a/hw/block/xen_disk.c +++ b/hw/block/xen_disk.c @@ -103,12 +103,24 @@ struct XenBlkDev { =20 /* ------------------------------------------------------------- */ =20 +static void ioreq_buf_alloc(struct ioreq *ioreq, size_t alignment) +{ + if (ioreq->buf) + qemu_vfree(ioreq->buf); + ioreq->buf =3D qemu_memalign(XC_PAGE_SIZE, ioreq->size); +} +static void ioreq_buf_free(struct ioreq *ioreq) +{ + if (ioreq->buf) + qemu_vfree(ioreq->buf); + ioreq->buf =3D NULL; +} static void ioreq_reset(struct ioreq *ioreq) { + ioreq_buf_free(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 @@ -315,14 +327,14 @@ static void qemu_aio_complete(void *opaque, int ret) if (ret =3D=3D 0) { ioreq_grant_copy(ioreq); } - qemu_vfree(ioreq->buf); + ioreq_buf_free(ioreq); break; case BLKIF_OP_WRITE: case BLKIF_OP_FLUSH_DISKCACHE: if (!ioreq->req.nr_segments) { break; } - qemu_vfree(ioreq->buf); + ioreq_buf_free(ioreq); break; default: break; @@ -390,12 +402,12 @@ 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); + ioreq_buf_alloc(ioreq, XC_PAGE_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); + ioreq_buf_free(ioreq); goto err; } =20