lockdep.c 87 KB

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