Re: [PATCH v33 1/7] sched/psi: Preserve sleep-persistent PSI flags on off-CPU sleeping dequeues

From: John Stultz

Date: Wed Nov 25 2026 - 20:32:23 EST


On Sun, Oct 25, 2026 at 10:00 PM K Prateek Nayak <kprateek.nayak@xxxxxxx> wrote:
> On 10/24/2026 5:14 PM, Peter Zijlstra wrote:
> > On Fri, Oct 23, 2026 at 10:04:12PM +0000, John Stultz wrote:
> >> diff --git a/kernel/sched/stats.h b/kernel/sched/stats.h
> >> index ebe0a7765f98e..20820fe1021a1 100644
> >> --- a/kernel/sched/stats.h
> >> +++ b/kernel/sched/stats.h
> >> @@ -175,12 +175,28 @@ static inline void psi_dequeue(struct task_struct *p, int flags)
> >> * Do nothing here.
> >> *
> >> * In the SCHED_PROXY_EXECUTION case we may do sleeping
> >> - * dequeues that are not followed by a task switch, so check
> >> - * TSK_ONCPU is set to ensure the task switch is imminent.
> >> - * Otherwise clear the flags as usual.
> >> + * dequeues that are not followed by a task switch. If TSK_ONCPU
> >> + * is not set (meaning we already switched away while the task
> >> + * stayed on the rq as a blocked donor), handle the sleeping
> >> + * state transition here so sleep-persistent states (TSK_MEMSTALL,
> >> + * TSK_IOWAIT) are preserved.
> >> */
> >> - if ((flags & DEQUEUE_SLEEP) && (p->psi_flags & TSK_ONCPU))
> >> + if (flags & DEQUEUE_SLEEP) {
> >> + int clear = TSK_RUNNING, set = 0;
> >> +
> >> + if (p->psi_flags & TSK_ONCPU)
> >> + return;
> >> + if (p->se.sched_delayed)
> >> + return;
> >
> > Must this really be sched_delayed, that is, cannot p->is_blocked be used
> > here? I'm trying to push sched_delayed into fair.c if at all possible.
>
> In this case, blocked donors going to sleep would need
> to set the PSI flags accordingly (do what psi_sched_switch()
> missed).
>
> "p->se.sched_delayed" tasks would have already set those indicators
> but can still arrive here from dequeue_task(SLEEP | DELAYED) of
> sched_change_begin() - in all those cases, the dequeue flags will
> also have DEQUEUE_DELAYED which can be used instead to bail out
> early here.
>
> Would that be okay?

That looks like it is working. I'll include it in my next iteration!
Thanks again for the suggestion!
-john