lockdep.c 105 KB

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