Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp6493245ybi; Wed, 5 Jun 2019 01:42:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqzB5pvpe/YnxOa+e8wY3Y06UOe5qy0DWGJtGS+D5V8HFEJXO5HebR2D12KZDD15SLDCdR0T X-Received: by 2002:a62:6341:: with SMTP id x62mr44949039pfb.63.1559724131913; Wed, 05 Jun 2019 01:42:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559724131; cv=none; d=google.com; s=arc-20160816; b=to/gR8rUBAx+FFk/S96itxk3jXrx6SFCUyB7MTKC8N0iNfvUJkK1Nb5HDfTfVNuc5C zNpgmKDsDcFC1L4Xnw+z+tWGSPT1xo1ipSUusgXoabSrAbJzfcUTNknDML0sVls8JPqp TW8lgSb9nJtm2WsqRWyFiK/R74sZDCwrhwnoE6kA0Z4t2O9qkwkHIbILi5kVMOLr+Mcd drKSgY6cmdMLQRjg75+nJ6ktulcxaYXrlqB7Y/AY1Wqr18sKRGvGAyR8B8exdmaTm4D7 mfaas0OZmLK8F/w3TC11lqaD4DFYKBBcWuB0P5p0Pr6TDYYqpwSNXIV4qVzgccTe8IkI gpig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:cc:to:from :dkim-signature; bh=JBtc314UesgooPBehJ4MgN+ovha9N0zjMaGxUg7Avfs=; b=ukT3nvoVkKFmiBOv0O1yz5opxZTsaeB96xKvlAJaJtaTP+RLyfrx4N4WgYCtQXWgR+ 22sUOca8iC52M3wTBu5OJ7iks2yk+Qq2sgIbyEgD/Dft9ensiHrqlXmybEdCZl+FR0HA T3KzBWSXAkANl4vssyuBDHbxhRQYiInk4cl0RvL3kOQvKP2gX2rMmwRwYpNQCqQS7HO0 63+g0Y/NCMgj0O8pHfPW9ornwotUbXBEmRAGVmKh9aML/btYQQvlDT5cI5ul4OHGoP6q W3V7RM3ATqOgi9NdgWQjU/eTW/6HrjpLgWLd7l7wl/uYmH831EXaS8p7VjXHfOW7uV+o dhvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@cs.msu.ru header.s=dkim header.b=ZMlCrKZB; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=cs.msu.ru Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x19si26110432pgj.92.2019.06.05.01.41.55; Wed, 05 Jun 2019 01:42:11 -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=fail header.i=@cs.msu.ru header.s=dkim header.b=ZMlCrKZB; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=cs.msu.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726738AbfFEIkp (ORCPT + 99 others); Wed, 5 Jun 2019 04:40:45 -0400 Received: from mx.cs.msu.ru ([188.44.42.42]:51120 "EHLO mail.cs.msu.ru" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726636AbfFEIko (ORCPT ); Wed, 5 Jun 2019 04:40:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=cs.msu.ru; s=dkim; h=Subject:Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=JBtc314UesgooPBehJ4MgN+ovha9N0zjMaGxUg7Avfs=; b=ZMlCrKZBdN2eB6AijUgU4Mgf1O 82mHsoM2IbV5rxn9iCL5JWbICQQrj9ZK7ObCGZBJE6SJKmyaa9CFkmQMJ/aUKLzsVJvRsWDcsf6VU QZPrhWMEB8pFMM/GDs++eeHS1qHO13GHqsIcg3WwD4JuU+Z98E941D/NMZ64gqXEsO2DiMEcTy91y RZOAbmCAa3Gza9x8h5YfNAE+2noTawb8+5UznHfn/p/VEA+5A3Uikya4tISmlH4NQUtIBiIp4vnNm pSSpSLL9nTNSAHA1Xsyke7etkZ8qAavhHRHRqYQ4P5QOgJRiH6NymV0TCuRZFI3S3i6DSvictKkl4 Y9CWPNpQ==; Received: from [37.204.119.143] (port=55354 helo=localhost.localdomain) by mail.cs.msu.ru with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92 (FreeBSD)) (envelope-from ) id 1hYR9U-0008s7-Uv; Wed, 05 Jun 2019 11:20:11 +0300 From: Arseny Maslennikov To: Greg Kroah-Hartman , Jiri Slaby , Ingo Molnar , Peter Zijlstra , linux-kernel@vger.kernel.org Cc: "Vladimir D . Seleznev" , Arseny Maslennikov Date: Wed, 5 Jun 2019 11:19:05 +0300 Message-Id: <20190605081906.28938-7-ar@cs.msu.ru> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190605081906.28938-1-ar@cs.msu.ru> References: <20190605081906.28938-1-ar@cs.msu.ru> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 37.204.119.143 X-SA-Exim-Mail-From: ar@cs.msu.ru Subject: [PATCH 6/7] n_tty: ->ops->write: Cut core logic out to a separate function X-SA-Exim-Version: 4.2 X-SA-Exim-Scanned: No (on mail.cs.msu.ru); Unknown failure Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To simplify internal re-use of the line discipline's write method, we isolate the work it does to its own function. Since in-kernel callers might not refer to the tty through a file, the struct file* argument might make no sense, so we also stop tty_io_nonblock() from dereferencing file too early, allowing to pass NULL as the referring file here. Signed-off-by: Arseny Maslennikov --- drivers/tty/n_tty.c | 33 ++++++++++++++++++++++----------- include/linux/tty.h | 2 +- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 29f33798a6cd..7d7d84adcffe 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -2309,22 +2309,15 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, * lock themselves) */ -static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, - const unsigned char *buf, size_t nr) +static ssize_t do_n_tty_write(struct tty_struct *tty, struct file *file, + const unsigned char *buf, size_t nr) { const unsigned char *b = buf; DEFINE_WAIT_FUNC(wait, woken_wake_function); int c; ssize_t retval = 0; - /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ - if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) { - retval = tty_check_change(tty); - if (retval) - return retval; - } - - down_read(&tty->termios_rwsem); + lockdep_assert_held_read(&tty->termios_rwsem); /* Write out any echoed characters that are still pending */ process_echoes(tty); @@ -2392,10 +2385,28 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, remove_wait_queue(&tty->write_wait, &wait); if (nr && tty->fasync) set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); - up_read(&tty->termios_rwsem); return (b - buf) ? b - buf : retval; } +static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, + const unsigned char *buf, size_t nr) +{ + ssize_t retval = 0; + + /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ + if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) { + retval = tty_check_change(tty); + if (retval) + return retval; + } + + down_read(&tty->termios_rwsem); + retval = do_n_tty_write(tty, file, buf, nr); + up_read(&tty->termios_rwsem); + + return retval; +} + /** * n_tty_poll - poll method for N_TTY * @tty: terminal device diff --git a/include/linux/tty.h b/include/linux/tty.h index 07189f18f93d..ddb97704956d 100644 --- a/include/linux/tty.h +++ b/include/linux/tty.h @@ -388,7 +388,7 @@ static inline void tty_set_flow_change(struct tty_struct *tty, int val) static inline bool tty_io_nonblock(struct tty_struct *tty, struct file *file) { - return file->f_flags & O_NONBLOCK || + return (file && file->f_flags & O_NONBLOCK) || test_bit(TTY_LDISC_CHANGING, &tty->flags); } -- 2.20.1