Skip to contentSkip to conversation

media: v4l2-core: Drop manual fwnode_handle_put() via scope-based cleanup

PATCH Superseded Biren Pandya 2 participants 2 messages Jun 19 – Jul 30 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 media: v4l2-core: Drop manual fwnode_handle_put() via scope-based cleanup

Simplify v4l2_subdev_get_fwnode_pad_1_to_1() by converting the local fwnode variable to use the __free(fwnode_handle) scope-based cleanup macro.

This removes the need for manual fwnode_handle_put() calls and naturally ensures the fwnode reference is held during the device_match_fwnode() comparison.

---
Changes in v3:
- Resend as a standalone patch to resolve Media CI robot grouping error caused by duplicate Message-IDs in the v2 thread. No code changes.
- Link to v2: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Updated the commit message to remove the "Use-After-Free" language,
as there is no vulnerability in practice.
- Adjusted the implementation to declare struct fwnode_handle *fwnode directly at the point of initialization, rather than at the top of the block, conforming to recommended usage.
- Renamed the patch subject to better reflect the true nature of the change.
- Link to v1: https://lore.kernel.org/all/[email protected]/

 drivers/media/v4l2-core/v4l2-subdev.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +2 -3
index e6b133ef7850..806b059410ce 100644
@@ -1243,15 +1243,14 @@ const struct v4l2_file_operations v4l2_subdev_fops = {
 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
 				      struct fwnode_endpoint *endpoint)
 {
-	struct fwnode_handle *fwnode;
+	struct fwnode_handle *fwnode __free(fwnode_handle) =
+		fwnode_graph_get_port_parent(endpoint->local_fwnode);
 	struct v4l2_subdev *sd;
 
 	if (!is_media_entity_v4l2_subdev(entity))
 		return -EINVAL;
 
 	sd = media_entity_to_v4l2_subdev(entity);
 
-	fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
-	fwnode_handle_put(fwnode);
 
 	if (device_match_fwnode(sd->dev, fwnode))

return endpoint->port;

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

Hi Biren,

On the subject: s/core/subdev/ .

Quoted 31 lines · Biren Pandya

Simplify v4l2_subdev_get_fwnode_pad_1_to_1() by converting the local fwnode variable to use the __free(fwnode_handle) scope-based cleanup macro.

This removes the need for manual fwnode_handle_put() calls and naturally ensures the fwnode reference is held during the device_match_fwnode() comparison.

---
Changes in v3:
- Resend as a standalone patch to resolve Media CI robot grouping error caused by duplicate Message-IDs in the v2 thread. No code changes.
- Link to v2: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Updated the commit message to remove the "Use-After-Free" language,
as there is no vulnerability in practice.
- Adjusted the implementation to declare struct fwnode_handle *fwnode directly at the point of initialization, rather than at the top of the block, conforming to recommended usage.
- Renamed the patch subject to better reflect the true nature of the change.
- Link to v1: https://lore.kernel.org/all/[email protected]/

 drivers/media/v4l2-core/v4l2-subdev.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-subdev.c +0 -0
index e6b133ef7850..806b059410ce 100644

drivers/media/v4l2-core/v4l2-subdev.c const struct v4l2_file_operations v4l2_subdev_fops = { · line 1246

Quoted 5 lines
drivers/media/v4l2-core/v4l2-subdev.c +1 -1
@@ -1243,15 +1243,14 @@ const struct v4l2_file_operations v4l2_subdev_fops = {
 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
 				      struct fwnode_endpoint *endpoint)
 {
-	struct fwnode_handle *fwnode;
+	struct fwnode_handle *fwnode __free(fwnode_handle) =

__free() is declared in linux/cleanup.h.

drivers/media/v4l2-core/v4l2-subdev.c const struct v4l2_file_operations v4l2_subdev_fops = { · line 1247

Quoted 1 line
drivers/media/v4l2-core/v4l2-subdev.c +1 -0
+		fwnode_graph_get_port_parent(endpoint->local_fwnode);

Can you declare this where fwnode_graph_get_port_parent() was called previously?

Quoted 13 lines
drivers/media/v4l2-core/v4l2-subdev.c +0 -2
 	struct v4l2_subdev *sd;
 
 	if (!is_media_entity_v4l2_subdev(entity))
 		return -EINVAL;
 
 	sd = media_entity_to_v4l2_subdev(entity);
 
-	fwnode = fwnode_graph_get_port_parent(endpoint->local_fwnode);
-	fwnode_handle_put(fwnode);
 
 	if (device_match_fwnode(sd->dev, fwnode))

return endpoint->port;

Signature · 3 lines
Regards,

Sakari Ailus