Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * kernel/lockdep_proc.c
0004  *
0005  * Runtime locking correctness validator
0006  *
0007  * Started by Ingo Molnar:
0008  *
0009  *  Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
0010  *  Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
0011  *
0012  * Code for /proc/lockdep and /proc/lockdep_stats:
0013  *
0014  */
0015 #include <linux/export.h>
0016 #include <linux/proc_fs.h>
0017 #include <linux/seq_file.h>
0018 #include <linux/kallsyms.h>
0019 #include <linux/debug_locks.h>
0020 #include <linux/vmalloc.h>
0021 #include <linux/sort.h>
0022 #include <linux/uaccess.h>
0023 #include <asm/div64.h>
0024 
0025 #include "lockdep_internals.h"
0026 
0027 /*
0028  * Since iteration of lock_classes is done without holding the lockdep lock,
0029  * it is not safe to iterate all_lock_classes list directly as the iteration
0030  * may branch off to free_lock_classes or the zapped list. Iteration is done
0031  * directly on the lock_classes array by checking the lock_classes_in_use
0032  * bitmap and max_lock_class_idx.
0033  */
0034 #define iterate_lock_classes(idx, class)                \
0035     for (idx = 0, class = lock_classes; idx <= max_lock_class_idx;  \
0036          idx++, class++)
0037 
0038 static void *l_next(struct seq_file *m, void *v, loff_t *pos)
0039 {
0040     struct lock_class *class = v;
0041 
0042     ++class;
0043     *pos = class - lock_classes;
0044     return (*pos > max_lock_class_idx) ? NULL : class;
0045 }
0046 
0047 static void *l_start(struct seq_file *m, loff_t *pos)
0048 {
0049     unsigned long idx = *pos;
0050 
0051     if (idx > max_lock_class_idx)
0052         return NULL;
0053     return lock_classes + idx;
0054 }
0055 
0056 static void l_stop(struct seq_file *m, void *v)
0057 {
0058 }
0059 
0060 static void print_name(struct seq_file *m, struct lock_class *class)
0061 {
0062     char str[KSYM_NAME_LEN];
0063     const char *name = class->name;
0064 
0065     if (!name) {
0066         name = __get_key_name(class->key, str);
0067         seq_printf(m, "%s", name);
0068     } else{
0069         seq_printf(m, "%s", name);
0070         if (class->name_version > 1)
0071             seq_printf(m, "#%d", class->name_version);
0072         if (class->subclass)
0073             seq_printf(m, "/%d", class->subclass);
0074     }
0075 }
0076 
0077 static int l_show(struct seq_file *m, void *v)
0078 {
0079     struct lock_class *class = v;
0080     struct lock_list *entry;
0081     char usage[LOCK_USAGE_CHARS];
0082     int idx = class - lock_classes;
0083 
0084     if (v == lock_classes)
0085         seq_printf(m, "all lock classes:\n");
0086 
0087     if (!test_bit(idx, lock_classes_in_use))
0088         return 0;
0089 
0090     seq_printf(m, "%p", class->key);
0091 #ifdef CONFIG_DEBUG_LOCKDEP
0092     seq_printf(m, " OPS:%8ld", debug_class_ops_read(class));
0093 #endif
0094     if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
0095         seq_printf(m, " FD:%5ld", lockdep_count_forward_deps(class));
0096         seq_printf(m, " BD:%5ld", lockdep_count_backward_deps(class));
0097 
0098         get_usage_chars(class, usage);
0099         seq_printf(m, " %s", usage);
0100     }
0101 
0102     seq_printf(m, ": ");
0103     print_name(m, class);
0104     seq_puts(m, "\n");
0105 
0106     if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
0107         list_for_each_entry(entry, &class->locks_after, entry) {
0108             if (entry->distance == 1) {
0109                 seq_printf(m, " -> [%p] ", entry->class->key);
0110                 print_name(m, entry->class);
0111                 seq_puts(m, "\n");
0112             }
0113         }
0114         seq_puts(m, "\n");
0115     }
0116 
0117     return 0;
0118 }
0119 
0120 static const struct seq_operations lockdep_ops = {
0121     .start  = l_start,
0122     .next   = l_next,
0123     .stop   = l_stop,
0124     .show   = l_show,
0125 };
0126 
0127 #ifdef CONFIG_PROVE_LOCKING
0128 static void *lc_start(struct seq_file *m, loff_t *pos)
0129 {
0130     if (*pos < 0)
0131         return NULL;
0132 
0133     if (*pos == 0)
0134         return SEQ_START_TOKEN;
0135 
0136     return lock_chains + (*pos - 1);
0137 }
0138 
0139 static void *lc_next(struct seq_file *m, void *v, loff_t *pos)
0140 {
0141     *pos = lockdep_next_lockchain(*pos - 1) + 1;
0142     return lc_start(m, pos);
0143 }
0144 
0145 static void lc_stop(struct seq_file *m, void *v)
0146 {
0147 }
0148 
0149 static int lc_show(struct seq_file *m, void *v)
0150 {
0151     struct lock_chain *chain = v;
0152     struct lock_class *class;
0153     int i;
0154     static const char * const irq_strs[] = {
0155         [0]              = "0",
0156         [LOCK_CHAIN_HARDIRQ_CONTEXT] = "hardirq",
0157         [LOCK_CHAIN_SOFTIRQ_CONTEXT] = "softirq",
0158         [LOCK_CHAIN_SOFTIRQ_CONTEXT|
0159          LOCK_CHAIN_HARDIRQ_CONTEXT] = "hardirq|softirq",
0160     };
0161 
0162     if (v == SEQ_START_TOKEN) {
0163         if (!nr_free_chain_hlocks)
0164             seq_printf(m, "(buggered) ");
0165         seq_printf(m, "all lock chains:\n");
0166         return 0;
0167     }
0168 
0169     seq_printf(m, "irq_context: %s\n", irq_strs[chain->irq_context]);
0170 
0171     for (i = 0; i < chain->depth; i++) {
0172         class = lock_chain_get_class(chain, i);
0173         if (!class->key)
0174             continue;
0175 
0176         seq_printf(m, "[%p] ", class->key);
0177         print_name(m, class);
0178         seq_puts(m, "\n");
0179     }
0180     seq_puts(m, "\n");
0181 
0182     return 0;
0183 }
0184 
0185 static const struct seq_operations lockdep_chains_ops = {
0186     .start  = lc_start,
0187     .next   = lc_next,
0188     .stop   = lc_stop,
0189     .show   = lc_show,
0190 };
0191 #endif /* CONFIG_PROVE_LOCKING */
0192 
0193 static void lockdep_stats_debug_show(struct seq_file *m)
0194 {
0195 #ifdef CONFIG_DEBUG_LOCKDEP
0196     unsigned long long hi1 = debug_atomic_read(hardirqs_on_events),
0197                hi2 = debug_atomic_read(hardirqs_off_events),
0198                hr1 = debug_atomic_read(redundant_hardirqs_on),
0199                hr2 = debug_atomic_read(redundant_hardirqs_off),
0200                si1 = debug_atomic_read(softirqs_on_events),
0201                si2 = debug_atomic_read(softirqs_off_events),
0202                sr1 = debug_atomic_read(redundant_softirqs_on),
0203                sr2 = debug_atomic_read(redundant_softirqs_off);
0204 
0205     seq_printf(m, " chain lookup misses:           %11llu\n",
0206         debug_atomic_read(chain_lookup_misses));
0207     seq_printf(m, " chain lookup hits:             %11llu\n",
0208         debug_atomic_read(chain_lookup_hits));
0209     seq_printf(m, " cyclic checks:                 %11llu\n",
0210         debug_atomic_read(nr_cyclic_checks));
0211     seq_printf(m, " redundant checks:              %11llu\n",
0212         debug_atomic_read(nr_redundant_checks));
0213     seq_printf(m, " redundant links:               %11llu\n",
0214         debug_atomic_read(nr_redundant));
0215     seq_printf(m, " find-mask forwards checks:     %11llu\n",
0216         debug_atomic_read(nr_find_usage_forwards_checks));
0217     seq_printf(m, " find-mask backwards checks:    %11llu\n",
0218         debug_atomic_read(nr_find_usage_backwards_checks));
0219 
0220     seq_printf(m, " hardirq on events:             %11llu\n", hi1);
0221     seq_printf(m, " hardirq off events:            %11llu\n", hi2);
0222     seq_printf(m, " redundant hardirq ons:         %11llu\n", hr1);
0223     seq_printf(m, " redundant hardirq offs:        %11llu\n", hr2);
0224     seq_printf(m, " softirq on events:             %11llu\n", si1);
0225     seq_printf(m, " softirq off events:            %11llu\n", si2);
0226     seq_printf(m, " redundant softirq ons:         %11llu\n", sr1);
0227     seq_printf(m, " redundant softirq offs:        %11llu\n", sr2);
0228 #endif
0229 }
0230 
0231 static int lockdep_stats_show(struct seq_file *m, void *v)
0232 {
0233     unsigned long nr_unused = 0, nr_uncategorized = 0,
0234               nr_irq_safe = 0, nr_irq_unsafe = 0,
0235               nr_softirq_safe = 0, nr_softirq_unsafe = 0,
0236               nr_hardirq_safe = 0, nr_hardirq_unsafe = 0,
0237               nr_irq_read_safe = 0, nr_irq_read_unsafe = 0,
0238               nr_softirq_read_safe = 0, nr_softirq_read_unsafe = 0,
0239               nr_hardirq_read_safe = 0, nr_hardirq_read_unsafe = 0,
0240               sum_forward_deps = 0;
0241 
0242 #ifdef CONFIG_PROVE_LOCKING
0243     struct lock_class *class;
0244     unsigned long idx;
0245 
0246     iterate_lock_classes(idx, class) {
0247         if (!test_bit(idx, lock_classes_in_use))
0248             continue;
0249 
0250         if (class->usage_mask == 0)
0251             nr_unused++;
0252         if (class->usage_mask == LOCKF_USED)
0253             nr_uncategorized++;
0254         if (class->usage_mask & LOCKF_USED_IN_IRQ)
0255             nr_irq_safe++;
0256         if (class->usage_mask & LOCKF_ENABLED_IRQ)
0257             nr_irq_unsafe++;
0258         if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
0259             nr_softirq_safe++;
0260         if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ)
0261             nr_softirq_unsafe++;
0262         if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
0263             nr_hardirq_safe++;
0264         if (class->usage_mask & LOCKF_ENABLED_HARDIRQ)
0265             nr_hardirq_unsafe++;
0266         if (class->usage_mask & LOCKF_USED_IN_IRQ_READ)
0267             nr_irq_read_safe++;
0268         if (class->usage_mask & LOCKF_ENABLED_IRQ_READ)
0269             nr_irq_read_unsafe++;
0270         if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ)
0271             nr_softirq_read_safe++;
0272         if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
0273             nr_softirq_read_unsafe++;
0274         if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ)
0275             nr_hardirq_read_safe++;
0276         if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
0277             nr_hardirq_read_unsafe++;
0278 
0279         sum_forward_deps += lockdep_count_forward_deps(class);
0280     }
0281 
0282 #ifdef CONFIG_DEBUG_LOCKDEP
0283     DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused);
0284 #endif
0285 
0286 #endif
0287     seq_printf(m, " lock-classes:                  %11lu [max: %lu]\n",
0288             nr_lock_classes, MAX_LOCKDEP_KEYS);
0289     seq_printf(m, " direct dependencies:           %11lu [max: %lu]\n",
0290             nr_list_entries, MAX_LOCKDEP_ENTRIES);
0291     seq_printf(m, " indirect dependencies:         %11lu\n",
0292             sum_forward_deps);
0293 
0294     /*
0295      * Total number of dependencies:
0296      *
0297      * All irq-safe locks may nest inside irq-unsafe locks,
0298      * plus all the other known dependencies:
0299      */
0300     seq_printf(m, " all direct dependencies:       %11lu\n",
0301             nr_irq_unsafe * nr_irq_safe +
0302             nr_hardirq_unsafe * nr_hardirq_safe +
0303             nr_list_entries);
0304 
0305 #ifdef CONFIG_PROVE_LOCKING
0306     seq_printf(m, " dependency chains:             %11lu [max: %lu]\n",
0307             lock_chain_count(), MAX_LOCKDEP_CHAINS);
0308     seq_printf(m, " dependency chain hlocks used:  %11lu [max: %lu]\n",
0309             MAX_LOCKDEP_CHAIN_HLOCKS -
0310             (nr_free_chain_hlocks + nr_lost_chain_hlocks),
0311             MAX_LOCKDEP_CHAIN_HLOCKS);
0312     seq_printf(m, " dependency chain hlocks lost:  %11u\n",
0313             nr_lost_chain_hlocks);
0314 #endif
0315 
0316 #ifdef CONFIG_TRACE_IRQFLAGS
0317     seq_printf(m, " in-hardirq chains:             %11u\n",
0318             nr_hardirq_chains);
0319     seq_printf(m, " in-softirq chains:             %11u\n",
0320             nr_softirq_chains);
0321 #endif
0322     seq_printf(m, " in-process chains:             %11u\n",
0323             nr_process_chains);
0324     seq_printf(m, " stack-trace entries:           %11lu [max: %lu]\n",
0325             nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES);
0326 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_PROVE_LOCKING)
0327     seq_printf(m, " number of stack traces:        %11llu\n",
0328            lockdep_stack_trace_count());
0329     seq_printf(m, " number of stack hash chains:   %11llu\n",
0330            lockdep_stack_hash_count());
0331 #endif
0332     seq_printf(m, " combined max dependencies:     %11u\n",
0333             (nr_hardirq_chains + 1) *
0334             (nr_softirq_chains + 1) *
0335             (nr_process_chains + 1)
0336     );
0337     seq_printf(m, " hardirq-safe locks:            %11lu\n",
0338             nr_hardirq_safe);
0339     seq_printf(m, " hardirq-unsafe locks:          %11lu\n",
0340             nr_hardirq_unsafe);
0341     seq_printf(m, " softirq-safe locks:            %11lu\n",
0342             nr_softirq_safe);
0343     seq_printf(m, " softirq-unsafe locks:          %11lu\n",
0344             nr_softirq_unsafe);
0345     seq_printf(m, " irq-safe locks:                %11lu\n",
0346             nr_irq_safe);
0347     seq_printf(m, " irq-unsafe locks:              %11lu\n",
0348             nr_irq_unsafe);
0349 
0350     seq_printf(m, " hardirq-read-safe locks:       %11lu\n",
0351             nr_hardirq_read_safe);
0352     seq_printf(m, " hardirq-read-unsafe locks:     %11lu\n",
0353             nr_hardirq_read_unsafe);
0354     seq_printf(m, " softirq-read-safe locks:       %11lu\n",
0355             nr_softirq_read_safe);
0356     seq_printf(m, " softirq-read-unsafe locks:     %11lu\n",
0357             nr_softirq_read_unsafe);
0358     seq_printf(m, " irq-read-safe locks:           %11lu\n",
0359             nr_irq_read_safe);
0360     seq_printf(m, " irq-read-unsafe locks:         %11lu\n",
0361             nr_irq_read_unsafe);
0362 
0363     seq_printf(m, " uncategorized locks:           %11lu\n",
0364             nr_uncategorized);
0365     seq_printf(m, " unused locks:                  %11lu\n",
0366             nr_unused);
0367     seq_printf(m, " max locking depth:             %11u\n",
0368             max_lockdep_depth);
0369 #ifdef CONFIG_PROVE_LOCKING
0370     seq_printf(m, " max bfs queue depth:           %11u\n",
0371             max_bfs_queue_depth);
0372 #endif
0373     seq_printf(m, " max lock class index:          %11lu\n",
0374             max_lock_class_idx);
0375     lockdep_stats_debug_show(m);
0376     seq_printf(m, " debug_locks:                   %11u\n",
0377             debug_locks);
0378 
0379     /*
0380      * Zapped classes and lockdep data buffers reuse statistics.
0381      */
0382     seq_puts(m, "\n");
0383     seq_printf(m, " zapped classes:                %11lu\n",
0384             nr_zapped_classes);
0385 #ifdef CONFIG_PROVE_LOCKING
0386     seq_printf(m, " zapped lock chains:            %11lu\n",
0387             nr_zapped_lock_chains);
0388     seq_printf(m, " large chain blocks:            %11u\n",
0389             nr_large_chain_blocks);
0390 #endif
0391     return 0;
0392 }
0393 
0394 #ifdef CONFIG_LOCK_STAT
0395 
0396 struct lock_stat_data {
0397     struct lock_class *class;
0398     struct lock_class_stats stats;
0399 };
0400 
0401 struct lock_stat_seq {
0402     struct lock_stat_data *iter_end;
0403     struct lock_stat_data stats[MAX_LOCKDEP_KEYS];
0404 };
0405 
0406 /*
0407  * sort on absolute number of contentions
0408  */
0409 static int lock_stat_cmp(const void *l, const void *r)
0410 {
0411     const struct lock_stat_data *dl = l, *dr = r;
0412     unsigned long nl, nr;
0413 
0414     nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr;
0415     nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr;
0416 
0417     return nr - nl;
0418 }
0419 
0420 static void seq_line(struct seq_file *m, char c, int offset, int length)
0421 {
0422     int i;
0423 
0424     for (i = 0; i < offset; i++)
0425         seq_puts(m, " ");
0426     for (i = 0; i < length; i++)
0427         seq_printf(m, "%c", c);
0428     seq_puts(m, "\n");
0429 }
0430 
0431 static void snprint_time(char *buf, size_t bufsiz, s64 nr)
0432 {
0433     s64 div;
0434     s32 rem;
0435 
0436     nr += 5; /* for display rounding */
0437     div = div_s64_rem(nr, 1000, &rem);
0438     snprintf(buf, bufsiz, "%lld.%02d", (long long)div, (int)rem/10);
0439 }
0440 
0441 static void seq_time(struct seq_file *m, s64 time)
0442 {
0443     char num[15];
0444 
0445     snprint_time(num, sizeof(num), time);
0446     seq_printf(m, " %14s", num);
0447 }
0448 
0449 static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
0450 {
0451     seq_printf(m, "%14lu", lt->nr);
0452     seq_time(m, lt->min);
0453     seq_time(m, lt->max);
0454     seq_time(m, lt->total);
0455     seq_time(m, lt->nr ? div64_u64(lt->total, lt->nr) : 0);
0456 }
0457 
0458 static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
0459 {
0460     const struct lockdep_subclass_key *ckey;
0461     struct lock_class_stats *stats;
0462     struct lock_class *class;
0463     const char *cname;
0464     int i, namelen;
0465     char name[39];
0466 
0467     class = data->class;
0468     stats = &data->stats;
0469 
0470     namelen = 38;
0471     if (class->name_version > 1)
0472         namelen -= 2; /* XXX truncates versions > 9 */
0473     if (class->subclass)
0474         namelen -= 2;
0475 
0476     rcu_read_lock_sched();
0477     cname = rcu_dereference_sched(class->name);
0478     ckey  = rcu_dereference_sched(class->key);
0479 
0480     if (!cname && !ckey) {
0481         rcu_read_unlock_sched();
0482         return;
0483 
0484     } else if (!cname) {
0485         char str[KSYM_NAME_LEN];
0486         const char *key_name;
0487 
0488         key_name = __get_key_name(ckey, str);
0489         snprintf(name, namelen, "%s", key_name);
0490     } else {
0491         snprintf(name, namelen, "%s", cname);
0492     }
0493     rcu_read_unlock_sched();
0494 
0495     namelen = strlen(name);
0496     if (class->name_version > 1) {
0497         snprintf(name+namelen, 3, "#%d", class->name_version);
0498         namelen += 2;
0499     }
0500     if (class->subclass) {
0501         snprintf(name+namelen, 3, "/%d", class->subclass);
0502         namelen += 2;
0503     }
0504 
0505     if (stats->write_holdtime.nr) {
0506         if (stats->read_holdtime.nr)
0507             seq_printf(m, "%38s-W:", name);
0508         else
0509             seq_printf(m, "%40s:", name);
0510 
0511         seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
0512         seq_lock_time(m, &stats->write_waittime);
0513         seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
0514         seq_lock_time(m, &stats->write_holdtime);
0515         seq_puts(m, "\n");
0516     }
0517 
0518     if (stats->read_holdtime.nr) {
0519         seq_printf(m, "%38s-R:", name);
0520         seq_printf(m, "%14lu ", stats->bounces[bounce_contended_read]);
0521         seq_lock_time(m, &stats->read_waittime);
0522         seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_read]);
0523         seq_lock_time(m, &stats->read_holdtime);
0524         seq_puts(m, "\n");
0525     }
0526 
0527     if (stats->read_waittime.nr + stats->write_waittime.nr == 0)
0528         return;
0529 
0530     if (stats->read_holdtime.nr)
0531         namelen += 2;
0532 
0533     for (i = 0; i < LOCKSTAT_POINTS; i++) {
0534         char ip[32];
0535 
0536         if (class->contention_point[i] == 0)
0537             break;
0538 
0539         if (!i)
0540             seq_line(m, '-', 40-namelen, namelen);
0541 
0542         snprintf(ip, sizeof(ip), "[<%p>]",
0543                 (void *)class->contention_point[i]);
0544         seq_printf(m, "%40s %14lu %29s %pS\n",
0545                name, stats->contention_point[i],
0546                ip, (void *)class->contention_point[i]);
0547     }
0548     for (i = 0; i < LOCKSTAT_POINTS; i++) {
0549         char ip[32];
0550 
0551         if (class->contending_point[i] == 0)
0552             break;
0553 
0554         if (!i)
0555             seq_line(m, '-', 40-namelen, namelen);
0556 
0557         snprintf(ip, sizeof(ip), "[<%p>]",
0558                 (void *)class->contending_point[i]);
0559         seq_printf(m, "%40s %14lu %29s %pS\n",
0560                name, stats->contending_point[i],
0561                ip, (void *)class->contending_point[i]);
0562     }
0563     if (i) {
0564         seq_puts(m, "\n");
0565         seq_line(m, '.', 0, 40 + 1 + 12 * (14 + 1));
0566         seq_puts(m, "\n");
0567     }
0568 }
0569 
0570 static void seq_header(struct seq_file *m)
0571 {
0572     seq_puts(m, "lock_stat version 0.4\n");
0573 
0574     if (unlikely(!debug_locks))
0575         seq_printf(m, "*WARNING* lock debugging disabled!! - possibly due to a lockdep warning\n");
0576 
0577     seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
0578     seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
0579             "%14s %14s\n",
0580             "class name",
0581             "con-bounces",
0582             "contentions",
0583             "waittime-min",
0584             "waittime-max",
0585             "waittime-total",
0586             "waittime-avg",
0587             "acq-bounces",
0588             "acquisitions",
0589             "holdtime-min",
0590             "holdtime-max",
0591             "holdtime-total",
0592             "holdtime-avg");
0593     seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
0594     seq_printf(m, "\n");
0595 }
0596 
0597 static void *ls_start(struct seq_file *m, loff_t *pos)
0598 {
0599     struct lock_stat_seq *data = m->private;
0600     struct lock_stat_data *iter;
0601 
0602     if (*pos == 0)
0603         return SEQ_START_TOKEN;
0604 
0605     iter = data->stats + (*pos - 1);
0606     if (iter >= data->iter_end)
0607         iter = NULL;
0608 
0609     return iter;
0610 }
0611 
0612 static void *ls_next(struct seq_file *m, void *v, loff_t *pos)
0613 {
0614     (*pos)++;
0615     return ls_start(m, pos);
0616 }
0617 
0618 static void ls_stop(struct seq_file *m, void *v)
0619 {
0620 }
0621 
0622 static int ls_show(struct seq_file *m, void *v)
0623 {
0624     if (v == SEQ_START_TOKEN)
0625         seq_header(m);
0626     else
0627         seq_stats(m, v);
0628 
0629     return 0;
0630 }
0631 
0632 static const struct seq_operations lockstat_ops = {
0633     .start  = ls_start,
0634     .next   = ls_next,
0635     .stop   = ls_stop,
0636     .show   = ls_show,
0637 };
0638 
0639 static int lock_stat_open(struct inode *inode, struct file *file)
0640 {
0641     int res;
0642     struct lock_class *class;
0643     struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq));
0644 
0645     if (!data)
0646         return -ENOMEM;
0647 
0648     res = seq_open(file, &lockstat_ops);
0649     if (!res) {
0650         struct lock_stat_data *iter = data->stats;
0651         struct seq_file *m = file->private_data;
0652         unsigned long idx;
0653 
0654         iterate_lock_classes(idx, class) {
0655             if (!test_bit(idx, lock_classes_in_use))
0656                 continue;
0657             iter->class = class;
0658             iter->stats = lock_stats(class);
0659             iter++;
0660         }
0661 
0662         data->iter_end = iter;
0663 
0664         sort(data->stats, data->iter_end - data->stats,
0665                 sizeof(struct lock_stat_data),
0666                 lock_stat_cmp, NULL);
0667 
0668         m->private = data;
0669     } else
0670         vfree(data);
0671 
0672     return res;
0673 }
0674 
0675 static ssize_t lock_stat_write(struct file *file, const char __user *buf,
0676                    size_t count, loff_t *ppos)
0677 {
0678     struct lock_class *class;
0679     unsigned long idx;
0680     char c;
0681 
0682     if (count) {
0683         if (get_user(c, buf))
0684             return -EFAULT;
0685 
0686         if (c != '0')
0687             return count;
0688 
0689         iterate_lock_classes(idx, class) {
0690             if (!test_bit(idx, lock_classes_in_use))
0691                 continue;
0692             clear_lock_stats(class);
0693         }
0694     }
0695     return count;
0696 }
0697 
0698 static int lock_stat_release(struct inode *inode, struct file *file)
0699 {
0700     struct seq_file *seq = file->private_data;
0701 
0702     vfree(seq->private);
0703     return seq_release(inode, file);
0704 }
0705 
0706 static const struct proc_ops lock_stat_proc_ops = {
0707     .proc_open  = lock_stat_open,
0708     .proc_write = lock_stat_write,
0709     .proc_read  = seq_read,
0710     .proc_lseek = seq_lseek,
0711     .proc_release   = lock_stat_release,
0712 };
0713 #endif /* CONFIG_LOCK_STAT */
0714 
0715 static int __init lockdep_proc_init(void)
0716 {
0717     proc_create_seq("lockdep", S_IRUSR, NULL, &lockdep_ops);
0718 #ifdef CONFIG_PROVE_LOCKING
0719     proc_create_seq("lockdep_chains", S_IRUSR, NULL, &lockdep_chains_ops);
0720 #endif
0721     proc_create_single("lockdep_stats", S_IRUSR, NULL, lockdep_stats_show);
0722 #ifdef CONFIG_LOCK_STAT
0723     proc_create("lock_stat", S_IRUSR | S_IWUSR, NULL, &lock_stat_proc_ops);
0724 #endif
0725 
0726     return 0;
0727 }
0728 
0729 __initcall(lockdep_proc_init);
0730