lockdep.c 68 KB

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