1 From 4cdd17ba1dff20ffc99fdbd2e6f0201fc7fe67df Mon Sep 17 00:00:00 2001
2 From: Jiri Slaby <jslaby@suse.cz>
3 Date: Wed, 17 Apr 2019 10:58:53 +0200
4 Subject: TTY: serial_core, add ->install
6 From: Jiri Slaby <jslaby@suse.cz>
8 commit 4cdd17ba1dff20ffc99fdbd2e6f0201fc7fe67df upstream.
10 We need to compute the uart state only on the first open. This is
11 usually what is done in the ->install hook. serial_core used to do this
12 in ->open on every open. So move it to ->install.
14 As a side effect, it ensures the state is set properly in the window
15 after tty_init_dev is called, but before uart_open. This fixes a bunch
16 of races between tty_open and flush_to_ldisc we were dealing with
19 One of such bugs was attempted to fix in commit fedb5760648a (serial:
20 fix race between flush_to_ldisc and tty_open), but it only took care of
21 a couple of functions (uart_start and uart_unthrottle). I was able to
22 reproduce the crash on a SLE system, but in uart_write_room which is
23 also called from flush_to_ldisc via process_echoes. I was *unable* to
24 reproduce the bug locally. It is due to having this patch in my queue
27 general protection fault: 0000 [#1] SMP KASAN PTI
28 CPU: 1 PID: 5 Comm: kworker/u4:0 Tainted: G L 4.12.14-396-default #1 SLE15-SP1 (unreleased)
29 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.12.0-0-ga698c89-prebuilt.qemu.org 04/01/2014
30 Workqueue: events_unbound flush_to_ldisc
31 task: ffff8800427d8040 task.stack: ffff8800427f0000
32 RIP: 0010:uart_write_room+0xc4/0x590
33 RSP: 0018:ffff8800427f7088 EFLAGS: 00010202
34 RAX: dffffc0000000000 RBX: 0000000000000000 RCX: 0000000000000000
35 RDX: 000000000000002f RSI: 00000000000000ee RDI: ffff88003888bd90
36 RBP: ffffffffb9545850 R08: 0000000000000001 R09: 0000000000000400
37 R10: ffff8800427d825c R11: 000000000000006e R12: 1ffff100084fee12
38 R13: ffffc900004c5000 R14: ffff88003888bb28 R15: 0000000000000178
39 FS: 0000000000000000(0000) GS:ffff880043300000(0000) knlGS:0000000000000000
40 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
41 CR2: 0000561da0794148 CR3: 000000000ebf4000 CR4: 00000000000006e0
43 tty_write_room+0x6d/0xc0
44 __process_echoes+0x55/0x870
45 n_tty_receive_buf_common+0x105e/0x26d0
46 tty_ldisc_receive_buf+0xb7/0x1c0
47 tty_port_default_receive_buf+0x107/0x180
48 flush_to_ldisc+0x35d/0x5c0
51 0 in rbx means tty->driver_data is NULL in uart_write_room. 0x178 is
52 tried to be dereferenced (0x178 >> 3 is 0x2f in rdx) at
53 uart_write_room+0xc4. 0x178 is exactly (struct uart_state *)NULL->refcount
54 used in uart_port_lock from uart_write_room.
56 So revert the upstream commit here as my local patch should fix the
59 Signed-off-by: Jiri Slaby <jslaby@suse.cz>
60 Cc: Li RongQing <lirongqing@baidu.com>
61 Cc: Wang Li <wangli39@baidu.com>
62 Cc: Zhang Yu <zhangyu31@baidu.com>
63 Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
64 Cc: stable <stable@vger.kernel.org>
65 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
68 drivers/tty/serial/serial_core.c | 24 +++++++++++++-----------
69 1 file changed, 13 insertions(+), 11 deletions(-)
71 --- a/drivers/tty/serial/serial_core.c
72 +++ b/drivers/tty/serial/serial_core.c
73 @@ -141,9 +141,6 @@ static void uart_start(struct tty_struct
74 struct uart_port *port;
80 port = uart_port_lock(state, flags);
82 uart_port_unlock(port, flags);
83 @@ -1714,11 +1711,8 @@ static void uart_dtr_rts(struct tty_port
85 static int uart_open(struct tty_struct *tty, struct file *filp)
87 - struct uart_driver *drv = tty->driver->driver_state;
88 - int retval, line = tty->index;
89 - struct uart_state *state = drv->state + line;
91 - tty->driver_data = state;
92 + struct uart_state *state = tty->driver_data;
95 retval = tty_port_open(&state->port, tty, filp);
97 @@ -2409,9 +2403,6 @@ static void uart_poll_put_char(struct tt
98 struct uart_state *state = drv->state + line;
99 struct uart_port *port;
104 port = uart_port_ref(state);
107 @@ -2423,7 +2414,18 @@ static void uart_poll_put_char(struct tt
111 +static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
113 + struct uart_driver *drv = driver->driver_state;
114 + struct uart_state *state = drv->state + tty->index;
116 + tty->driver_data = state;
118 + return tty_standard_install(driver, tty);
121 static const struct tty_operations uart_ops = {
122 + .install = uart_install,