lockdep.c 105 KB

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