]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
kunit: Fix race condition in try-catch completion
authorDavid Gow <davidgow@google.com>
Fri, 12 Apr 2024 02:59:01 +0000 (10:59 +0800)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 6 May 2024 20:22:02 +0000 (14:22 -0600)
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: 93533996100c ("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>
lib/kunit/try-catch.c

index fa687278ccc923e1f1a0bd4c900881fc54c70b6c..6bbe0025b0790bd2affacae4bbc36f2739a6f9d7 100644 (file)
@@ -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;
@@ -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;