[PATCH] vfio iommu type1: Fix memory leak in vfio_iommu_type1_pin_pages

From: Xiaoyang Xu
Date: Fri Oct 16 2020 - 03:32:02 EST


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@xxxxxxxxxx>
---
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