lockdep.c 111 KB

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