linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iommu/dart: add missing put_device() call in apple_dart_of_xlate
@ 2025-07-12 16:16 Zhang Shurong
  2025-07-12 18:22 ` Markus Elfring
  2025-07-14 11:23 ` Will Deacon
  0 siblings, 2 replies; 3+ messages in thread
From: Zhang Shurong @ 2025-07-12 16:16 UTC (permalink / raw)
  To: sven
  Cc: j, alyssa, neal, joro, will, robin.murphy, asahi,
	linux-arm-kernel, iommu, linux-kernel, Zhang Shurong

The apple_dart_of_xlate() function obtains a platform device reference
via of_find_device_by_node() but doesn't release it with put_device().
This patch adds proper device reference handling to prevent memory
leaks.

Fixes: 46d1fb072e76 ("iommu/dart: Add DART iommu driver")
Signed-off-by: Zhang Shurong <zhang_shurong@foxmail.com>
---
 drivers/iommu/apple-dart.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c
index 190f28d76615..811bf5176568 100644
--- a/drivers/iommu/apple-dart.c
+++ b/drivers/iommu/apple-dart.c
@@ -796,8 +796,10 @@ static int apple_dart_of_xlate(struct device *dev,
 
 	if (!cfg) {
 		cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
-		if (!cfg)
+		if (!cfg)  {
+			put_device(&iommu_pdev->dev);
 			return -ENOMEM;
+		}
 		/* Will be ANDed with DART capabilities */
 		cfg->supports_bypass = true;
 	}
@@ -805,8 +807,10 @@ static int apple_dart_of_xlate(struct device *dev,
 
 	cfg_dart = cfg->stream_maps[0].dart;
 	if (cfg_dart) {
-		if (cfg_dart->pgsize != dart->pgsize)
+		if (cfg_dart->pgsize != dart->pgsize) {
+			put_device(&iommu_pdev->dev);
 			return -EINVAL;
+		}
 	}
 
 	cfg->supports_bypass &= dart->supports_bypass;
@@ -825,6 +829,8 @@ static int apple_dart_of_xlate(struct device *dev,
 		}
 	}
 
+
+	put_device(&iommu_pdev->dev);
 	return -EINVAL;
 }
 
-- 
2.39.5


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

* Re: [PATCH] iommu/dart: add missing put_device() call in apple_dart_of_xlate
  2025-07-12 16:16 [PATCH] iommu/dart: add missing put_device() call in apple_dart_of_xlate Zhang Shurong
@ 2025-07-12 18:22 ` Markus Elfring
  2025-07-14 11:23 ` Will Deacon
  1 sibling, 0 replies; 3+ messages in thread
From: Markus Elfring @ 2025-07-12 18:22 UTC (permalink / raw)
  To: Zhang Shurong, asahi, iommu, linux-arm-kernel
  Cc: LKML, Alyssa Rosenzweig, Janne Grunau, Jörg Rödel,
	Neal Gompa, Robin Murphy, Sven Peter, Will Deacon

> The apple_dart_of_xlate() function obtains a platform device reference
> via of_find_device_by_node() but doesn't release it with put_device().
> This patch adds proper device reference handling to prevent memory
> leaks.

Will another goto chain become helpful here?

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.16-rc5#n94


Under which circumstances can the application of the attribute “put_device” grow?
https://elixir.bootlin.com/linux/v6.16-rc5/source/include/linux/device.h#L1140

Regards,
Markus

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

* Re: [PATCH] iommu/dart: add missing put_device() call in apple_dart_of_xlate
  2025-07-12 16:16 [PATCH] iommu/dart: add missing put_device() call in apple_dart_of_xlate Zhang Shurong
  2025-07-12 18:22 ` Markus Elfring
@ 2025-07-14 11:23 ` Will Deacon
  1 sibling, 0 replies; 3+ messages in thread
From: Will Deacon @ 2025-07-14 11:23 UTC (permalink / raw)
  To: Zhang Shurong
  Cc: sven, j, alyssa, neal, joro, robin.murphy, asahi,
	linux-arm-kernel, iommu, linux-kernel

On Sun, Jul 13, 2025 at 12:16:24AM +0800, Zhang Shurong wrote:
> The apple_dart_of_xlate() function obtains a platform device reference
> via of_find_device_by_node() but doesn't release it with put_device().
> This patch adds proper device reference handling to prevent memory
> leaks.
> 
> Fixes: 46d1fb072e76 ("iommu/dart: Add DART iommu driver")
> Signed-off-by: Zhang Shurong <zhang_shurong@foxmail.com>
> ---
>  drivers/iommu/apple-dart.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c
> index 190f28d76615..811bf5176568 100644
> --- a/drivers/iommu/apple-dart.c
> +++ b/drivers/iommu/apple-dart.c
> @@ -796,8 +796,10 @@ static int apple_dart_of_xlate(struct device *dev,
>  
>  	if (!cfg) {
>  		cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
> -		if (!cfg)
> +		if (!cfg)  {
> +			put_device(&iommu_pdev->dev);
>  			return -ENOMEM;
> +		}
>  		/* Will be ANDed with DART capabilities */
>  		cfg->supports_bypass = true;
>  	}
> @@ -805,8 +807,10 @@ static int apple_dart_of_xlate(struct device *dev,
>  
>  	cfg_dart = cfg->stream_maps[0].dart;
>  	if (cfg_dart) {
> -		if (cfg_dart->pgsize != dart->pgsize)
> +		if (cfg_dart->pgsize != dart->pgsize) {
> +			put_device(&iommu_pdev->dev);
>  			return -EINVAL;
> +		}
>  	}
>  
>  	cfg->supports_bypass &= dart->supports_bypass;
> @@ -825,6 +829,8 @@ static int apple_dart_of_xlate(struct device *dev,
>  		}
>  	}
>  
> +
> +	put_device(&iommu_pdev->dev);
>  	return -EINVAL;

Why is it correct to retain the reference on the device in the cases
where this function returns 0?

Will

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

end of thread, other threads:[~2025-07-14 11:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-12 16:16 [PATCH] iommu/dart: add missing put_device() call in apple_dart_of_xlate Zhang Shurong
2025-07-12 18:22 ` Markus Elfring
2025-07-14 11:23 ` 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).