From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0A8B1EC014; Tue, 15 Oct 2024 11:45:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728992732; cv=none; b=qxrN50dYaY/o5pmpIC9TLfaNPO5f0P38YeZ4JvDOpaHQP8pfrQVWcVaOlOXQskFeD76YUm8AWH4MvFtsRJMrGVf9Am645NAwr88BaUN9a+F9syHYORaA90iP3N3OeA5NaGe/hNQvO34BYfUmScVSj57TCvzeLO/Dc1Y/YxkV//E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728992732; c=relaxed/simple; bh=0FAsMIml8DRc7G5Gj/qNZyBLBK+TfDNmdh0/wSmD/HY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MUbeNQf0w7w4graZY4i88y0Cg9lahUGOf5qmmVDRcQTc/iUiZZHi7NyZwaLHsVwnN0yMknf0N2sjxxxEMuzE9VTwoDrw/tPxaAim6UTy9wsSx3vAB/lc6brtAzdqQ7d41HIvmarTPQvV3GOlVCcX3QAm3ep9BsPqNdg297Mkfb8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=oNUksopc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="oNUksopc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DB5ECC4CECE; Tue, 15 Oct 2024 11:45:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1728992732; bh=0FAsMIml8DRc7G5Gj/qNZyBLBK+TfDNmdh0/wSmD/HY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oNUksopc8XIJgLJdGND+aHVS5b4PxQJK54L/1+3rkpr69L7I9rPArsM2HoAYCTJGy Qut+6o0I7qYAT8tr1FvBMZX4KSHMUxL1sa0jT9LZpcl97tLnam66YQcsSz9OdFl+yh 9+boplaJAtgr0UQPKIfn9Node8ZjKgTvq0HaHV0Q= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chen Yu , Peter Zijlstra , Andrew Morton , Andreas Gruenbacher , David Gow , Mateusz Guzik , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Tejun Heo , Sasha Levin Subject: [PATCH 5.15 202/691] kthread: fix task state in kthread worker if being frozen Date: Tue, 15 Oct 2024 13:22:30 +0200 Message-ID: <20241015112448.375859859@linuxfoundation.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241015112440.309539031@linuxfoundation.org> References: <20241015112440.309539031@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Chen Yu [ Upstream commit e16c7b07784f3fb03025939c4590b9a7c64970a7 ] When analyzing a kernel waring message, Peter pointed out that there is a race condition when the kworker is being frozen and falls into try_to_freeze() with TASK_INTERRUPTIBLE, which could trigger a might_sleep() warning in try_to_freeze(). Although the root cause is not related to freeze()[1], it is still worthy to fix this issue ahead. One possible race scenario: CPU 0 CPU 1 ----- ----- // kthread_worker_fn set_current_state(TASK_INTERRUPTIBLE); suspend_freeze_processes() freeze_processes static_branch_inc(&freezer_active); freeze_kernel_threads pm_nosig_freezing = true; if (work) { //false __set_current_state(TASK_RUNNING); } else if (!freezing(current)) //false, been frozen freezing(): if (static_branch_unlikely(&freezer_active)) if (pm_nosig_freezing) return true; schedule() } // state is still TASK_INTERRUPTIBLE try_to_freeze() might_sleep() <--- warning Fix this by explicitly set the TASK_RUNNING before entering try_to_freeze(). Link: https://lore.kernel.org/lkml/Zs2ZoAcUsZMX2B%2FI@chenyu5-mobl2/ [1] Link: https://lkml.kernel.org/r/20240827112308.181081-1-yu.c.chen@intel.com Fixes: b56c0d8937e6 ("kthread: implement kthread_worker") Signed-off-by: Chen Yu Suggested-by: Peter Zijlstra Suggested-by: Andrew Morton Cc: Andreas Gruenbacher Cc: David Gow Cc: Mateusz Guzik Cc: Mickaël Salaün Cc: Tejun Heo Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- kernel/kthread.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/kernel/kthread.c b/kernel/kthread.c index 4cc6897b7ca40..13cfe01ed2fc7 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -781,8 +781,16 @@ int kthread_worker_fn(void *worker_ptr) * event only cares about the address. */ trace_sched_kthread_work_execute_end(work, func); - } else if (!freezing(current)) + } else if (!freezing(current)) { schedule(); + } else { + /* + * Handle the case where the current remains + * TASK_INTERRUPTIBLE. try_to_freeze() expects + * the current to be TASK_RUNNING. + */ + __set_current_state(TASK_RUNNING); + } try_to_freeze(); cond_resched(); -- 2.43.0