lock_events.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * Authors: Waiman Long <[email protected]>
  14. */
  15. /*
  16. * Collect locking event counts
  17. */
  18. #include <linux/debugfs.h>
  19. #include <linux/sched.h>
  20. #include <linux/sched/clock.h>
  21. #include <linux/fs.h>
  22. #include "lock_events.h"
  23. #undef LOCK_EVENT
  24. #define LOCK_EVENT(name) [LOCKEVENT_ ## name] = #name,
  25. #define LOCK_EVENTS_DIR "lock_event_counts"
  26. /*
  27. * When CONFIG_LOCK_EVENT_COUNTS is enabled, event counts of different
  28. * types of locks will be reported under the <debugfs>/lock_event_counts/
  29. * directory. See lock_events_list.h for the list of available locking
  30. * events.
  31. *
  32. * Writing to the special ".reset_counts" file will reset all the above
  33. * locking event counts. This is a very slow operation and so should not
  34. * be done frequently.
  35. *
  36. * These event counts are implemented as per-cpu variables which are
  37. * summed and computed whenever the corresponding debugfs files are read. This
  38. * minimizes added overhead making the counts usable even in a production
  39. * environment.
  40. */
  41. static const char * const lockevent_names[lockevent_num + 1] = {
  42. #include "lock_events_list.h"
  43. [LOCKEVENT_reset_cnts] = ".reset_counts",
  44. };
  45. /*
  46. * Per-cpu counts
  47. */
  48. DEFINE_PER_CPU(unsigned long, lockevents[lockevent_num]);
  49. /*
  50. * The lockevent_read() function can be overridden.
  51. */
  52. ssize_t __weak lockevent_read(struct file *file, char __user *user_buf,
  53. size_t count, loff_t *ppos)
  54. {
  55. char buf[64];
  56. int cpu, id, len;
  57. u64 sum = 0;
  58. /*
  59. * Get the counter ID stored in file->f_inode->i_private
  60. */
  61. id = (long)file_inode(file)->i_private;
  62. if (id >= lockevent_num)
  63. return -EBADF;
  64. for_each_possible_cpu(cpu)
  65. sum += per_cpu(lockevents[id], cpu);
  66. len = snprintf(buf, sizeof(buf) - 1, "%llu\n", sum);
  67. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  68. }
  69. /*
  70. * Function to handle write request
  71. *
  72. * When idx = reset_cnts, reset all the counts.
  73. */
  74. static ssize_t lockevent_write(struct file *file, const char __user *user_buf,
  75. size_t count, loff_t *ppos)
  76. {
  77. int cpu;
  78. /*
  79. * Get the counter ID stored in file->f_inode->i_private
  80. */
  81. if ((long)file_inode(file)->i_private != LOCKEVENT_reset_cnts)
  82. return count;
  83. for_each_possible_cpu(cpu) {
  84. int i;
  85. unsigned long *ptr = per_cpu_ptr(lockevents, cpu);
  86. for (i = 0 ; i < lockevent_num; i++)
  87. WRITE_ONCE(ptr[i], 0);
  88. }
  89. return count;
  90. }
  91. /*
  92. * Debugfs data structures
  93. */
  94. static const struct file_operations fops_lockevent = {
  95. .read = lockevent_read,
  96. .write = lockevent_write,
  97. .llseek = default_llseek,
  98. };
  99. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  100. #include <asm/paravirt.h>
  101. static bool __init skip_lockevent(const char *name)
  102. {
  103. static int pv_on __initdata = -1;
  104. if (pv_on < 0)
  105. pv_on = !pv_is_native_spin_unlock();
  106. /*
  107. * Skip PV qspinlock events on bare metal.
  108. */
  109. if (!pv_on && !memcmp(name, "pv_", 3))
  110. return true;
  111. return false;
  112. }
  113. #else
  114. static inline bool skip_lockevent(const char *name)
  115. {
  116. return false;
  117. }
  118. #endif
  119. /*
  120. * Initialize debugfs for the locking event counts.
  121. */
  122. static int __init init_lockevent_counts(void)
  123. {
  124. struct dentry *d_counts = debugfs_create_dir(LOCK_EVENTS_DIR, NULL);
  125. int i;
  126. if (!d_counts)
  127. goto out;
  128. /*
  129. * Create the debugfs files
  130. *
  131. * As reading from and writing to the stat files can be slow, only
  132. * root is allowed to do the read/write to limit impact to system
  133. * performance.
  134. */
  135. for (i = 0; i < lockevent_num; i++) {
  136. if (skip_lockevent(lockevent_names[i]))
  137. continue;
  138. if (!debugfs_create_file(lockevent_names[i], 0400, d_counts,
  139. (void *)(long)i, &fops_lockevent))
  140. goto fail_undo;
  141. }
  142. if (!debugfs_create_file(lockevent_names[LOCKEVENT_reset_cnts], 0200,
  143. d_counts, (void *)(long)LOCKEVENT_reset_cnts,
  144. &fops_lockevent))
  145. goto fail_undo;
  146. return 0;
  147. fail_undo:
  148. debugfs_remove_recursive(d_counts);
  149. out:
  150. pr_warn("Could not create '%s' debugfs entries\n", LOCK_EVENTS_DIR);
  151. return -ENOMEM;
  152. }
  153. fs_initcall(init_lockevent_counts);