lockdep.c 110 KB

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