From: Oleg Nesterov Date: Mon, 24 Aug 2009 10:45:29 +0000 (+0200) Subject: kthreads: fix kthread_create() vs kthread_stop() race X-Git-Tag: v2.6.27.32~12 X-Git-Url: https://git.openpandora.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b9246e2b55f8aa3c3d9732d0eb7e9943a6f9492;p=pandora-kernel.git kthreads: fix kthread_create() vs kthread_stop() race The bug should be "accidently" fixed by recent changes in 2.6.31, all kernels <= 2.6.30 need the fix. The problem was never noticed before, it was found because it causes mysterious failures with GFS mount/umount. Credits to Robert Peterson. He blaimed kthread.c from the very beginning. But, despite my promise, I forgot to inspect the old implementation until he did a lot of testing and reminded me. This led to huge delay in fixing this bug. kthread_stop() does put_task_struct(k) before it clears kthread_stop_info.k. This means another kthread_create() can re-use this task_struct, but the new kthread can still see kthread_should_stop() == T and exit even without calling threadfn(). Reported-by: Robert Peterson Tested-by: Robert Peterson Signed-off-by: Oleg Nesterov Acked-by: Rusty Russell Signed-off-by: Greg Kroah-Hartman --- diff --git a/kernel/kthread.c b/kernel/kthread.c index 96cff2f8710b..9548d5210d26 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -213,12 +213,12 @@ int kthread_stop(struct task_struct *k) /* Now set kthread_should_stop() to true, and wake it up. */ kthread_stop_info.k = k; wake_up_process(k); - put_task_struct(k); /* Once it dies, reset stop ptr, gather result and we're done. */ wait_for_completion(&kthread_stop_info.done); kthread_stop_info.k = NULL; ret = kthread_stop_info.err; + put_task_struct(k); mutex_unlock(&kthread_stop_lock); return ret;