2023-09-15 21:24:27

by Lizhi Hou

[permalink] [raw]
Subject: [PATCH 1/2] PCI: of: Fix memory leak when of_changeset_create_node() failed

Destroy and free cset when failure happens.

Fixes: 407d1a51921e ("PCI: Create device tree node for bridge")
Reported-by: Herve Codina <[email protected]>
Closes: https://lore.kernel.org/all/[email protected]/
Signed-off-by: Lizhi Hou <[email protected]>
---
drivers/pci/of.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/drivers/pci/of.c b/drivers/pci/of.c
index 2af64bcb7da3..498b5cae8bca 100644
--- a/drivers/pci/of.c
+++ b/drivers/pci/of.c
@@ -657,30 +657,33 @@ void of_pci_make_dev_node(struct pci_dev *pdev)

cset = kmalloc(sizeof(*cset), GFP_KERNEL);
if (!cset)
- goto failed;
+ goto failed_alloc_cset;
of_changeset_init(cset);

np = of_changeset_create_node(cset, ppnode, name);
if (!np)
- goto failed;
- np->data = cset;
+ goto failed_create_node;

ret = of_pci_add_properties(pdev, cset, np);
if (ret)
- goto failed;
+ goto failed_add_prop;

ret = of_changeset_apply(cset);
if (ret)
- goto failed;
+ goto failed_add_prop;

+ np->data = cset;
pdev->dev.of_node = np;
kfree(name);

return;

-failed:
- if (np)
- of_node_put(np);
+failed_add_prop:
+ of_node_put(np);
+failed_create_node:
+ of_changeset_destroy(cset);
+ kfree(cset);
+failed_alloc_cset:
kfree(name);
}
#endif
--
2.34.1


2023-09-28 11:52:35

by Herve Codina

[permalink] [raw]
Subject: Re: [PATCH 1/2] PCI: of: Fix memory leak when of_changeset_create_node() failed

Hi Lizhi,

On Fri, 15 Sep 2023 11:08:06 -0700
Lizhi Hou <[email protected]> wrote:

> Destroy and free cset when failure happens.
>
> Fixes: 407d1a51921e ("PCI: Create device tree node for bridge")
> Reported-by: Herve Codina <[email protected]>
> Closes: https://lore.kernel.org/all/[email protected]/
> Signed-off-by: Lizhi Hou <[email protected]>
> ---
> drivers/pci/of.c | 19 +++++++++++--------
> 1 file changed, 11 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/pci/of.c b/drivers/pci/of.c
> index 2af64bcb7da3..498b5cae8bca 100644
> --- a/drivers/pci/of.c
> +++ b/drivers/pci/of.c
> @@ -657,30 +657,33 @@ void of_pci_make_dev_node(struct pci_dev *pdev)
>
> cset = kmalloc(sizeof(*cset), GFP_KERNEL);
> if (!cset)
> - goto failed;
> + goto failed_alloc_cset;

Usually goto labels indicate what the goto does instead of where we come
from.
https://elixir.bootlin.com/linux/v6.5/source/Documentation/process/coding-style.rst#L536

In this case, it should be "goto failed_free_name".

> of_changeset_init(cset);
>
> np = of_changeset_create_node(cset, ppnode, name);
> if (!np)
> - goto failed;
> - np->data = cset;
> + goto failed_create_node;

same comment

>
> ret = of_pci_add_properties(pdev, cset, np);
> if (ret)
> - goto failed;
> + goto failed_add_prop;

same comment

>
> ret = of_changeset_apply(cset);
> if (ret)
> - goto failed;
> + goto failed_add_prop;

same comment

>
> + np->data = cset;
> pdev->dev.of_node = np;
> kfree(name);
>
> return;
>
> -failed:
> - if (np)
> - of_node_put(np);
> +failed_add_prop:
> + of_node_put(np);
> +failed_create_node:
> + of_changeset_destroy(cset);
> + kfree(cset);
> +failed_alloc_cset:
> kfree(name);
> }
> #endif

Best regards,
Hervé