^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Base unit test (KUnit) API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2019, Google LLC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Brendan Higgins <brendanhiggins@google.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <kunit/test.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "debugfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "string-stream.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "try-catch-impl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Append formatted message to log, size of which is limited to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * KUNIT_LOG_SIZE bytes (including null terminating byte).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) void kunit_log_append(char *log, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) char line[KUNIT_LOG_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int len_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!log)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) len_left = KUNIT_LOG_SIZE - strlen(log) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (len_left <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) vsnprintf(line, sizeof(line), fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) strncat(log, line, len_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) EXPORT_SYMBOL_GPL(kunit_log_append);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct kunit_case *test_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) size_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) kunit_suite_for_each_test_case(suite, test_case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static void kunit_print_subtest_start(struct kunit_suite *suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) suite->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) kunit_suite_num_test_cases(suite));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static void kunit_print_ok_not_ok(void *test_or_suite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) bool is_test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) bool is_ok,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) size_t test_number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) const char *description)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct kunit_suite *suite = is_test ? NULL : test_or_suite;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct kunit *test = is_test ? test_or_suite : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * We do not log the test suite results as doing so would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * mean debugfs display would consist of the test suite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * description and status prior to individual test results.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * Hence directly printk the suite status, and we will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * separately seq_printf() the suite status for the debugfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * representation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) pr_info("%s %zd - %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) kunit_status_to_string(is_ok),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) test_number, description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) kunit_log(KERN_INFO, test, KUNIT_SUBTEST_INDENT "%s %zd - %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) kunit_status_to_string(is_ok),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) test_number, description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) bool kunit_suite_has_succeeded(struct kunit_suite *suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) const struct kunit_case *test_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) kunit_suite_for_each_test_case(suite, test_case) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!test_case->success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static void kunit_print_subtest_end(struct kunit_suite *suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static size_t kunit_suite_counter = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) kunit_print_ok_not_ok((void *)suite, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) kunit_suite_has_succeeded(suite),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) kunit_suite_counter++,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) suite->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned int kunit_test_case_num(struct kunit_suite *suite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct kunit_case *test_case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct kunit_case *tc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned int i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) kunit_suite_for_each_test_case(suite, tc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (tc == test_case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) EXPORT_SYMBOL_GPL(kunit_test_case_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void kunit_print_string_stream(struct kunit *test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct string_stream *stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct string_stream_fragment *fragment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (string_stream_is_empty(stream))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) buf = string_stream_get_string(stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) kunit_err(test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) "Could not allocate buffer, dumping stream:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) list_for_each_entry(fragment, &stream->fragments, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) kunit_err(test, "%s", fragment->fragment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) kunit_err(test, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) kunit_err(test, "%s", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) kunit_kfree(test, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct string_stream *stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) kunit_set_failure(test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) stream = alloc_string_stream(test, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!stream) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) WARN(true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) "Could not allocate stream to print failed assertion in %s:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) assert->file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) assert->line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) assert->format(assert, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) kunit_print_string_stream(test, stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) WARN_ON(string_stream_destroy(stream));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static void __noreturn kunit_abort(struct kunit *test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) kunit_try_catch_throw(&test->try_catch); /* Does not return. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * Throw could not abort from test.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * XXX: we should never reach this line! As kunit_try_catch_throw is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * marked __noreturn.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) WARN_ONCE(true, "Throw could not abort from test!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) void kunit_do_assertion(struct kunit *test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct kunit_assert *assert,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) bool pass,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (pass)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) assert->message.fmt = fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) assert->message.va = &args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) kunit_fail(test, assert);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (assert->type == KUNIT_ASSERTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) kunit_abort(test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) EXPORT_SYMBOL_GPL(kunit_do_assertion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) void kunit_init_test(struct kunit *test, const char *name, char *log)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) spin_lock_init(&test->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) INIT_LIST_HEAD(&test->resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) test->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) test->log = log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (test->log)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) test->log[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) test->success = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) EXPORT_SYMBOL_GPL(kunit_init_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * Initializes and runs test case. Does not clean up or do post validations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static void kunit_run_case_internal(struct kunit *test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct kunit_suite *suite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct kunit_case *test_case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (suite->init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ret = suite->init(test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) kunit_err(test, "failed to initialize: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) kunit_set_failure(test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) test_case->run_case(test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static void kunit_case_internal_cleanup(struct kunit *test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) kunit_cleanup(test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * Performs post validations and cleanup after a test case was run.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static void kunit_run_case_cleanup(struct kunit *test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct kunit_suite *suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (suite->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) suite->exit(test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) kunit_case_internal_cleanup(test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct kunit_try_catch_context {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct kunit *test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct kunit_suite *suite;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct kunit_case *test_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static void kunit_try_run_case(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) struct kunit_try_catch_context *ctx = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct kunit *test = ctx->test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct kunit_suite *suite = ctx->suite;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct kunit_case *test_case = ctx->test_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) #if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) current->kunit_test = test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) #endif /* IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * kunit_run_case_internal may encounter a fatal error; if it does,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * abort will be called, this thread will exit, and finally the parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * thread will resume control and handle any necessary clean up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) kunit_run_case_internal(test, suite, test_case);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /* This line may never be reached. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) kunit_run_case_cleanup(test, suite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static void kunit_catch_run_case(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct kunit_try_catch_context *ctx = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct kunit *test = ctx->test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct kunit_suite *suite = ctx->suite;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (try_exit_code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) kunit_set_failure(test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * Test case could not finish, we have no idea what state it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * in, so don't do clean up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (try_exit_code == -ETIMEDOUT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) kunit_err(test, "test case timed out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * Unknown internal error occurred preventing test case from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * running, so there is nothing to clean up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) kunit_err(test, "internal error occurred preventing test case from running: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) try_exit_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * Test case was run, but aborted. It is the test case's business as to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * whether it failed or not, we just need to clean up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) kunit_run_case_cleanup(test, suite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * Performs all logic to run a test case. It also catches most errors that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * occur in a test case and reports them as failures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static void kunit_run_case_catch_errors(struct kunit_suite *suite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct kunit_case *test_case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct kunit_try_catch_context context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct kunit_try_catch *try_catch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct kunit test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) kunit_init_test(&test, test_case->name, test_case->log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) try_catch = &test.try_catch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) kunit_try_catch_init(try_catch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) &test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) kunit_try_run_case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) kunit_catch_run_case);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) context.test = &test;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) context.suite = suite;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) context.test_case = test_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) kunit_try_catch_run(try_catch, &context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) test_case->success = test.success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) kunit_print_ok_not_ok(&test, true, test_case->success,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) kunit_test_case_num(suite, test_case),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) test_case->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int kunit_run_tests(struct kunit_suite *suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct kunit_case *test_case;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) kunit_print_subtest_start(suite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) kunit_suite_for_each_test_case(suite, test_case)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) kunit_run_case_catch_errors(suite, test_case);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) kunit_print_subtest_end(suite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) EXPORT_SYMBOL_GPL(kunit_run_tests);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static void kunit_init_suite(struct kunit_suite *suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) kunit_debugfs_create_suite(suite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) int __kunit_test_suites_init(struct kunit_suite * const * const suites)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) for (i = 0; suites[i] != NULL; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) kunit_init_suite(suites[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) kunit_run_tests(suites[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) static void kunit_exit_suite(struct kunit_suite *suite)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) kunit_debugfs_destroy_suite(suite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) void __kunit_test_suites_exit(struct kunit_suite **suites)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) for (i = 0; suites[i] != NULL; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) kunit_exit_suite(suites[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * Used for static resources and when a kunit_resource * has been created by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * kunit_alloc_resource(). When an init function is supplied, @data is passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * into the init function; otherwise, we simply set the resource data field to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * the data value passed in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) int kunit_add_resource(struct kunit *test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) kunit_resource_init_t init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) kunit_resource_free_t free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct kunit_resource *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) res->free = free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) kref_init(&res->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ret = init(res, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) res->data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) spin_lock(&test->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) list_add_tail(&res->node, &test->resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /* refcount for list is established by kref_init() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) spin_unlock(&test->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) EXPORT_SYMBOL_GPL(kunit_add_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int kunit_add_named_resource(struct kunit *test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) kunit_resource_init_t init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) kunit_resource_free_t free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct kunit_resource *res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) struct kunit_resource *existing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (!name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) existing = kunit_find_named_resource(test, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (existing) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) kunit_put_resource(existing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) res->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return kunit_add_resource(test, init, free, res, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) EXPORT_SYMBOL_GPL(kunit_add_named_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) kunit_resource_init_t init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) kunit_resource_free_t free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) gfp_t internal_gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) struct kunit_resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) res = kzalloc(sizeof(*res), internal_gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) ret = kunit_add_resource(test, init, free, res, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * bump refcount for get; kunit_resource_put() should be called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * when done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) kunit_get_resource(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) spin_lock(&test->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) list_del(&res->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) spin_unlock(&test->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) kunit_put_resource(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) EXPORT_SYMBOL_GPL(kunit_remove_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) void *match_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct kunit_resource *res = kunit_find_resource(test, match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) match_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) kunit_remove_resource(test, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) /* We have a reference also via _find(); drop it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) kunit_put_resource(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) EXPORT_SYMBOL_GPL(kunit_destroy_resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct kunit_kmalloc_params {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) gfp_t gfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct kunit_kmalloc_params *params = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) res->data = kmalloc(params->size, params->gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (!res->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) static void kunit_kmalloc_free(struct kunit_resource *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) kfree(res->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) struct kunit_kmalloc_params params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) .size = size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) .gfp = gfp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return kunit_alloc_resource(test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) kunit_kmalloc_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) kunit_kmalloc_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) ¶ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) EXPORT_SYMBOL_GPL(kunit_kmalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) void kunit_kfree(struct kunit *test, const void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) struct kunit_resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) res = kunit_find_resource(test, kunit_resource_instance_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) (void *)ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * Removing the resource from the list of resources drops the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * reference count to 1; the final put will trigger the free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) kunit_remove_resource(test, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) kunit_put_resource(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) EXPORT_SYMBOL_GPL(kunit_kfree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) void kunit_cleanup(struct kunit *test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) struct kunit_resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * test->resources is a stack - each allocation must be freed in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * reverse order from which it was added since one resource may depend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * on another for its entire lifetime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * Also, we cannot use the normal list_for_each constructs, even the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * safe ones because *arbitrary* nodes may be deleted when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * kunit_resource_free is called; the list_for_each_safe variants only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * protect against the current node being deleted, not the next.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) spin_lock(&test->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (list_empty(&test->resources)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) spin_unlock(&test->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) res = list_last_entry(&test->resources,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) struct kunit_resource,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * Need to unlock here as a resource may remove another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * resource, and this can't happen if the test->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * is held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) spin_unlock(&test->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) kunit_remove_resource(test, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) #if (IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) current->kunit_test = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) #endif /* IS_ENABLED(CONFIG_KASAN) && IS_ENABLED(CONFIG_KUNIT)*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) EXPORT_SYMBOL_GPL(kunit_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) static int __init kunit_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) kunit_debugfs_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) late_initcall(kunit_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) static void __exit kunit_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) kunit_debugfs_cleanup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) module_exit(kunit_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) MODULE_LICENSE("GPL v2");