linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtd: rawnand: denali: Fix a possible resource leak in denali_pci_probe
@ 2022-08-01  8:03 GONG, Ruiqi
  2022-08-01 18:26 ` Christophe JAILLET
  2022-09-19 15:34 ` Miquel Raynal
  0 siblings, 2 replies; 4+ messages in thread
From: GONG, Ruiqi @ 2022-08-01  8:03 UTC (permalink / raw)
  To: miquel.raynal, richard, vigneshr
  Cc: linux-mtd, linux-kernel, wangweiyang2, gongruiqi1

Call pci_release_regions() to retrieve the allocated resource when
devm_ioremap() or denali_init() failed.

Signed-off-by: GONG, Ruiqi <gongruiqi1@huawei.com>
---
 drivers/mtd/nand/raw/denali_pci.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/nand/raw/denali_pci.c b/drivers/mtd/nand/raw/denali_pci.c
index de7e722d3826..40943cda0914 100644
--- a/drivers/mtd/nand/raw/denali_pci.c
+++ b/drivers/mtd/nand/raw/denali_pci.c
@@ -74,21 +74,22 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
 		return ret;
 	}
 
+	ret = -ENOMEM;
 	denali->reg = devm_ioremap(denali->dev, csr_base, csr_len);
 	if (!denali->reg) {
 		dev_err(&dev->dev, "Spectra: Unable to remap memory region\n");
-		return -ENOMEM;
+		goto out_release_pci;
 	}
 
 	denali->host = devm_ioremap(denali->dev, mem_base, mem_len);
 	if (!denali->host) {
 		dev_err(&dev->dev, "Spectra: ioremap failed!");
-		return -ENOMEM;
+		goto out_release_pci;
 	}
 
 	ret = denali_init(denali);
 	if (ret)
-		return ret;
+		goto out_release_pci;
 
 	nsels = denali->nbanks;
 
@@ -116,6 +117,8 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
 
 out_remove_denali:
 	denali_remove(denali);
+out_release_pci:
+	pci_release_regions(dev);
 	return ret;
 }
 
-- 
2.25.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: rawnand: denali: Fix a possible resource leak in denali_pci_probe
  2022-08-01  8:03 [PATCH] mtd: rawnand: denali: Fix a possible resource leak in denali_pci_probe GONG, Ruiqi
@ 2022-08-01 18:26 ` Christophe JAILLET
  2022-09-19 15:35   ` Miquel Raynal
  2022-09-19 15:34 ` Miquel Raynal
  1 sibling, 1 reply; 4+ messages in thread
From: Christophe JAILLET @ 2022-08-01 18:26 UTC (permalink / raw)
  To: GONG, Ruiqi, miquel.raynal, richard, vigneshr
  Cc: linux-mtd, linux-kernel, wangweiyang2

Le 01/08/2022 à 10:03, GONG, Ruiqi a écrit :
> Call pci_release_regions() to retrieve the allocated resource when
> devm_ioremap() or denali_init() failed.
> 

Hi,
this is not correct.

First, should you be right, you should also update the .remove() 
function the same way.

Second, at the beginning there is pcim_enable_device() call.
This looked like magic to me when I first saw it, but this function 
makes some pci_ functions work just as it they were pcim_ functions.

See pcim_enable_device() ([1]), at line 2132.
When pcim_release() ([2]) is called by the framework, then regions are 
released at line 2079

CJ

[1]: https://elixir.bootlin.com/linux/v5.19/source/drivers/pci/pci.c#L2118
[2]: https://elixir.bootlin.com/linux/v5.19/source/drivers/pci/pci.c#L2071

> Signed-off-by: GONG, Ruiqi <gongruiqi1@huawei.com>
> ---
>   drivers/mtd/nand/raw/denali_pci.c | 9 ++++++---
>   1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/mtd/nand/raw/denali_pci.c b/drivers/mtd/nand/raw/denali_pci.c
> index de7e722d3826..40943cda0914 100644
> --- a/drivers/mtd/nand/raw/denali_pci.c
> +++ b/drivers/mtd/nand/raw/denali_pci.c
> @@ -74,21 +74,22 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
>   		return ret;
>   	}
>   
> +	ret = -ENOMEM;
>   	denali->reg = devm_ioremap(denali->dev, csr_base, csr_len);
>   	if (!denali->reg) {
>   		dev_err(&dev->dev, "Spectra: Unable to remap memory region\n");
> -		return -ENOMEM;
> +		goto out_release_pci;
>   	}
>   
>   	denali->host = devm_ioremap(denali->dev, mem_base, mem_len);
>   	if (!denali->host) {
>   		dev_err(&dev->dev, "Spectra: ioremap failed!");
> -		return -ENOMEM;
> +		goto out_release_pci;
>   	}
>   
>   	ret = denali_init(denali);
>   	if (ret)
> -		return ret;
> +		goto out_release_pci;
>   
>   	nsels = denali->nbanks;
>   
> @@ -116,6 +117,8 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
>   
>   out_remove_denali:
>   	denali_remove(denali);
> +out_release_pci:
> +	pci_release_regions(dev);
>   	return ret;
>   }
>   


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: rawnand: denali: Fix a possible resource leak in denali_pci_probe
  2022-08-01  8:03 [PATCH] mtd: rawnand: denali: Fix a possible resource leak in denali_pci_probe GONG, Ruiqi
  2022-08-01 18:26 ` Christophe JAILLET
@ 2022-09-19 15:34 ` Miquel Raynal
  1 sibling, 0 replies; 4+ messages in thread
From: Miquel Raynal @ 2022-09-19 15:34 UTC (permalink / raw)
  To: GONG, Ruiqi; +Cc: richard, vigneshr, linux-mtd, linux-kernel, wangweiyang2

Hi Ruiqi,

gongruiqi1@huawei.com wrote on Mon, 1 Aug 2022 16:03:15 +0800:

> Call pci_release_regions() to retrieve the allocated resource when
> devm_ioremap() or denali_init() failed.

				  fail
> 
> Signed-off-by: GONG, Ruiqi <gongruiqi1@huawei.com>
> ---
>  drivers/mtd/nand/raw/denali_pci.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/mtd/nand/raw/denali_pci.c b/drivers/mtd/nand/raw/denali_pci.c
> index de7e722d3826..40943cda0914 100644
> --- a/drivers/mtd/nand/raw/denali_pci.c
> +++ b/drivers/mtd/nand/raw/denali_pci.c
> @@ -74,21 +74,22 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
>  		return ret;
>  	}
>  
> +	ret = -ENOMEM;

I don't like so much this construction as later changes in the probe
might just fail this logic.

Please just set ret = -ENOMEM; in the error path each time it's needed.
I don't care about loosing 3 lines of code if it clarifies what the
error path returns.

>  	denali->reg = devm_ioremap(denali->dev, csr_base, csr_len);
>  	if (!denali->reg) {
>  		dev_err(&dev->dev, "Spectra: Unable to remap memory region\n");
> -		return -ENOMEM;
> +		goto out_release_pci;
>  	}
>  
>  	denali->host = devm_ioremap(denali->dev, mem_base, mem_len);
>  	if (!denali->host) {
>  		dev_err(&dev->dev, "Spectra: ioremap failed!");
> -		return -ENOMEM;
> +		goto out_release_pci;
>  	}
>  
>  	ret = denali_init(denali);
>  	if (ret)
> -		return ret;
> +		goto out_release_pci;
>  
>  	nsels = denali->nbanks;
>  
> @@ -116,6 +117,8 @@ static int denali_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
>  
>  out_remove_denali:
>  	denali_remove(denali);
> +out_release_pci:
> +	pci_release_regions(dev);
>  	return ret;
>  }
>  


Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mtd: rawnand: denali: Fix a possible resource leak in denali_pci_probe
  2022-08-01 18:26 ` Christophe JAILLET
@ 2022-09-19 15:35   ` Miquel Raynal
  0 siblings, 0 replies; 4+ messages in thread
From: Miquel Raynal @ 2022-09-19 15:35 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: GONG, Ruiqi, richard, vigneshr, linux-mtd, linux-kernel, wangweiyang2

Hi,

christophe.jaillet@wanadoo.fr wrote on Mon, 1 Aug 2022 20:26:07 +0200:

> Le 01/08/2022 à 10:03, GONG, Ruiqi a écrit :
> > Call pci_release_regions() to retrieve the allocated resource when
> > devm_ioremap() or denali_init() failed.
> >   
> 
> Hi,
> this is not correct.
> 
> First, should you be right, you should also update the .remove() function the same way.
> 
> Second, at the beginning there is pcim_enable_device() call.
> This looked like magic to me when I first saw it, but this function makes some pci_ functions work just as it they were pcim_ functions.
> 
> See pcim_enable_device() ([1]), at line 2132.
> When pcim_release() ([2]) is called by the framework, then regions are released at line 2079

Interesting, I just see this answer now, please ignore my first comment
then, it's not useful.

Thanks Christophe for the feedback.

Cheers,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-09-19 15:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-01  8:03 [PATCH] mtd: rawnand: denali: Fix a possible resource leak in denali_pci_probe GONG, Ruiqi
2022-08-01 18:26 ` Christophe JAILLET
2022-09-19 15:35   ` Miquel Raynal
2022-09-19 15:34 ` Miquel Raynal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).