Re: [PATCH] serial: core: Don't spin for nbcon ownership with the port lock held

From: Eliav Farber

Date: Tue Dec 01 2026 - 09:58:37 EST


On 2026-11-30, John Ogness <john.ogness@xxxxxxxxxxxxx> 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.

Right. I will reword it to:

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.

> Any foreign panic that occurs between the port-lock and ownership
> acquire can cause this.

Yes. I will put that in the commit log.

>> - This may belong in printk instead. [...]
>
> I do not think the reacquire fix should extend to general port
> locking. [...]

OK, I will drop that note.

> 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();
> }

Tested this and it fixes the stall. The irq variant has no counterpart
though, so it needs a new uart_port_trylock_irq() on spin_trylock_irq().

> 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;
> }

That one cannot be used as written. __uart_port_using_nbcon() starts
with lockdep_assert_held_once(&up->lock), and the up->cons reads below
it are only safe under the port lock, as the comment there says.

The patch as posted already leaves non-nbcon ports alone: such a port
takes spin_lock() and one __uart_port_using_nbcon() test that returns
false, which is what __uart_port_nbcon_acquire() did.

If you want the trylock wrappers reused anyway, this keeps the assert
valid and leaves the normal path on spin_lock():

static inline void uart_port_lock(struct uart_port *up)
{
spin_lock(&up->lock);
if (__uart_port_nbcon_try_acquire(up))
return;
spin_unlock(&up->lock);

while (!uart_port_trylock(up))
cpu_relax();
}

Which one do you want in v2?

Eliav