From: John Ogness <john.ogness@linutronix.de>
To: Eliav Farber <farbere@amazon.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jiri Slaby <jirislaby@kernel.org>
Cc: 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: Re: [PATCH] serial: core: Don't spin for nbcon ownership with the port lock held
Date: Mon, 30 Nov 2026 18:34:04 +0106 [thread overview]
Message-ID: <87zeuq1b6j.fsf@jogness.linutronix.de> (raw)
In-Reply-To: <20261130155133.37435-1-farbere@amazon.com>
Hi Eliav,
On 2026-11-30, Eliav Farber <farbere@amazon.com> wrote:
> 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. The context is requested at
> NBCON_PRIO_NORMAL, and while another CPU is in panic that request is
> always refused, so the spin never ends.
It is always refused because it is not a reacquire.
Commit 571c1ea91a73 ("printk: nbcon: Allow reacquire during panic") only
considered the reacquire situation.
> The CPU is left holding the port lock, and in the _irq and _irqsave
> cases it is left with interrupts disabled, so it stops answering the
> stop IPI from panic().
Right, not good.
> ---
>
> 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.
Any foreign panic that occurs between the port-lock and ownership
acquire can cause this.
> - This may belong in printk instead. Letting a NORMAL requester give up, or
> allowing direct acquires during panic, was discussed at
> https://lore.kernel.org/all/84frgdcgug.fsf@jogness.linutronix.de/
> Happy to drop this if you prefer that route.
I do not think the reacquire fix should extend to general port
locking. The idea with reacquire is that the ex-owner already has the
port lock and just needs to reacquire in order to finish some important
task (such as re-enabling interrupts).
> 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);
> + }
Should these instead reuse the trylock variants? For example:
static inline void uart_port_lock(struct uart_port *up)
{
while (!uart_port_trylock(up))
cpu_relax();
}
Or perhaps in order to allow non-nbcon drivers to remain minimally
affected in behavior:
static inline void uart_port_lock(struct uart_port *up)
{
if (!__uart_port_using_nbcon(up)) {
spin_lock(&up->lock);
return;
}
while (!uart_port_trylock(up))
cpu_relax();
}
The same for the irq and irqsave variants.
Thoughts?
John Ogness
next prev parent reply other threads:[~2026-11-30 17:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 15:51 [PATCH] serial: core: Don't spin for nbcon ownership with the port lock held Eliav Farber
2026-11-30 16:09 ` sashiko-bot
2026-11-30 17:28 ` John Ogness [this message]
2026-12-01 14:58 ` Eliav Farber
2026-12-01 16:29 ` John Ogness
2026-12-02 5:24 ` Farber, Eliav
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=87zeuq1b6j.fsf@jogness.linutronix.de \
--to=john.ogness@linutronix.de \
--cc=farbere@amazon.com \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.