lockdep.h 14 KB

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