Skip to contentSkip to conversation

tty_lock: undo the old tty_lock use on the ctty

PATCH In review Alan Cox 6 participants 19 messages May 3, 2012 – May 11, 2012 TTY layer and serial drivers

lore
Share & tools
lore

Alan Cox

author
#0 lore raw

PATCH 1/3 tty_lock: undo the old tty_lock use on the ctty

From: Alan Cox <[email protected]>

get_current_tty has its own consistent locking. That means a pile of the tty lock cases are not needed. As get_current_tty also keeps a reference the tty object lifetime means we can propogate the lock removal out.

---

 drivers/tty/tty_io.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +7 -2
index d939bd7..b425c79 100644
@@ -855,10 +855,11 @@ void disassociate_ctty(int on_exit)
  */
 void no_tty(void)
 {
+	/* FIXME: Review locking here. The tty_lock never covered any race
+	   between a new association and proc_clear_tty but possible we need
+	   to protect against this anyway */
 	struct task_struct *tsk = current;
-	tty_lock();
 	disassociate_ctty(0);
-	tty_unlock();
 	proc_clear_tty(tsk);
 }
 
@@ -1800,6 +1801,9 @@ int tty_release(struct inode *inode, struct file *filp)
  *
  *	We cannot return driver and index like for the other nodes because
  *	devpts will not work then. It expects inodes to be from devpts FS.
+ *
+ *	We need to move to returning a refcounted object from all the lookup
+ *	paths including this one.
  */
 static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
 {
@@ -1816,6 +1820,7 @@ static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
 	/* noctty = 1; */
 	tty_kref_put(tty);
 	/* FIXME: we put a reference and return a TTY! */
+	/* This is only safe because the caller holds tty_mutex */
 	return tty;
 }
 
author
#1 lore raw

PATCH 2/3 pty: Lock the devpts bits privately

From: Alan Cox <[email protected]>

This is a private pty affair, we don't want to tangle it with the tty_lock any more as we know all the other non tty locking is now handled by the vfs so we too can move.

---

 drivers/tty/pty.c |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)
drivers/tty/pty.c +15 -4
index eeae7fa..5505ffc 100644
@@ -26,11 +26,13 @@
 #include <linux/bitops.h>
 #include <linux/devpts_fs.h>
 #include <linux/slab.h>
+#include <linux/mutex.h>
 
 
 #ifdef CONFIG_UNIX98_PTYS
 static struct tty_driver *ptm_driver;
 static struct tty_driver *pts_driver;
+static DEFINE_MUTEX(devpts_mutex);
 #endif
 
 static void pty_close(struct tty_struct *tty, struct file *filp)
@@ -54,8 +56,11 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
 	if (tty->driver->subtype == PTY_TYPE_MASTER) {
 		set_bit(TTY_OTHER_CLOSED, &tty->flags);
 #ifdef CONFIG_UNIX98_PTYS
-		if (tty->driver == ptm_driver)
+		if (tty->driver == ptm_driver) {
+		        mutex_lock(&devpts_mutex);
 			devpts_pty_kill(tty->link);
+		        mutex_unlock(&devpts_mutex);
+		}
 #endif
 		tty_unlock();
 		tty_vhangup(tty->link);
@@ -475,13 +480,17 @@ static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
  *	@idx: tty index
  *
  *	Look up a pty master device. Called under the tty_mutex for now.
- *	This provides our locking.
+ *	This provides our locking for the tty pointer.
  */
 
 static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
 		struct inode *pts_inode, int idx)
 {
-	struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
+	struct tty_struct *tty;
+
+	mutex_lock(&devpts_mutex);
+	tty = devpts_get_tty(pts_inode, idx);
+	mutex_unlock(&devpts_mutex);
 	/* Master must be open before slave */
 	if (!tty)
 		return ERR_PTR(-EIO);
@@ -622,8 +631,10 @@ static int ptmx_open(struct inode *inode, struct file *filp)
 	}
 
 	mutex_lock(&tty_mutex);
-	tty_lock();
+	mutex_lock(&devpts_mutex);
 	tty = tty_init_dev(ptm_driver, index);
+	mutex_unlock(&devpts_mutex);
+	tty_lock();
 	mutex_unlock(&tty_mutex);
 
 	if (IS_ERR(tty)) {
#2 lore raw

Re: [PATCH 2/3] pty: Lock the devpts bits privately

Quoted 9 lines · Alan Cox

From: Alan Cox <[email protected]>

This is a private pty affair, we don't want to tangle it with the tty_lock any more as we know all the other non tty locking is now handled by the vfs so we too can move.

(quoted diff) +1 -0
+		        mutex_lock(&devpts_mutex);

Review

Quoted 10 lines
(quoted diff) +6 -0
 			devpts_pty_kill(tty->link);
+		        mutex_unlock(&devpts_mutex);
 
+	mutex_lock(&devpts_mutex);
+	tty = devpts_get_tty(pts_inode, idx);
+	mutex_unlock(&devpts_mutex);
 
+	mutex_lock(&devpts_mutex);
 	tty = tty_init_dev(ptm_driver, index);
+	mutex_unlock(&devpts_mutex);

Conceptually this seems fine, but it would seem cleaner to me to push this mutex into the called functions in devpts; I suspect the lock could be eliminated or at least be made per instance there (which would make massive-container people happy...)

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
author
#3 lore raw

Re: [PATCH 2/3] pty: Lock the devpts bits privately

Quoted 17 lines · On Tue, 08 May 2012 11:18:35 -0700 "H. Peter Anvin" <[email protected]> wrote:
Quoted 15 lines · Alan Cox

From: Alan Cox <[email protected]>

This is a private pty affair, we don't want to tangle it with the tty_lock any more as we know all the other non tty locking is now handled by the vfs so we too can move.

drivers/tty/pty.c +5 -0
+		        mutex_lock(&devpts_mutex);
 			devpts_pty_kill(tty->link);
+		        mutex_unlock(&devpts_mutex);
 
+	mutex_lock(&devpts_mutex);
+	tty = devpts_get_tty(pts_inode, idx);
+	mutex_unlock(&devpts_mutex);
Quoted 8 lines

drivers/tty/pty.c static int ptmx_open(struct inode *inode, struct file *filp) · line 636

Quoted 3 lines
drivers/tty/pty.c +2 -0
+	mutex_lock(&devpts_mutex);
 	tty = tty_init_dev(ptm_driver, index);
+	mutex_unlock(&devpts_mutex);

Conceptually this seems fine, but it would seem cleaner to me to push this mutex into the called functions in devpts; I suspect the lock could be eliminated or at least be made per instance there (which would make massive-container people happy...)

One step at a time. I agree entirely that the ideal case is that devpts_foo is internally locked and coherent. That is an exercise for someone who likes devpts 8)

Alan

#4 lore raw

Re: [PATCH 2/3] pty: Lock the devpts bits privately

Quoted 5 lines · Alan Cox

One step at a time. I agree entirely that the ideal case is that devpts_foo is internally locked and coherent. That is an exercise for someone who likes devpts 8)

OK. I can look at that later; this should detach it enough from the tty locking that the rest is easy.

-hpa

author
#5 lore raw

PATCH 3/3 tty_lock: Localise the lock

In reply to Alan Cox

From: Alan Cox <[email protected]>

In each remaining case the tty_lock is associated with a specific tty. This means we can now lock on a per tty basis. We do need tty_lock_pair() for the pty case. Uglier but still a step in the right direction.

---

 drivers/tty/amiserial.c   |   12 ++++----
 drivers/tty/cyclades.c    |    2 +
 drivers/tty/n_r3964.c     |   11 ++++---
 drivers/tty/pty.c         |   23 +++++++++------
 drivers/tty/synclink.c    |    4 +--
 drivers/tty/synclink_gt.c |    4 +--
 drivers/tty/synclinkmp.c  |    4 +--
 drivers/tty/tty_io.c      |   67 +++++++++++++++++++++++++++------------------
 drivers/tty/tty_ldisc.c   |   30 ++++++++++----------
 drivers/tty/tty_mutex.c   |   60 ++++++++++++++++++++++++++++++----------
 drivers/tty/tty_port.c    |    6 ++--
 include/linux/tty.h       |   23 +++++++++------
 12 files changed, 149 insertions(+), 97 deletions(-)
drivers/tty/amiserial.c +6 -6
index 6cc4358..b88a65c 100644
@@ -1033,7 +1033,7 @@ static int get_serial_info(struct tty_struct *tty, struct serial_state *state,
 	if (!retinfo)
 		return -EFAULT;
 	memset(&tmp, 0, sizeof(tmp));
-	tty_lock();
+	tty_lock(tty);
 	tmp.line = tty->index;
 	tmp.port = state->port;
 	tmp.flags = state->tport.flags;
@@ -1042,7 +1042,7 @@ static int get_serial_info(struct tty_struct *tty, struct serial_state *state,
 	tmp.close_delay = state->tport.close_delay;
 	tmp.closing_wait = state->tport.closing_wait;
 	tmp.custom_divisor = state->custom_divisor;
-	tty_unlock();
+	tty_unlock(tty);
 	if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
 		return -EFAULT;
 	return 0;
@@ -1059,12 +1059,12 @@ static int set_serial_info(struct tty_struct *tty, struct serial_state *state,
 	if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
 		return -EFAULT;
 
-	tty_lock();
+	tty_lock(tty);
 	change_spd = ((new_serial.flags ^ port->flags) & ASYNC_SPD_MASK) ||
 		new_serial.custom_divisor != state->custom_divisor;
 	if (new_serial.irq || new_serial.port != state->port ||
 			new_serial.xmit_fifo_size != state->xmit_fifo_size) {
-		tty_unlock();
+		tty_unlock(tty);
 		return -EINVAL;
 	}
   
@@ -1084,7 +1084,7 @@ static int set_serial_info(struct tty_struct *tty, struct serial_state *state,
 	}
 
 	if (new_serial.baud_base < 9600) {
-		tty_unlock();
+		tty_unlock(tty);
 		return -EINVAL;
 	}
 
@@ -1116,7 +1116,7 @@ check_and_exit:
 		}
 	} else
 		retval = startup(tty, state);
-	tty_unlock();
+	tty_unlock(tty);
 	return retval;
 }
 
drivers/tty/cyclades.c +1 -1
index e61cabd..6984e1a 100644
@@ -1599,7 +1599,7 @@ static int cy_open(struct tty_struct *tty, struct file *filp)
 	 * If the port is the middle of closing, bail out now
 	 */
 	if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) {
-		wait_event_interruptible_tty(info->port.close_wait,
+		wait_event_interruptible_tty(tty, info->port.close_wait,
 				!(info->port.flags & ASYNC_CLOSING));
 		return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
 	}
drivers/tty/n_r3964.c +6 -5
index 5c6c314..656ad93 100644
@@ -1065,7 +1065,8 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
 
 	TRACE_L("read()");
 
-	tty_lock();
+	/* FIXME: should use a private lock */
+	tty_lock(tty);
 
 	pClient = findClient(pInfo, task_pid(current));
 	if (pClient) {
@@ -1077,7 +1078,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
 				goto unlock;
 			}
 			/* block until there is a message: */
-			wait_event_interruptible_tty(pInfo->read_wait,
+			wait_event_interruptible_tty(tty, pInfo->read_wait,
 					(pMsg = remove_msg(pInfo, pClient)));
 		}
 
@@ -1107,7 +1108,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
 	}
 	ret = -EPERM;
 unlock:
-	tty_unlock();
+	tty_unlock(tty);
 	return ret;
 }
 
@@ -1156,7 +1157,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
 	pHeader->locks = 0;
 	pHeader->owner = NULL;
 
-	tty_lock();
+	tty_lock(tty);
 
 	pClient = findClient(pInfo, task_pid(current));
 	if (pClient) {
@@ -1175,7 +1176,7 @@ static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
 	add_tx_queue(pInfo, pHeader);
 	trigger_transmit(pInfo);
 
-	tty_unlock();
+	tty_unlock(tty);
 
 	return 0;
 }
drivers/tty/pty.c +13 -10
index 5505ffc..d6fa842 100644
@@ -47,6 +47,7 @@ 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;
+	/* Review - krefs on tty_link ?? */
 	if (!tty->link)
 		return;
 	tty->link->packet = 0;
@@ -62,9 +63,7 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
 		        mutex_unlock(&devpts_mutex);
 		}
 #endif
-		tty_unlock();
 		tty_vhangup(tty->link);
-		tty_lock();
 	}
 }
 
@@ -622,26 +621,29 @@ static int ptmx_open(struct inode *inode, struct file *filp)
 		return retval;
 
 	/* find a device that is not in use. */
-	tty_lock();
+	mutex_lock(&devpts_mutex);
 	index = devpts_new_index(inode);
-	tty_unlock();
 	if (index < 0) {
 		retval = index;
 		goto err_file;
 	}
 
+	mutex_unlock(&devpts_mutex);
+
 	mutex_lock(&tty_mutex);
 	mutex_lock(&devpts_mutex);
 	tty = tty_init_dev(ptm_driver, index);
-	mutex_unlock(&devpts_mutex);
-	tty_lock();
-	mutex_unlock(&tty_mutex);
 
 	if (IS_ERR(tty)) {
 		retval = PTR_ERR(tty);
 		goto out;
 	}
 
+	/* The tty returned here is locked so we can safely
+	   drop the mutex */
+	mutex_unlock(&devpts_mutex);
+	mutex_unlock(&tty_mutex);
+
 	set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
 
 	tty_add_file(tty, filp);
@@ -654,16 +656,17 @@ static int ptmx_open(struct inode *inode, struct file *filp)
 	if (retval)
 		goto err_release;
 
-	tty_unlock();
+	tty_unlock(tty);
 	return 0;
 err_release:
-	tty_unlock();
+	tty_unlock(tty);
 	tty_release(inode, filp);
 	return retval;
 out:
+	mutex_unlock(&tty_mutex);
 	devpts_kill_index(inode, index);
-	tty_unlock();
 err_file:
+        mutex_unlock(&devpts_mutex);
 	tty_free_file(filp);
 	return retval;
 }
drivers/tty/synclink.c +2 -2
index d063b7d..bc1ae4a 100644
@@ -3338,9 +3338,9 @@ static int block_til_ready(struct tty_struct *tty, struct file * filp,
 			printk("%s(%d):block_til_ready blocking on %s count=%d\n",
 				 __FILE__,__LINE__, tty->driver->name, port->count );
 				 
-		tty_unlock();
+		tty_unlock(tty);
 		schedule();
-		tty_lock();
+		tty_lock(tty);
 	}
 	
 	set_current_state(TASK_RUNNING);
drivers/tty/synclink_gt.c +2 -2
index fcde827..df4f0dc 100644
@@ -3337,9 +3337,9 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
 		}
 
 		DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
-		tty_unlock();
+		tty_unlock(tty);
 		schedule();
-		tty_lock();
+		tty_lock(tty);
 	}
 
 	set_current_state(TASK_RUNNING);
drivers/tty/synclinkmp.c +2 -2
index a040a46..845502f 100644
@@ -3358,9 +3358,9 @@ static int block_til_ready(struct tty_struct *tty, struct file *filp,
 			printk("%s(%d):%s block_til_ready() count=%d\n",
 				 __FILE__,__LINE__, tty->driver->name, port->count );
 
-		tty_unlock();
+		tty_unlock(tty);
 		schedule();
-		tty_lock();
+		tty_lock(tty);
 	}
 
 	set_current_state(TASK_RUNNING);
drivers/tty/tty_io.c +40 -27

234 lines not on this page · raw message

drivers/tty/tty_ldisc.c +15 -15
index 24b95db..fa65cde 100644
@@ -567,7 +567,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 	if (IS_ERR(new_ldisc))
 		return PTR_ERR(new_ldisc);
 
-	tty_lock();
+	tty_lock(tty);
 	/*
 	 *	We need to look at the tty locking here for pty/tty pairs
 	 *	when both sides try to change in parallel.
@@ -581,12 +581,12 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 	 */
 
 	if (tty->ldisc->ops->num == ldisc) {
-		tty_unlock();
+		tty_unlock(tty);
 		tty_ldisc_put(new_ldisc);
 		return 0;
 	}
 
-	tty_unlock();
+	tty_unlock(tty);
 	/*
 	 *	Problem: What do we do if this blocks ?
 	 *	We could deadlock here
@@ -594,7 +594,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 
 	tty_wait_until_sent(tty, 0);
 
-	tty_lock();
+	tty_lock(tty);
 	mutex_lock(&tty->ldisc_mutex);
 
 	/*
@@ -604,10 +604,10 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 
 	while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
 		mutex_unlock(&tty->ldisc_mutex);
-		tty_unlock();
+		tty_unlock(tty);
 		wait_event(tty_ldisc_wait,
 			test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
-		tty_lock();
+		tty_lock(tty);
 		mutex_lock(&tty->ldisc_mutex);
 	}
 
@@ -622,7 +622,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 
 	o_ldisc = tty->ldisc;
 
-	tty_unlock();
+	tty_unlock(tty);
 	/*
 	 *	Make sure we don't change while someone holds a
 	 *	reference to the line discipline. The TTY_LDISC bit
@@ -649,7 +649,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 
 	retval = tty_ldisc_wait_idle(tty, 5 * HZ);
 
-	tty_lock();
+	tty_lock(tty);
 	mutex_lock(&tty->ldisc_mutex);
 
 	/* handle wait idle failure locked */
@@ -664,7 +664,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 		clear_bit(TTY_LDISC_CHANGING, &tty->flags);
 		mutex_unlock(&tty->ldisc_mutex);
 		tty_ldisc_put(new_ldisc);
-		tty_unlock();
+		tty_unlock(tty);
 		return -EIO;
 	}
 
@@ -707,7 +707,7 @@ enable:
 	if (o_work)
 		schedule_work(&o_tty->buf.work);
 	mutex_unlock(&tty->ldisc_mutex);
-	tty_unlock();
+	tty_unlock(tty);
 	return retval;
 }
 
@@ -815,11 +815,11 @@ void tty_ldisc_hangup(struct tty_struct *tty)
 	 * need to wait for another function taking the BTM
 	 */
 	clear_bit(TTY_LDISC, &tty->flags);
-	tty_unlock();
+	tty_unlock(tty);
 	cancel_work_sync(&tty->buf.work);
 	mutex_unlock(&tty->ldisc_mutex);
 retry:
-	tty_lock();
+	tty_lock(tty);
 	mutex_lock(&tty->ldisc_mutex);
 
 	/* At this point we have a closed ldisc and we want to
@@ -830,7 +830,7 @@ retry:
 		if (atomic_read(&tty->ldisc->users) != 1) {
 			char cur_n[TASK_COMM_LEN], tty_n[64];
 			long timeout = 3 * HZ;
-			tty_unlock();
+			tty_unlock(tty);
 
 			while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
 				timeout = MAX_SCHEDULE_TIMEOUT;
@@ -911,10 +911,10 @@ void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
 	 * race with the set_ldisc code path.
 	 */
 
-	tty_unlock();
+	tty_unlock(tty);
 	tty_ldisc_halt(tty);
 	tty_ldisc_flush_works(tty);
-	tty_lock();
+	tty_lock(tty);
 
 	mutex_lock(&tty->ldisc_mutex);
 	/*
drivers/tty/tty_mutex.c +45 -15

74 lines not on this page · raw message

drivers/tty/tty_port.c +3 -3
index d24807a..64f0975 100644
@@ -237,7 +237,7 @@ int tty_port_block_til_ready(struct tty_port *port,
 
 	/* block if port is in the process of being closed */
 	if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
-		wait_event_interruptible_tty(port->close_wait,
+		wait_event_interruptible_tty(tty, port->close_wait,
 				!(port->flags & ASYNC_CLOSING));
 		if (port->flags & ASYNC_HUP_NOTIFY)
 			return -EAGAIN;
@@ -303,9 +303,9 @@ int tty_port_block_til_ready(struct tty_port *port,
 			retval = -ERESTARTSYS;
 			break;
 		}
-		tty_unlock();
+		tty_unlock(tty);
 		schedule();
-		tty_lock();
+		tty_lock(tty);
 	}
 	finish_wait(&port->open_wait, &wait);
 
include/linux/tty.h +14 -9
index 9f47ab5..4990ef2 100644
@@ -268,6 +268,7 @@ struct tty_struct {
 	struct mutex ldisc_mutex;
 	struct tty_ldisc *ldisc;
 
+	struct mutex legacy_mutex;
 	struct mutex termios_mutex;
 	spinlock_t ctrl_lock;
 	/* Termios values are protected by the termios mutex */
@@ -605,8 +606,12 @@ extern long vt_compat_ioctl(struct tty_struct *tty,
 
 /* tty_mutex.c */
 /* functions for preparation of BKL removal */
-extern void __lockfunc tty_lock(void) __acquires(tty_lock);
-extern void __lockfunc tty_unlock(void) __releases(tty_lock);
+extern void __lockfunc tty_lock(struct tty_struct *tty);
+extern void __lockfunc tty_unlock(struct tty_struct *tty);
+extern void __lockfunc tty_lock_pair(struct tty_struct *tty,
+				struct tty_struct *tty2);
+extern void __lockfunc tty_unlock_pair(struct tty_struct *tty,
+				struct tty_struct *tty2);
 
 /*
  * this shall be called only from where BTM is held (like close)
@@ -621,9 +626,9 @@ extern void __lockfunc tty_unlock(void) __releases(tty_lock);
 static inline void tty_wait_until_sent_from_close(struct tty_struct *tty,
 		long timeout)
 {
-	tty_unlock(); /* tty->ops->close holds the BTM, drop it while waiting */
+	tty_unlock(tty); /* tty->ops->close holds the BTM, drop it while waiting */
 	tty_wait_until_sent(tty, timeout);
-	tty_lock();
+	tty_lock(tty);
 }
 
 /*
@@ -638,16 +643,16 @@ static inline void tty_wait_until_sent_from_close(struct tty_struct *tty,
  *
  * Do not use in new code.
  */
-#define wait_event_interruptible_tty(wq, condition)			\
+#define wait_event_interruptible_tty(tty, wq, condition)		\
 ({									\
 	int __ret = 0;							\
 	if (!(condition)) {						\
-		__wait_event_interruptible_tty(wq, condition, __ret);	\
+		__wait_event_interruptible_tty(tty, wq, condition, __ret);	\
 	}								\
 	__ret;								\
 })
 
-#define __wait_event_interruptible_tty(wq, condition, ret)		\
+#define __wait_event_interruptible_tty(tty, wq, condition, ret)		\
 do {									\
 	DEFINE_WAIT(__wait);						\
 									\
@@ -656,9 +661,9 @@ do {									\
 		if (condition)						\
 			break;						\
 		if (!signal_pending(current)) {				\
-			tty_unlock();					\
+			tty_unlock(tty);					\
 			schedule();					\
-			tty_lock();					\
+			tty_lock(tty);					\
 			continue;					\
 		}							\
 		ret = -ERESTARTSYS;					\
maintainer
#7 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

In reply to Alan Cox
Quoted 10 lines · Alan Cox

From: Alan Cox <[email protected]>

In each remaining case the tty_lock is associated with a specific tty. This means we can now lock on a per tty basis. We do need tty_lock_pair() for the pty case. Uglier but still a step in the right direction.

---

Quoted 13 lines
 drivers/tty/amiserial.c   |   12 ++++----
 drivers/tty/cyclades.c    |    2 +
 drivers/tty/n_r3964.c     |   11 ++++---
 drivers/tty/pty.c         |   23 +++++++++------
 drivers/tty/synclink.c    |    4 +--
 drivers/tty/synclink_gt.c |    4 +--
 drivers/tty/synclinkmp.c  |    4 +--
 drivers/tty/tty_io.c      |   67 +++++++++++++++++++++++++++------------------
 drivers/tty/tty_ldisc.c   |   30 ++++++++++----------
 drivers/tty/tty_mutex.c   |   60 ++++++++++++++++++++++++++++++----------
 drivers/tty/tty_port.c    |    6 ++--
 include/linux/tty.h       |   23 +++++++++------
 12 files changed, 149 insertions(+), 97 deletions(-)

You forgot the call in net/bluetooth/rfcomm/tty.c, I'll go fix that up...

maintainer
#8 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

Quoted 25 lines · Greg KH
Quoted 10 lines · Alan Cox

From: Alan Cox <[email protected]>

In each remaining case the tty_lock is associated with a specific tty. This means we can now lock on a per tty basis. We do need tty_lock_pair() for the pty case. Uglier but still a step in the right direction.

---

Quoted 13 lines
 drivers/tty/amiserial.c   |   12 ++++----
 drivers/tty/cyclades.c    |    2 +
 drivers/tty/n_r3964.c     |   11 ++++---
 drivers/tty/pty.c         |   23 +++++++++------
 drivers/tty/synclink.c    |    4 +--
 drivers/tty/synclink_gt.c |    4 +--
 drivers/tty/synclinkmp.c  |    4 +--
 drivers/tty/tty_io.c      |   67 +++++++++++++++++++++++++++------------------
 drivers/tty/tty_ldisc.c   |   30 ++++++++++----------
 drivers/tty/tty_mutex.c   |   60 ++++++++++++++++++++++++++++++----------
 drivers/tty/tty_port.c    |    6 ++--
 include/linux/tty.h       |   23 +++++++++------
 12 files changed, 149 insertions(+), 97 deletions(-)
Quoted 2 lines

You forgot the call in net/bluetooth/rfcomm/tty.c, I'll go fix that up...

And drivers/tty/serial/crisv10.c and
drivers/staging/serial/68360serial.c...

#9 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

In reply to Alan Cox

Hi Alan,

drivers/tty/pty.c static void pty_close(struct tty_struct *tty, struct file *filp) · line 68

Quoted 13 lines · Alan Cox
drivers/tty/pty.c +0 -2
index 5505ffc..d6fa842 100644
@@ -62,9 +63,7 @@ static void pty_close(struct tty_struct *tty, struct file *filp)
                        mutex_unlock(&devpts_mutex);
                }
  #endif
-               tty_unlock();
                tty_vhangup(tty->link);
-               tty_lock();
        }
  }

I don't believe that this change is correct.

Consider the following scenario:

tty_release -> tty_lock -> pty_close -> tty_vhangup -> tty_lock

Which would cause a deadlock.
--
To unsubscribe from this list: send the line "unsubscribe linux-serial" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#10 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

Quoted 5 lines

I don't believe that this change is correct.

Consider the following scenario:

tty_release -> tty_lock -> pty_close -> tty_vhangup -> tty_lock

We hang up tty->link not tty.

It's now a per tty lock. So I think we are ok.

Alan

#11 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

Quoted 9 lines · Alan Cox
Quoted 5 lines

I don't believe that this change is correct.

Consider the following scenario:

tty_release -> tty_lock -> pty_close -> tty_vhangup -> tty_lock

We hang up tty->link not tty.

It's now a per tty lock. So I think we are ok.

Unless we can cause tty->link == tty, in which case:

[ 6522.256890] =============================================
[ 6522.257023] [ INFO: possible recursive locking detected ]
[ 6522.257023] 3.4.0-rc6-next-20120507-sasha-00001-g06a300f #175 Tainted: G        W   
[ 6522.257023] ---------------------------------------------
[ 6522.257023] trinity/18088 is trying to acquire lock:
[ 6522.257023]  (&tty->legacy_mutex){+.+.+.}, at: [<ffffffff82d8a6f2>] tty_lock+0x72/0x80
[ 6522.257023] 
[ 6522.257023] but task is already holding lock:
[ 6522.257023]  (&tty->legacy_mutex){+.+.+.}, at: [<ffffffff82d8a6f2>] tty_lock+0x72/0x80
[ 6522.257023] 
[ 6522.257023] other info that might help us debug this:
[ 6522.257023]  Possible unsafe locking scenario:
[ 6522.257023] 
[ 6522.257023]        CPU0
[ 6522.257023]        ----
[ 6522.257023]   lock(&tty->legacy_mutex);
[ 6522.257023]   lock(&tty->legacy_mutex);
[ 6522.257023] 
[ 6522.257023]  *** DEADLOCK ***
[ 6522.257023] 
[ 6522.257023]  May be due to missing lock nesting notation
[ 6522.257023] 
[ 6522.257023] 1 lock held by trinity/18088:
[ 6522.257023]  #0:  (&tty->legacy_mutex){+.+.+.}, at: [<ffffffff82d8a6f2>] tty_lock+0x72/0x80
[ 6522.257023] 
[ 6522.257023] stack backtrace:
[ 6522.257023] Pid: 18088, comm: trinity Tainted: G        W    3.4.0-rc6-next-20120507-sasha-00001-g06a300f #175
[ 6522.257023] Call Trace:
[ 6522.257023]  [<ffffffff8111a509>] print_deadlock_bug+0x119/0x140
[ 6522.257023]  [<ffffffff8111c6fe>] validate_chain+0x5ee/0x790
[ 6522.257023]  [<ffffffff810f1418>] ? sched_clock_cpu+0x108/0x120
[ 6522.257023]  [<ffffffff8111ccc3>] __lock_acquire+0x423/0x4c0
[ 6522.257023]  [<ffffffff8111ce3c>] lock_acquire+0xdc/0x120
[ 6522.257023]  [<ffffffff82d8a6f2>] ? tty_lock+0x72/0x80
[ 6522.257023]  [<ffffffff82d86b60>] __mutex_lock_common+0x60/0x590
[ 6522.257023]  [<ffffffff82d8a6f2>] ? tty_lock+0x72/0x80
[ 6522.257023]  [<ffffffff82d8a6f2>] ? tty_lock+0x72/0x80
[ 6522.257023]  [<ffffffff82d871c0>] mutex_lock_nested+0x40/0x50
[ 6522.257023]  [<ffffffff82d8a6f2>] tty_lock+0x72/0x80
[ 6522.257023]  [<ffffffff81a2ce34>] __tty_hangup+0x74/0x400
[ 6522.257023]  [<ffffffff82d8a154>] ? _raw_spin_unlock_irqrestore+0x94/0xc0
[ 6522.257023]  [<ffffffff81a2d1e9>] tty_vhangup+0x9/0x10
[ 6522.257023]  [<ffffffff81a36264>] pty_close+0x154/0x160
[ 6522.257023]  [<ffffffff81a2dfcd>] tty_release+0xed/0x4d0
[ 6522.257023]  [<ffffffff8122d0eb>] ? vfs_lock_file+0x3b/0x40
[ 6522.257023]  [<ffffffff8122d18e>] ? locks_remove_posix+0x9e/0xe0
[ 6522.257023]  [<ffffffff811e13ea>] __fput+0x11a/0x2c0
[ 6522.257023]  [<ffffffff811e15a5>] fput+0x15/0x20
[ 6522.257023]  [<ffffffff811dd8b2>] filp_close+0x82/0xa0
[ 6522.257023]  [<ffffffff810bb914>] close_files+0x1b4/0x200
[ 6522.257023]  [<ffffffff810bb760>] ? sys_waitid+0x200/0x200
[ 6522.257023]  [<ffffffff810bb981>] put_files_struct+0x21/0x180
[ 6522.257023]  [<ffffffff82d8a090>] ? _raw_spin_unlock+0x30/0x60
[ 6522.257023]  [<ffffffff810bbb2d>] exit_files+0x4d/0x60
[ 6522.257023]  [<ffffffff810bc7b5>] do_exit+0x285/0x460
[ 6522.257023]  [<ffffffff810e74e1>] ? get_parent_ip+0x11/0x50
[ 6522.257023]  [<ffffffff810bca31>] do_group_exit+0xa1/0xe0
[ 6522.257023]  [<ffffffff810cceb8>] get_signal_to_deliver+0x348/0x3a0
[ 6522.257023]  [<ffffffff810e855d>] ? finish_task_switch+0x8d/0x110
[ 6522.257023]  [<ffffffff8104daf2>] do_signal+0x42/0x120
[ 6522.257023]  [<ffffffff810e74e1>] ? get_parent_ip+0x11/0x50
[ 6522.257023]  [<ffffffff810e7c1e>] ? sub_preempt_count+0xae/0xf0
[ 6522.257023]  [<ffffffff82d8850f>] ? __schedule+0x79f/0x7d0
[ 6522.257023]  [<ffffffff82d8a934>] ? retint_restore_args+0x13/0x13
[ 6522.257023]  [<ffffffff82d8a9bf>] ? retint_signal+0x11/0x92
[ 6522.257023]  [<ffffffff8104dc24>] do_notify_resume+0x54/0xb0
[ 6522.257023]  [<ffffffff82d8a9fb>] retint_signal+0x4d/0x92
author
#12 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

Quoted 5 lines · On Mon, 07 May 2012 18:30:08 +0200 Sasha Levin <[email protected]> wrote:
Quoted 4 lines · Alan Cox
Quoted 4 lines

I don't believe that this change is correct.

Consider the following scenario:

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

tty_release -> tty_lock -> pty_close -> tty_vhangup -> tty_lock

We hang up tty->link not tty.

It's now a per tty lock. So I think we are ok.

Unless we can cause tty->link == tty, in which case:

We should not be able to cause tty->link == tty. So that's a different problem altogether.

tty->link is set to point to the other half of the pty in pty_install and in pty98_unix_install. It's never assigned to the same tty and ptys simply wouldn't work if this wasn't the case.

So whatever your trace is showing, that's not the bug. Something more complicated would appear to be afoot.

Alan

#13 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

Quoted 2 lines

So whatever your trace is showing, that's not the bug. Something more complicated would appear to be afoot.

Oddly enough, tty != tty->link, but the lockdep warning triggers.

Any idea why it might happen?

#14 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

Quoted 6 lines · Sasha Levin
Quoted 2 lines

So whatever your trace is showing, that's not the bug. Something more complicated would appear to be afoot.

Oddly enough, tty != tty->link, but the lockdep warning triggers.

Any idea why it might happen?

I think so, both locks are the same lockdep class. So lockdep thinks it is the same lock. However this is a false positive. I guess we need mutex_lock_nested...

regards,

Signature · 2 lines
js
suse labs
#15 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

Quoted 11 lines · Jiri Slaby
Quoted 6 lines · Sasha Levin
Quoted 2 lines

So whatever your trace is showing, that's not the bug. Something more complicated would appear to be afoot.

Oddly enough, tty != tty->link, but the lockdep warning triggers.

Any idea why it might happen?

I think so, both locks are the same lockdep class. So lockdep thinks it is the same lock. However this is a false positive. I guess we need mutex_lock_nested...

It looks like it causes an actual deadlock, and hung_tasks screams if left alone for a bit, so it doesn't look like a lockdep issue.

#16 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

Quoted 7 lines · Sasha Levin
Quoted 6 lines · Jiri Slaby
Quoted 5 lines · Sasha Levin
Quoted 2 lines

So whatever your trace is showing, that's not the bug. Something more complicated would appear to be afoot.

Oddly enough, tty != tty->link, but the lockdep warning triggers.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Any idea why it might happen?

I think so, both locks are the same lockdep class. So lockdep thinks it is the same lock. However this is a false positive. I guess we need mutex_lock_nested...

It looks like it causes an actual deadlock, and hung_tasks screams if left alone for a bit, so it doesn't look like a lockdep issue.

I've applied the patch that unlocks before hangup, and started seeing this new warning instead (sorry if output below looks a bit broken, linux-next has a printk rework in progress):

[   47.919734] =============================================
[   47.920013] [ INFO: possible recursive locking detected ]
[   47.920013] 3.4.0-rc6-next-20120511-sasha-00001-g1975c5f #183
Tainted: G        W
[   47.920013] ---------------------------------------------
[   47.920013] trinity/6825 is trying to acquire lock:
[   47.920013]  (&tty->legacy_mutex){+.+.+.}, at: [<ffffffff82e9cd02>]
tty_lock+0x72/0x80
[   47.920013]
but task is already holding lock:
[   47.920013]  (&tty->legacy_mutex){+.+.+.}, at: [<ffffffff82e9cd02>]
tty_lock+0x72/0x80
[   47.920013]
other info that might help us debug this:
[   47.920013]  Possible unsafe locking scenario:
[   47.920013]        CPU0
[   47.920013]        ----
[   47.920013]   lock(&tty->legacy_mutex);
[   47.920013]   lock(&tty->legacy_mutex);
[   47.920013]
 *** DEADLOCK ***
[   47.920013]  May be due to missing lock nesting notation
[   47.920013] 2 locks held by trinity/6825:
[   47.920013]  #0:  (tty_mutex){+.+.+.}, at: [<ffffffff81b00837>]
tty_release+0x177/0x4d0
[   47.920013]  #1:  (&tty->legacy_mutex){+.+.+.}, at:
[<ffffffff82e9cd02>] tty_lock+0x72/0x80
[   47.920013]
stack backtrace:
[   47.920013] Pid: 6825, comm: trinity Tainted: G        W
3.4.0-rc6-next-20120511-sasha-00001-g1975c5f #183
[   47.920013] Call Trace:
[   47.920013]  [<ffffffff811440b9>] print_deadlock_bug+0x119/0x140
[   47.920013]  [<ffffffff8114622e>] validate_chain+0x5ee/0x790
[   47.920013]  [<ffffffff81119eb8>] ? sched_clock_cpu+0x108/0x120
[   47.920013]  [<ffffffff811467f3>] __lock_acquire+0x423/0x4c0
[   47.920013]  [<ffffffff81146a1a>] lock_acquire+0x18a/0x1e0
[   47.920013]  [<ffffffff82e9cd02>] ? tty_lock+0x72/0x80
[   47.920013]  [<ffffffff82e99558>] ? __mutex_lock_common+0x518/0x590
[   47.920013]  [<ffffffff82e990a0>] __mutex_lock_common+0x60/0x590
[   47.920013]  [<ffffffff82e9cd02>] ? tty_lock+0x72/0x80
[   47.920013]  [<ffffffff82e9cd02>] ? tty_lock+0x72/0x80
[   47.920013]  [<ffffffff82e99700>] mutex_lock_nested+0x40/0x50
[   47.920013]  [<ffffffff82e9cd02>] tty_lock+0x72/0x80
[   47.920013]  [<ffffffff82e9cd36>] tty_lock_pair+0x26/0x54
[   47.920013]  [<ffffffff81b00842>] tty_release+0x182/0x4d0
[   47.920013]  [<ffffffff8122cf1a>] __fput+0x11a/0x2c0
[   47.920013]  [<ffffffff8122d0d5>] fput+0x15/0x20
[   47.920013]  [<ffffffff812293e2>] filp_close+0x82/0xa0
[   47.920013]  [<ffffffff810d6214>] close_files+0x1b4/0x200
[   47.920013]  [<ffffffff810d6060>] ? wait_task_stopped+0x3c0/0x3c0
[   47.920013]  [<ffffffff810d6425>] ? exit_files+0x45/0x60
[   47.920013]  [<ffffffff810d6281>] put_files_struct+0x21/0x180
[   47.920013]  [<ffffffff82e9c6a0>] ? _raw_spin_unlock+0x30/0x60
[   47.920013]  [<ffffffff810d642d>] exit_files+0x4d/0x60
[   47.920013]  [<ffffffff810d86e2>] do_exit+0x322/0x500
[   47.920013]  [<ffffffff810d8961>] do_group_exit+0xa1/0xe0
[   47.920013]  [<ffffffff810d89b2>] sys_exit_group+0x12/0x20
[   47.920013]  [<ffffffff82e9d7f9>] system_call_fastpath+0x16/0x1b
maintainer
#17 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

Quoted 14 lines · Sasha Levin
Quoted 13 lines · Sasha Levin
Quoted 11 lines · Jiri Slaby
Quoted 6 lines · Sasha Levin
Quoted 2 lines

So whatever your trace is showing, that's not the bug. Something more complicated would appear to be afoot.

Oddly enough, tty != tty->link, but the lockdep warning triggers.

Any idea why it might happen?

I think so, both locks are the same lockdep class. So lockdep thinks it is the same lock. However this is a false positive. I guess we need mutex_lock_nested...

Quoted 6 lines
Quoted 2 lines

It looks like it causes an actual deadlock, and hung_tasks screams if left alone for a bit, so it doesn't look like a lockdep issue.

I've applied the patch that unlocks before hangup, and started seeing this new warning instead (sorry if output below looks a bit broken, linux-next has a printk rework in progress):

The printk rework should now be working again in linux-next. Is it still causing problems for you?

thanks,

greg k-h

#18 lore raw

Re: [PATCH 3/3] tty_lock: Localise the lock

Quoted 2 lines · Greg KH

The printk rework should now be working again in linux-next.  Is it still causing problems for you?

Mostly does, there's still a minor issue regarding showing timestamps on each line in the log, I've commented about it in the original thread (
http://permalink.gmane.org/gmane.linux.kernel/1295580).

It's fairly visible in the lockdep spew, since lockdep does lots of this "printk("\nsomething\n");"
--
To unsubscribe from this list: send the line "unsubscribe linux-serial" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html