Skip to contentSkip to conversation

serial: core: Initialise spin lock before use in uart_configure_port()

PATCH Superseded Andy Shevchenko 3 participants 4 messages Jul 6, 2020 TTY layer and serial drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 serial: core: Initialise spin lock before use in uart_configure_port()

The comment near to uart_port_spin_lock_init() says:

Ensure that the serial console lock is initialised early. If this port is a console, then the spinlock is already initialised.

and there is nothing about enabled or disabled consoles. The commit a3cb39d258ef ("serial: core: Allow detach and attach serial device for console") made a change, which follows the comment, and also to prevent reinitialisation of the lock in use, when user detaches and attaches back the same console device. But this change discovers another issue, that uart_add_one_port() tries to access a spin lock that now may be uninitialised. This happens when a driver expects the serial core to register a console on its behalf. In this case we must initialise a spin lock before use.

Fixes a3cb39d258ef ("serial: core: Allow detach and attach serial device for console")
Cc Tony Lindgren <[email protected]>
Cc Geert Uytterhoeven <[email protected]>

---
v2: my bad to send the version with one typo, now squashed and resent

I hope this is now properly root caused. To the people in Reported-by list, can you revert the corresponding change you either reported or submitted to the certain serial driver and apply this patch and retest?

Tony, can you also test that this doesn't change anything for the PM case for OMAP?

Geert, I tried to explain above why the change had been made in the first place.

 drivers/tty/serial/serial_core.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/tty/serial/serial_core.c +14 -2
index 3cc183acf7ba..55f9615f0b50 100644
@@ -1915,6 +1915,12 @@ static inline bool uart_console_enabled(struct uart_port *port)
 	return uart_console(port) && (port->cons->flags & CON_ENABLED);
 }
 
+static inline void uart_any_port_spin_lock_init(struct uart_port *port)
+{
+	spin_lock_init(&port->lock);
+	lockdep_set_class(&port->lock, &port_lock_key);
+}
+
 /*
  * Ensure that the serial console lock is initialised early.
  * If this port is a console, then the spinlock is already initialised.
@@ -1924,8 +1930,7 @@ static inline void uart_port_spin_lock_init(struct uart_port *port)
 	if (uart_console(port))
 		return;
 
-	spin_lock_init(&port->lock);
-	lockdep_set_class(&port->lock, &port_lock_key);
+	uart_any_port_spin_lock_init(port);
 }
 
 #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
@@ -2371,6 +2376,13 @@ uart_configure_port(struct uart_driver *drv, struct uart_state *state,
 		/* Power up port for set_mctrl() */
 		uart_change_pm(state, UART_PM_STATE_ON);
 
+		/*
+		 * If this driver supports console, and it hasn't been
+		 * successfully registered yet, initialise spin lock for it.
+		 */
+		if (port->cons && !(port->cons->flags & CON_ENABLED))
+			uart_any_port_spin_lock_init(port);
+
 		/*
 		 * Ensure that the modem control lines are de-activated.
 		 * keep the DTR setting that is set in uart_set_options()
Signature · 1 line
2.27.0
Acked-by on v2 1/1
#1 lore raw
Quoted 44 lines · Andy Shevchenko

The comment near to uart_port_spin_lock_init() says:

Ensure that the serial console lock is initialised early. If this port is a console, then the spinlock is already initialised.

and there is nothing about enabled or disabled consoles. The commit a3cb39d258ef ("serial: core: Allow detach and attach serial device for console") made a change, which follows the comment, and also to prevent reinitialisation of the lock in use, when user detaches and attaches back the same console device. But this change discovers another issue, that uart_add_one_port() tries to access a spin lock that now may be uninitialised. This happens when a driver expects the serial core to register a console on its behalf. In this case we must initialise a spin lock before use.

Fixes a3cb39d258ef ("serial: core: Allow detach and attach serial

device for console")

---
v2: my bad to send the version with one typo, now squashed and resent

I hope this is now properly root caused. To the people in Reported-by list, can you revert the corresponding change you either reported or submitted to the certain serial driver and apply this patch and retest?

Tony, can you also test that this doesn't change anything for the PM case for OMAP?

Geert, I tried to explain above why the change had been made in the first place.

 drivers/tty/serial/serial_core.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/tty/serial/serial_core.c +0 -0
index 3cc183acf7ba..55f9615f0b50 100644

drivers/tty/serial/serial_core.c static inline bool uart_console_enabled(struct · line 1920

Quoted 6 lines
drivers/tty/serial/serial_core.c +1 -0
@@ -1915,6 +1915,12 @@ static inline bool uart_console_enabled(struct
 uart_port *port)
 	return uart_console(port) && (port->cons->flags & CON_ENABLED);
 }
 
+static inline void uart_any_port_spin_lock_init(struct uart_port 
 *port)

nit: __uart_port_spin_lock_init() looks like a better name to me (as a primitive of uart_port_spin_lock_init). You can also drop the inline which doesn't mean much these days (and this isn't a hot path).

Quoted 35 lines
drivers/tty/serial/serial_core.c +5 -0
+{
+	spin_lock_init(&port->lock);
+	lockdep_set_class(&port->lock, &port_lock_key);
+}
+
 /*

* Ensure that the serial console lock is initialised early.
* If this port is a console, then the spinlock is already
initialised.

drivers/tty/serial/serial_core.c +1 -2
@@ -1924,8 +1930,7 @@ static inline void
 uart_port_spin_lock_init(struct uart_port *port)
 	if (uart_console(port))
 		return;
 
-	spin_lock_init(&port->lock);
-	lockdep_set_class(&port->lock, &port_lock_key);
+	uart_any_port_spin_lock_init(port);
 }
 

#if defined(CONFIG_SERIAL_CORE_CONSOLE) ||
defined(CONFIG_CONSOLE_POLL)

drivers/tty/serial/serial_core.c +7 -0
@@ -2371,6 +2376,13 @@ uart_configure_port(struct uart_driver *drv,
 struct uart_state *state,
 		/* Power up port for set_mctrl() */
 		uart_change_pm(state, UART_PM_STATE_ON);
 
+		/*
+		 * If this driver supports console, and it hasn't been
+		 * successfully registered yet, initialise spin lock for it.
+		 */
+		if (port->cons && !(port->cons->flags & CON_ENABLED))
+			uart_any_port_spin_lock_init(port);
+
 		/*
 		 * Ensure that the modem control lines are de-activated.
Quoted 1 line

* keep the DTR setting that is set in uart_set_options()

Otherwise looks OK to me (having tested an earlier version). With the above addressed:

        M.
Signature · 1 line
Jazz is not dead. It just smells funny...
#2 lore raw
Quoted 15 lines · Marc Zyngier
Quoted 14 lines · Andy Shevchenko

The comment near to uart_port_spin_lock_init() says:

Ensure that the serial console lock is initialised early. If this port is a console, then the spinlock is already initialised.

and there is nothing about enabled or disabled consoles. The commit a3cb39d258ef ("serial: core: Allow detach and attach serial device for console") made a change, which follows the comment, and also to prevent reinitialisation of the lock in use, when user detaches and attaches back the same console device. But this change discovers another issue, that uart_add_one_port() tries to access a spin lock that now may be uninitialised. This happens when a driver expects the serial core to register a console on its behalf. In this case we must initialise a spin lock before use.

...

Quoted 6 lines
Quoted 1 line

+static inline void uart_any_port_spin_lock_init(struct uart_port *port)

nit: __uart_port_spin_lock_init() looks like a better name to me (as a primitive of uart_port_spin_lock_init). You can also drop the inline which doesn't mean much these days (and this isn't a hot path).

Actually I was thinking about that name, but decided to go without underscores (b/c some developers like this approach, some like that one). I'm fine with your proposal nevertheless.

Quoted 14 lines

drivers/tty/serial/serial_core.c uart_configure_port(struct uart_driver *drv, struct uart_state *state, · line 2388

Quoted 10 lines
drivers/tty/serial/serial_core.c +7 -0
+		/*
+		 * If this driver supports console, and it hasn't been
+		 * successfully registered yet, initialise spin lock for it.
+		 */
+		if (port->cons && !(port->cons->flags & CON_ENABLED))
+			uart_any_port_spin_lock_init(port);
+
 		/*
 		 * Ensure that the modem control lines are de-activated.
 		 * keep the DTR setting that is set in uart_set_options()

Otherwise looks OK to me (having tested an earlier version). With the above addressed:

Quoted 1 line

Thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko