Re: spin_unlock optimization(i386)

Helge Hafting (helge.hafting@idb.hist.no)
Fri, 26 Nov 1999 10:52:18 +0100


Erich Boleyn wrote:
> > ok. I think we can actually prove wether reads can even execute noncausal.
> > Lets consider the following code:
> >
> > CPU0 CPU1
> >
> > volatile int i, __fill[8], j; int a, b;
> >
> > i = j = 0;
> >
> > for (;;) { for (;;) {
> > i++; a = i;
> > j++; b = j;
> > } if (a < b)
> > BUG()
> > }

> To guarantee that you've observed the store to "i", you HAVE to read
> the store to "j" first! Plus, your second "for" loop could have blocked
> just after reading "i" into "a", while the first loop could be continuing
> to count upward. "b" could end up with an arbitrarily larger value.
>
> To make this work correctly in the face of both Processor Ordering
> and pauses in the process execution, the second loop should actually say:
>
> for (;;) {
> b = j;
> a = i;
> if (a < b)
> BUG();
> }

Don't be confused when this too eventually breaks when tested.
The counters will wrap, i slightly before j, and that may trigger the
BUG().

For a safe test, change the first loop to:

i = j = 0;

for (;;) {
i++;
j++;
if (i == 0x7FFFFFFF) {
j = 0;
i = 0;
}

Helge Hafting

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/