Netdev List
 help / color / mirror / Atom feed
* [PATCH v3] net: ethernet: renesas: rswitch: fix device_node refcount leak in rswitch_get_port_node()
@ 2026-08-20  5:26 Manush Prajwal
  2026-08-20 15:26 ` Andrew Lunn
  0 siblings, 1 reply; 2+ messages in thread
From: Manush Prajwal @ 2026-08-20  5:26 UTC (permalink / raw)
  To: yoshihiro.shimoda.uh, michael.dege, andrew+netdev, davem,
	edumazet, kuba, pabeni
  Cc: netdev, linux-renesas-soc

On an of_property_read_u32() failure, rswitch_get_port_node() set port
to NULL and jumped to the out label before releasing the reference the
for_each_available_child_of_node() iterator was holding on it. Once
port was overwritten with NULL, that reference could never be
released since out: only put "ports", the parent node.

Rework the function around for_each_available_child_of_node_scoped()
instead of adding a manual of_node_put(), so the iterator's reference
is dropped automatically on every exit path. Since port is the
function's return value, take an explicit reference with of_node_get()
on the match before breaking out of the loop.

Signed-off-by: Manush Prajwal <manushprajwal555@gmail.com>
---
v3: Reorder local variable declarations into reverse Christmas tree
    order, per Andrew Lunn's review.
v2: Rework using for_each_available_child_of_node_scoped() instead of
    a manual of_node_put(), per Andrew Lunn's review.

 drivers/net/ethernet/renesas/rswitch_main.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/drivers/net/ethernet/renesas/rswitch_main.c b/drivers/net/ethernet/renesas/rswitch_main.c
index 6fe9648163..ffdc42f430 100644
--- a/drivers/net/ethernet/renesas/rswitch_main.c
+++ b/drivers/net/ethernet/renesas/rswitch_main.c
@@ -1303,8 +1303,8 @@
 /* Call of_node_put(port) after done */
 static struct device_node *rswitch_get_port_node(struct rswitch_device *rdev)
 {
-	struct device_node *ports, *port;
-	int err = 0;
+	struct device_node *port = NULL;
+	struct device_node *ports;
 	u32 index;

 	ports = of_get_child_by_name(rdev->ndev->dev.parent->of_node,
@@ -1312,17 +1312,15 @@ static struct device_node *rswitch_get_port_node(struct rswitch_device *rdev)
 	if (!ports)
 		return NULL;

-	for_each_available_child_of_node(ports, port) {
-		err = of_property_read_u32(port, "reg", &index);
-		if (err < 0) {
-			port = NULL;
-			goto out;
-		}
-		if (index == rdev->etha->index)
+	for_each_available_child_of_node_scoped(ports, child) {
+		if (of_property_read_u32(child, "reg", &index))
+			break;
+		if (index == rdev->etha->index) {
+			port = of_node_get(child);
 			break;
+		}
 	}

-out:
 	of_node_put(ports);

 	return port;
--
2.46.2.windows.1


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

* Re: [PATCH v3] net: ethernet: renesas: rswitch: fix device_node refcount leak in rswitch_get_port_node()
  2026-08-20  5:26 [PATCH v3] net: ethernet: renesas: rswitch: fix device_node refcount leak in rswitch_get_port_node() Manush Prajwal
@ 2026-08-20 15:26 ` Andrew Lunn
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Lunn @ 2026-08-20 15:26 UTC (permalink / raw)
  To: Manush Prajwal
  Cc: yoshihiro.shimoda.uh, michael.dege, andrew+netdev, davem,
	edumazet, kuba, pabeni, netdev, linux-renesas-soc

> -	for_each_available_child_of_node(ports, port) {
> -		err = of_property_read_u32(port, "reg", &index);
> -		if (err < 0) {
> -			port = NULL;
> -			goto out;
> -		}
> -		if (index == rdev->etha->index)
> +	for_each_available_child_of_node_scoped(ports, child) {

> +		if (of_property_read_u32(child, "reg", &index))
> +			break;

Why did you change this?

Please keep the changes minimal, only change what must be changed.

I doubt you have tested this code in any way, so the change has
risk. By keeping the change as small as possible, you keep the risk
lower, so you are less likely to break it.

	Andrew

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

end of thread, other threads:[~2026-08-20 15:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-20  5:26 [PATCH v3] net: ethernet: renesas: rswitch: fix device_node refcount leak in rswitch_get_port_node() Manush Prajwal
2026-08-20 15:26 ` Andrew Lunn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox