On Tue, 2013-10-01 at 16:01 -0400, Waiman Long wrote:I may be missing something. My understanding is that barrier onlyBut this is the lock function, a barrier() call should be enough to
The cpu could still be executing out of order load instruction from the
critical section before checking node->locked? Probably smp_mb() is
still needed.
Tim
prevent the critical section from creeping up there. We certainly need
some kind of memory barrier at the end of the unlock function.
prevents the compiler from rearranging instructions, but not for cpu out
of order execution (as in smp_mb). So cpu could read memory in the next
critical section, before node->locked is true, (i.e. unlock has been
completed). If we only have a simple barrier at end of mcs_lock, then
say the code on CPU1 is
mcs_lock
x = 1;
...
x = 2;
mcs_unlock
and CPU 2 is
mcs_lock
y = x;
...
mcs_unlock
We expect y to be 2 after the "y = x" assignment. But we
we may execute the code as
CPU1 CPU2
x = 1;
... y = x; ( y=1, out of order load)
x = 2
mcs_unlock
Check node->locked==true
continue executing critical section (y=1 when we expect y=2)
So we get y to be 1 when we expect that it should be 2. Adding smp_mb
after the node->locked check in lock code
ACCESS_ONCE(prev->next) = node;
/* Wait until the lock holder passes the lock down */
while (!ACCESS_ONCE(node->locked))
arch_mutex_cpu_relax();
smp_mb();
should prevent this scenario.
Thanks.
Tim