test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Base unit test (KUnit) API.
  4. *
  5. * Copyright (C) 2019, Google LLC.
  6. * Author: Brendan Higgins <[email protected]>
  7. */
  8. #include <kunit/resource.h>
  9. #include <kunit/test.h>
  10. #include <kunit/string-stream.h>
  11. #include <kunit/test-bug.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/panic.h>
  16. #include <linux/sched/debug.h>
  17. #include <linux/sched.h>
  18. #include "debugfs.h"
  19. #include "try-catch-impl.h"
  20. #ifdef CONFIG_SEC_KUNIT
  21. struct test_global_context {
  22. struct list_head initcalls;
  23. };
  24. static struct test_global_context test_global_context = {
  25. .initcalls = LIST_HEAD_INIT(test_global_context.initcalls),
  26. };
  27. void test_install_initcall(struct test_initcall *initcall)
  28. {
  29. list_add_tail(&initcall->node, &test_global_context.initcalls);
  30. }
  31. #endif
  32. #if IS_BUILTIN(CONFIG_KUNIT)
  33. /*
  34. * Fail the current test and print an error message to the log.
  35. */
  36. void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
  37. {
  38. va_list args;
  39. int len;
  40. char *buffer;
  41. if (!current->kunit_test)
  42. return;
  43. kunit_set_failure(current->kunit_test);
  44. /* kunit_err() only accepts literals, so evaluate the args first. */
  45. va_start(args, fmt);
  46. len = vsnprintf(NULL, 0, fmt, args) + 1;
  47. va_end(args);
  48. buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL);
  49. if (!buffer)
  50. return;
  51. va_start(args, fmt);
  52. vsnprintf(buffer, len, fmt, args);
  53. va_end(args);
  54. kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
  55. kunit_kfree(current->kunit_test, buffer);
  56. }
  57. EXPORT_SYMBOL_GPL(__kunit_fail_current_test);
  58. #endif
  59. /*
  60. * Enable KUnit tests to run.
  61. */
  62. #ifdef CONFIG_KUNIT_DEFAULT_ENABLED
  63. static bool enable_param = true;
  64. #else
  65. static bool enable_param;
  66. #endif
  67. module_param_named(enable, enable_param, bool, 0);
  68. MODULE_PARM_DESC(enable, "Enable KUnit tests");
  69. /*
  70. * KUnit statistic mode:
  71. * 0 - disabled
  72. * 1 - only when there is more than one subtest
  73. * 2 - enabled
  74. */
  75. static int kunit_stats_enabled = 1;
  76. module_param_named(stats_enabled, kunit_stats_enabled, int, 0644);
  77. MODULE_PARM_DESC(stats_enabled,
  78. "Print test stats: never (0), only for multiple subtests (1), or always (2)");
  79. struct kunit_result_stats {
  80. unsigned long passed;
  81. unsigned long skipped;
  82. unsigned long failed;
  83. unsigned long total;
  84. };
  85. static bool kunit_should_print_stats(struct kunit_result_stats stats)
  86. {
  87. if (kunit_stats_enabled == 0)
  88. return false;
  89. if (kunit_stats_enabled == 2)
  90. return true;
  91. return (stats.total > 1);
  92. }
  93. static void kunit_print_test_stats(struct kunit *test,
  94. struct kunit_result_stats stats)
  95. {
  96. if (!kunit_should_print_stats(stats))
  97. return;
  98. kunit_log(KERN_INFO, test,
  99. KUNIT_SUBTEST_INDENT
  100. "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
  101. test->name,
  102. stats.passed,
  103. stats.failed,
  104. stats.skipped,
  105. stats.total);
  106. }
  107. /*
  108. * Append formatted message to log, size of which is limited to
  109. * KUNIT_LOG_SIZE bytes (including null terminating byte).
  110. */
  111. void kunit_log_append(char *log, const char *fmt, ...)
  112. {
  113. char line[KUNIT_LOG_SIZE];
  114. va_list args;
  115. int len_left;
  116. if (!log)
  117. return;
  118. len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
  119. if (len_left <= 0)
  120. return;
  121. va_start(args, fmt);
  122. vsnprintf(line, sizeof(line), fmt, args);
  123. va_end(args);
  124. strncat(log, line, len_left);
  125. }
  126. EXPORT_SYMBOL_GPL(kunit_log_append);
  127. size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
  128. {
  129. struct kunit_case *test_case;
  130. size_t len = 0;
  131. kunit_suite_for_each_test_case(suite, test_case)
  132. len++;
  133. return len;
  134. }
  135. EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
  136. static void kunit_print_suite_start(struct kunit_suite *suite)
  137. {
  138. /*
  139. * We do not log the test suite header as doing so would
  140. * mean debugfs display would consist of the test suite
  141. * header prior to individual test results.
  142. * Hence directly printk the suite status, and we will
  143. * separately seq_printf() the suite header for the debugfs
  144. * representation.
  145. */
  146. pr_info(KUNIT_SUBTEST_INDENT "KTAP version 1\n");
  147. pr_info(KUNIT_SUBTEST_INDENT "# Subtest: %s\n",
  148. suite->name);
  149. pr_info(KUNIT_SUBTEST_INDENT "1..%zd\n",
  150. kunit_suite_num_test_cases(suite));
  151. }
  152. static void kunit_print_ok_not_ok(void *test_or_suite,
  153. bool is_test,
  154. enum kunit_status status,
  155. size_t test_number,
  156. const char *description,
  157. const char *directive)
  158. {
  159. struct kunit_suite *suite = is_test ? NULL : test_or_suite;
  160. struct kunit *test = is_test ? test_or_suite : NULL;
  161. const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
  162. /*
  163. * We do not log the test suite results as doing so would
  164. * mean debugfs display would consist of an incorrect test
  165. * number. Hence directly printk the suite result, and we will
  166. * separately seq_printf() the suite results for the debugfs
  167. * representation.
  168. */
  169. if (suite)
  170. pr_info("%s %zd %s%s%s\n",
  171. kunit_status_to_ok_not_ok(status),
  172. test_number, description, directive_header,
  173. (status == KUNIT_SKIPPED) ? directive : "");
  174. else
  175. kunit_log(KERN_INFO, test,
  176. KUNIT_SUBTEST_INDENT "%s %zd %s%s%s",
  177. kunit_status_to_ok_not_ok(status),
  178. test_number, description, directive_header,
  179. (status == KUNIT_SKIPPED) ? directive : "");
  180. }
  181. enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
  182. {
  183. const struct kunit_case *test_case;
  184. enum kunit_status status = KUNIT_SKIPPED;
  185. if (suite->suite_init_err)
  186. return KUNIT_FAILURE;
  187. kunit_suite_for_each_test_case(suite, test_case) {
  188. if (test_case->status == KUNIT_FAILURE)
  189. return KUNIT_FAILURE;
  190. else if (test_case->status == KUNIT_SUCCESS)
  191. status = KUNIT_SUCCESS;
  192. }
  193. return status;
  194. }
  195. EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
  196. static size_t kunit_suite_counter = 1;
  197. static void kunit_print_suite_end(struct kunit_suite *suite)
  198. {
  199. kunit_print_ok_not_ok((void *)suite, false,
  200. kunit_suite_has_succeeded(suite),
  201. kunit_suite_counter++,
  202. suite->name,
  203. suite->status_comment);
  204. }
  205. unsigned int kunit_test_case_num(struct kunit_suite *suite,
  206. struct kunit_case *test_case)
  207. {
  208. struct kunit_case *tc;
  209. unsigned int i = 1;
  210. kunit_suite_for_each_test_case(suite, tc) {
  211. if (tc == test_case)
  212. return i;
  213. i++;
  214. }
  215. return 0;
  216. }
  217. EXPORT_SYMBOL_GPL(kunit_test_case_num);
  218. static void kunit_print_string_stream(struct kunit *test,
  219. struct string_stream *stream)
  220. {
  221. struct string_stream_fragment *fragment;
  222. char *buf;
  223. if (string_stream_is_empty(stream))
  224. return;
  225. buf = string_stream_get_string(stream);
  226. if (!buf) {
  227. kunit_err(test,
  228. "Could not allocate buffer, dumping stream:\n");
  229. list_for_each_entry(fragment, &stream->fragments, node) {
  230. kunit_err(test, "%s", fragment->fragment);
  231. }
  232. kunit_err(test, "\n");
  233. } else {
  234. kunit_err(test, "%s", buf);
  235. kunit_kfree(test, buf);
  236. }
  237. }
  238. static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
  239. enum kunit_assert_type type, const struct kunit_assert *assert,
  240. assert_format_t assert_format, const struct va_format *message)
  241. {
  242. struct string_stream *stream;
  243. kunit_set_failure(test);
  244. stream = alloc_string_stream(test, GFP_KERNEL);
  245. if (IS_ERR(stream)) {
  246. WARN(true,
  247. "Could not allocate stream to print failed assertion in %s:%d\n",
  248. loc->file,
  249. loc->line);
  250. return;
  251. }
  252. kunit_assert_prologue(loc, type, stream);
  253. assert_format(assert, message, stream);
  254. kunit_print_string_stream(test, stream);
  255. string_stream_destroy(stream);
  256. }
  257. static void __noreturn kunit_abort(struct kunit *test)
  258. {
  259. kunit_try_catch_throw(&test->try_catch); /* Does not return. */
  260. /*
  261. * Throw could not abort from test.
  262. *
  263. * XXX: we should never reach this line! As kunit_try_catch_throw is
  264. * marked __noreturn.
  265. */
  266. WARN_ONCE(true, "Throw could not abort from test!\n");
  267. }
  268. void kunit_do_failed_assertion(struct kunit *test,
  269. const struct kunit_loc *loc,
  270. enum kunit_assert_type type,
  271. const struct kunit_assert *assert,
  272. assert_format_t assert_format,
  273. const char *fmt, ...)
  274. {
  275. va_list args;
  276. struct va_format message;
  277. va_start(args, fmt);
  278. message.fmt = fmt;
  279. message.va = &args;
  280. kunit_fail(test, loc, type, assert, assert_format, &message);
  281. va_end(args);
  282. if (type == KUNIT_ASSERTION)
  283. kunit_abort(test);
  284. }
  285. EXPORT_SYMBOL_GPL(kunit_do_failed_assertion);
  286. void kunit_init_test(struct kunit *test, const char *name, char *log)
  287. {
  288. spin_lock_init(&test->lock);
  289. INIT_LIST_HEAD(&test->resources);
  290. #ifdef CONFIG_SEC_KUNIT
  291. INIT_LIST_HEAD(&test->post_conditions);
  292. #endif
  293. test->name = name;
  294. test->log = log;
  295. if (test->log)
  296. test->log[0] = '\0';
  297. test->status = KUNIT_SUCCESS;
  298. test->status_comment[0] = '\0';
  299. }
  300. EXPORT_SYMBOL_GPL(kunit_init_test);
  301. /*
  302. * Initializes and runs test case. Does not clean up or do post validations.
  303. */
  304. static void kunit_run_case_internal(struct kunit *test,
  305. struct kunit_suite *suite,
  306. struct kunit_case *test_case)
  307. {
  308. #ifdef CONFIG_SEC_KUNIT
  309. struct test_initcall *initcall;
  310. int ret;
  311. list_for_each_entry(initcall, &test_global_context.initcalls, node) {
  312. ret = initcall->init(initcall, test);
  313. if (ret) {
  314. kunit_err(test, "failed to initialize: %d", ret);
  315. kunit_set_failure(test);
  316. return;
  317. }
  318. }
  319. #endif
  320. if (suite->init) {
  321. int ret;
  322. ret = suite->init(test);
  323. if (ret) {
  324. kunit_err(test, "failed to initialize: %d\n", ret);
  325. kunit_set_failure(test);
  326. return;
  327. }
  328. }
  329. test_case->run_case(test);
  330. }
  331. static void kunit_case_internal_cleanup(struct kunit *test)
  332. {
  333. #ifdef CONFIG_SEC_KUNIT
  334. struct test_initcall *initcall;
  335. list_for_each_entry(initcall, &test_global_context.initcalls, node) {
  336. initcall->exit(initcall);
  337. }
  338. #endif
  339. kunit_cleanup(test);
  340. }
  341. /*
  342. * Performs post validations and cleanup after a test case was run.
  343. * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
  344. */
  345. static void kunit_run_case_cleanup(struct kunit *test,
  346. struct kunit_suite *suite)
  347. {
  348. #ifdef CONFIG_SEC_KUNIT
  349. struct kunit_post_condition *condition, *condition_safe;
  350. list_for_each_entry_safe(condition,
  351. condition_safe,
  352. &test->post_conditions,
  353. node) {
  354. condition->validate(condition);
  355. list_del(&condition->node);
  356. }
  357. #endif
  358. if (suite->exit)
  359. suite->exit(test);
  360. kunit_case_internal_cleanup(test);
  361. }
  362. struct kunit_try_catch_context {
  363. struct kunit *test;
  364. struct kunit_suite *suite;
  365. struct kunit_case *test_case;
  366. };
  367. static void kunit_try_run_case(void *data)
  368. {
  369. struct kunit_try_catch_context *ctx = data;
  370. struct kunit *test = ctx->test;
  371. struct kunit_suite *suite = ctx->suite;
  372. struct kunit_case *test_case = ctx->test_case;
  373. current->kunit_test = test;
  374. /*
  375. * kunit_run_case_internal may encounter a fatal error; if it does,
  376. * abort will be called, this thread will exit, and finally the parent
  377. * thread will resume control and handle any necessary clean up.
  378. */
  379. kunit_run_case_internal(test, suite, test_case);
  380. /* This line may never be reached. */
  381. kunit_run_case_cleanup(test, suite);
  382. }
  383. static void kunit_catch_run_case(void *data)
  384. {
  385. struct kunit_try_catch_context *ctx = data;
  386. struct kunit *test = ctx->test;
  387. struct kunit_suite *suite = ctx->suite;
  388. int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
  389. if (try_exit_code) {
  390. kunit_set_failure(test);
  391. /*
  392. * Test case could not finish, we have no idea what state it is
  393. * in, so don't do clean up.
  394. */
  395. if (try_exit_code == -ETIMEDOUT) {
  396. kunit_err(test, "test case timed out\n");
  397. /*
  398. * Unknown internal error occurred preventing test case from
  399. * running, so there is nothing to clean up.
  400. */
  401. } else {
  402. kunit_err(test, "internal error occurred preventing test case from running: %d\n",
  403. try_exit_code);
  404. }
  405. return;
  406. }
  407. /*
  408. * Test case was run, but aborted. It is the test case's business as to
  409. * whether it failed or not, we just need to clean up.
  410. */
  411. kunit_run_case_cleanup(test, suite);
  412. }
  413. /*
  414. * Performs all logic to run a test case. It also catches most errors that
  415. * occur in a test case and reports them as failures.
  416. */
  417. static void kunit_run_case_catch_errors(struct kunit_suite *suite,
  418. struct kunit_case *test_case,
  419. struct kunit *test)
  420. {
  421. struct kunit_try_catch_context context;
  422. struct kunit_try_catch *try_catch;
  423. kunit_init_test(test, test_case->name, test_case->log);
  424. try_catch = &test->try_catch;
  425. kunit_try_catch_init(try_catch,
  426. test,
  427. kunit_try_run_case,
  428. kunit_catch_run_case);
  429. context.test = test;
  430. context.suite = suite;
  431. context.test_case = test_case;
  432. kunit_try_catch_run(try_catch, &context);
  433. /* Propagate the parameter result to the test case. */
  434. if (test->status == KUNIT_FAILURE)
  435. test_case->status = KUNIT_FAILURE;
  436. else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
  437. test_case->status = KUNIT_SUCCESS;
  438. }
  439. static void kunit_print_suite_stats(struct kunit_suite *suite,
  440. struct kunit_result_stats suite_stats,
  441. struct kunit_result_stats param_stats)
  442. {
  443. if (kunit_should_print_stats(suite_stats)) {
  444. kunit_log(KERN_INFO, suite,
  445. "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
  446. suite->name,
  447. suite_stats.passed,
  448. suite_stats.failed,
  449. suite_stats.skipped,
  450. suite_stats.total);
  451. }
  452. if (kunit_should_print_stats(param_stats)) {
  453. kunit_log(KERN_INFO, suite,
  454. "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
  455. param_stats.passed,
  456. param_stats.failed,
  457. param_stats.skipped,
  458. param_stats.total);
  459. }
  460. }
  461. static void kunit_update_stats(struct kunit_result_stats *stats,
  462. enum kunit_status status)
  463. {
  464. switch (status) {
  465. case KUNIT_SUCCESS:
  466. stats->passed++;
  467. break;
  468. case KUNIT_SKIPPED:
  469. stats->skipped++;
  470. break;
  471. case KUNIT_FAILURE:
  472. stats->failed++;
  473. break;
  474. }
  475. stats->total++;
  476. }
  477. static void kunit_accumulate_stats(struct kunit_result_stats *total,
  478. struct kunit_result_stats add)
  479. {
  480. total->passed += add.passed;
  481. total->skipped += add.skipped;
  482. total->failed += add.failed;
  483. total->total += add.total;
  484. }
  485. int kunit_run_tests(struct kunit_suite *suite)
  486. {
  487. char param_desc[KUNIT_PARAM_DESC_SIZE];
  488. struct kunit_case *test_case;
  489. struct kunit_result_stats suite_stats = { 0 };
  490. struct kunit_result_stats total_stats = { 0 };
  491. /* Taint the kernel so we know we've run tests. */
  492. add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
  493. if (suite->suite_init) {
  494. suite->suite_init_err = suite->suite_init(suite);
  495. if (suite->suite_init_err) {
  496. kunit_err(suite, KUNIT_SUBTEST_INDENT
  497. "# failed to initialize (%d)", suite->suite_init_err);
  498. goto suite_end;
  499. }
  500. }
  501. kunit_print_suite_start(suite);
  502. kunit_suite_for_each_test_case(suite, test_case) {
  503. struct kunit test = { .param_value = NULL, .param_index = 0 };
  504. struct kunit_result_stats param_stats = { 0 };
  505. test_case->status = KUNIT_SKIPPED;
  506. if (!test_case->generate_params) {
  507. /* Non-parameterised test. */
  508. kunit_run_case_catch_errors(suite, test_case, &test);
  509. kunit_update_stats(&param_stats, test.status);
  510. } else {
  511. /* Get initial param. */
  512. param_desc[0] = '\0';
  513. test.param_value = test_case->generate_params(NULL, param_desc);
  514. kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
  515. "KTAP version 1\n");
  516. kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
  517. "# Subtest: %s", test_case->name);
  518. while (test.param_value) {
  519. kunit_run_case_catch_errors(suite, test_case, &test);
  520. if (param_desc[0] == '\0') {
  521. snprintf(param_desc, sizeof(param_desc),
  522. "param-%d", test.param_index);
  523. }
  524. kunit_log(KERN_INFO, &test,
  525. KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
  526. "%s %d %s",
  527. kunit_status_to_ok_not_ok(test.status),
  528. test.param_index + 1, param_desc);
  529. /* Get next param. */
  530. param_desc[0] = '\0';
  531. test.param_value = test_case->generate_params(test.param_value, param_desc);
  532. test.param_index++;
  533. kunit_update_stats(&param_stats, test.status);
  534. }
  535. }
  536. kunit_print_test_stats(&test, param_stats);
  537. kunit_print_ok_not_ok(&test, true, test_case->status,
  538. kunit_test_case_num(suite, test_case),
  539. test_case->name,
  540. test.status_comment);
  541. kunit_update_stats(&suite_stats, test_case->status);
  542. kunit_accumulate_stats(&total_stats, param_stats);
  543. }
  544. if (suite->suite_exit)
  545. suite->suite_exit(suite);
  546. kunit_print_suite_stats(suite, suite_stats, total_stats);
  547. suite_end:
  548. kunit_print_suite_end(suite);
  549. return 0;
  550. }
  551. EXPORT_SYMBOL_GPL(kunit_run_tests);
  552. static void kunit_init_suite(struct kunit_suite *suite)
  553. {
  554. kunit_debugfs_create_suite(suite);
  555. suite->status_comment[0] = '\0';
  556. suite->suite_init_err = 0;
  557. }
  558. bool kunit_enabled(void)
  559. {
  560. return enable_param;
  561. }
  562. int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
  563. {
  564. unsigned int i;
  565. if (!kunit_enabled() && num_suites > 0) {
  566. pr_info("kunit: disabled\n");
  567. return 0;
  568. }
  569. for (i = 0; i < num_suites; i++) {
  570. kunit_init_suite(suites[i]);
  571. kunit_run_tests(suites[i]);
  572. }
  573. return 0;
  574. }
  575. EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
  576. static void kunit_exit_suite(struct kunit_suite *suite)
  577. {
  578. kunit_debugfs_destroy_suite(suite);
  579. }
  580. void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
  581. {
  582. unsigned int i;
  583. if (!kunit_enabled())
  584. return;
  585. for (i = 0; i < num_suites; i++)
  586. kunit_exit_suite(suites[i]);
  587. kunit_suite_counter = 1;
  588. }
  589. EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
  590. #ifdef CONFIG_MODULES
  591. static void kunit_module_init(struct module *mod)
  592. {
  593. __kunit_test_suites_init(mod->kunit_suites, mod->num_kunit_suites);
  594. }
  595. static void kunit_module_exit(struct module *mod)
  596. {
  597. __kunit_test_suites_exit(mod->kunit_suites, mod->num_kunit_suites);
  598. }
  599. static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
  600. void *data)
  601. {
  602. struct module *mod = data;
  603. switch (val) {
  604. case MODULE_STATE_LIVE:
  605. break;
  606. case MODULE_STATE_GOING:
  607. kunit_module_exit(mod);
  608. break;
  609. case MODULE_STATE_COMING:
  610. kunit_module_init(mod);
  611. break;
  612. case MODULE_STATE_UNFORMED:
  613. break;
  614. }
  615. return 0;
  616. }
  617. static struct notifier_block kunit_mod_nb = {
  618. .notifier_call = kunit_module_notify,
  619. .priority = 0,
  620. };
  621. #endif
  622. struct kunit_kmalloc_array_params {
  623. size_t n;
  624. size_t size;
  625. gfp_t gfp;
  626. };
  627. static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context)
  628. {
  629. struct kunit_kmalloc_array_params *params = context;
  630. res->data = kmalloc_array(params->n, params->size, params->gfp);
  631. if (!res->data)
  632. return -ENOMEM;
  633. return 0;
  634. }
  635. static void kunit_kmalloc_array_free(struct kunit_resource *res)
  636. {
  637. kfree(res->data);
  638. }
  639. void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
  640. {
  641. struct kunit_kmalloc_array_params params = {
  642. .size = size,
  643. .n = n,
  644. .gfp = gfp
  645. };
  646. return kunit_alloc_resource(test,
  647. kunit_kmalloc_array_init,
  648. kunit_kmalloc_array_free,
  649. gfp,
  650. &params);
  651. }
  652. EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
  653. static inline bool kunit_kfree_match(struct kunit *test,
  654. struct kunit_resource *res, void *match_data)
  655. {
  656. /* Only match resources allocated with kunit_kmalloc() and friends. */
  657. return res->free == kunit_kmalloc_array_free && res->data == match_data;
  658. }
  659. void kunit_kfree(struct kunit *test, const void *ptr)
  660. {
  661. if (!ptr)
  662. return;
  663. if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr))
  664. KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr);
  665. }
  666. EXPORT_SYMBOL_GPL(kunit_kfree);
  667. void kunit_cleanup(struct kunit *test)
  668. {
  669. struct kunit_resource *res;
  670. unsigned long flags;
  671. /*
  672. * test->resources is a stack - each allocation must be freed in the
  673. * reverse order from which it was added since one resource may depend
  674. * on another for its entire lifetime.
  675. * Also, we cannot use the normal list_for_each constructs, even the
  676. * safe ones because *arbitrary* nodes may be deleted when
  677. * kunit_resource_free is called; the list_for_each_safe variants only
  678. * protect against the current node being deleted, not the next.
  679. */
  680. while (true) {
  681. spin_lock_irqsave(&test->lock, flags);
  682. if (list_empty(&test->resources)) {
  683. spin_unlock_irqrestore(&test->lock, flags);
  684. break;
  685. }
  686. res = list_last_entry(&test->resources,
  687. struct kunit_resource,
  688. node);
  689. /*
  690. * Need to unlock here as a resource may remove another
  691. * resource, and this can't happen if the test->lock
  692. * is held.
  693. */
  694. spin_unlock_irqrestore(&test->lock, flags);
  695. kunit_remove_resource(test, res);
  696. }
  697. current->kunit_test = NULL;
  698. }
  699. EXPORT_SYMBOL_GPL(kunit_cleanup);
  700. static int __init kunit_init(void)
  701. {
  702. kunit_debugfs_init();
  703. #ifdef CONFIG_MODULES
  704. return register_module_notifier(&kunit_mod_nb);
  705. #else
  706. return 0;
  707. #endif
  708. }
  709. late_initcall(kunit_init);
  710. static void __exit kunit_exit(void)
  711. {
  712. #ifdef CONFIG_MODULES
  713. unregister_module_notifier(&kunit_mod_nb);
  714. #endif
  715. kunit_debugfs_cleanup();
  716. }
  717. module_exit(kunit_exit);
  718. MODULE_LICENSE("GPL v2");