ccb5292d1e1944bae1150d14f4103588aea1b1f5
[releases.git] / lockdep_proc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * kernel/lockdep_proc.c
4  *
5  * Runtime locking correctness validator
6  *
7  * Started by Ingo Molnar:
8  *
9  *  Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
10  *  Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
11  *
12  * Code for /proc/lockdep and /proc/lockdep_stats:
13  *
14  */
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>
24
25 #include "lockdep_internals.h"
26
27 /*
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.
33  */
34 #define iterate_lock_classes(idx, class)                                \
35         for (idx = 0, class = lock_classes; idx <= max_lock_class_idx;  \
36              idx++, class++)
37
38 static void *l_next(struct seq_file *m, void *v, loff_t *pos)
39 {
40         struct lock_class *class = v;
41
42         ++class;
43         *pos = class - lock_classes;
44         return (*pos > max_lock_class_idx) ? NULL : class;
45 }
46
47 static void *l_start(struct seq_file *m, loff_t *pos)
48 {
49         unsigned long idx = *pos;
50
51         if (idx > max_lock_class_idx)
52                 return NULL;
53         return lock_classes + idx;
54 }
55
56 static void l_stop(struct seq_file *m, void *v)
57 {
58 }
59
60 static void print_name(struct seq_file *m, struct lock_class *class)
61 {
62         char str[KSYM_NAME_LEN];
63         const char *name = class->name;
64
65         if (!name) {
66                 name = __get_key_name(class->key, str);
67                 seq_printf(m, "%s", name);
68         } else{
69                 seq_printf(m, "%s", name);
70                 if (class->name_version > 1)
71                         seq_printf(m, "#%d", class->name_version);
72                 if (class->subclass)
73                         seq_printf(m, "/%d", class->subclass);
74         }
75 }
76
77 static int l_show(struct seq_file *m, void *v)
78 {
79         struct lock_class *class = v;
80         struct lock_list *entry;
81         char usage[LOCK_USAGE_CHARS];
82         int idx = class - lock_classes;
83
84         if (v == lock_classes)
85                 seq_printf(m, "all lock classes:\n");
86
87         if (!test_bit(idx, lock_classes_in_use))
88                 return 0;
89
90         seq_printf(m, "%p", class->key);
91 #ifdef CONFIG_DEBUG_LOCKDEP
92         seq_printf(m, " OPS:%8ld", debug_class_ops_read(class));
93 #endif
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));
97 #endif
98
99         get_usage_chars(class, usage);
100         seq_printf(m, " %s", usage);
101
102         seq_printf(m, ": ");
103         print_name(m, class);
104         seq_puts(m, "\n");
105
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);
110                         seq_puts(m, "\n");
111                 }
112         }
113         seq_puts(m, "\n");
114
115         return 0;
116 }
117
118 static const struct seq_operations lockdep_ops = {
119         .start  = l_start,
120         .next   = l_next,
121         .stop   = l_stop,
122         .show   = l_show,
123 };
124
125 #ifdef CONFIG_PROVE_LOCKING
126 static void *lc_start(struct seq_file *m, loff_t *pos)
127 {
128         if (*pos < 0)
129                 return NULL;
130
131         if (*pos == 0)
132                 return SEQ_START_TOKEN;
133
134         return lock_chains + (*pos - 1);
135 }
136
137 static void *lc_next(struct seq_file *m, void *v, loff_t *pos)
138 {
139         *pos = lockdep_next_lockchain(*pos - 1) + 1;
140         return lc_start(m, pos);
141 }
142
143 static void lc_stop(struct seq_file *m, void *v)
144 {
145 }
146
147 static int lc_show(struct seq_file *m, void *v)
148 {
149         struct lock_chain *chain = v;
150         struct lock_class *class;
151         int i;
152         static const char * const irq_strs[] = {
153                 [0]                          = "0",
154                 [LOCK_CHAIN_HARDIRQ_CONTEXT] = "hardirq",
155                 [LOCK_CHAIN_SOFTIRQ_CONTEXT] = "softirq",
156                 [LOCK_CHAIN_SOFTIRQ_CONTEXT|
157                  LOCK_CHAIN_HARDIRQ_CONTEXT] = "hardirq|softirq",
158         };
159
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");
164                 return 0;
165         }
166
167         seq_printf(m, "irq_context: %s\n", irq_strs[chain->irq_context]);
168
169         for (i = 0; i < chain->depth; i++) {
170                 class = lock_chain_get_class(chain, i);
171                 if (!class->key)
172                         continue;
173
174                 seq_printf(m, "[%p] ", class->key);
175                 print_name(m, class);
176                 seq_puts(m, "\n");
177         }
178         seq_puts(m, "\n");
179
180         return 0;
181 }
182
183 static const struct seq_operations lockdep_chains_ops = {
184         .start  = lc_start,
185         .next   = lc_next,
186         .stop   = lc_stop,
187         .show   = lc_show,
188 };
189 #endif /* CONFIG_PROVE_LOCKING */
190
191 static void lockdep_stats_debug_show(struct seq_file *m)
192 {
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);
202
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));
217
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);
226 #endif
227 }
228
229 static int lockdep_stats_show(struct seq_file *m, void *v)
230 {
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;
239
240 #ifdef CONFIG_PROVE_LOCKING
241         struct lock_class *class;
242         unsigned long idx;
243
244         iterate_lock_classes(idx, class) {
245                 if (!test_bit(idx, lock_classes_in_use))
246                         continue;
247
248                 if (class->usage_mask == 0)
249                         nr_unused++;
250                 if (class->usage_mask == LOCKF_USED)
251                         nr_uncategorized++;
252                 if (class->usage_mask & LOCKF_USED_IN_IRQ)
253                         nr_irq_safe++;
254                 if (class->usage_mask & LOCKF_ENABLED_IRQ)
255                         nr_irq_unsafe++;
256                 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
257                         nr_softirq_safe++;
258                 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ)
259                         nr_softirq_unsafe++;
260                 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
261                         nr_hardirq_safe++;
262                 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ)
263                         nr_hardirq_unsafe++;
264                 if (class->usage_mask & LOCKF_USED_IN_IRQ_READ)
265                         nr_irq_read_safe++;
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++;
276
277                 sum_forward_deps += lockdep_count_forward_deps(class);
278         }
279
280 #ifdef CONFIG_DEBUG_LOCKDEP
281         DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused);
282 #endif
283
284 #endif
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",
290                         sum_forward_deps);
291
292         /*
293          * Total number of dependencies:
294          *
295          * All irq-safe locks may nest inside irq-unsafe locks,
296          * plus all the other known dependencies:
297          */
298         seq_printf(m, " all direct dependencies:       %11lu\n",
299                         nr_irq_unsafe * nr_irq_safe +
300                         nr_hardirq_unsafe * nr_hardirq_safe +
301                         nr_list_entries);
302
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);
312 #endif
313
314 #ifdef CONFIG_TRACE_IRQFLAGS
315         seq_printf(m, " in-hardirq chains:             %11u\n",
316                         nr_hardirq_chains);
317         seq_printf(m, " in-softirq chains:             %11u\n",
318                         nr_softirq_chains);
319 #endif
320         seq_printf(m, " in-process chains:             %11u\n",
321                         nr_process_chains);
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());
329 #endif
330         seq_printf(m, " combined max dependencies:     %11u\n",
331                         (nr_hardirq_chains + 1) *
332                         (nr_softirq_chains + 1) *
333                         (nr_process_chains + 1)
334         );
335         seq_printf(m, " hardirq-safe locks:            %11lu\n",
336                         nr_hardirq_safe);
337         seq_printf(m, " hardirq-unsafe locks:          %11lu\n",
338                         nr_hardirq_unsafe);
339         seq_printf(m, " softirq-safe locks:            %11lu\n",
340                         nr_softirq_safe);
341         seq_printf(m, " softirq-unsafe locks:          %11lu\n",
342                         nr_softirq_unsafe);
343         seq_printf(m, " irq-safe locks:                %11lu\n",
344                         nr_irq_safe);
345         seq_printf(m, " irq-unsafe locks:              %11lu\n",
346                         nr_irq_unsafe);
347
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",
357                         nr_irq_read_safe);
358         seq_printf(m, " irq-read-unsafe locks:         %11lu\n",
359                         nr_irq_read_unsafe);
360
361         seq_printf(m, " uncategorized locks:           %11lu\n",
362                         nr_uncategorized);
363         seq_printf(m, " unused locks:                  %11lu\n",
364                         nr_unused);
365         seq_printf(m, " max locking depth:             %11u\n",
366                         max_lockdep_depth);
367 #ifdef CONFIG_PROVE_LOCKING
368         seq_printf(m, " max bfs queue depth:           %11u\n",
369                         max_bfs_queue_depth);
370 #endif
371         seq_printf(m, " max lock class index:          %11lu\n",
372                         max_lock_class_idx);
373         lockdep_stats_debug_show(m);
374         seq_printf(m, " debug_locks:                   %11u\n",
375                         debug_locks);
376
377         /*
378          * Zappped classes and lockdep data buffers reuse statistics.
379          */
380         seq_puts(m, "\n");
381         seq_printf(m, " zapped classes:                %11lu\n",
382                         nr_zapped_classes);
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);
388 #endif
389         return 0;
390 }
391
392 #ifdef CONFIG_LOCK_STAT
393
394 struct lock_stat_data {
395         struct lock_class *class;
396         struct lock_class_stats stats;
397 };
398
399 struct lock_stat_seq {
400         struct lock_stat_data *iter_end;
401         struct lock_stat_data stats[MAX_LOCKDEP_KEYS];
402 };
403
404 /*
405  * sort on absolute number of contentions
406  */
407 static int lock_stat_cmp(const void *l, const void *r)
408 {
409         const struct lock_stat_data *dl = l, *dr = r;
410         unsigned long nl, nr;
411
412         nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr;
413         nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr;
414
415         return nr - nl;
416 }
417
418 static void seq_line(struct seq_file *m, char c, int offset, int length)
419 {
420         int i;
421
422         for (i = 0; i < offset; i++)
423                 seq_puts(m, " ");
424         for (i = 0; i < length; i++)
425                 seq_printf(m, "%c", c);
426         seq_puts(m, "\n");
427 }
428
429 static void snprint_time(char *buf, size_t bufsiz, s64 nr)
430 {
431         s64 div;
432         s32 rem;
433
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);
437 }
438
439 static void seq_time(struct seq_file *m, s64 time)
440 {
441         char num[15];
442
443         snprint_time(num, sizeof(num), time);
444         seq_printf(m, " %14s", num);
445 }
446
447 static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
448 {
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);
454 }
455
456 static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
457 {
458         const struct lockdep_subclass_key *ckey;
459         struct lock_class_stats *stats;
460         struct lock_class *class;
461         const char *cname;
462         int i, namelen;
463         char name[39];
464
465         class = data->class;
466         stats = &data->stats;
467
468         namelen = 38;
469         if (class->name_version > 1)
470                 namelen -= 2; /* XXX truncates versions > 9 */
471         if (class->subclass)
472                 namelen -= 2;
473
474         rcu_read_lock_sched();
475         cname = rcu_dereference_sched(class->name);
476         ckey  = rcu_dereference_sched(class->key);
477
478         if (!cname && !ckey) {
479                 rcu_read_unlock_sched();
480                 return;
481
482         } else if (!cname) {
483                 char str[KSYM_NAME_LEN];
484                 const char *key_name;
485
486                 key_name = __get_key_name(ckey, str);
487                 snprintf(name, namelen, "%s", key_name);
488         } else {
489                 snprintf(name, namelen, "%s", cname);
490         }
491         rcu_read_unlock_sched();
492
493         namelen = strlen(name);
494         if (class->name_version > 1) {
495                 snprintf(name+namelen, 3, "#%d", class->name_version);
496                 namelen += 2;
497         }
498         if (class->subclass) {
499                 snprintf(name+namelen, 3, "/%d", class->subclass);
500                 namelen += 2;
501         }
502
503         if (stats->write_holdtime.nr) {
504                 if (stats->read_holdtime.nr)
505                         seq_printf(m, "%38s-W:", name);
506                 else
507                         seq_printf(m, "%40s:", name);
508
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);
513                 seq_puts(m, "\n");
514         }
515
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);
522                 seq_puts(m, "\n");
523         }
524
525         if (stats->read_waittime.nr + stats->write_waittime.nr == 0)
526                 return;
527
528         if (stats->read_holdtime.nr)
529                 namelen += 2;
530
531         for (i = 0; i < LOCKSTAT_POINTS; i++) {
532                 char ip[32];
533
534                 if (class->contention_point[i] == 0)
535                         break;
536
537                 if (!i)
538                         seq_line(m, '-', 40-namelen, namelen);
539
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]);
545         }
546         for (i = 0; i < LOCKSTAT_POINTS; i++) {
547                 char ip[32];
548
549                 if (class->contending_point[i] == 0)
550                         break;
551
552                 if (!i)
553                         seq_line(m, '-', 40-namelen, namelen);
554
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]);
560         }
561         if (i) {
562                 seq_puts(m, "\n");
563                 seq_line(m, '.', 0, 40 + 1 + 12 * (14 + 1));
564                 seq_puts(m, "\n");
565         }
566 }
567
568 static void seq_header(struct seq_file *m)
569 {
570         seq_puts(m, "lock_stat version 0.4\n");
571
572         if (unlikely(!debug_locks))
573                 seq_printf(m, "*WARNING* lock debugging disabled!! - possibly due to a lockdep warning\n");
574
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 "
577                         "%14s %14s\n",
578                         "class name",
579                         "con-bounces",
580                         "contentions",
581                         "waittime-min",
582                         "waittime-max",
583                         "waittime-total",
584                         "waittime-avg",
585                         "acq-bounces",
586                         "acquisitions",
587                         "holdtime-min",
588                         "holdtime-max",
589                         "holdtime-total",
590                         "holdtime-avg");
591         seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
592         seq_printf(m, "\n");
593 }
594
595 static void *ls_start(struct seq_file *m, loff_t *pos)
596 {
597         struct lock_stat_seq *data = m->private;
598         struct lock_stat_data *iter;
599
600         if (*pos == 0)
601                 return SEQ_START_TOKEN;
602
603         iter = data->stats + (*pos - 1);
604         if (iter >= data->iter_end)
605                 iter = NULL;
606
607         return iter;
608 }
609
610 static void *ls_next(struct seq_file *m, void *v, loff_t *pos)
611 {
612         (*pos)++;
613         return ls_start(m, pos);
614 }
615
616 static void ls_stop(struct seq_file *m, void *v)
617 {
618 }
619
620 static int ls_show(struct seq_file *m, void *v)
621 {
622         if (v == SEQ_START_TOKEN)
623                 seq_header(m);
624         else
625                 seq_stats(m, v);
626
627         return 0;
628 }
629
630 static const struct seq_operations lockstat_ops = {
631         .start  = ls_start,
632         .next   = ls_next,
633         .stop   = ls_stop,
634         .show   = ls_show,
635 };
636
637 static int lock_stat_open(struct inode *inode, struct file *file)
638 {
639         int res;
640         struct lock_class *class;
641         struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq));
642
643         if (!data)
644                 return -ENOMEM;
645
646         res = seq_open(file, &lockstat_ops);
647         if (!res) {
648                 struct lock_stat_data *iter = data->stats;
649                 struct seq_file *m = file->private_data;
650                 unsigned long idx;
651
652                 iterate_lock_classes(idx, class) {
653                         if (!test_bit(idx, lock_classes_in_use))
654                                 continue;
655                         iter->class = class;
656                         iter->stats = lock_stats(class);
657                         iter++;
658                 }
659
660                 data->iter_end = iter;
661
662                 sort(data->stats, data->iter_end - data->stats,
663                                 sizeof(struct lock_stat_data),
664                                 lock_stat_cmp, NULL);
665
666                 m->private = data;
667         } else
668                 vfree(data);
669
670         return res;
671 }
672
673 static ssize_t lock_stat_write(struct file *file, const char __user *buf,
674                                size_t count, loff_t *ppos)
675 {
676         struct lock_class *class;
677         unsigned long idx;
678         char c;
679
680         if (count) {
681                 if (get_user(c, buf))
682                         return -EFAULT;
683
684                 if (c != '0')
685                         return count;
686
687                 iterate_lock_classes(idx, class) {
688                         if (!test_bit(idx, lock_classes_in_use))
689                                 continue;
690                         clear_lock_stats(class);
691                 }
692         }
693         return count;
694 }
695
696 static int lock_stat_release(struct inode *inode, struct file *file)
697 {
698         struct seq_file *seq = file->private_data;
699
700         vfree(seq->private);
701         return seq_release(inode, file);
702 }
703
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,
710 };
711 #endif /* CONFIG_LOCK_STAT */
712
713 static int __init lockdep_proc_init(void)
714 {
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);
718 #endif
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);
722 #endif
723
724         return 0;
725 }
726
727 __initcall(lockdep_proc_init);
728