net: ethernet: renesas: rswitch: fix device_node refcount leak in rswitch_get_port_node()

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>
Link: https://patch.msgid.link/6a882352.ee10049a.267d65.7a31@mx.google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Manush Prajwal 2026-08-21 15:37:14 +05:30 committed by Jakub Kicinski
parent 01ba856c36
commit 5c07193ebe

View File

@ -1303,7 +1303,8 @@ static int rswitch_etha_mii_write_c22(struct mii_bus *bus, int phyad,
/* Call of_node_put(port) after done */
static struct device_node *rswitch_get_port_node(struct rswitch_device *rdev)
{
struct device_node *ports, *port;
struct device_node *port = NULL;
struct device_node *ports;
int err = 0;
u32 index;
@ -1312,17 +1313,16 @@ 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) {
err = of_property_read_u32(child, "reg", &index);
if (err < 0)
break;
if (index == rdev->etha->index) {
port = of_node_get(child);
break;
}
}
out:
of_node_put(ports);
return port;