2 * Runtime locking correctness validator
4 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
6 * see Documentation/lockdep-design.txt for more details.
8 #ifndef __LINUX_LOCKDEP_H
9 #define __LINUX_LOCKDEP_H
15 #include <linux/linkage.h>
16 #include <linux/list.h>
17 #include <linux/debug_locks.h>
18 #include <linux/stacktrace.h>
21 * Lock-class usage-state bits:
28 LOCK_ENABLED_SOFTIRQS
,
29 LOCK_ENABLED_HARDIRQS
,
30 LOCK_USED_IN_HARDIRQ_READ
,
31 LOCK_USED_IN_SOFTIRQ_READ
,
32 LOCK_ENABLED_SOFTIRQS_READ
,
33 LOCK_ENABLED_HARDIRQS_READ
,
38 * Usage-state bitmasks:
40 #define LOCKF_USED (1 << LOCK_USED)
41 #define LOCKF_USED_IN_HARDIRQ (1 << LOCK_USED_IN_HARDIRQ)
42 #define LOCKF_USED_IN_SOFTIRQ (1 << LOCK_USED_IN_SOFTIRQ)
43 #define LOCKF_ENABLED_HARDIRQS (1 << LOCK_ENABLED_HARDIRQS)
44 #define LOCKF_ENABLED_SOFTIRQS (1 << LOCK_ENABLED_SOFTIRQS)
46 #define LOCKF_ENABLED_IRQS (LOCKF_ENABLED_HARDIRQS | LOCKF_ENABLED_SOFTIRQS)
47 #define LOCKF_USED_IN_IRQ (LOCKF_USED_IN_HARDIRQ | LOCKF_USED_IN_SOFTIRQ)
49 #define LOCKF_USED_IN_HARDIRQ_READ (1 << LOCK_USED_IN_HARDIRQ_READ)
50 #define LOCKF_USED_IN_SOFTIRQ_READ (1 << LOCK_USED_IN_SOFTIRQ_READ)
51 #define LOCKF_ENABLED_HARDIRQS_READ (1 << LOCK_ENABLED_HARDIRQS_READ)
52 #define LOCKF_ENABLED_SOFTIRQS_READ (1 << LOCK_ENABLED_SOFTIRQS_READ)
54 #define LOCKF_ENABLED_IRQS_READ \
55 (LOCKF_ENABLED_HARDIRQS_READ | LOCKF_ENABLED_SOFTIRQS_READ)
56 #define LOCKF_USED_IN_IRQ_READ \
57 (LOCKF_USED_IN_HARDIRQ_READ | LOCKF_USED_IN_SOFTIRQ_READ)
59 #define MAX_LOCKDEP_SUBCLASSES 8UL
62 * Lock-classes are keyed via unique addresses, by embedding the
63 * lockclass-key into the kernel (or module) .data section. (For
64 * static locks we use the lock address itself as the key.)
66 struct lockdep_subclass_key
{
68 } __attribute__ ((__packed__
));
70 struct lock_class_key
{
71 struct lockdep_subclass_key subkeys
[MAX_LOCKDEP_SUBCLASSES
];
75 * The lock-class itself:
81 struct list_head hash_entry
;
84 * global list of all lock-classes:
86 struct list_head lock_entry
;
88 struct lockdep_subclass_key
*key
;
89 unsigned int subclass
;
92 * IRQ/softirq usage tracking bits:
94 unsigned long usage_mask
;
95 struct stack_trace usage_traces
[LOCK_USAGE_STATES
];
98 * These fields represent a directed graph of lock dependencies,
99 * to every node we attach a list of "forward" and a list of
100 * "backward" graph nodes.
102 struct list_head locks_after
, locks_before
;
105 * Generation counter, when doing certain classes of graph walking,
106 * to ensure that we check one node only once:
108 unsigned int version
;
111 * Statistics counter:
120 * Map the lock object (the lock instance) to the lock-class object.
121 * This is embedded into specific lock instances:
124 struct lock_class_key
*key
;
125 struct lock_class
*class_cache
;
130 * Every lock has a list of other locks that were taken after it.
131 * We only grow the list, never remove from it:
134 struct list_head entry
;
135 struct lock_class
*class;
136 struct stack_trace trace
;
141 * We record lock dependency chains, so that we can cache them:
144 struct list_head entry
;
150 * One-way hash of the dependency chain up to this point. We
151 * hash the hashes step by step as the dependency chain grows.
153 * We use it for dependency-caching and we skip detection
154 * passes and dependency-updates if there is a cache-hit, so
155 * it is absolutely critical for 100% coverage of the validator
156 * to have a unique key value for every unique dependency path
157 * that can occur in the system, to make a unique hash value
158 * as likely as possible - hence the 64-bit width.
160 * The task struct holds the current hash value (initialized
161 * with zero), here we store the previous hash value:
164 struct lock_class
*class;
165 unsigned long acquire_ip
;
166 struct lockdep_map
*instance
;
169 * The lock-stack is unified in that the lock chains of interrupt
170 * contexts nest ontop of process context chains, but we 'separate'
171 * the hashes by starting with 0 if we cross into an interrupt
172 * context, and we also keep do not add cross-context lock
173 * dependencies - the lock usage graph walking covers that area
174 * anyway, and we'd just unnecessarily increase the number of
175 * dependencies otherwise. [Note: hardirq and softirq contexts
176 * are separated from each other too.]
178 * The following field is used to detect when we cross into an
189 * Initialization, self-test and debugging-output methods:
191 extern void lockdep_init(void);
192 extern void lockdep_info(void);
193 extern void lockdep_reset(void);
194 extern void lockdep_reset_lock(struct lockdep_map
*lock
);
195 extern void lockdep_free_key_range(void *start
, unsigned long size
);
197 extern void lockdep_off(void);
198 extern void lockdep_on(void);
201 * These methods are used by specific locking variants (spinlocks,
202 * rwlocks, mutexes and rwsems) to pass init/acquire/release events
206 extern void lockdep_init_map(struct lockdep_map
*lock
, const char *name
,
207 struct lock_class_key
*key
, int subclass
);
210 * To initialize a lockdep_map statically use this macro.
211 * Note that _name must not be NULL.
213 #define STATIC_LOCKDEP_MAP_INIT(_name, _key) \
214 { .name = (_name), .key = (void *)(_key), }
217 * Reinitialize a lock key - for cases where there is special locking or
218 * special initialization of locks so that the validator gets the scope
219 * of dependencies wrong: they are either too broad (they need a class-split)
220 * or they are too narrow (they suffer from a false class-split):
222 #define lockdep_set_class(lock, key) \
223 lockdep_init_map(&(lock)->dep_map, #key, key, 0)
224 #define lockdep_set_class_and_name(lock, key, name) \
225 lockdep_init_map(&(lock)->dep_map, name, key, 0)
226 #define lockdep_set_class_and_subclass(lock, key, sub) \
227 lockdep_init_map(&(lock)->dep_map, #key, key, sub)
228 #define lockdep_set_subclass(lock, sub) \
229 lockdep_init_map(&(lock)->dep_map, #lock, \
230 (lock)->dep_map.key, sub)
237 * 0: exclusive (write) acquire
238 * 1: read-acquire (no recursion allowed)
239 * 2: read-acquire with same-instance recursion allowed
244 * 1: simple checks (freeing, held-at-exit-time, etc.)
247 extern void lock_acquire(struct lockdep_map
*lock
, unsigned int subclass
,
248 int trylock
, int read
, int check
, unsigned long ip
);
250 extern void lock_release(struct lockdep_map
*lock
, int nested
,
253 # define INIT_LOCKDEP .lockdep_recursion = 0,
255 #define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
259 static inline void lockdep_off(void)
263 static inline void lockdep_on(void)
267 # define lock_acquire(l, s, t, r, c, i) do { } while (0)
268 # define lock_release(l, n, i) do { } while (0)
269 # define lockdep_init() do { } while (0)
270 # define lockdep_info() do { } while (0)
271 # define lockdep_init_map(lock, name, key, sub) do { (void)(key); } while (0)
272 # define lockdep_set_class(lock, key) do { (void)(key); } while (0)
273 # define lockdep_set_class_and_name(lock, key, name) \
274 do { (void)(key); } while (0)
275 #define lockdep_set_class_and_subclass(lock, key, sub) \
276 do { (void)(key); } while (0)
277 #define lockdep_set_subclass(lock, sub) do { } while (0)
279 # define INIT_LOCKDEP
280 # define lockdep_reset() do { debug_locks = 1; } while (0)
281 # define lockdep_free_key_range(start, size) do { } while (0)
283 * The class key takes no space if lockdep is disabled:
285 struct lock_class_key
{ };
287 #define lockdep_depth(tsk) (0)
289 #endif /* !LOCKDEP */
291 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_GENERIC_HARDIRQS)
292 extern void early_init_irq_lock_class(void);
294 static inline void early_init_irq_lock_class(void)
299 #ifdef CONFIG_TRACE_IRQFLAGS
300 extern void early_boot_irqs_off(void);
301 extern void early_boot_irqs_on(void);
302 extern void print_irqtrace_events(struct task_struct
*curr
);
304 static inline void early_boot_irqs_off(void)
307 static inline void early_boot_irqs_on(void)
310 static inline void print_irqtrace_events(struct task_struct
*curr
)
316 * For trivial one-depth nesting of a lock-class, the following
317 * global define can be used. (Subsystems with multiple levels
318 * of nesting should define their own lock-nesting subclasses.)
320 #define SINGLE_DEPTH_NESTING 1
323 * Map the dependency ops to NOP or to real lockdep ops, depending
324 * on the per lock-class debug mode:
327 #ifdef CONFIG_DEBUG_LOCK_ALLOC
328 # ifdef CONFIG_PROVE_LOCKING
329 # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
331 # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
333 # define spin_release(l, n, i) lock_release(l, n, i)
335 # define spin_acquire(l, s, t, i) do { } while (0)
336 # define spin_release(l, n, i) do { } while (0)
339 #ifdef CONFIG_DEBUG_LOCK_ALLOC
340 # ifdef CONFIG_PROVE_LOCKING
341 # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
342 # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 2, i)
344 # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
345 # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 1, i)
347 # define rwlock_release(l, n, i) lock_release(l, n, i)
349 # define rwlock_acquire(l, s, t, i) do { } while (0)
350 # define rwlock_acquire_read(l, s, t, i) do { } while (0)
351 # define rwlock_release(l, n, i) do { } while (0)
354 #ifdef CONFIG_DEBUG_LOCK_ALLOC
355 # ifdef CONFIG_PROVE_LOCKING
356 # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
358 # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
360 # define mutex_release(l, n, i) lock_release(l, n, i)
362 # define mutex_acquire(l, s, t, i) do { } while (0)
363 # define mutex_release(l, n, i) do { } while (0)
366 #ifdef CONFIG_DEBUG_LOCK_ALLOC
367 # ifdef CONFIG_PROVE_LOCKING
368 # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
369 # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 2, i)
371 # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
372 # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 1, i)
374 # define rwsem_release(l, n, i) lock_release(l, n, i)
376 # define rwsem_acquire(l, s, t, i) do { } while (0)
377 # define rwsem_acquire_read(l, s, t, i) do { } while (0)
378 # define rwsem_release(l, n, i) do { } while (0)
381 #endif /* __LINUX_LOCKDEP_H */