From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ECD8829A1 for ; Fri, 18 Nov 2022 11:54:36 +0000 (UTC) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C0F2923A; Fri, 18 Nov 2022 03:54:36 -0800 (PST) Received: from [10.57.70.144] (unknown [10.57.70.144]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B16E83F587; Fri, 18 Nov 2022 03:54:29 -0800 (PST) Message-ID: <75fa75cf-a4ab-d7d8-e3ab-adb402600f7e@arm.com> Date: Fri, 18 Nov 2022 11:54:15 +0000 Precedence: bulk X-Mailing-List: iommu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH 2/2] iommu: fix smmu initialization memory leak problem To: liulongfang , Will Deacon Cc: linux-arm-kernel@lists.infradead.org, iommu@lists.linux.dev References: <20221021035147.15292-1-liulongfang@huawei.com> <20221021035147.15292-3-liulongfang@huawei.com> <20221114180821.GC31476@willie-the-truck> <26e8c338-7e4b-b35e-c68a-78a7cbdf2d45@huawei.com> Content-Language: en-GB From: Robin Murphy In-Reply-To: <26e8c338-7e4b-b35e-c68a-78a7cbdf2d45@huawei.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 2022-11-18 09:28, liulongfang wrote: > On 2022/11/15 2:08, Will Deacon Wrote: >> On Fri, Oct 21, 2022 at 11:51:47AM +0800, Longfang Liu wrote: >>> When iommu_device_register() in arm_smmu_device_probe() fails, >>> in addition to sysfs needs to be deleted, device should also >>> be disabled, and the memory of iopf needs to be released to >>> prevent memory leak of iopf. >>> >>> Signed-off-by: Longfang Liu >>> --- >>> drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++-- >>> 1 file changed, 7 insertions(+), 2 deletions(-) >>> >>> diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c >>> index a1db07bed6a9..c70defb0c866 100644 >>> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c >>> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c >>> @@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev) >>> ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev); >>> if (ret) { >>> dev_err(dev, "Failed to register iommu\n"); >>> - iommu_device_sysfs_remove(&smmu->iommu); >>> - return ret; >>> + goto err_sysfs_remove; >>> } >>> >>> return 0; >>> + >>> +err_sysfs_remove: >>> + iommu_device_sysfs_remove(&smmu->iommu); >>> + arm_smmu_device_disable(smmu); >>> + iopf_queue_free(smmu->evtq.iopf); >>> + return ret; >> >> Doesn't this miss the cases where iommu_device_sysfs_add() or >> arm_smmu_device_reset() fail? >> >> We'd probably be better off using something like devres_alloc() to track >> the iopf queue here. >> > This is actually not a problem found by the test, but a problem found > by the code logic analysis. When an error exits, the memory allocated > by the iopf queue is not released during the entire exit process. Sure, but the point is that there are at least 5 points of failure after iopf_queue_alloc() succeeds, which could result in an early exit from probe. This patch only affects the last one of those, so the theoretical problem it claims to fix still exists just as much as before. Robin. 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 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ABE86C4332F for ; Fri, 18 Nov 2022 11:55:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fTFvTGlHep2c53ngx/8Xsijqm+gmAKYO0Mpp7DJpLAc=; b=BElrmAAZ0dCbk1 oS2lfS3wxaKyPh4+iVCNS0u8ve/Vk/VtfNE7jGukMYP4Q76oDjYKwhEjvV8veBFbgOf0ssZpX2jBn ye4DQYMii5aDyK78V1pi5EQI9QD//VwnUYQxn9cdtwWXW0P+A3OHw8ZO8fUPGsz9Ejap2oDaz83ZJ i/QGN+OnHYVJ3W0c7vBx416Be4i4EYSFYfoabJBIUoG0Fd4NEvuh4GivWNbxd/6bPt7xBV9t0yoWm mddvoWKNHOLxxP9JhG3gyBPi1taKqfVCOEhM++OQ7wgegwu61wBYfIha25bKbz7jk0yoWTpX4bJbq 9Dp3Hgd2GfI9B1l3QvfA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ovzx8-003mRf-Hv; Fri, 18 Nov 2022 11:54:38 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ovzx5-003mQV-4Y for linux-arm-kernel@lists.infradead.org; Fri, 18 Nov 2022 11:54:36 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C0F2923A; Fri, 18 Nov 2022 03:54:36 -0800 (PST) Received: from [10.57.70.144] (unknown [10.57.70.144]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B16E83F587; Fri, 18 Nov 2022 03:54:29 -0800 (PST) Message-ID: <75fa75cf-a4ab-d7d8-e3ab-adb402600f7e@arm.com> Date: Fri, 18 Nov 2022 11:54:15 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH 2/2] iommu: fix smmu initialization memory leak problem To: liulongfang , Will Deacon Cc: linux-arm-kernel@lists.infradead.org, iommu@lists.linux.dev References: <20221021035147.15292-1-liulongfang@huawei.com> <20221021035147.15292-3-liulongfang@huawei.com> <20221114180821.GC31476@willie-the-truck> <26e8c338-7e4b-b35e-c68a-78a7cbdf2d45@huawei.com> Content-Language: en-GB From: Robin Murphy In-Reply-To: <26e8c338-7e4b-b35e-c68a-78a7cbdf2d45@huawei.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221118_035435_252478_181BD972 X-CRM114-Status: GOOD ( 15.95 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 2022-11-18 09:28, liulongfang wrote: > On 2022/11/15 2:08, Will Deacon Wrote: >> On Fri, Oct 21, 2022 at 11:51:47AM +0800, Longfang Liu wrote: >>> When iommu_device_register() in arm_smmu_device_probe() fails, >>> in addition to sysfs needs to be deleted, device should also >>> be disabled, and the memory of iopf needs to be released to >>> prevent memory leak of iopf. >>> >>> Signed-off-by: Longfang Liu >>> --- >>> drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +++++++-- >>> 1 file changed, 7 insertions(+), 2 deletions(-) >>> >>> diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c >>> index a1db07bed6a9..c70defb0c866 100644 >>> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c >>> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c >>> @@ -3816,11 +3816,16 @@ static int arm_smmu_device_probe(struct platform_device *pdev) >>> ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev); >>> if (ret) { >>> dev_err(dev, "Failed to register iommu\n"); >>> - iommu_device_sysfs_remove(&smmu->iommu); >>> - return ret; >>> + goto err_sysfs_remove; >>> } >>> >>> return 0; >>> + >>> +err_sysfs_remove: >>> + iommu_device_sysfs_remove(&smmu->iommu); >>> + arm_smmu_device_disable(smmu); >>> + iopf_queue_free(smmu->evtq.iopf); >>> + return ret; >> >> Doesn't this miss the cases where iommu_device_sysfs_add() or >> arm_smmu_device_reset() fail? >> >> We'd probably be better off using something like devres_alloc() to track >> the iopf queue here. >> > This is actually not a problem found by the test, but a problem found > by the code logic analysis. When an error exits, the memory allocated > by the iopf queue is not released during the entire exit process. Sure, but the point is that there are at least 5 points of failure after iopf_queue_alloc() succeeds, which could result in an early exit from probe. This patch only affects the last one of those, so the theoretical problem it claims to fix still exists just as much as before. Robin. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel