kcsan-test.c 35 KB

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