From nobody Wed Dec 17 17:43:19 2025 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 71C1D207A2A for ; Tue, 4 Mar 2025 14:23:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741098229; cv=none; b=pfEbiD3JG0kbs+OGBSIJruhrOhGm9ChvjYamvJtEFvMYCKhMaA3WDGZ4o2Odo2T2cYr+eSPWXp1wCflyeVTbgjgvqeR2Uyg6q5V7qQDzpID1XZl3/xvfCLQDbq1XB7yf7z/CL0cG2hYvQkk4vqpF2r7hH5HhwBwTpVIEoXNillI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741098229; c=relaxed/simple; bh=ePyTVUHpYXKRFzTNY0Q7NWvN8OyPJ3n0fcbMve5YEMM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eaaM710DbPcshkiNVOPxx4L/XTUeo9c5QUtTRs0sheMPiCE8Zhb/VkejUpEhnErq5jS9p1obLElXJA30eRcnnXCSe6oQVXm9yfSZQTHtkbJ3CM2kEnl/4xP8zuu/KBkRK7qfAdypxzLa0URDDN+u+uKqEv4ZEj4bm5DcmA0oyPk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 758D7FEC; Tue, 4 Mar 2025 06:24:00 -0800 (PST) Received: from e130256.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 219013F66E; Tue, 4 Mar 2025 06:23:45 -0800 (PST) From: Hongyan Xia To: Ingo Molnar , Peter Zijlstra , Vincent Guittot , Dietmar Eggemann , Juri Lelli , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider Cc: Morten Rasmussen , Lukasz Luba , Christian Loehle , Pierre Gondois , linux-kernel@vger.kernel.org Subject: [PATCH v2 7/8] sched/uclamp: Propagate negative bias Date: Tue, 4 Mar 2025 14:23:14 +0000 Message-Id: <53749ecebbed9ef59f6f0fea9c8a8daec0733d68.1741091349.git.hongyan.xia2@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: 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" Negative bias is interesting, because dequeuing such a task will actually increase utilization. Solve by applying PELT decay to negative biases as well. This in fact can be implemented easily with some math tricks. Signed-off-by: Hongyan Xia --- kernel/sched/fair.c | 46 +++++++++++++++++++++++++++++++++++++++++++- kernel/sched/sched.h | 4 ++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 68e7b1ac7a57..944953b90297 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -4886,6 +4886,48 @@ static inline unsigned long root_cfs_util_uclamp(str= uct rq *rq) =20 return max(ret, 0L); } + +/* + * Negative biases are tricky. If we remove them right away then dequeuing= a + * uclamp_max task has the interesting effect that dequeuing results in a = higher + * rq utilization. Solve this by applying PELT decay to the bias itself. + * + * Keeping track of a PELT-decayed negative bias is extra overhead. Howeve= r, we + * observe this interesting math property, where y is the decay factor and= p is + * the number of periods elapsed: + * + * util_new =3D util_old * y^p - neg_bias * y^p + * =3D (util_old - neg_bias) * y^p + * + * Therefore, we simply subtract the negative bias from util_avg the momen= t we + * dequeue, then the PELT signal itself is the total of util_avg and the d= ecayed + * negative bias, and we no longer need to track the decayed bias separate= ly. + */ +static void propagate_negative_bias(struct task_struct *p) +{ + if (task_util_bias(p) < 0 && !task_on_rq_migrating(p)) { + unsigned long neg_bias =3D -task_util_bias(p); + struct sched_entity *se =3D &p->se; + + p->se.avg.util_avg_bias =3D 0; + + for_each_sched_entity(se) { + struct sched_avg *sa =3D &se->avg; + u32 divider =3D get_pelt_divider(sa); + + sub_positive(&sa->util_avg, neg_bias); + sub_positive(&sa->util_sum, neg_bias * divider); + sa->util_sum =3D max_t(u32, sa->util_sum, + sa->util_avg * PELT_MIN_DIVIDER); + sa =3D &cfs_rq_of(se)->avg; + divider =3D get_pelt_divider(sa); + sub_positive(&sa->util_avg, neg_bias); + sub_positive(&sa->util_sum, neg_bias * divider); + sa->util_sum =3D max_t(u32, sa->util_sum, + sa->util_avg * PELT_MIN_DIVIDER); + } + } +} #else static inline long task_util_bias(struct task_struct *p) { @@ -7114,8 +7156,10 @@ static int dequeue_entities(struct rq *rq, struct sc= hed_entity *se, int flags) } =20 sub_nr_running(rq, h_nr_queued); - if (p) + if (p) { util_bias_dequeue(rq, p); + propagate_negative_bias(p); + } =20 if (rq_h_nr_queued && !rq->cfs.h_nr_queued) dl_server_stop(&rq->fair_server); diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index f4a82e6cc029..654eede62979 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3431,6 +3431,10 @@ static inline void util_bias_dequeue(struct rq *rq, = struct task_struct *p) { } =20 +static inline void propagate_negative_bias(struct task_struct *p) +{ +} + #endif /* !CONFIG_UCLAMP_TASK */ =20 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ --=20 2.34.1