Skip to content

Commit f743061

Browse files
andy-shevgregkh
authored andcommitted
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 a3cb39d ("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: a3cb39d ("serial: core: Allow detach and attach serial device for console") Reported-by: Marc Zyngier <[email protected]> Reported-by: Lad Prabhakar <[email protected]> Reported-by: Guenter Roeck <[email protected]> Reported-by: Anatoly Pugachev <[email protected]> Acked-by: Marc Zyngier <[email protected]> Tested-by: Tony Lindgren <[email protected]> Cc: Geert Uytterhoeven <[email protected]> Signed-off-by: Andy Shevchenko <[email protected]> Tested-by: Lad Prabhakar <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent d8edf8e commit f743061

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

drivers/tty/serial/serial_core.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,12 @@ static inline bool uart_console_enabled(struct uart_port *port)
19141914
return uart_console(port) && (port->cons->flags & CON_ENABLED);
19151915
}
19161916

1917+
static void __uart_port_spin_lock_init(struct uart_port *port)
1918+
{
1919+
spin_lock_init(&port->lock);
1920+
lockdep_set_class(&port->lock, &port_lock_key);
1921+
}
1922+
19171923
/*
19181924
* Ensure that the serial console lock is initialised early.
19191925
* If this port is a console, then the spinlock is already initialised.
@@ -1923,8 +1929,7 @@ static inline void uart_port_spin_lock_init(struct uart_port *port)
19231929
if (uart_console(port))
19241930
return;
19251931

1926-
spin_lock_init(&port->lock);
1927-
lockdep_set_class(&port->lock, &port_lock_key);
1932+
__uart_port_spin_lock_init(port);
19281933
}
19291934

19301935
#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
@@ -2370,6 +2375,13 @@ uart_configure_port(struct uart_driver *drv, struct uart_state *state,
23702375
/* Power up port for set_mctrl() */
23712376
uart_change_pm(state, UART_PM_STATE_ON);
23722377

2378+
/*
2379+
* If this driver supports console, and it hasn't been
2380+
* successfully registered yet, initialise spin lock for it.
2381+
*/
2382+
if (port->cons && !(port->cons->flags & CON_ENABLED))
2383+
__uart_port_spin_lock_init(port);
2384+
23732385
/*
23742386
* Ensure that the modem control lines are de-activated.
23752387
* keep the DTR setting that is set in uart_set_options()

0 commit comments

Comments
 (0)