lockdep.c 87 KB

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