lockdep.c 90 KB

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