Skip to content

Commit b67abaa

Browse files
jkrzyszt-intelshuahkh
authored andcommitted
kunit: Allow kunit test modules to use test filtering
External tools, e.g., Intel GPU tools (IGT), support execution of individual selftests provided by kernel modules. That could be also applicable to kunit test modules if they provided test filtering. But test filtering is now possible only when kunit code is built into the kernel. Moreover, a filter can be specified only at boot time, then reboot is required each time a different filter is needed. Build the test filtering code also when kunit is configured as a module, expose test filtering functions to other kunit source files, and use them in kunit module notifier callback functions. Userspace can then reload the kunit module with a value of the filter_glob parameter tuned to a specific kunit test module every time it wants to limit the scope of tests executed on that module load. Make the kunit.filter* parameters visible in sysfs for user convenience. v5: Refresh on tpp of attributes filtering fix v4: Refresh on top of newly applied attributes patches and changes introdced by new versions of other patches submitted in series with this one. v3: Fix CONFIG_GLOB, required by filtering functions, not selected when building as a module ([email protected]). v2: Fix new name of a structure moved to kunit namespace not updated across all uses ([email protected]). Signed-off-by: Janusz Krzysztofik <[email protected]> Reviewed-by: David Gow <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 18258c6 commit b67abaa

File tree

4 files changed

+66
-27
lines changed

4 files changed

+66
-27
lines changed

include/kunit/test.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ static inline void kunit_set_failure(struct kunit *test)
310310

311311
bool kunit_enabled(void);
312312
const char *kunit_action(void);
313+
const char *kunit_filter_glob(void);
314+
char *kunit_filter(void);
315+
char *kunit_filter_action(void);
313316

314317
void kunit_init_test(struct kunit *test, const char *name, char *log);
315318

@@ -320,6 +323,14 @@ size_t kunit_suite_num_test_cases(struct kunit_suite *suite);
320323
unsigned int kunit_test_case_num(struct kunit_suite *suite,
321324
struct kunit_case *test_case);
322325

326+
struct kunit_suite_set
327+
kunit_filter_suites(const struct kunit_suite_set *suite_set,
328+
const char *filter_glob,
329+
char *filters,
330+
char *filter_action,
331+
int *err);
332+
void kunit_free_suite_set(struct kunit_suite_set suite_set);
333+
323334
int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites);
324335

325336
void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites);

lib/kunit/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
menuconfig KUNIT
66
tristate "KUnit - Enable support for unit tests"
7-
select GLOB if KUNIT=y
7+
select GLOB
88
help
99
Enables support for kernel unit tests (KUnit), a lightweight unit
1010
testing and mocking framework for the Linux kernel. These tests are

lib/kunit/executor.c

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,37 @@ const char *kunit_action(void)
2727
return action_param;
2828
}
2929

30-
#if IS_BUILTIN(CONFIG_KUNIT)
31-
3230
static char *filter_glob_param;
3331
static char *filter_param;
3432
static char *filter_action_param;
3533

36-
module_param_named(filter_glob, filter_glob_param, charp, 0);
34+
module_param_named(filter_glob, filter_glob_param, charp, 0400);
3735
MODULE_PARM_DESC(filter_glob,
3836
"Filter which KUnit test suites/tests run at boot-time, e.g. list* or list*.*del_test");
39-
module_param_named(filter, filter_param, charp, 0);
37+
module_param_named(filter, filter_param, charp, 0400);
4038
MODULE_PARM_DESC(filter,
4139
"Filter which KUnit test suites/tests run at boot-time using attributes, e.g. speed>slow");
42-
module_param_named(filter_action, filter_action_param, charp, 0);
40+
module_param_named(filter_action, filter_action_param, charp, 0400);
4341
MODULE_PARM_DESC(filter_action,
4442
"Changes behavior of filtered tests using attributes, valid values are:\n"
4543
"<none>: do not run filtered tests as normal\n"
4644
"'skip': skip all filtered tests instead so tests will appear in output\n");
4745

46+
const char *kunit_filter_glob(void)
47+
{
48+
return filter_glob_param;
49+
}
50+
51+
char *kunit_filter(void)
52+
{
53+
return filter_param;
54+
}
55+
56+
char *kunit_filter_action(void)
57+
{
58+
return filter_action_param;
59+
}
60+
4861
/* glob_match() needs NULL terminated strings, so we need a copy of filter_glob_param. */
4962
struct kunit_glob_filter {
5063
char *suite_glob;
@@ -108,10 +121,7 @@ kunit_filter_glob_tests(const struct kunit_suite *const suite, const char *test_
108121
return copy;
109122
}
110123

111-
static char *kunit_shutdown;
112-
core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
113-
114-
static void kunit_free_suite_set(struct kunit_suite_set suite_set)
124+
void kunit_free_suite_set(struct kunit_suite_set suite_set)
115125
{
116126
struct kunit_suite * const *suites;
117127

@@ -120,7 +130,7 @@ static void kunit_free_suite_set(struct kunit_suite_set suite_set)
120130
kfree(suite_set.start);
121131
}
122132

123-
static struct kunit_suite_set
133+
struct kunit_suite_set
124134
kunit_filter_suites(const struct kunit_suite_set *suite_set,
125135
const char *filter_glob,
126136
char *filters,
@@ -218,22 +228,6 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
218228
return filtered;
219229
}
220230

221-
static void kunit_handle_shutdown(void)
222-
{
223-
if (!kunit_shutdown)
224-
return;
225-
226-
if (!strcmp(kunit_shutdown, "poweroff"))
227-
kernel_power_off();
228-
else if (!strcmp(kunit_shutdown, "halt"))
229-
kernel_halt();
230-
else if (!strcmp(kunit_shutdown, "reboot"))
231-
kernel_restart(NULL);
232-
233-
}
234-
235-
#endif
236-
237231
void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin)
238232
{
239233
size_t num_suites = suite_set->end - suite_set->start;
@@ -271,6 +265,23 @@ void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr)
271265

272266
#if IS_BUILTIN(CONFIG_KUNIT)
273267

268+
static char *kunit_shutdown;
269+
core_param(kunit_shutdown, kunit_shutdown, charp, 0644);
270+
271+
static void kunit_handle_shutdown(void)
272+
{
273+
if (!kunit_shutdown)
274+
return;
275+
276+
if (!strcmp(kunit_shutdown, "poweroff"))
277+
kernel_power_off();
278+
else if (!strcmp(kunit_shutdown, "halt"))
279+
kernel_halt();
280+
else if (!strcmp(kunit_shutdown, "reboot"))
281+
kernel_restart(NULL);
282+
283+
}
284+
274285
int kunit_run_all_tests(void)
275286
{
276287
struct kunit_suite_set suite_set = {

lib/kunit/test.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,17 @@ static void kunit_module_init(struct module *mod)
740740
mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
741741
};
742742
const char *action = kunit_action();
743+
int err = 0;
744+
745+
suite_set = kunit_filter_suites(&suite_set,
746+
kunit_filter_glob() ?: "*.*",
747+
kunit_filter(), kunit_filter_action(),
748+
&err);
749+
if (err)
750+
pr_err("kunit module: error filtering suites: %d\n", err);
751+
752+
mod->kunit_suites = (struct kunit_suite **)suite_set.start;
753+
mod->num_kunit_suites = suite_set.end - suite_set.start;
743754

744755
if (!action)
745756
kunit_exec_run_tests(&suite_set, false);
@@ -753,11 +764,17 @@ static void kunit_module_init(struct module *mod)
753764

754765
static void kunit_module_exit(struct module *mod)
755766
{
767+
struct kunit_suite_set suite_set = {
768+
mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
769+
};
756770
const char *action = kunit_action();
757771

758772
if (!action)
759773
__kunit_test_suites_exit(mod->kunit_suites,
760774
mod->num_kunit_suites);
775+
776+
if (suite_set.start)
777+
kunit_free_suite_set(suite_set);
761778
}
762779

763780
static int kunit_module_notify(struct notifier_block *nb, unsigned long val,

0 commit comments

Comments
 (0)