Skip to contentSkip to conversation

sched/eevdf: Minor fixes for reweight_entity()

PATCH Superseded Tianchen Ding 2 participants 7 messages Mar 4, 2024 – Mar 5, 2024 Scheduler

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover sched/eevdf: Minor fixes for reweight_entity()

According to the discussion in [1], fix a calculation bug in reweight_entity().

[1] https://lore.kernel.org/all/[email protected]/

Tianchen Ding (2):
sched/eevdf: Always update V if se->on_rq when reweighting sched/eevdf: Fix miscalculation in reweight_entity() when se is not
curr

 kernel/sched/fair.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
Signature · 1 line
2.39.3
#1 lore raw

PATCH 1/2 sched/eevdf: Always update V if se->on_rq when reweighting

reweight_eevdf() needs the latest V to do accurate calculation for new ve and vd. So update V unconditionally when se is runnable.

---
 kernel/sched/fair.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
kernel/sched/fair.c +2 -3
index 03be0d1330a6..5551ce2af73e 100644
@@ -3790,9 +3790,8 @@ static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
 
 	if (se->on_rq) {
 		/* commit outstanding execution time */
-		if (curr)
-			update_curr(cfs_rq);
-		else
+		update_curr(cfs_rq);
+		if (!curr)
 			__dequeue_entity(cfs_rq, se);
 		update_load_sub(&cfs_rq->load, se->load.weight);
 	}
Signature · 1 line
2.39.3
#2 lore raw

Re: [PATCH 1/2] sched/eevdf: Always update V if se->on_rq when reweighting

On 3/4/24 11:00 AM, Tianchen Ding Wrote:

Quoted 2 lines

reweight_eevdf() needs the latest V to do accurate calculation for new ve and vd. So update V unconditionally when se is runnable.

As this should come along with commit eab03c23c2a1, I think it would be better add a Fixes tag?

Quoted 23 lines
---
  kernel/sched/fair.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
kernel/sched/fair.c +2 -3
index 03be0d1330a6..5551ce2af73e 100644
@@ -3790,9 +3790,8 @@ static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
 
  	if (se->on_rq) {
  		/* commit outstanding execution time */
-		if (curr)
-			update_curr(cfs_rq);
-		else
+		update_curr(cfs_rq);
+		if (!curr)
  			__dequeue_entity(cfs_rq, se);
  		update_load_sub(&cfs_rq->load, se->load.weight);
  	}
#3 lore raw

Re: [PATCH 1/2] sched/eevdf: Always update V if se->on_rq when reweighting

Quoted 7 lines · Abel Wu

On 3/4/24 11:00 AM, Tianchen Ding Wrote:

Quoted 2 lines

reweight_eevdf() needs the latest V to do accurate calculation for new ve and vd. So update V unconditionally when se is runnable.

As this should come along with commit eab03c23c2a1, I think it would be better add a Fixes tag?

I'm not sure. In fact avg_vruntime_sub() in reweight_entity() was first introduced by af4cf40470c2, and at that time the algorithm about reweight was not completed...

#4 lore raw

Re: Re: [PATCH 1/2] sched/eevdf: Always update V if se->on_rq when reweighting

On 3/5/24 10:13 AM, Tianchen Ding Wrote:

Quoted 11 lines · Abel Wu
Quoted 7 lines

On 3/4/24 11:00 AM, Tianchen Ding Wrote:

Quoted 2 lines

reweight_eevdf() needs the latest V to do accurate calculation for new ve and vd. So update V unconditionally when se is runnable.

As this should come along with commit eab03c23c2a1, I think it would be better add a Fixes tag?

I'm not sure. In fact avg_vruntime_sub() in reweight_entity() was first introduced by af4cf40470c2, and at that time the algorithm about reweight was not completed...

It is because before commit eab03c23c2a1, reweight didn't rely on V.

#5 lore raw

PATCH 2/2 sched/eevdf: Fix miscalculation in reweight_entity() when se is not curr

In reply to Tianchen Ding

reweight_eevdf() only keeps V unchanged inside itself. When se != cfs_rq->curr, it would be dequeued from rb tree first. So that V is changed and the result is wrong. Pass the original V to reweight_eevdf() to fix this issue.

Fixes eab03c23c2a1 ("sched/eevdf: Fix vruntime adjustment on reweight")
---
 kernel/sched/fair.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/sched/fair.c +4 -3
index 5551ce2af73e..091a1a750638 100644
@@ -3676,11 +3676,10 @@ static inline void
 dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
 #endif
 
-static void reweight_eevdf(struct cfs_rq *cfs_rq, struct sched_entity *se,
+static void reweight_eevdf(struct sched_entity *se, u64 avruntime,
 			   unsigned long weight)
 {
 	unsigned long old_weight = se->load.weight;
-	u64 avruntime = avg_vruntime(cfs_rq);
 	s64 vlag, vslice;
 
 	/*
@@ -3787,10 +3786,12 @@ static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
 			    unsigned long weight)
 {
 	bool curr = cfs_rq->curr == se;
+	u64 avruntime;
 
 	if (se->on_rq) {
 		/* commit outstanding execution time */
 		update_curr(cfs_rq);
+		avruntime = avg_vruntime(cfs_rq);
 		if (!curr)
 			__dequeue_entity(cfs_rq, se);
 		update_load_sub(&cfs_rq->load, se->load.weight);
@@ -3804,7 +3805,7 @@ static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
 		 */
 		se->vlag = div_s64(se->vlag * se->load.weight, weight);
 	} else {
-		reweight_eevdf(cfs_rq, se, weight);
+		reweight_eevdf(se, avruntime, weight);
 	}
 
 	update_load_set(&se->load, weight);
Signature · 1 line
2.39.3
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] sched/eevdf: Fix miscalculation in reweight_entity() when se is not curr

On 3/4/24 11:00 AM, Tianchen Ding Wrote:

Quoted 42 lines

reweight_eevdf() only keeps V unchanged inside itself. When se != cfs_rq->curr, it would be dequeued from rb tree first. So that V is changed and the result is wrong. Pass the original V to reweight_eevdf() to fix this issue.

Fixes eab03c23c2a1 ("sched/eevdf: Fix vruntime adjustment on reweight")
---
  kernel/sched/fair.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
kernel/sched/fair.c +3 -2
index 5551ce2af73e..091a1a750638 100644
@@ -3676,11 +3676,10 @@ static inline void
  dequeue_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
  #endif
 
-static void reweight_eevdf(struct cfs_rq *cfs_rq, struct sched_entity *se,
+static void reweight_eevdf(struct sched_entity *se, u64 avruntime,
  			   unsigned long weight)
  {
  	unsigned long old_weight = se->load.weight;
-	u64 avruntime = avg_vruntime(cfs_rq);
  	s64 vlag, vslice;
 
  	/*
@@ -3787,10 +3786,12 @@ static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
  			    unsigned long weight)
  {
  	bool curr = cfs_rq->curr == se;
+	u64 avruntime;
 
  	if (se->on_rq) {
  		/* commit outstanding execution time */
  		update_curr(cfs_rq);
+		avruntime = avg_vruntime(cfs_rq);
  		if (!curr)
  			__dequeue_entity(cfs_rq, se);
  		update_load_sub(&cfs_rq->load, se->load.weight);

kernel/sched/fair.c static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, · line 3811

Quoted 8 lines
kernel/sched/fair.c +1 -1
@@ -3804,7 +3805,7 @@ static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
  		 */
  		se->vlag = div_s64(se->vlag * se->load.weight, weight);
  	} else {
-		reweight_eevdf(cfs_rq, se, weight);
+		reweight_eevdf(se, avruntime, weight);
  	}
 
  	update_load_set(&se->load, weight);