1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#define DISABLE_BRANCH_PROFILING
29#include <linux/mutex.h>
30#include <linux/sched.h>
31#include <linux/delay.h>
32#include <linux/module.h>
33#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
35#include <linux/spinlock.h>
36#include <linux/kallsyms.h>
37#include <linux/interrupt.h>
38#include <linux/stacktrace.h>
39#include <linux/debug_locks.h>
40#include <linux/irqflags.h>
41#include <linux/utsname.h>
42#include <linux/hash.h>
43#include <linux/ftrace.h>
44#include <linux/stringify.h>
45#include <linux/bitops.h>
46#include <linux/gfp.h>
47#include <linux/kmemcheck.h>
48
49#include <asm/sections.h>
50
51#include "lockdep_internals.h"
52
53#define CREATE_TRACE_POINTS
54#include <trace/events/lock.h>
55
56#ifdef CONFIG_PROVE_LOCKING
57int prove_locking = 1;
58module_param(prove_locking, int, 0644);
59#else
60#define prove_locking 0
61#endif
62
63#ifdef CONFIG_LOCK_STAT
64int lock_stat = 1;
65module_param(lock_stat, int, 0644);
66#else
67#define lock_stat 0
68#endif
69
70
71
72
73
74
75
76
77
78static arch_spinlock_t lockdep_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
79
80static int graph_lock(void)
81{
82 arch_spin_lock(&lockdep_lock);
83
84
85
86
87
88
89 if (!debug_locks) {
90 arch_spin_unlock(&lockdep_lock);
91 return 0;
92 }
93
94 current->lockdep_recursion++;
95 return 1;
96}
97
98static inline int graph_unlock(void)
99{
100 if (debug_locks && !arch_spin_is_locked(&lockdep_lock))
101 return DEBUG_LOCKS_WARN_ON(1);
102
103 current->lockdep_recursion--;
104 arch_spin_unlock(&lockdep_lock);
105 return 0;
106}
107
108
109
110
111
112static inline int debug_locks_off_graph_unlock(void)
113{
114 int ret = debug_locks_off();
115
116 arch_spin_unlock(&lockdep_lock);
117
118 return ret;
119}
120
121static int lockdep_initialized;
122
123unsigned long nr_list_entries;
124static struct lock_list list_entries[MAX_LOCKDEP_ENTRIES];
125
126
127
128
129
130
131
132unsigned long nr_lock_classes;
133static struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
134
135static inline struct lock_class *hlock_class(struct held_lock *hlock)
136{
137 if (!hlock->class_idx) {
138 DEBUG_LOCKS_WARN_ON(1);
139 return NULL;
140 }
141 return lock_classes + hlock->class_idx - 1;
142}
143
144#ifdef CONFIG_LOCK_STAT
145static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS],
146 cpu_lock_stats);
147
148static inline u64 lockstat_clock(void)
149{
150 return local_clock();
151}
152
153static int lock_point(unsigned long points[], unsigned long ip)
154{
155 int i;
156
157 for (i = 0; i < LOCKSTAT_POINTS; i++) {
158 if (points[i] == 0) {
159 points[i] = ip;
160 break;
161 }
162 if (points[i] == ip)
163 break;
164 }
165
166 return i;
167}
168
169static void lock_time_inc(struct lock_time *lt, u64 time)
170{
171 if (time > lt->max)
172 lt->max = time;
173
174 if (time < lt->min || !lt->nr)
175 lt->min = time;
176
177 lt->total += time;
178 lt->nr++;
179}
180
181static inline void lock_time_add(struct lock_time *src, struct lock_time *dst)
182{
183 if (!src->nr)
184 return;
185
186 if (src->max > dst->max)
187 dst->max = src->max;
188
189 if (src->min < dst->min || !dst->nr)
190 dst->min = src->min;
191
192 dst->total += src->total;
193 dst->nr += src->nr;
194}
195
196struct lock_class_stats lock_stats(struct lock_class *class)
197{
198 struct lock_class_stats stats;
199 int cpu, i;
200
201 memset(&stats, 0, sizeof(struct lock_class_stats));
202 for_each_possible_cpu(cpu) {
203 struct lock_class_stats *pcs =
204 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
205
206 for (i = 0; i < ARRAY_SIZE(stats.contention_point); i++)
207 stats.contention_point[i] += pcs->contention_point[i];
208
209 for (i = 0; i < ARRAY_SIZE(stats.contending_point); i++)
210 stats.contending_point[i] += pcs->contending_point[i];
211
212 lock_time_add(&pcs->read_waittime, &stats.read_waittime);
213 lock_time_add(&pcs->write_waittime, &stats.write_waittime);
214
215 lock_time_add(&pcs->read_holdtime, &stats.read_holdtime);
216 lock_time_add(&pcs->write_holdtime, &stats.write_holdtime);
217
218 for (i = 0; i < ARRAY_SIZE(stats.bounces); i++)
219 stats.bounces[i] += pcs->bounces[i];
220 }
221
222 return stats;
223}
224
225void clear_lock_stats(struct lock_class *class)
226{
227 int cpu;
228
229 for_each_possible_cpu(cpu) {
230 struct lock_class_stats *cpu_stats =
231 &per_cpu(cpu_lock_stats, cpu)[class - lock_classes];
232
233 memset(cpu_stats, 0, sizeof(struct lock_class_stats));
234 }
235 memset(class->contention_point, 0, sizeof(class->contention_point));
236 memset(class->contending_point, 0, sizeof(class->contending_point));
237}
238
239static struct lock_class_stats *get_lock_stats(struct lock_class *class)
240{
241 return &get_cpu_var(cpu_lock_stats)[class - lock_classes];
242}
243
244static void put_lock_stats(struct lock_class_stats *stats)
245{
246 put_cpu_var(cpu_lock_stats);
247}
248
249static void lock_release_holdtime(struct held_lock *hlock)
250{
251 struct lock_class_stats *stats;
252 u64 holdtime;
253
254 if (!lock_stat)
255 return;
256
257 holdtime = lockstat_clock() - hlock->holdtime_stamp;
258
259 stats = get_lock_stats(hlock_class(hlock));
260 if (hlock->read)
261 lock_time_inc(&stats->read_holdtime, holdtime);
262 else
263 lock_time_inc(&stats->write_holdtime, holdtime);
264 put_lock_stats(stats);
265}
266#else
267static inline void lock_release_holdtime(struct held_lock *hlock)
268{
269}
270#endif
271
272
273
274
275
276
277LIST_HEAD(all_lock_classes);
278
279
280
281
282#define CLASSHASH_BITS (MAX_LOCKDEP_KEYS_BITS - 1)
283#define CLASSHASH_SIZE (1UL << CLASSHASH_BITS)
284#define __classhashfn(key) hash_long((unsigned long)key, CLASSHASH_BITS)
285#define classhashentry(key) (classhash_table + __classhashfn((key)))
286
287static struct list_head classhash_table[CLASSHASH_SIZE];
288
289
290
291
292
293#define CHAINHASH_BITS (MAX_LOCKDEP_CHAINS_BITS-1)
294#define CHAINHASH_SIZE (1UL << CHAINHASH_BITS)
295#define __chainhashfn(chain) hash_long(chain, CHAINHASH_BITS)
296#define chainhashentry(chain) (chainhash_table + __chainhashfn((chain)))
297
298static struct list_head chainhash_table[CHAINHASH_SIZE];
299
300
301
302
303
304
305
306#define iterate_chain_key(key1, key2) \
307 (((key1) << MAX_LOCKDEP_KEYS_BITS) ^ \
308 ((key1) >> (64-MAX_LOCKDEP_KEYS_BITS)) ^ \
309 (key2))
310
311void lockdep_off(void)
312{
313 current->lockdep_recursion++;
314}
315EXPORT_SYMBOL(lockdep_off);
316
317void lockdep_on(void)
318{
319 current->lockdep_recursion--;
320}
321EXPORT_SYMBOL(lockdep_on);
322
323
324
325
326
327#define VERBOSE 0
328#define VERY_VERBOSE 0
329
330#if VERBOSE
331# define HARDIRQ_VERBOSE 1
332# define SOFTIRQ_VERBOSE 1
333# define RECLAIM_VERBOSE 1
334#else
335# define HARDIRQ_VERBOSE 0
336# define SOFTIRQ_VERBOSE 0
337# define RECLAIM_VERBOSE 0
338#endif
339
340#if VERBOSE || HARDIRQ_VERBOSE || SOFTIRQ_VERBOSE || RECLAIM_VERBOSE
341
342
343
344static int class_filter(struct lock_class *class)
345{
346#if 0
347
348 if (class->name_version == 1 &&
349 !strcmp(class->name, "lockname"))
350 return 1;
351 if (class->name_version == 1 &&
352 !strcmp(class->name, "&struct->lockfield"))
353 return 1;
354#endif
355
356 return 0;
357}
358#endif
359
360static int verbose(struct lock_class *class)
361{
362#if VERBOSE
363 return class_filter(class);
364#endif
365 return 0;
366}
367
368
369
370
371
372unsigned long nr_stack_trace_entries;
373static unsigned long stack_trace[MAX_STACK_TRACE_ENTRIES];
374
375static int save_trace(struct stack_trace *trace)
376{
377 trace->nr_entries = 0;
378 trace->max_entries = MAX_STACK_TRACE_ENTRIES - nr_stack_trace_entries;
379 trace->entries = stack_trace + nr_stack_trace_entries;
380
381 trace->skip = 3;
382
383 save_stack_trace(trace);
384
385
386
387
388
389
390
391
392 if (trace->nr_entries != 0 &&
393 trace->entries[trace->nr_entries-1] == ULONG_MAX)
394 trace->nr_entries--;
395
396 trace->max_entries = trace->nr_entries;
397
398 nr_stack_trace_entries += trace->nr_entries;
399
400 if (nr_stack_trace_entries >= MAX_STACK_TRACE_ENTRIES-1) {
401 if (!debug_locks_off_graph_unlock())
402 return 0;
403
404 printk("BUG: MAX_STACK_TRACE_ENTRIES too low!\n");
405 printk("turning off the locking correctness validator.\n");
406 dump_stack();
407
408 return 0;
409 }
410
411 return 1;
412}
413
414unsigned int nr_hardirq_chains;
415unsigned int nr_softirq_chains;
416unsigned int nr_process_chains;
417unsigned int max_lockdep_depth;
418
419#ifdef CONFIG_DEBUG_LOCKDEP
420
421
422
423
424
425static int lockdep_init_error;
426static unsigned long lockdep_init_trace_data[20];
427static struct stack_trace lockdep_init_trace = {
428 .max_entries = ARRAY_SIZE(lockdep_init_trace_data),
429 .entries = lockdep_init_trace_data,
430};
431
432
433
434
435DEFINE_PER_CPU(struct lockdep_stats, lockdep_stats);
436#endif
437
438
439
440
441
442#define __USAGE(__STATE) \
443 [LOCK_USED_IN_##__STATE] = "IN-"__stringify(__STATE)"-W", \
444 [LOCK_ENABLED_##__STATE] = __stringify(__STATE)"-ON-W", \
445 [LOCK_USED_IN_##__STATE##_READ] = "IN-"__stringify(__STATE)"-R",\
446 [LOCK_ENABLED_##__STATE##_READ] = __stringify(__STATE)"-ON-R",
447
448static const char *usage_str[] =
449{
450#define LOCKDEP_STATE(__STATE) __USAGE(__STATE)
451#include "lockdep_states.h"
452#undef LOCKDEP_STATE
453 [LOCK_USED] = "INITIAL USE",
454};
455
456const char * __get_key_name(struct lockdep_subclass_key *key, char *str)
457{
458 return kallsyms_lookup((unsigned long)key, NULL, NULL, NULL, str);
459}
460
461static inline unsigned long lock_flag(enum lock_usage_bit bit)
462{
463 return 1UL << bit;
464}
465
466static char get_usage_char(struct lock_class *class, enum lock_usage_bit bit)
467{
468 char c = '.';
469
470 if (class->usage_mask & lock_flag(bit + 2))
471 c = '+';
472 if (class->usage_mask & lock_flag(bit)) {
473 c = '-';
474 if (class->usage_mask & lock_flag(bit + 2))
475 c = '?';
476 }
477
478 return c;
479}
480
481void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS])
482{
483 int i = 0;
484
485#define LOCKDEP_STATE(__STATE) \
486 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE); \
487 usage[i++] = get_usage_char(class, LOCK_USED_IN_##__STATE##_READ);
488#include "lockdep_states.h"
489#undef LOCKDEP_STATE
490
491 usage[i] = '\0';
492}
493
494static int __print_lock_name(struct lock_class *class)
495{
496 char str[KSYM_NAME_LEN];
497 const char *name;
498
499 name = class->name;
500 if (!name)
501 name = __get_key_name(class->key, str);
502
503 return printk("%s", name);
504}
505
506static void print_lock_name(struct lock_class *class)
507{
508 char str[KSYM_NAME_LEN], usage[LOCK_USAGE_CHARS];
509 const char *name;
510
511 get_usage_chars(class, usage);
512
513 name = class->name;
514 if (!name) {
515 name = __get_key_name(class->key, str);
516 printk(" (%s", name);
517 } else {
518 printk(" (%s", name);
519 if (class->name_version > 1)
520 printk("#%d", class->name_version);
521 if (class->subclass)
522 printk("/%d", class->subclass);
523 }
524 printk("){%s}", usage);
525}
526
527static void print_lockdep_cache(struct lockdep_map *lock)
528{
529 const char *name;
530 char str[KSYM_NAME_LEN];
531
532 name = lock->name;
533 if (!name)
534 name = __get_key_name(lock->key->subkeys, str);
535
536 printk("%s", name);
537}
538
539static void print_lock(struct held_lock *hlock)
540{
541 print_lock_name(hlock_class(hlock));
542 printk(", at: ");
543 print_ip_sym(hlock->acquire_ip);
544}
545
546static void lockdep_print_held_locks(struct task_struct *curr)
547{
548 int i, depth = curr->lockdep_depth;
549
550 if (!depth) {
551 printk("no locks held by %s/%d.\n", curr->comm, task_pid_nr(curr));
552 return;
553 }
554 printk("%d lock%s held by %s/%d:\n",
555 depth, depth > 1 ? "s" : "", curr->comm, task_pid_nr(curr));
556
557 for (i = 0; i < depth; i++) {
558 printk(" #%d: ", i);
559 print_lock(curr->held_locks + i);
560 }
561}
562
563static void print_kernel_version(void)
564{
565 printk("%s %.*s\n", init_utsname()->release,
566 (int)strcspn(init_utsname()->version, " "),
567 init_utsname()->version);
568}
569
570static int very_verbose(struct lock_class *class)
571{
572#if VERY_VERBOSE
573 return class_filter(class);
574#endif
575 return 0;
576}
577
578
579
580
581static int static_obj(void *obj)
582{
583 unsigned long start = (unsigned long) &_stext,
584 end = (unsigned long) &_end,
585 addr = (unsigned long) obj;
586
587
588
589
590 if ((addr >= start) && (addr < end))
591 return 1;
592
593 if (arch_is_kernel_data(addr))
594 return 1;
595
596
597
598
599 if (is_kernel_percpu_address(addr))
600 return 1;
601
602
603
604
605 return is_module_address(addr) || is_module_percpu_address(addr);
606}
607
608
609
610
611
612static int count_matching_names(struct lock_class *new_class)
613{
614 struct lock_class *class;
615 int count = 0;
616
617 if (!new_class->name)
618 return 0;
619
620 list_for_each_entry(class, &all_lock_classes, lock_entry) {
621 if (new_class->key - new_class->subclass == class->key)
622 return class->name_version;
623 if (class->name && !strcmp(class->name, new_class->name))
624 count = max(count, class->name_version);
625 }
626
627 return count + 1;
628}
629
630
631
632
633
634
635static inline struct lock_class *
636look_up_lock_class(struct lockdep_map *lock, unsigned int subclass)
637{
638 struct lockdep_subclass_key *key;
639 struct list_head *hash_head;
640 struct lock_class *class;
641
642#ifdef CONFIG_DEBUG_LOCKDEP
643
644
645
646
647
648 if (unlikely(!lockdep_initialized)) {
649 lockdep_init();
650 lockdep_init_error = 1;
651 save_stack_trace(&lockdep_init_trace);
652 }
653#endif
654
655 if (unlikely(subclass >= MAX_LOCKDEP_SUBCLASSES)) {
656 debug_locks_off();
657 printk(KERN_ERR
658 "BUG: looking up invalid subclass: %u\n", subclass);
659 printk(KERN_ERR
660 "turning off the locking correctness validator.\n");
661 dump_stack();
662 return NULL;
663 }
664
665
666
667
668
669 if (unlikely(!lock->key))
670 lock->key = (void *)lock;
671
672
673
674
675
676
677
678 BUILD_BUG_ON(sizeof(struct lock_class_key) >
679 sizeof(struct lockdep_map));
680
681 key = lock->key->subkeys + subclass;
682
683 hash_head = classhashentry(key);
684
685
686
687
688
689 list_for_each_entry(class, hash_head, hash_entry) {
690 if (class->key == key) {
691 WARN_ON_ONCE(class->name != lock->name);
692 return class;
693 }
694 }
695
696 return NULL;
697}
698
699
700
701
702
703
704static inline struct lock_class *
705register_lock_class(struct lockdep_map *lock, unsigned int subclass, int force)
706{
707 struct lockdep_subclass_key *key;
708 struct list_head *hash_head;
709 struct lock_class *class;
710 unsigned long flags;
711
712 class = look_up_lock_class(lock, subclass);
713 if (likely(class))
714 return class;
715
716
717
718
719 if (!static_obj(lock->key)) {
720 debug_locks_off();
721 printk("INFO: trying to register non-static key.\n");
722 printk("the code is fine but needs lockdep annotation.\n");
723 printk("turning off the locking correctness validator.\n");
724 dump_stack();
725
726 return NULL;
727 }
728
729 key = lock->key->subkeys + subclass;
730 hash_head = classhashentry(key);
731
732 raw_local_irq_save(flags);
733 if (!graph_lock()) {
734 raw_local_irq_restore(flags);
735 return NULL;
736 }
737
738
739
740
741 list_for_each_entry(class, hash_head, hash_entry)
742 if (class->key == key)
743 goto out_unlock_set;
744
745
746
747
748 if (nr_lock_classes >= MAX_LOCKDEP_KEYS) {
749 if (!debug_locks_off_graph_unlock()) {
750 raw_local_irq_restore(flags);
751 return NULL;
752 }
753 raw_local_irq_restore(flags);
754
755 printk("BUG: MAX_LOCKDEP_KEYS too low!\n");
756 printk("turning off the locking correctness validator.\n");
757 dump_stack();
758 return NULL;
759 }
760 class = lock_classes + nr_lock_classes++;
761 debug_atomic_inc(nr_unused_locks);
762 class->key = key;
763 class->name = lock->name;
764 class->subclass = subclass;
765 INIT_LIST_HEAD(&class->lock_entry);
766 INIT_LIST_HEAD(&class->locks_before);
767 INIT_LIST_HEAD(&class->locks_after);
768 class->name_version = count_matching_names(class);
769
770
771
772
773 list_add_tail_rcu(&class->hash_entry, hash_head);
774
775
776
777 list_add_tail_rcu(&class->lock_entry, &all_lock_classes);
778
779 if (verbose(class)) {
780 graph_unlock();
781 raw_local_irq_restore(flags);
782
783 printk("\nnew class %p: %s", class->key, class->name);
784 if (class->name_version > 1)
785 printk("#%d", class->name_version);
786 printk("\n");
787 dump_stack();
788
789 raw_local_irq_save(flags);
790 if (!graph_lock()) {
791 raw_local_irq_restore(flags);
792 return NULL;
793 }
794 }
795out_unlock_set:
796 graph_unlock();
797 raw_local_irq_restore(flags);
798
799 if (!subclass || force)
800 lock->class_cache[0] = class;
801 else if (subclass < NR_LOCKDEP_CACHING_CLASSES)
802 lock->class_cache[subclass] = class;
803
804 if (DEBUG_LOCKS_WARN_ON(class->subclass != subclass))
805 return NULL;
806
807 return class;
808}
809
810#ifdef CONFIG_PROVE_LOCKING
811
812
813
814
815static struct lock_list *alloc_list_entry(void)
816{
817 if (nr_list_entries >= MAX_LOCKDEP_ENTRIES) {
818 if (!debug_locks_off_graph_unlock())
819 return NULL;
820
821 printk("BUG: MAX_LOCKDEP_ENTRIES too low!\n");
822 printk("turning off the locking correctness validator.\n");
823 dump_stack();
824 return NULL;
825 }
826 return list_entries + nr_list_entries++;
827}
828
829
830
831
832static int add_lock_to_list(struct lock_class *class, struct lock_class *this,
833 struct list_head *head, unsigned long ip,
834 int distance, struct stack_trace *trace)
835{
836 struct lock_list *entry;
837
838
839
840
841 entry = alloc_list_entry();
842 if (!entry)
843 return 0;
844
845 entry->class = this;
846 entry->distance = distance;
847 entry->trace = *trace;
848
849
850
851
852
853
854
855 list_add_tail_rcu(&entry->entry, head);
856
857 return 1;
858}
859
860
861
862
863#define MAX_CIRCULAR_QUEUE_SIZE 4096UL
864#define CQ_MASK (MAX_CIRCULAR_QUEUE_SIZE-1)
865
866
867
868
869
870
871
872struct circular_queue {
873 unsigned long element[MAX_CIRCULAR_QUEUE_SIZE];
874 unsigned int front, rear;
875};
876
877static struct circular_queue lock_cq;
878
879unsigned int max_bfs_queue_depth;
880
881static unsigned int lockdep_dependency_gen_id;
882
883static inline void __cq_init(struct circular_queue *cq)
884{
885 cq->front = cq->rear = 0;
886 lockdep_dependency_gen_id++;
887}
888
889static inline int __cq_empty(struct circular_queue *cq)
890{
891 return (cq->front == cq->rear);
892}
893
894static inline int __cq_full(struct circular_queue *cq)
895{
896 return ((cq->rear + 1) & CQ_MASK) == cq->front;
897}
898
899static inline int __cq_enqueue(struct circular_queue *cq, unsigned long elem)
900{
901 if (__cq_full(cq))
902 return -1;
903
904 cq->element[cq->rear] = elem;
905 cq->rear = (cq->rear + 1) & CQ_MASK;
906 return 0;
907}
908
909static inline int __cq_dequeue(struct circular_queue *cq, unsigned long *elem)
910{
911 if (__cq_empty(cq))
912 return -1;
913
914 *elem = cq->element[cq->front];
915 cq->front = (cq->front + 1) & CQ_MASK;
916 return 0;
917}
918
919static inline unsigned int __cq_get_elem_count(struct circular_queue *cq)
920{
921 return (cq->rear - cq->front) & CQ_MASK;
922}
923
924static inline void mark_lock_accessed(struct lock_list *lock,
925 struct lock_list *parent)
926{
927 unsigned long nr;
928
929 nr = lock - list_entries;
930 WARN_ON(nr >= nr_list_entries);
931 lock->parent = parent;
932 lock->class->dep_gen_id = lockdep_dependency_gen_id;
933}
934
935static inline unsigned long lock_accessed(struct lock_list *lock)
936{
937 unsigned long nr;
938
939 nr = lock - list_entries;
940 WARN_ON(nr >= nr_list_entries);
941 return lock->class->dep_gen_id == lockdep_dependency_gen_id;
942}
943
944static inline struct lock_list *get_lock_parent(struct lock_list *child)
945{
946 return child->parent;
947}
948
949static inline int get_lock_depth(struct lock_list *child)
950{
951 int depth = 0;
952 struct lock_list *parent;
953
954 while ((parent = get_lock_parent(child))) {
955 child = parent;
956 depth++;
957 }
958 return depth;
959}
960
961static int __bfs(struct lock_list *source_entry,
962 void *data,
963 int (*match)(struct lock_list *entry, void *data),
964 struct lock_list **target_entry,
965 int forward)
966{
967 struct lock_list *entry;
968 struct list_head *head;
969 struct circular_queue *cq = &lock_cq;
970 int ret = 1;
971
972 if (match(source_entry, data)) {
973 *target_entry = source_entry;
974 ret = 0;
975 goto exit;
976 }
977
978 if (forward)
979 head = &source_entry->class->locks_after;
980 else
981 head = &source_entry->class->locks_before;
982
983 if (list_empty(head))
984 goto exit;
985
986 __cq_init(cq);
987 __cq_enqueue(cq, (unsigned long)source_entry);
988
989 while (!__cq_empty(cq)) {
990 struct lock_list *lock;
991
992 __cq_dequeue(cq, (unsigned long *)&lock);
993
994 if (!lock->class) {
995 ret = -2;
996 goto exit;
997 }
998
999 if (forward)
1000 head = &lock->class->locks_after;
1001 else
1002 head = &lock->class->locks_before;
1003
1004 list_for_each_entry(entry, head, entry) {
1005 if (!lock_accessed(entry)) {
1006 unsigned int cq_depth;
1007 mark_lock_accessed(entry, lock);
1008 if (match(entry, data)) {
1009 *target_entry = entry;
1010 ret = 0;
1011 goto exit;
1012 }
1013
1014 if (__cq_enqueue(cq, (unsigned long)entry)) {
1015 ret = -1;
1016 goto exit;
1017 }
1018 cq_depth = __cq_get_elem_count(cq);
1019 if (max_bfs_queue_depth < cq_depth)
1020 max_bfs_queue_depth = cq_depth;
1021 }
1022 }
1023 }
1024exit:
1025 return ret;
1026}
1027
1028static inline int __bfs_forwards(struct lock_list *src_entry,
1029 void *data,
1030 int (*match)(struct lock_list *entry, void *data),
1031 struct lock_list **target_entry)
1032{
1033 return __bfs(src_entry, data, match, target_entry, 1);
1034
1035}
1036
1037static inline int __bfs_backwards(struct lock_list *src_entry,
1038 void *data,
1039 int (*match)(struct lock_list *entry, void *data),
1040 struct lock_list **target_entry)
1041{
1042 return __bfs(src_entry, data, match, target_entry, 0);
1043
1044}
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056static noinline int
1057print_circular_bug_entry(struct lock_list *target, int depth)
1058{
1059 if (debug_locks_silent)
1060 return 0;
1061 printk("\n-> #%u", depth);
1062 print_lock_name(target->class);
1063 printk(":\n");
1064 print_stack_trace(&target->trace, 6);
1065
1066 return 0;
1067}
1068
1069static void
1070print_circular_lock_scenario(struct held_lock *src,
1071 struct held_lock *tgt,
1072 struct lock_list *prt)
1073{
1074 struct lock_class *source = hlock_class(src);
1075 struct lock_class *target = hlock_class(tgt);
1076 struct lock_class *parent = prt->class;
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091 if (parent != source) {
1092 printk("Chain exists of:\n ");
1093 __print_lock_name(source);
1094 printk(" --> ");
1095 __print_lock_name(parent);
1096 printk(" --> ");
1097 __print_lock_name(target);
1098 printk("\n\n");
1099 }
1100
1101 printk(" Possible unsafe locking scenario:\n\n");
1102 printk(" CPU0 CPU1\n");
1103 printk(" ---- ----\n");
1104 printk(" lock(");
1105 __print_lock_name(target);
1106 printk(");\n");
1107 printk(" lock(");
1108 __print_lock_name(parent);
1109 printk(");\n");
1110 printk(" lock(");
1111 __print_lock_name(target);
1112 printk(");\n");
1113 printk(" lock(");
1114 __print_lock_name(source);
1115 printk(");\n");
1116 printk("\n *** DEADLOCK ***\n\n");
1117}
1118
1119
1120
1121
1122
1123static noinline int
1124print_circular_bug_header(struct lock_list *entry, unsigned int depth,
1125 struct held_lock *check_src,
1126 struct held_lock *check_tgt)
1127{
1128 struct task_struct *curr = current;
1129
1130 if (debug_locks_silent)
1131 return 0;
1132
1133 printk("\n=======================================================\n");
1134 printk( "[ INFO: possible circular locking dependency detected ]\n");
1135 print_kernel_version();
1136 printk( "-------------------------------------------------------\n");
1137 printk("%s/%d is trying to acquire lock:\n",
1138 curr->comm, task_pid_nr(curr));
1139 print_lock(check_src);
1140 printk("\nbut task is already holding lock:\n");
1141 print_lock(check_tgt);
1142 printk("\nwhich lock already depends on the new lock.\n\n");
1143 printk("\nthe existing dependency chain (in reverse order) is:\n");
1144
1145 print_circular_bug_entry(entry, depth);
1146
1147 return 0;
1148}
1149
1150static inline int class_equal(struct lock_list *entry, void *data)
1151{
1152 return entry->class == data;
1153}
1154
1155static noinline int print_circular_bug(struct lock_list *this,
1156 struct lock_list *target,
1157 struct held_lock *check_src,
1158 struct held_lock *check_tgt)
1159{
1160 struct task_struct *curr = current;
1161 struct lock_list *parent;
1162 struct lock_list *first_parent;
1163 int depth;
1164
1165 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1166 return 0;
1167
1168 if (!save_trace(&this->trace))
1169 return 0;
1170
1171 depth = get_lock_depth(target);
1172
1173 print_circular_bug_header(target, depth, check_src, check_tgt);
1174
1175 parent = get_lock_parent(target);
1176 first_parent = parent;
1177
1178 while (parent) {
1179 print_circular_bug_entry(parent, --depth);
1180 parent = get_lock_parent(parent);
1181 }
1182
1183 printk("\nother info that might help us debug this:\n\n");
1184 print_circular_lock_scenario(check_src, check_tgt,
1185 first_parent);
1186
1187 lockdep_print_held_locks(curr);
1188
1189 printk("\nstack backtrace:\n");
1190 dump_stack();
1191
1192 return 0;
1193}
1194
1195static noinline int print_bfs_bug(int ret)
1196{
1197 if (!debug_locks_off_graph_unlock())
1198 return 0;
1199
1200 WARN(1, "lockdep bfs error:%d\n", ret);
1201
1202 return 0;
1203}
1204
1205static int noop_count(struct lock_list *entry, void *data)
1206{
1207 (*(unsigned long *)data)++;
1208 return 0;
1209}
1210
1211unsigned long __lockdep_count_forward_deps(struct lock_list *this)
1212{
1213 unsigned long count = 0;
1214 struct lock_list *uninitialized_var(target_entry);
1215
1216 __bfs_forwards(this, (void *)&count, noop_count, &target_entry);
1217
1218 return count;
1219}
1220unsigned long lockdep_count_forward_deps(struct lock_class *class)
1221{
1222 unsigned long ret, flags;
1223 struct lock_list this;
1224
1225 this.parent = NULL;
1226 this.class = class;
1227
1228 local_irq_save(flags);
1229 arch_spin_lock(&lockdep_lock);
1230 ret = __lockdep_count_forward_deps(&this);
1231 arch_spin_unlock(&lockdep_lock);
1232 local_irq_restore(flags);
1233
1234 return ret;
1235}
1236
1237unsigned long __lockdep_count_backward_deps(struct lock_list *this)
1238{
1239 unsigned long count = 0;
1240 struct lock_list *uninitialized_var(target_entry);
1241
1242 __bfs_backwards(this, (void *)&count, noop_count, &target_entry);
1243
1244 return count;
1245}
1246
1247unsigned long lockdep_count_backward_deps(struct lock_class *class)
1248{
1249 unsigned long ret, flags;
1250 struct lock_list this;
1251
1252 this.parent = NULL;
1253 this.class = class;
1254
1255 local_irq_save(flags);
1256 arch_spin_lock(&lockdep_lock);
1257 ret = __lockdep_count_backward_deps(&this);
1258 arch_spin_unlock(&lockdep_lock);
1259 local_irq_restore(flags);
1260
1261 return ret;
1262}
1263
1264
1265
1266
1267
1268static noinline int
1269check_noncircular(struct lock_list *root, struct lock_class *target,
1270 struct lock_list **target_entry)
1271{
1272 int result;
1273
1274 debug_atomic_inc(nr_cyclic_checks);
1275
1276 result = __bfs_forwards(root, target, class_equal, target_entry);
1277
1278 return result;
1279}
1280
1281#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
1282
1283
1284
1285
1286
1287
1288static inline int usage_match(struct lock_list *entry, void *bit)
1289{
1290 return entry->class->usage_mask & (1 << (enum lock_usage_bit)bit);
1291}
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305static int
1306find_usage_forwards(struct lock_list *root, enum lock_usage_bit bit,
1307 struct lock_list **target_entry)
1308{
1309 int result;
1310
1311 debug_atomic_inc(nr_find_usage_forwards_checks);
1312
1313 result = __bfs_forwards(root, (void *)bit, usage_match, target_entry);
1314
1315 return result;
1316}
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328static int
1329find_usage_backwards(struct lock_list *root, enum lock_usage_bit bit,
1330 struct lock_list **target_entry)
1331{
1332 int result;
1333
1334 debug_atomic_inc(nr_find_usage_backwards_checks);
1335
1336 result = __bfs_backwards(root, (void *)bit, usage_match, target_entry);
1337
1338 return result;
1339}
1340
1341static void print_lock_class_header(struct lock_class *class, int depth)
1342{
1343 int bit;
1344
1345 printk("%*s->", depth, "");
1346 print_lock_name(class);
1347 printk(" ops: %lu", class->ops);
1348 printk(" {\n");
1349
1350 for (bit = 0; bit < LOCK_USAGE_STATES; bit++) {
1351 if (class->usage_mask & (1 << bit)) {
1352 int len = depth;
1353
1354 len += printk("%*s %s", depth, "", usage_str[bit]);
1355 len += printk(" at:\n");
1356 print_stack_trace(class->usage_traces + bit, len);
1357 }
1358 }
1359 printk("%*s }\n", depth, "");
1360
1361 printk("%*s ... key at: ",depth,"");
1362 print_ip_sym((unsigned long)class->key);
1363}
1364
1365
1366
1367
1368static void __used
1369print_shortest_lock_dependencies(struct lock_list *leaf,
1370 struct lock_list *root)
1371{
1372 struct lock_list *entry = leaf;
1373 int depth;
1374
1375
1376 depth = get_lock_depth(leaf);
1377
1378 do {
1379 print_lock_class_header(entry->class, depth);
1380 printk("%*s ... acquired at:\n", depth, "");
1381 print_stack_trace(&entry->trace, 2);
1382 printk("\n");
1383
1384 if (depth == 0 && (entry != root)) {
1385 printk("lockdep:%s bad path found in chain graph\n", __func__);
1386 break;
1387 }
1388
1389 entry = get_lock_parent(entry);
1390 depth--;
1391 } while (entry && (depth >= 0));
1392
1393 return;
1394}
1395
1396static void
1397print_irq_lock_scenario(struct lock_list *safe_entry,
1398 struct lock_list *unsafe_entry,
1399 struct lock_class *prev_class,
1400 struct lock_class *next_class)
1401{
1402 struct lock_class *safe_class = safe_entry->class;
1403 struct lock_class *unsafe_class = unsafe_entry->class;
1404 struct lock_class *middle_class = prev_class;
1405
1406 if (middle_class == safe_class)
1407 middle_class = next_class;
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422 if (middle_class != unsafe_class) {
1423 printk("Chain exists of:\n ");
1424 __print_lock_name(safe_class);
1425 printk(" --> ");
1426 __print_lock_name(middle_class);
1427 printk(" --> ");
1428 __print_lock_name(unsafe_class);
1429 printk("\n\n");
1430 }
1431
1432 printk(" Possible interrupt unsafe locking scenario:\n\n");
1433 printk(" CPU0 CPU1\n");
1434 printk(" ---- ----\n");
1435 printk(" lock(");
1436 __print_lock_name(unsafe_class);
1437 printk(");\n");
1438 printk(" local_irq_disable();\n");
1439 printk(" lock(");
1440 __print_lock_name(safe_class);
1441 printk(");\n");
1442 printk(" lock(");
1443 __print_lock_name(middle_class);
1444 printk(");\n");
1445 printk(" <Interrupt>\n");
1446 printk(" lock(");
1447 __print_lock_name(safe_class);
1448 printk(");\n");
1449 printk("\n *** DEADLOCK ***\n\n");
1450}
1451
1452static int
1453print_bad_irq_dependency(struct task_struct *curr,
1454 struct lock_list *prev_root,
1455 struct lock_list *next_root,
1456 struct lock_list *backwards_entry,
1457 struct lock_list *forwards_entry,
1458 struct held_lock *prev,
1459 struct held_lock *next,
1460 enum lock_usage_bit bit1,
1461 enum lock_usage_bit bit2,
1462 const char *irqclass)
1463{
1464 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1465 return 0;
1466
1467 printk("\n======================================================\n");
1468 printk( "[ INFO: %s-safe -> %s-unsafe lock order detected ]\n",
1469 irqclass, irqclass);
1470 print_kernel_version();
1471 printk( "------------------------------------------------------\n");
1472 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] is trying to acquire:\n",
1473 curr->comm, task_pid_nr(curr),
1474 curr->hardirq_context, hardirq_count() >> HARDIRQ_SHIFT,
1475 curr->softirq_context, softirq_count() >> SOFTIRQ_SHIFT,
1476 curr->hardirqs_enabled,
1477 curr->softirqs_enabled);
1478 print_lock(next);
1479
1480 printk("\nand this task is already holding:\n");
1481 print_lock(prev);
1482 printk("which would create a new lock dependency:\n");
1483 print_lock_name(hlock_class(prev));
1484 printk(" ->");
1485 print_lock_name(hlock_class(next));
1486 printk("\n");
1487
1488 printk("\nbut this new dependency connects a %s-irq-safe lock:\n",
1489 irqclass);
1490 print_lock_name(backwards_entry->class);
1491 printk("\n... which became %s-irq-safe at:\n", irqclass);
1492
1493 print_stack_trace(backwards_entry->class->usage_traces + bit1, 1);
1494
1495 printk("\nto a %s-irq-unsafe lock:\n", irqclass);
1496 print_lock_name(forwards_entry->class);
1497 printk("\n... which became %s-irq-unsafe at:\n", irqclass);
1498 printk("...");
1499
1500 print_stack_trace(forwards_entry->class->usage_traces + bit2, 1);
1501
1502 printk("\nother info that might help us debug this:\n\n");
1503 print_irq_lock_scenario(backwards_entry, forwards_entry,
1504 hlock_class(prev), hlock_class(next));
1505
1506 lockdep_print_held_locks(curr);
1507
1508 printk("\nthe dependencies between %s-irq-safe lock", irqclass);
1509 printk(" and the holding lock:\n");
1510 if (!save_trace(&prev_root->trace))
1511 return 0;
1512 print_shortest_lock_dependencies(backwards_entry, prev_root);
1513
1514 printk("\nthe dependencies between the lock to be acquired");
1515 printk(" and %s-irq-unsafe lock:\n", irqclass);
1516 if (!save_trace(&next_root->trace))
1517 return 0;
1518 print_shortest_lock_dependencies(forwards_entry, next_root);
1519
1520 printk("\nstack backtrace:\n");
1521 dump_stack();
1522
1523 return 0;
1524}
1525
1526static int
1527check_usage(struct task_struct *curr, struct held_lock *prev,
1528 struct held_lock *next, enum lock_usage_bit bit_backwards,
1529 enum lock_usage_bit bit_forwards, const char *irqclass)
1530{
1531 int ret;
1532 struct lock_list this, that;
1533 struct lock_list *uninitialized_var(target_entry);
1534 struct lock_list *uninitialized_var(target_entry1);
1535
1536 this.parent = NULL;
1537
1538 this.class = hlock_class(prev);
1539 ret = find_usage_backwards(&this, bit_backwards, &target_entry);
1540 if (ret < 0)
1541 return print_bfs_bug(ret);
1542 if (ret == 1)
1543 return ret;
1544
1545 that.parent = NULL;
1546 that.class = hlock_class(next);
1547 ret = find_usage_forwards(&that, bit_forwards, &target_entry1);
1548 if (ret < 0)
1549 return print_bfs_bug(ret);
1550 if (ret == 1)
1551 return ret;
1552
1553 return print_bad_irq_dependency(curr, &this, &that,
1554 target_entry, target_entry1,
1555 prev, next,
1556 bit_backwards, bit_forwards, irqclass);
1557}
1558
1559static const char *state_names[] = {
1560#define LOCKDEP_STATE(__STATE) \
1561 __stringify(__STATE),
1562#include "lockdep_states.h"
1563#undef LOCKDEP_STATE
1564};
1565
1566static const char *state_rnames[] = {
1567#define LOCKDEP_STATE(__STATE) \
1568 __stringify(__STATE)"-READ",
1569#include "lockdep_states.h"
1570#undef LOCKDEP_STATE
1571};
1572
1573static inline const char *state_name(enum lock_usage_bit bit)
1574{
1575 return (bit & 1) ? state_rnames[bit >> 2] : state_names[bit >> 2];
1576}
1577
1578static int exclusive_bit(int new_bit)
1579{
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591 int state = new_bit & ~3;
1592 int dir = new_bit & 2;
1593
1594
1595
1596
1597 return state | (dir ^ 2);
1598}
1599
1600static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
1601 struct held_lock *next, enum lock_usage_bit bit)
1602{
1603
1604
1605
1606
1607
1608
1609 if (!check_usage(curr, prev, next, bit,
1610 exclusive_bit(bit), state_name(bit)))
1611 return 0;
1612
1613 bit++;
1614
1615
1616
1617
1618
1619
1620
1621 if (!check_usage(curr, prev, next, bit,
1622 exclusive_bit(bit), state_name(bit)))
1623 return 0;
1624
1625 return 1;
1626}
1627
1628static int
1629check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1630 struct held_lock *next)
1631{
1632#define LOCKDEP_STATE(__STATE) \
1633 if (!check_irq_usage(curr, prev, next, LOCK_USED_IN_##__STATE)) \
1634 return 0;
1635#include "lockdep_states.h"
1636#undef LOCKDEP_STATE
1637
1638 return 1;
1639}
1640
1641static void inc_chains(void)
1642{
1643 if (current->hardirq_context)
1644 nr_hardirq_chains++;
1645 else {
1646 if (current->softirq_context)
1647 nr_softirq_chains++;
1648 else
1649 nr_process_chains++;
1650 }
1651}
1652
1653#else
1654
1655static inline int
1656check_prev_add_irq(struct task_struct *curr, struct held_lock *prev,
1657 struct held_lock *next)
1658{
1659 return 1;
1660}
1661
1662static inline void inc_chains(void)
1663{
1664 nr_process_chains++;
1665}
1666
1667#endif
1668
1669static void
1670print_deadlock_scenario(struct held_lock *nxt,
1671 struct held_lock *prv)
1672{
1673 struct lock_class *next = hlock_class(nxt);
1674 struct lock_class *prev = hlock_class(prv);
1675
1676 printk(" Possible unsafe locking scenario:\n\n");
1677 printk(" CPU0\n");
1678 printk(" ----\n");
1679 printk(" lock(");
1680 __print_lock_name(prev);
1681 printk(");\n");
1682 printk(" lock(");
1683 __print_lock_name(next);
1684 printk(");\n");
1685 printk("\n *** DEADLOCK ***\n\n");
1686 printk(" May be due to missing lock nesting notation\n\n");
1687}
1688
1689static int
1690print_deadlock_bug(struct task_struct *curr, struct held_lock *prev,
1691 struct held_lock *next)
1692{
1693 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
1694 return 0;
1695
1696 printk("\n=============================================\n");
1697 printk( "[ INFO: possible recursive locking detected ]\n");
1698 print_kernel_version();
1699 printk( "---------------------------------------------\n");
1700 printk("%s/%d is trying to acquire lock:\n",
1701 curr->comm, task_pid_nr(curr));
1702 print_lock(next);
1703 printk("\nbut task is already holding lock:\n");
1704 print_lock(prev);
1705
1706 printk("\nother info that might help us debug this:\n");
1707 print_deadlock_scenario(next, prev);
1708 lockdep_print_held_locks(curr);
1709
1710 printk("\nstack backtrace:\n");
1711 dump_stack();
1712
1713 return 0;
1714}
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724static int
1725check_deadlock(struct task_struct *curr, struct held_lock *next,
1726 struct lockdep_map *next_instance, int read)
1727{
1728 struct held_lock *prev;
1729 struct held_lock *nest = NULL;
1730 int i;
1731
1732 for (i = 0; i < curr->lockdep_depth; i++) {
1733 prev = curr->held_locks + i;
1734
1735 if (prev->instance == next->nest_lock)
1736 nest = prev;
1737
1738 if (hlock_class(prev) != hlock_class(next))
1739 continue;
1740
1741
1742
1743
1744
1745 if ((read == 2) && prev->read)
1746 return 2;
1747
1748
1749
1750
1751
1752 if (nest)
1753 return 2;
1754
1755 return print_deadlock_bug(curr, prev, next);
1756 }
1757 return 1;
1758}
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782static int
1783check_prev_add(struct task_struct *curr, struct held_lock *prev,
1784 struct held_lock *next, int distance, int trylock_loop)
1785{
1786 struct lock_list *entry;
1787 int ret;
1788 struct lock_list this;
1789 struct lock_list *uninitialized_var(target_entry);
1790
1791
1792
1793
1794
1795
1796
1797 static struct stack_trace trace;
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808 this.class = hlock_class(next);
1809 this.parent = NULL;
1810 ret = check_noncircular(&this, hlock_class(prev), &target_entry);
1811 if (unlikely(!ret))
1812 return print_circular_bug(&this, target_entry, next, prev);
1813 else if (unlikely(ret < 0))
1814 return print_bfs_bug(ret);
1815
1816 if (!check_prev_add_irq(curr, prev, next))
1817 return 0;
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827 if (next->read == 2 || prev->read == 2)
1828 return 1;
1829
1830
1831
1832
1833
1834
1835
1836
1837 list_for_each_entry(entry, &hlock_class(prev)->locks_after, entry) {
1838 if (entry->class == hlock_class(next)) {
1839 if (distance == 1)
1840 entry->distance = 1;
1841 return 2;
1842 }
1843 }
1844
1845 if (!trylock_loop && !save_trace(&trace))
1846 return 0;
1847
1848
1849
1850
1851
1852 ret = add_lock_to_list(hlock_class(prev), hlock_class(next),
1853 &hlock_class(prev)->locks_after,
1854 next->acquire_ip, distance, &trace);
1855
1856 if (!ret)
1857 return 0;
1858
1859 ret = add_lock_to_list(hlock_class(next), hlock_class(prev),
1860 &hlock_class(next)->locks_before,
1861 next->acquire_ip, distance, &trace);
1862 if (!ret)
1863 return 0;
1864
1865
1866
1867
1868 if (verbose(hlock_class(prev)) || verbose(hlock_class(next))) {
1869 graph_unlock();
1870 printk("\n new dependency: ");
1871 print_lock_name(hlock_class(prev));
1872 printk(" => ");
1873 print_lock_name(hlock_class(next));
1874 printk("\n");
1875 dump_stack();
1876 return graph_lock();
1877 }
1878 return 1;
1879}
1880
1881
1882
1883
1884
1885
1886
1887static int
1888check_prevs_add(struct task_struct *curr, struct held_lock *next)
1889{
1890 int depth = curr->lockdep_depth;
1891 int trylock_loop = 0;
1892 struct held_lock *hlock;
1893
1894
1895
1896
1897
1898
1899 if (!depth)
1900 goto out_bug;
1901
1902
1903
1904
1905 if (curr->held_locks[depth].irq_context !=
1906 curr->held_locks[depth-1].irq_context)
1907 goto out_bug;
1908
1909 for (;;) {
1910 int distance = curr->lockdep_depth - depth + 1;
1911 hlock = curr->held_locks + depth-1;
1912
1913
1914
1915
1916 if (hlock->read != 2) {
1917 if (!check_prev_add(curr, hlock, next,
1918 distance, trylock_loop))
1919 return 0;
1920
1921
1922
1923
1924
1925
1926 if (!hlock->trylock)
1927 break;
1928 }
1929 depth--;
1930
1931
1932
1933 if (!depth)
1934 break;
1935
1936
1937
1938 if (curr->held_locks[depth].irq_context !=
1939 curr->held_locks[depth-1].irq_context)
1940 break;
1941 trylock_loop = 1;
1942 }
1943 return 1;
1944out_bug:
1945 if (!debug_locks_off_graph_unlock())
1946 return 0;
1947
1948 WARN_ON(1);
1949
1950 return 0;
1951}
1952
1953unsigned long nr_lock_chains;
1954struct lock_chain lock_chains[MAX_LOCKDEP_CHAINS];
1955int nr_chain_hlocks;
1956static u16 chain_hlocks[MAX_LOCKDEP_CHAIN_HLOCKS];
1957
1958struct lock_class *lock_chain_get_class(struct lock_chain *chain, int i)
1959{
1960 return lock_classes + chain_hlocks[chain->base + i];
1961}
1962
1963
1964
1965
1966
1967
1968
1969static inline int lookup_chain_cache(struct task_struct *curr,
1970 struct held_lock *hlock,
1971 u64 chain_key)
1972{
1973 struct lock_class *class = hlock_class(hlock);
1974 struct list_head *hash_head = chainhashentry(chain_key);
1975 struct lock_chain *chain;
1976 struct held_lock *hlock_curr, *hlock_next;
1977 int i, j;
1978
1979 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
1980 return 0;
1981
1982
1983
1984
1985 list_for_each_entry(chain, hash_head, entry) {
1986 if (chain->chain_key == chain_key) {
1987cache_hit:
1988 debug_atomic_inc(chain_lookup_hits);
1989 if (very_verbose(class))
1990 printk("\nhash chain already cached, key: "
1991 "%016Lx tail class: [%p] %s\n",
1992 (unsigned long long)chain_key,
1993 class->key, class->name);
1994 return 0;
1995 }
1996 }
1997 if (very_verbose(class))
1998 printk("\nnew hash chain, key: %016Lx tail class: [%p] %s\n",
1999 (unsigned long long)chain_key, class->key, class->name);
2000
2001
2002
2003
2004 if (!graph_lock())
2005 return 0;
2006
2007
2008
2009 list_for_each_entry(chain, hash_head, entry) {
2010 if (chain->chain_key == chain_key) {
2011 graph_unlock();
2012 goto cache_hit;
2013 }
2014 }
2015 if (unlikely(nr_lock_chains >= MAX_LOCKDEP_CHAINS)) {
2016 if (!debug_locks_off_graph_unlock())
2017 return 0;
2018
2019 printk("BUG: MAX_LOCKDEP_CHAINS too low!\n");
2020 printk("turning off the locking correctness validator.\n");
2021 dump_stack();
2022 return 0;
2023 }
2024 chain = lock_chains + nr_lock_chains++;
2025 chain->chain_key = chain_key;
2026 chain->irq_context = hlock->irq_context;
2027
2028 hlock_next = hlock;
2029 for (i = curr->lockdep_depth - 1; i >= 0; i--) {
2030 hlock_curr = curr->held_locks + i;
2031 if (hlock_curr->irq_context != hlock_next->irq_context)
2032 break;
2033 hlock_next = hlock;
2034 }
2035 i++;
2036 chain->depth = curr->lockdep_depth + 1 - i;
2037 if (likely(nr_chain_hlocks + chain->depth <= MAX_LOCKDEP_CHAIN_HLOCKS)) {
2038 chain->base = nr_chain_hlocks;
2039 nr_chain_hlocks += chain->depth;
2040 for (j = 0; j < chain->depth - 1; j++, i++) {
2041 int lock_id = curr->held_locks[i].class_idx - 1;
2042 chain_hlocks[chain->base + j] = lock_id;
2043 }
2044 chain_hlocks[chain->base + j] = class - lock_classes;
2045 }
2046 list_add_tail_rcu(&chain->entry, hash_head);
2047 debug_atomic_inc(chain_lookup_misses);
2048 inc_chains();
2049
2050 return 1;
2051}
2052
2053static int validate_chain(struct task_struct *curr, struct lockdep_map *lock,
2054 struct held_lock *hlock, int chain_head, u64 chain_key)
2055{
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066 if (!hlock->trylock && (hlock->check == 2) &&
2067 lookup_chain_cache(curr, hlock, chain_key)) {
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080 int ret = check_deadlock(curr, hlock, lock, hlock->read);
2081
2082 if (!ret)
2083 return 0;
2084
2085
2086
2087
2088
2089 if (ret == 2)
2090 hlock->read = 2;
2091
2092
2093
2094
2095 if (!chain_head && ret != 2)
2096 if (!check_prevs_add(curr, hlock))
2097 return 0;
2098 graph_unlock();
2099 } else
2100
2101 if (unlikely(!debug_locks))
2102 return 0;
2103
2104 return 1;
2105}
2106#else
2107static inline int validate_chain(struct task_struct *curr,
2108 struct lockdep_map *lock, struct held_lock *hlock,
2109 int chain_head, u64 chain_key)
2110{
2111 return 1;
2112}
2113#endif
2114
2115
2116
2117
2118
2119static void check_chain_key(struct task_struct *curr)
2120{
2121#ifdef CONFIG_DEBUG_LOCKDEP
2122 struct held_lock *hlock, *prev_hlock = NULL;
2123 unsigned int i, id;
2124 u64 chain_key = 0;
2125
2126 for (i = 0; i < curr->lockdep_depth; i++) {
2127 hlock = curr->held_locks + i;
2128 if (chain_key != hlock->prev_chain_key) {
2129 debug_locks_off();
2130 WARN(1, "hm#1, depth: %u [%u], %016Lx != %016Lx\n",
2131 curr->lockdep_depth, i,
2132 (unsigned long long)chain_key,
2133 (unsigned long long)hlock->prev_chain_key);
2134 return;
2135 }
2136 id = hlock->class_idx - 1;
2137 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
2138 return;
2139
2140 if (prev_hlock && (prev_hlock->irq_context !=
2141 hlock->irq_context))
2142 chain_key = 0;
2143 chain_key = iterate_chain_key(chain_key, id);
2144 prev_hlock = hlock;
2145 }
2146 if (chain_key != curr->curr_chain_key) {
2147 debug_locks_off();
2148 WARN(1, "hm#2, depth: %u [%u], %016Lx != %016Lx\n",
2149 curr->lockdep_depth, i,
2150 (unsigned long long)chain_key,
2151 (unsigned long long)curr->curr_chain_key);
2152 }
2153#endif
2154}
2155
2156static void
2157print_usage_bug_scenario(struct held_lock *lock)
2158{
2159 struct lock_class *class = hlock_class(lock);
2160
2161 printk(" Possible unsafe locking scenario:\n\n");
2162 printk(" CPU0\n");
2163 printk(" ----\n");
2164 printk(" lock(");
2165 __print_lock_name(class);
2166 printk(");\n");
2167 printk(" <Interrupt>\n");
2168 printk(" lock(");
2169 __print_lock_name(class);
2170 printk(");\n");
2171 printk("\n *** DEADLOCK ***\n\n");
2172}
2173
2174static int
2175print_usage_bug(struct task_struct *curr, struct held_lock *this,
2176 enum lock_usage_bit prev_bit, enum lock_usage_bit new_bit)
2177{
2178 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2179 return 0;
2180
2181 printk("\n=================================\n");
2182 printk( "[ INFO: inconsistent lock state ]\n");
2183 print_kernel_version();
2184 printk( "---------------------------------\n");
2185
2186 printk("inconsistent {%s} -> {%s} usage.\n",
2187 usage_str[prev_bit], usage_str[new_bit]);
2188
2189 printk("%s/%d [HC%u[%lu]:SC%u[%lu]:HE%u:SE%u] takes:\n",
2190 curr->comm, task_pid_nr(curr),
2191 trace_hardirq_context(curr), hardirq_count() >> HARDIRQ_SHIFT,
2192 trace_softirq_context(curr), softirq_count() >> SOFTIRQ_SHIFT,
2193 trace_hardirqs_enabled(curr),
2194 trace_softirqs_enabled(curr));
2195 print_lock(this);
2196
2197 printk("{%s} state was registered at:\n", usage_str[prev_bit]);
2198 print_stack_trace(hlock_class(this)->usage_traces + prev_bit, 1);
2199
2200 print_irqtrace_events(curr);
2201 printk("\nother info that might help us debug this:\n");
2202 print_usage_bug_scenario(this);
2203
2204 lockdep_print_held_locks(curr);
2205
2206 printk("\nstack backtrace:\n");
2207 dump_stack();
2208
2209 return 0;
2210}
2211
2212
2213
2214
2215static inline int
2216valid_state(struct task_struct *curr, struct held_lock *this,
2217 enum lock_usage_bit new_bit, enum lock_usage_bit bad_bit)
2218{
2219 if (unlikely(hlock_class(this)->usage_mask & (1 << bad_bit)))
2220 return print_usage_bug(curr, this, bad_bit, new_bit);
2221 return 1;
2222}
2223
2224static int mark_lock(struct task_struct *curr, struct held_lock *this,
2225 enum lock_usage_bit new_bit);
2226
2227#if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
2228
2229
2230
2231
2232static int
2233print_irq_inversion_bug(struct task_struct *curr,
2234 struct lock_list *root, struct lock_list *other,
2235 struct held_lock *this, int forwards,
2236 const char *irqclass)
2237{
2238 struct lock_list *entry = other;
2239 struct lock_list *middle = NULL;
2240 int depth;
2241
2242 if (!debug_locks_off_graph_unlock() || debug_locks_silent)
2243 return 0;
2244
2245 printk("\n=========================================================\n");
2246 printk( "[ INFO: possible irq lock inversion dependency detected ]\n");
2247 print_kernel_version();
2248 printk( "---------------------------------------------------------\n");
2249 printk("%s/%d just changed the state of lock:\n",
2250 curr->comm, task_pid_nr(curr));
2251 print_lock(this);
2252 if (forwards)
2253 printk("but this lock took another, %s-unsafe lock in the past:\n", irqclass);
2254 else
2255 printk("but this lock was taken by another, %s-safe lock in the past:\n", irqclass);
2256 print_lock_name(other->class);
2257 printk("\n\nand interrupts could create inverse lock ordering between them.\n\n");
2258
2259 printk("\nother info that might help us debug this:\n");
2260
2261
2262 depth = get_lock_depth(other);
2263 do {
2264 if (depth == 0 && (entry != root)) {
2265 printk("lockdep:%s bad path found in chain graph\n", __func__);
2266 break;
2267 }
2268 middle = entry;
2269 entry = get_lock_parent(entry);
2270 depth--;
2271 } while (entry && entry != root && (depth >= 0));
2272 if (forwards)
2273 print_irq_lock_scenario(root, other,
2274 middle ? middle->class : root->class, other->class);
2275 else
2276 print_irq_lock_scenario(other, root,
2277 middle ? middle->class : other->class, root->class);
2278
2279 lockdep_print_held_locks(curr);
2280
2281 printk("\nthe shortest dependencies between 2nd lock and 1st lock:\n");
2282 if (!save_trace(&root->trace))
2283 return 0;
2284 print_shortest_lock_dependencies(other, root);
2285
2286 printk("\nstack backtrace:\n");
2287 dump_stack();
2288
2289 return 0;
2290}
2291
2292
2293
2294
2295
2296static int
2297check_usage_forwards(struct task_struct *curr, struct held_lock *this,
2298 enum lock_usage_bit bit, const char *irqclass)
2299{
2300 int ret;
2301 struct lock_list root;
2302 struct lock_list *uninitialized_var(target_entry);
2303
2304 root.parent = NULL;
2305 root.class = hlock_class(this);
2306 ret = find_usage_forwards(&root, bit, &target_entry);
2307 if (ret < 0)
2308 return print_bfs_bug(ret);
2309 if (ret == 1)
2310 return ret;
2311
2312 return print_irq_inversion_bug(curr, &root, target_entry,
2313 this, 1, irqclass);
2314}
2315
2316
2317
2318
2319
2320static int
2321check_usage_backwards(struct task_struct *curr, struct held_lock *this,
2322 enum lock_usage_bit bit, const char *irqclass)
2323{
2324 int ret;
2325 struct lock_list root;
2326 struct lock_list *uninitialized_var(target_entry);
2327
2328 root.parent = NULL;
2329 root.class = hlock_class(this);
2330 ret = find_usage_backwards(&root, bit, &target_entry);
2331 if (ret < 0)
2332 return print_bfs_bug(ret);
2333 if (ret == 1)
2334 return ret;
2335
2336 return print_irq_inversion_bug(curr, &root, target_entry,
2337 this, 0, irqclass);
2338}
2339
2340void print_irqtrace_events(struct task_struct *curr)
2341{
2342 printk("irq event stamp: %u\n", curr->irq_events);
2343 printk("hardirqs last enabled at (%u): ", curr->hardirq_enable_event);
2344 print_ip_sym(curr->hardirq_enable_ip);
2345 printk("hardirqs last disabled at (%u): ", curr->hardirq_disable_event);
2346 print_ip_sym(curr->hardirq_disable_ip);
2347 printk("softirqs last enabled at (%u): ", curr->softirq_enable_event);
2348 print_ip_sym(curr->softirq_enable_ip);
2349 printk("softirqs last disabled at (%u): ", curr->softirq_disable_event);
2350 print_ip_sym(curr->softirq_disable_ip);
2351}
2352
2353static int HARDIRQ_verbose(struct lock_class *class)
2354{
2355#if HARDIRQ_VERBOSE
2356 return class_filter(class);
2357#endif
2358 return 0;
2359}
2360
2361static int SOFTIRQ_verbose(struct lock_class *class)
2362{
2363#if SOFTIRQ_VERBOSE
2364 return class_filter(class);
2365#endif
2366 return 0;
2367}
2368
2369static int RECLAIM_FS_verbose(struct lock_class *class)
2370{
2371#if RECLAIM_VERBOSE
2372 return class_filter(class);
2373#endif
2374 return 0;
2375}
2376
2377#define STRICT_READ_CHECKS 1
2378
2379static int (*state_verbose_f[])(struct lock_class *class) = {
2380#define LOCKDEP_STATE(__STATE) \
2381 __STATE##_verbose,
2382#include "lockdep_states.h"
2383#undef LOCKDEP_STATE
2384};
2385
2386static inline int state_verbose(enum lock_usage_bit bit,
2387 struct lock_class *class)
2388{
2389 return state_verbose_f[bit >> 2](class);
2390}
2391
2392typedef int (*check_usage_f)(struct task_struct *, struct held_lock *,
2393 enum lock_usage_bit bit, const char *name);
2394
2395static int
2396mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2397 enum lock_usage_bit new_bit)
2398{
2399 int excl_bit = exclusive_bit(new_bit);
2400 int read = new_bit & 1;
2401 int dir = new_bit & 2;
2402
2403
2404
2405
2406
2407
2408
2409
2410 check_usage_f usage = dir ?
2411 check_usage_backwards : check_usage_forwards;
2412
2413
2414
2415
2416
2417 if (!valid_state(curr, this, new_bit, excl_bit))
2418 return 0;
2419
2420
2421
2422
2423
2424 if ((!read || !dir || STRICT_READ_CHECKS) &&
2425 !usage(curr, this, excl_bit, state_name(new_bit & ~1)))
2426 return 0;
2427
2428
2429
2430
2431 if (!read) {
2432 if (!valid_state(curr, this, new_bit, excl_bit + 1))
2433 return 0;
2434
2435 if (STRICT_READ_CHECKS &&
2436 !usage(curr, this, excl_bit + 1,
2437 state_name(new_bit + 1)))
2438 return 0;
2439 }
2440
2441 if (state_verbose(new_bit, hlock_class(this)))
2442 return 2;
2443
2444 return 1;
2445}
2446
2447enum mark_type {
2448#define LOCKDEP_STATE(__STATE) __STATE,
2449#include "lockdep_states.h"
2450#undef LOCKDEP_STATE
2451};
2452
2453
2454
2455
2456static int
2457mark_held_locks(struct task_struct *curr, enum mark_type mark)
2458{
2459 enum lock_usage_bit usage_bit;
2460 struct held_lock *hlock;
2461 int i;
2462
2463 for (i = 0; i < curr->lockdep_depth; i++) {
2464 hlock = curr->held_locks + i;
2465
2466 usage_bit = 2 + (mark << 2);
2467 if (hlock->read)
2468 usage_bit += 1;
2469
2470 BUG_ON(usage_bit >= LOCK_USAGE_STATES);
2471
2472 if (hlock_class(hlock)->key == __lockdep_no_validate__.subkeys)
2473 continue;
2474
2475 if (!mark_lock(curr, hlock, usage_bit))
2476 return 0;
2477 }
2478
2479 return 1;
2480}
2481
2482
2483
2484
2485static void __trace_hardirqs_on_caller(unsigned long ip)
2486{
2487 struct task_struct *curr = current;
2488
2489
2490 curr->hardirqs_enabled = 1;
2491
2492
2493
2494
2495
2496 if (!mark_held_locks(curr, HARDIRQ))
2497 return;
2498
2499
2500
2501
2502
2503 if (curr->softirqs_enabled)
2504 if (!mark_held_locks(curr, SOFTIRQ))
2505 return;
2506
2507 curr->hardirq_enable_ip = ip;
2508 curr->hardirq_enable_event = ++curr->irq_events;
2509 debug_atomic_inc(hardirqs_on_events);
2510}
2511
2512void trace_hardirqs_on_caller(unsigned long ip)
2513{
2514 time_hardirqs_on(CALLER_ADDR0, ip);
2515
2516 if (unlikely(!debug_locks || current->lockdep_recursion))
2517 return;
2518
2519 if (unlikely(current->hardirqs_enabled)) {
2520
2521
2522
2523
2524
2525 __debug_atomic_inc(redundant_hardirqs_on);
2526 return;
2527 }
2528
2529 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2530 return;
2531
2532 if (DEBUG_LOCKS_WARN_ON(unlikely(early_boot_irqs_disabled)))
2533 return;
2534
2535 if (DEBUG_LOCKS_WARN_ON(current->hardirq_context))
2536 return;
2537
2538 current->lockdep_recursion = 1;
2539 __trace_hardirqs_on_caller(ip);
2540 current->lockdep_recursion = 0;
2541}
2542EXPORT_SYMBOL(trace_hardirqs_on_caller);
2543
2544void trace_hardirqs_on(void)
2545{
2546 trace_hardirqs_on_caller(CALLER_ADDR0);
2547}
2548EXPORT_SYMBOL(trace_hardirqs_on);
2549
2550
2551
2552
2553void trace_hardirqs_off_caller(unsigned long ip)
2554{
2555 struct task_struct *curr = current;
2556
2557 time_hardirqs_off(CALLER_ADDR0, ip);
2558
2559 if (unlikely(!debug_locks || current->lockdep_recursion))
2560 return;
2561
2562 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2563 return;
2564
2565 if (curr->hardirqs_enabled) {
2566
2567
2568
2569 curr->hardirqs_enabled = 0;
2570 curr->hardirq_disable_ip = ip;
2571 curr->hardirq_disable_event = ++curr->irq_events;
2572 debug_atomic_inc(hardirqs_off_events);
2573 } else
2574 debug_atomic_inc(redundant_hardirqs_off);
2575}
2576EXPORT_SYMBOL(trace_hardirqs_off_caller);
2577
2578void trace_hardirqs_off(void)
2579{
2580 trace_hardirqs_off_caller(CALLER_ADDR0);
2581}
2582EXPORT_SYMBOL(trace_hardirqs_off);
2583
2584
2585
2586
2587void trace_softirqs_on(unsigned long ip)
2588{
2589 struct task_struct *curr = current;
2590
2591 if (unlikely(!debug_locks || current->lockdep_recursion))
2592 return;
2593
2594 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2595 return;
2596
2597 if (curr->softirqs_enabled) {
2598 debug_atomic_inc(redundant_softirqs_on);
2599 return;
2600 }
2601
2602 current->lockdep_recursion = 1;
2603
2604
2605
2606 curr->softirqs_enabled = 1;
2607 curr->softirq_enable_ip = ip;
2608 curr->softirq_enable_event = ++curr->irq_events;
2609 debug_atomic_inc(softirqs_on_events);
2610
2611
2612
2613
2614
2615 if (curr->hardirqs_enabled)
2616 mark_held_locks(curr, SOFTIRQ);
2617 current->lockdep_recursion = 0;
2618}
2619
2620
2621
2622
2623void trace_softirqs_off(unsigned long ip)
2624{
2625 struct task_struct *curr = current;
2626
2627 if (unlikely(!debug_locks || current->lockdep_recursion))
2628 return;
2629
2630 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2631 return;
2632
2633 if (curr->softirqs_enabled) {
2634
2635
2636
2637 curr->softirqs_enabled = 0;
2638 curr->softirq_disable_ip = ip;
2639 curr->softirq_disable_event = ++curr->irq_events;
2640 debug_atomic_inc(softirqs_off_events);
2641 DEBUG_LOCKS_WARN_ON(!softirq_count());
2642 } else
2643 debug_atomic_inc(redundant_softirqs_off);
2644}
2645
2646static void __lockdep_trace_alloc(gfp_t gfp_mask, unsigned long flags)
2647{
2648 struct task_struct *curr = current;
2649
2650 if (unlikely(!debug_locks))
2651 return;
2652
2653
2654 if (!(gfp_mask & __GFP_WAIT))
2655 return;
2656
2657
2658 if ((curr->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
2659 return;
2660
2661
2662 if (!(gfp_mask & __GFP_FS))
2663 return;
2664
2665 if (DEBUG_LOCKS_WARN_ON(irqs_disabled_flags(flags)))
2666 return;
2667
2668 mark_held_locks(curr, RECLAIM_FS);
2669}
2670
2671static void check_flags(unsigned long flags);
2672
2673void lockdep_trace_alloc(gfp_t gfp_mask)
2674{
2675 unsigned long flags;
2676
2677 if (unlikely(current->lockdep_recursion))
2678 return;
2679
2680 raw_local_irq_save(flags);
2681 check_flags(flags);
2682 current->lockdep_recursion = 1;
2683 __lockdep_trace_alloc(gfp_mask, flags);
2684 current->lockdep_recursion = 0;
2685 raw_local_irq_restore(flags);
2686}
2687
2688static int mark_irqflags(struct task_struct *curr, struct held_lock *hlock)
2689{
2690
2691
2692
2693
2694 if (!hlock->trylock) {
2695 if (hlock->read) {
2696 if (curr->hardirq_context)
2697 if (!mark_lock(curr, hlock,
2698 LOCK_USED_IN_HARDIRQ_READ))
2699 return 0;
2700 if (curr->softirq_context)
2701 if (!mark_lock(curr, hlock,
2702 LOCK_USED_IN_SOFTIRQ_READ))
2703 return 0;
2704 } else {
2705 if (curr->hardirq_context)
2706 if (!mark_lock(curr, hlock, LOCK_USED_IN_HARDIRQ))
2707 return 0;
2708 if (curr->softirq_context)
2709 if (!mark_lock(curr, hlock, LOCK_USED_IN_SOFTIRQ))
2710 return 0;
2711 }
2712 }
2713 if (!hlock->hardirqs_off) {
2714 if (hlock->read) {
2715 if (!mark_lock(curr, hlock,
2716 LOCK_ENABLED_HARDIRQ_READ))
2717 return 0;
2718 if (curr->softirqs_enabled)
2719 if (!mark_lock(curr, hlock,
2720 LOCK_ENABLED_SOFTIRQ_READ))
2721 return 0;
2722 } else {
2723 if (!mark_lock(curr, hlock,
2724 LOCK_ENABLED_HARDIRQ))
2725 return 0;
2726 if (curr->softirqs_enabled)
2727 if (!mark_lock(curr, hlock,
2728 LOCK_ENABLED_SOFTIRQ))
2729 return 0;
2730 }
2731 }
2732
2733
2734
2735
2736
2737
2738
2739 if (!hlock->trylock && (curr->lockdep_reclaim_gfp & __GFP_FS)) {
2740 if (hlock->read) {
2741 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS_READ))
2742 return 0;
2743 } else {
2744 if (!mark_lock(curr, hlock, LOCK_USED_IN_RECLAIM_FS))
2745 return 0;
2746 }
2747 }
2748
2749 return 1;
2750}
2751
2752static int separate_irq_context(struct task_struct *curr,
2753 struct held_lock *hlock)
2754{
2755 unsigned int depth = curr->lockdep_depth;
2756
2757
2758
2759
2760 hlock->irq_context = 2*(curr->hardirq_context ? 1 : 0) +
2761 curr->softirq_context;
2762 if (depth) {
2763 struct held_lock *prev_hlock;
2764
2765 prev_hlock = curr->held_locks + depth-1;
2766
2767
2768
2769
2770
2771 if (prev_hlock->irq_context != hlock->irq_context)
2772 return 1;
2773 }
2774 return 0;
2775}
2776
2777#else
2778
2779static inline
2780int mark_lock_irq(struct task_struct *curr, struct held_lock *this,
2781 enum lock_usage_bit new_bit)
2782{
2783 WARN_ON(1);
2784 return 1;
2785}
2786
2787static inline int mark_irqflags(struct task_struct *curr,
2788 struct held_lock *hlock)
2789{
2790 return 1;
2791}
2792
2793static inline int separate_irq_context(struct task_struct *curr,
2794 struct held_lock *hlock)
2795{
2796 return 0;
2797}
2798
2799void lockdep_trace_alloc(gfp_t gfp_mask)
2800{
2801}
2802
2803#endif
2804
2805
2806
2807
2808static int mark_lock(struct task_struct *curr, struct held_lock *this,
2809 enum lock_usage_bit new_bit)
2810{
2811 unsigned int new_mask = 1 << new_bit, ret = 1;
2812
2813
2814
2815
2816
2817 if (likely(hlock_class(this)->usage_mask & new_mask))
2818 return 1;
2819
2820 if (!graph_lock())
2821 return 0;
2822
2823
2824
2825 if (unlikely(hlock_class(this)->usage_mask & new_mask)) {
2826 graph_unlock();
2827 return 1;
2828 }
2829
2830 hlock_class(this)->usage_mask |= new_mask;
2831
2832 if (!save_trace(hlock_class(this)->usage_traces + new_bit))
2833 return 0;
2834
2835 switch (new_bit) {
2836#define LOCKDEP_STATE(__STATE) \
2837 case LOCK_USED_IN_##__STATE: \
2838 case LOCK_USED_IN_##__STATE##_READ: \
2839 case LOCK_ENABLED_##__STATE: \
2840 case LOCK_ENABLED_##__STATE##_READ:
2841#include "lockdep_states.h"
2842#undef LOCKDEP_STATE
2843 ret = mark_lock_irq(curr, this, new_bit);
2844 if (!ret)
2845 return 0;
2846 break;
2847 case LOCK_USED:
2848 debug_atomic_dec(nr_unused_locks);
2849 break;
2850 default:
2851 if (!debug_locks_off_graph_unlock())
2852 return 0;
2853 WARN_ON(1);
2854 return 0;
2855 }
2856
2857 graph_unlock();
2858
2859
2860
2861
2862 if (ret == 2) {
2863 printk("\nmarked lock as {%s}:\n", usage_str[new_bit]);
2864 print_lock(this);
2865 print_irqtrace_events(curr);
2866 dump_stack();
2867 }
2868
2869 return ret;
2870}
2871
2872
2873
2874
2875void lockdep_init_map(struct lockdep_map *lock, const char *name,
2876 struct lock_class_key *key, int subclass)
2877{
2878 int i;
2879
2880 kmemcheck_mark_initialized(lock, sizeof(*lock));
2881
2882 for (i = 0; i < NR_LOCKDEP_CACHING_CLASSES; i++)
2883 lock->class_cache[i] = NULL;
2884
2885#ifdef CONFIG_LOCK_STAT
2886 lock->cpu = raw_smp_processor_id();
2887#endif
2888
2889 if (DEBUG_LOCKS_WARN_ON(!name)) {
2890 lock->name = "NULL";
2891 return;
2892 }
2893
2894 lock->name = name;
2895
2896 if (DEBUG_LOCKS_WARN_ON(!key))
2897 return;
2898
2899
2900
2901 if (!static_obj(key)) {
2902 printk("BUG: key %p not in .data!\n", key);
2903 DEBUG_LOCKS_WARN_ON(1);
2904 return;
2905 }
2906 lock->key = key;
2907
2908 if (unlikely(!debug_locks))
2909 return;
2910
2911 if (subclass)
2912 register_lock_class(lock, subclass, 1);
2913}
2914EXPORT_SYMBOL_GPL(lockdep_init_map);
2915
2916struct lock_class_key __lockdep_no_validate__;
2917
2918
2919
2920
2921
2922static int __lock_acquire(struct lockdep_map *lock, unsigned int subclass,
2923 int trylock, int read, int check, int hardirqs_off,
2924 struct lockdep_map *nest_lock, unsigned long ip,
2925 int references)
2926{
2927 struct task_struct *curr = current;
2928 struct lock_class *class = NULL;
2929 struct held_lock *hlock;
2930 unsigned int depth, id;
2931 int chain_head = 0;
2932 int class_idx;
2933 u64 chain_key;
2934
2935 if (!prove_locking)
2936 check = 1;
2937
2938 if (unlikely(!debug_locks))
2939 return 0;
2940
2941 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
2942 return 0;
2943
2944 if (lock->key == &__lockdep_no_validate__)
2945 check = 1;
2946
2947 if (subclass < NR_LOCKDEP_CACHING_CLASSES)
2948 class = lock->class_cache[subclass];
2949
2950
2951
2952 if (unlikely(!class)) {
2953 class = register_lock_class(lock, subclass, 0);
2954 if (!class)
2955 return 0;
2956 }
2957 atomic_inc((atomic_t *)&class->ops);
2958 if (very_verbose(class)) {
2959 printk("\nacquire class [%p] %s", class->key, class->name);
2960 if (class->name_version > 1)
2961 printk("#%d", class->name_version);
2962 printk("\n");
2963 dump_stack();
2964 }
2965
2966
2967
2968
2969
2970
2971 depth = curr->lockdep_depth;
2972 if (DEBUG_LOCKS_WARN_ON(depth >= MAX_LOCK_DEPTH))
2973 return 0;
2974
2975 class_idx = class - lock_classes + 1;
2976
2977 if (depth) {
2978 hlock = curr->held_locks + depth - 1;
2979 if (hlock->class_idx == class_idx && nest_lock) {
2980 if (hlock->references)
2981 hlock->references++;
2982 else
2983 hlock->references = 2;
2984
2985 return 1;
2986 }
2987 }
2988
2989 hlock = curr->held_locks + depth;
2990 if (DEBUG_LOCKS_WARN_ON(!class))
2991 return 0;
2992 hlock->class_idx = class_idx;
2993 hlock->acquire_ip = ip;
2994 hlock->instance = lock;
2995 hlock->nest_lock = nest_lock;
2996 hlock->trylock = trylock;
2997 hlock->read = read;
2998 hlock->check = check;
2999 hlock->hardirqs_off = !!hardirqs_off;
3000 hlock->references = references;
3001#ifdef CONFIG_LOCK_STAT
3002 hlock->waittime_stamp = 0;
3003 hlock->holdtime_stamp = lockstat_clock();
3004#endif
3005
3006 if (check == 2 && !mark_irqflags(curr, hlock))
3007 return 0;
3008
3009
3010 if (!mark_lock(curr, hlock, LOCK_USED))
3011 return 0;
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023 id = class - lock_classes;
3024 if (DEBUG_LOCKS_WARN_ON(id >= MAX_LOCKDEP_KEYS))
3025 return 0;
3026
3027 chain_key = curr->curr_chain_key;
3028 if (!depth) {
3029 if (DEBUG_LOCKS_WARN_ON(chain_key != 0))
3030 return 0;
3031 chain_head = 1;
3032 }
3033
3034 hlock->prev_chain_key = chain_key;
3035 if (separate_irq_context(curr, hlock)) {
3036 chain_key = 0;
3037 chain_head = 1;
3038 }
3039 chain_key = iterate_chain_key(chain_key, id);
3040
3041 if (!validate_chain(curr, lock, hlock, chain_head, chain_key))
3042 return 0;
3043
3044 curr->curr_chain_key = chain_key;
3045 curr->lockdep_depth++;
3046 check_chain_key(curr);
3047#ifdef CONFIG_DEBUG_LOCKDEP
3048 if (unlikely(!debug_locks))
3049 return 0;
3050#endif
3051 if (unlikely(curr->lockdep_depth >= MAX_LOCK_DEPTH)) {
3052 debug_locks_off();
3053 printk("BUG: MAX_LOCK_DEPTH too low!\n");
3054 printk("turning off the locking correctness validator.\n");
3055 dump_stack();
3056 return 0;
3057 }
3058
3059 if (unlikely(curr->lockdep_depth > max_lockdep_depth))
3060 max_lockdep_depth = curr->lockdep_depth;
3061
3062 return 1;
3063}
3064
3065static int
3066print_unlock_inbalance_bug(struct task_struct *curr, struct lockdep_map *lock,
3067 unsigned long ip)
3068{
3069 if (!debug_locks_off())
3070 return 0;
3071 if (debug_locks_silent)
3072 return 0;
3073
3074 printk("\n=====================================\n");
3075 printk( "[ BUG: bad unlock balance detected! ]\n");
3076 printk( "-------------------------------------\n");
3077 printk("%s/%d is trying to release lock (",
3078 curr->comm, task_pid_nr(curr));
3079 print_lockdep_cache(lock);
3080 printk(") at:\n");
3081 print_ip_sym(ip);
3082 printk("but there are no more locks to release!\n");
3083 printk("\nother info that might help us debug this:\n");
3084 lockdep_print_held_locks(curr);
3085
3086 printk("\nstack backtrace:\n");
3087 dump_stack();
3088
3089 return 0;
3090}
3091
3092
3093
3094
3095static int check_unlock(struct task_struct *curr, struct lockdep_map *lock,
3096 unsigned long ip)
3097{
3098 if (unlikely(!debug_locks))
3099 return 0;
3100 if (DEBUG_LOCKS_WARN_ON(!irqs_disabled()))
3101 return 0;
3102
3103 if (curr->lockdep_depth <= 0)
3104 return print_unlock_inbalance_bug(curr, lock, ip);
3105
3106 return 1;
3107}
3108
3109static int match_held_lock(struct held_lock *hlock, struct lockdep_map *lock)
3110{
3111 if (hlock->instance == lock)
3112 return 1;
3113
3114 if (hlock->references) {
3115 struct lock_class *class = lock->class_cache[0];
3116
3117 if (!class)
3118 class = look_up_lock_class(lock, 0);
3119
3120
3121
3122
3123
3124
3125
3126 if (!class)
3127 return 0;
3128
3129 if (DEBUG_LOCKS_WARN_ON(!hlock->nest_lock))
3130 return 0;
3131
3132 if (hlock->class_idx == class - lock_classes + 1)
3133 return 1;
3134 }
3135
3136 return 0;
3137}
3138
3139static int
3140__lock_set_class(struct lockdep_map *lock, const char *name,
3141 struct lock_class_key *key, unsigned int subclass,
3142 unsigned long ip)
3143{
3144 struct task_struct *curr = current;
3145 struct held_lock *hlock, *prev_hlock;
3146 struct lock_class *class;
3147 unsigned int depth;
3148 int i;
3149
3150 depth = curr->lockdep_depth;
3151 if (DEBUG_LOCKS_WARN_ON(!depth))
3152 return 0;
3153
3154 prev_hlock = NULL;
3155 for (i = depth-1; i >= 0; i--) {
3156 hlock = curr->held_locks + i;
3157
3158
3159
3160 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3161 break;
3162 if (match_held_lock(hlock, lock))
3163 goto found_it;
3164 prev_hlock = hlock;
3165 }
3166 return print_unlock_inbalance_bug(curr, lock, ip);
3167
3168found_it:
3169 lockdep_init_map(lock, name, key, 0);
3170 class = register_lock_class(lock, subclass, 0);
3171 hlock->class_idx = class - lock_classes + 1;
3172
3173 curr->lockdep_depth = i;
3174 curr->curr_chain_key = hlock->prev_chain_key;
3175
3176 for (; i < depth; i++) {
3177 hlock = curr->held_locks + i;
3178 if (!__lock_acquire(hlock->instance,
3179 hlock_class(hlock)->subclass, hlock->trylock,
3180 hlock->read, hlock->check, hlock->hardirqs_off,
3181 hlock->nest_lock, hlock->acquire_ip,
3182 hlock->references))
3183 return 0;
3184 }
3185
3186 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth))
3187 return 0;
3188 return 1;
3189}
3190
3191
3192
3193
3194
3195
3196
3197static int
3198lock_release_non_nested(struct task_struct *curr,
3199 struct lockdep_map *lock, unsigned long ip)
3200{
3201 struct held_lock *hlock, *prev_hlock;
3202 unsigned int depth;
3203 int i;
3204
3205
3206
3207
3208
3209 depth = curr->lockdep_depth;
3210 if (DEBUG_LOCKS_WARN_ON(!depth))
3211 return 0;
3212
3213 prev_hlock = NULL;
3214 for (i = depth-1; i >= 0; i--) {
3215 hlock = curr->held_locks + i;
3216
3217
3218
3219 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3220 break;
3221 if (match_held_lock(hlock, lock))
3222 goto found_it;
3223 prev_hlock = hlock;
3224 }
3225 return print_unlock_inbalance_bug(curr, lock, ip);
3226
3227found_it:
3228 if (hlock->instance == lock)
3229 lock_release_holdtime(hlock);
3230
3231 if (hlock->references) {
3232 hlock->references--;
3233 if (hlock->references) {
3234
3235
3236
3237
3238
3239 return 1;
3240 }
3241 }
3242
3243
3244
3245
3246
3247
3248
3249 curr->lockdep_depth = i;
3250 curr->curr_chain_key = hlock->prev_chain_key;
3251
3252 for (i++; i < depth; i++) {
3253 hlock = curr->held_locks + i;
3254 if (!__lock_acquire(hlock->instance,
3255 hlock_class(hlock)->subclass, hlock->trylock,
3256 hlock->read, hlock->check, hlock->hardirqs_off,
3257 hlock->nest_lock, hlock->acquire_ip,
3258 hlock->references))
3259 return 0;
3260 }
3261
3262 if (DEBUG_LOCKS_WARN_ON(curr->lockdep_depth != depth - 1))
3263 return 0;
3264 return 1;
3265}
3266
3267
3268
3269
3270
3271
3272
3273static int lock_release_nested(struct task_struct *curr,
3274 struct lockdep_map *lock, unsigned long ip)
3275{
3276 struct held_lock *hlock;
3277 unsigned int depth;
3278
3279
3280
3281
3282 depth = curr->lockdep_depth - 1;
3283 hlock = curr->held_locks + depth;
3284
3285
3286
3287
3288 if (hlock->instance != lock || hlock->references)
3289 return lock_release_non_nested(curr, lock, ip);
3290 curr->lockdep_depth--;
3291
3292 if (DEBUG_LOCKS_WARN_ON(!depth && (hlock->prev_chain_key != 0)))
3293 return 0;
3294
3295 curr->curr_chain_key = hlock->prev_chain_key;
3296
3297 lock_release_holdtime(hlock);
3298
3299#ifdef CONFIG_DEBUG_LOCKDEP
3300 hlock->prev_chain_key = 0;
3301 hlock->class_idx = 0;
3302 hlock->acquire_ip = 0;
3303 hlock->irq_context = 0;
3304#endif
3305 return 1;
3306}
3307
3308
3309
3310
3311
3312
3313
3314static void
3315__lock_release(struct lockdep_map *lock, int nested, unsigned long ip)
3316{
3317 struct task_struct *curr = current;
3318
3319 if (!check_unlock(curr, lock, ip))
3320 return;
3321
3322 if (nested) {
3323 if (!lock_release_nested(curr, lock, ip))
3324 return;
3325 } else {
3326 if (!lock_release_non_nested(curr, lock, ip))
3327 return;
3328 }
3329
3330 check_chain_key(curr);
3331}
3332
3333static int __lock_is_held(struct lockdep_map *lock)
3334{
3335 struct task_struct *curr = current;
3336 int i;
3337
3338 for (i = 0; i < curr->lockdep_depth; i++) {
3339 struct held_lock *hlock = curr->held_locks + i;
3340
3341 if (match_held_lock(hlock, lock))
3342 return 1;
3343 }
3344
3345 return 0;
3346}
3347
3348
3349
3350
3351static void check_flags(unsigned long flags)
3352{
3353#if defined(CONFIG_PROVE_LOCKING) && defined(CONFIG_DEBUG_LOCKDEP) && \
3354 defined(CONFIG_TRACE_IRQFLAGS)
3355 if (!debug_locks)
3356 return;
3357
3358 if (irqs_disabled_flags(flags)) {
3359 if (DEBUG_LOCKS_WARN_ON(current->hardirqs_enabled)) {
3360 printk("possible reason: unannotated irqs-off.\n");
3361 }
3362 } else {
3363 if (DEBUG_LOCKS_WARN_ON(!current->hardirqs_enabled)) {
3364 printk("possible reason: unannotated irqs-on.\n");
3365 }
3366 }
3367
3368
3369
3370
3371
3372
3373 if (!hardirq_count()) {
3374 if (softirq_count())
3375 DEBUG_LOCKS_WARN_ON(current->softirqs_enabled);
3376 else
3377 DEBUG_LOCKS_WARN_ON(!current->softirqs_enabled);
3378 }
3379
3380 if (!debug_locks)
3381 print_irqtrace_events(current);
3382#endif
3383}
3384
3385void lock_set_class(struct lockdep_map *lock, const char *name,
3386 struct lock_class_key *key, unsigned int subclass,
3387 unsigned long ip)
3388{
3389 unsigned long flags;
3390
3391 if (unlikely(current->lockdep_recursion))
3392 return;
3393
3394 raw_local_irq_save(flags);
3395 current->lockdep_recursion = 1;
3396 check_flags(flags);
3397 if (__lock_set_class(lock, name, key, subclass, ip))
3398 check_chain_key(current);
3399 current->lockdep_recursion = 0;
3400 raw_local_irq_restore(flags);
3401}
3402EXPORT_SYMBOL_GPL(lock_set_class);
3403
3404
3405
3406
3407
3408void lock_acquire(struct lockdep_map *lock, unsigned int subclass,
3409 int trylock, int read, int check,
3410 struct lockdep_map *nest_lock, unsigned long ip)
3411{
3412 unsigned long flags;
3413
3414 if (unlikely(current->lockdep_recursion))
3415 return;
3416
3417 raw_local_irq_save(flags);
3418 check_flags(flags);
3419
3420 current->lockdep_recursion = 1;
3421 trace_lock_acquire(lock, subclass, trylock, read, check, nest_lock, ip);
3422 __lock_acquire(lock, subclass, trylock, read, check,
3423 irqs_disabled_flags(flags), nest_lock, ip, 0);
3424 current->lockdep_recursion = 0;
3425 raw_local_irq_restore(flags);
3426}
3427EXPORT_SYMBOL_GPL(lock_acquire);
3428
3429void lock_release(struct lockdep_map *lock, int nested,
3430 unsigned long ip)
3431{
3432 unsigned long flags;
3433
3434 if (unlikely(current->lockdep_recursion))
3435 return;
3436
3437 raw_local_irq_save(flags);
3438 check_flags(flags);
3439 current->lockdep_recursion = 1;
3440 trace_lock_release(lock, ip);
3441 __lock_release(lock, nested, ip);
3442 current->lockdep_recursion = 0;
3443 raw_local_irq_restore(flags);
3444}
3445EXPORT_SYMBOL_GPL(lock_release);
3446
3447int lock_is_held(struct lockdep_map *lock)
3448{
3449 unsigned long flags;
3450 int ret = 0;
3451
3452 if (unlikely(current->lockdep_recursion))
3453 return 1;
3454
3455 raw_local_irq_save(flags);
3456 check_flags(flags);
3457
3458 current->lockdep_recursion = 1;
3459 ret = __lock_is_held(lock);
3460 current->lockdep_recursion = 0;
3461 raw_local_irq_restore(flags);
3462
3463 return ret;
3464}
3465EXPORT_SYMBOL_GPL(lock_is_held);
3466
3467void lockdep_set_current_reclaim_state(gfp_t gfp_mask)
3468{
3469 current->lockdep_reclaim_gfp = gfp_mask;
3470}
3471
3472void lockdep_clear_current_reclaim_state(void)
3473{
3474 current->lockdep_reclaim_gfp = 0;
3475}
3476
3477#ifdef CONFIG_LOCK_STAT
3478static int
3479print_lock_contention_bug(struct task_struct *curr, struct lockdep_map *lock,
3480 unsigned long ip)
3481{
3482 if (!debug_locks_off())
3483 return 0;
3484 if (debug_locks_silent)
3485 return 0;
3486
3487 printk("\n=================================\n");
3488 printk( "[ BUG: bad contention detected! ]\n");
3489 printk( "---------------------------------\n");
3490 printk("%s/%d is trying to contend lock (",
3491 curr->comm, task_pid_nr(curr));
3492 print_lockdep_cache(lock);
3493 printk(") at:\n");
3494 print_ip_sym(ip);
3495 printk("but there are no locks held!\n");
3496 printk("\nother info that might help us debug this:\n");
3497 lockdep_print_held_locks(curr);
3498
3499 printk("\nstack backtrace:\n");
3500 dump_stack();
3501
3502 return 0;
3503}
3504
3505static void
3506__lock_contended(struct lockdep_map *lock, unsigned long ip)
3507{
3508 struct task_struct *curr = current;
3509 struct held_lock *hlock, *prev_hlock;
3510 struct lock_class_stats *stats;
3511 unsigned int depth;
3512 int i, contention_point, contending_point;
3513
3514 depth = curr->lockdep_depth;
3515 if (DEBUG_LOCKS_WARN_ON(!depth))
3516 return;
3517
3518 prev_hlock = NULL;
3519 for (i = depth-1; i >= 0; i--) {
3520 hlock = curr->held_locks + i;
3521
3522
3523
3524 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3525 break;
3526 if (match_held_lock(hlock, lock))
3527 goto found_it;
3528 prev_hlock = hlock;
3529 }
3530 print_lock_contention_bug(curr, lock, ip);
3531 return;
3532
3533found_it:
3534 if (hlock->instance != lock)
3535 return;
3536
3537 hlock->waittime_stamp = lockstat_clock();
3538
3539 contention_point = lock_point(hlock_class(hlock)->contention_point, ip);
3540 contending_point = lock_point(hlock_class(hlock)->contending_point,
3541 lock->ip);
3542
3543 stats = get_lock_stats(hlock_class(hlock));
3544 if (contention_point < LOCKSTAT_POINTS)
3545 stats->contention_point[contention_point]++;
3546 if (contending_point < LOCKSTAT_POINTS)
3547 stats->contending_point[contending_point]++;
3548 if (lock->cpu != smp_processor_id())
3549 stats->bounces[bounce_contended + !!hlock->read]++;
3550 put_lock_stats(stats);
3551}
3552
3553static void
3554__lock_acquired(struct lockdep_map *lock, unsigned long ip)
3555{
3556 struct task_struct *curr = current;
3557 struct held_lock *hlock, *prev_hlock;
3558 struct lock_class_stats *stats;
3559 unsigned int depth;
3560 u64 now, waittime = 0;
3561 int i, cpu;
3562
3563 depth = curr->lockdep_depth;
3564 if (DEBUG_LOCKS_WARN_ON(!depth))
3565 return;
3566
3567 prev_hlock = NULL;
3568 for (i = depth-1; i >= 0; i--) {
3569 hlock = curr->held_locks + i;
3570
3571
3572
3573 if (prev_hlock && prev_hlock->irq_context != hlock->irq_context)
3574 break;
3575 if (match_held_lock(hlock, lock))
3576 goto found_it;
3577 prev_hlock = hlock;
3578 }
3579 print_lock_contention_bug(curr, lock, _RET_IP_);
3580 return;
3581
3582found_it:
3583 if (hlock->instance != lock)
3584 return;
3585
3586 cpu = smp_processor_id();
3587 if (hlock->waittime_stamp) {
3588 now = lockstat_clock();
3589 waittime = now - hlock->waittime_stamp;
3590 hlock->holdtime_stamp = now;
3591 }
3592
3593 trace_lock_acquired(lock, ip);
3594
3595 stats = get_lock_stats(hlock_class(hlock));
3596 if (waittime) {
3597 if (hlock->read)
3598 lock_time_inc(&stats->read_waittime, waittime);
3599 else
3600 lock_time_inc(&stats->write_waittime, waittime);
3601 }
3602 if (lock->cpu != cpu)
3603 stats->bounces[bounce_acquired + !!hlock->read]++;
3604 put_lock_stats(stats);
3605
3606 lock->cpu = cpu;
3607 lock->ip = ip;
3608}
3609
3610void lock_contended(struct lockdep_map *lock, unsigned long ip)
3611{
3612 unsigned long flags;
3613
3614 if (unlikely(!lock_stat))
3615 return;
3616
3617 if (unlikely(current->lockdep_recursion))
3618 return;
3619
3620 raw_local_irq_save(flags);
3621 check_flags(flags);
3622 current->lockdep_recursion = 1;
3623 trace_lock_contended(lock, ip);
3624 __lock_contended(lock, ip);
3625 current->lockdep_recursion = 0;
3626 raw_local_irq_restore(flags);
3627}
3628EXPORT_SYMBOL_GPL(lock_contended);
3629
3630void lock_acquired(struct lockdep_map *lock, unsigned long ip)
3631{
3632 unsigned long flags;
3633
3634 if (unlikely(!lock_stat))
3635 return;
3636
3637 if (unlikely(current->lockdep_recursion))
3638 return;
3639
3640 raw_local_irq_save(flags);
3641 check_flags(flags);
3642 current->lockdep_recursion = 1;
3643 __lock_acquired(lock, ip);
3644 current->lockdep_recursion = 0;
3645 raw_local_irq_restore(flags);
3646}
3647EXPORT_SYMBOL_GPL(lock_acquired);
3648#endif
3649
3650
3651
3652
3653
3654
3655void lockdep_reset(void)
3656{
3657 unsigned long flags;
3658 int i;
3659
3660 raw_local_irq_save(flags);
3661 current->curr_chain_key = 0;
3662 current->lockdep_depth = 0;
3663 current->lockdep_recursion = 0;
3664 memset(current->held_locks, 0, MAX_LOCK_DEPTH*sizeof(struct held_lock));
3665 nr_hardirq_chains = 0;
3666 nr_softirq_chains = 0;
3667 nr_process_chains = 0;
3668 debug_locks = 1;
3669 for (i = 0; i < CHAINHASH_SIZE; i++)
3670 INIT_LIST_HEAD(chainhash_table + i);
3671 raw_local_irq_restore(flags);
3672}
3673
3674static void zap_class(struct lock_class *class)
3675{
3676 int i;
3677
3678
3679
3680
3681
3682 for (i = 0; i < nr_list_entries; i++) {
3683 if (list_entries[i].class == class)
3684 list_del_rcu(&list_entries[i].entry);
3685 }
3686
3687
3688
3689 list_del_rcu(&class->hash_entry);
3690 list_del_rcu(&class->lock_entry);
3691
3692 class->key = NULL;
3693}
3694
3695static inline int within(const void *addr, void *start, unsigned long size)
3696{
3697 return addr >= start && addr < start + size;
3698}
3699
3700void lockdep_free_key_range(void *start, unsigned long size)
3701{
3702 struct lock_class *class, *next;
3703 struct list_head *head;
3704 unsigned long flags;
3705 int i;
3706 int locked;
3707
3708 raw_local_irq_save(flags);
3709 locked = graph_lock();
3710
3711
3712
3713
3714 for (i = 0; i < CLASSHASH_SIZE; i++) {
3715 head = classhash_table + i;
3716 if (list_empty(head))
3717 continue;
3718 list_for_each_entry_safe(class, next, head, hash_entry) {
3719 if (within(class->key, start, size))
3720 zap_class(class);
3721 else if (within(class->name, start, size))
3722 zap_class(class);
3723 }
3724 }
3725
3726 if (locked)
3727 graph_unlock();
3728 raw_local_irq_restore(flags);
3729}
3730
3731void lockdep_reset_lock(struct lockdep_map *lock)
3732{
3733 struct lock_class *class, *next;
3734 struct list_head *head;
3735 unsigned long flags;
3736 int i, j;
3737 int locked;
3738
3739 raw_local_irq_save(flags);
3740
3741
3742
3743
3744 for (j = 0; j < MAX_LOCKDEP_SUBCLASSES; j++) {
3745
3746
3747
3748 class = look_up_lock_class(lock, j);
3749 if (class)
3750 zap_class(class);
3751 }
3752
3753
3754
3755
3756 locked = graph_lock();
3757 for (i = 0; i < CLASSHASH_SIZE; i++) {
3758 head = classhash_table + i;
3759 if (list_empty(head))
3760 continue;
3761 list_for_each_entry_safe(class, next, head, hash_entry) {
3762 int match = 0;
3763
3764 for (j = 0; j < NR_LOCKDEP_CACHING_CLASSES; j++)
3765 match |= class == lock->class_cache[j];
3766
3767 if (unlikely(match)) {
3768 if (debug_locks_off_graph_unlock())
3769 WARN_ON(1);
3770 goto out_restore;
3771 }
3772 }
3773 }
3774 if (locked)
3775 graph_unlock();
3776
3777out_restore:
3778 raw_local_irq_restore(flags);
3779}
3780
3781void lockdep_init(void)
3782{
3783 int i;
3784
3785
3786
3787
3788
3789
3790
3791 if (lockdep_initialized)
3792 return;
3793
3794 for (i = 0; i < CLASSHASH_SIZE; i++)
3795 INIT_LIST_HEAD(classhash_table + i);
3796
3797 for (i = 0; i < CHAINHASH_SIZE; i++)
3798 INIT_LIST_HEAD(chainhash_table + i);
3799
3800 lockdep_initialized = 1;
3801}
3802
3803void __init lockdep_info(void)
3804{
3805 printk("Lock dependency validator: Copyright (c) 2006 Red Hat, Inc., Ingo Molnar\n");
3806
3807 printk("... MAX_LOCKDEP_SUBCLASSES: %lu\n", MAX_LOCKDEP_SUBCLASSES);
3808 printk("... MAX_LOCK_DEPTH: %lu\n", MAX_LOCK_DEPTH);
3809 printk("... MAX_LOCKDEP_KEYS: %lu\n", MAX_LOCKDEP_KEYS);
3810 printk("... CLASSHASH_SIZE: %lu\n", CLASSHASH_SIZE);
3811 printk("... MAX_LOCKDEP_ENTRIES: %lu\n", MAX_LOCKDEP_ENTRIES);
3812 printk("... MAX_LOCKDEP_CHAINS: %lu\n", MAX_LOCKDEP_CHAINS);
3813 printk("... CHAINHASH_SIZE: %lu\n", CHAINHASH_SIZE);
3814
3815 printk(" memory used by lock dependency info: %lu kB\n",
3816 (sizeof(struct lock_class) * MAX_LOCKDEP_KEYS +
3817 sizeof(struct list_head) * CLASSHASH_SIZE +
3818 sizeof(struct lock_list) * MAX_LOCKDEP_ENTRIES +
3819 sizeof(struct lock_chain) * MAX_LOCKDEP_CHAINS +
3820 sizeof(struct list_head) * CHAINHASH_SIZE
3821#ifdef CONFIG_PROVE_LOCKING
3822 + sizeof(struct circular_queue)
3823#endif
3824 ) / 1024
3825 );
3826
3827 printk(" per task-struct memory footprint: %lu bytes\n",
3828 sizeof(struct held_lock) * MAX_LOCK_DEPTH);
3829
3830#ifdef CONFIG_DEBUG_LOCKDEP
3831 if (lockdep_init_error) {
3832 printk("WARNING: lockdep init error! Arch code didn't call lockdep_init() early enough?\n");
3833 printk("Call stack leading to lockdep invocation was:\n");
3834 print_stack_trace(&lockdep_init_trace, 0);
3835 }
3836#endif
3837}
3838
3839static void
3840print_freed_lock_bug(struct task_struct *curr, const void *mem_from,
3841 const void *mem_to, struct held_lock *hlock)
3842{
3843 if (!debug_locks_off())
3844 return;
3845 if (debug_locks_silent)
3846 return;
3847
3848 printk("\n=========================\n");
3849 printk( "[ BUG: held lock freed! ]\n");
3850 printk( "-------------------------\n");
3851 printk("%s/%d is freeing memory %p-%p, with a lock still held there!\n",
3852 curr->comm, task_pid_nr(curr), mem_from, mem_to-1);
3853 print_lock(hlock);
3854 lockdep_print_held_locks(curr);
3855
3856 printk("\nstack backtrace:\n");
3857 dump_stack();
3858}
3859
3860static inline int not_in_range(const void* mem_from, unsigned long mem_len,
3861 const void* lock_from, unsigned long lock_len)
3862{
3863 return lock_from + lock_len <= mem_from ||
3864 mem_from + mem_len <= lock_from;
3865}
3866
3867
3868
3869
3870
3871
3872void debug_check_no_locks_freed(const void *mem_from, unsigned long mem_len)
3873{
3874 struct task_struct *curr = current;
3875 struct held_lock *hlock;
3876 unsigned long flags;
3877 int i;
3878
3879 if (unlikely(!debug_locks))
3880 return;
3881
3882 local_irq_save(flags);
3883 for (i = 0; i < curr->lockdep_depth; i++) {
3884 hlock = curr->held_locks + i;
3885
3886 if (not_in_range(mem_from, mem_len, hlock->instance,
3887 sizeof(*hlock->instance)))
3888 continue;
3889
3890 print_freed_lock_bug(curr, mem_from, mem_from + mem_len, hlock);
3891 break;
3892 }
3893 local_irq_restore(flags);
3894}
3895EXPORT_SYMBOL_GPL(debug_check_no_locks_freed);
3896
3897static void print_held_locks_bug(struct task_struct *curr)
3898{
3899 if (!debug_locks_off())
3900 return;
3901 if (debug_locks_silent)
3902 return;
3903
3904 printk("\n=====================================\n");
3905 printk( "[ BUG: lock held at task exit time! ]\n");
3906 printk( "-------------------------------------\n");
3907 printk("%s/%d is exiting with locks still held!\n",
3908 curr->comm, task_pid_nr(curr));
3909 lockdep_print_held_locks(curr);
3910
3911 printk("\nstack backtrace:\n");
3912 dump_stack();
3913}
3914
3915void debug_check_no_locks_held(struct task_struct *task)
3916{
3917 if (unlikely(task->lockdep_depth > 0))
3918 print_held_locks_bug(task);
3919}
3920
3921void debug_show_all_locks(void)
3922{
3923 struct task_struct *g, *p;
3924 int count = 10;
3925 int unlock = 1;
3926
3927 if (unlikely(!debug_locks)) {
3928 printk("INFO: lockdep is turned off.\n");
3929 return;
3930 }
3931 printk("\nShowing all locks held in the system:\n");
3932
3933
3934
3935
3936
3937
3938
3939retry:
3940 if (!read_trylock(&tasklist_lock)) {
3941 if (count == 10)
3942 printk("hm, tasklist_lock locked, retrying... ");
3943 if (count) {
3944 count--;
3945 printk(" #%d", 10-count);
3946 mdelay(200);
3947 goto retry;
3948 }
3949 printk(" ignoring it.\n");
3950 unlock = 0;
3951 } else {
3952 if (count != 10)
3953 printk(KERN_CONT " locked it.\n");
3954 }
3955
3956 do_each_thread(g, p) {
3957
3958
3959
3960
3961
3962 if (p->state == TASK_RUNNING && p != current)
3963 continue;
3964 if (p->lockdep_depth)
3965 lockdep_print_held_locks(p);
3966 if (!unlock)
3967 if (read_trylock(&tasklist_lock))
3968 unlock = 1;
3969 } while_each_thread(g, p);
3970
3971 printk("\n");
3972 printk("=============================================\n\n");
3973
3974 if (unlock)
3975 read_unlock(&tasklist_lock);
3976}
3977EXPORT_SYMBOL_GPL(debug_show_all_locks);
3978
3979
3980
3981
3982
3983void debug_show_held_locks(struct task_struct *task)
3984{
3985 if (unlikely(!debug_locks)) {
3986 printk("INFO: lockdep is turned off.\n");
3987 return;
3988 }
3989 lockdep_print_held_locks(task);
3990}
3991EXPORT_SYMBOL_GPL(debug_show_held_locks);
3992
3993void lockdep_sys_exit(void)
3994{
3995 struct task_struct *curr = current;
3996
3997 if (unlikely(curr->lockdep_depth)) {
3998 if (!debug_locks_off())
3999 return;
4000 printk("\n================================================\n");
4001 printk( "[ BUG: lock held when returning to user space! ]\n");
4002 printk( "------------------------------------------------\n");
4003 printk("%s/%d is leaving the kernel with locks still held!\n",
4004 curr->comm, curr->pid);
4005 lockdep_print_held_locks(curr);
4006 }
4007}
4008
4009void lockdep_rcu_dereference(const char *file, const int line)
4010{
4011 struct task_struct *curr = current;
4012
4013#ifndef CONFIG_PROVE_RCU_REPEATEDLY
4014 if (!debug_locks_off())
4015 return;
4016#endif
4017
4018 printk("\n===================================================\n");
4019 printk( "[ INFO: suspicious rcu_dereference_check() usage. ]\n");
4020 printk( "---------------------------------------------------\n");
4021 printk("%s:%d invoked rcu_dereference_check() without protection!\n",
4022 file, line);
4023 printk("\nother info that might help us debug this:\n\n");
4024 printk("\nrcu_scheduler_active = %d, debug_locks = %d\n", rcu_scheduler_active, debug_locks);
4025 lockdep_print_held_locks(curr);
4026 printk("\nstack backtrace:\n");
4027 dump_stack();
4028}
4029EXPORT_SYMBOL_GPL(lockdep_rcu_dereference);
4030