lockdep.h 14.0 KB
Newer Older
I
Ingo Molnar 已提交
1 2 3
/*
 * Runtime locking correctness validator
 *
P
Peter Zijlstra 已提交
4 5
 *  Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
 *  Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
I
Ingo Molnar 已提交
6 7 8 9 10 11
 *
 * see Documentation/lockdep-design.txt for more details.
 */
#ifndef __LINUX_LOCKDEP_H
#define __LINUX_LOCKDEP_H

12
struct task_struct;
P
Peter Zijlstra 已提交
13
struct lockdep_map;
14

15 16
#ifdef CONFIG_LOCKDEP

I
Ingo Molnar 已提交
17 18 19 20 21 22
#include <linux/linkage.h>
#include <linux/list.h>
#include <linux/debug_locks.h>
#include <linux/stacktrace.h>

/*
23 24
 * We'd rather not expose kernel/lockdep_states.h this wide, but we do need
 * the total number of states... :-(
I
Ingo Molnar 已提交
25
 */
26
#define XXX_LOCK_USAGE_STATES		(1+3*4)
I
Ingo Molnar 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#define MAX_LOCKDEP_SUBCLASSES		8UL

/*
 * Lock-classes are keyed via unique addresses, by embedding the
 * lockclass-key into the kernel (or module) .data section. (For
 * static locks we use the lock address itself as the key.)
 */
struct lockdep_subclass_key {
	char __one_byte;
} __attribute__ ((__packed__));

struct lock_class_key {
	struct lockdep_subclass_key	subkeys[MAX_LOCKDEP_SUBCLASSES];
};

P
Peter Zijlstra 已提交
43 44
#define LOCKSTAT_POINTS		4

I
Ingo Molnar 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/*
 * The lock-class itself:
 */
struct lock_class {
	/*
	 * class-hash:
	 */
	struct list_head		hash_entry;

	/*
	 * global list of all lock-classes:
	 */
	struct list_head		lock_entry;

	struct lockdep_subclass_key	*key;
	unsigned int			subclass;
61
	unsigned int			dep_gen_id;
I
Ingo Molnar 已提交
62 63 64 65 66

	/*
	 * IRQ/softirq usage tracking bits:
	 */
	unsigned long			usage_mask;
67
	struct stack_trace		usage_traces[XXX_LOCK_USAGE_STATES];
I
Ingo Molnar 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

	/*
	 * These fields represent a directed graph of lock dependencies,
	 * to every node we attach a list of "forward" and a list of
	 * "backward" graph nodes.
	 */
	struct list_head		locks_after, locks_before;

	/*
	 * Generation counter, when doing certain classes of graph walking,
	 * to ensure that we check one node only once:
	 */
	unsigned int			version;

	/*
	 * Statistics counter:
	 */
	unsigned long			ops;

	const char			*name;
	int				name_version;
P
Peter Zijlstra 已提交
89 90

#ifdef CONFIG_LOCK_STAT
P
Peter Zijlstra 已提交
91 92
	unsigned long			contention_point[LOCKSTAT_POINTS];
	unsigned long			contending_point[LOCKSTAT_POINTS];
P
Peter Zijlstra 已提交
93 94 95 96 97 98 99 100 101
#endif
};

#ifdef CONFIG_LOCK_STAT
struct lock_time {
	s64				min;
	s64				max;
	s64				total;
	unsigned long			nr;
I
Ingo Molnar 已提交
102 103
};

P
Peter Zijlstra 已提交
104 105 106 107 108 109 110 111 112 113 114
enum bounce_type {
	bounce_acquired_write,
	bounce_acquired_read,
	bounce_contended_write,
	bounce_contended_read,
	nr_bounce_types,

	bounce_acquired = bounce_acquired_write,
	bounce_contended = bounce_contended_write,
};

P
Peter Zijlstra 已提交
115 116
struct lock_class_stats {
	unsigned long			contention_point[4];
P
Peter Zijlstra 已提交
117
	unsigned long			contending_point[4];
P
Peter Zijlstra 已提交
118 119 120 121
	struct lock_time		read_waittime;
	struct lock_time		write_waittime;
	struct lock_time		read_holdtime;
	struct lock_time		write_holdtime;
P
Peter Zijlstra 已提交
122
	unsigned long			bounces[nr_bounce_types];
P
Peter Zijlstra 已提交
123 124 125 126 127 128
};

struct lock_class_stats lock_stats(struct lock_class *class);
void clear_lock_stats(struct lock_class *class);
#endif

I
Ingo Molnar 已提交
129 130 131 132 133 134
/*
 * Map the lock object (the lock instance) to the lock-class object.
 * This is embedded into specific lock instances:
 */
struct lockdep_map {
	struct lock_class_key		*key;
135
	struct lock_class		*class_cache;
I
Ingo Molnar 已提交
136
	const char			*name;
P
Peter Zijlstra 已提交
137 138
#ifdef CONFIG_LOCK_STAT
	int				cpu;
P
Peter Zijlstra 已提交
139
	unsigned long			ip;
P
Peter Zijlstra 已提交
140
#endif
I
Ingo Molnar 已提交
141 142 143 144 145 146 147 148 149 150
};

/*
 * Every lock has a list of other locks that were taken after it.
 * We only grow the list, never remove from it:
 */
struct lock_list {
	struct list_head		entry;
	struct lock_class		*class;
	struct stack_trace		trace;
151
	int				distance;
I
Ingo Molnar 已提交
152 153 154 155 156 157
};

/*
 * We record lock dependency chains, so that we can cache them:
 */
struct lock_chain {
158 159 160
	u8				irq_context;
	u8				depth;
	u16				base;
I
Ingo Molnar 已提交
161 162 163 164
	struct list_head		entry;
	u64				chain_key;
};

I
Ingo Molnar 已提交
165
#define MAX_LOCKDEP_KEYS_BITS		13
166 167 168 169 170 171
/*
 * Subtract one because we offset hlock->class_idx by 1 in order
 * to make 0 mean no class. This avoids overflowing the class_idx
 * bitfield and hitting the BUG in hlock_class().
 */
#define MAX_LOCKDEP_KEYS		((1UL << MAX_LOCKDEP_KEYS_BITS) - 1)
D
Dave Jones 已提交
172

I
Ingo Molnar 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
struct held_lock {
	/*
	 * One-way hash of the dependency chain up to this point. We
	 * hash the hashes step by step as the dependency chain grows.
	 *
	 * We use it for dependency-caching and we skip detection
	 * passes and dependency-updates if there is a cache-hit, so
	 * it is absolutely critical for 100% coverage of the validator
	 * to have a unique key value for every unique dependency path
	 * that can occur in the system, to make a unique hash value
	 * as likely as possible - hence the 64-bit width.
	 *
	 * The task struct holds the current hash value (initialized
	 * with zero), here we store the previous hash value:
	 */
	u64				prev_chain_key;
	unsigned long			acquire_ip;
	struct lockdep_map		*instance;
P
Peter Zijlstra 已提交
191
	struct lockdep_map		*nest_lock;
P
Peter Zijlstra 已提交
192 193 194 195
#ifdef CONFIG_LOCK_STAT
	u64 				waittime_stamp;
	u64				holdtime_stamp;
#endif
D
Dave Jones 已提交
196
	unsigned int			class_idx:MAX_LOCKDEP_KEYS_BITS;
I
Ingo Molnar 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209
	/*
	 * The lock-stack is unified in that the lock chains of interrupt
	 * contexts nest ontop of process context chains, but we 'separate'
	 * the hashes by starting with 0 if we cross into an interrupt
	 * context, and we also keep do not add cross-context lock
	 * dependencies - the lock usage graph walking covers that area
	 * anyway, and we'd just unnecessarily increase the number of
	 * dependencies otherwise. [Note: hardirq and softirq contexts
	 * are separated from each other too.]
	 *
	 * The following field is used to detect when we cross into an
	 * interrupt context:
	 */
D
Dave Jones 已提交
210 211 212 213 214
	unsigned int irq_context:2; /* bit 0 - soft, bit 1 - hard */
	unsigned int trylock:1;
	unsigned int read:2;        /* see lock_acquire() comment */
	unsigned int check:2;       /* see lock_acquire() comment */
	unsigned int hardirqs_off:1;
I
Ingo Molnar 已提交
215 216 217 218 219 220 221 222 223 224
};

/*
 * Initialization, self-test and debugging-output methods:
 */
extern void lockdep_init(void);
extern void lockdep_info(void);
extern void lockdep_reset(void);
extern void lockdep_reset_lock(struct lockdep_map *lock);
extern void lockdep_free_key_range(void *start, unsigned long size);
P
Peter Zijlstra 已提交
225
extern void lockdep_sys_exit(void);
I
Ingo Molnar 已提交
226 227 228 229 230 231 232 233 234 235 236

extern void lockdep_off(void);
extern void lockdep_on(void);

/*
 * These methods are used by specific locking variants (spinlocks,
 * rwlocks, mutexes and rwsems) to pass init/acquire/release events
 * to lockdep:
 */

extern void lockdep_init_map(struct lockdep_map *lock, const char *name,
237
			     struct lock_class_key *key, int subclass);
I
Ingo Molnar 已提交
238

239 240 241 242 243 244 245
/*
 * To initialize a lockdep_map statically use this macro.
 * Note that _name must not be NULL.
 */
#define STATIC_LOCKDEP_MAP_INIT(_name, _key) \
	{ .name = (_name), .key = (void *)(_key), }

I
Ingo Molnar 已提交
246 247 248 249 250 251 252
/*
 * Reinitialize a lock key - for cases where there is special locking or
 * special initialization of locks so that the validator gets the scope
 * of dependencies wrong: they are either too broad (they need a class-split)
 * or they are too narrow (they suffer from a false class-split):
 */
#define lockdep_set_class(lock, key) \
253
		lockdep_init_map(&(lock)->dep_map, #key, key, 0)
I
Ingo Molnar 已提交
254
#define lockdep_set_class_and_name(lock, key, name) \
255 256 257 258 259 260
		lockdep_init_map(&(lock)->dep_map, name, key, 0)
#define lockdep_set_class_and_subclass(lock, key, sub) \
		lockdep_init_map(&(lock)->dep_map, #key, key, sub)
#define lockdep_set_subclass(lock, sub)	\
		lockdep_init_map(&(lock)->dep_map, #lock, \
				 (lock)->dep_map.key, sub)
I
Ingo Molnar 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

/*
 * Acquire a lock.
 *
 * Values for "read":
 *
 *   0: exclusive (write) acquire
 *   1: read-acquire (no recursion allowed)
 *   2: read-acquire with same-instance recursion allowed
 *
 * Values for check:
 *
 *   0: disabled
 *   1: simple checks (freeing, held-at-exit-time, etc.)
 *   2: full validation
 */
extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
P
Peter Zijlstra 已提交
278 279
			 int trylock, int read, int check,
			 struct lockdep_map *nest_lock, unsigned long ip);
I
Ingo Molnar 已提交
280 281 282 283

extern void lock_release(struct lockdep_map *lock, int nested,
			 unsigned long ip);

284 285 286 287 288 289 290 291 292
extern void lock_set_class(struct lockdep_map *lock, const char *name,
			   struct lock_class_key *key, unsigned int subclass,
			   unsigned long ip);

static inline void lock_set_subclass(struct lockdep_map *lock,
		unsigned int subclass, unsigned long ip)
{
	lock_set_class(lock, lock->name, lock->key, subclass, ip);
}
293

294 295 296 297 298
extern void lockdep_set_current_reclaim_state(gfp_t gfp_mask);
extern void lockdep_clear_current_reclaim_state(void);
extern void lockdep_trace_alloc(gfp_t mask);

# define INIT_LOCKDEP				.lockdep_recursion = 0, .lockdep_reclaim_gfp = 0,
I
Ingo Molnar 已提交
299

300
#define lockdep_depth(tsk)	(debug_locks ? (tsk)->lockdep_depth : 0)
301

I
Ingo Molnar 已提交
302 303 304 305 306 307 308 309 310 311
#else /* !LOCKDEP */

static inline void lockdep_off(void)
{
}

static inline void lockdep_on(void)
{
}

P
Peter Zijlstra 已提交
312
# define lock_acquire(l, s, t, r, c, n, i)	do { } while (0)
I
Ingo Molnar 已提交
313
# define lock_release(l, n, i)			do { } while (0)
314
# define lock_set_class(l, n, k, s, i)		do { } while (0)
315
# define lock_set_subclass(l, s, i)		do { } while (0)
316 317 318
# define lockdep_set_current_reclaim_state(g)	do { } while (0)
# define lockdep_clear_current_reclaim_state()	do { } while (0)
# define lockdep_trace_alloc(g)			do { } while (0)
I
Ingo Molnar 已提交
319 320
# define lockdep_init()				do { } while (0)
# define lockdep_info()				do { } while (0)
321 322
# define lockdep_init_map(lock, name, key, sub) \
		do { (void)(name); (void)(key); } while (0)
I
Ingo Molnar 已提交
323 324
# define lockdep_set_class(lock, key)		do { (void)(key); } while (0)
# define lockdep_set_class_and_name(lock, key, name) \
325
		do { (void)(key); (void)(name); } while (0)
326 327
#define lockdep_set_class_and_subclass(lock, key, sub) \
		do { (void)(key); } while (0)
328 329
#define lockdep_set_subclass(lock, sub)		do { } while (0)

I
Ingo Molnar 已提交
330 331 332
# define INIT_LOCKDEP
# define lockdep_reset()		do { debug_locks = 1; } while (0)
# define lockdep_free_key_range(start, size)	do { } while (0)
P
Peter Zijlstra 已提交
333
# define lockdep_sys_exit() 			do { } while (0)
I
Ingo Molnar 已提交
334 335 336 337
/*
 * The class key takes no space if lockdep is disabled:
 */
struct lock_class_key { };
338 339 340

#define lockdep_depth(tsk)	(0)

I
Ingo Molnar 已提交
341 342
#endif /* !LOCKDEP */

P
Peter Zijlstra 已提交
343 344 345
#ifdef CONFIG_LOCK_STAT

extern void lock_contended(struct lockdep_map *lock, unsigned long ip);
P
Peter Zijlstra 已提交
346
extern void lock_acquired(struct lockdep_map *lock, unsigned long ip);
P
Peter Zijlstra 已提交
347 348 349 350 351 352 353

#define LOCK_CONTENDED(_lock, try, lock)			\
do {								\
	if (!try(_lock)) {					\
		lock_contended(&(_lock)->dep_map, _RET_IP_);	\
		lock(_lock);					\
	}							\
P
Peter Zijlstra 已提交
354
	lock_acquired(&(_lock)->dep_map, _RET_IP_);			\
P
Peter Zijlstra 已提交
355 356 357 358 359
} while (0)

#else /* CONFIG_LOCK_STAT */

#define lock_contended(lockdep_map, ip) do {} while (0)
P
Peter Zijlstra 已提交
360
#define lock_acquired(lockdep_map, ip) do {} while (0)
P
Peter Zijlstra 已提交
361 362 363 364 365 366

#define LOCK_CONTENDED(_lock, try, lock) \
	lock(_lock)

#endif /* CONFIG_LOCK_STAT */

367
#ifdef CONFIG_GENERIC_HARDIRQS
I
Ingo Molnar 已提交
368 369
extern void early_init_irq_lock_class(void);
#else
370 371 372
static inline void early_init_irq_lock_class(void)
{
}
I
Ingo Molnar 已提交
373 374
#endif

I
Ingo Molnar 已提交
375 376 377
#ifdef CONFIG_TRACE_IRQFLAGS
extern void early_boot_irqs_off(void);
extern void early_boot_irqs_on(void);
378
extern void print_irqtrace_events(struct task_struct *curr);
I
Ingo Molnar 已提交
379
#else
380 381 382 383 384 385 386 387 388
static inline void early_boot_irqs_off(void)
{
}
static inline void early_boot_irqs_on(void)
{
}
static inline void print_irqtrace_events(struct task_struct *curr)
{
}
I
Ingo Molnar 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
#endif

/*
 * For trivial one-depth nesting of a lock-class, the following
 * global define can be used. (Subsystems with multiple levels
 * of nesting should define their own lock-nesting subclasses.)
 */
#define SINGLE_DEPTH_NESTING			1

/*
 * Map the dependency ops to NOP or to real lockdep ops, depending
 * on the per lock-class debug mode:
 */

#ifdef CONFIG_DEBUG_LOCK_ALLOC
# ifdef CONFIG_PROVE_LOCKING
P
Peter Zijlstra 已提交
405
#  define spin_acquire(l, s, t, i)		lock_acquire(l, s, t, 0, 2, NULL, i)
P
Peter Zijlstra 已提交
406
#  define spin_acquire_nest(l, s, t, n, i)	lock_acquire(l, s, t, 0, 2, n, i)
I
Ingo Molnar 已提交
407
# else
P
Peter Zijlstra 已提交
408
#  define spin_acquire(l, s, t, i)		lock_acquire(l, s, t, 0, 1, NULL, i)
P
Peter Zijlstra 已提交
409
#  define spin_acquire_nest(l, s, t, n, i)	lock_acquire(l, s, t, 0, 1, NULL, i)
I
Ingo Molnar 已提交
410 411 412 413 414 415 416 417 418
# endif
# define spin_release(l, n, i)			lock_release(l, n, i)
#else
# define spin_acquire(l, s, t, i)		do { } while (0)
# define spin_release(l, n, i)			do { } while (0)
#endif

#ifdef CONFIG_DEBUG_LOCK_ALLOC
# ifdef CONFIG_PROVE_LOCKING
P
Peter Zijlstra 已提交
419 420
#  define rwlock_acquire(l, s, t, i)		lock_acquire(l, s, t, 0, 2, NULL, i)
#  define rwlock_acquire_read(l, s, t, i)	lock_acquire(l, s, t, 2, 2, NULL, i)
I
Ingo Molnar 已提交
421
# else
P
Peter Zijlstra 已提交
422 423
#  define rwlock_acquire(l, s, t, i)		lock_acquire(l, s, t, 0, 1, NULL, i)
#  define rwlock_acquire_read(l, s, t, i)	lock_acquire(l, s, t, 2, 1, NULL, i)
I
Ingo Molnar 已提交
424 425 426 427 428 429 430 431 432 433
# endif
# define rwlock_release(l, n, i)		lock_release(l, n, i)
#else
# define rwlock_acquire(l, s, t, i)		do { } while (0)
# define rwlock_acquire_read(l, s, t, i)	do { } while (0)
# define rwlock_release(l, n, i)		do { } while (0)
#endif

#ifdef CONFIG_DEBUG_LOCK_ALLOC
# ifdef CONFIG_PROVE_LOCKING
P
Peter Zijlstra 已提交
434
#  define mutex_acquire(l, s, t, i)		lock_acquire(l, s, t, 0, 2, NULL, i)
I
Ingo Molnar 已提交
435
# else
P
Peter Zijlstra 已提交
436
#  define mutex_acquire(l, s, t, i)		lock_acquire(l, s, t, 0, 1, NULL, i)
I
Ingo Molnar 已提交
437 438 439 440 441 442 443 444 445
# endif
# define mutex_release(l, n, i)			lock_release(l, n, i)
#else
# define mutex_acquire(l, s, t, i)		do { } while (0)
# define mutex_release(l, n, i)			do { } while (0)
#endif

#ifdef CONFIG_DEBUG_LOCK_ALLOC
# ifdef CONFIG_PROVE_LOCKING
P
Peter Zijlstra 已提交
446 447
#  define rwsem_acquire(l, s, t, i)		lock_acquire(l, s, t, 0, 2, NULL, i)
#  define rwsem_acquire_read(l, s, t, i)	lock_acquire(l, s, t, 1, 2, NULL, i)
I
Ingo Molnar 已提交
448
# else
P
Peter Zijlstra 已提交
449 450
#  define rwsem_acquire(l, s, t, i)		lock_acquire(l, s, t, 0, 1, NULL, i)
#  define rwsem_acquire_read(l, s, t, i)	lock_acquire(l, s, t, 1, 1, NULL, i)
I
Ingo Molnar 已提交
451 452 453 454 455 456 457 458
# endif
# define rwsem_release(l, n, i)			lock_release(l, n, i)
#else
# define rwsem_acquire(l, s, t, i)		do { } while (0)
# define rwsem_acquire_read(l, s, t, i)		do { } while (0)
# define rwsem_release(l, n, i)			do { } while (0)
#endif

P
Peter Zijlstra 已提交
459 460
#ifdef CONFIG_DEBUG_LOCK_ALLOC
# ifdef CONFIG_PROVE_LOCKING
461
#  define lock_map_acquire(l)		lock_acquire(l, 0, 0, 0, 2, NULL, _THIS_IP_)
P
Peter Zijlstra 已提交
462
# else
463
#  define lock_map_acquire(l)		lock_acquire(l, 0, 0, 0, 1, NULL, _THIS_IP_)
P
Peter Zijlstra 已提交
464
# endif
465
# define lock_map_release(l)			lock_release(l, 1, _THIS_IP_)
P
Peter Zijlstra 已提交
466
#else
467 468
# define lock_map_acquire(l)			do { } while (0)
# define lock_map_release(l)			do { } while (0)
P
Peter Zijlstra 已提交
469 470
#endif

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
#ifdef CONFIG_PROVE_LOCKING
# define might_lock(lock) 						\
do {									\
	typecheck(struct lockdep_map *, &(lock)->dep_map);		\
	lock_acquire(&(lock)->dep_map, 0, 0, 0, 2, NULL, _THIS_IP_);	\
	lock_release(&(lock)->dep_map, 0, _THIS_IP_);			\
} while (0)
# define might_lock_read(lock) 						\
do {									\
	typecheck(struct lockdep_map *, &(lock)->dep_map);		\
	lock_acquire(&(lock)->dep_map, 0, 0, 1, 2, NULL, _THIS_IP_);	\
	lock_release(&(lock)->dep_map, 0, _THIS_IP_);			\
} while (0)
#else
# define might_lock(lock) do { } while (0)
# define might_lock_read(lock) do { } while (0)
#endif

I
Ingo Molnar 已提交
489
#endif /* __LINUX_LOCKDEP_H */