blob: 20b6797babe2c40780c028ea16ffdd0d1095b658 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07002/*
3 * Runtime locking correctness validator
4 *
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -07005 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
Peter Zijlstra90eec102015-11-16 11:08:45 +01006 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07007 *
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -03008 * see Documentation/locking/lockdep-design.rst for more details.
Ingo Molnarfbb9ce952006-07-03 00:24:50 -07009 */
10#ifndef __LINUX_LOCKDEP_H
11#define __LINUX_LOCKDEP_H
12
Herbert Xuc935cd62020-06-17 17:17:19 +100013#include <linux/lockdep_types.h>
Peter Zijlstra0cd39f42020-08-06 14:35:11 +020014#include <linux/smp.h>
Peter Zijlstraa21ee602020-05-25 12:22:41 +020015#include <asm/percpu.h>
Herbert Xuc935cd62020-06-17 17:17:19 +100016
Heiko Carstensa1e96b02007-02-12 00:52:20 -080017struct task_struct;
18
Dave Young2edf5e42010-03-10 15:24:10 -080019/* for sysctl */
20extern int prove_locking;
21extern int lock_stat;
22
Michael S. Tsirkindb0b0ea2006-09-29 01:59:28 -070023#ifdef CONFIG_LOCKDEP
24
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070025#include <linux/linkage.h>
Herbert Xu5be542e2020-07-16 16:36:50 +100026#include <linux/list.h>
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070027#include <linux/debug_locks.h>
28#include <linux/stacktrace.h>
29
Peter Zijlstra4d82a1d2012-05-15 08:06:19 -070030static inline void lockdep_copy_map(struct lockdep_map *to,
31 struct lockdep_map *from)
32{
33 int i;
34
35 *to = *from;
36 /*
37 * Since the class cache can be modified concurrently we could observe
38 * half pointers (64bit arch using 32bit copy insns). Therefore clear
39 * the caches and take the performance hit.
40 *
41 * XXX it doesn't work well with lockdep_set_class_and_subclass(), since
42 * that relies on cache abuse.
43 */
44 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
45 to->class_cache[i] = NULL;
46}
47
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070048/*
49 * Every lock has a list of other locks that were taken after it.
50 * We only grow the list, never remove from it:
51 */
52struct lock_list {
53 struct list_head entry;
54 struct lock_class *class;
Bart Van Assche86cffb82019-02-14 15:00:41 -080055 struct lock_class *links_to;
Bart Van Assche12593b72019-07-22 11:24:42 -070056 const struct lock_trace *trace;
Boqun Fengbd76eca2020-08-07 15:42:24 +080057 u16 distance;
Boqun Feng3454a362020-08-07 15:42:25 +080058 /* bitmap of different dependencies from head to this */
59 u8 dep;
Boqun Feng6971c0f2020-08-07 15:42:26 +080060 /* used by BFS to record whether "prev -> this" only has -(*R)-> */
61 u8 only_xr;
Ming Leic94aa5c2009-07-16 15:44:29 +020062
Peter Zijlstraaf012962009-07-16 15:44:29 +020063 /*
64 * The parent field is used to implement breadth-first search, and the
65 * bit 0 is reused to indicate if the lock has been accessed in BFS.
Ming Leic94aa5c2009-07-16 15:44:29 +020066 */
67 struct lock_list *parent;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070068};
69
Yuyang Dud16dbd12019-05-06 16:19:22 +080070/**
71 * struct lock_chain - lock dependency chain record
72 *
73 * @irq_context: the same as irq_context in held_lock below
74 * @depth: the number of held locks in this chain
75 * @base: the index in chain_hlocks for this chain
76 * @entry: the collided lock chains in lock_chain hash list
77 * @chain_key: the hash key of this lock_chain
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070078 */
79struct lock_chain {
Yuyang Dud16dbd12019-05-06 16:19:22 +080080 /* see BUILD_BUG_ON()s in add_chain_cache() */
Peter Zijlstra75dd6022016-03-30 11:36:59 +020081 unsigned int irq_context : 2,
82 depth : 6,
83 base : 24;
84 /* 4 byte hole */
Andrew Mortona63f38c2016-02-03 13:44:12 -080085 struct hlist_node entry;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070086 u64 chain_key;
87};
88
Ingo Molnare5f363e32008-08-11 12:37:27 +020089#define MAX_LOCKDEP_KEYS_BITS 13
Yuyang Du01bb6f02019-05-06 16:19:25 +080090#define MAX_LOCKDEP_KEYS (1UL << MAX_LOCKDEP_KEYS_BITS)
91#define INITIAL_CHAIN_KEY -1
Dave Jonesf82b2172008-08-11 09:30:23 +020092
Ingo Molnarfbb9ce952006-07-03 00:24:50 -070093struct held_lock {
94 /*
95 * One-way hash of the dependency chain up to this point. We
96 * hash the hashes step by step as the dependency chain grows.
97 *
98 * We use it for dependency-caching and we skip detection
99 * passes and dependency-updates if there is a cache-hit, so
100 * it is absolutely critical for 100% coverage of the validator
101 * to have a unique key value for every unique dependency path
102 * that can occur in the system, to make a unique hash value
103 * as likely as possible - hence the 64-bit width.
104 *
105 * The task struct holds the current hash value (initialized
106 * with zero), here we store the previous hash value:
107 */
108 u64 prev_chain_key;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700109 unsigned long acquire_ip;
110 struct lockdep_map *instance;
Peter Zijlstra7531e2f2008-08-11 09:30:24 +0200111 struct lockdep_map *nest_lock;
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700112#ifdef CONFIG_LOCK_STAT
113 u64 waittime_stamp;
114 u64 holdtime_stamp;
115#endif
Yuyang Du01bb6f02019-05-06 16:19:25 +0800116 /*
117 * class_idx is zero-indexed; it points to the element in
118 * lock_classes this held lock instance belongs to. class_idx is in
119 * the range from 0 to (MAX_LOCKDEP_KEYS-1) inclusive.
120 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200121 unsigned int class_idx:MAX_LOCKDEP_KEYS_BITS;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700122 /*
123 * The lock-stack is unified in that the lock chains of interrupt
124 * contexts nest ontop of process context chains, but we 'separate'
125 * the hashes by starting with 0 if we cross into an interrupt
126 * context, and we also keep do not add cross-context lock
127 * dependencies - the lock usage graph walking covers that area
128 * anyway, and we'd just unnecessarily increase the number of
129 * dependencies otherwise. [Note: hardirq and softirq contexts
130 * are separated from each other too.]
131 *
132 * The following field is used to detect when we cross into an
133 * interrupt context:
134 */
Dave Jonesf82b2172008-08-11 09:30:23 +0200135 unsigned int irq_context:2; /* bit 0 - soft, bit 1 - hard */
Peter Zijlstrabb97a912009-07-20 19:15:35 +0200136 unsigned int trylock:1; /* 16 bits */
137
Dave Jonesf82b2172008-08-11 09:30:23 +0200138 unsigned int read:2; /* see lock_acquire() comment */
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +0100139 unsigned int check:1; /* see lock_acquire() comment */
Dave Jonesf82b2172008-08-11 09:30:23 +0200140 unsigned int hardirqs_off:1;
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +0100141 unsigned int references:12; /* 32 bits */
Peter Zijlstraa24fc602015-06-11 14:46:53 +0200142 unsigned int pin_count;
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700143};
144
145/*
146 * Initialization, self-test and debugging-output methods:
147 */
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -0700148extern void lockdep_init(void);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700149extern void lockdep_reset(void);
150extern void lockdep_reset_lock(struct lockdep_map *lock);
151extern void lockdep_free_key_range(void *start, unsigned long size);
Andi Kleen63f9a7f2014-02-08 08:52:01 +0100152extern asmlinkage void lockdep_sys_exit(void);
Bart Van Asschecdc84d72019-02-14 15:00:44 -0800153extern void lockdep_set_selftest_task(struct task_struct *task);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700154
Yuyang Due196e472019-05-06 16:19:23 +0800155extern void lockdep_init_task(struct task_struct *task);
156
Peter Zijlstrae616cb82020-02-24 22:14:51 +0100157/*
158 * Split the recrursion counter in two to readily detect 'off' vs recursion.
159 */
160#define LOCKDEP_RECURSION_BITS 16
161#define LOCKDEP_OFF (1U << LOCKDEP_RECURSION_BITS)
162#define LOCKDEP_RECURSION_MASK (LOCKDEP_OFF - 1)
163
164/*
165 * lockdep_{off,on}() are macros to avoid tracing and kprobes; not inlines due
166 * to header dependencies.
167 */
168
169#define lockdep_off() \
170do { \
171 current->lockdep_recursion += LOCKDEP_OFF; \
172} while (0)
173
174#define lockdep_on() \
175do { \
176 current->lockdep_recursion -= LOCKDEP_OFF; \
177} while (0)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700178
Bart Van Assche108c1482019-02-14 15:00:53 -0800179extern void lockdep_register_key(struct lock_class_key *key);
180extern void lockdep_unregister_key(struct lock_class_key *key);
181
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700182/*
183 * These methods are used by specific locking variants (spinlocks,
184 * rwlocks, mutexes and rwsems) to pass init/acquire/release events
185 * to lockdep:
186 */
187
Peter Zijlstrad5462a62020-12-09 16:06:21 +0100188extern void lockdep_init_map_type(struct lockdep_map *lock, const char *name,
189 struct lock_class_key *key, int subclass, u8 inner, u8 outer, u8 lock_type);
190
191static inline void
192lockdep_init_map_waits(struct lockdep_map *lock, const char *name,
193 struct lock_class_key *key, int subclass, u8 inner, u8 outer)
194{
195 lockdep_init_map_type(lock, name, key, subclass, inner, LD_WAIT_INV, LD_LOCK_NORMAL);
196}
Peter Zijlstrade8f5e42020-03-21 12:26:01 +0100197
198static inline void
199lockdep_init_map_wait(struct lockdep_map *lock, const char *name,
Peter Zijlstrad5462a62020-12-09 16:06:21 +0100200 struct lock_class_key *key, int subclass, u8 inner)
Peter Zijlstrade8f5e42020-03-21 12:26:01 +0100201{
202 lockdep_init_map_waits(lock, name, key, subclass, inner, LD_WAIT_INV);
203}
204
205static inline void lockdep_init_map(struct lockdep_map *lock, const char *name,
206 struct lock_class_key *key, int subclass)
207{
208 lockdep_init_map_wait(lock, name, key, subclass, LD_WAIT_INV);
209}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700210
211/*
212 * Reinitialize a lock key - for cases where there is special locking or
213 * special initialization of locks so that the validator gets the scope
214 * of dependencies wrong: they are either too broad (they need a class-split)
215 * or they are too narrow (they suffer from a false class-split):
216 */
Peter Zijlstrade8f5e42020-03-21 12:26:01 +0100217#define lockdep_set_class(lock, key) \
218 lockdep_init_map_waits(&(lock)->dep_map, #key, key, 0, \
219 (lock)->dep_map.wait_type_inner, \
220 (lock)->dep_map.wait_type_outer)
221
222#define lockdep_set_class_and_name(lock, key, name) \
223 lockdep_init_map_waits(&(lock)->dep_map, name, key, 0, \
224 (lock)->dep_map.wait_type_inner, \
225 (lock)->dep_map.wait_type_outer)
226
227#define lockdep_set_class_and_subclass(lock, key, sub) \
228 lockdep_init_map_waits(&(lock)->dep_map, #key, key, sub,\
229 (lock)->dep_map.wait_type_inner, \
230 (lock)->dep_map.wait_type_outer)
231
232#define lockdep_set_subclass(lock, sub) \
233 lockdep_init_map_waits(&(lock)->dep_map, #lock, (lock)->dep_map.key, sub,\
234 (lock)->dep_map.wait_type_inner, \
235 (lock)->dep_map.wait_type_outer)
Peter Zijlstra1704f472010-03-19 01:37:42 +0100236
237#define lockdep_set_novalidate_class(lock) \
Oleg Nesterov47be1c12014-01-20 19:20:16 +0100238 lockdep_set_class_and_name(lock, &__lockdep_no_validate__, #lock)
Peter Zijlstrade8f5e42020-03-21 12:26:01 +0100239
Jan Kara9a7aa122009-06-04 15:26:49 +0200240/*
241 * Compare locking classes
242 */
243#define lockdep_match_class(lock, key) lockdep_match_key(&(lock)->dep_map, key)
244
245static inline int lockdep_match_key(struct lockdep_map *lock,
246 struct lock_class_key *key)
247{
248 return lock->key == key;
249}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700250
251/*
252 * Acquire a lock.
253 *
254 * Values for "read":
255 *
256 * 0: exclusive (write) acquire
257 * 1: read-acquire (no recursion allowed)
258 * 2: read-acquire with same-instance recursion allowed
259 *
260 * Values for check:
261 *
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +0100262 * 0: simple checks (freeing, held-at-exit-time, etc.)
263 * 1: full validation
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700264 */
265extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
Peter Zijlstra7531e2f2008-08-11 09:30:24 +0200266 int trylock, int read, int check,
267 struct lockdep_map *nest_lock, unsigned long ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700268
Qian Cai5facae42019-09-19 12:09:40 -0400269extern void lock_release(struct lockdep_map *lock, unsigned long ip);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700270
Peter Zijlstraf8319482016-11-30 14:32:25 +1100271/*
272 * Same "read" as for lock_acquire(), except -1 means any.
273 */
Matthew Wilcox08f36ff2018-01-17 07:14:13 -0800274extern int lock_is_held_type(const struct lockdep_map *lock, int read);
Peter Zijlstraf607c662009-07-20 19:16:29 +0200275
Matthew Wilcox08f36ff2018-01-17 07:14:13 -0800276static inline int lock_is_held(const struct lockdep_map *lock)
Peter Zijlstraf8319482016-11-30 14:32:25 +1100277{
278 return lock_is_held_type(lock, -1);
279}
280
281#define lockdep_is_held(lock) lock_is_held(&(lock)->dep_map)
282#define lockdep_is_held_type(lock, r) lock_is_held_type(&(lock)->dep_map, (r))
Peter Zijlstraf607c662009-07-20 19:16:29 +0200283
Peter Zijlstra00ef9f72008-12-04 09:00:17 +0100284extern void lock_set_class(struct lockdep_map *lock, const char *name,
285 struct lock_class_key *key, unsigned int subclass,
286 unsigned long ip);
287
288static 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}
Peter Zijlstra64aa3482008-08-11 09:30:21 +0200293
J. R. Okajima6419c4a2017-02-03 01:38:17 +0900294extern void lock_downgrade(struct lockdep_map *lock, unsigned long ip);
295
Peter Zijlstrae7904a22015-08-01 19:25:08 +0200296#define NIL_COOKIE (struct pin_cookie){ .val = 0U, }
297
298extern struct pin_cookie lock_pin_lock(struct lockdep_map *lock);
299extern void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie);
300extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
Peter Zijlstraa24fc602015-06-11 14:46:53 +0200301
Jarek Poplawskie3a55fd2007-03-22 00:11:26 -0800302#define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800303
Johannes Bergb1ae3452013-02-21 16:42:47 -0800304#define lockdep_assert_held(l) do { \
305 WARN_ON(debug_locks && !lockdep_is_held(l)); \
306 } while (0)
Peter Zijlstraf607c662009-07-20 19:16:29 +0200307
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300308#define lockdep_assert_held_write(l) do { \
Peter Zijlstraf8319482016-11-30 14:32:25 +1100309 WARN_ON(debug_locks && !lockdep_is_held_type(l, 0)); \
310 } while (0)
311
312#define lockdep_assert_held_read(l) do { \
313 WARN_ON(debug_locks && !lockdep_is_held_type(l, 1)); \
314 } while (0)
315
Peter Hurley9a371102014-09-10 14:31:39 -0400316#define lockdep_assert_held_once(l) do { \
317 WARN_ON_ONCE(debug_locks && !lockdep_is_held(l)); \
318 } while (0)
319
Peter Zijlstra94d24fc2011-06-07 11:17:30 +0200320#define lockdep_recursing(tsk) ((tsk)->lockdep_recursion)
321
Peter Zijlstrae7904a22015-08-01 19:25:08 +0200322#define lockdep_pin_lock(l) lock_pin_lock(&(l)->dep_map)
323#define lockdep_repin_lock(l,c) lock_repin_lock(&(l)->dep_map, (c))
324#define lockdep_unpin_lock(l,c) lock_unpin_lock(&(l)->dep_map, (c))
Peter Zijlstraa24fc602015-06-11 14:46:53 +0200325
Michel Lespinassea51805e2013-07-08 14:23:49 -0700326#else /* !CONFIG_LOCKDEP */
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700327
Yuyang Due196e472019-05-06 16:19:23 +0800328static inline void lockdep_init_task(struct task_struct *task)
329{
330}
331
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700332static inline void lockdep_off(void)
333{
334}
335
336static inline void lockdep_on(void)
337{
338}
339
Bart Van Asschecdc84d72019-02-14 15:00:44 -0800340static inline void lockdep_set_selftest_task(struct task_struct *task)
341{
342}
343
Peter Zijlstra7531e2f2008-08-11 09:30:24 +0200344# define lock_acquire(l, s, t, r, c, n, i) do { } while (0)
Qian Cai5facae42019-09-19 12:09:40 -0400345# define lock_release(l, i) do { } while (0)
J. R. Okajima6419c4a2017-02-03 01:38:17 +0900346# define lock_downgrade(l, i) do { } while (0)
Peter Zijlstra00ef9f72008-12-04 09:00:17 +0100347# define lock_set_class(l, n, k, s, i) do { } while (0)
Peter Zijlstra64aa3482008-08-11 09:30:21 +0200348# define lock_set_subclass(l, s, i) do { } while (0)
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -0700349# define lockdep_init() do { } while (0)
Peter Zijlstrad5462a62020-12-09 16:06:21 +0100350# define lockdep_init_map_type(lock, name, key, sub, inner, outer, type) \
351 do { (void)(name); (void)(key); } while (0)
Peter Zijlstrade8f5e42020-03-21 12:26:01 +0100352# define lockdep_init_map_waits(lock, name, key, sub, inner, outer) \
353 do { (void)(name); (void)(key); } while (0)
354# define lockdep_init_map_wait(lock, name, key, sub, inner) \
355 do { (void)(name); (void)(key); } while (0)
Ingo Molnare25cf3d2008-10-17 15:55:07 +0200356# define lockdep_init_map(lock, name, key, sub) \
357 do { (void)(name); (void)(key); } while (0)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700358# define lockdep_set_class(lock, key) do { (void)(key); } while (0)
359# define lockdep_set_class_and_name(lock, key, name) \
Ingo Molnare25cf3d2008-10-17 15:55:07 +0200360 do { (void)(key); (void)(name); } while (0)
Peter Zijlstra4dfbb9d2006-10-11 01:45:14 -0400361#define lockdep_set_class_and_subclass(lock, key, sub) \
362 do { (void)(key); } while (0)
Andrew Morton07646e22006-10-11 23:45:23 -0400363#define lockdep_set_subclass(lock, sub) do { } while (0)
Peter Zijlstra1704f472010-03-19 01:37:42 +0100364
365#define lockdep_set_novalidate_class(lock) do { } while (0)
366
Jan Kara9a7aa122009-06-04 15:26:49 +0200367/*
368 * We don't define lockdep_match_class() and lockdep_match_key() for !LOCKDEP
369 * case since the result is not well defined and the caller should rather
370 * #ifdef the call himself.
371 */
Andrew Morton07646e22006-10-11 23:45:23 -0400372
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700373# define lockdep_reset() do { debug_locks = 1; } while (0)
374# define lockdep_free_key_range(start, size) do { } while (0)
Peter Zijlstrab351d162007-10-11 22:11:12 +0200375# define lockdep_sys_exit() do { } while (0)
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800376
Bart Van Assche108c1482019-02-14 15:00:53 -0800377static inline void lockdep_register_key(struct lock_class_key *key)
378{
379}
380
381static inline void lockdep_unregister_key(struct lock_class_key *key)
382{
383}
384
Peter Zijlstrad5abe662006-12-06 20:37:26 -0800385#define lockdep_depth(tsk) (0)
386
Peter Zijlstraf8319482016-11-30 14:32:25 +1100387#define lockdep_is_held_type(l, r) (1)
388
Paul Bolle5cd3f5a2013-01-24 21:53:17 +0100389#define lockdep_assert_held(l) do { (void)(l); } while (0)
Nikolay Borisov9ffbe8a2019-05-31 13:06:51 +0300390#define lockdep_assert_held_write(l) do { (void)(l); } while (0)
Peter Zijlstraf8319482016-11-30 14:32:25 +1100391#define lockdep_assert_held_read(l) do { (void)(l); } while (0)
Peter Hurley9a371102014-09-10 14:31:39 -0400392#define lockdep_assert_held_once(l) do { (void)(l); } while (0)
Peter Zijlstraf607c662009-07-20 19:16:29 +0200393
Peter Zijlstra94d24fc2011-06-07 11:17:30 +0200394#define lockdep_recursing(tsk) (0)
395
Peter Zijlstrae7904a22015-08-01 19:25:08 +0200396#define NIL_COOKIE (struct pin_cookie){ }
397
Arnd Bergmann3771b0f2019-03-25 13:57:57 +0100398#define lockdep_pin_lock(l) ({ struct pin_cookie cookie = { }; cookie; })
Peter Zijlstrae7904a22015-08-01 19:25:08 +0200399#define lockdep_repin_lock(l, c) do { (void)(l); (void)(c); } while (0)
400#define lockdep_unpin_lock(l, c) do { (void)(l); (void)(c); } while (0)
Peter Zijlstraa24fc602015-06-11 14:46:53 +0200401
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700402#endif /* !LOCKDEP */
403
Byungchul Parkb09be672017-08-07 16:12:52 +0900404enum xhlock_context_t {
405 XHLOCK_HARD,
406 XHLOCK_SOFT,
Byungchul Parkb09be672017-08-07 16:12:52 +0900407 XHLOCK_CTX_NR,
408};
409
Boqun Feng52fa5bc2017-08-17 17:46:12 +0800410#define lockdep_init_map_crosslock(m, n, k, s) do {} while (0)
Byungchul Parkb09be672017-08-07 16:12:52 +0900411/*
412 * To initialize a lockdep_map statically use this macro.
413 * Note that _name must not be NULL.
414 */
415#define STATIC_LOCKDEP_MAP_INIT(_name, _key) \
416 { .name = (_name), .key = (void *)(_key), }
417
Peter Zijlstraf52be572017-08-29 10:59:39 +0200418static inline void lockdep_invariant_state(bool force) {}
Byungchul Parkb09be672017-08-07 16:12:52 +0900419static inline void lockdep_free_task(struct task_struct *task) {}
Byungchul Parkb09be672017-08-07 16:12:52 +0900420
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700421#ifdef CONFIG_LOCK_STAT
422
423extern void lock_contended(struct lockdep_map *lock, unsigned long ip);
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200424extern void lock_acquired(struct lockdep_map *lock, unsigned long ip);
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700425
426#define LOCK_CONTENDED(_lock, try, lock) \
427do { \
428 if (!try(_lock)) { \
429 lock_contended(&(_lock)->dep_map, _RET_IP_); \
430 lock(_lock); \
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700431 } \
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200432 lock_acquired(&(_lock)->dep_map, _RET_IP_); \
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700433} while (0)
434
Michal Hocko916633a2016-04-07 17:12:31 +0200435#define LOCK_CONTENDED_RETURN(_lock, try, lock) \
436({ \
437 int ____err = 0; \
438 if (!try(_lock)) { \
439 lock_contended(&(_lock)->dep_map, _RET_IP_); \
440 ____err = lock(_lock); \
441 } \
442 if (!____err) \
443 lock_acquired(&(_lock)->dep_map, _RET_IP_); \
444 ____err; \
445})
446
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700447#else /* CONFIG_LOCK_STAT */
448
449#define lock_contended(lockdep_map, ip) do {} while (0)
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200450#define lock_acquired(lockdep_map, ip) do {} while (0)
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700451
452#define LOCK_CONTENDED(_lock, try, lock) \
453 lock(_lock)
454
Michal Hocko916633a2016-04-07 17:12:31 +0200455#define LOCK_CONTENDED_RETURN(_lock, try, lock) \
456 lock(_lock)
457
Peter Zijlstraf20786f2007-07-19 01:48:56 -0700458#endif /* CONFIG_LOCK_STAT */
459
Robin Holte8c158b2009-04-02 16:59:45 -0700460#ifdef CONFIG_LOCKDEP
461
462/*
463 * On lockdep we dont want the hand-coded irq-enable of
464 * _raw_*_lock_flags() code, because lockdep assumes
465 * that interrupts are not re-enabled during lock-acquire:
466 */
467#define LOCK_CONTENDED_FLAGS(_lock, try, lock, lockfl, flags) \
468 LOCK_CONTENDED((_lock), (try), (lock))
469
470#else /* CONFIG_LOCKDEP */
471
472#define LOCK_CONTENDED_FLAGS(_lock, try, lock, lockfl, flags) \
473 lockfl((_lock), (flags))
474
475#endif /* CONFIG_LOCKDEP */
476
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -0700477#ifdef CONFIG_PROVE_LOCKING
Ingo Molnar3117df02006-12-13 00:34:43 -0800478extern void print_irqtrace_events(struct task_struct *curr);
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700479#else
Ingo Molnar3117df02006-12-13 00:34:43 -0800480static inline void print_irqtrace_events(struct task_struct *curr)
481{
482}
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700483#endif
484
Boqun Fenge9181882020-08-07 15:42:20 +0800485/* Variable used to make lockdep treat read_lock() as recursive in selftests */
486#ifdef CONFIG_DEBUG_LOCKING_API_SELFTESTS
487extern unsigned int force_read_lock_recursive;
488#else /* CONFIG_DEBUG_LOCKING_API_SELFTESTS */
489#define force_read_lock_recursive 0
490#endif /* CONFIG_DEBUG_LOCKING_API_SELFTESTS */
491
492#ifdef CONFIG_LOCKDEP
493extern bool read_lock_is_recursive(void);
494#else /* CONFIG_LOCKDEP */
495/* If !LOCKDEP, the value is meaningless */
496#define read_lock_is_recursive() 0
497#endif
498
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700499/*
500 * For trivial one-depth nesting of a lock-class, the following
501 * global define can be used. (Subsystems with multiple levels
502 * of nesting should define their own lock-nesting subclasses.)
503 */
504#define SINGLE_DEPTH_NESTING 1
505
506/*
507 * Map the dependency ops to NOP or to real lockdep ops, depending
508 * on the per lock-class debug mode:
509 */
510
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +0100511#define lock_acquire_exclusive(l, s, t, n, i) lock_acquire(l, s, t, 0, 1, n, i)
512#define lock_acquire_shared(l, s, t, n, i) lock_acquire(l, s, t, 1, 1, n, i)
513#define lock_acquire_shared_recursive(l, s, t, n, i) lock_acquire(l, s, t, 2, 1, n, i)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700514
Michel Lespinassea51805e2013-07-08 14:23:49 -0700515#define spin_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
516#define spin_acquire_nest(l, s, t, n, i) lock_acquire_exclusive(l, s, t, n, i)
Qian Cai5facae42019-09-19 12:09:40 -0400517#define spin_release(l, i) lock_release(l, i)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700518
Michel Lespinassea51805e2013-07-08 14:23:49 -0700519#define rwlock_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
Boqun Fenge9181882020-08-07 15:42:20 +0800520#define rwlock_acquire_read(l, s, t, i) \
521do { \
522 if (read_lock_is_recursive()) \
523 lock_acquire_shared_recursive(l, s, t, NULL, i); \
524 else \
525 lock_acquire_shared(l, s, t, NULL, i); \
526} while (0)
527
Qian Cai5facae42019-09-19 12:09:40 -0400528#define rwlock_release(l, i) lock_release(l, i)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700529
John Stultz1ca7d672013-10-07 15:51:59 -0700530#define seqcount_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
531#define seqcount_acquire_read(l, s, t, i) lock_acquire_shared_recursive(l, s, t, NULL, i)
Qian Cai5facae42019-09-19 12:09:40 -0400532#define seqcount_release(l, i) lock_release(l, i)
John Stultz1ca7d672013-10-07 15:51:59 -0700533
Michel Lespinassea51805e2013-07-08 14:23:49 -0700534#define mutex_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
535#define mutex_acquire_nest(l, s, t, n, i) lock_acquire_exclusive(l, s, t, n, i)
Qian Cai5facae42019-09-19 12:09:40 -0400536#define mutex_release(l, i) lock_release(l, i)
Michel Lespinassea51805e2013-07-08 14:23:49 -0700537
538#define rwsem_acquire(l, s, t, i) lock_acquire_exclusive(l, s, t, NULL, i)
539#define rwsem_acquire_nest(l, s, t, n, i) lock_acquire_exclusive(l, s, t, n, i)
540#define rwsem_acquire_read(l, s, t, i) lock_acquire_shared(l, s, t, NULL, i)
Qian Cai5facae42019-09-19 12:09:40 -0400541#define rwsem_release(l, i) lock_release(l, i)
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700542
Michel Lespinassea51805e2013-07-08 14:23:49 -0700543#define lock_map_acquire(l) lock_acquire_exclusive(l, 0, 0, NULL, _THIS_IP_)
544#define lock_map_acquire_read(l) lock_acquire_shared_recursive(l, 0, 0, NULL, _THIS_IP_)
Paul E. McKenneydd56af42014-08-25 20:25:06 -0700545#define lock_map_acquire_tryread(l) lock_acquire_shared_recursive(l, 0, 1, NULL, _THIS_IP_)
Qian Cai5facae42019-09-19 12:09:40 -0400546#define lock_map_release(l) lock_release(l, _THIS_IP_)
Peter Zijlstra4f3e7522008-08-11 09:30:23 +0200547
Peter Zijlstra76b189e2008-09-10 09:57:35 +0200548#ifdef CONFIG_PROVE_LOCKING
Peter Zijlstrabaffd722020-10-05 09:56:57 +0200549# define might_lock(lock) \
Peter Zijlstra76b189e2008-09-10 09:57:35 +0200550do { \
551 typecheck(struct lockdep_map *, &(lock)->dep_map); \
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +0100552 lock_acquire(&(lock)->dep_map, 0, 0, 0, 1, NULL, _THIS_IP_); \
Qian Cai5facae42019-09-19 12:09:40 -0400553 lock_release(&(lock)->dep_map, _THIS_IP_); \
Peter Zijlstra76b189e2008-09-10 09:57:35 +0200554} while (0)
Peter Zijlstrabaffd722020-10-05 09:56:57 +0200555# define might_lock_read(lock) \
Peter Zijlstra76b189e2008-09-10 09:57:35 +0200556do { \
557 typecheck(struct lockdep_map *, &(lock)->dep_map); \
Oleg Nesterovfb9edbe2014-01-20 19:20:06 +0100558 lock_acquire(&(lock)->dep_map, 0, 0, 1, 1, NULL, _THIS_IP_); \
Qian Cai5facae42019-09-19 12:09:40 -0400559 lock_release(&(lock)->dep_map, _THIS_IP_); \
Peter Zijlstra76b189e2008-09-10 09:57:35 +0200560} while (0)
Peter Zijlstrabaffd722020-10-05 09:56:57 +0200561# define might_lock_nested(lock, subclass) \
Daniel Vettere692b402019-11-04 18:37:19 +0100562do { \
563 typecheck(struct lockdep_map *, &(lock)->dep_map); \
564 lock_acquire(&(lock)->dep_map, subclass, 0, 1, 1, NULL, \
565 _THIS_IP_); \
Jani Nikula023265e2019-12-11 10:35:37 +0200566 lock_release(&(lock)->dep_map, _THIS_IP_); \
Daniel Vettere692b402019-11-04 18:37:19 +0100567} while (0)
Frederic Weisbeckerf54bb2e2017-11-06 16:01:17 +0100568
Peter Zijlstraa21ee602020-05-25 12:22:41 +0200569DECLARE_PER_CPU(int, hardirqs_enabled);
570DECLARE_PER_CPU(int, hardirq_context);
Peter Zijlstra4d004092020-10-02 11:04:21 +0200571DECLARE_PER_CPU(unsigned int, lockdep_recursion);
Frederic Weisbeckerf54bb2e2017-11-06 16:01:17 +0100572
Peter Zijlstrabaffd722020-10-05 09:56:57 +0200573#define __lockdep_enabled (debug_locks && !this_cpu_read(lockdep_recursion))
Peter Zijlstrafddf9052020-08-20 09:13:30 +0200574
Peter Zijlstraa21ee602020-05-25 12:22:41 +0200575#define lockdep_assert_irqs_enabled() \
576do { \
Peter Zijlstrabaffd722020-10-05 09:56:57 +0200577 WARN_ON_ONCE(__lockdep_enabled && !this_cpu_read(hardirqs_enabled)); \
Peter Zijlstraa21ee602020-05-25 12:22:41 +0200578} while (0)
Frederic Weisbeckerf54bb2e2017-11-06 16:01:17 +0100579
Peter Zijlstraa21ee602020-05-25 12:22:41 +0200580#define lockdep_assert_irqs_disabled() \
581do { \
Peter Zijlstrabaffd722020-10-05 09:56:57 +0200582 WARN_ON_ONCE(__lockdep_enabled && this_cpu_read(hardirqs_enabled)); \
Peter Zijlstraa21ee602020-05-25 12:22:41 +0200583} while (0)
584
585#define lockdep_assert_in_irq() \
586do { \
Peter Zijlstrabaffd722020-10-05 09:56:57 +0200587 WARN_ON_ONCE(__lockdep_enabled && !this_cpu_read(hardirq_context)); \
Peter Zijlstraa21ee602020-05-25 12:22:41 +0200588} while (0)
Joel Fernandes (Google)71d8d152019-03-26 15:24:08 -0400589
Ahmed S. Darwish8fd8ad52020-07-20 17:55:13 +0200590#define lockdep_assert_preemption_enabled() \
591do { \
592 WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_COUNT) && \
Peter Zijlstra4d004092020-10-02 11:04:21 +0200593 __lockdep_enabled && \
Ahmed S. Darwish8fd8ad52020-07-20 17:55:13 +0200594 (preempt_count() != 0 || \
Peter Zijlstrabaffd722020-10-05 09:56:57 +0200595 !this_cpu_read(hardirqs_enabled))); \
Ahmed S. Darwish8fd8ad52020-07-20 17:55:13 +0200596} while (0)
597
598#define lockdep_assert_preemption_disabled() \
599do { \
600 WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_COUNT) && \
Peter Zijlstra4d004092020-10-02 11:04:21 +0200601 __lockdep_enabled && \
Ahmed S. Darwish8fd8ad52020-07-20 17:55:13 +0200602 (preempt_count() == 0 && \
Peter Zijlstrabaffd722020-10-05 09:56:57 +0200603 this_cpu_read(hardirqs_enabled))); \
Ahmed S. Darwish8fd8ad52020-07-20 17:55:13 +0200604} while (0)
605
Peter Zijlstra76b189e2008-09-10 09:57:35 +0200606#else
607# define might_lock(lock) do { } while (0)
608# define might_lock_read(lock) do { } while (0)
Daniel Vettere692b402019-11-04 18:37:19 +0100609# define might_lock_nested(lock, subclass) do { } while (0)
Peter Zijlstraa21ee602020-05-25 12:22:41 +0200610
Frederic Weisbeckerf54bb2e2017-11-06 16:01:17 +0100611# define lockdep_assert_irqs_enabled() do { } while (0)
612# define lockdep_assert_irqs_disabled() do { } while (0)
Joel Fernandes (Google)71d8d152019-03-26 15:24:08 -0400613# define lockdep_assert_in_irq() do { } while (0)
Ahmed S. Darwish8fd8ad52020-07-20 17:55:13 +0200614
615# define lockdep_assert_preemption_enabled() do { } while (0)
616# define lockdep_assert_preemption_disabled() do { } while (0)
Peter Zijlstra76b189e2008-09-10 09:57:35 +0200617#endif
618
Sebastian Siewior8bf6c672020-03-23 16:20:19 +0100619#ifdef CONFIG_PROVE_RAW_LOCK_NESTING
620
621# define lockdep_assert_RT_in_threaded_ctx() do { \
622 WARN_ONCE(debug_locks && !current->lockdep_recursion && \
Peter Zijlstraf9ad4a52020-05-27 13:03:26 +0200623 lockdep_hardirq_context() && \
Sebastian Siewior8bf6c672020-03-23 16:20:19 +0100624 !(current->hardirq_threaded || current->irq_config), \
625 "Not in threaded context on PREEMPT_RT as expected\n"); \
626} while (0)
627
628#else
629
630# define lockdep_assert_RT_in_threaded_ctx() do { } while (0)
631
632#endif
633
Paul E. McKenneyd24209bb2015-01-21 15:26:03 -0800634#ifdef CONFIG_LOCKDEP
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -0700635void lockdep_rcu_suspicious(const char *file, const int line, const char *s);
Paul E. McKenneyd24209bb2015-01-21 15:26:03 -0800636#else
637static inline void
638lockdep_rcu_suspicious(const char *file, const int line, const char *s)
639{
640}
Paul E. McKenney0632eb32010-02-22 17:04:47 -0800641#endif
642
Ingo Molnarfbb9ce952006-07-03 00:24:50 -0700643#endif /* __LINUX_LOCKDEP_H */