lockdep.c 91 KB

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