lockdep.c 68 KB

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