Skip to contentSkip to conversation

media: i2c: Automate fwnode lifecycle with __free() scope guards

PATCH Superseded Biren Pandya 4 participants 11 messages Jun 16 – Jul 8

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover media: i2c: Automate fwnode lifecycle with __free() scope guards

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 | 4 ++--
 drivers/media/i2c/imx290.c | 4 ++--
 drivers/media/i2c/ov5640.c | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)

base-commit: 424280953322cf66314f3ba5e2d1ef345f21c770

Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH v2 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 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/ov5640.c +2 -2
index 85ecc23b3587..418cade11d00 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,7 @@ 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 fwnode_handle *endpoint __free(fwnode_handle) = NULL;
 	struct ov5640_dev *sensor;
 	int ret;
 
@@ -3878,7 +3879,6 @@ static int ov5640_probe(struct i2c_client *client)
 	}
 
 	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 v2 1/3] media: i2c: ov5640: Drop manual fwnode_handle_put() via scope-based cleanup

Hi Biren,

Quoted 26 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.

---
 drivers/media/i2c/ov5640.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/ov5640.c +1 -0
index 85ecc23b3587..418cade11d00 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>

drivers/media/i2c/ov5640.c static int ov5640_check_chip_id(struct ov5640_dev *sensor) · line 3849

Quoted 5 lines
drivers/media/i2c/ov5640.c +1 -1
@@ -3845,7 +3846,7 @@ 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 fwnode_handle *endpoint __free(fwnode_handle) = NULL;

The recommended pattern in __free() use is to assign something else than NULL to it in declaration unless required otherwise.

Quoted 11 lines
drivers/media/i2c/ov5640.c +0 -1
 	struct ov5640_dev *sensor;
 	int ret;
 
@@ -3878,7 +3879,6 @@ static int ov5640_probe(struct i2c_client *client)
 	}
 
 	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 v3 1/1 media: i2c: ov5640: use scoped fwnode_handle endpoint 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 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.

---
v3: adopted __free scoped guard (Laurent); explicitly dropped redundant endpoint check (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)
maintainer
#4 lore raw

Re: [PATCH v3] media: i2c: ov5640: use scoped fwnode_handle endpoint cleanup

Hi Biren,

Quoted 4 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().

Quoted 8 lines

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.

---
v3: adopted __free scoped guard (Laurent); explicitly dropped redundant endpoint check (Sakari).

Please don't include git tags in changelog; git am will pick the lines as tags to the changelog.

The threading appears to be broken in the set; please resend v3 (three patches) as you sent v2.

Signature · 3 lines
Kind regards,

Sakari Ailus
#5 lore raw

PATCH v2 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 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/imx290.c +2 -2
index 21cbc81cb2ed..5c369c7ee21f 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)
maintainer Reviewed-by on v2 2/3
#6 lore raw

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

Quoted 10 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 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.

- Mani

Quoted 36 lines
---
 drivers/media/i2c/imx290.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/imx290.c +2 -2
index 21cbc81cb2ed..5c369c7ee21f 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)
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#7 lore raw

PATCH v3 1/1 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.

v3: picked up Reviewed-by tag.

---
v3: picked up 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 v2 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 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/imx219.c +2 -2
index 7da02ce5da15..d76eae880d73 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)
maintainer Reviewed-by on v2 3/3
#9 lore raw

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

Quoted 10 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 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.

Quoted 36 lines
---
 drivers/media/i2c/imx219.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/imx219.c +2 -3
index 7da02ce5da15..d76eae880d73 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;
 }
--

2.50.1 (Apple Git-155)

#10 lore raw

PATCH v3 1/1 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.

v3: picked up Reviewed-by tag.

---
v3: picked up 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)