lockdep.c 108 KB

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