• Mikulas Patocka's avatar
    blockdev: turn a rw semaphore into a percpu rw semaphore · 62ac665f
    Mikulas Patocka authored
    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: default avatarMikulas Patocka <mpatocka@redhat.com>
    Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
    62ac665f
block_dev.c 44.3 KB