Skip to contentSkip to conversation

CodeSamples/count: change full memory barrier to partial one

PATCH Applied Alan Huang 3 participants 3 messages Apr 5, 2023 – Apr 6, 2023

lore
Share & tools
lore

Alan Huang

author
#0 lore raw

PATCH 1/1 CodeSamples/count: change full memory barrier to partial one

The original memory barriers in count_stat_eventual.c ensure writing to global_count happens before writing to stopflag and reading from stopflag happens before later reading from global_count.

Thus, smp_load_acquire and smp_store_release will suffice.

---
 CodeSamples/count/count_stat_eventual.c | 6 ++----
 count/count.tex                         | 6 +++---
 2 files changed, 5 insertions(+), 7 deletions(-)
CodeSamples/count/count_stat_eventual.c +2 -4
index 967644de..7157ee0e 100644
@@ -51,8 +51,7 @@ void *eventual(void *arg)				//\lnlbl{eventual:b}
 		WRITE_ONCE(global_count, sum);
 		poll(NULL, 0, 1);
 		if (READ_ONCE(stopflag)) {
-			smp_mb();
-			WRITE_ONCE(stopflag, stopflag + 1);
+			smp_store_release(&stopflag, stopflag + 1);
 		}
 	}
 	return NULL;
@@ -73,9 +72,8 @@ void count_init(void)					//\lnlbl{init:b}
 void count_cleanup(void)				//\lnlbl{cleanup:b}
 {
 	WRITE_ONCE(stopflag, 1);
-	while (READ_ONCE(stopflag) < 3)
+	while (smp_load_acquire(&stopflag) < 3)
 		poll(NULL, 0, 1);
-	smp_mb();
 }							//\lnlbl{cleanup:e}
 //\end{snippet}
 
count/count.tex +3 -3
index 80ada104..8ab67e2e 100644
@@ -1013,9 +1013,9 @@ between passes.
 
 The \co{count_cleanup()} function on
 \clnrefrange{cleanup:b}{cleanup:e} coordinates termination.
-The calls to \co{smp_mb()} here and in \co{eventual()} ensure
-that all updates to \co{global_count} are visible to code following
-the call to \co{count_cleanup()}.
+The call to \co{smp_load_acquire()} here and the call to \co{smp_store_release()}
+in \co{eventual()} ensure that all updates to \co{global_count} are visible
+to code following the call to \co{count_cleanup()}.
 
 This approach gives extremely fast counter read-out while still
 supporting linear counter-update scalability.
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 44 lines · Alan Huang

The original memory barriers in count_stat_eventual.c ensure writing to global_count happens before writing to stopflag and reading from stopflag happens before later reading from global_count.

Thus, smp_load_acquire and smp_store_release will suffice.

---
 CodeSamples/count/count_stat_eventual.c | 6 ++----
 count/count.tex                         | 6 +++---
 2 files changed, 5 insertions(+), 7 deletions(-)
CodeSamples/count/count_stat_eventual.c +2 -4
index 967644de..7157ee0e 100644
@@ -51,8 +51,7 @@ void *eventual(void *arg)				//\lnlbl{eventual:b}
 		WRITE_ONCE(global_count, sum);
 		poll(NULL, 0, 1);
 		if (READ_ONCE(stopflag)) {
-			smp_mb();
-			WRITE_ONCE(stopflag, stopflag + 1);
+			smp_store_release(&stopflag, stopflag + 1);
 		}
 	}
 	return NULL;
@@ -73,9 +72,8 @@ void count_init(void)					//\lnlbl{init:b}
 void count_cleanup(void)				//\lnlbl{cleanup:b}
 {
 	WRITE_ONCE(stopflag, 1);
-	while (READ_ONCE(stopflag) < 3)
+	while (smp_load_acquire(&stopflag) < 3)
 		poll(NULL, 0, 1);
-	smp_mb();
 }							//\lnlbl{cleanup:e}
 //\end{snippet}
 
count/count.tex +0 -0
index 80ada104..8ab67e2e 100644
@@ -1013,9 +1013,9 @@ between passes.
 
 The \co{count_cleanup()} function on

count/count.tex between passes. · line 1021

Quoted 10 lines
count/count.tex +3 -3
 \clnrefrange{cleanup:b}{cleanup:e} coordinates termination.
-The calls to \co{smp_mb()} here and in \co{eventual()} ensure
-that all updates to \co{global_count} are visible to code following
-the call to \co{count_cleanup()}.
+The call to \co{smp_load_acquire()} here and the call to \co{smp_store_release()}
+in \co{eventual()} ensure that all updates to \co{global_count} are visible
+to code following the call to \co{count_cleanup()}.
 
 This approach gives extremely fast counter read-out while still
 supporting linear counter-update scalability.

Looks good to me! Thanks.

#2 lore raw
Quoted 53 lines · Akira Yokosawa
Quoted 52 lines · Alan Huang

The original memory barriers in count_stat_eventual.c ensure writing to global_count happens before writing to stopflag and reading from stopflag happens before later reading from global_count.

Thus, smp_load_acquire and smp_store_release will suffice.

---
 CodeSamples/count/count_stat_eventual.c | 6 ++----
 count/count.tex                         | 6 +++---
 2 files changed, 5 insertions(+), 7 deletions(-)
CodeSamples/count/count_stat_eventual.c +2 -4
index 967644de..7157ee0e 100644
@@ -51,8 +51,7 @@ void *eventual(void *arg)				//\lnlbl{eventual:b}
 		WRITE_ONCE(global_count, sum);
 		poll(NULL, 0, 1);
 		if (READ_ONCE(stopflag)) {
-			smp_mb();
-			WRITE_ONCE(stopflag, stopflag + 1);
+			smp_store_release(&stopflag, stopflag + 1);
 		}
 	}
 	return NULL;
@@ -73,9 +72,8 @@ void count_init(void)					//\lnlbl{init:b}
 void count_cleanup(void)				//\lnlbl{cleanup:b}
 {
 	WRITE_ONCE(stopflag, 1);
-	while (READ_ONCE(stopflag) < 3)
+	while (smp_load_acquire(&stopflag) < 3)
 		poll(NULL, 0, 1);
-	smp_mb();
 }							//\lnlbl{cleanup:e}
 //\end{snippet}
 
count/count.tex +3 -3
index 80ada104..8ab67e2e 100644
@@ -1013,9 +1013,9 @@ between passes.
 
 The \co{count_cleanup()} function on
 \clnrefrange{cleanup:b}{cleanup:e} coordinates termination.
-The calls to \co{smp_mb()} here and in \co{eventual()} ensure
-that all updates to \co{global_count} are visible to code following
-the call to \co{count_cleanup()}.
+The call to \co{smp_load_acquire()} here and the call to \co{smp_store_release()}
+in \co{eventual()} ensure that all updates to \co{global_count} are visible
+to code following the call to \co{count_cleanup()}.
 
Quoted 6 lines

count/count.tex between passes. · line 1021

Quoted 2 lines
count/count.tex +0 -0
 This approach gives extremely fast counter read-out while still
 supporting linear counter-update scalability.

Looks good to me! Thanks.

Queued and pushed with the inevitable wordsmithing, thank you both!

Thanx, Paul