Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp1055029pxu; Fri, 16 Oct 2020 02:40:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwR9+8RWATFPLhS2jTL3YfTOgPbB7GV6vxUz4IE2l7xuW+CkgJJ8rsBVoEpa8XPQ//XIXRw X-Received: by 2002:a50:d796:: with SMTP id w22mr2936388edi.123.1602841242328; Fri, 16 Oct 2020 02:40:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602841242; cv=none; d=google.com; s=arc-20160816; b=A4YdKliqfX58PEK3IbPSQrZ2wfGdD53xrBjSEvvl95G9/Jac07mUOrN1IW3rdVZ+xW VRCrK5PQYtlR9Q1xAQ78Sr+kMy653ifRUPsEvzFen+5U4vTdoADFnB1h2Eux+SvLEMnl JqwYL1LihLD4ijHXxmtCrQn6wNyDn6BEj+CkAKPlu7P2EH/mt6CMT8vDnzVbilzYSqVs SFk+zlBSk7Gu+8SpUDHFZ9RllCvtH+/YOSPXkz0t3qo0UPfSBnk9P60mClgtT+vaP8s0 oKeROwd62uUx39TKItqEKD6/+nPKJhaWIvRrCtd2uh/B4a9+uR1fravLtYkZENhT7Hla E1vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:user-agent:date:message-id:subject:from:cc:to; bh=0M6yv0Os/t0yMGoAebtvplTZyQXemm5vJTDM6nyV9xY=; b=vVAz2CEJZrerM8txd32n+KksGV7gBFYAlDmnusUWd79jHyHZj4BzAGfIRjPAAuryer IY/rnd3iJDM9Ty0MxhWrqHXIWO458wLPdCzifbInwR0YwJ94T/S9v8aRPXfvYanVDjEZ f5cZzrl8bXMhmLimUEOlvr4/Ly31Cyeea+3RhtSvPwKHAKKmIjLnwW1Gr7jc1d5YHTSt ZjFO44ex8SxvtrpdYnkff3aqSO5a2IaIcVw/5xaIElRKFhwfiAiXN/qPfJvAGbQRU4PH G4xmWOQKIIhOOpDQWuNsGimsZsNVxnpLp9gnWIDA22+a3mQ42chG0d1TQB83Kb3Wq0Pl cSqQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k16si1313420edv.269.2020.10.16.02.40.20; Fri, 16 Oct 2020 02:40:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406087AbgJPJgB (ORCPT + 99 others); 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