public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Wang YanQing <udknight@gmail.com>
To: gregkh@linuxfoundation.org
Cc: jslaby@suse.com, linux-kernel@vger.kernel.org, mikey@neuling.org,
	viro@ZenIV.linux.org.uk, johan@kernel.org,
	peter@hurleysoftware.com, alex.popov@linux.com, robh@kernel.org,
	mpatocka@redhat.com, dvyukov@google.com,
	benh@kernel.crashing.org
Subject: [PATCH] tty:tty_ldisc: add tty_ldisc_lock|unlock to prevent concurrent update to ldisc in tty_ldisc_reinit
Date: Mon, 10 Apr 2017 00:52:27 +0800	[thread overview]
Message-ID: <20170409165227.GA7945@udknight> (raw)

This patch could fix the issue that tty_reopen in tty_io
calling tty_ldisc_reinit without holding tty->ldisc_sem.

Signed-off-by: Wang YanQing <udknight@gmail.com>
---
 drivers/tty/tty_ldisc.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c
index b0500a0..b1f7fa5 100644
--- a/drivers/tty/tty_ldisc.c
+++ b/drivers/tty/tty_ldisc.c
@@ -616,6 +616,10 @@ int tty_ldisc_reinit(struct tty_struct *tty, int disc)
 	if (IS_ERR(ld))
 		return PTR_ERR(ld);
 
+	/*
+	 * Avoid racing set_ldisc or tty_ldisc_release
+	 */
+	tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
 	if (tty->ldisc) {
 		tty_ldisc_close(tty, tty->ldisc);
 		tty_ldisc_put(tty->ldisc);
@@ -629,6 +633,7 @@ int tty_ldisc_reinit(struct tty_struct *tty, int disc)
 		tty_ldisc_put(tty->ldisc);
 		tty->ldisc = NULL;
 	}
+	tty_ldisc_unlock(tty);
 	return retval;
 }
 
@@ -672,22 +677,18 @@ void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
 	/*
 	 * Shutdown the current line discipline, and reset it to
 	 * N_TTY if need be.
-	 *
-	 * Avoid racing set_ldisc or tty_ldisc_release
 	 */
-	tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
-
 	if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
 		tty_reset_termios(tty);
 
-	if (tty->ldisc) {
-		if (reinit) {
-			if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0)
-				tty_ldisc_reinit(tty, N_TTY);
-		} else
-			tty_ldisc_kill(tty);
+	if (reinit) {
+		if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0)
+			tty_ldisc_reinit(tty, N_TTY);
+	} else {
+		tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
+		tty_ldisc_kill(tty);
+		tty_ldisc_unlock(tty);
 	}
-	tty_ldisc_unlock(tty);
 }
 
 /**
-- 
1.8.5.6.2.g3d8a54e.dirty

                 reply	other threads:[~2017-04-09 16:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20170409165227.GA7945@udknight \
    --to=udknight@gmail.com \
    --cc=alex.popov@linux.com \
    --cc=benh@kernel.crashing.org \
    --cc=dvyukov@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=johan@kernel.org \
    --cc=jslaby@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mikey@neuling.org \
    --cc=mpatocka@redhat.com \
    --cc=peter@hurleysoftware.com \
    --cc=robh@kernel.org \
    --cc=viro@ZenIV.linux.org.uk \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox