* [PATCH v2 1/4] serial: max310x: do not advance the TX fifo past a flush
2026-11-17 6:14 [PATCH v2 0/4] serial: max310x: transmit path fixes Tapio Reijonen
@ 2026-11-17 6:14 ` 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
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ messages in thread
From: Tapio Reijonen @ 2026-11-17 6:14 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: linux-serial, linux-kernel, Hugo Villeneuve, Tapio Reijonen,
stable
max310x_handle_tx() runs from a work item. It peeks at the transmit
fifo with kfifo_out_linear_ptr(), writes the bytes to the chip over
SPI, which sleeps, and advances the fifo afterwards with
uart_xmit_advance(), all without the port lock. uart_flush_buffer()
resets the fifo under the port lock. When the reset lands between the
peek and the advance, the advance adds the chunk length to a read
index that is already zero, the index ends up ahead of the write
index, and kfifo_len() wraps to a value near 4G.
From then on the fifo never reads as empty: tcdrain() sleeps forever
in tty_wait_until_sent(), and while the port stays open the work item
keeps sending stale bytes from the buffer. Observed with a tcdrain()
in flight at 300 baud when the port was hung up by an unbind, which
flushes twice: the first flush left 498 bytes, the second found
4294967217, the draining task read 4294967276 and never woke again.
Before the kfifo conversion the same race left the circular buffer
with up to 4 KiB of stale data to resend, after which it recovered.
Copy the bytes into a per-port buffer under the port lock, send them
from there, and take the lock again for the advance. A flush_buffer()
callback records a reset of the fifo, and the advance is skipped when
one happened during the transfer. kfifo_reset() is documented to
require that no other thread accesses the fifo, so the consumer has to
hold the same lock as the flush.
The bytes stay in the fifo until the chip has them. tx_empty() reads
only the chip FIFO level, so taking them out before the transfer would
make tcdrain() and close() see an idle transmitter with up to one FIFO
load still to be sent.
The lock is not held over the SPI transfer. regmap hands that to the
controller straight from the buffer, which is why the buffer is per
port and not on the stack. The copy is limited to the buffer size: its
length is derived from port->fifosize, which TIOCSSERIAL can change,
and from a FIFO level read from the chip.
Fixes: 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo")
Cc: stable@vger.kernel.org
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
drivers/tty/serial/max310x.c | 46 ++++++++++++++++++++++++++++++++++++--------
1 file changed, 38 insertions(+), 8 deletions(-)
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index a0ed70185555eed745d905b36c8b8f162468dd6f..e6a740295b96653281a92f8d94999f06c2c74685 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -317,11 +317,13 @@ struct max310x_one {
bool sw_rts_during_tx;
bool cancel_tx_delay_tmr;
bool tx_teardown; /* envelope being torn down */
+ bool tx_flushed; /* fifo reset under a transfer */
bool rs485_pending; /* rs_work not yet applied */
bool tx_break; /* break_ctl() owns the transceiver */
enum max310x_tx_state tx_state;
u8 rx_buf[MAX310X_FIFO_SIZE];
+ u8 tx_buf[MAX310X_FIFO_SIZE];
};
#define to_max310x_port(_port) \
container_of(_port, struct max310x_one, port)
@@ -911,6 +913,7 @@ static void max310x_delayed_stop_tx(struct uart_port *port)
static void max310x_handle_tx(struct uart_port *port)
{
+ struct max310x_one *one = to_max310x_port(port);
struct tty_port *tport = &port->state->port;
if (unlikely(port->x_char)) {
@@ -931,23 +934,41 @@ static void max310x_handle_tx(struct uart_port *port)
return;
}
- /*
- * It's a circ buffer -- wrap around.
- * We could do that in one SPI transaction, but meh.
- */
while (!kfifo_is_empty(&tport->xmit_fifo)) {
unsigned int txlen, to_send;
- unsigned char *tail;
+ unsigned long flags;
/* Limit to space available in TX FIFO */
txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
txlen = port->fifosize - txlen;
+ txlen = min(txlen, sizeof(one->tx_buf));
if (!txlen)
break;
- to_send = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail, txlen);
- max310x_batch_write(port, tail, to_send);
- uart_xmit_advance(port, to_send);
+ /*
+ * Copy the bytes under the port lock, but leave them in the
+ * fifo until the chip has them: an empty fifo with an empty
+ * chip FIFO reads as an idle transmitter.
+ */
+ uart_port_lock_irqsave(port, &flags);
+ to_send = kfifo_out_peek(&tport->xmit_fifo, one->tx_buf, txlen);
+ one->tx_flushed = false;
+ uart_port_unlock_irqrestore(port, flags);
+ if (!to_send)
+ break;
+
+ max310x_batch_write(port, one->tx_buf, to_send);
+
+ /*
+ * uart_flush_buffer() reset the fifo during the transfer:
+ * advancing now would move the read index past that reset.
+ */
+ uart_port_lock_irqsave(port, &flags);
+ if (one->tx_flushed)
+ port->icount.tx += to_send;
+ else
+ uart_xmit_advance(port, to_send);
+ uart_port_unlock_irqrestore(port, flags);
}
if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
@@ -1094,6 +1115,14 @@ static irqreturn_t max310x_ist(int irq, void *dev_id)
return IRQ_RETVAL(handled);
}
+/* called with port.lock taken and irqs off */
+static void max310x_flush_buffer(struct uart_port *port)
+{
+ struct max310x_one *one = to_max310x_port(port);
+
+ one->tx_flushed = true;
+}
+
static void max310x_tx_proc(struct work_struct *ws)
{
struct max310x_one *one = container_of(ws, struct max310x_one, tx_work);
@@ -1732,6 +1761,7 @@ static const struct uart_ops max310x_ops = {
.break_ctl = max310x_break_ctl,
.startup = max310x_startup,
.shutdown = max310x_shutdown,
+ .flush_buffer = max310x_flush_buffer,
.set_termios = max310x_set_termios,
.type = max310x_type,
.request_port = max310x_request_port,
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 1/4] serial: max310x: do not advance the TX fifo past a flush
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
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 6:31 UTC (permalink / raw)
To: Tapio Reijonen; +Cc: linux-serial
> max310x_handle_tx() runs from a work item. It peeks at the transmit
> fifo with kfifo_out_linear_ptr(), writes the bytes to the chip over
> SPI, which sleeps, and advances the fifo afterwards with
> uart_xmit_advance(), all without the port lock. uart_flush_buffer()
> resets the fifo under the port lock. When the reset lands between the
> peek and the advance, the advance adds the chunk length to a read
> index that is already zero, the index ends up ahead of the write
> index, and kfifo_len() wraps to a value near 4G.
>
> From then on the fifo never reads as empty: tcdrain() sleeps forever
> in tty_wait_until_sent(), and while the port stays open the work item
> keeps sending stale bytes from the buffer. Observed with a tcdrain()
> in flight at 300 baud when the port was hung up by an unbind, which
> flushes twice: the first flush left 498 bytes, the second found
> 4294967217, the draining task read 4294967276 and never woke again.
> [ ... ]
> and from a FIFO level read from the chip.
>
> Fixes: 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo")
> Cc: stable@vger.kernel.org
> Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-max310x-tx-fixes-v2-0-b395126fd23d@vaisala.com?part=1
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 2/4] serial: max310x: re-check the TX queue before the after-send hold
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:14 ` 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:14 ` [PATCH v2 4/4] serial: max310x: end the RTS envelope when a flush leaves nothing to send Tapio Reijonen
3 siblings, 1 reply; 9+ messages in thread
From: Tapio Reijonen @ 2026-11-17 6:14 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: linux-serial, linux-kernel, Hugo Villeneuve, Tapio Reijonen
max310x_delayed_stop_tx() decides that a software-timed RS-485 envelope
is over when the transmit queue is empty and the chip's TX FIFO is
empty. It tests the queue first, then reads TXFIFOLVL over the bus, and
only then takes the port lock and arms the after-send hold. The bus read
sleeps, under load for tens of milliseconds, and write() queues data
under the port lock in that window. start_tx() finds the envelope in
MAX310X_TX_SEND and does nothing but reschedule tx_work.
When the stop routine resumes it sees an empty chip FIFO and arms the
hold on a queue that was empty before the read. The hold expires, the
RTS release is scheduled, the rescheduled tx_work pushes the new data
into the chip FIFO, and RTS drops while the chip is still shifting it
out. The tail of the write leaves through a disabled transceiver. No
FIFO overflows, so no counter moves; the loss is silent.
Seen on a MAX14830 behind a slow SPI path with 20 ms of before-send
delay: 25-byte writes every 20 ms lost 1600 to 2200 bytes per port in
five minutes in RS-485 mode and nothing in RS-232 mode under the same
traffic, overrun counters equal; 96-byte writes every 60 ms lost
partial chunks of 14 to 75 bytes a few times per run.
Re-check the queue under the port lock before arming the hold. Data
queued before the check keeps the envelope open and the pending tx_work
ends it once that data has drained; data queued after the hold is
armed is already handled by start_tx(), which cancels the hold.
Fixes: 33632215e1bb ("serial: max310x: drive RTS in software when hardware delays are too short")
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
drivers/tty/serial/max310x.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index e6a740295b96653281a92f8d94999f06c2c74685..7ddfe8773faf9bc51621c49fa5d8e7d6b1e4bc34 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -897,6 +897,14 @@ static void max310x_delayed_stop_tx(struct uart_port *port)
READ_ONCE(one->tx_state) != MAX310X_TX_SEND)
return;
+ /*
+ * Data queued while TXFIFOLVL was read: start_tx() saw SEND and only
+ * rescheduled tx_work, so arming the hold now would drop RTS under it.
+ */
+ if (!kfifo_is_empty(&port->state->port.xmit_fifo) &&
+ !uart_tx_stopped(port))
+ return;
+
if (!hrtimer_active(&one->tx_delay_tmr)) {
/*
* Add one character for the byte still in the shift register -
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 2/4] serial: max310x: re-check the TX queue before the after-send hold
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
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 6:21 UTC (permalink / raw)
To: Tapio Reijonen; +Cc: linux-serial
> max310x_delayed_stop_tx() decides that a software-timed RS-485 envelope
> is over when the transmit queue is empty and the chip's TX FIFO is
> empty. It tests the queue first, then reads TXFIFOLVL over the bus, and
> only then takes the port lock and arms the after-send hold. The bus read
> sleeps, under load for tens of milliseconds, and write() queues data
> under the port lock in that window. start_tx() finds the envelope in
> MAX310X_TX_SEND and does nothing but reschedule tx_work.
>
> When the stop routine resumes it sees an empty chip FIFO and arms the
> hold on a queue that was empty before the read. The hold expires, the
> RTS release is scheduled, the rescheduled tx_work pushes the new data
> into the chip FIFO, and RTS drops while the chip is still shifting it
> out. The tail of the write leaves through a disabled transceiver. No
> FIFO overflows, so no counter moves; the loss is silent.
>
> Seen on a MAX14830 behind a slow SPI path with 20 ms of before-send
> delay: 25-byte writes every 20 ms lost 1600 to 2200 bytes per port in
> five minutes in RS-485 mode and nothing in RS-232 mode under the same
> traffic, overrun counters equal; 96-byte writes every 60 ms lost
> partial chunks of 14 to 75 bytes a few times per run.
>
> Re-check the queue under the port lock before arming the hold. Data
> queued before the check keeps the envelope open and the pending tx_work
> ends it once that data has drained; data queued after the hold is
> armed is already handled by start_tx(), which cancels the hold.
>
> Fixes: 33632215e1bb ("serial: max310x: drive RTS in software when hardware delays are too short")
> Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-max310x-tx-fixes-v2-0-b395126fd23d@vaisala.com?part=2
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 3/4] serial: max310x: hold TX data until the software RTS send phase
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:14 ` [PATCH v2 2/4] serial: max310x: re-check the TX queue before the after-send hold Tapio Reijonen
@ 2026-11-17 6:14 ` Tapio Reijonen
2026-11-17 6:48 ` sashiko-bot
2026-11-17 6:14 ` [PATCH v2 4/4] serial: max310x: end the RTS envelope when a flush leaves nothing to send Tapio Reijonen
3 siblings, 1 reply; 9+ messages in thread
From: Tapio Reijonen @ 2026-11-17 6:14 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: linux-serial, linux-kernel, Hugo Villeneuve, Tapio Reijonen
On the software-timed RS-485 path, start_tx() begins an envelope by
setting MAX310X_TX_WAIT_BEFORE_SEND and queuing the RTS worker; the
data may only go to the chip once the before-send timer has moved the
envelope to MAX310X_TX_SEND and scheduled tx_work. Nothing enforced
that: max310x_handle_tx() pushed whatever was queued whenever it ran.
The chip latches its TX-empty interrupt when the last byte of the
previous envelope leaves the FIFO. Under load the IRQ thread reads that
bit late, after the next write has started a new envelope, and
schedules tx_work for it. That run found the new data and pushed it
into the FIFO while RTS was still down or the setup delay still
running; the chip transmitted at once, the head of the write left
through a disabled transceiver and the receiver saw the rest misframed.
Seen on a MAX14830 with a 20 ms before-send delay: with 25-byte writes
every 20 ms, 33 to 88 such losses per port in five minutes remained
after the after-send race was fixed, several of them as misframed
bytes.
Hold the data while the envelope is not in its send phase. The
before-send timer schedules tx_work when it moves the envelope to
MAX310X_TX_SEND, and so does a write arriving during a send, so the
data leaves as soon as RTS is up.
One more path enters MAX310X_TX_SEND, and it does not schedule
tx_work: a set_termios() that moves a running transfer from chip-timed
to driver-timed RTS adopts it in max310x_adopt_hw_tx(). A tx_work that
runs after the path has changed and before the adoption is now held,
and with the chip FIFO empty nothing sends the queued data until the
serial core's runtime PM calls start_tx() about 0.5 s later. With only
the hold in place, 10 of 338 baud rate changes made near the end of a
chip FIFO load stalled for 370 to 490 ms. Restart the transmission at
the end of set_termios() the way rs_work does after an RS485
reconfigure, through a helper for both. The helper leaves stopped
output alone, as serial core does.
A software envelope between two FIFO loads looks the same as an
adopted transfer, so a set_termios() that lands there restarts it as
well: RTS stays asserted and the rest of the data waits for one more
before-send delay. rs_work has always done that for TIOCSRS485.
The comment in the IRQ handler said that a stale TX-empty interrupt
cannot push data during the setup delay; correct it. The hold does
not apply to the hardware RTS path, which does not use tx_state.
Fixes: 33632215e1bb ("serial: max310x: drive RTS in software when hardware delays are too short")
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
drivers/tty/serial/max310x.c | 72 ++++++++++++++++++++++++++++++++------------
1 file changed, 52 insertions(+), 20 deletions(-)
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index 7ddfe8773faf9bc51621c49fa5d8e7d6b1e4bc34..d92bf241558862cd83073c1d12ec9f0aaf88f090 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;
@@ -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);
@@ -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);
+}
+
static void max310x_break_ctl(struct uart_port *port, int break_state)
{
struct max310x_one *one = to_max310x_port(port);
@@ -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;
/* Mask termios capabilities we don't support */
@@ -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);
}
static void max310x_rs_proc(struct work_struct *ws)
@@ -1518,26 +1562,14 @@ static void max310x_rs_proc(struct work_struct *ws)
/*
* The configuration is applied: release any TX that start_tx()
* deferred while the reconfigure was pending, now on the right
- * path. start_tx() can drop and retake the lock through the
- * uart_port API - a raw spinlock guard here would unbalance it.
+ * path.
*/
chip_tx_empty = !max310x_port_read(&one->port,
MAX310X_TXFIFOLVL_REG);
uart_port_lock_irqsave(&one->port, &flags);
one->rs485_pending = false;
- if (one->port.x_char ||
- !kfifo_is_empty(&one->port.state->port.xmit_fifo)) {
- /*
- * A send state adopted without chip data is the deferred
- * write itself: restart it as a fresh envelope so the
- * before-send delay is honoured.
- */
- if (chip_tx_empty &&
- READ_ONCE(one->tx_state) == MAX310X_TX_SEND)
- WRITE_ONCE(one->tx_state, MAX310X_TX_OFF);
- max310x_start_tx(&one->port);
- }
+ max310x_restart_tx(one, chip_tx_empty);
uart_port_unlock_irqrestore(&one->port, flags);
}
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 3/4] serial: max310x: hold TX data until the software RTS send phase
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
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 6:48 UTC (permalink / raw)
To: Tapio Reijonen; +Cc: linux-serial
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2 4/4] serial: max310x: end the RTS envelope when a flush leaves nothing to send
2026-11-17 6:14 [PATCH v2 0/4] serial: max310x: transmit path fixes Tapio Reijonen
` (2 preceding siblings ...)
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:14 ` Tapio Reijonen
2026-11-17 6:22 ` sashiko-bot
3 siblings, 1 reply; 9+ messages in thread
From: Tapio Reijonen @ 2026-11-17 6:14 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: linux-serial, linux-kernel, Hugo Villeneuve, Tapio Reijonen,
Sashiko
With software-timed RTS, max310x_handle_tx() ends the envelope only at
its entry check, when it finds the transmit fifo empty. If
uart_flush_buffer() empties the fifo after that check, while the
function reads TXFIFOLVL, the loop ends without writing to the chip and
the function returns. Nothing was written, so no TX-empty interrupt
follows. tx_state stays at MAX310X_TX_SEND and RTS stays asserted until
the next write or until the port is closed, and an RS-485 transceiver
blocks the bus for that time.
A tcflush(TCOFLUSH) timed to the end of the delay before send
reproduces it: 68 of 975 flushes left the port in the send state, seen
by a following byte that left without the delay.
Call max310x_delayed_stop_tx() when the loop has pushed nothing. It
returns when data is still queued or the chip FIFO still drains, and
arms the after-send hold otherwise. With the change, 9509 such flushes
left no port in the send state.
Fixes: 33632215e1bb ("serial: max310x: drive RTS in software when hardware delays are too short")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20261116-max310x-tx-fixes-v1-0-61c01f180911%40vaisala.com
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
drivers/tty/serial/max310x.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index d92bf241558862cd83073c1d12ec9f0aaf88f090..4d8b7d79f2c9450a1e8c6d382710cbe526196ae1 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -923,6 +923,7 @@ static void max310x_handle_tx(struct uart_port *port)
{
struct max310x_one *one = to_max310x_port(port);
struct tty_port *tport = &port->state->port;
+ bool pushed = false;
if (unlikely(port->x_char)) {
max310x_port_write(port, MAX310X_THR_REG, port->x_char);
@@ -976,6 +977,7 @@ static void max310x_handle_tx(struct uart_port *port)
break;
max310x_batch_write(port, one->tx_buf, to_send);
+ pushed = true;
/*
* uart_flush_buffer() reset the fifo during the transfer:
@@ -989,6 +991,14 @@ static void max310x_handle_tx(struct uart_port *port)
uart_port_unlock_irqrestore(port, flags);
}
+ /*
+ * Nothing went to the chip. If a flush has emptied the fifo since
+ * the check above, no TX-empty interrupt follows to end the
+ * envelope.
+ */
+ if (!pushed)
+ max310x_delayed_stop_tx(port);
+
if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
uart_write_wakeup(port);
}
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v2 4/4] serial: max310x: end the RTS envelope when a flush leaves nothing to send
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
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-11-17 6:22 UTC (permalink / raw)
To: Tapio Reijonen; +Cc: linux-serial
> With software-timed RTS, max310x_handle_tx() ends the envelope only at
> its entry check, when it finds the transmit fifo empty. If
> uart_flush_buffer() empties the fifo after that check, while the
> function reads TXFIFOLVL, the loop ends without writing to the chip and
> the function returns. Nothing was written, so no TX-empty interrupt
> follows. tx_state stays at MAX310X_TX_SEND and RTS stays asserted until
> the next write or until the port is closed, and an RS-485 transceiver
> blocks the bus for that time.
>
> A tcflush(TCOFLUSH) timed to the end of the delay before send
> reproduces it: 68 of 975 flushes left the port in the send state, seen
> by a following byte that left without the delay.
>
> Call max310x_delayed_stop_tx() when the loop has pushed nothing. It
> returns when data is still queued or the chip FIFO still drains, and
> arms the after-send hold otherwise. With the change, 9509 such flushes
> left no port in the send state.
>
> Fixes: 33632215e1bb ("serial: max310x: drive RTS in software when hardware delays are too short")
> Reported-by: Sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/patchset/20261116-max310x-tx-fixes-v1-0-61c01f180911%40vaisala.com
> Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117-max310x-tx-fixes-v2-0-b395126fd23d@vaisala.com?part=4
^ permalink raw reply [flat|nested] 9+ messages in thread