lockdep.c 111 KB

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