2 * Runtime locking correctness validator
4 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * see Documentation/lockdep-design.txt for more details.
9 #ifndef __LINUX_LOCKDEP_WRAPPER_H
10 #define __LINUX_LOCKDEP_WRAPPER_H
12 #include_next <linux/lockdep.h>
14 #include <linux/version.h>
15 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
22 #include <linux/linkage.h>
23 #include <linux/list.h>
24 #include <linux/debug_locks.h>
25 #include <linux/stacktrace.h>
28 * Lock-class usage-state bits:
35 LOCK_ENABLED_SOFTIRQS,
36 LOCK_ENABLED_HARDIRQS,
37 LOCK_USED_IN_HARDIRQ_READ,
38 LOCK_USED_IN_SOFTIRQ_READ,
39 LOCK_ENABLED_SOFTIRQS_READ,
40 LOCK_ENABLED_HARDIRQS_READ,
45 * Usage-state bitmasks:
47 #define LOCKF_USED (1 << LOCK_USED)
48 #define LOCKF_USED_IN_HARDIRQ (1 << LOCK_USED_IN_HARDIRQ)
49 #define LOCKF_USED_IN_SOFTIRQ (1 << LOCK_USED_IN_SOFTIRQ)
50 #define LOCKF_ENABLED_HARDIRQS (1 << LOCK_ENABLED_HARDIRQS)
51 #define LOCKF_ENABLED_SOFTIRQS (1 << LOCK_ENABLED_SOFTIRQS)
53 #define LOCKF_ENABLED_IRQS (LOCKF_ENABLED_HARDIRQS | LOCKF_ENABLED_SOFTIRQS)
54 #define LOCKF_USED_IN_IRQ (LOCKF_USED_IN_HARDIRQ | LOCKF_USED_IN_SOFTIRQ)
56 #define LOCKF_USED_IN_HARDIRQ_READ (1 << LOCK_USED_IN_HARDIRQ_READ)
57 #define LOCKF_USED_IN_SOFTIRQ_READ (1 << LOCK_USED_IN_SOFTIRQ_READ)
58 #define LOCKF_ENABLED_HARDIRQS_READ (1 << LOCK_ENABLED_HARDIRQS_READ)
59 #define LOCKF_ENABLED_SOFTIRQS_READ (1 << LOCK_ENABLED_SOFTIRQS_READ)
61 #define LOCKF_ENABLED_IRQS_READ \
62 (LOCKF_ENABLED_HARDIRQS_READ | LOCKF_ENABLED_SOFTIRQS_READ)
63 #define LOCKF_USED_IN_IRQ_READ \
64 (LOCKF_USED_IN_HARDIRQ_READ | LOCKF_USED_IN_SOFTIRQ_READ)
66 #define MAX_LOCKDEP_SUBCLASSES 8UL
69 * Lock-classes are keyed via unique addresses, by embedding the
70 * lockclass-key into the kernel (or module) .data section. (For
71 * static locks we use the lock address itself as the key.)
73 struct lockdep_subclass_key {
75 } __attribute__ ((__packed__));
77 struct lock_class_key {
78 struct lockdep_subclass_key subkeys[MAX_LOCKDEP_SUBCLASSES];
82 * The lock-class itself:
88 struct list_head hash_entry;
91 * global list of all lock-classes:
93 struct list_head lock_entry;
95 struct lockdep_subclass_key *key;
96 unsigned int subclass;
99 * IRQ/softirq usage tracking bits:
101 unsigned long usage_mask;
102 struct stack_trace usage_traces[LOCK_USAGE_STATES];
105 * These fields represent a directed graph of lock dependencies,
106 * to every node we attach a list of "forward" and a list of
107 * "backward" graph nodes.
109 struct list_head locks_after, locks_before;
112 * Generation counter, when doing certain classes of graph walking,
113 * to ensure that we check one node only once:
115 unsigned int version;
118 * Statistics counter:
125 #ifdef CONFIG_LOCK_STAT
126 unsigned long contention_point[4];
130 #ifdef CONFIG_LOCK_STAT
139 bounce_acquired_write,
140 bounce_acquired_read,
141 bounce_contended_write,
142 bounce_contended_read,
145 bounce_acquired = bounce_acquired_write,
146 bounce_contended = bounce_contended_write,
149 struct lock_class_stats {
150 unsigned long contention_point[4];
151 struct lock_time read_waittime;
152 struct lock_time write_waittime;
153 struct lock_time read_holdtime;
154 struct lock_time write_holdtime;
155 unsigned long bounces[nr_bounce_types];
158 struct lock_class_stats lock_stats(struct lock_class *class);
159 void clear_lock_stats(struct lock_class *class);
163 * Map the lock object (the lock instance) to the lock-class object.
164 * This is embedded into specific lock instances:
167 struct lock_class_key *key;
168 struct lock_class *class_cache;
170 #ifdef CONFIG_LOCK_STAT
176 * Every lock has a list of other locks that were taken after it.
177 * We only grow the list, never remove from it:
180 struct list_head entry;
181 struct lock_class *class;
182 struct stack_trace trace;
187 * We record lock dependency chains, so that we can cache them:
190 struct list_head entry;
196 * One-way hash of the dependency chain up to this point. We
197 * hash the hashes step by step as the dependency chain grows.
199 * We use it for dependency-caching and we skip detection
200 * passes and dependency-updates if there is a cache-hit, so
201 * it is absolutely critical for 100% coverage of the validator
202 * to have a unique key value for every unique dependency path
203 * that can occur in the system, to make a unique hash value
204 * as likely as possible - hence the 64-bit width.
206 * The task struct holds the current hash value (initialized
207 * with zero), here we store the previous hash value:
210 struct lock_class *class;
211 unsigned long acquire_ip;
212 struct lockdep_map *instance;
214 #ifdef CONFIG_LOCK_STAT
219 * The lock-stack is unified in that the lock chains of interrupt
220 * contexts nest ontop of process context chains, but we 'separate'
221 * the hashes by starting with 0 if we cross into an interrupt
222 * context, and we also keep do not add cross-context lock
223 * dependencies - the lock usage graph walking covers that area
224 * anyway, and we'd just unnecessarily increase the number of
225 * dependencies otherwise. [Note: hardirq and softirq contexts
226 * are separated from each other too.]
228 * The following field is used to detect when we cross into an
239 * Initialization, self-test and debugging-output methods:
241 extern void lockdep_init(void);
242 extern void lockdep_info(void);
243 extern void lockdep_reset(void);
244 extern void lockdep_reset_lock(struct lockdep_map *lock);
245 extern void lockdep_free_key_range(void *start, unsigned long size);
247 extern void lockdep_off(void);
248 extern void lockdep_on(void);
251 * These methods are used by specific locking variants (spinlocks,
252 * rwlocks, mutexes and rwsems) to pass init/acquire/release events
256 extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
257 struct lock_class_key *key, int subclass);
260 * Reinitialize a lock key - for cases where there is special locking or
261 * special initialization of locks so that the validator gets the scope
262 * of dependencies wrong: they are either too broad (they need a class-split)
263 * or they are too narrow (they suffer from a false class-split):
265 #define lockdep_set_class(lock, key) \
266 lockdep_init_map(&(lock)->dep_map, #key, key, 0)
267 #define lockdep_set_class_and_name(lock, key, name) \
268 lockdep_init_map(&(lock)->dep_map, name, key, 0)
269 #define lockdep_set_class_and_subclass(lock, key, sub) \
270 lockdep_init_map(&(lock)->dep_map, #key, key, sub)
271 #define lockdep_set_subclass(lock, sub) \
272 lockdep_init_map(&(lock)->dep_map, #lock, \
273 (lock)->dep_map.key, sub)
280 * 0: exclusive (write) acquire
281 * 1: read-acquire (no recursion allowed)
282 * 2: read-acquire with same-instance recursion allowed
287 * 1: simple checks (freeing, held-at-exit-time, etc.)
290 extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
291 int trylock, int read, int check, unsigned long ip);
293 extern void lock_release(struct lockdep_map *lock, int nested,
296 # define INIT_LOCKDEP .lockdep_recursion = 0,
298 #define lockdep_depth(tsk) (debug_locks ? (tsk)->lockdep_depth : 0)
302 static inline void lockdep_off(void)
306 static inline void lockdep_on(void)
310 # define lock_acquire(l, s, t, r, c, i) do { } while (0)
311 # define lock_release(l, n, i) do { } while (0)
312 # define lockdep_init() do { } while (0)
313 # define lockdep_info() do { } while (0)
314 # define lockdep_init_map(lock, name, key, sub) do { (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); } 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)
322 # define INIT_LOCKDEP
323 # define lockdep_reset() do { debug_locks = 1; } while (0)
324 # define lockdep_free_key_range(start, size) do { } while (0)
326 * The class key takes no space if lockdep is disabled:
328 struct lock_class_key { };
330 #define lockdep_depth(tsk) (0)
332 #endif /* !LOCKDEP */
334 #ifdef CONFIG_LOCK_STAT
336 extern void lock_contended(struct lockdep_map *lock, unsigned long ip);
337 extern void lock_acquired(struct lockdep_map *lock);
339 #define LOCK_CONTENDED(_lock, try, lock) \
342 lock_contended(&(_lock)->dep_map, _RET_IP_); \
345 lock_acquired(&(_lock)->dep_map); \
348 #else /* CONFIG_LOCK_STAT */
350 #define lock_contended(lockdep_map, ip) do {} while (0)
351 #define lock_acquired(lockdep_map) do {} while (0)
353 #define LOCK_CONTENDED(_lock, try, lock) \
356 #endif /* CONFIG_LOCK_STAT */
358 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_GENERIC_HARDIRQS)
359 extern void early_init_irq_lock_class(void);
361 static inline void early_init_irq_lock_class(void)
366 #ifdef CONFIG_TRACE_IRQFLAGS
367 extern void early_boot_irqs_off(void);
368 extern void early_boot_irqs_on(void);
369 extern void print_irqtrace_events(struct task_struct *curr);
371 static inline void early_boot_irqs_off(void)
374 static inline void early_boot_irqs_on(void)
377 static inline void print_irqtrace_events(struct task_struct *curr)
383 * For trivial one-depth nesting of a lock-class, the following
384 * global define can be used. (Subsystems with multiple levels
385 * of nesting should define their own lock-nesting subclasses.)
387 #define SINGLE_DEPTH_NESTING 1
390 * Map the dependency ops to NOP or to real lockdep ops, depending
391 * on the per lock-class debug mode:
394 #ifdef CONFIG_DEBUG_LOCK_ALLOC
395 # ifdef CONFIG_PROVE_LOCKING
396 # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
398 # define spin_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
400 # define spin_release(l, n, i) lock_release(l, n, i)
402 # define spin_acquire(l, s, t, i) do { } while (0)
403 # define spin_release(l, n, i) do { } while (0)
406 #ifdef CONFIG_DEBUG_LOCK_ALLOC
407 # ifdef CONFIG_PROVE_LOCKING
408 # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
409 # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 2, i)
411 # define rwlock_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
412 # define rwlock_acquire_read(l, s, t, i) lock_acquire(l, s, t, 2, 1, i)
414 # define rwlock_release(l, n, i) lock_release(l, n, i)
416 # define rwlock_acquire(l, s, t, i) do { } while (0)
417 # define rwlock_acquire_read(l, s, t, i) do { } while (0)
418 # define rwlock_release(l, n, i) do { } while (0)
421 #ifdef CONFIG_DEBUG_LOCK_ALLOC
422 # ifdef CONFIG_PROVE_LOCKING
423 # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
425 # define mutex_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
427 # define mutex_release(l, n, i) lock_release(l, n, i)
429 # define mutex_acquire(l, s, t, i) do { } while (0)
430 # define mutex_release(l, n, i) do { } while (0)
433 #ifdef CONFIG_DEBUG_LOCK_ALLOC
434 # ifdef CONFIG_PROVE_LOCKING
435 # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 2, i)
436 # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 2, i)
438 # define rwsem_acquire(l, s, t, i) lock_acquire(l, s, t, 0, 1, i)
439 # define rwsem_acquire_read(l, s, t, i) lock_acquire(l, s, t, 1, 1, i)
441 # define rwsem_release(l, n, i) lock_release(l, n, i)
443 # define rwsem_acquire(l, s, t, i) do { } while (0)
444 # define rwsem_acquire_read(l, s, t, i) do { } while (0)
445 # define rwsem_release(l, n, i) do { } while (0)
448 #endif /* linux kernel < 2.6.18 */
450 #endif /* __LINUX_LOCKDEP_WRAPPER_H */