xref: /linux/Documentation/locking/lockdep-design.rst (revision fab6216fafdd74cd84de929ffe7b787976d32cff)
1387b1468SMauro Carvalho ChehabRuntime locking correctness validator
2387b1468SMauro Carvalho Chehab=====================================
3387b1468SMauro Carvalho Chehab
4387b1468SMauro Carvalho Chehabstarted by Ingo Molnar <mingo@redhat.com>
5387b1468SMauro Carvalho Chehab
6387b1468SMauro Carvalho Chehabadditions by Arjan van de Ven <arjan@linux.intel.com>
7387b1468SMauro Carvalho Chehab
8387b1468SMauro Carvalho ChehabLock-class
9387b1468SMauro Carvalho Chehab----------
10387b1468SMauro Carvalho Chehab
11387b1468SMauro Carvalho ChehabThe basic object the validator operates upon is a 'class' of locks.
12387b1468SMauro Carvalho Chehab
13387b1468SMauro Carvalho ChehabA class of locks is a group of locks that are logically the same with
14387b1468SMauro Carvalho Chehabrespect to locking rules, even if the locks may have multiple (possibly
15387b1468SMauro Carvalho Chehabtens of thousands of) instantiations. For example a lock in the inode
16387b1468SMauro Carvalho Chehabstruct is one class, while each inode has its own instantiation of that
17387b1468SMauro Carvalho Chehablock class.
18387b1468SMauro Carvalho Chehab
19387b1468SMauro Carvalho ChehabThe validator tracks the 'usage state' of lock-classes, and it tracks
20387b1468SMauro Carvalho Chehabthe dependencies between different lock-classes. Lock usage indicates
21387b1468SMauro Carvalho Chehabhow a lock is used with regard to its IRQ contexts, while lock
22387b1468SMauro Carvalho Chehabdependency can be understood as lock order, where L1 -> L2 suggests that
23387b1468SMauro Carvalho Chehaba task is attempting to acquire L2 while holding L1. From lockdep's
24387b1468SMauro Carvalho Chehabperspective, the two locks (L1 and L2) are not necessarily related; that
25387b1468SMauro Carvalho Chehabdependency just means the order ever happened. The validator maintains a
26387b1468SMauro Carvalho Chehabcontinuing effort to prove lock usages and dependencies are correct or
27387b1468SMauro Carvalho Chehabthe validator will shoot a splat if incorrect.
28387b1468SMauro Carvalho Chehab
29387b1468SMauro Carvalho ChehabA lock-class's behavior is constructed by its instances collectively:
30387b1468SMauro Carvalho Chehabwhen the first instance of a lock-class is used after bootup the class
31387b1468SMauro Carvalho Chehabgets registered, then all (subsequent) instances will be mapped to the
32387b1468SMauro Carvalho Chehabclass and hence their usages and dependecies will contribute to those of
33387b1468SMauro Carvalho Chehabthe class. A lock-class does not go away when a lock instance does, but
34387b1468SMauro Carvalho Chehabit can be removed if the memory space of the lock class (static or
35387b1468SMauro Carvalho Chehabdynamic) is reclaimed, this happens for example when a module is
36387b1468SMauro Carvalho Chehabunloaded or a workqueue is destroyed.
37387b1468SMauro Carvalho Chehab
38387b1468SMauro Carvalho ChehabState
39387b1468SMauro Carvalho Chehab-----
40387b1468SMauro Carvalho Chehab
41387b1468SMauro Carvalho ChehabThe validator tracks lock-class usage history and divides the usage into
42387b1468SMauro Carvalho Chehab(4 usages * n STATEs + 1) categories:
43387b1468SMauro Carvalho Chehab
44387b1468SMauro Carvalho Chehabwhere the 4 usages can be:
45e3e7439dSMauro Carvalho Chehab
46387b1468SMauro Carvalho Chehab- 'ever held in STATE context'
47387b1468SMauro Carvalho Chehab- 'ever held as readlock in STATE context'
48387b1468SMauro Carvalho Chehab- 'ever held with STATE enabled'
49387b1468SMauro Carvalho Chehab- 'ever held as readlock with STATE enabled'
50387b1468SMauro Carvalho Chehab
51387b1468SMauro Carvalho Chehabwhere the n STATEs are coded in kernel/locking/lockdep_states.h and as of
52387b1468SMauro Carvalho Chehabnow they include:
53e3e7439dSMauro Carvalho Chehab
54387b1468SMauro Carvalho Chehab- hardirq
55387b1468SMauro Carvalho Chehab- softirq
56387b1468SMauro Carvalho Chehab
57387b1468SMauro Carvalho Chehabwhere the last 1 category is:
58e3e7439dSMauro Carvalho Chehab
59387b1468SMauro Carvalho Chehab- 'ever used'                                       [ == !unused        ]
60387b1468SMauro Carvalho Chehab
61387b1468SMauro Carvalho ChehabWhen locking rules are violated, these usage bits are presented in the
62387b1468SMauro Carvalho Chehablocking error messages, inside curlies, with a total of 2 * n STATEs bits.
63387b1468SMauro Carvalho ChehabA contrived example::
64387b1468SMauro Carvalho Chehab
65387b1468SMauro Carvalho Chehab   modprobe/2287 is trying to acquire lock:
66387b1468SMauro Carvalho Chehab    (&sio_locks[i].lock){-.-.}, at: [<c02867fd>] mutex_lock+0x21/0x24
67387b1468SMauro Carvalho Chehab
68387b1468SMauro Carvalho Chehab   but task is already holding lock:
69387b1468SMauro Carvalho Chehab    (&sio_locks[i].lock){-.-.}, at: [<c02867fd>] mutex_lock+0x21/0x24
70387b1468SMauro Carvalho Chehab
71387b1468SMauro Carvalho Chehab
72387b1468SMauro Carvalho ChehabFor a given lock, the bit positions from left to right indicate the usage
73387b1468SMauro Carvalho Chehabof the lock and readlock (if exists), for each of the n STATEs listed
74387b1468SMauro Carvalho Chehababove respectively, and the character displayed at each bit position
75387b1468SMauro Carvalho Chehabindicates:
76387b1468SMauro Carvalho Chehab
77387b1468SMauro Carvalho Chehab   ===  ===================================================
78387b1468SMauro Carvalho Chehab   '.'  acquired while irqs disabled and not in irq context
79387b1468SMauro Carvalho Chehab   '-'  acquired in irq context
80387b1468SMauro Carvalho Chehab   '+'  acquired with irqs enabled
81387b1468SMauro Carvalho Chehab   '?'  acquired in irq context with irqs enabled.
82387b1468SMauro Carvalho Chehab   ===  ===================================================
83387b1468SMauro Carvalho Chehab
84387b1468SMauro Carvalho ChehabThe bits are illustrated with an example::
85387b1468SMauro Carvalho Chehab
86387b1468SMauro Carvalho Chehab    (&sio_locks[i].lock){-.-.}, at: [<c02867fd>] mutex_lock+0x21/0x24
87387b1468SMauro Carvalho Chehab                         ||||
88387b1468SMauro Carvalho Chehab                         ||| \-> softirq disabled and not in softirq context
89387b1468SMauro Carvalho Chehab                         || \--> acquired in softirq context
90387b1468SMauro Carvalho Chehab                         | \---> hardirq disabled and not in hardirq context
91387b1468SMauro Carvalho Chehab                          \----> acquired in hardirq context
92387b1468SMauro Carvalho Chehab
93387b1468SMauro Carvalho Chehab
94387b1468SMauro Carvalho ChehabFor a given STATE, whether the lock is ever acquired in that STATE
95387b1468SMauro Carvalho Chehabcontext and whether that STATE is enabled yields four possible cases as
96387b1468SMauro Carvalho Chehabshown in the table below. The bit character is able to indicate which
97387b1468SMauro Carvalho Chehabexact case is for the lock as of the reporting time.
98387b1468SMauro Carvalho Chehab
99387b1468SMauro Carvalho Chehab  +--------------+-------------+--------------+
100387b1468SMauro Carvalho Chehab  |              | irq enabled | irq disabled |
101387b1468SMauro Carvalho Chehab  +--------------+-------------+--------------+
102e3e7439dSMauro Carvalho Chehab  | ever in irq  |     '?'     |      '-'     |
103387b1468SMauro Carvalho Chehab  +--------------+-------------+--------------+
104e3e7439dSMauro Carvalho Chehab  | never in irq |     '+'     |      '.'     |
105387b1468SMauro Carvalho Chehab  +--------------+-------------+--------------+
106387b1468SMauro Carvalho Chehab
107387b1468SMauro Carvalho ChehabThe character '-' suggests irq is disabled because if otherwise the
108387b1468SMauro Carvalho Chehabcharactor '?' would have been shown instead. Similar deduction can be
109387b1468SMauro Carvalho Chehabapplied for '+' too.
110387b1468SMauro Carvalho Chehab
111387b1468SMauro Carvalho ChehabUnused locks (e.g., mutexes) cannot be part of the cause of an error.
112387b1468SMauro Carvalho Chehab
113387b1468SMauro Carvalho Chehab
114387b1468SMauro Carvalho ChehabSingle-lock state rules:
115387b1468SMauro Carvalho Chehab------------------------
116387b1468SMauro Carvalho Chehab
117387b1468SMauro Carvalho ChehabA lock is irq-safe means it was ever used in an irq context, while a lock
118387b1468SMauro Carvalho Chehabis irq-unsafe means it was ever acquired with irq enabled.
119387b1468SMauro Carvalho Chehab
120387b1468SMauro Carvalho ChehabA softirq-unsafe lock-class is automatically hardirq-unsafe as well. The
121387b1468SMauro Carvalho Chehabfollowing states must be exclusive: only one of them is allowed to be set
122387b1468SMauro Carvalho Chehabfor any lock-class based on its usage::
123387b1468SMauro Carvalho Chehab
124387b1468SMauro Carvalho Chehab <hardirq-safe> or <hardirq-unsafe>
125387b1468SMauro Carvalho Chehab <softirq-safe> or <softirq-unsafe>
126387b1468SMauro Carvalho Chehab
127387b1468SMauro Carvalho ChehabThis is because if a lock can be used in irq context (irq-safe) then it
128387b1468SMauro Carvalho Chehabcannot be ever acquired with irq enabled (irq-unsafe). Otherwise, a
129387b1468SMauro Carvalho Chehabdeadlock may happen. For example, in the scenario that after this lock
130387b1468SMauro Carvalho Chehabwas acquired but before released, if the context is interrupted this
131387b1468SMauro Carvalho Chehablock will be attempted to acquire twice, which creates a deadlock,
132387b1468SMauro Carvalho Chehabreferred to as lock recursion deadlock.
133387b1468SMauro Carvalho Chehab
134387b1468SMauro Carvalho ChehabThe validator detects and reports lock usage that violates these
135387b1468SMauro Carvalho Chehabsingle-lock state rules.
136387b1468SMauro Carvalho Chehab
137387b1468SMauro Carvalho ChehabMulti-lock dependency rules:
138387b1468SMauro Carvalho Chehab----------------------------
139387b1468SMauro Carvalho Chehab
140387b1468SMauro Carvalho ChehabThe same lock-class must not be acquired twice, because this could lead
141387b1468SMauro Carvalho Chehabto lock recursion deadlocks.
142387b1468SMauro Carvalho Chehab
143387b1468SMauro Carvalho ChehabFurthermore, two locks can not be taken in inverse order::
144387b1468SMauro Carvalho Chehab
145387b1468SMauro Carvalho Chehab <L1> -> <L2>
146387b1468SMauro Carvalho Chehab <L2> -> <L1>
147387b1468SMauro Carvalho Chehab
148387b1468SMauro Carvalho Chehabbecause this could lead to a deadlock - referred to as lock inversion
149387b1468SMauro Carvalho Chehabdeadlock - as attempts to acquire the two locks form a circle which
150387b1468SMauro Carvalho Chehabcould lead to the two contexts waiting for each other permanently. The
151387b1468SMauro Carvalho Chehabvalidator will find such dependency circle in arbitrary complexity,
152387b1468SMauro Carvalho Chehabi.e., there can be any other locking sequence between the acquire-lock
153387b1468SMauro Carvalho Chehaboperations; the validator will still find whether these locks can be
154387b1468SMauro Carvalho Chehabacquired in a circular fashion.
155387b1468SMauro Carvalho Chehab
156387b1468SMauro Carvalho ChehabFurthermore, the following usage based lock dependencies are not allowed
157387b1468SMauro Carvalho Chehabbetween any two lock-classes::
158387b1468SMauro Carvalho Chehab
159387b1468SMauro Carvalho Chehab   <hardirq-safe>   ->  <hardirq-unsafe>
160387b1468SMauro Carvalho Chehab   <softirq-safe>   ->  <softirq-unsafe>
161387b1468SMauro Carvalho Chehab
162387b1468SMauro Carvalho ChehabThe first rule comes from the fact that a hardirq-safe lock could be
163387b1468SMauro Carvalho Chehabtaken by a hardirq context, interrupting a hardirq-unsafe lock - and
164387b1468SMauro Carvalho Chehabthus could result in a lock inversion deadlock. Likewise, a softirq-safe
165387b1468SMauro Carvalho Chehablock could be taken by an softirq context, interrupting a softirq-unsafe
166387b1468SMauro Carvalho Chehablock.
167387b1468SMauro Carvalho Chehab
168387b1468SMauro Carvalho ChehabThe above rules are enforced for any locking sequence that occurs in the
169387b1468SMauro Carvalho Chehabkernel: when acquiring a new lock, the validator checks whether there is
170387b1468SMauro Carvalho Chehabany rule violation between the new lock and any of the held locks.
171387b1468SMauro Carvalho Chehab
172387b1468SMauro Carvalho ChehabWhen a lock-class changes its state, the following aspects of the above
173387b1468SMauro Carvalho Chehabdependency rules are enforced:
174387b1468SMauro Carvalho Chehab
175387b1468SMauro Carvalho Chehab- if a new hardirq-safe lock is discovered, we check whether it
176387b1468SMauro Carvalho Chehab  took any hardirq-unsafe lock in the past.
177387b1468SMauro Carvalho Chehab
178387b1468SMauro Carvalho Chehab- if a new softirq-safe lock is discovered, we check whether it took
179387b1468SMauro Carvalho Chehab  any softirq-unsafe lock in the past.
180387b1468SMauro Carvalho Chehab
181387b1468SMauro Carvalho Chehab- if a new hardirq-unsafe lock is discovered, we check whether any
182387b1468SMauro Carvalho Chehab  hardirq-safe lock took it in the past.
183387b1468SMauro Carvalho Chehab
184387b1468SMauro Carvalho Chehab- if a new softirq-unsafe lock is discovered, we check whether any
185387b1468SMauro Carvalho Chehab  softirq-safe lock took it in the past.
186387b1468SMauro Carvalho Chehab
187387b1468SMauro Carvalho Chehab(Again, we do these checks too on the basis that an interrupt context
188387b1468SMauro Carvalho Chehabcould interrupt _any_ of the irq-unsafe or hardirq-unsafe locks, which
189387b1468SMauro Carvalho Chehabcould lead to a lock inversion deadlock - even if that lock scenario did
190387b1468SMauro Carvalho Chehabnot trigger in practice yet.)
191387b1468SMauro Carvalho Chehab
192387b1468SMauro Carvalho ChehabException: Nested data dependencies leading to nested locking
193387b1468SMauro Carvalho Chehab-------------------------------------------------------------
194387b1468SMauro Carvalho Chehab
195387b1468SMauro Carvalho ChehabThere are a few cases where the Linux kernel acquires more than one
196387b1468SMauro Carvalho Chehabinstance of the same lock-class. Such cases typically happen when there
197387b1468SMauro Carvalho Chehabis some sort of hierarchy within objects of the same type. In these
198387b1468SMauro Carvalho Chehabcases there is an inherent "natural" ordering between the two objects
199387b1468SMauro Carvalho Chehab(defined by the properties of the hierarchy), and the kernel grabs the
200387b1468SMauro Carvalho Chehablocks in this fixed order on each of the objects.
201387b1468SMauro Carvalho Chehab
202387b1468SMauro Carvalho ChehabAn example of such an object hierarchy that results in "nested locking"
203387b1468SMauro Carvalho Chehabis that of a "whole disk" block-dev object and a "partition" block-dev
204387b1468SMauro Carvalho Chehabobject; the partition is "part of" the whole device and as long as one
205387b1468SMauro Carvalho Chehabalways takes the whole disk lock as a higher lock than the partition
206387b1468SMauro Carvalho Chehablock, the lock ordering is fully correct. The validator does not
207387b1468SMauro Carvalho Chehabautomatically detect this natural ordering, as the locking rule behind
208387b1468SMauro Carvalho Chehabthe ordering is not static.
209387b1468SMauro Carvalho Chehab
210387b1468SMauro Carvalho ChehabIn order to teach the validator about this correct usage model, new
211387b1468SMauro Carvalho Chehabversions of the various locking primitives were added that allow you to
212387b1468SMauro Carvalho Chehabspecify a "nesting level". An example call, for the block device mutex,
213387b1468SMauro Carvalho Chehablooks like this::
214387b1468SMauro Carvalho Chehab
215387b1468SMauro Carvalho Chehab  enum bdev_bd_mutex_lock_class
216387b1468SMauro Carvalho Chehab  {
217387b1468SMauro Carvalho Chehab       BD_MUTEX_NORMAL,
218387b1468SMauro Carvalho Chehab       BD_MUTEX_WHOLE,
219387b1468SMauro Carvalho Chehab       BD_MUTEX_PARTITION
220387b1468SMauro Carvalho Chehab  };
221387b1468SMauro Carvalho Chehab
222387b1468SMauro Carvalho Chehab  mutex_lock_nested(&bdev->bd_contains->bd_mutex, BD_MUTEX_PARTITION);
223387b1468SMauro Carvalho Chehab
224387b1468SMauro Carvalho ChehabIn this case the locking is done on a bdev object that is known to be a
225387b1468SMauro Carvalho Chehabpartition.
226387b1468SMauro Carvalho Chehab
227387b1468SMauro Carvalho ChehabThe validator treats a lock that is taken in such a nested fashion as a
228387b1468SMauro Carvalho Chehabseparate (sub)class for the purposes of validation.
229387b1468SMauro Carvalho Chehab
230387b1468SMauro Carvalho ChehabNote: When changing code to use the _nested() primitives, be careful and
231387b1468SMauro Carvalho Chehabcheck really thoroughly that the hierarchy is correctly mapped; otherwise
232387b1468SMauro Carvalho Chehabyou can get false positives or false negatives.
233387b1468SMauro Carvalho Chehab
234387b1468SMauro Carvalho ChehabAnnotations
235387b1468SMauro Carvalho Chehab-----------
236387b1468SMauro Carvalho Chehab
237387b1468SMauro Carvalho ChehabTwo constructs can be used to annotate and check where and if certain locks
238387b1468SMauro Carvalho Chehabmust be held: lockdep_assert_held*(&lock) and lockdep_*pin_lock(&lock).
239387b1468SMauro Carvalho Chehab
240387b1468SMauro Carvalho ChehabAs the name suggests, lockdep_assert_held* family of macros assert that a
241387b1468SMauro Carvalho Chehabparticular lock is held at a certain time (and generate a WARN() otherwise).
242387b1468SMauro Carvalho ChehabThis annotation is largely used all over the kernel, e.g. kernel/sched/
243387b1468SMauro Carvalho Chehabcore.c::
244387b1468SMauro Carvalho Chehab
245387b1468SMauro Carvalho Chehab  void update_rq_clock(struct rq *rq)
246387b1468SMauro Carvalho Chehab  {
247387b1468SMauro Carvalho Chehab	s64 delta;
248387b1468SMauro Carvalho Chehab
249387b1468SMauro Carvalho Chehab	lockdep_assert_held(&rq->lock);
250387b1468SMauro Carvalho Chehab	[...]
251387b1468SMauro Carvalho Chehab  }
252387b1468SMauro Carvalho Chehab
253387b1468SMauro Carvalho Chehabwhere holding rq->lock is required to safely update a rq's clock.
254387b1468SMauro Carvalho Chehab
255387b1468SMauro Carvalho ChehabThe other family of macros is lockdep_*pin_lock(), which is admittedly only
256387b1468SMauro Carvalho Chehabused for rq->lock ATM. Despite their limited adoption these annotations
257387b1468SMauro Carvalho Chehabgenerate a WARN() if the lock of interest is "accidentally" unlocked. This turns
258387b1468SMauro Carvalho Chehabout to be especially helpful to debug code with callbacks, where an upper
259387b1468SMauro Carvalho Chehablayer assumes a lock remains taken, but a lower layer thinks it can maybe drop
260387b1468SMauro Carvalho Chehaband reacquire the lock ("unwittingly" introducing races). lockdep_pin_lock()
261387b1468SMauro Carvalho Chehabreturns a 'struct pin_cookie' that is then used by lockdep_unpin_lock() to check
262387b1468SMauro Carvalho Chehabthat nobody tampered with the lock, e.g. kernel/sched/sched.h::
263387b1468SMauro Carvalho Chehab
264387b1468SMauro Carvalho Chehab  static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
265387b1468SMauro Carvalho Chehab  {
266387b1468SMauro Carvalho Chehab	rf->cookie = lockdep_pin_lock(&rq->lock);
267387b1468SMauro Carvalho Chehab	[...]
268387b1468SMauro Carvalho Chehab  }
269387b1468SMauro Carvalho Chehab
270387b1468SMauro Carvalho Chehab  static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
271387b1468SMauro Carvalho Chehab  {
272387b1468SMauro Carvalho Chehab	[...]
273387b1468SMauro Carvalho Chehab	lockdep_unpin_lock(&rq->lock, rf->cookie);
274387b1468SMauro Carvalho Chehab  }
275387b1468SMauro Carvalho Chehab
276387b1468SMauro Carvalho ChehabWhile comments about locking requirements might provide useful information,
277387b1468SMauro Carvalho Chehabthe runtime checks performed by annotations are invaluable when debugging
278387b1468SMauro Carvalho Chehablocking problems and they carry the same level of details when inspecting
279387b1468SMauro Carvalho Chehabcode.  Always prefer annotations when in doubt!
280387b1468SMauro Carvalho Chehab
281387b1468SMauro Carvalho ChehabProof of 100% correctness:
282387b1468SMauro Carvalho Chehab--------------------------
283387b1468SMauro Carvalho Chehab
284387b1468SMauro Carvalho ChehabThe validator achieves perfect, mathematical 'closure' (proof of locking
285387b1468SMauro Carvalho Chehabcorrectness) in the sense that for every simple, standalone single-task
286387b1468SMauro Carvalho Chehablocking sequence that occurred at least once during the lifetime of the
287387b1468SMauro Carvalho Chehabkernel, the validator proves it with a 100% certainty that no
288387b1468SMauro Carvalho Chehabcombination and timing of these locking sequences can cause any class of
289387b1468SMauro Carvalho Chehablock related deadlock. [1]_
290387b1468SMauro Carvalho Chehab
291387b1468SMauro Carvalho ChehabI.e. complex multi-CPU and multi-task locking scenarios do not have to
292387b1468SMauro Carvalho Chehaboccur in practice to prove a deadlock: only the simple 'component'
293387b1468SMauro Carvalho Chehablocking chains have to occur at least once (anytime, in any
294387b1468SMauro Carvalho Chehabtask/context) for the validator to be able to prove correctness. (For
295387b1468SMauro Carvalho Chehabexample, complex deadlocks that would normally need more than 3 CPUs and
296387b1468SMauro Carvalho Chehaba very unlikely constellation of tasks, irq-contexts and timings to
297387b1468SMauro Carvalho Chehaboccur, can be detected on a plain, lightly loaded single-CPU system as
298387b1468SMauro Carvalho Chehabwell!)
299387b1468SMauro Carvalho Chehab
300387b1468SMauro Carvalho ChehabThis radically decreases the complexity of locking related QA of the
301387b1468SMauro Carvalho Chehabkernel: what has to be done during QA is to trigger as many "simple"
302387b1468SMauro Carvalho Chehabsingle-task locking dependencies in the kernel as possible, at least
303387b1468SMauro Carvalho Chehabonce, to prove locking correctness - instead of having to trigger every
304387b1468SMauro Carvalho Chehabpossible combination of locking interaction between CPUs, combined with
305387b1468SMauro Carvalho Chehabevery possible hardirq and softirq nesting scenario (which is impossible
306387b1468SMauro Carvalho Chehabto do in practice).
307387b1468SMauro Carvalho Chehab
308387b1468SMauro Carvalho Chehab.. [1]
309387b1468SMauro Carvalho Chehab
310387b1468SMauro Carvalho Chehab    assuming that the validator itself is 100% correct, and no other
311387b1468SMauro Carvalho Chehab    part of the system corrupts the state of the validator in any way.
312387b1468SMauro Carvalho Chehab    We also assume that all NMI/SMM paths [which could interrupt
313387b1468SMauro Carvalho Chehab    even hardirq-disabled codepaths] are correct and do not interfere
314387b1468SMauro Carvalho Chehab    with the validator. We also assume that the 64-bit 'chain hash'
315387b1468SMauro Carvalho Chehab    value is unique for every lock-chain in the system. Also, lock
316387b1468SMauro Carvalho Chehab    recursion must not be higher than 20.
317387b1468SMauro Carvalho Chehab
318387b1468SMauro Carvalho ChehabPerformance:
319387b1468SMauro Carvalho Chehab------------
320387b1468SMauro Carvalho Chehab
321387b1468SMauro Carvalho ChehabThe above rules require **massive** amounts of runtime checking. If we did
322387b1468SMauro Carvalho Chehabthat for every lock taken and for every irqs-enable event, it would
323387b1468SMauro Carvalho Chehabrender the system practically unusably slow. The complexity of checking
324387b1468SMauro Carvalho Chehabis O(N^2), so even with just a few hundred lock-classes we'd have to do
325387b1468SMauro Carvalho Chehabtens of thousands of checks for every event.
326387b1468SMauro Carvalho Chehab
327387b1468SMauro Carvalho ChehabThis problem is solved by checking any given 'locking scenario' (unique
328387b1468SMauro Carvalho Chehabsequence of locks taken after each other) only once. A simple stack of
329387b1468SMauro Carvalho Chehabheld locks is maintained, and a lightweight 64-bit hash value is
330387b1468SMauro Carvalho Chehabcalculated, which hash is unique for every lock chain. The hash value,
331387b1468SMauro Carvalho Chehabwhen the chain is validated for the first time, is then put into a hash
332387b1468SMauro Carvalho Chehabtable, which hash-table can be checked in a lockfree manner. If the
333387b1468SMauro Carvalho Chehablocking chain occurs again later on, the hash table tells us that we
334387b1468SMauro Carvalho Chehabdon't have to validate the chain again.
335387b1468SMauro Carvalho Chehab
336387b1468SMauro Carvalho ChehabTroubleshooting:
337387b1468SMauro Carvalho Chehab----------------
338387b1468SMauro Carvalho Chehab
339387b1468SMauro Carvalho ChehabThe validator tracks a maximum of MAX_LOCKDEP_KEYS number of lock classes.
340e3e7439dSMauro Carvalho ChehabExceeding this number will trigger the following lockdep warning::
341387b1468SMauro Carvalho Chehab
342387b1468SMauro Carvalho Chehab	(DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
343387b1468SMauro Carvalho Chehab
344387b1468SMauro Carvalho ChehabBy default, MAX_LOCKDEP_KEYS is currently set to 8191, and typical
345387b1468SMauro Carvalho Chehabdesktop systems have less than 1,000 lock classes, so this warning
346387b1468SMauro Carvalho Chehabnormally results from lock-class leakage or failure to properly
347387b1468SMauro Carvalho Chehabinitialize locks.  These two problems are illustrated below:
348387b1468SMauro Carvalho Chehab
349387b1468SMauro Carvalho Chehab1.	Repeated module loading and unloading while running the validator
350387b1468SMauro Carvalho Chehab	will result in lock-class leakage.  The issue here is that each
351387b1468SMauro Carvalho Chehab	load of the module will create a new set of lock classes for
352387b1468SMauro Carvalho Chehab	that module's locks, but module unloading does not remove old
353387b1468SMauro Carvalho Chehab	classes (see below discussion of reuse of lock classes for why).
354387b1468SMauro Carvalho Chehab	Therefore, if that module is loaded and unloaded repeatedly,
355387b1468SMauro Carvalho Chehab	the number of lock classes will eventually reach the maximum.
356387b1468SMauro Carvalho Chehab
357387b1468SMauro Carvalho Chehab2.	Using structures such as arrays that have large numbers of
358387b1468SMauro Carvalho Chehab	locks that are not explicitly initialized.  For example,
359387b1468SMauro Carvalho Chehab	a hash table with 8192 buckets where each bucket has its own
360387b1468SMauro Carvalho Chehab	spinlock_t will consume 8192 lock classes -unless- each spinlock
361387b1468SMauro Carvalho Chehab	is explicitly initialized at runtime, for example, using the
362387b1468SMauro Carvalho Chehab	run-time spin_lock_init() as opposed to compile-time initializers
363387b1468SMauro Carvalho Chehab	such as __SPIN_LOCK_UNLOCKED().  Failure to properly initialize
364387b1468SMauro Carvalho Chehab	the per-bucket spinlocks would guarantee lock-class overflow.
365387b1468SMauro Carvalho Chehab	In contrast, a loop that called spin_lock_init() on each lock
366387b1468SMauro Carvalho Chehab	would place all 8192 locks into a single lock class.
367387b1468SMauro Carvalho Chehab
368387b1468SMauro Carvalho Chehab	The moral of this story is that you should always explicitly
369387b1468SMauro Carvalho Chehab	initialize your locks.
370387b1468SMauro Carvalho Chehab
371387b1468SMauro Carvalho ChehabOne might argue that the validator should be modified to allow
372387b1468SMauro Carvalho Chehablock classes to be reused.  However, if you are tempted to make this
373387b1468SMauro Carvalho Chehabargument, first review the code and think through the changes that would
374387b1468SMauro Carvalho Chehabbe required, keeping in mind that the lock classes to be removed are
375387b1468SMauro Carvalho Chehablikely to be linked into the lock-dependency graph.  This turns out to
376387b1468SMauro Carvalho Chehabbe harder to do than to say.
377387b1468SMauro Carvalho Chehab
378387b1468SMauro Carvalho ChehabOf course, if you do run out of lock classes, the next thing to do is
379387b1468SMauro Carvalho Chehabto find the offending lock classes.  First, the following command gives
380387b1468SMauro Carvalho Chehabyou the number of lock classes currently in use along with the maximum::
381387b1468SMauro Carvalho Chehab
382387b1468SMauro Carvalho Chehab	grep "lock-classes" /proc/lockdep_stats
383387b1468SMauro Carvalho Chehab
384387b1468SMauro Carvalho ChehabThis command produces the following output on a modest system::
385387b1468SMauro Carvalho Chehab
386387b1468SMauro Carvalho Chehab	lock-classes:                          748 [max: 8191]
387387b1468SMauro Carvalho Chehab
388387b1468SMauro Carvalho ChehabIf the number allocated (748 above) increases continually over time,
389387b1468SMauro Carvalho Chehabthen there is likely a leak.  The following command can be used to
390387b1468SMauro Carvalho Chehabidentify the leaking lock classes::
391387b1468SMauro Carvalho Chehab
392387b1468SMauro Carvalho Chehab	grep "BD" /proc/lockdep
393387b1468SMauro Carvalho Chehab
394387b1468SMauro Carvalho ChehabRun the command and save the output, then compare against the output from
395387b1468SMauro Carvalho Chehaba later run of this command to identify the leakers.  This same output
396387b1468SMauro Carvalho Chehabcan also help you find situations where runtime lock initialization has
397387b1468SMauro Carvalho Chehabbeen omitted.
398224ec489SBoqun Feng
399224ec489SBoqun FengRecursive read locks:
400224ec489SBoqun Feng---------------------
401224ec489SBoqun FengThe whole of the rest document tries to prove a certain type of cycle is equivalent
402224ec489SBoqun Fengto deadlock possibility.
403224ec489SBoqun Feng
404224ec489SBoqun FengThere are three types of lockers: writers (i.e. exclusive lockers, like
405224ec489SBoqun Fengspin_lock() or write_lock()), non-recursive readers (i.e. shared lockers, like
406224ec489SBoqun Fengdown_read()) and recursive readers (recursive shared lockers, like rcu_read_lock()).
407224ec489SBoqun FengAnd we use the following notations of those lockers in the rest of the document:
408224ec489SBoqun Feng
409224ec489SBoqun Feng	W or E:	stands for writers (exclusive lockers).
410224ec489SBoqun Feng	r:	stands for non-recursive readers.
411224ec489SBoqun Feng	R:	stands for recursive readers.
412224ec489SBoqun Feng	S:	stands for all readers (non-recursive + recursive), as both are shared lockers.
413224ec489SBoqun Feng	N:	stands for writers and non-recursive readers, as both are not recursive.
414224ec489SBoqun Feng
415224ec489SBoqun FengObviously, N is "r or W" and S is "r or R".
416224ec489SBoqun Feng
417224ec489SBoqun FengRecursive readers, as their name indicates, are the lockers allowed to acquire
418224ec489SBoqun Fengeven inside the critical section of another reader of the same lock instance,
419224ec489SBoqun Fengin other words, allowing nested read-side critical sections of one lock instance.
420224ec489SBoqun Feng
421224ec489SBoqun FengWhile non-recursive readers will cause a self deadlock if trying to acquire inside
422224ec489SBoqun Fengthe critical section of another reader of the same lock instance.
423224ec489SBoqun Feng
424224ec489SBoqun FengThe difference between recursive readers and non-recursive readers is because:
425224ec489SBoqun Fengrecursive readers get blocked only by a write lock *holder*, while non-recursive
426e3e7439dSMauro Carvalho Chehabreaders could get blocked by a write lock *waiter*. Considering the follow
427e3e7439dSMauro Carvalho Chehabexample::
428224ec489SBoqun Feng
429224ec489SBoqun Feng	TASK A:			TASK B:
430224ec489SBoqun Feng
431224ec489SBoqun Feng	read_lock(X);
432224ec489SBoqun Feng				write_lock(X);
433224ec489SBoqun Feng	read_lock_2(X);
434224ec489SBoqun Feng
435224ec489SBoqun FengTask A gets the reader (no matter whether recursive or non-recursive) on X via
436224ec489SBoqun Fengread_lock() first. And when task B tries to acquire writer on X, it will block
437224ec489SBoqun Fengand become a waiter for writer on X. Now if read_lock_2() is recursive readers,
438224ec489SBoqun Fengtask A will make progress, because writer waiters don't block recursive readers,
439224ec489SBoqun Fengand there is no deadlock. However, if read_lock_2() is non-recursive readers,
440224ec489SBoqun Fengit will get blocked by writer waiter B, and cause a self deadlock.
441224ec489SBoqun Feng
442224ec489SBoqun FengBlock conditions on readers/writers of the same lock instance:
443224ec489SBoqun Feng--------------------------------------------------------------
444224ec489SBoqun FengThere are simply four block conditions:
445224ec489SBoqun Feng
446224ec489SBoqun Feng1.	Writers block other writers.
447224ec489SBoqun Feng2.	Readers block writers.
448224ec489SBoqun Feng3.	Writers block both recursive readers and non-recursive readers.
449224ec489SBoqun Feng4.	And readers (recursive or not) don't block other recursive readers but
450224ec489SBoqun Feng	may block non-recursive readers (because of the potential co-existing
451224ec489SBoqun Feng	writer waiters)
452224ec489SBoqun Feng
453224ec489SBoqun FengBlock condition matrix, Y means the row blocks the column, and N means otherwise.
454224ec489SBoqun Feng
455224ec489SBoqun Feng	+---+---+---+---+
456*fab6216fSXiongwei Song	|   | W | r | R |
457224ec489SBoqun Feng	+---+---+---+---+
458*fab6216fSXiongwei Song	| W | Y | Y | Y |
459224ec489SBoqun Feng	+---+---+---+---+
460e3e7439dSMauro Carvalho Chehab	| r | Y | Y | N |
461e3e7439dSMauro Carvalho Chehab	+---+---+---+---+
462e3e7439dSMauro Carvalho Chehab	| R | Y | Y | N |
463e3e7439dSMauro Carvalho Chehab	+---+---+---+---+
464224ec489SBoqun Feng
465224ec489SBoqun Feng	(W: writers, r: non-recursive readers, R: recursive readers)
466224ec489SBoqun Feng
467224ec489SBoqun Feng
468224ec489SBoqun Fengacquired recursively. Unlike non-recursive read locks, recursive read locks
469224ec489SBoqun Fengonly get blocked by current write lock *holders* other than write lock
470e3e7439dSMauro Carvalho Chehab*waiters*, for example::
471224ec489SBoqun Feng
472224ec489SBoqun Feng	TASK A:			TASK B:
473224ec489SBoqun Feng
474224ec489SBoqun Feng	read_lock(X);
475224ec489SBoqun Feng
476224ec489SBoqun Feng				write_lock(X);
477224ec489SBoqun Feng
478224ec489SBoqun Feng	read_lock(X);
479224ec489SBoqun Feng
480224ec489SBoqun Fengis not a deadlock for recursive read locks, as while the task B is waiting for
481224ec489SBoqun Fengthe lock X, the second read_lock() doesn't need to wait because it's a recursive
482224ec489SBoqun Fengread lock. However if the read_lock() is non-recursive read lock, then the above
483224ec489SBoqun Fengcase is a deadlock, because even if the write_lock() in TASK B cannot get the
484224ec489SBoqun Fenglock, but it can block the second read_lock() in TASK A.
485224ec489SBoqun Feng
486224ec489SBoqun FengNote that a lock can be a write lock (exclusive lock), a non-recursive read
487224ec489SBoqun Fenglock (non-recursive shared lock) or a recursive read lock (recursive shared
488224ec489SBoqun Fenglock), depending on the lock operations used to acquire it (more specifically,
489224ec489SBoqun Fengthe value of the 'read' parameter for lock_acquire()). In other words, a single
490224ec489SBoqun Fenglock instance has three types of acquisition depending on the acquisition
491224ec489SBoqun Fengfunctions: exclusive, non-recursive read, and recursive read.
492224ec489SBoqun Feng
493224ec489SBoqun FengTo be concise, we call that write locks and non-recursive read locks as
494224ec489SBoqun Feng"non-recursive" locks and recursive read locks as "recursive" locks.
495224ec489SBoqun Feng
496224ec489SBoqun FengRecursive locks don't block each other, while non-recursive locks do (this is
497224ec489SBoqun Fengeven true for two non-recursive read locks). A non-recursive lock can block the
498224ec489SBoqun Fengcorresponding recursive lock, and vice versa.
499224ec489SBoqun Feng
500e3e7439dSMauro Carvalho ChehabA deadlock case with recursive locks involved is as follow::
501224ec489SBoqun Feng
502224ec489SBoqun Feng	TASK A:			TASK B:
503224ec489SBoqun Feng
504224ec489SBoqun Feng	read_lock(X);
505224ec489SBoqun Feng				read_lock(Y);
506224ec489SBoqun Feng	write_lock(Y);
507224ec489SBoqun Feng				write_lock(X);
508224ec489SBoqun Feng
509224ec489SBoqun FengTask A is waiting for task B to read_unlock() Y and task B is waiting for task
510224ec489SBoqun FengA to read_unlock() X.
511224ec489SBoqun Feng
512224ec489SBoqun FengDependency types and strong dependency paths:
513224ec489SBoqun Feng---------------------------------------------
514224ec489SBoqun FengLock dependencies record the orders of the acquisitions of a pair of locks, and
515224ec489SBoqun Fengbecause there are 3 types for lockers, there are, in theory, 9 types of lock
516224ec489SBoqun Fengdependencies, but we can show that 4 types of lock dependencies are enough for
517224ec489SBoqun Fengdeadlock detection.
518224ec489SBoqun Feng
519e3e7439dSMauro Carvalho ChehabFor each lock dependency::
520224ec489SBoqun Feng
521224ec489SBoqun Feng	L1 -> L2
522224ec489SBoqun Feng
523224ec489SBoqun Feng, which means lockdep has seen L1 held before L2 held in the same context at runtime.
524224ec489SBoqun FengAnd in deadlock detection, we care whether we could get blocked on L2 with L1 held,
525224ec489SBoqun FengIOW, whether there is a locker L3 that L1 blocks L3 and L2 gets blocked by L3. So
526224ec489SBoqun Fengwe only care about 1) what L1 blocks and 2) what blocks L2. As a result, we can combine
527224ec489SBoqun Fengrecursive readers and non-recursive readers for L1 (as they block the same types) and
528224ec489SBoqun Fengwe can combine writers and non-recursive readers for L2 (as they get blocked by the
529224ec489SBoqun Fengsame types).
530224ec489SBoqun Feng
531224ec489SBoqun FengWith the above combination for simplification, there are 4 types of dependency edges
532224ec489SBoqun Fengin the lockdep graph:
533224ec489SBoqun Feng
534e3e7439dSMauro Carvalho Chehab1) -(ER)->:
535e3e7439dSMauro Carvalho Chehab	    exclusive writer to recursive reader dependency, "X -(ER)-> Y" means
536224ec489SBoqun Feng	    X -> Y and X is a writer and Y is a recursive reader.
537224ec489SBoqun Feng
538e3e7439dSMauro Carvalho Chehab2) -(EN)->:
539e3e7439dSMauro Carvalho Chehab	    exclusive writer to non-recursive locker dependency, "X -(EN)-> Y" means
540224ec489SBoqun Feng	    X -> Y and X is a writer and Y is either a writer or non-recursive reader.
541224ec489SBoqun Feng
542e3e7439dSMauro Carvalho Chehab3) -(SR)->:
543e3e7439dSMauro Carvalho Chehab	    shared reader to recursive reader dependency, "X -(SR)-> Y" means
544224ec489SBoqun Feng	    X -> Y and X is a reader (recursive or not) and Y is a recursive reader.
545224ec489SBoqun Feng
546e3e7439dSMauro Carvalho Chehab4) -(SN)->:
547e3e7439dSMauro Carvalho Chehab	    shared reader to non-recursive locker dependency, "X -(SN)-> Y" means
548224ec489SBoqun Feng	    X -> Y and X is a reader (recursive or not) and Y is either a writer or
549224ec489SBoqun Feng	    non-recursive reader.
550224ec489SBoqun Feng
551e3e7439dSMauro Carvalho ChehabNote that given two locks, they may have multiple dependencies between them,
552e3e7439dSMauro Carvalho Chehabfor example::
553224ec489SBoqun Feng
554224ec489SBoqun Feng	TASK A:
555224ec489SBoqun Feng
556224ec489SBoqun Feng	read_lock(X);
557224ec489SBoqun Feng	write_lock(Y);
558224ec489SBoqun Feng	...
559224ec489SBoqun Feng
560224ec489SBoqun Feng	TASK B:
561224ec489SBoqun Feng
562224ec489SBoqun Feng	write_lock(X);
563224ec489SBoqun Feng	write_lock(Y);
564224ec489SBoqun Feng
565224ec489SBoqun Feng, we have both X -(SN)-> Y and X -(EN)-> Y in the dependency graph.
566224ec489SBoqun Feng
567224ec489SBoqun FengWe use -(xN)-> to represent edges that are either -(EN)-> or -(SN)->, the
568224ec489SBoqun Fengsimilar for -(Ex)->, -(xR)-> and -(Sx)->
569224ec489SBoqun Feng
570224ec489SBoqun FengA "path" is a series of conjunct dependency edges in the graph. And we define a
571224ec489SBoqun Feng"strong" path, which indicates the strong dependency throughout each dependency
572224ec489SBoqun Fengin the path, as the path that doesn't have two conjunct edges (dependencies) as
573224ec489SBoqun Feng-(xR)-> and -(Sx)->. In other words, a "strong" path is a path from a lock
574224ec489SBoqun Fengwalking to another through the lock dependencies, and if X -> Y -> Z is in the
575224ec489SBoqun Fengpath (where X, Y, Z are locks), and the walk from X to Y is through a -(SR)-> or
576224ec489SBoqun Feng-(ER)-> dependency, the walk from Y to Z must not be through a -(SN)-> or
577224ec489SBoqun Feng-(SR)-> dependency.
578224ec489SBoqun Feng
579224ec489SBoqun FengWe will see why the path is called "strong" in next section.
580224ec489SBoqun Feng
581224ec489SBoqun FengRecursive Read Deadlock Detection:
582224ec489SBoqun Feng----------------------------------
583224ec489SBoqun Feng
584224ec489SBoqun FengWe now prove two things:
585224ec489SBoqun Feng
586224ec489SBoqun FengLemma 1:
587224ec489SBoqun Feng
588224ec489SBoqun FengIf there is a closed strong path (i.e. a strong circle), then there is a
589224ec489SBoqun Fengcombination of locking sequences that causes deadlock. I.e. a strong circle is
590224ec489SBoqun Fengsufficient for deadlock detection.
591224ec489SBoqun Feng
592224ec489SBoqun FengLemma 2:
593224ec489SBoqun Feng
594224ec489SBoqun FengIf there is no closed strong path (i.e. strong circle), then there is no
595224ec489SBoqun Fengcombination of locking sequences that could cause deadlock. I.e.  strong
596224ec489SBoqun Fengcircles are necessary for deadlock detection.
597224ec489SBoqun Feng
598224ec489SBoqun FengWith these two Lemmas, we can easily say a closed strong path is both sufficient
599224ec489SBoqun Fengand necessary for deadlocks, therefore a closed strong path is equivalent to
600224ec489SBoqun Fengdeadlock possibility. As a closed strong path stands for a dependency chain that
601224ec489SBoqun Fengcould cause deadlocks, so we call it "strong", considering there are dependency
602224ec489SBoqun Fengcircles that won't cause deadlocks.
603224ec489SBoqun Feng
604224ec489SBoqun FengProof for sufficiency (Lemma 1):
605224ec489SBoqun Feng
606e3e7439dSMauro Carvalho ChehabLet's say we have a strong circle::
607224ec489SBoqun Feng
608224ec489SBoqun Feng	L1 -> L2 ... -> Ln -> L1
609224ec489SBoqun Feng
610e3e7439dSMauro Carvalho Chehab, which means we have dependencies::
611224ec489SBoqun Feng
612224ec489SBoqun Feng	L1 -> L2
613224ec489SBoqun Feng	L2 -> L3
614224ec489SBoqun Feng	...
615224ec489SBoqun Feng	Ln-1 -> Ln
616224ec489SBoqun Feng	Ln -> L1
617224ec489SBoqun Feng
618224ec489SBoqun FengWe now can construct a combination of locking sequences that cause deadlock:
619224ec489SBoqun Feng
620224ec489SBoqun FengFirstly let's make one CPU/task get the L1 in L1 -> L2, and then another get
621224ec489SBoqun Fengthe L2 in L2 -> L3, and so on. After this, all of the Lx in Lx -> Lx+1 are
622224ec489SBoqun Fengheld by different CPU/tasks.
623224ec489SBoqun Feng
624224ec489SBoqun FengAnd then because we have L1 -> L2, so the holder of L1 is going to acquire L2
625224ec489SBoqun Fengin L1 -> L2, however since L2 is already held by another CPU/task, plus L1 ->
626224ec489SBoqun FengL2 and L2 -> L3 are not -(xR)-> and -(Sx)-> (the definition of strong), which
627224ec489SBoqun Fengmeans either L2 in L1 -> L2 is a non-recursive locker (blocked by anyone) or
628224ec489SBoqun Fengthe L2 in L2 -> L3, is writer (blocking anyone), therefore the holder of L1
629224ec489SBoqun Fengcannot get L2, it has to wait L2's holder to release.
630224ec489SBoqun Feng
631224ec489SBoqun FengMoreover, we can have a similar conclusion for L2's holder: it has to wait L3's
632224ec489SBoqun Fengholder to release, and so on. We now can prove that Lx's holder has to wait for
633224ec489SBoqun FengLx+1's holder to release, and note that Ln+1 is L1, so we have a circular
634224ec489SBoqun Fengwaiting scenario and nobody can get progress, therefore a deadlock.
635224ec489SBoqun Feng
636224ec489SBoqun FengProof for necessary (Lemma 2):
637224ec489SBoqun Feng
638224ec489SBoqun FengLemma 2 is equivalent to: If there is a deadlock scenario, then there must be a
639224ec489SBoqun Fengstrong circle in the dependency graph.
640224ec489SBoqun Feng
641224ec489SBoqun FengAccording to Wikipedia[1], if there is a deadlock, then there must be a circular
642224ec489SBoqun Fengwaiting scenario, means there are N CPU/tasks, where CPU/task P1 is waiting for
643224ec489SBoqun Fenga lock held by P2, and P2 is waiting for a lock held by P3, ... and Pn is waiting
644224ec489SBoqun Fengfor a lock held by P1. Let's name the lock Px is waiting as Lx, so since P1 is waiting
645224ec489SBoqun Fengfor L1 and holding Ln, so we will have Ln -> L1 in the dependency graph. Similarly,
646224ec489SBoqun Fengwe have L1 -> L2, L2 -> L3, ..., Ln-1 -> Ln in the dependency graph, which means we
647e3e7439dSMauro Carvalho Chehabhave a circle::
648224ec489SBoqun Feng
649224ec489SBoqun Feng	Ln -> L1 -> L2 -> ... -> Ln
650224ec489SBoqun Feng
651224ec489SBoqun Feng, and now let's prove the circle is strong:
652224ec489SBoqun Feng
653224ec489SBoqun FengFor a lock Lx, Px contributes the dependency Lx-1 -> Lx and Px+1 contributes
654224ec489SBoqun Fengthe dependency Lx -> Lx+1, and since Px is waiting for Px+1 to release Lx,
655224ec489SBoqun Fengso it's impossible that Lx on Px+1 is a reader and Lx on Px is a recursive
656224ec489SBoqun Fengreader, because readers (no matter recursive or not) don't block recursive
657224ec489SBoqun Fengreaders, therefore Lx-1 -> Lx and Lx -> Lx+1 cannot be a -(xR)-> -(Sx)-> pair,
658224ec489SBoqun Fengand this is true for any lock in the circle, therefore, the circle is strong.
659224ec489SBoqun Feng
660224ec489SBoqun FengReferences:
661224ec489SBoqun Feng-----------
662224ec489SBoqun Feng[1]: https://en.wikipedia.org/wiki/Deadlock
663224ec489SBoqun Feng[2]: Shibu, K. (2009). Intro To Embedded Systems (1st ed.). Tata McGraw-Hill
664