lockdep.c 77 KB

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