lockdep.h 15 KB

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