lockdep.c 69 KB

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