lockdep.c 85 KB

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