linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] futex: Avoid wake_futex for a PI futex_q
@ 2012-11-21  7:36 Darren Hart
  2012-11-21 20:16 ` [tip:core/locking] " tip-bot for Darren Hart
       [not found] ` <20121121143454.d72866e8.akpm@linux-foundation.org>
  0 siblings, 2 replies; 3+ messages in thread
From: Darren Hart @ 2012-11-21  7:36 UTC (permalink / raw)
  To: Linux Kernel Mailing List
  Cc: Dave Jones, Darren Hart, Thomas Gleixner, Peter Zijlstra,
	Ingo Molnar, John Kacur, stable

Dave Jones reported a bug with futex_lock_pi() that his trinity test
exposed. Sometime between queue_me() and taking the q.lock_ptr, the
lock_ptr became NULL, resulting in a crash.

While futex_wake() is careful to not call wake_futex() on futex_q's with
a pi_state or an rt_waiter (which are either waiting for a
futex_unlock_pi() or a PI futex_requeue()), futex_wake_op() and
futex_requeue() do not perform the same test.

Update futex_wake_op() and futex_requeue() to test for q.pi_state and
q.rt_waiter and abort with -EINVAL if detected. To ensure any future
breakage is caught, add a WARN() to wake_futex() if the same condition
is true.

This fix has seen 3 hours of testing with "trinity -c futex" on an
x86_64 VM with 4 CPUS.

Signed-off-by: Darren Hart <dvhart@linux.intel.com>
Reported-by: Dave Jones <davej@redat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: John Kacur <jkacur@redhat.com>
Cc: stable@vger.kernel.org
---
 kernel/futex.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index 3717e7b..5699b21 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -840,6 +840,11 @@ static void wake_futex(struct futex_q *q)
 {
 	struct task_struct *p = q->task;
 
+	if (q->pi_state || q->rt_waiter) {
+		WARN(1, "%s: refusing to wake PI futex\n", __FUNCTION__);
+		return;
+	}
+
 	/*
 	 * We set q->lock_ptr = NULL _before_ we wake up the task. If
 	 * a non-futex wake up happens on another CPU then the task
@@ -1075,6 +1080,10 @@ retry_private:
 
 	plist_for_each_entry_safe(this, next, head, list) {
 		if (match_futex (&this->key, &key1)) {
+			if (this->pi_state || this->rt_waiter) {
+				ret = -EINVAL;
+				goto out_unlock;
+			}
 			wake_futex(this);
 			if (++ret >= nr_wake)
 				break;
@@ -1087,6 +1096,10 @@ retry_private:
 		op_ret = 0;
 		plist_for_each_entry_safe(this, next, head, list) {
 			if (match_futex (&this->key, &key2)) {
+				if (this->pi_state || this->rt_waiter) {
+					ret = -EINVAL;
+					goto out_unlock;
+				}
 				wake_futex(this);
 				if (++op_ret >= nr_wake2)
 					break;
@@ -1095,6 +1108,7 @@ retry_private:
 		ret += op_ret;
 	}
 
+out_unlock:
 	double_unlock_hb(hb1, hb2);
 out_put_keys:
 	put_futex_key(&key2);
@@ -1384,9 +1398,13 @@ retry_private:
 		/*
 		 * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
 		 * be paired with each other and no other futex ops.
+		 *
+		 * We should never be requeueing a futex_q with a pi_state,
+		 * which is awaiting a futex_unlock_pi().
 		 */
 		if ((requeue_pi && !this->rt_waiter) ||
-		    (!requeue_pi && this->rt_waiter)) {
+		    (!requeue_pi && this->rt_waiter) ||
+		    this->pi_state) {
 			ret = -EINVAL;
 			break;
 		}
-- 
1.7.11.7


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [tip:core/locking] futex: Avoid wake_futex for a PI futex_q
  2012-11-21  7:36 [PATCH] futex: Avoid wake_futex for a PI futex_q Darren Hart
@ 2012-11-21 20:16 ` tip-bot for Darren Hart
       [not found] ` <20121121143454.d72866e8.akpm@linux-foundation.org>
  1 sibling, 0 replies; 3+ messages in thread
From: tip-bot for Darren Hart @ 2012-11-21 20:16 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: linux-kernel, hpa, mingo, jkacur, dvhart, peterz, davej, tglx

Commit-ID:  0e8f7a5954be13d0c8dcbca3204a9e962498c46e
Gitweb:     http://git.kernel.org/tip/0e8f7a5954be13d0c8dcbca3204a9e962498c46e
Author:     Darren Hart <dvhart@linux.intel.com>
AuthorDate: Tue, 20 Nov 2012 23:36:45 -0800
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Wed, 21 Nov 2012 21:05:34 +0100

futex: Avoid wake_futex for a PI futex_q

Dave Jones reported a bug with futex_lock_pi() that his trinity test
exposed. Sometime between queue_me() and taking the q.lock_ptr, the
lock_ptr became NULL, resulting in a crash.

While futex_wake() is careful to not call wake_futex() on futex_q's with
a pi_state or an rt_waiter (which are either waiting for a
futex_unlock_pi() or a PI futex_requeue()), futex_wake_op() and
futex_requeue() do not perform the same test.

Update futex_wake_op() and futex_requeue() to test for q.pi_state and
q.rt_waiter and abort with -EINVAL if detected. To ensure any future
breakage is caught, add a WARN() to wake_futex() if the same condition
is true.

This fix has seen 3 hours of testing with "trinity -c futex" on an
x86_64 VM with 4 CPUS.

Reported-by: Dave Jones <davej@redat.com>
Signed-off-by: Darren Hart <dvhart@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: John Kacur <jkacur@redhat.com>
Cc: stable@vger.kernel.org
Link: http://lkml.kernel.org/r/3b25c8ba053760892871713ff6e81660433f6734.1353483196.git.dvhart@linux.intel.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/futex.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/kernel/futex.c b/kernel/futex.c
index 3717e7b..5699b21 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -840,6 +840,11 @@ static void wake_futex(struct futex_q *q)
 {
 	struct task_struct *p = q->task;
 
+	if (q->pi_state || q->rt_waiter) {
+		WARN(1, "%s: refusing to wake PI futex\n", __FUNCTION__);
+		return;
+	}
+
 	/*
 	 * We set q->lock_ptr = NULL _before_ we wake up the task. If
 	 * a non-futex wake up happens on another CPU then the task
@@ -1075,6 +1080,10 @@ retry_private:
 
 	plist_for_each_entry_safe(this, next, head, list) {
 		if (match_futex (&this->key, &key1)) {
+			if (this->pi_state || this->rt_waiter) {
+				ret = -EINVAL;
+				goto out_unlock;
+			}
 			wake_futex(this);
 			if (++ret >= nr_wake)
 				break;
@@ -1087,6 +1096,10 @@ retry_private:
 		op_ret = 0;
 		plist_for_each_entry_safe(this, next, head, list) {
 			if (match_futex (&this->key, &key2)) {
+				if (this->pi_state || this->rt_waiter) {
+					ret = -EINVAL;
+					goto out_unlock;
+				}
 				wake_futex(this);
 				if (++op_ret >= nr_wake2)
 					break;
@@ -1095,6 +1108,7 @@ retry_private:
 		ret += op_ret;
 	}
 
+out_unlock:
 	double_unlock_hb(hb1, hb2);
 out_put_keys:
 	put_futex_key(&key2);
@@ -1384,9 +1398,13 @@ retry_private:
 		/*
 		 * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
 		 * be paired with each other and no other futex ops.
+		 *
+		 * We should never be requeueing a futex_q with a pi_state,
+		 * which is awaiting a futex_unlock_pi().
 		 */
 		if ((requeue_pi && !this->rt_waiter) ||
-		    (!requeue_pi && this->rt_waiter)) {
+		    (!requeue_pi && this->rt_waiter) ||
+		    this->pi_state) {
 			ret = -EINVAL;
 			break;
 		}

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] futex: Avoid wake_futex for a PI futex_q
       [not found] ` <20121121143454.d72866e8.akpm@linux-foundation.org>
@ 2012-11-21 23:35   ` Darren Hart
  0 siblings, 0 replies; 3+ messages in thread
From: Darren Hart @ 2012-11-21 23:35 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Linux Kernel Mailing List, Dave Jones, Thomas Gleixner,
	Peter Zijlstra, Ingo Molnar, John Kacur, stable



On 11/21/2012 02:34 PM, Andrew Morton wrote:
> On Tue, 20 Nov 2012 23:36:45 -0800
> Darren Hart <dvhart@linux.intel.com> wrote:
> 
>> Dave Jones reported a bug with futex_lock_pi() that his trinity test
>> exposed. Sometime between queue_me() and taking the q.lock_ptr, the
>> lock_ptr became NULL, resulting in a crash.
>>
>> While futex_wake() is careful to not call wake_futex() on futex_q's with
>> a pi_state or an rt_waiter (which are either waiting for a
>> futex_unlock_pi() or a PI futex_requeue()), futex_wake_op() and
>> futex_requeue() do not perform the same test.
>>
>> Update futex_wake_op() and futex_requeue() to test for q.pi_state and
>> q.rt_waiter and abort with -EINVAL if detected. To ensure any future
>> breakage is caught, add a WARN() to wake_futex() if the same condition
>> is true.
>>
>> This fix has seen 3 hours of testing with "trinity -c futex" on an
>> x86_64 VM with 4 CPUS.
>>
>> ...
>>
>> --- a/kernel/futex.c
>> +++ b/kernel/futex.c
>> @@ -840,6 +840,11 @@ static void wake_futex(struct futex_q *q)
>>  {
>>  	struct task_struct *p = q->task;
>>  
>> +	if (q->pi_state || q->rt_waiter) {
>> +		WARN(1, "%s: refusing to wake PI futex\n", __FUNCTION__);
>> +		return;
>> +	}
> 
> There's no need to display __FUNCTION__ because WARN() gives a
> backtrace, and we can more neatly use the WARN() return value:
> 
> --- a/kernel/futex.c~futex-avoid-wake_futex-for-a-pi-futex_q-fix
> +++ a/kernel/futex.c
> @@ -843,10 +843,8 @@ static void wake_futex(struct futex_q *q
>  {
>  	struct task_struct *p = q->task;
>  
> -	if (q->pi_state || q->rt_waiter) {
> -		WARN(1, "%s: refusing to wake PI futex\n", __FUNCTION__);
> +	if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
>  		return;
> -	}

Thanks, that's better. Duh. That block of code used to be a lot more
complex during my debug sessions, I should have caught that and boiled
it down one step further.

--
Darren


>  
>  	/*
>  	 * We set q->lock_ptr = NULL _before_ we wake up the task. If
> _
> 
>>
>> ...
>>

-- 
Darren Hart
Intel Open Source Technology Center
Yocto Project - Technical Lead - Linux Kernel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-11-22 19:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-21  7:36 [PATCH] futex: Avoid wake_futex for a PI futex_q Darren Hart
2012-11-21 20:16 ` [tip:core/locking] " tip-bot for Darren Hart
     [not found] ` <20121121143454.d72866e8.akpm@linux-foundation.org>
2012-11-21 23:35   ` [PATCH] " Darren Hart

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).