All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] drivers/mmc: fix reference count leaks in moxart_probe
@ 2021-09-28  2:13 Xin Xiong
  2021-09-30 11:13 ` Ulf Hansson
  0 siblings, 1 reply; 2+ messages in thread
From: Xin Xiong @ 2021-09-28  2:13 UTC (permalink / raw)
  To: Ulf Hansson, linux-mmc, linux-kernel
  Cc: yuanxzhang, Xin Xiong, Xiyu Yang, Xin Tan

The issue happens in several error handling paths on two refcounted
object related to the object "host" (dma_chan_rx, dma_chan_tx). In
these paths, the function forgets to decrement one or both objects'
reference count increased earlier by dma_request_chan(), causing
reference count leaks.

Fix it by balancing the refcounts of both objects in some error
handling 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/mmc/host/moxart-mmc.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/drivers/mmc/host/moxart-mmc.c b/drivers/mmc/host/moxart-mmc.c
index 6c9d38132..f421be7ce 100644
--- a/drivers/mmc/host/moxart-mmc.c
+++ b/drivers/mmc/host/moxart-mmc.c
@@ -621,6 +621,14 @@ static int moxart_probe(struct platform_device *pdev)
 			ret = -EPROBE_DEFER;
 			goto out;
 		}
+		if (!IS_ERR(host->dma_chan_tx)) {
+			dma_release_channel(host->dma_chan_tx);
+			host->dma_chan_tx = NULL;
+		}
+		if (!IS_ERR(host->dma_chan_rx)) {
+			dma_release_channel(host->dma_chan_rx);
+			host->dma_chan_rx = NULL;
+		}
 		dev_dbg(dev, "PIO mode transfer enabled\n");
 		host->have_dma = false;
 	} else {
@@ -675,6 +683,10 @@ static int moxart_probe(struct platform_device *pdev)
 	return 0;
 
 out:
+	if (!IS_ERR_OR_NULL(host->dma_chan_tx))
+		dma_release_channel(host->dma_chan_tx);
+	if (!IS_ERR_OR_NULL(host->dma_chan_rx))
+		dma_release_channel(host->dma_chan_rx);
 	if (mmc)
 		mmc_free_host(mmc);
 	return ret;
-- 
2.25.1


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

* Re: [PATCH v3] drivers/mmc: fix reference count leaks in moxart_probe
  2021-09-28  2:13 [PATCH v3] drivers/mmc: fix reference count leaks in moxart_probe Xin Xiong
@ 2021-09-30 11:13 ` Ulf Hansson
  0 siblings, 0 replies; 2+ messages in thread
From: Ulf Hansson @ 2021-09-30 11:13 UTC (permalink / raw)
  To: Xin Xiong
  Cc: linux-mmc, Linux Kernel Mailing List, yuanxzhang, Xiyu Yang, Xin Tan

On Tue, 28 Sept 2021 at 04:15, Xin Xiong <xiongx18@fudan.edu.cn> wrote:
>
> The issue happens in several error handling paths on two refcounted
> object related to the object "host" (dma_chan_rx, dma_chan_tx). In
> these paths, the function forgets to decrement one or both objects'
> reference count increased earlier by dma_request_chan(), causing
> reference count leaks.
>
> Fix it by balancing the refcounts of both objects in some error
> handling 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/mmc/host/moxart-mmc.c | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/drivers/mmc/host/moxart-mmc.c b/drivers/mmc/host/moxart-mmc.c
> index 6c9d38132..f421be7ce 100644
> --- a/drivers/mmc/host/moxart-mmc.c
> +++ b/drivers/mmc/host/moxart-mmc.c
> @@ -621,6 +621,14 @@ static int moxart_probe(struct platform_device *pdev)
>                         ret = -EPROBE_DEFER;
>                         goto out;
>                 }
> +               if (!IS_ERR(host->dma_chan_tx)) {
> +                       dma_release_channel(host->dma_chan_tx);
> +                       host->dma_chan_tx = NULL;
> +               }
> +               if (!IS_ERR(host->dma_chan_rx)) {
> +                       dma_release_channel(host->dma_chan_rx);
> +                       host->dma_chan_rx = NULL;
> +               }
>                 dev_dbg(dev, "PIO mode transfer enabled\n");
>                 host->have_dma = false;
>         } else {
> @@ -675,6 +683,10 @@ static int moxart_probe(struct platform_device *pdev)
>         return 0;
>
>  out:
> +       if (!IS_ERR_OR_NULL(host->dma_chan_tx))
> +               dma_release_channel(host->dma_chan_tx);
> +       if (!IS_ERR_OR_NULL(host->dma_chan_rx))
> +               dma_release_channel(host->dma_chan_rx);
>         if (mmc)
>                 mmc_free_host(mmc);
>         return ret;

This looks much better! However, it seems like we also need to deal
with the NULL case in moxart_remove(), similar to as above.

Kind regards
Uffe

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

end of thread, other threads:[~2021-09-30 11:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-28  2:13 [PATCH v3] drivers/mmc: fix reference count leaks in moxart_probe Xin Xiong
2021-09-30 11:13 ` Ulf Hansson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.