public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Darren Hart <dvhltc@us.ibm.com>
To: linux-kernel@vger.kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@elte.hu>,
	Rusty Russell <rusty@rustcorp.com.au>,
	Darren Hart <dvhltc@us.ibm.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@elte.hu>,
	Rusty Russell <rusty@rustcorp.com.au>
Subject: [PATCH 3/6] futex: add double_unlock_hb()
Date: Thu, 12 Mar 2009 00:55:52 -0700	[thread overview]
Message-ID: <20090312075552.9856.48021.stgit@Aeon> (raw)
In-Reply-To: <20090312075349.9856.83687.stgit@Aeon>

The futex code uses double_lock_hb() which locks the hb->lock's in pointer
value order.  There is no parallel unlock routine, and the code unlocks them
in name order, ignoring pointer value.  This opens up a window for an ABBA
deadlock.  This patch adds double_unlock_hb() to remove the window as well
as refactor the duplicated code segments.

Build and boot tested on a 4 way Intel x86_64 workstation.  Passes basic
pthread_mutex and PI tests out of ltp/testcases/realtime.

Signed-off-by: Darren Hart <dvhltc@us.ibm.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Rusty Russell <rusty@rustcorp.com.au>
---

 kernel/futex.c |   29 +++++++++++++++++------------
 1 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index 4000454..e149545 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -690,6 +690,19 @@ double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
 	}
 }
 
+static inline void
+double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
+{
+	if (hb1 <= hb2) {
+		spin_unlock(&hb2->lock);
+		if (hb1 < hb2)
+			spin_unlock(&hb1->lock);
+	} else { /* hb1 > hb2 */
+		spin_unlock(&hb1->lock);
+		spin_unlock(&hb2->lock);
+	}
+}
+
 /*
  * Wake up waiters matching bitset queued on this futex (uaddr).
  */
@@ -767,9 +780,7 @@ retry:
 	if (unlikely(op_ret < 0)) {
 		u32 dummy;
 
-		spin_unlock(&hb1->lock);
-		if (hb1 != hb2)
-			spin_unlock(&hb2->lock);
+		double_unlock_hb(hb1, hb2);
 
 #ifndef CONFIG_MMU
 		/*
@@ -833,9 +844,7 @@ retry:
 		ret += op_ret;
 	}
 
-	spin_unlock(&hb1->lock);
-	if (hb1 != hb2)
-		spin_unlock(&hb2->lock);
+	double_unlock_hb(hb1, hb2);
 out_put_keys:
 	put_futex_key(fshared, &key2);
 out_put_key1:
@@ -876,9 +885,7 @@ retry:
 		ret = get_futex_value_locked(&curval, uaddr1);
 
 		if (unlikely(ret)) {
-			spin_unlock(&hb1->lock);
-			if (hb1 != hb2)
-				spin_unlock(&hb2->lock);
+			double_unlock_hb(hb1, hb2);
 
 			put_futex_key(fshared, &key2);
 			put_futex_key(fshared, &key1);
@@ -925,9 +932,7 @@ retry:
 	}
 
 out_unlock:
-	spin_unlock(&hb1->lock);
-	if (hb1 != hb2)
-		spin_unlock(&hb2->lock);
+	double_unlock_hb(hb1, hb2);
 
 	/* drop_futex_key_refs() must be called outside the spinlocks. */
 	while (--drop_count >= 0)


  parent reply	other threads:[~2009-03-12  7:56 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-12  7:55 [PATCH 0/6] Futex fixes and cleanups Darren Hart
2009-03-12  7:55 ` [PATCH 1/6] Update futex commentary Darren Hart
2009-03-12 10:24   ` [tip:core/futexes] futex: update " Darren Hart
2009-03-12  7:55 ` [PATCH 2/6] Additional (get|put)_futex_key() fixes Darren Hart
2009-03-12 10:16   ` Ingo Molnar
2009-03-12 13:42     ` Thomas Gleixner
2009-03-12 23:22       ` Darren Hart
2009-03-12 10:24   ` [tip:core/futexes] futex: additional " Darren Hart
2009-03-13  0:20     ` Ingo Molnar
2009-03-13  5:46       ` Darren Hart
2009-03-13  0:24   ` [tip:core/urgent] " Darren Hart
2009-03-12  7:55 ` Darren Hart [this message]
2009-03-12 10:07   ` [PATCH 3/6] futex: add double_unlock_hb() Peter Zijlstra
2009-03-12 10:10     ` Ingo Molnar
2009-03-12 10:58       ` Thomas Gleixner
2009-03-12 15:13         ` Darren Hart
2009-03-12 10:24   ` [tip:core/futexes] " Darren Hart
2009-03-12  7:55 ` [PATCH 4/6] futex: Use current->time_slack_ns for rt tasks too Darren Hart
2009-03-12 10:11   ` Peter Zijlstra
2009-03-12 10:24   ` [tip:core/futexes] futex: use " Darren Hart
2009-03-12 13:53     ` Arjan van de Ven
2009-03-12 14:02       ` Peter Zijlstra
2009-03-12 14:25         ` Thomas Gleixner
2009-03-12 14:48           ` Peter Zijlstra
2009-03-12 15:01             ` Arjan van de Ven
2009-03-12 21:33               ` Darren Hart
2009-03-12 21:43                 ` Thomas Gleixner
2009-03-12 21:29         ` Darren Hart
2009-03-12  7:56 ` [PATCH 5/6] futex: unlock before returning -EFAULT Darren Hart
2009-03-12 10:13   ` Peter Zijlstra
2009-03-12 10:47     ` Thomas Gleixner
2009-03-12 11:06       ` Peter Zijlstra
2009-03-12 15:15         ` Darren Hart
2009-03-12 22:17     ` Darren Hart
2009-03-12 10:24   ` [tip:core/futexes] " Darren Hart
2009-03-13  0:24   ` [tip:core/urgent] " Darren Hart
2009-03-12  7:56 ` [PATCH 6/6] futex: cleanup fault logic Darren Hart
2009-03-12 10:15   ` Peter Zijlstra
2009-03-12 15:09     ` Darren Hart
2009-03-12 10:25   ` [tip:core/futexes] futex: clean up " Darren Hart
2009-03-12 12:22 ` [PATCH 0/6] Futex fixes and cleanups Ingo Molnar
2009-03-12 15:21   ` Darren Hart

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=20090312075552.9856.48021.stgit@Aeon \
    --to=dvhltc@us.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=peterz@infradead.org \
    --cc=rusty@rustcorp.com.au \
    --cc=tglx@linutronix.de \
    /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