1c839423a328590377fa5eb889848be002e194fe
[sliver-openvswitch.git] / datapath / linux-2.6 / compat-2.6 / include / linux / lockdep.h
1 /*
2  * Runtime locking correctness validator
3  *
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>
6  *
7  * see Documentation/lockdep-design.txt for more details.
8  */
9 #ifndef __LINUX_LOCKDEP_WRAPPER_H
10 #define __LINUX_LOCKDEP_WRAPPER_H
11
12 #include_next <linux/lockdep.h>
13
14 #include <linux/version.h>
15 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
16
17 struct task_struct;
18 struct lockdep_map;
19
20 #ifdef CONFIG_LOCKDEP
21
22 #include <linux/linkage.h>
23 #include <linux/list.h>
24 #include <linux/debug_locks.h>
25 #include <linux/stacktrace.h>
26
27 /*
28  * Lock-class usage-state bits:
29  */
30 enum lock_usage_bit
31 {
32         LOCK_USED = 0,
33         LOCK_USED_IN_HARDIRQ,
34         LOCK_USED_IN_SOFTIRQ,
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,
41         LOCK_USAGE_STATES
42 };
43
44 /*
45  * Usage-state bitmasks:
46  */
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)
52
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)
55
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)
60
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)
65
66 #define MAX_LOCKDEP_SUBCLASSES          8UL
67
68 /*
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.)
72  */
73 struct lockdep_subclass_key {
74         char __one_byte;
75 } __attribute__ ((__packed__));
76
77 struct lock_class_key {
78         struct lockdep_subclass_key     subkeys[MAX_LOCKDEP_SUBCLASSES];
79 };
80
81 /*
82  * The lock-class itself:
83  */
84 struct lock_class {
85         /*
86          * class-hash:
87          */
88         struct list_head                hash_entry;
89
90         /*
91          * global list of all lock-classes:
92          */
93         struct list_head                lock_entry;
94
95         struct lockdep_subclass_key     *key;
96         unsigned int                    subclass;
97
98         /*
99          * IRQ/softirq usage tracking bits:
100          */
101         unsigned long                   usage_mask;
102         struct stack_trace              usage_traces[LOCK_USAGE_STATES];
103
104         /*
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.
108          */
109         struct list_head                locks_after, locks_before;
110
111         /*
112          * Generation counter, when doing certain classes of graph walking,
113          * to ensure that we check one node only once:
114          */
115         unsigned int                    version;
116
117         /*
118          * Statistics counter:
119          */
120         unsigned long                   ops;
121
122         const char                      *name;
123         int                             name_version;
124
125 #ifdef CONFIG_LOCK_STAT
126         unsigned long                   contention_point[4];
127 #endif
128 };
129
130 #ifdef CONFIG_LOCK_STAT
131 struct lock_time {
132         s64                             min;
133         s64                             max;
134         s64                             total;
135         unsigned long                   nr;
136 };
137
138 enum bounce_type {
139         bounce_acquired_write,
140         bounce_acquired_read,
141         bounce_contended_write,
142         bounce_contended_read,
143         nr_bounce_types,
144
145         bounce_acquired = bounce_acquired_write,
146         bounce_contended = bounce_contended_write,
147 };
148
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];
156 };
157
158 struct lock_class_stats lock_stats(struct lock_class *class);
159 void clear_lock_stats(struct lock_class *class);
160 #endif
161
162 /*
163  * Map the lock object (the lock instance) to the lock-class object.
164  * This is embedded into specific lock instances:
165  */
166 struct lockdep_map {
167         struct lock_class_key           *key;
168         struct lock_class               *class_cache;
169         const char                      *name;
170 #ifdef CONFIG_LOCK_STAT
171         int                             cpu;
172 #endif
173 };
174
175 /*
176  * Every lock has a list of other locks that were taken after it.
177  * We only grow the list, never remove from it:
178  */
179 struct lock_list {
180         struct list_head                entry;
181         struct lock_class               *class;
182         struct stack_trace              trace;
183         int                             distance;
184 };
185
186 /*
187  * We record lock dependency chains, so that we can cache them:
188  */
189 struct lock_chain {
190         struct list_head                entry;
191         u64                             chain_key;
192 };
193
194 struct held_lock {
195         /*
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.
198          *
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.
205          *
206          * The task struct holds the current hash value (initialized
207          * with zero), here we store the previous hash value:
208          */
209         u64                             prev_chain_key;
210         struct lock_class               *class;
211         unsigned long                   acquire_ip;
212         struct lockdep_map              *instance;
213
214 #ifdef CONFIG_LOCK_STAT
215         u64                             waittime_stamp;
216         u64                             holdtime_stamp;
217 #endif
218         /*
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.]
227          *
228          * The following field is used to detect when we cross into an
229          * interrupt context:
230          */
231         int                             irq_context;
232         int                             trylock;
233         int                             read;
234         int                             check;
235         int                             hardirqs_off;
236 };
237
238 /*
239  * Initialization, self-test and debugging-output methods:
240  */
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);
246
247 extern void lockdep_off(void);
248 extern void lockdep_on(void);
249
250 /*
251  * These methods are used by specific locking variants (spinlocks,
252  * rwlocks, mutexes and rwsems) to pass init/acquire/release events
253  * to lockdep:
254  */
255
256 extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
257                         struct lock_class_key *key, int subclass);
258
259 /*
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):
264  */
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)
274
275 /*
276  * Acquire a lock.
277  *
278  * Values for "read":
279  *
280  *   0: exclusive (write) acquire
281  *   1: read-acquire (no recursion allowed)
282  *   2: read-acquire with same-instance recursion allowed
283  *
284  * Values for check:
285  *
286  *   0: disabled
287  *   1: simple checks (freeing, held-at-exit-time, etc.)
288  *   2: full validation
289  */
290 extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
291                          int trylock, int read, int check, unsigned long ip);
292
293 extern void lock_release(struct lockdep_map *lock, int nested,
294                          unsigned long ip);
295
296 # define INIT_LOCKDEP                           .lockdep_recursion = 0,
297
298 #define lockdep_depth(tsk)      (debug_locks ? (tsk)->lockdep_depth : 0)
299
300 #else /* !LOCKDEP */
301
302 static inline void lockdep_off(void)
303 {
304 }
305
306 static inline void lockdep_on(void)
307 {
308 }
309
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)
321
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)
325 /*
326  * The class key takes no space if lockdep is disabled:
327  */
328 struct lock_class_key { };
329
330 #define lockdep_depth(tsk)      (0)
331
332 #endif /* !LOCKDEP */
333
334 #ifdef CONFIG_LOCK_STAT
335
336 extern void lock_contended(struct lockdep_map *lock, unsigned long ip);
337 extern void lock_acquired(struct lockdep_map *lock);
338
339 #define LOCK_CONTENDED(_lock, try, lock)                        \
340 do {                                                            \
341         if (!try(_lock)) {                                      \
342                 lock_contended(&(_lock)->dep_map, _RET_IP_);    \
343                 lock(_lock);                                    \
344         }                                                       \
345         lock_acquired(&(_lock)->dep_map);                       \
346 } while (0)
347
348 #else /* CONFIG_LOCK_STAT */
349
350 #define lock_contended(lockdep_map, ip) do {} while (0)
351 #define lock_acquired(lockdep_map) do {} while (0)
352
353 #define LOCK_CONTENDED(_lock, try, lock) \
354         lock(_lock)
355
356 #endif /* CONFIG_LOCK_STAT */
357
358 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_GENERIC_HARDIRQS)
359 extern void early_init_irq_lock_class(void);
360 #else
361 static inline void early_init_irq_lock_class(void)
362 {
363 }
364 #endif
365
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);
370 #else
371 static inline void early_boot_irqs_off(void)
372 {
373 }
374 static inline void early_boot_irqs_on(void)
375 {
376 }
377 static inline void print_irqtrace_events(struct task_struct *curr)
378 {
379 }
380 #endif
381
382 /*
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.)
386  */
387 #define SINGLE_DEPTH_NESTING                    1
388
389 /*
390  * Map the dependency ops to NOP or to real lockdep ops, depending
391  * on the per lock-class debug mode:
392  */
393
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)
397 # else
398 #  define spin_acquire(l, s, t, i)              lock_acquire(l, s, t, 0, 1, i)
399 # endif
400 # define spin_release(l, n, i)                  lock_release(l, n, i)
401 #else
402 # define spin_acquire(l, s, t, i)               do { } while (0)
403 # define spin_release(l, n, i)                  do { } while (0)
404 #endif
405
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)
410 # else
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)
413 # endif
414 # define rwlock_release(l, n, i)                lock_release(l, n, i)
415 #else
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)
419 #endif
420
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)
424 # else
425 #  define mutex_acquire(l, s, t, i)             lock_acquire(l, s, t, 0, 1, i)
426 # endif
427 # define mutex_release(l, n, i)                 lock_release(l, n, i)
428 #else
429 # define mutex_acquire(l, s, t, i)              do { } while (0)
430 # define mutex_release(l, n, i)                 do { } while (0)
431 #endif
432
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)
437 # else
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)
440 # endif
441 # define rwsem_release(l, n, i)                 lock_release(l, n, i)
442 #else
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)
446 #endif
447
448 #endif /* linux kernel < 2.6.18 */
449
450 #endif /* __LINUX_LOCKDEP_WRAPPER_H */