2 * kernel/lockdep_proc.c
4 * Runtime locking correctness validator
6 * Started by Ingo Molnar:
8 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
11 * Code for /proc/lockdep and /proc/lockdep_stats:
14 #include <linux/module.h>
15 #include <linux/proc_fs.h>
16 #include <linux/seq_file.h>
17 #include <linux/kallsyms.h>
18 #include <linux/debug_locks.h>
19 #include <linux/vmalloc.h>
20 #include <linux/sort.h>
21 #include <asm/uaccess.h>
22 #include <asm/div64.h>
24 #include "lockdep_internals.h"
26 static void *l_next(struct seq_file *m, void *v, loff_t *pos)
28 struct lock_class *class;
32 if (v == SEQ_START_TOKEN)
37 if (class->lock_entry.next != &all_lock_classes)
38 class = list_entry(class->lock_entry.next,
39 struct lock_class, lock_entry);
47 static void *l_start(struct seq_file *m, loff_t *pos)
49 struct lock_class *class;
53 return SEQ_START_TOKEN;
55 list_for_each_entry(class, &all_lock_classes, lock_entry) {
62 static void l_stop(struct seq_file *m, void *v)
66 static void print_name(struct seq_file *m, struct lock_class *class)
69 const char *name = class->name;
72 name = __get_key_name(class->key, str);
73 seq_printf(m, "%s", name);
75 seq_printf(m, "%s", name);
76 if (class->name_version > 1)
77 seq_printf(m, "#%d", class->name_version);
79 seq_printf(m, "/%d", class->subclass);
83 static int l_show(struct seq_file *m, void *v)
85 unsigned long nr_forward_deps, nr_backward_deps;
86 struct lock_class *class = v;
87 struct lock_list *entry;
90 if (v == SEQ_START_TOKEN) {
91 seq_printf(m, "all lock classes:\n");
95 seq_printf(m, "%p", class->key);
96 #ifdef CONFIG_DEBUG_LOCKDEP
97 seq_printf(m, " OPS:%8ld", class->ops);
99 nr_forward_deps = lockdep_count_forward_deps(class);
100 seq_printf(m, " FD:%5ld", nr_forward_deps);
102 nr_backward_deps = lockdep_count_backward_deps(class);
103 seq_printf(m, " BD:%5ld", nr_backward_deps);
105 get_usage_chars(class, &c1, &c2, &c3, &c4);
106 seq_printf(m, " %c%c%c%c", c1, c2, c3, c4);
109 print_name(m, class);
112 list_for_each_entry(entry, &class->locks_after, entry) {
113 if (entry->distance == 1) {
114 seq_printf(m, " -> [%p] ", entry->class->key);
115 print_name(m, entry->class);
124 static const struct seq_operations lockdep_ops = {
131 static int lockdep_open(struct inode *inode, struct file *file)
133 int res = seq_open(file, &lockdep_ops);
135 struct seq_file *m = file->private_data;
137 if (!list_empty(&all_lock_classes))
138 m->private = list_entry(all_lock_classes.next,
139 struct lock_class, lock_entry);
146 static const struct file_operations proc_lockdep_operations = {
147 .open = lockdep_open,
150 .release = seq_release,
153 #ifdef CONFIG_PROVE_LOCKING
154 static void *lc_next(struct seq_file *m, void *v, loff_t *pos)
156 struct lock_chain *chain;
160 if (v == SEQ_START_TOKEN)
165 if (*pos < nr_lock_chains)
166 chain = lock_chains + *pos;
174 static void *lc_start(struct seq_file *m, loff_t *pos)
177 return SEQ_START_TOKEN;
179 if (*pos < nr_lock_chains)
180 return lock_chains + *pos;
185 static void lc_stop(struct seq_file *m, void *v)
189 static int lc_show(struct seq_file *m, void *v)
191 struct lock_chain *chain = v;
192 struct lock_class *class;
195 if (v == SEQ_START_TOKEN) {
196 seq_printf(m, "all lock chains:\n");
200 seq_printf(m, "irq_context: %d\n", chain->irq_context);
202 for (i = 0; i < chain->depth; i++) {
203 class = lock_chain_get_class(chain, i);
204 seq_printf(m, "[%p] ", class->key);
205 print_name(m, class);
213 static const struct seq_operations lockdep_chains_ops = {
220 static int lockdep_chains_open(struct inode *inode, struct file *file)
222 int res = seq_open(file, &lockdep_chains_ops);
224 struct seq_file *m = file->private_data;
227 m->private = lock_chains;
234 static const struct file_operations proc_lockdep_chains_operations = {
235 .open = lockdep_chains_open,
238 .release = seq_release,
240 #endif /* CONFIG_PROVE_LOCKING */
242 static void lockdep_stats_debug_show(struct seq_file *m)
244 #ifdef CONFIG_DEBUG_LOCKDEP
245 unsigned int hi1 = debug_atomic_read(&hardirqs_on_events),
246 hi2 = debug_atomic_read(&hardirqs_off_events),
247 hr1 = debug_atomic_read(&redundant_hardirqs_on),
248 hr2 = debug_atomic_read(&redundant_hardirqs_off),
249 si1 = debug_atomic_read(&softirqs_on_events),
250 si2 = debug_atomic_read(&softirqs_off_events),
251 sr1 = debug_atomic_read(&redundant_softirqs_on),
252 sr2 = debug_atomic_read(&redundant_softirqs_off);
254 seq_printf(m, " chain lookup misses: %11u\n",
255 debug_atomic_read(&chain_lookup_misses));
256 seq_printf(m, " chain lookup hits: %11u\n",
257 debug_atomic_read(&chain_lookup_hits));
258 seq_printf(m, " cyclic checks: %11u\n",
259 debug_atomic_read(&nr_cyclic_checks));
260 seq_printf(m, " cyclic-check recursions: %11u\n",
261 debug_atomic_read(&nr_cyclic_check_recursions));
262 seq_printf(m, " find-mask forwards checks: %11u\n",
263 debug_atomic_read(&nr_find_usage_forwards_checks));
264 seq_printf(m, " find-mask forwards recursions: %11u\n",
265 debug_atomic_read(&nr_find_usage_forwards_recursions));
266 seq_printf(m, " find-mask backwards checks: %11u\n",
267 debug_atomic_read(&nr_find_usage_backwards_checks));
268 seq_printf(m, " find-mask backwards recursions:%11u\n",
269 debug_atomic_read(&nr_find_usage_backwards_recursions));
271 seq_printf(m, " hardirq on events: %11u\n", hi1);
272 seq_printf(m, " hardirq off events: %11u\n", hi2);
273 seq_printf(m, " redundant hardirq ons: %11u\n", hr1);
274 seq_printf(m, " redundant hardirq offs: %11u\n", hr2);
275 seq_printf(m, " softirq on events: %11u\n", si1);
276 seq_printf(m, " softirq off events: %11u\n", si2);
277 seq_printf(m, " redundant softirq ons: %11u\n", sr1);
278 seq_printf(m, " redundant softirq offs: %11u\n", sr2);
282 static int lockdep_stats_show(struct seq_file *m, void *v)
284 struct lock_class *class;
285 unsigned long nr_unused = 0, nr_uncategorized = 0,
286 nr_irq_safe = 0, nr_irq_unsafe = 0,
287 nr_softirq_safe = 0, nr_softirq_unsafe = 0,
288 nr_hardirq_safe = 0, nr_hardirq_unsafe = 0,
289 nr_irq_read_safe = 0, nr_irq_read_unsafe = 0,
290 nr_softirq_read_safe = 0, nr_softirq_read_unsafe = 0,
291 nr_hardirq_read_safe = 0, nr_hardirq_read_unsafe = 0,
292 sum_forward_deps = 0, factor = 0;
294 list_for_each_entry(class, &all_lock_classes, lock_entry) {
296 if (class->usage_mask == 0)
298 if (class->usage_mask == LOCKF_USED)
300 if (class->usage_mask & LOCKF_USED_IN_IRQ)
302 if (class->usage_mask & LOCKF_ENABLED_IRQS)
304 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
306 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
308 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
310 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
312 if (class->usage_mask & LOCKF_USED_IN_IRQ_READ)
314 if (class->usage_mask & LOCKF_ENABLED_IRQS_READ)
315 nr_irq_read_unsafe++;
316 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ)
317 nr_softirq_read_safe++;
318 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
319 nr_softirq_read_unsafe++;
320 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ)
321 nr_hardirq_read_safe++;
322 if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
323 nr_hardirq_read_unsafe++;
325 sum_forward_deps += lockdep_count_forward_deps(class);
327 #ifdef CONFIG_DEBUG_LOCKDEP
328 DEBUG_LOCKS_WARN_ON(debug_atomic_read(&nr_unused_locks) != nr_unused);
330 seq_printf(m, " lock-classes: %11lu [max: %lu]\n",
331 nr_lock_classes, MAX_LOCKDEP_KEYS);
332 seq_printf(m, " direct dependencies: %11lu [max: %lu]\n",
333 nr_list_entries, MAX_LOCKDEP_ENTRIES);
334 seq_printf(m, " indirect dependencies: %11lu\n",
338 * Total number of dependencies:
340 * All irq-safe locks may nest inside irq-unsafe locks,
341 * plus all the other known dependencies:
343 seq_printf(m, " all direct dependencies: %11lu\n",
344 nr_irq_unsafe * nr_irq_safe +
345 nr_hardirq_unsafe * nr_hardirq_safe +
349 * Estimated factor between direct and indirect
353 factor = sum_forward_deps / nr_list_entries;
355 #ifdef CONFIG_PROVE_LOCKING
356 seq_printf(m, " dependency chains: %11lu [max: %lu]\n",
357 nr_lock_chains, MAX_LOCKDEP_CHAINS);
358 seq_printf(m, " dependency chain hlocks: %11d [max: %lu]\n",
359 nr_chain_hlocks, MAX_LOCKDEP_CHAIN_HLOCKS);
362 #ifdef CONFIG_TRACE_IRQFLAGS
363 seq_printf(m, " in-hardirq chains: %11u\n",
365 seq_printf(m, " in-softirq chains: %11u\n",
368 seq_printf(m, " in-process chains: %11u\n",
370 seq_printf(m, " stack-trace entries: %11lu [max: %lu]\n",
371 nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES);
372 seq_printf(m, " combined max dependencies: %11u\n",
373 (nr_hardirq_chains + 1) *
374 (nr_softirq_chains + 1) *
375 (nr_process_chains + 1)
377 seq_printf(m, " hardirq-safe locks: %11lu\n",
379 seq_printf(m, " hardirq-unsafe locks: %11lu\n",
381 seq_printf(m, " softirq-safe locks: %11lu\n",
383 seq_printf(m, " softirq-unsafe locks: %11lu\n",
385 seq_printf(m, " irq-safe locks: %11lu\n",
387 seq_printf(m, " irq-unsafe locks: %11lu\n",
390 seq_printf(m, " hardirq-read-safe locks: %11lu\n",
391 nr_hardirq_read_safe);
392 seq_printf(m, " hardirq-read-unsafe locks: %11lu\n",
393 nr_hardirq_read_unsafe);
394 seq_printf(m, " softirq-read-safe locks: %11lu\n",
395 nr_softirq_read_safe);
396 seq_printf(m, " softirq-read-unsafe locks: %11lu\n",
397 nr_softirq_read_unsafe);
398 seq_printf(m, " irq-read-safe locks: %11lu\n",
400 seq_printf(m, " irq-read-unsafe locks: %11lu\n",
403 seq_printf(m, " uncategorized locks: %11lu\n",
405 seq_printf(m, " unused locks: %11lu\n",
407 seq_printf(m, " max locking depth: %11u\n",
409 seq_printf(m, " max recursion depth: %11u\n",
410 max_recursion_depth);
411 lockdep_stats_debug_show(m);
412 seq_printf(m, " debug_locks: %11u\n",
418 static int lockdep_stats_open(struct inode *inode, struct file *file)
420 return single_open(file, lockdep_stats_show, NULL);
423 static const struct file_operations proc_lockdep_stats_operations = {
424 .open = lockdep_stats_open,
427 .release = single_release,
430 #ifdef CONFIG_LOCK_STAT
432 struct lock_stat_data {
433 struct lock_class *class;
434 struct lock_class_stats stats;
437 struct lock_stat_seq {
438 struct lock_stat_data *iter;
439 struct lock_stat_data *iter_end;
440 struct lock_stat_data stats[MAX_LOCKDEP_KEYS];
444 * sort on absolute number of contentions
446 static int lock_stat_cmp(const void *l, const void *r)
448 const struct lock_stat_data *dl = l, *dr = r;
449 unsigned long nl, nr;
451 nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr;
452 nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr;
457 static void seq_line(struct seq_file *m, char c, int offset, int length)
461 for (i = 0; i < offset; i++)
463 for (i = 0; i < length; i++)
464 seq_printf(m, "%c", c);
468 static void snprint_time(char *buf, size_t bufsiz, s64 nr)
472 rem = do_div(nr, 1000); /* XXX: do_div_signed */
473 snprintf(buf, bufsiz, "%lld.%02d", (long long)nr, ((int)rem+5)/10);
476 static void seq_time(struct seq_file *m, s64 time)
480 snprint_time(num, sizeof(num), time);
481 seq_printf(m, " %14s", num);
484 static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
486 seq_printf(m, "%14lu", lt->nr);
487 seq_time(m, lt->min);
488 seq_time(m, lt->max);
489 seq_time(m, lt->total);
492 static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
495 struct lock_class *class;
496 struct lock_class_stats *stats;
500 stats = &data->stats;
503 if (class->name_version > 1)
504 namelen -= 2; /* XXX truncates versions > 9 */
509 char str[KSYM_NAME_LEN];
510 const char *key_name;
512 key_name = __get_key_name(class->key, str);
513 snprintf(name, namelen, "%s", key_name);
515 snprintf(name, namelen, "%s", class->name);
517 namelen = strlen(name);
518 if (class->name_version > 1) {
519 snprintf(name+namelen, 3, "#%d", class->name_version);
522 if (class->subclass) {
523 snprintf(name+namelen, 3, "/%d", class->subclass);
527 if (stats->write_holdtime.nr) {
528 if (stats->read_holdtime.nr)
529 seq_printf(m, "%38s-W:", name);
531 seq_printf(m, "%40s:", name);
533 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
534 seq_lock_time(m, &stats->write_waittime);
535 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
536 seq_lock_time(m, &stats->write_holdtime);
540 if (stats->read_holdtime.nr) {
541 seq_printf(m, "%38s-R:", name);
542 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_read]);
543 seq_lock_time(m, &stats->read_waittime);
544 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_read]);
545 seq_lock_time(m, &stats->read_holdtime);
549 if (stats->read_waittime.nr + stats->write_waittime.nr == 0)
552 if (stats->read_holdtime.nr)
555 for (i = 0; i < ARRAY_SIZE(class->contention_point); i++) {
556 char sym[KSYM_SYMBOL_LEN];
559 if (class->contention_point[i] == 0)
563 seq_line(m, '-', 40-namelen, namelen);
565 sprint_symbol(sym, class->contention_point[i]);
566 snprintf(ip, sizeof(ip), "[<%p>]",
567 (void *)class->contention_point[i]);
568 seq_printf(m, "%40s %14lu %29s %s\n", name,
569 stats->contention_point[i],
574 seq_line(m, '.', 0, 40 + 1 + 10 * (14 + 1));
579 static void seq_header(struct seq_file *m)
581 seq_printf(m, "lock_stat version 0.2\n");
582 seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
583 seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s "
596 seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
600 static void *ls_start(struct seq_file *m, loff_t *pos)
602 struct lock_stat_seq *data = m->private;
605 return SEQ_START_TOKEN;
607 data->iter = data->stats + *pos;
608 if (data->iter >= data->iter_end)
614 static void *ls_next(struct seq_file *m, void *v, loff_t *pos)
616 struct lock_stat_seq *data = m->private;
620 if (v == SEQ_START_TOKEN)
621 data->iter = data->stats;
627 if (data->iter == data->iter_end)
633 static void ls_stop(struct seq_file *m, void *v)
637 static int ls_show(struct seq_file *m, void *v)
639 if (v == SEQ_START_TOKEN)
647 static struct seq_operations lockstat_ops = {
654 static int lock_stat_open(struct inode *inode, struct file *file)
657 struct lock_class *class;
658 struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq));
663 res = seq_open(file, &lockstat_ops);
665 struct lock_stat_data *iter = data->stats;
666 struct seq_file *m = file->private_data;
669 list_for_each_entry(class, &all_lock_classes, lock_entry) {
671 iter->stats = lock_stats(class);
674 data->iter_end = iter;
676 sort(data->stats, data->iter_end - data->iter,
677 sizeof(struct lock_stat_data),
678 lock_stat_cmp, NULL);
687 static ssize_t lock_stat_write(struct file *file, const char __user *buf,
688 size_t count, loff_t *ppos)
690 struct lock_class *class;
694 if (get_user(c, buf))
700 list_for_each_entry(class, &all_lock_classes, lock_entry)
701 clear_lock_stats(class);
706 static int lock_stat_release(struct inode *inode, struct file *file)
708 struct seq_file *seq = file->private_data;
712 return seq_release(inode, file);
715 static const struct file_operations proc_lock_stat_operations = {
716 .open = lock_stat_open,
717 .write = lock_stat_write,
720 .release = lock_stat_release,
722 #endif /* CONFIG_LOCK_STAT */
724 static int __init lockdep_proc_init(void)
726 proc_create("lockdep", S_IRUSR, NULL, &proc_lockdep_operations);
727 #ifdef CONFIG_PROVE_LOCKING
728 proc_create("lockdep_chains", S_IRUSR, NULL,
729 &proc_lockdep_chains_operations);
731 proc_create("lockdep_stats", S_IRUSR, NULL,
732 &proc_lockdep_stats_operations);
734 #ifdef CONFIG_LOCK_STAT
735 proc_create("lock_stat", S_IRUSR, NULL, &proc_lock_stat_operations);
741 __initcall(lockdep_proc_init);