• Waiman Long's avatar
    locking/rwsem: Enable time-based spinning on reader-owned rwsem · 7d43f1ce
    Waiman Long authored
    When the rwsem is owned by reader, writers stop optimistic spinning
    simply because there is no easy way to figure out if all the readers
    are actively running or not. However, there are scenarios where
    the readers are unlikely to sleep and optimistic spinning can help
    performance.
    
    This patch provides a simple mechanism for spinning on a reader-owned
    rwsem by a writer. It is a time threshold based spinning where the
    allowable spinning time can vary from 10us to 25us depending on the
    condition of the rwsem.
    
    When the time threshold is exceeded, the nonspinnable bits will be set
    in the owner field to indicate that no more optimistic spinning will
    be allowed on this rwsem until it becomes writer owned again. Not even
    readers is allowed to acquire the reader-locked rwsem by optimistic
    spinning for fairness.
    
    We also want a writer to acquire the lock after the readers hold the
    lock for a relatively long time. In order to give preference to writers
    under such a circumstance, the single RWSEM_NONSPINNABLE bit is now split
    into two - one for reader and one for writer. When optimistic spinning
    is disabled, both bits will be set. When the reader count drop down
    to 0, the writer nonspinnable bit will be cleared to allow writers to
    spin on the lock, but not the readers. When a writer acquires the lock,
    it will write its own task structure pointer into sem->owner and clear
    the reader nonspinnable bit in the process.
    
    The time taken for each iteration of the reader-owned rwsem spinning
    loop varies. Below are sample minimum elapsed times for 16 iterations
    of the loop.
    
          System                 Time for 16 Iterations
          ------                 ----------------------
      1-socket Skylake                  ~800ns
      4-socket Broadwell                ~300ns
      2-socket ThunderX2 (arm64)        ~250ns
    
    When the lock cacheline is contended, we can see up to almost 10X
    increase in elapsed time.  So 25us will be at most 500, 1300 and 1600
    iterations for each of the above systems.
    
    With a locking microbenchmark running on 5.1 based kernel, the total
    locking rates (in kops/s) on a 8-socket IvyBridge-EX system with
    equal numbers of readers and writers before and after this patch were
    as follows:
    
       # of Threads  Pre-patch    Post-patch
       ------------  ---------    ----------
            2          1,759        6,684
            4          1,684        6,738
            8          1,074        7,222
           16            900        7,163
           32            458        7,316
           64            208          520
          128            168          425
          240            143          474
    
    This patch gives a big boost in performance for mixed reader/writer
    workloads.
    
    With 32 locking threads, the rwsem lock event data were:
    
    rwsem_opt_fail=79850
    rwsem_opt_nospin=5069
    rwsem_opt_rlock=597484
    rwsem_opt_wlock=957339
    rwsem_sleep_reader=57782
    rwsem_sleep_writer=55663
    
    With 64 locking threads, the data looked like:
    
    rwsem_opt_fail=346723
    rwsem_opt_nospin=6293
    rwsem_opt_rlock=1127119
    rwsem_opt_wlock=1400628
    rwsem_sleep_reader=308201
    rwsem_sleep_writer=72281
    
    So a lot more threads acquired the lock in the slowpath and more threads
    went to sleep.
    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-15-longman@redhat.comSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
    7d43f1ce
rwsem.c 39.9 KB