aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/percpu-rw-semaphore.txt
diff options
context:
space:
mode:
authorMikulas Patocka <mpatocka@redhat.com>2012-09-26 01:46:43 -0400
committerJens Axboe <axboe@kernel.dk>2012-09-26 01:46:43 -0400
commit62ac665ff9fc07497ca524bd20d6a96893d11071 (patch)
treedfd697e488fde4b46f1cb2ebfb380bb881115827 /Documentation/percpu-rw-semaphore.txt
parentb87570f5d349661814b262dd5fc40787700f80d6 (diff)
blockdev: turn a rw semaphore into a percpu rw semaphore
This avoids cache line bouncing when many processes lock the semaphore for read. New percpu lock implementation The lock consists of an array of percpu unsigned integers, a boolean variable and a mutex. When we take the lock for read, we enter rcu read section, check for a "locked" variable. If it is false, we increase a percpu counter on the current cpu and exit the rcu section. If "locked" is true, we exit the rcu section, take the mutex and drop it (this waits until a writer finished) and retry. Unlocking for read just decreases percpu variable. Note that we can unlock on a difference cpu than where we locked, in this case the counter underflows. The sum of all percpu counters represents the number of processes that hold the lock for read. When we need to lock for write, we take the mutex, set "locked" variable to true and synchronize rcu. Since RCU has been synchronized, no processes can create new read locks. We wait until the sum of percpu counters is zero - when it is, there are no readers in the critical section. Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'Documentation/percpu-rw-semaphore.txt')
-rw-r--r--Documentation/percpu-rw-semaphore.txt27
1 files changed, 27 insertions, 0 deletions
diff --git a/Documentation/percpu-rw-semaphore.txt b/Documentation/percpu-rw-semaphore.txt
new file mode 100644
index 000000000000..eddd77094725
--- /dev/null
+++ b/Documentation/percpu-rw-semaphore.txt
@@ -0,0 +1,27 @@
1Percpu rw semaphores
2--------------------
3
4Percpu rw semaphores is a new read-write semaphore design that is
5optimized for locking for reading.
6
7The problem with traditional read-write semaphores is that when multiple
8cores take the lock for reading, the cache line containing the semaphore
9is bouncing between L1 caches of the cores, causing performance
10degradation.
11
12Locking for reading it very fast, it uses RCU and it avoids any atomic
13instruction in the lock and unlock path. On the other hand, locking for
14writing is very expensive, it calls synchronize_rcu() that can take
15hundreds of microseconds.
16
17The lock is declared with "struct percpu_rw_semaphore" type.
18The lock is initialized percpu_init_rwsem, it returns 0 on success and
19-ENOMEM on allocation failure.
20The lock must be freed with percpu_free_rwsem to avoid memory leak.
21
22The lock is locked for read with percpu_down_read, percpu_up_read and
23for write with percpu_down_write, percpu_up_write.
24
25The idea of using RCU for optimized rw-lock was introduced by
26Eric Dumazet <eric.dumazet@gmail.com>.
27The code was written by Mikulas Patocka <mpatocka@redhat.com>