mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] leds: max77705: fix fwnode reference leak in max77705_add_led()
@ 2026-09-06 19:11 Miles Krause via B4 Relay
  2026-09-17 13:25 ` Lee Jones
  0 siblings, 1 reply; 4+ messages in thread
From: Miles Krause via B4 Relay @ 2026-09-06 19:11 UTC (permalink / raw)
  To: Chanwoo Choi, Krzysztof Kozlowski, Lee Jones, Pavel Machek,
	Dzmitry Sankouski
  Cc: linux-kernel, linux-leds, Miles Krause

From: Miles Krause <mileskrause5200@gmail.com>

max77705_add_led() iterates over the multicolor LED's child nodes with
fwnode_for_each_child_node() and returns directly from inside the loop
when parsing a child fails:

	fwnode_for_each_child_node(np, child) {
		ret = max77705_parse_subled(dev, child, &info[i]);
		if (ret < 0)
			return ret;
		...
	}

The iterator holds a reference on the current child for the duration of
each iteration: fwnode_get_next_child_node() takes a reference on the
node it returns and only drops the previous one when it is called again
(for the OF backend that is the of_node_put(prev) in
of_get_next_status_child()).  Returning from inside the loop skips that
final call, so the reference taken for the child that failed to parse is
never released.

max77705_parse_subled() rejects a missing, zero or out-of-range "reg"
property and propagates errors from reading "color", so a malformed
device tree is enough to leak a device_node reference.

Use fwnode_for_each_child_node_scoped() instead, which releases the
reference on every exit path, and drop the now unused 'child'
declaration.  max77705_led_probe() already uses the equivalent
device_for_each_child_node_scoped() for the outer loop.

Fixes: aebb5fc9a0d8 ("leds: max77705: Add LEDs support")
Signed-off-by: Miles Krause <mileskrause5200@gmail.com>
---
 drivers/leds/leds-max77705.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/leds/leds-max77705.c b/drivers/leds/leds-max77705.c
index 1e2054c1bf80..4fd803c95989 100644
--- a/drivers/leds/leds-max77705.c
+++ b/drivers/leds/leds-max77705.c
@@ -160,7 +160,6 @@ static int max77705_add_led(struct device *dev, struct regmap *regmap, struct fw
 	struct max77705_led *led;
 	struct led_classdev *cdev;
 	struct mc_subled *info;
-	struct fwnode_handle *child;
 	struct led_init_data init_data = {};
 
 	led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
@@ -191,7 +190,7 @@ static int max77705_add_led(struct device *dev, struct regmap *regmap, struct fw
 		cdev->brightness_set_blocking = max77705_led_brightness_set_multi;
 		cdev->blink_set = max77705_rgb_blink;
 
-		fwnode_for_each_child_node(np, child) {
+		fwnode_for_each_child_node_scoped(np, child) {
 			ret = max77705_parse_subled(dev, child, &info[i]);
 			if (ret < 0)
 				return ret;

---
base-commit: 88405f0ad1d5c680afe3ea0ce9345fa9e1deaac8
change-id: 20260906-leds-max77705-fwnode-leak-1cde49cec58c

Best regards,
-- 
Miles Krause <mileskrause5200@gmail.com>



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

* Re: [PATCH] leds: max77705: fix fwnode reference leak in max77705_add_led()
  2026-09-06 19:11 [PATCH] leds: max77705: fix fwnode reference leak in max77705_add_led() Miles Krause via B4 Relay
@ 2026-09-17 13:25 ` Lee Jones
  0 siblings, 0 replies; 4+ messages in thread
From: Lee Jones @ 2026-09-17 13:25 UTC (permalink / raw)
  To: mileskrause5200
  Cc: Chanwoo Choi, Krzysztof Kozlowski, Pavel Machek,
	Dzmitry Sankouski, linux-kernel, linux-leds

On Sun, 06 Sep 2026, Miles Krause via B4 Relay wrote:

> From: Miles Krause <mileskrause5200@gmail.com>
> 
> max77705_add_led() iterates over the multicolor LED's child nodes with
> fwnode_for_each_child_node() and returns directly from inside the loop
> when parsing a child fails:
> 
> 	fwnode_for_each_child_node(np, child) {
> 		ret = max77705_parse_subled(dev, child, &info[i]);
> 		if (ret < 0)
> 			return ret;
> 		...
> 	}
> 
> The iterator holds a reference on the current child for the duration of
> each iteration: fwnode_get_next_child_node() takes a reference on the
> node it returns and only drops the previous one when it is called again
> (for the OF backend that is the of_node_put(prev) in
> of_get_next_status_child()).  Returning from inside the loop skips that
> final call, so the reference taken for the child that failed to parse is
> never released.
> 
> max77705_parse_subled() rejects a missing, zero or out-of-range "reg"
> property and propagates errors from reading "color", so a malformed
> device tree is enough to leak a device_node reference.
> 
> Use fwnode_for_each_child_node_scoped() instead, which releases the
> reference on every exit path, and drop the now unused 'child'
> declaration.  max77705_led_probe() already uses the equivalent
> device_for_each_child_node_scoped() for the outer loop.
> 
> Fixes: aebb5fc9a0d8 ("leds: max77705: Add LEDs support")
> Signed-off-by: Miles Krause <mileskrause5200@gmail.com>

Beaten to it by this I'm afraid:

  4805e5ec50d0 leds: max77705: Use fwnode_for_each_child_node_scoped() in max77705_add_led()

-- 
Lee Jones

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

* Re: [PATCH] leds: max77705: fix fwnode reference leak in max77705_add_led()
  2026-08-28  8:46 Manush Prajwal
@ 2026-08-28  8:56 ` Krzysztof Kozlowski
  0 siblings, 0 replies; 4+ messages in thread
From: Krzysztof Kozlowski @ 2026-08-28  8:56 UTC (permalink / raw)
  To: Manush Prajwal, cw00.choi, lee, pavel
  Cc: linux-leds, linux-kernel, dsankouski, sakari.ailus

On 28/08/2026 10:46, Manush Prajwal wrote:
> In the RGB LED path of max77705_add_led(), the
> fwnode_for_each_child_node() loop over the subled child nodes
> returns directly on a max77705_parse_subled() failure without
> releasing the reference held on the current child, since
> fwnode_for_each_child_node() is not a scoped/cleanup-based
> iterator and expects the caller to drop the reference itself on
> any early exit from the loop body.
> 
> Call fwnode_handle_put() on the child fwnode before returning to
> fix the leak.
> 
> Fixes: 3b6eaa3db567 ("leds: Use fwnode_for_each_child_node() instead")
> Signed-off-by: Manush Prajwal <manushprajwal555@gmail.com>
> ---
>  drivers/leds/leds-max77705.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/leds/leds-max77705.c b/drivers/leds/leds-max77705.c
> index 1e2054c..a5030c3 100644
> --- a/drivers/leds/leds-max77705.c
> +++ b/drivers/leds/leds-max77705.c
> @@ -193,8 +193,10 @@ static int max77705_add_led(struct device *dev, struct regmap *regmap, struct fw
> 
>  		fwnode_for_each_child_node(np, child) {

Would fwnode_for_each_child_node_scoped() work here?

>  			ret = max77705_parse_subled(dev, child, &info[i]);
> -			if (ret < 0)
> +			if (ret < 0) {
> +				fwnode_handle_put(child);
>  				return ret;
> +			}
> 
Best regards,
Krzysztof

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

* [PATCH] leds: max77705: fix fwnode reference leak in max77705_add_led()
@ 2026-08-28  8:46 Manush Prajwal
  2026-08-28  8:56 ` Krzysztof Kozlowski
  0 siblings, 1 reply; 4+ messages in thread
From: Manush Prajwal @ 2026-08-28  8:46 UTC (permalink / raw)
  To: cw00.choi, krzk, lee, pavel
  Cc: linux-leds, linux-kernel, dsankouski, sakari.ailus

In the RGB LED path of max77705_add_led(), the
fwnode_for_each_child_node() loop over the subled child nodes
returns directly on a max77705_parse_subled() failure without
releasing the reference held on the current child, since
fwnode_for_each_child_node() is not a scoped/cleanup-based
iterator and expects the caller to drop the reference itself on
any early exit from the loop body.

Call fwnode_handle_put() on the child fwnode before returning to
fix the leak.

Fixes: 3b6eaa3db567 ("leds: Use fwnode_for_each_child_node() instead")
Signed-off-by: Manush Prajwal <manushprajwal555@gmail.com>
---
 drivers/leds/leds-max77705.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/leds/leds-max77705.c b/drivers/leds/leds-max77705.c
index 1e2054c..a5030c3 100644
--- a/drivers/leds/leds-max77705.c
+++ b/drivers/leds/leds-max77705.c
@@ -193,8 +193,10 @@ static int max77705_add_led(struct device *dev, struct regmap *regmap, struct fw

 		fwnode_for_each_child_node(np, child) {
 			ret = max77705_parse_subled(dev, child, &info[i]);
-			if (ret < 0)
+			if (ret < 0) {
+				fwnode_handle_put(child);
 				return ret;
+			}

 			info[i].intensity = 0;
 			i++;
--
2.46.2.windows.1


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

end of thread, other threads:[~2026-09-17 13:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-06 19:11 [PATCH] leds: max77705: fix fwnode reference leak in max77705_add_led() Miles Krause via B4 Relay
2026-09-17 13:25 ` Lee Jones
  -- strict thread matches above, loose matches on Subject: below --
2026-08-28  8:46 Manush Prajwal
2026-08-28  8:56 ` Krzysztof Kozlowski

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®