lockdep.c 69 KB

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