All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin
@ 2020-04-09 13:45 Muchun Song
  2020-04-10 11:56 ` Will Deacon
  0 siblings, 1 reply; 9+ messages in thread
From: Muchun Song @ 2020-04-09 13:45 UTC (permalink / raw)
  To: peterz, mingo, will, mingo; +Cc: linux-kernel, Muchun Song

The creators of the C language gave us the while keyword. Let's use
that instead of synthesizing it from if+goto.

Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
 include/linux/seqlock.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
index 8b97204f35a77..7bdea019814ce 100644
--- a/include/linux/seqlock.h
+++ b/include/linux/seqlock.h
@@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
 {
 	unsigned ret;
 
-repeat:
-	ret = READ_ONCE(s->sequence);
-	if (unlikely(ret & 1)) {
+	while (unlikely((ret = READ_ONCE(s->sequence)) & 1))
 		cpu_relax();
-		goto repeat;
-	}
 	kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
 	return ret;
 }
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin
  2020-04-09 13:45 [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin Muchun Song
@ 2020-04-10 11:56 ` Will Deacon
  2020-04-14  8:58   ` [External] " Muchun Song
  2020-04-14 11:05   ` Peter Zijlstra
  0 siblings, 2 replies; 9+ messages in thread
From: Will Deacon @ 2020-04-10 11:56 UTC (permalink / raw)
  To: Muchun Song; +Cc: peterz, mingo, mingo, linux-kernel

On Thu, Apr 09, 2020 at 09:45:58PM +0800, Muchun Song wrote:
> The creators of the C language gave us the while keyword. Let's use
> that instead of synthesizing it from if+goto.
> 
> Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> ---
>  include/linux/seqlock.h | 6 +-----
>  1 file changed, 1 insertion(+), 5 deletions(-)
> 
> diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
> index 8b97204f35a77..7bdea019814ce 100644
> --- a/include/linux/seqlock.h
> +++ b/include/linux/seqlock.h
> @@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
>  {
>  	unsigned ret;
>  
> -repeat:
> -	ret = READ_ONCE(s->sequence);
> -	if (unlikely(ret & 1)) {
> +	while (unlikely((ret = READ_ONCE(s->sequence)) & 1))
>  		cpu_relax();
> -		goto repeat;
> -	}
>  	kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
>  	return ret;

Patch looks fine to me, but I'll leave it to Peter as I don't have a
preference either way.

Will

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [External] Re: [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin
  2020-04-10 11:56 ` Will Deacon
@ 2020-04-14  8:58   ` Muchun Song
  2020-04-14 11:05   ` Peter Zijlstra
  1 sibling, 0 replies; 9+ messages in thread
From: Muchun Song @ 2020-04-14  8:58 UTC (permalink / raw)
  To: peterz; +Cc: mingo, Will Deacon, mingo, linux-kernel

On Fri, Apr 10, 2020 at 7:57 PM Will Deacon <will@kernel.org> wrote:
>
> On Thu, Apr 09, 2020 at 09:45:58PM +0800, Muchun Song wrote:
> > The creators of the C language gave us the while keyword. Let's use
> > that instead of synthesizing it from if+goto.
> >
> > Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> > ---
> >  include/linux/seqlock.h | 6 +-----
> >  1 file changed, 1 insertion(+), 5 deletions(-)
> >
> > diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
> > index 8b97204f35a77..7bdea019814ce 100644
> > --- a/include/linux/seqlock.h
> > +++ b/include/linux/seqlock.h
> > @@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
> >  {
> >       unsigned ret;
> >
> > -repeat:
> > -     ret = READ_ONCE(s->sequence);
> > -     if (unlikely(ret & 1)) {
> > +     while (unlikely((ret = READ_ONCE(s->sequence)) & 1))
> >               cpu_relax();
> > -             goto repeat;
> > -     }
> >       kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
> >       return ret;
>
> Patch looks fine to me, but I'll leave it to Peter as I don't have a
> preference either way.
>

This patch can make the code look simple and easy to read.
What is your opinion, Peter? Thanks.


-- 
Yours,
Muchun

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin
  2020-04-10 11:56 ` Will Deacon
  2020-04-14  8:58   ` [External] " Muchun Song
@ 2020-04-14 11:05   ` Peter Zijlstra
  2020-04-14 12:01     ` [External] " Muchun Song
                       ` (2 more replies)
  1 sibling, 3 replies; 9+ messages in thread
From: Peter Zijlstra @ 2020-04-14 11:05 UTC (permalink / raw)
  To: Will Deacon; +Cc: Muchun Song, mingo, mingo, linux-kernel

On Fri, Apr 10, 2020 at 12:56:58PM +0100, Will Deacon wrote:
> On Thu, Apr 09, 2020 at 09:45:58PM +0800, Muchun Song wrote:
> > The creators of the C language gave us the while keyword. Let's use
> > that instead of synthesizing it from if+goto.
> > 
> > Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> > ---
> >  include/linux/seqlock.h | 6 +-----
> >  1 file changed, 1 insertion(+), 5 deletions(-)
> > 
> > diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
> > index 8b97204f35a77..7bdea019814ce 100644
> > --- a/include/linux/seqlock.h
> > +++ b/include/linux/seqlock.h
> > @@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
> >  {
> >  	unsigned ret;
> >  
> > -repeat:
> > -	ret = READ_ONCE(s->sequence);
> > -	if (unlikely(ret & 1)) {
> > +	while (unlikely((ret = READ_ONCE(s->sequence)) & 1))
> >  		cpu_relax();
> > -		goto repeat;
> > -	}
> >  	kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
> >  	return ret;
> 
> Patch looks fine to me, but I'll leave it to Peter as I don't have a
> preference either way.

Linus sometimes prefers the goto variant as that better expresses the
exception model. But like Will, I don't particularly care. That said,
Will, would it make sense to use smp_cond_load_relaxed() here ?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [External] Re: [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin
  2020-04-14 11:05   ` Peter Zijlstra
@ 2020-04-14 12:01     ` Muchun Song
  2020-04-15 11:41       ` Peter Zijlstra
  2020-04-14 13:48     ` Will Deacon
  2020-04-15  9:37     ` David Laight
  2 siblings, 1 reply; 9+ messages in thread
From: Muchun Song @ 2020-04-14 12:01 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: Will Deacon, mingo, mingo, linux-kernel

On Tue, Apr 14, 2020 at 7:05 PM Peter Zijlstra <peterz@infradead.org> wrote:
>
> On Fri, Apr 10, 2020 at 12:56:58PM +0100, Will Deacon wrote:
> > On Thu, Apr 09, 2020 at 09:45:58PM +0800, Muchun Song wrote:
> > > The creators of the C language gave us the while keyword. Let's use
> > > that instead of synthesizing it from if+goto.
> > >
> > > Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> > > ---
> > >  include/linux/seqlock.h | 6 +-----
> > >  1 file changed, 1 insertion(+), 5 deletions(-)
> > >
> > > diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
> > > index 8b97204f35a77..7bdea019814ce 100644
> > > --- a/include/linux/seqlock.h
> > > +++ b/include/linux/seqlock.h
> > > @@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
> > >  {
> > >     unsigned ret;
> > >
> > > -repeat:
> > > -   ret = READ_ONCE(s->sequence);
> > > -   if (unlikely(ret & 1)) {
> > > +   while (unlikely((ret = READ_ONCE(s->sequence)) & 1))
> > >             cpu_relax();
> > > -           goto repeat;
> > > -   }
> > >     kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
> > >     return ret;
> >
> > Patch looks fine to me, but I'll leave it to Peter as I don't have a
> > preference either way.
>
> Linus sometimes prefers the goto variant as that better expresses the
> exception model. But like Will, I don't particularly care. That said,
> Will, would it make sense to use smp_cond_load_relaxed() here ?

I have a similar idea. Would it make sense to use smp_cond_load_acquire()
in raw_read_seqcount_begin()?

-- 
Yours,
Muchun

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin
  2020-04-14 11:05   ` Peter Zijlstra
  2020-04-14 12:01     ` [External] " Muchun Song
@ 2020-04-14 13:48     ` Will Deacon
  2020-04-15 11:44       ` Peter Zijlstra
  2020-04-15  9:37     ` David Laight
  2 siblings, 1 reply; 9+ messages in thread
From: Will Deacon @ 2020-04-14 13:48 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: Muchun Song, mingo, mingo, linux-kernel

On Tue, Apr 14, 2020 at 01:05:16PM +0200, Peter Zijlstra wrote:
> On Fri, Apr 10, 2020 at 12:56:58PM +0100, Will Deacon wrote:
> > On Thu, Apr 09, 2020 at 09:45:58PM +0800, Muchun Song wrote:
> > > The creators of the C language gave us the while keyword. Let's use
> > > that instead of synthesizing it from if+goto.
> > > 
> > > Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> > > ---
> > >  include/linux/seqlock.h | 6 +-----
> > >  1 file changed, 1 insertion(+), 5 deletions(-)
> > > 
> > > diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
> > > index 8b97204f35a77..7bdea019814ce 100644
> > > --- a/include/linux/seqlock.h
> > > +++ b/include/linux/seqlock.h
> > > @@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
> > >  {
> > >  	unsigned ret;
> > >  
> > > -repeat:
> > > -	ret = READ_ONCE(s->sequence);
> > > -	if (unlikely(ret & 1)) {
> > > +	while (unlikely((ret = READ_ONCE(s->sequence)) & 1))
> > >  		cpu_relax();
> > > -		goto repeat;
> > > -	}
> > >  	kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
> > >  	return ret;
> > 
> > Patch looks fine to me, but I'll leave it to Peter as I don't have a
> > preference either way.
> 
> Linus sometimes prefers the goto variant as that better expresses the
> exception model. But like Will, I don't particularly care. That said,
> Will, would it make sense to use smp_cond_load_relaxed() here ?

Oh yeah, good thinking. Didn't spot that one, but it should work well as
long as smp_cond_load_relaxed() always implies a control dependency (surely
it has to?)

Will

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin
  2020-04-14 11:05   ` Peter Zijlstra
  2020-04-14 12:01     ` [External] " Muchun Song
  2020-04-14 13:48     ` Will Deacon
@ 2020-04-15  9:37     ` David Laight
  2 siblings, 0 replies; 9+ messages in thread
From: David Laight @ 2020-04-15  9:37 UTC (permalink / raw)
  To: 'Peter Zijlstra', Will Deacon
  Cc: Muchun Song, mingo, mingo, linux-kernel

From: Peter Zijlstra
> Sent: 14 April 2020 12:05
> On Fri, Apr 10, 2020 at 12:56:58PM +0100, Will Deacon wrote:
> > On Thu, Apr 09, 2020 at 09:45:58PM +0800, Muchun Song wrote:
> > > The creators of the C language gave us the while keyword. Let's use
> > > that instead of synthesizing it from if+goto.
> > >
> > > Signed-off-by: Muchun Song <songmuchun@bytedance.com>
> > > ---
> > >  include/linux/seqlock.h | 6 +-----
> > >  1 file changed, 1 insertion(+), 5 deletions(-)
> > >
> > > diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h
> > > index 8b97204f35a77..7bdea019814ce 100644
> > > --- a/include/linux/seqlock.h
> > > +++ b/include/linux/seqlock.h
> > > @@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
> > >  {
> > >  	unsigned ret;
> > >
> > > -repeat:
> > > -	ret = READ_ONCE(s->sequence);
> > > -	if (unlikely(ret & 1)) {
> > > +	while (unlikely((ret = READ_ONCE(s->sequence)) & 1))
> > >  		cpu_relax();
> > > -		goto repeat;
> > > -	}
> > >  	kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX);
> > >  	return ret;
> >
> > Patch looks fine to me, but I'll leave it to Peter as I don't have a
> > preference either way.
> 
> Linus sometimes prefers the goto variant as that better expresses the
> exception model. But like Will, I don't particularly care. That said,
> Will, would it make sense to use smp_cond_load_relaxed() here ?

gcc also has a nasty habit of converting:
	while (foo)
		bar;
into:
	if (foo) {
		do
			bar;
		while (foo);
	}
with all the code bloat that entails - especially when 'foo'
is non-trivial.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [External] Re: [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin
  2020-04-14 12:01     ` [External] " Muchun Song
@ 2020-04-15 11:41       ` Peter Zijlstra
  0 siblings, 0 replies; 9+ messages in thread
From: Peter Zijlstra @ 2020-04-15 11:41 UTC (permalink / raw)
  To: Muchun Song; +Cc: Will Deacon, mingo, mingo, linux-kernel

On Tue, Apr 14, 2020 at 08:01:06PM +0800, Muchun Song wrote:
> On Tue, Apr 14, 2020 at 7:05 PM Peter Zijlstra <peterz@infradead.org> wrote:

> > .... That said,
> > Will, would it make sense to use smp_cond_load_relaxed() here ?
> 
> I have a similar idea. Would it make sense to use smp_cond_load_acquire()
> in raw_read_seqcount_begin()?

Not sure; I did consider it, but that rmb it has seems more natural in
the over-all ordering scheme here. I mean:

	load seqcount		inc seqcount
	rmb			wmb
	// load stuff		// modify stuff
	rmb			wmb
	compare seqcount	inc seqcount

is nice and symmetric, making that upper left rmb an acquire 'works' but
is just weird IMO. And I suppose you can make the lower right wmb a
store-release, which is somewhat better, but then it gets all weird when
you consider things like barrier and latch.

So best to just leave it as is I think.

Those incs do seem to be really wanting a WRITE_ONCE() though.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin
  2020-04-14 13:48     ` Will Deacon
@ 2020-04-15 11:44       ` Peter Zijlstra
  0 siblings, 0 replies; 9+ messages in thread
From: Peter Zijlstra @ 2020-04-15 11:44 UTC (permalink / raw)
  To: Will Deacon; +Cc: Muchun Song, mingo, mingo, linux-kernel

On Tue, Apr 14, 2020 at 02:48:31PM +0100, Will Deacon wrote:
> Oh yeah, good thinking. Didn't spot that one, but it should work well as
> long as smp_cond_load_relaxed() always implies a control dependency (surely
> it has to?)

Well, yeah, but then you know as well as I do that compilers are dodgy
pieces of crap which are out to get you. Still, I too can't see how it
could mess this up.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-04-15 12:29 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-09 13:45 [PATCH] seqlock: Use while instead of if+goto in __read_seqcount_begin Muchun Song
2020-04-10 11:56 ` Will Deacon
2020-04-14  8:58   ` [External] " Muchun Song
2020-04-14 11:05   ` Peter Zijlstra
2020-04-14 12:01     ` [External] " Muchun Song
2020-04-15 11:41       ` Peter Zijlstra
2020-04-14 13:48     ` Will Deacon
2020-04-15 11:44       ` Peter Zijlstra
2020-04-15  9:37     ` David Laight

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.