~ [ 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-5.10.229)


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

~ [ 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