lockdep.c 85 KB

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