lockdep.c 126 KB

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