lockdep.c 111 KB

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