RE: [PATCH][RFC] Implement arch primitives for busywait loops
From: David Laight <hidden>
Date: 2016-09-16 11:59:57
Also in:
linux-arch
From: Nicholas Piggin
Sent: 16 September 2016 12:52 On Fri, 16 Sep 2016 11:30:58 +0000 David Laight [off-list ref] wrote: =20quoted
From: Nicholas Pigginquoted
Sent: 16 September 2016 09:58 Implementing busy wait loops with cpu_relax() in callers poses some difficulties for powerpc. First, we want to put our SMT thread into a low priority mode for the duration of the loop, but then return to normal priority after exitin=
g
quoted
quoted
the loop. Dependong on the CPU design, 'HMT_low() ; HMT_medium();' a=
s
quoted
quoted
cpu_relax() does may have HMT_medium take effect before HMT_low made any (or much) difference. Second, it can be beneficial for some implementations to spin on the exit condition with a statically predicted-not-taken branch (i.e., always predict the loop will exit). This is a quick RFC with a couple of users converted to see what people think. I don't use a C branch with hints, because we don't wan=
t
quoted
quoted
the compiler moving the loop body out of line, which makes it a bit messy unfortunately. If there's a better way to do it, I'm all ears.I think it will still all go wrong if the conditional isn't trivial. In particular if the condition contains || or && it is likely to have a branch - which could invert the loop.=20 I don't know that it will. =20 Yes, if we have exit condition that requires more branches in order to be computed then we lose our nice property of never taking a branch miss on loop exit. But we still avoid *this* branch miss, and still prevent multiple iterations of the wait loop being speculatively executed concurrently when there's no work to be done. =20 And C doesn't know about the loop, so it can't do any transformation except to compute the final condition. =20 Or have I missed something?
Try putting the code inside a conditional or at the bottom of a loop.
gcc can replicate code to remove a branch.
So:
for (;;) {
a;
if (b)
c;
d;
}
can become:
x1:
a;
if (b) to x2;
d;
goto x1;
x2:
c;
d;
goto x1;
Which won't work.
David