tty: Flush ldisc buffer atomically with tty flip buffers
authorPeter Hurley <peter@hurleysoftware.com>
Wed, 5 Nov 2014 17:13:09 +0000 (12:13 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 6 Nov 2014 02:50:43 +0000 (18:50 -0800)
tty_ldisc_flush() first clears the line discipline input buffer,
then clears the tty flip buffers. However, this allows for existing
data in the tty flip buffers to be added after the ldisc input
buffer has been cleared, but before the flip buffers have been cleared.

Add an optional ldisc parameter to tty_buffer_flush() to allow
tty_ldisc_flush() to pass the ldisc to clear.

NB: Initially, the plan was to do this automatically in
tty_buffer_flush(). However, an audit of the behavior of existing
line disciplines showed that performing a ldisc buffer flush on
ioctl(TCFLSH) was not always the outcome. For example, some line
disciplines have flush_buffer() methods but not ioctl() methods,
so a ->flush_buffer() command would be unexpected.

Reviewed-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/tty_buffer.c
drivers/tty/tty_io.c
drivers/tty/tty_ldisc.c
include/linux/tty.h

index 143deb62467d57a1f1216d3f418c2bfccb72201c..3605103fc1ac6d891f255fea127f6fbd8b6ad96b 100644 (file)
@@ -202,14 +202,16 @@ static void tty_buffer_free(struct tty_port *port, struct tty_buffer *b)
 /**
  *     tty_buffer_flush                -       flush full tty buffers
  *     @tty: tty to flush
+ *     @ld:  optional ldisc ptr (must be referenced)
  *
- *     flush all the buffers containing receive data.
+ *     flush all the buffers containing receive data. If ld != NULL,
+ *     flush the ldisc input buffer.
  *
  *     Locking: takes buffer lock to ensure single-threaded flip buffer
  *              'consumer'
  */
 
-void tty_buffer_flush(struct tty_struct *tty)
+void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld)
 {
        struct tty_port *port = tty->port;
        struct tty_bufhead *buf = &port->buf;
@@ -223,6 +225,10 @@ void tty_buffer_flush(struct tty_struct *tty)
                buf->head = next;
        }
        buf->head->read = buf->head->commit;
+
+       if (ld && ld->ops->flush_buffer)
+               ld->ops->flush_buffer(tty);
+
        atomic_dec(&buf->priority);
        mutex_unlock(&buf->lock);
 }
index 4ecee2856eceb6a24ca1f4c6afeaaa6f6b14db76..aa83cd1bf07161c95dd1ea9f46cce878129f3589 100644 (file)
@@ -2890,7 +2890,7 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
                case TCIFLUSH:
                case TCIOFLUSH:
                /* flush tty buffer and allow ldisc to process ioctl */
-                       tty_buffer_flush(tty);
+                       tty_buffer_flush(tty, NULL);
                        break;
                }
                break;
index 6368dd95e13740ffd52f4a54e65ed11179ca41b0..b66a81d0549e3171420ae948ec3a24ef74987836 100644 (file)
@@ -397,19 +397,17 @@ static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
  *     tty_ldisc_flush -       flush line discipline queue
  *     @tty: tty
  *
- *     Flush the line discipline queue (if any) for this tty. If there
- *     is no line discipline active this is a no-op.
+ *     Flush the line discipline queue (if any) and the tty flip buffers
+ *     for this tty.
  */
 
 void tty_ldisc_flush(struct tty_struct *tty)
 {
        struct tty_ldisc *ld = tty_ldisc_ref(tty);
-       if (ld) {
-               if (ld->ops->flush_buffer)
-                       ld->ops->flush_buffer(tty);
+
+       tty_buffer_flush(tty, ld);
+       if (ld)
                tty_ldisc_deref(ld);
-       }
-       tty_buffer_flush(tty);
 }
 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
 
index e33ea1a0c2d0e2403f4d5390696e6152519cc21f..f6835ea1079a2a4e1cd54d1058803438d6f33c7e 100644 (file)
@@ -441,7 +441,7 @@ extern void __do_SAK(struct tty_struct *tty);
 extern void no_tty(void);
 extern void tty_flush_to_ldisc(struct tty_struct *tty);
 extern void tty_buffer_free_all(struct tty_port *port);
-extern void tty_buffer_flush(struct tty_struct *tty);
+extern void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld);
 extern void tty_buffer_init(struct tty_port *port);
 extern speed_t tty_termios_baud_rate(struct ktermios *termios);
 extern speed_t tty_termios_input_baud_rate(struct ktermios *termios);
This page took 0.029766 seconds and 5 git commands to generate.