1 // SPDX-License-Identifier: GPL-2.0
3 #define pr_fmt(fmt) "kcsan: " fmt
5 #include <linux/atomic.h>
6 #include <linux/bsearch.h>
8 #include <linux/debugfs.h>
9 #include <linux/init.h>
10 #include <linux/kallsyms.h>
11 #include <linux/sched.h>
12 #include <linux/seq_file.h>
13 #include <linux/slab.h>
14 #include <linux/sort.h>
15 #include <linux/string.h>
16 #include <linux/uaccess.h>
21 * Statistics counters.
23 static atomic_long_t counters[KCSAN_COUNTER_COUNT];
24 static const char *const counter_names[] = {
25 [KCSAN_COUNTER_USED_WATCHPOINTS] = "used_watchpoints",
26 [KCSAN_COUNTER_SETUP_WATCHPOINTS] = "setup_watchpoints",
27 [KCSAN_COUNTER_DATA_RACES] = "data_races",
28 [KCSAN_COUNTER_ASSERT_FAILURES] = "assert_failures",
29 [KCSAN_COUNTER_NO_CAPACITY] = "no_capacity",
30 [KCSAN_COUNTER_REPORT_RACES] = "report_races",
31 [KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN] = "races_unknown_origin",
32 [KCSAN_COUNTER_UNENCODABLE_ACCESSES] = "unencodable_accesses",
33 [KCSAN_COUNTER_ENCODING_FALSE_POSITIVES] = "encoding_false_positives",
35 static_assert(ARRAY_SIZE(counter_names) == KCSAN_COUNTER_COUNT);
38 * Addresses for filtering functions from reporting. This list can be used as a
39 * whitelist or blacklist.
42 unsigned long *addrs; /* array of addresses */
43 size_t size; /* current size */
44 int used; /* number of elements used */
45 bool sorted; /* if elements are sorted */
46 bool whitelist; /* if list is a blacklist or whitelist */
47 } report_filterlist = {
49 .size = 8, /* small initial size */
52 .whitelist = false, /* default is blacklist */
54 static DEFINE_SPINLOCK(report_filterlist_lock);
56 void kcsan_counter_inc(enum kcsan_counter_id id)
58 atomic_long_inc(&counters[id]);
61 void kcsan_counter_dec(enum kcsan_counter_id id)
63 atomic_long_dec(&counters[id]);
67 * The microbenchmark allows benchmarking KCSAN core runtime only. To run
68 * multiple threads, pipe 'microbench=<iters>' from multiple tasks into the
69 * debugfs file. This will not generate any conflicts, and tests fast-path only.
71 static noinline void microbenchmark(unsigned long iters)
73 const struct kcsan_ctx ctx_save = current->kcsan_ctx;
74 const bool was_enabled = READ_ONCE(kcsan_enabled);
77 /* We may have been called from an atomic region; reset context. */
78 memset(¤t->kcsan_ctx, 0, sizeof(current->kcsan_ctx));
80 * Disable to benchmark fast-path for all accesses, and (expected
81 * negligible) call into slow-path, but never set up watchpoints.
83 WRITE_ONCE(kcsan_enabled, false);
85 pr_info("%s begin | iters: %lu\n", __func__, iters);
87 cycles = get_cycles();
89 unsigned long addr = iters & ((PAGE_SIZE << 8) - 1);
90 int type = !(iters & 0x7f) ? KCSAN_ACCESS_ATOMIC :
91 (!(iters & 0xf) ? KCSAN_ACCESS_WRITE : 0);
92 __kcsan_check_access((void *)addr, sizeof(long), type);
94 cycles = get_cycles() - cycles;
96 pr_info("%s end | cycles: %llu\n", __func__, cycles);
98 WRITE_ONCE(kcsan_enabled, was_enabled);
100 current->kcsan_ctx = ctx_save;
103 static int cmp_filterlist_addrs(const void *rhs, const void *lhs)
105 const unsigned long a = *(const unsigned long *)rhs;
106 const unsigned long b = *(const unsigned long *)lhs;
108 return a < b ? -1 : a == b ? 0 : 1;
111 bool kcsan_skip_report_debugfs(unsigned long func_addr)
113 unsigned long symbolsize, offset;
117 if (!kallsyms_lookup_size_offset(func_addr, &symbolsize, &offset))
119 func_addr -= offset; /* Get function start */
121 spin_lock_irqsave(&report_filterlist_lock, flags);
122 if (report_filterlist.used == 0)
125 /* Sort array if it is unsorted, and then do a binary search. */
126 if (!report_filterlist.sorted) {
127 sort(report_filterlist.addrs, report_filterlist.used,
128 sizeof(unsigned long), cmp_filterlist_addrs, NULL);
129 report_filterlist.sorted = true;
131 ret = !!bsearch(&func_addr, report_filterlist.addrs,
132 report_filterlist.used, sizeof(unsigned long),
133 cmp_filterlist_addrs);
134 if (report_filterlist.whitelist)
138 spin_unlock_irqrestore(&report_filterlist_lock, flags);
142 static void set_report_filterlist_whitelist(bool whitelist)
146 spin_lock_irqsave(&report_filterlist_lock, flags);
147 report_filterlist.whitelist = whitelist;
148 spin_unlock_irqrestore(&report_filterlist_lock, flags);
151 /* Returns 0 on success, error-code otherwise. */
152 static ssize_t insert_report_filterlist(const char *func)
155 unsigned long addr = kallsyms_lookup_name(func);
159 pr_err("could not find function: '%s'\n", func);
163 spin_lock_irqsave(&report_filterlist_lock, flags);
165 if (report_filterlist.addrs == NULL) {
166 /* initial allocation */
167 report_filterlist.addrs =
168 kmalloc_array(report_filterlist.size,
169 sizeof(unsigned long), GFP_ATOMIC);
170 if (report_filterlist.addrs == NULL) {
174 } else if (report_filterlist.used == report_filterlist.size) {
175 /* resize filterlist */
176 size_t new_size = report_filterlist.size * 2;
177 unsigned long *new_addrs =
178 krealloc(report_filterlist.addrs,
179 new_size * sizeof(unsigned long), GFP_ATOMIC);
181 if (new_addrs == NULL) {
182 /* leave filterlist itself untouched */
187 report_filterlist.size = new_size;
188 report_filterlist.addrs = new_addrs;
191 /* Note: deduplicating should be done in userspace. */
192 report_filterlist.addrs[report_filterlist.used++] =
193 kallsyms_lookup_name(func);
194 report_filterlist.sorted = false;
197 spin_unlock_irqrestore(&report_filterlist_lock, flags);
202 static int show_info(struct seq_file *file, void *v)
208 seq_printf(file, "enabled: %i\n", READ_ONCE(kcsan_enabled));
209 for (i = 0; i < KCSAN_COUNTER_COUNT; ++i)
210 seq_printf(file, "%s: %ld\n", counter_names[i], atomic_long_read(&counters[i]));
212 /* show filter functions, and filter type */
213 spin_lock_irqsave(&report_filterlist_lock, flags);
214 seq_printf(file, "\n%s functions: %s\n",
215 report_filterlist.whitelist ? "whitelisted" : "blacklisted",
216 report_filterlist.used == 0 ? "none" : "");
217 for (i = 0; i < report_filterlist.used; ++i)
218 seq_printf(file, " %ps\n", (void *)report_filterlist.addrs[i]);
219 spin_unlock_irqrestore(&report_filterlist_lock, flags);
224 static int debugfs_open(struct inode *inode, struct file *file)
226 return single_open(file, show_info, NULL);
230 debugfs_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
232 char kbuf[KSYM_NAME_LEN];
234 int read_len = count < (sizeof(kbuf) - 1) ? count : (sizeof(kbuf) - 1);
236 if (copy_from_user(kbuf, buf, read_len))
238 kbuf[read_len] = '\0';
239 arg = strstrip(kbuf);
241 if (!strcmp(arg, "on")) {
242 WRITE_ONCE(kcsan_enabled, true);
243 } else if (!strcmp(arg, "off")) {
244 WRITE_ONCE(kcsan_enabled, false);
245 } else if (str_has_prefix(arg, "microbench=")) {
248 if (kstrtoul(&arg[strlen("microbench=")], 0, &iters))
250 microbenchmark(iters);
251 } else if (!strcmp(arg, "whitelist")) {
252 set_report_filterlist_whitelist(true);
253 } else if (!strcmp(arg, "blacklist")) {
254 set_report_filterlist_whitelist(false);
255 } else if (arg[0] == '!') {
256 ssize_t ret = insert_report_filterlist(&arg[1]);
267 static const struct file_operations debugfs_ops =
270 .open = debugfs_open,
271 .write = debugfs_write,
272 .release = single_release
275 void __init kcsan_debugfs_init(void)
277 debugfs_create_file("kcsan", 0644, NULL, NULL, &debugfs_ops);