lockdep.c 83 KB

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