lockdep.c 91 KB

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