Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7477655imu; Thu, 31 Jan 2019 10:44:34 -0800 (PST) X-Google-Smtp-Source: ALg8bN7bGpugdl/Jf1UjBwf2dNi5iGaMSZzyOJv8i9fxcP2ThrYUp1N5igjBW8i5NvaFbgTnLQD0 X-Received: by 2002:a63:bc02:: with SMTP id q2mr32710422pge.116.1548960274154; Thu, 31 Jan 2019 10:44:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548960274; cv=none; d=google.com; s=arc-20160816; b=YUXBd4XLi4o/XjvQ4nzxPrMO86ew80AiMXff0HSjcWNUKDWt5Irer6+3/8tU9Bgo2s 3/wI5ZAzqt5BBNVINMJ/QXd6dDP+9CuqteKxHmH4K27A6/5OfmjbN68TssKRG9WFHb7w aHu7R3jo78xuRwCASdmMzRha3qqijb0EJERu8HCyo21Z09g5KN/Xq2jIsw6fQwLTzibv hmEMlMM1P5UgzPdobuhIOuPWwIoaTbIwZJPXqFD5gYGhfKEW66ZIWZLzHQvdoefQxCjk H7Ry0VPhy1wFd1FHPJ1q7ibdyxq79YLOuPXbjU3WVWtNEVoVBKaBJdS0HNWYw69hQl6x 3x/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Z+g6s9S3LfvXy35X6PtzmhTyFQhue5BCfthYZP46eVc=; b=QTEv6XUV1empcVe0xRxJHDk70Jltr8HcQaMwYCOIg74SjMRyxwnhkJBk2y8Wqi95MW yNSRa/CF/empF2bxR1dNqOZKiNapKji3kIJW+94Nx55uIxPzHrSbMiI3bfOJpSqqNgOA 60GGHntu5oTi5HeBC4RyfyMT1PHvSVriJ7E3sGEpWMP620DDdo79pp3aofLbOVsAecZs 6w64y+tUqsDlEdGYkluM1GpOEDWhYyuCauPg7GNrtu1XvM2PV3gDun3C27FA7dgu2PDt E4m0m1Y01o+pD1aQtFxYhM2awOJ4xvBxo8VApLtQZRdeA17v3cviiMqe6riexJLrQXuf kSHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qv4sOcLM; 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 f16si4746486pgg.173.2019.01.31.10.44.18; Thu, 31 Jan 2019 10:44: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; dkim=pass header.i=@kernel.org header.s=default header.b=qv4sOcLM; 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 S1727210AbfAaSoN (ORCPT + 99 others); Thu, 31 Jan 2019 13:44:13 -0500 Received: from mail.kernel.org ([198.145.29.99]:60902 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726233AbfAaSoN (ORCPT ); Thu, 31 Jan 2019 13:44:13 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0EF5A218AC; Thu, 31 Jan 2019 18:44:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548960251; bh=li/wbQZqLc2GiajeYDvMLcmv5dmFcHfzQsmFERlq8k4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=qv4sOcLMVTmjxvIJ70UkJJRo7fLlkII4yZB7PNdrbh74IT/N+x9bWF0coIWefczps h2MwrYXrAGx9yzcVydGQE0OceLWKwptifYoejapP0lR6pxQ+h7aqqBjt9Y7oohUI+k U+XTDL7hHCI1sXbmflnqFL9SqhdwZcAeT3x8drT8= Date: Thu, 31 Jan 2019 19:44:09 +0100 From: Greg Kroah-Hartman To: "Li,Rongqing" Cc: "linux-serial@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "jslaby@suse.com" , "gkohli@codeaurora.org" Subject: Re: =?utf-8?B?562U5aSNOiBbUEFUQ0hdW1Y1?= =?utf-8?Q?=5D?= tty: fix race between flush_to_ldisc and tty_open Message-ID: <20190131184409.GA5238@kroah.com> References: <1548927796-11348-1-git-send-email-lirongqing@baidu.com> <20190131105527.GB8271@kroah.com> <22a5d8639b154119af7d7661e17025f2@baidu.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <22a5d8639b154119af7d7661e17025f2@baidu.com> User-Agent: Mutt/1.11.2 (2019-01-07) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jan 31, 2019 at 11:15:48AM +0000, Li,Rongqing wrote: > > > > -----邮件原件----- > > 发件人: linux-kernel-owner@vger.kernel.org > > [mailto:linux-kernel-owner@vger.kernel.org] 代表 Greg Kroah-Hartman > > 发送时间: 2019年1月31日 18:55 > > 收件人: Li,Rongqing > > 抄送: linux-serial@vger.kernel.org; linux-kernel@vger.kernel.org; > > jslaby@suse.com; gkohli@codeaurora.org > > 主题: Re: [PATCH][V5] tty: fix race between flush_to_ldisc and tty_open > > > > On Thu, Jan 31, 2019 at 05:43:16PM +0800, Li RongQing wrote: > > > 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 tty_open and > > > tty->driver_data may be NULL. > > > > > > CPU0 CPU1 > > > ---- ---- > > > 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 get 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, and n_tty_receive_buf_common maybe calls uart_unthrottle, so > > > add the same check > > > > > > Signed-off-by: Wang Li > > > Signed-off-by: Zhang Yu > > > Signed-off-by: Li RongQing > > > --- > > > V5: move check into uart_start from n_tty_receive_buf_common > > > V4: add version information > > > V3: not used ldisc semaphore lock, only checking tty->driver_data with > > > NULL > > > V2: fix building error by EXPORT_SYMBOL tty_ldisc_unlock > > > V1: extend ldisc lock to protect that tty->driver_data is inited > > > > > > drivers/tty/serial/serial_core.c | 6 ++++++ > > > 1 file changed, 6 insertions(+) > > > > > > diff --git a/drivers/tty/serial/serial_core.c > > > b/drivers/tty/serial/serial_core.c > > > index 5c01bb6d1c24..556f50aa1b58 100644 > > > --- a/drivers/tty/serial/serial_core.c > > > +++ b/drivers/tty/serial/serial_core.c > > > @@ -130,6 +130,9 @@ 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); > > > @@ -727,6 +730,9 @@ 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; > > > -- > > > 2.16.2 > > > > > > Hm, I wrote this patch, not you, right? So shouldn't I get the > > credit/blame for it? :) > > > > Welcome you to add your credit/blame/signature > and I am not clear the rule, and be afraid to become fake No problem, I've fixed this up when committing this, and added some wording change to the changelog text. Thanks so much for working through all of this, it's a bug that has always been there for forever it seems, nice catch! greg k-h