• Waiman Long's avatar
    locking/rwsem: Implement a new locking scheme · 64489e78
    Waiman Long authored
    The current way of using various reader, writer and waiting biases
    in the rwsem code are confusing and hard to understand. I have to
    reread the rwsem count guide in the rwsem-xadd.c file from time to
    time to remind myself how this whole thing works. It also makes the
    rwsem code harder to be optimized.
    
    To make rwsem more sane, a new locking scheme similar to the one in
    qrwlock is now being used.  The atomic long count has the following
    bit definitions:
    
      Bit  0   - writer locked bit
      Bit  1   - waiters present bit
      Bits 2-7 - reserved for future extension
      Bits 8-X - reader count (24/56 bits)
    
    The cmpxchg instruction is now used to acquire the write lock. The read
    lock is still acquired with xadd instruction, so there is no change here.
    This scheme will allow up to 16M/64P active readers which should be
    more than enough. We can always use some more reserved bits if necessary.
    
    With that change, we can deterministically know if a rwsem has been
    write-locked. Looking at the count alone, however, one cannot determine
    for certain if a rwsem is owned by readers or not as the readers that
    set the reader count bits may be in the process of backing out. So we
    still need the reader-owned bit in the owner field to be sure.
    
    With a locking microbenchmark running on 5.1 based kernel, the total
    locking rates (in kops/s) of the benchmark on a 8-socket 120-core
    IvyBridge-EX system before and after the patch were as follows:
    
                      Before Patch      After Patch
       # of Threads  wlock    rlock    wlock    rlock
       ------------  -----    -----    -----    -----
            1        30,659   31,341   31,055   31,283
            2         8,909   16,457    9,884   17,659
            4         9,028   15,823    8,933   20,233
            8         8,410   14,212    7,230   17,140
           16         8,217   25,240    7,479   24,607
    
    The locking rates of the benchmark on a Power8 system were as follows:
    
                      Before Patch      After Patch
       # of Threads  wlock    rlock    wlock    rlock
       ------------  -----    -----    -----    -----
            1        12,963   13,647   13,275   13,601
            2         7,570   11,569    7,902   10,829
            4         5,232    5,516    5,466    5,435
            8         5,233    3,386    5,467    3,168
    
    The locking rates of the benchmark on a 2-socket ARM64 system were
    as follows:
    
                      Before Patch      After Patch
       # of Threads  wlock    rlock    wlock    rlock
       ------------  -----    -----    -----    -----
            1        21,495   21,046   21,524   21,074
            2         5,293   10,502    5,333   10,504
            4         5,325   11,463    5,358   11,631
            8         5,391   11,712    5,470   11,680
    
    The performance are roughly the same before and after the patch. There
    are run-to-run variations in performance. Runs with higher variances
    usually have higher throughput.
    Signed-off-by: default avatarWaiman Long <longman@redhat.com>
    Signed-off-by: default avatarPeter Zijlstra (Intel) <peterz@infradead.org>
    Cc: Borislav Petkov <bp@alien8.de>
    Cc: Davidlohr Bueso <dave@stgolabs.net>
    Cc: H. Peter Anvin <hpa@zytor.com>
    Cc: Linus Torvalds <torvalds@linux-foundation.org>
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: Thomas Gleixner <tglx@linutronix.de>
    Cc: Tim Chen <tim.c.chen@linux.intel.com>
    Cc: Will Deacon <will.deacon@arm.com>
    Cc: huang ying <huang.ying.caritas@gmail.com>
    Link: https://lkml.kernel.org/r/20190520205918.22251-4-longman@redhat.comSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
    64489e78
rwsem-xadd.c 16.8 KB