lockdep.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Runtime locking correctness validator
  3. *
  4. * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  6. *
  7. * see Documentation/lockdep-design.txt for more details.
  8. */
  9. #ifndef __LINUX_LOCKDEP_H
  10. #define __LINUX_LOCKDEP_H
  11. struct task_struct;
  12. struct lockdep_map;
  13. #ifdef CONFIG_LOCKDEP
  14. #include <linux/linkage.h>
  15. #include <linux/list.h>
  16. #include <linux/debug_locks.h>
  17. #include <linux/stacktrace.h>
  18. /*
  19. * Lock-class usage-state bits:
  20. */
  21. enum lock_usage_bit
  22. {
  23. LOCK_USED = 0,
  24. LOCK_USED_IN_HARDIRQ,
  25. LOCK_USED_IN_SOFTIRQ,
  26. LOCK_ENABLED_SOFTIRQS,
  27. LOCK_ENABLED_HARDIRQS,
  28. LOCK_USED_IN_HARDIRQ_READ,
  29. LOCK_USED_IN_SOFTIRQ_READ,
  30. LOCK_ENABLED_SOFTIRQS_READ,
  31. LOCK_ENABLED_HARDIRQS_READ,
  32. LOCK_USAGE_STATES
  33. };
  34. /*
  35. * Usage-state bitmasks:
  36. */
  37. #define LOCKF_USED (1 << LOCK_USED)
  38. #define LOCKF_USED_IN_HARDIRQ (1 << LOCK_USED_IN_HARDIRQ)
  39. #define LOCKF_USED_IN_SOFTIRQ (1 << LOCK_USED_IN_SOFTIRQ)
  40. #define LOCKF_ENABLED_HARDIRQS (1 << LOCK_ENABLED_HARDIRQS)
  41. #define LOCKF_ENABLED_SOFTIRQS (1 << LOCK_ENABLED_SOFTIRQS)
  42. #define LOCKF_ENABLED_IRQS (LOCKF_ENABLED_HARDIRQS | LOCKF_ENABLED_SOFTIRQS)
  43. #define LOCKF_USED_IN_IRQ (LOCKF_USED_IN_HARDIRQ | LOCKF_USED_IN_SOFTIRQ)
  44. #define LOCKF_USED_IN_HARDIRQ_READ (1 << LOCK_USED_IN_HARDIRQ_READ)
  45. #define LOCKF_USED_IN_SOFTIRQ_READ (1 << LOCK_USED_IN_SOFTIRQ_READ)
  46. #define LOCKF_ENABLED_HARDIRQS_READ (1 << LOCK_ENABLED_HARDIRQS_READ)
  47. #define LOCKF_ENABLED_SOFTIRQS_READ (1 << LOCK_ENABLED_SOFTIRQS_READ)
  48. #define LOCKF_ENABLED_IRQS_READ \
  49. (LOCKF_ENABLED_HARDIRQS_READ | LOCKF_ENABLED_SOFTIRQS_READ)
  50. #define LOCKF_USED_IN_IRQ_READ \
  51. (LOCKF_USED_IN_HARDIRQ_READ | LOCKF_USED_IN_SOFTIRQ_READ)
  52. #define MAX_LOCKDEP_SUBCLASSES 8UL
  53. /*
  54. * Lock-classes are keyed via unique addresses, by embedding the
  55. * lockclass-key into the kernel (or module) .data section. (For
  56. * static locks we use the lock address itself as the key.)
  57. */
  58. struct lockdep_subclass_key {
  59. char __one_byte;
  60. } __attribute__ ((__packed__));
  61. struct lock_class_key {
  62. struct lockdep_subclass_key subkeys[MAX_LOCKDEP_SUBCLASSES];
  63. };
  64. /*
  65. * The lock-class itself:
  66. */
  67. struct lock_class {
  68. /*
  69. * class-hash:
  70. */
  71. struct list_head hash_entry;
  72. /*
  73. * global list of all lock-classes:
  74. */
  75. struct list_head lock_entry;
  76. struct lockdep_subclass_key *key;
  77. unsigned int subclass;
  78. /*
  79. * IRQ/softirq usage tracking bits:
  80. */
  81. unsigned long usage_mask;
  82. struct stack_trace usage_traces[LOCK_USAGE_STATES];
  83. /*
  84. * These fields represent a directed graph of lock dependencies,
  85. * to every node we attach a list of "forward" and a list of
  86. * "backward" graph nodes.
  87. */
  88. struct list_head locks_after, locks_before;
  89. /*
  90. * Generation counter, when doing certain classes of graph walking,
  91. * to ensure that we check one node only once:
  92. */
  93. unsigned int version;
  94. /*
  95. * Statistics counter:
  96. */
  97. unsigned long ops;
  98. const char *name;
  99. int name_version;
  100. #ifdef CONFIG_LOCK_STAT
  101. unsigned long contention_point[4];
  102. #endif
  103. };
  104. #ifdef CONFIG_LOCK_STAT
  105. struct lock_time {
  106. s64 min;
  107. s64 max;
  108. s64 total;
  109. unsigned long nr;
  110. };
  111. enum bounce_type {
  112. bounce_acquired_write,
  113. bounce_acquired_read,
  114. bounce_contended_write,
  115. bounce_contended_read,
  116. nr_bounce_types,
  117. bounce_acquired = bounce_acquired_write,
  118. bounce_contended = bounce_contended_write,
  119. };
  120. struct lock_class_stats {
  121. unsigned long contention_point[4];
  122. struct lock_time read_waittime;
  123. struct lock_time write_waittime;
  124. struct lock_time read_holdtime;
  125. struct lock_time write_holdtime;
  126. unsigned long bounces[nr_bounce_types];
  127. };
  128. struct lock_class_stats lock_stats(struct lock_class *class);
  129. void clear_lock_stats(struct lock_class *class);
  130. #endif
  131. /*
  132. * Map the lock object (the lock instance) to the lock-class object.
  133. * This is embedded into specific lock instances:
  134. */
  135. struct lockdep_map {
  136. struct lock_class_key *key;
  137. struct lock_class *class_cache;
  138. const char *name;
  139. #ifdef CONFIG_LOCK_STAT
  140. int cpu;
  141. #endif
  142. };
  143. /*
  144. * Every lock has a list of other locks that were taken after it.
  145. * We only grow the list, never remove from it:
  146. */
  147. struct lock_list {
  148. struct list_head entry;
  149. struct lock_class *class;
  150. struct stack_trace trace;
  151. int distance;
  152. };
  153. /*
  154. * We record lock dependency chains, so that we can cache them:
  155. */
  156. struct lock_chain {
  157. u8 irq_context;
  158. u8 depth;
  159. u16 base;
  160. struct list_head entry;
  161. u64 chain_key;
  162. };
  163. struct held_lock {
  164. /*
  165. * One-way hash of the dependency chain up to this point. We
  166. * hash the hashes step by step as the dependency chain grows.
  167. *
  168. * We use it for dependency-caching and we skip detection
  169. * passes and dependency-updates if there is a cache-hit, so
  170. * it is absolutely critical for 100% coverage of the validator
  171. * to have a unique key value for every unique dependency path
  172. * that can occur in the system, to make a unique hash value
  173. * as likely as possible - hence the 64-bit width.
  174. *
  175. * The task struct holds the current hash value (initialized
  176. * with zero), here we store the previous hash value:
  177. */
  178. u64 prev_chain_key;
  179. struct lock_class *class;
  180. unsigned long acquire_ip;
  181. struct lockdep_map *instance;
  182. #ifdef CONFIG_LOCK_STAT
  183. u64 waittime_stamp;
  184. u64 holdtime_stamp;
  185. #endif
  186. /*
  187. * The lock-stack is unified in that the lock chains of interrupt
  188. * contexts nest ontop of process context chains, but we 'separate'
  189. * the hashes by starting with 0 if we cross into an interrupt
  190. * context, and we also keep do not add cross-context lock
  191. * dependencies - the lock usage graph walking covers that area
  192. * anyway, and we'd just unnecessarily increase the number of
  193. * dependencies otherwise. [Note: hardirq and softirq contexts
  194. * are separated from each other too.]
  195. *
  196. * The following field is used to detect when we cross into an
  197. * interrupt context:
  198. */
  199. int irq_context;
  200. int trylock;
  201. int read;
  202. int check;
  203. int hardirqs_off;
  204. };
  205. /*
  206. * Initialization, self-test and debugging-output methods:
  207. */
  208. extern void lockdep_init(void);
  209. extern void lockdep_info(void);
  210. extern void lockdep_reset(void);
  211. extern void lockdep_reset_lock(struct lockdep_map *lock);
  212. extern void lockdep_free_key_range(void *start, unsigned long size);
  213. extern void lockdep_sys_exit(void);
  214. extern void lockdep_off(void);
  215. extern void lockdep_on(void);
  216. /*
  217. * These methods are used by specific locking variants (spinlocks,
  218. * rwlocks, mutexes and rwsems) to pass init/acquire/release events
  219. * to lockdep:
  220. */
  221. extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
  222. struct lock_class_key *key, int subclass);
  223. /*
  224. * To initialize a lockdep_map statically use this macro.
  225. * Note that _name must not be NULL.
  226. */
  227. #define STATIC_LOCKDEP_MAP_INIT(_name, _key) \
  228. { .name = (_name), .key = (void *)(_key), }
  229. /*
  230. * Reinitialize a lock key - for cases where there is special locking or
  231. * special initialization of locks so that the validator gets the scope
  232. * of dependencies wrong: they are either too broad (they need a class-split)
  233. * or they are too narrow (they suffer from a false class-split):
  234. */
  235. #define lockdep_set_class(lock, key) \
  236. lockdep_init_map(&(lock)->dep_map, #key, key, 0)
  237. #define lockdep_set_class_and_name(lock, key, name) \
  238. lockdep_init_map(&(lock)->dep_map, name, key, 0)
  239. #define lockdep_set_class_and_subclass(lock, key, sub) \
  240. lockdep_init_map(&(lock)->dep_map, #key, key, sub)
  241. #define lockdep_set_subclass(lock, sub) \
  242. lockdep_init_map(&(lock)->dep_map, #lock, \
  243. (lock)->dep_map.key, sub)
  244. /*
  245. * Acquire a lock.
  246. *
  247. * Values for "read":
  248. *
  249. * 0: exclusive (write) acquire
  250. * 1: read-acquire (no recursion allowed)
  251. * 2: read-acquire with same-instance recursion allowed
  252. *
  253. * Values for check:
  254. *
  255. * 0: disabled
  256. * 1: simple checks (freeing, held-at-exit-time, etc.)
  257. * 2: full validation
  258. */
  259. extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  260. int trylock, int read, int check, unsigned long ip);
  261. extern void lock_release(struct lockdep_map *lock, int nested,
  262. unsigned long ip);
  263. # define INIT_LOCKDEP .lockdep_recursion = 0,
  264. #define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
  265. #else /* !LOCKDEP */
  266. static inline void lockdep_off(void)
  267. {
  268. }
  269. static inline void lockdep_on(void)
  270. {
  271. }
  272. # define lock_acquire(l, s, t, r, c, i) do { } while (0)
  273. # define lock_release(l, n, i) do { } while (0)
  274. # define lockdep_init() do { } while (0)
  275. # define lockdep_info() do { } while (0)
  276. # define lockdep_init_map(lock, name, key, sub) do { (void)(key); } while (0)
  277. # define lockdep_set_class(lock, key) do { (void)(key); } while (0)
  278. # define lockdep_set_class_and_name(lock, key, name) \
  279. do { (void)(key); } while (0)
  280. #define lockdep_set_class_and_subclass(lock, key, sub) \
  281. do { (void)(key); } while (0)
  282. #define lockdep_set_subclass(lock, sub) do { } while (0)
  283. # define INIT_LOCKDEP
  284. # define lockdep_reset() do { debug_locks = 1; } while (0)
  285. # define lockdep_free_key_range(start, size) do { } while (0)
  286. # define lockdep_sys_exit() do { } while (0)
  287. /*
  288. * The class key takes no space if lockdep is disabled:
  289. */
  290. struct lock_class_key { };
  291. #define lockdep_depth(tsk) (0)
  292. #endif /* !LOCKDEP */
  293. #ifdef CONFIG_LOCK_STAT
  294. extern void lock_contended(struct lockdep_map *lock, unsigned long ip);
  295. extern void lock_acquired(struct lockdep_map *lock);
  296. #define LOCK_CONTENDED(_lock, try, lock) \
  297. do { \
  298. if (!try(_lock)) { \
  299. lock_contended(&(_lock)->dep_map, _RET_IP_); \
  300. lock(_lock); \
  301. } \
  302. lock_acquired(&(_lock)->dep_map); \
  303. } while (0)
  304. #else /* CONFIG_LOCK_STAT */
  305. #define lock_contended(lockdep_map, ip) do {} while (0)
  306. #define lock_acquired(lockdep_map) do {} while (0)
  307. #define LOCK_CONTENDED(_lock, try, lock) \
  308. lock(_lock)
  309. #endif /* CONFIG_LOCK_STAT */
  310. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_GENERIC_HARDIRQS)
  311. extern void early_init_irq_lock_class(void);
  312. #else
  313. static inline void early_init_irq_lock_class(void)
  314. {
  315. }
  316. #endif
  317. #ifdef CONFIG_TRACE_IRQFLAGS
  318. extern void early_boot_irqs_off(void);
  319. extern void early_boot_irqs_on(void);
  320. extern void print_irqtrace_events(struct task_struct *curr);
  321. #else
  322. static inline void early_boot_irqs_off(void)
  323. {
  324. }
  325. static inline void early_boot_irqs_on(void)
  326. {
  327. }
  328. static inline void print_irqtrace_events(struct task_struct *curr)
  329. {
  330. }
  331. #endif
  332. /*
  333. * For trivial one-depth nesting of a lock-class, the following
  334. * global define can be used. (Subsystems with multiple levels
  335. * of nesting should define their own lock-nesting subclasses.)
  336. */
  337. #define SINGLE_DEPTH_NESTING 1
  338. /*
  339. * Map the dependency ops to NOP or to real lockdep ops, depending
  340. * on the per lock-class debug mode:
  341. */
  342. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  343. # ifdef CONFIG_PROVE_LOCKING
  344. # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  345. # else
  346. # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  347. # endif
  348. # define spin_release(l, n, i) lock_release(l, n, i)
  349. #else
  350. # define spin_acquire(l, s, t, i) do { } while (0)
  351. # define spin_release(l, n, i) do { } while (0)
  352. #endif
  353. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  354. # ifdef CONFIG_PROVE_LOCKING
  355. # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  356. # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 2, i)
  357. # else
  358. # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  359. # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 1, i)
  360. # endif
  361. # define rwlock_release(l, n, i) lock_release(l, n, i)
  362. #else
  363. # define rwlock_acquire(l, s, t, i) do { } while (0)
  364. # define rwlock_acquire_read(l, s, t, i) do { } while (0)
  365. # define rwlock_release(l, n, i) do { } while (0)
  366. #endif
  367. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  368. # ifdef CONFIG_PROVE_LOCKING
  369. # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  370. # else
  371. # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  372. # endif
  373. # define mutex_release(l, n, i) lock_release(l, n, i)
  374. #else
  375. # define mutex_acquire(l, s, t, i) do { } while (0)
  376. # define mutex_release(l, n, i) do { } while (0)
  377. #endif
  378. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  379. # ifdef CONFIG_PROVE_LOCKING
  380. # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  381. # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 2, i)
  382. # else
  383. # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  384. # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 1, i)
  385. # endif
  386. # define rwsem_release(l, n, i) lock_release(l, n, i)
  387. #else
  388. # define rwsem_acquire(l, s, t, i) do { } while (0)
  389. # define rwsem_acquire_read(l, s, t, i) do { } while (0)
  390. # define rwsem_release(l, n, i) do { } while (0)
  391. #endif
  392. #endif /* __LINUX_LOCKDEP_H */