public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>,
	"Eric W . Biederman" <ebiederm@xmission.com>,
	Sasha Levin <sashal@kernel.org>,
	akpm@linux-foundation.org, pmladek@suse.com,
	laoar.shao@gmail.com, dinguyen@kernel.org, cai.huoqing@linux.dev,
	arnd@arndb.de
Subject: [PATCH AUTOSEL 5.19 01/10] signal: break out of wait loops on kthread_stop()
Date: Fri, 14 Oct 2022 09:52:12 -0400	[thread overview]
Message-ID: <20221014135222.2109334-1-sashal@kernel.org> (raw)

From: "Jason A. Donenfeld" <Jason@zx2c4.com>

[ Upstream commit a7c01fa93aeb03ab76cd3cb2107990dd160498e6 ]

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 the 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, by setting TIF_NOTIFY_SIGNAL.

The same also probably applies to the similar kthread_park()
functionality, but that can be addressed later, as its semantics are
slightly different.

Cc: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
v1: https://lkml.kernel.org/r/20220627120020.608117-1-Jason@zx2c4.com
v2: https://lkml.kernel.org/r/20220627145716.641185-1-Jason@zx2c4.com
v3: https://lkml.kernel.org/r/20220628161441.892925-1-Jason@zx2c4.com
v4: https://lkml.kernel.org/r/20220711202136.64458-1-Jason@zx2c4.com
v5: https://lkml.kernel.org/r/20220711232123.136330-1-Jason@zx2c4.com
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 kernel/kthread.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/kernel/kthread.c b/kernel/kthread.c
index 3c677918d8f2..7243a010f433 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -704,6 +704,7 @@ int kthread_stop(struct task_struct *k)
 	kthread = to_kthread(k);
 	set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
 	kthread_unpark(k);
+	set_tsk_thread_flag(k, TIF_NOTIFY_SIGNAL);
 	wake_up_process(k);
 	wait_for_completion(&kthread->exited);
 	ret = kthread->result;
-- 
2.35.1


             reply	other threads:[~2022-10-14 13:53 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-14 13:52 Sasha Levin [this message]
2022-10-14 13:52 ` [PATCH AUTOSEL 5.19 02/10] powerpc/selftests: Use timersub() for gettimeofday() Sasha Levin
2022-10-14 13:52 ` [PATCH AUTOSEL 5.19 03/10] kvm: x86: Do proper cleanup if kvm_x86_ops->vm_init() fails Sasha Levin
2022-11-02 18:01   ` Paolo Bonzini
2022-10-14 13:52 ` [PATCH AUTOSEL 5.19 04/10] powerpc/math-emu: Remove -w build flag and fix warnings Sasha Levin
2022-10-14 13:52 ` [PATCH AUTOSEL 5.19 05/10] powerpc/85xx: Fix fall-through warning for Clang Sasha Levin
2022-10-14 13:52 ` [PATCH AUTOSEL 5.19 06/10] powerpc: Remove direct call to personality syscall handler Sasha Levin
2022-10-14 13:52 ` [PATCH AUTOSEL 5.19 07/10] powerpc/perf: Fix branch_filter support for multiple filters Sasha Levin
2022-10-14 13:52 ` [PATCH AUTOSEL 5.19 08/10] powerpc/rtas: block error injection when locked down Sasha Levin
2022-10-14 13:52 ` [PATCH AUTOSEL 5.19 09/10] powerpc/mm: Fix UBSAN warning reported on hugetlb Sasha Levin
2022-10-14 13:52 ` [PATCH AUTOSEL 5.19 10/10] powerpc/64: Fix msr_check_and_set/clear MSR[EE] race Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221014135222.2109334-1-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=cai.huoqing@linux.dev \
    --cc=dinguyen@kernel.org \
    --cc=ebiederm@xmission.com \
    --cc=laoar.shao@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmladek@suse.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox