lockdep.c 69 KB

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