[PATCH] sched/core: Avoid counting sleep time as run delay after migration

From: Wenwu Hou

Date: Wed Nov 25 2026 - 06:56:22 EST


A task with sched_delayed set is sleeping even though it remains on
its runqueue. Migrating such a task calls enqueue_task() on the
destination CPU, where sched_info_enqueue() starts last_queued despite
the task still being asleep. A subsequent wakeup does not replace the
nonzero timestamp, so sched_info_arrive() includes the intervening
sleep in run_delay.

The timestamp can also survive final delayed dequeue: pick_next_entity()
calls __dequeue_task() directly, bypassing sched_info_dequeue(). A later
ordinary wakeup therefore retains the migration timestamp as well.

Reproduce with the Python script [1] on a four-vCPU KVM guest with
DELAY_DEQUEUE enabled.
With competing loads on CPU0 and CPU1, a worker runs on CPU0 for 8 ms
and sleeps for 40 ms. Migrate it to CPU1 while sleeping on alternate
trials, and compare run_delay against trials without migration.

Output excerpts from python3 repro.py:

Before this patch:
worker=697, CPUs=[0, 1, 2]
trial=00 migrate=True state=S run_delay_delta_ms=40.259
trial=01 migrate=False state=S run_delay_delta_ms=0.223
trial=02 migrate=True state=S run_delay_delta_ms=40.194
trial=03 migrate=False state=S run_delay_delta_ms=0.229
trial=04 migrate=True state=S run_delay_delta_ms=40.202
trial=05 migrate=False state=S run_delay_delta_ms=0.230

After this patch:
worker=350, CPUs=[0, 1, 2]
trial=00 migrate=True state=S run_delay_delta_ms=0.005
trial=01 migrate=False state=S run_delay_delta_ms=0.225
trial=02 migrate=True state=S run_delay_delta_ms=0.620
trial=03 migrate=False state=S run_delay_delta_ms=0.232
trial=04 migrate=True state=S run_delay_delta_ms=0.236
trial=05 migrate=False state=S run_delay_delta_ms=0.234

Across 15 trials per group, migrated-task run_delay increments fall
from 40.167-40.259 ms to 0.005-0.624 ms. Controls measure
0.223-0.234 ms before and 0.201-0.620 ms after.

Link: https://gist.github.com/hwenwur/61cf383f8df72d4270ad9918b1d4edc8 [1]
Fixes: 152e11f6df29 ("sched/fair: Implement delayed dequeue")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Wenwu Hou <hwenwur@xxxxxxxxx>
---
kernel/sched/core.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 515b9d0353ce9..043885c66365e 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -2198,7 +2198,8 @@ void enqueue_task(struct rq *rq, struct task_struct *p, int flags)

psi_enqueue(p, flags);

- if (!(flags & ENQUEUE_RESTORE))
+ /* Delayed tasks remain asleep across migration requeues. */
+ if (!(flags & ENQUEUE_RESTORE) && !p->se.sched_delayed)
sched_info_enqueue(rq, p);

if (sched_core_enabled(rq))
--
2.43.0