From nobody Sun Feb 8 13:11:23 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B4316C7EE24 for ; Mon, 5 Jun 2023 19:17:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235064AbjFETRM (ORCPT ); Mon, 5 Jun 2023 15:17:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235314AbjFETQZ (ORCPT ); Mon, 5 Jun 2023 15:16:25 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3FBB106; Mon, 5 Jun 2023 12:16:22 -0700 (PDT) Date: Mon, 05 Jun 2023 19:16:20 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1685992581; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=64qXtPcj4Jdo/8zlj4PIoNJS9+khEW9797U80Cy1DQ0=; b=XjHir3xp5/LyIymyml3Q7/UR5Y+nm+wlxC8buXG2Us4KgN5n2/U7jVXvkQWk7+V/9xghCt SE3Senq9lC5dQ5fKiyBl7x5Ba23iP5TO8li99XW3Ek2gHsEKAj5fMmclTPQ1P+0YLkVH+J y1NpwANVVzsheqJCcPNtMDl+m4/gn11AEQs2dr76RrspL89gXpnbbbCvMat0EKzH5wdLtA mf6jLEOUWmz+hcu2rmWoL0kBD5dPMXIIDoiG2y6EtwKBmLvlvBm5HO4OUXQ/PmVQfcCuPg J/i5hgFSBpC2OgWHsC4+PCQsbAE16a6lithllPAoXi5zD5Li4cGfXzMJAxMv+Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1685992581; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=64qXtPcj4Jdo/8zlj4PIoNJS9+khEW9797U80Cy1DQ0=; b=WAJ+Dl2nRlgF03dEEhslF7WuHwk1z9Xel99Lr5m5kNzbkj4r9oH7Z9+q9YHe9yKsf1gy9t 39iAg3bRaTXqIGAw== From: "tip-bot2 for Peter Zijlstra" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] sched: Consider task_struct::saved_state in wait_task_inactive() Cc: Sebastian Andrzej Siewior , "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230601091234.GW83892@hirez.programming.kicks-ass.net> References: <20230601091234.GW83892@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Message-ID: <168599258059.404.17087833652728912462.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the sched/core branch of tip: Commit-ID: 1c06918788e8ae6e69e4381a2806617312922524 Gitweb: https://git.kernel.org/tip/1c06918788e8ae6e69e4381a280661731= 2922524 Author: Peter Zijlstra AuthorDate: Wed, 31 May 2023 16:39:07 +02:00 Committer: Peter Zijlstra CommitterDate: Mon, 05 Jun 2023 21:11:03 +02:00 sched: Consider task_struct::saved_state in wait_task_inactive() With the introduction of task_struct::saved_state in commit 5f220be21418 ("sched/wakeup: Prepare for RT sleeping spin/rwlocks") matching the task state has gotten more complicated. That same commit changed try_to_wake_up() to consider both states, but wait_task_inactive() has been neglected. Sebastian noted that the wait_task_inactive() usage in ptrace_check_attach() can misbehave when ptrace_stop() is blocked on the tasklist_lock after it sets TASK_TRACED. Therefore extract a common helper from ttwu_state_match() and use that to teach wait_task_inactive() about the PREEMPT_RT locks. Originally-by: Sebastian Andrzej Siewior Signed-off-by: Peter Zijlstra (Intel) Tested-by: Sebastian Andrzej Siewior Link: https://lkml.kernel.org/r/20230601091234.GW83892@hirez.programming.ki= cks-ass.net --- kernel/sched/core.c | 59 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 810cf7d..ac38225 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -2213,6 +2213,39 @@ void check_preempt_curr(struct rq *rq, struct task_s= truct *p, int flags) rq_clock_skip_update(rq); } =20 +static __always_inline +int __task_state_match(struct task_struct *p, unsigned int state) +{ + if (READ_ONCE(p->__state) & state) + return 1; + +#ifdef CONFIG_PREEMPT_RT + if (READ_ONCE(p->saved_state) & state) + return -1; +#endif + return 0; +} + +static __always_inline +int task_state_match(struct task_struct *p, unsigned int state) +{ +#ifdef CONFIG_PREEMPT_RT + int match; + + /* + * Serialize against current_save_and_set_rtlock_wait_state() and + * current_restore_rtlock_saved_state(). + */ + raw_spin_lock_irq(&p->pi_lock); + match =3D __task_state_match(p, state); + raw_spin_unlock_irq(&p->pi_lock); + + return match; +#else + return __task_state_match(p, state); +#endif +} + /* * wait_task_inactive - wait for a thread to unschedule. * @@ -2231,7 +2264,7 @@ void check_preempt_curr(struct rq *rq, struct task_st= ruct *p, int flags) */ unsigned long wait_task_inactive(struct task_struct *p, unsigned int match= _state) { - int running, queued; + int running, queued, match; struct rq_flags rf; unsigned long ncsw; struct rq *rq; @@ -2257,7 +2290,7 @@ unsigned long wait_task_inactive(struct task_struct *= p, unsigned int match_state * is actually now running somewhere else! */ while (task_on_cpu(rq, p)) { - if (!(READ_ONCE(p->__state) & match_state)) + if (!task_state_match(p, match_state)) return 0; cpu_relax(); } @@ -2272,8 +2305,15 @@ unsigned long wait_task_inactive(struct task_struct = *p, unsigned int match_state running =3D task_on_cpu(rq, p); queued =3D task_on_rq_queued(p); ncsw =3D 0; - if (READ_ONCE(p->__state) & match_state) + if ((match =3D __task_state_match(p, match_state))) { + /* + * When matching on p->saved_state, consider this task + * still queued so it will wait. + */ + if (match < 0) + queued =3D 1; ncsw =3D p->nvcsw | LONG_MIN; /* sets MSB */ + } task_rq_unlock(rq, p, &rf); =20 /* @@ -4003,15 +4043,14 @@ static void ttwu_queue(struct task_struct *p, int c= pu, int wake_flags) static __always_inline bool ttwu_state_match(struct task_struct *p, unsigned int state, int *succ= ess) { + int match; + if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)) { WARN_ON_ONCE((state & TASK_RTLOCK_WAIT) && state !=3D TASK_RTLOCK_WAIT); } =20 - if (READ_ONCE(p->__state) & state) { - *success =3D 1; - return true; - } + *success =3D !!(match =3D __task_state_match(p, state)); =20 #ifdef CONFIG_PREEMPT_RT /* @@ -4027,12 +4066,10 @@ bool ttwu_state_match(struct task_struct *p, unsign= ed int state, int *success) * p::saved_state to TASK_RUNNING so any further tests will * not result in false positives vs. @success */ - if (p->saved_state & state) { + if (match < 0) p->saved_state =3D TASK_RUNNING; - *success =3D 1; - } #endif - return false; + return match > 0; } =20 /*