mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Waiman Long <llong@redhat.com>
To: paulmck@kernel.org, Waiman Long <llong@redhat.com>
Cc: Daniel Xu <dxu@dxuuu.xyz>,
	mingo@redhat.com, will@kernel.org, peterz@infradead.org,
	boqun.feng@gmail.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] seqlock: Use WRITE_ONCE() when updating sequence
Date: Wed, 18 Dec 2024 11:10:01 -0500	[thread overview]
Message-ID: <8da9c55b-65fb-4c81-b6ea-f80892e3cff7@redhat.com> (raw)
In-Reply-To: <21914c89-4f4a-4e09-97de-4d5b99a48243@paulmck-laptop>

On 12/18/24 10:45 AM, Paul E. McKenney wrote:
> On Tue, Dec 17, 2024 at 10:30:39PM -0500, Waiman Long wrote:
>> On 12/17/24 6:17 PM, Daniel Xu wrote:
>>> `sequence` is a concurrently accessed shared variable on the reader
>>> side. Therefore, it needs to be wrapped in WRITE_ONCE() in order to
>>> prevent unwanted compiler optimizations like store tearing.
>>>
>>> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
>>> ---
>>>    include/linux/seqlock.h | 14 +++++++-------
>>>    1 file changed, 7 insertions(+), 7 deletions(-)
>>>
>>> diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
>>> index 5298765d6ca4..f4c6f2507742 100644
>>> --- a/include/linux/seqlock.h
>>> +++ b/include/linux/seqlock.h
>>> @@ -45,7 +45,7 @@ static inline void __seqcount_init(seqcount_t *s, const char *name,
>>>    	 * Make sure we are not reinitializing a held lock:
>>>    	 */
>>>    	lockdep_init_map(&s->dep_map, name, key, 0);
>>> -	s->sequence = 0;
>>> +	WRITE_ONCE(s->sequence, 0);
>>>    }
>> The init function certainly doesn't need to use WRITE_ONCE().
>>>    #ifdef CONFIG_DEBUG_LOCK_ALLOC
>>> @@ -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();
>>>    }
>>> @@ -426,7 +426,7 @@ do {									\
>>>    static inline void do_raw_write_seqcount_end(seqcount_t *s)
>>>    {
>>>    	smp_wmb();
>>> -	s->sequence++;
>>> +	WRITE_ONCE(s->sequence, READ_ONCE(s->sequence) + 1);
>>>    	kcsan_nestable_atomic_end();
>>>    }
>>> @@ -548,9 +548,9 @@ static inline void do_write_seqcount_end(seqcount_t *s)
>>>    static inline void do_raw_write_seqcount_barrier(seqcount_t *s)
>>>    {
>>>    	kcsan_nestable_atomic_begin();
>>> -	s->sequence++;
>>> +	WRITE_ONCE(s->sequence, READ_ONCE(s->sequence) + 1);
>>>    	smp_wmb();
>>> -	s->sequence++;
>>> +	WRITE_ONCE(s->sequence, READ_ONCE(s->sequence) + 1);
>>>    	kcsan_nestable_atomic_end();
>>>    }
>>> @@ -569,7 +569,7 @@ static inline void do_write_seqcount_invalidate(seqcount_t *s)
>>>    {
>>>    	smp_wmb();
>>>    	kcsan_nestable_atomic_begin();
>>> -	s->sequence+=2;
>>> +	WRITE_ONCE(s->sequence, READ_ONCE(s->sequence) + 2);
>>>    	kcsan_nestable_atomic_end();
>>>    }
>>> @@ -673,7 +673,7 @@ read_seqcount_latch_retry(const seqcount_latch_t *s, unsigned start)
>>>    static __always_inline void raw_write_seqcount_latch(seqcount_latch_t *s)
>>>    {
>>>    	smp_wmb();	/* prior stores before incrementing "sequence" */
>>> -	s->seqcount.sequence++;
>>> +	WRITE_ONCE(s->seqcount.sequence, READ_ONCE(s->seqcount.sequence) + 1);
>>>    	smp_wmb();      /* increment "sequence" before following stores */
>>>    }
>> For seqcount, its actual value isn't important. What is important is whether
>> the value changes and whether it is even or odd. So even if store tearing is
>> happening, it shouldn't affect its operation. I doubt we need to use
>> WRITE_ONCE() here. Could you come up with a scenario where store tearing
>> will make it behave incorrectly?
> But why expand the state space?
I don't quite get what you mean by "state space".
>
> Also, there are potentially "optimizations" other than store tearing.
> No, I haven't seen them yet, but then again, there were a great many
> optimizations that were not being used back when I started coding C.

All the inc's are bounded by smp_wmb()'s which should limit the kind of 
optimizations that can be done by the compiler. That is why I don't see 
a need to use WRITE_ONCE() here. Of course, there may be some corner 
cases that I may miss. So I ask for whether such a case exists.

Cheers,
Longman


  reply	other threads:[~2024-12-18 16:10 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-17 23:17 Daniel Xu
2024-12-18  3:30 ` Waiman Long
2024-12-18 15:45   ` Paul E. McKenney
2024-12-18 16:10     ` Waiman Long [this message]
2024-12-18 16:57       ` Paul E. McKenney
2024-12-18 18:38         ` Waiman Long
2024-12-18 18:52           ` Paul E. McKenney
2024-12-18 20:09             ` Waiman Long
2024-12-18 10:30 ` Peter Zijlstra
2024-12-18 15:43   ` Paul E. McKenney
2024-12-18 16:23     ` Peter Zijlstra
2024-12-18 16:29       ` Peter Zijlstra
2024-12-18 16:59         ` Paul E. McKenney
2024-12-18 17:12           ` Peter Zijlstra
2024-12-18 19:15             ` Paul E. McKenney
2024-12-18 19:56             ` Florian Weimer
2024-12-19 16:10               ` Paul E. McKenney
2024-12-19 16:45                 ` Florian Weimer
2024-12-19 17:48                   ` Paul E. McKenney
2024-12-19 17:53                   ` Peter Zijlstra
2024-12-19 16:34             ` Will Deacon
2024-12-19 17:53               ` Paul E. McKenney
2024-12-19 17:58                 ` Will Deacon
2024-12-19 18:06                   ` Paul E. McKenney
2024-12-19 18:31                     ` Will Deacon
2024-12-20 17:40                       ` Paul E. McKenney
2025-01-25  0:31 ` Daniel Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8da9c55b-65fb-4c81-b6ea-f80892e3cff7@redhat.com \
    --to=llong@redhat.com \
    --cc=boqun.feng@gmail.com \
    --cc=dxu@dxuuu.xyz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®