lockdep.c 110 KB

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