Skip to contentSkip to conversation

tty BKL pushdown

RFC PATCH In review Alan Cox 2 participants 7 messages Nov 18, 2009 – Nov 20, 2009

Subsystems (2)
lore
Share & tools
lore

Alan Cox

author
#0 lore raw

cover tty BKL pushdown

First steps to pushing the BKL down further into the tty layer. For review and analysis not application !

---

Alan Cox (5):
tty: split the lock up a bit further
tty: Move the leader test in disassociate
tty: Push the bkl down a bit in the hangup code
tty: Push the lock down further into the ldisc code
tty: push the BKL down into the handlers a bit

 drivers/char/pty.c       |    2 -
 drivers/char/tty_io.c    |  150 ++++++++++++++++++++++++++--------------------
 drivers/char/tty_ldisc.c |   23 ++++++-
 include/linux/tty.h      |    2 -
 kernel/exit.c            |    2 -
 5 files changed, 109 insertions(+), 70 deletions(-)
author
#1 lore raw

RFC PATCH 1/5 tty: push the BKL down into the handlers a bit

Start trying to untangle the remaining BKL mess

---

 drivers/char/pty.c       |    2 -
 drivers/char/tty_io.c    |  140 ++++++++++++++++++++++++++--------------------
 drivers/char/tty_ldisc.c |   13 ++++
 include/linux/tty.h      |    2 -
 4 files changed, 94 insertions(+), 63 deletions(-)
drivers/char/pty.c +1 -1
index d516e9c..169cdcc 100644
@@ -659,7 +659,7 @@ static int __ptmx_open(struct inode *inode, struct file *filp)
 	if (!retval)
 		return 0;
 out1:
-	tty_release_dev(filp);
+	tty_release(inode, filp);
 	return retval;
 out:
 	devpts_kill_index(inode, index);
drivers/char/tty_io.c +79 -61

356 lines not on this page · raw message

drivers/char/tty_ldisc.c +13 -0
index feb5507..d914e77 100644
@@ -34,6 +34,8 @@
 #include <linux/vt_kern.h>
 #include <linux/selection.h>
 
+#include <linux/smp_lock.h>	/* For the moment */
+
 #include <linux/kmod.h>
 #include <linux/nsproxy.h>
 
@@ -545,6 +547,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 	if (IS_ERR(new_ldisc))
 		return PTR_ERR(new_ldisc);
 
+	lock_kernel();
 	/*
 	 *	We need to look at the tty locking here for pty/tty pairs
 	 *	when both sides try to change in parallel.
@@ -558,6 +561,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 	 */
 
 	if (tty->ldisc->ops->num == ldisc) {
+		unlock_kernel();
 		tty_ldisc_put(new_ldisc);
 		return 0;
 	}
@@ -569,6 +573,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 
 	tty_wait_until_sent(tty, 0);
 
+	unlock_kernel();
 	mutex_lock(&tty->ldisc_mutex);
 
 	/*
@@ -582,6 +587,9 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 			test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
 		mutex_lock(&tty->ldisc_mutex);
 	}
+
+	lock_kernel();
+
 	set_bit(TTY_LDISC_CHANGING, &tty->flags);
 
 	/*
@@ -592,6 +600,8 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 	tty->receive_room = 0;
 
 	o_ldisc = tty->ldisc;
+
+	unlock_kernel();
 	/*
 	 *	Make sure we don't change while someone holds a
 	 *	reference to the line discipline. The TTY_LDISC bit
@@ -617,12 +627,14 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 	flush_scheduled_work();
 
 	mutex_lock(&tty->ldisc_mutex);
+	lock_kernel();
 	if (test_bit(TTY_HUPPED, &tty->flags)) {
 		/* We were raced by the hangup method. It will have stomped
 		   the ldisc data and closed the ldisc down */
 		clear_bit(TTY_LDISC_CHANGING, &tty->flags);
 		mutex_unlock(&tty->ldisc_mutex);
 		tty_ldisc_put(new_ldisc);
+		unlock_kernel();
 		return -EIO;
 	}
 
@@ -664,6 +676,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 	if (o_work)
 		schedule_delayed_work(&o_tty->buf.work, 1);
 	mutex_unlock(&tty->ldisc_mutex);
+	unlock_kernel();
 	return retval;
 }
 
include/linux/tty.h +1 -1
index e6da667..405a903 100644
@@ -449,7 +449,7 @@ extern void initialize_tty_struct(struct tty_struct *tty,
 		struct tty_driver *driver, int idx);
 extern struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
 								int first_ok);
-extern void tty_release_dev(struct file *filp);
+extern int tty_release(struct inode *inode, struct file *filp);
 extern int tty_init_termios(struct tty_struct *tty);
 
 extern struct tty_struct *tty_pair_get_tty(struct tty_struct *tty);
#2 lore raw

Re: [RFC PATCH 1/5] tty: push the BKL down into the handlers a bit

Quoted 11 lines · Alan Cox

Start trying to untangle the remaining BKL mess

---

 drivers/char/pty.c       |    2 -
 drivers/char/tty_io.c    |  140 ++++++++++++++++++++++++++--------------------
 drivers/char/tty_ldisc.c |   13 ++++
 include/linux/tty.h      |    2 -
 4 files changed, 94 insertions(+), 63 deletions(-)
  1565                  if (o_tty->termios != tty->driver->other->termios[idx]) {
  1566                          unlock_kernel();
  1567                          printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
  1568                                            "not o_termios for (%s)\n",
  1569                                 idx, tty->name);
  1570                          return 0;
  1571                  }
  1572                  if (o_tty->link != tty) {

We would need an unlock_kernel() here.

  1573                          printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
  1574                          return 0;
  1575                  }

regards,
dan carpenter

author
#3 lore raw

PATCH 2/5 tty: Push the lock down further into the ldisc code

In reply to Alan Cox

---

 drivers/char/tty_io.c    |    2 --
 drivers/char/tty_ldisc.c |   12 +++++++++---
 2 files changed, 9 insertions(+), 5 deletions(-)
drivers/char/tty_io.c +0 -2
index 05e94a2..a1b1ff6 100644
@@ -1347,9 +1347,7 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
 	 * If we fail here just call release_tty to clean up.  No need
 	 * to decrement the use counts, as release_tty doesn't care.
 	 */
-	lock_kernel();
 	retval = tty_ldisc_setup(tty, tty->link);
-	unlock_kernel();
 	if (retval)
 		goto release_mem_out;
 	return tty;
drivers/char/tty_ldisc.c +9 -3
index d914e77..3f653f7 100644
@@ -445,8 +445,14 @@ static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
 {
 	WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
-	if (ld->ops->open)
-		return ld->ops->open(tty);
+	if (ld->ops->open) {
+		int ret;
+                /* BKL here locks versus a hangup event */
+		lock_kernel();
+		ret = ld->ops->open(tty);
+		unlock_kernel();
+		return ret;
+	}
 	return 0;
 }
 
@@ -566,6 +572,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 		return 0;
 	}
 
+	unlock_kernel();
 	/*
 	 *	Problem: What do we do if this blocks ?
 	 *	We could deadlock here
@@ -573,7 +580,6 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc)
 
 	tty_wait_until_sent(tty, 0);
 
-	unlock_kernel();
 	mutex_lock(&tty->ldisc_mutex);
 
 	/*
author
#4 lore raw

PATCH 3/5 tty: Push the bkl down a bit in the hangup code

In reply to Alan Cox

We know that the redirect field is handled via its own locking in all places

---

 drivers/char/tty_io.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/char/tty_io.c +2 -2
index a1b1ff6..f4a0404 100644
@@ -505,8 +505,6 @@ static void do_tty_hangup(struct work_struct *work)
 	if (!tty)
 		return;
 
-	/* inuse_filps is protected by the single kernel lock */
-	lock_kernel();
 
 	spin_lock(&redirect_lock);
 	if (redirect && redirect->private_data == tty) {
@@ -515,6 +513,8 @@ static void do_tty_hangup(struct work_struct *work)
 	}
 	spin_unlock(&redirect_lock);
 
+	/* inuse_filps is protected by the single kernel lock */
+	lock_kernel();
 	check_tty_count(tty, "do_tty_hangup");
 	file_list_lock();
 	/* This breaks for file handles being sent over AF_UNIX sockets ? */
author
#5 lore raw

PATCH 4/5 tty: Move the leader test in disassociate

In reply to Alan Cox

There are two call points, both want to check that tty->signal->leader is set. Move the test into disassociate_ctty() as that will make locking changes easier in a bit

---

 drivers/char/tty_io.c |    5 +++--
 kernel/exit.c         |    2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)
drivers/char/tty_io.c +3 -2
index f4a0404..aee68f5 100644
@@ -707,6 +707,8 @@ void disassociate_ctty(int on_exit)
 	struct tty_struct *tty;
 	struct pid *tty_pgrp = NULL;
 
+	if (!current->signal->leader)
+		return;
 
 	tty = get_current_tty();
 	if (tty) {
@@ -772,8 +774,7 @@ void no_tty(void)
 {
 	struct task_struct *tsk = current;
 	lock_kernel();
-	if (tsk->signal->leader)
-		disassociate_ctty(0);
+	disassociate_ctty(0);
 	unlock_kernel();
 	proc_clear_tty(tsk);
 }
kernel/exit.c +1 -1
index f7864ac..dbca05b 100644
@@ -970,7 +970,7 @@ NORET_TYPE void do_exit(long code)
 	exit_thread();
 	cgroup_exit(tsk, 1);
 
-	if (group_dead && tsk->signal->leader)
+	if (group_dead)
 		disassociate_ctty(1);
 
 	module_put(task_thread_info(tsk)->exec_domain->module);
author
#6 lore raw

PATCH 5/5 tty: split the lock up a bit further

In reply to Alan Cox

The tty count sanity check may need the BKL, that isn't clear. However it is clear that the count use of the lock is internal and independant of the bigger use of the lock.

Furthermore the file list locking is also separately locked already

---

 drivers/char/tty_io.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
drivers/char/tty_io.c +3 -0
index aee68f5..4c2f793 100644
@@ -516,6 +516,8 @@ static void do_tty_hangup(struct work_struct *work)
 	/* inuse_filps is protected by the single kernel lock */
 	lock_kernel();
 	check_tty_count(tty, "do_tty_hangup");
+	unlock_kernel();
+
 	file_list_lock();
 	/* This breaks for file handles being sent over AF_UNIX sockets ? */
 	list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
@@ -529,6 +531,7 @@ static void do_tty_hangup(struct work_struct *work)
 	}
 	file_list_unlock();
 
+	lock_kernel();
 	tty_ldisc_hangup(tty);
 
 	read_lock(&tasklist_lock);