Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3983777yba; Wed, 17 Apr 2019 02:00:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqxTEe3FQUKsrt+Xa/dYwAZJDkdLylWjRWtME3WN6E6+FidHdDnVoEScR/Tp6X0lB/AMWOlM X-Received: by 2002:a63:1d5b:: with SMTP id d27mr79681830pgm.386.1555491601920; Wed, 17 Apr 2019 02:00:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555491601; cv=none; d=google.com; s=arc-20160816; b=vF4bKecIzNwqE7ixCLg89F+WCbG/KleDu4Pny/iw8gq5cuUMxK7yU32l1i0EPsxsLi Ix3S5CR7sQ1kQcxNR+Fyc+nO9/UpbjKMDKFKgqnYtSmxwKwnEPl/cL/KAlZ/nLgKacum C1IJ1RvbFdvP3tmrs8OhCYwgzb7GIts1QACtBNfDCWAvSjZdFK4qEvEUR5MQrPJU0j+5 3iiysWFj/xTDn2cYkHtTxKH3oFORhtVbHXNtUrMxwogxRhKISf85WSqw9j0pgoadOdwN hGX8YTE3PFVQXUyDTgM1PQpgfOAlIs3g81CqHdgtNJRnRaEpCYmw6ZyJcdZpXpHMbx2o jvZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=D0Q3ke4MknNEDXqf1OEz30zKVd99ZUjd/jJAxAW2c1k=; b=V2bz49kUArPxxX9sZO2yKWpmkbHiSj7EZQ6no/aRmLVSOzPSb/qwNyoJzrLD+1+kQj KYMRRJJ0UjaRRBGUj5g5Eo/UBL80F8y4CCKVI6pMKB80DGnadcWoYD66ytW2r8X26kZ7 Nbs0U1ji6N8F34J2VVGO4V9+zzwUdvo+UuneeUrhZPleHabfBg1gFsKbAP9Y+lHay6Rg JnyD03K/vuSYcfjOlJH9HlzhZr7y5wPTlaUFfR+Mn+7+NHb8KjZYitjuDQ6OS6SHTObn yYbC181x7qDMdX3Dqbvm0Wcfv2aRENJoFI8XzkZdzVbZswWJMnYoLYfQXOgJ3J+0CRmt ld1A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j8si32425013pfr.47.2019.04.17.01.59.46; Wed, 17 Apr 2019 02:00:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728856AbfDQI65 (ORCPT + 99 others); Wed, 17 Apr 2019 04:58:57 -0400 Received: from mx2.suse.de ([195.135.220.15]:44330 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726237AbfDQI65 (ORCPT ); Wed, 17 Apr 2019 04:58:57 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 0D177B03A; Wed, 17 Apr 2019 08:58:55 +0000 (UTC) From: Jiri Slaby To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, Jiri Slaby , Li RongQing , Wang Li , Zhang Yu , stable Subject: [PATCH] TTY: serial_core, add ->install Date: Wed, 17 Apr 2019 10:58:53 +0200 Message-Id: <20190417085853.21708-1-jslaby@suse.cz> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 fedb5760648a (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 Cc: Li RongQing Cc: Wang Li Cc: Zhang Yu Cc: Greg Kroah-Hartman Cc: stable --- ============================= NOTE ============================= Could you test your use-case at Baidu, guys, please? drivers/tty/serial/serial_core.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c index 7c787e517fa5..33319544d9d2 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -130,9 +130,6 @@ static void uart_start(struct tty_struct *tty) struct uart_port *port; unsigned long flags; - if (!state) - return; - port = uart_port_lock(state, flags); __uart_start(tty); uart_port_unlock(port, flags); @@ -730,9 +727,6 @@ static void uart_unthrottle(struct tty_struct *tty) upstat_t mask = UPSTAT_SYNC_FIFO; struct uart_port *port; - if (!state) - return; - port = uart_port_ref(state); if (!port) return; @@ -1732,6 +1726,16 @@ static void uart_dtr_rts(struct tty_port *port, int raise) uart_port_deref(uport); } +static int uart_install(struct tty_driver *driver, struct tty_struct *tty) +{ + struct uart_driver *drv = driver->driver_state; + struct uart_state *state = drv->state + tty->index; + + tty->driver_data = state; + + return tty_standard_install(driver, tty); +} + /* * Calls to uart_open are serialised by the tty_lock in * drivers/tty/tty_io.c:tty_open() @@ -1744,11 +1748,8 @@ static void uart_dtr_rts(struct tty_port *port, int raise) */ static int uart_open(struct tty_struct *tty, struct file *filp) { - struct uart_driver *drv = tty->driver->driver_state; - int retval, line = tty->index; - struct uart_state *state = drv->state + line; - - tty->driver_data = state; + struct uart_state *state = tty->driver_data; + int retval; retval = tty_port_open(&state->port, tty, filp); if (retval > 0) @@ -2433,6 +2434,7 @@ static void uart_poll_put_char(struct tty_driver *driver, int line, char ch) #endif static const struct tty_operations uart_ops = { + .install = uart_install, .open = uart_open, .close = uart_close, .write = uart_write, -- 2.21.0