lockdep.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Runtime locking correctness validator
  4. *
  5. * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <[email protected]>
  6. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
  7. *
  8. * see Documentation/locking/lockdep-design.rst for more details.
  9. */
  10. #ifndef __LINUX_LOCKDEP_H
  11. #define __LINUX_LOCKDEP_H
  12. #include <linux/lockdep_types.h>
  13. #include <linux/smp.h>
  14. #include <asm/percpu.h>
  15. struct task_struct;
  16. #ifdef CONFIG_LOCKDEP
  17. #include <linux/linkage.h>
  18. #include <linux/list.h>
  19. #include <linux/debug_locks.h>
  20. #include <linux/stacktrace.h>
  21. static inline void lockdep_copy_map(struct lockdep_map *to,
  22. struct lockdep_map *from)
  23. {
  24. int i;
  25. *to = *from;
  26. /*
  27. * Since the class cache can be modified concurrently we could observe
  28. * half pointers (64bit arch using 32bit copy insns). Therefore clear
  29. * the caches and take the performance hit.
  30. *
  31. * XXX it doesn't work well with lockdep_set_class_and_subclass(), since
  32. * that relies on cache abuse.
  33. */
  34. for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
  35. to->class_cache[i] = NULL;
  36. }
  37. /*
  38. * Every lock has a list of other locks that were taken after it.
  39. * We only grow the list, never remove from it:
  40. */
  41. struct lock_list {
  42. struct list_head entry;
  43. struct lock_class *class;
  44. struct lock_class *links_to;
  45. const struct lock_trace *trace;
  46. u16 distance;
  47. /* bitmap of different dependencies from head to this */
  48. u8 dep;
  49. /* used by BFS to record whether "prev -> this" only has -(*R)-> */
  50. u8 only_xr;
  51. /*
  52. * The parent field is used to implement breadth-first search, and the
  53. * bit 0 is reused to indicate if the lock has been accessed in BFS.
  54. */
  55. struct lock_list *parent;
  56. };
  57. /**
  58. * struct lock_chain - lock dependency chain record
  59. *
  60. * @irq_context: the same as irq_context in held_lock below
  61. * @depth: the number of held locks in this chain
  62. * @base: the index in chain_hlocks for this chain
  63. * @entry: the collided lock chains in lock_chain hash list
  64. * @chain_key: the hash key of this lock_chain
  65. */
  66. struct lock_chain {
  67. /* see BUILD_BUG_ON()s in add_chain_cache() */
  68. unsigned int irq_context : 2,
  69. depth : 6,
  70. base : 24;
  71. /* 4 byte hole */
  72. struct hlist_node entry;
  73. u64 chain_key;
  74. };
  75. #define MAX_LOCKDEP_KEYS_BITS 13
  76. #define MAX_LOCKDEP_KEYS (1UL << MAX_LOCKDEP_KEYS_BITS)
  77. #define INITIAL_CHAIN_KEY -1
  78. struct held_lock {
  79. /*
  80. * One-way hash of the dependency chain up to this point. We
  81. * hash the hashes step by step as the dependency chain grows.
  82. *
  83. * We use it for dependency-caching and we skip detection
  84. * passes and dependency-updates if there is a cache-hit, so
  85. * it is absolutely critical for 100% coverage of the validator
  86. * to have a unique key value for every unique dependency path
  87. * that can occur in the system, to make a unique hash value
  88. * as likely as possible - hence the 64-bit width.
  89. *
  90. * The task struct holds the current hash value (initialized
  91. * with zero), here we store the previous hash value:
  92. */
  93. u64 prev_chain_key;
  94. unsigned long acquire_ip;
  95. struct lockdep_map *instance;
  96. struct lockdep_map *nest_lock;
  97. #ifdef CONFIG_LOCK_STAT
  98. u64 waittime_stamp;
  99. u64 holdtime_stamp;
  100. #endif
  101. /*
  102. * class_idx is zero-indexed; it points to the element in
  103. * lock_classes this held lock instance belongs to. class_idx is in
  104. * the range from 0 to (MAX_LOCKDEP_KEYS-1) inclusive.
  105. */
  106. unsigned int class_idx:MAX_LOCKDEP_KEYS_BITS;
  107. /*
  108. * The lock-stack is unified in that the lock chains of interrupt
  109. * contexts nest ontop of process context chains, but we 'separate'
  110. * the hashes by starting with 0 if we cross into an interrupt
  111. * context, and we also keep do not add cross-context lock
  112. * dependencies - the lock usage graph walking covers that area
  113. * anyway, and we'd just unnecessarily increase the number of
  114. * dependencies otherwise. [Note: hardirq and softirq contexts
  115. * are separated from each other too.]
  116. *
  117. * The following field is used to detect when we cross into an
  118. * interrupt context:
  119. */
  120. unsigned int irq_context:2; /* bit 0 - soft, bit 1 - hard */
  121. unsigned int trylock:1; /* 16 bits */
  122. unsigned int read:2; /* see lock_acquire() comment */
  123. unsigned int check:1; /* see lock_acquire() comment */
  124. unsigned int hardirqs_off:1;
  125. unsigned int references:12; /* 32 bits */
  126. unsigned int pin_count;
  127. };
  128. /*
  129. * Initialization, self-test and debugging-output methods:
  130. */
  131. extern void lockdep_init(void);
  132. extern void lockdep_reset(void);
  133. extern void lockdep_reset_lock(struct lockdep_map *lock);
  134. extern void lockdep_free_key_range(void *start, unsigned long size);
  135. extern asmlinkage void lockdep_sys_exit(void);
  136. extern void lockdep_set_selftest_task(struct task_struct *task);
  137. extern void lockdep_init_task(struct task_struct *task);
  138. /*
  139. * Split the recursion counter in two to readily detect 'off' vs recursion.
  140. */
  141. #define LOCKDEP_RECURSION_BITS 16
  142. #define LOCKDEP_OFF (1U << LOCKDEP_RECURSION_BITS)
  143. #define LOCKDEP_RECURSION_MASK (LOCKDEP_OFF - 1)
  144. /*
  145. * lockdep_{off,on}() are macros to avoid tracing and kprobes; not inlines due
  146. * to header dependencies.
  147. */
  148. #define lockdep_off() \
  149. do { \
  150. current->lockdep_recursion += LOCKDEP_OFF; \
  151. } while (0)
  152. #define lockdep_on() \
  153. do { \
  154. current->lockdep_recursion -= LOCKDEP_OFF; \
  155. } while (0)
  156. extern void lockdep_register_key(struct lock_class_key *key);
  157. extern void lockdep_unregister_key(struct lock_class_key *key);
  158. /*
  159. * These methods are used by specific locking variants (spinlocks,
  160. * rwlocks, mutexes and rwsems) to pass init/acquire/release events
  161. * to lockdep:
  162. */
  163. extern void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
  164. struct lock_class_key *key, int subclass, u8 inner, u8 outer, u8 lock_type);
  165. static inline void
  166. lockdep_init_map_waits(struct lockdep_map *lock, const char *name,
  167. struct lock_class_key *key, int subclass, u8 inner, u8 outer)
  168. {
  169. lockdep_init_map_type(lock, name, key, subclass, inner, outer, LD_LOCK_NORMAL);
  170. }
  171. static inline void
  172. lockdep_init_map_wait(struct lockdep_map *lock, const char *name,
  173. struct lock_class_key *key, int subclass, u8 inner)
  174. {
  175. lockdep_init_map_waits(lock, name, key, subclass, inner, LD_WAIT_INV);
  176. }
  177. static inline void lockdep_init_map(struct lockdep_map *lock, const char *name,
  178. struct lock_class_key *key, int subclass)
  179. {
  180. lockdep_init_map_wait(lock, name, key, subclass, LD_WAIT_INV);
  181. }
  182. /*
  183. * Reinitialize a lock key - for cases where there is special locking or
  184. * special initialization of locks so that the validator gets the scope
  185. * of dependencies wrong: they are either too broad (they need a class-split)
  186. * or they are too narrow (they suffer from a false class-split):
  187. */
  188. #define lockdep_set_class(lock, key) \
  189. lockdep_init_map_type(&(lock)->dep_map, #key, key, 0, \
  190. (lock)->dep_map.wait_type_inner, \
  191. (lock)->dep_map.wait_type_outer, \
  192. (lock)->dep_map.lock_type)
  193. #define lockdep_set_class_and_name(lock, key, name) \
  194. lockdep_init_map_type(&(lock)->dep_map, name, key, 0, \
  195. (lock)->dep_map.wait_type_inner, \
  196. (lock)->dep_map.wait_type_outer, \
  197. (lock)->dep_map.lock_type)
  198. #define lockdep_set_class_and_subclass(lock, key, sub) \
  199. lockdep_init_map_type(&(lock)->dep_map, #key, key, sub, \
  200. (lock)->dep_map.wait_type_inner, \
  201. (lock)->dep_map.wait_type_outer, \
  202. (lock)->dep_map.lock_type)
  203. #define lockdep_set_subclass(lock, sub) \
  204. lockdep_init_map_type(&(lock)->dep_map, #lock, (lock)->dep_map.key, sub,\
  205. (lock)->dep_map.wait_type_inner, \
  206. (lock)->dep_map.wait_type_outer, \
  207. (lock)->dep_map.lock_type)
  208. #define lockdep_set_novalidate_class(lock) \
  209. lockdep_set_class_and_name(lock, &__lockdep_no_validate__, #lock)
  210. /*
  211. * Compare locking classes
  212. */
  213. #define lockdep_match_class(lock, key) lockdep_match_key(&(lock)->dep_map, key)
  214. static inline int lockdep_match_key(struct lockdep_map *lock,
  215. struct lock_class_key *key)
  216. {
  217. return lock->key == key;
  218. }
  219. /*
  220. * Acquire a lock.
  221. *
  222. * Values for "read":
  223. *
  224. * 0: exclusive (write) acquire
  225. * 1: read-acquire (no recursion allowed)
  226. * 2: read-acquire with same-instance recursion allowed
  227. *
  228. * Values for check:
  229. *
  230. * 0: simple checks (freeing, held-at-exit-time, etc.)
  231. * 1: full validation
  232. */
  233. extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
  234. int trylock, int read, int check,
  235. struct lockdep_map *nest_lock, unsigned long ip);
  236. extern void lock_release(struct lockdep_map *lock, unsigned long ip);
  237. /* lock_is_held_type() returns */
  238. #define LOCK_STATE_UNKNOWN -1
  239. #define LOCK_STATE_NOT_HELD 0
  240. #define LOCK_STATE_HELD 1
  241. /*
  242. * Same "read" as for lock_acquire(), except -1 means any.
  243. */
  244. extern int lock_is_held_type(const struct lockdep_map *lock, int read);
  245. static inline int lock_is_held(const struct lockdep_map *lock)
  246. {
  247. return lock_is_held_type(lock, -1);
  248. }
  249. #define lockdep_is_held(lock) lock_is_held(&(lock)->dep_map)
  250. #define lockdep_is_held_type(lock, r) lock_is_held_type(&(lock)->dep_map, (r))
  251. extern void lock_set_class(struct lockdep_map *lock, const char *name,
  252. struct lock_class_key *key, unsigned int subclass,
  253. unsigned long ip);
  254. #define lock_set_novalidate_class(l, n, i) \
  255. lock_set_class(l, n, &__lockdep_no_validate__, 0, i)
  256. static inline void lock_set_subclass(struct lockdep_map *lock,
  257. unsigned int subclass, unsigned long ip)
  258. {
  259. lock_set_class(lock, lock->name, lock->key, subclass, ip);
  260. }
  261. extern void lock_downgrade(struct lockdep_map *lock, unsigned long ip);
  262. #define NIL_COOKIE (struct pin_cookie){ .val = 0U, }
  263. extern struct pin_cookie lock_pin_lock(struct lockdep_map *lock);
  264. extern void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie);
  265. extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
  266. #define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
  267. #define lockdep_assert(cond) \
  268. do { WARN_ON(debug_locks && !(cond)); } while (0)
  269. #define lockdep_assert_once(cond) \
  270. do { WARN_ON_ONCE(debug_locks && !(cond)); } while (0)
  271. #define lockdep_assert_held(l) \
  272. lockdep_assert(lockdep_is_held(l) != LOCK_STATE_NOT_HELD)
  273. #define lockdep_assert_not_held(l) \
  274. lockdep_assert(lockdep_is_held(l) != LOCK_STATE_HELD)
  275. #define lockdep_assert_held_write(l) \
  276. lockdep_assert(lockdep_is_held_type(l, 0))
  277. #define lockdep_assert_held_read(l) \
  278. lockdep_assert(lockdep_is_held_type(l, 1))
  279. #define lockdep_assert_held_once(l) \
  280. lockdep_assert_once(lockdep_is_held(l) != LOCK_STATE_NOT_HELD)
  281. #define lockdep_assert_none_held_once() \
  282. lockdep_assert_once(!current->lockdep_depth)
  283. #define lockdep_recursing(tsk) ((tsk)->lockdep_recursion)
  284. #define lockdep_pin_lock(l) lock_pin_lock(&(l)->dep_map)
  285. #define lockdep_repin_lock(l,c) lock_repin_lock(&(l)->dep_map, (c))
  286. #define lockdep_unpin_lock(l,c) lock_unpin_lock(&(l)->dep_map, (c))
  287. #else /* !CONFIG_LOCKDEP */
  288. static inline void lockdep_init_task(struct task_struct *task)
  289. {
  290. }
  291. static inline void lockdep_off(void)
  292. {
  293. }
  294. static inline void lockdep_on(void)
  295. {
  296. }
  297. static inline void lockdep_set_selftest_task(struct task_struct *task)
  298. {
  299. }
  300. # define lock_acquire(l, s, t, r, c, n, i) do { } while (0)
  301. # define lock_release(l, i) do { } while (0)
  302. # define lock_downgrade(l, i) do { } while (0)
  303. # define lock_set_class(l, n, key, s, i) do { (void)(key); } while (0)
  304. # define lock_set_novalidate_class(l, n, i) do { } while (0)
  305. # define lock_set_subclass(l, s, i) do { } while (0)
  306. # define lockdep_init() do { } while (0)
  307. # define lockdep_init_map_type(lock, name, key, sub, inner, outer, type) \
  308. do { (void)(name); (void)(key); } while (0)
  309. # define lockdep_init_map_waits(lock, name, key, sub, inner, outer) \
  310. do { (void)(name); (void)(key); } while (0)
  311. # define lockdep_init_map_wait(lock, name, key, sub, inner) \
  312. do { (void)(name); (void)(key); } while (0)
  313. # define lockdep_init_map(lock, name, key, sub) \
  314. do { (void)(name); (void)(key); } while (0)
  315. # define lockdep_set_class(lock, key) do { (void)(key); } while (0)
  316. # define lockdep_set_class_and_name(lock, key, name) \
  317. do { (void)(key); (void)(name); } while (0)
  318. #define lockdep_set_class_and_subclass(lock, key, sub) \
  319. do { (void)(key); } while (0)
  320. #define lockdep_set_subclass(lock, sub) do { } while (0)
  321. #define lockdep_set_novalidate_class(lock) do { } while (0)
  322. /*
  323. * We don't define lockdep_match_class() and lockdep_match_key() for !LOCKDEP
  324. * case since the result is not well defined and the caller should rather
  325. * #ifdef the call himself.
  326. */
  327. # define lockdep_reset() do { debug_locks = 1; } while (0)
  328. # define lockdep_free_key_range(start, size) do { } while (0)
  329. # define lockdep_sys_exit() do { } while (0)
  330. static inline void lockdep_register_key(struct lock_class_key *key)
  331. {
  332. }
  333. static inline void lockdep_unregister_key(struct lock_class_key *key)
  334. {
  335. }
  336. #define lockdep_depth(tsk) (0)
  337. /*
  338. * Dummy forward declarations, allow users to write less ifdef-y code
  339. * and depend on dead code elimination.
  340. */
  341. extern int lock_is_held(const void *);
  342. extern int lockdep_is_held(const void *);
  343. #define lockdep_is_held_type(l, r) (1)
  344. #define lockdep_assert(c) do { } while (0)
  345. #define lockdep_assert_once(c) do { } while (0)
  346. #define lockdep_assert_held(l) do { (void)(l); } while (0)
  347. #define lockdep_assert_not_held(l) do { (void)(l); } while (0)
  348. #define lockdep_assert_held_write(l) do { (void)(l); } while (0)
  349. #define lockdep_assert_held_read(l) do { (void)(l); } while (0)
  350. #define lockdep_assert_held_once(l) do { (void)(l); } while (0)
  351. #define lockdep_assert_none_held_once() do { } while (0)
  352. #define lockdep_recursing(tsk) (0)
  353. #define NIL_COOKIE (struct pin_cookie){ }
  354. #define lockdep_pin_lock(l) ({ struct pin_cookie cookie = { }; cookie; })
  355. #define lockdep_repin_lock(l, c) do { (void)(l); (void)(c); } while (0)
  356. #define lockdep_unpin_lock(l, c) do { (void)(l); (void)(c); } while (0)
  357. #endif /* !LOCKDEP */
  358. enum xhlock_context_t {
  359. XHLOCK_HARD,
  360. XHLOCK_SOFT,
  361. XHLOCK_CTX_NR,
  362. };
  363. #define lockdep_init_map_crosslock(m, n, k, s) do {} while (0)
  364. /*
  365. * To initialize a lockdep_map statically use this macro.
  366. * Note that _name must not be NULL.
  367. */
  368. #define STATIC_LOCKDEP_MAP_INIT(_name, _key) \
  369. { .name = (_name), .key = (void *)(_key), }
  370. static inline void lockdep_invariant_state(bool force) {}
  371. static inline void lockdep_free_task(struct task_struct *task) {}
  372. #ifdef CONFIG_LOCK_STAT
  373. extern void lock_contended(struct lockdep_map *lock, unsigned long ip);
  374. extern void lock_acquired(struct lockdep_map *lock, unsigned long ip);
  375. #define LOCK_CONTENDED(_lock, try, lock) \
  376. do { \
  377. if (!try(_lock)) { \
  378. lock_contended(&(_lock)->dep_map, _RET_IP_); \
  379. lock(_lock); \
  380. } \
  381. lock_acquired(&(_lock)->dep_map, _RET_IP_); \
  382. } while (0)
  383. #define LOCK_CONTENDED_RETURN(_lock, try, lock) \
  384. ({ \
  385. int ____err = 0; \
  386. if (!try(_lock)) { \
  387. lock_contended(&(_lock)->dep_map, _RET_IP_); \
  388. ____err = lock(_lock); \
  389. } \
  390. if (!____err) \
  391. lock_acquired(&(_lock)->dep_map, _RET_IP_); \
  392. ____err; \
  393. })
  394. #else /* CONFIG_LOCK_STAT */
  395. #define lock_contended(lockdep_map, ip) do {} while (0)
  396. #define lock_acquired(lockdep_map, ip) do {} while (0)
  397. #define LOCK_CONTENDED(_lock, try, lock) \
  398. lock(_lock)
  399. #define LOCK_CONTENDED_RETURN(_lock, try, lock) \
  400. lock(_lock)
  401. #endif /* CONFIG_LOCK_STAT */
  402. #ifdef CONFIG_PROVE_LOCKING
  403. extern void print_irqtrace_events(struct task_struct *curr);
  404. #else
  405. static inline void print_irqtrace_events(struct task_struct *curr)
  406. {
  407. }
  408. #endif
  409. /* Variable used to make lockdep treat read_lock() as recursive in selftests */
  410. #ifdef CONFIG_DEBUG_LOCKING_API_SELFTESTS
  411. extern unsigned int force_read_lock_recursive;
  412. #else /* CONFIG_DEBUG_LOCKING_API_SELFTESTS */
  413. #define force_read_lock_recursive 0
  414. #endif /* CONFIG_DEBUG_LOCKING_API_SELFTESTS */
  415. #ifdef CONFIG_LOCKDEP
  416. extern bool read_lock_is_recursive(void);
  417. #else /* CONFIG_LOCKDEP */
  418. /* If !LOCKDEP, the value is meaningless */
  419. #define read_lock_is_recursive() 0
  420. #endif
  421. /*
  422. * For trivial one-depth nesting of a lock-class, the following
  423. * global define can be used. (Subsystems with multiple levels
  424. * of nesting should define their own lock-nesting subclasses.)
  425. */
  426. #define SINGLE_DEPTH_NESTING 1
  427. /*
  428. * Map the dependency ops to NOP or to real lockdep ops, depending
  429. * on the per lock-class debug mode:
  430. */
  431. #define lock_acquire_exclusive(l, s, t, n, i) lock_acquire(l, s, t, 0, 1, n, i)
  432. #define lock_acquire_shared(l, s, t, n, i) lock_acquire(l, s, t, 1, 1, n, i)
  433. #define lock_acquire_shared_recursive(l, s, t, n, i) lock_acquire(l, s, t, 2, 1, n, i)
  434. #define spin_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
  435. #define spin_acquire_nest(l, s, t, n, i) lock_acquire_exclusive(l, s, t, n, i)
  436. #define spin_release(l, i) lock_release(l, i)
  437. #define rwlock_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
  438. #define rwlock_acquire_read(l, s, t, i) \
  439. do { \
  440. if (read_lock_is_recursive()) \
  441. lock_acquire_shared_recursive(l, s, t, NULL, i); \
  442. else \
  443. lock_acquire_shared(l, s, t, NULL, i); \
  444. } while (0)
  445. #define rwlock_release(l, i) lock_release(l, i)
  446. #define seqcount_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
  447. #define seqcount_acquire_read(l, s, t, i) lock_acquire_shared_recursive(l, s, t, NULL, i)
  448. #define seqcount_release(l, i) lock_release(l, i)
  449. #define mutex_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
  450. #define mutex_acquire_nest(l, s, t, n, i) lock_acquire_exclusive(l, s, t, n, i)
  451. #define mutex_release(l, i) lock_release(l, i)
  452. #define rwsem_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
  453. #define rwsem_acquire_nest(l, s, t, n, i) lock_acquire_exclusive(l, s, t, n, i)
  454. #define rwsem_acquire_read(l, s, t, i) lock_acquire_shared(l, s, t, NULL, i)
  455. #define rwsem_release(l, i) lock_release(l, i)
  456. #define lock_map_acquire(l) lock_acquire_exclusive(l, 0, 0, NULL, _THIS_IP_)
  457. #define lock_map_acquire_read(l) lock_acquire_shared_recursive(l, 0, 0, NULL, _THIS_IP_)
  458. #define lock_map_acquire_tryread(l) lock_acquire_shared_recursive(l, 0, 1, NULL, _THIS_IP_)
  459. #define lock_map_release(l) lock_release(l, _THIS_IP_)
  460. #ifdef CONFIG_PROVE_LOCKING
  461. # define might_lock(lock) \
  462. do { \
  463. typecheck(struct lockdep_map *, &(lock)->dep_map); \
  464. lock_acquire(&(lock)->dep_map, 0, 0, 0, 1, NULL, _THIS_IP_); \
  465. lock_release(&(lock)->dep_map, _THIS_IP_); \
  466. } while (0)
  467. # define might_lock_read(lock) \
  468. do { \
  469. typecheck(struct lockdep_map *, &(lock)->dep_map); \
  470. lock_acquire(&(lock)->dep_map, 0, 0, 1, 1, NULL, _THIS_IP_); \
  471. lock_release(&(lock)->dep_map, _THIS_IP_); \
  472. } while (0)
  473. # define might_lock_nested(lock, subclass) \
  474. do { \
  475. typecheck(struct lockdep_map *, &(lock)->dep_map); \
  476. lock_acquire(&(lock)->dep_map, subclass, 0, 1, 1, NULL, \
  477. _THIS_IP_); \
  478. lock_release(&(lock)->dep_map, _THIS_IP_); \
  479. } while (0)
  480. DECLARE_PER_CPU(int, hardirqs_enabled);
  481. DECLARE_PER_CPU(int, hardirq_context);
  482. DECLARE_PER_CPU(unsigned int, lockdep_recursion);
  483. #define __lockdep_enabled (debug_locks && !this_cpu_read(lockdep_recursion))
  484. #define lockdep_assert_irqs_enabled() \
  485. do { \
  486. WARN_ON_ONCE(__lockdep_enabled && !this_cpu_read(hardirqs_enabled)); \
  487. } while (0)
  488. #define lockdep_assert_irqs_disabled() \
  489. do { \
  490. WARN_ON_ONCE(__lockdep_enabled && this_cpu_read(hardirqs_enabled)); \
  491. } while (0)
  492. #define lockdep_assert_in_irq() \
  493. do { \
  494. WARN_ON_ONCE(__lockdep_enabled && !this_cpu_read(hardirq_context)); \
  495. } while (0)
  496. #define lockdep_assert_preemption_enabled() \
  497. do { \
  498. WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_COUNT) && \
  499. __lockdep_enabled && \
  500. (preempt_count() != 0 || \
  501. !this_cpu_read(hardirqs_enabled))); \
  502. } while (0)
  503. #define lockdep_assert_preemption_disabled() \
  504. do { \
  505. WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_COUNT) && \
  506. __lockdep_enabled && \
  507. (preempt_count() == 0 && \
  508. this_cpu_read(hardirqs_enabled))); \
  509. } while (0)
  510. /*
  511. * Acceptable for protecting per-CPU resources accessed from BH.
  512. * Much like in_softirq() - semantics are ambiguous, use carefully.
  513. */
  514. #define lockdep_assert_in_softirq() \
  515. do { \
  516. WARN_ON_ONCE(__lockdep_enabled && \
  517. (!in_softirq() || in_irq() || in_nmi())); \
  518. } while (0)
  519. #else
  520. # define might_lock(lock) do { } while (0)
  521. # define might_lock_read(lock) do { } while (0)
  522. # define might_lock_nested(lock, subclass) do { } while (0)
  523. # define lockdep_assert_irqs_enabled() do { } while (0)
  524. # define lockdep_assert_irqs_disabled() do { } while (0)
  525. # define lockdep_assert_in_irq() do { } while (0)
  526. # define lockdep_assert_preemption_enabled() do { } while (0)
  527. # define lockdep_assert_preemption_disabled() do { } while (0)
  528. # define lockdep_assert_in_softirq() do { } while (0)
  529. #endif
  530. #ifdef CONFIG_PROVE_RAW_LOCK_NESTING
  531. # define lockdep_assert_RT_in_threaded_ctx() do { \
  532. WARN_ONCE(debug_locks && !current->lockdep_recursion && \
  533. lockdep_hardirq_context() && \
  534. !(current->hardirq_threaded || current->irq_config), \
  535. "Not in threaded context on PREEMPT_RT as expected\n"); \
  536. } while (0)
  537. #else
  538. # define lockdep_assert_RT_in_threaded_ctx() do { } while (0)
  539. #endif
  540. #ifdef CONFIG_LOCKDEP
  541. void lockdep_rcu_suspicious(const char *file, const int line, const char *s);
  542. #else
  543. static inline void
  544. lockdep_rcu_suspicious(const char *file, const int line, const char *s)
  545. {
  546. }
  547. #endif
  548. #endif /* __LINUX_LOCKDEP_H */