]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-4.19/tty-serial_core-add-install.patch
4.19-stable patches
[thirdparty/kernel/stable-queue.git] / queue-4.19 / tty-serial_core-add-install.patch
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
5
6 From: Jiri Slaby <jslaby@suse.cz>
7
8 commit 4cdd17ba1dff20ffc99fdbd2e6f0201fc7fe67df upstream.
9
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.
13
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
17 recently.
18
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
25 since 2012!
26
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
42 Call Trace:
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
49 ...
50
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.
55
56 So revert the upstream commit here as my local patch should fix the
57 whole family.
58
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>
66
67 ---
68 drivers/tty/serial/serial_core.c | 24 +++++++++++++-----------
69 1 file changed, 13 insertions(+), 11 deletions(-)
70
71 --- a/drivers/tty/serial/serial_core.c
72 +++ b/drivers/tty/serial/serial_core.c
73 @@ -130,9 +130,6 @@ static void uart_start(struct tty_struct
74 struct uart_port *port;
75 unsigned long flags;
76
77 - if (!state)
78 - return;
79 -
80 port = uart_port_lock(state, flags);
81 __uart_start(tty);
82 uart_port_unlock(port, flags);
83 @@ -730,9 +727,6 @@ static void uart_unthrottle(struct tty_s
84 upstat_t mask = UPSTAT_SYNC_FIFO;
85 struct uart_port *port;
86
87 - if (!state)
88 - return;
89 -
90 port = uart_port_ref(state);
91 if (!port)
92 return;
93 @@ -1708,6 +1702,16 @@ static void uart_dtr_rts(struct tty_port
94 uart_port_deref(uport);
95 }
96
97 +static int uart_install(struct tty_driver *driver, struct tty_struct *tty)
98 +{
99 + struct uart_driver *drv = driver->driver_state;
100 + struct uart_state *state = drv->state + tty->index;
101 +
102 + tty->driver_data = state;
103 +
104 + return tty_standard_install(driver, tty);
105 +}
106 +
107 /*
108 * Calls to uart_open are serialised by the tty_lock in
109 * drivers/tty/tty_io.c:tty_open()
110 @@ -1720,11 +1724,8 @@ static void uart_dtr_rts(struct tty_port
111 */
112 static int uart_open(struct tty_struct *tty, struct file *filp)
113 {
114 - struct uart_driver *drv = tty->driver->driver_state;
115 - int retval, line = tty->index;
116 - struct uart_state *state = drv->state + line;
117 -
118 - tty->driver_data = state;
119 + struct uart_state *state = tty->driver_data;
120 + int retval;
121
122 retval = tty_port_open(&state->port, tty, filp);
123 if (retval > 0)
124 @@ -2409,6 +2410,7 @@ static void uart_poll_put_char(struct tt
125 #endif
126
127 static const struct tty_operations uart_ops = {
128 + .install = uart_install,
129 .open = uart_open,
130 .close = uart_close,
131 .write = uart_write,