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 <[email protected]> 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", class->ops); 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 SEQ_START_TOKEN; 109 110 if (*pos - 1 < nr_lock_chains) 111 return lock_chains + (*pos - 1); 112 113 return NULL; 114 } 115 116 static void *lc_next(struct seq_file *m, void *v, loff_t *pos) 117 { 118 (*pos)++; 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 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++; 221 if (class->usage_mask & LOCKF_ENABLED_IRQ) 222 nr_irq_unsafe++; 223 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ) 224 nr_softirq_safe++; 225 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ) 226 nr_softirq_unsafe++; 227 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ) 228 nr_hardirq_safe++; 229 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ) 230 nr_hardirq_unsafe++; 231 if (class->usage_mask & LOCKF_USED_IN_IRQ_READ) 232 nr_irq_read_safe++; 233 if (class->usage_mask & LOCKF_ENABLED_IRQ_READ) 234 nr_irq_read_unsafe++; 235 if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ) 236 nr_softirq_read_safe++; 237 if (class->usage_mask & LOCKF_ENABLED_SOFTIRQ_READ) 238 nr_softirq_read_unsafe++; 239 if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ) 240 nr_hardirq_read_safe++; 241 if (class->usage_mask & LOCKF_ENABLED_HARDIRQ_READ) 242 nr_hardirq_read_unsafe++; 243 244 #ifdef CONFIG_PROVE_LOCKING 245 sum_forward_deps += lockdep_count_forward_deps(class); 246 #endif 247 } 248 #ifdef CONFIG_DEBUG_LOCKDEP 249 DEBUG_LOCKS_WARN_ON(debug_atomic_read(nr_unused_locks) != nr_unused); 250 #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 269 #ifdef CONFIG_PROVE_LOCKING 270 seq_printf(m, " dependency chains: %11lu [max: %lu]\n", 271 nr_lock_chains, MAX_LOCKDEP_CHAINS); 272 seq_printf(m, " dependency chain hlocks: %11d [max: %lu]\n", 273 nr_chain_hlocks, MAX_LOCKDEP_CHAIN_HLOCKS); 274 #endif 275 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); 323 #ifdef CONFIG_PROVE_LOCKING 324 seq_printf(m, " max bfs queue depth: %11u\n", 325 max_bfs_queue_depth); 326 #endif 327 lockdep_stats_debug_show(m); 328 seq_printf(m, " debug_locks: %11u\n", 329 debug_locks); 330 331 return 0; 332 } 333 334 static int lockdep_stats_open(struct inode *inode, struct file *file) 335 { 336 return single_open(file, lockdep_stats_show, NULL); 337 } 338 339 static const struct file_operations proc_lockdep_stats_operations = { 340 .open = lockdep_stats_open, 341 .read = seq_read, 342 .llseek = seq_lseek, 343 .release = single_release, 344 }; 345 346 #ifdef CONFIG_LOCK_STAT 347 348 struct lock_stat_data { 349 struct lock_class *class; 350 struct lock_class_stats stats; 351 }; 352 353 struct lock_stat_seq { 354 struct lock_stat_data *iter_end; 355 struct lock_stat_data stats[MAX_LOCKDEP_KEYS]; 356 }; 357 358 /* 359 * sort on absolute number of contentions 360 */ 361 static int lock_stat_cmp(const void *l, const void *r) 362 { 363 const struct lock_stat_data *dl = l, *dr = r; 364 unsigned long nl, nr; 365 366 nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr; 367 nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr; 368 369 return nr - nl; 370 } 371 372 static void seq_line(struct seq_file *m, char c, int offset, int length) 373 { 374 int i; 375 376 for (i = 0; i < offset; i++) 377 seq_puts(m, " "); 378 for (i = 0; i < length; i++) 379 seq_printf(m, "%c", c); 380 seq_puts(m, "\n"); 381 } 382 383 static void snprint_time(char *buf, size_t bufsiz, s64 nr) 384 { 385 s64 div; 386 s32 rem; 387 388 nr += 5; /* for display rounding */ 389 div = div_s64_rem(nr, 1000, &rem); 390 snprintf(buf, bufsiz, "%lld.%02d", (long long)div, (int)rem/10); 391 } 392 393 static void seq_time(struct seq_file *m, s64 time) 394 { 395 char num[15]; 396 397 snprint_time(num, sizeof(num), time); 398 seq_printf(m, " %14s", num); 399 } 400 401 static void seq_lock_time(struct seq_file *m, struct lock_time *lt) 402 { 403 seq_printf(m, "%14lu", lt->nr); 404 seq_time(m, lt->min); 405 seq_time(m, lt->max); 406 seq_time(m, lt->total); 407 seq_time(m, lt->nr ? div_s64(lt->total, lt->nr) : 0); 408 } 409 410 static void seq_stats(struct seq_file *m, struct lock_stat_data *data) 411 { 412 struct lockdep_subclass_key *ckey; 413 struct lock_class_stats *stats; 414 struct lock_class *class; 415 const char *cname; 416 int i, namelen; 417 char name[39]; 418 419 class = data->class; 420 stats = &data->stats; 421 422 namelen = 38; 423 if (class->name_version > 1) 424 namelen -= 2; /* XXX truncates versions > 9 */ 425 if (class->subclass) 426 namelen -= 2; 427 428 rcu_read_lock_sched(); 429 cname = rcu_dereference_sched(class->name); 430 ckey = rcu_dereference_sched(class->key); 431 432 if (!cname && !ckey) { 433 rcu_read_unlock_sched(); 434 return; 435 436 } else if (!cname) { 437 char str[KSYM_NAME_LEN]; 438 const char *key_name; 439 440 key_name = __get_key_name(ckey, str); 441 snprintf(name, namelen, "%s", key_name); 442 } else { 443 snprintf(name, namelen, "%s", cname); 444 } 445 rcu_read_unlock_sched(); 446 447 namelen = strlen(name); 448 if (class->name_version > 1) { 449 snprintf(name+namelen, 3, "#%d", class->name_version); 450 namelen += 2; 451 } 452 if (class->subclass) { 453 snprintf(name+namelen, 3, "/%d", class->subclass); 454 namelen += 2; 455 } 456 457 if (stats->write_holdtime.nr) { 458 if (stats->read_holdtime.nr) 459 seq_printf(m, "%38s-W:", name); 460 else 461 seq_printf(m, "%40s:", name); 462 463 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_write]); 464 seq_lock_time(m, &stats->write_waittime); 465 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_write]); 466 seq_lock_time(m, &stats->write_holdtime); 467 seq_puts(m, "\n"); 468 } 469 470 if (stats->read_holdtime.nr) { 471 seq_printf(m, "%38s-R:", name); 472 seq_printf(m, "%14lu ", stats->bounces[bounce_contended_read]); 473 seq_lock_time(m, &stats->read_waittime); 474 seq_printf(m, " %14lu ", stats->bounces[bounce_acquired_read]); 475 seq_lock_time(m, &stats->read_holdtime); 476 seq_puts(m, "\n"); 477 } 478 479 if (stats->read_waittime.nr + stats->write_waittime.nr == 0) 480 return; 481 482 if (stats->read_holdtime.nr) 483 namelen += 2; 484 485 for (i = 0; i < LOCKSTAT_POINTS; i++) { 486 char ip[32]; 487 488 if (class->contention_point[i] == 0) 489 break; 490 491 if (!i) 492 seq_line(m, '-', 40-namelen, namelen); 493 494 snprintf(ip, sizeof(ip), "[<%p>]", 495 (void *)class->contention_point[i]); 496 seq_printf(m, "%40s %14lu %29s %pS\n", 497 name, stats->contention_point[i], 498 ip, (void *)class->contention_point[i]); 499 } 500 for (i = 0; i < LOCKSTAT_POINTS; i++) { 501 char ip[32]; 502 503 if (class->contending_point[i] == 0) 504 break; 505 506 if (!i) 507 seq_line(m, '-', 40-namelen, namelen); 508 509 snprintf(ip, sizeof(ip), "[<%p>]", 510 (void *)class->contending_point[i]); 511 seq_printf(m, "%40s %14lu %29s %pS\n", 512 name, stats->contending_point[i], 513 ip, (void *)class->contending_point[i]); 514 } 515 if (i) { 516 seq_puts(m, "\n"); 517 seq_line(m, '.', 0, 40 + 1 + 12 * (14 + 1)); 518 seq_puts(m, "\n"); 519 } 520 } 521 522 static void seq_header(struct seq_file *m) 523 { 524 seq_puts(m, "lock_stat version 0.4\n"); 525 526 if (unlikely(!debug_locks)) 527 seq_printf(m, "*WARNING* lock debugging disabled!! - possibly due to a lockdep warning\n"); 528 529 seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1)); 530 seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s %14s %14s " 531 "%14s %14s\n", 532 "class name", 533 "con-bounces", 534 "contentions", 535 "waittime-min", 536 "waittime-max", 537 "waittime-total", 538 "waittime-avg", 539 "acq-bounces", 540 "acquisitions", 541 "holdtime-min", 542 "holdtime-max", 543 "holdtime-total", 544 "holdtime-avg"); 545 seq_line(m, '-', 0, 40 + 1 + 12 * (14 + 1)); 546 seq_printf(m, "\n"); 547 } 548 549 static void *ls_start(struct seq_file *m, loff_t *pos) 550 { 551 struct lock_stat_seq *data = m->private; 552 struct lock_stat_data *iter; 553 554 if (*pos == 0) 555 return SEQ_START_TOKEN; 556 557 iter = data->stats + (*pos - 1); 558 if (iter >= data->iter_end) 559 iter = NULL; 560 561 return iter; 562 } 563 564 static void *ls_next(struct seq_file *m, void *v, loff_t *pos) 565 { 566 (*pos)++; 567 return ls_start(m, pos); 568 } 569 570 static void ls_stop(struct seq_file *m, void *v) 571 { 572 } 573 574 static int ls_show(struct seq_file *m, void *v) 575 { 576 if (v == SEQ_START_TOKEN) 577 seq_header(m); 578 else 579 seq_stats(m, v); 580 581 return 0; 582 } 583 584 static const struct seq_operations lockstat_ops = { 585 .start = ls_start, 586 .next = ls_next, 587 .stop = ls_stop, 588 .show = ls_show, 589 }; 590 591 static int lock_stat_open(struct inode *inode, struct file *file) 592 { 593 int res; 594 struct lock_class *class; 595 struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq)); 596 597 if (!data) 598 return -ENOMEM; 599 600 res = seq_open(file, &lockstat_ops); 601 if (!res) { 602 struct lock_stat_data *iter = data->stats; 603 struct seq_file *m = file->private_data; 604 605 list_for_each_entry(class, &all_lock_classes, lock_entry) { 606 iter->class = class; 607 iter->stats = lock_stats(class); 608 iter++; 609 } 610 data->iter_end = iter; 611 612 sort(data->stats, data->iter_end - data->stats, 613 sizeof(struct lock_stat_data), 614 lock_stat_cmp, NULL); 615 616 m->private = data; 617 } else 618 vfree(data); 619 620 return res; 621 } 622 623 static ssize_t lock_stat_write(struct file *file, const char __user *buf, 624 size_t count, loff_t *ppos) 625 { 626 struct lock_class *class; 627 char c; 628 629 if (count) { 630 if (get_user(c, buf)) 631 return -EFAULT; 632 633 if (c != '0') 634 return count; 635 636 list_for_each_entry(class, &all_lock_classes, lock_entry) 637 clear_lock_stats(class); 638 } 639 return count; 640 } 641 642 static int lock_stat_release(struct inode *inode, struct file *file) 643 { 644 struct seq_file *seq = file->private_data; 645 646 vfree(seq->private); 647 return seq_release(inode, file); 648 } 649 650 static const struct file_operations proc_lock_stat_operations = { 651 .open = lock_stat_open, 652 .write = lock_stat_write, 653 .read = seq_read, 654 .llseek = seq_lseek, 655 .release = lock_stat_release, 656 }; 657 #endif /* CONFIG_LOCK_STAT */ 658 659 static int __init lockdep_proc_init(void) 660 { 661 proc_create_seq("lockdep", S_IRUSR, NULL, &lockdep_ops); 662 #ifdef CONFIG_PROVE_LOCKING 663 proc_create_seq("lockdep_chains", S_IRUSR, NULL, &lockdep_chains_ops); 664 #endif 665 proc_create("lockdep_stats", S_IRUSR, NULL, 666 &proc_lockdep_stats_operations); 667 668 #ifdef CONFIG_LOCK_STAT 669 proc_create("lock_stat", S_IRUSR | S_IWUSR, NULL, 670 &proc_lock_stat_operations); 671 #endif 672 673 return 0; 674 } 675 676 __initcall(lockdep_proc_init); 677 678