lockdep.c 85 KB

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