lockdep.c 91 KB

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