linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtd: rawnand: atmel: fix refcount leaks in atmel_nand_controller_init
@ 2022-01-25 12:31 Xin Xiong
  2022-01-25 17:55 ` Miquel Raynal
  0 siblings, 1 reply; 2+ messages in thread
From: Xin Xiong @ 2022-01-25 12:31 UTC (permalink / raw)
  To: Tudor Ambarus, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, Nicolas Ferre, Alexandre Belloni,
	Ludovic Desroches, linux-mtd, linux-arm-kernel, linux-kernel
  Cc: yuanxzhang, Xin Xiong, Xiyu Yang, Xin Tan

The reference counting issue happens in several error handling paths
on a refcounted object "nc->dmac". In these paths, the function simply
returns the error code, forgetting to balance the reference count
increased earlier by dma_request_channel(), which may cause refcount
leaks. What's more, the "nc->mck" object also needs to be handled
correctly.

Fix it by adding specific error handling code in those error paths.

Signed-off-by: Xin Xiong <xiongx18@fudan.edu.cn>
Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
---
 drivers/mtd/nand/raw/atmel/nand-controller.c | 22 +++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/nand/raw/atmel/nand-controller.c b/drivers/mtd/nand/raw/atmel/nand-controller.c
index f3276ee9e..b3801ebd2 100644
--- a/drivers/mtd/nand/raw/atmel/nand-controller.c
+++ b/drivers/mtd/nand/raw/atmel/nand-controller.c
@@ -2060,13 +2060,15 @@ static int atmel_nand_controller_init(struct atmel_nand_controller *nc,
 	nc->mck = of_clk_get(dev->parent->of_node, 0);
 	if (IS_ERR(nc->mck)) {
 		dev_err(dev, "Failed to retrieve MCK clk\n");
-		return PTR_ERR(nc->mck);
+		ret = PTR_ERR(nc->mck);
+		goto out_release_dma;
 	}
 
 	np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
 	if (!np) {
 		dev_err(dev, "Missing or invalid atmel,smc property\n");
-		return -EINVAL;
+		ret = -EINVAL;
+		goto out_put_clk;
 	}
 
 	nc->smc = syscon_node_to_regmap(np);
@@ -2074,10 +2076,24 @@ static int atmel_nand_controller_init(struct atmel_nand_controller *nc,
 	if (IS_ERR(nc->smc)) {
 		ret = PTR_ERR(nc->smc);
 		dev_err(dev, "Could not get SMC regmap (err = %d)\n", ret);
-		return ret;
+		goto out_put_clk;
 	}
 
 	return 0;
+
+out_put_clk:
+	clk_put(nc->mck);
+	nc->mck = NULL;
+
+out_release_dma:
+	if (nc->caps->has_dma && !atmel_nand_avoid_dma) {
+		if (!IS_ERR_OR_NULL(nc->dmac)) {
+			dma_release_channel(nc->dmac);
+			nc->dmac = NULL;
+		}
+	}
+
+	return ret;
 }
 
 static int
-- 
2.25.1


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

* Re: [PATCH] mtd: rawnand: atmel: fix refcount leaks in atmel_nand_controller_init
  2022-01-25 12:31 [PATCH] mtd: rawnand: atmel: fix refcount leaks in atmel_nand_controller_init Xin Xiong
@ 2022-01-25 17:55 ` Miquel Raynal
  0 siblings, 0 replies; 2+ messages in thread
From: Miquel Raynal @ 2022-01-25 17:55 UTC (permalink / raw)
  To: Xin Xiong
  Cc: Tudor Ambarus, Richard Weinberger, Vignesh Raghavendra,
	Nicolas Ferre, Alexandre Belloni, Ludovic Desroches, linux-mtd,
	linux-arm-kernel, linux-kernel, yuanxzhang, Xiyu Yang, Xin Tan

Hi Xin,

xiongx18@fudan.edu.cn wrote on Tue, 25 Jan 2022 20:31:56 +0800:

> The reference counting issue happens in several error handling paths
> on a refcounted object "nc->dmac". In these paths, the function simply
> returns the error code, forgetting to balance the reference count
> increased earlier by dma_request_channel(), which may cause refcount
> leaks. What's more, the "nc->mck" object also needs to be handled
> correctly.

You might want to split this into two commits.

> 
> Fix it by adding specific error handling code in those error paths.

You need a Fixes tag here.

> Signed-off-by: Xin Xiong <xiongx18@fudan.edu.cn>
> Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
> Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>

You can't do that. The patch is rather straightforward, so 3
authorship seem a little bit too much.

If someone suggested you a change, or sent you a proposal, you should
use a Suggested-by. If it was reported by a colleague, use Reported-by.
If it was co-developped, use the right tag and the right Signed-off-by
as documented in the submitted-process.rst file. If you picked the
patch from someone else, you must preserve the authorship and just add
your SoB.

> ---
>  drivers/mtd/nand/raw/atmel/nand-controller.c | 22 +++++++++++++++++---
>  1 file changed, 19 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/mtd/nand/raw/atmel/nand-controller.c b/drivers/mtd/nand/raw/atmel/nand-controller.c
> index f3276ee9e..b3801ebd2 100644
> --- a/drivers/mtd/nand/raw/atmel/nand-controller.c
> +++ b/drivers/mtd/nand/raw/atmel/nand-controller.c
> @@ -2060,13 +2060,15 @@ static int atmel_nand_controller_init(struct atmel_nand_controller *nc,
>  	nc->mck = of_clk_get(dev->parent->of_node, 0);
>  	if (IS_ERR(nc->mck)) {
>  		dev_err(dev, "Failed to retrieve MCK clk\n");
> -		return PTR_ERR(nc->mck);
> +		ret = PTR_ERR(nc->mck);
> +		goto out_release_dma;
>  	}
>  
>  	np = of_parse_phandle(dev->parent->of_node, "atmel,smc", 0);
>  	if (!np) {
>  		dev_err(dev, "Missing or invalid atmel,smc property\n");
> -		return -EINVAL;
> +		ret = -EINVAL;
> +		goto out_put_clk;
>  	}
>  
>  	nc->smc = syscon_node_to_regmap(np);
> @@ -2074,10 +2076,24 @@ static int atmel_nand_controller_init(struct atmel_nand_controller *nc,
>  	if (IS_ERR(nc->smc)) {
>  		ret = PTR_ERR(nc->smc);
>  		dev_err(dev, "Could not get SMC regmap (err = %d)\n", ret);
> -		return ret;
> +		goto out_put_clk;
>  	}
>  
>  	return 0;
> +
> +out_put_clk:
> +	clk_put(nc->mck);
> +	nc->mck = NULL;
> +
> +out_release_dma:
> +	if (nc->caps->has_dma && !atmel_nand_avoid_dma) {
> +		if (!IS_ERR_OR_NULL(nc->dmac)) {

	   && !IS_ERR...

> +			dma_release_channel(nc->dmac);
> +			nc->dmac = NULL;
> +		}
> +	}
> +
> +	return ret;
>  }
>  
>  static int


Thanks,
Miquèl

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

end of thread, other threads:[~2022-01-25 17:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-25 12:31 [PATCH] mtd: rawnand: atmel: fix refcount leaks in atmel_nand_controller_init Xin Xiong
2022-01-25 17:55 ` 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).