lockdep.c 110 KB

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