lockdep.c 84 KB

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