mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] serial: fsl_lpuart: fix rx buffer and DMA map leaks in start_rx_dma
@ 2026-04-20 13:59 Shitalkumar Gandhi
  2026-04-21  2:33 ` Frank Li
  2026-05-03 19:49 ` Shitalkumar Gandhi
  0 siblings, 2 replies; 5+ messages in thread
From: Shitalkumar Gandhi @ 2026-04-20 13:59 UTC (permalink / raw)
  To: gregkh, jirislaby
  Cc: bhuvanchandra.dv, Frank.Li, peng.fan, sherry.sun, linux-serial,
	imx, linux-arm-kernel, linux-kernel, stable, Shitalkumar Gandhi

lpuart_start_rx_dma() allocates sport->rx_ring.buf with kzalloc() and
then maps a scatterlist via dma_map_sg().  On three subsequent error
paths the function returns directly without releasing those resources:

  - when dma_map_sg() returns 0 (-EINVAL):
      ring->buf is leaked.
  - when dmaengine_slave_config() fails:
      ring->buf and the DMA mapping are leaked.
  - when dmaengine_prep_dma_cyclic() returns NULL:
      ring->buf and the DMA mapping are leaked.

The sole cleanup path, lpuart_dma_rx_free(), is only reached when
lpuart_dma_rx_use is set, and the caller lpuart_rx_dma_startup() clears
that flag on failure of lpuart_start_rx_dma().  So these resources are
permanently leaked on every failure in this function.  Repeated port
open/close or termios changes under error conditions will slowly consume
memory and leave stale streaming DMA mappings behind.

Fix it by introducing two error labels that unmap the scatterlist and
free the ring buffer as appropriate.  While here, replace the misleading
-EFAULT (bad userspace pointer) returned when dmaengine_prep_dma_cyclic()
fails with the more accurate -ENOMEM, matching how other dmaengine users
in the tree treat this failure.

No functional change on the success path.

Fixes: 5887ad43ee02 ("tty: serial: fsl_lpuart: Use cyclic DMA for Rx")
Cc: stable@vger.kernel.org

Signed-off-by: Shitalkumar Gandhi <shitalkumar.gandhi@cambiumnetworks.com>
---
 drivers/tty/serial/fsl_lpuart.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c
index f36d50fe056f..296a096be351 100644
--- a/drivers/tty/serial/fsl_lpuart.c
+++ b/drivers/tty/serial/fsl_lpuart.c
@@ -1376,7 +1376,8 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
 
 	if (!nent) {
 		dev_err(sport->port.dev, "DMA Rx mapping error\n");
-		return -EINVAL;
+		ret = -EINVAL;
+		goto err_free_buf;
 	}
 
 	dma_rx_sconfig.src_addr = lpuart_dma_datareg_addr(sport);
@@ -1388,7 +1389,7 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
 	if (ret < 0) {
 		dev_err(sport->port.dev,
 				"DMA Rx slave config failed, err = %d\n", ret);
-		return ret;
+		goto err_unmap_sg;
 	}
 
 	sport->dma_rx_desc = dmaengine_prep_dma_cyclic(chan,
@@ -1399,7 +1400,8 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
 				 DMA_PREP_INTERRUPT);
 	if (!sport->dma_rx_desc) {
 		dev_err(sport->port.dev, "Cannot prepare cyclic DMA\n");
-		return -EFAULT;
+		ret = -ENOMEM;
+		goto err_unmap_sg;
 	}
 
 	sport->dma_rx_desc->callback = lpuart_dma_rx_complete;
@@ -1423,6 +1425,13 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
 	}
 
 	return 0;
+
+err_unmap_sg:
+	dma_unmap_sg(chan->device->dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
+err_free_buf:
+	kfree(ring->buf);
+	ring->buf = NULL;
+	return ret;
 }
 
 static void lpuart_dma_rx_free(struct uart_port *port)
-- 
2.25.1


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

* Re: [PATCH] serial: fsl_lpuart: fix rx buffer and DMA map leaks in start_rx_dma
  2026-04-20 13:59 [PATCH] serial: fsl_lpuart: fix rx buffer and DMA map leaks in start_rx_dma Shitalkumar Gandhi
@ 2026-04-21  2:33 ` Frank Li
  2026-05-03 19:49 ` Shitalkumar Gandhi
  1 sibling, 0 replies; 5+ messages in thread
From: Frank Li @ 2026-04-21  2:33 UTC (permalink / raw)
  To: Shitalkumar Gandhi
  Cc: gregkh, jirislaby, bhuvanchandra.dv, peng.fan, sherry.sun,
	linux-serial, imx, linux-arm-kernel, linux-kernel, stable,
	Shitalkumar Gandhi

On Mon, Apr 20, 2026 at 07:29:03PM +0530, Shitalkumar Gandhi wrote:
> lpuart_start_rx_dma() allocates sport->rx_ring.buf with kzalloc() and
> then maps a scatterlist via dma_map_sg().  On three subsequent error
> paths the function returns directly without releasing those resources:
>
>   - when dma_map_sg() returns 0 (-EINVAL):
>       ring->buf is leaked.
>   - when dmaengine_slave_config() fails:
>       ring->buf and the DMA mapping are leaked.
>   - when dmaengine_prep_dma_cyclic() returns NULL:
>       ring->buf and the DMA mapping are leaked.
>
> The sole cleanup path, lpuart_dma_rx_free(), is only reached when
> lpuart_dma_rx_use is set, and the caller lpuart_rx_dma_startup() clears
> that flag on failure of lpuart_start_rx_dma().  So these resources are
> permanently leaked on every failure in this function.  Repeated port
> open/close or termios changes under error conditions will slowly consume
> memory and leave stale streaming DMA mappings behind.
>
> Fix it by introducing two error labels that unmap the scatterlist and
> free the ring buffer as appropriate.  While here, replace the misleading
> -EFAULT (bad userspace pointer) returned when dmaengine_prep_dma_cyclic()
> fails with the more accurate -ENOMEM, matching how other dmaengine users
> in the tree treat this failure.
>
> No functional change on the success path.
>
> Fixes: 5887ad43ee02 ("tty: serial: fsl_lpuart: Use cyclic DMA for Rx")
> Cc: stable@vger.kernel.org
>
> Signed-off-by: Shitalkumar Gandhi <shitalkumar.gandhi@cambiumnetworks.com>
> ---

Reviewed-by: Frank Li <Frank.Li@nxp.com>

>  drivers/tty/serial/fsl_lpuart.c | 15 ++++++++++++---
>  1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c
> index f36d50fe056f..296a096be351 100644
> --- a/drivers/tty/serial/fsl_lpuart.c
> +++ b/drivers/tty/serial/fsl_lpuart.c
> @@ -1376,7 +1376,8 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
>
>  	if (!nent) {
>  		dev_err(sport->port.dev, "DMA Rx mapping error\n");
> -		return -EINVAL;
> +		ret = -EINVAL;
> +		goto err_free_buf;
>  	}
>
>  	dma_rx_sconfig.src_addr = lpuart_dma_datareg_addr(sport);
> @@ -1388,7 +1389,7 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
>  	if (ret < 0) {
>  		dev_err(sport->port.dev,
>  				"DMA Rx slave config failed, err = %d\n", ret);
> -		return ret;
> +		goto err_unmap_sg;
>  	}
>
>  	sport->dma_rx_desc = dmaengine_prep_dma_cyclic(chan,
> @@ -1399,7 +1400,8 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
>  				 DMA_PREP_INTERRUPT);
>  	if (!sport->dma_rx_desc) {
>  		dev_err(sport->port.dev, "Cannot prepare cyclic DMA\n");
> -		return -EFAULT;
> +		ret = -ENOMEM;
> +		goto err_unmap_sg;
>  	}
>
>  	sport->dma_rx_desc->callback = lpuart_dma_rx_complete;
> @@ -1423,6 +1425,13 @@ static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
>  	}
>
>  	return 0;
> +
> +err_unmap_sg:
> +	dma_unmap_sg(chan->device->dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
> +err_free_buf:
> +	kfree(ring->buf);
> +	ring->buf = NULL;
> +	return ret;
>  }
>
>  static void lpuart_dma_rx_free(struct uart_port *port)
> --
> 2.25.1
>

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

* Re: [PATCH] serial: fsl_lpuart: fix rx buffer and DMA map leaks in start_rx_dma
  2026-04-20 13:59 [PATCH] serial: fsl_lpuart: fix rx buffer and DMA map leaks in start_rx_dma Shitalkumar Gandhi
  2026-04-21  2:33 ` Frank Li
@ 2026-05-03 19:49 ` Shitalkumar Gandhi
  2026-05-04  6:24   ` Greg Kroah-Hartman
  1 sibling, 1 reply; 5+ messages in thread
From: Shitalkumar Gandhi @ 2026-05-03 19:49 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby
  Cc: Frank Li, Bhuvanchandra DV, Peng Fan, Sherry Sun, linux-serial,
	imx, linux-arm-kernel, linux-kernel, Shitalkumar Gandhi

Hi Greg,

Any update on this? It has Frank's Reviewed-by but I don't see
it queued in tty-next or tty-testing yet.

Thanks,
Shital

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

* Re: [PATCH] serial: fsl_lpuart: fix rx buffer and DMA map leaks in start_rx_dma
  2026-05-03 19:49 ` Shitalkumar Gandhi
@ 2026-05-04  6:24   ` Greg Kroah-Hartman
  2026-05-04  6:49     ` Shitalkumar Gandhi
  0 siblings, 1 reply; 5+ messages in thread
From: Greg Kroah-Hartman @ 2026-05-04  6:24 UTC (permalink / raw)
  To: Shitalkumar Gandhi
  Cc: Jiri Slaby, Frank Li, Bhuvanchandra DV, Peng Fan, Sherry Sun,
	linux-serial, imx, linux-arm-kernel, linux-kernel,
	Shitalkumar Gandhi

On Mon, May 04, 2026 at 01:19:21AM +0530, Shitalkumar Gandhi wrote:
> Hi Greg,
> 
> Any update on this? It has Frank's Reviewed-by but I don't see
> it queued in tty-next or tty-testing yet.

What is "this"?  I see no context here :(

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

* Re: [PATCH] serial: fsl_lpuart: fix rx buffer and DMA map leaks in start_rx_dma
  2026-05-04  6:24   ` Greg Kroah-Hartman
@ 2026-05-04  6:49     ` Shitalkumar Gandhi
  0 siblings, 0 replies; 5+ messages in thread
From: Shitalkumar Gandhi @ 2026-05-04  6:49 UTC (permalink / raw)
  To: gregkh
  Cc: Frank.Li, bhuvanchandra.dv, imx, jirislaby, linux-arm-kernel,
	linux-kernel, linux-serial, peng.fan, sherry.sun,
	shital.gandhi45, Shitalkumar Gandhi

On Mon, May 04, 2026 at 08:24:20AM +0200, Greg Kroah-Hartman wrote:
> > Any update on this? It has Frank's Reviewed-by but I don't see
> > it queued in tty-next or tty-testing yet.
>
> What is "this"?  I see no context here :(

Sorry for the missing context. The patch is:

  serial: fsl_lpuart: fix rx buffer and DMA map leaks in start_rx_dma

Fixes three error paths in lpuart_start_rx_dma() that leak ring->buf
and the DMA mapping. Sent April 20, Frank Li gave Reviewed-by on
April 21.

Lore: https://lore.kernel.org/linux-serial/20260420135903.2062024-1-shitalkumar.gandhi@cambiumnetworks.com/

Happy to resend with Frank's Reviewed-by added if that helps.

Thanks,
Shital

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

end of thread, other threads:[~2026-05-04  6:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-20 13:59 [PATCH] serial: fsl_lpuart: fix rx buffer and DMA map leaks in start_rx_dma Shitalkumar Gandhi
2026-04-21  2:33 ` Frank Li
2026-05-03 19:49 ` Shitalkumar Gandhi
2026-05-04  6:24   ` Greg Kroah-Hartman
2026-05-04  6:49     ` Shitalkumar Gandhi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®