drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / lockdep.c
CommitLineData
fbb9ce95
IM
1/*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
4b32d0a4
PZ
8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
fbb9ce95
IM
10 *
11 * this code maps all the lock dependencies as they occur in a live kernel
12 * and will warn about the following classes of locking bugs:
13 *
6fa3eb70 14 * - lock inversion scenarios * - circular lock dependencies
fbb9ce95
IM
15 * - hardirq/softirq safe/unsafe locking bugs
16 *
17 * Bugs are reported even if the current locking scenario does not cause
18 * any deadlock at this point.
19 *
20 * I.e. if anytime in the past two locks were taken in a different order,
21 * even if it happened for another task, even if those were different
22 * locks (but of the same class as this lock), this code will detect it.
23 *
24 * Thanks to Arjan van de Ven for coming up with the initial idea of
25 * mapping lock dependencies runtime.
26 */
a5e25883 27#define DISABLE_BRANCH_PROFILING
fbb9ce95
IM
28#include <linux/mutex.h>
29#include <linux/sched.h>
30#include <linux/delay.h>
31#include <linux/module.h>
32#include <linux/proc_fs.h>
33#include <linux/seq_file.h>
34#include <linux/spinlock.h>
35#include <linux/kallsyms.h>
36#include <linux/interrupt.h>
37#include <linux/stacktrace.h>
38#include <linux/debug_locks.h>
39#include <linux/irqflags.h>
99de055a 40#include <linux/utsname.h>
4b32d0a4 41#include <linux/hash.h>
81d68a96 42#include <linux/ftrace.h>
b4b136f4 43#include <linux/stringify.h>
d588e461 44#include <linux/bitops.h>
5a0e3ad6 45#include <linux/gfp.h>
d3d03d4f 46#include <linux/kmemcheck.h>
6fa3eb70 47#include <linux/aee.h>
af012961 48
fbb9ce95
IM
49#include <asm/sections.h>
50
51#include "lockdep_internals.h"
52
a8d154b0 53#define CREATE_TRACE_POINTS
67178767 54#include <trace/events/lock.h>
a8d154b0 55
f20786ff
PZ
56#ifdef CONFIG_PROVE_LOCKING
57int prove_locking = 1;
58module_param(prove_locking, int, 0644);
59#else
60#define prove_locking 0
61#endif
62
63#ifdef CONFIG_LOCK_STAT
64int lock_stat = 1;
65module_param(lock_stat, int, 0644);
66#else
67#define lock_stat 0
68#endif
69
6fa3eb70
S
70static void lockdep_aee(void)
71{
72 char aee_str[40];
73 snprintf( aee_str, 40, "[%s]LockProve Warning", current->comm);
74 aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_DUMMY_DUMP | DB_OPT_FTRACE, aee_str,"LockProve Debug\n");
75
76}
77
fbb9ce95 78/*
74c383f1
IM
79 * lockdep_lock: protects the lockdep graph, the hashes and the
80 * class/list/hash allocators.
fbb9ce95
IM
81 *
82 * This is one of the rare exceptions where it's justified
83 * to use a raw spinlock - we really dont want the spinlock
74c383f1 84 * code to recurse back into the lockdep code...
fbb9ce95 85 */
edc35bd7 86static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
74c383f1
IM
87
88static int graph_lock(void)
89{
0199c4e6 90 arch_spin_lock(&lockdep_lock);
74c383f1
IM
91 /*
92 * Make sure that if another CPU detected a bug while
93 * walking the graph we dont change it (while the other
94 * CPU is busy printing out stuff with the graph lock
95 * dropped already)
96 */
97 if (!debug_locks) {
0199c4e6 98 arch_spin_unlock(&lockdep_lock);
74c383f1
IM
99 return 0;
100 }
bb065afb
SR
101 /* prevent any recursions within lockdep from causing deadlocks */
102 current->lockdep_recursion++;
74c383f1
IM
103 return 1;
104}
105
106static inline int graph_unlock(void)
107{
0119fee4
PZ
108 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
109 /*
110 * The lockdep graph lock isn't locked while we expect it to
111 * be, we're confused now, bye!
112 */
381a2292 113 return DEBUG_LOCKS_WARN_ON(1);
0119fee4 114 }
381a2292 115
bb065afb 116 current->lockdep_recursion--;
0199c4e6 117 arch_spin_unlock(&lockdep_lock);
74c383f1
IM
118 return 0;
119}
120
121/*
122 * Turn lock debugging off and return with 0 if it was off already,
123 * and also release the graph lock:
124 */
125static inline int debug_locks_off_graph_unlock(void)
126{
127 int ret = debug_locks_off();
128
0199c4e6 129 arch_spin_unlock(&lockdep_lock);
74c383f1
IM
130
131 return ret;
132}
fbb9ce95
IM
133
134static int lockdep_initialized;
135
136unsigned long nr_list_entries;
af012961 137static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
fbb9ce95 138
fbb9ce95
IM
139/*
140 * All data structures here are protected by the global debug_lock.
141 *
142 * Mutex key structs only get allocated, once during bootup, and never
143 * get freed - this significantly simplifies the debugging code.
144 */
145unsigned long nr_lock_classes;
146static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
147
f82b217e
DJ
148static inline struct lock_class *hlock_class(struct held_lock *hlock)
149{
150 if (!hlock->class_idx) {
0119fee4
PZ
151 /*
152 * Someone passed in garbage, we give up.
153 */
f82b217e
DJ
154 DEBUG_LOCKS_WARN_ON(1);
155 return NULL;
156 }
157 return lock_classes + hlock->class_idx - 1;
158}
159
f20786ff 160#ifdef CONFIG_LOCK_STAT
1871e52c
TH
161static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
162 cpu_lock_stats);
f20786ff 163
3365e779
PZ
164static inline u64 lockstat_clock(void)
165{
c676329a 166 return local_clock();
3365e779
PZ
167}
168
c7e78cff 169static int lock_point(unsigned long points[], unsigned long ip)
f20786ff
PZ
170{
171 int i;
172
c7e78cff
PZ
173 for (i = 0; i < LOCKSTAT_POINTS; i++) {
174 if (points[i] == 0) {
175 points[i] = ip;
f20786ff
PZ
176 break;
177 }
c7e78cff 178 if (points[i] == ip)
f20786ff
PZ
179 break;
180 }
181
182 return i;
183}
184
3365e779 185static void lock_time_inc(struct lock_time *lt, u64 time)
f20786ff
PZ
186{
187 if (time > lt->max)
188 lt->max = time;
189
109d71c6 190 if (time < lt->min || !lt->nr)
f20786ff
PZ
191 lt->min = time;
192
193 lt->total += time;
194 lt->nr++;
195}
196
c46261de
PZ
197static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
198{
109d71c6
FR
199 if (!src->nr)
200 return;
201
202 if (src->max > dst->max)
203 dst->max = src->max;
204
205 if (src->min < dst->min || !dst->nr)
206 dst->min = src->min;
207
c46261de
PZ
208 dst->total += src->total;
209 dst->nr += src->nr;
210}
211
212struct lock_class_stats lock_stats(struct lock_class *class)
213{
214 struct lock_class_stats stats;
215 int cpu, i;
216
217 memset(&stats, 0, sizeof(struct lock_class_stats));
218 for_each_possible_cpu(cpu) {
219 struct lock_class_stats *pcs =
1871e52c 220 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
c46261de
PZ
221
222 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
223 stats.contention_point[i] += pcs->contention_point[i];
224
c7e78cff
PZ
225 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
226 stats.contending_point[i] += pcs->contending_point[i];
227
c46261de
PZ
228 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
229 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
230
231 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
232 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
96645678
PZ
233
234 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
235 stats.bounces[i] += pcs->bounces[i];
c46261de
PZ
236 }
237
238 return stats;
239}
240
241void clear_lock_stats(struct lock_class *class)
242{
243 int cpu;
244
245 for_each_possible_cpu(cpu) {
246 struct lock_class_stats *cpu_stats =
1871e52c 247 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
c46261de
PZ
248
249 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
250 }
251 memset(class->contention_point, 0, sizeof(class->contention_point));
c7e78cff 252 memset(class->contending_point, 0, sizeof(class->contending_point));
c46261de
PZ
253}
254
f20786ff
PZ
255static struct lock_class_stats *get_lock_stats(struct lock_class *class)
256{
1871e52c 257 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
f20786ff
PZ
258}
259
260static void put_lock_stats(struct lock_class_stats *stats)
261{
1871e52c 262 put_cpu_var(cpu_lock_stats);
f20786ff
PZ
263}
264
265static void lock_release_holdtime(struct held_lock *hlock)
266{
267 struct lock_class_stats *stats;
3365e779 268 u64 holdtime;
f20786ff
PZ
269
270 if (!lock_stat)
271 return;
272
3365e779 273 holdtime = lockstat_clock() - hlock->holdtime_stamp;
f20786ff 274
f82b217e 275 stats = get_lock_stats(hlock_class(hlock));
f20786ff
PZ
276 if (hlock->read)
277 lock_time_inc(&stats->read_holdtime, holdtime);
278 else
279 lock_time_inc(&stats->write_holdtime, holdtime);
280 put_lock_stats(stats);
281}
282#else
283static inline void lock_release_holdtime(struct held_lock *hlock)
284{
285}
286#endif
287
fbb9ce95
IM
288/*
289 * We keep a global list of all lock classes. The list only grows,
290 * never shrinks. The list is only accessed with the lockdep
291 * spinlock lock held.
292 */
293LIST_HEAD(all_lock_classes);
294
295/*
296 * The lockdep classes are in a hash-table as well, for fast lookup:
297 */
298#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
299#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
4b32d0a4 300#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
fbb9ce95
IM
301#define classhashentry(key) (classhash_table + __classhashfn((key)))
302
303static struct list_head classhash_table[CLASSHASH_SIZE];
304
fbb9ce95
IM
305/*
306 * We put the lock dependency chains into a hash-table as well, to cache
307 * their existence:
308 */
309#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
310#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
4b32d0a4 311#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
fbb9ce95
IM
312#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
313
314static struct list_head chainhash_table[CHAINHASH_SIZE];
315
316/*
317 * The hash key of the lock dependency chains is a hash itself too:
318 * it's a hash of all locks taken up to that lock, including that lock.
319 * It's a 64-bit hash, because it's important for the keys to be
320 * unique.
321 */
322#define iterate_chain_key(key1, key2) \
03cbc358
IM
323 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
324 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
fbb9ce95
IM
325 (key2))
326
1d09daa5 327void lockdep_off(void)
fbb9ce95
IM
328{
329 current->lockdep_recursion++;
330}
fbb9ce95
IM
331EXPORT_SYMBOL(lockdep_off);
332
1d09daa5 333void lockdep_on(void)
fbb9ce95
IM
334{
335 current->lockdep_recursion--;
336}
fbb9ce95
IM
337EXPORT_SYMBOL(lockdep_on);
338
fbb9ce95
IM
339/*
340 * Debugging switches:
341 */
342
343#define VERBOSE 0
33e94e96 344#define VERY_VERBOSE 0
fbb9ce95
IM
345
346#if VERBOSE
347# define HARDIRQ_VERBOSE 1
348# define SOFTIRQ_VERBOSE 1
cf40bd16 349# define RECLAIM_VERBOSE 1
fbb9ce95
IM
350#else
351# define HARDIRQ_VERBOSE 0
352# define SOFTIRQ_VERBOSE 0
cf40bd16 353# define RECLAIM_VERBOSE 0
fbb9ce95
IM
354#endif
355
cf40bd16 356#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
fbb9ce95
IM
357/*
358 * Quick filtering for interesting events:
359 */
360static int class_filter(struct lock_class *class)
361{
f9829cce
AK
362#if 0
363 /* Example */
fbb9ce95 364 if (class->name_version == 1 &&
f9829cce 365 !strcmp(class->name, "lockname"))
fbb9ce95
IM
366 return 1;
367 if (class->name_version == 1 &&
f9829cce 368 !strcmp(class->name, "&struct->lockfield"))
fbb9ce95 369 return 1;
f9829cce 370#endif
a6640897
IM
371 /* Filter everything else. 1 would be to allow everything else */
372 return 0;
fbb9ce95
IM
373}
374#endif
375
376static int verbose(struct lock_class *class)
377{
378#if VERBOSE
379 return class_filter(class);
380#endif
381 return 0;
382}
383
fbb9ce95
IM
384/*
385 * Stack-trace: tightly packed array of stack backtrace
74c383f1 386 * addresses. Protected by the graph_lock.
fbb9ce95
IM
387 */
388unsigned long nr_stack_trace_entries;
389static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
390
2c522836
DJ
391static void print_lockdep_off(const char *bug_msg)
392{
393 printk(KERN_DEBUG "%s\n", bug_msg);
394 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
395 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
396}
397
fbb9ce95
IM
398static int save_trace(struct stack_trace *trace)
399{
400 trace->nr_entries = 0;
401 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
402 trace->entries = stack_trace + nr_stack_trace_entries;
403
5a1b3999 404 trace->skip = 3;
5a1b3999 405
ab1b6f03 406 save_stack_trace(trace);
fbb9ce95 407
4f84f433
PZ
408 /*
409 * Some daft arches put -1 at the end to indicate its a full trace.
410 *
411 * <rant> this is buggy anyway, since it takes a whole extra entry so a
412 * complete trace that maxes out the entries provided will be reported
413 * as incomplete, friggin useless </rant>
414 */
ea5b41f9
TL
415 if (trace->nr_entries != 0 &&
416 trace->entries[trace->nr_entries-1] == ULONG_MAX)
4f84f433
PZ
417 trace->nr_entries--;
418
fbb9ce95
IM
419 trace->max_entries = trace->nr_entries;
420
421 nr_stack_trace_entries += trace->nr_entries;
fbb9ce95 422
4f84f433 423 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
74c383f1
IM
424 if (!debug_locks_off_graph_unlock())
425 return 0;
426
2c522836 427 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
74c383f1
IM
428 dump_stack();
429
fbb9ce95
IM
430 return 0;
431 }
432
433 return 1;
434}
435
436unsigned int nr_hardirq_chains;
437unsigned int nr_softirq_chains;
438unsigned int nr_process_chains;
439unsigned int max_lockdep_depth;
fbb9ce95
IM
440
441#ifdef CONFIG_DEBUG_LOCKDEP
442/*
443 * We cannot printk in early bootup code. Not even early_printk()
444 * might work. So we mark any initialization errors and printk
445 * about it later on, in lockdep_info().
446 */
447static int lockdep_init_error;
81140acc 448static const char *lock_init_error;
c71063c9
JB
449static unsigned long lockdep_init_trace_data[20];
450static struct stack_trace lockdep_init_trace = {
451 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
452 .entries = lockdep_init_trace_data,
453};
fbb9ce95
IM
454
455/*
456 * Various lockdep statistics:
457 */
bd6d29c2 458DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
fbb9ce95
IM
459#endif
460
461/*
462 * Locking printouts:
463 */
464
fabe9c42 465#define __USAGE(__STATE) \
b4b136f4
PZ
466 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
467 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
468 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
469 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
fabe9c42 470
fbb9ce95
IM
471static const char *usage_str[] =
472{
fabe9c42
PZ
473#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
474#include "lockdep_states.h"
475#undef LOCKDEP_STATE
476 [LOCK_USED] = "INITIAL USE",
fbb9ce95
IM
477};
478
479const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
480{
ffb45122 481 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
fbb9ce95
IM
482}
483
3ff176ca 484static inline unsigned long lock_flag(enum lock_usage_bit bit)
fbb9ce95 485{
3ff176ca
PZ
486 return 1UL << bit;
487}
fbb9ce95 488
3ff176ca
PZ
489static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
490{
491 char c = '.';
492
493 if (class->usage_mask & lock_flag(bit + 2))
494 c = '+';
495 if (class->usage_mask & lock_flag(bit)) {
496 c = '-';
497 if (class->usage_mask & lock_flag(bit + 2))
498 c = '?';
fbb9ce95
IM
499 }
500
3ff176ca
PZ
501 return c;
502}
cf40bd16 503
f510b233 504void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
3ff176ca 505{
f510b233 506 int i = 0;
cf40bd16 507
f510b233
PZ
508#define LOCKDEP_STATE(__STATE) \
509 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
510 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
511#include "lockdep_states.h"
512#undef LOCKDEP_STATE
513
514 usage[i] = '\0';
fbb9ce95
IM
515}
516
e5e78d08 517static void __print_lock_name(struct lock_class *class)
3003eba3
SR
518{
519 char str[KSYM_NAME_LEN];
520 const char *name;
521
fbb9ce95
IM
522 name = class->name;
523 if (!name) {
524 name = __get_key_name(class->key, str);
e5e78d08 525 printk("%s", name);
fbb9ce95 526 } else {
e5e78d08 527 printk("%s", name);
fbb9ce95
IM
528 if (class->name_version > 1)
529 printk("#%d", class->name_version);
530 if (class->subclass)
531 printk("/%d", class->subclass);
532 }
e5e78d08
SR
533}
534
535static void print_lock_name(struct lock_class *class)
536{
537 char usage[LOCK_USAGE_CHARS];
538
539 get_usage_chars(class, usage);
540
541 printk(" (");
542 __print_lock_name(class);
f510b233 543 printk("){%s}", usage);
fbb9ce95
IM
544}
545
546static void print_lockdep_cache(struct lockdep_map *lock)
547{
548 const char *name;
9281acea 549 char str[KSYM_NAME_LEN];
fbb9ce95
IM
550
551 name = lock->name;
552 if (!name)
553 name = __get_key_name(lock->key->subkeys, str);
554
555 printk("%s", name);
556}
557
558static void print_lock(struct held_lock *hlock)
559{
6fa3eb70
S
560 struct lock_class *lock = hlock_class(hlock);
561 if(lock != NULL){
562 print_lock_name(lock);
563 printk(", at: ");
564 print_ip_sym(hlock->acquire_ip);
565 }
fbb9ce95
IM
566}
567
568static void lockdep_print_held_locks(struct task_struct *curr)
569{
570 int i, depth = curr->lockdep_depth;
571
572 if (!depth) {
ba25f9dc 573 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
fbb9ce95
IM
574 return;
575 }
6fa3eb70
S
576 if (curr->state == TASK_RUNNING)
577 printk("[Caution!] %s/%d is runable state\n", curr->comm, curr->pid);
fbb9ce95 578 printk("%d lock%s held by %s/%d:\n",
ba25f9dc 579 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
fbb9ce95
IM
580
581 for (i = 0; i < depth; i++) {
582 printk(" #%d: ", i);
583 print_lock(curr->held_locks + i);
584 }
585}
fbb9ce95 586
fbdc4b9a 587static void print_kernel_ident(void)
8e18257d 588{
fbdc4b9a 589 printk("%s %.*s %s\n", init_utsname()->release,
8e18257d 590 (int)strcspn(init_utsname()->version, " "),
fbdc4b9a
BH
591 init_utsname()->version,
592 print_tainted());
8e18257d
PZ
593}
594
595static int very_verbose(struct lock_class *class)
596{
597#if VERY_VERBOSE
598 return class_filter(class);
599#endif
600 return 0;
601}
602
fbb9ce95 603/*
8e18257d 604 * Is this the address of a static object:
fbb9ce95 605 */
8e18257d 606static int static_obj(void *obj)
fbb9ce95 607{
8e18257d
PZ
608 unsigned long start = (unsigned long) &_stext,
609 end = (unsigned long) &_end,
610 addr = (unsigned long) obj;
8e18257d 611
fbb9ce95 612 /*
8e18257d 613 * static variable?
fbb9ce95 614 */
8e18257d
PZ
615 if ((addr >= start) && (addr < end))
616 return 1;
fbb9ce95 617
2a9ad18d
MF
618 if (arch_is_kernel_data(addr))
619 return 1;
620
fbb9ce95 621 /*
10fad5e4 622 * in-kernel percpu var?
fbb9ce95 623 */
10fad5e4
TH
624 if (is_kernel_percpu_address(addr))
625 return 1;
fbb9ce95 626
8e18257d 627 /*
10fad5e4 628 * module static or percpu var?
8e18257d 629 */
10fad5e4 630 return is_module_address(addr) || is_module_percpu_address(addr);
99de055a
DJ
631}
632
fbb9ce95 633/*
8e18257d
PZ
634 * To make lock name printouts unique, we calculate a unique
635 * class->name_version generation counter:
fbb9ce95 636 */
8e18257d 637static int count_matching_names(struct lock_class *new_class)
fbb9ce95 638{
8e18257d
PZ
639 struct lock_class *class;
640 int count = 0;
fbb9ce95 641
8e18257d 642 if (!new_class->name)
fbb9ce95
IM
643 return 0;
644
8e18257d
PZ
645 list_for_each_entry(class, &all_lock_classes, lock_entry) {
646 if (new_class->key - new_class->subclass == class->key)
647 return class->name_version;
648 if (class->name && !strcmp(class->name, new_class->name))
649 count = max(count, class->name_version);
650 }
fbb9ce95 651
8e18257d 652 return count + 1;
fbb9ce95
IM
653}
654
8e18257d
PZ
655/*
656 * Register a lock's class in the hash-table, if the class is not present
657 * yet. Otherwise we look it up. We cache the result in the lock object
658 * itself, so actual lookup of the hash should be once per lock object.
659 */
660static inline struct lock_class *
661look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
fbb9ce95 662{
8e18257d
PZ
663 struct lockdep_subclass_key *key;
664 struct list_head *hash_head;
665 struct lock_class *class;
fbb9ce95 666
8e18257d
PZ
667#ifdef CONFIG_DEBUG_LOCKDEP
668 /*
669 * If the architecture calls into lockdep before initializing
670 * the hashes then we'll warn about it later. (we cannot printk
671 * right now)
672 */
673 if (unlikely(!lockdep_initialized)) {
674 lockdep_init();
675 lockdep_init_error = 1;
81140acc 676 lock_init_error = lock->name;
c71063c9 677 save_stack_trace(&lockdep_init_trace);
8e18257d
PZ
678 }
679#endif
fbb9ce95 680
4ba053c0
HM
681 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
682 debug_locks_off();
683 printk(KERN_ERR
684 "BUG: looking up invalid subclass: %u\n", subclass);
685 printk(KERN_ERR
686 "turning off the locking correctness validator.\n");
687 dump_stack();
688 return NULL;
689 }
690
8e18257d
PZ
691 /*
692 * Static locks do not have their class-keys yet - for them the key
693 * is the lock object itself:
694 */
695 if (unlikely(!lock->key))
696 lock->key = (void *)lock;
fbb9ce95 697
8e18257d
PZ
698 /*
699 * NOTE: the class-key must be unique. For dynamic locks, a static
700 * lock_class_key variable is passed in through the mutex_init()
701 * (or spin_lock_init()) call - which acts as the key. For static
702 * locks we use the lock object itself as the key.
703 */
4b32d0a4
PZ
704 BUILD_BUG_ON(sizeof(struct lock_class_key) >
705 sizeof(struct lockdep_map));
fbb9ce95 706
8e18257d 707 key = lock->key->subkeys + subclass;
ca268c69 708
8e18257d 709 hash_head = classhashentry(key);
74c383f1 710
8e18257d
PZ
711 /*
712 * We can walk the hash lockfree, because the hash only
713 * grows, and we are careful when adding entries to the end:
714 */
4b32d0a4
PZ
715 list_for_each_entry(class, hash_head, hash_entry) {
716 if (class->key == key) {
0119fee4
PZ
717 /*
718 * Huh! same key, different name? Did someone trample
719 * on some memory? We're most confused.
720 */
4b32d0a4 721 WARN_ON_ONCE(class->name != lock->name);
8e18257d 722 return class;
4b32d0a4
PZ
723 }
724 }
fbb9ce95 725
8e18257d 726 return NULL;
fbb9ce95
IM
727}
728
729/*
8e18257d
PZ
730 * Register a lock's class in the hash-table, if the class is not present
731 * yet. Otherwise we look it up. We cache the result in the lock object
732 * itself, so actual lookup of the hash should be once per lock object.
fbb9ce95 733 */
8e18257d
PZ
734static inline struct lock_class *
735register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
fbb9ce95 736{
8e18257d
PZ
737 struct lockdep_subclass_key *key;
738 struct list_head *hash_head;
739 struct lock_class *class;
740 unsigned long flags;
741
742 class = look_up_lock_class(lock, subclass);
743 if (likely(class))
87cdee71 744 goto out_set_class_cache;
8e18257d
PZ
745
746 /*
747 * Debug-check: all keys must be persistent!
748 */
749 if (!static_obj(lock->key)) {
750 debug_locks_off();
751 printk("INFO: trying to register non-static key.\n");
752 printk("the code is fine but needs lockdep annotation.\n");
753 printk("turning off the locking correctness validator.\n");
754 dump_stack();
755
756 return NULL;
757 }
758
759 key = lock->key->subkeys + subclass;
760 hash_head = classhashentry(key);
761
762 raw_local_irq_save(flags);
763 if (!graph_lock()) {
764 raw_local_irq_restore(flags);
765 return NULL;
766 }
767 /*
768 * We have to do the hash-walk again, to avoid races
769 * with another CPU:
770 */
771 list_for_each_entry(class, hash_head, hash_entry)
772 if (class->key == key)
773 goto out_unlock_set;
774 /*
775 * Allocate a new key from the static array, and add it to
776 * the hash:
777 */
778 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
779 if (!debug_locks_off_graph_unlock()) {
780 raw_local_irq_restore(flags);
781 return NULL;
782 }
783 raw_local_irq_restore(flags);
784
2c522836 785 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
eedeeabd 786 dump_stack();
8e18257d
PZ
787 return NULL;
788 }
789 class = lock_classes + nr_lock_classes++;
bd6d29c2 790 debug_atomic_inc(nr_unused_locks);
8e18257d
PZ
791 class->key = key;
792 class->name = lock->name;
793 class->subclass = subclass;
794 INIT_LIST_HEAD(&class->lock_entry);
795 INIT_LIST_HEAD(&class->locks_before);
796 INIT_LIST_HEAD(&class->locks_after);
797 class->name_version = count_matching_names(class);
798 /*
799 * We use RCU's safe list-add method to make
800 * parallel walking of the hash-list safe:
801 */
802 list_add_tail_rcu(&class->hash_entry, hash_head);
1481197b
DF
803 /*
804 * Add it to the global list of classes:
805 */
806 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
8e18257d
PZ
807
808 if (verbose(class)) {
809 graph_unlock();
810 raw_local_irq_restore(flags);
811
812 printk("\nnew class %p: %s", class->key, class->name);
813 if (class->name_version > 1)
814 printk("#%d", class->name_version);
815 printk("\n");
816 dump_stack();
817
818 raw_local_irq_save(flags);
819 if (!graph_lock()) {
820 raw_local_irq_restore(flags);
821 return NULL;
822 }
823 }
824out_unlock_set:
825 graph_unlock();
826 raw_local_irq_restore(flags);
827
87cdee71 828out_set_class_cache:
8e18257d 829 if (!subclass || force)
62016250
HM
830 lock->class_cache[0] = class;
831 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
832 lock->class_cache[subclass] = class;
8e18257d 833
0119fee4
PZ
834 /*
835 * Hash collision, did we smoke some? We found a class with a matching
836 * hash but the subclass -- which is hashed in -- didn't match.
837 */
8e18257d
PZ
838 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
839 return NULL;
840
841 return class;
842}
843
844#ifdef CONFIG_PROVE_LOCKING
845/*
846 * Allocate a lockdep entry. (assumes the graph_lock held, returns
847 * with NULL on failure)
848 */
849static struct lock_list *alloc_list_entry(void)
850{
851 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
852 if (!debug_locks_off_graph_unlock())
853 return NULL;
854
2c522836 855 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
eedeeabd 856 dump_stack();
8e18257d
PZ
857 return NULL;
858 }
859 return list_entries + nr_list_entries++;
860}
861
862/*
863 * Add a new dependency to the head of the list:
864 */
865static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
4726f2a6
YZ
866 struct list_head *head, unsigned long ip,
867 int distance, struct stack_trace *trace)
8e18257d
PZ
868{
869 struct lock_list *entry;
870 /*
871 * Lock not present yet - get a new dependency struct and
872 * add it to the list:
873 */
874 entry = alloc_list_entry();
875 if (!entry)
876 return 0;
877
74870172
ZY
878 entry->class = this;
879 entry->distance = distance;
4726f2a6 880 entry->trace = *trace;
8e18257d
PZ
881 /*
882 * Since we never remove from the dependency list, the list can
883 * be walked lockless by other CPUs, it's only allocation
884 * that must be protected by the spinlock. But this also means
885 * we must make new entries visible only once writes to the
886 * entry become visible - hence the RCU op:
887 */
888 list_add_tail_rcu(&entry->entry, head);
889
890 return 1;
891}
892
98c33edd
PZ
893/*
894 * For good efficiency of modular, we use power of 2
895 */
af012961
PZ
896#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
897#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
898
98c33edd
PZ
899/*
900 * The circular_queue and helpers is used to implement the
af012961
PZ
901 * breadth-first search(BFS)algorithem, by which we can build
902 * the shortest path from the next lock to be acquired to the
903 * previous held lock if there is a circular between them.
98c33edd 904 */
af012961
PZ
905struct circular_queue {
906 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
907 unsigned int front, rear;
908};
909
910static struct circular_queue lock_cq;
af012961 911
12f3dfd0 912unsigned int max_bfs_queue_depth;
af012961 913
e351b660
ML
914static unsigned int lockdep_dependency_gen_id;
915
af012961
PZ
916static inline void __cq_init(struct circular_queue *cq)
917{
918 cq->front = cq->rear = 0;
e351b660 919 lockdep_dependency_gen_id++;
af012961
PZ
920}
921
922static inline int __cq_empty(struct circular_queue *cq)
923{
924 return (cq->front == cq->rear);
925}
926
927static inline int __cq_full(struct circular_queue *cq)
928{
929 return ((cq->rear + 1) & CQ_MASK) == cq->front;
930}
931
932static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
933{
934 if (__cq_full(cq))
935 return -1;
936
937 cq->element[cq->rear] = elem;
938 cq->rear = (cq->rear + 1) & CQ_MASK;
939 return 0;
940}
941
942static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
943{
944 if (__cq_empty(cq))
945 return -1;
946
947 *elem = cq->element[cq->front];
948 cq->front = (cq->front + 1) & CQ_MASK;
949 return 0;
950}
951
952static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
953{
954 return (cq->rear - cq->front) & CQ_MASK;
955}
956
957static inline void mark_lock_accessed(struct lock_list *lock,
958 struct lock_list *parent)
959{
960 unsigned long nr;
98c33edd 961
af012961 962 nr = lock - list_entries;
0119fee4 963 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
af012961 964 lock->parent = parent;
e351b660 965 lock->class->dep_gen_id = lockdep_dependency_gen_id;
af012961
PZ
966}
967
968static inline unsigned long lock_accessed(struct lock_list *lock)
969{
970 unsigned long nr;
98c33edd 971
af012961 972 nr = lock - list_entries;
0119fee4 973 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
e351b660 974 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
af012961
PZ
975}
976
977static inline struct lock_list *get_lock_parent(struct lock_list *child)
978{
979 return child->parent;
980}
981
982static inline int get_lock_depth(struct lock_list *child)
983{
984 int depth = 0;
985 struct lock_list *parent;
986
987 while ((parent = get_lock_parent(child))) {
988 child = parent;
989 depth++;
990 }
991 return depth;
992}
993
9e2d551e 994static int __bfs(struct lock_list *source_entry,
af012961
PZ
995 void *data,
996 int (*match)(struct lock_list *entry, void *data),
997 struct lock_list **target_entry,
998 int forward)
c94aa5ca
ML
999{
1000 struct lock_list *entry;
d588e461 1001 struct list_head *head;
c94aa5ca
ML
1002 struct circular_queue *cq = &lock_cq;
1003 int ret = 1;
1004
9e2d551e 1005 if (match(source_entry, data)) {
c94aa5ca
ML
1006 *target_entry = source_entry;
1007 ret = 0;
1008 goto exit;
1009 }
1010
d588e461
ML
1011 if (forward)
1012 head = &source_entry->class->locks_after;
1013 else
1014 head = &source_entry->class->locks_before;
1015
1016 if (list_empty(head))
1017 goto exit;
1018
1019 __cq_init(cq);
c94aa5ca
ML
1020 __cq_enqueue(cq, (unsigned long)source_entry);
1021
1022 while (!__cq_empty(cq)) {
1023 struct lock_list *lock;
c94aa5ca
ML
1024
1025 __cq_dequeue(cq, (unsigned long *)&lock);
1026
1027 if (!lock->class) {
1028 ret = -2;
1029 goto exit;
1030 }
1031
1032 if (forward)
1033 head = &lock->class->locks_after;
1034 else
1035 head = &lock->class->locks_before;
1036
1037 list_for_each_entry(entry, head, entry) {
1038 if (!lock_accessed(entry)) {
12f3dfd0 1039 unsigned int cq_depth;
c94aa5ca 1040 mark_lock_accessed(entry, lock);
9e2d551e 1041 if (match(entry, data)) {
c94aa5ca
ML
1042 *target_entry = entry;
1043 ret = 0;
1044 goto exit;
1045 }
1046
1047 if (__cq_enqueue(cq, (unsigned long)entry)) {
1048 ret = -1;
1049 goto exit;
1050 }
12f3dfd0
ML
1051 cq_depth = __cq_get_elem_count(cq);
1052 if (max_bfs_queue_depth < cq_depth)
1053 max_bfs_queue_depth = cq_depth;
c94aa5ca
ML
1054 }
1055 }
1056 }
1057exit:
1058 return ret;
1059}
1060
d7aaba14 1061static inline int __bfs_forwards(struct lock_list *src_entry,
9e2d551e
ML
1062 void *data,
1063 int (*match)(struct lock_list *entry, void *data),
1064 struct lock_list **target_entry)
c94aa5ca 1065{
9e2d551e 1066 return __bfs(src_entry, data, match, target_entry, 1);
c94aa5ca
ML
1067
1068}
1069
d7aaba14 1070static inline int __bfs_backwards(struct lock_list *src_entry,
9e2d551e
ML
1071 void *data,
1072 int (*match)(struct lock_list *entry, void *data),
1073 struct lock_list **target_entry)
c94aa5ca 1074{
9e2d551e 1075 return __bfs(src_entry, data, match, target_entry, 0);
c94aa5ca
ML
1076
1077}
1078
8e18257d
PZ
1079/*
1080 * Recursive, forwards-direction lock-dependency checking, used for
1081 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1082 * checking.
8e18257d 1083 */
8e18257d
PZ
1084
1085/*
1086 * Print a dependency chain entry (this is only done when a deadlock
1087 * has been detected):
1088 */
1089static noinline int
24208ca7 1090print_circular_bug_entry(struct lock_list *target, int depth)
8e18257d
PZ
1091{
1092 if (debug_locks_silent)
1093 return 0;
1094 printk("\n-> #%u", depth);
1095 print_lock_name(target->class);
1096 printk(":\n");
1097 print_stack_trace(&target->trace, 6);
1098
1099 return 0;
1100}
1101
f4185812
SR
1102static void
1103print_circular_lock_scenario(struct held_lock *src,
1104 struct held_lock *tgt,
1105 struct lock_list *prt)
1106{
1107 struct lock_class *source = hlock_class(src);
1108 struct lock_class *target = hlock_class(tgt);
1109 struct lock_class *parent = prt->class;
1110
1111 /*
1112 * A direct locking problem where unsafe_class lock is taken
1113 * directly by safe_class lock, then all we need to show
1114 * is the deadlock scenario, as it is obvious that the
1115 * unsafe lock is taken under the safe lock.
1116 *
1117 * But if there is a chain instead, where the safe lock takes
1118 * an intermediate lock (middle_class) where this lock is
1119 * not the same as the safe lock, then the lock chain is
1120 * used to describe the problem. Otherwise we would need
1121 * to show a different CPU case for each link in the chain
1122 * from the safe_class lock to the unsafe_class lock.
1123 */
1124 if (parent != source) {
1125 printk("Chain exists of:\n ");
1126 __print_lock_name(source);
1127 printk(" --> ");
1128 __print_lock_name(parent);
1129 printk(" --> ");
1130 __print_lock_name(target);
1131 printk("\n\n");
1132 }
1133
1134 printk(" Possible unsafe locking scenario:\n\n");
1135 printk(" CPU0 CPU1\n");
1136 printk(" ---- ----\n");
1137 printk(" lock(");
1138 __print_lock_name(target);
1139 printk(");\n");
1140 printk(" lock(");
1141 __print_lock_name(parent);
1142 printk(");\n");
1143 printk(" lock(");
1144 __print_lock_name(target);
1145 printk(");\n");
1146 printk(" lock(");
1147 __print_lock_name(source);
1148 printk(");\n");
1149 printk("\n *** DEADLOCK ***\n\n");
1150}
1151
8e18257d
PZ
1152/*
1153 * When a circular dependency is detected, print the
1154 * header first:
1155 */
1156static noinline int
db0002a3
ML
1157print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1158 struct held_lock *check_src,
1159 struct held_lock *check_tgt)
8e18257d
PZ
1160{
1161 struct task_struct *curr = current;
1162
c94aa5ca 1163 if (debug_locks_silent)
8e18257d 1164 return 0;
6fa3eb70
S
1165 //Add by Mtk
1166 lockdep_aee();
8e18257d 1167
b3fbab05
PM
1168 printk("\n");
1169 printk("======================================================\n");
6fa3eb70 1170 printk("[ ProveLock INFO: possible circular locking dependency detected ]\n");
fbdc4b9a 1171 print_kernel_ident();
b3fbab05 1172 printk("-------------------------------------------------------\n");
8e18257d 1173 printk("%s/%d is trying to acquire lock:\n",
ba25f9dc 1174 curr->comm, task_pid_nr(curr));
db0002a3 1175 print_lock(check_src);
8e18257d 1176 printk("\nbut task is already holding lock:\n");
db0002a3 1177 print_lock(check_tgt);
8e18257d
PZ
1178 printk("\nwhich lock already depends on the new lock.\n\n");
1179 printk("\nthe existing dependency chain (in reverse order) is:\n");
1180
1181 print_circular_bug_entry(entry, depth);
1182
1183 return 0;
1184}
1185
9e2d551e
ML
1186static inline int class_equal(struct lock_list *entry, void *data)
1187{
1188 return entry->class == data;
1189}
1190
db0002a3
ML
1191static noinline int print_circular_bug(struct lock_list *this,
1192 struct lock_list *target,
1193 struct held_lock *check_src,
1194 struct held_lock *check_tgt)
8e18257d
PZ
1195{
1196 struct task_struct *curr = current;
c94aa5ca 1197 struct lock_list *parent;
f4185812 1198 struct lock_list *first_parent;
24208ca7 1199 int depth;
8e18257d 1200
c94aa5ca 1201 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
8e18257d
PZ
1202 return 0;
1203
db0002a3 1204 if (!save_trace(&this->trace))
8e18257d
PZ
1205 return 0;
1206
c94aa5ca
ML
1207 depth = get_lock_depth(target);
1208
db0002a3 1209 print_circular_bug_header(target, depth, check_src, check_tgt);
c94aa5ca
ML
1210
1211 parent = get_lock_parent(target);
f4185812 1212 first_parent = parent;
c94aa5ca
ML
1213
1214 while (parent) {
1215 print_circular_bug_entry(parent, --depth);
1216 parent = get_lock_parent(parent);
1217 }
8e18257d
PZ
1218
1219 printk("\nother info that might help us debug this:\n\n");
f4185812
SR
1220 print_circular_lock_scenario(check_src, check_tgt,
1221 first_parent);
1222
8e18257d
PZ
1223 lockdep_print_held_locks(curr);
1224
1225 printk("\nstack backtrace:\n");
1226 dump_stack();
1227
1228 return 0;
1229}
1230
db0002a3
ML
1231static noinline int print_bfs_bug(int ret)
1232{
1233 if (!debug_locks_off_graph_unlock())
1234 return 0;
1235
0119fee4
PZ
1236 /*
1237 * Breadth-first-search failed, graph got corrupted?
1238 */
db0002a3
ML
1239 WARN(1, "lockdep bfs error:%d\n", ret);
1240
1241 return 0;
1242}
1243
ef681026 1244static int noop_count(struct lock_list *entry, void *data)
419ca3f1 1245{
ef681026
ML
1246 (*(unsigned long *)data)++;
1247 return 0;
1248}
419ca3f1 1249
ef681026
ML
1250unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1251{
1252 unsigned long count = 0;
1253 struct lock_list *uninitialized_var(target_entry);
419ca3f1 1254
ef681026 1255 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
419ca3f1 1256
ef681026 1257 return count;
419ca3f1 1258}
419ca3f1
DM
1259unsigned long lockdep_count_forward_deps(struct lock_class *class)
1260{
1261 unsigned long ret, flags;
ef681026
ML
1262 struct lock_list this;
1263
1264 this.parent = NULL;
1265 this.class = class;
419ca3f1
DM
1266
1267 local_irq_save(flags);
0199c4e6 1268 arch_spin_lock(&lockdep_lock);
ef681026 1269 ret = __lockdep_count_forward_deps(&this);
0199c4e6 1270 arch_spin_unlock(&lockdep_lock);
419ca3f1
DM
1271 local_irq_restore(flags);
1272
1273 return ret;
1274}
1275
ef681026 1276unsigned long __lockdep_count_backward_deps(struct lock_list *this)
419ca3f1 1277{
ef681026
ML
1278 unsigned long count = 0;
1279 struct lock_list *uninitialized_var(target_entry);
419ca3f1 1280
ef681026 1281 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
419ca3f1 1282
ef681026 1283 return count;
419ca3f1
DM
1284}
1285
1286unsigned long lockdep_count_backward_deps(struct lock_class *class)
1287{
1288 unsigned long ret, flags;
ef681026
ML
1289 struct lock_list this;
1290
1291 this.parent = NULL;
1292 this.class = class;
419ca3f1
DM
1293
1294 local_irq_save(flags);
0199c4e6 1295 arch_spin_lock(&lockdep_lock);
ef681026 1296 ret = __lockdep_count_backward_deps(&this);
0199c4e6 1297 arch_spin_unlock(&lockdep_lock);
419ca3f1
DM
1298 local_irq_restore(flags);
1299
1300 return ret;
1301}
1302
8e18257d
PZ
1303/*
1304 * Prove that the dependency graph starting at <entry> can not
1305 * lead to <target>. Print an error and return 0 if it does.
1306 */
1307static noinline int
db0002a3
ML
1308check_noncircular(struct lock_list *root, struct lock_class *target,
1309 struct lock_list **target_entry)
8e18257d 1310{
db0002a3 1311 int result;
8e18257d 1312
bd6d29c2 1313 debug_atomic_inc(nr_cyclic_checks);
419ca3f1 1314
d7aaba14 1315 result = __bfs_forwards(root, target, class_equal, target_entry);
fbb9ce95 1316
db0002a3
ML
1317 return result;
1318}
c94aa5ca 1319
81d68a96 1320#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
fbb9ce95
IM
1321/*
1322 * Forwards and backwards subgraph searching, for the purposes of
1323 * proving that two subgraphs can be connected by a new dependency
1324 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1325 */
fbb9ce95 1326
d7aaba14
ML
1327static inline int usage_match(struct lock_list *entry, void *bit)
1328{
1329 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1330}
1331
1332
1333
fbb9ce95
IM
1334/*
1335 * Find a node in the forwards-direction dependency sub-graph starting
d7aaba14 1336 * at @root->class that matches @bit.
fbb9ce95 1337 *
d7aaba14
ML
1338 * Return 0 if such a node exists in the subgraph, and put that node
1339 * into *@target_entry.
fbb9ce95 1340 *
d7aaba14
ML
1341 * Return 1 otherwise and keep *@target_entry unchanged.
1342 * Return <0 on error.
fbb9ce95 1343 */
d7aaba14
ML
1344static int
1345find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1346 struct lock_list **target_entry)
fbb9ce95 1347{
d7aaba14 1348 int result;
fbb9ce95 1349
bd6d29c2 1350 debug_atomic_inc(nr_find_usage_forwards_checks);
fbb9ce95 1351
d7aaba14
ML
1352 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1353
1354 return result;
fbb9ce95
IM
1355}
1356
1357/*
1358 * Find a node in the backwards-direction dependency sub-graph starting
d7aaba14 1359 * at @root->class that matches @bit.
fbb9ce95 1360 *
d7aaba14
ML
1361 * Return 0 if such a node exists in the subgraph, and put that node
1362 * into *@target_entry.
fbb9ce95 1363 *
d7aaba14
ML
1364 * Return 1 otherwise and keep *@target_entry unchanged.
1365 * Return <0 on error.
fbb9ce95 1366 */
d7aaba14
ML
1367static int
1368find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1369 struct lock_list **target_entry)
fbb9ce95 1370{
d7aaba14 1371 int result;
fbb9ce95 1372
bd6d29c2 1373 debug_atomic_inc(nr_find_usage_backwards_checks);
fbb9ce95 1374
d7aaba14 1375 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
f82b217e 1376
d7aaba14 1377 return result;
fbb9ce95
IM
1378}
1379
af012961
PZ
1380static void print_lock_class_header(struct lock_class *class, int depth)
1381{
1382 int bit;
1383
1384 printk("%*s->", depth, "");
1385 print_lock_name(class);
1386 printk(" ops: %lu", class->ops);
1387 printk(" {\n");
1388
1389 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1390 if (class->usage_mask & (1 << bit)) {
1391 int len = depth;
1392
1393 len += printk("%*s %s", depth, "", usage_str[bit]);
1394 len += printk(" at:\n");
1395 print_stack_trace(class->usage_traces + bit, len);
1396 }
1397 }
1398 printk("%*s }\n", depth, "");
1399
1400 printk("%*s ... key at: ",depth,"");
1401 print_ip_sym((unsigned long)class->key);
1402}
1403
1404/*
1405 * printk the shortest lock dependencies from @start to @end in reverse order:
1406 */
1407static void __used
1408print_shortest_lock_dependencies(struct lock_list *leaf,
1409 struct lock_list *root)
1410{
1411 struct lock_list *entry = leaf;
1412 int depth;
1413
1414 /*compute depth from generated tree by BFS*/
1415 depth = get_lock_depth(leaf);
1416
1417 do {
1418 print_lock_class_header(entry->class, depth);
1419 printk("%*s ... acquired at:\n", depth, "");
1420 print_stack_trace(&entry->trace, 2);
1421 printk("\n");
1422
1423 if (depth == 0 && (entry != root)) {
6be8c393 1424 printk("lockdep:%s bad path found in chain graph\n", __func__);
af012961
PZ
1425 break;
1426 }
1427
1428 entry = get_lock_parent(entry);
1429 depth--;
1430 } while (entry && (depth >= 0));
1431
1432 return;
1433}
d7aaba14 1434
3003eba3
SR
1435static void
1436print_irq_lock_scenario(struct lock_list *safe_entry,
1437 struct lock_list *unsafe_entry,
dad3d743
SR
1438 struct lock_class *prev_class,
1439 struct lock_class *next_class)
3003eba3
SR
1440{
1441 struct lock_class *safe_class = safe_entry->class;
1442 struct lock_class *unsafe_class = unsafe_entry->class;
dad3d743 1443 struct lock_class *middle_class = prev_class;
3003eba3
SR
1444
1445 if (middle_class == safe_class)
dad3d743 1446 middle_class = next_class;
3003eba3
SR
1447
1448 /*
1449 * A direct locking problem where unsafe_class lock is taken
1450 * directly by safe_class lock, then all we need to show
1451 * is the deadlock scenario, as it is obvious that the
1452 * unsafe lock is taken under the safe lock.
1453 *
1454 * But if there is a chain instead, where the safe lock takes
1455 * an intermediate lock (middle_class) where this lock is
1456 * not the same as the safe lock, then the lock chain is
1457 * used to describe the problem. Otherwise we would need
1458 * to show a different CPU case for each link in the chain
1459 * from the safe_class lock to the unsafe_class lock.
1460 */
1461 if (middle_class != unsafe_class) {
1462 printk("Chain exists of:\n ");
1463 __print_lock_name(safe_class);
1464 printk(" --> ");
1465 __print_lock_name(middle_class);
1466 printk(" --> ");
1467 __print_lock_name(unsafe_class);
1468 printk("\n\n");
1469 }
1470
1471 printk(" Possible interrupt unsafe locking scenario:\n\n");
1472 printk(" CPU0 CPU1\n");
1473 printk(" ---- ----\n");
1474 printk(" lock(");
1475 __print_lock_name(unsafe_class);
1476 printk(");\n");
1477 printk(" local_irq_disable();\n");
1478 printk(" lock(");
1479 __print_lock_name(safe_class);
1480 printk(");\n");
1481 printk(" lock(");
1482 __print_lock_name(middle_class);
1483 printk(");\n");
1484 printk(" <Interrupt>\n");
1485 printk(" lock(");
1486 __print_lock_name(safe_class);
1487 printk(");\n");
1488 printk("\n *** DEADLOCK ***\n\n");
1489}
1490
fbb9ce95
IM
1491static int
1492print_bad_irq_dependency(struct task_struct *curr,
24208ca7
ML
1493 struct lock_list *prev_root,
1494 struct lock_list *next_root,
1495 struct lock_list *backwards_entry,
1496 struct lock_list *forwards_entry,
fbb9ce95
IM
1497 struct held_lock *prev,
1498 struct held_lock *next,
1499 enum lock_usage_bit bit1,
1500 enum lock_usage_bit bit2,
1501 const char *irqclass)
1502{
74c383f1 1503 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1504 return 0;
1505
6fa3eb70
S
1506 //Add by Mtk
1507 lockdep_aee();
1508
b3fbab05
PM
1509 printk("\n");
1510 printk("======================================================\n");
6fa3eb70 1511 printk("[ ProveLock INFO: %s-safe -> %s-unsafe lock order detected ]\n",
fbb9ce95 1512 irqclass, irqclass);
fbdc4b9a 1513 print_kernel_ident();
b3fbab05 1514 printk("------------------------------------------------------\n");
fbb9ce95 1515 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
ba25f9dc 1516 curr->comm, task_pid_nr(curr),
fbb9ce95
IM
1517 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1518 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1519 curr->hardirqs_enabled,
1520 curr->softirqs_enabled);
1521 print_lock(next);
1522
1523 printk("\nand this task is already holding:\n");
1524 print_lock(prev);
1525 printk("which would create a new lock dependency:\n");
f82b217e 1526 print_lock_name(hlock_class(prev));
fbb9ce95 1527 printk(" ->");
f82b217e 1528 print_lock_name(hlock_class(next));
fbb9ce95
IM
1529 printk("\n");
1530
1531 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1532 irqclass);
24208ca7 1533 print_lock_name(backwards_entry->class);
fbb9ce95
IM
1534 printk("\n... which became %s-irq-safe at:\n", irqclass);
1535
24208ca7 1536 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
fbb9ce95
IM
1537
1538 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
24208ca7 1539 print_lock_name(forwards_entry->class);
fbb9ce95
IM
1540 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1541 printk("...");
1542
24208ca7 1543 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
fbb9ce95
IM
1544
1545 printk("\nother info that might help us debug this:\n\n");
dad3d743
SR
1546 print_irq_lock_scenario(backwards_entry, forwards_entry,
1547 hlock_class(prev), hlock_class(next));
3003eba3 1548
fbb9ce95
IM
1549 lockdep_print_held_locks(curr);
1550
24208ca7
ML
1551 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1552 printk(" and the holding lock:\n");
1553 if (!save_trace(&prev_root->trace))
1554 return 0;
1555 print_shortest_lock_dependencies(backwards_entry, prev_root);
fbb9ce95 1556
24208ca7
ML
1557 printk("\nthe dependencies between the lock to be acquired");
1558 printk(" and %s-irq-unsafe lock:\n", irqclass);
1559 if (!save_trace(&next_root->trace))
1560 return 0;
1561 print_shortest_lock_dependencies(forwards_entry, next_root);
fbb9ce95
IM
1562
1563 printk("\nstack backtrace:\n");
1564 dump_stack();
1565
1566 return 0;
1567}
1568
1569static int
1570check_usage(struct task_struct *curr, struct held_lock *prev,
1571 struct held_lock *next, enum lock_usage_bit bit_backwards,
1572 enum lock_usage_bit bit_forwards, const char *irqclass)
1573{
1574 int ret;
24208ca7 1575 struct lock_list this, that;
d7aaba14 1576 struct lock_list *uninitialized_var(target_entry);
24208ca7 1577 struct lock_list *uninitialized_var(target_entry1);
d7aaba14
ML
1578
1579 this.parent = NULL;
1580
1581 this.class = hlock_class(prev);
1582 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
af012961
PZ
1583 if (ret < 0)
1584 return print_bfs_bug(ret);
1585 if (ret == 1)
1586 return ret;
d7aaba14 1587
24208ca7
ML
1588 that.parent = NULL;
1589 that.class = hlock_class(next);
1590 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
af012961
PZ
1591 if (ret < 0)
1592 return print_bfs_bug(ret);
1593 if (ret == 1)
1594 return ret;
fbb9ce95 1595
24208ca7
ML
1596 return print_bad_irq_dependency(curr, &this, &that,
1597 target_entry, target_entry1,
1598 prev, next,
fbb9ce95
IM
1599 bit_backwards, bit_forwards, irqclass);
1600}
1601
4f367d8a
PZ
1602static const char *state_names[] = {
1603#define LOCKDEP_STATE(__STATE) \
b4b136f4 1604 __stringify(__STATE),
4f367d8a
PZ
1605#include "lockdep_states.h"
1606#undef LOCKDEP_STATE
1607};
1608
1609static const char *state_rnames[] = {
1610#define LOCKDEP_STATE(__STATE) \
b4b136f4 1611 __stringify(__STATE)"-READ",
4f367d8a
PZ
1612#include "lockdep_states.h"
1613#undef LOCKDEP_STATE
1614};
1615
1616static inline const char *state_name(enum lock_usage_bit bit)
8e18257d 1617{
4f367d8a
PZ
1618 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1619}
8e18257d 1620
4f367d8a
PZ
1621static int exclusive_bit(int new_bit)
1622{
8e18257d 1623 /*
4f367d8a
PZ
1624 * USED_IN
1625 * USED_IN_READ
1626 * ENABLED
1627 * ENABLED_READ
1628 *
1629 * bit 0 - write/read
1630 * bit 1 - used_in/enabled
1631 * bit 2+ state
8e18257d 1632 */
4f367d8a
PZ
1633
1634 int state = new_bit & ~3;
1635 int dir = new_bit & 2;
8e18257d
PZ
1636
1637 /*
4f367d8a 1638 * keep state, bit flip the direction and strip read.
8e18257d 1639 */
4f367d8a
PZ
1640 return state | (dir ^ 2);
1641}
1642
1643static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1644 struct held_lock *next, enum lock_usage_bit bit)
1645{
8e18257d 1646 /*
4f367d8a
PZ
1647 * Prove that the new dependency does not connect a hardirq-safe
1648 * lock with a hardirq-unsafe lock - to achieve this we search
8e18257d
PZ
1649 * the backwards-subgraph starting at <prev>, and the
1650 * forwards-subgraph starting at <next>:
1651 */
4f367d8a
PZ
1652 if (!check_usage(curr, prev, next, bit,
1653 exclusive_bit(bit), state_name(bit)))
8e18257d
PZ
1654 return 0;
1655
4f367d8a
PZ
1656 bit++; /* _READ */
1657
cf40bd16 1658 /*
4f367d8a
PZ
1659 * Prove that the new dependency does not connect a hardirq-safe-read
1660 * lock with a hardirq-unsafe lock - to achieve this we search
cf40bd16
NP
1661 * the backwards-subgraph starting at <prev>, and the
1662 * forwards-subgraph starting at <next>:
1663 */
4f367d8a
PZ
1664 if (!check_usage(curr, prev, next, bit,
1665 exclusive_bit(bit), state_name(bit)))
cf40bd16
NP
1666 return 0;
1667
4f367d8a
PZ
1668 return 1;
1669}
1670
1671static int
1672check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1673 struct held_lock *next)
1674{
1675#define LOCKDEP_STATE(__STATE) \
1676 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
cf40bd16 1677 return 0;
4f367d8a
PZ
1678#include "lockdep_states.h"
1679#undef LOCKDEP_STATE
cf40bd16 1680
8e18257d
PZ
1681 return 1;
1682}
1683
1684static void inc_chains(void)
1685{
1686 if (current->hardirq_context)
1687 nr_hardirq_chains++;
1688 else {
1689 if (current->softirq_context)
1690 nr_softirq_chains++;
1691 else
1692 nr_process_chains++;
1693 }
1694}
1695
1696#else
1697
1698static inline int
1699check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1700 struct held_lock *next)
1701{
1702 return 1;
1703}
1704
1705static inline void inc_chains(void)
1706{
1707 nr_process_chains++;
1708}
1709
fbb9ce95
IM
1710#endif
1711
48702ecf
SR
1712static void
1713print_deadlock_scenario(struct held_lock *nxt,
1714 struct held_lock *prv)
1715{
1716 struct lock_class *next = hlock_class(nxt);
1717 struct lock_class *prev = hlock_class(prv);
1718
1719 printk(" Possible unsafe locking scenario:\n\n");
1720 printk(" CPU0\n");
1721 printk(" ----\n");
1722 printk(" lock(");
1723 __print_lock_name(prev);
1724 printk(");\n");
1725 printk(" lock(");
1726 __print_lock_name(next);
1727 printk(");\n");
1728 printk("\n *** DEADLOCK ***\n\n");
1729 printk(" May be due to missing lock nesting notation\n\n");
1730}
1731
fbb9ce95
IM
1732static int
1733print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1734 struct held_lock *next)
1735{
74c383f1 1736 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
1737 return 0;
1738
6fa3eb70
S
1739 //Add by Mtk
1740 lockdep_aee();
1741
b3fbab05
PM
1742 printk("\n");
1743 printk("=============================================\n");
6fa3eb70 1744 printk("[ ProveLock INFO: possible recursive locking detected ]\n");
fbdc4b9a 1745 print_kernel_ident();
b3fbab05 1746 printk("---------------------------------------------\n");
fbb9ce95 1747 printk("%s/%d is trying to acquire lock:\n",
ba25f9dc 1748 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
1749 print_lock(next);
1750 printk("\nbut task is already holding lock:\n");
1751 print_lock(prev);
1752
1753 printk("\nother info that might help us debug this:\n");
48702ecf 1754 print_deadlock_scenario(next, prev);
fbb9ce95
IM
1755 lockdep_print_held_locks(curr);
1756
1757 printk("\nstack backtrace:\n");
1758 dump_stack();
1759
1760 return 0;
1761}
1762
1763/*
1764 * Check whether we are holding such a class already.
1765 *
1766 * (Note that this has to be done separately, because the graph cannot
1767 * detect such classes of deadlocks.)
1768 *
1769 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1770 */
1771static int
1772check_deadlock(struct task_struct *curr, struct held_lock *next,
1773 struct lockdep_map *next_instance, int read)
1774{
1775 struct held_lock *prev;
7531e2f3 1776 struct held_lock *nest = NULL;
fbb9ce95
IM
1777 int i;
1778
1779 for (i = 0; i < curr->lockdep_depth; i++) {
1780 prev = curr->held_locks + i;
7531e2f3
PZ
1781
1782 if (prev->instance == next->nest_lock)
1783 nest = prev;
1784
f82b217e 1785 if (hlock_class(prev) != hlock_class(next))
fbb9ce95 1786 continue;
7531e2f3 1787
fbb9ce95
IM
1788 /*
1789 * Allow read-after-read recursion of the same
6c9076ec 1790 * lock class (i.e. read_lock(lock)+read_lock(lock)):
fbb9ce95 1791 */
6c9076ec 1792 if ((read == 2) && prev->read)
fbb9ce95 1793 return 2;
7531e2f3
PZ
1794
1795 /*
1796 * We're holding the nest_lock, which serializes this lock's
1797 * nesting behaviour.
1798 */
1799 if (nest)
1800 return 2;
1801
fbb9ce95
IM
1802 return print_deadlock_bug(curr, prev, next);
1803 }
1804 return 1;
1805}
1806
1807/*
1808 * There was a chain-cache miss, and we are about to add a new dependency
1809 * to a previous lock. We recursively validate the following rules:
1810 *
1811 * - would the adding of the <prev> -> <next> dependency create a
1812 * circular dependency in the graph? [== circular deadlock]
1813 *
1814 * - does the new prev->next dependency connect any hardirq-safe lock
1815 * (in the full backwards-subgraph starting at <prev>) with any
1816 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1817 * <next>)? [== illegal lock inversion with hardirq contexts]
1818 *
1819 * - does the new prev->next dependency connect any softirq-safe lock
1820 * (in the full backwards-subgraph starting at <prev>) with any
1821 * softirq-unsafe lock (in the full forwards-subgraph starting at
1822 * <next>)? [== illegal lock inversion with softirq contexts]
1823 *
1824 * any of these scenarios could lead to a deadlock.
1825 *
1826 * Then if all the validations pass, we add the forwards and backwards
1827 * dependency.
1828 */
1829static int
1830check_prev_add(struct task_struct *curr, struct held_lock *prev,
4726f2a6 1831 struct held_lock *next, int distance, int trylock_loop)
fbb9ce95
IM
1832{
1833 struct lock_list *entry;
1834 int ret;
db0002a3
ML
1835 struct lock_list this;
1836 struct lock_list *uninitialized_var(target_entry);
4726f2a6
YZ
1837 /*
1838 * Static variable, serialized by the graph_lock().
1839 *
1840 * We use this static variable to save the stack trace in case
1841 * we call into this function multiple times due to encountering
1842 * trylocks in the held lock stack.
1843 */
1844 static struct stack_trace trace;
fbb9ce95
IM
1845
1846 /*
1847 * Prove that the new <prev> -> <next> dependency would not
1848 * create a circular dependency in the graph. (We do this by
1849 * forward-recursing into the graph starting at <next>, and
1850 * checking whether we can reach <prev>.)
1851 *
1852 * We are using global variables to control the recursion, to
1853 * keep the stackframe size of the recursive functions low:
1854 */
db0002a3
ML
1855 this.class = hlock_class(next);
1856 this.parent = NULL;
1857 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1858 if (unlikely(!ret))
1859 return print_circular_bug(&this, target_entry, next, prev);
1860 else if (unlikely(ret < 0))
1861 return print_bfs_bug(ret);
c94aa5ca 1862
8e18257d 1863 if (!check_prev_add_irq(curr, prev, next))
fbb9ce95
IM
1864 return 0;
1865
fbb9ce95
IM
1866 /*
1867 * For recursive read-locks we do all the dependency checks,
1868 * but we dont store read-triggered dependencies (only
1869 * write-triggered dependencies). This ensures that only the
1870 * write-side dependencies matter, and that if for example a
1871 * write-lock never takes any other locks, then the reads are
1872 * equivalent to a NOP.
1873 */
1874 if (next->read == 2 || prev->read == 2)
1875 return 1;
1876 /*
1877 * Is the <prev> -> <next> dependency already present?
1878 *
1879 * (this may occur even though this is a new chain: consider
1880 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1881 * chains - the second one will be new, but L1 already has
1882 * L2 added to its dependency list, due to the first chain.)
1883 */
f82b217e
DJ
1884 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1885 if (entry->class == hlock_class(next)) {
068135e6
JB
1886 if (distance == 1)
1887 entry->distance = 1;
fbb9ce95 1888 return 2;
068135e6 1889 }
fbb9ce95
IM
1890 }
1891
4726f2a6
YZ
1892 if (!trylock_loop && !save_trace(&trace))
1893 return 0;
1894
fbb9ce95
IM
1895 /*
1896 * Ok, all validations passed, add the new lock
1897 * to the previous lock's dependency list:
1898 */
f82b217e
DJ
1899 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1900 &hlock_class(prev)->locks_after,
4726f2a6 1901 next->acquire_ip, distance, &trace);
068135e6 1902
fbb9ce95
IM
1903 if (!ret)
1904 return 0;
910b1b2e 1905
f82b217e
DJ
1906 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1907 &hlock_class(next)->locks_before,
4726f2a6 1908 next->acquire_ip, distance, &trace);
910b1b2e
JP
1909 if (!ret)
1910 return 0;
fbb9ce95
IM
1911
1912 /*
8e18257d
PZ
1913 * Debugging printouts:
1914 */
f82b217e 1915 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
8e18257d
PZ
1916 graph_unlock();
1917 printk("\n new dependency: ");
f82b217e 1918 print_lock_name(hlock_class(prev));
8e18257d 1919 printk(" => ");
f82b217e 1920 print_lock_name(hlock_class(next));
8e18257d 1921 printk("\n");
fbb9ce95 1922 dump_stack();
8e18257d 1923 return graph_lock();
fbb9ce95 1924 }
8e18257d
PZ
1925 return 1;
1926}
fbb9ce95 1927
8e18257d
PZ
1928/*
1929 * Add the dependency to all directly-previous locks that are 'relevant'.
1930 * The ones that are relevant are (in increasing distance from curr):
1931 * all consecutive trylock entries and the final non-trylock entry - or
1932 * the end of this context's lock-chain - whichever comes first.
1933 */
1934static int
1935check_prevs_add(struct task_struct *curr, struct held_lock *next)
1936{
1937 int depth = curr->lockdep_depth;
4726f2a6 1938 int trylock_loop = 0;
8e18257d 1939 struct held_lock *hlock;
d6d897ce 1940
fbb9ce95 1941 /*
8e18257d
PZ
1942 * Debugging checks.
1943 *
1944 * Depth must not be zero for a non-head lock:
fbb9ce95 1945 */
8e18257d
PZ
1946 if (!depth)
1947 goto out_bug;
fbb9ce95 1948 /*
8e18257d
PZ
1949 * At least two relevant locks must exist for this
1950 * to be a head:
fbb9ce95 1951 */
8e18257d
PZ
1952 if (curr->held_locks[depth].irq_context !=
1953 curr->held_locks[depth-1].irq_context)
1954 goto out_bug;
74c383f1 1955
8e18257d
PZ
1956 for (;;) {
1957 int distance = curr->lockdep_depth - depth + 1;
1958 hlock = curr->held_locks + depth-1;
1959 /*
1960 * Only non-recursive-read entries get new dependencies
1961 * added:
1962 */
1963 if (hlock->read != 2) {
4726f2a6
YZ
1964 if (!check_prev_add(curr, hlock, next,
1965 distance, trylock_loop))
8e18257d
PZ
1966 return 0;
1967 /*
1968 * Stop after the first non-trylock entry,
1969 * as non-trylock entries have added their
1970 * own direct dependencies already, so this
1971 * lock is connected to them indirectly:
1972 */
1973 if (!hlock->trylock)
1974 break;
74c383f1 1975 }
8e18257d
PZ
1976 depth--;
1977 /*
1978 * End of lock-stack?
1979 */
1980 if (!depth)
1981 break;
1982 /*
1983 * Stop the search if we cross into another context:
1984 */
1985 if (curr->held_locks[depth].irq_context !=
1986 curr->held_locks[depth-1].irq_context)
1987 break;
4726f2a6 1988 trylock_loop = 1;
fbb9ce95 1989 }
8e18257d
PZ
1990 return 1;
1991out_bug:
1992 if (!debug_locks_off_graph_unlock())
1993 return 0;
fbb9ce95 1994
0119fee4
PZ
1995 /*
1996 * Clearly we all shouldn't be here, but since we made it we
1997 * can reliable say we messed up our state. See the above two
1998 * gotos for reasons why we could possibly end up here.
1999 */
8e18257d 2000 WARN_ON(1);
fbb9ce95 2001
8e18257d 2002 return 0;
fbb9ce95
IM
2003}
2004
8e18257d 2005unsigned long nr_lock_chains;
443cd507 2006struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
cd1a28e8 2007int nr_chain_hlocks;
443cd507
HY
2008static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2009
2010struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2011{
2012 return lock_classes + chain_hlocks[chain->base + i];
2013}
8e18257d 2014
fbb9ce95
IM
2015/*
2016 * Look up a dependency chain. If the key is not present yet then
9e860d00
JP
2017 * add it and return 1 - in this case the new dependency chain is
2018 * validated. If the key is already hashed, return 0.
2019 * (On return with 1 graph_lock is held.)
fbb9ce95 2020 */
443cd507
HY
2021static inline int lookup_chain_cache(struct task_struct *curr,
2022 struct held_lock *hlock,
2023 u64 chain_key)
fbb9ce95 2024{
f82b217e 2025 struct lock_class *class = hlock_class(hlock);
fbb9ce95
IM
2026 struct list_head *hash_head = chainhashentry(chain_key);
2027 struct lock_chain *chain;
bfaf4af8 2028 struct held_lock *hlock_curr;
e0944ee6 2029 int i, j;
fbb9ce95 2030
0119fee4
PZ
2031 /*
2032 * We might need to take the graph lock, ensure we've got IRQs
2033 * disabled to make this an IRQ-safe lock.. for recursion reasons
2034 * lockdep won't complain about its own locking errors.
2035 */
381a2292
JP
2036 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2037 return 0;
fbb9ce95
IM
2038 /*
2039 * We can walk it lock-free, because entries only get added
2040 * to the hash:
2041 */
2042 list_for_each_entry(chain, hash_head, entry) {
2043 if (chain->chain_key == chain_key) {
2044cache_hit:
bd6d29c2 2045 debug_atomic_inc(chain_lookup_hits);
81fc685a 2046 if (very_verbose(class))
755cd900
AM
2047 printk("\nhash chain already cached, key: "
2048 "%016Lx tail class: [%p] %s\n",
2049 (unsigned long long)chain_key,
2050 class->key, class->name);
fbb9ce95
IM
2051 return 0;
2052 }
2053 }
81fc685a 2054 if (very_verbose(class))
755cd900
AM
2055 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2056 (unsigned long long)chain_key, class->key, class->name);
fbb9ce95
IM
2057 /*
2058 * Allocate a new chain entry from the static array, and add
2059 * it to the hash:
2060 */
74c383f1
IM
2061 if (!graph_lock())
2062 return 0;
fbb9ce95
IM
2063 /*
2064 * We have to walk the chain again locked - to avoid duplicates:
2065 */
2066 list_for_each_entry(chain, hash_head, entry) {
2067 if (chain->chain_key == chain_key) {
74c383f1 2068 graph_unlock();
fbb9ce95
IM
2069 goto cache_hit;
2070 }
2071 }
2072 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
74c383f1
IM
2073 if (!debug_locks_off_graph_unlock())
2074 return 0;
2075
2c522836 2076 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
eedeeabd 2077 dump_stack();
fbb9ce95
IM
2078 return 0;
2079 }
2080 chain = lock_chains + nr_lock_chains++;
2081 chain->chain_key = chain_key;
443cd507
HY
2082 chain->irq_context = hlock->irq_context;
2083 /* Find the first held_lock of current chain */
443cd507
HY
2084 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2085 hlock_curr = curr->held_locks + i;
bfaf4af8 2086 if (hlock_curr->irq_context != hlock->irq_context)
443cd507 2087 break;
443cd507
HY
2088 }
2089 i++;
2090 chain->depth = curr->lockdep_depth + 1 - i;
e0944ee6
SR
2091 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2092 chain->base = nr_chain_hlocks;
2093 nr_chain_hlocks += chain->depth;
443cd507 2094 for (j = 0; j < chain->depth - 1; j++, i++) {
f82b217e 2095 int lock_id = curr->held_locks[i].class_idx - 1;
443cd507
HY
2096 chain_hlocks[chain->base + j] = lock_id;
2097 }
2098 chain_hlocks[chain->base + j] = class - lock_classes;
2099 }
fbb9ce95 2100 list_add_tail_rcu(&chain->entry, hash_head);
bd6d29c2 2101 debug_atomic_inc(chain_lookup_misses);
8e18257d
PZ
2102 inc_chains();
2103
2104 return 1;
2105}
2106
2107static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
4e6045f1 2108 struct held_lock *hlock, int chain_head, u64 chain_key)
8e18257d
PZ
2109{
2110 /*
2111 * Trylock needs to maintain the stack of held locks, but it
2112 * does not add new dependencies, because trylock can be done
2113 * in any order.
2114 *
2115 * We look up the chain_key and do the O(N^2) check and update of
2116 * the dependencies only if this is a new dependency chain.
2117 * (If lookup_chain_cache() returns with 1 it acquires
2118 * graph_lock for us)
2119 */
2120 if (!hlock->trylock && (hlock->check == 2) &&
443cd507 2121 lookup_chain_cache(curr, hlock, chain_key)) {
8e18257d
PZ
2122 /*
2123 * Check whether last held lock:
2124 *
2125 * - is irq-safe, if this lock is irq-unsafe
2126 * - is softirq-safe, if this lock is hardirq-unsafe
2127 *
2128 * And check whether the new lock's dependency graph
2129 * could lead back to the previous lock.
2130 *
2131 * any of these scenarios could lead to a deadlock. If
2132 * All validations
2133 */
2134 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2135
2136 if (!ret)
2137 return 0;
2138 /*
2139 * Mark recursive read, as we jump over it when
2140 * building dependencies (just like we jump over
2141 * trylock entries):
2142 */
2143 if (ret == 2)
2144 hlock->read = 2;
2145 /*
2146 * Add dependency only if this lock is not the head
2147 * of the chain, and if it's not a secondary read-lock:
2148 */
2149 if (!chain_head && ret != 2)
2150 if (!check_prevs_add(curr, hlock))
2151 return 0;
2152 graph_unlock();
2153 } else
2154 /* after lookup_chain_cache(): */
2155 if (unlikely(!debug_locks))
2156 return 0;
fbb9ce95
IM
2157
2158 return 1;
2159}
8e18257d
PZ
2160#else
2161static inline int validate_chain(struct task_struct *curr,
2162 struct lockdep_map *lock, struct held_lock *hlock,
3aa416b0 2163 int chain_head, u64 chain_key)
8e18257d
PZ
2164{
2165 return 1;
2166}
ca58abcb 2167#endif
fbb9ce95
IM
2168
2169/*
2170 * We are building curr_chain_key incrementally, so double-check
2171 * it from scratch, to make sure that it's done correctly:
2172 */
1d09daa5 2173static void check_chain_key(struct task_struct *curr)
fbb9ce95
IM
2174{
2175#ifdef CONFIG_DEBUG_LOCKDEP
2176 struct held_lock *hlock, *prev_hlock = NULL;
2177 unsigned int i, id;
2178 u64 chain_key = 0;
2179
2180 for (i = 0; i < curr->lockdep_depth; i++) {
2181 hlock = curr->held_locks + i;
2182 if (chain_key != hlock->prev_chain_key) {
2183 debug_locks_off();
0119fee4
PZ
2184 /*
2185 * We got mighty confused, our chain keys don't match
2186 * with what we expect, someone trample on our task state?
2187 */
2df8b1d6 2188 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
fbb9ce95
IM
2189 curr->lockdep_depth, i,
2190 (unsigned long long)chain_key,
2191 (unsigned long long)hlock->prev_chain_key);
fbb9ce95
IM
2192 return;
2193 }
f82b217e 2194 id = hlock->class_idx - 1;
0119fee4
PZ
2195 /*
2196 * Whoops ran out of static storage again?
2197 */
381a2292
JP
2198 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2199 return;
2200
fbb9ce95
IM
2201 if (prev_hlock && (prev_hlock->irq_context !=
2202 hlock->irq_context))
2203 chain_key = 0;
2204 chain_key = iterate_chain_key(chain_key, id);
2205 prev_hlock = hlock;
2206 }
2207 if (chain_key != curr->curr_chain_key) {
2208 debug_locks_off();
0119fee4
PZ
2209 /*
2210 * More smoking hash instead of calculating it, damn see these
2211 * numbers float.. I bet that a pink elephant stepped on my memory.
2212 */
2df8b1d6 2213 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
fbb9ce95
IM
2214 curr->lockdep_depth, i,
2215 (unsigned long long)chain_key,
2216 (unsigned long long)curr->curr_chain_key);
fbb9ce95
IM
2217 }
2218#endif
2219}
2220
282b5c2f
SR
2221static void
2222print_usage_bug_scenario(struct held_lock *lock)
2223{
2224 struct lock_class *class = hlock_class(lock);
2225
2226 printk(" Possible unsafe locking scenario:\n\n");
2227 printk(" CPU0\n");
2228 printk(" ----\n");
2229 printk(" lock(");
2230 __print_lock_name(class);
2231 printk(");\n");
2232 printk(" <Interrupt>\n");
2233 printk(" lock(");
2234 __print_lock_name(class);
2235 printk(");\n");
2236 printk("\n *** DEADLOCK ***\n\n");
2237}
2238
8e18257d
PZ
2239static int
2240print_usage_bug(struct task_struct *curr, struct held_lock *this,
2241 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2242{
2243 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2244 return 0;
2245
6fa3eb70
S
2246 //Add by Mtk
2247 lockdep_aee();
2248
b3fbab05
PM
2249 printk("\n");
2250 printk("=================================\n");
6fa3eb70 2251 printk("[ ProveLock INFO: inconsistent lock state ]\n");
fbdc4b9a 2252 print_kernel_ident();
b3fbab05 2253 printk("---------------------------------\n");
8e18257d
PZ
2254
2255 printk("inconsistent {%s} -> {%s} usage.\n",
2256 usage_str[prev_bit], usage_str[new_bit]);
2257
2258 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
ba25f9dc 2259 curr->comm, task_pid_nr(curr),
8e18257d
PZ
2260 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2261 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2262 trace_hardirqs_enabled(curr),
2263 trace_softirqs_enabled(curr));
2264 print_lock(this);
2265
2266 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
f82b217e 2267 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
8e18257d
PZ
2268
2269 print_irqtrace_events(curr);
2270 printk("\nother info that might help us debug this:\n");
282b5c2f
SR
2271 print_usage_bug_scenario(this);
2272
8e18257d
PZ
2273 lockdep_print_held_locks(curr);
2274
2275 printk("\nstack backtrace:\n");
2276 dump_stack();
2277
2278 return 0;
2279}
2280
2281/*
2282 * Print out an error if an invalid bit is set:
2283 */
2284static inline int
2285valid_state(struct task_struct *curr, struct held_lock *this,
2286 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2287{
f82b217e 2288 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
8e18257d
PZ
2289 return print_usage_bug(curr, this, bad_bit, new_bit);
2290 return 1;
2291}
2292
2293static int mark_lock(struct task_struct *curr, struct held_lock *this,
2294 enum lock_usage_bit new_bit);
2295
81d68a96 2296#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
fbb9ce95
IM
2297
2298/*
2299 * print irq inversion bug:
2300 */
2301static int
24208ca7
ML
2302print_irq_inversion_bug(struct task_struct *curr,
2303 struct lock_list *root, struct lock_list *other,
fbb9ce95
IM
2304 struct held_lock *this, int forwards,
2305 const char *irqclass)
2306{
dad3d743
SR
2307 struct lock_list *entry = other;
2308 struct lock_list *middle = NULL;
2309 int depth;
2310
74c383f1 2311 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
fbb9ce95
IM
2312 return 0;
2313
6fa3eb70
S
2314 //Add by Mtk
2315 lockdep_aee();
2316
b3fbab05
PM
2317 printk("\n");
2318 printk("=========================================================\n");
6fa3eb70 2319 printk("[ ProveLock INFO: possible irq lock inversion dependency detected ]\n");
fbdc4b9a 2320 print_kernel_ident();
b3fbab05 2321 printk("---------------------------------------------------------\n");
fbb9ce95 2322 printk("%s/%d just changed the state of lock:\n",
ba25f9dc 2323 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
2324 print_lock(this);
2325 if (forwards)
26575e28 2326 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
fbb9ce95 2327 else
26575e28 2328 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
24208ca7 2329 print_lock_name(other->class);
fbb9ce95
IM
2330 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2331
2332 printk("\nother info that might help us debug this:\n");
dad3d743
SR
2333
2334 /* Find a middle lock (if one exists) */
2335 depth = get_lock_depth(other);
2336 do {
2337 if (depth == 0 && (entry != root)) {
2338 printk("lockdep:%s bad path found in chain graph\n", __func__);
2339 break;
2340 }
2341 middle = entry;
2342 entry = get_lock_parent(entry);
2343 depth--;
2344 } while (entry && entry != root && (depth >= 0));
2345 if (forwards)
2346 print_irq_lock_scenario(root, other,
2347 middle ? middle->class : root->class, other->class);
2348 else
2349 print_irq_lock_scenario(other, root,
2350 middle ? middle->class : other->class, root->class);
2351
fbb9ce95
IM
2352 lockdep_print_held_locks(curr);
2353
24208ca7
ML
2354 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2355 if (!save_trace(&root->trace))
2356 return 0;
2357 print_shortest_lock_dependencies(other, root);
fbb9ce95
IM
2358
2359 printk("\nstack backtrace:\n");
2360 dump_stack();
2361
2362 return 0;
2363}
2364
2365/*
2366 * Prove that in the forwards-direction subgraph starting at <this>
2367 * there is no lock matching <mask>:
2368 */
2369static int
2370check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2371 enum lock_usage_bit bit, const char *irqclass)
2372{
2373 int ret;
d7aaba14
ML
2374 struct lock_list root;
2375 struct lock_list *uninitialized_var(target_entry);
fbb9ce95 2376
d7aaba14
ML
2377 root.parent = NULL;
2378 root.class = hlock_class(this);
2379 ret = find_usage_forwards(&root, bit, &target_entry);
af012961
PZ
2380 if (ret < 0)
2381 return print_bfs_bug(ret);
2382 if (ret == 1)
2383 return ret;
fbb9ce95 2384
24208ca7 2385 return print_irq_inversion_bug(curr, &root, target_entry,
d7aaba14 2386 this, 1, irqclass);
fbb9ce95
IM
2387}
2388
2389/*
2390 * Prove that in the backwards-direction subgraph starting at <this>
2391 * there is no lock matching <mask>:
2392 */
2393static int
2394check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2395 enum lock_usage_bit bit, const char *irqclass)
2396{
2397 int ret;
d7aaba14
ML
2398 struct lock_list root;
2399 struct lock_list *uninitialized_var(target_entry);
fbb9ce95 2400
d7aaba14
ML
2401 root.parent = NULL;
2402 root.class = hlock_class(this);
2403 ret = find_usage_backwards(&root, bit, &target_entry);
af012961
PZ
2404 if (ret < 0)
2405 return print_bfs_bug(ret);
2406 if (ret == 1)
2407 return ret;
fbb9ce95 2408
24208ca7 2409 return print_irq_inversion_bug(curr, &root, target_entry,
48d50674 2410 this, 0, irqclass);
fbb9ce95
IM
2411}
2412
3117df04 2413void print_irqtrace_events(struct task_struct *curr)
fbb9ce95
IM
2414{
2415 printk("irq event stamp: %u\n", curr->irq_events);
2416 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2417 print_ip_sym(curr->hardirq_enable_ip);
2418 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2419 print_ip_sym(curr->hardirq_disable_ip);
2420 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2421 print_ip_sym(curr->softirq_enable_ip);
2422 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2423 print_ip_sym(curr->softirq_disable_ip);
2424}
2425
cd95302d 2426static int HARDIRQ_verbose(struct lock_class *class)
fbb9ce95 2427{
8e18257d
PZ
2428#if HARDIRQ_VERBOSE
2429 return class_filter(class);
2430#endif
fbb9ce95
IM
2431 return 0;
2432}
2433
cd95302d 2434static int SOFTIRQ_verbose(struct lock_class *class)
fbb9ce95 2435{
8e18257d
PZ
2436#if SOFTIRQ_VERBOSE
2437 return class_filter(class);
2438#endif
2439 return 0;
fbb9ce95
IM
2440}
2441
cd95302d 2442static int RECLAIM_FS_verbose(struct lock_class *class)
cf40bd16
NP
2443{
2444#if RECLAIM_VERBOSE
2445 return class_filter(class);
2446#endif
2447 return 0;
2448}
2449
fbb9ce95
IM
2450#define STRICT_READ_CHECKS 1
2451
cd95302d
PZ
2452static int (*state_verbose_f[])(struct lock_class *class) = {
2453#define LOCKDEP_STATE(__STATE) \
2454 __STATE##_verbose,
2455#include "lockdep_states.h"
2456#undef LOCKDEP_STATE
2457};
2458
2459static inline int state_verbose(enum lock_usage_bit bit,
2460 struct lock_class *class)
2461{
2462 return state_verbose_f[bit >> 2](class);
2463}
2464
42c50d54
PZ
2465typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2466 enum lock_usage_bit bit, const char *name);
2467
6a6904d3 2468static int
1c21f14e
PZ
2469mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2470 enum lock_usage_bit new_bit)
6a6904d3 2471{
f989209e 2472 int excl_bit = exclusive_bit(new_bit);
9d3651a2 2473 int read = new_bit & 1;
42c50d54
PZ
2474 int dir = new_bit & 2;
2475
38aa2714
PZ
2476 /*
2477 * mark USED_IN has to look forwards -- to ensure no dependency
2478 * has ENABLED state, which would allow recursion deadlocks.
2479 *
2480 * mark ENABLED has to look backwards -- to ensure no dependee
2481 * has USED_IN state, which, again, would allow recursion deadlocks.
2482 */
42c50d54
PZ
2483 check_usage_f usage = dir ?
2484 check_usage_backwards : check_usage_forwards;
f989209e 2485
38aa2714
PZ
2486 /*
2487 * Validate that this particular lock does not have conflicting
2488 * usage states.
2489 */
6a6904d3
PZ
2490 if (!valid_state(curr, this, new_bit, excl_bit))
2491 return 0;
42c50d54 2492
38aa2714
PZ
2493 /*
2494 * Validate that the lock dependencies don't have conflicting usage
2495 * states.
2496 */
2497 if ((!read || !dir || STRICT_READ_CHECKS) &&
1c21f14e 2498 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
6a6904d3 2499 return 0;
780e820b 2500
38aa2714
PZ
2501 /*
2502 * Check for read in write conflicts
2503 */
2504 if (!read) {
2505 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2506 return 0;
2507
2508 if (STRICT_READ_CHECKS &&
4f367d8a
PZ
2509 !usage(curr, this, excl_bit + 1,
2510 state_name(new_bit + 1)))
38aa2714
PZ
2511 return 0;
2512 }
780e820b 2513
cd95302d 2514 if (state_verbose(new_bit, hlock_class(this)))
6a6904d3
PZ
2515 return 2;
2516
2517 return 1;
2518}
2519
cf40bd16 2520enum mark_type {
36bfb9bb
PZ
2521#define LOCKDEP_STATE(__STATE) __STATE,
2522#include "lockdep_states.h"
2523#undef LOCKDEP_STATE
cf40bd16
NP
2524};
2525
fbb9ce95
IM
2526/*
2527 * Mark all held locks with a usage bit:
2528 */
1d09daa5 2529static int
cf40bd16 2530mark_held_locks(struct task_struct *curr, enum mark_type mark)
fbb9ce95
IM
2531{
2532 enum lock_usage_bit usage_bit;
2533 struct held_lock *hlock;
2534 int i;
2535
2536 for (i = 0; i < curr->lockdep_depth; i++) {
2537 hlock = curr->held_locks + i;
2538
cf2ad4d1
PZ
2539 usage_bit = 2 + (mark << 2); /* ENABLED */
2540 if (hlock->read)
2541 usage_bit += 1; /* READ */
2542
2543 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
cf40bd16 2544
70a0686a 2545 if (hlock_class(hlock)->key == __lockdep_no_validate__.subkeys)
efbe2eee
PZ
2546 continue;
2547
4ff773bb 2548 if (!mark_lock(curr, hlock, usage_bit))
fbb9ce95
IM
2549 return 0;
2550 }
2551
2552 return 1;
2553}
2554
fbb9ce95
IM
2555/*
2556 * Hardirqs will be enabled:
2557 */
dd4e5d3a 2558static void __trace_hardirqs_on_caller(unsigned long ip)
fbb9ce95
IM
2559{
2560 struct task_struct *curr = current;
fbb9ce95 2561
fbb9ce95
IM
2562 /* we'll do an OFF -> ON transition: */
2563 curr->hardirqs_enabled = 1;
fbb9ce95 2564
fbb9ce95
IM
2565 /*
2566 * We are going to turn hardirqs on, so set the
2567 * usage bit for all held locks:
2568 */
cf40bd16 2569 if (!mark_held_locks(curr, HARDIRQ))
fbb9ce95
IM
2570 return;
2571 /*
2572 * If we have softirqs enabled, then set the usage
2573 * bit for all held locks. (disabled hardirqs prevented
2574 * this bit from being set before)
2575 */
2576 if (curr->softirqs_enabled)
cf40bd16 2577 if (!mark_held_locks(curr, SOFTIRQ))
fbb9ce95
IM
2578 return;
2579
8e18257d
PZ
2580 curr->hardirq_enable_ip = ip;
2581 curr->hardirq_enable_event = ++curr->irq_events;
bd6d29c2 2582 debug_atomic_inc(hardirqs_on_events);
8e18257d 2583}
dd4e5d3a
PZ
2584
2585void trace_hardirqs_on_caller(unsigned long ip)
2586{
2587 time_hardirqs_on(CALLER_ADDR0, ip);
2588
2589 if (unlikely(!debug_locks || current->lockdep_recursion))
2590 return;
2591
7d36b26b
PZ
2592 if (unlikely(current->hardirqs_enabled)) {
2593 /*
2594 * Neither irq nor preemption are disabled here
2595 * so this is racy by nature but losing one hit
2596 * in a stat is not a big deal.
2597 */
2598 __debug_atomic_inc(redundant_hardirqs_on);
2599 return;
2600 }
2601
0119fee4
PZ
2602 /*
2603 * We're enabling irqs and according to our state above irqs weren't
2604 * already enabled, yet we find the hardware thinks they are in fact
2605 * enabled.. someone messed up their IRQ state tracing.
2606 */
dd4e5d3a
PZ
2607 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2608 return;
2609
0119fee4
PZ
2610 /*
2611 * See the fine text that goes along with this variable definition.
2612 */
7d36b26b
PZ
2613 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2614 return;
2615
0119fee4
PZ
2616 /*
2617 * Can't allow enabling interrupts while in an interrupt handler,
2618 * that's general bad form and such. Recursion, limited stack etc..
2619 */
7d36b26b
PZ
2620 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2621 return;
2622
dd4e5d3a
PZ
2623 current->lockdep_recursion = 1;
2624 __trace_hardirqs_on_caller(ip);
2625 current->lockdep_recursion = 0;
2626}
81d68a96 2627EXPORT_SYMBOL(trace_hardirqs_on_caller);
8e18257d 2628
1d09daa5 2629void trace_hardirqs_on(void)
81d68a96
SR
2630{
2631 trace_hardirqs_on_caller(CALLER_ADDR0);
2632}
8e18257d
PZ
2633EXPORT_SYMBOL(trace_hardirqs_on);
2634
2635/*
2636 * Hardirqs were disabled:
2637 */
6afe40b4 2638void trace_hardirqs_off_caller(unsigned long ip)
8e18257d
PZ
2639{
2640 struct task_struct *curr = current;
2641
6afe40b4 2642 time_hardirqs_off(CALLER_ADDR0, ip);
81d68a96 2643
8e18257d
PZ
2644 if (unlikely(!debug_locks || current->lockdep_recursion))
2645 return;
2646
0119fee4
PZ
2647 /*
2648 * So we're supposed to get called after you mask local IRQs, but for
2649 * some reason the hardware doesn't quite think you did a proper job.
2650 */
8e18257d
PZ
2651 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2652 return;
2653
2654 if (curr->hardirqs_enabled) {
2655 /*
2656 * We have done an ON -> OFF transition:
2657 */
2658 curr->hardirqs_enabled = 0;
6afe40b4 2659 curr->hardirq_disable_ip = ip;
8e18257d 2660 curr->hardirq_disable_event = ++curr->irq_events;
bd6d29c2 2661 debug_atomic_inc(hardirqs_off_events);
8e18257d 2662 } else
bd6d29c2 2663 debug_atomic_inc(redundant_hardirqs_off);
8e18257d 2664}
81d68a96 2665EXPORT_SYMBOL(trace_hardirqs_off_caller);
8e18257d 2666
1d09daa5 2667void trace_hardirqs_off(void)
81d68a96
SR
2668{
2669 trace_hardirqs_off_caller(CALLER_ADDR0);
2670}
8e18257d
PZ
2671EXPORT_SYMBOL(trace_hardirqs_off);
2672
2673/*
2674 * Softirqs will be enabled:
2675 */
2676void trace_softirqs_on(unsigned long ip)
2677{
2678 struct task_struct *curr = current;
2679
dd4e5d3a 2680 if (unlikely(!debug_locks || current->lockdep_recursion))
8e18257d
PZ
2681 return;
2682
0119fee4
PZ
2683 /*
2684 * We fancy IRQs being disabled here, see softirq.c, avoids
2685 * funny state and nesting things.
2686 */
8e18257d
PZ
2687 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2688 return;
2689
2690 if (curr->softirqs_enabled) {
bd6d29c2 2691 debug_atomic_inc(redundant_softirqs_on);
8e18257d
PZ
2692 return;
2693 }
2694
dd4e5d3a 2695 current->lockdep_recursion = 1;
8e18257d
PZ
2696 /*
2697 * We'll do an OFF -> ON transition:
2698 */
2699 curr->softirqs_enabled = 1;
2700 curr->softirq_enable_ip = ip;
2701 curr->softirq_enable_event = ++curr->irq_events;
bd6d29c2 2702 debug_atomic_inc(softirqs_on_events);
8e18257d
PZ
2703 /*
2704 * We are going to turn softirqs on, so set the
2705 * usage bit for all held locks, if hardirqs are
2706 * enabled too:
2707 */
2708 if (curr->hardirqs_enabled)
cf40bd16 2709 mark_held_locks(curr, SOFTIRQ);
dd4e5d3a 2710 current->lockdep_recursion = 0;
8e18257d
PZ
2711}
2712
2713/*
2714 * Softirqs were disabled:
2715 */
2716void trace_softirqs_off(unsigned long ip)
2717{
2718 struct task_struct *curr = current;
2719
dd4e5d3a 2720 if (unlikely(!debug_locks || current->lockdep_recursion))
8e18257d
PZ
2721 return;
2722
0119fee4
PZ
2723 /*
2724 * We fancy IRQs being disabled here, see softirq.c
2725 */
8e18257d
PZ
2726 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2727 return;
2728
2729 if (curr->softirqs_enabled) {
2730 /*
2731 * We have done an ON -> OFF transition:
2732 */
2733 curr->softirqs_enabled = 0;
2734 curr->softirq_disable_ip = ip;
2735 curr->softirq_disable_event = ++curr->irq_events;
bd6d29c2 2736 debug_atomic_inc(softirqs_off_events);
0119fee4
PZ
2737 /*
2738 * Whoops, we wanted softirqs off, so why aren't they?
2739 */
8e18257d
PZ
2740 DEBUG_LOCKS_WARN_ON(!softirq_count());
2741 } else
bd6d29c2 2742 debug_atomic_inc(redundant_softirqs_off);
8e18257d
PZ
2743}
2744
2f850181 2745static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
cf40bd16
NP
2746{
2747 struct task_struct *curr = current;
2748
2749 if (unlikely(!debug_locks))
2750 return;
2751
2752 /* no reclaim without waiting on it */
2753 if (!(gfp_mask & __GFP_WAIT))
2754 return;
2755
2756 /* this guy won't enter reclaim */
2757 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2758 return;
2759
2760 /* We're only interested __GFP_FS allocations for now */
2761 if (!(gfp_mask & __GFP_FS))
2762 return;
2763
0119fee4
PZ
2764 /*
2765 * Oi! Can't be having __GFP_FS allocations with IRQs disabled.
2766 */
2f850181 2767 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
cf40bd16
NP
2768 return;
2769
2770 mark_held_locks(curr, RECLAIM_FS);
2771}
2772
2f850181
PZ
2773static void check_flags(unsigned long flags);
2774
2775void lockdep_trace_alloc(gfp_t gfp_mask)
2776{
2777 unsigned long flags;
2778
2779 if (unlikely(current->lockdep_recursion))
2780 return;
2781
2782 raw_local_irq_save(flags);
2783 check_flags(flags);
2784 current->lockdep_recursion = 1;
2785 __lockdep_trace_alloc(gfp_mask, flags);
2786 current->lockdep_recursion = 0;
2787 raw_local_irq_restore(flags);
2788}
2789
8e18257d
PZ
2790static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2791{
2792 /*
2793 * If non-trylock use in a hardirq or softirq context, then
2794 * mark the lock as used in these contexts:
2795 */
2796 if (!hlock->trylock) {
2797 if (hlock->read) {
2798 if (curr->hardirq_context)
2799 if (!mark_lock(curr, hlock,
2800 LOCK_USED_IN_HARDIRQ_READ))
2801 return 0;
2802 if (curr->softirq_context)
2803 if (!mark_lock(curr, hlock,
2804 LOCK_USED_IN_SOFTIRQ_READ))
2805 return 0;
2806 } else {
2807 if (curr->hardirq_context)
2808 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2809 return 0;
2810 if (curr->softirq_context)
2811 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2812 return 0;
2813 }
2814 }
2815 if (!hlock->hardirqs_off) {
2816 if (hlock->read) {
2817 if (!mark_lock(curr, hlock,
4fc95e86 2818 LOCK_ENABLED_HARDIRQ_READ))
8e18257d
PZ
2819 return 0;
2820 if (curr->softirqs_enabled)
2821 if (!mark_lock(curr, hlock,
4fc95e86 2822 LOCK_ENABLED_SOFTIRQ_READ))
8e18257d
PZ
2823 return 0;
2824 } else {
2825 if (!mark_lock(curr, hlock,
4fc95e86 2826 LOCK_ENABLED_HARDIRQ))
8e18257d
PZ
2827 return 0;
2828 if (curr->softirqs_enabled)
2829 if (!mark_lock(curr, hlock,
4fc95e86 2830 LOCK_ENABLED_SOFTIRQ))
8e18257d
PZ
2831 return 0;
2832 }
2833 }
2834
cf40bd16
NP
2835 /*
2836 * We reuse the irq context infrastructure more broadly as a general
2837 * context checking code. This tests GFP_FS recursion (a lock taken
2838 * during reclaim for a GFP_FS allocation is held over a GFP_FS
2839 * allocation).
2840 */
2841 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2842 if (hlock->read) {
2843 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2844 return 0;
2845 } else {
2846 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2847 return 0;
2848 }
2849 }
2850
8e18257d
PZ
2851 return 1;
2852}
2853
2854static int separate_irq_context(struct task_struct *curr,
2855 struct held_lock *hlock)
2856{
2857 unsigned int depth = curr->lockdep_depth;
2858
2859 /*
2860 * Keep track of points where we cross into an interrupt context:
2861 */
2862 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2863 curr->softirq_context;
2864 if (depth) {
2865 struct held_lock *prev_hlock;
2866
2867 prev_hlock = curr->held_locks + depth-1;
2868 /*
2869 * If we cross into another context, reset the
2870 * hash key (this also prevents the checking and the
2871 * adding of the dependency to 'prev'):
2872 */
2873 if (prev_hlock->irq_context != hlock->irq_context)
2874 return 1;
2875 }
2876 return 0;
fbb9ce95
IM
2877}
2878
0119fee4 2879#else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
fbb9ce95 2880
8e18257d
PZ
2881static inline
2882int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2883 enum lock_usage_bit new_bit)
fbb9ce95 2884{
0119fee4 2885 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
8e18257d
PZ
2886 return 1;
2887}
fbb9ce95 2888
8e18257d
PZ
2889static inline int mark_irqflags(struct task_struct *curr,
2890 struct held_lock *hlock)
2891{
2892 return 1;
2893}
fbb9ce95 2894
8e18257d
PZ
2895static inline int separate_irq_context(struct task_struct *curr,
2896 struct held_lock *hlock)
2897{
2898 return 0;
fbb9ce95
IM
2899}
2900
868a23a8
PZ
2901void lockdep_trace_alloc(gfp_t gfp_mask)
2902{
2903}
2904
0119fee4 2905#endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
fbb9ce95
IM
2906
2907/*
8e18257d 2908 * Mark a lock with a usage bit, and validate the state transition:
fbb9ce95 2909 */
1d09daa5 2910static int mark_lock(struct task_struct *curr, struct held_lock *this,
0764d23c 2911 enum lock_usage_bit new_bit)
fbb9ce95 2912{
8e18257d 2913 unsigned int new_mask = 1 << new_bit, ret = 1;
fbb9ce95
IM
2914
2915 /*
8e18257d
PZ
2916 * If already set then do not dirty the cacheline,
2917 * nor do any checks:
fbb9ce95 2918 */
f82b217e 2919 if (likely(hlock_class(this)->usage_mask & new_mask))
8e18257d
PZ
2920 return 1;
2921
2922 if (!graph_lock())
2923 return 0;
fbb9ce95 2924 /*
25985edc 2925 * Make sure we didn't race:
fbb9ce95 2926 */
f82b217e 2927 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
8e18257d
PZ
2928 graph_unlock();
2929 return 1;
2930 }
fbb9ce95 2931
f82b217e 2932 hlock_class(this)->usage_mask |= new_mask;
fbb9ce95 2933
f82b217e 2934 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
8e18257d 2935 return 0;
fbb9ce95 2936
8e18257d 2937 switch (new_bit) {
5346417e
PZ
2938#define LOCKDEP_STATE(__STATE) \
2939 case LOCK_USED_IN_##__STATE: \
2940 case LOCK_USED_IN_##__STATE##_READ: \
2941 case LOCK_ENABLED_##__STATE: \
2942 case LOCK_ENABLED_##__STATE##_READ:
2943#include "lockdep_states.h"
2944#undef LOCKDEP_STATE
8e18257d
PZ
2945 ret = mark_lock_irq(curr, this, new_bit);
2946 if (!ret)
2947 return 0;
2948 break;
2949 case LOCK_USED:
bd6d29c2 2950 debug_atomic_dec(nr_unused_locks);
8e18257d
PZ
2951 break;
2952 default:
2953 if (!debug_locks_off_graph_unlock())
2954 return 0;
2955 WARN_ON(1);
2956 return 0;
2957 }
fbb9ce95 2958
8e18257d
PZ
2959 graph_unlock();
2960
2961 /*
2962 * We must printk outside of the graph_lock:
2963 */
2964 if (ret == 2) {
2965 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2966 print_lock(this);
2967 print_irqtrace_events(curr);
2968 dump_stack();
2969 }
2970
2971 return ret;
2972}
fbb9ce95
IM
2973
2974/*
2975 * Initialize a lock instance's lock-class mapping info:
2976 */
2977void lockdep_init_map(struct lockdep_map *lock, const char *name,
4dfbb9d8 2978 struct lock_class_key *key, int subclass)
fbb9ce95 2979{
d3d03d4f
YZ
2980 int i;
2981
2982 kmemcheck_mark_initialized(lock, sizeof(*lock));
2983
2984 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
2985 lock->class_cache[i] = NULL;
62016250 2986
c8a25005
PZ
2987#ifdef CONFIG_LOCK_STAT
2988 lock->cpu = raw_smp_processor_id();
2989#endif
2990
0119fee4
PZ
2991 /*
2992 * Can't be having no nameless bastards around this place!
2993 */
c8a25005
PZ
2994 if (DEBUG_LOCKS_WARN_ON(!name)) {
2995 lock->name = "NULL";
fbb9ce95 2996 return;
c8a25005
PZ
2997 }
2998
2999 lock->name = name;
fbb9ce95 3000
0119fee4
PZ
3001 /*
3002 * No key, no joy, we need to hash something.
3003 */
fbb9ce95
IM
3004 if (DEBUG_LOCKS_WARN_ON(!key))
3005 return;
fbb9ce95
IM
3006 /*
3007 * Sanity check, the lock-class key must be persistent:
3008 */
3009 if (!static_obj(key)) {
3010 printk("BUG: key %p not in .data!\n", key);
0119fee4
PZ
3011 /*
3012 * What it says above ^^^^^, I suggest you read it.
3013 */
fbb9ce95
IM
3014 DEBUG_LOCKS_WARN_ON(1);
3015 return;
3016 }
fbb9ce95 3017 lock->key = key;
c8a25005
PZ
3018
3019 if (unlikely(!debug_locks))
3020 return;
3021
4dfbb9d8
PZ
3022 if (subclass)
3023 register_lock_class(lock, subclass, 1);
fbb9ce95 3024}
fbb9ce95
IM
3025EXPORT_SYMBOL_GPL(lockdep_init_map);
3026
1704f47b 3027struct lock_class_key __lockdep_no_validate__;
ea6749c7 3028EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
1704f47b 3029
d0945950
ML
3030static int
3031print_lock_nested_lock_not_held(struct task_struct *curr,
3032 struct held_lock *hlock,
3033 unsigned long ip)
3034{
3035 if (!debug_locks_off())
3036 return 0;
3037 if (debug_locks_silent)
3038 return 0;
3039
3040 printk("\n");
3041 printk("==================================\n");
3042 printk("[ BUG: Nested lock was not taken ]\n");
3043 print_kernel_ident();
3044 printk("----------------------------------\n");
3045
3046 printk("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3047 print_lock(hlock);
3048
3049 printk("\nbut this task is not holding:\n");
3050 printk("%s\n", hlock->nest_lock->name);
3051
3052 printk("\nstack backtrace:\n");
3053 dump_stack();
3054
3055 printk("\nother info that might help us debug this:\n");
3056 lockdep_print_held_locks(curr);
3057
3058 printk("\nstack backtrace:\n");
3059 dump_stack();
3060
3061 return 0;
3062}
3063
3064static int __lock_is_held(struct lockdep_map *lock);
3065
fbb9ce95
IM
3066/*
3067 * This gets called for every mutex_lock*()/spin_lock*() operation.
3068 * We maintain the dependency maps and validate the locking attempt:
3069 */
3070static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3071 int trylock, int read, int check, int hardirqs_off,
bb97a91e
PZ
3072 struct lockdep_map *nest_lock, unsigned long ip,
3073 int references)
fbb9ce95
IM
3074{
3075 struct task_struct *curr = current;
d6d897ce 3076 struct lock_class *class = NULL;
fbb9ce95 3077 struct held_lock *hlock;
fbb9ce95
IM
3078 unsigned int depth, id;
3079 int chain_head = 0;
bb97a91e 3080 int class_idx;
fbb9ce95
IM
3081 u64 chain_key;
3082
f20786ff
PZ
3083 if (!prove_locking)
3084 check = 1;
3085
fbb9ce95
IM
3086 if (unlikely(!debug_locks))
3087 return 0;
3088
0119fee4
PZ
3089 /*
3090 * Lockdep should run with IRQs disabled, otherwise we could
3091 * get an interrupt which would want to take locks, which would
3092 * end up in lockdep and have you got a head-ache already?
3093 */
fbb9ce95
IM
3094 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3095 return 0;
3096
1704f47b
PZ
3097 if (lock->key == &__lockdep_no_validate__)
3098 check = 1;
3099
62016250
HM
3100 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3101 class = lock->class_cache[subclass];
d6d897ce 3102 /*
62016250 3103 * Not cached?
d6d897ce 3104 */
fbb9ce95 3105 if (unlikely(!class)) {
4dfbb9d8 3106 class = register_lock_class(lock, subclass, 0);
fbb9ce95
IM
3107 if (!class)
3108 return 0;
3109 }
bd6d29c2 3110 atomic_inc((atomic_t *)&class->ops);
fbb9ce95
IM
3111 if (very_verbose(class)) {
3112 printk("\nacquire class [%p] %s", class->key, class->name);
3113 if (class->name_version > 1)
3114 printk("#%d", class->name_version);
3115 printk("\n");
3116 dump_stack();
3117 }
3118
3119 /*
3120 * Add the lock to the list of currently held locks.
3121 * (we dont increase the depth just yet, up until the
3122 * dependency checks are done)
3123 */
3124 depth = curr->lockdep_depth;
0119fee4
PZ
3125 /*
3126 * Ran out of static storage for our per-task lock stack again have we?
3127 */
fbb9ce95
IM
3128 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3129 return 0;
3130
bb97a91e
PZ
3131 class_idx = class - lock_classes + 1;
3132
3133 if (depth) {
3134 hlock = curr->held_locks + depth - 1;
3135 if (hlock->class_idx == class_idx && nest_lock) {
3136 if (hlock->references)
3137 hlock->references++;
3138 else
3139 hlock->references = 2;
3140
3141 return 1;
3142 }
3143 }
3144
fbb9ce95 3145 hlock = curr->held_locks + depth;
0119fee4
PZ
3146 /*
3147 * Plain impossible, we just registered it and checked it weren't no
3148 * NULL like.. I bet this mushroom I ate was good!
3149 */
f82b217e
DJ
3150 if (DEBUG_LOCKS_WARN_ON(!class))
3151 return 0;
bb97a91e 3152 hlock->class_idx = class_idx;
fbb9ce95
IM
3153 hlock->acquire_ip = ip;
3154 hlock->instance = lock;
7531e2f3 3155 hlock->nest_lock = nest_lock;
fbb9ce95
IM
3156 hlock->trylock = trylock;
3157 hlock->read = read;
3158 hlock->check = check;
6951b12a 3159 hlock->hardirqs_off = !!hardirqs_off;
bb97a91e 3160 hlock->references = references;
f20786ff
PZ
3161#ifdef CONFIG_LOCK_STAT
3162 hlock->waittime_stamp = 0;
3365e779 3163 hlock->holdtime_stamp = lockstat_clock();
f20786ff 3164#endif
fbb9ce95 3165
8e18257d
PZ
3166 if (check == 2 && !mark_irqflags(curr, hlock))
3167 return 0;
3168
fbb9ce95 3169 /* mark it as used: */
4ff773bb 3170 if (!mark_lock(curr, hlock, LOCK_USED))
fbb9ce95 3171 return 0;
8e18257d 3172
fbb9ce95 3173 /*
17aacfb9 3174 * Calculate the chain hash: it's the combined hash of all the
fbb9ce95
IM
3175 * lock keys along the dependency chain. We save the hash value
3176 * at every step so that we can get the current hash easily
3177 * after unlock. The chain hash is then used to cache dependency
3178 * results.
3179 *
3180 * The 'key ID' is what is the most compact key value to drive
3181 * the hash, not class->key.
3182 */
3183 id = class - lock_classes;
0119fee4
PZ
3184 /*
3185 * Whoops, we did it again.. ran straight out of our static allocation.
3186 */
fbb9ce95
IM
3187 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
3188 return 0;
3189
3190 chain_key = curr->curr_chain_key;
3191 if (!depth) {
0119fee4
PZ
3192 /*
3193 * How can we have a chain hash when we ain't got no keys?!
3194 */
fbb9ce95
IM
3195 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3196 return 0;
3197 chain_head = 1;
3198 }
3199
3200 hlock->prev_chain_key = chain_key;
8e18257d
PZ
3201 if (separate_irq_context(curr, hlock)) {
3202 chain_key = 0;
3203 chain_head = 1;
fbb9ce95 3204 }
fbb9ce95 3205 chain_key = iterate_chain_key(chain_key, id);
fbb9ce95 3206
d0945950
ML
3207 if (nest_lock && !__lock_is_held(nest_lock))
3208 return print_lock_nested_lock_not_held(curr, hlock, ip);
3209
3aa416b0 3210 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
8e18257d 3211 return 0;
381a2292 3212
3aa416b0 3213 curr->curr_chain_key = chain_key;
fbb9ce95
IM
3214 curr->lockdep_depth++;
3215 check_chain_key(curr);
60e114d1
JP
3216#ifdef CONFIG_DEBUG_LOCKDEP
3217 if (unlikely(!debug_locks))
3218 return 0;
3219#endif
fbb9ce95
IM
3220 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3221 debug_locks_off();
2c522836
DJ
3222 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3223 printk(KERN_DEBUG "depth: %i max: %lu!\n",
c0540606 3224 curr->lockdep_depth, MAX_LOCK_DEPTH);
c0540606
BG
3225
3226 lockdep_print_held_locks(current);
3227 debug_show_all_locks();
eedeeabd 3228 dump_stack();
c0540606 3229
fbb9ce95
IM
3230 return 0;
3231 }
381a2292 3232
fbb9ce95
IM
3233 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3234 max_lockdep_depth = curr->lockdep_depth;
3235
3236 return 1;
3237}
3238
3239static int
f86f7554 3240print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
fbb9ce95
IM
3241 unsigned long ip)
3242{
3243 if (!debug_locks_off())
3244 return 0;
3245 if (debug_locks_silent)
3246 return 0;
3247
6fa3eb70
S
3248 //Add by Mtk
3249 lockdep_aee();
3250
b3fbab05
PM
3251 printk("\n");
3252 printk("=====================================\n");
6fa3eb70 3253 printk("[ ProveLock BUG: bad unlock balance detected! ]\n");
fbdc4b9a 3254 print_kernel_ident();
b3fbab05 3255 printk("-------------------------------------\n");
fbb9ce95 3256 printk("%s/%d is trying to release lock (",
ba25f9dc 3257 curr->comm, task_pid_nr(curr));
fbb9ce95
IM
3258 print_lockdep_cache(lock);
3259 printk(") at:\n");
3260 print_ip_sym(ip);
3261 printk("but there are no more locks to release!\n");
3262 printk("\nother info that might help us debug this:\n");
3263 lockdep_print_held_locks(curr);
3264
3265 printk("\nstack backtrace:\n");
3266 dump_stack();
3267
3268 return 0;
3269}
3270
3271/*
3272 * Common debugging checks for both nested and non-nested unlock:
3273 */
3274static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
3275 unsigned long ip)
3276{
3277 if (unlikely(!debug_locks))
3278 return 0;
0119fee4
PZ
3279 /*
3280 * Lockdep should run with IRQs disabled, recursion, head-ache, etc..
3281 */
fbb9ce95
IM
3282 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3283 return 0;
3284
3285 if (curr->lockdep_depth <= 0)
f86f7554 3286 return print_unlock_imbalance_bug(curr, lock, ip);
fbb9ce95
IM
3287
3288 return 1;
3289}
3290
bb97a91e
PZ
3291static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3292{
3293 if (hlock->instance == lock)
3294 return 1;
3295
3296 if (hlock->references) {
62016250 3297 struct lock_class *class = lock->class_cache[0];
bb97a91e
PZ
3298
3299 if (!class)
3300 class = look_up_lock_class(lock, 0);
3301
80e0401e
PZ
3302 /*
3303 * If look_up_lock_class() failed to find a class, we're trying
3304 * to test if we hold a lock that has never yet been acquired.
3305 * Clearly if the lock hasn't been acquired _ever_, we're not
3306 * holding it either, so report failure.
3307 */
3308 if (!class)
bb97a91e
PZ
3309 return 0;
3310
0119fee4
PZ
3311 /*
3312 * References, but not a lock we're actually ref-counting?
3313 * State got messed up, follow the sites that change ->references
3314 * and try to make sense of it.
3315 */
bb97a91e
PZ
3316 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3317 return 0;
3318
3319 if (hlock->class_idx == class - lock_classes + 1)
3320 return 1;
3321 }
3322
3323 return 0;
3324}
3325
64aa348e 3326static int
00ef9f73
PZ
3327__lock_set_class(struct lockdep_map *lock, const char *name,
3328 struct lock_class_key *key, unsigned int subclass,
3329 unsigned long ip)
64aa348e
PZ
3330{
3331 struct task_struct *curr = current;
3332 struct held_lock *hlock, *prev_hlock;
3333 struct lock_class *class;
3334 unsigned int depth;
3335 int i;
3336
3337 depth = curr->lockdep_depth;
0119fee4
PZ
3338 /*
3339 * This function is about (re)setting the class of a held lock,
3340 * yet we're not actually holding any locks. Naughty user!
3341 */
64aa348e
PZ
3342 if (DEBUG_LOCKS_WARN_ON(!depth))
3343 return 0;
3344
3345 prev_hlock = NULL;
3346 for (i = depth-1; i >= 0; i--) {
3347 hlock = curr->held_locks + i;
3348 /*
3349 * We must not cross into another context:
3350 */
3351 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3352 break;
bb97a91e 3353 if (match_held_lock(hlock, lock))
64aa348e
PZ
3354 goto found_it;
3355 prev_hlock = hlock;
3356 }
f86f7554 3357 return print_unlock_imbalance_bug(curr, lock, ip);
64aa348e
PZ
3358
3359found_it:
00ef9f73 3360 lockdep_init_map(lock, name, key, 0);
64aa348e 3361 class = register_lock_class(lock, subclass, 0);
f82b217e 3362 hlock->class_idx = class - lock_classes + 1;
64aa348e
PZ
3363
3364 curr->lockdep_depth = i;
3365 curr->curr_chain_key = hlock->prev_chain_key;
3366
3367 for (; i < depth; i++) {
3368 hlock = curr->held_locks + i;
3369 if (!__lock_acquire(hlock->instance,
f82b217e 3370 hlock_class(hlock)->subclass, hlock->trylock,
64aa348e 3371 hlock->read, hlock->check, hlock->hardirqs_off,
bb97a91e
PZ
3372 hlock->nest_lock, hlock->acquire_ip,
3373 hlock->references))
64aa348e
PZ
3374 return 0;
3375 }
3376
0119fee4
PZ
3377 /*
3378 * I took it apart and put it back together again, except now I have
3379 * these 'spare' parts.. where shall I put them.
3380 */
64aa348e
PZ
3381 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3382 return 0;
3383 return 1;
3384}
3385
fbb9ce95
IM
3386/*
3387 * Remove the lock to the list of currently held locks in a
3388 * potentially non-nested (out of order) manner. This is a
3389 * relatively rare operation, as all the unlock APIs default
3390 * to nested mode (which uses lock_release()):
3391 */
3392static int
3393lock_release_non_nested(struct task_struct *curr,
3394 struct lockdep_map *lock, unsigned long ip)
3395{
3396 struct held_lock *hlock, *prev_hlock;
3397 unsigned int depth;
3398 int i;
3399
3400 /*
3401 * Check whether the lock exists in the current stack
3402 * of held locks:
3403 */
3404 depth = curr->lockdep_depth;
0119fee4
PZ
3405 /*
3406 * So we're all set to release this lock.. wait what lock? We don't
3407 * own any locks, you've been drinking again?
3408 */
fbb9ce95
IM
3409 if (DEBUG_LOCKS_WARN_ON(!depth))
3410 return 0;
3411
3412 prev_hlock = NULL;
3413 for (i = depth-1; i >= 0; i--) {
3414 hlock = curr->held_locks + i;
3415 /*
3416 * We must not cross into another context:
3417 */
3418 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3419 break;
bb97a91e 3420 if (match_held_lock(hlock, lock))
fbb9ce95
IM
3421 goto found_it;
3422 prev_hlock = hlock;
3423 }
f86f7554 3424 return print_unlock_imbalance_bug(curr, lock, ip);
fbb9ce95
IM
3425
3426found_it:
bb97a91e
PZ
3427 if (hlock->instance == lock)
3428 lock_release_holdtime(hlock);
3429
3430 if (hlock->references) {
3431 hlock->references--;
3432 if (hlock->references) {
3433 /*
3434 * We had, and after removing one, still have
3435 * references, the current lock stack is still
3436 * valid. We're done!
3437 */
3438 return 1;
3439 }
3440 }
f20786ff 3441
fbb9ce95
IM
3442 /*
3443 * We have the right lock to unlock, 'hlock' points to it.
3444 * Now we remove it from the stack, and add back the other
3445 * entries (if any), recalculating the hash along the way:
3446 */
bb97a91e 3447
fbb9ce95
IM
3448 curr->lockdep_depth = i;
3449 curr->curr_chain_key = hlock->prev_chain_key;
3450
3451 for (i++; i < depth; i++) {
3452 hlock = curr->held_locks + i;
3453 if (!__lock_acquire(hlock->instance,
f82b217e 3454 hlock_class(hlock)->subclass, hlock->trylock,
fbb9ce95 3455 hlock->read, hlock->check, hlock->hardirqs_off,
bb97a91e
PZ
3456 hlock->nest_lock, hlock->acquire_ip,
3457 hlock->references))
fbb9ce95
IM
3458 return 0;
3459 }
3460
0119fee4
PZ
3461 /*
3462 * We had N bottles of beer on the wall, we drank one, but now
3463 * there's not N-1 bottles of beer left on the wall...
3464 */
fbb9ce95
IM
3465 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3466 return 0;
3467 return 1;
3468}
3469
3470/*
3471 * Remove the lock to the list of currently held locks - this gets
3472 * called on mutex_unlock()/spin_unlock*() (or on a failed
3473 * mutex_lock_interruptible()). This is done for unlocks that nest
3474 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3475 */
3476static int lock_release_nested(struct task_struct *curr,
3477 struct lockdep_map *lock, unsigned long ip)
3478{
3479 struct held_lock *hlock;
3480 unsigned int depth;
3481
3482 /*
3483 * Pop off the top of the lock stack:
3484 */
3485 depth = curr->lockdep_depth - 1;
3486 hlock = curr->held_locks + depth;
3487
3488 /*
3489 * Is the unlock non-nested:
3490 */
bb97a91e 3491 if (hlock->instance != lock || hlock->references)
fbb9ce95
IM
3492 return lock_release_non_nested(curr, lock, ip);
3493 curr->lockdep_depth--;
3494
0119fee4
PZ
3495 /*
3496 * No more locks, but somehow we've got hash left over, who left it?
3497 */
fbb9ce95
IM
3498 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3499 return 0;
3500
3501 curr->curr_chain_key = hlock->prev_chain_key;
3502
f20786ff
PZ
3503 lock_release_holdtime(hlock);
3504
fbb9ce95
IM
3505#ifdef CONFIG_DEBUG_LOCKDEP
3506 hlock->prev_chain_key = 0;
f82b217e 3507 hlock->class_idx = 0;
fbb9ce95
IM
3508 hlock->acquire_ip = 0;
3509 hlock->irq_context = 0;
3510#endif
3511 return 1;
3512}
3513
3514/*
3515 * Remove the lock to the list of currently held locks - this gets
3516 * called on mutex_unlock()/spin_unlock*() (or on a failed
3517 * mutex_lock_interruptible()). This is done for unlocks that nest
3518 * perfectly. (i.e. the current top of the lock-stack is unlocked)
3519 */
3520static void
3521__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3522{
3523 struct task_struct *curr = current;
3524
3525 if (!check_unlock(curr, lock, ip))
3526 return;
3527
3528 if (nested) {
3529 if (!lock_release_nested(curr, lock, ip))
3530 return;
3531 } else {
3532 if (!lock_release_non_nested(curr, lock, ip))
3533 return;
3534 }
3535
3536 check_chain_key(curr);
3537}
3538
f607c668
PZ
3539static int __lock_is_held(struct lockdep_map *lock)
3540{
3541 struct task_struct *curr = current;
3542 int i;
3543
3544 for (i = 0; i < curr->lockdep_depth; i++) {
bb97a91e
PZ
3545 struct held_lock *hlock = curr->held_locks + i;
3546
3547 if (match_held_lock(hlock, lock))
f607c668
PZ
3548 return 1;
3549 }
3550
3551 return 0;
3552}
3553
fbb9ce95
IM
3554/*
3555 * Check whether we follow the irq-flags state precisely:
3556 */
1d09daa5 3557static void check_flags(unsigned long flags)
fbb9ce95 3558{
992860e9
IM
3559#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3560 defined(CONFIG_TRACE_IRQFLAGS)
fbb9ce95
IM
3561 if (!debug_locks)
3562 return;
3563
5f9fa8a6
IM
3564 if (irqs_disabled_flags(flags)) {
3565 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3566 printk("possible reason: unannotated irqs-off.\n");
3567 }
3568 } else {
3569 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3570 printk("possible reason: unannotated irqs-on.\n");
3571 }
3572 }
fbb9ce95
IM
3573
3574 /*
3575 * We dont accurately track softirq state in e.g.
3576 * hardirq contexts (such as on 4KSTACKS), so only
3577 * check if not in hardirq contexts:
3578 */
3579 if (!hardirq_count()) {
0119fee4
PZ
3580 if (softirq_count()) {
3581 /* like the above, but with softirqs */
fbb9ce95 3582 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
0119fee4
PZ
3583 } else {
3584 /* lick the above, does it taste good? */
fbb9ce95 3585 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
0119fee4 3586 }
fbb9ce95
IM
3587 }
3588
3589 if (!debug_locks)
3590 print_irqtrace_events(current);
3591#endif
3592}
3593
00ef9f73
PZ
3594void lock_set_class(struct lockdep_map *lock, const char *name,
3595 struct lock_class_key *key, unsigned int subclass,
3596 unsigned long ip)
64aa348e
PZ
3597{
3598 unsigned long flags;
3599
3600 if (unlikely(current->lockdep_recursion))
3601 return;
3602
3603 raw_local_irq_save(flags);
3604 current->lockdep_recursion = 1;
3605 check_flags(flags);
00ef9f73 3606 if (__lock_set_class(lock, name, key, subclass, ip))
64aa348e
PZ
3607 check_chain_key(current);
3608 current->lockdep_recursion = 0;
3609 raw_local_irq_restore(flags);
3610}
00ef9f73 3611EXPORT_SYMBOL_GPL(lock_set_class);
64aa348e 3612
fbb9ce95
IM
3613/*
3614 * We are not always called with irqs disabled - do that here,
3615 * and also avoid lockdep recursion:
3616 */
1d09daa5 3617void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
7531e2f3
PZ
3618 int trylock, int read, int check,
3619 struct lockdep_map *nest_lock, unsigned long ip)
fbb9ce95
IM
3620{
3621 unsigned long flags;
6fa3eb70
S
3622 if (unlikely(!debug_locks))
3623 return;
fbb9ce95
IM
3624 if (unlikely(current->lockdep_recursion))
3625 return;
3626
6fa3eb70
S
3627 if (unlikely(lock->skip==1))
3628 return;
3629
fbb9ce95
IM
3630 raw_local_irq_save(flags);
3631 check_flags(flags);
3632
3633 current->lockdep_recursion = 1;
db2c4c77 3634 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
fbb9ce95 3635 __lock_acquire(lock, subclass, trylock, read, check,
bb97a91e 3636 irqs_disabled_flags(flags), nest_lock, ip, 0);
fbb9ce95
IM
3637 current->lockdep_recursion = 0;
3638 raw_local_irq_restore(flags);
3639}
fbb9ce95
IM
3640EXPORT_SYMBOL_GPL(lock_acquire);
3641
1d09daa5 3642void lock_release(struct lockdep_map *lock, int nested,
0764d23c 3643 unsigned long ip)
fbb9ce95
IM
3644{
3645 unsigned long flags;
6fa3eb70
S
3646 if (unlikely(!debug_locks))
3647 return;
fbb9ce95
IM
3648
3649 if (unlikely(current->lockdep_recursion))
3650 return;
3651
6fa3eb70
S
3652 if (unlikely(lock->skip==1))
3653 return;
3654
fbb9ce95
IM
3655 raw_local_irq_save(flags);
3656 check_flags(flags);
3657 current->lockdep_recursion = 1;
93135439 3658 trace_lock_release(lock, ip);
fbb9ce95
IM
3659 __lock_release(lock, nested, ip);
3660 current->lockdep_recursion = 0;
3661 raw_local_irq_restore(flags);
3662}
fbb9ce95
IM
3663EXPORT_SYMBOL_GPL(lock_release);
3664
f607c668
PZ
3665int lock_is_held(struct lockdep_map *lock)
3666{
3667 unsigned long flags;
3668 int ret = 0;
3669
3670 if (unlikely(current->lockdep_recursion))
f2513cde 3671 return 1; /* avoid false negative lockdep_assert_held() */
f607c668
PZ
3672
3673 raw_local_irq_save(flags);
3674 check_flags(flags);
3675
3676 current->lockdep_recursion = 1;
3677 ret = __lock_is_held(lock);
3678 current->lockdep_recursion = 0;
3679 raw_local_irq_restore(flags);
3680
3681 return ret;
3682}
3683EXPORT_SYMBOL_GPL(lock_is_held);
3684
cf40bd16
NP
3685void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3686{
3687 current->lockdep_reclaim_gfp = gfp_mask;
3688}
3689
3690void lockdep_clear_current_reclaim_state(void)
3691{
3692 current->lockdep_reclaim_gfp = 0;
3693}
3694
f20786ff
PZ
3695#ifdef CONFIG_LOCK_STAT
3696static int
3697print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3698 unsigned long ip)
3699{
3700 if (!debug_locks_off())
3701 return 0;
3702 if (debug_locks_silent)
3703 return 0;
3704
6fa3eb70
S
3705 //Add by Mtk
3706 lockdep_aee();
3707
b3fbab05
PM
3708 printk("\n");
3709 printk("=================================\n");
6fa3eb70 3710 printk("[ ProveLock BUG: bad contention detected! ]\n");
fbdc4b9a 3711 print_kernel_ident();
b3fbab05 3712 printk("---------------------------------\n");
f20786ff 3713 printk("%s/%d is trying to contend lock (",
ba25f9dc 3714 curr->comm, task_pid_nr(curr));
f20786ff
PZ
3715 print_lockdep_cache(lock);
3716 printk(") at:\n");
3717 print_ip_sym(ip);
3718 printk("but there are no locks held!\n");
3719 printk("\nother info that might help us debug this:\n");
3720 lockdep_print_held_locks(curr);
3721
3722 printk("\nstack backtrace:\n");
3723 dump_stack();
3724
3725 return 0;
3726}
3727
3728static void
3729__lock_contended(struct lockdep_map *lock, unsigned long ip)
3730{
3731 struct task_struct *curr = current;
3732 struct held_lock *hlock, *prev_hlock;
3733 struct lock_class_stats *stats;
3734 unsigned int depth;
c7e78cff 3735 int i, contention_point, contending_point;
f20786ff
PZ
3736
3737 depth = curr->lockdep_depth;
0119fee4
PZ
3738 /*
3739 * Whee, we contended on this lock, except it seems we're not
3740 * actually trying to acquire anything much at all..
3741 */
f20786ff
PZ
3742 if (DEBUG_LOCKS_WARN_ON(!depth))
3743 return;
3744
3745 prev_hlock = NULL;
3746 for (i = depth-1; i >= 0; i--) {
3747 hlock = curr->held_locks + i;
3748 /*
3749 * We must not cross into another context:
3750 */
3751 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3752 break;
bb97a91e 3753 if (match_held_lock(hlock, lock))
f20786ff
PZ
3754 goto found_it;
3755 prev_hlock = hlock;
3756 }
3757 print_lock_contention_bug(curr, lock, ip);
3758 return;
3759
3760found_it:
bb97a91e
PZ
3761 if (hlock->instance != lock)
3762 return;
3763
3365e779 3764 hlock->waittime_stamp = lockstat_clock();
f20786ff 3765
c7e78cff
PZ
3766 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3767 contending_point = lock_point(hlock_class(hlock)->contending_point,
3768 lock->ip);
f20786ff 3769
f82b217e 3770 stats = get_lock_stats(hlock_class(hlock));
c7e78cff
PZ
3771 if (contention_point < LOCKSTAT_POINTS)
3772 stats->contention_point[contention_point]++;
3773 if (contending_point < LOCKSTAT_POINTS)
3774 stats->contending_point[contending_point]++;
96645678
PZ
3775 if (lock->cpu != smp_processor_id())
3776 stats->bounces[bounce_contended + !!hlock->read]++;
f20786ff
PZ
3777 put_lock_stats(stats);
3778}
3779
3780static void
c7e78cff 3781__lock_acquired(struct lockdep_map *lock, unsigned long ip)
f20786ff
PZ
3782{
3783 struct task_struct *curr = current;
3784 struct held_lock *hlock, *prev_hlock;
3785 struct lock_class_stats *stats;
3786 unsigned int depth;
3365e779 3787 u64 now, waittime = 0;
96645678 3788 int i, cpu;
f20786ff
PZ
3789
3790 depth = curr->lockdep_depth;
0119fee4
PZ
3791 /*
3792 * Yay, we acquired ownership of this lock we didn't try to
3793 * acquire, how the heck did that happen?
3794 */
f20786ff
PZ
3795 if (DEBUG_LOCKS_WARN_ON(!depth))
3796 return;
3797
3798 prev_hlock = NULL;
3799 for (i = depth-1; i >= 0; i--) {
3800 hlock = curr->held_locks + i;
3801 /*
3802 * We must not cross into another context:
3803 */
3804 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3805 break;
bb97a91e 3806 if (match_held_lock(hlock, lock))
f20786ff
PZ
3807 goto found_it;
3808 prev_hlock = hlock;
3809 }
3810 print_lock_contention_bug(curr, lock, _RET_IP_);
3811 return;
3812
3813found_it:
bb97a91e
PZ
3814 if (hlock->instance != lock)
3815 return;
3816
96645678
PZ
3817 cpu = smp_processor_id();
3818 if (hlock->waittime_stamp) {
3365e779 3819 now = lockstat_clock();
96645678
PZ
3820 waittime = now - hlock->waittime_stamp;
3821 hlock->holdtime_stamp = now;
3822 }
f20786ff 3823
883a2a31 3824 trace_lock_acquired(lock, ip);
2062501a 3825
f82b217e 3826 stats = get_lock_stats(hlock_class(hlock));
96645678
PZ
3827 if (waittime) {
3828 if (hlock->read)
3829 lock_time_inc(&stats->read_waittime, waittime);
3830 else
3831 lock_time_inc(&stats->write_waittime, waittime);
3832 }
3833 if (lock->cpu != cpu)
3834 stats->bounces[bounce_acquired + !!hlock->read]++;
f20786ff 3835 put_lock_stats(stats);
96645678
PZ
3836
3837 lock->cpu = cpu;
c7e78cff 3838 lock->ip = ip;
f20786ff
PZ
3839}
3840
3841void lock_contended(struct lockdep_map *lock, unsigned long ip)
3842{
3843 unsigned long flags;
3844
3845 if (unlikely(!lock_stat))
3846 return;
3847
3848 if (unlikely(current->lockdep_recursion))
3849 return;
3850
3851 raw_local_irq_save(flags);
3852 check_flags(flags);
3853 current->lockdep_recursion = 1;
db2c4c77 3854 trace_lock_contended(lock, ip);
f20786ff
PZ
3855 __lock_contended(lock, ip);
3856 current->lockdep_recursion = 0;
3857 raw_local_irq_restore(flags);
3858}
3859EXPORT_SYMBOL_GPL(lock_contended);
3860
c7e78cff 3861void lock_acquired(struct lockdep_map *lock, unsigned long ip)
f20786ff
PZ
3862{
3863 unsigned long flags;
3864
3865 if (unlikely(!lock_stat))
3866 return;
3867
3868 if (unlikely(current->lockdep_recursion))
3869 return;
3870
3871 raw_local_irq_save(flags);
3872 check_flags(flags);
3873 current->lockdep_recursion = 1;
c7e78cff 3874 __lock_acquired(lock, ip);
f20786ff
PZ
3875 current->lockdep_recursion = 0;
3876 raw_local_irq_restore(flags);
3877}
3878EXPORT_SYMBOL_GPL(lock_acquired);
3879#endif
3880
fbb9ce95
IM
3881/*
3882 * Used by the testsuite, sanitize the validator state
3883 * after a simulated failure:
3884 */
3885
3886void lockdep_reset(void)
3887{
3888 unsigned long flags;
23d95a03 3889 int i;
fbb9ce95
IM
3890
3891 raw_local_irq_save(flags);
3892 current->curr_chain_key = 0;
3893 current->lockdep_depth = 0;
3894 current->lockdep_recursion = 0;
3895 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3896 nr_hardirq_chains = 0;
3897 nr_softirq_chains = 0;
3898 nr_process_chains = 0;
3899 debug_locks = 1;
23d95a03
IM
3900 for (i = 0; i < CHAINHASH_SIZE; i++)
3901 INIT_LIST_HEAD(chainhash_table + i);
fbb9ce95
IM
3902 raw_local_irq_restore(flags);
3903}
3904
3905static void zap_class(struct lock_class *class)
3906{
3907 int i;
3908
3909 /*
3910 * Remove all dependencies this lock is
3911 * involved in:
3912 */
3913 for (i = 0; i < nr_list_entries; i++) {
3914 if (list_entries[i].class == class)
3915 list_del_rcu(&list_entries[i].entry);
3916 }
3917 /*
3918 * Unhash the class and remove it from the all_lock_classes list:
3919 */
3920 list_del_rcu(&class->hash_entry);
3921 list_del_rcu(&class->lock_entry);
3922
8bfe0298 3923 class->key = NULL;
fbb9ce95
IM
3924}
3925
fabe874a 3926static inline int within(const void *addr, void *start, unsigned long size)
fbb9ce95
IM
3927{
3928 return addr >= start && addr < start + size;
3929}
3930
3931void lockdep_free_key_range(void *start, unsigned long size)
3932{
3933 struct lock_class *class, *next;
3934 struct list_head *head;
3935 unsigned long flags;
3936 int i;
5a26db5b 3937 int locked;
fbb9ce95
IM
3938
3939 raw_local_irq_save(flags);
5a26db5b 3940 locked = graph_lock();
fbb9ce95
IM
3941
3942 /*
3943 * Unhash all classes that were created by this module:
3944 */
3945 for (i = 0; i < CLASSHASH_SIZE; i++) {
3946 head = classhash_table + i;
3947 if (list_empty(head))
3948 continue;
fabe874a 3949 list_for_each_entry_safe(class, next, head, hash_entry) {
fbb9ce95
IM
3950 if (within(class->key, start, size))
3951 zap_class(class);
fabe874a
AV
3952 else if (within(class->name, start, size))
3953 zap_class(class);
3954 }
fbb9ce95
IM
3955 }
3956
5a26db5b
NP
3957 if (locked)
3958 graph_unlock();
fbb9ce95
IM
3959 raw_local_irq_restore(flags);
3960}
3961
3962void lockdep_reset_lock(struct lockdep_map *lock)
3963{
d6d897ce 3964 struct lock_class *class, *next;
fbb9ce95
IM
3965 struct list_head *head;
3966 unsigned long flags;
3967 int i, j;
5a26db5b 3968 int locked;
fbb9ce95
IM
3969
3970 raw_local_irq_save(flags);
fbb9ce95
IM
3971
3972 /*
d6d897ce
IM
3973 * Remove all classes this lock might have:
3974 */
3975 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3976 /*
3977 * If the class exists we look it up and zap it:
3978 */
3979 class = look_up_lock_class(lock, j);
3980 if (class)
3981 zap_class(class);
3982 }
3983 /*
3984 * Debug check: in the end all mapped classes should
3985 * be gone.
fbb9ce95 3986 */
5a26db5b 3987 locked = graph_lock();
fbb9ce95
IM
3988 for (i = 0; i < CLASSHASH_SIZE; i++) {
3989 head = classhash_table + i;
3990 if (list_empty(head))
3991 continue;
3992 list_for_each_entry_safe(class, next, head, hash_entry) {
62016250
HM
3993 int match = 0;
3994
3995 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
3996 match |= class == lock->class_cache[j];
3997
3998 if (unlikely(match)) {
0119fee4
PZ
3999 if (debug_locks_off_graph_unlock()) {
4000 /*
4001 * We all just reset everything, how did it match?
4002 */
74c383f1 4003 WARN_ON(1);
0119fee4 4004 }
d6d897ce 4005 goto out_restore;
fbb9ce95
IM
4006 }
4007 }
4008 }
5a26db5b
NP
4009 if (locked)
4010 graph_unlock();
d6d897ce
IM
4011
4012out_restore:
fbb9ce95
IM
4013 raw_local_irq_restore(flags);
4014}
4015
1499993c 4016void lockdep_init(void)
fbb9ce95
IM
4017{
4018 int i;
4019
4020 /*
4021 * Some architectures have their own start_kernel()
4022 * code which calls lockdep_init(), while we also
4023 * call lockdep_init() from the start_kernel() itself,
4024 * and we want to initialize the hashes only once:
4025 */
4026 if (lockdep_initialized)
4027 return;
4028
4029 for (i = 0; i < CLASSHASH_SIZE; i++)
4030 INIT_LIST_HEAD(classhash_table + i);
4031
4032 for (i = 0; i < CHAINHASH_SIZE; i++)
4033 INIT_LIST_HEAD(chainhash_table + i);
4034
4035 lockdep_initialized = 1;
4036}
4037
4038void __init lockdep_info(void)
4039{
4040 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4041
b0788caf 4042 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
fbb9ce95
IM
4043 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4044 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
b0788caf 4045 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
fbb9ce95
IM
4046 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4047 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4048 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4049
4050 printk(" memory used by lock dependency info: %lu kB\n",
4051 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4052 sizeof(struct list_head) * CLASSHASH_SIZE +
4053 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4054 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
90629209 4055 sizeof(struct list_head) * CHAINHASH_SIZE
4dd861d6 4056#ifdef CONFIG_PROVE_LOCKING
e351b660 4057 + sizeof(struct circular_queue)
4dd861d6 4058#endif
90629209 4059 ) / 1024
4dd861d6 4060 );
fbb9ce95
IM
4061
4062 printk(" per task-struct memory footprint: %lu bytes\n",
4063 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
4064
4065#ifdef CONFIG_DEBUG_LOCKDEP
c71063c9 4066 if (lockdep_init_error) {
81140acc
ML
4067 printk("WARNING: lockdep init error! lock-%s was acquired"
4068 "before lockdep_init\n", lock_init_error);
c71063c9
JB
4069 printk("Call stack leading to lockdep invocation was:\n");
4070 print_stack_trace(&lockdep_init_trace, 0);
4071 }
fbb9ce95
IM
4072#endif
4073}
4074
fbb9ce95
IM
4075static void
4076print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
55794a41 4077 const void *mem_to, struct held_lock *hlock)
fbb9ce95
IM
4078{
4079 if (!debug_locks_off())
4080 return;
4081 if (debug_locks_silent)
4082 return;
4083
6fa3eb70
S
4084 //Add by Mtk
4085 lockdep_aee();
4086
b3fbab05
PM
4087 printk("\n");
4088 printk("=========================\n");
6fa3eb70 4089 printk("[ ProveLock BUG: held lock freed! ]\n");
fbdc4b9a 4090 print_kernel_ident();
b3fbab05 4091 printk("-------------------------\n");
fbb9ce95 4092 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
ba25f9dc 4093 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
55794a41 4094 print_lock(hlock);
fbb9ce95
IM
4095 lockdep_print_held_locks(curr);
4096
4097 printk("\nstack backtrace:\n");
4098 dump_stack();
4099}
4100
54561783
ON
4101static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4102 const void* lock_from, unsigned long lock_len)
4103{
4104 return lock_from + lock_len <= mem_from ||
4105 mem_from + mem_len <= lock_from;
4106}
4107
fbb9ce95
IM
4108/*
4109 * Called when kernel memory is freed (or unmapped), or if a lock
4110 * is destroyed or reinitialized - this code checks whether there is
4111 * any held lock in the memory range of <from> to <to>:
4112 */
4113void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4114{
fbb9ce95
IM
4115 struct task_struct *curr = current;
4116 struct held_lock *hlock;
4117 unsigned long flags;
4118 int i;
4119
4120 if (unlikely(!debug_locks))
4121 return;
4122
4123 local_irq_save(flags);
4124 for (i = 0; i < curr->lockdep_depth; i++) {
4125 hlock = curr->held_locks + i;
4126
54561783
ON
4127 if (not_in_range(mem_from, mem_len, hlock->instance,
4128 sizeof(*hlock->instance)))
fbb9ce95
IM
4129 continue;
4130
54561783 4131 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
fbb9ce95
IM
4132 break;
4133 }
4134 local_irq_restore(flags);
4135}
ed07536e 4136EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
fbb9ce95 4137
6fa3eb70 4138static void print_held_locks_bug(void)
fbb9ce95
IM
4139{
4140 if (!debug_locks_off())
4141 return;
4142 if (debug_locks_silent)
4143 return;
6fa3eb70
S
4144 printk("[ ProveLock BUG: %s/%d still has locks held! ]\n",
4145 current->comm, task_pid_nr(current));
4146 return;
4147
4148 //Add by Mtk
4149 lockdep_aee();
fbb9ce95 4150
b3fbab05
PM
4151 printk("\n");
4152 printk("=====================================\n");
6fa3eb70
S
4153 printk("[ ProveLock BUG: %s/%d still has locks held! ]\n",
4154 current->comm, task_pid_nr(current));
fbdc4b9a 4155 print_kernel_ident();
b3fbab05 4156 printk("-------------------------------------\n");
6fa3eb70 4157 lockdep_print_held_locks(current);
fbb9ce95
IM
4158 printk("\nstack backtrace:\n");
4159 dump_stack();
4160}
4161
6fa3eb70 4162void debug_check_no_locks_held(void)
fbb9ce95 4163{
6fa3eb70
S
4164 if (unlikely(current->lockdep_depth > 0))
4165 print_held_locks_bug();
fbb9ce95 4166}
6fa3eb70 4167EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
fbb9ce95
IM
4168
4169void debug_show_all_locks(void)
4170{
4171 struct task_struct *g, *p;
4172 int count = 10;
4173 int unlock = 1;
4174
9c35dd7f
JP
4175 if (unlikely(!debug_locks)) {
4176 printk("INFO: lockdep is turned off.\n");
4177 return;
4178 }
fbb9ce95
IM
4179 printk("\nShowing all locks held in the system:\n");
4180
4181 /*
4182 * Here we try to get the tasklist_lock as hard as possible,
4183 * if not successful after 2 seconds we ignore it (but keep
4184 * trying). This is to enable a debug printout even if a
4185 * tasklist_lock-holding task deadlocks or crashes.
4186 */
4187retry:
4188 if (!read_trylock(&tasklist_lock)) {
4189 if (count == 10)
4190 printk("hm, tasklist_lock locked, retrying... ");
4191 if (count) {
4192 count--;
4193 printk(" #%d", 10-count);
4194 mdelay(200);
4195 goto retry;
4196 }
4197 printk(" ignoring it.\n");
4198 unlock = 0;
46fec7ac 4199 } else {
4200 if (count != 10)
4201 printk(KERN_CONT " locked it.\n");
fbb9ce95 4202 }
fbb9ce95
IM
4203
4204 do_each_thread(g, p) {
85684873
IM
4205 /*
4206 * It's not reliable to print a task's held locks
4207 * if it's not sleeping (or if it's not the current
4208 * task):
4209 */
6fa3eb70
S
4210 if (p->state == TASK_RUNNING && p != current){
4211 printk("[Caution!] %s/%d is running now\n", p->comm, p->pid);
85684873 4212 continue;
6fa3eb70 4213 }
fbb9ce95
IM
4214 if (p->lockdep_depth)
4215 lockdep_print_held_locks(p);
4216 if (!unlock)
4217 if (read_trylock(&tasklist_lock))
4218 unlock = 1;
4219 } while_each_thread(g, p);
4220
4221 printk("\n");
4222 printk("=============================================\n\n");
4223
4224 if (unlock)
4225 read_unlock(&tasklist_lock);
4226}
fbb9ce95
IM
4227EXPORT_SYMBOL_GPL(debug_show_all_locks);
4228
82a1fcb9
IM
4229/*
4230 * Careful: only use this function if you are sure that
4231 * the task cannot run in parallel!
4232 */
f1b499f0 4233void debug_show_held_locks(struct task_struct *task)
fbb9ce95 4234{
9c35dd7f
JP
4235 if (unlikely(!debug_locks)) {
4236 printk("INFO: lockdep is turned off.\n");
4237 return;
4238 }
fbb9ce95
IM
4239 lockdep_print_held_locks(task);
4240}
fbb9ce95 4241EXPORT_SYMBOL_GPL(debug_show_held_locks);
b351d164
PZ
4242
4243void lockdep_sys_exit(void)
4244{
4245 struct task_struct *curr = current;
4246
4247 if (unlikely(curr->lockdep_depth)) {
4248 if (!debug_locks_off())
4249 return;
b3fbab05
PM
4250 printk("\n");
4251 printk("================================================\n");
4252 printk("[ BUG: lock held when returning to user space! ]\n");
fbdc4b9a 4253 print_kernel_ident();
b3fbab05 4254 printk("------------------------------------------------\n");
b351d164
PZ
4255 printk("%s/%d is leaving the kernel with locks still held!\n",
4256 curr->comm, curr->pid);
4257 lockdep_print_held_locks(curr);
4258 }
4259}
0632eb3d 4260
b3fbab05 4261void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
0632eb3d
PM
4262{
4263 struct task_struct *curr = current;
4264
2b3fc35f 4265#ifndef CONFIG_PROVE_RCU_REPEATEDLY
0632eb3d
PM
4266 if (!debug_locks_off())
4267 return;
6fa3eb70 4268
2b3fc35f
LJ
4269#endif /* #ifdef CONFIG_PROVE_RCU_REPEATEDLY */
4270 /* Note: the following can be executed concurrently, so be careful. */
6fa3eb70
S
4271 //Add by Mtk
4272 lockdep_aee();
4273
b3fbab05
PM
4274 printk("\n");
4275 printk("===============================\n");
6fa3eb70 4276 printk("[ ProveLock INFO: suspicious RCU usage. ]\n");
fbdc4b9a 4277 print_kernel_ident();
b3fbab05
PM
4278 printk("-------------------------------\n");
4279 printk("%s:%d %s!\n", file, line, s);
0632eb3d 4280 printk("\nother info that might help us debug this:\n\n");
c5fdcec9
PM
4281 printk("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4282 !rcu_lockdep_current_cpu_online()
4283 ? "RCU used illegally from offline CPU!\n"
4284 : rcu_is_cpu_idle()
4285 ? "RCU used illegally from idle CPU!\n"
4286 : "",
4287 rcu_scheduler_active, debug_locks);
0464e937
FW
4288
4289 /*
4290 * If a CPU is in the RCU-free window in idle (ie: in the section
4291 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4292 * considers that CPU to be in an "extended quiescent state",
4293 * which means that RCU will be completely ignoring that CPU.
4294 * Therefore, rcu_read_lock() and friends have absolutely no
4295 * effect on a CPU running in that state. In other words, even if
4296 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4297 * delete data structures out from under it. RCU really has no
4298 * choice here: we need to keep an RCU-free window in idle where
4299 * the CPU may possibly enter into low power mode. This way we can
4300 * notice an extended quiescent state to other CPUs that started a grace
4301 * period. Otherwise we would delay any grace period as long as we run
4302 * in the idle task.
4303 *
4304 * So complain bitterly if someone does call rcu_read_lock(),
4305 * rcu_read_lock_bh() and so on from extended quiescent states.
4306 */
4307 if (rcu_is_cpu_idle())
4308 printk("RCU used illegally from extended quiescent state!\n");
4309
0632eb3d
PM
4310 lockdep_print_held_locks(curr);
4311 printk("\nstack backtrace:\n");
4312 dump_stack();
4313}
b3fbab05 4314EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);