Skip to contentSkip to conversation

Convert 8250 to NBCON, take 2

PATCH Superseded John Ogness 3 participants 7 messages Jul 20 – Jul 21

Subsystems (4)

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v7 cover Convert 8250 to NBCON, take 2

Hi,

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

Based on Sashiko feedback, the changes since v6:

- 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
serial: 8250: Revert "drop lockdep annotation from

    serial8250_clear_IER()"
 drivers/tty/serial/8250/8250.h      |   4 +-
 drivers/tty/serial/8250/8250_core.c |  45 ++++++-
 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, 222 insertions(+), 45 deletions(-)

base-commit: 782f4dbd1794b4f30dc116a7ca42c5962c409be8

Signature · 1 line
2.47.3
#1 lore raw

PATCH v7 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 may not be holding the port lock for atomic printing). 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 |  45 ++++++-
 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, 219 insertions(+), 45 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 +40 -5

76 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..12c2f6aadd420 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;
+
+	/* Do not queue irq_work for MSR handling when suspending. */
+	bool			avoid_modem_status_work;
+
 #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
 	unsigned char		msr_saved_flags;
+	struct irq_work		modem_status_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 v7 1/2] serial: 8250: Switch to nbcon console, take 2

Quoted 27 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 may not be holding the port lock for atomic printing). This allows restoring a lockdep check to serial8250_clear_IER() in a follow-up commit.

...

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

Quoted 3 lines
drivers/tty/serial/8250/8250_core.c +3 -0
+	/* irq_work allowed again. Handle MSR now if pending. */
+	up->avoid_modem_status_work = false;
+	guard(uart_port_lock_irqsave)(port);

To make guard()() visible, we usually add a blank line before and after guard()() line. I dunno if scoped_guard() makes more sense here as Petr proposed, I'm fine with either.

Quoted 2 lines
drivers/tty/serial/8250/8250_core.c +2 -0
+	if (uart_console(port) && up->msr_saved_flags)
+		serial8250_modem_status(up);

...

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

Quoted 13 lines
drivers/tty/serial/8250/8250_port.c +13 -0
+static void serial8250_console_byte_write(struct uart_8250_port *up,
+					  struct nbcon_write_context *wctxt)
+{
+	struct uart_port *port = &up->port;
+	const char *s = wctxt->outbuf;
+	const char *end = s + wctxt->len;
+
+	/*
+	 * Write out the message. If a handover or takeover occurs, writing
+	 * must be aborted since wctxt->outbuf and wctxt->len are no longer
+	 * valid.
+	 */
+	while (s != end) {

Can len == 0? If not, I would write this as do {} while ().

Quoted 8 lines
drivers/tty/serial/8250/8250_port.c +7 -0
+		if (!nbcon_enter_unsafe(wctxt))
+			return;
+
+		uart_console_write(port, s++, 1, serial8250_console_wait_putchar);
+
+		nbcon_exit_unsafe(wctxt);
+	}
 }

...

drivers/tty/serial/8250/8250_port.c static unsigned int probe_baud(struct uart_port *port) · line 3516

Quoted 11 lines
drivers/tty/serial/8250/8250_port.c +11 -0
+/*
+ * irq_work handler to perform modem control. Only triggered via
+ * ->write_atomic() callback because it may be in a scheduler or
+ * NMI context, unable to wake tasks.
+ */
+static void modem_status_handler(struct irq_work *iwp)
+{
+	struct uart_8250_port *up = container_of(iwp, struct uart_8250_port, modem_status_work);
+	struct uart_port *port = &up->port;
+
+	guard(uart_port_lock)(port);

+ blank line.

Quoted 2 lines
(quoted diff) +2 -0
+	serial8250_modem_status(up);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

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

Quoted 7 lines · Andy Shevchenko

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

Quoted 3 lines
drivers/tty/serial/8250/8250_core.c +3 -0
+	/* irq_work allowed again. Handle MSR now if pending. */
+	up->avoid_modem_status_work = false;
+	guard(uart_port_lock_irqsave)(port);

To make guard()() visible, we usually add a blank line before and after guard()() line. I dunno if scoped_guard() makes more sense here as Petr proposed, I'm fine with either.

Ack.

Quoted 7 lines
Quoted 7 lines
drivers/tty/serial/8250/8250_port.c +7 -0
+static void serial8250_console_byte_write(struct uart_8250_port *up,
+					  struct nbcon_write_context *wctxt)
+{
+	struct uart_port *port = &up->port;
+	const char *s = wctxt->outbuf;
+	const char *end = s + wctxt->len;
+
Quoted 8 lines

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

Quoted 6 lines
drivers/tty/serial/8250/8250_port.c +6 -0
+	/*
+	 * Write out the message. If a handover or takeover occurs, writing
+	 * must be aborted since wctxt->outbuf and wctxt->len are no longer
+	 * valid.
+	 */
+	while (s != end) {

Can len == 0? If not, I would write this as do {} while ().

printk should never make such requests (len=0 means it is a skipped record) but the driver should not rely on that. Also, this pattern is the same used by serial8250_console_fifo_write()

Quoted 5 lines
Quoted 5 lines

+/*
+ * irq_work handler to perform modem control. Only triggered via
+ * ->write_atomic() callback because it may be in a scheduler or
+ * NMI context, unable to wake tasks.
+ */

Quoted 8 lines

drivers/tty/serial/8250/8250_port.c static unsigned int probe_baud(struct uart_port *port) · line 3516

Quoted 6 lines
drivers/tty/serial/8250/8250_port.c +6 -0
+static void modem_status_handler(struct irq_work *iwp)
+{
+	struct uart_8250_port *up = container_of(iwp, struct uart_8250_port, modem_status_work);
+	struct uart_port *port = &up->port;
+
+	guard(uart_port_lock)(port);

+ blank line.

Ack.

John

#4 lore raw

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

In reply to John Ogness

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

This reverts commit 3d9e6f556e235ddcdc9f73600fdd46fe1736b090.

---
 drivers/tty/serial/8250/8250_port.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/tty/serial/8250/8250_port.c +3 -0
index 8753f86d633ef..31386dd283ef8 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
#5 lore raw

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

Quoted 4 lines · John Ogness

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

This reverts commit 3d9e6f556e235ddcdc9f73600fdd46fe1736b090.

If needed, could this be please replaced with
3d9e6f556e235 ("serial: 8250: drop lockdep annotation from serial8250_clear_IER()")

instead the bare number, please?

Quoted 1 line

Sebastian

#6 lore raw

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

Quoted 10 lines · Sebastian Andrzej Siewior
Quoted 4 lines · John Ogness

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

This reverts commit 3d9e6f556e235ddcdc9f73600fdd46fe1736b090.

If needed, could this be please replaced with
3d9e6f556e235 ("serial: 8250: drop lockdep annotation from serial8250_clear_IER()")

instead the bare number, please?

IIRC this is a requirement for stable that they will able to find the reverts without having a Fixes tags.

On top of that we may add the standard format into the commit message.

Signature · 2 lines
With Best Regards,
Andy Shevchenko