lockdep.c 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564
  1. /*
  2. * kernel/lockdep.c
  3. *
  4. * Runtime locking correctness validator
  5. *
  6. * Started by Ingo Molnar:
  7. *
  8. * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  9. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
  10. *
  11. * this code maps all the lock dependencies as they occur in a live kernel
  12. * and will warn about the following classes of locking bugs:
  13. *
  14. * - lock inversion scenarios
  15. * - circular lock dependencies
  16. * - hardirq/softirq safe/unsafe locking bugs
  17. *
  18. * Bugs are reported even if the current locking scenario does not cause
  19. * any deadlock at this point.
  20. *
  21. * I.e. if anytime in the past two locks were taken in a different order,
  22. * even if it happened for another task, even if those were different
  23. * locks (but of the same class as this lock), this code will detect it.
  24. *
  25. * Thanks to Arjan van de Ven for coming up with the initial idea of
  26. * mapping lock dependencies runtime.
  27. */
  28. #define DISABLE_BRANCH_PROFILING
  29. #include <linux/mutex.h>
  30. #include <linux/sched.h>
  31. #include <linux/sched/clock.h>
  32. #include <linux/sched/task.h>
  33. #include <linux/sched/mm.h>
  34. #include <linux/delay.h>
  35. #include <linux/module.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/seq_file.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/kallsyms.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/stacktrace.h>
  42. #include <linux/debug_locks.h>
  43. #include <linux/irqflags.h>
  44. #include <linux/utsname.h>
  45. #include <linux/hash.h>
  46. #include <linux/ftrace.h>
  47. #include <linux/stringify.h>
  48. #include <linux/bitops.h>
  49. #include <linux/gfp.h>
  50. #include <linux/random.h>
  51. #include <linux/jhash.h>
  52. #include <linux/nmi.h>
  53. #include <asm/sections.h>
  54. #include "lockdep_internals.h"
  55. #define CREATE_TRACE_POINTS
  56. #include <trace/events/lock.h>
  57. #ifdef CONFIG_PROVE_LOCKING
  58. int prove_locking = 1;
  59. module_param(prove_locking, int, 0644);
  60. #else
  61. #define prove_locking 0
  62. #endif
  63. #ifdef CONFIG_LOCK_STAT
  64. int lock_stat = 1;
  65. module_param(lock_stat, int, 0644);
  66. #else
  67. #define lock_stat 0
  68. #endif
  69. /*
  70. * lockdep_lock: protects the lockdep graph, the hashes and the
  71. * class/list/hash allocators.
  72. *
  73. * This is one of the rare exceptions where it's justified
  74. * to use a raw spinlock - we really dont want the spinlock
  75. * code to recurse back into the lockdep code...
  76. */
  77. static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  78. static int graph_lock(void)
  79. {
  80. arch_spin_lock(&lockdep_lock);
  81. /*
  82. * Make sure that if another CPU detected a bug while
  83. * walking the graph we dont change it (while the other
  84. * CPU is busy printing out stuff with the graph lock
  85. * dropped already)
  86. */
  87. if (!debug_locks) {
  88. arch_spin_unlock(&lockdep_lock);
  89. return 0;
  90. }
  91. /* prevent any recursions within lockdep from causing deadlocks */
  92. current->lockdep_recursion++;
  93. return 1;
  94. }
  95. static inline int graph_unlock(void)
  96. {
  97. if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
  98. /*
  99. * The lockdep graph lock isn't locked while we expect it to
  100. * be, we're confused now, bye!
  101. */
  102. return DEBUG_LOCKS_WARN_ON(1);
  103. }
  104. current->lockdep_recursion--;
  105. arch_spin_unlock(&lockdep_lock);
  106. return 0;
  107. }
  108. /*
  109. * Turn lock debugging off and return with 0 if it was off already,
  110. * and also release the graph lock:
  111. */
  112. static inline int debug_locks_off_graph_unlock(void)
  113. {
  114. int ret = debug_locks_off();
  115. arch_spin_unlock(&lockdep_lock);
  116. return ret;
  117. }
  118. unsigned long nr_list_entries;
  119. static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
  120. /*
  121. * All data structures here are protected by the global debug_lock.
  122. *
  123. * Mutex key structs only get allocated, once during bootup, and never
  124. * get freed - this significantly simplifies the debugging code.
  125. */
  126. unsigned long nr_lock_classes;
  127. static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
  128. static inline struct lock_class *hlock_class(struct held_lock *hlock)
  129. {
  130. if (!hlock->class_idx) {
  131. /*
  132. * Someone passed in garbage, we give up.
  133. */
  134. DEBUG_LOCKS_WARN_ON(1);
  135. return NULL;
  136. }
  137. return lock_classes + hlock->class_idx - 1;
  138. }
  139. #ifdef CONFIG_LOCK_STAT
  140. static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
  141. static inline u64 lockstat_clock(void)
  142. {
  143. return local_clock();
  144. }
  145. static int lock_point(unsigned long points[], unsigned long ip)
  146. {
  147. int i;
  148. for (i = 0; i < LOCKSTAT_POINTS; i++) {
  149. if (points[i] == 0) {
  150. points[i] = ip;
  151. break;
  152. }
  153. if (points[i] == ip)
  154. break;
  155. }
  156. return i;
  157. }
  158. static void lock_time_inc(struct lock_time *lt, u64 time)
  159. {
  160. if (time > lt->max)
  161. lt->max = time;
  162. if (time < lt->min || !lt->nr)
  163. lt->min = time;
  164. lt->total += time;
  165. lt->nr++;
  166. }
  167. static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
  168. {
  169. if (!src->nr)
  170. return;
  171. if (src->max > dst->max)
  172. dst->max = src->max;
  173. if (src->min < dst->min || !dst->nr)
  174. dst->min = src->min;
  175. dst->total += src->total;
  176. dst->nr += src->nr;
  177. }
  178. struct lock_class_stats lock_stats(struct lock_class *class)
  179. {
  180. struct lock_class_stats stats;
  181. int cpu, i;
  182. memset(&stats, 0, sizeof(struct lock_class_stats));
  183. for_each_possible_cpu(cpu) {
  184. struct lock_class_stats *pcs =
  185. &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
  186. for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
  187. stats.contention_point[i] += pcs->contention_point[i];
  188. for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
  189. stats.contending_point[i] += pcs->contending_point[i];
  190. lock_time_add(&pcs->read_waittime, &stats.read_waittime);
  191. lock_time_add(&pcs->write_waittime, &stats.write_waittime);
  192. lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
  193. lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
  194. for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
  195. stats.bounces[i] += pcs->bounces[i];
  196. }
  197. return stats;
  198. }
  199. void clear_lock_stats(struct lock_class *class)
  200. {
  201. int cpu;
  202. for_each_possible_cpu(cpu) {
  203. struct lock_class_stats *cpu_stats =
  204. &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
  205. memset(cpu_stats, 0, sizeof(struct lock_class_stats));
  206. }
  207. memset(class->contention_point, 0, sizeof(class->contention_point));
  208. memset(class->contending_point, 0, sizeof(class->contending_point));
  209. }
  210. static struct lock_class_stats *get_lock_stats(struct lock_class *class)
  211. {
  212. return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
  213. }
  214. static void put_lock_stats(struct lock_class_stats *stats)
  215. {
  216. put_cpu_var(cpu_lock_stats);
  217. }
  218. static void lock_release_holdtime(struct held_lock *hlock)
  219. {
  220. struct lock_class_stats *stats;
  221. u64 holdtime;
  222. if (!lock_stat)
  223. return;
  224. holdtime = lockstat_clock() - hlock->holdtime_stamp;
  225. stats = get_lock_stats(hlock_class(hlock));
  226. if (hlock->read)
  227. lock_time_inc(&stats->read_holdtime, holdtime);
  228. else
  229. lock_time_inc(&stats->write_holdtime, holdtime);
  230. put_lock_stats(stats);
  231. }
  232. #else
  233. static inline void lock_release_holdtime(struct held_lock *hlock)
  234. {
  235. }
  236. #endif
  237. /*
  238. * We keep a global list of all lock classes. The list only grows,
  239. * never shrinks. The list is only accessed with the lockdep
  240. * spinlock lock held.
  241. */
  242. LIST_HEAD(all_lock_classes);
  243. /*
  244. * The lockdep classes are in a hash-table as well, for fast lookup:
  245. */
  246. #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
  247. #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
  248. #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
  249. #define classhashentry(key) (classhash_table + __classhashfn((key)))
  250. static struct hlist_head classhash_table[CLASSHASH_SIZE];
  251. /*
  252. * We put the lock dependency chains into a hash-table as well, to cache
  253. * their existence:
  254. */
  255. #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
  256. #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
  257. #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
  258. #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
  259. static struct hlist_head chainhash_table[CHAINHASH_SIZE];
  260. /*
  261. * The hash key of the lock dependency chains is a hash itself too:
  262. * it's a hash of all locks taken up to that lock, including that lock.
  263. * It's a 64-bit hash, because it's important for the keys to be
  264. * unique.
  265. */
  266. static inline u64 iterate_chain_key(u64 key, u32 idx)
  267. {
  268. u32 k0 = key, k1 = key >> 32;
  269. __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
  270. return k0 | (u64)k1 << 32;
  271. }
  272. void lockdep_off(void)
  273. {
  274. current->lockdep_recursion++;
  275. }
  276. EXPORT_SYMBOL(lockdep_off);
  277. void lockdep_on(void)
  278. {
  279. current->lockdep_recursion--;
  280. }
  281. EXPORT_SYMBOL(lockdep_on);
  282. /*
  283. * Debugging switches:
  284. */
  285. #define VERBOSE 0
  286. #define VERY_VERBOSE 0
  287. #if VERBOSE
  288. # define HARDIRQ_VERBOSE 1
  289. # define SOFTIRQ_VERBOSE 1
  290. #else
  291. # define HARDIRQ_VERBOSE 0
  292. # define SOFTIRQ_VERBOSE 0
  293. #endif
  294. #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
  295. /*
  296. * Quick filtering for interesting events:
  297. */
  298. static int class_filter(struct lock_class *class)
  299. {
  300. #if 0
  301. /* Example */
  302. if (class->name_version == 1 &&
  303. !strcmp(class->name, "lockname"))
  304. return 1;
  305. if (class->name_version == 1 &&
  306. !strcmp(class->name, "&struct->lockfield"))
  307. return 1;
  308. #endif
  309. /* Filter everything else. 1 would be to allow everything else */
  310. return 0;
  311. }
  312. #endif
  313. static int verbose(struct lock_class *class)
  314. {
  315. #if VERBOSE
  316. return class_filter(class);
  317. #endif
  318. return 0;
  319. }
  320. /*
  321. * Stack-trace: tightly packed array of stack backtrace
  322. * addresses. Protected by the graph_lock.
  323. */
  324. unsigned long nr_stack_trace_entries;
  325. static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
  326. static void print_lockdep_off(const char *bug_msg)
  327. {
  328. printk(KERN_DEBUG "%s\n", bug_msg);
  329. printk(KERN_DEBUG "turning off the locking correctness validator.\n");
  330. #ifdef CONFIG_LOCK_STAT
  331. printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
  332. #endif
  333. }
  334. static int save_trace(struct stack_trace *trace)
  335. {
  336. trace->nr_entries = 0;
  337. trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
  338. trace->entries = stack_trace + nr_stack_trace_entries;
  339. trace->skip = 3;
  340. save_stack_trace(trace);
  341. /*
  342. * Some daft arches put -1 at the end to indicate its a full trace.
  343. *
  344. * <rant> this is buggy anyway, since it takes a whole extra entry so a
  345. * complete trace that maxes out the entries provided will be reported
  346. * as incomplete, friggin useless </rant>
  347. */
  348. if (trace->nr_entries != 0 &&
  349. trace->entries[trace->nr_entries-1] == ULONG_MAX)
  350. trace->nr_entries--;
  351. trace->max_entries = trace->nr_entries;
  352. nr_stack_trace_entries += trace->nr_entries;
  353. if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
  354. if (!debug_locks_off_graph_unlock())
  355. return 0;
  356. print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
  357. dump_stack();
  358. return 0;
  359. }
  360. return 1;
  361. }
  362. unsigned int nr_hardirq_chains;
  363. unsigned int nr_softirq_chains;
  364. unsigned int nr_process_chains;
  365. unsigned int max_lockdep_depth;
  366. #ifdef CONFIG_DEBUG_LOCKDEP
  367. /*
  368. * Various lockdep statistics:
  369. */
  370. DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
  371. #endif
  372. /*
  373. * Locking printouts:
  374. */
  375. #define __USAGE(__STATE) \
  376. [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
  377. [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
  378. [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
  379. [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
  380. static const char *usage_str[] =
  381. {
  382. #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
  383. #include "lockdep_states.h"
  384. #undef LOCKDEP_STATE
  385. [LOCK_USED] = "INITIAL USE",
  386. };
  387. const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
  388. {
  389. return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
  390. }
  391. static inline unsigned long lock_flag(enum lock_usage_bit bit)
  392. {
  393. return 1UL << bit;
  394. }
  395. static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
  396. {
  397. char c = '.';
  398. if (class->usage_mask & lock_flag(bit + 2))
  399. c = '+';
  400. if (class->usage_mask & lock_flag(bit)) {
  401. c = '-';
  402. if (class->usage_mask & lock_flag(bit + 2))
  403. c = '?';
  404. }
  405. return c;
  406. }
  407. void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
  408. {
  409. int i = 0;
  410. #define LOCKDEP_STATE(__STATE) \
  411. usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
  412. usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
  413. #include "lockdep_states.h"
  414. #undef LOCKDEP_STATE
  415. usage[i] = '\0';
  416. }
  417. static void __print_lock_name(struct lock_class *class)
  418. {
  419. char str[KSYM_NAME_LEN];
  420. const char *name;
  421. name = class->name;
  422. if (!name) {
  423. name = __get_key_name(class->key, str);
  424. printk(KERN_CONT "%s", name);
  425. } else {
  426. printk(KERN_CONT "%s", name);
  427. if (class->name_version > 1)
  428. printk(KERN_CONT "#%d", class->name_version);
  429. if (class->subclass)
  430. printk(KERN_CONT "/%d", class->subclass);
  431. }
  432. }
  433. static void print_lock_name(struct lock_class *class)
  434. {
  435. char usage[LOCK_USAGE_CHARS];
  436. get_usage_chars(class, usage);
  437. printk(KERN_CONT " (");
  438. __print_lock_name(class);
  439. printk(KERN_CONT "){%s}", usage);
  440. }
  441. static void print_lockdep_cache(struct lockdep_map *lock)
  442. {
  443. const char *name;
  444. char str[KSYM_NAME_LEN];
  445. name = lock->name;
  446. if (!name)
  447. name = __get_key_name(lock->key->subkeys, str);
  448. printk(KERN_CONT "%s", name);
  449. }
  450. static void print_lock(struct held_lock *hlock)
  451. {
  452. /*
  453. * We can be called locklessly through debug_show_all_locks() so be
  454. * extra careful, the hlock might have been released and cleared.
  455. */
  456. unsigned int class_idx = hlock->class_idx;
  457. /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
  458. barrier();
  459. if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
  460. printk(KERN_CONT "<RELEASED>\n");
  461. return;
  462. }
  463. printk(KERN_CONT "%p", hlock->instance);
  464. print_lock_name(lock_classes + class_idx - 1);
  465. printk(KERN_CONT ", at: %pS\n", (void *)hlock->acquire_ip);
  466. }
  467. static void lockdep_print_held_locks(struct task_struct *p)
  468. {
  469. int i, depth = READ_ONCE(p->lockdep_depth);
  470. if (!depth)
  471. printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p));
  472. else
  473. printk("%d lock%s held by %s/%d:\n", depth,
  474. depth > 1 ? "s" : "", p->comm, task_pid_nr(p));
  475. /*
  476. * It's not reliable to print a task's held locks if it's not sleeping
  477. * and it's not the current task.
  478. */
  479. if (p->state == TASK_RUNNING && p != current)
  480. return;
  481. for (i = 0; i < depth; i++) {
  482. printk(" #%d: ", i);
  483. print_lock(p->held_locks + i);
  484. }
  485. }
  486. static void print_kernel_ident(void)
  487. {
  488. printk("%s %.*s %s\n", init_utsname()->release,
  489. (int)strcspn(init_utsname()->version, " "),
  490. init_utsname()->version,
  491. print_tainted());
  492. }
  493. static int very_verbose(struct lock_class *class)
  494. {
  495. #if VERY_VERBOSE
  496. return class_filter(class);
  497. #endif
  498. return 0;
  499. }
  500. /*
  501. * Is this the address of a static object:
  502. */
  503. #ifdef __KERNEL__
  504. static int static_obj(void *obj)
  505. {
  506. unsigned long start = (unsigned long) &_stext,
  507. end = (unsigned long) &_end,
  508. addr = (unsigned long) obj;
  509. /*
  510. * static variable?
  511. */
  512. if ((addr >= start) && (addr < end))
  513. return 1;
  514. if (arch_is_kernel_data(addr))
  515. return 1;
  516. /*
  517. * in-kernel percpu var?
  518. */
  519. if (is_kernel_percpu_address(addr))
  520. return 1;
  521. /*
  522. * module static or percpu var?
  523. */
  524. return is_module_address(addr) || is_module_percpu_address(addr);
  525. }
  526. #endif
  527. /*
  528. * To make lock name printouts unique, we calculate a unique
  529. * class->name_version generation counter:
  530. */
  531. static int count_matching_names(struct lock_class *new_class)
  532. {
  533. struct lock_class *class;
  534. int count = 0;
  535. if (!new_class->name)
  536. return 0;
  537. list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
  538. if (new_class->key - new_class->subclass == class->key)
  539. return class->name_version;
  540. if (class->name && !strcmp(class->name, new_class->name))
  541. count = max(count, class->name_version);
  542. }
  543. return count + 1;
  544. }
  545. static inline struct lock_class *
  546. look_up_lock_class(const struct lockdep_map *lock, unsigned int subclass)
  547. {
  548. struct lockdep_subclass_key *key;
  549. struct hlist_head *hash_head;
  550. struct lock_class *class;
  551. if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
  552. debug_locks_off();
  553. printk(KERN_ERR
  554. "BUG: looking up invalid subclass: %u\n", subclass);
  555. printk(KERN_ERR
  556. "turning off the locking correctness validator.\n");
  557. dump_stack();
  558. return NULL;
  559. }
  560. /*
  561. * If it is not initialised then it has never been locked,
  562. * so it won't be present in the hash table.
  563. */
  564. if (unlikely(!lock->key))
  565. return NULL;
  566. /*
  567. * NOTE: the class-key must be unique. For dynamic locks, a static
  568. * lock_class_key variable is passed in through the mutex_init()
  569. * (or spin_lock_init()) call - which acts as the key. For static
  570. * locks we use the lock object itself as the key.
  571. */
  572. BUILD_BUG_ON(sizeof(struct lock_class_key) >
  573. sizeof(struct lockdep_map));
  574. key = lock->key->subkeys + subclass;
  575. hash_head = classhashentry(key);
  576. /*
  577. * We do an RCU walk of the hash, see lockdep_free_key_range().
  578. */
  579. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  580. return NULL;
  581. hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
  582. if (class->key == key) {
  583. /*
  584. * Huh! same key, different name? Did someone trample
  585. * on some memory? We're most confused.
  586. */
  587. WARN_ON_ONCE(class->name != lock->name);
  588. return class;
  589. }
  590. }
  591. return NULL;
  592. }
  593. /*
  594. * Static locks do not have their class-keys yet - for them the key is
  595. * the lock object itself. If the lock is in the per cpu area, the
  596. * canonical address of the lock (per cpu offset removed) is used.
  597. */
  598. static bool assign_lock_key(struct lockdep_map *lock)
  599. {
  600. unsigned long can_addr, addr = (unsigned long)lock;
  601. if (__is_kernel_percpu_address(addr, &can_addr))
  602. lock->key = (void *)can_addr;
  603. else if (__is_module_percpu_address(addr, &can_addr))
  604. lock->key = (void *)can_addr;
  605. else if (static_obj(lock))
  606. lock->key = (void *)lock;
  607. else {
  608. /* Debug-check: all keys must be persistent! */
  609. debug_locks_off();
  610. pr_err("INFO: trying to register non-static key.\n");
  611. pr_err("the code is fine but needs lockdep annotation.\n");
  612. pr_err("turning off the locking correctness validator.\n");
  613. dump_stack();
  614. return false;
  615. }
  616. return true;
  617. }
  618. /*
  619. * Register a lock's class in the hash-table, if the class is not present
  620. * yet. Otherwise we look it up. We cache the result in the lock object
  621. * itself, so actual lookup of the hash should be once per lock object.
  622. */
  623. static struct lock_class *
  624. register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
  625. {
  626. struct lockdep_subclass_key *key;
  627. struct hlist_head *hash_head;
  628. struct lock_class *class;
  629. DEBUG_LOCKS_WARN_ON(!irqs_disabled());
  630. class = look_up_lock_class(lock, subclass);
  631. if (likely(class))
  632. goto out_set_class_cache;
  633. if (!lock->key) {
  634. if (!assign_lock_key(lock))
  635. return NULL;
  636. } else if (!static_obj(lock->key)) {
  637. return NULL;
  638. }
  639. key = lock->key->subkeys + subclass;
  640. hash_head = classhashentry(key);
  641. if (!graph_lock()) {
  642. return NULL;
  643. }
  644. /*
  645. * We have to do the hash-walk again, to avoid races
  646. * with another CPU:
  647. */
  648. hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
  649. if (class->key == key)
  650. goto out_unlock_set;
  651. }
  652. /*
  653. * Allocate a new key from the static array, and add it to
  654. * the hash:
  655. */
  656. if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
  657. if (!debug_locks_off_graph_unlock()) {
  658. return NULL;
  659. }
  660. print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
  661. dump_stack();
  662. return NULL;
  663. }
  664. class = lock_classes + nr_lock_classes++;
  665. debug_atomic_inc(nr_unused_locks);
  666. class->key = key;
  667. class->name = lock->name;
  668. class->subclass = subclass;
  669. INIT_LIST_HEAD(&class->lock_entry);
  670. INIT_LIST_HEAD(&class->locks_before);
  671. INIT_LIST_HEAD(&class->locks_after);
  672. class->name_version = count_matching_names(class);
  673. /*
  674. * We use RCU's safe list-add method to make
  675. * parallel walking of the hash-list safe:
  676. */
  677. hlist_add_head_rcu(&class->hash_entry, hash_head);
  678. /*
  679. * Add it to the global list of classes:
  680. */
  681. list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
  682. if (verbose(class)) {
  683. graph_unlock();
  684. printk("\nnew class %px: %s", class->key, class->name);
  685. if (class->name_version > 1)
  686. printk(KERN_CONT "#%d", class->name_version);
  687. printk(KERN_CONT "\n");
  688. dump_stack();
  689. if (!graph_lock()) {
  690. return NULL;
  691. }
  692. }
  693. out_unlock_set:
  694. graph_unlock();
  695. out_set_class_cache:
  696. if (!subclass || force)
  697. lock->class_cache[0] = class;
  698. else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
  699. lock->class_cache[subclass] = class;
  700. /*
  701. * Hash collision, did we smoke some? We found a class with a matching
  702. * hash but the subclass -- which is hashed in -- didn't match.
  703. */
  704. if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
  705. return NULL;
  706. return class;
  707. }
  708. #ifdef CONFIG_PROVE_LOCKING
  709. /*
  710. * Allocate a lockdep entry. (assumes the graph_lock held, returns
  711. * with NULL on failure)
  712. */
  713. static struct lock_list *alloc_list_entry(void)
  714. {
  715. if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
  716. if (!debug_locks_off_graph_unlock())
  717. return NULL;
  718. print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
  719. dump_stack();
  720. return NULL;
  721. }
  722. return list_entries + nr_list_entries++;
  723. }
  724. /*
  725. * Add a new dependency to the head of the list:
  726. */
  727. static int add_lock_to_list(struct lock_class *this, struct list_head *head,
  728. unsigned long ip, int distance,
  729. struct stack_trace *trace)
  730. {
  731. struct lock_list *entry;
  732. /*
  733. * Lock not present yet - get a new dependency struct and
  734. * add it to the list:
  735. */
  736. entry = alloc_list_entry();
  737. if (!entry)
  738. return 0;
  739. entry->class = this;
  740. entry->distance = distance;
  741. entry->trace = *trace;
  742. /*
  743. * Both allocation and removal are done under the graph lock; but
  744. * iteration is under RCU-sched; see look_up_lock_class() and
  745. * lockdep_free_key_range().
  746. */
  747. list_add_tail_rcu(&entry->entry, head);
  748. return 1;
  749. }
  750. /*
  751. * For good efficiency of modular, we use power of 2
  752. */
  753. #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
  754. #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
  755. /*
  756. * The circular_queue and helpers is used to implement the
  757. * breadth-first search(BFS)algorithem, by which we can build
  758. * the shortest path from the next lock to be acquired to the
  759. * previous held lock if there is a circular between them.
  760. */
  761. struct circular_queue {
  762. unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
  763. unsigned int front, rear;
  764. };
  765. static struct circular_queue lock_cq;
  766. unsigned int max_bfs_queue_depth;
  767. static unsigned int lockdep_dependency_gen_id;
  768. static inline void __cq_init(struct circular_queue *cq)
  769. {
  770. cq->front = cq->rear = 0;
  771. lockdep_dependency_gen_id++;
  772. }
  773. static inline int __cq_empty(struct circular_queue *cq)
  774. {
  775. return (cq->front == cq->rear);
  776. }
  777. static inline int __cq_full(struct circular_queue *cq)
  778. {
  779. return ((cq->rear + 1) & CQ_MASK) == cq->front;
  780. }
  781. static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
  782. {
  783. if (__cq_full(cq))
  784. return -1;
  785. cq->element[cq->rear] = elem;
  786. cq->rear = (cq->rear + 1) & CQ_MASK;
  787. return 0;
  788. }
  789. static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
  790. {
  791. if (__cq_empty(cq))
  792. return -1;
  793. *elem = cq->element[cq->front];
  794. cq->front = (cq->front + 1) & CQ_MASK;
  795. return 0;
  796. }
  797. static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
  798. {
  799. return (cq->rear - cq->front) & CQ_MASK;
  800. }
  801. static inline void mark_lock_accessed(struct lock_list *lock,
  802. struct lock_list *parent)
  803. {
  804. unsigned long nr;
  805. nr = lock - list_entries;
  806. WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
  807. lock->parent = parent;
  808. lock->class->dep_gen_id = lockdep_dependency_gen_id;
  809. }
  810. static inline unsigned long lock_accessed(struct lock_list *lock)
  811. {
  812. unsigned long nr;
  813. nr = lock - list_entries;
  814. WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
  815. return lock->class->dep_gen_id == lockdep_dependency_gen_id;
  816. }
  817. static inline struct lock_list *get_lock_parent(struct lock_list *child)
  818. {
  819. return child->parent;
  820. }
  821. static inline int get_lock_depth(struct lock_list *child)
  822. {
  823. int depth = 0;
  824. struct lock_list *parent;
  825. while ((parent = get_lock_parent(child))) {
  826. child = parent;
  827. depth++;
  828. }
  829. return depth;
  830. }
  831. static int __bfs(struct lock_list *source_entry,
  832. void *data,
  833. int (*match)(struct lock_list *entry, void *data),
  834. struct lock_list **target_entry,
  835. int forward)
  836. {
  837. struct lock_list *entry;
  838. struct list_head *head;
  839. struct circular_queue *cq = &lock_cq;
  840. int ret = 1;
  841. if (match(source_entry, data)) {
  842. *target_entry = source_entry;
  843. ret = 0;
  844. goto exit;
  845. }
  846. if (forward)
  847. head = &source_entry->class->locks_after;
  848. else
  849. head = &source_entry->class->locks_before;
  850. if (list_empty(head))
  851. goto exit;
  852. __cq_init(cq);
  853. __cq_enqueue(cq, (unsigned long)source_entry);
  854. while (!__cq_empty(cq)) {
  855. struct lock_list *lock;
  856. __cq_dequeue(cq, (unsigned long *)&lock);
  857. if (!lock->class) {
  858. ret = -2;
  859. goto exit;
  860. }
  861. if (forward)
  862. head = &lock->class->locks_after;
  863. else
  864. head = &lock->class->locks_before;
  865. DEBUG_LOCKS_WARN_ON(!irqs_disabled());
  866. list_for_each_entry_rcu(entry, head, entry) {
  867. if (!lock_accessed(entry)) {
  868. unsigned int cq_depth;
  869. mark_lock_accessed(entry, lock);
  870. if (match(entry, data)) {
  871. *target_entry = entry;
  872. ret = 0;
  873. goto exit;
  874. }
  875. if (__cq_enqueue(cq, (unsigned long)entry)) {
  876. ret = -1;
  877. goto exit;
  878. }
  879. cq_depth = __cq_get_elem_count(cq);
  880. if (max_bfs_queue_depth < cq_depth)
  881. max_bfs_queue_depth = cq_depth;
  882. }
  883. }
  884. }
  885. exit:
  886. return ret;
  887. }
  888. static inline int __bfs_forwards(struct lock_list *src_entry,
  889. void *data,
  890. int (*match)(struct lock_list *entry, void *data),
  891. struct lock_list **target_entry)
  892. {
  893. return __bfs(src_entry, data, match, target_entry, 1);
  894. }
  895. static inline int __bfs_backwards(struct lock_list *src_entry,
  896. void *data,
  897. int (*match)(struct lock_list *entry, void *data),
  898. struct lock_list **target_entry)
  899. {
  900. return __bfs(src_entry, data, match, target_entry, 0);
  901. }
  902. /*
  903. * Recursive, forwards-direction lock-dependency checking, used for
  904. * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
  905. * checking.
  906. */
  907. /*
  908. * Print a dependency chain entry (this is only done when a deadlock
  909. * has been detected):
  910. */
  911. static noinline int
  912. print_circular_bug_entry(struct lock_list *target, int depth)
  913. {
  914. if (debug_locks_silent)
  915. return 0;
  916. printk("\n-> #%u", depth);
  917. print_lock_name(target->class);
  918. printk(KERN_CONT ":\n");
  919. print_stack_trace(&target->trace, 6);
  920. return 0;
  921. }
  922. static void
  923. print_circular_lock_scenario(struct held_lock *src,
  924. struct held_lock *tgt,
  925. struct lock_list *prt)
  926. {
  927. struct lock_class *source = hlock_class(src);
  928. struct lock_class *target = hlock_class(tgt);
  929. struct lock_class *parent = prt->class;
  930. /*
  931. * A direct locking problem where unsafe_class lock is taken
  932. * directly by safe_class lock, then all we need to show
  933. * is the deadlock scenario, as it is obvious that the
  934. * unsafe lock is taken under the safe lock.
  935. *
  936. * But if there is a chain instead, where the safe lock takes
  937. * an intermediate lock (middle_class) where this lock is
  938. * not the same as the safe lock, then the lock chain is
  939. * used to describe the problem. Otherwise we would need
  940. * to show a different CPU case for each link in the chain
  941. * from the safe_class lock to the unsafe_class lock.
  942. */
  943. if (parent != source) {
  944. printk("Chain exists of:\n ");
  945. __print_lock_name(source);
  946. printk(KERN_CONT " --> ");
  947. __print_lock_name(parent);
  948. printk(KERN_CONT " --> ");
  949. __print_lock_name(target);
  950. printk(KERN_CONT "\n\n");
  951. }
  952. printk(" Possible unsafe locking scenario:\n\n");
  953. printk(" CPU0 CPU1\n");
  954. printk(" ---- ----\n");
  955. printk(" lock(");
  956. __print_lock_name(target);
  957. printk(KERN_CONT ");\n");
  958. printk(" lock(");
  959. __print_lock_name(parent);
  960. printk(KERN_CONT ");\n");
  961. printk(" lock(");
  962. __print_lock_name(target);
  963. printk(KERN_CONT ");\n");
  964. printk(" lock(");
  965. __print_lock_name(source);
  966. printk(KERN_CONT ");\n");
  967. printk("\n *** DEADLOCK ***\n\n");
  968. }
  969. /*
  970. * When a circular dependency is detected, print the
  971. * header first:
  972. */
  973. static noinline int
  974. print_circular_bug_header(struct lock_list *entry, unsigned int depth,
  975. struct held_lock *check_src,
  976. struct held_lock *check_tgt)
  977. {
  978. struct task_struct *curr = current;
  979. if (debug_locks_silent)
  980. return 0;
  981. pr_warn("\n");
  982. pr_warn("======================================================\n");
  983. pr_warn("WARNING: possible circular locking dependency detected\n");
  984. print_kernel_ident();
  985. pr_warn("------------------------------------------------------\n");
  986. pr_warn("%s/%d is trying to acquire lock:\n",
  987. curr->comm, task_pid_nr(curr));
  988. print_lock(check_src);
  989. pr_warn("\nbut task is already holding lock:\n");
  990. print_lock(check_tgt);
  991. pr_warn("\nwhich lock already depends on the new lock.\n\n");
  992. pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
  993. print_circular_bug_entry(entry, depth);
  994. return 0;
  995. }
  996. static inline int class_equal(struct lock_list *entry, void *data)
  997. {
  998. return entry->class == data;
  999. }
  1000. static noinline int print_circular_bug(struct lock_list *this,
  1001. struct lock_list *target,
  1002. struct held_lock *check_src,
  1003. struct held_lock *check_tgt,
  1004. struct stack_trace *trace)
  1005. {
  1006. struct task_struct *curr = current;
  1007. struct lock_list *parent;
  1008. struct lock_list *first_parent;
  1009. int depth;
  1010. if (!debug_locks_off_graph_unlock() || debug_locks_silent)
  1011. return 0;
  1012. if (!save_trace(&this->trace))
  1013. return 0;
  1014. depth = get_lock_depth(target);
  1015. print_circular_bug_header(target, depth, check_src, check_tgt);
  1016. parent = get_lock_parent(target);
  1017. first_parent = parent;
  1018. while (parent) {
  1019. print_circular_bug_entry(parent, --depth);
  1020. parent = get_lock_parent(parent);
  1021. }
  1022. printk("\nother info that might help us debug this:\n\n");
  1023. print_circular_lock_scenario(check_src, check_tgt,
  1024. first_parent);
  1025. lockdep_print_held_locks(curr);
  1026. printk("\nstack backtrace:\n");
  1027. dump_stack();
  1028. return 0;
  1029. }
  1030. static noinline int print_bfs_bug(int ret)
  1031. {
  1032. if (!debug_locks_off_graph_unlock())
  1033. return 0;
  1034. /*
  1035. * Breadth-first-search failed, graph got corrupted?
  1036. */
  1037. WARN(1, "lockdep bfs error:%d\n", ret);
  1038. return 0;
  1039. }
  1040. static int noop_count(struct lock_list *entry, void *data)
  1041. {
  1042. (*(unsigned long *)data)++;
  1043. return 0;
  1044. }
  1045. static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
  1046. {
  1047. unsigned long count = 0;
  1048. struct lock_list *uninitialized_var(target_entry);
  1049. __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
  1050. return count;
  1051. }
  1052. unsigned long lockdep_count_forward_deps(struct lock_class *class)
  1053. {
  1054. unsigned long ret, flags;
  1055. struct lock_list this;
  1056. this.parent = NULL;
  1057. this.class = class;
  1058. local_irq_save(flags);
  1059. arch_spin_lock(&lockdep_lock);
  1060. ret = __lockdep_count_forward_deps(&this);
  1061. arch_spin_unlock(&lockdep_lock);
  1062. local_irq_restore(flags);
  1063. return ret;
  1064. }
  1065. static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
  1066. {
  1067. unsigned long count = 0;
  1068. struct lock_list *uninitialized_var(target_entry);
  1069. __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
  1070. return count;
  1071. }
  1072. unsigned long lockdep_count_backward_deps(struct lock_class *class)
  1073. {
  1074. unsigned long ret, flags;
  1075. struct lock_list this;
  1076. this.parent = NULL;
  1077. this.class = class;
  1078. local_irq_save(flags);
  1079. arch_spin_lock(&lockdep_lock);
  1080. ret = __lockdep_count_backward_deps(&this);
  1081. arch_spin_unlock(&lockdep_lock);
  1082. 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. __visible void trace_hardirqs_on_caller(unsigned long ip)
  2408. {
  2409. time_hardirqs_on(CALLER_ADDR0, ip);
  2410. if (unlikely(!debug_locks || current->lockdep_recursion))
  2411. return;
  2412. if (unlikely(current->hardirqs_enabled)) {
  2413. /*
  2414. * Neither irq nor preemption are disabled here
  2415. * so this is racy by nature but losing one hit
  2416. * in a stat is not a big deal.
  2417. */
  2418. __debug_atomic_inc(redundant_hardirqs_on);
  2419. return;
  2420. }
  2421. /*
  2422. * We're enabling irqs and according to our state above irqs weren't
  2423. * already enabled, yet we find the hardware thinks they are in fact
  2424. * enabled.. someone messed up their IRQ state tracing.
  2425. */
  2426. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2427. return;
  2428. /*
  2429. * See the fine text that goes along with this variable definition.
  2430. */
  2431. if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
  2432. return;
  2433. /*
  2434. * Can't allow enabling interrupts while in an interrupt handler,
  2435. * that's general bad form and such. Recursion, limited stack etc..
  2436. */
  2437. if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
  2438. return;
  2439. current->lockdep_recursion = 1;
  2440. __trace_hardirqs_on_caller(ip);
  2441. current->lockdep_recursion = 0;
  2442. }
  2443. EXPORT_SYMBOL(trace_hardirqs_on_caller);
  2444. void trace_hardirqs_on(void)
  2445. {
  2446. trace_hardirqs_on_caller(CALLER_ADDR0);
  2447. }
  2448. EXPORT_SYMBOL(trace_hardirqs_on);
  2449. /*
  2450. * Hardirqs were disabled:
  2451. */
  2452. __visible void trace_hardirqs_off_caller(unsigned long ip)
  2453. {
  2454. struct task_struct *curr = current;
  2455. time_hardirqs_off(CALLER_ADDR0, ip);
  2456. if (unlikely(!debug_locks || current->lockdep_recursion))
  2457. return;
  2458. /*
  2459. * So we're supposed to get called after you mask local IRQs, but for
  2460. * some reason the hardware doesn't quite think you did a proper job.
  2461. */
  2462. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2463. return;
  2464. if (curr->hardirqs_enabled) {
  2465. /*
  2466. * We have done an ON -> OFF transition:
  2467. */
  2468. curr->hardirqs_enabled = 0;
  2469. curr->hardirq_disable_ip = ip;
  2470. curr->hardirq_disable_event = ++curr->irq_events;
  2471. debug_atomic_inc(hardirqs_off_events);
  2472. } else
  2473. debug_atomic_inc(redundant_hardirqs_off);
  2474. }
  2475. EXPORT_SYMBOL(trace_hardirqs_off_caller);
  2476. void trace_hardirqs_off(void)
  2477. {
  2478. trace_hardirqs_off_caller(CALLER_ADDR0);
  2479. }
  2480. EXPORT_SYMBOL(trace_hardirqs_off);
  2481. /*
  2482. * Softirqs will be enabled:
  2483. */
  2484. void trace_softirqs_on(unsigned long ip)
  2485. {
  2486. struct task_struct *curr = current;
  2487. if (unlikely(!debug_locks || current->lockdep_recursion))
  2488. return;
  2489. /*
  2490. * We fancy IRQs being disabled here, see softirq.c, avoids
  2491. * funny state and nesting things.
  2492. */
  2493. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2494. return;
  2495. if (curr->softirqs_enabled) {
  2496. debug_atomic_inc(redundant_softirqs_on);
  2497. return;
  2498. }
  2499. current->lockdep_recursion = 1;
  2500. /*
  2501. * We'll do an OFF -> ON transition:
  2502. */
  2503. curr->softirqs_enabled = 1;
  2504. curr->softirq_enable_ip = ip;
  2505. curr->softirq_enable_event = ++curr->irq_events;
  2506. debug_atomic_inc(softirqs_on_events);
  2507. /*
  2508. * We are going to turn softirqs on, so set the
  2509. * usage bit for all held locks, if hardirqs are
  2510. * enabled too:
  2511. */
  2512. if (curr->hardirqs_enabled)
  2513. mark_held_locks(curr, SOFTIRQ);
  2514. current->lockdep_recursion = 0;
  2515. }
  2516. /*
  2517. * Softirqs were disabled:
  2518. */
  2519. void trace_softirqs_off(unsigned long ip)
  2520. {
  2521. struct task_struct *curr = current;
  2522. if (unlikely(!debug_locks || current->lockdep_recursion))
  2523. return;
  2524. /*
  2525. * We fancy IRQs being disabled here, see softirq.c
  2526. */
  2527. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2528. return;
  2529. if (curr->softirqs_enabled) {
  2530. /*
  2531. * We have done an ON -> OFF transition:
  2532. */
  2533. curr->softirqs_enabled = 0;
  2534. curr->softirq_disable_ip = ip;
  2535. curr->softirq_disable_event = ++curr->irq_events;
  2536. debug_atomic_inc(softirqs_off_events);
  2537. /*
  2538. * Whoops, we wanted softirqs off, so why aren't they?
  2539. */
  2540. DEBUG_LOCKS_WARN_ON(!softirq_count());
  2541. } else
  2542. debug_atomic_inc(redundant_softirqs_off);
  2543. }
  2544. static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
  2545. {
  2546. /*
  2547. * If non-trylock use in a hardirq or softirq context, then
  2548. * mark the lock as used in these contexts:
  2549. */
  2550. if (!hlock->trylock) {
  2551. if (hlock->read) {
  2552. if (curr->hardirq_context)
  2553. if (!mark_lock(curr, hlock,
  2554. LOCK_USED_IN_HARDIRQ_READ))
  2555. return 0;
  2556. if (curr->softirq_context)
  2557. if (!mark_lock(curr, hlock,
  2558. LOCK_USED_IN_SOFTIRQ_READ))
  2559. return 0;
  2560. } else {
  2561. if (curr->hardirq_context)
  2562. if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
  2563. return 0;
  2564. if (curr->softirq_context)
  2565. if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
  2566. return 0;
  2567. }
  2568. }
  2569. if (!hlock->hardirqs_off) {
  2570. if (hlock->read) {
  2571. if (!mark_lock(curr, hlock,
  2572. LOCK_ENABLED_HARDIRQ_READ))
  2573. return 0;
  2574. if (curr->softirqs_enabled)
  2575. if (!mark_lock(curr, hlock,
  2576. LOCK_ENABLED_SOFTIRQ_READ))
  2577. return 0;
  2578. } else {
  2579. if (!mark_lock(curr, hlock,
  2580. LOCK_ENABLED_HARDIRQ))
  2581. return 0;
  2582. if (curr->softirqs_enabled)
  2583. if (!mark_lock(curr, hlock,
  2584. LOCK_ENABLED_SOFTIRQ))
  2585. return 0;
  2586. }
  2587. }
  2588. return 1;
  2589. }
  2590. static inline unsigned int task_irq_context(struct task_struct *task)
  2591. {
  2592. return 2 * !!task->hardirq_context + !!task->softirq_context;
  2593. }
  2594. static int separate_irq_context(struct task_struct *curr,
  2595. struct held_lock *hlock)
  2596. {
  2597. unsigned int depth = curr->lockdep_depth;
  2598. /*
  2599. * Keep track of points where we cross into an interrupt context:
  2600. */
  2601. if (depth) {
  2602. struct held_lock *prev_hlock;
  2603. prev_hlock = curr->held_locks + depth-1;
  2604. /*
  2605. * If we cross into another context, reset the
  2606. * hash key (this also prevents the checking and the
  2607. * adding of the dependency to 'prev'):
  2608. */
  2609. if (prev_hlock->irq_context != hlock->irq_context)
  2610. return 1;
  2611. }
  2612. return 0;
  2613. }
  2614. #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
  2615. static inline
  2616. int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
  2617. enum lock_usage_bit new_bit)
  2618. {
  2619. WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
  2620. return 1;
  2621. }
  2622. static inline int mark_irqflags(struct task_struct *curr,
  2623. struct held_lock *hlock)
  2624. {
  2625. return 1;
  2626. }
  2627. static inline unsigned int task_irq_context(struct task_struct *task)
  2628. {
  2629. return 0;
  2630. }
  2631. static inline int separate_irq_context(struct task_struct *curr,
  2632. struct held_lock *hlock)
  2633. {
  2634. return 0;
  2635. }
  2636. #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
  2637. /*
  2638. * Mark a lock with a usage bit, and validate the state transition:
  2639. */
  2640. static int mark_lock(struct task_struct *curr, struct held_lock *this,
  2641. enum lock_usage_bit new_bit)
  2642. {
  2643. unsigned int new_mask = 1 << new_bit, ret = 1;
  2644. /*
  2645. * If already set then do not dirty the cacheline,
  2646. * nor do any checks:
  2647. */
  2648. if (likely(hlock_class(this)->usage_mask & new_mask))
  2649. return 1;
  2650. if (!graph_lock())
  2651. return 0;
  2652. /*
  2653. * Make sure we didn't race:
  2654. */
  2655. if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
  2656. graph_unlock();
  2657. return 1;
  2658. }
  2659. hlock_class(this)->usage_mask |= new_mask;
  2660. if (!save_trace(hlock_class(this)->usage_traces + new_bit))
  2661. return 0;
  2662. switch (new_bit) {
  2663. #define LOCKDEP_STATE(__STATE) \
  2664. case LOCK_USED_IN_##__STATE: \
  2665. case LOCK_USED_IN_##__STATE##_READ: \
  2666. case LOCK_ENABLED_##__STATE: \
  2667. case LOCK_ENABLED_##__STATE##_READ:
  2668. #include "lockdep_states.h"
  2669. #undef LOCKDEP_STATE
  2670. ret = mark_lock_irq(curr, this, new_bit);
  2671. if (!ret)
  2672. return 0;
  2673. break;
  2674. case LOCK_USED:
  2675. debug_atomic_dec(nr_unused_locks);
  2676. break;
  2677. default:
  2678. if (!debug_locks_off_graph_unlock())
  2679. return 0;
  2680. WARN_ON(1);
  2681. return 0;
  2682. }
  2683. graph_unlock();
  2684. /*
  2685. * We must printk outside of the graph_lock:
  2686. */
  2687. if (ret == 2) {
  2688. printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
  2689. print_lock(this);
  2690. print_irqtrace_events(curr);
  2691. dump_stack();
  2692. }
  2693. return ret;
  2694. }
  2695. /*
  2696. * Initialize a lock instance's lock-class mapping info:
  2697. */
  2698. static void __lockdep_init_map(struct lockdep_map *lock, const char *name,
  2699. struct lock_class_key *key, int subclass)
  2700. {
  2701. int i;
  2702. for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
  2703. lock->class_cache[i] = NULL;
  2704. #ifdef CONFIG_LOCK_STAT
  2705. lock->cpu = raw_smp_processor_id();
  2706. #endif
  2707. /*
  2708. * Can't be having no nameless bastards around this place!
  2709. */
  2710. if (DEBUG_LOCKS_WARN_ON(!name)) {
  2711. lock->name = "NULL";
  2712. return;
  2713. }
  2714. lock->name = name;
  2715. /*
  2716. * No key, no joy, we need to hash something.
  2717. */
  2718. if (DEBUG_LOCKS_WARN_ON(!key))
  2719. return;
  2720. /*
  2721. * Sanity check, the lock-class key must be persistent:
  2722. */
  2723. if (!static_obj(key)) {
  2724. printk("BUG: key %px not in .data!\n", key);
  2725. /*
  2726. * What it says above ^^^^^, I suggest you read it.
  2727. */
  2728. DEBUG_LOCKS_WARN_ON(1);
  2729. return;
  2730. }
  2731. lock->key = key;
  2732. if (unlikely(!debug_locks))
  2733. return;
  2734. if (subclass) {
  2735. unsigned long flags;
  2736. if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
  2737. return;
  2738. raw_local_irq_save(flags);
  2739. current->lockdep_recursion = 1;
  2740. register_lock_class(lock, subclass, 1);
  2741. current->lockdep_recursion = 0;
  2742. raw_local_irq_restore(flags);
  2743. }
  2744. }
  2745. void lockdep_init_map(struct lockdep_map *lock, const char *name,
  2746. struct lock_class_key *key, int subclass)
  2747. {
  2748. __lockdep_init_map(lock, name, key, subclass);
  2749. }
  2750. EXPORT_SYMBOL_GPL(lockdep_init_map);
  2751. struct lock_class_key __lockdep_no_validate__;
  2752. EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
  2753. static int
  2754. print_lock_nested_lock_not_held(struct task_struct *curr,
  2755. struct held_lock *hlock,
  2756. unsigned long ip)
  2757. {
  2758. if (!debug_locks_off())
  2759. return 0;
  2760. if (debug_locks_silent)
  2761. return 0;
  2762. pr_warn("\n");
  2763. pr_warn("==================================\n");
  2764. pr_warn("WARNING: Nested lock was not taken\n");
  2765. print_kernel_ident();
  2766. pr_warn("----------------------------------\n");
  2767. pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
  2768. print_lock(hlock);
  2769. pr_warn("\nbut this task is not holding:\n");
  2770. pr_warn("%s\n", hlock->nest_lock->name);
  2771. pr_warn("\nstack backtrace:\n");
  2772. dump_stack();
  2773. pr_warn("\nother info that might help us debug this:\n");
  2774. lockdep_print_held_locks(curr);
  2775. pr_warn("\nstack backtrace:\n");
  2776. dump_stack();
  2777. return 0;
  2778. }
  2779. static int __lock_is_held(const struct lockdep_map *lock, int read);
  2780. /*
  2781. * This gets called for every mutex_lock*()/spin_lock*() operation.
  2782. * We maintain the dependency maps and validate the locking attempt:
  2783. */
  2784. static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  2785. int trylock, int read, int check, int hardirqs_off,
  2786. struct lockdep_map *nest_lock, unsigned long ip,
  2787. int references, int pin_count)
  2788. {
  2789. struct task_struct *curr = current;
  2790. struct lock_class *class = NULL;
  2791. struct held_lock *hlock;
  2792. unsigned int depth;
  2793. int chain_head = 0;
  2794. int class_idx;
  2795. u64 chain_key;
  2796. if (unlikely(!debug_locks))
  2797. return 0;
  2798. /*
  2799. * Lockdep should run with IRQs disabled, otherwise we could
  2800. * get an interrupt which would want to take locks, which would
  2801. * end up in lockdep and have you got a head-ache already?
  2802. */
  2803. if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
  2804. return 0;
  2805. if (!prove_locking || lock->key == &__lockdep_no_validate__)
  2806. check = 0;
  2807. if (subclass < NR_LOCKDEP_CACHING_CLASSES)
  2808. class = lock->class_cache[subclass];
  2809. /*
  2810. * Not cached?
  2811. */
  2812. if (unlikely(!class)) {
  2813. class = register_lock_class(lock, subclass, 0);
  2814. if (!class)
  2815. return 0;
  2816. }
  2817. atomic_inc((atomic_t *)&class->ops);
  2818. if (very_verbose(class)) {
  2819. printk("\nacquire class [%px] %s", class->key, class->name);
  2820. if (class->name_version > 1)
  2821. printk(KERN_CONT "#%d", class->name_version);
  2822. printk(KERN_CONT "\n");
  2823. dump_stack();
  2824. }
  2825. /*
  2826. * Add the lock to the list of currently held locks.
  2827. * (we dont increase the depth just yet, up until the
  2828. * dependency checks are done)
  2829. */
  2830. depth = curr->lockdep_depth;
  2831. /*
  2832. * Ran out of static storage for our per-task lock stack again have we?
  2833. */
  2834. if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
  2835. return 0;
  2836. class_idx = class - lock_classes + 1;
  2837. if (depth) {
  2838. hlock = curr->held_locks + depth - 1;
  2839. if (hlock->class_idx == class_idx && nest_lock) {
  2840. if (hlock->references) {
  2841. /*
  2842. * Check: unsigned int references:12, overflow.
  2843. */
  2844. if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
  2845. return 0;
  2846. hlock->references++;
  2847. } else {
  2848. hlock->references = 2;
  2849. }
  2850. return 1;
  2851. }
  2852. }
  2853. hlock = curr->held_locks + depth;
  2854. /*
  2855. * Plain impossible, we just registered it and checked it weren't no
  2856. * NULL like.. I bet this mushroom I ate was good!
  2857. */
  2858. if (DEBUG_LOCKS_WARN_ON(!class))
  2859. return 0;
  2860. hlock->class_idx = class_idx;
  2861. hlock->acquire_ip = ip;
  2862. hlock->instance = lock;
  2863. hlock->nest_lock = nest_lock;
  2864. hlock->irq_context = task_irq_context(curr);
  2865. hlock->trylock = trylock;
  2866. hlock->read = read;
  2867. hlock->check = check;
  2868. hlock->hardirqs_off = !!hardirqs_off;
  2869. hlock->references = references;
  2870. #ifdef CONFIG_LOCK_STAT
  2871. hlock->waittime_stamp = 0;
  2872. hlock->holdtime_stamp = lockstat_clock();
  2873. #endif
  2874. hlock->pin_count = pin_count;
  2875. if (check && !mark_irqflags(curr, hlock))
  2876. return 0;
  2877. /* mark it as used: */
  2878. if (!mark_lock(curr, hlock, LOCK_USED))
  2879. return 0;
  2880. /*
  2881. * Calculate the chain hash: it's the combined hash of all the
  2882. * lock keys along the dependency chain. We save the hash value
  2883. * at every step so that we can get the current hash easily
  2884. * after unlock. The chain hash is then used to cache dependency
  2885. * results.
  2886. *
  2887. * The 'key ID' is what is the most compact key value to drive
  2888. * the hash, not class->key.
  2889. */
  2890. /*
  2891. * Whoops, we did it again.. ran straight out of our static allocation.
  2892. */
  2893. if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
  2894. return 0;
  2895. chain_key = curr->curr_chain_key;
  2896. if (!depth) {
  2897. /*
  2898. * How can we have a chain hash when we ain't got no keys?!
  2899. */
  2900. if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
  2901. return 0;
  2902. chain_head = 1;
  2903. }
  2904. hlock->prev_chain_key = chain_key;
  2905. if (separate_irq_context(curr, hlock)) {
  2906. chain_key = 0;
  2907. chain_head = 1;
  2908. }
  2909. chain_key = iterate_chain_key(chain_key, class_idx);
  2910. if (nest_lock && !__lock_is_held(nest_lock, -1))
  2911. return print_lock_nested_lock_not_held(curr, hlock, ip);
  2912. if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
  2913. return 0;
  2914. curr->curr_chain_key = chain_key;
  2915. curr->lockdep_depth++;
  2916. check_chain_key(curr);
  2917. #ifdef CONFIG_DEBUG_LOCKDEP
  2918. if (unlikely(!debug_locks))
  2919. return 0;
  2920. #endif
  2921. if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
  2922. debug_locks_off();
  2923. print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
  2924. printk(KERN_DEBUG "depth: %i max: %lu!\n",
  2925. curr->lockdep_depth, MAX_LOCK_DEPTH);
  2926. lockdep_print_held_locks(current);
  2927. debug_show_all_locks();
  2928. dump_stack();
  2929. return 0;
  2930. }
  2931. if (unlikely(curr->lockdep_depth > max_lockdep_depth))
  2932. max_lockdep_depth = curr->lockdep_depth;
  2933. return 1;
  2934. }
  2935. static int
  2936. print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
  2937. unsigned long ip)
  2938. {
  2939. if (!debug_locks_off())
  2940. return 0;
  2941. if (debug_locks_silent)
  2942. return 0;
  2943. pr_warn("\n");
  2944. pr_warn("=====================================\n");
  2945. pr_warn("WARNING: bad unlock balance detected!\n");
  2946. print_kernel_ident();
  2947. pr_warn("-------------------------------------\n");
  2948. pr_warn("%s/%d is trying to release lock (",
  2949. curr->comm, task_pid_nr(curr));
  2950. print_lockdep_cache(lock);
  2951. pr_cont(") at:\n");
  2952. print_ip_sym(ip);
  2953. pr_warn("but there are no more locks to release!\n");
  2954. pr_warn("\nother info that might help us debug this:\n");
  2955. lockdep_print_held_locks(curr);
  2956. pr_warn("\nstack backtrace:\n");
  2957. dump_stack();
  2958. return 0;
  2959. }
  2960. static int match_held_lock(const struct held_lock *hlock,
  2961. const struct lockdep_map *lock)
  2962. {
  2963. if (hlock->instance == lock)
  2964. return 1;
  2965. if (hlock->references) {
  2966. const struct lock_class *class = lock->class_cache[0];
  2967. if (!class)
  2968. class = look_up_lock_class(lock, 0);
  2969. /*
  2970. * If look_up_lock_class() failed to find a class, we're trying
  2971. * to test if we hold a lock that has never yet been acquired.
  2972. * Clearly if the lock hasn't been acquired _ever_, we're not
  2973. * holding it either, so report failure.
  2974. */
  2975. if (!class)
  2976. return 0;
  2977. /*
  2978. * References, but not a lock we're actually ref-counting?
  2979. * State got messed up, follow the sites that change ->references
  2980. * and try to make sense of it.
  2981. */
  2982. if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
  2983. return 0;
  2984. if (hlock->class_idx == class - lock_classes + 1)
  2985. return 1;
  2986. }
  2987. return 0;
  2988. }
  2989. /* @depth must not be zero */
  2990. static struct held_lock *find_held_lock(struct task_struct *curr,
  2991. struct lockdep_map *lock,
  2992. unsigned int depth, int *idx)
  2993. {
  2994. struct held_lock *ret, *hlock, *prev_hlock;
  2995. int i;
  2996. i = depth - 1;
  2997. hlock = curr->held_locks + i;
  2998. ret = hlock;
  2999. if (match_held_lock(hlock, lock))
  3000. goto out;
  3001. ret = NULL;
  3002. for (i--, prev_hlock = hlock--;
  3003. i >= 0;
  3004. i--, prev_hlock = hlock--) {
  3005. /*
  3006. * We must not cross into another context:
  3007. */
  3008. if (prev_hlock->irq_context != hlock->irq_context) {
  3009. ret = NULL;
  3010. break;
  3011. }
  3012. if (match_held_lock(hlock, lock)) {
  3013. ret = hlock;
  3014. break;
  3015. }
  3016. }
  3017. out:
  3018. *idx = i;
  3019. return ret;
  3020. }
  3021. static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
  3022. int idx)
  3023. {
  3024. struct held_lock *hlock;
  3025. for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
  3026. if (!__lock_acquire(hlock->instance,
  3027. hlock_class(hlock)->subclass,
  3028. hlock->trylock,
  3029. hlock->read, hlock->check,
  3030. hlock->hardirqs_off,
  3031. hlock->nest_lock, hlock->acquire_ip,
  3032. hlock->references, hlock->pin_count))
  3033. return 1;
  3034. }
  3035. return 0;
  3036. }
  3037. static int
  3038. __lock_set_class(struct lockdep_map *lock, const char *name,
  3039. struct lock_class_key *key, unsigned int subclass,
  3040. unsigned long ip)
  3041. {
  3042. struct task_struct *curr = current;
  3043. struct held_lock *hlock;
  3044. struct lock_class *class;
  3045. unsigned int depth;
  3046. int i;
  3047. depth = curr->lockdep_depth;
  3048. /*
  3049. * This function is about (re)setting the class of a held lock,
  3050. * yet we're not actually holding any locks. Naughty user!
  3051. */
  3052. if (DEBUG_LOCKS_WARN_ON(!depth))
  3053. return 0;
  3054. hlock = find_held_lock(curr, lock, depth, &i);
  3055. if (!hlock)
  3056. return print_unlock_imbalance_bug(curr, lock, ip);
  3057. lockdep_init_map(lock, name, key, 0);
  3058. class = register_lock_class(lock, subclass, 0);
  3059. hlock->class_idx = class - lock_classes + 1;
  3060. curr->lockdep_depth = i;
  3061. curr->curr_chain_key = hlock->prev_chain_key;
  3062. if (reacquire_held_locks(curr, depth, i))
  3063. return 0;
  3064. /*
  3065. * I took it apart and put it back together again, except now I have
  3066. * these 'spare' parts.. where shall I put them.
  3067. */
  3068. if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
  3069. return 0;
  3070. return 1;
  3071. }
  3072. static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
  3073. {
  3074. struct task_struct *curr = current;
  3075. struct held_lock *hlock;
  3076. unsigned int depth;
  3077. int i;
  3078. depth = curr->lockdep_depth;
  3079. /*
  3080. * This function is about (re)setting the class of a held lock,
  3081. * yet we're not actually holding any locks. Naughty user!
  3082. */
  3083. if (DEBUG_LOCKS_WARN_ON(!depth))
  3084. return 0;
  3085. hlock = find_held_lock(curr, lock, depth, &i);
  3086. if (!hlock)
  3087. return print_unlock_imbalance_bug(curr, lock, ip);
  3088. curr->lockdep_depth = i;
  3089. curr->curr_chain_key = hlock->prev_chain_key;
  3090. WARN(hlock->read, "downgrading a read lock");
  3091. hlock->read = 1;
  3092. hlock->acquire_ip = ip;
  3093. if (reacquire_held_locks(curr, depth, i))
  3094. return 0;
  3095. /*
  3096. * I took it apart and put it back together again, except now I have
  3097. * these 'spare' parts.. where shall I put them.
  3098. */
  3099. if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
  3100. return 0;
  3101. return 1;
  3102. }
  3103. /*
  3104. * Remove the lock to the list of currently held locks - this gets
  3105. * called on mutex_unlock()/spin_unlock*() (or on a failed
  3106. * mutex_lock_interruptible()).
  3107. *
  3108. * @nested is an hysterical artifact, needs a tree wide cleanup.
  3109. */
  3110. static int
  3111. __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
  3112. {
  3113. struct task_struct *curr = current;
  3114. struct held_lock *hlock;
  3115. unsigned int depth;
  3116. int i;
  3117. if (unlikely(!debug_locks))
  3118. return 0;
  3119. depth = curr->lockdep_depth;
  3120. /*
  3121. * So we're all set to release this lock.. wait what lock? We don't
  3122. * own any locks, you've been drinking again?
  3123. */
  3124. if (DEBUG_LOCKS_WARN_ON(depth <= 0))
  3125. return print_unlock_imbalance_bug(curr, lock, ip);
  3126. /*
  3127. * Check whether the lock exists in the current stack
  3128. * of held locks:
  3129. */
  3130. hlock = find_held_lock(curr, lock, depth, &i);
  3131. if (!hlock)
  3132. return print_unlock_imbalance_bug(curr, lock, ip);
  3133. if (hlock->instance == lock)
  3134. lock_release_holdtime(hlock);
  3135. WARN(hlock->pin_count, "releasing a pinned lock\n");
  3136. if (hlock->references) {
  3137. hlock->references--;
  3138. if (hlock->references) {
  3139. /*
  3140. * We had, and after removing one, still have
  3141. * references, the current lock stack is still
  3142. * valid. We're done!
  3143. */
  3144. return 1;
  3145. }
  3146. }
  3147. /*
  3148. * We have the right lock to unlock, 'hlock' points to it.
  3149. * Now we remove it from the stack, and add back the other
  3150. * entries (if any), recalculating the hash along the way:
  3151. */
  3152. curr->lockdep_depth = i;
  3153. curr->curr_chain_key = hlock->prev_chain_key;
  3154. if (reacquire_held_locks(curr, depth, i + 1))
  3155. return 0;
  3156. /*
  3157. * We had N bottles of beer on the wall, we drank one, but now
  3158. * there's not N-1 bottles of beer left on the wall...
  3159. */
  3160. if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
  3161. return 0;
  3162. return 1;
  3163. }
  3164. static int __lock_is_held(const struct lockdep_map *lock, int read)
  3165. {
  3166. struct task_struct *curr = current;
  3167. int i;
  3168. for (i = 0; i < curr->lockdep_depth; i++) {
  3169. struct held_lock *hlock = curr->held_locks + i;
  3170. if (match_held_lock(hlock, lock)) {
  3171. if (read == -1 || hlock->read == read)
  3172. return 1;
  3173. return 0;
  3174. }
  3175. }
  3176. return 0;
  3177. }
  3178. static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
  3179. {
  3180. struct pin_cookie cookie = NIL_COOKIE;
  3181. struct task_struct *curr = current;
  3182. int i;
  3183. if (unlikely(!debug_locks))
  3184. return cookie;
  3185. for (i = 0; i < curr->lockdep_depth; i++) {
  3186. struct held_lock *hlock = curr->held_locks + i;
  3187. if (match_held_lock(hlock, lock)) {
  3188. /*
  3189. * Grab 16bits of randomness; this is sufficient to not
  3190. * be guessable and still allows some pin nesting in
  3191. * our u32 pin_count.
  3192. */
  3193. cookie.val = 1 + (prandom_u32() >> 16);
  3194. hlock->pin_count += cookie.val;
  3195. return cookie;
  3196. }
  3197. }
  3198. WARN(1, "pinning an unheld lock\n");
  3199. return cookie;
  3200. }
  3201. static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3202. {
  3203. struct task_struct *curr = current;
  3204. int i;
  3205. if (unlikely(!debug_locks))
  3206. return;
  3207. for (i = 0; i < curr->lockdep_depth; i++) {
  3208. struct held_lock *hlock = curr->held_locks + i;
  3209. if (match_held_lock(hlock, lock)) {
  3210. hlock->pin_count += cookie.val;
  3211. return;
  3212. }
  3213. }
  3214. WARN(1, "pinning an unheld lock\n");
  3215. }
  3216. static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3217. {
  3218. struct task_struct *curr = current;
  3219. int i;
  3220. if (unlikely(!debug_locks))
  3221. return;
  3222. for (i = 0; i < curr->lockdep_depth; i++) {
  3223. struct held_lock *hlock = curr->held_locks + i;
  3224. if (match_held_lock(hlock, lock)) {
  3225. if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
  3226. return;
  3227. hlock->pin_count -= cookie.val;
  3228. if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
  3229. hlock->pin_count = 0;
  3230. return;
  3231. }
  3232. }
  3233. WARN(1, "unpinning an unheld lock\n");
  3234. }
  3235. /*
  3236. * Check whether we follow the irq-flags state precisely:
  3237. */
  3238. static void check_flags(unsigned long flags)
  3239. {
  3240. #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
  3241. defined(CONFIG_TRACE_IRQFLAGS)
  3242. if (!debug_locks)
  3243. return;
  3244. if (irqs_disabled_flags(flags)) {
  3245. if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
  3246. printk("possible reason: unannotated irqs-off.\n");
  3247. }
  3248. } else {
  3249. if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
  3250. printk("possible reason: unannotated irqs-on.\n");
  3251. }
  3252. }
  3253. /*
  3254. * We dont accurately track softirq state in e.g.
  3255. * hardirq contexts (such as on 4KSTACKS), so only
  3256. * check if not in hardirq contexts:
  3257. */
  3258. if (!hardirq_count()) {
  3259. if (softirq_count()) {
  3260. /* like the above, but with softirqs */
  3261. DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
  3262. } else {
  3263. /* lick the above, does it taste good? */
  3264. DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
  3265. }
  3266. }
  3267. if (!debug_locks)
  3268. print_irqtrace_events(current);
  3269. #endif
  3270. }
  3271. void lock_set_class(struct lockdep_map *lock, const char *name,
  3272. struct lock_class_key *key, unsigned int subclass,
  3273. 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_set_class(lock, name, key, subclass, ip))
  3282. check_chain_key(current);
  3283. current->lockdep_recursion = 0;
  3284. raw_local_irq_restore(flags);
  3285. }
  3286. EXPORT_SYMBOL_GPL(lock_set_class);
  3287. void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
  3288. {
  3289. unsigned long flags;
  3290. if (unlikely(current->lockdep_recursion))
  3291. return;
  3292. raw_local_irq_save(flags);
  3293. current->lockdep_recursion = 1;
  3294. check_flags(flags);
  3295. if (__lock_downgrade(lock, ip))
  3296. check_chain_key(current);
  3297. current->lockdep_recursion = 0;
  3298. raw_local_irq_restore(flags);
  3299. }
  3300. EXPORT_SYMBOL_GPL(lock_downgrade);
  3301. /*
  3302. * We are not always called with irqs disabled - do that here,
  3303. * and also avoid lockdep recursion:
  3304. */
  3305. void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  3306. int trylock, int read, int check,
  3307. struct lockdep_map *nest_lock, unsigned long ip)
  3308. {
  3309. unsigned long flags;
  3310. if (unlikely(current->lockdep_recursion))
  3311. return;
  3312. raw_local_irq_save(flags);
  3313. check_flags(flags);
  3314. current->lockdep_recursion = 1;
  3315. trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
  3316. __lock_acquire(lock, subclass, trylock, read, check,
  3317. irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
  3318. current->lockdep_recursion = 0;
  3319. raw_local_irq_restore(flags);
  3320. }
  3321. EXPORT_SYMBOL_GPL(lock_acquire);
  3322. void lock_release(struct lockdep_map *lock, int nested,
  3323. unsigned long ip)
  3324. {
  3325. unsigned long flags;
  3326. if (unlikely(current->lockdep_recursion))
  3327. return;
  3328. raw_local_irq_save(flags);
  3329. check_flags(flags);
  3330. current->lockdep_recursion = 1;
  3331. trace_lock_release(lock, ip);
  3332. if (__lock_release(lock, nested, ip))
  3333. check_chain_key(current);
  3334. current->lockdep_recursion = 0;
  3335. raw_local_irq_restore(flags);
  3336. }
  3337. EXPORT_SYMBOL_GPL(lock_release);
  3338. int lock_is_held_type(const struct lockdep_map *lock, int read)
  3339. {
  3340. unsigned long flags;
  3341. int ret = 0;
  3342. if (unlikely(current->lockdep_recursion))
  3343. return 1; /* avoid false negative lockdep_assert_held() */
  3344. raw_local_irq_save(flags);
  3345. check_flags(flags);
  3346. current->lockdep_recursion = 1;
  3347. ret = __lock_is_held(lock, read);
  3348. current->lockdep_recursion = 0;
  3349. raw_local_irq_restore(flags);
  3350. return ret;
  3351. }
  3352. EXPORT_SYMBOL_GPL(lock_is_held_type);
  3353. struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
  3354. {
  3355. struct pin_cookie cookie = NIL_COOKIE;
  3356. unsigned long flags;
  3357. if (unlikely(current->lockdep_recursion))
  3358. return cookie;
  3359. raw_local_irq_save(flags);
  3360. check_flags(flags);
  3361. current->lockdep_recursion = 1;
  3362. cookie = __lock_pin_lock(lock);
  3363. current->lockdep_recursion = 0;
  3364. raw_local_irq_restore(flags);
  3365. return cookie;
  3366. }
  3367. EXPORT_SYMBOL_GPL(lock_pin_lock);
  3368. void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3369. {
  3370. unsigned long flags;
  3371. if (unlikely(current->lockdep_recursion))
  3372. return;
  3373. raw_local_irq_save(flags);
  3374. check_flags(flags);
  3375. current->lockdep_recursion = 1;
  3376. __lock_repin_lock(lock, cookie);
  3377. current->lockdep_recursion = 0;
  3378. raw_local_irq_restore(flags);
  3379. }
  3380. EXPORT_SYMBOL_GPL(lock_repin_lock);
  3381. void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
  3382. {
  3383. unsigned long flags;
  3384. if (unlikely(current->lockdep_recursion))
  3385. return;
  3386. raw_local_irq_save(flags);
  3387. check_flags(flags);
  3388. current->lockdep_recursion = 1;
  3389. __lock_unpin_lock(lock, cookie);
  3390. current->lockdep_recursion = 0;
  3391. raw_local_irq_restore(flags);
  3392. }
  3393. EXPORT_SYMBOL_GPL(lock_unpin_lock);
  3394. #ifdef CONFIG_LOCK_STAT
  3395. static int
  3396. print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
  3397. unsigned long ip)
  3398. {
  3399. if (!debug_locks_off())
  3400. return 0;
  3401. if (debug_locks_silent)
  3402. return 0;
  3403. pr_warn("\n");
  3404. pr_warn("=================================\n");
  3405. pr_warn("WARNING: bad contention detected!\n");
  3406. print_kernel_ident();
  3407. pr_warn("---------------------------------\n");
  3408. pr_warn("%s/%d is trying to contend lock (",
  3409. curr->comm, task_pid_nr(curr));
  3410. print_lockdep_cache(lock);
  3411. pr_cont(") at:\n");
  3412. print_ip_sym(ip);
  3413. pr_warn("but there are no locks held!\n");
  3414. pr_warn("\nother info that might help us debug this:\n");
  3415. lockdep_print_held_locks(curr);
  3416. pr_warn("\nstack backtrace:\n");
  3417. dump_stack();
  3418. return 0;
  3419. }
  3420. static void
  3421. __lock_contended(struct lockdep_map *lock, unsigned long ip)
  3422. {
  3423. struct task_struct *curr = current;
  3424. struct held_lock *hlock;
  3425. struct lock_class_stats *stats;
  3426. unsigned int depth;
  3427. int i, contention_point, contending_point;
  3428. depth = curr->lockdep_depth;
  3429. /*
  3430. * Whee, we contended on this lock, except it seems we're not
  3431. * actually trying to acquire anything much at all..
  3432. */
  3433. if (DEBUG_LOCKS_WARN_ON(!depth))
  3434. return;
  3435. hlock = find_held_lock(curr, lock, depth, &i);
  3436. if (!hlock) {
  3437. print_lock_contention_bug(curr, lock, ip);
  3438. return;
  3439. }
  3440. if (hlock->instance != lock)
  3441. return;
  3442. hlock->waittime_stamp = lockstat_clock();
  3443. contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
  3444. contending_point = lock_point(hlock_class(hlock)->contending_point,
  3445. lock->ip);
  3446. stats = get_lock_stats(hlock_class(hlock));
  3447. if (contention_point < LOCKSTAT_POINTS)
  3448. stats->contention_point[contention_point]++;
  3449. if (contending_point < LOCKSTAT_POINTS)
  3450. stats->contending_point[contending_point]++;
  3451. if (lock->cpu != smp_processor_id())
  3452. stats->bounces[bounce_contended + !!hlock->read]++;
  3453. put_lock_stats(stats);
  3454. }
  3455. static void
  3456. __lock_acquired(struct lockdep_map *lock, unsigned long ip)
  3457. {
  3458. struct task_struct *curr = current;
  3459. struct held_lock *hlock;
  3460. struct lock_class_stats *stats;
  3461. unsigned int depth;
  3462. u64 now, waittime = 0;
  3463. int i, cpu;
  3464. depth = curr->lockdep_depth;
  3465. /*
  3466. * Yay, we acquired ownership of this lock we didn't try to
  3467. * acquire, how the heck did that happen?
  3468. */
  3469. if (DEBUG_LOCKS_WARN_ON(!depth))
  3470. return;
  3471. hlock = find_held_lock(curr, lock, depth, &i);
  3472. if (!hlock) {
  3473. print_lock_contention_bug(curr, lock, _RET_IP_);
  3474. return;
  3475. }
  3476. if (hlock->instance != lock)
  3477. return;
  3478. cpu = smp_processor_id();
  3479. if (hlock->waittime_stamp) {
  3480. now = lockstat_clock();
  3481. waittime = now - hlock->waittime_stamp;
  3482. hlock->holdtime_stamp = now;
  3483. }
  3484. trace_lock_acquired(lock, ip);
  3485. stats = get_lock_stats(hlock_class(hlock));
  3486. if (waittime) {
  3487. if (hlock->read)
  3488. lock_time_inc(&stats->read_waittime, waittime);
  3489. else
  3490. lock_time_inc(&stats->write_waittime, waittime);
  3491. }
  3492. if (lock->cpu != cpu)
  3493. stats->bounces[bounce_acquired + !!hlock->read]++;
  3494. put_lock_stats(stats);
  3495. lock->cpu = cpu;
  3496. lock->ip = ip;
  3497. }
  3498. void lock_contended(struct lockdep_map *lock, unsigned long ip)
  3499. {
  3500. unsigned long flags;
  3501. if (unlikely(!lock_stat))
  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. trace_lock_contended(lock, ip);
  3509. __lock_contended(lock, ip);
  3510. current->lockdep_recursion = 0;
  3511. raw_local_irq_restore(flags);
  3512. }
  3513. EXPORT_SYMBOL_GPL(lock_contended);
  3514. void lock_acquired(struct lockdep_map *lock, unsigned long ip)
  3515. {
  3516. unsigned long flags;
  3517. if (unlikely(!lock_stat))
  3518. return;
  3519. if (unlikely(current->lockdep_recursion))
  3520. return;
  3521. raw_local_irq_save(flags);
  3522. check_flags(flags);
  3523. current->lockdep_recursion = 1;
  3524. __lock_acquired(lock, ip);
  3525. current->lockdep_recursion = 0;
  3526. raw_local_irq_restore(flags);
  3527. }
  3528. EXPORT_SYMBOL_GPL(lock_acquired);
  3529. #endif
  3530. /*
  3531. * Used by the testsuite, sanitize the validator state
  3532. * after a simulated failure:
  3533. */
  3534. void lockdep_reset(void)
  3535. {
  3536. unsigned long flags;
  3537. int i;
  3538. raw_local_irq_save(flags);
  3539. current->curr_chain_key = 0;
  3540. current->lockdep_depth = 0;
  3541. current->lockdep_recursion = 0;
  3542. memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
  3543. nr_hardirq_chains = 0;
  3544. nr_softirq_chains = 0;
  3545. nr_process_chains = 0;
  3546. debug_locks = 1;
  3547. for (i = 0; i < CHAINHASH_SIZE; i++)
  3548. INIT_HLIST_HEAD(chainhash_table + i);
  3549. raw_local_irq_restore(flags);
  3550. }
  3551. static void zap_class(struct lock_class *class)
  3552. {
  3553. int i;
  3554. /*
  3555. * Remove all dependencies this lock is
  3556. * involved in:
  3557. */
  3558. for (i = 0; i < nr_list_entries; i++) {
  3559. if (list_entries[i].class == class)
  3560. list_del_rcu(&list_entries[i].entry);
  3561. }
  3562. /*
  3563. * Unhash the class and remove it from the all_lock_classes list:
  3564. */
  3565. hlist_del_rcu(&class->hash_entry);
  3566. list_del_rcu(&class->lock_entry);
  3567. RCU_INIT_POINTER(class->key, NULL);
  3568. RCU_INIT_POINTER(class->name, NULL);
  3569. }
  3570. static inline int within(const void *addr, void *start, unsigned long size)
  3571. {
  3572. return addr >= start && addr < start + size;
  3573. }
  3574. /*
  3575. * Used in module.c to remove lock classes from memory that is going to be
  3576. * freed; and possibly re-used by other modules.
  3577. *
  3578. * We will have had one sync_sched() before getting here, so we're guaranteed
  3579. * nobody will look up these exact classes -- they're properly dead but still
  3580. * allocated.
  3581. */
  3582. void lockdep_free_key_range(void *start, unsigned long size)
  3583. {
  3584. struct lock_class *class;
  3585. struct hlist_head *head;
  3586. unsigned long flags;
  3587. int i;
  3588. int locked;
  3589. raw_local_irq_save(flags);
  3590. locked = graph_lock();
  3591. /*
  3592. * Unhash all classes that were created by this module:
  3593. */
  3594. for (i = 0; i < CLASSHASH_SIZE; i++) {
  3595. head = classhash_table + i;
  3596. hlist_for_each_entry_rcu(class, head, hash_entry) {
  3597. if (within(class->key, start, size))
  3598. zap_class(class);
  3599. else if (within(class->name, start, size))
  3600. zap_class(class);
  3601. }
  3602. }
  3603. if (locked)
  3604. graph_unlock();
  3605. raw_local_irq_restore(flags);
  3606. /*
  3607. * Wait for any possible iterators from look_up_lock_class() to pass
  3608. * before continuing to free the memory they refer to.
  3609. *
  3610. * sync_sched() is sufficient because the read-side is IRQ disable.
  3611. */
  3612. synchronize_sched();
  3613. /*
  3614. * XXX at this point we could return the resources to the pool;
  3615. * instead we leak them. We would need to change to bitmap allocators
  3616. * instead of the linear allocators we have now.
  3617. */
  3618. }
  3619. void lockdep_reset_lock(struct lockdep_map *lock)
  3620. {
  3621. struct lock_class *class;
  3622. struct hlist_head *head;
  3623. unsigned long flags;
  3624. int i, j;
  3625. int locked;
  3626. raw_local_irq_save(flags);
  3627. /*
  3628. * Remove all classes this lock might have:
  3629. */
  3630. for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
  3631. /*
  3632. * If the class exists we look it up and zap it:
  3633. */
  3634. class = look_up_lock_class(lock, j);
  3635. if (class)
  3636. zap_class(class);
  3637. }
  3638. /*
  3639. * Debug check: in the end all mapped classes should
  3640. * be gone.
  3641. */
  3642. locked = graph_lock();
  3643. for (i = 0; i < CLASSHASH_SIZE; i++) {
  3644. head = classhash_table + i;
  3645. hlist_for_each_entry_rcu(class, head, hash_entry) {
  3646. int match = 0;
  3647. for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
  3648. match |= class == lock->class_cache[j];
  3649. if (unlikely(match)) {
  3650. if (debug_locks_off_graph_unlock()) {
  3651. /*
  3652. * We all just reset everything, how did it match?
  3653. */
  3654. WARN_ON(1);
  3655. }
  3656. goto out_restore;
  3657. }
  3658. }
  3659. }
  3660. if (locked)
  3661. graph_unlock();
  3662. out_restore:
  3663. raw_local_irq_restore(flags);
  3664. }
  3665. void __init lockdep_info(void)
  3666. {
  3667. printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
  3668. printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
  3669. printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
  3670. printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
  3671. printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
  3672. printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
  3673. printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
  3674. printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
  3675. printk(" memory used by lock dependency info: %lu kB\n",
  3676. (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
  3677. sizeof(struct list_head) * CLASSHASH_SIZE +
  3678. sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
  3679. sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
  3680. sizeof(struct list_head) * CHAINHASH_SIZE
  3681. #ifdef CONFIG_PROVE_LOCKING
  3682. + sizeof(struct circular_queue)
  3683. #endif
  3684. ) / 1024
  3685. );
  3686. printk(" per task-struct memory footprint: %lu bytes\n",
  3687. sizeof(struct held_lock) * MAX_LOCK_DEPTH);
  3688. }
  3689. static void
  3690. print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
  3691. const void *mem_to, struct held_lock *hlock)
  3692. {
  3693. if (!debug_locks_off())
  3694. return;
  3695. if (debug_locks_silent)
  3696. return;
  3697. pr_warn("\n");
  3698. pr_warn("=========================\n");
  3699. pr_warn("WARNING: held lock freed!\n");
  3700. print_kernel_ident();
  3701. pr_warn("-------------------------\n");
  3702. pr_warn("%s/%d is freeing memory %px-%px, with a lock still held there!\n",
  3703. curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
  3704. print_lock(hlock);
  3705. lockdep_print_held_locks(curr);
  3706. pr_warn("\nstack backtrace:\n");
  3707. dump_stack();
  3708. }
  3709. static inline int not_in_range(const void* mem_from, unsigned long mem_len,
  3710. const void* lock_from, unsigned long lock_len)
  3711. {
  3712. return lock_from + lock_len <= mem_from ||
  3713. mem_from + mem_len <= lock_from;
  3714. }
  3715. /*
  3716. * Called when kernel memory is freed (or unmapped), or if a lock
  3717. * is destroyed or reinitialized - this code checks whether there is
  3718. * any held lock in the memory range of <from> to <to>:
  3719. */
  3720. void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
  3721. {
  3722. struct task_struct *curr = current;
  3723. struct held_lock *hlock;
  3724. unsigned long flags;
  3725. int i;
  3726. if (unlikely(!debug_locks))
  3727. return;
  3728. local_irq_save(flags);
  3729. for (i = 0; i < curr->lockdep_depth; i++) {
  3730. hlock = curr->held_locks + i;
  3731. if (not_in_range(mem_from, mem_len, hlock->instance,
  3732. sizeof(*hlock->instance)))
  3733. continue;
  3734. print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
  3735. break;
  3736. }
  3737. local_irq_restore(flags);
  3738. }
  3739. EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
  3740. static void print_held_locks_bug(void)
  3741. {
  3742. if (!debug_locks_off())
  3743. return;
  3744. if (debug_locks_silent)
  3745. return;
  3746. pr_warn("\n");
  3747. pr_warn("====================================\n");
  3748. pr_warn("WARNING: %s/%d still has locks held!\n",
  3749. current->comm, task_pid_nr(current));
  3750. print_kernel_ident();
  3751. pr_warn("------------------------------------\n");
  3752. lockdep_print_held_locks(current);
  3753. pr_warn("\nstack backtrace:\n");
  3754. dump_stack();
  3755. }
  3756. void debug_check_no_locks_held(void)
  3757. {
  3758. if (unlikely(current->lockdep_depth > 0))
  3759. print_held_locks_bug();
  3760. }
  3761. EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
  3762. #ifdef __KERNEL__
  3763. void debug_show_all_locks(void)
  3764. {
  3765. struct task_struct *g, *p;
  3766. if (unlikely(!debug_locks)) {
  3767. pr_warn("INFO: lockdep is turned off.\n");
  3768. return;
  3769. }
  3770. pr_warn("\nShowing all locks held in the system:\n");
  3771. rcu_read_lock();
  3772. for_each_process_thread(g, p) {
  3773. if (!p->lockdep_depth)
  3774. continue;
  3775. lockdep_print_held_locks(p);
  3776. touch_nmi_watchdog();
  3777. touch_all_softlockup_watchdogs();
  3778. }
  3779. rcu_read_unlock();
  3780. pr_warn("\n");
  3781. pr_warn("=============================================\n\n");
  3782. }
  3783. EXPORT_SYMBOL_GPL(debug_show_all_locks);
  3784. #endif
  3785. /*
  3786. * Careful: only use this function if you are sure that
  3787. * the task cannot run in parallel!
  3788. */
  3789. void debug_show_held_locks(struct task_struct *task)
  3790. {
  3791. if (unlikely(!debug_locks)) {
  3792. printk("INFO: lockdep is turned off.\n");
  3793. return;
  3794. }
  3795. lockdep_print_held_locks(task);
  3796. }
  3797. EXPORT_SYMBOL_GPL(debug_show_held_locks);
  3798. asmlinkage __visible void lockdep_sys_exit(void)
  3799. {
  3800. struct task_struct *curr = current;
  3801. if (unlikely(curr->lockdep_depth)) {
  3802. if (!debug_locks_off())
  3803. return;
  3804. pr_warn("\n");
  3805. pr_warn("================================================\n");
  3806. pr_warn("WARNING: lock held when returning to user space!\n");
  3807. print_kernel_ident();
  3808. pr_warn("------------------------------------------------\n");
  3809. pr_warn("%s/%d is leaving the kernel with locks still held!\n",
  3810. curr->comm, curr->pid);
  3811. lockdep_print_held_locks(curr);
  3812. }
  3813. /*
  3814. * The lock history for each syscall should be independent. So wipe the
  3815. * slate clean on return to userspace.
  3816. */
  3817. lockdep_invariant_state(false);
  3818. }
  3819. void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
  3820. {
  3821. struct task_struct *curr = current;
  3822. /* Note: the following can be executed concurrently, so be careful. */
  3823. pr_warn("\n");
  3824. pr_warn("=============================\n");
  3825. pr_warn("WARNING: suspicious RCU usage\n");
  3826. print_kernel_ident();
  3827. pr_warn("-----------------------------\n");
  3828. pr_warn("%s:%d %s!\n", file, line, s);
  3829. pr_warn("\nother info that might help us debug this:\n\n");
  3830. pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
  3831. !rcu_lockdep_current_cpu_online()
  3832. ? "RCU used illegally from offline CPU!\n"
  3833. : !rcu_is_watching()
  3834. ? "RCU used illegally from idle CPU!\n"
  3835. : "",
  3836. rcu_scheduler_active, debug_locks);
  3837. /*
  3838. * If a CPU is in the RCU-free window in idle (ie: in the section
  3839. * between rcu_idle_enter() and rcu_idle_exit(), then RCU
  3840. * considers that CPU to be in an "extended quiescent state",
  3841. * which means that RCU will be completely ignoring that CPU.
  3842. * Therefore, rcu_read_lock() and friends have absolutely no
  3843. * effect on a CPU running in that state. In other words, even if
  3844. * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
  3845. * delete data structures out from under it. RCU really has no
  3846. * choice here: we need to keep an RCU-free window in idle where
  3847. * the CPU may possibly enter into low power mode. This way we can
  3848. * notice an extended quiescent state to other CPUs that started a grace
  3849. * period. Otherwise we would delay any grace period as long as we run
  3850. * in the idle task.
  3851. *
  3852. * So complain bitterly if someone does call rcu_read_lock(),
  3853. * rcu_read_lock_bh() and so on from extended quiescent states.
  3854. */
  3855. if (!rcu_is_watching())
  3856. pr_warn("RCU used illegally from extended quiescent state!\n");
  3857. lockdep_print_held_locks(curr);
  3858. pr_warn("\nstack backtrace:\n");
  3859. dump_stack();
  3860. }
  3861. EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);