lockdep.c 102 KB

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