Re: [PATCH v4 2/2] sched/fair: Keep load_avg in sync after reweighting

From: Peter Zijlstra

Date: Fri Dec 04 2026 - 11:51:33 EST


On Wed, Jun 24, 2026 at 09:16:48AM +0000, Zicheng Qu wrote:
> se->avg.load_avg is derived from se->avg.load_sum and se_weight().
> When the fair load weight is rebuilt while switching back to fair,
> load_avg must be rebuilt too before the entity is attached.
>
> Factor the existing reweight_entity() calculation into a helper and use
> it from switching_to_fair() as well.
>
> Link: https://lore.kernel.org/all/20260530050609.0D0FB1F00893@xxxxxxxxxxxxxxx/
> Signed-off-by: Zicheng Qu <quzicheng@xxxxxxxxxx>
> ---
> Changes in v4:
> - New patch.
>
> kernel/sched/fair.c | 14 +++++++++-----
> 1 file changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
> index edb11065a9fc..19270132a506 100644
> --- a/kernel/sched/fair.c
> +++ b/kernel/sched/fair.c
> @@ -4663,6 +4663,13 @@ rescale_entity(struct sched_entity *se, unsigned long weight, bool rel_vprot)
> se->vprot = div64_long(se->vprot * old_weight, weight);
> }
>
> +static inline void update_load_avg_after_reweight(struct sched_entity *se)
> +{
> + u32 divider = get_pelt_divider(&se->avg);
> +
> + se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider);
> +}
> +
> static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
> unsigned long weight)
> {
> @@ -4692,11 +4699,7 @@ static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
> rescale_entity(se, weight, rel_vprot);
>
> update_load_set(&se->load, weight);
> -
> - do {
> - u32 divider = get_pelt_divider(&se->avg);
> - se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider);
> - } while (0);
> + update_load_avg_after_reweight(se);
>
> enqueue_load_avg(cfs_rq, se);
> if (se->on_rq) {
> @@ -15005,6 +15008,7 @@ static void switching_to_fair(struct rq *rq, struct task_struct *p)
> * Rebuild it before the task is enqueued.
> */
> set_load_weight(p, false);
> + update_load_avg_after_reweight(&p->se);
> }
>
> static void switched_from_fair(struct rq *rq, struct task_struct *p)

This being an old patch it no long applies; but while doing the rebase,
I noticed that there is only one other update_load_set() user
(init_tg_cfs_entry). Calling it there should be a no-op, since load_sum
will be 0 at init.

Does it make sense to do something like so?

---
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -4766,6 +4766,13 @@ dequeue_load_avg(struct cfs_rq *cfs_rq,
se_weight(se) * -se->avg.load_sum);
}

+static inline void update_load(struct sched_entity *se, unsigned long load)
+{
+ u32 divider = get_pelt_divider(&se->avg);
+ update_load_set(&se->load, load);
+ se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider);
+}
+
static void
rescale_entity(struct sched_entity *se, unsigned long weight, bool rel_vprot)
{
@@ -4896,7 +4903,7 @@ static void reweight_eevdf(struct cfs_rq

rescale_entity(se, weight, rel_vprot);

- update_load_set(&se->h_load, weight);
+ update_load(se, weight);

if (on_rq) {
if (rel_vprot)
@@ -4923,12 +4930,7 @@ static void reweight_entity(struct cfs_r
}
dequeue_load_avg(cfs_rq, se);

- update_load_set(&se->load, weight);
-
- do {
- u32 divider = get_pelt_divider(&se->avg);
- se->avg.load_avg = div_u64(se_weight(se) * se->avg.load_sum, divider);
- } while (0);
+ update_load(se, weight);

enqueue_load_avg(cfs_rq, se);

@@ -15514,6 +15516,7 @@ static void switching_to_fair(struct rq
* Rebuild it before the task is enqueued.
*/
set_load_weight(p, false);
+ update_load(&p->se, p->se.load.weight);
}

static void switched_from_fair(struct rq *rq, struct task_struct *p)
@@ -15742,7 +15745,7 @@ void init_tg_cfs_entry(struct task_group

se->my_q = cfs_rq;
/* guarantee group entities always have weight */
- update_load_set(&se->load, NICE_0_LOAD);
+ update_load(se, NICE_0_LOAD);
se->parent = parent;
}