Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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