lockdep.c 76 KB

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