Linux Serial subsystem development
 help / color / mirror / Atom feed
* [PATCH] serial: max310x: look at the port once after enabling its interrupts
@ 2026-11-20  7:39 Tapio Reijonen
  2026-11-20  7:53 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Tapio Reijonen @ 2026-11-20  7:39 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, Alexander Shiyan
  Cc: linux-serial, linux-kernel, Hugo Villeneuve, stable,
	Tapio Reijonen

max310x_startup() resets the FIFOs, writes the RS-485 and flow control
registers, then reads the interrupt status register to clear it and
enables the interrupts. The receiver runs from the moment the FIFO
reset is released, so a character that arrives before the status read
sets the receive status bit, and the read clears it again.

That bit is an edge: with RxEmtyInv set the chip raises it when the
receive FIFO goes from empty to not empty. The character stays in the
FIFO, so the edge does not repeat, and since the rework this is the
only receive interrupt the driver enables. No interrupt arrives and
the port receives nothing until it is closed and opened again.

Opening a port on a line that carries a continuous stream left it deaf
in 14 of 48 attempts at 9600 baud and in nearly every attempt from
38400 baud up.

Wake the interrupt thread once after the interrupts are enabled. The
port handler reads the receive FIFO level besides the status register,
so it picks up the character without its interrupt. On a chip with
more than one port the thread only visits ports that the global
interrupt register names, so mark the port in a mask that the thread
takes in addition.

The wake relies on the driver's own interrupt thread. Behind an
interrupt controller that uses nested threading there is none, the
wake does nothing and the port can still come up deaf as before.

Reading the status register before the FIFO reset is released would
keep the event without a wake, but after the release the register
reads CTSInt, RFifoEmptyInt and TFifoEmptyInt on every open. The read
after the configuration is what discards them; without it every open
raises an interrupt and reports a CTS change that did not happen.

Fixes: 10d8b34a4217 ("serial: max310x: Driver rework")
Cc: stable@vger.kernel.org
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
A port of a MAX310x chip that is opened while characters arrive on its
line can come up deaf: it receives nothing until it is closed and
opened again. The commit message has the mechanism.

Tested on a MAX14830 (four ports, SPI) on v7.4-rc3. A second device
sends a continuous stream to the port while it is opened in four ways
(plain open, open with a flush, with a termios change, with an RS-485
configuration).

Without the patch, 14 of 48 opens at 9600 baud came up deaf, and
almost every open from 38400 baud up.

With the patch:

- one port receiving, 48 opens each at 9600, 38400 and 115200 baud,
  run twice: 0 of 288 deaf;
- the same stream on all four ports of the chip, all four opened at
  the same time and one opened while the other three receive, at 9600,
  19200 and 38400 baud: 0 of 315 deaf;
- 24 opens on an idle line: no interrupt and no CTS change counted.

An alternative was tried first: reading the status register while the
FIFO reset is still asserted, so that nothing can be lost afterwards.
It cured the deafness as well, but the chip then reports CTSInt,
RFifoEmptyInt and TFifoEmptyInt on the first status read after the
interrupts are enabled, so every open raised an interrupt and reported
a CTS change that did not happen. Waking the thread leaves the status
handling as it is.

irq_wake_thread() with a mask that the thread reads in addition to the
hardware status is what pciehp_request() does with pending_events, and
oa_tc6_init() wakes its thread the same way for an event that predates
the handler.

The patch is independent of the other MAX310x fixes on the list.

The patch was written and reviewed with help from an LLM assistant
(Claude), used for code reading, review of the drafts and the test
scripts; the analysis, the change and the testing on hardware are
mine.
---
 drivers/tty/serial/max310x.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index de8a1f05cf30ed121e5d62140f2e8dccb851a4ed..cdd1655c9f35c51e0559d8c24b3bfd7c1fb79ff8 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -331,6 +331,7 @@ struct max310x_port {
 	const struct max310x_if_cfg *if_cfg;
 	struct regmap		*regmap;
 	struct clk		*clk;
+	unsigned long		poll_ports;	/* ports to service once without an IRQ */
 #ifdef CONFIG_GPIOLIB
 	struct gpio_chip	gpio;
 #endif
@@ -1076,7 +1077,7 @@ static irqreturn_t max310x_ist(int irq, void *dev_id)
 			WARN_ON_ONCE(regmap_read(s->regmap,
 						 MAX310X_GLOBALIRQ_REG, &val));
 
-			irq = val;
+			irq = val & ~xchg(&s->poll_ports, 0);
 
 			for_each_clear_bit(port, &irq, s->devtype->nr) {
 				done = false;
@@ -1539,6 +1540,7 @@ static int max310x_rs485_config(struct uart_port *port, struct ktermios *termios
 
 static int max310x_startup(struct uart_port *port)
 {
+	struct max310x_port *s = dev_get_drvdata(port->dev);
 	struct max310x_one *one = to_max310x_port(port);
 	unsigned int val;
 
@@ -1595,11 +1597,19 @@ static int max310x_startup(struct uart_port *port)
 	val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT;
 	max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT);
 
+	/*
+	 * A character received since the FIFO reset has lost its interrupt
+	 * to the status read above: have the handler look at this port once.
+	 */
+	set_bit(one - s->p, &s->poll_ports);
+	irq_wake_thread(port->irq, s);
+
 	return 0;
 }
 
 static void max310x_shutdown(struct uart_port *port)
 {
+	struct max310x_port *s = dev_get_drvdata(port->dev);
 	struct max310x_one *one = to_max310x_port(port);
 
 	/*
@@ -1612,6 +1622,7 @@ static void max310x_shutdown(struct uart_port *port)
 	scoped_guard(spinlock_irqsave, &port->lock)
 		one->tx_teardown = true;
 	max310x_port_write(port, MAX310X_IRQEN_REG, 0);
+	clear_bit(one - s->p, &s->poll_ports);
 
 	/*
 	 * Stop the transmitter: the character in flight completes, data

---
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
change-id: 20261118-max310x-startup-irq-status-0639806a1275

Best regards,
-- 
Tapio Reijonen <tapio.reijonen@vaisala.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] serial: max310x: look at the port once after enabling its interrupts
  2026-11-20  7:39 [PATCH] serial: max310x: look at the port once after enabling its interrupts Tapio Reijonen
@ 2026-11-20  7:53 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-20  7:53 UTC (permalink / raw)
  To: Tapio Reijonen; +Cc: linux-serial

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] serial: max310x: race between woken max310x_ist() and
  max310x_shutdown()
--

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

serial: max310x: look at the port once after enabling its interrupts

This commit wakes the IRQ thread once at the end of max310x_startup() and
tracks ports to poll in s->poll_ports so characters received between FIFO
reset release and the initial status read are not stranded.

> diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
> index de8a1f05cf30e..cdd1655c9f35c 100644
> --- a/drivers/tty/serial/max310x.c
> +++ b/drivers/tty/serial/max310x.c
[ ... ]
> @@ -1076,7 +1077,7 @@ static irqreturn_t max310x_ist(int irq, void *dev_id)
>  			WARN_ON_ONCE(regmap_read(s->regmap,
>  						 MAX310X_GLOBALIRQ_REG, &val));
>  
> -			irq = val;
> +			irq = val & ~xchg(&s->poll_ports, 0);
>  
>  			for_each_clear_bit(port, &irq, s->devtype->nr) {
>  				done = false;
> @@ -1539,6 +1540,7 @@ static int max310x_rs485_config(struct uart_port *port, struct ktermios *termios
>  
>  static int max310x_startup(struct uart_port *port)
>  {
> +	struct max310x_port *s = dev_get_drvdata(port->dev);
>  	struct max310x_one *one = to_max310x_port(port);
>  	unsigned int val;
>  
> @@ -1595,11 +1597,19 @@ static int max310x_startup(struct uart_port *port)
>  	val = MAX310X_IRQ_RXEMPTY_BIT | MAX310X_IRQ_TXEMPTY_BIT;
>  	max310x_port_write(port, MAX310X_IRQEN_REG, val | MAX310X_IRQ_CTS_BIT);
>  
> +	/*
> +	 * A character received since the FIFO reset has lost its interrupt
> +	 * to the status read above: have the handler look at this port once.
> +	 */
> +	set_bit(one - s->p, &s->poll_ports);
> +	irq_wake_thread(port->irq, s);
> +
>  	return 0;
>  }
>  
>  static void max310x_shutdown(struct uart_port *port)
>  {
> +	struct max310x_port *s = dev_get_drvdata(port->dev);
>  	struct max310x_one *one = to_max310x_port(port);
>  
>  	/*
> @@ -1612,6 +1622,7 @@ static void max310x_shutdown(struct uart_port *port)
>  	scoped_guard(spinlock_irqsave, &port->lock)
>  		one->tx_teardown = true;
>  	max310x_port_write(port, MAX310X_IRQEN_REG, 0);
> +	clear_bit(one - s->p, &s->poll_ports);

[Severity: High]
Can the IRQ thread woken by max310x_startup() run max310x_port_irq()
during or after max310x_shutdown(), re-arming one->tx_work after
cancel_work_sync(&one->tx_work)?

CPU0                                CPU1 (IRQ thread)
max310x_startup()
  set_bit(..., &s->poll_ports)
  irq_wake_thread(port->irq, s)
                                    max310x_ist()
                                      xchg(&s->poll_ports, 0)
max310x_shutdown()
  one->tx_teardown = true
  max310x_port_write(..., IRQEN, 0)
  clear_bit(..., &s->poll_ports)
  ...
  /* pulses MAX310X_MODE2_FIFORST_BIT,
     latching TXEMPTY in IRQSTS */
  cancel_work_sync(&one->tx_work)
  max310x_power(port, 0)
                                      max310x_port_irq()
                                        ists = ...IRQSTS_REG
                                        schedule_work(&one->tx_work)
uart_shutdown()
  uart_free_xmit_buf()
    kfree(port.xmit_fifo)
                                    max310x_tx_proc()
                                      max310x_handle_tx()
                                        <-- use-after-free on xmit_fifo

On multi-port chips, max310x_ist() consumes s->poll_ports with xchg()
before iterating over the ports, so clear_bit() in max310x_shutdown() does
not stop an already woken thread from visiting the port.  On single-port
chips (s->devtype->nr == 1), the else branch in max310x_ist() ignores
s->poll_ports altogether and unconditionally calls max310x_port_irq(s, 0).

Because serial_core only calls synchronize_irq() in uart_port_shutdown()
after uport->ops->shutdown() returns, max310x_shutdown()'s FIFO reset can
latch MAX310X_IRQ_TXEMPTY_BIT and MAX310X_IRQ_CTS_BIT in
MAX310X_IRQSTS_REG, and max310x_port_irq() does not check
one->tx_teardown before scheduling one->tx_work.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-max310x-startup-irq-status-v1-1-aee4e1416cf6@vaisala.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-20  7:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  7:39 [PATCH] serial: max310x: look at the port once after enabling its interrupts Tapio Reijonen
2026-11-20  7:53 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox