Convert 8250 to NBCON, take 2
Subsystems (5)
v9 cover Convert 8250 to NBCON, take 2
Hi,
This is v9 of a series to convert the 8250 driver to an NBCON console, providing both threaded and atomic printing implementations. v8 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 v8:
- In serial8250_suspend_port() and serial8250_resume_port(), acquire the port->lock using the _irq() variant instead of _irqsave() since these callbacks are always invoked from task context
- In serial8250_fifo_wait_for_lsr_thre(), enter and exit an unsafe section for each wait_for_lsr() in the loop since wait_for_lsr() uses serial_lsr_in(), which performs RMW updates to uart_8250_port->lsr_saved_flags. Without an unsafe section, a context could race with another higher priority context during the RMW update.
- Change single-sentence comments to not have a trailing period
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 | 204 +++++++++++++++++++++++----- include/linux/serial_8250.h | 16 ++- 5 files changed, 240 insertions(+), 51 deletions(-)
base-commit: 782f4dbd1794b4f30dc116a7ca42c5962c409be8
Signature · 1 line
2.47.3
PATCH v9 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 | 201 +++++++++++++++++++++++----- include/linux/serial_8250.h | 16 ++- 5 files changed, 237 insertions(+), 51 deletions(-)
drivers/tty/serial/8250/8250.h +3 -1
@@ -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
@@ -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 +165 -36
335 lines not on this page · raw message
include/linux/serial_8250.h +14 -2
@@ -150,8 +150,20 @@ struct uart_8250_port {#define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITSu16 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_DELTAunsigned 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
Re: [PATCH tty v9 1/2] serial: 8250: Switch to nbcon console, take 2
Quoted 8 lines · Sashiko
Quoted 8 lines
drivers/tty/serial/8250/8250_core.c +0
-0@@ -584,13 +609,19 @@ void serial8250_suspend_port(int line)struct uart_8250_port *up = &serial8250_ports[line];struct uart_port *port = &up->port;
Quoted 11 lines
drivers/tty/serial/8250/8250_core.c void serial8250_suspend_port(int line) · line 616
Quoted 8 lines
drivers/tty/serial/8250/8250_core.c +5
-3-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);Does clearing
up->console_msr_work_allowunderuart_port_lock_irqproperly serialize againstserial8250_console_write()?
Yes.
Quoted 5 lines
Since
serial8250_console_write()performs atomic writes without taking the port lock, could a delayed execution checkup->console_msr_work_allowlocklessly, get preempted or delayed, and thenserial8250_suspend_port()clear the flag and callirq_work_sync()before the delayed thread resumes?
No. uart_port_lock_irq() also acquires nbcon ownership. That is what synchronizes against serial8250_console_write() when atomic printing.
Quoted 32 lines
Quoted 18 lines
drivers/tty/serial/8250/8250_port.c +6
-1@@ -3198,13 +3218,28 @@ 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 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 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 (wctxt && !nbcon_enter_unsafe (wctxt)) +return ; +if (wait_for_lsr (up,UART_LSR_THRE ))return ;Does this early return on success bypass the cleanup of the unsafe section?
Quoted 6 lines
If
wait_for_lsr()returns true, wouldn'tserial8250_fifo_wait_for_lsr_thre()return immediately without callingnbcon_exit_unsafe(wctxt)?Could this resource leak of the nbcon unsafe state prevent other contexts from taking over ownership?
It simply has the effect of joining this unsafe section with the next one. In this case it really wouldn't matter. But still, it was not my intention. I will fix this for v10.
How I plan for it to look for v10:
for (i = 0; i < count; i++) {
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;
}I am wondering if we should add a WARN_ON_ONCE() to nbcon_enter_unsafe() if the context is already unsafe. It is a simple one-liner. That would at least catch these kinds of mistakes during runtime. (I would do that in a separate printk patch, not in this series.)
Quoted 22 lines
Quoted 9 lines
drivers/tty/serial/8250/8250_port.c +3
-2@@ -3378,11 +3469,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) { +/* drivers/tty/serial/8250/8250_port.c void serial8250_console_write(struct uart_8250_port *up, const char *s, · line 3484
Quoted 10 lines
drivers/tty/serial/8250/8250_port.c +10
-0+* 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);This is where the lockless read of
up->console_msr_work_allowoccurs.
Quoted 3 lines
If execution is delayed immediately after checking this flag, could the port be suspended and
irq_work_sync()finish beforeirq_work_queue()is called here?
No. Both the reader and writer must first aquire exclusive nbcon ownership. Also, WRT @console_msr_work_allow, both reader and writer are even in unsafe sections and so cannot lose ownership except for on a hostile takeover during the final unsafe flush of a panic.
John
Re: [PATCH tty v9 1/2] serial: 8250: Switch to nbcon console, take 2
Quoted 61 lines · John Ogness
Quoted 32 lines · Sashiko
Quoted 18 lines
drivers/tty/serial/8250/8250_port.c +6
-1@@ -3198,13 +3218,28 @@ 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 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 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 (wctxt && !nbcon_enter_unsafe (wctxt)) +return ; +if (wait_for_lsr (up,UART_LSR_THRE ))return ;Does this early return on success bypass the cleanup of the unsafe section?
Quoted 6 lines
If
wait_for_lsr()returns true, wouldn'tserial8250_fifo_wait_for_lsr_thre()return immediately without callingnbcon_exit_unsafe(wctxt)?Could this resource leak of the nbcon unsafe state prevent other contexts from taking over ownership?
It simply has the effect of joining this unsafe section with the next one. In this case it really wouldn't matter. But still, it was not my intention. I will fix this for v10.
How I plan for it to look for v10:
for (i = 0; i < count; i++) { 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; }
Quoted 4 lines
I am wondering if we should add a
WARN_ON_ONCE()tonbcon_enter_unsafe()if the context is already unsafe. It is a simple one-liner. That would at least catch these kinds of mistakes during runtime. (I would do that in a separate printk patch, not in this series.)
Yup, makes sense.
Best Regards,
Petr
Jon Hunter
maintainerRe: [PATCH tty v9 1/2] serial: 8250: Switch to nbcon console, take 2
In reply to John OgnessHi 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_NBCONto 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_workis used to defer modem control since it may be called from a context that does not allow waking up tasks. During suspend/resume theirq_workis 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 ofUART_IERduring console writing (which will not be holding the port lock for atomic printing or KDB/KGDB). This allows restoring a lockdep check toserial8250_clear_IER()in a follow-up commit.Signed-off-by John Ogness <[email protected]>
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 the only interesting thing I have observed is that if I add 'keep_bootcon' to the command line the board does boot.
Jon
Signature · 1 line
nvpublic
Andy Shevchenko
reviewerRe: [PATCH tty v9 1/2] serial: 8250: Switch to nbcon console, take 2
Quoted 30 lines · Jon Hunter
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_NBCONto 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_workis used to defer modem control since it may be called from a context that does not allow waking up tasks. During suspend/resume theirq_workis not used as this has been shown to cause suspend problems for some hardware. Upon resume, any pending modem control is performed.Quoted 5 lines
Note: A new
__serial8250_clear_IER()is introduced for direct clearing ofUART_IERduring console writing (which will not be holding the port lock for atomic printing or KDB/KGDB). This allows restoring a lockdep check toserial8250_clear_IER()in a follow-up commit.
Quoted 3 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.
Wasn't v11 applied and not v9?
Quoted 3 lines
So far the only
interesting thing I have observed is that if I add 'keep_bootcon' to the command line the board does boot.
keep_bootcon defers driver taking over, that's why it works, it uses just simple primitives instead of full-featured driver.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jon Hunter
maintainerRe: [PATCH tty v9 1/2] serial: 8250: Switch to nbcon console, take 2
Quoted 31 lines · Andy Shevchenko
Quoted 30 lines · Jon Hunter
Quoted 28 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_NBCONto 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_workis used to defer modem control since it may be called from a context that does not allow waking up tasks. During suspend/resume theirq_workis 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 ofUART_IERduring console writing (which will not be holding the port lock for atomic printing or KDB/KGDB). This allows restoring a lockdep check toserial8250_clear_IER()in a follow-up commit.
Quoted 5 lines
Quoted 3 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.
Wasn't v11 applied and not v9?
Ah may be I replied to the wrong version. However, I am testing next-20260817. So whatever is in -next.
Quoted 6 lines
Quoted 3 lines
So far the only
interesting thing I have observed is that if I add 'keep_bootcon' to the command line the board does boot.
keep_bootcondefers driver taking over, that's why it works, it uses just simple primitives instead of full-featured driver.
Yes that makes sense then.
Jon
Signature · 1 line
nvpublic
Re: [PATCH tty v9 1/2] serial: 8250: Switch to nbcon console, take 2
In reply to Andy ShevchenkoQuoted 35 lines · Andy Shevchenko
Quoted 30 lines · Jon Hunter
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_NBCONto 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_workis used to defer modem control since it may be called from a context that does not allow waking up tasks. During suspend/resume theirq_workis not used as this has been shown to cause suspend problems for some hardware. Upon resume, any pending modem control is performed.Quoted 5 lines
Note: A new
__serial8250_clear_IER()is introduced for direct clearing ofUART_IERduring console writing (which will not be holding the port lock for atomic printing or KDB/KGDB). This allows restoring a lockdep check toserial8250_clear_IER()in a follow-up commit.Quoted 3 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.
Quoted 8 lines
Wasn't v11 applied and not v9?
Quoted 3 lines
So far the only
interesting thing I have observed is that if I add 'keep_bootcon' to the command line the board does boot.
keep_bootcondefers driver taking over, that's why it works, it uses just simple primitives instead of full-featured driver.
It is the other way. The full-featured driver is used even with keep_bootcon. The option causes that the simple boot driver stays registered in parallel. You probably see duplicated lines because of this.
Anyway, the most important effect of "keep_bootcon" is that boot consoles prevent using the printk kthreads. Even the nbcon consoles need to be handled in the legacy loop.
There reason is a combination of two things:
1. Boot consoles are synchronized only using the legacy console_lock (console_sem). port->lock does not exist at boot time.
2. There is no simple way how to match boot console drivers with the full-featured drivers. They use a different interface to access the same HW.
As a result, even the full featured console drivers have to rely on the legacy console_lock as long as any boot console is used.
The regression seems to be when the uart 8250 port become handled by the printk kthread.
Best Regards,
Petr
PS: I am not sure whether to continue the discussion here.
It might be better to move it to v11. see
https://lore.kernel.org/all/[email protected]/
I am going to reply there as well.
PATCH v9 2/2 Revert "serial: 8250: drop lockdep annotation from serial8250_clear_IER()"
In reply to John OgnessThis 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
@@ -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