From patchwork Tue Dec 11 16:02:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olaf Hering X-Patchwork-Id: 10724097 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 577F71751 for ; Tue, 11 Dec 2018 16:11:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 470502B410 for ; Tue, 11 Dec 2018 16:11:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3A4862B421; Tue, 11 Dec 2018 16:11:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CD7552B410 for ; Tue, 11 Dec 2018 16:11:52 +0000 (UTC) Received: from localhost ([::1]:39656 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gWkdU-00066z-2P for patchwork-qemu-devel@patchwork.kernel.org; Tue, 11 Dec 2018 11:11:52 -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 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 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) 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 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 { /* ------------------------------------------------------------- */ +static void ioreq_buf_alloc(struct ioreq *ioreq, size_t alignment) +{ + if (ioreq->buf) + qemu_vfree(ioreq->buf); + ioreq->buf = qemu_memalign(XC_PAGE_SIZE, ioreq->size); +} +static void ioreq_buf_free(struct ioreq *ioreq) +{ + if (ioreq->buf) + qemu_vfree(ioreq->buf); + ioreq->buf = NULL; +} static void ioreq_reset(struct ioreq *ioreq) { + ioreq_buf_free(ioreq); memset(&ioreq->req, 0, sizeof(ioreq->req)); ioreq->status = 0; ioreq->start = 0; - ioreq->buf = NULL; ioreq->size = 0; ioreq->presync = 0; @@ -315,14 +327,14 @@ static void qemu_aio_complete(void *opaque, int ret) if (ret == 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 = ioreq->blkdev; - ioreq->buf = qemu_memalign(XC_PAGE_SIZE, ioreq->size); + ioreq_buf_alloc(ioreq, XC_PAGE_SIZE); if (ioreq->req.nr_segments && (ioreq->req.operation == BLKIF_OP_WRITE || ioreq->req.operation == BLKIF_OP_FLUSH_DISKCACHE) && ioreq_grant_copy(ioreq)) { - qemu_vfree(ioreq->buf); + ioreq_buf_free(ioreq); goto err; }