lockdep.c 85 KB

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