Re: [PATCH] documentation: Fix two-CPU control-dependency example

From: Boqun Feng
Date: Thu Jul 20 2017 - 02:13:45 EST


On Wed, Jul 19, 2017 at 10:47:04PM -0700, Paul E. McKenney wrote:
[...]
> > Hi Paul,
> >
> > I know the compiler could optimize atomics in very interesting ways, but
> > this case is about volatile, so I guess our case is still fine? ;-)
>
> Hello, Boqun,
>
> When I asked that question, the answer I got was "the compiler must
> emit the load instruction, but is under no obligation to actually use the
> value loaded".
>
> I don't happen to like that answer, by the way. ;-)
>

Me neither, seems to me the kernel happens to work well at
compiler-optimization's mercy ;-/

With claim like that, compiler could do optimization as turning:

struct task_struct *owner;

for (;;) {
owner = READ_ONCE(lock->owner);
if (owner && !mutex_spin_on_owner(lock, owner))
break;
/* ... */

into:

struct task_struct *owner;

owner = READ_ONCE(lock->owner);

for (;;READ_ONCE(lock->owner)) {
if (owner && !mutex_spin_on_owner(lock, owner))
break;
/* ... */

Because the load executed in every loop, and they just happen to choose
not to use the values. And this is within their rights!

Regards,
Boqun

> Thanx, Paul
>
> > > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0062r1.html
> > >
> >
> > Great material to wake up mind in the morning! Thanks.
> >
> > Regards,
> > Boqun
> >
> > > What are your thoughts on this?
> > >
> > > Thanx, Paul
> > >
> > > > Thanks, Akira
> > > >
> > > > > That said, I very much welcome critical reviews of memory-barriers.txt,
> > > > > so please do feel free to continue doing that!
> > > > >
> > > > > Thanx, Paul
> > > > >
> > > > >
> > > >
> > >
> >
>