Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp687341imm; Wed, 29 Aug 2018 09:38:05 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbEaRdV1xkn7aKqjqFOAU2qtsypkYbnTYrjoACgzYvCs/TWqZaYCEPWgUUNOMPScEUdIiHC X-Received: by 2002:a63:d54e:: with SMTP id v14-v6mr6536411pgi.264.1535560685132; Wed, 29 Aug 2018 09:38:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535560685; cv=none; d=google.com; s=arc-20160816; b=MWlFmeVoN+2SMIQAQj5EltHA6vATlDW470O5t4U3YBvBXIxrXfpNWGe2TvvRCHPqrM HMGB4Wo1rSd2RLfuSv87UF835oFj0/KE3l02zv/FZA2FJs6Rt6i+8364nKZ/qNU+W+99 Nnz3Zkm/hQNBHwo1w75dX69HhRt6+0ur9s0Bgo+Si4gXotVu79p1YVugckMunfuOFvxl lGG8Geu9sSd8rBGldTAqpGa2Qx0v8Tipko7HY4goJcB7Ir7gQ6qXiaIuRJrqbyCFyotX UQDaThKOfjFbj4luKNz1wtl+WMCyIKoq0XkUp5nVykJ7hTOJQcnPHjEjr+lvLJvdUHxd JEzA== 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 :references:in-reply-to:date:cc:to:from:subject:message-id :dkim-signature:arc-authentication-results; bh=3kgpvui78MyCtiysS+db7aq1YPpHwu05uJeSHvX2Afc=; b=XhD0t7b1b2An4/b7SLqF8PkpqebnmBGOUhur8QIrbhlzCYtDRIvOpZM8vIdtAVElkg 6ZhlFsCEi7AEu4ub/RZy9YNodbistQ2id0f4OTR5UvFIOFR9mH2L3gJH8ap6ikqO86WU 12SR+VIR3rZKLqlFdK2gX8lQqNNLiWtLbwysLl25LwXcgJzbLXnzbw9B5VksQWPPLwPm D/ntp+gQB95HSxOvmiBMrUitucdTkMr8PBjYJr9qoQvLs1SC569d8DpTB5uG1JTSYjug 13ydXKJsRdgXoVHhNroEoS2h6uPsWKTE3FSq06bMKitbYJZSBUj9dt2Wnw3yHttynpvW NQUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=ITQM2TL4; 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; dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=arista.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p187-v6si4250114pfp.27.2018.08.29.09.37.50; Wed, 29 Aug 2018 09:38:05 -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=@arista.com header.s=googlenew header.b=ITQM2TL4; 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; dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=arista.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728136AbeH2UeH (ORCPT + 99 others); Wed, 29 Aug 2018 16:34:07 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:40547 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727293AbeH2UeH (ORCPT ); Wed, 29 Aug 2018 16:34:07 -0400 Received: by mail-ed1-f65.google.com with SMTP id e19-v6so4396665edq.7 for ; Wed, 29 Aug 2018 09:36:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=message-id:subject:from:to:cc:date:in-reply-to:references :mime-version:content-transfer-encoding; bh=3kgpvui78MyCtiysS+db7aq1YPpHwu05uJeSHvX2Afc=; b=ITQM2TL4GPneKbbB1zH9yeNBZOR3Bg7pmIN8L9j5VkuIkNmsTyDXjH6jdnA2SrsTEa qT0tTgv0ReuECEFPZk/5G3XIaH60WadE6hdmV9ShoW7bNfcvP4YkOL0k+emBBJHeRHJ5 eUm45uJemOsiHJ0KVvKty41yQdpjERQD5vMX/dW2adQq5YC2Ab26KGkV22l6vh0qgNaQ P1Sxcb3GVEi7fk3LzrDlR9z0izukP6x6pO14Nopo82Mjzt/notxNy4QOEqfqKBShGoyR jmp5VwnQ20jc+GXwa5007sgMRD1CyBBihhEsiGbyzWRfe4MaNX0zX+XO0jH+gV94v2Nk eAGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:mime-version:content-transfer-encoding; bh=3kgpvui78MyCtiysS+db7aq1YPpHwu05uJeSHvX2Afc=; b=hdbpb64e7mJIEJq2pct9xg7yc4Y0CP54mJouLGLEFTu0HA8Mc/IuVwbJSe1/Mv7lNA hHpyyuxYj1Wnlj+iV4ba6H9I7kFmMZxD8NuuoCBpjqoGiG3WXu06GOWbBm/Dxo0a4jJl 6UaRJwITEmfC02muwkkHDIg3wf2WdGsmDQbUqNPSNIdYPmcPVnxBdUjh5F1cKf1LtqjO LuRQ1+HV+C9mLHLtL9QFxhMEn6kvazgaZ+lOiT+1CGmHxwp7/99n6JnBCU7D58r5cfvL jUPXyw5/1mDIhlZT2RuhaFsB/5od8W360MCnz54J18cV/jh6vYBgLZn03rzliWwhGNby 9s2g== X-Gm-Message-State: APzg51B7msS+ZCXiPPLkxOJDy0b1njIx6jgTB3Krx6IaqY6JSOMGk0mk NPJD4p+6op8hNWUoxhgMSCm5uQ== X-Received: by 2002:a50:9aa4:: with SMTP id p33-v6mr8599627edb.156.1535560582554; Wed, 29 Aug 2018 09:36:22 -0700 (PDT) Received: from dhcp.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id 30-v6sm1991887edt.24.2018.08.29.09.36.21 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 29 Aug 2018 09:36:21 -0700 (PDT) Message-ID: <1535560580.23560.65.camel@arista.com> Subject: Re: [PATCH 2/4] tty: Hold tty_ldisc_lock() during tty_reopen() From: Dmitry Safonov To: Jiri Slaby , linux-kernel@vger.kernel.org Cc: Daniel Axtens , Dmitry Safonov <0x7f454c46@gmail.com>, Sergey Senozhatsky , Dmitry Vyukov , Tan Xiaojun , Peter Hurley , Pasi =?ISO-8859-1?Q?K=E4rkk=E4inen?= , Greg Kroah-Hartman , Michael Neuling , Mikulas Patocka , stable@vger.kernel.org Date: Wed, 29 Aug 2018 17:36:20 +0100 In-Reply-To: <914d8184-d5e6-519c-b355-7f1360cfa6a0@suse.cz> References: <20180829022353.23568-1-dima@arista.com> <20180829022353.23568-3-dima@arista.com> <914d8184-d5e6-519c-b355-7f1360cfa6a0@suse.cz> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.24.6 (3.24.6-1.fc26) Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 2018-08-29 at 16:40 +0200, Jiri Slaby wrote: > On 08/29/2018, 04:23 AM, Dmitry Safonov wrote: > > tty_ldisc_reinit() doesn't race with neither tty_ldisc_hangup() > > nor set_ldisc() nor tty_ldisc_release() as they use tty lock. > > But it races with anyone who expects line discipline to be the same > > after hoding read semaphore in tty_ldisc_ref(). > > > > We've seen the following crash on v4.9.108 stable: > > > > BUG: unable to handle kernel paging request at 0000000000002260 > > IP: [..] n_tty_receive_buf_common+0x5f/0x86d > > Workqueue: events_unbound flush_to_ldisc > > Call Trace: > > [..] n_tty_receive_buf2 > > [..] tty_ldisc_receive_buf > > [..] flush_to_ldisc > > [..] process_one_work > > [..] worker_thread > > [..] kthread > > [..] ret_from_fork > > > > I think, tty_ldisc_reinit() should be called with ldisc_sem hold > > for > > writing, which will protect any reader against line discipline > > changes. > > > > Note: I failed to reproduce the described crash, so obiviously > > can't > > guarantee that this is the place where line discipline was > > switched. > > > > Cc: Greg Kroah-Hartman > > Cc: Jiri Slaby > > Cc: stable@vger.kernel.org > > Signed-off-by: Dmitry Safonov > > --- > > drivers/tty/tty_io.c | 9 +++++++-- > > 1 file changed, 7 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c > > index 5e5da9acaf0a..3ef8b977b167 100644 > > --- a/drivers/tty/tty_io.c > > +++ b/drivers/tty/tty_io.c > > @@ -1267,15 +1267,20 @@ static int tty_reopen(struct tty_struct > > *tty) > > if (test_bit(TTY_EXCLUSIVE, &tty->flags) && > > !capable(CAP_SYS_ADMIN)) > > return -EBUSY; > > > > - tty->count++; > > + retval = tty_ldisc_lock(tty, 5 * HZ); > > Why 5 secs? This would cause random errors on machines under heavy > load. Yeah, I think MAX_SCHEDULE_TIMEOUT will make more sense here.. Not sure, why I decided to go with 5*HZ instead. Will resend with new timeout, if everything else looks good to you. (having in mind my argument for count++ in 1/4) > > > + if (retval) > > + return retval; > > > > + tty->count++; > > if (tty->ldisc) > > - return 0; > > + goto out_unlock; > > > > retval = tty_ldisc_reinit(tty, tty->termios.c_line); > > if (retval) > > tty->count--; > > > > +out_unlock: > > + tty_ldisc_unlock(tty); > > return retval; > > So what about: > tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT); > if (!tty->ldisc) > ret = tty_ldisc_reinit(tty, tty->termios.c_line); > tty_ldisc_unlock(tty); > > if (!ret) > tty->count++; > > return ret; > -- Thanks, Dmitry