lockdep.c 67 KB

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