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 6F6C81AB6E2; Mon, 14 Oct 2024 15:00:02 +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=1728918002; cv=none; b=lArRVTJ6CfCE/gsF4Of5AoO/sTtksGdJzB4gAbl9YZGZ4B511eUZq2yyWK2uAWzhqtDcFfkr/7ln1IdXL5Xp2GrVxKR83oT9v7kZ4pI3YC0/5xqz+9FMNNC++6Zr3+RSQi1gFV8oPIMPb1tNFfnwSXdWqBzxx7eDP9ryIu5Uzlg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728918002; c=relaxed/simple; bh=bDypw+SCBYOH3+CIYIQPg1PPmxUdZDNjZOF2ciaBV0g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=L1SLB/TLUyE6PK4QO/iHZSstRXbpXKXGKi8i4qNZJ0dW7sQNBFYAplAri5dgSzA7biXQXpcTTHKEWa02T2n1oO4/5JZWAFtu0dV4qG8KaJ2ZOQLipeauUPUorvs4VRrD0UoqnhPe2HyRtyJzE8n/HyVfwcf7ZI9S6tmbNvcg7XU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=PtRT3rkf; 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="PtRT3rkf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 58DA8C4CEC3; Mon, 14 Oct 2024 15:00:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1728918002; bh=bDypw+SCBYOH3+CIYIQPg1PPmxUdZDNjZOF2ciaBV0g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PtRT3rkf7F2tJ4SSJHblAGOym7d0HsRCBp8sr441tSCSnhEWV8DWvnSMTvficbmEj +NOcHf6SfhbYLB37r73WnHx5jDrOsAFoLsd+1lKTOZZqV6xqJ8qRgV2Xb2iO3Mwy3z oYypXeDqh5FcnTZiiCBohU2DoGSlAC/ewU9gbwPM= 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 6.1 163/798] kthread: fix task state in kthread worker if being frozen Date: Mon, 14 Oct 2024 16:11:57 +0200 Message-ID: <20241014141224.333110594@linuxfoundation.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241014141217.941104064@linuxfoundation.org> References: <20241014141217.941104064@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 6.1-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 f97fd01a29325..bf6eff073cd43 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -826,8 +826,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