1 // SPDX-License-Identifier: GPL-2.0
3 * kernel/lockdep_proc.c
5 * Runtime locking correctness validator
7 * Started by Ingo Molnar:
9 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
10 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
12 * Code for /proc/lockdep and /proc/lockdep_stats:
15 #include <linux/export.h>
16 #include <linux/proc_fs.h>
17 #include <linux/seq_file.h>
18 #include <linux/kallsyms.h>
19 #include <linux/debug_locks.h>
20 #include <linux/vmalloc.h>
21 #include <linux/sort.h>
22 #include <linux/uaccess.h>
23 #include <asm/div64.h>
25 #include "lockdep_internals.h"
28 * Since iteration of lock_classes is done without holding the lockdep lock,
29 * it is not safe to iterate all_lock_classes list directly as the iteration
30 * may branch off to free_lock_classes or the zapped list. Iteration is done
31 * directly on the lock_classes array by checking the lock_classes_in_use
32 * bitmap and max_lock_class_idx.
34 #define iterate_lock_classes(idx, class) \
35 for (idx = 0, class = lock_classes; idx <= max_lock_class_idx; \
38 static void *l_next(struct seq_file *m, void *v, loff_t *pos)
40 struct lock_class *class = v;
43 *pos = class - lock_classes;
44 return (*pos > max_lock_class_idx) ? NULL : class;
47 static void *l_start(struct seq_file *m, loff_t *pos)
49 unsigned long idx = *pos;
51 if (idx > max_lock_class_idx)
53 return lock_classes + idx;
56 static void l_stop(struct seq_file *m, void *v)
60 static void print_name(struct seq_file *m, struct lock_class *class)
62 char str[KSYM_NAME_LEN];
63 const char *name = class->name;
66 name = __get_key_name(class->key, str);
67 seq_printf(m, "%s", name);
69 seq_printf(m, "%s", name);
70 if (class->name_version > 1)
71 seq_printf(m, "#%d", class->name_version);
73 seq_printf(m, "/%d", class->subclass);
77 static int l_show(struct seq_file *m, void *v)
79 struct lock_class *class = v;
80 struct lock_list *entry;
81 char usage[LOCK_USAGE_CHARS];
82 int idx = class - lock_classes;
84 if (v == lock_classes)
85 seq_printf(m, "all lock classes:\n");
87 if (!test_bit(idx, lock_classes_in_use))
90 seq_printf(m, "%p", class->key);
91 #ifdef CONFIG_DEBUG_LOCKDEP
92 seq_printf(m, " OPS:%8ld", debug_class_ops_read(class));
94 #ifdef CONFIG_PROVE_LOCKING
95 seq_printf(m, " FD:%5ld", lockdep_count_forward_deps(class));
96 seq_printf(m, " BD:%5ld", lockdep_count_backward_deps(class));
99 get_usage_chars(class, usage);
100 seq_printf(m, " %s", usage);
103 print_name(m, class);
106 list_for_each_entry(entry, &class->locks_after, entry) {
107 if (entry->distance == 1) {
108 seq_printf(m, " -> [%p] ", entry->class->key);
109 print_name(m, entry->class);
118 static const struct seq_operations lockdep_ops = {
125 #ifdef CONFIG_PROVE_LOCKING
126 static void *lc_start(struct seq_file *m, loff_t *pos)
132 return SEQ_START_TOKEN;
134 return lock_chains + (*pos - 1);
137 static void *lc_next(struct seq_file *m, void *v, loff_t *pos)
139 *pos = lockdep_next_lockchain(*pos - 1) + 1;
140 return lc_start(m, pos);
143 static void lc_stop(struct seq_file *m, void *v)
147 static int lc_show(struct seq_file *m, void *v)
149 struct lock_chain *chain = v;
150 struct lock_class *class;
152 static const char * const irq_strs[] = {
154 [LOCK_CHAIN_HARDIRQ_CONTEXT] = "hardirq",
155 [LOCK_CHAIN_SOFTIRQ_CONTEXT] = "softirq",
156 [LOCK_CHAIN_SOFTIRQ_CONTEXT|
157 LOCK_CHAIN_HARDIRQ_CONTEXT] = "hardirq|softirq",
160 if (v == SEQ_START_TOKEN) {
161 if (!nr_free_chain_hlocks)
162 seq_printf(m, "(buggered) ");
163 seq_printf(m, "all lock chains:\n");
167 seq_printf(m, "irq_context: %s\n", irq_strs[chain->irq_context]);
169 for (i = 0; i < chain->depth; i++) {
170 class = lock_chain_get_class(chain, i);
174 seq_printf(m, "[%p] ", class->key);
175 print_name(m, class);
183 static const struct seq_operations lockdep_chains_ops = {
189 #endif /* CONFIG_PROVE_LOCKING */
191 static void lockdep_stats_debug_show(struct seq_file *m)
193 #ifdef CONFIG_DEBUG_LOCKDEP
194 unsigned long long hi1 = debug_atomic_read(hardirqs_on_events),
195 hi2 = debug_atomic_read(hardirqs_off_events),
196 hr1 = debug_atomic_read(redundant_hardirqs_on),
197 hr2 = debug_atomic_read(redundant_hardirqs_off),
198 si1 = debug_atomic_read(softirqs_on_events),
199 si2 = debug_atomic_read(softirqs_off_events),
200 sr1 = debug_atomic_read(redundant_softirqs_on),
201 sr2 = debug_atomic_read(redundant_softirqs_off);
203 seq_printf(m, " chain lookup misses: %11llu\n",
204 debug_atomic_read(chain_lookup_misses));
205 seq_printf(m, " chain lookup hits: %11llu\n",
206 debug_atomic_read(chain_lookup_hits));
207 seq_printf(m, " cyclic checks: %11llu\n",
208 debug_atomic_read(nr_cyclic_checks));
209 seq_printf(m, " redundant checks: %11llu\n",
210 debug_atomic_read(nr_redundant_checks));
211 seq_printf(m, " redundant links: %11llu\n",
212 debug_atomic_read(nr_redundant));
213 seq_printf(m, " find-mask forwards checks: %11llu\n",
214 debug_atomic_read(nr_find_usage_forwards_checks));
215 seq_printf(m, " find-mask backwards checks: %11llu\n",
216 debug_atomic_read(nr_find_usage_backwards_checks));
218 seq_printf(m, " hardirq on events: %11llu\n", hi1);
219 seq_printf(m, " hardirq off events: %11llu\n", hi2);
220 seq_printf(m, " redundant hardirq ons: %11llu\n", hr1);
221 seq_printf(m, " redundant hardirq offs: %11llu\n", hr2);
222 seq_printf(m, " softirq on events: %11llu\n", si1);
223 seq_printf(m, " softirq off events: %11llu\n", si2);
224 seq_printf(m, " redundant softirq ons: %11llu\n", sr1);
225 seq_printf(m, " redundant softirq offs: %11llu\n", sr2);
229 static int lockdep_stats_show(struct seq_file *m, void *v)
231 unsigned long nr_unused = 0, nr_uncategorized = 0,
232 nr_irq_safe = 0, nr_irq_unsafe = 0,
233 nr_softirq_safe = 0, nr_softirq_unsafe = 0,
234 nr_hardirq_safe = 0, nr_hardirq_unsafe = 0,
235 nr_irq_read_safe = 0, nr_irq_read_unsafe = 0,
236 nr_softirq_read_safe = 0, nr_softirq_read_unsafe = 0,
237 nr_hardirq_read_safe = 0, nr_hardirq_read_unsafe = 0,
238 sum_forward_deps = 0;
240 #ifdef CONFIG_PROVE_LOCKING
241 struct lock_class *class;
244 iterate_lock_classes(idx, class) {
245 if (!test_bit(idx, lock_classes_in_use))
248 if (class->usage_mask == 0)
250 if (class->usage_mask == LOCKF_USED)
252 if (class->usage_mask & LOCKF_USED_IN_IRQ)
254 if (class->usage_mask & LOCKF_ENABLED_IRQ)
256 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
258 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ)
260 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
262 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ)
264 if (class->usage_mask & LOCKF_USED_IN_IRQ_READ)
266 if (class->usage_mask & LOCKF_ENABLED_IRQ_READ)
267 nr_irq_read_unsafe++;
268 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ)
269 nr_softirq_read_safe++;
270 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
271 nr_softirq_read_unsafe++;
272 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ)
273 nr_hardirq_read_safe++;
274 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
275 nr_hardirq_read_unsafe++;
277 sum_forward_deps += lockdep_count_forward_deps(class);
280 #ifdef CONFIG_DEBUG_LOCKDEP
281 DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused);
285 seq_printf(m, " lock-classes: %11lu [max: %lu]\n",
286 nr_lock_classes, MAX_LOCKDEP_KEYS);
287 seq_printf(m, " direct dependencies: %11lu [max: %lu]\n",
288 nr_list_entries, MAX_LOCKDEP_ENTRIES);
289 seq_printf(m, " indirect dependencies: %11lu\n",
293 * Total number of dependencies:
295 * All irq-safe locks may nest inside irq-unsafe locks,
296 * plus all the other known dependencies:
298 seq_printf(m, " all direct dependencies: %11lu\n",
299 nr_irq_unsafe * nr_irq_safe +
300 nr_hardirq_unsafe * nr_hardirq_safe +
303 #ifdef CONFIG_PROVE_LOCKING
304 seq_printf(m, " dependency chains: %11lu [max: %lu]\n",
305 lock_chain_count(), MAX_LOCKDEP_CHAINS);
306 seq_printf(m, " dependency chain hlocks used: %11lu [max: %lu]\n",
307 MAX_LOCKDEP_CHAIN_HLOCKS -
308 (nr_free_chain_hlocks + nr_lost_chain_hlocks),
309 MAX_LOCKDEP_CHAIN_HLOCKS);
310 seq_printf(m, " dependency chain hlocks lost: %11u\n",
311 nr_lost_chain_hlocks);
314 #ifdef CONFIG_TRACE_IRQFLAGS
315 seq_printf(m, " in-hardirq chains: %11u\n",
317 seq_printf(m, " in-softirq chains: %11u\n",
320 seq_printf(m, " in-process chains: %11u\n",
322 seq_printf(m, " stack-trace entries: %11lu [max: %lu]\n",
323 nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES);
324 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
325 seq_printf(m, " number of stack traces: %11llu\n",
326 lockdep_stack_trace_count());
327 seq_printf(m, " number of stack hash chains: %11llu\n",
328 lockdep_stack_hash_count());
330 seq_printf(m, " combined max dependencies: %11u\n",
331 (nr_hardirq_chains + 1) *
332 (nr_softirq_chains + 1) *
333 (nr_process_chains + 1)
335 seq_printf(m, " hardirq-safe locks: %11lu\n",
337 seq_printf(m, " hardirq-unsafe locks: %11lu\n",
339 seq_printf(m, " softirq-safe locks: %11lu\n",
341 seq_printf(m, " softirq-unsafe locks: %11lu\n",
343 seq_printf(m, " irq-safe locks: %11lu\n",
345 seq_printf(m, " irq-unsafe locks: %11lu\n",
348 seq_printf(m, " hardirq-read-safe locks: %11lu\n",
349 nr_hardirq_read_safe);
350 seq_printf(m, " hardirq-read-unsafe locks: %11lu\n",
351 nr_hardirq_read_unsafe);
352 seq_printf(m, " softirq-read-safe locks: %11lu\n",
353 nr_softirq_read_safe);
354 seq_printf(m, " softirq-read-unsafe locks: %11lu\n",
355 nr_softirq_read_unsafe);
356 seq_printf(m, " irq-read-safe locks: %11lu\n",
358 seq_printf(m, " irq-read-unsafe locks: %11lu\n",
361 seq_printf(m, " uncategorized locks: %11lu\n",
363 seq_printf(m, " unused locks: %11lu\n",
365 seq_printf(m, " max locking depth: %11u\n",
367 #ifdef CONFIG_PROVE_LOCKING
368 seq_printf(m, " max bfs queue depth: %11u\n",
369 max_bfs_queue_depth);
371 seq_printf(m, " max lock class index: %11lu\n",
373 lockdep_stats_debug_show(m);
374 seq_printf(m, " debug_locks: %11u\n",
378 * Zappped classes and lockdep data buffers reuse statistics.
381 seq_printf(m, " zapped classes: %11lu\n",
383 #ifdef CONFIG_PROVE_LOCKING
384 seq_printf(m, " zapped lock chains: %11lu\n",
385 nr_zapped_lock_chains);
386 seq_printf(m, " large chain blocks: %11u\n",
387 nr_large_chain_blocks);
392 #ifdef CONFIG_LOCK_STAT
394 struct lock_stat_data {
395 struct lock_class *class;
396 struct lock_class_stats stats;
399 struct lock_stat_seq {
400 struct lock_stat_data *iter_end;
401 struct lock_stat_data stats[MAX_LOCKDEP_KEYS];
405 * sort on absolute number of contentions
407 static int lock_stat_cmp(const void *l, const void *r)
409 const struct lock_stat_data *dl = l, *dr = r;
410 unsigned long nl, nr;
412 nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr;
413 nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr;
418 static void seq_line(struct seq_file *m, char c, int offset, int length)
422 for (i = 0; i < offset; i++)
424 for (i = 0; i < length; i++)
425 seq_printf(m, "%c", c);
429 static void snprint_time(char *buf, size_t bufsiz, s64 nr)
434 nr += 5; /* for display rounding */
435 div = div_s64_rem(nr, 1000, &rem);
436 snprintf(buf, bufsiz, "%lld.%02d", (long long)div, (int)rem/10);
439 static void seq_time(struct seq_file *m, s64 time)
443 snprint_time(num, sizeof(num), time);
444 seq_printf(m, " %14s", num);
447 static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
449 seq_printf(m, "%14lu", lt->nr);
450 seq_time(m, lt->min);
451 seq_time(m, lt->max);
452 seq_time(m, lt->total);
453 seq_time(m, lt->nr ? div64_u64(lt->total, lt->nr) : 0);
456 static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
458 const struct lockdep_subclass_key *ckey;
459 struct lock_class_stats *stats;
460 struct lock_class *class;
466 stats = &data->stats;
469 if (class->name_version > 1)
470 namelen -= 2; /* XXX truncates versions > 9 */
474 rcu_read_lock_sched();
475 cname = rcu_dereference_sched(class->name);
476 ckey = rcu_dereference_sched(class->key);
478 if (!cname && !ckey) {
479 rcu_read_unlock_sched();
483 char str[KSYM_NAME_LEN];
484 const char *key_name;
486 key_name = __get_key_name(ckey, str);
487 snprintf(name, namelen, "%s", key_name);
489 snprintf(name, namelen, "%s", cname);
491 rcu_read_unlock_sched();
493 namelen = strlen(name);
494 if (class->name_version > 1) {
495 snprintf(name+namelen, 3, "#%d", class->name_version);
498 if (class->subclass) {
499 snprintf(name+namelen, 3, "/%d", class->subclass);
503 if (stats->write_holdtime.nr) {
504 if (stats->read_holdtime.nr)
505 seq_printf(m, "%38s-W:", name);
507 seq_printf(m, "%40s:", name);
509 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
510 seq_lock_time(m, &stats->write_waittime);
511 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
512 seq_lock_time(m, &stats->write_holdtime);
516 if (stats->read_holdtime.nr) {
517 seq_printf(m, "%38s-R:", name);
518 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_read]);
519 seq_lock_time(m, &stats->read_waittime);
520 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_read]);
521 seq_lock_time(m, &stats->read_holdtime);
525 if (stats->read_waittime.nr + stats->write_waittime.nr == 0)
528 if (stats->read_holdtime.nr)
531 for (i = 0; i < LOCKSTAT_POINTS; i++) {
534 if (class->contention_point[i] == 0)
538 seq_line(m, '-', 40-namelen, namelen);
540 snprintf(ip, sizeof(ip), "[<%p>]",
541 (void *)class->contention_point[i]);
542 seq_printf(m, "%40s %14lu %29s %pS\n",
543 name, stats->contention_point[i],
544 ip, (void *)class->contention_point[i]);
546 for (i = 0; i < LOCKSTAT_POINTS; i++) {
549 if (class->contending_point[i] == 0)
553 seq_line(m, '-', 40-namelen, namelen);
555 snprintf(ip, sizeof(ip), "[<%p>]",
556 (void *)class->contending_point[i]);
557 seq_printf(m, "%40s %14lu %29s %pS\n",
558 name, stats->contending_point[i],
559 ip, (void *)class->contending_point[i]);
563 seq_line(m, '.', 0, 40 + 1 + 12 * (14 + 1));
568 static void seq_header(struct seq_file *m)
570 seq_puts(m, "lock_stat version 0.4\n");
572 if (unlikely(!debug_locks))
573 seq_printf(m, "*WARNING* lock debugging disabled!! - possibly due to a lockdep warning\n");
575 seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
576 seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
591 seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
595 static void *ls_start(struct seq_file *m, loff_t *pos)
597 struct lock_stat_seq *data = m->private;
598 struct lock_stat_data *iter;
601 return SEQ_START_TOKEN;
603 iter = data->stats + (*pos - 1);
604 if (iter >= data->iter_end)
610 static void *ls_next(struct seq_file *m, void *v, loff_t *pos)
613 return ls_start(m, pos);
616 static void ls_stop(struct seq_file *m, void *v)
620 static int ls_show(struct seq_file *m, void *v)
622 if (v == SEQ_START_TOKEN)
630 static const struct seq_operations lockstat_ops = {
637 static int lock_stat_open(struct inode *inode, struct file *file)
640 struct lock_class *class;
641 struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq));
646 res = seq_open(file, &lockstat_ops);
648 struct lock_stat_data *iter = data->stats;
649 struct seq_file *m = file->private_data;
652 iterate_lock_classes(idx, class) {
653 if (!test_bit(idx, lock_classes_in_use))
656 iter->stats = lock_stats(class);
660 data->iter_end = iter;
662 sort(data->stats, data->iter_end - data->stats,
663 sizeof(struct lock_stat_data),
664 lock_stat_cmp, NULL);
673 static ssize_t lock_stat_write(struct file *file, const char __user *buf,
674 size_t count, loff_t *ppos)
676 struct lock_class *class;
681 if (get_user(c, buf))
687 iterate_lock_classes(idx, class) {
688 if (!test_bit(idx, lock_classes_in_use))
690 clear_lock_stats(class);
696 static int lock_stat_release(struct inode *inode, struct file *file)
698 struct seq_file *seq = file->private_data;
701 return seq_release(inode, file);
704 static const struct proc_ops lock_stat_proc_ops = {
705 .proc_open = lock_stat_open,
706 .proc_write = lock_stat_write,
707 .proc_read = seq_read,
708 .proc_lseek = seq_lseek,
709 .proc_release = lock_stat_release,
711 #endif /* CONFIG_LOCK_STAT */
713 static int __init lockdep_proc_init(void)
715 proc_create_seq("lockdep", S_IRUSR, NULL, &lockdep_ops);
716 #ifdef CONFIG_PROVE_LOCKING
717 proc_create_seq("lockdep_chains", S_IRUSR, NULL, &lockdep_chains_ops);
719 proc_create_single("lockdep_stats", S_IRUSR, NULL, lockdep_stats_show);
720 #ifdef CONFIG_LOCK_STAT
721 proc_create("lock_stat", S_IRUSR | S_IWUSR, NULL, &lock_stat_proc_ops);
727 __initcall(lockdep_proc_init);