lockdep.c 69 KB

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