lockdep.c 69 KB

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