Skip to content

Commit 3d7cbd4

Browse files
sudipm-mukherjeegregkh
authored andcommitted
tty: link tty and port before configuring it as console
commit fb2b900 upstream. There seems to be a race condition in tty drivers and I could see on many boot cycles a NULL pointer dereference as tty_init_dev() tries to do 'tty->port->itty = tty' even though tty->port is NULL. 'tty->port' will be set by the driver and if the driver has not yet done it before we open the tty device we can get to this situation. By adding some extra debug prints, I noticed that: 6.650130: uart_add_one_port 6.663849: register_console 6.664846: tty_open 6.674391: tty_init_dev 6.675456: tty_port_link_device uart_add_one_port() registers the console, as soon as it registers, the userspace tries to use it and that leads to tty_open() but uart_add_one_port() has not yet done tty_port_link_device() and so tty->port is not yet configured when control reaches tty_init_dev(). Further look into the code and tty_port_link_device() is done by uart_add_one_port(). After registering the console uart_add_one_port() will call tty_port_register_device_attr_serdev() and tty_port_link_device() is called from this. Call add tty_port_link_device() before uart_configure_port() is done and add a check in tty_port_link_device() so that it only links the port if it has not been done yet. Suggested-by: Jiri Slaby <[email protected]> Signed-off-by: Sudip Mukherjee <[email protected]> Cc: stable <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent a188bd5 commit 3d7cbd4

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

drivers/tty/serial/serial_core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,6 +2810,7 @@ int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
28102810
if (uport->cons && uport->dev)
28112811
of_console_check(uport->dev->of_node, uport->cons->name, uport->line);
28122812

2813+
tty_port_link_device(port, drv->tty_driver, uport->line);
28132814
uart_configure_port(drv, state, uport);
28142815

28152816
port->console = uart_console(uport);

drivers/tty/tty_port.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ void tty_port_link_device(struct tty_port *port,
8888
{
8989
if (WARN_ON(index >= driver->num))
9090
return;
91-
driver->ports[index] = port;
91+
if (!driver->ports[index])
92+
driver->ports[index] = port;
9293
}
9394
EXPORT_SYMBOL_GPL(tty_port_link_device);
9495

0 commit comments

Comments
 (0)