diff options
author | Mikulas Patocka <mpatocka@redhat.com> | 2012-09-26 01:46:43 -0400 |
---|---|---|
committer | Jens Axboe <axboe@kernel.dk> | 2012-09-26 01:46:43 -0400 |
commit | 62ac665ff9fc07497ca524bd20d6a96893d11071 (patch) | |
tree | dfd697e488fde4b46f1cb2ebfb380bb881115827 /Documentation/percpu-rw-semaphore.txt | |
parent | b87570f5d349661814b262dd5fc40787700f80d6 (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.txt | 27 |
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 @@ | |||
1 | Percpu rw semaphores | ||
2 | -------------------- | ||
3 | |||
4 | Percpu rw semaphores is a new read-write semaphore design that is | ||
5 | optimized for locking for reading. | ||
6 | |||
7 | The problem with traditional read-write semaphores is that when multiple | ||
8 | cores take the lock for reading, the cache line containing the semaphore | ||
9 | is bouncing between L1 caches of the cores, causing performance | ||
10 | degradation. | ||
11 | |||
12 | Locking for reading it very fast, it uses RCU and it avoids any atomic | ||
13 | instruction in the lock and unlock path. On the other hand, locking for | ||
14 | writing is very expensive, it calls synchronize_rcu() that can take | ||
15 | hundreds of microseconds. | ||
16 | |||
17 | The lock is declared with "struct percpu_rw_semaphore" type. | ||
18 | The lock is initialized percpu_init_rwsem, it returns 0 on success and | ||
19 | -ENOMEM on allocation failure. | ||
20 | The lock must be freed with percpu_free_rwsem to avoid memory leak. | ||
21 | |||
22 | The lock is locked for read with percpu_down_read, percpu_up_read and | ||
23 | for write with percpu_down_write, percpu_up_write. | ||
24 | |||
25 | The idea of using RCU for optimized rw-lock was introduced by | ||
26 | Eric Dumazet <eric.dumazet@gmail.com>. | ||
27 | The code was written by Mikulas Patocka <mpatocka@redhat.com> | ||