seqlock: Use while instead of if+goto in __read_seqcount_begin
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
@@ -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
Will Deacon
maintainerQuoted 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.
Signed-off-by Muchun Song <[email protected]>--- include/linux/seqlock.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)include/linux/seqlock.h +0
-0@@ -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
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.
Signed-off-by Muchun Song <[email protected]>--- include/linux/seqlock.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)include/linux/seqlock.h +0
-0@@ -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
Peter Zijlstra
maintainerQuoted 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.
Signed-off-by Muchun Song <[email protected]>--- include/linux/seqlock.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)include/linux/seqlock.h +0
-0@@ -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 ?
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.
Signed-off-by Muchun Song <[email protected]>--- include/linux/seqlock.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)include/linux/seqlock.h +0
-0@@ -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
Peter Zijlstra
maintainerQuoted 7 lines · Muchun Song
Quoted 2 lines · Peter Zijlstra
.... That said,
Will, would it make sense to usesmp_cond_load_relaxed()here ?I have a similar idea. Would it make sense to use
smp_cond_load_acquire()inraw_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.
Will Deacon
maintainerQuoted 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.
Signed-off-by Muchun Song <[email protected]>--- include/linux/seqlock.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)include/linux/seqlock.h +0
-0@@ -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
Peter Zijlstra
maintainerQuoted 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.
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.
Signed-off-by Muchun Song <[email protected]>--- include/linux/seqlock.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)include/linux/seqlock.h +0
-0@@ -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)