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