[PATCH 3/4] tty: atmel_serial: take the port lock in console write

From: Praneeth Balasubramanian

Date: Sat Dec 05 2026 - 01:36:03 EST


atmel_console_write() touches the PDC and USART TX control registers and
the tx_stopped flag without holding the port lock. atmel_stop_tx() and
atmel_start_tx() run under the lock. So the console can race with a
TCXONC ioctl or another path that stops the transmitter. The comment
that says interrupts are disabled on entry does not hold on PREEMPT_RT.
There legacy consoles are printed from a kthread, and con->write runs in
task context with interrupts enabled.

If atmel_stop_tx() runs while the console is between its PDC_TXTEN
snapshot and the TXTEN restore, the console re-enables a transmitter that
was just stopped and writes back a stale PDC_TXTEN value. The port ends up
in an inconsistent state and serial output can stall.

Take the port lock for the whole callback so the register accesses are
serialized against the other paths. Drop the stale comment. Follow the
pl011 pattern: skip the lock when a sysrq is being handled on the port,
since atmel_rx_chars() calls uart_handle_sysrq_char() under the port lock,
and use a trylock when an oops is in progress.

This was found by an LLM-based audit that checks code against the rules
stated in kernel comments.

Fixes: 39d4c922b596 ("atmel_serial: fix uart/console concurrent access")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Praneeth Balasubramanian <praneethb2002@xxxxxxxxx>
---

Notes:
This was found by an LLM-based audit, part of research at UCSB SecLab.
An LLM turned a kernel comment into a rule, and three LLM votes checked
the code against it. The fix and changelog were drafted with GLM-5.3,
then checked against the source and partly rewritten with Claude Opus
5.5. I reviewed the patch and take responsibility for it.

Testing: builds without new warnings with W=1 (x86_64 allmodconfig),
clang 23. checkpatch.pl is clean. Not runtime-tested, and there is no
reproducer. I do not have this hardware.

James Hilliard's serial port lock series (v14, 2026-12-04) changes other
functions in atmel_serial.c, not atmel_console_write(). The two may need
a rebase against each other.

drivers/tty/serial/atmel_serial.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/tty/serial/atmel_serial.c b/drivers/tty/serial/atmel_serial.c
index c3880259e3bc..7446c956262a 100644
--- a/drivers/tty/serial/atmel_serial.c
+++ b/drivers/tty/serial/atmel_serial.c
@@ -2560,15 +2560,21 @@ static void atmel_console_putchar(struct uart_port *port, unsigned char ch)
atmel_uart_write_char(port, ch);
}

-/*
- * Interrupts are disabled on entering
- */
static void atmel_console_write(struct console *co, const char *s, u_int count)
{
struct uart_port *port = &atmel_ports[co->index].uart;
struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
unsigned int status, imr;
unsigned int pdc_tx;
+ unsigned long flags;
+ int locked = 1;
+
+ if (port->sysrq)
+ locked = 0;
+ else if (oops_in_progress)
+ locked = uart_port_trylock_irqsave(port, &flags);
+ else
+ uart_port_lock_irqsave(port, &flags);

/*
* First, save IMR and then disable interrupts
@@ -2601,6 +2607,9 @@ static void atmel_console_write(struct console *co, const char *s, u_int count)

/* set interrupts back the way they were */
atmel_uart_writel(port, ATMEL_US_IER, imr);
+
+ if (locked)
+ uart_port_unlock_irqrestore(port, flags);
}

/*
--
2.25.1