blob: 8bfa970965a1b9037e6190da49b02e86db046078 [file] [log] [blame]
Marco Elverdfd402a2019-11-14 19:02:54 +01001// SPDX-License-Identifier: GPL-2.0
Marco Elverbd0ccc42021-01-15 18:09:53 +01002/*
3 * KCSAN reporting.
4 *
5 * Copyright (C) 2019, Google LLC.
6 */
Marco Elverdfd402a2019-11-14 19:02:54 +01007
Marco Elver2402d0e2020-02-22 00:10:27 +01008#include <linux/debug_locks.h>
9#include <linux/delay.h>
Marco Elver05f9a402020-01-10 19:48:34 +010010#include <linux/jiffies.h>
Marco Elverdfd402a2019-11-14 19:02:54 +010011#include <linux/kernel.h>
Marco Elverf1bc9622020-01-15 17:25:12 +010012#include <linux/lockdep.h>
Marco Elverdfd402a2019-11-14 19:02:54 +010013#include <linux/preempt.h>
14#include <linux/printk.h>
15#include <linux/sched.h>
16#include <linux/spinlock.h>
17#include <linux/stacktrace.h>
18
19#include "kcsan.h"
20#include "encoding.h"
21
22/*
23 * Max. number of stack entries to show in the report.
24 */
25#define NUM_STACK_ENTRIES 64
26
Marco Elver135c0872020-03-18 18:38:44 +010027/* Common access info. */
28struct access_info {
29 const volatile void *ptr;
30 size_t size;
31 int access_type;
32 int task_pid;
33 int cpu_id;
34};
35
Marco Elverdfd402a2019-11-14 19:02:54 +010036/*
37 * Other thread info: communicated from other racing thread to thread that set
Marco Elver61194182020-03-18 18:38:45 +010038 * up the watchpoint, which then prints the complete report atomically.
Marco Elverdfd402a2019-11-14 19:02:54 +010039 */
Marco Elver135c0872020-03-18 18:38:44 +010040struct other_info {
41 struct access_info ai;
Ingo Molnar5cbaefe2019-11-20 10:41:43 +010042 unsigned long stack_entries[NUM_STACK_ENTRIES];
43 int num_stack_entries;
Marco Elver2402d0e2020-02-22 00:10:27 +010044
45 /*
46 * Optionally pass @current. Typically we do not need to pass @current
47 * via @other_info since just @task_pid is sufficient. Passing @current
48 * has additional overhead.
49 *
50 * To safely pass @current, we must either use get_task_struct/
51 * put_task_struct, or stall the thread that populated @other_info.
52 *
53 * We cannot rely on get_task_struct/put_task_struct in case
54 * release_report() races with a task being released, and would have to
55 * free it in release_report(). This may result in deadlock if we want
56 * to use KCSAN on the allocators.
57 *
58 * Since we also want to reliably print held locks for
59 * CONFIG_KCSAN_VERBOSE, the current implementation stalls the thread
60 * that populated @other_info until it has been consumed.
61 */
62 struct task_struct *task;
Marco Elver135c0872020-03-18 18:38:44 +010063};
64
Marco Elver61194182020-03-18 18:38:45 +010065/*
66 * To never block any producers of struct other_info, we need as many elements
67 * as we have watchpoints (upper bound on concurrent races to report).
68 */
69static struct other_info other_infos[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1];
Marco Elverdfd402a2019-11-14 19:02:54 +010070
71/*
Marco Elverd591ec32020-02-06 16:46:24 +010072 * Information about reported races; used to rate limit reporting.
Marco Elver05f9a402020-01-10 19:48:34 +010073 */
74struct report_time {
75 /*
Marco Elverd591ec32020-02-06 16:46:24 +010076 * The last time the race was reported.
Marco Elver05f9a402020-01-10 19:48:34 +010077 */
78 unsigned long time;
79
80 /*
81 * The frames of the 2 threads; if only 1 thread is known, one frame
82 * will be 0.
83 */
84 unsigned long frame1;
85 unsigned long frame2;
86};
87
88/*
89 * Since we also want to be able to debug allocators with KCSAN, to avoid
90 * deadlock, report_times cannot be dynamically resized with krealloc in
91 * rate_limit_report.
92 *
93 * Therefore, we use a fixed-size array, which at most will occupy a page. This
94 * still adequately rate limits reports, assuming that a) number of unique data
Marco Elverd591ec32020-02-06 16:46:24 +010095 * races is not excessive, and b) occurrence of unique races within the
Marco Elver05f9a402020-01-10 19:48:34 +010096 * same time window is limited.
97 */
98#define REPORT_TIMES_MAX (PAGE_SIZE / sizeof(struct report_time))
99#define REPORT_TIMES_SIZE \
100 (CONFIG_KCSAN_REPORT_ONCE_IN_MS > REPORT_TIMES_MAX ? \
101 REPORT_TIMES_MAX : \
102 CONFIG_KCSAN_REPORT_ONCE_IN_MS)
103static struct report_time report_times[REPORT_TIMES_SIZE];
104
105/*
Marco Elver61194182020-03-18 18:38:45 +0100106 * Spinlock serializing report generation, and access to @other_infos. Although
107 * it could make sense to have a finer-grained locking story for @other_infos,
108 * report generation needs to be serialized either way, so not much is gained.
Marco Elverdfd402a2019-11-14 19:02:54 +0100109 */
Marco Elver61194182020-03-18 18:38:45 +0100110static DEFINE_RAW_SPINLOCK(report_lock);
Marco Elverdfd402a2019-11-14 19:02:54 +0100111
112/*
Marco Elverd591ec32020-02-06 16:46:24 +0100113 * Checks if the race identified by thread frames frame1 and frame2 has
Marco Elver05f9a402020-01-10 19:48:34 +0100114 * been reported since (now - KCSAN_REPORT_ONCE_IN_MS).
115 */
116static bool rate_limit_report(unsigned long frame1, unsigned long frame2)
117{
118 struct report_time *use_entry = &report_times[0];
119 unsigned long invalid_before;
120 int i;
121
122 BUILD_BUG_ON(CONFIG_KCSAN_REPORT_ONCE_IN_MS != 0 && REPORT_TIMES_SIZE == 0);
123
124 if (CONFIG_KCSAN_REPORT_ONCE_IN_MS == 0)
125 return false;
126
127 invalid_before = jiffies - msecs_to_jiffies(CONFIG_KCSAN_REPORT_ONCE_IN_MS);
128
Marco Elverd591ec32020-02-06 16:46:24 +0100129 /* Check if a matching race report exists. */
Marco Elver05f9a402020-01-10 19:48:34 +0100130 for (i = 0; i < REPORT_TIMES_SIZE; ++i) {
131 struct report_time *rt = &report_times[i];
132
133 /*
134 * Must always select an entry for use to store info as we
135 * cannot resize report_times; at the end of the scan, use_entry
136 * will be the oldest entry, which ideally also happened before
137 * KCSAN_REPORT_ONCE_IN_MS ago.
138 */
139 if (time_before(rt->time, use_entry->time))
140 use_entry = rt;
141
142 /*
143 * Initially, no need to check any further as this entry as well
144 * as following entries have never been used.
145 */
146 if (rt->time == 0)
147 break;
148
149 /* Check if entry expired. */
150 if (time_before(rt->time, invalid_before))
151 continue; /* before KCSAN_REPORT_ONCE_IN_MS ago */
152
Marco Elverd591ec32020-02-06 16:46:24 +0100153 /* Reported recently, check if race matches. */
Marco Elver05f9a402020-01-10 19:48:34 +0100154 if ((rt->frame1 == frame1 && rt->frame2 == frame2) ||
155 (rt->frame1 == frame2 && rt->frame2 == frame1))
156 return true;
157 }
158
159 use_entry->time = jiffies;
160 use_entry->frame1 = frame1;
161 use_entry->frame2 = frame2;
162 return false;
163}
164
165/*
Marco Elverdfd402a2019-11-14 19:02:54 +0100166 * Special rules to skip reporting.
167 */
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100168static bool
Marco Elverb738f612020-02-11 17:04:21 +0100169skip_report(enum kcsan_value_change value_change, unsigned long top_frame)
Marco Elverdfd402a2019-11-14 19:02:54 +0100170{
Marco Elver81af89e2020-02-11 17:04:22 +0100171 /* Should never get here if value_change==FALSE. */
172 WARN_ON_ONCE(value_change == KCSAN_VALUE_CHANGE_FALSE);
173
Marco Elverad4f8ee2020-01-29 16:01:02 +0100174 /*
Marco Elverb738f612020-02-11 17:04:21 +0100175 * The first call to skip_report always has value_change==TRUE, since we
Marco Elverad4f8ee2020-01-29 16:01:02 +0100176 * cannot know the value written of an instrumented access. For the 2nd
177 * call there are 6 cases with CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY:
178 *
Marco Elverb738f612020-02-11 17:04:21 +0100179 * 1. read watchpoint, conflicting write (value_change==TRUE): report;
180 * 2. read watchpoint, conflicting write (value_change==MAYBE): skip;
181 * 3. write watchpoint, conflicting write (value_change==TRUE): report;
182 * 4. write watchpoint, conflicting write (value_change==MAYBE): skip;
183 * 5. write watchpoint, conflicting read (value_change==MAYBE): skip;
184 * 6. write watchpoint, conflicting read (value_change==TRUE): report;
Marco Elverad4f8ee2020-01-29 16:01:02 +0100185 *
186 * Cases 1-4 are intuitive and expected; case 5 ensures we do not report
Marco Elverd591ec32020-02-06 16:46:24 +0100187 * data races where the write may have rewritten the same value; case 6
188 * is possible either if the size is larger than what we check value
189 * changes for or the access type is KCSAN_ACCESS_ASSERT.
Marco Elverad4f8ee2020-01-29 16:01:02 +0100190 */
Marco Elverb738f612020-02-11 17:04:21 +0100191 if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) &&
192 value_change == KCSAN_VALUE_CHANGE_MAYBE) {
Marco Elverdfd402a2019-11-14 19:02:54 +0100193 /*
194 * The access is a write, but the data value did not change.
195 *
196 * We opt-out of this filter for certain functions at request of
197 * maintainers.
198 */
199 char buf[64];
Marco Elverf770ed12020-04-10 18:44:17 +0200200 int len = scnprintf(buf, sizeof(buf), "%ps", (void *)top_frame);
Marco Elverdfd402a2019-11-14 19:02:54 +0100201
Marco Elverf770ed12020-04-10 18:44:17 +0200202 if (!strnstr(buf, "rcu_", len) &&
203 !strnstr(buf, "_rcu", len) &&
204 !strnstr(buf, "_srcu", len))
Marco Elverdfd402a2019-11-14 19:02:54 +0100205 return true;
206 }
207
208 return kcsan_skip_report_debugfs(top_frame);
209}
210
Marco Elver47144ec2020-01-10 19:48:33 +0100211static const char *get_access_type(int type)
Marco Elverdfd402a2019-11-14 19:02:54 +0100212{
Marco Elver757a4ce2020-03-25 17:41:56 +0100213 if (type & KCSAN_ACCESS_ASSERT) {
214 if (type & KCSAN_ACCESS_SCOPED) {
215 if (type & KCSAN_ACCESS_WRITE)
216 return "assert no accesses (scoped)";
217 else
218 return "assert no writes (scoped)";
219 } else {
220 if (type & KCSAN_ACCESS_WRITE)
221 return "assert no accesses";
222 else
223 return "assert no writes";
224 }
225 }
226
Marco Elver47144ec2020-01-10 19:48:33 +0100227 switch (type) {
228 case 0:
229 return "read";
230 case KCSAN_ACCESS_ATOMIC:
231 return "read (marked)";
232 case KCSAN_ACCESS_WRITE:
233 return "write";
234 case KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
235 return "write (marked)";
Marco Elver14e2ac82020-07-24 09:00:01 +0200236 case KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE:
237 return "read-write";
238 case KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
239 return "read-write (marked)";
Marco Elver757a4ce2020-03-25 17:41:56 +0100240 case KCSAN_ACCESS_SCOPED:
241 return "read (scoped)";
242 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_ATOMIC:
243 return "read (marked, scoped)";
244 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE:
245 return "write (scoped)";
246 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
247 return "write (marked, scoped)";
Marco Elver47144ec2020-01-10 19:48:33 +0100248 default:
249 BUG();
250 }
Marco Elverdfd402a2019-11-14 19:02:54 +0100251}
252
Marco Elverd591ec32020-02-06 16:46:24 +0100253static const char *get_bug_type(int type)
254{
255 return (type & KCSAN_ACCESS_ASSERT) != 0 ? "assert: race" : "data-race";
256}
257
Marco Elverdfd402a2019-11-14 19:02:54 +0100258/* Return thread description: in task or interrupt. */
259static const char *get_thread_desc(int task_id)
260{
261 if (task_id != -1) {
262 static char buf[32]; /* safe: protected by report_lock */
263
264 snprintf(buf, sizeof(buf), "task %i", task_id);
265 return buf;
266 }
267 return "interrupt";
268}
269
270/* Helper to skip KCSAN-related functions in stack-trace. */
Marco Elver135c0872020-03-18 18:38:44 +0100271static int get_stack_skipnr(const unsigned long stack_entries[], int num_entries)
Marco Elverdfd402a2019-11-14 19:02:54 +0100272{
273 char buf[64];
Marco Elvercdb9b072020-04-10 18:44:18 +0200274 char *cur;
275 int len, skip;
Marco Elverdfd402a2019-11-14 19:02:54 +0100276
Marco Elvercdb9b072020-04-10 18:44:18 +0200277 for (skip = 0; skip < num_entries; ++skip) {
Marco Elverf770ed12020-04-10 18:44:17 +0200278 len = scnprintf(buf, sizeof(buf), "%ps", (void *)stack_entries[skip]);
Marco Elvercdb9b072020-04-10 18:44:18 +0200279
280 /* Never show tsan_* or {read,write}_once_size. */
281 if (strnstr(buf, "tsan_", len) ||
282 strnstr(buf, "_once_size", len))
283 continue;
284
285 cur = strnstr(buf, "kcsan_", len);
286 if (cur) {
Marco Elvera4e74fa2020-07-31 10:17:20 +0200287 cur += strlen("kcsan_");
288 if (!str_has_prefix(cur, "test"))
Marco Elvercdb9b072020-04-10 18:44:18 +0200289 continue; /* KCSAN runtime function. */
290 /* KCSAN related test. */
291 }
292
293 /*
294 * No match for runtime functions -- @skip entries to skip to
295 * get to first frame of interest.
296 */
297 break;
Marco Elverdfd402a2019-11-14 19:02:54 +0100298 }
Marco Elvercdb9b072020-04-10 18:44:18 +0200299
Marco Elverdfd402a2019-11-14 19:02:54 +0100300 return skip;
301}
302
303/* Compares symbolized strings of addr1 and addr2. */
304static int sym_strcmp(void *addr1, void *addr2)
305{
306 char buf1[64];
307 char buf2[64];
308
309 snprintf(buf1, sizeof(buf1), "%pS", addr1);
310 snprintf(buf2, sizeof(buf2), "%pS", addr2);
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100311
Marco Elverdfd402a2019-11-14 19:02:54 +0100312 return strncmp(buf1, buf2, sizeof(buf1));
313}
314
Marco Elver2402d0e2020-02-22 00:10:27 +0100315static void print_verbose_info(struct task_struct *task)
316{
317 if (!task)
318 return;
319
Marco Elver92c209a2020-07-29 13:09:16 +0200320 /* Restore IRQ state trace for printing. */
321 kcsan_restore_irqtrace(task);
322
Marco Elver2402d0e2020-02-22 00:10:27 +0100323 pr_err("\n");
324 debug_show_held_locks(task);
325 print_irqtrace_events(task);
326}
327
Mark Rutland97aa6132021-04-14 13:28:20 +0200328static void print_report(enum kcsan_value_change value_change,
Marco Elver135c0872020-03-18 18:38:44 +0100329 enum kcsan_report_type type,
330 const struct access_info *ai,
331 const struct other_info *other_info)
Marco Elverdfd402a2019-11-14 19:02:54 +0100332{
333 unsigned long stack_entries[NUM_STACK_ENTRIES] = { 0 };
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100334 int num_stack_entries = stack_trace_save(stack_entries, NUM_STACK_ENTRIES, 1);
Marco Elverdfd402a2019-11-14 19:02:54 +0100335 int skipnr = get_stack_skipnr(stack_entries, num_stack_entries);
Marco Elver05f9a402020-01-10 19:48:34 +0100336 unsigned long this_frame = stack_entries[skipnr];
337 unsigned long other_frame = 0;
338 int other_skipnr = 0; /* silence uninit warnings */
Marco Elverdfd402a2019-11-14 19:02:54 +0100339
340 /*
341 * Must check report filter rules before starting to print.
342 */
Marco Elverb738f612020-02-11 17:04:21 +0100343 if (skip_report(KCSAN_VALUE_CHANGE_TRUE, stack_entries[skipnr]))
Mark Rutland97aa6132021-04-14 13:28:20 +0200344 return;
Marco Elverdfd402a2019-11-14 19:02:54 +0100345
346 if (type == KCSAN_REPORT_RACE_SIGNAL) {
Marco Elver135c0872020-03-18 18:38:44 +0100347 other_skipnr = get_stack_skipnr(other_info->stack_entries,
348 other_info->num_stack_entries);
349 other_frame = other_info->stack_entries[other_skipnr];
Marco Elverdfd402a2019-11-14 19:02:54 +0100350
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100351 /* @value_change is only known for the other thread */
Marco Elverad4f8ee2020-01-29 16:01:02 +0100352 if (skip_report(value_change, other_frame))
Mark Rutland97aa6132021-04-14 13:28:20 +0200353 return;
Marco Elverdfd402a2019-11-14 19:02:54 +0100354 }
355
Marco Elver05f9a402020-01-10 19:48:34 +0100356 if (rate_limit_report(this_frame, other_frame))
Mark Rutland97aa6132021-04-14 13:28:20 +0200357 return;
Marco Elver05f9a402020-01-10 19:48:34 +0100358
Marco Elverdfd402a2019-11-14 19:02:54 +0100359 /* Print report header. */
360 pr_err("==================================================================\n");
361 switch (type) {
362 case KCSAN_REPORT_RACE_SIGNAL: {
Marco Elverdfd402a2019-11-14 19:02:54 +0100363 int cmp;
364
365 /*
366 * Order functions lexographically for consistent bug titles.
367 * Do not print offset of functions to keep title short.
368 */
Marco Elver05f9a402020-01-10 19:48:34 +0100369 cmp = sym_strcmp((void *)other_frame, (void *)this_frame);
Marco Elverd591ec32020-02-06 16:46:24 +0100370 pr_err("BUG: KCSAN: %s in %ps / %ps\n",
Marco Elver135c0872020-03-18 18:38:44 +0100371 get_bug_type(ai->access_type | other_info->ai.access_type),
Marco Elver05f9a402020-01-10 19:48:34 +0100372 (void *)(cmp < 0 ? other_frame : this_frame),
373 (void *)(cmp < 0 ? this_frame : other_frame));
Marco Elverdfd402a2019-11-14 19:02:54 +0100374 } break;
375
376 case KCSAN_REPORT_RACE_UNKNOWN_ORIGIN:
Marco Elver135c0872020-03-18 18:38:44 +0100377 pr_err("BUG: KCSAN: %s in %pS\n", get_bug_type(ai->access_type),
Marco Elverd591ec32020-02-06 16:46:24 +0100378 (void *)this_frame);
Marco Elverdfd402a2019-11-14 19:02:54 +0100379 break;
380
381 default:
382 BUG();
383 }
384
385 pr_err("\n");
386
387 /* Print information about the racing accesses. */
388 switch (type) {
389 case KCSAN_REPORT_RACE_SIGNAL:
390 pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n",
Marco Elver135c0872020-03-18 18:38:44 +0100391 get_access_type(other_info->ai.access_type), other_info->ai.ptr,
392 other_info->ai.size, get_thread_desc(other_info->ai.task_pid),
393 other_info->ai.cpu_id);
Marco Elverdfd402a2019-11-14 19:02:54 +0100394
395 /* Print the other thread's stack trace. */
Marco Elver135c0872020-03-18 18:38:44 +0100396 stack_trace_print(other_info->stack_entries + other_skipnr,
397 other_info->num_stack_entries - other_skipnr,
Marco Elverdfd402a2019-11-14 19:02:54 +0100398 0);
399
Marco Elver2402d0e2020-02-22 00:10:27 +0100400 if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
Marco Elver135c0872020-03-18 18:38:44 +0100401 print_verbose_info(other_info->task);
Marco Elver2402d0e2020-02-22 00:10:27 +0100402
Marco Elverdfd402a2019-11-14 19:02:54 +0100403 pr_err("\n");
404 pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n",
Marco Elver135c0872020-03-18 18:38:44 +0100405 get_access_type(ai->access_type), ai->ptr, ai->size,
406 get_thread_desc(ai->task_pid), ai->cpu_id);
Marco Elverdfd402a2019-11-14 19:02:54 +0100407 break;
408
409 case KCSAN_REPORT_RACE_UNKNOWN_ORIGIN:
410 pr_err("race at unknown origin, with %s to 0x%px of %zu bytes by %s on cpu %i:\n",
Marco Elver135c0872020-03-18 18:38:44 +0100411 get_access_type(ai->access_type), ai->ptr, ai->size,
412 get_thread_desc(ai->task_pid), ai->cpu_id);
Marco Elverdfd402a2019-11-14 19:02:54 +0100413 break;
414
415 default:
416 BUG();
417 }
418 /* Print stack trace of this thread. */
419 stack_trace_print(stack_entries + skipnr, num_stack_entries - skipnr,
420 0);
421
Marco Elver2402d0e2020-02-22 00:10:27 +0100422 if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
423 print_verbose_info(current);
424
Marco Elverdfd402a2019-11-14 19:02:54 +0100425 /* Print report footer. */
426 pr_err("\n");
427 pr_err("Reported by Kernel Concurrency Sanitizer on:\n");
428 dump_stack_print_info(KERN_DEFAULT);
429 pr_err("==================================================================\n");
430
Mark Rutland97aa6132021-04-14 13:28:20 +0200431 if (panic_on_warn)
432 panic("panic_on_warn set ...\n");
Marco Elverdfd402a2019-11-14 19:02:54 +0100433}
434
Marco Elver135c0872020-03-18 18:38:44 +0100435static void release_report(unsigned long *flags, struct other_info *other_info)
Marco Elverdfd402a2019-11-14 19:02:54 +0100436{
Marco Elver135c0872020-03-18 18:38:44 +0100437 if (other_info)
Marco Elver61194182020-03-18 18:38:45 +0100438 /*
439 * Use size to denote valid/invalid, since KCSAN entirely
440 * ignores 0-sized accesses.
441 */
442 other_info->ai.size = 0;
Marco Elverdfd402a2019-11-14 19:02:54 +0100443
Marco Elver61194182020-03-18 18:38:45 +0100444 raw_spin_unlock_irqrestore(&report_lock, *flags);
Marco Elverdfd402a2019-11-14 19:02:54 +0100445}
446
447/*
Marco Elver135c0872020-03-18 18:38:44 +0100448 * Sets @other_info->task and awaits consumption of @other_info.
Marco Elver2402d0e2020-02-22 00:10:27 +0100449 *
450 * Precondition: report_lock is held.
451 * Postcondition: report_lock is held.
452 */
Marco Elver135c0872020-03-18 18:38:44 +0100453static void set_other_info_task_blocking(unsigned long *flags,
454 const struct access_info *ai,
455 struct other_info *other_info)
Marco Elver2402d0e2020-02-22 00:10:27 +0100456{
457 /*
458 * We may be instrumenting a code-path where current->state is already
459 * something other than TASK_RUNNING.
460 */
461 const bool is_running = current->state == TASK_RUNNING;
462 /*
463 * To avoid deadlock in case we are in an interrupt here and this is a
464 * race with a task on the same CPU (KCSAN_INTERRUPT_WATCHER), provide a
465 * timeout to ensure this works in all contexts.
466 *
467 * Await approximately the worst case delay of the reporting thread (if
468 * we are not interrupted).
469 */
470 int timeout = max(kcsan_udelay_task, kcsan_udelay_interrupt);
471
Marco Elver135c0872020-03-18 18:38:44 +0100472 other_info->task = current;
Marco Elver2402d0e2020-02-22 00:10:27 +0100473 do {
474 if (is_running) {
475 /*
476 * Let lockdep know the real task is sleeping, to print
477 * the held locks (recall we turned lockdep off, so
478 * locking/unlocking @report_lock won't be recorded).
479 */
480 set_current_state(TASK_UNINTERRUPTIBLE);
481 }
Marco Elver61194182020-03-18 18:38:45 +0100482 raw_spin_unlock_irqrestore(&report_lock, *flags);
Marco Elver2402d0e2020-02-22 00:10:27 +0100483 /*
484 * We cannot call schedule() since we also cannot reliably
485 * determine if sleeping here is permitted -- see in_atomic().
486 */
487
488 udelay(1);
Marco Elver61194182020-03-18 18:38:45 +0100489 raw_spin_lock_irqsave(&report_lock, *flags);
Marco Elver2402d0e2020-02-22 00:10:27 +0100490 if (timeout-- < 0) {
491 /*
Marco Elver135c0872020-03-18 18:38:44 +0100492 * Abort. Reset @other_info->task to NULL, since it
Marco Elver2402d0e2020-02-22 00:10:27 +0100493 * appears the other thread is still going to consume
494 * it. It will result in no verbose info printed for
495 * this task.
496 */
Marco Elver135c0872020-03-18 18:38:44 +0100497 other_info->task = NULL;
Marco Elver2402d0e2020-02-22 00:10:27 +0100498 break;
499 }
500 /*
Marco Elver61194182020-03-18 18:38:45 +0100501 * If invalid, or @ptr nor @current matches, then @other_info
502 * has been consumed and we may continue. If not, retry.
Marco Elver2402d0e2020-02-22 00:10:27 +0100503 */
Marco Elver61194182020-03-18 18:38:45 +0100504 } while (other_info->ai.size && other_info->ai.ptr == ai->ptr &&
505 other_info->task == current);
Marco Elver2402d0e2020-02-22 00:10:27 +0100506 if (is_running)
507 set_current_state(TASK_RUNNING);
508}
509
Marco Elver61194182020-03-18 18:38:45 +0100510/* Populate @other_info; requires that the provided @other_info not in use. */
511static void prepare_report_producer(unsigned long *flags,
512 const struct access_info *ai,
513 struct other_info *other_info)
Marco Elverdfd402a2019-11-14 19:02:54 +0100514{
Marco Elver61194182020-03-18 18:38:45 +0100515 raw_spin_lock_irqsave(&report_lock, *flags);
516
517 /*
518 * The same @other_infos entry cannot be used concurrently, because
519 * there is a one-to-one mapping to watchpoint slots (@watchpoints in
520 * core.c), and a watchpoint is only released for reuse after reporting
521 * is done by the consumer of @other_info. Therefore, it is impossible
522 * for another concurrent prepare_report_producer() to set the same
523 * @other_info, and are guaranteed exclusivity for the @other_infos
524 * entry pointed to by @other_info.
525 *
526 * To check this property holds, size should never be non-zero here,
527 * because every consumer of struct other_info resets size to 0 in
528 * release_report().
529 */
530 WARN_ON(other_info->ai.size);
531
532 other_info->ai = *ai;
533 other_info->num_stack_entries = stack_trace_save(other_info->stack_entries, NUM_STACK_ENTRIES, 2);
534
535 if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
536 set_other_info_task_blocking(flags, ai, other_info);
537
538 raw_spin_unlock_irqrestore(&report_lock, *flags);
539}
540
541/* Awaits producer to fill @other_info and then returns. */
542static bool prepare_report_consumer(unsigned long *flags,
543 const struct access_info *ai,
544 struct other_info *other_info)
545{
546
547 raw_spin_lock_irqsave(&report_lock, *flags);
548 while (!other_info->ai.size) { /* Await valid @other_info. */
549 raw_spin_unlock_irqrestore(&report_lock, *flags);
550 cpu_relax();
551 raw_spin_lock_irqsave(&report_lock, *flags);
Marco Elverdfd402a2019-11-14 19:02:54 +0100552 }
553
Marco Elver61194182020-03-18 18:38:45 +0100554 /* Should always have a matching access based on watchpoint encoding. */
555 if (WARN_ON(!matching_access((unsigned long)other_info->ai.ptr & WATCHPOINT_ADDR_MASK, other_info->ai.size,
556 (unsigned long)ai->ptr & WATCHPOINT_ADDR_MASK, ai->size)))
557 goto discard;
Marco Elverdfd402a2019-11-14 19:02:54 +0100558
Marco Elver61194182020-03-18 18:38:45 +0100559 if (!matching_access((unsigned long)other_info->ai.ptr, other_info->ai.size,
560 (unsigned long)ai->ptr, ai->size)) {
561 /*
562 * If the actual accesses to not match, this was a false
563 * positive due to watchpoint encoding.
564 */
Marco Elver2e986b82020-08-10 10:06:25 +0200565 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ENCODING_FALSE_POSITIVES]);
Marco Elver61194182020-03-18 18:38:45 +0100566 goto discard;
567 }
568
569 return true;
570
571discard:
572 release_report(flags, other_info);
573 return false;
574}
575
576/*
577 * Depending on the report type either sets @other_info and returns false, or
578 * awaits @other_info and returns true. If @other_info is not required for the
579 * report type, simply acquires @report_lock and returns true.
580 */
581static noinline bool prepare_report(unsigned long *flags,
582 enum kcsan_report_type type,
583 const struct access_info *ai,
584 struct other_info *other_info)
585{
Marco Elverdfd402a2019-11-14 19:02:54 +0100586 switch (type) {
587 case KCSAN_REPORT_CONSUMED_WATCHPOINT:
Marco Elver61194182020-03-18 18:38:45 +0100588 prepare_report_producer(flags, ai, other_info);
Marco Elverdfd402a2019-11-14 19:02:54 +0100589 return false;
Marco Elverdfd402a2019-11-14 19:02:54 +0100590 case KCSAN_REPORT_RACE_SIGNAL:
Marco Elver61194182020-03-18 18:38:45 +0100591 return prepare_report_consumer(flags, ai, other_info);
Marco Elverdfd402a2019-11-14 19:02:54 +0100592 default:
Marco Elver61194182020-03-18 18:38:45 +0100593 /* @other_info not required; just acquire @report_lock. */
594 raw_spin_lock_irqsave(&report_lock, *flags);
595 return true;
Marco Elverdfd402a2019-11-14 19:02:54 +0100596 }
Marco Elverdfd402a2019-11-14 19:02:54 +0100597}
598
Mark Rutland793c2572021-04-14 13:28:18 +0200599static void kcsan_report(const volatile void *ptr, size_t size, int access_type,
600 enum kcsan_value_change value_change,
Mark Rutland95f75242021-04-14 13:28:19 +0200601 enum kcsan_report_type type, struct other_info *other_info)
Marco Elverdfd402a2019-11-14 19:02:54 +0100602{
603 unsigned long flags = 0;
Marco Elver135c0872020-03-18 18:38:44 +0100604 const struct access_info ai = {
605 .ptr = ptr,
606 .size = size,
607 .access_type = access_type,
608 .task_pid = in_task() ? task_pid_nr(current) : -1,
609 .cpu_id = raw_smp_processor_id()
610 };
Marco Elver61194182020-03-18 18:38:45 +0100611
612 kcsan_disable_current();
Marco Elverdfd402a2019-11-14 19:02:54 +0100613
Marco Elverf1bc9622020-01-15 17:25:12 +0100614 /*
Marco Elver248591f2020-06-24 13:32:46 +0200615 * Because we may generate reports when we're in scheduler code, the use
616 * of printk() could deadlock. Until such time that all printing code
617 * called in print_report() is scheduler-safe, accept the risk, and just
618 * get our message out. As such, also disable lockdep to hide the
619 * warning, and avoid disabling lockdep for the rest of the kernel.
Marco Elverf1bc9622020-01-15 17:25:12 +0100620 */
621 lockdep_off();
622
Marco Elver135c0872020-03-18 18:38:44 +0100623 if (prepare_report(&flags, type, &ai, other_info)) {
Marco Elver81af89e2020-02-11 17:04:22 +0100624 /*
625 * Never report if value_change is FALSE, only if we it is
626 * either TRUE or MAYBE. In case of MAYBE, further filtering may
627 * be done once we know the full stack trace in print_report().
628 */
Mark Rutland97aa6132021-04-14 13:28:20 +0200629 if (value_change != KCSAN_VALUE_CHANGE_FALSE)
630 print_report(value_change, type, &ai, other_info);
Marco Elverdfd402a2019-11-14 19:02:54 +0100631
Marco Elver135c0872020-03-18 18:38:44 +0100632 release_report(&flags, other_info);
Marco Elverdfd402a2019-11-14 19:02:54 +0100633 }
Marco Elverf1bc9622020-01-15 17:25:12 +0100634
635 lockdep_on();
Marco Elver61194182020-03-18 18:38:45 +0100636 kcsan_enable_current();
Marco Elverdfd402a2019-11-14 19:02:54 +0100637}
Mark Rutland793c2572021-04-14 13:28:18 +0200638
639void kcsan_report_set_info(const volatile void *ptr, size_t size, int access_type,
640 int watchpoint_idx)
641{
642 kcsan_report(ptr, size, access_type, KCSAN_VALUE_CHANGE_MAYBE,
Mark Rutland95f75242021-04-14 13:28:19 +0200643 KCSAN_REPORT_CONSUMED_WATCHPOINT, &other_infos[watchpoint_idx]);
Mark Rutland793c2572021-04-14 13:28:18 +0200644}
645
646void kcsan_report_known_origin(const volatile void *ptr, size_t size, int access_type,
647 enum kcsan_value_change value_change, int watchpoint_idx)
648{
649 kcsan_report(ptr, size, access_type, value_change,
Mark Rutland95f75242021-04-14 13:28:19 +0200650 KCSAN_REPORT_RACE_SIGNAL, &other_infos[watchpoint_idx]);
Mark Rutland793c2572021-04-14 13:28:18 +0200651}
652
653void kcsan_report_unknown_origin(const volatile void *ptr, size_t size, int access_type)
654{
655 kcsan_report(ptr, size, access_type, KCSAN_VALUE_CHANGE_TRUE,
Mark Rutland95f75242021-04-14 13:28:19 +0200656 KCSAN_REPORT_RACE_UNKNOWN_ORIGIN, NULL);
Mark Rutland793c2572021-04-14 13:28:18 +0200657}