lockdep.h 10 KB

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