All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jonas Juffinger" <jonas.juffinger@liebherr.com>
Cc: linux-serial@vger.kernel.org
Subject: Re: [PATCH 2/2] serial: fsl_lpuart: Enable DMA for TX in console mode
Date: Sun, 15 Nov 2026 18:21:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-215709@kernel.org> (raw)
In-Reply-To: <20261115-fsl_lpuart_8_bit_break_detection-v1-2-b7ac2307dd55@liebherr.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] serial: fsl_lpuart: TX DMA race with lpuart32_console_write()
  on 32-bit LPUART
- [High] serial: fsl_lpuart: UAF of dma_tx_chan in
  lpuart_tx_dma_startup() on resume
- [Medium] serial: fsl_lpuart: console port.lock deadlock via dev_err()
  in TX DMA paths
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] tty: serial: fsl_lpuart: double dma_release_channel() in
  lpuart_remove() [1]
--

commit bca86d1a502b49317e75f113fc0d208cb42b275f
Author: Jonas Juffinger <jonas.juffinger@liebherr.com>

serial: fsl_lpuart: Enable DMA for TX in console mode

This commit removes the console port check in lpuart_tx_dma_startup() so
that TX DMA can be used on LPUART ports operating as a serial console.

> diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c
> index 5817c47e4db3f..e569e2c6ffe9c 100644
> --- a/drivers/tty/serial/fsl_lpuart.c
> +++ b/drivers/tty/serial/fsl_lpuart.c
> @@ -1765,9 +1765,6 @@ static void lpuart_tx_dma_startup(struct lpuart_port *sport)
>  	u32 uartbaud;
>  	int ret;
>  
> -	if (uart_console(&sport->port))
> -		goto err;
> -
>  	if (!sport->dma_tx_chan)
>  		goto err;
>  

[Severity: High]
Can enabling TX DMA on a console port cause concurrent DMA and polled CPU
writes to the TX FIFO during console output?

On 32-bit LPUART, lpuart_tx_dma_startup() enables hardware TX DMA requests
by setting UARTBAUD_TDMAE in UARTBAUD.  When printk() invokes
lpuart32_console_write(), it only masks interrupt bits in UARTCTRL and
never clears UARTBAUD_TDMAE:

drivers/tty/serial/fsl_lpuart.c:lpuart32_console_write() {
    ...
	cr = old_cr = lpuart32_read(&sport->port, UARTCTRL);
	cr |= UARTCTRL_TE | UARTCTRL_RE;
	cr &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
	lpuart32_write(&sport->port, cr, UARTCTRL);

	uart_console_write(&sport->port, s, count, lpuart32_console_putchar);
    ...
}

If a TTY TX DMA transfer is in progress when console output runs, both the
DMA engine and lpuart32_console_putchar() respond to UARTSTAT_TDRE and
write to UARTDATA concurrently, corrupting serial output and potentially
overflowing the TX FIFO.  On 8-bit LPUART, lpuart_console_write() clears
UARTCR2_TIE without waiting for in-flight DMA beats to finish, which
pauses active multi-byte DMA transfers mid-stream and interleaves console
characters into the TTY stream.

[Severity: High]
Does removing this check cause a use-after-free of sport->dma_tx_chan on
system resume when no_console_suspend is enabled?

When a TTY port is opened and then closed, lpuart_startup() /
lpuart32_startup() allocates sport->dma_tx_chan via lpuart_request_dma(),
and lpuart_shutdown() / lpuart32_shutdown() releases it in
lpuart_dma_shutdown() without clearing the pointer:

drivers/tty/serial/fsl_lpuart.c:lpuart_dma_shutdown() {
    ...
	if (sport->dma_tx_chan)
		dma_release_channel(sport->dma_tx_chan);
	if (sport->dma_rx_chan)
		dma_release_channel(sport->dma_rx_chan);
}

When console_suspend_enabled is false, lpuart_uport_is_active() returns
true for the console port even while the TTY port is closed.  On resume,
lpuart_resume() calls lpuart_hw_setup() / lpuart32_hw_setup() ->
lpuart_tx_dma_startup() without calling lpuart_request_dma().  Without the
uart_console() guard, lpuart_tx_dma_startup() sees the non-NULL dangling
sport->dma_tx_chan pointer, passes it to lpuart_dma_tx_request() ->
dmaengine_slave_config(), and sets sport->lpuart_dma_tx_use = true on a
freed channel.

In addition, if the console port is open with an active TX DMA transfer
when lpuart_suspend() runs under no_console_suspend, uart_suspend_port()
returns early without draining TX, and lpuart_suspend() terminates
sport->dma_tx_chan and clears sport->dma_tx_in_progress without unmapping
sport->tx_sgl, leaking the DMA mapping.

[Severity: Medium]
Can calling dev_err() in the TX DMA error paths deadlock on
sport->port.lock when the port is a console?

lpuart_tx_dma_startup() -> lpuart_dma_tx_request() and lpuart_start_tx() /
lpuart32_start_tx() -> lpuart_dma_tx() all run under sport->port.lock:

lpuart_start_tx()
  uart_port_lock_irqsave(&sport->port, &flags)
  lpuart_dma_tx()
    dev_err(dev, "DMA mapping error for TX.\n")
      ...
        lpuart_console_write()
          uart_port_lock_irqsave(&sport->port, &flags)  <-- spins forever

If dmaengine_slave_config() fails in lpuart_dma_tx_request(), or if
dma_map_sg() or dmaengine_prep_slave_sg() fails in lpuart_dma_tx(),
dev_err() synchronously calls into the console write path and deadlocks on
sport->port.lock.

[Severity: Critical]
This problem wasn't introduced by this patch, but can lpuart_remove() call
dma_release_channel() a second time on sport->dma_tx_chan and
sport->dma_rx_chan [1]?

As noted above, lpuart_dma_shutdown() releases sport->dma_tx_chan and
sport->dma_rx_chan on port close without setting either pointer to NULL.
If the device is later unbound or the driver is unloaded, lpuart_remove()
checks those still non-NULL pointers and calls dma_release_channel() again
on the freed channels:

drivers/tty/serial/fsl_lpuart.c:lpuart_remove() {
    ...
	if (sport->dma_tx_chan)
		dma_release_channel(sport->dma_tx_chan);

	if (sport->dma_rx_chan)
		dma_release_channel(sport->dma_rx_chan);
    ...
}

Clearing sport->dma_tx_chan and sport->dma_rx_chan to NULL in
lpuart_dma_shutdown() would avoid both the double release in
lpuart_remove() and the dangling channel pointer across port close.

[1] https://sashiko.dev/#/bug/linux-4b237987-ed3a-4504-92ea-6f92bae6e727

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261115-fsl_lpuart_8_bit_break_detection-v1-0-b7ac2307dd55@liebherr.com?part=2

  reply	other threads:[~2026-11-15 18:21 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-15 18:01 [PATCH 0/2] serial: fsl_lpuart: 8-bit FSL-LPUART driver improvements Jonas Juffinger via B4 Relay
2026-11-15 18:01 ` Jonas Juffinger
2026-11-15 18:01 ` [PATCH 1/2] serial: fsl_lpuart: Enable break detection in 8-bit driver Jonas Juffinger via B4 Relay
2026-11-15 18:01   ` Jonas Juffinger
2026-11-15 18:08   ` sashiko-bot
2026-11-16  8:12   ` Sherry Sun
2026-11-15 18:01 ` [PATCH 2/2] serial: fsl_lpuart: Enable DMA for TX in console mode Jonas Juffinger via B4 Relay
2026-11-15 18:01   ` Jonas Juffinger
2026-11-15 18:21   ` sashiko-bot [this message]
2026-11-16  8:15   ` Sherry Sun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-215709@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=jonas.juffinger@liebherr.com \
    --cc=linux-serial@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.