Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp254163imm; Mon, 4 Jun 2018 17:03:41 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJ2NsnkTN92kehs3XH1EQ6wTCGdXbHV4X9OURq2OlGcG7X3AwJdFQW1kilOlfcF4KDc68mD X-Received: by 2002:a17:902:8d85:: with SMTP id v5-v6mr24004327plo.93.1528157021040; Mon, 04 Jun 2018 17:03:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528157021; cv=none; d=google.com; s=arc-20160816; b=wWNL4jbxgYXzOnoJmzynALdFBaWdN7ZhoDWl7va0YU53UB00ZOGcSEPV2LVPL3C/mr 5UQeWCqZ6IFKPZbxCXNWxi4pTdGVyL0O70MWoMZrj2CdK2WvIF8xt3JIMidrLyiGHqOL Qahv7sP7s2E1Z7pZUgq2vSunNYhCk27uVT0n1iZEhjyafpmffNAcVEjg7LiSkQgu9ER/ +7BJhzu8hJMCRxaIyjTnBtoZ2H6kEMf1n2VsVmyq3KHDMXulsTuDVPCTphzRJ/8bq4Wu lK1mNbiKznBcZOPzgBSqsovE6VEnKoTMg7EOr5Q3n4Wr5N+gL9IM62mf9Q2N93NQcWlb o2/Q== 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:dkim-signature :arc-authentication-results; bh=skyAZcaaYXfY7AQVVH//DSX9uatYJKZiE5Y/XAj04fM=; b=o1bBZNFHwoATKwosHdvrR7DlxubC8aZ/OWSirXF6DLHQl4d4OcR+QsL1a57JLWJz20 X5pNGhrLVrxECpI6ZqeIOx5qncjZYoP4V21DoOI0R+3b71gN7Q0hvEr6cG+/F1PQnMYW SKVvMvyBqhQqP2LYefjhcJ/tGKLQFFHr4MyGeFj2eBq7bs/2AReb4hF1RQSvrjLU6ABE E1umc+SWj4MTXfq+pzco/yv6i14z/VcgLN097sv9EXHu+GXtWy8GPUOyA3dWd/rwNLJp lamfe1KCEOXGXYclDSMClVA/lqlknIxR8X3lV7SX2V5yg6tAq/3nanJumdTrd+fV0Gak YasQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@tycho-ws.20150623.gappssmtp.com header.s=20150623 header.b=Unfbqvym; 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 o26-v6si8920144pfe.44.2018.06.04.17.03.24; Mon, 04 Jun 2018 17:03:41 -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; dkim=pass header.i=@tycho-ws.20150623.gappssmtp.com header.s=20150623 header.b=Unfbqvym; 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 S1751242AbeFEAC5 (ORCPT + 99 others); Mon, 4 Jun 2018 20:02:57 -0400 Received: from mail-it0-f66.google.com ([209.85.214.66]:38263 "EHLO mail-it0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751042AbeFEACz (ORCPT ); Mon, 4 Jun 2018 20:02:55 -0400 Received: by mail-it0-f66.google.com with SMTP id v83-v6so1218381itc.3 for ; Mon, 04 Jun 2018 17:02:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tycho-ws.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=skyAZcaaYXfY7AQVVH//DSX9uatYJKZiE5Y/XAj04fM=; b=UnfbqvymTxoWMbA6nGMBu57bcuU/EFseBhw6kk9dZx9/MgoMkIWh9EBZUMTRhGGiF8 DKUihBoOzqgQddQ5YLWwQrrrvB5wSv2OOlXlga7qijK/q96vxZU1+z3Q1JlGU6IwTqBa f8fYH/ngT6QlzmtNhLRtU4AHOK2KPrc2n6WE4po+GxSCz7f89+EXupfZwmCYyH87BvS4 dV5V3T2jNKbLGntxQkg+fztdcao26k5M6tnU+VGWoAXlIEsjgNCdBU932gZ4EA6AWa+H m5XlkZ2YRNZvevyh8a102zPoXqCqu51SwpSd/eocT6JDqrlOGdVlCWjIKpv8zIBgk5z5 mrkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=skyAZcaaYXfY7AQVVH//DSX9uatYJKZiE5Y/XAj04fM=; b=IBWn6PuUPGTuSs7An4h0jhutK2e+K2rW5KyqbL76cZThOab+SXZGQmow3jxd0CVODH kM4Hu/UniTNl43npvJjOgWmEgxUuJCw7Ani+YN3NmRIJRNSt0iYi87ige0+zWpSOrWsx btUhLtJyNtLPlF/8QZ5gDd2fgqBVGdEwbsSzfZRUFcqqPTNwujigKu9wWqpdtK+tDz+e xpuO++XXkcSN8choP0jVQqZtsOjKC+csp1hZ7jTEpbVQa9oROZCwnIxJHsOu/1DtxfXW 6ZiDF88jR05Um+B7mBo69R3fVlLFY5IBwhqmsuwy4yM1K0Cdd0fF+9GeQmQ2GxrkhzU7 BwTQ== X-Gm-Message-State: APt69E3P3F6JehCIuEdU2qsEUDIcHdhjJgS+jv6DnCDnVumzM1AyvLcQ /HaMjcCbXFn1E9VVw/vqwnB3bA== X-Received: by 2002:a24:d6cf:: with SMTP id o198-v6mr16626311itg.3.1528156974570; Mon, 04 Jun 2018 17:02:54 -0700 (PDT) Received: from cisco.lan ([8.24.24.129]) by smtp.gmail.com with ESMTPSA id l13-v6sm4008507iob.82.2018.06.04.17.02.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Jun 2018 17:02:53 -0700 (PDT) From: Tycho Andersen To: Greg Kroah-Hartman , Jiri Slaby Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Serge E . Hallyn" , Tycho Andersen Subject: [PATCH] uart: fix race between uart_put_char() and uart_shutdown() Date: Mon, 4 Jun 2018 18:01:27 -0600 Message-Id: <20180605000127.5495-1-tycho@tycho.ws> X-Mailer: git-send-email 2.17.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We have reports of the following crash: PID: 7 TASK: ffff88085c6d61c0 CPU: 1 COMMAND: "kworker/u25:0" #0 [ffff88085c6db710] machine_kexec at ffffffff81046239 #1 [ffff88085c6db760] crash_kexec at ffffffff810fc248 #2 [ffff88085c6db830] oops_end at ffffffff81008ae7 #3 [ffff88085c6db860] no_context at ffffffff81050b8f #4 [ffff88085c6db8b0] __bad_area_nosemaphore at ffffffff81050d75 #5 [ffff88085c6db900] bad_area_nosemaphore at ffffffff81050e83 #6 [ffff88085c6db910] __do_page_fault at ffffffff8105132e #7 [ffff88085c6db9b0] do_page_fault at ffffffff8105152c #8 [ffff88085c6db9c0] page_fault at ffffffff81a3f122 [exception RIP: uart_put_char+149] RIP: ffffffff814b67b5 RSP: ffff88085c6dba78 RFLAGS: 00010006 RAX: 0000000000000292 RBX: ffffffff827c5120 RCX: 0000000000000081 RDX: 0000000000000000 RSI: 000000000000005f RDI: ffffffff827c5120 RBP: ffff88085c6dba98 R8: 000000000000012c R9: ffffffff822ea320 R10: ffff88085fe4db04 R11: 0000000000000001 R12: ffff881059f9c000 R13: 0000000000000001 R14: 000000000000005f R15: 0000000000000fba ORIG_RAX: ffffffffffffffff CS: 0010 SS: 0018 #9 [ffff88085c6dbaa0] tty_put_char at ffffffff81497544 #10 [ffff88085c6dbac0] do_output_char at ffffffff8149c91c #11 [ffff88085c6dbae0] __process_echoes at ffffffff8149cb8b #12 [ffff88085c6dbb30] commit_echoes at ffffffff8149cdc2 #13 [ffff88085c6dbb60] n_tty_receive_buf_fast at ffffffff8149e49b #14 [ffff88085c6dbbc0] __receive_buf at ffffffff8149ef5a #15 [ffff88085c6dbc20] n_tty_receive_buf_common at ffffffff8149f016 #16 [ffff88085c6dbca0] n_tty_receive_buf2 at ffffffff8149f194 #17 [ffff88085c6dbcb0] flush_to_ldisc at ffffffff814a238a #18 [ffff88085c6dbd50] process_one_work at ffffffff81090be2 #19 [ffff88085c6dbe20] worker_thread at ffffffff81091b4d #20 [ffff88085c6dbeb0] kthread at ffffffff81096384 #21 [ffff88085c6dbf50] ret_from_fork at ffffffff81a3d69f​ after slogging through some dissasembly: ffffffff814b6720 : ffffffff814b6720: 55 push %rbp ffffffff814b6721: 48 89 e5 mov %rsp,%rbp ffffffff814b6724: 48 83 ec 20 sub $0x20,%rsp ffffffff814b6728: 48 89 1c 24 mov %rbx,(%rsp) ffffffff814b672c: 4c 89 64 24 08 mov %r12,0x8(%rsp) ffffffff814b6731: 4c 89 6c 24 10 mov %r13,0x10(%rsp) ffffffff814b6736: 4c 89 74 24 18 mov %r14,0x18(%rsp) ffffffff814b673b: e8 b0 8e 58 00 callq ffffffff81a3f5f0 ffffffff814b6740: 4c 8b a7 88 02 00 00 mov 0x288(%rdi),%r12 ffffffff814b6747: 45 31 ed xor %r13d,%r13d ffffffff814b674a: 41 89 f6 mov %esi,%r14d ffffffff814b674d: 49 83 bc 24 70 01 00 cmpq $0x0,0x170(%r12) ffffffff814b6754: 00 00 ffffffff814b6756: 49 8b 9c 24 80 01 00 mov 0x180(%r12),%rbx ffffffff814b675d: 00 ffffffff814b675e: 74 2f je ffffffff814b678f ffffffff814b6760: 48 89 df mov %rbx,%rdi ffffffff814b6763: e8 a8 67 58 00 callq ffffffff81a3cf10 <_raw_spin_lock_irqsave> ffffffff814b6768: 41 8b 8c 24 78 01 00 mov 0x178(%r12),%ecx ffffffff814b676f: 00 ffffffff814b6770: 89 ca mov %ecx,%edx ffffffff814b6772: f7 d2 not %edx ffffffff814b6774: 41 03 94 24 7c 01 00 add 0x17c(%r12),%edx ffffffff814b677b: 00 ffffffff814b677c: 81 e2 ff 0f 00 00 and $0xfff,%edx ffffffff814b6782: 75 23 jne ffffffff814b67a7 ffffffff814b6784: 48 89 c6 mov %rax,%rsi ffffffff814b6787: 48 89 df mov %rbx,%rdi ffffffff814b678a: e8 e1 64 58 00 callq ffffffff81a3cc70 <_raw_spin_unlock_irqrestore> ffffffff814b678f: 44 89 e8 mov %r13d,%eax ffffffff814b6792: 48 8b 1c 24 mov (%rsp),%rbx ffffffff814b6796: 4c 8b 64 24 08 mov 0x8(%rsp),%r12 ffffffff814b679b: 4c 8b 6c 24 10 mov 0x10(%rsp),%r13 ffffffff814b67a0: 4c 8b 74 24 18 mov 0x18(%rsp),%r14 ffffffff814b67a5: c9 leaveq ffffffff814b67a6: c3 retq ffffffff814b67a7: 49 8b 94 24 70 01 00 mov 0x170(%r12),%rdx ffffffff814b67ae: 00 ffffffff814b67af: 48 63 c9 movslq %ecx,%rcx ffffffff814b67b2: 41 b5 01 mov $0x1,%r13b ffffffff814b67b5: 44 88 34 0a mov %r14b,(%rdx,%rcx,1) ffffffff814b67b9: 41 8b 94 24 78 01 00 mov 0x178(%r12),%edx ffffffff814b67c0: 00 ffffffff814b67c1: 83 c2 01 add $0x1,%edx ffffffff814b67c4: 81 e2 ff 0f 00 00 and $0xfff,%edx ffffffff814b67ca: 41 89 94 24 78 01 00 mov %edx,0x178(%r12) ffffffff814b67d1: 00 ffffffff814b67d2: eb b0 jmp ffffffff814b6784 ffffffff814b67d4: 66 66 66 2e 0f 1f 84 data32 data32 nopw %cs:0x0(%rax,%rax,1) ffffffff814b67db: 00 00 00 00 00 for our build, this is crashing at: circ->buf[circ->head] = c; Looking in uart_port_startup(), it seems that circ->buf (state->xmit.buf) protected by the "per-port mutex", which based on uart_port_check() is state->port.mutex. Indeed, the lock acquired in uart_put_char() is uport->lock, i.e. not the same lock. Anyway, since the lock is not acquired, if uart_shutdown() is called, the last chunk of that function may release state->xmit.buf before its assigned to null, and cause the race above. To fix it, we simply also acquire state->port.mutex. Unfortunately, I don't have any insightful thoughts about how to test this. Ideas are appreciated :) Signed-off-by: Tycho Andersen --- drivers/tty/serial/serial_core.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c index 0466f9f08a91..883a8c15510c 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -532,9 +532,15 @@ static int uart_put_char(struct tty_struct *tty, unsigned char c) unsigned long flags; int ret = 0; + /* + * state->xmit.buf is protected by state->port.mutex, see the note in + * uart_port_startup() + */ + mutex_lock(&state->port.mutex); + circ = &state->xmit; if (!circ->buf) - return 0; + goto out; port = uart_port_lock(state, flags); if (port && uart_circ_chars_free(circ) != 0) { @@ -543,6 +549,9 @@ static int uart_put_char(struct tty_struct *tty, unsigned char c) ret = 1; } uart_port_unlock(port, flags); + +out: + mutex_unlock(&state->port.mutex); return ret; } -- 2.17.0