Skip to contentSkip to conversation

usb: typec: anx7411: Use scope-based resource management in anx7411_typec_port_probe()

PATCH In review Markus Elfring 3 participants 6 messages Jun 5, 2024 – Jun 28, 2024

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

PATCH 1/1 usb: typec: anx7411: Use scope-based resource management in anx7411_typec_port_probe()

From: Markus Elfring <[email protected]>
Date: Wed, 5 Jun 2024 18:56:19 +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 fe6d8a9c8e64 ("usb: typec: anx7411: Add Analogix PD ANX7411 support")
---
 drivers/usb/typec/anx7411.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/usb/typec/anx7411.c +4 -3
index b12a07edc71b..9fb52f233a30 100644
@@ -16,6 +16,7 @@
 #include <linux/of_graph.h>
 #include <linux/of_platform.h>
 #include <linux/pm_runtime.h>
+#include <linux/property.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/types.h>
@@ -1142,11 +1143,11 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
 {
 	struct typec_capability *cap = &ctx->typec.caps;
 	struct typec_params *typecp = &ctx->typec;
-	struct fwnode_handle *fwnode;
 	const char *buf;
 	int ret, i;
 
-	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;
 
@@ -1237,7 +1238,7 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
 		typecp->caps_flags |= HAS_SINK_WATT;
 	}
 
-	cap->fwnode = fwnode;
+	cap->fwnode = no_free_ptr(fwnode);
 
 	ctx->typec.role_sw = usb_role_switch_get(dev);
 	if (IS_ERR(ctx->typec.role_sw)) {

--
2.45.1

maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 5 Jun 2024 18:56:19 +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 6 lines

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

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

Fixes fe6d8a9c8e64 ("usb: typec: anx7411: Add Analogix PD ANX7411 support")

Was the fwnode leaked, or why else is this a "fix"? It's not clear from the commit message. In any case:

Quoted 41 lines
---
 drivers/usb/typec/anx7411.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/usb/typec/anx7411.c +4 -4
index b12a07edc71b..9fb52f233a30 100644
@@ -16,6 +16,7 @@
 #include <linux/of_graph.h>
 #include <linux/of_platform.h>
 #include <linux/pm_runtime.h>
+#include <linux/property.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/types.h>
@@ -1142,11 +1143,11 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
 {
 	struct typec_capability *cap = &ctx->typec.caps;
 	struct typec_params *typecp = &ctx->typec;
-	struct fwnode_handle *fwnode;
 	const char *buf;
 	int ret, i;
 
-	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;
 
@@ -1237,7 +1238,7 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
 		typecp->caps_flags |= HAS_SINK_WATT;
 	}
 
-	cap->fwnode = fwnode;
+	cap->fwnode = no_free_ptr(fwnode);
 
 	ctx->typec.role_sw = usb_role_switch_get(dev);
 	if (IS_ERR(ctx->typec.role_sw)) {
--

2.45.1

Signature · 1 line
heikki
#2 lore raw
Quoted 7 lines
Quoted 7 lines

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)”.

Quoted 7 lines
Quoted 4 lines

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

Fixes fe6d8a9c8e64 ("usb: typec: anx7411: Add Analogix PD ANX7411 support")

Was the fwnode leaked, or why else is this a "fix"? It's not clear from the commit message.

Can you notice that fwnode_handle_put() calls were forgotten in the mentioned function implementation?
https://elixir.bootlin.com/linux/v6.10-rc2/source/drivers/usb/typec/anx7411.c#L1140

I propose another code cleanup accordingly.

Will development attention grow anyhow for information in the available API documentation?

Example:
device_get_named_child_node()
https://elixir.bootlin.com/linux/v6.10-rc2/source/drivers/base/property.c#L839

Regards,
Markus

#3 lore raw
In reply to Markus Elfring
Quoted 8 lines · On Wed, 5 Jun 2024 19:11:04 +0200 Markus Elfring <[email protected]> wrote:

From: Markus Elfring <[email protected]>
Date: Wed, 5 Jun 2024 18:56:19 +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 6 lines

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

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

Fixes fe6d8a9c8e64 ("usb: typec: anx7411: Add Analogix PD ANX7411 support")

Hi Markus,

Good catch. However in this case I think this is insufficient. Also your patch description should more clearly state the bug rather and impacts (a potential resource leak).

I'm not 100% sure how this should work though.

If the expectation is that a reference to the fwnode is held when we enter typec_register_port(), then if that errors out then we need fwnode_handle_put().

If expectation is that the reference is not held, then we should always call fwnode_handle_put() before that is called. Internally it just uses this to fill in port->dev.fwnode.

Given typec_get_fw_cap() is called from there and doesn't get a reference I think expectation is that the fwnode is held by the driver calling typec_register_port() until that is unregistered.

Hence should be put in the error path of that call in the calling driver.

	ctx->typec.port = typec_register_port(dev, cap);
	if (IS_ERR(ctx->typec.port)) {
		// fwnode_handle_put() in here.
 		ret = PTR_ERR(ctx->typec.port);
		ctx->typec.port = NULL;
		dev_err(dev, "Failed to register type c port %d\n", ret);
		return ret;
	}

That makes it tricky to use no_free_ptr() so I wonder if this is a case where the old fashioned fix of adding all the relevant fwnode_handle_put() calls is the better option. The __free() approach doesn't always fit.

Jonathan

Quoted 42 lines
---
 drivers/usb/typec/anx7411.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/usb/typec/anx7411.c +4 -4
index b12a07edc71b..9fb52f233a30 100644
@@ -16,6 +16,7 @@
 #include <linux/of_graph.h>
 #include <linux/of_platform.h>
 #include <linux/pm_runtime.h>
+#include <linux/property.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/types.h>
@@ -1142,11 +1143,11 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
 {
 	struct typec_capability *cap = &ctx->typec.caps;
 	struct typec_params *typecp = &ctx->typec;
-	struct fwnode_handle *fwnode;
 	const char *buf;
 	int ret, i;
 
-	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;
 
@@ -1237,7 +1238,7 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
 		typecp->caps_flags |= HAS_SINK_WATT;
 	}
 
-	cap->fwnode = fwnode;
+	cap->fwnode = no_free_ptr(fwnode);
 
 	ctx->typec.role_sw = usb_role_switch_get(dev);
 	if (IS_ERR(ctx->typec.role_sw)) {
--

2.45.1

#4 lore raw
Quoted 4 lines

That makes it tricky to use no_free_ptr() so I wonder if this is a case where the old fashioned fix of adding all the relevant fwnode_handle_put() calls is the better option. The __free() approach doesn't always fit.

Can involved development concerns be clarified better anyhow?

Regards,
Markus

#5 lore raw

PATCH 1/1 usb: typec: anx7411: Use scope-based resource management in anx7411_typec_port_probe()

In reply to Jonathan Cameron

From: Markus Elfring <[email protected]>
Date: Fri, 28 Jun 2024 08:25:34 +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 some 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 fe6d8a9c8e64 ("usb: typec: anx7411: Add Analogix PD ANX7411 support")

---

V2:
* The change description was extended as requested by patch reviewers.

* The tag “Reviewed-by” was added according to feedback by Heikki Krogerus.

* A “stable tag” was added.

V1:
Initial patch review
https://lore.kernel.org/r/[email protected]/
https://lkml.org/lkml/2024/6/5/1276

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

 drivers/usb/typec/anx7411.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/usb/typec/anx7411.c +4 -3
index b12a07edc71b..9fb52f233a30 100644
@@ -16,6 +16,7 @@
 #include <linux/of_graph.h>
 #include <linux/of_platform.h>
 #include <linux/pm_runtime.h>
+#include <linux/property.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/types.h>
@@ -1142,11 +1143,11 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
 {
 	struct typec_capability *cap = &ctx->typec.caps;
 	struct typec_params *typecp = &ctx->typec;
-	struct fwnode_handle *fwnode;
 	const char *buf;
 	int ret, i;
 
-	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;
 
@@ -1237,7 +1238,7 @@ static int anx7411_typec_port_probe(struct anx7411_data *ctx,
 		typecp->caps_flags |= HAS_SINK_WATT;
 	}
 
-	cap->fwnode = fwnode;
+	cap->fwnode = no_free_ptr(fwnode);
 
 	ctx->typec.role_sw = usb_role_switch_get(dev);
 	if (IS_ERR(ctx->typec.role_sw)) {

--
2.45.2