Skip to content

Commit 4cdd17b

Browse files
Jiri Slabygregkh
authored andcommitted
TTY: serial_core, add ->install
We need to compute the uart state only on the first open. This is usually what is done in the ->install hook. serial_core used to do this in ->open on every open. So move it to ->install. As a side effect, it ensures the state is set properly in the window after tty_init_dev is called, but before uart_open. This fixes a bunch of races between tty_open and flush_to_ldisc we were dealing with recently. One of such bugs was attempted to fix in commit fedb576 (serial: fix race between flush_to_ldisc and tty_open), but it only took care of a couple of functions (uart_start and uart_unthrottle). I was able to reproduce the crash on a SLE system, but in uart_write_room which is also called from flush_to_ldisc via process_echoes. I was *unable* to reproduce the bug locally. It is due to having this patch in my queue since 2012! general protection fault: 0000 [#1] SMP KASAN PTI CPU: 1 PID: 5 Comm: kworker/u4:0 Tainted: G L 4.12.14-396-default #1 SLE15-SP1 (unreleased) Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.12.0-0-ga698c89-prebuilt.qemu.org 04/01/2014 Workqueue: events_unbound flush_to_ldisc task: ffff8800427d8040 task.stack: ffff8800427f0000 RIP: 0010:uart_write_room+0xc4/0x590 RSP: 0018:ffff8800427f7088 EFLAGS: 00010202 RAX: dffffc0000000000 RBX: 0000000000000000 RCX: 0000000000000000 RDX: 000000000000002f RSI: 00000000000000ee RDI: ffff88003888bd90 RBP: ffffffffb9545850 R08: 0000000000000001 R09: 0000000000000400 R10: ffff8800427d825c R11: 000000000000006e R12: 1ffff100084fee12 R13: ffffc900004c5000 R14: ffff88003888bb28 R15: 0000000000000178 FS: 0000000000000000(0000) GS:ffff880043300000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000561da0794148 CR3: 000000000ebf4000 CR4: 00000000000006e0 Call Trace: tty_write_room+0x6d/0xc0 __process_echoes+0x55/0x870 n_tty_receive_buf_common+0x105e/0x26d0 tty_ldisc_receive_buf+0xb7/0x1c0 tty_port_default_receive_buf+0x107/0x180 flush_to_ldisc+0x35d/0x5c0 ... 0 in rbx means tty->driver_data is NULL in uart_write_room. 0x178 is tried to be dereferenced (0x178 >> 3 is 0x2f in rdx) at uart_write_room+0xc4. 0x178 is exactly (struct uart_state *)NULL->refcount used in uart_port_lock from uart_write_room. So revert the upstream commit here as my local patch should fix the whole family. Signed-off-by: Jiri Slaby <jslaby@suse.cz> Cc: Li RongQing <lirongqing@baidu.com> Cc: Wang Li <wangli39@baidu.com> Cc: Zhang Yu <zhangyu31@baidu.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: stable <stable@vger.kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6bc3703 commit 4cdd17b

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

drivers/tty/serial/serial_core.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,6 @@ static void uart_start(struct tty_struct *tty)
130130
struct uart_port *port;
131131
unsigned long flags;
132132

133-
if (!state)
134-
return;
135-
136133
port = uart_port_lock(state, flags);
137134
__uart_start(tty);
138135
uart_port_unlock(port, flags);
@@ -730,9 +727,6 @@ static void uart_unthrottle(struct tty_struct *tty)
730727
upstat_t mask = UPSTAT_SYNC_FIFO;
731728
struct uart_port *port;
732729

733-
if (!state)
734-
return;
735-
736730
port = uart_port_ref(state);
737731
if (!port)
738732
return;
@@ -1747,6 +1741,16 @@ static void uart_dtr_rts(struct tty_port *port, int raise)
17471741
uart_port_deref(uport);
17481742
}
17491743

1744+
static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
1745+
{
1746+
struct uart_driver *drv = driver->driver_state;
1747+
struct uart_state *state = drv->state + tty->index;
1748+
1749+
tty->driver_data = state;
1750+
1751+
return tty_standard_install(driver, tty);
1752+
}
1753+
17501754
/*
17511755
* Calls to uart_open are serialised by the tty_lock in
17521756
* drivers/tty/tty_io.c:tty_open()
@@ -1759,11 +1763,8 @@ static void uart_dtr_rts(struct tty_port *port, int raise)
17591763
*/
17601764
static int uart_open(struct tty_struct *tty, struct file *filp)
17611765
{
1762-
struct uart_driver *drv = tty->driver->driver_state;
1763-
int retval, line = tty->index;
1764-
struct uart_state *state = drv->state + line;
1765-
1766-
tty->driver_data = state;
1766+
struct uart_state *state = tty->driver_data;
1767+
int retval;
17671768

17681769
retval = tty_port_open(&state->port, tty, filp);
17691770
if (retval > 0)
@@ -2448,6 +2449,7 @@ static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)
24482449
#endif
24492450

24502451
static const struct tty_operations uart_ops = {
2452+
.install = uart_install,
24512453
.open = uart_open,
24522454
.close = uart_close,
24532455
.write = uart_write,

0 commit comments

Comments
 (0)