Skip to contentSkip to conversation

Toyrcu: replace racy plain accesses with READ_ONCE()

PATCH New Junchang Wang 1 participant 3 messages May 1, 2019

lore
Share & tools
lore
#0 lore raw

v4 cover Toyrcu: replace racy plain accesses with READ_ONCE()

Hi list,

Variable rcu_gp_ctr is accessed concurrently by the updater and readers, and hence should be protected by READ_ONCE() and WRITE_ONCE(). This patch set updates rcu and rcu_qs.

The tex file is updated accordingly. And a new scheme, which is suggested by Akira and can extract code snippets from the source code, has been applied.

Please note that rcu_nest also needs to be updated. I will first make sure that I'm fully understand how it works, and then submit a patch for it.

Junchang Wang (2):
rcu: Use READ_ONCE() and WRITE_ONCE() for shared variable rcu_gp_ctr
rcu_qs: Use READ_ONCE() AND WRITE_ONCE() for shared variable
rcu_gp_ctr

 CodeSamples/defer/rcu.c    |  34 +++++---
 CodeSamples/defer/rcu.h    |  21 +++--
 CodeSamples/defer/rcu_qs.c |  31 ++++---
 CodeSamples/defer/rcu_qs.h |  42 ++++-----
 appendix/toyrcu/toyrcu.tex | 206 +++++++++++++++++----------------------------
 5 files changed, 152 insertions(+), 182 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH v4 1/2 rcu: Use READ_ONCE() and WRITE_ONCE() for shared variable rcu_gp_ctr

Replace racy plain accesses with READ_ONCE()/WRITE_ONCE(). And then update the tex file accordingly. A new scheme, which directly extracts snippet from rcu.[hc], is applied.

---
 CodeSamples/defer/rcu.c    | 34 +++++++++-------
 CodeSamples/defer/rcu.h    | 21 ++++++----
 appendix/toyrcu/toyrcu.tex | 97 ++++++++++++++++++++--------------------------
 3 files changed, 74 insertions(+), 78 deletions(-)
CodeSamples/defer/rcu.c +20 -14
index 4b868cc..483b5fa 100644
@@ -21,45 +21,51 @@
 #include "../api.h"
 #include "rcu.h"
 
-void synchronize_rcu(void)
+//\begin{snippet}[labelbase=ln:defer:rcu:synchronize,gobbleblank=yes,commandchars=\%\@\$]
+void synchronize_rcu(void)			//\lnlbl{syn:b}
 {
 	int t;
-
+							//\fcvblank
 	/* Memory barrier ensures mutation seen before grace period. */
 
-	smp_mb();
+	smp_mb();				//\lnlbl{syn:mb1}
 
 	/* Only one synchronize_rcu() at a time. */
 
-	spin_lock(&rcu_gp_lock);
+	spin_lock(&rcu_gp_lock);		//\lnlbl{syn:spinlock}
 
 	/* Advance to a new grace-period number, enforce ordering. */
 
-	rcu_gp_ctr += 2;
-	smp_mb();
+	WRITE_ONCE(rcu_gp_ctr, rcu_gp_ctr + 2); //\lnlbl{syn:increasegp}
+	smp_mb();				//\lnlbl{syn:mb2}
 
 	/*
 	 * Wait until all threads are either out of their RCU read-side
 	 * critical sections or are aware of the new grace period.
 	 */
 
-	for_each_thread(t) {
-		while ((per_thread(rcu_reader_gp, t) & 0x1) &&
-		       ((per_thread(rcu_reader_gp, t) - rcu_gp_ctr) < 0)) {
+	for_each_thread(t) {			//\lnlbl{syn:scan:b}
+		while ((per_thread(rcu_reader_gp, t) & 0x1) &&//\lnlbl{syn:even}
+		       ((per_thread(rcu_reader_gp, t) -	//\lnlbl{syn:gt1}
+		         rcu_gp_ctr) < 0)) {		//\lnlbl{syn:gt2}
+#ifndef FCV_SNIPPET
 			/*@@@ poll(NULL, 0, 10); */
 			barrier();
+#else
+			poll(NULL, 0, 10);	//\lnlbl{syn:poll}
+#endif
 		}
-	}
+	}					//\lnlbl{syn:scan:e}
 
 	/* Let other synchronize_rcu() instances move ahead. */
 
-	spin_unlock(&rcu_gp_lock);
+	spin_unlock(&rcu_gp_lock);		//\lnlbl{syn:spinunlock}
 
 	/* Ensure that any subsequent free()s happen -after- above checks. */
 
-	smp_mb();
-}
-
+	smp_mb();				//\lnlbl{syn:mb3}
+}						//\lnlbl{syn:e}
+//\end{snippet}
 #ifdef TEST
 #include "rcutorture.h"
 #endif /* #ifdef TEST */
CodeSamples/defer/rcu.h +13 -8
index f493f8b..3641c33 100644
@@ -31,7 +31,8 @@ static inline void rcu_init(void)
 	init_per_thread(rcu_reader_gp_snap, 0);
 }
 
-static inline void rcu_read_lock(void)
+//\begin{snippet}[labelbase=ln:defer:rcu:read_lock_unlock,gobbleblank=yes,commandchars=\%\@\$]
+static inline void rcu_read_lock(void)			//\lnlbl{lock:b}
 {
 	/*
 	 * Copy the current GP counter to this thread's counter, setting
@@ -41,11 +42,12 @@ static inline void rcu_read_lock(void)
 	 * periodic per-thread processing.)
 	 */
 
-	__get_thread_var(rcu_reader_gp) = rcu_gp_ctr + 1;
-	smp_mb();
-}
-
-static inline void rcu_read_unlock(void)
+	__get_thread_var(rcu_reader_gp) =		//\lnlbl{lock:gp1}
+		READ_ONCE(rcu_gp_ctr) + 1;		//\lnlbl{lock:gp2}
+	smp_mb();					//\lnlbl{lock:mb}
+}							//\lnlbl{lock:e}
+								//\fcvblank
+static inline void rcu_read_unlock(void)		//\lnlbl{unlock:b}
 {
 	/*
 	 * Copy the current GP counter to this thread's counter, but
@@ -54,8 +56,11 @@ static inline void rcu_read_unlock(void)
 	 * the memory barrier requires periodic per-thread processing.)
 	 */
 
-	smp_mb();
-	__get_thread_var(rcu_reader_gp) = rcu_gp_ctr;
+	smp_mb();					//\lnlbl{unlock:mb}
+	__get_thread_var(rcu_reader_gp) =		//\lnlbl{unlock:gp1}
+		READ_ONCE(rcu_gp_ctr);			//\lnlbl{unlock:gp2}
 }
+								//\fcvblank
+//\end{snippet}
 
 extern void synchronize_rcu(void);
appendix/toyrcu/toyrcu.tex +41 -56

175 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

PATCH v4 2/2 rcu_qs: Use READ_ONCE() AND WRITE_ONCE() for shared variable rcu_gp_ctr

In reply to Junchang Wang

Replace racy plain accesses with READ_ONCE()/WRITE_ONCE(). And then update the tex file accordingly. A new scheme, which directly extracts snippet from rcu_qs.[hc], is applied.

---
 CodeSamples/defer/rcu_qs.c |  31 ++++++++-----
 CodeSamples/defer/rcu_qs.h |  42 +++++++++--------
 appendix/toyrcu/toyrcu.tex | 109 +++++++++++++++------------------------------
 3 files changed, 78 insertions(+), 104 deletions(-)
CodeSamples/defer/rcu_qs.c +19 -12
index 27e45f7..6263798 100644
@@ -27,44 +27,51 @@
 #include "../api.h"
 #include "rcu_qs.h"
 
-void synchronize_rcu(void)
+//\begin{snippet}[labelbase=ln:defer:rcu_qs:synchronize,gobbleblank=yes,commandchars=\%\@\$]
+void synchronize_rcu(void)			//\lnlbl{syn:b}
 {
 	int t;
-
+							//\fcvblank
 	/* Memory barrier ensures mutation seen before grace period. */
 
-	smp_mb();
+	smp_mb();				//\lnlbl{syn:mb1}
 
 	/* Only one synchronize_rcu() at a time. */
 
-	spin_lock(&rcu_gp_lock);
+	spin_lock(&rcu_gp_lock);		//\lnlbl{syn:spinlock}
 
 	/* Advance to a new grace-period number, enforce ordering. */
 
-	rcu_gp_ctr += 2;
-	smp_mb();
+	WRITE_ONCE(rcu_gp_ctr, rcu_gp_ctr + 2);	//\lnlbl{syn:increasegp}
+	smp_mb();				//\lnlbl{syn:mb2}
 
 	/*
 	 * Wait until all threads are either out of their RCU read-side
 	 * critical sections or are aware of the new grace period.
 	 */
 
-	for_each_thread(t) {
-		while (rcu_gp_ongoing(t) &&
-		       ((per_thread(rcu_reader_qs_gp, t) - rcu_gp_ctr) < 0)) {
+	for_each_thread(t) {			//\lnlbl{syn:scan:b}
+		while (rcu_gp_ongoing(t) &&	//\lnlbl{syn:ongoing}
+		       ((per_thread(rcu_reader_qs_gp, t)//\lnlbl{syn:gp1}
+		         - rcu_gp_ctr) < 0)) {	//\lnlbl{syn:gp2}
+#ifndef FCV_SNIPPET
 			/* @@@ poll(NULL, 0, 10); */
 			barrier();
+#else
+			poll(NULL, 0, 10);	//\lnlbl{syn:poll}
+#endif
 		}
-	}
+	}					//\lnlbl{syn:scan:e}
 
 	/* Let other synchronize_rcu() instances move ahead. */
 
-	spin_unlock(&rcu_gp_lock);
+	spin_unlock(&rcu_gp_lock);		//\lnlbl{syn:spinunlock}
 
 	/* Ensure that any subsequent free()s happen -after- above checks. */
 
-	smp_mb();
+	smp_mb();				//\lnlbl{syn:mb3}
 }
+//\end{snippet}
 
 #ifdef TEST
 #include "rcutorture.h"
CodeSamples/defer/rcu_qs.h +23 -19

54 lines not on this page · raw message

appendix/toyrcu/toyrcu.tex +36 -73

181 lines not on this page · raw message

Signature · 1 line
2.7.4