lockdep_proc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * kernel/lockdep_proc.c
  3. *
  4. * Runtime locking correctness validator
  5. *
  6. * Started by Ingo Molnar:
  7. *
  8. * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  9. *
  10. * Code for /proc/lockdep and /proc/lockdep_stats:
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/debug_locks.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/sort.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/div64.h>
  22. #include "lockdep_internals.h"
  23. static void *l_next(struct seq_file *m, void *v, loff_t *pos)
  24. {
  25. struct lock_class *class = v;
  26. (*pos)++;
  27. if (class->lock_entry.next != &all_lock_classes)
  28. class = list_entry(class->lock_entry.next, struct lock_class,
  29. lock_entry);
  30. else
  31. class = NULL;
  32. m->private = class;
  33. return class;
  34. }
  35. static void *l_start(struct seq_file *m, loff_t *pos)
  36. {
  37. struct lock_class *class = m->private;
  38. if (&class->lock_entry == all_lock_classes.next)
  39. seq_printf(m, "all lock classes:\n");
  40. return class;
  41. }
  42. static void l_stop(struct seq_file *m, void *v)
  43. {
  44. }
  45. static unsigned long count_forward_deps(struct lock_class *class)
  46. {
  47. struct lock_list *entry;
  48. unsigned long ret = 1;
  49. /*
  50. * Recurse this class's dependency list:
  51. */
  52. list_for_each_entry(entry, &class->locks_after, entry)
  53. ret += count_forward_deps(entry->class);
  54. return ret;
  55. }
  56. static unsigned long count_backward_deps(struct lock_class *class)
  57. {
  58. struct lock_list *entry;
  59. unsigned long ret = 1;
  60. /*
  61. * Recurse this class's dependency list:
  62. */
  63. list_for_each_entry(entry, &class->locks_before, entry)
  64. ret += count_backward_deps(entry->class);
  65. return ret;
  66. }
  67. static void print_name(struct seq_file *m, struct lock_class *class)
  68. {
  69. char str[128];
  70. const char *name = class->name;
  71. if (!name) {
  72. name = __get_key_name(class->key, str);
  73. seq_printf(m, "%s", name);
  74. } else{
  75. seq_printf(m, "%s", name);
  76. if (class->name_version > 1)
  77. seq_printf(m, "#%d", class->name_version);
  78. if (class->subclass)
  79. seq_printf(m, "/%d", class->subclass);
  80. }
  81. }
  82. static int l_show(struct seq_file *m, void *v)
  83. {
  84. unsigned long nr_forward_deps, nr_backward_deps;
  85. struct lock_class *class = m->private;
  86. struct lock_list *entry;
  87. char c1, c2, c3, c4;
  88. seq_printf(m, "%p", class->key);
  89. #ifdef CONFIG_DEBUG_LOCKDEP
  90. seq_printf(m, " OPS:%8ld", class->ops);
  91. #endif
  92. nr_forward_deps = count_forward_deps(class);
  93. seq_printf(m, " FD:%5ld", nr_forward_deps);
  94. nr_backward_deps = count_backward_deps(class);
  95. seq_printf(m, " BD:%5ld", nr_backward_deps);
  96. get_usage_chars(class, &c1, &c2, &c3, &c4);
  97. seq_printf(m, " %c%c%c%c", c1, c2, c3, c4);
  98. seq_printf(m, ": ");
  99. print_name(m, class);
  100. seq_puts(m, "\n");
  101. list_for_each_entry(entry, &class->locks_after, entry) {
  102. if (entry->distance == 1) {
  103. seq_printf(m, " -> [%p] ", entry->class);
  104. print_name(m, entry->class);
  105. seq_puts(m, "\n");
  106. }
  107. }
  108. seq_puts(m, "\n");
  109. return 0;
  110. }
  111. static const struct seq_operations lockdep_ops = {
  112. .start = l_start,
  113. .next = l_next,
  114. .stop = l_stop,
  115. .show = l_show,
  116. };
  117. static int lockdep_open(struct inode *inode, struct file *file)
  118. {
  119. int res = seq_open(file, &lockdep_ops);
  120. if (!res) {
  121. struct seq_file *m = file->private_data;
  122. if (!list_empty(&all_lock_classes))
  123. m->private = list_entry(all_lock_classes.next,
  124. struct lock_class, lock_entry);
  125. else
  126. m->private = NULL;
  127. }
  128. return res;
  129. }
  130. static const struct file_operations proc_lockdep_operations = {
  131. .open = lockdep_open,
  132. .read = seq_read,
  133. .llseek = seq_lseek,
  134. .release = seq_release,
  135. };
  136. static void lockdep_stats_debug_show(struct seq_file *m)
  137. {
  138. #ifdef CONFIG_DEBUG_LOCKDEP
  139. unsigned int hi1 = debug_atomic_read(&hardirqs_on_events),
  140. hi2 = debug_atomic_read(&hardirqs_off_events),
  141. hr1 = debug_atomic_read(&redundant_hardirqs_on),
  142. hr2 = debug_atomic_read(&redundant_hardirqs_off),
  143. si1 = debug_atomic_read(&softirqs_on_events),
  144. si2 = debug_atomic_read(&softirqs_off_events),
  145. sr1 = debug_atomic_read(&redundant_softirqs_on),
  146. sr2 = debug_atomic_read(&redundant_softirqs_off);
  147. seq_printf(m, " chain lookup misses: %11u\n",
  148. debug_atomic_read(&chain_lookup_misses));
  149. seq_printf(m, " chain lookup hits: %11u\n",
  150. debug_atomic_read(&chain_lookup_hits));
  151. seq_printf(m, " cyclic checks: %11u\n",
  152. debug_atomic_read(&nr_cyclic_checks));
  153. seq_printf(m, " cyclic-check recursions: %11u\n",
  154. debug_atomic_read(&nr_cyclic_check_recursions));
  155. seq_printf(m, " find-mask forwards checks: %11u\n",
  156. debug_atomic_read(&nr_find_usage_forwards_checks));
  157. seq_printf(m, " find-mask forwards recursions: %11u\n",
  158. debug_atomic_read(&nr_find_usage_forwards_recursions));
  159. seq_printf(m, " find-mask backwards checks: %11u\n",
  160. debug_atomic_read(&nr_find_usage_backwards_checks));
  161. seq_printf(m, " find-mask backwards recursions:%11u\n",
  162. debug_atomic_read(&nr_find_usage_backwards_recursions));
  163. seq_printf(m, " hardirq on events: %11u\n", hi1);
  164. seq_printf(m, " hardirq off events: %11u\n", hi2);
  165. seq_printf(m, " redundant hardirq ons: %11u\n", hr1);
  166. seq_printf(m, " redundant hardirq offs: %11u\n", hr2);
  167. seq_printf(m, " softirq on events: %11u\n", si1);
  168. seq_printf(m, " softirq off events: %11u\n", si2);
  169. seq_printf(m, " redundant softirq ons: %11u\n", sr1);
  170. seq_printf(m, " redundant softirq offs: %11u\n", sr2);
  171. #endif
  172. }
  173. static int lockdep_stats_show(struct seq_file *m, void *v)
  174. {
  175. struct lock_class *class;
  176. unsigned long nr_unused = 0, nr_uncategorized = 0,
  177. nr_irq_safe = 0, nr_irq_unsafe = 0,
  178. nr_softirq_safe = 0, nr_softirq_unsafe = 0,
  179. nr_hardirq_safe = 0, nr_hardirq_unsafe = 0,
  180. nr_irq_read_safe = 0, nr_irq_read_unsafe = 0,
  181. nr_softirq_read_safe = 0, nr_softirq_read_unsafe = 0,
  182. nr_hardirq_read_safe = 0, nr_hardirq_read_unsafe = 0,
  183. sum_forward_deps = 0, factor = 0;
  184. list_for_each_entry(class, &all_lock_classes, lock_entry) {
  185. if (class->usage_mask == 0)
  186. nr_unused++;
  187. if (class->usage_mask == LOCKF_USED)
  188. nr_uncategorized++;
  189. if (class->usage_mask & LOCKF_USED_IN_IRQ)
  190. nr_irq_safe++;
  191. if (class->usage_mask & LOCKF_ENABLED_IRQS)
  192. nr_irq_unsafe++;
  193. if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ)
  194. nr_softirq_safe++;
  195. if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS)
  196. nr_softirq_unsafe++;
  197. if (class->usage_mask & LOCKF_USED_IN_HARDIRQ)
  198. nr_hardirq_safe++;
  199. if (class->usage_mask & LOCKF_ENABLED_HARDIRQS)
  200. nr_hardirq_unsafe++;
  201. if (class->usage_mask & LOCKF_USED_IN_IRQ_READ)
  202. nr_irq_read_safe++;
  203. if (class->usage_mask & LOCKF_ENABLED_IRQS_READ)
  204. nr_irq_read_unsafe++;
  205. if (class->usage_mask & LOCKF_USED_IN_SOFTIRQ_READ)
  206. nr_softirq_read_safe++;
  207. if (class->usage_mask & LOCKF_ENABLED_SOFTIRQS_READ)
  208. nr_softirq_read_unsafe++;
  209. if (class->usage_mask & LOCKF_USED_IN_HARDIRQ_READ)
  210. nr_hardirq_read_safe++;
  211. if (class->usage_mask & LOCKF_ENABLED_HARDIRQS_READ)
  212. nr_hardirq_read_unsafe++;
  213. sum_forward_deps += count_forward_deps(class);
  214. }
  215. #ifdef CONFIG_DEBUG_LOCKDEP
  216. DEBUG_LOCKS_WARN_ON(debug_atomic_read(&nr_unused_locks) != nr_unused);
  217. #endif
  218. seq_printf(m, " lock-classes: %11lu [max: %lu]\n",
  219. nr_lock_classes, MAX_LOCKDEP_KEYS);
  220. seq_printf(m, " direct dependencies: %11lu [max: %lu]\n",
  221. nr_list_entries, MAX_LOCKDEP_ENTRIES);
  222. seq_printf(m, " indirect dependencies: %11lu\n",
  223. sum_forward_deps);
  224. /*
  225. * Total number of dependencies:
  226. *
  227. * All irq-safe locks may nest inside irq-unsafe locks,
  228. * plus all the other known dependencies:
  229. */
  230. seq_printf(m, " all direct dependencies: %11lu\n",
  231. nr_irq_unsafe * nr_irq_safe +
  232. nr_hardirq_unsafe * nr_hardirq_safe +
  233. nr_list_entries);
  234. /*
  235. * Estimated factor between direct and indirect
  236. * dependencies:
  237. */
  238. if (nr_list_entries)
  239. factor = sum_forward_deps / nr_list_entries;
  240. #ifdef CONFIG_PROVE_LOCKING
  241. seq_printf(m, " dependency chains: %11lu [max: %lu]\n",
  242. nr_lock_chains, MAX_LOCKDEP_CHAINS);
  243. #endif
  244. #ifdef CONFIG_TRACE_IRQFLAGS
  245. seq_printf(m, " in-hardirq chains: %11u\n",
  246. nr_hardirq_chains);
  247. seq_printf(m, " in-softirq chains: %11u\n",
  248. nr_softirq_chains);
  249. #endif
  250. seq_printf(m, " in-process chains: %11u\n",
  251. nr_process_chains);
  252. seq_printf(m, " stack-trace entries: %11lu [max: %lu]\n",
  253. nr_stack_trace_entries, MAX_STACK_TRACE_ENTRIES);
  254. seq_printf(m, " combined max dependencies: %11u\n",
  255. (nr_hardirq_chains + 1) *
  256. (nr_softirq_chains + 1) *
  257. (nr_process_chains + 1)
  258. );
  259. seq_printf(m, " hardirq-safe locks: %11lu\n",
  260. nr_hardirq_safe);
  261. seq_printf(m, " hardirq-unsafe locks: %11lu\n",
  262. nr_hardirq_unsafe);
  263. seq_printf(m, " softirq-safe locks: %11lu\n",
  264. nr_softirq_safe);
  265. seq_printf(m, " softirq-unsafe locks: %11lu\n",
  266. nr_softirq_unsafe);
  267. seq_printf(m, " irq-safe locks: %11lu\n",
  268. nr_irq_safe);
  269. seq_printf(m, " irq-unsafe locks: %11lu\n",
  270. nr_irq_unsafe);
  271. seq_printf(m, " hardirq-read-safe locks: %11lu\n",
  272. nr_hardirq_read_safe);
  273. seq_printf(m, " hardirq-read-unsafe locks: %11lu\n",
  274. nr_hardirq_read_unsafe);
  275. seq_printf(m, " softirq-read-safe locks: %11lu\n",
  276. nr_softirq_read_safe);
  277. seq_printf(m, " softirq-read-unsafe locks: %11lu\n",
  278. nr_softirq_read_unsafe);
  279. seq_printf(m, " irq-read-safe locks: %11lu\n",
  280. nr_irq_read_safe);
  281. seq_printf(m, " irq-read-unsafe locks: %11lu\n",
  282. nr_irq_read_unsafe);
  283. seq_printf(m, " uncategorized locks: %11lu\n",
  284. nr_uncategorized);
  285. seq_printf(m, " unused locks: %11lu\n",
  286. nr_unused);
  287. seq_printf(m, " max locking depth: %11u\n",
  288. max_lockdep_depth);
  289. seq_printf(m, " max recursion depth: %11u\n",
  290. max_recursion_depth);
  291. lockdep_stats_debug_show(m);
  292. seq_printf(m, " debug_locks: %11u\n",
  293. debug_locks);
  294. return 0;
  295. }
  296. static int lockdep_stats_open(struct inode *inode, struct file *file)
  297. {
  298. return single_open(file, lockdep_stats_show, NULL);
  299. }
  300. static const struct file_operations proc_lockdep_stats_operations = {
  301. .open = lockdep_stats_open,
  302. .read = seq_read,
  303. .llseek = seq_lseek,
  304. .release = seq_release,
  305. };
  306. #ifdef CONFIG_LOCK_STAT
  307. struct lock_stat_data {
  308. struct lock_class *class;
  309. struct lock_class_stats stats;
  310. };
  311. struct lock_stat_seq {
  312. struct lock_stat_data *iter;
  313. struct lock_stat_data *iter_end;
  314. struct lock_stat_data stats[MAX_LOCKDEP_KEYS];
  315. };
  316. /*
  317. * sort on absolute number of contentions
  318. */
  319. static int lock_stat_cmp(const void *l, const void *r)
  320. {
  321. const struct lock_stat_data *dl = l, *dr = r;
  322. unsigned long nl, nr;
  323. nl = dl->stats.read_waittime.nr + dl->stats.write_waittime.nr;
  324. nr = dr->stats.read_waittime.nr + dr->stats.write_waittime.nr;
  325. return nr - nl;
  326. }
  327. static void seq_line(struct seq_file *m, char c, int offset, int length)
  328. {
  329. int i;
  330. for (i = 0; i < offset; i++)
  331. seq_puts(m, " ");
  332. for (i = 0; i < length; i++)
  333. seq_printf(m, "%c", c);
  334. seq_puts(m, "\n");
  335. }
  336. static void snprint_time(char *buf, size_t bufsiz, s64 nr)
  337. {
  338. unsigned long rem;
  339. rem = do_div(nr, 1000); /* XXX: do_div_signed */
  340. snprintf(buf, bufsiz, "%lld.%02d", (long long)nr, ((int)rem+5)/10);
  341. }
  342. static void seq_time(struct seq_file *m, s64 time)
  343. {
  344. char num[15];
  345. snprint_time(num, sizeof(num), time);
  346. seq_printf(m, " %14s", num);
  347. }
  348. static void seq_lock_time(struct seq_file *m, struct lock_time *lt)
  349. {
  350. seq_printf(m, "%14lu", lt->nr);
  351. seq_time(m, lt->min);
  352. seq_time(m, lt->max);
  353. seq_time(m, lt->total);
  354. }
  355. static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
  356. {
  357. char name[39];
  358. struct lock_class *class;
  359. struct lock_class_stats *stats;
  360. int i, namelen;
  361. class = data->class;
  362. stats = &data->stats;
  363. snprintf(name, 38, "%s", class->name);
  364. namelen = strlen(name);
  365. if (stats->write_holdtime.nr) {
  366. if (stats->read_holdtime.nr)
  367. seq_printf(m, "%38s-W:", name);
  368. else
  369. seq_printf(m, "%40s:", name);
  370. seq_lock_time(m, &stats->write_waittime);
  371. seq_puts(m, " ");
  372. seq_lock_time(m, &stats->write_holdtime);
  373. seq_puts(m, "\n");
  374. }
  375. if (stats->read_holdtime.nr) {
  376. seq_printf(m, "%38s-R:", name);
  377. seq_lock_time(m, &stats->read_waittime);
  378. seq_puts(m, " ");
  379. seq_lock_time(m, &stats->read_holdtime);
  380. seq_puts(m, "\n");
  381. }
  382. if (stats->read_waittime.nr + stats->write_waittime.nr == 0)
  383. return;
  384. if (stats->read_holdtime.nr)
  385. namelen += 2;
  386. for (i = 0; i < ARRAY_SIZE(class->contention_point); i++) {
  387. char sym[KSYM_SYMBOL_LEN];
  388. char ip[32];
  389. if (class->contention_point[i] == 0)
  390. break;
  391. if (!i)
  392. seq_line(m, '-', 40-namelen, namelen);
  393. sprint_symbol(sym, class->contention_point[i]);
  394. snprintf(ip, sizeof(ip), "[<%p>]",
  395. (void *)class->contention_point[i]);
  396. seq_printf(m, "%40s %14lu %29s %s\n", name,
  397. stats->contention_point[i],
  398. ip, sym);
  399. }
  400. if (i) {
  401. seq_puts(m, "\n");
  402. seq_line(m, '.', 0, 40 + 1 + 8 * (14 + 1));
  403. seq_puts(m, "\n");
  404. }
  405. }
  406. static void seq_header(struct seq_file *m)
  407. {
  408. seq_printf(m, "lock_stat version 0.1\n");
  409. seq_line(m, '-', 0, 40 + 1 + 8 * (14 + 1));
  410. seq_printf(m, "%40s %14s %14s %14s %14s %14s %14s %14s %14s\n",
  411. "class name",
  412. "contentions",
  413. "waittime-min",
  414. "waittime-max",
  415. "waittime-total",
  416. "acquisitions",
  417. "holdtime-min",
  418. "holdtime-max",
  419. "holdtime-total");
  420. seq_line(m, '-', 0, 40 + 1 + 8 * (14 + 1));
  421. seq_printf(m, "\n");
  422. }
  423. static void *ls_start(struct seq_file *m, loff_t *pos)
  424. {
  425. struct lock_stat_seq *data = m->private;
  426. if (data->iter == data->stats)
  427. seq_header(m);
  428. return data->iter;
  429. }
  430. static void *ls_next(struct seq_file *m, void *v, loff_t *pos)
  431. {
  432. struct lock_stat_seq *data = m->private;
  433. (*pos)++;
  434. data->iter = v;
  435. data->iter++;
  436. if (data->iter == data->iter_end)
  437. data->iter = NULL;
  438. return data->iter;
  439. }
  440. static void ls_stop(struct seq_file *m, void *v)
  441. {
  442. }
  443. static int ls_show(struct seq_file *m, void *v)
  444. {
  445. struct lock_stat_seq *data = m->private;
  446. seq_stats(m, data->iter);
  447. return 0;
  448. }
  449. static struct seq_operations lockstat_ops = {
  450. .start = ls_start,
  451. .next = ls_next,
  452. .stop = ls_stop,
  453. .show = ls_show,
  454. };
  455. static int lock_stat_open(struct inode *inode, struct file *file)
  456. {
  457. int res;
  458. struct lock_class *class;
  459. struct lock_stat_seq *data = vmalloc(sizeof(struct lock_stat_seq));
  460. if (!data)
  461. return -ENOMEM;
  462. res = seq_open(file, &lockstat_ops);
  463. if (!res) {
  464. struct lock_stat_data *iter = data->stats;
  465. struct seq_file *m = file->private_data;
  466. data->iter = iter;
  467. list_for_each_entry(class, &all_lock_classes, lock_entry) {
  468. iter->class = class;
  469. iter->stats = lock_stats(class);
  470. iter++;
  471. }
  472. data->iter_end = iter;
  473. sort(data->stats, data->iter_end - data->iter,
  474. sizeof(struct lock_stat_data),
  475. lock_stat_cmp, NULL);
  476. m->private = data;
  477. } else
  478. vfree(data);
  479. return res;
  480. }
  481. static ssize_t lock_stat_write(struct file *file, const char __user *buf,
  482. size_t count, loff_t *ppos)
  483. {
  484. struct lock_class *class;
  485. char c;
  486. if (count) {
  487. if (get_user(c, buf))
  488. return -EFAULT;
  489. if (c != '0')
  490. return count;
  491. list_for_each_entry(class, &all_lock_classes, lock_entry)
  492. clear_lock_stats(class);
  493. }
  494. return count;
  495. }
  496. static int lock_stat_release(struct inode *inode, struct file *file)
  497. {
  498. struct seq_file *seq = file->private_data;
  499. vfree(seq->private);
  500. seq->private = NULL;
  501. return seq_release(inode, file);
  502. }
  503. static const struct file_operations proc_lock_stat_operations = {
  504. .open = lock_stat_open,
  505. .write = lock_stat_write,
  506. .read = seq_read,
  507. .llseek = seq_lseek,
  508. .release = lock_stat_release,
  509. };
  510. #endif /* CONFIG_LOCK_STAT */
  511. static int __init lockdep_proc_init(void)
  512. {
  513. struct proc_dir_entry *entry;
  514. entry = create_proc_entry("lockdep", S_IRUSR, NULL);
  515. if (entry)
  516. entry->proc_fops = &proc_lockdep_operations;
  517. entry = create_proc_entry("lockdep_stats", S_IRUSR, NULL);
  518. if (entry)
  519. entry->proc_fops = &proc_lockdep_stats_operations;
  520. #ifdef CONFIG_LOCK_STAT
  521. entry = create_proc_entry("lock_stat", S_IRUSR, NULL);
  522. if (entry)
  523. entry->proc_fops = &proc_lock_stat_operations;
  524. #endif
  525. return 0;
  526. }
  527. __initcall(lockdep_proc_init);