lockdep.c 89 KB

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