lockdep.c 68 KB

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