Skip to content

Commit

Permalink
kunit: Fix race condition in try-catch completion
Browse files Browse the repository at this point in the history
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 <[email protected]>
Closes: https://lore.kernel.org/lkml/[email protected]/
Tested-by: Linux Kernel Functional Testing <[email protected]>
Acked-by: Mickaël Salaün <[email protected]>
Signed-off-by: David Gow <[email protected]>
Reviewed-by: Rae Moar <[email protected]>
Tested-by: Miguel Ojeda <[email protected]>
Signed-off-by: Shuah Khan <[email protected]>
  • Loading branch information
sulix authored and shuahkh committed May 6, 2024
1 parent 170c317 commit 1eb69de
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/kunit/try-catch.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
{
struct kunit *test = try_catch->test;
struct task_struct *task_struct;
struct completion *task_done;
int exit_code, time_remaining;

try_catch->context = context;
Expand All @@ -75,13 +76,16 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
return;
}
get_task_struct(task_struct);
wake_up_process(task_struct);
/*
* As for a vfork(2), task_struct->vfork_done (pointing to the
* underlying kthread->exited) can be used to wait for the end of a
* kernel thread.
* kernel thread. It is set to NULL when the thread exits, so we
* keep a copy here.
*/
time_remaining = wait_for_completion_timeout(task_struct->vfork_done,
task_done = task_struct->vfork_done;
wake_up_process(task_struct);

time_remaining = wait_for_completion_timeout(task_done,
kunit_test_timeout());
if (time_remaining == 0) {
try_catch->try_result = -ETIMEDOUT;
Expand Down

0 comments on commit 1eb69de

Please sign in to comment.