fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / 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
13 #ifdef CONFIG_LOCKDEP
14
15 #include <linux/linkage.h>
16 #include <linux/list.h>
17 #include <linux/debug_locks.h>
18 #include <linux/stacktrace.h>
19
20 /*
21  * Lock-class usage-state bits:
22  */
23 enum lock_usage_bit
24 {
25         LOCK_USED = 0,
26         LOCK_USED_IN_HARDIRQ,
27         LOCK_USED_IN_SOFTIRQ,
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,
34         LOCK_USAGE_STATES
35 };
36
37 /*
38  * Usage-state bitmasks:
39  */
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)
45
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)
48
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)
53
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)
58
59 #define MAX_LOCKDEP_SUBCLASSES          8UL
60
61 /*
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.)
65  */
66 struct lockdep_subclass_key {
67         char __one_byte;
68 } __attribute__ ((__packed__));
69
70 struct lock_class_key {
71         struct lockdep_subclass_key     subkeys[MAX_LOCKDEP_SUBCLASSES];
72 };
73
74 /*
75  * The lock-class itself:
76  */
77 struct lock_class {
78         /*
79          * class-hash:
80          */
81         struct list_head                hash_entry;
82
83         /*
84          * global list of all lock-classes:
85          */
86         struct list_head                lock_entry;
87
88         struct lockdep_subclass_key     *key;
89         unsigned int                    subclass;
90
91         /*
92          * IRQ/softirq usage tracking bits:
93          */
94         unsigned long                   usage_mask;
95         struct stack_trace              usage_traces[LOCK_USAGE_STATES];
96
97         /*
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.
101          */
102         struct list_head                locks_after, locks_before;
103
104         /*
105          * Generation counter, when doing certain classes of graph walking,
106          * to ensure that we check one node only once:
107          */
108         unsigned int                    version;
109
110         /*
111          * Statistics counter:
112          */
113         unsigned long                   ops;
114
115         const char                      *name;
116         int                             name_version;
117 };
118
119 /*
120  * Map the lock object (the lock instance) to the lock-class object.
121  * This is embedded into specific lock instances:
122  */
123 struct lockdep_map {
124         struct lock_class_key           *key;
125         struct lock_class               *class_cache;
126         const char                      *name;
127 };
128
129 /*
130  * Every lock has a list of other locks that were taken after it.
131  * We only grow the list, never remove from it:
132  */
133 struct lock_list {
134         struct list_head                entry;
135         struct lock_class               *class;
136         struct stack_trace              trace;
137 };
138
139 /*
140  * We record lock dependency chains, so that we can cache them:
141  */
142 struct lock_chain {
143         struct list_head                entry;
144         u64                             chain_key;
145 };
146
147 struct held_lock {
148         /*
149          * One-way hash of the dependency chain up to this point. We
150          * hash the hashes step by step as the dependency chain grows.
151          *
152          * We use it for dependency-caching and we skip detection
153          * passes and dependency-updates if there is a cache-hit, so
154          * it is absolutely critical for 100% coverage of the validator
155          * to have a unique key value for every unique dependency path
156          * that can occur in the system, to make a unique hash value
157          * as likely as possible - hence the 64-bit width.
158          *
159          * The task struct holds the current hash value (initialized
160          * with zero), here we store the previous hash value:
161          */
162         u64                             prev_chain_key;
163         struct lock_class               *class;
164         unsigned long                   acquire_ip;
165         struct lockdep_map              *instance;
166
167         /*
168          * The lock-stack is unified in that the lock chains of interrupt
169          * contexts nest ontop of process context chains, but we 'separate'
170          * the hashes by starting with 0 if we cross into an interrupt
171          * context, and we also keep do not add cross-context lock
172          * dependencies - the lock usage graph walking covers that area
173          * anyway, and we'd just unnecessarily increase the number of
174          * dependencies otherwise. [Note: hardirq and softirq contexts
175          * are separated from each other too.]
176          *
177          * The following field is used to detect when we cross into an
178          * interrupt context:
179          */
180         int                             irq_context;
181         int                             trylock;
182         int                             read;
183         int                             check;
184         int                             hardirqs_off;
185 };
186
187 /*
188  * Initialization, self-test and debugging-output methods:
189  */
190 extern void lockdep_init(void);
191 extern void lockdep_info(void);
192 extern void lockdep_reset(void);
193 extern void lockdep_reset_lock(struct lockdep_map *lock);
194 extern void lockdep_free_key_range(void *start, unsigned long size);
195
196 extern void lockdep_off(void);
197 extern void lockdep_on(void);
198
199 /*
200  * These methods are used by specific locking variants (spinlocks,
201  * rwlocks, mutexes and rwsems) to pass init/acquire/release events
202  * to lockdep:
203  */
204
205 extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
206                              struct lock_class_key *key, int subclass);
207
208 /*
209  * Reinitialize a lock key - for cases where there is special locking or
210  * special initialization of locks so that the validator gets the scope
211  * of dependencies wrong: they are either too broad (they need a class-split)
212  * or they are too narrow (they suffer from a false class-split):
213  */
214 #define lockdep_set_class(lock, key) \
215                 lockdep_init_map(&(lock)->dep_map, #key, key, 0)
216 #define lockdep_set_class_and_name(lock, key, name) \
217                 lockdep_init_map(&(lock)->dep_map, name, key, 0)
218 #define lockdep_set_class_and_subclass(lock, key, sub) \
219                 lockdep_init_map(&(lock)->dep_map, #key, key, sub)
220 #define lockdep_set_subclass(lock, sub) \
221                 lockdep_init_map(&(lock)->dep_map, #lock, \
222                                  (lock)->dep_map.key, sub)
223
224 /*
225  * Acquire a lock.
226  *
227  * Values for "read":
228  *
229  *   0: exclusive (write) acquire
230  *   1: read-acquire (no recursion allowed)
231  *   2: read-acquire with same-instance recursion allowed
232  *
233  * Values for check:
234  *
235  *   0: disabled
236  *   1: simple checks (freeing, held-at-exit-time, etc.)
237  *   2: full validation
238  */
239 extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
240                          int trylock, int read, int check, unsigned long ip);
241
242 extern void lock_release(struct lockdep_map *lock, int nested,
243                          unsigned long ip);
244
245 # define INIT_LOCKDEP                           .lockdep_recursion = 0,
246
247 #define lockdep_depth(tsk)      ((tsk)->lockdep_depth)
248
249 #else /* !LOCKDEP */
250
251 static inline void lockdep_off(void)
252 {
253 }
254
255 static inline void lockdep_on(void)
256 {
257 }
258
259 # define lock_acquire(l, s, t, r, c, i)         do { } while (0)
260 # define lock_release(l, n, i)                  do { } while (0)
261 # define lockdep_init()                         do { } while (0)
262 # define lockdep_info()                         do { } while (0)
263 # define lockdep_init_map(lock, name, key, sub) do { (void)(key); } while (0)
264 # define lockdep_set_class(lock, key)           do { (void)(key); } while (0)
265 # define lockdep_set_class_and_name(lock, key, name) \
266                 do { (void)(key); } while (0)
267 #define lockdep_set_class_and_subclass(lock, key, sub) \
268                 do { (void)(key); } while (0)
269 #define lockdep_set_subclass(lock, sub)         do { } while (0)
270
271 # define INIT_LOCKDEP
272 # define lockdep_reset()                do { debug_locks = 1; } while (0)
273 # define lockdep_free_key_range(start, size)    do { } while (0)
274 /*
275  * The class key takes no space if lockdep is disabled:
276  */
277 struct lock_class_key { };
278
279 #define lockdep_depth(tsk)      (0)
280
281 #endif /* !LOCKDEP */
282
283 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_GENERIC_HARDIRQS)
284 extern void early_init_irq_lock_class(void);
285 #else
286 static inline void early_init_irq_lock_class(void)
287 {
288 }
289 #endif
290
291 #ifdef CONFIG_TRACE_IRQFLAGS
292 extern void early_boot_irqs_off(void);
293 extern void early_boot_irqs_on(void);
294 extern void print_irqtrace_events(struct task_struct *curr);
295 #else
296 static inline void early_boot_irqs_off(void)
297 {
298 }
299 static inline void early_boot_irqs_on(void)
300 {
301 }
302 static inline void print_irqtrace_events(struct task_struct *curr)
303 {
304 }
305 #endif
306
307 /*
308  * For trivial one-depth nesting of a lock-class, the following
309  * global define can be used. (Subsystems with multiple levels
310  * of nesting should define their own lock-nesting subclasses.)
311  */
312 #define SINGLE_DEPTH_NESTING                    1
313
314 /*
315  * Map the dependency ops to NOP or to real lockdep ops, depending
316  * on the per lock-class debug mode:
317  */
318
319 #ifdef CONFIG_DEBUG_LOCK_ALLOC
320 # ifdef CONFIG_PROVE_LOCKING
321 #  define spin_acquire(l, s, t, i)              lock_acquire(l, s, t, 0, 2, i)
322 # else
323 #  define spin_acquire(l, s, t, i)              lock_acquire(l, s, t, 0, 1, i)
324 # endif
325 # define spin_release(l, n, i)                  lock_release(l, n, i)
326 #else
327 # define spin_acquire(l, s, t, i)               do { } while (0)
328 # define spin_release(l, n, i)                  do { } while (0)
329 #endif
330
331 #ifdef CONFIG_DEBUG_LOCK_ALLOC
332 # ifdef CONFIG_PROVE_LOCKING
333 #  define rwlock_acquire(l, s, t, i)            lock_acquire(l, s, t, 0, 2, i)
334 #  define rwlock_acquire_read(l, s, t, i)       lock_acquire(l, s, t, 2, 2, i)
335 # else
336 #  define rwlock_acquire(l, s, t, i)            lock_acquire(l, s, t, 0, 1, i)
337 #  define rwlock_acquire_read(l, s, t, i)       lock_acquire(l, s, t, 2, 1, i)
338 # endif
339 # define rwlock_release(l, n, i)                lock_release(l, n, i)
340 #else
341 # define rwlock_acquire(l, s, t, i)             do { } while (0)
342 # define rwlock_acquire_read(l, s, t, i)        do { } while (0)
343 # define rwlock_release(l, n, i)                do { } while (0)
344 #endif
345
346 #ifdef CONFIG_DEBUG_LOCK_ALLOC
347 # ifdef CONFIG_PROVE_LOCKING
348 #  define mutex_acquire(l, s, t, i)             lock_acquire(l, s, t, 0, 2, i)
349 # else
350 #  define mutex_acquire(l, s, t, i)             lock_acquire(l, s, t, 0, 1, i)
351 # endif
352 # define mutex_release(l, n, i)                 lock_release(l, n, i)
353 #else
354 # define mutex_acquire(l, s, t, i)              do { } while (0)
355 # define mutex_release(l, n, i)                 do { } while (0)
356 #endif
357
358 #ifdef CONFIG_DEBUG_LOCK_ALLOC
359 # ifdef CONFIG_PROVE_LOCKING
360 #  define rwsem_acquire(l, s, t, i)             lock_acquire(l, s, t, 0, 2, i)
361 #  define rwsem_acquire_read(l, s, t, i)        lock_acquire(l, s, t, 1, 2, i)
362 # else
363 #  define rwsem_acquire(l, s, t, i)             lock_acquire(l, s, t, 0, 1, i)
364 #  define rwsem_acquire_read(l, s, t, i)        lock_acquire(l, s, t, 1, 1, i)
365 # endif
366 # define rwsem_release(l, n, i)                 lock_release(l, n, i)
367 #else
368 # define rwsem_acquire(l, s, t, i)              do { } while (0)
369 # define rwsem_acquire_read(l, s, t, i)         do { } while (0)
370 # define rwsem_release(l, n, i)                 do { } while (0)
371 #endif
372
373 #endif /* __LINUX_LOCKDEP_H */