lockdep.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795
  1. /*
  2. * kernel/lockdep.c
  3. *
  4. * Runtime locking correctness validator
  5. *
  6. * Started by Ingo Molnar:
  7. *
  8. * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  9. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  10. *
  11. * this code maps all the lock dependencies as they occur in a live kernel
  12. * and will warn about the following classes of locking bugs:
  13. *
  14. * - lock inversion scenarios
  15. * - circular lock dependencies
  16. * - hardirq/softirq safe/unsafe locking bugs
  17. *
  18. * Bugs are reported even if the current locking scenario does not cause
  19. * any deadlock at this point.
  20. *
  21. * I.e. if anytime in the past two locks were taken in a different order,
  22. * even if it happened for another task, even if those were different
  23. * locks (but of the same class as this lock), this code will detect it.
  24. *
  25. * Thanks to Arjan van de Ven for coming up with the initial idea of
  26. * mapping lock dependencies runtime.
  27. */
  28. #define DISABLE_BRANCH_PROFILING
  29. #include <linux/mutex.h>
  30. #include <linux/sched.h>
  31. #include <linux/delay.h>
  32. #include <linux/module.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/kallsyms.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/stacktrace.h>
  39. #include <linux/debug_locks.h>
  40. #include <linux/irqflags.h>
  41. #include <linux/utsname.h>
  42. #include <linux/hash.h>
  43. #include <linux/ftrace.h>
  44. #include <linux/stringify.h>
  45. #include <linux/bitops.h>
  46. #include <asm/sections.h>
  47. #include "lockdep_internals.h"
  48. #define CREATE_TRACE_POINTS
  49. #include <trace/events/lockdep.h>
  50. #ifdef CONFIG_PROVE_LOCKING
  51. int prove_locking = 1;
  52. module_param(prove_locking, int, 0644);
  53. #else
  54. #define prove_locking 0
  55. #endif
  56. #ifdef CONFIG_LOCK_STAT
  57. int lock_stat = 1;
  58. module_param(lock_stat, int, 0644);
  59. #else
  60. #define lock_stat 0
  61. #endif
  62. /*
  63. * lockdep_lock: protects the lockdep graph, the hashes and the
  64. * class/list/hash allocators.
  65. *
  66. * This is one of the rare exceptions where it's justified
  67. * to use a raw spinlock - we really dont want the spinlock
  68. * code to recurse back into the lockdep code...
  69. */
  70. static raw_spinlock_t lockdep_lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  71. static int graph_lock(void)
  72. {
  73. __raw_spin_lock(&lockdep_lock);
  74. /*
  75. * Make sure that if another CPU detected a bug while
  76. * walking the graph we dont change it (while the other
  77. * CPU is busy printing out stuff with the graph lock
  78. * dropped already)
  79. */
  80. if (!debug_locks) {
  81. __raw_spin_unlock(&lockdep_lock);
  82. return 0;
  83. }
  84. /* prevent any recursions within lockdep from causing deadlocks */
  85. current->lockdep_recursion++;
  86. return 1;
  87. }
  88. static inline int graph_unlock(void)
  89. {
  90. if (debug_locks && !__raw_spin_is_locked(&lockdep_lock))
  91. return DEBUG_LOCKS_WARN_ON(1);
  92. current->lockdep_recursion--;
  93. __raw_spin_unlock(&lockdep_lock);
  94. return 0;
  95. }
  96. /*
  97. * Turn lock debugging off and return with 0 if it was off already,
  98. * and also release the graph lock:
  99. */
  100. static inline int debug_locks_off_graph_unlock(void)
  101. {
  102. int ret = debug_locks_off();
  103. __raw_spin_unlock(&lockdep_lock);
  104. return ret;
  105. }
  106. static int lockdep_initialized;
  107. unsigned long nr_list_entries;
  108. static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
  109. /*
  110. * All data structures here are protected by the global debug_lock.
  111. *
  112. * Mutex key structs only get allocated, once during bootup, and never
  113. * get freed - this significantly simplifies the debugging code.
  114. */
  115. unsigned long nr_lock_classes;
  116. static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
  117. static inline struct lock_class *hlock_class(struct held_lock *hlock)
  118. {
  119. if (!hlock->class_idx) {
  120. DEBUG_LOCKS_WARN_ON(1);
  121. return NULL;
  122. }
  123. return lock_classes + hlock->class_idx - 1;
  124. }
  125. #ifdef CONFIG_LOCK_STAT
  126. static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], lock_stats);
  127. static int lock_point(unsigned long points[], unsigned long ip)
  128. {
  129. int i;
  130. for (i = 0; i < LOCKSTAT_POINTS; i++) {
  131. if (points[i] == 0) {
  132. points[i] = ip;
  133. break;
  134. }
  135. if (points[i] == ip)
  136. break;
  137. }
  138. return i;
  139. }
  140. static void lock_time_inc(struct lock_time *lt, s64 time)
  141. {
  142. if (time > lt->max)
  143. lt->max = time;
  144. if (time < lt->min || !lt->min)
  145. lt->min = time;
  146. lt->total += time;
  147. lt->nr++;
  148. }
  149. static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
  150. {
  151. dst->min += src->min;
  152. dst->max += src->max;
  153. dst->total += src->total;
  154. dst->nr += src->nr;
  155. }
  156. struct lock_class_stats lock_stats(struct lock_class *class)
  157. {
  158. struct lock_class_stats stats;
  159. int cpu, i;
  160. memset(&stats, 0, sizeof(struct lock_class_stats));
  161. for_each_possible_cpu(cpu) {
  162. struct lock_class_stats *pcs =
  163. &per_cpu(lock_stats, cpu)[class - lock_classes];
  164. for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
  165. stats.contention_point[i] += pcs->contention_point[i];
  166. for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
  167. stats.contending_point[i] += pcs->contending_point[i];
  168. lock_time_add(&pcs->read_waittime, &stats.read_waittime);
  169. lock_time_add(&pcs->write_waittime, &stats.write_waittime);
  170. lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
  171. lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
  172. for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
  173. stats.bounces[i] += pcs->bounces[i];
  174. }
  175. return stats;
  176. }
  177. void clear_lock_stats(struct lock_class *class)
  178. {
  179. int cpu;
  180. for_each_possible_cpu(cpu) {
  181. struct lock_class_stats *cpu_stats =
  182. &per_cpu(lock_stats, cpu)[class - lock_classes];
  183. memset(cpu_stats, 0, sizeof(struct lock_class_stats));
  184. }
  185. memset(class->contention_point, 0, sizeof(class->contention_point));
  186. memset(class->contending_point, 0, sizeof(class->contending_point));
  187. }
  188. static struct lock_class_stats *get_lock_stats(struct lock_class *class)
  189. {
  190. return &get_cpu_var(lock_stats)[class - lock_classes];
  191. }
  192. static void put_lock_stats(struct lock_class_stats *stats)
  193. {
  194. put_cpu_var(lock_stats);
  195. }
  196. static void lock_release_holdtime(struct held_lock *hlock)
  197. {
  198. struct lock_class_stats *stats;
  199. s64 holdtime;
  200. if (!lock_stat)
  201. return;
  202. holdtime = sched_clock() - hlock->holdtime_stamp;
  203. stats = get_lock_stats(hlock_class(hlock));
  204. if (hlock->read)
  205. lock_time_inc(&stats->read_holdtime, holdtime);
  206. else
  207. lock_time_inc(&stats->write_holdtime, holdtime);
  208. put_lock_stats(stats);
  209. }
  210. #else
  211. static inline void lock_release_holdtime(struct held_lock *hlock)
  212. {
  213. }
  214. #endif
  215. /*
  216. * We keep a global list of all lock classes. The list only grows,
  217. * never shrinks. The list is only accessed with the lockdep
  218. * spinlock lock held.
  219. */
  220. LIST_HEAD(all_lock_classes);
  221. /*
  222. * The lockdep classes are in a hash-table as well, for fast lookup:
  223. */
  224. #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
  225. #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
  226. #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
  227. #define classhashentry(key) (classhash_table + __classhashfn((key)))
  228. static struct list_head classhash_table[CLASSHASH_SIZE];
  229. /*
  230. * We put the lock dependency chains into a hash-table as well, to cache
  231. * their existence:
  232. */
  233. #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
  234. #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
  235. #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
  236. #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
  237. static struct list_head chainhash_table[CHAINHASH_SIZE];
  238. /*
  239. * The hash key of the lock dependency chains is a hash itself too:
  240. * it's a hash of all locks taken up to that lock, including that lock.
  241. * It's a 64-bit hash, because it's important for the keys to be
  242. * unique.
  243. */
  244. #define iterate_chain_key(key1, key2) \
  245. (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
  246. ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
  247. (key2))
  248. void lockdep_off(void)
  249. {
  250. current->lockdep_recursion++;
  251. }
  252. EXPORT_SYMBOL(lockdep_off);
  253. void lockdep_on(void)
  254. {
  255. current->lockdep_recursion--;
  256. }
  257. EXPORT_SYMBOL(lockdep_on);
  258. /*
  259. * Debugging switches:
  260. */
  261. #define VERBOSE 0
  262. #define VERY_VERBOSE 0
  263. #if VERBOSE
  264. # define HARDIRQ_VERBOSE 1
  265. # define SOFTIRQ_VERBOSE 1
  266. # define RECLAIM_VERBOSE 1
  267. #else
  268. # define HARDIRQ_VERBOSE 0
  269. # define SOFTIRQ_VERBOSE 0
  270. # define RECLAIM_VERBOSE 0
  271. #endif
  272. #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
  273. /*
  274. * Quick filtering for interesting events:
  275. */
  276. static int class_filter(struct lock_class *class)
  277. {
  278. #if 0
  279. /* Example */
  280. if (class->name_version == 1 &&
  281. !strcmp(class->name, "lockname"))
  282. return 1;
  283. if (class->name_version == 1 &&
  284. !strcmp(class->name, "&struct->lockfield"))
  285. return 1;
  286. #endif
  287. /* Filter everything else. 1 would be to allow everything else */
  288. return 0;
  289. }
  290. #endif
  291. static int verbose(struct lock_class *class)
  292. {
  293. #if VERBOSE
  294. return class_filter(class);
  295. #endif
  296. return 0;
  297. }
  298. /*
  299. * Stack-trace: tightly packed array of stack backtrace
  300. * addresses. Protected by the graph_lock.
  301. */
  302. unsigned long nr_stack_trace_entries;
  303. static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
  304. static int save_trace(struct stack_trace *trace)
  305. {
  306. trace->nr_entries = 0;
  307. trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
  308. trace->entries = stack_trace + nr_stack_trace_entries;
  309. trace->skip = 3;
  310. save_stack_trace(trace);
  311. /*
  312. * Some daft arches put -1 at the end to indicate its a full trace.
  313. *
  314. * <rant> this is buggy anyway, since it takes a whole extra entry so a
  315. * complete trace that maxes out the entries provided will be reported
  316. * as incomplete, friggin useless </rant>
  317. */
  318. if (trace->entries[trace->nr_entries-1] == ULONG_MAX)
  319. trace->nr_entries--;
  320. trace->max_entries = trace->nr_entries;
  321. nr_stack_trace_entries += trace->nr_entries;
  322. if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
  323. if (!debug_locks_off_graph_unlock())
  324. return 0;
  325. printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
  326. printk("turning off the locking correctness validator.\n");
  327. dump_stack();
  328. return 0;
  329. }
  330. return 1;
  331. }
  332. unsigned int nr_hardirq_chains;
  333. unsigned int nr_softirq_chains;
  334. unsigned int nr_process_chains;
  335. unsigned int max_lockdep_depth;
  336. #ifdef CONFIG_DEBUG_LOCKDEP
  337. /*
  338. * We cannot printk in early bootup code. Not even early_printk()
  339. * might work. So we mark any initialization errors and printk
  340. * about it later on, in lockdep_info().
  341. */
  342. static int lockdep_init_error;
  343. static unsigned long lockdep_init_trace_data[20];
  344. static struct stack_trace lockdep_init_trace = {
  345. .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
  346. .entries = lockdep_init_trace_data,
  347. };
  348. /*
  349. * Various lockdep statistics:
  350. */
  351. atomic_t chain_lookup_hits;
  352. atomic_t chain_lookup_misses;
  353. atomic_t hardirqs_on_events;
  354. atomic_t hardirqs_off_events;
  355. atomic_t redundant_hardirqs_on;
  356. atomic_t redundant_hardirqs_off;
  357. atomic_t softirqs_on_events;
  358. atomic_t softirqs_off_events;
  359. atomic_t redundant_softirqs_on;
  360. atomic_t redundant_softirqs_off;
  361. atomic_t nr_unused_locks;
  362. atomic_t nr_cyclic_checks;
  363. atomic_t nr_find_usage_forwards_checks;
  364. atomic_t nr_find_usage_backwards_checks;
  365. #endif
  366. /*
  367. * Locking printouts:
  368. */
  369. #define __USAGE(__STATE) \
  370. [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
  371. [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
  372. [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
  373. [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
  374. static const char *usage_str[] =
  375. {
  376. #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
  377. #include "lockdep_states.h"
  378. #undef LOCKDEP_STATE
  379. [LOCK_USED] = "INITIAL USE",
  380. };
  381. const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
  382. {
  383. return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
  384. }
  385. static inline unsigned long lock_flag(enum lock_usage_bit bit)
  386. {
  387. return 1UL << bit;
  388. }
  389. static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
  390. {
  391. char c = '.';
  392. if (class->usage_mask & lock_flag(bit + 2))
  393. c = '+';
  394. if (class->usage_mask & lock_flag(bit)) {
  395. c = '-';
  396. if (class->usage_mask & lock_flag(bit + 2))
  397. c = '?';
  398. }
  399. return c;
  400. }
  401. void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
  402. {
  403. int i = 0;
  404. #define LOCKDEP_STATE(__STATE) \
  405. usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
  406. usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
  407. #include "lockdep_states.h"
  408. #undef LOCKDEP_STATE
  409. usage[i] = '\0';
  410. }
  411. static void print_lock_name(struct lock_class *class)
  412. {
  413. char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
  414. const char *name;
  415. get_usage_chars(class, usage);
  416. name = class->name;
  417. if (!name) {
  418. name = __get_key_name(class->key, str);
  419. printk(" (%s", name);
  420. } else {
  421. printk(" (%s", name);
  422. if (class->name_version > 1)
  423. printk("#%d", class->name_version);
  424. if (class->subclass)
  425. printk("/%d", class->subclass);
  426. }
  427. printk("){%s}", usage);
  428. }
  429. static void print_lockdep_cache(struct lockdep_map *lock)
  430. {
  431. const char *name;
  432. char str[KSYM_NAME_LEN];
  433. name = lock->name;
  434. if (!name)
  435. name = __get_key_name(lock->key->subkeys, str);
  436. printk("%s", name);
  437. }
  438. static void print_lock(struct held_lock *hlock)
  439. {
  440. print_lock_name(hlock_class(hlock));
  441. printk(", at: ");
  442. print_ip_sym(hlock->acquire_ip);
  443. }
  444. static void lockdep_print_held_locks(struct task_struct *curr)
  445. {
  446. int i, depth = curr->lockdep_depth;
  447. if (!depth) {
  448. printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
  449. return;
  450. }
  451. printk("%d lock%s held by %s/%d:\n",
  452. depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
  453. for (i = 0; i < depth; i++) {
  454. printk(" #%d: ", i);
  455. print_lock(curr->held_locks + i);
  456. }
  457. }
  458. static void print_kernel_version(void)
  459. {
  460. printk("%s %.*s\n", init_utsname()->release,
  461. (int)strcspn(init_utsname()->version, " "),
  462. init_utsname()->version);
  463. }
  464. static int very_verbose(struct lock_class *class)
  465. {
  466. #if VERY_VERBOSE
  467. return class_filter(class);
  468. #endif
  469. return 0;
  470. }
  471. /*
  472. * Is this the address of a static object:
  473. */
  474. static int static_obj(void *obj)
  475. {
  476. unsigned long start = (unsigned long) &_stext,
  477. end = (unsigned long) &_end,
  478. addr = (unsigned long) obj;
  479. #ifdef CONFIG_SMP
  480. int i;
  481. #endif
  482. /*
  483. * static variable?
  484. */
  485. if ((addr >= start) && (addr < end))
  486. return 1;
  487. #ifdef CONFIG_SMP
  488. /*
  489. * percpu var?
  490. */
  491. for_each_possible_cpu(i) {
  492. start = (unsigned long) &__per_cpu_start + per_cpu_offset(i);
  493. end = (unsigned long) &__per_cpu_start + PERCPU_ENOUGH_ROOM
  494. + per_cpu_offset(i);
  495. if ((addr >= start) && (addr < end))
  496. return 1;
  497. }
  498. #endif
  499. /*
  500. * module var?
  501. */
  502. return is_module_address(addr);
  503. }
  504. /*
  505. * To make lock name printouts unique, we calculate a unique
  506. * class->name_version generation counter:
  507. */
  508. static int count_matching_names(struct lock_class *new_class)
  509. {
  510. struct lock_class *class;
  511. int count = 0;
  512. if (!new_class->name)
  513. return 0;
  514. list_for_each_entry(class, &all_lock_classes, lock_entry) {
  515. if (new_class->key - new_class->subclass == class->key)
  516. return class->name_version;
  517. if (class->name && !strcmp(class->name, new_class->name))
  518. count = max(count, class->name_version);
  519. }
  520. return count + 1;
  521. }
  522. /*
  523. * Register a lock's class in the hash-table, if the class is not present
  524. * yet. Otherwise we look it up. We cache the result in the lock object
  525. * itself, so actual lookup of the hash should be once per lock object.
  526. */
  527. static inline struct lock_class *
  528. look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
  529. {
  530. struct lockdep_subclass_key *key;
  531. struct list_head *hash_head;
  532. struct lock_class *class;
  533. #ifdef CONFIG_DEBUG_LOCKDEP
  534. /*
  535. * If the architecture calls into lockdep before initializing
  536. * the hashes then we'll warn about it later. (we cannot printk
  537. * right now)
  538. */
  539. if (unlikely(!lockdep_initialized)) {
  540. lockdep_init();
  541. lockdep_init_error = 1;
  542. save_stack_trace(&lockdep_init_trace);
  543. }
  544. #endif
  545. /*
  546. * Static locks do not have their class-keys yet - for them the key
  547. * is the lock object itself:
  548. */
  549. if (unlikely(!lock->key))
  550. lock->key = (void *)lock;
  551. /*
  552. * NOTE: the class-key must be unique. For dynamic locks, a static
  553. * lock_class_key variable is passed in through the mutex_init()
  554. * (or spin_lock_init()) call - which acts as the key. For static
  555. * locks we use the lock object itself as the key.
  556. */
  557. BUILD_BUG_ON(sizeof(struct lock_class_key) >
  558. sizeof(struct lockdep_map));
  559. key = lock->key->subkeys + subclass;
  560. hash_head = classhashentry(key);
  561. /*
  562. * We can walk the hash lockfree, because the hash only
  563. * grows, and we are careful when adding entries to the end:
  564. */
  565. list_for_each_entry(class, hash_head, hash_entry) {
  566. if (class->key == key) {
  567. WARN_ON_ONCE(class->name != lock->name);
  568. return class;
  569. }
  570. }
  571. return NULL;
  572. }
  573. /*
  574. * Register a lock's class in the hash-table, if the class is not present
  575. * yet. Otherwise we look it up. We cache the result in the lock object
  576. * itself, so actual lookup of the hash should be once per lock object.
  577. */
  578. static inline struct lock_class *
  579. register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
  580. {
  581. struct lockdep_subclass_key *key;
  582. struct list_head *hash_head;
  583. struct lock_class *class;
  584. unsigned long flags;
  585. class = look_up_lock_class(lock, subclass);
  586. if (likely(class))
  587. return class;
  588. /*
  589. * Debug-check: all keys must be persistent!
  590. */
  591. if (!static_obj(lock->key)) {
  592. debug_locks_off();
  593. printk("INFO: trying to register non-static key.\n");
  594. printk("the code is fine but needs lockdep annotation.\n");
  595. printk("turning off the locking correctness validator.\n");
  596. dump_stack();
  597. return NULL;
  598. }
  599. key = lock->key->subkeys + subclass;
  600. hash_head = classhashentry(key);
  601. raw_local_irq_save(flags);
  602. if (!graph_lock()) {
  603. raw_local_irq_restore(flags);
  604. return NULL;
  605. }
  606. /*
  607. * We have to do the hash-walk again, to avoid races
  608. * with another CPU:
  609. */
  610. list_for_each_entry(class, hash_head, hash_entry)
  611. if (class->key == key)
  612. goto out_unlock_set;
  613. /*
  614. * Allocate a new key from the static array, and add it to
  615. * the hash:
  616. */
  617. if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
  618. if (!debug_locks_off_graph_unlock()) {
  619. raw_local_irq_restore(flags);
  620. return NULL;
  621. }
  622. raw_local_irq_restore(flags);
  623. printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
  624. printk("turning off the locking correctness validator.\n");
  625. dump_stack();
  626. return NULL;
  627. }
  628. class = lock_classes + nr_lock_classes++;
  629. debug_atomic_inc(&nr_unused_locks);
  630. class->key = key;
  631. class->name = lock->name;
  632. class->subclass = subclass;
  633. INIT_LIST_HEAD(&class->lock_entry);
  634. INIT_LIST_HEAD(&class->locks_before);
  635. INIT_LIST_HEAD(&class->locks_after);
  636. class->name_version = count_matching_names(class);
  637. /*
  638. * We use RCU's safe list-add method to make
  639. * parallel walking of the hash-list safe:
  640. */
  641. list_add_tail_rcu(&class->hash_entry, hash_head);
  642. /*
  643. * Add it to the global list of classes:
  644. */
  645. list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
  646. if (verbose(class)) {
  647. graph_unlock();
  648. raw_local_irq_restore(flags);
  649. printk("\nnew class %p: %s", class->key, class->name);
  650. if (class->name_version > 1)
  651. printk("#%d", class->name_version);
  652. printk("\n");
  653. dump_stack();
  654. raw_local_irq_save(flags);
  655. if (!graph_lock()) {
  656. raw_local_irq_restore(flags);
  657. return NULL;
  658. }
  659. }
  660. out_unlock_set:
  661. graph_unlock();
  662. raw_local_irq_restore(flags);
  663. if (!subclass || force)
  664. lock->class_cache = class;
  665. if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
  666. return NULL;
  667. return class;
  668. }
  669. #ifdef CONFIG_PROVE_LOCKING
  670. /*
  671. * Allocate a lockdep entry. (assumes the graph_lock held, returns
  672. * with NULL on failure)
  673. */
  674. static struct lock_list *alloc_list_entry(void)
  675. {
  676. if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
  677. if (!debug_locks_off_graph_unlock())
  678. return NULL;
  679. printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
  680. printk("turning off the locking correctness validator.\n");
  681. dump_stack();
  682. return NULL;
  683. }
  684. return list_entries + nr_list_entries++;
  685. }
  686. /*
  687. * Add a new dependency to the head of the list:
  688. */
  689. static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
  690. struct list_head *head, unsigned long ip, int distance)
  691. {
  692. struct lock_list *entry;
  693. /*
  694. * Lock not present yet - get a new dependency struct and
  695. * add it to the list:
  696. */
  697. entry = alloc_list_entry();
  698. if (!entry)
  699. return 0;
  700. if (!save_trace(&entry->trace))
  701. return 0;
  702. entry->class = this;
  703. entry->distance = distance;
  704. /*
  705. * Since we never remove from the dependency list, the list can
  706. * be walked lockless by other CPUs, it's only allocation
  707. * that must be protected by the spinlock. But this also means
  708. * we must make new entries visible only once writes to the
  709. * entry become visible - hence the RCU op:
  710. */
  711. list_add_tail_rcu(&entry->entry, head);
  712. return 1;
  713. }
  714. /*
  715. * For good efficiency of modular, we use power of 2
  716. */
  717. #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
  718. #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
  719. /*
  720. * The circular_queue and helpers is used to implement the
  721. * breadth-first search(BFS)algorithem, by which we can build
  722. * the shortest path from the next lock to be acquired to the
  723. * previous held lock if there is a circular between them.
  724. */
  725. struct circular_queue {
  726. unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
  727. unsigned int front, rear;
  728. };
  729. static struct circular_queue lock_cq;
  730. unsigned int max_bfs_queue_depth;
  731. static unsigned int lockdep_dependency_gen_id;
  732. static inline void __cq_init(struct circular_queue *cq)
  733. {
  734. cq->front = cq->rear = 0;
  735. lockdep_dependency_gen_id++;
  736. }
  737. static inline int __cq_empty(struct circular_queue *cq)
  738. {
  739. return (cq->front == cq->rear);
  740. }
  741. static inline int __cq_full(struct circular_queue *cq)
  742. {
  743. return ((cq->rear + 1) & CQ_MASK) == cq->front;
  744. }
  745. static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
  746. {
  747. if (__cq_full(cq))
  748. return -1;
  749. cq->element[cq->rear] = elem;
  750. cq->rear = (cq->rear + 1) & CQ_MASK;
  751. return 0;
  752. }
  753. static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
  754. {
  755. if (__cq_empty(cq))
  756. return -1;
  757. *elem = cq->element[cq->front];
  758. cq->front = (cq->front + 1) & CQ_MASK;
  759. return 0;
  760. }
  761. static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
  762. {
  763. return (cq->rear - cq->front) & CQ_MASK;
  764. }
  765. static inline void mark_lock_accessed(struct lock_list *lock,
  766. struct lock_list *parent)
  767. {
  768. unsigned long nr;
  769. nr = lock - list_entries;
  770. WARN_ON(nr >= nr_list_entries);
  771. lock->parent = parent;
  772. lock->class->dep_gen_id = lockdep_dependency_gen_id;
  773. }
  774. static inline unsigned long lock_accessed(struct lock_list *lock)
  775. {
  776. unsigned long nr;
  777. nr = lock - list_entries;
  778. WARN_ON(nr >= nr_list_entries);
  779. return lock->class->dep_gen_id == lockdep_dependency_gen_id;
  780. }
  781. static inline struct lock_list *get_lock_parent(struct lock_list *child)
  782. {
  783. return child->parent;
  784. }
  785. static inline int get_lock_depth(struct lock_list *child)
  786. {
  787. int depth = 0;
  788. struct lock_list *parent;
  789. while ((parent = get_lock_parent(child))) {
  790. child = parent;
  791. depth++;
  792. }
  793. return depth;
  794. }
  795. static int __bfs(struct lock_list *source_entry,
  796. void *data,
  797. int (*match)(struct lock_list *entry, void *data),
  798. struct lock_list **target_entry,
  799. int forward)
  800. {
  801. struct lock_list *entry;
  802. struct list_head *head;
  803. struct circular_queue *cq = &lock_cq;
  804. int ret = 1;
  805. if (match(source_entry, data)) {
  806. *target_entry = source_entry;
  807. ret = 0;
  808. goto exit;
  809. }
  810. if (forward)
  811. head = &source_entry->class->locks_after;
  812. else
  813. head = &source_entry->class->locks_before;
  814. if (list_empty(head))
  815. goto exit;
  816. __cq_init(cq);
  817. __cq_enqueue(cq, (unsigned long)source_entry);
  818. while (!__cq_empty(cq)) {
  819. struct lock_list *lock;
  820. __cq_dequeue(cq, (unsigned long *)&lock);
  821. if (!lock->class) {
  822. ret = -2;
  823. goto exit;
  824. }
  825. if (forward)
  826. head = &lock->class->locks_after;
  827. else
  828. head = &lock->class->locks_before;
  829. list_for_each_entry(entry, head, entry) {
  830. if (!lock_accessed(entry)) {
  831. unsigned int cq_depth;
  832. mark_lock_accessed(entry, lock);
  833. if (match(entry, data)) {
  834. *target_entry = entry;
  835. ret = 0;
  836. goto exit;
  837. }
  838. if (__cq_enqueue(cq, (unsigned long)entry)) {
  839. ret = -1;
  840. goto exit;
  841. }
  842. cq_depth = __cq_get_elem_count(cq);
  843. if (max_bfs_queue_depth < cq_depth)
  844. max_bfs_queue_depth = cq_depth;
  845. }
  846. }
  847. }
  848. exit:
  849. return ret;
  850. }
  851. static inline int __bfs_forwards(struct lock_list *src_entry,
  852. void *data,
  853. int (*match)(struct lock_list *entry, void *data),
  854. struct lock_list **target_entry)
  855. {
  856. return __bfs(src_entry, data, match, target_entry, 1);
  857. }
  858. static inline int __bfs_backwards(struct lock_list *src_entry,
  859. void *data,
  860. int (*match)(struct lock_list *entry, void *data),
  861. struct lock_list **target_entry)
  862. {
  863. return __bfs(src_entry, data, match, target_entry, 0);
  864. }
  865. /*
  866. * Recursive, forwards-direction lock-dependency checking, used for
  867. * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
  868. * checking.
  869. */
  870. /*
  871. * Print a dependency chain entry (this is only done when a deadlock
  872. * has been detected):
  873. */
  874. static noinline int
  875. print_circular_bug_entry(struct lock_list *target, int depth)
  876. {
  877. if (debug_locks_silent)
  878. return 0;
  879. printk("\n-> #%u", depth);
  880. print_lock_name(target->class);
  881. printk(":\n");
  882. print_stack_trace(&target->trace, 6);
  883. return 0;
  884. }
  885. /*
  886. * When a circular dependency is detected, print the
  887. * header first:
  888. */
  889. static noinline int
  890. print_circular_bug_header(struct lock_list *entry, unsigned int depth,
  891. struct held_lock *check_src,
  892. struct held_lock *check_tgt)
  893. {
  894. struct task_struct *curr = current;
  895. if (debug_locks_silent)
  896. return 0;
  897. printk("\n=======================================================\n");
  898. printk( "[ INFO: possible circular locking dependency detected ]\n");
  899. print_kernel_version();
  900. printk( "-------------------------------------------------------\n");
  901. printk("%s/%d is trying to acquire lock:\n",
  902. curr->comm, task_pid_nr(curr));
  903. print_lock(check_src);
  904. printk("\nbut task is already holding lock:\n");
  905. print_lock(check_tgt);
  906. printk("\nwhich lock already depends on the new lock.\n\n");
  907. printk("\nthe existing dependency chain (in reverse order) is:\n");
  908. print_circular_bug_entry(entry, depth);
  909. return 0;
  910. }
  911. static inline int class_equal(struct lock_list *entry, void *data)
  912. {
  913. return entry->class == data;
  914. }
  915. static noinline int print_circular_bug(struct lock_list *this,
  916. struct lock_list *target,
  917. struct held_lock *check_src,
  918. struct held_lock *check_tgt)
  919. {
  920. struct task_struct *curr = current;
  921. struct lock_list *parent;
  922. int depth;
  923. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  924. return 0;
  925. if (!save_trace(&this->trace))
  926. return 0;
  927. depth = get_lock_depth(target);
  928. print_circular_bug_header(target, depth, check_src, check_tgt);
  929. parent = get_lock_parent(target);
  930. while (parent) {
  931. print_circular_bug_entry(parent, --depth);
  932. parent = get_lock_parent(parent);
  933. }
  934. printk("\nother info that might help us debug this:\n\n");
  935. lockdep_print_held_locks(curr);
  936. printk("\nstack backtrace:\n");
  937. dump_stack();
  938. return 0;
  939. }
  940. static noinline int print_bfs_bug(int ret)
  941. {
  942. if (!debug_locks_off_graph_unlock())
  943. return 0;
  944. WARN(1, "lockdep bfs error:%d\n", ret);
  945. return 0;
  946. }
  947. static int noop_count(struct lock_list *entry, void *data)
  948. {
  949. (*(unsigned long *)data)++;
  950. return 0;
  951. }
  952. unsigned long __lockdep_count_forward_deps(struct lock_list *this)
  953. {
  954. unsigned long count = 0;
  955. struct lock_list *uninitialized_var(target_entry);
  956. __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
  957. return count;
  958. }
  959. unsigned long lockdep_count_forward_deps(struct lock_class *class)
  960. {
  961. unsigned long ret, flags;
  962. struct lock_list this;
  963. this.parent = NULL;
  964. this.class = class;
  965. local_irq_save(flags);
  966. __raw_spin_lock(&lockdep_lock);
  967. ret = __lockdep_count_forward_deps(&this);
  968. __raw_spin_unlock(&lockdep_lock);
  969. local_irq_restore(flags);
  970. return ret;
  971. }
  972. unsigned long __lockdep_count_backward_deps(struct lock_list *this)
  973. {
  974. unsigned long count = 0;
  975. struct lock_list *uninitialized_var(target_entry);
  976. __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
  977. return count;
  978. }
  979. unsigned long lockdep_count_backward_deps(struct lock_class *class)
  980. {
  981. unsigned long ret, flags;
  982. struct lock_list this;
  983. this.parent = NULL;
  984. this.class = class;
  985. local_irq_save(flags);
  986. __raw_spin_lock(&lockdep_lock);
  987. ret = __lockdep_count_backward_deps(&this);
  988. __raw_spin_unlock(&lockdep_lock);
  989. local_irq_restore(flags);
  990. return ret;
  991. }
  992. /*
  993. * Prove that the dependency graph starting at <entry> can not
  994. * lead to <target>. Print an error and return 0 if it does.
  995. */
  996. static noinline int
  997. check_noncircular(struct lock_list *root, struct lock_class *target,
  998. struct lock_list **target_entry)
  999. {
  1000. int result;
  1001. debug_atomic_inc(&nr_cyclic_checks);
  1002. result = __bfs_forwards(root, target, class_equal, target_entry);
  1003. return result;
  1004. }
  1005. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
  1006. /*
  1007. * Forwards and backwards subgraph searching, for the purposes of
  1008. * proving that two subgraphs can be connected by a new dependency
  1009. * without creating any illegal irq-safe -> irq-unsafe lock dependency.
  1010. */
  1011. static inline int usage_match(struct lock_list *entry, void *bit)
  1012. {
  1013. return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
  1014. }
  1015. /*
  1016. * Find a node in the forwards-direction dependency sub-graph starting
  1017. * at @root->class that matches @bit.
  1018. *
  1019. * Return 0 if such a node exists in the subgraph, and put that node
  1020. * into *@target_entry.
  1021. *
  1022. * Return 1 otherwise and keep *@target_entry unchanged.
  1023. * Return <0 on error.
  1024. */
  1025. static int
  1026. find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
  1027. struct lock_list **target_entry)
  1028. {
  1029. int result;
  1030. debug_atomic_inc(&nr_find_usage_forwards_checks);
  1031. result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
  1032. return result;
  1033. }
  1034. /*
  1035. * Find a node in the backwards-direction dependency sub-graph starting
  1036. * at @root->class that matches @bit.
  1037. *
  1038. * Return 0 if such a node exists in the subgraph, and put that node
  1039. * into *@target_entry.
  1040. *
  1041. * Return 1 otherwise and keep *@target_entry unchanged.
  1042. * Return <0 on error.
  1043. */
  1044. static int
  1045. find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
  1046. struct lock_list **target_entry)
  1047. {
  1048. int result;
  1049. debug_atomic_inc(&nr_find_usage_backwards_checks);
  1050. result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
  1051. return result;
  1052. }
  1053. static void print_lock_class_header(struct lock_class *class, int depth)
  1054. {
  1055. int bit;
  1056. printk("%*s->", depth, "");
  1057. print_lock_name(class);
  1058. printk(" ops: %lu", class->ops);
  1059. printk(" {\n");
  1060. for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
  1061. if (class->usage_mask & (1 << bit)) {
  1062. int len = depth;
  1063. len += printk("%*s %s", depth, "", usage_str[bit]);
  1064. len += printk(" at:\n");
  1065. print_stack_trace(class->usage_traces + bit, len);
  1066. }
  1067. }
  1068. printk("%*s }\n", depth, "");
  1069. printk("%*s ... key at: ",depth,"");
  1070. print_ip_sym((unsigned long)class->key);
  1071. }
  1072. /*
  1073. * printk the shortest lock dependencies from @start to @end in reverse order:
  1074. */
  1075. static void __used
  1076. print_shortest_lock_dependencies(struct lock_list *leaf,
  1077. struct lock_list *root)
  1078. {
  1079. struct lock_list *entry = leaf;
  1080. int depth;
  1081. /*compute depth from generated tree by BFS*/
  1082. depth = get_lock_depth(leaf);
  1083. do {
  1084. print_lock_class_header(entry->class, depth);
  1085. printk("%*s ... acquired at:\n", depth, "");
  1086. print_stack_trace(&entry->trace, 2);
  1087. printk("\n");
  1088. if (depth == 0 && (entry != root)) {
  1089. printk("lockdep:%s bad BFS generated tree\n", __func__);
  1090. break;
  1091. }
  1092. entry = get_lock_parent(entry);
  1093. depth--;
  1094. } while (entry && (depth >= 0));
  1095. return;
  1096. }
  1097. static int
  1098. print_bad_irq_dependency(struct task_struct *curr,
  1099. struct lock_list *prev_root,
  1100. struct lock_list *next_root,
  1101. struct lock_list *backwards_entry,
  1102. struct lock_list *forwards_entry,
  1103. struct held_lock *prev,
  1104. struct held_lock *next,
  1105. enum lock_usage_bit bit1,
  1106. enum lock_usage_bit bit2,
  1107. const char *irqclass)
  1108. {
  1109. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1110. return 0;
  1111. printk("\n======================================================\n");
  1112. printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
  1113. irqclass, irqclass);
  1114. print_kernel_version();
  1115. printk( "------------------------------------------------------\n");
  1116. printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
  1117. curr->comm, task_pid_nr(curr),
  1118. curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
  1119. curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
  1120. curr->hardirqs_enabled,
  1121. curr->softirqs_enabled);
  1122. print_lock(next);
  1123. printk("\nand this task is already holding:\n");
  1124. print_lock(prev);
  1125. printk("which would create a new lock dependency:\n");
  1126. print_lock_name(hlock_class(prev));
  1127. printk(" ->");
  1128. print_lock_name(hlock_class(next));
  1129. printk("\n");
  1130. printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
  1131. irqclass);
  1132. print_lock_name(backwards_entry->class);
  1133. printk("\n... which became %s-irq-safe at:\n", irqclass);
  1134. print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
  1135. printk("\nto a %s-irq-unsafe lock:\n", irqclass);
  1136. print_lock_name(forwards_entry->class);
  1137. printk("\n... which became %s-irq-unsafe at:\n", irqclass);
  1138. printk("...");
  1139. print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
  1140. printk("\nother info that might help us debug this:\n\n");
  1141. lockdep_print_held_locks(curr);
  1142. printk("\nthe dependencies between %s-irq-safe lock", irqclass);
  1143. printk(" and the holding lock:\n");
  1144. if (!save_trace(&prev_root->trace))
  1145. return 0;
  1146. print_shortest_lock_dependencies(backwards_entry, prev_root);
  1147. printk("\nthe dependencies between the lock to be acquired");
  1148. printk(" and %s-irq-unsafe lock:\n", irqclass);
  1149. if (!save_trace(&next_root->trace))
  1150. return 0;
  1151. print_shortest_lock_dependencies(forwards_entry, next_root);
  1152. printk("\nstack backtrace:\n");
  1153. dump_stack();
  1154. return 0;
  1155. }
  1156. static int
  1157. check_usage(struct task_struct *curr, struct held_lock *prev,
  1158. struct held_lock *next, enum lock_usage_bit bit_backwards,
  1159. enum lock_usage_bit bit_forwards, const char *irqclass)
  1160. {
  1161. int ret;
  1162. struct lock_list this, that;
  1163. struct lock_list *uninitialized_var(target_entry);
  1164. struct lock_list *uninitialized_var(target_entry1);
  1165. this.parent = NULL;
  1166. this.class = hlock_class(prev);
  1167. ret = find_usage_backwards(&this, bit_backwards, &target_entry);
  1168. if (ret < 0)
  1169. return print_bfs_bug(ret);
  1170. if (ret == 1)
  1171. return ret;
  1172. that.parent = NULL;
  1173. that.class = hlock_class(next);
  1174. ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
  1175. if (ret < 0)
  1176. return print_bfs_bug(ret);
  1177. if (ret == 1)
  1178. return ret;
  1179. return print_bad_irq_dependency(curr, &this, &that,
  1180. target_entry, target_entry1,
  1181. prev, next,
  1182. bit_backwards, bit_forwards, irqclass);
  1183. }
  1184. static const char *state_names[] = {
  1185. #define LOCKDEP_STATE(__STATE) \
  1186. __stringify(__STATE),
  1187. #include "lockdep_states.h"
  1188. #undef LOCKDEP_STATE
  1189. };
  1190. static const char *state_rnames[] = {
  1191. #define LOCKDEP_STATE(__STATE) \
  1192. __stringify(__STATE)"-READ",
  1193. #include "lockdep_states.h"
  1194. #undef LOCKDEP_STATE
  1195. };
  1196. static inline const char *state_name(enum lock_usage_bit bit)
  1197. {
  1198. return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
  1199. }
  1200. static int exclusive_bit(int new_bit)
  1201. {
  1202. /*
  1203. * USED_IN
  1204. * USED_IN_READ
  1205. * ENABLED
  1206. * ENABLED_READ
  1207. *
  1208. * bit 0 - write/read
  1209. * bit 1 - used_in/enabled
  1210. * bit 2+ state
  1211. */
  1212. int state = new_bit & ~3;
  1213. int dir = new_bit & 2;
  1214. /*
  1215. * keep state, bit flip the direction and strip read.
  1216. */
  1217. return state | (dir ^ 2);
  1218. }
  1219. static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
  1220. struct held_lock *next, enum lock_usage_bit bit)
  1221. {
  1222. /*
  1223. * Prove that the new dependency does not connect a hardirq-safe
  1224. * lock with a hardirq-unsafe lock - to achieve this we search
  1225. * the backwards-subgraph starting at <prev>, and the
  1226. * forwards-subgraph starting at <next>:
  1227. */
  1228. if (!check_usage(curr, prev, next, bit,
  1229. exclusive_bit(bit), state_name(bit)))
  1230. return 0;
  1231. bit++; /* _READ */
  1232. /*
  1233. * Prove that the new dependency does not connect a hardirq-safe-read
  1234. * lock with a hardirq-unsafe lock - to achieve this we search
  1235. * the backwards-subgraph starting at <prev>, and the
  1236. * forwards-subgraph starting at <next>:
  1237. */
  1238. if (!check_usage(curr, prev, next, bit,
  1239. exclusive_bit(bit), state_name(bit)))
  1240. return 0;
  1241. return 1;
  1242. }
  1243. static int
  1244. check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
  1245. struct held_lock *next)
  1246. {
  1247. #define LOCKDEP_STATE(__STATE) \
  1248. if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
  1249. return 0;
  1250. #include "lockdep_states.h"
  1251. #undef LOCKDEP_STATE
  1252. return 1;
  1253. }
  1254. static void inc_chains(void)
  1255. {
  1256. if (current->hardirq_context)
  1257. nr_hardirq_chains++;
  1258. else {
  1259. if (current->softirq_context)
  1260. nr_softirq_chains++;
  1261. else
  1262. nr_process_chains++;
  1263. }
  1264. }
  1265. #else
  1266. static inline int
  1267. check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
  1268. struct held_lock *next)
  1269. {
  1270. return 1;
  1271. }
  1272. static inline void inc_chains(void)
  1273. {
  1274. nr_process_chains++;
  1275. }
  1276. #endif
  1277. static int
  1278. print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
  1279. struct held_lock *next)
  1280. {
  1281. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1282. return 0;
  1283. printk("\n=============================================\n");
  1284. printk( "[ INFO: possible recursive locking detected ]\n");
  1285. print_kernel_version();
  1286. printk( "---------------------------------------------\n");
  1287. printk("%s/%d is trying to acquire lock:\n",
  1288. curr->comm, task_pid_nr(curr));
  1289. print_lock(next);
  1290. printk("\nbut task is already holding lock:\n");
  1291. print_lock(prev);
  1292. printk("\nother info that might help us debug this:\n");
  1293. lockdep_print_held_locks(curr);
  1294. printk("\nstack backtrace:\n");
  1295. dump_stack();
  1296. return 0;
  1297. }
  1298. /*
  1299. * Check whether we are holding such a class already.
  1300. *
  1301. * (Note that this has to be done separately, because the graph cannot
  1302. * detect such classes of deadlocks.)
  1303. *
  1304. * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
  1305. */
  1306. static int
  1307. check_deadlock(struct task_struct *curr, struct held_lock *next,
  1308. struct lockdep_map *next_instance, int read)
  1309. {
  1310. struct held_lock *prev;
  1311. struct held_lock *nest = NULL;
  1312. int i;
  1313. for (i = 0; i < curr->lockdep_depth; i++) {
  1314. prev = curr->held_locks + i;
  1315. if (prev->instance == next->nest_lock)
  1316. nest = prev;
  1317. if (hlock_class(prev) != hlock_class(next))
  1318. continue;
  1319. /*
  1320. * Allow read-after-read recursion of the same
  1321. * lock class (i.e. read_lock(lock)+read_lock(lock)):
  1322. */
  1323. if ((read == 2) && prev->read)
  1324. return 2;
  1325. /*
  1326. * We're holding the nest_lock, which serializes this lock's
  1327. * nesting behaviour.
  1328. */
  1329. if (nest)
  1330. return 2;
  1331. return print_deadlock_bug(curr, prev, next);
  1332. }
  1333. return 1;
  1334. }
  1335. /*
  1336. * There was a chain-cache miss, and we are about to add a new dependency
  1337. * to a previous lock. We recursively validate the following rules:
  1338. *
  1339. * - would the adding of the <prev> -> <next> dependency create a
  1340. * circular dependency in the graph? [== circular deadlock]
  1341. *
  1342. * - does the new prev->next dependency connect any hardirq-safe lock
  1343. * (in the full backwards-subgraph starting at <prev>) with any
  1344. * hardirq-unsafe lock (in the full forwards-subgraph starting at
  1345. * <next>)? [== illegal lock inversion with hardirq contexts]
  1346. *
  1347. * - does the new prev->next dependency connect any softirq-safe lock
  1348. * (in the full backwards-subgraph starting at <prev>) with any
  1349. * softirq-unsafe lock (in the full forwards-subgraph starting at
  1350. * <next>)? [== illegal lock inversion with softirq contexts]
  1351. *
  1352. * any of these scenarios could lead to a deadlock.
  1353. *
  1354. * Then if all the validations pass, we add the forwards and backwards
  1355. * dependency.
  1356. */
  1357. static int
  1358. check_prev_add(struct task_struct *curr, struct held_lock *prev,
  1359. struct held_lock *next, int distance)
  1360. {
  1361. struct lock_list *entry;
  1362. int ret;
  1363. struct lock_list this;
  1364. struct lock_list *uninitialized_var(target_entry);
  1365. /*
  1366. * Prove that the new <prev> -> <next> dependency would not
  1367. * create a circular dependency in the graph. (We do this by
  1368. * forward-recursing into the graph starting at <next>, and
  1369. * checking whether we can reach <prev>.)
  1370. *
  1371. * We are using global variables to control the recursion, to
  1372. * keep the stackframe size of the recursive functions low:
  1373. */
  1374. this.class = hlock_class(next);
  1375. this.parent = NULL;
  1376. ret = check_noncircular(&this, hlock_class(prev), &target_entry);
  1377. if (unlikely(!ret))
  1378. return print_circular_bug(&this, target_entry, next, prev);
  1379. else if (unlikely(ret < 0))
  1380. return print_bfs_bug(ret);
  1381. if (!check_prev_add_irq(curr, prev, next))
  1382. return 0;
  1383. /*
  1384. * For recursive read-locks we do all the dependency checks,
  1385. * but we dont store read-triggered dependencies (only
  1386. * write-triggered dependencies). This ensures that only the
  1387. * write-side dependencies matter, and that if for example a
  1388. * write-lock never takes any other locks, then the reads are
  1389. * equivalent to a NOP.
  1390. */
  1391. if (next->read == 2 || prev->read == 2)
  1392. return 1;
  1393. /*
  1394. * Is the <prev> -> <next> dependency already present?
  1395. *
  1396. * (this may occur even though this is a new chain: consider
  1397. * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
  1398. * chains - the second one will be new, but L1 already has
  1399. * L2 added to its dependency list, due to the first chain.)
  1400. */
  1401. list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
  1402. if (entry->class == hlock_class(next)) {
  1403. if (distance == 1)
  1404. entry->distance = 1;
  1405. return 2;
  1406. }
  1407. }
  1408. /*
  1409. * Ok, all validations passed, add the new lock
  1410. * to the previous lock's dependency list:
  1411. */
  1412. ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
  1413. &hlock_class(prev)->locks_after,
  1414. next->acquire_ip, distance);
  1415. if (!ret)
  1416. return 0;
  1417. ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
  1418. &hlock_class(next)->locks_before,
  1419. next->acquire_ip, distance);
  1420. if (!ret)
  1421. return 0;
  1422. /*
  1423. * Debugging printouts:
  1424. */
  1425. if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
  1426. graph_unlock();
  1427. printk("\n new dependency: ");
  1428. print_lock_name(hlock_class(prev));
  1429. printk(" => ");
  1430. print_lock_name(hlock_class(next));
  1431. printk("\n");
  1432. dump_stack();
  1433. return graph_lock();
  1434. }
  1435. return 1;
  1436. }
  1437. /*
  1438. * Add the dependency to all directly-previous locks that are 'relevant'.
  1439. * The ones that are relevant are (in increasing distance from curr):
  1440. * all consecutive trylock entries and the final non-trylock entry - or
  1441. * the end of this context's lock-chain - whichever comes first.
  1442. */
  1443. static int
  1444. check_prevs_add(struct task_struct *curr, struct held_lock *next)
  1445. {
  1446. int depth = curr->lockdep_depth;
  1447. struct held_lock *hlock;
  1448. /*
  1449. * Debugging checks.
  1450. *
  1451. * Depth must not be zero for a non-head lock:
  1452. */
  1453. if (!depth)
  1454. goto out_bug;
  1455. /*
  1456. * At least two relevant locks must exist for this
  1457. * to be a head:
  1458. */
  1459. if (curr->held_locks[depth].irq_context !=
  1460. curr->held_locks[depth-1].irq_context)
  1461. goto out_bug;
  1462. for (;;) {
  1463. int distance = curr->lockdep_depth - depth + 1;
  1464. hlock = curr->held_locks + depth-1;
  1465. /*
  1466. * Only non-recursive-read entries get new dependencies
  1467. * added:
  1468. */
  1469. if (hlock->read != 2) {
  1470. if (!check_prev_add(curr, hlock, next, distance))
  1471. return 0;
  1472. /*
  1473. * Stop after the first non-trylock entry,
  1474. * as non-trylock entries have added their
  1475. * own direct dependencies already, so this
  1476. * lock is connected to them indirectly:
  1477. */
  1478. if (!hlock->trylock)
  1479. break;
  1480. }
  1481. depth--;
  1482. /*
  1483. * End of lock-stack?
  1484. */
  1485. if (!depth)
  1486. break;
  1487. /*
  1488. * Stop the search if we cross into another context:
  1489. */
  1490. if (curr->held_locks[depth].irq_context !=
  1491. curr->held_locks[depth-1].irq_context)
  1492. break;
  1493. }
  1494. return 1;
  1495. out_bug:
  1496. if (!debug_locks_off_graph_unlock())
  1497. return 0;
  1498. WARN_ON(1);
  1499. return 0;
  1500. }
  1501. unsigned long nr_lock_chains;
  1502. struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
  1503. int nr_chain_hlocks;
  1504. static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
  1505. struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
  1506. {
  1507. return lock_classes + chain_hlocks[chain->base + i];
  1508. }
  1509. /*
  1510. * Look up a dependency chain. If the key is not present yet then
  1511. * add it and return 1 - in this case the new dependency chain is
  1512. * validated. If the key is already hashed, return 0.
  1513. * (On return with 1 graph_lock is held.)
  1514. */
  1515. static inline int lookup_chain_cache(struct task_struct *curr,
  1516. struct held_lock *hlock,
  1517. u64 chain_key)
  1518. {
  1519. struct lock_class *class = hlock_class(hlock);
  1520. struct list_head *hash_head = chainhashentry(chain_key);
  1521. struct lock_chain *chain;
  1522. struct held_lock *hlock_curr, *hlock_next;
  1523. int i, j, n, cn;
  1524. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  1525. return 0;
  1526. /*
  1527. * We can walk it lock-free, because entries only get added
  1528. * to the hash:
  1529. */
  1530. list_for_each_entry(chain, hash_head, entry) {
  1531. if (chain->chain_key == chain_key) {
  1532. cache_hit:
  1533. debug_atomic_inc(&chain_lookup_hits);
  1534. if (very_verbose(class))
  1535. printk("\nhash chain already cached, key: "
  1536. "%016Lx tail class: [%p] %s\n",
  1537. (unsigned long long)chain_key,
  1538. class->key, class->name);
  1539. return 0;
  1540. }
  1541. }
  1542. if (very_verbose(class))
  1543. printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
  1544. (unsigned long long)chain_key, class->key, class->name);
  1545. /*
  1546. * Allocate a new chain entry from the static array, and add
  1547. * it to the hash:
  1548. */
  1549. if (!graph_lock())
  1550. return 0;
  1551. /*
  1552. * We have to walk the chain again locked - to avoid duplicates:
  1553. */
  1554. list_for_each_entry(chain, hash_head, entry) {
  1555. if (chain->chain_key == chain_key) {
  1556. graph_unlock();
  1557. goto cache_hit;
  1558. }
  1559. }
  1560. if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
  1561. if (!debug_locks_off_graph_unlock())
  1562. return 0;
  1563. printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
  1564. printk("turning off the locking correctness validator.\n");
  1565. dump_stack();
  1566. return 0;
  1567. }
  1568. chain = lock_chains + nr_lock_chains++;
  1569. chain->chain_key = chain_key;
  1570. chain->irq_context = hlock->irq_context;
  1571. /* Find the first held_lock of current chain */
  1572. hlock_next = hlock;
  1573. for (i = curr->lockdep_depth - 1; i >= 0; i--) {
  1574. hlock_curr = curr->held_locks + i;
  1575. if (hlock_curr->irq_context != hlock_next->irq_context)
  1576. break;
  1577. hlock_next = hlock;
  1578. }
  1579. i++;
  1580. chain->depth = curr->lockdep_depth + 1 - i;
  1581. cn = nr_chain_hlocks;
  1582. while (cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS) {
  1583. n = cmpxchg(&nr_chain_hlocks, cn, cn + chain->depth);
  1584. if (n == cn)
  1585. break;
  1586. cn = n;
  1587. }
  1588. if (likely(cn + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
  1589. chain->base = cn;
  1590. for (j = 0; j < chain->depth - 1; j++, i++) {
  1591. int lock_id = curr->held_locks[i].class_idx - 1;
  1592. chain_hlocks[chain->base + j] = lock_id;
  1593. }
  1594. chain_hlocks[chain->base + j] = class - lock_classes;
  1595. }
  1596. list_add_tail_rcu(&chain->entry, hash_head);
  1597. debug_atomic_inc(&chain_lookup_misses);
  1598. inc_chains();
  1599. return 1;
  1600. }
  1601. static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
  1602. struct held_lock *hlock, int chain_head, u64 chain_key)
  1603. {
  1604. /*
  1605. * Trylock needs to maintain the stack of held locks, but it
  1606. * does not add new dependencies, because trylock can be done
  1607. * in any order.
  1608. *
  1609. * We look up the chain_key and do the O(N^2) check and update of
  1610. * the dependencies only if this is a new dependency chain.
  1611. * (If lookup_chain_cache() returns with 1 it acquires
  1612. * graph_lock for us)
  1613. */
  1614. if (!hlock->trylock && (hlock->check == 2) &&
  1615. lookup_chain_cache(curr, hlock, chain_key)) {
  1616. /*
  1617. * Check whether last held lock:
  1618. *
  1619. * - is irq-safe, if this lock is irq-unsafe
  1620. * - is softirq-safe, if this lock is hardirq-unsafe
  1621. *
  1622. * And check whether the new lock's dependency graph
  1623. * could lead back to the previous lock.
  1624. *
  1625. * any of these scenarios could lead to a deadlock. If
  1626. * All validations
  1627. */
  1628. int ret = check_deadlock(curr, hlock, lock, hlock->read);
  1629. if (!ret)
  1630. return 0;
  1631. /*
  1632. * Mark recursive read, as we jump over it when
  1633. * building dependencies (just like we jump over
  1634. * trylock entries):
  1635. */
  1636. if (ret == 2)
  1637. hlock->read = 2;
  1638. /*
  1639. * Add dependency only if this lock is not the head
  1640. * of the chain, and if it's not a secondary read-lock:
  1641. */
  1642. if (!chain_head && ret != 2)
  1643. if (!check_prevs_add(curr, hlock))
  1644. return 0;
  1645. graph_unlock();
  1646. } else
  1647. /* after lookup_chain_cache(): */
  1648. if (unlikely(!debug_locks))
  1649. return 0;
  1650. return 1;
  1651. }
  1652. #else
  1653. static inline int validate_chain(struct task_struct *curr,
  1654. struct lockdep_map *lock, struct held_lock *hlock,
  1655. int chain_head, u64 chain_key)
  1656. {
  1657. return 1;
  1658. }
  1659. #endif
  1660. /*
  1661. * We are building curr_chain_key incrementally, so double-check
  1662. * it from scratch, to make sure that it's done correctly:
  1663. */
  1664. static void check_chain_key(struct task_struct *curr)
  1665. {
  1666. #ifdef CONFIG_DEBUG_LOCKDEP
  1667. struct held_lock *hlock, *prev_hlock = NULL;
  1668. unsigned int i, id;
  1669. u64 chain_key = 0;
  1670. for (i = 0; i < curr->lockdep_depth; i++) {
  1671. hlock = curr->held_locks + i;
  1672. if (chain_key != hlock->prev_chain_key) {
  1673. debug_locks_off();
  1674. WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
  1675. curr->lockdep_depth, i,
  1676. (unsigned long long)chain_key,
  1677. (unsigned long long)hlock->prev_chain_key);
  1678. return;
  1679. }
  1680. id = hlock->class_idx - 1;
  1681. if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
  1682. return;
  1683. if (prev_hlock && (prev_hlock->irq_context !=
  1684. hlock->irq_context))
  1685. chain_key = 0;
  1686. chain_key = iterate_chain_key(chain_key, id);
  1687. prev_hlock = hlock;
  1688. }
  1689. if (chain_key != curr->curr_chain_key) {
  1690. debug_locks_off();
  1691. WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
  1692. curr->lockdep_depth, i,
  1693. (unsigned long long)chain_key,
  1694. (unsigned long long)curr->curr_chain_key);
  1695. }
  1696. #endif
  1697. }
  1698. static int
  1699. print_usage_bug(struct task_struct *curr, struct held_lock *this,
  1700. enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
  1701. {
  1702. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1703. return 0;
  1704. printk("\n=================================\n");
  1705. printk( "[ INFO: inconsistent lock state ]\n");
  1706. print_kernel_version();
  1707. printk( "---------------------------------\n");
  1708. printk("inconsistent {%s} -> {%s} usage.\n",
  1709. usage_str[prev_bit], usage_str[new_bit]);
  1710. printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
  1711. curr->comm, task_pid_nr(curr),
  1712. trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
  1713. trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
  1714. trace_hardirqs_enabled(curr),
  1715. trace_softirqs_enabled(curr));
  1716. print_lock(this);
  1717. printk("{%s} state was registered at:\n", usage_str[prev_bit]);
  1718. print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
  1719. print_irqtrace_events(curr);
  1720. printk("\nother info that might help us debug this:\n");
  1721. lockdep_print_held_locks(curr);
  1722. printk("\nstack backtrace:\n");
  1723. dump_stack();
  1724. return 0;
  1725. }
  1726. /*
  1727. * Print out an error if an invalid bit is set:
  1728. */
  1729. static inline int
  1730. valid_state(struct task_struct *curr, struct held_lock *this,
  1731. enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
  1732. {
  1733. if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
  1734. return print_usage_bug(curr, this, bad_bit, new_bit);
  1735. return 1;
  1736. }
  1737. static int mark_lock(struct task_struct *curr, struct held_lock *this,
  1738. enum lock_usage_bit new_bit);
  1739. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
  1740. /*
  1741. * print irq inversion bug:
  1742. */
  1743. static int
  1744. print_irq_inversion_bug(struct task_struct *curr,
  1745. struct lock_list *root, struct lock_list *other,
  1746. struct held_lock *this, int forwards,
  1747. const char *irqclass)
  1748. {
  1749. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1750. return 0;
  1751. printk("\n=========================================================\n");
  1752. printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
  1753. print_kernel_version();
  1754. printk( "---------------------------------------------------------\n");
  1755. printk("%s/%d just changed the state of lock:\n",
  1756. curr->comm, task_pid_nr(curr));
  1757. print_lock(this);
  1758. if (forwards)
  1759. printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
  1760. else
  1761. printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
  1762. print_lock_name(other->class);
  1763. printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
  1764. printk("\nother info that might help us debug this:\n");
  1765. lockdep_print_held_locks(curr);
  1766. printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
  1767. if (!save_trace(&root->trace))
  1768. return 0;
  1769. print_shortest_lock_dependencies(other, root);
  1770. printk("\nstack backtrace:\n");
  1771. dump_stack();
  1772. return 0;
  1773. }
  1774. /*
  1775. * Prove that in the forwards-direction subgraph starting at <this>
  1776. * there is no lock matching <mask>:
  1777. */
  1778. static int
  1779. check_usage_forwards(struct task_struct *curr, struct held_lock *this,
  1780. enum lock_usage_bit bit, const char *irqclass)
  1781. {
  1782. int ret;
  1783. struct lock_list root;
  1784. struct lock_list *uninitialized_var(target_entry);
  1785. root.parent = NULL;
  1786. root.class = hlock_class(this);
  1787. ret = find_usage_forwards(&root, bit, &target_entry);
  1788. if (ret < 0)
  1789. return print_bfs_bug(ret);
  1790. if (ret == 1)
  1791. return ret;
  1792. return print_irq_inversion_bug(curr, &root, target_entry,
  1793. this, 1, irqclass);
  1794. }
  1795. /*
  1796. * Prove that in the backwards-direction subgraph starting at <this>
  1797. * there is no lock matching <mask>:
  1798. */
  1799. static int
  1800. check_usage_backwards(struct task_struct *curr, struct held_lock *this,
  1801. enum lock_usage_bit bit, const char *irqclass)
  1802. {
  1803. int ret;
  1804. struct lock_list root;
  1805. struct lock_list *uninitialized_var(target_entry);
  1806. root.parent = NULL;
  1807. root.class = hlock_class(this);
  1808. ret = find_usage_backwards(&root, bit, &target_entry);
  1809. if (ret < 0)
  1810. return print_bfs_bug(ret);
  1811. if (ret == 1)
  1812. return ret;
  1813. return print_irq_inversion_bug(curr, &root, target_entry,
  1814. this, 1, irqclass);
  1815. }
  1816. void print_irqtrace_events(struct task_struct *curr)
  1817. {
  1818. printk("irq event stamp: %u\n", curr->irq_events);
  1819. printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
  1820. print_ip_sym(curr->hardirq_enable_ip);
  1821. printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
  1822. print_ip_sym(curr->hardirq_disable_ip);
  1823. printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
  1824. print_ip_sym(curr->softirq_enable_ip);
  1825. printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
  1826. print_ip_sym(curr->softirq_disable_ip);
  1827. }
  1828. static int HARDIRQ_verbose(struct lock_class *class)
  1829. {
  1830. #if HARDIRQ_VERBOSE
  1831. return class_filter(class);
  1832. #endif
  1833. return 0;
  1834. }
  1835. static int SOFTIRQ_verbose(struct lock_class *class)
  1836. {
  1837. #if SOFTIRQ_VERBOSE
  1838. return class_filter(class);
  1839. #endif
  1840. return 0;
  1841. }
  1842. static int RECLAIM_FS_verbose(struct lock_class *class)
  1843. {
  1844. #if RECLAIM_VERBOSE
  1845. return class_filter(class);
  1846. #endif
  1847. return 0;
  1848. }
  1849. #define STRICT_READ_CHECKS 1
  1850. static int (*state_verbose_f[])(struct lock_class *class) = {
  1851. #define LOCKDEP_STATE(__STATE) \
  1852. __STATE##_verbose,
  1853. #include "lockdep_states.h"
  1854. #undef LOCKDEP_STATE
  1855. };
  1856. static inline int state_verbose(enum lock_usage_bit bit,
  1857. struct lock_class *class)
  1858. {
  1859. return state_verbose_f[bit >> 2](class);
  1860. }
  1861. typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
  1862. enum lock_usage_bit bit, const char *name);
  1863. static int
  1864. mark_lock_irq(struct task_struct *curr, struct held_lock *this,
  1865. enum lock_usage_bit new_bit)
  1866. {
  1867. int excl_bit = exclusive_bit(new_bit);
  1868. int read = new_bit & 1;
  1869. int dir = new_bit & 2;
  1870. /*
  1871. * mark USED_IN has to look forwards -- to ensure no dependency
  1872. * has ENABLED state, which would allow recursion deadlocks.
  1873. *
  1874. * mark ENABLED has to look backwards -- to ensure no dependee
  1875. * has USED_IN state, which, again, would allow recursion deadlocks.
  1876. */
  1877. check_usage_f usage = dir ?
  1878. check_usage_backwards : check_usage_forwards;
  1879. /*
  1880. * Validate that this particular lock does not have conflicting
  1881. * usage states.
  1882. */
  1883. if (!valid_state(curr, this, new_bit, excl_bit))
  1884. return 0;
  1885. /*
  1886. * Validate that the lock dependencies don't have conflicting usage
  1887. * states.
  1888. */
  1889. if ((!read || !dir || STRICT_READ_CHECKS) &&
  1890. !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
  1891. return 0;
  1892. /*
  1893. * Check for read in write conflicts
  1894. */
  1895. if (!read) {
  1896. if (!valid_state(curr, this, new_bit, excl_bit + 1))
  1897. return 0;
  1898. if (STRICT_READ_CHECKS &&
  1899. !usage(curr, this, excl_bit + 1,
  1900. state_name(new_bit + 1)))
  1901. return 0;
  1902. }
  1903. if (state_verbose(new_bit, hlock_class(this)))
  1904. return 2;
  1905. return 1;
  1906. }
  1907. enum mark_type {
  1908. #define LOCKDEP_STATE(__STATE) __STATE,
  1909. #include "lockdep_states.h"
  1910. #undef LOCKDEP_STATE
  1911. };
  1912. /*
  1913. * Mark all held locks with a usage bit:
  1914. */
  1915. static int
  1916. mark_held_locks(struct task_struct *curr, enum mark_type mark)
  1917. {
  1918. enum lock_usage_bit usage_bit;
  1919. struct held_lock *hlock;
  1920. int i;
  1921. for (i = 0; i < curr->lockdep_depth; i++) {
  1922. hlock = curr->held_locks + i;
  1923. usage_bit = 2 + (mark << 2); /* ENABLED */
  1924. if (hlock->read)
  1925. usage_bit += 1; /* READ */
  1926. BUG_ON(usage_bit >= LOCK_USAGE_STATES);
  1927. if (!mark_lock(curr, hlock, usage_bit))
  1928. return 0;
  1929. }
  1930. return 1;
  1931. }
  1932. /*
  1933. * Debugging helper: via this flag we know that we are in
  1934. * 'early bootup code', and will warn about any invalid irqs-on event:
  1935. */
  1936. static int early_boot_irqs_enabled;
  1937. void early_boot_irqs_off(void)
  1938. {
  1939. early_boot_irqs_enabled = 0;
  1940. }
  1941. void early_boot_irqs_on(void)
  1942. {
  1943. early_boot_irqs_enabled = 1;
  1944. }
  1945. /*
  1946. * Hardirqs will be enabled:
  1947. */
  1948. void trace_hardirqs_on_caller(unsigned long ip)
  1949. {
  1950. struct task_struct *curr = current;
  1951. time_hardirqs_on(CALLER_ADDR0, ip);
  1952. if (unlikely(!debug_locks || current->lockdep_recursion))
  1953. return;
  1954. if (DEBUG_LOCKS_WARN_ON(unlikely(!early_boot_irqs_enabled)))
  1955. return;
  1956. if (unlikely(curr->hardirqs_enabled)) {
  1957. debug_atomic_inc(&redundant_hardirqs_on);
  1958. return;
  1959. }
  1960. /* we'll do an OFF -> ON transition: */
  1961. curr->hardirqs_enabled = 1;
  1962. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  1963. return;
  1964. if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
  1965. return;
  1966. /*
  1967. * We are going to turn hardirqs on, so set the
  1968. * usage bit for all held locks:
  1969. */
  1970. if (!mark_held_locks(curr, HARDIRQ))
  1971. return;
  1972. /*
  1973. * If we have softirqs enabled, then set the usage
  1974. * bit for all held locks. (disabled hardirqs prevented
  1975. * this bit from being set before)
  1976. */
  1977. if (curr->softirqs_enabled)
  1978. if (!mark_held_locks(curr, SOFTIRQ))
  1979. return;
  1980. curr->hardirq_enable_ip = ip;
  1981. curr->hardirq_enable_event = ++curr->irq_events;
  1982. debug_atomic_inc(&hardirqs_on_events);
  1983. }
  1984. EXPORT_SYMBOL(trace_hardirqs_on_caller);
  1985. void trace_hardirqs_on(void)
  1986. {
  1987. trace_hardirqs_on_caller(CALLER_ADDR0);
  1988. }
  1989. EXPORT_SYMBOL(trace_hardirqs_on);
  1990. /*
  1991. * Hardirqs were disabled:
  1992. */
  1993. void trace_hardirqs_off_caller(unsigned long ip)
  1994. {
  1995. struct task_struct *curr = current;
  1996. time_hardirqs_off(CALLER_ADDR0, ip);
  1997. if (unlikely(!debug_locks || current->lockdep_recursion))
  1998. return;
  1999. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2000. return;
  2001. if (curr->hardirqs_enabled) {
  2002. /*
  2003. * We have done an ON -> OFF transition:
  2004. */
  2005. curr->hardirqs_enabled = 0;
  2006. curr->hardirq_disable_ip = ip;
  2007. curr->hardirq_disable_event = ++curr->irq_events;
  2008. debug_atomic_inc(&hardirqs_off_events);
  2009. } else
  2010. debug_atomic_inc(&redundant_hardirqs_off);
  2011. }
  2012. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  2013. void trace_hardirqs_off(void)
  2014. {
  2015. trace_hardirqs_off_caller(CALLER_ADDR0);
  2016. }
  2017. EXPORT_SYMBOL(trace_hardirqs_off);
  2018. /*
  2019. * Softirqs will be enabled:
  2020. */
  2021. void trace_softirqs_on(unsigned long ip)
  2022. {
  2023. struct task_struct *curr = current;
  2024. if (unlikely(!debug_locks))
  2025. return;
  2026. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2027. return;
  2028. if (curr->softirqs_enabled) {
  2029. debug_atomic_inc(&redundant_softirqs_on);
  2030. return;
  2031. }
  2032. /*
  2033. * We'll do an OFF -> ON transition:
  2034. */
  2035. curr->softirqs_enabled = 1;
  2036. curr->softirq_enable_ip = ip;
  2037. curr->softirq_enable_event = ++curr->irq_events;
  2038. debug_atomic_inc(&softirqs_on_events);
  2039. /*
  2040. * We are going to turn softirqs on, so set the
  2041. * usage bit for all held locks, if hardirqs are
  2042. * enabled too:
  2043. */
  2044. if (curr->hardirqs_enabled)
  2045. mark_held_locks(curr, SOFTIRQ);
  2046. }
  2047. /*
  2048. * Softirqs were disabled:
  2049. */
  2050. void trace_softirqs_off(unsigned long ip)
  2051. {
  2052. struct task_struct *curr = current;
  2053. if (unlikely(!debug_locks))
  2054. return;
  2055. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2056. return;
  2057. if (curr->softirqs_enabled) {
  2058. /*
  2059. * We have done an ON -> OFF transition:
  2060. */
  2061. curr->softirqs_enabled = 0;
  2062. curr->softirq_disable_ip = ip;
  2063. curr->softirq_disable_event = ++curr->irq_events;
  2064. debug_atomic_inc(&softirqs_off_events);
  2065. DEBUG_LOCKS_WARN_ON(!softirq_count());
  2066. } else
  2067. debug_atomic_inc(&redundant_softirqs_off);
  2068. }
  2069. static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
  2070. {
  2071. struct task_struct *curr = current;
  2072. if (unlikely(!debug_locks))
  2073. return;
  2074. /* no reclaim without waiting on it */
  2075. if (!(gfp_mask & __GFP_WAIT))
  2076. return;
  2077. /* this guy won't enter reclaim */
  2078. if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
  2079. return;
  2080. /* We're only interested __GFP_FS allocations for now */
  2081. if (!(gfp_mask & __GFP_FS))
  2082. return;
  2083. if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
  2084. return;
  2085. mark_held_locks(curr, RECLAIM_FS);
  2086. }
  2087. static void check_flags(unsigned long flags);
  2088. void lockdep_trace_alloc(gfp_t gfp_mask)
  2089. {
  2090. unsigned long flags;
  2091. if (unlikely(current->lockdep_recursion))
  2092. return;
  2093. raw_local_irq_save(flags);
  2094. check_flags(flags);
  2095. current->lockdep_recursion = 1;
  2096. __lockdep_trace_alloc(gfp_mask, flags);
  2097. current->lockdep_recursion = 0;
  2098. raw_local_irq_restore(flags);
  2099. }
  2100. static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
  2101. {
  2102. /*
  2103. * If non-trylock use in a hardirq or softirq context, then
  2104. * mark the lock as used in these contexts:
  2105. */
  2106. if (!hlock->trylock) {
  2107. if (hlock->read) {
  2108. if (curr->hardirq_context)
  2109. if (!mark_lock(curr, hlock,
  2110. LOCK_USED_IN_HARDIRQ_READ))
  2111. return 0;
  2112. if (curr->softirq_context)
  2113. if (!mark_lock(curr, hlock,
  2114. LOCK_USED_IN_SOFTIRQ_READ))
  2115. return 0;
  2116. } else {
  2117. if (curr->hardirq_context)
  2118. if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
  2119. return 0;
  2120. if (curr->softirq_context)
  2121. if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
  2122. return 0;
  2123. }
  2124. }
  2125. if (!hlock->hardirqs_off) {
  2126. if (hlock->read) {
  2127. if (!mark_lock(curr, hlock,
  2128. LOCK_ENABLED_HARDIRQ_READ))
  2129. return 0;
  2130. if (curr->softirqs_enabled)
  2131. if (!mark_lock(curr, hlock,
  2132. LOCK_ENABLED_SOFTIRQ_READ))
  2133. return 0;
  2134. } else {
  2135. if (!mark_lock(curr, hlock,
  2136. LOCK_ENABLED_HARDIRQ))
  2137. return 0;
  2138. if (curr->softirqs_enabled)
  2139. if (!mark_lock(curr, hlock,
  2140. LOCK_ENABLED_SOFTIRQ))
  2141. return 0;
  2142. }
  2143. }
  2144. /*
  2145. * We reuse the irq context infrastructure more broadly as a general
  2146. * context checking code. This tests GFP_FS recursion (a lock taken
  2147. * during reclaim for a GFP_FS allocation is held over a GFP_FS
  2148. * allocation).
  2149. */
  2150. if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
  2151. if (hlock->read) {
  2152. if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
  2153. return 0;
  2154. } else {
  2155. if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
  2156. return 0;
  2157. }
  2158. }
  2159. return 1;
  2160. }
  2161. static int separate_irq_context(struct task_struct *curr,
  2162. struct held_lock *hlock)
  2163. {
  2164. unsigned int depth = curr->lockdep_depth;
  2165. /*
  2166. * Keep track of points where we cross into an interrupt context:
  2167. */
  2168. hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
  2169. curr->softirq_context;
  2170. if (depth) {
  2171. struct held_lock *prev_hlock;
  2172. prev_hlock = curr->held_locks + depth-1;
  2173. /*
  2174. * If we cross into another context, reset the
  2175. * hash key (this also prevents the checking and the
  2176. * adding of the dependency to 'prev'):
  2177. */
  2178. if (prev_hlock->irq_context != hlock->irq_context)
  2179. return 1;
  2180. }
  2181. return 0;
  2182. }
  2183. #else
  2184. static inline
  2185. int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
  2186. enum lock_usage_bit new_bit)
  2187. {
  2188. WARN_ON(1);
  2189. return 1;
  2190. }
  2191. static inline int mark_irqflags(struct task_struct *curr,
  2192. struct held_lock *hlock)
  2193. {
  2194. return 1;
  2195. }
  2196. static inline int separate_irq_context(struct task_struct *curr,
  2197. struct held_lock *hlock)
  2198. {
  2199. return 0;
  2200. }
  2201. void lockdep_trace_alloc(gfp_t gfp_mask)
  2202. {
  2203. }
  2204. #endif
  2205. /*
  2206. * Mark a lock with a usage bit, and validate the state transition:
  2207. */
  2208. static int mark_lock(struct task_struct *curr, struct held_lock *this,
  2209. enum lock_usage_bit new_bit)
  2210. {
  2211. unsigned int new_mask = 1 << new_bit, ret = 1;
  2212. /*
  2213. * If already set then do not dirty the cacheline,
  2214. * nor do any checks:
  2215. */
  2216. if (likely(hlock_class(this)->usage_mask & new_mask))
  2217. return 1;
  2218. if (!graph_lock())
  2219. return 0;
  2220. /*
  2221. * Make sure we didnt race:
  2222. */
  2223. if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
  2224. graph_unlock();
  2225. return 1;
  2226. }
  2227. hlock_class(this)->usage_mask |= new_mask;
  2228. if (!save_trace(hlock_class(this)->usage_traces + new_bit))
  2229. return 0;
  2230. switch (new_bit) {
  2231. #define LOCKDEP_STATE(__STATE) \
  2232. case LOCK_USED_IN_##__STATE: \
  2233. case LOCK_USED_IN_##__STATE##_READ: \
  2234. case LOCK_ENABLED_##__STATE: \
  2235. case LOCK_ENABLED_##__STATE##_READ:
  2236. #include "lockdep_states.h"
  2237. #undef LOCKDEP_STATE
  2238. ret = mark_lock_irq(curr, this, new_bit);
  2239. if (!ret)
  2240. return 0;
  2241. break;
  2242. case LOCK_USED:
  2243. debug_atomic_dec(&nr_unused_locks);
  2244. break;
  2245. default:
  2246. if (!debug_locks_off_graph_unlock())
  2247. return 0;
  2248. WARN_ON(1);
  2249. return 0;
  2250. }
  2251. graph_unlock();
  2252. /*
  2253. * We must printk outside of the graph_lock:
  2254. */
  2255. if (ret == 2) {
  2256. printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
  2257. print_lock(this);
  2258. print_irqtrace_events(curr);
  2259. dump_stack();
  2260. }
  2261. return ret;
  2262. }
  2263. /*
  2264. * Initialize a lock instance's lock-class mapping info:
  2265. */
  2266. void lockdep_init_map(struct lockdep_map *lock, const char *name,
  2267. struct lock_class_key *key, int subclass)
  2268. {
  2269. lock->class_cache = NULL;
  2270. #ifdef CONFIG_LOCK_STAT
  2271. lock->cpu = raw_smp_processor_id();
  2272. #endif
  2273. if (DEBUG_LOCKS_WARN_ON(!name)) {
  2274. lock->name = "NULL";
  2275. return;
  2276. }
  2277. lock->name = name;
  2278. if (DEBUG_LOCKS_WARN_ON(!key))
  2279. return;
  2280. /*
  2281. * Sanity check, the lock-class key must be persistent:
  2282. */
  2283. if (!static_obj(key)) {
  2284. printk("BUG: key %p not in .data!\n", key);
  2285. DEBUG_LOCKS_WARN_ON(1);
  2286. return;
  2287. }
  2288. lock->key = key;
  2289. if (unlikely(!debug_locks))
  2290. return;
  2291. if (subclass)
  2292. register_lock_class(lock, subclass, 1);
  2293. }
  2294. EXPORT_SYMBOL_GPL(lockdep_init_map);
  2295. /*
  2296. * This gets called for every mutex_lock*()/spin_lock*() operation.
  2297. * We maintain the dependency maps and validate the locking attempt:
  2298. */
  2299. static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  2300. int trylock, int read, int check, int hardirqs_off,
  2301. struct lockdep_map *nest_lock, unsigned long ip,
  2302. int references)
  2303. {
  2304. struct task_struct *curr = current;
  2305. struct lock_class *class = NULL;
  2306. struct held_lock *hlock;
  2307. unsigned int depth, id;
  2308. int chain_head = 0;
  2309. int class_idx;
  2310. u64 chain_key;
  2311. if (!prove_locking)
  2312. check = 1;
  2313. if (unlikely(!debug_locks))
  2314. return 0;
  2315. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2316. return 0;
  2317. if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
  2318. debug_locks_off();
  2319. printk("BUG: MAX_LOCKDEP_SUBCLASSES too low!\n");
  2320. printk("turning off the locking correctness validator.\n");
  2321. dump_stack();
  2322. return 0;
  2323. }
  2324. if (!subclass)
  2325. class = lock->class_cache;
  2326. /*
  2327. * Not cached yet or subclass?
  2328. */
  2329. if (unlikely(!class)) {
  2330. class = register_lock_class(lock, subclass, 0);
  2331. if (!class)
  2332. return 0;
  2333. }
  2334. debug_atomic_inc((atomic_t *)&class->ops);
  2335. if (very_verbose(class)) {
  2336. printk("\nacquire class [%p] %s", class->key, class->name);
  2337. if (class->name_version > 1)
  2338. printk("#%d", class->name_version);
  2339. printk("\n");
  2340. dump_stack();
  2341. }
  2342. /*
  2343. * Add the lock to the list of currently held locks.
  2344. * (we dont increase the depth just yet, up until the
  2345. * dependency checks are done)
  2346. */
  2347. depth = curr->lockdep_depth;
  2348. if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
  2349. return 0;
  2350. class_idx = class - lock_classes + 1;
  2351. if (depth) {
  2352. hlock = curr->held_locks + depth - 1;
  2353. if (hlock->class_idx == class_idx && nest_lock) {
  2354. if (hlock->references)
  2355. hlock->references++;
  2356. else
  2357. hlock->references = 2;
  2358. return 1;
  2359. }
  2360. }
  2361. hlock = curr->held_locks + depth;
  2362. if (DEBUG_LOCKS_WARN_ON(!class))
  2363. return 0;
  2364. hlock->class_idx = class_idx;
  2365. hlock->acquire_ip = ip;
  2366. hlock->instance = lock;
  2367. hlock->nest_lock = nest_lock;
  2368. hlock->trylock = trylock;
  2369. hlock->read = read;
  2370. hlock->check = check;
  2371. hlock->hardirqs_off = !!hardirqs_off;
  2372. hlock->references = references;
  2373. #ifdef CONFIG_LOCK_STAT
  2374. hlock->waittime_stamp = 0;
  2375. hlock->holdtime_stamp = sched_clock();
  2376. #endif
  2377. if (check == 2 && !mark_irqflags(curr, hlock))
  2378. return 0;
  2379. /* mark it as used: */
  2380. if (!mark_lock(curr, hlock, LOCK_USED))
  2381. return 0;
  2382. /*
  2383. * Calculate the chain hash: it's the combined hash of all the
  2384. * lock keys along the dependency chain. We save the hash value
  2385. * at every step so that we can get the current hash easily
  2386. * after unlock. The chain hash is then used to cache dependency
  2387. * results.
  2388. *
  2389. * The 'key ID' is what is the most compact key value to drive
  2390. * the hash, not class->key.
  2391. */
  2392. id = class - lock_classes;
  2393. if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
  2394. return 0;
  2395. chain_key = curr->curr_chain_key;
  2396. if (!depth) {
  2397. if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
  2398. return 0;
  2399. chain_head = 1;
  2400. }
  2401. hlock->prev_chain_key = chain_key;
  2402. if (separate_irq_context(curr, hlock)) {
  2403. chain_key = 0;
  2404. chain_head = 1;
  2405. }
  2406. chain_key = iterate_chain_key(chain_key, id);
  2407. if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
  2408. return 0;
  2409. curr->curr_chain_key = chain_key;
  2410. curr->lockdep_depth++;
  2411. check_chain_key(curr);
  2412. #ifdef CONFIG_DEBUG_LOCKDEP
  2413. if (unlikely(!debug_locks))
  2414. return 0;
  2415. #endif
  2416. if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
  2417. debug_locks_off();
  2418. printk("BUG: MAX_LOCK_DEPTH too low!\n");
  2419. printk("turning off the locking correctness validator.\n");
  2420. dump_stack();
  2421. return 0;
  2422. }
  2423. if (unlikely(curr->lockdep_depth > max_lockdep_depth))
  2424. max_lockdep_depth = curr->lockdep_depth;
  2425. return 1;
  2426. }
  2427. static int
  2428. print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
  2429. unsigned long ip)
  2430. {
  2431. if (!debug_locks_off())
  2432. return 0;
  2433. if (debug_locks_silent)
  2434. return 0;
  2435. printk("\n=====================================\n");
  2436. printk( "[ BUG: bad unlock balance detected! ]\n");
  2437. printk( "-------------------------------------\n");
  2438. printk("%s/%d is trying to release lock (",
  2439. curr->comm, task_pid_nr(curr));
  2440. print_lockdep_cache(lock);
  2441. printk(") at:\n");
  2442. print_ip_sym(ip);
  2443. printk("but there are no more locks to release!\n");
  2444. printk("\nother info that might help us debug this:\n");
  2445. lockdep_print_held_locks(curr);
  2446. printk("\nstack backtrace:\n");
  2447. dump_stack();
  2448. return 0;
  2449. }
  2450. /*
  2451. * Common debugging checks for both nested and non-nested unlock:
  2452. */
  2453. static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
  2454. unsigned long ip)
  2455. {
  2456. if (unlikely(!debug_locks))
  2457. return 0;
  2458. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2459. return 0;
  2460. if (curr->lockdep_depth <= 0)
  2461. return print_unlock_inbalance_bug(curr, lock, ip);
  2462. return 1;
  2463. }
  2464. static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
  2465. {
  2466. if (hlock->instance == lock)
  2467. return 1;
  2468. if (hlock->references) {
  2469. struct lock_class *class = lock->class_cache;
  2470. if (!class)
  2471. class = look_up_lock_class(lock, 0);
  2472. if (DEBUG_LOCKS_WARN_ON(!class))
  2473. return 0;
  2474. if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
  2475. return 0;
  2476. if (hlock->class_idx == class - lock_classes + 1)
  2477. return 1;
  2478. }
  2479. return 0;
  2480. }
  2481. static int
  2482. __lock_set_class(struct lockdep_map *lock, const char *name,
  2483. struct lock_class_key *key, unsigned int subclass,
  2484. unsigned long ip)
  2485. {
  2486. struct task_struct *curr = current;
  2487. struct held_lock *hlock, *prev_hlock;
  2488. struct lock_class *class;
  2489. unsigned int depth;
  2490. int i;
  2491. depth = curr->lockdep_depth;
  2492. if (DEBUG_LOCKS_WARN_ON(!depth))
  2493. return 0;
  2494. prev_hlock = NULL;
  2495. for (i = depth-1; i >= 0; i--) {
  2496. hlock = curr->held_locks + i;
  2497. /*
  2498. * We must not cross into another context:
  2499. */
  2500. if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
  2501. break;
  2502. if (match_held_lock(hlock, lock))
  2503. goto found_it;
  2504. prev_hlock = hlock;
  2505. }
  2506. return print_unlock_inbalance_bug(curr, lock, ip);
  2507. found_it:
  2508. lockdep_init_map(lock, name, key, 0);
  2509. class = register_lock_class(lock, subclass, 0);
  2510. hlock->class_idx = class - lock_classes + 1;
  2511. curr->lockdep_depth = i;
  2512. curr->curr_chain_key = hlock->prev_chain_key;
  2513. for (; i < depth; i++) {
  2514. hlock = curr->held_locks + i;
  2515. if (!__lock_acquire(hlock->instance,
  2516. hlock_class(hlock)->subclass, hlock->trylock,
  2517. hlock->read, hlock->check, hlock->hardirqs_off,
  2518. hlock->nest_lock, hlock->acquire_ip,
  2519. hlock->references))
  2520. return 0;
  2521. }
  2522. if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
  2523. return 0;
  2524. return 1;
  2525. }
  2526. /*
  2527. * Remove the lock to the list of currently held locks in a
  2528. * potentially non-nested (out of order) manner. This is a
  2529. * relatively rare operation, as all the unlock APIs default
  2530. * to nested mode (which uses lock_release()):
  2531. */
  2532. static int
  2533. lock_release_non_nested(struct task_struct *curr,
  2534. struct lockdep_map *lock, unsigned long ip)
  2535. {
  2536. struct held_lock *hlock, *prev_hlock;
  2537. unsigned int depth;
  2538. int i;
  2539. /*
  2540. * Check whether the lock exists in the current stack
  2541. * of held locks:
  2542. */
  2543. depth = curr->lockdep_depth;
  2544. if (DEBUG_LOCKS_WARN_ON(!depth))
  2545. return 0;
  2546. prev_hlock = NULL;
  2547. for (i = depth-1; i >= 0; i--) {
  2548. hlock = curr->held_locks + i;
  2549. /*
  2550. * We must not cross into another context:
  2551. */
  2552. if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
  2553. break;
  2554. if (match_held_lock(hlock, lock))
  2555. goto found_it;
  2556. prev_hlock = hlock;
  2557. }
  2558. return print_unlock_inbalance_bug(curr, lock, ip);
  2559. found_it:
  2560. if (hlock->instance == lock)
  2561. lock_release_holdtime(hlock);
  2562. if (hlock->references) {
  2563. hlock->references--;
  2564. if (hlock->references) {
  2565. /*
  2566. * We had, and after removing one, still have
  2567. * references, the current lock stack is still
  2568. * valid. We're done!
  2569. */
  2570. return 1;
  2571. }
  2572. }
  2573. /*
  2574. * We have the right lock to unlock, 'hlock' points to it.
  2575. * Now we remove it from the stack, and add back the other
  2576. * entries (if any), recalculating the hash along the way:
  2577. */
  2578. curr->lockdep_depth = i;
  2579. curr->curr_chain_key = hlock->prev_chain_key;
  2580. for (i++; i < depth; i++) {
  2581. hlock = curr->held_locks + i;
  2582. if (!__lock_acquire(hlock->instance,
  2583. hlock_class(hlock)->subclass, hlock->trylock,
  2584. hlock->read, hlock->check, hlock->hardirqs_off,
  2585. hlock->nest_lock, hlock->acquire_ip,
  2586. hlock->references))
  2587. return 0;
  2588. }
  2589. if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
  2590. return 0;
  2591. return 1;
  2592. }
  2593. /*
  2594. * Remove the lock to the list of currently held locks - this gets
  2595. * called on mutex_unlock()/spin_unlock*() (or on a failed
  2596. * mutex_lock_interruptible()). This is done for unlocks that nest
  2597. * perfectly. (i.e. the current top of the lock-stack is unlocked)
  2598. */
  2599. static int lock_release_nested(struct task_struct *curr,
  2600. struct lockdep_map *lock, unsigned long ip)
  2601. {
  2602. struct held_lock *hlock;
  2603. unsigned int depth;
  2604. /*
  2605. * Pop off the top of the lock stack:
  2606. */
  2607. depth = curr->lockdep_depth - 1;
  2608. hlock = curr->held_locks + depth;
  2609. /*
  2610. * Is the unlock non-nested:
  2611. */
  2612. if (hlock->instance != lock || hlock->references)
  2613. return lock_release_non_nested(curr, lock, ip);
  2614. curr->lockdep_depth--;
  2615. if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
  2616. return 0;
  2617. curr->curr_chain_key = hlock->prev_chain_key;
  2618. lock_release_holdtime(hlock);
  2619. #ifdef CONFIG_DEBUG_LOCKDEP
  2620. hlock->prev_chain_key = 0;
  2621. hlock->class_idx = 0;
  2622. hlock->acquire_ip = 0;
  2623. hlock->irq_context = 0;
  2624. #endif
  2625. return 1;
  2626. }
  2627. /*
  2628. * Remove the lock to the list of currently held locks - this gets
  2629. * called on mutex_unlock()/spin_unlock*() (or on a failed
  2630. * mutex_lock_interruptible()). This is done for unlocks that nest
  2631. * perfectly. (i.e. the current top of the lock-stack is unlocked)
  2632. */
  2633. static void
  2634. __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
  2635. {
  2636. struct task_struct *curr = current;
  2637. if (!check_unlock(curr, lock, ip))
  2638. return;
  2639. if (nested) {
  2640. if (!lock_release_nested(curr, lock, ip))
  2641. return;
  2642. } else {
  2643. if (!lock_release_non_nested(curr, lock, ip))
  2644. return;
  2645. }
  2646. check_chain_key(curr);
  2647. }
  2648. static int __lock_is_held(struct lockdep_map *lock)
  2649. {
  2650. struct task_struct *curr = current;
  2651. int i;
  2652. for (i = 0; i < curr->lockdep_depth; i++) {
  2653. struct held_lock *hlock = curr->held_locks + i;
  2654. if (match_held_lock(hlock, lock))
  2655. return 1;
  2656. }
  2657. return 0;
  2658. }
  2659. /*
  2660. * Check whether we follow the irq-flags state precisely:
  2661. */
  2662. static void check_flags(unsigned long flags)
  2663. {
  2664. #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
  2665. defined(CONFIG_TRACE_IRQFLAGS)
  2666. if (!debug_locks)
  2667. return;
  2668. if (irqs_disabled_flags(flags)) {
  2669. if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
  2670. printk("possible reason: unannotated irqs-off.\n");
  2671. }
  2672. } else {
  2673. if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
  2674. printk("possible reason: unannotated irqs-on.\n");
  2675. }
  2676. }
  2677. /*
  2678. * We dont accurately track softirq state in e.g.
  2679. * hardirq contexts (such as on 4KSTACKS), so only
  2680. * check if not in hardirq contexts:
  2681. */
  2682. if (!hardirq_count()) {
  2683. if (softirq_count())
  2684. DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
  2685. else
  2686. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  2687. }
  2688. if (!debug_locks)
  2689. print_irqtrace_events(current);
  2690. #endif
  2691. }
  2692. void lock_set_class(struct lockdep_map *lock, const char *name,
  2693. struct lock_class_key *key, unsigned int subclass,
  2694. unsigned long ip)
  2695. {
  2696. unsigned long flags;
  2697. if (unlikely(current->lockdep_recursion))
  2698. return;
  2699. raw_local_irq_save(flags);
  2700. current->lockdep_recursion = 1;
  2701. check_flags(flags);
  2702. if (__lock_set_class(lock, name, key, subclass, ip))
  2703. check_chain_key(current);
  2704. current->lockdep_recursion = 0;
  2705. raw_local_irq_restore(flags);
  2706. }
  2707. EXPORT_SYMBOL_GPL(lock_set_class);
  2708. /*
  2709. * We are not always called with irqs disabled - do that here,
  2710. * and also avoid lockdep recursion:
  2711. */
  2712. void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  2713. int trylock, int read, int check,
  2714. struct lockdep_map *nest_lock, unsigned long ip)
  2715. {
  2716. unsigned long flags;
  2717. trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
  2718. if (unlikely(current->lockdep_recursion))
  2719. return;
  2720. raw_local_irq_save(flags);
  2721. check_flags(flags);
  2722. current->lockdep_recursion = 1;
  2723. __lock_acquire(lock, subclass, trylock, read, check,
  2724. irqs_disabled_flags(flags), nest_lock, ip, 0);
  2725. current->lockdep_recursion = 0;
  2726. raw_local_irq_restore(flags);
  2727. }
  2728. EXPORT_SYMBOL_GPL(lock_acquire);
  2729. void lock_release(struct lockdep_map *lock, int nested,
  2730. unsigned long ip)
  2731. {
  2732. unsigned long flags;
  2733. trace_lock_release(lock, nested, ip);
  2734. if (unlikely(current->lockdep_recursion))
  2735. return;
  2736. raw_local_irq_save(flags);
  2737. check_flags(flags);
  2738. current->lockdep_recursion = 1;
  2739. __lock_release(lock, nested, ip);
  2740. current->lockdep_recursion = 0;
  2741. raw_local_irq_restore(flags);
  2742. }
  2743. EXPORT_SYMBOL_GPL(lock_release);
  2744. int lock_is_held(struct lockdep_map *lock)
  2745. {
  2746. unsigned long flags;
  2747. int ret = 0;
  2748. if (unlikely(current->lockdep_recursion))
  2749. return ret;
  2750. raw_local_irq_save(flags);
  2751. check_flags(flags);
  2752. current->lockdep_recursion = 1;
  2753. ret = __lock_is_held(lock);
  2754. current->lockdep_recursion = 0;
  2755. raw_local_irq_restore(flags);
  2756. return ret;
  2757. }
  2758. EXPORT_SYMBOL_GPL(lock_is_held);
  2759. void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
  2760. {
  2761. current->lockdep_reclaim_gfp = gfp_mask;
  2762. }
  2763. void lockdep_clear_current_reclaim_state(void)
  2764. {
  2765. current->lockdep_reclaim_gfp = 0;
  2766. }
  2767. #ifdef CONFIG_LOCK_STAT
  2768. static int
  2769. print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
  2770. unsigned long ip)
  2771. {
  2772. if (!debug_locks_off())
  2773. return 0;
  2774. if (debug_locks_silent)
  2775. return 0;
  2776. printk("\n=================================\n");
  2777. printk( "[ BUG: bad contention detected! ]\n");
  2778. printk( "---------------------------------\n");
  2779. printk("%s/%d is trying to contend lock (",
  2780. curr->comm, task_pid_nr(curr));
  2781. print_lockdep_cache(lock);
  2782. printk(") at:\n");
  2783. print_ip_sym(ip);
  2784. printk("but there are no locks held!\n");
  2785. printk("\nother info that might help us debug this:\n");
  2786. lockdep_print_held_locks(curr);
  2787. printk("\nstack backtrace:\n");
  2788. dump_stack();
  2789. return 0;
  2790. }
  2791. static void
  2792. __lock_contended(struct lockdep_map *lock, unsigned long ip)
  2793. {
  2794. struct task_struct *curr = current;
  2795. struct held_lock *hlock, *prev_hlock;
  2796. struct lock_class_stats *stats;
  2797. unsigned int depth;
  2798. int i, contention_point, contending_point;
  2799. depth = curr->lockdep_depth;
  2800. if (DEBUG_LOCKS_WARN_ON(!depth))
  2801. return;
  2802. prev_hlock = NULL;
  2803. for (i = depth-1; i >= 0; i--) {
  2804. hlock = curr->held_locks + i;
  2805. /*
  2806. * We must not cross into another context:
  2807. */
  2808. if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
  2809. break;
  2810. if (match_held_lock(hlock, lock))
  2811. goto found_it;
  2812. prev_hlock = hlock;
  2813. }
  2814. print_lock_contention_bug(curr, lock, ip);
  2815. return;
  2816. found_it:
  2817. if (hlock->instance != lock)
  2818. return;
  2819. hlock->waittime_stamp = sched_clock();
  2820. contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
  2821. contending_point = lock_point(hlock_class(hlock)->contending_point,
  2822. lock->ip);
  2823. stats = get_lock_stats(hlock_class(hlock));
  2824. if (contention_point < LOCKSTAT_POINTS)
  2825. stats->contention_point[contention_point]++;
  2826. if (contending_point < LOCKSTAT_POINTS)
  2827. stats->contending_point[contending_point]++;
  2828. if (lock->cpu != smp_processor_id())
  2829. stats->bounces[bounce_contended + !!hlock->read]++;
  2830. put_lock_stats(stats);
  2831. }
  2832. static void
  2833. __lock_acquired(struct lockdep_map *lock, unsigned long ip)
  2834. {
  2835. struct task_struct *curr = current;
  2836. struct held_lock *hlock, *prev_hlock;
  2837. struct lock_class_stats *stats;
  2838. unsigned int depth;
  2839. u64 now;
  2840. s64 waittime = 0;
  2841. int i, cpu;
  2842. depth = curr->lockdep_depth;
  2843. if (DEBUG_LOCKS_WARN_ON(!depth))
  2844. return;
  2845. prev_hlock = NULL;
  2846. for (i = depth-1; i >= 0; i--) {
  2847. hlock = curr->held_locks + i;
  2848. /*
  2849. * We must not cross into another context:
  2850. */
  2851. if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
  2852. break;
  2853. if (match_held_lock(hlock, lock))
  2854. goto found_it;
  2855. prev_hlock = hlock;
  2856. }
  2857. print_lock_contention_bug(curr, lock, _RET_IP_);
  2858. return;
  2859. found_it:
  2860. if (hlock->instance != lock)
  2861. return;
  2862. cpu = smp_processor_id();
  2863. if (hlock->waittime_stamp) {
  2864. now = sched_clock();
  2865. waittime = now - hlock->waittime_stamp;
  2866. hlock->holdtime_stamp = now;
  2867. }
  2868. trace_lock_acquired(lock, ip, waittime);
  2869. stats = get_lock_stats(hlock_class(hlock));
  2870. if (waittime) {
  2871. if (hlock->read)
  2872. lock_time_inc(&stats->read_waittime, waittime);
  2873. else
  2874. lock_time_inc(&stats->write_waittime, waittime);
  2875. }
  2876. if (lock->cpu != cpu)
  2877. stats->bounces[bounce_acquired + !!hlock->read]++;
  2878. put_lock_stats(stats);
  2879. lock->cpu = cpu;
  2880. lock->ip = ip;
  2881. }
  2882. void lock_contended(struct lockdep_map *lock, unsigned long ip)
  2883. {
  2884. unsigned long flags;
  2885. trace_lock_contended(lock, ip);
  2886. if (unlikely(!lock_stat))
  2887. return;
  2888. if (unlikely(current->lockdep_recursion))
  2889. return;
  2890. raw_local_irq_save(flags);
  2891. check_flags(flags);
  2892. current->lockdep_recursion = 1;
  2893. __lock_contended(lock, ip);
  2894. current->lockdep_recursion = 0;
  2895. raw_local_irq_restore(flags);
  2896. }
  2897. EXPORT_SYMBOL_GPL(lock_contended);
  2898. void lock_acquired(struct lockdep_map *lock, unsigned long ip)
  2899. {
  2900. unsigned long flags;
  2901. if (unlikely(!lock_stat))
  2902. return;
  2903. if (unlikely(current->lockdep_recursion))
  2904. return;
  2905. raw_local_irq_save(flags);
  2906. check_flags(flags);
  2907. current->lockdep_recursion = 1;
  2908. __lock_acquired(lock, ip);
  2909. current->lockdep_recursion = 0;
  2910. raw_local_irq_restore(flags);
  2911. }
  2912. EXPORT_SYMBOL_GPL(lock_acquired);
  2913. #endif
  2914. /*
  2915. * Used by the testsuite, sanitize the validator state
  2916. * after a simulated failure:
  2917. */
  2918. void lockdep_reset(void)
  2919. {
  2920. unsigned long flags;
  2921. int i;
  2922. raw_local_irq_save(flags);
  2923. current->curr_chain_key = 0;
  2924. current->lockdep_depth = 0;
  2925. current->lockdep_recursion = 0;
  2926. memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
  2927. nr_hardirq_chains = 0;
  2928. nr_softirq_chains = 0;
  2929. nr_process_chains = 0;
  2930. debug_locks = 1;
  2931. for (i = 0; i < CHAINHASH_SIZE; i++)
  2932. INIT_LIST_HEAD(chainhash_table + i);
  2933. raw_local_irq_restore(flags);
  2934. }
  2935. static void zap_class(struct lock_class *class)
  2936. {
  2937. int i;
  2938. /*
  2939. * Remove all dependencies this lock is
  2940. * involved in:
  2941. */
  2942. for (i = 0; i < nr_list_entries; i++) {
  2943. if (list_entries[i].class == class)
  2944. list_del_rcu(&list_entries[i].entry);
  2945. }
  2946. /*
  2947. * Unhash the class and remove it from the all_lock_classes list:
  2948. */
  2949. list_del_rcu(&class->hash_entry);
  2950. list_del_rcu(&class->lock_entry);
  2951. class->key = NULL;
  2952. }
  2953. static inline int within(const void *addr, void *start, unsigned long size)
  2954. {
  2955. return addr >= start && addr < start + size;
  2956. }
  2957. void lockdep_free_key_range(void *start, unsigned long size)
  2958. {
  2959. struct lock_class *class, *next;
  2960. struct list_head *head;
  2961. unsigned long flags;
  2962. int i;
  2963. int locked;
  2964. raw_local_irq_save(flags);
  2965. locked = graph_lock();
  2966. /*
  2967. * Unhash all classes that were created by this module:
  2968. */
  2969. for (i = 0; i < CLASSHASH_SIZE; i++) {
  2970. head = classhash_table + i;
  2971. if (list_empty(head))
  2972. continue;
  2973. list_for_each_entry_safe(class, next, head, hash_entry) {
  2974. if (within(class->key, start, size))
  2975. zap_class(class);
  2976. else if (within(class->name, start, size))
  2977. zap_class(class);
  2978. }
  2979. }
  2980. if (locked)
  2981. graph_unlock();
  2982. raw_local_irq_restore(flags);
  2983. }
  2984. void lockdep_reset_lock(struct lockdep_map *lock)
  2985. {
  2986. struct lock_class *class, *next;
  2987. struct list_head *head;
  2988. unsigned long flags;
  2989. int i, j;
  2990. int locked;
  2991. raw_local_irq_save(flags);
  2992. /*
  2993. * Remove all classes this lock might have:
  2994. */
  2995. for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
  2996. /*
  2997. * If the class exists we look it up and zap it:
  2998. */
  2999. class = look_up_lock_class(lock, j);
  3000. if (class)
  3001. zap_class(class);
  3002. }
  3003. /*
  3004. * Debug check: in the end all mapped classes should
  3005. * be gone.
  3006. */
  3007. locked = graph_lock();
  3008. for (i = 0; i < CLASSHASH_SIZE; i++) {
  3009. head = classhash_table + i;
  3010. if (list_empty(head))
  3011. continue;
  3012. list_for_each_entry_safe(class, next, head, hash_entry) {
  3013. if (unlikely(class == lock->class_cache)) {
  3014. if (debug_locks_off_graph_unlock())
  3015. WARN_ON(1);
  3016. goto out_restore;
  3017. }
  3018. }
  3019. }
  3020. if (locked)
  3021. graph_unlock();
  3022. out_restore:
  3023. raw_local_irq_restore(flags);
  3024. }
  3025. void lockdep_init(void)
  3026. {
  3027. int i;
  3028. /*
  3029. * Some architectures have their own start_kernel()
  3030. * code which calls lockdep_init(), while we also
  3031. * call lockdep_init() from the start_kernel() itself,
  3032. * and we want to initialize the hashes only once:
  3033. */
  3034. if (lockdep_initialized)
  3035. return;
  3036. for (i = 0; i < CLASSHASH_SIZE; i++)
  3037. INIT_LIST_HEAD(classhash_table + i);
  3038. for (i = 0; i < CHAINHASH_SIZE; i++)
  3039. INIT_LIST_HEAD(chainhash_table + i);
  3040. lockdep_initialized = 1;
  3041. }
  3042. void __init lockdep_info(void)
  3043. {
  3044. printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
  3045. printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
  3046. printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
  3047. printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
  3048. printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
  3049. printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
  3050. printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
  3051. printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
  3052. printk(" memory used by lock dependency info: %lu kB\n",
  3053. (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
  3054. sizeof(struct list_head) * CLASSHASH_SIZE +
  3055. sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
  3056. sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
  3057. sizeof(struct list_head) * CHAINHASH_SIZE
  3058. #ifdef CONFIG_PROVE_LOCKING
  3059. + sizeof(struct circular_queue)
  3060. #endif
  3061. ) / 1024
  3062. );
  3063. printk(" per task-struct memory footprint: %lu bytes\n",
  3064. sizeof(struct held_lock) * MAX_LOCK_DEPTH);
  3065. #ifdef CONFIG_DEBUG_LOCKDEP
  3066. if (lockdep_init_error) {
  3067. printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
  3068. printk("Call stack leading to lockdep invocation was:\n");
  3069. print_stack_trace(&lockdep_init_trace, 0);
  3070. }
  3071. #endif
  3072. }
  3073. static void
  3074. print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
  3075. const void *mem_to, struct held_lock *hlock)
  3076. {
  3077. if (!debug_locks_off())
  3078. return;
  3079. if (debug_locks_silent)
  3080. return;
  3081. printk("\n=========================\n");
  3082. printk( "[ BUG: held lock freed! ]\n");
  3083. printk( "-------------------------\n");
  3084. printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
  3085. curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
  3086. print_lock(hlock);
  3087. lockdep_print_held_locks(curr);
  3088. printk("\nstack backtrace:\n");
  3089. dump_stack();
  3090. }
  3091. static inline int not_in_range(const void* mem_from, unsigned long mem_len,
  3092. const void* lock_from, unsigned long lock_len)
  3093. {
  3094. return lock_from + lock_len <= mem_from ||
  3095. mem_from + mem_len <= lock_from;
  3096. }
  3097. /*
  3098. * Called when kernel memory is freed (or unmapped), or if a lock
  3099. * is destroyed or reinitialized - this code checks whether there is
  3100. * any held lock in the memory range of <from> to <to>:
  3101. */
  3102. void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
  3103. {
  3104. struct task_struct *curr = current;
  3105. struct held_lock *hlock;
  3106. unsigned long flags;
  3107. int i;
  3108. if (unlikely(!debug_locks))
  3109. return;
  3110. local_irq_save(flags);
  3111. for (i = 0; i < curr->lockdep_depth; i++) {
  3112. hlock = curr->held_locks + i;
  3113. if (not_in_range(mem_from, mem_len, hlock->instance,
  3114. sizeof(*hlock->instance)))
  3115. continue;
  3116. print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
  3117. break;
  3118. }
  3119. local_irq_restore(flags);
  3120. }
  3121. EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
  3122. static void print_held_locks_bug(struct task_struct *curr)
  3123. {
  3124. if (!debug_locks_off())
  3125. return;
  3126. if (debug_locks_silent)
  3127. return;
  3128. printk("\n=====================================\n");
  3129. printk( "[ BUG: lock held at task exit time! ]\n");
  3130. printk( "-------------------------------------\n");
  3131. printk("%s/%d is exiting with locks still held!\n",
  3132. curr->comm, task_pid_nr(curr));
  3133. lockdep_print_held_locks(curr);
  3134. printk("\nstack backtrace:\n");
  3135. dump_stack();
  3136. }
  3137. void debug_check_no_locks_held(struct task_struct *task)
  3138. {
  3139. if (unlikely(task->lockdep_depth > 0))
  3140. print_held_locks_bug(task);
  3141. }
  3142. void debug_show_all_locks(void)
  3143. {
  3144. struct task_struct *g, *p;
  3145. int count = 10;
  3146. int unlock = 1;
  3147. if (unlikely(!debug_locks)) {
  3148. printk("INFO: lockdep is turned off.\n");
  3149. return;
  3150. }
  3151. printk("\nShowing all locks held in the system:\n");
  3152. /*
  3153. * Here we try to get the tasklist_lock as hard as possible,
  3154. * if not successful after 2 seconds we ignore it (but keep
  3155. * trying). This is to enable a debug printout even if a
  3156. * tasklist_lock-holding task deadlocks or crashes.
  3157. */
  3158. retry:
  3159. if (!read_trylock(&tasklist_lock)) {
  3160. if (count == 10)
  3161. printk("hm, tasklist_lock locked, retrying... ");
  3162. if (count) {
  3163. count--;
  3164. printk(" #%d", 10-count);
  3165. mdelay(200);
  3166. goto retry;
  3167. }
  3168. printk(" ignoring it.\n");
  3169. unlock = 0;
  3170. } else {
  3171. if (count != 10)
  3172. printk(KERN_CONT " locked it.\n");
  3173. }
  3174. do_each_thread(g, p) {
  3175. /*
  3176. * It's not reliable to print a task's held locks
  3177. * if it's not sleeping (or if it's not the current
  3178. * task):
  3179. */
  3180. if (p->state == TASK_RUNNING && p != current)
  3181. continue;
  3182. if (p->lockdep_depth)
  3183. lockdep_print_held_locks(p);
  3184. if (!unlock)
  3185. if (read_trylock(&tasklist_lock))
  3186. unlock = 1;
  3187. } while_each_thread(g, p);
  3188. printk("\n");
  3189. printk("=============================================\n\n");
  3190. if (unlock)
  3191. read_unlock(&tasklist_lock);
  3192. }
  3193. EXPORT_SYMBOL_GPL(debug_show_all_locks);
  3194. /*
  3195. * Careful: only use this function if you are sure that
  3196. * the task cannot run in parallel!
  3197. */
  3198. void __debug_show_held_locks(struct task_struct *task)
  3199. {
  3200. if (unlikely(!debug_locks)) {
  3201. printk("INFO: lockdep is turned off.\n");
  3202. return;
  3203. }
  3204. lockdep_print_held_locks(task);
  3205. }
  3206. EXPORT_SYMBOL_GPL(__debug_show_held_locks);
  3207. void debug_show_held_locks(struct task_struct *task)
  3208. {
  3209. __debug_show_held_locks(task);
  3210. }
  3211. EXPORT_SYMBOL_GPL(debug_show_held_locks);
  3212. void lockdep_sys_exit(void)
  3213. {
  3214. struct task_struct *curr = current;
  3215. if (unlikely(curr->lockdep_depth)) {
  3216. if (!debug_locks_off())
  3217. return;
  3218. printk("\n================================================\n");
  3219. printk( "[ BUG: lock held when returning to user space! ]\n");
  3220. printk( "------------------------------------------------\n");
  3221. printk("%s/%d is leaving the kernel with locks still held!\n",
  3222. curr->comm, curr->pid);
  3223. lockdep_print_held_locks(curr);
  3224. }
  3225. }