Skip to contentSkip to conversation

leds: pwm-multicolor: Manage the multi-led node reference

PATCH Applied Yige Jiang 3 participants 4 messages Oct 30 – Nov 26 LED

lore
Share & tools
lore

Yige Jiang

author
#0 lore raw

PATCH v2 1/1 leds: pwm-multicolor: Manage the multi-led node reference

led_pwm_mc_probe() leaks the multi-led node reference on successful probe and if the final PWM update fails. LED registration borrows the node, so it must remain valid until the LED is unregistered.

Register a managed put action before LED registration. Reverse devres order then unregisters the LED before releasing the node on unbind or probe failure. Drop the manual error-path put and return directly on probe errors, leaving the reference cleanup to devres.

Fixes 9fa2762110dd ("leds: Add PWM multicolor driver")
Assisted-by LLM

---
Changes in v2:
- Capitalize the subject description to follow the LED subsystem style.
- Remove the redundant release_mcnode label and return directly from the probe error paths.

v1: https://lore.kernel.org/r/[email protected]

Based on linux-next next-20261016 (2efb7a11f1aa).
Compile-tested: arm64, GCC 14.2.0, W=1, CONFIG_OF_DYNAMIC=y. COMPILE_TEST=y.
The object was rebuilt from this exact base with v2 applied. No full kernel or module-link, hardware or runtime test was performed.

 drivers/leds/rgb/leds-pwm-multicolor.c | 31 +++++++++++++-------------
 1 file changed, 16 insertions(+), 15 deletions(-)
drivers/leds/rgb/leds-pwm-multicolor.c +16 -15
index 6365459ec27f17187804153cde6cb1805eca33e9..10527a64d1982733a43b76bd8bc123aba95b6f72 100644
@@ -106,6 +106,11 @@ release_fwnode:
 	return ret;
 }
 
+static void led_pwm_mc_put_node(void *data)
+{
+	fwnode_handle_put(data);
+}
+
 static int led_pwm_mc_probe(struct platform_device *pdev)
 {
 	struct fwnode_handle *mcnode;
@@ -121,22 +126,22 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, -ENODEV,
 				     "expected multi-led node\n");
 
+	ret = devm_add_action_or_reset(&pdev->dev, led_pwm_mc_put_node, mcnode);
+	if (ret)
+		return ret;
+
 	/* count the nodes inside the multi-led node */
 	count = fwnode_get_child_node_count(mcnode);
 
 	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
 			    GFP_KERNEL);
-	if (!priv) {
-		ret = -ENOMEM;
-		goto release_mcnode;
-	}
+	if (!priv)
+		return -ENOMEM;
 	mutex_init(&priv->lock);
 
 	subled = devm_kcalloc(&pdev->dev, count, sizeof(*subled), GFP_KERNEL);
-	if (!subled) {
-		ret = -ENOMEM;
-		goto release_mcnode;
-	}
+	if (!subled)
+		return -ENOMEM;
 	priv->mc_cdev.subled_info = subled;
 
 	/* init the multicolor's LED class device */
@@ -144,14 +149,14 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 	ret = fwnode_property_read_u32(mcnode, "max-brightness",
 				 &cdev->max_brightness);
 	if (ret)
-		goto release_mcnode;
+		return ret;
 
 	cdev->flags = LED_CORE_SUSPENDRESUME;
 	cdev->brightness_set_blocking = led_pwm_mc_set;
 
 	ret = iterate_subleds(&pdev->dev, priv, mcnode);
 	if (ret)
-		goto release_mcnode;
+		return ret;
 
 	init_data.fwnode = mcnode;
 	ret = devm_led_classdev_multicolor_register_ext(&pdev->dev,
@@ -161,7 +166,7 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev,
 			"failed to register multicolor PWM led for %s: %d\n",
 			cdev->name, ret);
-		goto release_mcnode;
+		return ret;
 	}
 
 	ret = led_pwm_mc_set(cdev, cdev->brightness);
@@ -172,10 +177,6 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, priv);
 	return 0;
-
-release_mcnode:
-	fwnode_handle_put(mcnode);
-	return ret;
 }
 
 static const struct of_device_id of_pwm_leds_mc_match[] = {

base-commit: 2efb7a11f1aa4d2dc285fe2680cca22cb9f72f64

#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 4 lines

led_pwm_mc_probe() leaks the multi-led node reference on successful probe and if the final PWM update fails. LED registration borrows the node, so it must remain valid until the LED is unregistered.

Quoted 8 lines

Register a managed put action before LED registration. Reverse devres order then unregisters the LED before releasing the node on unbind or probe failure. Drop the manual error-path put and return directly on probe errors, leaving the reference cleanup to devres.

Fixes 9fa2762110dd ("leds: Add PWM multicolor driver")
Assisted-by LLM

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Yige Jiang
Quoted 11 lines · Yige Jiang

led_pwm_mc_probe() leaks the multi-led node reference on successful probe and if the final PWM update fails. LED registration borrows the node, so it must remain valid until the LED is unregistered.

Register a managed put action before LED registration. Reverse devres order then unregisters the LED before releasing the node on unbind or probe failure. Drop the manual error-path put and return directly on probe errors, leaving the reference cleanup to devres.

Fixes 9fa2762110dd ("leds: Add PWM multicolor driver")
Assisted-by LLM

I thought we dropped these now?

Quoted 23 lines

---
Changes in v2:
- Capitalize the subject description to follow the LED subsystem style.
- Remove the redundant release_mcnode label and return directly from the probe error paths.

v1: https://lore.kernel.org/r/[email protected]

Based on linux-next next-20261016 (2efb7a11f1aa).
Compile-tested: arm64, GCC 14.2.0, W=1, CONFIG_OF_DYNAMIC=y. COMPILE_TEST=y.
The object was rebuilt from this exact base with v2 applied. No full kernel or module-link, hardware or runtime test was performed.

 drivers/leds/rgb/leds-pwm-multicolor.c | 31 +++++++++++++-------------
 1 file changed, 16 insertions(+), 15 deletions(-)
drivers/leds/rgb/leds-pwm-multicolor.c +0 -0
index 6365459ec27f17187804153cde6cb1805eca33e9..10527a64d1982733a43b76bd8bc123aba95b6f72 100644

drivers/leds/rgb/leds-pwm-multicolor.c release_fwnode: · line 112

Quoted 7 lines
drivers/leds/rgb/leds-pwm-multicolor.c +4 -0
@@ -106,6 +106,11 @@ release_fwnode:
 	return ret;
 }
 
+static void led_pwm_mc_put_node(void *data)
+{
+	fwnode_handle_put(data);
+}

What about?

struct fwnode_handle *mcnode __free(fwnode_handle) =
device_get_named_child_node(&pdev->dev, "multi-led");

Quoted 75 lines
drivers/leds/rgb/leds-pwm-multicolor.c +12 -15
+
 static int led_pwm_mc_probe(struct platform_device *pdev)
 {
 	struct fwnode_handle *mcnode;
@@ -121,22 +126,22 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, -ENODEV,
 				     "expected multi-led node\n");
 
+	ret = devm_add_action_or_reset(&pdev->dev, led_pwm_mc_put_node, mcnode);
+	if (ret)
+		return ret;
+
 	/* count the nodes inside the multi-led node */
 	count = fwnode_get_child_node_count(mcnode);
 
 	priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count),
 			    GFP_KERNEL);
-	if (!priv) {
-		ret = -ENOMEM;
-		goto release_mcnode;
-	}
+	if (!priv)
+		return -ENOMEM;
 	mutex_init(&priv->lock);
 
 	subled = devm_kcalloc(&pdev->dev, count, sizeof(*subled), GFP_KERNEL);
-	if (!subled) {
-		ret = -ENOMEM;
-		goto release_mcnode;
-	}
+	if (!subled)
+		return -ENOMEM;
 	priv->mc_cdev.subled_info = subled;
 
 	/* init the multicolor's LED class device */
@@ -144,14 +149,14 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 	ret = fwnode_property_read_u32(mcnode, "max-brightness",
 				 &cdev->max_brightness);
 	if (ret)
-		goto release_mcnode;
+		return ret;
 
 	cdev->flags = LED_CORE_SUSPENDRESUME;
 	cdev->brightness_set_blocking = led_pwm_mc_set;
 
 	ret = iterate_subleds(&pdev->dev, priv, mcnode);
 	if (ret)
-		goto release_mcnode;
+		return ret;
 
 	init_data.fwnode = mcnode;
 	ret = devm_led_classdev_multicolor_register_ext(&pdev->dev,
@@ -161,7 +166,7 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 		dev_err(&pdev->dev,
 			"failed to register multicolor PWM led for %s: %d\n",
 			cdev->name, ret);
-		goto release_mcnode;
+		return ret;
 	}
 
 	ret = led_pwm_mc_set(cdev, cdev->brightness);
@@ -172,10 +177,6 @@ static int led_pwm_mc_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, priv);
 	return 0;
-
-release_mcnode:
-	fwnode_handle_put(mcnode);
-	return ret;
 }
 
 static const struct of_device_id of_pwm_leds_mc_match[] = {

base-commit: 2efb7a11f1aa4d2dc285fe2680cca22cb9f72f64

Signature · 1 line
Lee Jones
maintainer Applied
#3 lore raw

Re: (subset) [PATCH v2] leds: pwm-multicolor: Manage the multi-led node reference

In reply to Yige Jiang
Quoted 10 lines · Yige Jiang

led_pwm_mc_probe() leaks the multi-led node reference on successful probe and if the final PWM update fails. LED registration borrows the node, so it must remain valid until the LED is unregistered.

Register a managed put action before LED registration. Reverse devres order then unregisters the LED before releasing the node on unbind or probe failure. Drop the manual error-path put and return directly on probe errors, leaving the reference cleanup to devres.

[...]

Applied, thanks!

[1/1] leds: pwm-multicolor: Manage the multi-led node reference

      commit: 68c7c6f993cb3c2d999d843c7e732dc35d432804

--
Lee Jones [李琼斯]