Skip to contentSkip to conversation

Convert 8250 to NBCON, take 2

PATCH In review John Ogness 6 participants 30 messages Jul 29 – Aug 27

Subsystems (7)
lore
Share & tools
lore

John Ogness

author maintainer
#0 lore raw

v11 cover Convert 8250 to NBCON, take 2

Hi,

This is v11 of a series to convert the 8250 driver to an NBCON console, providing both threaded and atomic printing implementations. v10 of this series is here [0].

Note that v5 was pulled into the 6.14 merge window, but was then reverted before the 6.14-rc1 release due to kernel test robot problems. It turned out that the problems were related to general 8250 driver issues. Those issues have now been addressed mainline, paving the way for a second take at converting the 8250 driver to NBCON.

The changes since v10:

- Change the main write functions to be helper functions:

serial8250_console_fifo_write() -> __serial8250_console_fifo_write() serial8250_console_byte_write() -> __serial8250_console_byte_write()

These functions take additional char array and size arguments for text to write. This allows the functions to be used for special text. In particular, writing the extra '\n' after a handover/takeover.

- Add new serial8250_console_fifo_write() and serial8250_console_byte_write() functions that are simple wrappers for the respective helpers.

- Add a __serial8250_console_write() function to handle calling the appropriate write functions for the various use_fifo/!use_fifo and console_line_ended/!console_line_ended scenarios. This not only fixes a possible FIFO overrun pointed out by Sashiko for the "use_fifo && !console_line_ended" case, but also also consolidates all the actual writing code into a single helper.

- Toggle unsafe after 2 potentially large delays in the post-write part of serial8250_console_write(). This reduces the likelihood of corruption in panic output by providing more points to recognize ownership transfer.

John Ogness

[0] https://lore.kernel.org/lkml/[email protected]

John Ogness (2):
serial: 8250: Switch to nbcon console, take 2 Revert "serial: 8250: drop lockdep annotation from

    serial8250_clear_IER()"
 drivers/tty/serial/8250/8250.h      |   4 +-
 drivers/tty/serial/8250/8250_core.c |  65 ++++++--
 drivers/tty/serial/8250/8250_dw.c   |   2 +-
 drivers/tty/serial/8250/8250_port.c | 245 +++++++++++++++++++++++-----
 include/linux/serial_8250.h         |  16 +-
 5 files changed, 277 insertions(+), 55 deletions(-)

base-commit: a5339effdd523b4b3502fba5358d44e83c76a565

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v11 1/2 serial: 8250: Switch to nbcon console, take 2

Implement the necessary callbacks to switch the 8250 console driver to perform as an nbcon console.

Add implementations for the nbcon console callbacks:

->write_atomic()
->write_thread()
->device_lock()
->device_unlock()

and add CON_NBCON to the initial @flags.

All hardware access in the callbacks is within unsafe sections. The ->write_atomic() and ->write_thread() callbacks allow safe handover/takeover per byte and add a preceding newline if they take over from another context mid-line.

For the ->write_atomic() callback, a new irq_work is used to defer modem control since it may be called from a context that does not allow waking up tasks. During suspend/resume the irq_work is not used as this has been shown to cause suspend problems for some hardware. Upon resume, any pending modem control is performed.

Note: A new __serial8250_clear_IER() is introduced for direct clearing of UART_IER during console writing (which will not be holding the port lock for atomic printing or KDB/KGDB). This allows restoring a lockdep check to serial8250_clear_IER() in a follow-up commit.

---
 drivers/tty/serial/8250/8250.h      |   4 +-
 drivers/tty/serial/8250/8250_core.c |  65 ++++++--
 drivers/tty/serial/8250/8250_dw.c   |   2 +-
 drivers/tty/serial/8250/8250_port.c | 242 +++++++++++++++++++++++-----
 include/linux/serial_8250.h         |  16 +-
 5 files changed, 274 insertions(+), 55 deletions(-)
drivers/tty/serial/8250/8250.h +3 -1
index b62f88eec881f..d672b014ade02 100644
@@ -178,7 +178,9 @@ static unsigned int __maybe_unused serial_icr_read(struct uart_8250_port *up,
 
 void serial8250_clear_fifos(struct uart_8250_port *p);
 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
-void serial8250_fifo_wait_for_lsr_thre(struct uart_8250_port *up, unsigned int count);
+void serial8250_fifo_wait_for_lsr_thre(struct uart_8250_port *up,
+				       struct nbcon_write_context *wctxt,
+				       unsigned int count);
 
 void serial8250_rpm_get(struct uart_8250_port *p);
 void serial8250_rpm_put(struct uart_8250_port *p);
drivers/tty/serial/8250/8250_core.c +54 -11

97 lines not on this page · raw message

drivers/tty/serial/8250/8250_dw.c +1 -1
index 5fba913f33010..51d026f20825a 100644
@@ -156,7 +156,7 @@ static int dw8250_idle_enter(struct uart_port *p)
 	 *
 	 * FIXME: frame_time delay is too long with very low baudrates.
 	 */
-	serial8250_fifo_wait_for_lsr_thre(up, p->fifosize);
+	serial8250_fifo_wait_for_lsr_thre(up, NULL, p->fifosize);
 	ndelay(p->frame_time);
 
 	serial_port_out(p, UART_MCR, up->mcr | UART_MCR_LOOP);
drivers/tty/serial/8250/8250_port.c +202 -40

373 lines not on this page · raw message

include/linux/serial_8250.h +14 -2
index a95b2d143d248..eba36710bc476 100644
@@ -150,8 +150,20 @@ struct uart_8250_port {
 #define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
 	u16			lsr_saved_flags;
 	u16			lsr_save_mask;
+
+	/*
+	 * Track when a console line has been fully written to the
+	 * hardware, i.e. true when the most recent byte written to
+	 * UART_TX by the console was '\n'.
+	 */
+	bool			console_line_ended;
+
+	/* Allow queuing irq_work for MSR handling */
+	bool			console_msr_work_allow;
+
 #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
 	unsigned char		msr_saved_flags;
+	struct irq_work		console_msr_work;
 
 	struct uart_8250_dma	*dma;
 	const struct uart_8250_ops *ops;
@@ -203,8 +215,8 @@ void serial8250_tx_chars(struct uart_8250_port *up);
 unsigned int serial8250_modem_status(struct uart_8250_port *up);
 void serial8250_init_port(struct uart_8250_port *up);
 void serial8250_set_defaults(struct uart_8250_port *up);
-void serial8250_console_write(struct uart_8250_port *up, const char *s,
-			      unsigned int count);
+void serial8250_console_write(struct uart_8250_port *up,
+			      struct nbcon_write_context *wctxt, bool in_atomic);
 int serial8250_console_setup(struct uart_port *port, char *options, bool probe);
 int serial8250_console_exit(struct uart_port *port);
 
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi,

Sashiko did not find anything legitimate with this version. The two issues are incorrect (I explain why below).

If anyone has been waiting for Sashiko to "finish", this is the version that is ready for human review.

John

Quoted 40 lines · Sashiko
Quoted 5 lines
drivers/tty/serial/8250/8250_port.c +0 -0
index 8c241ec7f4f29..b9ea4f898474e 100644
@@ -3286,39 +3339,92 @@ static void serial8250_console_fifo_write(struct uart_8250_port *up,

[ ... ]

Quoted 34 lines

/*
- * Print a string to the serial port trying not to disturb
- * any possible real use of the port...
- *
- * The console_lock must be held when we get here.
- *
- * Doing runtime PM is really a bad idea for the kernel console.
- * Thus, we assume the function is called when device is powered up.
+ * Print a string to the serial port trying not to disturb
+ * any possible real use of the port...
*/

drivers/tty/serial/8250/8250_port.c +7 -11
-void serial8250_console_write(struct uart_8250_port *up, const char *s,
-			      unsigned int count)
+void serial8250_console_write(struct uart_8250_port *up,
+			      struct nbcon_write_context *wctxt,
+			      bool is_atomic)
 {
 	struct uart_8250_em485 *em485 = up->em485;
 	struct uart_port *port = &up->port;
-	unsigned long flags;
-	unsigned int ier, use_fifo;
-	int locked = 1;
-
-	touch_nmi_watchdog();
+	unsigned int ier;
+	bool use_fifo;
 
-	if (oops_in_progress)
-		locked = uart_port_trylock_irqsave(port, &flags);
-	else
-		uart_port_lock_irqsave(port, &flags);
+	if (!nbcon_enter_unsafe(wctxt))
+		return;
Quoted 13 lines

drivers/tty/serial/8250/8250_port.c static void serial8250_console_fifo_write(struct uart_8250_port *up, · line 3427

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +5 -2
 	/*
-	 *	First save the IER then disable the interrupts
+	 * First, save the IER, then disable the interrupts. The special
+	 * variant to clear the IER is used because emergency and panic
+	 * printing is synchronized only by nbcon ownership without
+	 * holding the port lock.
 	 */
 	ier = serial_port_in(port, UART_IER);
-	serial8250_clear_IER(up);
+	__serial8250_clear_IER(up);

Will this race with normal driver paths since the port lock is no longer acquired for atomic printing?

No. This is synchronized by nbcon ownership, which normal driver paths also acquire via the port->lock locking wrappers. It is even within an unsafe section.

Quoted 14 lines
Quoted 14 lines
drivers/tty/serial/8250/8250_port.c +8 -2
@@ -3378,11 +3502,25 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
 	 *	call it if we have saved something in the saved flags
 	 *	while processing with interrupts off.
 	 */
-	if (up->msr_saved_flags)
-		serial8250_modem_status(up);
+	if (up->msr_saved_flags) {
+		if (is_atomic) {
+			/*
+			 * For atomic, MSR handling must be deferred to
+			 * irq_work because this may be a context that does
+			 * not permit waking up tasks.
+			 *
+			 * But no irq_work may be queued when suspending.
Quoted 8 lines

drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3517

Quoted 5 lines
drivers/tty/serial/8250/8250_port.c +5 -0
+			 * In that case, the MSR handling will occur during
+			 * resume in serial8250_resume_port().
+			 */
+			if (up->console_msr_work_allow)
+				irq_work_queue(&up->console_msr_work);

Is there a race condition here between checking the flag and queuing the work?

Ditto.

John

maintainer
#3 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

In reply to John Ogness
Quoted 453 lines · John Ogness

Implement the necessary callbacks to switch the 8250 console driver to perform as an nbcon console.

Add implementations for the nbcon console callbacks:

->write_atomic()
->write_thread()
->device_lock()
->device_unlock()

and add CON_NBCON to the initial @flags.

All hardware access in the callbacks is within unsafe sections. The ->write_atomic() and ->write_thread() callbacks allow safe handover/takeover per byte and add a preceding newline if they take over from another context mid-line.

For the ->write_atomic() callback, a new irq_work is used to defer modem control since it may be called from a context that does not allow waking up tasks. During suspend/resume the irq_work is not used as this has been shown to cause suspend problems for some hardware. Upon resume, any pending modem control is performed.

Note: A new __serial8250_clear_IER() is introduced for direct clearing of UART_IER during console writing (which will not be holding the port lock for atomic printing or KDB/KGDB). This allows restoring a lockdep check to serial8250_clear_IER() in a follow-up commit.

---
 drivers/tty/serial/8250/8250.h      |   4 +-
 drivers/tty/serial/8250/8250_core.c |  65 ++++++--
 drivers/tty/serial/8250/8250_dw.c   |   2 +-
 drivers/tty/serial/8250/8250_port.c | 242 +++++++++++++++++++++++-----
 include/linux/serial_8250.h         |  16 +-
 5 files changed, 274 insertions(+), 55 deletions(-)
drivers/tty/serial/8250/8250.h +3 -1
index b62f88eec881f..d672b014ade02 100644
@@ -178,7 +178,9 @@ static unsigned int __maybe_unused serial_icr_read(struct uart_8250_port *up,
 
 void serial8250_clear_fifos(struct uart_8250_port *p);
 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
-void serial8250_fifo_wait_for_lsr_thre(struct uart_8250_port *up, unsigned int count);
+void serial8250_fifo_wait_for_lsr_thre(struct uart_8250_port *up,
+				       struct nbcon_write_context *wctxt,
+				       unsigned int count);
 
 void serial8250_rpm_get(struct uart_8250_port *p);
 void serial8250_rpm_put(struct uart_8250_port *p);
drivers/tty/serial/8250/8250_core.c +54 -11
index c0e8a4efbdcc8..5042a8e8c6fb1 100644
@@ -390,12 +390,34 @@ void __init serial8250_register_ports(struct uart_driver *drv, struct device *de
 
 #ifdef CONFIG_SERIAL_8250_CONSOLE
 
-static void univ8250_console_write(struct console *co, const char *s,
-				   unsigned int count)
+static void univ8250_console_write_atomic(struct console *co,
+					  struct nbcon_write_context *wctxt)
 {
 	struct uart_8250_port *up = &serial8250_ports[co->index];
 
-	serial8250_console_write(up, s, count);
+	serial8250_console_write(up, wctxt, true);
+}
+
+static void univ8250_console_write_thread(struct console *co,
+					  struct nbcon_write_context *wctxt)
+{
+	struct uart_8250_port *up = &serial8250_ports[co->index];
+
+	serial8250_console_write(up, wctxt, false);
+}
+
+static void univ8250_console_device_lock(struct console *co, unsigned long *flags)
+{
+	struct uart_port *up = &serial8250_ports[co->index].port;
+
+	__uart_port_lock_irqsave(up, flags);
+}
+
+static void univ8250_console_device_unlock(struct console *co, unsigned long flags)
+{
+	struct uart_port *up = &serial8250_ports[co->index].port;
+
+	__uart_port_unlock_irqrestore(up, flags);
 }
 
 static int univ8250_console_setup(struct console *co, char *options)
@@ -496,12 +518,15 @@ static int univ8250_console_match(struct console *co, char *name, int idx,
 
 static struct console univ8250_console = {
 	.name		= "ttyS",
-	.write		= univ8250_console_write,
+	.write_atomic	= univ8250_console_write_atomic,
+	.write_thread	= univ8250_console_write_thread,
+	.device_lock	= univ8250_console_device_lock,
+	.device_unlock	= univ8250_console_device_unlock,
 	.device		= uart_console_device,
 	.setup		= univ8250_console_setup,
 	.exit		= univ8250_console_exit,
 	.match		= univ8250_console_match,
-	.flags		= CON_PRINTBUFFER | CON_ANYTIME,
+	.flags		= CON_PRINTBUFFER | CON_ANYTIME | CON_NBCON,
 	.index		= -1,
 	.data		= &serial8250_reg,
 };
@@ -584,13 +609,19 @@ void serial8250_suspend_port(int line)
 	struct uart_8250_port *up = &serial8250_ports[line];
 	struct uart_port *port = &up->port;
 
-	if (!console_suspend_enabled && uart_console(port) &&
-	    port->type != PORT_8250) {
-		unsigned char canary = 0xa5;
+	if (uart_console(port)) {
+		/* No irq_work may be queued when suspending */
+		scoped_guard(uart_port_lock_irq, port)
+			up->console_msr_work_allow = false;
+		irq_work_sync(&up->console_msr_work);
 
-		serial_out(up, UART_SCR, canary);
-		if (serial_in(up, UART_SCR) == canary)
-			up->canary = canary;
+		if (!console_suspend_enabled && port->type != PORT_8250) {
+			unsigned char canary = 0xa5;
+
+			serial_out(up, UART_SCR, canary);
+			if (serial_in(up, UART_SCR) == canary)
+				up->canary = canary;
+		}
 	}
 
 	uart_suspend_port(&serial8250_reg, port);
@@ -620,6 +651,18 @@ void serial8250_resume_port(int line)
 		port->uartclk = 921600*16;
 	}
 	uart_resume_port(&serial8250_reg, port);
+
+	if (uart_console(port)) {
+
+		guard(uart_port_lock_irq)(port);
+
+		/* irq_work allowed again */
+		up->console_msr_work_allow = true;
+
+		/* Handle any pending MSR changes */
+		if (up->msr_saved_flags)
+			serial8250_modem_status(up);
+	}
 }
 EXPORT_SYMBOL(serial8250_resume_port);
 
drivers/tty/serial/8250/8250_dw.c +1 -1
index 5fba913f33010..51d026f20825a 100644
@@ -156,7 +156,7 @@ static int dw8250_idle_enter(struct uart_port *p)
 	 *
 	 * FIXME: frame_time delay is too long with very low baudrates.
 	 */
-	serial8250_fifo_wait_for_lsr_thre(up, p->fifosize);
+	serial8250_fifo_wait_for_lsr_thre(up, NULL, p->fifosize);
 	ndelay(p->frame_time);
 
 	serial_port_out(p, UART_MCR, up->mcr | UART_MCR_LOOP);
drivers/tty/serial/8250/8250_port.c +139 -36
index 8c241ec7f4f29..b9ea4f898474e 100644
@@ -704,7 +704,12 @@ static void serial8250_clear_interrupts(struct uart_port *port)
 	serial_port_in(port, UART_MSR);
 }
 
-static void serial8250_clear_IER(struct uart_8250_port *up)
+/*
+ * Only to be directly used by serial8250_console_write() and
+ * serial8250_put_poll_char(), which do not require the port lock.
+ * Use serial8250_clear_IER() instead for all other cases.
+ */
+static void __serial8250_clear_IER(struct uart_8250_port *up)
 {
 	if (up->capabilities & UART_CAP_UUE)
 		serial_out(up, UART_IER, UART_IER_UUE);
@@ -712,6 +717,11 @@ static void serial8250_clear_IER(struct uart_8250_port *up)
 		serial_out(up, UART_IER, 0);
 }
 
+static inline void serial8250_clear_IER(struct uart_8250_port *up)
+{
+	__serial8250_clear_IER(up);
+}
+
 /*
  * This is a quickie test to see how big the FIFO is.
  * It doesn't work at all the time, more's the pity.
@@ -1284,9 +1294,6 @@ void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier)
 {
 	unsigned char mcr = serial8250_in_MCR(p);
 
-	/* Port locked to synchronize UART_IER access against the console. */
-	lockdep_assert_held_once(&p->port.lock);
-
 	if (p->port.rs485.flags & SER_RS485_RTS_AFTER_SEND)
 		mcr |= UART_MCR_RTS;
 	else
@@ -1302,6 +1309,16 @@ void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier)
 		serial8250_clear_and_reinit_fifos(p);
 
 		if (toggle_ier) {
+			/*
+			 * Port locked to synchronize UART_IER access against
+			 * the console. The lockdep_assert must be restricted
+			 * to this condition because only here is it
+			 * guaranteed that the port lock is held. The other
+			 * hardware access in this function is synchronized
+			 * by console ownership.
+			 */
+			lockdep_assert_held_once(&p->port.lock);
+
 			p->ier |= UART_IER_RLSI | UART_IER_RDI;
 			serial_port_out(&p->port, UART_IER, p->ier);
 		}
@@ -2053,10 +2070,13 @@ static void serial8250_put_poll_char(struct uart_port *port,
 
 	guard(serial8250_rpm)(up);
 	/*
-	 *	First save the IER then disable the interrupts
+	 * First, save the IER, then disable the interrupts. The special
+	 * variant to clear the IER is used because KDB/KGDB printing
+	 * is synchronized via CPU quiescence without holding the port
+	 * lock.
 	 */
 	ier = serial_port_in(port, UART_IER);
-	serial8250_clear_IER(up);
+	__serial8250_clear_IER(up);
 
 	wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);
 	/*
@@ -3198,13 +3218,32 @@ void serial8250_set_defaults(struct uart_8250_port *up)
 }
 EXPORT_SYMBOL_GPL(serial8250_set_defaults);
 
-void serial8250_fifo_wait_for_lsr_thre(struct uart_8250_port *up, unsigned int count)
+void serial8250_fifo_wait_for_lsr_thre(struct uart_8250_port *up,
+				       struct nbcon_write_context *wctxt,
+				       unsigned int count)
 {
 	unsigned int i;
 
+	/*
+	 * For console writing, enter/exit an unsafe section for each byte
+	 * in order to pass the ownership as quickly as possible if a higher
+	 * priority context wants ownership. Otherwise, an attempt to take
+	 * over the ownership might timeout. The new owner will wait for
+	 * UART_LSR_THRE before reusing the fifo.
+	 */
 	for (i = 0; i < count; i++) {
-		if (wait_for_lsr(up, UART_LSR_THRE))
+		bool tx_ready;
+
+		if (wctxt && !nbcon_enter_unsafe(wctxt))
 			return;
+
+		tx_ready = wait_for_lsr(up, UART_LSR_THRE);
+
+		if (wctxt)
+			nbcon_exit_unsafe(wctxt);
+
+		if (tx_ready)
+			break;
 	}
 }
 EXPORT_SYMBOL_NS_GPL(serial8250_fifo_wait_for_lsr_thre, "SERIAL_8250");
@@ -3213,7 +3252,11 @@ EXPORT_SYMBOL_NS_GPL(serial8250_fifo_wait_for_lsr_thre, "SERIAL_8250");
 
 static void serial8250_console_putchar(struct uart_port *port, unsigned char ch)
 {
+	struct uart_8250_port *up = up_to_u8250p(port);
+
 	serial_port_out(port, UART_TX, ch);
+
+	up->console_line_ended = (ch == '\n');
 }
 
 static void serial8250_console_wait_putchar(struct uart_port *port, unsigned char ch)
@@ -3256,8 +3299,9 @@ static void serial8250_console_restore(struct uart_8250_port *up)
  * It sends fifosize bytes and then waits for the fifo
  * to get empty.
  */
-static void serial8250_console_fifo_write(struct uart_8250_port *up,
-					  const char *s, unsigned int count)
+static void __serial8250_console_fifo_write(struct uart_8250_port *up,
+					    struct nbcon_write_context *wctxt,
+					    const char *s, unsigned int count)
 {
 	const char *end = s + count;
 	unsigned int fifosize = up->tx_loadsz;
@@ -3268,9 +3312,16 @@ static void serial8250_console_fifo_write(struct uart_8250_port *up,
 
 	while (s != end) {
 		/* Allow timeout for each byte of a possibly full FIFO */
-		serial8250_fifo_wait_for_lsr_thre(up, fifosize);
+		serial8250_fifo_wait_for_lsr_thre(up, wctxt, fifosize);
 
+		/*
+		 * Fill the FIFO. If a handover or takeover occurs, writing
+		 * must be aborted since the string data is no longer valid.
+		 */
 		for (i = 0; i < fifosize && s != end; ++i) {
+			if (!nbcon_enter_unsafe(wctxt))
+				return;
+
 			if (*s == '\n' && !cr_sent) {
 				serial8250_console_putchar(port, '\r');
 				cr_sent = true;
@@ -3278,6 +3329,8 @@ static void serial8250_console_fifo_write(struct uart_8250_port *up,
 				serial8250_console_putchar(port, *s++);
 				cr_sent = false;
 			}
+
+			nbcon_exit_unsafe(wctxt);
 		}
 		tx_count = i;
 	}
@@ -3286,39 +3339,92 @@ static void serial8250_console_fifo_write(struct uart_8250_port *up,
 	 * Allow timeout for each byte written since the caller will only wait
 	 * for UART_LSR_BOTH_EMPTY using the timeout of a single character
 	 */
-	serial8250_fifo_wait_for_lsr_thre(up, tx_count);
+	serial8250_fifo_wait_for_lsr_thre(up, wctxt, tx_count);
+}
+
+static void serial8250_console_fifo_write(struct uart_8250_port *up,
+					  struct nbcon_write_context *wctxt)
+{
+	__serial8250_console_fifo_write(up, wctxt, wctxt->outbuf, wctxt->len);
+}
+
+static void __serial8250_console_byte_write(struct uart_8250_port *up,
+					    struct nbcon_write_context *wctxt,
+					    const char *s, unsigned int count)
+{
+	struct uart_port *port = &up->port;
+	const char *end = s + count;
+
+	/*
+	 * Write out the message. If a handover or takeover occurs, writing
+	 * must be aborted since the string data is no longer valid.
+	 */
+	while (s != end) {
+		if (!nbcon_enter_unsafe(wctxt))
+			return;
+
+		uart_console_write(port, s++, 1, serial8250_console_wait_putchar);
+
+		nbcon_exit_unsafe(wctxt);
+	}
+}
+
+static void serial8250_console_byte_write(struct uart_8250_port *up,
+					  struct nbcon_write_context *wctxt)
+{
+	__serial8250_console_byte_write(up, wctxt, wctxt->outbuf, wctxt->len);
+}
+
+/*
+ * Print the console line using the appropriate variant. If ownership is lost
+ * at any time during printing, the printing is aborted.
+ */
+static void __serial8250_console_write(struct uart_8250_port *up,
+				       struct nbcon_write_context *wctxt,
+				       bool use_fifo)
+{
+	/*
+	 * If the console printer did not fully output the previous line, it
+	 * must have been handed or taken over. Insert a newline in order to
+	 * maintain clean output.
+	 */
+	if (!up->console_line_ended) {
+		if (use_fifo)
+			__serial8250_console_fifo_write(up, wctxt, "\n", 1);
+		else
+			__serial8250_console_byte_write(up, wctxt, "\n", 1);
+	}
+
+	if (use_fifo)
+		serial8250_console_fifo_write(up, wctxt);
+	else
+		serial8250_console_byte_write(up, wctxt);
 }
 
 /*
- *	Print a string to the serial port trying not to disturb
- *	any possible real use of the port...
- *
- *	The console_lock must be held when we get here.
- *
- *	Doing runtime PM is really a bad idea for the kernel console.
- *	Thus, we assume the function is called when device is powered up.
+ * Print a string to the serial port trying not to disturb
+ * any possible real use of the port...
  */
-void serial8250_console_write(struct uart_8250_port *up, const char *s,
-			      unsigned int count)
+void serial8250_console_write(struct uart_8250_port *up,
+			      struct nbcon_write_context *wctxt,
+			      bool is_atomic)
 {
 	struct uart_8250_em485 *em485 = up->em485;
 	struct uart_port *port = &up->port;
-	unsigned long flags;
-	unsigned int ier, use_fifo;
-	int locked = 1;
-
-	touch_nmi_watchdog();
+	unsigned int ier;
+	bool use_fifo;
 
-	if (oops_in_progress)
-		locked = uart_port_trylock_irqsave(port, &flags);
-	else
-		uart_port_lock_irqsave(port, &flags);
+	if (!nbcon_enter_unsafe(wctxt))
+		return;
 
 	/*
-	 *	First save the IER then disable the interrupts
+	 * First, save the IER, then disable the interrupts. The special
+	 * variant to clear the IER is used because emergency and panic
+	 * printing is synchronized only by nbcon ownership without
+	 * holding the port lock.
 	 */
 	ier = serial_port_in(port, UART_IER);
-	serial8250_clear_IER(up);
+	__serial8250_clear_IER(up);
 
 	/* check scratch reg to see if port powered off during system sleep */
 	if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
@@ -3352,10 +3458,17 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
 		 */
 		!uart_console_hwflow_active(&up->port);
 
-	if (likely(use_fifo))
-		serial8250_console_fifo_write(up, s, count);
-	else
-		uart_console_write(port, s, count, serial8250_console_wait_putchar);
+	nbcon_exit_unsafe(wctxt);

drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3471

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +10 -0
+
+	__serial8250_console_write(up, wctxt, use_fifo);
+
+	/*
+	 * Re-enter an unsafe section in order to perform final actions
+	 * (such as re-enabling interrupts). If ownership was lost, this
+	 * context must reacquire ownership.
+	 */
+	while (!nbcon_enter_unsafe(wctxt))
+		nbcon_reacquire_nobuf(wctxt);

If we are going to use this on more locations than I would add a new API, e.g.

/**
* nbcon_enter_unsafe_reacquire - Enter an unsafe region in the driver,
* reacquire when needed.
* @wctxt: The write context that was handed to the write function
*
*
* The function is allowed to reacquire the console ownership to make
* sure that the caller can enter an unsafe region in the driver. *
* Warning: The caller must not longer access the text buffer. It is
* lost when the reacquire was needed. The function is intended
* for cleanup operations after emitting the message, for example,
* re-enabling interrupts on a used serial port.
*/
void nbcon_enter_unsafe_reacquire(struct nbcon_write_context *wctxt)

{
	while (!nbcon_enter_unsafe(wctxt))
		nbcon_reacquire_nobuf(wctxt);
}

drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3485

Quoted 12 lines
drivers/tty/serial/8250/8250_port.c +5 -0
 	/*
 	 *	Finally, wait for transmitter to become empty
@@ -3365,10 +3478,21 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
 
 	if (em485) {
 		mdelay(port->rs485.delay_rts_after_send);
+
+		/* Toggle unsafe after possibly long delay */
+		nbcon_exit_unsafe(wctxt);
+		while (!nbcon_enter_unsafe(wctxt))
+			nbcon_reacquire_nobuf(wctxt);

The comment describes what the code does but it does not explain why.

My first idea was that it would allow to takeover the ownership as soon as possible in an emergency context.

But the motivation seems to be to reduce the race with a possible unsafe takeover in the final panic flush, see
https://lore.kernel.org/all/[email protected]/

It opens some questions:

First, is it really necessary to finish the clean up when this CPU is supposed to be stopped and the panic CPU is about to enter the infinite loop?

Second, it is actually safe in the emergency context? For example, the mdelay() is there for a reason. Maybe, we should repeat it when the ownership has been lost here?

Third, is it necessary to call the mdelay() in the unsafe context?

OK, if it is safe in emergency then it does not harm in panic. And it might be safe in emergency. We could assume that the nested owner did the delay as well.

See below.

Quoted 29 lines
drivers/tty/serial/8250/8250_port.c +16 -2
 		if (em485->tx_stopped)
 			up->rs485_stop_tx(up, false);
 	}
 
+	/* Toggle unsafe after possibly long delay */
+	nbcon_exit_unsafe(wctxt);
+	while (!nbcon_enter_unsafe(wctxt))
+		nbcon_reacquire_nobuf(wctxt);
 	serial_port_out(port, UART_IER, ier);
 
 	/*
@@ -3378,11 +3502,25 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
 	 *	call it if we have saved something in the saved flags
 	 *	while processing with interrupts off.
 	 */
-	if (up->msr_saved_flags)
-		serial8250_modem_status(up);
+	if (up->msr_saved_flags) {
+		if (is_atomic) {
+			/*
+			 * For atomic, MSR handling must be deferred to
+			 * irq_work because this may be a context that does
+			 * not permit waking up tasks.
+			 *
+			 * But no irq_work may be queued when suspending.
+			 * In that case, the MSR handling will occur during
+			 * resume in serial8250_resume_port().
+			 */
+			if (up->console_msr_work_allow)

drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3524

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +6 -2
+				irq_work_queue(&up->console_msr_work);
+		} else {
+			serial8250_modem_status(up);
+		}
+	}
 
-	if (locked)
-		uart_port_unlock_irqrestore(port, flags);
+	nbcon_exit_unsafe(wctxt);
 }

I think about entering unsafe only when really needed. Instead of releasing it and taking again immediately.

Something like:

	/*
	 * Re-enter an unsafe section in order to perform final actions
	 * (such as re-enabling interrupts). If ownership was lost, this
	 * context must reacquire ownership.
	 */
	nbcon_enter_unsafe_reacquire(wctxt);

	/*
	 *	Finally, wait for transmitter to become empty
	 *	and restore the IER
	 */
	wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);

	/*
	 * Exit unsafe section after a potentially long wait. It allows
	 * a safe takeover before another potentially long way. Also
	 * it reduces the race window when a possible unsafe takeover
	 * happened unnoticed.
	 */
	nbcon_exit_unsafe()

	if (em485) {
		u32 delay;

		nbcon_enter_unsafe_reacquire(wctxt);
		delay = port->rs485.delay_rts_after_send;
		nbcon_exit_unsafe(wctxt);

		mdelay(port->rs485.delay_rts_after_send);

		nbcon_enter_unsafe_reacquire(wctxt);
		if (em485->tx_stopped)
			up->rs485_stop_tx(up, false);
		nbcon_exit_unsafe(wctxt);
	}

	nbcon_enter_unsafe_reacquire(wctxt);

	serial_port_out(port, UART_IER, ier);

	/*
	 *	The receive handling will happen properly because the
	 *	receive ready bit will still be set; it is not cleared
	 *	on read.  However, modem control will not, we must
	 *	call it if we have saved something in the saved flags
	 *	while processing with interrupts off.
	 */
	if (up->msr_saved_flags) {
		if (is_atomic) {
			/*
			 * For atomic, MSR handling must be deferred to
			 * irq_work because this may be a context that does
			 * not permit waking up tasks.
			 *
			 * But no irq_work may be queued when suspending.
			 * In that case, the MSR handling will occur during
			 * resume in serial8250_resume_port().
			 */
			if (up->console_msr_work_allow)
				irq_work_queue(&up->console_msr_work);
		} else {
			serial8250_modem_status(up);
		}
	}

	nbcon_exit_unsafe(wctxt);
}

But I have to say that both approaches are quite hairy. I am not sure if it is worth it.

drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3526

Quoted 1 line
drivers/tty/serial/8250/8250_port.c +0 -0
 static unsigned int probe_baud(struct uart_port *port)

Otherwise, I do not see any real problems in the code.

Best Regards,
Petr

author maintainer
#4 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 18 lines · Petr Mladek
Quoted 18 lines
drivers/tty/serial/8250/8250_port.c +6 -4
index 8c241ec7f4f29..b9ea4f898474e 100644
@@ -3352,10 +3458,17 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
 		 */
 		!uart_console_hwflow_active(&up->port);
 
-	if (likely(use_fifo))
-		serial8250_console_fifo_write(up, s, count);
-	else
-		uart_console_write(port, s, count, serial8250_console_wait_putchar);
+	nbcon_exit_unsafe(wctxt);
+
+	__serial8250_console_write(up, wctxt, use_fifo);
+
+	/*
+	 * Re-enter an unsafe section in order to perform final actions
Quoted 8 lines

drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3471

Quoted 5 lines
drivers/tty/serial/8250/8250_port.c +5 -0
+	 * (such as re-enabling interrupts). If ownership was lost, this
+	 * context must reacquire ownership.
+	 */
+	while (!nbcon_enter_unsafe(wctxt))
+		nbcon_reacquire_nobuf(wctxt);

If we are going to use this on more locations than I would add a new API, e.g.

Agreed.

The 8250 has already played a significant role in influencing other NBCON implementations. I expect there will be a lot of copy/paste in the beginning. Once we start seeing which code is copied and how it is used in other drivers, we should be be able to do some general nbcon and serial_core consolidation.

Quoted 15 lines

/**
* nbcon_enter_unsafe_reacquire - Enter an unsafe region in the driver,
* reacquire when needed.
* @wctxt: The write context that was handed to the write function
*
*
* The function is allowed to reacquire the console ownership to make
* sure that the caller can enter an unsafe region in the driver. *
* Warning: The caller must not longer access the text buffer. It is
* lost when the reacquire was needed. The function is intended
* for cleanup operations after emitting the message, for example,
* re-enabling interrupts on a used serial port.
*/
void nbcon_enter_unsafe_reacquire(struct nbcon_write_context *wctxt)

Quoted 4 lines
{
	while (!nbcon_enter_unsafe(wctxt))
		nbcon_reacquire_nobuf(wctxt);
}

I would name the function:

nbcon_enter_unsafe_reacquire_nobuf()

to make it clear that the buffer will be lost on a reacquire.

Quoted 11 lines

drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3485

Quoted 9 lines
drivers/tty/serial/8250/8250_port.c +5 -0
@@ -3365,10 +3478,21 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
 
 	if (em485) {
 		mdelay(port->rs485.delay_rts_after_send);
+
+		/* Toggle unsafe after possibly long delay */
+		nbcon_exit_unsafe(wctxt);
+		while (!nbcon_enter_unsafe(wctxt))
+			nbcon_reacquire_nobuf(wctxt);

The comment describes what the code does but it does not explain why.

Well, the _why_ is the same for all the toggling that we do throughout the console code.

Quoted 2 lines

My first idea was that it would allow to takeover the ownership as soon as possible in an emergency context.

Yes. That is one of the purposes. Even takeovers in panic will patiently wait up to 2ms before becoming hostile.

Quoted 3 lines

But the motivation seems to be to reduce the race with a possible unsafe takeover in the final panic flush, see
https://lore.kernel.org/all/[email protected]/

Yes, this is also valid, but for all other toggle situations as well. The driver should avoid touching the hardware if it is no longer the owner.

Quoted 5 lines

It opens some questions:

First, is it really necessary to finish the clean up when this CPU is supposed to be stopped and the panic CPU is about to enter the infinite loop?

Why even ask that question? The driver should try to do things correctly. We want to avoid driver code like:

	if (panic_on_other_cpu())
		return;

The driver should not care about panic and instead focus on following nbcon ownership rules.

Quoted 3 lines

Second, it is actually safe in the emergency context? For example, the mdelay() is there for a reason. Maybe, we should repeat it when the ownership has been lost here?

If ownership has been lost, the new owner will have performed the mdelay() and rs485_stop_tx() if needed. One could argue that if ownership was lost, it should _not_ call ->rs485_stop_tx() at all. But the new owner will also make an unnecessary call to ->rs485_start_tx().

If making these calls unnecessarily is a problem, the driver will need to detect the situation and add this condition.

I have added "RS485-handovers" to my list of things to do some stress testing.

Quoted 1 line

Third, is it necessary to call the mdelay() in the unsafe context?

No. It is not necessary. Worst case it will cause the printing thread to handle a WARN that could have been printed atomically. But still, we should avoid that.

Quoted 24 lines

I think about entering unsafe only when really needed. Instead of releasing it and taking again immediately.

Something like:

	/*
	 * Re-enter an unsafe section in order to perform final actions
	 * (such as re-enabling interrupts). If ownership was lost, this
	 * context must reacquire ownership.
	 */
	nbcon_enter_unsafe_reacquire(wctxt);

	/*
	 *	Finally, wait for transmitter to become empty
	 *	and restore the IER
	 */
	wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);

	/*
	 * Exit unsafe section after a potentially long wait. It allows
	 * a safe takeover before another potentially long way. Also
	 * it reduces the race window when a possible unsafe takeover
	 * happened unnoticed.
	 */
Quoted 8 lines
	nbcon_exit_unsafe()

	if (em485) {
		u32 delay;

		nbcon_enter_unsafe_reacquire(wctxt);
		delay = port->rs485.delay_rts_after_send;
		nbcon_exit_unsafe(wctxt);

This unsafe enter/exit is unnecessary. This value does not change.

Quoted 40 lines
		mdelay(port->rs485.delay_rts_after_send);

		nbcon_enter_unsafe_reacquire(wctxt);
		if (em485->tx_stopped)
			up->rs485_stop_tx(up, false);
		nbcon_exit_unsafe(wctxt);
	}

	nbcon_enter_unsafe_reacquire(wctxt);

	serial_port_out(port, UART_IER, ier);

	/*
	 *	The receive handling will happen properly because the
	 *	receive ready bit will still be set; it is not cleared
	 *	on read.  However, modem control will not, we must
	 *	call it if we have saved something in the saved flags
	 *	while processing with interrupts off.
	 */
	if (up->msr_saved_flags) {
		if (is_atomic) {
			/*
			 * For atomic, MSR handling must be deferred to
			 * irq_work because this may be a context that does
			 * not permit waking up tasks.
			 *
			 * But no irq_work may be queued when suspending.
			 * In that case, the MSR handling will occur during
			 * resume in serial8250_resume_port().
			 */
			if (up->console_msr_work_allow)
				irq_work_queue(&up->console_msr_work);
		} else {
			serial8250_modem_status(up);
		}
	}

	nbcon_exit_unsafe(wctxt);
}
Quoted 2 lines

But I have to say that both approaches are quite hairy. I am not sure if it is worth it.

I would just move the unsafe_exit before the mdelay(). I think that is sufficient here.

Quoted 1 line

Otherwise, I do not see any real problems in the code.

Thanks Petr for taking a detailed look at this!

I think v11 is OK for mainline (Greg has it in tty-testing now.) If anything else comes up and I need to touch the code again, I will relocate the unsafe_exit to before the mdelay().

John

maintainer
#5 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 28 lines · John Ogness
Quoted 18 lines · Petr Mladek
Quoted 18 lines
drivers/tty/serial/8250/8250_port.c +6 -4
index 8c241ec7f4f29..b9ea4f898474e 100644
@@ -3352,10 +3458,17 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
 		 */
 		!uart_console_hwflow_active(&up->port);
 
-	if (likely(use_fifo))
-		serial8250_console_fifo_write(up, s, count);
-	else
-		uart_console_write(port, s, count, serial8250_console_wait_putchar);
+	nbcon_exit_unsafe(wctxt);
+
+	__serial8250_console_write(up, wctxt, use_fifo);
+
+	/*
+	 * Re-enter an unsafe section in order to perform final actions
Quoted 8 lines

drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3471

Quoted 5 lines
drivers/tty/serial/8250/8250_port.c +5 -0
+	 * (such as re-enabling interrupts). If ownership was lost, this
+	 * context must reacquire ownership.
+	 */
+	while (!nbcon_enter_unsafe(wctxt))
+		nbcon_reacquire_nobuf(wctxt);

If we are going to use this on more locations than I would add a new API, e.g.

Quoted 7 lines

Agreed.

The 8250 has already played a significant role in influencing other NBCON implementations. I expect there will be a lot of copy/paste in the beginning. Once we start seeing which code is copied and how it is used in other drivers, we should be be able to do some general nbcon and serial_core consolidation.

Yup.

Quoted 20 lines
Quoted 15 lines

/**
* nbcon_enter_unsafe_reacquire - Enter an unsafe region in the driver,
* reacquire when needed.
* @wctxt: The write context that was handed to the write function
*
*
* The function is allowed to reacquire the console ownership to make
* sure that the caller can enter an unsafe region in the driver. *
* Warning: The caller must not longer access the text buffer. It is
* lost when the reacquire was needed. The function is intended
* for cleanup operations after emitting the message, for example,
* re-enabling interrupts on a used serial port.
*/
void nbcon_enter_unsafe_reacquire(struct nbcon_write_context *wctxt)

Quoted 4 lines
{
	while (!nbcon_enter_unsafe(wctxt))
		nbcon_reacquire_nobuf(wctxt);
}
Quoted 3 lines

I would name the function:

nbcon_enter_unsafe_reacquire_nobuf()

Yes, it looks better.

Quoted 14 lines

to make it clear that the buffer will be lost on a reacquire.

Quoted 11 lines

drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3485

Quoted 9 lines
drivers/tty/serial/8250/8250_port.c +5 -0
@@ -3365,10 +3478,21 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
 
 	if (em485) {
 		mdelay(port->rs485.delay_rts_after_send);
+
+		/* Toggle unsafe after possibly long delay */
+		nbcon_exit_unsafe(wctxt);
+		while (!nbcon_enter_unsafe(wctxt))
+			nbcon_reacquire_nobuf(wctxt);

The comment describes what the code does but it does not explain why.

Quoted 2 lines

Well, the _why_ is the same for all the toggling that we do throughout the console code.

Yes, a good place to document it would be a comment above a helper function.

Quoted 40 lines
Quoted 2 lines

My first idea was that it would allow to takeover the ownership as soon as possible in an emergency context.

Yes. That is one of the purposes. Even takeovers in panic will patiently wait up to 2ms before becoming hostile.

Quoted 3 lines

But the motivation seems to be to reduce the race with a possible unsafe takeover in the final panic flush, see
https://lore.kernel.org/all/[email protected]/

Yes, this is also valid, but for all other toggle situations as well. The driver should avoid touching the hardware if it is no longer the owner.

Quoted 26 lines

I think about entering unsafe only when really needed. Instead of releasing it and taking again immediately.

Something like:

/*
* Re-enter an unsafe section in order to perform final actions
* (such as re-enabling interrupts). If ownership was lost, this
* context must reacquire ownership.
*/
nbcon_enter_unsafe_reacquire(wctxt);

/*
* Finally, wait for transmitter to become empty
* and restore the IER
*/
wait_for_xmitr(up, UART_LSR_BOTH_EMPTY);

/*
* Exit unsafe section after a potentially long wait. It allows
* a safe takeover before another potentially long way. Also
* it reduces the race window when a possible unsafe takeover
* happened unnoticed.
*/
nbcon_exit_unsafe()

Quoted 8 lines
Quoted 6 lines
	if (em485) {
		u32 delay;

		nbcon_enter_unsafe_reacquire(wctxt);
		delay = port->rs485.delay_rts_after_send;
		nbcon_exit_unsafe(wctxt);

This unsafe enter/exit is unnecessary. This value does not change.

I have added the locking because of:

+ uart_ioctl()
+ uart_set_rs485_config()
+ uart_sanitize_serial_rs485()
+ uart_sanitize_serial_rs485_delays()

which does

	} else if (rs485->delay_rts_after_send > RS485_MAX_RTS_DELAY) {
		rs485->delay_rts_after_send = RS485_MAX_RTS_DELAY;

But I guess that this is is a rather theoretical race.

Quoted 37 lines
Quoted 37 lines
		mdelay(port->rs485.delay_rts_after_send);

		nbcon_enter_unsafe_reacquire(wctxt);
		if (em485->tx_stopped)
			up->rs485_stop_tx(up, false);
		nbcon_exit_unsafe(wctxt);
	}

	nbcon_enter_unsafe_reacquire(wctxt);

	serial_port_out(port, UART_IER, ier);

	/*
	 *	The receive handling will happen properly because the
	 *	receive ready bit will still be set; it is not cleared
	 *	on read.  However, modem control will not, we must
	 *	call it if we have saved something in the saved flags
	 *	while processing with interrupts off.
	 */
	if (up->msr_saved_flags) {
		if (is_atomic) {
			/*
			 * For atomic, MSR handling must be deferred to
			 * irq_work because this may be a context that does
			 * not permit waking up tasks.
			 *
			 * But no irq_work may be queued when suspending.
			 * In that case, the MSR handling will occur during
			 * resume in serial8250_resume_port().
			 */
			if (up->console_msr_work_allow)
				irq_work_queue(&up->console_msr_work);
		} else {
			serial8250_modem_status(up);
		}
	}
Quoted 8 lines
Quoted 5 lines
	nbcon_exit_unsafe(wctxt);
}

But I have to say that both approaches are quite hairy. I am not sure if it is worth it.

I would just move the unsafe_exit before the mdelay(). I think that is sufficient here.

Fair enough.

Quoted 7 lines
Quoted 1 line

Otherwise, I do not see any real problems in the code.

Thanks Petr for taking a detailed look at this!

I think v11 is OK for mainline (Greg has it in tty-testing now.) If anything else comes up and I need to touch the code again, I will relocate the unsafe_exit to before the mdelay().

I agree that v11 looks good for the mainline.
We could always improve/clean up the code later.

Best Regards,
Petr

maintainer
#6 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

In reply to John Ogness

Hi John,

Quoted 23 lines · John Ogness

Implement the necessary callbacks to switch the 8250 console driver to perform as an nbcon console.

Add implementations for the nbcon console callbacks:

   ->write_atomic()
   ->write_thread()
   ->device_lock()
   ->device_unlock()

and add CON_NBCON to the initial @flags.

All hardware access in the callbacks is within unsafe sections. The ->write_atomic() and ->write_thread() callbacks allow safe handover/takeover per byte and add a preceding newline if they take over from another context mid-line.

For the ->write_atomic() callback, a new irq_work is used to defer modem control since it may be called from a context that does not allow waking up tasks. During suspend/resume the irq_work is not used as this has been shown to cause suspend problems for some hardware. Upon resume, any pending modem control is performed.

Quoted 7 lines

Note: A new __serial8250_clear_IER() is introduced for direct clearing of UART_IER during console writing (which will not be holding the port lock for atomic printing or KDB/KGDB). This allows restoring a lockdep check to serial8250_clear_IER() in a follow-up commit.

I accidently responded to V9, but responding here with the same report for completeness.

This change is causing a boot regression for our Tegra20 and Tegra30 platforms. Reverting this on top of -next fixes the issue. Previously with V5 I did not see a boot issue only an issue in suspend. So far I have not had chance to dig any further.

Jon

Signature · 1 line
nvpublic
#7 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 1 line · Jon Hunter

Hi John,

Hi,

Quoted 4 lines

This change is causing a boot regression for our Tegra20 and Tegra30 platforms. Reverting this on top of -next fixes the issue. Previously with V5 I did not see a boot issue only an issue in suspend. So far I have not had chance to dig any further.

If my counting is correct: v5 is from last year

	https://lore.kernel.org/all/[email protected]/

and part of v6.14 however the nbcon bits itself were reverted before the final release. That is why v6 till the applied v11 has only two patches.

Quoted 1 line

Jon

Sebastian

maintainer
#8 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

In reply to Jon Hunter
Quoted 34 lines · Jon Hunter

Hi John,

Quoted 23 lines · John Ogness

Implement the necessary callbacks to switch the 8250 console driver to perform as an nbcon console.

Add implementations for the nbcon console callbacks:

   ->write_atomic()
   ->write_thread()
   ->device_lock()
   ->device_unlock()

and add CON_NBCON to the initial @flags.

All hardware access in the callbacks is within unsafe sections. The ->write_atomic() and ->write_thread() callbacks allow safe handover/takeover per byte and add a preceding newline if they take over from another context mid-line.

For the ->write_atomic() callback, a new irq_work is used to defer modem control since it may be called from a context that does not allow waking up tasks. During suspend/resume the irq_work is not used as this has been shown to cause suspend problems for some hardware. Upon resume, any pending modem control is performed.

Quoted 7 lines

Note: A new __serial8250_clear_IER() is introduced for direct clearing of UART_IER during console writing (which will not be holding the port lock for atomic printing or KDB/KGDB). This allows restoring a lockdep check to serial8250_clear_IER() in a follow-up commit.

Quoted 7 lines

I accidently responded to V9, but responding here with the same report for completeness.

This change is causing a boot regression for our Tegra20 and Tegra30 platforms. Reverting this on top of -next fixes the issue. Previously with V5 I did not see a boot issue only an issue in suspend. So far I have not had chance to dig any further.

Interesting.

Another clue, mentioned in the v9 thread [1], is that the boot regression does not happen with v11 when "keep_bootcon" option is used.

The "keep_bootcon" option causes that the boot console driver stays registered even when the full featured driver gets registered later.

The most important effect is that the printk kthreads can't be used as long as any boot console driver is registered. All drivers need to be called in the legacy loop in this case. There are two reasons for this:

1. Boot console drivers are synchronized only by the legacy console_lock (console_sem). port->lock is available only for the full featured driver.

2. There is no easy way to match boot console and full featured console drivers working on the same HW.

So, the regression seems to happen when the printk kthreads start being used.

Jon, could you please share the full log when "keep_bootcon" is used?

[1] https://lore.kernel.org/all/[email protected]/

Best Regards,
Petr

maintainer
#9 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi Petr,

On 18/08/2026 08:51, Petr Mladek wrote:

...

Quoted 28 lines
Quoted 4 lines

This change is causing a boot regression for our Tegra20 and Tegra30 platforms. Reverting this on top of -next fixes the issue. Previously with V5 I did not see a boot issue only an issue in suspend. So far I have not had chance to dig any further.

Interesting.

Another clue, mentioned in the v9 thread [1], is that the boot regression does not happen with v11 when "keep_bootcon" option is used.

The "keep_bootcon" option causes that the boot console driver stays registered even when the full featured driver gets registered later.

The most important effect is that the printk kthreads can't be used as long as any boot console driver is registered. All drivers need to be called in the legacy loop in this case. There are two reasons for this:

1. Boot console drivers are synchronized only by the legacy console_lock (console_sem). port->lock is available only for the full featured driver.

2. There is no easy way to match boot console and full featured console drivers working on the same HW.

Quoted 5 lines

So, the regression seems to happen when the printk kthreads start being used.

Jon, could you please share the full log when "keep_bootcon" is used?

Yes absolutely. You can find the boot log here [0]. So far nothing really stands out to me but let me know if you see anything.

Jon

[0] https://pastebin.com/FhQVSqfy

Signature · 1 line
nvpublic
maintainer
#10 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 37 lines · Jon Hunter

Hi Petr,

On 18/08/2026 08:51, Petr Mladek wrote:

...

Quoted 31 lines
Quoted 4 lines

This change is causing a boot regression for our Tegra20 and Tegra30 platforms. Reverting this on top of -next fixes the issue. Previously with V5 I did not see a boot issue only an issue in suspend. So far I have not had chance to dig any further.

Interesting.

Another clue, mentioned in the v9 thread [1], is that the boot regression does not happen with v11 when "keep_bootcon" option is used.

The "keep_bootcon" option causes that the boot console driver stays registered even when the full featured driver gets registered later.

The most important effect is that the printk kthreads can't be used as long as any boot console driver is registered. All drivers need to be called in the legacy loop in this case. There are two reasons for this:

1. Boot console drivers are synchronized only by the legacy console_lock (console_sem). port->lock is available only for the full featured driver.

2. There is no easy way to match boot console and full featured console drivers working on the same HW.

So, the regression seems to happen when the printk kthreads start being used.

Quoted 8 lines
Quoted 2 lines

Jon, could you please share the full log when "keep_bootcon" is used?

Yes absolutely. You can find the boot log here [0]. So far nothing really stands out to me but let me know if you see anything.

[0] https://pastebin.com/FhQVSqfy

Thanks for the log.

One or two things look strange/important to me.
But let me show all important parts:

[    0.000000] Kernel command line: console=ttyS0,115200n8 console=tty1 earlycon ignore_loglevel root=/dev/nfs rw ip=192.168.99.2:192.168.99.1:192.168.99.1:255.255.255.0::eth0:off nfsroot=192.168.99.1:/home/ausvrl81292/nfsroot,tcp rootwait keep_bootcon

The last "console=" parameter is "console=tty1". It is a so called preferred console. It has several effects:

+ it should get associated with /dev/console

+ it does not replay the log from the beginning when registered. Only newer messages are shown.

+ Boot consoles should get unregistered when this console gets registered (unless keep_bootcon is defined).

Note that "ttyS0" is _not_ the _preferred_console. As a result:

+ it will replay all messages when registered

+ boot console won't get unregistered when this one is registered

Now, the ordering is:

[    0.000000] earlycon: uart0 MMIO:0x70006300 (options '115200n8')
[    0.000000] printk: legacy bootconsole [uart0] enabled

First, earlycon is registered thanks because of the "earlycon" parameter.

[    0.036461] Console: colour dummy device 80x30
[    0.041020] printk: legacy console [tty1] enabled

Second, the graphical "tty1" gets registered because of the "console=tty1" parameter.

Normally, the boot console should get unregistered at this point. But it stays because of the "keep_bootcon" parameter.

[    0.645646] Serial: 8250/16550 driver, 4 ports, IRQ sharing disabled
[    0.655230] printk: console [ttyS0] disabled

IMPORTANT: This is the weird thing! I do not understand why "ttySO"
gets disabled when it has not been registered yet.

[    0.659964] 70006300.serial: ttyS0 MMIO32:0x70006300 (irq = 51, base_baud = 13500000) is a Tegra
[    0.000000] Booting Linux on physical CPU 0x0
[    0.669026] printk: console [ttyS0] enabled

The real "ttyS0" driver has been registered (added to console_list) and the legacy loop started flushing the messages in console_unlock().

The real "ttyS0" console driver started emitting messages from the beginning.

The boot console driver emits only the newly added messages "printk: console [ttyS0] enabled".

[    0.000000] Linux version 7.2.0-next-20260817 (jonathanh@build-jonathanh-noble-20260527) (arm-buildroot-linux-gnueabihf-gcc.br_real (Buildroot 2021.11-11272-ge2962af) 13.2.0, GNU ld (GNU Binutils) 2.42) #15 SMP PREEMPT Tue Aug 18 04:56:56 UTC 2026
[    0.000000] CPU: ARMv7 Processor [411fc090] revision 0 (ARMv7), cr=10c5387d

The real "ttyS0" console driver replays the entire log.

[    0.659964] 70006300.serial: ttyS0 MMIO32:0x70006300 (irq = 51, base_baud = 13500000) is a Tegra
[    0.669026] printk: console [ttyS0] enabled

And then all messages are emitted twice (by the boot console driver and by the real console driver:

[    1.574761] loop: module loaded
[    1.574761] loop: module loaded
[    1.584820] CAN device driver interface
[    1.584820] CAN device driver interface

The real console driver would normally emit these messages from the printk kthread. But it does it in the legacy loop because the boot console driver is still registered.

[    2.904178] ------------[ cut here ]------------
[    2.904178] ------------[ cut here ]------------
[    2.913507] WARNING: drivers/soc/tegra/pmc.c:4999 at tegra_pmc_enter_suspend_mode+0x168/0x178, CPU#0: swapper/0/0
[    2.913507] WARNING: drivers/soc/tegra/pmc.c:4999 at tegra_pmc_enter_suspend_mode+0x168/0x178, CPU#0: swapper/0/0

IMPORTANT: This is a warning. It is printed with
NBCON_PRIO_EMERGENCY. These messages would normally get flushed by nbcon_atomic_flush_pending() directly from printk() using con->write_atomic(). It would take over the console ownership from the kthread when needed.

In this particular log, it is emitted from the legacy loop in console_unlock() because the boot console is still registered.

Summary:

Almost everything works as expected except for:

1. I am not sure why "printk: console [ttyS0] disabled" is printed. It does not make any sense to me.

2. The WARNING would be handled with NBCON_PRIO_EMERGENCY. I wonder if this warning happened also with "v5" of this patchset.

Why is the WARNING important?

If the WARNING happened also with v5 of this patchset then it tested emergency mode as well. But the system booted with v5. So that a difference between v5 and v11 patchset might be important.

If The WARNING did _not_ happen with v5 then we probably did not test the emergency mode in this version. So that the problem might be in the emergency mode handling.

Ideas for testing:

1. I wonder if adding a WARN() with v5 of this patchset would make v5 fail as well.

2. If wonder if boot_delay=10 makes any difference. It might prevent some races.

Best Regards,
Petr

maintainer
#11 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi Petr,

On 18/08/2026 16:53, Petr Mladek wrote:

...

Quoted 24 lines

Summary:

Almost everything works as expected except for:

1. I am not sure why "printk: console [ttyS0] disabled" is printed. It does not make any sense to me.

2. The WARNING would be handled with NBCON_PRIO_EMERGENCY. I wonder if this warning happened also with "v5" of this patchset.

Why is the WARNING important?

If the WARNING happened also with v5 of this patchset then it tested emergency mode as well. But the system booted with v5. So that a difference between v5 and v11 patchset might be important.

If The WARNING did _not_ happen with v5 then we probably did not test the emergency mode in this version. So that the problem might be in the emergency mode handling.

Quoted 4 lines

Ideas for testing:

1. I wonder if adding a WARN() with v5 of this patchset would make v5 fail as well.

Thanks for the analysis. I don't think that WARN occurred back in V5 because this is something I believe is fairly recent and we need to fix. Anyway, for testing, I simply removed the warning to see if this has an impact and it does. So you are right, this appears to be the cause of the issue.

It would be interesting to know if anyone else sees this if an WARNING is inserted somewhere during boot.

Cheers
Jon

Signature · 1 line
nvpublic
author maintainer
#12 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi Jon,

Quoted 8 lines · Jon Hunter
Quoted 2 lines

1. I wonder if adding a WARN() with v5 of this patchset would make v5 fail as well.

Thanks for the analysis. I don't think that WARN occurred back in V5 because this is something I believe is fairly recent and we need to fix. Anyway, for testing, I simply removed the warning to see if this has an impact and it does. So you are right, this appears to be the cause of the issue.

Instead of the WARN, could you insert a pr_err() call? I would like to remove the EMERGENCY printing from the picture if we can. If it hangs due to NORMAL console printing from that suspend function, I expect it is irqwork related.

John

maintainer
#13 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi John,

Quoted 12 lines · John Ogness

Hi Jon,

Quoted 8 lines · Jon Hunter
Quoted 2 lines

1. I wonder if adding a WARN() with v5 of this patchset would make v5 fail as well.

Thanks for the analysis. I don't think that WARN occurred back in V5 because this is something I believe is fairly recent and we need to fix. Anyway, for testing, I simply removed the warning to see if this has an impact and it does. So you are right, this appears to be the cause of the issue.

Quoted 4 lines

Instead of the WARN, could you insert a pr_err() call? I would like to remove the EMERGENCY printing from the picture if we can. If it hangs due to NORMAL console printing from that suspend function, I expect it is irqwork related.

I replaced the WARN with a pr_err() and I see the same issue.

Thanks
Jon

Signature · 1 line
nvpublic
author maintainer
#14 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 1 line · Jon Hunter

I replaced the WARN with a pr_err() and I see the same issue.

Thanks for testing. But just to be certain, can you restore the original code (with the warning) and apply this horrible hack to force atomic printing? I want to be certain that the problem goes away when nbcon deferred printing is not active. For thet test you should boot normally (without keep_bootcon).

===== BEGIN HACK =====

kernel/printk/internal.h +1 -0
index 85fbf1801cbe0..5806815fa11b2 100644
@@ -202,6 +202,7 @@ static inline void printk_get_console_flush_type(struct console_flush_type *ft)
 				ft->nbcon_offload = true;
 			else
 				ft->nbcon_atomic = true;
+			ft->nbcon_atomic = true;
 		}
 
 		/* Legacy consoles are flushed directly when possible. */
kernel/printk/nbcon.c +1 -0
index 4b03b019cd5ee..152d9ac28176d 100644
@@ -1185,6 +1185,7 @@ static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_contex
 
 	if (kthread_should_stop())
 		return true;
+	return false;
 
 	/*
 	 * Block the kthread when the system is in an emergency or panic mode.

===== END HACK =====

I am expecting that this will work and the warning will be cleanly visible on the UART console.

If the above test was successful, I am wondering if cpuidle should be disabling the deferred printing as this seems quite similar to suspend.

Could you perform another test using the following hack _instead_ of the above hack? This is probably a bit extreme since it does more than just disable deferring, but it could help us figure out what is going on.

===== BEGIN HACK2 =====

drivers/cpuidle/cpuidle-tegra.c +6 -1
index aca907a62bb5d..0fb449e23f677 100644
@@ -15,6 +15,7 @@
 #define pr_fmt(fmt)	"tegra-cpuidle: " fmt
 
 #include <linux/atomic.h>
+#include <linux/console.h>
 #include <linux/cpuidle.h>
 #include <linux/cpumask.h>
 #include <linux/cpu_pm.h>
@@ -165,6 +166,8 @@ static __cpuidle int tegra_cpuidle_state_enter(struct cpuidle_device *dev,
 {
 	int err;
 
+	console_suspend_all();
+
 	/*
 	 * CC6 state is the "CPU cluster power-off" state.  In order to
 	 * enter this state, at first the secondary CPU cores need to be
@@ -176,7 +179,7 @@ static __cpuidle int tegra_cpuidle_state_enter(struct cpuidle_device *dev,
 	if (index == TEGRA_CC6) {
 		err = tegra_cpuidle_coupled_barrier(dev);
 		if (err)
-			return err;
+			goto out;
 	}
 
 	local_fiq_disable();
@@ -204,6 +207,8 @@ static __cpuidle int tegra_cpuidle_state_enter(struct cpuidle_device *dev,
 	cpu_pm_exit();
 	tegra_pm_clear_cpu_in_lp2();
 	local_fiq_enable();
+out:
+	console_resume_all();
 
 	return err ?: index;
 }
kernel/printk/printk.c +1 -1
index 3fcdf4b4e2e53..8d126b5068684 100644
@@ -2787,7 +2787,7 @@ void console_suspend_all(void)
 	struct console *con;
 
 	if (console_suspend_enabled)
-		pr_info("Suspending console(s) (use no_console_suspend to debug)\n");
+		pr_info_once("Suspending console(s) (use no_console_suspend to debug)\n");
 
 	/*
 	 * Flush any console backlog and then avoid queueing irq_work until

===== END HACK2 =====

John Ogness

maintainer
#15 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi John,

Quoted 36 lines · John Ogness
Quoted 1 line · Jon Hunter

I replaced the WARN with a pr_err() and I see the same issue.

Thanks for testing. But just to be certain, can you restore the original code (with the warning) and apply this horrible hack to force atomic printing? I want to be certain that the problem goes away when nbcon deferred printing is not active. For thet test you should boot normally (without keep_bootcon).

===== BEGIN HACK =====

kernel/printk/internal.h +1 -0
index 85fbf1801cbe0..5806815fa11b2 100644
@@ -202,6 +202,7 @@ static inline void printk_get_console_flush_type(struct console_flush_type *ft)
  				ft->nbcon_offload = true;
  			else
  				ft->nbcon_atomic = true;
+			ft->nbcon_atomic = true;
  		}
 
  		/* Legacy consoles are flushed directly when possible. */
kernel/printk/nbcon.c +1 -0
index 4b03b019cd5ee..152d9ac28176d 100644
@@ -1185,6 +1185,7 @@ static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_contex
 
  	if (kthread_should_stop())
  		return true;
+	return false;
 
  	/*
  	 * Block the kthread when the system is in an emergency or panic mode.

===== END HACK =====

Quoted 2 lines

I am expecting that this will work and the warning will be cleanly visible on the UART console.

With this I see a little more output on the console, but it still appears to hang later and never fully boots.

Boot log here: https://pastebin.com/Xe1vmeUj

Quoted 60 lines

If the above test was successful, I am wondering if cpuidle should be disabling the deferred printing as this seems quite similar to suspend.

Could you perform another test using the following hack _instead_ of the above hack? This is probably a bit extreme since it does more than just disable deferring, but it could help us figure out what is going on.

===== BEGIN HACK2 =====

drivers/cpuidle/cpuidle-tegra.c +6 -1
index aca907a62bb5d..0fb449e23f677 100644
@@ -15,6 +15,7 @@
  #define pr_fmt(fmt)	"tegra-cpuidle: " fmt
 
  #include <linux/atomic.h>
+#include <linux/console.h>
  #include <linux/cpuidle.h>
  #include <linux/cpumask.h>
  #include <linux/cpu_pm.h>
@@ -165,6 +166,8 @@ static __cpuidle int tegra_cpuidle_state_enter(struct cpuidle_device *dev,
  {
  	int err;
 
+	console_suspend_all();
+
  	/*
  	 * CC6 state is the "CPU cluster power-off" state.  In order to
  	 * enter this state, at first the secondary CPU cores need to be
@@ -176,7 +179,7 @@ static __cpuidle int tegra_cpuidle_state_enter(struct cpuidle_device *dev,
  	if (index == TEGRA_CC6) {
  		err = tegra_cpuidle_coupled_barrier(dev);
  		if (err)
-			return err;
+			goto out;
  	}
 
  	local_fiq_disable();
@@ -204,6 +207,8 @@ static __cpuidle int tegra_cpuidle_state_enter(struct cpuidle_device *dev,
  	cpu_pm_exit();
  	tegra_pm_clear_cpu_in_lp2();
  	local_fiq_enable();
+out:
+	console_resume_all();
 
  	return err ?: index;
  }
kernel/printk/printk.c +1 -1
index 3fcdf4b4e2e53..8d126b5068684 100644
@@ -2787,7 +2787,7 @@ void console_suspend_all(void)
  	struct console *con;
 
  	if (console_suspend_enabled)
-		pr_info("Suspending console(s) (use no_console_suspend to debug)\n");
+		pr_info_once("Suspending console(s) (use no_console_suspend to debug)\n");
 
  	/*
  	 * Flush any console backlog and then avoid queueing irq_work until
Quoted 1 line

===== END HACK2 =====

I tried the above anyway, but this is hitting a 'scheduling while atomic' bug ...

[    2.083585] BUG: scheduling while atomic: swapper/0/0/0x00000002
[    2.084118] Modules linked in:
[    2.099178]
[    2.106346] CPU: 1 UID: 0 PID: 0 Comm: swapper/1 Not tainted 7.2.0-next-20260817-00003-ga0c285a6f831 #23 PREEMPT
[    2.106413] Hardware name: NVIDIA Tegra SoC (Flattened Device Tree)
[    2.106440] Call trace:
[    2.106468]  unwind_backtrace from show_stack+0x10/0x14
[    2.106596]  show_stack from dump_stack_lvl+0x54/0x68
[    2.106714]  dump_stack_lvl from __schedule_bug+0x50/0x64
[    2.106815]  __schedule_bug from __schedule+0x9e0/0x1348
[    2.106897]  __schedule from schedule+0x2c/0xf8
[    2.106962]  schedule from schedule_timeout+0xbc/0x114
[    2.107049]  schedule_timeout from __down_common+0x14c/0x3ac
[    2.107137]  __down_common from down+0x4c/0x60
[    2.107213]  down from __pr_flush+0x294/0x3d0
[    2.107284]  __pr_flush from console_suspend_all+0x44/0xc8
[    2.107342]  console_suspend_all from tegra_cpuidle_state_enter+0x18/0x33c
[    2.107406]  tegra_cpuidle_state_enter from tegra_cpuidle_enter+0x168/0x184
[    2.107470]  tegra_cpuidle_enter from cpuidle_enter_state+0x80/0x610
[    2.107533]  cpuidle_enter_state from cpuidle_enter_state_coupled+0x3ac/0x400
[    2.107637]  cpuidle_enter_state_coupled from cpuidle_enter+0x3c/0x54
[    2.107736]  cpuidle_enter from do_idle+0x150/0x340
[    2.107809]  do_idle from cpu_startup_entry+0x28/0x2c
[    2.107861]  cpu_startup_entry from secondary_start_kernel+0x118/0x120
[    2.107959]  secondary_start_kernel from 0x1160a0
[    2.108085] bad: scheduling from the idle thread!

Cheers
Jon

Signature · 1 line
nvpublic
author maintainer
#16 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi Jon,

Quoted 4 lines · Jon Hunter

With this I see a little more output on the console, but it still appears to hang later and never fully boots.

Boot log here: https://pastebin.com/Xe1vmeUj

Thanks, this information is helpful. Below is another hack, to be used on the unmodified linux-next. For whatever reason, I think the queuing of irqwork is causing problems. Perhaps the irqwork is actually firing even though interrupts are supposed to be disabled here.

The following hack will disable the queuing during the cpuidle. Please give this a try:

===== BEGIN CPUIDLE HACK =====

drivers/cpuidle/cpuidle-tegra.c +5 -0
index aca907a62bb5d..1dca9d6defbff 100644
@@ -226,6 +226,7 @@ static int tegra_cpuidle_adjust_state_index(int index, unsigned int cpu)
 	return index;
 }
 
+extern bool console_irqwork_blocked;
 static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
 					 struct cpuidle_driver *drv,
 					 int index)
@@ -238,6 +239,8 @@ static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
 	if (dev->states_usage[index].disable)
 		return -1;
 
+	console_irqwork_blocked = true;
+
 	if (index == TEGRA_C1) {
 		if (do_rcu)
 			ct_cpuidle_enter();
@@ -256,6 +259,8 @@ static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
 		index = ret;
 	}
 
+	console_irqwork_blocked = false;
+
 	return index;
 }

===== END CPUIDLE HACK =====

John Ogness

maintainer
#17 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi John,

Quoted 46 lines · John Ogness

Hi Jon,

Quoted 4 lines · Jon Hunter

With this I see a little more output on the console, but it still appears to hang later and never fully boots.

Boot log here: https://pastebin.com/Xe1vmeUj

Thanks, this information is helpful. Below is another hack, to be used on the unmodified linux-next. For whatever reason, I think the queuing of irqwork is causing problems. Perhaps the irqwork is actually firing even though interrupts are supposed to be disabled here.

The following hack will disable the queuing during the cpuidle. Please give this a try:

===== BEGIN CPUIDLE HACK =====

drivers/cpuidle/cpuidle-tegra.c +5 -0
index aca907a62bb5d..1dca9d6defbff 100644
@@ -226,6 +226,7 @@ static int tegra_cpuidle_adjust_state_index(int index, unsigned int cpu)
  	return index;
  }
 
+extern bool console_irqwork_blocked;
  static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
  					 struct cpuidle_driver *drv,
  					 int index)
@@ -238,6 +239,8 @@ static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
  	if (dev->states_usage[index].disable)
  		return -1;
 
+	console_irqwork_blocked = true;
+
  	if (index == TEGRA_C1) {
  		if (do_rcu)
  			ct_cpuidle_enter();
@@ -256,6 +259,8 @@ static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
  		index = ret;
  	}
 
+	console_irqwork_blocked = false;
+
  	return index;
  }
Quoted 1 line

===== END CPUIDLE HACK =====

Yes that does boot. Let me know if there are any further tests I can run.

Thanks!
Jon

Signature · 1 line
nvpublic
author maintainer
#18 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 39 lines · Jon Hunter
Quoted 39 lines
Quoted 4 lines

With this I see a little more output on the console, but it still appears to hang later and never fully boots.

Boot log here: https://pastebin.com/Xe1vmeUj

Thanks, this information is helpful. Below is another hack, to be used on the unmodified linux-next. For whatever reason, I think the queuing of irqwork is causing problems. Perhaps the irqwork is actually firing even though interrupts are supposed to be disabled here.

The following hack will disable the queuing during the cpuidle. Please give this a try:

===== BEGIN CPUIDLE HACK =====

drivers/cpuidle/cpuidle-tegra.c +3 -0
index aca907a62bb5d..1dca9d6defbff 100644
@@ -226,6 +226,7 @@ static int tegra_cpuidle_adjust_state_index(int index, unsigned int cpu)
  	return index;
  }
 
+extern bool console_irqwork_blocked;
  static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
  					 struct cpuidle_driver *drv,
  					 int index)
@@ -238,6 +239,8 @@ static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
  	if (dev->states_usage[index].disable)
  		return -1;
 
+	console_irqwork_blocked = true;
+
  	if (index == TEGRA_C1) {
  		if (do_rcu)
  			ct_cpuidle_enter();
@@ -256,6 +259,8 @@ static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
  		index = ret;
  	}
 
Quoted 7 lines
Quoted 5 lines
drivers/cpuidle/cpuidle-tegra.c +2 -0
+	console_irqwork_blocked = false;
+
  	return index;
  }

===== END CPUIDLE HACK =====

Yes that does boot.

Your results surprise me a bit because in previous attempts it seemed you were also getting hangs related to printk's not produced within cpuidle. This would lead me to believe that cpuidle is entered while irq_work from a directly preceeding printk (from outside cpuidle) was pending and caused a problem.

Does this hack really work reliably, without using keep_bootcon or any other patches?

Also, although this hack will avoid queuing irq_work from within cpuidle, it does not prevent the 8250 console driver from queuing irq_work for MSR handling during atomic printing. There is no generic console callback to deal with that (other than suspending the console).

I suspect it is a general irq_work problem on tegra regarding suspend/cpuidle. Really the problem should be fixed there. But I would need hardware to investigate the issue.

As for this 8250 "switch to nbcon" series, I am uncertain how to proceed. Is it really printk's job (and, by extension, the console driver's job) to decipher when it is allowed to queue irq_work?

John

maintainer
#19 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 62 lines · John Ogness
Quoted 39 lines · Jon Hunter
Quoted 39 lines
Quoted 4 lines

With this I see a little more output on the console, but it still appears to hang later and never fully boots.

Boot log here: https://pastebin.com/Xe1vmeUj

Thanks, this information is helpful. Below is another hack, to be used on the unmodified linux-next. For whatever reason, I think the queuing of irqwork is causing problems. Perhaps the irqwork is actually firing even though interrupts are supposed to be disabled here.

The following hack will disable the queuing during the cpuidle. Please give this a try:

===== BEGIN CPUIDLE HACK =====

drivers/cpuidle/cpuidle-tegra.c +3 -0
index aca907a62bb5d..1dca9d6defbff 100644
@@ -226,6 +226,7 @@ static int tegra_cpuidle_adjust_state_index(int index, unsigned int cpu)
  	return index;
  }
 
+extern bool console_irqwork_blocked;
  static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
  					 struct cpuidle_driver *drv,
  					 int index)
@@ -238,6 +239,8 @@ static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
  	if (dev->states_usage[index].disable)
  		return -1;
 
+	console_irqwork_blocked = true;
+
  	if (index == TEGRA_C1) {
  		if (do_rcu)
  			ct_cpuidle_enter();
@@ -256,6 +259,8 @@ static __cpuidle int tegra_cpuidle_enter(struct cpuidle_device *dev,
  		index = ret;
  	}
 
Quoted 7 lines
Quoted 5 lines
drivers/cpuidle/cpuidle-tegra.c +2 -0
+	console_irqwork_blocked = false;
+
  	return index;
  }

===== END CPUIDLE HACK =====

Yes that does boot.

Your results surprise me a bit because in previous attempts it seemed you were also getting hangs related to printk's not produced within cpuidle. This would lead me to believe that cpuidle is entered while irq_work from a directly preceeding printk (from outside cpuidle) was pending and caused a problem.

Does this hack really work reliably, without using keep_bootcon or any other patches?

Also, although this hack will avoid queuing irq_work from within cpuidle, it does not prevent the 8250 console driver from queuing irq_work for MSR handling during atomic printing. There is no generic console callback to deal with that (other than suspending the console).

Quoted 7 lines

I suspect it is a general irq_work problem on tegra regarding suspend/cpuidle. Really the problem should be fixed there. But I would need hardware to investigate the issue.

As for this 8250 "switch to nbcon" series, I am uncertain how to proceed. Is it really printk's job (and, by extension, the console driver's job) to decipher when it is allowed to queue irq_work?

I wonder how other architectures hanle this situation. I wonder if they just ignore the pending IRQs.

Best Regards,
Petr

#20 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 6 lines · Petr Mladek
Quoted 3 lines

As for this 8250 "switch to nbcon" series, I am uncertain how to proceed. Is it really printk's job (and, by extension, the console driver's job) to decipher when it is allowed to queue irq_work?

I wonder how other architectures hanle this situation. I wonder if they just ignore the pending IRQs.

The interrupts should be disabled and the enqueued irq_work should be delayed until after the interrupts are enabled again while entering idle. An interrupt is probably what pulls the CPU out of idle. Is it possible to get some kind of crash dump or anything else to figure out the state of CPU while it hangs?
If that irq_work is the problem that making nbcon_irq_work LAZY should delay it, masking the problem.

Either way, I don't see why raising irq_work should be a problem. That interrupt has to trigger once interrupts are enabled again. However, if this is missed then further irq_work enqueues won't trigger an interrupt which might be a problem if something relies on it.

So the question what is doing while it makes no progress and does not fully boot.

Quoted 2 lines

Best Regards,
Petr

Sebastian

author maintainer
#21 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi Jon,

Quoted 2 lines · Sebastian Andrzej Siewior

If that irq_work is the problem that making nbcon_irq_work LAZY should delay it, masking the problem.

Making nbcon_irq_work LAZY should work around this problem. And, actually, it should be fine to make nbcon_irq_work LAZY in general since it is only related to non-emergency messages, which are delayed anyway.

Jon, could you run your tests with only the following patch applied? If this works for you, I will submit an official version of the patch.

kernel/printk/nbcon.c +1 -1
index a5921a84a80ed..ad79e30afd647 100644
@@ -1782,7 +1782,7 @@ bool nbcon_alloc(struct console *con)
 	}
 
 	rcuwait_init(&con->rcuwait);
-	init_irq_work(&con->irq_work, nbcon_irq_work);
+	con->irq_work = IRQ_WORK_INIT_LAZY(nbcon_irq_work);
 	atomic_long_set(&ACCESS_PRIVATE(con, nbcon_prev_seq), -1UL);
 	nbcon_state_set(con, &state);
 
maintainer
#22 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi John,

Quoted 18 lines · John Ogness

Hi Jon,

Quoted 2 lines · Sebastian Andrzej Siewior

If that irq_work is the problem that making nbcon_irq_work LAZY should delay it, masking the problem.

Making nbcon_irq_work LAZY should work around this problem. And, actually, it should be fine to make nbcon_irq_work LAZY in general since it is only related to non-emergency messages, which are delayed anyway.

Jon, could you run your tests with only the following patch applied? If this works for you, I will submit an official version of the patch.

kernel/printk/nbcon.c +0 -0
index a5921a84a80ed..ad79e30afd647 100644

kernel/printk/nbcon.c bool nbcon_alloc(struct console *con) · line 1788

Quoted 8 lines
kernel/printk/nbcon.c +1 -1
@@ -1782,7 +1782,7 @@ bool nbcon_alloc(struct console *con)
  	}
 
  	rcuwait_init(&con->rcuwait);
-	init_irq_work(&con->irq_work, nbcon_irq_work);
+	con->irq_work = IRQ_WORK_INIT_LAZY(nbcon_irq_work);
  	atomic_long_set(&ACCESS_PRIVATE(con, nbcon_prev_seq), -1UL);
  	nbcon_state_set(con, &state);
 

I gave this a quick try on Tegra20 and Tegra30 and I can confirm that this does work.

Thanks!
Jon

Signature · 1 line
nvpublic
author maintainer
#23 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Hi Jon,

Quoted 7 lines · Jon Hunter
Quoted 7 lines
kernel/printk/nbcon.c +0 -0
index a5921a84a80ed..ad79e30afd647 100644
@@ -1782,7 +1782,7 @@ bool nbcon_alloc(struct console *con)
  	}
 
Quoted 9 lines

kernel/printk/nbcon.c bool nbcon_alloc(struct console *con) · line 1788

Quoted 6 lines
kernel/printk/nbcon.c +1 -1
  	rcuwait_init(&con->rcuwait);
-	init_irq_work(&con->irq_work, nbcon_irq_work);
+	con->irq_work = IRQ_WORK_INIT_LAZY(nbcon_irq_work);
  	atomic_long_set(&ACCESS_PRIVATE(con, nbcon_prev_seq), -1UL);
  	nbcon_state_set(con, &state);
 

I gave this a quick try on Tegra20 and Tegra30 and I can confirm that this does work.

OK, great. I will prepare an official patch for mainline to change nbcon_irq_work to LAZY.

Back with v5 you reported [0] similar issues due to suspend/resume. For that we implemented the @console_irqwork_blocked workaround. We probably do not need that anymore. You could verify that with the following hack (and booting with no_console_suspend). Note that you also need the LAZY change above.

===== BEGIN SUSPEND HACK =====

kernel/printk/printk.c +0 -1
index 6d3d18a50da74..73c85f6de2c9e 100644
@@ -2796,7 +2796,6 @@ void console_suspend_all(void)
 	 * any klogd waiters are not triggered.
 	 */
 	pr_flush(1000, true);
-	console_irqwork_blocked = true;
 
 	if (!console_suspend_enabled)
 		return;

===== END SUSPEND HACK =====

However, this workaround did allow the consoles to switch to atomic mode on suspend when no_console_suspend is specified at boot. So the workaround might be interesting for that reason alone. But we should no longer need it for the reasons mentioned in commit 26873e3e7f0c ("printk: Avoid scheduling irq_work on suspend")

John

[0] https://lore.kernel.org/lkml/[email protected]

maintainer
#24 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

In reply to John Ogness

Hi John,

On 25/08/2026 23:02, John Ogness wrote:

...

Quoted 8 lines

Your results surprise me a bit because in previous attempts it seemed you were also getting hangs related to printk's not produced within cpuidle. This would lead me to believe that cpuidle is entered while irq_work from a directly preceeding printk (from outside cpuidle) was pending and caused a problem.

Does this hack really work reliably, without using keep_bootcon or any other patches?

I went back and started again on top of next-20260825.

Vanilla next-20260825 is failing on Tegra20 about 19 boots out of 20. With hack 1 and hack 2 it is also pretty much 100% failure with 20 boots. However, with the latest hack it is passing 100% (20 out of 20 boots). So yes I say that this works reliably.

Quoted 8 lines

Also, although this hack will avoid queuing irq_work from within cpuidle, it does not prevent the 8250 console driver from queuing irq_work for MSR handling during atomic printing. There is no generic console callback to deal with that (other than suspending the console).

I suspect it is a general irq_work problem on tegra regarding suspend/cpuidle. Really the problem should be fixed there. But I would need hardware to investigate the issue.

I am seeing this on Tegra20 and Tegra30 boards, which are the oldest we test and so difficult to get hardware for these.

So there is the following code in the cpuidle-tegra.c driver ...

if (tegra_pending_sgi()) {
/*
* CPU got local interrupt that will be lost after GIC's
* shutdown because GIC driver doesn't save/restore the
* pending SGI state across CPU cluster PM. Abort and retry
* next time.
*/
atomic_set(&tegra_abort_flag, 1);
}

So may be interrupts are getting lost?

Quoted 3 lines

As for this 8250 "switch to nbcon" series, I am uncertain how to proceed. Is it really printk's job (and, by extension, the console driver's job) to decipher when it is allowed to queue irq_work?

Yes understand. I am not sure if it is possible to opt out of this switch for old legacy platforms like this?

Jon

Signature · 1 line
nvpublic
author maintainer
#25 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

In reply to Petr Mladek
Quoted 1 line · Petr Mladek
Quoted 1 line

[0] https://pastebin.com/FhQVSqfy

...

Quoted 4 lines

Almost everything works as expected except for:

1. I am not sure why "printk: console [ttyS0] disabled" is printed. It does not make any sense to me.

I have seen this in the past. It can happens unregister_console_locked() has always printed the disabled message before actually checking if the console is registered.

IIRC it has something to do with serial8250_register_8250_port() calling uart_remove_one_port(). I remember starting to look into how to fix it but then was going down a rabbit hole that exceeded my scope at the time.

Quoted 3 lines

2. The WARNING would be handled with NBCON_PRIO_EMERGENCY. I wonder if this warning happened also with "v5" of this patchset.

The tegra issue we had with v5 was related to irqwork being scheduled when suspending. When keep_bootcon is specified, there will be no irqwork scheduled during boot because legacy_direct=true.

From the backtrace in the posted boot log we see that the warning is within tegra_pmc_enter_suspend_mode(). I am wondering if this is the same issue we saw with v5... an irqwork getting scheduled when the tegra is entering some suspend state.

Back then I was able to reproduce the suspend issue on a Phytec-WEGA board. I will see if I can reproduce this report on it as well.

John

maintainer
#26 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 23 lines · John Ogness
Quoted 1 line · Petr Mladek
Quoted 1 line

[0] https://pastebin.com/FhQVSqfy

...

Quoted 4 lines

Almost everything works as expected except for:

1. I am not sure why "printk: console [ttyS0] disabled" is printed. It does not make any sense to me.

I have seen this in the past. It can happens unregister_console_locked() has always printed the disabled message before actually checking if the console is registered.

IIRC it has something to do with serial8250_register_8250_port() calling uart_remove_one_port(). I remember starting to look into how to fix it but then was going down a rabbit hole that exceeded my scope at the time.

Quoted 3 lines

2. The WARNING would be handled with NBCON_PRIO_EMERGENCY. I wonder if this warning happened also with "v5" of this patchset.

Quoted 8 lines

The tegra issue we had with v5 was related to irqwork being scheduled when suspending. When keep_bootcon is specified, there will be no irqwork scheduled during boot because legacy_direct=true.

From the backtrace in the posted boot log we see that the warning is within tegra_pmc_enter_suspend_mode(). I am wondering if this is the same issue we saw with v5... an irqwork getting scheduled when the tegra is entering some suspend state.

You mentioning tegra_pmc_enter_suspend_mode() jogged a memory. We have to play some weird cpuidle tricks when PCI interrupts are anebled because of a hardware bug. That bug, as far as I know, is only Tegra20 specific, so it doesn't explain this. In this particular case we're also not talking about PCI in this case, *but* I wonder if there's maybe other IRQ issues related to cpuidle.

It's a bit of a shot in the dark, but we could try disabling the various suspend modes via the PMC DT node. Notably LP2 seems to be the one that's causing issues on Tegra20 with PCI, so maybe changing nvidia,suspend-mode to something like 0 or 1 would help narrow things down?

Thierry

maintainer
#27 lore raw

Re: [PATCH tty v11 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 40 lines · Thierry Reding
Quoted 23 lines · John Ogness
Quoted 1 line · Petr Mladek
Quoted 1 line

[0] https://pastebin.com/FhQVSqfy

...

Quoted 4 lines

Almost everything works as expected except for:

1. I am not sure why "printk: console [ttyS0] disabled" is printed. It does not make any sense to me.

I have seen this in the past. It can happens unregister_console_locked() has always printed the disabled message before actually checking if the console is registered.

IIRC it has something to do with serial8250_register_8250_port() calling uart_remove_one_port(). I remember starting to look into how to fix it but then was going down a rabbit hole that exceeded my scope at the time.

Quoted 3 lines

2. The WARNING would be handled with NBCON_PRIO_EMERGENCY. I wonder if this warning happened also with "v5" of this patchset.

Quoted 8 lines

The tegra issue we had with v5 was related to irqwork being scheduled when suspending. When keep_bootcon is specified, there will be no irqwork scheduled during boot because legacy_direct=true.

From the backtrace in the posted boot log we see that the warning is
within tegra_pmc_enter_suspend_mode(). I am wondering if this is the same issue we saw with v5... an irqwork getting scheduled when the tegra is entering some suspend state.

You mentioning tegra_pmc_enter_suspend_mode() jogged a memory. We have to play some weird cpuidle tricks when PCI interrupts are anebled because of a hardware bug. That bug, as far as I know, is only Tegra20 specific, so it doesn't explain this. In this particular case we're also not talking about PCI in this case, *but* I wonder if there's maybe other IRQ issues related to cpuidle.

Quoted 5 lines

It's a bit of a shot in the dark, but we could try disabling the various suspend modes via the PMC DT node. Notably LP2 seems to be the one that's causing issues on Tegra20 with PCI, so maybe changing nvidia,suspend-mode to something like 0 or 1 would help narrow things down?

By default 'nvidia,suspend-mode' is set to 1 for this board. In order, to disable CPU idle CC6 state (CPU cluster off) I needed to remove this property completely. Doing so does work around the problem and the device boots.

Similarly, if I make the following change to ensure that CC6 is disabled for Tegra20 and Tegra30 this also works for both Tegra20 and Tegra30 ...

drivers/cpuidle/cpuidle-tegra.c +2 -0
index aca907a62bb5..d592e1f37f00 100644
@@ -374,9 +374,11 @@ static int tegra_cpuidle_probe(struct platform_device *pdev)
        case TEGRA20:
                /* Tegra20 isn't capable to power-off individual CPU cores */
                tegra_cpuidle_disable_state(TEGRA_C7);
+               tegra_cpuidle_disable_state(TEGRA_CC6);
                break;
 
        case TEGRA30:
+               tegra_cpuidle_disable_state(TEGRA_CC6);
                break;
 
        case TEGRA114:

Jon

Signature · 1 line
nvpublic
author maintainer
#28 lore raw

PATCH v11 2/2 Revert "serial: 8250: drop lockdep annotation from serial8250_clear_IER()"

In reply to John Ogness

This reverts commit 3d9e6f556e235ddcdc9f73600fdd46fe1736b090.

The 8250 driver no longer depends on @oops_in_progress and will no longer violate the port->lock locking constraints.

---
 drivers/tty/serial/8250/8250_port.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/tty/serial/8250/8250_port.c +3 -0
index b9ea4f898474e..6c10bff109701 100644
@@ -719,6 +719,9 @@ static void __serial8250_clear_IER(struct uart_8250_port *up)
 
 static inline void serial8250_clear_IER(struct uart_8250_port *up)
 {
+	/* Port locked to synchronize UART_IER access against the console */
+	lockdep_assert_held_once(&up->port.lock);
+
 	__serial8250_clear_IER(up);
 }
 
Signature · 1 line
2.47.3
#29 lore raw
In reply to John Ogness
Quoted 6 lines · John Ogness

Hi,

This is v11 of a series to convert the 8250 driver to an NBCON console, providing both threaded and atomic printing implementations. v10 of this series is here [0].

Quoted 6 lines

Note that v5 was pulled into the 6.14 merge window, but was then reverted before the 6.14-rc1 release due to kernel test robot problems. It turned out that the problems were related to general 8250 driver issues. Those issues have now been addressed mainline, paving the way for a second take at converting the 8250 driver to NBCON.

Nice work, now queued up, let's see what breaks! :)