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

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

D
Dave Young 已提交
16 17 18 19
/* for sysctl */
extern int prove_locking;
extern int lock_stat;

20 21
#define MAX_LOCKDEP_SUBCLASSES		8UL

22 23
#include <linux/types.h>

24 25
#ifdef CONFIG_LOCKDEP

I
Ingo Molnar 已提交
26 27 28 29 30 31
#include <linux/linkage.h>
#include <linux/list.h>
#include <linux/debug_locks.h>
#include <linux/stacktrace.h>

/*
32 33
 * We'd rather not expose kernel/lockdep_states.h this wide, but we do need
 * the total number of states... :-(
I
Ingo Molnar 已提交
34
 */
35
#define XXX_LOCK_USAGE_STATES		(1+2*4)
I
Ingo Molnar 已提交
36

37 38 39 40 41 42 43 44 45 46 47
/*
 * NR_LOCKDEP_CACHING_CLASSES ... Number of classes
 * cached in the instance of lockdep_map
 *
 * Currently main class (subclass == 0) and signle depth subclass
 * are cached in lockdep_map. This optimization is mainly targeting
 * on rq->lock. double_rq_lock() acquires this highly competitive with
 * single depth.
 */
#define NR_LOCKDEP_CACHING_CLASSES	2

I
Ingo Molnar 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60
/*
 * 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];
};

61 62
extern struct lock_class_key __lockdep_no_validate__;

P
Peter Zijlstra 已提交
63 64
#define LOCKSTAT_POINTS		4

I
Ingo Molnar 已提交
65 66 67 68 69 70 71
/*
 * The lock-class itself:
 */
struct lock_class {
	/*
	 * class-hash:
	 */
72
	struct hlist_node		hash_entry;
I
Ingo Molnar 已提交
73 74 75 76 77 78 79 80

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

	struct lockdep_subclass_key	*key;
	unsigned int			subclass;
81
	unsigned int			dep_gen_id;
I
Ingo Molnar 已提交
82 83 84 85 86

	/*
	 * IRQ/softirq usage tracking bits:
	 */
	unsigned long			usage_mask;
87
	struct stack_trace		usage_traces[XXX_LOCK_USAGE_STATES];
I
Ingo Molnar 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

	/*
	 * 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 已提交
109 110

#ifdef CONFIG_LOCK_STAT
P
Peter Zijlstra 已提交
111 112
	unsigned long			contention_point[LOCKSTAT_POINTS];
	unsigned long			contending_point[LOCKSTAT_POINTS];
P
Peter Zijlstra 已提交
113 114 115 116 117 118 119 120 121
#endif
};

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

P
Peter Zijlstra 已提交
124 125 126 127 128 129 130 131 132 133 134
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 已提交
135
struct lock_class_stats {
136 137
	unsigned long			contention_point[LOCKSTAT_POINTS];
	unsigned long			contending_point[LOCKSTAT_POINTS];
P
Peter Zijlstra 已提交
138 139 140 141
	struct lock_time		read_waittime;
	struct lock_time		write_waittime;
	struct lock_time		read_holdtime;
	struct lock_time		write_holdtime;
P
Peter Zijlstra 已提交
142
	unsigned long			bounces[nr_bounce_types];
P
Peter Zijlstra 已提交
143 144 145 146 147 148
};

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

I
Ingo Molnar 已提交
149 150 151 152 153 154
/*
 * 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;
155
	struct lock_class		*class_cache[NR_LOCKDEP_CACHING_CLASSES];
I
Ingo Molnar 已提交
156
	const char			*name;
P
Peter Zijlstra 已提交
157 158
#ifdef CONFIG_LOCK_STAT
	int				cpu;
P
Peter Zijlstra 已提交
159
	unsigned long			ip;
P
Peter Zijlstra 已提交
160
#endif
161 162 163 164 165 166
#ifdef CONFIG_LOCKDEP_CROSSRELEASE
	/*
	 * Whether it's a crosslock.
	 */
	int				cross;
#endif
I
Ingo Molnar 已提交
167 168
};

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
static inline void lockdep_copy_map(struct lockdep_map *to,
				    struct lockdep_map *from)
{
	int i;

	*to = *from;
	/*
	 * Since the class cache can be modified concurrently we could observe
	 * half pointers (64bit arch using 32bit copy insns). Therefore clear
	 * the caches and take the performance hit.
	 *
	 * XXX it doesn't work well with lockdep_set_class_and_subclass(), since
	 *     that relies on cache abuse.
	 */
	for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
		to->class_cache[i] = NULL;
}

I
Ingo Molnar 已提交
187 188 189 190 191 192 193 194
/*
 * 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;
195
	int				distance;
196

P
Peter Zijlstra 已提交
197 198 199
	/*
	 * The parent field is used to implement breadth-first search, and the
	 * bit 0 is reused to indicate if the lock has been accessed in BFS.
200 201
	 */
	struct lock_list		*parent;
I
Ingo Molnar 已提交
202 203 204 205 206 207
};

/*
 * We record lock dependency chains, so that we can cache them:
 */
struct lock_chain {
208 209 210 211 212
	/* see BUILD_BUG_ON()s in lookup_chain_cache() */
	unsigned int			irq_context :  2,
					depth       :  6,
					base	    : 24;
	/* 4 byte hole */
213
	struct hlist_node		entry;
I
Ingo Molnar 已提交
214 215 216
	u64				chain_key;
};

I
Ingo Molnar 已提交
217
#define MAX_LOCKDEP_KEYS_BITS		13
218 219 220 221 222 223
/*
 * 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 已提交
224

I
Ingo Molnar 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
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 已提交
243
	struct lockdep_map		*nest_lock;
P
Peter Zijlstra 已提交
244 245 246 247
#ifdef CONFIG_LOCK_STAT
	u64 				waittime_stamp;
	u64				holdtime_stamp;
#endif
D
Dave Jones 已提交
248
	unsigned int			class_idx:MAX_LOCKDEP_KEYS_BITS;
I
Ingo Molnar 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261
	/*
	 * 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 已提交
262
	unsigned int irq_context:2; /* bit 0 - soft, bit 1 - hard */
263 264
	unsigned int trylock:1;						/* 16 bits */

D
Dave Jones 已提交
265
	unsigned int read:2;        /* see lock_acquire() comment */
266
	unsigned int check:1;       /* see lock_acquire() comment */
D
Dave Jones 已提交
267
	unsigned int hardirqs_off:1;
268
	unsigned int references:12;					/* 32 bits */
P
Peter Zijlstra 已提交
269
	unsigned int pin_count;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
#ifdef CONFIG_LOCKDEP_CROSSRELEASE
	/*
	 * Generation id.
	 *
	 * A value of cross_gen_id will be stored when holding this,
	 * which is globally increased whenever each crosslock is held.
	 */
	unsigned int gen_id;
#endif
};

#ifdef CONFIG_LOCKDEP_CROSSRELEASE
#define MAX_XHLOCK_TRACE_ENTRIES 5

/*
 * This is for keeping locks waiting for commit so that true dependencies
 * can be added at commit step.
 */
struct hist_lock {
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	/*
	 * Id for each entry in the ring buffer. This is used to
	 * decide whether the ring buffer was overwritten or not.
	 *
	 * For example,
	 *
	 *           |<----------- hist_lock ring buffer size ------->|
	 *           pppppppppppppppppppppiiiiiiiiiiiiiiiiiiiiiiiiiiiii
	 * wrapped > iiiiiiiiiiiiiiiiiiiiiiiiiii.......................
	 *
	 *           where 'p' represents an acquisition in process
	 *           context, 'i' represents an acquisition in irq
	 *           context.
	 *
	 * In this example, the ring buffer was overwritten by
	 * acquisitions in irq context, that should be detected on
	 * rollback or commit.
	 */
	unsigned int hist_id;

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	/*
	 * Seperate stack_trace data. This will be used at commit step.
	 */
	struct stack_trace	trace;
	unsigned long		trace_entries[MAX_XHLOCK_TRACE_ENTRIES];

	/*
	 * Seperate hlock instance. This will be used at commit step.
	 *
	 * TODO: Use a smaller data structure containing only necessary
	 * data. However, we should make lockdep code able to handle the
	 * smaller one first.
	 */
	struct held_lock	hlock;
};

/*
 * To initialize a lock as crosslock, lockdep_init_map_crosslock() should
 * be called instead of lockdep_init_map().
 */
struct cross_lock {
330 331 332 333 334 335 336 337 338 339 340 341 342
	/*
	 * When more than one acquisition of crosslocks are overlapped,
	 * we have to perform commit for them based on cross_gen_id of
	 * the first acquisition, which allows us to add more true
	 * dependencies.
	 *
	 * Moreover, when no acquisition of a crosslock is in progress,
	 * we should not perform commit because the lock might not exist
	 * any more, which might cause incorrect memory access. So we
	 * have to track the number of acquisitions of a crosslock.
	 */
	int nr_acquire;

343 344 345 346 347 348 349 350 351 352 353 354 355
	/*
	 * Seperate hlock instance. This will be used at commit step.
	 *
	 * TODO: Use a smaller data structure containing only necessary
	 * data. However, we should make lockdep code able to handle the
	 * smaller one first.
	 */
	struct held_lock	hlock;
};

struct lockdep_map_cross {
	struct lockdep_map map;
	struct cross_lock xlock;
I
Ingo Molnar 已提交
356
};
357
#endif
I
Ingo Molnar 已提交
358 359 360 361 362 363 364 365

/*
 * Initialization, self-test and debugging-output methods:
 */
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);
366
extern asmlinkage void lockdep_sys_exit(void);
I
Ingo Molnar 已提交
367 368 369 370 371 372 373 374 375 376 377

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,
378
			     struct lock_class_key *key, int subclass);
I
Ingo Molnar 已提交
379 380 381 382 383 384 385 386

/*
 * 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) \
387
		lockdep_init_map(&(lock)->dep_map, #key, key, 0)
I
Ingo Molnar 已提交
388
#define lockdep_set_class_and_name(lock, key, name) \
389 390 391 392 393 394
		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)
395 396

#define lockdep_set_novalidate_class(lock) \
397
	lockdep_set_class_and_name(lock, &__lockdep_no_validate__, #lock)
398 399 400 401 402 403 404 405 406 407
/*
 * Compare locking classes
 */
#define lockdep_match_class(lock, key) lockdep_match_key(&(lock)->dep_map, key)

static inline int lockdep_match_key(struct lockdep_map *lock,
				    struct lock_class_key *key)
{
	return lock->key == key;
}
I
Ingo Molnar 已提交
408 409 410 411 412 413 414 415 416 417 418 419

/*
 * 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:
 *
420 421
 *   0: simple checks (freeing, held-at-exit-time, etc.)
 *   1: full validation
I
Ingo Molnar 已提交
422 423
 */
extern void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
P
Peter Zijlstra 已提交
424 425
			 int trylock, int read, int check,
			 struct lockdep_map *nest_lock, unsigned long ip);
I
Ingo Molnar 已提交
426 427 428 429

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

430 431 432 433 434 435 436 437 438
/*
 * Same "read" as for lock_acquire(), except -1 means any.
 */
extern int lock_is_held_type(struct lockdep_map *lock, int read);

static inline int lock_is_held(struct lockdep_map *lock)
{
	return lock_is_held_type(lock, -1);
}
439

440 441
#define lockdep_is_held(lock)		lock_is_held(&(lock)->dep_map)
#define lockdep_is_held_type(lock, r)	lock_is_held_type(&(lock)->dep_map, (r))
442

443 444 445 446 447 448 449 450 451
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);
}
452

453 454
extern void lock_downgrade(struct lockdep_map *lock, unsigned long ip);

455 456 457 458 459 460 461
struct pin_cookie { unsigned int val; };

#define NIL_COOKIE (struct pin_cookie){ .val = 0U, }

extern struct pin_cookie lock_pin_lock(struct lockdep_map *lock);
extern void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie);
extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
P
Peter Zijlstra 已提交
462

463
# define INIT_LOCKDEP				.lockdep_recursion = 0,
I
Ingo Molnar 已提交
464

465
#define lockdep_depth(tsk)	(debug_locks ? (tsk)->lockdep_depth : 0)
466

467 468 469
#define lockdep_assert_held(l)	do {				\
		WARN_ON(debug_locks && !lockdep_is_held(l));	\
	} while (0)
470

471 472 473 474 475 476 477 478
#define lockdep_assert_held_exclusive(l)	do {			\
		WARN_ON(debug_locks && !lockdep_is_held_type(l, 0));	\
	} while (0)

#define lockdep_assert_held_read(l)	do {				\
		WARN_ON(debug_locks && !lockdep_is_held_type(l, 1));	\
	} while (0)

479 480 481 482
#define lockdep_assert_held_once(l)	do {				\
		WARN_ON_ONCE(debug_locks && !lockdep_is_held(l));	\
	} while (0)

483 484
#define lockdep_recursing(tsk)	((tsk)->lockdep_recursion)

485 486 487
#define lockdep_pin_lock(l)	lock_pin_lock(&(l)->dep_map)
#define lockdep_repin_lock(l,c)	lock_repin_lock(&(l)->dep_map, (c))
#define lockdep_unpin_lock(l,c)	lock_unpin_lock(&(l)->dep_map, (c))
P
Peter Zijlstra 已提交
488

489
#else /* !CONFIG_LOCKDEP */
I
Ingo Molnar 已提交
490 491 492 493 494 495 496 497 498

static inline void lockdep_off(void)
{
}

static inline void lockdep_on(void)
{
}

P
Peter Zijlstra 已提交
499
# define lock_acquire(l, s, t, r, c, n, i)	do { } while (0)
I
Ingo Molnar 已提交
500
# define lock_release(l, n, i)			do { } while (0)
501
# define lock_downgrade(l, i)			do { } while (0)
502
# define lock_set_class(l, n, k, s, i)		do { } while (0)
503
# define lock_set_subclass(l, s, i)		do { } while (0)
I
Ingo Molnar 已提交
504
# define lockdep_info()				do { } while (0)
505 506
# define lockdep_init_map(lock, name, key, sub) \
		do { (void)(name); (void)(key); } while (0)
I
Ingo Molnar 已提交
507 508
# define lockdep_set_class(lock, key)		do { (void)(key); } while (0)
# define lockdep_set_class_and_name(lock, key, name) \
509
		do { (void)(key); (void)(name); } while (0)
510 511
#define lockdep_set_class_and_subclass(lock, key, sub) \
		do { (void)(key); } while (0)
512
#define lockdep_set_subclass(lock, sub)		do { } while (0)
513 514 515

#define lockdep_set_novalidate_class(lock) do { } while (0)

516 517 518 519 520
/*
 * We don't define lockdep_match_class() and lockdep_match_key() for !LOCKDEP
 * case since the result is not well defined and the caller should rather
 * #ifdef the call himself.
 */
521

I
Ingo Molnar 已提交
522 523 524
# 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 已提交
525
# define lockdep_sys_exit() 			do { } while (0)
I
Ingo Molnar 已提交
526 527 528 529
/*
 * The class key takes no space if lockdep is disabled:
 */
struct lock_class_key { };
530

531 532 533 534 535
/*
 * The lockdep_map takes no space if lockdep is disabled:
 */
struct lockdep_map { };

536 537
#define lockdep_depth(tsk)	(0)

538 539
#define lockdep_is_held_type(l, r)		(1)

540
#define lockdep_assert_held(l)			do { (void)(l); } while (0)
541 542
#define lockdep_assert_held_exclusive(l)	do { (void)(l); } while (0)
#define lockdep_assert_held_read(l)		do { (void)(l); } while (0)
543
#define lockdep_assert_held_once(l)		do { (void)(l); } while (0)
544

545 546
#define lockdep_recursing(tsk)			(0)

547 548 549 550 551 552 553
struct pin_cookie { };

#define NIL_COOKIE (struct pin_cookie){ }

#define lockdep_pin_lock(l)			({ struct pin_cookie cookie; cookie; })
#define lockdep_repin_lock(l, c)		do { (void)(l); (void)(c); } while (0)
#define lockdep_unpin_lock(l, c)		do { (void)(l); (void)(c); } while (0)
P
Peter Zijlstra 已提交
554

I
Ingo Molnar 已提交
555 556
#endif /* !LOCKDEP */

557 558 559 560 561 562 563 564 565 566 567 568 569
enum xhlock_context_t {
	XHLOCK_HARD,
	XHLOCK_SOFT,
	XHLOCK_CTX_NR,
};

#ifdef CONFIG_LOCKDEP_CROSSRELEASE
extern void lockdep_init_map_crosslock(struct lockdep_map *lock,
				       const char *name,
				       struct lock_class_key *key,
				       int subclass);
extern void lock_commit_crosslock(struct lockdep_map *lock);

570 571 572 573 574 575 576
/*
 * What we essencially have to initialize is 'nr_acquire'. Other members
 * will be initialized in add_xlock().
 */
#define STATIC_CROSS_LOCK_INIT() \
	{ .nr_acquire = 0,}

577 578
#define STATIC_CROSS_LOCKDEP_MAP_INIT(_name, _key) \
	{ .map.name = (_name), .map.key = (void *)(_key), \
579
	  .map.cross = 1, .xlock = STATIC_CROSS_LOCK_INIT(), }
580 581 582 583 584 585 586 587

/*
 * 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), .cross = 0, }

588
extern void crossrelease_hist_start(enum xhlock_context_t c);
589
extern void crossrelease_hist_end(enum xhlock_context_t c);
590
extern void lockdep_invariant_state(bool force);
591 592
extern void lockdep_init_task(struct task_struct *task);
extern void lockdep_free_task(struct task_struct *task);
593
#else /* !CROSSRELEASE */
594
#define lockdep_init_map_crosslock(m, n, k, s) do {} while (0)
595 596 597 598 599 600 601
/*
 * 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), }

602
static inline void crossrelease_hist_start(enum xhlock_context_t c) {}
603
static inline void crossrelease_hist_end(enum xhlock_context_t c) {}
604
static inline void lockdep_invariant_state(bool force) {}
605 606
static inline void lockdep_init_task(struct task_struct *task) {}
static inline void lockdep_free_task(struct task_struct *task) {}
607
#endif /* CROSSRELEASE */
608

P
Peter Zijlstra 已提交
609 610 611
#ifdef CONFIG_LOCK_STAT

extern void lock_contended(struct lockdep_map *lock, unsigned long ip);
P
Peter Zijlstra 已提交
612
extern void lock_acquired(struct lockdep_map *lock, unsigned long ip);
P
Peter Zijlstra 已提交
613 614 615 616 617 618 619

#define LOCK_CONTENDED(_lock, try, lock)			\
do {								\
	if (!try(_lock)) {					\
		lock_contended(&(_lock)->dep_map, _RET_IP_);	\
		lock(_lock);					\
	}							\
P
Peter Zijlstra 已提交
620
	lock_acquired(&(_lock)->dep_map, _RET_IP_);			\
P
Peter Zijlstra 已提交
621 622
} while (0)

623 624 625 626 627 628 629 630 631 632 633 634
#define LOCK_CONTENDED_RETURN(_lock, try, lock)			\
({								\
	int ____err = 0;					\
	if (!try(_lock)) {					\
		lock_contended(&(_lock)->dep_map, _RET_IP_);	\
		____err = lock(_lock);				\
	}							\
	if (!____err)						\
		lock_acquired(&(_lock)->dep_map, _RET_IP_);	\
	____err;						\
})

P
Peter Zijlstra 已提交
635 636 637
#else /* CONFIG_LOCK_STAT */

#define lock_contended(lockdep_map, ip) do {} while (0)
P
Peter Zijlstra 已提交
638
#define lock_acquired(lockdep_map, ip) do {} while (0)
P
Peter Zijlstra 已提交
639 640 641 642

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

643 644 645
#define LOCK_CONTENDED_RETURN(_lock, try, lock) \
	lock(_lock)

P
Peter Zijlstra 已提交
646 647
#endif /* CONFIG_LOCK_STAT */

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
#ifdef CONFIG_LOCKDEP

/*
 * On lockdep we dont want the hand-coded irq-enable of
 * _raw_*_lock_flags() code, because lockdep assumes
 * that interrupts are not re-enabled during lock-acquire:
 */
#define LOCK_CONTENDED_FLAGS(_lock, try, lock, lockfl, flags) \
	LOCK_CONTENDED((_lock), (try), (lock))

#else /* CONFIG_LOCKDEP */

#define LOCK_CONTENDED_FLAGS(_lock, try, lock, lockfl, flags) \
	lockfl((_lock), (flags))

#endif /* CONFIG_LOCKDEP */

I
Ingo Molnar 已提交
665
#ifdef CONFIG_TRACE_IRQFLAGS
666
extern void print_irqtrace_events(struct task_struct *curr);
I
Ingo Molnar 已提交
667
#else
668 669 670
static inline void print_irqtrace_events(struct task_struct *curr)
{
}
I
Ingo Molnar 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684
#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:
 */

685 686 687
#define lock_acquire_exclusive(l, s, t, n, i)		lock_acquire(l, s, t, 0, 1, n, i)
#define lock_acquire_shared(l, s, t, n, i)		lock_acquire(l, s, t, 1, 1, n, i)
#define lock_acquire_shared_recursive(l, s, t, n, i)	lock_acquire(l, s, t, 2, 1, n, i)
I
Ingo Molnar 已提交
688

689 690 691
#define spin_acquire(l, s, t, i)		lock_acquire_exclusive(l, s, t, NULL, i)
#define spin_acquire_nest(l, s, t, n, i)	lock_acquire_exclusive(l, s, t, n, i)
#define spin_release(l, n, i)			lock_release(l, n, i)
I
Ingo Molnar 已提交
692

693 694 695
#define rwlock_acquire(l, s, t, i)		lock_acquire_exclusive(l, s, t, NULL, i)
#define rwlock_acquire_read(l, s, t, i)		lock_acquire_shared_recursive(l, s, t, NULL, i)
#define rwlock_release(l, n, i)			lock_release(l, n, i)
I
Ingo Molnar 已提交
696

697 698 699 700
#define seqcount_acquire(l, s, t, i)		lock_acquire_exclusive(l, s, t, NULL, i)
#define seqcount_acquire_read(l, s, t, i)	lock_acquire_shared_recursive(l, s, t, NULL, i)
#define seqcount_release(l, n, i)		lock_release(l, n, i)

701 702 703 704 705 706 707
#define mutex_acquire(l, s, t, i)		lock_acquire_exclusive(l, s, t, NULL, i)
#define mutex_acquire_nest(l, s, t, n, i)	lock_acquire_exclusive(l, s, t, n, i)
#define mutex_release(l, n, i)			lock_release(l, n, i)

#define rwsem_acquire(l, s, t, i)		lock_acquire_exclusive(l, s, t, NULL, i)
#define rwsem_acquire_nest(l, s, t, n, i)	lock_acquire_exclusive(l, s, t, n, i)
#define rwsem_acquire_read(l, s, t, i)		lock_acquire_shared(l, s, t, NULL, i)
708
#define rwsem_release(l, n, i)			lock_release(l, n, i)
I
Ingo Molnar 已提交
709

710 711
#define lock_map_acquire(l)			lock_acquire_exclusive(l, 0, 0, NULL, _THIS_IP_)
#define lock_map_acquire_read(l)		lock_acquire_shared_recursive(l, 0, 0, NULL, _THIS_IP_)
712
#define lock_map_acquire_tryread(l)		lock_acquire_shared_recursive(l, 0, 1, NULL, _THIS_IP_)
713
#define lock_map_release(l)			lock_release(l, 1, _THIS_IP_)
P
Peter Zijlstra 已提交
714

715 716 717 718
#ifdef CONFIG_PROVE_LOCKING
# define might_lock(lock) 						\
do {									\
	typecheck(struct lockdep_map *, &(lock)->dep_map);		\
719
	lock_acquire(&(lock)->dep_map, 0, 0, 0, 1, NULL, _THIS_IP_);	\
720 721 722 723 724
	lock_release(&(lock)->dep_map, 0, _THIS_IP_);			\
} while (0)
# define might_lock_read(lock) 						\
do {									\
	typecheck(struct lockdep_map *, &(lock)->dep_map);		\
725
	lock_acquire(&(lock)->dep_map, 0, 0, 1, 1, NULL, _THIS_IP_);	\
726 727 728 729 730 731 732
	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

733
#ifdef CONFIG_LOCKDEP
734
void lockdep_rcu_suspicious(const char *file, const int line, const char *s);
735 736 737 738 739
#else
static inline void
lockdep_rcu_suspicious(const char *file, const int line, const char *s)
{
}
740 741
#endif

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