lockdep.c 108 KB

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