Compare
dbd674ccf30f66f5eccf637964a93910df8dbf84..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
3 files changed,
+36
−15
modified drivers/power/sequencing/pwrseq-qcom-wcn.c
| @@ -496,8 +496,15 @@ static int pwrseq_qcom_wcn_probe(struct platform_device *pdev) | |||
| 496 | 496 | return dev_err_probe(dev, PTR_ERR(ctx->bt_gpio), | |
| 497 | 497 | "Failed to get the Bluetooth enable GPIO\n"); | |
| 498 | 498 | ||
| 499 | + | /* | |
| 500 | + | * FIXME: This should actually be GPIOD_OUT_LOW, but doing so would | |
| 501 | + | * cause the WLAN power to be toggled, resulting in PCIe link down. | |
| 502 | + | * Since the PCIe controller driver is not handling link down currently, | |
| 503 | + | * the device becomes unusable. So we need to keep this workaround until | |
| 504 | + | * the link down handling is implemented in the controller driver. | |
| 505 | + | */ | |
| 499 | 506 | ctx->wlan_gpio = devm_gpiod_get_optional(dev, "wlan-enable", | |
| 500 | - | GPIOD_OUT_LOW); | |
| 507 | + | GPIOD_ASIS); | |
| 501 | 508 | if (IS_ERR(ctx->wlan_gpio)) | |
| 502 | 509 | return dev_err_probe(dev, PTR_ERR(ctx->wlan_gpio), | |
| 503 | 510 | "Failed to get the WLAN enable GPIO\n"); | |
| @@ -508,6 +515,13 @@ static int pwrseq_qcom_wcn_probe(struct platform_device *pdev) | |||
| 508 | 515 | return dev_err_probe(dev, PTR_ERR(ctx->xo_clk_gpio), | |
| 509 | 516 | "Failed to get the XO_CLK GPIO\n"); | |
| 510 | 517 | ||
| 518 | + | /* | |
| 519 | + | * Set direction to output but keep the current value in order to not | |
| 520 | + | * disable the WLAN module accidentally if it's already powered on. | |
| 521 | + | */ | |
| 522 | + | gpiod_direction_output(ctx->wlan_gpio, | |
| 523 | + | gpiod_get_value_cansleep(ctx->wlan_gpio)); | |
| 524 | + | ||
| 511 | 525 | ctx->clk = devm_clk_get_optional(dev, NULL); | |
| 512 | 526 | if (IS_ERR(ctx->clk)) | |
| 513 | 527 | return dev_err_probe(dev, PTR_ERR(ctx->clk), | |
modified kernel/sched/core.c
| @@ -11327,17 +11327,16 @@ void sched_change_end(struct sched_change_ctx *ctx) | |||
| 11327 | 11327 | if (p->sched_class->switched_to) | |
| 11328 | 11328 | p->sched_class->switched_to(rq, p); | |
| 11329 | 11329 | ||
| 11330 | + | /* | |
| 11331 | + | * Check if the task can preempt the current in case the class | |
| 11332 | + | * was upgraded. Note that none of the switch*_from() methods | |
| 11333 | + | * know the new class and none of the switch*_to() methods know | |
| 11334 | + | * the old class so we must resched here. | |
| 11335 | + | */ | |
| 11336 | + | if (ctx->queued) | |
| 11337 | + | wakeup_preempt(rq, p, 0); | |
| 11338 | + | ||
| 11330 | 11339 | if (ctx->running) { | |
| 11331 | - | /* | |
| 11332 | - | * If this was a class promotion; let the old class | |
| 11333 | - | * know it got preempted. Note that none of the | |
| 11334 | - | * switch*_from() methods know the new class and none | |
| 11335 | - | * of the switch*_to() methods know the old class. | |
| 11336 | - | */ | |
| 11337 | - | if (sched_class_above(p->sched_class, ctx->class)) { | |
| 11338 | - | rq->next_class->wakeup_preempt(rq, p, 0); | |
| 11339 | - | rq->next_class = p->sched_class; | |
| 11340 | - | } | |
| 11341 | 11340 | /* | |
| 11342 | 11341 | * If this was a degradation in class; make sure to | |
| 11343 | 11342 | * reschedule. | |
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 | |