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 static void *l_next(struct seq_file *m, void *v, loff_t *pos) 28 { 29 return seq_list_next(v, &all_lock_classes, pos); 30 } 31 32 static void *l_start(struct seq_file *m, loff_t *pos) 33 { 34 return seq_list_start_head(&all_lock_classes, *pos); 35 } 36 37 static void l_stop(struct seq_file *m, void *v) 38 { 39 } 40 41 static void print_name(struct seq_file *m, struct lock_class *class) 42 { 43 char str[KSYM_NAME_LEN]; 44 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 58 static int l_show(struct seq_file *m, void *v) 59 { 60 struct lock_class *class = list_entry(v, struct lock_class, lock_entry); 61 struct lock_list *entry; 62 char usage[LOCK_USAGE_CHARS]; 63 64 if (v == &all_lock_classes) { 65 seq_printf(m, "all lock classes:\n"); 66 return 0; 67 } 68 69 seq_printf(m, "%p", class->key); 70 #ifdef CONFIG_DEBUG_LOCKDEP 71 seq_printf(m, " OPS:%8ld", debug_class_ops_read(class)); 72 #endif 73 #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 77 78 get_usage_chars(class, usage); 79 seq_printf(m, " %s", usage); 80 81 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) { 87 seq_printf(m, " -> [%p] ", entry->class->key); 88 print_name(m, entry->class); 89 seq_puts(m, "\n"); 90 } 91 } 92 seq_puts(m, "\n"); 93 94 return 0; 95 } 96 97 static const struct seq_operations lockdep_ops = { 98 .start = l_start, 99 .next = l_next, 100 .stop = l_stop, 101 .show = l_show, 102 }; 103 104 #ifdef CONFIG_PROVE_LOCKING 105 static void *lc_start(struct seq_file *m, loff_t *pos) 106 { 107 if (*pos < 0) 108 return NULL; 109 110 if (*pos == 0) 111 return SEQ_START_TOKEN; 112 113 return lock_chains + (*pos - 1); 114 } 115 116 static void *lc_next(struct seq_file *m, void *v, loff_t *pos) 117 { 118 *pos = lockdep_next_lockchain(*pos - 1) + 1; 119 return lc_start(m, pos); 120 } 121 122 static void lc_stop(struct seq_file *m, void *v) 123 { 124 } 125 126 static 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) { 133 if (nr_chain_hlocks > MAX_LOCKDEP_CHAIN_HLOCKS) 134 seq_printf(m, "(buggered) "); 135 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); 143 if (!class->key) 144 continue; 145 146 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 155 static const struct seq_operations lockdep_chains_ops = { 156 .start = lc_start, 157 .next = lc_next, 158 .stop = lc_stop, 159 .show = lc_show, 160 }; 161 #endif /* CONFIG_PROVE_LOCKING */ 162 163 static void lockdep_stats_debug_show(struct seq_file *m) 164 { 165 #ifdef CONFIG_DEBUG_LOCKDEP 166 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); 174 175 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)); 181 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)); 185 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)); 189 190 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); 198 #endif 199 } 200 201 static 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, 211 sum_forward_deps = 0; 212 213 #ifdef CONFIG_PROVE_LOCKING 214 list_for_each_entry(class, &all_lock_classes, lock_entry) { 215 216 if (class->usage_mask == 0) 217 nr_unused++; 218 if (class->usage_mask == LOCKF_USED) 219 nr_uncategorized++; 220 if (class->usage_mask & LOCKF_USED_IN_IRQ) 221 nr_irq_safe++; 222 if (class->usage_mask & LOCKF_ENABLED_IRQ) 223 nr_irq_unsafe++; 224 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ) 225 nr_softirq_safe++; 226 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ) 227 nr_softirq_unsafe++; 228 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ) 229 nr_hardirq_safe++; 230 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ) 231 nr_hardirq_unsafe++; 232 if (class->usage_mask & LOCKF_USED_IN_IRQ_READ) 233 nr_irq_read_safe++; 234 if (class->usage_mask & LOCKF_ENABLED_IRQ_READ) 235 nr_irq_read_unsafe++; 236 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) 237 nr_softirq_read_safe++; 238 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ) 239 nr_softirq_read_unsafe++; 240 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) 241 nr_hardirq_read_safe++; 242 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ) 243 nr_hardirq_read_unsafe++; 244 245 sum_forward_deps += lockdep_count_forward_deps(class); 246 } 247 #ifdef CONFIG_DEBUG_LOCKDEP 248 DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused); 249 #endif 250 251 #endif 252 seq_printf(m, " lock-classes: %11lu [max: %lu]\n", 253 nr_lock_classes, MAX_LOCKDEP_KEYS); 254 seq_printf(m, " direct dependencies: %11lu [max: %lu]\n", 255 nr_list_entries, MAX_LOCKDEP_ENTRIES); 256 seq_printf(m, " indirect dependencies: %11lu\n", 257 sum_forward_deps); 258 259 /* 260 * Total number of dependencies: 261 * 262 * All irq-safe locks may nest inside irq-unsafe locks, 263 * plus all the other known dependencies: 264 */ 265 seq_printf(m, " all direct dependencies: %11lu\n", 266 nr_irq_unsafe * nr_irq_safe + 267 nr_hardirq_unsafe * nr_hardirq_safe + 268 nr_list_entries); 269 270 #ifdef CONFIG_PROVE_LOCKING 271 seq_printf(m, " dependency chains: %11lu [max: %lu]\n", 272 lock_chain_count(), MAX_LOCKDEP_CHAINS); 273 seq_printf(m, " dependency chain hlocks: %11d [max: %lu]\n", 274 nr_chain_hlocks, MAX_LOCKDEP_CHAIN_HLOCKS); 275 #endif 276 277 #ifdef CONFIG_TRACE_IRQFLAGS 278 seq_printf(m, " in-hardirq chains: %11u\n", 279 nr_hardirq_chains); 280 seq_printf(m, " in-softirq chains: %11u\n", 281 nr_softirq_chains); 282 #endif 283 seq_printf(m, " in-process chains: %11u\n", 284 nr_process_chains); 285 seq_printf(m, " stack-trace entries: %11lu [max: %lu]\n", 286 nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES); 287 seq_printf(m, " combined max dependencies: %11u\n", 288 (nr_hardirq_chains + 1) * 289 (nr_softirq_chains + 1) * 290 (nr_process_chains + 1) 291 ); 292 seq_printf(m, " hardirq-safe locks: %11lu\n", 293 nr_hardirq_safe); 294 seq_printf(m, " hardirq-unsafe locks: %11lu\n", 295 nr_hardirq_unsafe); 296 seq_printf(m, " softirq-safe locks: %11lu\n", 297 nr_softirq_safe); 298 seq_printf(m, " softirq-unsafe locks: %11lu\n", 299 nr_softirq_unsafe); 300 seq_printf(m, " irq-safe locks: %11lu\n", 301 nr_irq_safe); 302 seq_printf(m, " irq-unsafe locks: %11lu\n", 303 nr_irq_unsafe); 304 305 seq_printf(m, " hardirq-read-safe locks: %11lu\n", 306 nr_hardirq_read_safe); 307 seq_printf(m, " hardirq-read-unsafe locks: %11lu\n", 308 nr_hardirq_read_unsafe); 309 seq_printf(m, " softirq-read-safe locks: %11lu\n", 310 nr_softirq_read_safe); 311 seq_printf(m, " softirq-read-unsafe locks: %11lu\n", 312 nr_softirq_read_unsafe); 313 seq_printf(m, " irq-read-safe locks: %11lu\n", 314 nr_irq_read_safe); 315 seq_printf(m, " irq-read-unsafe locks: %11lu\n", 316 nr_irq_read_unsafe); 317 318 seq_printf(m, " uncategorized locks: %11lu\n", 319 nr_uncategorized); 320 seq_printf(m, " unused locks: %11lu\n", 321 nr_unused); 322 seq_printf(m, " max locking depth: %11u\n", 323 max_lockdep_depth); 324 #ifdef CONFIG_PROVE_LOCKING 325 seq_printf(m, " max bfs queue depth: %11u\n", 326 max_bfs_queue_depth); 327 #endif 328 lockdep_stats_debug_show(m); 329 seq_printf(m, " debug_locks: %11u\n", 330 debug_locks); 331 332 return 0; 333 } 334 335 #ifdef CONFIG_LOCK_STAT 336 337 struct lock_stat_data { 338 struct lock_class *class; 339 struct lock_class_stats stats; 340 }; 341 342 struct lock_stat_seq { 343 struct lock_stat_data *iter_end; 344 struct lock_stat_data stats[MAX_LOCKDEP_KEYS]; 345 }; 346 347 /* 348 * sort on absolute number of contentions 349 */ 350 static int lock_stat_cmp(const void *l, const void *r) 351 { 352 const struct lock_stat_data *dl = l, *dr = r; 353 unsigned long nl, nr; 354 355 nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr; 356 nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr; 357 358 return nr - nl; 359 } 360 361 static void seq_line(struct seq_file *m, char c, int offset, int length) 362 { 363 int i; 364 365 for (i = 0; i < offset; i++) 366 seq_puts(m, " "); 367 for (i = 0; i < length; i++) 368 seq_printf(m, "%c", c); 369 seq_puts(m, "\n"); 370 } 371 372 static void snprint_time(char *buf, size_t bufsiz, s64 nr) 373 { 374 s64 div; 375 s32 rem; 376 377 nr += 5; /* for display rounding */ 378 div = div_s64_rem(nr, 1000, &rem); 379 snprintf(buf, bufsiz, "%lld.%02d", (long long)div, (int)rem/10); 380 } 381 382 static void seq_time(struct seq_file *m, s64 time) 383 { 384 char num[15]; 385 386 snprint_time(num, sizeof(num), time); 387 seq_printf(m, " %14s", num); 388 } 389 390 static void seq_lock_time(struct seq_file *m, struct lock_time *lt) 391 { 392 seq_printf(m, "%14lu", lt->nr); 393 seq_time(m, lt->min); 394 seq_time(m, lt->max); 395 seq_time(m, lt->total); 396 seq_time(m, lt->nr ? div_s64(lt->total, lt->nr) : 0); 397 } 398 399 static void seq_stats(struct seq_file *m, struct lock_stat_data *data) 400 { 401 struct lockdep_subclass_key *ckey; 402 struct lock_class_stats *stats; 403 struct lock_class *class; 404 const char *cname; 405 int i, namelen; 406 char name[39]; 407 408 class = data->class; 409 stats = &data->stats; 410 411 namelen = 38; 412 if (class->name_version > 1) 413 namelen -= 2; /* XXX truncates versions > 9 */ 414 if (class->subclass) 415 namelen -= 2; 416 417 rcu_read_lock_sched(); 418 cname = rcu_dereference_sched(class->name); 419 ckey = rcu_dereference_sched(class->key); 420 421 if (!cname && !ckey) { 422 rcu_read_unlock_sched(); 423 return; 424 425 } else if (!cname) { 426 char str[KSYM_NAME_LEN]; 427 const char *key_name; 428 429 key_name = __get_key_name(ckey, str); 430 snprintf(name, namelen, "%s", key_name); 431 } else { 432 snprintf(name, namelen, "%s", cname); 433 } 434 rcu_read_unlock_sched(); 435 436 namelen = strlen(name); 437 if (class->name_version > 1) { 438 snprintf(name+namelen, 3, "#%d", class->name_version); 439 namelen += 2; 440 } 441 if (class->subclass) { 442 snprintf(name+namelen, 3, "/%d", class->subclass); 443 namelen += 2; 444 } 445 446 if (stats->write_holdtime.nr) { 447 if (stats->read_holdtime.nr) 448 seq_printf(m, "%38s-W:", name); 449 else 450 seq_printf(m, "%40s:", name); 451 452 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]); 453 seq_lock_time(m, &stats->write_waittime); 454 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]); 455 seq_lock_time(m, &stats->write_holdtime); 456 seq_puts(m, "\n"); 457 } 458 459 if (stats->read_holdtime.nr) { 460 seq_printf(m, "%38s-R:", name); 461 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_read]); 462 seq_lock_time(m, &stats->read_waittime); 463 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_read]); 464 seq_lock_time(m, &stats->read_holdtime); 465 seq_puts(m, "\n"); 466 } 467 468 if (stats->read_waittime.nr + stats->write_waittime.nr == 0) 469 return; 470 471 if (stats->read_holdtime.nr) 472 namelen += 2; 473 474 for (i = 0; i < LOCKSTAT_POINTS; i++) { 475 char ip[32]; 476 477 if (class->contention_point[i] == 0) 478 break; 479 480 if (!i) 481 seq_line(m, '-', 40-namelen, namelen); 482 483 snprintf(ip, sizeof(ip), "[<%p>]", 484 (void *)class->contention_point[i]); 485 seq_printf(m, "%40s %14lu %29s %pS\n", 486 name, stats->contention_point[i], 487 ip, (void *)class->contention_point[i]); 488 } 489 for (i = 0; i < LOCKSTAT_POINTS; i++) { 490 char ip[32]; 491 492 if (class->contending_point[i] == 0) 493 break; 494 495 if (!i) 496 seq_line(m, '-', 40-namelen, namelen); 497 498 snprintf(ip, sizeof(ip), "[<%p>]", 499 (void *)class->contending_point[i]); 500 seq_printf(m, "%40s %14lu %29s %pS\n", 501 name, stats->contending_point[i], 502 ip, (void *)class->contending_point[i]); 503 } 504 if (i) { 505 seq_puts(m, "\n"); 506 seq_line(m, '.', 0, 40 + 1 + 12 * (14 + 1)); 507 seq_puts(m, "\n"); 508 } 509 } 510 511 static void seq_header(struct seq_file *m) 512 { 513 seq_puts(m, "lock_stat version 0.4\n"); 514 515 if (unlikely(!debug_locks)) 516 seq_printf(m, "*WARNING* lock debugging disabled!! - possibly due to a lockdep warning\n"); 517 518 seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1)); 519 seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s " 520 "%14s %14s\n", 521 "class name", 522 "con-bounces", 523 "contentions", 524 "waittime-min", 525 "waittime-max", 526 "waittime-total", 527 "waittime-avg", 528 "acq-bounces", 529 "acquisitions", 530 "holdtime-min", 531 "holdtime-max", 532 "holdtime-total", 533 "holdtime-avg"); 534 seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1)); 535 seq_printf(m, "\n"); 536 } 537 538 static void *ls_start(struct seq_file *m, loff_t *pos) 539 { 540 struct lock_stat_seq *data = m->private; 541 struct lock_stat_data *iter; 542 543 if (*pos == 0) 544 return SEQ_START_TOKEN; 545 546 iter = data->stats + (*pos - 1); 547 if (iter >= data->iter_end) 548 iter = NULL; 549 550 return iter; 551 } 552 553 static void *ls_next(struct seq_file *m, void *v, loff_t *pos) 554 { 555 (*pos)++; 556 return ls_start(m, pos); 557 } 558 559 static void ls_stop(struct seq_file *m, void *v) 560 { 561 } 562 563 static int ls_show(struct seq_file *m, void *v) 564 { 565 if (v == SEQ_START_TOKEN) 566 seq_header(m); 567 else 568 seq_stats(m, v); 569 570 return 0; 571 } 572 573 static const struct seq_operations lockstat_ops = { 574 .start = ls_start, 575 .next = ls_next, 576 .stop = ls_stop, 577 .show = ls_show, 578 }; 579 580 static int lock_stat_open(struct inode *inode, struct file *file) 581 { 582 int res; 583 struct lock_class *class; 584 struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq)); 585 586 if (!data) 587 return -ENOMEM; 588 589 res = seq_open(file, &lockstat_ops); 590 if (!res) { 591 struct lock_stat_data *iter = data->stats; 592 struct seq_file *m = file->private_data; 593 594 list_for_each_entry(class, &all_lock_classes, lock_entry) { 595 iter->class = class; 596 iter->stats = lock_stats(class); 597 iter++; 598 } 599 data->iter_end = iter; 600 601 sort(data->stats, data->iter_end - data->stats, 602 sizeof(struct lock_stat_data), 603 lock_stat_cmp, NULL); 604 605 m->private = data; 606 } else 607 vfree(data); 608 609 return res; 610 } 611 612 static ssize_t lock_stat_write(struct file *file, const char __user *buf, 613 size_t count, loff_t *ppos) 614 { 615 struct lock_class *class; 616 char c; 617 618 if (count) { 619 if (get_user(c, buf)) 620 return -EFAULT; 621 622 if (c != '0') 623 return count; 624 625 list_for_each_entry(class, &all_lock_classes, lock_entry) 626 clear_lock_stats(class); 627 } 628 return count; 629 } 630 631 static int lock_stat_release(struct inode *inode, struct file *file) 632 { 633 struct seq_file *seq = file->private_data; 634 635 vfree(seq->private); 636 return seq_release(inode, file); 637 } 638 639 static const struct file_operations proc_lock_stat_operations = { 640 .open = lock_stat_open, 641 .write = lock_stat_write, 642 .read = seq_read, 643 .llseek = seq_lseek, 644 .release = lock_stat_release, 645 }; 646 #endif /* CONFIG_LOCK_STAT */ 647 648 static int __init lockdep_proc_init(void) 649 { 650 proc_create_seq("lockdep", S_IRUSR, NULL, &lockdep_ops); 651 #ifdef CONFIG_PROVE_LOCKING 652 proc_create_seq("lockdep_chains", S_IRUSR, NULL, &lockdep_chains_ops); 653 #endif 654 proc_create_single("lockdep_stats", S_IRUSR, NULL, lockdep_stats_show); 655 #ifdef CONFIG_LOCK_STAT 656 proc_create("lock_stat", S_IRUSR | S_IWUSR, NULL, 657 &proc_lock_stat_operations); 658 #endif 659 660 return 0; 661 } 662 663 __initcall(lockdep_proc_init); 664 665