lockdep.c 111 KB

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