Skip to content

Commit 3a35c13

Browse files
l0kodshuahkh
authored andcommitted
kunit: Handle test faults
Previously, when a kernel test thread crashed (e.g. NULL pointer dereference, general protection fault), the KUnit test hanged for 30 seconds and exited with a timeout error. Fix this issue by waiting on task_struct->vfork_done instead of the custom kunit_try_catch.try_completion, and track the execution state by initially setting try_result with -EINTR and only setting it to 0 if the test passed. Fix kunit_generic_run_threadfn_adapter() signature by returning 0 instead of calling kthread_complete_and_exit(). Because thread's exit code is never checked, always set it to 0 to make it clear. To make this explicit, export kthread_exit() for KUnit tests built as module. Fix the -EINTR error message, which couldn't be reached until now. This is tested with a following patch. Cc: Brendan Higgins <brendanhiggins@google.com> Cc: Eric W. Biederman <ebiederm@xmission.com> Cc: Shuah Khan <skhan@linuxfoundation.org> Reviewed-by: Kees Cook <keescook@chromium.org> Reviewed-by: David Gow <davidgow@google.com> Tested-by: Rae Moar <rmoar@google.com> Signed-off-by: Mickaël Salaün <mic@digikod.net> Link: https://lore.kernel.org/r/20240408074625.65017-5-mic@digikod.net Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 53026ff commit 3a35c13

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

include/kunit/try-catch.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414

1515
typedef void (*kunit_try_catch_func_t)(void *);
1616

17-
struct completion;
1817
struct kunit;
1918

2019
/**
2120
* struct kunit_try_catch - provides a generic way to run code which might fail.
2221
* @test: The test case that is currently being executed.
23-
* @try_completion: Completion that the control thread waits on while test runs.
2422
* @try_result: Contains any errno obtained while running test case.
2523
* @try: The function, the test case, to attempt to run.
2624
* @catch: The function called if @try bails out.
@@ -46,7 +44,6 @@ struct kunit;
4644
struct kunit_try_catch {
4745
/* private: internal use only. */
4846
struct kunit *test;
49-
struct completion *try_completion;
5047
int try_result;
5148
kunit_try_catch_func_t try;
5249
kunit_try_catch_func_t catch;

kernel/kthread.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ void __noreturn kthread_exit(long result)
315315
kthread->result = result;
316316
do_exit(0);
317317
}
318+
EXPORT_SYMBOL(kthread_exit);
318319

319320
/**
320321
* kthread_complete_and_exit - Exit the current kthread.

lib/kunit/try-catch.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818
void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)
1919
{
2020
try_catch->try_result = -EFAULT;
21-
kthread_complete_and_exit(try_catch->try_completion, -EFAULT);
21+
kthread_exit(0);
2222
}
2323
EXPORT_SYMBOL_GPL(kunit_try_catch_throw);
2424

2525
static int kunit_generic_run_threadfn_adapter(void *data)
2626
{
2727
struct kunit_try_catch *try_catch = data;
2828

29+
try_catch->try_result = -EINTR;
2930
try_catch->try(try_catch->context);
31+
if (try_catch->try_result == -EINTR)
32+
try_catch->try_result = 0;
3033

31-
kthread_complete_and_exit(try_catch->try_completion, 0);
34+
return 0;
3235
}
3336

3437
static unsigned long kunit_test_timeout(void)
@@ -58,13 +61,11 @@ static unsigned long kunit_test_timeout(void)
5861

5962
void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
6063
{
61-
DECLARE_COMPLETION_ONSTACK(try_completion);
6264
struct kunit *test = try_catch->test;
6365
struct task_struct *task_struct;
6466
int exit_code, time_remaining;
6567

6668
try_catch->context = context;
67-
try_catch->try_completion = &try_completion;
6869
try_catch->try_result = 0;
6970
task_struct = kthread_create(kunit_generic_run_threadfn_adapter,
7071
try_catch, "kunit_try_catch_thread");
@@ -75,8 +76,12 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
7576
}
7677
get_task_struct(task_struct);
7778
wake_up_process(task_struct);
78-
79-
time_remaining = wait_for_completion_timeout(&try_completion,
79+
/*
80+
* As for a vfork(2), task_struct->vfork_done (pointing to the
81+
* underlying kthread->exited) can be used to wait for the end of a
82+
* kernel thread.
83+
*/
84+
time_remaining = wait_for_completion_timeout(task_struct->vfork_done,
8085
kunit_test_timeout());
8186
if (time_remaining == 0) {
8287
try_catch->try_result = -ETIMEDOUT;
@@ -92,7 +97,7 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
9297
if (exit_code == -EFAULT)
9398
try_catch->try_result = 0;
9499
else if (exit_code == -EINTR)
95-
kunit_err(test, "wake_up_process() was never called\n");
100+
kunit_err(test, "try faulted\n");
96101
else if (exit_code == -ETIMEDOUT)
97102
kunit_err(test, "try timed out\n");
98103
else if (exit_code)

0 commit comments

Comments
 (0)