lockdep.c 104 KB

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