lockdep.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450
  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(KERN_CONT "%s", name);
  424. } else {
  425. printk(KERN_CONT "%s", name);
  426. if (class->name_version > 1)
  427. printk(KERN_CONT "#%d", class->name_version);
  428. if (class->subclass)
  429. printk(KERN_CONT "/%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(KERN_CONT " (");
  437. __print_lock_name(class);
  438. printk(KERN_CONT "){%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(KERN_CONT "%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(KERN_CONT "<RELEASED>\n");
  460. return;
  461. }
  462. print_lock_name(lock_classes + class_idx - 1);
  463. printk(KERN_CONT ", at: [<%p>] %pS\n",
  464. (void *)hlock->acquire_ip, (void *)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(KERN_CONT "#%d", class->name_version);
  666. printk(KERN_CONT "\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(KERN_CONT ":\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(KERN_CONT " --> ");
  926. __print_lock_name(parent);
  927. printk(KERN_CONT " --> ");
  928. __print_lock_name(target);
  929. printk(KERN_CONT "\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(KERN_CONT ");\n");
  937. printk(" lock(");
  938. __print_lock_name(parent);
  939. printk(KERN_CONT ");\n");
  940. printk(" lock(");
  941. __print_lock_name(target);
  942. printk(KERN_CONT ");\n");
  943. printk(" lock(");
  944. __print_lock_name(source);
  945. printk(KERN_CONT ");\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(KERN_CONT " ops: %lu", class->ops);
  1130. printk(KERN_CONT " {\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(KERN_CONT " at:\n");
  1136. print_stack_trace(class->usage_traces + bit, len);
  1137. }
  1138. }
  1139. printk("%*s }\n", depth, "");
  1140. printk("%*s ... key at: [<%p>] %pS\n",
  1141. depth, "", class->key, 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(KERN_CONT " --> ");
  1196. __print_lock_name(middle_class);
  1197. printk(KERN_CONT " --> ");
  1198. __print_lock_name(unsafe_class);
  1199. printk(KERN_CONT "\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(KERN_CONT ");\n");
  1207. printk(" local_irq_disable();\n");
  1208. printk(" lock(");
  1209. __print_lock_name(safe_class);
  1210. printk(KERN_CONT ");\n");
  1211. printk(" lock(");
  1212. __print_lock_name(middle_class);
  1213. printk(KERN_CONT ");\n");
  1214. printk(" <Interrupt>\n");
  1215. printk(" lock(");
  1216. __print_lock_name(safe_class);
  1217. printk(KERN_CONT ");\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(KERN_CONT " ->");
  1252. print_lock_name(hlock_class(next));
  1253. printk(KERN_CONT "\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 and the holding lock:\n", irqclass);
  1269. if (!save_trace(&prev_root->trace))
  1270. return 0;
  1271. print_shortest_lock_dependencies(backwards_entry, prev_root);
  1272. printk("\nthe dependencies between the lock to be acquired");
  1273. printk(" and %s-irq-unsafe lock:\n", irqclass);
  1274. if (!save_trace(&next_root->trace))
  1275. return 0;
  1276. print_shortest_lock_dependencies(forwards_entry, next_root);
  1277. printk("\nstack backtrace:\n");
  1278. dump_stack();
  1279. return 0;
  1280. }
  1281. static int
  1282. check_usage(struct task_struct *curr, struct held_lock *prev,
  1283. struct held_lock *next, enum lock_usage_bit bit_backwards,
  1284. enum lock_usage_bit bit_forwards, const char *irqclass)
  1285. {
  1286. int ret;
  1287. struct lock_list this, that;
  1288. struct lock_list *uninitialized_var(target_entry);
  1289. struct lock_list *uninitialized_var(target_entry1);
  1290. this.parent = NULL;
  1291. this.class = hlock_class(prev);
  1292. ret = find_usage_backwards(&this, bit_backwards, &target_entry);
  1293. if (ret < 0)
  1294. return print_bfs_bug(ret);
  1295. if (ret == 1)
  1296. return ret;
  1297. that.parent = NULL;
  1298. that.class = hlock_class(next);
  1299. ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
  1300. if (ret < 0)
  1301. return print_bfs_bug(ret);
  1302. if (ret == 1)
  1303. return ret;
  1304. return print_bad_irq_dependency(curr, &this, &that,
  1305. target_entry, target_entry1,
  1306. prev, next,
  1307. bit_backwards, bit_forwards, irqclass);
  1308. }
  1309. static const char *state_names[] = {
  1310. #define LOCKDEP_STATE(__STATE) \
  1311. __stringify(__STATE),
  1312. #include "lockdep_states.h"
  1313. #undef LOCKDEP_STATE
  1314. };
  1315. static const char *state_rnames[] = {
  1316. #define LOCKDEP_STATE(__STATE) \
  1317. __stringify(__STATE)"-READ",
  1318. #include "lockdep_states.h"
  1319. #undef LOCKDEP_STATE
  1320. };
  1321. static inline const char *state_name(enum lock_usage_bit bit)
  1322. {
  1323. return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
  1324. }
  1325. static int exclusive_bit(int new_bit)
  1326. {
  1327. /*
  1328. * USED_IN
  1329. * USED_IN_READ
  1330. * ENABLED
  1331. * ENABLED_READ
  1332. *
  1333. * bit 0 - write/read
  1334. * bit 1 - used_in/enabled
  1335. * bit 2+ state
  1336. */
  1337. int state = new_bit & ~3;
  1338. int dir = new_bit & 2;
  1339. /*
  1340. * keep state, bit flip the direction and strip read.
  1341. */
  1342. return state | (dir ^ 2);
  1343. }
  1344. static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
  1345. struct held_lock *next, enum lock_usage_bit bit)
  1346. {
  1347. /*
  1348. * Prove that the new dependency does not connect a hardirq-safe
  1349. * lock with a hardirq-unsafe lock - to achieve this we search
  1350. * the backwards-subgraph starting at <prev>, and the
  1351. * forwards-subgraph starting at <next>:
  1352. */
  1353. if (!check_usage(curr, prev, next, bit,
  1354. exclusive_bit(bit), state_name(bit)))
  1355. return 0;
  1356. bit++; /* _READ */
  1357. /*
  1358. * Prove that the new dependency does not connect a hardirq-safe-read
  1359. * lock with a hardirq-unsafe lock - to achieve this we search
  1360. * the backwards-subgraph starting at <prev>, and the
  1361. * forwards-subgraph starting at <next>:
  1362. */
  1363. if (!check_usage(curr, prev, next, bit,
  1364. exclusive_bit(bit), state_name(bit)))
  1365. return 0;
  1366. return 1;
  1367. }
  1368. static int
  1369. check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
  1370. struct held_lock *next)
  1371. {
  1372. #define LOCKDEP_STATE(__STATE) \
  1373. if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
  1374. return 0;
  1375. #include "lockdep_states.h"
  1376. #undef LOCKDEP_STATE
  1377. return 1;
  1378. }
  1379. static void inc_chains(void)
  1380. {
  1381. if (current->hardirq_context)
  1382. nr_hardirq_chains++;
  1383. else {
  1384. if (current->softirq_context)
  1385. nr_softirq_chains++;
  1386. else
  1387. nr_process_chains++;
  1388. }
  1389. }
  1390. #else
  1391. static inline int
  1392. check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
  1393. struct held_lock *next)
  1394. {
  1395. return 1;
  1396. }
  1397. static inline void inc_chains(void)
  1398. {
  1399. nr_process_chains++;
  1400. }
  1401. #endif
  1402. static void
  1403. print_deadlock_scenario(struct held_lock *nxt,
  1404. struct held_lock *prv)
  1405. {
  1406. struct lock_class *next = hlock_class(nxt);
  1407. struct lock_class *prev = hlock_class(prv);
  1408. printk(" Possible unsafe locking scenario:\n\n");
  1409. printk(" CPU0\n");
  1410. printk(" ----\n");
  1411. printk(" lock(");
  1412. __print_lock_name(prev);
  1413. printk(KERN_CONT ");\n");
  1414. printk(" lock(");
  1415. __print_lock_name(next);
  1416. printk(KERN_CONT ");\n");
  1417. printk("\n *** DEADLOCK ***\n\n");
  1418. printk(" May be due to missing lock nesting notation\n\n");
  1419. }
  1420. static int
  1421. print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
  1422. struct held_lock *next)
  1423. {
  1424. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1425. return 0;
  1426. printk("\n");
  1427. printk("=============================================\n");
  1428. printk("[ INFO: possible recursive locking detected ]\n");
  1429. print_kernel_ident();
  1430. printk("---------------------------------------------\n");
  1431. printk("%s/%d is trying to acquire lock:\n",
  1432. curr->comm, task_pid_nr(curr));
  1433. print_lock(next);
  1434. printk("\nbut task is already holding lock:\n");
  1435. print_lock(prev);
  1436. printk("\nother info that might help us debug this:\n");
  1437. print_deadlock_scenario(next, prev);
  1438. lockdep_print_held_locks(curr);
  1439. printk("\nstack backtrace:\n");
  1440. dump_stack();
  1441. return 0;
  1442. }
  1443. /*
  1444. * Check whether we are holding such a class already.
  1445. *
  1446. * (Note that this has to be done separately, because the graph cannot
  1447. * detect such classes of deadlocks.)
  1448. *
  1449. * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
  1450. */
  1451. static int
  1452. check_deadlock(struct task_struct *curr, struct held_lock *next,
  1453. struct lockdep_map *next_instance, int read)
  1454. {
  1455. struct held_lock *prev;
  1456. struct held_lock *nest = NULL;
  1457. int i;
  1458. for (i = 0; i < curr->lockdep_depth; i++) {
  1459. prev = curr->held_locks + i;
  1460. if (prev->instance == next->nest_lock)
  1461. nest = prev;
  1462. if (hlock_class(prev) != hlock_class(next))
  1463. continue;
  1464. /*
  1465. * Allow read-after-read recursion of the same
  1466. * lock class (i.e. read_lock(lock)+read_lock(lock)):
  1467. */
  1468. if ((read == 2) && prev->read)
  1469. return 2;
  1470. /*
  1471. * We're holding the nest_lock, which serializes this lock's
  1472. * nesting behaviour.
  1473. */
  1474. if (nest)
  1475. return 2;
  1476. return print_deadlock_bug(curr, prev, next);
  1477. }
  1478. return 1;
  1479. }
  1480. /*
  1481. * There was a chain-cache miss, and we are about to add a new dependency
  1482. * to a previous lock. We recursively validate the following rules:
  1483. *
  1484. * - would the adding of the <prev> -> <next> dependency create a
  1485. * circular dependency in the graph? [== circular deadlock]
  1486. *
  1487. * - does the new prev->next dependency connect any hardirq-safe lock
  1488. * (in the full backwards-subgraph starting at <prev>) with any
  1489. * hardirq-unsafe lock (in the full forwards-subgraph starting at
  1490. * <next>)? [== illegal lock inversion with hardirq contexts]
  1491. *
  1492. * - does the new prev->next dependency connect any softirq-safe lock
  1493. * (in the full backwards-subgraph starting at <prev>) with any
  1494. * softirq-unsafe lock (in the full forwards-subgraph starting at
  1495. * <next>)? [== illegal lock inversion with softirq contexts]
  1496. *
  1497. * any of these scenarios could lead to a deadlock.
  1498. *
  1499. * Then if all the validations pass, we add the forwards and backwards
  1500. * dependency.
  1501. */
  1502. static int
  1503. check_prev_add(struct task_struct *curr, struct held_lock *prev,
  1504. struct held_lock *next, int distance, int *stack_saved)
  1505. {
  1506. struct lock_list *entry;
  1507. int ret;
  1508. struct lock_list this;
  1509. struct lock_list *uninitialized_var(target_entry);
  1510. /*
  1511. * Static variable, serialized by the graph_lock().
  1512. *
  1513. * We use this static variable to save the stack trace in case
  1514. * we call into this function multiple times due to encountering
  1515. * trylocks in the held lock stack.
  1516. */
  1517. static struct stack_trace trace;
  1518. /*
  1519. * Prove that the new <prev> -> <next> dependency would not
  1520. * create a circular dependency in the graph. (We do this by
  1521. * forward-recursing into the graph starting at <next>, and
  1522. * checking whether we can reach <prev>.)
  1523. *
  1524. * We are using global variables to control the recursion, to
  1525. * keep the stackframe size of the recursive functions low:
  1526. */
  1527. this.class = hlock_class(next);
  1528. this.parent = NULL;
  1529. ret = check_noncircular(&this, hlock_class(prev), &target_entry);
  1530. if (unlikely(!ret))
  1531. return print_circular_bug(&this, target_entry, next, prev);
  1532. else if (unlikely(ret < 0))
  1533. return print_bfs_bug(ret);
  1534. if (!check_prev_add_irq(curr, prev, next))
  1535. return 0;
  1536. /*
  1537. * For recursive read-locks we do all the dependency checks,
  1538. * but we dont store read-triggered dependencies (only
  1539. * write-triggered dependencies). This ensures that only the
  1540. * write-side dependencies matter, and that if for example a
  1541. * write-lock never takes any other locks, then the reads are
  1542. * equivalent to a NOP.
  1543. */
  1544. if (next->read == 2 || prev->read == 2)
  1545. return 1;
  1546. /*
  1547. * Is the <prev> -> <next> dependency already present?
  1548. *
  1549. * (this may occur even though this is a new chain: consider
  1550. * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
  1551. * chains - the second one will be new, but L1 already has
  1552. * L2 added to its dependency list, due to the first chain.)
  1553. */
  1554. list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
  1555. if (entry->class == hlock_class(next)) {
  1556. if (distance == 1)
  1557. entry->distance = 1;
  1558. return 2;
  1559. }
  1560. }
  1561. if (!*stack_saved) {
  1562. if (!save_trace(&trace))
  1563. return 0;
  1564. *stack_saved = 1;
  1565. }
  1566. /*
  1567. * Ok, all validations passed, add the new lock
  1568. * to the previous lock's dependency list:
  1569. */
  1570. ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
  1571. &hlock_class(prev)->locks_after,
  1572. next->acquire_ip, distance, &trace);
  1573. if (!ret)
  1574. return 0;
  1575. ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
  1576. &hlock_class(next)->locks_before,
  1577. next->acquire_ip, distance, &trace);
  1578. if (!ret)
  1579. return 0;
  1580. /*
  1581. * Debugging printouts:
  1582. */
  1583. if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
  1584. /* We drop graph lock, so another thread can overwrite trace. */
  1585. *stack_saved = 0;
  1586. graph_unlock();
  1587. printk("\n new dependency: ");
  1588. print_lock_name(hlock_class(prev));
  1589. printk(KERN_CONT " => ");
  1590. print_lock_name(hlock_class(next));
  1591. printk(KERN_CONT "\n");
  1592. dump_stack();
  1593. return graph_lock();
  1594. }
  1595. return 1;
  1596. }
  1597. /*
  1598. * Add the dependency to all directly-previous locks that are 'relevant'.
  1599. * The ones that are relevant are (in increasing distance from curr):
  1600. * all consecutive trylock entries and the final non-trylock entry - or
  1601. * the end of this context's lock-chain - whichever comes first.
  1602. */
  1603. static int
  1604. check_prevs_add(struct task_struct *curr, struct held_lock *next)
  1605. {
  1606. int depth = curr->lockdep_depth;
  1607. int stack_saved = 0;
  1608. struct held_lock *hlock;
  1609. /*
  1610. * Debugging checks.
  1611. *
  1612. * Depth must not be zero for a non-head lock:
  1613. */
  1614. if (!depth)
  1615. goto out_bug;
  1616. /*
  1617. * At least two relevant locks must exist for this
  1618. * to be a head:
  1619. */
  1620. if (curr->held_locks[depth].irq_context !=
  1621. curr->held_locks[depth-1].irq_context)
  1622. goto out_bug;
  1623. for (;;) {
  1624. int distance = curr->lockdep_depth - depth + 1;
  1625. hlock = curr->held_locks + depth - 1;
  1626. /*
  1627. * Only non-recursive-read entries get new dependencies
  1628. * added:
  1629. */
  1630. if (hlock->read != 2 && hlock->check) {
  1631. if (!check_prev_add(curr, hlock, next,
  1632. distance, &stack_saved))
  1633. return 0;
  1634. /*
  1635. * Stop after the first non-trylock entry,
  1636. * as non-trylock entries have added their
  1637. * own direct dependencies already, so this
  1638. * lock is connected to them indirectly:
  1639. */
  1640. if (!hlock->trylock)
  1641. break;
  1642. }
  1643. depth--;
  1644. /*
  1645. * End of lock-stack?
  1646. */
  1647. if (!depth)
  1648. break;
  1649. /*
  1650. * Stop the search if we cross into another context:
  1651. */
  1652. if (curr->held_locks[depth].irq_context !=
  1653. curr->held_locks[depth-1].irq_context)
  1654. break;
  1655. }
  1656. return 1;
  1657. out_bug:
  1658. if (!debug_locks_off_graph_unlock())
  1659. return 0;
  1660. /*
  1661. * Clearly we all shouldn't be here, but since we made it we
  1662. * can reliable say we messed up our state. See the above two
  1663. * gotos for reasons why we could possibly end up here.
  1664. */
  1665. WARN_ON(1);
  1666. return 0;
  1667. }
  1668. unsigned long nr_lock_chains;
  1669. struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
  1670. int nr_chain_hlocks;
  1671. static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
  1672. struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
  1673. {
  1674. return lock_classes + chain_hlocks[chain->base + i];
  1675. }
  1676. /*
  1677. * Returns the index of the first held_lock of the current chain
  1678. */
  1679. static inline int get_first_held_lock(struct task_struct *curr,
  1680. struct held_lock *hlock)
  1681. {
  1682. int i;
  1683. struct held_lock *hlock_curr;
  1684. for (i = curr->lockdep_depth - 1; i >= 0; i--) {
  1685. hlock_curr = curr->held_locks + i;
  1686. if (hlock_curr->irq_context != hlock->irq_context)
  1687. break;
  1688. }
  1689. return ++i;
  1690. }
  1691. #ifdef CONFIG_DEBUG_LOCKDEP
  1692. /*
  1693. * Returns the next chain_key iteration
  1694. */
  1695. static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
  1696. {
  1697. u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
  1698. printk(" class_idx:%d -> chain_key:%016Lx",
  1699. class_idx,
  1700. (unsigned long long)new_chain_key);
  1701. return new_chain_key;
  1702. }
  1703. static void
  1704. print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
  1705. {
  1706. struct held_lock *hlock;
  1707. u64 chain_key = 0;
  1708. int depth = curr->lockdep_depth;
  1709. int i;
  1710. printk("depth: %u\n", depth + 1);
  1711. for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
  1712. hlock = curr->held_locks + i;
  1713. chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
  1714. print_lock(hlock);
  1715. }
  1716. print_chain_key_iteration(hlock_next->class_idx, chain_key);
  1717. print_lock(hlock_next);
  1718. }
  1719. static void print_chain_keys_chain(struct lock_chain *chain)
  1720. {
  1721. int i;
  1722. u64 chain_key = 0;
  1723. int class_id;
  1724. printk("depth: %u\n", chain->depth);
  1725. for (i = 0; i < chain->depth; i++) {
  1726. class_id = chain_hlocks[chain->base + i];
  1727. chain_key = print_chain_key_iteration(class_id + 1, chain_key);
  1728. print_lock_name(lock_classes + class_id);
  1729. printk("\n");
  1730. }
  1731. }
  1732. static void print_collision(struct task_struct *curr,
  1733. struct held_lock *hlock_next,
  1734. struct lock_chain *chain)
  1735. {
  1736. printk("\n");
  1737. printk("======================\n");
  1738. printk("[chain_key collision ]\n");
  1739. print_kernel_ident();
  1740. printk("----------------------\n");
  1741. printk("%s/%d: ", current->comm, task_pid_nr(current));
  1742. printk("Hash chain already cached but the contents don't match!\n");
  1743. printk("Held locks:");
  1744. print_chain_keys_held_locks(curr, hlock_next);
  1745. printk("Locks in cached chain:");
  1746. print_chain_keys_chain(chain);
  1747. printk("\nstack backtrace:\n");
  1748. dump_stack();
  1749. }
  1750. #endif
  1751. /*
  1752. * Checks whether the chain and the current held locks are consistent
  1753. * in depth and also in content. If they are not it most likely means
  1754. * that there was a collision during the calculation of the chain_key.
  1755. * Returns: 0 not passed, 1 passed
  1756. */
  1757. static int check_no_collision(struct task_struct *curr,
  1758. struct held_lock *hlock,
  1759. struct lock_chain *chain)
  1760. {
  1761. #ifdef CONFIG_DEBUG_LOCKDEP
  1762. int i, j, id;
  1763. i = get_first_held_lock(curr, hlock);
  1764. if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
  1765. print_collision(curr, hlock, chain);
  1766. return 0;
  1767. }
  1768. for (j = 0; j < chain->depth - 1; j++, i++) {
  1769. id = curr->held_locks[i].class_idx - 1;
  1770. if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
  1771. print_collision(curr, hlock, chain);
  1772. return 0;
  1773. }
  1774. }
  1775. #endif
  1776. return 1;
  1777. }
  1778. /*
  1779. * Look up a dependency chain. If the key is not present yet then
  1780. * add it and return 1 - in this case the new dependency chain is
  1781. * validated. If the key is already hashed, return 0.
  1782. * (On return with 1 graph_lock is held.)
  1783. */
  1784. static inline int lookup_chain_cache(struct task_struct *curr,
  1785. struct held_lock *hlock,
  1786. u64 chain_key)
  1787. {
  1788. struct lock_class *class = hlock_class(hlock);
  1789. struct hlist_head *hash_head = chainhashentry(chain_key);
  1790. struct lock_chain *chain;
  1791. int i, j;
  1792. /*
  1793. * We might need to take the graph lock, ensure we've got IRQs
  1794. * disabled to make this an IRQ-safe lock.. for recursion reasons
  1795. * lockdep won't complain about its own locking errors.
  1796. */
  1797. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  1798. return 0;
  1799. /*
  1800. * We can walk it lock-free, because entries only get added
  1801. * to the hash:
  1802. */
  1803. hlist_for_each_entry_rcu(chain, hash_head, entry) {
  1804. if (chain->chain_key == chain_key) {
  1805. cache_hit:
  1806. debug_atomic_inc(chain_lookup_hits);
  1807. if (!check_no_collision(curr, hlock, chain))
  1808. return 0;
  1809. if (very_verbose(class))
  1810. printk("\nhash chain already cached, key: "
  1811. "%016Lx tail class: [%p] %s\n",
  1812. (unsigned long long)chain_key,
  1813. class->key, class->name);
  1814. return 0;
  1815. }
  1816. }
  1817. if (very_verbose(class))
  1818. printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
  1819. (unsigned long long)chain_key, class->key, class->name);
  1820. /*
  1821. * Allocate a new chain entry from the static array, and add
  1822. * it to the hash:
  1823. */
  1824. if (!graph_lock())
  1825. return 0;
  1826. /*
  1827. * We have to walk the chain again locked - to avoid duplicates:
  1828. */
  1829. hlist_for_each_entry(chain, hash_head, entry) {
  1830. if (chain->chain_key == chain_key) {
  1831. graph_unlock();
  1832. goto cache_hit;
  1833. }
  1834. }
  1835. if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
  1836. if (!debug_locks_off_graph_unlock())
  1837. return 0;
  1838. print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
  1839. dump_stack();
  1840. return 0;
  1841. }
  1842. chain = lock_chains + nr_lock_chains++;
  1843. chain->chain_key = chain_key;
  1844. chain->irq_context = hlock->irq_context;
  1845. i = get_first_held_lock(curr, hlock);
  1846. chain->depth = curr->lockdep_depth + 1 - i;
  1847. BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
  1848. BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
  1849. BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
  1850. if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
  1851. chain->base = nr_chain_hlocks;
  1852. for (j = 0; j < chain->depth - 1; j++, i++) {
  1853. int lock_id = curr->held_locks[i].class_idx - 1;
  1854. chain_hlocks[chain->base + j] = lock_id;
  1855. }
  1856. chain_hlocks[chain->base + j] = class - lock_classes;
  1857. }
  1858. if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
  1859. nr_chain_hlocks += chain->depth;
  1860. #ifdef CONFIG_DEBUG_LOCKDEP
  1861. /*
  1862. * Important for check_no_collision().
  1863. */
  1864. if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
  1865. if (debug_locks_off_graph_unlock())
  1866. return 0;
  1867. print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
  1868. dump_stack();
  1869. return 0;
  1870. }
  1871. #endif
  1872. hlist_add_head_rcu(&chain->entry, hash_head);
  1873. debug_atomic_inc(chain_lookup_misses);
  1874. inc_chains();
  1875. return 1;
  1876. }
  1877. static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
  1878. struct held_lock *hlock, int chain_head, u64 chain_key)
  1879. {
  1880. /*
  1881. * Trylock needs to maintain the stack of held locks, but it
  1882. * does not add new dependencies, because trylock can be done
  1883. * in any order.
  1884. *
  1885. * We look up the chain_key and do the O(N^2) check and update of
  1886. * the dependencies only if this is a new dependency chain.
  1887. * (If lookup_chain_cache() returns with 1 it acquires
  1888. * graph_lock for us)
  1889. */
  1890. if (!hlock->trylock && hlock->check &&
  1891. lookup_chain_cache(curr, hlock, chain_key)) {
  1892. /*
  1893. * Check whether last held lock:
  1894. *
  1895. * - is irq-safe, if this lock is irq-unsafe
  1896. * - is softirq-safe, if this lock is hardirq-unsafe
  1897. *
  1898. * And check whether the new lock's dependency graph
  1899. * could lead back to the previous lock.
  1900. *
  1901. * any of these scenarios could lead to a deadlock. If
  1902. * All validations
  1903. */
  1904. int ret = check_deadlock(curr, hlock, lock, hlock->read);
  1905. if (!ret)
  1906. return 0;
  1907. /*
  1908. * Mark recursive read, as we jump over it when
  1909. * building dependencies (just like we jump over
  1910. * trylock entries):
  1911. */
  1912. if (ret == 2)
  1913. hlock->read = 2;
  1914. /*
  1915. * Add dependency only if this lock is not the head
  1916. * of the chain, and if it's not a secondary read-lock:
  1917. */
  1918. if (!chain_head && ret != 2)
  1919. if (!check_prevs_add(curr, hlock))
  1920. return 0;
  1921. graph_unlock();
  1922. } else
  1923. /* after lookup_chain_cache(): */
  1924. if (unlikely(!debug_locks))
  1925. return 0;
  1926. return 1;
  1927. }
  1928. #else
  1929. static inline int validate_chain(struct task_struct *curr,
  1930. struct lockdep_map *lock, struct held_lock *hlock,
  1931. int chain_head, u64 chain_key)
  1932. {
  1933. return 1;
  1934. }
  1935. #endif
  1936. /*
  1937. * We are building curr_chain_key incrementally, so double-check
  1938. * it from scratch, to make sure that it's done correctly:
  1939. */
  1940. static void check_chain_key(struct task_struct *curr)
  1941. {
  1942. #ifdef CONFIG_DEBUG_LOCKDEP
  1943. struct held_lock *hlock, *prev_hlock = NULL;
  1944. unsigned int i;
  1945. u64 chain_key = 0;
  1946. for (i = 0; i < curr->lockdep_depth; i++) {
  1947. hlock = curr->held_locks + i;
  1948. if (chain_key != hlock->prev_chain_key) {
  1949. debug_locks_off();
  1950. /*
  1951. * We got mighty confused, our chain keys don't match
  1952. * with what we expect, someone trample on our task state?
  1953. */
  1954. WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
  1955. curr->lockdep_depth, i,
  1956. (unsigned long long)chain_key,
  1957. (unsigned long long)hlock->prev_chain_key);
  1958. return;
  1959. }
  1960. /*
  1961. * Whoops ran out of static storage again?
  1962. */
  1963. if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
  1964. return;
  1965. if (prev_hlock && (prev_hlock->irq_context !=
  1966. hlock->irq_context))
  1967. chain_key = 0;
  1968. chain_key = iterate_chain_key(chain_key, hlock->class_idx);
  1969. prev_hlock = hlock;
  1970. }
  1971. if (chain_key != curr->curr_chain_key) {
  1972. debug_locks_off();
  1973. /*
  1974. * More smoking hash instead of calculating it, damn see these
  1975. * numbers float.. I bet that a pink elephant stepped on my memory.
  1976. */
  1977. WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
  1978. curr->lockdep_depth, i,
  1979. (unsigned long long)chain_key,
  1980. (unsigned long long)curr->curr_chain_key);
  1981. }
  1982. #endif
  1983. }
  1984. static void
  1985. print_usage_bug_scenario(struct held_lock *lock)
  1986. {
  1987. struct lock_class *class = hlock_class(lock);
  1988. printk(" Possible unsafe locking scenario:\n\n");
  1989. printk(" CPU0\n");
  1990. printk(" ----\n");
  1991. printk(" lock(");
  1992. __print_lock_name(class);
  1993. printk(KERN_CONT ");\n");
  1994. printk(" <Interrupt>\n");
  1995. printk(" lock(");
  1996. __print_lock_name(class);
  1997. printk(KERN_CONT ");\n");
  1998. printk("\n *** DEADLOCK ***\n\n");
  1999. }
  2000. static int
  2001. print_usage_bug(struct task_struct *curr, struct held_lock *this,
  2002. enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
  2003. {
  2004. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  2005. return 0;
  2006. printk("\n");
  2007. printk("=================================\n");
  2008. printk("[ INFO: inconsistent lock state ]\n");
  2009. print_kernel_ident();
  2010. printk("---------------------------------\n");
  2011. printk("inconsistent {%s} -> {%s} usage.\n",
  2012. usage_str[prev_bit], usage_str[new_bit]);
  2013. printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
  2014. curr->comm, task_pid_nr(curr),
  2015. trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
  2016. trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
  2017. trace_hardirqs_enabled(curr),
  2018. trace_softirqs_enabled(curr));
  2019. print_lock(this);
  2020. printk("{%s} state was registered at:\n", usage_str[prev_bit]);
  2021. print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
  2022. print_irqtrace_events(curr);
  2023. printk("\nother info that might help us debug this:\n");
  2024. print_usage_bug_scenario(this);
  2025. lockdep_print_held_locks(curr);
  2026. printk("\nstack backtrace:\n");
  2027. dump_stack();
  2028. return 0;
  2029. }
  2030. /*
  2031. * Print out an error if an invalid bit is set:
  2032. */
  2033. static inline int
  2034. valid_state(struct task_struct *curr, struct held_lock *this,
  2035. enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
  2036. {
  2037. if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
  2038. return print_usage_bug(curr, this, bad_bit, new_bit);
  2039. return 1;
  2040. }
  2041. static int mark_lock(struct task_struct *curr, struct held_lock *this,
  2042. enum lock_usage_bit new_bit);
  2043. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
  2044. /*
  2045. * print irq inversion bug:
  2046. */
  2047. static int
  2048. print_irq_inversion_bug(struct task_struct *curr,
  2049. struct lock_list *root, struct lock_list *other,
  2050. struct held_lock *this, int forwards,
  2051. const char *irqclass)
  2052. {
  2053. struct lock_list *entry = other;
  2054. struct lock_list *middle = NULL;
  2055. int depth;
  2056. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  2057. return 0;
  2058. printk("\n");
  2059. printk("=========================================================\n");
  2060. printk("[ INFO: possible irq lock inversion dependency detected ]\n");
  2061. print_kernel_ident();
  2062. printk("---------------------------------------------------------\n");
  2063. printk("%s/%d just changed the state of lock:\n",
  2064. curr->comm, task_pid_nr(curr));
  2065. print_lock(this);
  2066. if (forwards)
  2067. printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
  2068. else
  2069. printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
  2070. print_lock_name(other->class);
  2071. printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
  2072. printk("\nother info that might help us debug this:\n");
  2073. /* Find a middle lock (if one exists) */
  2074. depth = get_lock_depth(other);
  2075. do {
  2076. if (depth == 0 && (entry != root)) {
  2077. printk("lockdep:%s bad path found in chain graph\n", __func__);
  2078. break;
  2079. }
  2080. middle = entry;
  2081. entry = get_lock_parent(entry);
  2082. depth--;
  2083. } while (entry && entry != root && (depth >= 0));
  2084. if (forwards)
  2085. print_irq_lock_scenario(root, other,
  2086. middle ? middle->class : root->class, other->class);
  2087. else
  2088. print_irq_lock_scenario(other, root,
  2089. middle ? middle->class : other->class, root->class);
  2090. lockdep_print_held_locks(curr);
  2091. printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
  2092. if (!save_trace(&root->trace))
  2093. return 0;
  2094. print_shortest_lock_dependencies(other, root);
  2095. printk("\nstack backtrace:\n");
  2096. dump_stack();
  2097. return 0;
  2098. }
  2099. /*
  2100. * Prove that in the forwards-direction subgraph starting at <this>
  2101. * there is no lock matching <mask>:
  2102. */
  2103. static int
  2104. check_usage_forwards(struct task_struct *curr, struct held_lock *this,
  2105. enum lock_usage_bit bit, const char *irqclass)
  2106. {
  2107. int ret;
  2108. struct lock_list root;
  2109. struct lock_list *uninitialized_var(target_entry);
  2110. root.parent = NULL;
  2111. root.class = hlock_class(this);
  2112. ret = find_usage_forwards(&root, bit, &target_entry);
  2113. if (ret < 0)
  2114. return print_bfs_bug(ret);
  2115. if (ret == 1)
  2116. return ret;
  2117. return print_irq_inversion_bug(curr, &root, target_entry,
  2118. this, 1, irqclass);
  2119. }
  2120. /*
  2121. * Prove that in the backwards-direction subgraph starting at <this>
  2122. * there is no lock matching <mask>:
  2123. */
  2124. static int
  2125. check_usage_backwards(struct task_struct *curr, struct held_lock *this,
  2126. enum lock_usage_bit bit, const char *irqclass)
  2127. {
  2128. int ret;
  2129. struct lock_list root;
  2130. struct lock_list *uninitialized_var(target_entry);
  2131. root.parent = NULL;
  2132. root.class = hlock_class(this);
  2133. ret = find_usage_backwards(&root, bit, &target_entry);
  2134. if (ret < 0)
  2135. return print_bfs_bug(ret);
  2136. if (ret == 1)
  2137. return ret;
  2138. return print_irq_inversion_bug(curr, &root, target_entry,
  2139. this, 0, irqclass);
  2140. }
  2141. void print_irqtrace_events(struct task_struct *curr)
  2142. {
  2143. printk("irq event stamp: %u\n", curr->irq_events);
  2144. printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
  2145. curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
  2146. (void *)curr->hardirq_enable_ip);
  2147. printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
  2148. curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
  2149. (void *)curr->hardirq_disable_ip);
  2150. printk("softirqs last enabled at (%u): [<%p>] %pS\n",
  2151. curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
  2152. (void *)curr->softirq_enable_ip);
  2153. printk("softirqs last disabled at (%u): [<%p>] %pS\n",
  2154. curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
  2155. (void *)curr->softirq_disable_ip);
  2156. }
  2157. static int HARDIRQ_verbose(struct lock_class *class)
  2158. {
  2159. #if HARDIRQ_VERBOSE
  2160. return class_filter(class);
  2161. #endif
  2162. return 0;
  2163. }
  2164. static int SOFTIRQ_verbose(struct lock_class *class)
  2165. {
  2166. #if SOFTIRQ_VERBOSE
  2167. return class_filter(class);
  2168. #endif
  2169. return 0;
  2170. }
  2171. static int RECLAIM_FS_verbose(struct lock_class *class)
  2172. {
  2173. #if RECLAIM_VERBOSE
  2174. return class_filter(class);
  2175. #endif
  2176. return 0;
  2177. }
  2178. #define STRICT_READ_CHECKS 1
  2179. static int (*state_verbose_f[])(struct lock_class *class) = {
  2180. #define LOCKDEP_STATE(__STATE) \
  2181. __STATE##_verbose,
  2182. #include "lockdep_states.h"
  2183. #undef LOCKDEP_STATE
  2184. };
  2185. static inline int state_verbose(enum lock_usage_bit bit,
  2186. struct lock_class *class)
  2187. {
  2188. return state_verbose_f[bit >> 2](class);
  2189. }
  2190. typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
  2191. enum lock_usage_bit bit, const char *name);
  2192. static int
  2193. mark_lock_irq(struct task_struct *curr, struct held_lock *this,
  2194. enum lock_usage_bit new_bit)
  2195. {
  2196. int excl_bit = exclusive_bit(new_bit);
  2197. int read = new_bit & 1;
  2198. int dir = new_bit & 2;
  2199. /*
  2200. * mark USED_IN has to look forwards -- to ensure no dependency
  2201. * has ENABLED state, which would allow recursion deadlocks.
  2202. *
  2203. * mark ENABLED has to look backwards -- to ensure no dependee
  2204. * has USED_IN state, which, again, would allow recursion deadlocks.
  2205. */
  2206. check_usage_f usage = dir ?
  2207. check_usage_backwards : check_usage_forwards;
  2208. /*
  2209. * Validate that this particular lock does not have conflicting
  2210. * usage states.
  2211. */
  2212. if (!valid_state(curr, this, new_bit, excl_bit))
  2213. return 0;
  2214. /*
  2215. * Validate that the lock dependencies don't have conflicting usage
  2216. * states.
  2217. */
  2218. if ((!read || !dir || STRICT_READ_CHECKS) &&
  2219. !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
  2220. return 0;
  2221. /*
  2222. * Check for read in write conflicts
  2223. */
  2224. if (!read) {
  2225. if (!valid_state(curr, this, new_bit, excl_bit + 1))
  2226. return 0;
  2227. if (STRICT_READ_CHECKS &&
  2228. !usage(curr, this, excl_bit + 1,
  2229. state_name(new_bit + 1)))
  2230. return 0;
  2231. }
  2232. if (state_verbose(new_bit, hlock_class(this)))
  2233. return 2;
  2234. return 1;
  2235. }
  2236. enum mark_type {
  2237. #define LOCKDEP_STATE(__STATE) __STATE,
  2238. #include "lockdep_states.h"
  2239. #undef LOCKDEP_STATE
  2240. };
  2241. /*
  2242. * Mark all held locks with a usage bit:
  2243. */
  2244. static int
  2245. mark_held_locks(struct task_struct *curr, enum mark_type mark)
  2246. {
  2247. enum lock_usage_bit usage_bit;
  2248. struct held_lock *hlock;
  2249. int i;
  2250. for (i = 0; i < curr->lockdep_depth; i++) {
  2251. hlock = curr->held_locks + i;
  2252. usage_bit = 2 + (mark << 2); /* ENABLED */
  2253. if (hlock->read)
  2254. usage_bit += 1; /* READ */
  2255. BUG_ON(usage_bit >= LOCK_USAGE_STATES);
  2256. if (!hlock->check)
  2257. continue;
  2258. if (!mark_lock(curr, hlock, usage_bit))
  2259. return 0;
  2260. }
  2261. return 1;
  2262. }
  2263. /*
  2264. * Hardirqs will be enabled:
  2265. */
  2266. static void __trace_hardirqs_on_caller(unsigned long ip)
  2267. {
  2268. struct task_struct *curr = current;
  2269. /* we'll do an OFF -> ON transition: */
  2270. curr->hardirqs_enabled = 1;
  2271. /*
  2272. * We are going to turn hardirqs on, so set the
  2273. * usage bit for all held locks:
  2274. */
  2275. if (!mark_held_locks(curr, HARDIRQ))
  2276. return;
  2277. /*
  2278. * If we have softirqs enabled, then set the usage
  2279. * bit for all held locks. (disabled hardirqs prevented
  2280. * this bit from being set before)
  2281. */
  2282. if (curr->softirqs_enabled)
  2283. if (!mark_held_locks(curr, SOFTIRQ))
  2284. return;
  2285. curr->hardirq_enable_ip = ip;
  2286. curr->hardirq_enable_event = ++curr->irq_events;
  2287. debug_atomic_inc(hardirqs_on_events);
  2288. }
  2289. __visible void trace_hardirqs_on_caller(unsigned long ip)
  2290. {
  2291. time_hardirqs_on(CALLER_ADDR0, ip);
  2292. if (unlikely(!debug_locks || current->lockdep_recursion))
  2293. return;
  2294. if (unlikely(current->hardirqs_enabled)) {
  2295. /*
  2296. * Neither irq nor preemption are disabled here
  2297. * so this is racy by nature but losing one hit
  2298. * in a stat is not a big deal.
  2299. */
  2300. __debug_atomic_inc(redundant_hardirqs_on);
  2301. return;
  2302. }
  2303. /*
  2304. * We're enabling irqs and according to our state above irqs weren't
  2305. * already enabled, yet we find the hardware thinks they are in fact
  2306. * enabled.. someone messed up their IRQ state tracing.
  2307. */
  2308. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2309. return;
  2310. /*
  2311. * See the fine text that goes along with this variable definition.
  2312. */
  2313. if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
  2314. return;
  2315. /*
  2316. * Can't allow enabling interrupts while in an interrupt handler,
  2317. * that's general bad form and such. Recursion, limited stack etc..
  2318. */
  2319. if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
  2320. return;
  2321. current->lockdep_recursion = 1;
  2322. __trace_hardirqs_on_caller(ip);
  2323. current->lockdep_recursion = 0;
  2324. }
  2325. EXPORT_SYMBOL(trace_hardirqs_on_caller);
  2326. void trace_hardirqs_on(void)
  2327. {
  2328. trace_hardirqs_on_caller(CALLER_ADDR0);
  2329. }
  2330. EXPORT_SYMBOL(trace_hardirqs_on);
  2331. /*
  2332. * Hardirqs were disabled:
  2333. */
  2334. __visible void trace_hardirqs_off_caller(unsigned long ip)
  2335. {
  2336. struct task_struct *curr = current;
  2337. time_hardirqs_off(CALLER_ADDR0, ip);
  2338. if (unlikely(!debug_locks || current->lockdep_recursion))
  2339. return;
  2340. /*
  2341. * So we're supposed to get called after you mask local IRQs, but for
  2342. * some reason the hardware doesn't quite think you did a proper job.
  2343. */
  2344. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2345. return;
  2346. if (curr->hardirqs_enabled) {
  2347. /*
  2348. * We have done an ON -> OFF transition:
  2349. */
  2350. curr->hardirqs_enabled = 0;
  2351. curr->hardirq_disable_ip = ip;
  2352. curr->hardirq_disable_event = ++curr->irq_events;
  2353. debug_atomic_inc(hardirqs_off_events);
  2354. } else
  2355. debug_atomic_inc(redundant_hardirqs_off);
  2356. }
  2357. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  2358. void trace_hardirqs_off(void)
  2359. {
  2360. trace_hardirqs_off_caller(CALLER_ADDR0);
  2361. }
  2362. EXPORT_SYMBOL(trace_hardirqs_off);
  2363. /*
  2364. * Softirqs will be enabled:
  2365. */
  2366. void trace_softirqs_on(unsigned long ip)
  2367. {
  2368. struct task_struct *curr = current;
  2369. if (unlikely(!debug_locks || current->lockdep_recursion))
  2370. return;
  2371. /*
  2372. * We fancy IRQs being disabled here, see softirq.c, avoids
  2373. * funny state and nesting things.
  2374. */
  2375. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2376. return;
  2377. if (curr->softirqs_enabled) {
  2378. debug_atomic_inc(redundant_softirqs_on);
  2379. return;
  2380. }
  2381. current->lockdep_recursion = 1;
  2382. /*
  2383. * We'll do an OFF -> ON transition:
  2384. */
  2385. curr->softirqs_enabled = 1;
  2386. curr->softirq_enable_ip = ip;
  2387. curr->softirq_enable_event = ++curr->irq_events;
  2388. debug_atomic_inc(softirqs_on_events);
  2389. /*
  2390. * We are going to turn softirqs on, so set the
  2391. * usage bit for all held locks, if hardirqs are
  2392. * enabled too:
  2393. */
  2394. if (curr->hardirqs_enabled)
  2395. mark_held_locks(curr, SOFTIRQ);
  2396. current->lockdep_recursion = 0;
  2397. }
  2398. /*
  2399. * Softirqs were disabled:
  2400. */
  2401. void trace_softirqs_off(unsigned long ip)
  2402. {
  2403. struct task_struct *curr = current;
  2404. if (unlikely(!debug_locks || current->lockdep_recursion))
  2405. return;
  2406. /*
  2407. * We fancy IRQs being disabled here, see softirq.c
  2408. */
  2409. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2410. return;
  2411. if (curr->softirqs_enabled) {
  2412. /*
  2413. * We have done an ON -> OFF transition:
  2414. */
  2415. curr->softirqs_enabled = 0;
  2416. curr->softirq_disable_ip = ip;
  2417. curr->softirq_disable_event = ++curr->irq_events;
  2418. debug_atomic_inc(softirqs_off_events);
  2419. /*
  2420. * Whoops, we wanted softirqs off, so why aren't they?
  2421. */
  2422. DEBUG_LOCKS_WARN_ON(!softirq_count());
  2423. } else
  2424. debug_atomic_inc(redundant_softirqs_off);
  2425. }
  2426. static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
  2427. {
  2428. struct task_struct *curr = current;
  2429. if (unlikely(!debug_locks))
  2430. return;
  2431. /* no reclaim without waiting on it */
  2432. if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
  2433. return;
  2434. /* this guy won't enter reclaim */
  2435. if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
  2436. return;
  2437. /* We're only interested __GFP_FS allocations for now */
  2438. if (!(gfp_mask & __GFP_FS))
  2439. return;
  2440. /*
  2441. * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
  2442. */
  2443. if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
  2444. return;
  2445. mark_held_locks(curr, RECLAIM_FS);
  2446. }
  2447. static void check_flags(unsigned long flags);
  2448. void lockdep_trace_alloc(gfp_t gfp_mask)
  2449. {
  2450. unsigned long flags;
  2451. if (unlikely(current->lockdep_recursion))
  2452. return;
  2453. raw_local_irq_save(flags);
  2454. check_flags(flags);
  2455. current->lockdep_recursion = 1;
  2456. __lockdep_trace_alloc(gfp_mask, flags);
  2457. current->lockdep_recursion = 0;
  2458. raw_local_irq_restore(flags);
  2459. }
  2460. static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
  2461. {
  2462. /*
  2463. * If non-trylock use in a hardirq or softirq context, then
  2464. * mark the lock as used in these contexts:
  2465. */
  2466. if (!hlock->trylock) {
  2467. if (hlock->read) {
  2468. if (curr->hardirq_context)
  2469. if (!mark_lock(curr, hlock,
  2470. LOCK_USED_IN_HARDIRQ_READ))
  2471. return 0;
  2472. if (curr->softirq_context)
  2473. if (!mark_lock(curr, hlock,
  2474. LOCK_USED_IN_SOFTIRQ_READ))
  2475. return 0;
  2476. } else {
  2477. if (curr->hardirq_context)
  2478. if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
  2479. return 0;
  2480. if (curr->softirq_context)
  2481. if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
  2482. return 0;
  2483. }
  2484. }
  2485. if (!hlock->hardirqs_off) {
  2486. if (hlock->read) {
  2487. if (!mark_lock(curr, hlock,
  2488. LOCK_ENABLED_HARDIRQ_READ))
  2489. return 0;
  2490. if (curr->softirqs_enabled)
  2491. if (!mark_lock(curr, hlock,
  2492. LOCK_ENABLED_SOFTIRQ_READ))
  2493. return 0;
  2494. } else {
  2495. if (!mark_lock(curr, hlock,
  2496. LOCK_ENABLED_HARDIRQ))
  2497. return 0;
  2498. if (curr->softirqs_enabled)
  2499. if (!mark_lock(curr, hlock,
  2500. LOCK_ENABLED_SOFTIRQ))
  2501. return 0;
  2502. }
  2503. }
  2504. /*
  2505. * We reuse the irq context infrastructure more broadly as a general
  2506. * context checking code. This tests GFP_FS recursion (a lock taken
  2507. * during reclaim for a GFP_FS allocation is held over a GFP_FS
  2508. * allocation).
  2509. */
  2510. if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
  2511. if (hlock->read) {
  2512. if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
  2513. return 0;
  2514. } else {
  2515. if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
  2516. return 0;
  2517. }
  2518. }
  2519. return 1;
  2520. }
  2521. static inline unsigned int task_irq_context(struct task_struct *task)
  2522. {
  2523. return 2 * !!task->hardirq_context + !!task->softirq_context;
  2524. }
  2525. static int separate_irq_context(struct task_struct *curr,
  2526. struct held_lock *hlock)
  2527. {
  2528. unsigned int depth = curr->lockdep_depth;
  2529. /*
  2530. * Keep track of points where we cross into an interrupt context:
  2531. */
  2532. if (depth) {
  2533. struct held_lock *prev_hlock;
  2534. prev_hlock = curr->held_locks + depth-1;
  2535. /*
  2536. * If we cross into another context, reset the
  2537. * hash key (this also prevents the checking and the
  2538. * adding of the dependency to 'prev'):
  2539. */
  2540. if (prev_hlock->irq_context != hlock->irq_context)
  2541. return 1;
  2542. }
  2543. return 0;
  2544. }
  2545. #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
  2546. static inline
  2547. int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
  2548. enum lock_usage_bit new_bit)
  2549. {
  2550. WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
  2551. return 1;
  2552. }
  2553. static inline int mark_irqflags(struct task_struct *curr,
  2554. struct held_lock *hlock)
  2555. {
  2556. return 1;
  2557. }
  2558. static inline unsigned int task_irq_context(struct task_struct *task)
  2559. {
  2560. return 0;
  2561. }
  2562. static inline int separate_irq_context(struct task_struct *curr,
  2563. struct held_lock *hlock)
  2564. {
  2565. return 0;
  2566. }
  2567. void lockdep_trace_alloc(gfp_t gfp_mask)
  2568. {
  2569. }
  2570. #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
  2571. /*
  2572. * Mark a lock with a usage bit, and validate the state transition:
  2573. */
  2574. static int mark_lock(struct task_struct *curr, struct held_lock *this,
  2575. enum lock_usage_bit new_bit)
  2576. {
  2577. unsigned int new_mask = 1 << new_bit, ret = 1;
  2578. /*
  2579. * If already set then do not dirty the cacheline,
  2580. * nor do any checks:
  2581. */
  2582. if (likely(hlock_class(this)->usage_mask & new_mask))
  2583. return 1;
  2584. if (!graph_lock())
  2585. return 0;
  2586. /*
  2587. * Make sure we didn't race:
  2588. */
  2589. if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
  2590. graph_unlock();
  2591. return 1;
  2592. }
  2593. hlock_class(this)->usage_mask |= new_mask;
  2594. if (!save_trace(hlock_class(this)->usage_traces + new_bit))
  2595. return 0;
  2596. switch (new_bit) {
  2597. #define LOCKDEP_STATE(__STATE) \
  2598. case LOCK_USED_IN_##__STATE: \
  2599. case LOCK_USED_IN_##__STATE##_READ: \
  2600. case LOCK_ENABLED_##__STATE: \
  2601. case LOCK_ENABLED_##__STATE##_READ:
  2602. #include "lockdep_states.h"
  2603. #undef LOCKDEP_STATE
  2604. ret = mark_lock_irq(curr, this, new_bit);
  2605. if (!ret)
  2606. return 0;
  2607. break;
  2608. case LOCK_USED:
  2609. debug_atomic_dec(nr_unused_locks);
  2610. break;
  2611. default:
  2612. if (!debug_locks_off_graph_unlock())
  2613. return 0;
  2614. WARN_ON(1);
  2615. return 0;
  2616. }
  2617. graph_unlock();
  2618. /*
  2619. * We must printk outside of the graph_lock:
  2620. */
  2621. if (ret == 2) {
  2622. printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
  2623. print_lock(this);
  2624. print_irqtrace_events(curr);
  2625. dump_stack();
  2626. }
  2627. return ret;
  2628. }
  2629. /*
  2630. * Initialize a lock instance's lock-class mapping info:
  2631. */
  2632. void lockdep_init_map(struct lockdep_map *lock, const char *name,
  2633. struct lock_class_key *key, int subclass)
  2634. {
  2635. int i;
  2636. kmemcheck_mark_initialized(lock, sizeof(*lock));
  2637. for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
  2638. lock->class_cache[i] = NULL;
  2639. #ifdef CONFIG_LOCK_STAT
  2640. lock->cpu = raw_smp_processor_id();
  2641. #endif
  2642. /*
  2643. * Can't be having no nameless bastards around this place!
  2644. */
  2645. if (DEBUG_LOCKS_WARN_ON(!name)) {
  2646. lock->name = "NULL";
  2647. return;
  2648. }
  2649. lock->name = name;
  2650. /*
  2651. * No key, no joy, we need to hash something.
  2652. */
  2653. if (DEBUG_LOCKS_WARN_ON(!key))
  2654. return;
  2655. /*
  2656. * Sanity check, the lock-class key must be persistent:
  2657. */
  2658. if (!static_obj(key)) {
  2659. printk("BUG: key %p not in .data!\n", key);
  2660. /*
  2661. * What it says above ^^^^^, I suggest you read it.
  2662. */
  2663. DEBUG_LOCKS_WARN_ON(1);
  2664. return;
  2665. }
  2666. lock->key = key;
  2667. if (unlikely(!debug_locks))
  2668. return;
  2669. if (subclass) {
  2670. unsigned long flags;
  2671. if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
  2672. return;
  2673. raw_local_irq_save(flags);
  2674. current->lockdep_recursion = 1;
  2675. register_lock_class(lock, subclass, 1);
  2676. current->lockdep_recursion = 0;
  2677. raw_local_irq_restore(flags);
  2678. }
  2679. }
  2680. EXPORT_SYMBOL_GPL(lockdep_init_map);
  2681. struct lock_class_key __lockdep_no_validate__;
  2682. EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
  2683. static int
  2684. print_lock_nested_lock_not_held(struct task_struct *curr,
  2685. struct held_lock *hlock,
  2686. unsigned long ip)
  2687. {
  2688. if (!debug_locks_off())
  2689. return 0;
  2690. if (debug_locks_silent)
  2691. return 0;
  2692. printk("\n");
  2693. printk("==================================\n");
  2694. printk("[ BUG: Nested lock was not taken ]\n");
  2695. print_kernel_ident();
  2696. printk("----------------------------------\n");
  2697. printk("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
  2698. print_lock(hlock);
  2699. printk("\nbut this task is not holding:\n");
  2700. printk("%s\n", hlock->nest_lock->name);
  2701. printk("\nstack backtrace:\n");
  2702. dump_stack();
  2703. printk("\nother info that might help us debug this:\n");
  2704. lockdep_print_held_locks(curr);
  2705. printk("\nstack backtrace:\n");
  2706. dump_stack();
  2707. return 0;
  2708. }
  2709. static int __lock_is_held(struct lockdep_map *lock);
  2710. /*
  2711. * This gets called for every mutex_lock*()/spin_lock*() operation.
  2712. * We maintain the dependency maps and validate the locking attempt:
  2713. */
  2714. static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  2715. int trylock, int read, int check, int hardirqs_off,
  2716. struct lockdep_map *nest_lock, unsigned long ip,
  2717. int references, int pin_count)
  2718. {
  2719. struct task_struct *curr = current;
  2720. struct lock_class *class = NULL;
  2721. struct held_lock *hlock;
  2722. unsigned int depth;
  2723. int chain_head = 0;
  2724. int class_idx;
  2725. u64 chain_key;
  2726. if (unlikely(!debug_locks))
  2727. return 0;
  2728. /*
  2729. * Lockdep should run with IRQs disabled, otherwise we could
  2730. * get an interrupt which would want to take locks, which would
  2731. * end up in lockdep and have you got a head-ache already?
  2732. */
  2733. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2734. return 0;
  2735. if (!prove_locking || lock->key == &__lockdep_no_validate__)
  2736. check = 0;
  2737. if (subclass < NR_LOCKDEP_CACHING_CLASSES)
  2738. class = lock->class_cache[subclass];
  2739. /*
  2740. * Not cached?
  2741. */
  2742. if (unlikely(!class)) {
  2743. class = register_lock_class(lock, subclass, 0);
  2744. if (!class)
  2745. return 0;
  2746. }
  2747. atomic_inc((atomic_t *)&class->ops);
  2748. if (very_verbose(class)) {
  2749. printk("\nacquire class [%p] %s", class->key, class->name);
  2750. if (class->name_version > 1)
  2751. printk(KERN_CONT "#%d", class->name_version);
  2752. printk(KERN_CONT "\n");
  2753. dump_stack();
  2754. }
  2755. /*
  2756. * Add the lock to the list of currently held locks.
  2757. * (we dont increase the depth just yet, up until the
  2758. * dependency checks are done)
  2759. */
  2760. depth = curr->lockdep_depth;
  2761. /*
  2762. * Ran out of static storage for our per-task lock stack again have we?
  2763. */
  2764. if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
  2765. return 0;
  2766. class_idx = class - lock_classes + 1;
  2767. if (depth) {
  2768. hlock = curr->held_locks + depth - 1;
  2769. if (hlock->class_idx == class_idx && nest_lock) {
  2770. if (hlock->references)
  2771. hlock->references++;
  2772. else
  2773. hlock->references = 2;
  2774. return 1;
  2775. }
  2776. }
  2777. hlock = curr->held_locks + depth;
  2778. /*
  2779. * Plain impossible, we just registered it and checked it weren't no
  2780. * NULL like.. I bet this mushroom I ate was good!
  2781. */
  2782. if (DEBUG_LOCKS_WARN_ON(!class))
  2783. return 0;
  2784. hlock->class_idx = class_idx;
  2785. hlock->acquire_ip = ip;
  2786. hlock->instance = lock;
  2787. hlock->nest_lock = nest_lock;
  2788. hlock->irq_context = task_irq_context(curr);
  2789. hlock->trylock = trylock;
  2790. hlock->read = read;
  2791. hlock->check = check;
  2792. hlock->hardirqs_off = !!hardirqs_off;
  2793. hlock->references = references;
  2794. #ifdef CONFIG_LOCK_STAT
  2795. hlock->waittime_stamp = 0;
  2796. hlock->holdtime_stamp = lockstat_clock();
  2797. #endif
  2798. hlock->pin_count = pin_count;
  2799. if (check && !mark_irqflags(curr, hlock))
  2800. return 0;
  2801. /* mark it as used: */
  2802. if (!mark_lock(curr, hlock, LOCK_USED))
  2803. return 0;
  2804. /*
  2805. * Calculate the chain hash: it's the combined hash of all the
  2806. * lock keys along the dependency chain. We save the hash value
  2807. * at every step so that we can get the current hash easily
  2808. * after unlock. The chain hash is then used to cache dependency
  2809. * results.
  2810. *
  2811. * The 'key ID' is what is the most compact key value to drive
  2812. * the hash, not class->key.
  2813. */
  2814. /*
  2815. * Whoops, we did it again.. ran straight out of our static allocation.
  2816. */
  2817. if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
  2818. return 0;
  2819. chain_key = curr->curr_chain_key;
  2820. if (!depth) {
  2821. /*
  2822. * How can we have a chain hash when we ain't got no keys?!
  2823. */
  2824. if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
  2825. return 0;
  2826. chain_head = 1;
  2827. }
  2828. hlock->prev_chain_key = chain_key;
  2829. if (separate_irq_context(curr, hlock)) {
  2830. chain_key = 0;
  2831. chain_head = 1;
  2832. }
  2833. chain_key = iterate_chain_key(chain_key, class_idx);
  2834. if (nest_lock && !__lock_is_held(nest_lock))
  2835. return print_lock_nested_lock_not_held(curr, hlock, ip);
  2836. if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
  2837. return 0;
  2838. curr->curr_chain_key = chain_key;
  2839. curr->lockdep_depth++;
  2840. check_chain_key(curr);
  2841. #ifdef CONFIG_DEBUG_LOCKDEP
  2842. if (unlikely(!debug_locks))
  2843. return 0;
  2844. #endif
  2845. if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
  2846. debug_locks_off();
  2847. print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
  2848. printk(KERN_DEBUG "depth: %i max: %lu!\n",
  2849. curr->lockdep_depth, MAX_LOCK_DEPTH);
  2850. lockdep_print_held_locks(current);
  2851. debug_show_all_locks();
  2852. dump_stack();
  2853. return 0;
  2854. }
  2855. if (unlikely(curr->lockdep_depth > max_lockdep_depth))
  2856. max_lockdep_depth = curr->lockdep_depth;
  2857. return 1;
  2858. }
  2859. static int
  2860. print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
  2861. unsigned long ip)
  2862. {
  2863. if (!debug_locks_off())
  2864. return 0;
  2865. if (debug_locks_silent)
  2866. return 0;
  2867. printk("\n");
  2868. printk("=====================================\n");
  2869. printk("[ BUG: bad unlock balance detected! ]\n");
  2870. print_kernel_ident();
  2871. printk("-------------------------------------\n");
  2872. printk("%s/%d is trying to release lock (",
  2873. curr->comm, task_pid_nr(curr));
  2874. print_lockdep_cache(lock);
  2875. printk(KERN_CONT ") at:\n");
  2876. print_ip_sym(ip);
  2877. printk("but there are no more locks to release!\n");
  2878. printk("\nother info that might help us debug this:\n");
  2879. lockdep_print_held_locks(curr);
  2880. printk("\nstack backtrace:\n");
  2881. dump_stack();
  2882. return 0;
  2883. }
  2884. static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
  2885. {
  2886. if (hlock->instance == lock)
  2887. return 1;
  2888. if (hlock->references) {
  2889. struct lock_class *class = lock->class_cache[0];
  2890. if (!class)
  2891. class = look_up_lock_class(lock, 0);
  2892. /*
  2893. * If look_up_lock_class() failed to find a class, we're trying
  2894. * to test if we hold a lock that has never yet been acquired.
  2895. * Clearly if the lock hasn't been acquired _ever_, we're not
  2896. * holding it either, so report failure.
  2897. */
  2898. if (!class)
  2899. return 0;
  2900. /*
  2901. * References, but not a lock we're actually ref-counting?
  2902. * State got messed up, follow the sites that change ->references
  2903. * and try to make sense of it.
  2904. */
  2905. if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
  2906. return 0;
  2907. if (hlock->class_idx == class - lock_classes + 1)
  2908. return 1;
  2909. }
  2910. return 0;
  2911. }
  2912. static int
  2913. __lock_set_class(struct lockdep_map *lock, const char *name,
  2914. struct lock_class_key *key, unsigned int subclass,
  2915. unsigned long ip)
  2916. {
  2917. struct task_struct *curr = current;
  2918. struct held_lock *hlock, *prev_hlock;
  2919. struct lock_class *class;
  2920. unsigned int depth;
  2921. int i;
  2922. depth = curr->lockdep_depth;
  2923. /*
  2924. * This function is about (re)setting the class of a held lock,
  2925. * yet we're not actually holding any locks. Naughty user!
  2926. */
  2927. if (DEBUG_LOCKS_WARN_ON(!depth))
  2928. return 0;
  2929. prev_hlock = NULL;
  2930. for (i = depth-1; i >= 0; i--) {
  2931. hlock = curr->held_locks + i;
  2932. /*
  2933. * We must not cross into another context:
  2934. */
  2935. if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
  2936. break;
  2937. if (match_held_lock(hlock, lock))
  2938. goto found_it;
  2939. prev_hlock = hlock;
  2940. }
  2941. return print_unlock_imbalance_bug(curr, lock, ip);
  2942. found_it:
  2943. lockdep_init_map(lock, name, key, 0);
  2944. class = register_lock_class(lock, subclass, 0);
  2945. hlock->class_idx = class - lock_classes + 1;
  2946. curr->lockdep_depth = i;
  2947. curr->curr_chain_key = hlock->prev_chain_key;
  2948. for (; i < depth; i++) {
  2949. hlock = curr->held_locks + i;
  2950. if (!__lock_acquire(hlock->instance,
  2951. hlock_class(hlock)->subclass, hlock->trylock,
  2952. hlock->read, hlock->check, hlock->hardirqs_off,
  2953. hlock->nest_lock, hlock->acquire_ip,
  2954. hlock->references, hlock->pin_count))
  2955. return 0;
  2956. }
  2957. /*
  2958. * I took it apart and put it back together again, except now I have
  2959. * these 'spare' parts.. where shall I put them.
  2960. */
  2961. if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
  2962. return 0;
  2963. return 1;
  2964. }
  2965. /*
  2966. * Remove the lock to the list of currently held locks - this gets
  2967. * called on mutex_unlock()/spin_unlock*() (or on a failed
  2968. * mutex_lock_interruptible()).
  2969. *
  2970. * @nested is an hysterical artifact, needs a tree wide cleanup.
  2971. */
  2972. static int
  2973. __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
  2974. {
  2975. struct task_struct *curr = current;
  2976. struct held_lock *hlock, *prev_hlock;
  2977. unsigned int depth;
  2978. int i;
  2979. if (unlikely(!debug_locks))
  2980. return 0;
  2981. depth = curr->lockdep_depth;
  2982. /*
  2983. * So we're all set to release this lock.. wait what lock? We don't
  2984. * own any locks, you've been drinking again?
  2985. */
  2986. if (DEBUG_LOCKS_WARN_ON(depth <= 0))
  2987. return print_unlock_imbalance_bug(curr, lock, ip);
  2988. /*
  2989. * Check whether the lock exists in the current stack
  2990. * of held locks:
  2991. */
  2992. prev_hlock = NULL;
  2993. for (i = depth-1; i >= 0; i--) {
  2994. hlock = curr->held_locks + i;
  2995. /*
  2996. * We must not cross into another context:
  2997. */
  2998. if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
  2999. break;
  3000. if (match_held_lock(hlock, lock))
  3001. goto found_it;
  3002. prev_hlock = hlock;
  3003. }
  3004. return print_unlock_imbalance_bug(curr, lock, ip);
  3005. found_it:
  3006. if (hlock->instance == lock)
  3007. lock_release_holdtime(hlock);
  3008. WARN(hlock->pin_count, "releasing a pinned lock\n");
  3009. if (hlock->references) {
  3010. hlock->references--;
  3011. if (hlock->references) {
  3012. /*
  3013. * We had, and after removing one, still have
  3014. * references, the current lock stack is still
  3015. * valid. We're done!
  3016. */
  3017. return 1;
  3018. }
  3019. }
  3020. /*
  3021. * We have the right lock to unlock, 'hlock' points to it.
  3022. * Now we remove it from the stack, and add back the other
  3023. * entries (if any), recalculating the hash along the way:
  3024. */
  3025. curr->lockdep_depth = i;
  3026. curr->curr_chain_key = hlock->prev_chain_key;
  3027. for (i++; i < depth; i++) {
  3028. hlock = curr->held_locks + i;
  3029. if (!__lock_acquire(hlock->instance,
  3030. hlock_class(hlock)->subclass, hlock->trylock,
  3031. hlock->read, hlock->check, hlock->hardirqs_off,
  3032. hlock->nest_lock, hlock->acquire_ip,
  3033. hlock->references, hlock->pin_count))
  3034. return 0;
  3035. }
  3036. /*
  3037. * We had N bottles of beer on the wall, we drank one, but now
  3038. * there's not N-1 bottles of beer left on the wall...
  3039. */
  3040. if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
  3041. return 0;
  3042. return 1;
  3043. }
  3044. static int __lock_is_held(struct lockdep_map *lock)
  3045. {
  3046. struct task_struct *curr = current;
  3047. int i;
  3048. for (i = 0; i < curr->lockdep_depth; i++) {
  3049. struct held_lock *hlock = curr->held_locks + i;
  3050. if (match_held_lock(hlock, lock))
  3051. return 1;
  3052. }
  3053. return 0;
  3054. }
  3055. static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
  3056. {
  3057. struct pin_cookie cookie = NIL_COOKIE;
  3058. struct task_struct *curr = current;
  3059. int i;
  3060. if (unlikely(!debug_locks))
  3061. return cookie;
  3062. for (i = 0; i < curr->lockdep_depth; i++) {
  3063. struct held_lock *hlock = curr->held_locks + i;
  3064. if (match_held_lock(hlock, lock)) {
  3065. /*
  3066. * Grab 16bits of randomness; this is sufficient to not
  3067. * be guessable and still allows some pin nesting in
  3068. * our u32 pin_count.
  3069. */
  3070. cookie.val = 1 + (prandom_u32() >> 16);
  3071. hlock->pin_count += cookie.val;
  3072. return cookie;
  3073. }
  3074. }
  3075. WARN(1, "pinning an unheld lock\n");
  3076. return cookie;
  3077. }
  3078. static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3079. {
  3080. struct task_struct *curr = current;
  3081. int i;
  3082. if (unlikely(!debug_locks))
  3083. return;
  3084. for (i = 0; i < curr->lockdep_depth; i++) {
  3085. struct held_lock *hlock = curr->held_locks + i;
  3086. if (match_held_lock(hlock, lock)) {
  3087. hlock->pin_count += cookie.val;
  3088. return;
  3089. }
  3090. }
  3091. WARN(1, "pinning an unheld lock\n");
  3092. }
  3093. static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3094. {
  3095. struct task_struct *curr = current;
  3096. int i;
  3097. if (unlikely(!debug_locks))
  3098. return;
  3099. for (i = 0; i < curr->lockdep_depth; i++) {
  3100. struct held_lock *hlock = curr->held_locks + i;
  3101. if (match_held_lock(hlock, lock)) {
  3102. if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
  3103. return;
  3104. hlock->pin_count -= cookie.val;
  3105. if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
  3106. hlock->pin_count = 0;
  3107. return;
  3108. }
  3109. }
  3110. WARN(1, "unpinning an unheld lock\n");
  3111. }
  3112. /*
  3113. * Check whether we follow the irq-flags state precisely:
  3114. */
  3115. static void check_flags(unsigned long flags)
  3116. {
  3117. #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
  3118. defined(CONFIG_TRACE_IRQFLAGS)
  3119. if (!debug_locks)
  3120. return;
  3121. if (irqs_disabled_flags(flags)) {
  3122. if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
  3123. printk("possible reason: unannotated irqs-off.\n");
  3124. }
  3125. } else {
  3126. if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
  3127. printk("possible reason: unannotated irqs-on.\n");
  3128. }
  3129. }
  3130. /*
  3131. * We dont accurately track softirq state in e.g.
  3132. * hardirq contexts (such as on 4KSTACKS), so only
  3133. * check if not in hardirq contexts:
  3134. */
  3135. if (!hardirq_count()) {
  3136. if (softirq_count()) {
  3137. /* like the above, but with softirqs */
  3138. DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
  3139. } else {
  3140. /* lick the above, does it taste good? */
  3141. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  3142. }
  3143. }
  3144. if (!debug_locks)
  3145. print_irqtrace_events(current);
  3146. #endif
  3147. }
  3148. void lock_set_class(struct lockdep_map *lock, const char *name,
  3149. struct lock_class_key *key, unsigned int subclass,
  3150. unsigned long ip)
  3151. {
  3152. unsigned long flags;
  3153. if (unlikely(current->lockdep_recursion))
  3154. return;
  3155. raw_local_irq_save(flags);
  3156. current->lockdep_recursion = 1;
  3157. check_flags(flags);
  3158. if (__lock_set_class(lock, name, key, subclass, ip))
  3159. check_chain_key(current);
  3160. current->lockdep_recursion = 0;
  3161. raw_local_irq_restore(flags);
  3162. }
  3163. EXPORT_SYMBOL_GPL(lock_set_class);
  3164. /*
  3165. * We are not always called with irqs disabled - do that here,
  3166. * and also avoid lockdep recursion:
  3167. */
  3168. void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  3169. int trylock, int read, int check,
  3170. struct lockdep_map *nest_lock, unsigned long ip)
  3171. {
  3172. unsigned long flags;
  3173. if (unlikely(current->lockdep_recursion))
  3174. return;
  3175. raw_local_irq_save(flags);
  3176. check_flags(flags);
  3177. current->lockdep_recursion = 1;
  3178. trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
  3179. __lock_acquire(lock, subclass, trylock, read, check,
  3180. irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
  3181. current->lockdep_recursion = 0;
  3182. raw_local_irq_restore(flags);
  3183. }
  3184. EXPORT_SYMBOL_GPL(lock_acquire);
  3185. void lock_release(struct lockdep_map *lock, int nested,
  3186. unsigned long ip)
  3187. {
  3188. unsigned long flags;
  3189. if (unlikely(current->lockdep_recursion))
  3190. return;
  3191. raw_local_irq_save(flags);
  3192. check_flags(flags);
  3193. current->lockdep_recursion = 1;
  3194. trace_lock_release(lock, ip);
  3195. if (__lock_release(lock, nested, ip))
  3196. check_chain_key(current);
  3197. current->lockdep_recursion = 0;
  3198. raw_local_irq_restore(flags);
  3199. }
  3200. EXPORT_SYMBOL_GPL(lock_release);
  3201. int lock_is_held(struct lockdep_map *lock)
  3202. {
  3203. unsigned long flags;
  3204. int ret = 0;
  3205. if (unlikely(current->lockdep_recursion))
  3206. return 1; /* avoid false negative lockdep_assert_held() */
  3207. raw_local_irq_save(flags);
  3208. check_flags(flags);
  3209. current->lockdep_recursion = 1;
  3210. ret = __lock_is_held(lock);
  3211. current->lockdep_recursion = 0;
  3212. raw_local_irq_restore(flags);
  3213. return ret;
  3214. }
  3215. EXPORT_SYMBOL_GPL(lock_is_held);
  3216. struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
  3217. {
  3218. struct pin_cookie cookie = NIL_COOKIE;
  3219. unsigned long flags;
  3220. if (unlikely(current->lockdep_recursion))
  3221. return cookie;
  3222. raw_local_irq_save(flags);
  3223. check_flags(flags);
  3224. current->lockdep_recursion = 1;
  3225. cookie = __lock_pin_lock(lock);
  3226. current->lockdep_recursion = 0;
  3227. raw_local_irq_restore(flags);
  3228. return cookie;
  3229. }
  3230. EXPORT_SYMBOL_GPL(lock_pin_lock);
  3231. void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3232. {
  3233. unsigned long flags;
  3234. if (unlikely(current->lockdep_recursion))
  3235. return;
  3236. raw_local_irq_save(flags);
  3237. check_flags(flags);
  3238. current->lockdep_recursion = 1;
  3239. __lock_repin_lock(lock, cookie);
  3240. current->lockdep_recursion = 0;
  3241. raw_local_irq_restore(flags);
  3242. }
  3243. EXPORT_SYMBOL_GPL(lock_repin_lock);
  3244. void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3245. {
  3246. unsigned long flags;
  3247. if (unlikely(current->lockdep_recursion))
  3248. return;
  3249. raw_local_irq_save(flags);
  3250. check_flags(flags);
  3251. current->lockdep_recursion = 1;
  3252. __lock_unpin_lock(lock, cookie);
  3253. current->lockdep_recursion = 0;
  3254. raw_local_irq_restore(flags);
  3255. }
  3256. EXPORT_SYMBOL_GPL(lock_unpin_lock);
  3257. void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
  3258. {
  3259. current->lockdep_reclaim_gfp = gfp_mask;
  3260. }
  3261. void lockdep_clear_current_reclaim_state(void)
  3262. {
  3263. current->lockdep_reclaim_gfp = 0;
  3264. }
  3265. #ifdef CONFIG_LOCK_STAT
  3266. static int
  3267. print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
  3268. unsigned long ip)
  3269. {
  3270. if (!debug_locks_off())
  3271. return 0;
  3272. if (debug_locks_silent)
  3273. return 0;
  3274. printk("\n");
  3275. printk("=================================\n");
  3276. printk("[ BUG: bad contention detected! ]\n");
  3277. print_kernel_ident();
  3278. printk("---------------------------------\n");
  3279. printk("%s/%d is trying to contend lock (",
  3280. curr->comm, task_pid_nr(curr));
  3281. print_lockdep_cache(lock);
  3282. printk(KERN_CONT ") at:\n");
  3283. print_ip_sym(ip);
  3284. printk("but there are no locks held!\n");
  3285. printk("\nother info that might help us debug this:\n");
  3286. lockdep_print_held_locks(curr);
  3287. printk("\nstack backtrace:\n");
  3288. dump_stack();
  3289. return 0;
  3290. }
  3291. static void
  3292. __lock_contended(struct lockdep_map *lock, unsigned long ip)
  3293. {
  3294. struct task_struct *curr = current;
  3295. struct held_lock *hlock, *prev_hlock;
  3296. struct lock_class_stats *stats;
  3297. unsigned int depth;
  3298. int i, contention_point, contending_point;
  3299. depth = curr->lockdep_depth;
  3300. /*
  3301. * Whee, we contended on this lock, except it seems we're not
  3302. * actually trying to acquire anything much at all..
  3303. */
  3304. if (DEBUG_LOCKS_WARN_ON(!depth))
  3305. return;
  3306. prev_hlock = NULL;
  3307. for (i = depth-1; i >= 0; i--) {
  3308. hlock = curr->held_locks + i;
  3309. /*
  3310. * We must not cross into another context:
  3311. */
  3312. if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
  3313. break;
  3314. if (match_held_lock(hlock, lock))
  3315. goto found_it;
  3316. prev_hlock = hlock;
  3317. }
  3318. print_lock_contention_bug(curr, lock, ip);
  3319. return;
  3320. found_it:
  3321. if (hlock->instance != lock)
  3322. return;
  3323. hlock->waittime_stamp = lockstat_clock();
  3324. contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
  3325. contending_point = lock_point(hlock_class(hlock)->contending_point,
  3326. lock->ip);
  3327. stats = get_lock_stats(hlock_class(hlock));
  3328. if (contention_point < LOCKSTAT_POINTS)
  3329. stats->contention_point[contention_point]++;
  3330. if (contending_point < LOCKSTAT_POINTS)
  3331. stats->contending_point[contending_point]++;
  3332. if (lock->cpu != smp_processor_id())
  3333. stats->bounces[bounce_contended + !!hlock->read]++;
  3334. put_lock_stats(stats);
  3335. }
  3336. static void
  3337. __lock_acquired(struct lockdep_map *lock, unsigned long ip)
  3338. {
  3339. struct task_struct *curr = current;
  3340. struct held_lock *hlock, *prev_hlock;
  3341. struct lock_class_stats *stats;
  3342. unsigned int depth;
  3343. u64 now, waittime = 0;
  3344. int i, cpu;
  3345. depth = curr->lockdep_depth;
  3346. /*
  3347. * Yay, we acquired ownership of this lock we didn't try to
  3348. * acquire, how the heck did that happen?
  3349. */
  3350. if (DEBUG_LOCKS_WARN_ON(!depth))
  3351. return;
  3352. prev_hlock = NULL;
  3353. for (i = depth-1; i >= 0; i--) {
  3354. hlock = curr->held_locks + i;
  3355. /*
  3356. * We must not cross into another context:
  3357. */
  3358. if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
  3359. break;
  3360. if (match_held_lock(hlock, lock))
  3361. goto found_it;
  3362. prev_hlock = hlock;
  3363. }
  3364. print_lock_contention_bug(curr, lock, _RET_IP_);
  3365. return;
  3366. found_it:
  3367. if (hlock->instance != lock)
  3368. return;
  3369. cpu = smp_processor_id();
  3370. if (hlock->waittime_stamp) {
  3371. now = lockstat_clock();
  3372. waittime = now - hlock->waittime_stamp;
  3373. hlock->holdtime_stamp = now;
  3374. }
  3375. trace_lock_acquired(lock, ip);
  3376. stats = get_lock_stats(hlock_class(hlock));
  3377. if (waittime) {
  3378. if (hlock->read)
  3379. lock_time_inc(&stats->read_waittime, waittime);
  3380. else
  3381. lock_time_inc(&stats->write_waittime, waittime);
  3382. }
  3383. if (lock->cpu != cpu)
  3384. stats->bounces[bounce_acquired + !!hlock->read]++;
  3385. put_lock_stats(stats);
  3386. lock->cpu = cpu;
  3387. lock->ip = ip;
  3388. }
  3389. void lock_contended(struct lockdep_map *lock, unsigned long ip)
  3390. {
  3391. unsigned long flags;
  3392. if (unlikely(!lock_stat))
  3393. return;
  3394. if (unlikely(current->lockdep_recursion))
  3395. return;
  3396. raw_local_irq_save(flags);
  3397. check_flags(flags);
  3398. current->lockdep_recursion = 1;
  3399. trace_lock_contended(lock, ip);
  3400. __lock_contended(lock, ip);
  3401. current->lockdep_recursion = 0;
  3402. raw_local_irq_restore(flags);
  3403. }
  3404. EXPORT_SYMBOL_GPL(lock_contended);
  3405. void lock_acquired(struct lockdep_map *lock, unsigned long ip)
  3406. {
  3407. unsigned long flags;
  3408. if (unlikely(!lock_stat))
  3409. return;
  3410. if (unlikely(current->lockdep_recursion))
  3411. return;
  3412. raw_local_irq_save(flags);
  3413. check_flags(flags);
  3414. current->lockdep_recursion = 1;
  3415. __lock_acquired(lock, ip);
  3416. current->lockdep_recursion = 0;
  3417. raw_local_irq_restore(flags);
  3418. }
  3419. EXPORT_SYMBOL_GPL(lock_acquired);
  3420. #endif
  3421. /*
  3422. * Used by the testsuite, sanitize the validator state
  3423. * after a simulated failure:
  3424. */
  3425. void lockdep_reset(void)
  3426. {
  3427. unsigned long flags;
  3428. int i;
  3429. raw_local_irq_save(flags);
  3430. current->curr_chain_key = 0;
  3431. current->lockdep_depth = 0;
  3432. current->lockdep_recursion = 0;
  3433. memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
  3434. nr_hardirq_chains = 0;
  3435. nr_softirq_chains = 0;
  3436. nr_process_chains = 0;
  3437. debug_locks = 1;
  3438. for (i = 0; i < CHAINHASH_SIZE; i++)
  3439. INIT_HLIST_HEAD(chainhash_table + i);
  3440. raw_local_irq_restore(flags);
  3441. }
  3442. static void zap_class(struct lock_class *class)
  3443. {
  3444. int i;
  3445. /*
  3446. * Remove all dependencies this lock is
  3447. * involved in:
  3448. */
  3449. for (i = 0; i < nr_list_entries; i++) {
  3450. if (list_entries[i].class == class)
  3451. list_del_rcu(&list_entries[i].entry);
  3452. }
  3453. /*
  3454. * Unhash the class and remove it from the all_lock_classes list:
  3455. */
  3456. hlist_del_rcu(&class->hash_entry);
  3457. list_del_rcu(&class->lock_entry);
  3458. RCU_INIT_POINTER(class->key, NULL);
  3459. RCU_INIT_POINTER(class->name, NULL);
  3460. }
  3461. static inline int within(const void *addr, void *start, unsigned long size)
  3462. {
  3463. return addr >= start && addr < start + size;
  3464. }
  3465. /*
  3466. * Used in module.c to remove lock classes from memory that is going to be
  3467. * freed; and possibly re-used by other modules.
  3468. *
  3469. * We will have had one sync_sched() before getting here, so we're guaranteed
  3470. * nobody will look up these exact classes -- they're properly dead but still
  3471. * allocated.
  3472. */
  3473. void lockdep_free_key_range(void *start, unsigned long size)
  3474. {
  3475. struct lock_class *class;
  3476. struct hlist_head *head;
  3477. unsigned long flags;
  3478. int i;
  3479. int locked;
  3480. raw_local_irq_save(flags);
  3481. locked = graph_lock();
  3482. /*
  3483. * Unhash all classes that were created by this module:
  3484. */
  3485. for (i = 0; i < CLASSHASH_SIZE; i++) {
  3486. head = classhash_table + i;
  3487. hlist_for_each_entry_rcu(class, head, hash_entry) {
  3488. if (within(class->key, start, size))
  3489. zap_class(class);
  3490. else if (within(class->name, start, size))
  3491. zap_class(class);
  3492. }
  3493. }
  3494. if (locked)
  3495. graph_unlock();
  3496. raw_local_irq_restore(flags);
  3497. /*
  3498. * Wait for any possible iterators from look_up_lock_class() to pass
  3499. * before continuing to free the memory they refer to.
  3500. *
  3501. * sync_sched() is sufficient because the read-side is IRQ disable.
  3502. */
  3503. synchronize_sched();
  3504. /*
  3505. * XXX at this point we could return the resources to the pool;
  3506. * instead we leak them. We would need to change to bitmap allocators
  3507. * instead of the linear allocators we have now.
  3508. */
  3509. }
  3510. void lockdep_reset_lock(struct lockdep_map *lock)
  3511. {
  3512. struct lock_class *class;
  3513. struct hlist_head *head;
  3514. unsigned long flags;
  3515. int i, j;
  3516. int locked;
  3517. raw_local_irq_save(flags);
  3518. /*
  3519. * Remove all classes this lock might have:
  3520. */
  3521. for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
  3522. /*
  3523. * If the class exists we look it up and zap it:
  3524. */
  3525. class = look_up_lock_class(lock, j);
  3526. if (class)
  3527. zap_class(class);
  3528. }
  3529. /*
  3530. * Debug check: in the end all mapped classes should
  3531. * be gone.
  3532. */
  3533. locked = graph_lock();
  3534. for (i = 0; i < CLASSHASH_SIZE; i++) {
  3535. head = classhash_table + i;
  3536. hlist_for_each_entry_rcu(class, head, hash_entry) {
  3537. int match = 0;
  3538. for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
  3539. match |= class == lock->class_cache[j];
  3540. if (unlikely(match)) {
  3541. if (debug_locks_off_graph_unlock()) {
  3542. /*
  3543. * We all just reset everything, how did it match?
  3544. */
  3545. WARN_ON(1);
  3546. }
  3547. goto out_restore;
  3548. }
  3549. }
  3550. }
  3551. if (locked)
  3552. graph_unlock();
  3553. out_restore:
  3554. raw_local_irq_restore(flags);
  3555. }
  3556. void __init lockdep_info(void)
  3557. {
  3558. printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
  3559. printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
  3560. printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
  3561. printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
  3562. printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
  3563. printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
  3564. printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
  3565. printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
  3566. printk(" memory used by lock dependency info: %lu kB\n",
  3567. (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
  3568. sizeof(struct list_head) * CLASSHASH_SIZE +
  3569. sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
  3570. sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
  3571. sizeof(struct list_head) * CHAINHASH_SIZE
  3572. #ifdef CONFIG_PROVE_LOCKING
  3573. + sizeof(struct circular_queue)
  3574. #endif
  3575. ) / 1024
  3576. );
  3577. printk(" per task-struct memory footprint: %lu bytes\n",
  3578. sizeof(struct held_lock) * MAX_LOCK_DEPTH);
  3579. }
  3580. static void
  3581. print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
  3582. const void *mem_to, struct held_lock *hlock)
  3583. {
  3584. if (!debug_locks_off())
  3585. return;
  3586. if (debug_locks_silent)
  3587. return;
  3588. printk("\n");
  3589. printk("=========================\n");
  3590. printk("[ BUG: held lock freed! ]\n");
  3591. print_kernel_ident();
  3592. printk("-------------------------\n");
  3593. printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
  3594. curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
  3595. print_lock(hlock);
  3596. lockdep_print_held_locks(curr);
  3597. printk("\nstack backtrace:\n");
  3598. dump_stack();
  3599. }
  3600. static inline int not_in_range(const void* mem_from, unsigned long mem_len,
  3601. const void* lock_from, unsigned long lock_len)
  3602. {
  3603. return lock_from + lock_len <= mem_from ||
  3604. mem_from + mem_len <= lock_from;
  3605. }
  3606. /*
  3607. * Called when kernel memory is freed (or unmapped), or if a lock
  3608. * is destroyed or reinitialized - this code checks whether there is
  3609. * any held lock in the memory range of <from> to <to>:
  3610. */
  3611. void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
  3612. {
  3613. struct task_struct *curr = current;
  3614. struct held_lock *hlock;
  3615. unsigned long flags;
  3616. int i;
  3617. if (unlikely(!debug_locks))
  3618. return;
  3619. local_irq_save(flags);
  3620. for (i = 0; i < curr->lockdep_depth; i++) {
  3621. hlock = curr->held_locks + i;
  3622. if (not_in_range(mem_from, mem_len, hlock->instance,
  3623. sizeof(*hlock->instance)))
  3624. continue;
  3625. print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
  3626. break;
  3627. }
  3628. local_irq_restore(flags);
  3629. }
  3630. EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
  3631. static void print_held_locks_bug(void)
  3632. {
  3633. if (!debug_locks_off())
  3634. return;
  3635. if (debug_locks_silent)
  3636. return;
  3637. printk("\n");
  3638. printk("=====================================\n");
  3639. printk("[ BUG: %s/%d still has locks held! ]\n",
  3640. current->comm, task_pid_nr(current));
  3641. print_kernel_ident();
  3642. printk("-------------------------------------\n");
  3643. lockdep_print_held_locks(current);
  3644. printk("\nstack backtrace:\n");
  3645. dump_stack();
  3646. }
  3647. void debug_check_no_locks_held(void)
  3648. {
  3649. if (unlikely(current->lockdep_depth > 0))
  3650. print_held_locks_bug();
  3651. }
  3652. EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
  3653. #ifdef __KERNEL__
  3654. void debug_show_all_locks(void)
  3655. {
  3656. struct task_struct *g, *p;
  3657. int count = 10;
  3658. int unlock = 1;
  3659. if (unlikely(!debug_locks)) {
  3660. printk("INFO: lockdep is turned off.\n");
  3661. return;
  3662. }
  3663. printk("\nShowing all locks held in the system:\n");
  3664. /*
  3665. * Here we try to get the tasklist_lock as hard as possible,
  3666. * if not successful after 2 seconds we ignore it (but keep
  3667. * trying). This is to enable a debug printout even if a
  3668. * tasklist_lock-holding task deadlocks or crashes.
  3669. */
  3670. retry:
  3671. if (!read_trylock(&tasklist_lock)) {
  3672. if (count == 10)
  3673. printk("hm, tasklist_lock locked, retrying... ");
  3674. if (count) {
  3675. count--;
  3676. printk(" #%d", 10-count);
  3677. mdelay(200);
  3678. goto retry;
  3679. }
  3680. printk(" ignoring it.\n");
  3681. unlock = 0;
  3682. } else {
  3683. if (count != 10)
  3684. printk(KERN_CONT " locked it.\n");
  3685. }
  3686. do_each_thread(g, p) {
  3687. /*
  3688. * It's not reliable to print a task's held locks
  3689. * if it's not sleeping (or if it's not the current
  3690. * task):
  3691. */
  3692. if (p->state == TASK_RUNNING && p != current)
  3693. continue;
  3694. if (p->lockdep_depth)
  3695. lockdep_print_held_locks(p);
  3696. if (!unlock)
  3697. if (read_trylock(&tasklist_lock))
  3698. unlock = 1;
  3699. } while_each_thread(g, p);
  3700. printk("\n");
  3701. printk("=============================================\n\n");
  3702. if (unlock)
  3703. read_unlock(&tasklist_lock);
  3704. }
  3705. EXPORT_SYMBOL_GPL(debug_show_all_locks);
  3706. #endif
  3707. /*
  3708. * Careful: only use this function if you are sure that
  3709. * the task cannot run in parallel!
  3710. */
  3711. void debug_show_held_locks(struct task_struct *task)
  3712. {
  3713. if (unlikely(!debug_locks)) {
  3714. printk("INFO: lockdep is turned off.\n");
  3715. return;
  3716. }
  3717. lockdep_print_held_locks(task);
  3718. }
  3719. EXPORT_SYMBOL_GPL(debug_show_held_locks);
  3720. asmlinkage __visible void lockdep_sys_exit(void)
  3721. {
  3722. struct task_struct *curr = current;
  3723. if (unlikely(curr->lockdep_depth)) {
  3724. if (!debug_locks_off())
  3725. return;
  3726. printk("\n");
  3727. printk("================================================\n");
  3728. printk("[ BUG: lock held when returning to user space! ]\n");
  3729. print_kernel_ident();
  3730. printk("------------------------------------------------\n");
  3731. printk("%s/%d is leaving the kernel with locks still held!\n",
  3732. curr->comm, curr->pid);
  3733. lockdep_print_held_locks(curr);
  3734. }
  3735. }
  3736. void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
  3737. {
  3738. struct task_struct *curr = current;
  3739. #ifndef CONFIG_PROVE_RCU_REPEATEDLY
  3740. if (!debug_locks_off())
  3741. return;
  3742. #endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
  3743. /* Note: the following can be executed concurrently, so be careful. */
  3744. printk("\n");
  3745. printk("===============================\n");
  3746. printk("[ INFO: suspicious RCU usage. ]\n");
  3747. print_kernel_ident();
  3748. printk("-------------------------------\n");
  3749. printk("%s:%d %s!\n", file, line, s);
  3750. printk("\nother info that might help us debug this:\n\n");
  3751. printk("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
  3752. !rcu_lockdep_current_cpu_online()
  3753. ? "RCU used illegally from offline CPU!\n"
  3754. : !rcu_is_watching()
  3755. ? "RCU used illegally from idle CPU!\n"
  3756. : "",
  3757. rcu_scheduler_active, debug_locks);
  3758. /*
  3759. * If a CPU is in the RCU-free window in idle (ie: in the section
  3760. * between rcu_idle_enter() and rcu_idle_exit(), then RCU
  3761. * considers that CPU to be in an "extended quiescent state",
  3762. * which means that RCU will be completely ignoring that CPU.
  3763. * Therefore, rcu_read_lock() and friends have absolutely no
  3764. * effect on a CPU running in that state. In other words, even if
  3765. * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
  3766. * delete data structures out from under it. RCU really has no
  3767. * choice here: we need to keep an RCU-free window in idle where
  3768. * the CPU may possibly enter into low power mode. This way we can
  3769. * notice an extended quiescent state to other CPUs that started a grace
  3770. * period. Otherwise we would delay any grace period as long as we run
  3771. * in the idle task.
  3772. *
  3773. * So complain bitterly if someone does call rcu_read_lock(),
  3774. * rcu_read_lock_bh() and so on from extended quiescent states.
  3775. */
  3776. if (!rcu_is_watching())
  3777. printk("RCU used illegally from extended quiescent state!\n");
  3778. lockdep_print_held_locks(curr);
  3779. printk("\nstack backtrace:\n");
  3780. dump_stack();
  3781. }
  3782. EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);