lockdep.h 15 KB

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