lockdep.c 106 KB

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