Skip to contentSkip to conversation

Convert 8250 to NBCON, take 2

PATCH Superseded John Ogness 2 participants 7 messages Jul 22 – Jul 23

Subsystems (4)

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v8 cover Convert 8250 to NBCON, take 2

Hi,

This is v8 of a series to convert the 8250 driver to an NBCON console, providing both threaded and atomic printing implementations. v7 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 v7:

- Perform the following renames to make it clear that these fields and functions are only related to the console:

In struct uart_8250_port:

    avoid_modem_status_work -> console_msr_work_allow
    modem_status_work       -> console_msr_work

The irq_work handler:

    modem_status_handler()  -> console_msr_handler()

Note that this also inverts the flag for allowing msr_work in order to avoid using a double-negative for the reader:

if (!avoid_modem_status_work)

changes to:

if (console_msr_work_allow)

- Modify uart_8250_port->console_msr_work_allow under the port->lock in order to synchronize with its reader serial8250_console_write().

- In serial8250_suspend_port() and serial8250_resume_port(), move the @console_msr_work_allow update inside "if (uart_console(port))". This involves refactoring the neighboring code as well and has the nice side-effect of grouping all the console specialties into a single block.

- Change the IER-disabling comments in serial8250_console_write() and serial8250_put_poll_char() to mention what type of synchronization is used when the port->lock is not taken.

The changes v6 -> v7:

- Add irq_work_sync() calls to make sure there is no pending modem status work when suspending or unregistering the console.

- In the put_poll_char() callback, use __serial8250_clear_IER() because KDB/KGDB do not acquire the port lock to print.

- In serial8250_console_write(), re-enter unsafe section after printing text, even if ownership was _not_ lost. The final actions need to be performed within an unsafe section.

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 | 200 +++++++++++++++++++++++-----
 include/linux/serial_8250.h         |  16 ++-
 5 files changed, 236 insertions(+), 51 deletions(-)

base-commit: 782f4dbd1794b4f30dc116a7ca42c5962c409be8

Signature · 1 line
2.47.3
#1 lore raw

PATCH v8 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 | 197 +++++++++++++++++++++++-----
 include/linux/serial_8250.h         |  16 ++-
 5 files changed, 233 insertions(+), 51 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 +161 -36

329 lines not on this page · raw message

include/linux/serial_8250.h +14 -2
index a95b2d143d248..0db2dd5569f4a 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
#2 lore raw

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

Quoted 38 lines · Sashiko
Quoted 18 lines
drivers/tty/serial/8250/8250_port.c +5 -1
index 8c241ec7f4f29..7726cca00d364 100644
@@ -3198,11 +3218,22 @@ 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 (i = 0; i < count; i++) {
+		/*
+		 * Pass the ownership as quickly as possible to a higher

drivers/tty/serial/8250/8250_port.c void serial8250_set_defaults(struct uart_8250_port *up) · line 3239

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +7 -0
+		 * priority context. Otherwise, its attempt to take over
+		 * the ownership might timeout. The new owner will wait
+		 * for UART_LSR_THRE before reusing the fifo.
+		 */
+		if (wctxt && !nbcon_can_proceed(wctxt))
+			return;
+
 		if (wait_for_lsr(up, UART_LSR_THRE))
 			return;
 	}

Does this leave the hardware state vulnerable to corruption during an emergency console takeover?

The serial8250_fifo_wait_for_lsr_thre() function polls the UART_LSR register by calling wait_for_lsr() and modifies up->lsr_saved_flags non-atomically. Since reading UART_LSR clears hardware error flags (parity, framing, overrun, break), concurrent reads can cause one CPU to consume and lose the flags before the other can observe them.

Quoted 6 lines

Because serial8250_fifo_wait_for_lsr_thre() is called outside the nbcon_enter_unsafe() block, the nbcon system might grant a safe takeover to an emergency message (e.g. sysrq, panic, or NMI printk) preempting this loop, as it assumes no hardware access is taking place. The emergency writer will then access the same hardware registers concurrently.

Sashiko is correct. I did not know that the serial_lsr_in() macro (used by wait_for_lsr()) updates @lsr_saved_flags! I looked at the other serial_lsr_in() call sites and they are all in nbcon unsafe sections.

The for-loop in serial8250_fifo_wait_for_lsr_thre() should look like this:

	for (i = 0; i < count; i++) {
		/* ... */
		if (wctxt && !nbcon_enter_unsafe(wctxt))
			return;

		if (wait_for_lsr(up, UART_LSR_THRE))
			return;

		if (wctxt)
			nbcon_exit_unsafe(wctxt);
	}

Note that Sashiko already reported [0] this in v7, but I did not look deep enough at that comment, thinking Sashiko was misunderstanding nbcon ownership. Instead it was me who was misunderstanding. :-/

John

[0] https://sashiko.dev/#/patchset/20260720135407.3925-1-john.ogness%40linutronix.de

#3 lore raw

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

Quoted 46 lines · John Ogness
Quoted 38 lines · Sashiko
Quoted 18 lines
drivers/tty/serial/8250/8250_port.c +5 -1
index 8c241ec7f4f29..7726cca00d364 100644
@@ -3198,11 +3218,22 @@ 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 (i = 0; i < count; i++) {
+		/*
+		 * Pass the ownership as quickly as possible to a higher

drivers/tty/serial/8250/8250_port.c void serial8250_set_defaults(struct uart_8250_port *up) · line 3239

Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +7 -0
+		 * priority context. Otherwise, its attempt to take over
+		 * the ownership might timeout. The new owner will wait
+		 * for UART_LSR_THRE before reusing the fifo.
+		 */
+		if (wctxt && !nbcon_can_proceed(wctxt))
+			return;
+
 		if (wait_for_lsr(up, UART_LSR_THRE))
 			return;
 	}

Does this leave the hardware state vulnerable to corruption during an emergency console takeover?

The serial8250_fifo_wait_for_lsr_thre() function polls the UART_LSR register by calling wait_for_lsr() and modifies up->lsr_saved_flags non-atomically. Since reading UART_LSR clears hardware error flags (parity, framing, overrun, break), concurrent reads can cause one CPU to consume and lose the flags before the other can observe them.

Quoted 6 lines

Because serial8250_fifo_wait_for_lsr_thre() is called outside the nbcon_enter_unsafe() block, the nbcon system might grant a safe takeover to an emergency message (e.g. sysrq, panic, or NMI printk) preempting this loop, as it assumes no hardware access is taking place. The emergency writer will then access the same hardware registers concurrently.

Quoted 3 lines

Sashiko is correct. I did not know that the serial_lsr_in() macro (used by wait_for_lsr()) updates @lsr_saved_flags! I looked at the other serial_lsr_in() call sites and they are all in nbcon unsafe sections.

Yeah, I missed this as well. I would not expect that a function called serial_lsr_in() would do a write. But it updates the internal structure....

Quoted 14 lines

The for-loop in serial8250_fifo_wait_for_lsr_thre() should look like this:

	for (i = 0; i < count; i++) {
		/* ... */
		if (wctxt && !nbcon_enter_unsafe(wctxt))
			return;

		if (wait_for_lsr(up, UART_LSR_THRE))
			return;

		if (wctxt)
			nbcon_exit_unsafe(wctxt);
	}

LGTM.

Quoted 3 lines

Note that Sashiko already reported [0] this in v7, but I did not look deep enough at that comment, thinking Sashiko was misunderstanding nbcon ownership. Instead it was me who was misunderstanding. :-/

I was confused as well.

Best Regards,
Petr

#4 lore raw

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

In reply to John Ogness
Quoted 32 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_core.c void serial8250_suspend_port(int line) · line 614

Quoted 9 lines
drivers/tty/serial/8250/8250_core.c +3 -3
@@ -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_irqsave, port)

Strictly speaking, it should be enough to call "uart_port_lock_irq" instead of the "_irqsafe" variant.

This code must be called in a schedulable context. Otherwise, we can't call irq_work_sync() below.

Honestly, I have been a bit lost in the maze of macros and callbacks. And I wanted to be sure where this is called. So, I added WARN_ON(1) and dumped all locks.

And it seems that we are on the safe side. This code is called here:

[   78.676626] CPU: 7 UID: 0 PID: 1542 Comm: bash Kdump: loaded Not tainted 7.2.0-rc4-default+ #24 PREEMPT(full)  252cd4fa4cf2d3bc860af0a8bfee0a3e125ae7f8
[   78.676634] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.17.0-2-g4f253b9b-prebuilt.qemu.org 04/01/2014
[   78.676640] RIP: 0010:serial8250_suspend_port.cold+0x1f/0x1e4
[   78.676648] Code: dc e8 70 f6 02 00 e9 b3 28 df 00 53 48 c7 c7 d0 6d 0d 9f 48 83 ec 20 48 89 54 24 10 4c 89 4c 24 08 4c 89 04 24 e8 2c 1b fb ff <0f> 0b e8 d5 ee fa ff 48 8b 3c 24 e8 6c fa
 49 01 83 3d 79 0c 6b 02
[   78.676653] RSP: 0018:ffffd16d82743c10 EFLAGS: 00010246
[   78.676662] RAX: 0000000000000041 RBX: ffff8d8a00ab3800 RCX: 0000000000000001
[   78.676667] RDX: 0000000000000000 RSI: ffffffff9f1ae0d7 RDI: 00000000ffffffff
[   78.676672] RBP: 0000000000000001 R08: 0000000000000000 R09: 0000000000000001
[   78.676677] R10: 0000000000000007 R11: 0000000000000000 R12: ffffffff9f7f5a80
[   78.676681] R13: 0000000000000001 R14: 0000000000000000 R15: 0000000000000000
[   78.676689] FS:  00007fadbce4e200(0000) GS:ffff8d8ada0a0000(0000) knlGS:0000000000000000
[   78.676695] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   78.676699] CR2: 000055b5982aea10 CR3: 000000010cb6b000 CR4: 0000000000750ef0
[   78.676707] PKRU: 55555554
[   78.676712] Call Trace:
[   78.676718]  <TASK>
[   78.676732]  serial_pnp_suspend+0x11/0x20
[   78.676745]  __pnp_bus_suspend+0x3d/0x100
[   78.676761]  ? __pfx_pnp_bus_freeze+0x10/0x10
[   78.676770]  dpm_run_callback+0x66/0x1f0
[   78.676790]  device_suspend+0x235/0x700
[   78.676798]  ? srso_alias_return_thunk+0x5/0xfbef5
[   78.676807]  ? __lock_release.isra.0+0x1c9/0x300
[   78.676825]  ? __pfx_dpm_watchdog_handler+0x10/0x10
[   78.676851]  ? __mutex_unlock_slowpath+0x1a9/0x410
[   78.676874]  dpm_suspend+0x18f/0x400
[   78.676888]  hibernation_snapshot+0xcb/0x1e0
[   78.676908]  hibernate.cold+0xfd/0x48b
[   78.676925]  state_store+0xc3/0xd0
[   78.676940]  kernfs_fop_write_iter+0x163/0x240
[   78.676960]  vfs_write+0x222/0x560
[   78.676997]  ksys_write+0x70/0xf0
[   78.677012]  do_syscall_64+0xb1/0x6d0
[   78.677021]  ? srso_alias_return_thunk+0x5/0xfbef5
[   78.677036]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[   78.677043] RIP: 0033:0x7fadbc106af3

And the locks are:

[   78.677780] 7 locks held by bash/1542:
[   78.677784]  #0: ffff8d8a038a4470 (sb_writers#5){.+.+}-{0:0}, at: ksys_write+0x70/0xf0
[   78.677820]  #1: ffff8d8a08f44288 (&of->mutex#2){+.+.}-{4:4}, at: kernfs_fop_write_iter+0x105/0x240
[   78.677849]  #2: ffff8d8a00d9a230 (kn->active#105){.+.+}-{0:0}, at: kernfs_fop_write_iter+0x11a/0x240
[   78.677878]  #3: ffffffff9f48c0a8 (system_transition_mutex){+.+.}-{4:4}, at: lock_system_sleep+0x2b/0x40
[   78.677904]  #4: ffffffff9f804968 (device_hotplug_lock){+.+.}-{4:4}, at: hibernate.cold+0xac/0x48b
[   78.677930]  #5: ffff8d8a00ab3938 (&dev->mutex){....}-{4:4}, at: device_suspend+0x1ff/0x700
[   78.677955]  #6: ffffffff9f5ab1a0 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire.constprop.0+0x7/0x30

, where the rcu_read_lock() is from debug_show_all_locks() itself.

The function serial8250_suspend_port() is called under dev->mutex so we are on the safe side.

Quoted 17 lines
drivers/tty/serial/8250/8250_core.c +9 -3
+			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);

drivers/tty/serial/8250/8250_core.c void serial8250_resume_port(int line) · line 657

Quoted 7 lines
drivers/tty/serial/8250/8250_core.c +4 -0
@@ -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_irqsave)(port);

This should be symetrical -> s/irqsave/irq

drivers/tty/serial/8250/8250_core.c void serial8250_resume_port(int line) · line 667

Quoted 9 lines
drivers/tty/serial/8250/8250_core.c +7 -0
+		/* 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);

Plus the problem reported by Sashiko.

Otherwise, it looks good to me.

Best Regards,
Petr

#5 lore raw

PATCH v8 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 7726cca00d364..dfbdaf1fcc79c 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
Reviewed-by on v8 2/2
#6 lore raw

Re: [PATCH tty v8 2/2] Revert "serial: 8250: drop lockdep annotation from serial8250_clear_IER()"

Quoted 6 lines · 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.

Makes sense.

Best Regards,
Petr