The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v4 0/2] sched/fair: Fix fair load state when switching to fair
       [not found] <20260530050609.0D0FB1F00893@smtp.kernel.org>
@ 2026-06-24  9:16 ` Zicheng Qu
  2026-06-24  9:16   ` [PATCH v4 1/2] sched/fair: Rebuild load weight " Zicheng Qu
                     ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Zicheng Qu @ 2026-06-24  9:16 UTC (permalink / raw)
  To: sashiko-bot
  Cc: sashiko-reviews, linux-kernel, sched-ext, peterz, arighi, brho,
	bsegall, changwoo, dietmar.eggemann, haoluo, joshdon, juri.lelli,
	kprateek.nayak, mgorman, mingo, rostedt, tj, vincent.guittot,
	void, vschneid, tanghui20, zhangqiao22, quzicheng, quzicheng315

Tasks outside fair may leave fair's sched_entity load state stale when
they switch back to fair. Patch 1 rebuilds p->se.load from fair's
switching_to hook, as in v3. Patch 2 keeps se->avg.load_avg in sync
with the rebuilt load weight before the entity is attached back to fair.

Changes in v4:
- Add Tejun's Acked-by to patch 1.
- Split the PELT load_avg synchronization into patch 2.

Changes in v3:
- Move the rebuild into fair's switching_to hook, as suggested by Peter.
  This lets fair prepare its own state before enqueue and avoids adding a
  sched_ext/fair-specific fixup to the generic sched_change_end() path.

Changes in v2:
- Move the fix from scx_root_disable() to the class switch path so it also
  covers partial-mode SCHED_EXT to SCHED_NORMAL transitions through
  sched_setscheduler(). Andrea identified this missing case in the v1
  discussion.

Zicheng Qu (2):
  sched/fair: Rebuild load weight when switching to fair
  sched/fair: Keep load_avg in sync after reweighting

 kernel/sched/fair.c | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

-- 
2.53.0

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 1/2] sched/fair: Rebuild load weight when switching to fair
  2026-06-24  9:16 ` [PATCH v4 0/2] sched/fair: Fix fair load state when switching to fair Zicheng Qu
@ 2026-06-24  9:16   ` Zicheng Qu
  2026-06-24  9:16   ` [PATCH v4 2/2] sched/fair: Keep load_avg in sync after reweighting Zicheng Qu
  2026-06-24 11:57   ` [PATCH v4 0/2] sched/fair: Fix fair load state when switching to fair Andrea Righi
  2 siblings, 0 replies; 4+ messages in thread
From: Zicheng Qu @ 2026-06-24  9:16 UTC (permalink / raw)
  To: sashiko-bot
  Cc: sashiko-reviews, linux-kernel, sched-ext, peterz, arighi, brho,
	bsegall, changwoo, dietmar.eggemann, haoluo, joshdon, juri.lelli,
	kprateek.nayak, mgorman, mingo, rostedt, tj, vincent.guittot,
	void, vschneid, tanghui20, zhangqiao22, quzicheng, quzicheng315

Tasks that run outside fair may not keep p->se.load in sync with their
current scheduling policy and static priority. sched_ext, for example,
uses p->scx.weight as the active scheduling weight, so p->se.load can be
stale when a task moves back to fair.

The fair_sched_class expects the sched_entity load weight to be valid
before the task is enqueued. Rebuild it from fair's switching_to hook,
which runs after the class has been changed to fair and before enqueue,
so both sched_ext disable and SCHED_EXT to SCHED_NORMAL transitions get
a native fair load weight.

Fixes: f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Zicheng Qu <quzicheng@huawei.com>
Acked-by: Tejun Heo <tj@kernel.org>
---
Changes in v4:
- No code changes.
- Add Tejun's Acked-by.
- Split the PELT load_avg synchronization into patch 2.

Changes in v3:
- Move the rebuild into fair's switching_to hook, as suggested by Peter.
  This lets fair prepare its own state before enqueue and avoids adding a
  sched_ext/fair-specific fixup to the generic sched_change_end() path.

Changes in v2:
- Move the fix from scx_root_disable() to the class switch path so it also
  covers partial-mode SCHED_EXT to SCHED_NORMAL transitions through
  sched_setscheduler(). Andrea identified this missing case in the v1
  discussion.

 kernel/sched/fair.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index d78467ec6ee1..edb11065a9fc 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -14998,6 +14998,15 @@ static void switching_from_fair(struct rq *rq, struct task_struct *p)
 		dequeue_task(rq, p, DEQUEUE_SLEEP | DEQUEUE_DELAYED | DEQUEUE_NOCLOCK);
 }
 
+static void switching_to_fair(struct rq *rq, struct task_struct *p)
+{
+	/*
+	 * Tasks may come from classes that don't keep se.load up to date.
+	 * Rebuild it before the task is enqueued.
+	 */
+	set_load_weight(p, false);
+}
+
 static void switched_from_fair(struct rq *rq, struct task_struct *p)
 {
 	detach_task_cfs_rq(p);
@@ -15379,6 +15388,7 @@ DEFINE_SCHED_CLASS(fair) = {
 	.reweight_task		= reweight_task_fair,
 	.prio_changed		= prio_changed_fair,
 	.switching_from		= switching_from_fair,
+	.switching_to		= switching_to_fair,
 	.switched_from		= switched_from_fair,
 	.switched_to		= switched_to_fair,
 
-- 
2.53.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v4 2/2] sched/fair: Keep load_avg in sync after reweighting
  2026-06-24  9:16 ` [PATCH v4 0/2] sched/fair: Fix fair load state when switching to fair Zicheng Qu
  2026-06-24  9:16   ` [PATCH v4 1/2] sched/fair: Rebuild load weight " Zicheng Qu
@ 2026-06-24  9:16   ` Zicheng Qu
  2026-06-24 11:57   ` [PATCH v4 0/2] sched/fair: Fix fair load state when switching to fair Andrea Righi
  2 siblings, 0 replies; 4+ messages in thread
From: Zicheng Qu @ 2026-06-24  9:16 UTC (permalink / raw)
  To: sashiko-bot
  Cc: sashiko-reviews, linux-kernel, sched-ext, peterz, arighi, brho,
	bsegall, changwoo, dietmar.eggemann, haoluo, joshdon, juri.lelli,
	kprateek.nayak, mgorman, mingo, rostedt, tj, vincent.guittot,
	void, vschneid, tanghui20, zhangqiao22, quzicheng, quzicheng315

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@smtp.kernel.org/
Signed-off-by: Zicheng Qu <quzicheng@huawei.com>
---
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)
-- 
2.53.0

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 0/2] sched/fair: Fix fair load state when switching to fair
  2026-06-24  9:16 ` [PATCH v4 0/2] sched/fair: Fix fair load state when switching to fair Zicheng Qu
  2026-06-24  9:16   ` [PATCH v4 1/2] sched/fair: Rebuild load weight " Zicheng Qu
  2026-06-24  9:16   ` [PATCH v4 2/2] sched/fair: Keep load_avg in sync after reweighting Zicheng Qu
@ 2026-06-24 11:57   ` Andrea Righi
  2 siblings, 0 replies; 4+ messages in thread
From: Andrea Righi @ 2026-06-24 11:57 UTC (permalink / raw)
  To: Zicheng Qu
  Cc: sashiko-bot, sashiko-reviews, linux-kernel, sched-ext, peterz,
	brho, bsegall, changwoo, dietmar.eggemann, haoluo, joshdon,
	juri.lelli, kprateek.nayak, mgorman, mingo, rostedt, tj,
	vincent.guittot, void, vschneid, tanghui20, zhangqiao22,
	quzicheng315

Hi Zicheng,

On Wed, Jun 24, 2026 at 09:16:46AM +0000, Zicheng Qu wrote:
> Tasks outside fair may leave fair's sched_entity load state stale when
> they switch back to fair. Patch 1 rebuilds p->se.load from fair's
> switching_to hook, as in v3. Patch 2 keeps se->avg.load_avg in sync
> with the rebuilt load weight before the entity is attached back to fair.

Looks good to me.

Acked-by: Andrea Righi <arighi@nvidia.com>

Thanks,
-Andrea

> 
> Changes in v4:
> - Add Tejun's Acked-by to patch 1.
> - Split the PELT load_avg synchronization into patch 2.
> 
> Changes in v3:
> - Move the rebuild into fair's switching_to hook, as suggested by Peter.
>   This lets fair prepare its own state before enqueue and avoids adding a
>   sched_ext/fair-specific fixup to the generic sched_change_end() path.
> 
> Changes in v2:
> - Move the fix from scx_root_disable() to the class switch path so it also
>   covers partial-mode SCHED_EXT to SCHED_NORMAL transitions through
>   sched_setscheduler(). Andrea identified this missing case in the v1
>   discussion.
> 
> Zicheng Qu (2):
>   sched/fair: Rebuild load weight when switching to fair
>   sched/fair: Keep load_avg in sync after reweighting
> 
>  kernel/sched/fair.c | 24 +++++++++++++++++++-----
>  1 file changed, 19 insertions(+), 5 deletions(-)
> 
> -- 
> 2.53.0

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-06-24 11:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20260530050609.0D0FB1F00893@smtp.kernel.org>
2026-06-24  9:16 ` [PATCH v4 0/2] sched/fair: Fix fair load state when switching to fair Zicheng Qu
2026-06-24  9:16   ` [PATCH v4 1/2] sched/fair: Rebuild load weight " Zicheng Qu
2026-06-24  9:16   ` [PATCH v4 2/2] sched/fair: Keep load_avg in sync after reweighting Zicheng Qu
2026-06-24 11:57   ` [PATCH v4 0/2] sched/fair: Fix fair load state when switching to fair Andrea Righi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox