Skip to contentSkip to conversation

coresight: Fix ref leak when of_coresight_parse_endpoint() fails

PATCH Applied James Clark 5 participants 5 messages May 29, 2024 – Jun 10, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 coresight: Fix ref leak when of_coresight_parse_endpoint() fails

of_graph_get_next_endpoint() releases the reference to the previous endpoint on each iteration, but when parsing fails the loop exits early meaning the last reference is never dropped.

Fix it by dropping the refcount in the exit condition.

Fixes d375b356e687 ("coresight: Fix support for sparsely populated ports")
---
 drivers/hwtracing/coresight/coresight-platform.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hwtracing/coresight/coresight-platform.c +3 -1
index 9d550f5697fa..57a009552cc5 100644
@@ -297,8 +297,10 @@ static int of_get_coresight_platform_data(struct device *dev,
 			continue;
 
 		ret = of_coresight_parse_endpoint(dev, ep, pdata);
-		if (ret)
+		if (ret) {
+			of_node_put(ep);
 			return ret;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw

Hi James,

Thank you for the patch.

Quoted 5 lines · James Clark

of_graph_get_next_endpoint() releases the reference to the previous endpoint on each iteration, but when parsing fails the loop exits early meaning the last reference is never dropped.

Fix it by dropping the refcount in the exit condition.

You can add

Quoted 2 lines
Fixes d375b356e687 ("coresight: Fix support for sparsely populated ports")

One a side note, maybe it would be nice to add a new version of the for_each_endpoint_of_node() macro that would declare the iterator as a local variable scoped to the loop, making sure the reference always gets released when we exit the loop.

And now that I've written that, it sounds we could as well have a version of the macro that doesn't take a new reference to the iterator. That may be simpler and less error-prone in the end.

Quoted 20 lines
---
 drivers/hwtracing/coresight/coresight-platform.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hwtracing/coresight/coresight-platform.c +3 -1
index 9d550f5697fa..57a009552cc5 100644
@@ -297,8 +297,10 @@ static int of_get_coresight_platform_data(struct device *dev,
 			continue;
 
 		ret = of_coresight_parse_endpoint(dev, ep, pdata);
-		if (ret)
+		if (ret) {
+			of_node_put(ep);
 			return ret;
+		}
 	}
 
 	return 0;
Signature · 3 lines
Regards,

Laurent Pinchart
#2 lore raw
In reply to James Clark

…

Quoted 1 line

Fix it by dropping the refcount in the exit condition.

reference counter?

How do you think about to use the summary phrase
“Fix reference counting leak in of_get_coresight_platform_data()”?

Regards,
Markus

#3 lore raw
Quoted 10 lines · Markus Elfring

…

Quoted 1 line

Fix it by dropping the refcount in the exit condition.

reference counter?

How do you think about to use the summary phrase
“Fix reference counting leak in of_get_coresight_platform_data()”?

Regards,
Markus

Hi,

This is the semi-friendly patch-bot of Greg Kroah-Hartman.

Markus, you seem to have sent a nonsensical or otherwise pointless review comment to a patch submission on a Linux kernel developer mailing list. I strongly suggest that you not do this anymore. Please do not bother developers who are actively working to produce patches and features with comments that, in the end, are a waste of time.

Patch submitter, please ignore Markus's suggestion; you do not need to follow it at all. The person/bot/AI that sent it is being ignored by almost all Linux kernel maintainers for having a persistent pattern of behavior of producing distracting and pointless commentary, and inability to adapt to feedback. Please feel free to also ignore emails from them.

thanks,

greg k-h's patch email bot

maintainer Applied
#4 lore raw
In reply to James Clark
Quoted 8 lines · James Clark

of_graph_get_next_endpoint() releases the reference to the previous endpoint on each iteration, but when parsing fails the loop exits early meaning the last reference is never dropped.

Fix it by dropping the refcount in the exit condition.

[...]

Applied, thanks!

[1/1] coresight: Fix ref leak when of_coresight_parse_endpoint() fails

      https://git.kernel.org/coresight/c/7fcb9cb2fe47294e16067c3cfd25332c8662a115

Best regards,

Signature · 1 line
Suzuki K Poulose <[email protected]>