lockdep.h 20 KB

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