lockdep.c 97 KB

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