lockdep.c 85 KB

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