lockdep.c 101 KB

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