Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5716988imu; Wed, 30 Jan 2019 02:20:44 -0800 (PST) X-Google-Smtp-Source: ALg8bN69+0a6XCraf2D7nzjymCL61OORQKGC8UGIgkc8WhX4V+evNOfESosD5lC0bFuxMovVMrQ5 X-Received: by 2002:a63:a452:: with SMTP id c18mr27264650pgp.204.1548843644148; Wed, 30 Jan 2019 02:20:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548843644; cv=none; d=google.com; s=arc-20160816; b=xZiFsuPwyOWzKNrzuaDwee+je3JhayB0lMtX/Yl1Jo0rHjjrFTBv6B+bmikS/UN7S7 U1OXEUDJ96pHlBJMiq6NwqpUK2Fu89HE7jICASZj6MaByOBvQW8LnHYMsEqIuhkVkvIw yS9wi3Y2KNdA7qmJl5cpiXL/5F3BYu6XEhNXRZa3RvVnNqAf2z3C3uyE/1cFu86/8HXp DfH3uAjbTubFjJzj04xh3u+H65wgFkq/Yc3bo+mG3YWMF7RKa3qgnTpL3UIWDLiwGbsC dNkPv3ZZeED4QGS7W2o3UyjdgudWFzWn6yxagG+JF4aI6MIuC6YysGWh8Ds96JSfMiG3 rXRw== 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=Z+fvY47kxeCmDpgjbVDFqreK0LF4NCT++1fvwky1rU0=; b=d8BWJlr0VTCFONI/QD+M0E+2OMM2iXUWZRdRSKK67Msiw2vMtVJxOW3ujmmixoYRK8 F1MYd8f+NKwPSbbXpUSTKQkQx+C9zVXm4BbtTeHVFC16y71t1QXQkAVF2gy3LZtzoZkv 2IykNtvkvzfbEgmLRSqWWTYHSkGIScULowg0Pq0pjqhkhd3wp58YKqY9tiRVqsjpcRAD ApLpjYnZ0G9uEfOZODBK8MsIVijJZMa7+qFytb+aCxwb2SmFptEYlCnq7/GeUqtlkoVY nTPPbD2NiJnvoSRGbGQ+k7w3E9W5qVmVbj1JTA1mdJGVaeQqybEovZlvvZjAepKHQ2+E DbEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IrpH1aTE; 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 g12si979769pgh.368.2019.01.30.02.20.28; Wed, 30 Jan 2019 02:20:44 -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=IrpH1aTE; 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 S1729989AbfA3KTR (ORCPT + 99 others); Wed, 30 Jan 2019 05:19:17 -0500 Received: from mail.kernel.org ([198.145.29.99]:39878 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726476AbfA3KTR (ORCPT ); Wed, 30 Jan 2019 05:19:17 -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 632A420833; Wed, 30 Jan 2019 10:19:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548843556; bh=vRx06fUlConj2xK/3BjttDMmHNSAQBWU8KS/doGHuc4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=IrpH1aTECYaK4fmQAh8NWZof1VcBOgtfp14ar3PuVp8mGMSG64K04XOP3a72/1Cka eJcTgXZKWq7itrJ+SnDE/KiyA1Fem0m2BvBE0PqozFppwH0B4J/WKhB0f2w9lI0bNQ 4Dmloc+t9isiPsvJ1tXdKyFD7HguSkmwAlPq+W0c= Date: Wed, 30 Jan 2019 11:19:14 +0100 From: Greg KH To: Li RongQing Cc: jslaby@suse.com, linux-kernel@vger.kernel.org, gkohli@codeaurora.org Subject: Re: [PATCH][v4] tty: fix race between flush_to_ldisc and tty_open Message-ID: <20190130101914.GA30587@kroah.com> References: <1547803637-29135-1-git-send-email-lirongqing@baidu.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1547803637-29135-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 Fri, Jan 18, 2019 at 05:27:17PM +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 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 > --- > 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/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; > + How is this working? What is setting driver_data to NULL to "stop" this race? There's no requirement that a tty driver set this field to NULL when it is "done" with the tty device, so I think you are just getting lucky in that your specific driver happens to be doing this. What driver are you testing this against? thanks, greg k-h