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