lockdep.h 11 KB

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