Skip to content

Commit 6f4cec2

Browse files
oleg-nesterovpaulmckrcu
authored andcommitted
rcu: Eliminate lockless accesses to rcu_sync->gp_count
The rcu_sync structure's ->gp_count field is always accessed under the protection of that same structure's ->rss_lock field, with the exception of a pair of WARN_ON_ONCE() calls just prior to acquiring that lock in functions rcu_sync_exit() and rcu_sync_dtor(). These lockless accesses are unnecessary and impair KCSAN's ability to catch bugs that might be inserted via other lockless accesses. This commit therefore moves those WARN_ON_ONCE() calls under the lock. Signed-off-by: Oleg Nesterov <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 7f09e70 commit 6f4cec2

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

kernel/rcu/sync.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void rcu_sync_enter(struct rcu_sync *rsp)
122122
* we are called at early boot time but this shouldn't happen.
123123
*/
124124
}
125-
WRITE_ONCE(rsp->gp_count, rsp->gp_count + 1);
125+
rsp->gp_count++;
126126
spin_unlock_irq(&rsp->rss_lock);
127127

128128
if (gp_state == GP_IDLE) {
@@ -151,15 +151,11 @@ void rcu_sync_enter(struct rcu_sync *rsp)
151151
*/
152152
void rcu_sync_exit(struct rcu_sync *rsp)
153153
{
154-
int gpc;
155-
156154
WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_IDLE);
157-
WARN_ON_ONCE(READ_ONCE(rsp->gp_count) == 0);
158155

159156
spin_lock_irq(&rsp->rss_lock);
160-
gpc = rsp->gp_count - 1;
161-
WRITE_ONCE(rsp->gp_count, gpc);
162-
if (!gpc) {
157+
WARN_ON_ONCE(rsp->gp_count == 0);
158+
if (!--rsp->gp_count) {
163159
if (rsp->gp_state == GP_PASSED) {
164160
WRITE_ONCE(rsp->gp_state, GP_EXIT);
165161
rcu_sync_call(rsp);
@@ -178,10 +174,10 @@ void rcu_sync_dtor(struct rcu_sync *rsp)
178174
{
179175
int gp_state;
180176

181-
WARN_ON_ONCE(READ_ONCE(rsp->gp_count));
182177
WARN_ON_ONCE(READ_ONCE(rsp->gp_state) == GP_PASSED);
183178

184179
spin_lock_irq(&rsp->rss_lock);
180+
WARN_ON_ONCE(rsp->gp_count);
185181
if (rsp->gp_state == GP_REPLAY)
186182
WRITE_ONCE(rsp->gp_state, GP_EXIT);
187183
gp_state = rsp->gp_state;

0 commit comments

Comments
 (0)