Re: [PATCH tip/core/rcu 5/6] Documentation/memory-barriers.txt: Need barriers() for some control dependencies

From: Josh Triplett
Date: Mon Feb 17 2014 - 19:45:31 EST


On Mon, Feb 17, 2014 at 04:17:40PM -0800, Paul E. McKenney wrote:
> On Mon, Feb 17, 2014 at 04:02:47PM -0800, Josh Triplett wrote:
> > On Mon, Feb 17, 2014 at 02:58:16PM -0800, Paul E. McKenney wrote:
> > > On Mon, Feb 17, 2014 at 01:46:06PM -0800, Josh Triplett wrote:
> > > > On Mon, Feb 17, 2014 at 01:26:52PM -0800, Paul E. McKenney wrote:
> > > > > From: "Paul E. McKenney" <paulmck@xxxxxxxxxxxxxxxxxx>
> > > > >
> > > > > Current compilers can "speculate" stores in the case where both legs
> > > > > of the "if" statement start with identical stores. Because the stores
> > > > > are identical, the compiler knows that the store will unconditionally
> > > > > execute regardless of the "if" condition, and so the compiler is within
> > > > > its rights to hoist the store to precede the condition. Such hoisting
> > > > > destroys the control-dependency ordering. This ordering can be restored
> > > > > by placing a barrier() at the beginning of each leg of the "if" statement.
> > > > > This commit adds this requirement to the control-dependencies section.
> > > > >
> > > > > Signed-off-by: Paul E. McKenney <paulmck@xxxxxxxxxxxxxxxxxx>
> > > >
> > > > This is starting to become a rather unreasonable level of fighting the
> > > > compiler. ("Threads cannot be implemented as a library" indeed.) This
> > > > doesn't seem like a reasonable thing to require users to do. Is there
> > > > really no other way to cope with this particular bit of "help" from the
> > > > compiler?
> > >
> > > Well, we could use smp_mb() instead of barrier(), but that was the
> > > sort of thing that Peter Zijlstra was trying to avoid.
> >
> > Yeah, that's not an improvement. The goal would be to make the code no
> > more complex than it already needs to be with ACCESS_ONCE; changing
> > "barrier()" to something else doesn't help (quite apart from smp_mb()
> > being suboptimal).
> >
> > > That said, I do sympathize completely with your position here -- it is
> > > just that it is better to have our compiler-fights documented that
> > > not, right?
> >
> > Sure, better to document them, but better still to not have them. Is
> > there some other way we could avoid this one entirely?
>
> We could try change the standard so as to outlaw pulling common code from
> both legs of an "if" statement, but that will be a serious uphill battle.

And insufficient given widespread use of existing compilers.

> Or perhaps do something to warn the developer about the possibility of
> this happening.
>
> Other thoughts?

Might be worth bringing this up with the GCC folks to find out if
there's something obvious we're missing. (For non-obvious values of
"obvious".)

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