1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * KCSAN test with various race scenarious to test runtime behaviour. Since the
4 * interface with which KCSAN's reports are obtained is via the console, this is
5 * the output we should verify. For each test case checks the presence (or
6 * absence) of generated reports. Relies on 'console' tracepoint to capture
7 * reports as they appear in the kernel log.
8 *
9 * Makes use of KUnit for test organization, and the Torture framework for test
10 * thread control.
11 *
12 * Copyright (C) 2020, Google LLC.
13 * Author: Marco Elver <elver@google.com>
14 */
15
16 #define pr_fmt(fmt) "kcsan_test: " fmt
17
18 #include <kunit/test.h>
19 #include <linux/jiffies.h>
20 #include <linux/kcsan-checks.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/seqlock.h>
24 #include <linux/spinlock.h>
25 #include <linux/string.h>
26 #include <linux/timer.h>
27 #include <linux/torture.h>
28 #include <linux/tracepoint.h>
29 #include <linux/types.h>
30 #include <trace/events/printk.h>
31
32 #define KCSAN_TEST_REQUIRES(test, cond) do { \
33 if (!(cond)) \
34 kunit_skip((test), "Test requires: " #cond); \
35 } while (0)
36
37 #ifdef CONFIG_CC_HAS_TSAN_COMPOUND_READ_BEFORE_WRITE
38 #define __KCSAN_ACCESS_RW(alt) (KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE)
39 #else
40 #define __KCSAN_ACCESS_RW(alt) (alt)
41 #endif
42
43 /* Points to current test-case memory access "kernels". */
44 static void (*access_kernels[2])(void);
45
46 static struct task_struct **threads; /* Lists of threads. */
47 static unsigned long end_time; /* End time of test. */
48
49 /* Report as observed from console. */
50 static struct {
51 spinlock_t lock;
52 int nlines;
53 char lines[3][512];
54 } observed = {
55 .lock = __SPIN_LOCK_UNLOCKED(observed.lock),
56 };
57
58 /* Setup test checking loop. */
59 static __no_kcsan inline void
begin_test_checks(void (* func1)(void),void (* func2)(void))60 begin_test_checks(void (*func1)(void), void (*func2)(void))
61 {
62 kcsan_disable_current();
63
64 /*
65 * Require at least as long as KCSAN_REPORT_ONCE_IN_MS, to ensure at
66 * least one race is reported.
67 */
68 end_time = jiffies + msecs_to_jiffies(CONFIG_KCSAN_REPORT_ONCE_IN_MS + 500);
69
70 /* Signal start; release potential initialization of shared data. */
71 smp_store_release(&access_kernels[0], func1);
72 smp_store_release(&access_kernels[1], func2);
73 }
74
75 /* End test checking loop. */
76 static __no_kcsan inline bool
end_test_checks(bool stop)77 end_test_checks(bool stop)
78 {
79 if (!stop && time_before(jiffies, end_time)) {
80 /* Continue checking */
81 might_sleep();
82 return false;
83 }
84
85 kcsan_enable_current();
86 return true;
87 }
88
89 /*
90 * Probe for console output: checks if a race was reported, and obtains observed
91 * lines of interest.
92 */
93 __no_kcsan
probe_console(void * ignore,const char * buf,size_t len)94 static void probe_console(void *ignore, const char *buf, size_t len)
95 {
96 unsigned long flags;
97 int nlines;
98
99 /*
100 * Note that KCSAN reports under a global lock, so we do not risk the
101 * possibility of having multiple reports interleaved. If that were the
102 * case, we'd expect tests to fail.
103 */
104
105 spin_lock_irqsave(&observed.lock, flags);
106 nlines = observed.nlines;
107
108 if (strnstr(buf, "BUG: KCSAN: ", len) && strnstr(buf, "test_", len)) {
109 /*
110 * KCSAN report and related to the test.
111 *
112 * The provided @buf is not NUL-terminated; copy no more than
113 * @len bytes and let strscpy() add the missing NUL-terminator.
114 */
115 strscpy(observed.lines[0], buf, min(len + 1, sizeof(observed.lines[0])));
116 nlines = 1;
117 } else if ((nlines == 1 || nlines == 2) && strnstr(buf, "bytes by", len)) {
118 strscpy(observed.lines[nlines++], buf, min(len + 1, sizeof(observed.lines[0])));
119
120 if (strnstr(buf, "race at unknown origin", len)) {
121 if (WARN_ON(nlines != 2))
122 goto out;
123
124 /* No second line of interest. */
125 strcpy(observed.lines[nlines++], "<none>");
126 }
127 }
128
129 out:
130 WRITE_ONCE(observed.nlines, nlines); /* Publish new nlines. */
131 spin_unlock_irqrestore(&observed.lock, flags);
132 }
133
134 /* Check if a report related to the test exists. */
135 __no_kcsan
report_available(void)136 static bool report_available(void)
137 {
138 return READ_ONCE(observed.nlines) == ARRAY_SIZE(observed.lines);
139 }
140
141 /* Report information we expect in a report. */
142 struct expect_report {
143 /* Access information of both accesses. */
144 struct {
145 void *fn; /* Function pointer to expected function of top frame. */
146 void *addr; /* Address of access; unchecked if NULL. */
147 size_t size; /* Size of access; unchecked if @addr is NULL. */
148 int type; /* Access type, see KCSAN_ACCESS definitions. */
149 } access[2];
150 };
151
152 /* Check observed report matches information in @r. */
153 __no_kcsan
report_matches(const struct expect_report * r)154 static bool report_matches(const struct expect_report *r)
155 {
156 const bool is_assert = (r->access[0].type | r->access[1].type) & KCSAN_ACCESS_ASSERT;
157 bool ret = false;
158 unsigned long flags;
159 typeof(observed.lines) expect;
160 const char *end;
161 char *cur;
162 int i;
163
164 /* Doubled-checked locking. */
165 if (!report_available())
166 return false;
167
168 /* Generate expected report contents. */
169
170 /* Title */
171 cur = expect[0];
172 end = &expect[0][sizeof(expect[0]) - 1];
173 cur += scnprintf(cur, end - cur, "BUG: KCSAN: %s in ",
174 is_assert ? "assert: race" : "data-race");
175 if (r->access[1].fn) {
176 char tmp[2][64];
177 int cmp;
178
179 /* Expect lexographically sorted function names in title. */
180 scnprintf(tmp[0], sizeof(tmp[0]), "%pS", r->access[0].fn);
181 scnprintf(tmp[1], sizeof(tmp[1]), "%pS", r->access[1].fn);
182 cmp = strcmp(tmp[0], tmp[1]);
183 cur += scnprintf(cur, end - cur, "%ps / %ps",
184 cmp < 0 ? r->access[0].fn : r->access[1].fn,
185 cmp < 0 ? r->access[1].fn : r->access[0].fn);
186 } else {
187 scnprintf(cur, end - cur, "%pS", r->access[0].fn);
188 /* The exact offset won't match, remove it. */
189 cur = strchr(expect[0], '+');
190 if (cur)
191 *cur = '\0';
192 }
193
194 /* Access 1 */
195 cur = expect[1];
196 end = &expect[1][sizeof(expect[1]) - 1];
197 if (!r->access[1].fn)
198 cur += scnprintf(cur, end - cur, "race at unknown origin, with ");
199
200 /* Access 1 & 2 */
201 for (i = 0; i < 2; ++i) {
202 const int ty = r->access[i].type;
203 const char *const access_type =
204 (ty & KCSAN_ACCESS_ASSERT) ?
205 ((ty & KCSAN_ACCESS_WRITE) ?
206 "assert no accesses" :
207 "assert no writes") :
208 ((ty & KCSAN_ACCESS_WRITE) ?
209 ((ty & KCSAN_ACCESS_COMPOUND) ?
210 "read-write" :
211 "write") :
212 "read");
213 const bool is_atomic = (ty & KCSAN_ACCESS_ATOMIC);
214 const bool is_scoped = (ty & KCSAN_ACCESS_SCOPED);
215 const char *const access_type_aux =
216 (is_atomic && is_scoped) ? " (marked, scoped)"
217 : (is_atomic ? " (marked)"
218 : (is_scoped ? " (scoped)" : ""));
219
220 if (i == 1) {
221 /* Access 2 */
222 cur = expect[2];
223 end = &expect[2][sizeof(expect[2]) - 1];
224
225 if (!r->access[1].fn) {
226 /* Dummy string if no second access is available. */
227 strcpy(cur, "<none>");
228 break;
229 }
230 }
231
232 cur += scnprintf(cur, end - cur, "%s%s to ", access_type,
233 access_type_aux);
234
235 if (r->access[i].addr) /* Address is optional. */
236 cur += scnprintf(cur, end - cur, "0x%px of %zu bytes",
237 r->access[i].addr, r->access[i].size);
238 }
239
240 spin_lock_irqsave(&observed.lock, flags);
241 if (!report_available())
242 goto out; /* A new report is being captured. */
243
244 /* Finally match expected output to what we actually observed. */
245 ret = strstr(observed.lines[0], expect[0]) &&
246 /* Access info may appear in any order. */
247 ((strstr(observed.lines[1], expect[1]) &&
248 strstr(observed.lines[2], expect[2])) ||
249 (strstr(observed.lines[1], expect[2]) &&
250 strstr(observed.lines[2], expect[1])));
251 out:
252 spin_unlock_irqrestore(&observed.lock, flags);
253 return ret;
254 }
255
256 /* ===== Test kernels ===== */
257
258 static long test_sink;
259 static long test_var;
260 /* @test_array should be large enough to fall into multiple watchpoint slots. */
261 static long test_array[3 * PAGE_SIZE / sizeof(long)];
262 static struct {
263 long val[8];
264 } test_struct;
265 static DEFINE_SEQLOCK(test_seqlock);
266
267 /*
268 * Helper to avoid compiler optimizing out reads, and to generate source values
269 * for writes.
270 */
271 __no_kcsan
sink_value(long v)272 static noinline void sink_value(long v) { WRITE_ONCE(test_sink, v); }
273
test_kernel_read(void)274 static noinline void test_kernel_read(void) { sink_value(test_var); }
275
test_kernel_write(void)276 static noinline void test_kernel_write(void)
277 {
278 test_var = READ_ONCE_NOCHECK(test_sink) + 1;
279 }
280
test_kernel_write_nochange(void)281 static noinline void test_kernel_write_nochange(void) { test_var = 42; }
282
283 /* Suffixed by value-change exception filter. */
test_kernel_write_nochange_rcu(void)284 static noinline void test_kernel_write_nochange_rcu(void) { test_var = 42; }
285
test_kernel_read_atomic(void)286 static noinline void test_kernel_read_atomic(void)
287 {
288 sink_value(READ_ONCE(test_var));
289 }
290
test_kernel_write_atomic(void)291 static noinline void test_kernel_write_atomic(void)
292 {
293 WRITE_ONCE(test_var, READ_ONCE_NOCHECK(test_sink) + 1);
294 }
295
test_kernel_atomic_rmw(void)296 static noinline void test_kernel_atomic_rmw(void)
297 {
298 /* Use builtin, so we can set up the "bad" atomic/non-atomic scenario. */
299 __atomic_fetch_add(&test_var, 1, __ATOMIC_RELAXED);
300 }
301
302 __no_kcsan
test_kernel_write_uninstrumented(void)303 static noinline void test_kernel_write_uninstrumented(void) { test_var++; }
304
test_kernel_data_race(void)305 static noinline void test_kernel_data_race(void) { data_race(test_var++); }
306
test_kernel_assert_writer(void)307 static noinline void test_kernel_assert_writer(void)
308 {
309 ASSERT_EXCLUSIVE_WRITER(test_var);
310 }
311
test_kernel_assert_access(void)312 static noinline void test_kernel_assert_access(void)
313 {
314 ASSERT_EXCLUSIVE_ACCESS(test_var);
315 }
316
317 #define TEST_CHANGE_BITS 0xff00ff00
318
test_kernel_change_bits(void)319 static noinline void test_kernel_change_bits(void)
320 {
321 if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) {
322 /*
323 * Avoid race of unknown origin for this test, just pretend they
324 * are atomic.
325 */
326 kcsan_nestable_atomic_begin();
327 test_var ^= TEST_CHANGE_BITS;
328 kcsan_nestable_atomic_end();
329 } else
330 WRITE_ONCE(test_var, READ_ONCE(test_var) ^ TEST_CHANGE_BITS);
331 }
332
test_kernel_assert_bits_change(void)333 static noinline void test_kernel_assert_bits_change(void)
334 {
335 ASSERT_EXCLUSIVE_BITS(test_var, TEST_CHANGE_BITS);
336 }
337
test_kernel_assert_bits_nochange(void)338 static noinline void test_kernel_assert_bits_nochange(void)
339 {
340 ASSERT_EXCLUSIVE_BITS(test_var, ~TEST_CHANGE_BITS);
341 }
342
343 /*
344 * Scoped assertions do trigger anywhere in scope. However, the report should
345 * still only point at the start of the scope.
346 */
test_enter_scope(void)347 static noinline void test_enter_scope(void)
348 {
349 int x = 0;
350
351 /* Unrelated accesses to scoped assert. */
352 READ_ONCE(test_sink);
353 kcsan_check_read(&x, sizeof(x));
354 }
355
test_kernel_assert_writer_scoped(void)356 static noinline void test_kernel_assert_writer_scoped(void)
357 {
358 ASSERT_EXCLUSIVE_WRITER_SCOPED(test_var);
359 test_enter_scope();
360 }
361
test_kernel_assert_access_scoped(void)362 static noinline void test_kernel_assert_access_scoped(void)
363 {
364 ASSERT_EXCLUSIVE_ACCESS_SCOPED(test_var);
365 test_enter_scope();
366 }
367
test_kernel_rmw_array(void)368 static noinline void test_kernel_rmw_array(void)
369 {
370 int i;
371
372 for (i = 0; i < ARRAY_SIZE(test_array); ++i)
373 test_array[i]++;
374 }
375
test_kernel_write_struct(void)376 static noinline void test_kernel_write_struct(void)
377 {
378 kcsan_check_write(&test_struct, sizeof(test_struct));
379 kcsan_disable_current();
380 test_struct.val[3]++; /* induce value change */
381 kcsan_enable_current();
382 }
383
test_kernel_write_struct_part(void)384 static noinline void test_kernel_write_struct_part(void)
385 {
386 test_struct.val[3] = 42;
387 }
388
test_kernel_read_struct_zero_size(void)389 static noinline void test_kernel_read_struct_zero_size(void)
390 {
391 kcsan_check_read(&test_struct.val[3], 0);
392 }
393
test_kernel_jiffies_reader(void)394 static noinline void test_kernel_jiffies_reader(void)
395 {
396 sink_value((long)jiffies);
397 }
398
test_kernel_seqlock_reader(void)399 static noinline void test_kernel_seqlock_reader(void)
400 {
401 unsigned int seq;
402
403 do {
404 seq = read_seqbegin(&test_seqlock);
405 sink_value(test_var);
406 } while (read_seqretry(&test_seqlock, seq));
407 }
408
test_kernel_seqlock_writer(void)409 static noinline void test_kernel_seqlock_writer(void)
410 {
411 unsigned long flags;
412
413 write_seqlock_irqsave(&test_seqlock, flags);
414 test_var++;
415 write_sequnlock_irqrestore(&test_seqlock, flags);
416 }
417
test_kernel_atomic_builtins(void)418 static noinline void test_kernel_atomic_builtins(void)
419 {
420 /*
421 * Generate concurrent accesses, expecting no reports, ensuring KCSAN
422 * treats builtin atomics as actually atomic.
423 */
424 __atomic_load_n(&test_var, __ATOMIC_RELAXED);
425 }
426
test_kernel_xor_1bit(void)427 static noinline void test_kernel_xor_1bit(void)
428 {
429 /* Do not report data races between the read-writes. */
430 kcsan_nestable_atomic_begin();
431 test_var ^= 0x10000;
432 kcsan_nestable_atomic_end();
433 }
434
435 /* ===== Test cases ===== */
436
437 /* Simple test with normal data race. */
438 __no_kcsan
test_basic(struct kunit * test)439 static void test_basic(struct kunit *test)
440 {
441 const struct expect_report expect = {
442 .access = {
443 { test_kernel_write, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
444 { test_kernel_read, &test_var, sizeof(test_var), 0 },
445 },
446 };
447 static const struct expect_report never = {
448 .access = {
449 { test_kernel_read, &test_var, sizeof(test_var), 0 },
450 { test_kernel_read, &test_var, sizeof(test_var), 0 },
451 },
452 };
453 bool match_expect = false;
454 bool match_never = false;
455
456 begin_test_checks(test_kernel_write, test_kernel_read);
457 do {
458 match_expect |= report_matches(&expect);
459 match_never = report_matches(&never);
460 } while (!end_test_checks(match_never));
461 KUNIT_EXPECT_TRUE(test, match_expect);
462 KUNIT_EXPECT_FALSE(test, match_never);
463 }
464
465 /*
466 * Stress KCSAN with lots of concurrent races on different addresses until
467 * timeout.
468 */
469 __no_kcsan
test_concurrent_races(struct kunit * test)470 static void test_concurrent_races(struct kunit *test)
471 {
472 const struct expect_report expect = {
473 .access = {
474 /* NULL will match any address. */
475 { test_kernel_rmw_array, NULL, 0, __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
476 { test_kernel_rmw_array, NULL, 0, __KCSAN_ACCESS_RW(0) },
477 },
478 };
479 static const struct expect_report never = {
480 .access = {
481 { test_kernel_rmw_array, NULL, 0, 0 },
482 { test_kernel_rmw_array, NULL, 0, 0 },
483 },
484 };
485 bool match_expect = false;
486 bool match_never = false;
487
488 begin_test_checks(test_kernel_rmw_array, test_kernel_rmw_array);
489 do {
490 match_expect |= report_matches(&expect);
491 match_never |= report_matches(&never);
492 } while (!end_test_checks(false));
493 KUNIT_EXPECT_TRUE(test, match_expect); /* Sanity check matches exist. */
494 KUNIT_EXPECT_FALSE(test, match_never);
495 }
496
497 /* Test the KCSAN_REPORT_VALUE_CHANGE_ONLY option. */
498 __no_kcsan
test_novalue_change(struct kunit * test)499 static void test_novalue_change(struct kunit *test)
500 {
501 const struct expect_report expect_rw = {
502 .access = {
503 { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
504 { test_kernel_read, &test_var, sizeof(test_var), 0 },
505 },
506 };
507 const struct expect_report expect_ww = {
508 .access = {
509 { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
510 { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
511 },
512 };
513 bool match_expect = false;
514
515 test_kernel_write_nochange(); /* Reset value. */
516 begin_test_checks(test_kernel_write_nochange, test_kernel_read);
517 do {
518 match_expect = report_matches(&expect_rw) || report_matches(&expect_ww);
519 } while (!end_test_checks(match_expect));
520 if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY))
521 KUNIT_EXPECT_FALSE(test, match_expect);
522 else
523 KUNIT_EXPECT_TRUE(test, match_expect);
524 }
525
526 /*
527 * Test that the rules where the KCSAN_REPORT_VALUE_CHANGE_ONLY option should
528 * never apply work.
529 */
530 __no_kcsan
test_novalue_change_exception(struct kunit * test)531 static void test_novalue_change_exception(struct kunit *test)
532 {
533 const struct expect_report expect_rw = {
534 .access = {
535 { test_kernel_write_nochange_rcu, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
536 { test_kernel_read, &test_var, sizeof(test_var), 0 },
537 },
538 };
539 const struct expect_report expect_ww = {
540 .access = {
541 { test_kernel_write_nochange_rcu, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
542 { test_kernel_write_nochange_rcu, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
543 },
544 };
545 bool match_expect = false;
546
547 test_kernel_write_nochange_rcu(); /* Reset value. */
548 begin_test_checks(test_kernel_write_nochange_rcu, test_kernel_read);
549 do {
550 match_expect = report_matches(&expect_rw) || report_matches(&expect_ww);
551 } while (!end_test_checks(match_expect));
552 KUNIT_EXPECT_TRUE(test, match_expect);
553 }
554
555 /* Test that data races of unknown origin are reported. */
556 __no_kcsan
test_unknown_origin(struct kunit * test)557 static void test_unknown_origin(struct kunit *test)
558 {
559 const struct expect_report expect = {
560 .access = {
561 { test_kernel_read, &test_var, sizeof(test_var), 0 },
562 { NULL },
563 },
564 };
565 bool match_expect = false;
566
567 begin_test_checks(test_kernel_write_uninstrumented, test_kernel_read);
568 do {
569 match_expect = report_matches(&expect);
570 } while (!end_test_checks(match_expect));
571 if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN))
572 KUNIT_EXPECT_TRUE(test, match_expect);
573 else
574 KUNIT_EXPECT_FALSE(test, match_expect);
575 }
576
577 /* Test KCSAN_ASSUME_PLAIN_WRITES_ATOMIC if it is selected. */
578 __no_kcsan
test_write_write_assume_atomic(struct kunit * test)579 static void test_write_write_assume_atomic(struct kunit *test)
580 {
581 const struct expect_report expect = {
582 .access = {
583 { test_kernel_write, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
584 { test_kernel_write, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
585 },
586 };
587 bool match_expect = false;
588
589 begin_test_checks(test_kernel_write, test_kernel_write);
590 do {
591 sink_value(READ_ONCE(test_var)); /* induce value-change */
592 match_expect = report_matches(&expect);
593 } while (!end_test_checks(match_expect));
594 if (IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC))
595 KUNIT_EXPECT_FALSE(test, match_expect);
596 else
597 KUNIT_EXPECT_TRUE(test, match_expect);
598 }
599
600 /*
601 * Test that data races with writes larger than word-size are always reported,
602 * even if KCSAN_ASSUME_PLAIN_WRITES_ATOMIC is selected.
603 */
604 __no_kcsan
test_write_write_struct(struct kunit * test)605 static void test_write_write_struct(struct kunit *test)
606 {
607 const struct expect_report expect = {
608 .access = {
609 { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
610 { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
611 },
612 };
613 bool match_expect = false;
614
615 begin_test_checks(test_kernel_write_struct, test_kernel_write_struct);
616 do {
617 match_expect = report_matches(&expect);
618 } while (!end_test_checks(match_expect));
619 KUNIT_EXPECT_TRUE(test, match_expect);
620 }
621
622 /*
623 * Test that data races where only one write is larger than word-size are always
624 * reported, even if KCSAN_ASSUME_PLAIN_WRITES_ATOMIC is selected.
625 */
626 __no_kcsan
test_write_write_struct_part(struct kunit * test)627 static void test_write_write_struct_part(struct kunit *test)
628 {
629 const struct expect_report expect = {
630 .access = {
631 { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
632 { test_kernel_write_struct_part, &test_struct.val[3], sizeof(test_struct.val[3]), KCSAN_ACCESS_WRITE },
633 },
634 };
635 bool match_expect = false;
636
637 begin_test_checks(test_kernel_write_struct, test_kernel_write_struct_part);
638 do {
639 match_expect = report_matches(&expect);
640 } while (!end_test_checks(match_expect));
641 KUNIT_EXPECT_TRUE(test, match_expect);
642 }
643
644 /* Test that races with atomic accesses never result in reports. */
645 __no_kcsan
test_read_atomic_write_atomic(struct kunit * test)646 static void test_read_atomic_write_atomic(struct kunit *test)
647 {
648 bool match_never = false;
649
650 begin_test_checks(test_kernel_read_atomic, test_kernel_write_atomic);
651 do {
652 match_never = report_available();
653 } while (!end_test_checks(match_never));
654 KUNIT_EXPECT_FALSE(test, match_never);
655 }
656
657 /* Test that a race with an atomic and plain access result in reports. */
658 __no_kcsan
test_read_plain_atomic_write(struct kunit * test)659 static void test_read_plain_atomic_write(struct kunit *test)
660 {
661 const struct expect_report expect = {
662 .access = {
663 { test_kernel_read, &test_var, sizeof(test_var), 0 },
664 { test_kernel_write_atomic, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC },
665 },
666 };
667 bool match_expect = false;
668
669 KCSAN_TEST_REQUIRES(test, !IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS));
670
671 begin_test_checks(test_kernel_read, test_kernel_write_atomic);
672 do {
673 match_expect = report_matches(&expect);
674 } while (!end_test_checks(match_expect));
675 KUNIT_EXPECT_TRUE(test, match_expect);
676 }
677
678 /* Test that atomic RMWs generate correct report. */
679 __no_kcsan
test_read_plain_atomic_rmw(struct kunit * test)680 static void test_read_plain_atomic_rmw(struct kunit *test)
681 {
682 const struct expect_report expect = {
683 .access = {
684 { test_kernel_read, &test_var, sizeof(test_var), 0 },
685 { test_kernel_atomic_rmw, &test_var, sizeof(test_var),
686 KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC },
687 },
688 };
689 bool match_expect = false;
690
691 KCSAN_TEST_REQUIRES(test, !IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS));
692
693 begin_test_checks(test_kernel_read, test_kernel_atomic_rmw);
694 do {
695 match_expect = report_matches(&expect);
696 } while (!end_test_checks(match_expect));
697 KUNIT_EXPECT_TRUE(test, match_expect);
698 }
699
700 /* Zero-sized accesses should never cause data race reports. */
701 __no_kcsan
test_zero_size_access(struct kunit * test)702 static void test_zero_size_access(struct kunit *test)
703 {
704 const struct expect_report expect = {
705 .access = {
706 { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
707 { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
708 },
709 };
710 const struct expect_report never = {
711 .access = {
712 { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
713 { test_kernel_read_struct_zero_size, &test_struct.val[3], 0, 0 },
714 },
715 };
716 bool match_expect = false;
717 bool match_never = false;
718
719 begin_test_checks(test_kernel_write_struct, test_kernel_read_struct_zero_size);
720 do {
721 match_expect |= report_matches(&expect);
722 match_never = report_matches(&never);
723 } while (!end_test_checks(match_never));
724 KUNIT_EXPECT_TRUE(test, match_expect); /* Sanity check. */
725 KUNIT_EXPECT_FALSE(test, match_never);
726 }
727
728 /* Test the data_race() macro. */
729 __no_kcsan
test_data_race(struct kunit * test)730 static void test_data_race(struct kunit *test)
731 {
732 bool match_never = false;
733
734 begin_test_checks(test_kernel_data_race, test_kernel_data_race);
735 do {
736 match_never = report_available();
737 } while (!end_test_checks(match_never));
738 KUNIT_EXPECT_FALSE(test, match_never);
739 }
740
741 __no_kcsan
test_assert_exclusive_writer(struct kunit * test)742 static void test_assert_exclusive_writer(struct kunit *test)
743 {
744 const struct expect_report expect = {
745 .access = {
746 { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
747 { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
748 },
749 };
750 bool match_expect = false;
751
752 begin_test_checks(test_kernel_assert_writer, test_kernel_write_nochange);
753 do {
754 match_expect = report_matches(&expect);
755 } while (!end_test_checks(match_expect));
756 KUNIT_EXPECT_TRUE(test, match_expect);
757 }
758
759 __no_kcsan
test_assert_exclusive_access(struct kunit * test)760 static void test_assert_exclusive_access(struct kunit *test)
761 {
762 const struct expect_report expect = {
763 .access = {
764 { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
765 { test_kernel_read, &test_var, sizeof(test_var), 0 },
766 },
767 };
768 bool match_expect = false;
769
770 begin_test_checks(test_kernel_assert_access, test_kernel_read);
771 do {
772 match_expect = report_matches(&expect);
773 } while (!end_test_checks(match_expect));
774 KUNIT_EXPECT_TRUE(test, match_expect);
775 }
776
777 __no_kcsan
test_assert_exclusive_access_writer(struct kunit * test)778 static void test_assert_exclusive_access_writer(struct kunit *test)
779 {
780 const struct expect_report expect_access_writer = {
781 .access = {
782 { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
783 { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
784 },
785 };
786 const struct expect_report expect_access_access = {
787 .access = {
788 { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
789 { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
790 },
791 };
792 const struct expect_report never = {
793 .access = {
794 { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
795 { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
796 },
797 };
798 bool match_expect_access_writer = false;
799 bool match_expect_access_access = false;
800 bool match_never = false;
801
802 begin_test_checks(test_kernel_assert_access, test_kernel_assert_writer);
803 do {
804 match_expect_access_writer |= report_matches(&expect_access_writer);
805 match_expect_access_access |= report_matches(&expect_access_access);
806 match_never |= report_matches(&never);
807 } while (!end_test_checks(match_never));
808 KUNIT_EXPECT_TRUE(test, match_expect_access_writer);
809 KUNIT_EXPECT_TRUE(test, match_expect_access_access);
810 KUNIT_EXPECT_FALSE(test, match_never);
811 }
812
813 __no_kcsan
test_assert_exclusive_bits_change(struct kunit * test)814 static void test_assert_exclusive_bits_change(struct kunit *test)
815 {
816 const struct expect_report expect = {
817 .access = {
818 { test_kernel_assert_bits_change, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
819 { test_kernel_change_bits, &test_var, sizeof(test_var),
820 KCSAN_ACCESS_WRITE | (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS) ? 0 : KCSAN_ACCESS_ATOMIC) },
821 },
822 };
823 bool match_expect = false;
824
825 begin_test_checks(test_kernel_assert_bits_change, test_kernel_change_bits);
826 do {
827 match_expect = report_matches(&expect);
828 } while (!end_test_checks(match_expect));
829 KUNIT_EXPECT_TRUE(test, match_expect);
830 }
831
832 __no_kcsan
test_assert_exclusive_bits_nochange(struct kunit * test)833 static void test_assert_exclusive_bits_nochange(struct kunit *test)
834 {
835 bool match_never = false;
836
837 begin_test_checks(test_kernel_assert_bits_nochange, test_kernel_change_bits);
838 do {
839 match_never = report_available();
840 } while (!end_test_checks(match_never));
841 KUNIT_EXPECT_FALSE(test, match_never);
842 }
843
844 __no_kcsan
test_assert_exclusive_writer_scoped(struct kunit * test)845 static void test_assert_exclusive_writer_scoped(struct kunit *test)
846 {
847 const struct expect_report expect_start = {
848 .access = {
849 { test_kernel_assert_writer_scoped, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_SCOPED },
850 { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
851 },
852 };
853 const struct expect_report expect_inscope = {
854 .access = {
855 { test_enter_scope, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_SCOPED },
856 { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
857 },
858 };
859 bool match_expect_start = false;
860 bool match_expect_inscope = false;
861
862 begin_test_checks(test_kernel_assert_writer_scoped, test_kernel_write_nochange);
863 do {
864 match_expect_start |= report_matches(&expect_start);
865 match_expect_inscope |= report_matches(&expect_inscope);
866 } while (!end_test_checks(match_expect_inscope));
867 KUNIT_EXPECT_TRUE(test, match_expect_start);
868 KUNIT_EXPECT_FALSE(test, match_expect_inscope);
869 }
870
871 __no_kcsan
test_assert_exclusive_access_scoped(struct kunit * test)872 static void test_assert_exclusive_access_scoped(struct kunit *test)
873 {
874 const struct expect_report expect_start1 = {
875 .access = {
876 { test_kernel_assert_access_scoped, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_SCOPED },
877 { test_kernel_read, &test_var, sizeof(test_var), 0 },
878 },
879 };
880 const struct expect_report expect_start2 = {
881 .access = { expect_start1.access[0], expect_start1.access[0] },
882 };
883 const struct expect_report expect_inscope = {
884 .access = {
885 { test_enter_scope, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_SCOPED },
886 { test_kernel_read, &test_var, sizeof(test_var), 0 },
887 },
888 };
889 bool match_expect_start = false;
890 bool match_expect_inscope = false;
891
892 begin_test_checks(test_kernel_assert_access_scoped, test_kernel_read);
893 end_time += msecs_to_jiffies(1000); /* This test requires a bit more time. */
894 do {
895 match_expect_start |= report_matches(&expect_start1) || report_matches(&expect_start2);
896 match_expect_inscope |= report_matches(&expect_inscope);
897 } while (!end_test_checks(match_expect_inscope));
898 KUNIT_EXPECT_TRUE(test, match_expect_start);
899 KUNIT_EXPECT_FALSE(test, match_expect_inscope);
900 }
901
902 /*
903 * jiffies is special (declared to be volatile) and its accesses are typically
904 * not marked; this test ensures that the compiler nor KCSAN gets confused about
905 * jiffies's declaration on different architectures.
906 */
907 __no_kcsan
test_jiffies_noreport(struct kunit * test)908 static void test_jiffies_noreport(struct kunit *test)
909 {
910 bool match_never = false;
911
912 begin_test_checks(test_kernel_jiffies_reader, test_kernel_jiffies_reader);
913 do {
914 match_never = report_available();
915 } while (!end_test_checks(match_never));
916 KUNIT_EXPECT_FALSE(test, match_never);
917 }
918
919 /* Test that racing accesses in seqlock critical sections are not reported. */
920 __no_kcsan
test_seqlock_noreport(struct kunit * test)921 static void test_seqlock_noreport(struct kunit *test)
922 {
923 bool match_never = false;
924
925 begin_test_checks(test_kernel_seqlock_reader, test_kernel_seqlock_writer);
926 do {
927 match_never = report_available();
928 } while (!end_test_checks(match_never));
929 KUNIT_EXPECT_FALSE(test, match_never);
930 }
931
932 /*
933 * Test atomic builtins work and required instrumentation functions exist. We
934 * also test that KCSAN understands they're atomic by racing with them via
935 * test_kernel_atomic_builtins(), and expect no reports.
936 *
937 * The atomic builtins _SHOULD NOT_ be used in normal kernel code!
938 */
test_atomic_builtins(struct kunit * test)939 static void test_atomic_builtins(struct kunit *test)
940 {
941 bool match_never = false;
942
943 begin_test_checks(test_kernel_atomic_builtins, test_kernel_atomic_builtins);
944 do {
945 long tmp;
946
947 kcsan_enable_current();
948
949 __atomic_store_n(&test_var, 42L, __ATOMIC_RELAXED);
950 KUNIT_EXPECT_EQ(test, 42L, __atomic_load_n(&test_var, __ATOMIC_RELAXED));
951
952 KUNIT_EXPECT_EQ(test, 42L, __atomic_exchange_n(&test_var, 20, __ATOMIC_RELAXED));
953 KUNIT_EXPECT_EQ(test, 20L, test_var);
954
955 tmp = 20L;
956 KUNIT_EXPECT_TRUE(test, __atomic_compare_exchange_n(&test_var, &tmp, 30L,
957 0, __ATOMIC_RELAXED,
958 __ATOMIC_RELAXED));
959 KUNIT_EXPECT_EQ(test, tmp, 20L);
960 KUNIT_EXPECT_EQ(test, test_var, 30L);
961 KUNIT_EXPECT_FALSE(test, __atomic_compare_exchange_n(&test_var, &tmp, 40L,
962 1, __ATOMIC_RELAXED,
963 __ATOMIC_RELAXED));
964 KUNIT_EXPECT_EQ(test, tmp, 30L);
965 KUNIT_EXPECT_EQ(test, test_var, 30L);
966
967 KUNIT_EXPECT_EQ(test, 30L, __atomic_fetch_add(&test_var, 1, __ATOMIC_RELAXED));
968 KUNIT_EXPECT_EQ(test, 31L, __atomic_fetch_sub(&test_var, 1, __ATOMIC_RELAXED));
969 KUNIT_EXPECT_EQ(test, 30L, __atomic_fetch_and(&test_var, 0xf, __ATOMIC_RELAXED));
970 KUNIT_EXPECT_EQ(test, 14L, __atomic_fetch_xor(&test_var, 0xf, __ATOMIC_RELAXED));
971 KUNIT_EXPECT_EQ(test, 1L, __atomic_fetch_or(&test_var, 0xf0, __ATOMIC_RELAXED));
972 KUNIT_EXPECT_EQ(test, 241L, __atomic_fetch_nand(&test_var, 0xf, __ATOMIC_RELAXED));
973 KUNIT_EXPECT_EQ(test, -2L, test_var);
974
975 __atomic_thread_fence(__ATOMIC_SEQ_CST);
976 __atomic_signal_fence(__ATOMIC_SEQ_CST);
977
978 kcsan_disable_current();
979
980 match_never = report_available();
981 } while (!end_test_checks(match_never));
982 KUNIT_EXPECT_FALSE(test, match_never);
983 }
984
985 __no_kcsan
test_1bit_value_change(struct kunit * test)986 static void test_1bit_value_change(struct kunit *test)
987 {
988 const struct expect_report expect = {
989 .access = {
990 { test_kernel_read, &test_var, sizeof(test_var), 0 },
991 { test_kernel_xor_1bit, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
992 },
993 };
994 bool match = false;
995
996 begin_test_checks(test_kernel_read, test_kernel_xor_1bit);
997 do {
998 match = IS_ENABLED(CONFIG_KCSAN_PERMISSIVE)
999 ? report_available()
1000 : report_matches(&expect);
1001 } while (!end_test_checks(match));
1002 if (IS_ENABLED(CONFIG_KCSAN_PERMISSIVE))
1003 KUNIT_EXPECT_FALSE(test, match);
1004 else
1005 KUNIT_EXPECT_TRUE(test, match);
1006 }
1007
1008 /*
1009 * Generate thread counts for all test cases. Values generated are in interval
1010 * [2, 5] followed by exponentially increasing thread counts from 8 to 32.
1011 *
1012 * The thread counts are chosen to cover potentially interesting boundaries and
1013 * corner cases (2 to 5), and then stress the system with larger counts.
1014 */
nthreads_gen_params(const void * prev,char * desc)1015 static const void *nthreads_gen_params(const void *prev, char *desc)
1016 {
1017 long nthreads = (long)prev;
1018
1019 if (nthreads < 0 || nthreads >= 32)
1020 nthreads = 0; /* stop */
1021 else if (!nthreads)
1022 nthreads = 2; /* initial value */
1023 else if (nthreads < 5)
1024 nthreads++;
1025 else if (nthreads == 5)
1026 nthreads = 8;
1027 else
1028 nthreads *= 2;
1029
1030 if (!IS_ENABLED(CONFIG_PREEMPT) || !IS_ENABLED(CONFIG_KCSAN_INTERRUPT_WATCHER)) {
1031 /*
1032 * Without any preemption, keep 2 CPUs free for other tasks, one
1033 * of which is the main test case function checking for
1034 * completion or failure.
1035 */
1036 const long min_unused_cpus = IS_ENABLED(CONFIG_PREEMPT_NONE) ? 2 : 0;
1037 const long min_required_cpus = 2 + min_unused_cpus;
1038
1039 if (num_online_cpus() < min_required_cpus) {
1040 pr_err_once("Too few online CPUs (%u < %ld) for test\n",
1041 num_online_cpus(), min_required_cpus);
1042 nthreads = 0;
1043 } else if (nthreads >= num_online_cpus() - min_unused_cpus) {
1044 /* Use negative value to indicate last param. */
1045 nthreads = -(num_online_cpus() - min_unused_cpus);
1046 pr_warn_once("Limiting number of threads to %ld (only %d online CPUs)\n",
1047 -nthreads, num_online_cpus());
1048 }
1049 }
1050
1051 snprintf(desc, KUNIT_PARAM_DESC_SIZE, "threads=%ld", abs(nthreads));
1052 return (void *)nthreads;
1053 }
1054
1055 #define KCSAN_KUNIT_CASE(test_name) KUNIT_CASE_PARAM(test_name, nthreads_gen_params)
1056 static struct kunit_case kcsan_test_cases[] = {
1057 KCSAN_KUNIT_CASE(test_basic),
1058 KCSAN_KUNIT_CASE(test_concurrent_races),
1059 KCSAN_KUNIT_CASE(test_novalue_change),
1060 KCSAN_KUNIT_CASE(test_novalue_change_exception),
1061 KCSAN_KUNIT_CASE(test_unknown_origin),
1062 KCSAN_KUNIT_CASE(test_write_write_assume_atomic),
1063 KCSAN_KUNIT_CASE(test_write_write_struct),
1064 KCSAN_KUNIT_CASE(test_write_write_struct_part),
1065 KCSAN_KUNIT_CASE(test_read_atomic_write_atomic),
1066 KCSAN_KUNIT_CASE(test_read_plain_atomic_write),
1067 KCSAN_KUNIT_CASE(test_read_plain_atomic_rmw),
1068 KCSAN_KUNIT_CASE(test_zero_size_access),
1069 KCSAN_KUNIT_CASE(test_data_race),
1070 KCSAN_KUNIT_CASE(test_assert_exclusive_writer),
1071 KCSAN_KUNIT_CASE(test_assert_exclusive_access),
1072 KCSAN_KUNIT_CASE(test_assert_exclusive_access_writer),
1073 KCSAN_KUNIT_CASE(test_assert_exclusive_bits_change),
1074 KCSAN_KUNIT_CASE(test_assert_exclusive_bits_nochange),
1075 KCSAN_KUNIT_CASE(test_assert_exclusive_writer_scoped),
1076 KCSAN_KUNIT_CASE(test_assert_exclusive_access_scoped),
1077 KCSAN_KUNIT_CASE(test_jiffies_noreport),
1078 KCSAN_KUNIT_CASE(test_seqlock_noreport),
1079 KCSAN_KUNIT_CASE(test_atomic_builtins),
1080 KCSAN_KUNIT_CASE(test_1bit_value_change),
1081 {},
1082 };
1083
1084 /* ===== End test cases ===== */
1085
1086 /* Concurrent accesses from interrupts. */
1087 __no_kcsan
access_thread_timer(struct timer_list * timer)1088 static void access_thread_timer(struct timer_list *timer)
1089 {
1090 static atomic_t cnt = ATOMIC_INIT(0);
1091 unsigned int idx;
1092 void (*func)(void);
1093
1094 idx = (unsigned int)atomic_inc_return(&cnt) % ARRAY_SIZE(access_kernels);
1095 /* Acquire potential initialization. */
1096 func = smp_load_acquire(&access_kernels[idx]);
1097 if (func)
1098 func();
1099 }
1100
1101 /* The main loop for each thread. */
1102 __no_kcsan
access_thread(void * arg)1103 static int access_thread(void *arg)
1104 {
1105 struct timer_list timer;
1106 unsigned int cnt = 0;
1107 unsigned int idx;
1108 void (*func)(void);
1109
1110 timer_setup_on_stack(&timer, access_thread_timer, 0);
1111 do {
1112 might_sleep();
1113
1114 if (!timer_pending(&timer))
1115 mod_timer(&timer, jiffies + 1);
1116 else {
1117 /* Iterate through all kernels. */
1118 idx = cnt++ % ARRAY_SIZE(access_kernels);
1119 /* Acquire potential initialization. */
1120 func = smp_load_acquire(&access_kernels[idx]);
1121 if (func)
1122 func();
1123 }
1124 } while (!torture_must_stop());
1125 del_timer_sync(&timer);
1126 destroy_timer_on_stack(&timer);
1127
1128 torture_kthread_stopping("access_thread");
1129 return 0;
1130 }
1131
1132 __no_kcsan
test_init(struct kunit * test)1133 static int test_init(struct kunit *test)
1134 {
1135 unsigned long flags;
1136 int nthreads;
1137 int i;
1138
1139 spin_lock_irqsave(&observed.lock, flags);
1140 for (i = 0; i < ARRAY_SIZE(observed.lines); ++i)
1141 observed.lines[i][0] = '\0';
1142 observed.nlines = 0;
1143 spin_unlock_irqrestore(&observed.lock, flags);
1144
1145 if (!torture_init_begin((char *)test->name, 1))
1146 return -EBUSY;
1147
1148 if (WARN_ON(threads))
1149 goto err;
1150
1151 for (i = 0; i < ARRAY_SIZE(access_kernels); ++i) {
1152 if (WARN_ON(access_kernels[i]))
1153 goto err;
1154 }
1155
1156 nthreads = abs((long)test->param_value);
1157 if (WARN_ON(!nthreads))
1158 goto err;
1159
1160 threads = kcalloc(nthreads + 1, sizeof(struct task_struct *), GFP_KERNEL);
1161 if (WARN_ON(!threads))
1162 goto err;
1163
1164 threads[nthreads] = NULL;
1165 for (i = 0; i < nthreads; ++i) {
1166 if (torture_create_kthread(access_thread, NULL, threads[i]))
1167 goto err;
1168 }
1169
1170 torture_init_end();
1171
1172 return 0;
1173
1174 err:
1175 kfree(threads);
1176 threads = NULL;
1177 torture_init_end();
1178 return -EINVAL;
1179 }
1180
1181 __no_kcsan
test_exit(struct kunit * test)1182 static void test_exit(struct kunit *test)
1183 {
1184 struct task_struct **stop_thread;
1185 int i;
1186
1187 if (torture_cleanup_begin())
1188 return;
1189
1190 for (i = 0; i < ARRAY_SIZE(access_kernels); ++i)
1191 WRITE_ONCE(access_kernels[i], NULL);
1192
1193 if (threads) {
1194 for (stop_thread = threads; *stop_thread; stop_thread++)
1195 torture_stop_kthread(reader_thread, *stop_thread);
1196
1197 kfree(threads);
1198 threads = NULL;
1199 }
1200
1201 torture_cleanup_end();
1202 }
1203
1204 static struct kunit_suite kcsan_test_suite = {
1205 .name = "kcsan",
1206 .test_cases = kcsan_test_cases,
1207 .init = test_init,
1208 .exit = test_exit,
1209 };
1210 static struct kunit_suite *kcsan_test_suites[] = { &kcsan_test_suite, NULL };
1211
1212 __no_kcsan
register_tracepoints(struct tracepoint * tp,void * ignore)1213 static void register_tracepoints(struct tracepoint *tp, void *ignore)
1214 {
1215 check_trace_callback_type_console(probe_console);
1216 if (!strcmp(tp->name, "console"))
1217 WARN_ON(tracepoint_probe_register(tp, probe_console, NULL));
1218 }
1219
1220 __no_kcsan
unregister_tracepoints(struct tracepoint * tp,void * ignore)1221 static void unregister_tracepoints(struct tracepoint *tp, void *ignore)
1222 {
1223 if (!strcmp(tp->name, "console"))
1224 tracepoint_probe_unregister(tp, probe_console, NULL);
1225 }
1226
1227 /*
1228 * We only want to do tracepoints setup and teardown once, therefore we have to
1229 * customize the init and exit functions and cannot rely on kunit_test_suite().
1230 */
kcsan_test_init(void)1231 static int __init kcsan_test_init(void)
1232 {
1233 /*
1234 * Because we want to be able to build the test as a module, we need to
1235 * iterate through all known tracepoints, since the static registration
1236 * won't work here.
1237 */
1238 for_each_kernel_tracepoint(register_tracepoints, NULL);
1239 return __kunit_test_suites_init(kcsan_test_suites);
1240 }
1241
kcsan_test_exit(void)1242 static void kcsan_test_exit(void)
1243 {
1244 __kunit_test_suites_exit(kcsan_test_suites);
1245 for_each_kernel_tracepoint(unregister_tracepoints, NULL);
1246 tracepoint_synchronize_unregister();
1247 }
1248
1249 late_initcall_sync(kcsan_test_init);
1250 module_exit(kcsan_test_exit);
1251
1252 MODULE_LICENSE("GPL v2");
1253 MODULE_AUTHOR("Marco Elver <elver@google.com>");
1254