toyrcu: replace ACCESS_ONCE() and fix typos
cover toyrcu: replace ACCESS_ONCE() and fix typos
Hi Paul and list,
I'm reading Chapter Toy RCU, and the following are some corrections. Most of them focus on replacing ACCESS_ONCE() with READ_ONCE() and WRITE_ONCE(). Please let me know if they look OK.
Thanks,
--Junchang
--
Junchang Wang (4):
Replace the plain accesses and the use of ACCESS_ONCE() with
READ_ONCE()/WRITE_ONCE().
ACCESS_ONCE() with READ_ONCE()/WRITE_ONCE()
Replace the plain accesses and the use of ACCESS_ONCE() with
READ_ONCE()/WRITE_ONCE().toyrcu: fix typo.
CodeSamples/defer/rcu.c | 5 +++-- CodeSamples/defer/rcu.h | 5 +++-- CodeSamples/defer/rcu_nest.c | 5 +++-- CodeSamples/defer/rcu_nest.h | 8 +------- CodeSamples/defer/rcu_qs.c | 5 +++-- CodeSamples/defer/rcu_qs.h | 4 ++-- appendix/toyrcu/toyrcu.tex | 28 ++++++++++++++-------------- 7 files changed, 29 insertions(+), 31 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/4 rcu: Use READ_ONCE() and WRITE_ONCE() for shared variable rcu_reader_gp
Replace the plain accesses and the use of ACCESS_ONCE() with READ_ONCE()/WRITE_ONCE(). And then update the tex file accordingly.
--- CodeSamples/defer/rcu.c | 5 +++-- CodeSamples/defer/rcu.h | 5 +++-- appendix/toyrcu/toyrcu.tex | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-)
CodeSamples/defer/rcu.c +3 -2
@@ -35,7 +35,7 @@ void synchronize_rcu(void)/* Advance to a new grace-period number, enforce ordering. */ - rcu_gp_ctr +=2 ; +WRITE_ONCE (rcu_gp_ctr,READ_ONCE (rcu_gp_ctr) +2 );smp_mb ();/*
@@ -45,7 +45,8 @@ void synchronize_rcu(void)for_each_thread (t) {while ((per_thread (rcu_reader_gp, t) &0x1 ) && - ((per_thread (rcu_reader_gp, t) - rcu_gp_ctr) <0 )) { + ((per_thread (rcu_reader_gp, t) - +READ_ONCE (rcu_gp_ctr)) <0 )) {/*@@@ poll(NULL, 0, 10); */ barrier (); }
CodeSamples/defer/rcu.h +3 -2
@@ -41,7 +41,8 @@ static inline void rcu_read_lock(void)* periodic per-thread processing.) */ -__get_thread_var (rcu_reader_gp) = rcu_gp_ctr +1 ; +__get_thread_var (rcu_reader_gp) = +READ_ONCE (rcu_gp_ctr) +1 ;smp_mb (); }
@@ -55,7 +56,7 @@ static inline void rcu_read_unlock(void)*/ smp_mb (); -__get_thread_var (rcu_reader_gp) = rcu_gp_ctr; +__get_thread_var (rcu_reader_gp) =READ_ONCE (rcu_gp_ctr); }extern void synchronize_rcu (void );
appendix/toyrcu/toyrcu.tex +4 -4
@@ -1223,7 +1223,7 @@ thread-local accesses to one, as is done in the next section. 1 static void rcu_read_lock(void) 2 { 3 __get_thread_var(rcu_reader_gp) = - 4 ACCESS_ONCE(rcu_gp_ctr) + 1; + 4 READ_ONCE(rcu_gp_ctr) + 1; 5 smp_mb(); 6 } 7
@@ -1231,7 +1231,7 @@ thread-local accesses to one, as is done in the next section. 9 { 10 smp_mb(); 11 __get_thread_var(rcu_reader_gp) = -12 ACCESS_ONCE(rcu_gp_ctr); +12 READ_ONCE(rcu_gp_ctr); 13 } 14 15 void synchronize_rcu(void)
@@ -1240,12 +1240,12 @@ thread-local accesses to one, as is done in the next section. 18 19 smp_mb(); 20 spin_lock(&rcu_gp_lock); -21 ACCESS_ONCE(rcu_gp_ctr) += 2; +21 WRITE_ONCE(rcu_gp_ctr, READ_ONCE(rcu_gp_ctr) + 2); 22 smp_mb(); 23 for_each_thread(t) { 24 while ((per_thread(rcu_reader_gp, t) & 0x1) && 25 ((per_thread(rcu_reader_gp, t) - -26 ACCESS_ONCE(rcu_gp_ctr)) < 0)) { +26 READ_ONCE(rcu_gp_ctr)) < 0)) { 27 poll(NULL, 0, 10); 28 } 29 }
Signature · 1 line
2.7.4
Re: [PATCH 1/4] rcu: Use READ_ONCE() and WRITE_ONCE() for shared variable rcu_reader_gp
Hi Junchang,
Quoted 15 lines · Junchang Wang
Replace the plain accesses and the use of
ACCESS_ONCE()with READ_ONCE()/WRITE_ONCE(). And then update the tex file accordingly.Signed-off-by Junchang Wang <[email protected]>--- CodeSamples/defer/rcu.c | 5 +++-- CodeSamples/defer/rcu.h | 5 +++-- appendix/toyrcu/toyrcu.tex | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-)CodeSamples/defer/rcu.c +0
-0
CodeSamples/defer/rcu.c void synchronize_rcu(void) · line 38
Quoted 5 lines
CodeSamples/defer/rcu.c +1
-1@@ -35,7 +35,7 @@ void synchronize_rcu(void)/* Advance to a new grace-period number, enforce ordering. */ - rcu_gp_ctr +=2 ; +WRITE_ONCE (rcu_gp_ctr,READ_ONCE (rcu_gp_ctr) +2 );
This is inside of critical section protecting update of rcu_gp_ctr, and READ_ONCE() is not necessary. On the other hand, WRITE_ONCE() is necessary because other CPUs/threads can do racy read accesses.
CodeSamples/defer/rcu.c void synchronize_rcu(void) · line 49
Quoted 10 lines
CodeSamples/defer/rcu.c +2
-1smp_mb ();/* @@ -45,7 +45,8 @@ void synchronize_rcu(void)for_each_thread (t) {while ((per_thread (rcu_reader_gp, t) &0x1 ) && - ((per_thread (rcu_reader_gp, t) - rcu_gp_ctr) <0 )) { + ((per_thread (rcu_reader_gp, t) - +READ_ONCE (rcu_gp_ctr)) <0 )) {
This READ_ONCE() is not necessary, neither.
Please refer to Section 4.3.4.4 "Avoiding Data Races" for where to use READ/WRITE_ONCE(). You might want to self review 2/4 and 3/4 of this patch set.
appendix/toyrcu/toyrcu.tex
Quoted 30 lines
CodeSamples/defer/rcu.c +0
-0/*@@@ poll(NULL, 0, 10); */ barrier (); }CodeSamples/defer/rcu.h +3
-2@@ -41,7 +41,8 @@ static inline void rcu_read_lock(void)* periodic per-thread processing.) */ -__get_thread_var (rcu_reader_gp) = rcu_gp_ctr +1 ; +__get_thread_var (rcu_reader_gp) = +READ_ONCE (rcu_gp_ctr) +1 ;smp_mb (); }@@ -55,7 +56,7 @@ static inline void rcu_read_unlock(void)*/ smp_mb (); -__get_thread_var (rcu_reader_gp) = rcu_gp_ctr; +__get_thread_var (rcu_reader_gp) =READ_ONCE (rcu_gp_ctr); }extern void synchronize_rcu (void );appendix/toyrcu/toyrcu.tex +0
-0
Or you can extract the snippet by using \begin{snippet} meta commands. That is a preferred way if there is code under CodeSamples/. Examples of the scheme can be found in (not yet up-to-date) Style Guide (D.3.1.1).
That said, you are not obliged to do so. Conversion to the new scheme is on my to-do list anyway.
Thanks, Akira
Quoted 34 lines
appendix/toyrcu/toyrcu.tex +4
-4@@ -1223,7 +1223,7 @@ thread-local accesses to one, as is done in the next section. 1 static void rcu_read_lock(void) 2 { 3 __get_thread_var(rcu_reader_gp) = - 4 ACCESS_ONCE(rcu_gp_ctr) + 1; + 4 READ_ONCE(rcu_gp_ctr) + 1; 5 smp_mb(); 6 } 7@@ -1231,7 +1231,7 @@ thread-local accesses to one, as is done in the next section. 9 { 10 smp_mb(); 11 __get_thread_var(rcu_reader_gp) = -12 ACCESS_ONCE(rcu_gp_ctr); +12 READ_ONCE(rcu_gp_ctr); 13 } 14 15 void synchronize_rcu(void)@@ -1240,12 +1240,12 @@ thread-local accesses to one, as is done in the next section. 18 19 smp_mb(); 20 spin_lock(&rcu_gp_lock); -21 ACCESS_ONCE(rcu_gp_ctr) += 2; +21 WRITE_ONCE(rcu_gp_ctr, READ_ONCE(rcu_gp_ctr) + 2); 22 smp_mb(); 23 for_each_thread(t) { 24 while ((per_thread(rcu_reader_gp, t) & 0x1) && 25 ((per_thread(rcu_reader_gp, t) - -26 ACCESS_ONCE(rcu_gp_ctr)) < 0)) { +26 READ_ONCE(rcu_gp_ctr)) < 0)) { 27 poll(NULL, 0, 10); 28 } 29 }
Re: [PATCH 1/4] rcu: Use READ_ONCE() and WRITE_ONCE() for shared variable rcu_reader_gp
Quoted 42 lines · Akira Yokosawa
Hi Junchang,
Quoted 15 lines · Junchang Wang
Replace the plain accesses and the use of
ACCESS_ONCE()with READ_ONCE()/WRITE_ONCE(). And then update the tex file accordingly.Signed-off-by Junchang Wang <[email protected]>--- CodeSamples/defer/rcu.c | 5 +++-- CodeSamples/defer/rcu.h | 5 +++-- appendix/toyrcu/toyrcu.tex | 8 ++++---- 3 files changed, 10 insertions(+), 8 deletions(-)CodeSamples/defer/rcu.c +0
-0CodeSamples/defer/rcu.c void synchronize_rcu(void) · line 38
Quoted 5 lines
CodeSamples/defer/rcu.c +1
-1@@ -35,7 +35,7 @@ void synchronize_rcu(void)/* Advance to a new grace-period number, enforce ordering. */ - rcu_gp_ctr +=2 ; +WRITE_ONCE (rcu_gp_ctr,READ_ONCE (rcu_gp_ctr) +2 );This is inside of critical section protecting update of
rcu_gp_ctr, andREAD_ONCE()is not necessary. On the other hand,WRITE_ONCE()is necessary because other CPUs/threads can do racy read accesses.CodeSamples/defer/rcu.c void synchronize_rcu(void) · line 49
Quoted 10 lines
CodeSamples/defer/rcu.c +2
-1smp_mb ();/* @@ -45,7 +45,8 @@ void synchronize_rcu(void)for_each_thread (t) {while ((per_thread (rcu_reader_gp, t) &0x1 ) && - ((per_thread (rcu_reader_gp, t) - rcu_gp_ctr) <0 )) { + ((per_thread (rcu_reader_gp, t) - +READ_ONCE (rcu_gp_ctr)) <0 )) {This
READ_ONCE()is not necessary, neither.
Quoted 3 lines
Please refer to Section 4.3.4.4 "Avoiding Data Races" for where to use READ/WRITE_ONCE(). You might want to self review 2/4 and 3/4 of this patch set.
Hi Akira,
Thanks a lot for pointing me to that! I will read that chapter and submit a new patch set for your review soon.
I would like to add you to the Reviewed-by. Would that be OK for you?
Quoted 26 lines
Quoted 26 lines
CodeSamples/defer/rcu.c +0
-0/*@@@ poll(NULL, 0, 10); */ barrier (); }CodeSamples/defer/rcu.h +3
-2@@ -41,7 +41,8 @@ static inline void rcu_read_lock(void)* periodic per-thread processing.) */ -__get_thread_var (rcu_reader_gp) = rcu_gp_ctr +1 ; +__get_thread_var (rcu_reader_gp) = +READ_ONCE (rcu_gp_ctr) +1 ;smp_mb (); }@@ -55,7 +56,7 @@ static inline void rcu_read_unlock(void)*/ smp_mb (); -__get_thread_var (rcu_reader_gp) = rcu_gp_ctr; +__get_thread_var (rcu_reader_gp) =READ_ONCE (rcu_gp_ctr); }
Quoted 11 lines
appendix/toyrcu/toyrcu.tex
Quoted 5 lines
CodeSamples/defer/rcu.h +0
-0extern void synchronize_rcu (void );appendix/toyrcu/toyrcu.tex +0
-0Or you can extract the snippet by using \begin{snippet} meta commands. That is a preferred way if there is code under CodeSamples/. Examples of the scheme can be found in (not yet up-to-date) Style Guide (D.3.1.1).
Thanks for the comment; this is the right approach.
Then, I am planning to submit two patch sets: the first is to correct the code, and the second is to use the new snippet scheme. Does that sound good?
Thanks,
--Junchang
Quoted 40 lines
That said, you are not obliged to do so. Conversion to the new scheme is on my to-do list anyway.
Thanks, Akira
Quoted 34 lines
appendix/toyrcu/toyrcu.tex +4
-4@@ -1223,7 +1223,7 @@ thread-local accesses to one, as is done in the next section. 1 static void rcu_read_lock(void) 2 { 3 __get_thread_var(rcu_reader_gp) = - 4 ACCESS_ONCE(rcu_gp_ctr) + 1; + 4 READ_ONCE(rcu_gp_ctr) + 1; 5 smp_mb(); 6 } 7@@ -1231,7 +1231,7 @@ thread-local accesses to one, as is done in the next section. 9 { 10 smp_mb(); 11 __get_thread_var(rcu_reader_gp) = -12 ACCESS_ONCE(rcu_gp_ctr); +12 READ_ONCE(rcu_gp_ctr); 13 } 14 15 void synchronize_rcu(void)@@ -1240,12 +1240,12 @@ thread-local accesses to one, as is done in the next section. 18 19 smp_mb(); 20 spin_lock(&rcu_gp_lock); -21 ACCESS_ONCE(rcu_gp_ctr) += 2; +21 WRITE_ONCE(rcu_gp_ctr, READ_ONCE(rcu_gp_ctr) + 2); 22 smp_mb(); 23 for_each_thread(t) { 24 while ((per_thread(rcu_reader_gp, t) & 0x1) && 25 ((per_thread(rcu_reader_gp, t) - -26 ACCESS_ONCE(rcu_gp_ctr)) < 0)) { +26 READ_ONCE(rcu_gp_ctr)) < 0)) { 27 poll(NULL, 0, 10); 28 } 29 }
PATCH 2/4 rcu_nest: Use READ_ONCE() and WRITE_ONCE() for shared variable rcu_gp_ctr
In reply to Junchang WangReplace the plain accesses and the use of ACCESS_ONCE() with READ_ONCE()/WRITE_ONCE(). And then update the tex file accordingly.
Remove the goto statement in rcu_read_lock() because it seems this goto logic has been discarded. If we should keep this code snippet, could you please let me know the goal of this code snippet? Then we should update the tex file accordingly.
--- CodeSamples/defer/rcu_nest.c | 5 +++-- CodeSamples/defer/rcu_nest.h | 8 +------- appendix/toyrcu/toyrcu.tex | 8 ++++---- 3 files changed, 8 insertions(+), 13 deletions(-)
CodeSamples/defer/rcu_nest.c +3 -2
@@ -35,7 +35,7 @@ void synchronize_rcu(void)/* Advance to a new grace-period number, enforce ordering. */ - rcu_gp_ctr +=RCU_GP_CTR_BOTTOM_BIT ; +WRITE_ONCE (rcu_gp_ctr,READ_ONCE (rcu_gp_ctr) +RCU_GP_CTR_BOTTOM_BIT );smp_mb ();/*
@@ -45,7 +45,8 @@ void synchronize_rcu(void)for_each_thread (t) {while (rcu_gp_ongoing (t) && - ((per_thread (rcu_reader_gp, t) - rcu_gp_ctr) <0 )) { + ((per_thread (rcu_reader_gp, t) - +READ_ONCE (rcu_gp_ctr)) <0 )) {/*@@@ poll(NULL, 0, 10); */ barrier (); }
CodeSamples/defer/rcu_nest.h +1 -7
@@ -49,18 +49,12 @@ static void rcu_read_lock(void)*/ rrgp = &__get_thread_var (rcu_reader_gp); -retry: tmp = *rrgp;if ((tmp &RCU_GP_CTR_NEST_MASK ) ==0 ) - tmp = rcu_gp_ctr; + tmp =READ_ONCE (rcu_gp_ctr); tmp++; *rrgp = tmp;smp_mb (); -if (((tmp &RCU_GP_CTR_NEST_MASK ) ==1 ) && - ((rcu_gp_ctr - tmp) > (RCU_GP_CTR_NEST_MASK <<8 )) !=0 ) { - (*rrgp)--; - goto retry; - } }static void rcu_read_unlock (void )
appendix/toyrcu/toyrcu.tex +4 -4
@@ -1420,7 +1420,7 @@ variables. 6 rrgp = &__get_thread_var(rcu_reader_gp); 7 tmp = *rrgp; 8 if ((tmp & RCU_GP_CTR_NEST_MASK) == 0) - 9 tmp = ACCESS_ONCE(rcu_gp_ctr); + 9 tmp = READ_ONCE(rcu_gp_ctr); 10 tmp++; 11 *rrgp = tmp; 12 smp_mb();
@@ -1440,13 +1440,13 @@ variables. 26 27 smp_mb(); 28 spin_lock(&rcu_gp_lock); -29 ACCESS_ONCE(rcu_gp_ctr) += -30 RCU_GP_CTR_BOTTOM_BIT; +29 WRITE_ONCE(rcu_gp_ctr, READ_ONCE(rcu_gp_ctr) + +30 RCU_GP_CTR_BOTTOM_BIT); 31 smp_mb(); 32 for_each_thread(t) { 33 while (rcu_gp_ongoing(t) && 34 ((per_thread(rcu_reader_gp, t) - -35 rcu_gp_ctr) < 0)) { +35 READ_ONCE(rcu_gp_ctr)) < 0)) { 36 poll(NULL, 0, 10); 37 } 38 }
Signature · 1 line
2.7.4
PATCH 3/4 rcu_qs: Use READ_ONCE() AND WRITE_ONCE() for shared variable rcu_gp_ctr
In reply to Junchang WangReplace the plain accesses and the use of ACCESS_ONCE() with READ_ONCE()/WRITE_ONCE(). And then update the tex file accordingly.
--- CodeSamples/defer/rcu_qs.c | 5 +++-- CodeSamples/defer/rcu_qs.h | 4 ++-- appendix/toyrcu/toyrcu.tex | 10 +++++----- 3 files changed, 10 insertions(+), 9 deletions(-)
CodeSamples/defer/rcu_qs.c +3 -2
@@ -41,7 +41,7 @@ void synchronize_rcu(void)/* Advance to a new grace-period number, enforce ordering. */ - rcu_gp_ctr +=2 ; +WRITE_ONCE (rcu_gp_ctr,READ_ONCE (rcu_gp_ctr) +2 );smp_mb ();/*
@@ -51,7 +51,8 @@ void synchronize_rcu(void)for_each_thread (t) {while (rcu_gp_ongoing (t) && - ((per_thread (rcu_reader_qs_gp, t) - rcu_gp_ctr) <0 )) { + ((per_thread (rcu_reader_qs_gp, t) - +READ_ONCE (rcu_gp_ctr)) <0 )) {/* @@@ poll(NULL, 0, 10); */ barrier (); }
CodeSamples/defer/rcu_qs.h +2 -2
@@ -59,14 +59,14 @@ static void rcu_read_unlock(void)static void rcu_quiescent_state (void ) {smp_mb (); -__get_thread_var (rcu_reader_qs_gp) =ACCESS_ONCE (rcu_gp_ctr) +1 ; +__get_thread_var (rcu_reader_qs_gp) =READ_ONCE (rcu_gp_ctr) +1 ;smp_mb (); }static void rcu_thread_offline (void ) {smp_mb (); -__get_thread_var (rcu_reader_qs_gp) =ACCESS_ONCE (rcu_gp_ctr); +__get_thread_var (rcu_reader_qs_gp) =READ_ONCE (rcu_gp_ctr);smp_mb (); }
appendix/toyrcu/toyrcu.tex +5 -5
@@ -1704,7 +1704,7 @@ overhead. 10 { 11 smp_mb(); 12 __get_thread_var(rcu_reader_qs_gp) = - 13 ACCESS_ONCE(rcu_gp_ctr) + 1; + 13 READ_ONCE(rcu_gp_ctr) + 1; 14 smp_mb(); 15 } 16
@@ -1712,7 +1712,7 @@ overhead. 18 { 19 smp_mb(); 20 __get_thread_var(rcu_reader_qs_gp) = - 21 ACCESS_ONCE(rcu_gp_ctr); + 21 READ_ONCE(rcu_gp_ctr); 22 smp_mb(); 23 } 24
@@ -1764,7 +1764,7 @@ to prevent any code prior to the quiescent state (including possible RCU read-side critical sections) from being reordered into the quiescent state. Lines~12-13 pick up a copy of the global \co{rcu_gp_ctr}, using -\co{ACCESS_ONCE()} to ensure that the compiler does not employ any +\co{READ_ONCE()} to ensure that the compiler does not employ any optimizations that would result in \co{rcu_gp_ctr} being fetched more than once, and then adds one to the value fetched and stores it into
@@ -1837,12 +1837,12 @@ quiescent state. 4 5 smp_mb(); 6 spin_lock(&rcu_gp_lock); - 7 rcu_gp_ctr += 2; + 7 WRITE_ONCE(rcu_gp_ctr, READ_ONCE(rcu_gp_ctr) + 2); 8 smp_mb(); 9 for_each_thread(t) { 10 while (rcu_gp_ongoing(t) && 11 ((per_thread(rcu_reader_qs_gp, t) - - 12 rcu_gp_ctr) < 0)) { + 12 READ_ONCE(rcu_gp_ctr)) < 0)) { 13 poll(NULL, 0, 10); 14 } 15 }
Signature · 1 line
2.7.4
PATCH 4/4 toyrcu: fix typo.
In reply to Junchang WangThis typo did confuse me when I was jumping from perfbook to the code. So fix it.
--- appendix/toyrcu/toyrcu.tex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
appendix/toyrcu/toyrcu.tex +1 -1
@@ -491,7 +491,7 @@ scheme that is more favorable to writers. \end{listing} Listing~\ref{lst:app:toyrcu:RCU Read-Side Using Global Reference-Count Pair} -(\path{rcu_rcgp.h}) +(\path{rcu_rcpg.h}) shows the read-side primitives of an RCU implementation that uses a pair of reference counters (\co{rcu_refcnt[]}), along with a global index that
Signature · 1 line
2.7.4