sched/stats: Fix run_delay over-count for migrated sched_delayed tasks
PATCH v3 1/1 sched/stats: Fix run_delay over-count for migrated sched_delayed tasks
From: Wei Yang <[email protected]>
With DELAY_DEQUEUE, a blocked task stays on the runqueue with se.sched_delayed set and its sched_info.last_queued is cleared, so the sleep is not counted into run_delay.
When such a delayed (sleeping) task is migrated across CPUs via the plain migration paths (move_queued_task / move_queued_task_locked, the latter used by __migrate_swap_task), activate_task(dst, 0) calls enqueue_task() without ENQUEUE_RESTORE, re-arming last_queued to the migration timestamp while the task is still sleeping. The later real wakeup (ENQUEUE_DELAYED) tries to re-arm last_queued at wakeup time but is suppressed because last_queued is already non-zero, so sched_info_arrive() folds the whole sleep duration between migration and wakeup into run_delay.
Load balance is affected too: the sched_delayed check in can_migrate_task() bails out only when env->migration_type != migrate_load, so it does not block migration when the type is migrate_load - which active load balance always uses (its lb_env leaves migration_type at 0 == migrate_load), and which regular load balance can also use via calculate_imbalance(). Either way the re-attach goes through attach_task() -> activate_task(rq, p, ENQUEUE_NOCLOCK), without ENQUEUE_RESTORE.
Fix by not re-arming last_queued for a sched_delayed task in sched_info_enqueue(). The wakeup path clears sched_delayed before reaching sched_info_enqueue(), so it still re-arms at the real wakeup time. Plain runnable tasks are unaffected.
---
Changes in v3:
- Collect Reviewed-by from Kayra Cizmeci and K Prateek Nayak.
- Collect Tested-by from K Prateek Nayak.
Changes in v2:
- Correct the load-balance description: sched_delayed tasks are not excluded from active load balancing. can_migrate_task()'s sched_delayed check only bails out when env->migration_type != migrate_load, so active load balance (which always uses migrate_load) still migrates them.
- Collect Reviewed-by from Chen Yu.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
kernel/sched/stats.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/stats.h +1 -1
@@ -290,7 +290,7 @@ static void sched_info_arrive(struct rq *rq, struct task_struct *t)*/ static inline void sched_info_enqueue (struct rq *rq,struct task_struct *t) { -if (!t->sched_info.last_queued) +if (!t->sched_info.last_queued && !t->se.sched_delayed) t->sched_info.last_queued =rq_clock (rq); }
Signature · 1 line
2.43.7
Peter Zijlstra
maintainerQuoted 45 lines · On Tue, Sep 22, 2026 at 04:24:32PM +0800, [email protected] wrote:
From: Wei Yang <[email protected]>
With
DELAY_DEQUEUE, a blocked task stays on the runqueue with se.sched_delayed set and its sched_info.last_queued is cleared, so the sleep is not counted intorun_delay.When such a delayed (sleeping) task is migrated across CPUs via the plain migration paths (
move_queued_task/move_queued_task_locked, the latter used by__migrate_swap_task),activate_task(dst, 0)callsenqueue_task()withoutENQUEUE_RESTORE, re-arminglast_queuedto the migration timestamp while the task is still sleeping. The later real wakeup (ENQUEUE_DELAYED) tries to re-armlast_queuedat wakeup time but is suppressed becauselast_queuedis already non-zero, sosched_info_arrive()folds the whole sleep duration between migration and wakeup intorun_delay.Load balance is affected too: the
sched_delayedcheck incan_migrate_task()bails out only whenenv->migration_type!=migrate_load, so it does not block migration when the type ismigrate_load- which active load balance always uses (itslb_envleavesmigration_typeat 0 ==migrate_load), and which regular load balance can also use viacalculate_imbalance(). Either way the re-attach goes throughattach_task()->activate_task(rq, p, ENQUEUE_NOCLOCK), withoutENQUEUE_RESTORE.Fix by not re-arming
last_queuedfor asched_delayedtask insched_info_enqueue(). The wakeup path clearssched_delayedbefore reachingsched_info_enqueue(), so it still re-arms at the real wakeup time. Plain runnable tasks are unaffected.Fixes 152e11f6df29 ("sched/fair: Implement delayed dequeue")Reported-by MingTao Huang <[email protected]>Signed-off-by Wei Yang <[email protected]>Reviewed-by Chen Yu <[email protected]>Reviewed-by Kayra Cizmeci <[email protected]>Reviewed-by K Prateek Nayak <[email protected]>Tested-by K Prateek Nayak <[email protected]>---
kernel/sched/stats.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/sched/stats.h +0
-0
kernel/sched/stats.h static void sched_info_arrive(struct rq *rq, struct task_struct *t) · line 295
Quoted 7 lines
kernel/sched/stats.h +1
-1@@ -290,7 +290,7 @@ static void sched_info_arrive(struct rq *rq, struct task_struct *t)*/ static inline void sched_info_enqueue (struct rq *rq,struct task_struct *t) { -if (!t->sched_info.last_queued) +if (!t->sched_info.last_queued && !t->se.sched_delayed) t->sched_info.last_queued =rq_clock (rq); }
Will we not have a similar problem with proxy exec? That is, would t->is_blocked be more appropriate?
Hi Peter,
Quoted 2 lines
Will we not have a similar problem with proxy exec? That is, would
t->is_blockedbe more appropriate?
Yeah.
Only place that sets is_blocked to 1 is try_to_block_task(), which gets only called from __schedule.
Also, we give should_block param. !task_is_blocked(), so
if there are a pointer we return false on try_to_block_task(), so on task_is_blocked() true case, and that means proxy tasks stay in rq. With blocked 1. So yeah, we have a similar problem on proxy exec.
To the second one tho, I think using task_is_blocked() would be better. Since, the current code looks to that for whether or not queue the proxy.
And, on some places is_blocked is cleared after enqueu. My brain stopped working 2 hours ago, this proxy execution stuff combined with delayed fries my brain. So I might be wrong, please correct me if so.
as far as I can tell reading tho. I'm sick of using tho. tho. :-)
Thanks,
Kayra
Hi Peter,
Quoted 2 lines · Peter Zijlstra
Will we not have a similar problem with proxy exec? That is, would
t->is_blockedbe more appropriate?
Yes, there is a similar issue: proxy_migrate_task() re-attaches a blocked donor via activate_task() without ENQUEUE_RESTORE -- a fake enqueue that should not update last_queued. Thanks to Kayra for confirming this and for the task_is_blocked() idea.
But t->is_blocked can't gate it. In ttwu_runnable(), the real wakeup of a delayed task does its enqueue_task(ENQUEUE_DELAYED) inside the if (p->is_blocked) branch, and is_blocked is only cleared afterwards in ttwu_do_wakeup(). So !is_blocked would suppress exactly the re-arm this patch relies on, and run_delay would never be accounted.
task_is_blocked() does look usable for the proxy case: every waker clears blocked_on before waking the task (mutex handoff, ww_mutex die/wound, proxy_needs_return), so it should not be set on a real wakeup enqueue. But it cannot replace se.sched_delayed -- a delayed sleeper has no blocked_on at all -- so it would have to be an additional condition rather than a substitute.
Before adding that I would like to reproduce the proxy case and confirm what the donor's run_delay should look like: whether the time it spent blocked on the mutex ought to be dropped like a delayed sleep, or accounted somehow.
Please shed some light on how the proxy case could be dealt with, or should this patch only focus on the delayed case?
Thanks,
Wei Yang
Hi Peter,
Just a gentle ping in case this got buried -- it's been a few weeks since v3. Happy to change whatever you think is needed.
To recap: proxy exec does seem to have the same re-arm problem, but I don't think t->is_blocked can gate it -- in ttwu_runnable() the real wakeup enqueue sits inside the if (p->is_blocked) branch, while is_blocked is cleared afterwards in ttwu_do_wakeup(), so !is_blocked would suppress the very re-arm this patch relies on. And I don't think task_is_blocked() can replace se.sched_delayed either, since a delayed sleeper has no blocked_on.
One more point: the bug predates proxy exec -- DELAY_DEQUEUE landed in v6.12, proxy exec only in v6.17. Coupling the fix with task_is_blocked() would tie it to infrastructure absent before v6.17, so it could no longer be backported as-is to the v6.12.y..v6.16.y trees where the over-count happens.
So I'd suggest taking this patch as-is (delayed dequeue only) and handling the proxy exec side separately; if you'd rather cover both here, I'm happy to send a v4 with !sched_delayed && !task_is_blocked(), though I'd want to reproduce the proxy case first and re-check Prateek's Tested-by -- just let me know.
Thanks,
Wei Yang