From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=52.12.53.23 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796203643; b=DERb+n3rBQjWxTTlEZfqvMXzuq4ChCVKUiV7jYcAHMY5FTqQECuJeUqESKpYPC33JZ4I mio7mwk5/WgKFoqiyo8avGcSqEbnFuZtBj6ppeUmf1NMegpC4AeQh76baAiBBI/ZlJ0UC 8Hrguurtg45GtHIlt0ZxAOzd7ONPBSkjXjYCwq9jdIPRXDM2JK8An/AErWlAVSLDQd3z0 319C8o9Zix7n8+XS1O3uIBD4zCTMgaL7PlDeHLNkeWkUTDk8FE0RRKO0MH7+sqVCdtWQf Pb5MaMa967WCd2YXtnEqZ1i9Kck78jwnefkCQMQ6JlEOxyU06Jr0sQVOQ+sgaj/XBFg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796203643; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:Received:Received:X-Farcaster-Flow-ID:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy; bh=xZl5Zvb92N/I/pDitlA+r2QT9Af7ukHYccmub0FhwK0=; b=Gr63IlspDS3S9ezd6UGNM+zD0ikzN13sZbY/Q7VcTBk5ikFmj8DiYGKiAEyJh/WQ0MJ7 DkEaQQdWrs8JeaoV3j5SmaxObjJa5iBaQq92l6VlPZbrnqEpPx6Zk/eAhLbk69CbK33BQ ckdSWsy69SfvjyZIBOLtRvtvbFqimXMQezVDjtoqdT3euwcPzkrmYwfCGc7Lv1TToK8Ki 02p1B1HQPxPijY8s02DYzKG2TV3+1hbGsenpfd2Uhwuaw6BtkvSGjO9RIS/WahGKY3aUg BM1GMpO4LYUVXMMZ86wQuhAo4AqX7XFHMxzmjSCQmpqyafNvg/qULet7huN654Y3gtQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=eNALPgl6; arc=none smtp.remote-ip=52.12.53.23 Received: from pdx-out-010.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-010.esa.us-west-2.outbound.mail-perimeter.amazon.com [52.12.53.23]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A6A6606A32D; Wed, 2 Dec 2026 09:27:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3A6A6606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=eNALPgl6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1796203642; x=1827739642; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=xZl5Zvb92N/I/pDitlA+r2QT9Af7ukHYccmub0FhwK0=; b=eNALPgl67DTk34WlZCR40Z64kWAc2cdFvZCn1Bg02KZmEa4DcShPUuil /5ZOrBWp2H4cFYRMJpP4BwFA6fHLQ9Ph3EzcR5NUvBgjcNXbvLarToMyX /Tb6HxyaOhe+Eolc9eKeUzjHoDToOjB9OwKbt5mH0YNKrBZhWtL83HZjM jyrUsxgkMdvmvxtEAb+MSAmQWcwDm3KFUFA/XDqvAUtIzV9Err0yD7+8u AZgjbeqtQAjFnEfb/bUOP1wY63DL9XLaGFgt3N/VfgDO9iT8pzO8CAM+5 tUJ9ch3oJzaPG7x04lAx3pYglr+RZUeyt8mVlvlO8wQCyN2clmi9lZYDl w==; X-CSE-ConnectionGUID: kqcxd+UKQ72+/AKY1m4Eig== X-CSE-MsgGUID: A5aiTQN2Soe6ZxHz+mIINQ== X-IronPort-AV: E=Sophos;i="6.27,246,1787011200"; d="scan'208";a="35524890" Received: from ip-10-5-0-115.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.0.115]) by internal-pdx-out-010.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2026 09:27:19 +0000 Received: from EX19MTAUWA001.ant.amazon.com [205.251.233.182:31739] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.42.230:2525] with esmtp (Farcaster) id ccc66d6e-639e-4367-823d-b6b69c40b35e; Wed, 2 Dec 2026 09:27:19 +0000 (UTC) X-Farcaster-Flow-ID: ccc66d6e-639e-4367-823d-b6b69c40b35e Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWA001.ant.amazon.com (10.250.64.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Wed, 2 Dec 2026 09:27:19 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Wed, 2 Dec 2026 09:27:17 +0000 From: Eliav Farber To: Greg Kroah-Hartman , Jiri Slaby CC: John Ogness , Petr Mladek , Steven Rostedt , Sergey Senozhatsky , , , , Eliav Farber 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 Message-ID: <20261202092712.18975-1-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D031UWA002.ant.amazon.com (10.13.139.96) To EX19D001UWA001.ant.amazon.com (10.13.138.214) 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 Reviewed-by: John Ogness --- 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