Skip to contentSkip to conversation

sched/stats: Fix run_delay over-count for migrated sched_delayed tasks

PATCH In review [email protected] 1 participant 2 messages Oct 24 – Oct 25 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH v4 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.

Fixes 152e11f6df29 ("sched/fair: Implement delayed dequeue")

---

Changes in v4:
- No code change; resending after the proxy exec discussion to resurface the patch.
- Proxy exec has the same re-arm problem, but it is handled separately:
t->is_blocked cannot 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 task_is_blocked() cannot replace se.sched_delayed since a delayed sleeper has no blocked_on. Coupling them would also prevent backporting to v6.12.y..v6.16.y, where the bug exists (DELAY_DEQUEUE, v6.12) but proxy exec does not (v6.17).

v3: https://lore.kernel.org/all/[email protected]/
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
index ebe0a7765f98..dc626f99ffd9 100644
@@ -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
#1 lore raw

Hi Peter,

Following up on the proxy exec discussion from the v3 thread
(https://lore.kernel.org/all/[email protected]/):
I have now reproduced the proxy case.

A userspace mutex is futex based and never sets blocked_on, so I used a small test module exposing a kernel mutex, with the owner pinned to a known CPU. While the donor is blocked, the module reports:

donor: blocked-in-module

    pid=10364 cpu_before=0 cpu_now=1 on_rq=1 state=2
    is_blocked=1 blocked_on=1

i.e. the donor stayed on the runqueue (on_rq=1) as a real proxy donor (is_blocked=1, blocked_on=1) and was moved to the owner's CPU while blocked (cpu_before=0, cpu_now=1).

Measured on a 2 CPU VM, 5 iterations each, hold 200 ms (run_delay is field 2 of /proc/<tid>/schedstat):

  config                           block      run_delay delta   migrated
  ------------------------------   -------    ---------------   --------
  cross-cpu, unfixed               169.9 ms   167.1 ms          5/5
  cross-cpu, v4 (!sched_delayed)   169.9 ms   167.1 ms          5/5
  cross-cpu, + !task_is_blocked()  169.8 ms     0.01 ms         5/5
  same-cpu (control)               167.3 ms     0.00 ms         0/5

So the same ~170 ms wait is charged ~167 ms or ~0 depending only on where the owner happens to run, and v4 alone does not change it -- the two are independent bugs.

The gate does not disable normal accounting either: with the donor runnable and contending for the CPU, run_delay still grows by ~39 ms over an 80 ms window (5/5 iterations, in both the cross-cpu and the same-cpu runs). So it suppresses only the fake enqueue, not real waits.

On your original question: t->is_blocked still cannot be the gate. 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.

task_is_blocked() does work, but only as an additional condition: with !sched_delayed && !task_is_blocked() the proxy case drops from 167.1 ms to 0.01 ms, while the delayed dequeue fix is unaffected, since a delayed sleeper has no blocked_on.

Given the proxy migration only landed in v7.1 while the delayed dequeue bug exists from v6.12, coupling them would either fail to build (v6.12.y..v6.16.y, no proxy exec at all) or add a dependency on code that cannot trigger (v6.17.y..v7.0.y, proxy exec present but no blocked-waiter migration), so I'll send the proxy one as a separate patch on top of v4.

Thanks,
Wei Yang