lockdep.c 85 KB

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