lockdep.c 81 KB

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