Skip to contentSkip to conversation

rcu: Remove unnecessary READ_ONCE in rcu_preempt_read_enter/exit

PATCH In review Alan Huang 2 participants 2 messages Mar 31, 2024 – Apr 9, 2024 RCU

lore
Share & tools
lore

Alan Huang

author
#0 lore raw

PATCH 1/1 rcu: Remove unnecessary READ_ONCE in rcu_preempt_read_enter/exit

The two READ_ONCEs here imply that there might be updater concurrently modifies rcu_read_lock_nesting, although it's true there could be interrupt handlers enter rcu RSCS, but rcu_read_lock_nesting should remain unchanged after they finished. That is, READ_ONCE and then WRITE_ONCE the same variable is not a valid pattern, this patch thus removes the two READ_ONCEs.

---
 kernel/rcu/tree_plugin.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/rcu/tree_plugin.h +2 -2
index 340bbefe5f65..c90edbb7fa40 100644
@@ -376,12 +376,12 @@ static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
 
 static void rcu_preempt_read_enter(void)
 {
-	WRITE_ONCE(current->rcu_read_lock_nesting, READ_ONCE(current->rcu_read_lock_nesting) + 1);
+	WRITE_ONCE(current->rcu_read_lock_nesting, current->rcu_read_lock_nesting + 1);
 }
 
 static int rcu_preempt_read_exit(void)
 {
-	int ret = READ_ONCE(current->rcu_read_lock_nesting) - 1;
+	int ret = current->rcu_read_lock_nesting - 1;
 
 	WRITE_ONCE(current->rcu_read_lock_nesting, ret);
 	return ret;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 6 lines · Alan Huang

The two READ_ONCEs here imply that there might be updater concurrently modifies rcu_read_lock_nesting, although it's true there could be interrupt handlers enter rcu RSCS, but rcu_read_lock_nesting should remain unchanged after they finished. That is, READ_ONCE and then WRITE_ONCE the same variable is not a valid pattern, this patch thus removes the two READ_ONCEs.

This is true, but I will take the READ_ONCE() calls over the KCSAN complaints. So I must avoid taking this one.

Please note that RCU uses strict KCSAN configuration [1].

Thanx, Paul

[1] https://docs.google.com/document/d/1FwZaXSg3A55ivVoWffA9iMuhJ3_Gmj_E494dLYjjyLQ/edit?usp=sharing

Quoted 28 lines
---
 kernel/rcu/tree_plugin.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/rcu/tree_plugin.h +2 -2
index 340bbefe5f65..c90edbb7fa40 100644
@@ -376,12 +376,12 @@ static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
 
 static void rcu_preempt_read_enter(void)
 {
-	WRITE_ONCE(current->rcu_read_lock_nesting, READ_ONCE(current->rcu_read_lock_nesting) + 1);
+	WRITE_ONCE(current->rcu_read_lock_nesting, current->rcu_read_lock_nesting + 1);
 }
 
 static int rcu_preempt_read_exit(void)
 {
-	int ret = READ_ONCE(current->rcu_read_lock_nesting) - 1;
+	int ret = current->rcu_read_lock_nesting - 1;
 
 	WRITE_ONCE(current->rcu_read_lock_nesting, ret);
 	return ret;
Signature · 1 line
2.34.1