lockdep.c 85 KB

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