From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.2 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,HK_RANDOM_FROM,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31A1FC433DF for ; Fri, 16 Oct 2020 09:36:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C6CDF2071A for ; Fri, 16 Oct 2020 09:36:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406098AbgJPJgB (ORCPT ); Fri, 16 Oct 2020 05:36:01 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:3984 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2406063AbgJPJgB (ORCPT ); Fri, 16 Oct 2020 05:36:01 -0400 Received: from dggeme753-chm.china.huawei.com (unknown [172.30.72.56]) by Forcepoint Email with ESMTP id 746B09A038579EA4BC6A; Fri, 16 Oct 2020 17:35:59 +0800 (CST) Received: from [10.174.184.120] (10.174.184.120) by dggeme753-chm.china.huawei.com (10.3.19.99) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1913.5; Fri, 16 Oct 2020 17:35:58 +0800 To: , , CC: , , , From: "xuxiaoyang (C)" Subject: [PATCH] vfio iommu type1: Fix memory leak in vfio_iommu_type1_pin_pages Message-ID: Date: Fri, 16 Oct 2020 17:35:58 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.3.2 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Language: en-GB Content-Transfer-Encoding: 7bit X-Originating-IP: [10.174.184.120] X-ClientProxiedBy: dggeme707-chm.china.huawei.com (10.1.199.103) To dggeme753-chm.china.huawei.com (10.3.19.99) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org >From 099744c26513e386e707faecb3f17726e236d9bc Mon Sep 17 00:00:00 2001 From: Xiaoyang Xu 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 --- 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