Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6977825imu; Thu, 31 Jan 2019 02:56:15 -0800 (PST) X-Google-Smtp-Source: ALg8bN45i0Sb2zzia2f3aF0YP0x01BTO+ugPiT+40DJCigJkH1I8qL52oGJb4gqF6Za6N7VV5NCm X-Received: by 2002:a63:4246:: with SMTP id p67mr30737250pga.335.1548932175555; Thu, 31 Jan 2019 02:56:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548932175; cv=none; d=google.com; s=arc-20160816; b=neJH/Ej/5O7pPuNq6/w/vreBGX56DEdEiDVjhORkndlc60U9xFfSq6IpHYfSQroIFb 7qSuo6bWdyA2aa7Vgd1I2h7YXiN+JpN2us3FcPVZLlSaWwu0TSMsyD+ZW/J6wFgXRinO 0ZQMrf06scJJNxu5HJtREYgQWArGowW3HEx6g75ePvWsx6bxSlZOcWdZutjdbAMcbNO1 43UX6q/ESD6OxtoFBW5ZmtKQ0CBALPDbpGmnGMuVvnhOL7T481ZL3ylZcQnUBphn8s69 t0hkrhokyNy+XFhbZHfWFPJZLf3agNM+iI5jymSfxNQMuNhahG7m9G1k2fX5Hir5aKik FTVA== 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-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=G4pHmJGKMm3kdrP3IFYuq6Wd7SBuPcB8iFuBenZTnxA=; b=qf6p52SxldN/jI8nBUs2JeWmPCG3fPyIZOoJKvmRORJqHOCqlgns9QIl4RrgBiCBMe jJlDq0gmkN5240/9Usg3EmTttKhQcn9Z2Hbj5ikHCx5zbiy1aSIapMOpweG0uhzCCfor yYRdZQGTCe9rc2YLEFqJvlpYRm07r5/E+XEy1/D45CPwDKaoi8yqKvUO5CONaFmAWnse nX89FzxkuH168xktqW2jxwNEPBABwya9a6046Eiq+K2jEtroVgccao3noyCjkWaHiuLn Lz1bl46h98XzSy54sQnEU2mNgkfTqpQ9xbtVXf/5dPbImaB8sKi/Z3FAKkCEObeVcX7d wjCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=NhpJUlqm; 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 v19si4358623pfa.80.2019.01.31.02.55.59; Thu, 31 Jan 2019 02:56:15 -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=NhpJUlqm; 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 S1727501AbfAaKzb (ORCPT + 99 others); Thu, 31 Jan 2019 05:55:31 -0500 Received: from mail.kernel.org ([198.145.29.99]:57900 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726221AbfAaKzb (ORCPT ); Thu, 31 Jan 2019 05:55:31 -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 982E920881; Thu, 31 Jan 2019 10:55:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548932130; bh=daEJmqzCPi2B5RpDdN2XBf3HLO4Bjp7et4ARW9cajKE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=NhpJUlqm5RMoSImLy2JJYLNPPnOcwadiagBrwkLDQpAFsjfd6BrPM4THjMJJKb780 rADmWGQ2EiA/k7z9rVX70zMWB04Wrsrg8OBUmm1ZbTK3R7eGkIRxZMrL8eFeTxktD1 ZjK7LBVAdxKbzRviLrzJ0eTsCXmedLwIzPLP+JoI= Date: Thu, 31 Jan 2019 11:55:27 +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: [PATCH][V5] tty: fix race between flush_to_ldisc and tty_open Message-ID: <20190131105527.GB8271@kroah.com> References: <1548927796-11348-1-git-send-email-lirongqing@baidu.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1548927796-11348-1-git-send-email-lirongqing@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 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? :) Also, this is a bug in the serial code, not necessarily the tty layer, so the subject should change... And you did test this, right? thanks, greg k-h