lockdep.h 15 KB

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