Skip to contentSkip to conversation

CodeSamples/count: Remove unnecessary memory barriers

PATCH Superseded Alan Huang 4 participants 10 messages Apr 7, 2023 – Apr 12, 2023

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Alan Huang

author
#0 lore raw

PATCH 1/1 CodeSamples/count: Remove unnecessary memory barriers

In count_lim_sig.c, there is only one ordering required, that is writing to counter happens before setting theft to THEFT_READY in add_count/sub_count's fast path. Therefore, partial memory barrier will suffice.

---
 CodeSamples/count/count_lim_sig.c | 10 +++-------
 count/count.tex                   | 12 ++++++------
 2 files changed, 9 insertions(+), 13 deletions(-)
CodeSamples/count/count_lim_sig.c +3 -7
index 59da8077..c2f61197 100644
@@ -56,12 +56,10 @@ static void flush_local_count_sig(int unused)	//\lnlbl{flush_sig:b}
 {
 	if (READ_ONCE(theft) != THEFT_REQ)	//\lnlbl{flush_sig:check:REQ}
 		return;				//\lnlbl{flush_sig:return:n}
-	smp_mb();				//\lnlbl{flush_sig:mb:1}
 	WRITE_ONCE(theft, THEFT_ACK);		//\lnlbl{flush_sig:set:ACK}
 	if (!counting) {			//\lnlbl{flush_sig:check:fast}
-		WRITE_ONCE(theft, THEFT_READY);	//\lnlbl{flush_sig:set:READY}
+		smp_store_release(&theft, THEFT_READY);	//\lnlbl{flush_sig:set:READY}
 	}
-	smp_mb();
 }						//\lnlbl{flush_sig:e}
 
 static void flush_local_count(void)			//\lnlbl{flush:b}
@@ -125,8 +123,7 @@ int add_count(unsigned long delta)			//\lnlbl{b}
 	WRITE_ONCE(counting, 0);			//\lnlbl{clearcnt}
 	barrier();					//\lnlbl{barrier:3}
 	if (READ_ONCE(theft) == THEFT_ACK) {		//\lnlbl{check:ACK}
-		smp_mb();				//\lnlbl{mb}
-		WRITE_ONCE(theft, THEFT_READY);		//\lnlbl{READY}
+		smp_store_release(&theft, THEFT_READY);		//\lnlbl{READY}
 	}
 	if (fastpath)
 		return 1;				//\lnlbl{return:fs}
@@ -164,8 +161,7 @@ int sub_count(unsigned long delta)
 	WRITE_ONCE(counting, 0);
 	barrier();
 	if (READ_ONCE(theft) == THEFT_ACK) {
-		smp_mb();
-		WRITE_ONCE(theft, THEFT_READY);
+		smp_store_release(&theft, THEFT_READY);
 	}
 	if (fastpath)
 		return 1;
count/count.tex +6 -6
index 8ab67e2e..4c139d63 100644
@@ -2425,12 +2425,11 @@ handler used in the theft process.
 \Clnref{check:REQ,return:n} check to see if
 the \co{theft} state is REQ, and, if not
 returns without change.
-\Clnref{mb:1} executes a \IX{memory barrier} to ensure that the sampling
-of the theft variable happens before any change to that variable.
 \Clnref{set:ACK} sets the \co{theft} state to ACK, and, if
 \clnref{check:fast} sees that
 this thread's fastpaths are not running, \clnref{set:READY} sets the \co{theft}
-state to READY\@.
+state to READY, with the release store ensuring any change to counter in
+the fastpath happens before the change of theft to READY\@.
 \end{fcvref}
 
 \begin{listing}
@@ -2595,9 +2594,10 @@ handlers to undertake theft.
 \Clnref{barrier:3} again disables compiler reordering, and then
 \clnref{check:ACK}
 checks to see if the signal handler deferred the \co{theft}
-state-change to READY, and, if so, \clnref{mb} executes a memory
-barrier to ensure that any CPU that sees \clnref{READY} setting state to
-READY also sees the effects of \clnref{add:f}.
+state-change to READY, and, if so, \clnref{READY} changes theft to
+READY with the release store ensuring that
+any CPU that sees the READY state also sees the effects
+of \clnref{add:f}.
 If the fastpath addition at \clnref{add:f} was executed, then
 \clnref{return:fs} returns
 success.
Signature · 1 line
2.34.1
author
#1 lore raw

Hi Paul and Akira,

This is the patch v3, I forgot to add a version tag…

And I think it may be better to add a question in a subsequent path.

Thanks,
Alan

Quoted 87 lines

2023年4月8日 上午1:58,Alan Huang <[email protected]> 写道:

In count_lim_sig.c, there is only one ordering required, that is writing to counter happens before setting theft to THEFT_READY in add_count/sub_count's fast path. Therefore, partial memory barrier will suffice.

---
CodeSamples/count/count_lim_sig.c | 10 +++-------
count/count.tex | 12 ++++++------

2 files changed, 9 insertions(+), 13 deletions(-)
CodeSamples/count/count_lim_sig.c +3 -7
index 59da8077..c2f61197 100644
@@ -56,12 +56,10 @@ static void flush_local_count_sig(int unused)	//\lnlbl{flush_sig:b}
 {
 	if (READ_ONCE(theft) != THEFT_REQ)	//\lnlbl{flush_sig:check:REQ}
 		return;				//\lnlbl{flush_sig:return:n}
-	smp_mb();				//\lnlbl{flush_sig:mb:1}
 	WRITE_ONCE(theft, THEFT_ACK);		//\lnlbl{flush_sig:set:ACK}
 	if (!counting) {			//\lnlbl{flush_sig:check:fast}
-		WRITE_ONCE(theft, THEFT_READY);	//\lnlbl{flush_sig:set:READY}
+		smp_store_release(&theft, THEFT_READY);	//\lnlbl{flush_sig:set:READY}
 	}
-	smp_mb();
 }						//\lnlbl{flush_sig:e}
 
 static void flush_local_count(void)			//\lnlbl{flush:b}
@@ -125,8 +123,7 @@ int add_count(unsigned long delta)			//\lnlbl{b}
 	WRITE_ONCE(counting, 0);			//\lnlbl{clearcnt}
 	barrier();					//\lnlbl{barrier:3}
 	if (READ_ONCE(theft) == THEFT_ACK) {		//\lnlbl{check:ACK}
-		smp_mb();				//\lnlbl{mb}
-		WRITE_ONCE(theft, THEFT_READY);		//\lnlbl{READY}
+		smp_store_release(&theft, THEFT_READY);		//\lnlbl{READY}
 	}
 	if (fastpath)
 		return 1;				//\lnlbl{return:fs}
@@ -164,8 +161,7 @@ int sub_count(unsigned long delta)
 	WRITE_ONCE(counting, 0);
 	barrier();
 	if (READ_ONCE(theft) == THEFT_ACK) {
-		smp_mb();
-		WRITE_ONCE(theft, THEFT_READY);
+		smp_store_release(&theft, THEFT_READY);
 	}
 	if (fastpath)
 		return 1;
count/count.tex +0 -0
index 8ab67e2e..4c139d63 100644
@@ -2425,12 +2425,11 @@ handler used in the theft process.

\Clnref{check:REQ,return:n} check to see if
the \co{theft} state is REQ, and, if not
returns without change.
-\Clnref{mb:1} executes a \IX{memory barrier} to ensure that the sampling -of the theft variable happens before any change to that variable. \Clnref{set:ACK} sets the \co{theft} state to ACK, and, if \clnref{check:fast} sees that
this thread's fastpaths are not running, \clnref{set:READY} sets the \co{theft}

count/count.tex +2 -1
-state to READY\@.
+state to READY, with the release store ensuring any change to counter in
+the fastpath happens before the change of theft to READY\@.

\end{fcvref}

\begin{listing}

count/count.tex +0 -0
@@ -2595,9 +2594,10 @@ handlers to undertake theft.

\Clnref{barrier:3} again disables compiler reordering, and then \clnref{check:ACK}
checks to see if the signal handler deferred the \co{theft}

count/count.tex +4 -3
-state-change to READY, and, if so, \clnref{mb} executes a memory
-barrier to ensure that any CPU that sees \clnref{READY} setting state to
-READY also sees the effects of \clnref{add:f}.
+state-change to READY, and, if so, \clnref{READY} changes theft to
+READY with the release store ensuring that
+any CPU that sees the READY state also sees the effects
+of \clnref{add:f}.

If the fastpath addition at \clnref{add:f} was executed, then \clnref{return:fs} returns
success.

Signature · 1 line
2.34.1
#2 lore raw

Hi,

Quoted 5 lines · Alan Huang

Hi Paul and Akira,

This is the patch v3, I forgot to add a version tag…

And I think it may be better to add a question in a subsequent path.

So I'm looking at Quick Quizzes on count_lim_sig.c. It looks to me Quick Quiz 5.50 lost its context due to the removal of smp_mb() and the use of smp_store_release().

A band-aide fix would be to change the quiz to:

In Listing 5.18's function flush_local_count_sig(), why are there READ_ONCE(), WRITE_ONCE(), and smp_store_release() wrappers around the uses of the theft per-thread variable?

, and to adjust the line count in its Answer.

However, smp_store_release() is by no means a simple wrapper. So I'm wondering if it is worthwhile to keep this quiz.

Paul, how about replacing the quiz with the new quiz asking:

In Listing 5.18, doesn't flush_local_count_sig() need a stronger memory barrier or two?

, and picking Alan's reasoning in the answer?

Thanks, Akira

Quoted 3 lines

Thanks,
Alan

#3 lore raw
Quoted 24 lines · Akira Yokosawa

Hi,

Quoted 5 lines · Alan Huang

Hi Paul and Akira,

This is the patch v3, I forgot to add a version tag…

And I think it may be better to add a question in a subsequent path.

So I'm looking at Quick Quizzes on count_lim_sig.c. It looks to me Quick Quiz 5.50 lost its context due to the removal of smp_mb() and the use of smp_store_release().

A band-aide fix would be to change the quiz to:

In Listing 5.18's function flush_local_count_sig(), why are there READ_ONCE(), WRITE_ONCE(), and smp_store_release() wrappers around the uses of the theft per-thread variable?

, and to adjust the line count in its Answer.

However, smp_store_release() is by no means a simple wrapper. So I'm wondering if it is worthwhile to keep this quiz.

Quoted 6 lines

Paul, how about replacing the quiz with the new quiz asking:

In Listing 5.18, doesn't flush_local_count_sig() need a stronger memory barrier or two?

, and picking Alan's reasoning in the answer?

How about something like this?

Thanx, Paul

------------------------------------------------------------------------

count/count.tex +4 -21
index 2aade053..ccc2e839 100644
@@ -2441,28 +2441,11 @@ the fastpath happens before this change of \co{theft} to READY\@.
 
 \QuickQuiz{
 	In \cref{lst:count:Signal-Theft Limit Counter Value-Migration Functions}'s
-	function \co{flush_local_count_sig()}, why are there
-	\co{READ_ONCE()} and \co{WRITE_ONCE()} wrappers around
-	the uses of the
-	\co{theft} per-thread variable?
+	doesn't \co{flush_local_count_sig()} need stronger memory barriers?
 }\QuickQuizAnswer{
-	\begin{fcvref}[ln:count:count_lim_sig:migration:flush_sig]
-	The first one (on \clnref{check:REQ}) can be argued to be unnecessary.
-	The last two (\clnref{set:ACK,set:READY}) are important.
-	If these are removed, the compiler would be within its rights
-	to rewrite \clnrefrange{set:ACK}{set:READY} as follows:
-	\end{fcvref}
-
-\begin{VerbatimN}[firstnumber=14]
-theft = THEFT_READY;
-if (counting) {
-	theft = THEFT_ACK;
-}
-\end{VerbatimN}
-
-	This would be fatal, as the slowpath might see the transient
-	value of \co{THEFT_READY}, and start stealing before the
-	corresponding thread was ready.
+	No, that \co{smp_store_release()} suffices because this code
+	communicates only with \co{flush_local_count()}, and there is
+	no need for store-to-load ordering.
 }\QuickQuizEnd
 
 \begin{fcvref}[ln:count:count_lim_sig:migration:flush]
author
#4 lore raw

The signal-theft counter reminds me of the signal-based user space RCU implementation, the signal hander there has two full memory barriers around it, how about mentioning that a little?

Thanks,
Alan

Quoted 77 lines

2023年4月11日 上午3:06,Paul E. McKenney <[email protected]> 写道:

Quoted 24 lines · Akira Yokosawa

Hi,

Quoted 5 lines · Alan Huang

Hi Paul and Akira,

This is the patch v3, I forgot to add a version tag…

And I think it may be better to add a question in a subsequent path.

So I'm looking at Quick Quizzes on count_lim_sig.c. It looks to me Quick Quiz 5.50 lost its context due to the removal of smp_mb() and the use of smp_store_release().

A band-aide fix would be to change the quiz to:

In Listing 5.18's function flush_local_count_sig(), why are there READ_ONCE(), WRITE_ONCE(), and smp_store_release() wrappers around the uses of the theft per-thread variable?

, and to adjust the line count in its Answer.

However, smp_store_release() is by no means a simple wrapper. So I'm wondering if it is worthwhile to keep this quiz.

Quoted 6 lines

Paul, how about replacing the quiz with the new quiz asking:

In Listing 5.18, doesn't flush_local_count_sig() need a stronger memory barrier or two?

, and picking Alan's reasoning in the answer?

How about something like this?

Thanx, Paul

------------------------------------------------------------------------

count/count.tex +4 -21
index 2aade053..ccc2e839 100644
@@ -2441,28 +2441,11 @@ the fastpath happens before this change of \co{theft} to READY\@.
 
 \QuickQuiz{
 	In \cref{lst:count:Signal-Theft Limit Counter Value-Migration Functions}'s
-	function \co{flush_local_count_sig()}, why are there
-	\co{READ_ONCE()} and \co{WRITE_ONCE()} wrappers around
-	the uses of the
-	\co{theft} per-thread variable?
+	doesn't \co{flush_local_count_sig()} need stronger memory barriers?
 }\QuickQuizAnswer{
-	\begin{fcvref}[ln:count:count_lim_sig:migration:flush_sig]
-	The first one (on \clnref{check:REQ}) can be argued to be unnecessary.
-	The last two (\clnref{set:ACK,set:READY}) are important.
-	If these are removed, the compiler would be within its rights
-	to rewrite \clnrefrange{set:ACK}{set:READY} as follows:
-	\end{fcvref}
-
-\begin{VerbatimN}[firstnumber=14]
-theft = THEFT_READY;
-if (counting) {
-	theft = THEFT_ACK;
-}
-\end{VerbatimN}
-
-	This would be fatal, as the slowpath might see the transient
-	value of \co{THEFT_READY}, and start stealing before the
-	corresponding thread was ready.
+	No, that \co{smp_store_release()} suffices because this code
+	communicates only with \co{flush_local_count()}, and there is
+	no need for store-to-load ordering.
 }\QuickQuizEnd
 

\begin{fcvref}[ln:count:count_lim_sig:migration:flush]

#5 lore raw

I may be missing something, but why do you need memory barriers at all in the signal handler? At least on the systems I know both setting up the signal handler and restoring the context require kernel calls. On x86 this doesn't matter as it is strongly ordered, but on ARM the kernel calls provide full synchronization points.
Are there architectures/OS combinations where that is not true?

--Elad

Quoted 87 lines · Alan Huang

The signal-theft counter reminds me of the signal-based user space RCU implementation, the signal hander there has two full memory barriers around it, how about mentioning that a little?

Thanks,
Alan

Quoted 77 lines

2023年4月11日 上午3:06,Paul E. McKenney <[email protected]> 写道:

Quoted 24 lines · Akira Yokosawa

Hi,

Quoted 5 lines · Alan Huang

Hi Paul and Akira,

This is the patch v3, I forgot to add a version tag…

And I think it may be better to add a question in a subsequent path.

So I'm looking at Quick Quizzes on count_lim_sig.c. It looks to me Quick Quiz 5.50 lost its context due to the removal of smp_mb() and the use of smp_store_release().

A band-aide fix would be to change the quiz to:

In Listing 5.18's function flush_local_count_sig(), why are there READ_ONCE(), WRITE_ONCE(), and smp_store_release() wrappers around the uses of the theft per-thread variable?

, and to adjust the line count in its Answer.

However, smp_store_release() is by no means a simple wrapper. So I'm wondering if it is worthwhile to keep this quiz.

Quoted 6 lines

Paul, how about replacing the quiz with the new quiz asking:

In Listing 5.18, doesn't flush_local_count_sig() need a stronger memory barrier or two?

, and picking Alan's reasoning in the answer?

How about something like this?

Thanx, Paul

------------------------------------------------------------------------

count/count.tex +4 -21
index 2aade053..ccc2e839 100644
@@ -2441,28 +2441,11 @@ the fastpath happens before this change of \co{theft} to READY\@.
 
 \QuickQuiz{
      In \cref{lst:count:Signal-Theft Limit Counter Value-Migration Functions}'s
-     function \co{flush_local_count_sig()}, why are there
-     \co{READ_ONCE()} and \co{WRITE_ONCE()} wrappers around
-     the uses of the
-     \co{theft} per-thread variable?
+     doesn't \co{flush_local_count_sig()} need stronger memory barriers?
 }\QuickQuizAnswer{
-     \begin{fcvref}[ln:count:count_lim_sig:migration:flush_sig]
-     The first one (on \clnref{check:REQ}) can be argued to be unnecessary.
-     The last two (\clnref{set:ACK,set:READY}) are important.
-     If these are removed, the compiler would be within its rights
-     to rewrite \clnrefrange{set:ACK}{set:READY} as follows:
-     \end{fcvref}
-
-\begin{VerbatimN}[firstnumber=14]
-theft = THEFT_READY;
-if (counting) {
-     theft = THEFT_ACK;
-}
-\end{VerbatimN}
-
-     This would be fatal, as the slowpath might see the transient
-     value of \co{THEFT_READY}, and start stealing before the
-     corresponding thread was ready.
+     No, that \co{smp_store_release()} suffices because this code
+     communicates only with \co{flush_local_count()}, and there is
+     no need for store-to-load ordering.
 }\QuickQuizEnd
 

\begin{fcvref}[ln:count:count_lim_sig:migration:flush]

#6 lore raw
Quoted 6 lines · Elad Lahav

I may be missing something, but why do you need memory barriers at all in the signal handler? At least on the systems I know both setting up the signal handler and restoring the context require kernel calls. On x86 this doesn't matter as it is strongly ordered, but on ARM the kernel calls provide full synchronization points.
Are there architectures/OS combinations where that is not true?

It is quite possible that there is still more memory ordering in this code than is absolutely required.

I would have some difficulty forcing myself to rely on implicit ordering due to signal entry and exit, but then again, I have also some difficulty forcing myself to rely on signals. ;-)

But if there is some signal entry/exit guarantee in (say) POSIX, we should be OK relying on it.

Any thoughts on interrupt handlers in kernels?

Thanx, Paul

Quoted 90 lines

--Elad

Quoted 87 lines · Alan Huang

The signal-theft counter reminds me of the signal-based user space RCU implementation, the signal hander there has two full memory barriers around it, how about mentioning that a little?

Thanks,
Alan

Quoted 77 lines

2023年4月11日 上午3:06,Paul E. McKenney <[email protected]> 写道:

Quoted 24 lines · Akira Yokosawa

Hi,

Quoted 5 lines · Alan Huang

Hi Paul and Akira,

This is the patch v3, I forgot to add a version tag…

And I think it may be better to add a question in a subsequent path.

So I'm looking at Quick Quizzes on count_lim_sig.c. It looks to me Quick Quiz 5.50 lost its context due to the removal of smp_mb() and the use of smp_store_release().

A band-aide fix would be to change the quiz to:

In Listing 5.18's function flush_local_count_sig(), why are there READ_ONCE(), WRITE_ONCE(), and smp_store_release() wrappers around the uses of the theft per-thread variable?

, and to adjust the line count in its Answer.

However, smp_store_release() is by no means a simple wrapper. So I'm wondering if it is worthwhile to keep this quiz.

Quoted 6 lines

Paul, how about replacing the quiz with the new quiz asking:

In Listing 5.18, doesn't flush_local_count_sig() need a stronger memory barrier or two?

, and picking Alan's reasoning in the answer?

How about something like this?

Thanx, Paul

------------------------------------------------------------------------

count/count.tex +4 -21
index 2aade053..ccc2e839 100644
@@ -2441,28 +2441,11 @@ the fastpath happens before this change of \co{theft} to READY\@.
 
 \QuickQuiz{
      In \cref{lst:count:Signal-Theft Limit Counter Value-Migration Functions}'s
-     function \co{flush_local_count_sig()}, why are there
-     \co{READ_ONCE()} and \co{WRITE_ONCE()} wrappers around
-     the uses of the
-     \co{theft} per-thread variable?
+     doesn't \co{flush_local_count_sig()} need stronger memory barriers?
 }\QuickQuizAnswer{
-     \begin{fcvref}[ln:count:count_lim_sig:migration:flush_sig]
-     The first one (on \clnref{check:REQ}) can be argued to be unnecessary.
-     The last two (\clnref{set:ACK,set:READY}) are important.
-     If these are removed, the compiler would be within its rights
-     to rewrite \clnrefrange{set:ACK}{set:READY} as follows:
-     \end{fcvref}
-
-\begin{VerbatimN}[firstnumber=14]
-theft = THEFT_READY;
-if (counting) {
-     theft = THEFT_ACK;
-}
-\end{VerbatimN}
-
-     This would be fatal, as the slowpath might see the transient
-     value of \co{THEFT_READY}, and start stealing before the
-     corresponding thread was ready.
+     No, that \co{smp_store_release()} suffices because this code
+     communicates only with \co{flush_local_count()}, and there is
+     no need for store-to-load ordering.
 }\QuickQuizEnd
 

\begin{fcvref}[ln:count:count_lim_sig:migration:flush]

Reviewed-by on v1 1/1
#7 lore raw
In reply to Alan Huang

Hi Alan,

Maybe I was not clear enough, but there are minor inconsistencies in PDF rendering. Please see below.

Quoted 54 lines · Alan Huang

In count_lim_sig.c, there is only one ordering required, that is writing to counter happens before setting theft to THEFT_READY in add_count/sub_count's fast path. Therefore, partial memory barrier will suffice.

---
 CodeSamples/count/count_lim_sig.c | 10 +++-------
 count/count.tex                   | 12 ++++++------
 2 files changed, 9 insertions(+), 13 deletions(-)
CodeSamples/count/count_lim_sig.c +3 -7
index 59da8077..c2f61197 100644
@@ -56,12 +56,10 @@ static void flush_local_count_sig(int unused)	//\lnlbl{flush_sig:b}
 {
 	if (READ_ONCE(theft) != THEFT_REQ)	//\lnlbl{flush_sig:check:REQ}
 		return;				//\lnlbl{flush_sig:return:n}
-	smp_mb();				//\lnlbl{flush_sig:mb:1}
 	WRITE_ONCE(theft, THEFT_ACK);		//\lnlbl{flush_sig:set:ACK}
 	if (!counting) {			//\lnlbl{flush_sig:check:fast}
-		WRITE_ONCE(theft, THEFT_READY);	//\lnlbl{flush_sig:set:READY}
+		smp_store_release(&theft, THEFT_READY);	//\lnlbl{flush_sig:set:READY}
 	}
-	smp_mb();
 }						//\lnlbl{flush_sig:e}
 
 static void flush_local_count(void)			//\lnlbl{flush:b}
@@ -125,8 +123,7 @@ int add_count(unsigned long delta)			//\lnlbl{b}
 	WRITE_ONCE(counting, 0);			//\lnlbl{clearcnt}
 	barrier();					//\lnlbl{barrier:3}
 	if (READ_ONCE(theft) == THEFT_ACK) {		//\lnlbl{check:ACK}
-		smp_mb();				//\lnlbl{mb}
-		WRITE_ONCE(theft, THEFT_READY);		//\lnlbl{READY}
+		smp_store_release(&theft, THEFT_READY);		//\lnlbl{READY}
 	}
 	if (fastpath)
 		return 1;				//\lnlbl{return:fs}
@@ -164,8 +161,7 @@ int sub_count(unsigned long delta)
 	WRITE_ONCE(counting, 0);
 	barrier();
 	if (READ_ONCE(theft) == THEFT_ACK) {
-		smp_mb();
-		WRITE_ONCE(theft, THEFT_READY);
+		smp_store_release(&theft, THEFT_READY);
 	}
 	if (fastpath)
 		return 1;
count/count.tex +0 -0
index 8ab67e2e..4c139d63 100644

count/count.tex handler used in the theft process. · line 2431

Quoted 10 lines
count/count.tex +1 -3
@@ -2425,12 +2425,11 @@ handler used in the theft process.
 \Clnref{check:REQ,return:n} check to see if
 the \co{theft} state is REQ, and, if not
 returns without change.
-\Clnref{mb:1} executes a \IX{memory barrier} to ensure that the sampling
-of the theft variable happens before any change to that variable.
 \Clnref{set:ACK} sets the \co{theft} state to ACK, and, if
 \clnref{check:fast} sees that
 this thread's fastpaths are not running, \clnref{set:READY} sets the \co{theft}
-state to READY\@.
+state to READY, with the release store ensuring any change to counter in
                                                                 \co{counter}

count/count.tex handler used in the theft process. · line 2432

Quoted 1 line
count/count.tex +1 -0
+the fastpath happens before the change of theft to READY\@.
                                             \co{theft}

count/count.tex handlers to undertake theft. · line 2597

Quoted 11 lines
count/count.tex +1 -3
 \end{fcvref}
 
 \begin{listing}
@@ -2595,9 +2594,10 @@ handlers to undertake theft.
 \Clnref{barrier:3} again disables compiler reordering, and then
 \clnref{check:ACK}
 checks to see if the signal handler deferred the \co{theft}
-state-change to READY, and, if so, \clnref{mb} executes a memory
-barrier to ensure that any CPU that sees \clnref{READY} setting state to
-READY also sees the effects of \clnref{add:f}.
+state-change to READY, and, if so, \clnref{READY} changes theft to
                                                             \co{theft}

count/count.tex handlers to undertake theft. · line 2603

Quoted 6 lines
count/count.tex +3 -0
+READY with the release store ensuring that
+any CPU that sees the READY state also sees the effects
+of \clnref{add:f}.
 If the fastpath addition at \clnref{add:f} was executed, then
 \clnref{return:fs} returns
 success.

With those addressed,

Thanks, Akira

author
#8 lore raw

Hi Akira,

Thanks for pointing that out, I have sent patch v4.

Thanks,
Alan

Quoted 102 lines

2023年4月8日 下午12:25,Akira Yokosawa <[email protected]> 写道:

Hi Alan,

Maybe I was not clear enough, but there are minor inconsistencies in PDF rendering. Please see below.

Quoted 62 lines · Alan Huang

In count_lim_sig.c, there is only one ordering required, that is writing to counter happens before setting theft to THEFT_READY in add_count/sub_count's fast path. Therefore, partial memory barrier will suffice.

---
CodeSamples/count/count_lim_sig.c | 10 +++-------
count/count.tex | 12 ++++++------

2 files changed, 9 insertions(+), 13 deletions(-)
CodeSamples/count/count_lim_sig.c +3 -7
index 59da8077..c2f61197 100644
@@ -56,12 +56,10 @@ static void flush_local_count_sig(int unused)	//\lnlbl{flush_sig:b}
 {
 	if (READ_ONCE(theft) != THEFT_REQ)	//\lnlbl{flush_sig:check:REQ}
 		return;				//\lnlbl{flush_sig:return:n}
-	smp_mb();				//\lnlbl{flush_sig:mb:1}
 	WRITE_ONCE(theft, THEFT_ACK);		//\lnlbl{flush_sig:set:ACK}
 	if (!counting) {			//\lnlbl{flush_sig:check:fast}
-		WRITE_ONCE(theft, THEFT_READY);	//\lnlbl{flush_sig:set:READY}
+		smp_store_release(&theft, THEFT_READY);	//\lnlbl{flush_sig:set:READY}
 	}
-	smp_mb();
 }						//\lnlbl{flush_sig:e}
 
 static void flush_local_count(void)			//\lnlbl{flush:b}
@@ -125,8 +123,7 @@ int add_count(unsigned long delta)			//\lnlbl{b}
 	WRITE_ONCE(counting, 0);			//\lnlbl{clearcnt}
 	barrier();					//\lnlbl{barrier:3}
 	if (READ_ONCE(theft) == THEFT_ACK) {		//\lnlbl{check:ACK}
-		smp_mb();				//\lnlbl{mb}
-		WRITE_ONCE(theft, THEFT_READY);		//\lnlbl{READY}
+		smp_store_release(&theft, THEFT_READY);		//\lnlbl{READY}
 	}
 	if (fastpath)
 		return 1;				//\lnlbl{return:fs}
@@ -164,8 +161,7 @@ int sub_count(unsigned long delta)
 	WRITE_ONCE(counting, 0);
 	barrier();
 	if (READ_ONCE(theft) == THEFT_ACK) {
-		smp_mb();
-		WRITE_ONCE(theft, THEFT_READY);
+		smp_store_release(&theft, THEFT_READY);
 	}
 	if (fastpath)
 		return 1;
count/count.tex +0 -0
index 8ab67e2e..4c139d63 100644
@@ -2425,12 +2425,11 @@ handler used in the theft process.

\Clnref{check:REQ,return:n} check to see if
the \co{theft} state is REQ, and, if not
returns without change.
-\Clnref{mb:1} executes a \IX{memory barrier} to ensure that the sampling -of the theft variable happens before any change to that variable. \Clnref{set:ACK} sets the \co{theft} state to ACK, and, if \clnref{check:fast} sees that
this thread's fastpaths are not running, \clnref{set:READY} sets the \co{theft}

count/count.tex handler used in the theft process. · line 2431

Quoted 2 lines
count/count.tex +1 -1
-state to READY\@.
+state to READY, with the release store ensuring any change to counter in
                                                                \co{counter}
Quoted 1 line

+the fastpath happens before the change of theft to READY\@.

                                            \co{theft}

count/count.tex handlers to undertake theft. · line 2597

Quoted 11 lines

\end{fcvref}

\begin{listing}

count/count.tex +0 -0
@@ -2595,9 +2594,10 @@ handlers to undertake theft.

\Clnref{barrier:3} again disables compiler reordering, and then \clnref{check:ACK}
checks to see if the signal handler deferred the \co{theft}

count/count.tex +1 -3
-state-change to READY, and, if so, \clnref{mb} executes a memory
-barrier to ensure that any CPU that sees \clnref{READY} setting state to
-READY also sees the effects of \clnref{add:f}.
+state-change to READY, and, if so, \clnref{READY} changes theft to
                                                            \co{theft}
Quoted 6 lines
count/count.tex +3 -0
+READY with the release store ensuring that
+any CPU that sees the READY state also sees the effects
+of \clnref{add:f}.

If the fastpath addition at \clnref{add:f} was executed, then \clnref{return:fs} returns
success.

With those addressed,

Reviewed-by Akira Yokosawa <[email protected] <mailto:[email protected]>>

Thanks, Akira

author
#9 lore raw
In reply to Akira Yokosawa

Hi Akira,

Thanks for pointing that out, I have sent patch v4.

Thanks,
Alan

Quoted 102 lines

2023年4月8日 下午12:25,Akira Yokosawa <[email protected]> 写道:

Hi Alan,

Maybe I was not clear enough, but there are minor inconsistencies in PDF rendering. Please see below.

Quoted 62 lines · Alan Huang

In count_lim_sig.c, there is only one ordering required, that is writing to counter happens before setting theft to THEFT_READY in add_count/sub_count's fast path. Therefore, partial memory barrier will suffice.

---
CodeSamples/count/count_lim_sig.c | 10 +++-------
count/count.tex | 12 ++++++------

2 files changed, 9 insertions(+), 13 deletions(-)
CodeSamples/count/count_lim_sig.c +3 -7
index 59da8077..c2f61197 100644
@@ -56,12 +56,10 @@ static void flush_local_count_sig(int unused)	//\lnlbl{flush_sig:b}
 {
 	if (READ_ONCE(theft) != THEFT_REQ)	//\lnlbl{flush_sig:check:REQ}
 		return;				//\lnlbl{flush_sig:return:n}
-	smp_mb();				//\lnlbl{flush_sig:mb:1}
 	WRITE_ONCE(theft, THEFT_ACK);		//\lnlbl{flush_sig:set:ACK}
 	if (!counting) {			//\lnlbl{flush_sig:check:fast}
-		WRITE_ONCE(theft, THEFT_READY);	//\lnlbl{flush_sig:set:READY}
+		smp_store_release(&theft, THEFT_READY);	//\lnlbl{flush_sig:set:READY}
 	}
-	smp_mb();
 }						//\lnlbl{flush_sig:e}
 
 static void flush_local_count(void)			//\lnlbl{flush:b}
@@ -125,8 +123,7 @@ int add_count(unsigned long delta)			//\lnlbl{b}
 	WRITE_ONCE(counting, 0);			//\lnlbl{clearcnt}
 	barrier();					//\lnlbl{barrier:3}
 	if (READ_ONCE(theft) == THEFT_ACK) {		//\lnlbl{check:ACK}
-		smp_mb();				//\lnlbl{mb}
-		WRITE_ONCE(theft, THEFT_READY);		//\lnlbl{READY}
+		smp_store_release(&theft, THEFT_READY);		//\lnlbl{READY}
 	}
 	if (fastpath)
 		return 1;				//\lnlbl{return:fs}
@@ -164,8 +161,7 @@ int sub_count(unsigned long delta)
 	WRITE_ONCE(counting, 0);
 	barrier();
 	if (READ_ONCE(theft) == THEFT_ACK) {
-		smp_mb();
-		WRITE_ONCE(theft, THEFT_READY);
+		smp_store_release(&theft, THEFT_READY);
 	}
 	if (fastpath)
 		return 1;
count/count.tex +0 -0
index 8ab67e2e..4c139d63 100644
@@ -2425,12 +2425,11 @@ handler used in the theft process.

\Clnref{check:REQ,return:n} check to see if
the \co{theft} state is REQ, and, if not
returns without change.
-\Clnref{mb:1} executes a \IX{memory barrier} to ensure that the sampling -of the theft variable happens before any change to that variable. \Clnref{set:ACK} sets the \co{theft} state to ACK, and, if \clnref{check:fast} sees that
this thread's fastpaths are not running, \clnref{set:READY} sets the \co{theft}

count/count.tex handler used in the theft process. · line 2431

Quoted 2 lines
count/count.tex +1 -1
-state to READY\@.
+state to READY, with the release store ensuring any change to counter in
                                                                \co{counter}
Quoted 1 line

+the fastpath happens before the change of theft to READY\@.

                                            \co{theft}

count/count.tex handlers to undertake theft. · line 2597

Quoted 11 lines

\end{fcvref}

\begin{listing}

count/count.tex +0 -0
@@ -2595,9 +2594,10 @@ handlers to undertake theft.

\Clnref{barrier:3} again disables compiler reordering, and then \clnref{check:ACK}
checks to see if the signal handler deferred the \co{theft}

count/count.tex +1 -3
-state-change to READY, and, if so, \clnref{mb} executes a memory
-barrier to ensure that any CPU that sees \clnref{READY} setting state to
-READY also sees the effects of \clnref{add:f}.
+state-change to READY, and, if so, \clnref{READY} changes theft to
                                                            \co{theft}
Quoted 6 lines
count/count.tex +3 -0
+READY with the release store ensuring that
+any CPU that sees the READY state also sees the effects
+of \clnref{add:f}.

If the fastpath addition at \clnref{add:f} was executed, then \clnref{return:fs} returns
success.

With those addressed,

Thanks, Akira