From patchwork Tue Jan 8 20:28:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guilherme G. Piccoli" X-Patchwork-Id: 1022127 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43Z3k33BHhz9s7h; Wed, 9 Jan 2019 07:28:27 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1ggxz3-0000gO-KE; Tue, 08 Jan 2019 20:28:21 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1ggxz1-0000fj-BI for kernel-team@lists.ubuntu.com; Tue, 08 Jan 2019 20:28:19 +0000 Received: from mail-qk1-f200.google.com ([209.85.222.200]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1ggxz1-00081m-0z for kernel-team@lists.ubuntu.com; Tue, 08 Jan 2019 20:28:19 +0000 Received: by mail-qk1-f200.google.com with SMTP id w28so4159387qkj.22 for ; Tue, 08 Jan 2019 12:28:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6Mmn1efbQegm1s2YQxJjqqhFyjzaYG3RCtqIpSVGqZ4=; b=GeJx3DDYwyOMyMaaf3EMqCzWe0hP3TpUcQ1e0Wl8S9e8AQggASqfjJ38jm5vWah2PE 7yjpUjYpl12LL+OXVos5ErH7+jj3OUd4pgAE1zJqcBIw4qvh/EUyOcO4AVxqruQpMEmV zsmTcJ46xIhR5ys4uPqpSt1mlblRPhLZmcWLWClD3Z7C98xVKrsExGwQdcSn/E0eF019 UZnxuSsYybm0FidPsuCwOOQidaLOAtkDs28RGoGQaoU+Pkm6GKs0xphygH0gyVzUyImc pCJM7pxhjVLjGaM789haavyNQXOageJFZlVzrz8spiKkJhsypFpmi6vYDINDk91sFSN/ +XCQ== X-Gm-Message-State: AJcUukdomi/Ee8sDPEyx+0k8oeXUxrNaCYtO6WPWzV74xoLSdxkRXiu2 MMadr3tN7s56dB5wbnokU+eJDvhpDRP2OZISlrkYEV/PCSg28OBxkPsvUcX5qIFe2kbF5yC4ALB Y+kxxyEs6L0Ko0Tufdx9anXItr+1XacgMF/pAOOgTzw== X-Received: by 2002:a0c:a3e2:: with SMTP id v89mr3161799qvv.226.1546979297977; Tue, 08 Jan 2019 12:28:17 -0800 (PST) X-Google-Smtp-Source: ALg8bN5hDmcQX/8v8c0kzNlaifkEMblhIeb8IQvj227kevYMjzNqbQ2jIACWlxUT8rQkmMxIAr44ig== X-Received: by 2002:a0c:a3e2:: with SMTP id v89mr3161791qvv.226.1546979297739; Tue, 08 Jan 2019 12:28:17 -0800 (PST) Received: from localhost ([191.13.50.232]) by smtp.gmail.com with ESMTPSA id s24sm14099400qkl.48.2019.01.08.12.28.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jan 2019 12:28:17 -0800 (PST) From: "Guilherme G. Piccoli" To: kernel-team@lists.ubuntu.com Subject: [SRU B] [PATCH 1/4] tty: Drop tty->count on tty_reopen() failure Date: Tue, 8 Jan 2019 18:28:08 -0200 Message-Id: <20190108202811.12136-2-gpiccoli@canonical.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190108202811.12136-1-gpiccoli@canonical.com> References: <20190108202811.12136-1-gpiccoli@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: gpiccoli@canonical.com Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Dmitry Safonov BugLink: https://bugs.launchpad.net/bugs/1791758 In case of tty_ldisc_reinit() failure, tty->count should be decremented back, otherwise we will never release_tty(). Tetsuo reported that it fixes noisy warnings on tty release like: pts pts4033: tty_release: tty->count(10529) != (#fd's(7) + #kopen's(0)) Fixes: commit 892d1fa7eaae ("tty: Destroy ldisc instance on hangup") Cc: stable@vger.kernel.org # v4.6+ Cc: Greg Kroah-Hartman Cc: Jiri Slaby Reviewed-by: Jiri Slaby Tested-by: Jiri Slaby Tested-by: Mark Rutland Tested-by: Tetsuo Handa Signed-off-by: Dmitry Safonov Signed-off-by: Greg Kroah-Hartman (cherry-picked from fe32416790093b31364c08395727de17ec96ace1 upstream) Signed-off-by: Guilherme G. Piccoli --- drivers/tty/tty_io.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c index 495686cd0086..67e2ea4e7f1d 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -1255,6 +1255,7 @@ static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct * static int tty_reopen(struct tty_struct *tty) { struct tty_driver *driver = tty->driver; + int retval; if (driver->type == TTY_DRIVER_TYPE_PTY && driver->subtype == PTY_TYPE_MASTER) @@ -1268,10 +1269,14 @@ static int tty_reopen(struct tty_struct *tty) tty->count++; - if (!tty->ldisc) - return tty_ldisc_reinit(tty, tty->termios.c_line); + if (tty->ldisc) + return 0; - return 0; + retval = tty_ldisc_reinit(tty, tty->termios.c_line); + if (retval) + tty->count--; + + return retval; } /** From patchwork Tue Jan 8 20:28:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guilherme G. Piccoli" X-Patchwork-Id: 1022128 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43Z3k63ZLsz9s7h; Wed, 9 Jan 2019 07:28:30 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1ggxz6-0000i5-Px; Tue, 08 Jan 2019 20:28:24 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1ggxz5-0000hL-5Z for kernel-team@lists.ubuntu.com; Tue, 08 Jan 2019 20:28:23 +0000 Received: from mail-qk1-f197.google.com ([209.85.222.197]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1ggxz4-00082A-Pj for kernel-team@lists.ubuntu.com; Tue, 08 Jan 2019 20:28:22 +0000 Received: by mail-qk1-f197.google.com with SMTP id y27so4198364qkj.21 for ; Tue, 08 Jan 2019 12:28:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=o1cZHIiiBbOTuv7H+71WqcFznK7Gl8vzRnE/I5gyl/A=; b=Le5xE+4AMiyLdmSDHPazIIrkhQy+xRWypxqwDdQ9H/qYTmZBpa6nlbS6chMWYgeM9n CZ7y0wFoYHQ73qtFKDgrPzuXzAdhD8Zrf8VvrXYE9LykMKoDkyduEH1eYPBUMc8kl3Yp EppKgibI/tJvskbgk9xn/krOxKu/xc1AuxXA7F5co3eReC7iGBQXwopyTsaJdvrd6sEV fKuB8rZhTe8w1s0uAQy8TRxBh+pkjyDumhcK7UDUm3ayoXEOydGlRuyzg9rfm93XNpP+ 9UKS+Vn0z42JKXJQrqEi+QO5jhNXR5jCH6/WJm4LSlLdZnAy74q2ZDT9AXWn3sH+qicj aHWQ== X-Gm-Message-State: AJcUukcftqmJBrA6vTZkyKoeKaIB/D64AdWwjivZNtZFNeW8mV4F0RDI 38IWzziUgZbF7d/0UUKUII2Pe9KUbo3py0+DePfcjE0TcJzOUbsTBjNbswPjbcZA6dfv2SwzgNo pcvPJv3khsidNdv3ahReg57yM9FhP2tKJJ1TzYCdKyw== X-Received: by 2002:ac8:3fc9:: with SMTP id v9mr3076654qtk.108.1546979301729; Tue, 08 Jan 2019 12:28:21 -0800 (PST) X-Google-Smtp-Source: ALg8bN6wyctloOXGQYO2fA7WDd6Q4NgtRG7ViTOBDkYfLYHKpa4e93QTsWYQCpjFVJKFxcuiG4B22w== X-Received: by 2002:ac8:3fc9:: with SMTP id v9mr3076645qtk.108.1546979301564; Tue, 08 Jan 2019 12:28:21 -0800 (PST) Received: from localhost ([191.13.50.232]) by smtp.gmail.com with ESMTPSA id y6sm16283082qke.34.2019.01.08.12.28.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jan 2019 12:28:20 -0800 (PST) From: "Guilherme G. Piccoli" To: kernel-team@lists.ubuntu.com Subject: [SRU B/C] [PATCH 2/4] tty: Hold tty_ldisc_lock() during tty_reopen() Date: Tue, 8 Jan 2019 18:28:09 -0200 Message-Id: <20190108202811.12136-3-gpiccoli@canonical.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190108202811.12136-1-gpiccoli@canonical.com> References: <20190108202811.12136-1-gpiccoli@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: gpiccoli@canonical.com Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Dmitry Safonov BugLink: https://bugs.launchpad.net/bugs/1791758 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 tty_ldisc_reinit() should be called with ldisc_sem hold for writing, which will protect any reader against line discipline changes. Cc: Jiri Slaby Cc: stable@vger.kernel.org # b027e2298bd5 ("tty: fix data race between tty_init_dev and flush of buf") Reviewed-by: Jiri Slaby Reported-by: syzbot+3aa9784721dfb90e984d@syzkaller.appspotmail.com Tested-by: Mark Rutland Tested-by: Tetsuo Handa Signed-off-by: Dmitry Safonov Tested-by: Tycho Andersen Signed-off-by: Greg Kroah-Hartman (cherry-picked from 83d817f41070c48bc3eb7ec18e43000a548fca5c upstream) Signed-off-by: Guilherme G. Piccoli --- 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 67e2ea4e7f1d..83e57a328f2b 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); + 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; } From patchwork Tue Jan 8 20:28:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guilherme G. Piccoli" X-Patchwork-Id: 1022129 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43Z3kB3zmlz9sBQ; Wed, 9 Jan 2019 07:28:34 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1ggxzB-0000kO-0T; Tue, 08 Jan 2019 20:28:29 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1ggxz9-0000jV-BK for kernel-team@lists.ubuntu.com; Tue, 08 Jan 2019 20:28:27 +0000 Received: from mail-qt1-f198.google.com ([209.85.160.198]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1ggxz8-00082M-TX for kernel-team@lists.ubuntu.com; Tue, 08 Jan 2019 20:28:27 +0000 Received: by mail-qt1-f198.google.com with SMTP id 41so4472288qto.17 for ; Tue, 08 Jan 2019 12:28:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AQ2H88LrXaHksu0t7CLDTIwdXliiOxavre7LXQjX4X0=; b=Z05uhCoY9R8NwmHl7uPXD+R8gFk0inl7JT371crSGkJZDz4RRn5b6PJSoxRxUfzv5s b3fv38hItTtdKwsaHecpkHoQxwR6pPAJ+bK/jet6i/evZnFaPeZoCCDtMcoHWYNXReuI tVwqUHFwiDY4C2TVtMbQeH/h1IwRRPA083hi4XFCw2bHgqO6jR0EwoJp76/w3+1pHedM X6KGEvx0J4yZ12zO2iL+PN0YQZOe6AXwisv6jbVA70vRhj7U2nVU18Y4fZlgGFfkMoDc vlnVD50BPRHQcwkHicKEa4NU4BCfZtdgo3HgIduOQDriOGSZb/oFuDC4wadMjRrTps+m k0kA== X-Gm-Message-State: AJcUukeNnIhvylnGxpgeOvtCueRBlwsgKF1djSqtBkUSF3PjwbRN91Od pXHn8aSsU62ly7O2HLNWVYo2W2HT4/W2EWVG3dTHPakAYy7Rx3JcCz+vXtdVZyvtpW504pDppSG LNO8SMdJxdDzC8DFnJP9H/7HIC0H9SMzuRReUH3xHBw== X-Received: by 2002:ac8:6a06:: with SMTP id t6mr3078684qtr.330.1546979305497; Tue, 08 Jan 2019 12:28:25 -0800 (PST) X-Google-Smtp-Source: ALg8bN5sg1Zw4S4lWAs8S1oP9a3ptLG7f9T/683rLXKUijIzLQtc/26Vh/slxgHg/pIcb9DguaELTg== X-Received: by 2002:ac8:6a06:: with SMTP id t6mr3078670qtr.330.1546979305289; Tue, 08 Jan 2019 12:28:25 -0800 (PST) Received: from localhost ([191.13.50.232]) by smtp.gmail.com with ESMTPSA id q21sm39493801qtk.79.2019.01.08.12.28.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jan 2019 12:28:24 -0800 (PST) From: "Guilherme G. Piccoli" To: kernel-team@lists.ubuntu.com Subject: [SRU B/C] [PATCH 3/4] tty: Don't block on IO when ldisc change is pending Date: Tue, 8 Jan 2019 18:28:10 -0200 Message-Id: <20190108202811.12136-4-gpiccoli@canonical.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190108202811.12136-1-gpiccoli@canonical.com> References: <20190108202811.12136-1-gpiccoli@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: gpiccoli@canonical.com Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Dmitry Safonov BugLink: https://bugs.launchpad.net/bugs/1791758 There might be situations where tty_ldisc_lock() has blocked, but there is already IO on tty and it prevents line discipline changes. It might theoretically turn into dead-lock. Basically, provide more priority to pending tty_ldisc_lock() than to servicing reads/writes over tty. User-visible issue was reported by Mikulas where on pa-risc with Debian 5 reboot took either 80 seconds, 3 minutes or 3:25 after proper locking in tty_reopen(). Cc: Jiri Slaby Reported-by: Mikulas Patocka Signed-off-by: Dmitry Safonov Signed-off-by: Greg Kroah-Hartman (cherry-picked from c96cf923a98d1b094df9f0cf97a83e118817e31b upstream) Signed-off-by: Guilherme G. Piccoli --- drivers/tty/n_hdlc.c | 4 ++-- drivers/tty/n_r3964.c | 2 +- drivers/tty/n_tty.c | 8 ++++---- drivers/tty/tty_ldisc.c | 7 +++++++ include/linux/tty.h | 7 +++++++ 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/drivers/tty/n_hdlc.c b/drivers/tty/n_hdlc.c index eea7b6cb3cc4..33ee831bbd4e 100644 --- a/drivers/tty/n_hdlc.c +++ b/drivers/tty/n_hdlc.c @@ -612,7 +612,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, } /* no data */ - if (file->f_flags & O_NONBLOCK) { + if (tty_io_nonblock(tty, file)) { ret = -EAGAIN; break; } @@ -679,7 +679,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, if (tbuf) break; - if (file->f_flags & O_NONBLOCK) { + if (tty_io_nonblock(tty, file)) { error = -EAGAIN; break; } diff --git a/drivers/tty/n_r3964.c b/drivers/tty/n_r3964.c index 30bb0900cd2f..30ba5491f32e 100644 --- a/drivers/tty/n_r3964.c +++ b/drivers/tty/n_r3964.c @@ -1078,7 +1078,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file, pMsg = remove_msg(pInfo, pClient); if (pMsg == NULL) { /* no messages available. */ - if (file->f_flags & O_NONBLOCK) { + if (tty_io_nonblock(tty, file)) { ret = -EAGAIN; goto unlock; } diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 63c593a74380..31067cfd677e 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -1671,7 +1671,7 @@ n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp, down_read(&tty->termios_rwsem); - while (1) { + do { /* * When PARMRK is set, each input char may take up to 3 chars * in the read buf; reduce the buffer space avail by 3x @@ -1713,7 +1713,7 @@ n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp, fp += n; count -= n; rcvd += n; - } + } while (!test_bit(TTY_LDISC_CHANGING, &tty->flags)); tty->receive_room = room; @@ -2188,7 +2188,7 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, break; if (!timeout) break; - if (file->f_flags & O_NONBLOCK) { + if (tty_io_nonblock(tty, file)) { retval = -EAGAIN; break; } @@ -2342,7 +2342,7 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, } if (!nr) break; - if (file->f_flags & O_NONBLOCK) { + if (tty_io_nonblock(tty, file)) { retval = -EAGAIN; break; } diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c index 0fd18f5fa67d..48fe8d9b6df9 100644 --- a/drivers/tty/tty_ldisc.c +++ b/drivers/tty/tty_ldisc.c @@ -340,6 +340,11 @@ int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) { int ret; + /* Kindly asking blocked readers to release the read side */ + set_bit(TTY_LDISC_CHANGING, &tty->flags); + wake_up_interruptible_all(&tty->read_wait); + wake_up_interruptible_all(&tty->write_wait); + ret = __tty_ldisc_lock(tty, timeout); if (!ret) return -EBUSY; @@ -350,6 +355,8 @@ int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) void tty_ldisc_unlock(struct tty_struct *tty) { clear_bit(TTY_LDISC_HALTED, &tty->flags); + /* Can be cleared here - ldisc_unlock will wake up writers firstly */ + clear_bit(TTY_LDISC_CHANGING, &tty->flags); __tty_ldisc_unlock(tty); } diff --git a/include/linux/tty.h b/include/linux/tty.h index 1dd587ba6d88..0cd621d8c7f0 100644 --- a/include/linux/tty.h +++ b/include/linux/tty.h @@ -365,6 +365,7 @@ struct tty_file_private { #define TTY_NO_WRITE_SPLIT 17 /* Preserve write boundaries to driver */ #define TTY_HUPPED 18 /* Post driver->hangup() */ #define TTY_HUPPING 19 /* Hangup in progress */ +#define TTY_LDISC_CHANGING 20 /* Change pending - non-block IO */ #define TTY_LDISC_HALTED 22 /* Line discipline is halted */ /* Values for tty->flow_change */ @@ -382,6 +383,12 @@ static inline void tty_set_flow_change(struct tty_struct *tty, int val) smp_mb(); } +static inline bool tty_io_nonblock(struct tty_struct *tty, struct file *file) +{ + return file->f_flags & O_NONBLOCK || + test_bit(TTY_LDISC_CHANGING, &tty->flags); +} + static inline bool tty_io_error(struct tty_struct *tty) { return test_bit(TTY_IO_ERROR, &tty->flags); From patchwork Tue Jan 8 20:28:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guilherme G. Piccoli" X-Patchwork-Id: 1022130 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43Z3kF05Jxz9s7h; Wed, 9 Jan 2019 07:28:37 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1ggxzD-0000m3-5P; Tue, 08 Jan 2019 20:28:31 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1ggxzC-0000lL-57 for kernel-team@lists.ubuntu.com; Tue, 08 Jan 2019 20:28:30 +0000 Received: from mail-qt1-f199.google.com ([209.85.160.199]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1ggxzB-00082Z-Qr for kernel-team@lists.ubuntu.com; Tue, 08 Jan 2019 20:28:29 +0000 Received: by mail-qt1-f199.google.com with SMTP id n95so4515417qte.16 for ; Tue, 08 Jan 2019 12:28:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2HIqAIoDWVwEkeh9pIlJ2HE2CfKkGKMZJi+wEQpaunM=; b=dkKSoUXJIfHDWBpN2SEnjURIA3CgzSIZjJ+5gK4GRdHs5Eq4xVkA4FYuzNjSE6iVZ/ 0lpTuf95lbJ0XsmYu2Vy5pK3neLoJxrDsu4aVOTgRS0cU5lQ5v5s2xosdSyo1hMkVgBV IGYNc7j/RYlGbHEaP96KRHRJFJWnoysTI37Cq2NRa/yuKF1FTWvXFxDqXLM+8XnL/ZB2 fAskiizkoK3Q+WirXrY7+tXRDYJj+enXz8aKxameFqiTxo8t7gOL9Ea+aR2RZfrpOhSN xWOV5jNLDvpwpUkDPva224MZ3ngXaqXCvQxT9sR+d+mr/UmB9PcaRgwMzbKQfEiCkOvS PVjg== X-Gm-Message-State: AJcUuke7CSLgHMuwfANCBgY0xU5rKYb86D9jx5h10toHaK9tJYKa8/sS VkICA/A7+S7dlg/NQbJyyicZrJL2j++EoaRLtRdlmLbbZE5aY/taI7zBW2SiiBrhTD639RBCH1c 1Y7Ud/3Oig4JQbTbo5XUdaEzYO7DK3xZNyTqhBd5vHg== X-Received: by 2002:a37:6b42:: with SMTP id g63mr2868238qkc.297.1546979308744; Tue, 08 Jan 2019 12:28:28 -0800 (PST) X-Google-Smtp-Source: ALg8bN7TS9h6RFJJ8t7uboj3tVzDZtQZ82nG0d0jSd8CC7wu4Ftx9HH1sYpzatF0aITJVAAlHtbT7A== X-Received: by 2002:a37:6b42:: with SMTP id g63mr2868232qkc.297.1546979308563; Tue, 08 Jan 2019 12:28:28 -0800 (PST) Received: from localhost ([191.13.50.232]) by smtp.gmail.com with ESMTPSA id a20sm12641907qkj.28.2019.01.08.12.28.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 08 Jan 2019 12:28:27 -0800 (PST) From: "Guilherme G. Piccoli" To: kernel-team@lists.ubuntu.com Subject: [SRU B/C] [PATCH 4/4] tty: Simplify tty->count math in tty_reopen() Date: Tue, 8 Jan 2019 18:28:11 -0200 Message-Id: <20190108202811.12136-5-gpiccoli@canonical.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20190108202811.12136-1-gpiccoli@canonical.com> References: <20190108202811.12136-1-gpiccoli@canonical.com> MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: gpiccoli@canonical.com Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Dmitry Safonov BugLink: https://bugs.launchpad.net/bugs/1791758 As notted by Jiri, tty_ldisc_reinit() shouldn't rely on tty counter. Simplify math by increasing the counter after reinit success. Cc: Jiri Slaby Link: lkml.kernel.org/r/<20180829022353.23568-2-dima@arista.com> Suggested-by: Jiri Slaby Reviewed-by: Jiri Slaby Tested-by: Mark Rutland Signed-off-by: Dmitry Safonov Signed-off-by: Greg Kroah-Hartman (cherry-picked from cf62a1a13749db0d32b5cdd800ea91a4087319de upstream) Signed-off-by: Guilherme G. Piccoli --- drivers/tty/tty_io.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c index 83e57a328f2b..b273aeadbb51 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -1271,16 +1271,13 @@ static int tty_reopen(struct tty_struct *tty) if (retval) return retval; - tty->count++; - if (tty->ldisc) - goto out_unlock; - - retval = tty_ldisc_reinit(tty, tty->termios.c_line); - if (retval) - tty->count--; - -out_unlock: + if (!tty->ldisc) + retval = tty_ldisc_reinit(tty, tty->termios.c_line); tty_ldisc_unlock(tty); + + if (retval == 0) + tty->count++; + return retval; }