lockdep.c 102 KB

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