Skip to contentSkip to conversation

CodeSample/count: Update the places where READ_ONCE is used

PATCH Applied Alan Huang 2 participants 2 messages May 30, 2023 – Jun 2, 2023

lore
Share & tools
lore

Alan Huang

author
#0 lore raw

PATCH 1/1 CodeSample/count: Update the places where READ_ONCE is used

There is no concurrent updates to 'total' by other threads, plain access is enough. But there might be threads updating 'counter', READ_ONCE is required.

---
 CodeSamples/count/count_end_rcu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
CodeSamples/count/count_end_rcu.c +2 -2
index 8fc70611..71bd5269 100644
@@ -48,10 +48,10 @@ unsigned long read_count(void)			//\lnlbl{read:b}
 
 	rcu_read_lock();			//\lnlbl{read:rrl}
 	cap = rcu_dereference(countarrayp);	//\lnlbl{read:deref}
-	sum = READ_ONCE(cap->total);		//\lnlbl{read:init}
+	sum = cap->total;		//\lnlbl{read:init}
 	for_each_thread(t) {			//\lnlbl{read:add:b}
 		ctrp = READ_ONCE(cap->counterp[t]);
-		if (ctrp != NULL) sum += *ctrp;	//\lnlbl{read:add:e}
+		if (ctrp != NULL) sum += READ_ONCE(*ctrp);	//\lnlbl{read:add:e}
 	}
 	rcu_read_unlock();			//\lnlbl{read:rru}
 	return sum;				//\lnlbl{read:ret}
Signature · 1 line
2.34.1
#1 lore raw
Quoted 5 lines · Alan Huang

There is no concurrent updates to 'total' by other threads, plain access is enough. But there might be threads updating 'counter', READ_ONCE is required.

Queued and pushed, thank you!

Thanx, Paul

Quoted 24 lines
---
 CodeSamples/count/count_end_rcu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
CodeSamples/count/count_end_rcu.c +2 -2
index 8fc70611..71bd5269 100644
@@ -48,10 +48,10 @@ unsigned long read_count(void)			//\lnlbl{read:b}
 
 	rcu_read_lock();			//\lnlbl{read:rrl}
 	cap = rcu_dereference(countarrayp);	//\lnlbl{read:deref}
-	sum = READ_ONCE(cap->total);		//\lnlbl{read:init}
+	sum = cap->total;		//\lnlbl{read:init}
 	for_each_thread(t) {			//\lnlbl{read:add:b}
 		ctrp = READ_ONCE(cap->counterp[t]);
-		if (ctrp != NULL) sum += *ctrp;	//\lnlbl{read:add:e}
+		if (ctrp != NULL) sum += READ_ONCE(*ctrp);	//\lnlbl{read:add:e}
 	}
 	rcu_read_unlock();			//\lnlbl{read:rru}
 	return sum;				//\lnlbl{read:ret}
Signature · 1 line
2.34.1