Skip to content

Commit 1eb69de

Browse files
sulixshuahkh
authored andcommitted
kunit: Fix race condition in try-catch completion
KUnit's try-catch infrastructure now uses vfork_done, which is always set to a valid completion when a kthread is created, but which is set to NULL once the thread terminates. This creates a race condition, where the kthread exits before we can wait on it. Keep a copy of vfork_done, which is taken before we wake_up_process() and so valid, and wait on that instead. Fixes: 9353399 ("kunit: Handle test faults") Reported-by: Linux Kernel Functional Testing <lkft@linaro.org> Closes: https://lore.kernel.org/lkml/20240410102710.35911-1-naresh.kamboju@linaro.org/ Tested-by: Linux Kernel Functional Testing <lkft@linaro.org> Acked-by: Mickaël Salaün <mic@digikod.net> Signed-off-by: David Gow <davidgow@google.com> Reviewed-by: Rae Moar <rmoar@google.com> Tested-by: Miguel Ojeda <ojeda@kernel.org> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 170c317 commit 1eb69de

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

lib/kunit/try-catch.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
6363
{
6464
struct kunit *test = try_catch->test;
6565
struct task_struct *task_struct;
66+
struct completion *task_done;
6667
int exit_code, time_remaining;
6768

6869
try_catch->context = context;
@@ -75,13 +76,16 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
7576
return;
7677
}
7778
get_task_struct(task_struct);
78-
wake_up_process(task_struct);
7979
/*
8080
* As for a vfork(2), task_struct->vfork_done (pointing to the
8181
* underlying kthread->exited) can be used to wait for the end of a
82-
* kernel thread.
82+
* kernel thread. It is set to NULL when the thread exits, so we
83+
* keep a copy here.
8384
*/
84-
time_remaining = wait_for_completion_timeout(task_struct->vfork_done,
85+
task_done = task_struct->vfork_done;
86+
wake_up_process(task_struct);
87+
88+
time_remaining = wait_for_completion_timeout(task_done,
8589
kunit_test_timeout());
8690
if (time_remaining == 0) {
8791
try_catch->try_result = -ETIMEDOUT;

0 commit comments

Comments
 (0)