mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] thermal/of: fix device node refcount leak in thermal_of_cm_lookup()
@ 2026-01-16  7:30 Weigang He
  2026-01-17 16:20 ` Markus Elfring
  0 siblings, 1 reply; 2+ messages in thread
From: Weigang He @ 2026-01-16  7:30 UTC (permalink / raw)
  To: Rafael J . Wysocki, Daniel Lezcano
  Cc: Zhang Rui, Lukasz Luba, linux-pm, linux-kernel, Weigang He, stable

of_parse_phandle() returns a device_node pointer with refcount
incremented. The caller must use of_node_put() when done.

thermal_of_cm_lookup() acquires a reference to tr_np via
of_parse_phandle() but fails to release it on multiple paths:
  - When tr_np != trip->priv (continue path)
  - When thermal_of_get_cooling_spec() returns true (early return)
  - At the end of each loop iteration

Add the missing of_node_put() calls on all paths to prevent the
reference count leak.

Fixes: 423de5b5bc5b ("thermal/of: Fix cdev lookup in thermal_of_should_bind()")
Cc: stable@vger.kernel.org
Signed-off-by: Weigang He <geoffreyhe2@gmail.com>
---
 drivers/thermal/thermal_of.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index 1a51a4d240ff6..ef3e5d4e3b6e8 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -284,8 +284,10 @@ static bool thermal_of_cm_lookup(struct device_node *cm_np,
 		int count, i;
 
 		tr_np = of_parse_phandle(child, "trip", 0);
-		if (tr_np != trip->priv)
+		if (tr_np != trip->priv) {
+			of_node_put(tr_np);
 			continue;
+		}
 
 		/* The trip has been found, look up the cdev. */
 		count = of_count_phandle_with_args(child, "cooling-device",
@@ -294,9 +296,12 @@ static bool thermal_of_cm_lookup(struct device_node *cm_np,
 			pr_err("Add a cooling_device property with at least one device\n");
 
 		for (i = 0; i < count; i++) {
-			if (thermal_of_get_cooling_spec(child, i, cdev, c))
+			if (thermal_of_get_cooling_spec(child, i, cdev, c)) {
+				of_node_put(tr_np);
 				return true;
+			}
 		}
+		of_node_put(tr_np);
 	}
 
 	return false;
-- 
2.34.1


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

* Re: [PATCH] thermal/of: fix device node refcount leak in thermal_of_cm_lookup()
  2026-01-16  7:30 [PATCH] thermal/of: fix device node refcount leak in thermal_of_cm_lookup() Weigang He
@ 2026-01-17 16:20 ` Markus Elfring
  0 siblings, 0 replies; 2+ messages in thread
From: Markus Elfring @ 2026-01-17 16:20 UTC (permalink / raw)
  To: Weigang He, linux-pm
  Cc: stable, LKML, kernel-janitors, Daniel Lezcano, Lukasz Luba,
	Rafael J. Wysocki, Zhang Rui

…
> Add the missing of_node_put() calls on all paths to prevent the

How do you think about to use an attribute like “__free(device_node)” instead?
https://elixir.bootlin.com/linux/v6.19-rc5/source/include/linux/of.h#L138
https://elixir.bootlin.com/linux/v6.19-rc5/source/drivers/thermal/thermal_of.c#L277-L303


Was such an improvable implementation detail detected by any known source code
analysis approaches?


> reference count leak.

Would you like to use this term also in the summary phrase?

Regards,
Markus

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

end of thread, other threads:[~2026-01-17 16:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-16  7:30 [PATCH] thermal/of: fix device node refcount leak in thermal_of_cm_lookup() Weigang He
2026-01-17 16:20 ` Markus Elfring

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

all inboxes | Powered by JetHome®