Linux kernel and device drivers for NXP i.MX platforms
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "James Hilliard" <james.hilliard1@gmail.com>
Cc: linux-serial@vger.kernel.org, imx@lists.linux.dev, Frank.Li@kernel.org
Subject: Re: [PATCH v14 7/8] serial: 8250: retire canceled RX DMA before reusing the receive buffer
Date: Fri, 04 Dec 2026 10:44:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-249620@kernel.org> (raw)
In-Reply-To: <20261204-submit-serial-8250-dma-cancel-v1-v14-7-b691fec8097d@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] serial: 8250: rx_flush_work() demotes bursty RX DMA to PIO on
  every timeout
- [Medium] serial: 8250: unsynchronized up->dma read in
  serial8250_em485_stop_tx()
--

commit ec92d45cfc9de1099bca1fc304582975c431c023
Author: James Hilliard <james.hilliard1@gmail.com>

serial: 8250: retire canceled RX DMA before reusing the receive buffer

This commit defers reuse of the 8250 RX DMA buffer after an RX flush until
a worker retires canceled callbacks with dmaengine_synchronize(), adding
pause retry and synchronous termination recovery paths along with IER
masking during recovery.

Link: https://lore.kernel.org/r/20261109162135.906262-1-rrangel@chromium.org/

[ ... ]

> diff --git a/drivers/tty/serial/8250/8250_dma.c b/drivers/tty/serial/8250/8250_dma.c
> index 739a8238e4dec..1d7c3c5b75643 100644
> --- a/drivers/tty/serial/8250/8250_dma.c
> +++ b/drivers/tty/serial/8250/8250_dma.c

[ ... ]

> @@ -240,6 +414,9 @@ int serial8250_rx_dma(struct uart_8250_port *p)
>  
>  	if (dma->shutting_down || dma->rx_running)
>  		return 0;
> +	/* A retiring transfer has released the FIFO, but not its buffer. */
> +	if (dma->rx_err || dma->rx_terminating)
> +		return -EIO;
>  
>  	serial8250_do_prepare_rx_dma(p);
>  
> @@ -260,16 +437,26 @@ int serial8250_rx_dma_flush(struct uart_8250_port *p)
>  {
>  	struct uart_8250_dma *dma = p->dma;
> +	int ret;
>  
> -	if (!dma || !dma->rxchan || !dma->rx_running)
> -		return;
> +	lockdep_assert_held_once(&p->port.lock);
>  
> -	dmaengine_pause(dma->rxchan);
> -	__dma_rx_complete(p);
> -	dmaengine_terminate_async(dma->rxchan);
> +	if (!serial8250_rx_dma_running(p))
> +		return 0;
> +	/* A late interrupt must not hand a still-active receiver to PIO. */
> +	if (dma->shutting_down)
> +		return -ESHUTDOWN;
> +	if (dma->rx_terminating)
> +		return -EIO;
> +	if (dma->rx_pause_pending)
> +		return -EAGAIN;
> +
> +	ret = serial8250_rx_dma_try_flush(p);
> +	schedule_work(&dma->rx_flush_work);
> +	return ret;
>  }

[Severity: Medium]
Does scheduling rx_flush_work on every RX timeout demote subsequent incoming
bursts from DMA to byte-by-byte PIO?

Because dma->rx_size defaults to PAGE_SIZE, every burst shorter than 4 KB
ends in UART_IIR_RX_TIMEOUT -> serial8250_rx_dma_flush().  Until
rx_flush_work finishes dmaengine_synchronize() on system_percpu_wq,
serial8250_rx_dma() returns -EIO and the ISR uses serial8250_rx_chars().

At high baudrates (1 to 4 Mbps), normal-priority workqueue latency and
provider device_synchronize waits (such as vchan_synchronize() or
synchronize_irq()) can exceed the FIFO fill time, raising IRQ overhead and
risking RX FIFO overruns; providers with NULL device_synchronize also wait.

Double-buffering the RX buffer would avoid blocking new DMA submissions
while the previous transfer retires.

[ ... ]

> diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c
> index cfbd154bf5f92..49d6a0b19fb40 100644
> --- a/drivers/tty/serial/8250/8250_port.c
> +++ b/drivers/tty/serial/8250/8250_port.c

[ ... ]

> @@ -1332,7 +1332,8 @@ void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier)
>  	 * Enable previously disabled RX interrupts.
>  	 */
>  	if (!(p->port.rs485.flags & SER_RS485_RX_DURING_TX)) {
> -		serial8250_clear_and_reinit_fifos(p);
> +		if (!serial8250_rx_dma_recovering(p))
> +			serial8250_clear_and_reinit_fifos(p);
>  
>  		if (toggle_ier) {
>  			/*

[Severity: Medium]
Can serial8250_rx_dma_recovering() race with serial8250_do_startup() clearing
up->dma to NULL on a console port with boot-time half-duplex RS485 emulation?

On drivers like 8250_dw, probe sets up->dma non-NULL even on a kernel
console, and uart_configure_port() initializes em485 before first open.  When
the console TTY is first opened, serial8250_do_startup() logs "forbid DMA for
kernel console" and clears up->dma unlocked while console writes run:

CPU0                                    CPU1
serial8250_do_startup()                 serial8250_console_write()
  if (up->dma && uart_console(port))      up->rs485_stop_tx(up, false)
    dev_warn_ratelimited(...)             serial8250_em485_stop_tx(up, false)
                                            serial8250_rx_dma_recovering(up)
                                              if (up->dma) /* non-NULL */
    up->dma = NULL;
                                              up->dma->rx_pause_pending
                                              <-- NULL pointer dereference

Because serial8250_rx_dma_recovering() reads up->dma repeatedly without
READ_ONCE() or a local copy, a reload after the NULL check causes a NULL
pointer dereference in the console write path.  Clearing up->dma under
uart_port_lock_irqsave in serial8250_do_startup() would close this race.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-submit-serial-8250-dma-cancel-v1-v14-0-b691fec8097d@gmail.com?part=7

  reply	other threads:[~2026-12-04 10:44 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 10:03 [PATCH v14 0/8] serial: 8250: serialize DMA cancellation with buffer reuse James Hilliard
2026-12-04 10:03 ` [PATCH v14 1/8] serial: 8250_omap: separate locked TX DMA completion processing James Hilliard
2026-12-04 10:18   ` sashiko-bot
2026-12-04 10:03 ` [PATCH v14 2/8] serial: serialize asynchronous write wakeups with the port lock James Hilliard
2026-12-04 10:30   ` sashiko-bot
2026-12-04 10:03 ` [PATCH v14 3/8] serial: 8250_omap: serialize RX DMA completion and flushing James Hilliard
2026-12-04 10:20   ` sashiko-bot
2026-12-04 10:03 ` [PATCH v14 4/8] serial: 8250_omap: quiesce DMA IRQ handling before shutdown James Hilliard
2026-12-04 10:03 ` [PATCH v14 5/8] serial: core: allow drivers to reserve flushed transmit storage James Hilliard
2026-12-04 10:37   ` sashiko-bot
2026-12-04 10:03 ` [PATCH v14 6/8] serial: 8250: drain canceled TX DMA before reusing its buffer James Hilliard
2026-12-04 10:31   ` sashiko-bot
2026-12-04 10:03 ` [PATCH v14 7/8] serial: 8250: retire canceled RX DMA before reusing the receive buffer James Hilliard
2026-12-04 10:44   ` sashiko-bot [this message]
2026-12-04 10:03 ` [PATCH v14 8/8] serial: 8250: retain DMA resources when termination fails James Hilliard

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-249620@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=imx@lists.linux.dev \
    --cc=james.hilliard1@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox