linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] iommu/arm-smmu: Fix arm_smmu_device refcount leak in address translation
@ 2021-06-10  2:49 Xiyu Yang
  2021-06-10 22:28 ` Rob Clark
  2021-06-11 16:15 ` Will Deacon
  0 siblings, 2 replies; 3+ messages in thread
From: Xiyu Yang @ 2021-06-10  2:49 UTC (permalink / raw)
  To: Will Deacon, Robin Murphy, Joerg Roedel, Rob Clark, Jon Hunter,
	Krishna Reddy, Jordan Crouse, Sai Prakash Ranjan,
	linux-arm-kernel, iommu, linux-kernel
  Cc: yuanxzhang, Xiyu Yang, Xin Tan

The reference counting issue happens in several exception handling paths
of arm_smmu_iova_to_phys_hard(). When those error scenarios occur, the
function forgets to decrease the refcount of "smmu" increased by
arm_smmu_rpm_get(), causing a refcount leak.

Fix this issue by jumping to "out" label when those error scenarios
occur.

Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
---
 drivers/iommu/arm/arm-smmu/arm-smmu.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c
index 6f72c4d208ca..3a3847277320 100644
--- a/drivers/iommu/arm/arm-smmu/arm-smmu.c
+++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c
@@ -1271,6 +1271,7 @@ static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
 	u64 phys;
 	unsigned long va, flags;
 	int ret, idx = cfg->cbndx;
+	phys_addr_t addr = 0;
 
 	ret = arm_smmu_rpm_get(smmu);
 	if (ret < 0)
@@ -1290,6 +1291,7 @@ static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
 		dev_err(dev,
 			"iova to phys timed out on %pad. Falling back to software table walk.\n",
 			&iova);
+		arm_smmu_rpm_put(smmu);
 		return ops->iova_to_phys(ops, iova);
 	}
 
@@ -1298,12 +1300,14 @@ static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
 	if (phys & ARM_SMMU_CB_PAR_F) {
 		dev_err(dev, "translation fault!\n");
 		dev_err(dev, "PAR = 0x%llx\n", phys);
-		return 0;
+		goto out;
 	}
 
+	addr = (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
+out:
 	arm_smmu_rpm_put(smmu);
 
-	return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
+	return addr;
 }
 
 static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] iommu/arm-smmu: Fix arm_smmu_device refcount leak in address translation
  2021-06-10  2:49 [PATCH v2] iommu/arm-smmu: Fix arm_smmu_device refcount leak in address translation Xiyu Yang
@ 2021-06-10 22:28 ` Rob Clark
  2021-06-11 16:15 ` Will Deacon
  1 sibling, 0 replies; 3+ messages in thread
From: Rob Clark @ 2021-06-10 22:28 UTC (permalink / raw)
  To: Xiyu Yang
  Cc: Will Deacon, Robin Murphy, Joerg Roedel, Jon Hunter,
	Krishna Reddy, Jordan Crouse, Sai Prakash Ranjan,
	moderated list:ARM64 PORT (AARCH64 ARCHITECTURE),
	iommu, LKML, yuanxzhang, Xin Tan

On Wed, Jun 9, 2021 at 7:50 PM Xiyu Yang <xiyuyang19@fudan.edu.cn> wrote:
>
> The reference counting issue happens in several exception handling paths
> of arm_smmu_iova_to_phys_hard(). When those error scenarios occur, the
> function forgets to decrease the refcount of "smmu" increased by
> arm_smmu_rpm_get(), causing a refcount leak.
>
> Fix this issue by jumping to "out" label when those error scenarios
> occur.
>
> Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
> Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
> ---
>  drivers/iommu/arm/arm-smmu/arm-smmu.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c
> index 6f72c4d208ca..3a3847277320 100644
> --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c
> +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c
> @@ -1271,6 +1271,7 @@ static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
>         u64 phys;
>         unsigned long va, flags;
>         int ret, idx = cfg->cbndx;
> +       phys_addr_t addr = 0;
>
>         ret = arm_smmu_rpm_get(smmu);
>         if (ret < 0)
> @@ -1290,6 +1291,7 @@ static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
>                 dev_err(dev,
>                         "iova to phys timed out on %pad. Falling back to software table walk.\n",
>                         &iova);
> +               arm_smmu_rpm_put(smmu);
>                 return ops->iova_to_phys(ops, iova);

I suppose you could also:

   addr = ops->iov_to_phys(...);
   goto out;

but either way,

Reviewed-by: Rob Clark <robdclark@chromium.org>

>         }
>
> @@ -1298,12 +1300,14 @@ static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
>         if (phys & ARM_SMMU_CB_PAR_F) {
>                 dev_err(dev, "translation fault!\n");
>                 dev_err(dev, "PAR = 0x%llx\n", phys);
> -               return 0;
> +               goto out;
>         }
>
> +       addr = (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
> +out:
>         arm_smmu_rpm_put(smmu);
>
> -       return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
> +       return addr;
>  }
>
>  static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
> --
> 2.7.4
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] iommu/arm-smmu: Fix arm_smmu_device refcount leak in address translation
  2021-06-10  2:49 [PATCH v2] iommu/arm-smmu: Fix arm_smmu_device refcount leak in address translation Xiyu Yang
  2021-06-10 22:28 ` Rob Clark
@ 2021-06-11 16:15 ` Will Deacon
  1 sibling, 0 replies; 3+ messages in thread
From: Will Deacon @ 2021-06-11 16:15 UTC (permalink / raw)
  To: Sai Prakash Ranjan, Jordan Crouse, Xiyu Yang, Krishna Reddy,
	Rob Clark, Joerg Roedel, Robin Murphy, linux-arm-kernel,
	Jon Hunter, linux-kernel, iommu
  Cc: catalin.marinas, kernel-team, Will Deacon, Xin Tan, yuanxzhang

On Thu, 10 Jun 2021 10:49:20 +0800, Xiyu Yang wrote:
> The reference counting issue happens in several exception handling paths
> of arm_smmu_iova_to_phys_hard(). When those error scenarios occur, the
> function forgets to decrease the refcount of "smmu" increased by
> arm_smmu_rpm_get(), causing a refcount leak.
> 
> Fix this issue by jumping to "out" label when those error scenarios
> occur.

Applied to will (for-joerg/arm-smmu/updates), thanks!

[1/1] iommu/arm-smmu: Fix arm_smmu_device refcount leak in address translation
      https://git.kernel.org/will/c/7c8f176d6a3f

Cheers,
-- 
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-06-11 16:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-10  2:49 [PATCH v2] iommu/arm-smmu: Fix arm_smmu_device refcount leak in address translation Xiyu Yang
2021-06-10 22:28 ` Rob Clark
2021-06-11 16:15 ` Will Deacon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).