1*4882a593SmuzhiyunRuntime locking correctness validator 2*4882a593Smuzhiyun===================================== 3*4882a593Smuzhiyun 4*4882a593Smuzhiyunstarted by Ingo Molnar <mingo@redhat.com> 5*4882a593Smuzhiyun 6*4882a593Smuzhiyunadditions by Arjan van de Ven <arjan@linux.intel.com> 7*4882a593Smuzhiyun 8*4882a593SmuzhiyunLock-class 9*4882a593Smuzhiyun---------- 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunThe basic object the validator operates upon is a 'class' of locks. 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunA class of locks is a group of locks that are logically the same with 14*4882a593Smuzhiyunrespect to locking rules, even if the locks may have multiple (possibly 15*4882a593Smuzhiyuntens of thousands of) instantiations. For example a lock in the inode 16*4882a593Smuzhiyunstruct is one class, while each inode has its own instantiation of that 17*4882a593Smuzhiyunlock class. 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunThe validator tracks the 'usage state' of lock-classes, and it tracks 20*4882a593Smuzhiyunthe dependencies between different lock-classes. Lock usage indicates 21*4882a593Smuzhiyunhow a lock is used with regard to its IRQ contexts, while lock 22*4882a593Smuzhiyundependency can be understood as lock order, where L1 -> L2 suggests that 23*4882a593Smuzhiyuna task is attempting to acquire L2 while holding L1. From lockdep's 24*4882a593Smuzhiyunperspective, the two locks (L1 and L2) are not necessarily related; that 25*4882a593Smuzhiyundependency just means the order ever happened. The validator maintains a 26*4882a593Smuzhiyuncontinuing effort to prove lock usages and dependencies are correct or 27*4882a593Smuzhiyunthe validator will shoot a splat if incorrect. 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunA lock-class's behavior is constructed by its instances collectively: 30*4882a593Smuzhiyunwhen the first instance of a lock-class is used after bootup the class 31*4882a593Smuzhiyungets registered, then all (subsequent) instances will be mapped to the 32*4882a593Smuzhiyunclass and hence their usages and dependecies will contribute to those of 33*4882a593Smuzhiyunthe class. A lock-class does not go away when a lock instance does, but 34*4882a593Smuzhiyunit can be removed if the memory space of the lock class (static or 35*4882a593Smuzhiyundynamic) is reclaimed, this happens for example when a module is 36*4882a593Smuzhiyununloaded or a workqueue is destroyed. 37*4882a593Smuzhiyun 38*4882a593SmuzhiyunState 39*4882a593Smuzhiyun----- 40*4882a593Smuzhiyun 41*4882a593SmuzhiyunThe validator tracks lock-class usage history and divides the usage into 42*4882a593Smuzhiyun(4 usages * n STATEs + 1) categories: 43*4882a593Smuzhiyun 44*4882a593Smuzhiyunwhere the 4 usages can be: 45*4882a593Smuzhiyun 46*4882a593Smuzhiyun- 'ever held in STATE context' 47*4882a593Smuzhiyun- 'ever held as readlock in STATE context' 48*4882a593Smuzhiyun- 'ever held with STATE enabled' 49*4882a593Smuzhiyun- 'ever held as readlock with STATE enabled' 50*4882a593Smuzhiyun 51*4882a593Smuzhiyunwhere the n STATEs are coded in kernel/locking/lockdep_states.h and as of 52*4882a593Smuzhiyunnow they include: 53*4882a593Smuzhiyun 54*4882a593Smuzhiyun- hardirq 55*4882a593Smuzhiyun- softirq 56*4882a593Smuzhiyun 57*4882a593Smuzhiyunwhere the last 1 category is: 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun- 'ever used' [ == !unused ] 60*4882a593Smuzhiyun 61*4882a593SmuzhiyunWhen locking rules are violated, these usage bits are presented in the 62*4882a593Smuzhiyunlocking error messages, inside curlies, with a total of 2 * n STATEs bits. 63*4882a593SmuzhiyunA contrived example:: 64*4882a593Smuzhiyun 65*4882a593Smuzhiyun modprobe/2287 is trying to acquire lock: 66*4882a593Smuzhiyun (&sio_locks[i].lock){-.-.}, at: [<c02867fd>] mutex_lock+0x21/0x24 67*4882a593Smuzhiyun 68*4882a593Smuzhiyun but task is already holding lock: 69*4882a593Smuzhiyun (&sio_locks[i].lock){-.-.}, at: [<c02867fd>] mutex_lock+0x21/0x24 70*4882a593Smuzhiyun 71*4882a593Smuzhiyun 72*4882a593SmuzhiyunFor a given lock, the bit positions from left to right indicate the usage 73*4882a593Smuzhiyunof the lock and readlock (if exists), for each of the n STATEs listed 74*4882a593Smuzhiyunabove respectively, and the character displayed at each bit position 75*4882a593Smuzhiyunindicates: 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun === =================================================== 78*4882a593Smuzhiyun '.' acquired while irqs disabled and not in irq context 79*4882a593Smuzhiyun '-' acquired in irq context 80*4882a593Smuzhiyun '+' acquired with irqs enabled 81*4882a593Smuzhiyun '?' acquired in irq context with irqs enabled. 82*4882a593Smuzhiyun === =================================================== 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunThe bits are illustrated with an example:: 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun (&sio_locks[i].lock){-.-.}, at: [<c02867fd>] mutex_lock+0x21/0x24 87*4882a593Smuzhiyun |||| 88*4882a593Smuzhiyun ||| \-> softirq disabled and not in softirq context 89*4882a593Smuzhiyun || \--> acquired in softirq context 90*4882a593Smuzhiyun | \---> hardirq disabled and not in hardirq context 91*4882a593Smuzhiyun \----> acquired in hardirq context 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun 94*4882a593SmuzhiyunFor a given STATE, whether the lock is ever acquired in that STATE 95*4882a593Smuzhiyuncontext and whether that STATE is enabled yields four possible cases as 96*4882a593Smuzhiyunshown in the table below. The bit character is able to indicate which 97*4882a593Smuzhiyunexact case is for the lock as of the reporting time. 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun +--------------+-------------+--------------+ 100*4882a593Smuzhiyun | | irq enabled | irq disabled | 101*4882a593Smuzhiyun +--------------+-------------+--------------+ 102*4882a593Smuzhiyun | ever in irq | '?' | '-' | 103*4882a593Smuzhiyun +--------------+-------------+--------------+ 104*4882a593Smuzhiyun | never in irq | '+' | '.' | 105*4882a593Smuzhiyun +--------------+-------------+--------------+ 106*4882a593Smuzhiyun 107*4882a593SmuzhiyunThe character '-' suggests irq is disabled because if otherwise the 108*4882a593Smuzhiyuncharactor '?' would have been shown instead. Similar deduction can be 109*4882a593Smuzhiyunapplied for '+' too. 110*4882a593Smuzhiyun 111*4882a593SmuzhiyunUnused locks (e.g., mutexes) cannot be part of the cause of an error. 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun 114*4882a593SmuzhiyunSingle-lock state rules: 115*4882a593Smuzhiyun------------------------ 116*4882a593Smuzhiyun 117*4882a593SmuzhiyunA lock is irq-safe means it was ever used in an irq context, while a lock 118*4882a593Smuzhiyunis irq-unsafe means it was ever acquired with irq enabled. 119*4882a593Smuzhiyun 120*4882a593SmuzhiyunA softirq-unsafe lock-class is automatically hardirq-unsafe as well. The 121*4882a593Smuzhiyunfollowing states must be exclusive: only one of them is allowed to be set 122*4882a593Smuzhiyunfor any lock-class based on its usage:: 123*4882a593Smuzhiyun 124*4882a593Smuzhiyun <hardirq-safe> or <hardirq-unsafe> 125*4882a593Smuzhiyun <softirq-safe> or <softirq-unsafe> 126*4882a593Smuzhiyun 127*4882a593SmuzhiyunThis is because if a lock can be used in irq context (irq-safe) then it 128*4882a593Smuzhiyuncannot be ever acquired with irq enabled (irq-unsafe). Otherwise, a 129*4882a593Smuzhiyundeadlock may happen. For example, in the scenario that after this lock 130*4882a593Smuzhiyunwas acquired but before released, if the context is interrupted this 131*4882a593Smuzhiyunlock will be attempted to acquire twice, which creates a deadlock, 132*4882a593Smuzhiyunreferred to as lock recursion deadlock. 133*4882a593Smuzhiyun 134*4882a593SmuzhiyunThe validator detects and reports lock usage that violates these 135*4882a593Smuzhiyunsingle-lock state rules. 136*4882a593Smuzhiyun 137*4882a593SmuzhiyunMulti-lock dependency rules: 138*4882a593Smuzhiyun---------------------------- 139*4882a593Smuzhiyun 140*4882a593SmuzhiyunThe same lock-class must not be acquired twice, because this could lead 141*4882a593Smuzhiyunto lock recursion deadlocks. 142*4882a593Smuzhiyun 143*4882a593SmuzhiyunFurthermore, two locks can not be taken in inverse order:: 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun <L1> -> <L2> 146*4882a593Smuzhiyun <L2> -> <L1> 147*4882a593Smuzhiyun 148*4882a593Smuzhiyunbecause this could lead to a deadlock - referred to as lock inversion 149*4882a593Smuzhiyundeadlock - as attempts to acquire the two locks form a circle which 150*4882a593Smuzhiyuncould lead to the two contexts waiting for each other permanently. The 151*4882a593Smuzhiyunvalidator will find such dependency circle in arbitrary complexity, 152*4882a593Smuzhiyuni.e., there can be any other locking sequence between the acquire-lock 153*4882a593Smuzhiyunoperations; the validator will still find whether these locks can be 154*4882a593Smuzhiyunacquired in a circular fashion. 155*4882a593Smuzhiyun 156*4882a593SmuzhiyunFurthermore, the following usage based lock dependencies are not allowed 157*4882a593Smuzhiyunbetween any two lock-classes:: 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun <hardirq-safe> -> <hardirq-unsafe> 160*4882a593Smuzhiyun <softirq-safe> -> <softirq-unsafe> 161*4882a593Smuzhiyun 162*4882a593SmuzhiyunThe first rule comes from the fact that a hardirq-safe lock could be 163*4882a593Smuzhiyuntaken by a hardirq context, interrupting a hardirq-unsafe lock - and 164*4882a593Smuzhiyunthus could result in a lock inversion deadlock. Likewise, a softirq-safe 165*4882a593Smuzhiyunlock could be taken by an softirq context, interrupting a softirq-unsafe 166*4882a593Smuzhiyunlock. 167*4882a593Smuzhiyun 168*4882a593SmuzhiyunThe above rules are enforced for any locking sequence that occurs in the 169*4882a593Smuzhiyunkernel: when acquiring a new lock, the validator checks whether there is 170*4882a593Smuzhiyunany rule violation between the new lock and any of the held locks. 171*4882a593Smuzhiyun 172*4882a593SmuzhiyunWhen a lock-class changes its state, the following aspects of the above 173*4882a593Smuzhiyundependency rules are enforced: 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun- if a new hardirq-safe lock is discovered, we check whether it 176*4882a593Smuzhiyun took any hardirq-unsafe lock in the past. 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun- if a new softirq-safe lock is discovered, we check whether it took 179*4882a593Smuzhiyun any softirq-unsafe lock in the past. 180*4882a593Smuzhiyun 181*4882a593Smuzhiyun- if a new hardirq-unsafe lock is discovered, we check whether any 182*4882a593Smuzhiyun hardirq-safe lock took it in the past. 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun- if a new softirq-unsafe lock is discovered, we check whether any 185*4882a593Smuzhiyun softirq-safe lock took it in the past. 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun(Again, we do these checks too on the basis that an interrupt context 188*4882a593Smuzhiyuncould interrupt _any_ of the irq-unsafe or hardirq-unsafe locks, which 189*4882a593Smuzhiyuncould lead to a lock inversion deadlock - even if that lock scenario did 190*4882a593Smuzhiyunnot trigger in practice yet.) 191*4882a593Smuzhiyun 192*4882a593SmuzhiyunException: Nested data dependencies leading to nested locking 193*4882a593Smuzhiyun------------------------------------------------------------- 194*4882a593Smuzhiyun 195*4882a593SmuzhiyunThere are a few cases where the Linux kernel acquires more than one 196*4882a593Smuzhiyuninstance of the same lock-class. Such cases typically happen when there 197*4882a593Smuzhiyunis some sort of hierarchy within objects of the same type. In these 198*4882a593Smuzhiyuncases there is an inherent "natural" ordering between the two objects 199*4882a593Smuzhiyun(defined by the properties of the hierarchy), and the kernel grabs the 200*4882a593Smuzhiyunlocks in this fixed order on each of the objects. 201*4882a593Smuzhiyun 202*4882a593SmuzhiyunAn example of such an object hierarchy that results in "nested locking" 203*4882a593Smuzhiyunis that of a "whole disk" block-dev object and a "partition" block-dev 204*4882a593Smuzhiyunobject; the partition is "part of" the whole device and as long as one 205*4882a593Smuzhiyunalways takes the whole disk lock as a higher lock than the partition 206*4882a593Smuzhiyunlock, the lock ordering is fully correct. The validator does not 207*4882a593Smuzhiyunautomatically detect this natural ordering, as the locking rule behind 208*4882a593Smuzhiyunthe ordering is not static. 209*4882a593Smuzhiyun 210*4882a593SmuzhiyunIn order to teach the validator about this correct usage model, new 211*4882a593Smuzhiyunversions of the various locking primitives were added that allow you to 212*4882a593Smuzhiyunspecify a "nesting level". An example call, for the block device mutex, 213*4882a593Smuzhiyunlooks like this:: 214*4882a593Smuzhiyun 215*4882a593Smuzhiyun enum bdev_bd_mutex_lock_class 216*4882a593Smuzhiyun { 217*4882a593Smuzhiyun BD_MUTEX_NORMAL, 218*4882a593Smuzhiyun BD_MUTEX_WHOLE, 219*4882a593Smuzhiyun BD_MUTEX_PARTITION 220*4882a593Smuzhiyun }; 221*4882a593Smuzhiyun 222*4882a593Smuzhiyun mutex_lock_nested(&bdev->bd_contains->bd_mutex, BD_MUTEX_PARTITION); 223*4882a593Smuzhiyun 224*4882a593SmuzhiyunIn this case the locking is done on a bdev object that is known to be a 225*4882a593Smuzhiyunpartition. 226*4882a593Smuzhiyun 227*4882a593SmuzhiyunThe validator treats a lock that is taken in such a nested fashion as a 228*4882a593Smuzhiyunseparate (sub)class for the purposes of validation. 229*4882a593Smuzhiyun 230*4882a593SmuzhiyunNote: When changing code to use the _nested() primitives, be careful and 231*4882a593Smuzhiyuncheck really thoroughly that the hierarchy is correctly mapped; otherwise 232*4882a593Smuzhiyunyou can get false positives or false negatives. 233*4882a593Smuzhiyun 234*4882a593SmuzhiyunAnnotations 235*4882a593Smuzhiyun----------- 236*4882a593Smuzhiyun 237*4882a593SmuzhiyunTwo constructs can be used to annotate and check where and if certain locks 238*4882a593Smuzhiyunmust be held: lockdep_assert_held*(&lock) and lockdep_*pin_lock(&lock). 239*4882a593Smuzhiyun 240*4882a593SmuzhiyunAs the name suggests, lockdep_assert_held* family of macros assert that a 241*4882a593Smuzhiyunparticular lock is held at a certain time (and generate a WARN() otherwise). 242*4882a593SmuzhiyunThis annotation is largely used all over the kernel, e.g. kernel/sched/ 243*4882a593Smuzhiyuncore.c:: 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun void update_rq_clock(struct rq *rq) 246*4882a593Smuzhiyun { 247*4882a593Smuzhiyun s64 delta; 248*4882a593Smuzhiyun 249*4882a593Smuzhiyun lockdep_assert_held(&rq->lock); 250*4882a593Smuzhiyun [...] 251*4882a593Smuzhiyun } 252*4882a593Smuzhiyun 253*4882a593Smuzhiyunwhere holding rq->lock is required to safely update a rq's clock. 254*4882a593Smuzhiyun 255*4882a593SmuzhiyunThe other family of macros is lockdep_*pin_lock(), which is admittedly only 256*4882a593Smuzhiyunused for rq->lock ATM. Despite their limited adoption these annotations 257*4882a593Smuzhiyungenerate a WARN() if the lock of interest is "accidentally" unlocked. This turns 258*4882a593Smuzhiyunout to be especially helpful to debug code with callbacks, where an upper 259*4882a593Smuzhiyunlayer assumes a lock remains taken, but a lower layer thinks it can maybe drop 260*4882a593Smuzhiyunand reacquire the lock ("unwittingly" introducing races). lockdep_pin_lock() 261*4882a593Smuzhiyunreturns a 'struct pin_cookie' that is then used by lockdep_unpin_lock() to check 262*4882a593Smuzhiyunthat nobody tampered with the lock, e.g. kernel/sched/sched.h:: 263*4882a593Smuzhiyun 264*4882a593Smuzhiyun static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf) 265*4882a593Smuzhiyun { 266*4882a593Smuzhiyun rf->cookie = lockdep_pin_lock(&rq->lock); 267*4882a593Smuzhiyun [...] 268*4882a593Smuzhiyun } 269*4882a593Smuzhiyun 270*4882a593Smuzhiyun static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf) 271*4882a593Smuzhiyun { 272*4882a593Smuzhiyun [...] 273*4882a593Smuzhiyun lockdep_unpin_lock(&rq->lock, rf->cookie); 274*4882a593Smuzhiyun } 275*4882a593Smuzhiyun 276*4882a593SmuzhiyunWhile comments about locking requirements might provide useful information, 277*4882a593Smuzhiyunthe runtime checks performed by annotations are invaluable when debugging 278*4882a593Smuzhiyunlocking problems and they carry the same level of details when inspecting 279*4882a593Smuzhiyuncode. Always prefer annotations when in doubt! 280*4882a593Smuzhiyun 281*4882a593SmuzhiyunProof of 100% correctness: 282*4882a593Smuzhiyun-------------------------- 283*4882a593Smuzhiyun 284*4882a593SmuzhiyunThe validator achieves perfect, mathematical 'closure' (proof of locking 285*4882a593Smuzhiyuncorrectness) in the sense that for every simple, standalone single-task 286*4882a593Smuzhiyunlocking sequence that occurred at least once during the lifetime of the 287*4882a593Smuzhiyunkernel, the validator proves it with a 100% certainty that no 288*4882a593Smuzhiyuncombination and timing of these locking sequences can cause any class of 289*4882a593Smuzhiyunlock related deadlock. [1]_ 290*4882a593Smuzhiyun 291*4882a593SmuzhiyunI.e. complex multi-CPU and multi-task locking scenarios do not have to 292*4882a593Smuzhiyunoccur in practice to prove a deadlock: only the simple 'component' 293*4882a593Smuzhiyunlocking chains have to occur at least once (anytime, in any 294*4882a593Smuzhiyuntask/context) for the validator to be able to prove correctness. (For 295*4882a593Smuzhiyunexample, complex deadlocks that would normally need more than 3 CPUs and 296*4882a593Smuzhiyuna very unlikely constellation of tasks, irq-contexts and timings to 297*4882a593Smuzhiyunoccur, can be detected on a plain, lightly loaded single-CPU system as 298*4882a593Smuzhiyunwell!) 299*4882a593Smuzhiyun 300*4882a593SmuzhiyunThis radically decreases the complexity of locking related QA of the 301*4882a593Smuzhiyunkernel: what has to be done during QA is to trigger as many "simple" 302*4882a593Smuzhiyunsingle-task locking dependencies in the kernel as possible, at least 303*4882a593Smuzhiyunonce, to prove locking correctness - instead of having to trigger every 304*4882a593Smuzhiyunpossible combination of locking interaction between CPUs, combined with 305*4882a593Smuzhiyunevery possible hardirq and softirq nesting scenario (which is impossible 306*4882a593Smuzhiyunto do in practice). 307*4882a593Smuzhiyun 308*4882a593Smuzhiyun.. [1] 309*4882a593Smuzhiyun 310*4882a593Smuzhiyun assuming that the validator itself is 100% correct, and no other 311*4882a593Smuzhiyun part of the system corrupts the state of the validator in any way. 312*4882a593Smuzhiyun We also assume that all NMI/SMM paths [which could interrupt 313*4882a593Smuzhiyun even hardirq-disabled codepaths] are correct and do not interfere 314*4882a593Smuzhiyun with the validator. We also assume that the 64-bit 'chain hash' 315*4882a593Smuzhiyun value is unique for every lock-chain in the system. Also, lock 316*4882a593Smuzhiyun recursion must not be higher than 20. 317*4882a593Smuzhiyun 318*4882a593SmuzhiyunPerformance: 319*4882a593Smuzhiyun------------ 320*4882a593Smuzhiyun 321*4882a593SmuzhiyunThe above rules require **massive** amounts of runtime checking. If we did 322*4882a593Smuzhiyunthat for every lock taken and for every irqs-enable event, it would 323*4882a593Smuzhiyunrender the system practically unusably slow. The complexity of checking 324*4882a593Smuzhiyunis O(N^2), so even with just a few hundred lock-classes we'd have to do 325*4882a593Smuzhiyuntens of thousands of checks for every event. 326*4882a593Smuzhiyun 327*4882a593SmuzhiyunThis problem is solved by checking any given 'locking scenario' (unique 328*4882a593Smuzhiyunsequence of locks taken after each other) only once. A simple stack of 329*4882a593Smuzhiyunheld locks is maintained, and a lightweight 64-bit hash value is 330*4882a593Smuzhiyuncalculated, which hash is unique for every lock chain. The hash value, 331*4882a593Smuzhiyunwhen the chain is validated for the first time, is then put into a hash 332*4882a593Smuzhiyuntable, which hash-table can be checked in a lockfree manner. If the 333*4882a593Smuzhiyunlocking chain occurs again later on, the hash table tells us that we 334*4882a593Smuzhiyundon't have to validate the chain again. 335*4882a593Smuzhiyun 336*4882a593SmuzhiyunTroubleshooting: 337*4882a593Smuzhiyun---------------- 338*4882a593Smuzhiyun 339*4882a593SmuzhiyunThe validator tracks a maximum of MAX_LOCKDEP_KEYS number of lock classes. 340*4882a593SmuzhiyunExceeding this number will trigger the following lockdep warning:: 341*4882a593Smuzhiyun 342*4882a593Smuzhiyun (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS)) 343*4882a593Smuzhiyun 344*4882a593SmuzhiyunBy default, MAX_LOCKDEP_KEYS is currently set to 8191, and typical 345*4882a593Smuzhiyundesktop systems have less than 1,000 lock classes, so this warning 346*4882a593Smuzhiyunnormally results from lock-class leakage or failure to properly 347*4882a593Smuzhiyuninitialize locks. These two problems are illustrated below: 348*4882a593Smuzhiyun 349*4882a593Smuzhiyun1. Repeated module loading and unloading while running the validator 350*4882a593Smuzhiyun will result in lock-class leakage. The issue here is that each 351*4882a593Smuzhiyun load of the module will create a new set of lock classes for 352*4882a593Smuzhiyun that module's locks, but module unloading does not remove old 353*4882a593Smuzhiyun classes (see below discussion of reuse of lock classes for why). 354*4882a593Smuzhiyun Therefore, if that module is loaded and unloaded repeatedly, 355*4882a593Smuzhiyun the number of lock classes will eventually reach the maximum. 356*4882a593Smuzhiyun 357*4882a593Smuzhiyun2. Using structures such as arrays that have large numbers of 358*4882a593Smuzhiyun locks that are not explicitly initialized. For example, 359*4882a593Smuzhiyun a hash table with 8192 buckets where each bucket has its own 360*4882a593Smuzhiyun spinlock_t will consume 8192 lock classes -unless- each spinlock 361*4882a593Smuzhiyun is explicitly initialized at runtime, for example, using the 362*4882a593Smuzhiyun run-time spin_lock_init() as opposed to compile-time initializers 363*4882a593Smuzhiyun such as __SPIN_LOCK_UNLOCKED(). Failure to properly initialize 364*4882a593Smuzhiyun the per-bucket spinlocks would guarantee lock-class overflow. 365*4882a593Smuzhiyun In contrast, a loop that called spin_lock_init() on each lock 366*4882a593Smuzhiyun would place all 8192 locks into a single lock class. 367*4882a593Smuzhiyun 368*4882a593Smuzhiyun The moral of this story is that you should always explicitly 369*4882a593Smuzhiyun initialize your locks. 370*4882a593Smuzhiyun 371*4882a593SmuzhiyunOne might argue that the validator should be modified to allow 372*4882a593Smuzhiyunlock classes to be reused. However, if you are tempted to make this 373*4882a593Smuzhiyunargument, first review the code and think through the changes that would 374*4882a593Smuzhiyunbe required, keeping in mind that the lock classes to be removed are 375*4882a593Smuzhiyunlikely to be linked into the lock-dependency graph. This turns out to 376*4882a593Smuzhiyunbe harder to do than to say. 377*4882a593Smuzhiyun 378*4882a593SmuzhiyunOf course, if you do run out of lock classes, the next thing to do is 379*4882a593Smuzhiyunto find the offending lock classes. First, the following command gives 380*4882a593Smuzhiyunyou the number of lock classes currently in use along with the maximum:: 381*4882a593Smuzhiyun 382*4882a593Smuzhiyun grep "lock-classes" /proc/lockdep_stats 383*4882a593Smuzhiyun 384*4882a593SmuzhiyunThis command produces the following output on a modest system:: 385*4882a593Smuzhiyun 386*4882a593Smuzhiyun lock-classes: 748 [max: 8191] 387*4882a593Smuzhiyun 388*4882a593SmuzhiyunIf the number allocated (748 above) increases continually over time, 389*4882a593Smuzhiyunthen there is likely a leak. The following command can be used to 390*4882a593Smuzhiyunidentify the leaking lock classes:: 391*4882a593Smuzhiyun 392*4882a593Smuzhiyun grep "BD" /proc/lockdep 393*4882a593Smuzhiyun 394*4882a593SmuzhiyunRun the command and save the output, then compare against the output from 395*4882a593Smuzhiyuna later run of this command to identify the leakers. This same output 396*4882a593Smuzhiyuncan also help you find situations where runtime lock initialization has 397*4882a593Smuzhiyunbeen omitted. 398*4882a593Smuzhiyun 399*4882a593SmuzhiyunRecursive read locks: 400*4882a593Smuzhiyun--------------------- 401*4882a593SmuzhiyunThe whole of the rest document tries to prove a certain type of cycle is equivalent 402*4882a593Smuzhiyunto deadlock possibility. 403*4882a593Smuzhiyun 404*4882a593SmuzhiyunThere are three types of lockers: writers (i.e. exclusive lockers, like 405*4882a593Smuzhiyunspin_lock() or write_lock()), non-recursive readers (i.e. shared lockers, like 406*4882a593Smuzhiyundown_read()) and recursive readers (recursive shared lockers, like rcu_read_lock()). 407*4882a593SmuzhiyunAnd we use the following notations of those lockers in the rest of the document: 408*4882a593Smuzhiyun 409*4882a593Smuzhiyun W or E: stands for writers (exclusive lockers). 410*4882a593Smuzhiyun r: stands for non-recursive readers. 411*4882a593Smuzhiyun R: stands for recursive readers. 412*4882a593Smuzhiyun S: stands for all readers (non-recursive + recursive), as both are shared lockers. 413*4882a593Smuzhiyun N: stands for writers and non-recursive readers, as both are not recursive. 414*4882a593Smuzhiyun 415*4882a593SmuzhiyunObviously, N is "r or W" and S is "r or R". 416*4882a593Smuzhiyun 417*4882a593SmuzhiyunRecursive readers, as their name indicates, are the lockers allowed to acquire 418*4882a593Smuzhiyuneven inside the critical section of another reader of the same lock instance, 419*4882a593Smuzhiyunin other words, allowing nested read-side critical sections of one lock instance. 420*4882a593Smuzhiyun 421*4882a593SmuzhiyunWhile non-recursive readers will cause a self deadlock if trying to acquire inside 422*4882a593Smuzhiyunthe critical section of another reader of the same lock instance. 423*4882a593Smuzhiyun 424*4882a593SmuzhiyunThe difference between recursive readers and non-recursive readers is because: 425*4882a593Smuzhiyunrecursive readers get blocked only by a write lock *holder*, while non-recursive 426*4882a593Smuzhiyunreaders could get blocked by a write lock *waiter*. Considering the follow 427*4882a593Smuzhiyunexample:: 428*4882a593Smuzhiyun 429*4882a593Smuzhiyun TASK A: TASK B: 430*4882a593Smuzhiyun 431*4882a593Smuzhiyun read_lock(X); 432*4882a593Smuzhiyun write_lock(X); 433*4882a593Smuzhiyun read_lock_2(X); 434*4882a593Smuzhiyun 435*4882a593SmuzhiyunTask A gets the reader (no matter whether recursive or non-recursive) on X via 436*4882a593Smuzhiyunread_lock() first. And when task B tries to acquire writer on X, it will block 437*4882a593Smuzhiyunand become a waiter for writer on X. Now if read_lock_2() is recursive readers, 438*4882a593Smuzhiyuntask A will make progress, because writer waiters don't block recursive readers, 439*4882a593Smuzhiyunand there is no deadlock. However, if read_lock_2() is non-recursive readers, 440*4882a593Smuzhiyunit will get blocked by writer waiter B, and cause a self deadlock. 441*4882a593Smuzhiyun 442*4882a593SmuzhiyunBlock conditions on readers/writers of the same lock instance: 443*4882a593Smuzhiyun-------------------------------------------------------------- 444*4882a593SmuzhiyunThere are simply four block conditions: 445*4882a593Smuzhiyun 446*4882a593Smuzhiyun1. Writers block other writers. 447*4882a593Smuzhiyun2. Readers block writers. 448*4882a593Smuzhiyun3. Writers block both recursive readers and non-recursive readers. 449*4882a593Smuzhiyun4. And readers (recursive or not) don't block other recursive readers but 450*4882a593Smuzhiyun may block non-recursive readers (because of the potential co-existing 451*4882a593Smuzhiyun writer waiters) 452*4882a593Smuzhiyun 453*4882a593SmuzhiyunBlock condition matrix, Y means the row blocks the column, and N means otherwise. 454*4882a593Smuzhiyun 455*4882a593Smuzhiyun +---+---+---+---+ 456*4882a593Smuzhiyun | | E | r | R | 457*4882a593Smuzhiyun +---+---+---+---+ 458*4882a593Smuzhiyun | E | Y | Y | Y | 459*4882a593Smuzhiyun +---+---+---+---+ 460*4882a593Smuzhiyun | r | Y | Y | N | 461*4882a593Smuzhiyun +---+---+---+---+ 462*4882a593Smuzhiyun | R | Y | Y | N | 463*4882a593Smuzhiyun +---+---+---+---+ 464*4882a593Smuzhiyun 465*4882a593Smuzhiyun (W: writers, r: non-recursive readers, R: recursive readers) 466*4882a593Smuzhiyun 467*4882a593Smuzhiyun 468*4882a593Smuzhiyunacquired recursively. Unlike non-recursive read locks, recursive read locks 469*4882a593Smuzhiyunonly get blocked by current write lock *holders* other than write lock 470*4882a593Smuzhiyun*waiters*, for example:: 471*4882a593Smuzhiyun 472*4882a593Smuzhiyun TASK A: TASK B: 473*4882a593Smuzhiyun 474*4882a593Smuzhiyun read_lock(X); 475*4882a593Smuzhiyun 476*4882a593Smuzhiyun write_lock(X); 477*4882a593Smuzhiyun 478*4882a593Smuzhiyun read_lock(X); 479*4882a593Smuzhiyun 480*4882a593Smuzhiyunis not a deadlock for recursive read locks, as while the task B is waiting for 481*4882a593Smuzhiyunthe lock X, the second read_lock() doesn't need to wait because it's a recursive 482*4882a593Smuzhiyunread lock. However if the read_lock() is non-recursive read lock, then the above 483*4882a593Smuzhiyuncase is a deadlock, because even if the write_lock() in TASK B cannot get the 484*4882a593Smuzhiyunlock, but it can block the second read_lock() in TASK A. 485*4882a593Smuzhiyun 486*4882a593SmuzhiyunNote that a lock can be a write lock (exclusive lock), a non-recursive read 487*4882a593Smuzhiyunlock (non-recursive shared lock) or a recursive read lock (recursive shared 488*4882a593Smuzhiyunlock), depending on the lock operations used to acquire it (more specifically, 489*4882a593Smuzhiyunthe value of the 'read' parameter for lock_acquire()). In other words, a single 490*4882a593Smuzhiyunlock instance has three types of acquisition depending on the acquisition 491*4882a593Smuzhiyunfunctions: exclusive, non-recursive read, and recursive read. 492*4882a593Smuzhiyun 493*4882a593SmuzhiyunTo be concise, we call that write locks and non-recursive read locks as 494*4882a593Smuzhiyun"non-recursive" locks and recursive read locks as "recursive" locks. 495*4882a593Smuzhiyun 496*4882a593SmuzhiyunRecursive locks don't block each other, while non-recursive locks do (this is 497*4882a593Smuzhiyuneven true for two non-recursive read locks). A non-recursive lock can block the 498*4882a593Smuzhiyuncorresponding recursive lock, and vice versa. 499*4882a593Smuzhiyun 500*4882a593SmuzhiyunA deadlock case with recursive locks involved is as follow:: 501*4882a593Smuzhiyun 502*4882a593Smuzhiyun TASK A: TASK B: 503*4882a593Smuzhiyun 504*4882a593Smuzhiyun read_lock(X); 505*4882a593Smuzhiyun read_lock(Y); 506*4882a593Smuzhiyun write_lock(Y); 507*4882a593Smuzhiyun write_lock(X); 508*4882a593Smuzhiyun 509*4882a593SmuzhiyunTask A is waiting for task B to read_unlock() Y and task B is waiting for task 510*4882a593SmuzhiyunA to read_unlock() X. 511*4882a593Smuzhiyun 512*4882a593SmuzhiyunDependency types and strong dependency paths: 513*4882a593Smuzhiyun--------------------------------------------- 514*4882a593SmuzhiyunLock dependencies record the orders of the acquisitions of a pair of locks, and 515*4882a593Smuzhiyunbecause there are 3 types for lockers, there are, in theory, 9 types of lock 516*4882a593Smuzhiyundependencies, but we can show that 4 types of lock dependencies are enough for 517*4882a593Smuzhiyundeadlock detection. 518*4882a593Smuzhiyun 519*4882a593SmuzhiyunFor each lock dependency:: 520*4882a593Smuzhiyun 521*4882a593Smuzhiyun L1 -> L2 522*4882a593Smuzhiyun 523*4882a593Smuzhiyun, which means lockdep has seen L1 held before L2 held in the same context at runtime. 524*4882a593SmuzhiyunAnd in deadlock detection, we care whether we could get blocked on L2 with L1 held, 525*4882a593SmuzhiyunIOW, whether there is a locker L3 that L1 blocks L3 and L2 gets blocked by L3. So 526*4882a593Smuzhiyunwe only care about 1) what L1 blocks and 2) what blocks L2. As a result, we can combine 527*4882a593Smuzhiyunrecursive readers and non-recursive readers for L1 (as they block the same types) and 528*4882a593Smuzhiyunwe can combine writers and non-recursive readers for L2 (as they get blocked by the 529*4882a593Smuzhiyunsame types). 530*4882a593Smuzhiyun 531*4882a593SmuzhiyunWith the above combination for simplification, there are 4 types of dependency edges 532*4882a593Smuzhiyunin the lockdep graph: 533*4882a593Smuzhiyun 534*4882a593Smuzhiyun1) -(ER)->: 535*4882a593Smuzhiyun exclusive writer to recursive reader dependency, "X -(ER)-> Y" means 536*4882a593Smuzhiyun X -> Y and X is a writer and Y is a recursive reader. 537*4882a593Smuzhiyun 538*4882a593Smuzhiyun2) -(EN)->: 539*4882a593Smuzhiyun exclusive writer to non-recursive locker dependency, "X -(EN)-> Y" means 540*4882a593Smuzhiyun X -> Y and X is a writer and Y is either a writer or non-recursive reader. 541*4882a593Smuzhiyun 542*4882a593Smuzhiyun3) -(SR)->: 543*4882a593Smuzhiyun shared reader to recursive reader dependency, "X -(SR)-> Y" means 544*4882a593Smuzhiyun X -> Y and X is a reader (recursive or not) and Y is a recursive reader. 545*4882a593Smuzhiyun 546*4882a593Smuzhiyun4) -(SN)->: 547*4882a593Smuzhiyun shared reader to non-recursive locker dependency, "X -(SN)-> Y" means 548*4882a593Smuzhiyun X -> Y and X is a reader (recursive or not) and Y is either a writer or 549*4882a593Smuzhiyun non-recursive reader. 550*4882a593Smuzhiyun 551*4882a593SmuzhiyunNote that given two locks, they may have multiple dependencies between them, 552*4882a593Smuzhiyunfor example:: 553*4882a593Smuzhiyun 554*4882a593Smuzhiyun TASK A: 555*4882a593Smuzhiyun 556*4882a593Smuzhiyun read_lock(X); 557*4882a593Smuzhiyun write_lock(Y); 558*4882a593Smuzhiyun ... 559*4882a593Smuzhiyun 560*4882a593Smuzhiyun TASK B: 561*4882a593Smuzhiyun 562*4882a593Smuzhiyun write_lock(X); 563*4882a593Smuzhiyun write_lock(Y); 564*4882a593Smuzhiyun 565*4882a593Smuzhiyun, we have both X -(SN)-> Y and X -(EN)-> Y in the dependency graph. 566*4882a593Smuzhiyun 567*4882a593SmuzhiyunWe use -(xN)-> to represent edges that are either -(EN)-> or -(SN)->, the 568*4882a593Smuzhiyunsimilar for -(Ex)->, -(xR)-> and -(Sx)-> 569*4882a593Smuzhiyun 570*4882a593SmuzhiyunA "path" is a series of conjunct dependency edges in the graph. And we define a 571*4882a593Smuzhiyun"strong" path, which indicates the strong dependency throughout each dependency 572*4882a593Smuzhiyunin the path, as the path that doesn't have two conjunct edges (dependencies) as 573*4882a593Smuzhiyun-(xR)-> and -(Sx)->. In other words, a "strong" path is a path from a lock 574*4882a593Smuzhiyunwalking to another through the lock dependencies, and if X -> Y -> Z is in the 575*4882a593Smuzhiyunpath (where X, Y, Z are locks), and the walk from X to Y is through a -(SR)-> or 576*4882a593Smuzhiyun-(ER)-> dependency, the walk from Y to Z must not be through a -(SN)-> or 577*4882a593Smuzhiyun-(SR)-> dependency. 578*4882a593Smuzhiyun 579*4882a593SmuzhiyunWe will see why the path is called "strong" in next section. 580*4882a593Smuzhiyun 581*4882a593SmuzhiyunRecursive Read Deadlock Detection: 582*4882a593Smuzhiyun---------------------------------- 583*4882a593Smuzhiyun 584*4882a593SmuzhiyunWe now prove two things: 585*4882a593Smuzhiyun 586*4882a593SmuzhiyunLemma 1: 587*4882a593Smuzhiyun 588*4882a593SmuzhiyunIf there is a closed strong path (i.e. a strong circle), then there is a 589*4882a593Smuzhiyuncombination of locking sequences that causes deadlock. I.e. a strong circle is 590*4882a593Smuzhiyunsufficient for deadlock detection. 591*4882a593Smuzhiyun 592*4882a593SmuzhiyunLemma 2: 593*4882a593Smuzhiyun 594*4882a593SmuzhiyunIf there is no closed strong path (i.e. strong circle), then there is no 595*4882a593Smuzhiyuncombination of locking sequences that could cause deadlock. I.e. strong 596*4882a593Smuzhiyuncircles are necessary for deadlock detection. 597*4882a593Smuzhiyun 598*4882a593SmuzhiyunWith these two Lemmas, we can easily say a closed strong path is both sufficient 599*4882a593Smuzhiyunand necessary for deadlocks, therefore a closed strong path is equivalent to 600*4882a593Smuzhiyundeadlock possibility. As a closed strong path stands for a dependency chain that 601*4882a593Smuzhiyuncould cause deadlocks, so we call it "strong", considering there are dependency 602*4882a593Smuzhiyuncircles that won't cause deadlocks. 603*4882a593Smuzhiyun 604*4882a593SmuzhiyunProof for sufficiency (Lemma 1): 605*4882a593Smuzhiyun 606*4882a593SmuzhiyunLet's say we have a strong circle:: 607*4882a593Smuzhiyun 608*4882a593Smuzhiyun L1 -> L2 ... -> Ln -> L1 609*4882a593Smuzhiyun 610*4882a593Smuzhiyun, which means we have dependencies:: 611*4882a593Smuzhiyun 612*4882a593Smuzhiyun L1 -> L2 613*4882a593Smuzhiyun L2 -> L3 614*4882a593Smuzhiyun ... 615*4882a593Smuzhiyun Ln-1 -> Ln 616*4882a593Smuzhiyun Ln -> L1 617*4882a593Smuzhiyun 618*4882a593SmuzhiyunWe now can construct a combination of locking sequences that cause deadlock: 619*4882a593Smuzhiyun 620*4882a593SmuzhiyunFirstly let's make one CPU/task get the L1 in L1 -> L2, and then another get 621*4882a593Smuzhiyunthe L2 in L2 -> L3, and so on. After this, all of the Lx in Lx -> Lx+1 are 622*4882a593Smuzhiyunheld by different CPU/tasks. 623*4882a593Smuzhiyun 624*4882a593SmuzhiyunAnd then because we have L1 -> L2, so the holder of L1 is going to acquire L2 625*4882a593Smuzhiyunin L1 -> L2, however since L2 is already held by another CPU/task, plus L1 -> 626*4882a593SmuzhiyunL2 and L2 -> L3 are not -(xR)-> and -(Sx)-> (the definition of strong), which 627*4882a593Smuzhiyunmeans either L2 in L1 -> L2 is a non-recursive locker (blocked by anyone) or 628*4882a593Smuzhiyunthe L2 in L2 -> L3, is writer (blocking anyone), therefore the holder of L1 629*4882a593Smuzhiyuncannot get L2, it has to wait L2's holder to release. 630*4882a593Smuzhiyun 631*4882a593SmuzhiyunMoreover, we can have a similar conclusion for L2's holder: it has to wait L3's 632*4882a593Smuzhiyunholder to release, and so on. We now can prove that Lx's holder has to wait for 633*4882a593SmuzhiyunLx+1's holder to release, and note that Ln+1 is L1, so we have a circular 634*4882a593Smuzhiyunwaiting scenario and nobody can get progress, therefore a deadlock. 635*4882a593Smuzhiyun 636*4882a593SmuzhiyunProof for necessary (Lemma 2): 637*4882a593Smuzhiyun 638*4882a593SmuzhiyunLemma 2 is equivalent to: If there is a deadlock scenario, then there must be a 639*4882a593Smuzhiyunstrong circle in the dependency graph. 640*4882a593Smuzhiyun 641*4882a593SmuzhiyunAccording to Wikipedia[1], if there is a deadlock, then there must be a circular 642*4882a593Smuzhiyunwaiting scenario, means there are N CPU/tasks, where CPU/task P1 is waiting for 643*4882a593Smuzhiyuna lock held by P2, and P2 is waiting for a lock held by P3, ... and Pn is waiting 644*4882a593Smuzhiyunfor a lock held by P1. Let's name the lock Px is waiting as Lx, so since P1 is waiting 645*4882a593Smuzhiyunfor L1 and holding Ln, so we will have Ln -> L1 in the dependency graph. Similarly, 646*4882a593Smuzhiyunwe have L1 -> L2, L2 -> L3, ..., Ln-1 -> Ln in the dependency graph, which means we 647*4882a593Smuzhiyunhave a circle:: 648*4882a593Smuzhiyun 649*4882a593Smuzhiyun Ln -> L1 -> L2 -> ... -> Ln 650*4882a593Smuzhiyun 651*4882a593Smuzhiyun, and now let's prove the circle is strong: 652*4882a593Smuzhiyun 653*4882a593SmuzhiyunFor a lock Lx, Px contributes the dependency Lx-1 -> Lx and Px+1 contributes 654*4882a593Smuzhiyunthe dependency Lx -> Lx+1, and since Px is waiting for Px+1 to release Lx, 655*4882a593Smuzhiyunso it's impossible that Lx on Px+1 is a reader and Lx on Px is a recursive 656*4882a593Smuzhiyunreader, because readers (no matter recursive or not) don't block recursive 657*4882a593Smuzhiyunreaders, therefore Lx-1 -> Lx and Lx -> Lx+1 cannot be a -(xR)-> -(Sx)-> pair, 658*4882a593Smuzhiyunand this is true for any lock in the circle, therefore, the circle is strong. 659*4882a593Smuzhiyun 660*4882a593SmuzhiyunReferences: 661*4882a593Smuzhiyun----------- 662*4882a593Smuzhiyun[1]: https://en.wikipedia.org/wiki/Deadlock 663*4882a593Smuzhiyun[2]: Shibu, K. (2009). Intro To Embedded Systems (1st ed.). Tata McGraw-Hill 664