From: Praneeth Balasubramanian <praneethb2002@gmail.com>
To: Richard Genoud <richard.genoud@bootlin.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jiri Slaby <jirislaby@kernel.org>,
Nicolas Ferre <nicolas.ferre@microchip.com>,
Alexandre Belloni <alexandre.belloni@bootlin.com>,
Claudiu Beznea <claudiu.beznea@tuxon.dev>
Cc: Praneeth Balasubramanian <praneethb2002@gmail.com>,
stable@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-serial@vger.kernel.org,
linux-arm-kernel@lists.infradead.org
Subject: [PATCH 3/4] tty: atmel_serial: take the port lock in console write
Date: Fri, 4 Dec 2026 22:35:35 -0800 [thread overview]
Message-ID: <20261205063536.2519566-4-praneethb2002@gmail.com> (raw)
In-Reply-To: <20261205063536.2519566-1-praneethb2002@gmail.com>
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@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---
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
next prev parent reply other threads:[~2026-12-05 6:35 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 6:35 [PATCH 0/4] tty: fix a stack overread and missing locking in serial drivers Praneeth Balasubramanian
2026-12-05 6:35 ` [PATCH 1/4] tty: hvsi: fix stack overread in hvsi_version_respond() Praneeth Balasubramanian
2026-12-05 6:35 ` [PATCH 2/4] serial: men_z135_uart: do not release an unrequested memory region Praneeth Balasubramanian
2026-12-05 6:35 ` Praneeth Balasubramanian [this message]
2026-12-05 6:35 ` [PATCH 4/4] mxser: take termios_rwsem in mxser_set_serial_info() Praneeth Balasubramanian
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=20261205063536.2519566-4-praneethb2002@gmail.com \
--to=praneethb2002@gmail.com \
--cc=alexandre.belloni@bootlin.com \
--cc=claudiu.beznea@tuxon.dev \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=nicolas.ferre@microchip.com \
--cc=richard.genoud@bootlin.com \
--cc=stable@vger.kernel.org \
/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