lockdep.c 126 KB

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