blob: 9c49ec645d8b5037e66d991f6b4dc2fa22d1c5fe [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnara8f24a32006-07-03 00:24:52 -07002/*
3 * kernel/lockdep_proc.c
4 *
5 * Runtime locking correctness validator
6 *
7 * Started by Ingo Molnar:
8 *
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -07009 * Copyright (C) 2006,2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
Peter Zijlstra90eec102015-11-16 11:08:45 +010010 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra
Ingo Molnara8f24a32006-07-03 00:24:52 -070011 *
12 * Code for /proc/lockdep and /proc/lockdep_stats:
13 *
14 */
Paul Gortmaker9984de12011-05-23 14:51:41 -040015#include <linux/export.h>
Ingo Molnara8f24a32006-07-03 00:24:52 -070016#include <linux/proc_fs.h>
17#include <linux/seq_file.h>
18#include <linux/kallsyms.h>
19#include <linux/debug_locks.h>
Peter Zijlstrac46261d2007-07-19 01:48:57 -070020#include <linux/vmalloc.h>
21#include <linux/sort.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080022#include <linux/uaccess.h>
Peter Zijlstrac46261d2007-07-19 01:48:57 -070023#include <asm/div64.h>
Ingo Molnara8f24a32006-07-03 00:24:52 -070024
25#include "lockdep_internals.h"
26
27static void *l_next(struct seq_file *m, void *v, loff_t *pos)
28{
Li Zefan8109e1d2009-08-17 13:40:17 +080029 return seq_list_next(v, &all_lock_classes, pos);
Ingo Molnara8f24a32006-07-03 00:24:52 -070030}
31
32static void *l_start(struct seq_file *m, loff_t *pos)
33{
Li Zefan8109e1d2009-08-17 13:40:17 +080034 return seq_list_start_head(&all_lock_classes, *pos);
Ingo Molnara8f24a32006-07-03 00:24:52 -070035}
36
37static void l_stop(struct seq_file *m, void *v)
38{
39}
40
Jason Baron068135e2007-02-10 01:44:59 -080041static void print_name(struct seq_file *m, struct lock_class *class)
42{
Cyrill Gorcunov99fb4a12012-10-20 23:05:19 +040043 char str[KSYM_NAME_LEN];
Jason Baron068135e2007-02-10 01:44:59 -080044 const char *name = class->name;
45
46 if (!name) {
47 name = __get_key_name(class->key, str);
48 seq_printf(m, "%s", name);
49 } else{
50 seq_printf(m, "%s", name);
51 if (class->name_version > 1)
52 seq_printf(m, "#%d", class->name_version);
53 if (class->subclass)
54 seq_printf(m, "/%d", class->subclass);
55 }
56}
57
Ingo Molnara8f24a32006-07-03 00:24:52 -070058static int l_show(struct seq_file *m, void *v)
59{
Li Zefan8109e1d2009-08-17 13:40:17 +080060 struct lock_class *class = list_entry(v, struct lock_class, lock_entry);
Jason Baron068135e2007-02-10 01:44:59 -080061 struct lock_list *entry;
Peter Zijlstraf510b232009-01-22 17:53:47 +010062 char usage[LOCK_USAGE_CHARS];
Ingo Molnara8f24a32006-07-03 00:24:52 -070063
Li Zefan8109e1d2009-08-17 13:40:17 +080064 if (v == &all_lock_classes) {
Tim Pepper94c61c02007-10-11 22:11:11 +020065 seq_printf(m, "all lock classes:\n");
66 return 0;
67 }
68
Ingo Molnara8f24a32006-07-03 00:24:52 -070069 seq_printf(m, "%p", class->key);
70#ifdef CONFIG_DEBUG_LOCKDEP
Waiman Long8ca2b56c2018-10-03 13:07:18 -040071 seq_printf(m, " OPS:%8ld", debug_class_ops_read(class));
Ingo Molnara8f24a32006-07-03 00:24:52 -070072#endif
Stephen Hemmingerdf60a842008-08-15 09:33:05 -070073#ifdef CONFIG_PROVE_LOCKING
74 seq_printf(m, " FD:%5ld", lockdep_count_forward_deps(class));
75 seq_printf(m, " BD:%5ld", lockdep_count_backward_deps(class));
76#endif
Ingo Molnara8f24a32006-07-03 00:24:52 -070077
Peter Zijlstraf510b232009-01-22 17:53:47 +010078 get_usage_chars(class, usage);
79 seq_printf(m, " %s", usage);
Ingo Molnara8f24a32006-07-03 00:24:52 -070080
Jason Baron068135e2007-02-10 01:44:59 -080081 seq_printf(m, ": ");
82 print_name(m, class);
83 seq_puts(m, "\n");
84
85 list_for_each_entry(entry, &class->locks_after, entry) {
86 if (entry->distance == 1) {
Huang, Ying2429e4e2008-06-13 14:40:17 +080087 seq_printf(m, " -> [%p] ", entry->class->key);
Jason Baron068135e2007-02-10 01:44:59 -080088 print_name(m, entry->class);
89 seq_puts(m, "\n");
90 }
Ingo Molnara8f24a32006-07-03 00:24:52 -070091 }
92 seq_puts(m, "\n");
93
94 return 0;
95}
96
Helge Deller15ad7cd2006-12-06 20:40:36 -080097static const struct seq_operations lockdep_ops = {
Ingo Molnara8f24a32006-07-03 00:24:52 -070098 .start = l_start,
99 .next = l_next,
100 .stop = l_stop,
101 .show = l_show,
102};
103
Huang, Yingcd1a28e2008-06-23 11:20:54 +0800104#ifdef CONFIG_PROVE_LOCKING
Huang, Ying443cd502008-06-20 16:39:21 +0800105static void *lc_start(struct seq_file *m, loff_t *pos)
106{
Bart Van Assche22126842019-02-14 15:00:48 -0800107 if (*pos < 0)
108 return NULL;
109
Huang, Ying443cd502008-06-20 16:39:21 +0800110 if (*pos == 0)
111 return SEQ_START_TOKEN;
112
Bart Van Assche22126842019-02-14 15:00:48 -0800113 return lock_chains + (*pos - 1);
Huang, Ying443cd502008-06-20 16:39:21 +0800114}
115
Li Zefan12aac192009-08-17 13:40:39 +0800116static void *lc_next(struct seq_file *m, void *v, loff_t *pos)
117{
Bart Van Assche22126842019-02-14 15:00:48 -0800118 *pos = lockdep_next_lockchain(*pos - 1) + 1;
Li Zefan12aac192009-08-17 13:40:39 +0800119 return lc_start(m, pos);
120}
121
Huang, Ying443cd502008-06-20 16:39:21 +0800122static void lc_stop(struct seq_file *m, void *v)
123{
124}
125
126static int lc_show(struct seq_file *m, void *v)
127{
128 struct lock_chain *chain = v;
129 struct lock_class *class;
130 int i;
131
132 if (v == SEQ_START_TOKEN) {
Peter Zijlstra75dd6022016-03-30 11:36:59 +0200133 if (nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS)
134 seq_printf(m, "(buggered) ");
Huang, Ying443cd502008-06-20 16:39:21 +0800135 seq_printf(m, "all lock chains:\n");
136 return 0;
137 }
138
139 seq_printf(m, "irq_context: %d\n", chain->irq_context);
140
141 for (i = 0; i < chain->depth; i++) {
142 class = lock_chain_get_class(chain, i);
Rabin Vincent8bfe0292008-08-11 09:30:26 +0200143 if (!class->key)
144 continue;
145
Huang, Ying443cd502008-06-20 16:39:21 +0800146 seq_printf(m, "[%p] ", class->key);
147 print_name(m, class);
148 seq_puts(m, "\n");
149 }
150 seq_puts(m, "\n");
151
152 return 0;
153}
154
155static const struct seq_operations lockdep_chains_ops = {
156 .start = lc_start,
157 .next = lc_next,
158 .stop = lc_stop,
159 .show = lc_show,
160};
Huang, Yingcd1a28e2008-06-23 11:20:54 +0800161#endif /* CONFIG_PROVE_LOCKING */
Huang, Ying443cd502008-06-20 16:39:21 +0800162
Ingo Molnara8f24a32006-07-03 00:24:52 -0700163static void lockdep_stats_debug_show(struct seq_file *m)
164{
165#ifdef CONFIG_DEBUG_LOCKDEP
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200166 unsigned long long hi1 = debug_atomic_read(hardirqs_on_events),
167 hi2 = debug_atomic_read(hardirqs_off_events),
168 hr1 = debug_atomic_read(redundant_hardirqs_on),
169 hr2 = debug_atomic_read(redundant_hardirqs_off),
170 si1 = debug_atomic_read(softirqs_on_events),
171 si2 = debug_atomic_read(softirqs_off_events),
172 sr1 = debug_atomic_read(redundant_softirqs_on),
173 sr2 = debug_atomic_read(redundant_softirqs_off);
Ingo Molnara8f24a32006-07-03 00:24:52 -0700174
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200175 seq_printf(m, " chain lookup misses: %11llu\n",
176 debug_atomic_read(chain_lookup_misses));
177 seq_printf(m, " chain lookup hits: %11llu\n",
178 debug_atomic_read(chain_lookup_hits));
179 seq_printf(m, " cyclic checks: %11llu\n",
180 debug_atomic_read(nr_cyclic_checks));
Peter Zijlstraae813302017-03-03 10:13:38 +0100181 seq_printf(m, " redundant checks: %11llu\n",
182 debug_atomic_read(nr_redundant_checks));
183 seq_printf(m, " redundant links: %11llu\n",
184 debug_atomic_read(nr_redundant));
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200185 seq_printf(m, " find-mask forwards checks: %11llu\n",
186 debug_atomic_read(nr_find_usage_forwards_checks));
187 seq_printf(m, " find-mask backwards checks: %11llu\n",
188 debug_atomic_read(nr_find_usage_backwards_checks));
Ingo Molnara8f24a32006-07-03 00:24:52 -0700189
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200190 seq_printf(m, " hardirq on events: %11llu\n", hi1);
191 seq_printf(m, " hardirq off events: %11llu\n", hi2);
192 seq_printf(m, " redundant hardirq ons: %11llu\n", hr1);
193 seq_printf(m, " redundant hardirq offs: %11llu\n", hr2);
194 seq_printf(m, " softirq on events: %11llu\n", si1);
195 seq_printf(m, " softirq off events: %11llu\n", si2);
196 seq_printf(m, " redundant softirq ons: %11llu\n", sr1);
197 seq_printf(m, " redundant softirq offs: %11llu\n", sr2);
Ingo Molnara8f24a32006-07-03 00:24:52 -0700198#endif
199}
200
201static int lockdep_stats_show(struct seq_file *m, void *v)
202{
203 struct lock_class *class;
204 unsigned long nr_unused = 0, nr_uncategorized = 0,
205 nr_irq_safe = 0, nr_irq_unsafe = 0,
206 nr_softirq_safe = 0, nr_softirq_unsafe = 0,
207 nr_hardirq_safe = 0, nr_hardirq_unsafe = 0,
208 nr_irq_read_safe = 0, nr_irq_read_unsafe = 0,
209 nr_softirq_read_safe = 0, nr_softirq_read_unsafe = 0,
210 nr_hardirq_read_safe = 0, nr_hardirq_read_unsafe = 0,
Sergey Senozhatskydec29602011-03-23 14:38:28 +0200211 sum_forward_deps = 0;
Ingo Molnara8f24a32006-07-03 00:24:52 -0700212
213 list_for_each_entry(class, &all_lock_classes, lock_entry) {
214
215 if (class->usage_mask == 0)
216 nr_unused++;
217 if (class->usage_mask == LOCKF_USED)
218 nr_uncategorized++;
219 if (class->usage_mask & LOCKF_USED_IN_IRQ)
220 nr_irq_safe++;
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100221 if (class->usage_mask & LOCKF_ENABLED_IRQ)
Ingo Molnara8f24a32006-07-03 00:24:52 -0700222 nr_irq_unsafe++;
223 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
224 nr_softirq_safe++;
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100225 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ)
Ingo Molnara8f24a32006-07-03 00:24:52 -0700226 nr_softirq_unsafe++;
227 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
228 nr_hardirq_safe++;
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100229 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ)
Ingo Molnara8f24a32006-07-03 00:24:52 -0700230 nr_hardirq_unsafe++;
231 if (class->usage_mask & LOCKF_USED_IN_IRQ_READ)
232 nr_irq_read_safe++;
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100233 if (class->usage_mask & LOCKF_ENABLED_IRQ_READ)
Ingo Molnara8f24a32006-07-03 00:24:52 -0700234 nr_irq_read_unsafe++;
235 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ)
236 nr_softirq_read_safe++;
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100237 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ)
Ingo Molnara8f24a32006-07-03 00:24:52 -0700238 nr_softirq_read_unsafe++;
239 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ)
240 nr_hardirq_read_safe++;
Peter Zijlstra4fc95e82009-01-22 13:10:52 +0100241 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ)
Ingo Molnara8f24a32006-07-03 00:24:52 -0700242 nr_hardirq_read_unsafe++;
243
Stephen Hemmingerdf60a842008-08-15 09:33:05 -0700244#ifdef CONFIG_PROVE_LOCKING
David Miller419ca3f2008-07-29 21:45:03 -0700245 sum_forward_deps += lockdep_count_forward_deps(class);
Stephen Hemmingerdf60a842008-08-15 09:33:05 -0700246#endif
Ingo Molnara8f24a32006-07-03 00:24:52 -0700247 }
Robert P. J. Day501b9eb2007-02-10 01:46:34 -0800248#ifdef CONFIG_DEBUG_LOCKDEP
Frederic Weisbeckerbd6d29c2010-04-06 00:10:17 +0200249 DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused);
Ingo Molnara8f24a32006-07-03 00:24:52 -0700250#endif
251 seq_printf(m, " lock-classes: %11lu [max: %lu]\n",
252 nr_lock_classes, MAX_LOCKDEP_KEYS);
253 seq_printf(m, " direct dependencies: %11lu [max: %lu]\n",
254 nr_list_entries, MAX_LOCKDEP_ENTRIES);
255 seq_printf(m, " indirect dependencies: %11lu\n",
256 sum_forward_deps);
257
258 /*
259 * Total number of dependencies:
260 *
261 * All irq-safe locks may nest inside irq-unsafe locks,
262 * plus all the other known dependencies:
263 */
264 seq_printf(m, " all direct dependencies: %11lu\n",
265 nr_irq_unsafe * nr_irq_safe +
266 nr_hardirq_unsafe * nr_hardirq_safe +
267 nr_list_entries);
268
Peter Zijlstra8e182572007-07-19 01:48:54 -0700269#ifdef CONFIG_PROVE_LOCKING
Ingo Molnara8f24a32006-07-03 00:24:52 -0700270 seq_printf(m, " dependency chains: %11lu [max: %lu]\n",
Bart Van Assche22126842019-02-14 15:00:48 -0800271 lock_chain_count(), MAX_LOCKDEP_CHAINS);
Huang, Ying443cd502008-06-20 16:39:21 +0800272 seq_printf(m, " dependency chain hlocks: %11d [max: %lu]\n",
Huang, Yingcd1a28e2008-06-23 11:20:54 +0800273 nr_chain_hlocks, MAX_LOCKDEP_CHAIN_HLOCKS);
Peter Zijlstra8e182572007-07-19 01:48:54 -0700274#endif
Ingo Molnara8f24a32006-07-03 00:24:52 -0700275
276#ifdef CONFIG_TRACE_IRQFLAGS
277 seq_printf(m, " in-hardirq chains: %11u\n",
278 nr_hardirq_chains);
279 seq_printf(m, " in-softirq chains: %11u\n",
280 nr_softirq_chains);
281#endif
282 seq_printf(m, " in-process chains: %11u\n",
283 nr_process_chains);
284 seq_printf(m, " stack-trace entries: %11lu [max: %lu]\n",
285 nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES);
286 seq_printf(m, " combined max dependencies: %11u\n",
287 (nr_hardirq_chains + 1) *
288 (nr_softirq_chains + 1) *
289 (nr_process_chains + 1)
290 );
291 seq_printf(m, " hardirq-safe locks: %11lu\n",
292 nr_hardirq_safe);
293 seq_printf(m, " hardirq-unsafe locks: %11lu\n",
294 nr_hardirq_unsafe);
295 seq_printf(m, " softirq-safe locks: %11lu\n",
296 nr_softirq_safe);
297 seq_printf(m, " softirq-unsafe locks: %11lu\n",
298 nr_softirq_unsafe);
299 seq_printf(m, " irq-safe locks: %11lu\n",
300 nr_irq_safe);
301 seq_printf(m, " irq-unsafe locks: %11lu\n",
302 nr_irq_unsafe);
303
304 seq_printf(m, " hardirq-read-safe locks: %11lu\n",
305 nr_hardirq_read_safe);
306 seq_printf(m, " hardirq-read-unsafe locks: %11lu\n",
307 nr_hardirq_read_unsafe);
308 seq_printf(m, " softirq-read-safe locks: %11lu\n",
309 nr_softirq_read_safe);
310 seq_printf(m, " softirq-read-unsafe locks: %11lu\n",
311 nr_softirq_read_unsafe);
312 seq_printf(m, " irq-read-safe locks: %11lu\n",
313 nr_irq_read_safe);
314 seq_printf(m, " irq-read-unsafe locks: %11lu\n",
315 nr_irq_read_unsafe);
316
317 seq_printf(m, " uncategorized locks: %11lu\n",
318 nr_uncategorized);
319 seq_printf(m, " unused locks: %11lu\n",
320 nr_unused);
321 seq_printf(m, " max locking depth: %11u\n",
322 max_lockdep_depth);
Ingo Molnarbbfa2622009-08-02 14:44:24 +0200323#ifdef CONFIG_PROVE_LOCKING
Ming Lei12f3dfd2009-07-16 15:44:29 +0200324 seq_printf(m, " max bfs queue depth: %11u\n",
325 max_bfs_queue_depth);
Ingo Molnarbbfa2622009-08-02 14:44:24 +0200326#endif
Ingo Molnara8f24a32006-07-03 00:24:52 -0700327 lockdep_stats_debug_show(m);
328 seq_printf(m, " debug_locks: %11u\n",
329 debug_locks);
330
331 return 0;
332}
333
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700334#ifdef CONFIG_LOCK_STAT
335
336struct lock_stat_data {
337 struct lock_class *class;
338 struct lock_class_stats stats;
339};
340
341struct lock_stat_seq {
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700342 struct lock_stat_data *iter_end;
343 struct lock_stat_data stats[MAX_LOCKDEP_KEYS];
344};
345
346/*
347 * sort on absolute number of contentions
348 */
349static int lock_stat_cmp(const void *l, const void *r)
350{
351 const struct lock_stat_data *dl = l, *dr = r;
352 unsigned long nl, nr;
353
354 nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr;
355 nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr;
356
357 return nr - nl;
358}
359
360static void seq_line(struct seq_file *m, char c, int offset, int length)
361{
362 int i;
363
364 for (i = 0; i < offset; i++)
365 seq_puts(m, " ");
366 for (i = 0; i < length; i++)
367 seq_printf(m, "%c", c);
368 seq_puts(m, "\n");
369}
370
371static void snprint_time(char *buf, size_t bufsiz, s64 nr)
372{
Peter Zijlstra6918bc52008-09-23 15:33:41 +0200373 s64 div;
374 s32 rem;
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700375
Joe Korty2189459d2008-08-25 17:15:33 -0400376 nr += 5; /* for display rounding */
Peter Zijlstra6918bc52008-09-23 15:33:41 +0200377 div = div_s64_rem(nr, 1000, &rem);
378 snprintf(buf, bufsiz, "%lld.%02d", (long long)div, (int)rem/10);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700379}
380
381static void seq_time(struct seq_file *m, s64 time)
382{
383 char num[15];
384
385 snprint_time(num, sizeof(num), time);
386 seq_printf(m, " %14s", num);
387}
388
389static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
390{
391 seq_printf(m, "%14lu", lt->nr);
392 seq_time(m, lt->min);
393 seq_time(m, lt->max);
394 seq_time(m, lt->total);
Peter Zijlstra838cc7b2013-11-06 17:42:30 +0100395 seq_time(m, lt->nr ? div_s64(lt->total, lt->nr) : 0);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700396}
397
398static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
399{
Peter Zijlstracee34d82015-06-02 12:50:13 +0200400 struct lockdep_subclass_key *ckey;
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700401 struct lock_class_stats *stats;
Peter Zijlstracee34d82015-06-02 12:50:13 +0200402 struct lock_class *class;
403 const char *cname;
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700404 int i, namelen;
Peter Zijlstracee34d82015-06-02 12:50:13 +0200405 char name[39];
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700406
407 class = data->class;
408 stats = &data->stats;
409
Peter Zijlstrad38e1d52007-07-19 01:49:01 -0700410 namelen = 38;
411 if (class->name_version > 1)
412 namelen -= 2; /* XXX truncates versions > 9 */
413 if (class->subclass)
414 namelen -= 2;
415
Peter Zijlstracee34d82015-06-02 12:50:13 +0200416 rcu_read_lock_sched();
417 cname = rcu_dereference_sched(class->name);
418 ckey = rcu_dereference_sched(class->key);
419
420 if (!cname && !ckey) {
421 rcu_read_unlock_sched();
422 return;
423
424 } else if (!cname) {
Peter Zijlstrad38e1d52007-07-19 01:49:01 -0700425 char str[KSYM_NAME_LEN];
426 const char *key_name;
427
Peter Zijlstracee34d82015-06-02 12:50:13 +0200428 key_name = __get_key_name(ckey, str);
Peter Zijlstrad38e1d52007-07-19 01:49:01 -0700429 snprintf(name, namelen, "%s", key_name);
430 } else {
Peter Zijlstracee34d82015-06-02 12:50:13 +0200431 snprintf(name, namelen, "%s", cname);
Peter Zijlstrad38e1d52007-07-19 01:49:01 -0700432 }
Peter Zijlstracee34d82015-06-02 12:50:13 +0200433 rcu_read_unlock_sched();
434
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700435 namelen = strlen(name);
Peter Zijlstrad38e1d52007-07-19 01:49:01 -0700436 if (class->name_version > 1) {
437 snprintf(name+namelen, 3, "#%d", class->name_version);
438 namelen += 2;
439 }
440 if (class->subclass) {
441 snprintf(name+namelen, 3, "/%d", class->subclass);
442 namelen += 2;
443 }
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700444
445 if (stats->write_holdtime.nr) {
446 if (stats->read_holdtime.nr)
447 seq_printf(m, "%38s-W:", name);
448 else
449 seq_printf(m, "%40s:", name);
450
Peter Zijlstra96645672007-07-19 01:49:00 -0700451 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700452 seq_lock_time(m, &stats->write_waittime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700453 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700454 seq_lock_time(m, &stats->write_holdtime);
455 seq_puts(m, "\n");
456 }
457
458 if (stats->read_holdtime.nr) {
459 seq_printf(m, "%38s-R:", name);
Peter Zijlstra96645672007-07-19 01:49:00 -0700460 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_read]);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700461 seq_lock_time(m, &stats->read_waittime);
Peter Zijlstra96645672007-07-19 01:49:00 -0700462 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_read]);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700463 seq_lock_time(m, &stats->read_holdtime);
464 seq_puts(m, "\n");
465 }
466
467 if (stats->read_waittime.nr + stats->write_waittime.nr == 0)
468 return;
469
470 if (stats->read_holdtime.nr)
471 namelen += 2;
472
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200473 for (i = 0; i < LOCKSTAT_POINTS; i++) {
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700474 char ip[32];
475
476 if (class->contention_point[i] == 0)
477 break;
478
479 if (!i)
480 seq_line(m, '-', 40-namelen, namelen);
481
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700482 snprintf(ip, sizeof(ip), "[<%p>]",
483 (void *)class->contention_point[i]);
Joe Perches3cf9b852010-11-05 16:12:38 -0700484 seq_printf(m, "%40s %14lu %29s %pS\n",
485 name, stats->contention_point[i],
486 ip, (void *)class->contention_point[i]);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700487 }
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200488 for (i = 0; i < LOCKSTAT_POINTS; i++) {
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200489 char ip[32];
490
491 if (class->contending_point[i] == 0)
492 break;
493
494 if (!i)
495 seq_line(m, '-', 40-namelen, namelen);
496
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200497 snprintf(ip, sizeof(ip), "[<%p>]",
498 (void *)class->contending_point[i]);
Joe Perches3cf9b852010-11-05 16:12:38 -0700499 seq_printf(m, "%40s %14lu %29s %pS\n",
500 name, stats->contending_point[i],
501 ip, (void *)class->contending_point[i]);
Peter Zijlstrac7e78cf2008-10-16 23:17:09 +0200502 }
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700503 if (i) {
504 seq_puts(m, "\n");
Davidlohr Bueso1232e382013-10-08 20:37:16 -0700505 seq_line(m, '.', 0, 40 + 1 + 12 * (14 + 1));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700506 seq_puts(m, "\n");
507 }
508}
509
510static void seq_header(struct seq_file *m)
511{
Davidlohr Bueso1232e382013-10-08 20:37:16 -0700512 seq_puts(m, "lock_stat version 0.4\n");
Peter Zijlstra9833f8c2009-02-14 16:59:04 +0100513
514 if (unlikely(!debug_locks))
515 seq_printf(m, "*WARNING* lock debugging disabled!! - possibly due to a lockdep warning\n");
516
Davidlohr Bueso1232e382013-10-08 20:37:16 -0700517 seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
518 seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s "
Peter Zijlstra96645672007-07-19 01:49:00 -0700519 "%14s %14s\n",
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700520 "class name",
Peter Zijlstra96645672007-07-19 01:49:00 -0700521 "con-bounces",
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700522 "contentions",
523 "waittime-min",
524 "waittime-max",
525 "waittime-total",
Davidlohr Bueso1232e382013-10-08 20:37:16 -0700526 "waittime-avg",
Peter Zijlstra96645672007-07-19 01:49:00 -0700527 "acq-bounces",
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700528 "acquisitions",
529 "holdtime-min",
530 "holdtime-max",
Davidlohr Bueso1232e382013-10-08 20:37:16 -0700531 "holdtime-total",
532 "holdtime-avg");
533 seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1));
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700534 seq_printf(m, "\n");
535}
536
537static void *ls_start(struct seq_file *m, loff_t *pos)
538{
539 struct lock_stat_seq *data = m->private;
Li Zefan96004bb2009-08-17 13:40:59 +0800540 struct lock_stat_data *iter;
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700541
Tim Pepper94c61c02007-10-11 22:11:11 +0200542 if (*pos == 0)
543 return SEQ_START_TOKEN;
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700544
Li Zefan96004bb2009-08-17 13:40:59 +0800545 iter = data->stats + (*pos - 1);
546 if (iter >= data->iter_end)
547 iter = NULL;
Peter Zijlstra4b32d0a2007-07-19 01:48:59 -0700548
Li Zefan96004bb2009-08-17 13:40:59 +0800549 return iter;
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700550}
551
552static void *ls_next(struct seq_file *m, void *v, loff_t *pos)
553{
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700554 (*pos)++;
Li Zefan96004bb2009-08-17 13:40:59 +0800555 return ls_start(m, pos);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700556}
557
558static void ls_stop(struct seq_file *m, void *v)
559{
560}
561
562static int ls_show(struct seq_file *m, void *v)
563{
Tim Pepper94c61c02007-10-11 22:11:11 +0200564 if (v == SEQ_START_TOKEN)
565 seq_header(m);
566 else
567 seq_stats(m, v);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700568
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700569 return 0;
570}
571
James Morris88e9d342009-09-22 16:43:43 -0700572static const struct seq_operations lockstat_ops = {
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700573 .start = ls_start,
574 .next = ls_next,
575 .stop = ls_stop,
576 .show = ls_show,
577};
578
579static int lock_stat_open(struct inode *inode, struct file *file)
580{
581 int res;
582 struct lock_class *class;
583 struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq));
584
585 if (!data)
586 return -ENOMEM;
587
588 res = seq_open(file, &lockstat_ops);
589 if (!res) {
590 struct lock_stat_data *iter = data->stats;
591 struct seq_file *m = file->private_data;
592
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700593 list_for_each_entry(class, &all_lock_classes, lock_entry) {
594 iter->class = class;
595 iter->stats = lock_stats(class);
596 iter++;
597 }
598 data->iter_end = iter;
599
Li Zefan96004bb2009-08-17 13:40:59 +0800600 sort(data->stats, data->iter_end - data->stats,
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700601 sizeof(struct lock_stat_data),
602 lock_stat_cmp, NULL);
603
604 m->private = data;
605 } else
606 vfree(data);
607
608 return res;
609}
610
611static ssize_t lock_stat_write(struct file *file, const char __user *buf,
612 size_t count, loff_t *ppos)
613{
614 struct lock_class *class;
615 char c;
616
617 if (count) {
618 if (get_user(c, buf))
619 return -EFAULT;
620
621 if (c != '0')
622 return count;
623
624 list_for_each_entry(class, &all_lock_classes, lock_entry)
625 clear_lock_stats(class);
626 }
627 return count;
628}
629
630static int lock_stat_release(struct inode *inode, struct file *file)
631{
632 struct seq_file *seq = file->private_data;
633
634 vfree(seq->private);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700635 return seq_release(inode, file);
636}
637
638static const struct file_operations proc_lock_stat_operations = {
639 .open = lock_stat_open,
640 .write = lock_stat_write,
641 .read = seq_read,
642 .llseek = seq_lseek,
643 .release = lock_stat_release,
644};
645#endif /* CONFIG_LOCK_STAT */
646
Ingo Molnara8f24a32006-07-03 00:24:52 -0700647static int __init lockdep_proc_init(void)
648{
Christoph Hellwigfddda2b2018-04-13 19:44:18 +0200649 proc_create_seq("lockdep", S_IRUSR, NULL, &lockdep_ops);
Huang, Yingcd1a28e2008-06-23 11:20:54 +0800650#ifdef CONFIG_PROVE_LOCKING
Christoph Hellwigfddda2b2018-04-13 19:44:18 +0200651 proc_create_seq("lockdep_chains", S_IRUSR, NULL, &lockdep_chains_ops);
Huang, Yingcd1a28e2008-06-23 11:20:54 +0800652#endif
Christoph Hellwig3f3942a2018-05-15 15:57:23 +0200653 proc_create_single("lockdep_stats", S_IRUSR, NULL, lockdep_stats_show);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700654#ifdef CONFIG_LOCK_STAT
Li Zefan97954472009-08-07 16:37:10 +0800655 proc_create("lock_stat", S_IRUSR | S_IWUSR, NULL,
656 &proc_lock_stat_operations);
Peter Zijlstrac46261d2007-07-19 01:48:57 -0700657#endif
658
Ingo Molnara8f24a32006-07-03 00:24:52 -0700659 return 0;
660}
661
662__initcall(lockdep_proc_init);
663