]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - kernel/locking/lockdep.c
ASoC: pcm512x: add missing MODULE_DESCRIPTION/AUTHOR/LICENSE
[mirror_ubuntu-focal-kernel.git] / kernel / locking / lockdep.c
1 /*
2 * kernel/lockdep.c
3 *
4 * Runtime locking correctness validator
5 *
6 * Started by Ingo Molnar:
7 *
8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
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 *
14 * - lock inversion scenarios
15 * - circular lock dependencies
16 * - hardirq/softirq safe/unsafe locking bugs
17 *
18 * Bugs are reported even if the current locking scenario does not cause
19 * any deadlock at this point.
20 *
21 * I.e. if anytime in the past two locks were taken in a different order,
22 * even if it happened for another task, even if those were different
23 * locks (but of the same class as this lock), this code will detect it.
24 *
25 * Thanks to Arjan van de Ven for coming up with the initial idea of
26 * mapping lock dependencies runtime.
27 */
28 #define DISABLE_BRANCH_PROFILING
29 #include <linux/mutex.h>
30 #include <linux/sched.h>
31 #include <linux/sched/clock.h>
32 #include <linux/sched/task.h>
33 #include <linux/sched/mm.h>
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <linux/spinlock.h>
39 #include <linux/kallsyms.h>
40 #include <linux/interrupt.h>
41 #include <linux/stacktrace.h>
42 #include <linux/debug_locks.h>
43 #include <linux/irqflags.h>
44 #include <linux/utsname.h>
45 #include <linux/hash.h>
46 #include <linux/ftrace.h>
47 #include <linux/stringify.h>
48 #include <linux/bitops.h>
49 #include <linux/gfp.h>
50 #include <linux/random.h>
51 #include <linux/jhash.h>
52
53 #include <asm/sections.h>
54
55 #include "lockdep_internals.h"
56
57 #define CREATE_TRACE_POINTS
58 #include <trace/events/lock.h>
59
60 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
61 #include <linux/slab.h>
62 #endif
63
64 #ifdef CONFIG_PROVE_LOCKING
65 int prove_locking = 1;
66 module_param(prove_locking, int, 0644);
67 #else
68 #define prove_locking 0
69 #endif
70
71 #ifdef CONFIG_LOCK_STAT
72 int lock_stat = 1;
73 module_param(lock_stat, int, 0644);
74 #else
75 #define lock_stat 0
76 #endif
77
78 #ifdef CONFIG_BOOTPARAM_LOCKDEP_CROSSRELEASE_FULLSTACK
79 static int crossrelease_fullstack = 1;
80 #else
81 static int crossrelease_fullstack;
82 #endif
83 static int __init allow_crossrelease_fullstack(char *str)
84 {
85 crossrelease_fullstack = 1;
86 return 0;
87 }
88
89 early_param("crossrelease_fullstack", allow_crossrelease_fullstack);
90
91 /*
92 * lockdep_lock: protects the lockdep graph, the hashes and the
93 * class/list/hash allocators.
94 *
95 * This is one of the rare exceptions where it's justified
96 * to use a raw spinlock - we really dont want the spinlock
97 * code to recurse back into the lockdep code...
98 */
99 static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
100
101 static int graph_lock(void)
102 {
103 arch_spin_lock(&lockdep_lock);
104 /*
105 * Make sure that if another CPU detected a bug while
106 * walking the graph we dont change it (while the other
107 * CPU is busy printing out stuff with the graph lock
108 * dropped already)
109 */
110 if (!debug_locks) {
111 arch_spin_unlock(&lockdep_lock);
112 return 0;
113 }
114 /* prevent any recursions within lockdep from causing deadlocks */
115 current->lockdep_recursion++;
116 return 1;
117 }
118
119 static inline int graph_unlock(void)
120 {
121 if (debug_locks && !arch_spin_is_locked(&lockdep_lock)) {
122 /*
123 * The lockdep graph lock isn't locked while we expect it to
124 * be, we're confused now, bye!
125 */
126 return DEBUG_LOCKS_WARN_ON(1);
127 }
128
129 current->lockdep_recursion--;
130 arch_spin_unlock(&lockdep_lock);
131 return 0;
132 }
133
134 /*
135 * Turn lock debugging off and return with 0 if it was off already,
136 * and also release the graph lock:
137 */
138 static inline int debug_locks_off_graph_unlock(void)
139 {
140 int ret = debug_locks_off();
141
142 arch_spin_unlock(&lockdep_lock);
143
144 return ret;
145 }
146
147 unsigned long nr_list_entries;
148 static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
149
150 /*
151 * All data structures here are protected by the global debug_lock.
152 *
153 * Mutex key structs only get allocated, once during bootup, and never
154 * get freed - this significantly simplifies the debugging code.
155 */
156 unsigned long nr_lock_classes;
157 static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
158
159 static inline struct lock_class *hlock_class(struct held_lock *hlock)
160 {
161 if (!hlock->class_idx) {
162 /*
163 * Someone passed in garbage, we give up.
164 */
165 DEBUG_LOCKS_WARN_ON(1);
166 return NULL;
167 }
168 return lock_classes + hlock->class_idx - 1;
169 }
170
171 #ifdef CONFIG_LOCK_STAT
172 static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
173
174 static inline u64 lockstat_clock(void)
175 {
176 return local_clock();
177 }
178
179 static int lock_point(unsigned long points[], unsigned long ip)
180 {
181 int i;
182
183 for (i = 0; i < LOCKSTAT_POINTS; i++) {
184 if (points[i] == 0) {
185 points[i] = ip;
186 break;
187 }
188 if (points[i] == ip)
189 break;
190 }
191
192 return i;
193 }
194
195 static void lock_time_inc(struct lock_time *lt, u64 time)
196 {
197 if (time > lt->max)
198 lt->max = time;
199
200 if (time < lt->min || !lt->nr)
201 lt->min = time;
202
203 lt->total += time;
204 lt->nr++;
205 }
206
207 static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
208 {
209 if (!src->nr)
210 return;
211
212 if (src->max > dst->max)
213 dst->max = src->max;
214
215 if (src->min < dst->min || !dst->nr)
216 dst->min = src->min;
217
218 dst->total += src->total;
219 dst->nr += src->nr;
220 }
221
222 struct lock_class_stats lock_stats(struct lock_class *class)
223 {
224 struct lock_class_stats stats;
225 int cpu, i;
226
227 memset(&stats, 0, sizeof(struct lock_class_stats));
228 for_each_possible_cpu(cpu) {
229 struct lock_class_stats *pcs =
230 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
231
232 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
233 stats.contention_point[i] += pcs->contention_point[i];
234
235 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
236 stats.contending_point[i] += pcs->contending_point[i];
237
238 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
239 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
240
241 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
242 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
243
244 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
245 stats.bounces[i] += pcs->bounces[i];
246 }
247
248 return stats;
249 }
250
251 void clear_lock_stats(struct lock_class *class)
252 {
253 int cpu;
254
255 for_each_possible_cpu(cpu) {
256 struct lock_class_stats *cpu_stats =
257 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
258
259 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
260 }
261 memset(class->contention_point, 0, sizeof(class->contention_point));
262 memset(class->contending_point, 0, sizeof(class->contending_point));
263 }
264
265 static struct lock_class_stats *get_lock_stats(struct lock_class *class)
266 {
267 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
268 }
269
270 static void put_lock_stats(struct lock_class_stats *stats)
271 {
272 put_cpu_var(cpu_lock_stats);
273 }
274
275 static void lock_release_holdtime(struct held_lock *hlock)
276 {
277 struct lock_class_stats *stats;
278 u64 holdtime;
279
280 if (!lock_stat)
281 return;
282
283 holdtime = lockstat_clock() - hlock->holdtime_stamp;
284
285 stats = get_lock_stats(hlock_class(hlock));
286 if (hlock->read)
287 lock_time_inc(&stats->read_holdtime, holdtime);
288 else
289 lock_time_inc(&stats->write_holdtime, holdtime);
290 put_lock_stats(stats);
291 }
292 #else
293 static inline void lock_release_holdtime(struct held_lock *hlock)
294 {
295 }
296 #endif
297
298 /*
299 * We keep a global list of all lock classes. The list only grows,
300 * never shrinks. The list is only accessed with the lockdep
301 * spinlock lock held.
302 */
303 LIST_HEAD(all_lock_classes);
304
305 /*
306 * The lockdep classes are in a hash-table as well, for fast lookup:
307 */
308 #define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
309 #define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
310 #define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
311 #define classhashentry(key) (classhash_table + __classhashfn((key)))
312
313 static struct hlist_head classhash_table[CLASSHASH_SIZE];
314
315 /*
316 * We put the lock dependency chains into a hash-table as well, to cache
317 * their existence:
318 */
319 #define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
320 #define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
321 #define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
322 #define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
323
324 static struct hlist_head chainhash_table[CHAINHASH_SIZE];
325
326 /*
327 * The hash key of the lock dependency chains is a hash itself too:
328 * it's a hash of all locks taken up to that lock, including that lock.
329 * It's a 64-bit hash, because it's important for the keys to be
330 * unique.
331 */
332 static inline u64 iterate_chain_key(u64 key, u32 idx)
333 {
334 u32 k0 = key, k1 = key >> 32;
335
336 __jhash_mix(idx, k0, k1); /* Macro that modifies arguments! */
337
338 return k0 | (u64)k1 << 32;
339 }
340
341 void lockdep_off(void)
342 {
343 current->lockdep_recursion++;
344 }
345 EXPORT_SYMBOL(lockdep_off);
346
347 void lockdep_on(void)
348 {
349 current->lockdep_recursion--;
350 }
351 EXPORT_SYMBOL(lockdep_on);
352
353 /*
354 * Debugging switches:
355 */
356
357 #define VERBOSE 0
358 #define VERY_VERBOSE 0
359
360 #if VERBOSE
361 # define HARDIRQ_VERBOSE 1
362 # define SOFTIRQ_VERBOSE 1
363 #else
364 # define HARDIRQ_VERBOSE 0
365 # define SOFTIRQ_VERBOSE 0
366 #endif
367
368 #if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE
369 /*
370 * Quick filtering for interesting events:
371 */
372 static int class_filter(struct lock_class *class)
373 {
374 #if 0
375 /* Example */
376 if (class->name_version == 1 &&
377 !strcmp(class->name, "lockname"))
378 return 1;
379 if (class->name_version == 1 &&
380 !strcmp(class->name, "&struct->lockfield"))
381 return 1;
382 #endif
383 /* Filter everything else. 1 would be to allow everything else */
384 return 0;
385 }
386 #endif
387
388 static int verbose(struct lock_class *class)
389 {
390 #if VERBOSE
391 return class_filter(class);
392 #endif
393 return 0;
394 }
395
396 /*
397 * Stack-trace: tightly packed array of stack backtrace
398 * addresses. Protected by the graph_lock.
399 */
400 unsigned long nr_stack_trace_entries;
401 static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
402
403 static void print_lockdep_off(const char *bug_msg)
404 {
405 printk(KERN_DEBUG "%s\n", bug_msg);
406 printk(KERN_DEBUG "turning off the locking correctness validator.\n");
407 #ifdef CONFIG_LOCK_STAT
408 printk(KERN_DEBUG "Please attach the output of /proc/lock_stat to the bug report\n");
409 #endif
410 }
411
412 static int save_trace(struct stack_trace *trace)
413 {
414 trace->nr_entries = 0;
415 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
416 trace->entries = stack_trace + nr_stack_trace_entries;
417
418 trace->skip = 3;
419
420 save_stack_trace(trace);
421
422 /*
423 * Some daft arches put -1 at the end to indicate its a full trace.
424 *
425 * <rant> this is buggy anyway, since it takes a whole extra entry so a
426 * complete trace that maxes out the entries provided will be reported
427 * as incomplete, friggin useless </rant>
428 */
429 if (trace->nr_entries != 0 &&
430 trace->entries[trace->nr_entries-1] == ULONG_MAX)
431 trace->nr_entries--;
432
433 trace->max_entries = trace->nr_entries;
434
435 nr_stack_trace_entries += trace->nr_entries;
436
437 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
438 if (!debug_locks_off_graph_unlock())
439 return 0;
440
441 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
442 dump_stack();
443
444 return 0;
445 }
446
447 return 1;
448 }
449
450 unsigned int nr_hardirq_chains;
451 unsigned int nr_softirq_chains;
452 unsigned int nr_process_chains;
453 unsigned int max_lockdep_depth;
454
455 #ifdef CONFIG_DEBUG_LOCKDEP
456 /*
457 * Various lockdep statistics:
458 */
459 DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
460 #endif
461
462 /*
463 * Locking printouts:
464 */
465
466 #define __USAGE(__STATE) \
467 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
468 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
469 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
470 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
471
472 static const char *usage_str[] =
473 {
474 #define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
475 #include "lockdep_states.h"
476 #undef LOCKDEP_STATE
477 [LOCK_USED] = "INITIAL USE",
478 };
479
480 const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
481 {
482 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
483 }
484
485 static inline unsigned long lock_flag(enum lock_usage_bit bit)
486 {
487 return 1UL << bit;
488 }
489
490 static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
491 {
492 char c = '.';
493
494 if (class->usage_mask & lock_flag(bit + 2))
495 c = '+';
496 if (class->usage_mask & lock_flag(bit)) {
497 c = '-';
498 if (class->usage_mask & lock_flag(bit + 2))
499 c = '?';
500 }
501
502 return c;
503 }
504
505 void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
506 {
507 int i = 0;
508
509 #define LOCKDEP_STATE(__STATE) \
510 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
511 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
512 #include "lockdep_states.h"
513 #undef LOCKDEP_STATE
514
515 usage[i] = '\0';
516 }
517
518 static void __print_lock_name(struct lock_class *class)
519 {
520 char str[KSYM_NAME_LEN];
521 const char *name;
522
523 name = class->name;
524 if (!name) {
525 name = __get_key_name(class->key, str);
526 printk(KERN_CONT "%s", name);
527 } else {
528 printk(KERN_CONT "%s", name);
529 if (class->name_version > 1)
530 printk(KERN_CONT "#%d", class->name_version);
531 if (class->subclass)
532 printk(KERN_CONT "/%d", class->subclass);
533 }
534 }
535
536 static void print_lock_name(struct lock_class *class)
537 {
538 char usage[LOCK_USAGE_CHARS];
539
540 get_usage_chars(class, usage);
541
542 printk(KERN_CONT " (");
543 __print_lock_name(class);
544 printk(KERN_CONT "){%s}", usage);
545 }
546
547 static void print_lockdep_cache(struct lockdep_map *lock)
548 {
549 const char *name;
550 char str[KSYM_NAME_LEN];
551
552 name = lock->name;
553 if (!name)
554 name = __get_key_name(lock->key->subkeys, str);
555
556 printk(KERN_CONT "%s", name);
557 }
558
559 static void print_lock(struct held_lock *hlock)
560 {
561 /*
562 * We can be called locklessly through debug_show_all_locks() so be
563 * extra careful, the hlock might have been released and cleared.
564 */
565 unsigned int class_idx = hlock->class_idx;
566
567 /* Don't re-read hlock->class_idx, can't use READ_ONCE() on bitfields: */
568 barrier();
569
570 if (!class_idx || (class_idx - 1) >= MAX_LOCKDEP_KEYS) {
571 printk(KERN_CONT "<RELEASED>\n");
572 return;
573 }
574
575 print_lock_name(lock_classes + class_idx - 1);
576 printk(KERN_CONT ", at: [<%p>] %pS\n",
577 (void *)hlock->acquire_ip, (void *)hlock->acquire_ip);
578 }
579
580 static void lockdep_print_held_locks(struct task_struct *curr)
581 {
582 int i, depth = curr->lockdep_depth;
583
584 if (!depth) {
585 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
586 return;
587 }
588 printk("%d lock%s held by %s/%d:\n",
589 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
590
591 for (i = 0; i < depth; i++) {
592 printk(" #%d: ", i);
593 print_lock(curr->held_locks + i);
594 }
595 }
596
597 static void print_kernel_ident(void)
598 {
599 printk("%s %.*s %s\n", init_utsname()->release,
600 (int)strcspn(init_utsname()->version, " "),
601 init_utsname()->version,
602 print_tainted());
603 }
604
605 static int very_verbose(struct lock_class *class)
606 {
607 #if VERY_VERBOSE
608 return class_filter(class);
609 #endif
610 return 0;
611 }
612
613 /*
614 * Is this the address of a static object:
615 */
616 #ifdef __KERNEL__
617 static int static_obj(void *obj)
618 {
619 unsigned long start = (unsigned long) &_stext,
620 end = (unsigned long) &_end,
621 addr = (unsigned long) obj;
622
623 /*
624 * static variable?
625 */
626 if ((addr >= start) && (addr < end))
627 return 1;
628
629 if (arch_is_kernel_data(addr))
630 return 1;
631
632 /*
633 * in-kernel percpu var?
634 */
635 if (is_kernel_percpu_address(addr))
636 return 1;
637
638 /*
639 * module static or percpu var?
640 */
641 return is_module_address(addr) || is_module_percpu_address(addr);
642 }
643 #endif
644
645 /*
646 * To make lock name printouts unique, we calculate a unique
647 * class->name_version generation counter:
648 */
649 static int count_matching_names(struct lock_class *new_class)
650 {
651 struct lock_class *class;
652 int count = 0;
653
654 if (!new_class->name)
655 return 0;
656
657 list_for_each_entry_rcu(class, &all_lock_classes, lock_entry) {
658 if (new_class->key - new_class->subclass == class->key)
659 return class->name_version;
660 if (class->name && !strcmp(class->name, new_class->name))
661 count = max(count, class->name_version);
662 }
663
664 return count + 1;
665 }
666
667 /*
668 * Register a lock's class in the hash-table, if the class is not present
669 * yet. Otherwise we look it up. We cache the result in the lock object
670 * itself, so actual lookup of the hash should be once per lock object.
671 */
672 static inline struct lock_class *
673 look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
674 {
675 struct lockdep_subclass_key *key;
676 struct hlist_head *hash_head;
677 struct lock_class *class;
678 bool is_static = false;
679
680 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
681 debug_locks_off();
682 printk(KERN_ERR
683 "BUG: looking up invalid subclass: %u\n", subclass);
684 printk(KERN_ERR
685 "turning off the locking correctness validator.\n");
686 dump_stack();
687 return NULL;
688 }
689
690 /*
691 * Static locks do not have their class-keys yet - for them the key
692 * is the lock object itself. If the lock is in the per cpu area,
693 * the canonical address of the lock (per cpu offset removed) is
694 * used.
695 */
696 if (unlikely(!lock->key)) {
697 unsigned long can_addr, addr = (unsigned long)lock;
698
699 if (__is_kernel_percpu_address(addr, &can_addr))
700 lock->key = (void *)can_addr;
701 else if (__is_module_percpu_address(addr, &can_addr))
702 lock->key = (void *)can_addr;
703 else if (static_obj(lock))
704 lock->key = (void *)lock;
705 else
706 return ERR_PTR(-EINVAL);
707 is_static = true;
708 }
709
710 /*
711 * NOTE: the class-key must be unique. For dynamic locks, a static
712 * lock_class_key variable is passed in through the mutex_init()
713 * (or spin_lock_init()) call - which acts as the key. For static
714 * locks we use the lock object itself as the key.
715 */
716 BUILD_BUG_ON(sizeof(struct lock_class_key) >
717 sizeof(struct lockdep_map));
718
719 key = lock->key->subkeys + subclass;
720
721 hash_head = classhashentry(key);
722
723 /*
724 * We do an RCU walk of the hash, see lockdep_free_key_range().
725 */
726 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
727 return NULL;
728
729 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
730 if (class->key == key) {
731 /*
732 * Huh! same key, different name? Did someone trample
733 * on some memory? We're most confused.
734 */
735 WARN_ON_ONCE(class->name != lock->name);
736 return class;
737 }
738 }
739
740 return is_static || static_obj(lock->key) ? NULL : ERR_PTR(-EINVAL);
741 }
742
743 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
744 static void cross_init(struct lockdep_map *lock, int cross);
745 static int cross_lock(struct lockdep_map *lock);
746 static int lock_acquire_crosslock(struct held_lock *hlock);
747 static int lock_release_crosslock(struct lockdep_map *lock);
748 #else
749 static inline void cross_init(struct lockdep_map *lock, int cross) {}
750 static inline int cross_lock(struct lockdep_map *lock) { return 0; }
751 static inline int lock_acquire_crosslock(struct held_lock *hlock) { return 2; }
752 static inline int lock_release_crosslock(struct lockdep_map *lock) { return 2; }
753 #endif
754
755 /*
756 * Register a lock's class in the hash-table, if the class is not present
757 * yet. Otherwise we look it up. We cache the result in the lock object
758 * itself, so actual lookup of the hash should be once per lock object.
759 */
760 static struct lock_class *
761 register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
762 {
763 struct lockdep_subclass_key *key;
764 struct hlist_head *hash_head;
765 struct lock_class *class;
766
767 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
768
769 class = look_up_lock_class(lock, subclass);
770 if (likely(!IS_ERR_OR_NULL(class)))
771 goto out_set_class_cache;
772
773 /*
774 * Debug-check: all keys must be persistent!
775 */
776 if (IS_ERR(class)) {
777 debug_locks_off();
778 printk("INFO: trying to register non-static key.\n");
779 printk("the code is fine but needs lockdep annotation.\n");
780 printk("turning off the locking correctness validator.\n");
781 dump_stack();
782 return NULL;
783 }
784
785 key = lock->key->subkeys + subclass;
786 hash_head = classhashentry(key);
787
788 if (!graph_lock()) {
789 return NULL;
790 }
791 /*
792 * We have to do the hash-walk again, to avoid races
793 * with another CPU:
794 */
795 hlist_for_each_entry_rcu(class, hash_head, hash_entry) {
796 if (class->key == key)
797 goto out_unlock_set;
798 }
799
800 /*
801 * Allocate a new key from the static array, and add it to
802 * the hash:
803 */
804 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
805 if (!debug_locks_off_graph_unlock()) {
806 return NULL;
807 }
808
809 print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!");
810 dump_stack();
811 return NULL;
812 }
813 class = lock_classes + nr_lock_classes++;
814 debug_atomic_inc(nr_unused_locks);
815 class->key = key;
816 class->name = lock->name;
817 class->subclass = subclass;
818 INIT_LIST_HEAD(&class->lock_entry);
819 INIT_LIST_HEAD(&class->locks_before);
820 INIT_LIST_HEAD(&class->locks_after);
821 class->name_version = count_matching_names(class);
822 /*
823 * We use RCU's safe list-add method to make
824 * parallel walking of the hash-list safe:
825 */
826 hlist_add_head_rcu(&class->hash_entry, hash_head);
827 /*
828 * Add it to the global list of classes:
829 */
830 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
831
832 if (verbose(class)) {
833 graph_unlock();
834
835 printk("\nnew class %p: %s", class->key, class->name);
836 if (class->name_version > 1)
837 printk(KERN_CONT "#%d", class->name_version);
838 printk(KERN_CONT "\n");
839 dump_stack();
840
841 if (!graph_lock()) {
842 return NULL;
843 }
844 }
845 out_unlock_set:
846 graph_unlock();
847
848 out_set_class_cache:
849 if (!subclass || force)
850 lock->class_cache[0] = class;
851 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
852 lock->class_cache[subclass] = class;
853
854 /*
855 * Hash collision, did we smoke some? We found a class with a matching
856 * hash but the subclass -- which is hashed in -- didn't match.
857 */
858 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
859 return NULL;
860
861 return class;
862 }
863
864 #ifdef CONFIG_PROVE_LOCKING
865 /*
866 * Allocate a lockdep entry. (assumes the graph_lock held, returns
867 * with NULL on failure)
868 */
869 static struct lock_list *alloc_list_entry(void)
870 {
871 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
872 if (!debug_locks_off_graph_unlock())
873 return NULL;
874
875 print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!");
876 dump_stack();
877 return NULL;
878 }
879 return list_entries + nr_list_entries++;
880 }
881
882 /*
883 * Add a new dependency to the head of the list:
884 */
885 static int add_lock_to_list(struct lock_class *this, struct list_head *head,
886 unsigned long ip, int distance,
887 struct stack_trace *trace)
888 {
889 struct lock_list *entry;
890 /*
891 * Lock not present yet - get a new dependency struct and
892 * add it to the list:
893 */
894 entry = alloc_list_entry();
895 if (!entry)
896 return 0;
897
898 entry->class = this;
899 entry->distance = distance;
900 entry->trace = *trace;
901 /*
902 * Both allocation and removal are done under the graph lock; but
903 * iteration is under RCU-sched; see look_up_lock_class() and
904 * lockdep_free_key_range().
905 */
906 list_add_tail_rcu(&entry->entry, head);
907
908 return 1;
909 }
910
911 /*
912 * For good efficiency of modular, we use power of 2
913 */
914 #define MAX_CIRCULAR_QUEUE_SIZE 4096UL
915 #define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
916
917 /*
918 * The circular_queue and helpers is used to implement the
919 * breadth-first search(BFS)algorithem, by which we can build
920 * the shortest path from the next lock to be acquired to the
921 * previous held lock if there is a circular between them.
922 */
923 struct circular_queue {
924 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
925 unsigned int front, rear;
926 };
927
928 static struct circular_queue lock_cq;
929
930 unsigned int max_bfs_queue_depth;
931
932 static unsigned int lockdep_dependency_gen_id;
933
934 static inline void __cq_init(struct circular_queue *cq)
935 {
936 cq->front = cq->rear = 0;
937 lockdep_dependency_gen_id++;
938 }
939
940 static inline int __cq_empty(struct circular_queue *cq)
941 {
942 return (cq->front == cq->rear);
943 }
944
945 static inline int __cq_full(struct circular_queue *cq)
946 {
947 return ((cq->rear + 1) & CQ_MASK) == cq->front;
948 }
949
950 static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
951 {
952 if (__cq_full(cq))
953 return -1;
954
955 cq->element[cq->rear] = elem;
956 cq->rear = (cq->rear + 1) & CQ_MASK;
957 return 0;
958 }
959
960 static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
961 {
962 if (__cq_empty(cq))
963 return -1;
964
965 *elem = cq->element[cq->front];
966 cq->front = (cq->front + 1) & CQ_MASK;
967 return 0;
968 }
969
970 static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
971 {
972 return (cq->rear - cq->front) & CQ_MASK;
973 }
974
975 static inline void mark_lock_accessed(struct lock_list *lock,
976 struct lock_list *parent)
977 {
978 unsigned long nr;
979
980 nr = lock - list_entries;
981 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
982 lock->parent = parent;
983 lock->class->dep_gen_id = lockdep_dependency_gen_id;
984 }
985
986 static inline unsigned long lock_accessed(struct lock_list *lock)
987 {
988 unsigned long nr;
989
990 nr = lock - list_entries;
991 WARN_ON(nr >= nr_list_entries); /* Out-of-bounds, input fail */
992 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
993 }
994
995 static inline struct lock_list *get_lock_parent(struct lock_list *child)
996 {
997 return child->parent;
998 }
999
1000 static inline int get_lock_depth(struct lock_list *child)
1001 {
1002 int depth = 0;
1003 struct lock_list *parent;
1004
1005 while ((parent = get_lock_parent(child))) {
1006 child = parent;
1007 depth++;
1008 }
1009 return depth;
1010 }
1011
1012 static int __bfs(struct lock_list *source_entry,
1013 void *data,
1014 int (*match)(struct lock_list *entry, void *data),
1015 struct lock_list **target_entry,
1016 int forward)
1017 {
1018 struct lock_list *entry;
1019 struct list_head *head;
1020 struct circular_queue *cq = &lock_cq;
1021 int ret = 1;
1022
1023 if (match(source_entry, data)) {
1024 *target_entry = source_entry;
1025 ret = 0;
1026 goto exit;
1027 }
1028
1029 if (forward)
1030 head = &source_entry->class->locks_after;
1031 else
1032 head = &source_entry->class->locks_before;
1033
1034 if (list_empty(head))
1035 goto exit;
1036
1037 __cq_init(cq);
1038 __cq_enqueue(cq, (unsigned long)source_entry);
1039
1040 while (!__cq_empty(cq)) {
1041 struct lock_list *lock;
1042
1043 __cq_dequeue(cq, (unsigned long *)&lock);
1044
1045 if (!lock->class) {
1046 ret = -2;
1047 goto exit;
1048 }
1049
1050 if (forward)
1051 head = &lock->class->locks_after;
1052 else
1053 head = &lock->class->locks_before;
1054
1055 DEBUG_LOCKS_WARN_ON(!irqs_disabled());
1056
1057 list_for_each_entry_rcu(entry, head, entry) {
1058 if (!lock_accessed(entry)) {
1059 unsigned int cq_depth;
1060 mark_lock_accessed(entry, lock);
1061 if (match(entry, data)) {
1062 *target_entry = entry;
1063 ret = 0;
1064 goto exit;
1065 }
1066
1067 if (__cq_enqueue(cq, (unsigned long)entry)) {
1068 ret = -1;
1069 goto exit;
1070 }
1071 cq_depth = __cq_get_elem_count(cq);
1072 if (max_bfs_queue_depth < cq_depth)
1073 max_bfs_queue_depth = cq_depth;
1074 }
1075 }
1076 }
1077 exit:
1078 return ret;
1079 }
1080
1081 static inline int __bfs_forwards(struct lock_list *src_entry,
1082 void *data,
1083 int (*match)(struct lock_list *entry, void *data),
1084 struct lock_list **target_entry)
1085 {
1086 return __bfs(src_entry, data, match, target_entry, 1);
1087
1088 }
1089
1090 static inline int __bfs_backwards(struct lock_list *src_entry,
1091 void *data,
1092 int (*match)(struct lock_list *entry, void *data),
1093 struct lock_list **target_entry)
1094 {
1095 return __bfs(src_entry, data, match, target_entry, 0);
1096
1097 }
1098
1099 /*
1100 * Recursive, forwards-direction lock-dependency checking, used for
1101 * both noncyclic checking and for hardirq-unsafe/softirq-unsafe
1102 * checking.
1103 */
1104
1105 /*
1106 * Print a dependency chain entry (this is only done when a deadlock
1107 * has been detected):
1108 */
1109 static noinline int
1110 print_circular_bug_entry(struct lock_list *target, int depth)
1111 {
1112 if (debug_locks_silent)
1113 return 0;
1114 printk("\n-> #%u", depth);
1115 print_lock_name(target->class);
1116 printk(KERN_CONT ":\n");
1117 print_stack_trace(&target->trace, 6);
1118
1119 return 0;
1120 }
1121
1122 static void
1123 print_circular_lock_scenario(struct held_lock *src,
1124 struct held_lock *tgt,
1125 struct lock_list *prt)
1126 {
1127 struct lock_class *source = hlock_class(src);
1128 struct lock_class *target = hlock_class(tgt);
1129 struct lock_class *parent = prt->class;
1130
1131 /*
1132 * A direct locking problem where unsafe_class lock is taken
1133 * directly by safe_class lock, then all we need to show
1134 * is the deadlock scenario, as it is obvious that the
1135 * unsafe lock is taken under the safe lock.
1136 *
1137 * But if there is a chain instead, where the safe lock takes
1138 * an intermediate lock (middle_class) where this lock is
1139 * not the same as the safe lock, then the lock chain is
1140 * used to describe the problem. Otherwise we would need
1141 * to show a different CPU case for each link in the chain
1142 * from the safe_class lock to the unsafe_class lock.
1143 */
1144 if (parent != source) {
1145 printk("Chain exists of:\n ");
1146 __print_lock_name(source);
1147 printk(KERN_CONT " --> ");
1148 __print_lock_name(parent);
1149 printk(KERN_CONT " --> ");
1150 __print_lock_name(target);
1151 printk(KERN_CONT "\n\n");
1152 }
1153
1154 if (cross_lock(tgt->instance)) {
1155 printk(" Possible unsafe locking scenario by crosslock:\n\n");
1156 printk(" CPU0 CPU1\n");
1157 printk(" ---- ----\n");
1158 printk(" lock(");
1159 __print_lock_name(parent);
1160 printk(KERN_CONT ");\n");
1161 printk(" lock(");
1162 __print_lock_name(target);
1163 printk(KERN_CONT ");\n");
1164 printk(" lock(");
1165 __print_lock_name(source);
1166 printk(KERN_CONT ");\n");
1167 printk(" unlock(");
1168 __print_lock_name(target);
1169 printk(KERN_CONT ");\n");
1170 printk("\n *** DEADLOCK ***\n\n");
1171 } else {
1172 printk(" Possible unsafe locking scenario:\n\n");
1173 printk(" CPU0 CPU1\n");
1174 printk(" ---- ----\n");
1175 printk(" lock(");
1176 __print_lock_name(target);
1177 printk(KERN_CONT ");\n");
1178 printk(" lock(");
1179 __print_lock_name(parent);
1180 printk(KERN_CONT ");\n");
1181 printk(" lock(");
1182 __print_lock_name(target);
1183 printk(KERN_CONT ");\n");
1184 printk(" lock(");
1185 __print_lock_name(source);
1186 printk(KERN_CONT ");\n");
1187 printk("\n *** DEADLOCK ***\n\n");
1188 }
1189 }
1190
1191 /*
1192 * When a circular dependency is detected, print the
1193 * header first:
1194 */
1195 static noinline int
1196 print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1197 struct held_lock *check_src,
1198 struct held_lock *check_tgt)
1199 {
1200 struct task_struct *curr = current;
1201
1202 if (debug_locks_silent)
1203 return 0;
1204
1205 pr_warn("\n");
1206 pr_warn("======================================================\n");
1207 pr_warn("WARNING: possible circular locking dependency detected\n");
1208 print_kernel_ident();
1209 pr_warn("------------------------------------------------------\n");
1210 pr_warn("%s/%d is trying to acquire lock:\n",
1211 curr->comm, task_pid_nr(curr));
1212 print_lock(check_src);
1213
1214 if (cross_lock(check_tgt->instance))
1215 pr_warn("\nbut now in release context of a crosslock acquired at the following:\n");
1216 else
1217 pr_warn("\nbut task is already holding lock:\n");
1218
1219 print_lock(check_tgt);
1220 pr_warn("\nwhich lock already depends on the new lock.\n\n");
1221 pr_warn("\nthe existing dependency chain (in reverse order) is:\n");
1222
1223 print_circular_bug_entry(entry, depth);
1224
1225 return 0;
1226 }
1227
1228 static inline int class_equal(struct lock_list *entry, void *data)
1229 {
1230 return entry->class == data;
1231 }
1232
1233 static noinline int print_circular_bug(struct lock_list *this,
1234 struct lock_list *target,
1235 struct held_lock *check_src,
1236 struct held_lock *check_tgt,
1237 struct stack_trace *trace)
1238 {
1239 struct task_struct *curr = current;
1240 struct lock_list *parent;
1241 struct lock_list *first_parent;
1242 int depth;
1243
1244 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1245 return 0;
1246
1247 if (cross_lock(check_tgt->instance))
1248 this->trace = *trace;
1249 else if (!save_trace(&this->trace))
1250 return 0;
1251
1252 depth = get_lock_depth(target);
1253
1254 print_circular_bug_header(target, depth, check_src, check_tgt);
1255
1256 parent = get_lock_parent(target);
1257 first_parent = parent;
1258
1259 while (parent) {
1260 print_circular_bug_entry(parent, --depth);
1261 parent = get_lock_parent(parent);
1262 }
1263
1264 printk("\nother info that might help us debug this:\n\n");
1265 print_circular_lock_scenario(check_src, check_tgt,
1266 first_parent);
1267
1268 lockdep_print_held_locks(curr);
1269
1270 printk("\nstack backtrace:\n");
1271 dump_stack();
1272
1273 return 0;
1274 }
1275
1276 static noinline int print_bfs_bug(int ret)
1277 {
1278 if (!debug_locks_off_graph_unlock())
1279 return 0;
1280
1281 /*
1282 * Breadth-first-search failed, graph got corrupted?
1283 */
1284 WARN(1, "lockdep bfs error:%d\n", ret);
1285
1286 return 0;
1287 }
1288
1289 static int noop_count(struct lock_list *entry, void *data)
1290 {
1291 (*(unsigned long *)data)++;
1292 return 0;
1293 }
1294
1295 static unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1296 {
1297 unsigned long count = 0;
1298 struct lock_list *uninitialized_var(target_entry);
1299
1300 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1301
1302 return count;
1303 }
1304 unsigned long lockdep_count_forward_deps(struct lock_class *class)
1305 {
1306 unsigned long ret, flags;
1307 struct lock_list this;
1308
1309 this.parent = NULL;
1310 this.class = class;
1311
1312 local_irq_save(flags);
1313 arch_spin_lock(&lockdep_lock);
1314 ret = __lockdep_count_forward_deps(&this);
1315 arch_spin_unlock(&lockdep_lock);
1316 local_irq_restore(flags);
1317
1318 return ret;
1319 }
1320
1321 static unsigned long __lockdep_count_backward_deps(struct lock_list *this)
1322 {
1323 unsigned long count = 0;
1324 struct lock_list *uninitialized_var(target_entry);
1325
1326 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
1327
1328 return count;
1329 }
1330
1331 unsigned long lockdep_count_backward_deps(struct lock_class *class)
1332 {
1333 unsigned long ret, flags;
1334 struct lock_list this;
1335
1336 this.parent = NULL;
1337 this.class = class;
1338
1339 local_irq_save(flags);
1340 arch_spin_lock(&lockdep_lock);
1341 ret = __lockdep_count_backward_deps(&this);
1342 arch_spin_unlock(&lockdep_lock);
1343 local_irq_restore(flags);
1344
1345 return ret;
1346 }
1347
1348 /*
1349 * Prove that the dependency graph starting at <entry> can not
1350 * lead to <target>. Print an error and return 0 if it does.
1351 */
1352 static noinline int
1353 check_noncircular(struct lock_list *root, struct lock_class *target,
1354 struct lock_list **target_entry)
1355 {
1356 int result;
1357
1358 debug_atomic_inc(nr_cyclic_checks);
1359
1360 result = __bfs_forwards(root, target, class_equal, target_entry);
1361
1362 return result;
1363 }
1364
1365 static noinline int
1366 check_redundant(struct lock_list *root, struct lock_class *target,
1367 struct lock_list **target_entry)
1368 {
1369 int result;
1370
1371 debug_atomic_inc(nr_redundant_checks);
1372
1373 result = __bfs_forwards(root, target, class_equal, target_entry);
1374
1375 return result;
1376 }
1377
1378 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1379 /*
1380 * Forwards and backwards subgraph searching, for the purposes of
1381 * proving that two subgraphs can be connected by a new dependency
1382 * without creating any illegal irq-safe -> irq-unsafe lock dependency.
1383 */
1384
1385 static inline int usage_match(struct lock_list *entry, void *bit)
1386 {
1387 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1388 }
1389
1390
1391
1392 /*
1393 * Find a node in the forwards-direction dependency sub-graph starting
1394 * at @root->class that matches @bit.
1395 *
1396 * Return 0 if such a node exists in the subgraph, and put that node
1397 * into *@target_entry.
1398 *
1399 * Return 1 otherwise and keep *@target_entry unchanged.
1400 * Return <0 on error.
1401 */
1402 static int
1403 find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1404 struct lock_list **target_entry)
1405 {
1406 int result;
1407
1408 debug_atomic_inc(nr_find_usage_forwards_checks);
1409
1410 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1411
1412 return result;
1413 }
1414
1415 /*
1416 * Find a node in the backwards-direction dependency sub-graph starting
1417 * at @root->class that matches @bit.
1418 *
1419 * Return 0 if such a node exists in the subgraph, and put that node
1420 * into *@target_entry.
1421 *
1422 * Return 1 otherwise and keep *@target_entry unchanged.
1423 * Return <0 on error.
1424 */
1425 static int
1426 find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1427 struct lock_list **target_entry)
1428 {
1429 int result;
1430
1431 debug_atomic_inc(nr_find_usage_backwards_checks);
1432
1433 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
1434
1435 return result;
1436 }
1437
1438 static void print_lock_class_header(struct lock_class *class, int depth)
1439 {
1440 int bit;
1441
1442 printk("%*s->", depth, "");
1443 print_lock_name(class);
1444 printk(KERN_CONT " ops: %lu", class->ops);
1445 printk(KERN_CONT " {\n");
1446
1447 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1448 if (class->usage_mask & (1 << bit)) {
1449 int len = depth;
1450
1451 len += printk("%*s %s", depth, "", usage_str[bit]);
1452 len += printk(KERN_CONT " at:\n");
1453 print_stack_trace(class->usage_traces + bit, len);
1454 }
1455 }
1456 printk("%*s }\n", depth, "");
1457
1458 printk("%*s ... key at: [<%p>] %pS\n",
1459 depth, "", class->key, class->key);
1460 }
1461
1462 /*
1463 * printk the shortest lock dependencies from @start to @end in reverse order:
1464 */
1465 static void __used
1466 print_shortest_lock_dependencies(struct lock_list *leaf,
1467 struct lock_list *root)
1468 {
1469 struct lock_list *entry = leaf;
1470 int depth;
1471
1472 /*compute depth from generated tree by BFS*/
1473 depth = get_lock_depth(leaf);
1474
1475 do {
1476 print_lock_class_header(entry->class, depth);
1477 printk("%*s ... acquired at:\n", depth, "");
1478 print_stack_trace(&entry->trace, 2);
1479 printk("\n");
1480
1481 if (depth == 0 && (entry != root)) {
1482 printk("lockdep:%s bad path found in chain graph\n", __func__);
1483 break;
1484 }
1485
1486 entry = get_lock_parent(entry);
1487 depth--;
1488 } while (entry && (depth >= 0));
1489
1490 return;
1491 }
1492
1493 static void
1494 print_irq_lock_scenario(struct lock_list *safe_entry,
1495 struct lock_list *unsafe_entry,
1496 struct lock_class *prev_class,
1497 struct lock_class *next_class)
1498 {
1499 struct lock_class *safe_class = safe_entry->class;
1500 struct lock_class *unsafe_class = unsafe_entry->class;
1501 struct lock_class *middle_class = prev_class;
1502
1503 if (middle_class == safe_class)
1504 middle_class = next_class;
1505
1506 /*
1507 * A direct locking problem where unsafe_class lock is taken
1508 * directly by safe_class lock, then all we need to show
1509 * is the deadlock scenario, as it is obvious that the
1510 * unsafe lock is taken under the safe lock.
1511 *
1512 * But if there is a chain instead, where the safe lock takes
1513 * an intermediate lock (middle_class) where this lock is
1514 * not the same as the safe lock, then the lock chain is
1515 * used to describe the problem. Otherwise we would need
1516 * to show a different CPU case for each link in the chain
1517 * from the safe_class lock to the unsafe_class lock.
1518 */
1519 if (middle_class != unsafe_class) {
1520 printk("Chain exists of:\n ");
1521 __print_lock_name(safe_class);
1522 printk(KERN_CONT " --> ");
1523 __print_lock_name(middle_class);
1524 printk(KERN_CONT " --> ");
1525 __print_lock_name(unsafe_class);
1526 printk(KERN_CONT "\n\n");
1527 }
1528
1529 printk(" Possible interrupt unsafe locking scenario:\n\n");
1530 printk(" CPU0 CPU1\n");
1531 printk(" ---- ----\n");
1532 printk(" lock(");
1533 __print_lock_name(unsafe_class);
1534 printk(KERN_CONT ");\n");
1535 printk(" local_irq_disable();\n");
1536 printk(" lock(");
1537 __print_lock_name(safe_class);
1538 printk(KERN_CONT ");\n");
1539 printk(" lock(");
1540 __print_lock_name(middle_class);
1541 printk(KERN_CONT ");\n");
1542 printk(" <Interrupt>\n");
1543 printk(" lock(");
1544 __print_lock_name(safe_class);
1545 printk(KERN_CONT ");\n");
1546 printk("\n *** DEADLOCK ***\n\n");
1547 }
1548
1549 static int
1550 print_bad_irq_dependency(struct task_struct *curr,
1551 struct lock_list *prev_root,
1552 struct lock_list *next_root,
1553 struct lock_list *backwards_entry,
1554 struct lock_list *forwards_entry,
1555 struct held_lock *prev,
1556 struct held_lock *next,
1557 enum lock_usage_bit bit1,
1558 enum lock_usage_bit bit2,
1559 const char *irqclass)
1560 {
1561 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1562 return 0;
1563
1564 pr_warn("\n");
1565 pr_warn("=====================================================\n");
1566 pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n",
1567 irqclass, irqclass);
1568 print_kernel_ident();
1569 pr_warn("-----------------------------------------------------\n");
1570 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1571 curr->comm, task_pid_nr(curr),
1572 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1573 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1574 curr->hardirqs_enabled,
1575 curr->softirqs_enabled);
1576 print_lock(next);
1577
1578 pr_warn("\nand this task is already holding:\n");
1579 print_lock(prev);
1580 pr_warn("which would create a new lock dependency:\n");
1581 print_lock_name(hlock_class(prev));
1582 pr_cont(" ->");
1583 print_lock_name(hlock_class(next));
1584 pr_cont("\n");
1585
1586 pr_warn("\nbut this new dependency connects a %s-irq-safe lock:\n",
1587 irqclass);
1588 print_lock_name(backwards_entry->class);
1589 pr_warn("\n... which became %s-irq-safe at:\n", irqclass);
1590
1591 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1592
1593 pr_warn("\nto a %s-irq-unsafe lock:\n", irqclass);
1594 print_lock_name(forwards_entry->class);
1595 pr_warn("\n... which became %s-irq-unsafe at:\n", irqclass);
1596 pr_warn("...");
1597
1598 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1599
1600 pr_warn("\nother info that might help us debug this:\n\n");
1601 print_irq_lock_scenario(backwards_entry, forwards_entry,
1602 hlock_class(prev), hlock_class(next));
1603
1604 lockdep_print_held_locks(curr);
1605
1606 pr_warn("\nthe dependencies between %s-irq-safe lock and the holding lock:\n", irqclass);
1607 if (!save_trace(&prev_root->trace))
1608 return 0;
1609 print_shortest_lock_dependencies(backwards_entry, prev_root);
1610
1611 pr_warn("\nthe dependencies between the lock to be acquired");
1612 pr_warn(" and %s-irq-unsafe lock:\n", irqclass);
1613 if (!save_trace(&next_root->trace))
1614 return 0;
1615 print_shortest_lock_dependencies(forwards_entry, next_root);
1616
1617 pr_warn("\nstack backtrace:\n");
1618 dump_stack();
1619
1620 return 0;
1621 }
1622
1623 static int
1624 check_usage(struct task_struct *curr, struct held_lock *prev,
1625 struct held_lock *next, enum lock_usage_bit bit_backwards,
1626 enum lock_usage_bit bit_forwards, const char *irqclass)
1627 {
1628 int ret;
1629 struct lock_list this, that;
1630 struct lock_list *uninitialized_var(target_entry);
1631 struct lock_list *uninitialized_var(target_entry1);
1632
1633 this.parent = NULL;
1634
1635 this.class = hlock_class(prev);
1636 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1637 if (ret < 0)
1638 return print_bfs_bug(ret);
1639 if (ret == 1)
1640 return ret;
1641
1642 that.parent = NULL;
1643 that.class = hlock_class(next);
1644 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1645 if (ret < 0)
1646 return print_bfs_bug(ret);
1647 if (ret == 1)
1648 return ret;
1649
1650 return print_bad_irq_dependency(curr, &this, &that,
1651 target_entry, target_entry1,
1652 prev, next,
1653 bit_backwards, bit_forwards, irqclass);
1654 }
1655
1656 static const char *state_names[] = {
1657 #define LOCKDEP_STATE(__STATE) \
1658 __stringify(__STATE),
1659 #include "lockdep_states.h"
1660 #undef LOCKDEP_STATE
1661 };
1662
1663 static const char *state_rnames[] = {
1664 #define LOCKDEP_STATE(__STATE) \
1665 __stringify(__STATE)"-READ",
1666 #include "lockdep_states.h"
1667 #undef LOCKDEP_STATE
1668 };
1669
1670 static inline const char *state_name(enum lock_usage_bit bit)
1671 {
1672 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1673 }
1674
1675 static int exclusive_bit(int new_bit)
1676 {
1677 /*
1678 * USED_IN
1679 * USED_IN_READ
1680 * ENABLED
1681 * ENABLED_READ
1682 *
1683 * bit 0 - write/read
1684 * bit 1 - used_in/enabled
1685 * bit 2+ state
1686 */
1687
1688 int state = new_bit & ~3;
1689 int dir = new_bit & 2;
1690
1691 /*
1692 * keep state, bit flip the direction and strip read.
1693 */
1694 return state | (dir ^ 2);
1695 }
1696
1697 static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1698 struct held_lock *next, enum lock_usage_bit bit)
1699 {
1700 /*
1701 * Prove that the new dependency does not connect a hardirq-safe
1702 * lock with a hardirq-unsafe lock - to achieve this we search
1703 * the backwards-subgraph starting at <prev>, and the
1704 * forwards-subgraph starting at <next>:
1705 */
1706 if (!check_usage(curr, prev, next, bit,
1707 exclusive_bit(bit), state_name(bit)))
1708 return 0;
1709
1710 bit++; /* _READ */
1711
1712 /*
1713 * Prove that the new dependency does not connect a hardirq-safe-read
1714 * lock with a hardirq-unsafe lock - to achieve this we search
1715 * the backwards-subgraph starting at <prev>, and the
1716 * forwards-subgraph starting at <next>:
1717 */
1718 if (!check_usage(curr, prev, next, bit,
1719 exclusive_bit(bit), state_name(bit)))
1720 return 0;
1721
1722 return 1;
1723 }
1724
1725 static int
1726 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1727 struct held_lock *next)
1728 {
1729 #define LOCKDEP_STATE(__STATE) \
1730 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1731 return 0;
1732 #include "lockdep_states.h"
1733 #undef LOCKDEP_STATE
1734
1735 return 1;
1736 }
1737
1738 static void inc_chains(void)
1739 {
1740 if (current->hardirq_context)
1741 nr_hardirq_chains++;
1742 else {
1743 if (current->softirq_context)
1744 nr_softirq_chains++;
1745 else
1746 nr_process_chains++;
1747 }
1748 }
1749
1750 #else
1751
1752 static inline int
1753 check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1754 struct held_lock *next)
1755 {
1756 return 1;
1757 }
1758
1759 static inline void inc_chains(void)
1760 {
1761 nr_process_chains++;
1762 }
1763
1764 #endif
1765
1766 static void
1767 print_deadlock_scenario(struct held_lock *nxt,
1768 struct held_lock *prv)
1769 {
1770 struct lock_class *next = hlock_class(nxt);
1771 struct lock_class *prev = hlock_class(prv);
1772
1773 printk(" Possible unsafe locking scenario:\n\n");
1774 printk(" CPU0\n");
1775 printk(" ----\n");
1776 printk(" lock(");
1777 __print_lock_name(prev);
1778 printk(KERN_CONT ");\n");
1779 printk(" lock(");
1780 __print_lock_name(next);
1781 printk(KERN_CONT ");\n");
1782 printk("\n *** DEADLOCK ***\n\n");
1783 printk(" May be due to missing lock nesting notation\n\n");
1784 }
1785
1786 static int
1787 print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1788 struct held_lock *next)
1789 {
1790 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1791 return 0;
1792
1793 pr_warn("\n");
1794 pr_warn("============================================\n");
1795 pr_warn("WARNING: possible recursive locking detected\n");
1796 print_kernel_ident();
1797 pr_warn("--------------------------------------------\n");
1798 pr_warn("%s/%d is trying to acquire lock:\n",
1799 curr->comm, task_pid_nr(curr));
1800 print_lock(next);
1801 pr_warn("\nbut task is already holding lock:\n");
1802 print_lock(prev);
1803
1804 pr_warn("\nother info that might help us debug this:\n");
1805 print_deadlock_scenario(next, prev);
1806 lockdep_print_held_locks(curr);
1807
1808 pr_warn("\nstack backtrace:\n");
1809 dump_stack();
1810
1811 return 0;
1812 }
1813
1814 /*
1815 * Check whether we are holding such a class already.
1816 *
1817 * (Note that this has to be done separately, because the graph cannot
1818 * detect such classes of deadlocks.)
1819 *
1820 * Returns: 0 on deadlock detected, 1 on OK, 2 on recursive read
1821 */
1822 static int
1823 check_deadlock(struct task_struct *curr, struct held_lock *next,
1824 struct lockdep_map *next_instance, int read)
1825 {
1826 struct held_lock *prev;
1827 struct held_lock *nest = NULL;
1828 int i;
1829
1830 for (i = 0; i < curr->lockdep_depth; i++) {
1831 prev = curr->held_locks + i;
1832
1833 if (prev->instance == next->nest_lock)
1834 nest = prev;
1835
1836 if (hlock_class(prev) != hlock_class(next))
1837 continue;
1838
1839 /*
1840 * Allow read-after-read recursion of the same
1841 * lock class (i.e. read_lock(lock)+read_lock(lock)):
1842 */
1843 if ((read == 2) && prev->read)
1844 return 2;
1845
1846 /*
1847 * We're holding the nest_lock, which serializes this lock's
1848 * nesting behaviour.
1849 */
1850 if (nest)
1851 return 2;
1852
1853 if (cross_lock(prev->instance))
1854 continue;
1855
1856 return print_deadlock_bug(curr, prev, next);
1857 }
1858 return 1;
1859 }
1860
1861 /*
1862 * There was a chain-cache miss, and we are about to add a new dependency
1863 * to a previous lock. We recursively validate the following rules:
1864 *
1865 * - would the adding of the <prev> -> <next> dependency create a
1866 * circular dependency in the graph? [== circular deadlock]
1867 *
1868 * - does the new prev->next dependency connect any hardirq-safe lock
1869 * (in the full backwards-subgraph starting at <prev>) with any
1870 * hardirq-unsafe lock (in the full forwards-subgraph starting at
1871 * <next>)? [== illegal lock inversion with hardirq contexts]
1872 *
1873 * - does the new prev->next dependency connect any softirq-safe lock
1874 * (in the full backwards-subgraph starting at <prev>) with any
1875 * softirq-unsafe lock (in the full forwards-subgraph starting at
1876 * <next>)? [== illegal lock inversion with softirq contexts]
1877 *
1878 * any of these scenarios could lead to a deadlock.
1879 *
1880 * Then if all the validations pass, we add the forwards and backwards
1881 * dependency.
1882 */
1883 static int
1884 check_prev_add(struct task_struct *curr, struct held_lock *prev,
1885 struct held_lock *next, int distance, struct stack_trace *trace,
1886 int (*save)(struct stack_trace *trace))
1887 {
1888 struct lock_list *uninitialized_var(target_entry);
1889 struct lock_list *entry;
1890 struct lock_list this;
1891 int ret;
1892
1893 /*
1894 * Prove that the new <prev> -> <next> dependency would not
1895 * create a circular dependency in the graph. (We do this by
1896 * forward-recursing into the graph starting at <next>, and
1897 * checking whether we can reach <prev>.)
1898 *
1899 * We are using global variables to control the recursion, to
1900 * keep the stackframe size of the recursive functions low:
1901 */
1902 this.class = hlock_class(next);
1903 this.parent = NULL;
1904 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1905 if (unlikely(!ret)) {
1906 if (!trace->entries) {
1907 /*
1908 * If @save fails here, the printing might trigger
1909 * a WARN but because of the !nr_entries it should
1910 * not do bad things.
1911 */
1912 save(trace);
1913 }
1914 return print_circular_bug(&this, target_entry, next, prev, trace);
1915 }
1916 else if (unlikely(ret < 0))
1917 return print_bfs_bug(ret);
1918
1919 if (!check_prev_add_irq(curr, prev, next))
1920 return 0;
1921
1922 /*
1923 * For recursive read-locks we do all the dependency checks,
1924 * but we dont store read-triggered dependencies (only
1925 * write-triggered dependencies). This ensures that only the
1926 * write-side dependencies matter, and that if for example a
1927 * write-lock never takes any other locks, then the reads are
1928 * equivalent to a NOP.
1929 */
1930 if (next->read == 2 || prev->read == 2)
1931 return 1;
1932 /*
1933 * Is the <prev> -> <next> dependency already present?
1934 *
1935 * (this may occur even though this is a new chain: consider
1936 * e.g. the L1 -> L2 -> L3 -> L4 and the L5 -> L1 -> L2 -> L3
1937 * chains - the second one will be new, but L1 already has
1938 * L2 added to its dependency list, due to the first chain.)
1939 */
1940 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1941 if (entry->class == hlock_class(next)) {
1942 if (distance == 1)
1943 entry->distance = 1;
1944 return 1;
1945 }
1946 }
1947
1948 /*
1949 * Is the <prev> -> <next> link redundant?
1950 */
1951 this.class = hlock_class(prev);
1952 this.parent = NULL;
1953 ret = check_redundant(&this, hlock_class(next), &target_entry);
1954 if (!ret) {
1955 debug_atomic_inc(nr_redundant);
1956 return 2;
1957 }
1958 if (ret < 0)
1959 return print_bfs_bug(ret);
1960
1961
1962 if (!trace->entries && !save(trace))
1963 return 0;
1964
1965 /*
1966 * Ok, all validations passed, add the new lock
1967 * to the previous lock's dependency list:
1968 */
1969 ret = add_lock_to_list(hlock_class(next),
1970 &hlock_class(prev)->locks_after,
1971 next->acquire_ip, distance, trace);
1972
1973 if (!ret)
1974 return 0;
1975
1976 ret = add_lock_to_list(hlock_class(prev),
1977 &hlock_class(next)->locks_before,
1978 next->acquire_ip, distance, trace);
1979 if (!ret)
1980 return 0;
1981
1982 return 2;
1983 }
1984
1985 /*
1986 * Add the dependency to all directly-previous locks that are 'relevant'.
1987 * The ones that are relevant are (in increasing distance from curr):
1988 * all consecutive trylock entries and the final non-trylock entry - or
1989 * the end of this context's lock-chain - whichever comes first.
1990 */
1991 static int
1992 check_prevs_add(struct task_struct *curr, struct held_lock *next)
1993 {
1994 int depth = curr->lockdep_depth;
1995 struct held_lock *hlock;
1996 struct stack_trace trace = {
1997 .nr_entries = 0,
1998 .max_entries = 0,
1999 .entries = NULL,
2000 .skip = 0,
2001 };
2002
2003 /*
2004 * Debugging checks.
2005 *
2006 * Depth must not be zero for a non-head lock:
2007 */
2008 if (!depth)
2009 goto out_bug;
2010 /*
2011 * At least two relevant locks must exist for this
2012 * to be a head:
2013 */
2014 if (curr->held_locks[depth].irq_context !=
2015 curr->held_locks[depth-1].irq_context)
2016 goto out_bug;
2017
2018 for (;;) {
2019 int distance = curr->lockdep_depth - depth + 1;
2020 hlock = curr->held_locks + depth - 1;
2021 /*
2022 * Only non-crosslock entries get new dependencies added.
2023 * Crosslock entries will be added by commit later:
2024 */
2025 if (!cross_lock(hlock->instance)) {
2026 /*
2027 * Only non-recursive-read entries get new dependencies
2028 * added:
2029 */
2030 if (hlock->read != 2 && hlock->check) {
2031 int ret = check_prev_add(curr, hlock, next,
2032 distance, &trace, save_trace);
2033 if (!ret)
2034 return 0;
2035
2036 /*
2037 * Stop after the first non-trylock entry,
2038 * as non-trylock entries have added their
2039 * own direct dependencies already, so this
2040 * lock is connected to them indirectly:
2041 */
2042 if (!hlock->trylock)
2043 break;
2044 }
2045 }
2046 depth--;
2047 /*
2048 * End of lock-stack?
2049 */
2050 if (!depth)
2051 break;
2052 /*
2053 * Stop the search if we cross into another context:
2054 */
2055 if (curr->held_locks[depth].irq_context !=
2056 curr->held_locks[depth-1].irq_context)
2057 break;
2058 }
2059 return 1;
2060 out_bug:
2061 if (!debug_locks_off_graph_unlock())
2062 return 0;
2063
2064 /*
2065 * Clearly we all shouldn't be here, but since we made it we
2066 * can reliable say we messed up our state. See the above two
2067 * gotos for reasons why we could possibly end up here.
2068 */
2069 WARN_ON(1);
2070
2071 return 0;
2072 }
2073
2074 unsigned long nr_lock_chains;
2075 struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
2076 int nr_chain_hlocks;
2077 static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
2078
2079 struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
2080 {
2081 return lock_classes + chain_hlocks[chain->base + i];
2082 }
2083
2084 /*
2085 * Returns the index of the first held_lock of the current chain
2086 */
2087 static inline int get_first_held_lock(struct task_struct *curr,
2088 struct held_lock *hlock)
2089 {
2090 int i;
2091 struct held_lock *hlock_curr;
2092
2093 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2094 hlock_curr = curr->held_locks + i;
2095 if (hlock_curr->irq_context != hlock->irq_context)
2096 break;
2097
2098 }
2099
2100 return ++i;
2101 }
2102
2103 #ifdef CONFIG_DEBUG_LOCKDEP
2104 /*
2105 * Returns the next chain_key iteration
2106 */
2107 static u64 print_chain_key_iteration(int class_idx, u64 chain_key)
2108 {
2109 u64 new_chain_key = iterate_chain_key(chain_key, class_idx);
2110
2111 printk(" class_idx:%d -> chain_key:%016Lx",
2112 class_idx,
2113 (unsigned long long)new_chain_key);
2114 return new_chain_key;
2115 }
2116
2117 static void
2118 print_chain_keys_held_locks(struct task_struct *curr, struct held_lock *hlock_next)
2119 {
2120 struct held_lock *hlock;
2121 u64 chain_key = 0;
2122 int depth = curr->lockdep_depth;
2123 int i;
2124
2125 printk("depth: %u\n", depth + 1);
2126 for (i = get_first_held_lock(curr, hlock_next); i < depth; i++) {
2127 hlock = curr->held_locks + i;
2128 chain_key = print_chain_key_iteration(hlock->class_idx, chain_key);
2129
2130 print_lock(hlock);
2131 }
2132
2133 print_chain_key_iteration(hlock_next->class_idx, chain_key);
2134 print_lock(hlock_next);
2135 }
2136
2137 static void print_chain_keys_chain(struct lock_chain *chain)
2138 {
2139 int i;
2140 u64 chain_key = 0;
2141 int class_id;
2142
2143 printk("depth: %u\n", chain->depth);
2144 for (i = 0; i < chain->depth; i++) {
2145 class_id = chain_hlocks[chain->base + i];
2146 chain_key = print_chain_key_iteration(class_id + 1, chain_key);
2147
2148 print_lock_name(lock_classes + class_id);
2149 printk("\n");
2150 }
2151 }
2152
2153 static void print_collision(struct task_struct *curr,
2154 struct held_lock *hlock_next,
2155 struct lock_chain *chain)
2156 {
2157 pr_warn("\n");
2158 pr_warn("============================\n");
2159 pr_warn("WARNING: chain_key collision\n");
2160 print_kernel_ident();
2161 pr_warn("----------------------------\n");
2162 pr_warn("%s/%d: ", current->comm, task_pid_nr(current));
2163 pr_warn("Hash chain already cached but the contents don't match!\n");
2164
2165 pr_warn("Held locks:");
2166 print_chain_keys_held_locks(curr, hlock_next);
2167
2168 pr_warn("Locks in cached chain:");
2169 print_chain_keys_chain(chain);
2170
2171 pr_warn("\nstack backtrace:\n");
2172 dump_stack();
2173 }
2174 #endif
2175
2176 /*
2177 * Checks whether the chain and the current held locks are consistent
2178 * in depth and also in content. If they are not it most likely means
2179 * that there was a collision during the calculation of the chain_key.
2180 * Returns: 0 not passed, 1 passed
2181 */
2182 static int check_no_collision(struct task_struct *curr,
2183 struct held_lock *hlock,
2184 struct lock_chain *chain)
2185 {
2186 #ifdef CONFIG_DEBUG_LOCKDEP
2187 int i, j, id;
2188
2189 i = get_first_held_lock(curr, hlock);
2190
2191 if (DEBUG_LOCKS_WARN_ON(chain->depth != curr->lockdep_depth - (i - 1))) {
2192 print_collision(curr, hlock, chain);
2193 return 0;
2194 }
2195
2196 for (j = 0; j < chain->depth - 1; j++, i++) {
2197 id = curr->held_locks[i].class_idx - 1;
2198
2199 if (DEBUG_LOCKS_WARN_ON(chain_hlocks[chain->base + j] != id)) {
2200 print_collision(curr, hlock, chain);
2201 return 0;
2202 }
2203 }
2204 #endif
2205 return 1;
2206 }
2207
2208 /*
2209 * This is for building a chain between just two different classes,
2210 * instead of adding a new hlock upon current, which is done by
2211 * add_chain_cache().
2212 *
2213 * This can be called in any context with two classes, while
2214 * add_chain_cache() must be done within the lock owener's context
2215 * since it uses hlock which might be racy in another context.
2216 */
2217 static inline int add_chain_cache_classes(unsigned int prev,
2218 unsigned int next,
2219 unsigned int irq_context,
2220 u64 chain_key)
2221 {
2222 struct hlist_head *hash_head = chainhashentry(chain_key);
2223 struct lock_chain *chain;
2224
2225 /*
2226 * Allocate a new chain entry from the static array, and add
2227 * it to the hash:
2228 */
2229
2230 /*
2231 * We might need to take the graph lock, ensure we've got IRQs
2232 * disabled to make this an IRQ-safe lock.. for recursion reasons
2233 * lockdep won't complain about its own locking errors.
2234 */
2235 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2236 return 0;
2237
2238 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2239 if (!debug_locks_off_graph_unlock())
2240 return 0;
2241
2242 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2243 dump_stack();
2244 return 0;
2245 }
2246
2247 chain = lock_chains + nr_lock_chains++;
2248 chain->chain_key = chain_key;
2249 chain->irq_context = irq_context;
2250 chain->depth = 2;
2251 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2252 chain->base = nr_chain_hlocks;
2253 nr_chain_hlocks += chain->depth;
2254 chain_hlocks[chain->base] = prev - 1;
2255 chain_hlocks[chain->base + 1] = next -1;
2256 }
2257 #ifdef CONFIG_DEBUG_LOCKDEP
2258 /*
2259 * Important for check_no_collision().
2260 */
2261 else {
2262 if (!debug_locks_off_graph_unlock())
2263 return 0;
2264
2265 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2266 dump_stack();
2267 return 0;
2268 }
2269 #endif
2270
2271 hlist_add_head_rcu(&chain->entry, hash_head);
2272 debug_atomic_inc(chain_lookup_misses);
2273 inc_chains();
2274
2275 return 1;
2276 }
2277
2278 /*
2279 * Adds a dependency chain into chain hashtable. And must be called with
2280 * graph_lock held.
2281 *
2282 * Return 0 if fail, and graph_lock is released.
2283 * Return 1 if succeed, with graph_lock held.
2284 */
2285 static inline int add_chain_cache(struct task_struct *curr,
2286 struct held_lock *hlock,
2287 u64 chain_key)
2288 {
2289 struct lock_class *class = hlock_class(hlock);
2290 struct hlist_head *hash_head = chainhashentry(chain_key);
2291 struct lock_chain *chain;
2292 int i, j;
2293
2294 /*
2295 * Allocate a new chain entry from the static array, and add
2296 * it to the hash:
2297 */
2298
2299 /*
2300 * We might need to take the graph lock, ensure we've got IRQs
2301 * disabled to make this an IRQ-safe lock.. for recursion reasons
2302 * lockdep won't complain about its own locking errors.
2303 */
2304 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2305 return 0;
2306
2307 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2308 if (!debug_locks_off_graph_unlock())
2309 return 0;
2310
2311 print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!");
2312 dump_stack();
2313 return 0;
2314 }
2315 chain = lock_chains + nr_lock_chains++;
2316 chain->chain_key = chain_key;
2317 chain->irq_context = hlock->irq_context;
2318 i = get_first_held_lock(curr, hlock);
2319 chain->depth = curr->lockdep_depth + 1 - i;
2320
2321 BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));
2322 BUILD_BUG_ON((1UL << 6) <= ARRAY_SIZE(curr->held_locks));
2323 BUILD_BUG_ON((1UL << 8*sizeof(chain_hlocks[0])) <= ARRAY_SIZE(lock_classes));
2324
2325 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2326 chain->base = nr_chain_hlocks;
2327 for (j = 0; j < chain->depth - 1; j++, i++) {
2328 int lock_id = curr->held_locks[i].class_idx - 1;
2329 chain_hlocks[chain->base + j] = lock_id;
2330 }
2331 chain_hlocks[chain->base + j] = class - lock_classes;
2332 }
2333
2334 if (nr_chain_hlocks < MAX_LOCKDEP_CHAIN_HLOCKS)
2335 nr_chain_hlocks += chain->depth;
2336
2337 #ifdef CONFIG_DEBUG_LOCKDEP
2338 /*
2339 * Important for check_no_collision().
2340 */
2341 if (unlikely(nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)) {
2342 if (!debug_locks_off_graph_unlock())
2343 return 0;
2344
2345 print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!");
2346 dump_stack();
2347 return 0;
2348 }
2349 #endif
2350
2351 hlist_add_head_rcu(&chain->entry, hash_head);
2352 debug_atomic_inc(chain_lookup_misses);
2353 inc_chains();
2354
2355 return 1;
2356 }
2357
2358 /*
2359 * Look up a dependency chain.
2360 */
2361 static inline struct lock_chain *lookup_chain_cache(u64 chain_key)
2362 {
2363 struct hlist_head *hash_head = chainhashentry(chain_key);
2364 struct lock_chain *chain;
2365
2366 /*
2367 * We can walk it lock-free, because entries only get added
2368 * to the hash:
2369 */
2370 hlist_for_each_entry_rcu(chain, hash_head, entry) {
2371 if (chain->chain_key == chain_key) {
2372 debug_atomic_inc(chain_lookup_hits);
2373 return chain;
2374 }
2375 }
2376 return NULL;
2377 }
2378
2379 /*
2380 * If the key is not present yet in dependency chain cache then
2381 * add it and return 1 - in this case the new dependency chain is
2382 * validated. If the key is already hashed, return 0.
2383 * (On return with 1 graph_lock is held.)
2384 */
2385 static inline int lookup_chain_cache_add(struct task_struct *curr,
2386 struct held_lock *hlock,
2387 u64 chain_key)
2388 {
2389 struct lock_class *class = hlock_class(hlock);
2390 struct lock_chain *chain = lookup_chain_cache(chain_key);
2391
2392 if (chain) {
2393 cache_hit:
2394 if (!check_no_collision(curr, hlock, chain))
2395 return 0;
2396
2397 if (very_verbose(class)) {
2398 printk("\nhash chain already cached, key: "
2399 "%016Lx tail class: [%p] %s\n",
2400 (unsigned long long)chain_key,
2401 class->key, class->name);
2402 }
2403
2404 return 0;
2405 }
2406
2407 if (very_verbose(class)) {
2408 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
2409 (unsigned long long)chain_key, class->key, class->name);
2410 }
2411
2412 if (!graph_lock())
2413 return 0;
2414
2415 /*
2416 * We have to walk the chain again locked - to avoid duplicates:
2417 */
2418 chain = lookup_chain_cache(chain_key);
2419 if (chain) {
2420 graph_unlock();
2421 goto cache_hit;
2422 }
2423
2424 if (!add_chain_cache(curr, hlock, chain_key))
2425 return 0;
2426
2427 return 1;
2428 }
2429
2430 static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
2431 struct held_lock *hlock, int chain_head, u64 chain_key)
2432 {
2433 /*
2434 * Trylock needs to maintain the stack of held locks, but it
2435 * does not add new dependencies, because trylock can be done
2436 * in any order.
2437 *
2438 * We look up the chain_key and do the O(N^2) check and update of
2439 * the dependencies only if this is a new dependency chain.
2440 * (If lookup_chain_cache_add() return with 1 it acquires
2441 * graph_lock for us)
2442 */
2443 if (!hlock->trylock && hlock->check &&
2444 lookup_chain_cache_add(curr, hlock, chain_key)) {
2445 /*
2446 * Check whether last held lock:
2447 *
2448 * - is irq-safe, if this lock is irq-unsafe
2449 * - is softirq-safe, if this lock is hardirq-unsafe
2450 *
2451 * And check whether the new lock's dependency graph
2452 * could lead back to the previous lock.
2453 *
2454 * any of these scenarios could lead to a deadlock. If
2455 * All validations
2456 */
2457 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2458
2459 if (!ret)
2460 return 0;
2461 /*
2462 * Mark recursive read, as we jump over it when
2463 * building dependencies (just like we jump over
2464 * trylock entries):
2465 */
2466 if (ret == 2)
2467 hlock->read = 2;
2468 /*
2469 * Add dependency only if this lock is not the head
2470 * of the chain, and if it's not a secondary read-lock:
2471 */
2472 if (!chain_head && ret != 2) {
2473 if (!check_prevs_add(curr, hlock))
2474 return 0;
2475 }
2476
2477 graph_unlock();
2478 } else {
2479 /* after lookup_chain_cache_add(): */
2480 if (unlikely(!debug_locks))
2481 return 0;
2482 }
2483
2484 return 1;
2485 }
2486 #else
2487 static inline int validate_chain(struct task_struct *curr,
2488 struct lockdep_map *lock, struct held_lock *hlock,
2489 int chain_head, u64 chain_key)
2490 {
2491 return 1;
2492 }
2493 #endif
2494
2495 /*
2496 * We are building curr_chain_key incrementally, so double-check
2497 * it from scratch, to make sure that it's done correctly:
2498 */
2499 static void check_chain_key(struct task_struct *curr)
2500 {
2501 #ifdef CONFIG_DEBUG_LOCKDEP
2502 struct held_lock *hlock, *prev_hlock = NULL;
2503 unsigned int i;
2504 u64 chain_key = 0;
2505
2506 for (i = 0; i < curr->lockdep_depth; i++) {
2507 hlock = curr->held_locks + i;
2508 if (chain_key != hlock->prev_chain_key) {
2509 debug_locks_off();
2510 /*
2511 * We got mighty confused, our chain keys don't match
2512 * with what we expect, someone trample on our task state?
2513 */
2514 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2515 curr->lockdep_depth, i,
2516 (unsigned long long)chain_key,
2517 (unsigned long long)hlock->prev_chain_key);
2518 return;
2519 }
2520 /*
2521 * Whoops ran out of static storage again?
2522 */
2523 if (DEBUG_LOCKS_WARN_ON(hlock->class_idx > MAX_LOCKDEP_KEYS))
2524 return;
2525
2526 if (prev_hlock && (prev_hlock->irq_context !=
2527 hlock->irq_context))
2528 chain_key = 0;
2529 chain_key = iterate_chain_key(chain_key, hlock->class_idx);
2530 prev_hlock = hlock;
2531 }
2532 if (chain_key != curr->curr_chain_key) {
2533 debug_locks_off();
2534 /*
2535 * More smoking hash instead of calculating it, damn see these
2536 * numbers float.. I bet that a pink elephant stepped on my memory.
2537 */
2538 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2539 curr->lockdep_depth, i,
2540 (unsigned long long)chain_key,
2541 (unsigned long long)curr->curr_chain_key);
2542 }
2543 #endif
2544 }
2545
2546 static void
2547 print_usage_bug_scenario(struct held_lock *lock)
2548 {
2549 struct lock_class *class = hlock_class(lock);
2550
2551 printk(" Possible unsafe locking scenario:\n\n");
2552 printk(" CPU0\n");
2553 printk(" ----\n");
2554 printk(" lock(");
2555 __print_lock_name(class);
2556 printk(KERN_CONT ");\n");
2557 printk(" <Interrupt>\n");
2558 printk(" lock(");
2559 __print_lock_name(class);
2560 printk(KERN_CONT ");\n");
2561 printk("\n *** DEADLOCK ***\n\n");
2562 }
2563
2564 static int
2565 print_usage_bug(struct task_struct *curr, struct held_lock *this,
2566 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2567 {
2568 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2569 return 0;
2570
2571 pr_warn("\n");
2572 pr_warn("================================\n");
2573 pr_warn("WARNING: inconsistent lock state\n");
2574 print_kernel_ident();
2575 pr_warn("--------------------------------\n");
2576
2577 pr_warn("inconsistent {%s} -> {%s} usage.\n",
2578 usage_str[prev_bit], usage_str[new_bit]);
2579
2580 pr_warn("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2581 curr->comm, task_pid_nr(curr),
2582 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2583 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2584 trace_hardirqs_enabled(curr),
2585 trace_softirqs_enabled(curr));
2586 print_lock(this);
2587
2588 pr_warn("{%s} state was registered at:\n", usage_str[prev_bit]);
2589 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2590
2591 print_irqtrace_events(curr);
2592 pr_warn("\nother info that might help us debug this:\n");
2593 print_usage_bug_scenario(this);
2594
2595 lockdep_print_held_locks(curr);
2596
2597 pr_warn("\nstack backtrace:\n");
2598 dump_stack();
2599
2600 return 0;
2601 }
2602
2603 /*
2604 * Print out an error if an invalid bit is set:
2605 */
2606 static inline int
2607 valid_state(struct task_struct *curr, struct held_lock *this,
2608 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2609 {
2610 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2611 return print_usage_bug(curr, this, bad_bit, new_bit);
2612 return 1;
2613 }
2614
2615 static int mark_lock(struct task_struct *curr, struct held_lock *this,
2616 enum lock_usage_bit new_bit);
2617
2618 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2619
2620 /*
2621 * print irq inversion bug:
2622 */
2623 static int
2624 print_irq_inversion_bug(struct task_struct *curr,
2625 struct lock_list *root, struct lock_list *other,
2626 struct held_lock *this, int forwards,
2627 const char *irqclass)
2628 {
2629 struct lock_list *entry = other;
2630 struct lock_list *middle = NULL;
2631 int depth;
2632
2633 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2634 return 0;
2635
2636 pr_warn("\n");
2637 pr_warn("========================================================\n");
2638 pr_warn("WARNING: possible irq lock inversion dependency detected\n");
2639 print_kernel_ident();
2640 pr_warn("--------------------------------------------------------\n");
2641 pr_warn("%s/%d just changed the state of lock:\n",
2642 curr->comm, task_pid_nr(curr));
2643 print_lock(this);
2644 if (forwards)
2645 pr_warn("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2646 else
2647 pr_warn("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2648 print_lock_name(other->class);
2649 pr_warn("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2650
2651 pr_warn("\nother info that might help us debug this:\n");
2652
2653 /* Find a middle lock (if one exists) */
2654 depth = get_lock_depth(other);
2655 do {
2656 if (depth == 0 && (entry != root)) {
2657 pr_warn("lockdep:%s bad path found in chain graph\n", __func__);
2658 break;
2659 }
2660 middle = entry;
2661 entry = get_lock_parent(entry);
2662 depth--;
2663 } while (entry && entry != root && (depth >= 0));
2664 if (forwards)
2665 print_irq_lock_scenario(root, other,
2666 middle ? middle->class : root->class, other->class);
2667 else
2668 print_irq_lock_scenario(other, root,
2669 middle ? middle->class : other->class, root->class);
2670
2671 lockdep_print_held_locks(curr);
2672
2673 pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2674 if (!save_trace(&root->trace))
2675 return 0;
2676 print_shortest_lock_dependencies(other, root);
2677
2678 pr_warn("\nstack backtrace:\n");
2679 dump_stack();
2680
2681 return 0;
2682 }
2683
2684 /*
2685 * Prove that in the forwards-direction subgraph starting at <this>
2686 * there is no lock matching <mask>:
2687 */
2688 static int
2689 check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2690 enum lock_usage_bit bit, const char *irqclass)
2691 {
2692 int ret;
2693 struct lock_list root;
2694 struct lock_list *uninitialized_var(target_entry);
2695
2696 root.parent = NULL;
2697 root.class = hlock_class(this);
2698 ret = find_usage_forwards(&root, bit, &target_entry);
2699 if (ret < 0)
2700 return print_bfs_bug(ret);
2701 if (ret == 1)
2702 return ret;
2703
2704 return print_irq_inversion_bug(curr, &root, target_entry,
2705 this, 1, irqclass);
2706 }
2707
2708 /*
2709 * Prove that in the backwards-direction subgraph starting at <this>
2710 * there is no lock matching <mask>:
2711 */
2712 static int
2713 check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2714 enum lock_usage_bit bit, const char *irqclass)
2715 {
2716 int ret;
2717 struct lock_list root;
2718 struct lock_list *uninitialized_var(target_entry);
2719
2720 root.parent = NULL;
2721 root.class = hlock_class(this);
2722 ret = find_usage_backwards(&root, bit, &target_entry);
2723 if (ret < 0)
2724 return print_bfs_bug(ret);
2725 if (ret == 1)
2726 return ret;
2727
2728 return print_irq_inversion_bug(curr, &root, target_entry,
2729 this, 0, irqclass);
2730 }
2731
2732 void print_irqtrace_events(struct task_struct *curr)
2733 {
2734 printk("irq event stamp: %u\n", curr->irq_events);
2735 printk("hardirqs last enabled at (%u): [<%p>] %pS\n",
2736 curr->hardirq_enable_event, (void *)curr->hardirq_enable_ip,
2737 (void *)curr->hardirq_enable_ip);
2738 printk("hardirqs last disabled at (%u): [<%p>] %pS\n",
2739 curr->hardirq_disable_event, (void *)curr->hardirq_disable_ip,
2740 (void *)curr->hardirq_disable_ip);
2741 printk("softirqs last enabled at (%u): [<%p>] %pS\n",
2742 curr->softirq_enable_event, (void *)curr->softirq_enable_ip,
2743 (void *)curr->softirq_enable_ip);
2744 printk("softirqs last disabled at (%u): [<%p>] %pS\n",
2745 curr->softirq_disable_event, (void *)curr->softirq_disable_ip,
2746 (void *)curr->softirq_disable_ip);
2747 }
2748
2749 static int HARDIRQ_verbose(struct lock_class *class)
2750 {
2751 #if HARDIRQ_VERBOSE
2752 return class_filter(class);
2753 #endif
2754 return 0;
2755 }
2756
2757 static int SOFTIRQ_verbose(struct lock_class *class)
2758 {
2759 #if SOFTIRQ_VERBOSE
2760 return class_filter(class);
2761 #endif
2762 return 0;
2763 }
2764
2765 #define STRICT_READ_CHECKS 1
2766
2767 static int (*state_verbose_f[])(struct lock_class *class) = {
2768 #define LOCKDEP_STATE(__STATE) \
2769 __STATE##_verbose,
2770 #include "lockdep_states.h"
2771 #undef LOCKDEP_STATE
2772 };
2773
2774 static inline int state_verbose(enum lock_usage_bit bit,
2775 struct lock_class *class)
2776 {
2777 return state_verbose_f[bit >> 2](class);
2778 }
2779
2780 typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2781 enum lock_usage_bit bit, const char *name);
2782
2783 static int
2784 mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2785 enum lock_usage_bit new_bit)
2786 {
2787 int excl_bit = exclusive_bit(new_bit);
2788 int read = new_bit & 1;
2789 int dir = new_bit & 2;
2790
2791 /*
2792 * mark USED_IN has to look forwards -- to ensure no dependency
2793 * has ENABLED state, which would allow recursion deadlocks.
2794 *
2795 * mark ENABLED has to look backwards -- to ensure no dependee
2796 * has USED_IN state, which, again, would allow recursion deadlocks.
2797 */
2798 check_usage_f usage = dir ?
2799 check_usage_backwards : check_usage_forwards;
2800
2801 /*
2802 * Validate that this particular lock does not have conflicting
2803 * usage states.
2804 */
2805 if (!valid_state(curr, this, new_bit, excl_bit))
2806 return 0;
2807
2808 /*
2809 * Validate that the lock dependencies don't have conflicting usage
2810 * states.
2811 */
2812 if ((!read || !dir || STRICT_READ_CHECKS) &&
2813 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2814 return 0;
2815
2816 /*
2817 * Check for read in write conflicts
2818 */
2819 if (!read) {
2820 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2821 return 0;
2822
2823 if (STRICT_READ_CHECKS &&
2824 !usage(curr, this, excl_bit + 1,
2825 state_name(new_bit + 1)))
2826 return 0;
2827 }
2828
2829 if (state_verbose(new_bit, hlock_class(this)))
2830 return 2;
2831
2832 return 1;
2833 }
2834
2835 enum mark_type {
2836 #define LOCKDEP_STATE(__STATE) __STATE,
2837 #include "lockdep_states.h"
2838 #undef LOCKDEP_STATE
2839 };
2840
2841 /*
2842 * Mark all held locks with a usage bit:
2843 */
2844 static int
2845 mark_held_locks(struct task_struct *curr, enum mark_type mark)
2846 {
2847 enum lock_usage_bit usage_bit;
2848 struct held_lock *hlock;
2849 int i;
2850
2851 for (i = 0; i < curr->lockdep_depth; i++) {
2852 hlock = curr->held_locks + i;
2853
2854 usage_bit = 2 + (mark << 2); /* ENABLED */
2855 if (hlock->read)
2856 usage_bit += 1; /* READ */
2857
2858 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2859
2860 if (!hlock->check)
2861 continue;
2862
2863 if (!mark_lock(curr, hlock, usage_bit))
2864 return 0;
2865 }
2866
2867 return 1;
2868 }
2869
2870 /*
2871 * Hardirqs will be enabled:
2872 */
2873 static void __trace_hardirqs_on_caller(unsigned long ip)
2874 {
2875 struct task_struct *curr = current;
2876
2877 /* we'll do an OFF -> ON transition: */
2878 curr->hardirqs_enabled = 1;
2879
2880 /*
2881 * We are going to turn hardirqs on, so set the
2882 * usage bit for all held locks:
2883 */
2884 if (!mark_held_locks(curr, HARDIRQ))
2885 return;
2886 /*
2887 * If we have softirqs enabled, then set the usage
2888 * bit for all held locks. (disabled hardirqs prevented
2889 * this bit from being set before)
2890 */
2891 if (curr->softirqs_enabled)
2892 if (!mark_held_locks(curr, SOFTIRQ))
2893 return;
2894
2895 curr->hardirq_enable_ip = ip;
2896 curr->hardirq_enable_event = ++curr->irq_events;
2897 debug_atomic_inc(hardirqs_on_events);
2898 }
2899
2900 __visible void trace_hardirqs_on_caller(unsigned long ip)
2901 {
2902 time_hardirqs_on(CALLER_ADDR0, ip);
2903
2904 if (unlikely(!debug_locks || current->lockdep_recursion))
2905 return;
2906
2907 if (unlikely(current->hardirqs_enabled)) {
2908 /*
2909 * Neither irq nor preemption are disabled here
2910 * so this is racy by nature but losing one hit
2911 * in a stat is not a big deal.
2912 */
2913 __debug_atomic_inc(redundant_hardirqs_on);
2914 return;
2915 }
2916
2917 /*
2918 * We're enabling irqs and according to our state above irqs weren't
2919 * already enabled, yet we find the hardware thinks they are in fact
2920 * enabled.. someone messed up their IRQ state tracing.
2921 */
2922 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2923 return;
2924
2925 /*
2926 * See the fine text that goes along with this variable definition.
2927 */
2928 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2929 return;
2930
2931 /*
2932 * Can't allow enabling interrupts while in an interrupt handler,
2933 * that's general bad form and such. Recursion, limited stack etc..
2934 */
2935 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2936 return;
2937
2938 current->lockdep_recursion = 1;
2939 __trace_hardirqs_on_caller(ip);
2940 current->lockdep_recursion = 0;
2941 }
2942 EXPORT_SYMBOL(trace_hardirqs_on_caller);
2943
2944 void trace_hardirqs_on(void)
2945 {
2946 trace_hardirqs_on_caller(CALLER_ADDR0);
2947 }
2948 EXPORT_SYMBOL(trace_hardirqs_on);
2949
2950 /*
2951 * Hardirqs were disabled:
2952 */
2953 __visible void trace_hardirqs_off_caller(unsigned long ip)
2954 {
2955 struct task_struct *curr = current;
2956
2957 time_hardirqs_off(CALLER_ADDR0, ip);
2958
2959 if (unlikely(!debug_locks || current->lockdep_recursion))
2960 return;
2961
2962 /*
2963 * So we're supposed to get called after you mask local IRQs, but for
2964 * some reason the hardware doesn't quite think you did a proper job.
2965 */
2966 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2967 return;
2968
2969 if (curr->hardirqs_enabled) {
2970 /*
2971 * We have done an ON -> OFF transition:
2972 */
2973 curr->hardirqs_enabled = 0;
2974 curr->hardirq_disable_ip = ip;
2975 curr->hardirq_disable_event = ++curr->irq_events;
2976 debug_atomic_inc(hardirqs_off_events);
2977 } else
2978 debug_atomic_inc(redundant_hardirqs_off);
2979 }
2980 EXPORT_SYMBOL(trace_hardirqs_off_caller);
2981
2982 void trace_hardirqs_off(void)
2983 {
2984 trace_hardirqs_off_caller(CALLER_ADDR0);
2985 }
2986 EXPORT_SYMBOL(trace_hardirqs_off);
2987
2988 /*
2989 * Softirqs will be enabled:
2990 */
2991 void trace_softirqs_on(unsigned long ip)
2992 {
2993 struct task_struct *curr = current;
2994
2995 if (unlikely(!debug_locks || current->lockdep_recursion))
2996 return;
2997
2998 /*
2999 * We fancy IRQs being disabled here, see softirq.c, avoids
3000 * funny state and nesting things.
3001 */
3002 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3003 return;
3004
3005 if (curr->softirqs_enabled) {
3006 debug_atomic_inc(redundant_softirqs_on);
3007 return;
3008 }
3009
3010 current->lockdep_recursion = 1;
3011 /*
3012 * We'll do an OFF -> ON transition:
3013 */
3014 curr->softirqs_enabled = 1;
3015 curr->softirq_enable_ip = ip;
3016 curr->softirq_enable_event = ++curr->irq_events;
3017 debug_atomic_inc(softirqs_on_events);
3018 /*
3019 * We are going to turn softirqs on, so set the
3020 * usage bit for all held locks, if hardirqs are
3021 * enabled too:
3022 */
3023 if (curr->hardirqs_enabled)
3024 mark_held_locks(curr, SOFTIRQ);
3025 current->lockdep_recursion = 0;
3026 }
3027
3028 /*
3029 * Softirqs were disabled:
3030 */
3031 void trace_softirqs_off(unsigned long ip)
3032 {
3033 struct task_struct *curr = current;
3034
3035 if (unlikely(!debug_locks || current->lockdep_recursion))
3036 return;
3037
3038 /*
3039 * We fancy IRQs being disabled here, see softirq.c
3040 */
3041 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3042 return;
3043
3044 if (curr->softirqs_enabled) {
3045 /*
3046 * We have done an ON -> OFF transition:
3047 */
3048 curr->softirqs_enabled = 0;
3049 curr->softirq_disable_ip = ip;
3050 curr->softirq_disable_event = ++curr->irq_events;
3051 debug_atomic_inc(softirqs_off_events);
3052 /*
3053 * Whoops, we wanted softirqs off, so why aren't they?
3054 */
3055 DEBUG_LOCKS_WARN_ON(!softirq_count());
3056 } else
3057 debug_atomic_inc(redundant_softirqs_off);
3058 }
3059
3060 static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
3061 {
3062 /*
3063 * If non-trylock use in a hardirq or softirq context, then
3064 * mark the lock as used in these contexts:
3065 */
3066 if (!hlock->trylock) {
3067 if (hlock->read) {
3068 if (curr->hardirq_context)
3069 if (!mark_lock(curr, hlock,
3070 LOCK_USED_IN_HARDIRQ_READ))
3071 return 0;
3072 if (curr->softirq_context)
3073 if (!mark_lock(curr, hlock,
3074 LOCK_USED_IN_SOFTIRQ_READ))
3075 return 0;
3076 } else {
3077 if (curr->hardirq_context)
3078 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
3079 return 0;
3080 if (curr->softirq_context)
3081 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
3082 return 0;
3083 }
3084 }
3085 if (!hlock->hardirqs_off) {
3086 if (hlock->read) {
3087 if (!mark_lock(curr, hlock,
3088 LOCK_ENABLED_HARDIRQ_READ))
3089 return 0;
3090 if (curr->softirqs_enabled)
3091 if (!mark_lock(curr, hlock,
3092 LOCK_ENABLED_SOFTIRQ_READ))
3093 return 0;
3094 } else {
3095 if (!mark_lock(curr, hlock,
3096 LOCK_ENABLED_HARDIRQ))
3097 return 0;
3098 if (curr->softirqs_enabled)
3099 if (!mark_lock(curr, hlock,
3100 LOCK_ENABLED_SOFTIRQ))
3101 return 0;
3102 }
3103 }
3104
3105 return 1;
3106 }
3107
3108 static inline unsigned int task_irq_context(struct task_struct *task)
3109 {
3110 return 2 * !!task->hardirq_context + !!task->softirq_context;
3111 }
3112
3113 static int separate_irq_context(struct task_struct *curr,
3114 struct held_lock *hlock)
3115 {
3116 unsigned int depth = curr->lockdep_depth;
3117
3118 /*
3119 * Keep track of points where we cross into an interrupt context:
3120 */
3121 if (depth) {
3122 struct held_lock *prev_hlock;
3123
3124 prev_hlock = curr->held_locks + depth-1;
3125 /*
3126 * If we cross into another context, reset the
3127 * hash key (this also prevents the checking and the
3128 * adding of the dependency to 'prev'):
3129 */
3130 if (prev_hlock->irq_context != hlock->irq_context)
3131 return 1;
3132 }
3133 return 0;
3134 }
3135
3136 #else /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3137
3138 static inline
3139 int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
3140 enum lock_usage_bit new_bit)
3141 {
3142 WARN_ON(1); /* Impossible innit? when we don't have TRACE_IRQFLAG */
3143 return 1;
3144 }
3145
3146 static inline int mark_irqflags(struct task_struct *curr,
3147 struct held_lock *hlock)
3148 {
3149 return 1;
3150 }
3151
3152 static inline unsigned int task_irq_context(struct task_struct *task)
3153 {
3154 return 0;
3155 }
3156
3157 static inline int separate_irq_context(struct task_struct *curr,
3158 struct held_lock *hlock)
3159 {
3160 return 0;
3161 }
3162
3163 #endif /* defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING) */
3164
3165 /*
3166 * Mark a lock with a usage bit, and validate the state transition:
3167 */
3168 static int mark_lock(struct task_struct *curr, struct held_lock *this,
3169 enum lock_usage_bit new_bit)
3170 {
3171 unsigned int new_mask = 1 << new_bit, ret = 1;
3172
3173 /*
3174 * If already set then do not dirty the cacheline,
3175 * nor do any checks:
3176 */
3177 if (likely(hlock_class(this)->usage_mask & new_mask))
3178 return 1;
3179
3180 if (!graph_lock())
3181 return 0;
3182 /*
3183 * Make sure we didn't race:
3184 */
3185 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
3186 graph_unlock();
3187 return 1;
3188 }
3189
3190 hlock_class(this)->usage_mask |= new_mask;
3191
3192 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
3193 return 0;
3194
3195 switch (new_bit) {
3196 #define LOCKDEP_STATE(__STATE) \
3197 case LOCK_USED_IN_##__STATE: \
3198 case LOCK_USED_IN_##__STATE##_READ: \
3199 case LOCK_ENABLED_##__STATE: \
3200 case LOCK_ENABLED_##__STATE##_READ:
3201 #include "lockdep_states.h"
3202 #undef LOCKDEP_STATE
3203 ret = mark_lock_irq(curr, this, new_bit);
3204 if (!ret)
3205 return 0;
3206 break;
3207 case LOCK_USED:
3208 debug_atomic_dec(nr_unused_locks);
3209 break;
3210 default:
3211 if (!debug_locks_off_graph_unlock())
3212 return 0;
3213 WARN_ON(1);
3214 return 0;
3215 }
3216
3217 graph_unlock();
3218
3219 /*
3220 * We must printk outside of the graph_lock:
3221 */
3222 if (ret == 2) {
3223 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
3224 print_lock(this);
3225 print_irqtrace_events(curr);
3226 dump_stack();
3227 }
3228
3229 return ret;
3230 }
3231
3232 /*
3233 * Initialize a lock instance's lock-class mapping info:
3234 */
3235 static void __lockdep_init_map(struct lockdep_map *lock, const char *name,
3236 struct lock_class_key *key, int subclass)
3237 {
3238 int i;
3239
3240 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
3241 lock->class_cache[i] = NULL;
3242
3243 #ifdef CONFIG_LOCK_STAT
3244 lock->cpu = raw_smp_processor_id();
3245 #endif
3246
3247 /*
3248 * Can't be having no nameless bastards around this place!
3249 */
3250 if (DEBUG_LOCKS_WARN_ON(!name)) {
3251 lock->name = "NULL";
3252 return;
3253 }
3254
3255 lock->name = name;
3256
3257 /*
3258 * No key, no joy, we need to hash something.
3259 */
3260 if (DEBUG_LOCKS_WARN_ON(!key))
3261 return;
3262 /*
3263 * Sanity check, the lock-class key must be persistent:
3264 */
3265 if (!static_obj(key)) {
3266 printk("BUG: key %p not in .data!\n", key);
3267 /*
3268 * What it says above ^^^^^, I suggest you read it.
3269 */
3270 DEBUG_LOCKS_WARN_ON(1);
3271 return;
3272 }
3273 lock->key = key;
3274
3275 if (unlikely(!debug_locks))
3276 return;
3277
3278 if (subclass) {
3279 unsigned long flags;
3280
3281 if (DEBUG_LOCKS_WARN_ON(current->lockdep_recursion))
3282 return;
3283
3284 raw_local_irq_save(flags);
3285 current->lockdep_recursion = 1;
3286 register_lock_class(lock, subclass, 1);
3287 current->lockdep_recursion = 0;
3288 raw_local_irq_restore(flags);
3289 }
3290 }
3291
3292 void lockdep_init_map(struct lockdep_map *lock, const char *name,
3293 struct lock_class_key *key, int subclass)
3294 {
3295 cross_init(lock, 0);
3296 __lockdep_init_map(lock, name, key, subclass);
3297 }
3298 EXPORT_SYMBOL_GPL(lockdep_init_map);
3299
3300 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
3301 void lockdep_init_map_crosslock(struct lockdep_map *lock, const char *name,
3302 struct lock_class_key *key, int subclass)
3303 {
3304 cross_init(lock, 1);
3305 __lockdep_init_map(lock, name, key, subclass);
3306 }
3307 EXPORT_SYMBOL_GPL(lockdep_init_map_crosslock);
3308 #endif
3309
3310 struct lock_class_key __lockdep_no_validate__;
3311 EXPORT_SYMBOL_GPL(__lockdep_no_validate__);
3312
3313 static int
3314 print_lock_nested_lock_not_held(struct task_struct *curr,
3315 struct held_lock *hlock,
3316 unsigned long ip)
3317 {
3318 if (!debug_locks_off())
3319 return 0;
3320 if (debug_locks_silent)
3321 return 0;
3322
3323 pr_warn("\n");
3324 pr_warn("==================================\n");
3325 pr_warn("WARNING: Nested lock was not taken\n");
3326 print_kernel_ident();
3327 pr_warn("----------------------------------\n");
3328
3329 pr_warn("%s/%d is trying to lock:\n", curr->comm, task_pid_nr(curr));
3330 print_lock(hlock);
3331
3332 pr_warn("\nbut this task is not holding:\n");
3333 pr_warn("%s\n", hlock->nest_lock->name);
3334
3335 pr_warn("\nstack backtrace:\n");
3336 dump_stack();
3337
3338 pr_warn("\nother info that might help us debug this:\n");
3339 lockdep_print_held_locks(curr);
3340
3341 pr_warn("\nstack backtrace:\n");
3342 dump_stack();
3343
3344 return 0;
3345 }
3346
3347 static int __lock_is_held(struct lockdep_map *lock, int read);
3348
3349 /*
3350 * This gets called for every mutex_lock*()/spin_lock*() operation.
3351 * We maintain the dependency maps and validate the locking attempt:
3352 */
3353 static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3354 int trylock, int read, int check, int hardirqs_off,
3355 struct lockdep_map *nest_lock, unsigned long ip,
3356 int references, int pin_count)
3357 {
3358 struct task_struct *curr = current;
3359 struct lock_class *class = NULL;
3360 struct held_lock *hlock;
3361 unsigned int depth;
3362 int chain_head = 0;
3363 int class_idx;
3364 u64 chain_key;
3365 int ret;
3366
3367 if (unlikely(!debug_locks))
3368 return 0;
3369
3370 /*
3371 * Lockdep should run with IRQs disabled, otherwise we could
3372 * get an interrupt which would want to take locks, which would
3373 * end up in lockdep and have you got a head-ache already?
3374 */
3375 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3376 return 0;
3377
3378 if (!prove_locking || lock->key == &__lockdep_no_validate__)
3379 check = 0;
3380
3381 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
3382 class = lock->class_cache[subclass];
3383 /*
3384 * Not cached?
3385 */
3386 if (unlikely(!class)) {
3387 class = register_lock_class(lock, subclass, 0);
3388 if (!class)
3389 return 0;
3390 }
3391 atomic_inc((atomic_t *)&class->ops);
3392 if (very_verbose(class)) {
3393 printk("\nacquire class [%p] %s", class->key, class->name);
3394 if (class->name_version > 1)
3395 printk(KERN_CONT "#%d", class->name_version);
3396 printk(KERN_CONT "\n");
3397 dump_stack();
3398 }
3399
3400 /*
3401 * Add the lock to the list of currently held locks.
3402 * (we dont increase the depth just yet, up until the
3403 * dependency checks are done)
3404 */
3405 depth = curr->lockdep_depth;
3406 /*
3407 * Ran out of static storage for our per-task lock stack again have we?
3408 */
3409 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
3410 return 0;
3411
3412 class_idx = class - lock_classes + 1;
3413
3414 /* TODO: nest_lock is not implemented for crosslock yet. */
3415 if (depth && !cross_lock(lock)) {
3416 hlock = curr->held_locks + depth - 1;
3417 if (hlock->class_idx == class_idx && nest_lock) {
3418 if (hlock->references) {
3419 /*
3420 * Check: unsigned int references:12, overflow.
3421 */
3422 if (DEBUG_LOCKS_WARN_ON(hlock->references == (1 << 12)-1))
3423 return 0;
3424
3425 hlock->references++;
3426 } else {
3427 hlock->references = 2;
3428 }
3429
3430 return 1;
3431 }
3432 }
3433
3434 hlock = curr->held_locks + depth;
3435 /*
3436 * Plain impossible, we just registered it and checked it weren't no
3437 * NULL like.. I bet this mushroom I ate was good!
3438 */
3439 if (DEBUG_LOCKS_WARN_ON(!class))
3440 return 0;
3441 hlock->class_idx = class_idx;
3442 hlock->acquire_ip = ip;
3443 hlock->instance = lock;
3444 hlock->nest_lock = nest_lock;
3445 hlock->irq_context = task_irq_context(curr);
3446 hlock->trylock = trylock;
3447 hlock->read = read;
3448 hlock->check = check;
3449 hlock->hardirqs_off = !!hardirqs_off;
3450 hlock->references = references;
3451 #ifdef CONFIG_LOCK_STAT
3452 hlock->waittime_stamp = 0;
3453 hlock->holdtime_stamp = lockstat_clock();
3454 #endif
3455 hlock->pin_count = pin_count;
3456
3457 if (check && !mark_irqflags(curr, hlock))
3458 return 0;
3459
3460 /* mark it as used: */
3461 if (!mark_lock(curr, hlock, LOCK_USED))
3462 return 0;
3463
3464 /*
3465 * Calculate the chain hash: it's the combined hash of all the
3466 * lock keys along the dependency chain. We save the hash value
3467 * at every step so that we can get the current hash easily
3468 * after unlock. The chain hash is then used to cache dependency
3469 * results.
3470 *
3471 * The 'key ID' is what is the most compact key value to drive
3472 * the hash, not class->key.
3473 */
3474 /*
3475 * Whoops, we did it again.. ran straight out of our static allocation.
3476 */
3477 if (DEBUG_LOCKS_WARN_ON(class_idx > MAX_LOCKDEP_KEYS))
3478 return 0;
3479
3480 chain_key = curr->curr_chain_key;
3481 if (!depth) {
3482 /*
3483 * How can we have a chain hash when we ain't got no keys?!
3484 */
3485 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3486 return 0;
3487 chain_head = 1;
3488 }
3489
3490 hlock->prev_chain_key = chain_key;
3491 if (separate_irq_context(curr, hlock)) {
3492 chain_key = 0;
3493 chain_head = 1;
3494 }
3495 chain_key = iterate_chain_key(chain_key, class_idx);
3496
3497 if (nest_lock && !__lock_is_held(nest_lock, -1))
3498 return print_lock_nested_lock_not_held(curr, hlock, ip);
3499
3500 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
3501 return 0;
3502
3503 ret = lock_acquire_crosslock(hlock);
3504 /*
3505 * 2 means normal acquire operations are needed. Otherwise, it's
3506 * ok just to return with '0:fail, 1:success'.
3507 */
3508 if (ret != 2)
3509 return ret;
3510
3511 curr->curr_chain_key = chain_key;
3512 curr->lockdep_depth++;
3513 check_chain_key(curr);
3514 #ifdef CONFIG_DEBUG_LOCKDEP
3515 if (unlikely(!debug_locks))
3516 return 0;
3517 #endif
3518 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3519 debug_locks_off();
3520 print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!");
3521 printk(KERN_DEBUG "depth: %i max: %lu!\n",
3522 curr->lockdep_depth, MAX_LOCK_DEPTH);
3523
3524 lockdep_print_held_locks(current);
3525 debug_show_all_locks();
3526 dump_stack();
3527
3528 return 0;
3529 }
3530
3531 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3532 max_lockdep_depth = curr->lockdep_depth;
3533
3534 return 1;
3535 }
3536
3537 static int
3538 print_unlock_imbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3539 unsigned long ip)
3540 {
3541 if (!debug_locks_off())
3542 return 0;
3543 if (debug_locks_silent)
3544 return 0;
3545
3546 pr_warn("\n");
3547 pr_warn("=====================================\n");
3548 pr_warn("WARNING: bad unlock balance detected!\n");
3549 print_kernel_ident();
3550 pr_warn("-------------------------------------\n");
3551 pr_warn("%s/%d is trying to release lock (",
3552 curr->comm, task_pid_nr(curr));
3553 print_lockdep_cache(lock);
3554 pr_cont(") at:\n");
3555 print_ip_sym(ip);
3556 pr_warn("but there are no more locks to release!\n");
3557 pr_warn("\nother info that might help us debug this:\n");
3558 lockdep_print_held_locks(curr);
3559
3560 pr_warn("\nstack backtrace:\n");
3561 dump_stack();
3562
3563 return 0;
3564 }
3565
3566 static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3567 {
3568 if (hlock->instance == lock)
3569 return 1;
3570
3571 if (hlock->references) {
3572 struct lock_class *class = lock->class_cache[0];
3573
3574 if (!class)
3575 class = look_up_lock_class(lock, 0);
3576
3577 /*
3578 * If look_up_lock_class() failed to find a class, we're trying
3579 * to test if we hold a lock that has never yet been acquired.
3580 * Clearly if the lock hasn't been acquired _ever_, we're not
3581 * holding it either, so report failure.
3582 */
3583 if (IS_ERR_OR_NULL(class))
3584 return 0;
3585
3586 /*
3587 * References, but not a lock we're actually ref-counting?
3588 * State got messed up, follow the sites that change ->references
3589 * and try to make sense of it.
3590 */
3591 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3592 return 0;
3593
3594 if (hlock->class_idx == class - lock_classes + 1)
3595 return 1;
3596 }
3597
3598 return 0;
3599 }
3600
3601 /* @depth must not be zero */
3602 static struct held_lock *find_held_lock(struct task_struct *curr,
3603 struct lockdep_map *lock,
3604 unsigned int depth, int *idx)
3605 {
3606 struct held_lock *ret, *hlock, *prev_hlock;
3607 int i;
3608
3609 i = depth - 1;
3610 hlock = curr->held_locks + i;
3611 ret = hlock;
3612 if (match_held_lock(hlock, lock))
3613 goto out;
3614
3615 ret = NULL;
3616 for (i--, prev_hlock = hlock--;
3617 i >= 0;
3618 i--, prev_hlock = hlock--) {
3619 /*
3620 * We must not cross into another context:
3621 */
3622 if (prev_hlock->irq_context != hlock->irq_context) {
3623 ret = NULL;
3624 break;
3625 }
3626 if (match_held_lock(hlock, lock)) {
3627 ret = hlock;
3628 break;
3629 }
3630 }
3631
3632 out:
3633 *idx = i;
3634 return ret;
3635 }
3636
3637 static int reacquire_held_locks(struct task_struct *curr, unsigned int depth,
3638 int idx)
3639 {
3640 struct held_lock *hlock;
3641
3642 for (hlock = curr->held_locks + idx; idx < depth; idx++, hlock++) {
3643 if (!__lock_acquire(hlock->instance,
3644 hlock_class(hlock)->subclass,
3645 hlock->trylock,
3646 hlock->read, hlock->check,
3647 hlock->hardirqs_off,
3648 hlock->nest_lock, hlock->acquire_ip,
3649 hlock->references, hlock->pin_count))
3650 return 1;
3651 }
3652 return 0;
3653 }
3654
3655 static int
3656 __lock_set_class(struct lockdep_map *lock, const char *name,
3657 struct lock_class_key *key, unsigned int subclass,
3658 unsigned long ip)
3659 {
3660 struct task_struct *curr = current;
3661 struct held_lock *hlock;
3662 struct lock_class *class;
3663 unsigned int depth;
3664 int i;
3665
3666 depth = curr->lockdep_depth;
3667 /*
3668 * This function is about (re)setting the class of a held lock,
3669 * yet we're not actually holding any locks. Naughty user!
3670 */
3671 if (DEBUG_LOCKS_WARN_ON(!depth))
3672 return 0;
3673
3674 hlock = find_held_lock(curr, lock, depth, &i);
3675 if (!hlock)
3676 return print_unlock_imbalance_bug(curr, lock, ip);
3677
3678 lockdep_init_map(lock, name, key, 0);
3679 class = register_lock_class(lock, subclass, 0);
3680 hlock->class_idx = class - lock_classes + 1;
3681
3682 curr->lockdep_depth = i;
3683 curr->curr_chain_key = hlock->prev_chain_key;
3684
3685 if (reacquire_held_locks(curr, depth, i))
3686 return 0;
3687
3688 /*
3689 * I took it apart and put it back together again, except now I have
3690 * these 'spare' parts.. where shall I put them.
3691 */
3692 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3693 return 0;
3694 return 1;
3695 }
3696
3697 static int __lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3698 {
3699 struct task_struct *curr = current;
3700 struct held_lock *hlock;
3701 unsigned int depth;
3702 int i;
3703
3704 depth = curr->lockdep_depth;
3705 /*
3706 * This function is about (re)setting the class of a held lock,
3707 * yet we're not actually holding any locks. Naughty user!
3708 */
3709 if (DEBUG_LOCKS_WARN_ON(!depth))
3710 return 0;
3711
3712 hlock = find_held_lock(curr, lock, depth, &i);
3713 if (!hlock)
3714 return print_unlock_imbalance_bug(curr, lock, ip);
3715
3716 curr->lockdep_depth = i;
3717 curr->curr_chain_key = hlock->prev_chain_key;
3718
3719 WARN(hlock->read, "downgrading a read lock");
3720 hlock->read = 1;
3721 hlock->acquire_ip = ip;
3722
3723 if (reacquire_held_locks(curr, depth, i))
3724 return 0;
3725
3726 /*
3727 * I took it apart and put it back together again, except now I have
3728 * these 'spare' parts.. where shall I put them.
3729 */
3730 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3731 return 0;
3732 return 1;
3733 }
3734
3735 /*
3736 * Remove the lock to the list of currently held locks - this gets
3737 * called on mutex_unlock()/spin_unlock*() (or on a failed
3738 * mutex_lock_interruptible()).
3739 *
3740 * @nested is an hysterical artifact, needs a tree wide cleanup.
3741 */
3742 static int
3743 __lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3744 {
3745 struct task_struct *curr = current;
3746 struct held_lock *hlock;
3747 unsigned int depth;
3748 int ret, i;
3749
3750 if (unlikely(!debug_locks))
3751 return 0;
3752
3753 ret = lock_release_crosslock(lock);
3754 /*
3755 * 2 means normal release operations are needed. Otherwise, it's
3756 * ok just to return with '0:fail, 1:success'.
3757 */
3758 if (ret != 2)
3759 return ret;
3760
3761 depth = curr->lockdep_depth;
3762 /*
3763 * So we're all set to release this lock.. wait what lock? We don't
3764 * own any locks, you've been drinking again?
3765 */
3766 if (DEBUG_LOCKS_WARN_ON(depth <= 0))
3767 return print_unlock_imbalance_bug(curr, lock, ip);
3768
3769 /*
3770 * Check whether the lock exists in the current stack
3771 * of held locks:
3772 */
3773 hlock = find_held_lock(curr, lock, depth, &i);
3774 if (!hlock)
3775 return print_unlock_imbalance_bug(curr, lock, ip);
3776
3777 if (hlock->instance == lock)
3778 lock_release_holdtime(hlock);
3779
3780 WARN(hlock->pin_count, "releasing a pinned lock\n");
3781
3782 if (hlock->references) {
3783 hlock->references--;
3784 if (hlock->references) {
3785 /*
3786 * We had, and after removing one, still have
3787 * references, the current lock stack is still
3788 * valid. We're done!
3789 */
3790 return 1;
3791 }
3792 }
3793
3794 /*
3795 * We have the right lock to unlock, 'hlock' points to it.
3796 * Now we remove it from the stack, and add back the other
3797 * entries (if any), recalculating the hash along the way:
3798 */
3799
3800 curr->lockdep_depth = i;
3801 curr->curr_chain_key = hlock->prev_chain_key;
3802
3803 if (reacquire_held_locks(curr, depth, i + 1))
3804 return 0;
3805
3806 /*
3807 * We had N bottles of beer on the wall, we drank one, but now
3808 * there's not N-1 bottles of beer left on the wall...
3809 */
3810 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3811 return 0;
3812
3813 return 1;
3814 }
3815
3816 static int __lock_is_held(struct lockdep_map *lock, int read)
3817 {
3818 struct task_struct *curr = current;
3819 int i;
3820
3821 for (i = 0; i < curr->lockdep_depth; i++) {
3822 struct held_lock *hlock = curr->held_locks + i;
3823
3824 if (match_held_lock(hlock, lock)) {
3825 if (read == -1 || hlock->read == read)
3826 return 1;
3827
3828 return 0;
3829 }
3830 }
3831
3832 return 0;
3833 }
3834
3835 static struct pin_cookie __lock_pin_lock(struct lockdep_map *lock)
3836 {
3837 struct pin_cookie cookie = NIL_COOKIE;
3838 struct task_struct *curr = current;
3839 int i;
3840
3841 if (unlikely(!debug_locks))
3842 return cookie;
3843
3844 for (i = 0; i < curr->lockdep_depth; i++) {
3845 struct held_lock *hlock = curr->held_locks + i;
3846
3847 if (match_held_lock(hlock, lock)) {
3848 /*
3849 * Grab 16bits of randomness; this is sufficient to not
3850 * be guessable and still allows some pin nesting in
3851 * our u32 pin_count.
3852 */
3853 cookie.val = 1 + (prandom_u32() >> 16);
3854 hlock->pin_count += cookie.val;
3855 return cookie;
3856 }
3857 }
3858
3859 WARN(1, "pinning an unheld lock\n");
3860 return cookie;
3861 }
3862
3863 static void __lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3864 {
3865 struct task_struct *curr = current;
3866 int i;
3867
3868 if (unlikely(!debug_locks))
3869 return;
3870
3871 for (i = 0; i < curr->lockdep_depth; i++) {
3872 struct held_lock *hlock = curr->held_locks + i;
3873
3874 if (match_held_lock(hlock, lock)) {
3875 hlock->pin_count += cookie.val;
3876 return;
3877 }
3878 }
3879
3880 WARN(1, "pinning an unheld lock\n");
3881 }
3882
3883 static void __lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
3884 {
3885 struct task_struct *curr = current;
3886 int i;
3887
3888 if (unlikely(!debug_locks))
3889 return;
3890
3891 for (i = 0; i < curr->lockdep_depth; i++) {
3892 struct held_lock *hlock = curr->held_locks + i;
3893
3894 if (match_held_lock(hlock, lock)) {
3895 if (WARN(!hlock->pin_count, "unpinning an unpinned lock\n"))
3896 return;
3897
3898 hlock->pin_count -= cookie.val;
3899
3900 if (WARN((int)hlock->pin_count < 0, "pin count corrupted\n"))
3901 hlock->pin_count = 0;
3902
3903 return;
3904 }
3905 }
3906
3907 WARN(1, "unpinning an unheld lock\n");
3908 }
3909
3910 /*
3911 * Check whether we follow the irq-flags state precisely:
3912 */
3913 static void check_flags(unsigned long flags)
3914 {
3915 #if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3916 defined(CONFIG_TRACE_IRQFLAGS)
3917 if (!debug_locks)
3918 return;
3919
3920 if (irqs_disabled_flags(flags)) {
3921 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3922 printk("possible reason: unannotated irqs-off.\n");
3923 }
3924 } else {
3925 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3926 printk("possible reason: unannotated irqs-on.\n");
3927 }
3928 }
3929
3930 /*
3931 * We dont accurately track softirq state in e.g.
3932 * hardirq contexts (such as on 4KSTACKS), so only
3933 * check if not in hardirq contexts:
3934 */
3935 if (!hardirq_count()) {
3936 if (softirq_count()) {
3937 /* like the above, but with softirqs */
3938 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3939 } else {
3940 /* lick the above, does it taste good? */
3941 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3942 }
3943 }
3944
3945 if (!debug_locks)
3946 print_irqtrace_events(current);
3947 #endif
3948 }
3949
3950 void lock_set_class(struct lockdep_map *lock, const char *name,
3951 struct lock_class_key *key, unsigned int subclass,
3952 unsigned long ip)
3953 {
3954 unsigned long flags;
3955
3956 if (unlikely(current->lockdep_recursion))
3957 return;
3958
3959 raw_local_irq_save(flags);
3960 current->lockdep_recursion = 1;
3961 check_flags(flags);
3962 if (__lock_set_class(lock, name, key, subclass, ip))
3963 check_chain_key(current);
3964 current->lockdep_recursion = 0;
3965 raw_local_irq_restore(flags);
3966 }
3967 EXPORT_SYMBOL_GPL(lock_set_class);
3968
3969 void lock_downgrade(struct lockdep_map *lock, unsigned long ip)
3970 {
3971 unsigned long flags;
3972
3973 if (unlikely(current->lockdep_recursion))
3974 return;
3975
3976 raw_local_irq_save(flags);
3977 current->lockdep_recursion = 1;
3978 check_flags(flags);
3979 if (__lock_downgrade(lock, ip))
3980 check_chain_key(current);
3981 current->lockdep_recursion = 0;
3982 raw_local_irq_restore(flags);
3983 }
3984 EXPORT_SYMBOL_GPL(lock_downgrade);
3985
3986 /*
3987 * We are not always called with irqs disabled - do that here,
3988 * and also avoid lockdep recursion:
3989 */
3990 void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3991 int trylock, int read, int check,
3992 struct lockdep_map *nest_lock, unsigned long ip)
3993 {
3994 unsigned long flags;
3995
3996 if (unlikely(current->lockdep_recursion))
3997 return;
3998
3999 raw_local_irq_save(flags);
4000 check_flags(flags);
4001
4002 current->lockdep_recursion = 1;
4003 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
4004 __lock_acquire(lock, subclass, trylock, read, check,
4005 irqs_disabled_flags(flags), nest_lock, ip, 0, 0);
4006 current->lockdep_recursion = 0;
4007 raw_local_irq_restore(flags);
4008 }
4009 EXPORT_SYMBOL_GPL(lock_acquire);
4010
4011 void lock_release(struct lockdep_map *lock, int nested,
4012 unsigned long ip)
4013 {
4014 unsigned long flags;
4015
4016 if (unlikely(current->lockdep_recursion))
4017 return;
4018
4019 raw_local_irq_save(flags);
4020 check_flags(flags);
4021 current->lockdep_recursion = 1;
4022 trace_lock_release(lock, ip);
4023 if (__lock_release(lock, nested, ip))
4024 check_chain_key(current);
4025 current->lockdep_recursion = 0;
4026 raw_local_irq_restore(flags);
4027 }
4028 EXPORT_SYMBOL_GPL(lock_release);
4029
4030 int lock_is_held_type(struct lockdep_map *lock, int read)
4031 {
4032 unsigned long flags;
4033 int ret = 0;
4034
4035 if (unlikely(current->lockdep_recursion))
4036 return 1; /* avoid false negative lockdep_assert_held() */
4037
4038 raw_local_irq_save(flags);
4039 check_flags(flags);
4040
4041 current->lockdep_recursion = 1;
4042 ret = __lock_is_held(lock, read);
4043 current->lockdep_recursion = 0;
4044 raw_local_irq_restore(flags);
4045
4046 return ret;
4047 }
4048 EXPORT_SYMBOL_GPL(lock_is_held_type);
4049
4050 struct pin_cookie lock_pin_lock(struct lockdep_map *lock)
4051 {
4052 struct pin_cookie cookie = NIL_COOKIE;
4053 unsigned long flags;
4054
4055 if (unlikely(current->lockdep_recursion))
4056 return cookie;
4057
4058 raw_local_irq_save(flags);
4059 check_flags(flags);
4060
4061 current->lockdep_recursion = 1;
4062 cookie = __lock_pin_lock(lock);
4063 current->lockdep_recursion = 0;
4064 raw_local_irq_restore(flags);
4065
4066 return cookie;
4067 }
4068 EXPORT_SYMBOL_GPL(lock_pin_lock);
4069
4070 void lock_repin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
4071 {
4072 unsigned long flags;
4073
4074 if (unlikely(current->lockdep_recursion))
4075 return;
4076
4077 raw_local_irq_save(flags);
4078 check_flags(flags);
4079
4080 current->lockdep_recursion = 1;
4081 __lock_repin_lock(lock, cookie);
4082 current->lockdep_recursion = 0;
4083 raw_local_irq_restore(flags);
4084 }
4085 EXPORT_SYMBOL_GPL(lock_repin_lock);
4086
4087 void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie cookie)
4088 {
4089 unsigned long flags;
4090
4091 if (unlikely(current->lockdep_recursion))
4092 return;
4093
4094 raw_local_irq_save(flags);
4095 check_flags(flags);
4096
4097 current->lockdep_recursion = 1;
4098 __lock_unpin_lock(lock, cookie);
4099 current->lockdep_recursion = 0;
4100 raw_local_irq_restore(flags);
4101 }
4102 EXPORT_SYMBOL_GPL(lock_unpin_lock);
4103
4104 #ifdef CONFIG_LOCK_STAT
4105 static int
4106 print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
4107 unsigned long ip)
4108 {
4109 if (!debug_locks_off())
4110 return 0;
4111 if (debug_locks_silent)
4112 return 0;
4113
4114 pr_warn("\n");
4115 pr_warn("=================================\n");
4116 pr_warn("WARNING: bad contention detected!\n");
4117 print_kernel_ident();
4118 pr_warn("---------------------------------\n");
4119 pr_warn("%s/%d is trying to contend lock (",
4120 curr->comm, task_pid_nr(curr));
4121 print_lockdep_cache(lock);
4122 pr_cont(") at:\n");
4123 print_ip_sym(ip);
4124 pr_warn("but there are no locks held!\n");
4125 pr_warn("\nother info that might help us debug this:\n");
4126 lockdep_print_held_locks(curr);
4127
4128 pr_warn("\nstack backtrace:\n");
4129 dump_stack();
4130
4131 return 0;
4132 }
4133
4134 static void
4135 __lock_contended(struct lockdep_map *lock, unsigned long ip)
4136 {
4137 struct task_struct *curr = current;
4138 struct held_lock *hlock;
4139 struct lock_class_stats *stats;
4140 unsigned int depth;
4141 int i, contention_point, contending_point;
4142
4143 depth = curr->lockdep_depth;
4144 /*
4145 * Whee, we contended on this lock, except it seems we're not
4146 * actually trying to acquire anything much at all..
4147 */
4148 if (DEBUG_LOCKS_WARN_ON(!depth))
4149 return;
4150
4151 hlock = find_held_lock(curr, lock, depth, &i);
4152 if (!hlock) {
4153 print_lock_contention_bug(curr, lock, ip);
4154 return;
4155 }
4156
4157 if (hlock->instance != lock)
4158 return;
4159
4160 hlock->waittime_stamp = lockstat_clock();
4161
4162 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
4163 contending_point = lock_point(hlock_class(hlock)->contending_point,
4164 lock->ip);
4165
4166 stats = get_lock_stats(hlock_class(hlock));
4167 if (contention_point < LOCKSTAT_POINTS)
4168 stats->contention_point[contention_point]++;
4169 if (contending_point < LOCKSTAT_POINTS)
4170 stats->contending_point[contending_point]++;
4171 if (lock->cpu != smp_processor_id())
4172 stats->bounces[bounce_contended + !!hlock->read]++;
4173 put_lock_stats(stats);
4174 }
4175
4176 static void
4177 __lock_acquired(struct lockdep_map *lock, unsigned long ip)
4178 {
4179 struct task_struct *curr = current;
4180 struct held_lock *hlock;
4181 struct lock_class_stats *stats;
4182 unsigned int depth;
4183 u64 now, waittime = 0;
4184 int i, cpu;
4185
4186 depth = curr->lockdep_depth;
4187 /*
4188 * Yay, we acquired ownership of this lock we didn't try to
4189 * acquire, how the heck did that happen?
4190 */
4191 if (DEBUG_LOCKS_WARN_ON(!depth))
4192 return;
4193
4194 hlock = find_held_lock(curr, lock, depth, &i);
4195 if (!hlock) {
4196 print_lock_contention_bug(curr, lock, _RET_IP_);
4197 return;
4198 }
4199
4200 if (hlock->instance != lock)
4201 return;
4202
4203 cpu = smp_processor_id();
4204 if (hlock->waittime_stamp) {
4205 now = lockstat_clock();
4206 waittime = now - hlock->waittime_stamp;
4207 hlock->holdtime_stamp = now;
4208 }
4209
4210 trace_lock_acquired(lock, ip);
4211
4212 stats = get_lock_stats(hlock_class(hlock));
4213 if (waittime) {
4214 if (hlock->read)
4215 lock_time_inc(&stats->read_waittime, waittime);
4216 else
4217 lock_time_inc(&stats->write_waittime, waittime);
4218 }
4219 if (lock->cpu != cpu)
4220 stats->bounces[bounce_acquired + !!hlock->read]++;
4221 put_lock_stats(stats);
4222
4223 lock->cpu = cpu;
4224 lock->ip = ip;
4225 }
4226
4227 void lock_contended(struct lockdep_map *lock, unsigned long ip)
4228 {
4229 unsigned long flags;
4230
4231 if (unlikely(!lock_stat))
4232 return;
4233
4234 if (unlikely(current->lockdep_recursion))
4235 return;
4236
4237 raw_local_irq_save(flags);
4238 check_flags(flags);
4239 current->lockdep_recursion = 1;
4240 trace_lock_contended(lock, ip);
4241 __lock_contended(lock, ip);
4242 current->lockdep_recursion = 0;
4243 raw_local_irq_restore(flags);
4244 }
4245 EXPORT_SYMBOL_GPL(lock_contended);
4246
4247 void lock_acquired(struct lockdep_map *lock, unsigned long ip)
4248 {
4249 unsigned long flags;
4250
4251 if (unlikely(!lock_stat))
4252 return;
4253
4254 if (unlikely(current->lockdep_recursion))
4255 return;
4256
4257 raw_local_irq_save(flags);
4258 check_flags(flags);
4259 current->lockdep_recursion = 1;
4260 __lock_acquired(lock, ip);
4261 current->lockdep_recursion = 0;
4262 raw_local_irq_restore(flags);
4263 }
4264 EXPORT_SYMBOL_GPL(lock_acquired);
4265 #endif
4266
4267 /*
4268 * Used by the testsuite, sanitize the validator state
4269 * after a simulated failure:
4270 */
4271
4272 void lockdep_reset(void)
4273 {
4274 unsigned long flags;
4275 int i;
4276
4277 raw_local_irq_save(flags);
4278 current->curr_chain_key = 0;
4279 current->lockdep_depth = 0;
4280 current->lockdep_recursion = 0;
4281 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
4282 nr_hardirq_chains = 0;
4283 nr_softirq_chains = 0;
4284 nr_process_chains = 0;
4285 debug_locks = 1;
4286 for (i = 0; i < CHAINHASH_SIZE; i++)
4287 INIT_HLIST_HEAD(chainhash_table + i);
4288 raw_local_irq_restore(flags);
4289 }
4290
4291 static void zap_class(struct lock_class *class)
4292 {
4293 int i;
4294
4295 /*
4296 * Remove all dependencies this lock is
4297 * involved in:
4298 */
4299 for (i = 0; i < nr_list_entries; i++) {
4300 if (list_entries[i].class == class)
4301 list_del_rcu(&list_entries[i].entry);
4302 }
4303 /*
4304 * Unhash the class and remove it from the all_lock_classes list:
4305 */
4306 hlist_del_rcu(&class->hash_entry);
4307 list_del_rcu(&class->lock_entry);
4308
4309 RCU_INIT_POINTER(class->key, NULL);
4310 RCU_INIT_POINTER(class->name, NULL);
4311 }
4312
4313 static inline int within(const void *addr, void *start, unsigned long size)
4314 {
4315 return addr >= start && addr < start + size;
4316 }
4317
4318 /*
4319 * Used in module.c to remove lock classes from memory that is going to be
4320 * freed; and possibly re-used by other modules.
4321 *
4322 * We will have had one sync_sched() before getting here, so we're guaranteed
4323 * nobody will look up these exact classes -- they're properly dead but still
4324 * allocated.
4325 */
4326 void lockdep_free_key_range(void *start, unsigned long size)
4327 {
4328 struct lock_class *class;
4329 struct hlist_head *head;
4330 unsigned long flags;
4331 int i;
4332 int locked;
4333
4334 raw_local_irq_save(flags);
4335 locked = graph_lock();
4336
4337 /*
4338 * Unhash all classes that were created by this module:
4339 */
4340 for (i = 0; i < CLASSHASH_SIZE; i++) {
4341 head = classhash_table + i;
4342 hlist_for_each_entry_rcu(class, head, hash_entry) {
4343 if (within(class->key, start, size))
4344 zap_class(class);
4345 else if (within(class->name, start, size))
4346 zap_class(class);
4347 }
4348 }
4349
4350 if (locked)
4351 graph_unlock();
4352 raw_local_irq_restore(flags);
4353
4354 /*
4355 * Wait for any possible iterators from look_up_lock_class() to pass
4356 * before continuing to free the memory they refer to.
4357 *
4358 * sync_sched() is sufficient because the read-side is IRQ disable.
4359 */
4360 synchronize_sched();
4361
4362 /*
4363 * XXX at this point we could return the resources to the pool;
4364 * instead we leak them. We would need to change to bitmap allocators
4365 * instead of the linear allocators we have now.
4366 */
4367 }
4368
4369 void lockdep_reset_lock(struct lockdep_map *lock)
4370 {
4371 struct lock_class *class;
4372 struct hlist_head *head;
4373 unsigned long flags;
4374 int i, j;
4375 int locked;
4376
4377 raw_local_irq_save(flags);
4378
4379 /*
4380 * Remove all classes this lock might have:
4381 */
4382 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
4383 /*
4384 * If the class exists we look it up and zap it:
4385 */
4386 class = look_up_lock_class(lock, j);
4387 if (!IS_ERR_OR_NULL(class))
4388 zap_class(class);
4389 }
4390 /*
4391 * Debug check: in the end all mapped classes should
4392 * be gone.
4393 */
4394 locked = graph_lock();
4395 for (i = 0; i < CLASSHASH_SIZE; i++) {
4396 head = classhash_table + i;
4397 hlist_for_each_entry_rcu(class, head, hash_entry) {
4398 int match = 0;
4399
4400 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
4401 match |= class == lock->class_cache[j];
4402
4403 if (unlikely(match)) {
4404 if (debug_locks_off_graph_unlock()) {
4405 /*
4406 * We all just reset everything, how did it match?
4407 */
4408 WARN_ON(1);
4409 }
4410 goto out_restore;
4411 }
4412 }
4413 }
4414 if (locked)
4415 graph_unlock();
4416
4417 out_restore:
4418 raw_local_irq_restore(flags);
4419 }
4420
4421 void __init lockdep_info(void)
4422 {
4423 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
4424
4425 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
4426 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
4427 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
4428 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
4429 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
4430 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
4431 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
4432
4433 printk(" memory used by lock dependency info: %lu kB\n",
4434 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
4435 sizeof(struct list_head) * CLASSHASH_SIZE +
4436 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
4437 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
4438 sizeof(struct list_head) * CHAINHASH_SIZE
4439 #ifdef CONFIG_PROVE_LOCKING
4440 + sizeof(struct circular_queue)
4441 #endif
4442 ) / 1024
4443 );
4444
4445 printk(" per task-struct memory footprint: %lu bytes\n",
4446 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
4447 }
4448
4449 static void
4450 print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
4451 const void *mem_to, struct held_lock *hlock)
4452 {
4453 if (!debug_locks_off())
4454 return;
4455 if (debug_locks_silent)
4456 return;
4457
4458 pr_warn("\n");
4459 pr_warn("=========================\n");
4460 pr_warn("WARNING: held lock freed!\n");
4461 print_kernel_ident();
4462 pr_warn("-------------------------\n");
4463 pr_warn("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
4464 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
4465 print_lock(hlock);
4466 lockdep_print_held_locks(curr);
4467
4468 pr_warn("\nstack backtrace:\n");
4469 dump_stack();
4470 }
4471
4472 static inline int not_in_range(const void* mem_from, unsigned long mem_len,
4473 const void* lock_from, unsigned long lock_len)
4474 {
4475 return lock_from + lock_len <= mem_from ||
4476 mem_from + mem_len <= lock_from;
4477 }
4478
4479 /*
4480 * Called when kernel memory is freed (or unmapped), or if a lock
4481 * is destroyed or reinitialized - this code checks whether there is
4482 * any held lock in the memory range of <from> to <to>:
4483 */
4484 void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
4485 {
4486 struct task_struct *curr = current;
4487 struct held_lock *hlock;
4488 unsigned long flags;
4489 int i;
4490
4491 if (unlikely(!debug_locks))
4492 return;
4493
4494 local_irq_save(flags);
4495 for (i = 0; i < curr->lockdep_depth; i++) {
4496 hlock = curr->held_locks + i;
4497
4498 if (not_in_range(mem_from, mem_len, hlock->instance,
4499 sizeof(*hlock->instance)))
4500 continue;
4501
4502 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
4503 break;
4504 }
4505 local_irq_restore(flags);
4506 }
4507 EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
4508
4509 static void print_held_locks_bug(void)
4510 {
4511 if (!debug_locks_off())
4512 return;
4513 if (debug_locks_silent)
4514 return;
4515
4516 pr_warn("\n");
4517 pr_warn("====================================\n");
4518 pr_warn("WARNING: %s/%d still has locks held!\n",
4519 current->comm, task_pid_nr(current));
4520 print_kernel_ident();
4521 pr_warn("------------------------------------\n");
4522 lockdep_print_held_locks(current);
4523 pr_warn("\nstack backtrace:\n");
4524 dump_stack();
4525 }
4526
4527 void debug_check_no_locks_held(void)
4528 {
4529 if (unlikely(current->lockdep_depth > 0))
4530 print_held_locks_bug();
4531 }
4532 EXPORT_SYMBOL_GPL(debug_check_no_locks_held);
4533
4534 #ifdef __KERNEL__
4535 void debug_show_all_locks(void)
4536 {
4537 struct task_struct *g, *p;
4538 int count = 10;
4539 int unlock = 1;
4540
4541 if (unlikely(!debug_locks)) {
4542 pr_warn("INFO: lockdep is turned off.\n");
4543 return;
4544 }
4545 pr_warn("\nShowing all locks held in the system:\n");
4546
4547 /*
4548 * Here we try to get the tasklist_lock as hard as possible,
4549 * if not successful after 2 seconds we ignore it (but keep
4550 * trying). This is to enable a debug printout even if a
4551 * tasklist_lock-holding task deadlocks or crashes.
4552 */
4553 retry:
4554 if (!read_trylock(&tasklist_lock)) {
4555 if (count == 10)
4556 pr_warn("hm, tasklist_lock locked, retrying... ");
4557 if (count) {
4558 count--;
4559 pr_cont(" #%d", 10-count);
4560 mdelay(200);
4561 goto retry;
4562 }
4563 pr_cont(" ignoring it.\n");
4564 unlock = 0;
4565 } else {
4566 if (count != 10)
4567 pr_cont(" locked it.\n");
4568 }
4569
4570 do_each_thread(g, p) {
4571 /*
4572 * It's not reliable to print a task's held locks
4573 * if it's not sleeping (or if it's not the current
4574 * task):
4575 */
4576 if (p->state == TASK_RUNNING && p != current)
4577 continue;
4578 if (p->lockdep_depth)
4579 lockdep_print_held_locks(p);
4580 if (!unlock)
4581 if (read_trylock(&tasklist_lock))
4582 unlock = 1;
4583 } while_each_thread(g, p);
4584
4585 pr_warn("\n");
4586 pr_warn("=============================================\n\n");
4587
4588 if (unlock)
4589 read_unlock(&tasklist_lock);
4590 }
4591 EXPORT_SYMBOL_GPL(debug_show_all_locks);
4592 #endif
4593
4594 /*
4595 * Careful: only use this function if you are sure that
4596 * the task cannot run in parallel!
4597 */
4598 void debug_show_held_locks(struct task_struct *task)
4599 {
4600 if (unlikely(!debug_locks)) {
4601 printk("INFO: lockdep is turned off.\n");
4602 return;
4603 }
4604 lockdep_print_held_locks(task);
4605 }
4606 EXPORT_SYMBOL_GPL(debug_show_held_locks);
4607
4608 asmlinkage __visible void lockdep_sys_exit(void)
4609 {
4610 struct task_struct *curr = current;
4611
4612 if (unlikely(curr->lockdep_depth)) {
4613 if (!debug_locks_off())
4614 return;
4615 pr_warn("\n");
4616 pr_warn("================================================\n");
4617 pr_warn("WARNING: lock held when returning to user space!\n");
4618 print_kernel_ident();
4619 pr_warn("------------------------------------------------\n");
4620 pr_warn("%s/%d is leaving the kernel with locks still held!\n",
4621 curr->comm, curr->pid);
4622 lockdep_print_held_locks(curr);
4623 }
4624
4625 /*
4626 * The lock history for each syscall should be independent. So wipe the
4627 * slate clean on return to userspace.
4628 */
4629 lockdep_invariant_state(false);
4630 }
4631
4632 void lockdep_rcu_suspicious(const char *file, const int line, const char *s)
4633 {
4634 struct task_struct *curr = current;
4635
4636 /* Note: the following can be executed concurrently, so be careful. */
4637 pr_warn("\n");
4638 pr_warn("=============================\n");
4639 pr_warn("WARNING: suspicious RCU usage\n");
4640 print_kernel_ident();
4641 pr_warn("-----------------------------\n");
4642 pr_warn("%s:%d %s!\n", file, line, s);
4643 pr_warn("\nother info that might help us debug this:\n\n");
4644 pr_warn("\n%srcu_scheduler_active = %d, debug_locks = %d\n",
4645 !rcu_lockdep_current_cpu_online()
4646 ? "RCU used illegally from offline CPU!\n"
4647 : !rcu_is_watching()
4648 ? "RCU used illegally from idle CPU!\n"
4649 : "",
4650 rcu_scheduler_active, debug_locks);
4651
4652 /*
4653 * If a CPU is in the RCU-free window in idle (ie: in the section
4654 * between rcu_idle_enter() and rcu_idle_exit(), then RCU
4655 * considers that CPU to be in an "extended quiescent state",
4656 * which means that RCU will be completely ignoring that CPU.
4657 * Therefore, rcu_read_lock() and friends have absolutely no
4658 * effect on a CPU running in that state. In other words, even if
4659 * such an RCU-idle CPU has called rcu_read_lock(), RCU might well
4660 * delete data structures out from under it. RCU really has no
4661 * choice here: we need to keep an RCU-free window in idle where
4662 * the CPU may possibly enter into low power mode. This way we can
4663 * notice an extended quiescent state to other CPUs that started a grace
4664 * period. Otherwise we would delay any grace period as long as we run
4665 * in the idle task.
4666 *
4667 * So complain bitterly if someone does call rcu_read_lock(),
4668 * rcu_read_lock_bh() and so on from extended quiescent states.
4669 */
4670 if (!rcu_is_watching())
4671 pr_warn("RCU used illegally from extended quiescent state!\n");
4672
4673 lockdep_print_held_locks(curr);
4674 pr_warn("\nstack backtrace:\n");
4675 dump_stack();
4676 }
4677 EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious);
4678
4679 #ifdef CONFIG_LOCKDEP_CROSSRELEASE
4680
4681 /*
4682 * Crossrelease works by recording a lock history for each thread and
4683 * connecting those historic locks that were taken after the
4684 * wait_for_completion() in the complete() context.
4685 *
4686 * Task-A Task-B
4687 *
4688 * mutex_lock(&A);
4689 * mutex_unlock(&A);
4690 *
4691 * wait_for_completion(&C);
4692 * lock_acquire_crosslock();
4693 * atomic_inc_return(&cross_gen_id);
4694 * |
4695 * | mutex_lock(&B);
4696 * | mutex_unlock(&B);
4697 * |
4698 * | complete(&C);
4699 * `-- lock_commit_crosslock();
4700 *
4701 * Which will then add a dependency between B and C.
4702 */
4703
4704 #define xhlock(i) (current->xhlocks[(i) % MAX_XHLOCKS_NR])
4705
4706 /*
4707 * Whenever a crosslock is held, cross_gen_id will be increased.
4708 */
4709 static atomic_t cross_gen_id; /* Can be wrapped */
4710
4711 /*
4712 * Make an entry of the ring buffer invalid.
4713 */
4714 static inline void invalidate_xhlock(struct hist_lock *xhlock)
4715 {
4716 /*
4717 * Normally, xhlock->hlock.instance must be !NULL.
4718 */
4719 xhlock->hlock.instance = NULL;
4720 }
4721
4722 /*
4723 * Lock history stacks; we have 2 nested lock history stacks:
4724 *
4725 * HARD(IRQ)
4726 * SOFT(IRQ)
4727 *
4728 * The thing is that once we complete a HARD/SOFT IRQ the future task locks
4729 * should not depend on any of the locks observed while running the IRQ. So
4730 * what we do is rewind the history buffer and erase all our knowledge of that
4731 * temporal event.
4732 */
4733
4734 void crossrelease_hist_start(enum xhlock_context_t c)
4735 {
4736 struct task_struct *cur = current;
4737
4738 if (!cur->xhlocks)
4739 return;
4740
4741 cur->xhlock_idx_hist[c] = cur->xhlock_idx;
4742 cur->hist_id_save[c] = cur->hist_id;
4743 }
4744
4745 void crossrelease_hist_end(enum xhlock_context_t c)
4746 {
4747 struct task_struct *cur = current;
4748
4749 if (cur->xhlocks) {
4750 unsigned int idx = cur->xhlock_idx_hist[c];
4751 struct hist_lock *h = &xhlock(idx);
4752
4753 cur->xhlock_idx = idx;
4754
4755 /* Check if the ring was overwritten. */
4756 if (h->hist_id != cur->hist_id_save[c])
4757 invalidate_xhlock(h);
4758 }
4759 }
4760
4761 /*
4762 * lockdep_invariant_state() is used to annotate independence inside a task, to
4763 * make one task look like multiple independent 'tasks'.
4764 *
4765 * Take for instance workqueues; each work is independent of the last. The
4766 * completion of a future work does not depend on the completion of a past work
4767 * (in general). Therefore we must not carry that (lock) dependency across
4768 * works.
4769 *
4770 * This is true for many things; pretty much all kthreads fall into this
4771 * pattern, where they have an invariant state and future completions do not
4772 * depend on past completions. Its just that since they all have the 'same'
4773 * form -- the kthread does the same over and over -- it doesn't typically
4774 * matter.
4775 *
4776 * The same is true for system-calls, once a system call is completed (we've
4777 * returned to userspace) the next system call does not depend on the lock
4778 * history of the previous system call.
4779 *
4780 * They key property for independence, this invariant state, is that it must be
4781 * a point where we hold no locks and have no history. Because if we were to
4782 * hold locks, the restore at _end() would not necessarily recover it's history
4783 * entry. Similarly, independence per-definition means it does not depend on
4784 * prior state.
4785 */
4786 void lockdep_invariant_state(bool force)
4787 {
4788 /*
4789 * We call this at an invariant point, no current state, no history.
4790 * Verify the former, enforce the latter.
4791 */
4792 WARN_ON_ONCE(!force && current->lockdep_depth);
4793 invalidate_xhlock(&xhlock(current->xhlock_idx));
4794 }
4795
4796 static int cross_lock(struct lockdep_map *lock)
4797 {
4798 return lock ? lock->cross : 0;
4799 }
4800
4801 /*
4802 * This is needed to decide the relationship between wrapable variables.
4803 */
4804 static inline int before(unsigned int a, unsigned int b)
4805 {
4806 return (int)(a - b) < 0;
4807 }
4808
4809 static inline struct lock_class *xhlock_class(struct hist_lock *xhlock)
4810 {
4811 return hlock_class(&xhlock->hlock);
4812 }
4813
4814 static inline struct lock_class *xlock_class(struct cross_lock *xlock)
4815 {
4816 return hlock_class(&xlock->hlock);
4817 }
4818
4819 /*
4820 * Should we check a dependency with previous one?
4821 */
4822 static inline int depend_before(struct held_lock *hlock)
4823 {
4824 return hlock->read != 2 && hlock->check && !hlock->trylock;
4825 }
4826
4827 /*
4828 * Should we check a dependency with next one?
4829 */
4830 static inline int depend_after(struct held_lock *hlock)
4831 {
4832 return hlock->read != 2 && hlock->check;
4833 }
4834
4835 /*
4836 * Check if the xhlock is valid, which would be false if,
4837 *
4838 * 1. Has not used after initializaion yet.
4839 * 2. Got invalidated.
4840 *
4841 * Remind hist_lock is implemented as a ring buffer.
4842 */
4843 static inline int xhlock_valid(struct hist_lock *xhlock)
4844 {
4845 /*
4846 * xhlock->hlock.instance must be !NULL.
4847 */
4848 return !!xhlock->hlock.instance;
4849 }
4850
4851 /*
4852 * Record a hist_lock entry.
4853 *
4854 * Irq disable is only required.
4855 */
4856 static void add_xhlock(struct held_lock *hlock)
4857 {
4858 unsigned int idx = ++current->xhlock_idx;
4859 struct hist_lock *xhlock = &xhlock(idx);
4860
4861 #ifdef CONFIG_DEBUG_LOCKDEP
4862 /*
4863 * This can be done locklessly because they are all task-local
4864 * state, we must however ensure IRQs are disabled.
4865 */
4866 WARN_ON_ONCE(!irqs_disabled());
4867 #endif
4868
4869 /* Initialize hist_lock's members */
4870 xhlock->hlock = *hlock;
4871 xhlock->hist_id = ++current->hist_id;
4872
4873 xhlock->trace.nr_entries = 0;
4874 xhlock->trace.max_entries = MAX_XHLOCK_TRACE_ENTRIES;
4875 xhlock->trace.entries = xhlock->trace_entries;
4876
4877 if (crossrelease_fullstack) {
4878 xhlock->trace.skip = 3;
4879 save_stack_trace(&xhlock->trace);
4880 } else {
4881 xhlock->trace.nr_entries = 1;
4882 xhlock->trace.entries[0] = hlock->acquire_ip;
4883 }
4884 }
4885
4886 static inline int same_context_xhlock(struct hist_lock *xhlock)
4887 {
4888 return xhlock->hlock.irq_context == task_irq_context(current);
4889 }
4890
4891 /*
4892 * This should be lockless as far as possible because this would be
4893 * called very frequently.
4894 */
4895 static void check_add_xhlock(struct held_lock *hlock)
4896 {
4897 /*
4898 * Record a hist_lock, only in case that acquisitions ahead
4899 * could depend on the held_lock. For example, if the held_lock
4900 * is trylock then acquisitions ahead never depends on that.
4901 * In that case, we don't need to record it. Just return.
4902 */
4903 if (!current->xhlocks || !depend_before(hlock))
4904 return;
4905
4906 add_xhlock(hlock);
4907 }
4908
4909 /*
4910 * For crosslock.
4911 */
4912 static int add_xlock(struct held_lock *hlock)
4913 {
4914 struct cross_lock *xlock;
4915 unsigned int gen_id;
4916
4917 if (!graph_lock())
4918 return 0;
4919
4920 xlock = &((struct lockdep_map_cross *)hlock->instance)->xlock;
4921
4922 /*
4923 * When acquisitions for a crosslock are overlapped, we use
4924 * nr_acquire to perform commit for them, based on cross_gen_id
4925 * of the first acquisition, which allows to add additional
4926 * dependencies.
4927 *
4928 * Moreover, when no acquisition of a crosslock is in progress,
4929 * we should not perform commit because the lock might not exist
4930 * any more, which might cause incorrect memory access. So we
4931 * have to track the number of acquisitions of a crosslock.
4932 *
4933 * depend_after() is necessary to initialize only the first
4934 * valid xlock so that the xlock can be used on its commit.
4935 */
4936 if (xlock->nr_acquire++ && depend_after(&xlock->hlock))
4937 goto unlock;
4938
4939 gen_id = (unsigned int)atomic_inc_return(&cross_gen_id);
4940 xlock->hlock = *hlock;
4941 xlock->hlock.gen_id = gen_id;
4942 unlock:
4943 graph_unlock();
4944 return 1;
4945 }
4946
4947 /*
4948 * Called for both normal and crosslock acquires. Normal locks will be
4949 * pushed on the hist_lock queue. Cross locks will record state and
4950 * stop regular lock_acquire() to avoid being placed on the held_lock
4951 * stack.
4952 *
4953 * Return: 0 - failure;
4954 * 1 - crosslock, done;
4955 * 2 - normal lock, continue to held_lock[] ops.
4956 */
4957 static int lock_acquire_crosslock(struct held_lock *hlock)
4958 {
4959 /*
4960 * CONTEXT 1 CONTEXT 2
4961 * --------- ---------
4962 * lock A (cross)
4963 * X = atomic_inc_return(&cross_gen_id)
4964 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4965 * Y = atomic_read_acquire(&cross_gen_id)
4966 * lock B
4967 *
4968 * atomic_read_acquire() is for ordering between A and B,
4969 * IOW, A happens before B, when CONTEXT 2 see Y >= X.
4970 *
4971 * Pairs with atomic_inc_return() in add_xlock().
4972 */
4973 hlock->gen_id = (unsigned int)atomic_read_acquire(&cross_gen_id);
4974
4975 if (cross_lock(hlock->instance))
4976 return add_xlock(hlock);
4977
4978 check_add_xhlock(hlock);
4979 return 2;
4980 }
4981
4982 static int copy_trace(struct stack_trace *trace)
4983 {
4984 unsigned long *buf = stack_trace + nr_stack_trace_entries;
4985 unsigned int max_nr = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
4986 unsigned int nr = min(max_nr, trace->nr_entries);
4987
4988 trace->nr_entries = nr;
4989 memcpy(buf, trace->entries, nr * sizeof(trace->entries[0]));
4990 trace->entries = buf;
4991 nr_stack_trace_entries += nr;
4992
4993 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
4994 if (!debug_locks_off_graph_unlock())
4995 return 0;
4996
4997 print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!");
4998 dump_stack();
4999
5000 return 0;
5001 }
5002
5003 return 1;
5004 }
5005
5006 static int commit_xhlock(struct cross_lock *xlock, struct hist_lock *xhlock)
5007 {
5008 unsigned int xid, pid;
5009 u64 chain_key;
5010
5011 xid = xlock_class(xlock) - lock_classes;
5012 chain_key = iterate_chain_key((u64)0, xid);
5013 pid = xhlock_class(xhlock) - lock_classes;
5014 chain_key = iterate_chain_key(chain_key, pid);
5015
5016 if (lookup_chain_cache(chain_key))
5017 return 1;
5018
5019 if (!add_chain_cache_classes(xid, pid, xhlock->hlock.irq_context,
5020 chain_key))
5021 return 0;
5022
5023 if (!check_prev_add(current, &xlock->hlock, &xhlock->hlock, 1,
5024 &xhlock->trace, copy_trace))
5025 return 0;
5026
5027 return 1;
5028 }
5029
5030 static void commit_xhlocks(struct cross_lock *xlock)
5031 {
5032 unsigned int cur = current->xhlock_idx;
5033 unsigned int prev_hist_id = xhlock(cur).hist_id;
5034 unsigned int i;
5035
5036 if (!graph_lock())
5037 return;
5038
5039 if (xlock->nr_acquire) {
5040 for (i = 0; i < MAX_XHLOCKS_NR; i++) {
5041 struct hist_lock *xhlock = &xhlock(cur - i);
5042
5043 if (!xhlock_valid(xhlock))
5044 break;
5045
5046 if (before(xhlock->hlock.gen_id, xlock->hlock.gen_id))
5047 break;
5048
5049 if (!same_context_xhlock(xhlock))
5050 break;
5051
5052 /*
5053 * Filter out the cases where the ring buffer was
5054 * overwritten and the current entry has a bigger
5055 * hist_id than the previous one, which is impossible
5056 * otherwise:
5057 */
5058 if (unlikely(before(prev_hist_id, xhlock->hist_id)))
5059 break;
5060
5061 prev_hist_id = xhlock->hist_id;
5062
5063 /*
5064 * commit_xhlock() returns 0 with graph_lock already
5065 * released if fail.
5066 */
5067 if (!commit_xhlock(xlock, xhlock))
5068 return;
5069 }
5070 }
5071
5072 graph_unlock();
5073 }
5074
5075 void lock_commit_crosslock(struct lockdep_map *lock)
5076 {
5077 struct cross_lock *xlock;
5078 unsigned long flags;
5079
5080 if (unlikely(!debug_locks || current->lockdep_recursion))
5081 return;
5082
5083 if (!current->xhlocks)
5084 return;
5085
5086 /*
5087 * Do commit hist_locks with the cross_lock, only in case that
5088 * the cross_lock could depend on acquisitions after that.
5089 *
5090 * For example, if the cross_lock does not have the 'check' flag
5091 * then we don't need to check dependencies and commit for that.
5092 * Just skip it. In that case, of course, the cross_lock does
5093 * not depend on acquisitions ahead, either.
5094 *
5095 * WARNING: Don't do that in add_xlock() in advance. When an
5096 * acquisition context is different from the commit context,
5097 * invalid(skipped) cross_lock might be accessed.
5098 */
5099 if (!depend_after(&((struct lockdep_map_cross *)lock)->xlock.hlock))
5100 return;
5101
5102 raw_local_irq_save(flags);
5103 check_flags(flags);
5104 current->lockdep_recursion = 1;
5105 xlock = &((struct lockdep_map_cross *)lock)->xlock;
5106 commit_xhlocks(xlock);
5107 current->lockdep_recursion = 0;
5108 raw_local_irq_restore(flags);
5109 }
5110 EXPORT_SYMBOL_GPL(lock_commit_crosslock);
5111
5112 /*
5113 * Return: 0 - failure;
5114 * 1 - crosslock, done;
5115 * 2 - normal lock, continue to held_lock[] ops.
5116 */
5117 static int lock_release_crosslock(struct lockdep_map *lock)
5118 {
5119 if (cross_lock(lock)) {
5120 if (!graph_lock())
5121 return 0;
5122 ((struct lockdep_map_cross *)lock)->xlock.nr_acquire--;
5123 graph_unlock();
5124 return 1;
5125 }
5126 return 2;
5127 }
5128
5129 static void cross_init(struct lockdep_map *lock, int cross)
5130 {
5131 if (cross)
5132 ((struct lockdep_map_cross *)lock)->xlock.nr_acquire = 0;
5133
5134 lock->cross = cross;
5135
5136 /*
5137 * Crossrelease assumes that the ring buffer size of xhlocks
5138 * is aligned with power of 2. So force it on build.
5139 */
5140 BUILD_BUG_ON(MAX_XHLOCKS_NR & (MAX_XHLOCKS_NR - 1));
5141 }
5142
5143 void lockdep_init_task(struct task_struct *task)
5144 {
5145 int i;
5146
5147 task->xhlock_idx = UINT_MAX;
5148 task->hist_id = 0;
5149
5150 for (i = 0; i < XHLOCK_CTX_NR; i++) {
5151 task->xhlock_idx_hist[i] = UINT_MAX;
5152 task->hist_id_save[i] = 0;
5153 }
5154
5155 task->xhlocks = kzalloc(sizeof(struct hist_lock) * MAX_XHLOCKS_NR,
5156 GFP_KERNEL);
5157 }
5158
5159 void lockdep_free_task(struct task_struct *task)
5160 {
5161 if (task->xhlocks) {
5162 void *tmp = task->xhlocks;
5163 /* Diable crossrelease for current */
5164 task->xhlocks = NULL;
5165 kfree(tmp);
5166 }
5167 }
5168 #endif