From: Will Deacon <hidden> Date: 2012-08-07 11:57:25
Hello,
ARM recently moved to asm-generic/mutex-xchg.h for its mutex implementation
after our previous implementation was found to be missing some crucial
memory barriers. However, I'm seeing some problems running hackbench on
SMP platforms due to the way in which the MUTEX_SPIN_ON_OWNER code operates.
The symptoms are that a bunch of hackbench tasks are left waiting on an
unlocked mutex and therefore never get woken up to claim it. I think this
boils down to the following sequence:
Task A Task B Task C Lock value
0 1
1 lock() 0
2 lock() 0
3 spin(A) 0
4 unlock() 1
5 lock() 0
6 cmpxchg(1,0) 0
7 contended() -1
8 lock() 0
9 spin(C) 0
10 unlock() 1
11 cmpxchg(1,0) 0
12 unlock() 1
At this point, the lock is unlocked, but Task B is in an uninterruptible
sleep with nobody to wake it up.
The following patch fixes the problem by ensuring we put the lock into
the contended state if we acquire it from the spin loop on the slowpath
but I'd like to be sure that this won't cause problems with other mutex
implementations:
From: Peter Zijlstra <hidden> Date: 2012-08-07 13:48:55
On Tue, 2012-08-07 at 12:56 +0100, Will Deacon wrote:
Hello,
ARM recently moved to asm-generic/mutex-xchg.h for its mutex implementation
after our previous implementation was found to be missing some crucial
memory barriers.
This is a76d7bd96d ("ARM: 7467/1: mutex: use generic xchg-based
implementation for ARMv6+"), right? Why do you use xchg and not dec
based? The changelog mumbles something about shorter critical sections,
but me not knowing anything about ARM wonders about the why of that.
quoted hunk
However, I'm seeing some problems running hackbench on
SMP platforms due to the way in which the MUTEX_SPIN_ON_OWNER code operates.
The symptoms are that a bunch of hackbench tasks are left waiting on an
unlocked mutex and therefore never get woken up to claim it. I think this
boils down to the following sequence:
Task A Task B Task C Lock value
0 1
1 lock() 0
2 lock() 0
3 spin(A) 0
4 unlock() 1
5 lock() 0
6 cmpxchg(1,0) 0
7 contended() -1
8 lock() 0
9 spin(C) 0
10 unlock() 1
11 cmpxchg(1,0) 0
12 unlock() 1
At this point, the lock is unlocked, but Task B is in an uninterruptible
sleep with nobody to wake it up.
The following patch fixes the problem by ensuring we put the lock into
the contended state if we acquire it from the spin loop on the slowpath
but I'd like to be sure that this won't cause problems with other mutex
implementations:
@@ -170,7 +170,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,if(owner&&!mutex_spin_on_owner(lock,owner))break;-if(atomic_cmpxchg(&lock->count,1,0)==1){+if(atomic_cmpxchg(&lock->count,1,-1)==1){lock_acquired(&lock->dep_map,ip);mutex_set_owner(lock);preempt_enable();
But in this case, either B is still spinning in our spin-loop, or it has
already passed the atomic_xchg(&lock->count, -1) when we fell out.
Since you say B is in UNINTERRUPTIBLE state, we'll assume it fell
through and so the lock count should be -1 (or less) to mark it
contended.
From: Will Deacon <hidden> Date: 2012-08-07 14:05:23
On Tue, Aug 07, 2012 at 02:48:42PM +0100, Peter Zijlstra wrote:
On Tue, 2012-08-07 at 12:56 +0100, Will Deacon wrote:
quoted
ARM recently moved to asm-generic/mutex-xchg.h for its mutex implementation
after our previous implementation was found to be missing some crucial
memory barriers.
This is a76d7bd96d ("ARM: 7467/1: mutex: use generic xchg-based
implementation for ARMv6+"), right? Why do you use xchg and not dec
based? The changelog mumbles something about shorter critical sections,
but me not knowing anything about ARM wonders about the why of that.
Correct, that's the patch. We don't have atomic add/sub instructions on ARM,
so instead we have to do:
1: ldrex ... @ Exclusive load
add/sub ... @ Do the arithmetic
strex ... @ Exclusive store
cmp ... @ Check the store succeeded
bne 1b @ Retry if we weren't atomic
So using dec adds a sub where we wouldn't need an instruction there for xchg.
I suspect there's no measurable difference between the two, but we use the
xchg-based implementation for CPUs prior to ARMv6 so it saves an ifdef as
well. Some discussion on the original patch here:
http://lists.infradead.org/pipermail/linux-arm-kernel/2012-July/109333.html
quoted
Task A Task B Task C Lock value
0 1
1 lock() 0
2 lock() 0
3 spin(A) 0
4 unlock() 1
5 lock() 0
6 cmpxchg(1,0) 0
7 contended() -1
8 lock() 0
9 spin(C) 0
10 unlock() 1
11 cmpxchg(1,0) 0
12 unlock() 1
At this point, the lock is unlocked, but Task B is in an uninterruptible
sleep with nobody to wake it up.
[...]
But in this case, either B is still spinning in our spin-loop, or it has
already passed the atomic_xchg(&lock->count, -1) when we fell out.
Yes, it does that xchg on line 7 (see the lock value of -1)...
Since you say B is in UNINTERRUPTIBLE state, we'll assume it fell
through and so the lock count should be -1 (or less) to mark it
contended.
... but then A sets it straight back to 0 in __mutex_fastpath_lock and falls
down the slowpath due to it being contended. The problem is that it doesn't
restore the -1 when it acquires the lock on line 11, so B is never woken up.
Will
From: Will Deacon <hidden> Date: 2012-08-07 17:34:09
On Tue, Aug 07, 2012 at 06:14:36PM +0100, Nicolas Pitre wrote:
On Tue, 7 Aug 2012, Will Deacon wrote:
quoted
The symptoms are that a bunch of hackbench tasks are left waiting on an
unlocked mutex and therefore never get woken up to claim it. I think this
boils down to the following sequence:
Task A Task B Task C Lock value
0 1
1 lock() 0
2 lock() 0
3 spin(A) 0
4 unlock() 1
5 lock() 0
6 cmpxchg(1,0) 0
7 contended() -1
8 lock() 0
9 spin(C) 0
10 unlock() 1
11 cmpxchg(1,0) 0
12 unlock() 1
At this point, the lock is unlocked, but Task B is in an uninterruptible
sleep with nobody to wake it up.
I fail to see how the lock value would go from -1 to 0 on line 8. How
does that happen?
What I think is happening is that B writes the -1 in __mutex_lock_common
and, after seeing a NULL owner (C may not have set that yet), drops through
to the:
if (atomic_xchg(&lock->count, -1) == 1)
goto done;
bit. At the same time, A does a mutex_lock, which goes down the fastpath:
if (unlikely(atomic_xchg(count, 0) != 1))
fail_fn(count);
setting the count to 0. It then trundles off down the slowpath and spins on
the new owner (C).
Maybe my diagram is confusing... the lock value is supposed to be the value
*after* the relevant operations on that same line have completed.
@@ -170,7 +170,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,if(owner&&!mutex_spin_on_owner(lock,owner))break;-if(atomic_cmpxchg(&lock->count,1,0)==1){+if(atomic_cmpxchg(&lock->count,1,-1)==1){lock_acquired(&lock->dep_map,ip);mutex_set_owner(lock);preempt_enable();
This would force invokation of the slow path on unlock even if in most
cases the lock is unlikely to be contended. The really slow path does
check if the waiting list is empty and sets the count to 0 before
exiting to avoid that. I don't see how this could be done safely in the
spin_on_owner loop code as the lock->wait_lock isn't held (which appears
to be the point of this code in the first place).
Indeed, it will trigger the slowpath on the next unlock but only in the case
that the lock was contended. You're right that there might not be any
waiters though, and we'd need to take the spinlock to check that.
Yet, if the lock is heavily contended with a waiting task, the count
should never get back to 1 and the cmpxchg on line 11 would not set the
count to 0. Hence my interrogation about line 8 above.
Hmm. __mutex_fastpath_unlock always sets the count to 1:
if (unlikely(atomic_xchg(count, 1) != 0))
failt_fn(count);
so there's always a window for a spinning waiter (as opposed to one blocked
in the queue) to succeed in the cmpxchg.
Unless I'm barking up the wrong tree!
Will
From: Will Deacon <hidden> Date: 2012-08-07 17:38:25
On Tue, Aug 07, 2012 at 06:33:44PM +0100, Will Deacon wrote:
What I think is happening is that B writes the -1 in __mutex_lock_common
and, after seeing a NULL owner (C may not have set that yet), drops through
to the:
if (atomic_xchg(&lock->count, -1) == 1)
goto done;
Sorry, should have proofread that. I meant to say:
What I think is happening is that B writes the -1 in __mutex_lock_common
after seeing a NULL owner (C may not have set that yet) and dropping through
to the:
if (atomic_xchg(&lock->count, -1) == 1)
goto done;
Will
From: Nicolas Pitre <nico@fluxnic.net> Date: 2012-08-07 18:14:50
On Tue, 7 Aug 2012, Will Deacon wrote:
Hello,
ARM recently moved to asm-generic/mutex-xchg.h for its mutex implementation
after our previous implementation was found to be missing some crucial
memory barriers. However, I'm seeing some problems running hackbench on
SMP platforms due to the way in which the MUTEX_SPIN_ON_OWNER code operates.
The symptoms are that a bunch of hackbench tasks are left waiting on an
unlocked mutex and therefore never get woken up to claim it. I think this
boils down to the following sequence:
Task A Task B Task C Lock value
0 1
1 lock() 0
2 lock() 0
3 spin(A) 0
4 unlock() 1
5 lock() 0
6 cmpxchg(1,0) 0
7 contended() -1
8 lock() 0
9 spin(C) 0
10 unlock() 1
11 cmpxchg(1,0) 0
12 unlock() 1
At this point, the lock is unlocked, but Task B is in an uninterruptible
sleep with nobody to wake it up.
I fail to see how the lock value would go from -1 to 0 on line 8. How
does that happen?
quoted hunk
The following patch fixes the problem by ensuring we put the lock into
the contended state if we acquire it from the spin loop on the slowpath
but I'd like to be sure that this won't cause problems with other mutex
implementations:
@@ -170,7 +170,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,if(owner&&!mutex_spin_on_owner(lock,owner))break;-if(atomic_cmpxchg(&lock->count,1,0)==1){+if(atomic_cmpxchg(&lock->count,1,-1)==1){lock_acquired(&lock->dep_map,ip);mutex_set_owner(lock);preempt_enable();
This would force invokation of the slow path on unlock even if in most
cases the lock is unlikely to be contended. The really slow path does
check if the waiting list is empty and sets the count to 0 before
exiting to avoid that. I don't see how this could be done safely in the
spin_on_owner loop code as the lock->wait_lock isn't held (which appears
to be the point of this code in the first place).
Yet, if the lock is heavily contended with a waiting task, the count
should never get back to 1 and the cmpxchg on line 11 would not set the
count to 0. Hence my interrogation about line 8 above.
Nicolas
From: Nicolas Pitre <nico@fluxnic.net> Date: 2012-08-07 18:28:34
On Tue, 7 Aug 2012, Will Deacon wrote:
On Tue, Aug 07, 2012 at 06:14:36PM +0100, Nicolas Pitre wrote:
quoted
On Tue, 7 Aug 2012, Will Deacon wrote:
quoted
The symptoms are that a bunch of hackbench tasks are left waiting on an
unlocked mutex and therefore never get woken up to claim it. I think this
boils down to the following sequence:
Task A Task B Task C Lock value
0 1
1 lock() 0
2 lock() 0
3 spin(A) 0
4 unlock() 1
5 lock() 0
6 cmpxchg(1,0) 0
7 contended() -1
8 lock() 0
9 spin(C) 0
10 unlock() 1
11 cmpxchg(1,0) 0
12 unlock() 1
At this point, the lock is unlocked, but Task B is in an uninterruptible
sleep with nobody to wake it up.
I fail to see how the lock value would go from -1 to 0 on line 8. How
does that happen?
[...]
Forget that. I assumed cmpxchg when it is just xchg.
(And, for that matter, I'm even the original author for some of that
code.: http://lkml.org/lkml/2005/12/26/83).
Back to thinking.
Nicolas
From: Nicolas Pitre <nico@fluxnic.net> Date: 2012-08-09 05:12:18
On Tue, 7 Aug 2012, Will Deacon wrote:
quoted hunk
Hello,
ARM recently moved to asm-generic/mutex-xchg.h for its mutex implementation
after our previous implementation was found to be missing some crucial
memory barriers. However, I'm seeing some problems running hackbench on
SMP platforms due to the way in which the MUTEX_SPIN_ON_OWNER code operates.
The symptoms are that a bunch of hackbench tasks are left waiting on an
unlocked mutex and therefore never get woken up to claim it. I think this
boils down to the following sequence:
Task A Task B Task C Lock value
0 1
1 lock() 0
2 lock() 0
3 spin(A) 0
4 unlock() 1
5 lock() 0
6 cmpxchg(1,0) 0
7 contended() -1
8 lock() 0
9 spin(C) 0
10 unlock() 1
11 cmpxchg(1,0) 0
12 unlock() 1
At this point, the lock is unlocked, but Task B is in an uninterruptible
sleep with nobody to wake it up.
The following patch fixes the problem by ensuring we put the lock into
the contended state if we acquire it from the spin loop on the slowpath
but I'd like to be sure that this won't cause problems with other mutex
implementations:
@@ -170,7 +170,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,if(owner&&!mutex_spin_on_owner(lock,owner))break;-if(atomic_cmpxchg(&lock->count,1,0)==1){+if(atomic_cmpxchg(&lock->count,1,-1)==1){lock_acquired(&lock->dep_map,ip);mutex_set_owner(lock);preempt_enable();
All comments welcome.
Well... after thinking about this for a while, I came to the conclusion
that the mutex_spin_on_owner code is indeed breaking the contract
between the xchg lock fast path and the slow path. The xchg fast path
will always set the count to 0 and rely on the slow path to restore a
possible pre-existing negative count. So the above change would be
needed for correctness in the xchg case, even if it always forces the
unlock into the slow path.
As I mentioned previously, we don't want to force the slow path in all
cases though. The atomic decrement based fast path doesn't need that,
so I'd suggest this fix instead:
@@ -161,6 +161,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,for(;;){structtask_struct*owner;+intlocked_val;/**Ifthere'sanowner,waitforittoeither
@@ -170,7 +171,19 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,if(owner&&!mutex_spin_on_owner(lock,owner))break;-if(atomic_cmpxchg(&lock->count,1,0)==1){+#ifdef __MUTEX_XCHG_FAST_PATH+/*+*Thefastpathbasedonxchgsetsatransient0count,+*relyingontheslowpathtorestoreapossible+*pre-existingcontendedcount.Withoutcheckingthe+*waiters'listwemustpresumepossiblecontensionhere.+*/+locked_val=-1;+#else+locked_val=0;+#endif++if(atomic_cmpxchg(&lock->count,1,locked_val)==1){lock_acquired(&lock->dep_map,ip);mutex_set_owner(lock);preempt_enable();
That would be needed for the stable tree as well.
A further cleanup could remove all definitions of
__mutex_slowpath_needs_to_unlock() given that they're all set to 1
except for the xchg fast path, in which case __MUTEX_XCHG_FAST_PATH
could be reused instead.
Of course that might tilt the balance towards using mutex-dec.h on ARM
v6 and above instead of mutex-xchg.h. But that is an orthogonal issue,
and that doesn't remove the need for fixing the xchg based case for
correctness.
Nicolas
@@ -170,7 +170,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,if(owner&&!mutex_spin_on_owner(lock,owner))break;-if(atomic_cmpxchg(&lock->count,1,0)==1){+if(atomic_cmpxchg(&lock->count,1,-1)==1){lock_acquired(&lock->dep_map,ip);mutex_set_owner(lock);preempt_enable();
All comments welcome.
Well... after thinking about this for a while, I came to the conclusion
that the mutex_spin_on_owner code is indeed breaking the contract
between the xchg lock fast path and the slow path. The xchg fast path
will always set the count to 0 and rely on the slow path to restore a
possible pre-existing negative count. So the above change would be
needed for correctness in the xchg case, even if it always forces the
unlock into the slow path.
Great, so we agree on that.
As I mentioned previously, we don't want to force the slow path in all
cases though. The atomic decrement based fast path doesn't need that,
so I'd suggest this fix instead:
One minor typo and a suggested alternative below...
@@ -161,6 +161,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,for(;;){structtask_struct*owner;+intlocked_val;/**Ifthere'sanowner,waitforittoeither
@@ -170,7 +171,19 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,if(owner&&!mutex_spin_on_owner(lock,owner))break;-if(atomic_cmpxchg(&lock->count,1,0)==1){+#ifdef __MUTEX_XCHG_FAST_PATH+/*+*Thefastpathbasedonxchgsetsatransient0count,+*relyingontheslowpathtorestoreapossible+*pre-existingcontendedcount.Withoutcheckingthe+*waiters'listwemustpresumepossiblecontensionhere.
s/contension/contention/
+ */
+ locked_val = -1;
+#else
+ locked_val = 0;
+#endif
+
+ if (atomic_cmpxchg(&lock->count, 1, locked_val) == 1) {
lock_acquired(&lock->dep_map, ip);
mutex_set_owner(lock);
preempt_enable();
That would be needed for the stable tree as well.
A further cleanup could remove all definitions of
__mutex_slowpath_needs_to_unlock() given that they're all set to 1
except for the xchg fast path, in which case __MUTEX_XCHG_FAST_PATH
could be reused instead.
I think we could actually fix this entirely in mutex-xchg.h by doing
something in fastpath_lock similar to what we do for trylock:
Of course that might tilt the balance towards using mutex-dec.h on ARM
v6 and above instead of mutex-xchg.h. But that is an orthogonal issue,
and that doesn't remove the need for fixing the xchg based case for
correctness.
I'll do some hackbench runs against mutex-dec once we've decided on the final
xchg code. If it's faster, I'll submit a patch for ARM.
Cheers,
Will
Yes, that looks fine. I'd remove that if (prev < 0) entirely though.
We'll just swap a 0 for a 0 if the count wasn't < 0, or a 0 for a 1 if
the mutex just got unlocked which is also fine. This is especially
beneficial when a native xchg processor instruction is used.
quoted
Of course that might tilt the balance towards using mutex-dec.h on ARM
v6 and above instead of mutex-xchg.h. But that is an orthogonal issue,
and that doesn't remove the need for fixing the xchg based case for
correctness.
I'll do some hackbench runs against mutex-dec once we've decided on the final
xchg code. If it's faster, I'll submit a patch for ARM.
I don't think it would be faster. It is just potentially more fair.
Nicolas
Yes, that looks fine. I'd remove that if (prev < 0) entirely though.
We'll just swap a 0 for a 0 if the count wasn't < 0, or a 0 for a 1 if
the mutex just got unlocked which is also fine. This is especially
beneficial when a native xchg processor instruction is used.
In fact, this suggestion isn't entirely correct either. The inner xchg
in this case should be -1 and not 'count'. If 'count' is 0 and the
mutex becomes contended in the small window between the two xchg's then
the contention mark would be lost again.
In other words, I think this should look like this:
From: Will Deacon <hidden> Date: 2012-08-09 17:50:40
On Thu, Aug 09, 2012 at 05:57:33PM +0100, Nicolas Pitre wrote:
quoted hunk
On Thu, 9 Aug 2012, Nicolas Pitre wrote:
quoted
Yes, that looks fine. I'd remove that if (prev < 0) entirely though.
We'll just swap a 0 for a 0 if the count wasn't < 0, or a 0 for a 1 if
the mutex just got unlocked which is also fine. This is especially
beneficial when a native xchg processor instruction is used.
In fact, this suggestion isn't entirely correct either. The inner xchg
in this case should be -1 and not 'count'. If 'count' is 0 and the
mutex becomes contended in the small window between the two xchg's then
the contention mark would be lost again.
In other words, I think this should look like this:
@@ -25,8 +25,11 @@staticinlinevoid__mutex_fastpath_lock(atomic_t*count,void(*fail_fn)(atomic_t*)){-if(unlikely(atomic_xchg(count,0)!=1))-fail_fn(count);+if(unlikely(atomic_xchg(count,0)!=1)){+/* Mark lock contention explicitly */+if(likely(atomic_xchg(count,-1)!=1))+fail_fn(count);+}}/**
Doesn't this mean that we're no longer just swapping 0 for a 0 if the lock
was taken, therefore needlessly sending the current owner down the slowpath
on unlock? If we have the explicit test, that doesn't happen and the
disassembly isn't much different (an additional cmp/conditional branch).
Will
From: Nicolas Pitre <nico@fluxnic.net> Date: 2012-08-09 18:09:05
On Thu, 9 Aug 2012, Will Deacon wrote:
On Thu, Aug 09, 2012 at 05:57:33PM +0100, Nicolas Pitre wrote:
quoted
On Thu, 9 Aug 2012, Nicolas Pitre wrote:
quoted
Yes, that looks fine. I'd remove that if (prev < 0) entirely though.
We'll just swap a 0 for a 0 if the count wasn't < 0, or a 0 for a 1 if
the mutex just got unlocked which is also fine. This is especially
beneficial when a native xchg processor instruction is used.
In fact, this suggestion isn't entirely correct either. The inner xchg
in this case should be -1 and not 'count'. If 'count' is 0 and the
mutex becomes contended in the small window between the two xchg's then
the contention mark would be lost again.
In other words, I think this should look like this:
@@ -25,8 +25,11 @@staticinlinevoid__mutex_fastpath_lock(atomic_t*count,void(*fail_fn)(atomic_t*)){-if(unlikely(atomic_xchg(count,0)!=1))-fail_fn(count);+if(unlikely(atomic_xchg(count,0)!=1)){+/* Mark lock contention explicitly */+if(likely(atomic_xchg(count,-1)!=1))+fail_fn(count);+}}/**
Doesn't this mean that we're no longer just swapping 0 for a 0 if the lock
was taken, therefore needlessly sending the current owner down the slowpath
on unlock?
If the lock was taken, this means the count was either 0 or -1. If it
was 1 then we just put a 0 there and we own it. But if the cound was 0
then we should store -1 instead, which is what the inner xchg does. If
the count was already -1 then we store -1 back. That more closely mimic
what the atomic dec does which is what we want.
Nicolas
@@ -25,8 +25,11 @@staticinlinevoid__mutex_fastpath_lock(atomic_t*count,void(*fail_fn)(atomic_t*)){-if(unlikely(atomic_xchg(count,0)!=1))-fail_fn(count);+if(unlikely(atomic_xchg(count,0)!=1)){+/* Mark lock contention explicitly */+if(likely(atomic_xchg(count,-1)!=1))+fail_fn(count);+}}/**
Doesn't this mean that we're no longer just swapping 0 for a 0 if the lock
was taken, therefore needlessly sending the current owner down the slowpath
on unlock?
If the lock was taken, this means the count was either 0 or -1. If it
was 1 then we just put a 0 there and we own it. But if the cound was 0
then we should store -1 instead, which is what the inner xchg does. If
the count was already -1 then we store -1 back. That more closely mimic
what the atomic dec does which is what we want.
Ok, I just wasn't sure that marking the lock contended was required when it
was previously locked, given that we'll drop into spinning on the owner
anyway.
I'll add a commit message to the above and re-post if that's ok?
Will
@@ -25,8 +25,11 @@staticinlinevoid__mutex_fastpath_lock(atomic_t*count,void(*fail_fn)(atomic_t*)){-if(unlikely(atomic_xchg(count,0)!=1))-fail_fn(count);+if(unlikely(atomic_xchg(count,0)!=1)){+/* Mark lock contention explicitly */+if(likely(atomic_xchg(count,-1)!=1))+fail_fn(count);+}}/**
Doesn't this mean that we're no longer just swapping 0 for a 0 if the lock
was taken, therefore needlessly sending the current owner down the slowpath
on unlock?
If the lock was taken, this means the count was either 0 or -1. If it
was 1 then we just put a 0 there and we own it. But if the cound was 0
then we should store -1 instead, which is what the inner xchg does. If
the count was already -1 then we store -1 back. That more closely mimic
what the atomic dec does which is what we want.
Ok, I just wasn't sure that marking the lock contended was required when it
was previously locked, given that we'll drop into spinning on the owner
anyway.
That's fine, and the owner will put 1 back when it unlocks it as well as
processing the wait queue which is what we need.
I'll add a commit message to the above and re-post if that's ok?
Sure. Don't forget to update __mutex_fastpath_lock_retval() as well.
Nicolas
From: Nicolas Pitre <nico@fluxnic.net> Date: 2012-08-13 13:35:13
On Mon, 13 Aug 2012, Peter Zijlstra wrote:
OK, I like this.. Thanks guys! Will will you send a final and complete
patch?
Here it is:
--- >8
Date: Fri, 10 Aug 2012 15:22:09 +0100
From: Will Deacon <redacted>
Subject: [PATCH] mutex: place lock in contended state after fastpath_lock failure
ARM recently moved to asm-generic/mutex-xchg.h for its mutex
implementation after the previous implementation was found to be missing
some crucial memory barriers. However, this has revealed some problems
running hackbench on SMP platforms due to the way in which the
MUTEX_SPIN_ON_OWNER code operates.
The symptoms are that a bunch of hackbench tasks are left waiting on an
unlocked mutex and therefore never get woken up to claim it. This boils
down to the following sequence of events:
Task A Task B Task C Lock value
0 1
1 lock() 0
2 lock() 0
3 spin(A) 0
4 unlock() 1
5 lock() 0
6 cmpxchg(1,0) 0
7 contended() -1
8 lock() 0
9 spin(C) 0
10 unlock() 1
11 cmpxchg(1,0) 0
12 unlock() 1
At this point, the lock is unlocked, but Task B is in an uninterruptible
sleep with nobody to wake it up.
This patch fixes the problem by ensuring we put the lock into the
contended state if we fail to acquire it on the fastpath, ensuring that
any blocked waiters are woken up when the mutex is released.
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Thomas Gleixner <redacted>
Cc: Chris Mason <redacted>
Cc: Ingo Molnar <redacted>
Cc: <redacted>
Signed-off-by: Will Deacon <redacted>
Reviewed-by: Nicolas Pitre <redacted>
---
include/asm-generic/mutex-xchg.h | 11 +++++++++--
1 files changed, 9 insertions(+), 2 deletions(-)
From: Peter Zijlstra <hidden> Date: 2012-08-13 14:05:34
On Mon, 2012-08-13 at 09:35 -0400, Nicolas Pitre wrote:
Date: Fri, 10 Aug 2012 15:22:09 +0100
From: Will Deacon <redacted>
Subject: [PATCH] mutex: place lock in contended state after fastpath_lock failure
ARM recently moved to asm-generic/mutex-xchg.h for its mutex
implementation after the previous implementation was found to be missing
some crucial memory barriers. However, this has revealed some problems
running hackbench on SMP platforms due to the way in which the
MUTEX_SPIN_ON_OWNER code operates.
The symptoms are that a bunch of hackbench tasks are left waiting on an
unlocked mutex and therefore never get woken up to claim it. This boils
down to the following sequence of events:
Task A Task B Task C Lock value
0 1
1 lock() 0
2 lock() 0
3 spin(A) 0
4 unlock() 1
5 lock() 0
6 cmpxchg(1,0) 0
7 contended() -1
8 lock() 0
9 spin(C) 0
10 unlock() 1
11 cmpxchg(1,0) 0
12 unlock() 1
At this point, the lock is unlocked, but Task B is in an uninterruptible
sleep with nobody to wake it up.
This patch fixes the problem by ensuring we put the lock into the
contended state if we fail to acquire it on the fastpath, ensuring that
any blocked waiters are woken up when the mutex is released.
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Thomas Gleixner <redacted>
Cc: Chris Mason <redacted>
Cc: Ingo Molnar <redacted>
Cc: <redacted>
Signed-off-by: Will Deacon <redacted>
Reviewed-by: Nicolas Pitre <redacted>
Acked-by: Peter Zijlstra <redacted>
Will you carry this through the ARM tree or do you want me/Ingo to take
it?