1
2
3
4
5
6
7
8#include <linux/debug_locks.h>
9#include <linux/delay.h>
10#include <linux/jiffies.h>
11#include <linux/kallsyms.h>
12#include <linux/kernel.h>
13#include <linux/lockdep.h>
14#include <linux/preempt.h>
15#include <linux/printk.h>
16#include <linux/sched.h>
17#include <linux/spinlock.h>
18#include <linux/stacktrace.h>
19
20#include "kcsan.h"
21#include "encoding.h"
22
23
24
25
26#define NUM_STACK_ENTRIES 64
27
28
29struct access_info {
30 const volatile void *ptr;
31 size_t size;
32 int access_type;
33 int task_pid;
34 int cpu_id;
35 unsigned long ip;
36};
37
38
39
40
41
42struct other_info {
43 struct access_info ai;
44 unsigned long stack_entries[NUM_STACK_ENTRIES];
45 int num_stack_entries;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 struct task_struct *task;
65};
66
67
68
69
70
71static struct other_info other_infos[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1];
72
73
74
75
76struct report_time {
77
78
79
80 unsigned long time;
81
82
83
84
85
86 unsigned long frame1;
87 unsigned long frame2;
88};
89
90
91
92
93
94
95
96
97
98
99
100#define REPORT_TIMES_MAX (PAGE_SIZE / sizeof(struct report_time))
101#define REPORT_TIMES_SIZE \
102 (CONFIG_KCSAN_REPORT_ONCE_IN_MS > REPORT_TIMES_MAX ? \
103 REPORT_TIMES_MAX : \
104 CONFIG_KCSAN_REPORT_ONCE_IN_MS)
105static struct report_time report_times[REPORT_TIMES_SIZE];
106
107
108
109
110
111
112static DEFINE_RAW_SPINLOCK(report_lock);
113
114
115
116
117
118static bool rate_limit_report(unsigned long frame1, unsigned long frame2)
119{
120 struct report_time *use_entry = &report_times[0];
121 unsigned long invalid_before;
122 int i;
123
124 BUILD_BUG_ON(CONFIG_KCSAN_REPORT_ONCE_IN_MS != 0 && REPORT_TIMES_SIZE == 0);
125
126 if (CONFIG_KCSAN_REPORT_ONCE_IN_MS == 0)
127 return false;
128
129 invalid_before = jiffies - msecs_to_jiffies(CONFIG_KCSAN_REPORT_ONCE_IN_MS);
130
131
132 for (i = 0; i < REPORT_TIMES_SIZE; ++i) {
133 struct report_time *rt = &report_times[i];
134
135
136
137
138
139
140
141 if (time_before(rt->time, use_entry->time))
142 use_entry = rt;
143
144
145
146
147
148 if (rt->time == 0)
149 break;
150
151
152 if (time_before(rt->time, invalid_before))
153 continue;
154
155
156 if ((rt->frame1 == frame1 && rt->frame2 == frame2) ||
157 (rt->frame1 == frame2 && rt->frame2 == frame1))
158 return true;
159 }
160
161 use_entry->time = jiffies;
162 use_entry->frame1 = frame1;
163 use_entry->frame2 = frame2;
164 return false;
165}
166
167
168
169
170static bool
171skip_report(enum kcsan_value_change value_change, unsigned long top_frame)
172{
173
174 WARN_ON_ONCE(value_change == KCSAN_VALUE_CHANGE_FALSE);
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY) &&
194 value_change == KCSAN_VALUE_CHANGE_MAYBE) {
195
196
197
198
199
200
201 char buf[64];
202 int len = scnprintf(buf, sizeof(buf), "%ps", (void *)top_frame);
203
204 if (!strnstr(buf, "rcu_", len) &&
205 !strnstr(buf, "_rcu", len) &&
206 !strnstr(buf, "_srcu", len))
207 return true;
208 }
209
210 return kcsan_skip_report_debugfs(top_frame);
211}
212
213static const char *get_access_type(int type)
214{
215 if (type & KCSAN_ACCESS_ASSERT) {
216 if (type & KCSAN_ACCESS_SCOPED) {
217 if (type & KCSAN_ACCESS_WRITE)
218 return "assert no accesses (reordered)";
219 else
220 return "assert no writes (reordered)";
221 } else {
222 if (type & KCSAN_ACCESS_WRITE)
223 return "assert no accesses";
224 else
225 return "assert no writes";
226 }
227 }
228
229 switch (type) {
230 case 0:
231 return "read";
232 case KCSAN_ACCESS_ATOMIC:
233 return "read (marked)";
234 case KCSAN_ACCESS_WRITE:
235 return "write";
236 case KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
237 return "write (marked)";
238 case KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE:
239 return "read-write";
240 case KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
241 return "read-write (marked)";
242 case KCSAN_ACCESS_SCOPED:
243 return "read (reordered)";
244 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_ATOMIC:
245 return "read (marked, reordered)";
246 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE:
247 return "write (reordered)";
248 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
249 return "write (marked, reordered)";
250 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE:
251 return "read-write (reordered)";
252 case KCSAN_ACCESS_SCOPED | KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC:
253 return "read-write (marked, reordered)";
254 default:
255 BUG();
256 }
257}
258
259static const char *get_bug_type(int type)
260{
261 return (type & KCSAN_ACCESS_ASSERT) != 0 ? "assert: race" : "data-race";
262}
263
264
265static const char *get_thread_desc(int task_id)
266{
267 if (task_id != -1) {
268 static char buf[32];
269
270 snprintf(buf, sizeof(buf), "task %i", task_id);
271 return buf;
272 }
273 return "interrupt";
274}
275
276
277static int get_stack_skipnr(const unsigned long stack_entries[], int num_entries)
278{
279 char buf[64];
280 char *cur;
281 int len, skip;
282
283 for (skip = 0; skip < num_entries; ++skip) {
284 len = scnprintf(buf, sizeof(buf), "%ps", (void *)stack_entries[skip]);
285
286
287 if (strnstr(buf, "tsan_", len) ||
288 strnstr(buf, "_once_size", len))
289 continue;
290
291 cur = strnstr(buf, "kcsan_", len);
292 if (cur) {
293 cur += strlen("kcsan_");
294 if (!str_has_prefix(cur, "test"))
295 continue;
296
297 }
298
299
300
301
302
303 break;
304 }
305
306 return skip;
307}
308
309
310
311
312
313
314static int
315replace_stack_entry(unsigned long stack_entries[], int num_entries, unsigned long ip,
316 unsigned long *replaced)
317{
318 unsigned long symbolsize, offset;
319 unsigned long target_func;
320 int skip;
321
322 if (kallsyms_lookup_size_offset(ip, &symbolsize, &offset))
323 target_func = ip - offset;
324 else
325 goto fallback;
326
327 for (skip = 0; skip < num_entries; ++skip) {
328 unsigned long func = stack_entries[skip];
329
330 if (!kallsyms_lookup_size_offset(func, &symbolsize, &offset))
331 goto fallback;
332 func -= offset;
333
334 if (func == target_func) {
335 *replaced = stack_entries[skip];
336 stack_entries[skip] = ip;
337 return skip;
338 }
339 }
340
341fallback:
342
343 WARN_ONCE(1, "Cannot find frame for %pS in stack trace", (void *)ip);
344 return get_stack_skipnr(stack_entries, num_entries);
345}
346
347static int
348sanitize_stack_entries(unsigned long stack_entries[], int num_entries, unsigned long ip,
349 unsigned long *replaced)
350{
351 return ip ? replace_stack_entry(stack_entries, num_entries, ip, replaced) :
352 get_stack_skipnr(stack_entries, num_entries);
353}
354
355
356static int sym_strcmp(void *addr1, void *addr2)
357{
358 char buf1[64];
359 char buf2[64];
360
361 snprintf(buf1, sizeof(buf1), "%pS", addr1);
362 snprintf(buf2, sizeof(buf2), "%pS", addr2);
363
364 return strncmp(buf1, buf2, sizeof(buf1));
365}
366
367static void
368print_stack_trace(unsigned long stack_entries[], int num_entries, unsigned long reordered_to)
369{
370 stack_trace_print(stack_entries, num_entries, 0);
371 if (reordered_to)
372 pr_err(" |\n +-> reordered to: %pS\n", (void *)reordered_to);
373}
374
375static void print_verbose_info(struct task_struct *task)
376{
377 if (!task)
378 return;
379
380
381 kcsan_restore_irqtrace(task);
382
383 pr_err("\n");
384 debug_show_held_locks(task);
385 print_irqtrace_events(task);
386}
387
388static void print_report(enum kcsan_value_change value_change,
389 const struct access_info *ai,
390 struct other_info *other_info,
391 u64 old, u64 new, u64 mask)
392{
393 unsigned long reordered_to = 0;
394 unsigned long stack_entries[NUM_STACK_ENTRIES] = { 0 };
395 int num_stack_entries = stack_trace_save(stack_entries, NUM_STACK_ENTRIES, 1);
396 int skipnr = sanitize_stack_entries(stack_entries, num_stack_entries, ai->ip, &reordered_to);
397 unsigned long this_frame = stack_entries[skipnr];
398 unsigned long other_reordered_to = 0;
399 unsigned long other_frame = 0;
400 int other_skipnr = 0;
401
402
403
404
405 if (skip_report(KCSAN_VALUE_CHANGE_TRUE, stack_entries[skipnr]))
406 return;
407
408 if (other_info) {
409 other_skipnr = sanitize_stack_entries(other_info->stack_entries,
410 other_info->num_stack_entries,
411 other_info->ai.ip, &other_reordered_to);
412 other_frame = other_info->stack_entries[other_skipnr];
413
414
415 if (skip_report(value_change, other_frame))
416 return;
417 }
418
419 if (rate_limit_report(this_frame, other_frame))
420 return;
421
422
423 pr_err("==================================================================\n");
424 if (other_info) {
425 int cmp;
426
427
428
429
430
431 cmp = sym_strcmp((void *)other_frame, (void *)this_frame);
432 pr_err("BUG: KCSAN: %s in %ps / %ps\n",
433 get_bug_type(ai->access_type | other_info->ai.access_type),
434 (void *)(cmp < 0 ? other_frame : this_frame),
435 (void *)(cmp < 0 ? this_frame : other_frame));
436 } else {
437 pr_err("BUG: KCSAN: %s in %pS\n", get_bug_type(ai->access_type),
438 (void *)this_frame);
439 }
440
441 pr_err("\n");
442
443
444 if (other_info) {
445 pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n",
446 get_access_type(other_info->ai.access_type), other_info->ai.ptr,
447 other_info->ai.size, get_thread_desc(other_info->ai.task_pid),
448 other_info->ai.cpu_id);
449
450
451 print_stack_trace(other_info->stack_entries + other_skipnr,
452 other_info->num_stack_entries - other_skipnr,
453 other_reordered_to);
454 if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
455 print_verbose_info(other_info->task);
456
457 pr_err("\n");
458 pr_err("%s to 0x%px of %zu bytes by %s on cpu %i:\n",
459 get_access_type(ai->access_type), ai->ptr, ai->size,
460 get_thread_desc(ai->task_pid), ai->cpu_id);
461 } else {
462 pr_err("race at unknown origin, with %s to 0x%px of %zu bytes by %s on cpu %i:\n",
463 get_access_type(ai->access_type), ai->ptr, ai->size,
464 get_thread_desc(ai->task_pid), ai->cpu_id);
465 }
466
467 print_stack_trace(stack_entries + skipnr, num_stack_entries - skipnr, reordered_to);
468 if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
469 print_verbose_info(current);
470
471
472 if (ai->size <= 8) {
473 int hex_len = ai->size * 2;
474 u64 diff = old ^ new;
475
476 if (mask)
477 diff &= mask;
478 if (diff) {
479 pr_err("\n");
480 pr_err("value changed: 0x%0*llx -> 0x%0*llx\n",
481 hex_len, old, hex_len, new);
482 if (mask) {
483 pr_err(" bits changed: 0x%0*llx with mask 0x%0*llx\n",
484 hex_len, diff, hex_len, mask);
485 }
486 }
487 }
488
489
490 pr_err("\n");
491 pr_err("Reported by Kernel Concurrency Sanitizer on:\n");
492 dump_stack_print_info(KERN_DEFAULT);
493 pr_err("==================================================================\n");
494
495 if (panic_on_warn)
496 panic("panic_on_warn set ...\n");
497}
498
499static void release_report(unsigned long *flags, struct other_info *other_info)
500{
501
502
503
504
505 other_info->ai.size = 0;
506 raw_spin_unlock_irqrestore(&report_lock, *flags);
507}
508
509
510
511
512
513
514
515static void set_other_info_task_blocking(unsigned long *flags,
516 const struct access_info *ai,
517 struct other_info *other_info)
518{
519
520
521
522
523 const bool is_running = task_is_running(current);
524
525
526
527
528
529
530
531
532 int timeout = max(kcsan_udelay_task, kcsan_udelay_interrupt);
533
534 other_info->task = current;
535 do {
536 if (is_running) {
537
538
539
540
541
542 set_current_state(TASK_UNINTERRUPTIBLE);
543 }
544 raw_spin_unlock_irqrestore(&report_lock, *flags);
545
546
547
548
549
550 udelay(1);
551 raw_spin_lock_irqsave(&report_lock, *flags);
552 if (timeout-- < 0) {
553
554
555
556
557
558
559 other_info->task = NULL;
560 break;
561 }
562
563
564
565
566 } while (other_info->ai.size && other_info->ai.ptr == ai->ptr &&
567 other_info->task == current);
568 if (is_running)
569 set_current_state(TASK_RUNNING);
570}
571
572
573static void prepare_report_producer(unsigned long *flags,
574 const struct access_info *ai,
575 struct other_info *other_info)
576{
577 raw_spin_lock_irqsave(&report_lock, *flags);
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592 WARN_ON(other_info->ai.size);
593
594 other_info->ai = *ai;
595 other_info->num_stack_entries = stack_trace_save(other_info->stack_entries, NUM_STACK_ENTRIES, 2);
596
597 if (IS_ENABLED(CONFIG_KCSAN_VERBOSE))
598 set_other_info_task_blocking(flags, ai, other_info);
599
600 raw_spin_unlock_irqrestore(&report_lock, *flags);
601}
602
603
604static bool prepare_report_consumer(unsigned long *flags,
605 const struct access_info *ai,
606 struct other_info *other_info)
607{
608
609 raw_spin_lock_irqsave(&report_lock, *flags);
610 while (!other_info->ai.size) {
611 raw_spin_unlock_irqrestore(&report_lock, *flags);
612 cpu_relax();
613 raw_spin_lock_irqsave(&report_lock, *flags);
614 }
615
616
617 if (WARN_ON(!matching_access((unsigned long)other_info->ai.ptr & WATCHPOINT_ADDR_MASK, other_info->ai.size,
618 (unsigned long)ai->ptr & WATCHPOINT_ADDR_MASK, ai->size)))
619 goto discard;
620
621 if (!matching_access((unsigned long)other_info->ai.ptr, other_info->ai.size,
622 (unsigned long)ai->ptr, ai->size)) {
623
624
625
626
627 atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ENCODING_FALSE_POSITIVES]);
628 goto discard;
629 }
630
631 return true;
632
633discard:
634 release_report(flags, other_info);
635 return false;
636}
637
638static struct access_info prepare_access_info(const volatile void *ptr, size_t size,
639 int access_type, unsigned long ip)
640{
641 return (struct access_info) {
642 .ptr = ptr,
643 .size = size,
644 .access_type = access_type,
645 .task_pid = in_task() ? task_pid_nr(current) : -1,
646 .cpu_id = raw_smp_processor_id(),
647
648 .ip = (access_type & KCSAN_ACCESS_SCOPED) ? ip : 0,
649 };
650}
651
652void kcsan_report_set_info(const volatile void *ptr, size_t size, int access_type,
653 unsigned long ip, int watchpoint_idx)
654{
655 const struct access_info ai = prepare_access_info(ptr, size, access_type, ip);
656 unsigned long flags;
657
658 kcsan_disable_current();
659 lockdep_off();
660
661 prepare_report_producer(&flags, &ai, &other_infos[watchpoint_idx]);
662
663 lockdep_on();
664 kcsan_enable_current();
665}
666
667void kcsan_report_known_origin(const volatile void *ptr, size_t size, int access_type,
668 unsigned long ip, enum kcsan_value_change value_change,
669 int watchpoint_idx, u64 old, u64 new, u64 mask)
670{
671 const struct access_info ai = prepare_access_info(ptr, size, access_type, ip);
672 struct other_info *other_info = &other_infos[watchpoint_idx];
673 unsigned long flags = 0;
674
675 kcsan_disable_current();
676
677
678
679
680
681
682
683 lockdep_off();
684
685 if (!prepare_report_consumer(&flags, &ai, other_info))
686 goto out;
687
688
689
690
691
692 if (value_change != KCSAN_VALUE_CHANGE_FALSE)
693 print_report(value_change, &ai, other_info, old, new, mask);
694
695 release_report(&flags, other_info);
696out:
697 lockdep_on();
698 kcsan_enable_current();
699}
700
701void kcsan_report_unknown_origin(const volatile void *ptr, size_t size, int access_type,
702 unsigned long ip, u64 old, u64 new, u64 mask)
703{
704 const struct access_info ai = prepare_access_info(ptr, size, access_type, ip);
705 unsigned long flags;
706
707 kcsan_disable_current();
708 lockdep_off();
709
710 raw_spin_lock_irqsave(&report_lock, flags);
711 print_report(KCSAN_VALUE_CHANGE_TRUE, &ai, NULL, old, new, mask);
712 raw_spin_unlock_irqrestore(&report_lock, flags);
713
714 lockdep_on();
715 kcsan_enable_current();
716}
717