lockdep.c 91 KB

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