kcsan_test.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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 <elver@google.com>
  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 long __data_racy test_data_racy;
  265. static DEFINE_SEQLOCK(test_seqlock);
  266. static DEFINE_SPINLOCK(test_spinlock);
  267. static DEFINE_MUTEX(test_mutex);
  268. /*
  269. * Helper to avoid compiler optimizing out reads, and to generate source values
  270. * for writes.
  271. */
  272. __no_kcsan
  273. static noinline void sink_value(long v) { WRITE_ONCE(test_sink, v); }
  274. /*
  275. * Generates a delay and some accesses that enter the runtime but do not produce
  276. * data races.
  277. */
  278. static noinline void test_delay(int iter)
  279. {
  280. while (iter--)
  281. sink_value(READ_ONCE(test_sink));
  282. }
  283. static noinline void test_kernel_read(void) { sink_value(test_var); }
  284. static noinline void test_kernel_write(void)
  285. {
  286. test_var = READ_ONCE_NOCHECK(test_sink) + 1;
  287. }
  288. static noinline void test_kernel_write_nochange(void) { test_var = 42; }
  289. /* Suffixed by value-change exception filter. */
  290. static noinline void test_kernel_write_nochange_rcu(void) { test_var = 42; }
  291. static noinline void test_kernel_read_atomic(void)
  292. {
  293. sink_value(READ_ONCE(test_var));
  294. }
  295. static noinline void test_kernel_write_atomic(void)
  296. {
  297. WRITE_ONCE(test_var, READ_ONCE_NOCHECK(test_sink) + 1);
  298. }
  299. static noinline void test_kernel_atomic_rmw(void)
  300. {
  301. /* Use builtin, so we can set up the "bad" atomic/non-atomic scenario. */
  302. __atomic_fetch_add(&test_var, 1, __ATOMIC_RELAXED);
  303. }
  304. __no_kcsan
  305. static noinline void test_kernel_write_uninstrumented(void) { test_var++; }
  306. static noinline void test_kernel_data_race(void) { data_race(test_var++); }
  307. static noinline void test_kernel_data_racy_qualifier(void) { test_data_racy++; }
  308. static noinline void test_kernel_assert_writer(void)
  309. {
  310. ASSERT_EXCLUSIVE_WRITER(test_var);
  311. }
  312. static noinline void test_kernel_assert_access(void)
  313. {
  314. ASSERT_EXCLUSIVE_ACCESS(test_var);
  315. }
  316. #define TEST_CHANGE_BITS 0xff00ff00
  317. static noinline void test_kernel_change_bits(void)
  318. {
  319. if (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS)) {
  320. /*
  321. * Avoid race of unknown origin for this test, just pretend they
  322. * are atomic.
  323. */
  324. kcsan_nestable_atomic_begin();
  325. test_var ^= TEST_CHANGE_BITS;
  326. kcsan_nestable_atomic_end();
  327. } else
  328. WRITE_ONCE(test_var, READ_ONCE(test_var) ^ TEST_CHANGE_BITS);
  329. }
  330. static noinline void test_kernel_assert_bits_change(void)
  331. {
  332. ASSERT_EXCLUSIVE_BITS(test_var, TEST_CHANGE_BITS);
  333. }
  334. static noinline void test_kernel_assert_bits_nochange(void)
  335. {
  336. ASSERT_EXCLUSIVE_BITS(test_var, ~TEST_CHANGE_BITS);
  337. }
  338. /*
  339. * Scoped assertions do trigger anywhere in scope. However, the report should
  340. * still only point at the start of the scope.
  341. */
  342. static noinline void test_enter_scope(void)
  343. {
  344. int x = 0;
  345. /* Unrelated accesses to scoped assert. */
  346. READ_ONCE(test_sink);
  347. kcsan_check_read(&x, sizeof(x));
  348. }
  349. static noinline void test_kernel_assert_writer_scoped(void)
  350. {
  351. ASSERT_EXCLUSIVE_WRITER_SCOPED(test_var);
  352. test_enter_scope();
  353. }
  354. static noinline void test_kernel_assert_access_scoped(void)
  355. {
  356. ASSERT_EXCLUSIVE_ACCESS_SCOPED(test_var);
  357. test_enter_scope();
  358. }
  359. static noinline void test_kernel_rmw_array(void)
  360. {
  361. int i;
  362. for (i = 0; i < ARRAY_SIZE(test_array); ++i)
  363. test_array[i]++;
  364. }
  365. static noinline void test_kernel_write_struct(void)
  366. {
  367. kcsan_check_write(&test_struct, sizeof(test_struct));
  368. kcsan_disable_current();
  369. test_struct.val[3]++; /* induce value change */
  370. kcsan_enable_current();
  371. }
  372. static noinline void test_kernel_write_struct_part(void)
  373. {
  374. test_struct.val[3] = 42;
  375. }
  376. static noinline void test_kernel_read_struct_zero_size(void)
  377. {
  378. kcsan_check_read(&test_struct.val[3], 0);
  379. }
  380. static noinline void test_kernel_jiffies_reader(void)
  381. {
  382. sink_value((long)jiffies);
  383. }
  384. static noinline void test_kernel_seqlock_reader(void)
  385. {
  386. unsigned int seq;
  387. do {
  388. seq = read_seqbegin(&test_seqlock);
  389. sink_value(test_var);
  390. } while (read_seqretry(&test_seqlock, seq));
  391. }
  392. static noinline void test_kernel_seqlock_writer(void)
  393. {
  394. unsigned long flags;
  395. write_seqlock_irqsave(&test_seqlock, flags);
  396. test_var++;
  397. write_sequnlock_irqrestore(&test_seqlock, flags);
  398. }
  399. static noinline void test_kernel_atomic_builtins(void)
  400. {
  401. /*
  402. * Generate concurrent accesses, expecting no reports, ensuring KCSAN
  403. * treats builtin atomics as actually atomic.
  404. */
  405. __atomic_load_n(&test_var, __ATOMIC_RELAXED);
  406. }
  407. static noinline void test_kernel_xor_1bit(void)
  408. {
  409. /* Do not report data races between the read-writes. */
  410. kcsan_nestable_atomic_begin();
  411. test_var ^= 0x10000;
  412. kcsan_nestable_atomic_end();
  413. }
  414. #define TEST_KERNEL_LOCKED(name, acquire, release) \
  415. static noinline void test_kernel_##name(void) \
  416. { \
  417. long *flag = &test_struct.val[0]; \
  418. long v = 0; \
  419. if (!(acquire)) \
  420. return; \
  421. while (v++ < 100) { \
  422. test_var++; \
  423. barrier(); \
  424. } \
  425. release; \
  426. test_delay(10); \
  427. }
  428. TEST_KERNEL_LOCKED(with_memorder,
  429. cmpxchg_acquire(flag, 0, 1) == 0,
  430. smp_store_release(flag, 0));
  431. TEST_KERNEL_LOCKED(wrong_memorder,
  432. cmpxchg_relaxed(flag, 0, 1) == 0,
  433. WRITE_ONCE(*flag, 0));
  434. TEST_KERNEL_LOCKED(atomic_builtin_with_memorder,
  435. __atomic_compare_exchange_n(flag, &v, 1, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED),
  436. __atomic_store_n(flag, 0, __ATOMIC_RELEASE));
  437. TEST_KERNEL_LOCKED(atomic_builtin_wrong_memorder,
  438. __atomic_compare_exchange_n(flag, &v, 1, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED),
  439. __atomic_store_n(flag, 0, __ATOMIC_RELAXED));
  440. /* ===== Test cases ===== */
  441. /*
  442. * Tests that various barriers have the expected effect on internal state. Not
  443. * exhaustive on atomic_t operations. Unlike the selftest, also checks for
  444. * too-strict barrier instrumentation; these can be tolerated, because it does
  445. * not cause false positives, but at least we should be aware of such cases.
  446. */
  447. static void test_barrier_nothreads(struct kunit *test)
  448. {
  449. #ifdef CONFIG_KCSAN_WEAK_MEMORY
  450. struct kcsan_scoped_access *reorder_access = &current->kcsan_ctx.reorder_access;
  451. #else
  452. struct kcsan_scoped_access *reorder_access = NULL;
  453. #endif
  454. arch_spinlock_t arch_spinlock = __ARCH_SPIN_LOCK_UNLOCKED;
  455. atomic_t dummy = ATOMIC_INIT(0);
  456. KCSAN_TEST_REQUIRES(test, reorder_access != NULL);
  457. KCSAN_TEST_REQUIRES(test, IS_ENABLED(CONFIG_SMP));
  458. #define __KCSAN_EXPECT_BARRIER(access_type, barrier, order_before, name) \
  459. do { \
  460. reorder_access->type = (access_type) | KCSAN_ACCESS_SCOPED; \
  461. reorder_access->size = sizeof(test_var); \
  462. barrier; \
  463. KUNIT_EXPECT_EQ_MSG(test, reorder_access->size, \
  464. order_before ? 0 : sizeof(test_var), \
  465. "improperly instrumented type=(" #access_type "): " name); \
  466. } while (0)
  467. #define KCSAN_EXPECT_READ_BARRIER(b, o) __KCSAN_EXPECT_BARRIER(0, b, o, #b)
  468. #define KCSAN_EXPECT_WRITE_BARRIER(b, o) __KCSAN_EXPECT_BARRIER(KCSAN_ACCESS_WRITE, b, o, #b)
  469. #define KCSAN_EXPECT_RW_BARRIER(b, o) __KCSAN_EXPECT_BARRIER(KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_WRITE, b, o, #b)
  470. /*
  471. * Lockdep initialization can strengthen certain locking operations due
  472. * to calling into instrumented files; "warm up" our locks.
  473. */
  474. spin_lock(&test_spinlock);
  475. spin_unlock(&test_spinlock);
  476. mutex_lock(&test_mutex);
  477. mutex_unlock(&test_mutex);
  478. /* Force creating a valid entry in reorder_access first. */
  479. test_var = 0;
  480. while (test_var++ < 1000000 && reorder_access->size != sizeof(test_var))
  481. __kcsan_check_read(&test_var, sizeof(test_var));
  482. KUNIT_ASSERT_EQ(test, reorder_access->size, sizeof(test_var));
  483. kcsan_nestable_atomic_begin(); /* No watchpoints in called functions. */
  484. KCSAN_EXPECT_READ_BARRIER(mb(), true);
  485. KCSAN_EXPECT_READ_BARRIER(wmb(), false);
  486. KCSAN_EXPECT_READ_BARRIER(rmb(), true);
  487. KCSAN_EXPECT_READ_BARRIER(smp_mb(), true);
  488. KCSAN_EXPECT_READ_BARRIER(smp_wmb(), false);
  489. KCSAN_EXPECT_READ_BARRIER(smp_rmb(), true);
  490. KCSAN_EXPECT_READ_BARRIER(dma_wmb(), false);
  491. KCSAN_EXPECT_READ_BARRIER(dma_rmb(), true);
  492. KCSAN_EXPECT_READ_BARRIER(smp_mb__before_atomic(), true);
  493. KCSAN_EXPECT_READ_BARRIER(smp_mb__after_atomic(), true);
  494. KCSAN_EXPECT_READ_BARRIER(smp_mb__after_spinlock(), true);
  495. KCSAN_EXPECT_READ_BARRIER(smp_store_mb(test_var, 0), true);
  496. KCSAN_EXPECT_READ_BARRIER(smp_load_acquire(&test_var), false);
  497. KCSAN_EXPECT_READ_BARRIER(smp_store_release(&test_var, 0), true);
  498. KCSAN_EXPECT_READ_BARRIER(xchg(&test_var, 0), true);
  499. KCSAN_EXPECT_READ_BARRIER(xchg_release(&test_var, 0), true);
  500. KCSAN_EXPECT_READ_BARRIER(xchg_relaxed(&test_var, 0), false);
  501. KCSAN_EXPECT_READ_BARRIER(cmpxchg(&test_var, 0, 0), true);
  502. KCSAN_EXPECT_READ_BARRIER(cmpxchg_release(&test_var, 0, 0), true);
  503. KCSAN_EXPECT_READ_BARRIER(cmpxchg_relaxed(&test_var, 0, 0), false);
  504. KCSAN_EXPECT_READ_BARRIER(atomic_read(&dummy), false);
  505. KCSAN_EXPECT_READ_BARRIER(atomic_read_acquire(&dummy), false);
  506. KCSAN_EXPECT_READ_BARRIER(atomic_set(&dummy, 0), false);
  507. KCSAN_EXPECT_READ_BARRIER(atomic_set_release(&dummy, 0), true);
  508. KCSAN_EXPECT_READ_BARRIER(atomic_add(1, &dummy), false);
  509. KCSAN_EXPECT_READ_BARRIER(atomic_add_return(1, &dummy), true);
  510. KCSAN_EXPECT_READ_BARRIER(atomic_add_return_acquire(1, &dummy), false);
  511. KCSAN_EXPECT_READ_BARRIER(atomic_add_return_release(1, &dummy), true);
  512. KCSAN_EXPECT_READ_BARRIER(atomic_add_return_relaxed(1, &dummy), false);
  513. KCSAN_EXPECT_READ_BARRIER(atomic_fetch_add(1, &dummy), true);
  514. KCSAN_EXPECT_READ_BARRIER(atomic_fetch_add_acquire(1, &dummy), false);
  515. KCSAN_EXPECT_READ_BARRIER(atomic_fetch_add_release(1, &dummy), true);
  516. KCSAN_EXPECT_READ_BARRIER(atomic_fetch_add_relaxed(1, &dummy), false);
  517. KCSAN_EXPECT_READ_BARRIER(test_and_set_bit(0, &test_var), true);
  518. KCSAN_EXPECT_READ_BARRIER(test_and_clear_bit(0, &test_var), true);
  519. KCSAN_EXPECT_READ_BARRIER(test_and_change_bit(0, &test_var), true);
  520. KCSAN_EXPECT_READ_BARRIER(clear_bit_unlock(0, &test_var), true);
  521. KCSAN_EXPECT_READ_BARRIER(__clear_bit_unlock(0, &test_var), true);
  522. KCSAN_EXPECT_READ_BARRIER(arch_spin_lock(&arch_spinlock), false);
  523. KCSAN_EXPECT_READ_BARRIER(arch_spin_unlock(&arch_spinlock), true);
  524. KCSAN_EXPECT_READ_BARRIER(spin_lock(&test_spinlock), false);
  525. KCSAN_EXPECT_READ_BARRIER(spin_unlock(&test_spinlock), true);
  526. KCSAN_EXPECT_READ_BARRIER(mutex_lock(&test_mutex), false);
  527. KCSAN_EXPECT_READ_BARRIER(mutex_unlock(&test_mutex), true);
  528. KCSAN_EXPECT_WRITE_BARRIER(mb(), true);
  529. KCSAN_EXPECT_WRITE_BARRIER(wmb(), true);
  530. KCSAN_EXPECT_WRITE_BARRIER(rmb(), false);
  531. KCSAN_EXPECT_WRITE_BARRIER(smp_mb(), true);
  532. KCSAN_EXPECT_WRITE_BARRIER(smp_wmb(), true);
  533. KCSAN_EXPECT_WRITE_BARRIER(smp_rmb(), false);
  534. KCSAN_EXPECT_WRITE_BARRIER(dma_wmb(), true);
  535. KCSAN_EXPECT_WRITE_BARRIER(dma_rmb(), false);
  536. KCSAN_EXPECT_WRITE_BARRIER(smp_mb__before_atomic(), true);
  537. KCSAN_EXPECT_WRITE_BARRIER(smp_mb__after_atomic(), true);
  538. KCSAN_EXPECT_WRITE_BARRIER(smp_mb__after_spinlock(), true);
  539. KCSAN_EXPECT_WRITE_BARRIER(smp_store_mb(test_var, 0), true);
  540. KCSAN_EXPECT_WRITE_BARRIER(smp_load_acquire(&test_var), false);
  541. KCSAN_EXPECT_WRITE_BARRIER(smp_store_release(&test_var, 0), true);
  542. KCSAN_EXPECT_WRITE_BARRIER(xchg(&test_var, 0), true);
  543. KCSAN_EXPECT_WRITE_BARRIER(xchg_release(&test_var, 0), true);
  544. KCSAN_EXPECT_WRITE_BARRIER(xchg_relaxed(&test_var, 0), false);
  545. KCSAN_EXPECT_WRITE_BARRIER(cmpxchg(&test_var, 0, 0), true);
  546. KCSAN_EXPECT_WRITE_BARRIER(cmpxchg_release(&test_var, 0, 0), true);
  547. KCSAN_EXPECT_WRITE_BARRIER(cmpxchg_relaxed(&test_var, 0, 0), false);
  548. KCSAN_EXPECT_WRITE_BARRIER(atomic_read(&dummy), false);
  549. KCSAN_EXPECT_WRITE_BARRIER(atomic_read_acquire(&dummy), false);
  550. KCSAN_EXPECT_WRITE_BARRIER(atomic_set(&dummy, 0), false);
  551. KCSAN_EXPECT_WRITE_BARRIER(atomic_set_release(&dummy, 0), true);
  552. KCSAN_EXPECT_WRITE_BARRIER(atomic_add(1, &dummy), false);
  553. KCSAN_EXPECT_WRITE_BARRIER(atomic_add_return(1, &dummy), true);
  554. KCSAN_EXPECT_WRITE_BARRIER(atomic_add_return_acquire(1, &dummy), false);
  555. KCSAN_EXPECT_WRITE_BARRIER(atomic_add_return_release(1, &dummy), true);
  556. KCSAN_EXPECT_WRITE_BARRIER(atomic_add_return_relaxed(1, &dummy), false);
  557. KCSAN_EXPECT_WRITE_BARRIER(atomic_fetch_add(1, &dummy), true);
  558. KCSAN_EXPECT_WRITE_BARRIER(atomic_fetch_add_acquire(1, &dummy), false);
  559. KCSAN_EXPECT_WRITE_BARRIER(atomic_fetch_add_release(1, &dummy), true);
  560. KCSAN_EXPECT_WRITE_BARRIER(atomic_fetch_add_relaxed(1, &dummy), false);
  561. KCSAN_EXPECT_WRITE_BARRIER(test_and_set_bit(0, &test_var), true);
  562. KCSAN_EXPECT_WRITE_BARRIER(test_and_clear_bit(0, &test_var), true);
  563. KCSAN_EXPECT_WRITE_BARRIER(test_and_change_bit(0, &test_var), true);
  564. KCSAN_EXPECT_WRITE_BARRIER(clear_bit_unlock(0, &test_var), true);
  565. KCSAN_EXPECT_WRITE_BARRIER(__clear_bit_unlock(0, &test_var), true);
  566. KCSAN_EXPECT_WRITE_BARRIER(arch_spin_lock(&arch_spinlock), false);
  567. KCSAN_EXPECT_WRITE_BARRIER(arch_spin_unlock(&arch_spinlock), true);
  568. KCSAN_EXPECT_WRITE_BARRIER(spin_lock(&test_spinlock), false);
  569. KCSAN_EXPECT_WRITE_BARRIER(spin_unlock(&test_spinlock), true);
  570. KCSAN_EXPECT_WRITE_BARRIER(mutex_lock(&test_mutex), false);
  571. KCSAN_EXPECT_WRITE_BARRIER(mutex_unlock(&test_mutex), true);
  572. KCSAN_EXPECT_RW_BARRIER(mb(), true);
  573. KCSAN_EXPECT_RW_BARRIER(wmb(), true);
  574. KCSAN_EXPECT_RW_BARRIER(rmb(), true);
  575. KCSAN_EXPECT_RW_BARRIER(smp_mb(), true);
  576. KCSAN_EXPECT_RW_BARRIER(smp_wmb(), true);
  577. KCSAN_EXPECT_RW_BARRIER(smp_rmb(), true);
  578. KCSAN_EXPECT_RW_BARRIER(dma_wmb(), true);
  579. KCSAN_EXPECT_RW_BARRIER(dma_rmb(), true);
  580. KCSAN_EXPECT_RW_BARRIER(smp_mb__before_atomic(), true);
  581. KCSAN_EXPECT_RW_BARRIER(smp_mb__after_atomic(), true);
  582. KCSAN_EXPECT_RW_BARRIER(smp_mb__after_spinlock(), true);
  583. KCSAN_EXPECT_RW_BARRIER(smp_store_mb(test_var, 0), true);
  584. KCSAN_EXPECT_RW_BARRIER(smp_load_acquire(&test_var), false);
  585. KCSAN_EXPECT_RW_BARRIER(smp_store_release(&test_var, 0), true);
  586. KCSAN_EXPECT_RW_BARRIER(xchg(&test_var, 0), true);
  587. KCSAN_EXPECT_RW_BARRIER(xchg_release(&test_var, 0), true);
  588. KCSAN_EXPECT_RW_BARRIER(xchg_relaxed(&test_var, 0), false);
  589. KCSAN_EXPECT_RW_BARRIER(cmpxchg(&test_var, 0, 0), true);
  590. KCSAN_EXPECT_RW_BARRIER(cmpxchg_release(&test_var, 0, 0), true);
  591. KCSAN_EXPECT_RW_BARRIER(cmpxchg_relaxed(&test_var, 0, 0), false);
  592. KCSAN_EXPECT_RW_BARRIER(atomic_read(&dummy), false);
  593. KCSAN_EXPECT_RW_BARRIER(atomic_read_acquire(&dummy), false);
  594. KCSAN_EXPECT_RW_BARRIER(atomic_set(&dummy, 0), false);
  595. KCSAN_EXPECT_RW_BARRIER(atomic_set_release(&dummy, 0), true);
  596. KCSAN_EXPECT_RW_BARRIER(atomic_add(1, &dummy), false);
  597. KCSAN_EXPECT_RW_BARRIER(atomic_add_return(1, &dummy), true);
  598. KCSAN_EXPECT_RW_BARRIER(atomic_add_return_acquire(1, &dummy), false);
  599. KCSAN_EXPECT_RW_BARRIER(atomic_add_return_release(1, &dummy), true);
  600. KCSAN_EXPECT_RW_BARRIER(atomic_add_return_relaxed(1, &dummy), false);
  601. KCSAN_EXPECT_RW_BARRIER(atomic_fetch_add(1, &dummy), true);
  602. KCSAN_EXPECT_RW_BARRIER(atomic_fetch_add_acquire(1, &dummy), false);
  603. KCSAN_EXPECT_RW_BARRIER(atomic_fetch_add_release(1, &dummy), true);
  604. KCSAN_EXPECT_RW_BARRIER(atomic_fetch_add_relaxed(1, &dummy), false);
  605. KCSAN_EXPECT_RW_BARRIER(test_and_set_bit(0, &test_var), true);
  606. KCSAN_EXPECT_RW_BARRIER(test_and_clear_bit(0, &test_var), true);
  607. KCSAN_EXPECT_RW_BARRIER(test_and_change_bit(0, &test_var), true);
  608. KCSAN_EXPECT_RW_BARRIER(clear_bit_unlock(0, &test_var), true);
  609. KCSAN_EXPECT_RW_BARRIER(__clear_bit_unlock(0, &test_var), true);
  610. KCSAN_EXPECT_RW_BARRIER(arch_spin_lock(&arch_spinlock), false);
  611. KCSAN_EXPECT_RW_BARRIER(arch_spin_unlock(&arch_spinlock), true);
  612. KCSAN_EXPECT_RW_BARRIER(spin_lock(&test_spinlock), false);
  613. KCSAN_EXPECT_RW_BARRIER(spin_unlock(&test_spinlock), true);
  614. KCSAN_EXPECT_RW_BARRIER(mutex_lock(&test_mutex), false);
  615. KCSAN_EXPECT_RW_BARRIER(mutex_unlock(&test_mutex), true);
  616. KCSAN_EXPECT_READ_BARRIER(xor_unlock_is_negative_byte(1, &test_var), true);
  617. KCSAN_EXPECT_WRITE_BARRIER(xor_unlock_is_negative_byte(1, &test_var), true);
  618. KCSAN_EXPECT_RW_BARRIER(xor_unlock_is_negative_byte(1, &test_var), true);
  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. /* Test the __data_racy type qualifier. */
  898. __no_kcsan
  899. static void test_data_racy_qualifier(struct kunit *test)
  900. {
  901. bool match_never = false;
  902. begin_test_checks(test_kernel_data_racy_qualifier, test_kernel_data_racy_qualifier);
  903. do {
  904. match_never = report_available();
  905. } while (!end_test_checks(match_never));
  906. KUNIT_EXPECT_FALSE(test, match_never);
  907. }
  908. __no_kcsan
  909. static void test_assert_exclusive_writer(struct kunit *test)
  910. {
  911. struct expect_report expect = {
  912. .access = {
  913. { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
  914. { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
  915. },
  916. };
  917. bool match_expect = false;
  918. begin_test_checks(test_kernel_assert_writer, test_kernel_write_nochange);
  919. do {
  920. match_expect = report_matches(&expect);
  921. } while (!end_test_checks(match_expect));
  922. KUNIT_EXPECT_TRUE(test, match_expect);
  923. }
  924. __no_kcsan
  925. static void test_assert_exclusive_access(struct kunit *test)
  926. {
  927. struct expect_report expect = {
  928. .access = {
  929. { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
  930. { test_kernel_read, &test_var, sizeof(test_var), 0 },
  931. },
  932. };
  933. bool match_expect = false;
  934. begin_test_checks(test_kernel_assert_access, test_kernel_read);
  935. do {
  936. match_expect = report_matches(&expect);
  937. } while (!end_test_checks(match_expect));
  938. KUNIT_EXPECT_TRUE(test, match_expect);
  939. }
  940. __no_kcsan
  941. static void test_assert_exclusive_access_writer(struct kunit *test)
  942. {
  943. struct expect_report expect_access_writer = {
  944. .access = {
  945. { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
  946. { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
  947. },
  948. };
  949. struct expect_report expect_access_access = {
  950. .access = {
  951. { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
  952. { test_kernel_assert_access, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE },
  953. },
  954. };
  955. struct expect_report never = {
  956. .access = {
  957. { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
  958. { test_kernel_assert_writer, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
  959. },
  960. };
  961. bool match_expect_access_writer = false;
  962. bool match_expect_access_access = false;
  963. bool match_never = false;
  964. begin_test_checks(test_kernel_assert_access, test_kernel_assert_writer);
  965. do {
  966. match_expect_access_writer |= report_matches(&expect_access_writer);
  967. match_expect_access_access |= report_matches(&expect_access_access);
  968. match_never |= report_matches(&never);
  969. } while (!end_test_checks(match_never));
  970. KUNIT_EXPECT_TRUE(test, match_expect_access_writer);
  971. KUNIT_EXPECT_TRUE(test, match_expect_access_access);
  972. KUNIT_EXPECT_FALSE(test, match_never);
  973. }
  974. __no_kcsan
  975. static void test_assert_exclusive_bits_change(struct kunit *test)
  976. {
  977. struct expect_report expect = {
  978. .access = {
  979. { test_kernel_assert_bits_change, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT },
  980. { test_kernel_change_bits, &test_var, sizeof(test_var),
  981. KCSAN_ACCESS_WRITE | (IS_ENABLED(CONFIG_KCSAN_IGNORE_ATOMICS) ? 0 : KCSAN_ACCESS_ATOMIC) },
  982. },
  983. };
  984. bool match_expect = false;
  985. begin_test_checks(test_kernel_assert_bits_change, test_kernel_change_bits);
  986. do {
  987. match_expect = report_matches(&expect);
  988. } while (!end_test_checks(match_expect));
  989. KUNIT_EXPECT_TRUE(test, match_expect);
  990. }
  991. __no_kcsan
  992. static void test_assert_exclusive_bits_nochange(struct kunit *test)
  993. {
  994. bool match_never = false;
  995. begin_test_checks(test_kernel_assert_bits_nochange, test_kernel_change_bits);
  996. do {
  997. match_never = report_available();
  998. } while (!end_test_checks(match_never));
  999. KUNIT_EXPECT_FALSE(test, match_never);
  1000. }
  1001. __no_kcsan
  1002. static void test_assert_exclusive_writer_scoped(struct kunit *test)
  1003. {
  1004. struct expect_report expect_start = {
  1005. .access = {
  1006. { test_kernel_assert_writer_scoped, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_SCOPED },
  1007. { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
  1008. },
  1009. };
  1010. struct expect_report expect_inscope = {
  1011. .access = {
  1012. { test_enter_scope, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_SCOPED },
  1013. { test_kernel_write_nochange, &test_var, sizeof(test_var), KCSAN_ACCESS_WRITE },
  1014. },
  1015. };
  1016. bool match_expect_start = false;
  1017. bool match_expect_inscope = false;
  1018. begin_test_checks(test_kernel_assert_writer_scoped, test_kernel_write_nochange);
  1019. do {
  1020. match_expect_start |= report_matches(&expect_start);
  1021. match_expect_inscope |= report_matches(&expect_inscope);
  1022. } while (!end_test_checks(match_expect_inscope));
  1023. KUNIT_EXPECT_TRUE(test, match_expect_start);
  1024. KUNIT_EXPECT_FALSE(test, match_expect_inscope);
  1025. }
  1026. __no_kcsan
  1027. static void test_assert_exclusive_access_scoped(struct kunit *test)
  1028. {
  1029. struct expect_report expect_start1 = {
  1030. .access = {
  1031. { test_kernel_assert_access_scoped, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_SCOPED },
  1032. { test_kernel_read, &test_var, sizeof(test_var), 0 },
  1033. },
  1034. };
  1035. struct expect_report expect_start2 = {
  1036. .access = { expect_start1.access[0], expect_start1.access[0] },
  1037. };
  1038. struct expect_report expect_inscope = {
  1039. .access = {
  1040. { test_enter_scope, &test_var, sizeof(test_var), KCSAN_ACCESS_ASSERT | KCSAN_ACCESS_WRITE | KCSAN_ACCESS_SCOPED },
  1041. { test_kernel_read, &test_var, sizeof(test_var), 0 },
  1042. },
  1043. };
  1044. bool match_expect_start = false;
  1045. bool match_expect_inscope = false;
  1046. begin_test_checks(test_kernel_assert_access_scoped, test_kernel_read);
  1047. end_time += msecs_to_jiffies(1000); /* This test requires a bit more time. */
  1048. do {
  1049. match_expect_start |= report_matches(&expect_start1) || report_matches(&expect_start2);
  1050. match_expect_inscope |= report_matches(&expect_inscope);
  1051. } while (!end_test_checks(match_expect_inscope));
  1052. KUNIT_EXPECT_TRUE(test, match_expect_start);
  1053. KUNIT_EXPECT_FALSE(test, match_expect_inscope);
  1054. }
  1055. /*
  1056. * jiffies is special (declared to be volatile) and its accesses are typically
  1057. * not marked; this test ensures that the compiler nor KCSAN gets confused about
  1058. * jiffies's declaration on different architectures.
  1059. */
  1060. __no_kcsan
  1061. static void test_jiffies_noreport(struct kunit *test)
  1062. {
  1063. bool match_never = false;
  1064. begin_test_checks(test_kernel_jiffies_reader, test_kernel_jiffies_reader);
  1065. do {
  1066. match_never = report_available();
  1067. } while (!end_test_checks(match_never));
  1068. KUNIT_EXPECT_FALSE(test, match_never);
  1069. }
  1070. /* Test that racing accesses in seqlock critical sections are not reported. */
  1071. __no_kcsan
  1072. static void test_seqlock_noreport(struct kunit *test)
  1073. {
  1074. bool match_never = false;
  1075. begin_test_checks(test_kernel_seqlock_reader, test_kernel_seqlock_writer);
  1076. do {
  1077. match_never = report_available();
  1078. } while (!end_test_checks(match_never));
  1079. KUNIT_EXPECT_FALSE(test, match_never);
  1080. }
  1081. /*
  1082. * Test atomic builtins work and required instrumentation functions exist. We
  1083. * also test that KCSAN understands they're atomic by racing with them via
  1084. * test_kernel_atomic_builtins(), and expect no reports.
  1085. *
  1086. * The atomic builtins _SHOULD NOT_ be used in normal kernel code!
  1087. */
  1088. static void test_atomic_builtins(struct kunit *test)
  1089. {
  1090. bool match_never = false;
  1091. begin_test_checks(test_kernel_atomic_builtins, test_kernel_atomic_builtins);
  1092. do {
  1093. long tmp;
  1094. kcsan_enable_current();
  1095. __atomic_store_n(&test_var, 42L, __ATOMIC_RELAXED);
  1096. KUNIT_EXPECT_EQ(test, 42L, __atomic_load_n(&test_var, __ATOMIC_RELAXED));
  1097. KUNIT_EXPECT_EQ(test, 42L, __atomic_exchange_n(&test_var, 20, __ATOMIC_RELAXED));
  1098. KUNIT_EXPECT_EQ(test, 20L, test_var);
  1099. tmp = 20L;
  1100. KUNIT_EXPECT_TRUE(test, __atomic_compare_exchange_n(&test_var, &tmp, 30L,
  1101. 0, __ATOMIC_RELAXED,
  1102. __ATOMIC_RELAXED));
  1103. KUNIT_EXPECT_EQ(test, tmp, 20L);
  1104. KUNIT_EXPECT_EQ(test, test_var, 30L);
  1105. KUNIT_EXPECT_FALSE(test, __atomic_compare_exchange_n(&test_var, &tmp, 40L,
  1106. 1, __ATOMIC_RELAXED,
  1107. __ATOMIC_RELAXED));
  1108. KUNIT_EXPECT_EQ(test, tmp, 30L);
  1109. KUNIT_EXPECT_EQ(test, test_var, 30L);
  1110. KUNIT_EXPECT_EQ(test, 30L, __atomic_fetch_add(&test_var, 1, __ATOMIC_RELAXED));
  1111. KUNIT_EXPECT_EQ(test, 31L, __atomic_fetch_sub(&test_var, 1, __ATOMIC_RELAXED));
  1112. KUNIT_EXPECT_EQ(test, 30L, __atomic_fetch_and(&test_var, 0xf, __ATOMIC_RELAXED));
  1113. KUNIT_EXPECT_EQ(test, 14L, __atomic_fetch_xor(&test_var, 0xf, __ATOMIC_RELAXED));
  1114. KUNIT_EXPECT_EQ(test, 1L, __atomic_fetch_or(&test_var, 0xf0, __ATOMIC_RELAXED));
  1115. KUNIT_EXPECT_EQ(test, 241L, __atomic_fetch_nand(&test_var, 0xf, __ATOMIC_RELAXED));
  1116. KUNIT_EXPECT_EQ(test, -2L, test_var);
  1117. __atomic_thread_fence(__ATOMIC_SEQ_CST);
  1118. __atomic_signal_fence(__ATOMIC_SEQ_CST);
  1119. kcsan_disable_current();
  1120. match_never = report_available();
  1121. } while (!end_test_checks(match_never));
  1122. KUNIT_EXPECT_FALSE(test, match_never);
  1123. }
  1124. __no_kcsan
  1125. static void test_1bit_value_change(struct kunit *test)
  1126. {
  1127. struct expect_report expect = {
  1128. .access = {
  1129. { test_kernel_read, &test_var, sizeof(test_var), 0 },
  1130. { test_kernel_xor_1bit, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
  1131. },
  1132. };
  1133. bool match = false;
  1134. begin_test_checks(test_kernel_read, test_kernel_xor_1bit);
  1135. do {
  1136. match = IS_ENABLED(CONFIG_KCSAN_PERMISSIVE)
  1137. ? report_available()
  1138. : report_matches(&expect);
  1139. } while (!end_test_checks(match));
  1140. if (IS_ENABLED(CONFIG_KCSAN_PERMISSIVE))
  1141. KUNIT_EXPECT_FALSE(test, match);
  1142. else
  1143. KUNIT_EXPECT_TRUE(test, match);
  1144. }
  1145. __no_kcsan
  1146. static void test_correct_barrier(struct kunit *test)
  1147. {
  1148. struct expect_report expect = {
  1149. .access = {
  1150. { test_kernel_with_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
  1151. { test_kernel_with_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(0) },
  1152. },
  1153. };
  1154. bool match_expect = false;
  1155. test_struct.val[0] = 0; /* init unlocked */
  1156. begin_test_checks(test_kernel_with_memorder, test_kernel_with_memorder);
  1157. do {
  1158. match_expect = report_matches_any_reordered(&expect);
  1159. } while (!end_test_checks(match_expect));
  1160. KUNIT_EXPECT_FALSE(test, match_expect);
  1161. }
  1162. __no_kcsan
  1163. static void test_missing_barrier(struct kunit *test)
  1164. {
  1165. struct expect_report expect = {
  1166. .access = {
  1167. { test_kernel_wrong_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
  1168. { test_kernel_wrong_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(0) },
  1169. },
  1170. };
  1171. bool match_expect = false;
  1172. test_struct.val[0] = 0; /* init unlocked */
  1173. begin_test_checks(test_kernel_wrong_memorder, test_kernel_wrong_memorder);
  1174. do {
  1175. match_expect = report_matches_any_reordered(&expect);
  1176. } while (!end_test_checks(match_expect));
  1177. if (IS_ENABLED(CONFIG_KCSAN_WEAK_MEMORY))
  1178. KUNIT_EXPECT_TRUE(test, match_expect);
  1179. else
  1180. KUNIT_EXPECT_FALSE(test, match_expect);
  1181. }
  1182. __no_kcsan
  1183. static void test_atomic_builtins_correct_barrier(struct kunit *test)
  1184. {
  1185. struct expect_report expect = {
  1186. .access = {
  1187. { test_kernel_atomic_builtin_with_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
  1188. { test_kernel_atomic_builtin_with_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(0) },
  1189. },
  1190. };
  1191. bool match_expect = false;
  1192. test_struct.val[0] = 0; /* init unlocked */
  1193. begin_test_checks(test_kernel_atomic_builtin_with_memorder,
  1194. test_kernel_atomic_builtin_with_memorder);
  1195. do {
  1196. match_expect = report_matches_any_reordered(&expect);
  1197. } while (!end_test_checks(match_expect));
  1198. KUNIT_EXPECT_FALSE(test, match_expect);
  1199. }
  1200. __no_kcsan
  1201. static void test_atomic_builtins_missing_barrier(struct kunit *test)
  1202. {
  1203. struct expect_report expect = {
  1204. .access = {
  1205. { test_kernel_atomic_builtin_wrong_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(KCSAN_ACCESS_WRITE) },
  1206. { test_kernel_atomic_builtin_wrong_memorder, &test_var, sizeof(test_var), __KCSAN_ACCESS_RW(0) },
  1207. },
  1208. };
  1209. bool match_expect = false;
  1210. test_struct.val[0] = 0; /* init unlocked */
  1211. begin_test_checks(test_kernel_atomic_builtin_wrong_memorder,
  1212. test_kernel_atomic_builtin_wrong_memorder);
  1213. do {
  1214. match_expect = report_matches_any_reordered(&expect);
  1215. } while (!end_test_checks(match_expect));
  1216. if (IS_ENABLED(CONFIG_KCSAN_WEAK_MEMORY))
  1217. KUNIT_EXPECT_TRUE(test, match_expect);
  1218. else
  1219. KUNIT_EXPECT_FALSE(test, match_expect);
  1220. }
  1221. /*
  1222. * Generate thread counts for all test cases. Values generated are in interval
  1223. * [2, 5] followed by exponentially increasing thread counts from 8 to 32.
  1224. *
  1225. * The thread counts are chosen to cover potentially interesting boundaries and
  1226. * corner cases (2 to 5), and then stress the system with larger counts.
  1227. */
  1228. static const void *nthreads_gen_params(const void *prev, char *desc)
  1229. {
  1230. long nthreads = (long)prev;
  1231. if (nthreads < 0 || nthreads >= 32)
  1232. nthreads = 0; /* stop */
  1233. else if (!nthreads)
  1234. nthreads = 2; /* initial value */
  1235. else if (nthreads < 5)
  1236. nthreads++;
  1237. else if (nthreads == 5)
  1238. nthreads = 8;
  1239. else
  1240. nthreads *= 2;
  1241. if (!preempt_model_preemptible() ||
  1242. !IS_ENABLED(CONFIG_KCSAN_INTERRUPT_WATCHER)) {
  1243. /*
  1244. * Without any preemption, keep 2 CPUs free for other tasks, one
  1245. * of which is the main test case function checking for
  1246. * completion or failure.
  1247. */
  1248. const long min_unused_cpus = preempt_model_none() ? 2 : 0;
  1249. const long min_required_cpus = 2 + min_unused_cpus;
  1250. if (num_online_cpus() < min_required_cpus) {
  1251. pr_err_once("Too few online CPUs (%u < %ld) for test\n",
  1252. num_online_cpus(), min_required_cpus);
  1253. nthreads = 0;
  1254. } else if (nthreads >= num_online_cpus() - min_unused_cpus) {
  1255. /* Use negative value to indicate last param. */
  1256. nthreads = -(num_online_cpus() - min_unused_cpus);
  1257. pr_warn_once("Limiting number of threads to %ld (only %d online CPUs)\n",
  1258. -nthreads, num_online_cpus());
  1259. }
  1260. }
  1261. snprintf(desc, KUNIT_PARAM_DESC_SIZE, "threads=%ld", abs(nthreads));
  1262. return (void *)nthreads;
  1263. }
  1264. #define KCSAN_KUNIT_CASE(test_name) KUNIT_CASE_PARAM(test_name, nthreads_gen_params)
  1265. static struct kunit_case kcsan_test_cases[] = {
  1266. KUNIT_CASE(test_barrier_nothreads),
  1267. KCSAN_KUNIT_CASE(test_basic),
  1268. KCSAN_KUNIT_CASE(test_concurrent_races),
  1269. KCSAN_KUNIT_CASE(test_novalue_change),
  1270. KCSAN_KUNIT_CASE(test_novalue_change_exception),
  1271. KCSAN_KUNIT_CASE(test_unknown_origin),
  1272. KCSAN_KUNIT_CASE(test_write_write_assume_atomic),
  1273. KCSAN_KUNIT_CASE(test_write_write_struct),
  1274. KCSAN_KUNIT_CASE(test_write_write_struct_part),
  1275. KCSAN_KUNIT_CASE(test_read_atomic_write_atomic),
  1276. KCSAN_KUNIT_CASE(test_read_plain_atomic_write),
  1277. KCSAN_KUNIT_CASE(test_read_plain_atomic_rmw),
  1278. KCSAN_KUNIT_CASE(test_zero_size_access),
  1279. KCSAN_KUNIT_CASE(test_data_race),
  1280. KCSAN_KUNIT_CASE(test_data_racy_qualifier),
  1281. KCSAN_KUNIT_CASE(test_assert_exclusive_writer),
  1282. KCSAN_KUNIT_CASE(test_assert_exclusive_access),
  1283. KCSAN_KUNIT_CASE(test_assert_exclusive_access_writer),
  1284. KCSAN_KUNIT_CASE(test_assert_exclusive_bits_change),
  1285. KCSAN_KUNIT_CASE(test_assert_exclusive_bits_nochange),
  1286. KCSAN_KUNIT_CASE(test_assert_exclusive_writer_scoped),
  1287. KCSAN_KUNIT_CASE(test_assert_exclusive_access_scoped),
  1288. KCSAN_KUNIT_CASE(test_jiffies_noreport),
  1289. KCSAN_KUNIT_CASE(test_seqlock_noreport),
  1290. KCSAN_KUNIT_CASE(test_atomic_builtins),
  1291. KCSAN_KUNIT_CASE(test_1bit_value_change),
  1292. KCSAN_KUNIT_CASE(test_correct_barrier),
  1293. KCSAN_KUNIT_CASE(test_missing_barrier),
  1294. KCSAN_KUNIT_CASE(test_atomic_builtins_correct_barrier),
  1295. KCSAN_KUNIT_CASE(test_atomic_builtins_missing_barrier),
  1296. {},
  1297. };
  1298. /* ===== End test cases ===== */
  1299. /* Concurrent accesses from interrupts. */
  1300. __no_kcsan
  1301. static void access_thread_timer(struct timer_list *timer)
  1302. {
  1303. static atomic_t cnt = ATOMIC_INIT(0);
  1304. unsigned int idx;
  1305. void (*func)(void);
  1306. idx = (unsigned int)atomic_inc_return(&cnt) % ARRAY_SIZE(access_kernels);
  1307. /* Acquire potential initialization. */
  1308. func = smp_load_acquire(&access_kernels[idx]);
  1309. if (func)
  1310. func();
  1311. }
  1312. /* The main loop for each thread. */
  1313. __no_kcsan
  1314. static int access_thread(void *arg)
  1315. {
  1316. struct timer_list timer;
  1317. unsigned int cnt = 0;
  1318. unsigned int idx;
  1319. void (*func)(void);
  1320. timer_setup_on_stack(&timer, access_thread_timer, 0);
  1321. do {
  1322. might_sleep();
  1323. if (!timer_pending(&timer))
  1324. mod_timer(&timer, jiffies + 1);
  1325. else {
  1326. /* Iterate through all kernels. */
  1327. idx = cnt++ % ARRAY_SIZE(access_kernels);
  1328. /* Acquire potential initialization. */
  1329. func = smp_load_acquire(&access_kernels[idx]);
  1330. if (func)
  1331. func();
  1332. }
  1333. } while (!torture_must_stop());
  1334. del_timer_sync(&timer);
  1335. destroy_timer_on_stack(&timer);
  1336. torture_kthread_stopping("access_thread");
  1337. return 0;
  1338. }
  1339. __no_kcsan
  1340. static int test_init(struct kunit *test)
  1341. {
  1342. unsigned long flags;
  1343. int nthreads;
  1344. int i;
  1345. spin_lock_irqsave(&observed.lock, flags);
  1346. for (i = 0; i < ARRAY_SIZE(observed.lines); ++i)
  1347. observed.lines[i][0] = '\0';
  1348. observed.nlines = 0;
  1349. spin_unlock_irqrestore(&observed.lock, flags);
  1350. if (strstr(test->name, "nothreads"))
  1351. return 0;
  1352. if (!torture_init_begin((char *)test->name, 1))
  1353. return -EBUSY;
  1354. if (WARN_ON(threads))
  1355. goto err;
  1356. for (i = 0; i < ARRAY_SIZE(access_kernels); ++i) {
  1357. if (WARN_ON(access_kernels[i]))
  1358. goto err;
  1359. }
  1360. nthreads = abs((long)test->param_value);
  1361. if (WARN_ON(!nthreads))
  1362. goto err;
  1363. threads = kcalloc(nthreads + 1, sizeof(struct task_struct *), GFP_KERNEL);
  1364. if (WARN_ON(!threads))
  1365. goto err;
  1366. threads[nthreads] = NULL;
  1367. for (i = 0; i < nthreads; ++i) {
  1368. if (torture_create_kthread(access_thread, NULL, threads[i]))
  1369. goto err;
  1370. }
  1371. torture_init_end();
  1372. return 0;
  1373. err:
  1374. kfree(threads);
  1375. threads = NULL;
  1376. torture_init_end();
  1377. return -EINVAL;
  1378. }
  1379. __no_kcsan
  1380. static void test_exit(struct kunit *test)
  1381. {
  1382. struct task_struct **stop_thread;
  1383. int i;
  1384. if (strstr(test->name, "nothreads"))
  1385. return;
  1386. if (torture_cleanup_begin())
  1387. return;
  1388. for (i = 0; i < ARRAY_SIZE(access_kernels); ++i)
  1389. WRITE_ONCE(access_kernels[i], NULL);
  1390. if (threads) {
  1391. for (stop_thread = threads; *stop_thread; stop_thread++)
  1392. torture_stop_kthread(reader_thread, *stop_thread);
  1393. kfree(threads);
  1394. threads = NULL;
  1395. }
  1396. torture_cleanup_end();
  1397. }
  1398. __no_kcsan
  1399. static void register_tracepoints(void)
  1400. {
  1401. register_trace_console(probe_console, NULL);
  1402. }
  1403. __no_kcsan
  1404. static void unregister_tracepoints(void)
  1405. {
  1406. unregister_trace_console(probe_console, NULL);
  1407. }
  1408. static int kcsan_suite_init(struct kunit_suite *suite)
  1409. {
  1410. register_tracepoints();
  1411. return 0;
  1412. }
  1413. static void kcsan_suite_exit(struct kunit_suite *suite)
  1414. {
  1415. unregister_tracepoints();
  1416. tracepoint_synchronize_unregister();
  1417. }
  1418. static struct kunit_suite kcsan_test_suite = {
  1419. .name = "kcsan",
  1420. .test_cases = kcsan_test_cases,
  1421. .init = test_init,
  1422. .exit = test_exit,
  1423. .suite_init = kcsan_suite_init,
  1424. .suite_exit = kcsan_suite_exit,
  1425. };
  1426. kunit_test_suites(&kcsan_test_suite);
  1427. MODULE_DESCRIPTION("KCSAN test suite");
  1428. MODULE_LICENSE("GPL v2");
  1429. MODULE_AUTHOR("Marco Elver <elver@google.com>");