lockdep.c 79 KB

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