lockdep.c 97 KB

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