Skip to contentSkip to conversation

of.c: replace of_node_put with __free improves cleanup

PATCH In review David Hunter 4 participants 4 messages Jul 19, 2024 – Aug 7, 2024 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 of.c: replace of_node_put with __free improves cleanup

The use of the __free function allows the cleanup to be based on scope instead of on another function called later. This makes the cleanup automatic and less susceptible to errors later.

This code was compiled without errors or warnings.

---
 drivers/pci/of.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/pci/of.c +1 -3
index b908fe1ae951..8b150982f5cd 100644
@@ -616,16 +616,14 @@ int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
 
 void of_pci_remove_node(struct pci_dev *pdev)
 {
-	struct device_node *np;
+	struct device_node *np __free(device_node) = pci_device_to_OF_node(pdev);
 
-	np = pci_device_to_OF_node(pdev);
 	if (!np || !of_node_check_flag(np, OF_DYNAMIC))
 		return;
 	pdev->dev.of_node = NULL;
 
 	of_changeset_revert(np->data);
 	of_changeset_destroy(np->data);
-	of_node_put(np);
 }
 
 void of_pci_make_dev_node(struct pci_dev *pdev)
Signature · 1 line
2.34.1
#1 lore raw

[+cc Rob, Jonathan]

Quoted 5 lines · David Hunter

The use of the __free function allows the cleanup to be based on scope instead of on another function called later. This makes the cleanup automatic and less susceptible to errors later.

This code was compiled without errors or warnings.

I *think* this looks OK, but I'm not comfy with all this scope magic yet, so would like Jonathan and/or Rob to take a peek too.

And is there some way to include a hint here about how to find the implicit of_node_put()? I think it's this from 9448e55d032d ("of: Add cleanup.h based auto release via __free(device_node) markings"):

+DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T))

but it did take some looking to find it.

If it looks good, I'll tweak the commit log to use imperative mood:
https://chris.beams.io/posts/git-commit/
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id=v6.9#n94

since this technically says what *could* happen but not what the patch *does*.

Quoted 30 lines
---
 drivers/pci/of.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/pci/of.c +1 -3
index b908fe1ae951..8b150982f5cd 100644
@@ -616,16 +616,14 @@ int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
 
 void of_pci_remove_node(struct pci_dev *pdev)
 {
-	struct device_node *np;
+	struct device_node *np __free(device_node) = pci_device_to_OF_node(pdev);
 
-	np = pci_device_to_OF_node(pdev);
 	if (!np || !of_node_check_flag(np, OF_DYNAMIC))
 		return;
 	pdev->dev.of_node = NULL;
 
 	of_changeset_revert(np->data);
 	of_changeset_destroy(np->data);
-	of_node_put(np);
 }
 
 void of_pci_make_dev_node(struct pci_dev *pdev)
Signature · 1 line
2.34.1
#2 lore raw
Quoted 11 lines · On Thu, 1 Aug 2024 18:55:26 -0500 Bjorn Helgaas <[email protected]> wrote:

[+cc Rob, Jonathan]

Quoted 5 lines · David Hunter

The use of the __free function allows the cleanup to be based on scope instead of on another function called later. This makes the cleanup automatic and less susceptible to errors later.

This code was compiled without errors or warnings.

I *think* this looks OK, but I'm not comfy with all this scope magic yet, so would like Jonathan and/or Rob to take a peek too.

I'm suspicious of usecases where there isn't a constructor / destructor pair.

This is more of a 'steal' the pointer and destroy it pattern.

Also, bug in this case.... see below.

Quoted 6 lines

And is there some way to include a hint here about how to find the implicit of_node_put()? I think it's this from 9448e55d032d ("of: Add cleanup.h based auto release via __free(device_node) markings"):

+DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T))

Yes, it's that one. Makes sense to add a reference to that in the patch description for these.

Quoted 21 lines

but it did take some looking to find it.

If it looks good, I'll tweak the commit log to use imperative mood:
https://chris.beams.io/posts/git-commit/
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id=v6.9#n94

since this technically says what *could* happen but not what the patch *does*.

Quoted 11 lines
---
 drivers/pci/of.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/pci/of.c +0 -0
index b908fe1ae951..8b150982f5cd 100644
@@ -616,16 +616,14 @@ int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
 
Quoted 7 lines

drivers/pci/of.c int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge) · line 621

Quoted 7 lines
drivers/pci/of.c +1 -2
 void of_pci_remove_node(struct pci_dev *pdev)
 {
-	struct device_node *np;
+	struct device_node *np __free(device_node) = pci_device_to_OF_node(pdev);
 
-	np = pci_device_to_OF_node(pdev);
 	if (!np || !of_node_check_flag(np, OF_DYNAMIC))

Wil now put the node if that second check fails. Didn't do that before and I'm guessing we shouldn't? Technically it calls the cleanup in the !np case but that is fine as we check for NULL pointer.

So I'd leave this particular one alone.

Quoted 12 lines
Quoted 12 lines
drivers/pci/of.c +0 -1
 		return;
 	pdev->dev.of_node = NULL;
 
 	of_changeset_revert(np->data);
 	of_changeset_destroy(np->data);
-	of_node_put(np);
 }
 
 void of_pci_make_dev_node(struct pci_dev *pdev)
Signature · 1 line
2.34.1
#3 lore raw
Quoted 57 lines · Jonathan Cameron
Quoted 11 lines · On Thu, 1 Aug 2024 18:55:26 -0500 Bjorn Helgaas <[email protected]> wrote:

[+cc Rob, Jonathan]

Quoted 5 lines · David Hunter

The use of the __free function allows the cleanup to be based on scope instead of on another function called later. This makes the cleanup automatic and less susceptible to errors later.

This code was compiled without errors or warnings.

I *think* this looks OK, but I'm not comfy with all this scope magic yet, so would like Jonathan and/or Rob to take a peek too.

I'm suspicious of usecases where there isn't a constructor / destructor pair.

This is more of a 'steal' the pointer and destroy it pattern.

Also, bug in this case.... see below.

Quoted 6 lines

And is there some way to include a hint here about how to find the implicit of_node_put()? I think it's this from 9448e55d032d ("of: Add cleanup.h based auto release via __free(device_node) markings"):

+DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T))

Yes, it's that one. Makes sense to add a reference to that in the patch description for these.

Quoted 26 lines

but it did take some looking to find it.

If it looks good, I'll tweak the commit log to use imperative mood:
https://chris.beams.io/posts/git-commit/
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id=v6.9#n94

since this technically says what *could* happen but not what the patch *does*.

Quoted 16 lines
---
 drivers/pci/of.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/pci/of.c +1 -1
index b908fe1ae951..8b150982f5cd 100644
@@ -616,16 +616,14 @@ int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
 
 void of_pci_remove_node(struct pci_dev *pdev)
 {
-   struct device_node *np;
+   struct device_node *np __free(device_node) = pci_device_to_OF_node(pdev);
 
Quoted 8 lines
Quoted 2 lines

drivers/pci/of.c int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge) · line 621

Quoted 2 lines
drivers/pci/of.c +0 -1
-   np = pci_device_to_OF_node(pdev);
    if (!np || !of_node_check_flag(np, OF_DYNAMIC))

Wil now put the node if that second check fails. Didn't do that before and I'm guessing we shouldn't? Technically it calls the cleanup in the !np case but that is fine as we check for NULL pointer.

So I'd leave this particular one alone.

Right. The pci_device_to_OF_node() doesn't do a get, so using __free() isn't really appropriate here. The put here is to free the node. The get to balance the put was the allocation of the node.

Rob