sched: Fix incorrect sched_stat_wait statistics for rt and dl
PATCH 1/1 sched: Fix incorrect sched_stat_wait statistics for rt and dl
From: Liang Luo <[email protected]>
The update_stats_wait_start_*() wrappers only record wait_start while schedstat_enabled(). With schedstats disabled by default, a task enqueued before they are enabled at runtime has a zero wait_start, and its next __update_stats_wait_end() computes the wait time as
rq_clock(rq) - 0
which is the time since boot. The bogus delta is folded into wait_max and wait_sum and passed to trace_sched_stat_wait(). Neither wait_max nor wait_sum ever shrinks, so a single bogus sample corrupts the task's wait statistics until it exits.
Reproduced on mainline with a SCHED_RR task:
$ grep wait_max /proc/<pid>/sched wait_max : 1058533.555050
on a machine with ~1058s of uptime.
The fair class has been immune since commit b9c88f752268 ("sched/fair: Improve the accuracy of sched_stat_wait statistics"), which skips the accounting on a zero wait_start, but the check lives in the fair wrapper. The rt and dl wrappers call __update_stats_wait_end() directly and never picked it up.
Move the check into __update_stats_wait_end() so that every class and call site shares it, and drop the now redundant one from the fair wrapper.
---
An equivalent fix was proposed by Zhang Qiao in 2024 but never merged:
https://lore.kernel.org/r/[email protected]
kernel/sched/fair.c | 9 --------- kernel/sched/stats.c | 12 +++++++++++- 2 files changed, 11 insertions(+), 10 deletions(-)
kernel/sched/fair.c +0 -9
@@ -2116,15 +2116,6 @@ update_stats_wait_end_fair(struct cfs_rq *cfs_rq, struct sched_entity *se) stats =__schedstats_from_se (se); -/* -* When the sched_schedstat changes from 0 to 1, some sched se -* maybe already in the runqueue, the se->statistics.wait_start -* will be 0.So it will let the delta wrong. We need to avoid this -* scenario. -*/ -if (unlikely (!schedstat_val (stats->wait_start))) -return ; -if (entity_is_task (se)) p =task_of (se);
kernel/sched/stats.c +11 -1
@@ -21,7 +21,17 @@ void __update_stats_wait_start(struct rq *rq, struct task_struct *p,void __update_stats_wait_end (struct rq *rq,struct task_struct *p,struct sched_statistics *stats) { -u64 delta =rq_clock (rq) -schedstat_val (stats->wait_start); +u64 delta; + +/* +* Tasks enqueued while schedstats were disabled have a zero +* wait_start: the wait was never recorded. Skip it, as the delta +* against 0 would be the time since boot. +*/ +if (unlikely (!schedstat_val (stats->wait_start))) +return ; + + delta =rq_clock (rq) -schedstat_val (stats->wait_start);if (p) {if (task_on_rq_migrating (p)) {
Signature · 1 line
2.43.0