public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] - Fix memory ordering problem in wake_futex()
@ 2005-12-23 16:38 Jack Steiner
  2005-12-23 17:05 ` Joe Seigh
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Jack Steiner @ 2005-12-23 16:38 UTC (permalink / raw)
  To: linux-kernel; +Cc: linux-ia64


Here is a fix for a ugly race condition that occurs in wake_futex() on IA64.

On IA64, locks are released using a "st.rel" instruction. This ensures that
preceding "stores" are visible before the lock is released but does NOT prevent
a "store" that follows the "st.rel" from becoming visible before the "st.rel".
The result is that the task that owns the futex_q continues prematurely. 

The failure I saw is the task that owned the futex_q resumed prematurely and
was context-switch off of the cpu. The task's switch_stack occupied the same
space of the futex_q. The store to q->lock_ptr overwrote the ar.bspstore in the
switch_stack. When the task resumed, it ran with a corrupted ar.bspstore.
Things went downhill from there.

Without the fix, the application fails roughly every 10 minutes. With
the fix, it ran 16 hours without a failure.

----
Fix a memory ordering problem that occurs on IA64. The "store" to q->lock_ptr
in wake_futex() can become visible before wake_up_all() clears the lock in the
futex_q. 


	Signed-off-by: Jack Steiner <steiner@sgi.com>





Index: linux/kernel/futex.c
===================================================================
--- linux.orig/kernel/futex.c	2005-12-22 15:05:43.821889257 -0600
+++ linux/kernel/futex.c	2005-12-22 15:30:21.617973325 -0600
@@ -287,7 +287,13 @@ static void wake_futex(struct futex_q *q
 	/*
 	 * The waiting task can free the futex_q as soon as this is written,
 	 * without taking any locks.  This must come last.
+	 *
+	 * A memory barrier is required here to prevent the following store
+	 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
+	 * at the end of wake_up_all() does not prevent this store from
+	 * moving.
 	 */
+	wmb();
 	q->lock_ptr = NULL;
 }
 

^ permalink raw reply	[flat|nested] 13+ messages in thread
* Re: [PATCH] - Fix memory ordering problem in wake_futex()
@ 2005-12-23 22:23 Manfred Spraul
  2005-12-23 22:52 ` Manfred Spraul
  2005-12-24  3:45 ` Jack Steiner
  0 siblings, 2 replies; 13+ messages in thread
From: Manfred Spraul @ 2005-12-23 22:23 UTC (permalink / raw)
  To: Jack Steiner; +Cc: Linux Kernel Mailing List

Jack wrote:

>On IA64, locks are released using a "st.rel" instruction. This ensures that
>preceding "stores" are visible before the lock is released but does NOT prevent
>a "store" that follows the "st.rel" from becoming visible before the "st.rel".
>The result is that the task that owns the futex_q continues prematurely. 
>
>The failure I saw is the task that owned the futex_q resumed prematurely and
>was context-switch off of the cpu. The task's switch_stack occupied the same
>space of the futex_q. The store to q->lock_ptr overwrote the ar.bspstore in the
>switch_stack.
>
Bad race.
Unfortuantely the scenario that you describe is quite frequent:
- autoremove_wake_function()
- ipc/sem.c (search for IN_WAKEUP)
- ipc/msg.c appears to be correct, there are smp_wmb() calls.

--
    Manfred

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

end of thread, other threads:[~2005-12-27 16:31 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-12-23 16:38 [PATCH] - Fix memory ordering problem in wake_futex() Jack Steiner
2005-12-23 17:05 ` Joe Seigh
2005-12-23 20:48 ` Olof Johansson
2005-12-23 21:32   ` Jack Steiner
2005-12-23 21:59     ` Olof Johansson
2005-12-23 23:48       ` Robin Holt
2005-12-24 13:45 ` Jack Steiner
2005-12-24 18:13   ` Olof Johansson
2005-12-27 16:30     ` Jack Steiner
  -- strict thread matches above, loose matches on Subject: below --
2005-12-23 22:23 Manfred Spraul
2005-12-23 22:52 ` Manfred Spraul
2005-12-24  3:45 ` Jack Steiner
2005-12-25 16:02   ` Manfred Spraul

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox