Skip to content

Commit 001dac6

Browse files
oleg-nesterovpaulmck
authored andcommitted
locking/percpu-rwsem: Make use of the rcu_sync infrastructure
Currently down_write/up_write calls synchronize_sched_expedited() twice, which is evil. Change this code to rely on rcu-sync primitives. This avoids the _expedited "big hammer", and this can be faster in the contended case or even in the case when a single thread does down_write/up_write in a loop. Of course, a single down_write() will take more time, but otoh it will be much more friendly to the whole system. To simplify the review this patch doesn't update the comments, fixed by the next change. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Reviewed-by: Josh Triplett <josh@joshtriplett.org>
1 parent 95b19f6 commit 001dac6

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

‎include/linux/percpu-rwsem.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
#include <linux/rwsem.h>
66
#include <linux/percpu.h>
77
#include <linux/wait.h>
8+
#include <linux/rcu_sync.h>
89
#include <linux/lockdep.h>
910

1011
struct percpu_rw_semaphore {
12+
struct rcu_sync rss;
1113
unsigned int __percpu *fast_read_ctr;
12-
atomic_t write_ctr;
1314
struct rw_semaphore rw_sem;
1415
atomic_t slow_read_ctr;
1516
wait_queue_head_t write_waitq;

‎kernel/locking/percpu-rwsem.c‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int __percpu_init_rwsem(struct percpu_rw_semaphore *brw,
1717

1818
/* ->rw_sem represents the whole percpu_rw_semaphore for lockdep */
1919
__init_rwsem(&brw->rw_sem, name, rwsem_key);
20-
atomic_set(&brw->write_ctr, 0);
20+
rcu_sync_init(&brw->rss, RCU_SCHED_SYNC);
2121
atomic_set(&brw->slow_read_ctr, 0);
2222
init_waitqueue_head(&brw->write_waitq);
2323
return 0;
@@ -33,6 +33,7 @@ void percpu_free_rwsem(struct percpu_rw_semaphore *brw)
3333
if (!brw->fast_read_ctr)
3434
return;
3535

36+
rcu_sync_dtor(&brw->rss);
3637
free_percpu(brw->fast_read_ctr);
3738
brw->fast_read_ctr = NULL; /* catch use after free bugs */
3839
}
@@ -62,13 +63,12 @@ void percpu_free_rwsem(struct percpu_rw_semaphore *brw)
6263
*/
6364
static bool update_fast_ctr(struct percpu_rw_semaphore *brw, unsigned int val)
6465
{
65-
bool success = false;
66+
bool success;
6667

6768
preempt_disable();
68-
if (likely(!atomic_read(&brw->write_ctr))) {
69+
success = rcu_sync_is_idle(&brw->rss);
70+
if (likely(success))
6971
__this_cpu_add(*brw->fast_read_ctr, val);
70-
success = true;
71-
}
7272
preempt_enable();
7373

7474
return success;
@@ -149,8 +149,6 @@ static int clear_fast_ctr(struct percpu_rw_semaphore *brw)
149149
*/
150150
void percpu_down_write(struct percpu_rw_semaphore *brw)
151151
{
152-
/* tell update_fast_ctr() there is a pending writer */
153-
atomic_inc(&brw->write_ctr);
154152
/*
155153
* 1. Ensures that write_ctr != 0 is visible to any down_read/up_read
156154
* so that update_fast_ctr() can't succeed.
@@ -162,7 +160,7 @@ void percpu_down_write(struct percpu_rw_semaphore *brw)
162160
* fast-path, it executes a full memory barrier before we return.
163161
* See R_W case in the comment above update_fast_ctr().
164162
*/
165-
synchronize_sched_expedited();
163+
rcu_sync_enter(&brw->rss);
166164

167165
/* exclude other writers, and block the new readers completely */
168166
down_write(&brw->rw_sem);
@@ -183,8 +181,6 @@ void percpu_up_write(struct percpu_rw_semaphore *brw)
183181
* Insert the barrier before the next fast-path in down_read,
184182
* see W_R case in the comment above update_fast_ctr().
185183
*/
186-
synchronize_sched_expedited();
187-
/* the last writer unblocks update_fast_ctr() */
188-
atomic_dec(&brw->write_ctr);
184+
rcu_sync_exit(&brw->rss);
189185
}
190186
EXPORT_SYMBOL_GPL(percpu_up_write);

0 commit comments

Comments
 (0)