Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2894080imu; Fri, 18 Jan 2019 00:57:34 -0800 (PST) X-Google-Smtp-Source: ALg8bN6ErK1kCzuQjRyCxuXaozME4N+k29DTgu5BJNuxzeWS4TtjyHONpdUjzHQ+FeswfgBgVvxL X-Received: by 2002:a63:d547:: with SMTP id v7mr16495968pgi.339.1547801854899; Fri, 18 Jan 2019 00:57:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547801854; cv=none; d=google.com; s=arc-20160816; b=T8oSXuBIrn43JZWSzx+CDBtmKcIbbgSZMNy2BLRTgLCoU1f67LfhAuA7F2WZvAuoLU zA20waDk9IYQJtv0UnHRxga6Wtr8DHxrf7yS3SrsstFY3QaaajQf/klg766EK2gEruqR YJh5RgIMQUT7r00ba7M6bTZMEbFWeJ5rneRbzz1p2+A1yepLRSctywOXXn2bKdQMZHh0 /YKVbO9bLIWq9m3nIezcnglRclLOJv+D4S+dCc+vMq88WVo+wTGU1X4VhbwJNw9DehV9 j1yy6QHWAcj2GzfIiELRpyrM4n0J6ubPaP+dogtjyWrvELpwzl8WeWFGVqzvumO18SIL S7zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:to:from; bh=ckasBn3FbInX2OHnTlDEs69C+SXErTg2WiABsDBG1jg=; b=x494vRD0jXPuoBj6XS6rX6/n9cLmb0tTkWghK3w2iMH4TzyRpzfevXW715jtPuU0/o mPsth0fpBSCbLA+SDvxk2fp9+HmfWHlZh8AwThsDiddOlSZPjX7GkiqaKGVCPdtYJlR9 9xPr5XdqNE2iOhDUH0z7ao6T16TF9BaLm8GJFqrOI/u6PGlNJ4wC15QaM6il74z61M/0 ItPWdGTOfsZOG9gsMm6lyygih9snu4WBztN7650GrptVT1ZGkurLMIR5h+MWPrIcnv6a gccbJg7ZQXtY2C8QXFQSnPWGuuKlXvNSLLGiNXKdXZRPQLL6qdLMWCT78Lq8YSZceFcL /IIg== 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 189si4105268pfd.142.2019.01.18.00.57.19; Fri, 18 Jan 2019 00:57:34 -0800 (PST) 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 S1725994AbfARI4R (ORCPT + 99 others); Fri, 18 Jan 2019 03:56:17 -0500 Received: from mx60.baidu.com ([61.135.168.60]:57249 "EHLO tc-sys-mailedm04.tc.baidu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725784AbfARI4R (ORCPT ); Fri, 18 Jan 2019 03:56:17 -0500 Received: from localhost (cp01-cos-dev01.cp01.baidu.com [10.92.119.46]) by tc-sys-mailedm04.tc.baidu.com (Postfix) with ESMTP id 95DC4236C028; Fri, 18 Jan 2019 16:56:04 +0800 (CST) From: Li RongQing To: gregkh@linuxfoundation.org, jslaby@suse.com, linux-kernel@vger.kernel.org, gkohli@codeaurora.org Subject: [PATCH][v3] tty: fix race between flush_to_ldisc and tty_open Date: Fri, 18 Jan 2019 16:56:04 +0800 Message-Id: <1547801764-13660-1-git-send-email-lirongqing@baidu.com> X-Mailer: git-send-email 1.7.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There still is a race window after the commit b027e2298bd588 ("tty: fix data race between tty_init_dev and flush of buf"), and we encountered this crash issue if receive_buf call comes before tty initialization completes in n_tty_open and tty->driver_data may be NULL. CPU0 CPU1 ---- ---- n_tty_open tty_init_dev tty_ldisc_unlock schedule flush_to_ldisc receive_buf tty_port_default_receive_buf tty_ldisc_receive_buf n_tty_receive_buf_common __receive_buf uart_flush_chars uart_start /*tty->driver_data is NULL*/ tty->ops->open /*init tty->driver_data*/ it can be fixed by extending ldisc semaphore lock in tty_init_dev to driver_data initialized completely after tty->ops->open(), but this will lead to put lock on one function and unlock in some other function, and hard to maintain, so fix this race only by checking tty->driver_data when receiving, and return if tty->driver_data is NULL Signed-off-by: Wang Li Signed-off-by: Zhang Yu Signed-off-by: Li RongQing --- drivers/tty/tty_port.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/tty/tty_port.c b/drivers/tty/tty_port.c index 044c3cbdcfa4..86d0bec38322 100644 --- a/drivers/tty/tty_port.c +++ b/drivers/tty/tty_port.c @@ -31,6 +31,9 @@ static int tty_port_default_receive_buf(struct tty_port *port, if (!tty) return 0; + if (!tty->driver_data) + return 0; + disc = tty_ldisc_ref(tty); if (!disc) return 0; -- 2.16.2