lockdep.c 86 KB

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