lockdep.c 91 KB

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