lockdep.c 68 KB

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