1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/reboot.h> 4 #include <kunit/test.h> 5 #include <kunit/attributes.h> 6 #include <linux/glob.h> 7 #include <linux/moduleparam.h> 8 9 /* 10 * These symbols point to the .kunit_test_suites section and are defined in 11 * include/asm-generic/vmlinux.lds.h, and consequently must be extern. 12 */ 13 extern struct kunit_suite * const __kunit_suites_start[]; 14 extern struct kunit_suite * const __kunit_suites_end[]; 15 extern struct kunit_suite * const __kunit_init_suites_start[]; 16 extern struct kunit_suite * const __kunit_init_suites_end[]; 17 18 static char *action_param; 19 20 module_param_named(action, action_param, charp, 0400); 21 MODULE_PARM_DESC(action, 22 "Changes KUnit executor behavior, valid values are:\n" 23 "<none>: run the tests like normal\n" 24 "'list' to list test names instead of running them.\n" 25 "'list_attr' to list test names and attributes instead of running them.\n"); 26 27 const char *kunit_action(void) 28 { 29 return action_param; 30 } 31 32 /* 33 * Run KUnit tests after initialization 34 */ 35 #ifdef CONFIG_KUNIT_AUTORUN_ENABLED 36 static bool autorun_param = true; 37 #else 38 static bool autorun_param; 39 #endif 40 module_param_named(autorun, autorun_param, bool, 0); 41 MODULE_PARM_DESC(autorun, "Run KUnit tests after initialization"); 42 43 bool kunit_autorun(void) 44 { 45 return autorun_param; 46 } 47 48 #define PARAM_FROM_CONFIG(config) (config[0] ? config : NULL) 49 50 static char *filter_glob_param = PARAM_FROM_CONFIG(CONFIG_KUNIT_DEFAULT_FILTER_GLOB); 51 static char *filter_param = PARAM_FROM_CONFIG(CONFIG_KUNIT_DEFAULT_FILTER); 52 static char *filter_action_param = PARAM_FROM_CONFIG(CONFIG_KUNIT_DEFAULT_FILTER_ACTION); 53 54 module_param_named(filter_glob, filter_glob_param, charp, 0600); 55 MODULE_PARM_DESC(filter_glob, 56 "Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test"); 57 module_param_named(filter, filter_param, charp, 0600); 58 MODULE_PARM_DESC(filter, 59 "Filter which KUnit test suites/tests run at boot-time using attributes, e.g. speed>slow"); 60 module_param_named(filter_action, filter_action_param, charp, 0600); 61 MODULE_PARM_DESC(filter_action, 62 "Changes behavior of filtered tests using attributes, valid values are:\n" 63 "<none>: do not run filtered tests as normal\n" 64 "'skip': skip all filtered tests instead so tests will appear in output\n"); 65 66 const char *kunit_filter_glob(void) 67 { 68 return filter_glob_param; 69 } 70 71 char *kunit_filter(void) 72 { 73 return filter_param; 74 } 75 76 char *kunit_filter_action(void) 77 { 78 return filter_action_param; 79 } 80 81 /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */ 82 struct kunit_glob_filter { 83 char *suite_glob; 84 char *test_glob; 85 }; 86 87 /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */ 88 static int kunit_parse_glob_filter(struct kunit_glob_filter *parsed, 89 const char *filter_glob) 90 { 91 const char *period = strchr(filter_glob, '.'); 92 93 if (!period) { 94 parsed->suite_glob = kstrdup(filter_glob, GFP_KERNEL); 95 if (!parsed->suite_glob) 96 return -ENOMEM; 97 parsed->test_glob = NULL; 98 return 0; 99 } 100 101 parsed->suite_glob = kstrndup(filter_glob, period - filter_glob, GFP_KERNEL); 102 if (!parsed->suite_glob) 103 return -ENOMEM; 104 105 parsed->test_glob = kstrdup(period + 1, GFP_KERNEL); 106 if (!parsed->test_glob) { 107 kfree(parsed->suite_glob); 108 return -ENOMEM; 109 } 110 111 return 0; 112 } 113 114 /* Create a copy of suite with only tests that match test_glob. */ 115 static struct kunit_suite * 116 kunit_filter_glob_tests(const struct kunit_suite *const suite, const char *test_glob) 117 { 118 int n = 0; 119 struct kunit_case *filtered, *test_case; 120 struct kunit_suite *copy; 121 122 kunit_suite_for_each_test_case(suite, test_case) { 123 if (!test_glob || glob_match(test_glob, test_case->name)) 124 ++n; 125 } 126 127 if (n == 0) 128 return NULL; 129 130 copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL); 131 if (!copy) 132 return ERR_PTR(-ENOMEM); 133 134 filtered = kzalloc_objs(*filtered, n + 1, GFP_KERNEL); 135 if (!filtered) { 136 kfree(copy); 137 return ERR_PTR(-ENOMEM); 138 } 139 140 n = 0; 141 kunit_suite_for_each_test_case(suite, test_case) { 142 if (!test_glob || glob_match(test_glob, test_case->name)) 143 filtered[n++] = *test_case; 144 } 145 146 copy->test_cases = filtered; 147 return copy; 148 } 149 150 void kunit_free_suite_set(struct kunit_suite_set suite_set) 151 { 152 struct kunit_suite * const *suites; 153 154 for (suites = suite_set.start; suites < suite_set.end; suites++) { 155 kfree((*suites)->test_cases); 156 kfree(*suites); 157 } 158 kfree(suite_set.start); 159 } 160 161 /* 162 * Filter and reallocate test suites. Must return the filtered test suites set 163 * allocated at a valid virtual address or NULL in case of error. 164 */ 165 struct kunit_suite_set 166 kunit_filter_suites(const struct kunit_suite_set *suite_set, 167 const char *filter_glob, 168 char *filters, 169 char *filter_action, 170 int *err) 171 { 172 int i, j, k; 173 int filter_count = 0; 174 struct kunit_suite **copy, **copy_start, *filtered_suite, *new_filtered_suite; 175 struct kunit_suite_set filtered = {NULL, NULL}; 176 struct kunit_glob_filter parsed_glob; 177 struct kunit_attr_filter *parsed_filters = NULL; 178 struct kunit_suite * const *suites; 179 180 const size_t max = suite_set->end - suite_set->start; 181 182 copy = kzalloc_objs(*copy, max, GFP_KERNEL); 183 if (!copy) { /* won't be able to run anything, return an empty set */ 184 return filtered; 185 } 186 copy_start = copy; 187 188 if (filter_glob) { 189 *err = kunit_parse_glob_filter(&parsed_glob, filter_glob); 190 if (*err) 191 goto free_copy; 192 } 193 194 /* Parse attribute filters */ 195 if (filters) { 196 filter_count = kunit_get_filter_count(filters); 197 parsed_filters = kzalloc_objs(*parsed_filters, filter_count, 198 GFP_KERNEL); 199 if (!parsed_filters) { 200 *err = -ENOMEM; 201 goto free_parsed_glob; 202 } 203 for (j = 0; j < filter_count; j++) 204 parsed_filters[j] = kunit_next_attr_filter(&filters, err); 205 if (*err) 206 goto free_parsed_filters; 207 } 208 209 for (i = 0; &suite_set->start[i] != suite_set->end; i++) { 210 filtered_suite = suite_set->start[i]; 211 if (filter_glob) { 212 if (!glob_match(parsed_glob.suite_glob, filtered_suite->name)) 213 continue; 214 filtered_suite = kunit_filter_glob_tests(filtered_suite, 215 parsed_glob.test_glob); 216 if (IS_ERR(filtered_suite)) { 217 *err = PTR_ERR(filtered_suite); 218 goto free_filtered_suite; 219 } 220 } 221 if (filter_count > 0 && parsed_filters != NULL) { 222 for (k = 0; k < filter_count; k++) { 223 new_filtered_suite = kunit_filter_attr_tests(filtered_suite, 224 parsed_filters[k], filter_action, err); 225 226 /* Free previous copy of suite */ 227 if (k > 0 || filter_glob) { 228 kfree(filtered_suite->test_cases); 229 kfree(filtered_suite); 230 } 231 232 filtered_suite = new_filtered_suite; 233 234 if (*err) 235 goto free_filtered_suite; 236 237 if (IS_ERR(filtered_suite)) { 238 *err = PTR_ERR(filtered_suite); 239 goto free_filtered_suite; 240 } 241 if (!filtered_suite) 242 break; 243 } 244 } 245 246 if (!filtered_suite) 247 continue; 248 249 *copy++ = filtered_suite; 250 } 251 filtered.start = copy_start; 252 filtered.end = copy; 253 254 free_filtered_suite: 255 if (*err) { 256 for (suites = copy_start; suites < copy; suites++) { 257 kfree((*suites)->test_cases); 258 kfree(*suites); 259 } 260 } 261 262 free_parsed_filters: 263 if (filter_count) 264 kfree(parsed_filters); 265 266 free_parsed_glob: 267 if (filter_glob) { 268 kfree(parsed_glob.suite_glob); 269 kfree(parsed_glob.test_glob); 270 } 271 272 free_copy: 273 if (*err) 274 kfree(copy_start); 275 276 return filtered; 277 } 278 279 void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin) 280 { 281 size_t num_suites = suite_set->end - suite_set->start; 282 bool autorun = kunit_autorun(); 283 284 if (autorun && (builtin || num_suites)) { 285 pr_info("KTAP version 1\n"); 286 pr_info("1..%zu\n", num_suites); 287 } 288 289 __kunit_test_suites_init(suite_set->start, num_suites, autorun); 290 } 291 292 void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr) 293 { 294 struct kunit_suite * const *suites; 295 struct kunit_case *test_case; 296 297 /* Hack: print a ktap header so kunit.py can find the start of KUnit output. */ 298 pr_info("KTAP version 1\n"); 299 300 for (suites = suite_set->start; suites < suite_set->end; suites++) { 301 /* Print suite name and suite attributes */ 302 pr_info("%s\n", (*suites)->name); 303 if (include_attr) 304 kunit_print_attr((void *)(*suites), false, 0); 305 306 /* Print test case name and attributes in suite */ 307 kunit_suite_for_each_test_case((*suites), test_case) { 308 pr_info("%s.%s\n", (*suites)->name, test_case->name); 309 if (include_attr) 310 kunit_print_attr((void *)test_case, true, 0); 311 } 312 } 313 } 314 315 struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set, 316 struct kunit_suite_set suite_set) 317 { 318 struct kunit_suite_set total_suite_set = {NULL, NULL}; 319 struct kunit_suite **total_suite_start = NULL; 320 size_t init_num_suites, num_suites, suite_size; 321 int i = 0; 322 323 init_num_suites = init_suite_set.end - init_suite_set.start; 324 num_suites = suite_set.end - suite_set.start; 325 suite_size = sizeof(suite_set.start); 326 327 /* Allocate memory for array of all kunit suites */ 328 total_suite_start = kmalloc_array(init_num_suites + num_suites, suite_size, GFP_KERNEL); 329 if (!total_suite_start) 330 return total_suite_set; 331 332 /* Append and mark init suites and then append all other kunit suites */ 333 memcpy(total_suite_start, init_suite_set.start, init_num_suites * suite_size); 334 for (i = 0; i < init_num_suites; i++) 335 total_suite_start[i]->is_init = true; 336 337 memcpy(total_suite_start + init_num_suites, suite_set.start, num_suites * suite_size); 338 339 /* Set kunit suite set start and end */ 340 total_suite_set.start = total_suite_start; 341 total_suite_set.end = total_suite_start + (init_num_suites + num_suites); 342 343 return total_suite_set; 344 } 345 346 #if IS_BUILTIN(CONFIG_KUNIT) 347 348 static char *kunit_shutdown; 349 core_param(kunit_shutdown, kunit_shutdown, charp, 0644); 350 351 static void kunit_handle_shutdown(void) 352 { 353 if (!kunit_shutdown) 354 return; 355 356 if (!strcmp(kunit_shutdown, "poweroff")) 357 kernel_power_off(); 358 else if (!strcmp(kunit_shutdown, "halt")) 359 kernel_halt(); 360 else if (!strcmp(kunit_shutdown, "reboot")) 361 kernel_restart(NULL); 362 363 } 364 365 int kunit_run_all_tests(void) 366 { 367 struct kunit_suite_set suite_set = {NULL, NULL}; 368 struct kunit_suite_set filtered_suite_set = {NULL, NULL}; 369 struct kunit_suite_set init_suite_set = { 370 __kunit_init_suites_start, __kunit_init_suites_end, 371 }; 372 struct kunit_suite_set normal_suite_set = { 373 __kunit_suites_start, __kunit_suites_end, 374 }; 375 size_t init_num_suites = init_suite_set.end - init_suite_set.start; 376 int err = 0; 377 378 if (init_num_suites > 0) { 379 suite_set = kunit_merge_suite_sets(init_suite_set, normal_suite_set); 380 if (!suite_set.start) 381 goto out; 382 } else 383 suite_set = normal_suite_set; 384 385 if (!kunit_enabled()) { 386 pr_info("kunit: disabled\n"); 387 goto free_out; 388 } 389 390 if (filter_glob_param || filter_param) { 391 filtered_suite_set = kunit_filter_suites(&suite_set, filter_glob_param, 392 filter_param, filter_action_param, &err); 393 394 /* Free original suite set before using filtered suite set */ 395 if (init_num_suites > 0) 396 kfree(suite_set.start); 397 suite_set = filtered_suite_set; 398 399 if (err) { 400 pr_err("kunit executor: error filtering suites: %d\n", err); 401 goto free_out; 402 } 403 } 404 405 if (!action_param) 406 kunit_exec_run_tests(&suite_set, true); 407 else if (strcmp(action_param, "list") == 0) 408 kunit_exec_list_tests(&suite_set, false); 409 else if (strcmp(action_param, "list_attr") == 0) 410 kunit_exec_list_tests(&suite_set, true); 411 else 412 pr_err("kunit executor: unknown action '%s'\n", action_param); 413 414 free_out: 415 if (filter_glob_param || filter_param) 416 kunit_free_suite_set(suite_set); 417 else if (init_num_suites > 0) 418 /* Don't use kunit_free_suite_set because suites aren't individually allocated */ 419 kfree(suite_set.start); 420 421 out: 422 kunit_handle_shutdown(); 423 return err; 424 } 425 426 #if IS_BUILTIN(CONFIG_KUNIT_TEST) 427 #include "executor_test.c" 428 #endif 429 430 #endif /* IS_BUILTIN(CONFIG_KUNIT) */ 431