Skip to contentSkip to conversation

leds: max5970: fix unreleased fwnode_handle in probe function

PATCH Applied Javier Carrasco 2 participants 6 messages Oct 19, 2024 – Oct 31, 2024

Subsystems (2)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover leds: max5970: fix unreleased fwnode_handle in probe function

This series fixes the wrong management of the 'led_node' fwnode_handle, which is not released after it is no longer required. This affects both the normal path of execution and the existing error paths (currently two) in max5970_led_probe().

First, the missing callst to fwnode_handle_put() in the different code paths are added, to make the patch available for stable kernels. Then, the code gets updated to a more robust approach by means of the __free() macro to automatically release the node when it goes out of scope, removing the need for explicit calls to fwnode_handle_put().

---
Javier Carrasco (2):
leds: max5970: fix unreleased fwnode_handle in probe function
leds: max5970: use cleanup facility for fwnode_handle led_node

 drivers/leds/leds-max5970.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

---
base-commit: f2493655d2d3d5c6958ed996b043c821c23ae8d3
change-id: 20241019-max5970-of_node_put-939b004f57d2

Best regards,

Signature · 1 line
Javier Carrasco <[email protected]>
#1 lore raw

PATCH 1/2 leds: max5970: fix unreleased fwnode_handle in probe function

An object initialized via device_get_named_child_node() requires calls to fwnode_handle_put() when it is no longer required to avoid leaking memory.

Add the missing calls to fwnode_handle_put() in the different paths (error paths and normal exit).

Fixes 736214b4b02a ("leds: max5970: Add support for max5970")
---
 drivers/leds/leds-max5970.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/leds/leds-max5970.c +4 -0
index 56a584311581..c021330e0ae7 100644
@@ -72,6 +72,7 @@ static int max5970_led_probe(struct platform_device *pdev)
 
 		ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
 		if (!ddata) {
+			fwnode_handle_put(led_node);
 			fwnode_handle_put(child);
 			return -ENOMEM;
 		}
@@ -89,11 +90,14 @@ static int max5970_led_probe(struct platform_device *pdev)
 
 		ret = devm_led_classdev_register(dev, &ddata->cdev);
 		if (ret < 0) {
+			fwnode_handle_put(led_node);
 			fwnode_handle_put(child);
 			return dev_err_probe(dev, ret, "Failed to initialize LED %u\n", reg);
 		}
 	}
 
+	fwnode_handle_put(led_node);
+
 	return ret;
 }
 
Signature · 1 line
2.43.0
maintainer Applied
#2 lore raw

Re: (subset) [PATCH 1/2] leds: max5970: fix unreleased fwnode_handle in probe function

Quoted 8 lines · Javier Carrasco

An object initialized via device_get_named_child_node() requires calls to fwnode_handle_put() when it is no longer required to avoid leaking memory.

Add the missing calls to fwnode_handle_put() in the different paths (error paths and normal exit).

[...]

Applied, thanks!

[1/2] leds: max5970: fix unreleased fwnode_handle in probe function

      commit: 42c04062ba3cd1f2aef96dc160e0ab4b45b5e10a

--
Lee Jones [李琼斯]

maintainer
#3 lore raw

Re: (subset) [PATCH 1/2] leds: max5970: fix unreleased fwnode_handle in probe function

Quoted 8 lines · Lee Jones
Quoted 7 lines · Javier Carrasco

An object initialized via device_get_named_child_node() requires calls to fwnode_handle_put() when it is no longer required to avoid leaking memory.

Add the missing calls to fwnode_handle_put() in the different paths (error paths and normal exit).

Quoted 6 lines
Quoted 1 line

[...]

Applied, thanks!

[1/2] leds: max5970: fix unreleased fwnode_handle in probe function

      commit: 42c04062ba3cd1f2aef96dc160e0ab4b45b5e10a

Unapplied.

Signature · 1 line
Lee Jones [李琼斯]
#4 lore raw

PATCH 2/2 leds: max5970: use cleanup facility for fwnode_handle led_node

In reply to Javier Carrasco

Add the automatic cleanup facility for 'led_node' to simplify the code and make it more robust against new error paths where omitting a call to fwnode_handle_put() would leak memory.

---
 drivers/leds/leds-max5970.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/leds/leds-max5970.c +3 -6
index c021330e0ae7..285074c53b23 100644
@@ -45,7 +45,7 @@ static int max5970_led_set_brightness(struct led_classdev *cdev,
 
 static int max5970_led_probe(struct platform_device *pdev)
 {
-	struct fwnode_handle *led_node, *child;
+	struct fwnode_handle *child;
 	struct device *dev = &pdev->dev;
 	struct regmap *regmap;
 	struct max5970_led *ddata;
@@ -55,7 +55,8 @@ static int max5970_led_probe(struct platform_device *pdev)
 	if (!regmap)
 		return -ENODEV;
 
-	led_node = device_get_named_child_node(dev->parent, "leds");
+	struct fwnode_handle *led_node __free(fwnode_handle) =
+		device_get_named_child_node(dev->parent, "leds");
 	if (!led_node)
 		return -ENODEV;
 
@@ -72,7 +73,6 @@ static int max5970_led_probe(struct platform_device *pdev)
 
 		ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL);
 		if (!ddata) {
-			fwnode_handle_put(led_node);
 			fwnode_handle_put(child);
 			return -ENOMEM;
 		}
@@ -90,14 +90,11 @@ static int max5970_led_probe(struct platform_device *pdev)
 
 		ret = devm_led_classdev_register(dev, &ddata->cdev);
 		if (ret < 0) {
-			fwnode_handle_put(led_node);
 			fwnode_handle_put(child);
 			return dev_err_probe(dev, ret, "Failed to initialize LED %u\n", reg);
 		}
 	}
 
-	fwnode_handle_put(led_node);
-
 	return ret;
 }
 
Signature · 1 line
2.43.0
maintainer
#5 lore raw

Re: [PATCH 2/2] leds: max5970: use cleanup facility for fwnode_handle led_node

Quoted 8 lines · Javier Carrasco

Add the automatic cleanup facility for 'led_node' to simplify the code and make it more robust against new error paths where omitting a call to fwnode_handle_put() would leak memory.

---
 drivers/leds/leds-max5970.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

This makes patch 1 completely pointless.

Please squash them and resubmit.

Signature · 1 line
Lee Jones [李琼斯]