lockstat: core infrastructure
[linux-2.6] / include / linux / lockdep.h
1 /*
2  * Runtime locking correctness validator
3  *
4  *  Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5  *
6  * see Documentation/lockdep-design.txt for more details.
7  */
8 #ifndef __LINUX_LOCKDEP_H
9 #define __LINUX_LOCKDEP_H
10
11 struct task_struct;
12 struct lockdep_map;
13
14 #ifdef CONFIG_LOCKDEP
15
16 #include <linux/linkage.h>
17 #include <linux/list.h>
18 #include <linux/debug_locks.h>
19 #include <linux/stacktrace.h>
20
21 /*
22  * Lock-class usage-state bits:
23  */
24 enum lock_usage_bit
25 {
26         LOCK_USED = 0,
27         LOCK_USED_IN_HARDIRQ,
28         LOCK_USED_IN_SOFTIRQ,
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,
35         LOCK_USAGE_STATES
36 };
37
38 /*
39  * Usage-state bitmasks:
40  */
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)
46
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)
49
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)
54
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)
59
60 #define MAX_LOCKDEP_SUBCLASSES          8UL
61
62 /*
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.)
66  */
67 struct lockdep_subclass_key {
68         char __one_byte;
69 } __attribute__ ((__packed__));
70
71 struct lock_class_key {
72         struct lockdep_subclass_key     subkeys[MAX_LOCKDEP_SUBCLASSES];
73 };
74
75 /*
76  * The lock-class itself:
77  */
78 struct lock_class {
79         /*
80          * class-hash:
81          */
82         struct list_head                hash_entry;
83
84         /*
85          * global list of all lock-classes:
86          */
87         struct list_head                lock_entry;
88
89         struct lockdep_subclass_key     *key;
90         unsigned int                    subclass;
91
92         /*
93          * IRQ/softirq usage tracking bits:
94          */
95         unsigned long                   usage_mask;
96         struct stack_trace              usage_traces[LOCK_USAGE_STATES];
97
98         /*
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.
102          */
103         struct list_head                locks_after, locks_before;
104
105         /*
106          * Generation counter, when doing certain classes of graph walking,
107          * to ensure that we check one node only once:
108          */
109         unsigned int                    version;
110
111         /*
112          * Statistics counter:
113          */
114         unsigned long                   ops;
115
116         const char                      *name;
117         int                             name_version;
118
119 #ifdef CONFIG_LOCK_STAT
120         unsigned long                   contention_point[4];
121 #endif
122 };
123
124 #ifdef CONFIG_LOCK_STAT
125 struct lock_time {
126         s64                             min;
127         s64                             max;
128         s64                             total;
129         unsigned long                   nr;
130 };
131
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;
138 };
139
140 struct lock_class_stats lock_stats(struct lock_class *class);
141 void clear_lock_stats(struct lock_class *class);
142 #endif
143
144 /*
145  * Map the lock object (the lock instance) to the lock-class object.
146  * This is embedded into specific lock instances:
147  */
148 struct lockdep_map {
149         struct lock_class_key           *key;
150         struct lock_class               *class_cache;
151         const char                      *name;
152 };
153
154 /*
155  * Every lock has a list of other locks that were taken after it.
156  * We only grow the list, never remove from it:
157  */
158 struct lock_list {
159         struct list_head                entry;
160         struct lock_class               *class;
161         struct stack_trace              trace;
162         int                             distance;
163 };
164
165 /*
166  * We record lock dependency chains, so that we can cache them:
167  */
168 struct lock_chain {
169         struct list_head                entry;
170         u64                             chain_key;
171 };
172
173 struct held_lock {
174         /*
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.
177          *
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.
184          *
185          * The task struct holds the current hash value (initialized
186          * with zero), here we store the previous hash value:
187          */
188         u64                             prev_chain_key;
189         struct lock_class               *class;
190         unsigned long                   acquire_ip;
191         struct lockdep_map              *instance;
192
193 #ifdef CONFIG_LOCK_STAT
194         u64                             waittime_stamp;
195         u64                             holdtime_stamp;
196 #endif
197         /*
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.]
206          *
207          * The following field is used to detect when we cross into an
208          * interrupt context:
209          */
210         int                             irq_context;
211         int                             trylock;
212         int                             read;
213         int                             check;
214         int                             hardirqs_off;
215 };
216
217 /*
218  * Initialization, self-test and debugging-output methods:
219  */
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);
225
226 extern void lockdep_off(void);
227 extern void lockdep_on(void);
228
229 /*
230  * These methods are used by specific locking variants (spinlocks,
231  * rwlocks, mutexes and rwsems) to pass init/acquire/release events
232  * to lockdep:
233  */
234
235 extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
236                              struct lock_class_key *key, int subclass);
237
238 /*
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):
243  */
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)
253
254 /*
255  * Acquire a lock.
256  *
257  * Values for "read":
258  *
259  *   0: exclusive (write) acquire
260  *   1: read-acquire (no recursion allowed)
261  *   2: read-acquire with same-instance recursion allowed
262  *
263  * Values for check:
264  *
265  *   0: disabled
266  *   1: simple checks (freeing, held-at-exit-time, etc.)
267  *   2: full validation
268  */
269 extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
270                          int trylock, int read, int check, unsigned long ip);
271
272 extern void lock_release(struct lockdep_map *lock, int nested,
273                          unsigned long ip);
274
275 # define INIT_LOCKDEP                           .lockdep_recursion = 0,
276
277 #define lockdep_depth(tsk)      (debug_locks ? (tsk)->lockdep_depth : 0)
278
279 #else /* !LOCKDEP */
280
281 static inline void lockdep_off(void)
282 {
283 }
284
285 static inline void lockdep_on(void)
286 {
287 }
288
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)
300
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)
304 /*
305  * The class key takes no space if lockdep is disabled:
306  */
307 struct lock_class_key { };
308
309 #define lockdep_depth(tsk)      (0)
310
311 #endif /* !LOCKDEP */
312
313 #ifdef CONFIG_LOCK_STAT
314
315 extern void lock_contended(struct lockdep_map *lock, unsigned long ip);
316 extern void lock_acquired(struct lockdep_map *lock);
317
318 #define LOCK_CONTENDED(_lock, try, lock)                        \
319 do {                                                            \
320         if (!try(_lock)) {                                      \
321                 lock_contended(&(_lock)->dep_map, _RET_IP_);    \
322                 lock(_lock);                                    \
323                 lock_acquired(&(_lock)->dep_map);               \
324         }                                                       \
325 } while (0)
326
327 #else /* CONFIG_LOCK_STAT */
328
329 #define lock_contended(lockdep_map, ip) do {} while (0)
330 #define lock_acquired(lockdep_map) do {} while (0)
331
332 #define LOCK_CONTENDED(_lock, try, lock) \
333         lock(_lock)
334
335 #endif /* CONFIG_LOCK_STAT */
336
337 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_GENERIC_HARDIRQS)
338 extern void early_init_irq_lock_class(void);
339 #else
340 static inline void early_init_irq_lock_class(void)
341 {
342 }
343 #endif
344
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);
349 #else
350 static inline void early_boot_irqs_off(void)
351 {
352 }
353 static inline void early_boot_irqs_on(void)
354 {
355 }
356 static inline void print_irqtrace_events(struct task_struct *curr)
357 {
358 }
359 #endif
360
361 /*
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.)
365  */
366 #define SINGLE_DEPTH_NESTING                    1
367
368 /*
369  * Map the dependency ops to NOP or to real lockdep ops, depending
370  * on the per lock-class debug mode:
371  */
372
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)
376 # else
377 #  define spin_acquire(l, s, t, i)              lock_acquire(l, s, t, 0, 1, i)
378 # endif
379 # define spin_release(l, n, i)                  lock_release(l, n, i)
380 #else
381 # define spin_acquire(l, s, t, i)               do { } while (0)
382 # define spin_release(l, n, i)                  do { } while (0)
383 #endif
384
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)
389 # else
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)
392 # endif
393 # define rwlock_release(l, n, i)                lock_release(l, n, i)
394 #else
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)
398 #endif
399
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)
403 # else
404 #  define mutex_acquire(l, s, t, i)             lock_acquire(l, s, t, 0, 1, i)
405 # endif
406 # define mutex_release(l, n, i)                 lock_release(l, n, i)
407 #else
408 # define mutex_acquire(l, s, t, i)              do { } while (0)
409 # define mutex_release(l, n, i)                 do { } while (0)
410 #endif
411
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)
416 # else
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)
419 # endif
420 # define rwsem_release(l, n, i)                 lock_release(l, n, i)
421 #else
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)
425 #endif
426
427 #endif /* __LINUX_LOCKDEP_H */