All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arseny Maslennikov <ar@cs.msu.ru>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Slaby <jslaby@suse.com>,
	linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Arseny Maslennikov <ar@cs.msu.ru>, Rob Landley <rob@landley.net>,
	"Eric W. Biederman" <ebiederm@xmission.com>,
	Pavel Machek <pavel@ucw.cz>,
	linux-api@vger.kernel.org,
	"Vladimir D. Seleznev" <vseleznv@altlinux.org>
Subject: [PATCH v3 6/7] n_tty: ->ops->write: Cut core logic out to a separate function
Date: Thu, 30 Apr 2020 09:43:00 +0300	[thread overview]
Message-ID: <20200430064301.1099452-7-ar@cs.msu.ru> (raw)
In-Reply-To: <20200430064301.1099452-1-ar@cs.msu.ru>

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 <ar@cs.msu.ru>
---
 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 10d6b60a5..f72a3fd4b 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 cafb9b7f7..3499845ab 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -390,7 +390,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.26.2


  parent reply	other threads:[~2020-04-30  7:17 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-30  6:42 [PATCH v3 0/7] TTY Keyboard Status Request Arseny Maslennikov
2020-04-30  6:42 ` [PATCH v3 1/7] signal.h: Define SIGINFO on all architectures Arseny Maslennikov
2020-04-30  6:42 ` [PATCH v3 2/7] tty: termios: Reserve space for VSTATUS in .c_cc Arseny Maslennikov
2020-04-30  6:42 ` [PATCH v3 3/7] n_tty: Send SIGINFO to fg pgrp on status request character Arseny Maslennikov
2020-04-30  6:42 ` [PATCH v3 4/7] linux/signal.h: Ignore SIGINFO by default in new tasks Arseny Maslennikov
2020-04-30  6:53   ` Jiri Slaby
2020-04-30  7:14     ` Christian Brauner
2020-04-30  7:19       ` Greg Kroah-Hartman
2020-04-30  7:37       ` Aleksa Sarai
2020-04-30  8:00         ` Christian Brauner
2020-04-30  6:42 ` [PATCH v3 5/7] tty: Add NOKERNINFO lflag to termios Arseny Maslennikov
2020-04-30  6:55   ` Jiri Slaby
2020-04-30  7:20     ` Arseny Maslennikov
2020-04-30  6:43 ` Arseny Maslennikov [this message]
2020-04-30  6:43 ` [PATCH v3 7/7] n_tty: Provide an informational line on VSTATUS receipt Arseny Maslennikov
2020-04-30  7:29   ` Jiri Slaby
2020-04-30  9:08     ` Arseny Maslennikov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200430064301.1099452-7-ar@cs.msu.ru \
    --to=ar@cs.msu.ru \
    --cc=ebiederm@xmission.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jslaby@suse.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=pavel@ucw.cz \
    --cc=rob@landley.net \
    --cc=vseleznv@altlinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.