lockdep.c 101 KB

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