blob: d3bf87e6007ca7d555794606b6d56e90dbdaf363 [file] [log] [blame]
Marco Elverdfd402a2019-11-14 19:02:54 +01001// SPDX-License-Identifier: GPL-2.0
2
Marco Elver2402d0e2020-02-22 00:10:27 +01003#include <linux/debug_locks.h>
4#include <linux/delay.h>
Marco Elver05f9a402020-01-10 19:48:34 +01005#include <linux/jiffies.h>
Marco Elverdfd402a2019-11-14 19:02:54 +01006#include <linux/kernel.h>
Marco Elverf1bc9622020-01-15 17:25:12 +01007#include <linux/lockdep.h>
Marco Elverdfd402a2019-11-14 19:02:54 +01008#include <linux/preempt.h>
9#include <linux/printk.h>
10#include <linux/sched.h>
11#include <linux/spinlock.h>
12#include <linux/stacktrace.h>
13
14#include "kcsan.h"
15#include "encoding.h"
16
17/*
18 * Max. number of stack entries to show in the report.
19 */
20#define NUM_STACK_ENTRIES 64
21
Marco Elver135c0872020-03-18 18:38:44 +010022/* Common access info. */
23struct access_info {
24 const volatile void *ptr;
25 size_t size;
26 int access_type;
27 int task_pid;
28 int cpu_id;
29};
30
Marco Elverdfd402a2019-11-14 19:02:54 +010031/*
32 * Other thread info: communicated from other racing thread to thread that set
Marco Elver61194182020-03-18 18:38:45 +010033 * up the watchpoint, which then prints the complete report atomically.
Marco Elverdfd402a2019-11-14 19:02:54 +010034 */
Marco Elver135c0872020-03-18 18:38:44 +010035struct other_info {
36 struct access_info ai;
Ingo Molnar5cbaefe2019-11-20 10:41:43 +010037 unsigned long stack_entries[NUM_STACK_ENTRIES];
38 int num_stack_entries;
Marco Elver2402d0e2020-02-22 00:10:27 +010039
40 /*
41 * Optionally pass @current. Typically we do not need to pass @current
42 * via @other_info since just @task_pid is sufficient. Passing @current
43 * has additional overhead.
44 *
45 * To safely pass @current, we must either use get_task_struct/
46 * put_task_struct, or stall the thread that populated @other_info.
47 *
48 * We cannot rely on get_task_struct/put_task_struct in case
49 * release_report() races with a task being released, and would have to
50 * free it in release_report(). This may result in deadlock if we want
51 * to use KCSAN on the allocators.
52 *
53 * Since we also want to reliably print held locks for
54 * CONFIG_KCSAN_VERBOSE, the current implementation stalls the thread
55 * that populated @other_info until it has been consumed.
56 */
57 struct task_struct *task;
Marco Elver135c0872020-03-18 18:38:44 +010058};
59
Marco Elver61194182020-03-18 18:38:45 +010060/*
61 * To never block any producers of struct other_info, we need as many elements
62 * as we have watchpoints (upper bound on concurrent races to report).
63 */
64static struct other_info other_infos[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1];
Marco Elverdfd402a2019-11-14 19:02:54 +010065
66/*
Marco Elverd591ec32020-02-06 16:46:24 +010067 * Information about reported races; used to rate limit reporting.
Marco Elver05f9a402020-01-10 19:48:34 +010068 */
69struct report_time {
70 /*
Marco Elverd591ec32020-02-06 16:46:24 +010071 * The last time the race was reported.
Marco Elver05f9a402020-01-10 19:48:34 +010072 */
73 unsigned long time;
74
75 /*
76 * The frames of the 2 threads; if only 1 thread is known, one frame
77 * will be 0.
78 */
79 unsigned long frame1;
80 unsigned long frame2;
81};
82
83/*
84 * Since we also want to be able to debug allocators with KCSAN, to avoid
85 * deadlock, report_times cannot be dynamically resized with krealloc in
86 * rate_limit_report.
87 *
88 * Therefore, we use a fixed-size array, which at most will occupy a page. This
89 * still adequately rate limits reports, assuming that a) number of unique data
Marco Elverd591ec32020-02-06 16:46:24 +010090 * races is not excessive, and b) occurrence of unique races within the
Marco Elver05f9a402020-01-10 19:48:34 +010091 * same time window is limited.
92 */
93#define REPORT_TIMES_MAX (PAGE_SIZE / sizeof(struct report_time))
94#define REPORT_TIMES_SIZE \
95 (CONFIG_KCSAN_REPORT_ONCE_IN_MS > REPORT_TIMES_MAX ? \
96 REPORT_TIMES_MAX : \
97 CONFIG_KCSAN_REPORT_ONCE_IN_MS)
98static struct report_time report_times[REPORT_TIMES_SIZE];
99
100/*
Marco Elver61194182020-03-18 18:38:45 +0100101 * Spinlock serializing report generation, and access to @other_infos. Although
102 * it could make sense to have a finer-grained locking story for @other_infos,
103 * report generation needs to be serialized either way, so not much is gained.
Marco Elverdfd402a2019-11-14 19:02:54 +0100104 */
Marco Elver61194182020-03-18 18:38:45 +0100105static DEFINE_RAW_SPINLOCK(report_lock);
Marco Elverdfd402a2019-11-14 19:02:54 +0100106
107/*
Marco Elverd591ec32020-02-06 16:46:24 +0100108 * Checks if the race identified by thread frames frame1 and frame2 has
Marco Elver05f9a402020-01-10 19:48:34 +0100109 * been reported since (now - KCSAN_REPORT_ONCE_IN_MS).
110 */
111static bool rate_limit_report(unsigned long frame1, unsigned long frame2)
112{
113 struct report_time *use_entry = &report_times[0];
114 unsigned long invalid_before;
115 int i;
116
117 BUILD_BUG_ON(CONFIG_KCSAN_REPORT_ONCE_IN_MS != 0 && REPORT_TIMES_SIZE == 0);
118
119 if (CONFIG_KCSAN_REPORT_ONCE_IN_MS == 0)
120 return false;
121
122 invalid_before = jiffies - msecs_to_jiffies(CONFIG_KCSAN_REPORT_ONCE_IN_MS);
123
Marco Elverd591ec32020-02-06 16:46:24 +0100124 /* Check if a matching race report exists. */
Marco Elver05f9a402020-01-10 19:48:34 +0100125 for (i = 0; i < REPORT_TIMES_SIZE; ++i) {
126 struct report_time *rt = &report_times[i];
127
128 /*
129 * Must always select an entry for use to store info as we
130 * cannot resize report_times; at the end of the scan, use_entry
131 * will be the oldest entry, which ideally also happened before
132 * KCSAN_REPORT_ONCE_IN_MS ago.
133 */
134 if (time_before(rt->time, use_entry->time))
135 use_entry = rt;
136
137 /*
138 * Initially, no need to check any further as this entry as well
139 * as following entries have never been used.
140 */
141 if (rt->time == 0)
142 break;
143
144 /* Check if entry expired. */
145 if (time_before(rt->time, invalid_before))
146 continue; /* before KCSAN_REPORT_ONCE_IN_MS ago */
147
Marco Elverd591ec32020-02-06 16:46:24 +0100148 /* Reported recently, check if race matches. */
Marco Elver05f9a402020-01-10 19:48:34 +0100149 if ((rt->frame1 == frame1 && rt->frame2 == frame2) ||
150 (rt->frame1 == frame2 && rt->frame2 == frame1))
151 return true;
152 }
153
154 use_entry->time = jiffies;
155 use_entry->frame1 = frame1;
156 use_entry->frame2 = frame2;
157 return false;
158}
159
160/*
Marco Elverdfd402a2019-11-14 19:02:54 +0100161 * Special rules to skip reporting.
162 */
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100163static bool
Marco Elverb738f612020-02-11 17:04:21 +0100164skip_report(enum kcsan_value_change value_change, unsigned long top_frame)
Marco Elverdfd402a2019-11-14 19:02:54 +0100165{
Marco Elver81af89e2020-02-11 17:04:22 +0100166 /* Should never get here if value_change==FALSE. */
167 WARN_ON_ONCE(value_change == KCSAN_VALUE_CHANGE_FALSE);
168
Marco Elverad4f8ee2020-01-29 16:01:02 +0100169 /*
Marco Elverb738f612020-02-11 17:04:21 +0100170 * The first call to skip_report always has value_change==TRUE, since we
Marco Elverad4f8ee2020-01-29 16:01:02 +0100171 * cannot know the value written of an instrumented access. For the 2nd
172 * call there are 6 cases with CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY:
173 *
Marco Elverb738f612020-02-11 17:04:21 +0100174 * 1. read watchpoint, conflicting write (value_change==TRUE): report;
175 * 2. read watchpoint, conflicting write (value_change==MAYBE): skip;
176 * 3. write watchpoint, conflicting write (value_change==TRUE): report;
177 * 4. write watchpoint, conflicting write (value_change==MAYBE): skip;
178 * 5. write watchpoint, conflicting read (value_change==MAYBE): skip;
179 * 6. write watchpoint, conflicting read (value_change==TRUE): report;
Marco Elverad4f8ee2020-01-29 16:01:02 +0100180 *
181 * Cases 1-4 are intuitive and expected; case 5 ensures we do not report
Marco Elverd591ec32020-02-06 16:46:24 +0100182 * data races where the write may have rewritten the same value; case 6
183 * is possible either if the size is larger than what we check value
184 * changes for or the access type is KCSAN_ACCESS_ASSERT.
Marco Elverad4f8ee2020-01-29 16:01:02 +0100185 */
Marco Elverb738f612020-02-11 17:04:21 +0100186 if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) &&
187 value_change == KCSAN_VALUE_CHANGE_MAYBE) {
Marco Elverdfd402a2019-11-14 19:02:54 +0100188 /*
189 * The access is a write, but the data value did not change.
190 *
191 * We opt-out of this filter for certain functions at request of
192 * maintainers.
193 */
194 char buf[64];
Marco Elverf770ed12020-04-10 18:44:17 +0200195 int len = scnprintf(buf, sizeof(buf), "%ps", (void *)top_frame);
Marco Elverdfd402a2019-11-14 19:02:54 +0100196
Marco Elverf770ed12020-04-10 18:44:17 +0200197 if (!strnstr(buf, "rcu_", len) &&
198 !strnstr(buf, "_rcu", len) &&
199 !strnstr(buf, "_srcu", len))
Marco Elverdfd402a2019-11-14 19:02:54 +0100200 return true;
201 }
202
203 return kcsan_skip_report_debugfs(top_frame);
204}
205
Marco Elver47144ec2020-01-10 19:48:33 +0100206static const char *get_access_type(int type)
Marco Elverdfd402a2019-11-14 19:02:54 +0100207{
Marco Elver757a4ce2020-03-25 17:41:56 +0100208 if (type & KCSAN_ACCESS_ASSERT) {
209 if (type & KCSAN_ACCESS_SCOPED) {
210 if (type & KCSAN_ACCESS_WRITE)
211 return "assert no accesses (scoped)";
212 else
213 return "assert no writes (scoped)";
214 } else {
215 if (type & KCSAN_ACCESS_WRITE)
216 return "assert no accesses";
217 else
218 return "assert no writes";
219 }
220 }
221
Marco Elver47144ec2020-01-10 19:48:33 +0100222 switch (type) {
223 case 0:
224 return "read";
225 case KCSAN_ACCESS_ATOMIC:
226 return "read (marked)";
227 case KCSAN_ACCESS_WRITE:
228 return "write";
229 case KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
230 return "write (marked)";
Marco Elver14e2ac82020-07-24 09:00:01 +0200231 case KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE:
232 return "read-write";
233 case KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
234 return "read-write (marked)";
Marco Elver757a4ce2020-03-25 17:41:56 +0100235 case KCSAN_ACCESS_SCOPED:
236 return "read (scoped)";
237 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_ATOMIC:
238 return "read (marked, scoped)";
239 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE:
240 return "write (scoped)";
241 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
242 return "write (marked, scoped)";
Marco Elver47144ec2020-01-10 19:48:33 +0100243 default:
244 BUG();
245 }
Marco Elverdfd402a2019-11-14 19:02:54 +0100246}
247
Marco Elverd591ec32020-02-06 16:46:24 +0100248static const char *get_bug_type(int type)
249{
250 return (type & KCSAN_ACCESS_ASSERT) != 0 ? "assert: race" : "data-race";
251}
252
Marco Elverdfd402a2019-11-14 19:02:54 +0100253/* Return thread description: in task or interrupt. */
254static const char *get_thread_desc(int task_id)
255{
256 if (task_id != -1) {
257 static char buf[32]; /* safe: protected by report_lock */
258
259 snprintf(buf, sizeof(buf), "task %i", task_id);
260 return buf;
261 }
262 return "interrupt";
263}
264
265/* Helper to skip KCSAN-related functions in stack-trace. */
Marco Elver135c0872020-03-18 18:38:44 +0100266static int get_stack_skipnr(const unsigned long stack_entries[], int num_entries)
Marco Elverdfd402a2019-11-14 19:02:54 +0100267{
268 char buf[64];
Marco Elvercdb9b072020-04-10 18:44:18 +0200269 char *cur;
270 int len, skip;
Marco Elverdfd402a2019-11-14 19:02:54 +0100271
Marco Elvercdb9b072020-04-10 18:44:18 +0200272 for (skip = 0; skip < num_entries; ++skip) {
Marco Elverf770ed12020-04-10 18:44:17 +0200273 len = scnprintf(buf, sizeof(buf), "%ps", (void *)stack_entries[skip]);
Marco Elvercdb9b072020-04-10 18:44:18 +0200274
275 /* Never show tsan_* or {read,write}_once_size. */
276 if (strnstr(buf, "tsan_", len) ||
277 strnstr(buf, "_once_size", len))
278 continue;
279
280 cur = strnstr(buf, "kcsan_", len);
281 if (cur) {
Marco Elvera4e74fa2020-07-31 10:17:20 +0200282 cur += strlen("kcsan_");
283 if (!str_has_prefix(cur, "test"))
Marco Elvercdb9b072020-04-10 18:44:18 +0200284 continue; /* KCSAN runtime function. */
285 /* KCSAN related test. */
286 }
287
288 /*
289 * No match for runtime functions -- @skip entries to skip to
290 * get to first frame of interest.
291 */
292 break;
Marco Elverdfd402a2019-11-14 19:02:54 +0100293 }
Marco Elvercdb9b072020-04-10 18:44:18 +0200294
Marco Elverdfd402a2019-11-14 19:02:54 +0100295 return skip;
296}
297
298/* Compares symbolized strings of addr1 and addr2. */
299static int sym_strcmp(void *addr1, void *addr2)
300{
301 char buf1[64];
302 char buf2[64];
303
304 snprintf(buf1, sizeof(buf1), "%pS", addr1);
305 snprintf(buf2, sizeof(buf2), "%pS", addr2);
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100306
Marco Elverdfd402a2019-11-14 19:02:54 +0100307 return strncmp(buf1, buf2, sizeof(buf1));
308}
309
Marco Elver2402d0e2020-02-22 00:10:27 +0100310static void print_verbose_info(struct task_struct *task)
311{
312 if (!task)
313 return;
314
Marco Elver92c209a2020-07-29 13:09:16 +0200315 /* Restore IRQ state trace for printing. */
316 kcsan_restore_irqtrace(task);
317
Marco Elver2402d0e2020-02-22 00:10:27 +0100318 pr_err("\n");
319 debug_show_held_locks(task);
320 print_irqtrace_events(task);
321}
322
Marco Elverdfd402a2019-11-14 19:02:54 +0100323/*
324 * Returns true if a report was generated, false otherwise.
325 */
Marco Elver135c0872020-03-18 18:38:44 +0100326static bool print_report(enum kcsan_value_change value_change,
327 enum kcsan_report_type type,
328 const struct access_info *ai,
329 const struct other_info *other_info)
Marco Elverdfd402a2019-11-14 19:02:54 +0100330{
331 unsigned long stack_entries[NUM_STACK_ENTRIES] = { 0 };
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100332 int num_stack_entries = stack_trace_save(stack_entries, NUM_STACK_ENTRIES, 1);
Marco Elverdfd402a2019-11-14 19:02:54 +0100333 int skipnr = get_stack_skipnr(stack_entries, num_stack_entries);
Marco Elver05f9a402020-01-10 19:48:34 +0100334 unsigned long this_frame = stack_entries[skipnr];
335 unsigned long other_frame = 0;
336 int other_skipnr = 0; /* silence uninit warnings */
Marco Elverdfd402a2019-11-14 19:02:54 +0100337
338 /*
339 * Must check report filter rules before starting to print.
340 */
Marco Elverb738f612020-02-11 17:04:21 +0100341 if (skip_report(KCSAN_VALUE_CHANGE_TRUE, stack_entries[skipnr]))
Marco Elverdfd402a2019-11-14 19:02:54 +0100342 return false;
343
344 if (type == KCSAN_REPORT_RACE_SIGNAL) {
Marco Elver135c0872020-03-18 18:38:44 +0100345 other_skipnr = get_stack_skipnr(other_info->stack_entries,
346 other_info->num_stack_entries);
347 other_frame = other_info->stack_entries[other_skipnr];
Marco Elverdfd402a2019-11-14 19:02:54 +0100348
Ingo Molnar5cbaefe2019-11-20 10:41:43 +0100349 /* @value_change is only known for the other thread */
Marco Elverad4f8ee2020-01-29 16:01:02 +0100350 if (skip_report(value_change, other_frame))
Marco Elverdfd402a2019-11-14 19:02:54 +0100351 return false;
352 }
353
Marco Elver05f9a402020-01-10 19:48:34 +0100354 if (rate_limit_report(this_frame, other_frame))
355 return false;
356
Marco Elverdfd402a2019-11-14 19:02:54 +0100357 /* Print report header. */
358 pr_err("==================================================================\n");
359 switch (type) {
360 case KCSAN_REPORT_RACE_SIGNAL: {
Marco Elverdfd402a2019-11-14 19:02:54 +0100361 int cmp;
362
363 /*
364 * Order functions lexographically for consistent bug titles.
365 * Do not print offset of functions to keep title short.
366 */
Marco Elver05f9a402020-01-10 19:48:34 +0100367 cmp = sym_strcmp((void *)other_frame, (void *)this_frame);
Marco Elverd591ec32020-02-06 16:46:24 +0100368 pr_err("BUG: KCSAN: %s in %ps / %ps\n",
Marco Elver135c0872020-03-18 18:38:44 +0100369 get_bug_type(ai->access_type | other_info->ai.access_type),
Marco Elver05f9a402020-01-10 19:48:34 +0100370 (void *)(cmp < 0 ? other_frame : this_frame),
371 (void *)(cmp < 0 ? this_frame : other_frame));
Marco Elverdfd402a2019-11-14 19:02:54 +0100372 } break;
373
374 case KCSAN_REPORT_RACE_UNKNOWN_ORIGIN:
Marco Elver135c0872020-03-18 18:38:44 +0100375 pr_err("BUG: KCSAN: %s in %pS\n", get_bug_type(ai->access_type),
Marco Elverd591ec32020-02-06 16:46:24 +0100376 (void *)this_frame);
Marco Elverdfd402a2019-11-14 19:02:54 +0100377 break;
378
379 default:
380 BUG();
381 }
382
383 pr_err("\n");
384
385 /* Print information about the racing accesses. */
386 switch (type) {
387 case KCSAN_REPORT_RACE_SIGNAL:
388 pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n",
Marco Elver135c0872020-03-18 18:38:44 +0100389 get_access_type(other_info->ai.access_type), other_info->ai.ptr,
390 other_info->ai.size, get_thread_desc(other_info->ai.task_pid),
391 other_info->ai.cpu_id);
Marco Elverdfd402a2019-11-14 19:02:54 +0100392
393 /* Print the other thread's stack trace. */
Marco Elver135c0872020-03-18 18:38:44 +0100394 stack_trace_print(other_info->stack_entries + other_skipnr,
395 other_info->num_stack_entries - other_skipnr,
Marco Elverdfd402a2019-11-14 19:02:54 +0100396 0);
397
Marco Elver2402d0e2020-02-22 00:10:27 +0100398 if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
Marco Elver135c0872020-03-18 18:38:44 +0100399 print_verbose_info(other_info->task);
Marco Elver2402d0e2020-02-22 00:10:27 +0100400
Marco Elverdfd402a2019-11-14 19:02:54 +0100401 pr_err("\n");
402 pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n",
Marco Elver135c0872020-03-18 18:38:44 +0100403 get_access_type(ai->access_type), ai->ptr, ai->size,
404 get_thread_desc(ai->task_pid), ai->cpu_id);
Marco Elverdfd402a2019-11-14 19:02:54 +0100405 break;
406
407 case KCSAN_REPORT_RACE_UNKNOWN_ORIGIN:
408 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 +0100409 get_access_type(ai->access_type), ai->ptr, ai->size,
410 get_thread_desc(ai->task_pid), ai->cpu_id);
Marco Elverdfd402a2019-11-14 19:02:54 +0100411 break;
412
413 default:
414 BUG();
415 }
416 /* Print stack trace of this thread. */
417 stack_trace_print(stack_entries + skipnr, num_stack_entries - skipnr,
418 0);
419
Marco Elver2402d0e2020-02-22 00:10:27 +0100420 if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
421 print_verbose_info(current);
422
Marco Elverdfd402a2019-11-14 19:02:54 +0100423 /* Print report footer. */
424 pr_err("\n");
425 pr_err("Reported by Kernel Concurrency Sanitizer on:\n");
426 dump_stack_print_info(KERN_DEFAULT);
427 pr_err("==================================================================\n");
428
429 return true;
430}
431
Marco Elver135c0872020-03-18 18:38:44 +0100432static void release_report(unsigned long *flags, struct other_info *other_info)
Marco Elverdfd402a2019-11-14 19:02:54 +0100433{
Marco Elver135c0872020-03-18 18:38:44 +0100434 if (other_info)
Marco Elver61194182020-03-18 18:38:45 +0100435 /*
436 * Use size to denote valid/invalid, since KCSAN entirely
437 * ignores 0-sized accesses.
438 */
439 other_info->ai.size = 0;
Marco Elverdfd402a2019-11-14 19:02:54 +0100440
Marco Elver61194182020-03-18 18:38:45 +0100441 raw_spin_unlock_irqrestore(&report_lock, *flags);
Marco Elverdfd402a2019-11-14 19:02:54 +0100442}
443
444/*
Marco Elver135c0872020-03-18 18:38:44 +0100445 * Sets @other_info->task and awaits consumption of @other_info.
Marco Elver2402d0e2020-02-22 00:10:27 +0100446 *
447 * Precondition: report_lock is held.
448 * Postcondition: report_lock is held.
449 */
Marco Elver135c0872020-03-18 18:38:44 +0100450static void set_other_info_task_blocking(unsigned long *flags,
451 const struct access_info *ai,
452 struct other_info *other_info)
Marco Elver2402d0e2020-02-22 00:10:27 +0100453{
454 /*
455 * We may be instrumenting a code-path where current->state is already
456 * something other than TASK_RUNNING.
457 */
458 const bool is_running = current->state == TASK_RUNNING;
459 /*
460 * To avoid deadlock in case we are in an interrupt here and this is a
461 * race with a task on the same CPU (KCSAN_INTERRUPT_WATCHER), provide a
462 * timeout to ensure this works in all contexts.
463 *
464 * Await approximately the worst case delay of the reporting thread (if
465 * we are not interrupted).
466 */
467 int timeout = max(kcsan_udelay_task, kcsan_udelay_interrupt);
468
Marco Elver135c0872020-03-18 18:38:44 +0100469 other_info->task = current;
Marco Elver2402d0e2020-02-22 00:10:27 +0100470 do {
471 if (is_running) {
472 /*
473 * Let lockdep know the real task is sleeping, to print
474 * the held locks (recall we turned lockdep off, so
475 * locking/unlocking @report_lock won't be recorded).
476 */
477 set_current_state(TASK_UNINTERRUPTIBLE);
478 }
Marco Elver61194182020-03-18 18:38:45 +0100479 raw_spin_unlock_irqrestore(&report_lock, *flags);
Marco Elver2402d0e2020-02-22 00:10:27 +0100480 /*
481 * We cannot call schedule() since we also cannot reliably
482 * determine if sleeping here is permitted -- see in_atomic().
483 */
484
485 udelay(1);
Marco Elver61194182020-03-18 18:38:45 +0100486 raw_spin_lock_irqsave(&report_lock, *flags);
Marco Elver2402d0e2020-02-22 00:10:27 +0100487 if (timeout-- < 0) {
488 /*
Marco Elver135c0872020-03-18 18:38:44 +0100489 * Abort. Reset @other_info->task to NULL, since it
Marco Elver2402d0e2020-02-22 00:10:27 +0100490 * appears the other thread is still going to consume
491 * it. It will result in no verbose info printed for
492 * this task.
493 */
Marco Elver135c0872020-03-18 18:38:44 +0100494 other_info->task = NULL;
Marco Elver2402d0e2020-02-22 00:10:27 +0100495 break;
496 }
497 /*
Marco Elver61194182020-03-18 18:38:45 +0100498 * If invalid, or @ptr nor @current matches, then @other_info
499 * has been consumed and we may continue. If not, retry.
Marco Elver2402d0e2020-02-22 00:10:27 +0100500 */
Marco Elver61194182020-03-18 18:38:45 +0100501 } while (other_info->ai.size && other_info->ai.ptr == ai->ptr &&
502 other_info->task == current);
Marco Elver2402d0e2020-02-22 00:10:27 +0100503 if (is_running)
504 set_current_state(TASK_RUNNING);
505}
506
Marco Elver61194182020-03-18 18:38:45 +0100507/* Populate @other_info; requires that the provided @other_info not in use. */
508static void prepare_report_producer(unsigned long *flags,
509 const struct access_info *ai,
510 struct other_info *other_info)
Marco Elverdfd402a2019-11-14 19:02:54 +0100511{
Marco Elver61194182020-03-18 18:38:45 +0100512 raw_spin_lock_irqsave(&report_lock, *flags);
513
514 /*
515 * The same @other_infos entry cannot be used concurrently, because
516 * there is a one-to-one mapping to watchpoint slots (@watchpoints in
517 * core.c), and a watchpoint is only released for reuse after reporting
518 * is done by the consumer of @other_info. Therefore, it is impossible
519 * for another concurrent prepare_report_producer() to set the same
520 * @other_info, and are guaranteed exclusivity for the @other_infos
521 * entry pointed to by @other_info.
522 *
523 * To check this property holds, size should never be non-zero here,
524 * because every consumer of struct other_info resets size to 0 in
525 * release_report().
526 */
527 WARN_ON(other_info->ai.size);
528
529 other_info->ai = *ai;
530 other_info->num_stack_entries = stack_trace_save(other_info->stack_entries, NUM_STACK_ENTRIES, 2);
531
532 if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
533 set_other_info_task_blocking(flags, ai, other_info);
534
535 raw_spin_unlock_irqrestore(&report_lock, *flags);
536}
537
538/* Awaits producer to fill @other_info and then returns. */
539static bool prepare_report_consumer(unsigned long *flags,
540 const struct access_info *ai,
541 struct other_info *other_info)
542{
543
544 raw_spin_lock_irqsave(&report_lock, *flags);
545 while (!other_info->ai.size) { /* Await valid @other_info. */
546 raw_spin_unlock_irqrestore(&report_lock, *flags);
547 cpu_relax();
548 raw_spin_lock_irqsave(&report_lock, *flags);
Marco Elverdfd402a2019-11-14 19:02:54 +0100549 }
550
Marco Elver61194182020-03-18 18:38:45 +0100551 /* Should always have a matching access based on watchpoint encoding. */
552 if (WARN_ON(!matching_access((unsigned long)other_info->ai.ptr & WATCHPOINT_ADDR_MASK, other_info->ai.size,
553 (unsigned long)ai->ptr & WATCHPOINT_ADDR_MASK, ai->size)))
554 goto discard;
Marco Elverdfd402a2019-11-14 19:02:54 +0100555
Marco Elver61194182020-03-18 18:38:45 +0100556 if (!matching_access((unsigned long)other_info->ai.ptr, other_info->ai.size,
557 (unsigned long)ai->ptr, ai->size)) {
558 /*
559 * If the actual accesses to not match, this was a false
560 * positive due to watchpoint encoding.
561 */
Marco Elver2e986b82020-08-10 10:06:25 +0200562 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ENCODING_FALSE_POSITIVES]);
Marco Elver61194182020-03-18 18:38:45 +0100563 goto discard;
564 }
565
566 return true;
567
568discard:
569 release_report(flags, other_info);
570 return false;
571}
572
573/*
574 * Depending on the report type either sets @other_info and returns false, or
575 * awaits @other_info and returns true. If @other_info is not required for the
576 * report type, simply acquires @report_lock and returns true.
577 */
578static noinline bool prepare_report(unsigned long *flags,
579 enum kcsan_report_type type,
580 const struct access_info *ai,
581 struct other_info *other_info)
582{
Marco Elverdfd402a2019-11-14 19:02:54 +0100583 switch (type) {
584 case KCSAN_REPORT_CONSUMED_WATCHPOINT:
Marco Elver61194182020-03-18 18:38:45 +0100585 prepare_report_producer(flags, ai, other_info);
Marco Elverdfd402a2019-11-14 19:02:54 +0100586 return false;
Marco Elverdfd402a2019-11-14 19:02:54 +0100587 case KCSAN_REPORT_RACE_SIGNAL:
Marco Elver61194182020-03-18 18:38:45 +0100588 return prepare_report_consumer(flags, ai, other_info);
Marco Elverdfd402a2019-11-14 19:02:54 +0100589 default:
Marco Elver61194182020-03-18 18:38:45 +0100590 /* @other_info not required; just acquire @report_lock. */
591 raw_spin_lock_irqsave(&report_lock, *flags);
592 return true;
Marco Elverdfd402a2019-11-14 19:02:54 +0100593 }
Marco Elverdfd402a2019-11-14 19:02:54 +0100594}
595
Marco Elver47144ec2020-01-10 19:48:33 +0100596void kcsan_report(const volatile void *ptr, size_t size, int access_type,
Marco Elver135c0872020-03-18 18:38:44 +0100597 enum kcsan_value_change value_change,
Marco Elver61194182020-03-18 18:38:45 +0100598 enum kcsan_report_type type, int watchpoint_idx)
Marco Elverdfd402a2019-11-14 19:02:54 +0100599{
600 unsigned long flags = 0;
Marco Elver135c0872020-03-18 18:38:44 +0100601 const struct access_info ai = {
602 .ptr = ptr,
603 .size = size,
604 .access_type = access_type,
605 .task_pid = in_task() ? task_pid_nr(current) : -1,
606 .cpu_id = raw_smp_processor_id()
607 };
608 struct other_info *other_info = type == KCSAN_REPORT_RACE_UNKNOWN_ORIGIN
Marco Elver61194182020-03-18 18:38:45 +0100609 ? NULL : &other_infos[watchpoint_idx];
610
611 kcsan_disable_current();
612 if (WARN_ON(watchpoint_idx < 0 || watchpoint_idx >= ARRAY_SIZE(other_infos)))
613 goto out;
Marco Elverdfd402a2019-11-14 19:02:54 +0100614
Marco Elverf1bc9622020-01-15 17:25:12 +0100615 /*
Marco Elver248591f2020-06-24 13:32:46 +0200616 * Because we may generate reports when we're in scheduler code, the use
617 * of printk() could deadlock. Until such time that all printing code
618 * called in print_report() is scheduler-safe, accept the risk, and just
619 * get our message out. As such, also disable lockdep to hide the
620 * warning, and avoid disabling lockdep for the rest of the kernel.
Marco Elverf1bc9622020-01-15 17:25:12 +0100621 */
622 lockdep_off();
623
Marco Elver135c0872020-03-18 18:38:44 +0100624 if (prepare_report(&flags, type, &ai, other_info)) {
Marco Elver81af89e2020-02-11 17:04:22 +0100625 /*
626 * Never report if value_change is FALSE, only if we it is
627 * either TRUE or MAYBE. In case of MAYBE, further filtering may
628 * be done once we know the full stack trace in print_report().
629 */
630 bool reported = value_change != KCSAN_VALUE_CHANGE_FALSE &&
Marco Elver135c0872020-03-18 18:38:44 +0100631 print_report(value_change, type, &ai, other_info);
Marco Elver81af89e2020-02-11 17:04:22 +0100632
633 if (reported && panic_on_warn)
Marco Elverdfd402a2019-11-14 19:02:54 +0100634 panic("panic_on_warn set ...\n");
635
Marco Elver135c0872020-03-18 18:38:44 +0100636 release_report(&flags, other_info);
Marco Elverdfd402a2019-11-14 19:02:54 +0100637 }
Marco Elverf1bc9622020-01-15 17:25:12 +0100638
639 lockdep_on();
Marco Elver61194182020-03-18 18:38:45 +0100640out:
641 kcsan_enable_current();
Marco Elverdfd402a2019-11-14 19:02:54 +0100642}