lockdep.c 101 KB

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