lockdep.c 89 KB

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