lockdep.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #include <linux/linkage.h>
  11. #include <linux/list.h>
  12. #include <linux/debug_locks.h>
  13. #include <linux/stacktrace.h>
  14. #ifdef CONFIG_LOCKDEP
  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);
  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)
  187. #define lockdep_set_class_and_name(lock, key, name) \
  188. lockdep_init_map(&(lock)->dep_map, name, key)
  189. /*
  190. * Acquire a lock.
  191. *
  192. * Values for "read":
  193. *
  194. * 0: exclusive (write) acquire
  195. * 1: read-acquire (no recursion allowed)
  196. * 2: read-acquire with same-instance recursion allowed
  197. *
  198. * Values for check:
  199. *
  200. * 0: disabled
  201. * 1: simple checks (freeing, held-at-exit-time, etc.)
  202. * 2: full validation
  203. */
  204. extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  205. int trylock, int read, int check, unsigned long ip);
  206. extern void lock_release(struct lockdep_map *lock, int nested,
  207. unsigned long ip);
  208. # define INIT_LOCKDEP .lockdep_recursion = 0,
  209. #else /* !LOCKDEP */
  210. static inline void lockdep_off(void)
  211. {
  212. }
  213. static inline void lockdep_on(void)
  214. {
  215. }
  216. static inline int lockdep_internal(void)
  217. {
  218. return 0;
  219. }
  220. # define lock_acquire(l, s, t, r, c, i) do { } while (0)
  221. # define lock_release(l, n, i) do { } while (0)
  222. # define lockdep_init() do { } while (0)
  223. # define lockdep_info() do { } while (0)
  224. # define lockdep_init_map(lock, name, key) do { (void)(key); } while (0)
  225. # define lockdep_set_class(lock, key) do { (void)(key); } while (0)
  226. # define lockdep_set_class_and_name(lock, key, name) \
  227. do { (void)(key); } while (0)
  228. # define INIT_LOCKDEP
  229. # define lockdep_reset() do { debug_locks = 1; } while (0)
  230. # define lockdep_free_key_range(start, size) do { } while (0)
  231. /*
  232. * The class key takes no space if lockdep is disabled:
  233. */
  234. struct lock_class_key { };
  235. #endif /* !LOCKDEP */
  236. #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_GENERIC_HARDIRQS)
  237. extern void early_init_irq_lock_class(void);
  238. #else
  239. # define early_init_irq_lock_class() do { } while (0)
  240. #endif
  241. #ifdef CONFIG_TRACE_IRQFLAGS
  242. extern void early_boot_irqs_off(void);
  243. extern void early_boot_irqs_on(void);
  244. #else
  245. # define early_boot_irqs_off() do { } while (0)
  246. # define early_boot_irqs_on() do { } while (0)
  247. #endif
  248. /*
  249. * For trivial one-depth nesting of a lock-class, the following
  250. * global define can be used. (Subsystems with multiple levels
  251. * of nesting should define their own lock-nesting subclasses.)
  252. */
  253. #define SINGLE_DEPTH_NESTING 1
  254. /*
  255. * Map the dependency ops to NOP or to real lockdep ops, depending
  256. * on the per lock-class debug mode:
  257. */
  258. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  259. # ifdef CONFIG_PROVE_LOCKING
  260. # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  261. # else
  262. # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  263. # endif
  264. # define spin_release(l, n, i) lock_release(l, n, i)
  265. #else
  266. # define spin_acquire(l, s, t, i) do { } while (0)
  267. # define spin_release(l, n, i) do { } while (0)
  268. #endif
  269. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  270. # ifdef CONFIG_PROVE_LOCKING
  271. # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  272. # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 2, i)
  273. # else
  274. # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  275. # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 1, i)
  276. # endif
  277. # define rwlock_release(l, n, i) lock_release(l, n, i)
  278. #else
  279. # define rwlock_acquire(l, s, t, i) do { } while (0)
  280. # define rwlock_acquire_read(l, s, t, i) do { } while (0)
  281. # define rwlock_release(l, n, i) do { } while (0)
  282. #endif
  283. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  284. # ifdef CONFIG_PROVE_LOCKING
  285. # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  286. # else
  287. # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  288. # endif
  289. # define mutex_release(l, n, i) lock_release(l, n, i)
  290. #else
  291. # define mutex_acquire(l, s, t, i) do { } while (0)
  292. # define mutex_release(l, n, i) do { } while (0)
  293. #endif
  294. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  295. # ifdef CONFIG_PROVE_LOCKING
  296. # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
  297. # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 2, i)
  298. # else
  299. # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
  300. # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 1, i)
  301. # endif
  302. # define rwsem_release(l, n, i) lock_release(l, n, i)
  303. #else
  304. # define rwsem_acquire(l, s, t, i) do { } while (0)
  305. # define rwsem_acquire_read(l, s, t, i) do { } while (0)
  306. # define rwsem_release(l, n, i) do { } while (0)
  307. #endif
  308. #endif /* __LINUX_LOCKDEP_H */