Re: [PATCH] seqlock: Use WRITE_ONCE() when updating sequence

From: Peter Zijlstra
Date: Wed Dec 18 2024 - 11:30:05 EST


On Wed, Dec 18, 2024 at 05:23:25PM +0100, Peter Zijlstra wrote:
> On Wed, Dec 18, 2024 at 07:43:41AM -0800, Paul E. McKenney wrote:
> > On Wed, Dec 18, 2024 at 11:30:00AM +0100, Peter Zijlstra wrote:
> > > On Tue, Dec 17, 2024 at 03:17:36PM -0800, Daniel Xu wrote:
>
>
> > > > @@ -405,7 +405,7 @@ do { \
> > > > static inline void do_raw_write_seqcount_begin(seqcount_t *s)
> > > > {
> > > > kcsan_nestable_atomic_begin();
> > > > - s->sequence++;
> > > > + WRITE_ONCE(s->sequence, READ_ONCE(s->sequence) + 1);
> > > > smp_wmb();
> > > > }
> > >
> > > This results in significantly worse code-gen, it will change an inc to
> > > memory with a load,inc,store.
> >
> > Isn't that code-generation bug in the process of being fixed?
>
> Last time I looked the compiler wasn't allowed to touch it because of
> all the volatile going around. Did anything change?
>
> > And, either way, given the likely cache miss, should we really care?
>
> Yeah, extra register pressure too.

Perhaps something like: (*(volatile unsigned int *)&s->sequence)++; ?
I'd have to check what the compiler makes of that.

/me mucks about with godbolt for a bit...

GCC doesn't optimize that, but Clang does.

I would still very much refrain from making this change until both
compilers can generate sane code for it.