lockdep.c 85 KB

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