linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] memory: samsung: exynos-srom: Fix of_iomap leak in exynos_srom_probe
@ 2025-07-31  8:33 Zhen Ni
  2025-07-31  9:06 ` Krzysztof Kozlowski
  2025-08-06  2:55 ` [PATCH v2] " Zhen Ni
  0 siblings, 2 replies; 6+ messages in thread
From: Zhen Ni @ 2025-07-31  8:33 UTC (permalink / raw)
  To: krzk, alim.akhtar
  Cc: linux-kernel, linux-arm-kernel, linux-samsung-soc, Zhen Ni

The current error handling in exynos_srom_probe() has a resource leak
in the of_platform_populate() failure path. When this function fails
after successful resource allocation, srom->reg_base is not released.

To fix this issue, replace of_iomap() with
devm_platform_ioremap_resource(). devm_platform_ioremap_resource()
is a specialized function for platform devices.
It allows 'srom->reg_base' to be automatically released whether the
probe function succeeds or fails.

Besides, use IS_ERR() instead of !srom->reg_base
as the return value of devm_platform_ioremap_resource()
can either be a pointer to the remapped memory or
an ERR_PTR() encoded error code if the operation fails.

Signed-off-by: Zhen Ni <zhen.ni@easystack.cn>
---
 drivers/memory/samsung/exynos-srom.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/drivers/memory/samsung/exynos-srom.c b/drivers/memory/samsung/exynos-srom.c
index e73dd330af47..d913fb901973 100644
--- a/drivers/memory/samsung/exynos-srom.c
+++ b/drivers/memory/samsung/exynos-srom.c
@@ -121,20 +121,18 @@ static int exynos_srom_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	srom->dev = dev;
-	srom->reg_base = of_iomap(np, 0);
-	if (!srom->reg_base) {
+	srom->reg_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(srom->reg_base)) {
 		dev_err(&pdev->dev, "iomap of exynos srom controller failed\n");
-		return -ENOMEM;
+		return PTR_ERR(srom->reg_base);
 	}
 
 	platform_set_drvdata(pdev, srom);
 
 	srom->reg_offset = exynos_srom_alloc_reg_dump(exynos_srom_offsets,
 						      ARRAY_SIZE(exynos_srom_offsets));
-	if (!srom->reg_offset) {
-		iounmap(srom->reg_base);
+	if (!srom->reg_offset)
 		return -ENOMEM;
-	}
 
 	for_each_child_of_node(np, child) {
 		if (exynos_srom_configure_bank(srom, child)) {
-- 
2.20.1


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

* Re: [PATCH] memory: samsung: exynos-srom: Fix of_iomap leak in exynos_srom_probe
  2025-07-31  8:33 [PATCH] memory: samsung: exynos-srom: Fix of_iomap leak in exynos_srom_probe Zhen Ni
@ 2025-07-31  9:06 ` Krzysztof Kozlowski
  2025-07-31  9:40   ` zhen.ni
  2025-08-06  2:55 ` [PATCH v2] " Zhen Ni
  1 sibling, 1 reply; 6+ messages in thread
From: Krzysztof Kozlowski @ 2025-07-31  9:06 UTC (permalink / raw)
  To: Zhen Ni, alim.akhtar; +Cc: linux-kernel, linux-arm-kernel, linux-samsung-soc

On 31/07/2025 10:33, Zhen Ni wrote:
> The current error handling in exynos_srom_probe() has a resource leak
> in the of_platform_populate() failure path. When this function fails
> after successful resource allocation, srom->reg_base is not released.
> 
> To fix this issue, replace of_iomap() with
> devm_platform_ioremap_resource(). devm_platform_ioremap_resource()
> is a specialized function for platform devices.

Don't explain us kernel code. Drop sentence.

> It allows 'srom->reg_base' to be automatically released whether the
> probe function succeeds or fails.

It's obvious.

> 
> Besides, use IS_ERR() instead of !srom->reg_base

I don't understand this. You keep explaining the code and this suggests
you made change here not related to original case. Can you return
srom->reg_base here? No?


> as the return value of devm_platform_ioremap_resource()
> can either be a pointer to the remapped memory or
> an ERR_PTR() encoded error code if the operation fails.
> 

Missing fixes and cc-stable.

> Signed-off-by: Zhen Ni <zhen.ni@easystack.cn>
> ---
>  drivers/memory/samsung/exynos-srom.c | 10 ++++------
>  1 file changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/memory/samsung/exynos-srom.c b/drivers/memory/samsung/exynos-srom.c
> index e73dd330af47..d913fb901973 100644
> --- a/drivers/memory/samsung/exynos-srom.c
> +++ b/drivers/memory/samsung/exynos-srom.c
> @@ -121,20 +121,18 @@ static int exynos_srom_probe(struct platform_device *pdev)
>  		return -ENOMEM;
>  
>  	srom->dev = dev;
> -	srom->reg_base = of_iomap(np, 0);
> -	if (!srom->reg_base) {
> +	srom->reg_base = devm_platform_ioremap_resource(pdev, 0);
> +	if (IS_ERR(srom->reg_base)) {
>  		dev_err(&pdev->dev, "iomap of exynos srom controller failed\n");
> -		return -ENOMEM;
> +		return PTR_ERR(srom->reg_base);

How did you test it?

Best regards,
Krzysztof

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

* Re: [PATCH] memory: samsung: exynos-srom: Fix of_iomap leak in exynos_srom_probe
  2025-07-31  9:06 ` Krzysztof Kozlowski
@ 2025-07-31  9:40   ` zhen.ni
  2025-07-31 10:59     ` Krzysztof Kozlowski
  0 siblings, 1 reply; 6+ messages in thread
From: zhen.ni @ 2025-07-31  9:40 UTC (permalink / raw)
  To: Krzysztof Kozlowski, alim.akhtar
  Cc: linux-kernel, linux-arm-kernel, linux-samsung-soc

Hi,

During static analysis with Smatch, I identified the following issue:

```
drivers/memory/samsung/exynos-srom.c:155 exynos_srom_probe()
warn: 'srom->reg_base' from of_iomap() not released on lines: 155.
```

Problem analysis:
1. In exynos_srom_probe(), the reg_base resource is acquired via of_iomap()
2. The of_platform_populate() call at the end of the function has a 
possible failure path:
```
root = root ? of_node_get(root) : of_find_node_by_path("/");
if (!root)
      return -EINVAL; // Failure return point
```
3. When this path is taken, reg_base is not released, causing a resource 
leak

Verification:
After applying the patch, the Smatch warning is resolved


Best regards,
Zhen


在 2025/7/31 17:06, Krzysztof Kozlowski 写道:
> On 31/07/2025 10:33, Zhen Ni wrote:
>> The current error handling in exynos_srom_probe() has a resource leak
>> in the of_platform_populate() failure path. When this function fails
>> after successful resource allocation, srom->reg_base is not released.
>>
>> To fix this issue, replace of_iomap() with
>> devm_platform_ioremap_resource(). devm_platform_ioremap_resource()
>> is a specialized function for platform devices.
> 
> Don't explain us kernel code. Drop sentence.
> 
>> It allows 'srom->reg_base' to be automatically released whether the
>> probe function succeeds or fails.
> 
> It's obvious.
> 
>>
>> Besides, use IS_ERR() instead of !srom->reg_base
> 
> I don't understand this. You keep explaining the code and this suggests
> you made change here not related to original case. Can you return
> srom->reg_base here? No?
> 
> 
>> as the return value of devm_platform_ioremap_resource()
>> can either be a pointer to the remapped memory or
>> an ERR_PTR() encoded error code if the operation fails.
>>
> 
> Missing fixes and cc-stable.
> 
>> Signed-off-by: Zhen Ni <zhen.ni@easystack.cn>
>> ---
>>   drivers/memory/samsung/exynos-srom.c | 10 ++++------
>>   1 file changed, 4 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/memory/samsung/exynos-srom.c b/drivers/memory/samsung/exynos-srom.c
>> index e73dd330af47..d913fb901973 100644
>> --- a/drivers/memory/samsung/exynos-srom.c
>> +++ b/drivers/memory/samsung/exynos-srom.c
>> @@ -121,20 +121,18 @@ static int exynos_srom_probe(struct platform_device *pdev)
>>   		return -ENOMEM;
>>   
>>   	srom->dev = dev;
>> -	srom->reg_base = of_iomap(np, 0);
>> -	if (!srom->reg_base) {
>> +	srom->reg_base = devm_platform_ioremap_resource(pdev, 0);
>> +	if (IS_ERR(srom->reg_base)) {
>>   		dev_err(&pdev->dev, "iomap of exynos srom controller failed\n");
>> -		return -ENOMEM;
>> +		return PTR_ERR(srom->reg_base);
> 
> How did you test it?
> 
> Best regards,
> Krzysztof
> 
> 

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

* Re: [PATCH] memory: samsung: exynos-srom: Fix of_iomap leak in exynos_srom_probe
  2025-07-31  9:40   ` zhen.ni
@ 2025-07-31 10:59     ` Krzysztof Kozlowski
  0 siblings, 0 replies; 6+ messages in thread
From: Krzysztof Kozlowski @ 2025-07-31 10:59 UTC (permalink / raw)
  To: zhen.ni, alim.akhtar; +Cc: linux-kernel, linux-arm-kernel, linux-samsung-soc

On 31/07/2025 11:40, zhen.ni wrote:
> Hi,
> 
> During static analysis with Smatch, I identified the following issue:
> 
> ```
> drivers/memory/samsung/exynos-srom.c:155 exynos_srom_probe()
> warn: 'srom->reg_base' from of_iomap() not released on lines: 155.

Nothing in commit msg explained you used Smatch. You must describe the
tools you use.

> ```
> 
> Problem analysis:
> 1. In exynos_srom_probe(), the reg_base resource is acquired via of_iomap()
> 2. The of_platform_populate() call at the end of the function has a 
> possible failure path:

Above is AI/LLM output?

> ```
> root = root ? of_node_get(root) : of_find_node_by_path("/");
> if (!root)
>       return -EINVAL; // Failure return point
> ```
> 3. When this path is taken, reg_base is not released, causing a resource 
> leak
> 
> Verification:
> After applying the patch, the Smatch warning is resolved

This means no testing. You should mention it as well.

Don't top post. See netiquette.


Best regards,
Krzysztof

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

* [PATCH v2] memory: samsung: exynos-srom: Fix of_iomap leak in exynos_srom_probe
  2025-07-31  8:33 [PATCH] memory: samsung: exynos-srom: Fix of_iomap leak in exynos_srom_probe Zhen Ni
  2025-07-31  9:06 ` Krzysztof Kozlowski
@ 2025-08-06  2:55 ` Zhen Ni
  2025-08-13 10:30   ` Krzysztof Kozlowski
  1 sibling, 1 reply; 6+ messages in thread
From: Zhen Ni @ 2025-08-06  2:55 UTC (permalink / raw)
  To: krzk, alim.akhtar
  Cc: linux-kernel, linux-arm-kernel, linux-samsung-soc, Zhen Ni,
	stable

The of_platform_populate() call at the end of the function has a
possible failure path, causing a resource leak.

Replace of_iomap() with devm_platform_ioremap_resource() to ensure
automatic cleanup of srom->reg_base.

This issue was detected by smatch static analysis:
drivers/memory/samsung/exynos-srom.c:155 exynos_srom_probe()warn:
'srom->reg_base' from of_iomap() not released on lines: 155.

Fixes: 8ac2266d8831 ("memory: samsung: exynos-srom: Add support for bank configuration")
Cc: stable@vger.kernel.org
Signed-off-by: Zhen Ni <zhen.ni@easystack.cn>
---
chanegs in v2:
- Update commit msg
---
 drivers/memory/samsung/exynos-srom.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/drivers/memory/samsung/exynos-srom.c b/drivers/memory/samsung/exynos-srom.c
index e73dd330af47..d913fb901973 100644
--- a/drivers/memory/samsung/exynos-srom.c
+++ b/drivers/memory/samsung/exynos-srom.c
@@ -121,20 +121,18 @@ static int exynos_srom_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	srom->dev = dev;
-	srom->reg_base = of_iomap(np, 0);
-	if (!srom->reg_base) {
+	srom->reg_base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(srom->reg_base)) {
 		dev_err(&pdev->dev, "iomap of exynos srom controller failed\n");
-		return -ENOMEM;
+		return PTR_ERR(srom->reg_base);
 	}
 
 	platform_set_drvdata(pdev, srom);
 
 	srom->reg_offset = exynos_srom_alloc_reg_dump(exynos_srom_offsets,
 						      ARRAY_SIZE(exynos_srom_offsets));
-	if (!srom->reg_offset) {
-		iounmap(srom->reg_base);
+	if (!srom->reg_offset)
 		return -ENOMEM;
-	}
 
 	for_each_child_of_node(np, child) {
 		if (exynos_srom_configure_bank(srom, child)) {
-- 
2.20.1


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

* Re: [PATCH v2] memory: samsung: exynos-srom: Fix of_iomap leak in exynos_srom_probe
  2025-08-06  2:55 ` [PATCH v2] " Zhen Ni
@ 2025-08-13 10:30   ` Krzysztof Kozlowski
  0 siblings, 0 replies; 6+ messages in thread
From: Krzysztof Kozlowski @ 2025-08-13 10:30 UTC (permalink / raw)
  To: krzk, alim.akhtar, Zhen Ni
  Cc: linux-kernel, linux-arm-kernel, linux-samsung-soc, stable


On Wed, 06 Aug 2025 10:55:38 +0800, Zhen Ni wrote:
> The of_platform_populate() call at the end of the function has a
> possible failure path, causing a resource leak.
> 
> Replace of_iomap() with devm_platform_ioremap_resource() to ensure
> automatic cleanup of srom->reg_base.
> 
> This issue was detected by smatch static analysis:
> drivers/memory/samsung/exynos-srom.c:155 exynos_srom_probe()warn:
> 'srom->reg_base' from of_iomap() not released on lines: 155.
> 
> [...]

Applied, thanks!

[1/1] memory: samsung: exynos-srom: Fix of_iomap leak in exynos_srom_probe
      https://git.kernel.org/krzk/linux-mem-ctrl/c/6744085079e785dae5f7a2239456135407c58b25

Best regards,
-- 
Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>


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

end of thread, other threads:[~2025-08-13 10:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-31  8:33 [PATCH] memory: samsung: exynos-srom: Fix of_iomap leak in exynos_srom_probe Zhen Ni
2025-07-31  9:06 ` Krzysztof Kozlowski
2025-07-31  9:40   ` zhen.ni
2025-07-31 10:59     ` Krzysztof Kozlowski
2025-08-06  2:55 ` [PATCH v2] " Zhen Ni
2025-08-13 10:30   ` Krzysztof Kozlowski

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).