lockdep.c 80 KB

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