All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tapio Reijonen" <tapio.reijonen@vaisala.com>
Cc: linux-serial@vger.kernel.org
Subject: Re: [PATCH 3/3] serial: max310x: hold TX data until the software RTS send phase
Date: Mon, 16 Nov 2026 10:34:26 +0000	[thread overview]
Message-ID: <sashiko-outbox-216745@kernel.org> (raw)
In-Reply-To: <20261116-max310x-tx-fixes-v1-3-61c01f180911@vaisala.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] serial: max310x: max310x_adopt_hw_tx() bypasses send-phase
  hold on empty FIFO
- [Medium] serial: max310x: x_char sent before software RTS send-phase
  check
- [Medium] serial: max310x: early return in max310x_restart_tx() strands
  TX_SEND state
It also noticed 2 related pre-existing issues this patch does not
introduce:
- [High] serial: max310x: missing port lock in max310x_port_irq() [1]
- [Medium] serial: max310x: unprotected port->status modification in
  max310x_set_termios() [2]
--

commit c6e5f39e8c525f49645d0ceb47c724d2102f907b
Author: Tapio Reijonen <tapio.reijonen@vaisala.com>

serial: max310x: hold TX data until the software RTS send phase

This commit holds queued TX data in max310x_handle_tx() until the
software-timed RS-485 RTS envelope reaches MAX310X_TX_SEND, and adds
max310x_restart_tx() to restart pending transmissions after set_termios()
and RS-485 reconfiguration.

> diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
> index 7ddfe8773faf9..d92bf24155886 100644
> --- a/drivers/tty/serial/max310x.c
> +++ b/drivers/tty/serial/max310x.c
> @@ -942,6 +942,16 @@ static void max310x_handle_tx(struct uart_port *port)
>  		return;
>  	}
>  
> +	/*
> +	 * Outside the send phase of a software envelope RTS is down, still
> +	 * in its setup delay, or about to drop: a late TX-empty interrupt
> +	 * must not push the data out now. start_tx() opens an envelope and
> +	 * tmr_tx() reschedules us at MAX310X_TX_SEND.
> +	 */
> +	if (READ_ONCE(one->sw_rts_during_tx) &&
> +	    READ_ONCE(one->tx_state) != MAX310X_TX_SEND)
> +		return;
> +
>  	while (!kfifo_is_empty(&tport->xmit_fifo)) {
>  		unsigned int txlen, to_send;
>  		unsigned long flags;

[Severity: Medium]
Can port->x_char still be sent before the software RTS envelope reaches
MAX310X_TX_SEND?

At the top of max310x_handle_tx(), port->x_char is written to
MAX310X_THR_REG and cleared before this send-phase check:

drivers/tty/serial/max310x.c:max310x_handle_tx() {
	if (unlikely(port->x_char)) {
		max310x_port_write(port, MAX310X_THR_REG, port->x_char);
		port->icount.tx++;
		port->x_char = 0;
		return;
	}

When uart_send_xchar() calls max310x_start_tx() on the software RTS path,
it opens an envelope in MAX310X_TX_WAIT_BEFORE_SEND.  If a late TX-empty
interrupt from the previous envelope schedules tx_work before the
before-send timer expires, max310x_handle_tx() transmits port->x_char while
RTS is still deasserted or in its setup delay, losing or misframing the
flow-control byte.

> @@ -1077,8 +1087,8 @@ static irqreturn_t max310x_port_irq(struct max310x_port *s, int portno)
>  		if (rxlen)
>  			max310x_handle_rx(port, rxlen);
>  		/*
> -		 * TXEMPTY latches on the FIFO becoming empty, so a stale
> -		 * interrupt cannot pump data during an RTS setup delay.
> +		 * A TX-empty read late can belong to the previous envelope;
> +		 * max310x_handle_tx() holds the data until the send phase.
>  		 */
>  		if (ists & MAX310X_IRQ_TXEMPTY_BIT)
>  			schedule_work(&s->p[portno].tx_work);

[Severity: High]
This problem wasn't introduced by this patch, but max310x_port_irq() calls
uart_handle_cts_change() without holding port->lock [1]:

drivers/tty/serial/max310x.c:max310x_port_irq() {
		if (ists & MAX310X_IRQ_CTS_BIT) {
			lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
			uart_handle_cts_change(port, lsr & MAX310X_LSR_CTS_BIT);
		}

uart_handle_cts_change() requires the caller to hold port->lock (checked via
lockdep_assert_held_once(&uport->lock)), so a CTS interrupt triggers a
lockdep warning and races on port->hw_stopped, port->icount.cts, and TX
start/stop state.

> @@ -1314,6 +1324,28 @@ static void max310x_set_rts_ctl_params(struct max310x_one *one)
>  			    MAX310X_IRDA_RTSINVERT_BIT, irda);
>  }
>  
> +/*
> + * A reconfigure is applied: restart a transmission that it left queued, on
> + * the path that is now configured. A send state without chip data has
> + * nothing in flight: restart it as a fresh envelope so the before-send delay
> + * is honoured. start_tx() can drop and retake the lock through the uart_port
> + * API - callers must take it with uart_port_lock*(), not a spinlock guard.
> + */
> +static void max310x_restart_tx(struct max310x_one *one, bool chip_tx_empty)
> +{
> +	struct uart_port *port = &one->port;
> +
> +	lockdep_assert_held(&port->lock);
> +
> +	if (!port->x_char && (kfifo_is_empty(&port->state->port.xmit_fifo) ||
> +			      uart_tx_stopped(port)))
> +		return;
> +
> +	if (chip_tx_empty && READ_ONCE(one->tx_state) == MAX310X_TX_SEND)
> +		WRITE_ONCE(one->tx_state, MAX310X_TX_OFF);
> +	max310x_start_tx(port);
> +}

[Severity: Medium]
Can returning early in max310x_restart_tx() when xmit_fifo is empty or
uart_tx_stopped(port) is true leave one->tx_state stranded in
MAX310X_TX_SEND with RTS asserted?

When max310x_set_rts_ctl_params() adopts a transfer with an empty chip FIFO
and queued data in xmit_fifo, max310x_adopt_hw_tx() sets one->tx_state to
MAX310X_TX_SEND, asserts RTS, and calls max310x_delayed_stop_tx(), which
returns early without arming tx_delay_tmr because xmit_fifo is non-empty
and unstopped:

CPU0                                  CPU1
max310x_set_termios()
  max310x_set_rts_ctl_params()
    max310x_adopt_hw_tx()
      one->tx_state = MAX310X_TX_SEND
    max310x_rts_ctl(..., true)
    max310x_delayed_stop_tx()
      /* xmit_fifo not empty: returns */
  /* unlocked SPI/I2C writes */
                                      uart_flush_buffer()
                                        kfifo_reset(&tport->xmit_fifo)
  max310x_restart_tx(one, true)
    if (kfifo_is_empty(...))
      return;  <-- tx_state stuck at MAX310X_TX_SEND

Because the chip TX FIFO is already empty, no TXEMPTY interrupt will fire
to arm the after-send timer, leaving RTS permanently asserted on the RS-485
bus until a later write or port close.  In addition, max310x_adopt_hw_tx()
does not check uart_tx_stopped(port), so reconfiguring while TX is stopped
with bytes in xmit_fifo adopts the stopped queue and asserts RTS on an idle
bus.

> @@ -1363,7 +1395,10 @@ static void max310x_set_termios(struct uart_port *port,
>  				const struct ktermios *old)
>  {
>  	unsigned int frame_bits = tty_get_frame_size(termios->c_cflag);
> +	struct max310x_one *one = to_max310x_port(port);
>  	unsigned int lcr = 0, flow = 0;
> +	unsigned long flags;
> +	bool chip_tx_empty;
>  	int baud;

[Severity: Medium]
This problem wasn't introduced by this patch, but max310x_set_termios()
modifies port->status without holding port->lock [2]:

drivers/tty/serial/max310x.c:max310x_set_termios() {
	port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF);

	if (termios->c_cflag & CRTSCTS) {
		/* Enable AUTORTS and AUTOCTS */
		port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
    ...
	if (termios->c_iflag & IXOFF) {
		port->status |= UPSTAT_AUTOXOFF;

serial_core requires holding port->lock (along with termios_rwsem and the
port mutex) when modifying port->status so updates do not race with
concurrent serial core and interrupt handlers.

> @@ -1482,10 +1517,19 @@ static void max310x_set_termios(struct uart_port *port,
>  	 * taking over an in-flight transfer arms the after-send hold from
>  	 * the character time.
>  	 */
> -	to_max310x_port(port)->baud = baud;
> -	to_max310x_port(port)->char_time_us =
> -		DIV_ROUND_UP(USEC_PER_SEC * frame_bits, baud);
> -	max310x_set_rts_ctl_params(to_max310x_port(port));
> +	one->baud = baud;
> +	one->char_time_us = DIV_ROUND_UP(USEC_PER_SEC * frame_bits, baud);
> +	max310x_set_rts_ctl_params(one);
> +
> +	/*
> +	 * A tx_work that ran while the RTS path was switched above has
> +	 * returned without sending, and nothing else pushes the data it left.
> +	 */
> +	chip_tx_empty = !max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
> +
> +	uart_port_lock_irqsave(port, &flags);
> +	max310x_restart_tx(one, chip_tx_empty);
> +	uart_port_unlock_irqrestore(port, flags);
>  }

[Severity: Medium]
Can tx_work run between max310x_adopt_hw_tx() and max310x_restart_tx() when
the chip FIFO is empty, bypassing the send-phase hold?

In max310x_adopt_hw_tx(), a non-empty xmit_fifo causes one->tx_state to be
set to MAX310X_TX_SEND even when MAX310X_TXFIFOLVL_REG is 0:

drivers/tty/serial/max310x.c:max310x_adopt_hw_tx() {
	if (!max310x_port_read(port, MAX310X_TXFIFOLVL_REG) &&
	    kfifo_is_empty(&port->state->port.xmit_fifo))
		return false;

	scoped_guard(spinlock_irqsave, &port->lock) {
		if (one->tx_teardown)
			return false;
		if (READ_ONCE(one->tx_state) != MAX310X_TX_OFF)
			return true;
		WRITE_ONCE(one->tx_state, MAX310X_TX_SEND);
		one->cancel_tx_delay_tmr = false;
	}

Between that store and max310x_restart_tx() rewinding one->tx_state back to
MAX310X_TX_OFF, the driver performs several sleeping SPI/I2C register
accesses:

CPU0                                  CPU1 (kworker)
max310x_set_termios()
  max310x_set_rts_ctl_params()
    max310x_adopt_hw_tx()
      /* TXFIFOLVL == 0, xmit_fifo != empty */
      one->tx_state = MAX310X_TX_SEND
                                      max310x_handle_tx()
                                        /* sees tx_state == MAX310X_TX_SEND */
                                        max310x_batch_write()  <-- no setup delay
    max310x_rts_ctl(..., true)
    ...
  max310x_restart_tx()

Any tx_work running in that window (or scheduled by a concurrent
max310x_start_tx() that sees MAX310X_TX_SEND) passes the new check in
max310x_handle_tx() and pushes bytes into the chip FIFO before RTS is
asserted and before delay_rts_before_send has elapsed.

[1] https://sashiko.dev/#/bug/linux-f1129200-a607-415f-a0e8-4690c614c508
[2] https://sashiko.dev/#/bug/linux-9474384d-0d83-420d-ac64-f26b48f8e734

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116-max310x-tx-fixes-v1-0-61c01f180911@vaisala.com?part=3

      reply	other threads:[~2026-11-16 10:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  9:57 [PATCH 0/3] serial: max310x: transmit path fixes Tapio Reijonen
2026-11-16  9:57 ` [PATCH 1/3] serial: max310x: do not advance the TX fifo past a flush Tapio Reijonen
2026-11-16 10:18   ` sashiko-bot
2026-11-16  9:57 ` [PATCH 2/3] serial: max310x: re-check the TX queue before the after-send hold Tapio Reijonen
2026-11-16 10:12   ` sashiko-bot
2026-11-16  9:57 ` [PATCH 3/3] serial: max310x: hold TX data until the software RTS send phase Tapio Reijonen
2026-11-16 10:34   ` sashiko-bot [this message]

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-216745@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tapio.reijonen@vaisala.com \
    /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.