debugfs.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) "kcsan: " fmt
  3. #include <linux/atomic.h>
  4. #include <linux/bsearch.h>
  5. #include <linux/bug.h>
  6. #include <linux/debugfs.h>
  7. #include <linux/init.h>
  8. #include <linux/kallsyms.h>
  9. #include <linux/sched.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/slab.h>
  12. #include <linux/sort.h>
  13. #include <linux/string.h>
  14. #include <linux/uaccess.h>
  15. #include "kcsan.h"
  16. atomic_long_t kcsan_counters[KCSAN_COUNTER_COUNT];
  17. static const char *const counter_names[] = {
  18. [KCSAN_COUNTER_USED_WATCHPOINTS] = "used_watchpoints",
  19. [KCSAN_COUNTER_SETUP_WATCHPOINTS] = "setup_watchpoints",
  20. [KCSAN_COUNTER_DATA_RACES] = "data_races",
  21. [KCSAN_COUNTER_ASSERT_FAILURES] = "assert_failures",
  22. [KCSAN_COUNTER_NO_CAPACITY] = "no_capacity",
  23. [KCSAN_COUNTER_REPORT_RACES] = "report_races",
  24. [KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN] = "races_unknown_origin",
  25. [KCSAN_COUNTER_UNENCODABLE_ACCESSES] = "unencodable_accesses",
  26. [KCSAN_COUNTER_ENCODING_FALSE_POSITIVES] = "encoding_false_positives",
  27. };
  28. static_assert(ARRAY_SIZE(counter_names) == KCSAN_COUNTER_COUNT);
  29. /*
  30. * Addresses for filtering functions from reporting. This list can be used as a
  31. * whitelist or blacklist.
  32. */
  33. static struct {
  34. unsigned long *addrs; /* array of addresses */
  35. size_t size; /* current size */
  36. int used; /* number of elements used */
  37. bool sorted; /* if elements are sorted */
  38. bool whitelist; /* if list is a blacklist or whitelist */
  39. } report_filterlist = {
  40. .addrs = NULL,
  41. .size = 8, /* small initial size */
  42. .used = 0,
  43. .sorted = false,
  44. .whitelist = false, /* default is blacklist */
  45. };
  46. static DEFINE_SPINLOCK(report_filterlist_lock);
  47. /*
  48. * The microbenchmark allows benchmarking KCSAN core runtime only. To run
  49. * multiple threads, pipe 'microbench=<iters>' from multiple tasks into the
  50. * debugfs file. This will not generate any conflicts, and tests fast-path only.
  51. */
  52. static noinline void microbenchmark(unsigned long iters)
  53. {
  54. const struct kcsan_ctx ctx_save = current->kcsan_ctx;
  55. const bool was_enabled = READ_ONCE(kcsan_enabled);
  56. cycles_t cycles;
  57. /* We may have been called from an atomic region; reset context. */
  58. memset(&current->kcsan_ctx, 0, sizeof(current->kcsan_ctx));
  59. /*
  60. * Disable to benchmark fast-path for all accesses, and (expected
  61. * negligible) call into slow-path, but never set up watchpoints.
  62. */
  63. WRITE_ONCE(kcsan_enabled, false);
  64. pr_info("%s begin | iters: %lu\n", __func__, iters);
  65. cycles = get_cycles();
  66. while (iters--) {
  67. unsigned long addr = iters & ((PAGE_SIZE << 8) - 1);
  68. int type = !(iters & 0x7f) ? KCSAN_ACCESS_ATOMIC :
  69. (!(iters & 0xf) ? KCSAN_ACCESS_WRITE : 0);
  70. __kcsan_check_access((void *)addr, sizeof(long), type);
  71. }
  72. cycles = get_cycles() - cycles;
  73. pr_info("%s end | cycles: %llu\n", __func__, cycles);
  74. WRITE_ONCE(kcsan_enabled, was_enabled);
  75. /* restore context */
  76. current->kcsan_ctx = ctx_save;
  77. }
  78. static int cmp_filterlist_addrs(const void *rhs, const void *lhs)
  79. {
  80. const unsigned long a = *(const unsigned long *)rhs;
  81. const unsigned long b = *(const unsigned long *)lhs;
  82. return a < b ? -1 : a == b ? 0 : 1;
  83. }
  84. bool kcsan_skip_report_debugfs(unsigned long func_addr)
  85. {
  86. unsigned long symbolsize, offset;
  87. unsigned long flags;
  88. bool ret = false;
  89. if (!kallsyms_lookup_size_offset(func_addr, &symbolsize, &offset))
  90. return false;
  91. func_addr -= offset; /* Get function start */
  92. spin_lock_irqsave(&report_filterlist_lock, flags);
  93. if (report_filterlist.used == 0)
  94. goto out;
  95. /* Sort array if it is unsorted, and then do a binary search. */
  96. if (!report_filterlist.sorted) {
  97. sort(report_filterlist.addrs, report_filterlist.used,
  98. sizeof(unsigned long), cmp_filterlist_addrs, NULL);
  99. report_filterlist.sorted = true;
  100. }
  101. ret = !!bsearch(&func_addr, report_filterlist.addrs,
  102. report_filterlist.used, sizeof(unsigned long),
  103. cmp_filterlist_addrs);
  104. if (report_filterlist.whitelist)
  105. ret = !ret;
  106. out:
  107. spin_unlock_irqrestore(&report_filterlist_lock, flags);
  108. return ret;
  109. }
  110. static void set_report_filterlist_whitelist(bool whitelist)
  111. {
  112. unsigned long flags;
  113. spin_lock_irqsave(&report_filterlist_lock, flags);
  114. report_filterlist.whitelist = whitelist;
  115. spin_unlock_irqrestore(&report_filterlist_lock, flags);
  116. }
  117. /* Returns 0 on success, error-code otherwise. */
  118. static ssize_t insert_report_filterlist(const char *func)
  119. {
  120. unsigned long flags;
  121. unsigned long addr = kallsyms_lookup_name(func);
  122. ssize_t ret = 0;
  123. if (!addr) {
  124. pr_err("could not find function: '%s'\n", func);
  125. return -ENOENT;
  126. }
  127. spin_lock_irqsave(&report_filterlist_lock, flags);
  128. if (report_filterlist.addrs == NULL) {
  129. /* initial allocation */
  130. report_filterlist.addrs =
  131. kmalloc_array(report_filterlist.size,
  132. sizeof(unsigned long), GFP_ATOMIC);
  133. if (report_filterlist.addrs == NULL) {
  134. ret = -ENOMEM;
  135. goto out;
  136. }
  137. } else if (report_filterlist.used == report_filterlist.size) {
  138. /* resize filterlist */
  139. size_t new_size = report_filterlist.size * 2;
  140. unsigned long *new_addrs =
  141. krealloc(report_filterlist.addrs,
  142. new_size * sizeof(unsigned long), GFP_ATOMIC);
  143. if (new_addrs == NULL) {
  144. /* leave filterlist itself untouched */
  145. ret = -ENOMEM;
  146. goto out;
  147. }
  148. report_filterlist.size = new_size;
  149. report_filterlist.addrs = new_addrs;
  150. }
  151. /* Note: deduplicating should be done in userspace. */
  152. report_filterlist.addrs[report_filterlist.used++] =
  153. kallsyms_lookup_name(func);
  154. report_filterlist.sorted = false;
  155. out:
  156. spin_unlock_irqrestore(&report_filterlist_lock, flags);
  157. return ret;
  158. }
  159. static int show_info(struct seq_file *file, void *v)
  160. {
  161. int i;
  162. unsigned long flags;
  163. /* show stats */
  164. seq_printf(file, "enabled: %i\n", READ_ONCE(kcsan_enabled));
  165. for (i = 0; i < KCSAN_COUNTER_COUNT; ++i) {
  166. seq_printf(file, "%s: %ld\n", counter_names[i],
  167. atomic_long_read(&kcsan_counters[i]));
  168. }
  169. /* show filter functions, and filter type */
  170. spin_lock_irqsave(&report_filterlist_lock, flags);
  171. seq_printf(file, "\n%s functions: %s\n",
  172. report_filterlist.whitelist ? "whitelisted" : "blacklisted",
  173. report_filterlist.used == 0 ? "none" : "");
  174. for (i = 0; i < report_filterlist.used; ++i)
  175. seq_printf(file, " %ps\n", (void *)report_filterlist.addrs[i]);
  176. spin_unlock_irqrestore(&report_filterlist_lock, flags);
  177. return 0;
  178. }
  179. static int debugfs_open(struct inode *inode, struct file *file)
  180. {
  181. return single_open(file, show_info, NULL);
  182. }
  183. static ssize_t
  184. debugfs_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
  185. {
  186. char kbuf[KSYM_NAME_LEN];
  187. char *arg;
  188. int read_len = count < (sizeof(kbuf) - 1) ? count : (sizeof(kbuf) - 1);
  189. if (copy_from_user(kbuf, buf, read_len))
  190. return -EFAULT;
  191. kbuf[read_len] = '\0';
  192. arg = strstrip(kbuf);
  193. if (!strcmp(arg, "on")) {
  194. WRITE_ONCE(kcsan_enabled, true);
  195. } else if (!strcmp(arg, "off")) {
  196. WRITE_ONCE(kcsan_enabled, false);
  197. } else if (str_has_prefix(arg, "microbench=")) {
  198. unsigned long iters;
  199. if (kstrtoul(&arg[strlen("microbench=")], 0, &iters))
  200. return -EINVAL;
  201. microbenchmark(iters);
  202. } else if (!strcmp(arg, "whitelist")) {
  203. set_report_filterlist_whitelist(true);
  204. } else if (!strcmp(arg, "blacklist")) {
  205. set_report_filterlist_whitelist(false);
  206. } else if (arg[0] == '!') {
  207. ssize_t ret = insert_report_filterlist(&arg[1]);
  208. if (ret < 0)
  209. return ret;
  210. } else {
  211. return -EINVAL;
  212. }
  213. return count;
  214. }
  215. static const struct file_operations debugfs_ops =
  216. {
  217. .read = seq_read,
  218. .open = debugfs_open,
  219. .write = debugfs_write,
  220. .release = single_release
  221. };
  222. static int __init kcsan_debugfs_init(void)
  223. {
  224. debugfs_create_file("kcsan", 0644, NULL, NULL, &debugfs_ops);
  225. return 0;
  226. }
  227. late_initcall(kcsan_debugfs_init);