Skip to contentSkip to conversation

media: i2c: Adopt scope-based fwnode_handle_put

PATCH Superseded Biren Pandya 2 participants 8 messages Jun 15 – Jul 8 1 message not in the archive

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover media: i2c: Adopt scope-based fwnode_handle_put

This series updates the firmware node parsing logic across three highly active V4L2 I2C sensor drivers (ov5640, imx290, and imx219) to utilize the modern __free(fwnode_handle) scoped guard macro introduced in <linux/cleanup.h>.

By binding the lifecycle of the fwnode_handle directly to the variable's scope, we eliminate the need for manual fwnode_handle_put() calls. This permanently removes the risk of subtle early-free or missing-put memory leaks in error-handling paths and establishes a cleaner pattern for newly merged drivers to follow.

Biren Pandya (3):
media: i2c: ov5640: Drop manual fwnode_handle_put() via scope-based
cleanup
media: i2c: imx290: Drop manual fwnode_handle_put() via scope-based
cleanup
media: i2c: imx219: Drop manual fwnode_handle_put() via scope-based
cleanup

 drivers/media/i2c/imx219.c | 6 +++---
 drivers/media/i2c/imx290.c | 6 +++---
 drivers/media/i2c/ov5640.c | 7 +++----
 3 files changed, 9 insertions(+), 10 deletions(-)
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 1/3 media: i2c: ov5640: Drop manual fwnode_handle_put() via scope-based cleanup

Utilize the __free(fwnode_handle) scoped guard macro from <linux/cleanup.h> to automate the lifecycle management of the endpoint fwnode in ov5640_probe().

This eliminates the need for the manual fwnode_handle_put() call, preventing potential memory leaks in error paths and simplifying the probe routine.

---
 drivers/media/i2c/ov5640.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/media/i2c/ov5640.c +3 -4
index ce6b0724afa1..7795f2de0f75 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2014-2017 Mentor Graphics Inc.
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/clk-provider.h>
 #include <linux/clkdev.h>
@@ -3845,7 +3846,6 @@ static int ov5640_check_chip_id(struct ov5640_dev *sensor)
 static int ov5640_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
-	struct fwnode_handle *endpoint;
 	struct ov5640_dev *sensor;
 	int ret;
 
@@ -3870,15 +3870,14 @@ static int ov5640_probe(struct i2c_client *client)
 
 	sensor->ae_target = 52;
 
-	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
-						  NULL);
+	struct fwnode_handle *endpoint __free(fwnode_handle) =
+		fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), NULL);
 	if (!endpoint) {
 		dev_err(dev, "endpoint node not found\n");
 		return -EINVAL;
 	}
 
 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
-	fwnode_handle_put(endpoint);
 	if (ret) {
 		dev_err(dev, "Could not parse endpoint\n");
 		return ret;
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#2 lore raw

Re: [PATCH 1/3] media: i2c: ov5640: Drop manual fwnode_handle_put() via scope-based cleanup

Hi Biren,

Quoted 3 lines · Biren Pandya

Utilize the __free(fwnode_handle) scoped guard macro from <linux/cleanup.h> to automate the lifecycle management of the endpoint fwnode in ov5640_probe().

This eliminates the need for the manual fwnode_handle_put() call, preventing potential memory leaks in error paths and simplifying the probe routine.

Please wrap to 75 characters per line.

Quoted 27 lines
---
 drivers/media/i2c/ov5640.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/media/i2c/ov5640.c +1 -1
index ce6b0724afa1..7795f2de0f75 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2014-2017 Mentor Graphics Inc.
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/clk-provider.h>
 #include <linux/clkdev.h>
@@ -3845,7 +3846,6 @@ static int ov5640_check_chip_id(struct ov5640_dev *sensor)
 static int ov5640_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
-	struct fwnode_handle *endpoint;
 	struct ov5640_dev *sensor;
 	int ret;
 

drivers/media/i2c/ov5640.c static int ov5640_probe(struct i2c_client *client) · line 3874

Quoted 7 lines
drivers/media/i2c/ov5640.c +2 -2
@@ -3870,15 +3870,14 @@ static int ov5640_probe(struct i2c_client *client)
 
 	sensor->ae_target = 52;
 
-	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
-						  NULL);
+	struct fwnode_handle *endpoint __free(fwnode_handle) =
+		fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), NULL);

I'm fine with using __free() here.

drivers/media/i2c/ov5640.c static int ov5640_probe(struct i2c_client *client) · line 3878

Quoted 4 lines
drivers/media/i2c/ov5640.c +0 -0
 	if (!endpoint) {
 		dev_err(dev, "endpoint node not found\n");
 		return -EINVAL;
 	}

This check can be removed in the same patch. It's now redundant as v4l2_fwnode_endpoint_parse() also does it.

Quoted 6 lines
drivers/media/i2c/ov5640.c +0 -1
 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
-	fwnode_handle_put(endpoint);
 	if (ret) {
 		dev_err(dev, "Could not parse endpoint\n");
 		return ret;
Signature · 3 lines
Regards,

Sakari Ailus
#3 lore raw

PATCH 2/3 media: i2c: imx290: Drop manual fwnode_handle_put() via scope-based cleanup

In reply to Biren Pandya

Utilize the __free(fwnode_handle) scoped guard macro from <linux/cleanup.h> to automate the lifecycle management of the endpoint fwnode in imx290_parse_dt().

This safely ties the release of the fwnode_handle to its compiler scope, allowing us to drop the manual fwnode_handle_put() call and removing the risk of missed puts if new early returns are added in the future.

---
 drivers/media/i2c/imx290.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/media/i2c/imx290.c +3 -3
index 21cbc81cb2ed..b0fd3e1dec2e 100644
@@ -8,6 +8,7 @@
  * Author: Manivannan Sadhasivam <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/gpio/consumer.h>
@@ -1514,20 +1515,19 @@ static int imx290_parse_dt(struct imx290 *imx290)
 	struct v4l2_fwnode_endpoint ep = {
 		.bus_type = V4L2_MBUS_CSI2_DPHY
 	};
-	struct fwnode_handle *endpoint;
 	int ret;
 	s64 fq;
 
 	imx290->model = of_device_get_match_data(imx290->dev);
 
-	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(imx290->dev), NULL);
+	struct fwnode_handle *endpoint __free(fwnode_handle) =
+		fwnode_graph_get_next_endpoint(dev_fwnode(imx290->dev), NULL);
 	if (!endpoint) {
 		dev_err(imx290->dev, "Endpoint node not found\n");
 		return -EINVAL;
 	}
 
 	ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep);
-	fwnode_handle_put(endpoint);
 	if (ret == -ENXIO) {
 		dev_err(imx290->dev, "Unsupported bus type, should be CSI2\n");
 		goto done;
Signature · 1 line
2.50.1 (Apple Git-155)
#4 lore raw

PATCH 3/3 media: i2c: imx219: Drop manual fwnode_handle_put() via scope-based cleanup

In reply to Biren Pandya

Utilize the __free(fwnode_handle) scoped guard macro from <linux/cleanup.h> to automate the lifecycle management of the endpoint fwnode in imx219_check_hwcfg().

This inherently guarantees that the endpoint node is released when it goes out of scope. Consequently, the manual fwnode_handle_put() call in the error_out label is no longer needed and has been removed.

---
 drivers/media/i2c/imx219.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/media/i2c/imx219.c +3 -3
index 7da02ce5da15..14dd5acee284 100644
@@ -14,6 +14,7 @@
  *
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/gpio/consumer.h>
@@ -1110,14 +1111,14 @@ static int imx219_identify_module(struct imx219 *imx219)
 
 static int imx219_check_hwcfg(struct device *dev, struct imx219 *imx219)
 {
-	struct fwnode_handle *endpoint;
 	struct v4l2_fwnode_endpoint ep_cfg = {
 		.bus_type = V4L2_MBUS_CSI2_DPHY
 	};
 	unsigned long link_freq_bitmap;
 	int ret = -EINVAL;
 
-	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
+	struct fwnode_handle *endpoint __free(fwnode_handle) =
+		fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
 	if (!endpoint)
 		return dev_err_probe(dev, -EINVAL, "endpoint node not found\n");
 
@@ -1172,7 +1173,6 @@ static int imx219_check_hwcfg(struct device *dev, struct imx219 *imx219)
 
 error_out:
 	v4l2_fwnode_endpoint_free(&ep_cfg);
-	fwnode_handle_put(endpoint);
 
 	return ret;
 }
Signature · 1 line
2.50.1 (Apple Git-155)

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw

PATCH v3 1/3 media: i2c: ov5640: use scoped fwnode_handle endpoint cleanup

Utilize the __free(fwnode_handle) scoped guard macro from <linux/cleanup.h> to automate the lifecycle management of the endpoint fwnode in ov5640_probe().
This eliminates the need for manual fwnode_handle_put() calls. Additionally, drop the redundant !endpoint check before v4l2_fwnode_endpoint_parse(), as the parse function already handles NULL endpoints safely.

---
Changes in v3:
- Resend as a complete series to fix broken threading.
- Moved changelogs here.

Changes in v2:
- Used scoped __free(fwnode_handle) macro (Laurent).
- Dropped redundant !endpoint check in ov5640 (Sakari).

 drivers/media/i2c/ov5640.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/media/i2c/ov5640.c +3 -8
index 8deb5f5501faf..29d4dee9690b9 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2014-2017 Mentor Graphics Inc.
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/clk-provider.h>
 #include <linux/clkdev.h>
@@ -3844,7 +3845,6 @@ static int ov5640_check_chip_id(struct ov5640_dev *sensor)
 static int ov5640_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
-	struct fwnode_handle *endpoint;
 	struct ov5640_dev *sensor;
 	int ret;
 
@@ -3869,15 +3869,10 @@ static int ov5640_probe(struct i2c_client *client)
 
 	sensor->ae_target = 52;
 
-	endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
-						  NULL);
-	if (!endpoint) {
-		dev_err(dev, "endpoint node not found\n");
-		return -EINVAL;
-	}
+	struct fwnode_handle *endpoint __free(fwnode_handle) =
+		fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), NULL);
 
 	ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
-	fwnode_handle_put(endpoint);
 	if (ret) {
 		dev_err(dev, "Could not parse endpoint\n");
 		return ret;
Signature · 1 line
2.50.1 (Apple Git-155)
#7 lore raw

PATCH v3 2/3 media: i2c: imx290: Drop manual fwnode_handle_put() via scope-based cleanup

In reply to a message not in the archive

Utilize the __free(fwnode_handle) scoped guard macro from <linux/cleanup.h> to automate the lifecycle management of the endpoint fwnode in imx290_parse_dt().
This safely ties the release of the fwnode_handle to its compiler scope, allowing us to drop the manual fwnode_handle_put() call and removing the risk of missed puts if new early returns are added in the future.

---
Changes in v3:
- Resend as a complete series to fix broken threading.
- Collected Reviewed-by tag.

 drivers/media/i2c/imx290.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/imx290.c +2 -2
index 21cbc81cb2edc..5c369c7ee21f7 100644
@@ -8,6 +8,7 @@
  * Author: Manivannan Sadhasivam <[email protected]>
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/gpio/consumer.h>
@@ -1514,7 +1515,7 @@ static int imx290_parse_dt(struct imx290 *imx290)
 	struct v4l2_fwnode_endpoint ep = {
 		.bus_type = V4L2_MBUS_CSI2_DPHY
 	};
-	struct fwnode_handle *endpoint;
+	struct fwnode_handle *endpoint __free(fwnode_handle) = NULL;
 	int ret;
 	s64 fq;
 
@@ -1527,7 +1528,6 @@ static int imx290_parse_dt(struct imx290 *imx290)
 	}
 
 	ret = v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep);
-	fwnode_handle_put(endpoint);
 	if (ret == -ENXIO) {
 		dev_err(imx290->dev, "Unsupported bus type, should be CSI2\n");
 		goto done;
Signature · 1 line
2.50.1 (Apple Git-155)
#8 lore raw

PATCH v3 3/3 media: i2c: imx219: Drop manual fwnode_handle_put() via scope-based cleanup

In reply to a message not in the archive

Utilize the __free(fwnode_handle) scoped guard macro from <linux/cleanup.h> to automate the lifecycle management of the endpoint fwnode in imx219_check_hwcfg().
This inherently guarantees that the endpoint node is released when it goes out of scope. Consequently, the manual fwnode_handle_put() call in the error_out label is no longer needed and has been removed.

---
Changes in v3:
- Resend as a complete series to fix broken threading.
- Collected Reviewed-by tag.

 drivers/media/i2c/imx219.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/imx219.c +2 -2
index 7da02ce5da154..d76eae880d730 100644
@@ -14,6 +14,7 @@
  *
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/gpio/consumer.h>
@@ -1110,7 +1111,7 @@ static int imx219_identify_module(struct imx219 *imx219)
 
 static int imx219_check_hwcfg(struct device *dev, struct imx219 *imx219)
 {
-	struct fwnode_handle *endpoint;
+	struct fwnode_handle *endpoint __free(fwnode_handle) = NULL;
 	struct v4l2_fwnode_endpoint ep_cfg = {
 		.bus_type = V4L2_MBUS_CSI2_DPHY
 	};
@@ -1172,7 +1173,6 @@ static int imx219_check_hwcfg(struct device *dev, struct imx219 *imx219)
 
 error_out:
 	v4l2_fwnode_endpoint_free(&ep_cfg);
-	fwnode_handle_put(endpoint);
 
 	return ret;
 }
Signature · 1 line
2.50.1 (Apple Git-155)