From nobody Thu Sep 11 16:20:30 2025 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 99A97C6379F for ; Fri, 17 Feb 2023 14:53:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229942AbjBQOxg (ORCPT ); Fri, 17 Feb 2023 09:53:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229896AbjBQOxd (ORCPT ); Fri, 17 Feb 2023 09:53:33 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CB583B0D7 for ; Fri, 17 Feb 2023 06:53:15 -0800 (PST) Date: Fri, 17 Feb 2023 15:53:02 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1676645593; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=eBITpJvFAgJDlrvkIZ8qiH8cZafnQorRAJ90UsZZfh8=; b=DEAEl9roZus/CNCAm5+6poOLk9HQXmUVToZJmyysfvdx0XhtspkSxOZX0ReGtGqU/hxxAm MNyl7TgMNRZh8QjVd/ywNPw/nXxWT7iMQFEGQfdqVG4tVprovvtO0bEp6tBIiWr5dgRsw5 pa17jLPT2TqU5npTsXmTQdhrUoVbGNaNlyZJ2m9tz2uKNFrjMlqmsj62ePTRQqv/ommd+I R7ZPLpzpREfztW3scv7VEsipGzU9X+LEBLOfUuyzkCEepkJFA5WAUeKHorwVckAUC475RX FlwFI88g9aEnX6GSObvFNwvLlHp+Ru0UCBuHnp7eI8nMLXfdfvYgtxUTBqv4FA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1676645593; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=eBITpJvFAgJDlrvkIZ8qiH8cZafnQorRAJ90UsZZfh8=; b=8Kv/gmXsSjVXxQ9/NI0Wdr+MXMkGPlc67msI8eJuJ9o2FICMtJfX/otamCA+Edc6xotfRO /RRz0glgXysZWUCA== From: Sebastian Andrzej Siewior To: linux-kernel@vger.kernel.org Cc: Ben Segall , Daniel Bristot de Oliveira , Dietmar Eggemann , Ingo Molnar , Juri Lelli , Mel Gorman , Peter Zijlstra , Steven Rostedt , Thomas Gleixner , Valentin Schneider , Vincent Guittot Subject: [PATCH] sched: Consider task_struct::saved_state in wait_task_inactive(). Message-ID: MIME-Version: 1.0 Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" wait_task_inactive() waits for thread to unschedule in a certain task state. On PREEMPT_RT that state may be stored in task_struct::saved_state while the thread, that is being waited for, blocks on a sleeping lock and task_struct::__state is set to TASK_RTLOCK_WAIT. It is not possible to check only for TASK_RTLOCK_WAIT to be sure that the t= ask is blocked on a sleeping lock because during wake up (after the sleeping lo= ck has been acquired) the task state is set TASK_RUNNING. After the task in on= CPU and acquired the pi_lock it will reset the state accordingly but until then TASK_RUNNING will be observed (with the desired state is saved in saved_sta= te). Check also for task_struct::saved_state if the desired match was not found = in task_struct::__state on PREEMPT_RT. If the state was found in saved_state, = wait until the task is idle and state is visible in task_struct::__state. Signed-off-by: Sebastian Andrzej Siewior Reviewed-by: Valentin Schneider Tested-by: Sebastian Andrzej Siewior --- Repost of https://lore.kernel.org/Yt%2FpQAFQ1xKNK0RY@linutronix.de kernel/sched/core.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++= +---- 1 file changed, 76 insertions(+), 5 deletions(-) --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3266,6 +3266,76 @@ int migrate_swap(struct task_struct *cur } #endif /* CONFIG_NUMA_BALANCING */ =20 +#ifdef CONFIG_PREEMPT_RT + +/* + * Consider: + * + * set_special_state(X); + * + * do_things() + * // Somewhere in there is an rtlock that can be contended: + * current_save_and_set_rtlock_wait_state(); + * [...] + * schedule_rtlock(); (A) + * [...] + * current_restore_rtlock_saved_state(); + * + * schedule(); (B) + * + * If p->saved_state is anything else than TASK_RUNNING, then p blocked on= an + * rtlock (A) *before* voluntarily calling into schedule() (B) after setti= ng its + * state to X. For things like ptrace (X=3DTASK_TRACED), the task could ha= ve more + * work to do upon acquiring the lock in do_things() before whoever called + * wait_task_inactive() should return. IOW, we have to wait for: + * + * p.saved_state =3D TASK_RUNNING + * p.__state =3D X + * + * which implies the task isn't blocked on an RT lock and got to schedule(= ) (B). + * + * Also see comments in ttwu_state_match(). + */ + +static __always_inline bool state_mismatch(struct task_struct *p, unsigned= int match_state) +{ + unsigned long flags; + bool mismatch; + + raw_spin_lock_irqsave(&p->pi_lock, flags); + if (READ_ONCE(p->__state) & match_state) + mismatch =3D false; + else if (READ_ONCE(p->saved_state) & match_state) + mismatch =3D false; + else + mismatch =3D true; + + raw_spin_unlock_irqrestore(&p->pi_lock, flags); + return mismatch; +} +static __always_inline bool state_match(struct task_struct *p, unsigned in= t match_state, + bool *wait) +{ + if (READ_ONCE(p->__state) & match_state) + return true; + if (READ_ONCE(p->saved_state) & match_state) { + *wait =3D true; + return true; + } + return false; +} +#else +static __always_inline bool state_mismatch(struct task_struct *p, unsigned= int match_state) +{ + return !(READ_ONCE(p->__state) & match_state); +} +static __always_inline bool state_match(struct task_struct *p, unsigned in= t match_state, + bool *wait) +{ + return (READ_ONCE(p->__state) & match_state); +} +#endif + /* * wait_task_inactive - wait for a thread to unschedule. * @@ -3284,7 +3354,7 @@ int migrate_swap(struct task_struct *cur */ unsigned long wait_task_inactive(struct task_struct *p, unsigned int match= _state) { - int running, queued; + bool running, wait; struct rq_flags rf; unsigned long ncsw; struct rq *rq; @@ -3310,7 +3380,7 @@ unsigned long wait_task_inactive(struct * is actually now running somewhere else! */ while (task_on_cpu(rq, p)) { - if (!(READ_ONCE(p->__state) & match_state)) + if (state_mismatch(p, match_state)) return 0; cpu_relax(); } @@ -3323,9 +3393,10 @@ unsigned long wait_task_inactive(struct rq =3D task_rq_lock(p, &rf); trace_sched_wait_task(p); running =3D task_on_cpu(rq, p); - queued =3D task_on_rq_queued(p); + wait =3D task_on_rq_queued(p); ncsw =3D 0; - if (READ_ONCE(p->__state) & match_state) + + if (state_match(p, match_state, &wait)) ncsw =3D p->nvcsw | LONG_MIN; /* sets MSB */ task_rq_unlock(rq, p, &rf); =20 @@ -3355,7 +3426,7 @@ unsigned long wait_task_inactive(struct * running right now), it's preempted, and we should * yield - it could be a while. */ - if (unlikely(queued)) { + if (unlikely(wait)) { ktime_t to =3D NSEC_PER_SEC / HZ; =20 set_current_state(TASK_UNINTERRUPTIBLE);