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 16 #if IS_BUILTIN(CONFIG_KUNIT) 17 18 static char *filter_glob_param; 19 static char *action_param; 20 static char *filter_param; 21 static char *filter_action_param; 22 23 module_param_named(filter_glob, filter_glob_param, charp, 0); 24 MODULE_PARM_DESC(filter_glob, 25 "Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test"); 26 module_param_named(action, action_param, charp, 0); 27 MODULE_PARM_DESC(action, 28 "Changes KUnit executor behavior, valid values are:\n" 29 "<none>: run the tests like normal\n" 30 "'list' to list test names instead of running them.\n" 31 "'list_attr' to list test names and attributes instead of running them.\n"); 32 module_param_named(filter, filter_param, charp, 0); 33 MODULE_PARM_DESC(filter, 34 "Filter which KUnit test suites/tests run at boot-time using attributes, e.g. speed>slow"); 35 module_param_named(filter_action, filter_action_param, charp, 0); 36 MODULE_PARM_DESC(filter_action, 37 "Changes behavior of filtered tests using attributes, valid values are:\n" 38 "<none>: do not run filtered tests as normal\n" 39 "'skip': skip all filtered tests instead so tests will appear in output\n"); 40 41 /* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */ 42 struct kunit_glob_filter { 43 char *suite_glob; 44 char *test_glob; 45 }; 46 47 /* Split "suite_glob.test_glob" into two. Assumes filter_glob is not empty. */ 48 static void kunit_parse_glob_filter(struct kunit_glob_filter *parsed, 49 const char *filter_glob) 50 { 51 const int len = strlen(filter_glob); 52 const char *period = strchr(filter_glob, '.'); 53 54 if (!period) { 55 parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL); 56 parsed->test_glob = NULL; 57 strcpy(parsed->suite_glob, filter_glob); 58 return; 59 } 60 61 parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL); 62 parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL); 63 64 strncpy(parsed->suite_glob, filter_glob, period - filter_glob); 65 strncpy(parsed->test_glob, period + 1, len - (period - filter_glob)); 66 } 67 68 /* Create a copy of suite with only tests that match test_glob. */ 69 static struct kunit_suite * 70 kunit_filter_glob_tests(const struct kunit_suite *const suite, const char *test_glob) 71 { 72 int n = 0; 73 struct kunit_case *filtered, *test_case; 74 struct kunit_suite *copy; 75 76 kunit_suite_for_each_test_case(suite, test_case) { 77 if (!test_glob || glob_match(test_glob, test_case->name)) 78 ++n; 79 } 80 81 if (n == 0) 82 return NULL; 83 84 copy = kmemdup(suite, sizeof(*copy), GFP_KERNEL); 85 if (!copy) 86 return ERR_PTR(-ENOMEM); 87 88 filtered = kcalloc(n + 1, sizeof(*filtered), GFP_KERNEL); 89 if (!filtered) { 90 kfree(copy); 91 return ERR_PTR(-ENOMEM); 92 } 93 94 n = 0; 95 kunit_suite_for_each_test_case(suite, test_case) { 96 if (!test_glob || glob_match(test_glob, test_case->name)) 97 filtered[n++] = *test_case; 98 } 99 100 copy->test_cases = filtered; 101 return copy; 102 } 103 104 static char *kunit_shutdown; 105 core_param(kunit_shutdown, kunit_shutdown, charp, 0644); 106 107 /* Stores an array of suites, end points one past the end */ 108 struct suite_set { 109 struct kunit_suite * const *start; 110 struct kunit_suite * const *end; 111 }; 112 113 static void kunit_free_suite_set(struct suite_set suite_set) 114 { 115 struct kunit_suite * const *suites; 116 117 for (suites = suite_set.start; suites < suite_set.end; suites++) 118 kfree(*suites); 119 kfree(suite_set.start); 120 } 121 122 static struct suite_set kunit_filter_suites(const struct suite_set *suite_set, 123 const char *filter_glob, 124 char *filters, 125 char *filter_action, 126 int *err) 127 { 128 int i, j, k; 129 int filter_count = 0; 130 struct kunit_suite **copy, *filtered_suite, *new_filtered_suite; 131 struct suite_set filtered; 132 struct kunit_glob_filter parsed_glob; 133 struct kunit_attr_filter *parsed_filters; 134 135 const size_t max = suite_set->end - suite_set->start; 136 137 copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL); 138 filtered.start = copy; 139 if (!copy) { /* won't be able to run anything, return an empty set */ 140 filtered.end = copy; 141 return filtered; 142 } 143 144 if (filter_glob) 145 kunit_parse_glob_filter(&parsed_glob, filter_glob); 146 147 /* Parse attribute filters */ 148 if (filters) { 149 filter_count = kunit_get_filter_count(filters); 150 parsed_filters = kcalloc(filter_count + 1, sizeof(*parsed_filters), GFP_KERNEL); 151 for (j = 0; j < filter_count; j++) 152 parsed_filters[j] = kunit_next_attr_filter(&filters, err); 153 if (*err) 154 return filtered; 155 } 156 157 for (i = 0; &suite_set->start[i] != suite_set->end; i++) { 158 filtered_suite = suite_set->start[i]; 159 if (filter_glob) { 160 if (!glob_match(parsed_glob.suite_glob, filtered_suite->name)) 161 continue; 162 filtered_suite = kunit_filter_glob_tests(filtered_suite, 163 parsed_glob.test_glob); 164 if (IS_ERR(filtered_suite)) { 165 *err = PTR_ERR(filtered_suite); 166 return filtered; 167 } 168 } 169 if (filter_count) { 170 for (k = 0; k < filter_count; k++) { 171 new_filtered_suite = kunit_filter_attr_tests(filtered_suite, 172 parsed_filters[k], filter_action, err); 173 174 /* Free previous copy of suite */ 175 if (k > 0 || filter_glob) 176 kfree(filtered_suite); 177 filtered_suite = new_filtered_suite; 178 179 if (*err) 180 return filtered; 181 if (IS_ERR(filtered_suite)) { 182 *err = PTR_ERR(filtered_suite); 183 return filtered; 184 } 185 if (!filtered_suite) 186 break; 187 } 188 } 189 190 if (!filtered_suite) 191 continue; 192 193 *copy++ = filtered_suite; 194 } 195 filtered.end = copy; 196 197 if (filter_glob) { 198 kfree(parsed_glob.suite_glob); 199 kfree(parsed_glob.test_glob); 200 } 201 202 if (filter_count) 203 kfree(parsed_filters); 204 205 return filtered; 206 } 207 208 static void kunit_handle_shutdown(void) 209 { 210 if (!kunit_shutdown) 211 return; 212 213 if (!strcmp(kunit_shutdown, "poweroff")) 214 kernel_power_off(); 215 else if (!strcmp(kunit_shutdown, "halt")) 216 kernel_halt(); 217 else if (!strcmp(kunit_shutdown, "reboot")) 218 kernel_restart(NULL); 219 220 } 221 222 static void kunit_exec_run_tests(struct suite_set *suite_set) 223 { 224 size_t num_suites = suite_set->end - suite_set->start; 225 226 pr_info("KTAP version 1\n"); 227 pr_info("1..%zu\n", num_suites); 228 229 __kunit_test_suites_init(suite_set->start, num_suites); 230 } 231 232 static void kunit_exec_list_tests(struct suite_set *suite_set, bool include_attr) 233 { 234 struct kunit_suite * const *suites; 235 struct kunit_case *test_case; 236 237 /* Hack: print a ktap header so kunit.py can find the start of KUnit output. */ 238 pr_info("KTAP version 1\n"); 239 240 for (suites = suite_set->start; suites < suite_set->end; suites++) { 241 /* Print suite name and suite attributes */ 242 pr_info("%s\n", (*suites)->name); 243 if (include_attr) 244 kunit_print_attr((void *)(*suites), false, 0); 245 246 /* Print test case name and attributes in suite */ 247 kunit_suite_for_each_test_case((*suites), test_case) { 248 pr_info("%s.%s\n", (*suites)->name, test_case->name); 249 if (include_attr) 250 kunit_print_attr((void *)test_case, true, 0); 251 } 252 } 253 } 254 255 int kunit_run_all_tests(void) 256 { 257 struct suite_set suite_set = {__kunit_suites_start, __kunit_suites_end}; 258 int err = 0; 259 if (!kunit_enabled()) { 260 pr_info("kunit: disabled\n"); 261 goto out; 262 } 263 264 if (filter_glob_param || filter_param) { 265 suite_set = kunit_filter_suites(&suite_set, filter_glob_param, 266 filter_param, filter_action_param, &err); 267 if (err) { 268 pr_err("kunit executor: error filtering suites: %d\n", err); 269 goto out; 270 } 271 } 272 273 if (!action_param) 274 kunit_exec_run_tests(&suite_set); 275 else if (strcmp(action_param, "list") == 0) 276 kunit_exec_list_tests(&suite_set, false); 277 else if (strcmp(action_param, "list_attr") == 0) 278 kunit_exec_list_tests(&suite_set, true); 279 else 280 pr_err("kunit executor: unknown action '%s'\n", action_param); 281 282 if (filter_glob_param || filter_param) { /* a copy was made of each suite */ 283 kunit_free_suite_set(suite_set); 284 } 285 286 out: 287 kunit_handle_shutdown(); 288 return err; 289 } 290 291 #if IS_BUILTIN(CONFIG_KUNIT_TEST) 292 #include "executor_test.c" 293 #endif 294 295 #endif /* IS_BUILTIN(CONFIG_KUNIT) */ 296