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