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
16 #include <linux/linkage.h>
17 #include <linux/list.h>
18 #include <linux/debug_locks.h>
19 #include <linux/stacktrace.h>
22 * Lock-class usage-state bits:
29 LOCK_ENABLED_SOFTIRQS,
30 LOCK_ENABLED_HARDIRQS,
31 LOCK_USED_IN_HARDIRQ_READ,
32 LOCK_USED_IN_SOFTIRQ_READ,
33 LOCK_ENABLED_SOFTIRQS_READ,
34 LOCK_ENABLED_HARDIRQS_READ,
39 * Usage-state bitmasks:
41 #define LOCKF_USED (1 << LOCK_USED)
42 #define LOCKF_USED_IN_HARDIRQ (1 << LOCK_USED_IN_HARDIRQ)
43 #define LOCKF_USED_IN_SOFTIRQ (1 << LOCK_USED_IN_SOFTIRQ)
44 #define LOCKF_ENABLED_HARDIRQS (1 << LOCK_ENABLED_HARDIRQS)
45 #define LOCKF_ENABLED_SOFTIRQS (1 << LOCK_ENABLED_SOFTIRQS)
47 #define LOCKF_ENABLED_IRQS (LOCKF_ENABLED_HARDIRQS | LOCKF_ENABLED_SOFTIRQS)
48 #define LOCKF_USED_IN_IRQ (LOCKF_USED_IN_HARDIRQ | LOCKF_USED_IN_SOFTIRQ)
50 #define LOCKF_USED_IN_HARDIRQ_READ (1 << LOCK_USED_IN_HARDIRQ_READ)
51 #define LOCKF_USED_IN_SOFTIRQ_READ (1 << LOCK_USED_IN_SOFTIRQ_READ)
52 #define LOCKF_ENABLED_HARDIRQS_READ (1 << LOCK_ENABLED_HARDIRQS_READ)
53 #define LOCKF_ENABLED_SOFTIRQS_READ (1 << LOCK_ENABLED_SOFTIRQS_READ)
55 #define LOCKF_ENABLED_IRQS_READ \
56 (LOCKF_ENABLED_HARDIRQS_READ | LOCKF_ENABLED_SOFTIRQS_READ)
57 #define LOCKF_USED_IN_IRQ_READ \
58 (LOCKF_USED_IN_HARDIRQ_READ | LOCKF_USED_IN_SOFTIRQ_READ)
60 #define MAX_LOCKDEP_SUBCLASSES 8UL
63 * Lock-classes are keyed via unique addresses, by embedding the
64 * lockclass-key into the kernel (or module) .data section. (For
65 * static locks we use the lock address itself as the key.)
67 struct lockdep_subclass_key {
69 } __attribute__ ((__packed__));
71 struct lock_class_key {
72 struct lockdep_subclass_key subkeys[MAX_LOCKDEP_SUBCLASSES];
76 * The lock-class itself:
82 struct list_head hash_entry;
85 * global list of all lock-classes:
87 struct list_head lock_entry;
89 struct lockdep_subclass_key *key;
90 unsigned int subclass;
93 * IRQ/softirq usage tracking bits:
95 unsigned long usage_mask;
96 struct stack_trace usage_traces[LOCK_USAGE_STATES];
99 * These fields represent a directed graph of lock dependencies,
100 * to every node we attach a list of "forward" and a list of
101 * "backward" graph nodes.
103 struct list_head locks_after, locks_before;
106 * Generation counter, when doing certain classes of graph walking,
107 * to ensure that we check one node only once:
109 unsigned int version;
112 * Statistics counter:
119 #ifdef CONFIG_LOCK_STAT
120 unsigned long contention_point[4];
124 #ifdef CONFIG_LOCK_STAT
132 struct lock_class_stats {
133 unsigned long contention_point[4];
134 struct lock_time read_waittime;
135 struct lock_time write_waittime;
136 struct lock_time read_holdtime;
137 struct lock_time write_holdtime;
140 struct lock_class_stats lock_stats(struct lock_class *class);
141 void clear_lock_stats(struct lock_class *class);
145 * Map the lock object (the lock instance) to the lock-class object.
146 * This is embedded into specific lock instances:
149 struct lock_class_key *key;
150 struct lock_class *class_cache;
155 * Every lock has a list of other locks that were taken after it.
156 * We only grow the list, never remove from it:
159 struct list_head entry;
160 struct lock_class *class;
161 struct stack_trace trace;
166 * We record lock dependency chains, so that we can cache them:
169 struct list_head entry;
175 * One-way hash of the dependency chain up to this point. We
176 * hash the hashes step by step as the dependency chain grows.
178 * We use it for dependency-caching and we skip detection
179 * passes and dependency-updates if there is a cache-hit, so
180 * it is absolutely critical for 100% coverage of the validator
181 * to have a unique key value for every unique dependency path
182 * that can occur in the system, to make a unique hash value
183 * as likely as possible - hence the 64-bit width.
185 * The task struct holds the current hash value (initialized
186 * with zero), here we store the previous hash value:
189 struct lock_class *class;
190 unsigned long acquire_ip;
191 struct lockdep_map *instance;
193 #ifdef CONFIG_LOCK_STAT
198 * The lock-stack is unified in that the lock chains of interrupt
199 * contexts nest ontop of process context chains, but we 'separate'
200 * the hashes by starting with 0 if we cross into an interrupt
201 * context, and we also keep do not add cross-context lock
202 * dependencies - the lock usage graph walking covers that area
203 * anyway, and we'd just unnecessarily increase the number of
204 * dependencies otherwise. [Note: hardirq and softirq contexts
205 * are separated from each other too.]
207 * The following field is used to detect when we cross into an
218 * Initialization, self-test and debugging-output methods:
220 extern void lockdep_init(void);
221 extern void lockdep_info(void);
222 extern void lockdep_reset(void);
223 extern void lockdep_reset_lock(struct lockdep_map *lock);
224 extern void lockdep_free_key_range(void *start, unsigned long size);
226 extern void lockdep_off(void);
227 extern void lockdep_on(void);
230 * These methods are used by specific locking variants (spinlocks,
231 * rwlocks, mutexes and rwsems) to pass init/acquire/release events
235 extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
236 struct lock_class_key *key, int subclass);
239 * Reinitialize a lock key - for cases where there is special locking or
240 * special initialization of locks so that the validator gets the scope
241 * of dependencies wrong: they are either too broad (they need a class-split)
242 * or they are too narrow (they suffer from a false class-split):
244 #define lockdep_set_class(lock, key) \
245 lockdep_init_map(&(lock)->dep_map, #key, key, 0)
246 #define lockdep_set_class_and_name(lock, key, name) \
247 lockdep_init_map(&(lock)->dep_map, name, key, 0)
248 #define lockdep_set_class_and_subclass(lock, key, sub) \
249 lockdep_init_map(&(lock)->dep_map, #key, key, sub)
250 #define lockdep_set_subclass(lock, sub) \
251 lockdep_init_map(&(lock)->dep_map, #lock, \
252 (lock)->dep_map.key, sub)
259 * 0: exclusive (write) acquire
260 * 1: read-acquire (no recursion allowed)
261 * 2: read-acquire with same-instance recursion allowed
266 * 1: simple checks (freeing, held-at-exit-time, etc.)
269 extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
270 int trylock, int read, int check, unsigned long ip);
272 extern void lock_release(struct lockdep_map *lock, int nested,
275 # define INIT_LOCKDEP .lockdep_recursion = 0,
277 #define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
281 static inline void lockdep_off(void)
285 static inline void lockdep_on(void)
289 # define lock_acquire(l, s, t, r, c, i) do { } while (0)
290 # define lock_release(l, n, i) do { } while (0)
291 # define lockdep_init() do { } while (0)
292 # define lockdep_info() do { } while (0)
293 # define lockdep_init_map(lock, name, key, sub) do { (void)(key); } while (0)
294 # define lockdep_set_class(lock, key) do { (void)(key); } while (0)
295 # define lockdep_set_class_and_name(lock, key, name) \
296 do { (void)(key); } while (0)
297 #define lockdep_set_class_and_subclass(lock, key, sub) \
298 do { (void)(key); } while (0)
299 #define lockdep_set_subclass(lock, sub) do { } while (0)
301 # define INIT_LOCKDEP
302 # define lockdep_reset() do { debug_locks = 1; } while (0)
303 # define lockdep_free_key_range(start, size) do { } while (0)
305 * The class key takes no space if lockdep is disabled:
307 struct lock_class_key { };
309 #define lockdep_depth(tsk) (0)
311 #endif /* !LOCKDEP */
313 #ifdef CONFIG_LOCK_STAT
315 extern void lock_contended(struct lockdep_map *lock, unsigned long ip);
316 extern void lock_acquired(struct lockdep_map *lock);
318 #define LOCK_CONTENDED(_lock, try, lock) \
321 lock_contended(&(_lock)->dep_map, _RET_IP_); \
323 lock_acquired(&(_lock)->dep_map); \
327 #else /* CONFIG_LOCK_STAT */
329 #define lock_contended(lockdep_map, ip) do {} while (0)
330 #define lock_acquired(lockdep_map) do {} while (0)
332 #define LOCK_CONTENDED(_lock, try, lock) \
335 #endif /* CONFIG_LOCK_STAT */
337 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_GENERIC_HARDIRQS)
338 extern void early_init_irq_lock_class(void);
340 static inline void early_init_irq_lock_class(void)
345 #ifdef CONFIG_TRACE_IRQFLAGS
346 extern void early_boot_irqs_off(void);
347 extern void early_boot_irqs_on(void);
348 extern void print_irqtrace_events(struct task_struct *curr);
350 static inline void early_boot_irqs_off(void)
353 static inline void early_boot_irqs_on(void)
356 static inline void print_irqtrace_events(struct task_struct *curr)
362 * For trivial one-depth nesting of a lock-class, the following
363 * global define can be used. (Subsystems with multiple levels
364 * of nesting should define their own lock-nesting subclasses.)
366 #define SINGLE_DEPTH_NESTING 1
369 * Map the dependency ops to NOP or to real lockdep ops, depending
370 * on the per lock-class debug mode:
373 #ifdef CONFIG_DEBUG_LOCK_ALLOC
374 # ifdef CONFIG_PROVE_LOCKING
375 # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
377 # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
379 # define spin_release(l, n, i) lock_release(l, n, i)
381 # define spin_acquire(l, s, t, i) do { } while (0)
382 # define spin_release(l, n, i) do { } while (0)
385 #ifdef CONFIG_DEBUG_LOCK_ALLOC
386 # ifdef CONFIG_PROVE_LOCKING
387 # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
388 # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 2, i)
390 # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
391 # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 1, i)
393 # define rwlock_release(l, n, i) lock_release(l, n, i)
395 # define rwlock_acquire(l, s, t, i) do { } while (0)
396 # define rwlock_acquire_read(l, s, t, i) do { } while (0)
397 # define rwlock_release(l, n, i) do { } while (0)
400 #ifdef CONFIG_DEBUG_LOCK_ALLOC
401 # ifdef CONFIG_PROVE_LOCKING
402 # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
404 # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
406 # define mutex_release(l, n, i) lock_release(l, n, i)
408 # define mutex_acquire(l, s, t, i) do { } while (0)
409 # define mutex_release(l, n, i) do { } while (0)
412 #ifdef CONFIG_DEBUG_LOCK_ALLOC
413 # ifdef CONFIG_PROVE_LOCKING
414 # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
415 # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 2, i)
417 # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
418 # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 1, i)
420 # define rwsem_release(l, n, i) lock_release(l, n, i)
422 # define rwsem_acquire(l, s, t, i) do { } while (0)
423 # define rwsem_acquire_read(l, s, t, i) do { } while (0)
424 # define rwsem_release(l, n, i) do { } while (0)
427 #endif /* __LINUX_LOCKDEP_H */