lockdep.c 67 KB

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