lockdep.c 90 KB

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