lockdep.c 68 KB

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