lockdep.c 85 KB

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