lockdep.h 14 KB

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