lockdep.c 105 KB

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