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