mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alan Cox <alan@lxorguk.ukuu.org.uk>
To: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	"Rafael J. Wysocki" <rjw@sisk.pl>, Ray Lee <ray-lk@madrabbit.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [Regression] kdesu broken
Date: Mon, 27 Jul 2009 11:57:23 +0100	[thread overview]
Message-ID: <20090727115723.1e8de60e@lxorguk.ukuu.org.uk> (raw)
In-Reply-To: <87bpn7mzli.fsf@devron.myhome.or.jp>

> I see. It sounds like good thing. The attached patch or something?
> Anyway, I'm not familiar with the tty stuff obviously, so, I'm not sure
> whether this patch is right or not.

It turns out to be a little bit trickier than I thought because of open
v close v flush_to_ldisc races (some of the open/close ones being long
standing).

We now use tty->buf.lock to keep EOF/EOFPENDING/OTHER_CLOSED all in order
together. That has no real cost as we already hold the buf.lock in the hot
path which is flush_to_ldisc.

Anyway this is my current thoughts (not yet given a testing)


diff --git a/drivers/char/n_tty.c b/drivers/char/n_tty.c
index ff47907..acae995 100644
--- a/drivers/char/n_tty.c
+++ b/drivers/char/n_tty.c
@@ -1777,7 +1777,8 @@ do_it_again:
 			tty->minimum_to_wake = (minimum - (b - buf));
 
 		if (!input_available_p(tty, 0)) {
-			if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
+			if (test_bit(TTY_EOF, &tty->flags)) {
+				/* PTY pair closed and all data consumed */
 				retval = -EIO;
 				break;
 			}
diff --git a/drivers/char/pty.c b/drivers/char/pty.c
index 6e6942c..de10cc0 100644
--- a/drivers/char/pty.c
+++ b/drivers/char/pty.c
@@ -38,6 +38,9 @@ static struct tty_driver *pts_driver;
 
 static void pty_close(struct tty_struct *tty, struct file *filp)
 {
+	struct tty_struct *pair;
+	unsigned long flags;
+
 	BUG_ON(!tty);
 	if (tty->driver->subtype == PTY_TYPE_MASTER)
 		WARN_ON(tty->count > 1);
@@ -47,13 +50,22 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
 	}
 	wake_up_interruptible(&tty->read_wait);
 	wake_up_interruptible(&tty->write_wait);
+	
 	tty->packet = 0;
-	if (!tty->link)
+	pair = tty->link;
+	if (!pair)
 		return;
-	tty->link->packet = 0;
-	set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
-	wake_up_interruptible(&tty->link->read_wait);
-	wake_up_interruptible(&tty->link->write_wait);
+
+	spin_lock_irqsave(&pair->buf.lock, flags);
+	pair->packet = 0;
+	/* Indicate that the other end is now closed, set the
+	   ENDPENDING marker so that the true end can be processed by
+	   the line discipline */
+	set_bit(TTY_EOFPENDING, &pair->flags);
+	set_bit(TTY_OTHER_CLOSED, &pair->flags);
+	spin_unlock_irqrestore(&pair->buf.lock, flags);
+	wake_up_interruptible(&pair->read_wait);
+	wake_up_interruptible(&pair->write_wait);
 	if (tty->driver->subtype == PTY_TYPE_MASTER) {
 		set_bit(TTY_OTHER_CLOSED, &tty->flags);
 #ifdef CONFIG_UNIX98_PTYS
@@ -180,7 +192,6 @@ static void pty_flush_buffer(struct tty_struct *tty)
 
 	if (!to)
 		return;
-	/* tty_buffer_flush(to); FIXME */
 	if (to->packet) {
 		spin_lock_irqsave(&tty->ctrl_lock, flags);
 		tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
@@ -191,23 +202,30 @@ static void pty_flush_buffer(struct tty_struct *tty)
 
 static int pty_open(struct tty_struct *tty, struct file *filp)
 {
-	int	retval = -ENODEV;
+	int	retval = -EIO;
+	unsigned long flags;
+	struct tty_struct *pair;
 
-	if (!tty || !tty->link)
-		goto out;
+	if (tty == NULL || (pair = tty->link) == NULL)
+		return -ENODEV;
 
-	retval = -EIO;
 	if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
+		return -EIO;
+	spin_lock_irqsave(&pair->buf.lock, flags);
+	if (test_bit(TTY_PTY_LOCK, &pair->flags))
 		goto out;
-	if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
-		goto out;
-	if (tty->link->count != 1)
+	if (pair->count != 1)
 		goto out;
 
-	clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
+	clear_bit(TTY_OTHER_CLOSED, &pair->flags);
+	/* The buf.lock stops this racing a flush_to_ldisc from
+	   the other end */
+	clear_bit(TTY_EOFPENDING, &pair->flags);
+	clear_bit(TTY_EOF, &pair->flags);
 	set_bit(TTY_THROTTLED, &tty->flags);
 	retval = 0;
 out:
+	spin_unlock_irqrestore(&pair->buf.lock, flags);
 	return retval;
 }
 
diff --git a/drivers/char/tty_buffer.c b/drivers/char/tty_buffer.c
index 810ee25..448cd1d 100644
--- a/drivers/char/tty_buffer.c
+++ b/drivers/char/tty_buffer.c
@@ -119,6 +119,12 @@ static void __tty_buffer_flush(struct tty_struct *tty)
 		tty_buffer_free(tty, thead);
 	}
 	tty->buf.tail = NULL;
+	/* We can EOF a pty/tty pair with a flush as well as by consuming
+	   all the data left over */
+	if (test_bit(TTY_EOFPENDING, &tty->flags)) {
+		set_bit(TTY_EOF, &tty->flags);
+		wake_up(&tty->read_wait);
+	}
 }
 
 /**
@@ -455,6 +461,10 @@ static void flush_to_ldisc(struct work_struct *work)
 		clear_bit(TTY_FLUSHPENDING, &tty->flags);
 		wake_up(&tty->read_wait);
 	}
+	if (tty->buf.head == NULL && test_bit(TTY_EOFPENDING, &tty->flags)) {
+		wake_up(&tty->read_wait);
+		set_bit(TTY_EOF, &tty->flags);
+	}
 	clear_bit(TTY_FLUSHING, &tty->flags);
 	spin_unlock_irqrestore(&tty->buf.lock, flags);
 
diff --git a/include/linux/tty.h b/include/linux/tty.h
index 85aa525..427d107 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -321,6 +321,8 @@ struct tty_struct {
 #define TTY_LDISC 		9	/* Line discipline attached */
 #define TTY_LDISC_CHANGING 	10	/* Line discipline changing */
 #define TTY_LDISC_OPEN	 	11	/* Line discipline is open */
+#define TTY_EOF			12	/* TTY/PTY pair at EOF */
+#define TTY_EOFPENDING		13	/* TTY/PTY pair EOF when data emptied */
 #define TTY_HW_COOK_OUT 	14	/* Hardware can do output cooking */
 #define TTY_HW_COOK_IN 		15	/* Hardware can do input cooking */
 #define TTY_PTY_LOCK 		16	/* pty private */

  reply	other threads:[~2009-07-27 10:56 UTC|newest]

Thread overview: 104+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-23 23:45 Rafael J. Wysocki
2009-07-24  0:21 ` Ray Lee
2009-07-24 15:21   ` Rafael J. Wysocki
2009-07-24 15:40     ` Alan Cox
2009-07-24 16:34       ` Linus Torvalds
2009-07-25  6:04         ` OGAWA Hirofumi
2009-07-25 13:31           ` Alan Cox
2009-07-25 14:05           ` Alan Cox
2009-07-25 14:55             ` OGAWA Hirofumi
2009-07-25 15:32               ` Alan Cox
2009-07-26 11:51                 ` OGAWA Hirofumi
2009-07-27 10:57                   ` Alan Cox [this message]
2009-07-27 12:07                     ` OGAWA Hirofumi
2009-07-27 12:46                       ` OGAWA Hirofumi
2009-07-27 13:23                       ` [PATCH] " Alan Cox
2009-07-27 13:50                         ` OGAWA Hirofumi
2009-07-27 13:58                           ` Alan Cox
2009-07-27 15:04                             ` OGAWA Hirofumi
2009-07-27 16:14                               ` Aneesh Kumar K.V
2009-07-27 16:42                                 ` Alan Cox
2009-07-27 17:12                                   ` Aneesh Kumar K.V
2009-07-27 19:28                                     ` OGAWA Hirofumi
2009-07-27 19:40                                       ` Linus Torvalds
2009-07-27 20:38                                         ` OGAWA Hirofumi
2009-07-27 20:45                                           ` Linus Torvalds
2009-07-27 21:42                                           ` Alan Cox
2009-07-27 22:04                                             ` Linus Torvalds
2009-07-27 22:41                                               ` Alan Cox
2009-07-27 20:52                                         ` Alan Cox
2009-07-27 21:22                                           ` Linus Torvalds
2009-07-27 21:54                                             ` Alan Cox
2009-07-27 21:20                                       ` Alan Cox
2009-07-28  5:33                                         ` OGAWA Hirofumi
2009-07-28 10:22                                           ` Alan Cox
2009-07-28 10:42                                             ` OGAWA Hirofumi
2009-07-28 15:49                                             ` Linus Torvalds
2009-07-28 16:42                                               ` Alan Cox
2009-07-28 16:49                                                 ` Linus Torvalds
2009-07-28 16:52                                                   ` Linus Torvalds
2009-07-28 17:09                                                     ` Alan Cox
2009-07-28 18:45                                                       ` Linus Torvalds
2009-07-28 17:06                                                   ` Alan Cox
2009-07-28 18:44                                                     ` Linus Torvalds
2009-07-28 18:56                                                       ` Alan Cox
2009-07-28 19:08                                                         ` Linus Torvalds
2009-07-28 19:15                                                           ` Alan Cox
2009-07-28 19:56                                                             ` Greg KH
2009-07-28 20:47                                                               ` Theodore Tso
2009-07-28 21:01                                                                 ` Greg KH
2009-07-28 22:02                                                                   ` Theodore Tso
2009-07-28 23:49                                                                     ` Alan Cox
2009-07-29  0:12                                                                       ` Greg KH
2009-07-30 23:16                                                                       ` Alan Cox
2009-07-30 23:24                                                                         ` Greg KH
2009-07-31 13:49                                                                       ` Alan Cox
2009-07-31 14:17                                                                         ` Greg KH
2009-07-28 23:46                                                           ` Alan Cox
2009-07-29  0:10                                                             ` Linus Torvalds
2009-07-29  0:26                                                               ` Linus Torvalds
2009-07-29  7:01                                                                 ` Aneesh Kumar K.V
2009-07-29  0:34                                                               ` Alan Cox
2009-07-29  1:04                                                                 ` Linus Torvalds
2009-07-29  1:23                                                                   ` Linus Torvalds
2009-07-29 11:17                                                                     ` Alan Cox
2009-07-29  8:59                                                                   ` Alan Cox
2009-07-29 15:48                                                                     ` Linus Torvalds
2009-07-29 15:55                                                                       ` Alan Cox
2009-07-29 16:05                                                                         ` Linus Torvalds
2009-07-29 16:39                                                                           ` Alan Cox
2009-07-29 19:07                                                                             ` Linus Torvalds
2009-07-29  2:50                                                               ` Gene Heskett
2009-07-29  4:49                                                                 ` Linus Torvalds
2009-07-29  4:54                                                                   ` Linus Torvalds
2009-07-29  5:04                                                                     ` Gene Heskett
2009-07-29  5:00                                                                   ` Gene Heskett
2009-07-29  5:08                                                                     ` Andrew Morton
2009-07-29  7:46                                                                       ` Gene Heskett
2009-07-29 11:07                                                                   ` Alan Cox
2009-07-29 17:40                                                                     ` Gene Heskett
2009-07-29 18:28                                                                       ` Frans Pop
2009-07-29 18:43                                                                         ` Gene Heskett
2009-07-29 19:08                                                                           ` Frans Pop
2009-07-29 19:19                                                                             ` Gene Heskett
2009-07-30 12:43                                                                               ` Valdis.Kletnieks
2009-07-30 15:35                                                                                 ` Gene Heskett
2009-07-30 18:39                                                                                   ` Valdis.Kletnieks
2009-07-31  2:01                                                                                   ` H. Peter Anvin
2009-07-28 15:48                                           ` Linus Torvalds
2009-07-28 16:16                                             ` OGAWA Hirofumi
2009-07-27 18:28                                 ` Andreas Schwab
2009-07-27 13:58                         ` Aneesh Kumar K.V
2009-07-25 20:12             ` [Regression] " Rafael J. Wysocki
2009-07-26 17:41             ` Aneesh Kumar K.V
2009-07-29  2:20             ` Gene Heskett
2009-07-25 11:48         ` Alan Cox
2009-07-25 14:02           ` Frans Pop
2009-07-25 20:16             ` Rafael J. Wysocki
2009-07-25 21:03               ` Alan Cox
2009-07-26 15:51             ` Sergey Senozhatsky
2009-07-24 18:25   ` Aneesh Kumar K.V
2009-07-25 12:07     ` Alan Cox
2009-07-25 16:18       ` Aneesh Kumar K.V
2009-07-25 17:06         ` Aneesh Kumar K.V
2009-07-29 19:09     ` [Regression] kdesu broken, now usb fixed in current git pull Gene Heskett

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=20090727115723.1e8de60e@lxorguk.ukuu.org.uk \
    --to=alan@lxorguk.ukuu.org.uk \
    --cc=akpm@linux-foundation.org \
    --cc=hirofumi@mail.parknet.co.jp \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ray-lk@madrabbit.org \
    --cc=rjw@sisk.pl \
    --cc=torvalds@linux-foundation.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®