kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "xuxiaoyang (C)" <xuxiaoyang2@huawei.com>
To: <linux-kernel@vger.kernel.org>, <kvm@vger.kernel.org>,
	<alex.williamson@redhat.com>
Cc: <kwankhede@nvidia.com>, <xieyingtai@huawei.com>,
	<lizhengui@huawei.com>, <xuxiaoyang2@huawei.com>
Subject: [PATCH] vfio iommu type1: Fix memory leak in vfio_iommu_type1_pin_pages
Date: Fri, 16 Oct 2020 17:35:58 +0800	[thread overview]
Message-ID: <f65f1c3c-c1bc-a44a-15ea-4cb6e43c8b4b@huawei.com> (raw)

From 099744c26513e386e707faecb3f17726e236d9bc Mon Sep 17 00:00:00 2001
From: Xiaoyang Xu <xuxiaoyang2@huawei.com>
Date: Fri, 16 Oct 2020 15:32:02 +0800
Subject: [PATCH] vfio iommu type1: Fix memory leak in
 vfio_iommu_type1_pin_pages

pfn is not added to pfn_list when vfio_add_to_pfn_list fails.
vfio_unpin_page_external will exit directly without calling
vfio_iova_put_vfio_pfn.This will lead to a memory leak.

Signed-off-by: Xiaoyang Xu <xuxiaoyang2@huawei.com>
---
 drivers/vfio/vfio_iommu_type1.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index c255a6683f31..26f518b02c81 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -640,6 +640,7 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
 	unsigned long remote_vaddr;
 	struct vfio_dma *dma;
 	bool do_accounting;
+	int unlocked;

 	if (!iommu || !user_pfn || !phys_pfn)
 		return -EINVAL;
@@ -693,7 +694,9 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,

 		ret = vfio_add_to_pfn_list(dma, iova, phys_pfn[i]);
 		if (ret) {
-			vfio_unpin_page_external(dma, iova, do_accounting);
+			unlocked = put_pfn(phys_pfn[i], dma->prot);
+			if (do_accounting)
+				vfio_lock_acct(dma, -unlocked, true);
 			goto pin_unwind;
 		}

--
2.19.1


             reply	other threads:[~2020-10-16  9:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-16  9:35 xuxiaoyang (C) [this message]
2020-10-20 16:34 ` [PATCH] vfio iommu type1: Fix memory leak in vfio_iommu_type1_pin_pages Alex Williamson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f65f1c3c-c1bc-a44a-15ea-4cb6e43c8b4b@huawei.com \
    --to=xuxiaoyang2@huawei.com \
    --cc=alex.williamson@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwankhede@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizhengui@huawei.com \
    --cc=xieyingtai@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).