Re: Linux-kernel examples for LKMM recipes

From: Paul E. McKenney
Date: Tue Oct 17 2017 - 14:56:22 EST


On Fri, Oct 13, 2017 at 04:09:26PM -0400, Alan Stern wrote:
> On Fri, 13 Oct 2017, Paul E. McKenney wrote:
> > On Fri, Oct 13, 2017 at 03:44:07PM -0400, Alan Stern wrote:
> > > On Wed, 11 Oct 2017, Paul E. McKenney wrote:

[ . . . ]

> > Perhaps the recipes document should just baldly state that any execution
> > having only one thread and/or having only one variable will be fully
> > ordered?
>
> That wouldn't be a bad idea. (Although the part about only one thread
> should be pretty obvious.)
>
> Also, you have to be a little careful because the ordering of the
> execution may not always agree with the ordering of the source code.
> The compiler is allowed to evaluate the arguments to a function call,
> for example, in any order it likes.

How about this?

0. Simple special cases

If there is only one CPU on the one hand or only one variable
on the other, the code will execute in order. There are (as
usual) some things to be careful of:

a. There are some aspects of the C language that are
unordered. For example, the compiler can output code
computing arguments of a multi-parameter function in
any order it likes, or even interleaved if it so chooses.

b. Compilers are permitted to use the "as-if" rule.
That is, a compiler can emit whatever code it likes,
as long as the results appear just as if the compiler
had followed all the relevant rules. To see this,
compiler with a high level of optimization and run
the debugger on the resulting binary.

c. If there is only one variable but multiple CPUs, all
accesses to that variable must be aligned and full sized.
Variables that straddle cachelines or pages void your
full-ordering warranty, as do undersized accesses that
load from or store to only part of the variable.

Thanx, Paul