Skip to content

Commit 6f46e6f

Browse files
committed
Merge tag 'linux_kselftest-kunit-6.17-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
Pull kunit updates from Shuah Khan: "Correct MODULE_IMPORT_NS() syntax documentation, make kunit_test timeout configurable via a module parameter and a Kconfig option, fix longest symbol length test, add a test for static stub, and adjust kunit_test timeout based on test_{suite,case} speed" * tag 'linux_kselftest-kunit-6.17-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: kunit: fix longest symbol length test kunit: Make default kunit_test timeout configurable via both a module parameter and a Kconfig option kunit: Adjust kunit_test timeout based on test_{suite,case} speed kunit: Add test for static stub Documentation: kunit: Correct MODULE_IMPORT_NS() syntax
2 parents 22c5696 + 34db4fb commit 6f46e6f

9 files changed

Lines changed: 118 additions & 37 deletions

File tree

Documentation/dev-tools/kunit/usage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ the template below.
699699
#include <kunit/visibility.h>
700700
#include <my_file.h>
701701
...
702-
MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING);
702+
MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
703703
...
704704
// Use do_interesting_thing() in tests
705705

include/kunit/try-catch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct kunit_try_catch {
4747
int try_result;
4848
kunit_try_catch_func_t try;
4949
kunit_try_catch_func_t catch;
50+
unsigned long timeout;
5051
void *context;
5152
};
5253

lib/Kconfig.debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2894,6 +2894,7 @@ config FORTIFY_KUNIT_TEST
28942894
config LONGEST_SYM_KUNIT_TEST
28952895
tristate "Test the longest symbol possible" if !KUNIT_ALL_TESTS
28962896
depends on KUNIT && KPROBES
2897+
depends on !PREFIX_SYMBOLS && !CFI_CLANG && !GCOV_KERNEL
28972898
default KUNIT_ALL_TESTS
28982899
help
28992900
Tests the longest symbol possible

lib/kunit/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,17 @@ config KUNIT_AUTORUN_ENABLED
9393
In most cases this should be left as Y. Only if additional opt-in
9494
behavior is needed should this be set to N.
9595

96+
config KUNIT_DEFAULT_TIMEOUT
97+
int "Default value of the timeout module parameter"
98+
default 300
99+
help
100+
Sets the default timeout, in seconds, for Kunit test cases. This value
101+
is further multiplied by a factor determined by the assigned speed
102+
setting: 1x for `DEFAULT`, 3x for `KUNIT_SPEED_SLOW`, and 12x for
103+
`KUNIT_SPEED_VERY_SLOW`. This allows slower tests on slower machines
104+
sufficient time to complete.
105+
106+
If unsure, the default timeout of 300 seconds is suitable for most
107+
cases.
108+
96109
endif # KUNIT

lib/kunit/kunit-test.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "linux/gfp_types.h"
99
#include <kunit/test.h>
1010
#include <kunit/test-bug.h>
11+
#include <kunit/static_stub.h>
1112

1213
#include <linux/device.h>
1314
#include <kunit/device.h>
@@ -43,7 +44,8 @@ static void kunit_test_try_catch_successful_try_no_catch(struct kunit *test)
4344
kunit_try_catch_init(try_catch,
4445
test,
4546
kunit_test_successful_try,
46-
kunit_test_no_catch);
47+
kunit_test_no_catch,
48+
300 * msecs_to_jiffies(MSEC_PER_SEC));
4749
kunit_try_catch_run(try_catch, test);
4850

4951
KUNIT_EXPECT_TRUE(test, ctx->function_called);
@@ -75,7 +77,8 @@ static void kunit_test_try_catch_unsuccessful_try_does_catch(struct kunit *test)
7577
kunit_try_catch_init(try_catch,
7678
test,
7779
kunit_test_unsuccessful_try,
78-
kunit_test_catch);
80+
kunit_test_catch,
81+
300 * msecs_to_jiffies(MSEC_PER_SEC));
7982
kunit_try_catch_run(try_catch, test);
8083

8184
KUNIT_EXPECT_TRUE(test, ctx->function_called);
@@ -129,7 +132,8 @@ static void kunit_test_fault_null_dereference(struct kunit *test)
129132
kunit_try_catch_init(try_catch,
130133
test,
131134
kunit_test_null_dereference,
132-
kunit_test_catch);
135+
kunit_test_catch,
136+
300 * msecs_to_jiffies(MSEC_PER_SEC));
133137
kunit_try_catch_run(try_catch, test);
134138

135139
KUNIT_EXPECT_EQ(test, try_catch->try_result, -EINTR);
@@ -868,10 +872,53 @@ static struct kunit_suite kunit_current_test_suite = {
868872
.test_cases = kunit_current_test_cases,
869873
};
870874

875+
static void kunit_stub_test(struct kunit *test)
876+
{
877+
struct kunit fake_test;
878+
const unsigned long fake_real_fn_addr = 0x1234;
879+
const unsigned long fake_replacement_addr = 0x5678;
880+
struct kunit_resource *res;
881+
struct {
882+
void *real_fn_addr;
883+
void *replacement_addr;
884+
} *stub_ctx;
885+
886+
kunit_init_test(&fake_test, "kunit_stub_fake_test", NULL);
887+
KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
888+
KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0);
889+
890+
__kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr,
891+
(void *)fake_replacement_addr);
892+
KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
893+
KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 1);
894+
895+
res = list_first_entry(&fake_test.resources, struct kunit_resource, node);
896+
KUNIT_EXPECT_NOT_NULL(test, res);
897+
898+
stub_ctx = res->data;
899+
KUNIT_EXPECT_NOT_NULL(test, stub_ctx);
900+
KUNIT_EXPECT_EQ(test, (unsigned long)stub_ctx->real_fn_addr, fake_real_fn_addr);
901+
KUNIT_EXPECT_EQ(test, (unsigned long)stub_ctx->replacement_addr, fake_replacement_addr);
902+
903+
__kunit_activate_static_stub(&fake_test, (void *)fake_real_fn_addr, NULL);
904+
KUNIT_ASSERT_EQ(test, fake_test.status, KUNIT_SUCCESS);
905+
KUNIT_ASSERT_EQ(test, list_count_nodes(&fake_test.resources), 0);
906+
}
907+
908+
static struct kunit_case kunit_stub_test_cases[] = {
909+
KUNIT_CASE(kunit_stub_test),
910+
{}
911+
};
912+
913+
static struct kunit_suite kunit_stub_test_suite = {
914+
.name = "kunit_stub",
915+
.test_cases = kunit_stub_test_cases,
916+
};
917+
871918
kunit_test_suites(&kunit_try_catch_test_suite, &kunit_resource_test_suite,
872919
&kunit_log_test_suite, &kunit_status_test_suite,
873920
&kunit_current_test_suite, &kunit_device_test_suite,
874-
&kunit_fault_test_suite);
921+
&kunit_fault_test_suite, &kunit_stub_test_suite);
875922

876923
MODULE_DESCRIPTION("KUnit test for core test infrastructure");
877924
MODULE_LICENSE("GPL v2");

lib/kunit/test.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ static bool enable_param;
6969
module_param_named(enable, enable_param, bool, 0);
7070
MODULE_PARM_DESC(enable, "Enable KUnit tests");
7171

72+
/*
73+
* Configure the base timeout.
74+
*/
75+
static unsigned long kunit_base_timeout = CONFIG_KUNIT_DEFAULT_TIMEOUT;
76+
module_param_named(timeout, kunit_base_timeout, ulong, 0644);
77+
MODULE_PARM_DESC(timeout, "Set the base timeout for Kunit test cases");
78+
7279
/*
7380
* KUnit statistic mode:
7481
* 0 - disabled
@@ -373,6 +380,40 @@ static void kunit_run_case_check_speed(struct kunit *test,
373380
duration.tv_sec, duration.tv_nsec);
374381
}
375382

383+
/* Returns timeout multiplier based on speed.
384+
* DEFAULT: 1
385+
* KUNIT_SPEED_SLOW: 3
386+
* KUNIT_SPEED_VERY_SLOW: 12
387+
*/
388+
static int kunit_timeout_mult(enum kunit_speed speed)
389+
{
390+
switch (speed) {
391+
case KUNIT_SPEED_SLOW:
392+
return 3;
393+
case KUNIT_SPEED_VERY_SLOW:
394+
return 12;
395+
default:
396+
return 1;
397+
}
398+
}
399+
400+
static unsigned long kunit_test_timeout(struct kunit_suite *suite, struct kunit_case *test_case)
401+
{
402+
int mult = 1;
403+
404+
/*
405+
* The default test timeout is 300 seconds and will be adjusted by mult
406+
* based on the test speed. The test speed will be overridden by the
407+
* innermost test component.
408+
*/
409+
if (suite->attr.speed != KUNIT_SPEED_UNSET)
410+
mult = kunit_timeout_mult(suite->attr.speed);
411+
if (test_case->attr.speed != KUNIT_SPEED_UNSET)
412+
mult = kunit_timeout_mult(test_case->attr.speed);
413+
return mult * kunit_base_timeout * msecs_to_jiffies(MSEC_PER_SEC);
414+
}
415+
416+
376417
/*
377418
* Initializes and runs test case. Does not clean up or do post validations.
378419
*/
@@ -527,7 +568,8 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite,
527568
kunit_try_catch_init(try_catch,
528569
test,
529570
kunit_try_run_case,
530-
kunit_catch_run_case);
571+
kunit_catch_run_case,
572+
kunit_test_timeout(suite, test_case));
531573
context.test = test;
532574
context.suite = suite;
533575
context.test_case = test_case;
@@ -537,7 +579,8 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite,
537579
kunit_try_catch_init(try_catch,
538580
test,
539581
kunit_try_run_case_cleanup,
540-
kunit_catch_run_case_cleanup);
582+
kunit_catch_run_case_cleanup,
583+
kunit_test_timeout(suite, test_case));
541584
kunit_try_catch_run(try_catch, &context);
542585

543586
/* Propagate the parameter result to the test case. */

lib/kunit/try-catch-impl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ struct kunit;
1717
static inline void kunit_try_catch_init(struct kunit_try_catch *try_catch,
1818
struct kunit *test,
1919
kunit_try_catch_func_t try,
20-
kunit_try_catch_func_t catch)
20+
kunit_try_catch_func_t catch,
21+
unsigned long timeout)
2122
{
2223
try_catch->test = test;
2324
try_catch->try = try;
2425
try_catch->catch = catch;
26+
try_catch->timeout = timeout;
2527
}
2628

2729
#endif /* _KUNIT_TRY_CATCH_IMPL_H */

lib/kunit/try-catch.c

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,6 @@ static int kunit_generic_run_threadfn_adapter(void *data)
3434
return 0;
3535
}
3636

37-
static unsigned long kunit_test_timeout(void)
38-
{
39-
/*
40-
* TODO(brendanhiggins@google.com): We should probably have some type of
41-
* variable timeout here. The only question is what that timeout value
42-
* should be.
43-
*
44-
* The intention has always been, at some point, to be able to label
45-
* tests with some type of size bucket (unit/small, integration/medium,
46-
* large/system/end-to-end, etc), where each size bucket would get a
47-
* default timeout value kind of like what Bazel does:
48-
* https://docs.bazel.build/versions/master/be/common-definitions.html#test.size
49-
* There is still some debate to be had on exactly how we do this. (For
50-
* one, we probably want to have some sort of test runner level
51-
* timeout.)
52-
*
53-
* For more background on this topic, see:
54-
* https://mike-bland.com/2011/11/01/small-medium-large.html
55-
*
56-
* If tests timeout due to exceeding sysctl_hung_task_timeout_secs,
57-
* the task will be killed and an oops generated.
58-
*/
59-
return 300 * msecs_to_jiffies(MSEC_PER_SEC); /* 5 min */
60-
}
61-
6237
void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
6338
{
6439
struct kunit *test = try_catch->test;
@@ -85,8 +60,8 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
8560
task_done = task_struct->vfork_done;
8661
wake_up_process(task_struct);
8762

88-
time_remaining = wait_for_completion_timeout(task_done,
89-
kunit_test_timeout());
63+
time_remaining = wait_for_completion_timeout(
64+
task_done, try_catch->timeout);
9065
if (time_remaining == 0) {
9166
try_catch->try_result = -ETIMEDOUT;
9267
kthread_stop(task_struct);

lib/tests/longest_symbol_kunit.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* Test the longest symbol length. Execute with:
44
* ./tools/testing/kunit/kunit.py run longest-symbol
55
* --arch=x86_64 --kconfig_add CONFIG_KPROBES=y --kconfig_add CONFIG_MODULES=y
6-
* --kconfig_add CONFIG_RETPOLINE=n --kconfig_add CONFIG_CFI_CLANG=n
7-
* --kconfig_add CONFIG_MITIGATION_RETPOLINE=n
6+
* --kconfig_add CONFIG_CPU_MITIGATIONS=n --kconfig_add CONFIG_GCOV_KERNEL=n
87
*/
98

109
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

0 commit comments

Comments
 (0)