kcsan_test.c 51 KB

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