lglock.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. lglock - local/global locks for mostly local access patterns
  2. ------------------------------------------------------------
  3. Origin: Nick Piggin's VFS scalability series introduced during
  4. 2.6.35++ [1] [2]
  5. Location: kernel/locking/lglock.c
  6. include/linux/lglock.h
  7. Users: currently only the VFS and stop_machine related code
  8. Design Goal:
  9. ------------
  10. Improve scalability of globally used large data sets that are
  11. distributed over all CPUs as per_cpu elements.
  12. To manage global data structures that are partitioned over all CPUs
  13. as per_cpu elements but can be mostly handled by CPU local actions
  14. lglock will be used where the majority of accesses are cpu local
  15. reading and occasional cpu local writing with very infrequent
  16. global write access.
  17. * deal with things locally whenever possible
  18. - very fast access to the local per_cpu data
  19. - reasonably fast access to specific per_cpu data on a different
  20. CPU
  21. * while making global action possible when needed
  22. - by expensive access to all CPUs locks - effectively
  23. resulting in a globally visible critical section.
  24. Design:
  25. -------
  26. Basically it is an array of per_cpu spinlocks with the
  27. lg_local_lock/unlock accessing the local CPUs lock object and the
  28. lg_local_lock_cpu/unlock_cpu accessing a remote CPUs lock object
  29. the lg_local_lock has to disable preemption as migration protection so
  30. that the reference to the local CPUs lock does not go out of scope.
  31. Due to the lg_local_lock/unlock only touching cpu-local resources it
  32. is fast. Taking the local lock on a different CPU will be more
  33. expensive but still relatively cheap.
  34. One can relax the migration constraints by acquiring the current
  35. CPUs lock with lg_local_lock_cpu, remember the cpu, and release that
  36. lock at the end of the critical section even if migrated. This should
  37. give most of the performance benefits without inhibiting migration
  38. though needs careful considerations for nesting of lglocks and
  39. consideration of deadlocks with lg_global_lock.
  40. The lg_global_lock/unlock locks all underlying spinlocks of all
  41. possible CPUs (including those off-line). The preemption disable/enable
  42. are needed in the non-RT kernels to prevent deadlocks like:
  43. on cpu 1
  44. task A task B
  45. lg_global_lock
  46. got cpu 0 lock
  47. <<<< preempt <<<<
  48. lg_local_lock_cpu for cpu 0
  49. spin on cpu 0 lock
  50. On -RT this deadlock scenario is resolved by the arch_spin_locks in the
  51. lglocks being replaced by rt_mutexes which resolve the above deadlock
  52. by boosting the lock-holder.
  53. Implementation:
  54. ---------------
  55. The initial lglock implementation from Nick Piggin used some complex
  56. macros to generate the lglock/brlock in lglock.h - they were later
  57. turned into a set of functions by Andi Kleen [7]. The change to functions
  58. was motivated by the presence of multiple lock users and also by them
  59. being easier to maintain than the generating macros. This change to
  60. functions is also the basis to eliminated the restriction of not
  61. being initializeable in kernel modules (the remaining problem is that
  62. locks are not explicitly initialized - see lockdep-design.txt)
  63. Declaration and initialization:
  64. -------------------------------
  65. #include <linux/lglock.h>
  66. DEFINE_LGLOCK(name)
  67. or:
  68. DEFINE_STATIC_LGLOCK(name);
  69. lg_lock_init(&name, "lockdep_name_string");
  70. on UP this is mapped to DEFINE_SPINLOCK(name) in both cases, note
  71. also that as of 3.18-rc6 all declaration in use are of the _STATIC_
  72. variant (and it seems that the non-static was never in use).
  73. lg_lock_init is initializing the lockdep map only.
  74. Usage:
  75. ------
  76. From the locking semantics it is a spinlock. It could be called a
  77. locality aware spinlock. lg_local_* behaves like a per_cpu
  78. spinlock and lg_global_* like a global spinlock.
  79. No surprises in the API.
  80. lg_local_lock(*lglock);
  81. access to protected per_cpu object on this CPU
  82. lg_local_unlock(*lglock);
  83. lg_local_lock_cpu(*lglock, cpu);
  84. access to protected per_cpu object on other CPU cpu
  85. lg_local_unlock_cpu(*lglock, cpu);
  86. lg_global_lock(*lglock);
  87. access all protected per_cpu objects on all CPUs
  88. lg_global_unlock(*lglock);
  89. There are no _trylock variants of the lglocks.
  90. Note that the lg_global_lock/unlock has to iterate over all possible
  91. CPUs rather than the actually present CPUs or a CPU could go off-line
  92. with a held lock [4] and that makes it very expensive. A discussion on
  93. these issues can be found at [5]
  94. Constraints:
  95. ------------
  96. * currently the declaration of lglocks in kernel modules is not
  97. possible, though this should be doable with little change.
  98. * lglocks are not recursive.
  99. * suitable for code that can do most operations on the CPU local
  100. data and will very rarely need the global lock
  101. * lg_global_lock/unlock is *very* expensive and does not scale
  102. * on UP systems all lg_* primitives are simply spinlocks
  103. * in PREEMPT_RT the spinlock becomes an rt-mutex and can sleep but
  104. does not change the tasks state while sleeping [6].
  105. * in PREEMPT_RT the preempt_disable/enable in lg_local_lock/unlock
  106. is downgraded to a migrate_disable/enable, the other
  107. preempt_disable/enable are downgraded to barriers [6].
  108. The deadlock noted for non-RT above is resolved due to rt_mutexes
  109. boosting the lock-holder in this case which arch_spin_locks do
  110. not do.
  111. lglocks were designed for very specific problems in the VFS and probably
  112. only are the right answer in these corner cases. Any new user that looks
  113. at lglocks probably wants to look at the seqlock and RCU alternatives as
  114. her first choice. There are also efforts to resolve the RCU issues that
  115. currently prevent using RCU in place of view remaining lglocks.
  116. Note on brlock history:
  117. -----------------------
  118. The 'Big Reader' read-write spinlocks were originally introduced by
  119. Ingo Molnar in 2000 (2.4/2.5 kernel series) and removed in 2003. They
  120. later were introduced by the VFS scalability patch set in 2.6 series
  121. again as the "big reader lock" brlock [2] variant of lglock which has
  122. been replaced by seqlock primitives or by RCU based primitives in the
  123. 3.13 kernel series as was suggested in [3] in 2003. The brlock was
  124. entirely removed in the 3.13 kernel series.
  125. Link: 1 http://lkml.org/lkml/2010/8/2/81
  126. Link: 2 http://lwn.net/Articles/401738/
  127. Link: 3 http://lkml.org/lkml/2003/3/9/205
  128. Link: 4 https://lkml.org/lkml/2011/8/24/185
  129. Link: 5 http://lkml.org/lkml/2011/12/18/189
  130. Link: 6 https://www.kernel.org/pub/linux/kernel/projects/rt/
  131. patch series - lglocks-rt.patch.patch
  132. Link: 7 http://lkml.org/lkml/2012/3/5/26