lockdep-design.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. Runtime locking correctness validator
  2. =====================================
  3. started by Ingo Molnar <mingo@redhat.com>
  4. additions by Arjan van de Ven <arjan@linux.intel.com>
  5. Lock-class
  6. ----------
  7. The basic object the validator operates upon is a 'class' of locks.
  8. A class of locks is a group of locks that are logically the same with
  9. respect to locking rules, even if the locks may have multiple (possibly
  10. tens of thousands of) instantiations. For example a lock in the inode
  11. struct is one class, while each inode has its own instantiation of that
  12. lock class.
  13. The validator tracks the 'state' of lock-classes, and it tracks
  14. dependencies between different lock-classes. The validator maintains a
  15. rolling proof that the state and the dependencies are correct.
  16. Unlike an lock instantiation, the lock-class itself never goes away: when
  17. a lock-class is used for the first time after bootup it gets registered,
  18. and all subsequent uses of that lock-class will be attached to this
  19. lock-class.
  20. State
  21. -----
  22. The validator tracks lock-class usage history into 4n + 1 separate state bits:
  23. - 'ever held in STATE context'
  24. - 'ever held as readlock in STATE context'
  25. - 'ever held with STATE enabled'
  26. - 'ever held as readlock with STATE enabled'
  27. Where STATE can be either one of (kernel/locking/lockdep_states.h)
  28. - hardirq
  29. - softirq
  30. - reclaim_fs
  31. - 'ever used' [ == !unused ]
  32. When locking rules are violated, these state bits are presented in the
  33. locking error messages, inside curlies. A contrived example:
  34. modprobe/2287 is trying to acquire lock:
  35. (&sio_locks[i].lock){-.-...}, at: [<c02867fd>] mutex_lock+0x21/0x24
  36. but task is already holding lock:
  37. (&sio_locks[i].lock){-.-...}, at: [<c02867fd>] mutex_lock+0x21/0x24
  38. The bit position indicates STATE, STATE-read, for each of the states listed
  39. above, and the character displayed in each indicates:
  40. '.' acquired while irqs disabled and not in irq context
  41. '-' acquired in irq context
  42. '+' acquired with irqs enabled
  43. '?' acquired in irq context with irqs enabled.
  44. Unused mutexes cannot be part of the cause of an error.
  45. Single-lock state rules:
  46. ------------------------
  47. A softirq-unsafe lock-class is automatically hardirq-unsafe as well. The
  48. following states are exclusive, and only one of them is allowed to be
  49. set for any lock-class:
  50. <hardirq-safe> and <hardirq-unsafe>
  51. <softirq-safe> and <softirq-unsafe>
  52. The validator detects and reports lock usage that violate these
  53. single-lock state rules.
  54. Multi-lock dependency rules:
  55. ----------------------------
  56. The same lock-class must not be acquired twice, because this could lead
  57. to lock recursion deadlocks.
  58. Furthermore, two locks may not be taken in different order:
  59. <L1> -> <L2>
  60. <L2> -> <L1>
  61. because this could lead to lock inversion deadlocks. (The validator
  62. finds such dependencies in arbitrary complexity, i.e. there can be any
  63. other locking sequence between the acquire-lock operations, the
  64. validator will still track all dependencies between locks.)
  65. Furthermore, the following usage based lock dependencies are not allowed
  66. between any two lock-classes:
  67. <hardirq-safe> -> <hardirq-unsafe>
  68. <softirq-safe> -> <softirq-unsafe>
  69. The first rule comes from the fact the a hardirq-safe lock could be
  70. taken by a hardirq context, interrupting a hardirq-unsafe lock - and
  71. thus could result in a lock inversion deadlock. Likewise, a softirq-safe
  72. lock could be taken by an softirq context, interrupting a softirq-unsafe
  73. lock.
  74. The above rules are enforced for any locking sequence that occurs in the
  75. kernel: when acquiring a new lock, the validator checks whether there is
  76. any rule violation between the new lock and any of the held locks.
  77. When a lock-class changes its state, the following aspects of the above
  78. dependency rules are enforced:
  79. - if a new hardirq-safe lock is discovered, we check whether it
  80. took any hardirq-unsafe lock in the past.
  81. - if a new softirq-safe lock is discovered, we check whether it took
  82. any softirq-unsafe lock in the past.
  83. - if a new hardirq-unsafe lock is discovered, we check whether any
  84. hardirq-safe lock took it in the past.
  85. - if a new softirq-unsafe lock is discovered, we check whether any
  86. softirq-safe lock took it in the past.
  87. (Again, we do these checks too on the basis that an interrupt context
  88. could interrupt _any_ of the irq-unsafe or hardirq-unsafe locks, which
  89. could lead to a lock inversion deadlock - even if that lock scenario did
  90. not trigger in practice yet.)
  91. Exception: Nested data dependencies leading to nested locking
  92. -------------------------------------------------------------
  93. There are a few cases where the Linux kernel acquires more than one
  94. instance of the same lock-class. Such cases typically happen when there
  95. is some sort of hierarchy within objects of the same type. In these
  96. cases there is an inherent "natural" ordering between the two objects
  97. (defined by the properties of the hierarchy), and the kernel grabs the
  98. locks in this fixed order on each of the objects.
  99. An example of such an object hierarchy that results in "nested locking"
  100. is that of a "whole disk" block-dev object and a "partition" block-dev
  101. object; the partition is "part of" the whole device and as long as one
  102. always takes the whole disk lock as a higher lock than the partition
  103. lock, the lock ordering is fully correct. The validator does not
  104. automatically detect this natural ordering, as the locking rule behind
  105. the ordering is not static.
  106. In order to teach the validator about this correct usage model, new
  107. versions of the various locking primitives were added that allow you to
  108. specify a "nesting level". An example call, for the block device mutex,
  109. looks like this:
  110. enum bdev_bd_mutex_lock_class
  111. {
  112. BD_MUTEX_NORMAL,
  113. BD_MUTEX_WHOLE,
  114. BD_MUTEX_PARTITION
  115. };
  116. mutex_lock_nested(&bdev->bd_contains->bd_mutex, BD_MUTEX_PARTITION);
  117. In this case the locking is done on a bdev object that is known to be a
  118. partition.
  119. The validator treats a lock that is taken in such a nested fashion as a
  120. separate (sub)class for the purposes of validation.
  121. Note: When changing code to use the _nested() primitives, be careful and
  122. check really thoroughly that the hierarchy is correctly mapped; otherwise
  123. you can get false positives or false negatives.
  124. Proof of 100% correctness:
  125. --------------------------
  126. The validator achieves perfect, mathematical 'closure' (proof of locking
  127. correctness) in the sense that for every simple, standalone single-task
  128. locking sequence that occurred at least once during the lifetime of the
  129. kernel, the validator proves it with a 100% certainty that no
  130. combination and timing of these locking sequences can cause any class of
  131. lock related deadlock. [*]
  132. I.e. complex multi-CPU and multi-task locking scenarios do not have to
  133. occur in practice to prove a deadlock: only the simple 'component'
  134. locking chains have to occur at least once (anytime, in any
  135. task/context) for the validator to be able to prove correctness. (For
  136. example, complex deadlocks that would normally need more than 3 CPUs and
  137. a very unlikely constellation of tasks, irq-contexts and timings to
  138. occur, can be detected on a plain, lightly loaded single-CPU system as
  139. well!)
  140. This radically decreases the complexity of locking related QA of the
  141. kernel: what has to be done during QA is to trigger as many "simple"
  142. single-task locking dependencies in the kernel as possible, at least
  143. once, to prove locking correctness - instead of having to trigger every
  144. possible combination of locking interaction between CPUs, combined with
  145. every possible hardirq and softirq nesting scenario (which is impossible
  146. to do in practice).
  147. [*] assuming that the validator itself is 100% correct, and no other
  148. part of the system corrupts the state of the validator in any way.
  149. We also assume that all NMI/SMM paths [which could interrupt
  150. even hardirq-disabled codepaths] are correct and do not interfere
  151. with the validator. We also assume that the 64-bit 'chain hash'
  152. value is unique for every lock-chain in the system. Also, lock
  153. recursion must not be higher than 20.
  154. Performance:
  155. ------------
  156. The above rules require _massive_ amounts of runtime checking. If we did
  157. that for every lock taken and for every irqs-enable event, it would
  158. render the system practically unusably slow. The complexity of checking
  159. is O(N^2), so even with just a few hundred lock-classes we'd have to do
  160. tens of thousands of checks for every event.
  161. This problem is solved by checking any given 'locking scenario' (unique
  162. sequence of locks taken after each other) only once. A simple stack of
  163. held locks is maintained, and a lightweight 64-bit hash value is
  164. calculated, which hash is unique for every lock chain. The hash value,
  165. when the chain is validated for the first time, is then put into a hash
  166. table, which hash-table can be checked in a lockfree manner. If the
  167. locking chain occurs again later on, the hash table tells us that we
  168. dont have to validate the chain again.
  169. Troubleshooting:
  170. ----------------
  171. The validator tracks a maximum of MAX_LOCKDEP_KEYS number of lock classes.
  172. Exceeding this number will trigger the following lockdep warning:
  173. (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
  174. By default, MAX_LOCKDEP_KEYS is currently set to 8191, and typical
  175. desktop systems have less than 1,000 lock classes, so this warning
  176. normally results from lock-class leakage or failure to properly
  177. initialize locks. These two problems are illustrated below:
  178. 1. Repeated module loading and unloading while running the validator
  179. will result in lock-class leakage. The issue here is that each
  180. load of the module will create a new set of lock classes for
  181. that module's locks, but module unloading does not remove old
  182. classes (see below discussion of reuse of lock classes for why).
  183. Therefore, if that module is loaded and unloaded repeatedly,
  184. the number of lock classes will eventually reach the maximum.
  185. 2. Using structures such as arrays that have large numbers of
  186. locks that are not explicitly initialized. For example,
  187. a hash table with 8192 buckets where each bucket has its own
  188. spinlock_t will consume 8192 lock classes -unless- each spinlock
  189. is explicitly initialized at runtime, for example, using the
  190. run-time spin_lock_init() as opposed to compile-time initializers
  191. such as __SPIN_LOCK_UNLOCKED(). Failure to properly initialize
  192. the per-bucket spinlocks would guarantee lock-class overflow.
  193. In contrast, a loop that called spin_lock_init() on each lock
  194. would place all 8192 locks into a single lock class.
  195. The moral of this story is that you should always explicitly
  196. initialize your locks.
  197. One might argue that the validator should be modified to allow
  198. lock classes to be reused. However, if you are tempted to make this
  199. argument, first review the code and think through the changes that would
  200. be required, keeping in mind that the lock classes to be removed are
  201. likely to be linked into the lock-dependency graph. This turns out to
  202. be harder to do than to say.
  203. Of course, if you do run out of lock classes, the next thing to do is
  204. to find the offending lock classes. First, the following command gives
  205. you the number of lock classes currently in use along with the maximum:
  206. grep "lock-classes" /proc/lockdep_stats
  207. This command produces the following output on a modest system:
  208. lock-classes: 748 [max: 8191]
  209. If the number allocated (748 above) increases continually over time,
  210. then there is likely a leak. The following command can be used to
  211. identify the leaking lock classes:
  212. grep "BD" /proc/lockdep
  213. Run the command and save the output, then compare against the output from
  214. a later run of this command to identify the leakers. This same output
  215. can also help you find situations where runtime lock initialization has
  216. been omitted.