lockdep.c 84 KB

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