lockdep.c 68 KB

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