lockdep.c 79 KB

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