lockdep.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. };
  116. /*
  117. * We record lock dependency chains, so that we can cache them:
  118. */
  119. struct lock_chain {
  120. struct list_head entry;
  121. u64 chain_key;
  122. };
  123. struct held_lock {
  124. /*
  125. * One-way hash of the dependency chain up to this point. We
  126. * hash the hashes step by step as the dependency chain grows.
  127. *
  128. * We use it for dependency-caching and we skip detection
  129. * passes and dependency-updates if there is a cache-hit, so
  130. * it is absolutely critical for 100% coverage of the validator
  131. * to have a unique key value for every unique dependency path
  132. * that can occur in the system, to make a unique hash value
  133. * as likely as possible - hence the 64-bit width.
  134. *
  135. * The task struct holds the current hash value (initialized
  136. * with zero), here we store the previous hash value:
  137. */
  138. u64 prev_chain_key;
  139. struct lock_class *class;
  140. unsigned long acquire_ip;
  141. struct lockdep_map *instance;
  142. /*
  143. * The lock-stack is unified in that the lock chains of interrupt
  144. * contexts nest ontop of process context chains, but we 'separate'
  145. * the hashes by starting with 0 if we cross into an interrupt
  146. * context, and we also keep do not add cross-context lock
  147. * dependencies - the lock usage graph walking covers that area
  148. * anyway, and we'd just unnecessarily increase the number of
  149. * dependencies otherwise. [Note: hardirq and softirq contexts
  150. * are separated from each other too.]
  151. *
  152. * The following field is used to detect when we cross into an
  153. * interrupt context:
  154. */
  155. int irq_context;
  156. int trylock;
  157. int read;
  158. int check;
  159. int hardirqs_off;
  160. };
  161. /*
  162. * Initialization, self-test and debugging-output methods:
  163. */
  164. extern void lockdep_init(void);
  165. extern void lockdep_info(void);
  166. extern void lockdep_reset(void);
  167. extern void lockdep_reset_lock(struct lockdep_map *lock);
  168. extern void lockdep_free_key_range(void *start, unsigned long size);
  169. extern void lockdep_off(void);
  170. extern void lockdep_on(void);
  171. extern int lockdep_internal(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. #else /* !LOCKDEP */
  215. static inline void lockdep_off(void)
  216. {
  217. }
  218. static inline void lockdep_on(void)
  219. {
  220. }
  221. static inline int lockdep_internal(void)
  222. {
  223. return 0;
  224. }
  225. # define lock_acquire(l, s, t, r, c, i) do { } while (0)
  226. # define lock_release(l, n, i) do { } while (0)
  227. # define lockdep_init() do { } while (0)
  228. # define lockdep_info() do { } while (0)
  229. # define lockdep_init_map(lock, name, key, sub) do { (void)(key); } while (0)
  230. # define lockdep_set_class(lock, key) do { (void)(key); } while (0)
  231. # define lockdep_set_class_and_name(lock, key, name) \
  232. do { (void)(key); } while (0)
  233. #define lockdep_set_class_and_subclass(lock, key, sub) \
  234. do { (void)(key); } while (0)
  235. # define INIT_LOCKDEP
  236. # define lockdep_reset() do { debug_locks = 1; } while (0)
  237. # define lockdep_free_key_range(start, size) do { } while (0)
  238. /*
  239. * The class key takes no space if lockdep is disabled:
  240. */
  241. struct lock_class_key { };
  242. #endif /* !LOCKDEP */
  243. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_GENERIC_HARDIRQS)
  244. extern void early_init_irq_lock_class(void);
  245. #else
  246. # define early_init_irq_lock_class() do { } while (0)
  247. #endif
  248. #ifdef CONFIG_TRACE_IRQFLAGS
  249. extern void early_boot_irqs_off(void);
  250. extern void early_boot_irqs_on(void);
  251. #else
  252. # define early_boot_irqs_off() do { } while (0)
  253. # define early_boot_irqs_on() do { } while (0)
  254. #endif
  255. /*
  256. * For trivial one-depth nesting of a lock-class, the following
  257. * global define can be used. (Subsystems with multiple levels
  258. * of nesting should define their own lock-nesting subclasses.)
  259. */
  260. #define SINGLE_DEPTH_NESTING 1
  261. /*
  262. * Map the dependency ops to NOP or to real lockdep ops, depending
  263. * on the per lock-class debug mode:
  264. */
  265. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  266. # ifdef CONFIG_PROVE_LOCKING
  267. # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  268. # else
  269. # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  270. # endif
  271. # define spin_release(l, n, i) lock_release(l, n, i)
  272. #else
  273. # define spin_acquire(l, s, t, i) do { } while (0)
  274. # define spin_release(l, n, i) do { } while (0)
  275. #endif
  276. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  277. # ifdef CONFIG_PROVE_LOCKING
  278. # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  279. # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 2, i)
  280. # else
  281. # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  282. # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 1, i)
  283. # endif
  284. # define rwlock_release(l, n, i) lock_release(l, n, i)
  285. #else
  286. # define rwlock_acquire(l, s, t, i) do { } while (0)
  287. # define rwlock_acquire_read(l, s, t, i) do { } while (0)
  288. # define rwlock_release(l, n, i) do { } while (0)
  289. #endif
  290. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  291. # ifdef CONFIG_PROVE_LOCKING
  292. # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  293. # else
  294. # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  295. # endif
  296. # define mutex_release(l, n, i) lock_release(l, n, i)
  297. #else
  298. # define mutex_acquire(l, s, t, i) do { } while (0)
  299. # define mutex_release(l, n, i) do { } while (0)
  300. #endif
  301. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  302. # ifdef CONFIG_PROVE_LOCKING
  303. # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  304. # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 2, i)
  305. # else
  306. # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  307. # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 1, i)
  308. # endif
  309. # define rwsem_release(l, n, i) lock_release(l, n, i)
  310. #else
  311. # define rwsem_acquire(l, s, t, i) do { } while (0)
  312. # define rwsem_acquire_read(l, s, t, i) do { } while (0)
  313. # define rwsem_release(l, n, i) do { } while (0)
  314. #endif
  315. #endif /* __LINUX_LOCKDEP_H */