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 struct lock_class *class = v;
86 struct lock_list *entry;
87 char usage[LOCK_USAGE_CHARS];
89 if (v == SEQ_START_TOKEN) {
90 seq_printf(m, "all lock classes:\n");
94 seq_printf(m, "%p", class->key);
95 #ifdef CONFIG_DEBUG_LOCKDEP
96 seq_printf(m, " OPS:%8ld", class->ops);
98 #ifdef CONFIG_PROVE_LOCKING
99 seq_printf(m, " FD:%5ld", lockdep_count_forward_deps(class));
100 seq_printf(m, " BD:%5ld", lockdep_count_backward_deps(class));
103 get_usage_chars(class, usage);
104 seq_printf(m, " %s", usage);
107 print_name(m, class);
110 list_for_each_entry(entry, &class->locks_after, entry) {
111 if (entry->distance == 1) {
112 seq_printf(m, " -> [%p] ", entry->class->key);
113 print_name(m, entry->class);
122 static const struct seq_operations lockdep_ops = {
129 static int lockdep_open(struct inode *inode, struct file *file)
131 int res = seq_open(file, &lockdep_ops);
133 struct seq_file *m = file->private_data;
135 if (!list_empty(&all_lock_classes))
136 m->private = list_entry(all_lock_classes.next,
137 struct lock_class, lock_entry);
144 static const struct file_operations proc_lockdep_operations = {
145 .open = lockdep_open,
148 .release = seq_release,
151 #ifdef CONFIG_PROVE_LOCKING
152 static void *lc_next(struct seq_file *m, void *v, loff_t *pos)
154 struct lock_chain *chain;
158 if (v == SEQ_START_TOKEN)
163 if (*pos < nr_lock_chains)
164 chain = lock_chains + *pos;
172 static void *lc_start(struct seq_file *m, loff_t *pos)
175 return SEQ_START_TOKEN;
177 if (*pos < nr_lock_chains)
178 return lock_chains + *pos;
183 static void lc_stop(struct seq_file *m, void *v)
187 static int lc_show(struct seq_file *m, void *v)
189 struct lock_chain *chain = v;
190 struct lock_class *class;
193 if (v == SEQ_START_TOKEN) {
194 seq_printf(m, "all lock chains:\n");
198 seq_printf(m, "irq_context: %d\n", chain->irq_context);
200 for (i = 0; i < chain->depth; i++) {
201 class = lock_chain_get_class(chain, i);
205 seq_printf(m, "[%p] ", class->key);
206 print_name(m, class);
214 static const struct seq_operations lockdep_chains_ops = {
221 static int lockdep_chains_open(struct inode *inode, struct file *file)
223 int res = seq_open(file, &lockdep_chains_ops);
225 struct seq_file *m = file->private_data;
228 m->private = lock_chains;
235 static const struct file_operations proc_lockdep_chains_operations = {
236 .open = lockdep_chains_open,
239 .release = seq_release,
241 #endif /* CONFIG_PROVE_LOCKING */
243 static void lockdep_stats_debug_show(struct seq_file *m)
245 #ifdef CONFIG_DEBUG_LOCKDEP
246 unsigned int hi1 = debug_atomic_read(&hardirqs_on_events),
247 hi2 = debug_atomic_read(&hardirqs_off_events),
248 hr1 = debug_atomic_read(&redundant_hardirqs_on),
249 hr2 = debug_atomic_read(&redundant_hardirqs_off),
250 si1 = debug_atomic_read(&softirqs_on_events),
251 si2 = debug_atomic_read(&softirqs_off_events),
252 sr1 = debug_atomic_read(&redundant_softirqs_on),
253 sr2 = debug_atomic_read(&redundant_softirqs_off);
255 seq_printf(m, " chain lookup misses: %11u\n",
256 debug_atomic_read(&chain_lookup_misses));
257 seq_printf(m, " chain lookup hits: %11u\n",
258 debug_atomic_read(&chain_lookup_hits));
259 seq_printf(m, " cyclic checks: %11u\n",
260 debug_atomic_read(&nr_cyclic_checks));
261 seq_printf(m, " cyclic-check recursions: %11u\n",
262 debug_atomic_read(&nr_cyclic_check_recursions));
263 seq_printf(m, " find-mask forwards checks: %11u\n",
264 debug_atomic_read(&nr_find_usage_forwards_checks));
265 seq_printf(m, " find-mask forwards recursions: %11u\n",
266 debug_atomic_read(&nr_find_usage_forwards_recursions));
267 seq_printf(m, " find-mask backwards checks: %11u\n",
268 debug_atomic_read(&nr_find_usage_backwards_checks));
269 seq_printf(m, " find-mask backwards recursions:%11u\n",
270 debug_atomic_read(&nr_find_usage_backwards_recursions));
272 seq_printf(m, " hardirq on events: %11u\n", hi1);
273 seq_printf(m, " hardirq off events: %11u\n", hi2);
274 seq_printf(m, " redundant hardirq ons: %11u\n", hr1);
275 seq_printf(m, " redundant hardirq offs: %11u\n", hr2);
276 seq_printf(m, " softirq on events: %11u\n", si1);
277 seq_printf(m, " softirq off events: %11u\n", si2);
278 seq_printf(m, " redundant softirq ons: %11u\n", sr1);
279 seq_printf(m, " redundant softirq offs: %11u\n", sr2);
283 static int lockdep_stats_show(struct seq_file *m, void *v)
285 struct lock_class *class;
286 unsigned long nr_unused = 0, nr_uncategorized = 0,
287 nr_irq_safe = 0, nr_irq_unsafe = 0,
288 nr_softirq_safe = 0, nr_softirq_unsafe = 0,
289 nr_hardirq_safe = 0, nr_hardirq_unsafe = 0,
290 nr_irq_read_safe = 0, nr_irq_read_unsafe = 0,
291 nr_softirq_read_safe = 0, nr_softirq_read_unsafe = 0,
292 nr_hardirq_read_safe = 0, nr_hardirq_read_unsafe = 0,
293 sum_forward_deps = 0, factor = 0;
295 list_for_each_entry(class, &all_lock_classes, lock_entry) {
297 if (class->usage_mask == 0)
299 if (class->usage_mask == LOCKF_USED)
301 if (class->usage_mask & LOCKF_USED_IN_IRQ)
303 if (class->usage_mask & LOCKF_ENABLED_IRQ)
305 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
307 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ)
309 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
311 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ)
313 if (class->usage_mask & LOCKF_USED_IN_IRQ_READ)
315 if (class->usage_mask & LOCKF_ENABLED_IRQ_READ)
316 nr_irq_read_unsafe++;
317 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ)
318 nr_softirq_read_safe++;
319 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
320 nr_softirq_read_unsafe++;
321 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ)
322 nr_hardirq_read_safe++;
323 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
324 nr_hardirq_read_unsafe++;
326 #ifdef CONFIG_PROVE_LOCKING
327 sum_forward_deps += lockdep_count_forward_deps(class);
330 #ifdef CONFIG_DEBUG_LOCKDEP
331 DEBUG_LOCKS_WARN_ON(debug_atomic_read(&nr_unused_locks) != nr_unused);
333 seq_printf(m, " lock-classes: %11lu [max: %lu]\n",
334 nr_lock_classes, MAX_LOCKDEP_KEYS);
335 seq_printf(m, " direct dependencies: %11lu [max: %lu]\n",
336 nr_list_entries, MAX_LOCKDEP_ENTRIES);
337 seq_printf(m, " indirect dependencies: %11lu\n",
341 * Total number of dependencies:
343 * All irq-safe locks may nest inside irq-unsafe locks,
344 * plus all the other known dependencies:
346 seq_printf(m, " all direct dependencies: %11lu\n",
347 nr_irq_unsafe * nr_irq_safe +
348 nr_hardirq_unsafe * nr_hardirq_safe +
352 * Estimated factor between direct and indirect
356 factor = sum_forward_deps / nr_list_entries;
358 #ifdef CONFIG_PROVE_LOCKING
359 seq_printf(m, " dependency chains: %11lu [max: %lu]\n",
360 nr_lock_chains, MAX_LOCKDEP_CHAINS);
361 seq_printf(m, " dependency chain hlocks: %11d [max: %lu]\n",
362 nr_chain_hlocks, MAX_LOCKDEP_CHAIN_HLOCKS);
365 #ifdef CONFIG_TRACE_IRQFLAGS
366 seq_printf(m, " in-hardirq chains: %11u\n",
368 seq_printf(m, " in-softirq chains: %11u\n",
371 seq_printf(m, " in-process chains: %11u\n",
373 seq_printf(m, " stack-trace entries: %11lu [max: %lu]\n",
374 nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES);
375 seq_printf(m, " combined max dependencies: %11u\n",
376 (nr_hardirq_chains + 1) *
377 (nr_softirq_chains + 1) *
378 (nr_process_chains + 1)
380 seq_printf(m, " hardirq-safe locks: %11lu\n",
382 seq_printf(m, " hardirq-unsafe locks: %11lu\n",
384 seq_printf(m, " softirq-safe locks: %11lu\n",
386 seq_printf(m, " softirq-unsafe locks: %11lu\n",
388 seq_printf(m, " irq-safe locks: %11lu\n",
390 seq_printf(m, " irq-unsafe locks: %11lu\n",
393 seq_printf(m, " hardirq-read-safe locks: %11lu\n",
394 nr_hardirq_read_safe);
395 seq_printf(m, " hardirq-read-unsafe locks: %11lu\n",
396 nr_hardirq_read_unsafe);
397 seq_printf(m, " softirq-read-safe locks: %11lu\n",
398 nr_softirq_read_safe);
399 seq_printf(m, " softirq-read-unsafe locks: %11lu\n",
400 nr_softirq_read_unsafe);
401 seq_printf(m, " irq-read-safe locks: %11lu\n",
403 seq_printf(m, " irq-read-unsafe locks: %11lu\n",
406 seq_printf(m, " uncategorized locks: %11lu\n",
408 seq_printf(m, " unused locks: %11lu\n",
410 seq_printf(m, " max locking depth: %11u\n",
412 seq_printf(m, " max recursion depth: %11u\n",
413 max_recursion_depth);
414 seq_printf(m, " max bfs queue depth: %11u\n",
415 max_bfs_queue_depth);
416 lockdep_stats_debug_show(m);
417 seq_printf(m, " debug_locks: %11u\n",
423 static int lockdep_stats_open(struct inode *inode, struct file *file)
425 return single_open(file, lockdep_stats_show, NULL);
428 static const struct file_operations proc_lockdep_stats_operations = {
429 .open = lockdep_stats_open,
432 .release = single_release,
435 #ifdef CONFIG_LOCK_STAT
437 struct lock_stat_data {
438 struct lock_class *class;
439 struct lock_class_stats stats;
442 struct lock_stat_seq {
443 struct lock_stat_data *iter;
444 struct lock_stat_data *iter_end;
445 struct lock_stat_data stats[MAX_LOCKDEP_KEYS];
449 * sort on absolute number of contentions
451 static int lock_stat_cmp(const void *l, const void *r)
453 const struct lock_stat_data *dl = l, *dr = r;
454 unsigned long nl, nr;
456 nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr;
457 nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr;
462 static void seq_line(struct seq_file *m, char c, int offset, int length)
466 for (i = 0; i < offset; i++)
468 for (i = 0; i < length; i++)
469 seq_printf(m, "%c", c);
473 static void snprint_time(char *buf, size_t bufsiz, s64 nr)
478 nr += 5; /* for display rounding */
479 div = div_s64_rem(nr, 1000, &rem);
480 snprintf(buf, bufsiz, "%lld.%02d", (long long)div, (int)rem/10);
483 static void seq_time(struct seq_file *m, s64 time)
487 snprint_time(num, sizeof(num), time);
488 seq_printf(m, " %14s", num);
491 static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
493 seq_printf(m, "%14lu", lt->nr);
494 seq_time(m, lt->min);
495 seq_time(m, lt->max);
496 seq_time(m, lt->total);
499 static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
502 struct lock_class *class;
503 struct lock_class_stats *stats;
507 stats = &data->stats;
510 if (class->name_version > 1)
511 namelen -= 2; /* XXX truncates versions > 9 */
516 char str[KSYM_NAME_LEN];
517 const char *key_name;
519 key_name = __get_key_name(class->key, str);
520 snprintf(name, namelen, "%s", key_name);
522 snprintf(name, namelen, "%s", class->name);
524 namelen = strlen(name);
525 if (class->name_version > 1) {
526 snprintf(name+namelen, 3, "#%d", class->name_version);
529 if (class->subclass) {
530 snprintf(name+namelen, 3, "/%d", class->subclass);
534 if (stats->write_holdtime.nr) {
535 if (stats->read_holdtime.nr)
536 seq_printf(m, "%38s-W:", name);
538 seq_printf(m, "%40s:", name);
540 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
541 seq_lock_time(m, &stats->write_waittime);
542 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
543 seq_lock_time(m, &stats->write_holdtime);
547 if (stats->read_holdtime.nr) {
548 seq_printf(m, "%38s-R:", name);
549 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_read]);
550 seq_lock_time(m, &stats->read_waittime);
551 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_read]);
552 seq_lock_time(m, &stats->read_holdtime);
556 if (stats->read_waittime.nr + stats->write_waittime.nr == 0)
559 if (stats->read_holdtime.nr)
562 for (i = 0; i < LOCKSTAT_POINTS; i++) {
563 char sym[KSYM_SYMBOL_LEN];
566 if (class->contention_point[i] == 0)
570 seq_line(m, '-', 40-namelen, namelen);
572 sprint_symbol(sym, class->contention_point[i]);
573 snprintf(ip, sizeof(ip), "[<%p>]",
574 (void *)class->contention_point[i]);
575 seq_printf(m, "%40s %14lu %29s %s\n", name,
576 stats->contention_point[i],
579 for (i = 0; i < LOCKSTAT_POINTS; i++) {
580 char sym[KSYM_SYMBOL_LEN];
583 if (class->contending_point[i] == 0)
587 seq_line(m, '-', 40-namelen, namelen);
589 sprint_symbol(sym, class->contending_point[i]);
590 snprintf(ip, sizeof(ip), "[<%p>]",
591 (void *)class->contending_point[i]);
592 seq_printf(m, "%40s %14lu %29s %s\n", name,
593 stats->contending_point[i],
598 seq_line(m, '.', 0, 40 + 1 + 10 * (14 + 1));
603 static void seq_header(struct seq_file *m)
605 seq_printf(m, "lock_stat version 0.3\n");
607 if (unlikely(!debug_locks))
608 seq_printf(m, "*WARNING* lock debugging disabled!! - possibly due to a lockdep warning\n");
610 seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
611 seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s "
624 seq_line(m, '-', 0, 40 + 1 + 10 * (14 + 1));
628 static void *ls_start(struct seq_file *m, loff_t *pos)
630 struct lock_stat_seq *data = m->private;
633 return SEQ_START_TOKEN;
635 data->iter = data->stats + *pos;
636 if (data->iter >= data->iter_end)
642 static void *ls_next(struct seq_file *m, void *v, loff_t *pos)
644 struct lock_stat_seq *data = m->private;
648 if (v == SEQ_START_TOKEN)
649 data->iter = data->stats;
655 if (data->iter == data->iter_end)
661 static void ls_stop(struct seq_file *m, void *v)
665 static int ls_show(struct seq_file *m, void *v)
667 if (v == SEQ_START_TOKEN)
675 static struct seq_operations lockstat_ops = {
682 static int lock_stat_open(struct inode *inode, struct file *file)
685 struct lock_class *class;
686 struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq));
691 res = seq_open(file, &lockstat_ops);
693 struct lock_stat_data *iter = data->stats;
694 struct seq_file *m = file->private_data;
697 list_for_each_entry(class, &all_lock_classes, lock_entry) {
699 iter->stats = lock_stats(class);
702 data->iter_end = iter;
704 sort(data->stats, data->iter_end - data->iter,
705 sizeof(struct lock_stat_data),
706 lock_stat_cmp, NULL);
715 static ssize_t lock_stat_write(struct file *file, const char __user *buf,
716 size_t count, loff_t *ppos)
718 struct lock_class *class;
722 if (get_user(c, buf))
728 list_for_each_entry(class, &all_lock_classes, lock_entry)
729 clear_lock_stats(class);
734 static int lock_stat_release(struct inode *inode, struct file *file)
736 struct seq_file *seq = file->private_data;
740 return seq_release(inode, file);
743 static const struct file_operations proc_lock_stat_operations = {
744 .open = lock_stat_open,
745 .write = lock_stat_write,
748 .release = lock_stat_release,
750 #endif /* CONFIG_LOCK_STAT */
752 static int __init lockdep_proc_init(void)
754 proc_create("lockdep", S_IRUSR, NULL, &proc_lockdep_operations);
755 #ifdef CONFIG_PROVE_LOCKING
756 proc_create("lockdep_chains", S_IRUSR, NULL,
757 &proc_lockdep_chains_operations);
759 proc_create("lockdep_stats", S_IRUSR, NULL,
760 &proc_lockdep_stats_operations);
762 #ifdef CONFIG_LOCK_STAT
763 proc_create("lock_stat", S_IRUSR, NULL, &proc_lock_stat_operations);
769 __initcall(lockdep_proc_init);