diff mbox series

[v2,2/3] pinctrl: single: fix possible memory leak when pinctrl_enable() fails

Message ID 20240606023704.3931561-3-yangyingliang@huawei.com
State New
Headers show
Series pinctrl: fix possible memory leak when pinctrl_enable() fails | expand

Commit Message

Yang Yingliang June 6, 2024, 2:37 a.m. UTC
This driver calls pinctrl_register_and_init() which is not
devm_ managed, it will leads memory leak if pinctrl_enable()
fails. Replace it with devm_pinctrl_register_and_init().
And call pcs_free_resources() if pinctrl_enable() fails.

Fixes: 5038a66dad01 ("pinctrl: core: delete incorrect free in pinctrl_enable()")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/pinctrl/pinctrl-single.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Dan Carpenter Aug. 15, 2024, 2:42 p.m. UTC | #1
On Thu, Jun 06, 2024 at 10:37:03AM +0800, Yang Yingliang wrote:
> This driver calls pinctrl_register_and_init() which is not
> devm_ managed, it will leads memory leak if pinctrl_enable()
> fails. Replace it with devm_pinctrl_register_and_init().
> And call pcs_free_resources() if pinctrl_enable() fails.
> 
> Fixes: 5038a66dad01 ("pinctrl: core: delete incorrect free in pinctrl_enable()")
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> ---
>  drivers/pinctrl/pinctrl-single.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
> index a798f31d6954..4c6bfabb6bd7 100644
> --- a/drivers/pinctrl/pinctrl-single.c
> +++ b/drivers/pinctrl/pinctrl-single.c
> @@ -1329,7 +1329,6 @@ static void pcs_irq_free(struct pcs_device *pcs)
>  static void pcs_free_resources(struct pcs_device *pcs)
>  {
>  	pcs_irq_free(pcs);
> -	pinctrl_unregister(pcs->pctl);
>  
>  #if IS_BUILTIN(CONFIG_PINCTRL_SINGLE)
>  	if (pcs->missing_nr_pinctrl_cells)
> @@ -1879,7 +1878,7 @@ static int pcs_probe(struct platform_device *pdev)
>  	if (ret < 0)
>  		goto free;
>  
> -	ret = pinctrl_register_and_init(&pcs->desc, pcs->dev, pcs, &pcs->pctl);
> +	ret = devm_pinctrl_register_and_init(pcs->dev, &pcs->desc, pcs, &pcs->pctl);
>  	if (ret) {
>  		dev_err(pcs->dev, "could not register single pinctrl driver\n");
>  		goto free;
> @@ -1912,8 +1911,10 @@ static int pcs_probe(struct platform_device *pdev)
>  
>  	dev_info(pcs->dev, "%i pins, size %u\n", pcs->desc.npins, pcs->size);
>  
> -	return pinctrl_enable(pcs->pctl);
> +	if (pinctrl_enable(pcs->pctl))
> +		goto free;

Sorry, I didn't notice this until now.  This should have been:

	ret = pinctrl_enable(pcs->pctl);
	if (ret)
		goto free;

The error code needs to be saved.

regards,
dan carpenter

>  
> +	return 0;
>  free:
>  	pcs_free_resources(pcs);
>  
> -- 
> 2.25.1
diff mbox series

Patch

diff --git a/drivers/pinctrl/pinctrl-single.c b/drivers/pinctrl/pinctrl-single.c
index a798f31d6954..4c6bfabb6bd7 100644
--- a/drivers/pinctrl/pinctrl-single.c
+++ b/drivers/pinctrl/pinctrl-single.c
@@ -1329,7 +1329,6 @@  static void pcs_irq_free(struct pcs_device *pcs)
 static void pcs_free_resources(struct pcs_device *pcs)
 {
 	pcs_irq_free(pcs);
-	pinctrl_unregister(pcs->pctl);
 
 #if IS_BUILTIN(CONFIG_PINCTRL_SINGLE)
 	if (pcs->missing_nr_pinctrl_cells)
@@ -1879,7 +1878,7 @@  static int pcs_probe(struct platform_device *pdev)
 	if (ret < 0)
 		goto free;
 
-	ret = pinctrl_register_and_init(&pcs->desc, pcs->dev, pcs, &pcs->pctl);
+	ret = devm_pinctrl_register_and_init(pcs->dev, &pcs->desc, pcs, &pcs->pctl);
 	if (ret) {
 		dev_err(pcs->dev, "could not register single pinctrl driver\n");
 		goto free;
@@ -1912,8 +1911,10 @@  static int pcs_probe(struct platform_device *pdev)
 
 	dev_info(pcs->dev, "%i pins, size %u\n", pcs->desc.npins, pcs->size);
 
-	return pinctrl_enable(pcs->pctl);
+	if (pinctrl_enable(pcs->pctl))
+		goto free;
 
+	return 0;
 free:
 	pcs_free_resources(pcs);