Commit ec79d605 authored by Arnd Bergmann's avatar Arnd Bergmann Committed by Greg Kroah-Hartman

tty: replace BKL with a new tty_lock

As a preparation for replacing the big kernel lock
in the TTY layer, wrap all the callers in new
macros tty_lock, tty_lock_nested and tty_unlock.
Signed-off-by: default avatarArnd Bergmann <arnd@arndb.de>
Cc: Alan Cox <alan@lxorguk.ukuu.org.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 3f582b8c
...@@ -1072,7 +1072,7 @@ static int get_serial_info(struct async_struct * info, ...@@ -1072,7 +1072,7 @@ static int get_serial_info(struct async_struct * info,
if (!retinfo) if (!retinfo)
return -EFAULT; return -EFAULT;
memset(&tmp, 0, sizeof(tmp)); memset(&tmp, 0, sizeof(tmp));
lock_kernel(); tty_lock();
tmp.type = state->type; tmp.type = state->type;
tmp.line = state->line; tmp.line = state->line;
tmp.port = state->port; tmp.port = state->port;
...@@ -1083,7 +1083,7 @@ static int get_serial_info(struct async_struct * info, ...@@ -1083,7 +1083,7 @@ static int get_serial_info(struct async_struct * info,
tmp.close_delay = state->close_delay; tmp.close_delay = state->close_delay;
tmp.closing_wait = state->closing_wait; tmp.closing_wait = state->closing_wait;
tmp.custom_divisor = state->custom_divisor; tmp.custom_divisor = state->custom_divisor;
unlock_kernel(); tty_unlock();
if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
return -EFAULT; return -EFAULT;
return 0; return 0;
...@@ -1100,14 +1100,14 @@ static int set_serial_info(struct async_struct * info, ...@@ -1100,14 +1100,14 @@ static int set_serial_info(struct async_struct * info,
if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
return -EFAULT; return -EFAULT;
lock_kernel(); tty_lock();
state = info->state; state = info->state;
old_state = *state; old_state = *state;
change_irq = new_serial.irq != state->irq; change_irq = new_serial.irq != state->irq;
change_port = (new_serial.port != state->port); change_port = (new_serial.port != state->port);
if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) { if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
unlock_kernel(); tty_unlock();
return -EINVAL; return -EINVAL;
} }
...@@ -1127,7 +1127,7 @@ static int set_serial_info(struct async_struct * info, ...@@ -1127,7 +1127,7 @@ static int set_serial_info(struct async_struct * info,
} }
if (new_serial.baud_base < 9600) { if (new_serial.baud_base < 9600) {
unlock_kernel(); tty_unlock();
return -EINVAL; return -EINVAL;
} }
...@@ -1163,7 +1163,7 @@ check_and_exit: ...@@ -1163,7 +1163,7 @@ check_and_exit:
} }
} else } else
retval = startup(info); retval = startup(info);
unlock_kernel(); tty_unlock();
return retval; return retval;
} }
...@@ -1538,7 +1538,7 @@ static void rs_wait_until_sent(struct tty_struct *tty, int timeout) ...@@ -1538,7 +1538,7 @@ static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
orig_jiffies = jiffies; orig_jiffies = jiffies;
lock_kernel(); tty_lock_nested(); /* tty_wait_until_sent is called from lots of places */
/* /*
* Set the check interval to be 1/5 of the estimated time to * Set the check interval to be 1/5 of the estimated time to
* send a single character, and make it at least 1. The check * send a single character, and make it at least 1. The check
...@@ -1579,7 +1579,7 @@ static void rs_wait_until_sent(struct tty_struct *tty, int timeout) ...@@ -1579,7 +1579,7 @@ static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
break; break;
} }
__set_current_state(TASK_RUNNING); __set_current_state(TASK_RUNNING);
unlock_kernel(); tty_unlock();
#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies); printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
#endif #endif
......
...@@ -67,15 +67,15 @@ static void set_led(char state) ...@@ -67,15 +67,15 @@ static void set_led(char state)
static int briq_panel_open(struct inode *ino, struct file *filep) static int briq_panel_open(struct inode *ino, struct file *filep)
{ {
lock_kernel(); tty_lock();
/* enforce single access, vfd_is_open is protected by BKL */ /* enforce single access, vfd_is_open is protected by BKL */
if (vfd_is_open) { if (vfd_is_open) {
unlock_kernel(); tty_unlock();
return -EBUSY; return -EBUSY;
} }
vfd_is_open = 1; vfd_is_open = 1;
unlock_kernel(); tty_unlock();
return 0; return 0;
} }
......
...@@ -598,18 +598,18 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, ...@@ -598,18 +598,18 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
return -EFAULT; return -EFAULT;
} }
lock_kernel(); tty_lock();
for (;;) { for (;;) {
if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
unlock_kernel(); tty_unlock();
return -EIO; return -EIO;
} }
n_hdlc = tty2n_hdlc (tty); n_hdlc = tty2n_hdlc (tty);
if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC || if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
tty != n_hdlc->tty) { tty != n_hdlc->tty) {
unlock_kernel(); tty_unlock();
return 0; return 0;
} }
...@@ -619,13 +619,13 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, ...@@ -619,13 +619,13 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
/* no data */ /* no data */
if (file->f_flags & O_NONBLOCK) { if (file->f_flags & O_NONBLOCK) {
unlock_kernel(); tty_unlock();
return -EAGAIN; return -EAGAIN;
} }
interruptible_sleep_on (&tty->read_wait); interruptible_sleep_on (&tty->read_wait);
if (signal_pending(current)) { if (signal_pending(current)) {
unlock_kernel(); tty_unlock();
return -EINTR; return -EINTR;
} }
} }
...@@ -648,7 +648,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file, ...@@ -648,7 +648,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
kfree(rbuf); kfree(rbuf);
else else
n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf); n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf);
unlock_kernel(); tty_unlock();
return ret; return ret;
} /* end of n_hdlc_tty_read() */ } /* end of n_hdlc_tty_read() */
...@@ -691,7 +691,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, ...@@ -691,7 +691,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
count = maxframe; count = maxframe;
} }
lock_kernel(); tty_lock();
add_wait_queue(&tty->write_wait, &wait); add_wait_queue(&tty->write_wait, &wait);
set_current_state(TASK_INTERRUPTIBLE); set_current_state(TASK_INTERRUPTIBLE);
...@@ -731,7 +731,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file, ...@@ -731,7 +731,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf); n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
n_hdlc_send_frames(n_hdlc,tty); n_hdlc_send_frames(n_hdlc,tty);
} }
unlock_kernel(); tty_unlock();
return error; return error;
} /* end of n_hdlc_tty_write() */ } /* end of n_hdlc_tty_write() */
......
...@@ -1067,7 +1067,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file, ...@@ -1067,7 +1067,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
TRACE_L("read()"); TRACE_L("read()");
lock_kernel(); tty_lock();
pClient = findClient(pInfo, task_pid(current)); pClient = findClient(pInfo, task_pid(current));
if (pClient) { if (pClient) {
...@@ -1109,7 +1109,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file, ...@@ -1109,7 +1109,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
} }
ret = -EPERM; ret = -EPERM;
unlock: unlock:
unlock_kernel(); tty_unlock();
return ret; return ret;
} }
...@@ -1158,7 +1158,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file, ...@@ -1158,7 +1158,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
pHeader->locks = 0; pHeader->locks = 0;
pHeader->owner = NULL; pHeader->owner = NULL;
lock_kernel(); tty_lock();
pClient = findClient(pInfo, task_pid(current)); pClient = findClient(pInfo, task_pid(current));
if (pClient) { if (pClient) {
...@@ -1177,7 +1177,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file, ...@@ -1177,7 +1177,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
add_tx_queue(pInfo, pHeader); add_tx_queue(pInfo, pHeader);
trigger_transmit(pInfo); trigger_transmit(pInfo);
unlock_kernel(); tty_unlock();
return 0; return 0;
} }
......
...@@ -692,9 +692,9 @@ static int ptmx_open(struct inode *inode, struct file *filp) ...@@ -692,9 +692,9 @@ static int ptmx_open(struct inode *inode, struct file *filp)
{ {
int ret; int ret;
lock_kernel(); tty_lock();
ret = __ptmx_open(inode, filp); ret = __ptmx_open(inode, filp);
unlock_kernel(); tty_unlock();
return ret; return ret;
} }
......
...@@ -313,7 +313,7 @@ int paste_selection(struct tty_struct *tty) ...@@ -313,7 +313,7 @@ int paste_selection(struct tty_struct *tty)
struct tty_ldisc *ld; struct tty_ldisc *ld;
DECLARE_WAITQUEUE(wait, current); DECLARE_WAITQUEUE(wait, current);
lock_kernel(); tty_lock_nested(); /* always called with BTM from vt_ioctl */
acquire_console_sem(); acquire_console_sem();
poke_blanked_console(); poke_blanked_console();
...@@ -338,6 +338,6 @@ int paste_selection(struct tty_struct *tty) ...@@ -338,6 +338,6 @@ int paste_selection(struct tty_struct *tty)
__set_current_state(TASK_RUNNING); __set_current_state(TASK_RUNNING);
tty_ldisc_deref(ld); tty_ldisc_deref(ld);
unlock_kernel(); tty_unlock();
return 0; return 0;
} }
...@@ -1505,7 +1505,7 @@ cy_ioctl(struct tty_struct *tty, struct file *file, ...@@ -1505,7 +1505,7 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
printk("cy_ioctl %s, cmd = %x arg = %lx\n", tty->name, cmd, arg); /* */ printk("cy_ioctl %s, cmd = %x arg = %lx\n", tty->name, cmd, arg); /* */
#endif #endif
lock_kernel(); tty_lock();
switch (cmd) { switch (cmd) {
case CYGETMON: case CYGETMON:
...@@ -1561,7 +1561,7 @@ cy_ioctl(struct tty_struct *tty, struct file *file, ...@@ -1561,7 +1561,7 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
default: default:
ret_val = -ENOIOCTLCMD; ret_val = -ENOIOCTLCMD;
} }
unlock_kernel(); tty_unlock();
#ifdef SERIAL_DEBUG_OTHER #ifdef SERIAL_DEBUG_OTHER
printk("cy_ioctl done\n"); printk("cy_ioctl done\n");
......
...@@ -1699,7 +1699,7 @@ static long sx_fw_ioctl(struct file *filp, unsigned int cmd, ...@@ -1699,7 +1699,7 @@ static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
if (!capable(CAP_SYS_RAWIO)) if (!capable(CAP_SYS_RAWIO))
return -EPERM; return -EPERM;
lock_kernel(); tty_lock();
sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg); sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
...@@ -1848,7 +1848,7 @@ static long sx_fw_ioctl(struct file *filp, unsigned int cmd, ...@@ -1848,7 +1848,7 @@ static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
break; break;
} }
out: out:
unlock_kernel(); tty_unlock();
func_exit(); func_exit();
return rc; return rc;
} }
...@@ -1859,7 +1859,7 @@ static int sx_break(struct tty_struct *tty, int flag) ...@@ -1859,7 +1859,7 @@ static int sx_break(struct tty_struct *tty, int flag)
int rv; int rv;
func_enter(); func_enter();
lock_kernel(); tty_lock();
if (flag) if (flag)
rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK); rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK);
...@@ -1868,7 +1868,7 @@ static int sx_break(struct tty_struct *tty, int flag) ...@@ -1868,7 +1868,7 @@ static int sx_break(struct tty_struct *tty, int flag)
if (rv != 1) if (rv != 1)
printk(KERN_ERR "sx: couldn't send break (%x).\n", printk(KERN_ERR "sx: couldn't send break (%x).\n",
read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat))); read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat)));
unlock_kernel(); tty_unlock();
func_exit(); func_exit();
return 0; return 0;
} }
...@@ -1909,7 +1909,7 @@ static int sx_ioctl(struct tty_struct *tty, struct file *filp, ...@@ -1909,7 +1909,7 @@ static int sx_ioctl(struct tty_struct *tty, struct file *filp,
/* func_enter2(); */ /* func_enter2(); */
rc = 0; rc = 0;
lock_kernel(); tty_lock();
switch (cmd) { switch (cmd) {
case TIOCGSERIAL: case TIOCGSERIAL:
rc = gs_getserial(&port->gs, argp); rc = gs_getserial(&port->gs, argp);
...@@ -1921,7 +1921,7 @@ static int sx_ioctl(struct tty_struct *tty, struct file *filp, ...@@ -1921,7 +1921,7 @@ static int sx_ioctl(struct tty_struct *tty, struct file *filp,
rc = -ENOIOCTLCMD; rc = -ENOIOCTLCMD;
break; break;
} }
unlock_kernel(); tty_unlock();
/* func_exit(); */ /* func_exit(); */
return rc; return rc;
......
...@@ -149,6 +149,7 @@ static long tty_compat_ioctl(struct file *file, unsigned int cmd, ...@@ -149,6 +149,7 @@ static long tty_compat_ioctl(struct file *file, unsigned int cmd,
#else #else
#define tty_compat_ioctl NULL #define tty_compat_ioctl NULL
#endif #endif
static int __tty_fasync(int fd, struct file *filp, int on);
static int tty_fasync(int fd, struct file *filp, int on); static int tty_fasync(int fd, struct file *filp, int on);
static void release_tty(struct tty_struct *tty, int idx); static void release_tty(struct tty_struct *tty, int idx);
static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
...@@ -483,7 +484,7 @@ EXPORT_SYMBOL_GPL(tty_wakeup); ...@@ -483,7 +484,7 @@ EXPORT_SYMBOL_GPL(tty_wakeup);
* remains intact. * remains intact.
* *
* Locking: * Locking:
* BKL * BTM
* redirect lock for undoing redirection * redirect lock for undoing redirection
* file list lock for manipulating list of ttys * file list lock for manipulating list of ttys
* tty_ldisc_lock from called functions * tty_ldisc_lock from called functions
...@@ -513,8 +514,11 @@ static void do_tty_hangup(struct work_struct *work) ...@@ -513,8 +514,11 @@ static void do_tty_hangup(struct work_struct *work)
} }
spin_unlock(&redirect_lock); spin_unlock(&redirect_lock);
/* inuse_filps is protected by the single kernel lock */ /* inuse_filps is protected by the single tty lock,
lock_kernel(); this really needs to change if we want to flush the
workqueue with the lock held */
tty_lock_nested(); /* called with BTM held from pty_close and
others */
check_tty_count(tty, "do_tty_hangup"); check_tty_count(tty, "do_tty_hangup");
file_list_lock(); file_list_lock();
...@@ -525,7 +529,7 @@ static void do_tty_hangup(struct work_struct *work) ...@@ -525,7 +529,7 @@ static void do_tty_hangup(struct work_struct *work)
if (filp->f_op->write != tty_write) if (filp->f_op->write != tty_write)
continue; continue;
closecount++; closecount++;
tty_fasync(-1, filp, 0); /* can't block */ __tty_fasync(-1, filp, 0); /* can't block */
filp->f_op = &hung_up_tty_fops; filp->f_op = &hung_up_tty_fops;
} }
file_list_unlock(); file_list_unlock();
...@@ -594,7 +598,7 @@ static void do_tty_hangup(struct work_struct *work) ...@@ -594,7 +598,7 @@ static void do_tty_hangup(struct work_struct *work)
*/ */
set_bit(TTY_HUPPED, &tty->flags); set_bit(TTY_HUPPED, &tty->flags);
tty_ldisc_enable(tty); tty_ldisc_enable(tty);
unlock_kernel(); tty_unlock();
if (f) if (f)
fput(f); fput(f);
} }
...@@ -696,7 +700,8 @@ static void session_clear_tty(struct pid *session) ...@@ -696,7 +700,8 @@ static void session_clear_tty(struct pid *session)
* exiting; it is 0 if called by the ioctl TIOCNOTTY. * exiting; it is 0 if called by the ioctl TIOCNOTTY.
* *
* Locking: * Locking:
* BKL is taken for hysterical raisins * BTM is taken for hysterical raisins, and held when
* called from no_tty().
* tty_mutex is taken to protect tty * tty_mutex is taken to protect tty
* ->siglock is taken to protect ->signal/->sighand * ->siglock is taken to protect ->signal/->sighand
* tasklist_lock is taken to walk process list for sessions * tasklist_lock is taken to walk process list for sessions
...@@ -714,10 +719,10 @@ void disassociate_ctty(int on_exit) ...@@ -714,10 +719,10 @@ void disassociate_ctty(int on_exit)
tty = get_current_tty(); tty = get_current_tty();
if (tty) { if (tty) {
tty_pgrp = get_pid(tty->pgrp); tty_pgrp = get_pid(tty->pgrp);
lock_kernel(); tty_lock_nested(); /* see above */
if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
tty_vhangup(tty); tty_vhangup(tty);
unlock_kernel(); tty_unlock();
tty_kref_put(tty); tty_kref_put(tty);
} else if (on_exit) { } else if (on_exit) {
struct pid *old_pgrp; struct pid *old_pgrp;
...@@ -774,9 +779,9 @@ void disassociate_ctty(int on_exit) ...@@ -774,9 +779,9 @@ void disassociate_ctty(int on_exit)
void no_tty(void) void no_tty(void)
{ {
struct task_struct *tsk = current; struct task_struct *tsk = current;
lock_kernel(); tty_lock();
disassociate_ctty(0); disassociate_ctty(0);
unlock_kernel(); tty_unlock();
proc_clear_tty(tsk); proc_clear_tty(tsk);
} }
...@@ -1013,19 +1018,19 @@ out: ...@@ -1013,19 +1018,19 @@ out:
* We don't put it into the syslog queue right now maybe in the future if * We don't put it into the syslog queue right now maybe in the future if
* really needed. * really needed.
* *
* We must still hold the BKL and test the CLOSING flag for the moment. * We must still hold the BTM and test the CLOSING flag for the moment.
*/ */
void tty_write_message(struct tty_struct *tty, char *msg) void tty_write_message(struct tty_struct *tty, char *msg)
{ {
if (tty) { if (tty) {
mutex_lock(&tty->atomic_write_lock); mutex_lock(&tty->atomic_write_lock);
lock_kernel(); tty_lock();
if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
unlock_kernel(); tty_unlock();
tty->ops->write(tty, msg, strlen(msg)); tty->ops->write(tty, msg, strlen(msg));
} else } else
unlock_kernel(); tty_unlock();
tty_write_unlock(tty); tty_write_unlock(tty);
} }
return; return;
...@@ -1208,18 +1213,18 @@ static int tty_driver_install_tty(struct tty_driver *driver, ...@@ -1208,18 +1213,18 @@ static int tty_driver_install_tty(struct tty_driver *driver,
int ret; int ret;
if (driver->ops->install) { if (driver->ops->install) {
lock_kernel(); tty_lock_nested(); /* already called with BTM held */
ret = driver->ops->install(driver, tty); ret = driver->ops->install(driver, tty);
unlock_kernel(); tty_unlock();
return ret; return ret;
} }
if (tty_init_termios(tty) == 0) { if (tty_init_termios(tty) == 0) {
lock_kernel(); tty_lock_nested();
tty_driver_kref_get(driver); tty_driver_kref_get(driver);
tty->count++; tty->count++;
driver->ttys[idx] = tty; driver->ttys[idx] = tty;
unlock_kernel(); tty_unlock();
return 0; return 0;
} }
return -ENOMEM; return -ENOMEM;
...@@ -1312,14 +1317,15 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx, ...@@ -1312,14 +1317,15 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
struct tty_struct *tty; struct tty_struct *tty;
int retval; int retval;
lock_kernel(); tty_lock_nested(); /* always called with tty lock held already */
/* Check if pty master is being opened multiple times */ /* Check if pty master is being opened multiple times */
if (driver->subtype == PTY_TYPE_MASTER && if (driver->subtype == PTY_TYPE_MASTER &&
(driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) { (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
unlock_kernel(); tty_unlock();
return ERR_PTR(-EIO); return ERR_PTR(-EIO);
} }
unlock_kernel(); tty_unlock();
/*