Skip to contentSkip to conversation

seqlock: Use while instead of if+goto in __read_seqcount_begin

PATCH In review Muchun Song 4 participants 9 messages Apr 9, 2020 – Apr 15, 2020 Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 seqlock: Use while instead of if+goto in __read_seqcount_begin

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

---
 include/linux/seqlock.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/seqlock.h +1 -5
index 8b97204f35a77..7bdea019814ce 100644
@@ -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;
 }
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 15 lines · Muchun Song

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

---
 include/linux/seqlock.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/seqlock.h +0 -0
index 8b97204f35a77..7bdea019814ce 100644
@@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
 {
 	unsigned ret;

include/linux/seqlock.h static inline unsigned __read_seqcount_begin(const seqcount_t *s) · line 131

Quoted 10 lines
include/linux/seqlock.h +1 -5
 
-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

#2 lore raw
Quoted 18 lines · Will Deacon
Quoted 16 lines · Muchun Song

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

---
 include/linux/seqlock.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/seqlock.h +0 -0
index 8b97204f35a77..7bdea019814ce 100644
@@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
 {
      unsigned ret;
 
Quoted 13 lines

include/linux/seqlock.h static inline unsigned __read_seqcount_begin(const seqcount_t *s) · line 131

Quoted 9 lines
include/linux/seqlock.h +1 -5
-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.

Signature · 2 lines
Yours,
Muchun
maintainer
#3 lore raw
In reply to Will Deacon
Quoted 17 lines · Will Deacon
Quoted 16 lines · Muchun Song

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

---
 include/linux/seqlock.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/seqlock.h +0 -0
index 8b97204f35a77..7bdea019814ce 100644
@@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
 {
 	unsigned ret;
 
Quoted 12 lines

include/linux/seqlock.h static inline unsigned __read_seqcount_begin(const seqcount_t *s) · line 131

Quoted 9 lines
include/linux/seqlock.h +1 -5
-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 ?

#4 lore raw
Quoted 32 lines · Peter Zijlstra
Quoted 17 lines · Will Deacon
Quoted 16 lines · Muchun Song

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

---
 include/linux/seqlock.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/seqlock.h +0 -0
index 8b97204f35a77..7bdea019814ce 100644
@@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
 {
    unsigned ret;
 
Quoted 12 lines

include/linux/seqlock.h static inline unsigned __read_seqcount_begin(const seqcount_t *s) · line 131

Quoted 9 lines
include/linux/seqlock.h +1 -5
-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.

Quoted 3 lines

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()?

Signature · 2 lines
Yours,
Muchun
maintainer
#5 lore raw
Quoted 7 lines · Muchun Song
Quoted 2 lines · Peter Zijlstra

.... 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.

maintainer
#6 lore raw
In reply to Peter Zijlstra
Quoted 31 lines · Peter Zijlstra
Quoted 17 lines · Will Deacon
Quoted 16 lines · Muchun Song

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

---
 include/linux/seqlock.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/seqlock.h +0 -0
index 8b97204f35a77..7bdea019814ce 100644
@@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
 {
 	unsigned ret;
 
Quoted 12 lines

include/linux/seqlock.h static inline unsigned __read_seqcount_begin(const seqcount_t *s) · line 131

Quoted 9 lines
include/linux/seqlock.h +1 -5
-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.

Quoted 3 lines

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

maintainer
#7 lore raw
Quoted 3 lines · Will Deacon

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.

#8 lore raw
In reply to Peter Zijlstra

From: Peter Zijlstra

Quoted 32 lines

Sent: 14 April 2020 12:05

Quoted 17 lines · Will Deacon
Quoted 16 lines · Muchun Song

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

---
 include/linux/seqlock.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/seqlock.h +0 -0
index 8b97204f35a77..7bdea019814ce 100644
@@ -125,12 +125,8 @@ static inline unsigned __read_seqcount_begin(const seqcount_t *s)
 {
 	unsigned ret;
 
Quoted 12 lines

include/linux/seqlock.h static inline unsigned __read_seqcount_begin(const seqcount_t *s) · line 131

Quoted 9 lines
include/linux/seqlock.h +1 -5
-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.

Quoted 3 lines

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)