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/export.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 return seq_list_next(v, &all_lock_classes, pos);
31 static void *l_start(struct seq_file *m, loff_t *pos)
33 return seq_list_start_head(&all_lock_classes, *pos);
36 static void l_stop(struct seq_file *m, void *v)
40 static void print_name(struct seq_file *m, struct lock_class *class)
42 char str[KSYM_NAME_LEN];
43 const char *name = class->name;
46 name = __get_key_name(class->key, str);
47 seq_printf(m, "%s", name);
49 seq_printf(m, "%s", name);
50 if (class->name_version > 1)
51 seq_printf(m, "#%d", class->name_version);
53 seq_printf(m, "/%d", class->subclass);
57 static int l_show(struct seq_file *m, void *v)
59 struct lock_class *class = list_entry(v, struct lock_class, lock_entry);
60 struct lock_list *entry;
61 char usage[LOCK_USAGE_CHARS];
63 if (v == &all_lock_classes) {
64 seq_printf(m, "all lock classes:\n");
68 seq_printf(m, "%p", class->key);
69 #ifdef CONFIG_DEBUG_LOCKDEP
70 seq_printf(m, " OPS:%8ld", class->ops);
72 #ifdef CONFIG_PROVE_LOCKING
73 seq_printf(m, " FD:%5ld", lockdep_count_forward_deps(class));
74 seq_printf(m, " BD:%5ld", lockdep_count_backward_deps(class));
77 get_usage_chars(class, usage);
78 seq_printf(m, " %s", usage);
84 list_for_each_entry(entry, &class->locks_after, entry) {
85 if (entry->distance == 1) {
86 seq_printf(m, " -> [%p] ", entry->class->key);
87 print_name(m, entry->class);
96 static const struct seq_operations lockdep_ops = {
103 static int lockdep_open(struct inode *inode, struct file *file)
105 return seq_open(file, &lockdep_ops);
108 static const struct file_operations proc_lockdep_operations = {
109 .open = lockdep_open,
112 .release = seq_release,
115 #ifdef CONFIG_PROVE_LOCKING
116 static void *lc_start(struct seq_file *m, loff_t *pos)
119 return SEQ_START_TOKEN;
121 if (*pos - 1 < nr_lock_chains)
122 return lock_chains + (*pos - 1);
127 static void *lc_next(struct seq_file *m, void *v, loff_t *pos)
130 return lc_start(m, pos);
133 static void lc_stop(struct seq_file *m, void *v)
137 static int lc_show(struct seq_file *m, void *v)
139 struct lock_chain *chain = v;
140 struct lock_class *class;
143 if (v == SEQ_START_TOKEN) {
144 seq_printf(m, "all lock chains:\n");
148 seq_printf(m, "irq_context: %d\n", chain->irq_context);
150 for (i = 0; i < chain->depth; i++) {
151 class = lock_chain_get_class(chain, i);
155 seq_printf(m, "[%p] ", class->key);
156 print_name(m, class);
164 static const struct seq_operations lockdep_chains_ops = {
171 static int lockdep_chains_open(struct inode *inode, struct file *file)
173 return seq_open(file, &lockdep_chains_ops);
176 static const struct file_operations proc_lockdep_chains_operations = {
177 .open = lockdep_chains_open,
180 .release = seq_release,
182 #endif /* CONFIG_PROVE_LOCKING */
184 static void lockdep_stats_debug_show(struct seq_file *m)
186 #ifdef CONFIG_DEBUG_LOCKDEP
187 unsigned long long hi1 = debug_atomic_read(hardirqs_on_events),
188 hi2 = debug_atomic_read(hardirqs_off_events),
189 hr1 = debug_atomic_read(redundant_hardirqs_on),
190 hr2 = debug_atomic_read(redundant_hardirqs_off),
191 si1 = debug_atomic_read(softirqs_on_events),
192 si2 = debug_atomic_read(softirqs_off_events),
193 sr1 = debug_atomic_read(redundant_softirqs_on),
194 sr2 = debug_atomic_read(redundant_softirqs_off);
196 seq_printf(m, " chain lookup misses: %11llu\n",
197 debug_atomic_read(chain_lookup_misses));
198 seq_printf(m, " chain lookup hits: %11llu\n",
199 debug_atomic_read(chain_lookup_hits));
200 seq_printf(m, " cyclic checks: %11llu\n",
201 debug_atomic_read(nr_cyclic_checks));
202 seq_printf(m, " find-mask forwards checks: %11llu\n",
203 debug_atomic_read(nr_find_usage_forwards_checks));
204 seq_printf(m, " find-mask backwards checks: %11llu\n",
205 debug_atomic_read(nr_find_usage_backwards_checks));
207 seq_printf(m, " hardirq on events: %11llu\n", hi1);
208 seq_printf(m, " hardirq off events: %11llu\n", hi2);
209 seq_printf(m, " redundant hardirq ons: %11llu\n", hr1);
210 seq_printf(m, " redundant hardirq offs: %11llu\n", hr2);
211 seq_printf(m, " softirq on events: %11llu\n", si1);
212 seq_printf(m, " softirq off events: %11llu\n", si2);
213 seq_printf(m, " redundant softirq ons: %11llu\n", sr1);
214 seq_printf(m, " redundant softirq offs: %11llu\n", sr2);
218 static int lockdep_stats_show(struct seq_file *m, void *v)
220 struct lock_class *class;
221 unsigned long nr_unused = 0, nr_uncategorized = 0,
222 nr_irq_safe = 0, nr_irq_unsafe = 0,
223 nr_softirq_safe = 0, nr_softirq_unsafe = 0,
224 nr_hardirq_safe = 0, nr_hardirq_unsafe = 0,
225 nr_irq_read_safe = 0, nr_irq_read_unsafe = 0,
226 nr_softirq_read_safe = 0, nr_softirq_read_unsafe = 0,
227 nr_hardirq_read_safe = 0, nr_hardirq_read_unsafe = 0,
228 sum_forward_deps = 0;
230 list_for_each_entry(class, &all_lock_classes, lock_entry) {
232 if (class->usage_mask == 0)
234 if (class->usage_mask == LOCKF_USED)
236 if (class->usage_mask & LOCKF_USED_IN_IRQ)
238 if (class->usage_mask & LOCKF_ENABLED_IRQ)
240 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
242 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ)
244 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
246 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ)
248 if (class->usage_mask & LOCKF_USED_IN_IRQ_READ)
250 if (class->usage_mask & LOCKF_ENABLED_IRQ_READ)
251 nr_irq_read_unsafe++;
252 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ)
253 nr_softirq_read_safe++;
254 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
255 nr_softirq_read_unsafe++;
256 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ)
257 nr_hardirq_read_safe++;
258 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
259 nr_hardirq_read_unsafe++;
261 #ifdef CONFIG_PROVE_LOCKING
262 sum_forward_deps += lockdep_count_forward_deps(class);
265 #ifdef CONFIG_DEBUG_LOCKDEP
266 DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused);
268 seq_printf(m, " lock-classes: %11lu [max: %lu]\n",
269 nr_lock_classes, MAX_LOCKDEP_KEYS);
270 seq_printf(m, " direct dependencies: %11lu [max: %lu]\n",
271 nr_list_entries, MAX_LOCKDEP_ENTRIES);
272 seq_printf(m, " indirect dependencies: %11lu\n",
276 * Total number of dependencies:
278 * All irq-safe locks may nest inside irq-unsafe locks,
279 * plus all the other known dependencies:
281 seq_printf(m, " all direct dependencies: %11lu\n",
282 nr_irq_unsafe * nr_irq_safe +
283 nr_hardirq_unsafe * nr_hardirq_safe +
286 #ifdef CONFIG_PROVE_LOCKING
287 seq_printf(m, " dependency chains: %11lu [max: %lu]\n",
288 nr_lock_chains, MAX_LOCKDEP_CHAINS);
289 seq_printf(m, " dependency chain hlocks: %11d [max: %lu]\n",
290 nr_chain_hlocks, MAX_LOCKDEP_CHAIN_HLOCKS);
293 #ifdef CONFIG_TRACE_IRQFLAGS
294 seq_printf(m, " in-hardirq chains: %11u\n",
296 seq_printf(m, " in-softirq chains: %11u\n",
299 seq_printf(m, " in-process chains: %11u\n",
301 seq_printf(m, " stack-trace entries: %11lu [max: %lu]\n",
302 nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES);
303 seq_printf(m, " combined max dependencies: %11u\n",
304 (nr_hardirq_chains + 1) *
305 (nr_softirq_chains + 1) *
306 (nr_process_chains + 1)
308 seq_printf(m, " hardirq-safe locks: %11lu\n",
310 seq_printf(m, " hardirq-unsafe locks: %11lu\n",
312 seq_printf(m, " softirq-safe locks: %11lu\n",
314 seq_printf(m, " softirq-unsafe locks: %11lu\n",
316 seq_printf(m, " irq-safe locks: %11lu\n",
318 seq_printf(m, " irq-unsafe locks: %11lu\n",
321 seq_printf(m, " hardirq-read-safe locks: %11lu\n",
322 nr_hardirq_read_safe);
323 seq_printf(m, " hardirq-read-unsafe locks: %11lu\n",
324 nr_hardirq_read_unsafe);
325 seq_printf(m, " softirq-read-safe locks: %11lu\n",
326 nr_softirq_read_safe);
327 seq_printf(m, " softirq-read-unsafe locks: %11lu\n",
328 nr_softirq_read_unsafe);
329 seq_printf(m, " irq-read-safe locks: %11lu\n",
331 seq_printf(m, " irq-read-unsafe locks: %11lu\n",
334 seq_printf(m, " uncategorized locks: %11lu\n",
336 seq_printf(m, " unused locks: %11lu\n",
338 seq_printf(m, " max locking depth: %11u\n",
340 #ifdef CONFIG_PROVE_LOCKING
341 seq_printf(m, " max bfs queue depth: %11u\n",
342 max_bfs_queue_depth);
344 lockdep_stats_debug_show(m);
345 seq_printf(m, " debug_locks: %11u\n",
351 static int lockdep_stats_open(struct inode *inode, struct file *file)
353 return single_open(file, lockdep_stats_show, NULL);
356 static const struct file_operations proc_lockdep_stats_operations = {
357 .open = lockdep_stats_open,
360 .release = single_release,
363 #ifdef CONFIG_LOCK_STAT
365 struct lock_stat_data {
366 struct lock_class *class;
367 struct lock_class_stats stats;
370 struct lock_stat_seq {
371 struct lock_stat_data *iter_end;
372 struct lock_stat_data stats[MAX_LOCKDEP_KEYS];
376 * sort on absolute number of contentions
378 static int lock_stat_cmp(const void *l, const void *r)
380 const struct lock_stat_data *dl = l, *dr = r;
381 unsigned long nl, nr;
383 nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr;
384 nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr;
389 static void seq_line(struct seq_file *m, char c, int offset, int length)
393 for (i = 0; i < offset; i++)
395 for (i = 0; i < length; i++)
396 seq_printf(m, "%c", c);
400 static void snprint_time(char *buf, size_t bufsiz, s64 nr)
405 nr += 5; /* for display rounding */
406 div = div_s64_rem(nr, 1000, &rem);
407 snprintf(buf, bufsiz, "%lld.%02d", (long long)div, (int)rem/10);
410 static void seq_time(struct seq_file *m, s64 time)
414 snprint_time(num, sizeof(num), time);
415 seq_printf(m, " %14s", num);
418 static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
420 seq_printf(m, "%14lu", lt->nr);
421 seq_time(m, lt->min);
422 seq_time(m, lt->max);
423 seq_time(m, lt->total);
424 seq_time(m, lt->nr ? div_s64(lt->total, lt->nr) : 0);
427 static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
429 struct lockdep_subclass_key *ckey;
430 struct lock_class_stats *stats;
431 struct lock_class *class;
437 stats = &data->stats;
440 if (class->name_version > 1)
441 namelen -= 2; /* XXX truncates versions > 9 */
445 rcu_read_lock_sched();
446 cname = rcu_dereference_sched(class->name);
447 ckey = rcu_dereference_sched(class->key);
449 if (!cname && !ckey) {
450 rcu_read_unlock_sched();
454 char str[KSYM_NAME_LEN];
455 const char *key_name;
457 key_name = __get_key_name(ckey, str);
458 snprintf(name, namelen, "%s", key_name);
460 snprintf(name, namelen, "%s", cname);
462 rcu_read_unlock_sched();
464 namelen = strlen(name);
465 if (class->name_version > 1) {
466 snprintf(name+namelen, 3, "#%d", class->name_version);
469 if (class->subclass) {
470 snprintf(name+namelen, 3, "/%d", class->subclass);
474 if (stats->write_holdtime.nr) {
475 if (stats->read_holdtime.nr)
476 seq_printf(m, "%38s-W:", name);
478 seq_printf(m, "%40s:", name);
480 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
481 seq_lock_time(m, &stats->write_waittime);
482 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
483 seq_lock_time(m, &stats->write_holdtime);
487 if (stats->read_holdtime.nr) {
488 seq_printf(m, "%38s-R:", name);
489 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_read]);
490 seq_lock_time(m, &stats->read_waittime);
491 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_read]);
492 seq_lock_time(m, &stats->read_holdtime);
496 if (stats->read_waittime.nr + stats->write_waittime.nr == 0)
499 if (stats->read_holdtime.nr)
502 for (i = 0; i < LOCKSTAT_POINTS; i++) {
505 if (class->contention_point[i] == 0)
509 seq_line(m, '-', 40-namelen, namelen);
511 snprintf(ip, sizeof(ip), "[<%p>]",
512 (void *)class->contention_point[i]);
513 seq_printf(m, "%40s %14lu %29s %pS\n",
514 name, stats->contention_point[i],
515 ip, (void *)class->contention_point[i]);
517 for (i = 0; i < LOCKSTAT_POINTS; i++) {
520 if (class->contending_point[i] == 0)
524 seq_line(m, '-', 40-namelen, namelen);
526 snprintf(ip, sizeof(ip), "[<%p>]",
527 (void *)class->contending_point[i]);
528 seq_printf(m, "%40s %14lu %29s %pS\n",
529 name, stats->contending_point[i],
530 ip, (void *)class->contending_point[i]);
534 seq_line(m, '.', 0, 40 + 1 + 12 * (14 + 1));
539 static void seq_header(struct seq_file *m)
541 seq_puts(m, "lock_stat version 0.4\n");
543 if (unlikely(!debug_locks))
544 seq_printf(m, "*WARNING* lock debugging disabled!! - possibly due to a lockdep warning\n");
546 seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
547 seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
562 seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
566 static void *ls_start(struct seq_file *m, loff_t *pos)
568 struct lock_stat_seq *data = m->private;
569 struct lock_stat_data *iter;
572 return SEQ_START_TOKEN;
574 iter = data->stats + (*pos - 1);
575 if (iter >= data->iter_end)
581 static void *ls_next(struct seq_file *m, void *v, loff_t *pos)
584 return ls_start(m, pos);
587 static void ls_stop(struct seq_file *m, void *v)
591 static int ls_show(struct seq_file *m, void *v)
593 if (v == SEQ_START_TOKEN)
601 static const struct seq_operations lockstat_ops = {
608 static int lock_stat_open(struct inode *inode, struct file *file)
611 struct lock_class *class;
612 struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq));
617 res = seq_open(file, &lockstat_ops);
619 struct lock_stat_data *iter = data->stats;
620 struct seq_file *m = file->private_data;
622 list_for_each_entry(class, &all_lock_classes, lock_entry) {
624 iter->stats = lock_stats(class);
627 data->iter_end = iter;
629 sort(data->stats, data->iter_end - data->stats,
630 sizeof(struct lock_stat_data),
631 lock_stat_cmp, NULL);
640 static ssize_t lock_stat_write(struct file *file, const char __user *buf,
641 size_t count, loff_t *ppos)
643 struct lock_class *class;
647 if (get_user(c, buf))
653 list_for_each_entry(class, &all_lock_classes, lock_entry)
654 clear_lock_stats(class);
659 static int lock_stat_release(struct inode *inode, struct file *file)
661 struct seq_file *seq = file->private_data;
664 return seq_release(inode, file);
667 static const struct file_operations proc_lock_stat_operations = {
668 .open = lock_stat_open,
669 .write = lock_stat_write,
672 .release = lock_stat_release,
674 #endif /* CONFIG_LOCK_STAT */
676 static int __init lockdep_proc_init(void)
678 proc_create("lockdep", S_IRUSR, NULL, &proc_lockdep_operations);
679 #ifdef CONFIG_PROVE_LOCKING
680 proc_create("lockdep_chains", S_IRUSR, NULL,
681 &proc_lockdep_chains_operations);
683 proc_create("lockdep_stats", S_IRUSR, NULL,
684 &proc_lockdep_stats_operations);
686 #ifdef CONFIG_LOCK_STAT
687 proc_create("lock_stat", S_IRUSR | S_IWUSR, NULL,
688 &proc_lock_stat_operations);
694 __initcall(lockdep_proc_init);