Skip to contentSkip to conversation

CodeSample/count: Use plain access instead of WRITE_ONCE

PATCH In review Alan Huang 2 participants 4 messages May 28, 2023 – May 30, 2023

lore
Share & tools
lore

Alan Huang

author
#0 lore raw

PATCH 1/2 CodeSample/count: Use plain access instead of WRITE_ONCE

The cap here has not been published, plain access is enough.

---
 CodeSamples/count/count_end_rcu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
CodeSamples/count/count_end_rcu.c +1 -1
index 1171f0b6..8fc70611 100644
@@ -89,7 +89,7 @@ void count_unregister_thread(int nthreadsexpected)	//\lnlbl{unreg:b}
 	}						//\lnlbl{unreg:alloc:e}
 	spin_lock(&final_mutex);			//\lnlbl{unreg:acq}
 	*cap = *countarrayp;				//\lnlbl{unreg:copy}
-	WRITE_ONCE(cap->total, cap->total + counter);	//\lnlbl{unreg:add}
+	cap->total += counter;	//\lnlbl{unreg:add}
 	cap->counterp[idx] = NULL;			//\lnlbl{unreg:null}
 	capold = countarrayp;				//\lnlbl{unreg:retain}
 	rcu_assign_pointer(countarrayp, cap);		//\lnlbl{unreg:assign}
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/2 together: Remove wrong content

In count_unregister_thread, both adding the counter to total and NULLs the exiting thread's counterp[] array element happen before publishing the newly allocated cap. Therefore, count_unregister_thread() can not result in the outgoing thread's work to be double counted.

---
 together/applyrcu.tex | 9 ---------
 1 file changed, 9 deletions(-)
together/applyrcu.tex +0 -9
index 1e69fe3e..09a265f7 100644
@@ -151,15 +151,6 @@ is invoked by each newly created thread.
 	So, interestingly enough, when adding a new thread, this
 	implementation gets the effect of allocating a new structure,
 	but without actually having to do the allocation.
-
-	\begin{fcvref}[ln:count:count_end_rcu:whole:unreg]
-	On the other hand, \co{count_unregister_thread()} can result
-	in the outgoing thread's work being double counted.
-	This can happen when \co{read_count()} is invoked between
-	\clnref{add,null}.
-	There are efficient ways of avoiding this double-counting, but
-	these are left as an exercise for the reader.
-	\end{fcvref}
 }\QuickQuizEnd
 
 \begin{fcvref}[ln:count:count_end_rcu:whole:unreg]
Signature · 1 line
2.34.1
#2 lore raw
In reply to Alan Huang
Quoted 3 lines · Alan Huang

The cap here has not been published, plain access is enough.

Good catch, thank you!

Does line 27 of that same listing need a READ_ONCE(*ctrp)?

Thanx, Paul

Quoted 20 lines
---
 CodeSamples/count/count_end_rcu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
CodeSamples/count/count_end_rcu.c +1 -1
index 1171f0b6..8fc70611 100644
@@ -89,7 +89,7 @@ void count_unregister_thread(int nthreadsexpected)	//\lnlbl{unreg:b}
 	}						//\lnlbl{unreg:alloc:e}
 	spin_lock(&final_mutex);			//\lnlbl{unreg:acq}
 	*cap = *countarrayp;				//\lnlbl{unreg:copy}
-	WRITE_ONCE(cap->total, cap->total + counter);	//\lnlbl{unreg:add}
+	cap->total += counter;	//\lnlbl{unreg:add}
 	cap->counterp[idx] = NULL;			//\lnlbl{unreg:null}
 	capold = countarrayp;				//\lnlbl{unreg:retain}
 	rcu_assign_pointer(countarrayp, cap);		//\lnlbl{unreg:assign}
Signature · 1 line
2.34.1
author
#3 lore raw
Quoted 11 lines

2023年5月30日 08:30,Paul E. McKenney <[email protected]> 写道:

Quoted 3 lines · Alan Huang

The cap here has not been published, plain access is enough.

Good catch, thank you!

Does line 27 of that same listing need a READ_ONCE(*ctrp)?

It does need! I have sent another patch.

Thanks,
Alan

Quoted 23 lines

Thanx, Paul

Quoted 20 lines

---
CodeSamples/count/count_end_rcu.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
CodeSamples/count/count_end_rcu.c +1 -1
index 1171f0b6..8fc70611 100644
@@ -89,7 +89,7 @@ void count_unregister_thread(int nthreadsexpected)	//\lnlbl{unreg:b}
 	}						//\lnlbl{unreg:alloc:e}
 	spin_lock(&final_mutex);			//\lnlbl{unreg:acq}
 	*cap = *countarrayp;				//\lnlbl{unreg:copy}
-	WRITE_ONCE(cap->total, cap->total + counter);	//\lnlbl{unreg:add}
+	cap->total += counter;	//\lnlbl{unreg:add}
 	cap->counterp[idx] = NULL;			//\lnlbl{unreg:null}
 	capold = countarrayp;				//\lnlbl{unreg:retain}
 	rcu_assign_pointer(countarrayp, cap);		//\lnlbl{unreg:assign}
Signature · 1 line
2.34.1