crossrelease.txt 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. Crossrelease
  2. ============
  3. Started by Byungchul Park <byungchul.park@lge.com>
  4. Contents:
  5. (*) Background
  6. - What causes deadlock
  7. - How lockdep works
  8. (*) Limitation
  9. - Limit lockdep
  10. - Pros from the limitation
  11. - Cons from the limitation
  12. - Relax the limitation
  13. (*) Crossrelease
  14. - Introduce crossrelease
  15. - Introduce commit
  16. (*) Implementation
  17. - Data structures
  18. - How crossrelease works
  19. (*) Optimizations
  20. - Avoid duplication
  21. - Lockless for hot paths
  22. (*) APPENDIX A: What lockdep does to work aggresively
  23. (*) APPENDIX B: How to avoid adding false dependencies
  24. ==========
  25. Background
  26. ==========
  27. What causes deadlock
  28. --------------------
  29. A deadlock occurs when a context is waiting for an event to happen,
  30. which is impossible because another (or the) context who can trigger the
  31. event is also waiting for another (or the) event to happen, which is
  32. also impossible due to the same reason.
  33. For example:
  34. A context going to trigger event C is waiting for event A to happen.
  35. A context going to trigger event A is waiting for event B to happen.
  36. A context going to trigger event B is waiting for event C to happen.
  37. A deadlock occurs when these three wait operations run at the same time,
  38. because event C cannot be triggered if event A does not happen, which in
  39. turn cannot be triggered if event B does not happen, which in turn
  40. cannot be triggered if event C does not happen. After all, no event can
  41. be triggered since any of them never meets its condition to wake up.
  42. A dependency might exist between two waiters and a deadlock might happen
  43. due to an incorrect releationship between dependencies. Thus, we must
  44. define what a dependency is first. A dependency exists between them if:
  45. 1. There are two waiters waiting for each event at a given time.
  46. 2. The only way to wake up each waiter is to trigger its event.
  47. 3. Whether one can be woken up depends on whether the other can.
  48. Each wait in the example creates its dependency like:
  49. Event C depends on event A.
  50. Event A depends on event B.
  51. Event B depends on event C.
  52. NOTE: Precisely speaking, a dependency is one between whether a
  53. waiter for an event can be woken up and whether another waiter for
  54. another event can be woken up. However from now on, we will describe
  55. a dependency as if it's one between an event and another event for
  56. simplicity.
  57. And they form circular dependencies like:
  58. -> C -> A -> B -
  59. / \
  60. \ /
  61. ----------------
  62. where 'A -> B' means that event A depends on event B.
  63. Such circular dependencies lead to a deadlock since no waiter can meet
  64. its condition to wake up as described.
  65. CONCLUSION
  66. Circular dependencies cause a deadlock.
  67. How lockdep works
  68. -----------------
  69. Lockdep tries to detect a deadlock by checking dependencies created by
  70. lock operations, acquire and release. Waiting for a lock corresponds to
  71. waiting for an event, and releasing a lock corresponds to triggering an
  72. event in the previous section.
  73. In short, lockdep does:
  74. 1. Detect a new dependency.
  75. 2. Add the dependency into a global graph.
  76. 3. Check if that makes dependencies circular.
  77. 4. Report a deadlock or its possibility if so.
  78. For example, consider a graph built by lockdep that looks like:
  79. A -> B -
  80. \
  81. -> E
  82. /
  83. C -> D -
  84. where A, B,..., E are different lock classes.
  85. Lockdep will add a dependency into the graph on detection of a new
  86. dependency. For example, it will add a dependency 'E -> C' when a new
  87. dependency between lock E and lock C is detected. Then the graph will be:
  88. A -> B -
  89. \
  90. -> E -
  91. / \
  92. -> C -> D - \
  93. / /
  94. \ /
  95. ------------------
  96. where A, B,..., E are different lock classes.
  97. This graph contains a subgraph which demonstrates circular dependencies:
  98. -> E -
  99. / \
  100. -> C -> D - \
  101. / /
  102. \ /
  103. ------------------
  104. where C, D and E are different lock classes.
  105. This is the condition under which a deadlock might occur. Lockdep
  106. reports it on detection after adding a new dependency. This is the way
  107. how lockdep works.
  108. CONCLUSION
  109. Lockdep detects a deadlock or its possibility by checking if circular
  110. dependencies were created after adding each new dependency.
  111. ==========
  112. Limitation
  113. ==========
  114. Limit lockdep
  115. -------------
  116. Limiting lockdep to work on only typical locks e.g. spin locks and
  117. mutexes, which are released within the acquire context, the
  118. implementation becomes simple but its capacity for detection becomes
  119. limited. Let's check pros and cons in next section.
  120. Pros from the limitation
  121. ------------------------
  122. Given the limitation, when acquiring a lock, locks in a held_locks
  123. cannot be released if the context cannot acquire it so has to wait to
  124. acquire it, which means all waiters for the locks in the held_locks are
  125. stuck. It's an exact case to create dependencies between each lock in
  126. the held_locks and the lock to acquire.
  127. For example:
  128. CONTEXT X
  129. ---------
  130. acquire A
  131. acquire B /* Add a dependency 'A -> B' */
  132. release B
  133. release A
  134. where A and B are different lock classes.
  135. When acquiring lock A, the held_locks of CONTEXT X is empty thus no
  136. dependency is added. But when acquiring lock B, lockdep detects and adds
  137. a new dependency 'A -> B' between lock A in the held_locks and lock B.
  138. They can be simply added whenever acquiring each lock.
  139. And data required by lockdep exists in a local structure, held_locks
  140. embedded in task_struct. Forcing to access the data within the context,
  141. lockdep can avoid racy problems without explicit locks while handling
  142. the local data.
  143. Lastly, lockdep only needs to keep locks currently being held, to build
  144. a dependency graph. However, relaxing the limitation, it needs to keep
  145. even locks already released, because a decision whether they created
  146. dependencies might be long-deferred.
  147. To sum up, we can expect several advantages from the limitation:
  148. 1. Lockdep can easily identify a dependency when acquiring a lock.
  149. 2. Races are avoidable while accessing local locks in a held_locks.
  150. 3. Lockdep only needs to keep locks currently being held.
  151. CONCLUSION
  152. Given the limitation, the implementation becomes simple and efficient.
  153. Cons from the limitation
  154. ------------------------
  155. Given the limitation, lockdep is applicable only to typical locks. For
  156. example, page locks for page access or completions for synchronization
  157. cannot work with lockdep.
  158. Can we detect deadlocks below, under the limitation?
  159. Example 1:
  160. CONTEXT X CONTEXT Y CONTEXT Z
  161. --------- --------- ----------
  162. mutex_lock A
  163. lock_page B
  164. lock_page B
  165. mutex_lock A /* DEADLOCK */
  166. unlock_page B held by X
  167. unlock_page B
  168. mutex_unlock A
  169. mutex_unlock A
  170. where A and B are different lock classes.
  171. No, we cannot.
  172. Example 2:
  173. CONTEXT X CONTEXT Y
  174. --------- ---------
  175. mutex_lock A
  176. mutex_lock A
  177. wait_for_complete B /* DEADLOCK */
  178. complete B
  179. mutex_unlock A
  180. mutex_unlock A
  181. where A is a lock class and B is a completion variable.
  182. No, we cannot.
  183. CONCLUSION
  184. Given the limitation, lockdep cannot detect a deadlock or its
  185. possibility caused by page locks or completions.
  186. Relax the limitation
  187. --------------------
  188. Under the limitation, things to create dependencies are limited to
  189. typical locks. However, synchronization primitives like page locks and
  190. completions, which are allowed to be released in any context, also
  191. create dependencies and can cause a deadlock. So lockdep should track
  192. these locks to do a better job. We have to relax the limitation for
  193. these locks to work with lockdep.
  194. Detecting dependencies is very important for lockdep to work because
  195. adding a dependency means adding an opportunity to check whether it
  196. causes a deadlock. The more lockdep adds dependencies, the more it
  197. thoroughly works. Thus Lockdep has to do its best to detect and add as
  198. many true dependencies into a graph as possible.
  199. For example, considering only typical locks, lockdep builds a graph like:
  200. A -> B -
  201. \
  202. -> E
  203. /
  204. C -> D -
  205. where A, B,..., E are different lock classes.
  206. On the other hand, under the relaxation, additional dependencies might
  207. be created and added. Assuming additional 'FX -> C' and 'E -> GX' are
  208. added thanks to the relaxation, the graph will be:
  209. A -> B -
  210. \
  211. -> E -> GX
  212. /
  213. FX -> C -> D -
  214. where A, B,..., E, FX and GX are different lock classes, and a suffix
  215. 'X' is added on non-typical locks.
  216. The latter graph gives us more chances to check circular dependencies
  217. than the former. However, it might suffer performance degradation since
  218. relaxing the limitation, with which design and implementation of lockdep
  219. can be efficient, might introduce inefficiency inevitably. So lockdep
  220. should provide two options, strong detection and efficient detection.
  221. Choosing efficient detection:
  222. Lockdep works with only locks restricted to be released within the
  223. acquire context. However, lockdep works efficiently.
  224. Choosing strong detection:
  225. Lockdep works with all synchronization primitives. However, lockdep
  226. suffers performance degradation.
  227. CONCLUSION
  228. Relaxing the limitation, lockdep can add additional dependencies giving
  229. additional opportunities to check circular dependencies.
  230. ============
  231. Crossrelease
  232. ============
  233. Introduce crossrelease
  234. ----------------------
  235. In order to allow lockdep to handle additional dependencies by what
  236. might be released in any context, namely 'crosslock', we have to be able
  237. to identify those created by crosslocks. The proposed 'crossrelease'
  238. feature provoides a way to do that.
  239. Crossrelease feature has to do:
  240. 1. Identify dependencies created by crosslocks.
  241. 2. Add the dependencies into a dependency graph.
  242. That's all. Once a meaningful dependency is added into graph, then
  243. lockdep would work with the graph as it did. The most important thing
  244. crossrelease feature has to do is to correctly identify and add true
  245. dependencies into the global graph.
  246. A dependency e.g. 'A -> B' can be identified only in the A's release
  247. context because a decision required to identify the dependency can be
  248. made only in the release context. That is to decide whether A can be
  249. released so that a waiter for A can be woken up. It cannot be made in
  250. other than the A's release context.
  251. It's no matter for typical locks because each acquire context is same as
  252. its release context, thus lockdep can decide whether a lock can be
  253. released in the acquire context. However for crosslocks, lockdep cannot
  254. make the decision in the acquire context but has to wait until the
  255. release context is identified.
  256. Therefore, deadlocks by crosslocks cannot be detected just when it
  257. happens, because those cannot be identified until the crosslocks are
  258. released. However, deadlock possibilities can be detected and it's very
  259. worth. See 'APPENDIX A' section to check why.
  260. CONCLUSION
  261. Using crossrelease feature, lockdep can work with what might be released
  262. in any context, namely crosslock.
  263. Introduce commit
  264. ----------------
  265. Since crossrelease defers the work adding true dependencies of
  266. crosslocks until they are actually released, crossrelease has to queue
  267. all acquisitions which might create dependencies with the crosslocks.
  268. Then it identifies dependencies using the queued data in batches at a
  269. proper time. We call it 'commit'.
  270. There are four types of dependencies:
  271. 1. TT type: 'typical lock A -> typical lock B'
  272. Just when acquiring B, lockdep can see it's in the A's release
  273. context. So the dependency between A and B can be identified
  274. immediately. Commit is unnecessary.
  275. 2. TC type: 'typical lock A -> crosslock BX'
  276. Just when acquiring BX, lockdep can see it's in the A's release
  277. context. So the dependency between A and BX can be identified
  278. immediately. Commit is unnecessary, too.
  279. 3. CT type: 'crosslock AX -> typical lock B'
  280. When acquiring B, lockdep cannot identify the dependency because
  281. there's no way to know if it's in the AX's release context. It has
  282. to wait until the decision can be made. Commit is necessary.
  283. 4. CC type: 'crosslock AX -> crosslock BX'
  284. When acquiring BX, lockdep cannot identify the dependency because
  285. there's no way to know if it's in the AX's release context. It has
  286. to wait until the decision can be made. Commit is necessary.
  287. But, handling CC type is not implemented yet. It's a future work.
  288. Lockdep can work without commit for typical locks, but commit step is
  289. necessary once crosslocks are involved. Introducing commit, lockdep
  290. performs three steps. What lockdep does in each step is:
  291. 1. Acquisition: For typical locks, lockdep does what it originally did
  292. and queues the lock so that CT type dependencies can be checked using
  293. it at the commit step. For crosslocks, it saves data which will be
  294. used at the commit step and increases a reference count for it.
  295. 2. Commit: No action is reauired for typical locks. For crosslocks,
  296. lockdep adds CT type dependencies using the data saved at the
  297. acquisition step.
  298. 3. Release: No changes are required for typical locks. When a crosslock
  299. is released, it decreases a reference count for it.
  300. CONCLUSION
  301. Crossrelease introduces commit step to handle dependencies of crosslocks
  302. in batches at a proper time.
  303. ==============
  304. Implementation
  305. ==============
  306. Data structures
  307. ---------------
  308. Crossrelease introduces two main data structures.
  309. 1. hist_lock
  310. This is an array embedded in task_struct, for keeping lock history so
  311. that dependencies can be added using them at the commit step. Since
  312. it's local data, it can be accessed locklessly in the owner context.
  313. The array is filled at the acquisition step and consumed at the
  314. commit step. And it's managed in circular manner.
  315. 2. cross_lock
  316. One per lockdep_map exists. This is for keeping data of crosslocks
  317. and used at the commit step.
  318. How crossrelease works
  319. ----------------------
  320. It's the key of how crossrelease works, to defer necessary works to an
  321. appropriate point in time and perform in at once at the commit step.
  322. Let's take a look with examples step by step, starting from how lockdep
  323. works without crossrelease for typical locks.
  324. acquire A /* Push A onto held_locks */
  325. acquire B /* Push B onto held_locks and add 'A -> B' */
  326. acquire C /* Push C onto held_locks and add 'B -> C' */
  327. release C /* Pop C from held_locks */
  328. release B /* Pop B from held_locks */
  329. release A /* Pop A from held_locks */
  330. where A, B and C are different lock classes.
  331. NOTE: This document assumes that readers already understand how
  332. lockdep works without crossrelease thus omits details. But there's
  333. one thing to note. Lockdep pretends to pop a lock from held_locks
  334. when releasing it. But it's subtly different from the original pop
  335. operation because lockdep allows other than the top to be poped.
  336. In this case, lockdep adds 'the top of held_locks -> the lock to acquire'
  337. dependency every time acquiring a lock.
  338. After adding 'A -> B', a dependency graph will be:
  339. A -> B
  340. where A and B are different lock classes.
  341. And after adding 'B -> C', the graph will be:
  342. A -> B -> C
  343. where A, B and C are different lock classes.
  344. Let's performs commit step even for typical locks to add dependencies.
  345. Of course, commit step is not necessary for them, however, it would work
  346. well because this is a more general way.
  347. acquire A
  348. /*
  349. * Queue A into hist_locks
  350. *
  351. * In hist_locks: A
  352. * In graph: Empty
  353. */
  354. acquire B
  355. /*
  356. * Queue B into hist_locks
  357. *
  358. * In hist_locks: A, B
  359. * In graph: Empty
  360. */
  361. acquire C
  362. /*
  363. * Queue C into hist_locks
  364. *
  365. * In hist_locks: A, B, C
  366. * In graph: Empty
  367. */
  368. commit C
  369. /*
  370. * Add 'C -> ?'
  371. * Answer the following to decide '?'
  372. * What has been queued since acquire C: Nothing
  373. *
  374. * In hist_locks: A, B, C
  375. * In graph: Empty
  376. */
  377. release C
  378. commit B
  379. /*
  380. * Add 'B -> ?'
  381. * Answer the following to decide '?'
  382. * What has been queued since acquire B: C
  383. *
  384. * In hist_locks: A, B, C
  385. * In graph: 'B -> C'
  386. */
  387. release B
  388. commit A
  389. /*
  390. * Add 'A -> ?'
  391. * Answer the following to decide '?'
  392. * What has been queued since acquire A: B, C
  393. *
  394. * In hist_locks: A, B, C
  395. * In graph: 'B -> C', 'A -> B', 'A -> C'
  396. */
  397. release A
  398. where A, B and C are different lock classes.
  399. In this case, dependencies are added at the commit step as described.
  400. After commits for A, B and C, the graph will be:
  401. A -> B -> C
  402. where A, B and C are different lock classes.
  403. NOTE: A dependency 'A -> C' is optimized out.
  404. We can see the former graph built without commit step is same as the
  405. latter graph built using commit steps. Of course the former way leads to
  406. earlier finish for building the graph, which means we can detect a
  407. deadlock or its possibility sooner. So the former way would be prefered
  408. when possible. But we cannot avoid using the latter way for crosslocks.
  409. Let's look at how commit steps work for crosslocks. In this case, the
  410. commit step is performed only on crosslock AX as real. And it assumes
  411. that the AX release context is different from the AX acquire context.
  412. BX RELEASE CONTEXT BX ACQUIRE CONTEXT
  413. ------------------ ------------------
  414. acquire A
  415. /*
  416. * Push A onto held_locks
  417. * Queue A into hist_locks
  418. *
  419. * In held_locks: A
  420. * In hist_locks: A
  421. * In graph: Empty
  422. */
  423. acquire BX
  424. /*
  425. * Add 'the top of held_locks -> BX'
  426. *
  427. * In held_locks: A
  428. * In hist_locks: A
  429. * In graph: 'A -> BX'
  430. */
  431. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  432. It must be guaranteed that the following operations are seen after
  433. acquiring BX globally. It can be done by things like barrier.
  434. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  435. acquire C
  436. /*
  437. * Push C onto held_locks
  438. * Queue C into hist_locks
  439. *
  440. * In held_locks: C
  441. * In hist_locks: C
  442. * In graph: 'A -> BX'
  443. */
  444. release C
  445. /*
  446. * Pop C from held_locks
  447. *
  448. * In held_locks: Empty
  449. * In hist_locks: C
  450. * In graph: 'A -> BX'
  451. */
  452. acquire D
  453. /*
  454. * Push D onto held_locks
  455. * Queue D into hist_locks
  456. * Add 'the top of held_locks -> D'
  457. *
  458. * In held_locks: A, D
  459. * In hist_locks: A, D
  460. * In graph: 'A -> BX', 'A -> D'
  461. */
  462. acquire E
  463. /*
  464. * Push E onto held_locks
  465. * Queue E into hist_locks
  466. *
  467. * In held_locks: E
  468. * In hist_locks: C, E
  469. * In graph: 'A -> BX', 'A -> D'
  470. */
  471. release E
  472. /*
  473. * Pop E from held_locks
  474. *
  475. * In held_locks: Empty
  476. * In hist_locks: D, E
  477. * In graph: 'A -> BX', 'A -> D'
  478. */
  479. release D
  480. /*
  481. * Pop D from held_locks
  482. *
  483. * In held_locks: A
  484. * In hist_locks: A, D
  485. * In graph: 'A -> BX', 'A -> D'
  486. */
  487. commit BX
  488. /*
  489. * Add 'BX -> ?'
  490. * What has been queued since acquire BX: C, E
  491. *
  492. * In held_locks: Empty
  493. * In hist_locks: D, E
  494. * In graph: 'A -> BX', 'A -> D',
  495. * 'BX -> C', 'BX -> E'
  496. */
  497. release BX
  498. /*
  499. * In held_locks: Empty
  500. * In hist_locks: D, E
  501. * In graph: 'A -> BX', 'A -> D',
  502. * 'BX -> C', 'BX -> E'
  503. */
  504. release A
  505. /*
  506. * Pop A from held_locks
  507. *
  508. * In held_locks: Empty
  509. * In hist_locks: A, D
  510. * In graph: 'A -> BX', 'A -> D',
  511. * 'BX -> C', 'BX -> E'
  512. */
  513. where A, BX, C,..., E are different lock classes, and a suffix 'X' is
  514. added on crosslocks.
  515. Crossrelease considers all acquisitions after acqiuring BX are
  516. candidates which might create dependencies with BX. True dependencies
  517. will be determined when identifying the release context of BX. Meanwhile,
  518. all typical locks are queued so that they can be used at the commit step.
  519. And then two dependencies 'BX -> C' and 'BX -> E' are added at the
  520. commit step when identifying the release context.
  521. The final graph will be, with crossrelease:
  522. -> C
  523. /
  524. -> BX -
  525. / \
  526. A - -> E
  527. \
  528. -> D
  529. where A, BX, C,..., E are different lock classes, and a suffix 'X' is
  530. added on crosslocks.
  531. However, the final graph will be, without crossrelease:
  532. A -> D
  533. where A and D are different lock classes.
  534. The former graph has three more dependencies, 'A -> BX', 'BX -> C' and
  535. 'BX -> E' giving additional opportunities to check if they cause
  536. deadlocks. This way lockdep can detect a deadlock or its possibility
  537. caused by crosslocks.
  538. CONCLUSION
  539. We checked how crossrelease works with several examples.
  540. =============
  541. Optimizations
  542. =============
  543. Avoid duplication
  544. -----------------
  545. Crossrelease feature uses a cache like what lockdep already uses for
  546. dependency chains, but this time it's for caching CT type dependencies.
  547. Once that dependency is cached, the same will never be added again.
  548. Lockless for hot paths
  549. ----------------------
  550. To keep all locks for later use at the commit step, crossrelease adopts
  551. a local array embedded in task_struct, which makes access to the data
  552. lockless by forcing it to happen only within the owner context. It's
  553. like how lockdep handles held_locks. Lockless implmentation is important
  554. since typical locks are very frequently acquired and released.
  555. =================================================
  556. APPENDIX A: What lockdep does to work aggresively
  557. =================================================
  558. A deadlock actually occurs when all wait operations creating circular
  559. dependencies run at the same time. Even though they don't, a potential
  560. deadlock exists if the problematic dependencies exist. Thus it's
  561. meaningful to detect not only an actual deadlock but also its potential
  562. possibility. The latter is rather valuable. When a deadlock occurs
  563. actually, we can identify what happens in the system by some means or
  564. other even without lockdep. However, there's no way to detect possiblity
  565. without lockdep unless the whole code is parsed in head. It's terrible.
  566. Lockdep does the both, and crossrelease only focuses on the latter.
  567. Whether or not a deadlock actually occurs depends on several factors.
  568. For example, what order contexts are switched in is a factor. Assuming
  569. circular dependencies exist, a deadlock would occur when contexts are
  570. switched so that all wait operations creating the dependencies run
  571. simultaneously. Thus to detect a deadlock possibility even in the case
  572. that it has not occured yet, lockdep should consider all possible
  573. combinations of dependencies, trying to:
  574. 1. Use a global dependency graph.
  575. Lockdep combines all dependencies into one global graph and uses them,
  576. regardless of which context generates them or what order contexts are
  577. switched in. Aggregated dependencies are only considered so they are
  578. prone to be circular if a problem exists.
  579. 2. Check dependencies between classes instead of instances.
  580. What actually causes a deadlock are instances of lock. However,
  581. lockdep checks dependencies between classes instead of instances.
  582. This way lockdep can detect a deadlock which has not happened but
  583. might happen in future by others but the same class.
  584. 3. Assume all acquisitions lead to waiting.
  585. Although locks might be acquired without waiting which is essential
  586. to create dependencies, lockdep assumes all acquisitions lead to
  587. waiting since it might be true some time or another.
  588. CONCLUSION
  589. Lockdep detects not only an actual deadlock but also its possibility,
  590. and the latter is more valuable.
  591. ==================================================
  592. APPENDIX B: How to avoid adding false dependencies
  593. ==================================================
  594. Remind what a dependency is. A dependency exists if:
  595. 1. There are two waiters waiting for each event at a given time.
  596. 2. The only way to wake up each waiter is to trigger its event.
  597. 3. Whether one can be woken up depends on whether the other can.
  598. For example:
  599. acquire A
  600. acquire B /* A dependency 'A -> B' exists */
  601. release B
  602. release A
  603. where A and B are different lock classes.
  604. A depedency 'A -> B' exists since:
  605. 1. A waiter for A and a waiter for B might exist when acquiring B.
  606. 2. Only way to wake up each is to release what it waits for.
  607. 3. Whether the waiter for A can be woken up depends on whether the
  608. other can. IOW, TASK X cannot release A if it fails to acquire B.
  609. For another example:
  610. TASK X TASK Y
  611. ------ ------
  612. acquire AX
  613. acquire B /* A dependency 'AX -> B' exists */
  614. release B
  615. release AX held by Y
  616. where AX and B are different lock classes, and a suffix 'X' is added
  617. on crosslocks.
  618. Even in this case involving crosslocks, the same rule can be applied. A
  619. depedency 'AX -> B' exists since:
  620. 1. A waiter for AX and a waiter for B might exist when acquiring B.
  621. 2. Only way to wake up each is to release what it waits for.
  622. 3. Whether the waiter for AX can be woken up depends on whether the
  623. other can. IOW, TASK X cannot release AX if it fails to acquire B.
  624. Let's take a look at more complicated example:
  625. TASK X TASK Y
  626. ------ ------
  627. acquire B
  628. release B
  629. fork Y
  630. acquire AX
  631. acquire C /* A dependency 'AX -> C' exists */
  632. release C
  633. release AX held by Y
  634. where AX, B and C are different lock classes, and a suffix 'X' is
  635. added on crosslocks.
  636. Does a dependency 'AX -> B' exist? Nope.
  637. Two waiters are essential to create a dependency. However, waiters for
  638. AX and B to create 'AX -> B' cannot exist at the same time in this
  639. example. Thus the dependency 'AX -> B' cannot be created.
  640. It would be ideal if the full set of true ones can be considered. But
  641. we can ensure nothing but what actually happened. Relying on what
  642. actually happens at runtime, we can anyway add only true ones, though
  643. they might be a subset of true ones. It's similar to how lockdep works
  644. for typical locks. There might be more true dependencies than what
  645. lockdep has detected in runtime. Lockdep has no choice but to rely on
  646. what actually happens. Crossrelease also relies on it.
  647. CONCLUSION
  648. Relying on what actually happens, lockdep can avoid adding false
  649. dependencies.