From patchwork Tue Oct 27 13:49:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 306926 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=-12.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable 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 4369DC388F9 for ; Tue, 27 Oct 2020 18:14:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E8818207E8 for ; Tue, 27 Oct 2020 18:14:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603822479; bh=5+ZEnv38tX0bSTtZFBcShu/N2igvDJs0/DWtyflHcmg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=GluI2l5bbu7mM4cP8vbJRXsa2u5hceVLgD5NqOYLcZDXJiJ6ba7eUtzVGQnl0luB1 sFSEQ6XyvjMJ2OhflpgWl2//1xO/9ryLw8Y/qr7JP7jNOKrWrZ4eWq7OMwI+VbdvlN N8JhjG9uWOHBSoTYzH9p0EFhk7ro2n56NZcmAGkM= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756434AbgJ0ONQ (ORCPT ); Tue, 27 Oct 2020 10:13:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:34586 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2507323AbgJ0ONO (ORCPT ); Tue, 27 Oct 2020 10:13:14 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B97962072D; Tue, 27 Oct 2020 14:13:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603807994; bh=5+ZEnv38tX0bSTtZFBcShu/N2igvDJs0/DWtyflHcmg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LRaCi/CGVorR69MnI8GHFnyXwzzvHb+yWXe1zQHHO/G57s1mty8K31DfZMUfSg9lj G/SO7FiLq3M6ciajO8xIRPQzJa8Bl1Ng37ggqdU7/wdrYoIFl2qNTd0wtpeYQn3rwY QpkDWDrds0q+sK27CAi6bhDt0ktPihd6Eu9q2bz0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xiaoyang Xu , Alex Williamson , Sasha Levin Subject: [PATCH 4.14 118/191] vfio iommu type1: Fix memory leak in vfio_iommu_type1_pin_pages Date: Tue, 27 Oct 2020 14:49:33 +0100 Message-Id: <20201027134915.373064109@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027134909.701581493@linuxfoundation.org> References: <20201027134909.701581493@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Xiaoyang Xu [ Upstream commit 2e6cfd496f5b57034cf2aec738799571b5a52124 ] 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. Fixes: a54eb55045ae ("vfio iommu type1: Add support for mediated devices") Signed-off-by: Xiaoyang Xu [aw: simplified logic, add Fixes] Signed-off-by: Alex Williamson Signed-off-by: Sasha Levin --- drivers/vfio/vfio_iommu_type1.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 9c8ed9d7f9aa5..bfbe5236239bd 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -629,7 +629,8 @@ 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); + if (put_pfn(phys_pfn[i], dma->prot) && do_accounting) + vfio_lock_acct(dma, -1, true); goto pin_unwind; } }