lockdep.c 68 KB

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