lockdep.c 103 KB

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