path: root/kernel
diff options
authorJason A. Donenfeld <Jason@zx2c4.com>2022-06-27 13:40:47 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2022-06-27 14:06:49 +0200
commiteb64b3bcd2b00abf6bd0fef6494941dfb6f5c9c5 (patch)
tree84b699bd81865352b494c31fa782046826bde976 /kernel
parentm68k: virt: use RNG seed from bootinfo block (diff)
signal: break out of wait loops on kthread_stop()
I was recently surprised to learn that msleep_interruptible(), wait_for_completion_interruptible_timeout(), and related functions simply hung when I called kthread_stop() on kthreads using them. The solution to fixing the case with msleep_interruptible() was more simply to move to schedule_timeout_interruptible(). Why? The reason is that msleep_interruptible(), and many functions just like it, has a loop like this: while (timeout && !signal_pending(current)) timeout = schedule_timeout_interruptible(timeout); The call to kthread_stop() woke up the thread, so schedule_timeout_ interruptible() returned early, but because signal_pending() returned true, it went back into another timeout, which was never woken up. This wait loop pattern is common to various pieces of code, and I suspect that subtle misuse in a kthread that caused a deadlock in the code I looked at last week is also found elsewhere. So this commit causes signal_pending() to return true when kthread_stop() is called. This is already what's done for TIF_NOTIFY_SIGNAL, for these same purposes of breaking out of wait loops, so a similar KTHREAD_SHOULD_STOP check isn't too much different. Cc: Ingo Molnar <mingo@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Eric W. Biederman <ebiederm@xmission.com> Cc: Toke Høiland-Jørgensen <toke@redhat.com> Cc: Kalle Valo <kvalo@kernel.org> Cc: Johannes Berg <johannes@sipsolutions.net> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'kernel')
1 files changed, 6 insertions, 0 deletions
diff --git a/kernel/kthread.c b/kernel/kthread.c
index 3c677918d8f2..7e0743330cd4 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -145,6 +145,12 @@ void free_kthread_struct(struct task_struct *k)
+bool __kthread_should_stop(struct task_struct *k)
+ return (k->flags & PF_KTHREAD) &&
+ test_bit(KTHREAD_SHOULD_STOP, &to_kthread(k)->flags);
* kthread_should_stop - should this kthread return now?