lockdep.c 86 KB

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