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