lockdep.c 104 KB

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