Skip to contentSkip to conversation

extcon: rtk-type-c, ptn5150: fix connector fwnode_handle leaks

PATCH In review Selby Mashiki 2 participants 2 messages Oct 25

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

PATCH 1/1 extcon: rtk-type-c, ptn5150: fix connector fwnode_handle leaks

In extcon_rtk_type_c_init(), device_get_named_child_node(dev, "connector") acquires a reference to the connector fwnode_handle that is never released on any return path.

Similarly, in ptn5150_i2c_probe(), device_get_named_child_node(dev, "connector") acquires a reference to connector that is passed to fwnode_typec_switch_get() and fwnode_usb_role_switch_get() and then leaked when the function returns.

Mark both variables with __free(fwnode_handle) so the references are automatically released on scope exit.

Fixes 8a590d7371f0 ("extcon: add Realtek DHC RTD SoC Type-C driver")
Fixes 842546c56345 ("extcon: ptn5150: Add Type-C orientation switch support")
---
 drivers/extcon/extcon-ptn5150.c    |  2 +-
 drivers/extcon/extcon-rtk-type-c.c | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/extcon/extcon-ptn5150.c +1 -1
index eca1b140a..b5e1a3f3d 100644
@@ -256,7 +256,7 @@ static int ptn5150_i2c_probe(struct i2c_client *i2c)
 {
 	struct device *dev = &i2c->dev;
 	struct device_node *np = i2c->dev.of_node;
-	struct fwnode_handle *connector;
+	struct fwnode_handle *connector __free(fwnode_handle) = NULL;
 	struct ptn5150_info *info;
 	int ret;
 
drivers/extcon/extcon-rtk-type-c.c +5 -5
index 82b60b927..4b1fc084f 100644
@@ -1236,20 +1236,20 @@ static int extcon_rtk_type_c_init(struct type_c_data *type_c)
 	schedule_delayed_work(&type_c->delayed_work, msecs_to_jiffies(0));
 
 	if (!type_c->port) {
+		struct fwnode_handle *fwnode __free(fwnode_handle) =
+			device_get_named_child_node(dev, "connector");
 		struct typec_capability typec_cap = { };
-		struct fwnode_handle *fwnode;
 		const char *buf;
 		int ret;
 
+		if (!fwnode)
+			return -EINVAL;
+
 		typec_cap.revision = USB_TYPEC_REV_1_0;
 		typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
 		typec_cap.driver_data = type_c;
 		typec_cap.ops = &type_c_port_ops;
 
-		fwnode = device_get_named_child_node(dev, "connector");
-		if (!fwnode)
-			return -EINVAL;
-
 		ret = fwnode_property_read_string(fwnode, "power-role", &buf);
 		if (ret) {
 			dev_err(dev, "power-role not found: %d\n", ret);
Signature · 1 line
2.56.0
#1 lore raw
Quoted 25 lines · Selby Mashiki

In extcon_rtk_type_c_init(), device_get_named_child_node(dev, "connector") acquires a reference to the connector fwnode_handle that is never released on any return path.

Similarly, in ptn5150_i2c_probe(), device_get_named_child_node(dev, "connector") acquires a reference to connector that is passed to fwnode_typec_switch_get() and fwnode_usb_role_switch_get() and then leaked when the function returns.

Mark both variables with __free(fwnode_handle) so the references are automatically released on scope exit.

Fixes 8a590d7371f0 ("extcon: add Realtek DHC RTD SoC Type-C driver")
Fixes 842546c56345 ("extcon: ptn5150: Add Type-C orientation switch support")
---
 drivers/extcon/extcon-ptn5150.c    |  2 +-
 drivers/extcon/extcon-rtk-type-c.c | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/extcon/extcon-ptn5150.c +0 -0
index eca1b140a..b5e1a3f3d 100644

drivers/extcon/extcon-ptn5150.c static int ptn5150_i2c_probe(struct i2c_client *i2c) · line 259

Quoted 5 lines
drivers/extcon/extcon-ptn5150.c +1 -1
@@ -256,7 +256,7 @@ static int ptn5150_i2c_probe(struct i2c_client *i2c)
 {
 	struct device *dev = &i2c->dev;
 	struct device_node *np = i2c->dev.of_node;
-	struct fwnode_handle *connector;
+	struct fwnode_handle *connector __free(fwnode_handle) = NULL;

Why is the constructor empty? I cannot find any multi-assignment, which would lead to exceptions. Did you read description of cleanup usage?

drivers/extcon/extcon-rtk-type-c.c static int extcon_rtk_type_c_init(struct type_c_data *type_c) · line 1240

Quoted 13 lines
drivers/extcon/extcon-ptn5150.c +0 -0
 	struct ptn5150_info *info;
 	int ret;
 
drivers/extcon/extcon-rtk-type-c.c +2 -0
index 82b60b927..4b1fc084f 100644
@@ -1236,20 +1236,20 @@ static int extcon_rtk_type_c_init(struct type_c_data *type_c)
 	schedule_delayed_work(&type_c->delayed_work, msecs_to_jiffies(0));
 
 	if (!type_c->port) {
+		struct fwnode_handle *fwnode __free(fwnode_handle) =
+			device_get_named_child_node(dev, "connector");

This is odd. Why do you move it to the top?

drivers/extcon/extcon-rtk-type-c.c static int extcon_rtk_type_c_init(struct type_c_data *type_c) · line 1245

Quoted 6 lines
drivers/extcon/extcon-rtk-type-c.c +1 -1
 		struct typec_capability typec_cap = { };
-		struct fwnode_handle *fwnode;
 		const char *buf;
 		int ret;
 
+		if (!fwnode)

Here is the usage.

drivers/extcon/extcon-rtk-type-c.c static int extcon_rtk_type_c_init(struct type_c_data *type_c) · line 1247

Quoted 2 lines
drivers/extcon/extcon-rtk-type-c.c +2 -0
+			return -EINVAL;
+

cleanup.h is not always making code readable, so please avoid it if you do not intend to read its description how it supposed to be used.

Best regards,
Krzysztof