Commit
2f93bcb3f9aaf6ceee1aec0a97d4fe386d48e781
sched/fair: Avoid overflow in place_entity()
place_entity() inflates an entity's virtual lag with
lag * (load + weight) / load
Commit 4823725d9d1d ("sched/fair: Increase weight bits for avg_vruntime")
replaced the scaled-down weights used in this calculation with
avg_vruntime_weight(), removing the arithmetic headroom provided by
scale_load_down().
The multiplication can overflow s64 before the division even when the
final quotient is representable. A task's hierarchical effective weight
can be as small as 2 after avg_vruntime_weight() scaling under group
scheduling, while entity_lag() bounds |vlag| by
calc_delta_fair(cfs_rq_max_slice(cfs_rq) + TICK_NSEC, se)
For example, with HZ=1000, NICE_0_LOAD=1048576, and the minimum scaled
hierarchical weight scale_load(2)=2048, this bound is 891289600. With 114
maximum-weight entities, the runqueue load is 10361604096. The
intermediate product is:
891289600 * (10361604096 + 2)
= 9235189971864780800
> S64_MAX
The signed overflow corrupts the entity's placement.
Rewrite
lag * (load + weight) / load
as
lag + lag * weight / load
The two expressions are equivalent, but the latter avoids multiplying the
virtual lag by the total runqueue weight. entity_lag() bounds vlag against
the entity's effective weight, so the remaining lag * weight product stays
bounded. rescale_entity() preserves this scale across h_load changes and
already relies on the same product when rescaling vlag.
Keep the original zero-load warning semantics by multiplying lag by weight
after replacing the divisor with one.
Fixes: 4823725d9d1d ("sched/fair: Increase weight bits for avg_vruntime")
Signed-off-by: Hui Su <sh_def@163.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20260928124754.1692077-1-sh_def@163.com
Hui Su <sh_def@163.com> · 2026-09-28 12:47 UTC · committed by Peter Zijlstra
1 file changed,
+12
−4
modified kernel/sched/fair.c
| @@ -6463,12 +6463,20 @@ place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) | |||
| 6463 | 6463 | load = cfs_rq->sum_weight; | |
| 6464 | 6464 | if (curr && curr->on_rq) | |
| 6465 | 6465 | load += avg_vruntime_weight(cfs_rq, curr->h_load.weight); | |
| 6466 | - | ||
| 6467 | - | weight = avg_vruntime_weight(cfs_rq, se->h_load.weight); | |
| 6468 | - | lag *= load + weight; | |
| 6469 | 6466 | if (WARN_ON_ONCE(!load)) | |
| 6470 | 6467 | load = 1; | |
| 6471 | - | lag = div64_long(lag, load); | |
| 6468 | + | ||
| 6469 | + | weight = avg_vruntime_weight(cfs_rq, se->h_load.weight); | |
| 6470 | + | ||
| 6471 | + | /* | |
| 6472 | + | * Rewrite, because the product (W + w_i)*vl'_i might | |
| 6473 | + | * overflow s64: | |
| 6474 | + | * | |
| 6475 | + | * vl_i = (W + w_i)*vl'_i / W | |
| 6476 | + | * = (W*vl'_i + w_i*vl'_i) / W | |
| 6477 | + | * = vl'_i + w_i*vl'_i/W | |
| 6478 | + | */ | |
| 6479 | + | lag += div64_long(lag * weight, load); | |
| 6472 | 6480 | ||
| 6473 | 6481 | /* | |
| 6474 | 6482 | * A heavy entity (relative to the tree) will pull the | |