lockdep.c 85 KB

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