Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 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 <brendanhiggins@google.com> |
| 7 | */ |
| 8 | |
| 9 | #include <kunit/test.h> |
Uriel Guajardo | 359a376 | 2021-03-11 07:23:13 -0800 | [diff] [blame] | 10 | #include <kunit/test-bug.h> |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 12 | #include <linux/kref.h> |
David Gow | acd8e84 | 2021-08-03 22:08:08 -0700 | [diff] [blame] | 13 | #include <linux/moduleparam.h> |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 14 | #include <linux/sched/debug.h> |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 15 | #include <linux/sched.h> |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 16 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 17 | #include "debugfs.h" |
Alan Maguire | 109fb06 | 2020-01-06 22:28:18 +0000 | [diff] [blame] | 18 | #include "string-stream.h" |
Alan Maguire | 9bbb11c | 2020-01-06 22:28:19 +0000 | [diff] [blame] | 19 | #include "try-catch-impl.h" |
Alan Maguire | 109fb06 | 2020-01-06 22:28:18 +0000 | [diff] [blame] | 20 | |
Uriel Guajardo | 359a376 | 2021-03-11 07:23:13 -0800 | [diff] [blame] | 21 | #if IS_BUILTIN(CONFIG_KUNIT) |
| 22 | /* |
| 23 | * Fail the current test and print an error message to the log. |
| 24 | */ |
| 25 | void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...) |
| 26 | { |
| 27 | va_list args; |
| 28 | int len; |
| 29 | char *buffer; |
| 30 | |
| 31 | if (!current->kunit_test) |
| 32 | return; |
| 33 | |
| 34 | kunit_set_failure(current->kunit_test); |
| 35 | |
| 36 | /* kunit_err() only accepts literals, so evaluate the args first. */ |
| 37 | va_start(args, fmt); |
| 38 | len = vsnprintf(NULL, 0, fmt, args) + 1; |
| 39 | va_end(args); |
| 40 | |
| 41 | buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL); |
| 42 | if (!buffer) |
| 43 | return; |
| 44 | |
| 45 | va_start(args, fmt); |
| 46 | vsnprintf(buffer, len, fmt, args); |
| 47 | va_end(args); |
| 48 | |
| 49 | kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer); |
| 50 | kunit_kfree(current->kunit_test, buffer); |
| 51 | } |
| 52 | EXPORT_SYMBOL_GPL(__kunit_fail_current_test); |
| 53 | #endif |
| 54 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 55 | /* |
David Gow | acd8e84 | 2021-08-03 22:08:08 -0700 | [diff] [blame] | 56 | * KUnit statistic mode: |
| 57 | * 0 - disabled |
| 58 | * 1 - only when there is more than one subtest |
| 59 | * 2 - enabled |
| 60 | */ |
| 61 | static int kunit_stats_enabled = 1; |
| 62 | module_param_named(stats_enabled, kunit_stats_enabled, int, 0644); |
| 63 | MODULE_PARM_DESC(stats_enabled, |
| 64 | "Print test stats: never (0), only for multiple subtests (1), or always (2)"); |
| 65 | |
| 66 | struct kunit_result_stats { |
| 67 | unsigned long passed; |
| 68 | unsigned long skipped; |
| 69 | unsigned long failed; |
| 70 | unsigned long total; |
| 71 | }; |
| 72 | |
| 73 | static bool kunit_should_print_stats(struct kunit_result_stats stats) |
| 74 | { |
| 75 | if (kunit_stats_enabled == 0) |
| 76 | return false; |
| 77 | |
| 78 | if (kunit_stats_enabled == 2) |
| 79 | return true; |
| 80 | |
| 81 | return (stats.total > 1); |
| 82 | } |
| 83 | |
| 84 | static void kunit_print_test_stats(struct kunit *test, |
| 85 | struct kunit_result_stats stats) |
| 86 | { |
| 87 | if (!kunit_should_print_stats(stats)) |
| 88 | return; |
| 89 | |
| 90 | kunit_log(KERN_INFO, test, |
| 91 | KUNIT_SUBTEST_INDENT |
| 92 | "# %s: pass:%lu fail:%lu skip:%lu total:%lu", |
| 93 | test->name, |
| 94 | stats.passed, |
| 95 | stats.failed, |
| 96 | stats.skipped, |
| 97 | stats.total); |
| 98 | } |
| 99 | |
| 100 | /* |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 101 | * Append formatted message to log, size of which is limited to |
| 102 | * KUNIT_LOG_SIZE bytes (including null terminating byte). |
| 103 | */ |
| 104 | void kunit_log_append(char *log, const char *fmt, ...) |
| 105 | { |
| 106 | char line[KUNIT_LOG_SIZE]; |
| 107 | va_list args; |
| 108 | int len_left; |
| 109 | |
| 110 | if (!log) |
| 111 | return; |
| 112 | |
| 113 | len_left = KUNIT_LOG_SIZE - strlen(log) - 1; |
| 114 | if (len_left <= 0) |
| 115 | return; |
| 116 | |
| 117 | va_start(args, fmt); |
| 118 | vsnprintf(line, sizeof(line), fmt, args); |
| 119 | va_end(args); |
| 120 | |
| 121 | strncat(log, line, len_left); |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(kunit_log_append); |
| 124 | |
| 125 | size_t kunit_suite_num_test_cases(struct kunit_suite *suite) |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 126 | { |
| 127 | struct kunit_case *test_case; |
| 128 | size_t len = 0; |
| 129 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 130 | kunit_suite_for_each_test_case(suite, test_case) |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 131 | len++; |
| 132 | |
| 133 | return len; |
| 134 | } |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 135 | EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 136 | |
| 137 | static void kunit_print_subtest_start(struct kunit_suite *suite) |
| 138 | { |
Alan Maguire | c3bba69 | 2020-03-26 14:25:09 +0000 | [diff] [blame] | 139 | kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "# Subtest: %s", |
| 140 | suite->name); |
| 141 | kunit_log(KERN_INFO, suite, KUNIT_SUBTEST_INDENT "1..%zd", |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 142 | kunit_suite_num_test_cases(suite)); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 145 | static void kunit_print_ok_not_ok(void *test_or_suite, |
| 146 | bool is_test, |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 147 | enum kunit_status status, |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 148 | size_t test_number, |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 149 | const char *description, |
| 150 | const char *directive) |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 151 | { |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 152 | struct kunit_suite *suite = is_test ? NULL : test_or_suite; |
| 153 | struct kunit *test = is_test ? test_or_suite : NULL; |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 154 | const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : ""; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 155 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 156 | /* |
| 157 | * We do not log the test suite results as doing so would |
| 158 | * mean debugfs display would consist of the test suite |
| 159 | * description and status prior to individual test results. |
| 160 | * Hence directly printk the suite status, and we will |
| 161 | * separately seq_printf() the suite status for the debugfs |
| 162 | * representation. |
| 163 | */ |
| 164 | if (suite) |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 165 | pr_info("%s %zd - %s%s%s\n", |
| 166 | kunit_status_to_ok_not_ok(status), |
| 167 | test_number, description, directive_header, |
| 168 | (status == KUNIT_SKIPPED) ? directive : ""); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 169 | else |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 170 | kunit_log(KERN_INFO, test, |
| 171 | KUNIT_SUBTEST_INDENT "%s %zd - %s%s%s", |
| 172 | kunit_status_to_ok_not_ok(status), |
| 173 | test_number, description, directive_header, |
| 174 | (status == KUNIT_SKIPPED) ? directive : ""); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 175 | } |
| 176 | |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 177 | enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite) |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 178 | { |
| 179 | const struct kunit_case *test_case; |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 180 | enum kunit_status status = KUNIT_SKIPPED; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 181 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 182 | kunit_suite_for_each_test_case(suite, test_case) { |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 183 | if (test_case->status == KUNIT_FAILURE) |
| 184 | return KUNIT_FAILURE; |
| 185 | else if (test_case->status == KUNIT_SUCCESS) |
| 186 | status = KUNIT_SUCCESS; |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 187 | } |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 188 | |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 189 | return status; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 190 | } |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 191 | EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 192 | |
David Gow | 17ac23e | 2021-10-05 21:41:11 -0700 | [diff] [blame] | 193 | static size_t kunit_suite_counter = 1; |
| 194 | |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 195 | static void kunit_print_subtest_end(struct kunit_suite *suite) |
| 196 | { |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 197 | kunit_print_ok_not_ok((void *)suite, false, |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 198 | kunit_suite_has_succeeded(suite), |
| 199 | kunit_suite_counter++, |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 200 | suite->name, |
| 201 | suite->status_comment); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 204 | unsigned int kunit_test_case_num(struct kunit_suite *suite, |
| 205 | struct kunit_case *test_case) |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 206 | { |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 207 | struct kunit_case *tc; |
| 208 | unsigned int i = 1; |
| 209 | |
| 210 | kunit_suite_for_each_test_case(suite, tc) { |
| 211 | if (tc == test_case) |
| 212 | return i; |
| 213 | i++; |
| 214 | } |
| 215 | |
| 216 | return 0; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 217 | } |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 218 | EXPORT_SYMBOL_GPL(kunit_test_case_num); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 219 | |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 220 | static void kunit_print_string_stream(struct kunit *test, |
| 221 | struct string_stream *stream) |
| 222 | { |
| 223 | struct string_stream_fragment *fragment; |
| 224 | char *buf; |
| 225 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 226 | if (string_stream_is_empty(stream)) |
| 227 | return; |
| 228 | |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 229 | buf = string_stream_get_string(stream); |
| 230 | if (!buf) { |
| 231 | kunit_err(test, |
| 232 | "Could not allocate buffer, dumping stream:\n"); |
| 233 | list_for_each_entry(fragment, &stream->fragments, node) { |
Brendan Higgins | 741a98d | 2019-09-23 02:02:49 -0700 | [diff] [blame] | 234 | kunit_err(test, "%s", fragment->fragment); |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 235 | } |
| 236 | kunit_err(test, "\n"); |
| 237 | } else { |
Brendan Higgins | 741a98d | 2019-09-23 02:02:49 -0700 | [diff] [blame] | 238 | kunit_err(test, "%s", buf); |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 239 | kunit_kfree(test, buf); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static void kunit_fail(struct kunit *test, struct kunit_assert *assert) |
| 244 | { |
| 245 | struct string_stream *stream; |
| 246 | |
| 247 | kunit_set_failure(test); |
| 248 | |
| 249 | stream = alloc_string_stream(test, GFP_KERNEL); |
| 250 | if (!stream) { |
| 251 | WARN(true, |
| 252 | "Could not allocate stream to print failed assertion in %s:%d\n", |
| 253 | assert->file, |
| 254 | assert->line); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | assert->format(assert, stream); |
| 259 | |
| 260 | kunit_print_string_stream(test, stream); |
| 261 | |
| 262 | WARN_ON(string_stream_destroy(stream)); |
| 263 | } |
| 264 | |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 265 | static void __noreturn kunit_abort(struct kunit *test) |
| 266 | { |
| 267 | kunit_try_catch_throw(&test->try_catch); /* Does not return. */ |
| 268 | |
| 269 | /* |
| 270 | * Throw could not abort from test. |
| 271 | * |
| 272 | * XXX: we should never reach this line! As kunit_try_catch_throw is |
| 273 | * marked __noreturn. |
| 274 | */ |
| 275 | WARN_ONCE(true, "Throw could not abort from test!\n"); |
| 276 | } |
| 277 | |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 278 | void kunit_do_assertion(struct kunit *test, |
| 279 | struct kunit_assert *assert, |
| 280 | bool pass, |
| 281 | const char *fmt, ...) |
| 282 | { |
| 283 | va_list args; |
| 284 | |
| 285 | if (pass) |
| 286 | return; |
| 287 | |
| 288 | va_start(args, fmt); |
| 289 | |
| 290 | assert->message.fmt = fmt; |
| 291 | assert->message.va = &args; |
| 292 | |
| 293 | kunit_fail(test, assert); |
| 294 | |
| 295 | va_end(args); |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 296 | |
| 297 | if (assert->type == KUNIT_ASSERTION) |
| 298 | kunit_abort(test); |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 299 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 300 | EXPORT_SYMBOL_GPL(kunit_do_assertion); |
Brendan Higgins | 73cda7b | 2019-09-23 02:02:35 -0700 | [diff] [blame] | 301 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 302 | void kunit_init_test(struct kunit *test, const char *name, char *log) |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 303 | { |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 304 | spin_lock_init(&test->lock); |
| 305 | INIT_LIST_HEAD(&test->resources); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 306 | test->name = name; |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 307 | test->log = log; |
| 308 | if (test->log) |
| 309 | test->log[0] = '\0'; |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 310 | test->status = KUNIT_SUCCESS; |
| 311 | test->status_comment[0] = '\0'; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 312 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 313 | EXPORT_SYMBOL_GPL(kunit_init_test); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 314 | |
| 315 | /* |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 316 | * Initializes and runs test case. Does not clean up or do post validations. |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 317 | */ |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 318 | static void kunit_run_case_internal(struct kunit *test, |
| 319 | struct kunit_suite *suite, |
| 320 | struct kunit_case *test_case) |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 321 | { |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 322 | if (suite->init) { |
| 323 | int ret; |
| 324 | |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 325 | ret = suite->init(test); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 326 | if (ret) { |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 327 | kunit_err(test, "failed to initialize: %d\n", ret); |
| 328 | kunit_set_failure(test); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | } |
| 332 | |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 333 | test_case->run_case(test); |
| 334 | } |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 335 | |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 336 | static void kunit_case_internal_cleanup(struct kunit *test) |
| 337 | { |
| 338 | kunit_cleanup(test); |
| 339 | } |
| 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 | { |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 348 | if (suite->exit) |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 349 | suite->exit(test); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 350 | |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 351 | kunit_case_internal_cleanup(test); |
| 352 | } |
| 353 | |
| 354 | struct kunit_try_catch_context { |
| 355 | struct kunit *test; |
| 356 | struct kunit_suite *suite; |
| 357 | struct kunit_case *test_case; |
| 358 | }; |
| 359 | |
| 360 | static void kunit_try_run_case(void *data) |
| 361 | { |
| 362 | struct kunit_try_catch_context *ctx = data; |
| 363 | struct kunit *test = ctx->test; |
| 364 | struct kunit_suite *suite = ctx->suite; |
| 365 | struct kunit_case *test_case = ctx->test_case; |
| 366 | |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 367 | current->kunit_test = test; |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 368 | |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 369 | /* |
| 370 | * kunit_run_case_internal may encounter a fatal error; if it does, |
| 371 | * abort will be called, this thread will exit, and finally the parent |
| 372 | * thread will resume control and handle any necessary clean up. |
| 373 | */ |
| 374 | kunit_run_case_internal(test, suite, test_case); |
| 375 | /* This line may never be reached. */ |
| 376 | kunit_run_case_cleanup(test, suite); |
| 377 | } |
| 378 | |
| 379 | static void kunit_catch_run_case(void *data) |
| 380 | { |
| 381 | struct kunit_try_catch_context *ctx = data; |
| 382 | struct kunit *test = ctx->test; |
| 383 | struct kunit_suite *suite = ctx->suite; |
| 384 | int try_exit_code = kunit_try_catch_get_result(&test->try_catch); |
| 385 | |
| 386 | if (try_exit_code) { |
| 387 | kunit_set_failure(test); |
| 388 | /* |
| 389 | * Test case could not finish, we have no idea what state it is |
| 390 | * in, so don't do clean up. |
| 391 | */ |
| 392 | if (try_exit_code == -ETIMEDOUT) { |
| 393 | kunit_err(test, "test case timed out\n"); |
| 394 | /* |
| 395 | * Unknown internal error occurred preventing test case from |
| 396 | * running, so there is nothing to clean up. |
| 397 | */ |
| 398 | } else { |
| 399 | kunit_err(test, "internal error occurred preventing test case from running: %d\n", |
| 400 | try_exit_code); |
| 401 | } |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Test case was run, but aborted. It is the test case's business as to |
| 407 | * whether it failed or not, we just need to clean up. |
| 408 | */ |
| 409 | kunit_run_case_cleanup(test, suite); |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Performs all logic to run a test case. It also catches most errors that |
| 414 | * occur in a test case and reports them as failures. |
| 415 | */ |
| 416 | static void kunit_run_case_catch_errors(struct kunit_suite *suite, |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 417 | struct kunit_case *test_case, |
| 418 | struct kunit *test) |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 419 | { |
| 420 | struct kunit_try_catch_context context; |
| 421 | struct kunit_try_catch *try_catch; |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 422 | |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 423 | kunit_init_test(test, test_case->name, test_case->log); |
| 424 | try_catch = &test->try_catch; |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 425 | |
| 426 | kunit_try_catch_init(try_catch, |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 427 | test, |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 428 | kunit_try_run_case, |
| 429 | kunit_catch_run_case); |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 430 | context.test = test; |
Brendan Higgins | 5f3e062 | 2019-09-23 02:02:39 -0700 | [diff] [blame] | 431 | context.suite = suite; |
| 432 | context.test_case = test_case; |
| 433 | kunit_try_catch_run(try_catch, &context); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 434 | |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 435 | /* Propagate the parameter result to the test case. */ |
| 436 | if (test->status == KUNIT_FAILURE) |
| 437 | test_case->status = KUNIT_FAILURE; |
| 438 | else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS) |
| 439 | test_case->status = KUNIT_SUCCESS; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 440 | } |
| 441 | |
David Gow | acd8e84 | 2021-08-03 22:08:08 -0700 | [diff] [blame] | 442 | static void kunit_print_suite_stats(struct kunit_suite *suite, |
| 443 | struct kunit_result_stats suite_stats, |
| 444 | struct kunit_result_stats param_stats) |
| 445 | { |
| 446 | if (kunit_should_print_stats(suite_stats)) { |
| 447 | kunit_log(KERN_INFO, suite, |
| 448 | "# %s: pass:%lu fail:%lu skip:%lu total:%lu", |
| 449 | suite->name, |
| 450 | suite_stats.passed, |
| 451 | suite_stats.failed, |
| 452 | suite_stats.skipped, |
| 453 | suite_stats.total); |
| 454 | } |
| 455 | |
| 456 | if (kunit_should_print_stats(param_stats)) { |
| 457 | kunit_log(KERN_INFO, suite, |
| 458 | "# Totals: pass:%lu fail:%lu skip:%lu total:%lu", |
| 459 | param_stats.passed, |
| 460 | param_stats.failed, |
| 461 | param_stats.skipped, |
| 462 | param_stats.total); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | static void kunit_update_stats(struct kunit_result_stats *stats, |
| 467 | enum kunit_status status) |
| 468 | { |
| 469 | switch (status) { |
| 470 | case KUNIT_SUCCESS: |
| 471 | stats->passed++; |
| 472 | break; |
| 473 | case KUNIT_SKIPPED: |
| 474 | stats->skipped++; |
| 475 | break; |
| 476 | case KUNIT_FAILURE: |
| 477 | stats->failed++; |
| 478 | break; |
| 479 | } |
| 480 | |
| 481 | stats->total++; |
| 482 | } |
| 483 | |
| 484 | static void kunit_accumulate_stats(struct kunit_result_stats *total, |
| 485 | struct kunit_result_stats add) |
| 486 | { |
| 487 | total->passed += add.passed; |
| 488 | total->skipped += add.skipped; |
| 489 | total->failed += add.failed; |
| 490 | total->total += add.total; |
| 491 | } |
| 492 | |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 493 | int kunit_run_tests(struct kunit_suite *suite) |
| 494 | { |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 495 | char param_desc[KUNIT_PARAM_DESC_SIZE]; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 496 | struct kunit_case *test_case; |
David Gow | acd8e84 | 2021-08-03 22:08:08 -0700 | [diff] [blame] | 497 | struct kunit_result_stats suite_stats = { 0 }; |
| 498 | struct kunit_result_stats total_stats = { 0 }; |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 499 | |
| 500 | kunit_print_subtest_start(suite); |
| 501 | |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 502 | kunit_suite_for_each_test_case(suite, test_case) { |
| 503 | struct kunit test = { .param_value = NULL, .param_index = 0 }; |
David Gow | acd8e84 | 2021-08-03 22:08:08 -0700 | [diff] [blame] | 504 | struct kunit_result_stats param_stats = { 0 }; |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 505 | test_case->status = KUNIT_SKIPPED; |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 506 | |
David Gow | 37dbb4c | 2021-11-02 00:30:13 -0700 | [diff] [blame] | 507 | if (!test_case->generate_params) { |
| 508 | /* Non-parameterised test. */ |
| 509 | kunit_run_case_catch_errors(suite, test_case, &test); |
| 510 | kunit_update_stats(¶m_stats, test.status); |
| 511 | } else { |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 512 | /* Get initial param. */ |
| 513 | param_desc[0] = '\0'; |
| 514 | test.param_value = test_case->generate_params(NULL, param_desc); |
David Gow | 44b7da5 | 2021-11-02 00:30:14 -0700 | [diff] [blame] | 515 | kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT |
| 516 | "# Subtest: %s", test_case->name); |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 517 | |
David Gow | 37dbb4c | 2021-11-02 00:30:13 -0700 | [diff] [blame] | 518 | while (test.param_value) { |
| 519 | kunit_run_case_catch_errors(suite, test_case, &test); |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 520 | |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 521 | if (param_desc[0] == '\0') { |
| 522 | snprintf(param_desc, sizeof(param_desc), |
| 523 | "param-%d", test.param_index); |
| 524 | } |
| 525 | |
| 526 | kunit_log(KERN_INFO, &test, |
David Gow | 44b7da5 | 2021-11-02 00:30:14 -0700 | [diff] [blame] | 527 | KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT |
| 528 | "%s %d - %s", |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 529 | kunit_status_to_ok_not_ok(test.status), |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 530 | test.param_index + 1, param_desc); |
| 531 | |
| 532 | /* Get next param. */ |
| 533 | param_desc[0] = '\0'; |
| 534 | test.param_value = test_case->generate_params(test.param_value, param_desc); |
| 535 | test.param_index++; |
David Gow | 37dbb4c | 2021-11-02 00:30:13 -0700 | [diff] [blame] | 536 | |
| 537 | kunit_update_stats(¶m_stats, test.status); |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 538 | } |
David Gow | 37dbb4c | 2021-11-02 00:30:13 -0700 | [diff] [blame] | 539 | } |
David Gow | acd8e84 | 2021-08-03 22:08:08 -0700 | [diff] [blame] | 540 | |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 541 | |
David Gow | acd8e84 | 2021-08-03 22:08:08 -0700 | [diff] [blame] | 542 | kunit_print_test_stats(&test, param_stats); |
| 543 | |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 544 | kunit_print_ok_not_ok(&test, true, test_case->status, |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 545 | kunit_test_case_num(suite, test_case), |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 546 | test_case->name, |
| 547 | test.status_comment); |
David Gow | acd8e84 | 2021-08-03 22:08:08 -0700 | [diff] [blame] | 548 | |
| 549 | kunit_update_stats(&suite_stats, test_case->status); |
| 550 | kunit_accumulate_stats(&total_stats, param_stats); |
Arpitha Raghunandan | fadb08e | 2020-11-16 11:10:35 +0530 | [diff] [blame] | 551 | } |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 552 | |
David Gow | acd8e84 | 2021-08-03 22:08:08 -0700 | [diff] [blame] | 553 | kunit_print_suite_stats(suite, suite_stats, total_stats); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 554 | kunit_print_subtest_end(suite); |
| 555 | |
| 556 | return 0; |
| 557 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 558 | EXPORT_SYMBOL_GPL(kunit_run_tests); |
Brendan Higgins | 914cc63 | 2019-09-23 02:02:31 -0700 | [diff] [blame] | 559 | |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 560 | static void kunit_init_suite(struct kunit_suite *suite) |
| 561 | { |
| 562 | kunit_debugfs_create_suite(suite); |
David Gow | 6d2426b | 2021-06-24 23:58:12 -0700 | [diff] [blame] | 563 | suite->status_comment[0] = '\0'; |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Alan Maguire | aac3546 | 2020-08-04 13:47:42 -0700 | [diff] [blame] | 566 | int __kunit_test_suites_init(struct kunit_suite * const * const suites) |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 567 | { |
| 568 | unsigned int i; |
| 569 | |
| 570 | for (i = 0; suites[i] != NULL; i++) { |
| 571 | kunit_init_suite(suites[i]); |
| 572 | kunit_run_tests(suites[i]); |
| 573 | } |
| 574 | return 0; |
| 575 | } |
| 576 | EXPORT_SYMBOL_GPL(__kunit_test_suites_init); |
| 577 | |
| 578 | static void kunit_exit_suite(struct kunit_suite *suite) |
| 579 | { |
| 580 | kunit_debugfs_destroy_suite(suite); |
| 581 | } |
| 582 | |
| 583 | void __kunit_test_suites_exit(struct kunit_suite **suites) |
| 584 | { |
| 585 | unsigned int i; |
| 586 | |
| 587 | for (i = 0; suites[i] != NULL; i++) |
| 588 | kunit_exit_suite(suites[i]); |
David Gow | 17ac23e | 2021-10-05 21:41:11 -0700 | [diff] [blame] | 589 | |
| 590 | kunit_suite_counter = 1; |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 591 | } |
| 592 | EXPORT_SYMBOL_GPL(__kunit_test_suites_exit); |
| 593 | |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 594 | /* |
| 595 | * Used for static resources and when a kunit_resource * has been created by |
| 596 | * kunit_alloc_resource(). When an init function is supplied, @data is passed |
| 597 | * into the init function; otherwise, we simply set the resource data field to |
| 598 | * the data value passed in. |
| 599 | */ |
| 600 | int kunit_add_resource(struct kunit *test, |
| 601 | kunit_resource_init_t init, |
| 602 | kunit_resource_free_t free, |
| 603 | struct kunit_resource *res, |
| 604 | void *data) |
| 605 | { |
| 606 | int ret = 0; |
Vlastimil Babka | 26c6cb7 | 2021-06-28 19:34:30 -0700 | [diff] [blame] | 607 | unsigned long flags; |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 608 | |
| 609 | res->free = free; |
| 610 | kref_init(&res->refcount); |
| 611 | |
| 612 | if (init) { |
| 613 | ret = init(res, data); |
| 614 | if (ret) |
| 615 | return ret; |
| 616 | } else { |
| 617 | res->data = data; |
| 618 | } |
| 619 | |
Vlastimil Babka | 26c6cb7 | 2021-06-28 19:34:30 -0700 | [diff] [blame] | 620 | spin_lock_irqsave(&test->lock, flags); |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 621 | list_add_tail(&res->node, &test->resources); |
| 622 | /* refcount for list is established by kref_init() */ |
Vlastimil Babka | 26c6cb7 | 2021-06-28 19:34:30 -0700 | [diff] [blame] | 623 | spin_unlock_irqrestore(&test->lock, flags); |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 624 | |
| 625 | return ret; |
| 626 | } |
| 627 | EXPORT_SYMBOL_GPL(kunit_add_resource); |
| 628 | |
Alan Maguire | 725aca9 | 2020-05-29 22:46:21 +0100 | [diff] [blame] | 629 | int kunit_add_named_resource(struct kunit *test, |
| 630 | kunit_resource_init_t init, |
| 631 | kunit_resource_free_t free, |
| 632 | struct kunit_resource *res, |
| 633 | const char *name, |
| 634 | void *data) |
| 635 | { |
| 636 | struct kunit_resource *existing; |
| 637 | |
| 638 | if (!name) |
| 639 | return -EINVAL; |
| 640 | |
| 641 | existing = kunit_find_named_resource(test, name); |
| 642 | if (existing) { |
| 643 | kunit_put_resource(existing); |
| 644 | return -EEXIST; |
| 645 | } |
| 646 | |
| 647 | res->name = name; |
| 648 | |
| 649 | return kunit_add_resource(test, init, free, res, data); |
| 650 | } |
| 651 | EXPORT_SYMBOL_GPL(kunit_add_named_resource); |
| 652 | |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 653 | struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test, |
| 654 | kunit_resource_init_t init, |
| 655 | kunit_resource_free_t free, |
| 656 | gfp_t internal_gfp, |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 657 | void *data) |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 658 | { |
| 659 | struct kunit_resource *res; |
| 660 | int ret; |
| 661 | |
| 662 | res = kzalloc(sizeof(*res), internal_gfp); |
| 663 | if (!res) |
| 664 | return NULL; |
| 665 | |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 666 | ret = kunit_add_resource(test, init, free, res, data); |
| 667 | if (!ret) { |
| 668 | /* |
| 669 | * bump refcount for get; kunit_resource_put() should be called |
| 670 | * when done. |
| 671 | */ |
| 672 | kunit_get_resource(res); |
| 673 | return res; |
| 674 | } |
| 675 | return NULL; |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 676 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 677 | EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 678 | |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 679 | void kunit_remove_resource(struct kunit *test, struct kunit_resource *res) |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 680 | { |
Vlastimil Babka | 26c6cb7 | 2021-06-28 19:34:30 -0700 | [diff] [blame] | 681 | unsigned long flags; |
| 682 | |
| 683 | spin_lock_irqsave(&test->lock, flags); |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 684 | list_del(&res->node); |
Vlastimil Babka | 26c6cb7 | 2021-06-28 19:34:30 -0700 | [diff] [blame] | 685 | spin_unlock_irqrestore(&test->lock, flags); |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 686 | kunit_put_resource(res); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 687 | } |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 688 | EXPORT_SYMBOL_GPL(kunit_remove_resource); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 689 | |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 690 | int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match, |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 691 | void *match_data) |
| 692 | { |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 693 | struct kunit_resource *res = kunit_find_resource(test, match, |
| 694 | match_data); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 695 | |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 696 | if (!res) |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 697 | return -ENOENT; |
| 698 | |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 699 | kunit_remove_resource(test, res); |
| 700 | |
| 701 | /* We have a reference also via _find(); drop it. */ |
| 702 | kunit_put_resource(res); |
| 703 | |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 704 | return 0; |
| 705 | } |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 706 | EXPORT_SYMBOL_GPL(kunit_destroy_resource); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 707 | |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 708 | struct kunit_kmalloc_array_params { |
| 709 | size_t n; |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 710 | size_t size; |
| 711 | gfp_t gfp; |
| 712 | }; |
| 713 | |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 714 | static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context) |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 715 | { |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 716 | struct kunit_kmalloc_array_params *params = context; |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 717 | |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 718 | res->data = kmalloc_array(params->n, params->size, params->gfp); |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 719 | if (!res->data) |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 720 | return -ENOMEM; |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 725 | static void kunit_kmalloc_array_free(struct kunit_resource *res) |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 726 | { |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 727 | kfree(res->data); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 730 | void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp) |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 731 | { |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 732 | struct kunit_kmalloc_array_params params = { |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 733 | .size = size, |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 734 | .n = n, |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 735 | .gfp = gfp |
| 736 | }; |
| 737 | |
| 738 | return kunit_alloc_resource(test, |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 739 | kunit_kmalloc_array_init, |
| 740 | kunit_kmalloc_array_free, |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 741 | gfp, |
| 742 | ¶ms); |
| 743 | } |
Daniel Latypov | 7122deb | 2021-05-03 13:58:34 -0700 | [diff] [blame] | 744 | EXPORT_SYMBOL_GPL(kunit_kmalloc_array); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 745 | |
| 746 | void kunit_kfree(struct kunit *test, const void *ptr) |
| 747 | { |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 748 | struct kunit_resource *res; |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 749 | |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 750 | res = kunit_find_resource(test, kunit_resource_instance_match, |
| 751 | (void *)ptr); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 752 | |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 753 | /* |
| 754 | * Removing the resource from the list of resources drops the |
| 755 | * reference count to 1; the final put will trigger the free. |
| 756 | */ |
| 757 | kunit_remove_resource(test, res); |
| 758 | |
| 759 | kunit_put_resource(res); |
| 760 | |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 761 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 762 | EXPORT_SYMBOL_GPL(kunit_kfree); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 763 | |
| 764 | void kunit_cleanup(struct kunit *test) |
| 765 | { |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 766 | struct kunit_resource *res; |
Vlastimil Babka | 26c6cb7 | 2021-06-28 19:34:30 -0700 | [diff] [blame] | 767 | unsigned long flags; |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 768 | |
| 769 | /* |
| 770 | * test->resources is a stack - each allocation must be freed in the |
| 771 | * reverse order from which it was added since one resource may depend |
| 772 | * on another for its entire lifetime. |
| 773 | * Also, we cannot use the normal list_for_each constructs, even the |
| 774 | * safe ones because *arbitrary* nodes may be deleted when |
| 775 | * kunit_resource_free is called; the list_for_each_safe variants only |
| 776 | * protect against the current node being deleted, not the next. |
| 777 | */ |
| 778 | while (true) { |
Vlastimil Babka | 26c6cb7 | 2021-06-28 19:34:30 -0700 | [diff] [blame] | 779 | spin_lock_irqsave(&test->lock, flags); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 780 | if (list_empty(&test->resources)) { |
Vlastimil Babka | 26c6cb7 | 2021-06-28 19:34:30 -0700 | [diff] [blame] | 781 | spin_unlock_irqrestore(&test->lock, flags); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 782 | break; |
| 783 | } |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 784 | res = list_last_entry(&test->resources, |
| 785 | struct kunit_resource, |
| 786 | node); |
| 787 | /* |
| 788 | * Need to unlock here as a resource may remove another |
| 789 | * resource, and this can't happen if the test->lock |
| 790 | * is held. |
| 791 | */ |
Vlastimil Babka | 26c6cb7 | 2021-06-28 19:34:30 -0700 | [diff] [blame] | 792 | spin_unlock_irqrestore(&test->lock, flags); |
Alan Maguire | d4cdd14 | 2020-05-29 22:46:20 +0100 | [diff] [blame] | 793 | kunit_remove_resource(test, res); |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 794 | } |
Patricia Alfonso | 83c4e7a | 2020-10-13 16:55:02 -0700 | [diff] [blame] | 795 | current->kunit_test = NULL; |
Brendan Higgins | 0a756853 | 2019-09-23 02:02:32 -0700 | [diff] [blame] | 796 | } |
Alan Maguire | c475c77 | 2020-01-06 22:28:20 +0000 | [diff] [blame] | 797 | EXPORT_SYMBOL_GPL(kunit_cleanup); |
Alan Maguire | 9fe124b | 2020-01-06 22:28:22 +0000 | [diff] [blame] | 798 | |
| 799 | static int __init kunit_init(void) |
| 800 | { |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 801 | kunit_debugfs_init(); |
| 802 | |
Alan Maguire | 9fe124b | 2020-01-06 22:28:22 +0000 | [diff] [blame] | 803 | return 0; |
| 804 | } |
| 805 | late_initcall(kunit_init); |
| 806 | |
| 807 | static void __exit kunit_exit(void) |
| 808 | { |
Alan Maguire | e2219db | 2020-03-26 14:25:07 +0000 | [diff] [blame] | 809 | kunit_debugfs_cleanup(); |
Alan Maguire | 9fe124b | 2020-01-06 22:28:22 +0000 | [diff] [blame] | 810 | } |
| 811 | module_exit(kunit_exit); |
| 812 | |
| 813 | MODULE_LICENSE("GPL v2"); |