lockdep.c 86 KB

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