diff mbox series

[v3] PCI: of: Fix memory leak when of_changeset_create_node() failed

Message ID 1696007417-42059-1-git-send-email-lizhi.hou@amd.com (mailing list archive)
State Accepted
Delegated to: Bjorn Helgaas
Headers show
Series [v3] PCI: of: Fix memory leak when of_changeset_create_node() failed | expand

Commit Message

Lizhi Hou Sept. 29, 2023, 5:10 p.m. UTC
In function of_pci_make_dev_node(), the cset needs to be destroyed and
freed when of_changeset_create_node() returns null.

Fixes: 407d1a51921e ("PCI: Create device tree node for bridge")
Reported-by: Herve Codina <herve.codina@bootlin.com>
Closes: https://lore.kernel.org/all/20230911171319.495bb837@bootlin.com/
Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>
---
 drivers/pci/of.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

Comments

Herve Codina Sept. 29, 2023, 5:18 p.m. UTC | #1
Hi Lizhi,

On Fri, 29 Sep 2023 10:10:17 -0700
Lizhi Hou <lizhi.hou@amd.com> wrote:

> In function of_pci_make_dev_node(), the cset needs to be destroyed and
> freed when of_changeset_create_node() returns null.
> 
> Fixes: 407d1a51921e ("PCI: Create device tree node for bridge")
> Reported-by: Herve Codina <herve.codina@bootlin.com>
> Closes: https://lore.kernel.org/all/20230911171319.495bb837@bootlin.com/
> Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>
> ---
>  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..51e3dd0ea5ab 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 out_free_name;
>  	of_changeset_init(cset);
>  
>  	np = of_changeset_create_node(cset, ppnode, name);
>  	if (!np)
> -		goto failed;
> -	np->data = cset;
> +		goto out_destroy_cset;
>  
>  	ret = of_pci_add_properties(pdev, cset, np);
>  	if (ret)
> -		goto failed;
> +		goto out_free_node;
>  
>  	ret = of_changeset_apply(cset);
>  	if (ret)
> -		goto failed;
> +		goto out_free_node;
>  
> +	np->data = cset;
>  	pdev->dev.of_node = np;
>  	kfree(name);
>  
>  	return;
>  
> -failed:
> -	if (np)
> -		of_node_put(np);
> +out_free_node:
> +	of_node_put(np);
> +out_destroy_cset:
> +	of_changeset_destroy(cset);
> +	kfree(cset);
> +out_free_name:
>  	kfree(name);
>  }
>  #endif

Reviewed-by: Herve Codina <herve.codina@bootlin.com>

Best regards,
Hervé
Bjorn Helgaas Sept. 29, 2023, 10:37 p.m. UTC | #2
On Fri, Sep 29, 2023 at 10:10:17AM -0700, Lizhi Hou wrote:
> In function of_pci_make_dev_node(), the cset needs to be destroyed and
> freed when of_changeset_create_node() returns null.
> 
> Fixes: 407d1a51921e ("PCI: Create device tree node for bridge")
> Reported-by: Herve Codina <herve.codina@bootlin.com>
> Closes: https://lore.kernel.org/all/20230911171319.495bb837@bootlin.com/
> Signed-off-by: Lizhi Hou <lizhi.hou@amd.com>

Applied with Herve's Reviewed-by to for-linus for v6.6, since
407d1a51921e appeared in v6.6-rc1, thanks!

> ---
>  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..51e3dd0ea5ab 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 out_free_name;
>  	of_changeset_init(cset);
>  
>  	np = of_changeset_create_node(cset, ppnode, name);
>  	if (!np)
> -		goto failed;
> -	np->data = cset;
> +		goto out_destroy_cset;
>  
>  	ret = of_pci_add_properties(pdev, cset, np);
>  	if (ret)
> -		goto failed;
> +		goto out_free_node;
>  
>  	ret = of_changeset_apply(cset);
>  	if (ret)
> -		goto failed;
> +		goto out_free_node;
>  
> +	np->data = cset;
>  	pdev->dev.of_node = np;
>  	kfree(name);
>  
>  	return;
>  
> -failed:
> -	if (np)
> -		of_node_put(np);
> +out_free_node:
> +	of_node_put(np);
> +out_destroy_cset:
> +	of_changeset_destroy(cset);
> +	kfree(cset);
> +out_free_name:
>  	kfree(name);
>  }
>  #endif
> -- 
> 2.34.1
>
diff mbox series

Patch

diff --git a/drivers/pci/of.c b/drivers/pci/of.c
index 2af64bcb7da3..51e3dd0ea5ab 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 out_free_name;
 	of_changeset_init(cset);
 
 	np = of_changeset_create_node(cset, ppnode, name);
 	if (!np)
-		goto failed;
-	np->data = cset;
+		goto out_destroy_cset;
 
 	ret = of_pci_add_properties(pdev, cset, np);
 	if (ret)
-		goto failed;
+		goto out_free_node;
 
 	ret = of_changeset_apply(cset);
 	if (ret)
-		goto failed;
+		goto out_free_node;
 
+	np->data = cset;
 	pdev->dev.of_node = np;
 	kfree(name);
 
 	return;
 
-failed:
-	if (np)
-		of_node_put(np);
+out_free_node:
+	of_node_put(np);
+out_destroy_cset:
+	of_changeset_destroy(cset);
+	kfree(cset);
+out_free_name:
 	kfree(name);
 }
 #endif