Skip to contentSkip to conversation

extcon: realtek: Use scope-based resource management in extcon_rtk_type_c_init()

PATCH In review Markus Elfring 2 participants 3 messages Jun 4, 2024 – Jun 29, 2024

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

PATCH 1/1 extcon: realtek: Use scope-based resource management in extcon_rtk_type_c_init()

From: Markus Elfring <[email protected]>
Date: Tue, 4 Jun 2024 21:55:08 +0200

Scope-based resource management became supported also for another programming interface by contributions of Jonathan Cameron on 2024-02-17. See also the commit 59ed5e2d505bf5f9b4af64d0021cd0c96aec1f7c ("device
property: Add cleanup.h based fwnode_handle_put() scope based cleanup.").

* Thus use the attribute “__free(fwnode_handle)”.

* Reduce the scope for the local variable “fwnode”.

Fixes 8a590d7371f0 ("extcon: add Realtek DHC RTD SoC Type-C driver")
---
 drivers/extcon/extcon-rtk-type-c.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/extcon/extcon-rtk-type-c.c +3 -2
index 19a01e663733..8a4b074fd745 100644
@@ -18,6 +18,7 @@
 #include <linux/syscalls.h>
 #include <linux/suspend.h>
 #include <linux/debugfs.h>
+#include <linux/property.h>
 #include <linux/extcon.h>
 #include <linux/extcon-provider.h>
 #include <linux/sys_soc.h>
@@ -1237,7 +1238,6 @@ static int extcon_rtk_type_c_init(struct type_c_data *type_c)
 
 	if (!type_c->port) {
 		struct typec_capability typec_cap = { };
-		struct fwnode_handle *fwnode;
 		const char *buf;
 		int ret;
 
@@ -1246,7 +1246,8 @@ static int extcon_rtk_type_c_init(struct type_c_data *type_c)
 		typec_cap.driver_data = type_c;
 		typec_cap.ops = &type_c_port_ops;
 
-		fwnode = device_get_named_child_node(dev, "connector");
+		struct fwnode_handle *fwnode __free(fwnode_handle)
+					     = device_get_named_child_node(dev, "connector");
 		if (!fwnode)
 			return -EINVAL;
 

--
2.45.1

#1 lore raw
Quoted 8 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 4 Jun 2024 21:55:08 +0200

Scope-based resource management became supported also for another programming interface by contributions of Jonathan Cameron on 2024-02-17. See also the commit 59ed5e2d505bf5f9b4af64d0021cd0c96aec1f7c ("device
property: Add cleanup.h based fwnode_handle_put() scope based cleanup.").

Quoted 5 lines

* Thus use the attribute “__free(fwnode_handle)”.

* Reduce the scope for the local variable “fwnode”.

Fixes 8a590d7371f0 ("extcon: add Realtek DHC RTD SoC Type-C driver")

The problem here is that the above mentioned API only recently appeared in the kernel. It will be impossible to backport without backporting a new API, which is usually contradicts the idea of stable kernels. That's why it's better to really fix without using a new APIs and then convert to a new one. Hence needs two patches.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw

PATCH v2 1/1 extcon: realtek: Prevent resource leak in extcon_rtk_type_c_init()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Sat, 29 Jun 2024 11:30:06 +0200

The API documentation for the function “device_get_named_child_node” contains the following information:
“…
The caller is responsible for calling fwnode_handle_put() on the returned fwnode pointer.
…”

The mentioned requirement was not fulfilled so that a resource leak will occur in error cases after a device_get_named_child_node() call succeeded in this function implementation.

Thus apply scope-based resource management which became supported also for this programming interface by contributions of Jonathan Cameron on 2024-02-17.
See also the commit 59ed5e2d505bf5f9b4af64d0021cd0c96aec1f7c ("device
property: Add cleanup.h based fwnode_handle_put() scope based cleanup.").

* Use the attribute “__free(fwnode_handle)” accordingly.

* Reduce the scope for the local variable “fwnode”.

Fixes 8a590d7371f0 ("extcon: add Realtek DHC RTD SoC Type-C driver")

---

V2:
* The change description was extended.

* The summary phrase was adjusted.

* A “stable tag” was added.

I guess that clarifications can become interesting also for backporting concerns because of the proposed software transformation.

 drivers/extcon/extcon-rtk-type-c.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/extcon/extcon-rtk-type-c.c +3 -2
index 19a01e663733..8a4b074fd745 100644
@@ -18,6 +18,7 @@
 #include <linux/syscalls.h>
 #include <linux/suspend.h>
 #include <linux/debugfs.h>
+#include <linux/property.h>
 #include <linux/extcon.h>
 #include <linux/extcon-provider.h>
 #include <linux/sys_soc.h>
@@ -1237,7 +1238,6 @@ static int extcon_rtk_type_c_init(struct type_c_data *type_c)
 
 	if (!type_c->port) {
 		struct typec_capability typec_cap = { };
-		struct fwnode_handle *fwnode;
 		const char *buf;
 		int ret;
 
@@ -1246,7 +1246,8 @@ static int extcon_rtk_type_c_init(struct type_c_data *type_c)
 		typec_cap.driver_data = type_c;
 		typec_cap.ops = &type_c_port_ops;
 
-		fwnode = device_get_named_child_node(dev, "connector");
+		struct fwnode_handle *fwnode __free(fwnode_handle)
+					     = device_get_named_child_node(dev, "connector");
 		if (!fwnode)
 			return -EINVAL;
 

--
2.45.2