Re: [PATCH 11/18] seqcount: Introduce raw_write_seqcount_barrier()

Oleg Nesterov
2015-06-12T19:00:29Z

On 06/11, Paul E. McKenney wrote:
>
> > > + * seqcount_t seq;
> > > + * bool X = true, Y = false;
> > > + *
> > > + * void read(void)
> > > + * {
> > > + * bool x, y;
> > > + *
> > > + * do {
> > > + * int s = read_seqcount_begin(&seq);
> > > + *
> > > + * x = X; y = Y;
> > > + *
> > > + * } while (read_seqcount_retry(&seq, s));
> > > + *
> > > + * BUG_ON(!x && !y);
> > > + * }
> > > + *
> > > + * void write(void)
> > > + * {
> > > + * Y = true;
> > > + *
> > > + * write_seqcount_begin(seq);
> > > + * write_seqcount_end(seq);
> > > + *
> > > + * X = false;
> > > + * }
> >
> > > +static inline void raw_write_seqcount_barrier(seqcount_t *s)
> > > +{
> > > + s->sequence++;
> > > + smp_wmb();
> > > + s->sequence++;
> > > +}
> > > +
> > > /*
> > > * raw_write_seqcount_latch - redirect readers to even/odd copy
> > > * @s: pointer to seqcount_t
> >
> > Looks good otherwise.
> >
> > Reviewed-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
>
> Color me slow and stupid. Maybe due to reviewing a patch too early in
> the morning, who knows?
>
> There is nothing above that prevents the compiler and the CPU from
> reordering the assignments to X and Y with the increment of s->sequence++.

Yes, but this doesn't matter, I think. The writer does

Y = true;
1st_increment;

wmb();

2nd_increment;
X = false;

and we do not care about reordering before or after wmnb() at all. But we
rely on the fact that 1st_increment can not be reordered with "X = false",
and that "Y = true" can not be reordered with the 2nd_increment.

And another simple "proof" is that seqcount_barrier() is equivalent to
write_seqcount_begin() + + write_seqcount_end() and thus the code above
is correct, or the ACQUIRE/RELEASE semantics of seqcount_t is broken ;)

Oleg.