lockdep.c 68 KB

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