lockdep.c 69 KB

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