blob: 7cd34285df7405800021bcc61a9acd2343a6f70c [file] [log] [blame]
Marco Elverdfd402a2019-11-14 19:02:54 +01001// SPDX-License-Identifier: GPL-2.0
2
Marco Elver05f9a402020-01-10 19:48:34 +01003#include <linux/jiffies.h>
Marco Elverdfd402a2019-11-14 19:02:54 +01004#include <linux/kernel.h>
Marco Elverf1bc9622020-01-15 17:25:12 +01005#include <linux/lockdep.h>
Marco Elverdfd402a2019-11-14 19:02:54 +01006#include <linux/preempt.h>
7#include <linux/printk.h>
8#include <linux/sched.h>
9#include <linux/spinlock.h>
10#include <linux/stacktrace.h>
11
12#include "kcsan.h"
13#include "encoding.h"
14
15/*
16 * Max. number of stack entries to show in the report.
17 */
18#define NUM_STACK_ENTRIES 64
19
20/*
21 * Other thread info: communicated from other racing thread to thread that set
22 * up the watchpoint, which then prints the complete report atomically. Only
23 * need one struct, as all threads should to be serialized regardless to print
24 * the reports, with reporting being in the slow-path.
25 */
26static struct {
Ingo Molnar5cbaefe2019-11-20 10:41:43 +010027 const volatile void *ptr;
28 size_t size;
Marco Elver47144ec2020-01-10 19:48:33 +010029 int access_type;
Ingo Molnar5cbaefe2019-11-20 10:41:43 +010030 int task_pid;
31 int cpu_id;
32 unsigned long stack_entries[NUM_STACK_ENTRIES];
33 int num_stack_entries;
Marco Elverdfd402a2019-11-14 19:02:54 +010034} other_info = { .ptr = NULL };
35
36/*
Marco Elver05f9a402020-01-10 19:48:34 +010037 * Information about reported data races; used to rate limit reporting.
38 */
39struct report_time {
40 /*
41 * The last time the data race was reported.
42 */
43 unsigned long time;
44
45 /*
46 * The frames of the 2 threads; if only 1 thread is known, one frame
47 * will be 0.
48 */
49 unsigned long frame1;
50 unsigned long frame2;
51};
52
53/*
54 * Since we also want to be able to debug allocators with KCSAN, to avoid
55 * deadlock, report_times cannot be dynamically resized with krealloc in
56 * rate_limit_report.
57 *
58 * Therefore, we use a fixed-size array, which at most will occupy a page. This
59 * still adequately rate limits reports, assuming that a) number of unique data
60 * races is not excessive, and b) occurrence of unique data races within the
61 * same time window is limited.
62 */
63#define REPORT_TIMES_MAX (PAGE_SIZE / sizeof(struct report_time))
64#define REPORT_TIMES_SIZE \
65 (CONFIG_KCSAN_REPORT_ONCE_IN_MS > REPORT_TIMES_MAX ? \
66 REPORT_TIMES_MAX : \
67 CONFIG_KCSAN_REPORT_ONCE_IN_MS)
68static struct report_time report_times[REPORT_TIMES_SIZE];
69
70/*
Marco Elverdfd402a2019-11-14 19:02:54 +010071 * This spinlock protects reporting and other_info, since other_info is usually
72 * required when reporting.
73 */
74static DEFINE_SPINLOCK(report_lock);
75
76/*
Marco Elver05f9a402020-01-10 19:48:34 +010077 * Checks if the data race identified by thread frames frame1 and frame2 has
78 * been reported since (now - KCSAN_REPORT_ONCE_IN_MS).
79 */
80static bool rate_limit_report(unsigned long frame1, unsigned long frame2)
81{
82 struct report_time *use_entry = &report_times[0];
83 unsigned long invalid_before;
84 int i;
85
86 BUILD_BUG_ON(CONFIG_KCSAN_REPORT_ONCE_IN_MS != 0 && REPORT_TIMES_SIZE == 0);
87
88 if (CONFIG_KCSAN_REPORT_ONCE_IN_MS == 0)
89 return false;
90
91 invalid_before = jiffies - msecs_to_jiffies(CONFIG_KCSAN_REPORT_ONCE_IN_MS);
92
93 /* Check if a matching data race report exists. */
94 for (i = 0; i < REPORT_TIMES_SIZE; ++i) {
95 struct report_time *rt = &report_times[i];
96
97 /*
98 * Must always select an entry for use to store info as we
99 * cannot resize report_times; at the end of the scan, use_entry
100 * will be the oldest entry, which ideally also happened before
101 * KCSAN_REPORT_ONCE_IN_MS ago.
102 */
103 if (time_before(rt->time, use_entry->time))
104 use_entry = rt;
105
106 /*
107 * Initially, no need to check any further as this entry as well
108 * as following entries have never been used.
109 */
110 if (rt->time == 0)
111 break;
112
113 /* Check if entry expired. */
114 if (time_before(rt->time, invalid_before))
115 continue; /* before KCSAN_REPORT_ONCE_IN_MS ago */
116
117 /* Reported recently, check if data race matches. */
118 if ((rt->frame1 == frame1 && rt->frame2 == frame2) ||
119 (rt->frame1 == frame2 && rt->frame2 == frame1))
120 return true;
121 }
122
123 use_entry->time = jiffies;
124 use_entry->frame1 = frame1;
125 use_entry->frame2 = frame2;
126 return false;
127}
128
129/*
Marco Elverdfd402a2019-11-14 19:02:54 +0100130 * Special rules to skip reporting.
131 */
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100132static bool
Marco Elverad4f8ee2020-01-29 16:01:02 +0100133skip_report(bool value_change, unsigned long top_frame)
Marco Elverdfd402a2019-11-14 19:02:54 +0100134{
Marco Elverad4f8ee2020-01-29 16:01:02 +0100135 /*
136 * The first call to skip_report always has value_change==true, since we
137 * cannot know the value written of an instrumented access. For the 2nd
138 * call there are 6 cases with CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY:
139 *
140 * 1. read watchpoint, conflicting write (value_change==true): report;
141 * 2. read watchpoint, conflicting write (value_change==false): skip;
142 * 3. write watchpoint, conflicting write (value_change==true): report;
143 * 4. write watchpoint, conflicting write (value_change==false): skip;
144 * 5. write watchpoint, conflicting read (value_change==false): skip;
145 * 6. write watchpoint, conflicting read (value_change==true): impossible;
146 *
147 * Cases 1-4 are intuitive and expected; case 5 ensures we do not report
148 * data races where the write may have rewritten the same value; and
149 * case 6 is simply impossible.
150 */
151 if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) && !value_change) {
Marco Elverdfd402a2019-11-14 19:02:54 +0100152 /*
153 * The access is a write, but the data value did not change.
154 *
155 * We opt-out of this filter for certain functions at request of
156 * maintainers.
157 */
158 char buf[64];
159
160 snprintf(buf, sizeof(buf), "%ps", (void *)top_frame);
161 if (!strnstr(buf, "rcu_", sizeof(buf)) &&
162 !strnstr(buf, "_rcu", sizeof(buf)) &&
163 !strnstr(buf, "_srcu", sizeof(buf)))
164 return true;
165 }
166
167 return kcsan_skip_report_debugfs(top_frame);
168}
169
Marco Elver47144ec2020-01-10 19:48:33 +0100170static const char *get_access_type(int type)
Marco Elverdfd402a2019-11-14 19:02:54 +0100171{
Marco Elver47144ec2020-01-10 19:48:33 +0100172 switch (type) {
173 case 0:
174 return "read";
175 case KCSAN_ACCESS_ATOMIC:
176 return "read (marked)";
177 case KCSAN_ACCESS_WRITE:
178 return "write";
179 case KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
180 return "write (marked)";
181 default:
182 BUG();
183 }
Marco Elverdfd402a2019-11-14 19:02:54 +0100184}
185
186/* Return thread description: in task or interrupt. */
187static const char *get_thread_desc(int task_id)
188{
189 if (task_id != -1) {
190 static char buf[32]; /* safe: protected by report_lock */
191
192 snprintf(buf, sizeof(buf), "task %i", task_id);
193 return buf;
194 }
195 return "interrupt";
196}
197
198/* Helper to skip KCSAN-related functions in stack-trace. */
199static int get_stack_skipnr(unsigned long stack_entries[], int num_entries)
200{
201 char buf[64];
202 int skip = 0;
203
204 for (; skip < num_entries; ++skip) {
205 snprintf(buf, sizeof(buf), "%ps", (void *)stack_entries[skip]);
206 if (!strnstr(buf, "csan_", sizeof(buf)) &&
207 !strnstr(buf, "tsan_", sizeof(buf)) &&
208 !strnstr(buf, "_once_size", sizeof(buf))) {
209 break;
210 }
211 }
212 return skip;
213}
214
215/* Compares symbolized strings of addr1 and addr2. */
216static int sym_strcmp(void *addr1, void *addr2)
217{
218 char buf1[64];
219 char buf2[64];
220
221 snprintf(buf1, sizeof(buf1), "%pS", addr1);
222 snprintf(buf2, sizeof(buf2), "%pS", addr2);
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100223
Marco Elverdfd402a2019-11-14 19:02:54 +0100224 return strncmp(buf1, buf2, sizeof(buf1));
225}
226
227/*
228 * Returns true if a report was generated, false otherwise.
229 */
Marco Elver47144ec2020-01-10 19:48:33 +0100230static bool print_report(const volatile void *ptr, size_t size, int access_type,
Marco Elverdfd402a2019-11-14 19:02:54 +0100231 bool value_change, int cpu_id,
232 enum kcsan_report_type type)
233{
234 unsigned long stack_entries[NUM_STACK_ENTRIES] = { 0 };
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100235 int num_stack_entries = stack_trace_save(stack_entries, NUM_STACK_ENTRIES, 1);
Marco Elverdfd402a2019-11-14 19:02:54 +0100236 int skipnr = get_stack_skipnr(stack_entries, num_stack_entries);
Marco Elver05f9a402020-01-10 19:48:34 +0100237 unsigned long this_frame = stack_entries[skipnr];
238 unsigned long other_frame = 0;
239 int other_skipnr = 0; /* silence uninit warnings */
Marco Elverdfd402a2019-11-14 19:02:54 +0100240
241 /*
242 * Must check report filter rules before starting to print.
243 */
Marco Elverad4f8ee2020-01-29 16:01:02 +0100244 if (skip_report(true, stack_entries[skipnr]))
Marco Elverdfd402a2019-11-14 19:02:54 +0100245 return false;
246
247 if (type == KCSAN_REPORT_RACE_SIGNAL) {
248 other_skipnr = get_stack_skipnr(other_info.stack_entries,
249 other_info.num_stack_entries);
Marco Elver05f9a402020-01-10 19:48:34 +0100250 other_frame = other_info.stack_entries[other_skipnr];
Marco Elverdfd402a2019-11-14 19:02:54 +0100251
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100252 /* @value_change is only known for the other thread */
Marco Elverad4f8ee2020-01-29 16:01:02 +0100253 if (skip_report(value_change, other_frame))
Marco Elverdfd402a2019-11-14 19:02:54 +0100254 return false;
255 }
256
Marco Elver05f9a402020-01-10 19:48:34 +0100257 if (rate_limit_report(this_frame, other_frame))
258 return false;
259
Marco Elverdfd402a2019-11-14 19:02:54 +0100260 /* Print report header. */
261 pr_err("==================================================================\n");
262 switch (type) {
263 case KCSAN_REPORT_RACE_SIGNAL: {
Marco Elverdfd402a2019-11-14 19:02:54 +0100264 int cmp;
265
266 /*
267 * Order functions lexographically for consistent bug titles.
268 * Do not print offset of functions to keep title short.
269 */
Marco Elver05f9a402020-01-10 19:48:34 +0100270 cmp = sym_strcmp((void *)other_frame, (void *)this_frame);
Marco Elverdfd402a2019-11-14 19:02:54 +0100271 pr_err("BUG: KCSAN: data-race in %ps / %ps\n",
Marco Elver05f9a402020-01-10 19:48:34 +0100272 (void *)(cmp < 0 ? other_frame : this_frame),
273 (void *)(cmp < 0 ? this_frame : other_frame));
Marco Elverdfd402a2019-11-14 19:02:54 +0100274 } break;
275
276 case KCSAN_REPORT_RACE_UNKNOWN_ORIGIN:
Marco Elver05f9a402020-01-10 19:48:34 +0100277 pr_err("BUG: KCSAN: data-race in %pS\n", (void *)this_frame);
Marco Elverdfd402a2019-11-14 19:02:54 +0100278 break;
279
280 default:
281 BUG();
282 }
283
284 pr_err("\n");
285
286 /* Print information about the racing accesses. */
287 switch (type) {
288 case KCSAN_REPORT_RACE_SIGNAL:
289 pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n",
Marco Elver47144ec2020-01-10 19:48:33 +0100290 get_access_type(other_info.access_type), other_info.ptr,
Marco Elverdfd402a2019-11-14 19:02:54 +0100291 other_info.size, get_thread_desc(other_info.task_pid),
292 other_info.cpu_id);
293
294 /* Print the other thread's stack trace. */
295 stack_trace_print(other_info.stack_entries + other_skipnr,
296 other_info.num_stack_entries - other_skipnr,
297 0);
298
299 pr_err("\n");
300 pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n",
Marco Elver47144ec2020-01-10 19:48:33 +0100301 get_access_type(access_type), ptr, size,
Marco Elverdfd402a2019-11-14 19:02:54 +0100302 get_thread_desc(in_task() ? task_pid_nr(current) : -1),
303 cpu_id);
304 break;
305
306 case KCSAN_REPORT_RACE_UNKNOWN_ORIGIN:
307 pr_err("race at unknown origin, with %s to 0x%px of %zu bytes by %s on cpu %i:\n",
Marco Elver47144ec2020-01-10 19:48:33 +0100308 get_access_type(access_type), ptr, size,
Marco Elverdfd402a2019-11-14 19:02:54 +0100309 get_thread_desc(in_task() ? task_pid_nr(current) : -1),
310 cpu_id);
311 break;
312
313 default:
314 BUG();
315 }
316 /* Print stack trace of this thread. */
317 stack_trace_print(stack_entries + skipnr, num_stack_entries - skipnr,
318 0);
319
320 /* Print report footer. */
321 pr_err("\n");
322 pr_err("Reported by Kernel Concurrency Sanitizer on:\n");
323 dump_stack_print_info(KERN_DEFAULT);
324 pr_err("==================================================================\n");
325
326 return true;
327}
328
329static void release_report(unsigned long *flags, enum kcsan_report_type type)
330{
331 if (type == KCSAN_REPORT_RACE_SIGNAL)
332 other_info.ptr = NULL; /* mark for reuse */
333
334 spin_unlock_irqrestore(&report_lock, *flags);
335}
336
337/*
338 * Depending on the report type either sets other_info and returns false, or
339 * acquires the matching other_info and returns true. If other_info is not
340 * required for the report type, simply acquires report_lock and returns true.
341 */
342static bool prepare_report(unsigned long *flags, const volatile void *ptr,
Marco Elver47144ec2020-01-10 19:48:33 +0100343 size_t size, int access_type, int cpu_id,
Marco Elverdfd402a2019-11-14 19:02:54 +0100344 enum kcsan_report_type type)
345{
346 if (type != KCSAN_REPORT_CONSUMED_WATCHPOINT &&
347 type != KCSAN_REPORT_RACE_SIGNAL) {
348 /* other_info not required; just acquire report_lock */
349 spin_lock_irqsave(&report_lock, *flags);
350 return true;
351 }
352
353retry:
354 spin_lock_irqsave(&report_lock, *flags);
355
356 switch (type) {
357 case KCSAN_REPORT_CONSUMED_WATCHPOINT:
358 if (other_info.ptr != NULL)
359 break; /* still in use, retry */
360
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100361 other_info.ptr = ptr;
362 other_info.size = size;
Marco Elver47144ec2020-01-10 19:48:33 +0100363 other_info.access_type = access_type;
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100364 other_info.task_pid = in_task() ? task_pid_nr(current) : -1;
365 other_info.cpu_id = cpu_id;
366 other_info.num_stack_entries = stack_trace_save(other_info.stack_entries, NUM_STACK_ENTRIES, 1);
Marco Elverdfd402a2019-11-14 19:02:54 +0100367
368 spin_unlock_irqrestore(&report_lock, *flags);
369
370 /*
371 * The other thread will print the summary; other_info may now
372 * be consumed.
373 */
374 return false;
375
376 case KCSAN_REPORT_RACE_SIGNAL:
377 if (other_info.ptr == NULL)
378 break; /* no data available yet, retry */
379
380 /*
381 * First check if this is the other_info we are expecting, i.e.
382 * matches based on how watchpoint was encoded.
383 */
384 if (!matching_access((unsigned long)other_info.ptr &
385 WATCHPOINT_ADDR_MASK,
386 other_info.size,
387 (unsigned long)ptr & WATCHPOINT_ADDR_MASK,
388 size))
389 break; /* mismatching watchpoint, retry */
390
391 if (!matching_access((unsigned long)other_info.ptr,
392 other_info.size, (unsigned long)ptr,
393 size)) {
394 /*
395 * If the actual accesses to not match, this was a false
396 * positive due to watchpoint encoding.
397 */
398 kcsan_counter_inc(
399 KCSAN_COUNTER_ENCODING_FALSE_POSITIVES);
400
401 /* discard this other_info */
402 release_report(flags, KCSAN_REPORT_RACE_SIGNAL);
403 return false;
404 }
405
406 /*
407 * Matching & usable access in other_info: keep other_info_lock
408 * locked, as this thread consumes it to print the full report;
409 * unlocked in release_report.
410 */
411 return true;
412
413 default:
414 BUG();
415 }
416
417 spin_unlock_irqrestore(&report_lock, *flags);
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100418
Marco Elverdfd402a2019-11-14 19:02:54 +0100419 goto retry;
420}
421
Marco Elver47144ec2020-01-10 19:48:33 +0100422void kcsan_report(const volatile void *ptr, size_t size, int access_type,
Marco Elverdfd402a2019-11-14 19:02:54 +0100423 bool value_change, int cpu_id, enum kcsan_report_type type)
424{
425 unsigned long flags = 0;
426
Marco Elverf1bc9622020-01-15 17:25:12 +0100427 /*
428 * With TRACE_IRQFLAGS, lockdep's IRQ trace state becomes corrupted if
429 * we do not turn off lockdep here; this could happen due to recursion
430 * into lockdep via KCSAN if we detect a data race in utilities used by
431 * lockdep.
432 */
433 lockdep_off();
434
Marco Elverdfd402a2019-11-14 19:02:54 +0100435 kcsan_disable_current();
Marco Elver47144ec2020-01-10 19:48:33 +0100436 if (prepare_report(&flags, ptr, size, access_type, cpu_id, type)) {
437 if (print_report(ptr, size, access_type, value_change, cpu_id, type) && panic_on_warn)
Marco Elverdfd402a2019-11-14 19:02:54 +0100438 panic("panic_on_warn set ...\n");
439
440 release_report(&flags, type);
441 }
442 kcsan_enable_current();
Marco Elverf1bc9622020-01-15 17:25:12 +0100443
444 lockdep_on();
Marco Elverdfd402a2019-11-14 19:02:54 +0100445}