lockdep.c 109 KB

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