lockdep.c 90 KB

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