Linux Serial subsystem development
 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 v9 7/8] serial: 8250: retire canceled RX DMA before reusing the receive buffer
Date: Mon, 02 Nov 2026 23:49:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-194343@kernel.org> (raw)
In-Reply-To: <20261102-submit-serial-8250-dma-cancel-v1-v9-7-9159caade148@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] serial: 8250: unbounded pause retry loop in serial8250_rx_dma_flush_work()
--

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

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

This commit reserves the RX DMA receive buffer until a worker has drained
canceled callbacks and adds retry and recovery logic for RX DMA pause and
termination failures.

> diff --git a/drivers/tty/serial/8250/8250_dma.c b/drivers/tty/serial/8250/8250_dma.c
> index 344f0dd24b416..fd2a0ed211657 100644
> --- a/drivers/tty/serial/8250/8250_dma.c
> +++ b/drivers/tty/serial/8250/8250_dma.c
[ ... ]
> +/* Try to obtain a stable residue before retiring the current descriptor. */
> +static int serial8250_rx_dma_try_flush(struct uart_8250_port *p)
> +{
> +	struct uart_8250_dma *dma = p->dma;
> +	struct dma_tx_state state;
> +	enum dma_status status;
> +	int ret;
> +
> +	lockdep_assert_held_once(&p->port.lock);
> +
> +	ret = dmaengine_pause(dma->rxchan);
> +	status = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
> +	if (status == DMA_ERROR) {
> +		ret = -EIO;
> +		goto recover;
> +	}
> +	if (ret && status != DMA_COMPLETE) {
> +		/*
> +		 * Hardware may be idle before the provider publishes completion.
> +		 * Leave the descriptor alive and retry outside interrupt context;
> +		 * neither copying its buffer nor canceling it is safe yet.
> +		 */
> +		dma->rx_pause_pending = true;
> +		serial8250_out_IER(p, p->ier);
> +		return ret;
> +	}
[ ... ]
> +static void serial8250_rx_dma_flush_work(struct work_struct *work)
> +{
> +	struct uart_8250_dma *dma = container_of(work, struct uart_8250_dma, rx_flush_work);
> +	struct uart_8250_port *p = dma->port;
> +	unsigned int delay_us = USEC_PER_MSEC;
> +	ktime_t start = ktime_get();
> +	bool recover, retry, restore_rx;
> +	bool warned = false;
> +	int ret;
> +
> +	guard(serial8250_rpm)(p);
> +
> +	for (;;) {
> +		scoped_guard(uart_port_lock_irqsave, &p->port) {
> +			/* Shutdown cancels this work and drains both DMA channels. */
> +			if (dma->shutting_down)
> +				return;
> +			/* Serialize reset with completion and shutdown notifications. */
> +			WRITE_ONCE(dma->rx_pause_wakeup, false);
> +			retry = dma->rx_pause_pending && !dma->rx_terminating;
> +			if (retry) {
> +				ret = serial8250_rx_dma_try_flush(p);
> +				retry = dma->rx_pause_pending && !dma->rx_terminating;
> +			}
> +			recover = dma->rx_err;
> +		}
> +		if (!retry)
> +			break;
> +		if (!warned && ktime_ms_delta(ktime_get(), start) >= MSEC_PER_SEC) {
> +			dev_warn_ratelimited(p->port.dev,
> +					     "RX DMA pause stalled: %d; retaining transfer and retrying\n",
> +					     ret);
> +			warned = true;
> +		}
> +		/* Notifications also cover completion or shutdown before sleeping. */
> +		wait_event_hrtimeout(dma->rx_pause_wait, READ_ONCE(dma->rx_pause_wakeup),
> +				     ns_to_ktime(delay_us * NSEC_PER_USEC));
> +		delay_us = min(delay_us * 2, USEC_PER_SEC);
> +	}

[Severity: High]
As reported by Raul E Rangel on v8 [1], when dmaengine_pause(dma->rxchan)
persistently returns an error during a partial RX DMA transfer (where fewer
than dma->rx_size bytes have arrived and dmaengine_tx_status() remains
DMA_IN_PROGRESS), serial8250_rx_dma_try_flush() sets dma->rx_pause_pending =
true and masks RX interrupts via serial8250_out_IER().

In serial8250_rx_dma_flush_work(), this causes the for (;;) loop to retry
serial8250_rx_dma_try_flush() indefinitely until port shutdown instead of
escalating to serial8250_rx_dma_recover() after a bounded retry budget.

While a brief retry outside p->port.lock allows an already-completed transfer
whose DMA interrupt was masked by p->port.lock to publish DMA_COMPLETE, once
p->port.lock and local IRQs have been released across a few sleeps, any
pending completion interrupt has already run. If dmaengine_pause() continues
to fail while the remote peer is idle (for example, if dw-axi-dmac times out
waiting for DWAXIDMAC_IRQ_SUSPENDED and leaves the channel hardware-suspended
while returning -EAGAIN, or if tegra186-gpc-dma returns -ENOSYS when
!hw_support_pause), dmaengine_tx_status() will never reach DMA_COMPLETE.

While dma->rx_pause_pending remains stuck:
- UART reception is permanently wedged until the port is closed because RX
  interrupts stay masked in UART_IER and serial8250_rx_dma_recovering()
  forces skip_rx = true in serial8250_handle_irq_locked(), preventing PIO
  fallback.
- serial8250_rx_dma_flush_work() holds a runtime PM reference via
  guard(serial8250_rpm)(p) for the lifetime of the open port.
- serial8250_rx_dma_flush() returns -EAGAIN, which causes dw8250_idle_enter()
  in drivers/tty/serial/8250/8250_dw.c to abort divisor and LCR updates:

drivers/tty/serial/8250/8250_dw.c:dw8250_idle_enter() {
	...
	if (up->dma) {
		ret = serial8250_rx_dma_flush(up);
		if (ret) {
			serial8250_out_IER(up, up->ier);
			d->in_idle = 0;
			return ret;
		}
	...
}

Since the recover path right after the loop in serial8250_rx_dma_flush_work()
already calls dmaengine_terminate_sync(dma->rxchan) and retains the buffer
with RX masked if synchronous termination fails, can this loop bound the
number of pause retries and escalate to serial8250_rx_dma_recover(p) so the
port can terminate the transfer and fall back to PIO?

[1] https://lore.kernel.org/r/20261029222734.3086943-1-rrangel@chromium.org

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261102-submit-serial-8250-dma-cancel-v1-v9-0-9159caade148@gmail.com?part=7

  reply	other threads:[~2026-11-02 23:49 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02 23:14 [PATCH v9 0/8] serial: 8250: serialize DMA cancellation with buffer reuse James Hilliard
2026-11-02 23:14 ` [PATCH v9 1/8] serial: 8250_omap: separate locked TX DMA completion processing James Hilliard
2026-11-02 23:21   ` sashiko-bot
2026-11-03 16:10   ` Raul E Rangel
2026-11-02 23:14 ` [PATCH v9 2/8] serial: serialize asynchronous write wakeups with the port lock James Hilliard
2026-11-02 23:25   ` sashiko-bot
2026-11-03 16:10   ` Raul E Rangel
2026-11-03 17:06   ` Andy Shevchenko
2026-11-03 20:25     ` Maciej W. Rozycki
2026-11-02 23:14 ` [PATCH v9 3/8] serial: 8250_omap: serialize RX DMA completion and flushing James Hilliard
2026-11-02 23:32   ` sashiko-bot
2026-11-03 11:55   ` Ilpo Järvinen
2026-11-03 16:10   ` Raul E Rangel
2026-11-02 23:14 ` [PATCH v9 4/8] serial: 8250_omap: quiesce DMA IRQ handling before shutdown James Hilliard
2026-11-02 23:22   ` sashiko-bot
2026-11-03 16:11   ` Raul E Rangel
2026-11-02 23:14 ` [PATCH v9 5/8] serial: core: allow drivers to reserve flushed transmit storage James Hilliard
2026-11-02 23:32   ` sashiko-bot
2026-11-03 16:11   ` Raul E Rangel
2026-11-03 17:08   ` Andy Shevchenko
2026-11-02 23:14 ` [PATCH v9 6/8] serial: 8250: drain canceled TX DMA before reusing its buffer James Hilliard
2026-11-02 23:28   ` sashiko-bot
2026-11-03 16:11   ` Raul E Rangel
2026-11-03 17:21   ` Andy Shevchenko
2026-11-02 23:14 ` [PATCH v9 7/8] serial: 8250: retire canceled RX DMA before reusing the receive buffer James Hilliard
2026-11-02 23:49   ` sashiko-bot [this message]
2026-11-03  1:23     ` James Hilliard
2026-11-03 16:11   ` Raul E Rangel
2026-11-03 18:05     ` James Hilliard
2026-11-03 17:33   ` Andy Shevchenko
2026-11-03 17:54     ` James Hilliard
2026-11-03 18:06       ` Andy Shevchenko
2026-11-03 18:16         ` James Hilliard
2026-11-03 20:59           ` Andy Shevchenko
2026-11-02 23:14 ` [PATCH v9 8/8] serial: 8250: retain DMA resources when termination fails James Hilliard
2026-11-02 23:29   ` sashiko-bot
2026-11-03 16:11   ` Raul E Rangel
2026-11-03 17:35   ` Andy Shevchenko

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-194343@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