lockdep.c 92 KB

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