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
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 <linux/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 if (nr_chain_hlocks
> MAX_LOCKDEP_CHAIN_HLOCKS
)
145 seq_printf(m
, "(buggered) ");
146 seq_printf(m
, "all lock chains:\n");
150 seq_printf(m
, "irq_context: %d\n", chain
->irq_context
);
152 for (i
= 0; i
< chain
->depth
; i
++) {
153 class = lock_chain_get_class(chain
, i
);
157 seq_printf(m
, "[%p] ", class->key
);
158 print_name(m
, class);
166 static const struct seq_operations lockdep_chains_ops
= {
173 static int lockdep_chains_open(struct inode
*inode
, struct file
*file
)
175 return seq_open(file
, &lockdep_chains_ops
);
178 static const struct file_operations proc_lockdep_chains_operations
= {
179 .open
= lockdep_chains_open
,
182 .release
= seq_release
,
184 #endif /* CONFIG_PROVE_LOCKING */
186 static void lockdep_stats_debug_show(struct seq_file
*m
)
188 #ifdef CONFIG_DEBUG_LOCKDEP
189 unsigned long long hi1
= debug_atomic_read(hardirqs_on_events
),
190 hi2
= debug_atomic_read(hardirqs_off_events
),
191 hr1
= debug_atomic_read(redundant_hardirqs_on
),
192 hr2
= debug_atomic_read(redundant_hardirqs_off
),
193 si1
= debug_atomic_read(softirqs_on_events
),
194 si2
= debug_atomic_read(softirqs_off_events
),
195 sr1
= debug_atomic_read(redundant_softirqs_on
),
196 sr2
= debug_atomic_read(redundant_softirqs_off
);
198 seq_printf(m
, " chain lookup misses: %11llu\n",
199 debug_atomic_read(chain_lookup_misses
));
200 seq_printf(m
, " chain lookup hits: %11llu\n",
201 debug_atomic_read(chain_lookup_hits
));
202 seq_printf(m
, " cyclic checks: %11llu\n",
203 debug_atomic_read(nr_cyclic_checks
));
204 seq_printf(m
, " find-mask forwards checks: %11llu\n",
205 debug_atomic_read(nr_find_usage_forwards_checks
));
206 seq_printf(m
, " find-mask backwards checks: %11llu\n",
207 debug_atomic_read(nr_find_usage_backwards_checks
));
209 seq_printf(m
, " hardirq on events: %11llu\n", hi1
);
210 seq_printf(m
, " hardirq off events: %11llu\n", hi2
);
211 seq_printf(m
, " redundant hardirq ons: %11llu\n", hr1
);
212 seq_printf(m
, " redundant hardirq offs: %11llu\n", hr2
);
213 seq_printf(m
, " softirq on events: %11llu\n", si1
);
214 seq_printf(m
, " softirq off events: %11llu\n", si2
);
215 seq_printf(m
, " redundant softirq ons: %11llu\n", sr1
);
216 seq_printf(m
, " redundant softirq offs: %11llu\n", sr2
);
220 static int lockdep_stats_show(struct seq_file
*m
, void *v
)
222 struct lock_class
*class;
223 unsigned long nr_unused
= 0, nr_uncategorized
= 0,
224 nr_irq_safe
= 0, nr_irq_unsafe
= 0,
225 nr_softirq_safe
= 0, nr_softirq_unsafe
= 0,
226 nr_hardirq_safe
= 0, nr_hardirq_unsafe
= 0,
227 nr_irq_read_safe
= 0, nr_irq_read_unsafe
= 0,
228 nr_softirq_read_safe
= 0, nr_softirq_read_unsafe
= 0,
229 nr_hardirq_read_safe
= 0, nr_hardirq_read_unsafe
= 0,
230 sum_forward_deps
= 0;
232 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
234 if (class->usage_mask
== 0)
236 if (class->usage_mask
== LOCKF_USED
)
238 if (class->usage_mask
& LOCKF_USED_IN_IRQ
)
240 if (class->usage_mask
& LOCKF_ENABLED_IRQ
)
242 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ
)
244 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQ
)
246 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ
)
248 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQ
)
250 if (class->usage_mask
& LOCKF_USED_IN_IRQ_READ
)
252 if (class->usage_mask
& LOCKF_ENABLED_IRQ_READ
)
253 nr_irq_read_unsafe
++;
254 if (class->usage_mask
& LOCKF_USED_IN_SOFTIRQ_READ
)
255 nr_softirq_read_safe
++;
256 if (class->usage_mask
& LOCKF_ENABLED_SOFTIRQ_READ
)
257 nr_softirq_read_unsafe
++;
258 if (class->usage_mask
& LOCKF_USED_IN_HARDIRQ_READ
)
259 nr_hardirq_read_safe
++;
260 if (class->usage_mask
& LOCKF_ENABLED_HARDIRQ_READ
)
261 nr_hardirq_read_unsafe
++;
263 #ifdef CONFIG_PROVE_LOCKING
264 sum_forward_deps
+= lockdep_count_forward_deps(class);
267 #ifdef CONFIG_DEBUG_LOCKDEP
268 DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks
) != nr_unused
);
270 seq_printf(m
, " lock-classes: %11lu [max: %lu]\n",
271 nr_lock_classes
, MAX_LOCKDEP_KEYS
);
272 seq_printf(m
, " direct dependencies: %11lu [max: %lu]\n",
273 nr_list_entries
, MAX_LOCKDEP_ENTRIES
);
274 seq_printf(m
, " indirect dependencies: %11lu\n",
278 * Total number of dependencies:
280 * All irq-safe locks may nest inside irq-unsafe locks,
281 * plus all the other known dependencies:
283 seq_printf(m
, " all direct dependencies: %11lu\n",
284 nr_irq_unsafe
* nr_irq_safe
+
285 nr_hardirq_unsafe
* nr_hardirq_safe
+
288 #ifdef CONFIG_PROVE_LOCKING
289 seq_printf(m
, " dependency chains: %11lu [max: %lu]\n",
290 nr_lock_chains
, MAX_LOCKDEP_CHAINS
);
291 seq_printf(m
, " dependency chain hlocks: %11d [max: %lu]\n",
292 nr_chain_hlocks
, MAX_LOCKDEP_CHAIN_HLOCKS
);
295 #ifdef CONFIG_TRACE_IRQFLAGS
296 seq_printf(m
, " in-hardirq chains: %11u\n",
298 seq_printf(m
, " in-softirq chains: %11u\n",
301 seq_printf(m
, " in-process chains: %11u\n",
303 seq_printf(m
, " stack-trace entries: %11lu [max: %lu]\n",
304 nr_stack_trace_entries
, MAX_STACK_TRACE_ENTRIES
);
305 seq_printf(m
, " combined max dependencies: %11u\n",
306 (nr_hardirq_chains
+ 1) *
307 (nr_softirq_chains
+ 1) *
308 (nr_process_chains
+ 1)
310 seq_printf(m
, " hardirq-safe locks: %11lu\n",
312 seq_printf(m
, " hardirq-unsafe locks: %11lu\n",
314 seq_printf(m
, " softirq-safe locks: %11lu\n",
316 seq_printf(m
, " softirq-unsafe locks: %11lu\n",
318 seq_printf(m
, " irq-safe locks: %11lu\n",
320 seq_printf(m
, " irq-unsafe locks: %11lu\n",
323 seq_printf(m
, " hardirq-read-safe locks: %11lu\n",
324 nr_hardirq_read_safe
);
325 seq_printf(m
, " hardirq-read-unsafe locks: %11lu\n",
326 nr_hardirq_read_unsafe
);
327 seq_printf(m
, " softirq-read-safe locks: %11lu\n",
328 nr_softirq_read_safe
);
329 seq_printf(m
, " softirq-read-unsafe locks: %11lu\n",
330 nr_softirq_read_unsafe
);
331 seq_printf(m
, " irq-read-safe locks: %11lu\n",
333 seq_printf(m
, " irq-read-unsafe locks: %11lu\n",
336 seq_printf(m
, " uncategorized locks: %11lu\n",
338 seq_printf(m
, " unused locks: %11lu\n",
340 seq_printf(m
, " max locking depth: %11u\n",
342 #ifdef CONFIG_PROVE_LOCKING
343 seq_printf(m
, " max bfs queue depth: %11u\n",
344 max_bfs_queue_depth
);
346 lockdep_stats_debug_show(m
);
347 seq_printf(m
, " debug_locks: %11u\n",
353 static int lockdep_stats_open(struct inode
*inode
, struct file
*file
)
355 return single_open(file
, lockdep_stats_show
, NULL
);
358 static const struct file_operations proc_lockdep_stats_operations
= {
359 .open
= lockdep_stats_open
,
362 .release
= single_release
,
365 #ifdef CONFIG_LOCK_STAT
367 struct lock_stat_data
{
368 struct lock_class
*class;
369 struct lock_class_stats stats
;
372 struct lock_stat_seq
{
373 struct lock_stat_data
*iter_end
;
374 struct lock_stat_data stats
[MAX_LOCKDEP_KEYS
];
378 * sort on absolute number of contentions
380 static int lock_stat_cmp(const void *l
, const void *r
)
382 const struct lock_stat_data
*dl
= l
, *dr
= r
;
383 unsigned long nl
, nr
;
385 nl
= dl
->stats
.read_waittime
.nr
+ dl
->stats
.write_waittime
.nr
;
386 nr
= dr
->stats
.read_waittime
.nr
+ dr
->stats
.write_waittime
.nr
;
391 static void seq_line(struct seq_file
*m
, char c
, int offset
, int length
)
395 for (i
= 0; i
< offset
; i
++)
397 for (i
= 0; i
< length
; i
++)
398 seq_printf(m
, "%c", c
);
402 static void snprint_time(char *buf
, size_t bufsiz
, s64 nr
)
407 nr
+= 5; /* for display rounding */
408 div
= div_s64_rem(nr
, 1000, &rem
);
409 snprintf(buf
, bufsiz
, "%lld.%02d", (long long)div
, (int)rem
/10);
412 static void seq_time(struct seq_file
*m
, s64 time
)
416 snprint_time(num
, sizeof(num
), time
);
417 seq_printf(m
, " %14s", num
);
420 static void seq_lock_time(struct seq_file
*m
, struct lock_time
*lt
)
422 seq_printf(m
, "%14lu", lt
->nr
);
423 seq_time(m
, lt
->min
);
424 seq_time(m
, lt
->max
);
425 seq_time(m
, lt
->total
);
426 seq_time(m
, lt
->nr
? div_s64(lt
->total
, lt
->nr
) : 0);
429 static void seq_stats(struct seq_file
*m
, struct lock_stat_data
*data
)
431 struct lockdep_subclass_key
*ckey
;
432 struct lock_class_stats
*stats
;
433 struct lock_class
*class;
439 stats
= &data
->stats
;
442 if (class->name_version
> 1)
443 namelen
-= 2; /* XXX truncates versions > 9 */
447 rcu_read_lock_sched();
448 cname
= rcu_dereference_sched(class->name
);
449 ckey
= rcu_dereference_sched(class->key
);
451 if (!cname
&& !ckey
) {
452 rcu_read_unlock_sched();
456 char str
[KSYM_NAME_LEN
];
457 const char *key_name
;
459 key_name
= __get_key_name(ckey
, str
);
460 snprintf(name
, namelen
, "%s", key_name
);
462 snprintf(name
, namelen
, "%s", cname
);
464 rcu_read_unlock_sched();
466 namelen
= strlen(name
);
467 if (class->name_version
> 1) {
468 snprintf(name
+namelen
, 3, "#%d", class->name_version
);
471 if (class->subclass
) {
472 snprintf(name
+namelen
, 3, "/%d", class->subclass
);
476 if (stats
->write_holdtime
.nr
) {
477 if (stats
->read_holdtime
.nr
)
478 seq_printf(m
, "%38s-W:", name
);
480 seq_printf(m
, "%40s:", name
);
482 seq_printf(m
, "%14lu ", stats
->bounces
[bounce_contended_write
]);
483 seq_lock_time(m
, &stats
->write_waittime
);
484 seq_printf(m
, " %14lu ", stats
->bounces
[bounce_acquired_write
]);
485 seq_lock_time(m
, &stats
->write_holdtime
);
489 if (stats
->read_holdtime
.nr
) {
490 seq_printf(m
, "%38s-R:", name
);
491 seq_printf(m
, "%14lu ", stats
->bounces
[bounce_contended_read
]);
492 seq_lock_time(m
, &stats
->read_waittime
);
493 seq_printf(m
, " %14lu ", stats
->bounces
[bounce_acquired_read
]);
494 seq_lock_time(m
, &stats
->read_holdtime
);
498 if (stats
->read_waittime
.nr
+ stats
->write_waittime
.nr
== 0)
501 if (stats
->read_holdtime
.nr
)
504 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
507 if (class->contention_point
[i
] == 0)
511 seq_line(m
, '-', 40-namelen
, namelen
);
513 snprintf(ip
, sizeof(ip
), "[<%p>]",
514 (void *)class->contention_point
[i
]);
515 seq_printf(m
, "%40s %14lu %29s %pS\n",
516 name
, stats
->contention_point
[i
],
517 ip
, (void *)class->contention_point
[i
]);
519 for (i
= 0; i
< LOCKSTAT_POINTS
; i
++) {
522 if (class->contending_point
[i
] == 0)
526 seq_line(m
, '-', 40-namelen
, namelen
);
528 snprintf(ip
, sizeof(ip
), "[<%p>]",
529 (void *)class->contending_point
[i
]);
530 seq_printf(m
, "%40s %14lu %29s %pS\n",
531 name
, stats
->contending_point
[i
],
532 ip
, (void *)class->contending_point
[i
]);
536 seq_line(m
, '.', 0, 40 + 1 + 12 * (14 + 1));
541 static void seq_header(struct seq_file
*m
)
543 seq_puts(m
, "lock_stat version 0.4\n");
545 if (unlikely(!debug_locks
))
546 seq_printf(m
, "*WARNING* lock debugging disabled!! - possibly due to a lockdep warning\n");
548 seq_line(m
, '-', 0, 40 + 1 + 12 * (14 + 1));
549 seq_printf(m
, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
564 seq_line(m
, '-', 0, 40 + 1 + 12 * (14 + 1));
568 static void *ls_start(struct seq_file
*m
, loff_t
*pos
)
570 struct lock_stat_seq
*data
= m
->private;
571 struct lock_stat_data
*iter
;
574 return SEQ_START_TOKEN
;
576 iter
= data
->stats
+ (*pos
- 1);
577 if (iter
>= data
->iter_end
)
583 static void *ls_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
586 return ls_start(m
, pos
);
589 static void ls_stop(struct seq_file
*m
, void *v
)
593 static int ls_show(struct seq_file
*m
, void *v
)
595 if (v
== SEQ_START_TOKEN
)
603 static const struct seq_operations lockstat_ops
= {
610 static int lock_stat_open(struct inode
*inode
, struct file
*file
)
613 struct lock_class
*class;
614 struct lock_stat_seq
*data
= vmalloc(sizeof(struct lock_stat_seq
));
619 res
= seq_open(file
, &lockstat_ops
);
621 struct lock_stat_data
*iter
= data
->stats
;
622 struct seq_file
*m
= file
->private_data
;
624 list_for_each_entry(class, &all_lock_classes
, lock_entry
) {
626 iter
->stats
= lock_stats(class);
629 data
->iter_end
= iter
;
631 sort(data
->stats
, data
->iter_end
- data
->stats
,
632 sizeof(struct lock_stat_data
),
633 lock_stat_cmp
, NULL
);
642 static ssize_t
lock_stat_write(struct file
*file
, const char __user
*buf
,
643 size_t count
, loff_t
*ppos
)
645 struct lock_class
*class;
649 if (get_user(c
, buf
))
655 list_for_each_entry(class, &all_lock_classes
, lock_entry
)
656 clear_lock_stats(class);
661 static int lock_stat_release(struct inode
*inode
, struct file
*file
)
663 struct seq_file
*seq
= file
->private_data
;
666 return seq_release(inode
, file
);
669 static const struct file_operations proc_lock_stat_operations
= {
670 .open
= lock_stat_open
,
671 .write
= lock_stat_write
,
674 .release
= lock_stat_release
,
676 #endif /* CONFIG_LOCK_STAT */
678 static int __init
lockdep_proc_init(void)
680 proc_create("lockdep", S_IRUSR
, NULL
, &proc_lockdep_operations
);
681 #ifdef CONFIG_PROVE_LOCKING
682 proc_create("lockdep_chains", S_IRUSR
, NULL
,
683 &proc_lockdep_chains_operations
);
685 proc_create("lockdep_stats", S_IRUSR
, NULL
,
686 &proc_lockdep_stats_operations
);
688 #ifdef CONFIG_LOCK_STAT
689 proc_create("lock_stat", S_IRUSR
| S_IWUSR
, NULL
,
690 &proc_lock_stat_operations
);
696 __initcall(lockdep_proc_init
);