tty: BKL pushdown
Subsystems (3)
PATCH 1/1 tty: BKL pushdown
- Push the BKL down into the line disciplines
- Switch the tty layer to unlocked_ioctl
- Introduce a new ctrl_lock spin lock for the control bits
- Eliminate much of the lock_kernel use in n_tty
- Prepare to (but don't yet) call the drivers with the lock dropped on the paths that historically held the lock
BKL now primarily protects open/close/ldisc change in the tty layer
I'm sure this will break something so it wants to stew in -mm for a little while.
drivers/char/n_hdlc.c +18 -6
@@ -578,26 +578,36 @@return -EFAULT ; } +lock_kernel (); +for (;;) { -if (test_bit (TTY_OTHER_CLOSED , &tty->flags)) +if (test_bit (TTY_OTHER_CLOSED , &tty->flags)) { +unlock_kernel ();return -EIO ; + } n_hdlc =tty2n_hdlc (tty);if (!n_hdlc || n_hdlc->magic !=HDLC_MAGIC || - tty != n_hdlc->tty) + tty != n_hdlc->tty) { +unlock_kernel ();return 0 ; + } rbuf =n_hdlc_buf_get (&n_hdlc->rx_buf_list);if (rbuf)break ;/* no data */ -if (file->f_flags &O_NONBLOCK ) +if (file->f_flags &O_NONBLOCK ) { +unlock_kernel ();return -EAGAIN ; + }interruptible_sleep_on (&tty->read_wait); -if (signal_pending (current)) +if (signal_pending (current)) { +unlock_kernel ();return -EINTR ; + } }if (rbuf->count > nr)
@@ -618,7 +628,7 @@kfree (rbuf); else n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf); - +unlock_kernel ();return ret; }/* end of n_hdlc_tty_read() */
@@ -661,6 +671,8 @@ count = maxframe; } +lock_kernel (); +add_wait_queue (&tty->write_wait, &wait);set_current_state (TASK_INTERRUPTIBLE );
@@ -695,7 +707,7 @@n_hdlc_buf_put (&n_hdlc->tx_buf_list,tbuf);n_hdlc_send_frames (n_hdlc,tty); } - +unlock_kernel ();return error; }/* end of n_hdlc_tty_write() */
drivers/char/n_r3964.c +14 -2
@@ -1074,6 +1074,8 @@int count;TRACE_L ("read()" ); + +lock_kernel (); pClient =findClient (pInfo,task_pid (current));if (pClient) {
@@ -1081,6 +1083,7 @@if (pMsg ==NULL ) {/* no messages available. */ if (file->f_flags &O_NONBLOCK ) { +unlock_kernel ();return -EAGAIN ; }/* block until there is a message: */
@@ -1090,8 +1093,10 @@/* If we still haven't got a message, we must have been signalled */ -if (!pMsg) +if (!pMsg) { +unlock_kernel ();return -EINTR ; + }/* deliver msg to client process: */ theMsg.msg_id = pMsg->msg_id;
@@ -1102,12 +1107,15 @@kfree (pMsg);TRACE_M ("r3964_read - msg kfree %p" , pMsg); -if (copy_to_user (buf, &theMsg, count)) +if (copy_to_user (buf, &theMsg, count)) { +unlock_kernel ();return -EFAULT ; + }TRACE_PS ("read - return %d" , count);return count; } +unlock_kernel ();return -EPERM ; }
@@ -1156,6 +1164,8 @@ pHeader->locks =0 ; pHeader->owner =NULL ; +lock_kernel (); + pClient =findClient (pInfo,task_pid (current));if (pClient) { pHeader->owner = pClient;
@@ -1172,6 +1182,8 @@*/ add_tx_queue (pInfo, pHeader);trigger_transmit (pInfo); + +unlock_kernel ();return 0 ; }
drivers/char/n_tty.c +26 -8
@@ -183,22 +183,24 @@* at hangup) or when the N_TTY line discipline internally has to * clean the pending queue (for example some signals). * -* FIXME: tty->ctrl_status is not spinlocked and relies on -* lock_kernel() still. +* Locking: ctrl_lock */ static void n_tty_flush_buffer (struct tty_struct *tty) { +unsigned long flags;/* clear everything and unthrottle the driver */ reset_buffer_flags (tty);if (!tty->link)return ; +spin_lock_irqsave (&tty->ctrl_lock, flags);if (tty->link->packet) { tty->ctrl_status |=TIOCPKT_FLUSHREAD ;wake_up_interruptible (&tty->link->read_wait); } +spin_unlock_irqrestore (&tty->ctrl_lock, flags); }/**
@@ -264,7 +266,7 @@* relevant in the world today. If you ever need them, add them here. * * Called from both the receive and transmit sides and can be called -* re-entrantly. Relies on lock_kernel() still. +* re-entrantly. Relies on lock_kernel() for tty->column state. */ static int opost (unsigned char c,struct tty_struct *tty)
@@ -275,6 +277,7 @@if (!space)return -1 ; +lock_kernel ();if (O_OPOST (tty)) {switch (c) {case '\n' :
@@ -323,6 +326,7 @@ } } tty->driver->put_char (tty, c); +unlock_kernel ();return 0 ; }
@@ -337,7 +341,8 @@* the simple cases normally found and helps to generate blocks of * symbols for the console driver and thus improve performance. * -* Called from write_chan under the tty layer write lock. +* Called from write_chan under the tty layer write lock. Relies +* on lock_kernel for the tty->column state. */ static ssize_t opost_block(struct tty_struct *tty,
@@ -353,6 +358,7 @@if (nr > space) nr = space; +lock_kernel ();for (i =0 , cp = buf; i < nr; i++, cp++) {switch (*cp) {case '\n' :
@@ -387,6 +393,7 @@if (tty->driver->flush_chars) tty->driver->flush_chars (tty); i = tty->driver->write (tty, buf, i); +unlock_kernel ();return i; }
@@ -1194,6 +1201,11 @@* Perform job control management checks on this file/tty descriptor * and if appropriate send any needed signals and return a negative * error code if action should be taken. +* +* FIXME: +* Locking: None - redirected write test is safe, testing +* current->signal should possibly lock current->sighand +* pgrp locking ? */ static int job_control (struct tty_struct *tty,struct file *file)
@@ -1246,6 +1258,7 @@ssize_t size;long timeout;unsigned long flags; +int packet; do_it_again:
@@ -1289,16 +1302,19 @@if (mutex_lock_interruptible (&tty->atomic_read_lock))return -ERESTARTSYS ; } - + packet = tty->packet; +add_wait_queue (&tty->read_wait, &wait);while (nr) {/* First test for status change. */ -if (tty->packet && tty->link->ctrl_status) { +if (packet && tty->link->ctrl_status) {unsigned char cs;if (b != buf)break ; +spin_lock_irqsave (&tty->link->ctrl_lock, flags); cs = tty->link->ctrl_status; tty->link->ctrl_status =0 ; +spin_unlock_irqrestore (&tty->link->ctrl_lock, flags);if (tty_put_user (tty, cs, b++)) { retval = -EFAULT ; b--;
@@ -1333,6 +1349,7 @@ retval = -ERESTARTSYS ;break ; } +/* FIXME: does n_tty_set_room need locking ? */ n_tty_set_room (tty); timeout =schedule_timeout (timeout);continue ;
@@ -1340,7 +1357,7 @@__set_current_state (TASK_RUNNING );/* Deal with packet mode. */ -if (tty->packet && b == buf) { +if (packet && b == buf) {if (tty_put_user (tty,TIOCPKT_DATA , b++)) { retval = -EFAULT ; b--;
@@ -1388,6 +1405,8 @@break ; }else {int uncopied; +/* The copy function takes the read lock and handles +locking internally for this case */ uncopied =copy_from_read_buf (tty, &b, &nr); uncopied +=copy_from_read_buf (tty, &b, &nr);if (uncopied) {
@@ -1429,7 +1448,6 @@ goto do_it_again;n_tty_set_room (tty); -return retval; }
drivers/char/pty.c +3 -0
@@ -181,6 +181,7 @@static void pty_flush_buffer (struct tty_struct *tty) {struct tty_struct *to = tty->link; +unsigned long flags;if (!to)return ;
@@ -189,8 +190,10 @@ to->ldisc.flush_buffer (to);if (to->packet) { +spin_lock_irqsave (&tty->ctrl_lock, flags); tty->ctrl_status |=TIOCPKT_FLUSHWRITE ;wake_up_interruptible (&to->read_wait); +spin_unlock_irqrestore (&tty->ctrl_lock, flags); } }
drivers/char/tty_io.c +70 -31
346 lines not on this page · raw message
drivers/char/tty_ioctl.c +6 -0
@@ -395,6 +395,7 @@int canon_change;struct ktermios old_termios = *tty->termios;struct tty_ldisc *ld; +unsigned long flags;/* * Perform the actual termios internal changes under lock.
@@ -429,11 +430,13 @@STOP_CHAR (tty) =='\023' && START_CHAR(tty) == '\021');if (old_flow != new_flow) { +spin_lock_irqsave (&tty->ctrl_lock, flags); tty->ctrl_status &= ~(TIOCPKT_DOSTOP |TIOCPKT_NOSTOP );if (new_flow) tty->ctrl_status |=TIOCPKT_DOSTOP ;else tty->ctrl_status |=TIOCPKT_NOSTOP ; +spin_unlock_irqrestore (&tty->ctrl_lock, flags);wake_up_interruptible (&tty->link->read_wait); } }
@@ -905,6 +908,7 @@ unsigned int cmd, unsigned long arg) {struct tty_struct *real_tty; +unsigned long flags;int retval; if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
@@ -963,6 +967,7 @@return -ENOTTY ;if (get_user (pktmode, (int __user *) arg))return -EFAULT ; +spin_lock_irqsave (&tty->ctrl_lock, flags);if (pktmode) {if (!tty->packet) { tty->packet =1 ;
@@ -970,6 +975,7 @@ } }else tty->packet =0 ; +spin_unlock_irqrestore (&tty->ctrl_lock, flags);return 0 ; }default :
drivers/char/vt.c +4 -0
@@ -2531,6 +2531,9 @@if (get_user (type, p))return -EFAULT ; ret =0 ; + +lock_kernel (); +switch (type) {case TIOCL_SETSEL :
@@ -2605,6 +2608,7 @@ ret = -EINVAL ;break ; } +unlock_kernel ();return ret; }
include/linux/tty.h +2 -2
@@ -183,6 +200,7 @@int index;struct tty_ldisc ldisc;struct mutex termios_mutex; +spinlock_t ctrl_lock;struct ktermios *termios, *termios_locked;char name[64 ];struct pid *pgrp;
@@ -323,8 +342,7 @@extern void tty_wakeup (struct tty_struct *tty);extern void tty_ldisc_flush (struct tty_struct *tty); -extern int tty_ioctl (struct inode *inode,struct file *file,unsigned int cmd, -unsigned long arg); +extern long tty_ioctl (struct file *file,unsigned int cmd,unsigned long arg);extern int tty_mode_ioctl (struct tty_struct *tty,struct file *file,unsigned int cmd,unsigned long arg);extern int tty_perform_flush (struct tty_struct *tty,unsigned long arg);
PATCH 1/1 tty: BKL pushdown fix
--- drivers/char/n_hdlc.c | 4 ++-- drivers/char/n_r3964.c | 6 +++--- drivers/char/n_tty.c | 2 +- drivers/char/tty_io.c | 16 +++++++++------- drivers/char/vt.c | 8 ++++---- 5 files changed, 19 insertions(+), 17 deletions(-)
drivers/char/n_hdlc.c +2 -2
@@ -579,7 +579,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, }lock_kernel (); - +for (;;) {if (test_bit (TTY_OTHER_CLOSED , &tty->flags)) {unlock_kernel ();
@@ -672,7 +672,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, }lock_kernel (); - +add_wait_queue (&tty->write_wait, &wait);set_current_state (TASK_INTERRUPTIBLE );
drivers/char/n_r3964.c +3 -3
@@ -1074,7 +1074,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,int count;TRACE_L ("read()" ); - +lock_kernel (); pClient =findClient (pInfo,task_pid (current));
@@ -1165,7 +1165,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file, pHeader->owner =NULL ;lock_kernel (); - + pClient =findClient (pInfo,task_pid (current));if (pClient) { pHeader->owner = pClient;
@@ -1182,7 +1182,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file,*/ add_tx_queue (pInfo, pHeader);trigger_transmit (pInfo); - +unlock_kernel ();return 0 ;
drivers/char/n_tty.c +1 -1
@@ -1303,7 +1303,7 @@ do_it_again:return -ERESTARTSYS ; } packet = tty->packet; - +add_wait_queue (&tty->read_wait, &wait);while (nr) {/* First test for status change. */
drivers/char/tty_io.c +9 -7
@@ -1204,8 +1204,8 @@ static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)return POLLIN |POLLOUT |POLLERR |POLLHUP |POLLRDNORM |POLLWRNORM ; } -static long hung_up_tty_ioctl (struct inode *inode,struct file *file, -unsigned int cmd,unsigned long arg) +static long hung_up_tty_ioctl (struct file *file,unsigned int cmd, +unsigned long arg) {return cmd ==TIOCSPGRP ? -ENOTTY : -EIO ; }
@@ -1259,7 +1259,7 @@ static const struct file_operations hung_up_tty_fops = { .read = hung_up_tty_read, .write = hung_up_tty_write, .poll = hung_up_tty_poll, - .ioctl = hung_up_tty_ioctl, + .unlocked_ioctl = hung_up_tty_ioctl, .compat_ioctl = hung_up_tty_compat_ioctl, .release = tty_release, };
@@ -3208,10 +3208,12 @@ static int tiocsetd(struct tty_struct *tty, int __user *p)if (get_user (ldisc, p))return -EFAULT ; - +lock_kernel (); ret =tty_set_ldisc (tty, ldisc);unlock_kernel (); + +return ret; }/**
@@ -3230,7 +3232,7 @@ static int tiocsetd(struct tty_struct *tty, int __user *p)static int send_break (struct tty_struct *tty,unsigned int duration) {int retval = -EINTR ; - +lock_kernel ();if (tty_write_lock (tty,0 ) <0 ) goto out;
@@ -3345,9 +3347,9 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)/* * Break handling by driver */ - + retval = -EINVAL ; - +if (!tty->driver->break_ctl) {switch (cmd) {case TIOCSBRK :
drivers/char/vt.c +4 -4
@@ -2531,9 +2531,9 @@ int tioclinux(struct tty_struct *tty, unsigned long arg)if (get_user (type, p))return -EFAULT ; ret =0 ; - +lock_kernel (); - +switch (type) {case TIOCL_SETSEL :
@@ -2553,7 +2553,7 @@ int tioclinux(struct tty_struct *tty, unsigned long arg) ret =sel_loadlut (p);break ;case TIOCL_GETSHIFTSTATE : - +/* * Make it possible to react to Shift+Mousebutton. * Note that 'shift_state' is an undocumented
@@ -3823,7 +3823,7 @@ static int con_font_get(struct vc_data *vc, struct console_font_op *op) goto out; c = (font.width+7 )/8 *32 * font.charcount; - +if (op->data && font.charcount > op->charcount) rc = -ENOSPC ;if (!(op->flags &KD_FONT_FLAG_OLD )) {
Signature · 1 line
1.5.3.8
Re: [PATCH 1/1] tty: BKL pushdown fix
drivers/char/n_hdlc.c static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, · line 582
Quoted 8 lines · On Fri, 8 Feb 2008 20:04:47 +0100 Jiri Slaby <[email protected]> wrote:
drivers/char/n_hdlc.c +1
-1@@ -579,7 +579,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, }lock_kernel (); - +
yeah. I have a little script..
Re: [PATCH 1/1] tty: BKL pushdown fix
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Fri, 8 Feb 2008 20:04:47 +0100 Jiri Slaby <[email protected]> wrote:
drivers/char/n_hdlc.c +0
-0@@ -579,7 +579,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, }
Quoted 5 lines
drivers/char/n_hdlc.c static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, · line 582
Quoted 3 lines
drivers/char/n_hdlc.c +1
-1lock_kernel (); - +yeah. I have a little script..
Note that it isn't only whitespace fixes in the patch.
I have one more -fix.
PATCH 1/1 tty: BKL pushdown fix1
--- fs/compat_ioctl.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
fs/compat_ioctl.c +1 -1
@@ -1046,7 +1046,7 @@ static int vt_check(struct file *file)struct inode *inode = file->f_path.dentry->d_inode;struct vc_data *vc; -if (file->f_op->ioctl != tty_ioctl) +if (file->f_op->unlocked_ioctl != tty_ioctl)return -EINVAL ; tty = (struct tty_struct *)file->private_data;
Signature · 1 line
1.5.3.8
Re: [PATCH 1/1] tty: BKL pushdown fix1
Oh, CCing Alan.
Quoted 18 lines · Jiri Slaby
Signed-off-by Jiri Slaby <[email protected]>--- fs/compat_ioctl.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)fs/compat_ioctl.c +1
-1@@ -1046,7 +1046,7 @@ static int vt_check(struct file *file)struct inode *inode = file->f_path.dentry->d_inode;struct vc_data *vc; -if (file->f_op->ioctl != tty_ioctl) +if (file->f_op->unlocked_ioctl != tty_ioctl)return -EINVAL ; tty = (struct tty_struct *)file->private_data;
Re: [PATCH 1/1] tty: BKL pushdown fix1
In reply to Jiri SlabyQuoted 10 lines · On Fri, 8 Feb 2008 22:40:56 +0100 Jiri Slaby <[email protected]> wrote:
Signed-off-by Jiri Slaby <[email protected]>--- fs/compat_ioctl.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)fs/compat_ioctl.c +0
-0
fs/compat_ioctl.c static int vt_check(struct file *file) · line 1050
Quoted 6 lines
fs/compat_ioctl.c +1
-1@@ -1046,7 +1046,7 @@ static int vt_check(struct file *file)struct inode *inode = file->f_path.dentry->d_inode;struct vc_data *vc; -if (file->f_op->ioctl != tty_ioctl) +if (file->f_op->unlocked_ioctl != tty_ioctl)return -EINVAL ;
Nice catch
Re: [PATCH 1/1] tty: BKL pushdown fix
In reply to Jiri SlabyQuoted 9 lines · On Fri, 08 Feb 2008 22:38:28 +0100 Jiri Slaby <[email protected]> wrote:
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Fri, 8 Feb 2008 20:04:47 +0100 Jiri Slaby <[email protected]> wrote:
drivers/char/n_hdlc.c +0
-0@@ -579,7 +579,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, }
Quoted 7 lines
Quoted 5 lines
drivers/char/n_hdlc.c static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, · line 582
Quoted 3 lines
drivers/char/n_hdlc.c +1
-1lock_kernel (); - +yeah. I have a little script..
Note that it isn't only whitespace fixes in the patch.
Well no, I didn't note that. The absence of any changelog led me to believe that it was the obvious and trivial change which it appeared to be.
Quoted 1 line
I have one more -fix.
And that one had zero description either.
Please, it only takes 30 seconds.
Re: [PATCH 1/1] tty: BKL pushdown fix
Quoted 4 lines · Andrew Morton
Quoted 1 line · On Fri, 08 Feb 2008 22:38:28 +0100 Jiri Slaby <[email protected]> wrote:
Note that it isn't only whitespace fixes in the patch.
Quoted 8 lines
Well no, I didn't note that. The absence of any changelog led me to believe that it was the obvious and trivial change which it appeared to be.
Quoted 1 line
I have one more -fix.
And that one had zero description either.
Please, it only takes 30 seconds.
Well, I though -fix descriptions go to /dev/null after folding it to the parent. No problem to comment them too. Next time. Thanks.