From patchwork Thu Jan 18 15:41:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yongji Xie X-Patchwork-Id: 10173595 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id ABAB6602DC for ; Thu, 18 Jan 2018 15:45:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 927AF2842A for ; Thu, 18 Jan 2018 15:45:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8427F28497; Thu, 18 Jan 2018 15:45:28 +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=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 29C402842A for ; Thu, 18 Jan 2018 15:45:28 +0000 (UTC) Received: from localhost ([::1]:37734 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ecCNb-0007Uk-FR for patchwork-qemu-devel@patchwork.kernel.org; Thu, 18 Jan 2018 10:45:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38318) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ecCMN-0006TU-1k for qemu-devel@nongnu.org; Thu, 18 Jan 2018 10:44:11 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ecCMJ-00049h-Vq for qemu-devel@nongnu.org; Thu, 18 Jan 2018 10:44:11 -0500 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]:37945) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ecCMJ-000489-Pu for qemu-devel@nongnu.org; Thu, 18 Jan 2018 10:44:07 -0500 Received: by mail-pf0-x243.google.com with SMTP id k19so14892446pfj.5 for ; Thu, 18 Jan 2018 07:44:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=LY3c/hkOzpCRzrJ8blTMFYF7pt1zaoGEgREedbfAkas=; b=O7S42SR99UF51OGupAuHkxcGQsUpnuk/TMB+BqJ7ehOa1BNGXcyPJidM9TT12aDJvu bkou/35KLdZP4rLYZLo3o6Hv7Kocn0awHIDEmz2B+hNq/7cFgTVEeckVf8yOECZOzcl6 csYTT0NXiad2F07rBcv/bEjHI0yFA3w4TTeFNA6L3sO6tmVK9Xjwh0J85BaGefWW9o1O fYrRQWzDOcZo8N26PBSXyX15nHG3a4nMgWJyCh2I+jKhMseCGzweLqBrRb/Q3zcyXZ8t qsFkpH6ms5NliJcp7VL7AFZBooB/El9inGQuNQG0Ae/Y8W86sjrBEZkxu6kDch73oEbs bJRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=LY3c/hkOzpCRzrJ8blTMFYF7pt1zaoGEgREedbfAkas=; b=BQICrfL/xQFqbL20yvYxkaMaWB6dEauvLuyQjc+O24VtQ7ruOnutUJhps6OYifDqHp 2XJmlrNayvhCqa7fzvwbMzEviADahM1KMnpOwsrEIemmSV1p1n9FhPqsUpqIWlO6RTcP suLAadUrdOQ9FQeIZCPbNjal74cfBopiLHLtZ1qWDnbi5C5iZhd5gCVMX4lQ1psytUQ0 Y5drq27VNW917pyzQNChxhdW6x9PvKKFAhOnGxtdvtIungmGSvYerlYCnn/cPWMJgqaW OXbAHDoKzogoJ1VQ8fOYgb7Z0gqKnfDbUeq5cRDGoVmkX30LuuwjdDBcD8Mz0jbabukE p8vw== X-Gm-Message-State: AKwxytcZjMUmW4wlDcm3dBOWxbsgJdTJi2xlQLK4yn5tA1JDebZdBHvJ vRWmoPckc2Yqd+B6bRXiLiUhFWrm X-Google-Smtp-Source: ACJfBovoF97fHZOsqeKv1jm2dheq+II8rBtqE9B96cQXjrEs/s3pmCfpM69gv5N0OxKgbxVRPY3wQQ== X-Received: by 10.98.231.11 with SMTP id s11mr16405623pfh.174.1516290246548; Thu, 18 Jan 2018 07:44:06 -0800 (PST) Received: from localhost ([58.35.38.61]) by smtp.gmail.com with ESMTPSA id b11sm7823540pgu.25.2018.01.18.07.44.05 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 18 Jan 2018 07:44:05 -0800 (PST) From: Yongji Xie X-Google-Original-From: Yongji Xie To: mst@redhat.com, marcandre.lureau@redhat.com Date: Thu, 18 Jan 2018 23:41:56 +0800 Message-Id: <1516290116-4366-1-git-send-email-xieyongji@baidu.com> X-Mailer: git-send-email 1.7.9.5 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::243 Subject: [Qemu-devel] [PATCH] libvhost-user: Fix resource 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: qemu-devel@nongnu.org, maxime.coquelin@redhat.com, dgilbert@redhat.com, f4bug@amsat.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Free the mmaped memory when we need to mmap new memory space on vu_set_mem_table_exec() and vu_set_log_base_exec() to avoid memory leak. Also close the corresponding fd after mmap() on vu_set_log_base_exec() to avoid fd leak. Signed-off-by: Yongji Xie Reviewed-by: Marc-André Lureau Reviewed-by: Maxime Coquelin --- contrib/libvhost-user/libvhost-user.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/libvhost-user.c index f409bd3..cadced7 100644 --- a/contrib/libvhost-user/libvhost-user.c +++ b/contrib/libvhost-user/libvhost-user.c @@ -405,6 +405,15 @@ vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg) { int i; VhostUserMemory *memory = &vmsg->payload.memory; + + for (i = 0; i < dev->nregions; i++) { + VuDevRegion *r = &dev->regions[i]; + void *m = (void *) (uintptr_t) r->mmap_addr; + + if (m) { + munmap(m, r->size + r->mmap_offset); + } + } dev->nregions = memory->nregions; DPRINT("Nregions: %d\n", memory->nregions); @@ -470,9 +479,14 @@ vu_set_log_base_exec(VuDev *dev, VhostUserMsg *vmsg) rc = mmap(0, log_mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, log_mmap_offset); + close(fd); if (rc == MAP_FAILED) { perror("log mmap error"); } + + if (dev->log_table) { + munmap(dev->log_table, dev->log_size); + } dev->log_table = rc; dev->log_size = log_mmap_size;