Re: [PATCH 2/3] thermal: of: Fix OF node leak in thermal_of_zone_register()

From: Chen-Yu Tsai
Date: Thu Aug 15 2024 - 00:51:52 EST


On Wed, Aug 14, 2024 at 09:58:22PM +0200, Krzysztof Kozlowski wrote:
> thermal_of_zone_register() calls of_thermal_zone_find() which will
> iterate over OF nodes with for_each_available_child_of_node() to find
> matching thermal zone node. When it finds such, it exits the loop and
> returns the node. Prematurely ending for_each_available_child_of_node()
> loops requires dropping OF node reference, thus success of
> of_thermal_zone_find() means that caller must drop the reference.
>
> Fixes: 3fd6d6e2b4e8 ("thermal/of: Rework the thermal device tree initialization")
> Cc: <stable@xxxxxxxxxxxxxxx>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@xxxxxxxxxx>

Reviewed-by: Chen-Yu Tsai <wenst@xxxxxxxxxxxx>

> ---
> drivers/thermal/thermal_of.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
> index 30f8d6e70484..b08a9b64718d 100644
> --- a/drivers/thermal/thermal_of.c
> +++ b/drivers/thermal/thermal_of.c
> @@ -491,7 +491,8 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
> trips = thermal_of_trips_init(np, &ntrips);
> if (IS_ERR(trips)) {
> pr_err("Failed to find trip points for %pOFn id=%d\n", sensor, id);
> - return ERR_CAST(trips);
> + ret = PTR_ERR(trips);
> + goto out_of_node_put;
> }
>
> ret = thermal_of_monitor_init(np, &delay, &pdelay);
> @@ -519,6 +520,7 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
> goto out_kfree_trips;
> }
>
> + of_node_put(np);
> kfree(trips);
>
> ret = thermal_zone_device_enable(tz);
> @@ -533,6 +535,8 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node *
>
> out_kfree_trips:
> kfree(trips);
> +out_of_node_put:
> + of_node_put(np);
>
> return ERR_PTR(ret);
> }
> --
> 2.43.0
>