lockdep.c 78 KB

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