lockdep.c 104 KB

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