Linux Serial subsystem development
 help / color / mirror / Atom feed
From: Eliav Farber <farbere@amazon.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Slaby <jirislaby@kernel.org>
Cc: John Ogness <john.ogness@linutronix.de>,
	Petr Mladek <pmladek@suse.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Sergey Senozhatsky <senozhatsky@chromium.org>,
	<printk@lists.linux.dev>, <linux-serial@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, Eliav Farber <farbere@amazon.com>
Subject: [PATCH v2] serial: core: Don't spin for nbcon ownership with the port lock held
Date: Wed, 2 Dec 2026 09:27:12 +0000	[thread overview]
Message-ID: <20261202092712.18975-1-farbere@amazon.com> (raw)

uart_port_lock() and its variants take the port lock and then spin until
they own the nbcon console context, which synchronizes port->lock users
against the write_atomic() callback. During a panic on another CPU the
only acquire still allowed is a reacquire by the context that lost
ownership, see commit 571c1ea91a73 ("printk: nbcon: Allow reacquire
during panic"). The wrapper's acquire is not one, so it is always
refused and the spin never ends.

A panic on another CPU only has to land between taking the port lock and
acquiring ownership. The CPU is then left holding the port lock, and in
the _irq and _irqsave cases with interrupts disabled, so it stops
answering the stop IPI from panic(). Triggering a panic from a shell on
the serial console reproduces this: a CPU enters the wrapper while the
tty is transmitting and never leaves it. On an 8250 console the panic
output slows to tens of milliseconds per character and never reaches the
reboot.

Fix this by retrying with the port lock released and interrupts
restored. __uart_port_using_nbcon() is re-tested on each attempt because
the console can be unregistered while the lock is dropped. A caller that
already has interrupts disabled still spins, but no longer holds the
port lock.

This message and change were written with the help of an AI coding
assistant. I reviewed everything, corrected a few things, and tested
the result on arm64 hardware with an 8250 console.

Fixes: 4126f149c48b ("serial: core: Acquire nbcon context in port->lock wrapper")
Cc: stable@vger.kernel.org
Signed-off-by: Eliav Farber <farbere@amazon.com>
Reviewed-by: John Ogness <john.ogness@linutronix.de>
---

Changes in v2:
- Commit log only. The diff is identical to v1.
- Reworded the first two paragraphs per review: the wrapper's acquire is
  refused because it is not a reacquire, not because it is at
  NBCON_PRIO_NORMAL, and the window between taking the port lock and
  acquiring ownership is now named.
- Added the Reviewed-by.

v1: https://lore.kernel.org/all/20261130155133.37435-1-farbere@amazon.com/

Not for the commit log:

- The spin is in the shared wrapper, so every nbcon console with a
  write_atomic() callback is exposed, not only 8250. Tested on 8250. My
  pl011 boards panic normally, and I have no hardware for the other four
  drivers.

- The IRQs-off caller named in the commit log is not hypothetical:
  serial8250_handle_irq() takes the port lock through
  guard(uart_port_lock_check_sysrq_irqsave), in hardirq context.

- Tested on arm64 with a 16550A console at 115200. The panic was triggered
  by sysrq from a shell running on that console, so the tty was
  transmitting as the panic started. Before, one record took 11.8 s and the
  board never rebooted. After, 1.5 s from crash to "Rebooting in". A WARN
  that used to kill the board now prints 1172 characters in 104 ms.

 include/linux/serial_core.h | 38 +++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index 4eeb8253752a..24d1e693d836 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -665,7 +665,15 @@ static inline bool __uart_port_using_nbcon(struct uart_port *up)
 	return true;
 }
 
-/* Only for internal port lock wrapper usage. */
+/*
+ * Only for internal port lock wrapper usage.
+ *
+ * The wrappers must not spin for ownership while holding the port lock. A
+ * request at NBCON_PRIO_NORMAL is refused for as long as another CPU is in
+ * panic, so the wait is unbounded. The wrappers drop the lock between
+ * attempts, so re-test __uart_port_using_nbcon() each time. The console can
+ * be unregistered while the lock is not held.
+ */
 static inline bool __uart_port_nbcon_try_acquire(struct uart_port *up)
 {
 	if (!__uart_port_using_nbcon(up))
@@ -674,16 +682,6 @@ static inline bool __uart_port_nbcon_try_acquire(struct uart_port *up)
 	return nbcon_device_try_acquire(up->cons);
 }
 
-/* Only for internal port lock wrapper usage. */
-static inline void __uart_port_nbcon_acquire(struct uart_port *up)
-{
-	if (!__uart_port_using_nbcon(up))
-		return;
-
-	while (!nbcon_device_try_acquire(up->cons))
-		cpu_relax();
-}
-
 /* Only for internal port lock wrapper usage. */
 static inline void __uart_port_nbcon_release(struct uart_port *up)
 {
@@ -700,7 +698,11 @@ static inline void __uart_port_nbcon_release(struct uart_port *up)
 static inline void uart_port_lock(struct uart_port *up)
 {
 	spin_lock(&up->lock);
-	__uart_port_nbcon_acquire(up);
+	while (!__uart_port_nbcon_try_acquire(up)) {
+		spin_unlock(&up->lock);
+		cpu_relax();
+		spin_lock(&up->lock);
+	}
 }
 
 /**
@@ -710,7 +712,11 @@ static inline void uart_port_lock(struct uart_port *up)
 static inline void uart_port_lock_irq(struct uart_port *up)
 {
 	spin_lock_irq(&up->lock);
-	__uart_port_nbcon_acquire(up);
+	while (!__uart_port_nbcon_try_acquire(up)) {
+		spin_unlock_irq(&up->lock);
+		cpu_relax();
+		spin_lock_irq(&up->lock);
+	}
 }
 
 /**
@@ -721,7 +727,11 @@ static inline void uart_port_lock_irq(struct uart_port *up)
 static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned long *flags)
 {
 	spin_lock_irqsave(&up->lock, *flags);
-	__uart_port_nbcon_acquire(up);
+	while (!__uart_port_nbcon_try_acquire(up)) {
+		spin_unlock_irqrestore(&up->lock, *flags);
+		cpu_relax();
+		spin_lock_irqsave(&up->lock, *flags);
+	}
 }
 
 /**

base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
-- 
2.47.3


             reply	other threads:[~2026-12-02  9:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:27 Eliav Farber [this message]
2026-12-02  9:32 ` [PATCH v2] serial: core: Don't spin for nbcon ownership with the port lock held 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=20261202092712.18975-1-farbere@amazon.com \
    --to=farbere@amazon.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@kernel.org \
    --cc=john.ogness@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=pmladek@suse.com \
    --cc=printk@lists.linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.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