lockdep.c 88 KB

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