From nobody Thu Apr 2 20:25:33 2026 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F677336EC9 for ; Thu, 19 Feb 2026 08:11:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771488665; cv=none; b=QFCWF24+lve20o4FxmKj6KOvP13UMrW0LiEK8/AGkZt/tzmKqq8nn2RmZalI2LllY/5B0Gxy7Z+b2Wgbe7VHKx5A8SAMa6i4YKKlRob9aph5Bwkq9XETLIyuduHFh8I/0mwuicsE8m7eq8DRef7wC7nGzn0yR52SaxqqhrSF6dg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771488665; c=relaxed/simple; bh=gzrNG1llBp7ZLD0Wh9zMr3U5iseC+njiW/myQdt+j4A=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=Kg1836gIl4w3issbBwUOFI15OF+Hmvh7xMolUMMgKEmnC2Pl5lLj8esNVa37j1F5xmLhO8CBHP9irWyB1vUiew1Qa0dT4IS7QRmSXv/BF39C00Az1BX/p49YSobzMxCHxteUBcGGDdcag7G6nysG0tCdDGNiIL9V20pYIU1u1I4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=mL5EaJDK; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="mL5EaJDK" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=rq6xgkEkqw3ZQwG3a/Ndw9okxf/nU7AII02lB3PR93I=; b=mL5EaJDKmownbBUm/iWy4Spics wah3PMOotEqjbLVzrxRkCEf7uMsBIdH7XpVYCc72UTrAZ/5zO8j800mk/0veObuIPwlNHUTP7baF3 u4DxQlvkjamXPSK7320iM6hXTCujpnPGEdboKoz3BeTx1uFjvyf4XF/ltgi+Gkvio6at27+NiVhfx uVivl5C1izHiKkOwSfiEEDeZFIsRJCc7ax+EW9jJRfcuhMf7iMksc4xHUIMwgC4BEfd77DIzc56PF /70S6JCz8iYEIyEO8IHlH92Q+CyIdfu6mQ1a7QQxa/19TlHfo72+ayzrArfLuULaTdK7Ndtxe2qht jG9BRHog==; Received: from 2001-1c00-8d85-5700-266e-96ff-fe07-7dcc.cable.dynamic.v6.ziggo.nl ([2001:1c00:8d85:5700:266e:96ff:fe07:7dcc] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1vsz7a-00000007KJ0-0c8w; Thu, 19 Feb 2026 08:10:50 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id D1114303366; Thu, 19 Feb 2026 09:10:48 +0100 (CET) Message-ID: <20260219080625.066102672@infradead.org> User-Agent: quilt/0.68 Date: Thu, 19 Feb 2026 08:58:46 +0100 From: Peter Zijlstra To: mingo@kernel.org Cc: peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, linux-kernel@vger.kernel.org, wangtao554@huawei.com, quzicheng@huawei.com, kprateek.nayak@amd.com, dsmythies@telus.net, shubhang@os.amperecomputing.com Subject: [PATCH v2 6/7] sched/fair: Revert 6d71a9c61604 ("sched/fair: Fix EEVDF entity placement bug causing scheduling lag") References: <20260219075840.162631716@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Zicheng Qu reported that, because avg_vruntime() always includes cfs_rq->curr, when ->on_rq, place_entity() doesn't work right. Specifically, the lag scaling in place_entity() relies on avg_vruntime() being the state *before* placement of the new entity. However in this case avg_vruntime() will actually already include the entity, which breaks things. Also, Zicheng Qu argues that avg_vruntime should be invariant under reweight. IOW commit 6d71a9c61604 ("sched/fair: Fix EEVDF entity placement bug causing scheduling lag") was wrong! The issue reported in 6d71a9c61604 could possibly be explained by rounding artifacts -- notably the extreme weight '2' is outside of the range of avg_vruntime/sum_w_vruntime, since that uses scale_load_down(). By scaling vruntime by the real weight, but accounting it in vruntime with a factor 1024 more, the average moves significantly. However, that is now cured. Tested by reverting 66951e4860d3 ("sched/fair: Fix update_cfs_group() vs DELAY_DEQUEUE") and tracing vruntime and vlag figures again. Reported-by: Zicheng Qu Signed-off-by: Peter Zijlstra (Intel) Tested-by: K Prateek Nayak Tested-by: Shubhang Kaushik Reviewed-by: Vincent Guittot --- kernel/sched/fair.c | 148 +++++++++++++++++++++++++++++++++++++++++++----= ----- 1 file changed, 124 insertions(+), 24 deletions(-) --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -819,17 +819,22 @@ static inline u64 cfs_rq_max_slice(struc * * -r_max < lag < max(r_max, q) */ -static void update_entity_lag(struct cfs_rq *cfs_rq, struct sched_entity *= se) +static s64 entity_lag(struct cfs_rq *cfs_rq, struct sched_entity *se, u64 = avruntime) { u64 max_slice =3D cfs_rq_max_slice(cfs_rq) + TICK_NSEC; s64 vlag, limit; =20 - WARN_ON_ONCE(!se->on_rq); - - vlag =3D avg_vruntime(cfs_rq) - se->vruntime; + vlag =3D avruntime - se->vruntime; limit =3D calc_delta_fair(max_slice, se); =20 - se->vlag =3D clamp(vlag, -limit, limit); + return clamp(vlag, -limit, limit); +} + +static void update_entity_lag(struct cfs_rq *cfs_rq, struct sched_entity *= se) +{ + WARN_ON_ONCE(!se->on_rq); + + se->vlag =3D entity_lag(cfs_rq, se, avg_vruntime(cfs_rq)); } =20 /* @@ -3895,23 +3900,125 @@ dequeue_load_avg(struct cfs_rq *cfs_rq, se_weight(se) * -se->avg.load_sum); } =20 -static void place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, i= nt flags); +static void +rescale_entity(struct sched_entity *se, unsigned long weight, bool rel_vpr= ot) +{ + unsigned long old_weight =3D se->load.weight; + + /* + * VRUNTIME + * -------- + * + * COROLLARY #1: The virtual runtime of the entity needs to be + * adjusted if re-weight at !0-lag point. + * + * Proof: For contradiction assume this is not true, so we can + * re-weight without changing vruntime at !0-lag point. + * + * Weight VRuntime Avg-VRuntime + * before w v V + * after w' v' V' + * + * Since lag needs to be preserved through re-weight: + * + * lag =3D (V - v)*w =3D (V'- v')*w', where v =3D v' + * =3D=3D> V' =3D (V - v)*w/w' + v (1) + * + * Let W be the total weight of the entities before reweight, + * since V' is the new weighted average of entities: + * + * V' =3D (WV + w'v - wv) / (W + w' - w) (2) + * + * by using (1) & (2) we obtain: + * + * (WV + w'v - wv) / (W + w' - w) =3D (V - v)*w/w' + v + * =3D=3D> (WV-Wv+Wv+w'v-wv)/(W+w'-w) =3D (V - v)*w/w' + v + * =3D=3D> (WV - Wv)/(W + w' - w) + v =3D (V - v)*w/w' + v + * =3D=3D> (V - v)*W/(W + w' - w) =3D (V - v)*w/w' (3) + * + * Since we are doing at !0-lag point which means V !=3D v, we + * can simplify (3): + * + * =3D=3D> W / (W + w' - w) =3D w / w' + * =3D=3D> Ww' =3D Ww + ww' - ww + * =3D=3D> W * (w' - w) =3D w * (w' - w) + * =3D=3D> W =3D w (re-weight indicates w' !=3D w) + * + * So the cfs_rq contains only one entity, hence vruntime of + * the entity @v should always equal to the cfs_rq's weighted + * average vruntime @V, which means we will always re-weight + * at 0-lag point, thus breach assumption. Proof completed. + * + * + * COROLLARY #2: Re-weight does NOT affect weighted average + * vruntime of all the entities. + * + * Proof: According to corollary #1, Eq. (1) should be: + * + * (V - v)*w =3D (V' - v')*w' + * =3D=3D> v' =3D V' - (V - v)*w/w' (4) + * + * According to the weighted average formula, we have: + * + * V' =3D (WV - wv + w'v') / (W - w + w') + * =3D (WV - wv + w'(V' - (V - v)w/w')) / (W - w + w') + * =3D (WV - wv + w'V' - Vw + wv) / (W - w + w') + * =3D (WV + w'V' - Vw) / (W - w + w') + * + * =3D=3D> V'*(W - w + w') =3D WV + w'V' - Vw + * =3D=3D> V' * (W - w) =3D (W - w) * V (5) + * + * If the entity is the only one in the cfs_rq, then reweight + * always occurs at 0-lag point, so V won't change. Or else + * there are other entities, hence W !=3D w, then Eq. (5) turns + * into V' =3D V. So V won't change in either case, proof done. + * + * + * So according to corollary #1 & #2, the effect of re-weight + * on vruntime should be: + * + * v' =3D V' - (V - v) * w / w' (4) + * =3D V - (V - v) * w / w' + * =3D V - vl * w / w' + * =3D V - vl' + */ + se->vlag =3D div64_long(se->vlag * old_weight, weight); + + /* + * DEADLINE + * -------- + * + * When the weight changes, the virtual time slope changes and + * we should adjust the relative virtual deadline accordingly. + * + * d' =3D v' + (d - v)*w/w' + * =3D V' - (V - v)*w/w' + (d - v)*w/w' + * =3D V - (V - v)*w/w' + (d - v)*w/w' + * =3D V + (d - V)*w/w' + */ + if (se->rel_deadline) + se->deadline =3D div64_long(se->deadline * old_weight, weight); + + if (rel_vprot) + se->vprot =3D div64_long(se->vprot * old_weight, weight); +} =20 static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, unsigned long weight) { bool curr =3D cfs_rq->curr =3D=3D se; bool rel_vprot =3D false; - u64 vprot; + u64 avruntime =3D 0; =20 if (se->on_rq) { /* commit outstanding execution time */ update_curr(cfs_rq); - update_entity_lag(cfs_rq, se); - se->deadline -=3D se->vruntime; + avruntime =3D avg_vruntime(cfs_rq); + se->vlag =3D entity_lag(cfs_rq, se, avruntime); + se->deadline -=3D avruntime; se->rel_deadline =3D 1; if (curr && protect_slice(se)) { - vprot =3D se->vprot - se->vruntime; + se->vprot -=3D avruntime; rel_vprot =3D true; } =20 @@ -3922,30 +4029,23 @@ static void reweight_entity(struct cfs_r } dequeue_load_avg(cfs_rq, se); =20 - /* - * Because we keep se->vlag =3D V - v_i, while: lag_i =3D w_i*(V - v_i), - * we need to scale se->vlag when w_i changes. - */ - se->vlag =3D div64_long(se->vlag * se->load.weight, weight); - if (se->rel_deadline) - se->deadline =3D div64_long(se->deadline * se->load.weight, weight); - - if (rel_vprot) - vprot =3D div64_long(vprot * se->load.weight, weight); + rescale_entity(se, weight, rel_vprot); =20 update_load_set(&se->load, weight); =20 do { u32 divider =3D get_pelt_divider(&se->avg); - se->avg.load_avg =3D div_u64(se_weight(se) * se->avg.load_sum, divider); } while (0); =20 enqueue_load_avg(cfs_rq, se); if (se->on_rq) { - place_entity(cfs_rq, se, 0); if (rel_vprot) - se->vprot =3D se->vruntime + vprot; + se->vprot +=3D avruntime; + se->deadline +=3D avruntime; + se->rel_deadline =3D 0; + se->vruntime =3D avruntime - se->vlag; + update_load_add(&cfs_rq->load, se->load.weight); if (!curr) __enqueue_entity(cfs_rq, se); @@ -5303,7 +5403,7 @@ place_entity(struct cfs_rq *cfs_rq, stru =20 se->vruntime =3D vruntime - lag; =20 - if (se->rel_deadline) { + if (sched_feat(PLACE_REL_DEADLINE) && se->rel_deadline) { se->deadline +=3D se->vruntime; se->rel_deadline =3D 0; return;