lockdep-design.txt 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 5 separate state bits:
  23. - 'ever held in hardirq context' [ == hardirq-safe ]
  24. - 'ever held in softirq context' [ == softirq-safe ]
  25. - 'ever held with hardirqs enabled' [ == hardirq-unsafe ]
  26. - 'ever held with softirqs and hardirqs enabled' [ == softirq-unsafe ]
  27. - 'ever used' [ == !unused ]
  28. When locking rules are violated, these 4 state bits are presented in the
  29. locking error messages, inside curlies. A contrived example:
  30. modprobe/2287 is trying to acquire lock:
  31. (&sio_locks[i].lock){--..}, at: [<c02867fd>] mutex_lock+0x21/0x24
  32. but task is already holding lock:
  33. (&sio_locks[i].lock){--..}, at: [<c02867fd>] mutex_lock+0x21/0x24
  34. The bit position indicates hardirq, softirq, hardirq-read,
  35. softirq-read respectively, and the character displayed in each
  36. indicates:
  37. '.' acquired while irqs disabled
  38. '+' acquired in irq context
  39. '-' acquired with irqs enabled
  40. '?' read acquired in irq context with irqs enabled.
  41. Unused mutexes cannot be part of the cause of an error.
  42. Single-lock state rules:
  43. ------------------------
  44. A softirq-unsafe lock-class is automatically hardirq-unsafe as well. The
  45. following states are exclusive, and only one of them is allowed to be
  46. set for any lock-class:
  47. <hardirq-safe> and <hardirq-unsafe>
  48. <softirq-safe> and <softirq-unsafe>
  49. The validator detects and reports lock usage that violate these
  50. single-lock state rules.
  51. Multi-lock dependency rules:
  52. ----------------------------
  53. The same lock-class must not be acquired twice, because this could lead
  54. to lock recursion deadlocks.
  55. Furthermore, two locks may not be taken in different order:
  56. <L1> -> <L2>
  57. <L2> -> <L1>
  58. because this could lead to lock inversion deadlocks. (The validator
  59. finds such dependencies in arbitrary complexity, i.e. there can be any
  60. other locking sequence between the acquire-lock operations, the
  61. validator will still track all dependencies between locks.)
  62. Furthermore, the following usage based lock dependencies are not allowed
  63. between any two lock-classes:
  64. <hardirq-safe> -> <hardirq-unsafe>
  65. <softirq-safe> -> <softirq-unsafe>
  66. The first rule comes from the fact the a hardirq-safe lock could be
  67. taken by a hardirq context, interrupting a hardirq-unsafe lock - and
  68. thus could result in a lock inversion deadlock. Likewise, a softirq-safe
  69. lock could be taken by an softirq context, interrupting a softirq-unsafe
  70. lock.
  71. The above rules are enforced for any locking sequence that occurs in the
  72. kernel: when acquiring a new lock, the validator checks whether there is
  73. any rule violation between the new lock and any of the held locks.
  74. When a lock-class changes its state, the following aspects of the above
  75. dependency rules are enforced:
  76. - if a new hardirq-safe lock is discovered, we check whether it
  77. took any hardirq-unsafe lock in the past.
  78. - if a new softirq-safe lock is discovered, we check whether it took
  79. any softirq-unsafe lock in the past.
  80. - if a new hardirq-unsafe lock is discovered, we check whether any
  81. hardirq-safe lock took it in the past.
  82. - if a new softirq-unsafe lock is discovered, we check whether any
  83. softirq-safe lock took it in the past.
  84. (Again, we do these checks too on the basis that an interrupt context
  85. could interrupt _any_ of the irq-unsafe or hardirq-unsafe locks, which
  86. could lead to a lock inversion deadlock - even if that lock scenario did
  87. not trigger in practice yet.)
  88. Exception: Nested data dependencies leading to nested locking
  89. -------------------------------------------------------------
  90. There are a few cases where the Linux kernel acquires more than one
  91. instance of the same lock-class. Such cases typically happen when there
  92. is some sort of hierarchy within objects of the same type. In these
  93. cases there is an inherent "natural" ordering between the two objects
  94. (defined by the properties of the hierarchy), and the kernel grabs the
  95. locks in this fixed order on each of the objects.
  96. An example of such an object hierarchy that results in "nested locking"
  97. is that of a "whole disk" block-dev object and a "partition" block-dev
  98. object; the partition is "part of" the whole device and as long as one
  99. always takes the whole disk lock as a higher lock than the partition
  100. lock, the lock ordering is fully correct. The validator does not
  101. automatically detect this natural ordering, as the locking rule behind
  102. the ordering is not static.
  103. In order to teach the validator about this correct usage model, new
  104. versions of the various locking primitives were added that allow you to
  105. specify a "nesting level". An example call, for the block device mutex,
  106. looks like this:
  107. enum bdev_bd_mutex_lock_class
  108. {
  109. BD_MUTEX_NORMAL,
  110. BD_MUTEX_WHOLE,
  111. BD_MUTEX_PARTITION
  112. };
  113. mutex_lock_nested(&bdev->bd_contains->bd_mutex, BD_MUTEX_PARTITION);
  114. In this case the locking is done on a bdev object that is known to be a
  115. partition.
  116. The validator treats a lock that is taken in such a nested fashion as a
  117. separate (sub)class for the purposes of validation.
  118. Note: When changing code to use the _nested() primitives, be careful and
  119. check really thoroughly that the hierarchy is correctly mapped; otherwise
  120. you can get false positives or false negatives.
  121. Proof of 100% correctness:
  122. --------------------------
  123. The validator achieves perfect, mathematical 'closure' (proof of locking
  124. correctness) in the sense that for every simple, standalone single-task
  125. locking sequence that occurred at least once during the lifetime of the
  126. kernel, the validator proves it with a 100% certainty that no
  127. combination and timing of these locking sequences can cause any class of
  128. lock related deadlock. [*]
  129. I.e. complex multi-CPU and multi-task locking scenarios do not have to
  130. occur in practice to prove a deadlock: only the simple 'component'
  131. locking chains have to occur at least once (anytime, in any
  132. task/context) for the validator to be able to prove correctness. (For
  133. example, complex deadlocks that would normally need more than 3 CPUs and
  134. a very unlikely constellation of tasks, irq-contexts and timings to
  135. occur, can be detected on a plain, lightly loaded single-CPU system as
  136. well!)
  137. This radically decreases the complexity of locking related QA of the
  138. kernel: what has to be done during QA is to trigger as many "simple"
  139. single-task locking dependencies in the kernel as possible, at least
  140. once, to prove locking correctness - instead of having to trigger every
  141. possible combination of locking interaction between CPUs, combined with
  142. every possible hardirq and softirq nesting scenario (which is impossible
  143. to do in practice).
  144. [*] assuming that the validator itself is 100% correct, and no other
  145. part of the system corrupts the state of the validator in any way.
  146. We also assume that all NMI/SMM paths [which could interrupt
  147. even hardirq-disabled codepaths] are correct and do not interfere
  148. with the validator. We also assume that the 64-bit 'chain hash'
  149. value is unique for every lock-chain in the system. Also, lock
  150. recursion must not be higher than 20.
  151. Performance:
  152. ------------
  153. The above rules require _massive_ amounts of runtime checking. If we did
  154. that for every lock taken and for every irqs-enable event, it would
  155. render the system practically unusably slow. The complexity of checking
  156. is O(N^2), so even with just a few hundred lock-classes we'd have to do
  157. tens of thousands of checks for every event.
  158. This problem is solved by checking any given 'locking scenario' (unique
  159. sequence of locks taken after each other) only once. A simple stack of
  160. held locks is maintained, and a lightweight 64-bit hash value is
  161. calculated, which hash is unique for every lock chain. The hash value,
  162. when the chain is validated for the first time, is then put into a hash
  163. table, which hash-table can be checked in a lockfree manner. If the
  164. locking chain occurs again later on, the hash table tells us that we
  165. dont have to validate the chain again.