lockdep.c 78 KB

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