From: sashiko-bot@kernel.org
To: "Tapio Reijonen" <tapio.reijonen@vaisala.com>
Cc: linux-serial@vger.kernel.org
Subject: Re: [PATCH v2 3/4] serial: max310x: hold TX data until the software RTS send phase
Date: Tue, 17 Nov 2026 06:48:36 +0000 [thread overview]
Message-ID: <sashiko-outbox-218280@kernel.org> (raw)
In-Reply-To: <20261117-max310x-tx-fixes-v2-3-b395126fd23d@vaisala.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] serial: max310x: x_char sent before software RTS send-phase
check
- [Medium] serial: max310x: max310x_adopt_hw_tx() bypasses send-phase
hold on empty FIFO
- [Medium] serial: max310x: early return in max310x_restart_tx() strands
TX_SEND state
--
commit 4da682996f111cc76f553602be970865e015bb7f
Author: Tapio Reijonen <tapio.reijonen@vaisala.com>
serial: max310x: hold TX data until the software RTS send phase
This commit holds TX FIFO writes in max310x_handle_tx() until a
software-timed RS-485 RTS envelope reaches MAX310X_TX_SEND, and adds
max310x_restart_tx() to restart pending transmissions after termios or
RS-485 reconfigurations.
> 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)) {
[Severity: Medium]
Should this send-phase check also gate the port->x_char write at the top
of max310x_handle_tx()?
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() queues an XON/XOFF byte on the software RTS path,
max310x_start_tx() starts an envelope in MAX310X_TX_WAIT_BEFORE_SEND (and
max310x_restart_tx() likewise routes a deferred port->x_char through
max310x_start_tx()). If a late TXEMPTY interrupt from the previous
envelope schedules tx_work before tx_delay_tmr enters MAX310X_TX_SEND,
max310x_handle_tx() writes port->x_char to MAX310X_THR_REG and clears it
while RTS is still deasserted or in its setup delay, losing the character
on the wire and leaving the envelope to run empty once the timer expires.
[ ... ]
> @@ -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 here when xmit_fifo is empty or stopped leave
one->tx_state stranded in MAX310X_TX_SEND with RTS permanently asserted?
When max310x_set_rts_ctl_params() adopts a transfer whose data is only in
xmit_fifo (MAX310X_TXFIFOLVL_REG == 0), max310x_adopt_hw_tx() sets
one->tx_state to MAX310X_TX_SEND, asserts RTS, and calls
max310x_delayed_stop_tx(), which returns 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 non-empty: no timer */
uart_flush_buffer()
kfifo_reset(&tport->xmit_fifo)
max310x_restart_tx()
if (kfifo_is_empty(...))
return; <-- tx_state stuck in MAX310X_TX_SEND
Because the chip TX FIFO is already empty, no TXEMPTY interrupt will fire
to schedule tx_work, and neither uart_flush_buffer() nor stop_tty() runs
max310x_delayed_stop_tx(), leaving RTS driving the bus until a subsequent
write or port close.
> @@ -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() and
bypass the send-phase hold when the chip TX FIFO is empty?
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);
...
}
When max310x_set_termios() or max310x_rs_proc() switches to software RTS
with an empty chip FIFO and bytes waiting in xmit_fifo, one->tx_state
stays set to MAX310X_TX_SEND across the unlocked register updates in
max310x_set_rts_ctl_params() and the MAX310X_TXFIFOLVL_REG read before
max310x_restart_tx() rewinds it to MAX310X_TX_OFF:
CPU0 kworker
max310x_set_termios()
max310x_set_rts_ctl_params()
one->sw_rts_during_tx = true
max310x_adopt_hw_tx()
/* TXFIFOLVL == 0, kfifo != empty */
one->tx_state = MAX310X_TX_SEND
max310x_tx_proc()
max310x_handle_tx()
/* sees 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 write()
seeing MAX310X_TX_SEND in max310x_start_tx()) pushes the queued data to
the chip before RTS is asserted or before delay_rts_before_send has
elapsed.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-max310x-tx-fixes-v2-0-b395126fd23d@vaisala.com?part=3
next prev parent reply other threads:[~2026-11-17 6:48 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 6:14 [PATCH v2 0/4] serial: max310x: transmit path fixes Tapio Reijonen
2026-11-17 6:14 ` [PATCH v2 1/4] serial: max310x: do not advance the TX fifo past a flush Tapio Reijonen
2026-11-17 6:31 ` sashiko-bot
2026-11-17 6:14 ` [PATCH v2 2/4] serial: max310x: re-check the TX queue before the after-send hold Tapio Reijonen
2026-11-17 6:21 ` sashiko-bot
2026-11-17 6:14 ` [PATCH v2 3/4] serial: max310x: hold TX data until the software RTS send phase Tapio Reijonen
2026-11-17 6:48 ` sashiko-bot [this message]
2026-11-17 6:14 ` [PATCH v2 4/4] serial: max310x: end the RTS envelope when a flush leaves nothing to send Tapio Reijonen
2026-11-17 6:22 ` sashiko-bot
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-218280@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox