~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/kcsan/kcsan_test.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /kernel/kcsan/kcsan_test.c (Version linux-6.12-rc7) and /kernel/kcsan/kcsan_test.c (Version linux-6.9.12)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * KCSAN test with various race scenarious to       3  * KCSAN test with various race scenarious to test runtime behaviour. Since the
  4  * interface with which KCSAN's reports are ob      4  * interface with which KCSAN's reports are obtained is via the console, this is
  5  * the output we should verify. For each test       5  * the output we should verify. For each test case checks the presence (or
  6  * absence) of generated reports. Relies on 'c      6  * absence) of generated reports. Relies on 'console' tracepoint to capture
  7  * reports as they appear in the kernel log.        7  * reports as they appear in the kernel log.
  8  *                                                  8  *
  9  * Makes use of KUnit for test organization, a      9  * Makes use of KUnit for test organization, and the Torture framework for test
 10  * thread control.                                 10  * thread control.
 11  *                                                 11  *
 12  * Copyright (C) 2020, Google LLC.                 12  * Copyright (C) 2020, Google LLC.
 13  * Author: Marco Elver <elver@google.com>          13  * Author: Marco Elver <elver@google.com>
 14  */                                                14  */
 15                                                    15 
 16 #define pr_fmt(fmt) "kcsan_test: " fmt             16 #define pr_fmt(fmt) "kcsan_test: " fmt
 17                                                    17 
 18 #include <kunit/test.h>                            18 #include <kunit/test.h>
 19 #include <linux/atomic.h>                          19 #include <linux/atomic.h>
 20 #include <linux/bitops.h>                          20 #include <linux/bitops.h>
 21 #include <linux/jiffies.h>                         21 #include <linux/jiffies.h>
 22 #include <linux/kcsan-checks.h>                    22 #include <linux/kcsan-checks.h>
 23 #include <linux/kernel.h>                          23 #include <linux/kernel.h>
 24 #include <linux/mutex.h>                           24 #include <linux/mutex.h>
 25 #include <linux/sched.h>                           25 #include <linux/sched.h>
 26 #include <linux/seqlock.h>                         26 #include <linux/seqlock.h>
 27 #include <linux/spinlock.h>                        27 #include <linux/spinlock.h>
 28 #include <linux/string.h>                          28 #include <linux/string.h>
 29 #include <linux/timer.h>                           29 #include <linux/timer.h>
 30 #include <linux/torture.h>                         30 #include <linux/torture.h>
 31 #include <linux/tracepoint.h>                      31 #include <linux/tracepoint.h>
 32 #include <linux/types.h>                           32 #include <linux/types.h>
 33 #include <trace/events/printk.h>                   33 #include <trace/events/printk.h>
 34                                                    34 
 35 #define KCSAN_TEST_REQUIRES(test, cond) do {       35 #define KCSAN_TEST_REQUIRES(test, cond) do {                    \
 36         if (!(cond))                               36         if (!(cond))                                            \
 37                 kunit_skip((test), "Test requi     37                 kunit_skip((test), "Test requires: " #cond);    \
 38 } while (0)                                        38 } while (0)
 39                                                    39 
 40 #ifdef CONFIG_CC_HAS_TSAN_COMPOUND_READ_BEFORE     40 #ifdef CONFIG_CC_HAS_TSAN_COMPOUND_READ_BEFORE_WRITE
 41 #define __KCSAN_ACCESS_RW(alt) (KCSAN_ACCESS_C     41 #define __KCSAN_ACCESS_RW(alt) (KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE)
 42 #else                                              42 #else
 43 #define __KCSAN_ACCESS_RW(alt) (alt)               43 #define __KCSAN_ACCESS_RW(alt) (alt)
 44 #endif                                             44 #endif
 45                                                    45 
 46 /* Points to current test-case memory access "     46 /* Points to current test-case memory access "kernels". */
 47 static void (*access_kernels[2])(void);            47 static void (*access_kernels[2])(void);
 48                                                    48 
 49 static struct task_struct **threads; /* Lists      49 static struct task_struct **threads; /* Lists of threads. */
 50 static unsigned long end_time;       /* End ti     50 static unsigned long end_time;       /* End time of test. */
 51                                                    51 
 52 /* Report as observed from console. */             52 /* Report as observed from console. */
 53 static struct {                                    53 static struct {
 54         spinlock_t lock;                           54         spinlock_t lock;
 55         int nlines;                                55         int nlines;
 56         char lines[3][512];                        56         char lines[3][512];
 57 } observed = {                                     57 } observed = {
 58         .lock = __SPIN_LOCK_UNLOCKED(observed.     58         .lock = __SPIN_LOCK_UNLOCKED(observed.lock),
 59 };                                                 59 };
 60                                                    60 
 61 /* Setup test checking loop. */                    61 /* Setup test checking loop. */
 62 static __no_kcsan inline void                      62 static __no_kcsan inline void
 63 begin_test_checks(void (*func1)(void), void (*     63 begin_test_checks(void (*func1)(void), void (*func2)(void))
 64 {                                                  64 {
 65         kcsan_disable_current();                   65         kcsan_disable_current();
 66                                                    66 
 67         /*                                         67         /*
 68          * Require at least as long as KCSAN_R     68          * Require at least as long as KCSAN_REPORT_ONCE_IN_MS, to ensure at
 69          * least one race is reported.             69          * least one race is reported.
 70          */                                        70          */
 71         end_time = jiffies + msecs_to_jiffies(     71         end_time = jiffies + msecs_to_jiffies(CONFIG_KCSAN_REPORT_ONCE_IN_MS + 500);
 72                                                    72 
 73         /* Signal start; release potential ini     73         /* Signal start; release potential initialization of shared data. */
 74         smp_store_release(&access_kernels[0],      74         smp_store_release(&access_kernels[0], func1);
 75         smp_store_release(&access_kernels[1],      75         smp_store_release(&access_kernels[1], func2);
 76 }                                                  76 }
 77                                                    77 
 78 /* End test checking loop. */                      78 /* End test checking loop. */
 79 static __no_kcsan inline bool                      79 static __no_kcsan inline bool
 80 end_test_checks(bool stop)                         80 end_test_checks(bool stop)
 81 {                                                  81 {
 82         if (!stop && time_before(jiffies, end_     82         if (!stop && time_before(jiffies, end_time)) {
 83                 /* Continue checking */            83                 /* Continue checking */
 84                 might_sleep();                     84                 might_sleep();
 85                 return false;                      85                 return false;
 86         }                                          86         }
 87                                                    87 
 88         kcsan_enable_current();                    88         kcsan_enable_current();
 89         return true;                               89         return true;
 90 }                                                  90 }
 91                                                    91 
 92 /*                                                 92 /*
 93  * Probe for console output: checks if a race      93  * Probe for console output: checks if a race was reported, and obtains observed
 94  * lines of interest.                              94  * lines of interest.
 95  */                                                95  */
 96 __no_kcsan                                         96 __no_kcsan
 97 static void probe_console(void *ignore, const      97 static void probe_console(void *ignore, const char *buf, size_t len)
 98 {                                                  98 {
 99         unsigned long flags;                       99         unsigned long flags;
100         int nlines;                               100         int nlines;
101                                                   101 
102         /*                                        102         /*
103          * Note that KCSAN reports under a glo    103          * Note that KCSAN reports under a global lock, so we do not risk the
104          * possibility of having multiple repo    104          * possibility of having multiple reports interleaved. If that were the
105          * case, we'd expect tests to fail.       105          * case, we'd expect tests to fail.
106          */                                       106          */
107                                                   107 
108         spin_lock_irqsave(&observed.lock, flag    108         spin_lock_irqsave(&observed.lock, flags);
109         nlines = observed.nlines;                 109         nlines = observed.nlines;
110                                                   110 
111         if (strnstr(buf, "BUG: KCSAN: ", len)     111         if (strnstr(buf, "BUG: KCSAN: ", len) && strnstr(buf, "test_", len)) {
112                 /*                                112                 /*
113                  * KCSAN report and related to    113                  * KCSAN report and related to the test.
114                  *                                114                  *
115                  * The provided @buf is not NU    115                  * The provided @buf is not NUL-terminated; copy no more than
116                  * @len bytes and let strscpy(    116                  * @len bytes and let strscpy() add the missing NUL-terminator.
117                  */                               117                  */
118                 strscpy(observed.lines[0], buf    118                 strscpy(observed.lines[0], buf, min(len + 1, sizeof(observed.lines[0])));
119                 nlines = 1;                       119                 nlines = 1;
120         } else if ((nlines == 1 || nlines == 2    120         } else if ((nlines == 1 || nlines == 2) && strnstr(buf, "bytes by", len)) {
121                 strscpy(observed.lines[nlines+    121                 strscpy(observed.lines[nlines++], buf, min(len + 1, sizeof(observed.lines[0])));
122                                                   122 
123                 if (strnstr(buf, "race at unkn    123                 if (strnstr(buf, "race at unknown origin", len)) {
124                         if (WARN_ON(nlines !=     124                         if (WARN_ON(nlines != 2))
125                                 goto out;         125                                 goto out;
126                                                   126 
127                         /* No second line of i    127                         /* No second line of interest. */
128                         strcpy(observed.lines[    128                         strcpy(observed.lines[nlines++], "<none>");
129                 }                                 129                 }
130         }                                         130         }
131                                                   131 
132 out:                                              132 out:
133         WRITE_ONCE(observed.nlines, nlines); /    133         WRITE_ONCE(observed.nlines, nlines); /* Publish new nlines. */
134         spin_unlock_irqrestore(&observed.lock,    134         spin_unlock_irqrestore(&observed.lock, flags);
135 }                                                 135 }
136                                                   136 
137 /* Check if a report related to the test exist    137 /* Check if a report related to the test exists. */
138 __no_kcsan                                        138 __no_kcsan
139 static bool report_available(void)                139 static bool report_available(void)
140 {                                                 140 {
141         return READ_ONCE(observed.nlines) == A    141         return READ_ONCE(observed.nlines) == ARRAY_SIZE(observed.lines);
142 }                                                 142 }
143                                                   143 
144 /* Report information we expect in a report. *    144 /* Report information we expect in a report. */
145 struct expect_report {                            145 struct expect_report {
146         /* Access information of both accesses    146         /* Access information of both accesses. */
147         struct {                                  147         struct {
148                 void *fn;    /* Function point    148                 void *fn;    /* Function pointer to expected function of top frame. */
149                 void *addr;  /* Address of acc    149                 void *addr;  /* Address of access; unchecked if NULL. */
150                 size_t size; /* Size of access    150                 size_t size; /* Size of access; unchecked if @addr is NULL. */
151                 int type;    /* Access type, s    151                 int type;    /* Access type, see KCSAN_ACCESS definitions. */
152         } access[2];                              152         } access[2];
153 };                                                153 };
154                                                   154 
155 /* Check observed report matches information i    155 /* Check observed report matches information in @r. */
156 __no_kcsan                                        156 __no_kcsan
157 static bool __report_matches(const struct expe    157 static bool __report_matches(const struct expect_report *r)
158 {                                                 158 {
159         const bool is_assert = (r->access[0].t    159         const bool is_assert = (r->access[0].type | r->access[1].type) & KCSAN_ACCESS_ASSERT;
160         bool ret = false;                         160         bool ret = false;
161         unsigned long flags;                      161         unsigned long flags;
162         typeof(*observed.lines) *expect;          162         typeof(*observed.lines) *expect;
163         const char *end;                          163         const char *end;
164         char *cur;                                164         char *cur;
165         int i;                                    165         int i;
166                                                   166 
167         /* Doubled-checked locking. */            167         /* Doubled-checked locking. */
168         if (!report_available())                  168         if (!report_available())
169                 return false;                     169                 return false;
170                                                   170 
171         expect = kmalloc(sizeof(observed.lines    171         expect = kmalloc(sizeof(observed.lines), GFP_KERNEL);
172         if (WARN_ON(!expect))                     172         if (WARN_ON(!expect))
173                 return false;                     173                 return false;
174                                                   174 
175         /* Generate expected report contents.     175         /* Generate expected report contents. */
176                                                   176 
177         /* Title */                               177         /* Title */
178         cur = expect[0];                          178         cur = expect[0];
179         end = &expect[0][sizeof(expect[0]) - 1    179         end = &expect[0][sizeof(expect[0]) - 1];
180         cur += scnprintf(cur, end - cur, "BUG:    180         cur += scnprintf(cur, end - cur, "BUG: KCSAN: %s in ",
181                          is_assert ? "assert:     181                          is_assert ? "assert: race" : "data-race");
182         if (r->access[1].fn) {                    182         if (r->access[1].fn) {
183                 char tmp[2][64];                  183                 char tmp[2][64];
184                 int cmp;                          184                 int cmp;
185                                                   185 
186                 /* Expect lexographically sort    186                 /* Expect lexographically sorted function names in title. */
187                 scnprintf(tmp[0], sizeof(tmp[0    187                 scnprintf(tmp[0], sizeof(tmp[0]), "%pS", r->access[0].fn);
188                 scnprintf(tmp[1], sizeof(tmp[1    188                 scnprintf(tmp[1], sizeof(tmp[1]), "%pS", r->access[1].fn);
189                 cmp = strcmp(tmp[0], tmp[1]);     189                 cmp = strcmp(tmp[0], tmp[1]);
190                 cur += scnprintf(cur, end - cu    190                 cur += scnprintf(cur, end - cur, "%ps / %ps",
191                                  cmp < 0 ? r->    191                                  cmp < 0 ? r->access[0].fn : r->access[1].fn,
192                                  cmp < 0 ? r->    192                                  cmp < 0 ? r->access[1].fn : r->access[0].fn);
193         } else {                                  193         } else {
194                 scnprintf(cur, end - cur, "%pS    194                 scnprintf(cur, end - cur, "%pS", r->access[0].fn);
195                 /* The exact offset won't matc    195                 /* The exact offset won't match, remove it. */
196                 cur = strchr(expect[0], '+');     196                 cur = strchr(expect[0], '+');
197                 if (cur)                          197                 if (cur)
198                         *cur = '\0';              198                         *cur = '\0';
199         }                                         199         }
200                                                   200 
201         /* Access 1 */                            201         /* Access 1 */
202         cur = expect[1];                          202         cur = expect[1];
203         end = &expect[1][sizeof(expect[1]) - 1    203         end = &expect[1][sizeof(expect[1]) - 1];
204         if (!r->access[1].fn)                     204         if (!r->access[1].fn)
205                 cur += scnprintf(cur, end - cu    205                 cur += scnprintf(cur, end - cur, "race at unknown origin, with ");
206                                                   206 
207         /* Access 1 & 2 */                        207         /* Access 1 & 2 */
208         for (i = 0; i < 2; ++i) {                 208         for (i = 0; i < 2; ++i) {
209                 const int ty = r->access[i].ty    209                 const int ty = r->access[i].type;
210                 const char *const access_type     210                 const char *const access_type =
211                         (ty & KCSAN_ACCESS_ASS    211                         (ty & KCSAN_ACCESS_ASSERT) ?
212                                       ((ty & K    212                                       ((ty & KCSAN_ACCESS_WRITE) ?
213                                                   213                                                "assert no accesses" :
214                                                   214                                                "assert no writes") :
215                                       ((ty & K    215                                       ((ty & KCSAN_ACCESS_WRITE) ?
216                                                   216                                                ((ty & KCSAN_ACCESS_COMPOUND) ?
217                                                   217                                                         "read-write" :
218                                                   218                                                         "write") :
219                                                   219                                                "read");
220                 const bool is_atomic = (ty & K    220                 const bool is_atomic = (ty & KCSAN_ACCESS_ATOMIC);
221                 const bool is_scoped = (ty & K    221                 const bool is_scoped = (ty & KCSAN_ACCESS_SCOPED);
222                 const char *const access_type_    222                 const char *const access_type_aux =
223                                 (is_atomic &&     223                                 (is_atomic && is_scoped)        ? " (marked, reordered)"
224                                 : (is_atomic      224                                 : (is_atomic                    ? " (marked)"
225                                    : (is_scope    225                                    : (is_scoped                 ? " (reordered)" : ""));
226                                                   226 
227                 if (i == 1) {                     227                 if (i == 1) {
228                         /* Access 2 */            228                         /* Access 2 */
229                         cur = expect[2];          229                         cur = expect[2];
230                         end = &expect[2][sizeo    230                         end = &expect[2][sizeof(expect[2]) - 1];
231                                                   231 
232                         if (!r->access[1].fn)     232                         if (!r->access[1].fn) {
233                                 /* Dummy strin    233                                 /* Dummy string if no second access is available. */
234                                 strcpy(cur, "<    234                                 strcpy(cur, "<none>");
235                                 break;            235                                 break;
236                         }                         236                         }
237                 }                                 237                 }
238                                                   238 
239                 cur += scnprintf(cur, end - cu    239                 cur += scnprintf(cur, end - cur, "%s%s to ", access_type,
240                                  access_type_a    240                                  access_type_aux);
241                                                   241 
242                 if (r->access[i].addr) /* Addr    242                 if (r->access[i].addr) /* Address is optional. */
243                         cur += scnprintf(cur,     243                         cur += scnprintf(cur, end - cur, "0x%px of %zu bytes",
244                                          r->ac    244                                          r->access[i].addr, r->access[i].size);
245         }                                         245         }
246                                                   246 
247         spin_lock_irqsave(&observed.lock, flag    247         spin_lock_irqsave(&observed.lock, flags);
248         if (!report_available())                  248         if (!report_available())
249                 goto out; /* A new report is b    249                 goto out; /* A new report is being captured. */
250                                                   250 
251         /* Finally match expected output to wh    251         /* Finally match expected output to what we actually observed. */
252         ret = strstr(observed.lines[0], expect    252         ret = strstr(observed.lines[0], expect[0]) &&
253               /* Access info may appear in any    253               /* Access info may appear in any order. */
254               ((strstr(observed.lines[1], expe    254               ((strstr(observed.lines[1], expect[1]) &&
255                 strstr(observed.lines[2], expe    255                 strstr(observed.lines[2], expect[2])) ||
256                (strstr(observed.lines[1], expe    256                (strstr(observed.lines[1], expect[2]) &&
257                 strstr(observed.lines[2], expe    257                 strstr(observed.lines[2], expect[1])));
258 out:                                              258 out:
259         spin_unlock_irqrestore(&observed.lock,    259         spin_unlock_irqrestore(&observed.lock, flags);
260         kfree(expect);                            260         kfree(expect);
261         return ret;                               261         return ret;
262 }                                                 262 }
263                                                   263 
264 static __always_inline const struct expect_rep    264 static __always_inline const struct expect_report *
265 __report_set_scoped(struct expect_report *r, i    265 __report_set_scoped(struct expect_report *r, int accesses)
266 {                                                 266 {
267         BUILD_BUG_ON(accesses > 3);               267         BUILD_BUG_ON(accesses > 3);
268                                                   268 
269         if (accesses & 1)                         269         if (accesses & 1)
270                 r->access[0].type |= KCSAN_ACC    270                 r->access[0].type |= KCSAN_ACCESS_SCOPED;
271         else                                      271         else
272                 r->access[0].type &= ~KCSAN_AC    272                 r->access[0].type &= ~KCSAN_ACCESS_SCOPED;
273                                                   273 
274         if (accesses & 2)                         274         if (accesses & 2)
275                 r->access[1].type |= KCSAN_ACC    275                 r->access[1].type |= KCSAN_ACCESS_SCOPED;
276         else                                      276         else
277                 r->access[1].type &= ~KCSAN_AC    277                 r->access[1].type &= ~KCSAN_ACCESS_SCOPED;
278                                                   278 
279         return r;                                 279         return r;
280 }                                                 280 }
281                                                   281 
282 __no_kcsan                                        282 __no_kcsan
283 static bool report_matches_any_reordered(struc    283 static bool report_matches_any_reordered(struct expect_report *r)
284 {                                                 284 {
285         return __report_matches(__report_set_s    285         return __report_matches(__report_set_scoped(r, 0)) ||
286                __report_matches(__report_set_s    286                __report_matches(__report_set_scoped(r, 1)) ||
287                __report_matches(__report_set_s    287                __report_matches(__report_set_scoped(r, 2)) ||
288                __report_matches(__report_set_s    288                __report_matches(__report_set_scoped(r, 3));
289 }                                                 289 }
290                                                   290 
291 #ifdef CONFIG_KCSAN_WEAK_MEMORY                   291 #ifdef CONFIG_KCSAN_WEAK_MEMORY
292 /* Due to reordering accesses, any access may     292 /* Due to reordering accesses, any access may appear as "(reordered)". */
293 #define report_matches report_matches_any_reor    293 #define report_matches report_matches_any_reordered
294 #else                                             294 #else
295 #define report_matches __report_matches           295 #define report_matches __report_matches
296 #endif                                            296 #endif
297                                                   297 
298 /* ===== Test kernels ===== */                    298 /* ===== Test kernels ===== */
299                                                   299 
300 static long test_sink;                            300 static long test_sink;
301 static long test_var;                             301 static long test_var;
302 /* @test_array should be large enough to fall     302 /* @test_array should be large enough to fall into multiple watchpoint slots. */
303 static long test_array[3 * PAGE_SIZE / sizeof(    303 static long test_array[3 * PAGE_SIZE / sizeof(long)];
304 static struct {                                   304 static struct {
305         long val[8];                              305         long val[8];
306 } test_struct;                                    306 } test_struct;
307 static long __data_racy test_data_racy;        << 
308 static DEFINE_SEQLOCK(test_seqlock);              307 static DEFINE_SEQLOCK(test_seqlock);
309 static DEFINE_SPINLOCK(test_spinlock);            308 static DEFINE_SPINLOCK(test_spinlock);
310 static DEFINE_MUTEX(test_mutex);                  309 static DEFINE_MUTEX(test_mutex);
311                                                   310 
312 /*                                                311 /*
313  * Helper to avoid compiler optimizing out rea    312  * Helper to avoid compiler optimizing out reads, and to generate source values
314  * for writes.                                    313  * for writes.
315  */                                               314  */
316 __no_kcsan                                        315 __no_kcsan
317 static noinline void sink_value(long v) { WRIT    316 static noinline void sink_value(long v) { WRITE_ONCE(test_sink, v); }
318                                                   317 
319 /*                                                318 /*
320  * Generates a delay and some accesses that en    319  * Generates a delay and some accesses that enter the runtime but do not produce
321  * data races.                                    320  * data races.
322  */                                               321  */
323 static noinline void test_delay(int iter)         322 static noinline void test_delay(int iter)
324 {                                                 323 {
325         while (iter--)                            324         while (iter--)
326                 sink_value(READ_ONCE(test_sink    325                 sink_value(READ_ONCE(test_sink));
327 }                                                 326 }
328                                                   327 
329 static noinline void test_kernel_read(void) {     328 static noinline void test_kernel_read(void) { sink_value(test_var); }
330                                                   329 
331 static noinline void test_kernel_write(void)      330 static noinline void test_kernel_write(void)
332 {                                                 331 {
333         test_var = READ_ONCE_NOCHECK(test_sink    332         test_var = READ_ONCE_NOCHECK(test_sink) + 1;
334 }                                                 333 }
335                                                   334 
336 static noinline void test_kernel_write_nochang    335 static noinline void test_kernel_write_nochange(void) { test_var = 42; }
337                                                   336 
338 /* Suffixed by value-change exception filter.     337 /* Suffixed by value-change exception filter. */
339 static noinline void test_kernel_write_nochang    338 static noinline void test_kernel_write_nochange_rcu(void) { test_var = 42; }
340                                                   339 
341 static noinline void test_kernel_read_atomic(v    340 static noinline void test_kernel_read_atomic(void)
342 {                                                 341 {
343         sink_value(READ_ONCE(test_var));          342         sink_value(READ_ONCE(test_var));
344 }                                                 343 }
345                                                   344 
346 static noinline void test_kernel_write_atomic(    345 static noinline void test_kernel_write_atomic(void)
347 {                                                 346 {
348         WRITE_ONCE(test_var, READ_ONCE_NOCHECK    347         WRITE_ONCE(test_var, READ_ONCE_NOCHECK(test_sink) + 1);
349 }                                                 348 }
350                                                   349 
351 static noinline void test_kernel_atomic_rmw(vo    350 static noinline void test_kernel_atomic_rmw(void)
352 {                                                 351 {
353         /* Use builtin, so we can set up the "    352         /* Use builtin, so we can set up the "bad" atomic/non-atomic scenario. */
354         __atomic_fetch_add(&test_var, 1, __ATO    353         __atomic_fetch_add(&test_var, 1, __ATOMIC_RELAXED);
355 }                                                 354 }
356                                                   355 
357 __no_kcsan                                        356 __no_kcsan
358 static noinline void test_kernel_write_uninstr    357 static noinline void test_kernel_write_uninstrumented(void) { test_var++; }
359                                                   358 
360 static noinline void test_kernel_data_race(voi    359 static noinline void test_kernel_data_race(void) { data_race(test_var++); }
361                                                   360 
362 static noinline void test_kernel_data_racy_qua << 
363                                                << 
364 static noinline void test_kernel_assert_writer    361 static noinline void test_kernel_assert_writer(void)
365 {                                                 362 {
366         ASSERT_EXCLUSIVE_WRITER(test_var);        363         ASSERT_EXCLUSIVE_WRITER(test_var);
367 }                                                 364 }
368                                                   365 
369 static noinline void test_kernel_assert_access    366 static noinline void test_kernel_assert_access(void)
370 {                                                 367 {
371         ASSERT_EXCLUSIVE_ACCESS(test_var);        368         ASSERT_EXCLUSIVE_ACCESS(test_var);
372 }                                                 369 }
373                                                   370 
374 #define TEST_CHANGE_BITS 0xff00ff00               371 #define TEST_CHANGE_BITS 0xff00ff00
375                                                   372 
376 static noinline void test_kernel_change_bits(v    373 static noinline void test_kernel_change_bits(void)
377 {                                                 374 {
378         if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATO    375         if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) {
379                 /*                                376                 /*
380                  * Avoid race of unknown origi    377                  * Avoid race of unknown origin for this test, just pretend they
381                  * are atomic.                    378                  * are atomic.
382                  */                               379                  */
383                 kcsan_nestable_atomic_begin();    380                 kcsan_nestable_atomic_begin();
384                 test_var ^= TEST_CHANGE_BITS;     381                 test_var ^= TEST_CHANGE_BITS;
385                 kcsan_nestable_atomic_end();      382                 kcsan_nestable_atomic_end();
386         } else                                    383         } else
387                 WRITE_ONCE(test_var, READ_ONCE    384                 WRITE_ONCE(test_var, READ_ONCE(test_var) ^ TEST_CHANGE_BITS);
388 }                                                 385 }
389                                                   386 
390 static noinline void test_kernel_assert_bits_c    387 static noinline void test_kernel_assert_bits_change(void)
391 {                                                 388 {
392         ASSERT_EXCLUSIVE_BITS(test_var, TEST_C    389         ASSERT_EXCLUSIVE_BITS(test_var, TEST_CHANGE_BITS);
393 }                                                 390 }
394                                                   391 
395 static noinline void test_kernel_assert_bits_n    392 static noinline void test_kernel_assert_bits_nochange(void)
396 {                                                 393 {
397         ASSERT_EXCLUSIVE_BITS(test_var, ~TEST_    394         ASSERT_EXCLUSIVE_BITS(test_var, ~TEST_CHANGE_BITS);
398 }                                                 395 }
399                                                   396 
400 /*                                                397 /*
401  * Scoped assertions do trigger anywhere in sc    398  * Scoped assertions do trigger anywhere in scope. However, the report should
402  * still only point at the start of the scope.    399  * still only point at the start of the scope.
403  */                                               400  */
404 static noinline void test_enter_scope(void)       401 static noinline void test_enter_scope(void)
405 {                                                 402 {
406         int x = 0;                                403         int x = 0;
407                                                   404 
408         /* Unrelated accesses to scoped assert    405         /* Unrelated accesses to scoped assert. */
409         READ_ONCE(test_sink);                     406         READ_ONCE(test_sink);
410         kcsan_check_read(&x, sizeof(x));          407         kcsan_check_read(&x, sizeof(x));
411 }                                                 408 }
412                                                   409 
413 static noinline void test_kernel_assert_writer    410 static noinline void test_kernel_assert_writer_scoped(void)
414 {                                                 411 {
415         ASSERT_EXCLUSIVE_WRITER_SCOPED(test_va    412         ASSERT_EXCLUSIVE_WRITER_SCOPED(test_var);
416         test_enter_scope();                       413         test_enter_scope();
417 }                                                 414 }
418                                                   415 
419 static noinline void test_kernel_assert_access    416 static noinline void test_kernel_assert_access_scoped(void)
420 {                                                 417 {
421         ASSERT_EXCLUSIVE_ACCESS_SCOPED(test_va    418         ASSERT_EXCLUSIVE_ACCESS_SCOPED(test_var);
422         test_enter_scope();                       419         test_enter_scope();
423 }                                                 420 }
424                                                   421 
425 static noinline void test_kernel_rmw_array(voi    422 static noinline void test_kernel_rmw_array(void)
426 {                                                 423 {
427         int i;                                    424         int i;
428                                                   425 
429         for (i = 0; i < ARRAY_SIZE(test_array)    426         for (i = 0; i < ARRAY_SIZE(test_array); ++i)
430                 test_array[i]++;                  427                 test_array[i]++;
431 }                                                 428 }
432                                                   429 
433 static noinline void test_kernel_write_struct(    430 static noinline void test_kernel_write_struct(void)
434 {                                                 431 {
435         kcsan_check_write(&test_struct, sizeof    432         kcsan_check_write(&test_struct, sizeof(test_struct));
436         kcsan_disable_current();                  433         kcsan_disable_current();
437         test_struct.val[3]++; /* induce value     434         test_struct.val[3]++; /* induce value change */
438         kcsan_enable_current();                   435         kcsan_enable_current();
439 }                                                 436 }
440                                                   437 
441 static noinline void test_kernel_write_struct_    438 static noinline void test_kernel_write_struct_part(void)
442 {                                                 439 {
443         test_struct.val[3] = 42;                  440         test_struct.val[3] = 42;
444 }                                                 441 }
445                                                   442 
446 static noinline void test_kernel_read_struct_z    443 static noinline void test_kernel_read_struct_zero_size(void)
447 {                                                 444 {
448         kcsan_check_read(&test_struct.val[3],     445         kcsan_check_read(&test_struct.val[3], 0);
449 }                                                 446 }
450                                                   447 
451 static noinline void test_kernel_jiffies_reade    448 static noinline void test_kernel_jiffies_reader(void)
452 {                                                 449 {
453         sink_value((long)jiffies);                450         sink_value((long)jiffies);
454 }                                                 451 }
455                                                   452 
456 static noinline void test_kernel_seqlock_reade    453 static noinline void test_kernel_seqlock_reader(void)
457 {                                                 454 {
458         unsigned int seq;                         455         unsigned int seq;
459                                                   456 
460         do {                                      457         do {
461                 seq = read_seqbegin(&test_seql    458                 seq = read_seqbegin(&test_seqlock);
462                 sink_value(test_var);             459                 sink_value(test_var);
463         } while (read_seqretry(&test_seqlock,     460         } while (read_seqretry(&test_seqlock, seq));
464 }                                                 461 }
465                                                   462 
466 static noinline void test_kernel_seqlock_write    463 static noinline void test_kernel_seqlock_writer(void)
467 {                                                 464 {
468         unsigned long flags;                      465         unsigned long flags;
469                                                   466 
470         write_seqlock_irqsave(&test_seqlock, f    467         write_seqlock_irqsave(&test_seqlock, flags);
471         test_var++;                               468         test_var++;
472         write_sequnlock_irqrestore(&test_seqlo    469         write_sequnlock_irqrestore(&test_seqlock, flags);
473 }                                                 470 }
474                                                   471 
475 static noinline void test_kernel_atomic_builti    472 static noinline void test_kernel_atomic_builtins(void)
476 {                                                 473 {
477         /*                                        474         /*
478          * Generate concurrent accesses, expec    475          * Generate concurrent accesses, expecting no reports, ensuring KCSAN
479          * treats builtin atomics as actually     476          * treats builtin atomics as actually atomic.
480          */                                       477          */
481         __atomic_load_n(&test_var, __ATOMIC_RE    478         __atomic_load_n(&test_var, __ATOMIC_RELAXED);
482 }                                                 479 }
483                                                   480 
484 static noinline void test_kernel_xor_1bit(void    481 static noinline void test_kernel_xor_1bit(void)
485 {                                                 482 {
486         /* Do not report data races between th    483         /* Do not report data races between the read-writes. */
487         kcsan_nestable_atomic_begin();            484         kcsan_nestable_atomic_begin();
488         test_var ^= 0x10000;                      485         test_var ^= 0x10000;
489         kcsan_nestable_atomic_end();              486         kcsan_nestable_atomic_end();
490 }                                                 487 }
491                                                   488 
492 #define TEST_KERNEL_LOCKED(name, acquire, rele    489 #define TEST_KERNEL_LOCKED(name, acquire, release)              \
493         static noinline void test_kernel_##nam    490         static noinline void test_kernel_##name(void)           \
494         {                                         491         {                                                       \
495                 long *flag = &test_struct.val[    492                 long *flag = &test_struct.val[0];               \
496                 long v = 0;                       493                 long v = 0;                                     \
497                 if (!(acquire))                   494                 if (!(acquire))                                 \
498                         return;                   495                         return;                                 \
499                 while (v++ < 100) {               496                 while (v++ < 100) {                             \
500                         test_var++;               497                         test_var++;                             \
501                         barrier();                498                         barrier();                              \
502                 }                                 499                 }                                               \
503                 release;                          500                 release;                                        \
504                 test_delay(10);                   501                 test_delay(10);                                 \
505         }                                         502         }
506                                                   503 
507 TEST_KERNEL_LOCKED(with_memorder,                 504 TEST_KERNEL_LOCKED(with_memorder,
508                    cmpxchg_acquire(flag, 0, 1)    505                    cmpxchg_acquire(flag, 0, 1) == 0,
509                    smp_store_release(flag, 0))    506                    smp_store_release(flag, 0));
510 TEST_KERNEL_LOCKED(wrong_memorder,                507 TEST_KERNEL_LOCKED(wrong_memorder,
511                    cmpxchg_relaxed(flag, 0, 1)    508                    cmpxchg_relaxed(flag, 0, 1) == 0,
512                    WRITE_ONCE(*flag, 0));         509                    WRITE_ONCE(*flag, 0));
513 TEST_KERNEL_LOCKED(atomic_builtin_with_memorde    510 TEST_KERNEL_LOCKED(atomic_builtin_with_memorder,
514                    __atomic_compare_exchange_n    511                    __atomic_compare_exchange_n(flag, &v, 1, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED),
515                    __atomic_store_n(flag, 0, _    512                    __atomic_store_n(flag, 0, __ATOMIC_RELEASE));
516 TEST_KERNEL_LOCKED(atomic_builtin_wrong_memord    513 TEST_KERNEL_LOCKED(atomic_builtin_wrong_memorder,
517                    __atomic_compare_exchange_n    514                    __atomic_compare_exchange_n(flag, &v, 1, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED),
518                    __atomic_store_n(flag, 0, _    515                    __atomic_store_n(flag, 0, __ATOMIC_RELAXED));
519                                                   516 
520 /* ===== Test cases ===== */                      517 /* ===== Test cases ===== */
521                                                   518 
522 /*                                                519 /*
523  * Tests that various barriers have the expect    520  * Tests that various barriers have the expected effect on internal state. Not
524  * exhaustive on atomic_t operations. Unlike t    521  * exhaustive on atomic_t operations. Unlike the selftest, also checks for
525  * too-strict barrier instrumentation; these c    522  * too-strict barrier instrumentation; these can be tolerated, because it does
526  * not cause false positives, but at least we     523  * not cause false positives, but at least we should be aware of such cases.
527  */                                               524  */
528 static void test_barrier_nothreads(struct kuni    525 static void test_barrier_nothreads(struct kunit *test)
529 {                                                 526 {
530 #ifdef CONFIG_KCSAN_WEAK_MEMORY                   527 #ifdef CONFIG_KCSAN_WEAK_MEMORY
531         struct kcsan_scoped_access *reorder_ac    528         struct kcsan_scoped_access *reorder_access = &current->kcsan_ctx.reorder_access;
532 #else                                             529 #else
533         struct kcsan_scoped_access *reorder_ac    530         struct kcsan_scoped_access *reorder_access = NULL;
534 #endif                                            531 #endif
535         arch_spinlock_t arch_spinlock = __ARCH    532         arch_spinlock_t arch_spinlock = __ARCH_SPIN_LOCK_UNLOCKED;
536         atomic_t dummy;                           533         atomic_t dummy;
537                                                   534 
538         KCSAN_TEST_REQUIRES(test, reorder_acce    535         KCSAN_TEST_REQUIRES(test, reorder_access != NULL);
539         KCSAN_TEST_REQUIRES(test, IS_ENABLED(C    536         KCSAN_TEST_REQUIRES(test, IS_ENABLED(CONFIG_SMP));
540                                                   537 
541 #define __KCSAN_EXPECT_BARRIER(access_type, ba    538 #define __KCSAN_EXPECT_BARRIER(access_type, barrier, order_before, name)                        \
542         do {                                      539         do {                                                                                    \
543                 reorder_access->type = (access    540                 reorder_access->type = (access_type) | KCSAN_ACCESS_SCOPED;                     \
544                 reorder_access->size = sizeof(    541                 reorder_access->size = sizeof(test_var);                                        \
545                 barrier;                          542                 barrier;                                                                        \
546                 KUNIT_EXPECT_EQ_MSG(test, reor    543                 KUNIT_EXPECT_EQ_MSG(test, reorder_access->size,                                 \
547                                     order_befo    544                                     order_before ? 0 : sizeof(test_var),                        \
548                                     "improperl    545                                     "improperly instrumented type=(" #access_type "): " name);  \
549         } while (0)                               546         } while (0)
550 #define KCSAN_EXPECT_READ_BARRIER(b, o)  __KCS    547 #define KCSAN_EXPECT_READ_BARRIER(b, o)  __KCSAN_EXPECT_BARRIER(0, b, o, #b)
551 #define KCSAN_EXPECT_WRITE_BARRIER(b, o) __KCS    548 #define KCSAN_EXPECT_WRITE_BARRIER(b, o) __KCSAN_EXPECT_BARRIER(KCSAN_ACCESS_WRITE, b, o, #b)
552 #define KCSAN_EXPECT_RW_BARRIER(b, o)    __KCS    549 #define KCSAN_EXPECT_RW_BARRIER(b, o)    __KCSAN_EXPECT_BARRIER(KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE, b, o, #b)
553                                                   550 
554         /*                                        551         /*
555          * Lockdep initialization can strength    552          * Lockdep initialization can strengthen certain locking operations due
556          * to calling into instrumented files;    553          * to calling into instrumented files; "warm up" our locks.
557          */                                       554          */
558         spin_lock(&test_spinlock);                555         spin_lock(&test_spinlock);
559         spin_unlock(&test_spinlock);              556         spin_unlock(&test_spinlock);
560         mutex_lock(&test_mutex);                  557         mutex_lock(&test_mutex);
561         mutex_unlock(&test_mutex);                558         mutex_unlock(&test_mutex);
562                                                   559 
563         /* Force creating a valid entry in reo    560         /* Force creating a valid entry in reorder_access first. */
564         test_var = 0;                             561         test_var = 0;
565         while (test_var++ < 1000000 && reorder    562         while (test_var++ < 1000000 && reorder_access->size != sizeof(test_var))
566                 __kcsan_check_read(&test_var,     563                 __kcsan_check_read(&test_var, sizeof(test_var));
567         KUNIT_ASSERT_EQ(test, reorder_access->    564         KUNIT_ASSERT_EQ(test, reorder_access->size, sizeof(test_var));
568                                                   565 
569         kcsan_nestable_atomic_begin(); /* No w    566         kcsan_nestable_atomic_begin(); /* No watchpoints in called functions. */
570                                                   567 
571         KCSAN_EXPECT_READ_BARRIER(mb(), true);    568         KCSAN_EXPECT_READ_BARRIER(mb(), true);
572         KCSAN_EXPECT_READ_BARRIER(wmb(), false    569         KCSAN_EXPECT_READ_BARRIER(wmb(), false);
573         KCSAN_EXPECT_READ_BARRIER(rmb(), true)    570         KCSAN_EXPECT_READ_BARRIER(rmb(), true);
574         KCSAN_EXPECT_READ_BARRIER(smp_mb(), tr    571         KCSAN_EXPECT_READ_BARRIER(smp_mb(), true);
575         KCSAN_EXPECT_READ_BARRIER(smp_wmb(), f    572         KCSAN_EXPECT_READ_BARRIER(smp_wmb(), false);
576         KCSAN_EXPECT_READ_BARRIER(smp_rmb(), t    573         KCSAN_EXPECT_READ_BARRIER(smp_rmb(), true);
577         KCSAN_EXPECT_READ_BARRIER(dma_wmb(), f    574         KCSAN_EXPECT_READ_BARRIER(dma_wmb(), false);
578         KCSAN_EXPECT_READ_BARRIER(dma_rmb(), t    575         KCSAN_EXPECT_READ_BARRIER(dma_rmb(), true);
579         KCSAN_EXPECT_READ_BARRIER(smp_mb__befo    576         KCSAN_EXPECT_READ_BARRIER(smp_mb__before_atomic(), true);
580         KCSAN_EXPECT_READ_BARRIER(smp_mb__afte    577         KCSAN_EXPECT_READ_BARRIER(smp_mb__after_atomic(), true);
581         KCSAN_EXPECT_READ_BARRIER(smp_mb__afte    578         KCSAN_EXPECT_READ_BARRIER(smp_mb__after_spinlock(), true);
582         KCSAN_EXPECT_READ_BARRIER(smp_store_mb    579         KCSAN_EXPECT_READ_BARRIER(smp_store_mb(test_var, 0), true);
583         KCSAN_EXPECT_READ_BARRIER(smp_load_acq    580         KCSAN_EXPECT_READ_BARRIER(smp_load_acquire(&test_var), false);
584         KCSAN_EXPECT_READ_BARRIER(smp_store_re    581         KCSAN_EXPECT_READ_BARRIER(smp_store_release(&test_var, 0), true);
585         KCSAN_EXPECT_READ_BARRIER(xchg(&test_v    582         KCSAN_EXPECT_READ_BARRIER(xchg(&test_var, 0), true);
586         KCSAN_EXPECT_READ_BARRIER(xchg_release    583         KCSAN_EXPECT_READ_BARRIER(xchg_release(&test_var, 0), true);
587         KCSAN_EXPECT_READ_BARRIER(xchg_relaxed    584         KCSAN_EXPECT_READ_BARRIER(xchg_relaxed(&test_var, 0), false);
588         KCSAN_EXPECT_READ_BARRIER(cmpxchg(&tes    585         KCSAN_EXPECT_READ_BARRIER(cmpxchg(&test_var, 0,  0), true);
589         KCSAN_EXPECT_READ_BARRIER(cmpxchg_rele    586         KCSAN_EXPECT_READ_BARRIER(cmpxchg_release(&test_var, 0,  0), true);
590         KCSAN_EXPECT_READ_BARRIER(cmpxchg_rela    587         KCSAN_EXPECT_READ_BARRIER(cmpxchg_relaxed(&test_var, 0,  0), false);
591         KCSAN_EXPECT_READ_BARRIER(atomic_read(    588         KCSAN_EXPECT_READ_BARRIER(atomic_read(&dummy), false);
592         KCSAN_EXPECT_READ_BARRIER(atomic_read_    589         KCSAN_EXPECT_READ_BARRIER(atomic_read_acquire(&dummy), false);
593         KCSAN_EXPECT_READ_BARRIER(atomic_set(&    590         KCSAN_EXPECT_READ_BARRIER(atomic_set(&dummy, 0), false);
594         KCSAN_EXPECT_READ_BARRIER(atomic_set_r    591         KCSAN_EXPECT_READ_BARRIER(atomic_set_release(&dummy, 0), true);
595         KCSAN_EXPECT_READ_BARRIER(atomic_add(1    592         KCSAN_EXPECT_READ_BARRIER(atomic_add(1, &dummy), false);
596         KCSAN_EXPECT_READ_BARRIER(atomic_add_r    593         KCSAN_EXPECT_READ_BARRIER(atomic_add_return(1, &dummy), true);
597         KCSAN_EXPECT_READ_BARRIER(atomic_add_r    594         KCSAN_EXPECT_READ_BARRIER(atomic_add_return_acquire(1, &dummy), false);
598         KCSAN_EXPECT_READ_BARRIER(atomic_add_r    595         KCSAN_EXPECT_READ_BARRIER(atomic_add_return_release(1, &dummy), true);
599         KCSAN_EXPECT_READ_BARRIER(atomic_add_r    596         KCSAN_EXPECT_READ_BARRIER(atomic_add_return_relaxed(1, &dummy), false);
600         KCSAN_EXPECT_READ_BARRIER(atomic_fetch    597         KCSAN_EXPECT_READ_BARRIER(atomic_fetch_add(1, &dummy), true);
601         KCSAN_EXPECT_READ_BARRIER(atomic_fetch    598         KCSAN_EXPECT_READ_BARRIER(atomic_fetch_add_acquire(1, &dummy), false);
602         KCSAN_EXPECT_READ_BARRIER(atomic_fetch    599         KCSAN_EXPECT_READ_BARRIER(atomic_fetch_add_release(1, &dummy), true);
603         KCSAN_EXPECT_READ_BARRIER(atomic_fetch    600         KCSAN_EXPECT_READ_BARRIER(atomic_fetch_add_relaxed(1, &dummy), false);
604         KCSAN_EXPECT_READ_BARRIER(test_and_set    601         KCSAN_EXPECT_READ_BARRIER(test_and_set_bit(0, &test_var), true);
605         KCSAN_EXPECT_READ_BARRIER(test_and_cle    602         KCSAN_EXPECT_READ_BARRIER(test_and_clear_bit(0, &test_var), true);
606         KCSAN_EXPECT_READ_BARRIER(test_and_cha    603         KCSAN_EXPECT_READ_BARRIER(test_and_change_bit(0, &test_var), true);
607         KCSAN_EXPECT_READ_BARRIER(clear_bit_un    604         KCSAN_EXPECT_READ_BARRIER(clear_bit_unlock(0, &test_var), true);
608         KCSAN_EXPECT_READ_BARRIER(__clear_bit_    605         KCSAN_EXPECT_READ_BARRIER(__clear_bit_unlock(0, &test_var), true);
609         KCSAN_EXPECT_READ_BARRIER(arch_spin_lo    606         KCSAN_EXPECT_READ_BARRIER(arch_spin_lock(&arch_spinlock), false);
610         KCSAN_EXPECT_READ_BARRIER(arch_spin_un    607         KCSAN_EXPECT_READ_BARRIER(arch_spin_unlock(&arch_spinlock), true);
611         KCSAN_EXPECT_READ_BARRIER(spin_lock(&t    608         KCSAN_EXPECT_READ_BARRIER(spin_lock(&test_spinlock), false);
612         KCSAN_EXPECT_READ_BARRIER(spin_unlock(    609         KCSAN_EXPECT_READ_BARRIER(spin_unlock(&test_spinlock), true);
613         KCSAN_EXPECT_READ_BARRIER(mutex_lock(&    610         KCSAN_EXPECT_READ_BARRIER(mutex_lock(&test_mutex), false);
614         KCSAN_EXPECT_READ_BARRIER(mutex_unlock    611         KCSAN_EXPECT_READ_BARRIER(mutex_unlock(&test_mutex), true);
615                                                   612 
616         KCSAN_EXPECT_WRITE_BARRIER(mb(), true)    613         KCSAN_EXPECT_WRITE_BARRIER(mb(), true);
617         KCSAN_EXPECT_WRITE_BARRIER(wmb(), true    614         KCSAN_EXPECT_WRITE_BARRIER(wmb(), true);
618         KCSAN_EXPECT_WRITE_BARRIER(rmb(), fals    615         KCSAN_EXPECT_WRITE_BARRIER(rmb(), false);
619         KCSAN_EXPECT_WRITE_BARRIER(smp_mb(), t    616         KCSAN_EXPECT_WRITE_BARRIER(smp_mb(), true);
620         KCSAN_EXPECT_WRITE_BARRIER(smp_wmb(),     617         KCSAN_EXPECT_WRITE_BARRIER(smp_wmb(), true);
621         KCSAN_EXPECT_WRITE_BARRIER(smp_rmb(),     618         KCSAN_EXPECT_WRITE_BARRIER(smp_rmb(), false);
622         KCSAN_EXPECT_WRITE_BARRIER(dma_wmb(),     619         KCSAN_EXPECT_WRITE_BARRIER(dma_wmb(), true);
623         KCSAN_EXPECT_WRITE_BARRIER(dma_rmb(),     620         KCSAN_EXPECT_WRITE_BARRIER(dma_rmb(), false);
624         KCSAN_EXPECT_WRITE_BARRIER(smp_mb__bef    621         KCSAN_EXPECT_WRITE_BARRIER(smp_mb__before_atomic(), true);
625         KCSAN_EXPECT_WRITE_BARRIER(smp_mb__aft    622         KCSAN_EXPECT_WRITE_BARRIER(smp_mb__after_atomic(), true);
626         KCSAN_EXPECT_WRITE_BARRIER(smp_mb__aft    623         KCSAN_EXPECT_WRITE_BARRIER(smp_mb__after_spinlock(), true);
627         KCSAN_EXPECT_WRITE_BARRIER(smp_store_m    624         KCSAN_EXPECT_WRITE_BARRIER(smp_store_mb(test_var, 0), true);
628         KCSAN_EXPECT_WRITE_BARRIER(smp_load_ac    625         KCSAN_EXPECT_WRITE_BARRIER(smp_load_acquire(&test_var), false);
629         KCSAN_EXPECT_WRITE_BARRIER(smp_store_r    626         KCSAN_EXPECT_WRITE_BARRIER(smp_store_release(&test_var, 0), true);
630         KCSAN_EXPECT_WRITE_BARRIER(xchg(&test_    627         KCSAN_EXPECT_WRITE_BARRIER(xchg(&test_var, 0), true);
631         KCSAN_EXPECT_WRITE_BARRIER(xchg_releas    628         KCSAN_EXPECT_WRITE_BARRIER(xchg_release(&test_var, 0), true);
632         KCSAN_EXPECT_WRITE_BARRIER(xchg_relaxe    629         KCSAN_EXPECT_WRITE_BARRIER(xchg_relaxed(&test_var, 0), false);
633         KCSAN_EXPECT_WRITE_BARRIER(cmpxchg(&te    630         KCSAN_EXPECT_WRITE_BARRIER(cmpxchg(&test_var, 0,  0), true);
634         KCSAN_EXPECT_WRITE_BARRIER(cmpxchg_rel    631         KCSAN_EXPECT_WRITE_BARRIER(cmpxchg_release(&test_var, 0,  0), true);
635         KCSAN_EXPECT_WRITE_BARRIER(cmpxchg_rel    632         KCSAN_EXPECT_WRITE_BARRIER(cmpxchg_relaxed(&test_var, 0,  0), false);
636         KCSAN_EXPECT_WRITE_BARRIER(atomic_read    633         KCSAN_EXPECT_WRITE_BARRIER(atomic_read(&dummy), false);
637         KCSAN_EXPECT_WRITE_BARRIER(atomic_read    634         KCSAN_EXPECT_WRITE_BARRIER(atomic_read_acquire(&dummy), false);
638         KCSAN_EXPECT_WRITE_BARRIER(atomic_set(    635         KCSAN_EXPECT_WRITE_BARRIER(atomic_set(&dummy, 0), false);
639         KCSAN_EXPECT_WRITE_BARRIER(atomic_set_    636         KCSAN_EXPECT_WRITE_BARRIER(atomic_set_release(&dummy, 0), true);
640         KCSAN_EXPECT_WRITE_BARRIER(atomic_add(    637         KCSAN_EXPECT_WRITE_BARRIER(atomic_add(1, &dummy), false);
641         KCSAN_EXPECT_WRITE_BARRIER(atomic_add_    638         KCSAN_EXPECT_WRITE_BARRIER(atomic_add_return(1, &dummy), true);
642         KCSAN_EXPECT_WRITE_BARRIER(atomic_add_    639         KCSAN_EXPECT_WRITE_BARRIER(atomic_add_return_acquire(1, &dummy), false);
643         KCSAN_EXPECT_WRITE_BARRIER(atomic_add_    640         KCSAN_EXPECT_WRITE_BARRIER(atomic_add_return_release(1, &dummy), true);
644         KCSAN_EXPECT_WRITE_BARRIER(atomic_add_    641         KCSAN_EXPECT_WRITE_BARRIER(atomic_add_return_relaxed(1, &dummy), false);
645         KCSAN_EXPECT_WRITE_BARRIER(atomic_fetc    642         KCSAN_EXPECT_WRITE_BARRIER(atomic_fetch_add(1, &dummy), true);
646         KCSAN_EXPECT_WRITE_BARRIER(atomic_fetc    643         KCSAN_EXPECT_WRITE_BARRIER(atomic_fetch_add_acquire(1, &dummy), false);
647         KCSAN_EXPECT_WRITE_BARRIER(atomic_fetc    644         KCSAN_EXPECT_WRITE_BARRIER(atomic_fetch_add_release(1, &dummy), true);
648         KCSAN_EXPECT_WRITE_BARRIER(atomic_fetc    645         KCSAN_EXPECT_WRITE_BARRIER(atomic_fetch_add_relaxed(1, &dummy), false);
649         KCSAN_EXPECT_WRITE_BARRIER(test_and_se    646         KCSAN_EXPECT_WRITE_BARRIER(test_and_set_bit(0, &test_var), true);
650         KCSAN_EXPECT_WRITE_BARRIER(test_and_cl    647         KCSAN_EXPECT_WRITE_BARRIER(test_and_clear_bit(0, &test_var), true);
651         KCSAN_EXPECT_WRITE_BARRIER(test_and_ch    648         KCSAN_EXPECT_WRITE_BARRIER(test_and_change_bit(0, &test_var), true);
652         KCSAN_EXPECT_WRITE_BARRIER(clear_bit_u    649         KCSAN_EXPECT_WRITE_BARRIER(clear_bit_unlock(0, &test_var), true);
653         KCSAN_EXPECT_WRITE_BARRIER(__clear_bit    650         KCSAN_EXPECT_WRITE_BARRIER(__clear_bit_unlock(0, &test_var), true);
654         KCSAN_EXPECT_WRITE_BARRIER(arch_spin_l    651         KCSAN_EXPECT_WRITE_BARRIER(arch_spin_lock(&arch_spinlock), false);
655         KCSAN_EXPECT_WRITE_BARRIER(arch_spin_u    652         KCSAN_EXPECT_WRITE_BARRIER(arch_spin_unlock(&arch_spinlock), true);
656         KCSAN_EXPECT_WRITE_BARRIER(spin_lock(&    653         KCSAN_EXPECT_WRITE_BARRIER(spin_lock(&test_spinlock), false);
657         KCSAN_EXPECT_WRITE_BARRIER(spin_unlock    654         KCSAN_EXPECT_WRITE_BARRIER(spin_unlock(&test_spinlock), true);
658         KCSAN_EXPECT_WRITE_BARRIER(mutex_lock(    655         KCSAN_EXPECT_WRITE_BARRIER(mutex_lock(&test_mutex), false);
659         KCSAN_EXPECT_WRITE_BARRIER(mutex_unloc    656         KCSAN_EXPECT_WRITE_BARRIER(mutex_unlock(&test_mutex), true);
660                                                   657 
661         KCSAN_EXPECT_RW_BARRIER(mb(), true);      658         KCSAN_EXPECT_RW_BARRIER(mb(), true);
662         KCSAN_EXPECT_RW_BARRIER(wmb(), true);     659         KCSAN_EXPECT_RW_BARRIER(wmb(), true);
663         KCSAN_EXPECT_RW_BARRIER(rmb(), true);     660         KCSAN_EXPECT_RW_BARRIER(rmb(), true);
664         KCSAN_EXPECT_RW_BARRIER(smp_mb(), true    661         KCSAN_EXPECT_RW_BARRIER(smp_mb(), true);
665         KCSAN_EXPECT_RW_BARRIER(smp_wmb(), tru    662         KCSAN_EXPECT_RW_BARRIER(smp_wmb(), true);
666         KCSAN_EXPECT_RW_BARRIER(smp_rmb(), tru    663         KCSAN_EXPECT_RW_BARRIER(smp_rmb(), true);
667         KCSAN_EXPECT_RW_BARRIER(dma_wmb(), tru    664         KCSAN_EXPECT_RW_BARRIER(dma_wmb(), true);
668         KCSAN_EXPECT_RW_BARRIER(dma_rmb(), tru    665         KCSAN_EXPECT_RW_BARRIER(dma_rmb(), true);
669         KCSAN_EXPECT_RW_BARRIER(smp_mb__before    666         KCSAN_EXPECT_RW_BARRIER(smp_mb__before_atomic(), true);
670         KCSAN_EXPECT_RW_BARRIER(smp_mb__after_    667         KCSAN_EXPECT_RW_BARRIER(smp_mb__after_atomic(), true);
671         KCSAN_EXPECT_RW_BARRIER(smp_mb__after_    668         KCSAN_EXPECT_RW_BARRIER(smp_mb__after_spinlock(), true);
672         KCSAN_EXPECT_RW_BARRIER(smp_store_mb(t    669         KCSAN_EXPECT_RW_BARRIER(smp_store_mb(test_var, 0), true);
673         KCSAN_EXPECT_RW_BARRIER(smp_load_acqui    670         KCSAN_EXPECT_RW_BARRIER(smp_load_acquire(&test_var), false);
674         KCSAN_EXPECT_RW_BARRIER(smp_store_rele    671         KCSAN_EXPECT_RW_BARRIER(smp_store_release(&test_var, 0), true);
675         KCSAN_EXPECT_RW_BARRIER(xchg(&test_var    672         KCSAN_EXPECT_RW_BARRIER(xchg(&test_var, 0), true);
676         KCSAN_EXPECT_RW_BARRIER(xchg_release(&    673         KCSAN_EXPECT_RW_BARRIER(xchg_release(&test_var, 0), true);
677         KCSAN_EXPECT_RW_BARRIER(xchg_relaxed(&    674         KCSAN_EXPECT_RW_BARRIER(xchg_relaxed(&test_var, 0), false);
678         KCSAN_EXPECT_RW_BARRIER(cmpxchg(&test_    675         KCSAN_EXPECT_RW_BARRIER(cmpxchg(&test_var, 0,  0), true);
679         KCSAN_EXPECT_RW_BARRIER(cmpxchg_releas    676         KCSAN_EXPECT_RW_BARRIER(cmpxchg_release(&test_var, 0,  0), true);
680         KCSAN_EXPECT_RW_BARRIER(cmpxchg_relaxe    677         KCSAN_EXPECT_RW_BARRIER(cmpxchg_relaxed(&test_var, 0,  0), false);
681         KCSAN_EXPECT_RW_BARRIER(atomic_read(&d    678         KCSAN_EXPECT_RW_BARRIER(atomic_read(&dummy), false);
682         KCSAN_EXPECT_RW_BARRIER(atomic_read_ac    679         KCSAN_EXPECT_RW_BARRIER(atomic_read_acquire(&dummy), false);
683         KCSAN_EXPECT_RW_BARRIER(atomic_set(&du    680         KCSAN_EXPECT_RW_BARRIER(atomic_set(&dummy, 0), false);
684         KCSAN_EXPECT_RW_BARRIER(atomic_set_rel    681         KCSAN_EXPECT_RW_BARRIER(atomic_set_release(&dummy, 0), true);
685         KCSAN_EXPECT_RW_BARRIER(atomic_add(1,     682         KCSAN_EXPECT_RW_BARRIER(atomic_add(1, &dummy), false);
686         KCSAN_EXPECT_RW_BARRIER(atomic_add_ret    683         KCSAN_EXPECT_RW_BARRIER(atomic_add_return(1, &dummy), true);
687         KCSAN_EXPECT_RW_BARRIER(atomic_add_ret    684         KCSAN_EXPECT_RW_BARRIER(atomic_add_return_acquire(1, &dummy), false);
688         KCSAN_EXPECT_RW_BARRIER(atomic_add_ret    685         KCSAN_EXPECT_RW_BARRIER(atomic_add_return_release(1, &dummy), true);
689         KCSAN_EXPECT_RW_BARRIER(atomic_add_ret    686         KCSAN_EXPECT_RW_BARRIER(atomic_add_return_relaxed(1, &dummy), false);
690         KCSAN_EXPECT_RW_BARRIER(atomic_fetch_a    687         KCSAN_EXPECT_RW_BARRIER(atomic_fetch_add(1, &dummy), true);
691         KCSAN_EXPECT_RW_BARRIER(atomic_fetch_a    688         KCSAN_EXPECT_RW_BARRIER(atomic_fetch_add_acquire(1, &dummy), false);
692         KCSAN_EXPECT_RW_BARRIER(atomic_fetch_a    689         KCSAN_EXPECT_RW_BARRIER(atomic_fetch_add_release(1, &dummy), true);
693         KCSAN_EXPECT_RW_BARRIER(atomic_fetch_a    690         KCSAN_EXPECT_RW_BARRIER(atomic_fetch_add_relaxed(1, &dummy), false);
694         KCSAN_EXPECT_RW_BARRIER(test_and_set_b    691         KCSAN_EXPECT_RW_BARRIER(test_and_set_bit(0, &test_var), true);
695         KCSAN_EXPECT_RW_BARRIER(test_and_clear    692         KCSAN_EXPECT_RW_BARRIER(test_and_clear_bit(0, &test_var), true);
696         KCSAN_EXPECT_RW_BARRIER(test_and_chang    693         KCSAN_EXPECT_RW_BARRIER(test_and_change_bit(0, &test_var), true);
697         KCSAN_EXPECT_RW_BARRIER(clear_bit_unlo    694         KCSAN_EXPECT_RW_BARRIER(clear_bit_unlock(0, &test_var), true);
698         KCSAN_EXPECT_RW_BARRIER(__clear_bit_un    695         KCSAN_EXPECT_RW_BARRIER(__clear_bit_unlock(0, &test_var), true);
699         KCSAN_EXPECT_RW_BARRIER(arch_spin_lock    696         KCSAN_EXPECT_RW_BARRIER(arch_spin_lock(&arch_spinlock), false);
700         KCSAN_EXPECT_RW_BARRIER(arch_spin_unlo    697         KCSAN_EXPECT_RW_BARRIER(arch_spin_unlock(&arch_spinlock), true);
701         KCSAN_EXPECT_RW_BARRIER(spin_lock(&tes    698         KCSAN_EXPECT_RW_BARRIER(spin_lock(&test_spinlock), false);
702         KCSAN_EXPECT_RW_BARRIER(spin_unlock(&t    699         KCSAN_EXPECT_RW_BARRIER(spin_unlock(&test_spinlock), true);
703         KCSAN_EXPECT_RW_BARRIER(mutex_lock(&te    700         KCSAN_EXPECT_RW_BARRIER(mutex_lock(&test_mutex), false);
704         KCSAN_EXPECT_RW_BARRIER(mutex_unlock(&    701         KCSAN_EXPECT_RW_BARRIER(mutex_unlock(&test_mutex), true);
705         KCSAN_EXPECT_READ_BARRIER(xor_unlock_i    702         KCSAN_EXPECT_READ_BARRIER(xor_unlock_is_negative_byte(1, &test_var), true);
706         KCSAN_EXPECT_WRITE_BARRIER(xor_unlock_    703         KCSAN_EXPECT_WRITE_BARRIER(xor_unlock_is_negative_byte(1, &test_var), true);
707         KCSAN_EXPECT_RW_BARRIER(xor_unlock_is_    704         KCSAN_EXPECT_RW_BARRIER(xor_unlock_is_negative_byte(1, &test_var), true);
708         kcsan_nestable_atomic_end();              705         kcsan_nestable_atomic_end();
709 }                                                 706 }
710                                                   707 
711 /* Simple test with normal data race. */          708 /* Simple test with normal data race. */
712 __no_kcsan                                        709 __no_kcsan
713 static void test_basic(struct kunit *test)        710 static void test_basic(struct kunit *test)
714 {                                                 711 {
715         struct expect_report expect = {           712         struct expect_report expect = {
716                 .access = {                       713                 .access = {
717                         { test_kernel_write, &    714                         { test_kernel_write, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
718                         { test_kernel_read, &t    715                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
719                 },                                716                 },
720         };                                        717         };
721         struct expect_report never = {            718         struct expect_report never = {
722                 .access = {                       719                 .access = {
723                         { test_kernel_read, &t    720                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
724                         { test_kernel_read, &t    721                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
725                 },                                722                 },
726         };                                        723         };
727         bool match_expect = false;                724         bool match_expect = false;
728         bool match_never = false;                 725         bool match_never = false;
729                                                   726 
730         begin_test_checks(test_kernel_write, t    727         begin_test_checks(test_kernel_write, test_kernel_read);
731         do {                                      728         do {
732                 match_expect |= report_matches    729                 match_expect |= report_matches(&expect);
733                 match_never = report_matches(&    730                 match_never = report_matches(&never);
734         } while (!end_test_checks(match_never)    731         } while (!end_test_checks(match_never));
735         KUNIT_EXPECT_TRUE(test, match_expect);    732         KUNIT_EXPECT_TRUE(test, match_expect);
736         KUNIT_EXPECT_FALSE(test, match_never);    733         KUNIT_EXPECT_FALSE(test, match_never);
737 }                                                 734 }
738                                                   735 
739 /*                                                736 /*
740  * Stress KCSAN with lots of concurrent races     737  * Stress KCSAN with lots of concurrent races on different addresses until
741  * timeout.                                       738  * timeout.
742  */                                               739  */
743 __no_kcsan                                        740 __no_kcsan
744 static void test_concurrent_races(struct kunit    741 static void test_concurrent_races(struct kunit *test)
745 {                                                 742 {
746         struct expect_report expect = {           743         struct expect_report expect = {
747                 .access = {                       744                 .access = {
748                         /* NULL will match any    745                         /* NULL will match any address. */
749                         { test_kernel_rmw_arra    746                         { test_kernel_rmw_array, NULL, 0, __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
750                         { test_kernel_rmw_arra    747                         { test_kernel_rmw_array, NULL, 0, __KCSAN_ACCESS_RW(0) },
751                 },                                748                 },
752         };                                        749         };
753         struct expect_report never = {            750         struct expect_report never = {
754                 .access = {                       751                 .access = {
755                         { test_kernel_rmw_arra    752                         { test_kernel_rmw_array, NULL, 0, 0 },
756                         { test_kernel_rmw_arra    753                         { test_kernel_rmw_array, NULL, 0, 0 },
757                 },                                754                 },
758         };                                        755         };
759         bool match_expect = false;                756         bool match_expect = false;
760         bool match_never = false;                 757         bool match_never = false;
761                                                   758 
762         begin_test_checks(test_kernel_rmw_arra    759         begin_test_checks(test_kernel_rmw_array, test_kernel_rmw_array);
763         do {                                      760         do {
764                 match_expect |= report_matches    761                 match_expect |= report_matches(&expect);
765                 match_never |= report_matches(    762                 match_never |= report_matches(&never);
766         } while (!end_test_checks(false));        763         } while (!end_test_checks(false));
767         KUNIT_EXPECT_TRUE(test, match_expect);    764         KUNIT_EXPECT_TRUE(test, match_expect); /* Sanity check matches exist. */
768         KUNIT_EXPECT_FALSE(test, match_never);    765         KUNIT_EXPECT_FALSE(test, match_never);
769 }                                                 766 }
770                                                   767 
771 /* Test the KCSAN_REPORT_VALUE_CHANGE_ONLY opt    768 /* Test the KCSAN_REPORT_VALUE_CHANGE_ONLY option. */
772 __no_kcsan                                        769 __no_kcsan
773 static void test_novalue_change(struct kunit *    770 static void test_novalue_change(struct kunit *test)
774 {                                                 771 {
775         struct expect_report expect_rw = {        772         struct expect_report expect_rw = {
776                 .access = {                       773                 .access = {
777                         { test_kernel_write_no    774                         { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
778                         { test_kernel_read, &t    775                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
779                 },                                776                 },
780         };                                        777         };
781         struct expect_report expect_ww = {        778         struct expect_report expect_ww = {
782                 .access = {                       779                 .access = {
783                         { test_kernel_write_no    780                         { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
784                         { test_kernel_write_no    781                         { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
785                 },                                782                 },
786         };                                        783         };
787         bool match_expect = false;                784         bool match_expect = false;
788                                                   785 
789         test_kernel_write_nochange(); /* Reset    786         test_kernel_write_nochange(); /* Reset value. */
790         begin_test_checks(test_kernel_write_no    787         begin_test_checks(test_kernel_write_nochange, test_kernel_read);
791         do {                                      788         do {
792                 match_expect = report_matches(    789                 match_expect = report_matches(&expect_rw) || report_matches(&expect_ww);
793         } while (!end_test_checks(match_expect    790         } while (!end_test_checks(match_expect));
794         if (IS_ENABLED(CONFIG_KCSAN_REPORT_VAL    791         if (IS_ENABLED(CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY))
795                 KUNIT_EXPECT_FALSE(test, match    792                 KUNIT_EXPECT_FALSE(test, match_expect);
796         else                                      793         else
797                 KUNIT_EXPECT_TRUE(test, match_    794                 KUNIT_EXPECT_TRUE(test, match_expect);
798 }                                                 795 }
799                                                   796 
800 /*                                                797 /*
801  * Test that the rules where the KCSAN_REPORT_    798  * Test that the rules where the KCSAN_REPORT_VALUE_CHANGE_ONLY option should
802  * never apply work.                              799  * never apply work.
803  */                                               800  */
804 __no_kcsan                                        801 __no_kcsan
805 static void test_novalue_change_exception(stru    802 static void test_novalue_change_exception(struct kunit *test)
806 {                                                 803 {
807         struct expect_report expect_rw = {        804         struct expect_report expect_rw = {
808                 .access = {                       805                 .access = {
809                         { test_kernel_write_no    806                         { test_kernel_write_nochange_rcu, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
810                         { test_kernel_read, &t    807                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
811                 },                                808                 },
812         };                                        809         };
813         struct expect_report expect_ww = {        810         struct expect_report expect_ww = {
814                 .access = {                       811                 .access = {
815                         { test_kernel_write_no    812                         { test_kernel_write_nochange_rcu, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
816                         { test_kernel_write_no    813                         { test_kernel_write_nochange_rcu, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
817                 },                                814                 },
818         };                                        815         };
819         bool match_expect = false;                816         bool match_expect = false;
820                                                   817 
821         test_kernel_write_nochange_rcu(); /* R    818         test_kernel_write_nochange_rcu(); /* Reset value. */
822         begin_test_checks(test_kernel_write_no    819         begin_test_checks(test_kernel_write_nochange_rcu, test_kernel_read);
823         do {                                      820         do {
824                 match_expect = report_matches(    821                 match_expect = report_matches(&expect_rw) || report_matches(&expect_ww);
825         } while (!end_test_checks(match_expect    822         } while (!end_test_checks(match_expect));
826         KUNIT_EXPECT_TRUE(test, match_expect);    823         KUNIT_EXPECT_TRUE(test, match_expect);
827 }                                                 824 }
828                                                   825 
829 /* Test that data races of unknown origin are     826 /* Test that data races of unknown origin are reported. */
830 __no_kcsan                                        827 __no_kcsan
831 static void test_unknown_origin(struct kunit *    828 static void test_unknown_origin(struct kunit *test)
832 {                                                 829 {
833         struct expect_report expect = {           830         struct expect_report expect = {
834                 .access = {                       831                 .access = {
835                         { test_kernel_read, &t    832                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
836                         { NULL },                 833                         { NULL },
837                 },                                834                 },
838         };                                        835         };
839         bool match_expect = false;                836         bool match_expect = false;
840                                                   837 
841         begin_test_checks(test_kernel_write_un    838         begin_test_checks(test_kernel_write_uninstrumented, test_kernel_read);
842         do {                                      839         do {
843                 match_expect = report_matches(    840                 match_expect = report_matches(&expect);
844         } while (!end_test_checks(match_expect    841         } while (!end_test_checks(match_expect));
845         if (IS_ENABLED(CONFIG_KCSAN_REPORT_RAC    842         if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN))
846                 KUNIT_EXPECT_TRUE(test, match_    843                 KUNIT_EXPECT_TRUE(test, match_expect);
847         else                                      844         else
848                 KUNIT_EXPECT_FALSE(test, match    845                 KUNIT_EXPECT_FALSE(test, match_expect);
849 }                                                 846 }
850                                                   847 
851 /* Test KCSAN_ASSUME_PLAIN_WRITES_ATOMIC if it    848 /* Test KCSAN_ASSUME_PLAIN_WRITES_ATOMIC if it is selected. */
852 __no_kcsan                                        849 __no_kcsan
853 static void test_write_write_assume_atomic(str    850 static void test_write_write_assume_atomic(struct kunit *test)
854 {                                                 851 {
855         struct expect_report expect = {           852         struct expect_report expect = {
856                 .access = {                       853                 .access = {
857                         { test_kernel_write, &    854                         { test_kernel_write, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
858                         { test_kernel_write, &    855                         { test_kernel_write, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
859                 },                                856                 },
860         };                                        857         };
861         bool match_expect = false;                858         bool match_expect = false;
862                                                   859 
863         begin_test_checks(test_kernel_write, t    860         begin_test_checks(test_kernel_write, test_kernel_write);
864         do {                                      861         do {
865                 sink_value(READ_ONCE(test_var)    862                 sink_value(READ_ONCE(test_var)); /* induce value-change */
866                 match_expect = report_matches(    863                 match_expect = report_matches(&expect);
867         } while (!end_test_checks(match_expect    864         } while (!end_test_checks(match_expect));
868         if (IS_ENABLED(CONFIG_KCSAN_ASSUME_PLA    865         if (IS_ENABLED(CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC))
869                 KUNIT_EXPECT_FALSE(test, match    866                 KUNIT_EXPECT_FALSE(test, match_expect);
870         else                                      867         else
871                 KUNIT_EXPECT_TRUE(test, match_    868                 KUNIT_EXPECT_TRUE(test, match_expect);
872 }                                                 869 }
873                                                   870 
874 /*                                                871 /*
875  * Test that data races with writes larger tha    872  * Test that data races with writes larger than word-size are always reported,
876  * even if KCSAN_ASSUME_PLAIN_WRITES_ATOMIC is    873  * even if KCSAN_ASSUME_PLAIN_WRITES_ATOMIC is selected.
877  */                                               874  */
878 __no_kcsan                                        875 __no_kcsan
879 static void test_write_write_struct(struct kun    876 static void test_write_write_struct(struct kunit *test)
880 {                                                 877 {
881         struct expect_report expect = {           878         struct expect_report expect = {
882                 .access = {                       879                 .access = {
883                         { test_kernel_write_st    880                         { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
884                         { test_kernel_write_st    881                         { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
885                 },                                882                 },
886         };                                        883         };
887         bool match_expect = false;                884         bool match_expect = false;
888                                                   885 
889         begin_test_checks(test_kernel_write_st    886         begin_test_checks(test_kernel_write_struct, test_kernel_write_struct);
890         do {                                      887         do {
891                 match_expect = report_matches(    888                 match_expect = report_matches(&expect);
892         } while (!end_test_checks(match_expect    889         } while (!end_test_checks(match_expect));
893         KUNIT_EXPECT_TRUE(test, match_expect);    890         KUNIT_EXPECT_TRUE(test, match_expect);
894 }                                                 891 }
895                                                   892 
896 /*                                                893 /*
897  * Test that data races where only one write i    894  * Test that data races where only one write is larger than word-size are always
898  * reported, even if KCSAN_ASSUME_PLAIN_WRITES    895  * reported, even if KCSAN_ASSUME_PLAIN_WRITES_ATOMIC is selected.
899  */                                               896  */
900 __no_kcsan                                        897 __no_kcsan
901 static void test_write_write_struct_part(struc    898 static void test_write_write_struct_part(struct kunit *test)
902 {                                                 899 {
903         struct expect_report expect = {           900         struct expect_report expect = {
904                 .access = {                       901                 .access = {
905                         { test_kernel_write_st    902                         { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
906                         { test_kernel_write_st    903                         { test_kernel_write_struct_part, &test_struct.val[3], sizeof(test_struct.val[3]), KCSAN_ACCESS_WRITE },
907                 },                                904                 },
908         };                                        905         };
909         bool match_expect = false;                906         bool match_expect = false;
910                                                   907 
911         begin_test_checks(test_kernel_write_st    908         begin_test_checks(test_kernel_write_struct, test_kernel_write_struct_part);
912         do {                                      909         do {
913                 match_expect = report_matches(    910                 match_expect = report_matches(&expect);
914         } while (!end_test_checks(match_expect    911         } while (!end_test_checks(match_expect));
915         KUNIT_EXPECT_TRUE(test, match_expect);    912         KUNIT_EXPECT_TRUE(test, match_expect);
916 }                                                 913 }
917                                                   914 
918 /* Test that races with atomic accesses never     915 /* Test that races with atomic accesses never result in reports. */
919 __no_kcsan                                        916 __no_kcsan
920 static void test_read_atomic_write_atomic(stru    917 static void test_read_atomic_write_atomic(struct kunit *test)
921 {                                                 918 {
922         bool match_never = false;                 919         bool match_never = false;
923                                                   920 
924         begin_test_checks(test_kernel_read_ato    921         begin_test_checks(test_kernel_read_atomic, test_kernel_write_atomic);
925         do {                                      922         do {
926                 match_never = report_available    923                 match_never = report_available();
927         } while (!end_test_checks(match_never)    924         } while (!end_test_checks(match_never));
928         KUNIT_EXPECT_FALSE(test, match_never);    925         KUNIT_EXPECT_FALSE(test, match_never);
929 }                                                 926 }
930                                                   927 
931 /* Test that a race with an atomic and plain a    928 /* Test that a race with an atomic and plain access result in reports. */
932 __no_kcsan                                        929 __no_kcsan
933 static void test_read_plain_atomic_write(struc    930 static void test_read_plain_atomic_write(struct kunit *test)
934 {                                                 931 {
935         struct expect_report expect = {           932         struct expect_report expect = {
936                 .access = {                       933                 .access = {
937                         { test_kernel_read, &t    934                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
938                         { test_kernel_write_at    935                         { test_kernel_write_atomic, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC },
939                 },                                936                 },
940         };                                        937         };
941         bool match_expect = false;                938         bool match_expect = false;
942                                                   939 
943         KCSAN_TEST_REQUIRES(test, !IS_ENABLED(    940         KCSAN_TEST_REQUIRES(test, !IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS));
944                                                   941 
945         begin_test_checks(test_kernel_read, te    942         begin_test_checks(test_kernel_read, test_kernel_write_atomic);
946         do {                                      943         do {
947                 match_expect = report_matches(    944                 match_expect = report_matches(&expect);
948         } while (!end_test_checks(match_expect    945         } while (!end_test_checks(match_expect));
949         KUNIT_EXPECT_TRUE(test, match_expect);    946         KUNIT_EXPECT_TRUE(test, match_expect);
950 }                                                 947 }
951                                                   948 
952 /* Test that atomic RMWs generate correct repo    949 /* Test that atomic RMWs generate correct report. */
953 __no_kcsan                                        950 __no_kcsan
954 static void test_read_plain_atomic_rmw(struct     951 static void test_read_plain_atomic_rmw(struct kunit *test)
955 {                                                 952 {
956         struct expect_report expect = {           953         struct expect_report expect = {
957                 .access = {                       954                 .access = {
958                         { test_kernel_read, &t    955                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
959                         { test_kernel_atomic_r    956                         { test_kernel_atomic_rmw, &test_var, sizeof(test_var),
960                                 KCSAN_ACCESS_C    957                                 KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ATOMIC },
961                 },                                958                 },
962         };                                        959         };
963         bool match_expect = false;                960         bool match_expect = false;
964                                                   961 
965         KCSAN_TEST_REQUIRES(test, !IS_ENABLED(    962         KCSAN_TEST_REQUIRES(test, !IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS));
966                                                   963 
967         begin_test_checks(test_kernel_read, te    964         begin_test_checks(test_kernel_read, test_kernel_atomic_rmw);
968         do {                                      965         do {
969                 match_expect = report_matches(    966                 match_expect = report_matches(&expect);
970         } while (!end_test_checks(match_expect    967         } while (!end_test_checks(match_expect));
971         KUNIT_EXPECT_TRUE(test, match_expect);    968         KUNIT_EXPECT_TRUE(test, match_expect);
972 }                                                 969 }
973                                                   970 
974 /* Zero-sized accesses should never cause data    971 /* Zero-sized accesses should never cause data race reports. */
975 __no_kcsan                                        972 __no_kcsan
976 static void test_zero_size_access(struct kunit    973 static void test_zero_size_access(struct kunit *test)
977 {                                                 974 {
978         struct expect_report expect = {           975         struct expect_report expect = {
979                 .access = {                       976                 .access = {
980                         { test_kernel_write_st    977                         { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
981                         { test_kernel_write_st    978                         { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
982                 },                                979                 },
983         };                                        980         };
984         struct expect_report never = {            981         struct expect_report never = {
985                 .access = {                       982                 .access = {
986                         { test_kernel_write_st    983                         { test_kernel_write_struct, &test_struct, sizeof(test_struct), KCSAN_ACCESS_WRITE },
987                         { test_kernel_read_str    984                         { test_kernel_read_struct_zero_size, &test_struct.val[3], 0, 0 },
988                 },                                985                 },
989         };                                        986         };
990         bool match_expect = false;                987         bool match_expect = false;
991         bool match_never = false;                 988         bool match_never = false;
992                                                   989 
993         begin_test_checks(test_kernel_write_st    990         begin_test_checks(test_kernel_write_struct, test_kernel_read_struct_zero_size);
994         do {                                      991         do {
995                 match_expect |= report_matches    992                 match_expect |= report_matches(&expect);
996                 match_never = report_matches(&    993                 match_never = report_matches(&never);
997         } while (!end_test_checks(match_never)    994         } while (!end_test_checks(match_never));
998         KUNIT_EXPECT_TRUE(test, match_expect);    995         KUNIT_EXPECT_TRUE(test, match_expect); /* Sanity check. */
999         KUNIT_EXPECT_FALSE(test, match_never);    996         KUNIT_EXPECT_FALSE(test, match_never);
1000 }                                                997 }
1001                                                  998 
1002 /* Test the data_race() macro. */                999 /* Test the data_race() macro. */
1003 __no_kcsan                                       1000 __no_kcsan
1004 static void test_data_race(struct kunit *test    1001 static void test_data_race(struct kunit *test)
1005 {                                                1002 {
1006         bool match_never = false;                1003         bool match_never = false;
1007                                                  1004 
1008         begin_test_checks(test_kernel_data_ra    1005         begin_test_checks(test_kernel_data_race, test_kernel_data_race);
1009         do {                                     1006         do {
1010                 match_never = report_availabl    1007                 match_never = report_available();
1011         } while (!end_test_checks(match_never    1008         } while (!end_test_checks(match_never));
1012         KUNIT_EXPECT_FALSE(test, match_never)    1009         KUNIT_EXPECT_FALSE(test, match_never);
1013 }                                                1010 }
1014                                                  1011 
1015 /* Test the __data_racy type qualifier. */    << 
1016 __no_kcsan                                    << 
1017 static void test_data_racy_qualifier(struct k << 
1018 {                                             << 
1019         bool match_never = false;             << 
1020                                               << 
1021         begin_test_checks(test_kernel_data_ra << 
1022         do {                                  << 
1023                 match_never = report_availabl << 
1024         } while (!end_test_checks(match_never << 
1025         KUNIT_EXPECT_FALSE(test, match_never) << 
1026 }                                             << 
1027                                               << 
1028 __no_kcsan                                       1012 __no_kcsan
1029 static void test_assert_exclusive_writer(stru    1013 static void test_assert_exclusive_writer(struct kunit *test)
1030 {                                                1014 {
1031         struct expect_report expect = {          1015         struct expect_report expect = {
1032                 .access = {                      1016                 .access = {
1033                         { test_kernel_assert_    1017                         { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
1034                         { test_kernel_write_n    1018                         { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
1035                 },                               1019                 },
1036         };                                       1020         };
1037         bool match_expect = false;               1021         bool match_expect = false;
1038                                                  1022 
1039         begin_test_checks(test_kernel_assert_    1023         begin_test_checks(test_kernel_assert_writer, test_kernel_write_nochange);
1040         do {                                     1024         do {
1041                 match_expect = report_matches    1025                 match_expect = report_matches(&expect);
1042         } while (!end_test_checks(match_expec    1026         } while (!end_test_checks(match_expect));
1043         KUNIT_EXPECT_TRUE(test, match_expect)    1027         KUNIT_EXPECT_TRUE(test, match_expect);
1044 }                                                1028 }
1045                                                  1029 
1046 __no_kcsan                                       1030 __no_kcsan
1047 static void test_assert_exclusive_access(stru    1031 static void test_assert_exclusive_access(struct kunit *test)
1048 {                                                1032 {
1049         struct expect_report expect = {          1033         struct expect_report expect = {
1050                 .access = {                      1034                 .access = {
1051                         { test_kernel_assert_    1035                         { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
1052                         { test_kernel_read, &    1036                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
1053                 },                               1037                 },
1054         };                                       1038         };
1055         bool match_expect = false;               1039         bool match_expect = false;
1056                                                  1040 
1057         begin_test_checks(test_kernel_assert_    1041         begin_test_checks(test_kernel_assert_access, test_kernel_read);
1058         do {                                     1042         do {
1059                 match_expect = report_matches    1043                 match_expect = report_matches(&expect);
1060         } while (!end_test_checks(match_expec    1044         } while (!end_test_checks(match_expect));
1061         KUNIT_EXPECT_TRUE(test, match_expect)    1045         KUNIT_EXPECT_TRUE(test, match_expect);
1062 }                                                1046 }
1063                                                  1047 
1064 __no_kcsan                                       1048 __no_kcsan
1065 static void test_assert_exclusive_access_writ    1049 static void test_assert_exclusive_access_writer(struct kunit *test)
1066 {                                                1050 {
1067         struct expect_report expect_access_wr    1051         struct expect_report expect_access_writer = {
1068                 .access = {                      1052                 .access = {
1069                         { test_kernel_assert_    1053                         { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
1070                         { test_kernel_assert_    1054                         { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
1071                 },                               1055                 },
1072         };                                       1056         };
1073         struct expect_report expect_access_ac    1057         struct expect_report expect_access_access = {
1074                 .access = {                      1058                 .access = {
1075                         { test_kernel_assert_    1059                         { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
1076                         { test_kernel_assert_    1060                         { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
1077                 },                               1061                 },
1078         };                                       1062         };
1079         struct expect_report never = {           1063         struct expect_report never = {
1080                 .access = {                      1064                 .access = {
1081                         { test_kernel_assert_    1065                         { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
1082                         { test_kernel_assert_    1066                         { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
1083                 },                               1067                 },
1084         };                                       1068         };
1085         bool match_expect_access_writer = fal    1069         bool match_expect_access_writer = false;
1086         bool match_expect_access_access = fal    1070         bool match_expect_access_access = false;
1087         bool match_never = false;                1071         bool match_never = false;
1088                                                  1072 
1089         begin_test_checks(test_kernel_assert_    1073         begin_test_checks(test_kernel_assert_access, test_kernel_assert_writer);
1090         do {                                     1074         do {
1091                 match_expect_access_writer |=    1075                 match_expect_access_writer |= report_matches(&expect_access_writer);
1092                 match_expect_access_access |=    1076                 match_expect_access_access |= report_matches(&expect_access_access);
1093                 match_never |= report_matches    1077                 match_never |= report_matches(&never);
1094         } while (!end_test_checks(match_never    1078         } while (!end_test_checks(match_never));
1095         KUNIT_EXPECT_TRUE(test, match_expect_    1079         KUNIT_EXPECT_TRUE(test, match_expect_access_writer);
1096         KUNIT_EXPECT_TRUE(test, match_expect_    1080         KUNIT_EXPECT_TRUE(test, match_expect_access_access);
1097         KUNIT_EXPECT_FALSE(test, match_never)    1081         KUNIT_EXPECT_FALSE(test, match_never);
1098 }                                                1082 }
1099                                                  1083 
1100 __no_kcsan                                       1084 __no_kcsan
1101 static void test_assert_exclusive_bits_change    1085 static void test_assert_exclusive_bits_change(struct kunit *test)
1102 {                                                1086 {
1103         struct expect_report expect = {          1087         struct expect_report expect = {
1104                 .access = {                      1088                 .access = {
1105                         { test_kernel_assert_    1089                         { test_kernel_assert_bits_change, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
1106                         { test_kernel_change_    1090                         { test_kernel_change_bits, &test_var, sizeof(test_var),
1107                                 KCSAN_ACCESS_    1091                                 KCSAN_ACCESS_WRITE | (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS) ? 0 : KCSAN_ACCESS_ATOMIC) },
1108                 },                               1092                 },
1109         };                                       1093         };
1110         bool match_expect = false;               1094         bool match_expect = false;
1111                                                  1095 
1112         begin_test_checks(test_kernel_assert_    1096         begin_test_checks(test_kernel_assert_bits_change, test_kernel_change_bits);
1113         do {                                     1097         do {
1114                 match_expect = report_matches    1098                 match_expect = report_matches(&expect);
1115         } while (!end_test_checks(match_expec    1099         } while (!end_test_checks(match_expect));
1116         KUNIT_EXPECT_TRUE(test, match_expect)    1100         KUNIT_EXPECT_TRUE(test, match_expect);
1117 }                                                1101 }
1118                                                  1102 
1119 __no_kcsan                                       1103 __no_kcsan
1120 static void test_assert_exclusive_bits_nochan    1104 static void test_assert_exclusive_bits_nochange(struct kunit *test)
1121 {                                                1105 {
1122         bool match_never = false;                1106         bool match_never = false;
1123                                                  1107 
1124         begin_test_checks(test_kernel_assert_    1108         begin_test_checks(test_kernel_assert_bits_nochange, test_kernel_change_bits);
1125         do {                                     1109         do {
1126                 match_never = report_availabl    1110                 match_never = report_available();
1127         } while (!end_test_checks(match_never    1111         } while (!end_test_checks(match_never));
1128         KUNIT_EXPECT_FALSE(test, match_never)    1112         KUNIT_EXPECT_FALSE(test, match_never);
1129 }                                                1113 }
1130                                                  1114 
1131 __no_kcsan                                       1115 __no_kcsan
1132 static void test_assert_exclusive_writer_scop    1116 static void test_assert_exclusive_writer_scoped(struct kunit *test)
1133 {                                                1117 {
1134         struct expect_report expect_start = {    1118         struct expect_report expect_start = {
1135                 .access = {                      1119                 .access = {
1136                         { test_kernel_assert_    1120                         { test_kernel_assert_writer_scoped, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_SCOPED },
1137                         { test_kernel_write_n    1121                         { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
1138                 },                               1122                 },
1139         };                                       1123         };
1140         struct expect_report expect_inscope =    1124         struct expect_report expect_inscope = {
1141                 .access = {                      1125                 .access = {
1142                         { test_enter_scope, &    1126                         { test_enter_scope, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_SCOPED },
1143                         { test_kernel_write_n    1127                         { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
1144                 },                               1128                 },
1145         };                                       1129         };
1146         bool match_expect_start = false;         1130         bool match_expect_start = false;
1147         bool match_expect_inscope = false;       1131         bool match_expect_inscope = false;
1148                                                  1132 
1149         begin_test_checks(test_kernel_assert_    1133         begin_test_checks(test_kernel_assert_writer_scoped, test_kernel_write_nochange);
1150         do {                                     1134         do {
1151                 match_expect_start |= report_    1135                 match_expect_start |= report_matches(&expect_start);
1152                 match_expect_inscope |= repor    1136                 match_expect_inscope |= report_matches(&expect_inscope);
1153         } while (!end_test_checks(match_expec    1137         } while (!end_test_checks(match_expect_inscope));
1154         KUNIT_EXPECT_TRUE(test, match_expect_    1138         KUNIT_EXPECT_TRUE(test, match_expect_start);
1155         KUNIT_EXPECT_FALSE(test, match_expect    1139         KUNIT_EXPECT_FALSE(test, match_expect_inscope);
1156 }                                                1140 }
1157                                                  1141 
1158 __no_kcsan                                       1142 __no_kcsan
1159 static void test_assert_exclusive_access_scop    1143 static void test_assert_exclusive_access_scoped(struct kunit *test)
1160 {                                                1144 {
1161         struct expect_report expect_start1 =     1145         struct expect_report expect_start1 = {
1162                 .access = {                      1146                 .access = {
1163                         { test_kernel_assert_    1147                         { test_kernel_assert_access_scoped, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_SCOPED },
1164                         { test_kernel_read, &    1148                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
1165                 },                               1149                 },
1166         };                                       1150         };
1167         struct expect_report expect_start2 =     1151         struct expect_report expect_start2 = {
1168                 .access = { expect_start1.acc    1152                 .access = { expect_start1.access[0], expect_start1.access[0] },
1169         };                                       1153         };
1170         struct expect_report expect_inscope =    1154         struct expect_report expect_inscope = {
1171                 .access = {                      1155                 .access = {
1172                         { test_enter_scope, &    1156                         { test_enter_scope, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_SCOPED },
1173                         { test_kernel_read, &    1157                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
1174                 },                               1158                 },
1175         };                                       1159         };
1176         bool match_expect_start = false;         1160         bool match_expect_start = false;
1177         bool match_expect_inscope = false;       1161         bool match_expect_inscope = false;
1178                                                  1162 
1179         begin_test_checks(test_kernel_assert_    1163         begin_test_checks(test_kernel_assert_access_scoped, test_kernel_read);
1180         end_time += msecs_to_jiffies(1000); /    1164         end_time += msecs_to_jiffies(1000); /* This test requires a bit more time. */
1181         do {                                     1165         do {
1182                 match_expect_start |= report_    1166                 match_expect_start |= report_matches(&expect_start1) || report_matches(&expect_start2);
1183                 match_expect_inscope |= repor    1167                 match_expect_inscope |= report_matches(&expect_inscope);
1184         } while (!end_test_checks(match_expec    1168         } while (!end_test_checks(match_expect_inscope));
1185         KUNIT_EXPECT_TRUE(test, match_expect_    1169         KUNIT_EXPECT_TRUE(test, match_expect_start);
1186         KUNIT_EXPECT_FALSE(test, match_expect    1170         KUNIT_EXPECT_FALSE(test, match_expect_inscope);
1187 }                                                1171 }
1188                                                  1172 
1189 /*                                               1173 /*
1190  * jiffies is special (declared to be volatil    1174  * jiffies is special (declared to be volatile) and its accesses are typically
1191  * not marked; this test ensures that the com    1175  * not marked; this test ensures that the compiler nor KCSAN gets confused about
1192  * jiffies's declaration on different archite    1176  * jiffies's declaration on different architectures.
1193  */                                              1177  */
1194 __no_kcsan                                       1178 __no_kcsan
1195 static void test_jiffies_noreport(struct kuni    1179 static void test_jiffies_noreport(struct kunit *test)
1196 {                                                1180 {
1197         bool match_never = false;                1181         bool match_never = false;
1198                                                  1182 
1199         begin_test_checks(test_kernel_jiffies    1183         begin_test_checks(test_kernel_jiffies_reader, test_kernel_jiffies_reader);
1200         do {                                     1184         do {
1201                 match_never = report_availabl    1185                 match_never = report_available();
1202         } while (!end_test_checks(match_never    1186         } while (!end_test_checks(match_never));
1203         KUNIT_EXPECT_FALSE(test, match_never)    1187         KUNIT_EXPECT_FALSE(test, match_never);
1204 }                                                1188 }
1205                                                  1189 
1206 /* Test that racing accesses in seqlock criti    1190 /* Test that racing accesses in seqlock critical sections are not reported. */
1207 __no_kcsan                                       1191 __no_kcsan
1208 static void test_seqlock_noreport(struct kuni    1192 static void test_seqlock_noreport(struct kunit *test)
1209 {                                                1193 {
1210         bool match_never = false;                1194         bool match_never = false;
1211                                                  1195 
1212         begin_test_checks(test_kernel_seqlock    1196         begin_test_checks(test_kernel_seqlock_reader, test_kernel_seqlock_writer);
1213         do {                                     1197         do {
1214                 match_never = report_availabl    1198                 match_never = report_available();
1215         } while (!end_test_checks(match_never    1199         } while (!end_test_checks(match_never));
1216         KUNIT_EXPECT_FALSE(test, match_never)    1200         KUNIT_EXPECT_FALSE(test, match_never);
1217 }                                                1201 }
1218                                                  1202 
1219 /*                                               1203 /*
1220  * Test atomic builtins work and required ins    1204  * Test atomic builtins work and required instrumentation functions exist. We
1221  * also test that KCSAN understands they're a    1205  * also test that KCSAN understands they're atomic by racing with them via
1222  * test_kernel_atomic_builtins(), and expect     1206  * test_kernel_atomic_builtins(), and expect no reports.
1223  *                                               1207  *
1224  * The atomic builtins _SHOULD NOT_ be used i    1208  * The atomic builtins _SHOULD NOT_ be used in normal kernel code!
1225  */                                              1209  */
1226 static void test_atomic_builtins(struct kunit    1210 static void test_atomic_builtins(struct kunit *test)
1227 {                                                1211 {
1228         bool match_never = false;                1212         bool match_never = false;
1229                                                  1213 
1230         begin_test_checks(test_kernel_atomic_    1214         begin_test_checks(test_kernel_atomic_builtins, test_kernel_atomic_builtins);
1231         do {                                     1215         do {
1232                 long tmp;                        1216                 long tmp;
1233                                                  1217 
1234                 kcsan_enable_current();          1218                 kcsan_enable_current();
1235                                                  1219 
1236                 __atomic_store_n(&test_var, 4    1220                 __atomic_store_n(&test_var, 42L, __ATOMIC_RELAXED);
1237                 KUNIT_EXPECT_EQ(test, 42L, __    1221                 KUNIT_EXPECT_EQ(test, 42L, __atomic_load_n(&test_var, __ATOMIC_RELAXED));
1238                                                  1222 
1239                 KUNIT_EXPECT_EQ(test, 42L, __    1223                 KUNIT_EXPECT_EQ(test, 42L, __atomic_exchange_n(&test_var, 20, __ATOMIC_RELAXED));
1240                 KUNIT_EXPECT_EQ(test, 20L, te    1224                 KUNIT_EXPECT_EQ(test, 20L, test_var);
1241                                                  1225 
1242                 tmp = 20L;                       1226                 tmp = 20L;
1243                 KUNIT_EXPECT_TRUE(test, __ato    1227                 KUNIT_EXPECT_TRUE(test, __atomic_compare_exchange_n(&test_var, &tmp, 30L,
1244                                                  1228                                                                     0, __ATOMIC_RELAXED,
1245                                                  1229                                                                     __ATOMIC_RELAXED));
1246                 KUNIT_EXPECT_EQ(test, tmp, 20    1230                 KUNIT_EXPECT_EQ(test, tmp, 20L);
1247                 KUNIT_EXPECT_EQ(test, test_va    1231                 KUNIT_EXPECT_EQ(test, test_var, 30L);
1248                 KUNIT_EXPECT_FALSE(test, __at    1232                 KUNIT_EXPECT_FALSE(test, __atomic_compare_exchange_n(&test_var, &tmp, 40L,
1249                                                  1233                                                                      1, __ATOMIC_RELAXED,
1250                                                  1234                                                                      __ATOMIC_RELAXED));
1251                 KUNIT_EXPECT_EQ(test, tmp, 30    1235                 KUNIT_EXPECT_EQ(test, tmp, 30L);
1252                 KUNIT_EXPECT_EQ(test, test_va    1236                 KUNIT_EXPECT_EQ(test, test_var, 30L);
1253                                                  1237 
1254                 KUNIT_EXPECT_EQ(test, 30L, __    1238                 KUNIT_EXPECT_EQ(test, 30L, __atomic_fetch_add(&test_var, 1, __ATOMIC_RELAXED));
1255                 KUNIT_EXPECT_EQ(test, 31L, __    1239                 KUNIT_EXPECT_EQ(test, 31L, __atomic_fetch_sub(&test_var, 1, __ATOMIC_RELAXED));
1256                 KUNIT_EXPECT_EQ(test, 30L, __    1240                 KUNIT_EXPECT_EQ(test, 30L, __atomic_fetch_and(&test_var, 0xf, __ATOMIC_RELAXED));
1257                 KUNIT_EXPECT_EQ(test, 14L, __    1241                 KUNIT_EXPECT_EQ(test, 14L, __atomic_fetch_xor(&test_var, 0xf, __ATOMIC_RELAXED));
1258                 KUNIT_EXPECT_EQ(test, 1L, __a    1242                 KUNIT_EXPECT_EQ(test, 1L, __atomic_fetch_or(&test_var, 0xf0, __ATOMIC_RELAXED));
1259                 KUNIT_EXPECT_EQ(test, 241L, _    1243                 KUNIT_EXPECT_EQ(test, 241L, __atomic_fetch_nand(&test_var, 0xf, __ATOMIC_RELAXED));
1260                 KUNIT_EXPECT_EQ(test, -2L, te    1244                 KUNIT_EXPECT_EQ(test, -2L, test_var);
1261                                                  1245 
1262                 __atomic_thread_fence(__ATOMI    1246                 __atomic_thread_fence(__ATOMIC_SEQ_CST);
1263                 __atomic_signal_fence(__ATOMI    1247                 __atomic_signal_fence(__ATOMIC_SEQ_CST);
1264                                                  1248 
1265                 kcsan_disable_current();         1249                 kcsan_disable_current();
1266                                                  1250 
1267                 match_never = report_availabl    1251                 match_never = report_available();
1268         } while (!end_test_checks(match_never    1252         } while (!end_test_checks(match_never));
1269         KUNIT_EXPECT_FALSE(test, match_never)    1253         KUNIT_EXPECT_FALSE(test, match_never);
1270 }                                                1254 }
1271                                                  1255 
1272 __no_kcsan                                       1256 __no_kcsan
1273 static void test_1bit_value_change(struct kun    1257 static void test_1bit_value_change(struct kunit *test)
1274 {                                                1258 {
1275         struct expect_report expect = {          1259         struct expect_report expect = {
1276                 .access = {                      1260                 .access = {
1277                         { test_kernel_read, &    1261                         { test_kernel_read, &test_var, sizeof(test_var), 0 },
1278                         { test_kernel_xor_1bi    1262                         { test_kernel_xor_1bit, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
1279                 },                               1263                 },
1280         };                                       1264         };
1281         bool match = false;                      1265         bool match = false;
1282                                                  1266 
1283         begin_test_checks(test_kernel_read, t    1267         begin_test_checks(test_kernel_read, test_kernel_xor_1bit);
1284         do {                                     1268         do {
1285                 match = IS_ENABLED(CONFIG_KCS    1269                 match = IS_ENABLED(CONFIG_KCSAN_PERMISSIVE)
1286                                 ? report_avai    1270                                 ? report_available()
1287                                 : report_matc    1271                                 : report_matches(&expect);
1288         } while (!end_test_checks(match));       1272         } while (!end_test_checks(match));
1289         if (IS_ENABLED(CONFIG_KCSAN_PERMISSIV    1273         if (IS_ENABLED(CONFIG_KCSAN_PERMISSIVE))
1290                 KUNIT_EXPECT_FALSE(test, matc    1274                 KUNIT_EXPECT_FALSE(test, match);
1291         else                                     1275         else
1292                 KUNIT_EXPECT_TRUE(test, match    1276                 KUNIT_EXPECT_TRUE(test, match);
1293 }                                                1277 }
1294                                                  1278 
1295 __no_kcsan                                       1279 __no_kcsan
1296 static void test_correct_barrier(struct kunit    1280 static void test_correct_barrier(struct kunit *test)
1297 {                                                1281 {
1298         struct expect_report expect = {          1282         struct expect_report expect = {
1299                 .access = {                      1283                 .access = {
1300                         { test_kernel_with_me    1284                         { test_kernel_with_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
1301                         { test_kernel_with_me    1285                         { test_kernel_with_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(0) },
1302                 },                               1286                 },
1303         };                                       1287         };
1304         bool match_expect = false;               1288         bool match_expect = false;
1305                                                  1289 
1306         test_struct.val[0] = 0; /* init unloc    1290         test_struct.val[0] = 0; /* init unlocked */
1307         begin_test_checks(test_kernel_with_me    1291         begin_test_checks(test_kernel_with_memorder, test_kernel_with_memorder);
1308         do {                                     1292         do {
1309                 match_expect = report_matches    1293                 match_expect = report_matches_any_reordered(&expect);
1310         } while (!end_test_checks(match_expec    1294         } while (!end_test_checks(match_expect));
1311         KUNIT_EXPECT_FALSE(test, match_expect    1295         KUNIT_EXPECT_FALSE(test, match_expect);
1312 }                                                1296 }
1313                                                  1297 
1314 __no_kcsan                                       1298 __no_kcsan
1315 static void test_missing_barrier(struct kunit    1299 static void test_missing_barrier(struct kunit *test)
1316 {                                                1300 {
1317         struct expect_report expect = {          1301         struct expect_report expect = {
1318                 .access = {                      1302                 .access = {
1319                         { test_kernel_wrong_m    1303                         { test_kernel_wrong_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
1320                         { test_kernel_wrong_m    1304                         { test_kernel_wrong_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(0) },
1321                 },                               1305                 },
1322         };                                       1306         };
1323         bool match_expect = false;               1307         bool match_expect = false;
1324                                                  1308 
1325         test_struct.val[0] = 0; /* init unloc    1309         test_struct.val[0] = 0; /* init unlocked */
1326         begin_test_checks(test_kernel_wrong_m    1310         begin_test_checks(test_kernel_wrong_memorder, test_kernel_wrong_memorder);
1327         do {                                     1311         do {
1328                 match_expect = report_matches    1312                 match_expect = report_matches_any_reordered(&expect);
1329         } while (!end_test_checks(match_expec    1313         } while (!end_test_checks(match_expect));
1330         if (IS_ENABLED(CONFIG_KCSAN_WEAK_MEMO    1314         if (IS_ENABLED(CONFIG_KCSAN_WEAK_MEMORY))
1331                 KUNIT_EXPECT_TRUE(test, match    1315                 KUNIT_EXPECT_TRUE(test, match_expect);
1332         else                                     1316         else
1333                 KUNIT_EXPECT_FALSE(test, matc    1317                 KUNIT_EXPECT_FALSE(test, match_expect);
1334 }                                                1318 }
1335                                                  1319 
1336 __no_kcsan                                       1320 __no_kcsan
1337 static void test_atomic_builtins_correct_barr    1321 static void test_atomic_builtins_correct_barrier(struct kunit *test)
1338 {                                                1322 {
1339         struct expect_report expect = {          1323         struct expect_report expect = {
1340                 .access = {                      1324                 .access = {
1341                         { test_kernel_atomic_    1325                         { test_kernel_atomic_builtin_with_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
1342                         { test_kernel_atomic_    1326                         { test_kernel_atomic_builtin_with_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(0) },
1343                 },                               1327                 },
1344         };                                       1328         };
1345         bool match_expect = false;               1329         bool match_expect = false;
1346                                                  1330 
1347         test_struct.val[0] = 0; /* init unloc    1331         test_struct.val[0] = 0; /* init unlocked */
1348         begin_test_checks(test_kernel_atomic_    1332         begin_test_checks(test_kernel_atomic_builtin_with_memorder,
1349                           test_kernel_atomic_    1333                           test_kernel_atomic_builtin_with_memorder);
1350         do {                                     1334         do {
1351                 match_expect = report_matches    1335                 match_expect = report_matches_any_reordered(&expect);
1352         } while (!end_test_checks(match_expec    1336         } while (!end_test_checks(match_expect));
1353         KUNIT_EXPECT_FALSE(test, match_expect    1337         KUNIT_EXPECT_FALSE(test, match_expect);
1354 }                                                1338 }
1355                                                  1339 
1356 __no_kcsan                                       1340 __no_kcsan
1357 static void test_atomic_builtins_missing_barr    1341 static void test_atomic_builtins_missing_barrier(struct kunit *test)
1358 {                                                1342 {
1359         struct expect_report expect = {          1343         struct expect_report expect = {
1360                 .access = {                      1344                 .access = {
1361                         { test_kernel_atomic_    1345                         { test_kernel_atomic_builtin_wrong_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
1362                         { test_kernel_atomic_    1346                         { test_kernel_atomic_builtin_wrong_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(0) },
1363                 },                               1347                 },
1364         };                                       1348         };
1365         bool match_expect = false;               1349         bool match_expect = false;
1366                                                  1350 
1367         test_struct.val[0] = 0; /* init unloc    1351         test_struct.val[0] = 0; /* init unlocked */
1368         begin_test_checks(test_kernel_atomic_    1352         begin_test_checks(test_kernel_atomic_builtin_wrong_memorder,
1369                           test_kernel_atomic_    1353                           test_kernel_atomic_builtin_wrong_memorder);
1370         do {                                     1354         do {
1371                 match_expect = report_matches    1355                 match_expect = report_matches_any_reordered(&expect);
1372         } while (!end_test_checks(match_expec    1356         } while (!end_test_checks(match_expect));
1373         if (IS_ENABLED(CONFIG_KCSAN_WEAK_MEMO    1357         if (IS_ENABLED(CONFIG_KCSAN_WEAK_MEMORY))
1374                 KUNIT_EXPECT_TRUE(test, match    1358                 KUNIT_EXPECT_TRUE(test, match_expect);
1375         else                                     1359         else
1376                 KUNIT_EXPECT_FALSE(test, matc    1360                 KUNIT_EXPECT_FALSE(test, match_expect);
1377 }                                                1361 }
1378                                                  1362 
1379 /*                                               1363 /*
1380  * Generate thread counts for all test cases.    1364  * Generate thread counts for all test cases. Values generated are in interval
1381  * [2, 5] followed by exponentially increasin    1365  * [2, 5] followed by exponentially increasing thread counts from 8 to 32.
1382  *                                               1366  *
1383  * The thread counts are chosen to cover pote    1367  * The thread counts are chosen to cover potentially interesting boundaries and
1384  * corner cases (2 to 5), and then stress the    1368  * corner cases (2 to 5), and then stress the system with larger counts.
1385  */                                              1369  */
1386 static const void *nthreads_gen_params(const     1370 static const void *nthreads_gen_params(const void *prev, char *desc)
1387 {                                                1371 {
1388         long nthreads = (long)prev;              1372         long nthreads = (long)prev;
1389                                                  1373 
1390         if (nthreads < 0 || nthreads >= 32)      1374         if (nthreads < 0 || nthreads >= 32)
1391                 nthreads = 0; /* stop */         1375                 nthreads = 0; /* stop */
1392         else if (!nthreads)                      1376         else if (!nthreads)
1393                 nthreads = 2; /* initial valu    1377                 nthreads = 2; /* initial value */
1394         else if (nthreads < 5)                   1378         else if (nthreads < 5)
1395                 nthreads++;                      1379                 nthreads++;
1396         else if (nthreads == 5)                  1380         else if (nthreads == 5)
1397                 nthreads = 8;                    1381                 nthreads = 8;
1398         else                                     1382         else
1399                 nthreads *= 2;                   1383                 nthreads *= 2;
1400                                                  1384 
1401         if (!preempt_model_preemptible() ||      1385         if (!preempt_model_preemptible() ||
1402             !IS_ENABLED(CONFIG_KCSAN_INTERRUP    1386             !IS_ENABLED(CONFIG_KCSAN_INTERRUPT_WATCHER)) {
1403                 /*                               1387                 /*
1404                  * Without any preemption, ke    1388                  * Without any preemption, keep 2 CPUs free for other tasks, one
1405                  * of which is the main test     1389                  * of which is the main test case function checking for
1406                  * completion or failure.        1390                  * completion or failure.
1407                  */                              1391                  */
1408                 const long min_unused_cpus =     1392                 const long min_unused_cpus = preempt_model_none() ? 2 : 0;
1409                 const long min_required_cpus     1393                 const long min_required_cpus = 2 + min_unused_cpus;
1410                                                  1394 
1411                 if (num_online_cpus() < min_r    1395                 if (num_online_cpus() < min_required_cpus) {
1412                         pr_err_once("Too few     1396                         pr_err_once("Too few online CPUs (%u < %ld) for test\n",
1413                                     num_onlin    1397                                     num_online_cpus(), min_required_cpus);
1414                         nthreads = 0;            1398                         nthreads = 0;
1415                 } else if (nthreads >= num_on    1399                 } else if (nthreads >= num_online_cpus() - min_unused_cpus) {
1416                         /* Use negative value    1400                         /* Use negative value to indicate last param. */
1417                         nthreads = -(num_onli    1401                         nthreads = -(num_online_cpus() - min_unused_cpus);
1418                         pr_warn_once("Limitin    1402                         pr_warn_once("Limiting number of threads to %ld (only %d online CPUs)\n",
1419                                      -nthread    1403                                      -nthreads, num_online_cpus());
1420                 }                                1404                 }
1421         }                                        1405         }
1422                                                  1406 
1423         snprintf(desc, KUNIT_PARAM_DESC_SIZE,    1407         snprintf(desc, KUNIT_PARAM_DESC_SIZE, "threads=%ld", abs(nthreads));
1424         return (void *)nthreads;                 1408         return (void *)nthreads;
1425 }                                                1409 }
1426                                                  1410 
1427 #define KCSAN_KUNIT_CASE(test_name) KUNIT_CAS    1411 #define KCSAN_KUNIT_CASE(test_name) KUNIT_CASE_PARAM(test_name, nthreads_gen_params)
1428 static struct kunit_case kcsan_test_cases[] =    1412 static struct kunit_case kcsan_test_cases[] = {
1429         KUNIT_CASE(test_barrier_nothreads),      1413         KUNIT_CASE(test_barrier_nothreads),
1430         KCSAN_KUNIT_CASE(test_basic),            1414         KCSAN_KUNIT_CASE(test_basic),
1431         KCSAN_KUNIT_CASE(test_concurrent_race    1415         KCSAN_KUNIT_CASE(test_concurrent_races),
1432         KCSAN_KUNIT_CASE(test_novalue_change)    1416         KCSAN_KUNIT_CASE(test_novalue_change),
1433         KCSAN_KUNIT_CASE(test_novalue_change_    1417         KCSAN_KUNIT_CASE(test_novalue_change_exception),
1434         KCSAN_KUNIT_CASE(test_unknown_origin)    1418         KCSAN_KUNIT_CASE(test_unknown_origin),
1435         KCSAN_KUNIT_CASE(test_write_write_ass    1419         KCSAN_KUNIT_CASE(test_write_write_assume_atomic),
1436         KCSAN_KUNIT_CASE(test_write_write_str    1420         KCSAN_KUNIT_CASE(test_write_write_struct),
1437         KCSAN_KUNIT_CASE(test_write_write_str    1421         KCSAN_KUNIT_CASE(test_write_write_struct_part),
1438         KCSAN_KUNIT_CASE(test_read_atomic_wri    1422         KCSAN_KUNIT_CASE(test_read_atomic_write_atomic),
1439         KCSAN_KUNIT_CASE(test_read_plain_atom    1423         KCSAN_KUNIT_CASE(test_read_plain_atomic_write),
1440         KCSAN_KUNIT_CASE(test_read_plain_atom    1424         KCSAN_KUNIT_CASE(test_read_plain_atomic_rmw),
1441         KCSAN_KUNIT_CASE(test_zero_size_acces    1425         KCSAN_KUNIT_CASE(test_zero_size_access),
1442         KCSAN_KUNIT_CASE(test_data_race),        1426         KCSAN_KUNIT_CASE(test_data_race),
1443         KCSAN_KUNIT_CASE(test_data_racy_quali << 
1444         KCSAN_KUNIT_CASE(test_assert_exclusiv    1427         KCSAN_KUNIT_CASE(test_assert_exclusive_writer),
1445         KCSAN_KUNIT_CASE(test_assert_exclusiv    1428         KCSAN_KUNIT_CASE(test_assert_exclusive_access),
1446         KCSAN_KUNIT_CASE(test_assert_exclusiv    1429         KCSAN_KUNIT_CASE(test_assert_exclusive_access_writer),
1447         KCSAN_KUNIT_CASE(test_assert_exclusiv    1430         KCSAN_KUNIT_CASE(test_assert_exclusive_bits_change),
1448         KCSAN_KUNIT_CASE(test_assert_exclusiv    1431         KCSAN_KUNIT_CASE(test_assert_exclusive_bits_nochange),
1449         KCSAN_KUNIT_CASE(test_assert_exclusiv    1432         KCSAN_KUNIT_CASE(test_assert_exclusive_writer_scoped),
1450         KCSAN_KUNIT_CASE(test_assert_exclusiv    1433         KCSAN_KUNIT_CASE(test_assert_exclusive_access_scoped),
1451         KCSAN_KUNIT_CASE(test_jiffies_norepor    1434         KCSAN_KUNIT_CASE(test_jiffies_noreport),
1452         KCSAN_KUNIT_CASE(test_seqlock_norepor    1435         KCSAN_KUNIT_CASE(test_seqlock_noreport),
1453         KCSAN_KUNIT_CASE(test_atomic_builtins    1436         KCSAN_KUNIT_CASE(test_atomic_builtins),
1454         KCSAN_KUNIT_CASE(test_1bit_value_chan    1437         KCSAN_KUNIT_CASE(test_1bit_value_change),
1455         KCSAN_KUNIT_CASE(test_correct_barrier    1438         KCSAN_KUNIT_CASE(test_correct_barrier),
1456         KCSAN_KUNIT_CASE(test_missing_barrier    1439         KCSAN_KUNIT_CASE(test_missing_barrier),
1457         KCSAN_KUNIT_CASE(test_atomic_builtins    1440         KCSAN_KUNIT_CASE(test_atomic_builtins_correct_barrier),
1458         KCSAN_KUNIT_CASE(test_atomic_builtins    1441         KCSAN_KUNIT_CASE(test_atomic_builtins_missing_barrier),
1459         {},                                      1442         {},
1460 };                                               1443 };
1461                                                  1444 
1462 /* ===== End test cases ===== */                 1445 /* ===== End test cases ===== */
1463                                                  1446 
1464 /* Concurrent accesses from interrupts. */       1447 /* Concurrent accesses from interrupts. */
1465 __no_kcsan                                       1448 __no_kcsan
1466 static void access_thread_timer(struct timer_    1449 static void access_thread_timer(struct timer_list *timer)
1467 {                                                1450 {
1468         static atomic_t cnt = ATOMIC_INIT(0);    1451         static atomic_t cnt = ATOMIC_INIT(0);
1469         unsigned int idx;                        1452         unsigned int idx;
1470         void (*func)(void);                      1453         void (*func)(void);
1471                                                  1454 
1472         idx = (unsigned int)atomic_inc_return    1455         idx = (unsigned int)atomic_inc_return(&cnt) % ARRAY_SIZE(access_kernels);
1473         /* Acquire potential initialization.     1456         /* Acquire potential initialization. */
1474         func = smp_load_acquire(&access_kerne    1457         func = smp_load_acquire(&access_kernels[idx]);
1475         if (func)                                1458         if (func)
1476                 func();                          1459                 func();
1477 }                                                1460 }
1478                                                  1461 
1479 /* The main loop for each thread. */             1462 /* The main loop for each thread. */
1480 __no_kcsan                                       1463 __no_kcsan
1481 static int access_thread(void *arg)              1464 static int access_thread(void *arg)
1482 {                                                1465 {
1483         struct timer_list timer;                 1466         struct timer_list timer;
1484         unsigned int cnt = 0;                    1467         unsigned int cnt = 0;
1485         unsigned int idx;                        1468         unsigned int idx;
1486         void (*func)(void);                      1469         void (*func)(void);
1487                                                  1470 
1488         timer_setup_on_stack(&timer, access_t    1471         timer_setup_on_stack(&timer, access_thread_timer, 0);
1489         do {                                     1472         do {
1490                 might_sleep();                   1473                 might_sleep();
1491                                                  1474 
1492                 if (!timer_pending(&timer))      1475                 if (!timer_pending(&timer))
1493                         mod_timer(&timer, jif    1476                         mod_timer(&timer, jiffies + 1);
1494                 else {                           1477                 else {
1495                         /* Iterate through al    1478                         /* Iterate through all kernels. */
1496                         idx = cnt++ % ARRAY_S    1479                         idx = cnt++ % ARRAY_SIZE(access_kernels);
1497                         /* Acquire potential     1480                         /* Acquire potential initialization. */
1498                         func = smp_load_acqui    1481                         func = smp_load_acquire(&access_kernels[idx]);
1499                         if (func)                1482                         if (func)
1500                                 func();          1483                                 func();
1501                 }                                1484                 }
1502         } while (!torture_must_stop());          1485         } while (!torture_must_stop());
1503         del_timer_sync(&timer);                  1486         del_timer_sync(&timer);
1504         destroy_timer_on_stack(&timer);          1487         destroy_timer_on_stack(&timer);
1505                                                  1488 
1506         torture_kthread_stopping("access_thre    1489         torture_kthread_stopping("access_thread");
1507         return 0;                                1490         return 0;
1508 }                                                1491 }
1509                                                  1492 
1510 __no_kcsan                                       1493 __no_kcsan
1511 static int test_init(struct kunit *test)         1494 static int test_init(struct kunit *test)
1512 {                                                1495 {
1513         unsigned long flags;                     1496         unsigned long flags;
1514         int nthreads;                            1497         int nthreads;
1515         int i;                                   1498         int i;
1516                                                  1499 
1517         spin_lock_irqsave(&observed.lock, fla    1500         spin_lock_irqsave(&observed.lock, flags);
1518         for (i = 0; i < ARRAY_SIZE(observed.l    1501         for (i = 0; i < ARRAY_SIZE(observed.lines); ++i)
1519                 observed.lines[i][0] = '\0';     1502                 observed.lines[i][0] = '\0';
1520         observed.nlines = 0;                     1503         observed.nlines = 0;
1521         spin_unlock_irqrestore(&observed.lock    1504         spin_unlock_irqrestore(&observed.lock, flags);
1522                                                  1505 
1523         if (strstr(test->name, "nothreads"))     1506         if (strstr(test->name, "nothreads"))
1524                 return 0;                        1507                 return 0;
1525                                                  1508 
1526         if (!torture_init_begin((char *)test-    1509         if (!torture_init_begin((char *)test->name, 1))
1527                 return -EBUSY;                   1510                 return -EBUSY;
1528                                                  1511 
1529         if (WARN_ON(threads))                    1512         if (WARN_ON(threads))
1530                 goto err;                        1513                 goto err;
1531                                                  1514 
1532         for (i = 0; i < ARRAY_SIZE(access_ker    1515         for (i = 0; i < ARRAY_SIZE(access_kernels); ++i) {
1533                 if (WARN_ON(access_kernels[i]    1516                 if (WARN_ON(access_kernels[i]))
1534                         goto err;                1517                         goto err;
1535         }                                        1518         }
1536                                                  1519 
1537         nthreads = abs((long)test->param_valu    1520         nthreads = abs((long)test->param_value);
1538         if (WARN_ON(!nthreads))                  1521         if (WARN_ON(!nthreads))
1539                 goto err;                        1522                 goto err;
1540                                                  1523 
1541         threads = kcalloc(nthreads + 1, sizeo    1524         threads = kcalloc(nthreads + 1, sizeof(struct task_struct *), GFP_KERNEL);
1542         if (WARN_ON(!threads))                   1525         if (WARN_ON(!threads))
1543                 goto err;                        1526                 goto err;
1544                                                  1527 
1545         threads[nthreads] = NULL;                1528         threads[nthreads] = NULL;
1546         for (i = 0; i < nthreads; ++i) {         1529         for (i = 0; i < nthreads; ++i) {
1547                 if (torture_create_kthread(ac    1530                 if (torture_create_kthread(access_thread, NULL, threads[i]))
1548                         goto err;                1531                         goto err;
1549         }                                        1532         }
1550                                                  1533 
1551         torture_init_end();                      1534         torture_init_end();
1552                                                  1535 
1553         return 0;                                1536         return 0;
1554                                                  1537 
1555 err:                                             1538 err:
1556         kfree(threads);                          1539         kfree(threads);
1557         threads = NULL;                          1540         threads = NULL;
1558         torture_init_end();                      1541         torture_init_end();
1559         return -EINVAL;                          1542         return -EINVAL;
1560 }                                                1543 }
1561                                                  1544 
1562 __no_kcsan                                       1545 __no_kcsan
1563 static void test_exit(struct kunit *test)        1546 static void test_exit(struct kunit *test)
1564 {                                                1547 {
1565         struct task_struct **stop_thread;        1548         struct task_struct **stop_thread;
1566         int i;                                   1549         int i;
1567                                                  1550 
1568         if (strstr(test->name, "nothreads"))     1551         if (strstr(test->name, "nothreads"))
1569                 return;                          1552                 return;
1570                                                  1553 
1571         if (torture_cleanup_begin())             1554         if (torture_cleanup_begin())
1572                 return;                          1555                 return;
1573                                                  1556 
1574         for (i = 0; i < ARRAY_SIZE(access_ker    1557         for (i = 0; i < ARRAY_SIZE(access_kernels); ++i)
1575                 WRITE_ONCE(access_kernels[i],    1558                 WRITE_ONCE(access_kernels[i], NULL);
1576                                                  1559 
1577         if (threads) {                           1560         if (threads) {
1578                 for (stop_thread = threads; *    1561                 for (stop_thread = threads; *stop_thread; stop_thread++)
1579                         torture_stop_kthread(    1562                         torture_stop_kthread(reader_thread, *stop_thread);
1580                                                  1563 
1581                 kfree(threads);                  1564                 kfree(threads);
1582                 threads = NULL;                  1565                 threads = NULL;
1583         }                                        1566         }
1584                                                  1567 
1585         torture_cleanup_end();                   1568         torture_cleanup_end();
1586 }                                                1569 }
1587                                                  1570 
1588 __no_kcsan                                       1571 __no_kcsan
1589 static void register_tracepoints(void)           1572 static void register_tracepoints(void)
1590 {                                                1573 {
1591         register_trace_console(probe_console,    1574         register_trace_console(probe_console, NULL);
1592 }                                                1575 }
1593                                                  1576 
1594 __no_kcsan                                       1577 __no_kcsan
1595 static void unregister_tracepoints(void)         1578 static void unregister_tracepoints(void)
1596 {                                                1579 {
1597         unregister_trace_console(probe_consol    1580         unregister_trace_console(probe_console, NULL);
1598 }                                                1581 }
1599                                                  1582 
1600 static int kcsan_suite_init(struct kunit_suit    1583 static int kcsan_suite_init(struct kunit_suite *suite)
1601 {                                                1584 {
1602         register_tracepoints();                  1585         register_tracepoints();
1603         return 0;                                1586         return 0;
1604 }                                                1587 }
1605                                                  1588 
1606 static void kcsan_suite_exit(struct kunit_sui    1589 static void kcsan_suite_exit(struct kunit_suite *suite)
1607 {                                                1590 {
1608         unregister_tracepoints();                1591         unregister_tracepoints();
1609         tracepoint_synchronize_unregister();     1592         tracepoint_synchronize_unregister();
1610 }                                                1593 }
1611                                                  1594 
1612 static struct kunit_suite kcsan_test_suite =     1595 static struct kunit_suite kcsan_test_suite = {
1613         .name = "kcsan",                         1596         .name = "kcsan",
1614         .test_cases = kcsan_test_cases,          1597         .test_cases = kcsan_test_cases,
1615         .init = test_init,                       1598         .init = test_init,
1616         .exit = test_exit,                       1599         .exit = test_exit,
1617         .suite_init = kcsan_suite_init,          1600         .suite_init = kcsan_suite_init,
1618         .suite_exit = kcsan_suite_exit,          1601         .suite_exit = kcsan_suite_exit,
1619 };                                               1602 };
1620                                                  1603 
1621 kunit_test_suites(&kcsan_test_suite);            1604 kunit_test_suites(&kcsan_test_suite);
1622                                                  1605 
1623 MODULE_DESCRIPTION("KCSAN test suite");       << 
1624 MODULE_LICENSE("GPL v2");                        1606 MODULE_LICENSE("GPL v2");
1625 MODULE_AUTHOR("Marco Elver <elver@google.com>    1607 MODULE_AUTHOR("Marco Elver <elver@google.com>");
1626                                                  1608 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php