public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Ming Lei <ming.lei@canonical.com>,
	linux-kernel@vger.kernel.org, Alan Cox <alan@linux.intel.com>,
	Arnd Bergmann <arnd@arndb.de>
Subject: Re: [PATCH] tty: tty_mutex: fix lockdep warning in tty_lock_pair
Date: Thu, 17 May 2012 20:48:38 +0200	[thread overview]
Message-ID: <1337280518.4281.67.camel@twins> (raw)
In-Reply-To: <20120517182830.GA5254@kroah.com>

On Thu, 2012-05-17 at 11:28 -0700, Greg Kroah-Hartman wrote:
> > +static void __lockfunc tty_lock_nest_lock(struct tty_struct *tty,
> > +             struct tty_struct *tty2)
> 
> Duplicating tty_lock() just for this one issue seems wrong and prone to
> error, don't you think?
> 
> > +{
> > +     if (tty->magic != TTY_MAGIC) {
> > +             printk(KERN_ERR "L Bad %p\n", tty);
> > +             WARN_ON(1);
> > +             return;
> > +     }
> > +     tty_kref_get(tty);
> > +     mutex_lock_nest_lock(&tty->legacy_mutex, &tty2->legacy_mutex);

Yeah, its completely broken, even the lockdep annotation is the wrong
one.

Something like the (completely untested) below patch is the 'right' way.

---
 drivers/tty/tty_mutex.c |   35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/drivers/tty/tty_mutex.c b/drivers/tty/tty_mutex.c
index 69adc80..587330b 100644
--- a/drivers/tty/tty_mutex.c
+++ b/drivers/tty/tty_mutex.c
@@ -10,7 +10,7 @@
  * Getting the big tty mutex.
  */
 
-void __lockfunc tty_lock(struct tty_struct *tty)
+static void __lockfunc tty_lock_nested(struct tty_struct *tty, int subclass)
 {
 	if (tty->magic != TTY_MAGIC) {
 		printk(KERN_ERR "L Bad %p\n", tty);
@@ -18,7 +18,12 @@ void __lockfunc tty_lock(struct tty_struct *tty)
 		return;
 	}
 	tty_kref_get(tty);
-	mutex_lock(&tty->legacy_mutex);
+	mutex_lock_nested(&tty->legacy_mutex, subclass);
+}
+
+void __lockfunc tty_lock(struct tty_struct *tty)
+{
+	tty_lock_nested(tty, 0);
 }
 EXPORT_SYMBOL(tty_lock);
 
@@ -38,25 +43,25 @@ EXPORT_SYMBOL(tty_unlock);
  * Getting the big tty mutex for a pair of ttys with lock ordering
  * On a non pty/tty pair tty2 can be NULL which is just fine.
  */
-void __lockfunc tty_lock_pair(struct tty_struct *tty,
-					struct tty_struct *tty2)
+void __lockfunc tty_lock_pair(struct tty_struct *tty1, struct tty_struct *tty2)
 {
-	if (tty < tty2) {
-		tty_lock(tty);
-		tty_lock(tty2);
-	} else {
-		if (tty2 && tty2 != tty)
-			tty_lock(tty2);
-		tty_lock(tty);
+	if (!tty2 || tty1 == tty2) {
+		tty_lock(tty1);
+		return;
 	}
+
+	if (tty2 < tty1)
+		swap(tty1, tty2);
+
+	tty_lock(tty1);
+	tty_lock_nested(tty2, SINGLE_DEPTH_NESTING);
 }
 EXPORT_SYMBOL(tty_lock_pair);
 
-void __lockfunc tty_unlock_pair(struct tty_struct *tty,
-						struct tty_struct *tty2)
+void __lockfunc tty_unlock_pair(struct tty_struct *tty1, struct tty_struct *tty2)
 {
-	tty_unlock(tty);
-	if (tty2 && tty2 != tty)
+	tty_unlock(tty1);
+	if (tty2 && tty2 != tty1)
 		tty_unlock(tty2);
 }
 EXPORT_SYMBOL(tty_unlock_pair);


  reply	other threads:[~2012-05-17 18:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-17  5:58 [PATCH] tty: tty_mutex: fix lockdep warning in tty_lock_pair Ming Lei
2012-05-17 18:28 ` Greg Kroah-Hartman
2012-05-17 18:48   ` Peter Zijlstra [this message]
2012-05-18  1:57     ` Ming Lei
2012-05-21 11:31       ` Ming Lei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1337280518.4281.67.camel@twins \
    --to=peterz@infradead.org \
    --cc=alan@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ming.lei@canonical.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox